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);
606 disk_super = (struct btrfs_super_block *)bh->b_data;
607 devid = btrfs_stack_device_id(&disk_super->dev_item);
608 if (devid != device->devid)
611 if (memcmp(device->uuid, disk_super->dev_item.uuid,
615 device->generation = btrfs_super_generation(disk_super);
616 if (!latest_transid || device->generation > latest_transid) {
617 latest_devid = devid;
618 latest_transid = device->generation;
622 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
623 device->writeable = 0;
625 device->writeable = !bdev_read_only(bdev);
630 device->in_fs_metadata = 0;
631 device->mode = flags;
633 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
634 fs_devices->rotating = 1;
636 fs_devices->open_devices++;
637 if (device->writeable) {
638 fs_devices->rw_devices++;
639 list_add(&device->dev_alloc_list,
640 &fs_devices->alloc_list);
647 close_bdev_exclusive(bdev, FMODE_READ);
651 if (fs_devices->open_devices == 0) {
655 fs_devices->seeding = seeding;
656 fs_devices->opened = 1;
657 fs_devices->latest_bdev = latest_bdev;
658 fs_devices->latest_devid = latest_devid;
659 fs_devices->latest_trans = latest_transid;
660 fs_devices->total_rw_bytes = 0;
665 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
666 fmode_t flags, void *holder)
670 mutex_lock(&uuid_mutex);
671 if (fs_devices->opened) {
672 fs_devices->opened++;
675 ret = __btrfs_open_devices(fs_devices, flags, holder);
677 mutex_unlock(&uuid_mutex);
681 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
682 struct btrfs_fs_devices **fs_devices_ret)
684 struct btrfs_super_block *disk_super;
685 struct block_device *bdev;
686 struct buffer_head *bh;
691 mutex_lock(&uuid_mutex);
693 bdev = open_bdev_exclusive(path, flags, holder);
700 ret = set_blocksize(bdev, 4096);
703 bh = btrfs_read_dev_super(bdev);
708 disk_super = (struct btrfs_super_block *)bh->b_data;
709 devid = btrfs_stack_device_id(&disk_super->dev_item);
710 transid = btrfs_super_generation(disk_super);
711 if (disk_super->label[0])
712 printk(KERN_INFO "device label %s ", disk_super->label);
714 /* FIXME, make a readl uuid parser */
715 printk(KERN_INFO "device fsid %llx-%llx ",
716 *(unsigned long long *)disk_super->fsid,
717 *(unsigned long long *)(disk_super->fsid + 8));
719 printk(KERN_CONT "devid %llu transid %llu %s\n",
720 (unsigned long long)devid, (unsigned long long)transid, path);
721 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
725 close_bdev_exclusive(bdev, flags);
727 mutex_unlock(&uuid_mutex);
731 /* helper to account the used device space in the range */
732 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
733 u64 end, u64 *length)
735 struct btrfs_key key;
736 struct btrfs_root *root = device->dev_root;
737 struct btrfs_dev_extent *dev_extent;
738 struct btrfs_path *path;
742 struct extent_buffer *l;
746 if (start >= device->total_bytes)
749 path = btrfs_alloc_path();
754 key.objectid = device->devid;
756 key.type = BTRFS_DEV_EXTENT_KEY;
758 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
762 ret = btrfs_previous_item(root, path, key.objectid, key.type);
769 slot = path->slots[0];
770 if (slot >= btrfs_header_nritems(l)) {
771 ret = btrfs_next_leaf(root, path);
779 btrfs_item_key_to_cpu(l, &key, slot);
781 if (key.objectid < device->devid)
784 if (key.objectid > device->devid)
787 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
790 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
791 extent_end = key.offset + btrfs_dev_extent_length(l,
793 if (key.offset <= start && extent_end > end) {
794 *length = end - start + 1;
796 } else if (key.offset <= start && extent_end > start)
797 *length += extent_end - start;
798 else if (key.offset > start && extent_end <= end)
799 *length += extent_end - key.offset;
800 else if (key.offset > start && key.offset <= end) {
801 *length += end - key.offset + 1;
803 } else if (key.offset > end)
811 btrfs_free_path(path);
816 * find_free_dev_extent - find free space in the specified device
817 * @trans: transaction handler
818 * @device: the device which we search the free space in
819 * @num_bytes: the size of the free space that we need
820 * @start: store the start of the free space.
821 * @len: the size of the free space. that we find, or the size of the max
822 * free space if we don't find suitable free space
824 * this uses a pretty simple search, the expectation is that it is
825 * called very infrequently and that a given device has a small number
828 * @start is used to store the start of the free space if we find. But if we
829 * don't find suitable free space, it will be used to store the start position
830 * of the max free space.
832 * @len is used to store the size of the free space that we find.
833 * But if we don't find suitable free space, it is used to store the size of
834 * the max free space.
836 int find_free_dev_extent(struct btrfs_trans_handle *trans,
837 struct btrfs_device *device, u64 num_bytes,
838 u64 *start, u64 *len)
840 struct btrfs_key key;
841 struct btrfs_root *root = device->dev_root;
842 struct btrfs_dev_extent *dev_extent;
843 struct btrfs_path *path;
849 u64 search_end = device->total_bytes;
852 struct extent_buffer *l;
854 /* FIXME use last free of some kind */
856 /* we don't want to overwrite the superblock on the drive,
857 * so we make sure to start at an offset of at least 1MB
859 search_start = 1024 * 1024;
861 if (root->fs_info->alloc_start + num_bytes <= search_end)
862 search_start = max(root->fs_info->alloc_start, search_start);
864 max_hole_start = search_start;
867 if (search_start >= search_end) {
872 path = btrfs_alloc_path();
879 key.objectid = device->devid;
880 key.offset = search_start;
881 key.type = BTRFS_DEV_EXTENT_KEY;
883 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
887 ret = btrfs_previous_item(root, path, key.objectid, key.type);
894 slot = path->slots[0];
895 if (slot >= btrfs_header_nritems(l)) {
896 ret = btrfs_next_leaf(root, path);
904 btrfs_item_key_to_cpu(l, &key, slot);
906 if (key.objectid < device->devid)
909 if (key.objectid > device->devid)
912 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
915 if (key.offset > search_start) {
916 hole_size = key.offset - search_start;
918 if (hole_size > max_hole_size) {
919 max_hole_start = search_start;
920 max_hole_size = hole_size;
924 * If this free space is greater than which we need,
925 * it must be the max free space that we have found
926 * until now, so max_hole_start must point to the start
927 * of this free space and the length of this free space
928 * is stored in max_hole_size. Thus, we return
929 * max_hole_start and max_hole_size and go back to the
932 if (hole_size >= num_bytes) {
938 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
939 extent_end = key.offset + btrfs_dev_extent_length(l,
941 if (extent_end > search_start)
942 search_start = extent_end;
948 hole_size = search_end- search_start;
949 if (hole_size > max_hole_size) {
950 max_hole_start = search_start;
951 max_hole_size = hole_size;
955 if (hole_size < num_bytes)
961 btrfs_free_path(path);
963 *start = max_hole_start;
965 *len = max_hole_size;
969 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
970 struct btrfs_device *device,
974 struct btrfs_path *path;
975 struct btrfs_root *root = device->dev_root;
976 struct btrfs_key key;
977 struct btrfs_key found_key;
978 struct extent_buffer *leaf = NULL;
979 struct btrfs_dev_extent *extent = NULL;
981 path = btrfs_alloc_path();
985 key.objectid = device->devid;
987 key.type = BTRFS_DEV_EXTENT_KEY;
989 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
991 ret = btrfs_previous_item(root, path, key.objectid,
992 BTRFS_DEV_EXTENT_KEY);
994 leaf = path->nodes[0];
995 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
996 extent = btrfs_item_ptr(leaf, path->slots[0],
997 struct btrfs_dev_extent);
998 BUG_ON(found_key.offset > start || found_key.offset +
999 btrfs_dev_extent_length(leaf, extent) < start);
1001 } else if (ret == 0) {
1002 leaf = path->nodes[0];
1003 extent = btrfs_item_ptr(leaf, path->slots[0],
1004 struct btrfs_dev_extent);
1008 if (device->bytes_used > 0)
1009 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
1010 ret = btrfs_del_item(trans, root, path);
1013 btrfs_free_path(path);
1017 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1018 struct btrfs_device *device,
1019 u64 chunk_tree, u64 chunk_objectid,
1020 u64 chunk_offset, u64 start, u64 num_bytes)
1023 struct btrfs_path *path;
1024 struct btrfs_root *root = device->dev_root;
1025 struct btrfs_dev_extent *extent;
1026 struct extent_buffer *leaf;
1027 struct btrfs_key key;
1029 WARN_ON(!device->in_fs_metadata);
1030 path = btrfs_alloc_path();
1034 key.objectid = device->devid;
1036 key.type = BTRFS_DEV_EXTENT_KEY;
1037 ret = btrfs_insert_empty_item(trans, root, path, &key,
1041 leaf = path->nodes[0];
1042 extent = btrfs_item_ptr(leaf, path->slots[0],
1043 struct btrfs_dev_extent);
1044 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1045 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1046 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1048 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1049 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1052 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1053 btrfs_mark_buffer_dirty(leaf);
1054 btrfs_free_path(path);
1058 static noinline int find_next_chunk(struct btrfs_root *root,
1059 u64 objectid, u64 *offset)
1061 struct btrfs_path *path;
1063 struct btrfs_key key;
1064 struct btrfs_chunk *chunk;
1065 struct btrfs_key found_key;
1067 path = btrfs_alloc_path();
1070 key.objectid = objectid;
1071 key.offset = (u64)-1;
1072 key.type = BTRFS_CHUNK_ITEM_KEY;
1074 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1080 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1084 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1086 if (found_key.objectid != objectid)
1089 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1090 struct btrfs_chunk);
1091 *offset = found_key.offset +
1092 btrfs_chunk_length(path->nodes[0], chunk);
1097 btrfs_free_path(path);
1101 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1104 struct btrfs_key key;
1105 struct btrfs_key found_key;
1106 struct btrfs_path *path;
1108 root = root->fs_info->chunk_root;
1110 path = btrfs_alloc_path();
1114 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1115 key.type = BTRFS_DEV_ITEM_KEY;
1116 key.offset = (u64)-1;
1118 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1124 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1125 BTRFS_DEV_ITEM_KEY);
1129 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1131 *objectid = found_key.offset + 1;
1135 btrfs_free_path(path);
1140 * the device information is stored in the chunk root
1141 * the btrfs_device struct should be fully filled in
1143 int btrfs_add_device(struct btrfs_trans_handle *trans,
1144 struct btrfs_root *root,
1145 struct btrfs_device *device)
1148 struct btrfs_path *path;
1149 struct btrfs_dev_item *dev_item;
1150 struct extent_buffer *leaf;
1151 struct btrfs_key key;
1154 root = root->fs_info->chunk_root;
1156 path = btrfs_alloc_path();
1160 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1161 key.type = BTRFS_DEV_ITEM_KEY;
1162 key.offset = device->devid;
1164 ret = btrfs_insert_empty_item(trans, root, path, &key,
1169 leaf = path->nodes[0];
1170 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1172 btrfs_set_device_id(leaf, dev_item, device->devid);
1173 btrfs_set_device_generation(leaf, dev_item, 0);
1174 btrfs_set_device_type(leaf, dev_item, device->type);
1175 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1176 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1177 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1178 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1179 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1180 btrfs_set_device_group(leaf, dev_item, 0);
1181 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1182 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1183 btrfs_set_device_start_offset(leaf, dev_item, 0);
1185 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1186 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1187 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1188 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1189 btrfs_mark_buffer_dirty(leaf);
1193 btrfs_free_path(path);
1197 static int btrfs_rm_dev_item(struct btrfs_root *root,
1198 struct btrfs_device *device)
1201 struct btrfs_path *path;
1202 struct btrfs_key key;
1203 struct btrfs_trans_handle *trans;
1205 root = root->fs_info->chunk_root;
1207 path = btrfs_alloc_path();
1211 trans = btrfs_start_transaction(root, 0);
1212 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1213 key.type = BTRFS_DEV_ITEM_KEY;
1214 key.offset = device->devid;
1217 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1226 ret = btrfs_del_item(trans, root, path);
1230 btrfs_free_path(path);
1231 unlock_chunks(root);
1232 btrfs_commit_transaction(trans, root);
1236 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1238 struct btrfs_device *device;
1239 struct btrfs_device *next_device;
1240 struct block_device *bdev;
1241 struct buffer_head *bh = NULL;
1242 struct btrfs_super_block *disk_super;
1249 mutex_lock(&uuid_mutex);
1250 mutex_lock(&root->fs_info->volume_mutex);
1252 all_avail = root->fs_info->avail_data_alloc_bits |
1253 root->fs_info->avail_system_alloc_bits |
1254 root->fs_info->avail_metadata_alloc_bits;
1256 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1257 root->fs_info->fs_devices->num_devices <= 4) {
1258 printk(KERN_ERR "btrfs: unable to go below four devices "
1264 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1265 root->fs_info->fs_devices->num_devices <= 2) {
1266 printk(KERN_ERR "btrfs: unable to go below two "
1267 "devices on raid1\n");
1272 if (strcmp(device_path, "missing") == 0) {
1273 struct list_head *devices;
1274 struct btrfs_device *tmp;
1277 devices = &root->fs_info->fs_devices->devices;
1278 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1279 list_for_each_entry(tmp, devices, dev_list) {
1280 if (tmp->in_fs_metadata && !tmp->bdev) {
1285 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1290 printk(KERN_ERR "btrfs: no missing devices found to "
1295 bdev = open_bdev_exclusive(device_path, FMODE_READ,
1296 root->fs_info->bdev_holder);
1298 ret = PTR_ERR(bdev);
1302 set_blocksize(bdev, 4096);
1303 bh = btrfs_read_dev_super(bdev);
1308 disk_super = (struct btrfs_super_block *)bh->b_data;
1309 devid = btrfs_stack_device_id(&disk_super->dev_item);
1310 dev_uuid = disk_super->dev_item.uuid;
1311 device = btrfs_find_device(root, devid, dev_uuid,
1319 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1320 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1326 if (device->writeable) {
1327 list_del_init(&device->dev_alloc_list);
1328 root->fs_info->fs_devices->rw_devices--;
1331 ret = btrfs_shrink_device(device, 0);
1335 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1339 device->in_fs_metadata = 0;
1342 * the device list mutex makes sure that we don't change
1343 * the device list while someone else is writing out all
1344 * the device supers.
1346 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1347 list_del_init(&device->dev_list);
1348 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1350 device->fs_devices->num_devices--;
1352 if (device->missing)
1353 root->fs_info->fs_devices->missing_devices--;
1355 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1356 struct btrfs_device, dev_list);
1357 if (device->bdev == root->fs_info->sb->s_bdev)
1358 root->fs_info->sb->s_bdev = next_device->bdev;
1359 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1360 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1363 close_bdev_exclusive(device->bdev, device->mode);
1364 device->bdev = NULL;
1365 device->fs_devices->open_devices--;
1368 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1369 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1371 if (device->fs_devices->open_devices == 0) {
1372 struct btrfs_fs_devices *fs_devices;
1373 fs_devices = root->fs_info->fs_devices;
1374 while (fs_devices) {
1375 if (fs_devices->seed == device->fs_devices)
1377 fs_devices = fs_devices->seed;
1379 fs_devices->seed = device->fs_devices->seed;
1380 device->fs_devices->seed = NULL;
1381 __btrfs_close_devices(device->fs_devices);
1382 free_fs_devices(device->fs_devices);
1386 * at this point, the device is zero sized. We want to
1387 * remove it from the devices list and zero out the old super
1389 if (device->writeable) {
1390 /* make sure this device isn't detected as part of
1393 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1394 set_buffer_dirty(bh);
1395 sync_dirty_buffer(bh);
1398 kfree(device->name);
1406 close_bdev_exclusive(bdev, FMODE_READ);
1408 mutex_unlock(&root->fs_info->volume_mutex);
1409 mutex_unlock(&uuid_mutex);
1414 * does all the dirty work required for changing file system's UUID.
1416 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1417 struct btrfs_root *root)
1419 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1420 struct btrfs_fs_devices *old_devices;
1421 struct btrfs_fs_devices *seed_devices;
1422 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1423 struct btrfs_device *device;
1426 BUG_ON(!mutex_is_locked(&uuid_mutex));
1427 if (!fs_devices->seeding)
1430 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1434 old_devices = clone_fs_devices(fs_devices);
1435 if (IS_ERR(old_devices)) {
1436 kfree(seed_devices);
1437 return PTR_ERR(old_devices);
1440 list_add(&old_devices->list, &fs_uuids);
1442 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1443 seed_devices->opened = 1;
1444 INIT_LIST_HEAD(&seed_devices->devices);
1445 INIT_LIST_HEAD(&seed_devices->alloc_list);
1446 mutex_init(&seed_devices->device_list_mutex);
1447 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1448 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1449 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1450 device->fs_devices = seed_devices;
1453 fs_devices->seeding = 0;
1454 fs_devices->num_devices = 0;
1455 fs_devices->open_devices = 0;
1456 fs_devices->seed = seed_devices;
1458 generate_random_uuid(fs_devices->fsid);
1459 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1460 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1461 super_flags = btrfs_super_flags(disk_super) &
1462 ~BTRFS_SUPER_FLAG_SEEDING;
1463 btrfs_set_super_flags(disk_super, super_flags);
1469 * strore the expected generation for seed devices in device items.
1471 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1472 struct btrfs_root *root)
1474 struct btrfs_path *path;
1475 struct extent_buffer *leaf;
1476 struct btrfs_dev_item *dev_item;
1477 struct btrfs_device *device;
1478 struct btrfs_key key;
1479 u8 fs_uuid[BTRFS_UUID_SIZE];
1480 u8 dev_uuid[BTRFS_UUID_SIZE];
1484 path = btrfs_alloc_path();
1488 root = root->fs_info->chunk_root;
1489 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1491 key.type = BTRFS_DEV_ITEM_KEY;
1494 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1498 leaf = path->nodes[0];
1500 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1501 ret = btrfs_next_leaf(root, path);
1506 leaf = path->nodes[0];
1507 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1508 btrfs_release_path(root, path);
1512 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1513 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1514 key.type != BTRFS_DEV_ITEM_KEY)
1517 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1518 struct btrfs_dev_item);
1519 devid = btrfs_device_id(leaf, dev_item);
1520 read_extent_buffer(leaf, dev_uuid,
1521 (unsigned long)btrfs_device_uuid(dev_item),
1523 read_extent_buffer(leaf, fs_uuid,
1524 (unsigned long)btrfs_device_fsid(dev_item),
1526 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1529 if (device->fs_devices->seeding) {
1530 btrfs_set_device_generation(leaf, dev_item,
1531 device->generation);
1532 btrfs_mark_buffer_dirty(leaf);
1540 btrfs_free_path(path);
1544 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1546 struct btrfs_trans_handle *trans;
1547 struct btrfs_device *device;
1548 struct block_device *bdev;
1549 struct list_head *devices;
1550 struct super_block *sb = root->fs_info->sb;
1552 int seeding_dev = 0;
1555 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1558 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
1560 return PTR_ERR(bdev);
1562 if (root->fs_info->fs_devices->seeding) {
1564 down_write(&sb->s_umount);
1565 mutex_lock(&uuid_mutex);
1568 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1569 mutex_lock(&root->fs_info->volume_mutex);
1571 devices = &root->fs_info->fs_devices->devices;
1573 * we have the volume lock, so we don't need the extra
1574 * device list mutex while reading the list here.
1576 list_for_each_entry(device, devices, dev_list) {
1577 if (device->bdev == bdev) {
1583 device = kzalloc(sizeof(*device), GFP_NOFS);
1585 /* we can safely leave the fs_devices entry around */
1590 device->name = kstrdup(device_path, GFP_NOFS);
1591 if (!device->name) {
1597 ret = find_next_devid(root, &device->devid);
1603 trans = btrfs_start_transaction(root, 0);
1606 device->barriers = 1;
1607 device->writeable = 1;
1608 device->work.func = pending_bios_fn;
1609 generate_random_uuid(device->uuid);
1610 spin_lock_init(&device->io_lock);
1611 device->generation = trans->transid;
1612 device->io_width = root->sectorsize;
1613 device->io_align = root->sectorsize;
1614 device->sector_size = root->sectorsize;
1615 device->total_bytes = i_size_read(bdev->bd_inode);
1616 device->disk_total_bytes = device->total_bytes;
1617 device->dev_root = root->fs_info->dev_root;
1618 device->bdev = bdev;
1619 device->in_fs_metadata = 1;
1621 set_blocksize(device->bdev, 4096);
1624 sb->s_flags &= ~MS_RDONLY;
1625 ret = btrfs_prepare_sprout(trans, root);
1629 device->fs_devices = root->fs_info->fs_devices;
1632 * we don't want write_supers to jump in here with our device
1635 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1636 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1637 list_add(&device->dev_alloc_list,
1638 &root->fs_info->fs_devices->alloc_list);
1639 root->fs_info->fs_devices->num_devices++;
1640 root->fs_info->fs_devices->open_devices++;
1641 root->fs_info->fs_devices->rw_devices++;
1642 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1644 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1645 root->fs_info->fs_devices->rotating = 1;
1647 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1648 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1649 total_bytes + device->total_bytes);
1651 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1652 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1654 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1657 ret = init_first_rw_device(trans, root, device);
1659 ret = btrfs_finish_sprout(trans, root);
1662 ret = btrfs_add_device(trans, root, device);
1666 * we've got more storage, clear any full flags on the space
1669 btrfs_clear_space_info_full(root->fs_info);
1671 unlock_chunks(root);
1672 btrfs_commit_transaction(trans, root);
1675 mutex_unlock(&uuid_mutex);
1676 up_write(&sb->s_umount);
1678 ret = btrfs_relocate_sys_chunks(root);
1682 mutex_unlock(&root->fs_info->volume_mutex);
1685 close_bdev_exclusive(bdev, 0);
1687 mutex_unlock(&uuid_mutex);
1688 up_write(&sb->s_umount);
1693 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1694 struct btrfs_device *device)
1697 struct btrfs_path *path;
1698 struct btrfs_root *root;
1699 struct btrfs_dev_item *dev_item;
1700 struct extent_buffer *leaf;
1701 struct btrfs_key key;
1703 root = device->dev_root->fs_info->chunk_root;
1705 path = btrfs_alloc_path();
1709 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1710 key.type = BTRFS_DEV_ITEM_KEY;
1711 key.offset = device->devid;
1713 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1722 leaf = path->nodes[0];
1723 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1725 btrfs_set_device_id(leaf, dev_item, device->devid);
1726 btrfs_set_device_type(leaf, dev_item, device->type);
1727 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1728 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1729 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1730 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1731 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1732 btrfs_mark_buffer_dirty(leaf);
1735 btrfs_free_path(path);
1739 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1740 struct btrfs_device *device, u64 new_size)
1742 struct btrfs_super_block *super_copy =
1743 &device->dev_root->fs_info->super_copy;
1744 u64 old_total = btrfs_super_total_bytes(super_copy);
1745 u64 diff = new_size - device->total_bytes;
1747 if (!device->writeable)
1749 if (new_size <= device->total_bytes)
1752 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1753 device->fs_devices->total_rw_bytes += diff;
1755 device->total_bytes = new_size;
1756 device->disk_total_bytes = new_size;
1757 btrfs_clear_space_info_full(device->dev_root->fs_info);
1759 return btrfs_update_device(trans, device);
1762 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1763 struct btrfs_device *device, u64 new_size)
1766 lock_chunks(device->dev_root);
1767 ret = __btrfs_grow_device(trans, device, new_size);
1768 unlock_chunks(device->dev_root);
1772 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1773 struct btrfs_root *root,
1774 u64 chunk_tree, u64 chunk_objectid,
1778 struct btrfs_path *path;
1779 struct btrfs_key key;
1781 root = root->fs_info->chunk_root;
1782 path = btrfs_alloc_path();
1786 key.objectid = chunk_objectid;
1787 key.offset = chunk_offset;
1788 key.type = BTRFS_CHUNK_ITEM_KEY;
1790 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1793 ret = btrfs_del_item(trans, root, path);
1796 btrfs_free_path(path);
1800 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1803 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1804 struct btrfs_disk_key *disk_key;
1805 struct btrfs_chunk *chunk;
1812 struct btrfs_key key;
1814 array_size = btrfs_super_sys_array_size(super_copy);
1816 ptr = super_copy->sys_chunk_array;
1819 while (cur < array_size) {
1820 disk_key = (struct btrfs_disk_key *)ptr;
1821 btrfs_disk_key_to_cpu(&key, disk_key);
1823 len = sizeof(*disk_key);
1825 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1826 chunk = (struct btrfs_chunk *)(ptr + len);
1827 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1828 len += btrfs_chunk_item_size(num_stripes);
1833 if (key.objectid == chunk_objectid &&
1834 key.offset == chunk_offset) {
1835 memmove(ptr, ptr + len, array_size - (cur + len));
1837 btrfs_set_super_sys_array_size(super_copy, array_size);
1846 static int btrfs_relocate_chunk(struct btrfs_root *root,
1847 u64 chunk_tree, u64 chunk_objectid,
1850 struct extent_map_tree *em_tree;
1851 struct btrfs_root *extent_root;
1852 struct btrfs_trans_handle *trans;
1853 struct extent_map *em;
1854 struct map_lookup *map;
1858 root = root->fs_info->chunk_root;
1859 extent_root = root->fs_info->extent_root;
1860 em_tree = &root->fs_info->mapping_tree.map_tree;
1862 ret = btrfs_can_relocate(extent_root, chunk_offset);
1866 /* step one, relocate all the extents inside this chunk */
1867 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1871 trans = btrfs_start_transaction(root, 0);
1877 * step two, delete the device extents and the
1878 * chunk tree entries
1880 read_lock(&em_tree->lock);
1881 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1882 read_unlock(&em_tree->lock);
1884 BUG_ON(em->start > chunk_offset ||
1885 em->start + em->len < chunk_offset);
1886 map = (struct map_lookup *)em->bdev;
1888 for (i = 0; i < map->num_stripes; i++) {
1889 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1890 map->stripes[i].physical);
1893 if (map->stripes[i].dev) {
1894 ret = btrfs_update_device(trans, map->stripes[i].dev);
1898 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1903 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1904 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1908 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1911 write_lock(&em_tree->lock);
1912 remove_extent_mapping(em_tree, em);
1913 write_unlock(&em_tree->lock);
1918 /* once for the tree */
1919 free_extent_map(em);
1921 free_extent_map(em);
1923 unlock_chunks(root);
1924 btrfs_end_transaction(trans, root);
1928 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1930 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1931 struct btrfs_path *path;
1932 struct extent_buffer *leaf;
1933 struct btrfs_chunk *chunk;
1934 struct btrfs_key key;
1935 struct btrfs_key found_key;
1936 u64 chunk_tree = chunk_root->root_key.objectid;
1938 bool retried = false;
1942 path = btrfs_alloc_path();
1947 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1948 key.offset = (u64)-1;
1949 key.type = BTRFS_CHUNK_ITEM_KEY;
1952 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1957 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1964 leaf = path->nodes[0];
1965 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1967 chunk = btrfs_item_ptr(leaf, path->slots[0],
1968 struct btrfs_chunk);
1969 chunk_type = btrfs_chunk_type(leaf, chunk);
1970 btrfs_release_path(chunk_root, path);
1972 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1973 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1982 if (found_key.offset == 0)
1984 key.offset = found_key.offset - 1;
1987 if (failed && !retried) {
1991 } else if (failed && retried) {
1996 btrfs_free_path(path);
2000 static u64 div_factor(u64 num, int factor)
2009 int btrfs_balance(struct btrfs_root *dev_root)
2012 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2013 struct btrfs_device *device;
2016 struct btrfs_path *path;
2017 struct btrfs_key key;
2018 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2019 struct btrfs_trans_handle *trans;
2020 struct btrfs_key found_key;
2022 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2025 mutex_lock(&dev_root->fs_info->volume_mutex);
2026 dev_root = dev_root->fs_info->dev_root;
2028 /* step one make some room on all the devices */
2029 list_for_each_entry(device, devices, dev_list) {
2030 old_size = device->total_bytes;
2031 size_to_free = div_factor(old_size, 1);
2032 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2033 if (!device->writeable ||
2034 device->total_bytes - device->bytes_used > size_to_free)
2037 ret = btrfs_shrink_device(device, old_size - size_to_free);
2042 trans = btrfs_start_transaction(dev_root, 0);
2045 ret = btrfs_grow_device(trans, device, old_size);
2048 btrfs_end_transaction(trans, dev_root);
2051 /* step two, relocate all the chunks */
2052 path = btrfs_alloc_path();
2055 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2056 key.offset = (u64)-1;
2057 key.type = BTRFS_CHUNK_ITEM_KEY;
2060 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2065 * this shouldn't happen, it means the last relocate
2071 ret = btrfs_previous_item(chunk_root, path, 0,
2072 BTRFS_CHUNK_ITEM_KEY);
2076 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2078 if (found_key.objectid != key.objectid)
2081 /* chunk zero is special */
2082 if (found_key.offset == 0)
2085 btrfs_release_path(chunk_root, path);
2086 ret = btrfs_relocate_chunk(chunk_root,
2087 chunk_root->root_key.objectid,
2090 BUG_ON(ret && ret != -ENOSPC);
2091 key.offset = found_key.offset - 1;
2095 btrfs_free_path(path);
2096 mutex_unlock(&dev_root->fs_info->volume_mutex);
2101 * shrinking a device means finding all of the device extents past
2102 * the new size, and then following the back refs to the chunks.
2103 * The chunk relocation code actually frees the device extent
2105 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2107 struct btrfs_trans_handle *trans;
2108 struct btrfs_root *root = device->dev_root;
2109 struct btrfs_dev_extent *dev_extent = NULL;
2110 struct btrfs_path *path;
2118 bool retried = false;
2119 struct extent_buffer *l;
2120 struct btrfs_key key;
2121 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2122 u64 old_total = btrfs_super_total_bytes(super_copy);
2123 u64 old_size = device->total_bytes;
2124 u64 diff = device->total_bytes - new_size;
2126 if (new_size >= device->total_bytes)
2129 path = btrfs_alloc_path();
2137 device->total_bytes = new_size;
2138 if (device->writeable)
2139 device->fs_devices->total_rw_bytes -= diff;
2140 unlock_chunks(root);
2143 key.objectid = device->devid;
2144 key.offset = (u64)-1;
2145 key.type = BTRFS_DEV_EXTENT_KEY;
2148 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2152 ret = btrfs_previous_item(root, path, 0, key.type);
2157 btrfs_release_path(root, path);
2162 slot = path->slots[0];
2163 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2165 if (key.objectid != device->devid) {
2166 btrfs_release_path(root, path);
2170 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2171 length = btrfs_dev_extent_length(l, dev_extent);
2173 if (key.offset + length <= new_size) {
2174 btrfs_release_path(root, path);
2178 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2179 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2180 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2181 btrfs_release_path(root, path);
2183 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2185 if (ret && ret != -ENOSPC)
2192 if (failed && !retried) {
2196 } else if (failed && retried) {
2200 device->total_bytes = old_size;
2201 if (device->writeable)
2202 device->fs_devices->total_rw_bytes += diff;
2203 unlock_chunks(root);
2207 /* Shrinking succeeded, else we would be at "done". */
2208 trans = btrfs_start_transaction(root, 0);
2211 device->disk_total_bytes = new_size;
2212 /* Now btrfs_update_device() will change the on-disk size. */
2213 ret = btrfs_update_device(trans, device);
2215 unlock_chunks(root);
2216 btrfs_end_transaction(trans, root);
2219 WARN_ON(diff > old_total);
2220 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2221 unlock_chunks(root);
2222 btrfs_end_transaction(trans, root);
2224 btrfs_free_path(path);
2228 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *root,
2230 struct btrfs_key *key,
2231 struct btrfs_chunk *chunk, int item_size)
2233 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2234 struct btrfs_disk_key disk_key;
2238 array_size = btrfs_super_sys_array_size(super_copy);
2239 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2242 ptr = super_copy->sys_chunk_array + array_size;
2243 btrfs_cpu_key_to_disk(&disk_key, key);
2244 memcpy(ptr, &disk_key, sizeof(disk_key));
2245 ptr += sizeof(disk_key);
2246 memcpy(ptr, chunk, item_size);
2247 item_size += sizeof(disk_key);
2248 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2252 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
2253 int num_stripes, int sub_stripes)
2255 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2257 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2258 return calc_size * (num_stripes / sub_stripes);
2260 return calc_size * num_stripes;
2263 /* Used to sort the devices by max_avail(descending sort) */
2264 int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
2266 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2267 ((struct btrfs_device_info *)dev_info2)->max_avail)
2269 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2270 ((struct btrfs_device_info *)dev_info2)->max_avail)
2276 static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2277 int *num_stripes, int *min_stripes,
2284 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2285 *num_stripes = fs_devices->rw_devices;
2288 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2292 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2293 if (fs_devices->rw_devices < 2)
2298 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2299 *num_stripes = fs_devices->rw_devices;
2300 if (*num_stripes < 4)
2302 *num_stripes &= ~(u32)1;
2310 static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2311 u64 proposed_size, u64 type,
2312 int num_stripes, int small_stripe)
2314 int min_stripe_size = 1 * 1024 * 1024;
2315 u64 calc_size = proposed_size;
2316 u64 max_chunk_size = calc_size;
2319 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2320 BTRFS_BLOCK_GROUP_DUP |
2321 BTRFS_BLOCK_GROUP_RAID10))
2324 if (type & BTRFS_BLOCK_GROUP_DATA) {
2325 max_chunk_size = 10 * calc_size;
2326 min_stripe_size = 64 * 1024 * 1024;
2327 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2328 max_chunk_size = 256 * 1024 * 1024;
2329 min_stripe_size = 32 * 1024 * 1024;
2330 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2331 calc_size = 8 * 1024 * 1024;
2332 max_chunk_size = calc_size * 2;
2333 min_stripe_size = 1 * 1024 * 1024;
2336 /* we don't want a chunk larger than 10% of writeable space */
2337 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2340 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2341 calc_size = max_chunk_size * ncopies;
2342 do_div(calc_size, num_stripes);
2343 do_div(calc_size, BTRFS_STRIPE_LEN);
2344 calc_size *= BTRFS_STRIPE_LEN;
2347 /* we don't want tiny stripes */
2349 calc_size = max_t(u64, min_stripe_size, calc_size);
2352 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
2353 * we end up with something bigger than a stripe
2355 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
2357 do_div(calc_size, BTRFS_STRIPE_LEN);
2358 calc_size *= BTRFS_STRIPE_LEN;
2363 static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2366 struct map_lookup *new;
2367 size_t len = map_lookup_size(num_stripes);
2369 BUG_ON(map->num_stripes < num_stripes);
2371 if (map->num_stripes == num_stripes)
2374 new = kmalloc(len, GFP_NOFS);
2376 /* just change map->num_stripes */
2377 map->num_stripes = num_stripes;
2381 memcpy(new, map, len);
2382 new->num_stripes = num_stripes;
2388 * helper to allocate device space from btrfs_device_info, in which we stored
2389 * max free space information of every device. It is used when we can not
2390 * allocate chunks by default size.
2392 * By this helper, we can allocate a new chunk as larger as possible.
2394 static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2395 struct btrfs_fs_devices *fs_devices,
2396 struct btrfs_device_info *devices,
2397 int nr_device, u64 type,
2398 struct map_lookup **map_lookup,
2399 int min_stripes, u64 *stripe_size)
2401 int i, index, sort_again = 0;
2402 int min_devices = min_stripes;
2403 u64 max_avail, min_free;
2404 struct map_lookup *map = *map_lookup;
2407 if (nr_device < min_stripes)
2410 btrfs_descending_sort_devices(devices, nr_device);
2412 max_avail = devices[0].max_avail;
2416 for (i = 0; i < nr_device; i++) {
2418 * if dev_offset = 0, it means the free space of this device
2419 * is less than what we need, and we didn't search max avail
2420 * extent on this device, so do it now.
2422 if (!devices[i].dev_offset) {
2423 ret = find_free_dev_extent(trans, devices[i].dev,
2425 &devices[i].dev_offset,
2426 &devices[i].max_avail);
2427 if (ret != 0 && ret != -ENOSPC)
2433 /* we update the max avail free extent of each devices, sort again */
2435 btrfs_descending_sort_devices(devices, nr_device);
2437 if (type & BTRFS_BLOCK_GROUP_DUP)
2440 if (!devices[min_devices - 1].max_avail)
2443 max_avail = devices[min_devices - 1].max_avail;
2444 if (type & BTRFS_BLOCK_GROUP_DUP)
2445 do_div(max_avail, 2);
2447 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2449 if (type & BTRFS_BLOCK_GROUP_DUP)
2450 min_free = max_avail * 2;
2452 min_free = max_avail;
2454 if (min_free > devices[min_devices - 1].max_avail)
2457 map = __shrink_map_lookup_stripes(map, min_stripes);
2458 *stripe_size = max_avail;
2461 for (i = 0; i < min_stripes; i++) {
2462 map->stripes[i].dev = devices[index].dev;
2463 map->stripes[i].physical = devices[index].dev_offset;
2464 if (type & BTRFS_BLOCK_GROUP_DUP) {
2466 map->stripes[i].dev = devices[index].dev;
2467 map->stripes[i].physical = devices[index].dev_offset +
2477 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2478 struct btrfs_root *extent_root,
2479 struct map_lookup **map_ret,
2480 u64 *num_bytes, u64 *stripe_size,
2481 u64 start, u64 type)
2483 struct btrfs_fs_info *info = extent_root->fs_info;
2484 struct btrfs_device *device = NULL;
2485 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2486 struct list_head *cur;
2487 struct map_lookup *map;
2488 struct extent_map_tree *em_tree;
2489 struct extent_map *em;
2490 struct btrfs_device_info *devices_info;
2491 struct list_head private_devs;
2492 u64 calc_size = 1024 * 1024 * 1024;
2499 int min_devices; /* the min number of devices we need */
2504 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2505 (type & BTRFS_BLOCK_GROUP_DUP)) {
2507 type &= ~BTRFS_BLOCK_GROUP_DUP;
2509 if (list_empty(&fs_devices->alloc_list))
2512 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2513 &min_stripes, &sub_stripes);
2517 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2522 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2527 map->num_stripes = num_stripes;
2529 cur = fs_devices->alloc_list.next;
2533 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2536 if (type & BTRFS_BLOCK_GROUP_DUP) {
2537 min_free = calc_size * 2;
2540 min_free = calc_size;
2541 min_devices = min_stripes;
2544 INIT_LIST_HEAD(&private_devs);
2545 while (index < num_stripes) {
2546 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2547 BUG_ON(!device->writeable);
2548 if (device->total_bytes > device->bytes_used)
2549 avail = device->total_bytes - device->bytes_used;
2554 if (device->in_fs_metadata && avail >= min_free) {
2555 ret = find_free_dev_extent(trans, device, min_free,
2556 &devices_info[i].dev_offset,
2557 &devices_info[i].max_avail);
2559 list_move_tail(&device->dev_alloc_list,
2561 map->stripes[index].dev = device;
2562 map->stripes[index].physical =
2563 devices_info[i].dev_offset;
2565 if (type & BTRFS_BLOCK_GROUP_DUP) {
2566 map->stripes[index].dev = device;
2567 map->stripes[index].physical =
2568 devices_info[i].dev_offset +
2572 } else if (ret != -ENOSPC)
2575 devices_info[i].dev = device;
2577 } else if (device->in_fs_metadata &&
2578 avail >= BTRFS_STRIPE_LEN) {
2579 devices_info[i].dev = device;
2580 devices_info[i].max_avail = avail;
2584 if (cur == &fs_devices->alloc_list)
2588 list_splice(&private_devs, &fs_devices->alloc_list);
2589 if (index < num_stripes) {
2590 if (index >= min_stripes) {
2591 num_stripes = index;
2592 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2593 num_stripes /= sub_stripes;
2594 num_stripes *= sub_stripes;
2597 map = __shrink_map_lookup_stripes(map, num_stripes);
2598 } else if (i >= min_devices) {
2599 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2600 devices_info, i, type,
2610 map->sector_size = extent_root->sectorsize;
2611 map->stripe_len = BTRFS_STRIPE_LEN;
2612 map->io_align = BTRFS_STRIPE_LEN;
2613 map->io_width = BTRFS_STRIPE_LEN;
2615 map->sub_stripes = sub_stripes;
2618 *stripe_size = calc_size;
2619 *num_bytes = chunk_bytes_by_type(type, calc_size,
2620 map->num_stripes, sub_stripes);
2622 em = alloc_extent_map(GFP_NOFS);
2627 em->bdev = (struct block_device *)map;
2629 em->len = *num_bytes;
2630 em->block_start = 0;
2631 em->block_len = em->len;
2633 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2634 write_lock(&em_tree->lock);
2635 ret = add_extent_mapping(em_tree, em);
2636 write_unlock(&em_tree->lock);
2638 free_extent_map(em);
2640 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2641 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2646 while (index < map->num_stripes) {
2647 device = map->stripes[index].dev;
2648 dev_offset = map->stripes[index].physical;
2650 ret = btrfs_alloc_dev_extent(trans, device,
2651 info->chunk_root->root_key.objectid,
2652 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2653 start, dev_offset, calc_size);
2658 kfree(devices_info);
2663 kfree(devices_info);
2667 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2668 struct btrfs_root *extent_root,
2669 struct map_lookup *map, u64 chunk_offset,
2670 u64 chunk_size, u64 stripe_size)
2673 struct btrfs_key key;
2674 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2675 struct btrfs_device *device;
2676 struct btrfs_chunk *chunk;
2677 struct btrfs_stripe *stripe;
2678 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2682 chunk = kzalloc(item_size, GFP_NOFS);
2687 while (index < map->num_stripes) {
2688 device = map->stripes[index].dev;
2689 device->bytes_used += stripe_size;
2690 ret = btrfs_update_device(trans, device);
2696 stripe = &chunk->stripe;
2697 while (index < map->num_stripes) {
2698 device = map->stripes[index].dev;
2699 dev_offset = map->stripes[index].physical;
2701 btrfs_set_stack_stripe_devid(stripe, device->devid);
2702 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2703 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2708 btrfs_set_stack_chunk_length(chunk, chunk_size);
2709 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2710 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2711 btrfs_set_stack_chunk_type(chunk, map->type);
2712 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2713 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2714 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2715 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2716 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2718 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2719 key.type = BTRFS_CHUNK_ITEM_KEY;
2720 key.offset = chunk_offset;
2722 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2725 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2726 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2735 * Chunk allocation falls into two parts. The first part does works
2736 * that make the new allocated chunk useable, but not do any operation
2737 * that modifies the chunk tree. The second part does the works that
2738 * require modifying the chunk tree. This division is important for the
2739 * bootstrap process of adding storage to a seed btrfs.
2741 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2742 struct btrfs_root *extent_root, u64 type)
2747 struct map_lookup *map;
2748 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2751 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2756 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2757 &stripe_size, chunk_offset, type);
2761 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2762 chunk_size, stripe_size);
2767 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2768 struct btrfs_root *root,
2769 struct btrfs_device *device)
2772 u64 sys_chunk_offset;
2776 u64 sys_stripe_size;
2778 struct map_lookup *map;
2779 struct map_lookup *sys_map;
2780 struct btrfs_fs_info *fs_info = root->fs_info;
2781 struct btrfs_root *extent_root = fs_info->extent_root;
2784 ret = find_next_chunk(fs_info->chunk_root,
2785 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2788 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2789 (fs_info->metadata_alloc_profile &
2790 fs_info->avail_metadata_alloc_bits);
2791 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2793 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2794 &stripe_size, chunk_offset, alloc_profile);
2797 sys_chunk_offset = chunk_offset + chunk_size;
2799 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2800 (fs_info->system_alloc_profile &
2801 fs_info->avail_system_alloc_bits);
2802 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2804 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2805 &sys_chunk_size, &sys_stripe_size,
2806 sys_chunk_offset, alloc_profile);
2809 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2813 * Modifying chunk tree needs allocating new blocks from both
2814 * system block group and metadata block group. So we only can
2815 * do operations require modifying the chunk tree after both
2816 * block groups were created.
2818 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2819 chunk_size, stripe_size);
2822 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2823 sys_chunk_offset, sys_chunk_size,
2829 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2831 struct extent_map *em;
2832 struct map_lookup *map;
2833 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2837 read_lock(&map_tree->map_tree.lock);
2838 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2839 read_unlock(&map_tree->map_tree.lock);
2843 if (btrfs_test_opt(root, DEGRADED)) {
2844 free_extent_map(em);
2848 map = (struct map_lookup *)em->bdev;
2849 for (i = 0; i < map->num_stripes; i++) {
2850 if (!map->stripes[i].dev->writeable) {
2855 free_extent_map(em);
2859 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2861 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2864 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2866 struct extent_map *em;
2869 write_lock(&tree->map_tree.lock);
2870 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2872 remove_extent_mapping(&tree->map_tree, em);
2873 write_unlock(&tree->map_tree.lock);
2878 free_extent_map(em);
2879 /* once for the tree */
2880 free_extent_map(em);
2884 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2886 struct extent_map *em;
2887 struct map_lookup *map;
2888 struct extent_map_tree *em_tree = &map_tree->map_tree;
2891 read_lock(&em_tree->lock);
2892 em = lookup_extent_mapping(em_tree, logical, len);
2893 read_unlock(&em_tree->lock);
2896 BUG_ON(em->start > logical || em->start + em->len < logical);
2897 map = (struct map_lookup *)em->bdev;
2898 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2899 ret = map->num_stripes;
2900 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2901 ret = map->sub_stripes;
2904 free_extent_map(em);
2908 static int find_live_mirror(struct map_lookup *map, int first, int num,
2912 if (map->stripes[optimal].dev->bdev)
2914 for (i = first; i < first + num; i++) {
2915 if (map->stripes[i].dev->bdev)
2918 /* we couldn't find one that doesn't fail. Just return something
2919 * and the io error handling code will clean up eventually
2924 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2925 u64 logical, u64 *length,
2926 struct btrfs_multi_bio **multi_ret,
2927 int mirror_num, struct page *unplug_page)
2929 struct extent_map *em;
2930 struct map_lookup *map;
2931 struct extent_map_tree *em_tree = &map_tree->map_tree;
2935 int stripes_allocated = 8;
2936 int stripes_required = 1;
2941 struct btrfs_multi_bio *multi = NULL;
2943 if (multi_ret && !(rw & REQ_WRITE))
2944 stripes_allocated = 1;
2947 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2952 atomic_set(&multi->error, 0);
2955 read_lock(&em_tree->lock);
2956 em = lookup_extent_mapping(em_tree, logical, *length);
2957 read_unlock(&em_tree->lock);
2959 if (!em && unplug_page) {
2965 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2966 (unsigned long long)logical,
2967 (unsigned long long)*length);
2971 BUG_ON(em->start > logical || em->start + em->len < logical);
2972 map = (struct map_lookup *)em->bdev;
2973 offset = logical - em->start;
2975 if (mirror_num > map->num_stripes)
2978 /* if our multi bio struct is too small, back off and try again */
2979 if (rw & REQ_WRITE) {
2980 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2981 BTRFS_BLOCK_GROUP_DUP)) {
2982 stripes_required = map->num_stripes;
2984 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2985 stripes_required = map->sub_stripes;
2989 if (multi_ret && (rw & REQ_WRITE) &&
2990 stripes_allocated < stripes_required) {
2991 stripes_allocated = map->num_stripes;
2992 free_extent_map(em);
2998 * stripe_nr counts the total number of stripes we have to stride
2999 * to get to this block
3001 do_div(stripe_nr, map->stripe_len);
3003 stripe_offset = stripe_nr * map->stripe_len;
3004 BUG_ON(offset < stripe_offset);
3006 /* stripe_offset is the offset of this block in its stripe*/
3007 stripe_offset = offset - stripe_offset;
3009 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
3010 BTRFS_BLOCK_GROUP_RAID10 |
3011 BTRFS_BLOCK_GROUP_DUP)) {
3012 /* we limit the length of each bio to what fits in a stripe */
3013 *length = min_t(u64, em->len - offset,
3014 map->stripe_len - stripe_offset);
3016 *length = em->len - offset;
3019 if (!multi_ret && !unplug_page)
3024 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3025 if (unplug_page || (rw & REQ_WRITE))
3026 num_stripes = map->num_stripes;
3027 else if (mirror_num)
3028 stripe_index = mirror_num - 1;
3030 stripe_index = find_live_mirror(map, 0,
3032 current->pid % map->num_stripes);
3035 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3037 num_stripes = map->num_stripes;
3038 else if (mirror_num)
3039 stripe_index = mirror_num - 1;
3041 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3042 int factor = map->num_stripes / map->sub_stripes;
3044 stripe_index = do_div(stripe_nr, factor);
3045 stripe_index *= map->sub_stripes;
3047 if (unplug_page || (rw & REQ_WRITE))
3048 num_stripes = map->sub_stripes;
3049 else if (mirror_num)
3050 stripe_index += mirror_num - 1;
3052 stripe_index = find_live_mirror(map, stripe_index,
3053 map->sub_stripes, stripe_index +
3054 current->pid % map->sub_stripes);
3058 * after this do_div call, stripe_nr is the number of stripes
3059 * on this device we have to walk to find the data, and
3060 * stripe_index is the number of our device in the stripe array
3062 stripe_index = do_div(stripe_nr, map->num_stripes);
3064 BUG_ON(stripe_index >= map->num_stripes);
3066 for (i = 0; i < num_stripes; i++) {
3068 struct btrfs_device *device;
3069 struct backing_dev_info *bdi;
3071 device = map->stripes[stripe_index].dev;
3073 bdi = blk_get_backing_dev_info(device->bdev);
3074 if (bdi->unplug_io_fn)
3075 bdi->unplug_io_fn(bdi, unplug_page);
3078 multi->stripes[i].physical =
3079 map->stripes[stripe_index].physical +
3080 stripe_offset + stripe_nr * map->stripe_len;
3081 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3087 multi->num_stripes = num_stripes;
3088 multi->max_errors = max_errors;
3091 free_extent_map(em);
3095 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3096 u64 logical, u64 *length,
3097 struct btrfs_multi_bio **multi_ret, int mirror_num)
3099 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3103 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3104 u64 chunk_start, u64 physical, u64 devid,
3105 u64 **logical, int *naddrs, int *stripe_len)
3107 struct extent_map_tree *em_tree = &map_tree->map_tree;
3108 struct extent_map *em;
3109 struct map_lookup *map;
3116 read_lock(&em_tree->lock);
3117 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3118 read_unlock(&em_tree->lock);
3120 BUG_ON(!em || em->start != chunk_start);
3121 map = (struct map_lookup *)em->bdev;
3124 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3125 do_div(length, map->num_stripes / map->sub_stripes);
3126 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3127 do_div(length, map->num_stripes);
3129 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3132 for (i = 0; i < map->num_stripes; i++) {
3133 if (devid && map->stripes[i].dev->devid != devid)
3135 if (map->stripes[i].physical > physical ||
3136 map->stripes[i].physical + length <= physical)
3139 stripe_nr = physical - map->stripes[i].physical;
3140 do_div(stripe_nr, map->stripe_len);
3142 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3143 stripe_nr = stripe_nr * map->num_stripes + i;
3144 do_div(stripe_nr, map->sub_stripes);
3145 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3146 stripe_nr = stripe_nr * map->num_stripes + i;
3148 bytenr = chunk_start + stripe_nr * map->stripe_len;
3149 WARN_ON(nr >= map->num_stripes);
3150 for (j = 0; j < nr; j++) {
3151 if (buf[j] == bytenr)
3155 WARN_ON(nr >= map->num_stripes);
3162 *stripe_len = map->stripe_len;
3164 free_extent_map(em);
3168 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
3169 u64 logical, struct page *page)
3171 u64 length = PAGE_CACHE_SIZE;
3172 return __btrfs_map_block(map_tree, READ, logical, &length,
3176 static void end_bio_multi_stripe(struct bio *bio, int err)
3178 struct btrfs_multi_bio *multi = bio->bi_private;
3179 int is_orig_bio = 0;
3182 atomic_inc(&multi->error);
3184 if (bio == multi->orig_bio)
3187 if (atomic_dec_and_test(&multi->stripes_pending)) {
3190 bio = multi->orig_bio;
3192 bio->bi_private = multi->private;
3193 bio->bi_end_io = multi->end_io;
3194 /* only send an error to the higher layers if it is
3195 * beyond the tolerance of the multi-bio
3197 if (atomic_read(&multi->error) > multi->max_errors) {
3201 * this bio is actually up to date, we didn't
3202 * go over the max number of errors
3204 set_bit(BIO_UPTODATE, &bio->bi_flags);
3209 bio_endio(bio, err);
3210 } else if (!is_orig_bio) {
3215 struct async_sched {
3218 struct btrfs_fs_info *info;
3219 struct btrfs_work work;
3223 * see run_scheduled_bios for a description of why bios are collected for
3226 * This will add one bio to the pending list for a device and make sure
3227 * the work struct is scheduled.
3229 static noinline int schedule_bio(struct btrfs_root *root,
3230 struct btrfs_device *device,
3231 int rw, struct bio *bio)
3233 int should_queue = 1;
3234 struct btrfs_pending_bios *pending_bios;
3236 /* don't bother with additional async steps for reads, right now */
3237 if (!(rw & REQ_WRITE)) {
3239 submit_bio(rw, bio);
3245 * nr_async_bios allows us to reliably return congestion to the
3246 * higher layers. Otherwise, the async bio makes it appear we have
3247 * made progress against dirty pages when we've really just put it
3248 * on a queue for later
3250 atomic_inc(&root->fs_info->nr_async_bios);
3251 WARN_ON(bio->bi_next);
3252 bio->bi_next = NULL;
3255 spin_lock(&device->io_lock);
3256 if (bio->bi_rw & REQ_SYNC)
3257 pending_bios = &device->pending_sync_bios;
3259 pending_bios = &device->pending_bios;
3261 if (pending_bios->tail)
3262 pending_bios->tail->bi_next = bio;
3264 pending_bios->tail = bio;
3265 if (!pending_bios->head)
3266 pending_bios->head = bio;
3267 if (device->running_pending)
3270 spin_unlock(&device->io_lock);
3273 btrfs_queue_worker(&root->fs_info->submit_workers,
3278 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3279 int mirror_num, int async_submit)
3281 struct btrfs_mapping_tree *map_tree;
3282 struct btrfs_device *dev;
3283 struct bio *first_bio = bio;
3284 u64 logical = (u64)bio->bi_sector << 9;
3287 struct btrfs_multi_bio *multi = NULL;
3292 length = bio->bi_size;
3293 map_tree = &root->fs_info->mapping_tree;
3294 map_length = length;
3296 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3300 total_devs = multi->num_stripes;
3301 if (map_length < length) {
3302 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3303 "len %llu\n", (unsigned long long)logical,
3304 (unsigned long long)length,
3305 (unsigned long long)map_length);
3308 multi->end_io = first_bio->bi_end_io;
3309 multi->private = first_bio->bi_private;
3310 multi->orig_bio = first_bio;
3311 atomic_set(&multi->stripes_pending, multi->num_stripes);
3313 while (dev_nr < total_devs) {
3314 if (total_devs > 1) {
3315 if (dev_nr < total_devs - 1) {
3316 bio = bio_clone(first_bio, GFP_NOFS);
3321 bio->bi_private = multi;
3322 bio->bi_end_io = end_bio_multi_stripe;
3324 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3325 dev = multi->stripes[dev_nr].dev;
3326 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3327 bio->bi_bdev = dev->bdev;
3329 schedule_bio(root, dev, rw, bio);
3331 submit_bio(rw, bio);
3333 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3334 bio->bi_sector = logical >> 9;
3335 bio_endio(bio, -EIO);
3339 if (total_devs == 1)
3344 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3347 struct btrfs_device *device;
3348 struct btrfs_fs_devices *cur_devices;
3350 cur_devices = root->fs_info->fs_devices;
3351 while (cur_devices) {
3353 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3354 device = __find_device(&cur_devices->devices,
3359 cur_devices = cur_devices->seed;
3364 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3365 u64 devid, u8 *dev_uuid)
3367 struct btrfs_device *device;
3368 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3370 device = kzalloc(sizeof(*device), GFP_NOFS);
3373 list_add(&device->dev_list,
3374 &fs_devices->devices);
3375 device->barriers = 1;
3376 device->dev_root = root->fs_info->dev_root;
3377 device->devid = devid;
3378 device->work.func = pending_bios_fn;
3379 device->fs_devices = fs_devices;
3380 device->missing = 1;
3381 fs_devices->num_devices++;
3382 fs_devices->missing_devices++;
3383 spin_lock_init(&device->io_lock);
3384 INIT_LIST_HEAD(&device->dev_alloc_list);
3385 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3389 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3390 struct extent_buffer *leaf,
3391 struct btrfs_chunk *chunk)
3393 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3394 struct map_lookup *map;
3395 struct extent_map *em;
3399 u8 uuid[BTRFS_UUID_SIZE];
3404 logical = key->offset;
3405 length = btrfs_chunk_length(leaf, chunk);
3407 read_lock(&map_tree->map_tree.lock);
3408 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3409 read_unlock(&map_tree->map_tree.lock);
3411 /* already mapped? */
3412 if (em && em->start <= logical && em->start + em->len > logical) {
3413 free_extent_map(em);
3416 free_extent_map(em);
3419 em = alloc_extent_map(GFP_NOFS);
3422 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3423 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3425 free_extent_map(em);
3429 em->bdev = (struct block_device *)map;
3430 em->start = logical;
3432 em->block_start = 0;
3433 em->block_len = em->len;
3435 map->num_stripes = num_stripes;
3436 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3437 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3438 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3439 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3440 map->type = btrfs_chunk_type(leaf, chunk);
3441 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3442 for (i = 0; i < num_stripes; i++) {
3443 map->stripes[i].physical =
3444 btrfs_stripe_offset_nr(leaf, chunk, i);
3445 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3446 read_extent_buffer(leaf, uuid, (unsigned long)
3447 btrfs_stripe_dev_uuid_nr(chunk, i),
3449 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3451 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3453 free_extent_map(em);
3456 if (!map->stripes[i].dev) {
3457 map->stripes[i].dev =
3458 add_missing_dev(root, devid, uuid);
3459 if (!map->stripes[i].dev) {
3461 free_extent_map(em);
3465 map->stripes[i].dev->in_fs_metadata = 1;
3468 write_lock(&map_tree->map_tree.lock);
3469 ret = add_extent_mapping(&map_tree->map_tree, em);
3470 write_unlock(&map_tree->map_tree.lock);
3472 free_extent_map(em);
3477 static int fill_device_from_item(struct extent_buffer *leaf,
3478 struct btrfs_dev_item *dev_item,
3479 struct btrfs_device *device)
3483 device->devid = btrfs_device_id(leaf, dev_item);
3484 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3485 device->total_bytes = device->disk_total_bytes;
3486 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3487 device->type = btrfs_device_type(leaf, dev_item);
3488 device->io_align = btrfs_device_io_align(leaf, dev_item);
3489 device->io_width = btrfs_device_io_width(leaf, dev_item);
3490 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3492 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3493 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3498 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3500 struct btrfs_fs_devices *fs_devices;
3503 mutex_lock(&uuid_mutex);
3505 fs_devices = root->fs_info->fs_devices->seed;
3506 while (fs_devices) {
3507 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3511 fs_devices = fs_devices->seed;
3514 fs_devices = find_fsid(fsid);
3520 fs_devices = clone_fs_devices(fs_devices);
3521 if (IS_ERR(fs_devices)) {
3522 ret = PTR_ERR(fs_devices);
3526 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3527 root->fs_info->bdev_holder);
3531 if (!fs_devices->seeding) {
3532 __btrfs_close_devices(fs_devices);
3533 free_fs_devices(fs_devices);
3538 fs_devices->seed = root->fs_info->fs_devices->seed;
3539 root->fs_info->fs_devices->seed = fs_devices;
3541 mutex_unlock(&uuid_mutex);
3545 static int read_one_dev(struct btrfs_root *root,
3546 struct extent_buffer *leaf,
3547 struct btrfs_dev_item *dev_item)
3549 struct btrfs_device *device;
3552 u8 fs_uuid[BTRFS_UUID_SIZE];
3553 u8 dev_uuid[BTRFS_UUID_SIZE];
3555 devid = btrfs_device_id(leaf, dev_item);
3556 read_extent_buffer(leaf, dev_uuid,
3557 (unsigned long)btrfs_device_uuid(dev_item),
3559 read_extent_buffer(leaf, fs_uuid,
3560 (unsigned long)btrfs_device_fsid(dev_item),
3563 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3564 ret = open_seed_devices(root, fs_uuid);
3565 if (ret && !btrfs_test_opt(root, DEGRADED))
3569 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3570 if (!device || !device->bdev) {
3571 if (!btrfs_test_opt(root, DEGRADED))
3575 printk(KERN_WARNING "warning devid %llu missing\n",
3576 (unsigned long long)devid);
3577 device = add_missing_dev(root, devid, dev_uuid);
3580 } else if (!device->missing) {
3582 * this happens when a device that was properly setup
3583 * in the device info lists suddenly goes bad.
3584 * device->bdev is NULL, and so we have to set
3585 * device->missing to one here
3587 root->fs_info->fs_devices->missing_devices++;
3588 device->missing = 1;
3592 if (device->fs_devices != root->fs_info->fs_devices) {
3593 BUG_ON(device->writeable);
3594 if (device->generation !=
3595 btrfs_device_generation(leaf, dev_item))
3599 fill_device_from_item(leaf, dev_item, device);
3600 device->dev_root = root->fs_info->dev_root;
3601 device->in_fs_metadata = 1;
3602 if (device->writeable)
3603 device->fs_devices->total_rw_bytes += device->total_bytes;
3608 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3610 struct btrfs_dev_item *dev_item;
3612 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3614 return read_one_dev(root, buf, dev_item);
3617 int btrfs_read_sys_array(struct btrfs_root *root)
3619 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3620 struct extent_buffer *sb;
3621 struct btrfs_disk_key *disk_key;
3622 struct btrfs_chunk *chunk;
3624 unsigned long sb_ptr;
3630 struct btrfs_key key;
3632 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3633 BTRFS_SUPER_INFO_SIZE);
3636 btrfs_set_buffer_uptodate(sb);
3637 btrfs_set_buffer_lockdep_class(sb, 0);
3639 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3640 array_size = btrfs_super_sys_array_size(super_copy);
3642 ptr = super_copy->sys_chunk_array;
3643 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3646 while (cur < array_size) {
3647 disk_key = (struct btrfs_disk_key *)ptr;
3648 btrfs_disk_key_to_cpu(&key, disk_key);
3650 len = sizeof(*disk_key); ptr += len;
3654 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3655 chunk = (struct btrfs_chunk *)sb_ptr;
3656 ret = read_one_chunk(root, &key, sb, chunk);
3659 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3660 len = btrfs_chunk_item_size(num_stripes);
3669 free_extent_buffer(sb);
3673 int btrfs_read_chunk_tree(struct btrfs_root *root)
3675 struct btrfs_path *path;
3676 struct extent_buffer *leaf;
3677 struct btrfs_key key;
3678 struct btrfs_key found_key;
3682 root = root->fs_info->chunk_root;
3684 path = btrfs_alloc_path();
3688 /* first we search for all of the device items, and then we
3689 * read in all of the chunk items. This way we can create chunk
3690 * mappings that reference all of the devices that are afound
3692 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3696 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3700 leaf = path->nodes[0];
3701 slot = path->slots[0];
3702 if (slot >= btrfs_header_nritems(leaf)) {
3703 ret = btrfs_next_leaf(root, path);
3710 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3711 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3712 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3714 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3715 struct btrfs_dev_item *dev_item;
3716 dev_item = btrfs_item_ptr(leaf, slot,
3717 struct btrfs_dev_item);
3718 ret = read_one_dev(root, leaf, dev_item);
3722 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3723 struct btrfs_chunk *chunk;
3724 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3725 ret = read_one_chunk(root, &found_key, leaf, chunk);
3731 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3733 btrfs_release_path(root, path);
3738 btrfs_free_path(path);