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;
145 int sync_pending = 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 /* init readahead state */
370 spin_lock_init(&device->reada_lock);
371 device->reada_curr_zone = NULL;
372 atomic_set(&device->reada_in_flight, 0);
373 device->reada_next = 0;
374 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
375 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
377 mutex_lock(&fs_devices->device_list_mutex);
378 list_add_rcu(&device->dev_list, &fs_devices->devices);
379 mutex_unlock(&fs_devices->device_list_mutex);
381 device->fs_devices = fs_devices;
382 fs_devices->num_devices++;
383 } else if (!device->name || strcmp(device->name, path)) {
384 name = kstrdup(path, GFP_NOFS);
389 if (device->missing) {
390 fs_devices->missing_devices--;
395 if (found_transid > fs_devices->latest_trans) {
396 fs_devices->latest_devid = devid;
397 fs_devices->latest_trans = found_transid;
399 *fs_devices_ret = fs_devices;
403 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
405 struct btrfs_fs_devices *fs_devices;
406 struct btrfs_device *device;
407 struct btrfs_device *orig_dev;
409 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
411 return ERR_PTR(-ENOMEM);
413 INIT_LIST_HEAD(&fs_devices->devices);
414 INIT_LIST_HEAD(&fs_devices->alloc_list);
415 INIT_LIST_HEAD(&fs_devices->list);
416 mutex_init(&fs_devices->device_list_mutex);
417 fs_devices->latest_devid = orig->latest_devid;
418 fs_devices->latest_trans = orig->latest_trans;
419 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
421 /* We have held the volume lock, it is safe to get the devices. */
422 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
423 device = kzalloc(sizeof(*device), GFP_NOFS);
427 device->name = kstrdup(orig_dev->name, GFP_NOFS);
433 device->devid = orig_dev->devid;
434 device->work.func = pending_bios_fn;
435 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
436 spin_lock_init(&device->io_lock);
437 INIT_LIST_HEAD(&device->dev_list);
438 INIT_LIST_HEAD(&device->dev_alloc_list);
440 list_add(&device->dev_list, &fs_devices->devices);
441 device->fs_devices = fs_devices;
442 fs_devices->num_devices++;
446 free_fs_devices(fs_devices);
447 return ERR_PTR(-ENOMEM);
450 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
452 struct btrfs_device *device, *next;
454 mutex_lock(&uuid_mutex);
456 /* This is the initialized path, it is safe to release the devices. */
457 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
458 if (device->in_fs_metadata)
462 blkdev_put(device->bdev, device->mode);
464 fs_devices->open_devices--;
466 if (device->writeable) {
467 list_del_init(&device->dev_alloc_list);
468 device->writeable = 0;
469 fs_devices->rw_devices--;
471 list_del_init(&device->dev_list);
472 fs_devices->num_devices--;
477 if (fs_devices->seed) {
478 fs_devices = fs_devices->seed;
482 mutex_unlock(&uuid_mutex);
486 static void __free_device(struct work_struct *work)
488 struct btrfs_device *device;
490 device = container_of(work, struct btrfs_device, rcu_work);
493 blkdev_put(device->bdev, device->mode);
499 static void free_device(struct rcu_head *head)
501 struct btrfs_device *device;
503 device = container_of(head, struct btrfs_device, rcu);
505 INIT_WORK(&device->rcu_work, __free_device);
506 schedule_work(&device->rcu_work);
509 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
511 struct btrfs_device *device;
513 if (--fs_devices->opened > 0)
516 mutex_lock(&fs_devices->device_list_mutex);
517 list_for_each_entry(device, &fs_devices->devices, dev_list) {
518 struct btrfs_device *new_device;
521 fs_devices->open_devices--;
523 if (device->writeable) {
524 list_del_init(&device->dev_alloc_list);
525 fs_devices->rw_devices--;
528 if (device->can_discard)
529 fs_devices->num_can_discard--;
531 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
533 memcpy(new_device, device, sizeof(*new_device));
534 new_device->name = kstrdup(device->name, GFP_NOFS);
535 BUG_ON(device->name && !new_device->name);
536 new_device->bdev = NULL;
537 new_device->writeable = 0;
538 new_device->in_fs_metadata = 0;
539 new_device->can_discard = 0;
540 list_replace_rcu(&device->dev_list, &new_device->dev_list);
542 call_rcu(&device->rcu, free_device);
544 mutex_unlock(&fs_devices->device_list_mutex);
546 WARN_ON(fs_devices->open_devices);
547 WARN_ON(fs_devices->rw_devices);
548 fs_devices->opened = 0;
549 fs_devices->seeding = 0;
554 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
556 struct btrfs_fs_devices *seed_devices = NULL;
559 mutex_lock(&uuid_mutex);
560 ret = __btrfs_close_devices(fs_devices);
561 if (!fs_devices->opened) {
562 seed_devices = fs_devices->seed;
563 fs_devices->seed = NULL;
565 mutex_unlock(&uuid_mutex);
567 while (seed_devices) {
568 fs_devices = seed_devices;
569 seed_devices = fs_devices->seed;
570 __btrfs_close_devices(fs_devices);
571 free_fs_devices(fs_devices);
576 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
577 fmode_t flags, void *holder)
579 struct request_queue *q;
580 struct block_device *bdev;
581 struct list_head *head = &fs_devices->devices;
582 struct btrfs_device *device;
583 struct block_device *latest_bdev = NULL;
584 struct buffer_head *bh;
585 struct btrfs_super_block *disk_super;
586 u64 latest_devid = 0;
587 u64 latest_transid = 0;
594 list_for_each_entry(device, head, dev_list) {
600 bdev = blkdev_get_by_path(device->name, flags, holder);
602 printk(KERN_INFO "open %s failed\n", device->name);
605 set_blocksize(bdev, 4096);
607 bh = btrfs_read_dev_super(bdev);
611 disk_super = (struct btrfs_super_block *)bh->b_data;
612 devid = btrfs_stack_device_id(&disk_super->dev_item);
613 if (devid != device->devid)
616 if (memcmp(device->uuid, disk_super->dev_item.uuid,
620 device->generation = btrfs_super_generation(disk_super);
621 if (!latest_transid || device->generation > latest_transid) {
622 latest_devid = devid;
623 latest_transid = device->generation;
627 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
628 device->writeable = 0;
630 device->writeable = !bdev_read_only(bdev);
634 q = bdev_get_queue(bdev);
635 if (blk_queue_discard(q)) {
636 device->can_discard = 1;
637 fs_devices->num_can_discard++;
641 device->in_fs_metadata = 0;
642 device->mode = flags;
644 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
645 fs_devices->rotating = 1;
647 fs_devices->open_devices++;
648 if (device->writeable) {
649 fs_devices->rw_devices++;
650 list_add(&device->dev_alloc_list,
651 &fs_devices->alloc_list);
659 blkdev_put(bdev, flags);
663 if (fs_devices->open_devices == 0) {
667 fs_devices->seeding = seeding;
668 fs_devices->opened = 1;
669 fs_devices->latest_bdev = latest_bdev;
670 fs_devices->latest_devid = latest_devid;
671 fs_devices->latest_trans = latest_transid;
672 fs_devices->total_rw_bytes = 0;
677 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
678 fmode_t flags, void *holder)
682 mutex_lock(&uuid_mutex);
683 if (fs_devices->opened) {
684 fs_devices->opened++;
687 ret = __btrfs_open_devices(fs_devices, flags, holder);
689 mutex_unlock(&uuid_mutex);
693 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
694 struct btrfs_fs_devices **fs_devices_ret)
696 struct btrfs_super_block *disk_super;
697 struct block_device *bdev;
698 struct buffer_head *bh;
703 mutex_lock(&uuid_mutex);
706 bdev = blkdev_get_by_path(path, flags, holder);
713 ret = set_blocksize(bdev, 4096);
716 bh = btrfs_read_dev_super(bdev);
721 disk_super = (struct btrfs_super_block *)bh->b_data;
722 devid = btrfs_stack_device_id(&disk_super->dev_item);
723 transid = btrfs_super_generation(disk_super);
724 if (disk_super->label[0])
725 printk(KERN_INFO "device label %s ", disk_super->label);
727 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
728 printk(KERN_CONT "devid %llu transid %llu %s\n",
729 (unsigned long long)devid, (unsigned long long)transid, path);
730 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
734 blkdev_put(bdev, flags);
736 mutex_unlock(&uuid_mutex);
740 /* helper to account the used device space in the range */
741 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
742 u64 end, u64 *length)
744 struct btrfs_key key;
745 struct btrfs_root *root = device->dev_root;
746 struct btrfs_dev_extent *dev_extent;
747 struct btrfs_path *path;
751 struct extent_buffer *l;
755 if (start >= device->total_bytes)
758 path = btrfs_alloc_path();
763 key.objectid = device->devid;
765 key.type = BTRFS_DEV_EXTENT_KEY;
767 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
771 ret = btrfs_previous_item(root, path, key.objectid, key.type);
778 slot = path->slots[0];
779 if (slot >= btrfs_header_nritems(l)) {
780 ret = btrfs_next_leaf(root, path);
788 btrfs_item_key_to_cpu(l, &key, slot);
790 if (key.objectid < device->devid)
793 if (key.objectid > device->devid)
796 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
799 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
800 extent_end = key.offset + btrfs_dev_extent_length(l,
802 if (key.offset <= start && extent_end > end) {
803 *length = end - start + 1;
805 } else if (key.offset <= start && extent_end > start)
806 *length += extent_end - start;
807 else if (key.offset > start && extent_end <= end)
808 *length += extent_end - key.offset;
809 else if (key.offset > start && key.offset <= end) {
810 *length += end - key.offset + 1;
812 } else if (key.offset > end)
820 btrfs_free_path(path);
825 * find_free_dev_extent - find free space in the specified device
826 * @trans: transaction handler
827 * @device: the device which we search the free space in
828 * @num_bytes: the size of the free space that we need
829 * @start: store the start of the free space.
830 * @len: the size of the free space. that we find, or the size of the max
831 * free space if we don't find suitable free space
833 * this uses a pretty simple search, the expectation is that it is
834 * called very infrequently and that a given device has a small number
837 * @start is used to store the start of the free space if we find. But if we
838 * don't find suitable free space, it will be used to store the start position
839 * of the max free space.
841 * @len is used to store the size of the free space that we find.
842 * But if we don't find suitable free space, it is used to store the size of
843 * the max free space.
845 int find_free_dev_extent(struct btrfs_trans_handle *trans,
846 struct btrfs_device *device, u64 num_bytes,
847 u64 *start, u64 *len)
849 struct btrfs_key key;
850 struct btrfs_root *root = device->dev_root;
851 struct btrfs_dev_extent *dev_extent;
852 struct btrfs_path *path;
858 u64 search_end = device->total_bytes;
861 struct extent_buffer *l;
863 /* FIXME use last free of some kind */
865 /* we don't want to overwrite the superblock on the drive,
866 * so we make sure to start at an offset of at least 1MB
868 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
870 max_hole_start = search_start;
874 if (search_start >= search_end) {
879 path = btrfs_alloc_path();
886 key.objectid = device->devid;
887 key.offset = search_start;
888 key.type = BTRFS_DEV_EXTENT_KEY;
890 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
894 ret = btrfs_previous_item(root, path, key.objectid, key.type);
901 slot = path->slots[0];
902 if (slot >= btrfs_header_nritems(l)) {
903 ret = btrfs_next_leaf(root, path);
911 btrfs_item_key_to_cpu(l, &key, slot);
913 if (key.objectid < device->devid)
916 if (key.objectid > device->devid)
919 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
922 if (key.offset > search_start) {
923 hole_size = key.offset - search_start;
925 if (hole_size > max_hole_size) {
926 max_hole_start = search_start;
927 max_hole_size = hole_size;
931 * If this free space is greater than which we need,
932 * it must be the max free space that we have found
933 * until now, so max_hole_start must point to the start
934 * of this free space and the length of this free space
935 * is stored in max_hole_size. Thus, we return
936 * max_hole_start and max_hole_size and go back to the
939 if (hole_size >= num_bytes) {
945 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
946 extent_end = key.offset + btrfs_dev_extent_length(l,
948 if (extent_end > search_start)
949 search_start = extent_end;
956 * At this point, search_start should be the end of
957 * allocated dev extents, and when shrinking the device,
958 * search_end may be smaller than search_start.
960 if (search_end > search_start)
961 hole_size = search_end - search_start;
963 if (hole_size > max_hole_size) {
964 max_hole_start = search_start;
965 max_hole_size = hole_size;
969 if (hole_size < num_bytes)
975 btrfs_free_path(path);
977 *start = max_hole_start;
979 *len = max_hole_size;
983 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
984 struct btrfs_device *device,
988 struct btrfs_path *path;
989 struct btrfs_root *root = device->dev_root;
990 struct btrfs_key key;
991 struct btrfs_key found_key;
992 struct extent_buffer *leaf = NULL;
993 struct btrfs_dev_extent *extent = NULL;
995 path = btrfs_alloc_path();
999 key.objectid = device->devid;
1001 key.type = BTRFS_DEV_EXTENT_KEY;
1003 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1005 ret = btrfs_previous_item(root, path, key.objectid,
1006 BTRFS_DEV_EXTENT_KEY);
1009 leaf = path->nodes[0];
1010 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1011 extent = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_dev_extent);
1013 BUG_ON(found_key.offset > start || found_key.offset +
1014 btrfs_dev_extent_length(leaf, extent) < start);
1015 } else if (ret == 0) {
1016 leaf = path->nodes[0];
1017 extent = btrfs_item_ptr(leaf, path->slots[0],
1018 struct btrfs_dev_extent);
1022 if (device->bytes_used > 0) {
1023 u64 len = btrfs_dev_extent_length(leaf, extent);
1024 device->bytes_used -= len;
1025 spin_lock(&root->fs_info->free_chunk_lock);
1026 root->fs_info->free_chunk_space += len;
1027 spin_unlock(&root->fs_info->free_chunk_lock);
1029 ret = btrfs_del_item(trans, root, path);
1032 btrfs_free_path(path);
1036 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1037 struct btrfs_device *device,
1038 u64 chunk_tree, u64 chunk_objectid,
1039 u64 chunk_offset, u64 start, u64 num_bytes)
1042 struct btrfs_path *path;
1043 struct btrfs_root *root = device->dev_root;
1044 struct btrfs_dev_extent *extent;
1045 struct extent_buffer *leaf;
1046 struct btrfs_key key;
1048 WARN_ON(!device->in_fs_metadata);
1049 path = btrfs_alloc_path();
1053 key.objectid = device->devid;
1055 key.type = BTRFS_DEV_EXTENT_KEY;
1056 ret = btrfs_insert_empty_item(trans, root, path, &key,
1060 leaf = path->nodes[0];
1061 extent = btrfs_item_ptr(leaf, path->slots[0],
1062 struct btrfs_dev_extent);
1063 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1064 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1065 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1067 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1068 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1071 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1072 btrfs_mark_buffer_dirty(leaf);
1073 btrfs_free_path(path);
1077 static noinline int find_next_chunk(struct btrfs_root *root,
1078 u64 objectid, u64 *offset)
1080 struct btrfs_path *path;
1082 struct btrfs_key key;
1083 struct btrfs_chunk *chunk;
1084 struct btrfs_key found_key;
1086 path = btrfs_alloc_path();
1090 key.objectid = objectid;
1091 key.offset = (u64)-1;
1092 key.type = BTRFS_CHUNK_ITEM_KEY;
1094 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1100 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1104 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1106 if (found_key.objectid != objectid)
1109 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1110 struct btrfs_chunk);
1111 *offset = found_key.offset +
1112 btrfs_chunk_length(path->nodes[0], chunk);
1117 btrfs_free_path(path);
1121 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1124 struct btrfs_key key;
1125 struct btrfs_key found_key;
1126 struct btrfs_path *path;
1128 root = root->fs_info->chunk_root;
1130 path = btrfs_alloc_path();
1134 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1135 key.type = BTRFS_DEV_ITEM_KEY;
1136 key.offset = (u64)-1;
1138 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1144 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1145 BTRFS_DEV_ITEM_KEY);
1149 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1151 *objectid = found_key.offset + 1;
1155 btrfs_free_path(path);
1160 * the device information is stored in the chunk root
1161 * the btrfs_device struct should be fully filled in
1163 int btrfs_add_device(struct btrfs_trans_handle *trans,
1164 struct btrfs_root *root,
1165 struct btrfs_device *device)
1168 struct btrfs_path *path;
1169 struct btrfs_dev_item *dev_item;
1170 struct extent_buffer *leaf;
1171 struct btrfs_key key;
1174 root = root->fs_info->chunk_root;
1176 path = btrfs_alloc_path();
1180 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1181 key.type = BTRFS_DEV_ITEM_KEY;
1182 key.offset = device->devid;
1184 ret = btrfs_insert_empty_item(trans, root, path, &key,
1189 leaf = path->nodes[0];
1190 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1192 btrfs_set_device_id(leaf, dev_item, device->devid);
1193 btrfs_set_device_generation(leaf, dev_item, 0);
1194 btrfs_set_device_type(leaf, dev_item, device->type);
1195 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1196 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1197 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1198 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1199 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1200 btrfs_set_device_group(leaf, dev_item, 0);
1201 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1202 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1203 btrfs_set_device_start_offset(leaf, dev_item, 0);
1205 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1206 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1207 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1208 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1209 btrfs_mark_buffer_dirty(leaf);
1213 btrfs_free_path(path);
1217 static int btrfs_rm_dev_item(struct btrfs_root *root,
1218 struct btrfs_device *device)
1221 struct btrfs_path *path;
1222 struct btrfs_key key;
1223 struct btrfs_trans_handle *trans;
1225 root = root->fs_info->chunk_root;
1227 path = btrfs_alloc_path();
1231 trans = btrfs_start_transaction(root, 0);
1232 if (IS_ERR(trans)) {
1233 btrfs_free_path(path);
1234 return PTR_ERR(trans);
1236 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1237 key.type = BTRFS_DEV_ITEM_KEY;
1238 key.offset = device->devid;
1241 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1250 ret = btrfs_del_item(trans, root, path);
1254 btrfs_free_path(path);
1255 unlock_chunks(root);
1256 btrfs_commit_transaction(trans, root);
1260 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1262 struct btrfs_device *device;
1263 struct btrfs_device *next_device;
1264 struct block_device *bdev;
1265 struct buffer_head *bh = NULL;
1266 struct btrfs_super_block *disk_super;
1267 struct btrfs_fs_devices *cur_devices;
1273 bool clear_super = false;
1275 mutex_lock(&uuid_mutex);
1276 mutex_lock(&root->fs_info->volume_mutex);
1278 all_avail = root->fs_info->avail_data_alloc_bits |
1279 root->fs_info->avail_system_alloc_bits |
1280 root->fs_info->avail_metadata_alloc_bits;
1282 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1283 root->fs_info->fs_devices->num_devices <= 4) {
1284 printk(KERN_ERR "btrfs: unable to go below four devices "
1290 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1291 root->fs_info->fs_devices->num_devices <= 2) {
1292 printk(KERN_ERR "btrfs: unable to go below two "
1293 "devices on raid1\n");
1298 if (strcmp(device_path, "missing") == 0) {
1299 struct list_head *devices;
1300 struct btrfs_device *tmp;
1303 devices = &root->fs_info->fs_devices->devices;
1305 * It is safe to read the devices since the volume_mutex
1308 list_for_each_entry(tmp, devices, dev_list) {
1309 if (tmp->in_fs_metadata && !tmp->bdev) {
1318 printk(KERN_ERR "btrfs: no missing devices found to "
1323 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1324 root->fs_info->bdev_holder);
1326 ret = PTR_ERR(bdev);
1330 set_blocksize(bdev, 4096);
1331 bh = btrfs_read_dev_super(bdev);
1336 disk_super = (struct btrfs_super_block *)bh->b_data;
1337 devid = btrfs_stack_device_id(&disk_super->dev_item);
1338 dev_uuid = disk_super->dev_item.uuid;
1339 device = btrfs_find_device(root, devid, dev_uuid,
1347 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1348 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1354 if (device->writeable) {
1356 list_del_init(&device->dev_alloc_list);
1357 unlock_chunks(root);
1358 root->fs_info->fs_devices->rw_devices--;
1362 ret = btrfs_shrink_device(device, 0);
1366 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1370 spin_lock(&root->fs_info->free_chunk_lock);
1371 root->fs_info->free_chunk_space = device->total_bytes -
1373 spin_unlock(&root->fs_info->free_chunk_lock);
1375 device->in_fs_metadata = 0;
1376 btrfs_scrub_cancel_dev(root, device);
1379 * the device list mutex makes sure that we don't change
1380 * the device list while someone else is writing out all
1381 * the device supers.
1384 cur_devices = device->fs_devices;
1385 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1386 list_del_rcu(&device->dev_list);
1388 device->fs_devices->num_devices--;
1390 if (device->missing)
1391 root->fs_info->fs_devices->missing_devices--;
1393 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1394 struct btrfs_device, dev_list);
1395 if (device->bdev == root->fs_info->sb->s_bdev)
1396 root->fs_info->sb->s_bdev = next_device->bdev;
1397 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1398 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1401 device->fs_devices->open_devices--;
1403 call_rcu(&device->rcu, free_device);
1404 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1406 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1407 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1409 if (cur_devices->open_devices == 0) {
1410 struct btrfs_fs_devices *fs_devices;
1411 fs_devices = root->fs_info->fs_devices;
1412 while (fs_devices) {
1413 if (fs_devices->seed == cur_devices)
1415 fs_devices = fs_devices->seed;
1417 fs_devices->seed = cur_devices->seed;
1418 cur_devices->seed = NULL;
1420 __btrfs_close_devices(cur_devices);
1421 unlock_chunks(root);
1422 free_fs_devices(cur_devices);
1426 * at this point, the device is zero sized. We want to
1427 * remove it from the devices list and zero out the old super
1430 /* make sure this device isn't detected as part of
1433 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1434 set_buffer_dirty(bh);
1435 sync_dirty_buffer(bh);
1444 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1446 mutex_unlock(&root->fs_info->volume_mutex);
1447 mutex_unlock(&uuid_mutex);
1450 if (device->writeable) {
1452 list_add(&device->dev_alloc_list,
1453 &root->fs_info->fs_devices->alloc_list);
1454 unlock_chunks(root);
1455 root->fs_info->fs_devices->rw_devices++;
1461 * does all the dirty work required for changing file system's UUID.
1463 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1464 struct btrfs_root *root)
1466 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1467 struct btrfs_fs_devices *old_devices;
1468 struct btrfs_fs_devices *seed_devices;
1469 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1470 struct btrfs_device *device;
1473 BUG_ON(!mutex_is_locked(&uuid_mutex));
1474 if (!fs_devices->seeding)
1477 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1481 old_devices = clone_fs_devices(fs_devices);
1482 if (IS_ERR(old_devices)) {
1483 kfree(seed_devices);
1484 return PTR_ERR(old_devices);
1487 list_add(&old_devices->list, &fs_uuids);
1489 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1490 seed_devices->opened = 1;
1491 INIT_LIST_HEAD(&seed_devices->devices);
1492 INIT_LIST_HEAD(&seed_devices->alloc_list);
1493 mutex_init(&seed_devices->device_list_mutex);
1495 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1496 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1498 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1500 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1501 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1502 device->fs_devices = seed_devices;
1505 fs_devices->seeding = 0;
1506 fs_devices->num_devices = 0;
1507 fs_devices->open_devices = 0;
1508 fs_devices->seed = seed_devices;
1510 generate_random_uuid(fs_devices->fsid);
1511 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1512 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1513 super_flags = btrfs_super_flags(disk_super) &
1514 ~BTRFS_SUPER_FLAG_SEEDING;
1515 btrfs_set_super_flags(disk_super, super_flags);
1521 * strore the expected generation for seed devices in device items.
1523 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1524 struct btrfs_root *root)
1526 struct btrfs_path *path;
1527 struct extent_buffer *leaf;
1528 struct btrfs_dev_item *dev_item;
1529 struct btrfs_device *device;
1530 struct btrfs_key key;
1531 u8 fs_uuid[BTRFS_UUID_SIZE];
1532 u8 dev_uuid[BTRFS_UUID_SIZE];
1536 path = btrfs_alloc_path();
1540 root = root->fs_info->chunk_root;
1541 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1543 key.type = BTRFS_DEV_ITEM_KEY;
1546 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1550 leaf = path->nodes[0];
1552 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1553 ret = btrfs_next_leaf(root, path);
1558 leaf = path->nodes[0];
1559 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1560 btrfs_release_path(path);
1564 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1565 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1566 key.type != BTRFS_DEV_ITEM_KEY)
1569 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1570 struct btrfs_dev_item);
1571 devid = btrfs_device_id(leaf, dev_item);
1572 read_extent_buffer(leaf, dev_uuid,
1573 (unsigned long)btrfs_device_uuid(dev_item),
1575 read_extent_buffer(leaf, fs_uuid,
1576 (unsigned long)btrfs_device_fsid(dev_item),
1578 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1581 if (device->fs_devices->seeding) {
1582 btrfs_set_device_generation(leaf, dev_item,
1583 device->generation);
1584 btrfs_mark_buffer_dirty(leaf);
1592 btrfs_free_path(path);
1596 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1598 struct request_queue *q;
1599 struct btrfs_trans_handle *trans;
1600 struct btrfs_device *device;
1601 struct block_device *bdev;
1602 struct list_head *devices;
1603 struct super_block *sb = root->fs_info->sb;
1605 int seeding_dev = 0;
1608 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1611 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1612 root->fs_info->bdev_holder);
1614 return PTR_ERR(bdev);
1616 if (root->fs_info->fs_devices->seeding) {
1618 down_write(&sb->s_umount);
1619 mutex_lock(&uuid_mutex);
1622 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1623 mutex_lock(&root->fs_info->volume_mutex);
1625 devices = &root->fs_info->fs_devices->devices;
1627 * we have the volume lock, so we don't need the extra
1628 * device list mutex while reading the list here.
1630 list_for_each_entry(device, devices, dev_list) {
1631 if (device->bdev == bdev) {
1637 device = kzalloc(sizeof(*device), GFP_NOFS);
1639 /* we can safely leave the fs_devices entry around */
1644 device->name = kstrdup(device_path, GFP_NOFS);
1645 if (!device->name) {
1651 ret = find_next_devid(root, &device->devid);
1653 kfree(device->name);
1658 trans = btrfs_start_transaction(root, 0);
1659 if (IS_ERR(trans)) {
1660 kfree(device->name);
1662 ret = PTR_ERR(trans);
1668 q = bdev_get_queue(bdev);
1669 if (blk_queue_discard(q))
1670 device->can_discard = 1;
1671 device->writeable = 1;
1672 device->work.func = pending_bios_fn;
1673 generate_random_uuid(device->uuid);
1674 spin_lock_init(&device->io_lock);
1675 device->generation = trans->transid;
1676 device->io_width = root->sectorsize;
1677 device->io_align = root->sectorsize;
1678 device->sector_size = root->sectorsize;
1679 device->total_bytes = i_size_read(bdev->bd_inode);
1680 device->disk_total_bytes = device->total_bytes;
1681 device->dev_root = root->fs_info->dev_root;
1682 device->bdev = bdev;
1683 device->in_fs_metadata = 1;
1684 device->mode = FMODE_EXCL;
1685 set_blocksize(device->bdev, 4096);
1688 sb->s_flags &= ~MS_RDONLY;
1689 ret = btrfs_prepare_sprout(trans, root);
1693 device->fs_devices = root->fs_info->fs_devices;
1696 * we don't want write_supers to jump in here with our device
1699 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1700 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
1701 list_add(&device->dev_alloc_list,
1702 &root->fs_info->fs_devices->alloc_list);
1703 root->fs_info->fs_devices->num_devices++;
1704 root->fs_info->fs_devices->open_devices++;
1705 root->fs_info->fs_devices->rw_devices++;
1706 if (device->can_discard)
1707 root->fs_info->fs_devices->num_can_discard++;
1708 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1710 spin_lock(&root->fs_info->free_chunk_lock);
1711 root->fs_info->free_chunk_space += device->total_bytes;
1712 spin_unlock(&root->fs_info->free_chunk_lock);
1714 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1715 root->fs_info->fs_devices->rotating = 1;
1717 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1718 btrfs_set_super_total_bytes(root->fs_info->super_copy,
1719 total_bytes + device->total_bytes);
1721 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1722 btrfs_set_super_num_devices(root->fs_info->super_copy,
1724 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1727 ret = init_first_rw_device(trans, root, device);
1729 ret = btrfs_finish_sprout(trans, root);
1732 ret = btrfs_add_device(trans, root, device);
1736 * we've got more storage, clear any full flags on the space
1739 btrfs_clear_space_info_full(root->fs_info);
1741 unlock_chunks(root);
1742 btrfs_commit_transaction(trans, root);
1745 mutex_unlock(&uuid_mutex);
1746 up_write(&sb->s_umount);
1748 ret = btrfs_relocate_sys_chunks(root);
1752 mutex_unlock(&root->fs_info->volume_mutex);
1755 blkdev_put(bdev, FMODE_EXCL);
1757 mutex_unlock(&uuid_mutex);
1758 up_write(&sb->s_umount);
1763 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1764 struct btrfs_device *device)
1767 struct btrfs_path *path;
1768 struct btrfs_root *root;
1769 struct btrfs_dev_item *dev_item;
1770 struct extent_buffer *leaf;
1771 struct btrfs_key key;
1773 root = device->dev_root->fs_info->chunk_root;
1775 path = btrfs_alloc_path();
1779 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1780 key.type = BTRFS_DEV_ITEM_KEY;
1781 key.offset = device->devid;
1783 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1792 leaf = path->nodes[0];
1793 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1795 btrfs_set_device_id(leaf, dev_item, device->devid);
1796 btrfs_set_device_type(leaf, dev_item, device->type);
1797 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1798 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1799 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1800 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1801 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1802 btrfs_mark_buffer_dirty(leaf);
1805 btrfs_free_path(path);
1809 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1810 struct btrfs_device *device, u64 new_size)
1812 struct btrfs_super_block *super_copy =
1813 device->dev_root->fs_info->super_copy;
1814 u64 old_total = btrfs_super_total_bytes(super_copy);
1815 u64 diff = new_size - device->total_bytes;
1817 if (!device->writeable)
1819 if (new_size <= device->total_bytes)
1822 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1823 device->fs_devices->total_rw_bytes += diff;
1825 device->total_bytes = new_size;
1826 device->disk_total_bytes = new_size;
1827 btrfs_clear_space_info_full(device->dev_root->fs_info);
1829 return btrfs_update_device(trans, device);
1832 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1833 struct btrfs_device *device, u64 new_size)
1836 lock_chunks(device->dev_root);
1837 ret = __btrfs_grow_device(trans, device, new_size);
1838 unlock_chunks(device->dev_root);
1842 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1843 struct btrfs_root *root,
1844 u64 chunk_tree, u64 chunk_objectid,
1848 struct btrfs_path *path;
1849 struct btrfs_key key;
1851 root = root->fs_info->chunk_root;
1852 path = btrfs_alloc_path();
1856 key.objectid = chunk_objectid;
1857 key.offset = chunk_offset;
1858 key.type = BTRFS_CHUNK_ITEM_KEY;
1860 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1863 ret = btrfs_del_item(trans, root, path);
1865 btrfs_free_path(path);
1869 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1872 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1873 struct btrfs_disk_key *disk_key;
1874 struct btrfs_chunk *chunk;
1881 struct btrfs_key key;
1883 array_size = btrfs_super_sys_array_size(super_copy);
1885 ptr = super_copy->sys_chunk_array;
1888 while (cur < array_size) {
1889 disk_key = (struct btrfs_disk_key *)ptr;
1890 btrfs_disk_key_to_cpu(&key, disk_key);
1892 len = sizeof(*disk_key);
1894 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1895 chunk = (struct btrfs_chunk *)(ptr + len);
1896 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1897 len += btrfs_chunk_item_size(num_stripes);
1902 if (key.objectid == chunk_objectid &&
1903 key.offset == chunk_offset) {
1904 memmove(ptr, ptr + len, array_size - (cur + len));
1906 btrfs_set_super_sys_array_size(super_copy, array_size);
1915 static int btrfs_relocate_chunk(struct btrfs_root *root,
1916 u64 chunk_tree, u64 chunk_objectid,
1919 struct extent_map_tree *em_tree;
1920 struct btrfs_root *extent_root;
1921 struct btrfs_trans_handle *trans;
1922 struct extent_map *em;
1923 struct map_lookup *map;
1927 root = root->fs_info->chunk_root;
1928 extent_root = root->fs_info->extent_root;
1929 em_tree = &root->fs_info->mapping_tree.map_tree;
1931 ret = btrfs_can_relocate(extent_root, chunk_offset);
1935 /* step one, relocate all the extents inside this chunk */
1936 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1940 trans = btrfs_start_transaction(root, 0);
1941 BUG_ON(IS_ERR(trans));
1946 * step two, delete the device extents and the
1947 * chunk tree entries
1949 read_lock(&em_tree->lock);
1950 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1951 read_unlock(&em_tree->lock);
1953 BUG_ON(em->start > chunk_offset ||
1954 em->start + em->len < chunk_offset);
1955 map = (struct map_lookup *)em->bdev;
1957 for (i = 0; i < map->num_stripes; i++) {
1958 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1959 map->stripes[i].physical);
1962 if (map->stripes[i].dev) {
1963 ret = btrfs_update_device(trans, map->stripes[i].dev);
1967 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1972 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1974 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1975 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1979 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1982 write_lock(&em_tree->lock);
1983 remove_extent_mapping(em_tree, em);
1984 write_unlock(&em_tree->lock);
1989 /* once for the tree */
1990 free_extent_map(em);
1992 free_extent_map(em);
1994 unlock_chunks(root);
1995 btrfs_end_transaction(trans, root);
1999 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2001 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2002 struct btrfs_path *path;
2003 struct extent_buffer *leaf;
2004 struct btrfs_chunk *chunk;
2005 struct btrfs_key key;
2006 struct btrfs_key found_key;
2007 u64 chunk_tree = chunk_root->root_key.objectid;
2009 bool retried = false;
2013 path = btrfs_alloc_path();
2018 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2019 key.offset = (u64)-1;
2020 key.type = BTRFS_CHUNK_ITEM_KEY;
2023 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2028 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2035 leaf = path->nodes[0];
2036 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2038 chunk = btrfs_item_ptr(leaf, path->slots[0],
2039 struct btrfs_chunk);
2040 chunk_type = btrfs_chunk_type(leaf, chunk);
2041 btrfs_release_path(path);
2043 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2044 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2053 if (found_key.offset == 0)
2055 key.offset = found_key.offset - 1;
2058 if (failed && !retried) {
2062 } else if (failed && retried) {
2067 btrfs_free_path(path);
2071 static u64 div_factor(u64 num, int factor)
2080 int btrfs_balance(struct btrfs_root *dev_root)
2083 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2084 struct btrfs_device *device;
2087 struct btrfs_path *path;
2088 struct btrfs_key key;
2089 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2090 struct btrfs_trans_handle *trans;
2091 struct btrfs_key found_key;
2093 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2096 if (!capable(CAP_SYS_ADMIN))
2099 mutex_lock(&dev_root->fs_info->volume_mutex);
2100 dev_root = dev_root->fs_info->dev_root;
2102 /* step one make some room on all the devices */
2103 list_for_each_entry(device, devices, dev_list) {
2104 old_size = device->total_bytes;
2105 size_to_free = div_factor(old_size, 1);
2106 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2107 if (!device->writeable ||
2108 device->total_bytes - device->bytes_used > size_to_free)
2111 ret = btrfs_shrink_device(device, old_size - size_to_free);
2116 trans = btrfs_start_transaction(dev_root, 0);
2117 BUG_ON(IS_ERR(trans));
2119 ret = btrfs_grow_device(trans, device, old_size);
2122 btrfs_end_transaction(trans, dev_root);
2125 /* step two, relocate all the chunks */
2126 path = btrfs_alloc_path();
2131 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2132 key.offset = (u64)-1;
2133 key.type = BTRFS_CHUNK_ITEM_KEY;
2136 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2141 * this shouldn't happen, it means the last relocate
2147 ret = btrfs_previous_item(chunk_root, path, 0,
2148 BTRFS_CHUNK_ITEM_KEY);
2152 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2154 if (found_key.objectid != key.objectid)
2157 /* chunk zero is special */
2158 if (found_key.offset == 0)
2161 btrfs_release_path(path);
2162 ret = btrfs_relocate_chunk(chunk_root,
2163 chunk_root->root_key.objectid,
2166 if (ret && ret != -ENOSPC)
2168 key.offset = found_key.offset - 1;
2172 btrfs_free_path(path);
2173 mutex_unlock(&dev_root->fs_info->volume_mutex);
2178 * shrinking a device means finding all of the device extents past
2179 * the new size, and then following the back refs to the chunks.
2180 * The chunk relocation code actually frees the device extent
2182 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2184 struct btrfs_trans_handle *trans;
2185 struct btrfs_root *root = device->dev_root;
2186 struct btrfs_dev_extent *dev_extent = NULL;
2187 struct btrfs_path *path;
2195 bool retried = false;
2196 struct extent_buffer *l;
2197 struct btrfs_key key;
2198 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2199 u64 old_total = btrfs_super_total_bytes(super_copy);
2200 u64 old_size = device->total_bytes;
2201 u64 diff = device->total_bytes - new_size;
2203 if (new_size >= device->total_bytes)
2206 path = btrfs_alloc_path();
2214 device->total_bytes = new_size;
2215 if (device->writeable) {
2216 device->fs_devices->total_rw_bytes -= diff;
2217 spin_lock(&root->fs_info->free_chunk_lock);
2218 root->fs_info->free_chunk_space -= diff;
2219 spin_unlock(&root->fs_info->free_chunk_lock);
2221 unlock_chunks(root);
2224 key.objectid = device->devid;
2225 key.offset = (u64)-1;
2226 key.type = BTRFS_DEV_EXTENT_KEY;
2229 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2233 ret = btrfs_previous_item(root, path, 0, key.type);
2238 btrfs_release_path(path);
2243 slot = path->slots[0];
2244 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2246 if (key.objectid != device->devid) {
2247 btrfs_release_path(path);
2251 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2252 length = btrfs_dev_extent_length(l, dev_extent);
2254 if (key.offset + length <= new_size) {
2255 btrfs_release_path(path);
2259 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2260 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2261 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2262 btrfs_release_path(path);
2264 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2266 if (ret && ret != -ENOSPC)
2273 if (failed && !retried) {
2277 } else if (failed && retried) {
2281 device->total_bytes = old_size;
2282 if (device->writeable)
2283 device->fs_devices->total_rw_bytes += diff;
2284 spin_lock(&root->fs_info->free_chunk_lock);
2285 root->fs_info->free_chunk_space += diff;
2286 spin_unlock(&root->fs_info->free_chunk_lock);
2287 unlock_chunks(root);
2291 /* Shrinking succeeded, else we would be at "done". */
2292 trans = btrfs_start_transaction(root, 0);
2293 if (IS_ERR(trans)) {
2294 ret = PTR_ERR(trans);
2300 device->disk_total_bytes = new_size;
2301 /* Now btrfs_update_device() will change the on-disk size. */
2302 ret = btrfs_update_device(trans, device);
2304 unlock_chunks(root);
2305 btrfs_end_transaction(trans, root);
2308 WARN_ON(diff > old_total);
2309 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2310 unlock_chunks(root);
2311 btrfs_end_transaction(trans, root);
2313 btrfs_free_path(path);
2317 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2318 struct btrfs_root *root,
2319 struct btrfs_key *key,
2320 struct btrfs_chunk *chunk, int item_size)
2322 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2323 struct btrfs_disk_key disk_key;
2327 array_size = btrfs_super_sys_array_size(super_copy);
2328 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2331 ptr = super_copy->sys_chunk_array + array_size;
2332 btrfs_cpu_key_to_disk(&disk_key, key);
2333 memcpy(ptr, &disk_key, sizeof(disk_key));
2334 ptr += sizeof(disk_key);
2335 memcpy(ptr, chunk, item_size);
2336 item_size += sizeof(disk_key);
2337 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2342 * sort the devices in descending order by max_avail, total_avail
2344 static int btrfs_cmp_device_info(const void *a, const void *b)
2346 const struct btrfs_device_info *di_a = a;
2347 const struct btrfs_device_info *di_b = b;
2349 if (di_a->max_avail > di_b->max_avail)
2351 if (di_a->max_avail < di_b->max_avail)
2353 if (di_a->total_avail > di_b->total_avail)
2355 if (di_a->total_avail < di_b->total_avail)
2360 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2361 struct btrfs_root *extent_root,
2362 struct map_lookup **map_ret,
2363 u64 *num_bytes_out, u64 *stripe_size_out,
2364 u64 start, u64 type)
2366 struct btrfs_fs_info *info = extent_root->fs_info;
2367 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2368 struct list_head *cur;
2369 struct map_lookup *map = NULL;
2370 struct extent_map_tree *em_tree;
2371 struct extent_map *em;
2372 struct btrfs_device_info *devices_info = NULL;
2374 int num_stripes; /* total number of stripes to allocate */
2375 int sub_stripes; /* sub_stripes info for map */
2376 int dev_stripes; /* stripes per dev */
2377 int devs_max; /* max devs to use */
2378 int devs_min; /* min devs needed */
2379 int devs_increment; /* ndevs has to be a multiple of this */
2380 int ncopies; /* how many copies to data has */
2382 u64 max_stripe_size;
2390 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2391 (type & BTRFS_BLOCK_GROUP_DUP)) {
2393 type &= ~BTRFS_BLOCK_GROUP_DUP;
2396 if (list_empty(&fs_devices->alloc_list))
2403 devs_max = 0; /* 0 == as many as possible */
2407 * define the properties of each RAID type.
2408 * FIXME: move this to a global table and use it in all RAID
2411 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2415 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2417 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2422 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2431 if (type & BTRFS_BLOCK_GROUP_DATA) {
2432 max_stripe_size = 1024 * 1024 * 1024;
2433 max_chunk_size = 10 * max_stripe_size;
2434 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2435 max_stripe_size = 256 * 1024 * 1024;
2436 max_chunk_size = max_stripe_size;
2437 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2438 max_stripe_size = 8 * 1024 * 1024;
2439 max_chunk_size = 2 * max_stripe_size;
2441 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2446 /* we don't want a chunk larger than 10% of writeable space */
2447 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2450 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2455 cur = fs_devices->alloc_list.next;
2458 * in the first pass through the devices list, we gather information
2459 * about the available holes on each device.
2462 while (cur != &fs_devices->alloc_list) {
2463 struct btrfs_device *device;
2467 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2471 if (!device->writeable) {
2473 "btrfs: read-only device in alloc_list\n");
2478 if (!device->in_fs_metadata)
2481 if (device->total_bytes > device->bytes_used)
2482 total_avail = device->total_bytes - device->bytes_used;
2486 /* If there is no space on this device, skip it. */
2487 if (total_avail == 0)
2490 ret = find_free_dev_extent(trans, device,
2491 max_stripe_size * dev_stripes,
2492 &dev_offset, &max_avail);
2493 if (ret && ret != -ENOSPC)
2497 max_avail = max_stripe_size * dev_stripes;
2499 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2502 devices_info[ndevs].dev_offset = dev_offset;
2503 devices_info[ndevs].max_avail = max_avail;
2504 devices_info[ndevs].total_avail = total_avail;
2505 devices_info[ndevs].dev = device;
2510 * now sort the devices by hole size / available space
2512 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2513 btrfs_cmp_device_info, NULL);
2515 /* round down to number of usable stripes */
2516 ndevs -= ndevs % devs_increment;
2518 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2523 if (devs_max && ndevs > devs_max)
2526 * the primary goal is to maximize the number of stripes, so use as many
2527 * devices as possible, even if the stripes are not maximum sized.
2529 stripe_size = devices_info[ndevs-1].max_avail;
2530 num_stripes = ndevs * dev_stripes;
2532 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2533 stripe_size = max_chunk_size * ncopies;
2534 do_div(stripe_size, num_stripes);
2537 do_div(stripe_size, dev_stripes);
2538 do_div(stripe_size, BTRFS_STRIPE_LEN);
2539 stripe_size *= BTRFS_STRIPE_LEN;
2541 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2546 map->num_stripes = num_stripes;
2548 for (i = 0; i < ndevs; ++i) {
2549 for (j = 0; j < dev_stripes; ++j) {
2550 int s = i * dev_stripes + j;
2551 map->stripes[s].dev = devices_info[i].dev;
2552 map->stripes[s].physical = devices_info[i].dev_offset +
2556 map->sector_size = extent_root->sectorsize;
2557 map->stripe_len = BTRFS_STRIPE_LEN;
2558 map->io_align = BTRFS_STRIPE_LEN;
2559 map->io_width = BTRFS_STRIPE_LEN;
2561 map->sub_stripes = sub_stripes;
2564 num_bytes = stripe_size * (num_stripes / ncopies);
2566 *stripe_size_out = stripe_size;
2567 *num_bytes_out = num_bytes;
2569 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
2571 em = alloc_extent_map();
2576 em->bdev = (struct block_device *)map;
2578 em->len = num_bytes;
2579 em->block_start = 0;
2580 em->block_len = em->len;
2582 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2583 write_lock(&em_tree->lock);
2584 ret = add_extent_mapping(em_tree, em);
2585 write_unlock(&em_tree->lock);
2587 free_extent_map(em);
2589 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2590 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2594 for (i = 0; i < map->num_stripes; ++i) {
2595 struct btrfs_device *device;
2598 device = map->stripes[i].dev;
2599 dev_offset = map->stripes[i].physical;
2601 ret = btrfs_alloc_dev_extent(trans, device,
2602 info->chunk_root->root_key.objectid,
2603 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2604 start, dev_offset, stripe_size);
2608 kfree(devices_info);
2613 kfree(devices_info);
2617 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2618 struct btrfs_root *extent_root,
2619 struct map_lookup *map, u64 chunk_offset,
2620 u64 chunk_size, u64 stripe_size)
2623 struct btrfs_key key;
2624 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2625 struct btrfs_device *device;
2626 struct btrfs_chunk *chunk;
2627 struct btrfs_stripe *stripe;
2628 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2632 chunk = kzalloc(item_size, GFP_NOFS);
2637 while (index < map->num_stripes) {
2638 device = map->stripes[index].dev;
2639 device->bytes_used += stripe_size;
2640 ret = btrfs_update_device(trans, device);
2645 spin_lock(&extent_root->fs_info->free_chunk_lock);
2646 extent_root->fs_info->free_chunk_space -= (stripe_size *
2648 spin_unlock(&extent_root->fs_info->free_chunk_lock);
2651 stripe = &chunk->stripe;
2652 while (index < map->num_stripes) {
2653 device = map->stripes[index].dev;
2654 dev_offset = map->stripes[index].physical;
2656 btrfs_set_stack_stripe_devid(stripe, device->devid);
2657 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2658 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2663 btrfs_set_stack_chunk_length(chunk, chunk_size);
2664 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2665 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2666 btrfs_set_stack_chunk_type(chunk, map->type);
2667 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2668 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2669 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2670 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2671 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2673 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2674 key.type = BTRFS_CHUNK_ITEM_KEY;
2675 key.offset = chunk_offset;
2677 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2680 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2681 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2691 * Chunk allocation falls into two parts. The first part does works
2692 * that make the new allocated chunk useable, but not do any operation
2693 * that modifies the chunk tree. The second part does the works that
2694 * require modifying the chunk tree. This division is important for the
2695 * bootstrap process of adding storage to a seed btrfs.
2697 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2698 struct btrfs_root *extent_root, u64 type)
2703 struct map_lookup *map;
2704 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2707 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2712 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2713 &stripe_size, chunk_offset, type);
2717 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2718 chunk_size, stripe_size);
2723 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2724 struct btrfs_root *root,
2725 struct btrfs_device *device)
2728 u64 sys_chunk_offset;
2732 u64 sys_stripe_size;
2734 struct map_lookup *map;
2735 struct map_lookup *sys_map;
2736 struct btrfs_fs_info *fs_info = root->fs_info;
2737 struct btrfs_root *extent_root = fs_info->extent_root;
2740 ret = find_next_chunk(fs_info->chunk_root,
2741 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2745 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2746 (fs_info->metadata_alloc_profile &
2747 fs_info->avail_metadata_alloc_bits);
2748 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2750 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2751 &stripe_size, chunk_offset, alloc_profile);
2754 sys_chunk_offset = chunk_offset + chunk_size;
2756 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2757 (fs_info->system_alloc_profile &
2758 fs_info->avail_system_alloc_bits);
2759 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2761 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2762 &sys_chunk_size, &sys_stripe_size,
2763 sys_chunk_offset, alloc_profile);
2766 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2770 * Modifying chunk tree needs allocating new blocks from both
2771 * system block group and metadata block group. So we only can
2772 * do operations require modifying the chunk tree after both
2773 * block groups were created.
2775 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2776 chunk_size, stripe_size);
2779 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2780 sys_chunk_offset, sys_chunk_size,
2786 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2788 struct extent_map *em;
2789 struct map_lookup *map;
2790 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2794 read_lock(&map_tree->map_tree.lock);
2795 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2796 read_unlock(&map_tree->map_tree.lock);
2800 if (btrfs_test_opt(root, DEGRADED)) {
2801 free_extent_map(em);
2805 map = (struct map_lookup *)em->bdev;
2806 for (i = 0; i < map->num_stripes; i++) {
2807 if (!map->stripes[i].dev->writeable) {
2812 free_extent_map(em);
2816 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2818 extent_map_tree_init(&tree->map_tree);
2821 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2823 struct extent_map *em;
2826 write_lock(&tree->map_tree.lock);
2827 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2829 remove_extent_mapping(&tree->map_tree, em);
2830 write_unlock(&tree->map_tree.lock);
2835 free_extent_map(em);
2836 /* once for the tree */
2837 free_extent_map(em);
2841 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2843 struct extent_map *em;
2844 struct map_lookup *map;
2845 struct extent_map_tree *em_tree = &map_tree->map_tree;
2848 read_lock(&em_tree->lock);
2849 em = lookup_extent_mapping(em_tree, logical, len);
2850 read_unlock(&em_tree->lock);
2853 BUG_ON(em->start > logical || em->start + em->len < logical);
2854 map = (struct map_lookup *)em->bdev;
2855 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2856 ret = map->num_stripes;
2857 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2858 ret = map->sub_stripes;
2861 free_extent_map(em);
2865 static int find_live_mirror(struct map_lookup *map, int first, int num,
2869 if (map->stripes[optimal].dev->bdev)
2871 for (i = first; i < first + num; i++) {
2872 if (map->stripes[i].dev->bdev)
2875 /* we couldn't find one that doesn't fail. Just return something
2876 * and the io error handling code will clean up eventually
2881 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2882 u64 logical, u64 *length,
2883 struct btrfs_bio **bbio_ret,
2886 struct extent_map *em;
2887 struct map_lookup *map;
2888 struct extent_map_tree *em_tree = &map_tree->map_tree;
2891 u64 stripe_end_offset;
2895 int stripes_allocated = 8;
2896 int stripes_required = 1;
2901 struct btrfs_bio *bbio = NULL;
2903 if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
2904 stripes_allocated = 1;
2907 bbio = kzalloc(btrfs_bio_size(stripes_allocated),
2912 atomic_set(&bbio->error, 0);
2915 read_lock(&em_tree->lock);
2916 em = lookup_extent_mapping(em_tree, logical, *length);
2917 read_unlock(&em_tree->lock);
2920 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2921 (unsigned long long)logical,
2922 (unsigned long long)*length);
2926 BUG_ON(em->start > logical || em->start + em->len < logical);
2927 map = (struct map_lookup *)em->bdev;
2928 offset = logical - em->start;
2930 if (mirror_num > map->num_stripes)
2933 /* if our btrfs_bio struct is too small, back off and try again */
2934 if (rw & REQ_WRITE) {
2935 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2936 BTRFS_BLOCK_GROUP_DUP)) {
2937 stripes_required = map->num_stripes;
2939 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2940 stripes_required = map->sub_stripes;
2944 if (rw & REQ_DISCARD) {
2945 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2946 BTRFS_BLOCK_GROUP_RAID1 |
2947 BTRFS_BLOCK_GROUP_DUP |
2948 BTRFS_BLOCK_GROUP_RAID10)) {
2949 stripes_required = map->num_stripes;
2952 if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
2953 stripes_allocated < stripes_required) {
2954 stripes_allocated = map->num_stripes;
2955 free_extent_map(em);
2961 * stripe_nr counts the total number of stripes we have to stride
2962 * to get to this block
2964 do_div(stripe_nr, map->stripe_len);
2966 stripe_offset = stripe_nr * map->stripe_len;
2967 BUG_ON(offset < stripe_offset);
2969 /* stripe_offset is the offset of this block in its stripe*/
2970 stripe_offset = offset - stripe_offset;
2972 if (rw & REQ_DISCARD)
2973 *length = min_t(u64, em->len - offset, *length);
2974 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2975 BTRFS_BLOCK_GROUP_RAID1 |
2976 BTRFS_BLOCK_GROUP_RAID10 |
2977 BTRFS_BLOCK_GROUP_DUP)) {
2978 /* we limit the length of each bio to what fits in a stripe */
2979 *length = min_t(u64, em->len - offset,
2980 map->stripe_len - stripe_offset);
2982 *length = em->len - offset;
2990 stripe_nr_orig = stripe_nr;
2991 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2992 (~(map->stripe_len - 1));
2993 do_div(stripe_nr_end, map->stripe_len);
2994 stripe_end_offset = stripe_nr_end * map->stripe_len -
2996 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2997 if (rw & REQ_DISCARD)
2998 num_stripes = min_t(u64, map->num_stripes,
2999 stripe_nr_end - stripe_nr_orig);
3000 stripe_index = do_div(stripe_nr, map->num_stripes);
3001 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
3002 if (rw & (REQ_WRITE | REQ_DISCARD))
3003 num_stripes = map->num_stripes;
3004 else if (mirror_num)
3005 stripe_index = mirror_num - 1;
3007 stripe_index = find_live_mirror(map, 0,
3009 current->pid % map->num_stripes);
3010 mirror_num = stripe_index + 1;
3013 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3014 if (rw & (REQ_WRITE | REQ_DISCARD)) {
3015 num_stripes = map->num_stripes;
3016 } else if (mirror_num) {
3017 stripe_index = mirror_num - 1;
3022 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3023 int factor = map->num_stripes / map->sub_stripes;
3025 stripe_index = do_div(stripe_nr, factor);
3026 stripe_index *= map->sub_stripes;
3029 num_stripes = map->sub_stripes;
3030 else if (rw & REQ_DISCARD)
3031 num_stripes = min_t(u64, map->sub_stripes *
3032 (stripe_nr_end - stripe_nr_orig),
3034 else if (mirror_num)
3035 stripe_index += mirror_num - 1;
3037 stripe_index = find_live_mirror(map, stripe_index,
3038 map->sub_stripes, stripe_index +
3039 current->pid % map->sub_stripes);
3040 mirror_num = stripe_index + 1;
3044 * after this do_div call, stripe_nr is the number of stripes
3045 * on this device we have to walk to find the data, and
3046 * stripe_index is the number of our device in the stripe array
3048 stripe_index = do_div(stripe_nr, map->num_stripes);
3049 mirror_num = stripe_index + 1;
3051 BUG_ON(stripe_index >= map->num_stripes);
3053 if (rw & REQ_DISCARD) {
3054 for (i = 0; i < num_stripes; i++) {
3055 bbio->stripes[i].physical =
3056 map->stripes[stripe_index].physical +
3057 stripe_offset + stripe_nr * map->stripe_len;
3058 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
3060 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3062 u32 last_stripe = 0;
3065 div_u64_rem(stripe_nr_end - 1,
3069 for (j = 0; j < map->num_stripes; j++) {
3072 div_u64_rem(stripe_nr_end - 1 - j,
3073 map->num_stripes, &test);
3074 if (test == stripe_index)
3077 stripes = stripe_nr_end - 1 - j;
3078 do_div(stripes, map->num_stripes);
3079 bbio->stripes[i].length = map->stripe_len *
3080 (stripes - stripe_nr + 1);
3083 bbio->stripes[i].length -=
3087 if (stripe_index == last_stripe)
3088 bbio->stripes[i].length -=
3090 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3093 int factor = map->num_stripes /
3095 u32 last_stripe = 0;
3097 div_u64_rem(stripe_nr_end - 1,
3098 factor, &last_stripe);
3099 last_stripe *= map->sub_stripes;
3101 for (j = 0; j < factor; j++) {
3104 div_u64_rem(stripe_nr_end - 1 - j,
3108 stripe_index / map->sub_stripes)
3111 stripes = stripe_nr_end - 1 - j;
3112 do_div(stripes, factor);
3113 bbio->stripes[i].length = map->stripe_len *
3114 (stripes - stripe_nr + 1);
3116 if (i < map->sub_stripes) {
3117 bbio->stripes[i].length -=
3119 if (i == map->sub_stripes - 1)
3122 if (stripe_index >= last_stripe &&
3123 stripe_index <= (last_stripe +
3124 map->sub_stripes - 1)) {
3125 bbio->stripes[i].length -=
3129 bbio->stripes[i].length = *length;
3132 if (stripe_index == map->num_stripes) {
3133 /* This could only happen for RAID0/10 */
3139 for (i = 0; i < num_stripes; i++) {
3140 bbio->stripes[i].physical =
3141 map->stripes[stripe_index].physical +
3143 stripe_nr * map->stripe_len;
3144 bbio->stripes[i].dev =
3145 map->stripes[stripe_index].dev;
3151 bbio->num_stripes = num_stripes;
3152 bbio->max_errors = max_errors;
3153 bbio->mirror_num = mirror_num;
3156 free_extent_map(em);
3160 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3161 u64 logical, u64 *length,
3162 struct btrfs_bio **bbio_ret, int mirror_num)
3164 return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
3168 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3169 u64 chunk_start, u64 physical, u64 devid,
3170 u64 **logical, int *naddrs, int *stripe_len)
3172 struct extent_map_tree *em_tree = &map_tree->map_tree;
3173 struct extent_map *em;
3174 struct map_lookup *map;
3181 read_lock(&em_tree->lock);
3182 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3183 read_unlock(&em_tree->lock);
3185 BUG_ON(!em || em->start != chunk_start);
3186 map = (struct map_lookup *)em->bdev;
3189 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3190 do_div(length, map->num_stripes / map->sub_stripes);
3191 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3192 do_div(length, map->num_stripes);
3194 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3197 for (i = 0; i < map->num_stripes; i++) {
3198 if (devid && map->stripes[i].dev->devid != devid)
3200 if (map->stripes[i].physical > physical ||
3201 map->stripes[i].physical + length <= physical)
3204 stripe_nr = physical - map->stripes[i].physical;
3205 do_div(stripe_nr, map->stripe_len);
3207 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3208 stripe_nr = stripe_nr * map->num_stripes + i;
3209 do_div(stripe_nr, map->sub_stripes);
3210 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3211 stripe_nr = stripe_nr * map->num_stripes + i;
3213 bytenr = chunk_start + stripe_nr * map->stripe_len;
3214 WARN_ON(nr >= map->num_stripes);
3215 for (j = 0; j < nr; j++) {
3216 if (buf[j] == bytenr)
3220 WARN_ON(nr >= map->num_stripes);
3227 *stripe_len = map->stripe_len;
3229 free_extent_map(em);
3233 static void btrfs_end_bio(struct bio *bio, int err)
3235 struct btrfs_bio *bbio = bio->bi_private;
3236 int is_orig_bio = 0;
3239 atomic_inc(&bbio->error);
3241 if (bio == bbio->orig_bio)
3244 if (atomic_dec_and_test(&bbio->stripes_pending)) {
3247 bio = bbio->orig_bio;
3249 bio->bi_private = bbio->private;
3250 bio->bi_end_io = bbio->end_io;
3251 bio->bi_bdev = (struct block_device *)
3252 (unsigned long)bbio->mirror_num;
3253 /* only send an error to the higher layers if it is
3254 * beyond the tolerance of the multi-bio
3256 if (atomic_read(&bbio->error) > bbio->max_errors) {
3260 * this bio is actually up to date, we didn't
3261 * go over the max number of errors
3263 set_bit(BIO_UPTODATE, &bio->bi_flags);
3268 bio_endio(bio, err);
3269 } else if (!is_orig_bio) {
3274 struct async_sched {
3277 struct btrfs_fs_info *info;
3278 struct btrfs_work work;
3282 * see run_scheduled_bios for a description of why bios are collected for
3285 * This will add one bio to the pending list for a device and make sure
3286 * the work struct is scheduled.
3288 static noinline int schedule_bio(struct btrfs_root *root,
3289 struct btrfs_device *device,
3290 int rw, struct bio *bio)
3292 int should_queue = 1;
3293 struct btrfs_pending_bios *pending_bios;
3295 /* don't bother with additional async steps for reads, right now */
3296 if (!(rw & REQ_WRITE)) {
3298 submit_bio(rw, bio);
3304 * nr_async_bios allows us to reliably return congestion to the
3305 * higher layers. Otherwise, the async bio makes it appear we have
3306 * made progress against dirty pages when we've really just put it
3307 * on a queue for later
3309 atomic_inc(&root->fs_info->nr_async_bios);
3310 WARN_ON(bio->bi_next);
3311 bio->bi_next = NULL;
3314 spin_lock(&device->io_lock);
3315 if (bio->bi_rw & REQ_SYNC)
3316 pending_bios = &device->pending_sync_bios;
3318 pending_bios = &device->pending_bios;
3320 if (pending_bios->tail)
3321 pending_bios->tail->bi_next = bio;
3323 pending_bios->tail = bio;
3324 if (!pending_bios->head)
3325 pending_bios->head = bio;
3326 if (device->running_pending)
3329 spin_unlock(&device->io_lock);
3332 btrfs_queue_worker(&root->fs_info->submit_workers,
3337 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3338 int mirror_num, int async_submit)
3340 struct btrfs_mapping_tree *map_tree;
3341 struct btrfs_device *dev;
3342 struct bio *first_bio = bio;
3343 u64 logical = (u64)bio->bi_sector << 9;
3349 struct btrfs_bio *bbio = NULL;
3351 length = bio->bi_size;
3352 map_tree = &root->fs_info->mapping_tree;
3353 map_length = length;
3355 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
3359 total_devs = bbio->num_stripes;
3360 if (map_length < length) {
3361 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3362 "len %llu\n", (unsigned long long)logical,
3363 (unsigned long long)length,
3364 (unsigned long long)map_length);
3368 bbio->orig_bio = first_bio;
3369 bbio->private = first_bio->bi_private;
3370 bbio->end_io = first_bio->bi_end_io;
3371 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
3373 while (dev_nr < total_devs) {
3374 if (dev_nr < total_devs - 1) {
3375 bio = bio_clone(first_bio, GFP_NOFS);
3380 bio->bi_private = bbio;
3381 bio->bi_end_io = btrfs_end_bio;
3382 bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
3383 dev = bbio->stripes[dev_nr].dev;
3384 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3385 pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
3386 "(%s id %llu), size=%u\n", rw,
3387 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
3388 dev->name, dev->devid, bio->bi_size);
3389 bio->bi_bdev = dev->bdev;
3391 schedule_bio(root, dev, rw, bio);
3393 submit_bio(rw, bio);
3395 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3396 bio->bi_sector = logical >> 9;
3397 bio_endio(bio, -EIO);
3404 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3407 struct btrfs_device *device;
3408 struct btrfs_fs_devices *cur_devices;
3410 cur_devices = root->fs_info->fs_devices;
3411 while (cur_devices) {
3413 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3414 device = __find_device(&cur_devices->devices,
3419 cur_devices = cur_devices->seed;
3424 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3425 u64 devid, u8 *dev_uuid)
3427 struct btrfs_device *device;
3428 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3430 device = kzalloc(sizeof(*device), GFP_NOFS);
3433 list_add(&device->dev_list,
3434 &fs_devices->devices);
3435 device->dev_root = root->fs_info->dev_root;
3436 device->devid = devid;
3437 device->work.func = pending_bios_fn;
3438 device->fs_devices = fs_devices;
3439 device->missing = 1;
3440 fs_devices->num_devices++;
3441 fs_devices->missing_devices++;
3442 spin_lock_init(&device->io_lock);
3443 INIT_LIST_HEAD(&device->dev_alloc_list);
3444 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3448 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3449 struct extent_buffer *leaf,
3450 struct btrfs_chunk *chunk)
3452 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3453 struct map_lookup *map;
3454 struct extent_map *em;
3458 u8 uuid[BTRFS_UUID_SIZE];
3463 logical = key->offset;
3464 length = btrfs_chunk_length(leaf, chunk);
3466 read_lock(&map_tree->map_tree.lock);
3467 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3468 read_unlock(&map_tree->map_tree.lock);
3470 /* already mapped? */
3471 if (em && em->start <= logical && em->start + em->len > logical) {
3472 free_extent_map(em);
3475 free_extent_map(em);
3478 em = alloc_extent_map();
3481 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3482 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3484 free_extent_map(em);
3488 em->bdev = (struct block_device *)map;
3489 em->start = logical;
3491 em->block_start = 0;
3492 em->block_len = em->len;
3494 map->num_stripes = num_stripes;
3495 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3496 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3497 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3498 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3499 map->type = btrfs_chunk_type(leaf, chunk);
3500 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3501 for (i = 0; i < num_stripes; i++) {
3502 map->stripes[i].physical =
3503 btrfs_stripe_offset_nr(leaf, chunk, i);
3504 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3505 read_extent_buffer(leaf, uuid, (unsigned long)
3506 btrfs_stripe_dev_uuid_nr(chunk, i),
3508 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3510 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3512 free_extent_map(em);
3515 if (!map->stripes[i].dev) {
3516 map->stripes[i].dev =
3517 add_missing_dev(root, devid, uuid);
3518 if (!map->stripes[i].dev) {
3520 free_extent_map(em);
3524 map->stripes[i].dev->in_fs_metadata = 1;
3527 write_lock(&map_tree->map_tree.lock);
3528 ret = add_extent_mapping(&map_tree->map_tree, em);
3529 write_unlock(&map_tree->map_tree.lock);
3531 free_extent_map(em);
3536 static int fill_device_from_item(struct extent_buffer *leaf,
3537 struct btrfs_dev_item *dev_item,
3538 struct btrfs_device *device)
3542 device->devid = btrfs_device_id(leaf, dev_item);
3543 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3544 device->total_bytes = device->disk_total_bytes;
3545 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3546 device->type = btrfs_device_type(leaf, dev_item);
3547 device->io_align = btrfs_device_io_align(leaf, dev_item);
3548 device->io_width = btrfs_device_io_width(leaf, dev_item);
3549 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3551 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3552 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3557 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3559 struct btrfs_fs_devices *fs_devices;
3562 mutex_lock(&uuid_mutex);
3564 fs_devices = root->fs_info->fs_devices->seed;
3565 while (fs_devices) {
3566 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3570 fs_devices = fs_devices->seed;
3573 fs_devices = find_fsid(fsid);
3579 fs_devices = clone_fs_devices(fs_devices);
3580 if (IS_ERR(fs_devices)) {
3581 ret = PTR_ERR(fs_devices);
3585 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3586 root->fs_info->bdev_holder);
3590 if (!fs_devices->seeding) {
3591 __btrfs_close_devices(fs_devices);
3592 free_fs_devices(fs_devices);
3597 fs_devices->seed = root->fs_info->fs_devices->seed;
3598 root->fs_info->fs_devices->seed = fs_devices;
3600 mutex_unlock(&uuid_mutex);
3604 static int read_one_dev(struct btrfs_root *root,
3605 struct extent_buffer *leaf,
3606 struct btrfs_dev_item *dev_item)
3608 struct btrfs_device *device;
3611 u8 fs_uuid[BTRFS_UUID_SIZE];
3612 u8 dev_uuid[BTRFS_UUID_SIZE];
3614 devid = btrfs_device_id(leaf, dev_item);
3615 read_extent_buffer(leaf, dev_uuid,
3616 (unsigned long)btrfs_device_uuid(dev_item),
3618 read_extent_buffer(leaf, fs_uuid,
3619 (unsigned long)btrfs_device_fsid(dev_item),
3622 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3623 ret = open_seed_devices(root, fs_uuid);
3624 if (ret && !btrfs_test_opt(root, DEGRADED))
3628 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3629 if (!device || !device->bdev) {
3630 if (!btrfs_test_opt(root, DEGRADED))
3634 printk(KERN_WARNING "warning devid %llu missing\n",
3635 (unsigned long long)devid);
3636 device = add_missing_dev(root, devid, dev_uuid);
3639 } else if (!device->missing) {
3641 * this happens when a device that was properly setup
3642 * in the device info lists suddenly goes bad.
3643 * device->bdev is NULL, and so we have to set
3644 * device->missing to one here
3646 root->fs_info->fs_devices->missing_devices++;
3647 device->missing = 1;
3651 if (device->fs_devices != root->fs_info->fs_devices) {
3652 BUG_ON(device->writeable);
3653 if (device->generation !=
3654 btrfs_device_generation(leaf, dev_item))
3658 fill_device_from_item(leaf, dev_item, device);
3659 device->dev_root = root->fs_info->dev_root;
3660 device->in_fs_metadata = 1;
3661 if (device->writeable) {
3662 device->fs_devices->total_rw_bytes += device->total_bytes;
3663 spin_lock(&root->fs_info->free_chunk_lock);
3664 root->fs_info->free_chunk_space += device->total_bytes -
3666 spin_unlock(&root->fs_info->free_chunk_lock);
3672 int btrfs_read_sys_array(struct btrfs_root *root)
3674 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3675 struct extent_buffer *sb;
3676 struct btrfs_disk_key *disk_key;
3677 struct btrfs_chunk *chunk;
3679 unsigned long sb_ptr;
3685 struct btrfs_key key;
3687 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3688 BTRFS_SUPER_INFO_SIZE);
3691 btrfs_set_buffer_uptodate(sb);
3692 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
3694 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3695 array_size = btrfs_super_sys_array_size(super_copy);
3697 ptr = super_copy->sys_chunk_array;
3698 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3701 while (cur < array_size) {
3702 disk_key = (struct btrfs_disk_key *)ptr;
3703 btrfs_disk_key_to_cpu(&key, disk_key);
3705 len = sizeof(*disk_key); ptr += len;
3709 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3710 chunk = (struct btrfs_chunk *)sb_ptr;
3711 ret = read_one_chunk(root, &key, sb, chunk);
3714 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3715 len = btrfs_chunk_item_size(num_stripes);
3724 free_extent_buffer(sb);
3728 int btrfs_read_chunk_tree(struct btrfs_root *root)
3730 struct btrfs_path *path;
3731 struct extent_buffer *leaf;
3732 struct btrfs_key key;
3733 struct btrfs_key found_key;
3737 root = root->fs_info->chunk_root;
3739 path = btrfs_alloc_path();
3743 /* first we search for all of the device items, and then we
3744 * read in all of the chunk items. This way we can create chunk
3745 * mappings that reference all of the devices that are afound
3747 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3751 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3755 leaf = path->nodes[0];
3756 slot = path->slots[0];
3757 if (slot >= btrfs_header_nritems(leaf)) {
3758 ret = btrfs_next_leaf(root, path);
3765 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3766 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3767 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3769 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3770 struct btrfs_dev_item *dev_item;
3771 dev_item = btrfs_item_ptr(leaf, slot,
3772 struct btrfs_dev_item);
3773 ret = read_one_dev(root, leaf, dev_item);
3777 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3778 struct btrfs_chunk *chunk;
3779 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3780 ret = read_one_chunk(root, &found_key, leaf, chunk);
3786 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3788 btrfs_release_path(path);
3793 btrfs_free_path(path);