2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/slab.h>
21 #include <linux/buffer_head.h>
22 #include <linux/blkdev.h>
23 #include <linux/random.h>
24 #include <linux/iocontext.h>
25 #include <linux/capability.h>
26 #include <asm/div64.h>
29 #include "extent_map.h"
31 #include "transaction.h"
32 #include "print-tree.h"
34 #include "async-thread.h"
36 static int init_first_rw_device(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct btrfs_device *device);
39 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
41 static DEFINE_MUTEX(uuid_mutex);
42 static LIST_HEAD(fs_uuids);
44 static void lock_chunks(struct btrfs_root *root)
46 mutex_lock(&root->fs_info->chunk_mutex);
49 static void unlock_chunks(struct btrfs_root *root)
51 mutex_unlock(&root->fs_info->chunk_mutex);
54 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
56 struct btrfs_device *device;
57 WARN_ON(fs_devices->opened);
58 while (!list_empty(&fs_devices->devices)) {
59 device = list_entry(fs_devices->devices.next,
60 struct btrfs_device, dev_list);
61 list_del(&device->dev_list);
68 int btrfs_cleanup_fs_uuids(void)
70 struct btrfs_fs_devices *fs_devices;
72 while (!list_empty(&fs_uuids)) {
73 fs_devices = list_entry(fs_uuids.next,
74 struct btrfs_fs_devices, list);
75 list_del(&fs_devices->list);
76 free_fs_devices(fs_devices);
81 static noinline struct btrfs_device *__find_device(struct list_head *head,
84 struct btrfs_device *dev;
86 list_for_each_entry(dev, head, dev_list) {
87 if (dev->devid == devid &&
88 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
95 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
97 struct btrfs_fs_devices *fs_devices;
99 list_for_each_entry(fs_devices, &fs_uuids, list) {
100 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
106 static void requeue_list(struct btrfs_pending_bios *pending_bios,
107 struct bio *head, struct bio *tail)
110 struct bio *old_head;
112 old_head = pending_bios->head;
113 pending_bios->head = head;
114 if (pending_bios->tail)
115 tail->bi_next = old_head;
117 pending_bios->tail = tail;
121 * we try to collect pending bios for a device so we don't get a large
122 * number of procs sending bios down to the same device. This greatly
123 * improves the schedulers ability to collect and merge the bios.
125 * But, it also turns into a long list of bios to process and that is sure
126 * to eventually make the worker thread block. The solution here is to
127 * make some progress and then put this work struct back at the end of
128 * the list if the block device is congested. This way, multiple devices
129 * can make progress from a single worker thread.
131 static noinline int run_scheduled_bios(struct btrfs_device *device)
134 struct backing_dev_info *bdi;
135 struct btrfs_fs_info *fs_info;
136 struct btrfs_pending_bios *pending_bios;
140 unsigned long num_run;
141 unsigned long batch_run = 0;
143 unsigned long last_waited = 0;
145 int sync_pending = 0;
146 struct blk_plug plug;
149 * this function runs all the bios we've collected for
150 * a particular device. We don't want to wander off to
151 * another device without first sending all of these down.
152 * So, setup a plug here and finish it off before we return
154 blk_start_plug(&plug);
156 bdi = blk_get_backing_dev_info(device->bdev);
157 fs_info = device->dev_root->fs_info;
158 limit = btrfs_async_submit_limit(fs_info);
159 limit = limit * 2 / 3;
162 spin_lock(&device->io_lock);
167 /* take all the bios off the list at once and process them
168 * later on (without the lock held). But, remember the
169 * tail and other pointers so the bios can be properly reinserted
170 * into the list if we hit congestion
172 if (!force_reg && device->pending_sync_bios.head) {
173 pending_bios = &device->pending_sync_bios;
176 pending_bios = &device->pending_bios;
180 pending = pending_bios->head;
181 tail = pending_bios->tail;
182 WARN_ON(pending && !tail);
185 * if pending was null this time around, no bios need processing
186 * at all and we can stop. Otherwise it'll loop back up again
187 * and do an additional check so no bios are missed.
189 * device->running_pending is used to synchronize with the
192 if (device->pending_sync_bios.head == NULL &&
193 device->pending_bios.head == NULL) {
195 device->running_pending = 0;
198 device->running_pending = 1;
201 pending_bios->head = NULL;
202 pending_bios->tail = NULL;
204 spin_unlock(&device->io_lock);
209 /* we want to work on both lists, but do more bios on the
210 * sync list than the regular list
213 pending_bios != &device->pending_sync_bios &&
214 device->pending_sync_bios.head) ||
215 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
216 device->pending_bios.head)) {
217 spin_lock(&device->io_lock);
218 requeue_list(pending_bios, pending, tail);
223 pending = pending->bi_next;
225 atomic_dec(&fs_info->nr_async_bios);
227 if (atomic_read(&fs_info->nr_async_bios) < limit &&
228 waitqueue_active(&fs_info->async_submit_wait))
229 wake_up(&fs_info->async_submit_wait);
231 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
234 * if we're doing the sync list, record that our
235 * plug has some sync requests on it
237 * If we're doing the regular list and there are
238 * sync requests sitting around, unplug before
241 if (pending_bios == &device->pending_sync_bios) {
243 } else if (sync_pending) {
244 blk_finish_plug(&plug);
245 blk_start_plug(&plug);
249 submit_bio(cur->bi_rw, cur);
256 * we made progress, there is more work to do and the bdi
257 * is now congested. Back off and let other work structs
260 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
261 fs_info->fs_devices->open_devices > 1) {
262 struct io_context *ioc;
264 ioc = current->io_context;
267 * the main goal here is that we don't want to
268 * block if we're going to be able to submit
269 * more requests without blocking.
271 * This code does two great things, it pokes into
272 * the elevator code from a filesystem _and_
273 * it makes assumptions about how batching works.
275 if (ioc && ioc->nr_batch_requests > 0 &&
276 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
278 ioc->last_waited == last_waited)) {
280 * we want to go through our batch of
281 * requests and stop. So, we copy out
282 * the ioc->last_waited time and test
283 * against it before looping
285 last_waited = ioc->last_waited;
290 spin_lock(&device->io_lock);
291 requeue_list(pending_bios, pending, tail);
292 device->running_pending = 1;
294 spin_unlock(&device->io_lock);
295 btrfs_requeue_work(&device->work);
304 spin_lock(&device->io_lock);
305 if (device->pending_bios.head || device->pending_sync_bios.head)
307 spin_unlock(&device->io_lock);
310 blk_finish_plug(&plug);
314 static void pending_bios_fn(struct btrfs_work *work)
316 struct btrfs_device *device;
318 device = container_of(work, struct btrfs_device, work);
319 run_scheduled_bios(device);
322 static noinline int device_list_add(const char *path,
323 struct btrfs_super_block *disk_super,
324 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
326 struct btrfs_device *device;
327 struct btrfs_fs_devices *fs_devices;
328 u64 found_transid = btrfs_super_generation(disk_super);
331 fs_devices = find_fsid(disk_super->fsid);
333 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
336 INIT_LIST_HEAD(&fs_devices->devices);
337 INIT_LIST_HEAD(&fs_devices->alloc_list);
338 list_add(&fs_devices->list, &fs_uuids);
339 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
340 fs_devices->latest_devid = devid;
341 fs_devices->latest_trans = found_transid;
342 mutex_init(&fs_devices->device_list_mutex);
345 device = __find_device(&fs_devices->devices, devid,
346 disk_super->dev_item.uuid);
349 if (fs_devices->opened)
352 device = kzalloc(sizeof(*device), GFP_NOFS);
354 /* we can safely leave the fs_devices entry around */
357 device->devid = devid;
358 device->work.func = pending_bios_fn;
359 memcpy(device->uuid, disk_super->dev_item.uuid,
361 spin_lock_init(&device->io_lock);
362 device->name = kstrdup(path, GFP_NOFS);
367 INIT_LIST_HEAD(&device->dev_alloc_list);
369 /* init readahead state */
370 spin_lock_init(&device->reada_lock);
371 device->reada_curr_zone = NULL;
372 atomic_set(&device->reada_in_flight, 0);
373 device->reada_next = 0;
374 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
375 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
377 mutex_lock(&fs_devices->device_list_mutex);
378 list_add_rcu(&device->dev_list, &fs_devices->devices);
379 mutex_unlock(&fs_devices->device_list_mutex);
381 device->fs_devices = fs_devices;
382 fs_devices->num_devices++;
383 } else if (!device->name || strcmp(device->name, path)) {
384 name = kstrdup(path, GFP_NOFS);
389 if (device->missing) {
390 fs_devices->missing_devices--;
395 if (found_transid > fs_devices->latest_trans) {
396 fs_devices->latest_devid = devid;
397 fs_devices->latest_trans = found_transid;
399 *fs_devices_ret = fs_devices;
403 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
405 struct btrfs_fs_devices *fs_devices;
406 struct btrfs_device *device;
407 struct btrfs_device *orig_dev;
409 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
411 return ERR_PTR(-ENOMEM);
413 INIT_LIST_HEAD(&fs_devices->devices);
414 INIT_LIST_HEAD(&fs_devices->alloc_list);
415 INIT_LIST_HEAD(&fs_devices->list);
416 mutex_init(&fs_devices->device_list_mutex);
417 fs_devices->latest_devid = orig->latest_devid;
418 fs_devices->latest_trans = orig->latest_trans;
419 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
421 /* We have held the volume lock, it is safe to get the devices. */
422 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
423 device = kzalloc(sizeof(*device), GFP_NOFS);
427 device->name = kstrdup(orig_dev->name, GFP_NOFS);
433 device->devid = orig_dev->devid;
434 device->work.func = pending_bios_fn;
435 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
436 spin_lock_init(&device->io_lock);
437 INIT_LIST_HEAD(&device->dev_list);
438 INIT_LIST_HEAD(&device->dev_alloc_list);
440 list_add(&device->dev_list, &fs_devices->devices);
441 device->fs_devices = fs_devices;
442 fs_devices->num_devices++;
446 free_fs_devices(fs_devices);
447 return ERR_PTR(-ENOMEM);
450 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
452 struct btrfs_device *device, *next;
454 mutex_lock(&uuid_mutex);
456 /* This is the initialized path, it is safe to release the devices. */
457 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
458 if (device->in_fs_metadata)
462 blkdev_put(device->bdev, device->mode);
464 fs_devices->open_devices--;
466 if (device->writeable) {
467 list_del_init(&device->dev_alloc_list);
468 device->writeable = 0;
469 fs_devices->rw_devices--;
471 list_del_init(&device->dev_list);
472 fs_devices->num_devices--;
477 if (fs_devices->seed) {
478 fs_devices = fs_devices->seed;
482 mutex_unlock(&uuid_mutex);
486 static void __free_device(struct work_struct *work)
488 struct btrfs_device *device;
490 device = container_of(work, struct btrfs_device, rcu_work);
493 blkdev_put(device->bdev, device->mode);
499 static void free_device(struct rcu_head *head)
501 struct btrfs_device *device;
503 device = container_of(head, struct btrfs_device, rcu);
505 INIT_WORK(&device->rcu_work, __free_device);
506 schedule_work(&device->rcu_work);
509 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
511 struct btrfs_device *device;
513 if (--fs_devices->opened > 0)
516 mutex_lock(&fs_devices->device_list_mutex);
517 list_for_each_entry(device, &fs_devices->devices, dev_list) {
518 struct btrfs_device *new_device;
521 fs_devices->open_devices--;
523 if (device->writeable) {
524 list_del_init(&device->dev_alloc_list);
525 fs_devices->rw_devices--;
528 if (device->can_discard)
529 fs_devices->num_can_discard--;
531 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
533 memcpy(new_device, device, sizeof(*new_device));
534 new_device->name = kstrdup(device->name, GFP_NOFS);
535 BUG_ON(device->name && !new_device->name);
536 new_device->bdev = NULL;
537 new_device->writeable = 0;
538 new_device->in_fs_metadata = 0;
539 new_device->can_discard = 0;
540 list_replace_rcu(&device->dev_list, &new_device->dev_list);
542 call_rcu(&device->rcu, free_device);
544 mutex_unlock(&fs_devices->device_list_mutex);
546 WARN_ON(fs_devices->open_devices);
547 WARN_ON(fs_devices->rw_devices);
548 fs_devices->opened = 0;
549 fs_devices->seeding = 0;
554 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
556 struct btrfs_fs_devices *seed_devices = NULL;
559 mutex_lock(&uuid_mutex);
560 ret = __btrfs_close_devices(fs_devices);
561 if (!fs_devices->opened) {
562 seed_devices = fs_devices->seed;
563 fs_devices->seed = NULL;
565 mutex_unlock(&uuid_mutex);
567 while (seed_devices) {
568 fs_devices = seed_devices;
569 seed_devices = fs_devices->seed;
570 __btrfs_close_devices(fs_devices);
571 free_fs_devices(fs_devices);
576 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
577 fmode_t flags, void *holder)
579 struct request_queue *q;
580 struct block_device *bdev;
581 struct list_head *head = &fs_devices->devices;
582 struct btrfs_device *device;
583 struct block_device *latest_bdev = NULL;
584 struct buffer_head *bh;
585 struct btrfs_super_block *disk_super;
586 u64 latest_devid = 0;
587 u64 latest_transid = 0;
594 list_for_each_entry(device, head, dev_list) {
600 bdev = blkdev_get_by_path(device->name, flags, holder);
602 printk(KERN_INFO "open %s failed\n", device->name);
605 set_blocksize(bdev, 4096);
607 bh = btrfs_read_dev_super(bdev);
613 disk_super = (struct btrfs_super_block *)bh->b_data;
614 devid = btrfs_stack_device_id(&disk_super->dev_item);
615 if (devid != device->devid)
618 if (memcmp(device->uuid, disk_super->dev_item.uuid,
622 device->generation = btrfs_super_generation(disk_super);
623 if (!latest_transid || device->generation > latest_transid) {
624 latest_devid = devid;
625 latest_transid = device->generation;
629 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
630 device->writeable = 0;
632 device->writeable = !bdev_read_only(bdev);
636 q = bdev_get_queue(bdev);
637 if (blk_queue_discard(q)) {
638 device->can_discard = 1;
639 fs_devices->num_can_discard++;
643 device->in_fs_metadata = 0;
644 device->mode = flags;
646 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
647 fs_devices->rotating = 1;
649 fs_devices->open_devices++;
650 if (device->writeable) {
651 fs_devices->rw_devices++;
652 list_add(&device->dev_alloc_list,
653 &fs_devices->alloc_list);
661 blkdev_put(bdev, flags);
665 if (fs_devices->open_devices == 0) {
669 fs_devices->seeding = seeding;
670 fs_devices->opened = 1;
671 fs_devices->latest_bdev = latest_bdev;
672 fs_devices->latest_devid = latest_devid;
673 fs_devices->latest_trans = latest_transid;
674 fs_devices->total_rw_bytes = 0;
679 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
680 fmode_t flags, void *holder)
684 mutex_lock(&uuid_mutex);
685 if (fs_devices->opened) {
686 fs_devices->opened++;
689 ret = __btrfs_open_devices(fs_devices, flags, holder);
691 mutex_unlock(&uuid_mutex);
695 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
696 struct btrfs_fs_devices **fs_devices_ret)
698 struct btrfs_super_block *disk_super;
699 struct block_device *bdev;
700 struct buffer_head *bh;
705 mutex_lock(&uuid_mutex);
708 bdev = blkdev_get_by_path(path, flags, holder);
715 ret = set_blocksize(bdev, 4096);
718 bh = btrfs_read_dev_super(bdev);
723 disk_super = (struct btrfs_super_block *)bh->b_data;
724 devid = btrfs_stack_device_id(&disk_super->dev_item);
725 transid = btrfs_super_generation(disk_super);
726 if (disk_super->label[0])
727 printk(KERN_INFO "device label %s ", disk_super->label);
729 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
730 printk(KERN_CONT "devid %llu transid %llu %s\n",
731 (unsigned long long)devid, (unsigned long long)transid, path);
732 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
736 blkdev_put(bdev, flags);
738 mutex_unlock(&uuid_mutex);
742 /* helper to account the used device space in the range */
743 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
744 u64 end, u64 *length)
746 struct btrfs_key key;
747 struct btrfs_root *root = device->dev_root;
748 struct btrfs_dev_extent *dev_extent;
749 struct btrfs_path *path;
753 struct extent_buffer *l;
757 if (start >= device->total_bytes)
760 path = btrfs_alloc_path();
765 key.objectid = device->devid;
767 key.type = BTRFS_DEV_EXTENT_KEY;
769 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
773 ret = btrfs_previous_item(root, path, key.objectid, key.type);
780 slot = path->slots[0];
781 if (slot >= btrfs_header_nritems(l)) {
782 ret = btrfs_next_leaf(root, path);
790 btrfs_item_key_to_cpu(l, &key, slot);
792 if (key.objectid < device->devid)
795 if (key.objectid > device->devid)
798 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
801 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
802 extent_end = key.offset + btrfs_dev_extent_length(l,
804 if (key.offset <= start && extent_end > end) {
805 *length = end - start + 1;
807 } else if (key.offset <= start && extent_end > start)
808 *length += extent_end - start;
809 else if (key.offset > start && extent_end <= end)
810 *length += extent_end - key.offset;
811 else if (key.offset > start && key.offset <= end) {
812 *length += end - key.offset + 1;
814 } else if (key.offset > end)
822 btrfs_free_path(path);
827 * find_free_dev_extent - find free space in the specified device
828 * @trans: transaction handler
829 * @device: the device which we search the free space in
830 * @num_bytes: the size of the free space that we need
831 * @start: store the start of the free space.
832 * @len: the size of the free space. that we find, or the size of the max
833 * free space if we don't find suitable free space
835 * this uses a pretty simple search, the expectation is that it is
836 * called very infrequently and that a given device has a small number
839 * @start is used to store the start of the free space if we find. But if we
840 * don't find suitable free space, it will be used to store the start position
841 * of the max free space.
843 * @len is used to store the size of the free space that we find.
844 * But if we don't find suitable free space, it is used to store the size of
845 * the max free space.
847 int find_free_dev_extent(struct btrfs_trans_handle *trans,
848 struct btrfs_device *device, u64 num_bytes,
849 u64 *start, u64 *len)
851 struct btrfs_key key;
852 struct btrfs_root *root = device->dev_root;
853 struct btrfs_dev_extent *dev_extent;
854 struct btrfs_path *path;
860 u64 search_end = device->total_bytes;
863 struct extent_buffer *l;
865 /* FIXME use last free of some kind */
867 /* we don't want to overwrite the superblock on the drive,
868 * so we make sure to start at an offset of at least 1MB
870 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
872 max_hole_start = search_start;
876 if (search_start >= search_end) {
881 path = btrfs_alloc_path();
888 key.objectid = device->devid;
889 key.offset = search_start;
890 key.type = BTRFS_DEV_EXTENT_KEY;
892 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
896 ret = btrfs_previous_item(root, path, key.objectid, key.type);
903 slot = path->slots[0];
904 if (slot >= btrfs_header_nritems(l)) {
905 ret = btrfs_next_leaf(root, path);
913 btrfs_item_key_to_cpu(l, &key, slot);
915 if (key.objectid < device->devid)
918 if (key.objectid > device->devid)
921 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
924 if (key.offset > search_start) {
925 hole_size = key.offset - search_start;
927 if (hole_size > max_hole_size) {
928 max_hole_start = search_start;
929 max_hole_size = hole_size;
933 * If this free space is greater than which we need,
934 * it must be the max free space that we have found
935 * until now, so max_hole_start must point to the start
936 * of this free space and the length of this free space
937 * is stored in max_hole_size. Thus, we return
938 * max_hole_start and max_hole_size and go back to the
941 if (hole_size >= num_bytes) {
947 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
948 extent_end = key.offset + btrfs_dev_extent_length(l,
950 if (extent_end > search_start)
951 search_start = extent_end;
958 * At this point, search_start should be the end of
959 * allocated dev extents, and when shrinking the device,
960 * search_end may be smaller than search_start.
962 if (search_end > search_start)
963 hole_size = search_end - search_start;
965 if (hole_size > max_hole_size) {
966 max_hole_start = search_start;
967 max_hole_size = hole_size;
971 if (hole_size < num_bytes)
977 btrfs_free_path(path);
979 *start = max_hole_start;
981 *len = max_hole_size;
985 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
986 struct btrfs_device *device,
990 struct btrfs_path *path;
991 struct btrfs_root *root = device->dev_root;
992 struct btrfs_key key;
993 struct btrfs_key found_key;
994 struct extent_buffer *leaf = NULL;
995 struct btrfs_dev_extent *extent = NULL;
997 path = btrfs_alloc_path();
1001 key.objectid = device->devid;
1003 key.type = BTRFS_DEV_EXTENT_KEY;
1005 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1007 ret = btrfs_previous_item(root, path, key.objectid,
1008 BTRFS_DEV_EXTENT_KEY);
1011 leaf = path->nodes[0];
1012 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1013 extent = btrfs_item_ptr(leaf, path->slots[0],
1014 struct btrfs_dev_extent);
1015 BUG_ON(found_key.offset > start || found_key.offset +
1016 btrfs_dev_extent_length(leaf, extent) < start);
1017 } else if (ret == 0) {
1018 leaf = path->nodes[0];
1019 extent = btrfs_item_ptr(leaf, path->slots[0],
1020 struct btrfs_dev_extent);
1024 if (device->bytes_used > 0)
1025 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
1026 ret = btrfs_del_item(trans, root, path);
1029 btrfs_free_path(path);
1033 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1034 struct btrfs_device *device,
1035 u64 chunk_tree, u64 chunk_objectid,
1036 u64 chunk_offset, u64 start, u64 num_bytes)
1039 struct btrfs_path *path;
1040 struct btrfs_root *root = device->dev_root;
1041 struct btrfs_dev_extent *extent;
1042 struct extent_buffer *leaf;
1043 struct btrfs_key key;
1045 WARN_ON(!device->in_fs_metadata);
1046 path = btrfs_alloc_path();
1050 key.objectid = device->devid;
1052 key.type = BTRFS_DEV_EXTENT_KEY;
1053 ret = btrfs_insert_empty_item(trans, root, path, &key,
1057 leaf = path->nodes[0];
1058 extent = btrfs_item_ptr(leaf, path->slots[0],
1059 struct btrfs_dev_extent);
1060 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1061 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1062 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1064 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1065 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1068 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1069 btrfs_mark_buffer_dirty(leaf);
1070 btrfs_free_path(path);
1074 static noinline int find_next_chunk(struct btrfs_root *root,
1075 u64 objectid, u64 *offset)
1077 struct btrfs_path *path;
1079 struct btrfs_key key;
1080 struct btrfs_chunk *chunk;
1081 struct btrfs_key found_key;
1083 path = btrfs_alloc_path();
1087 key.objectid = objectid;
1088 key.offset = (u64)-1;
1089 key.type = BTRFS_CHUNK_ITEM_KEY;
1091 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1097 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1101 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1103 if (found_key.objectid != objectid)
1106 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1107 struct btrfs_chunk);
1108 *offset = found_key.offset +
1109 btrfs_chunk_length(path->nodes[0], chunk);
1114 btrfs_free_path(path);
1118 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
1121 struct btrfs_key key;
1122 struct btrfs_key found_key;
1123 struct btrfs_path *path;
1125 root = root->fs_info->chunk_root;
1127 path = btrfs_alloc_path();
1131 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1132 key.type = BTRFS_DEV_ITEM_KEY;
1133 key.offset = (u64)-1;
1135 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1141 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1142 BTRFS_DEV_ITEM_KEY);
1146 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1148 *objectid = found_key.offset + 1;
1152 btrfs_free_path(path);
1157 * the device information is stored in the chunk root
1158 * the btrfs_device struct should be fully filled in
1160 int btrfs_add_device(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root,
1162 struct btrfs_device *device)
1165 struct btrfs_path *path;
1166 struct btrfs_dev_item *dev_item;
1167 struct extent_buffer *leaf;
1168 struct btrfs_key key;
1171 root = root->fs_info->chunk_root;
1173 path = btrfs_alloc_path();
1177 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1178 key.type = BTRFS_DEV_ITEM_KEY;
1179 key.offset = device->devid;
1181 ret = btrfs_insert_empty_item(trans, root, path, &key,
1186 leaf = path->nodes[0];
1187 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1189 btrfs_set_device_id(leaf, dev_item, device->devid);
1190 btrfs_set_device_generation(leaf, dev_item, 0);
1191 btrfs_set_device_type(leaf, dev_item, device->type);
1192 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1193 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1194 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1195 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1196 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1197 btrfs_set_device_group(leaf, dev_item, 0);
1198 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1199 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1200 btrfs_set_device_start_offset(leaf, dev_item, 0);
1202 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1203 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1204 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1205 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1206 btrfs_mark_buffer_dirty(leaf);
1210 btrfs_free_path(path);
1214 static int btrfs_rm_dev_item(struct btrfs_root *root,
1215 struct btrfs_device *device)
1218 struct btrfs_path *path;
1219 struct btrfs_key key;
1220 struct btrfs_trans_handle *trans;
1222 root = root->fs_info->chunk_root;
1224 path = btrfs_alloc_path();
1228 trans = btrfs_start_transaction(root, 0);
1229 if (IS_ERR(trans)) {
1230 btrfs_free_path(path);
1231 return PTR_ERR(trans);
1233 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1234 key.type = BTRFS_DEV_ITEM_KEY;
1235 key.offset = device->devid;
1238 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1247 ret = btrfs_del_item(trans, root, path);
1251 btrfs_free_path(path);
1252 unlock_chunks(root);
1253 btrfs_commit_transaction(trans, root);
1257 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1259 struct btrfs_device *device;
1260 struct btrfs_device *next_device;
1261 struct block_device *bdev;
1262 struct buffer_head *bh = NULL;
1263 struct btrfs_super_block *disk_super;
1264 struct btrfs_fs_devices *cur_devices;
1270 bool clear_super = false;
1272 mutex_lock(&uuid_mutex);
1273 mutex_lock(&root->fs_info->volume_mutex);
1275 all_avail = root->fs_info->avail_data_alloc_bits |
1276 root->fs_info->avail_system_alloc_bits |
1277 root->fs_info->avail_metadata_alloc_bits;
1279 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1280 root->fs_info->fs_devices->num_devices <= 4) {
1281 printk(KERN_ERR "btrfs: unable to go below four devices "
1287 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1288 root->fs_info->fs_devices->num_devices <= 2) {
1289 printk(KERN_ERR "btrfs: unable to go below two "
1290 "devices on raid1\n");
1295 if (strcmp(device_path, "missing") == 0) {
1296 struct list_head *devices;
1297 struct btrfs_device *tmp;
1300 devices = &root->fs_info->fs_devices->devices;
1302 * It is safe to read the devices since the volume_mutex
1305 list_for_each_entry(tmp, devices, dev_list) {
1306 if (tmp->in_fs_metadata && !tmp->bdev) {
1315 printk(KERN_ERR "btrfs: no missing devices found to "
1320 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1321 root->fs_info->bdev_holder);
1323 ret = PTR_ERR(bdev);
1327 set_blocksize(bdev, 4096);
1328 bh = btrfs_read_dev_super(bdev);
1333 disk_super = (struct btrfs_super_block *)bh->b_data;
1334 devid = btrfs_stack_device_id(&disk_super->dev_item);
1335 dev_uuid = disk_super->dev_item.uuid;
1336 device = btrfs_find_device(root, devid, dev_uuid,
1344 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1345 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1351 if (device->writeable) {
1353 list_del_init(&device->dev_alloc_list);
1354 unlock_chunks(root);
1355 root->fs_info->fs_devices->rw_devices--;
1359 ret = btrfs_shrink_device(device, 0);
1363 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
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 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1703 root->fs_info->fs_devices->rotating = 1;
1705 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1706 btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1707 total_bytes + device->total_bytes);
1709 total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1710 btrfs_set_super_num_devices(&root->fs_info->super_copy,
1712 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1715 ret = init_first_rw_device(trans, root, device);
1717 ret = btrfs_finish_sprout(trans, root);
1720 ret = btrfs_add_device(trans, root, device);
1724 * we've got more storage, clear any full flags on the space
1727 btrfs_clear_space_info_full(root->fs_info);
1729 unlock_chunks(root);
1730 btrfs_commit_transaction(trans, root);
1733 mutex_unlock(&uuid_mutex);
1734 up_write(&sb->s_umount);
1736 ret = btrfs_relocate_sys_chunks(root);
1740 mutex_unlock(&root->fs_info->volume_mutex);
1743 blkdev_put(bdev, FMODE_EXCL);
1745 mutex_unlock(&uuid_mutex);
1746 up_write(&sb->s_umount);
1751 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1752 struct btrfs_device *device)
1755 struct btrfs_path *path;
1756 struct btrfs_root *root;
1757 struct btrfs_dev_item *dev_item;
1758 struct extent_buffer *leaf;
1759 struct btrfs_key key;
1761 root = device->dev_root->fs_info->chunk_root;
1763 path = btrfs_alloc_path();
1767 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1768 key.type = BTRFS_DEV_ITEM_KEY;
1769 key.offset = device->devid;
1771 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1780 leaf = path->nodes[0];
1781 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1783 btrfs_set_device_id(leaf, dev_item, device->devid);
1784 btrfs_set_device_type(leaf, dev_item, device->type);
1785 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1786 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1787 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1788 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1789 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1790 btrfs_mark_buffer_dirty(leaf);
1793 btrfs_free_path(path);
1797 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1798 struct btrfs_device *device, u64 new_size)
1800 struct btrfs_super_block *super_copy =
1801 &device->dev_root->fs_info->super_copy;
1802 u64 old_total = btrfs_super_total_bytes(super_copy);
1803 u64 diff = new_size - device->total_bytes;
1805 if (!device->writeable)
1807 if (new_size <= device->total_bytes)
1810 btrfs_set_super_total_bytes(super_copy, old_total + diff);
1811 device->fs_devices->total_rw_bytes += diff;
1813 device->total_bytes = new_size;
1814 device->disk_total_bytes = new_size;
1815 btrfs_clear_space_info_full(device->dev_root->fs_info);
1817 return btrfs_update_device(trans, device);
1820 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1821 struct btrfs_device *device, u64 new_size)
1824 lock_chunks(device->dev_root);
1825 ret = __btrfs_grow_device(trans, device, new_size);
1826 unlock_chunks(device->dev_root);
1830 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1831 struct btrfs_root *root,
1832 u64 chunk_tree, u64 chunk_objectid,
1836 struct btrfs_path *path;
1837 struct btrfs_key key;
1839 root = root->fs_info->chunk_root;
1840 path = btrfs_alloc_path();
1844 key.objectid = chunk_objectid;
1845 key.offset = chunk_offset;
1846 key.type = BTRFS_CHUNK_ITEM_KEY;
1848 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1851 ret = btrfs_del_item(trans, root, path);
1853 btrfs_free_path(path);
1857 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1860 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1861 struct btrfs_disk_key *disk_key;
1862 struct btrfs_chunk *chunk;
1869 struct btrfs_key key;
1871 array_size = btrfs_super_sys_array_size(super_copy);
1873 ptr = super_copy->sys_chunk_array;
1876 while (cur < array_size) {
1877 disk_key = (struct btrfs_disk_key *)ptr;
1878 btrfs_disk_key_to_cpu(&key, disk_key);
1880 len = sizeof(*disk_key);
1882 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1883 chunk = (struct btrfs_chunk *)(ptr + len);
1884 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1885 len += btrfs_chunk_item_size(num_stripes);
1890 if (key.objectid == chunk_objectid &&
1891 key.offset == chunk_offset) {
1892 memmove(ptr, ptr + len, array_size - (cur + len));
1894 btrfs_set_super_sys_array_size(super_copy, array_size);
1903 static int btrfs_relocate_chunk(struct btrfs_root *root,
1904 u64 chunk_tree, u64 chunk_objectid,
1907 struct extent_map_tree *em_tree;
1908 struct btrfs_root *extent_root;
1909 struct btrfs_trans_handle *trans;
1910 struct extent_map *em;
1911 struct map_lookup *map;
1915 root = root->fs_info->chunk_root;
1916 extent_root = root->fs_info->extent_root;
1917 em_tree = &root->fs_info->mapping_tree.map_tree;
1919 ret = btrfs_can_relocate(extent_root, chunk_offset);
1923 /* step one, relocate all the extents inside this chunk */
1924 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1928 trans = btrfs_start_transaction(root, 0);
1929 BUG_ON(IS_ERR(trans));
1934 * step two, delete the device extents and the
1935 * chunk tree entries
1937 read_lock(&em_tree->lock);
1938 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1939 read_unlock(&em_tree->lock);
1941 BUG_ON(em->start > chunk_offset ||
1942 em->start + em->len < chunk_offset);
1943 map = (struct map_lookup *)em->bdev;
1945 for (i = 0; i < map->num_stripes; i++) {
1946 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1947 map->stripes[i].physical);
1950 if (map->stripes[i].dev) {
1951 ret = btrfs_update_device(trans, map->stripes[i].dev);
1955 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1960 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
1962 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1963 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1967 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1970 write_lock(&em_tree->lock);
1971 remove_extent_mapping(em_tree, em);
1972 write_unlock(&em_tree->lock);
1977 /* once for the tree */
1978 free_extent_map(em);
1980 free_extent_map(em);
1982 unlock_chunks(root);
1983 btrfs_end_transaction(trans, root);
1987 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1989 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1990 struct btrfs_path *path;
1991 struct extent_buffer *leaf;
1992 struct btrfs_chunk *chunk;
1993 struct btrfs_key key;
1994 struct btrfs_key found_key;
1995 u64 chunk_tree = chunk_root->root_key.objectid;
1997 bool retried = false;
2001 path = btrfs_alloc_path();
2006 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2007 key.offset = (u64)-1;
2008 key.type = BTRFS_CHUNK_ITEM_KEY;
2011 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2016 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2023 leaf = path->nodes[0];
2024 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2026 chunk = btrfs_item_ptr(leaf, path->slots[0],
2027 struct btrfs_chunk);
2028 chunk_type = btrfs_chunk_type(leaf, chunk);
2029 btrfs_release_path(path);
2031 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2032 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2041 if (found_key.offset == 0)
2043 key.offset = found_key.offset - 1;
2046 if (failed && !retried) {
2050 } else if (failed && retried) {
2055 btrfs_free_path(path);
2059 static u64 div_factor(u64 num, int factor)
2068 int btrfs_balance(struct btrfs_root *dev_root)
2071 struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
2072 struct btrfs_device *device;
2075 struct btrfs_path *path;
2076 struct btrfs_key key;
2077 struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
2078 struct btrfs_trans_handle *trans;
2079 struct btrfs_key found_key;
2081 if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
2084 if (!capable(CAP_SYS_ADMIN))
2087 mutex_lock(&dev_root->fs_info->volume_mutex);
2088 dev_root = dev_root->fs_info->dev_root;
2090 /* step one make some room on all the devices */
2091 list_for_each_entry(device, devices, dev_list) {
2092 old_size = device->total_bytes;
2093 size_to_free = div_factor(old_size, 1);
2094 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
2095 if (!device->writeable ||
2096 device->total_bytes - device->bytes_used > size_to_free)
2099 ret = btrfs_shrink_device(device, old_size - size_to_free);
2104 trans = btrfs_start_transaction(dev_root, 0);
2105 BUG_ON(IS_ERR(trans));
2107 ret = btrfs_grow_device(trans, device, old_size);
2110 btrfs_end_transaction(trans, dev_root);
2113 /* step two, relocate all the chunks */
2114 path = btrfs_alloc_path();
2119 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2120 key.offset = (u64)-1;
2121 key.type = BTRFS_CHUNK_ITEM_KEY;
2124 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2129 * this shouldn't happen, it means the last relocate
2135 ret = btrfs_previous_item(chunk_root, path, 0,
2136 BTRFS_CHUNK_ITEM_KEY);
2140 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2142 if (found_key.objectid != key.objectid)
2145 /* chunk zero is special */
2146 if (found_key.offset == 0)
2149 btrfs_release_path(path);
2150 ret = btrfs_relocate_chunk(chunk_root,
2151 chunk_root->root_key.objectid,
2154 if (ret && ret != -ENOSPC)
2156 key.offset = found_key.offset - 1;
2160 btrfs_free_path(path);
2161 mutex_unlock(&dev_root->fs_info->volume_mutex);
2166 * shrinking a device means finding all of the device extents past
2167 * the new size, and then following the back refs to the chunks.
2168 * The chunk relocation code actually frees the device extent
2170 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
2172 struct btrfs_trans_handle *trans;
2173 struct btrfs_root *root = device->dev_root;
2174 struct btrfs_dev_extent *dev_extent = NULL;
2175 struct btrfs_path *path;
2183 bool retried = false;
2184 struct extent_buffer *l;
2185 struct btrfs_key key;
2186 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2187 u64 old_total = btrfs_super_total_bytes(super_copy);
2188 u64 old_size = device->total_bytes;
2189 u64 diff = device->total_bytes - new_size;
2191 if (new_size >= device->total_bytes)
2194 path = btrfs_alloc_path();
2202 device->total_bytes = new_size;
2203 if (device->writeable)
2204 device->fs_devices->total_rw_bytes -= diff;
2205 unlock_chunks(root);
2208 key.objectid = device->devid;
2209 key.offset = (u64)-1;
2210 key.type = BTRFS_DEV_EXTENT_KEY;
2213 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2217 ret = btrfs_previous_item(root, path, 0, key.type);
2222 btrfs_release_path(path);
2227 slot = path->slots[0];
2228 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
2230 if (key.objectid != device->devid) {
2231 btrfs_release_path(path);
2235 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2236 length = btrfs_dev_extent_length(l, dev_extent);
2238 if (key.offset + length <= new_size) {
2239 btrfs_release_path(path);
2243 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2244 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2245 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2246 btrfs_release_path(path);
2248 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
2250 if (ret && ret != -ENOSPC)
2257 if (failed && !retried) {
2261 } else if (failed && retried) {
2265 device->total_bytes = old_size;
2266 if (device->writeable)
2267 device->fs_devices->total_rw_bytes += diff;
2268 unlock_chunks(root);
2272 /* Shrinking succeeded, else we would be at "done". */
2273 trans = btrfs_start_transaction(root, 0);
2274 if (IS_ERR(trans)) {
2275 ret = PTR_ERR(trans);
2281 device->disk_total_bytes = new_size;
2282 /* Now btrfs_update_device() will change the on-disk size. */
2283 ret = btrfs_update_device(trans, device);
2285 unlock_chunks(root);
2286 btrfs_end_transaction(trans, root);
2289 WARN_ON(diff > old_total);
2290 btrfs_set_super_total_bytes(super_copy, old_total - diff);
2291 unlock_chunks(root);
2292 btrfs_end_transaction(trans, root);
2294 btrfs_free_path(path);
2298 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2299 struct btrfs_root *root,
2300 struct btrfs_key *key,
2301 struct btrfs_chunk *chunk, int item_size)
2303 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2304 struct btrfs_disk_key disk_key;
2308 array_size = btrfs_super_sys_array_size(super_copy);
2309 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2312 ptr = super_copy->sys_chunk_array + array_size;
2313 btrfs_cpu_key_to_disk(&disk_key, key);
2314 memcpy(ptr, &disk_key, sizeof(disk_key));
2315 ptr += sizeof(disk_key);
2316 memcpy(ptr, chunk, item_size);
2317 item_size += sizeof(disk_key);
2318 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2323 * sort the devices in descending order by max_avail, total_avail
2325 static int btrfs_cmp_device_info(const void *a, const void *b)
2327 const struct btrfs_device_info *di_a = a;
2328 const struct btrfs_device_info *di_b = b;
2330 if (di_a->max_avail > di_b->max_avail)
2332 if (di_a->max_avail < di_b->max_avail)
2334 if (di_a->total_avail > di_b->total_avail)
2336 if (di_a->total_avail < di_b->total_avail)
2341 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2342 struct btrfs_root *extent_root,
2343 struct map_lookup **map_ret,
2344 u64 *num_bytes_out, u64 *stripe_size_out,
2345 u64 start, u64 type)
2347 struct btrfs_fs_info *info = extent_root->fs_info;
2348 struct btrfs_fs_devices *fs_devices = info->fs_devices;
2349 struct list_head *cur;
2350 struct map_lookup *map = NULL;
2351 struct extent_map_tree *em_tree;
2352 struct extent_map *em;
2353 struct btrfs_device_info *devices_info = NULL;
2355 int num_stripes; /* total number of stripes to allocate */
2356 int sub_stripes; /* sub_stripes info for map */
2357 int dev_stripes; /* stripes per dev */
2358 int devs_max; /* max devs to use */
2359 int devs_min; /* min devs needed */
2360 int devs_increment; /* ndevs has to be a multiple of this */
2361 int ncopies; /* how many copies to data has */
2363 u64 max_stripe_size;
2371 if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2372 (type & BTRFS_BLOCK_GROUP_DUP)) {
2374 type &= ~BTRFS_BLOCK_GROUP_DUP;
2377 if (list_empty(&fs_devices->alloc_list))
2384 devs_max = 0; /* 0 == as many as possible */
2388 * define the properties of each RAID type.
2389 * FIXME: move this to a global table and use it in all RAID
2392 if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2396 } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2398 } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2403 } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2412 if (type & BTRFS_BLOCK_GROUP_DATA) {
2413 max_stripe_size = 1024 * 1024 * 1024;
2414 max_chunk_size = 10 * max_stripe_size;
2415 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2416 max_stripe_size = 256 * 1024 * 1024;
2417 max_chunk_size = max_stripe_size;
2418 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2419 max_stripe_size = 8 * 1024 * 1024;
2420 max_chunk_size = 2 * max_stripe_size;
2422 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
2427 /* we don't want a chunk larger than 10% of writeable space */
2428 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2431 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
2436 cur = fs_devices->alloc_list.next;
2439 * in the first pass through the devices list, we gather information
2440 * about the available holes on each device.
2443 while (cur != &fs_devices->alloc_list) {
2444 struct btrfs_device *device;
2448 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2452 if (!device->writeable) {
2454 "btrfs: read-only device in alloc_list\n");
2459 if (!device->in_fs_metadata)
2462 if (device->total_bytes > device->bytes_used)
2463 total_avail = device->total_bytes - device->bytes_used;
2467 /* If there is no space on this device, skip it. */
2468 if (total_avail == 0)
2471 ret = find_free_dev_extent(trans, device,
2472 max_stripe_size * dev_stripes,
2473 &dev_offset, &max_avail);
2474 if (ret && ret != -ENOSPC)
2478 max_avail = max_stripe_size * dev_stripes;
2480 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
2483 devices_info[ndevs].dev_offset = dev_offset;
2484 devices_info[ndevs].max_avail = max_avail;
2485 devices_info[ndevs].total_avail = total_avail;
2486 devices_info[ndevs].dev = device;
2491 * now sort the devices by hole size / available space
2493 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
2494 btrfs_cmp_device_info, NULL);
2496 /* round down to number of usable stripes */
2497 ndevs -= ndevs % devs_increment;
2499 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
2504 if (devs_max && ndevs > devs_max)
2507 * the primary goal is to maximize the number of stripes, so use as many
2508 * devices as possible, even if the stripes are not maximum sized.
2510 stripe_size = devices_info[ndevs-1].max_avail;
2511 num_stripes = ndevs * dev_stripes;
2513 if (stripe_size * num_stripes > max_chunk_size * ncopies) {
2514 stripe_size = max_chunk_size * ncopies;
2515 do_div(stripe_size, num_stripes);
2518 do_div(stripe_size, dev_stripes);
2519 do_div(stripe_size, BTRFS_STRIPE_LEN);
2520 stripe_size *= BTRFS_STRIPE_LEN;
2522 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2527 map->num_stripes = num_stripes;
2529 for (i = 0; i < ndevs; ++i) {
2530 for (j = 0; j < dev_stripes; ++j) {
2531 int s = i * dev_stripes + j;
2532 map->stripes[s].dev = devices_info[i].dev;
2533 map->stripes[s].physical = devices_info[i].dev_offset +
2537 map->sector_size = extent_root->sectorsize;
2538 map->stripe_len = BTRFS_STRIPE_LEN;
2539 map->io_align = BTRFS_STRIPE_LEN;
2540 map->io_width = BTRFS_STRIPE_LEN;
2542 map->sub_stripes = sub_stripes;
2545 num_bytes = stripe_size * (num_stripes / ncopies);
2547 *stripe_size_out = stripe_size;
2548 *num_bytes_out = num_bytes;
2550 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
2552 em = alloc_extent_map();
2557 em->bdev = (struct block_device *)map;
2559 em->len = num_bytes;
2560 em->block_start = 0;
2561 em->block_len = em->len;
2563 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2564 write_lock(&em_tree->lock);
2565 ret = add_extent_mapping(em_tree, em);
2566 write_unlock(&em_tree->lock);
2568 free_extent_map(em);
2570 ret = btrfs_make_block_group(trans, extent_root, 0, type,
2571 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2575 for (i = 0; i < map->num_stripes; ++i) {
2576 struct btrfs_device *device;
2579 device = map->stripes[i].dev;
2580 dev_offset = map->stripes[i].physical;
2582 ret = btrfs_alloc_dev_extent(trans, device,
2583 info->chunk_root->root_key.objectid,
2584 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2585 start, dev_offset, stripe_size);
2589 kfree(devices_info);
2594 kfree(devices_info);
2598 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2599 struct btrfs_root *extent_root,
2600 struct map_lookup *map, u64 chunk_offset,
2601 u64 chunk_size, u64 stripe_size)
2604 struct btrfs_key key;
2605 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2606 struct btrfs_device *device;
2607 struct btrfs_chunk *chunk;
2608 struct btrfs_stripe *stripe;
2609 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2613 chunk = kzalloc(item_size, GFP_NOFS);
2618 while (index < map->num_stripes) {
2619 device = map->stripes[index].dev;
2620 device->bytes_used += stripe_size;
2621 ret = btrfs_update_device(trans, device);
2627 stripe = &chunk->stripe;
2628 while (index < map->num_stripes) {
2629 device = map->stripes[index].dev;
2630 dev_offset = map->stripes[index].physical;
2632 btrfs_set_stack_stripe_devid(stripe, device->devid);
2633 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2634 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2639 btrfs_set_stack_chunk_length(chunk, chunk_size);
2640 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2641 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2642 btrfs_set_stack_chunk_type(chunk, map->type);
2643 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2644 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2645 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2646 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2647 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2649 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2650 key.type = BTRFS_CHUNK_ITEM_KEY;
2651 key.offset = chunk_offset;
2653 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2656 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2657 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2667 * Chunk allocation falls into two parts. The first part does works
2668 * that make the new allocated chunk useable, but not do any operation
2669 * that modifies the chunk tree. The second part does the works that
2670 * require modifying the chunk tree. This division is important for the
2671 * bootstrap process of adding storage to a seed btrfs.
2673 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2674 struct btrfs_root *extent_root, u64 type)
2679 struct map_lookup *map;
2680 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2683 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2688 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2689 &stripe_size, chunk_offset, type);
2693 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2694 chunk_size, stripe_size);
2699 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2700 struct btrfs_root *root,
2701 struct btrfs_device *device)
2704 u64 sys_chunk_offset;
2708 u64 sys_stripe_size;
2710 struct map_lookup *map;
2711 struct map_lookup *sys_map;
2712 struct btrfs_fs_info *fs_info = root->fs_info;
2713 struct btrfs_root *extent_root = fs_info->extent_root;
2716 ret = find_next_chunk(fs_info->chunk_root,
2717 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2721 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2722 (fs_info->metadata_alloc_profile &
2723 fs_info->avail_metadata_alloc_bits);
2724 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2726 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2727 &stripe_size, chunk_offset, alloc_profile);
2730 sys_chunk_offset = chunk_offset + chunk_size;
2732 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2733 (fs_info->system_alloc_profile &
2734 fs_info->avail_system_alloc_bits);
2735 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2737 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2738 &sys_chunk_size, &sys_stripe_size,
2739 sys_chunk_offset, alloc_profile);
2742 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2746 * Modifying chunk tree needs allocating new blocks from both
2747 * system block group and metadata block group. So we only can
2748 * do operations require modifying the chunk tree after both
2749 * block groups were created.
2751 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2752 chunk_size, stripe_size);
2755 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2756 sys_chunk_offset, sys_chunk_size,
2762 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2764 struct extent_map *em;
2765 struct map_lookup *map;
2766 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2770 read_lock(&map_tree->map_tree.lock);
2771 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2772 read_unlock(&map_tree->map_tree.lock);
2776 if (btrfs_test_opt(root, DEGRADED)) {
2777 free_extent_map(em);
2781 map = (struct map_lookup *)em->bdev;
2782 for (i = 0; i < map->num_stripes; i++) {
2783 if (!map->stripes[i].dev->writeable) {
2788 free_extent_map(em);
2792 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2794 extent_map_tree_init(&tree->map_tree);
2797 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2799 struct extent_map *em;
2802 write_lock(&tree->map_tree.lock);
2803 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2805 remove_extent_mapping(&tree->map_tree, em);
2806 write_unlock(&tree->map_tree.lock);
2811 free_extent_map(em);
2812 /* once for the tree */
2813 free_extent_map(em);
2817 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2819 struct extent_map *em;
2820 struct map_lookup *map;
2821 struct extent_map_tree *em_tree = &map_tree->map_tree;
2824 read_lock(&em_tree->lock);
2825 em = lookup_extent_mapping(em_tree, logical, len);
2826 read_unlock(&em_tree->lock);
2829 BUG_ON(em->start > logical || em->start + em->len < logical);
2830 map = (struct map_lookup *)em->bdev;
2831 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2832 ret = map->num_stripes;
2833 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2834 ret = map->sub_stripes;
2837 free_extent_map(em);
2841 static int find_live_mirror(struct map_lookup *map, int first, int num,
2845 if (map->stripes[optimal].dev->bdev)
2847 for (i = first; i < first + num; i++) {
2848 if (map->stripes[i].dev->bdev)
2851 /* we couldn't find one that doesn't fail. Just return something
2852 * and the io error handling code will clean up eventually
2857 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2858 u64 logical, u64 *length,
2859 struct btrfs_multi_bio **multi_ret,
2862 struct extent_map *em;
2863 struct map_lookup *map;
2864 struct extent_map_tree *em_tree = &map_tree->map_tree;
2867 u64 stripe_end_offset;
2871 int stripes_allocated = 8;
2872 int stripes_required = 1;
2877 struct btrfs_multi_bio *multi = NULL;
2879 if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
2880 stripes_allocated = 1;
2883 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2888 atomic_set(&multi->error, 0);
2891 read_lock(&em_tree->lock);
2892 em = lookup_extent_mapping(em_tree, logical, *length);
2893 read_unlock(&em_tree->lock);
2896 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2897 (unsigned long long)logical,
2898 (unsigned long long)*length);
2902 BUG_ON(em->start > logical || em->start + em->len < logical);
2903 map = (struct map_lookup *)em->bdev;
2904 offset = logical - em->start;
2906 if (mirror_num > map->num_stripes)
2909 /* if our multi bio struct is too small, back off and try again */
2910 if (rw & REQ_WRITE) {
2911 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2912 BTRFS_BLOCK_GROUP_DUP)) {
2913 stripes_required = map->num_stripes;
2915 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2916 stripes_required = map->sub_stripes;
2920 if (rw & REQ_DISCARD) {
2921 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2922 BTRFS_BLOCK_GROUP_RAID1 |
2923 BTRFS_BLOCK_GROUP_DUP |
2924 BTRFS_BLOCK_GROUP_RAID10)) {
2925 stripes_required = map->num_stripes;
2928 if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
2929 stripes_allocated < stripes_required) {
2930 stripes_allocated = map->num_stripes;
2931 free_extent_map(em);
2937 * stripe_nr counts the total number of stripes we have to stride
2938 * to get to this block
2940 do_div(stripe_nr, map->stripe_len);
2942 stripe_offset = stripe_nr * map->stripe_len;
2943 BUG_ON(offset < stripe_offset);
2945 /* stripe_offset is the offset of this block in its stripe*/
2946 stripe_offset = offset - stripe_offset;
2948 if (rw & REQ_DISCARD)
2949 *length = min_t(u64, em->len - offset, *length);
2950 else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
2951 BTRFS_BLOCK_GROUP_RAID1 |
2952 BTRFS_BLOCK_GROUP_RAID10 |
2953 BTRFS_BLOCK_GROUP_DUP)) {
2954 /* we limit the length of each bio to what fits in a stripe */
2955 *length = min_t(u64, em->len - offset,
2956 map->stripe_len - stripe_offset);
2958 *length = em->len - offset;
2966 stripe_nr_orig = stripe_nr;
2967 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
2968 (~(map->stripe_len - 1));
2969 do_div(stripe_nr_end, map->stripe_len);
2970 stripe_end_offset = stripe_nr_end * map->stripe_len -
2972 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2973 if (rw & REQ_DISCARD)
2974 num_stripes = min_t(u64, map->num_stripes,
2975 stripe_nr_end - stripe_nr_orig);
2976 stripe_index = do_div(stripe_nr, map->num_stripes);
2977 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2978 if (rw & (REQ_WRITE | REQ_DISCARD))
2979 num_stripes = map->num_stripes;
2980 else if (mirror_num)
2981 stripe_index = mirror_num - 1;
2983 stripe_index = find_live_mirror(map, 0,
2985 current->pid % map->num_stripes);
2988 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2989 if (rw & (REQ_WRITE | REQ_DISCARD))
2990 num_stripes = map->num_stripes;
2991 else if (mirror_num)
2992 stripe_index = mirror_num - 1;
2994 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2995 int factor = map->num_stripes / map->sub_stripes;
2997 stripe_index = do_div(stripe_nr, factor);
2998 stripe_index *= map->sub_stripes;
3001 num_stripes = map->sub_stripes;
3002 else if (rw & REQ_DISCARD)
3003 num_stripes = min_t(u64, map->sub_stripes *
3004 (stripe_nr_end - stripe_nr_orig),
3006 else if (mirror_num)
3007 stripe_index += mirror_num - 1;
3009 stripe_index = find_live_mirror(map, stripe_index,
3010 map->sub_stripes, stripe_index +
3011 current->pid % map->sub_stripes);
3015 * after this do_div call, stripe_nr is the number of stripes
3016 * on this device we have to walk to find the data, and
3017 * stripe_index is the number of our device in the stripe array
3019 stripe_index = do_div(stripe_nr, map->num_stripes);
3021 BUG_ON(stripe_index >= map->num_stripes);
3023 if (rw & REQ_DISCARD) {
3024 for (i = 0; i < num_stripes; i++) {
3025 multi->stripes[i].physical =
3026 map->stripes[stripe_index].physical +
3027 stripe_offset + stripe_nr * map->stripe_len;
3028 multi->stripes[i].dev = map->stripes[stripe_index].dev;
3030 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3032 u32 last_stripe = 0;
3035 div_u64_rem(stripe_nr_end - 1,
3039 for (j = 0; j < map->num_stripes; j++) {
3042 div_u64_rem(stripe_nr_end - 1 - j,
3043 map->num_stripes, &test);
3044 if (test == stripe_index)
3047 stripes = stripe_nr_end - 1 - j;
3048 do_div(stripes, map->num_stripes);
3049 multi->stripes[i].length = map->stripe_len *
3050 (stripes - stripe_nr + 1);
3053 multi->stripes[i].length -=
3057 if (stripe_index == last_stripe)
3058 multi->stripes[i].length -=
3060 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3063 int factor = map->num_stripes /
3065 u32 last_stripe = 0;
3067 div_u64_rem(stripe_nr_end - 1,
3068 factor, &last_stripe);
3069 last_stripe *= map->sub_stripes;
3071 for (j = 0; j < factor; j++) {
3074 div_u64_rem(stripe_nr_end - 1 - j,
3078 stripe_index / map->sub_stripes)
3081 stripes = stripe_nr_end - 1 - j;
3082 do_div(stripes, factor);
3083 multi->stripes[i].length = map->stripe_len *
3084 (stripes - stripe_nr + 1);
3086 if (i < map->sub_stripes) {
3087 multi->stripes[i].length -=
3089 if (i == map->sub_stripes - 1)
3092 if (stripe_index >= last_stripe &&
3093 stripe_index <= (last_stripe +
3094 map->sub_stripes - 1)) {
3095 multi->stripes[i].length -=
3099 multi->stripes[i].length = *length;
3102 if (stripe_index == map->num_stripes) {
3103 /* This could only happen for RAID0/10 */
3109 for (i = 0; i < num_stripes; i++) {
3110 multi->stripes[i].physical =
3111 map->stripes[stripe_index].physical +
3113 stripe_nr * map->stripe_len;
3114 multi->stripes[i].dev =
3115 map->stripes[stripe_index].dev;
3121 multi->num_stripes = num_stripes;
3122 multi->max_errors = max_errors;
3125 free_extent_map(em);
3129 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
3130 u64 logical, u64 *length,
3131 struct btrfs_multi_bio **multi_ret, int mirror_num)
3133 return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
3137 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
3138 u64 chunk_start, u64 physical, u64 devid,
3139 u64 **logical, int *naddrs, int *stripe_len)
3141 struct extent_map_tree *em_tree = &map_tree->map_tree;
3142 struct extent_map *em;
3143 struct map_lookup *map;
3150 read_lock(&em_tree->lock);
3151 em = lookup_extent_mapping(em_tree, chunk_start, 1);
3152 read_unlock(&em_tree->lock);
3154 BUG_ON(!em || em->start != chunk_start);
3155 map = (struct map_lookup *)em->bdev;
3158 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
3159 do_div(length, map->num_stripes / map->sub_stripes);
3160 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
3161 do_div(length, map->num_stripes);
3163 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
3166 for (i = 0; i < map->num_stripes; i++) {
3167 if (devid && map->stripes[i].dev->devid != devid)
3169 if (map->stripes[i].physical > physical ||
3170 map->stripes[i].physical + length <= physical)
3173 stripe_nr = physical - map->stripes[i].physical;
3174 do_div(stripe_nr, map->stripe_len);
3176 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
3177 stripe_nr = stripe_nr * map->num_stripes + i;
3178 do_div(stripe_nr, map->sub_stripes);
3179 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
3180 stripe_nr = stripe_nr * map->num_stripes + i;
3182 bytenr = chunk_start + stripe_nr * map->stripe_len;
3183 WARN_ON(nr >= map->num_stripes);
3184 for (j = 0; j < nr; j++) {
3185 if (buf[j] == bytenr)
3189 WARN_ON(nr >= map->num_stripes);
3196 *stripe_len = map->stripe_len;
3198 free_extent_map(em);
3202 static void end_bio_multi_stripe(struct bio *bio, int err)
3204 struct btrfs_multi_bio *multi = bio->bi_private;
3205 int is_orig_bio = 0;
3208 atomic_inc(&multi->error);
3210 if (bio == multi->orig_bio)
3213 if (atomic_dec_and_test(&multi->stripes_pending)) {
3216 bio = multi->orig_bio;
3218 bio->bi_private = multi->private;
3219 bio->bi_end_io = multi->end_io;
3220 /* only send an error to the higher layers if it is
3221 * beyond the tolerance of the multi-bio
3223 if (atomic_read(&multi->error) > multi->max_errors) {
3227 * this bio is actually up to date, we didn't
3228 * go over the max number of errors
3230 set_bit(BIO_UPTODATE, &bio->bi_flags);
3235 bio_endio(bio, err);
3236 } else if (!is_orig_bio) {
3241 struct async_sched {
3244 struct btrfs_fs_info *info;
3245 struct btrfs_work work;
3249 * see run_scheduled_bios for a description of why bios are collected for
3252 * This will add one bio to the pending list for a device and make sure
3253 * the work struct is scheduled.
3255 static noinline int schedule_bio(struct btrfs_root *root,
3256 struct btrfs_device *device,
3257 int rw, struct bio *bio)
3259 int should_queue = 1;
3260 struct btrfs_pending_bios *pending_bios;
3262 /* don't bother with additional async steps for reads, right now */
3263 if (!(rw & REQ_WRITE)) {
3265 submit_bio(rw, bio);
3271 * nr_async_bios allows us to reliably return congestion to the
3272 * higher layers. Otherwise, the async bio makes it appear we have
3273 * made progress against dirty pages when we've really just put it
3274 * on a queue for later
3276 atomic_inc(&root->fs_info->nr_async_bios);
3277 WARN_ON(bio->bi_next);
3278 bio->bi_next = NULL;
3281 spin_lock(&device->io_lock);
3282 if (bio->bi_rw & REQ_SYNC)
3283 pending_bios = &device->pending_sync_bios;
3285 pending_bios = &device->pending_bios;
3287 if (pending_bios->tail)
3288 pending_bios->tail->bi_next = bio;
3290 pending_bios->tail = bio;
3291 if (!pending_bios->head)
3292 pending_bios->head = bio;
3293 if (device->running_pending)
3296 spin_unlock(&device->io_lock);
3299 btrfs_queue_worker(&root->fs_info->submit_workers,
3304 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
3305 int mirror_num, int async_submit)
3307 struct btrfs_mapping_tree *map_tree;
3308 struct btrfs_device *dev;
3309 struct bio *first_bio = bio;
3310 u64 logical = (u64)bio->bi_sector << 9;
3313 struct btrfs_multi_bio *multi = NULL;
3318 length = bio->bi_size;
3319 map_tree = &root->fs_info->mapping_tree;
3320 map_length = length;
3322 ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
3326 total_devs = multi->num_stripes;
3327 if (map_length < length) {
3328 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
3329 "len %llu\n", (unsigned long long)logical,
3330 (unsigned long long)length,
3331 (unsigned long long)map_length);
3334 multi->end_io = first_bio->bi_end_io;
3335 multi->private = first_bio->bi_private;
3336 multi->orig_bio = first_bio;
3337 atomic_set(&multi->stripes_pending, multi->num_stripes);
3339 while (dev_nr < total_devs) {
3340 if (total_devs > 1) {
3341 if (dev_nr < total_devs - 1) {
3342 bio = bio_clone(first_bio, GFP_NOFS);
3347 bio->bi_private = multi;
3348 bio->bi_end_io = end_bio_multi_stripe;
3350 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
3351 dev = multi->stripes[dev_nr].dev;
3352 if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
3353 bio->bi_bdev = dev->bdev;
3355 schedule_bio(root, dev, rw, bio);
3357 submit_bio(rw, bio);
3359 bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
3360 bio->bi_sector = logical >> 9;
3361 bio_endio(bio, -EIO);
3365 if (total_devs == 1)
3370 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
3373 struct btrfs_device *device;
3374 struct btrfs_fs_devices *cur_devices;
3376 cur_devices = root->fs_info->fs_devices;
3377 while (cur_devices) {
3379 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3380 device = __find_device(&cur_devices->devices,
3385 cur_devices = cur_devices->seed;
3390 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
3391 u64 devid, u8 *dev_uuid)
3393 struct btrfs_device *device;
3394 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
3396 device = kzalloc(sizeof(*device), GFP_NOFS);
3399 list_add(&device->dev_list,
3400 &fs_devices->devices);
3401 device->dev_root = root->fs_info->dev_root;
3402 device->devid = devid;
3403 device->work.func = pending_bios_fn;
3404 device->fs_devices = fs_devices;
3405 device->missing = 1;
3406 fs_devices->num_devices++;
3407 fs_devices->missing_devices++;
3408 spin_lock_init(&device->io_lock);
3409 INIT_LIST_HEAD(&device->dev_alloc_list);
3410 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3414 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3415 struct extent_buffer *leaf,
3416 struct btrfs_chunk *chunk)
3418 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3419 struct map_lookup *map;
3420 struct extent_map *em;
3424 u8 uuid[BTRFS_UUID_SIZE];
3429 logical = key->offset;
3430 length = btrfs_chunk_length(leaf, chunk);
3432 read_lock(&map_tree->map_tree.lock);
3433 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3434 read_unlock(&map_tree->map_tree.lock);
3436 /* already mapped? */
3437 if (em && em->start <= logical && em->start + em->len > logical) {
3438 free_extent_map(em);
3441 free_extent_map(em);
3444 em = alloc_extent_map();
3447 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3448 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3450 free_extent_map(em);
3454 em->bdev = (struct block_device *)map;
3455 em->start = logical;
3457 em->block_start = 0;
3458 em->block_len = em->len;
3460 map->num_stripes = num_stripes;
3461 map->io_width = btrfs_chunk_io_width(leaf, chunk);
3462 map->io_align = btrfs_chunk_io_align(leaf, chunk);
3463 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3464 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3465 map->type = btrfs_chunk_type(leaf, chunk);
3466 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3467 for (i = 0; i < num_stripes; i++) {
3468 map->stripes[i].physical =
3469 btrfs_stripe_offset_nr(leaf, chunk, i);
3470 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3471 read_extent_buffer(leaf, uuid, (unsigned long)
3472 btrfs_stripe_dev_uuid_nr(chunk, i),
3474 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3476 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3478 free_extent_map(em);
3481 if (!map->stripes[i].dev) {
3482 map->stripes[i].dev =
3483 add_missing_dev(root, devid, uuid);
3484 if (!map->stripes[i].dev) {
3486 free_extent_map(em);
3490 map->stripes[i].dev->in_fs_metadata = 1;
3493 write_lock(&map_tree->map_tree.lock);
3494 ret = add_extent_mapping(&map_tree->map_tree, em);
3495 write_unlock(&map_tree->map_tree.lock);
3497 free_extent_map(em);
3502 static int fill_device_from_item(struct extent_buffer *leaf,
3503 struct btrfs_dev_item *dev_item,
3504 struct btrfs_device *device)
3508 device->devid = btrfs_device_id(leaf, dev_item);
3509 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3510 device->total_bytes = device->disk_total_bytes;
3511 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3512 device->type = btrfs_device_type(leaf, dev_item);
3513 device->io_align = btrfs_device_io_align(leaf, dev_item);
3514 device->io_width = btrfs_device_io_width(leaf, dev_item);
3515 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3517 ptr = (unsigned long)btrfs_device_uuid(dev_item);
3518 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3523 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3525 struct btrfs_fs_devices *fs_devices;
3528 mutex_lock(&uuid_mutex);
3530 fs_devices = root->fs_info->fs_devices->seed;
3531 while (fs_devices) {
3532 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3536 fs_devices = fs_devices->seed;
3539 fs_devices = find_fsid(fsid);
3545 fs_devices = clone_fs_devices(fs_devices);
3546 if (IS_ERR(fs_devices)) {
3547 ret = PTR_ERR(fs_devices);
3551 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3552 root->fs_info->bdev_holder);
3556 if (!fs_devices->seeding) {
3557 __btrfs_close_devices(fs_devices);
3558 free_fs_devices(fs_devices);
3563 fs_devices->seed = root->fs_info->fs_devices->seed;
3564 root->fs_info->fs_devices->seed = fs_devices;
3566 mutex_unlock(&uuid_mutex);
3570 static int read_one_dev(struct btrfs_root *root,
3571 struct extent_buffer *leaf,
3572 struct btrfs_dev_item *dev_item)
3574 struct btrfs_device *device;
3577 u8 fs_uuid[BTRFS_UUID_SIZE];
3578 u8 dev_uuid[BTRFS_UUID_SIZE];
3580 devid = btrfs_device_id(leaf, dev_item);
3581 read_extent_buffer(leaf, dev_uuid,
3582 (unsigned long)btrfs_device_uuid(dev_item),
3584 read_extent_buffer(leaf, fs_uuid,
3585 (unsigned long)btrfs_device_fsid(dev_item),
3588 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3589 ret = open_seed_devices(root, fs_uuid);
3590 if (ret && !btrfs_test_opt(root, DEGRADED))
3594 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3595 if (!device || !device->bdev) {
3596 if (!btrfs_test_opt(root, DEGRADED))
3600 printk(KERN_WARNING "warning devid %llu missing\n",
3601 (unsigned long long)devid);
3602 device = add_missing_dev(root, devid, dev_uuid);
3605 } else if (!device->missing) {
3607 * this happens when a device that was properly setup
3608 * in the device info lists suddenly goes bad.
3609 * device->bdev is NULL, and so we have to set
3610 * device->missing to one here
3612 root->fs_info->fs_devices->missing_devices++;
3613 device->missing = 1;
3617 if (device->fs_devices != root->fs_info->fs_devices) {
3618 BUG_ON(device->writeable);
3619 if (device->generation !=
3620 btrfs_device_generation(leaf, dev_item))
3624 fill_device_from_item(leaf, dev_item, device);
3625 device->dev_root = root->fs_info->dev_root;
3626 device->in_fs_metadata = 1;
3627 if (device->writeable)
3628 device->fs_devices->total_rw_bytes += device->total_bytes;
3633 int btrfs_read_sys_array(struct btrfs_root *root)
3635 struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3636 struct extent_buffer *sb;
3637 struct btrfs_disk_key *disk_key;
3638 struct btrfs_chunk *chunk;
3640 unsigned long sb_ptr;
3646 struct btrfs_key key;
3648 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3649 BTRFS_SUPER_INFO_SIZE);
3652 btrfs_set_buffer_uptodate(sb);
3653 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
3655 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3656 array_size = btrfs_super_sys_array_size(super_copy);
3658 ptr = super_copy->sys_chunk_array;
3659 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3662 while (cur < array_size) {
3663 disk_key = (struct btrfs_disk_key *)ptr;
3664 btrfs_disk_key_to_cpu(&key, disk_key);
3666 len = sizeof(*disk_key); ptr += len;
3670 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3671 chunk = (struct btrfs_chunk *)sb_ptr;
3672 ret = read_one_chunk(root, &key, sb, chunk);
3675 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3676 len = btrfs_chunk_item_size(num_stripes);
3685 free_extent_buffer(sb);
3689 int btrfs_read_chunk_tree(struct btrfs_root *root)
3691 struct btrfs_path *path;
3692 struct extent_buffer *leaf;
3693 struct btrfs_key key;
3694 struct btrfs_key found_key;
3698 root = root->fs_info->chunk_root;
3700 path = btrfs_alloc_path();
3704 /* first we search for all of the device items, and then we
3705 * read in all of the chunk items. This way we can create chunk
3706 * mappings that reference all of the devices that are afound
3708 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3712 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3716 leaf = path->nodes[0];
3717 slot = path->slots[0];
3718 if (slot >= btrfs_header_nritems(leaf)) {
3719 ret = btrfs_next_leaf(root, path);
3726 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3727 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3728 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3730 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3731 struct btrfs_dev_item *dev_item;
3732 dev_item = btrfs_item_ptr(leaf, slot,
3733 struct btrfs_dev_item);
3734 ret = read_one_dev(root, leaf, dev_item);
3738 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3739 struct btrfs_chunk *chunk;
3740 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3741 ret = read_one_chunk(root, &found_key, leaf, chunk);
3747 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3749 btrfs_release_path(path);
3754 btrfs_free_path(path);