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 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);
603 disk_super = (struct btrfs_super_block *)bh->b_data;
604 devid = btrfs_stack_device_id(&disk_super->dev_item);
605 if (devid != device->devid)
608 if (memcmp(device->uuid, disk_super->dev_item.uuid,
612 device->generation = btrfs_super_generation(disk_super);
613 if (!latest_transid || device->generation > latest_transid) {
614 latest_devid = devid;
615 latest_transid = device->generation;
619 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
620 device->writeable = 0;
622 device->writeable = !bdev_read_only(bdev);
626 q = bdev_get_queue(bdev);
627 if (blk_queue_discard(q)) {
628 device->can_discard = 1;
629 fs_devices->num_can_discard++;
633 device->in_fs_metadata = 0;
634 device->mode = flags;
636 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
637 fs_devices->rotating = 1;
639 fs_devices->open_devices++;
640 if (device->writeable) {
641 fs_devices->rw_devices++;
642 list_add(&device->dev_alloc_list,
643 &fs_devices->alloc_list);
651 blkdev_put(bdev, flags);
655 if (fs_devices->open_devices == 0) {
659 fs_devices->seeding = seeding;
660 fs_devices->opened = 1;
661 fs_devices->latest_bdev = latest_bdev;
662 fs_devices->latest_devid = latest_devid;
663 fs_devices->latest_trans = latest_transid;
664 fs_devices->total_rw_bytes = 0;
669 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
670 fmode_t flags, void *holder)
674 mutex_lock(&uuid_mutex);
675 if (fs_devices->opened) {
676 fs_devices->opened++;
679 ret = __btrfs_open_devices(fs_devices, flags, holder);
681 mutex_unlock(&uuid_mutex);
685 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
686 struct btrfs_fs_devices **fs_devices_ret)
688 struct btrfs_super_block *disk_super;
689 struct block_device *bdev;
690 struct buffer_head *bh;
695 mutex_lock(&uuid_mutex);
698 bdev = blkdev_get_by_path(path, flags, holder);
705 ret = set_blocksize(bdev, 4096);
708 bh = btrfs_read_dev_super(bdev);
713 disk_super = (struct btrfs_super_block *)bh->b_data;
714 devid = btrfs_stack_device_id(&disk_super->dev_item);
715 transid = btrfs_super_generation(disk_super);
716 if (disk_super->label[0])
717 printk(KERN_INFO "device label %s ", disk_super->label);
719 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
720 printk(KERN_CONT "devid %llu transid %llu %s\n",
721 (unsigned long long)devid, (unsigned long long)transid, path);
722 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
726 blkdev_put(bdev, flags);
728 mutex_unlock(&uuid_mutex);
732 /* helper to account the used device space in the range */
733 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
734 u64 end, u64 *length)
736 struct btrfs_key key;
737 struct btrfs_root *root = device->dev_root;
738 struct btrfs_dev_extent *dev_extent;
739 struct btrfs_path *path;
743 struct extent_buffer *l;
747 if (start >= device->total_bytes)
750 path = btrfs_alloc_path();
755 key.objectid = device->devid;
757 key.type = BTRFS_DEV_EXTENT_KEY;
759 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
763 ret = btrfs_previous_item(root, path, key.objectid, key.type);
770 slot = path->slots[0];
771 if (slot >= btrfs_header_nritems(l)) {
772 ret = btrfs_next_leaf(root, path);
780 btrfs_item_key_to_cpu(l, &key, slot);
782 if (key.objectid < device->devid)
785 if (key.objectid > device->devid)
788 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
791 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
792 extent_end = key.offset + btrfs_dev_extent_length(l,
794 if (key.offset <= start && extent_end > end) {
795 *length = end - start + 1;
797 } else if (key.offset <= start && extent_end > start)
798 *length += extent_end - start;
799 else if (key.offset > start && extent_end <= end)
800 *length += extent_end - key.offset;
801 else if (key.offset > start && key.offset <= end) {
802 *length += end - key.offset + 1;
804 } else if (key.offset > end)
812 btrfs_free_path(path);
817 * find_free_dev_extent - find free space in the specified device
818 * @trans: transaction handler
819 * @device: the device which we search the free space in
820 * @num_bytes: the size of the free space that we need
821 * @start: store the start of the free space.
822 * @len: the size of the free space. that we find, or the size of the max
823 * free space if we don't find suitable free space
825 * this uses a pretty simple search, the expectation is that it is
826 * called very infrequently and that a given device has a small number
829 * @start is used to store the start of the free space if we find. But if we
830 * don't find suitable free space, it will be used to store the start position
831 * of the max free space.
833 * @len is used to store the size of the free space that we find.
834 * But if we don't find suitable free space, it is used to store the size of
835 * the max free space.
837 int find_free_dev_extent(struct btrfs_trans_handle *trans,
838 struct btrfs_device *device, u64 num_bytes,
839 u64 *start, u64 *len)
841 struct btrfs_key key;
842 struct btrfs_root *root = device->dev_root;
843 struct btrfs_dev_extent *dev_extent;
844 struct btrfs_path *path;
850 u64 search_end = device->total_bytes;
853 struct extent_buffer *l;
855 /* FIXME use last free of some kind */
857 /* we don't want to overwrite the superblock on the drive,
858 * so we make sure to start at an offset of at least 1MB
860 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
862 max_hole_start = search_start;
866 if (search_start >= search_end) {
871 path = btrfs_alloc_path();
878 key.objectid = device->devid;
879 key.offset = search_start;
880 key.type = BTRFS_DEV_EXTENT_KEY;
882 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
886 ret = btrfs_previous_item(root, path, key.objectid, key.type);
893 slot = path->slots[0];
894 if (slot >= btrfs_header_nritems(l)) {
895 ret = btrfs_next_leaf(root, path);
903 btrfs_item_key_to_cpu(l, &key, slot);
905 if (key.objectid < device->devid)
908 if (key.objectid > device->devid)
911 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
914 if (key.offset > search_start) {
915 hole_size = key.offset - search_start;
917 if (hole_size > max_hole_size) {
918 max_hole_start = search_start;
919 max_hole_size = hole_size;
923 * If this free space is greater than which we need,
924 * it must be the max free space that we have found
925 * until now, so max_hole_start must point to the start
926 * of this free space and the length of this free space
927 * is stored in max_hole_size. Thus, we return
928 * max_hole_start and max_hole_size and go back to the
931 if (hole_size >= num_bytes) {
937 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
938 extent_end = key.offset + btrfs_dev_extent_length(l,
940 if (extent_end > search_start)
941 search_start = extent_end;
948 * At this point, search_start should be the end of
949 * allocated dev extents, and when shrinking the device,
950 * search_end may be smaller than search_start.
952 if (search_end > search_start)
953 hole_size = search_end - search_start;
955 if (hole_size > max_hole_size) {
956 max_hole_start = search_start;
957 max_hole_size = hole_size;
961 if (hole_size < num_bytes)
967 btrfs_free_path(path);
969 *start = max_hole_start;
971 *len = max_hole_size;
975 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
976 struct btrfs_device *device,
980 struct btrfs_path *path;
981 struct btrfs_root *root = device->dev_root;
982 struct btrfs_key key;
983 struct btrfs_key found_key;
984 struct extent_buffer *leaf = NULL;
985 struct btrfs_dev_extent *extent = NULL;
987 path = btrfs_alloc_path();
991 key.objectid = device->devid;
993 key.type = BTRFS_DEV_EXTENT_KEY;
995 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
997 ret = btrfs_previous_item(root, path, key.objectid,
998 BTRFS_DEV_EXTENT_KEY);
1001 leaf = path->nodes[0];
1002 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1003 extent = btrfs_item_ptr(leaf, path->slots[0],
1004 struct btrfs_dev_extent);
1005 BUG_ON(found_key.offset > start || found_key.offset +
1006 btrfs_dev_extent_length(leaf, extent) < start);
1007 } else if (ret == 0) {
1008 leaf = path->nodes[0];
1009 extent = btrfs_item_ptr(leaf, path->slots[0],
1010 struct btrfs_dev_extent);
1014 if (device->bytes_used > 0) {
1015 u64 len = btrfs_dev_extent_length(leaf, extent);
1016 device->bytes_used -= len;
1017 spin_lock(&root->fs_info->free_chunk_lock);
1018 root->fs_info->free_chunk_space += len;
1019 spin_unlock(&root->fs_info->free_chunk_lock);
1021 ret = btrfs_del_item(trans, root, path);
1024 btrfs_free_path(path);
1028 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1029 struct btrfs_device *device,
1030 u64 chunk_tree, u64 chunk_objectid,
1031 u64 chunk_offset, u64 start, u64 num_bytes)
1034 struct btrfs_path *path;
1035 struct btrfs_root *root = device->dev_root;
1036 struct btrfs_dev_extent *extent;
1037 struct extent_buffer *leaf;
1038 struct btrfs_key key;
1040 WARN_ON(!device->in_fs_metadata);
1041 path = btrfs_alloc_path();
1045 key.objectid = device->devid;
1047 key.type = BTRFS_DEV_EXTENT_KEY;
1048 ret = btrfs_insert_empty_item(trans, root, path, &key,
1052 leaf = path->nodes[0];
1053 extent = btrfs_item_ptr(leaf, path->slots[0],
1054 struct btrfs_dev_extent);
1055 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1056 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1057 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1059 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1060 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1063 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1064 btrfs_mark_buffer_dirty(leaf);
1065 btrfs_free_path(path);
1069 static noinline int find_next_chunk(struct btrfs_root *root,
1070 u64 objectid, u64 *offset)
1072 struct btrfs_path *path;
1074 struct btrfs_key key;
1075 struct btrfs_chunk *chunk;
1076 struct btrfs_key found_key;
1078 path = btrfs_alloc_path();
1082 key.objectid = objectid;
1083 key.offset = (u64)-1;
1084 key.type = BTRFS_CHUNK_ITEM_KEY;
1086 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1092 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1096 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1098 if (found_key.objectid != objectid)
1101 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1102 struct btrfs_chunk);
1103 *offset = found_key.offset +
1104 btrfs_chunk_length(path->nodes[0], chunk);
1109 btrfs_free_path(path);
1113 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1116 struct btrfs_key key;
1117 struct btrfs_key found_key;
1118 struct btrfs_path *path;
1120 root = root->fs_info->chunk_root;
1122 path = btrfs_alloc_path();
1126 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1127 key.type = BTRFS_DEV_ITEM_KEY;
1128 key.offset = (u64)-1;
1130 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1136 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1137 BTRFS_DEV_ITEM_KEY);
1141 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1143 *objectid = found_key.offset + 1;
1147 btrfs_free_path(path);
1152 * the device information is stored in the chunk root
1153 * the btrfs_device struct should be fully filled in
1155 int btrfs_add_device(struct btrfs_trans_handle *trans,
1156 struct btrfs_root *root,
1157 struct btrfs_device *device)
1160 struct btrfs_path *path;
1161 struct btrfs_dev_item *dev_item;
1162 struct extent_buffer *leaf;
1163 struct btrfs_key key;
1166 root = root->fs_info->chunk_root;
1168 path = btrfs_alloc_path();
1172 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1173 key.type = BTRFS_DEV_ITEM_KEY;
1174 key.offset = device->devid;
1176 ret = btrfs_insert_empty_item(trans, root, path, &key,
1181 leaf = path->nodes[0];
1182 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1184 btrfs_set_device_id(leaf, dev_item, device->devid);
1185 btrfs_set_device_generation(leaf, dev_item, 0);
1186 btrfs_set_device_type(leaf, dev_item, device->type);
1187 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1188 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1189 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1190 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1191 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1192 btrfs_set_device_group(leaf, dev_item, 0);
1193 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1194 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1195 btrfs_set_device_start_offset(leaf, dev_item, 0);
1197 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1198 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1199 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1200 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1201 btrfs_mark_buffer_dirty(leaf);
1205 btrfs_free_path(path);
1209 static int btrfs_rm_dev_item(struct btrfs_root *root,
1210 struct btrfs_device *device)
1213 struct btrfs_path *path;
1214 struct btrfs_key key;
1215 struct btrfs_trans_handle *trans;
1217 root = root->fs_info->chunk_root;
1219 path = btrfs_alloc_path();
1223 trans = btrfs_start_transaction(root, 0);
1224 if (IS_ERR(trans)) {
1225 btrfs_free_path(path);
1226 return PTR_ERR(trans);
1228 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1229 key.type = BTRFS_DEV_ITEM_KEY;
1230 key.offset = device->devid;
1233 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1242 ret = btrfs_del_item(trans, root, path);
1246 btrfs_free_path(path);
1247 unlock_chunks(root);
1248 btrfs_commit_transaction(trans, root);
1252 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1254 struct btrfs_device *device;
1255 struct btrfs_device *next_device;
1256 struct block_device *bdev;
1257 struct buffer_head *bh = NULL;
1258 struct btrfs_super_block *disk_super;
1259 struct btrfs_fs_devices *cur_devices;
1265 bool clear_super = false;
1267 mutex_lock(&uuid_mutex);
1268 mutex_lock(&root->fs_info->volume_mutex);
1270 all_avail = root->fs_info->avail_data_alloc_bits |
1271 root->fs_info->avail_system_alloc_bits |
1272 root->fs_info->avail_metadata_alloc_bits;
1274 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1275 root->fs_info->fs_devices->num_devices <= 4) {
1276 printk(KERN_ERR "btrfs: unable to go below four devices "
1282 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1283 root->fs_info->fs_devices->num_devices <= 2) {
1284 printk(KERN_ERR "btrfs: unable to go below two "
1285 "devices on raid1\n");
1290 if (strcmp(device_path, "missing") == 0) {
1291 struct list_head *devices;
1292 struct btrfs_device *tmp;
1295 devices = &root->fs_info->fs_devices->devices;
1297 * It is safe to read the devices since the volume_mutex
1300 list_for_each_entry(tmp, devices, dev_list) {
1301 if (tmp->in_fs_metadata && !tmp->bdev) {
1310 printk(KERN_ERR "btrfs: no missing devices found to "
1315 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1316 root->fs_info->bdev_holder);
1318 ret = PTR_ERR(bdev);
1322 set_blocksize(bdev, 4096);
1323 bh = btrfs_read_dev_super(bdev);
1328 disk_super = (struct btrfs_super_block *)bh->b_data;
1329 devid = btrfs_stack_device_id(&disk_super->dev_item);
1330 dev_uuid = disk_super->dev_item.uuid;
1331 device = btrfs_find_device(root, devid, dev_uuid,
1339 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1340 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1346 if (device->writeable) {
1348 list_del_init(&device->dev_alloc_list);
1349 unlock_chunks(root);
1350 root->fs_info->fs_devices->rw_devices--;
1354 ret = btrfs_shrink_device(device, 0);
1358 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1362 spin_lock(&root->fs_info->free_chunk_lock);
1363 root->fs_info->free_chunk_space = device->total_bytes -
1365 spin_unlock(&root->fs_info->free_chunk_lock);
1367 device->in_fs_metadata = 0;
1368 btrfs_scrub_cancel_dev(root, device);
1371 * the device list mutex makes sure that we don't change
1372 * the device list while someone else is writing out all
1373 * the device supers.
1376 cur_devices = device->fs_devices;
1377 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1378 list_del_rcu(&device->dev_list);
1380 device->fs_devices->num_devices--;
1382 if (device->missing)
1383 root->fs_info->fs_devices->missing_devices--;
1385 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1386 struct btrfs_device, dev_list);
1387 if (device->bdev == root->fs_info->sb->s_bdev)
1388 root->fs_info->sb->s_bdev = next_device->bdev;
1389 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1390 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1393 device->fs_devices->open_devices--;
1395 call_rcu(&device->rcu, free_device);
1396 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1398 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1399 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1401 if (cur_devices->open_devices == 0) {
1402 struct btrfs_fs_devices *fs_devices;
1403 fs_devices = root->fs_info->fs_devices;
1404 while (fs_devices) {
1405 if (fs_devices->seed == cur_devices)
1407 fs_devices = fs_devices->seed;
1409 fs_devices->seed = cur_devices->seed;
1410 cur_devices->seed = NULL;
1412 __btrfs_close_devices(cur_devices);
1413 unlock_chunks(root);
1414 free_fs_devices(cur_devices);
1418 * at this point, the device is zero sized. We want to
1419 * remove it from the devices list and zero out the old super
1422 /* make sure this device isn't detected as part of
1425 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1426 set_buffer_dirty(bh);
1427 sync_dirty_buffer(bh);
1436 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1438 mutex_unlock(&root->fs_info->volume_mutex);
1439 mutex_unlock(&uuid_mutex);
1442 if (device->writeable) {
1444 list_add(&device->dev_alloc_list,
1445 &root->fs_info->fs_devices->alloc_list);
1446 unlock_chunks(root);
1447 root->fs_info->fs_devices->rw_devices++;
1453 * does all the dirty work required for changing file system's UUID.
1455 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root)
1458 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1459 struct btrfs_fs_devices *old_devices;
1460 struct btrfs_fs_devices *seed_devices;
1461 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1462 struct btrfs_device *device;
1465 BUG_ON(!mutex_is_locked(&uuid_mutex));
1466 if (!fs_devices->seeding)
1469 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1473 old_devices = clone_fs_devices(fs_devices);
1474 if (IS_ERR(old_devices)) {
1475 kfree(seed_devices);
1476 return PTR_ERR(old_devices);
1479 list_add(&old_devices->list, &fs_uuids);
1481 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1482 seed_devices->opened = 1;
1483 INIT_LIST_HEAD(&seed_devices->devices);
1484 INIT_LIST_HEAD(&seed_devices->alloc_list);
1485 mutex_init(&seed_devices->device_list_mutex);
1487 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1488 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1490 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1492 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1493 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1494 device->fs_devices = seed_devices;
1497 fs_devices->seeding = 0;
1498 fs_devices->num_devices = 0;
1499 fs_devices->open_devices = 0;
1500 fs_devices->seed = seed_devices;
1502 generate_random_uuid(fs_devices->fsid);
1503 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1504 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1505 super_flags = btrfs_super_flags(disk_super) &
1506 ~BTRFS_SUPER_FLAG_SEEDING;
1507 btrfs_set_super_flags(disk_super, super_flags);
1513 * strore the expected generation for seed devices in device items.
1515 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1516 struct btrfs_root *root)
1518 struct btrfs_path *path;
1519 struct extent_buffer *leaf;
1520 struct btrfs_dev_item *dev_item;
1521 struct btrfs_device *device;
1522 struct btrfs_key key;
1523 u8 fs_uuid[BTRFS_UUID_SIZE];
1524 u8 dev_uuid[BTRFS_UUID_SIZE];
1528 path = btrfs_alloc_path();
1532 root = root->fs_info->chunk_root;
1533 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1535 key.type = BTRFS_DEV_ITEM_KEY;
1538 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1542 leaf = path->nodes[0];
1544 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1545 ret = btrfs_next_leaf(root, path);
1550 leaf = path->nodes[0];
1551 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1552 btrfs_release_path(path);
1556 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1557 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1558 key.type != BTRFS_DEV_ITEM_KEY)
1561 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1562 struct btrfs_dev_item);
1563 devid = btrfs_device_id(leaf, dev_item);
1564 read_extent_buffer(leaf, dev_uuid,
1565 (unsigned long)btrfs_device_uuid(dev_item),
1567 read_extent_buffer(leaf, fs_uuid,
1568 (unsigned long)btrfs_device_fsid(dev_item),
1570 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1573 if (device->fs_devices->seeding) {
1574 btrfs_set_device_generation(leaf, dev_item,
1575 device->generation);
1576 btrfs_mark_buffer_dirty(leaf);
1584 btrfs_free_path(path);
1588 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1590 struct request_queue *q;
1591 struct btrfs_trans_handle *trans;
1592 struct btrfs_device *device;
1593 struct block_device *bdev;
1594 struct list_head *devices;
1595 struct super_block *sb = root->fs_info->sb;
1597 int seeding_dev = 0;
1600 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1603 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1604 root->fs_info->bdev_holder);
1606 return PTR_ERR(bdev);
1608 if (root->fs_info->fs_devices->seeding) {
1610 down_write(&sb->s_umount);
1611 mutex_lock(&uuid_mutex);
1614 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1615 mutex_lock(&root->fs_info->volume_mutex);
1617 devices = &root->fs_info->fs_devices->devices;
1619 * we have the volume lock, so we don't need the extra
1620 * device list mutex while reading the list here.
1622 list_for_each_entry(device, devices, dev_list) {
1623 if (device->bdev == bdev) {
1629 device = kzalloc(sizeof(*device), GFP_NOFS);
1631 /* we can safely leave the fs_devices entry around */
1636 device->name = kstrdup(device_path, GFP_NOFS);
1637 if (!device->name) {
1643 ret = find_next_devid(root, &device->devid);
1645 kfree(device->name);
1650 trans = btrfs_start_transaction(root, 0);
1651 if (IS_ERR(trans)) {
1652 kfree(device->name);
1654 ret = PTR_ERR(trans);
1660 q = bdev_get_queue(bdev);
1661 if (blk_queue_discard(q))
1662 device->can_discard = 1;
1663 device->writeable = 1;
1664 device->work.func = pending_bios_fn;
1665 generate_random_uuid(device->uuid);
1666 spin_lock_init(&device->io_lock);
1667 device->generation = trans->transid;
1668 device->io_width = root->sectorsize;
1669 device->io_align = root->sectorsize;
1670 device->sector_size = root->sectorsize;
1671 device->total_bytes = i_size_read(bdev->bd_inode);
1672 device->disk_total_bytes = device->total_bytes;
1673 device->dev_root = root->fs_info->dev_root;
1674 device->bdev = bdev;
1675 device->in_fs_metadata = 1;
1676 device->mode = FMODE_EXCL;
1677 set_blocksize(device->bdev, 4096);
1680 sb->s_flags &= ~MS_RDONLY;
1681 ret = btrfs_prepare_sprout(trans, root);
1685 device->fs_devices = root->fs_info->fs_devices;
1688 * we don't want write_supers to jump in here with our device
1691 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1692 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
1693 list_add(&device->dev_alloc_list,
1694 &root->fs_info->fs_devices->alloc_list);
1695 root->fs_info->fs_devices->num_devices++;
1696 root->fs_info->fs_devices->open_devices++;
1697 root->fs_info->fs_devices->rw_devices++;
1698 if (device->can_discard)
1699 root->fs_info->fs_devices->num_can_discard++;
1700 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1702 spin_lock(&root->fs_info->free_chunk_lock);
1703 root->fs_info->free_chunk_space += device->total_bytes;
1704 spin_unlock(&root->fs_info->free_chunk_lock);
1706 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1707 root->fs_info->fs_devices->rotating = 1;
1709 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1710 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1711 total_bytes + device->total_bytes);
1713 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1714 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1716 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1719 ret = init_first_rw_device(trans, root, device);
1721 ret = btrfs_finish_sprout(trans, root);
1724 ret = btrfs_add_device(trans, root, device);
1728 * we've got more storage, clear any full flags on the space
1731 btrfs_clear_space_info_full(root->fs_info);
1733 unlock_chunks(root);
1734 btrfs_commit_transaction(trans, root);
1737 mutex_unlock(&uuid_mutex);
1738 up_write(&sb->s_umount);
1740 ret = btrfs_relocate_sys_chunks(root);
1744 mutex_unlock(&root->fs_info->volume_mutex);
1747 blkdev_put(bdev, FMODE_EXCL);
1749 mutex_unlock(&uuid_mutex);
1750 up_write(&sb->s_umount);
1755 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1756 struct btrfs_device *device)
1759 struct btrfs_path *path;
1760 struct btrfs_root *root;
1761 struct btrfs_dev_item *dev_item;
1762 struct extent_buffer *leaf;
1763 struct btrfs_key key;
1765 root = device->dev_root->fs_info->chunk_root;
1767 path = btrfs_alloc_path();
1771 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1772 key.type = BTRFS_DEV_ITEM_KEY;
1773 key.offset = device->devid;
1775 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1784 leaf = path->nodes[0];
1785 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1787 btrfs_set_device_id(leaf, dev_item, device->devid);
1788 btrfs_set_device_type(leaf, dev_item, device->type);
1789 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1790 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1791 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1792 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1793 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1794 btrfs_mark_buffer_dirty(leaf);
1797 btrfs_free_path(path);
1801 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1802 struct btrfs_device *device, u64 new_size)
1804 struct btrfs_super_block *super_copy =
1805 &device->dev_root->fs_info->super_copy;
1806 u64 old_total = btrfs_super_total_bytes(super_copy);
1807 u64 diff = new_size - device->total_bytes;
1809 if (!device->writeable)
1811 if (new_size <= device->total_bytes)
1814 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1815 device->fs_devices->total_rw_bytes += diff;
1817 device->total_bytes = new_size;
1818 device->disk_total_bytes = new_size;
1819 btrfs_clear_space_info_full(device->dev_root->fs_info);
1821 return btrfs_update_device(trans, device);
1824 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1825 struct btrfs_device *device, u64 new_size)
1828 lock_chunks(device->dev_root);
1829 ret = __btrfs_grow_device(trans, device, new_size);
1830 unlock_chunks(device->dev_root);
1834 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1835 struct btrfs_root *root,
1836 u64 chunk_tree, u64 chunk_objectid,
1840 struct btrfs_path *path;
1841 struct btrfs_key key;
1843 root = root->fs_info->chunk_root;
1844 path = btrfs_alloc_path();
1848 key.objectid = chunk_objectid;
1849 key.offset = chunk_offset;
1850 key.type = BTRFS_CHUNK_ITEM_KEY;
1852 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1855 ret = btrfs_del_item(trans, root, path);
1857 btrfs_free_path(path);
1861 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1864 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1865 struct btrfs_disk_key *disk_key;
1866 struct btrfs_chunk *chunk;
1873 struct btrfs_key key;
1875 array_size = btrfs_super_sys_array_size(super_copy);
1877 ptr = super_copy->sys_chunk_array;
1880 while (cur < array_size) {
1881 disk_key = (struct btrfs_disk_key *)ptr;
1882 btrfs_disk_key_to_cpu(&key, disk_key);
1884 len = sizeof(*disk_key);
1886 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1887 chunk = (struct btrfs_chunk *)(ptr + len);
1888 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1889 len += btrfs_chunk_item_size(num_stripes);
1894 if (key.objectid == chunk_objectid &&
1895 key.offset == chunk_offset) {
1896 memmove(ptr, ptr + len, array_size - (cur + len));
1898 btrfs_set_super_sys_array_size(super_copy, array_size);
1907 static int btrfs_relocate_chunk(struct btrfs_root *root,
1908 u64 chunk_tree, u64 chunk_objectid,
1911 struct extent_map_tree *em_tree;
1912 struct btrfs_root *extent_root;
1913 struct btrfs_trans_handle *trans;
1914 struct extent_map *em;
1915 struct map_lookup *map;
1919 root = root->fs_info->chunk_root;
1920 extent_root = root->fs_info->extent_root;
1921 em_tree = &root->fs_info->mapping_tree.map_tree;
1923 ret = btrfs_can_relocate(extent_root, chunk_offset);
1927 /* step one, relocate all the extents inside this chunk */
1928 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1932 trans = btrfs_start_transaction(root, 0);
1933 BUG_ON(IS_ERR(trans));
1938 * step two, delete the device extents and the
1939 * chunk tree entries
1941 read_lock(&em_tree->lock);
1942 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1943 read_unlock(&em_tree->lock);
1945 BUG_ON(em->start > chunk_offset ||
1946 em->start + em->len < chunk_offset);
1947 map = (struct map_lookup *)em->bdev;
1949 for (i = 0; i < map->num_stripes; i++) {
1950 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1951 map->stripes[i].physical);
1954 if (map->stripes[i].dev) {
1955 ret = btrfs_update_device(trans, map->stripes[i].dev);
1959 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1964 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1966 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1967 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1971 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1974 write_lock(&em_tree->lock);
1975 remove_extent_mapping(em_tree, em);
1976 write_unlock(&em_tree->lock);
1981 /* once for the tree */
1982 free_extent_map(em);
1984 free_extent_map(em);
1986 unlock_chunks(root);
1987 btrfs_end_transaction(trans, root);
1991 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1993 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1994 struct btrfs_path *path;
1995 struct extent_buffer *leaf;
1996 struct btrfs_chunk *chunk;
1997 struct btrfs_key key;
1998 struct btrfs_key found_key;
1999 u64 chunk_tree = chunk_root->root_key.objectid;
2001 bool retried = false;
2005 path = btrfs_alloc_path();
2010 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2011 key.offset = (u64)-1;
2012 key.type = BTRFS_CHUNK_ITEM_KEY;
2015 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2020 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2027 leaf = path->nodes[0];
2028 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2030 chunk = btrfs_item_ptr(leaf, path->slots[0],
2031 struct btrfs_chunk);
2032 chunk_type = btrfs_chunk_type(leaf, chunk);
2033 btrfs_release_path(path);
2035 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2036 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2045 if (found_key.offset == 0)
2047 key.offset = found_key.offset - 1;
2050 if (failed && !retried) {
2054 } else if (failed && retried) {
2059 btrfs_free_path(path);
2063 static u64 div_factor(u64 num, int factor)
2072 int btrfs_balance(struct btrfs_root *dev_root)
2075 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2076 struct btrfs_device *device;
2079 struct btrfs_path *path;
2080 struct btrfs_key key;
2081 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2082 struct btrfs_trans_handle *trans;
2083 struct btrfs_key found_key;
2085 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2088 if (!capable(CAP_SYS_ADMIN))
2091 mutex_lock(&dev_root->fs_info->volume_mutex);
2092 dev_root = dev_root->fs_info->dev_root;
2094 /* step one make some room on all the devices */
2095 list_for_each_entry(device, devices, dev_list) {
2096 old_size = device->total_bytes;
2097 size_to_free = div_factor(old_size, 1);
2098 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2099 if (!device->writeable ||
2100 device->total_bytes - device->bytes_used > size_to_free)
2103 ret = btrfs_shrink_device(device, old_size - size_to_free);
2108 trans = btrfs_start_transaction(dev_root, 0);
2109 BUG_ON(IS_ERR(trans));
2111 ret = btrfs_grow_device(trans, device, old_size);
2114 btrfs_end_transaction(trans, dev_root);
2117 /* step two, relocate all the chunks */
2118 path = btrfs_alloc_path();
2123 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2124 key.offset = (u64)-1;
2125 key.type = BTRFS_CHUNK_ITEM_KEY;
2128 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2133 * this shouldn't happen, it means the last relocate
2139 ret = btrfs_previous_item(chunk_root, path, 0,
2140 BTRFS_CHUNK_ITEM_KEY);
2144 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2146 if (found_key.objectid != key.objectid)
2149 /* chunk zero is special */
2150 if (found_key.offset == 0)
2153 btrfs_release_path(path);
2154 ret = btrfs_relocate_chunk(chunk_root,
2155 chunk_root->root_key.objectid,
2158 if (ret && ret != -ENOSPC)
2160 key.offset = found_key.offset - 1;
2164 btrfs_free_path(path);
2165 mutex_unlock(&dev_root->fs_info->volume_mutex);
2170 * shrinking a device means finding all of the device extents past
2171 * the new size, and then following the back refs to the chunks.
2172 * The chunk relocation code actually frees the device extent
2174 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2176 struct btrfs_trans_handle *trans;
2177 struct btrfs_root *root = device->dev_root;
2178 struct btrfs_dev_extent *dev_extent = NULL;
2179 struct btrfs_path *path;
2187 bool retried = false;
2188 struct extent_buffer *l;
2189 struct btrfs_key key;
2190 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2191 u64 old_total = btrfs_super_total_bytes(super_copy);
2192 u64 old_size = device->total_bytes;
2193 u64 diff = device->total_bytes - new_size;
2195 if (new_size >= device->total_bytes)
2198 path = btrfs_alloc_path();
2206 device->total_bytes = new_size;
2207 if (device->writeable) {
2208 device->fs_devices->total_rw_bytes -= diff;
2209 spin_lock(&root->fs_info->free_chunk_lock);
2210 root->fs_info->free_chunk_space -= diff;
2211 spin_unlock(&root->fs_info->free_chunk_lock);
2213 unlock_chunks(root);
2216 key.objectid = device->devid;
2217 key.offset = (u64)-1;
2218 key.type = BTRFS_DEV_EXTENT_KEY;
2221 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2225 ret = btrfs_previous_item(root, path, 0, key.type);
2230 btrfs_release_path(path);
2235 slot = path->slots[0];
2236 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2238 if (key.objectid != device->devid) {
2239 btrfs_release_path(path);
2243 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2244 length = btrfs_dev_extent_length(l, dev_extent);
2246 if (key.offset + length <= new_size) {
2247 btrfs_release_path(path);
2251 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2252 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2253 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2254 btrfs_release_path(path);
2256 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2258 if (ret && ret != -ENOSPC)
2265 if (failed && !retried) {
2269 } else if (failed && retried) {
2273 device->total_bytes = old_size;
2274 if (device->writeable)
2275 device->fs_devices->total_rw_bytes += diff;
2276 spin_lock(&root->fs_info->free_chunk_lock);
2277 root->fs_info->free_chunk_space += diff;
2278 spin_unlock(&root->fs_info->free_chunk_lock);
2279 unlock_chunks(root);
2283 /* Shrinking succeeded, else we would be at "done". */
2284 trans = btrfs_start_transaction(root, 0);
2285 if (IS_ERR(trans)) {
2286 ret = PTR_ERR(trans);
2292 device->disk_total_bytes = new_size;
2293 /* Now btrfs_update_device() will change the on-disk size. */
2294 ret = btrfs_update_device(trans, device);
2296 unlock_chunks(root);
2297 btrfs_end_transaction(trans, root);
2300 WARN_ON(diff > old_total);
2301 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2302 unlock_chunks(root);
2303 btrfs_end_transaction(trans, root);
2305 btrfs_free_path(path);
2309 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2310 struct btrfs_root *root,
2311 struct btrfs_key *key,
2312 struct btrfs_chunk *chunk, int item_size)
2314 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2315 struct btrfs_disk_key disk_key;
2319 array_size = btrfs_super_sys_array_size(super_copy);
2320 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2323 ptr = super_copy->sys_chunk_array + array_size;
2324 btrfs_cpu_key_to_disk(&disk_key, key);
2325 memcpy(ptr, &disk_key, sizeof(disk_key));
2326 ptr += sizeof(disk_key);
2327 memcpy(ptr, chunk, item_size);
2328 item_size += sizeof(disk_key);
2329 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2334 * sort the devices in descending order by max_avail, total_avail
2336 static int btrfs_cmp_device_info(const void *a, const void *b)
2338 const struct btrfs_device_info *di_a = a;
2339 const struct btrfs_device_info *di_b = b;
2341 if (di_a->max_avail > di_b->max_avail)
2343 if (di_a->max_avail < di_b->max_avail)
2345 if (di_a->total_avail > di_b->total_avail)
2347 if (di_a->total_avail < di_b->total_avail)
2352 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2353 struct btrfs_root *extent_root,
2354 struct map_lookup **map_ret,
2355 u64 *num_bytes_out, u64 *stripe_size_out,
2356 u64 start, u64 type)
2358 struct btrfs_fs_info *info = extent_root->fs_info;
2359 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2360 struct list_head *cur;
2361 struct map_lookup *map = NULL;
2362 struct extent_map_tree *em_tree;
2363 struct extent_map *em;
2364 struct btrfs_device_info *devices_info = NULL;
2366 int num_stripes; /* total number of stripes to allocate */
2367 int sub_stripes; /* sub_stripes info for map */
2368 int dev_stripes; /* stripes per dev */
2369 int devs_max; /* max devs to use */
2370 int devs_min; /* min devs needed */
2371 int devs_increment; /* ndevs has to be a multiple of this */
2372 int ncopies; /* how many copies to data has */
2374 u64 max_stripe_size;
2382 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2383 (type & BTRFS_BLOCK_GROUP_DUP)) {
2385 type &= ~BTRFS_BLOCK_GROUP_DUP;
2388 if (list_empty(&fs_devices->alloc_list))
2395 devs_max = 0; /* 0 == as many as possible */
2399 * define the properties of each RAID type.
2400 * FIXME: move this to a global table and use it in all RAID
2403 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2407 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2409 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2414 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2423 if (type & BTRFS_BLOCK_GROUP_DATA) {
2424 max_stripe_size = 1024 * 1024 * 1024;
2425 max_chunk_size = 10 * max_stripe_size;
2426 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2427 max_stripe_size = 256 * 1024 * 1024;
2428 max_chunk_size = max_stripe_size;
2429 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2430 max_stripe_size = 8 * 1024 * 1024;
2431 max_chunk_size = 2 * max_stripe_size;
2433 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2438 /* we don't want a chunk larger than 10% of writeable space */
2439 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2442 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2447 cur = fs_devices->alloc_list.next;
2450 * in the first pass through the devices list, we gather information
2451 * about the available holes on each device.
2454 while (cur != &fs_devices->alloc_list) {
2455 struct btrfs_device *device;
2459 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2463 if (!device->writeable) {
2465 "btrfs: read-only device in alloc_list\n");
2470 if (!device->in_fs_metadata)
2473 if (device->total_bytes > device->bytes_used)
2474 total_avail = device->total_bytes - device->bytes_used;
2478 /* If there is no space on this device, skip it. */
2479 if (total_avail == 0)
2482 ret = find_free_dev_extent(trans, device,
2483 max_stripe_size * dev_stripes,
2484 &dev_offset, &max_avail);
2485 if (ret && ret != -ENOSPC)
2489 max_avail = max_stripe_size * dev_stripes;
2491 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2494 devices_info[ndevs].dev_offset = dev_offset;
2495 devices_info[ndevs].max_avail = max_avail;
2496 devices_info[ndevs].total_avail = total_avail;
2497 devices_info[ndevs].dev = device;
2502 * now sort the devices by hole size / available space
2504 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2505 btrfs_cmp_device_info, NULL);
2507 /* round down to number of usable stripes */
2508 ndevs -= ndevs % devs_increment;
2510 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2515 if (devs_max && ndevs > devs_max)
2518 * the primary goal is to maximize the number of stripes, so use as many
2519 * devices as possible, even if the stripes are not maximum sized.
2521 stripe_size = devices_info[ndevs-1].max_avail;
2522 num_stripes = ndevs * dev_stripes;
2524 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2525 stripe_size = max_chunk_size * ncopies;
2526 do_div(stripe_size, num_stripes);
2529 do_div(stripe_size, dev_stripes);
2530 do_div(stripe_size, BTRFS_STRIPE_LEN);
2531 stripe_size *= BTRFS_STRIPE_LEN;
2533 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2538 map->num_stripes = num_stripes;
2540 for (i = 0; i < ndevs; ++i) {
2541 for (j = 0; j < dev_stripes; ++j) {
2542 int s = i * dev_stripes + j;
2543 map->stripes[s].dev = devices_info[i].dev;
2544 map->stripes[s].physical = devices_info[i].dev_offset +
2548 map->sector_size = extent_root->sectorsize;
2549 map->stripe_len = BTRFS_STRIPE_LEN;
2550 map->io_align = BTRFS_STRIPE_LEN;
2551 map->io_width = BTRFS_STRIPE_LEN;
2553 map->sub_stripes = sub_stripes;
2556 num_bytes = stripe_size * (num_stripes / ncopies);
2558 *stripe_size_out = stripe_size;
2559 *num_bytes_out = num_bytes;
2561 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
2563 em = alloc_extent_map();
2568 em->bdev = (struct block_device *)map;
2570 em->len = num_bytes;
2571 em->block_start = 0;
2572 em->block_len = em->len;
2574 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2575 write_lock(&em_tree->lock);
2576 ret = add_extent_mapping(em_tree, em);
2577 write_unlock(&em_tree->lock);
2579 free_extent_map(em);
2581 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2582 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2586 for (i = 0; i < map->num_stripes; ++i) {
2587 struct btrfs_device *device;
2590 device = map->stripes[i].dev;
2591 dev_offset = map->stripes[i].physical;
2593 ret = btrfs_alloc_dev_extent(trans, device,
2594 info->chunk_root->root_key.objectid,
2595 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2596 start, dev_offset, stripe_size);
2600 kfree(devices_info);
2605 kfree(devices_info);
2609 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2610 struct btrfs_root *extent_root,
2611 struct map_lookup *map, u64 chunk_offset,
2612 u64 chunk_size, u64 stripe_size)
2615 struct btrfs_key key;
2616 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2617 struct btrfs_device *device;
2618 struct btrfs_chunk *chunk;
2619 struct btrfs_stripe *stripe;
2620 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2624 chunk = kzalloc(item_size, GFP_NOFS);
2629 while (index < map->num_stripes) {
2630 device = map->stripes[index].dev;
2631 device->bytes_used += stripe_size;
2632 ret = btrfs_update_device(trans, device);
2637 spin_lock(&extent_root->fs_info->free_chunk_lock);
2638 extent_root->fs_info->free_chunk_space -= (stripe_size *
2640 spin_unlock(&extent_root->fs_info->free_chunk_lock);
2643 stripe = &chunk->stripe;
2644 while (index < map->num_stripes) {
2645 device = map->stripes[index].dev;
2646 dev_offset = map->stripes[index].physical;
2648 btrfs_set_stack_stripe_devid(stripe, device->devid);
2649 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2650 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2655 btrfs_set_stack_chunk_length(chunk, chunk_size);
2656 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2657 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2658 btrfs_set_stack_chunk_type(chunk, map->type);
2659 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2660 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2661 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2662 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2663 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2665 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2666 key.type = BTRFS_CHUNK_ITEM_KEY;
2667 key.offset = chunk_offset;
2669 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2672 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2673 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2683 * Chunk allocation falls into two parts. The first part does works
2684 * that make the new allocated chunk useable, but not do any operation
2685 * that modifies the chunk tree. The second part does the works that
2686 * require modifying the chunk tree. This division is important for the
2687 * bootstrap process of adding storage to a seed btrfs.
2689 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2690 struct btrfs_root *extent_root, u64 type)
2695 struct map_lookup *map;
2696 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2699 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2704 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2705 &stripe_size, chunk_offset, type);
2709 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2710 chunk_size, stripe_size);
2715 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2716 struct btrfs_root *root,
2717 struct btrfs_device *device)
2720 u64 sys_chunk_offset;
2724 u64 sys_stripe_size;
2726 struct map_lookup *map;
2727 struct map_lookup *sys_map;
2728 struct btrfs_fs_info *fs_info = root->fs_info;
2729 struct btrfs_root *extent_root = fs_info->extent_root;
2732 ret = find_next_chunk(fs_info->chunk_root,
2733 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2737 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2738 (fs_info->metadata_alloc_profile &
2739 fs_info->avail_metadata_alloc_bits);
2740 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2742 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2743 &stripe_size, chunk_offset, alloc_profile);
2746 sys_chunk_offset = chunk_offset + chunk_size;
2748 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2749 (fs_info->system_alloc_profile &
2750 fs_info->avail_system_alloc_bits);
2751 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2753 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2754 &sys_chunk_size, &sys_stripe_size,
2755 sys_chunk_offset, alloc_profile);
2758 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2762 * Modifying chunk tree needs allocating new blocks from both
2763 * system block group and metadata block group. So we only can
2764 * do operations require modifying the chunk tree after both
2765 * block groups were created.
2767 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2768 chunk_size, stripe_size);
2771 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2772 sys_chunk_offset, sys_chunk_size,
2778 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2780 struct extent_map *em;
2781 struct map_lookup *map;
2782 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2786 read_lock(&map_tree->map_tree.lock);
2787 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2788 read_unlock(&map_tree->map_tree.lock);
2792 if (btrfs_test_opt(root, DEGRADED)) {
2793 free_extent_map(em);
2797 map = (struct map_lookup *)em->bdev;
2798 for (i = 0; i < map->num_stripes; i++) {
2799 if (!map->stripes[i].dev->writeable) {
2804 free_extent_map(em);
2808 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2810 extent_map_tree_init(&tree->map_tree);
2813 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2815 struct extent_map *em;
2818 write_lock(&tree->map_tree.lock);
2819 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2821 remove_extent_mapping(&tree->map_tree, em);
2822 write_unlock(&tree->map_tree.lock);
2827 free_extent_map(em);
2828 /* once for the tree */
2829 free_extent_map(em);
2833 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2835 struct extent_map *em;
2836 struct map_lookup *map;
2837 struct extent_map_tree *em_tree = &map_tree->map_tree;
2840 read_lock(&em_tree->lock);
2841 em = lookup_extent_mapping(em_tree, logical, len);
2842 read_unlock(&em_tree->lock);
2845 BUG_ON(em->start > logical || em->start + em->len < logical);
2846 map = (struct map_lookup *)em->bdev;
2847 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2848 ret = map->num_stripes;
2849 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2850 ret = map->sub_stripes;
2853 free_extent_map(em);
2857 static int find_live_mirror(struct map_lookup *map, int first, int num,
2861 if (map->stripes[optimal].dev->bdev)
2863 for (i = first; i < first + num; i++) {
2864 if (map->stripes[i].dev->bdev)
2867 /* we couldn't find one that doesn't fail. Just return something
2868 * and the io error handling code will clean up eventually
2873 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2874 u64 logical, u64 *length,
2875 struct btrfs_multi_bio **multi_ret,
2878 struct extent_map *em;
2879 struct map_lookup *map;
2880 struct extent_map_tree *em_tree = &map_tree->map_tree;
2883 u64 stripe_end_offset;
2887 int stripes_allocated = 8;
2888 int stripes_required = 1;
2893 struct btrfs_multi_bio *multi = NULL;
2895 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
2896 stripes_allocated = 1;
2899 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2904 atomic_set(&multi->error, 0);
2907 read_lock(&em_tree->lock);
2908 em = lookup_extent_mapping(em_tree, logical, *length);
2909 read_unlock(&em_tree->lock);
2912 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2913 (unsigned long long)logical,
2914 (unsigned long long)*length);
2918 BUG_ON(em->start > logical || em->start + em->len < logical);
2919 map = (struct map_lookup *)em->bdev;
2920 offset = logical - em->start;
2922 if (mirror_num > map->num_stripes)
2925 /* if our multi bio struct is too small, back off and try again */
2926 if (rw & REQ_WRITE) {
2927 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2928 BTRFS_BLOCK_GROUP_DUP)) {
2929 stripes_required = map->num_stripes;
2931 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2932 stripes_required = map->sub_stripes;
2936 if (rw & REQ_DISCARD) {
2937 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2938 BTRFS_BLOCK_GROUP_RAID1 |
2939 BTRFS_BLOCK_GROUP_DUP |
2940 BTRFS_BLOCK_GROUP_RAID10)) {
2941 stripes_required = map->num_stripes;
2944 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
2945 stripes_allocated < stripes_required) {
2946 stripes_allocated = map->num_stripes;
2947 free_extent_map(em);
2953 * stripe_nr counts the total number of stripes we have to stride
2954 * to get to this block
2956 do_div(stripe_nr, map->stripe_len);
2958 stripe_offset = stripe_nr * map->stripe_len;
2959 BUG_ON(offset < stripe_offset);
2961 /* stripe_offset is the offset of this block in its stripe*/
2962 stripe_offset = offset - stripe_offset;
2964 if (rw & REQ_DISCARD)
2965 *length = min_t(u64, em->len - offset, *length);
2966 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2967 BTRFS_BLOCK_GROUP_RAID1 |
2968 BTRFS_BLOCK_GROUP_RAID10 |
2969 BTRFS_BLOCK_GROUP_DUP)) {
2970 /* we limit the length of each bio to what fits in a stripe */
2971 *length = min_t(u64, em->len - offset,
2972 map->stripe_len - stripe_offset);
2974 *length = em->len - offset;
2982 stripe_nr_orig = stripe_nr;
2983 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2984 (~(map->stripe_len - 1));
2985 do_div(stripe_nr_end, map->stripe_len);
2986 stripe_end_offset = stripe_nr_end * map->stripe_len -
2988 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2989 if (rw & REQ_DISCARD)
2990 num_stripes = min_t(u64, map->num_stripes,
2991 stripe_nr_end - stripe_nr_orig);
2992 stripe_index = do_div(stripe_nr, map->num_stripes);
2993 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2994 if (rw & (REQ_WRITE | REQ_DISCARD))
2995 num_stripes = map->num_stripes;
2996 else if (mirror_num)
2997 stripe_index = mirror_num - 1;
2999 stripe_index = find_live_mirror(map, 0,
3001 current->pid % map->num_stripes);
3004 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3005 if (rw & (REQ_WRITE | REQ_DISCARD))
3006 num_stripes = map->num_stripes;
3007 else if (mirror_num)
3008 stripe_index = mirror_num - 1;
3010 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3011 int factor = map->num_stripes / map->sub_stripes;
3013 stripe_index = do_div(stripe_nr, factor);
3014 stripe_index *= map->sub_stripes;
3017 num_stripes = map->sub_stripes;
3018 else if (rw & REQ_DISCARD)
3019 num_stripes = min_t(u64, map->sub_stripes *
3020 (stripe_nr_end - stripe_nr_orig),
3022 else if (mirror_num)
3023 stripe_index += mirror_num - 1;
3025 stripe_index = find_live_mirror(map, stripe_index,
3026 map->sub_stripes, stripe_index +
3027 current->pid % map->sub_stripes);
3031 * after this do_div call, stripe_nr is the number of stripes
3032 * on this device we have to walk to find the data, and
3033 * stripe_index is the number of our device in the stripe array
3035 stripe_index = do_div(stripe_nr, map->num_stripes);
3037 BUG_ON(stripe_index >= map->num_stripes);
3039 if (rw & REQ_DISCARD) {
3040 for (i = 0; i < num_stripes; i++) {
3041 multi->stripes[i].physical =
3042 map->stripes[stripe_index].physical +
3043 stripe_offset + stripe_nr * map->stripe_len;
3044 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3046 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3048 u32 last_stripe = 0;
3051 div_u64_rem(stripe_nr_end - 1,
3055 for (j = 0; j < map->num_stripes; j++) {
3058 div_u64_rem(stripe_nr_end - 1 - j,
3059 map->num_stripes, &test);
3060 if (test == stripe_index)
3063 stripes = stripe_nr_end - 1 - j;
3064 do_div(stripes, map->num_stripes);
3065 multi->stripes[i].length = map->stripe_len *
3066 (stripes - stripe_nr + 1);
3069 multi->stripes[i].length -=
3073 if (stripe_index == last_stripe)
3074 multi->stripes[i].length -=
3076 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3079 int factor = map->num_stripes /
3081 u32 last_stripe = 0;
3083 div_u64_rem(stripe_nr_end - 1,
3084 factor, &last_stripe);
3085 last_stripe *= map->sub_stripes;
3087 for (j = 0; j < factor; j++) {
3090 div_u64_rem(stripe_nr_end - 1 - j,
3094 stripe_index / map->sub_stripes)
3097 stripes = stripe_nr_end - 1 - j;
3098 do_div(stripes, factor);
3099 multi->stripes[i].length = map->stripe_len *
3100 (stripes - stripe_nr + 1);
3102 if (i < map->sub_stripes) {
3103 multi->stripes[i].length -=
3105 if (i == map->sub_stripes - 1)
3108 if (stripe_index >= last_stripe &&
3109 stripe_index <= (last_stripe +
3110 map->sub_stripes - 1)) {
3111 multi->stripes[i].length -=
3115 multi->stripes[i].length = *length;
3118 if (stripe_index == map->num_stripes) {
3119 /* This could only happen for RAID0/10 */
3125 for (i = 0; i < num_stripes; i++) {
3126 multi->stripes[i].physical =
3127 map->stripes[stripe_index].physical +
3129 stripe_nr * map->stripe_len;
3130 multi->stripes[i].dev =
3131 map->stripes[stripe_index].dev;
3137 multi->num_stripes = num_stripes;
3138 multi->max_errors = max_errors;
3141 free_extent_map(em);
3145 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3146 u64 logical, u64 *length,
3147 struct btrfs_multi_bio **multi_ret, int mirror_num)
3149 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3153 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3154 u64 chunk_start, u64 physical, u64 devid,
3155 u64 **logical, int *naddrs, int *stripe_len)
3157 struct extent_map_tree *em_tree = &map_tree->map_tree;
3158 struct extent_map *em;
3159 struct map_lookup *map;
3166 read_lock(&em_tree->lock);
3167 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3168 read_unlock(&em_tree->lock);
3170 BUG_ON(!em || em->start != chunk_start);
3171 map = (struct map_lookup *)em->bdev;
3174 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3175 do_div(length, map->num_stripes / map->sub_stripes);
3176 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3177 do_div(length, map->num_stripes);
3179 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3182 for (i = 0; i < map->num_stripes; i++) {
3183 if (devid && map->stripes[i].dev->devid != devid)
3185 if (map->stripes[i].physical > physical ||
3186 map->stripes[i].physical + length <= physical)
3189 stripe_nr = physical - map->stripes[i].physical;
3190 do_div(stripe_nr, map->stripe_len);
3192 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3193 stripe_nr = stripe_nr * map->num_stripes + i;
3194 do_div(stripe_nr, map->sub_stripes);
3195 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3196 stripe_nr = stripe_nr * map->num_stripes + i;
3198 bytenr = chunk_start + stripe_nr * map->stripe_len;
3199 WARN_ON(nr >= map->num_stripes);
3200 for (j = 0; j < nr; j++) {
3201 if (buf[j] == bytenr)
3205 WARN_ON(nr >= map->num_stripes);
3212 *stripe_len = map->stripe_len;
3214 free_extent_map(em);
3218 static void end_bio_multi_stripe(struct bio *bio, int err)
3220 struct btrfs_multi_bio *multi = bio->bi_private;
3221 int is_orig_bio = 0;
3224 atomic_inc(&multi->error);
3226 if (bio == multi->orig_bio)
3229 if (atomic_dec_and_test(&multi->stripes_pending)) {
3232 bio = multi->orig_bio;
3234 bio->bi_private = multi->private;
3235 bio->bi_end_io = multi->end_io;
3236 /* only send an error to the higher layers if it is
3237 * beyond the tolerance of the multi-bio
3239 if (atomic_read(&multi->error) > multi->max_errors) {
3243 * this bio is actually up to date, we didn't
3244 * go over the max number of errors
3246 set_bit(BIO_UPTODATE, &bio->bi_flags);
3251 bio_endio(bio, err);
3252 } else if (!is_orig_bio) {
3257 struct async_sched {
3260 struct btrfs_fs_info *info;
3261 struct btrfs_work work;
3265 * see run_scheduled_bios for a description of why bios are collected for
3268 * This will add one bio to the pending list for a device and make sure
3269 * the work struct is scheduled.
3271 static noinline int schedule_bio(struct btrfs_root *root,
3272 struct btrfs_device *device,
3273 int rw, struct bio *bio)
3275 int should_queue = 1;
3276 struct btrfs_pending_bios *pending_bios;
3278 /* don't bother with additional async steps for reads, right now */
3279 if (!(rw & REQ_WRITE)) {
3281 submit_bio(rw, bio);
3287 * nr_async_bios allows us to reliably return congestion to the
3288 * higher layers. Otherwise, the async bio makes it appear we have
3289 * made progress against dirty pages when we've really just put it
3290 * on a queue for later
3292 atomic_inc(&root->fs_info->nr_async_bios);
3293 WARN_ON(bio->bi_next);
3294 bio->bi_next = NULL;
3297 spin_lock(&device->io_lock);
3298 if (bio->bi_rw & REQ_SYNC)
3299 pending_bios = &device->pending_sync_bios;
3301 pending_bios = &device->pending_bios;
3303 if (pending_bios->tail)
3304 pending_bios->tail->bi_next = bio;
3306 pending_bios->tail = bio;
3307 if (!pending_bios->head)
3308 pending_bios->head = bio;
3309 if (device->running_pending)
3312 spin_unlock(&device->io_lock);
3315 btrfs_queue_worker(&root->fs_info->submit_workers,
3320 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3321 int mirror_num, int async_submit)
3323 struct btrfs_mapping_tree *map_tree;
3324 struct btrfs_device *dev;
3325 struct bio *first_bio = bio;
3326 u64 logical = (u64)bio->bi_sector << 9;
3329 struct btrfs_multi_bio *multi = NULL;
3334 length = bio->bi_size;
3335 map_tree = &root->fs_info->mapping_tree;
3336 map_length = length;
3338 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3342 total_devs = multi->num_stripes;
3343 if (map_length < length) {
3344 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3345 "len %llu\n", (unsigned long long)logical,
3346 (unsigned long long)length,
3347 (unsigned long long)map_length);
3350 multi->end_io = first_bio->bi_end_io;
3351 multi->private = first_bio->bi_private;
3352 multi->orig_bio = first_bio;
3353 atomic_set(&multi->stripes_pending, multi->num_stripes);
3355 while (dev_nr < total_devs) {
3356 if (total_devs > 1) {
3357 if (dev_nr < total_devs - 1) {
3358 bio = bio_clone(first_bio, GFP_NOFS);
3363 bio->bi_private = multi;
3364 bio->bi_end_io = end_bio_multi_stripe;
3366 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3367 dev = multi->stripes[dev_nr].dev;
3368 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3369 bio->bi_bdev = dev->bdev;
3371 schedule_bio(root, dev, rw, bio);
3373 submit_bio(rw, bio);
3375 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3376 bio->bi_sector = logical >> 9;
3377 bio_endio(bio, -EIO);
3381 if (total_devs == 1)
3386 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3389 struct btrfs_device *device;
3390 struct btrfs_fs_devices *cur_devices;
3392 cur_devices = root->fs_info->fs_devices;
3393 while (cur_devices) {
3395 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3396 device = __find_device(&cur_devices->devices,
3401 cur_devices = cur_devices->seed;
3406 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3407 u64 devid, u8 *dev_uuid)
3409 struct btrfs_device *device;
3410 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3412 device = kzalloc(sizeof(*device), GFP_NOFS);
3415 list_add(&device->dev_list,
3416 &fs_devices->devices);
3417 device->dev_root = root->fs_info->dev_root;
3418 device->devid = devid;
3419 device->work.func = pending_bios_fn;
3420 device->fs_devices = fs_devices;
3421 device->missing = 1;
3422 fs_devices->num_devices++;
3423 fs_devices->missing_devices++;
3424 spin_lock_init(&device->io_lock);
3425 INIT_LIST_HEAD(&device->dev_alloc_list);
3426 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3430 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3431 struct extent_buffer *leaf,
3432 struct btrfs_chunk *chunk)
3434 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3435 struct map_lookup *map;
3436 struct extent_map *em;
3440 u8 uuid[BTRFS_UUID_SIZE];
3445 logical = key->offset;
3446 length = btrfs_chunk_length(leaf, chunk);
3448 read_lock(&map_tree->map_tree.lock);
3449 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3450 read_unlock(&map_tree->map_tree.lock);
3452 /* already mapped? */
3453 if (em && em->start <= logical && em->start + em->len > logical) {
3454 free_extent_map(em);
3457 free_extent_map(em);
3460 em = alloc_extent_map();
3463 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3464 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3466 free_extent_map(em);
3470 em->bdev = (struct block_device *)map;
3471 em->start = logical;
3473 em->block_start = 0;
3474 em->block_len = em->len;
3476 map->num_stripes = num_stripes;
3477 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3478 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3479 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3480 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3481 map->type = btrfs_chunk_type(leaf, chunk);
3482 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3483 for (i = 0; i < num_stripes; i++) {
3484 map->stripes[i].physical =
3485 btrfs_stripe_offset_nr(leaf, chunk, i);
3486 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3487 read_extent_buffer(leaf, uuid, (unsigned long)
3488 btrfs_stripe_dev_uuid_nr(chunk, i),
3490 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3492 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3494 free_extent_map(em);
3497 if (!map->stripes[i].dev) {
3498 map->stripes[i].dev =
3499 add_missing_dev(root, devid, uuid);
3500 if (!map->stripes[i].dev) {
3502 free_extent_map(em);
3506 map->stripes[i].dev->in_fs_metadata = 1;
3509 write_lock(&map_tree->map_tree.lock);
3510 ret = add_extent_mapping(&map_tree->map_tree, em);
3511 write_unlock(&map_tree->map_tree.lock);
3513 free_extent_map(em);
3518 static int fill_device_from_item(struct extent_buffer *leaf,
3519 struct btrfs_dev_item *dev_item,
3520 struct btrfs_device *device)
3524 device->devid = btrfs_device_id(leaf, dev_item);
3525 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3526 device->total_bytes = device->disk_total_bytes;
3527 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3528 device->type = btrfs_device_type(leaf, dev_item);
3529 device->io_align = btrfs_device_io_align(leaf, dev_item);
3530 device->io_width = btrfs_device_io_width(leaf, dev_item);
3531 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3533 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3534 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3539 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3541 struct btrfs_fs_devices *fs_devices;
3544 mutex_lock(&uuid_mutex);
3546 fs_devices = root->fs_info->fs_devices->seed;
3547 while (fs_devices) {
3548 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3552 fs_devices = fs_devices->seed;
3555 fs_devices = find_fsid(fsid);
3561 fs_devices = clone_fs_devices(fs_devices);
3562 if (IS_ERR(fs_devices)) {
3563 ret = PTR_ERR(fs_devices);
3567 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3568 root->fs_info->bdev_holder);
3572 if (!fs_devices->seeding) {
3573 __btrfs_close_devices(fs_devices);
3574 free_fs_devices(fs_devices);
3579 fs_devices->seed = root->fs_info->fs_devices->seed;
3580 root->fs_info->fs_devices->seed = fs_devices;
3582 mutex_unlock(&uuid_mutex);
3586 static int read_one_dev(struct btrfs_root *root,
3587 struct extent_buffer *leaf,
3588 struct btrfs_dev_item *dev_item)
3590 struct btrfs_device *device;
3593 u8 fs_uuid[BTRFS_UUID_SIZE];
3594 u8 dev_uuid[BTRFS_UUID_SIZE];
3596 devid = btrfs_device_id(leaf, dev_item);
3597 read_extent_buffer(leaf, dev_uuid,
3598 (unsigned long)btrfs_device_uuid(dev_item),
3600 read_extent_buffer(leaf, fs_uuid,
3601 (unsigned long)btrfs_device_fsid(dev_item),
3604 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3605 ret = open_seed_devices(root, fs_uuid);
3606 if (ret && !btrfs_test_opt(root, DEGRADED))
3610 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3611 if (!device || !device->bdev) {
3612 if (!btrfs_test_opt(root, DEGRADED))
3616 printk(KERN_WARNING "warning devid %llu missing\n",
3617 (unsigned long long)devid);
3618 device = add_missing_dev(root, devid, dev_uuid);
3621 } else if (!device->missing) {
3623 * this happens when a device that was properly setup
3624 * in the device info lists suddenly goes bad.
3625 * device->bdev is NULL, and so we have to set
3626 * device->missing to one here
3628 root->fs_info->fs_devices->missing_devices++;
3629 device->missing = 1;
3633 if (device->fs_devices != root->fs_info->fs_devices) {
3634 BUG_ON(device->writeable);
3635 if (device->generation !=
3636 btrfs_device_generation(leaf, dev_item))
3640 fill_device_from_item(leaf, dev_item, device);
3641 device->dev_root = root->fs_info->dev_root;
3642 device->in_fs_metadata = 1;
3643 if (device->writeable) {
3644 device->fs_devices->total_rw_bytes += device->total_bytes;
3645 spin_lock(&root->fs_info->free_chunk_lock);
3646 root->fs_info->free_chunk_space += device->total_bytes -
3648 spin_unlock(&root->fs_info->free_chunk_lock);
3654 int btrfs_read_sys_array(struct btrfs_root *root)
3656 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3657 struct extent_buffer *sb;
3658 struct btrfs_disk_key *disk_key;
3659 struct btrfs_chunk *chunk;
3661 unsigned long sb_ptr;
3667 struct btrfs_key key;
3669 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3670 BTRFS_SUPER_INFO_SIZE);
3673 btrfs_set_buffer_uptodate(sb);
3674 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
3676 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3677 array_size = btrfs_super_sys_array_size(super_copy);
3679 ptr = super_copy->sys_chunk_array;
3680 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3683 while (cur < array_size) {
3684 disk_key = (struct btrfs_disk_key *)ptr;
3685 btrfs_disk_key_to_cpu(&key, disk_key);
3687 len = sizeof(*disk_key); ptr += len;
3691 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3692 chunk = (struct btrfs_chunk *)sb_ptr;
3693 ret = read_one_chunk(root, &key, sb, chunk);
3696 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3697 len = btrfs_chunk_item_size(num_stripes);
3706 free_extent_buffer(sb);
3710 int btrfs_read_chunk_tree(struct btrfs_root *root)
3712 struct btrfs_path *path;
3713 struct extent_buffer *leaf;
3714 struct btrfs_key key;
3715 struct btrfs_key found_key;
3719 root = root->fs_info->chunk_root;
3721 path = btrfs_alloc_path();
3725 /* first we search for all of the device items, and then we
3726 * read in all of the chunk items. This way we can create chunk
3727 * mappings that reference all of the devices that are afound
3729 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3733 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3737 leaf = path->nodes[0];
3738 slot = path->slots[0];
3739 if (slot >= btrfs_header_nritems(leaf)) {
3740 ret = btrfs_next_leaf(root, path);
3747 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3748 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3749 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3751 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3752 struct btrfs_dev_item *dev_item;
3753 dev_item = btrfs_item_ptr(leaf, slot,
3754 struct btrfs_dev_item);
3755 ret = read_one_dev(root, leaf, dev_item);
3759 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3760 struct btrfs_chunk *chunk;
3761 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3762 ret = read_one_chunk(root, &found_key, leaf, chunk);
3768 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3770 btrfs_release_path(path);
3775 btrfs_free_path(path);