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 (cur->bi_rw & REQ_SYNC)
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 (!device->name || strcmp(device->name, path)) {
417 name = kstrdup(path, GFP_NOFS);
422 if (device->missing) {
423 fs_devices->missing_devices--;
428 if (found_transid > fs_devices->latest_trans) {
429 fs_devices->latest_devid = devid;
430 fs_devices->latest_trans = found_transid;
432 *fs_devices_ret = fs_devices;
436 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
438 struct btrfs_fs_devices *fs_devices;
439 struct btrfs_device *device;
440 struct btrfs_device *orig_dev;
442 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
444 return ERR_PTR(-ENOMEM);
446 INIT_LIST_HEAD(&fs_devices->devices);
447 INIT_LIST_HEAD(&fs_devices->alloc_list);
448 INIT_LIST_HEAD(&fs_devices->list);
449 mutex_init(&fs_devices->device_list_mutex);
450 fs_devices->latest_devid = orig->latest_devid;
451 fs_devices->latest_trans = orig->latest_trans;
452 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
454 mutex_lock(&orig->device_list_mutex);
455 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
456 device = kzalloc(sizeof(*device), GFP_NOFS);
460 device->name = kstrdup(orig_dev->name, GFP_NOFS);
466 device->devid = orig_dev->devid;
467 device->work.func = pending_bios_fn;
468 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
469 device->barriers = 1;
470 spin_lock_init(&device->io_lock);
471 INIT_LIST_HEAD(&device->dev_list);
472 INIT_LIST_HEAD(&device->dev_alloc_list);
474 list_add(&device->dev_list, &fs_devices->devices);
475 device->fs_devices = fs_devices;
476 fs_devices->num_devices++;
478 mutex_unlock(&orig->device_list_mutex);
481 mutex_unlock(&orig->device_list_mutex);
482 free_fs_devices(fs_devices);
483 return ERR_PTR(-ENOMEM);
486 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
488 struct btrfs_device *device, *next;
490 mutex_lock(&uuid_mutex);
492 mutex_lock(&fs_devices->device_list_mutex);
493 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
494 if (device->in_fs_metadata)
498 close_bdev_exclusive(device->bdev, device->mode);
500 fs_devices->open_devices--;
502 if (device->writeable) {
503 list_del_init(&device->dev_alloc_list);
504 device->writeable = 0;
505 fs_devices->rw_devices--;
507 list_del_init(&device->dev_list);
508 fs_devices->num_devices--;
512 mutex_unlock(&fs_devices->device_list_mutex);
514 if (fs_devices->seed) {
515 fs_devices = fs_devices->seed;
519 mutex_unlock(&uuid_mutex);
523 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
525 struct btrfs_device *device;
527 if (--fs_devices->opened > 0)
530 list_for_each_entry(device, &fs_devices->devices, dev_list) {
532 close_bdev_exclusive(device->bdev, device->mode);
533 fs_devices->open_devices--;
535 if (device->writeable) {
536 list_del_init(&device->dev_alloc_list);
537 fs_devices->rw_devices--;
541 device->writeable = 0;
542 device->in_fs_metadata = 0;
544 WARN_ON(fs_devices->open_devices);
545 WARN_ON(fs_devices->rw_devices);
546 fs_devices->opened = 0;
547 fs_devices->seeding = 0;
552 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
554 struct btrfs_fs_devices *seed_devices = NULL;
557 mutex_lock(&uuid_mutex);
558 ret = __btrfs_close_devices(fs_devices);
559 if (!fs_devices->opened) {
560 seed_devices = fs_devices->seed;
561 fs_devices->seed = NULL;
563 mutex_unlock(&uuid_mutex);
565 while (seed_devices) {
566 fs_devices = seed_devices;
567 seed_devices = fs_devices->seed;
568 __btrfs_close_devices(fs_devices);
569 free_fs_devices(fs_devices);
574 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
575 fmode_t flags, void *holder)
577 struct block_device *bdev;
578 struct list_head *head = &fs_devices->devices;
579 struct btrfs_device *device;
580 struct block_device *latest_bdev = NULL;
581 struct buffer_head *bh;
582 struct btrfs_super_block *disk_super;
583 u64 latest_devid = 0;
584 u64 latest_transid = 0;
589 list_for_each_entry(device, head, dev_list) {
595 bdev = open_bdev_exclusive(device->name, flags, holder);
597 printk(KERN_INFO "open %s failed\n", device->name);
600 set_blocksize(bdev, 4096);
602 bh = btrfs_read_dev_super(bdev);
608 disk_super = (struct btrfs_super_block *)bh->b_data;
609 devid = btrfs_stack_device_id(&disk_super->dev_item);
610 if (devid != device->devid)
613 if (memcmp(device->uuid, disk_super->dev_item.uuid,
617 device->generation = btrfs_super_generation(disk_super);
618 if (!latest_transid || device->generation > latest_transid) {
619 latest_devid = devid;
620 latest_transid = device->generation;
624 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
625 device->writeable = 0;
627 device->writeable = !bdev_read_only(bdev);
632 device->in_fs_metadata = 0;
633 device->mode = flags;
635 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
636 fs_devices->rotating = 1;
638 fs_devices->open_devices++;
639 if (device->writeable) {
640 fs_devices->rw_devices++;
641 list_add(&device->dev_alloc_list,
642 &fs_devices->alloc_list);
649 close_bdev_exclusive(bdev, FMODE_READ);
653 if (fs_devices->open_devices == 0) {
657 fs_devices->seeding = seeding;
658 fs_devices->opened = 1;
659 fs_devices->latest_bdev = latest_bdev;
660 fs_devices->latest_devid = latest_devid;
661 fs_devices->latest_trans = latest_transid;
662 fs_devices->total_rw_bytes = 0;
667 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
668 fmode_t flags, void *holder)
672 mutex_lock(&uuid_mutex);
673 if (fs_devices->opened) {
674 fs_devices->opened++;
677 ret = __btrfs_open_devices(fs_devices, flags, holder);
679 mutex_unlock(&uuid_mutex);
683 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
684 struct btrfs_fs_devices **fs_devices_ret)
686 struct btrfs_super_block *disk_super;
687 struct block_device *bdev;
688 struct buffer_head *bh;
693 mutex_lock(&uuid_mutex);
695 bdev = open_bdev_exclusive(path, flags, holder);
702 ret = set_blocksize(bdev, 4096);
705 bh = btrfs_read_dev_super(bdev);
710 disk_super = (struct btrfs_super_block *)bh->b_data;
711 devid = btrfs_stack_device_id(&disk_super->dev_item);
712 transid = btrfs_super_generation(disk_super);
713 if (disk_super->label[0])
714 printk(KERN_INFO "device label %s ", disk_super->label);
716 /* FIXME, make a readl uuid parser */
717 printk(KERN_INFO "device fsid %llx-%llx ",
718 *(unsigned long long *)disk_super->fsid,
719 *(unsigned long long *)(disk_super->fsid + 8));
721 printk(KERN_CONT "devid %llu transid %llu %s\n",
722 (unsigned long long)devid, (unsigned long long)transid, path);
723 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
727 close_bdev_exclusive(bdev, flags);
729 mutex_unlock(&uuid_mutex);
733 /* helper to account the used device space in the range */
734 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
735 u64 end, u64 *length)
737 struct btrfs_key key;
738 struct btrfs_root *root = device->dev_root;
739 struct btrfs_dev_extent *dev_extent;
740 struct btrfs_path *path;
744 struct extent_buffer *l;
748 if (start >= device->total_bytes)
751 path = btrfs_alloc_path();
756 key.objectid = device->devid;
758 key.type = BTRFS_DEV_EXTENT_KEY;
760 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
764 ret = btrfs_previous_item(root, path, key.objectid, key.type);
771 slot = path->slots[0];
772 if (slot >= btrfs_header_nritems(l)) {
773 ret = btrfs_next_leaf(root, path);
781 btrfs_item_key_to_cpu(l, &key, slot);
783 if (key.objectid < device->devid)
786 if (key.objectid > device->devid)
789 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
792 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
793 extent_end = key.offset + btrfs_dev_extent_length(l,
795 if (key.offset <= start && extent_end > end) {
796 *length = end - start + 1;
798 } else if (key.offset <= start && extent_end > start)
799 *length += extent_end - start;
800 else if (key.offset > start && extent_end <= end)
801 *length += extent_end - key.offset;
802 else if (key.offset > start && key.offset <= end) {
803 *length += end - key.offset + 1;
805 } else if (key.offset > end)
813 btrfs_free_path(path);
818 * find_free_dev_extent - find free space in the specified device
819 * @trans: transaction handler
820 * @device: the device which we search the free space in
821 * @num_bytes: the size of the free space that we need
822 * @start: store the start of the free space.
823 * @len: the size of the free space. that we find, or the size of the max
824 * free space if we don't find suitable free space
826 * this uses a pretty simple search, the expectation is that it is
827 * called very infrequently and that a given device has a small number
830 * @start is used to store the start of the free space if we find. But if we
831 * don't find suitable free space, it will be used to store the start position
832 * of the max free space.
834 * @len is used to store the size of the free space that we find.
835 * But if we don't find suitable free space, it is used to store the size of
836 * the max free space.
838 int find_free_dev_extent(struct btrfs_trans_handle *trans,
839 struct btrfs_device *device, u64 num_bytes,
840 u64 *start, u64 *len)
842 struct btrfs_key key;
843 struct btrfs_root *root = device->dev_root;
844 struct btrfs_dev_extent *dev_extent;
845 struct btrfs_path *path;
851 u64 search_end = device->total_bytes;
854 struct extent_buffer *l;
856 /* FIXME use last free of some kind */
858 /* we don't want to overwrite the superblock on the drive,
859 * so we make sure to start at an offset of at least 1MB
861 search_start = 1024 * 1024;
863 if (root->fs_info->alloc_start + num_bytes <= search_end)
864 search_start = max(root->fs_info->alloc_start, search_start);
866 max_hole_start = search_start;
869 if (search_start >= search_end) {
874 path = btrfs_alloc_path();
881 key.objectid = device->devid;
882 key.offset = search_start;
883 key.type = BTRFS_DEV_EXTENT_KEY;
885 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
889 ret = btrfs_previous_item(root, path, key.objectid, key.type);
896 slot = path->slots[0];
897 if (slot >= btrfs_header_nritems(l)) {
898 ret = btrfs_next_leaf(root, path);
906 btrfs_item_key_to_cpu(l, &key, slot);
908 if (key.objectid < device->devid)
911 if (key.objectid > device->devid)
914 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
917 if (key.offset > search_start) {
918 hole_size = key.offset - search_start;
920 if (hole_size > max_hole_size) {
921 max_hole_start = search_start;
922 max_hole_size = hole_size;
926 * If this free space is greater than which we need,
927 * it must be the max free space that we have found
928 * until now, so max_hole_start must point to the start
929 * of this free space and the length of this free space
930 * is stored in max_hole_size. Thus, we return
931 * max_hole_start and max_hole_size and go back to the
934 if (hole_size >= num_bytes) {
940 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
941 extent_end = key.offset + btrfs_dev_extent_length(l,
943 if (extent_end > search_start)
944 search_start = extent_end;
950 hole_size = search_end- search_start;
951 if (hole_size > max_hole_size) {
952 max_hole_start = search_start;
953 max_hole_size = hole_size;
957 if (hole_size < num_bytes)
963 btrfs_free_path(path);
965 *start = max_hole_start;
967 *len = max_hole_size;
971 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
972 struct btrfs_device *device,
976 struct btrfs_path *path;
977 struct btrfs_root *root = device->dev_root;
978 struct btrfs_key key;
979 struct btrfs_key found_key;
980 struct extent_buffer *leaf = NULL;
981 struct btrfs_dev_extent *extent = NULL;
983 path = btrfs_alloc_path();
987 key.objectid = device->devid;
989 key.type = BTRFS_DEV_EXTENT_KEY;
991 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
993 ret = btrfs_previous_item(root, path, key.objectid,
994 BTRFS_DEV_EXTENT_KEY);
996 leaf = path->nodes[0];
997 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
998 extent = btrfs_item_ptr(leaf, path->slots[0],
999 struct btrfs_dev_extent);
1000 BUG_ON(found_key.offset > start || found_key.offset +
1001 btrfs_dev_extent_length(leaf, extent) < start);
1003 } else if (ret == 0) {
1004 leaf = path->nodes[0];
1005 extent = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_dev_extent);
1010 if (device->bytes_used > 0)
1011 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
1012 ret = btrfs_del_item(trans, root, path);
1015 btrfs_free_path(path);
1019 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1020 struct btrfs_device *device,
1021 u64 chunk_tree, u64 chunk_objectid,
1022 u64 chunk_offset, u64 start, u64 num_bytes)
1025 struct btrfs_path *path;
1026 struct btrfs_root *root = device->dev_root;
1027 struct btrfs_dev_extent *extent;
1028 struct extent_buffer *leaf;
1029 struct btrfs_key key;
1031 WARN_ON(!device->in_fs_metadata);
1032 path = btrfs_alloc_path();
1036 key.objectid = device->devid;
1038 key.type = BTRFS_DEV_EXTENT_KEY;
1039 ret = btrfs_insert_empty_item(trans, root, path, &key,
1043 leaf = path->nodes[0];
1044 extent = btrfs_item_ptr(leaf, path->slots[0],
1045 struct btrfs_dev_extent);
1046 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1047 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1048 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1050 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1051 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1054 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1055 btrfs_mark_buffer_dirty(leaf);
1056 btrfs_free_path(path);
1060 static noinline int find_next_chunk(struct btrfs_root *root,
1061 u64 objectid, u64 *offset)
1063 struct btrfs_path *path;
1065 struct btrfs_key key;
1066 struct btrfs_chunk *chunk;
1067 struct btrfs_key found_key;
1069 path = btrfs_alloc_path();
1072 key.objectid = objectid;
1073 key.offset = (u64)-1;
1074 key.type = BTRFS_CHUNK_ITEM_KEY;
1076 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1082 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1086 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1088 if (found_key.objectid != objectid)
1091 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1092 struct btrfs_chunk);
1093 *offset = found_key.offset +
1094 btrfs_chunk_length(path->nodes[0], chunk);
1099 btrfs_free_path(path);
1103 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1106 struct btrfs_key key;
1107 struct btrfs_key found_key;
1108 struct btrfs_path *path;
1110 root = root->fs_info->chunk_root;
1112 path = btrfs_alloc_path();
1116 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1117 key.type = BTRFS_DEV_ITEM_KEY;
1118 key.offset = (u64)-1;
1120 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1126 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1127 BTRFS_DEV_ITEM_KEY);
1131 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1133 *objectid = found_key.offset + 1;
1137 btrfs_free_path(path);
1142 * the device information is stored in the chunk root
1143 * the btrfs_device struct should be fully filled in
1145 int btrfs_add_device(struct btrfs_trans_handle *trans,
1146 struct btrfs_root *root,
1147 struct btrfs_device *device)
1150 struct btrfs_path *path;
1151 struct btrfs_dev_item *dev_item;
1152 struct extent_buffer *leaf;
1153 struct btrfs_key key;
1156 root = root->fs_info->chunk_root;
1158 path = btrfs_alloc_path();
1162 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1163 key.type = BTRFS_DEV_ITEM_KEY;
1164 key.offset = device->devid;
1166 ret = btrfs_insert_empty_item(trans, root, path, &key,
1171 leaf = path->nodes[0];
1172 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1174 btrfs_set_device_id(leaf, dev_item, device->devid);
1175 btrfs_set_device_generation(leaf, dev_item, 0);
1176 btrfs_set_device_type(leaf, dev_item, device->type);
1177 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1178 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1179 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1180 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1181 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1182 btrfs_set_device_group(leaf, dev_item, 0);
1183 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1184 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1185 btrfs_set_device_start_offset(leaf, dev_item, 0);
1187 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1188 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1189 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1190 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1191 btrfs_mark_buffer_dirty(leaf);
1195 btrfs_free_path(path);
1199 static int btrfs_rm_dev_item(struct btrfs_root *root,
1200 struct btrfs_device *device)
1203 struct btrfs_path *path;
1204 struct btrfs_key key;
1205 struct btrfs_trans_handle *trans;
1207 root = root->fs_info->chunk_root;
1209 path = btrfs_alloc_path();
1213 trans = btrfs_start_transaction(root, 0);
1214 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1215 key.type = BTRFS_DEV_ITEM_KEY;
1216 key.offset = device->devid;
1219 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1228 ret = btrfs_del_item(trans, root, path);
1232 btrfs_free_path(path);
1233 unlock_chunks(root);
1234 btrfs_commit_transaction(trans, root);
1238 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1240 struct btrfs_device *device;
1241 struct btrfs_device *next_device;
1242 struct block_device *bdev;
1243 struct buffer_head *bh = NULL;
1244 struct btrfs_super_block *disk_super;
1251 mutex_lock(&uuid_mutex);
1252 mutex_lock(&root->fs_info->volume_mutex);
1254 all_avail = root->fs_info->avail_data_alloc_bits |
1255 root->fs_info->avail_system_alloc_bits |
1256 root->fs_info->avail_metadata_alloc_bits;
1258 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1259 root->fs_info->fs_devices->num_devices <= 4) {
1260 printk(KERN_ERR "btrfs: unable to go below four devices "
1266 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1267 root->fs_info->fs_devices->num_devices <= 2) {
1268 printk(KERN_ERR "btrfs: unable to go below two "
1269 "devices on raid1\n");
1274 if (strcmp(device_path, "missing") == 0) {
1275 struct list_head *devices;
1276 struct btrfs_device *tmp;
1279 devices = &root->fs_info->fs_devices->devices;
1280 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1281 list_for_each_entry(tmp, devices, dev_list) {
1282 if (tmp->in_fs_metadata && !tmp->bdev) {
1287 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1292 printk(KERN_ERR "btrfs: no missing devices found to "
1297 bdev = open_bdev_exclusive(device_path, FMODE_READ,
1298 root->fs_info->bdev_holder);
1300 ret = PTR_ERR(bdev);
1304 set_blocksize(bdev, 4096);
1305 bh = btrfs_read_dev_super(bdev);
1310 disk_super = (struct btrfs_super_block *)bh->b_data;
1311 devid = btrfs_stack_device_id(&disk_super->dev_item);
1312 dev_uuid = disk_super->dev_item.uuid;
1313 device = btrfs_find_device(root, devid, dev_uuid,
1321 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1322 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1328 if (device->writeable) {
1329 list_del_init(&device->dev_alloc_list);
1330 root->fs_info->fs_devices->rw_devices--;
1333 ret = btrfs_shrink_device(device, 0);
1337 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1341 device->in_fs_metadata = 0;
1344 * the device list mutex makes sure that we don't change
1345 * the device list while someone else is writing out all
1346 * the device supers.
1348 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1349 list_del_init(&device->dev_list);
1350 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1352 device->fs_devices->num_devices--;
1354 if (device->missing)
1355 root->fs_info->fs_devices->missing_devices--;
1357 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1358 struct btrfs_device, dev_list);
1359 if (device->bdev == root->fs_info->sb->s_bdev)
1360 root->fs_info->sb->s_bdev = next_device->bdev;
1361 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1362 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1365 close_bdev_exclusive(device->bdev, device->mode);
1366 device->bdev = NULL;
1367 device->fs_devices->open_devices--;
1370 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1371 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1373 if (device->fs_devices->open_devices == 0) {
1374 struct btrfs_fs_devices *fs_devices;
1375 fs_devices = root->fs_info->fs_devices;
1376 while (fs_devices) {
1377 if (fs_devices->seed == device->fs_devices)
1379 fs_devices = fs_devices->seed;
1381 fs_devices->seed = device->fs_devices->seed;
1382 device->fs_devices->seed = NULL;
1383 __btrfs_close_devices(device->fs_devices);
1384 free_fs_devices(device->fs_devices);
1388 * at this point, the device is zero sized. We want to
1389 * remove it from the devices list and zero out the old super
1391 if (device->writeable) {
1392 /* make sure this device isn't detected as part of
1395 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1396 set_buffer_dirty(bh);
1397 sync_dirty_buffer(bh);
1400 kfree(device->name);
1408 close_bdev_exclusive(bdev, FMODE_READ);
1410 mutex_unlock(&root->fs_info->volume_mutex);
1411 mutex_unlock(&uuid_mutex);
1416 * does all the dirty work required for changing file system's UUID.
1418 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1419 struct btrfs_root *root)
1421 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1422 struct btrfs_fs_devices *old_devices;
1423 struct btrfs_fs_devices *seed_devices;
1424 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1425 struct btrfs_device *device;
1428 BUG_ON(!mutex_is_locked(&uuid_mutex));
1429 if (!fs_devices->seeding)
1432 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1436 old_devices = clone_fs_devices(fs_devices);
1437 if (IS_ERR(old_devices)) {
1438 kfree(seed_devices);
1439 return PTR_ERR(old_devices);
1442 list_add(&old_devices->list, &fs_uuids);
1444 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1445 seed_devices->opened = 1;
1446 INIT_LIST_HEAD(&seed_devices->devices);
1447 INIT_LIST_HEAD(&seed_devices->alloc_list);
1448 mutex_init(&seed_devices->device_list_mutex);
1449 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1450 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1451 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1452 device->fs_devices = seed_devices;
1455 fs_devices->seeding = 0;
1456 fs_devices->num_devices = 0;
1457 fs_devices->open_devices = 0;
1458 fs_devices->seed = seed_devices;
1460 generate_random_uuid(fs_devices->fsid);
1461 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1462 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1463 super_flags = btrfs_super_flags(disk_super) &
1464 ~BTRFS_SUPER_FLAG_SEEDING;
1465 btrfs_set_super_flags(disk_super, super_flags);
1471 * strore the expected generation for seed devices in device items.
1473 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1474 struct btrfs_root *root)
1476 struct btrfs_path *path;
1477 struct extent_buffer *leaf;
1478 struct btrfs_dev_item *dev_item;
1479 struct btrfs_device *device;
1480 struct btrfs_key key;
1481 u8 fs_uuid[BTRFS_UUID_SIZE];
1482 u8 dev_uuid[BTRFS_UUID_SIZE];
1486 path = btrfs_alloc_path();
1490 root = root->fs_info->chunk_root;
1491 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1493 key.type = BTRFS_DEV_ITEM_KEY;
1496 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1500 leaf = path->nodes[0];
1502 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1503 ret = btrfs_next_leaf(root, path);
1508 leaf = path->nodes[0];
1509 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1510 btrfs_release_path(root, path);
1514 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1515 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1516 key.type != BTRFS_DEV_ITEM_KEY)
1519 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1520 struct btrfs_dev_item);
1521 devid = btrfs_device_id(leaf, dev_item);
1522 read_extent_buffer(leaf, dev_uuid,
1523 (unsigned long)btrfs_device_uuid(dev_item),
1525 read_extent_buffer(leaf, fs_uuid,
1526 (unsigned long)btrfs_device_fsid(dev_item),
1528 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1531 if (device->fs_devices->seeding) {
1532 btrfs_set_device_generation(leaf, dev_item,
1533 device->generation);
1534 btrfs_mark_buffer_dirty(leaf);
1542 btrfs_free_path(path);
1546 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1548 struct btrfs_trans_handle *trans;
1549 struct btrfs_device *device;
1550 struct block_device *bdev;
1551 struct list_head *devices;
1552 struct super_block *sb = root->fs_info->sb;
1554 int seeding_dev = 0;
1557 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1560 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
1562 return PTR_ERR(bdev);
1564 if (root->fs_info->fs_devices->seeding) {
1566 down_write(&sb->s_umount);
1567 mutex_lock(&uuid_mutex);
1570 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1571 mutex_lock(&root->fs_info->volume_mutex);
1573 devices = &root->fs_info->fs_devices->devices;
1575 * we have the volume lock, so we don't need the extra
1576 * device list mutex while reading the list here.
1578 list_for_each_entry(device, devices, dev_list) {
1579 if (device->bdev == bdev) {
1585 device = kzalloc(sizeof(*device), GFP_NOFS);
1587 /* we can safely leave the fs_devices entry around */
1592 device->name = kstrdup(device_path, GFP_NOFS);
1593 if (!device->name) {
1599 ret = find_next_devid(root, &device->devid);
1605 trans = btrfs_start_transaction(root, 0);
1608 device->barriers = 1;
1609 device->writeable = 1;
1610 device->work.func = pending_bios_fn;
1611 generate_random_uuid(device->uuid);
1612 spin_lock_init(&device->io_lock);
1613 device->generation = trans->transid;
1614 device->io_width = root->sectorsize;
1615 device->io_align = root->sectorsize;
1616 device->sector_size = root->sectorsize;
1617 device->total_bytes = i_size_read(bdev->bd_inode);
1618 device->disk_total_bytes = device->total_bytes;
1619 device->dev_root = root->fs_info->dev_root;
1620 device->bdev = bdev;
1621 device->in_fs_metadata = 1;
1623 set_blocksize(device->bdev, 4096);
1626 sb->s_flags &= ~MS_RDONLY;
1627 ret = btrfs_prepare_sprout(trans, root);
1631 device->fs_devices = root->fs_info->fs_devices;
1634 * we don't want write_supers to jump in here with our device
1637 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1638 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1639 list_add(&device->dev_alloc_list,
1640 &root->fs_info->fs_devices->alloc_list);
1641 root->fs_info->fs_devices->num_devices++;
1642 root->fs_info->fs_devices->open_devices++;
1643 root->fs_info->fs_devices->rw_devices++;
1644 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1646 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1647 root->fs_info->fs_devices->rotating = 1;
1649 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1650 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1651 total_bytes + device->total_bytes);
1653 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1654 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1656 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1659 ret = init_first_rw_device(trans, root, device);
1661 ret = btrfs_finish_sprout(trans, root);
1664 ret = btrfs_add_device(trans, root, device);
1668 * we've got more storage, clear any full flags on the space
1671 btrfs_clear_space_info_full(root->fs_info);
1673 unlock_chunks(root);
1674 btrfs_commit_transaction(trans, root);
1677 mutex_unlock(&uuid_mutex);
1678 up_write(&sb->s_umount);
1680 ret = btrfs_relocate_sys_chunks(root);
1684 mutex_unlock(&root->fs_info->volume_mutex);
1687 close_bdev_exclusive(bdev, 0);
1689 mutex_unlock(&uuid_mutex);
1690 up_write(&sb->s_umount);
1695 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1696 struct btrfs_device *device)
1699 struct btrfs_path *path;
1700 struct btrfs_root *root;
1701 struct btrfs_dev_item *dev_item;
1702 struct extent_buffer *leaf;
1703 struct btrfs_key key;
1705 root = device->dev_root->fs_info->chunk_root;
1707 path = btrfs_alloc_path();
1711 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1712 key.type = BTRFS_DEV_ITEM_KEY;
1713 key.offset = device->devid;
1715 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1724 leaf = path->nodes[0];
1725 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1727 btrfs_set_device_id(leaf, dev_item, device->devid);
1728 btrfs_set_device_type(leaf, dev_item, device->type);
1729 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1730 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1731 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1732 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1733 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1734 btrfs_mark_buffer_dirty(leaf);
1737 btrfs_free_path(path);
1741 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1742 struct btrfs_device *device, u64 new_size)
1744 struct btrfs_super_block *super_copy =
1745 &device->dev_root->fs_info->super_copy;
1746 u64 old_total = btrfs_super_total_bytes(super_copy);
1747 u64 diff = new_size - device->total_bytes;
1749 if (!device->writeable)
1751 if (new_size <= device->total_bytes)
1754 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1755 device->fs_devices->total_rw_bytes += diff;
1757 device->total_bytes = new_size;
1758 device->disk_total_bytes = new_size;
1759 btrfs_clear_space_info_full(device->dev_root->fs_info);
1761 return btrfs_update_device(trans, device);
1764 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1765 struct btrfs_device *device, u64 new_size)
1768 lock_chunks(device->dev_root);
1769 ret = __btrfs_grow_device(trans, device, new_size);
1770 unlock_chunks(device->dev_root);
1774 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1775 struct btrfs_root *root,
1776 u64 chunk_tree, u64 chunk_objectid,
1780 struct btrfs_path *path;
1781 struct btrfs_key key;
1783 root = root->fs_info->chunk_root;
1784 path = btrfs_alloc_path();
1788 key.objectid = chunk_objectid;
1789 key.offset = chunk_offset;
1790 key.type = BTRFS_CHUNK_ITEM_KEY;
1792 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1795 ret = btrfs_del_item(trans, root, path);
1798 btrfs_free_path(path);
1802 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1805 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1806 struct btrfs_disk_key *disk_key;
1807 struct btrfs_chunk *chunk;
1814 struct btrfs_key key;
1816 array_size = btrfs_super_sys_array_size(super_copy);
1818 ptr = super_copy->sys_chunk_array;
1821 while (cur < array_size) {
1822 disk_key = (struct btrfs_disk_key *)ptr;
1823 btrfs_disk_key_to_cpu(&key, disk_key);
1825 len = sizeof(*disk_key);
1827 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1828 chunk = (struct btrfs_chunk *)(ptr + len);
1829 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1830 len += btrfs_chunk_item_size(num_stripes);
1835 if (key.objectid == chunk_objectid &&
1836 key.offset == chunk_offset) {
1837 memmove(ptr, ptr + len, array_size - (cur + len));
1839 btrfs_set_super_sys_array_size(super_copy, array_size);
1848 static int btrfs_relocate_chunk(struct btrfs_root *root,
1849 u64 chunk_tree, u64 chunk_objectid,
1852 struct extent_map_tree *em_tree;
1853 struct btrfs_root *extent_root;
1854 struct btrfs_trans_handle *trans;
1855 struct extent_map *em;
1856 struct map_lookup *map;
1860 root = root->fs_info->chunk_root;
1861 extent_root = root->fs_info->extent_root;
1862 em_tree = &root->fs_info->mapping_tree.map_tree;
1864 ret = btrfs_can_relocate(extent_root, chunk_offset);
1868 /* step one, relocate all the extents inside this chunk */
1869 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1873 trans = btrfs_start_transaction(root, 0);
1879 * step two, delete the device extents and the
1880 * chunk tree entries
1882 read_lock(&em_tree->lock);
1883 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1884 read_unlock(&em_tree->lock);
1886 BUG_ON(em->start > chunk_offset ||
1887 em->start + em->len < chunk_offset);
1888 map = (struct map_lookup *)em->bdev;
1890 for (i = 0; i < map->num_stripes; i++) {
1891 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1892 map->stripes[i].physical);
1895 if (map->stripes[i].dev) {
1896 ret = btrfs_update_device(trans, map->stripes[i].dev);
1900 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1905 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1906 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1910 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1913 write_lock(&em_tree->lock);
1914 remove_extent_mapping(em_tree, em);
1915 write_unlock(&em_tree->lock);
1920 /* once for the tree */
1921 free_extent_map(em);
1923 free_extent_map(em);
1925 unlock_chunks(root);
1926 btrfs_end_transaction(trans, root);
1930 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1932 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1933 struct btrfs_path *path;
1934 struct extent_buffer *leaf;
1935 struct btrfs_chunk *chunk;
1936 struct btrfs_key key;
1937 struct btrfs_key found_key;
1938 u64 chunk_tree = chunk_root->root_key.objectid;
1940 bool retried = false;
1944 path = btrfs_alloc_path();
1949 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1950 key.offset = (u64)-1;
1951 key.type = BTRFS_CHUNK_ITEM_KEY;
1954 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1959 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1966 leaf = path->nodes[0];
1967 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1969 chunk = btrfs_item_ptr(leaf, path->slots[0],
1970 struct btrfs_chunk);
1971 chunk_type = btrfs_chunk_type(leaf, chunk);
1972 btrfs_release_path(chunk_root, path);
1974 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1975 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1984 if (found_key.offset == 0)
1986 key.offset = found_key.offset - 1;
1989 if (failed && !retried) {
1993 } else if (failed && retried) {
1998 btrfs_free_path(path);
2002 static u64 div_factor(u64 num, int factor)
2011 int btrfs_balance(struct btrfs_root *dev_root)
2014 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2015 struct btrfs_device *device;
2018 struct btrfs_path *path;
2019 struct btrfs_key key;
2020 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2021 struct btrfs_trans_handle *trans;
2022 struct btrfs_key found_key;
2024 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2027 mutex_lock(&dev_root->fs_info->volume_mutex);
2028 dev_root = dev_root->fs_info->dev_root;
2030 /* step one make some room on all the devices */
2031 list_for_each_entry(device, devices, dev_list) {
2032 old_size = device->total_bytes;
2033 size_to_free = div_factor(old_size, 1);
2034 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2035 if (!device->writeable ||
2036 device->total_bytes - device->bytes_used > size_to_free)
2039 ret = btrfs_shrink_device(device, old_size - size_to_free);
2044 trans = btrfs_start_transaction(dev_root, 0);
2047 ret = btrfs_grow_device(trans, device, old_size);
2050 btrfs_end_transaction(trans, dev_root);
2053 /* step two, relocate all the chunks */
2054 path = btrfs_alloc_path();
2057 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2058 key.offset = (u64)-1;
2059 key.type = BTRFS_CHUNK_ITEM_KEY;
2062 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2067 * this shouldn't happen, it means the last relocate
2073 ret = btrfs_previous_item(chunk_root, path, 0,
2074 BTRFS_CHUNK_ITEM_KEY);
2078 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2080 if (found_key.objectid != key.objectid)
2083 /* chunk zero is special */
2084 if (found_key.offset == 0)
2087 btrfs_release_path(chunk_root, path);
2088 ret = btrfs_relocate_chunk(chunk_root,
2089 chunk_root->root_key.objectid,
2092 BUG_ON(ret && ret != -ENOSPC);
2093 key.offset = found_key.offset - 1;
2097 btrfs_free_path(path);
2098 mutex_unlock(&dev_root->fs_info->volume_mutex);
2103 * shrinking a device means finding all of the device extents past
2104 * the new size, and then following the back refs to the chunks.
2105 * The chunk relocation code actually frees the device extent
2107 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2109 struct btrfs_trans_handle *trans;
2110 struct btrfs_root *root = device->dev_root;
2111 struct btrfs_dev_extent *dev_extent = NULL;
2112 struct btrfs_path *path;
2120 bool retried = false;
2121 struct extent_buffer *l;
2122 struct btrfs_key key;
2123 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2124 u64 old_total = btrfs_super_total_bytes(super_copy);
2125 u64 old_size = device->total_bytes;
2126 u64 diff = device->total_bytes - new_size;
2128 if (new_size >= device->total_bytes)
2131 path = btrfs_alloc_path();
2139 device->total_bytes = new_size;
2140 if (device->writeable)
2141 device->fs_devices->total_rw_bytes -= diff;
2142 unlock_chunks(root);
2145 key.objectid = device->devid;
2146 key.offset = (u64)-1;
2147 key.type = BTRFS_DEV_EXTENT_KEY;
2150 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2154 ret = btrfs_previous_item(root, path, 0, key.type);
2159 btrfs_release_path(root, path);
2164 slot = path->slots[0];
2165 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2167 if (key.objectid != device->devid) {
2168 btrfs_release_path(root, path);
2172 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2173 length = btrfs_dev_extent_length(l, dev_extent);
2175 if (key.offset + length <= new_size) {
2176 btrfs_release_path(root, path);
2180 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2181 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2182 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2183 btrfs_release_path(root, path);
2185 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2187 if (ret && ret != -ENOSPC)
2194 if (failed && !retried) {
2198 } else if (failed && retried) {
2202 device->total_bytes = old_size;
2203 if (device->writeable)
2204 device->fs_devices->total_rw_bytes += diff;
2205 unlock_chunks(root);
2209 /* Shrinking succeeded, else we would be at "done". */
2210 trans = btrfs_start_transaction(root, 0);
2213 device->disk_total_bytes = new_size;
2214 /* Now btrfs_update_device() will change the on-disk size. */
2215 ret = btrfs_update_device(trans, device);
2217 unlock_chunks(root);
2218 btrfs_end_transaction(trans, root);
2221 WARN_ON(diff > old_total);
2222 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2223 unlock_chunks(root);
2224 btrfs_end_transaction(trans, root);
2226 btrfs_free_path(path);
2230 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2231 struct btrfs_root *root,
2232 struct btrfs_key *key,
2233 struct btrfs_chunk *chunk, int item_size)
2235 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2236 struct btrfs_disk_key disk_key;
2240 array_size = btrfs_super_sys_array_size(super_copy);
2241 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2244 ptr = super_copy->sys_chunk_array + array_size;
2245 btrfs_cpu_key_to_disk(&disk_key, key);
2246 memcpy(ptr, &disk_key, sizeof(disk_key));
2247 ptr += sizeof(disk_key);
2248 memcpy(ptr, chunk, item_size);
2249 item_size += sizeof(disk_key);
2250 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2254 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
2255 int num_stripes, int sub_stripes)
2257 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2259 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2260 return calc_size * (num_stripes / sub_stripes);
2262 return calc_size * num_stripes;
2265 /* Used to sort the devices by max_avail(descending sort) */
2266 int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
2268 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2269 ((struct btrfs_device_info *)dev_info2)->max_avail)
2271 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2272 ((struct btrfs_device_info *)dev_info2)->max_avail)
2278 static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2279 int *num_stripes, int *min_stripes,
2286 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2287 *num_stripes = fs_devices->rw_devices;
2290 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2294 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2295 if (fs_devices->rw_devices < 2)
2300 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2301 *num_stripes = fs_devices->rw_devices;
2302 if (*num_stripes < 4)
2304 *num_stripes &= ~(u32)1;
2312 static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2313 u64 proposed_size, u64 type,
2314 int num_stripes, int small_stripe)
2316 int min_stripe_size = 1 * 1024 * 1024;
2317 u64 calc_size = proposed_size;
2318 u64 max_chunk_size = calc_size;
2321 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2322 BTRFS_BLOCK_GROUP_DUP |
2323 BTRFS_BLOCK_GROUP_RAID10))
2326 if (type & BTRFS_BLOCK_GROUP_DATA) {
2327 max_chunk_size = 10 * calc_size;
2328 min_stripe_size = 64 * 1024 * 1024;
2329 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2330 max_chunk_size = 256 * 1024 * 1024;
2331 min_stripe_size = 32 * 1024 * 1024;
2332 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2333 calc_size = 8 * 1024 * 1024;
2334 max_chunk_size = calc_size * 2;
2335 min_stripe_size = 1 * 1024 * 1024;
2338 /* we don't want a chunk larger than 10% of writeable space */
2339 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2342 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2343 calc_size = max_chunk_size * ncopies;
2344 do_div(calc_size, num_stripes);
2345 do_div(calc_size, BTRFS_STRIPE_LEN);
2346 calc_size *= BTRFS_STRIPE_LEN;
2349 /* we don't want tiny stripes */
2351 calc_size = max_t(u64, min_stripe_size, calc_size);
2354 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
2355 * we end up with something bigger than a stripe
2357 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
2359 do_div(calc_size, BTRFS_STRIPE_LEN);
2360 calc_size *= BTRFS_STRIPE_LEN;
2365 static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2368 struct map_lookup *new;
2369 size_t len = map_lookup_size(num_stripes);
2371 BUG_ON(map->num_stripes < num_stripes);
2373 if (map->num_stripes == num_stripes)
2376 new = kmalloc(len, GFP_NOFS);
2378 /* just change map->num_stripes */
2379 map->num_stripes = num_stripes;
2383 memcpy(new, map, len);
2384 new->num_stripes = num_stripes;
2390 * helper to allocate device space from btrfs_device_info, in which we stored
2391 * max free space information of every device. It is used when we can not
2392 * allocate chunks by default size.
2394 * By this helper, we can allocate a new chunk as larger as possible.
2396 static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2397 struct btrfs_fs_devices *fs_devices,
2398 struct btrfs_device_info *devices,
2399 int nr_device, u64 type,
2400 struct map_lookup **map_lookup,
2401 int min_stripes, u64 *stripe_size)
2403 int i, index, sort_again = 0;
2404 int min_devices = min_stripes;
2405 u64 max_avail, min_free;
2406 struct map_lookup *map = *map_lookup;
2409 if (nr_device < min_stripes)
2412 btrfs_descending_sort_devices(devices, nr_device);
2414 max_avail = devices[0].max_avail;
2418 for (i = 0; i < nr_device; i++) {
2420 * if dev_offset = 0, it means the free space of this device
2421 * is less than what we need, and we didn't search max avail
2422 * extent on this device, so do it now.
2424 if (!devices[i].dev_offset) {
2425 ret = find_free_dev_extent(trans, devices[i].dev,
2427 &devices[i].dev_offset,
2428 &devices[i].max_avail);
2429 if (ret != 0 && ret != -ENOSPC)
2435 /* we update the max avail free extent of each devices, sort again */
2437 btrfs_descending_sort_devices(devices, nr_device);
2439 if (type & BTRFS_BLOCK_GROUP_DUP)
2442 if (!devices[min_devices - 1].max_avail)
2445 max_avail = devices[min_devices - 1].max_avail;
2446 if (type & BTRFS_BLOCK_GROUP_DUP)
2447 do_div(max_avail, 2);
2449 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2451 if (type & BTRFS_BLOCK_GROUP_DUP)
2452 min_free = max_avail * 2;
2454 min_free = max_avail;
2456 if (min_free > devices[min_devices - 1].max_avail)
2459 map = __shrink_map_lookup_stripes(map, min_stripes);
2460 *stripe_size = max_avail;
2463 for (i = 0; i < min_stripes; i++) {
2464 map->stripes[i].dev = devices[index].dev;
2465 map->stripes[i].physical = devices[index].dev_offset;
2466 if (type & BTRFS_BLOCK_GROUP_DUP) {
2468 map->stripes[i].dev = devices[index].dev;
2469 map->stripes[i].physical = devices[index].dev_offset +
2479 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2480 struct btrfs_root *extent_root,
2481 struct map_lookup **map_ret,
2482 u64 *num_bytes, u64 *stripe_size,
2483 u64 start, u64 type)
2485 struct btrfs_fs_info *info = extent_root->fs_info;
2486 struct btrfs_device *device = NULL;
2487 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2488 struct list_head *cur;
2489 struct map_lookup *map;
2490 struct extent_map_tree *em_tree;
2491 struct extent_map *em;
2492 struct btrfs_device_info *devices_info;
2493 struct list_head private_devs;
2494 u64 calc_size = 1024 * 1024 * 1024;
2501 int min_devices; /* the min number of devices we need */
2506 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2507 (type & BTRFS_BLOCK_GROUP_DUP)) {
2509 type &= ~BTRFS_BLOCK_GROUP_DUP;
2511 if (list_empty(&fs_devices->alloc_list))
2514 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2515 &min_stripes, &sub_stripes);
2519 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2524 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2529 map->num_stripes = num_stripes;
2531 cur = fs_devices->alloc_list.next;
2535 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2538 if (type & BTRFS_BLOCK_GROUP_DUP) {
2539 min_free = calc_size * 2;
2542 min_free = calc_size;
2543 min_devices = min_stripes;
2546 INIT_LIST_HEAD(&private_devs);
2547 while (index < num_stripes) {
2548 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2549 BUG_ON(!device->writeable);
2550 if (device->total_bytes > device->bytes_used)
2551 avail = device->total_bytes - device->bytes_used;
2556 if (device->in_fs_metadata && avail >= min_free) {
2557 ret = find_free_dev_extent(trans, device, min_free,
2558 &devices_info[i].dev_offset,
2559 &devices_info[i].max_avail);
2561 list_move_tail(&device->dev_alloc_list,
2563 map->stripes[index].dev = device;
2564 map->stripes[index].physical =
2565 devices_info[i].dev_offset;
2567 if (type & BTRFS_BLOCK_GROUP_DUP) {
2568 map->stripes[index].dev = device;
2569 map->stripes[index].physical =
2570 devices_info[i].dev_offset +
2574 } else if (ret != -ENOSPC)
2577 devices_info[i].dev = device;
2579 } else if (device->in_fs_metadata &&
2580 avail >= BTRFS_STRIPE_LEN) {
2581 devices_info[i].dev = device;
2582 devices_info[i].max_avail = avail;
2586 if (cur == &fs_devices->alloc_list)
2590 list_splice(&private_devs, &fs_devices->alloc_list);
2591 if (index < num_stripes) {
2592 if (index >= min_stripes) {
2593 num_stripes = index;
2594 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2595 num_stripes /= sub_stripes;
2596 num_stripes *= sub_stripes;
2599 map = __shrink_map_lookup_stripes(map, num_stripes);
2600 } else if (i >= min_devices) {
2601 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2602 devices_info, i, type,
2612 map->sector_size = extent_root->sectorsize;
2613 map->stripe_len = BTRFS_STRIPE_LEN;
2614 map->io_align = BTRFS_STRIPE_LEN;
2615 map->io_width = BTRFS_STRIPE_LEN;
2617 map->sub_stripes = sub_stripes;
2620 *stripe_size = calc_size;
2621 *num_bytes = chunk_bytes_by_type(type, calc_size,
2622 map->num_stripes, sub_stripes);
2624 em = alloc_extent_map(GFP_NOFS);
2629 em->bdev = (struct block_device *)map;
2631 em->len = *num_bytes;
2632 em->block_start = 0;
2633 em->block_len = em->len;
2635 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2636 write_lock(&em_tree->lock);
2637 ret = add_extent_mapping(em_tree, em);
2638 write_unlock(&em_tree->lock);
2640 free_extent_map(em);
2642 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2643 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2648 while (index < map->num_stripes) {
2649 device = map->stripes[index].dev;
2650 dev_offset = map->stripes[index].physical;
2652 ret = btrfs_alloc_dev_extent(trans, device,
2653 info->chunk_root->root_key.objectid,
2654 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2655 start, dev_offset, calc_size);
2660 kfree(devices_info);
2665 kfree(devices_info);
2669 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2670 struct btrfs_root *extent_root,
2671 struct map_lookup *map, u64 chunk_offset,
2672 u64 chunk_size, u64 stripe_size)
2675 struct btrfs_key key;
2676 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2677 struct btrfs_device *device;
2678 struct btrfs_chunk *chunk;
2679 struct btrfs_stripe *stripe;
2680 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2684 chunk = kzalloc(item_size, GFP_NOFS);
2689 while (index < map->num_stripes) {
2690 device = map->stripes[index].dev;
2691 device->bytes_used += stripe_size;
2692 ret = btrfs_update_device(trans, device);
2698 stripe = &chunk->stripe;
2699 while (index < map->num_stripes) {
2700 device = map->stripes[index].dev;
2701 dev_offset = map->stripes[index].physical;
2703 btrfs_set_stack_stripe_devid(stripe, device->devid);
2704 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2705 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2710 btrfs_set_stack_chunk_length(chunk, chunk_size);
2711 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2712 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2713 btrfs_set_stack_chunk_type(chunk, map->type);
2714 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2715 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2716 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2717 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2718 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2720 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2721 key.type = BTRFS_CHUNK_ITEM_KEY;
2722 key.offset = chunk_offset;
2724 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2727 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2728 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2737 * Chunk allocation falls into two parts. The first part does works
2738 * that make the new allocated chunk useable, but not do any operation
2739 * that modifies the chunk tree. The second part does the works that
2740 * require modifying the chunk tree. This division is important for the
2741 * bootstrap process of adding storage to a seed btrfs.
2743 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2744 struct btrfs_root *extent_root, u64 type)
2749 struct map_lookup *map;
2750 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2753 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2758 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2759 &stripe_size, chunk_offset, type);
2763 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2764 chunk_size, stripe_size);
2769 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2770 struct btrfs_root *root,
2771 struct btrfs_device *device)
2774 u64 sys_chunk_offset;
2778 u64 sys_stripe_size;
2780 struct map_lookup *map;
2781 struct map_lookup *sys_map;
2782 struct btrfs_fs_info *fs_info = root->fs_info;
2783 struct btrfs_root *extent_root = fs_info->extent_root;
2786 ret = find_next_chunk(fs_info->chunk_root,
2787 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2790 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2791 (fs_info->metadata_alloc_profile &
2792 fs_info->avail_metadata_alloc_bits);
2793 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2795 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2796 &stripe_size, chunk_offset, alloc_profile);
2799 sys_chunk_offset = chunk_offset + chunk_size;
2801 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2802 (fs_info->system_alloc_profile &
2803 fs_info->avail_system_alloc_bits);
2804 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2806 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2807 &sys_chunk_size, &sys_stripe_size,
2808 sys_chunk_offset, alloc_profile);
2811 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2815 * Modifying chunk tree needs allocating new blocks from both
2816 * system block group and metadata block group. So we only can
2817 * do operations require modifying the chunk tree after both
2818 * block groups were created.
2820 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2821 chunk_size, stripe_size);
2824 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2825 sys_chunk_offset, sys_chunk_size,
2831 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2833 struct extent_map *em;
2834 struct map_lookup *map;
2835 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2839 read_lock(&map_tree->map_tree.lock);
2840 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2841 read_unlock(&map_tree->map_tree.lock);
2845 if (btrfs_test_opt(root, DEGRADED)) {
2846 free_extent_map(em);
2850 map = (struct map_lookup *)em->bdev;
2851 for (i = 0; i < map->num_stripes; i++) {
2852 if (!map->stripes[i].dev->writeable) {
2857 free_extent_map(em);
2861 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2863 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2866 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2868 struct extent_map *em;
2871 write_lock(&tree->map_tree.lock);
2872 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2874 remove_extent_mapping(&tree->map_tree, em);
2875 write_unlock(&tree->map_tree.lock);
2880 free_extent_map(em);
2881 /* once for the tree */
2882 free_extent_map(em);
2886 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2888 struct extent_map *em;
2889 struct map_lookup *map;
2890 struct extent_map_tree *em_tree = &map_tree->map_tree;
2893 read_lock(&em_tree->lock);
2894 em = lookup_extent_mapping(em_tree, logical, len);
2895 read_unlock(&em_tree->lock);
2898 BUG_ON(em->start > logical || em->start + em->len < logical);
2899 map = (struct map_lookup *)em->bdev;
2900 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2901 ret = map->num_stripes;
2902 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2903 ret = map->sub_stripes;
2906 free_extent_map(em);
2910 static int find_live_mirror(struct map_lookup *map, int first, int num,
2914 if (map->stripes[optimal].dev->bdev)
2916 for (i = first; i < first + num; i++) {
2917 if (map->stripes[i].dev->bdev)
2920 /* we couldn't find one that doesn't fail. Just return something
2921 * and the io error handling code will clean up eventually
2926 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2927 u64 logical, u64 *length,
2928 struct btrfs_multi_bio **multi_ret,
2929 int mirror_num, struct page *unplug_page)
2931 struct extent_map *em;
2932 struct map_lookup *map;
2933 struct extent_map_tree *em_tree = &map_tree->map_tree;
2937 int stripes_allocated = 8;
2938 int stripes_required = 1;
2943 struct btrfs_multi_bio *multi = NULL;
2945 if (multi_ret && !(rw & REQ_WRITE))
2946 stripes_allocated = 1;
2949 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2954 atomic_set(&multi->error, 0);
2957 read_lock(&em_tree->lock);
2958 em = lookup_extent_mapping(em_tree, logical, *length);
2959 read_unlock(&em_tree->lock);
2961 if (!em && unplug_page) {
2967 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2968 (unsigned long long)logical,
2969 (unsigned long long)*length);
2973 BUG_ON(em->start > logical || em->start + em->len < logical);
2974 map = (struct map_lookup *)em->bdev;
2975 offset = logical - em->start;
2977 if (mirror_num > map->num_stripes)
2980 /* if our multi bio struct is too small, back off and try again */
2981 if (rw & REQ_WRITE) {
2982 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2983 BTRFS_BLOCK_GROUP_DUP)) {
2984 stripes_required = map->num_stripes;
2986 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2987 stripes_required = map->sub_stripes;
2991 if (multi_ret && (rw & REQ_WRITE) &&
2992 stripes_allocated < stripes_required) {
2993 stripes_allocated = map->num_stripes;
2994 free_extent_map(em);
3000 * stripe_nr counts the total number of stripes we have to stride
3001 * to get to this block
3003 do_div(stripe_nr, map->stripe_len);
3005 stripe_offset = stripe_nr * map->stripe_len;
3006 BUG_ON(offset < stripe_offset);
3008 /* stripe_offset is the offset of this block in its stripe*/
3009 stripe_offset = offset - stripe_offset;
3011 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
3012 BTRFS_BLOCK_GROUP_RAID10 |
3013 BTRFS_BLOCK_GROUP_DUP)) {
3014 /* we limit the length of each bio to what fits in a stripe */
3015 *length = min_t(u64, em->len - offset,
3016 map->stripe_len - stripe_offset);
3018 *length = em->len - offset;
3021 if (!multi_ret && !unplug_page)
3026 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3027 if (unplug_page || (rw & REQ_WRITE))
3028 num_stripes = map->num_stripes;
3029 else if (mirror_num)
3030 stripe_index = mirror_num - 1;
3032 stripe_index = find_live_mirror(map, 0,
3034 current->pid % map->num_stripes);
3037 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3039 num_stripes = map->num_stripes;
3040 else if (mirror_num)
3041 stripe_index = mirror_num - 1;
3043 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3044 int factor = map->num_stripes / map->sub_stripes;
3046 stripe_index = do_div(stripe_nr, factor);
3047 stripe_index *= map->sub_stripes;
3049 if (unplug_page || (rw & REQ_WRITE))
3050 num_stripes = map->sub_stripes;
3051 else if (mirror_num)
3052 stripe_index += mirror_num - 1;
3054 stripe_index = find_live_mirror(map, stripe_index,
3055 map->sub_stripes, stripe_index +
3056 current->pid % map->sub_stripes);
3060 * after this do_div call, stripe_nr is the number of stripes
3061 * on this device we have to walk to find the data, and
3062 * stripe_index is the number of our device in the stripe array
3064 stripe_index = do_div(stripe_nr, map->num_stripes);
3066 BUG_ON(stripe_index >= map->num_stripes);
3068 for (i = 0; i < num_stripes; i++) {
3070 struct btrfs_device *device;
3071 struct backing_dev_info *bdi;
3073 device = map->stripes[stripe_index].dev;
3075 bdi = blk_get_backing_dev_info(device->bdev);
3076 if (bdi->unplug_io_fn)
3077 bdi->unplug_io_fn(bdi, unplug_page);
3080 multi->stripes[i].physical =
3081 map->stripes[stripe_index].physical +
3082 stripe_offset + stripe_nr * map->stripe_len;
3083 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3089 multi->num_stripes = num_stripes;
3090 multi->max_errors = max_errors;
3093 free_extent_map(em);
3097 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3098 u64 logical, u64 *length,
3099 struct btrfs_multi_bio **multi_ret, int mirror_num)
3101 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3105 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3106 u64 chunk_start, u64 physical, u64 devid,
3107 u64 **logical, int *naddrs, int *stripe_len)
3109 struct extent_map_tree *em_tree = &map_tree->map_tree;
3110 struct extent_map *em;
3111 struct map_lookup *map;
3118 read_lock(&em_tree->lock);
3119 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3120 read_unlock(&em_tree->lock);
3122 BUG_ON(!em || em->start != chunk_start);
3123 map = (struct map_lookup *)em->bdev;
3126 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3127 do_div(length, map->num_stripes / map->sub_stripes);
3128 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3129 do_div(length, map->num_stripes);
3131 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3134 for (i = 0; i < map->num_stripes; i++) {
3135 if (devid && map->stripes[i].dev->devid != devid)
3137 if (map->stripes[i].physical > physical ||
3138 map->stripes[i].physical + length <= physical)
3141 stripe_nr = physical - map->stripes[i].physical;
3142 do_div(stripe_nr, map->stripe_len);
3144 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3145 stripe_nr = stripe_nr * map->num_stripes + i;
3146 do_div(stripe_nr, map->sub_stripes);
3147 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3148 stripe_nr = stripe_nr * map->num_stripes + i;
3150 bytenr = chunk_start + stripe_nr * map->stripe_len;
3151 WARN_ON(nr >= map->num_stripes);
3152 for (j = 0; j < nr; j++) {
3153 if (buf[j] == bytenr)
3157 WARN_ON(nr >= map->num_stripes);
3164 *stripe_len = map->stripe_len;
3166 free_extent_map(em);
3170 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
3171 u64 logical, struct page *page)
3173 u64 length = PAGE_CACHE_SIZE;
3174 return __btrfs_map_block(map_tree, READ, logical, &length,
3178 static void end_bio_multi_stripe(struct bio *bio, int err)
3180 struct btrfs_multi_bio *multi = bio->bi_private;
3181 int is_orig_bio = 0;
3184 atomic_inc(&multi->error);
3186 if (bio == multi->orig_bio)
3189 if (atomic_dec_and_test(&multi->stripes_pending)) {
3192 bio = multi->orig_bio;
3194 bio->bi_private = multi->private;
3195 bio->bi_end_io = multi->end_io;
3196 /* only send an error to the higher layers if it is
3197 * beyond the tolerance of the multi-bio
3199 if (atomic_read(&multi->error) > multi->max_errors) {
3203 * this bio is actually up to date, we didn't
3204 * go over the max number of errors
3206 set_bit(BIO_UPTODATE, &bio->bi_flags);
3211 bio_endio(bio, err);
3212 } else if (!is_orig_bio) {
3217 struct async_sched {
3220 struct btrfs_fs_info *info;
3221 struct btrfs_work work;
3225 * see run_scheduled_bios for a description of why bios are collected for
3228 * This will add one bio to the pending list for a device and make sure
3229 * the work struct is scheduled.
3231 static noinline int schedule_bio(struct btrfs_root *root,
3232 struct btrfs_device *device,
3233 int rw, struct bio *bio)
3235 int should_queue = 1;
3236 struct btrfs_pending_bios *pending_bios;
3238 /* don't bother with additional async steps for reads, right now */
3239 if (!(rw & REQ_WRITE)) {
3241 submit_bio(rw, bio);
3247 * nr_async_bios allows us to reliably return congestion to the
3248 * higher layers. Otherwise, the async bio makes it appear we have
3249 * made progress against dirty pages when we've really just put it
3250 * on a queue for later
3252 atomic_inc(&root->fs_info->nr_async_bios);
3253 WARN_ON(bio->bi_next);
3254 bio->bi_next = NULL;
3257 spin_lock(&device->io_lock);
3258 if (bio->bi_rw & REQ_SYNC)
3259 pending_bios = &device->pending_sync_bios;
3261 pending_bios = &device->pending_bios;
3263 if (pending_bios->tail)
3264 pending_bios->tail->bi_next = bio;
3266 pending_bios->tail = bio;
3267 if (!pending_bios->head)
3268 pending_bios->head = bio;
3269 if (device->running_pending)
3272 spin_unlock(&device->io_lock);
3275 btrfs_queue_worker(&root->fs_info->submit_workers,
3280 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3281 int mirror_num, int async_submit)
3283 struct btrfs_mapping_tree *map_tree;
3284 struct btrfs_device *dev;
3285 struct bio *first_bio = bio;
3286 u64 logical = (u64)bio->bi_sector << 9;
3289 struct btrfs_multi_bio *multi = NULL;
3294 length = bio->bi_size;
3295 map_tree = &root->fs_info->mapping_tree;
3296 map_length = length;
3298 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3302 total_devs = multi->num_stripes;
3303 if (map_length < length) {
3304 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3305 "len %llu\n", (unsigned long long)logical,
3306 (unsigned long long)length,
3307 (unsigned long long)map_length);
3310 multi->end_io = first_bio->bi_end_io;
3311 multi->private = first_bio->bi_private;
3312 multi->orig_bio = first_bio;
3313 atomic_set(&multi->stripes_pending, multi->num_stripes);
3315 while (dev_nr < total_devs) {
3316 if (total_devs > 1) {
3317 if (dev_nr < total_devs - 1) {
3318 bio = bio_clone(first_bio, GFP_NOFS);
3323 bio->bi_private = multi;
3324 bio->bi_end_io = end_bio_multi_stripe;
3326 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3327 dev = multi->stripes[dev_nr].dev;
3328 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3329 bio->bi_bdev = dev->bdev;
3331 schedule_bio(root, dev, rw, bio);
3333 submit_bio(rw, bio);
3335 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3336 bio->bi_sector = logical >> 9;
3337 bio_endio(bio, -EIO);
3341 if (total_devs == 1)
3346 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3349 struct btrfs_device *device;
3350 struct btrfs_fs_devices *cur_devices;
3352 cur_devices = root->fs_info->fs_devices;
3353 while (cur_devices) {
3355 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3356 device = __find_device(&cur_devices->devices,
3361 cur_devices = cur_devices->seed;
3366 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3367 u64 devid, u8 *dev_uuid)
3369 struct btrfs_device *device;
3370 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3372 device = kzalloc(sizeof(*device), GFP_NOFS);
3375 list_add(&device->dev_list,
3376 &fs_devices->devices);
3377 device->barriers = 1;
3378 device->dev_root = root->fs_info->dev_root;
3379 device->devid = devid;
3380 device->work.func = pending_bios_fn;
3381 device->fs_devices = fs_devices;
3382 device->missing = 1;
3383 fs_devices->num_devices++;
3384 fs_devices->missing_devices++;
3385 spin_lock_init(&device->io_lock);
3386 INIT_LIST_HEAD(&device->dev_alloc_list);
3387 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3391 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3392 struct extent_buffer *leaf,
3393 struct btrfs_chunk *chunk)
3395 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3396 struct map_lookup *map;
3397 struct extent_map *em;
3401 u8 uuid[BTRFS_UUID_SIZE];
3406 logical = key->offset;
3407 length = btrfs_chunk_length(leaf, chunk);
3409 read_lock(&map_tree->map_tree.lock);
3410 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3411 read_unlock(&map_tree->map_tree.lock);
3413 /* already mapped? */
3414 if (em && em->start <= logical && em->start + em->len > logical) {
3415 free_extent_map(em);
3418 free_extent_map(em);
3421 em = alloc_extent_map(GFP_NOFS);
3424 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3425 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3427 free_extent_map(em);
3431 em->bdev = (struct block_device *)map;
3432 em->start = logical;
3434 em->block_start = 0;
3435 em->block_len = em->len;
3437 map->num_stripes = num_stripes;
3438 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3439 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3440 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3441 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3442 map->type = btrfs_chunk_type(leaf, chunk);
3443 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3444 for (i = 0; i < num_stripes; i++) {
3445 map->stripes[i].physical =
3446 btrfs_stripe_offset_nr(leaf, chunk, i);
3447 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3448 read_extent_buffer(leaf, uuid, (unsigned long)
3449 btrfs_stripe_dev_uuid_nr(chunk, i),
3451 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3453 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3455 free_extent_map(em);
3458 if (!map->stripes[i].dev) {
3459 map->stripes[i].dev =
3460 add_missing_dev(root, devid, uuid);
3461 if (!map->stripes[i].dev) {
3463 free_extent_map(em);
3467 map->stripes[i].dev->in_fs_metadata = 1;
3470 write_lock(&map_tree->map_tree.lock);
3471 ret = add_extent_mapping(&map_tree->map_tree, em);
3472 write_unlock(&map_tree->map_tree.lock);
3474 free_extent_map(em);
3479 static int fill_device_from_item(struct extent_buffer *leaf,
3480 struct btrfs_dev_item *dev_item,
3481 struct btrfs_device *device)
3485 device->devid = btrfs_device_id(leaf, dev_item);
3486 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3487 device->total_bytes = device->disk_total_bytes;
3488 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3489 device->type = btrfs_device_type(leaf, dev_item);
3490 device->io_align = btrfs_device_io_align(leaf, dev_item);
3491 device->io_width = btrfs_device_io_width(leaf, dev_item);
3492 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3494 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3495 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3500 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3502 struct btrfs_fs_devices *fs_devices;
3505 mutex_lock(&uuid_mutex);
3507 fs_devices = root->fs_info->fs_devices->seed;
3508 while (fs_devices) {
3509 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3513 fs_devices = fs_devices->seed;
3516 fs_devices = find_fsid(fsid);
3522 fs_devices = clone_fs_devices(fs_devices);
3523 if (IS_ERR(fs_devices)) {
3524 ret = PTR_ERR(fs_devices);
3528 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3529 root->fs_info->bdev_holder);
3533 if (!fs_devices->seeding) {
3534 __btrfs_close_devices(fs_devices);
3535 free_fs_devices(fs_devices);
3540 fs_devices->seed = root->fs_info->fs_devices->seed;
3541 root->fs_info->fs_devices->seed = fs_devices;
3543 mutex_unlock(&uuid_mutex);
3547 static int read_one_dev(struct btrfs_root *root,
3548 struct extent_buffer *leaf,
3549 struct btrfs_dev_item *dev_item)
3551 struct btrfs_device *device;
3554 u8 fs_uuid[BTRFS_UUID_SIZE];
3555 u8 dev_uuid[BTRFS_UUID_SIZE];
3557 devid = btrfs_device_id(leaf, dev_item);
3558 read_extent_buffer(leaf, dev_uuid,
3559 (unsigned long)btrfs_device_uuid(dev_item),
3561 read_extent_buffer(leaf, fs_uuid,
3562 (unsigned long)btrfs_device_fsid(dev_item),
3565 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3566 ret = open_seed_devices(root, fs_uuid);
3567 if (ret && !btrfs_test_opt(root, DEGRADED))
3571 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3572 if (!device || !device->bdev) {
3573 if (!btrfs_test_opt(root, DEGRADED))
3577 printk(KERN_WARNING "warning devid %llu missing\n",
3578 (unsigned long long)devid);
3579 device = add_missing_dev(root, devid, dev_uuid);
3582 } else if (!device->missing) {
3584 * this happens when a device that was properly setup
3585 * in the device info lists suddenly goes bad.
3586 * device->bdev is NULL, and so we have to set
3587 * device->missing to one here
3589 root->fs_info->fs_devices->missing_devices++;
3590 device->missing = 1;
3594 if (device->fs_devices != root->fs_info->fs_devices) {
3595 BUG_ON(device->writeable);
3596 if (device->generation !=
3597 btrfs_device_generation(leaf, dev_item))
3601 fill_device_from_item(leaf, dev_item, device);
3602 device->dev_root = root->fs_info->dev_root;
3603 device->in_fs_metadata = 1;
3604 if (device->writeable)
3605 device->fs_devices->total_rw_bytes += device->total_bytes;
3610 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3612 struct btrfs_dev_item *dev_item;
3614 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3616 return read_one_dev(root, buf, dev_item);
3619 int btrfs_read_sys_array(struct btrfs_root *root)
3621 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3622 struct extent_buffer *sb;
3623 struct btrfs_disk_key *disk_key;
3624 struct btrfs_chunk *chunk;
3626 unsigned long sb_ptr;
3632 struct btrfs_key key;
3634 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3635 BTRFS_SUPER_INFO_SIZE);
3638 btrfs_set_buffer_uptodate(sb);
3639 btrfs_set_buffer_lockdep_class(sb, 0);
3641 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3642 array_size = btrfs_super_sys_array_size(super_copy);
3644 ptr = super_copy->sys_chunk_array;
3645 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3648 while (cur < array_size) {
3649 disk_key = (struct btrfs_disk_key *)ptr;
3650 btrfs_disk_key_to_cpu(&key, disk_key);
3652 len = sizeof(*disk_key); ptr += len;
3656 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3657 chunk = (struct btrfs_chunk *)sb_ptr;
3658 ret = read_one_chunk(root, &key, sb, chunk);
3661 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3662 len = btrfs_chunk_item_size(num_stripes);
3671 free_extent_buffer(sb);
3675 int btrfs_read_chunk_tree(struct btrfs_root *root)
3677 struct btrfs_path *path;
3678 struct extent_buffer *leaf;
3679 struct btrfs_key key;
3680 struct btrfs_key found_key;
3684 root = root->fs_info->chunk_root;
3686 path = btrfs_alloc_path();
3690 /* first we search for all of the device items, and then we
3691 * read in all of the chunk items. This way we can create chunk
3692 * mappings that reference all of the devices that are afound
3694 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3698 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3702 leaf = path->nodes[0];
3703 slot = path->slots[0];
3704 if (slot >= btrfs_header_nritems(leaf)) {
3705 ret = btrfs_next_leaf(root, path);
3712 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3713 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3714 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3716 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3717 struct btrfs_dev_item *dev_item;
3718 dev_item = btrfs_item_ptr(leaf, slot,
3719 struct btrfs_dev_item);
3720 ret = read_one_dev(root, leaf, dev_item);
3724 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3725 struct btrfs_chunk *chunk;
3726 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3727 ret = read_one_chunk(root, &found_key, leaf, chunk);
3733 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3735 btrfs_release_path(root, path);
3740 btrfs_free_path(path);