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);
605 disk_super = (struct btrfs_super_block *)bh->b_data;
606 devid = btrfs_stack_device_id(&disk_super->dev_item);
607 if (devid != device->devid)
610 if (memcmp(device->uuid, disk_super->dev_item.uuid,
614 device->generation = btrfs_super_generation(disk_super);
615 if (!latest_transid || device->generation > latest_transid) {
616 latest_devid = devid;
617 latest_transid = device->generation;
621 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
622 device->writeable = 0;
624 device->writeable = !bdev_read_only(bdev);
628 q = bdev_get_queue(bdev);
629 if (blk_queue_discard(q)) {
630 device->can_discard = 1;
631 fs_devices->num_can_discard++;
635 device->in_fs_metadata = 0;
636 device->mode = flags;
638 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
639 fs_devices->rotating = 1;
641 fs_devices->open_devices++;
642 if (device->writeable) {
643 fs_devices->rw_devices++;
644 list_add(&device->dev_alloc_list,
645 &fs_devices->alloc_list);
653 blkdev_put(bdev, flags);
657 if (fs_devices->open_devices == 0) {
661 fs_devices->seeding = seeding;
662 fs_devices->opened = 1;
663 fs_devices->latest_bdev = latest_bdev;
664 fs_devices->latest_devid = latest_devid;
665 fs_devices->latest_trans = latest_transid;
666 fs_devices->total_rw_bytes = 0;
671 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
672 fmode_t flags, void *holder)
676 mutex_lock(&uuid_mutex);
677 if (fs_devices->opened) {
678 fs_devices->opened++;
681 ret = __btrfs_open_devices(fs_devices, flags, holder);
683 mutex_unlock(&uuid_mutex);
687 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
688 struct btrfs_fs_devices **fs_devices_ret)
690 struct btrfs_super_block *disk_super;
691 struct block_device *bdev;
692 struct buffer_head *bh;
697 mutex_lock(&uuid_mutex);
700 bdev = blkdev_get_by_path(path, flags, holder);
707 ret = set_blocksize(bdev, 4096);
710 bh = btrfs_read_dev_super(bdev);
715 disk_super = (struct btrfs_super_block *)bh->b_data;
716 devid = btrfs_stack_device_id(&disk_super->dev_item);
717 transid = btrfs_super_generation(disk_super);
718 if (disk_super->label[0])
719 printk(KERN_INFO "device label %s ", disk_super->label);
721 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
722 printk(KERN_CONT "devid %llu transid %llu %s\n",
723 (unsigned long long)devid, (unsigned long long)transid, path);
724 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
728 blkdev_put(bdev, flags);
730 mutex_unlock(&uuid_mutex);
734 /* helper to account the used device space in the range */
735 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
736 u64 end, u64 *length)
738 struct btrfs_key key;
739 struct btrfs_root *root = device->dev_root;
740 struct btrfs_dev_extent *dev_extent;
741 struct btrfs_path *path;
745 struct extent_buffer *l;
749 if (start >= device->total_bytes)
752 path = btrfs_alloc_path();
757 key.objectid = device->devid;
759 key.type = BTRFS_DEV_EXTENT_KEY;
761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
765 ret = btrfs_previous_item(root, path, key.objectid, key.type);
772 slot = path->slots[0];
773 if (slot >= btrfs_header_nritems(l)) {
774 ret = btrfs_next_leaf(root, path);
782 btrfs_item_key_to_cpu(l, &key, slot);
784 if (key.objectid < device->devid)
787 if (key.objectid > device->devid)
790 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
793 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
794 extent_end = key.offset + btrfs_dev_extent_length(l,
796 if (key.offset <= start && extent_end > end) {
797 *length = end - start + 1;
799 } else if (key.offset <= start && extent_end > start)
800 *length += extent_end - start;
801 else if (key.offset > start && extent_end <= end)
802 *length += extent_end - key.offset;
803 else if (key.offset > start && key.offset <= end) {
804 *length += end - key.offset + 1;
806 } else if (key.offset > end)
814 btrfs_free_path(path);
819 * find_free_dev_extent - find free space in the specified device
820 * @trans: transaction handler
821 * @device: the device which we search the free space in
822 * @num_bytes: the size of the free space that we need
823 * @start: store the start of the free space.
824 * @len: the size of the free space. that we find, or the size of the max
825 * free space if we don't find suitable free space
827 * this uses a pretty simple search, the expectation is that it is
828 * called very infrequently and that a given device has a small number
831 * @start is used to store the start of the free space if we find. But if we
832 * don't find suitable free space, it will be used to store the start position
833 * of the max free space.
835 * @len is used to store the size of the free space that we find.
836 * But if we don't find suitable free space, it is used to store the size of
837 * the max free space.
839 int find_free_dev_extent(struct btrfs_trans_handle *trans,
840 struct btrfs_device *device, u64 num_bytes,
841 u64 *start, u64 *len)
843 struct btrfs_key key;
844 struct btrfs_root *root = device->dev_root;
845 struct btrfs_dev_extent *dev_extent;
846 struct btrfs_path *path;
852 u64 search_end = device->total_bytes;
855 struct extent_buffer *l;
857 /* FIXME use last free of some kind */
859 /* we don't want to overwrite the superblock on the drive,
860 * so we make sure to start at an offset of at least 1MB
862 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
864 max_hole_start = search_start;
868 if (search_start >= search_end) {
873 path = btrfs_alloc_path();
880 key.objectid = device->devid;
881 key.offset = search_start;
882 key.type = BTRFS_DEV_EXTENT_KEY;
884 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
888 ret = btrfs_previous_item(root, path, key.objectid, key.type);
895 slot = path->slots[0];
896 if (slot >= btrfs_header_nritems(l)) {
897 ret = btrfs_next_leaf(root, path);
905 btrfs_item_key_to_cpu(l, &key, slot);
907 if (key.objectid < device->devid)
910 if (key.objectid > device->devid)
913 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
916 if (key.offset > search_start) {
917 hole_size = key.offset - search_start;
919 if (hole_size > max_hole_size) {
920 max_hole_start = search_start;
921 max_hole_size = hole_size;
925 * If this free space is greater than which we need,
926 * it must be the max free space that we have found
927 * until now, so max_hole_start must point to the start
928 * of this free space and the length of this free space
929 * is stored in max_hole_size. Thus, we return
930 * max_hole_start and max_hole_size and go back to the
933 if (hole_size >= num_bytes) {
939 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
940 extent_end = key.offset + btrfs_dev_extent_length(l,
942 if (extent_end > search_start)
943 search_start = extent_end;
950 * At this point, search_start should be the end of
951 * allocated dev extents, and when shrinking the device,
952 * search_end may be smaller than search_start.
954 if (search_end > search_start)
955 hole_size = search_end - search_start;
957 if (hole_size > max_hole_size) {
958 max_hole_start = search_start;
959 max_hole_size = hole_size;
963 if (hole_size < num_bytes)
969 btrfs_free_path(path);
971 *start = max_hole_start;
973 *len = max_hole_size;
977 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
978 struct btrfs_device *device,
982 struct btrfs_path *path;
983 struct btrfs_root *root = device->dev_root;
984 struct btrfs_key key;
985 struct btrfs_key found_key;
986 struct extent_buffer *leaf = NULL;
987 struct btrfs_dev_extent *extent = NULL;
989 path = btrfs_alloc_path();
993 key.objectid = device->devid;
995 key.type = BTRFS_DEV_EXTENT_KEY;
997 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
999 ret = btrfs_previous_item(root, path, key.objectid,
1000 BTRFS_DEV_EXTENT_KEY);
1003 leaf = path->nodes[0];
1004 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1005 extent = btrfs_item_ptr(leaf, path->slots[0],
1006 struct btrfs_dev_extent);
1007 BUG_ON(found_key.offset > start || found_key.offset +
1008 btrfs_dev_extent_length(leaf, extent) < start);
1009 } else if (ret == 0) {
1010 leaf = path->nodes[0];
1011 extent = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_dev_extent);
1016 if (device->bytes_used > 0) {
1017 u64 len = btrfs_dev_extent_length(leaf, extent);
1018 device->bytes_used -= len;
1019 spin_lock(&root->fs_info->free_chunk_lock);
1020 root->fs_info->free_chunk_space += len;
1021 spin_unlock(&root->fs_info->free_chunk_lock);
1023 ret = btrfs_del_item(trans, root, path);
1026 btrfs_free_path(path);
1030 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1031 struct btrfs_device *device,
1032 u64 chunk_tree, u64 chunk_objectid,
1033 u64 chunk_offset, u64 start, u64 num_bytes)
1036 struct btrfs_path *path;
1037 struct btrfs_root *root = device->dev_root;
1038 struct btrfs_dev_extent *extent;
1039 struct extent_buffer *leaf;
1040 struct btrfs_key key;
1042 WARN_ON(!device->in_fs_metadata);
1043 path = btrfs_alloc_path();
1047 key.objectid = device->devid;
1049 key.type = BTRFS_DEV_EXTENT_KEY;
1050 ret = btrfs_insert_empty_item(trans, root, path, &key,
1054 leaf = path->nodes[0];
1055 extent = btrfs_item_ptr(leaf, path->slots[0],
1056 struct btrfs_dev_extent);
1057 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1058 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1059 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1061 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1062 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1065 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1066 btrfs_mark_buffer_dirty(leaf);
1067 btrfs_free_path(path);
1071 static noinline int find_next_chunk(struct btrfs_root *root,
1072 u64 objectid, u64 *offset)
1074 struct btrfs_path *path;
1076 struct btrfs_key key;
1077 struct btrfs_chunk *chunk;
1078 struct btrfs_key found_key;
1080 path = btrfs_alloc_path();
1084 key.objectid = objectid;
1085 key.offset = (u64)-1;
1086 key.type = BTRFS_CHUNK_ITEM_KEY;
1088 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1094 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1098 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1100 if (found_key.objectid != objectid)
1103 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1104 struct btrfs_chunk);
1105 *offset = found_key.offset +
1106 btrfs_chunk_length(path->nodes[0], chunk);
1111 btrfs_free_path(path);
1115 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1118 struct btrfs_key key;
1119 struct btrfs_key found_key;
1120 struct btrfs_path *path;
1122 root = root->fs_info->chunk_root;
1124 path = btrfs_alloc_path();
1128 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1129 key.type = BTRFS_DEV_ITEM_KEY;
1130 key.offset = (u64)-1;
1132 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1138 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1139 BTRFS_DEV_ITEM_KEY);
1143 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1145 *objectid = found_key.offset + 1;
1149 btrfs_free_path(path);
1154 * the device information is stored in the chunk root
1155 * the btrfs_device struct should be fully filled in
1157 int btrfs_add_device(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root,
1159 struct btrfs_device *device)
1162 struct btrfs_path *path;
1163 struct btrfs_dev_item *dev_item;
1164 struct extent_buffer *leaf;
1165 struct btrfs_key key;
1168 root = root->fs_info->chunk_root;
1170 path = btrfs_alloc_path();
1174 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1175 key.type = BTRFS_DEV_ITEM_KEY;
1176 key.offset = device->devid;
1178 ret = btrfs_insert_empty_item(trans, root, path, &key,
1183 leaf = path->nodes[0];
1184 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1186 btrfs_set_device_id(leaf, dev_item, device->devid);
1187 btrfs_set_device_generation(leaf, dev_item, 0);
1188 btrfs_set_device_type(leaf, dev_item, device->type);
1189 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1190 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1191 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1192 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1193 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1194 btrfs_set_device_group(leaf, dev_item, 0);
1195 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1196 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1197 btrfs_set_device_start_offset(leaf, dev_item, 0);
1199 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1200 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1201 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1202 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1203 btrfs_mark_buffer_dirty(leaf);
1207 btrfs_free_path(path);
1211 static int btrfs_rm_dev_item(struct btrfs_root *root,
1212 struct btrfs_device *device)
1215 struct btrfs_path *path;
1216 struct btrfs_key key;
1217 struct btrfs_trans_handle *trans;
1219 root = root->fs_info->chunk_root;
1221 path = btrfs_alloc_path();
1225 trans = btrfs_start_transaction(root, 0);
1226 if (IS_ERR(trans)) {
1227 btrfs_free_path(path);
1228 return PTR_ERR(trans);
1230 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1231 key.type = BTRFS_DEV_ITEM_KEY;
1232 key.offset = device->devid;
1235 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1244 ret = btrfs_del_item(trans, root, path);
1248 btrfs_free_path(path);
1249 unlock_chunks(root);
1250 btrfs_commit_transaction(trans, root);
1254 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1256 struct btrfs_device *device;
1257 struct btrfs_device *next_device;
1258 struct block_device *bdev;
1259 struct buffer_head *bh = NULL;
1260 struct btrfs_super_block *disk_super;
1261 struct btrfs_fs_devices *cur_devices;
1267 bool clear_super = false;
1269 mutex_lock(&uuid_mutex);
1270 mutex_lock(&root->fs_info->volume_mutex);
1272 all_avail = root->fs_info->avail_data_alloc_bits |
1273 root->fs_info->avail_system_alloc_bits |
1274 root->fs_info->avail_metadata_alloc_bits;
1276 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1277 root->fs_info->fs_devices->num_devices <= 4) {
1278 printk(KERN_ERR "btrfs: unable to go below four devices "
1284 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1285 root->fs_info->fs_devices->num_devices <= 2) {
1286 printk(KERN_ERR "btrfs: unable to go below two "
1287 "devices on raid1\n");
1292 if (strcmp(device_path, "missing") == 0) {
1293 struct list_head *devices;
1294 struct btrfs_device *tmp;
1297 devices = &root->fs_info->fs_devices->devices;
1299 * It is safe to read the devices since the volume_mutex
1302 list_for_each_entry(tmp, devices, dev_list) {
1303 if (tmp->in_fs_metadata && !tmp->bdev) {
1312 printk(KERN_ERR "btrfs: no missing devices found to "
1317 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1318 root->fs_info->bdev_holder);
1320 ret = PTR_ERR(bdev);
1324 set_blocksize(bdev, 4096);
1325 bh = btrfs_read_dev_super(bdev);
1330 disk_super = (struct btrfs_super_block *)bh->b_data;
1331 devid = btrfs_stack_device_id(&disk_super->dev_item);
1332 dev_uuid = disk_super->dev_item.uuid;
1333 device = btrfs_find_device(root, devid, dev_uuid,
1341 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1342 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1348 if (device->writeable) {
1350 list_del_init(&device->dev_alloc_list);
1351 unlock_chunks(root);
1352 root->fs_info->fs_devices->rw_devices--;
1356 ret = btrfs_shrink_device(device, 0);
1360 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1364 spin_lock(&root->fs_info->free_chunk_lock);
1365 root->fs_info->free_chunk_space = device->total_bytes -
1367 spin_unlock(&root->fs_info->free_chunk_lock);
1369 device->in_fs_metadata = 0;
1370 btrfs_scrub_cancel_dev(root, device);
1373 * the device list mutex makes sure that we don't change
1374 * the device list while someone else is writing out all
1375 * the device supers.
1378 cur_devices = device->fs_devices;
1379 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1380 list_del_rcu(&device->dev_list);
1382 device->fs_devices->num_devices--;
1384 if (device->missing)
1385 root->fs_info->fs_devices->missing_devices--;
1387 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1388 struct btrfs_device, dev_list);
1389 if (device->bdev == root->fs_info->sb->s_bdev)
1390 root->fs_info->sb->s_bdev = next_device->bdev;
1391 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1392 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1395 device->fs_devices->open_devices--;
1397 call_rcu(&device->rcu, free_device);
1398 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1400 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1401 btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1403 if (cur_devices->open_devices == 0) {
1404 struct btrfs_fs_devices *fs_devices;
1405 fs_devices = root->fs_info->fs_devices;
1406 while (fs_devices) {
1407 if (fs_devices->seed == cur_devices)
1409 fs_devices = fs_devices->seed;
1411 fs_devices->seed = cur_devices->seed;
1412 cur_devices->seed = NULL;
1414 __btrfs_close_devices(cur_devices);
1415 unlock_chunks(root);
1416 free_fs_devices(cur_devices);
1420 * at this point, the device is zero sized. We want to
1421 * remove it from the devices list and zero out the old super
1424 /* make sure this device isn't detected as part of
1427 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1428 set_buffer_dirty(bh);
1429 sync_dirty_buffer(bh);
1438 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1440 mutex_unlock(&root->fs_info->volume_mutex);
1441 mutex_unlock(&uuid_mutex);
1444 if (device->writeable) {
1446 list_add(&device->dev_alloc_list,
1447 &root->fs_info->fs_devices->alloc_list);
1448 unlock_chunks(root);
1449 root->fs_info->fs_devices->rw_devices++;
1455 * does all the dirty work required for changing file system's UUID.
1457 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1458 struct btrfs_root *root)
1460 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1461 struct btrfs_fs_devices *old_devices;
1462 struct btrfs_fs_devices *seed_devices;
1463 struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1464 struct btrfs_device *device;
1467 BUG_ON(!mutex_is_locked(&uuid_mutex));
1468 if (!fs_devices->seeding)
1471 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1475 old_devices = clone_fs_devices(fs_devices);
1476 if (IS_ERR(old_devices)) {
1477 kfree(seed_devices);
1478 return PTR_ERR(old_devices);
1481 list_add(&old_devices->list, &fs_uuids);
1483 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1484 seed_devices->opened = 1;
1485 INIT_LIST_HEAD(&seed_devices->devices);
1486 INIT_LIST_HEAD(&seed_devices->alloc_list);
1487 mutex_init(&seed_devices->device_list_mutex);
1489 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1490 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1492 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1494 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1495 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1496 device->fs_devices = seed_devices;
1499 fs_devices->seeding = 0;
1500 fs_devices->num_devices = 0;
1501 fs_devices->open_devices = 0;
1502 fs_devices->seed = seed_devices;
1504 generate_random_uuid(fs_devices->fsid);
1505 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1506 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1507 super_flags = btrfs_super_flags(disk_super) &
1508 ~BTRFS_SUPER_FLAG_SEEDING;
1509 btrfs_set_super_flags(disk_super, super_flags);
1515 * strore the expected generation for seed devices in device items.
1517 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1518 struct btrfs_root *root)
1520 struct btrfs_path *path;
1521 struct extent_buffer *leaf;
1522 struct btrfs_dev_item *dev_item;
1523 struct btrfs_device *device;
1524 struct btrfs_key key;
1525 u8 fs_uuid[BTRFS_UUID_SIZE];
1526 u8 dev_uuid[BTRFS_UUID_SIZE];
1530 path = btrfs_alloc_path();
1534 root = root->fs_info->chunk_root;
1535 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1537 key.type = BTRFS_DEV_ITEM_KEY;
1540 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1544 leaf = path->nodes[0];
1546 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1547 ret = btrfs_next_leaf(root, path);
1552 leaf = path->nodes[0];
1553 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1554 btrfs_release_path(path);
1558 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1559 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1560 key.type != BTRFS_DEV_ITEM_KEY)
1563 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1564 struct btrfs_dev_item);
1565 devid = btrfs_device_id(leaf, dev_item);
1566 read_extent_buffer(leaf, dev_uuid,
1567 (unsigned long)btrfs_device_uuid(dev_item),
1569 read_extent_buffer(leaf, fs_uuid,
1570 (unsigned long)btrfs_device_fsid(dev_item),
1572 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1575 if (device->fs_devices->seeding) {
1576 btrfs_set_device_generation(leaf, dev_item,
1577 device->generation);
1578 btrfs_mark_buffer_dirty(leaf);
1586 btrfs_free_path(path);
1590 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1592 struct request_queue *q;
1593 struct btrfs_trans_handle *trans;
1594 struct btrfs_device *device;
1595 struct block_device *bdev;
1596 struct list_head *devices;
1597 struct super_block *sb = root->fs_info->sb;
1599 int seeding_dev = 0;
1602 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1605 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1606 root->fs_info->bdev_holder);
1608 return PTR_ERR(bdev);
1610 if (root->fs_info->fs_devices->seeding) {
1612 down_write(&sb->s_umount);
1613 mutex_lock(&uuid_mutex);
1616 filemap_write_and_wait(bdev->bd_inode->i_mapping);
1617 mutex_lock(&root->fs_info->volume_mutex);
1619 devices = &root->fs_info->fs_devices->devices;
1621 * we have the volume lock, so we don't need the extra
1622 * device list mutex while reading the list here.
1624 list_for_each_entry(device, devices, dev_list) {
1625 if (device->bdev == bdev) {
1631 device = kzalloc(sizeof(*device), GFP_NOFS);
1633 /* we can safely leave the fs_devices entry around */
1638 device->name = kstrdup(device_path, GFP_NOFS);
1639 if (!device->name) {
1645 ret = find_next_devid(root, &device->devid);
1647 kfree(device->name);
1652 trans = btrfs_start_transaction(root, 0);
1653 if (IS_ERR(trans)) {
1654 kfree(device->name);
1656 ret = PTR_ERR(trans);
1662 q = bdev_get_queue(bdev);
1663 if (blk_queue_discard(q))
1664 device->can_discard = 1;
1665 device->writeable = 1;
1666 device->work.func = pending_bios_fn;
1667 generate_random_uuid(device->uuid);
1668 spin_lock_init(&device->io_lock);
1669 device->generation = trans->transid;
1670 device->io_width = root->sectorsize;
1671 device->io_align = root->sectorsize;
1672 device->sector_size = root->sectorsize;
1673 device->total_bytes = i_size_read(bdev->bd_inode);
1674 device->disk_total_bytes = device->total_bytes;
1675 device->dev_root = root->fs_info->dev_root;
1676 device->bdev = bdev;
1677 device->in_fs_metadata = 1;
1678 device->mode = FMODE_EXCL;
1679 set_blocksize(device->bdev, 4096);
1682 sb->s_flags &= ~MS_RDONLY;
1683 ret = btrfs_prepare_sprout(trans, root);
1687 device->fs_devices = root->fs_info->fs_devices;
1690 * we don't want write_supers to jump in here with our device
1693 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1694 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
1695 list_add(&device->dev_alloc_list,
1696 &root->fs_info->fs_devices->alloc_list);
1697 root->fs_info->fs_devices->num_devices++;
1698 root->fs_info->fs_devices->open_devices++;
1699 root->fs_info->fs_devices->rw_devices++;
1700 if (device->can_discard)
1701 root->fs_info->fs_devices->num_can_discard++;
1702 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1704 spin_lock(&root->fs_info->free_chunk_lock);
1705 root->fs_info->free_chunk_space += device->total_bytes;
1706 spin_unlock(&root->fs_info->free_chunk_lock);
1708 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1709 root->fs_info->fs_devices->rotating = 1;
1711 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1712 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1713 total_bytes + device->total_bytes);
1715 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1716 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1718 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1721 ret = init_first_rw_device(trans, root, device);
1723 ret = btrfs_finish_sprout(trans, root);
1726 ret = btrfs_add_device(trans, root, device);
1730 * we've got more storage, clear any full flags on the space
1733 btrfs_clear_space_info_full(root->fs_info);
1735 unlock_chunks(root);
1736 btrfs_commit_transaction(trans, root);
1739 mutex_unlock(&uuid_mutex);
1740 up_write(&sb->s_umount);
1742 ret = btrfs_relocate_sys_chunks(root);
1746 mutex_unlock(&root->fs_info->volume_mutex);
1749 blkdev_put(bdev, FMODE_EXCL);
1751 mutex_unlock(&uuid_mutex);
1752 up_write(&sb->s_umount);
1757 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1758 struct btrfs_device *device)
1761 struct btrfs_path *path;
1762 struct btrfs_root *root;
1763 struct btrfs_dev_item *dev_item;
1764 struct extent_buffer *leaf;
1765 struct btrfs_key key;
1767 root = device->dev_root->fs_info->chunk_root;
1769 path = btrfs_alloc_path();
1773 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1774 key.type = BTRFS_DEV_ITEM_KEY;
1775 key.offset = device->devid;
1777 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1786 leaf = path->nodes[0];
1787 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1789 btrfs_set_device_id(leaf, dev_item, device->devid);
1790 btrfs_set_device_type(leaf, dev_item, device->type);
1791 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1792 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1793 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1794 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1795 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1796 btrfs_mark_buffer_dirty(leaf);
1799 btrfs_free_path(path);
1803 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1804 struct btrfs_device *device, u64 new_size)
1806 struct btrfs_super_block *super_copy =
1807 &device->dev_root->fs_info->super_copy;
1808 u64 old_total = btrfs_super_total_bytes(super_copy);
1809 u64 diff = new_size - device->total_bytes;
1811 if (!device->writeable)
1813 if (new_size <= device->total_bytes)
1816 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1817 device->fs_devices->total_rw_bytes += diff;
1819 device->total_bytes = new_size;
1820 device->disk_total_bytes = new_size;
1821 btrfs_clear_space_info_full(device->dev_root->fs_info);
1823 return btrfs_update_device(trans, device);
1826 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1827 struct btrfs_device *device, u64 new_size)
1830 lock_chunks(device->dev_root);
1831 ret = __btrfs_grow_device(trans, device, new_size);
1832 unlock_chunks(device->dev_root);
1836 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1837 struct btrfs_root *root,
1838 u64 chunk_tree, u64 chunk_objectid,
1842 struct btrfs_path *path;
1843 struct btrfs_key key;
1845 root = root->fs_info->chunk_root;
1846 path = btrfs_alloc_path();
1850 key.objectid = chunk_objectid;
1851 key.offset = chunk_offset;
1852 key.type = BTRFS_CHUNK_ITEM_KEY;
1854 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1857 ret = btrfs_del_item(trans, root, path);
1859 btrfs_free_path(path);
1863 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1866 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1867 struct btrfs_disk_key *disk_key;
1868 struct btrfs_chunk *chunk;
1875 struct btrfs_key key;
1877 array_size = btrfs_super_sys_array_size(super_copy);
1879 ptr = super_copy->sys_chunk_array;
1882 while (cur < array_size) {
1883 disk_key = (struct btrfs_disk_key *)ptr;
1884 btrfs_disk_key_to_cpu(&key, disk_key);
1886 len = sizeof(*disk_key);
1888 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1889 chunk = (struct btrfs_chunk *)(ptr + len);
1890 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1891 len += btrfs_chunk_item_size(num_stripes);
1896 if (key.objectid == chunk_objectid &&
1897 key.offset == chunk_offset) {
1898 memmove(ptr, ptr + len, array_size - (cur + len));
1900 btrfs_set_super_sys_array_size(super_copy, array_size);
1909 static int btrfs_relocate_chunk(struct btrfs_root *root,
1910 u64 chunk_tree, u64 chunk_objectid,
1913 struct extent_map_tree *em_tree;
1914 struct btrfs_root *extent_root;
1915 struct btrfs_trans_handle *trans;
1916 struct extent_map *em;
1917 struct map_lookup *map;
1921 root = root->fs_info->chunk_root;
1922 extent_root = root->fs_info->extent_root;
1923 em_tree = &root->fs_info->mapping_tree.map_tree;
1925 ret = btrfs_can_relocate(extent_root, chunk_offset);
1929 /* step one, relocate all the extents inside this chunk */
1930 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1934 trans = btrfs_start_transaction(root, 0);
1935 BUG_ON(IS_ERR(trans));
1940 * step two, delete the device extents and the
1941 * chunk tree entries
1943 read_lock(&em_tree->lock);
1944 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1945 read_unlock(&em_tree->lock);
1947 BUG_ON(em->start > chunk_offset ||
1948 em->start + em->len < chunk_offset);
1949 map = (struct map_lookup *)em->bdev;
1951 for (i = 0; i < map->num_stripes; i++) {
1952 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1953 map->stripes[i].physical);
1956 if (map->stripes[i].dev) {
1957 ret = btrfs_update_device(trans, map->stripes[i].dev);
1961 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1966 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1968 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1969 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1973 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1976 write_lock(&em_tree->lock);
1977 remove_extent_mapping(em_tree, em);
1978 write_unlock(&em_tree->lock);
1983 /* once for the tree */
1984 free_extent_map(em);
1986 free_extent_map(em);
1988 unlock_chunks(root);
1989 btrfs_end_transaction(trans, root);
1993 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1995 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1996 struct btrfs_path *path;
1997 struct extent_buffer *leaf;
1998 struct btrfs_chunk *chunk;
1999 struct btrfs_key key;
2000 struct btrfs_key found_key;
2001 u64 chunk_tree = chunk_root->root_key.objectid;
2003 bool retried = false;
2007 path = btrfs_alloc_path();
2012 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2013 key.offset = (u64)-1;
2014 key.type = BTRFS_CHUNK_ITEM_KEY;
2017 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2022 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2029 leaf = path->nodes[0];
2030 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2032 chunk = btrfs_item_ptr(leaf, path->slots[0],
2033 struct btrfs_chunk);
2034 chunk_type = btrfs_chunk_type(leaf, chunk);
2035 btrfs_release_path(path);
2037 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2038 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2047 if (found_key.offset == 0)
2049 key.offset = found_key.offset - 1;
2052 if (failed && !retried) {
2056 } else if (failed && retried) {
2061 btrfs_free_path(path);
2065 static u64 div_factor(u64 num, int factor)
2074 int btrfs_balance(struct btrfs_root *dev_root)
2077 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2078 struct btrfs_device *device;
2081 struct btrfs_path *path;
2082 struct btrfs_key key;
2083 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2084 struct btrfs_trans_handle *trans;
2085 struct btrfs_key found_key;
2087 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2090 if (!capable(CAP_SYS_ADMIN))
2093 mutex_lock(&dev_root->fs_info->volume_mutex);
2094 dev_root = dev_root->fs_info->dev_root;
2096 /* step one make some room on all the devices */
2097 list_for_each_entry(device, devices, dev_list) {
2098 old_size = device->total_bytes;
2099 size_to_free = div_factor(old_size, 1);
2100 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2101 if (!device->writeable ||
2102 device->total_bytes - device->bytes_used > size_to_free)
2105 ret = btrfs_shrink_device(device, old_size - size_to_free);
2110 trans = btrfs_start_transaction(dev_root, 0);
2111 BUG_ON(IS_ERR(trans));
2113 ret = btrfs_grow_device(trans, device, old_size);
2116 btrfs_end_transaction(trans, dev_root);
2119 /* step two, relocate all the chunks */
2120 path = btrfs_alloc_path();
2125 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2126 key.offset = (u64)-1;
2127 key.type = BTRFS_CHUNK_ITEM_KEY;
2130 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2135 * this shouldn't happen, it means the last relocate
2141 ret = btrfs_previous_item(chunk_root, path, 0,
2142 BTRFS_CHUNK_ITEM_KEY);
2146 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2148 if (found_key.objectid != key.objectid)
2151 /* chunk zero is special */
2152 if (found_key.offset == 0)
2155 btrfs_release_path(path);
2156 ret = btrfs_relocate_chunk(chunk_root,
2157 chunk_root->root_key.objectid,
2160 if (ret && ret != -ENOSPC)
2162 key.offset = found_key.offset - 1;
2166 btrfs_free_path(path);
2167 mutex_unlock(&dev_root->fs_info->volume_mutex);
2172 * shrinking a device means finding all of the device extents past
2173 * the new size, and then following the back refs to the chunks.
2174 * The chunk relocation code actually frees the device extent
2176 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2178 struct btrfs_trans_handle *trans;
2179 struct btrfs_root *root = device->dev_root;
2180 struct btrfs_dev_extent *dev_extent = NULL;
2181 struct btrfs_path *path;
2189 bool retried = false;
2190 struct extent_buffer *l;
2191 struct btrfs_key key;
2192 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2193 u64 old_total = btrfs_super_total_bytes(super_copy);
2194 u64 old_size = device->total_bytes;
2195 u64 diff = device->total_bytes - new_size;
2197 if (new_size >= device->total_bytes)
2200 path = btrfs_alloc_path();
2208 device->total_bytes = new_size;
2209 if (device->writeable) {
2210 device->fs_devices->total_rw_bytes -= diff;
2211 spin_lock(&root->fs_info->free_chunk_lock);
2212 root->fs_info->free_chunk_space -= diff;
2213 spin_unlock(&root->fs_info->free_chunk_lock);
2215 unlock_chunks(root);
2218 key.objectid = device->devid;
2219 key.offset = (u64)-1;
2220 key.type = BTRFS_DEV_EXTENT_KEY;
2223 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2227 ret = btrfs_previous_item(root, path, 0, key.type);
2232 btrfs_release_path(path);
2237 slot = path->slots[0];
2238 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2240 if (key.objectid != device->devid) {
2241 btrfs_release_path(path);
2245 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2246 length = btrfs_dev_extent_length(l, dev_extent);
2248 if (key.offset + length <= new_size) {
2249 btrfs_release_path(path);
2253 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2254 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2255 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2256 btrfs_release_path(path);
2258 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2260 if (ret && ret != -ENOSPC)
2267 if (failed && !retried) {
2271 } else if (failed && retried) {
2275 device->total_bytes = old_size;
2276 if (device->writeable)
2277 device->fs_devices->total_rw_bytes += diff;
2278 spin_lock(&root->fs_info->free_chunk_lock);
2279 root->fs_info->free_chunk_space += diff;
2280 spin_unlock(&root->fs_info->free_chunk_lock);
2281 unlock_chunks(root);
2285 /* Shrinking succeeded, else we would be at "done". */
2286 trans = btrfs_start_transaction(root, 0);
2287 if (IS_ERR(trans)) {
2288 ret = PTR_ERR(trans);
2294 device->disk_total_bytes = new_size;
2295 /* Now btrfs_update_device() will change the on-disk size. */
2296 ret = btrfs_update_device(trans, device);
2298 unlock_chunks(root);
2299 btrfs_end_transaction(trans, root);
2302 WARN_ON(diff > old_total);
2303 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2304 unlock_chunks(root);
2305 btrfs_end_transaction(trans, root);
2307 btrfs_free_path(path);
2311 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2312 struct btrfs_root *root,
2313 struct btrfs_key *key,
2314 struct btrfs_chunk *chunk, int item_size)
2316 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2317 struct btrfs_disk_key disk_key;
2321 array_size = btrfs_super_sys_array_size(super_copy);
2322 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2325 ptr = super_copy->sys_chunk_array + array_size;
2326 btrfs_cpu_key_to_disk(&disk_key, key);
2327 memcpy(ptr, &disk_key, sizeof(disk_key));
2328 ptr += sizeof(disk_key);
2329 memcpy(ptr, chunk, item_size);
2330 item_size += sizeof(disk_key);
2331 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2336 * sort the devices in descending order by max_avail, total_avail
2338 static int btrfs_cmp_device_info(const void *a, const void *b)
2340 const struct btrfs_device_info *di_a = a;
2341 const struct btrfs_device_info *di_b = b;
2343 if (di_a->max_avail > di_b->max_avail)
2345 if (di_a->max_avail < di_b->max_avail)
2347 if (di_a->total_avail > di_b->total_avail)
2349 if (di_a->total_avail < di_b->total_avail)
2354 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2355 struct btrfs_root *extent_root,
2356 struct map_lookup **map_ret,
2357 u64 *num_bytes_out, u64 *stripe_size_out,
2358 u64 start, u64 type)
2360 struct btrfs_fs_info *info = extent_root->fs_info;
2361 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2362 struct list_head *cur;
2363 struct map_lookup *map = NULL;
2364 struct extent_map_tree *em_tree;
2365 struct extent_map *em;
2366 struct btrfs_device_info *devices_info = NULL;
2368 int num_stripes; /* total number of stripes to allocate */
2369 int sub_stripes; /* sub_stripes info for map */
2370 int dev_stripes; /* stripes per dev */
2371 int devs_max; /* max devs to use */
2372 int devs_min; /* min devs needed */
2373 int devs_increment; /* ndevs has to be a multiple of this */
2374 int ncopies; /* how many copies to data has */
2376 u64 max_stripe_size;
2384 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2385 (type & BTRFS_BLOCK_GROUP_DUP)) {
2387 type &= ~BTRFS_BLOCK_GROUP_DUP;
2390 if (list_empty(&fs_devices->alloc_list))
2397 devs_max = 0; /* 0 == as many as possible */
2401 * define the properties of each RAID type.
2402 * FIXME: move this to a global table and use it in all RAID
2405 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2409 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2411 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2416 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2425 if (type & BTRFS_BLOCK_GROUP_DATA) {
2426 max_stripe_size = 1024 * 1024 * 1024;
2427 max_chunk_size = 10 * max_stripe_size;
2428 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2429 max_stripe_size = 256 * 1024 * 1024;
2430 max_chunk_size = max_stripe_size;
2431 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2432 max_stripe_size = 8 * 1024 * 1024;
2433 max_chunk_size = 2 * max_stripe_size;
2435 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2440 /* we don't want a chunk larger than 10% of writeable space */
2441 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2444 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2449 cur = fs_devices->alloc_list.next;
2452 * in the first pass through the devices list, we gather information
2453 * about the available holes on each device.
2456 while (cur != &fs_devices->alloc_list) {
2457 struct btrfs_device *device;
2461 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2465 if (!device->writeable) {
2467 "btrfs: read-only device in alloc_list\n");
2472 if (!device->in_fs_metadata)
2475 if (device->total_bytes > device->bytes_used)
2476 total_avail = device->total_bytes - device->bytes_used;
2480 /* If there is no space on this device, skip it. */
2481 if (total_avail == 0)
2484 ret = find_free_dev_extent(trans, device,
2485 max_stripe_size * dev_stripes,
2486 &dev_offset, &max_avail);
2487 if (ret && ret != -ENOSPC)
2491 max_avail = max_stripe_size * dev_stripes;
2493 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2496 devices_info[ndevs].dev_offset = dev_offset;
2497 devices_info[ndevs].max_avail = max_avail;
2498 devices_info[ndevs].total_avail = total_avail;
2499 devices_info[ndevs].dev = device;
2504 * now sort the devices by hole size / available space
2506 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2507 btrfs_cmp_device_info, NULL);
2509 /* round down to number of usable stripes */
2510 ndevs -= ndevs % devs_increment;
2512 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2517 if (devs_max && ndevs > devs_max)
2520 * the primary goal is to maximize the number of stripes, so use as many
2521 * devices as possible, even if the stripes are not maximum sized.
2523 stripe_size = devices_info[ndevs-1].max_avail;
2524 num_stripes = ndevs * dev_stripes;
2526 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2527 stripe_size = max_chunk_size * ncopies;
2528 do_div(stripe_size, num_stripes);
2531 do_div(stripe_size, dev_stripes);
2532 do_div(stripe_size, BTRFS_STRIPE_LEN);
2533 stripe_size *= BTRFS_STRIPE_LEN;
2535 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2540 map->num_stripes = num_stripes;
2542 for (i = 0; i < ndevs; ++i) {
2543 for (j = 0; j < dev_stripes; ++j) {
2544 int s = i * dev_stripes + j;
2545 map->stripes[s].dev = devices_info[i].dev;
2546 map->stripes[s].physical = devices_info[i].dev_offset +
2550 map->sector_size = extent_root->sectorsize;
2551 map->stripe_len = BTRFS_STRIPE_LEN;
2552 map->io_align = BTRFS_STRIPE_LEN;
2553 map->io_width = BTRFS_STRIPE_LEN;
2555 map->sub_stripes = sub_stripes;
2558 num_bytes = stripe_size * (num_stripes / ncopies);
2560 *stripe_size_out = stripe_size;
2561 *num_bytes_out = num_bytes;
2563 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
2565 em = alloc_extent_map();
2570 em->bdev = (struct block_device *)map;
2572 em->len = num_bytes;
2573 em->block_start = 0;
2574 em->block_len = em->len;
2576 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2577 write_lock(&em_tree->lock);
2578 ret = add_extent_mapping(em_tree, em);
2579 write_unlock(&em_tree->lock);
2581 free_extent_map(em);
2583 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2584 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2588 for (i = 0; i < map->num_stripes; ++i) {
2589 struct btrfs_device *device;
2592 device = map->stripes[i].dev;
2593 dev_offset = map->stripes[i].physical;
2595 ret = btrfs_alloc_dev_extent(trans, device,
2596 info->chunk_root->root_key.objectid,
2597 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2598 start, dev_offset, stripe_size);
2602 kfree(devices_info);
2607 kfree(devices_info);
2611 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2612 struct btrfs_root *extent_root,
2613 struct map_lookup *map, u64 chunk_offset,
2614 u64 chunk_size, u64 stripe_size)
2617 struct btrfs_key key;
2618 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2619 struct btrfs_device *device;
2620 struct btrfs_chunk *chunk;
2621 struct btrfs_stripe *stripe;
2622 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2626 chunk = kzalloc(item_size, GFP_NOFS);
2631 while (index < map->num_stripes) {
2632 device = map->stripes[index].dev;
2633 device->bytes_used += stripe_size;
2634 ret = btrfs_update_device(trans, device);
2639 spin_lock(&extent_root->fs_info->free_chunk_lock);
2640 extent_root->fs_info->free_chunk_space -= (stripe_size *
2642 spin_unlock(&extent_root->fs_info->free_chunk_lock);
2645 stripe = &chunk->stripe;
2646 while (index < map->num_stripes) {
2647 device = map->stripes[index].dev;
2648 dev_offset = map->stripes[index].physical;
2650 btrfs_set_stack_stripe_devid(stripe, device->devid);
2651 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2652 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2657 btrfs_set_stack_chunk_length(chunk, chunk_size);
2658 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2659 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2660 btrfs_set_stack_chunk_type(chunk, map->type);
2661 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2662 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2663 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2664 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2665 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2667 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2668 key.type = BTRFS_CHUNK_ITEM_KEY;
2669 key.offset = chunk_offset;
2671 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2674 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2675 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2685 * Chunk allocation falls into two parts. The first part does works
2686 * that make the new allocated chunk useable, but not do any operation
2687 * that modifies the chunk tree. The second part does the works that
2688 * require modifying the chunk tree. This division is important for the
2689 * bootstrap process of adding storage to a seed btrfs.
2691 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *extent_root, u64 type)
2697 struct map_lookup *map;
2698 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2701 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2706 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2707 &stripe_size, chunk_offset, type);
2711 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2712 chunk_size, stripe_size);
2717 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2718 struct btrfs_root *root,
2719 struct btrfs_device *device)
2722 u64 sys_chunk_offset;
2726 u64 sys_stripe_size;
2728 struct map_lookup *map;
2729 struct map_lookup *sys_map;
2730 struct btrfs_fs_info *fs_info = root->fs_info;
2731 struct btrfs_root *extent_root = fs_info->extent_root;
2734 ret = find_next_chunk(fs_info->chunk_root,
2735 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2739 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2740 (fs_info->metadata_alloc_profile &
2741 fs_info->avail_metadata_alloc_bits);
2742 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2744 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2745 &stripe_size, chunk_offset, alloc_profile);
2748 sys_chunk_offset = chunk_offset + chunk_size;
2750 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2751 (fs_info->system_alloc_profile &
2752 fs_info->avail_system_alloc_bits);
2753 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2755 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2756 &sys_chunk_size, &sys_stripe_size,
2757 sys_chunk_offset, alloc_profile);
2760 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2764 * Modifying chunk tree needs allocating new blocks from both
2765 * system block group and metadata block group. So we only can
2766 * do operations require modifying the chunk tree after both
2767 * block groups were created.
2769 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2770 chunk_size, stripe_size);
2773 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2774 sys_chunk_offset, sys_chunk_size,
2780 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2782 struct extent_map *em;
2783 struct map_lookup *map;
2784 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2788 read_lock(&map_tree->map_tree.lock);
2789 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2790 read_unlock(&map_tree->map_tree.lock);
2794 if (btrfs_test_opt(root, DEGRADED)) {
2795 free_extent_map(em);
2799 map = (struct map_lookup *)em->bdev;
2800 for (i = 0; i < map->num_stripes; i++) {
2801 if (!map->stripes[i].dev->writeable) {
2806 free_extent_map(em);
2810 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2812 extent_map_tree_init(&tree->map_tree);
2815 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2817 struct extent_map *em;
2820 write_lock(&tree->map_tree.lock);
2821 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2823 remove_extent_mapping(&tree->map_tree, em);
2824 write_unlock(&tree->map_tree.lock);
2829 free_extent_map(em);
2830 /* once for the tree */
2831 free_extent_map(em);
2835 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2837 struct extent_map *em;
2838 struct map_lookup *map;
2839 struct extent_map_tree *em_tree = &map_tree->map_tree;
2842 read_lock(&em_tree->lock);
2843 em = lookup_extent_mapping(em_tree, logical, len);
2844 read_unlock(&em_tree->lock);
2847 BUG_ON(em->start > logical || em->start + em->len < logical);
2848 map = (struct map_lookup *)em->bdev;
2849 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2850 ret = map->num_stripes;
2851 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2852 ret = map->sub_stripes;
2855 free_extent_map(em);
2859 static int find_live_mirror(struct map_lookup *map, int first, int num,
2863 if (map->stripes[optimal].dev->bdev)
2865 for (i = first; i < first + num; i++) {
2866 if (map->stripes[i].dev->bdev)
2869 /* we couldn't find one that doesn't fail. Just return something
2870 * and the io error handling code will clean up eventually
2875 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2876 u64 logical, u64 *length,
2877 struct btrfs_multi_bio **multi_ret,
2880 struct extent_map *em;
2881 struct map_lookup *map;
2882 struct extent_map_tree *em_tree = &map_tree->map_tree;
2885 u64 stripe_end_offset;
2889 int stripes_allocated = 8;
2890 int stripes_required = 1;
2895 struct btrfs_multi_bio *multi = NULL;
2897 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
2898 stripes_allocated = 1;
2901 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2906 atomic_set(&multi->error, 0);
2909 read_lock(&em_tree->lock);
2910 em = lookup_extent_mapping(em_tree, logical, *length);
2911 read_unlock(&em_tree->lock);
2914 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2915 (unsigned long long)logical,
2916 (unsigned long long)*length);
2920 BUG_ON(em->start > logical || em->start + em->len < logical);
2921 map = (struct map_lookup *)em->bdev;
2922 offset = logical - em->start;
2924 if (mirror_num > map->num_stripes)
2927 /* if our multi bio struct is too small, back off and try again */
2928 if (rw & REQ_WRITE) {
2929 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2930 BTRFS_BLOCK_GROUP_DUP)) {
2931 stripes_required = map->num_stripes;
2933 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2934 stripes_required = map->sub_stripes;
2938 if (rw & REQ_DISCARD) {
2939 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2940 BTRFS_BLOCK_GROUP_RAID1 |
2941 BTRFS_BLOCK_GROUP_DUP |
2942 BTRFS_BLOCK_GROUP_RAID10)) {
2943 stripes_required = map->num_stripes;
2946 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
2947 stripes_allocated < stripes_required) {
2948 stripes_allocated = map->num_stripes;
2949 free_extent_map(em);
2955 * stripe_nr counts the total number of stripes we have to stride
2956 * to get to this block
2958 do_div(stripe_nr, map->stripe_len);
2960 stripe_offset = stripe_nr * map->stripe_len;
2961 BUG_ON(offset < stripe_offset);
2963 /* stripe_offset is the offset of this block in its stripe*/
2964 stripe_offset = offset - stripe_offset;
2966 if (rw & REQ_DISCARD)
2967 *length = min_t(u64, em->len - offset, *length);
2968 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2969 BTRFS_BLOCK_GROUP_RAID1 |
2970 BTRFS_BLOCK_GROUP_RAID10 |
2971 BTRFS_BLOCK_GROUP_DUP)) {
2972 /* we limit the length of each bio to what fits in a stripe */
2973 *length = min_t(u64, em->len - offset,
2974 map->stripe_len - stripe_offset);
2976 *length = em->len - offset;
2984 stripe_nr_orig = stripe_nr;
2985 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2986 (~(map->stripe_len - 1));
2987 do_div(stripe_nr_end, map->stripe_len);
2988 stripe_end_offset = stripe_nr_end * map->stripe_len -
2990 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2991 if (rw & REQ_DISCARD)
2992 num_stripes = min_t(u64, map->num_stripes,
2993 stripe_nr_end - stripe_nr_orig);
2994 stripe_index = do_div(stripe_nr, map->num_stripes);
2995 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2996 if (rw & (REQ_WRITE | REQ_DISCARD))
2997 num_stripes = map->num_stripes;
2998 else if (mirror_num)
2999 stripe_index = mirror_num - 1;
3001 stripe_index = find_live_mirror(map, 0,
3003 current->pid % map->num_stripes);
3006 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
3007 if (rw & (REQ_WRITE | REQ_DISCARD))
3008 num_stripes = map->num_stripes;
3009 else if (mirror_num)
3010 stripe_index = mirror_num - 1;
3012 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3013 int factor = map->num_stripes / map->sub_stripes;
3015 stripe_index = do_div(stripe_nr, factor);
3016 stripe_index *= map->sub_stripes;
3019 num_stripes = map->sub_stripes;
3020 else if (rw & REQ_DISCARD)
3021 num_stripes = min_t(u64, map->sub_stripes *
3022 (stripe_nr_end - stripe_nr_orig),
3024 else if (mirror_num)
3025 stripe_index += mirror_num - 1;
3027 stripe_index = find_live_mirror(map, stripe_index,
3028 map->sub_stripes, stripe_index +
3029 current->pid % map->sub_stripes);
3033 * after this do_div call, stripe_nr is the number of stripes
3034 * on this device we have to walk to find the data, and
3035 * stripe_index is the number of our device in the stripe array
3037 stripe_index = do_div(stripe_nr, map->num_stripes);
3039 BUG_ON(stripe_index >= map->num_stripes);
3041 if (rw & REQ_DISCARD) {
3042 for (i = 0; i < num_stripes; i++) {
3043 multi->stripes[i].physical =
3044 map->stripes[stripe_index].physical +
3045 stripe_offset + stripe_nr * map->stripe_len;
3046 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3048 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3050 u32 last_stripe = 0;
3053 div_u64_rem(stripe_nr_end - 1,
3057 for (j = 0; j < map->num_stripes; j++) {
3060 div_u64_rem(stripe_nr_end - 1 - j,
3061 map->num_stripes, &test);
3062 if (test == stripe_index)
3065 stripes = stripe_nr_end - 1 - j;
3066 do_div(stripes, map->num_stripes);
3067 multi->stripes[i].length = map->stripe_len *
3068 (stripes - stripe_nr + 1);
3071 multi->stripes[i].length -=
3075 if (stripe_index == last_stripe)
3076 multi->stripes[i].length -=
3078 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3081 int factor = map->num_stripes /
3083 u32 last_stripe = 0;
3085 div_u64_rem(stripe_nr_end - 1,
3086 factor, &last_stripe);
3087 last_stripe *= map->sub_stripes;
3089 for (j = 0; j < factor; j++) {
3092 div_u64_rem(stripe_nr_end - 1 - j,
3096 stripe_index / map->sub_stripes)
3099 stripes = stripe_nr_end - 1 - j;
3100 do_div(stripes, factor);
3101 multi->stripes[i].length = map->stripe_len *
3102 (stripes - stripe_nr + 1);
3104 if (i < map->sub_stripes) {
3105 multi->stripes[i].length -=
3107 if (i == map->sub_stripes - 1)
3110 if (stripe_index >= last_stripe &&
3111 stripe_index <= (last_stripe +
3112 map->sub_stripes - 1)) {
3113 multi->stripes[i].length -=
3117 multi->stripes[i].length = *length;
3120 if (stripe_index == map->num_stripes) {
3121 /* This could only happen for RAID0/10 */
3127 for (i = 0; i < num_stripes; i++) {
3128 multi->stripes[i].physical =
3129 map->stripes[stripe_index].physical +
3131 stripe_nr * map->stripe_len;
3132 multi->stripes[i].dev =
3133 map->stripes[stripe_index].dev;
3139 multi->num_stripes = num_stripes;
3140 multi->max_errors = max_errors;
3143 free_extent_map(em);
3147 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3148 u64 logical, u64 *length,
3149 struct btrfs_multi_bio **multi_ret, int mirror_num)
3151 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3155 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3156 u64 chunk_start, u64 physical, u64 devid,
3157 u64 **logical, int *naddrs, int *stripe_len)
3159 struct extent_map_tree *em_tree = &map_tree->map_tree;
3160 struct extent_map *em;
3161 struct map_lookup *map;
3168 read_lock(&em_tree->lock);
3169 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3170 read_unlock(&em_tree->lock);
3172 BUG_ON(!em || em->start != chunk_start);
3173 map = (struct map_lookup *)em->bdev;
3176 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3177 do_div(length, map->num_stripes / map->sub_stripes);
3178 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3179 do_div(length, map->num_stripes);
3181 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3184 for (i = 0; i < map->num_stripes; i++) {
3185 if (devid && map->stripes[i].dev->devid != devid)
3187 if (map->stripes[i].physical > physical ||
3188 map->stripes[i].physical + length <= physical)
3191 stripe_nr = physical - map->stripes[i].physical;
3192 do_div(stripe_nr, map->stripe_len);
3194 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3195 stripe_nr = stripe_nr * map->num_stripes + i;
3196 do_div(stripe_nr, map->sub_stripes);
3197 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3198 stripe_nr = stripe_nr * map->num_stripes + i;
3200 bytenr = chunk_start + stripe_nr * map->stripe_len;
3201 WARN_ON(nr >= map->num_stripes);
3202 for (j = 0; j < nr; j++) {
3203 if (buf[j] == bytenr)
3207 WARN_ON(nr >= map->num_stripes);
3214 *stripe_len = map->stripe_len;
3216 free_extent_map(em);
3220 static void end_bio_multi_stripe(struct bio *bio, int err)
3222 struct btrfs_multi_bio *multi = bio->bi_private;
3223 int is_orig_bio = 0;
3226 atomic_inc(&multi->error);
3228 if (bio == multi->orig_bio)
3231 if (atomic_dec_and_test(&multi->stripes_pending)) {
3234 bio = multi->orig_bio;
3236 bio->bi_private = multi->private;
3237 bio->bi_end_io = multi->end_io;
3238 /* only send an error to the higher layers if it is
3239 * beyond the tolerance of the multi-bio
3241 if (atomic_read(&multi->error) > multi->max_errors) {
3245 * this bio is actually up to date, we didn't
3246 * go over the max number of errors
3248 set_bit(BIO_UPTODATE, &bio->bi_flags);
3253 bio_endio(bio, err);
3254 } else if (!is_orig_bio) {
3259 struct async_sched {
3262 struct btrfs_fs_info *info;
3263 struct btrfs_work work;
3267 * see run_scheduled_bios for a description of why bios are collected for
3270 * This will add one bio to the pending list for a device and make sure
3271 * the work struct is scheduled.
3273 static noinline int schedule_bio(struct btrfs_root *root,
3274 struct btrfs_device *device,
3275 int rw, struct bio *bio)
3277 int should_queue = 1;
3278 struct btrfs_pending_bios *pending_bios;
3280 /* don't bother with additional async steps for reads, right now */
3281 if (!(rw & REQ_WRITE)) {
3283 submit_bio(rw, bio);
3289 * nr_async_bios allows us to reliably return congestion to the
3290 * higher layers. Otherwise, the async bio makes it appear we have
3291 * made progress against dirty pages when we've really just put it
3292 * on a queue for later
3294 atomic_inc(&root->fs_info->nr_async_bios);
3295 WARN_ON(bio->bi_next);
3296 bio->bi_next = NULL;
3299 spin_lock(&device->io_lock);
3300 if (bio->bi_rw & REQ_SYNC)
3301 pending_bios = &device->pending_sync_bios;
3303 pending_bios = &device->pending_bios;
3305 if (pending_bios->tail)
3306 pending_bios->tail->bi_next = bio;
3308 pending_bios->tail = bio;
3309 if (!pending_bios->head)
3310 pending_bios->head = bio;
3311 if (device->running_pending)
3314 spin_unlock(&device->io_lock);
3317 btrfs_queue_worker(&root->fs_info->submit_workers,
3322 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3323 int mirror_num, int async_submit)
3325 struct btrfs_mapping_tree *map_tree;
3326 struct btrfs_device *dev;
3327 struct bio *first_bio = bio;
3328 u64 logical = (u64)bio->bi_sector << 9;
3331 struct btrfs_multi_bio *multi = NULL;
3336 length = bio->bi_size;
3337 map_tree = &root->fs_info->mapping_tree;
3338 map_length = length;
3340 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3344 total_devs = multi->num_stripes;
3345 if (map_length < length) {
3346 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3347 "len %llu\n", (unsigned long long)logical,
3348 (unsigned long long)length,
3349 (unsigned long long)map_length);
3352 multi->end_io = first_bio->bi_end_io;
3353 multi->private = first_bio->bi_private;
3354 multi->orig_bio = first_bio;
3355 atomic_set(&multi->stripes_pending, multi->num_stripes);
3357 while (dev_nr < total_devs) {
3358 if (total_devs > 1) {
3359 if (dev_nr < total_devs - 1) {
3360 bio = bio_clone(first_bio, GFP_NOFS);
3365 bio->bi_private = multi;
3366 bio->bi_end_io = end_bio_multi_stripe;
3368 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3369 dev = multi->stripes[dev_nr].dev;
3370 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3371 bio->bi_bdev = dev->bdev;
3373 schedule_bio(root, dev, rw, bio);
3375 submit_bio(rw, bio);
3377 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3378 bio->bi_sector = logical >> 9;
3379 bio_endio(bio, -EIO);
3383 if (total_devs == 1)
3388 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3391 struct btrfs_device *device;
3392 struct btrfs_fs_devices *cur_devices;
3394 cur_devices = root->fs_info->fs_devices;
3395 while (cur_devices) {
3397 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3398 device = __find_device(&cur_devices->devices,
3403 cur_devices = cur_devices->seed;
3408 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3409 u64 devid, u8 *dev_uuid)
3411 struct btrfs_device *device;
3412 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3414 device = kzalloc(sizeof(*device), GFP_NOFS);
3417 list_add(&device->dev_list,
3418 &fs_devices->devices);
3419 device->dev_root = root->fs_info->dev_root;
3420 device->devid = devid;
3421 device->work.func = pending_bios_fn;
3422 device->fs_devices = fs_devices;
3423 device->missing = 1;
3424 fs_devices->num_devices++;
3425 fs_devices->missing_devices++;
3426 spin_lock_init(&device->io_lock);
3427 INIT_LIST_HEAD(&device->dev_alloc_list);
3428 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3432 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3433 struct extent_buffer *leaf,
3434 struct btrfs_chunk *chunk)
3436 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3437 struct map_lookup *map;
3438 struct extent_map *em;
3442 u8 uuid[BTRFS_UUID_SIZE];
3447 logical = key->offset;
3448 length = btrfs_chunk_length(leaf, chunk);
3450 read_lock(&map_tree->map_tree.lock);
3451 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3452 read_unlock(&map_tree->map_tree.lock);
3454 /* already mapped? */
3455 if (em && em->start <= logical && em->start + em->len > logical) {
3456 free_extent_map(em);
3459 free_extent_map(em);
3462 em = alloc_extent_map();
3465 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3466 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3468 free_extent_map(em);
3472 em->bdev = (struct block_device *)map;
3473 em->start = logical;
3475 em->block_start = 0;
3476 em->block_len = em->len;
3478 map->num_stripes = num_stripes;
3479 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3480 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3481 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3482 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3483 map->type = btrfs_chunk_type(leaf, chunk);
3484 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3485 for (i = 0; i < num_stripes; i++) {
3486 map->stripes[i].physical =
3487 btrfs_stripe_offset_nr(leaf, chunk, i);
3488 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3489 read_extent_buffer(leaf, uuid, (unsigned long)
3490 btrfs_stripe_dev_uuid_nr(chunk, i),
3492 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3494 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3496 free_extent_map(em);
3499 if (!map->stripes[i].dev) {
3500 map->stripes[i].dev =
3501 add_missing_dev(root, devid, uuid);
3502 if (!map->stripes[i].dev) {
3504 free_extent_map(em);
3508 map->stripes[i].dev->in_fs_metadata = 1;
3511 write_lock(&map_tree->map_tree.lock);
3512 ret = add_extent_mapping(&map_tree->map_tree, em);
3513 write_unlock(&map_tree->map_tree.lock);
3515 free_extent_map(em);
3520 static int fill_device_from_item(struct extent_buffer *leaf,
3521 struct btrfs_dev_item *dev_item,
3522 struct btrfs_device *device)
3526 device->devid = btrfs_device_id(leaf, dev_item);
3527 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3528 device->total_bytes = device->disk_total_bytes;
3529 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3530 device->type = btrfs_device_type(leaf, dev_item);
3531 device->io_align = btrfs_device_io_align(leaf, dev_item);
3532 device->io_width = btrfs_device_io_width(leaf, dev_item);
3533 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3535 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3536 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3541 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3543 struct btrfs_fs_devices *fs_devices;
3546 mutex_lock(&uuid_mutex);
3548 fs_devices = root->fs_info->fs_devices->seed;
3549 while (fs_devices) {
3550 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3554 fs_devices = fs_devices->seed;
3557 fs_devices = find_fsid(fsid);
3563 fs_devices = clone_fs_devices(fs_devices);
3564 if (IS_ERR(fs_devices)) {
3565 ret = PTR_ERR(fs_devices);
3569 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3570 root->fs_info->bdev_holder);
3574 if (!fs_devices->seeding) {
3575 __btrfs_close_devices(fs_devices);
3576 free_fs_devices(fs_devices);
3581 fs_devices->seed = root->fs_info->fs_devices->seed;
3582 root->fs_info->fs_devices->seed = fs_devices;
3584 mutex_unlock(&uuid_mutex);
3588 static int read_one_dev(struct btrfs_root *root,
3589 struct extent_buffer *leaf,
3590 struct btrfs_dev_item *dev_item)
3592 struct btrfs_device *device;
3595 u8 fs_uuid[BTRFS_UUID_SIZE];
3596 u8 dev_uuid[BTRFS_UUID_SIZE];
3598 devid = btrfs_device_id(leaf, dev_item);
3599 read_extent_buffer(leaf, dev_uuid,
3600 (unsigned long)btrfs_device_uuid(dev_item),
3602 read_extent_buffer(leaf, fs_uuid,
3603 (unsigned long)btrfs_device_fsid(dev_item),
3606 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3607 ret = open_seed_devices(root, fs_uuid);
3608 if (ret && !btrfs_test_opt(root, DEGRADED))
3612 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3613 if (!device || !device->bdev) {
3614 if (!btrfs_test_opt(root, DEGRADED))
3618 printk(KERN_WARNING "warning devid %llu missing\n",
3619 (unsigned long long)devid);
3620 device = add_missing_dev(root, devid, dev_uuid);
3623 } else if (!device->missing) {
3625 * this happens when a device that was properly setup
3626 * in the device info lists suddenly goes bad.
3627 * device->bdev is NULL, and so we have to set
3628 * device->missing to one here
3630 root->fs_info->fs_devices->missing_devices++;
3631 device->missing = 1;
3635 if (device->fs_devices != root->fs_info->fs_devices) {
3636 BUG_ON(device->writeable);
3637 if (device->generation !=
3638 btrfs_device_generation(leaf, dev_item))
3642 fill_device_from_item(leaf, dev_item, device);
3643 device->dev_root = root->fs_info->dev_root;
3644 device->in_fs_metadata = 1;
3645 if (device->writeable) {
3646 device->fs_devices->total_rw_bytes += device->total_bytes;
3647 spin_lock(&root->fs_info->free_chunk_lock);
3648 root->fs_info->free_chunk_space += device->total_bytes -
3650 spin_unlock(&root->fs_info->free_chunk_lock);
3656 int btrfs_read_sys_array(struct btrfs_root *root)
3658 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3659 struct extent_buffer *sb;
3660 struct btrfs_disk_key *disk_key;
3661 struct btrfs_chunk *chunk;
3663 unsigned long sb_ptr;
3669 struct btrfs_key key;
3671 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3672 BTRFS_SUPER_INFO_SIZE);
3675 btrfs_set_buffer_uptodate(sb);
3676 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
3678 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3679 array_size = btrfs_super_sys_array_size(super_copy);
3681 ptr = super_copy->sys_chunk_array;
3682 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3685 while (cur < array_size) {
3686 disk_key = (struct btrfs_disk_key *)ptr;
3687 btrfs_disk_key_to_cpu(&key, disk_key);
3689 len = sizeof(*disk_key); ptr += len;
3693 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3694 chunk = (struct btrfs_chunk *)sb_ptr;
3695 ret = read_one_chunk(root, &key, sb, chunk);
3698 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3699 len = btrfs_chunk_item_size(num_stripes);
3708 free_extent_buffer(sb);
3712 int btrfs_read_chunk_tree(struct btrfs_root *root)
3714 struct btrfs_path *path;
3715 struct extent_buffer *leaf;
3716 struct btrfs_key key;
3717 struct btrfs_key found_key;
3721 root = root->fs_info->chunk_root;
3723 path = btrfs_alloc_path();
3727 /* first we search for all of the device items, and then we
3728 * read in all of the chunk items. This way we can create chunk
3729 * mappings that reference all of the devices that are afound
3731 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3735 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3739 leaf = path->nodes[0];
3740 slot = path->slots[0];
3741 if (slot >= btrfs_header_nritems(leaf)) {
3742 ret = btrfs_next_leaf(root, path);
3749 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3750 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3751 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3753 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3754 struct btrfs_dev_item *dev_item;
3755 dev_item = btrfs_item_ptr(leaf, slot,
3756 struct btrfs_dev_item);
3757 ret = read_one_dev(root, leaf, dev_item);
3761 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3762 struct btrfs_chunk *chunk;
3763 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3764 ret = read_one_chunk(root, &found_key, leaf, chunk);
3770 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3772 btrfs_release_path(path);
3777 btrfs_free_path(path);