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);
732 * find_free_dev_extent - find free space in the specified device
733 * @trans: transaction handler
734 * @device: the device which we search the free space in
735 * @num_bytes: the size of the free space that we need
736 * @start: store the start of the free space.
737 * @len: the size of the free space. that we find, or the size of the max
738 * free space if we don't find suitable free space
740 * this uses a pretty simple search, the expectation is that it is
741 * called very infrequently and that a given device has a small number
744 * @start is used to store the start of the free space if we find. But if we
745 * don't find suitable free space, it will be used to store the start position
746 * of the max free space.
748 * @len is used to store the size of the free space that we find.
749 * But if we don't find suitable free space, it is used to store the size of
750 * the max free space.
752 int find_free_dev_extent(struct btrfs_trans_handle *trans,
753 struct btrfs_device *device, u64 num_bytes,
754 u64 *start, u64 *len)
756 struct btrfs_key key;
757 struct btrfs_root *root = device->dev_root;
758 struct btrfs_dev_extent *dev_extent;
759 struct btrfs_path *path;
765 u64 search_end = device->total_bytes;
768 struct extent_buffer *l;
770 /* FIXME use last free of some kind */
772 /* we don't want to overwrite the superblock on the drive,
773 * so we make sure to start at an offset of at least 1MB
775 search_start = 1024 * 1024;
777 if (root->fs_info->alloc_start + num_bytes <= search_end)
778 search_start = max(root->fs_info->alloc_start, search_start);
780 max_hole_start = search_start;
783 if (search_start >= search_end) {
788 path = btrfs_alloc_path();
795 key.objectid = device->devid;
796 key.offset = search_start;
797 key.type = BTRFS_DEV_EXTENT_KEY;
799 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
803 ret = btrfs_previous_item(root, path, key.objectid, key.type);
810 slot = path->slots[0];
811 if (slot >= btrfs_header_nritems(l)) {
812 ret = btrfs_next_leaf(root, path);
820 btrfs_item_key_to_cpu(l, &key, slot);
822 if (key.objectid < device->devid)
825 if (key.objectid > device->devid)
828 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
831 if (key.offset > search_start) {
832 hole_size = key.offset - search_start;
834 if (hole_size > max_hole_size) {
835 max_hole_start = search_start;
836 max_hole_size = hole_size;
840 * If this free space is greater than which we need,
841 * it must be the max free space that we have found
842 * until now, so max_hole_start must point to the start
843 * of this free space and the length of this free space
844 * is stored in max_hole_size. Thus, we return
845 * max_hole_start and max_hole_size and go back to the
848 if (hole_size >= num_bytes) {
854 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
855 extent_end = key.offset + btrfs_dev_extent_length(l,
857 if (extent_end > search_start)
858 search_start = extent_end;
864 hole_size = search_end- search_start;
865 if (hole_size > max_hole_size) {
866 max_hole_start = search_start;
867 max_hole_size = hole_size;
871 if (hole_size < num_bytes)
877 btrfs_free_path(path);
879 *start = max_hole_start;
881 *len = max_hole_size;
885 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
886 struct btrfs_device *device,
890 struct btrfs_path *path;
891 struct btrfs_root *root = device->dev_root;
892 struct btrfs_key key;
893 struct btrfs_key found_key;
894 struct extent_buffer *leaf = NULL;
895 struct btrfs_dev_extent *extent = NULL;
897 path = btrfs_alloc_path();
901 key.objectid = device->devid;
903 key.type = BTRFS_DEV_EXTENT_KEY;
905 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
907 ret = btrfs_previous_item(root, path, key.objectid,
908 BTRFS_DEV_EXTENT_KEY);
910 leaf = path->nodes[0];
911 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
912 extent = btrfs_item_ptr(leaf, path->slots[0],
913 struct btrfs_dev_extent);
914 BUG_ON(found_key.offset > start || found_key.offset +
915 btrfs_dev_extent_length(leaf, extent) < start);
917 } else if (ret == 0) {
918 leaf = path->nodes[0];
919 extent = btrfs_item_ptr(leaf, path->slots[0],
920 struct btrfs_dev_extent);
924 if (device->bytes_used > 0)
925 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
926 ret = btrfs_del_item(trans, root, path);
929 btrfs_free_path(path);
933 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
934 struct btrfs_device *device,
935 u64 chunk_tree, u64 chunk_objectid,
936 u64 chunk_offset, u64 start, u64 num_bytes)
939 struct btrfs_path *path;
940 struct btrfs_root *root = device->dev_root;
941 struct btrfs_dev_extent *extent;
942 struct extent_buffer *leaf;
943 struct btrfs_key key;
945 WARN_ON(!device->in_fs_metadata);
946 path = btrfs_alloc_path();
950 key.objectid = device->devid;
952 key.type = BTRFS_DEV_EXTENT_KEY;
953 ret = btrfs_insert_empty_item(trans, root, path, &key,
957 leaf = path->nodes[0];
958 extent = btrfs_item_ptr(leaf, path->slots[0],
959 struct btrfs_dev_extent);
960 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
961 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
962 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
964 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
965 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
968 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
969 btrfs_mark_buffer_dirty(leaf);
970 btrfs_free_path(path);
974 static noinline int find_next_chunk(struct btrfs_root *root,
975 u64 objectid, u64 *offset)
977 struct btrfs_path *path;
979 struct btrfs_key key;
980 struct btrfs_chunk *chunk;
981 struct btrfs_key found_key;
983 path = btrfs_alloc_path();
986 key.objectid = objectid;
987 key.offset = (u64)-1;
988 key.type = BTRFS_CHUNK_ITEM_KEY;
990 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
996 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1000 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1002 if (found_key.objectid != objectid)
1005 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1006 struct btrfs_chunk);
1007 *offset = found_key.offset +
1008 btrfs_chunk_length(path->nodes[0], chunk);
1013 btrfs_free_path(path);
1017 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1020 struct btrfs_key key;
1021 struct btrfs_key found_key;
1022 struct btrfs_path *path;
1024 root = root->fs_info->chunk_root;
1026 path = btrfs_alloc_path();
1030 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1031 key.type = BTRFS_DEV_ITEM_KEY;
1032 key.offset = (u64)-1;
1034 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1040 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1041 BTRFS_DEV_ITEM_KEY);
1045 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1047 *objectid = found_key.offset + 1;
1051 btrfs_free_path(path);
1056 * the device information is stored in the chunk root
1057 * the btrfs_device struct should be fully filled in
1059 int btrfs_add_device(struct btrfs_trans_handle *trans,
1060 struct btrfs_root *root,
1061 struct btrfs_device *device)
1064 struct btrfs_path *path;
1065 struct btrfs_dev_item *dev_item;
1066 struct extent_buffer *leaf;
1067 struct btrfs_key key;
1070 root = root->fs_info->chunk_root;
1072 path = btrfs_alloc_path();
1076 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1077 key.type = BTRFS_DEV_ITEM_KEY;
1078 key.offset = device->devid;
1080 ret = btrfs_insert_empty_item(trans, root, path, &key,
1085 leaf = path->nodes[0];
1086 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1088 btrfs_set_device_id(leaf, dev_item, device->devid);
1089 btrfs_set_device_generation(leaf, dev_item, 0);
1090 btrfs_set_device_type(leaf, dev_item, device->type);
1091 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1092 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1093 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1094 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1095 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1096 btrfs_set_device_group(leaf, dev_item, 0);
1097 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1098 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1099 btrfs_set_device_start_offset(leaf, dev_item, 0);
1101 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1102 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1103 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1104 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1105 btrfs_mark_buffer_dirty(leaf);
1109 btrfs_free_path(path);
1113 static int btrfs_rm_dev_item(struct btrfs_root *root,
1114 struct btrfs_device *device)
1117 struct btrfs_path *path;
1118 struct btrfs_key key;
1119 struct btrfs_trans_handle *trans;
1121 root = root->fs_info->chunk_root;
1123 path = btrfs_alloc_path();
1127 trans = btrfs_start_transaction(root, 0);
1128 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1129 key.type = BTRFS_DEV_ITEM_KEY;
1130 key.offset = device->devid;
1133 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1142 ret = btrfs_del_item(trans, root, path);
1146 btrfs_free_path(path);
1147 unlock_chunks(root);
1148 btrfs_commit_transaction(trans, root);
1152 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1154 struct btrfs_device *device;
1155 struct btrfs_device *next_device;
1156 struct block_device *bdev;
1157 struct buffer_head *bh = NULL;
1158 struct btrfs_super_block *disk_super;
1165 mutex_lock(&uuid_mutex);
1166 mutex_lock(&root->fs_info->volume_mutex);
1168 all_avail = root->fs_info->avail_data_alloc_bits |
1169 root->fs_info->avail_system_alloc_bits |
1170 root->fs_info->avail_metadata_alloc_bits;
1172 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1173 root->fs_info->fs_devices->num_devices <= 4) {
1174 printk(KERN_ERR "btrfs: unable to go below four devices "
1180 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1181 root->fs_info->fs_devices->num_devices <= 2) {
1182 printk(KERN_ERR "btrfs: unable to go below two "
1183 "devices on raid1\n");
1188 if (strcmp(device_path, "missing") == 0) {
1189 struct list_head *devices;
1190 struct btrfs_device *tmp;
1193 devices = &root->fs_info->fs_devices->devices;
1194 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1195 list_for_each_entry(tmp, devices, dev_list) {
1196 if (tmp->in_fs_metadata && !tmp->bdev) {
1201 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1206 printk(KERN_ERR "btrfs: no missing devices found to "
1211 bdev = open_bdev_exclusive(device_path, FMODE_READ,
1212 root->fs_info->bdev_holder);
1214 ret = PTR_ERR(bdev);
1218 set_blocksize(bdev, 4096);
1219 bh = btrfs_read_dev_super(bdev);
1224 disk_super = (struct btrfs_super_block *)bh->b_data;
1225 devid = btrfs_stack_device_id(&disk_super->dev_item);
1226 dev_uuid = disk_super->dev_item.uuid;
1227 device = btrfs_find_device(root, devid, dev_uuid,
1235 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1236 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1242 if (device->writeable) {
1243 list_del_init(&device->dev_alloc_list);
1244 root->fs_info->fs_devices->rw_devices--;
1247 ret = btrfs_shrink_device(device, 0);
1251 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1255 device->in_fs_metadata = 0;
1258 * the device list mutex makes sure that we don't change
1259 * the device list while someone else is writing out all
1260 * the device supers.
1262 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1263 list_del_init(&device->dev_list);
1264 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1266 device->fs_devices->num_devices--;
1268 if (device->missing)
1269 root->fs_info->fs_devices->missing_devices--;
1271 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1272 struct btrfs_device, dev_list);
1273 if (device->bdev == root->fs_info->sb->s_bdev)
1274 root->fs_info->sb->s_bdev = next_device->bdev;
1275 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1276 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1279 close_bdev_exclusive(device->bdev, device->mode);
1280 device->bdev = NULL;
1281 device->fs_devices->open_devices--;
1284 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1285 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1287 if (device->fs_devices->open_devices == 0) {
1288 struct btrfs_fs_devices *fs_devices;
1289 fs_devices = root->fs_info->fs_devices;
1290 while (fs_devices) {
1291 if (fs_devices->seed == device->fs_devices)
1293 fs_devices = fs_devices->seed;
1295 fs_devices->seed = device->fs_devices->seed;
1296 device->fs_devices->seed = NULL;
1297 __btrfs_close_devices(device->fs_devices);
1298 free_fs_devices(device->fs_devices);
1302 * at this point, the device is zero sized. We want to
1303 * remove it from the devices list and zero out the old super
1305 if (device->writeable) {
1306 /* make sure this device isn't detected as part of
1309 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1310 set_buffer_dirty(bh);
1311 sync_dirty_buffer(bh);
1314 kfree(device->name);
1322 close_bdev_exclusive(bdev, FMODE_READ);
1324 mutex_unlock(&root->fs_info->volume_mutex);
1325 mutex_unlock(&uuid_mutex);
1330 * does all the dirty work required for changing file system's UUID.
1332 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1333 struct btrfs_root *root)
1335 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1336 struct btrfs_fs_devices *old_devices;
1337 struct btrfs_fs_devices *seed_devices;
1338 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1339 struct btrfs_device *device;
1342 BUG_ON(!mutex_is_locked(&uuid_mutex));
1343 if (!fs_devices->seeding)
1346 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1350 old_devices = clone_fs_devices(fs_devices);
1351 if (IS_ERR(old_devices)) {
1352 kfree(seed_devices);
1353 return PTR_ERR(old_devices);
1356 list_add(&old_devices->list, &fs_uuids);
1358 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1359 seed_devices->opened = 1;
1360 INIT_LIST_HEAD(&seed_devices->devices);
1361 INIT_LIST_HEAD(&seed_devices->alloc_list);
1362 mutex_init(&seed_devices->device_list_mutex);
1363 list_splice_init(&fs_devices->devices, &seed_devices->devices);
1364 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1365 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1366 device->fs_devices = seed_devices;
1369 fs_devices->seeding = 0;
1370 fs_devices->num_devices = 0;
1371 fs_devices->open_devices = 0;
1372 fs_devices->seed = seed_devices;
1374 generate_random_uuid(fs_devices->fsid);
1375 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1376 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1377 super_flags = btrfs_super_flags(disk_super) &
1378 ~BTRFS_SUPER_FLAG_SEEDING;
1379 btrfs_set_super_flags(disk_super, super_flags);
1385 * strore the expected generation for seed devices in device items.
1387 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1388 struct btrfs_root *root)
1390 struct btrfs_path *path;
1391 struct extent_buffer *leaf;
1392 struct btrfs_dev_item *dev_item;
1393 struct btrfs_device *device;
1394 struct btrfs_key key;
1395 u8 fs_uuid[BTRFS_UUID_SIZE];
1396 u8 dev_uuid[BTRFS_UUID_SIZE];
1400 path = btrfs_alloc_path();
1404 root = root->fs_info->chunk_root;
1405 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1407 key.type = BTRFS_DEV_ITEM_KEY;
1410 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1414 leaf = path->nodes[0];
1416 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1417 ret = btrfs_next_leaf(root, path);
1422 leaf = path->nodes[0];
1423 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1424 btrfs_release_path(root, path);
1428 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1429 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1430 key.type != BTRFS_DEV_ITEM_KEY)
1433 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1434 struct btrfs_dev_item);
1435 devid = btrfs_device_id(leaf, dev_item);
1436 read_extent_buffer(leaf, dev_uuid,
1437 (unsigned long)btrfs_device_uuid(dev_item),
1439 read_extent_buffer(leaf, fs_uuid,
1440 (unsigned long)btrfs_device_fsid(dev_item),
1442 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1445 if (device->fs_devices->seeding) {
1446 btrfs_set_device_generation(leaf, dev_item,
1447 device->generation);
1448 btrfs_mark_buffer_dirty(leaf);
1456 btrfs_free_path(path);
1460 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1462 struct btrfs_trans_handle *trans;
1463 struct btrfs_device *device;
1464 struct block_device *bdev;
1465 struct list_head *devices;
1466 struct super_block *sb = root->fs_info->sb;
1468 int seeding_dev = 0;
1471 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1474 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
1476 return PTR_ERR(bdev);
1478 if (root->fs_info->fs_devices->seeding) {
1480 down_write(&sb->s_umount);
1481 mutex_lock(&uuid_mutex);
1484 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1485 mutex_lock(&root->fs_info->volume_mutex);
1487 devices = &root->fs_info->fs_devices->devices;
1489 * we have the volume lock, so we don't need the extra
1490 * device list mutex while reading the list here.
1492 list_for_each_entry(device, devices, dev_list) {
1493 if (device->bdev == bdev) {
1499 device = kzalloc(sizeof(*device), GFP_NOFS);
1501 /* we can safely leave the fs_devices entry around */
1506 device->name = kstrdup(device_path, GFP_NOFS);
1507 if (!device->name) {
1513 ret = find_next_devid(root, &device->devid);
1519 trans = btrfs_start_transaction(root, 0);
1522 device->barriers = 1;
1523 device->writeable = 1;
1524 device->work.func = pending_bios_fn;
1525 generate_random_uuid(device->uuid);
1526 spin_lock_init(&device->io_lock);
1527 device->generation = trans->transid;
1528 device->io_width = root->sectorsize;
1529 device->io_align = root->sectorsize;
1530 device->sector_size = root->sectorsize;
1531 device->total_bytes = i_size_read(bdev->bd_inode);
1532 device->disk_total_bytes = device->total_bytes;
1533 device->dev_root = root->fs_info->dev_root;
1534 device->bdev = bdev;
1535 device->in_fs_metadata = 1;
1537 set_blocksize(device->bdev, 4096);
1540 sb->s_flags &= ~MS_RDONLY;
1541 ret = btrfs_prepare_sprout(trans, root);
1545 device->fs_devices = root->fs_info->fs_devices;
1548 * we don't want write_supers to jump in here with our device
1551 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1552 list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1553 list_add(&device->dev_alloc_list,
1554 &root->fs_info->fs_devices->alloc_list);
1555 root->fs_info->fs_devices->num_devices++;
1556 root->fs_info->fs_devices->open_devices++;
1557 root->fs_info->fs_devices->rw_devices++;
1558 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1560 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1561 root->fs_info->fs_devices->rotating = 1;
1563 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1564 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1565 total_bytes + device->total_bytes);
1567 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1568 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1570 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1573 ret = init_first_rw_device(trans, root, device);
1575 ret = btrfs_finish_sprout(trans, root);
1578 ret = btrfs_add_device(trans, root, device);
1582 * we've got more storage, clear any full flags on the space
1585 btrfs_clear_space_info_full(root->fs_info);
1587 unlock_chunks(root);
1588 btrfs_commit_transaction(trans, root);
1591 mutex_unlock(&uuid_mutex);
1592 up_write(&sb->s_umount);
1594 ret = btrfs_relocate_sys_chunks(root);
1598 mutex_unlock(&root->fs_info->volume_mutex);
1601 close_bdev_exclusive(bdev, 0);
1603 mutex_unlock(&uuid_mutex);
1604 up_write(&sb->s_umount);
1609 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1610 struct btrfs_device *device)
1613 struct btrfs_path *path;
1614 struct btrfs_root *root;
1615 struct btrfs_dev_item *dev_item;
1616 struct extent_buffer *leaf;
1617 struct btrfs_key key;
1619 root = device->dev_root->fs_info->chunk_root;
1621 path = btrfs_alloc_path();
1625 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1626 key.type = BTRFS_DEV_ITEM_KEY;
1627 key.offset = device->devid;
1629 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1638 leaf = path->nodes[0];
1639 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1641 btrfs_set_device_id(leaf, dev_item, device->devid);
1642 btrfs_set_device_type(leaf, dev_item, device->type);
1643 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1644 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1645 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1646 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1647 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1648 btrfs_mark_buffer_dirty(leaf);
1651 btrfs_free_path(path);
1655 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1656 struct btrfs_device *device, u64 new_size)
1658 struct btrfs_super_block *super_copy =
1659 &device->dev_root->fs_info->super_copy;
1660 u64 old_total = btrfs_super_total_bytes(super_copy);
1661 u64 diff = new_size - device->total_bytes;
1663 if (!device->writeable)
1665 if (new_size <= device->total_bytes)
1668 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1669 device->fs_devices->total_rw_bytes += diff;
1671 device->total_bytes = new_size;
1672 device->disk_total_bytes = new_size;
1673 btrfs_clear_space_info_full(device->dev_root->fs_info);
1675 return btrfs_update_device(trans, device);
1678 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1679 struct btrfs_device *device, u64 new_size)
1682 lock_chunks(device->dev_root);
1683 ret = __btrfs_grow_device(trans, device, new_size);
1684 unlock_chunks(device->dev_root);
1688 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1689 struct btrfs_root *root,
1690 u64 chunk_tree, u64 chunk_objectid,
1694 struct btrfs_path *path;
1695 struct btrfs_key key;
1697 root = root->fs_info->chunk_root;
1698 path = btrfs_alloc_path();
1702 key.objectid = chunk_objectid;
1703 key.offset = chunk_offset;
1704 key.type = BTRFS_CHUNK_ITEM_KEY;
1706 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1709 ret = btrfs_del_item(trans, root, path);
1712 btrfs_free_path(path);
1716 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1719 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1720 struct btrfs_disk_key *disk_key;
1721 struct btrfs_chunk *chunk;
1728 struct btrfs_key key;
1730 array_size = btrfs_super_sys_array_size(super_copy);
1732 ptr = super_copy->sys_chunk_array;
1735 while (cur < array_size) {
1736 disk_key = (struct btrfs_disk_key *)ptr;
1737 btrfs_disk_key_to_cpu(&key, disk_key);
1739 len = sizeof(*disk_key);
1741 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1742 chunk = (struct btrfs_chunk *)(ptr + len);
1743 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1744 len += btrfs_chunk_item_size(num_stripes);
1749 if (key.objectid == chunk_objectid &&
1750 key.offset == chunk_offset) {
1751 memmove(ptr, ptr + len, array_size - (cur + len));
1753 btrfs_set_super_sys_array_size(super_copy, array_size);
1762 static int btrfs_relocate_chunk(struct btrfs_root *root,
1763 u64 chunk_tree, u64 chunk_objectid,
1766 struct extent_map_tree *em_tree;
1767 struct btrfs_root *extent_root;
1768 struct btrfs_trans_handle *trans;
1769 struct extent_map *em;
1770 struct map_lookup *map;
1774 root = root->fs_info->chunk_root;
1775 extent_root = root->fs_info->extent_root;
1776 em_tree = &root->fs_info->mapping_tree.map_tree;
1778 ret = btrfs_can_relocate(extent_root, chunk_offset);
1782 /* step one, relocate all the extents inside this chunk */
1783 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1787 trans = btrfs_start_transaction(root, 0);
1793 * step two, delete the device extents and the
1794 * chunk tree entries
1796 read_lock(&em_tree->lock);
1797 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1798 read_unlock(&em_tree->lock);
1800 BUG_ON(em->start > chunk_offset ||
1801 em->start + em->len < chunk_offset);
1802 map = (struct map_lookup *)em->bdev;
1804 for (i = 0; i < map->num_stripes; i++) {
1805 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1806 map->stripes[i].physical);
1809 if (map->stripes[i].dev) {
1810 ret = btrfs_update_device(trans, map->stripes[i].dev);
1814 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1819 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1820 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1824 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1827 write_lock(&em_tree->lock);
1828 remove_extent_mapping(em_tree, em);
1829 write_unlock(&em_tree->lock);
1834 /* once for the tree */
1835 free_extent_map(em);
1837 free_extent_map(em);
1839 unlock_chunks(root);
1840 btrfs_end_transaction(trans, root);
1844 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1846 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1847 struct btrfs_path *path;
1848 struct extent_buffer *leaf;
1849 struct btrfs_chunk *chunk;
1850 struct btrfs_key key;
1851 struct btrfs_key found_key;
1852 u64 chunk_tree = chunk_root->root_key.objectid;
1854 bool retried = false;
1858 path = btrfs_alloc_path();
1863 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1864 key.offset = (u64)-1;
1865 key.type = BTRFS_CHUNK_ITEM_KEY;
1868 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1873 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1880 leaf = path->nodes[0];
1881 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1883 chunk = btrfs_item_ptr(leaf, path->slots[0],
1884 struct btrfs_chunk);
1885 chunk_type = btrfs_chunk_type(leaf, chunk);
1886 btrfs_release_path(chunk_root, path);
1888 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1889 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1898 if (found_key.offset == 0)
1900 key.offset = found_key.offset - 1;
1903 if (failed && !retried) {
1907 } else if (failed && retried) {
1912 btrfs_free_path(path);
1916 static u64 div_factor(u64 num, int factor)
1925 int btrfs_balance(struct btrfs_root *dev_root)
1928 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1929 struct btrfs_device *device;
1932 struct btrfs_path *path;
1933 struct btrfs_key key;
1934 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1935 struct btrfs_trans_handle *trans;
1936 struct btrfs_key found_key;
1938 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1941 mutex_lock(&dev_root->fs_info->volume_mutex);
1942 dev_root = dev_root->fs_info->dev_root;
1944 /* step one make some room on all the devices */
1945 list_for_each_entry(device, devices, dev_list) {
1946 old_size = device->total_bytes;
1947 size_to_free = div_factor(old_size, 1);
1948 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1949 if (!device->writeable ||
1950 device->total_bytes - device->bytes_used > size_to_free)
1953 ret = btrfs_shrink_device(device, old_size - size_to_free);
1958 trans = btrfs_start_transaction(dev_root, 0);
1961 ret = btrfs_grow_device(trans, device, old_size);
1964 btrfs_end_transaction(trans, dev_root);
1967 /* step two, relocate all the chunks */
1968 path = btrfs_alloc_path();
1971 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1972 key.offset = (u64)-1;
1973 key.type = BTRFS_CHUNK_ITEM_KEY;
1976 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1981 * this shouldn't happen, it means the last relocate
1987 ret = btrfs_previous_item(chunk_root, path, 0,
1988 BTRFS_CHUNK_ITEM_KEY);
1992 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1994 if (found_key.objectid != key.objectid)
1997 /* chunk zero is special */
1998 if (found_key.offset == 0)
2001 btrfs_release_path(chunk_root, path);
2002 ret = btrfs_relocate_chunk(chunk_root,
2003 chunk_root->root_key.objectid,
2006 BUG_ON(ret && ret != -ENOSPC);
2007 key.offset = found_key.offset - 1;
2011 btrfs_free_path(path);
2012 mutex_unlock(&dev_root->fs_info->volume_mutex);
2017 * shrinking a device means finding all of the device extents past
2018 * the new size, and then following the back refs to the chunks.
2019 * The chunk relocation code actually frees the device extent
2021 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2023 struct btrfs_trans_handle *trans;
2024 struct btrfs_root *root = device->dev_root;
2025 struct btrfs_dev_extent *dev_extent = NULL;
2026 struct btrfs_path *path;
2034 bool retried = false;
2035 struct extent_buffer *l;
2036 struct btrfs_key key;
2037 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2038 u64 old_total = btrfs_super_total_bytes(super_copy);
2039 u64 old_size = device->total_bytes;
2040 u64 diff = device->total_bytes - new_size;
2042 if (new_size >= device->total_bytes)
2045 path = btrfs_alloc_path();
2053 device->total_bytes = new_size;
2054 if (device->writeable)
2055 device->fs_devices->total_rw_bytes -= diff;
2056 unlock_chunks(root);
2059 key.objectid = device->devid;
2060 key.offset = (u64)-1;
2061 key.type = BTRFS_DEV_EXTENT_KEY;
2064 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2068 ret = btrfs_previous_item(root, path, 0, key.type);
2073 btrfs_release_path(root, path);
2078 slot = path->slots[0];
2079 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2081 if (key.objectid != device->devid) {
2082 btrfs_release_path(root, path);
2086 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2087 length = btrfs_dev_extent_length(l, dev_extent);
2089 if (key.offset + length <= new_size) {
2090 btrfs_release_path(root, path);
2094 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2095 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2096 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2097 btrfs_release_path(root, path);
2099 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2101 if (ret && ret != -ENOSPC)
2108 if (failed && !retried) {
2112 } else if (failed && retried) {
2116 device->total_bytes = old_size;
2117 if (device->writeable)
2118 device->fs_devices->total_rw_bytes += diff;
2119 unlock_chunks(root);
2123 /* Shrinking succeeded, else we would be at "done". */
2124 trans = btrfs_start_transaction(root, 0);
2127 device->disk_total_bytes = new_size;
2128 /* Now btrfs_update_device() will change the on-disk size. */
2129 ret = btrfs_update_device(trans, device);
2131 unlock_chunks(root);
2132 btrfs_end_transaction(trans, root);
2135 WARN_ON(diff > old_total);
2136 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2137 unlock_chunks(root);
2138 btrfs_end_transaction(trans, root);
2140 btrfs_free_path(path);
2144 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2145 struct btrfs_root *root,
2146 struct btrfs_key *key,
2147 struct btrfs_chunk *chunk, int item_size)
2149 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2150 struct btrfs_disk_key disk_key;
2154 array_size = btrfs_super_sys_array_size(super_copy);
2155 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2158 ptr = super_copy->sys_chunk_array + array_size;
2159 btrfs_cpu_key_to_disk(&disk_key, key);
2160 memcpy(ptr, &disk_key, sizeof(disk_key));
2161 ptr += sizeof(disk_key);
2162 memcpy(ptr, chunk, item_size);
2163 item_size += sizeof(disk_key);
2164 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2168 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
2169 int num_stripes, int sub_stripes)
2171 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2173 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2174 return calc_size * (num_stripes / sub_stripes);
2176 return calc_size * num_stripes;
2179 /* Used to sort the devices by max_avail(descending sort) */
2180 int btrfs_cmp_device_free_bytes(const void *dev_info1, const void *dev_info2)
2182 if (((struct btrfs_device_info *)dev_info1)->max_avail >
2183 ((struct btrfs_device_info *)dev_info2)->max_avail)
2185 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
2186 ((struct btrfs_device_info *)dev_info2)->max_avail)
2192 static int __btrfs_calc_nstripes(struct btrfs_fs_devices *fs_devices, u64 type,
2193 int *num_stripes, int *min_stripes,
2200 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2201 *num_stripes = fs_devices->rw_devices;
2204 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2208 if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2209 if (fs_devices->rw_devices < 2)
2214 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2215 *num_stripes = fs_devices->rw_devices;
2216 if (*num_stripes < 4)
2218 *num_stripes &= ~(u32)1;
2226 static u64 __btrfs_calc_stripe_size(struct btrfs_fs_devices *fs_devices,
2227 u64 proposed_size, u64 type,
2228 int num_stripes, int small_stripe)
2230 int min_stripe_size = 1 * 1024 * 1024;
2231 u64 calc_size = proposed_size;
2232 u64 max_chunk_size = calc_size;
2235 if (type & (BTRFS_BLOCK_GROUP_RAID1 |
2236 BTRFS_BLOCK_GROUP_DUP |
2237 BTRFS_BLOCK_GROUP_RAID10))
2240 if (type & BTRFS_BLOCK_GROUP_DATA) {
2241 max_chunk_size = 10 * calc_size;
2242 min_stripe_size = 64 * 1024 * 1024;
2243 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2244 max_chunk_size = 256 * 1024 * 1024;
2245 min_stripe_size = 32 * 1024 * 1024;
2246 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2247 calc_size = 8 * 1024 * 1024;
2248 max_chunk_size = calc_size * 2;
2249 min_stripe_size = 1 * 1024 * 1024;
2252 /* we don't want a chunk larger than 10% of writeable space */
2253 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2256 if (calc_size * num_stripes > max_chunk_size * ncopies) {
2257 calc_size = max_chunk_size * ncopies;
2258 do_div(calc_size, num_stripes);
2259 do_div(calc_size, BTRFS_STRIPE_LEN);
2260 calc_size *= BTRFS_STRIPE_LEN;
2263 /* we don't want tiny stripes */
2265 calc_size = max_t(u64, min_stripe_size, calc_size);
2268 * we're about to do_div by the BTRFS_STRIPE_LEN so lets make sure
2269 * we end up with something bigger than a stripe
2271 calc_size = max_t(u64, calc_size, BTRFS_STRIPE_LEN);
2273 do_div(calc_size, BTRFS_STRIPE_LEN);
2274 calc_size *= BTRFS_STRIPE_LEN;
2279 static struct map_lookup *__shrink_map_lookup_stripes(struct map_lookup *map,
2282 struct map_lookup *new;
2283 size_t len = map_lookup_size(num_stripes);
2285 BUG_ON(map->num_stripes < num_stripes);
2287 if (map->num_stripes == num_stripes)
2290 new = kmalloc(len, GFP_NOFS);
2292 /* just change map->num_stripes */
2293 map->num_stripes = num_stripes;
2297 memcpy(new, map, len);
2298 new->num_stripes = num_stripes;
2304 * helper to allocate device space from btrfs_device_info, in which we stored
2305 * max free space information of every device. It is used when we can not
2306 * allocate chunks by default size.
2308 * By this helper, we can allocate a new chunk as larger as possible.
2310 static int __btrfs_alloc_tiny_space(struct btrfs_trans_handle *trans,
2311 struct btrfs_fs_devices *fs_devices,
2312 struct btrfs_device_info *devices,
2313 int nr_device, u64 type,
2314 struct map_lookup **map_lookup,
2315 int min_stripes, u64 *stripe_size)
2317 int i, index, sort_again = 0;
2318 int min_devices = min_stripes;
2319 u64 max_avail, min_free;
2320 struct map_lookup *map = *map_lookup;
2323 if (nr_device < min_stripes)
2326 btrfs_descending_sort_devices(devices, nr_device);
2328 max_avail = devices[0].max_avail;
2332 for (i = 0; i < nr_device; i++) {
2334 * if dev_offset = 0, it means the free space of this device
2335 * is less than what we need, and we didn't search max avail
2336 * extent on this device, so do it now.
2338 if (!devices[i].dev_offset) {
2339 ret = find_free_dev_extent(trans, devices[i].dev,
2341 &devices[i].dev_offset,
2342 &devices[i].max_avail);
2343 if (ret != 0 && ret != -ENOSPC)
2349 /* we update the max avail free extent of each devices, sort again */
2351 btrfs_descending_sort_devices(devices, nr_device);
2353 if (type & BTRFS_BLOCK_GROUP_DUP)
2356 if (!devices[min_devices - 1].max_avail)
2359 max_avail = devices[min_devices - 1].max_avail;
2360 if (type & BTRFS_BLOCK_GROUP_DUP)
2361 do_div(max_avail, 2);
2363 max_avail = __btrfs_calc_stripe_size(fs_devices, max_avail, type,
2365 if (type & BTRFS_BLOCK_GROUP_DUP)
2366 min_free = max_avail * 2;
2368 min_free = max_avail;
2370 if (min_free > devices[min_devices - 1].max_avail)
2373 map = __shrink_map_lookup_stripes(map, min_stripes);
2374 *stripe_size = max_avail;
2377 for (i = 0; i < min_stripes; i++) {
2378 map->stripes[i].dev = devices[index].dev;
2379 map->stripes[i].physical = devices[index].dev_offset;
2380 if (type & BTRFS_BLOCK_GROUP_DUP) {
2382 map->stripes[i].dev = devices[index].dev;
2383 map->stripes[i].physical = devices[index].dev_offset +
2393 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2394 struct btrfs_root *extent_root,
2395 struct map_lookup **map_ret,
2396 u64 *num_bytes, u64 *stripe_size,
2397 u64 start, u64 type)
2399 struct btrfs_fs_info *info = extent_root->fs_info;
2400 struct btrfs_device *device = NULL;
2401 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2402 struct list_head *cur;
2403 struct map_lookup *map;
2404 struct extent_map_tree *em_tree;
2405 struct extent_map *em;
2406 struct btrfs_device_info *devices_info;
2407 struct list_head private_devs;
2408 u64 calc_size = 1024 * 1024 * 1024;
2415 int min_devices; /* the min number of devices we need */
2420 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2421 (type & BTRFS_BLOCK_GROUP_DUP)) {
2423 type &= ~BTRFS_BLOCK_GROUP_DUP;
2425 if (list_empty(&fs_devices->alloc_list))
2428 ret = __btrfs_calc_nstripes(fs_devices, type, &num_stripes,
2429 &min_stripes, &sub_stripes);
2433 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2438 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2443 map->num_stripes = num_stripes;
2445 cur = fs_devices->alloc_list.next;
2449 calc_size = __btrfs_calc_stripe_size(fs_devices, calc_size, type,
2452 if (type & BTRFS_BLOCK_GROUP_DUP) {
2453 min_free = calc_size * 2;
2456 min_free = calc_size;
2457 min_devices = min_stripes;
2460 INIT_LIST_HEAD(&private_devs);
2461 while (index < num_stripes) {
2462 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2463 BUG_ON(!device->writeable);
2464 if (device->total_bytes > device->bytes_used)
2465 avail = device->total_bytes - device->bytes_used;
2470 if (device->in_fs_metadata && avail >= min_free) {
2471 ret = find_free_dev_extent(trans, device, min_free,
2472 &devices_info[i].dev_offset,
2473 &devices_info[i].max_avail);
2475 list_move_tail(&device->dev_alloc_list,
2477 map->stripes[index].dev = device;
2478 map->stripes[index].physical =
2479 devices_info[i].dev_offset;
2481 if (type & BTRFS_BLOCK_GROUP_DUP) {
2482 map->stripes[index].dev = device;
2483 map->stripes[index].physical =
2484 devices_info[i].dev_offset +
2488 } else if (ret != -ENOSPC)
2491 devices_info[i].dev = device;
2493 } else if (device->in_fs_metadata &&
2494 avail >= BTRFS_STRIPE_LEN) {
2495 devices_info[i].dev = device;
2496 devices_info[i].max_avail = avail;
2500 if (cur == &fs_devices->alloc_list)
2504 list_splice(&private_devs, &fs_devices->alloc_list);
2505 if (index < num_stripes) {
2506 if (index >= min_stripes) {
2507 num_stripes = index;
2508 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2509 num_stripes /= sub_stripes;
2510 num_stripes *= sub_stripes;
2513 map = __shrink_map_lookup_stripes(map, num_stripes);
2514 } else if (i >= min_devices) {
2515 ret = __btrfs_alloc_tiny_space(trans, fs_devices,
2516 devices_info, i, type,
2526 map->sector_size = extent_root->sectorsize;
2527 map->stripe_len = BTRFS_STRIPE_LEN;
2528 map->io_align = BTRFS_STRIPE_LEN;
2529 map->io_width = BTRFS_STRIPE_LEN;
2531 map->sub_stripes = sub_stripes;
2534 *stripe_size = calc_size;
2535 *num_bytes = chunk_bytes_by_type(type, calc_size,
2536 map->num_stripes, sub_stripes);
2538 em = alloc_extent_map(GFP_NOFS);
2543 em->bdev = (struct block_device *)map;
2545 em->len = *num_bytes;
2546 em->block_start = 0;
2547 em->block_len = em->len;
2549 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2550 write_lock(&em_tree->lock);
2551 ret = add_extent_mapping(em_tree, em);
2552 write_unlock(&em_tree->lock);
2554 free_extent_map(em);
2556 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2557 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2562 while (index < map->num_stripes) {
2563 device = map->stripes[index].dev;
2564 dev_offset = map->stripes[index].physical;
2566 ret = btrfs_alloc_dev_extent(trans, device,
2567 info->chunk_root->root_key.objectid,
2568 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2569 start, dev_offset, calc_size);
2574 kfree(devices_info);
2579 kfree(devices_info);
2583 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2584 struct btrfs_root *extent_root,
2585 struct map_lookup *map, u64 chunk_offset,
2586 u64 chunk_size, u64 stripe_size)
2589 struct btrfs_key key;
2590 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2591 struct btrfs_device *device;
2592 struct btrfs_chunk *chunk;
2593 struct btrfs_stripe *stripe;
2594 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2598 chunk = kzalloc(item_size, GFP_NOFS);
2603 while (index < map->num_stripes) {
2604 device = map->stripes[index].dev;
2605 device->bytes_used += stripe_size;
2606 ret = btrfs_update_device(trans, device);
2612 stripe = &chunk->stripe;
2613 while (index < map->num_stripes) {
2614 device = map->stripes[index].dev;
2615 dev_offset = map->stripes[index].physical;
2617 btrfs_set_stack_stripe_devid(stripe, device->devid);
2618 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2619 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2624 btrfs_set_stack_chunk_length(chunk, chunk_size);
2625 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2626 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2627 btrfs_set_stack_chunk_type(chunk, map->type);
2628 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2629 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2630 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2631 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2632 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2634 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2635 key.type = BTRFS_CHUNK_ITEM_KEY;
2636 key.offset = chunk_offset;
2638 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2641 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2642 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2651 * Chunk allocation falls into two parts. The first part does works
2652 * that make the new allocated chunk useable, but not do any operation
2653 * that modifies the chunk tree. The second part does the works that
2654 * require modifying the chunk tree. This division is important for the
2655 * bootstrap process of adding storage to a seed btrfs.
2657 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2658 struct btrfs_root *extent_root, u64 type)
2663 struct map_lookup *map;
2664 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2667 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2672 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2673 &stripe_size, chunk_offset, type);
2677 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2678 chunk_size, stripe_size);
2683 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2684 struct btrfs_root *root,
2685 struct btrfs_device *device)
2688 u64 sys_chunk_offset;
2692 u64 sys_stripe_size;
2694 struct map_lookup *map;
2695 struct map_lookup *sys_map;
2696 struct btrfs_fs_info *fs_info = root->fs_info;
2697 struct btrfs_root *extent_root = fs_info->extent_root;
2700 ret = find_next_chunk(fs_info->chunk_root,
2701 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2704 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2705 (fs_info->metadata_alloc_profile &
2706 fs_info->avail_metadata_alloc_bits);
2707 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2709 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2710 &stripe_size, chunk_offset, alloc_profile);
2713 sys_chunk_offset = chunk_offset + chunk_size;
2715 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2716 (fs_info->system_alloc_profile &
2717 fs_info->avail_system_alloc_bits);
2718 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2720 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2721 &sys_chunk_size, &sys_stripe_size,
2722 sys_chunk_offset, alloc_profile);
2725 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2729 * Modifying chunk tree needs allocating new blocks from both
2730 * system block group and metadata block group. So we only can
2731 * do operations require modifying the chunk tree after both
2732 * block groups were created.
2734 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2735 chunk_size, stripe_size);
2738 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2739 sys_chunk_offset, sys_chunk_size,
2745 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2747 struct extent_map *em;
2748 struct map_lookup *map;
2749 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2753 read_lock(&map_tree->map_tree.lock);
2754 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2755 read_unlock(&map_tree->map_tree.lock);
2759 if (btrfs_test_opt(root, DEGRADED)) {
2760 free_extent_map(em);
2764 map = (struct map_lookup *)em->bdev;
2765 for (i = 0; i < map->num_stripes; i++) {
2766 if (!map->stripes[i].dev->writeable) {
2771 free_extent_map(em);
2775 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2777 extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2780 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2782 struct extent_map *em;
2785 write_lock(&tree->map_tree.lock);
2786 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2788 remove_extent_mapping(&tree->map_tree, em);
2789 write_unlock(&tree->map_tree.lock);
2794 free_extent_map(em);
2795 /* once for the tree */
2796 free_extent_map(em);
2800 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2802 struct extent_map *em;
2803 struct map_lookup *map;
2804 struct extent_map_tree *em_tree = &map_tree->map_tree;
2807 read_lock(&em_tree->lock);
2808 em = lookup_extent_mapping(em_tree, logical, len);
2809 read_unlock(&em_tree->lock);
2812 BUG_ON(em->start > logical || em->start + em->len < logical);
2813 map = (struct map_lookup *)em->bdev;
2814 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2815 ret = map->num_stripes;
2816 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2817 ret = map->sub_stripes;
2820 free_extent_map(em);
2824 static int find_live_mirror(struct map_lookup *map, int first, int num,
2828 if (map->stripes[optimal].dev->bdev)
2830 for (i = first; i < first + num; i++) {
2831 if (map->stripes[i].dev->bdev)
2834 /* we couldn't find one that doesn't fail. Just return something
2835 * and the io error handling code will clean up eventually
2840 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2841 u64 logical, u64 *length,
2842 struct btrfs_multi_bio **multi_ret,
2843 int mirror_num, struct page *unplug_page)
2845 struct extent_map *em;
2846 struct map_lookup *map;
2847 struct extent_map_tree *em_tree = &map_tree->map_tree;
2851 int stripes_allocated = 8;
2852 int stripes_required = 1;
2857 struct btrfs_multi_bio *multi = NULL;
2859 if (multi_ret && !(rw & REQ_WRITE))
2860 stripes_allocated = 1;
2863 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2868 atomic_set(&multi->error, 0);
2871 read_lock(&em_tree->lock);
2872 em = lookup_extent_mapping(em_tree, logical, *length);
2873 read_unlock(&em_tree->lock);
2875 if (!em && unplug_page) {
2881 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2882 (unsigned long long)logical,
2883 (unsigned long long)*length);
2887 BUG_ON(em->start > logical || em->start + em->len < logical);
2888 map = (struct map_lookup *)em->bdev;
2889 offset = logical - em->start;
2891 if (mirror_num > map->num_stripes)
2894 /* if our multi bio struct is too small, back off and try again */
2895 if (rw & REQ_WRITE) {
2896 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2897 BTRFS_BLOCK_GROUP_DUP)) {
2898 stripes_required = map->num_stripes;
2900 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2901 stripes_required = map->sub_stripes;
2905 if (multi_ret && (rw & REQ_WRITE) &&
2906 stripes_allocated < stripes_required) {
2907 stripes_allocated = map->num_stripes;
2908 free_extent_map(em);
2914 * stripe_nr counts the total number of stripes we have to stride
2915 * to get to this block
2917 do_div(stripe_nr, map->stripe_len);
2919 stripe_offset = stripe_nr * map->stripe_len;
2920 BUG_ON(offset < stripe_offset);
2922 /* stripe_offset is the offset of this block in its stripe*/
2923 stripe_offset = offset - stripe_offset;
2925 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2926 BTRFS_BLOCK_GROUP_RAID10 |
2927 BTRFS_BLOCK_GROUP_DUP)) {
2928 /* we limit the length of each bio to what fits in a stripe */
2929 *length = min_t(u64, em->len - offset,
2930 map->stripe_len - stripe_offset);
2932 *length = em->len - offset;
2935 if (!multi_ret && !unplug_page)
2940 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2941 if (unplug_page || (rw & REQ_WRITE))
2942 num_stripes = map->num_stripes;
2943 else if (mirror_num)
2944 stripe_index = mirror_num - 1;
2946 stripe_index = find_live_mirror(map, 0,
2948 current->pid % map->num_stripes);
2951 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2953 num_stripes = map->num_stripes;
2954 else if (mirror_num)
2955 stripe_index = mirror_num - 1;
2957 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2958 int factor = map->num_stripes / map->sub_stripes;
2960 stripe_index = do_div(stripe_nr, factor);
2961 stripe_index *= map->sub_stripes;
2963 if (unplug_page || (rw & REQ_WRITE))
2964 num_stripes = map->sub_stripes;
2965 else if (mirror_num)
2966 stripe_index += mirror_num - 1;
2968 stripe_index = find_live_mirror(map, stripe_index,
2969 map->sub_stripes, stripe_index +
2970 current->pid % map->sub_stripes);
2974 * after this do_div call, stripe_nr is the number of stripes
2975 * on this device we have to walk to find the data, and
2976 * stripe_index is the number of our device in the stripe array
2978 stripe_index = do_div(stripe_nr, map->num_stripes);
2980 BUG_ON(stripe_index >= map->num_stripes);
2982 for (i = 0; i < num_stripes; i++) {
2984 struct btrfs_device *device;
2985 struct backing_dev_info *bdi;
2987 device = map->stripes[stripe_index].dev;
2989 bdi = blk_get_backing_dev_info(device->bdev);
2990 if (bdi->unplug_io_fn)
2991 bdi->unplug_io_fn(bdi, unplug_page);
2994 multi->stripes[i].physical =
2995 map->stripes[stripe_index].physical +
2996 stripe_offset + stripe_nr * map->stripe_len;
2997 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3003 multi->num_stripes = num_stripes;
3004 multi->max_errors = max_errors;
3007 free_extent_map(em);
3011 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3012 u64 logical, u64 *length,
3013 struct btrfs_multi_bio **multi_ret, int mirror_num)
3015 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3019 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3020 u64 chunk_start, u64 physical, u64 devid,
3021 u64 **logical, int *naddrs, int *stripe_len)
3023 struct extent_map_tree *em_tree = &map_tree->map_tree;
3024 struct extent_map *em;
3025 struct map_lookup *map;
3032 read_lock(&em_tree->lock);
3033 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3034 read_unlock(&em_tree->lock);
3036 BUG_ON(!em || em->start != chunk_start);
3037 map = (struct map_lookup *)em->bdev;
3040 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3041 do_div(length, map->num_stripes / map->sub_stripes);
3042 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3043 do_div(length, map->num_stripes);
3045 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3048 for (i = 0; i < map->num_stripes; i++) {
3049 if (devid && map->stripes[i].dev->devid != devid)
3051 if (map->stripes[i].physical > physical ||
3052 map->stripes[i].physical + length <= physical)
3055 stripe_nr = physical - map->stripes[i].physical;
3056 do_div(stripe_nr, map->stripe_len);
3058 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3059 stripe_nr = stripe_nr * map->num_stripes + i;
3060 do_div(stripe_nr, map->sub_stripes);
3061 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3062 stripe_nr = stripe_nr * map->num_stripes + i;
3064 bytenr = chunk_start + stripe_nr * map->stripe_len;
3065 WARN_ON(nr >= map->num_stripes);
3066 for (j = 0; j < nr; j++) {
3067 if (buf[j] == bytenr)
3071 WARN_ON(nr >= map->num_stripes);
3078 *stripe_len = map->stripe_len;
3080 free_extent_map(em);
3084 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
3085 u64 logical, struct page *page)
3087 u64 length = PAGE_CACHE_SIZE;
3088 return __btrfs_map_block(map_tree, READ, logical, &length,
3092 static void end_bio_multi_stripe(struct bio *bio, int err)
3094 struct btrfs_multi_bio *multi = bio->bi_private;
3095 int is_orig_bio = 0;
3098 atomic_inc(&multi->error);
3100 if (bio == multi->orig_bio)
3103 if (atomic_dec_and_test(&multi->stripes_pending)) {
3106 bio = multi->orig_bio;
3108 bio->bi_private = multi->private;
3109 bio->bi_end_io = multi->end_io;
3110 /* only send an error to the higher layers if it is
3111 * beyond the tolerance of the multi-bio
3113 if (atomic_read(&multi->error) > multi->max_errors) {
3117 * this bio is actually up to date, we didn't
3118 * go over the max number of errors
3120 set_bit(BIO_UPTODATE, &bio->bi_flags);
3125 bio_endio(bio, err);
3126 } else if (!is_orig_bio) {
3131 struct async_sched {
3134 struct btrfs_fs_info *info;
3135 struct btrfs_work work;
3139 * see run_scheduled_bios for a description of why bios are collected for
3142 * This will add one bio to the pending list for a device and make sure
3143 * the work struct is scheduled.
3145 static noinline int schedule_bio(struct btrfs_root *root,
3146 struct btrfs_device *device,
3147 int rw, struct bio *bio)
3149 int should_queue = 1;
3150 struct btrfs_pending_bios *pending_bios;
3152 /* don't bother with additional async steps for reads, right now */
3153 if (!(rw & REQ_WRITE)) {
3155 submit_bio(rw, bio);
3161 * nr_async_bios allows us to reliably return congestion to the
3162 * higher layers. Otherwise, the async bio makes it appear we have
3163 * made progress against dirty pages when we've really just put it
3164 * on a queue for later
3166 atomic_inc(&root->fs_info->nr_async_bios);
3167 WARN_ON(bio->bi_next);
3168 bio->bi_next = NULL;
3171 spin_lock(&device->io_lock);
3172 if (bio->bi_rw & REQ_SYNC)
3173 pending_bios = &device->pending_sync_bios;
3175 pending_bios = &device->pending_bios;
3177 if (pending_bios->tail)
3178 pending_bios->tail->bi_next = bio;
3180 pending_bios->tail = bio;
3181 if (!pending_bios->head)
3182 pending_bios->head = bio;
3183 if (device->running_pending)
3186 spin_unlock(&device->io_lock);
3189 btrfs_queue_worker(&root->fs_info->submit_workers,
3194 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3195 int mirror_num, int async_submit)
3197 struct btrfs_mapping_tree *map_tree;
3198 struct btrfs_device *dev;
3199 struct bio *first_bio = bio;
3200 u64 logical = (u64)bio->bi_sector << 9;
3203 struct btrfs_multi_bio *multi = NULL;
3208 length = bio->bi_size;
3209 map_tree = &root->fs_info->mapping_tree;
3210 map_length = length;
3212 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3216 total_devs = multi->num_stripes;
3217 if (map_length < length) {
3218 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3219 "len %llu\n", (unsigned long long)logical,
3220 (unsigned long long)length,
3221 (unsigned long long)map_length);
3224 multi->end_io = first_bio->bi_end_io;
3225 multi->private = first_bio->bi_private;
3226 multi->orig_bio = first_bio;
3227 atomic_set(&multi->stripes_pending, multi->num_stripes);
3229 while (dev_nr < total_devs) {
3230 if (total_devs > 1) {
3231 if (dev_nr < total_devs - 1) {
3232 bio = bio_clone(first_bio, GFP_NOFS);
3237 bio->bi_private = multi;
3238 bio->bi_end_io = end_bio_multi_stripe;
3240 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3241 dev = multi->stripes[dev_nr].dev;
3242 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3243 bio->bi_bdev = dev->bdev;
3245 schedule_bio(root, dev, rw, bio);
3247 submit_bio(rw, bio);
3249 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3250 bio->bi_sector = logical >> 9;
3251 bio_endio(bio, -EIO);
3255 if (total_devs == 1)
3260 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3263 struct btrfs_device *device;
3264 struct btrfs_fs_devices *cur_devices;
3266 cur_devices = root->fs_info->fs_devices;
3267 while (cur_devices) {
3269 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3270 device = __find_device(&cur_devices->devices,
3275 cur_devices = cur_devices->seed;
3280 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3281 u64 devid, u8 *dev_uuid)
3283 struct btrfs_device *device;
3284 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3286 device = kzalloc(sizeof(*device), GFP_NOFS);
3289 list_add(&device->dev_list,
3290 &fs_devices->devices);
3291 device->barriers = 1;
3292 device->dev_root = root->fs_info->dev_root;
3293 device->devid = devid;
3294 device->work.func = pending_bios_fn;
3295 device->fs_devices = fs_devices;
3296 device->missing = 1;
3297 fs_devices->num_devices++;
3298 fs_devices->missing_devices++;
3299 spin_lock_init(&device->io_lock);
3300 INIT_LIST_HEAD(&device->dev_alloc_list);
3301 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3305 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3306 struct extent_buffer *leaf,
3307 struct btrfs_chunk *chunk)
3309 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3310 struct map_lookup *map;
3311 struct extent_map *em;
3315 u8 uuid[BTRFS_UUID_SIZE];
3320 logical = key->offset;
3321 length = btrfs_chunk_length(leaf, chunk);
3323 read_lock(&map_tree->map_tree.lock);
3324 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3325 read_unlock(&map_tree->map_tree.lock);
3327 /* already mapped? */
3328 if (em && em->start <= logical && em->start + em->len > logical) {
3329 free_extent_map(em);
3332 free_extent_map(em);
3335 em = alloc_extent_map(GFP_NOFS);
3338 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3339 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3341 free_extent_map(em);
3345 em->bdev = (struct block_device *)map;
3346 em->start = logical;
3348 em->block_start = 0;
3349 em->block_len = em->len;
3351 map->num_stripes = num_stripes;
3352 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3353 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3354 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3355 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3356 map->type = btrfs_chunk_type(leaf, chunk);
3357 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3358 for (i = 0; i < num_stripes; i++) {
3359 map->stripes[i].physical =
3360 btrfs_stripe_offset_nr(leaf, chunk, i);
3361 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3362 read_extent_buffer(leaf, uuid, (unsigned long)
3363 btrfs_stripe_dev_uuid_nr(chunk, i),
3365 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3367 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3369 free_extent_map(em);
3372 if (!map->stripes[i].dev) {
3373 map->stripes[i].dev =
3374 add_missing_dev(root, devid, uuid);
3375 if (!map->stripes[i].dev) {
3377 free_extent_map(em);
3381 map->stripes[i].dev->in_fs_metadata = 1;
3384 write_lock(&map_tree->map_tree.lock);
3385 ret = add_extent_mapping(&map_tree->map_tree, em);
3386 write_unlock(&map_tree->map_tree.lock);
3388 free_extent_map(em);
3393 static int fill_device_from_item(struct extent_buffer *leaf,
3394 struct btrfs_dev_item *dev_item,
3395 struct btrfs_device *device)
3399 device->devid = btrfs_device_id(leaf, dev_item);
3400 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3401 device->total_bytes = device->disk_total_bytes;
3402 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3403 device->type = btrfs_device_type(leaf, dev_item);
3404 device->io_align = btrfs_device_io_align(leaf, dev_item);
3405 device->io_width = btrfs_device_io_width(leaf, dev_item);
3406 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3408 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3409 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3414 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3416 struct btrfs_fs_devices *fs_devices;
3419 mutex_lock(&uuid_mutex);
3421 fs_devices = root->fs_info->fs_devices->seed;
3422 while (fs_devices) {
3423 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3427 fs_devices = fs_devices->seed;
3430 fs_devices = find_fsid(fsid);
3436 fs_devices = clone_fs_devices(fs_devices);
3437 if (IS_ERR(fs_devices)) {
3438 ret = PTR_ERR(fs_devices);
3442 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3443 root->fs_info->bdev_holder);
3447 if (!fs_devices->seeding) {
3448 __btrfs_close_devices(fs_devices);
3449 free_fs_devices(fs_devices);
3454 fs_devices->seed = root->fs_info->fs_devices->seed;
3455 root->fs_info->fs_devices->seed = fs_devices;
3457 mutex_unlock(&uuid_mutex);
3461 static int read_one_dev(struct btrfs_root *root,
3462 struct extent_buffer *leaf,
3463 struct btrfs_dev_item *dev_item)
3465 struct btrfs_device *device;
3468 u8 fs_uuid[BTRFS_UUID_SIZE];
3469 u8 dev_uuid[BTRFS_UUID_SIZE];
3471 devid = btrfs_device_id(leaf, dev_item);
3472 read_extent_buffer(leaf, dev_uuid,
3473 (unsigned long)btrfs_device_uuid(dev_item),
3475 read_extent_buffer(leaf, fs_uuid,
3476 (unsigned long)btrfs_device_fsid(dev_item),
3479 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3480 ret = open_seed_devices(root, fs_uuid);
3481 if (ret && !btrfs_test_opt(root, DEGRADED))
3485 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3486 if (!device || !device->bdev) {
3487 if (!btrfs_test_opt(root, DEGRADED))
3491 printk(KERN_WARNING "warning devid %llu missing\n",
3492 (unsigned long long)devid);
3493 device = add_missing_dev(root, devid, dev_uuid);
3496 } else if (!device->missing) {
3498 * this happens when a device that was properly setup
3499 * in the device info lists suddenly goes bad.
3500 * device->bdev is NULL, and so we have to set
3501 * device->missing to one here
3503 root->fs_info->fs_devices->missing_devices++;
3504 device->missing = 1;
3508 if (device->fs_devices != root->fs_info->fs_devices) {
3509 BUG_ON(device->writeable);
3510 if (device->generation !=
3511 btrfs_device_generation(leaf, dev_item))
3515 fill_device_from_item(leaf, dev_item, device);
3516 device->dev_root = root->fs_info->dev_root;
3517 device->in_fs_metadata = 1;
3518 if (device->writeable)
3519 device->fs_devices->total_rw_bytes += device->total_bytes;
3524 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3526 struct btrfs_dev_item *dev_item;
3528 dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3530 return read_one_dev(root, buf, dev_item);
3533 int btrfs_read_sys_array(struct btrfs_root *root)
3535 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3536 struct extent_buffer *sb;
3537 struct btrfs_disk_key *disk_key;
3538 struct btrfs_chunk *chunk;
3540 unsigned long sb_ptr;
3546 struct btrfs_key key;
3548 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3549 BTRFS_SUPER_INFO_SIZE);
3552 btrfs_set_buffer_uptodate(sb);
3553 btrfs_set_buffer_lockdep_class(sb, 0);
3555 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3556 array_size = btrfs_super_sys_array_size(super_copy);
3558 ptr = super_copy->sys_chunk_array;
3559 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3562 while (cur < array_size) {
3563 disk_key = (struct btrfs_disk_key *)ptr;
3564 btrfs_disk_key_to_cpu(&key, disk_key);
3566 len = sizeof(*disk_key); ptr += len;
3570 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3571 chunk = (struct btrfs_chunk *)sb_ptr;
3572 ret = read_one_chunk(root, &key, sb, chunk);
3575 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3576 len = btrfs_chunk_item_size(num_stripes);
3585 free_extent_buffer(sb);
3589 int btrfs_read_chunk_tree(struct btrfs_root *root)
3591 struct btrfs_path *path;
3592 struct extent_buffer *leaf;
3593 struct btrfs_key key;
3594 struct btrfs_key found_key;
3598 root = root->fs_info->chunk_root;
3600 path = btrfs_alloc_path();
3604 /* first we search for all of the device items, and then we
3605 * read in all of the chunk items. This way we can create chunk
3606 * mappings that reference all of the devices that are afound
3608 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3612 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3616 leaf = path->nodes[0];
3617 slot = path->slots[0];
3618 if (slot >= btrfs_header_nritems(leaf)) {
3619 ret = btrfs_next_leaf(root, path);
3626 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3627 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3628 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3630 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3631 struct btrfs_dev_item *dev_item;
3632 dev_item = btrfs_item_ptr(leaf, slot,
3633 struct btrfs_dev_item);
3634 ret = read_one_dev(root, leaf, dev_item);
3638 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3639 struct btrfs_chunk *chunk;
3640 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3641 ret = read_one_chunk(root, &found_key, leaf, chunk);
3647 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3649 btrfs_release_path(root, path);
3654 btrfs_free_path(path);