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 <linux/capability.h>
26 #include <asm/div64.h>
29 #include "extent_map.h"
31 #include "transaction.h"
32 #include "print-tree.h"
34 #include "async-thread.h"
36 static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
41 static DEFINE_MUTEX(uuid_mutex);
42 static LIST_HEAD(fs_uuids);
44 static void lock_chunks(struct btrfs_root *root)
46 mutex_lock(&root->fs_info->chunk_mutex);
49 static void unlock_chunks(struct btrfs_root *root)
51 mutex_unlock(&root->fs_info->chunk_mutex);
54 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
56 struct btrfs_device *device;
57 WARN_ON(fs_devices->opened);
58 while (!list_empty(&fs_devices->devices)) {
59 device = list_entry(fs_devices->devices.next,
60 struct btrfs_device, dev_list);
61 list_del(&device->dev_list);
68 int btrfs_cleanup_fs_uuids(void)
70 struct btrfs_fs_devices *fs_devices;
72 while (!list_empty(&fs_uuids)) {
73 fs_devices = list_entry(fs_uuids.next,
74 struct btrfs_fs_devices, list);
75 list_del(&fs_devices->list);
76 free_fs_devices(fs_devices);
81 static noinline struct btrfs_device *__find_device(struct list_head *head,
84 struct btrfs_device *dev;
86 list_for_each_entry(dev, head, dev_list) {
87 if (dev->devid == devid &&
88 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
95 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
97 struct btrfs_fs_devices *fs_devices;
99 list_for_each_entry(fs_devices, &fs_uuids, list) {
100 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
106 static void requeue_list(struct btrfs_pending_bios *pending_bios,
107 struct bio *head, struct bio *tail)
110 struct bio *old_head;
112 old_head = pending_bios->head;
113 pending_bios->head = head;
114 if (pending_bios->tail)
115 tail->bi_next = old_head;
117 pending_bios->tail = tail;
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
131 static noinline int run_scheduled_bios(struct btrfs_device *device)
134 struct backing_dev_info *bdi;
135 struct btrfs_fs_info *fs_info;
136 struct btrfs_pending_bios *pending_bios;
140 unsigned long num_run;
141 unsigned long batch_run = 0;
143 unsigned long last_waited = 0;
146 struct blk_plug plug;
149 * this function runs all the bios we've collected for
150 * a particular device. We don't want to wander off to
151 * another device without first sending all of these down.
152 * So, setup a plug here and finish it off before we return
154 blk_start_plug(&plug);
156 bdi = blk_get_backing_dev_info(device->bdev);
157 fs_info = device->dev_root->fs_info;
158 limit = btrfs_async_submit_limit(fs_info);
159 limit = limit * 2 / 3;
162 spin_lock(&device->io_lock);
167 /* take all the bios off the list at once and process them
168 * later on (without the lock held). But, remember the
169 * tail and other pointers so the bios can be properly reinserted
170 * into the list if we hit congestion
172 if (!force_reg && device->pending_sync_bios.head) {
173 pending_bios = &device->pending_sync_bios;
176 pending_bios = &device->pending_bios;
180 pending = pending_bios->head;
181 tail = pending_bios->tail;
182 WARN_ON(pending && !tail);
185 * if pending was null this time around, no bios need processing
186 * at all and we can stop. Otherwise it'll loop back up again
187 * and do an additional check so no bios are missed.
189 * device->running_pending is used to synchronize with the
192 if (device->pending_sync_bios.head == NULL &&
193 device->pending_bios.head == NULL) {
195 device->running_pending = 0;
198 device->running_pending = 1;
201 pending_bios->head = NULL;
202 pending_bios->tail = NULL;
204 spin_unlock(&device->io_lock);
209 /* we want to work on both lists, but do more bios on the
210 * sync list than the regular list
213 pending_bios != &device->pending_sync_bios &&
214 device->pending_sync_bios.head) ||
215 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
216 device->pending_bios.head)) {
217 spin_lock(&device->io_lock);
218 requeue_list(pending_bios, pending, tail);
223 pending = pending->bi_next;
225 atomic_dec(&fs_info->nr_async_bios);
227 if (atomic_read(&fs_info->nr_async_bios) < limit &&
228 waitqueue_active(&fs_info->async_submit_wait))
229 wake_up(&fs_info->async_submit_wait);
231 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
241 if (pending_bios == &device->pending_sync_bios) {
243 } else if (sync_pending) {
244 blk_finish_plug(&plug);
245 blk_start_plug(&plug);
249 submit_bio(cur->bi_rw, cur);
256 * we made progress, there is more work to do and the bdi
257 * is now congested. Back off and let other work structs
260 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
261 fs_info->fs_devices->open_devices > 1) {
262 struct io_context *ioc;
264 ioc = current->io_context;
267 * the main goal here is that we don't want to
268 * block if we're going to be able to submit
269 * more requests without blocking.
271 * This code does two great things, it pokes into
272 * the elevator code from a filesystem _and_
273 * it makes assumptions about how batching works.
275 if (ioc && ioc->nr_batch_requests > 0 &&
276 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
278 ioc->last_waited == last_waited)) {
280 * we want to go through our batch of
281 * requests and stop. So, we copy out
282 * the ioc->last_waited time and test
283 * against it before looping
285 last_waited = ioc->last_waited;
290 spin_lock(&device->io_lock);
291 requeue_list(pending_bios, pending, tail);
292 device->running_pending = 1;
294 spin_unlock(&device->io_lock);
295 btrfs_requeue_work(&device->work);
304 spin_lock(&device->io_lock);
305 if (device->pending_bios.head || device->pending_sync_bios.head)
307 spin_unlock(&device->io_lock);
310 blk_finish_plug(&plug);
314 static void pending_bios_fn(struct btrfs_work *work)
316 struct btrfs_device *device;
318 device = container_of(work, struct btrfs_device, work);
319 run_scheduled_bios(device);
322 static noinline int device_list_add(const char *path,
323 struct btrfs_super_block *disk_super,
324 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
326 struct btrfs_device *device;
327 struct btrfs_fs_devices *fs_devices;
328 u64 found_transid = btrfs_super_generation(disk_super);
331 fs_devices = find_fsid(disk_super->fsid);
333 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
336 INIT_LIST_HEAD(&fs_devices->devices);
337 INIT_LIST_HEAD(&fs_devices->alloc_list);
338 list_add(&fs_devices->list, &fs_uuids);
339 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
340 fs_devices->latest_devid = devid;
341 fs_devices->latest_trans = found_transid;
342 mutex_init(&fs_devices->device_list_mutex);
345 device = __find_device(&fs_devices->devices, devid,
346 disk_super->dev_item.uuid);
349 if (fs_devices->opened)
352 device = kzalloc(sizeof(*device), GFP_NOFS);
354 /* we can safely leave the fs_devices entry around */
357 device->devid = devid;
358 device->work.func = pending_bios_fn;
359 memcpy(device->uuid, disk_super->dev_item.uuid,
361 spin_lock_init(&device->io_lock);
362 device->name = kstrdup(path, GFP_NOFS);
367 INIT_LIST_HEAD(&device->dev_alloc_list);
369 mutex_lock(&fs_devices->device_list_mutex);
370 list_add_rcu(&device->dev_list, &fs_devices->devices);
371 mutex_unlock(&fs_devices->device_list_mutex);
373 device->fs_devices = fs_devices;
374 fs_devices->num_devices++;
375 } else if (!device->name || strcmp(device->name, path)) {
376 name = kstrdup(path, GFP_NOFS);
381 if (device->missing) {
382 fs_devices->missing_devices--;
387 if (found_transid > fs_devices->latest_trans) {
388 fs_devices->latest_devid = devid;
389 fs_devices->latest_trans = found_transid;
391 *fs_devices_ret = fs_devices;
395 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
397 struct btrfs_fs_devices *fs_devices;
398 struct btrfs_device *device;
399 struct btrfs_device *orig_dev;
401 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
403 return ERR_PTR(-ENOMEM);
405 INIT_LIST_HEAD(&fs_devices->devices);
406 INIT_LIST_HEAD(&fs_devices->alloc_list);
407 INIT_LIST_HEAD(&fs_devices->list);
408 mutex_init(&fs_devices->device_list_mutex);
409 fs_devices->latest_devid = orig->latest_devid;
410 fs_devices->latest_trans = orig->latest_trans;
411 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
413 /* We have held the volume lock, it is safe to get the devices. */
414 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
415 device = kzalloc(sizeof(*device), GFP_NOFS);
419 device->name = kstrdup(orig_dev->name, GFP_NOFS);
425 device->devid = orig_dev->devid;
426 device->work.func = pending_bios_fn;
427 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
428 spin_lock_init(&device->io_lock);
429 INIT_LIST_HEAD(&device->dev_list);
430 INIT_LIST_HEAD(&device->dev_alloc_list);
432 list_add(&device->dev_list, &fs_devices->devices);
433 device->fs_devices = fs_devices;
434 fs_devices->num_devices++;
438 free_fs_devices(fs_devices);
439 return ERR_PTR(-ENOMEM);
442 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
444 struct btrfs_device *device, *next;
446 mutex_lock(&uuid_mutex);
448 /* This is the initialized path, it is safe to release the devices. */
449 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
450 if (device->in_fs_metadata)
454 blkdev_put(device->bdev, device->mode);
456 fs_devices->open_devices--;
458 if (device->writeable) {
459 list_del_init(&device->dev_alloc_list);
460 device->writeable = 0;
461 fs_devices->rw_devices--;
463 list_del_init(&device->dev_list);
464 fs_devices->num_devices--;
469 if (fs_devices->seed) {
470 fs_devices = fs_devices->seed;
474 mutex_unlock(&uuid_mutex);
478 static void __free_device(struct work_struct *work)
480 struct btrfs_device *device;
482 device = container_of(work, struct btrfs_device, rcu_work);
485 blkdev_put(device->bdev, device->mode);
491 static void free_device(struct rcu_head *head)
493 struct btrfs_device *device;
495 device = container_of(head, struct btrfs_device, rcu);
497 INIT_WORK(&device->rcu_work, __free_device);
498 schedule_work(&device->rcu_work);
501 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
503 struct btrfs_device *device;
505 if (--fs_devices->opened > 0)
508 mutex_lock(&fs_devices->device_list_mutex);
509 list_for_each_entry(device, &fs_devices->devices, dev_list) {
510 struct btrfs_device *new_device;
513 fs_devices->open_devices--;
515 if (device->writeable) {
516 list_del_init(&device->dev_alloc_list);
517 fs_devices->rw_devices--;
520 if (device->can_discard)
521 fs_devices->num_can_discard--;
523 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
525 memcpy(new_device, device, sizeof(*new_device));
526 new_device->name = kstrdup(device->name, GFP_NOFS);
527 BUG_ON(device->name && !new_device->name);
528 new_device->bdev = NULL;
529 new_device->writeable = 0;
530 new_device->in_fs_metadata = 0;
531 new_device->can_discard = 0;
532 list_replace_rcu(&device->dev_list, &new_device->dev_list);
534 call_rcu(&device->rcu, free_device);
536 mutex_unlock(&fs_devices->device_list_mutex);
538 WARN_ON(fs_devices->open_devices);
539 WARN_ON(fs_devices->rw_devices);
540 fs_devices->opened = 0;
541 fs_devices->seeding = 0;
546 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
548 struct btrfs_fs_devices *seed_devices = NULL;
551 mutex_lock(&uuid_mutex);
552 ret = __btrfs_close_devices(fs_devices);
553 if (!fs_devices->opened) {
554 seed_devices = fs_devices->seed;
555 fs_devices->seed = NULL;
557 mutex_unlock(&uuid_mutex);
559 while (seed_devices) {
560 fs_devices = seed_devices;
561 seed_devices = fs_devices->seed;
562 __btrfs_close_devices(fs_devices);
563 free_fs_devices(fs_devices);
568 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
569 fmode_t flags, void *holder)
571 struct request_queue *q;
572 struct block_device *bdev;
573 struct list_head *head = &fs_devices->devices;
574 struct btrfs_device *device;
575 struct block_device *latest_bdev = NULL;
576 struct buffer_head *bh;
577 struct btrfs_super_block *disk_super;
578 u64 latest_devid = 0;
579 u64 latest_transid = 0;
586 list_for_each_entry(device, head, dev_list) {
592 bdev = blkdev_get_by_path(device->name, flags, holder);
594 printk(KERN_INFO "open %s failed\n", device->name);
597 set_blocksize(bdev, 4096);
599 bh = btrfs_read_dev_super(bdev);
605 disk_super = (struct btrfs_super_block *)bh->b_data;
606 devid = btrfs_stack_device_id(&disk_super->dev_item);
607 if (devid != device->devid)
610 if (memcmp(device->uuid, disk_super->dev_item.uuid,
614 device->generation = btrfs_super_generation(disk_super);
615 if (!latest_transid || device->generation > latest_transid) {
616 latest_devid = devid;
617 latest_transid = device->generation;
621 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
622 device->writeable = 0;
624 device->writeable = !bdev_read_only(bdev);
628 q = bdev_get_queue(bdev);
629 if (blk_queue_discard(q)) {
630 device->can_discard = 1;
631 fs_devices->num_can_discard++;
635 device->in_fs_metadata = 0;
636 device->mode = flags;
638 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
639 fs_devices->rotating = 1;
641 fs_devices->open_devices++;
642 if (device->writeable) {
643 fs_devices->rw_devices++;
644 list_add(&device->dev_alloc_list,
645 &fs_devices->alloc_list);
653 blkdev_put(bdev, flags);
657 if (fs_devices->open_devices == 0) {
661 fs_devices->seeding = seeding;
662 fs_devices->opened = 1;
663 fs_devices->latest_bdev = latest_bdev;
664 fs_devices->latest_devid = latest_devid;
665 fs_devices->latest_trans = latest_transid;
666 fs_devices->total_rw_bytes = 0;
671 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
672 fmode_t flags, void *holder)
676 mutex_lock(&uuid_mutex);
677 if (fs_devices->opened) {
678 fs_devices->opened++;
681 ret = __btrfs_open_devices(fs_devices, flags, holder);
683 mutex_unlock(&uuid_mutex);
687 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
688 struct btrfs_fs_devices **fs_devices_ret)
690 struct btrfs_super_block *disk_super;
691 struct block_device *bdev;
692 struct buffer_head *bh;
697 mutex_lock(&uuid_mutex);
700 bdev = blkdev_get_by_path(path, flags, holder);
707 ret = set_blocksize(bdev, 4096);
710 bh = btrfs_read_dev_super(bdev);
715 disk_super = (struct btrfs_super_block *)bh->b_data;
716 devid = btrfs_stack_device_id(&disk_super->dev_item);
717 transid = btrfs_super_generation(disk_super);
718 if (disk_super->label[0])
719 printk(KERN_INFO "device label %s ", disk_super->label);
721 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
722 printk(KERN_CONT "devid %llu transid %llu %s\n",
723 (unsigned long long)devid, (unsigned long long)transid, path);
724 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
728 blkdev_put(bdev, flags);
730 mutex_unlock(&uuid_mutex);
734 /* helper to account the used device space in the range */
735 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
736 u64 end, u64 *length)
738 struct btrfs_key key;
739 struct btrfs_root *root = device->dev_root;
740 struct btrfs_dev_extent *dev_extent;
741 struct btrfs_path *path;
745 struct extent_buffer *l;
749 if (start >= device->total_bytes)
752 path = btrfs_alloc_path();
757 key.objectid = device->devid;
759 key.type = BTRFS_DEV_EXTENT_KEY;
761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
765 ret = btrfs_previous_item(root, path, key.objectid, key.type);
772 slot = path->slots[0];
773 if (slot >= btrfs_header_nritems(l)) {
774 ret = btrfs_next_leaf(root, path);
782 btrfs_item_key_to_cpu(l, &key, slot);
784 if (key.objectid < device->devid)
787 if (key.objectid > device->devid)
790 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
793 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
794 extent_end = key.offset + btrfs_dev_extent_length(l,
796 if (key.offset <= start && extent_end > end) {
797 *length = end - start + 1;
799 } else if (key.offset <= start && extent_end > start)
800 *length += extent_end - start;
801 else if (key.offset > start && extent_end <= end)
802 *length += extent_end - key.offset;
803 else if (key.offset > start && key.offset <= end) {
804 *length += end - key.offset + 1;
806 } else if (key.offset > end)
814 btrfs_free_path(path);
819 * find_free_dev_extent - find free space in the specified device
820 * @trans: transaction handler
821 * @device: the device which we search the free space in
822 * @num_bytes: the size of the free space that we need
823 * @start: store the start of the free space.
824 * @len: the size of the free space. that we find, or the size of the max
825 * free space if we don't find suitable free space
827 * this uses a pretty simple search, the expectation is that it is
828 * called very infrequently and that a given device has a small number
831 * @start is used to store the start of the free space if we find. But if we
832 * don't find suitable free space, it will be used to store the start position
833 * of the max free space.
835 * @len is used to store the size of the free space that we find.
836 * But if we don't find suitable free space, it is used to store the size of
837 * the max free space.
839 int find_free_dev_extent(struct btrfs_trans_handle *trans,
840 struct btrfs_device *device, u64 num_bytes,
841 u64 *start, u64 *len)
843 struct btrfs_key key;
844 struct btrfs_root *root = device->dev_root;
845 struct btrfs_dev_extent *dev_extent;
846 struct btrfs_path *path;
852 u64 search_end = device->total_bytes;
855 struct extent_buffer *l;
857 /* FIXME use last free of some kind */
859 /* we don't want to overwrite the superblock on the drive,
860 * so we make sure to start at an offset of at least 1MB
862 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
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);
995 leaf = path->nodes[0];
996 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
997 extent = btrfs_item_ptr(leaf, path->slots[0],
998 struct btrfs_dev_extent);
999 BUG_ON(found_key.offset > start || found_key.offset +
1000 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();
1071 key.objectid = objectid;
1072 key.offset = (u64)-1;
1073 key.type = BTRFS_CHUNK_ITEM_KEY;
1075 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1081 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1085 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1087 if (found_key.objectid != objectid)
1090 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1091 struct btrfs_chunk);
1092 *offset = found_key.offset +
1093 btrfs_chunk_length(path->nodes[0], chunk);
1098 btrfs_free_path(path);
1102 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1105 struct btrfs_key key;
1106 struct btrfs_key found_key;
1107 struct btrfs_path *path;
1109 root = root->fs_info->chunk_root;
1111 path = btrfs_alloc_path();
1115 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1116 key.type = BTRFS_DEV_ITEM_KEY;
1117 key.offset = (u64)-1;
1119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1125 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1126 BTRFS_DEV_ITEM_KEY);
1130 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1132 *objectid = found_key.offset + 1;
1136 btrfs_free_path(path);
1141 * the device information is stored in the chunk root
1142 * the btrfs_device struct should be fully filled in
1144 int btrfs_add_device(struct btrfs_trans_handle *trans,
1145 struct btrfs_root *root,
1146 struct btrfs_device *device)
1149 struct btrfs_path *path;
1150 struct btrfs_dev_item *dev_item;
1151 struct extent_buffer *leaf;
1152 struct btrfs_key key;
1155 root = root->fs_info->chunk_root;
1157 path = btrfs_alloc_path();
1161 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1162 key.type = BTRFS_DEV_ITEM_KEY;
1163 key.offset = device->devid;
1165 ret = btrfs_insert_empty_item(trans, root, path, &key,
1170 leaf = path->nodes[0];
1171 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1173 btrfs_set_device_id(leaf, dev_item, device->devid);
1174 btrfs_set_device_generation(leaf, dev_item, 0);
1175 btrfs_set_device_type(leaf, dev_item, device->type);
1176 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1177 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1178 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1179 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1180 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1181 btrfs_set_device_group(leaf, dev_item, 0);
1182 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1183 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1184 btrfs_set_device_start_offset(leaf, dev_item, 0);
1186 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1187 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1188 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1189 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1190 btrfs_mark_buffer_dirty(leaf);
1194 btrfs_free_path(path);
1198 static int btrfs_rm_dev_item(struct btrfs_root *root,
1199 struct btrfs_device *device)
1202 struct btrfs_path *path;
1203 struct btrfs_key key;
1204 struct btrfs_trans_handle *trans;
1206 root = root->fs_info->chunk_root;
1208 path = btrfs_alloc_path();
1212 trans = btrfs_start_transaction(root, 0);
1213 if (IS_ERR(trans)) {
1214 btrfs_free_path(path);
1215 return PTR_ERR(trans);
1217 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1218 key.type = BTRFS_DEV_ITEM_KEY;
1219 key.offset = device->devid;
1222 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1231 ret = btrfs_del_item(trans, root, path);
1235 btrfs_free_path(path);
1236 unlock_chunks(root);
1237 btrfs_commit_transaction(trans, root);
1241 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1243 struct btrfs_device *device;
1244 struct btrfs_device *next_device;
1245 struct block_device *bdev;
1246 struct buffer_head *bh = NULL;
1247 struct btrfs_super_block *disk_super;
1248 struct btrfs_fs_devices *cur_devices;
1254 bool clear_super = false;
1256 mutex_lock(&uuid_mutex);
1257 mutex_lock(&root->fs_info->volume_mutex);
1259 all_avail = root->fs_info->avail_data_alloc_bits |
1260 root->fs_info->avail_system_alloc_bits |
1261 root->fs_info->avail_metadata_alloc_bits;
1263 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1264 root->fs_info->fs_devices->num_devices <= 4) {
1265 printk(KERN_ERR "btrfs: unable to go below four devices "
1271 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1272 root->fs_info->fs_devices->num_devices <= 2) {
1273 printk(KERN_ERR "btrfs: unable to go below two "
1274 "devices on raid1\n");
1279 if (strcmp(device_path, "missing") == 0) {
1280 struct list_head *devices;
1281 struct btrfs_device *tmp;
1284 devices = &root->fs_info->fs_devices->devices;
1286 * It is safe to read the devices since the volume_mutex
1289 list_for_each_entry(tmp, devices, dev_list) {
1290 if (tmp->in_fs_metadata && !tmp->bdev) {
1299 printk(KERN_ERR "btrfs: no missing devices found to "
1304 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1305 root->fs_info->bdev_holder);
1307 ret = PTR_ERR(bdev);
1311 set_blocksize(bdev, 4096);
1312 bh = btrfs_read_dev_super(bdev);
1317 disk_super = (struct btrfs_super_block *)bh->b_data;
1318 devid = btrfs_stack_device_id(&disk_super->dev_item);
1319 dev_uuid = disk_super->dev_item.uuid;
1320 device = btrfs_find_device(root, devid, dev_uuid,
1328 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1329 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1335 if (device->writeable) {
1337 list_del_init(&device->dev_alloc_list);
1338 unlock_chunks(root);
1339 root->fs_info->fs_devices->rw_devices--;
1343 ret = btrfs_shrink_device(device, 0);
1347 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1351 device->in_fs_metadata = 0;
1352 btrfs_scrub_cancel_dev(root, device);
1355 * the device list mutex makes sure that we don't change
1356 * the device list while someone else is writing out all
1357 * the device supers.
1360 cur_devices = device->fs_devices;
1361 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1362 list_del_rcu(&device->dev_list);
1364 device->fs_devices->num_devices--;
1366 if (device->missing)
1367 root->fs_info->fs_devices->missing_devices--;
1369 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1370 struct btrfs_device, dev_list);
1371 if (device->bdev == root->fs_info->sb->s_bdev)
1372 root->fs_info->sb->s_bdev = next_device->bdev;
1373 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1374 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1377 device->fs_devices->open_devices--;
1379 call_rcu(&device->rcu, free_device);
1380 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1382 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1383 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1385 if (cur_devices->open_devices == 0) {
1386 struct btrfs_fs_devices *fs_devices;
1387 fs_devices = root->fs_info->fs_devices;
1388 while (fs_devices) {
1389 if (fs_devices->seed == cur_devices)
1391 fs_devices = fs_devices->seed;
1393 fs_devices->seed = cur_devices->seed;
1394 cur_devices->seed = NULL;
1396 __btrfs_close_devices(cur_devices);
1397 unlock_chunks(root);
1398 free_fs_devices(cur_devices);
1402 * at this point, the device is zero sized. We want to
1403 * remove it from the devices list and zero out the old super
1406 /* make sure this device isn't detected as part of
1409 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1410 set_buffer_dirty(bh);
1411 sync_dirty_buffer(bh);
1420 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1422 mutex_unlock(&root->fs_info->volume_mutex);
1423 mutex_unlock(&uuid_mutex);
1426 if (device->writeable) {
1428 list_add(&device->dev_alloc_list,
1429 &root->fs_info->fs_devices->alloc_list);
1430 unlock_chunks(root);
1431 root->fs_info->fs_devices->rw_devices++;
1437 * does all the dirty work required for changing file system's UUID.
1439 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root)
1442 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1443 struct btrfs_fs_devices *old_devices;
1444 struct btrfs_fs_devices *seed_devices;
1445 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1446 struct btrfs_device *device;
1449 BUG_ON(!mutex_is_locked(&uuid_mutex));
1450 if (!fs_devices->seeding)
1453 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1457 old_devices = clone_fs_devices(fs_devices);
1458 if (IS_ERR(old_devices)) {
1459 kfree(seed_devices);
1460 return PTR_ERR(old_devices);
1463 list_add(&old_devices->list, &fs_uuids);
1465 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1466 seed_devices->opened = 1;
1467 INIT_LIST_HEAD(&seed_devices->devices);
1468 INIT_LIST_HEAD(&seed_devices->alloc_list);
1469 mutex_init(&seed_devices->device_list_mutex);
1471 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1472 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1474 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1476 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1477 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1478 device->fs_devices = seed_devices;
1481 fs_devices->seeding = 0;
1482 fs_devices->num_devices = 0;
1483 fs_devices->open_devices = 0;
1484 fs_devices->seed = seed_devices;
1486 generate_random_uuid(fs_devices->fsid);
1487 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1488 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1489 super_flags = btrfs_super_flags(disk_super) &
1490 ~BTRFS_SUPER_FLAG_SEEDING;
1491 btrfs_set_super_flags(disk_super, super_flags);
1497 * strore the expected generation for seed devices in device items.
1499 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1500 struct btrfs_root *root)
1502 struct btrfs_path *path;
1503 struct extent_buffer *leaf;
1504 struct btrfs_dev_item *dev_item;
1505 struct btrfs_device *device;
1506 struct btrfs_key key;
1507 u8 fs_uuid[BTRFS_UUID_SIZE];
1508 u8 dev_uuid[BTRFS_UUID_SIZE];
1512 path = btrfs_alloc_path();
1516 root = root->fs_info->chunk_root;
1517 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1519 key.type = BTRFS_DEV_ITEM_KEY;
1522 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1526 leaf = path->nodes[0];
1528 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1529 ret = btrfs_next_leaf(root, path);
1534 leaf = path->nodes[0];
1535 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1536 btrfs_release_path(path);
1540 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1541 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1542 key.type != BTRFS_DEV_ITEM_KEY)
1545 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1546 struct btrfs_dev_item);
1547 devid = btrfs_device_id(leaf, dev_item);
1548 read_extent_buffer(leaf, dev_uuid,
1549 (unsigned long)btrfs_device_uuid(dev_item),
1551 read_extent_buffer(leaf, fs_uuid,
1552 (unsigned long)btrfs_device_fsid(dev_item),
1554 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1557 if (device->fs_devices->seeding) {
1558 btrfs_set_device_generation(leaf, dev_item,
1559 device->generation);
1560 btrfs_mark_buffer_dirty(leaf);
1568 btrfs_free_path(path);
1572 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1574 struct request_queue *q;
1575 struct btrfs_trans_handle *trans;
1576 struct btrfs_device *device;
1577 struct block_device *bdev;
1578 struct list_head *devices;
1579 struct super_block *sb = root->fs_info->sb;
1581 int seeding_dev = 0;
1584 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1587 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1588 root->fs_info->bdev_holder);
1590 return PTR_ERR(bdev);
1592 if (root->fs_info->fs_devices->seeding) {
1594 down_write(&sb->s_umount);
1595 mutex_lock(&uuid_mutex);
1598 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1599 mutex_lock(&root->fs_info->volume_mutex);
1601 devices = &root->fs_info->fs_devices->devices;
1603 * we have the volume lock, so we don't need the extra
1604 * device list mutex while reading the list here.
1606 list_for_each_entry(device, devices, dev_list) {
1607 if (device->bdev == bdev) {
1613 device = kzalloc(sizeof(*device), GFP_NOFS);
1615 /* we can safely leave the fs_devices entry around */
1620 device->name = kstrdup(device_path, GFP_NOFS);
1621 if (!device->name) {
1627 ret = find_next_devid(root, &device->devid);
1629 kfree(device->name);
1634 trans = btrfs_start_transaction(root, 0);
1635 if (IS_ERR(trans)) {
1636 kfree(device->name);
1638 ret = PTR_ERR(trans);
1644 q = bdev_get_queue(bdev);
1645 if (blk_queue_discard(q))
1646 device->can_discard = 1;
1647 device->writeable = 1;
1648 device->work.func = pending_bios_fn;
1649 generate_random_uuid(device->uuid);
1650 spin_lock_init(&device->io_lock);
1651 device->generation = trans->transid;
1652 device->io_width = root->sectorsize;
1653 device->io_align = root->sectorsize;
1654 device->sector_size = root->sectorsize;
1655 device->total_bytes = i_size_read(bdev->bd_inode);
1656 device->disk_total_bytes = device->total_bytes;
1657 device->dev_root = root->fs_info->dev_root;
1658 device->bdev = bdev;
1659 device->in_fs_metadata = 1;
1660 device->mode = FMODE_EXCL;
1661 set_blocksize(device->bdev, 4096);
1664 sb->s_flags &= ~MS_RDONLY;
1665 ret = btrfs_prepare_sprout(trans, root);
1669 device->fs_devices = root->fs_info->fs_devices;
1672 * we don't want write_supers to jump in here with our device
1675 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1676 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
1677 list_add(&device->dev_alloc_list,
1678 &root->fs_info->fs_devices->alloc_list);
1679 root->fs_info->fs_devices->num_devices++;
1680 root->fs_info->fs_devices->open_devices++;
1681 root->fs_info->fs_devices->rw_devices++;
1682 if (device->can_discard)
1683 root->fs_info->fs_devices->num_can_discard++;
1684 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1686 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1687 root->fs_info->fs_devices->rotating = 1;
1689 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1690 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1691 total_bytes + device->total_bytes);
1693 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1694 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1696 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1699 ret = init_first_rw_device(trans, root, device);
1701 ret = btrfs_finish_sprout(trans, root);
1704 ret = btrfs_add_device(trans, root, device);
1708 * we've got more storage, clear any full flags on the space
1711 btrfs_clear_space_info_full(root->fs_info);
1713 unlock_chunks(root);
1714 btrfs_commit_transaction(trans, root);
1717 mutex_unlock(&uuid_mutex);
1718 up_write(&sb->s_umount);
1720 ret = btrfs_relocate_sys_chunks(root);
1724 mutex_unlock(&root->fs_info->volume_mutex);
1727 blkdev_put(bdev, FMODE_EXCL);
1729 mutex_unlock(&uuid_mutex);
1730 up_write(&sb->s_umount);
1735 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1736 struct btrfs_device *device)
1739 struct btrfs_path *path;
1740 struct btrfs_root *root;
1741 struct btrfs_dev_item *dev_item;
1742 struct extent_buffer *leaf;
1743 struct btrfs_key key;
1745 root = device->dev_root->fs_info->chunk_root;
1747 path = btrfs_alloc_path();
1751 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1752 key.type = BTRFS_DEV_ITEM_KEY;
1753 key.offset = device->devid;
1755 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1764 leaf = path->nodes[0];
1765 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1767 btrfs_set_device_id(leaf, dev_item, device->devid);
1768 btrfs_set_device_type(leaf, dev_item, device->type);
1769 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1770 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1771 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1772 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1773 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1774 btrfs_mark_buffer_dirty(leaf);
1777 btrfs_free_path(path);
1781 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1782 struct btrfs_device *device, u64 new_size)
1784 struct btrfs_super_block *super_copy =
1785 &device->dev_root->fs_info->super_copy;
1786 u64 old_total = btrfs_super_total_bytes(super_copy);
1787 u64 diff = new_size - device->total_bytes;
1789 if (!device->writeable)
1791 if (new_size <= device->total_bytes)
1794 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1795 device->fs_devices->total_rw_bytes += diff;
1797 device->total_bytes = new_size;
1798 device->disk_total_bytes = new_size;
1799 btrfs_clear_space_info_full(device->dev_root->fs_info);
1801 return btrfs_update_device(trans, device);
1804 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1805 struct btrfs_device *device, u64 new_size)
1808 lock_chunks(device->dev_root);
1809 ret = __btrfs_grow_device(trans, device, new_size);
1810 unlock_chunks(device->dev_root);
1814 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1815 struct btrfs_root *root,
1816 u64 chunk_tree, u64 chunk_objectid,
1820 struct btrfs_path *path;
1821 struct btrfs_key key;
1823 root = root->fs_info->chunk_root;
1824 path = btrfs_alloc_path();
1828 key.objectid = chunk_objectid;
1829 key.offset = chunk_offset;
1830 key.type = BTRFS_CHUNK_ITEM_KEY;
1832 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1835 ret = btrfs_del_item(trans, root, path);
1837 btrfs_free_path(path);
1841 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1844 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1845 struct btrfs_disk_key *disk_key;
1846 struct btrfs_chunk *chunk;
1853 struct btrfs_key key;
1855 array_size = btrfs_super_sys_array_size(super_copy);
1857 ptr = super_copy->sys_chunk_array;
1860 while (cur < array_size) {
1861 disk_key = (struct btrfs_disk_key *)ptr;
1862 btrfs_disk_key_to_cpu(&key, disk_key);
1864 len = sizeof(*disk_key);
1866 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1867 chunk = (struct btrfs_chunk *)(ptr + len);
1868 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1869 len += btrfs_chunk_item_size(num_stripes);
1874 if (key.objectid == chunk_objectid &&
1875 key.offset == chunk_offset) {
1876 memmove(ptr, ptr + len, array_size - (cur + len));
1878 btrfs_set_super_sys_array_size(super_copy, array_size);
1887 static int btrfs_relocate_chunk(struct btrfs_root *root,
1888 u64 chunk_tree, u64 chunk_objectid,
1891 struct extent_map_tree *em_tree;
1892 struct btrfs_root *extent_root;
1893 struct btrfs_trans_handle *trans;
1894 struct extent_map *em;
1895 struct map_lookup *map;
1899 root = root->fs_info->chunk_root;
1900 extent_root = root->fs_info->extent_root;
1901 em_tree = &root->fs_info->mapping_tree.map_tree;
1903 ret = btrfs_can_relocate(extent_root, chunk_offset);
1907 /* step one, relocate all the extents inside this chunk */
1908 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1912 trans = btrfs_start_transaction(root, 0);
1913 BUG_ON(IS_ERR(trans));
1918 * step two, delete the device extents and the
1919 * chunk tree entries
1921 read_lock(&em_tree->lock);
1922 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1923 read_unlock(&em_tree->lock);
1925 BUG_ON(em->start > chunk_offset ||
1926 em->start + em->len < chunk_offset);
1927 map = (struct map_lookup *)em->bdev;
1929 for (i = 0; i < map->num_stripes; i++) {
1930 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1931 map->stripes[i].physical);
1934 if (map->stripes[i].dev) {
1935 ret = btrfs_update_device(trans, map->stripes[i].dev);
1939 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1944 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1946 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1947 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1951 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1954 write_lock(&em_tree->lock);
1955 remove_extent_mapping(em_tree, em);
1956 write_unlock(&em_tree->lock);
1961 /* once for the tree */
1962 free_extent_map(em);
1964 free_extent_map(em);
1966 unlock_chunks(root);
1967 btrfs_end_transaction(trans, root);
1971 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1973 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1974 struct btrfs_path *path;
1975 struct extent_buffer *leaf;
1976 struct btrfs_chunk *chunk;
1977 struct btrfs_key key;
1978 struct btrfs_key found_key;
1979 u64 chunk_tree = chunk_root->root_key.objectid;
1981 bool retried = false;
1985 path = btrfs_alloc_path();
1990 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1991 key.offset = (u64)-1;
1992 key.type = BTRFS_CHUNK_ITEM_KEY;
1995 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2000 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2007 leaf = path->nodes[0];
2008 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2010 chunk = btrfs_item_ptr(leaf, path->slots[0],
2011 struct btrfs_chunk);
2012 chunk_type = btrfs_chunk_type(leaf, chunk);
2013 btrfs_release_path(path);
2015 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2016 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2025 if (found_key.offset == 0)
2027 key.offset = found_key.offset - 1;
2030 if (failed && !retried) {
2034 } else if (failed && retried) {
2039 btrfs_free_path(path);
2043 static u64 div_factor(u64 num, int factor)
2052 int btrfs_balance(struct btrfs_root *dev_root)
2055 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2056 struct btrfs_device *device;
2059 struct btrfs_path *path;
2060 struct btrfs_key key;
2061 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2062 struct btrfs_trans_handle *trans;
2063 struct btrfs_key found_key;
2065 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2068 if (!capable(CAP_SYS_ADMIN))
2071 mutex_lock(&dev_root->fs_info->volume_mutex);
2072 dev_root = dev_root->fs_info->dev_root;
2074 /* step one make some room on all the devices */
2075 list_for_each_entry(device, devices, dev_list) {
2076 old_size = device->total_bytes;
2077 size_to_free = div_factor(old_size, 1);
2078 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2079 if (!device->writeable ||
2080 device->total_bytes - device->bytes_used > size_to_free)
2083 ret = btrfs_shrink_device(device, old_size - size_to_free);
2088 trans = btrfs_start_transaction(dev_root, 0);
2089 BUG_ON(IS_ERR(trans));
2091 ret = btrfs_grow_device(trans, device, old_size);
2094 btrfs_end_transaction(trans, dev_root);
2097 /* step two, relocate all the chunks */
2098 path = btrfs_alloc_path();
2103 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2104 key.offset = (u64)-1;
2105 key.type = BTRFS_CHUNK_ITEM_KEY;
2108 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2113 * this shouldn't happen, it means the last relocate
2119 ret = btrfs_previous_item(chunk_root, path, 0,
2120 BTRFS_CHUNK_ITEM_KEY);
2124 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2126 if (found_key.objectid != key.objectid)
2129 /* chunk zero is special */
2130 if (found_key.offset == 0)
2133 btrfs_release_path(path);
2134 ret = btrfs_relocate_chunk(chunk_root,
2135 chunk_root->root_key.objectid,
2138 if (ret && ret != -ENOSPC)
2140 key.offset = found_key.offset - 1;
2144 btrfs_free_path(path);
2145 mutex_unlock(&dev_root->fs_info->volume_mutex);
2150 * shrinking a device means finding all of the device extents past
2151 * the new size, and then following the back refs to the chunks.
2152 * The chunk relocation code actually frees the device extent
2154 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2156 struct btrfs_trans_handle *trans;
2157 struct btrfs_root *root = device->dev_root;
2158 struct btrfs_dev_extent *dev_extent = NULL;
2159 struct btrfs_path *path;
2167 bool retried = false;
2168 struct extent_buffer *l;
2169 struct btrfs_key key;
2170 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2171 u64 old_total = btrfs_super_total_bytes(super_copy);
2172 u64 old_size = device->total_bytes;
2173 u64 diff = device->total_bytes - new_size;
2175 if (new_size >= device->total_bytes)
2178 path = btrfs_alloc_path();
2186 device->total_bytes = new_size;
2187 if (device->writeable)
2188 device->fs_devices->total_rw_bytes -= diff;
2189 unlock_chunks(root);
2192 key.objectid = device->devid;
2193 key.offset = (u64)-1;
2194 key.type = BTRFS_DEV_EXTENT_KEY;
2197 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2201 ret = btrfs_previous_item(root, path, 0, key.type);
2206 btrfs_release_path(path);
2211 slot = path->slots[0];
2212 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2214 if (key.objectid != device->devid) {
2215 btrfs_release_path(path);
2219 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2220 length = btrfs_dev_extent_length(l, dev_extent);
2222 if (key.offset + length <= new_size) {
2223 btrfs_release_path(path);
2227 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2228 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2229 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2230 btrfs_release_path(path);
2232 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2234 if (ret && ret != -ENOSPC)
2241 if (failed && !retried) {
2245 } else if (failed && retried) {
2249 device->total_bytes = old_size;
2250 if (device->writeable)
2251 device->fs_devices->total_rw_bytes += diff;
2252 unlock_chunks(root);
2256 /* Shrinking succeeded, else we would be at "done". */
2257 trans = btrfs_start_transaction(root, 0);
2258 if (IS_ERR(trans)) {
2259 ret = PTR_ERR(trans);
2265 device->disk_total_bytes = new_size;
2266 /* Now btrfs_update_device() will change the on-disk size. */
2267 ret = btrfs_update_device(trans, device);
2269 unlock_chunks(root);
2270 btrfs_end_transaction(trans, root);
2273 WARN_ON(diff > old_total);
2274 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2275 unlock_chunks(root);
2276 btrfs_end_transaction(trans, root);
2278 btrfs_free_path(path);
2282 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2283 struct btrfs_root *root,
2284 struct btrfs_key *key,
2285 struct btrfs_chunk *chunk, int item_size)
2287 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2288 struct btrfs_disk_key disk_key;
2292 array_size = btrfs_super_sys_array_size(super_copy);
2293 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2296 ptr = super_copy->sys_chunk_array + array_size;
2297 btrfs_cpu_key_to_disk(&disk_key, key);
2298 memcpy(ptr, &disk_key, sizeof(disk_key));
2299 ptr += sizeof(disk_key);
2300 memcpy(ptr, chunk, item_size);
2301 item_size += sizeof(disk_key);
2302 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2307 * sort the devices in descending order by max_avail, total_avail
2309 static int btrfs_cmp_device_info(const void *a, const void *b)
2311 const struct btrfs_device_info *di_a = a;
2312 const struct btrfs_device_info *di_b = b;
2314 if (di_a->max_avail > di_b->max_avail)
2316 if (di_a->max_avail < di_b->max_avail)
2318 if (di_a->total_avail > di_b->total_avail)
2320 if (di_a->total_avail < di_b->total_avail)
2325 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2326 struct btrfs_root *extent_root,
2327 struct map_lookup **map_ret,
2328 u64 *num_bytes_out, u64 *stripe_size_out,
2329 u64 start, u64 type)
2331 struct btrfs_fs_info *info = extent_root->fs_info;
2332 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2333 struct list_head *cur;
2334 struct map_lookup *map = NULL;
2335 struct extent_map_tree *em_tree;
2336 struct extent_map *em;
2337 struct btrfs_device_info *devices_info = NULL;
2339 int num_stripes; /* total number of stripes to allocate */
2340 int sub_stripes; /* sub_stripes info for map */
2341 int dev_stripes; /* stripes per dev */
2342 int devs_max; /* max devs to use */
2343 int devs_min; /* min devs needed */
2344 int devs_increment; /* ndevs has to be a multiple of this */
2345 int ncopies; /* how many copies to data has */
2347 u64 max_stripe_size;
2355 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2356 (type & BTRFS_BLOCK_GROUP_DUP)) {
2358 type &= ~BTRFS_BLOCK_GROUP_DUP;
2361 if (list_empty(&fs_devices->alloc_list))
2368 devs_max = 0; /* 0 == as many as possible */
2372 * define the properties of each RAID type.
2373 * FIXME: move this to a global table and use it in all RAID
2376 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2380 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2382 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2387 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2396 if (type & BTRFS_BLOCK_GROUP_DATA) {
2397 max_stripe_size = 1024 * 1024 * 1024;
2398 max_chunk_size = 10 * max_stripe_size;
2399 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2400 max_stripe_size = 256 * 1024 * 1024;
2401 max_chunk_size = max_stripe_size;
2402 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2403 max_stripe_size = 8 * 1024 * 1024;
2404 max_chunk_size = 2 * max_stripe_size;
2406 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2411 /* we don't want a chunk larger than 10% of writeable space */
2412 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2415 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2420 cur = fs_devices->alloc_list.next;
2423 * in the first pass through the devices list, we gather information
2424 * about the available holes on each device.
2427 while (cur != &fs_devices->alloc_list) {
2428 struct btrfs_device *device;
2432 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2436 if (!device->writeable) {
2438 "btrfs: read-only device in alloc_list\n");
2443 if (!device->in_fs_metadata)
2446 if (device->total_bytes > device->bytes_used)
2447 total_avail = device->total_bytes - device->bytes_used;
2450 /* avail is off by max(alloc_start, 1MB), but that is the same
2451 * for all devices, so it doesn't hurt the sorting later on
2454 ret = find_free_dev_extent(trans, device,
2455 max_stripe_size * dev_stripes,
2456 &dev_offset, &max_avail);
2457 if (ret && ret != -ENOSPC)
2461 max_avail = max_stripe_size * dev_stripes;
2463 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2466 devices_info[ndevs].dev_offset = dev_offset;
2467 devices_info[ndevs].max_avail = max_avail;
2468 devices_info[ndevs].total_avail = total_avail;
2469 devices_info[ndevs].dev = device;
2474 * now sort the devices by hole size / available space
2476 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2477 btrfs_cmp_device_info, NULL);
2479 /* round down to number of usable stripes */
2480 ndevs -= ndevs % devs_increment;
2482 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2487 if (devs_max && ndevs > devs_max)
2490 * the primary goal is to maximize the number of stripes, so use as many
2491 * devices as possible, even if the stripes are not maximum sized.
2493 stripe_size = devices_info[ndevs-1].max_avail;
2494 num_stripes = ndevs * dev_stripes;
2496 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2497 stripe_size = max_chunk_size * ncopies;
2498 do_div(stripe_size, num_stripes);
2501 do_div(stripe_size, dev_stripes);
2502 do_div(stripe_size, BTRFS_STRIPE_LEN);
2503 stripe_size *= BTRFS_STRIPE_LEN;
2505 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2510 map->num_stripes = num_stripes;
2512 for (i = 0; i < ndevs; ++i) {
2513 for (j = 0; j < dev_stripes; ++j) {
2514 int s = i * dev_stripes + j;
2515 map->stripes[s].dev = devices_info[i].dev;
2516 map->stripes[s].physical = devices_info[i].dev_offset +
2520 map->sector_size = extent_root->sectorsize;
2521 map->stripe_len = BTRFS_STRIPE_LEN;
2522 map->io_align = BTRFS_STRIPE_LEN;
2523 map->io_width = BTRFS_STRIPE_LEN;
2525 map->sub_stripes = sub_stripes;
2528 num_bytes = stripe_size * (num_stripes / ncopies);
2530 *stripe_size_out = stripe_size;
2531 *num_bytes_out = num_bytes;
2533 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
2535 em = alloc_extent_map();
2540 em->bdev = (struct block_device *)map;
2542 em->len = num_bytes;
2543 em->block_start = 0;
2544 em->block_len = em->len;
2546 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2547 write_lock(&em_tree->lock);
2548 ret = add_extent_mapping(em_tree, em);
2549 write_unlock(&em_tree->lock);
2551 free_extent_map(em);
2553 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2554 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2558 for (i = 0; i < map->num_stripes; ++i) {
2559 struct btrfs_device *device;
2562 device = map->stripes[i].dev;
2563 dev_offset = map->stripes[i].physical;
2565 ret = btrfs_alloc_dev_extent(trans, device,
2566 info->chunk_root->root_key.objectid,
2567 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2568 start, dev_offset, stripe_size);
2572 kfree(devices_info);
2577 kfree(devices_info);
2581 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2582 struct btrfs_root *extent_root,
2583 struct map_lookup *map, u64 chunk_offset,
2584 u64 chunk_size, u64 stripe_size)
2587 struct btrfs_key key;
2588 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2589 struct btrfs_device *device;
2590 struct btrfs_chunk *chunk;
2591 struct btrfs_stripe *stripe;
2592 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2596 chunk = kzalloc(item_size, GFP_NOFS);
2601 while (index < map->num_stripes) {
2602 device = map->stripes[index].dev;
2603 device->bytes_used += stripe_size;
2604 ret = btrfs_update_device(trans, device);
2610 stripe = &chunk->stripe;
2611 while (index < map->num_stripes) {
2612 device = map->stripes[index].dev;
2613 dev_offset = map->stripes[index].physical;
2615 btrfs_set_stack_stripe_devid(stripe, device->devid);
2616 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2617 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2622 btrfs_set_stack_chunk_length(chunk, chunk_size);
2623 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2624 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2625 btrfs_set_stack_chunk_type(chunk, map->type);
2626 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2627 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2628 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2629 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2630 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2632 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2633 key.type = BTRFS_CHUNK_ITEM_KEY;
2634 key.offset = chunk_offset;
2636 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2639 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2640 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2650 * Chunk allocation falls into two parts. The first part does works
2651 * that make the new allocated chunk useable, but not do any operation
2652 * that modifies the chunk tree. The second part does the works that
2653 * require modifying the chunk tree. This division is important for the
2654 * bootstrap process of adding storage to a seed btrfs.
2656 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2657 struct btrfs_root *extent_root, u64 type)
2662 struct map_lookup *map;
2663 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2666 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2671 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2672 &stripe_size, chunk_offset, type);
2676 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2677 chunk_size, stripe_size);
2682 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2683 struct btrfs_root *root,
2684 struct btrfs_device *device)
2687 u64 sys_chunk_offset;
2691 u64 sys_stripe_size;
2693 struct map_lookup *map;
2694 struct map_lookup *sys_map;
2695 struct btrfs_fs_info *fs_info = root->fs_info;
2696 struct btrfs_root *extent_root = fs_info->extent_root;
2699 ret = find_next_chunk(fs_info->chunk_root,
2700 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);
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,
2845 struct extent_map *em;
2846 struct map_lookup *map;
2847 struct extent_map_tree *em_tree = &map_tree->map_tree;
2850 u64 stripe_end_offset;
2854 int stripes_allocated = 8;
2855 int stripes_required = 1;
2860 struct btrfs_multi_bio *multi = NULL;
2862 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
2863 stripes_allocated = 1;
2866 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2871 atomic_set(&multi->error, 0);
2874 read_lock(&em_tree->lock);
2875 em = lookup_extent_mapping(em_tree, logical, *length);
2876 read_unlock(&em_tree->lock);
2879 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2880 (unsigned long long)logical,
2881 (unsigned long long)*length);
2885 BUG_ON(em->start > logical || em->start + em->len < logical);
2886 map = (struct map_lookup *)em->bdev;
2887 offset = logical - em->start;
2889 if (mirror_num > map->num_stripes)
2892 /* if our multi bio struct is too small, back off and try again */
2893 if (rw & REQ_WRITE) {
2894 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2895 BTRFS_BLOCK_GROUP_DUP)) {
2896 stripes_required = map->num_stripes;
2898 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2899 stripes_required = map->sub_stripes;
2903 if (rw & REQ_DISCARD) {
2904 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2905 BTRFS_BLOCK_GROUP_RAID1 |
2906 BTRFS_BLOCK_GROUP_DUP |
2907 BTRFS_BLOCK_GROUP_RAID10)) {
2908 stripes_required = map->num_stripes;
2911 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
2912 stripes_allocated < stripes_required) {
2913 stripes_allocated = map->num_stripes;
2914 free_extent_map(em);
2920 * stripe_nr counts the total number of stripes we have to stride
2921 * to get to this block
2923 do_div(stripe_nr, map->stripe_len);
2925 stripe_offset = stripe_nr * map->stripe_len;
2926 BUG_ON(offset < stripe_offset);
2928 /* stripe_offset is the offset of this block in its stripe*/
2929 stripe_offset = offset - stripe_offset;
2931 if (rw & REQ_DISCARD)
2932 *length = min_t(u64, em->len - offset, *length);
2933 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2934 BTRFS_BLOCK_GROUP_RAID1 |
2935 BTRFS_BLOCK_GROUP_RAID10 |
2936 BTRFS_BLOCK_GROUP_DUP)) {
2937 /* we limit the length of each bio to what fits in a stripe */
2938 *length = min_t(u64, em->len - offset,
2939 map->stripe_len - stripe_offset);
2941 *length = em->len - offset;
2949 stripe_nr_orig = stripe_nr;
2950 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2951 (~(map->stripe_len - 1));
2952 do_div(stripe_nr_end, map->stripe_len);
2953 stripe_end_offset = stripe_nr_end * map->stripe_len -
2955 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2956 if (rw & REQ_DISCARD)
2957 num_stripes = min_t(u64, map->num_stripes,
2958 stripe_nr_end - stripe_nr_orig);
2959 stripe_index = do_div(stripe_nr, map->num_stripes);
2960 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2961 if (rw & (REQ_WRITE | REQ_DISCARD))
2962 num_stripes = map->num_stripes;
2963 else if (mirror_num)
2964 stripe_index = mirror_num - 1;
2966 stripe_index = find_live_mirror(map, 0,
2968 current->pid % map->num_stripes);
2971 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2972 if (rw & (REQ_WRITE | REQ_DISCARD))
2973 num_stripes = map->num_stripes;
2974 else if (mirror_num)
2975 stripe_index = mirror_num - 1;
2977 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2978 int factor = map->num_stripes / map->sub_stripes;
2980 stripe_index = do_div(stripe_nr, factor);
2981 stripe_index *= map->sub_stripes;
2984 num_stripes = map->sub_stripes;
2985 else if (rw & REQ_DISCARD)
2986 num_stripes = min_t(u64, map->sub_stripes *
2987 (stripe_nr_end - stripe_nr_orig),
2989 else if (mirror_num)
2990 stripe_index += mirror_num - 1;
2992 stripe_index = find_live_mirror(map, stripe_index,
2993 map->sub_stripes, stripe_index +
2994 current->pid % map->sub_stripes);
2998 * after this do_div call, stripe_nr is the number of stripes
2999 * on this device we have to walk to find the data, and
3000 * stripe_index is the number of our device in the stripe array
3002 stripe_index = do_div(stripe_nr, map->num_stripes);
3004 BUG_ON(stripe_index >= map->num_stripes);
3006 if (rw & REQ_DISCARD) {
3007 for (i = 0; i < num_stripes; i++) {
3008 multi->stripes[i].physical =
3009 map->stripes[stripe_index].physical +
3010 stripe_offset + stripe_nr * map->stripe_len;
3011 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3013 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3015 u32 last_stripe = 0;
3018 div_u64_rem(stripe_nr_end - 1,
3022 for (j = 0; j < map->num_stripes; j++) {
3025 div_u64_rem(stripe_nr_end - 1 - j,
3026 map->num_stripes, &test);
3027 if (test == stripe_index)
3030 stripes = stripe_nr_end - 1 - j;
3031 do_div(stripes, map->num_stripes);
3032 multi->stripes[i].length = map->stripe_len *
3033 (stripes - stripe_nr + 1);
3036 multi->stripes[i].length -=
3040 if (stripe_index == last_stripe)
3041 multi->stripes[i].length -=
3043 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3046 int factor = map->num_stripes /
3048 u32 last_stripe = 0;
3050 div_u64_rem(stripe_nr_end - 1,
3051 factor, &last_stripe);
3052 last_stripe *= map->sub_stripes;
3054 for (j = 0; j < factor; j++) {
3057 div_u64_rem(stripe_nr_end - 1 - j,
3061 stripe_index / map->sub_stripes)
3064 stripes = stripe_nr_end - 1 - j;
3065 do_div(stripes, factor);
3066 multi->stripes[i].length = map->stripe_len *
3067 (stripes - stripe_nr + 1);
3069 if (i < map->sub_stripes) {
3070 multi->stripes[i].length -=
3072 if (i == map->sub_stripes - 1)
3075 if (stripe_index >= last_stripe &&
3076 stripe_index <= (last_stripe +
3077 map->sub_stripes - 1)) {
3078 multi->stripes[i].length -=
3082 multi->stripes[i].length = *length;
3085 if (stripe_index == map->num_stripes) {
3086 /* This could only happen for RAID0/10 */
3092 for (i = 0; i < num_stripes; i++) {
3093 multi->stripes[i].physical =
3094 map->stripes[stripe_index].physical +
3096 stripe_nr * map->stripe_len;
3097 multi->stripes[i].dev =
3098 map->stripes[stripe_index].dev;
3104 multi->num_stripes = num_stripes;
3105 multi->max_errors = max_errors;
3108 free_extent_map(em);
3112 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3113 u64 logical, u64 *length,
3114 struct btrfs_multi_bio **multi_ret, int mirror_num)
3116 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3120 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3121 u64 chunk_start, u64 physical, u64 devid,
3122 u64 **logical, int *naddrs, int *stripe_len)
3124 struct extent_map_tree *em_tree = &map_tree->map_tree;
3125 struct extent_map *em;
3126 struct map_lookup *map;
3133 read_lock(&em_tree->lock);
3134 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3135 read_unlock(&em_tree->lock);
3137 BUG_ON(!em || em->start != chunk_start);
3138 map = (struct map_lookup *)em->bdev;
3141 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3142 do_div(length, map->num_stripes / map->sub_stripes);
3143 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3144 do_div(length, map->num_stripes);
3146 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3149 for (i = 0; i < map->num_stripes; i++) {
3150 if (devid && map->stripes[i].dev->devid != devid)
3152 if (map->stripes[i].physical > physical ||
3153 map->stripes[i].physical + length <= physical)
3156 stripe_nr = physical - map->stripes[i].physical;
3157 do_div(stripe_nr, map->stripe_len);
3159 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3160 stripe_nr = stripe_nr * map->num_stripes + i;
3161 do_div(stripe_nr, map->sub_stripes);
3162 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3163 stripe_nr = stripe_nr * map->num_stripes + i;
3165 bytenr = chunk_start + stripe_nr * map->stripe_len;
3166 WARN_ON(nr >= map->num_stripes);
3167 for (j = 0; j < nr; j++) {
3168 if (buf[j] == bytenr)
3172 WARN_ON(nr >= map->num_stripes);
3179 *stripe_len = map->stripe_len;
3181 free_extent_map(em);
3185 static void end_bio_multi_stripe(struct bio *bio, int err)
3187 struct btrfs_multi_bio *multi = bio->bi_private;
3188 int is_orig_bio = 0;
3191 atomic_inc(&multi->error);
3193 if (bio == multi->orig_bio)
3196 if (atomic_dec_and_test(&multi->stripes_pending)) {
3199 bio = multi->orig_bio;
3201 bio->bi_private = multi->private;
3202 bio->bi_end_io = multi->end_io;
3203 /* only send an error to the higher layers if it is
3204 * beyond the tolerance of the multi-bio
3206 if (atomic_read(&multi->error) > multi->max_errors) {
3210 * this bio is actually up to date, we didn't
3211 * go over the max number of errors
3213 set_bit(BIO_UPTODATE, &bio->bi_flags);
3218 bio_endio(bio, err);
3219 } else if (!is_orig_bio) {
3224 struct async_sched {
3227 struct btrfs_fs_info *info;
3228 struct btrfs_work work;
3232 * see run_scheduled_bios for a description of why bios are collected for
3235 * This will add one bio to the pending list for a device and make sure
3236 * the work struct is scheduled.
3238 static noinline int schedule_bio(struct btrfs_root *root,
3239 struct btrfs_device *device,
3240 int rw, struct bio *bio)
3242 int should_queue = 1;
3243 struct btrfs_pending_bios *pending_bios;
3245 /* don't bother with additional async steps for reads, right now */
3246 if (!(rw & REQ_WRITE)) {
3248 submit_bio(rw, bio);
3254 * nr_async_bios allows us to reliably return congestion to the
3255 * higher layers. Otherwise, the async bio makes it appear we have
3256 * made progress against dirty pages when we've really just put it
3257 * on a queue for later
3259 atomic_inc(&root->fs_info->nr_async_bios);
3260 WARN_ON(bio->bi_next);
3261 bio->bi_next = NULL;
3264 spin_lock(&device->io_lock);
3265 if (bio->bi_rw & REQ_SYNC)
3266 pending_bios = &device->pending_sync_bios;
3268 pending_bios = &device->pending_bios;
3270 if (pending_bios->tail)
3271 pending_bios->tail->bi_next = bio;
3273 pending_bios->tail = bio;
3274 if (!pending_bios->head)
3275 pending_bios->head = bio;
3276 if (device->running_pending)
3279 spin_unlock(&device->io_lock);
3282 btrfs_queue_worker(&root->fs_info->submit_workers,
3287 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3288 int mirror_num, int async_submit)
3290 struct btrfs_mapping_tree *map_tree;
3291 struct btrfs_device *dev;
3292 struct bio *first_bio = bio;
3293 u64 logical = (u64)bio->bi_sector << 9;
3296 struct btrfs_multi_bio *multi = NULL;
3301 length = bio->bi_size;
3302 map_tree = &root->fs_info->mapping_tree;
3303 map_length = length;
3305 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3309 total_devs = multi->num_stripes;
3310 if (map_length < length) {
3311 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3312 "len %llu\n", (unsigned long long)logical,
3313 (unsigned long long)length,
3314 (unsigned long long)map_length);
3317 multi->end_io = first_bio->bi_end_io;
3318 multi->private = first_bio->bi_private;
3319 multi->orig_bio = first_bio;
3320 atomic_set(&multi->stripes_pending, multi->num_stripes);
3322 while (dev_nr < total_devs) {
3323 if (total_devs > 1) {
3324 if (dev_nr < total_devs - 1) {
3325 bio = bio_clone(first_bio, GFP_NOFS);
3330 bio->bi_private = multi;
3331 bio->bi_end_io = end_bio_multi_stripe;
3333 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3334 dev = multi->stripes[dev_nr].dev;
3335 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3336 bio->bi_bdev = dev->bdev;
3338 schedule_bio(root, dev, rw, bio);
3340 submit_bio(rw, bio);
3342 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3343 bio->bi_sector = logical >> 9;
3344 bio_endio(bio, -EIO);
3348 if (total_devs == 1)
3353 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3356 struct btrfs_device *device;
3357 struct btrfs_fs_devices *cur_devices;
3359 cur_devices = root->fs_info->fs_devices;
3360 while (cur_devices) {
3362 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3363 device = __find_device(&cur_devices->devices,
3368 cur_devices = cur_devices->seed;
3373 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3374 u64 devid, u8 *dev_uuid)
3376 struct btrfs_device *device;
3377 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3379 device = kzalloc(sizeof(*device), GFP_NOFS);
3382 list_add(&device->dev_list,
3383 &fs_devices->devices);
3384 device->dev_root = root->fs_info->dev_root;
3385 device->devid = devid;
3386 device->work.func = pending_bios_fn;
3387 device->fs_devices = fs_devices;
3388 device->missing = 1;
3389 fs_devices->num_devices++;
3390 fs_devices->missing_devices++;
3391 spin_lock_init(&device->io_lock);
3392 INIT_LIST_HEAD(&device->dev_alloc_list);
3393 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3397 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3398 struct extent_buffer *leaf,
3399 struct btrfs_chunk *chunk)
3401 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3402 struct map_lookup *map;
3403 struct extent_map *em;
3407 u8 uuid[BTRFS_UUID_SIZE];
3412 logical = key->offset;
3413 length = btrfs_chunk_length(leaf, chunk);
3415 read_lock(&map_tree->map_tree.lock);
3416 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3417 read_unlock(&map_tree->map_tree.lock);
3419 /* already mapped? */
3420 if (em && em->start <= logical && em->start + em->len > logical) {
3421 free_extent_map(em);
3424 free_extent_map(em);
3427 em = alloc_extent_map();
3430 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3431 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3433 free_extent_map(em);
3437 em->bdev = (struct block_device *)map;
3438 em->start = logical;
3440 em->block_start = 0;
3441 em->block_len = em->len;
3443 map->num_stripes = num_stripes;
3444 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3445 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3446 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3447 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3448 map->type = btrfs_chunk_type(leaf, chunk);
3449 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3450 for (i = 0; i < num_stripes; i++) {
3451 map->stripes[i].physical =
3452 btrfs_stripe_offset_nr(leaf, chunk, i);
3453 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3454 read_extent_buffer(leaf, uuid, (unsigned long)
3455 btrfs_stripe_dev_uuid_nr(chunk, i),
3457 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3459 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3461 free_extent_map(em);
3464 if (!map->stripes[i].dev) {
3465 map->stripes[i].dev =
3466 add_missing_dev(root, devid, uuid);
3467 if (!map->stripes[i].dev) {
3469 free_extent_map(em);
3473 map->stripes[i].dev->in_fs_metadata = 1;
3476 write_lock(&map_tree->map_tree.lock);
3477 ret = add_extent_mapping(&map_tree->map_tree, em);
3478 write_unlock(&map_tree->map_tree.lock);
3480 free_extent_map(em);
3485 static int fill_device_from_item(struct extent_buffer *leaf,
3486 struct btrfs_dev_item *dev_item,
3487 struct btrfs_device *device)
3491 device->devid = btrfs_device_id(leaf, dev_item);
3492 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3493 device->total_bytes = device->disk_total_bytes;
3494 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3495 device->type = btrfs_device_type(leaf, dev_item);
3496 device->io_align = btrfs_device_io_align(leaf, dev_item);
3497 device->io_width = btrfs_device_io_width(leaf, dev_item);
3498 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3500 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3501 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3506 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3508 struct btrfs_fs_devices *fs_devices;
3511 mutex_lock(&uuid_mutex);
3513 fs_devices = root->fs_info->fs_devices->seed;
3514 while (fs_devices) {
3515 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3519 fs_devices = fs_devices->seed;
3522 fs_devices = find_fsid(fsid);
3528 fs_devices = clone_fs_devices(fs_devices);
3529 if (IS_ERR(fs_devices)) {
3530 ret = PTR_ERR(fs_devices);
3534 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3535 root->fs_info->bdev_holder);
3539 if (!fs_devices->seeding) {
3540 __btrfs_close_devices(fs_devices);
3541 free_fs_devices(fs_devices);
3546 fs_devices->seed = root->fs_info->fs_devices->seed;
3547 root->fs_info->fs_devices->seed = fs_devices;
3549 mutex_unlock(&uuid_mutex);
3553 static int read_one_dev(struct btrfs_root *root,
3554 struct extent_buffer *leaf,
3555 struct btrfs_dev_item *dev_item)
3557 struct btrfs_device *device;
3560 u8 fs_uuid[BTRFS_UUID_SIZE];
3561 u8 dev_uuid[BTRFS_UUID_SIZE];
3563 devid = btrfs_device_id(leaf, dev_item);
3564 read_extent_buffer(leaf, dev_uuid,
3565 (unsigned long)btrfs_device_uuid(dev_item),
3567 read_extent_buffer(leaf, fs_uuid,
3568 (unsigned long)btrfs_device_fsid(dev_item),
3571 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3572 ret = open_seed_devices(root, fs_uuid);
3573 if (ret && !btrfs_test_opt(root, DEGRADED))
3577 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3578 if (!device || !device->bdev) {
3579 if (!btrfs_test_opt(root, DEGRADED))
3583 printk(KERN_WARNING "warning devid %llu missing\n",
3584 (unsigned long long)devid);
3585 device = add_missing_dev(root, devid, dev_uuid);
3588 } else if (!device->missing) {
3590 * this happens when a device that was properly setup
3591 * in the device info lists suddenly goes bad.
3592 * device->bdev is NULL, and so we have to set
3593 * device->missing to one here
3595 root->fs_info->fs_devices->missing_devices++;
3596 device->missing = 1;
3600 if (device->fs_devices != root->fs_info->fs_devices) {
3601 BUG_ON(device->writeable);
3602 if (device->generation !=
3603 btrfs_device_generation(leaf, dev_item))
3607 fill_device_from_item(leaf, dev_item, device);
3608 device->dev_root = root->fs_info->dev_root;
3609 device->in_fs_metadata = 1;
3610 if (device->writeable)
3611 device->fs_devices->total_rw_bytes += device->total_bytes;
3616 int btrfs_read_sys_array(struct btrfs_root *root)
3618 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3619 struct extent_buffer *sb;
3620 struct btrfs_disk_key *disk_key;
3621 struct btrfs_chunk *chunk;
3623 unsigned long sb_ptr;
3629 struct btrfs_key key;
3631 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3632 BTRFS_SUPER_INFO_SIZE);
3635 btrfs_set_buffer_uptodate(sb);
3636 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
3638 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3639 array_size = btrfs_super_sys_array_size(super_copy);
3641 ptr = super_copy->sys_chunk_array;
3642 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3645 while (cur < array_size) {
3646 disk_key = (struct btrfs_disk_key *)ptr;
3647 btrfs_disk_key_to_cpu(&key, disk_key);
3649 len = sizeof(*disk_key); ptr += len;
3653 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3654 chunk = (struct btrfs_chunk *)sb_ptr;
3655 ret = read_one_chunk(root, &key, sb, chunk);
3658 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3659 len = btrfs_chunk_item_size(num_stripes);
3668 free_extent_buffer(sb);
3672 int btrfs_read_chunk_tree(struct btrfs_root *root)
3674 struct btrfs_path *path;
3675 struct extent_buffer *leaf;
3676 struct btrfs_key key;
3677 struct btrfs_key found_key;
3681 root = root->fs_info->chunk_root;
3683 path = btrfs_alloc_path();
3687 /* first we search for all of the device items, and then we
3688 * read in all of the chunk items. This way we can create chunk
3689 * mappings that reference all of the devices that are afound
3691 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3695 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3699 leaf = path->nodes[0];
3700 slot = path->slots[0];
3701 if (slot >= btrfs_header_nritems(leaf)) {
3702 ret = btrfs_next_leaf(root, path);
3709 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3710 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3711 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3713 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3714 struct btrfs_dev_item *dev_item;
3715 dev_item = btrfs_item_ptr(leaf, slot,
3716 struct btrfs_dev_item);
3717 ret = read_one_dev(root, leaf, dev_item);
3721 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3722 struct btrfs_chunk *chunk;
3723 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3724 ret = read_one_chunk(root, &found_key, leaf, chunk);
3730 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3732 btrfs_release_path(path);
3737 btrfs_free_path(path);