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 <linux/ratelimit.h>
27 #include <linux/kthread.h>
28 #include <linux/raid/pq.h>
29 #include <linux/semaphore.h>
30 #include <asm/div64.h>
32 #include "extent_map.h"
34 #include "transaction.h"
35 #include "print-tree.h"
38 #include "async-thread.h"
39 #include "check-integrity.h"
40 #include "rcu-string.h"
42 #include "dev-replace.h"
45 static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
49 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
50 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
51 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
53 DEFINE_MUTEX(uuid_mutex);
54 static LIST_HEAD(fs_uuids);
56 static void lock_chunks(struct btrfs_root *root)
58 mutex_lock(&root->fs_info->chunk_mutex);
61 static void unlock_chunks(struct btrfs_root *root)
63 mutex_unlock(&root->fs_info->chunk_mutex);
66 static struct btrfs_fs_devices *__alloc_fs_devices(void)
68 struct btrfs_fs_devices *fs_devs;
70 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
72 return ERR_PTR(-ENOMEM);
74 mutex_init(&fs_devs->device_list_mutex);
76 INIT_LIST_HEAD(&fs_devs->devices);
77 INIT_LIST_HEAD(&fs_devs->resized_devices);
78 INIT_LIST_HEAD(&fs_devs->alloc_list);
79 INIT_LIST_HEAD(&fs_devs->list);
85 * alloc_fs_devices - allocate struct btrfs_fs_devices
86 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
89 * Return: a pointer to a new &struct btrfs_fs_devices on success;
90 * ERR_PTR() on error. Returned struct is not linked onto any lists and
91 * can be destroyed with kfree() right away.
93 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
95 struct btrfs_fs_devices *fs_devs;
97 fs_devs = __alloc_fs_devices();
102 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
104 generate_random_uuid(fs_devs->fsid);
109 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
111 struct btrfs_device *device;
112 WARN_ON(fs_devices->opened);
113 while (!list_empty(&fs_devices->devices)) {
114 device = list_entry(fs_devices->devices.next,
115 struct btrfs_device, dev_list);
116 list_del(&device->dev_list);
117 rcu_string_free(device->name);
123 static void btrfs_kobject_uevent(struct block_device *bdev,
124 enum kobject_action action)
128 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
130 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
132 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
133 &disk_to_dev(bdev->bd_disk)->kobj);
136 void btrfs_cleanup_fs_uuids(void)
138 struct btrfs_fs_devices *fs_devices;
140 while (!list_empty(&fs_uuids)) {
141 fs_devices = list_entry(fs_uuids.next,
142 struct btrfs_fs_devices, list);
143 list_del(&fs_devices->list);
144 free_fs_devices(fs_devices);
148 static struct btrfs_device *__alloc_device(void)
150 struct btrfs_device *dev;
152 dev = kzalloc(sizeof(*dev), GFP_NOFS);
154 return ERR_PTR(-ENOMEM);
156 INIT_LIST_HEAD(&dev->dev_list);
157 INIT_LIST_HEAD(&dev->dev_alloc_list);
158 INIT_LIST_HEAD(&dev->resized_list);
160 spin_lock_init(&dev->io_lock);
162 spin_lock_init(&dev->reada_lock);
163 atomic_set(&dev->reada_in_flight, 0);
164 atomic_set(&dev->dev_stats_ccnt, 0);
165 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
166 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
171 static noinline struct btrfs_device *__find_device(struct list_head *head,
174 struct btrfs_device *dev;
176 list_for_each_entry(dev, head, dev_list) {
177 if (dev->devid == devid &&
178 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
185 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
187 struct btrfs_fs_devices *fs_devices;
189 list_for_each_entry(fs_devices, &fs_uuids, list) {
190 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
197 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
198 int flush, struct block_device **bdev,
199 struct buffer_head **bh)
203 *bdev = blkdev_get_by_path(device_path, flags, holder);
206 ret = PTR_ERR(*bdev);
207 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
212 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
213 ret = set_blocksize(*bdev, 4096);
215 blkdev_put(*bdev, flags);
218 invalidate_bdev(*bdev);
219 *bh = btrfs_read_dev_super(*bdev);
222 blkdev_put(*bdev, flags);
234 static void requeue_list(struct btrfs_pending_bios *pending_bios,
235 struct bio *head, struct bio *tail)
238 struct bio *old_head;
240 old_head = pending_bios->head;
241 pending_bios->head = head;
242 if (pending_bios->tail)
243 tail->bi_next = old_head;
245 pending_bios->tail = tail;
249 * we try to collect pending bios for a device so we don't get a large
250 * number of procs sending bios down to the same device. This greatly
251 * improves the schedulers ability to collect and merge the bios.
253 * But, it also turns into a long list of bios to process and that is sure
254 * to eventually make the worker thread block. The solution here is to
255 * make some progress and then put this work struct back at the end of
256 * the list if the block device is congested. This way, multiple devices
257 * can make progress from a single worker thread.
259 static noinline void run_scheduled_bios(struct btrfs_device *device)
262 struct backing_dev_info *bdi;
263 struct btrfs_fs_info *fs_info;
264 struct btrfs_pending_bios *pending_bios;
268 unsigned long num_run;
269 unsigned long batch_run = 0;
271 unsigned long last_waited = 0;
273 int sync_pending = 0;
274 struct blk_plug plug;
277 * this function runs all the bios we've collected for
278 * a particular device. We don't want to wander off to
279 * another device without first sending all of these down.
280 * So, setup a plug here and finish it off before we return
282 blk_start_plug(&plug);
284 bdi = blk_get_backing_dev_info(device->bdev);
285 fs_info = device->dev_root->fs_info;
286 limit = btrfs_async_submit_limit(fs_info);
287 limit = limit * 2 / 3;
290 spin_lock(&device->io_lock);
295 /* take all the bios off the list at once and process them
296 * later on (without the lock held). But, remember the
297 * tail and other pointers so the bios can be properly reinserted
298 * into the list if we hit congestion
300 if (!force_reg && device->pending_sync_bios.head) {
301 pending_bios = &device->pending_sync_bios;
304 pending_bios = &device->pending_bios;
308 pending = pending_bios->head;
309 tail = pending_bios->tail;
310 WARN_ON(pending && !tail);
313 * if pending was null this time around, no bios need processing
314 * at all and we can stop. Otherwise it'll loop back up again
315 * and do an additional check so no bios are missed.
317 * device->running_pending is used to synchronize with the
320 if (device->pending_sync_bios.head == NULL &&
321 device->pending_bios.head == NULL) {
323 device->running_pending = 0;
326 device->running_pending = 1;
329 pending_bios->head = NULL;
330 pending_bios->tail = NULL;
332 spin_unlock(&device->io_lock);
337 /* we want to work on both lists, but do more bios on the
338 * sync list than the regular list
341 pending_bios != &device->pending_sync_bios &&
342 device->pending_sync_bios.head) ||
343 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
344 device->pending_bios.head)) {
345 spin_lock(&device->io_lock);
346 requeue_list(pending_bios, pending, tail);
351 pending = pending->bi_next;
354 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
355 waitqueue_active(&fs_info->async_submit_wait))
356 wake_up(&fs_info->async_submit_wait);
358 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
361 * if we're doing the sync list, record that our
362 * plug has some sync requests on it
364 * If we're doing the regular list and there are
365 * sync requests sitting around, unplug before
368 if (pending_bios == &device->pending_sync_bios) {
370 } else if (sync_pending) {
371 blk_finish_plug(&plug);
372 blk_start_plug(&plug);
376 btrfsic_submit_bio(cur->bi_rw, cur);
383 * we made progress, there is more work to do and the bdi
384 * is now congested. Back off and let other work structs
387 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
388 fs_info->fs_devices->open_devices > 1) {
389 struct io_context *ioc;
391 ioc = current->io_context;
394 * the main goal here is that we don't want to
395 * block if we're going to be able to submit
396 * more requests without blocking.
398 * This code does two great things, it pokes into
399 * the elevator code from a filesystem _and_
400 * it makes assumptions about how batching works.
402 if (ioc && ioc->nr_batch_requests > 0 &&
403 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
405 ioc->last_waited == last_waited)) {
407 * we want to go through our batch of
408 * requests and stop. So, we copy out
409 * the ioc->last_waited time and test
410 * against it before looping
412 last_waited = ioc->last_waited;
417 spin_lock(&device->io_lock);
418 requeue_list(pending_bios, pending, tail);
419 device->running_pending = 1;
421 spin_unlock(&device->io_lock);
422 btrfs_queue_work(fs_info->submit_workers,
426 /* unplug every 64 requests just for good measure */
427 if (batch_run % 64 == 0) {
428 blk_finish_plug(&plug);
429 blk_start_plug(&plug);
438 spin_lock(&device->io_lock);
439 if (device->pending_bios.head || device->pending_sync_bios.head)
441 spin_unlock(&device->io_lock);
444 blk_finish_plug(&plug);
447 static void pending_bios_fn(struct btrfs_work *work)
449 struct btrfs_device *device;
451 device = container_of(work, struct btrfs_device, work);
452 run_scheduled_bios(device);
456 * Add new device to list of registered devices
459 * 1 - first time device is seen
460 * 0 - device already known
463 static noinline int device_list_add(const char *path,
464 struct btrfs_super_block *disk_super,
465 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
467 struct btrfs_device *device;
468 struct btrfs_fs_devices *fs_devices;
469 struct rcu_string *name;
471 u64 found_transid = btrfs_super_generation(disk_super);
473 fs_devices = find_fsid(disk_super->fsid);
475 fs_devices = alloc_fs_devices(disk_super->fsid);
476 if (IS_ERR(fs_devices))
477 return PTR_ERR(fs_devices);
479 list_add(&fs_devices->list, &fs_uuids);
483 device = __find_device(&fs_devices->devices, devid,
484 disk_super->dev_item.uuid);
488 if (fs_devices->opened)
491 device = btrfs_alloc_device(NULL, &devid,
492 disk_super->dev_item.uuid);
493 if (IS_ERR(device)) {
494 /* we can safely leave the fs_devices entry around */
495 return PTR_ERR(device);
498 name = rcu_string_strdup(path, GFP_NOFS);
503 rcu_assign_pointer(device->name, name);
505 mutex_lock(&fs_devices->device_list_mutex);
506 list_add_rcu(&device->dev_list, &fs_devices->devices);
507 fs_devices->num_devices++;
508 mutex_unlock(&fs_devices->device_list_mutex);
511 device->fs_devices = fs_devices;
512 } else if (!device->name || strcmp(device->name->str, path)) {
514 * When FS is already mounted.
515 * 1. If you are here and if the device->name is NULL that
516 * means this device was missing at time of FS mount.
517 * 2. If you are here and if the device->name is different
518 * from 'path' that means either
519 * a. The same device disappeared and reappeared with
521 * b. The missing-disk-which-was-replaced, has
524 * We must allow 1 and 2a above. But 2b would be a spurious
527 * Further in case of 1 and 2a above, the disk at 'path'
528 * would have missed some transaction when it was away and
529 * in case of 2a the stale bdev has to be updated as well.
530 * 2b must not be allowed at all time.
534 * For now, we do allow update to btrfs_fs_device through the
535 * btrfs dev scan cli after FS has been mounted. We're still
536 * tracking a problem where systems fail mount by subvolume id
537 * when we reject replacement on a mounted FS.
539 if (!fs_devices->opened && found_transid < device->generation) {
541 * That is if the FS is _not_ mounted and if you
542 * are here, that means there is more than one
543 * disk with same uuid and devid.We keep the one
544 * with larger generation number or the last-in if
545 * generation are equal.
550 name = rcu_string_strdup(path, GFP_NOFS);
553 rcu_string_free(device->name);
554 rcu_assign_pointer(device->name, name);
555 if (device->missing) {
556 fs_devices->missing_devices--;
562 * Unmount does not free the btrfs_device struct but would zero
563 * generation along with most of the other members. So just update
564 * it back. We need it to pick the disk with largest generation
567 if (!fs_devices->opened)
568 device->generation = found_transid;
570 *fs_devices_ret = fs_devices;
575 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
577 struct btrfs_fs_devices *fs_devices;
578 struct btrfs_device *device;
579 struct btrfs_device *orig_dev;
581 fs_devices = alloc_fs_devices(orig->fsid);
582 if (IS_ERR(fs_devices))
585 mutex_lock(&orig->device_list_mutex);
586 fs_devices->total_devices = orig->total_devices;
588 /* We have held the volume lock, it is safe to get the devices. */
589 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
590 struct rcu_string *name;
592 device = btrfs_alloc_device(NULL, &orig_dev->devid,
598 * This is ok to do without rcu read locked because we hold the
599 * uuid mutex so nothing we touch in here is going to disappear.
601 if (orig_dev->name) {
602 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
607 rcu_assign_pointer(device->name, name);
610 list_add(&device->dev_list, &fs_devices->devices);
611 device->fs_devices = fs_devices;
612 fs_devices->num_devices++;
614 mutex_unlock(&orig->device_list_mutex);
617 mutex_unlock(&orig->device_list_mutex);
618 free_fs_devices(fs_devices);
619 return ERR_PTR(-ENOMEM);
622 void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
623 struct btrfs_fs_devices *fs_devices, int step)
625 struct btrfs_device *device, *next;
626 struct btrfs_device *latest_dev = NULL;
628 mutex_lock(&uuid_mutex);
630 /* This is the initialized path, it is safe to release the devices. */
631 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
632 if (device->in_fs_metadata) {
633 if (!device->is_tgtdev_for_dev_replace &&
635 device->generation > latest_dev->generation)) {
641 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
643 * In the first step, keep the device which has
644 * the correct fsid and the devid that is used
645 * for the dev_replace procedure.
646 * In the second step, the dev_replace state is
647 * read from the device tree and it is known
648 * whether the procedure is really active or
649 * not, which means whether this device is
650 * used or whether it should be removed.
652 if (step == 0 || device->is_tgtdev_for_dev_replace) {
657 blkdev_put(device->bdev, device->mode);
659 fs_devices->open_devices--;
661 if (device->writeable) {
662 list_del_init(&device->dev_alloc_list);
663 device->writeable = 0;
664 if (!device->is_tgtdev_for_dev_replace)
665 fs_devices->rw_devices--;
667 list_del_init(&device->dev_list);
668 fs_devices->num_devices--;
669 rcu_string_free(device->name);
673 if (fs_devices->seed) {
674 fs_devices = fs_devices->seed;
678 fs_devices->latest_bdev = latest_dev->bdev;
680 mutex_unlock(&uuid_mutex);
683 static void __free_device(struct work_struct *work)
685 struct btrfs_device *device;
687 device = container_of(work, struct btrfs_device, rcu_work);
690 blkdev_put(device->bdev, device->mode);
692 rcu_string_free(device->name);
696 static void free_device(struct rcu_head *head)
698 struct btrfs_device *device;
700 device = container_of(head, struct btrfs_device, rcu);
702 INIT_WORK(&device->rcu_work, __free_device);
703 schedule_work(&device->rcu_work);
706 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
708 struct btrfs_device *device;
710 if (--fs_devices->opened > 0)
713 mutex_lock(&fs_devices->device_list_mutex);
714 list_for_each_entry(device, &fs_devices->devices, dev_list) {
715 struct btrfs_device *new_device;
716 struct rcu_string *name;
719 fs_devices->open_devices--;
721 if (device->writeable &&
722 device->devid != BTRFS_DEV_REPLACE_DEVID) {
723 list_del_init(&device->dev_alloc_list);
724 fs_devices->rw_devices--;
728 fs_devices->missing_devices--;
730 new_device = btrfs_alloc_device(NULL, &device->devid,
732 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
734 /* Safe because we are under uuid_mutex */
736 name = rcu_string_strdup(device->name->str, GFP_NOFS);
737 BUG_ON(!name); /* -ENOMEM */
738 rcu_assign_pointer(new_device->name, name);
741 list_replace_rcu(&device->dev_list, &new_device->dev_list);
742 new_device->fs_devices = device->fs_devices;
744 call_rcu(&device->rcu, free_device);
746 mutex_unlock(&fs_devices->device_list_mutex);
748 WARN_ON(fs_devices->open_devices);
749 WARN_ON(fs_devices->rw_devices);
750 fs_devices->opened = 0;
751 fs_devices->seeding = 0;
756 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
758 struct btrfs_fs_devices *seed_devices = NULL;
761 mutex_lock(&uuid_mutex);
762 ret = __btrfs_close_devices(fs_devices);
763 if (!fs_devices->opened) {
764 seed_devices = fs_devices->seed;
765 fs_devices->seed = NULL;
767 mutex_unlock(&uuid_mutex);
769 while (seed_devices) {
770 fs_devices = seed_devices;
771 seed_devices = fs_devices->seed;
772 __btrfs_close_devices(fs_devices);
773 free_fs_devices(fs_devices);
776 * Wait for rcu kworkers under __btrfs_close_devices
777 * to finish all blkdev_puts so device is really
778 * free when umount is done.
784 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
785 fmode_t flags, void *holder)
787 struct request_queue *q;
788 struct block_device *bdev;
789 struct list_head *head = &fs_devices->devices;
790 struct btrfs_device *device;
791 struct btrfs_device *latest_dev = NULL;
792 struct buffer_head *bh;
793 struct btrfs_super_block *disk_super;
800 list_for_each_entry(device, head, dev_list) {
806 /* Just open everything we can; ignore failures here */
807 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
811 disk_super = (struct btrfs_super_block *)bh->b_data;
812 devid = btrfs_stack_device_id(&disk_super->dev_item);
813 if (devid != device->devid)
816 if (memcmp(device->uuid, disk_super->dev_item.uuid,
820 device->generation = btrfs_super_generation(disk_super);
822 device->generation > latest_dev->generation)
825 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
826 device->writeable = 0;
828 device->writeable = !bdev_read_only(bdev);
832 q = bdev_get_queue(bdev);
833 if (blk_queue_discard(q))
834 device->can_discard = 1;
837 device->in_fs_metadata = 0;
838 device->mode = flags;
840 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
841 fs_devices->rotating = 1;
843 fs_devices->open_devices++;
844 if (device->writeable &&
845 device->devid != BTRFS_DEV_REPLACE_DEVID) {
846 fs_devices->rw_devices++;
847 list_add(&device->dev_alloc_list,
848 &fs_devices->alloc_list);
855 blkdev_put(bdev, flags);
858 if (fs_devices->open_devices == 0) {
862 fs_devices->seeding = seeding;
863 fs_devices->opened = 1;
864 fs_devices->latest_bdev = latest_dev->bdev;
865 fs_devices->total_rw_bytes = 0;
870 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
871 fmode_t flags, void *holder)
875 mutex_lock(&uuid_mutex);
876 if (fs_devices->opened) {
877 fs_devices->opened++;
880 ret = __btrfs_open_devices(fs_devices, flags, holder);
882 mutex_unlock(&uuid_mutex);
887 * Look for a btrfs signature on a device. This may be called out of the mount path
888 * and we are not allowed to call set_blocksize during the scan. The superblock
889 * is read via pagecache
891 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
892 struct btrfs_fs_devices **fs_devices_ret)
894 struct btrfs_super_block *disk_super;
895 struct block_device *bdev;
906 * we would like to check all the supers, but that would make
907 * a btrfs mount succeed after a mkfs from a different FS.
908 * So, we need to add a special mount option to scan for
909 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
911 bytenr = btrfs_sb_offset(0);
913 mutex_lock(&uuid_mutex);
915 bdev = blkdev_get_by_path(path, flags, holder);
922 /* make sure our super fits in the device */
923 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
926 /* make sure our super fits in the page */
927 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
930 /* make sure our super doesn't straddle pages on disk */
931 index = bytenr >> PAGE_CACHE_SHIFT;
932 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
935 /* pull in the page with our super */
936 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
939 if (IS_ERR_OR_NULL(page))
944 /* align our pointer to the offset of the super block */
945 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
947 if (btrfs_super_bytenr(disk_super) != bytenr ||
948 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
951 devid = btrfs_stack_device_id(&disk_super->dev_item);
952 transid = btrfs_super_generation(disk_super);
953 total_devices = btrfs_super_num_devices(disk_super);
955 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
957 if (disk_super->label[0]) {
958 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
959 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
960 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
962 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
965 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
968 if (!ret && fs_devices_ret)
969 (*fs_devices_ret)->total_devices = total_devices;
973 page_cache_release(page);
976 blkdev_put(bdev, flags);
978 mutex_unlock(&uuid_mutex);
982 /* helper to account the used device space in the range */
983 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
984 u64 end, u64 *length)
986 struct btrfs_key key;
987 struct btrfs_root *root = device->dev_root;
988 struct btrfs_dev_extent *dev_extent;
989 struct btrfs_path *path;
993 struct extent_buffer *l;
997 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
1000 path = btrfs_alloc_path();
1005 key.objectid = device->devid;
1007 key.type = BTRFS_DEV_EXTENT_KEY;
1009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1013 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1020 slot = path->slots[0];
1021 if (slot >= btrfs_header_nritems(l)) {
1022 ret = btrfs_next_leaf(root, path);
1030 btrfs_item_key_to_cpu(l, &key, slot);
1032 if (key.objectid < device->devid)
1035 if (key.objectid > device->devid)
1038 if (key.type != BTRFS_DEV_EXTENT_KEY)
1041 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1042 extent_end = key.offset + btrfs_dev_extent_length(l,
1044 if (key.offset <= start && extent_end > end) {
1045 *length = end - start + 1;
1047 } else if (key.offset <= start && extent_end > start)
1048 *length += extent_end - start;
1049 else if (key.offset > start && extent_end <= end)
1050 *length += extent_end - key.offset;
1051 else if (key.offset > start && key.offset <= end) {
1052 *length += end - key.offset + 1;
1054 } else if (key.offset > end)
1062 btrfs_free_path(path);
1066 static int contains_pending_extent(struct btrfs_trans_handle *trans,
1067 struct btrfs_device *device,
1068 u64 *start, u64 len)
1070 struct extent_map *em;
1073 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
1074 struct map_lookup *map;
1077 map = (struct map_lookup *)em->bdev;
1078 for (i = 0; i < map->num_stripes; i++) {
1079 if (map->stripes[i].dev != device)
1081 if (map->stripes[i].physical >= *start + len ||
1082 map->stripes[i].physical + em->orig_block_len <=
1085 *start = map->stripes[i].physical +
1096 * find_free_dev_extent - find free space in the specified device
1097 * @device: the device which we search the free space in
1098 * @num_bytes: the size of the free space that we need
1099 * @start: store the start of the free space.
1100 * @len: the size of the free space. that we find, or the size of the max
1101 * free space if we don't find suitable free space
1103 * this uses a pretty simple search, the expectation is that it is
1104 * called very infrequently and that a given device has a small number
1107 * @start is used to store the start of the free space if we find. But if we
1108 * don't find suitable free space, it will be used to store the start position
1109 * of the max free space.
1111 * @len is used to store the size of the free space that we find.
1112 * But if we don't find suitable free space, it is used to store the size of
1113 * the max free space.
1115 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1116 struct btrfs_device *device, u64 num_bytes,
1117 u64 *start, u64 *len)
1119 struct btrfs_key key;
1120 struct btrfs_root *root = device->dev_root;
1121 struct btrfs_dev_extent *dev_extent;
1122 struct btrfs_path *path;
1128 u64 search_end = device->total_bytes;
1131 struct extent_buffer *l;
1133 /* FIXME use last free of some kind */
1135 /* we don't want to overwrite the superblock on the drive,
1136 * so we make sure to start at an offset of at least 1MB
1138 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1140 path = btrfs_alloc_path();
1144 max_hole_start = search_start;
1148 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
1154 path->search_commit_root = 1;
1155 path->skip_locking = 1;
1157 key.objectid = device->devid;
1158 key.offset = search_start;
1159 key.type = BTRFS_DEV_EXTENT_KEY;
1161 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1165 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1172 slot = path->slots[0];
1173 if (slot >= btrfs_header_nritems(l)) {
1174 ret = btrfs_next_leaf(root, path);
1182 btrfs_item_key_to_cpu(l, &key, slot);
1184 if (key.objectid < device->devid)
1187 if (key.objectid > device->devid)
1190 if (key.type != BTRFS_DEV_EXTENT_KEY)
1193 if (key.offset > search_start) {
1194 hole_size = key.offset - search_start;
1197 * Have to check before we set max_hole_start, otherwise
1198 * we could end up sending back this offset anyway.
1200 if (contains_pending_extent(trans, device,
1205 if (hole_size > max_hole_size) {
1206 max_hole_start = search_start;
1207 max_hole_size = hole_size;
1211 * If this free space is greater than which we need,
1212 * it must be the max free space that we have found
1213 * until now, so max_hole_start must point to the start
1214 * of this free space and the length of this free space
1215 * is stored in max_hole_size. Thus, we return
1216 * max_hole_start and max_hole_size and go back to the
1219 if (hole_size >= num_bytes) {
1225 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1226 extent_end = key.offset + btrfs_dev_extent_length(l,
1228 if (extent_end > search_start)
1229 search_start = extent_end;
1236 * At this point, search_start should be the end of
1237 * allocated dev extents, and when shrinking the device,
1238 * search_end may be smaller than search_start.
1240 if (search_end > search_start)
1241 hole_size = search_end - search_start;
1243 if (hole_size > max_hole_size) {
1244 max_hole_start = search_start;
1245 max_hole_size = hole_size;
1248 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1249 btrfs_release_path(path);
1254 if (hole_size < num_bytes)
1260 btrfs_free_path(path);
1261 *start = max_hole_start;
1263 *len = max_hole_size;
1267 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1268 struct btrfs_device *device,
1269 u64 start, u64 *dev_extent_len)
1272 struct btrfs_path *path;
1273 struct btrfs_root *root = device->dev_root;
1274 struct btrfs_key key;
1275 struct btrfs_key found_key;
1276 struct extent_buffer *leaf = NULL;
1277 struct btrfs_dev_extent *extent = NULL;
1279 path = btrfs_alloc_path();
1283 key.objectid = device->devid;
1285 key.type = BTRFS_DEV_EXTENT_KEY;
1287 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1289 ret = btrfs_previous_item(root, path, key.objectid,
1290 BTRFS_DEV_EXTENT_KEY);
1293 leaf = path->nodes[0];
1294 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1295 extent = btrfs_item_ptr(leaf, path->slots[0],
1296 struct btrfs_dev_extent);
1297 BUG_ON(found_key.offset > start || found_key.offset +
1298 btrfs_dev_extent_length(leaf, extent) < start);
1300 btrfs_release_path(path);
1302 } else if (ret == 0) {
1303 leaf = path->nodes[0];
1304 extent = btrfs_item_ptr(leaf, path->slots[0],
1305 struct btrfs_dev_extent);
1307 btrfs_error(root->fs_info, ret, "Slot search failed");
1311 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1313 ret = btrfs_del_item(trans, root, path);
1315 btrfs_error(root->fs_info, ret,
1316 "Failed to remove dev extent item");
1319 btrfs_free_path(path);
1323 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1324 struct btrfs_device *device,
1325 u64 chunk_tree, u64 chunk_objectid,
1326 u64 chunk_offset, u64 start, u64 num_bytes)
1329 struct btrfs_path *path;
1330 struct btrfs_root *root = device->dev_root;
1331 struct btrfs_dev_extent *extent;
1332 struct extent_buffer *leaf;
1333 struct btrfs_key key;
1335 WARN_ON(!device->in_fs_metadata);
1336 WARN_ON(device->is_tgtdev_for_dev_replace);
1337 path = btrfs_alloc_path();
1341 key.objectid = device->devid;
1343 key.type = BTRFS_DEV_EXTENT_KEY;
1344 ret = btrfs_insert_empty_item(trans, root, path, &key,
1349 leaf = path->nodes[0];
1350 extent = btrfs_item_ptr(leaf, path->slots[0],
1351 struct btrfs_dev_extent);
1352 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1353 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1354 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1356 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1357 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
1359 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1360 btrfs_mark_buffer_dirty(leaf);
1362 btrfs_free_path(path);
1366 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1368 struct extent_map_tree *em_tree;
1369 struct extent_map *em;
1373 em_tree = &fs_info->mapping_tree.map_tree;
1374 read_lock(&em_tree->lock);
1375 n = rb_last(&em_tree->map);
1377 em = rb_entry(n, struct extent_map, rb_node);
1378 ret = em->start + em->len;
1380 read_unlock(&em_tree->lock);
1385 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1389 struct btrfs_key key;
1390 struct btrfs_key found_key;
1391 struct btrfs_path *path;
1393 path = btrfs_alloc_path();
1397 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1398 key.type = BTRFS_DEV_ITEM_KEY;
1399 key.offset = (u64)-1;
1401 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1405 BUG_ON(ret == 0); /* Corruption */
1407 ret = btrfs_previous_item(fs_info->chunk_root, path,
1408 BTRFS_DEV_ITEMS_OBJECTID,
1409 BTRFS_DEV_ITEM_KEY);
1413 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1415 *devid_ret = found_key.offset + 1;
1419 btrfs_free_path(path);
1424 * the device information is stored in the chunk root
1425 * the btrfs_device struct should be fully filled in
1427 static int btrfs_add_device(struct btrfs_trans_handle *trans,
1428 struct btrfs_root *root,
1429 struct btrfs_device *device)
1432 struct btrfs_path *path;
1433 struct btrfs_dev_item *dev_item;
1434 struct extent_buffer *leaf;
1435 struct btrfs_key key;
1438 root = root->fs_info->chunk_root;
1440 path = btrfs_alloc_path();
1444 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1445 key.type = BTRFS_DEV_ITEM_KEY;
1446 key.offset = device->devid;
1448 ret = btrfs_insert_empty_item(trans, root, path, &key,
1453 leaf = path->nodes[0];
1454 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1456 btrfs_set_device_id(leaf, dev_item, device->devid);
1457 btrfs_set_device_generation(leaf, dev_item, 0);
1458 btrfs_set_device_type(leaf, dev_item, device->type);
1459 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1460 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1461 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1462 btrfs_set_device_total_bytes(leaf, dev_item,
1463 btrfs_device_get_disk_total_bytes(device));
1464 btrfs_set_device_bytes_used(leaf, dev_item,
1465 btrfs_device_get_bytes_used(device));
1466 btrfs_set_device_group(leaf, dev_item, 0);
1467 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1468 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1469 btrfs_set_device_start_offset(leaf, dev_item, 0);
1471 ptr = btrfs_device_uuid(dev_item);
1472 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1473 ptr = btrfs_device_fsid(dev_item);
1474 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1475 btrfs_mark_buffer_dirty(leaf);
1479 btrfs_free_path(path);
1484 * Function to update ctime/mtime for a given device path.
1485 * Mainly used for ctime/mtime based probe like libblkid.
1487 static void update_dev_time(char *path_name)
1491 filp = filp_open(path_name, O_RDWR, 0);
1494 file_update_time(filp);
1495 filp_close(filp, NULL);
1499 static int btrfs_rm_dev_item(struct btrfs_root *root,
1500 struct btrfs_device *device)
1503 struct btrfs_path *path;
1504 struct btrfs_key key;
1505 struct btrfs_trans_handle *trans;
1507 root = root->fs_info->chunk_root;
1509 path = btrfs_alloc_path();
1513 trans = btrfs_start_transaction(root, 0);
1514 if (IS_ERR(trans)) {
1515 btrfs_free_path(path);
1516 return PTR_ERR(trans);
1518 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1519 key.type = BTRFS_DEV_ITEM_KEY;
1520 key.offset = device->devid;
1522 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1531 ret = btrfs_del_item(trans, root, path);
1535 btrfs_free_path(path);
1536 btrfs_commit_transaction(trans, root);
1540 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1542 struct btrfs_device *device;
1543 struct btrfs_device *next_device;
1544 struct block_device *bdev;
1545 struct buffer_head *bh = NULL;
1546 struct btrfs_super_block *disk_super;
1547 struct btrfs_fs_devices *cur_devices;
1554 bool clear_super = false;
1556 mutex_lock(&uuid_mutex);
1559 seq = read_seqbegin(&root->fs_info->profiles_lock);
1561 all_avail = root->fs_info->avail_data_alloc_bits |
1562 root->fs_info->avail_system_alloc_bits |
1563 root->fs_info->avail_metadata_alloc_bits;
1564 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
1566 num_devices = root->fs_info->fs_devices->num_devices;
1567 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1568 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1569 WARN_ON(num_devices < 1);
1572 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1574 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
1575 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
1579 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
1580 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
1584 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1585 root->fs_info->fs_devices->rw_devices <= 2) {
1586 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
1589 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1590 root->fs_info->fs_devices->rw_devices <= 3) {
1591 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
1595 if (strcmp(device_path, "missing") == 0) {
1596 struct list_head *devices;
1597 struct btrfs_device *tmp;
1600 devices = &root->fs_info->fs_devices->devices;
1602 * It is safe to read the devices since the volume_mutex
1605 list_for_each_entry(tmp, devices, dev_list) {
1606 if (tmp->in_fs_metadata &&
1607 !tmp->is_tgtdev_for_dev_replace &&
1617 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1621 ret = btrfs_get_bdev_and_sb(device_path,
1622 FMODE_WRITE | FMODE_EXCL,
1623 root->fs_info->bdev_holder, 0,
1627 disk_super = (struct btrfs_super_block *)bh->b_data;
1628 devid = btrfs_stack_device_id(&disk_super->dev_item);
1629 dev_uuid = disk_super->dev_item.uuid;
1630 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1638 if (device->is_tgtdev_for_dev_replace) {
1639 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1643 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1644 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1648 if (device->writeable) {
1650 list_del_init(&device->dev_alloc_list);
1651 device->fs_devices->rw_devices--;
1652 unlock_chunks(root);
1656 mutex_unlock(&uuid_mutex);
1657 ret = btrfs_shrink_device(device, 0);
1658 mutex_lock(&uuid_mutex);
1663 * TODO: the superblock still includes this device in its num_devices
1664 * counter although write_all_supers() is not locked out. This
1665 * could give a filesystem state which requires a degraded mount.
1667 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1671 device->in_fs_metadata = 0;
1672 btrfs_scrub_cancel_dev(root->fs_info, device);
1675 * the device list mutex makes sure that we don't change
1676 * the device list while someone else is writing out all
1677 * the device supers. Whoever is writing all supers, should
1678 * lock the device list mutex before getting the number of
1679 * devices in the super block (super_copy). Conversely,
1680 * whoever updates the number of devices in the super block
1681 * (super_copy) should hold the device list mutex.
1684 cur_devices = device->fs_devices;
1685 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1686 list_del_rcu(&device->dev_list);
1688 device->fs_devices->num_devices--;
1689 device->fs_devices->total_devices--;
1691 if (device->missing)
1692 device->fs_devices->missing_devices--;
1694 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1695 struct btrfs_device, dev_list);
1696 if (device->bdev == root->fs_info->sb->s_bdev)
1697 root->fs_info->sb->s_bdev = next_device->bdev;
1698 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1699 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1702 device->fs_devices->open_devices--;
1703 /* remove sysfs entry */
1704 btrfs_kobj_rm_device(root->fs_info, device);
1707 call_rcu(&device->rcu, free_device);
1709 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1710 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
1711 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1713 if (cur_devices->open_devices == 0) {
1714 struct btrfs_fs_devices *fs_devices;
1715 fs_devices = root->fs_info->fs_devices;
1716 while (fs_devices) {
1717 if (fs_devices->seed == cur_devices) {
1718 fs_devices->seed = cur_devices->seed;
1721 fs_devices = fs_devices->seed;
1723 cur_devices->seed = NULL;
1724 __btrfs_close_devices(cur_devices);
1725 free_fs_devices(cur_devices);
1728 root->fs_info->num_tolerated_disk_barrier_failures =
1729 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1732 * at this point, the device is zero sized. We want to
1733 * remove it from the devices list and zero out the old super
1735 if (clear_super && disk_super) {
1739 /* make sure this device isn't detected as part of
1742 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1743 set_buffer_dirty(bh);
1744 sync_dirty_buffer(bh);
1746 /* clear the mirror copies of super block on the disk
1747 * being removed, 0th copy is been taken care above and
1748 * the below would take of the rest
1750 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1751 bytenr = btrfs_sb_offset(i);
1752 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1753 i_size_read(bdev->bd_inode))
1757 bh = __bread(bdev, bytenr / 4096,
1758 BTRFS_SUPER_INFO_SIZE);
1762 disk_super = (struct btrfs_super_block *)bh->b_data;
1764 if (btrfs_super_bytenr(disk_super) != bytenr ||
1765 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1768 memset(&disk_super->magic, 0,
1769 sizeof(disk_super->magic));
1770 set_buffer_dirty(bh);
1771 sync_dirty_buffer(bh);
1778 /* Notify udev that device has changed */
1779 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
1781 /* Update ctime/mtime for device path for libblkid */
1782 update_dev_time(device_path);
1788 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1790 mutex_unlock(&uuid_mutex);
1793 if (device->writeable) {
1795 list_add(&device->dev_alloc_list,
1796 &root->fs_info->fs_devices->alloc_list);
1797 device->fs_devices->rw_devices++;
1798 unlock_chunks(root);
1803 void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1804 struct btrfs_device *srcdev)
1806 struct btrfs_fs_devices *fs_devices;
1808 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1811 * in case of fs with no seed, srcdev->fs_devices will point
1812 * to fs_devices of fs_info. However when the dev being replaced is
1813 * a seed dev it will point to the seed's local fs_devices. In short
1814 * srcdev will have its correct fs_devices in both the cases.
1816 fs_devices = srcdev->fs_devices;
1818 list_del_rcu(&srcdev->dev_list);
1819 list_del_rcu(&srcdev->dev_alloc_list);
1820 fs_devices->num_devices--;
1821 if (srcdev->missing)
1822 fs_devices->missing_devices--;
1824 if (srcdev->writeable) {
1825 fs_devices->rw_devices--;
1826 /* zero out the old super if it is writable */
1827 btrfs_scratch_superblock(srcdev);
1831 fs_devices->open_devices--;
1833 call_rcu(&srcdev->rcu, free_device);
1836 * unless fs_devices is seed fs, num_devices shouldn't go
1839 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1841 /* if this is no devs we rather delete the fs_devices */
1842 if (!fs_devices->num_devices) {
1843 struct btrfs_fs_devices *tmp_fs_devices;
1845 tmp_fs_devices = fs_info->fs_devices;
1846 while (tmp_fs_devices) {
1847 if (tmp_fs_devices->seed == fs_devices) {
1848 tmp_fs_devices->seed = fs_devices->seed;
1851 tmp_fs_devices = tmp_fs_devices->seed;
1853 fs_devices->seed = NULL;
1854 __btrfs_close_devices(fs_devices);
1855 free_fs_devices(fs_devices);
1859 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1860 struct btrfs_device *tgtdev)
1862 struct btrfs_device *next_device;
1864 mutex_lock(&uuid_mutex);
1866 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1868 btrfs_scratch_superblock(tgtdev);
1869 fs_info->fs_devices->open_devices--;
1871 fs_info->fs_devices->num_devices--;
1873 next_device = list_entry(fs_info->fs_devices->devices.next,
1874 struct btrfs_device, dev_list);
1875 if (tgtdev->bdev == fs_info->sb->s_bdev)
1876 fs_info->sb->s_bdev = next_device->bdev;
1877 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1878 fs_info->fs_devices->latest_bdev = next_device->bdev;
1879 list_del_rcu(&tgtdev->dev_list);
1881 call_rcu(&tgtdev->rcu, free_device);
1883 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1884 mutex_unlock(&uuid_mutex);
1887 static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1888 struct btrfs_device **device)
1891 struct btrfs_super_block *disk_super;
1894 struct block_device *bdev;
1895 struct buffer_head *bh;
1898 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1899 root->fs_info->bdev_holder, 0, &bdev, &bh);
1902 disk_super = (struct btrfs_super_block *)bh->b_data;
1903 devid = btrfs_stack_device_id(&disk_super->dev_item);
1904 dev_uuid = disk_super->dev_item.uuid;
1905 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1910 blkdev_put(bdev, FMODE_READ);
1914 int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1916 struct btrfs_device **device)
1919 if (strcmp(device_path, "missing") == 0) {
1920 struct list_head *devices;
1921 struct btrfs_device *tmp;
1923 devices = &root->fs_info->fs_devices->devices;
1925 * It is safe to read the devices since the volume_mutex
1926 * is held by the caller.
1928 list_for_each_entry(tmp, devices, dev_list) {
1929 if (tmp->in_fs_metadata && !tmp->bdev) {
1936 btrfs_err(root->fs_info, "no missing device found");
1942 return btrfs_find_device_by_path(root, device_path, device);
1947 * does all the dirty work required for changing file system's UUID.
1949 static int btrfs_prepare_sprout(struct btrfs_root *root)
1951 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1952 struct btrfs_fs_devices *old_devices;
1953 struct btrfs_fs_devices *seed_devices;
1954 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
1955 struct btrfs_device *device;
1958 BUG_ON(!mutex_is_locked(&uuid_mutex));
1959 if (!fs_devices->seeding)
1962 seed_devices = __alloc_fs_devices();
1963 if (IS_ERR(seed_devices))
1964 return PTR_ERR(seed_devices);
1966 old_devices = clone_fs_devices(fs_devices);
1967 if (IS_ERR(old_devices)) {
1968 kfree(seed_devices);
1969 return PTR_ERR(old_devices);
1972 list_add(&old_devices->list, &fs_uuids);
1974 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1975 seed_devices->opened = 1;
1976 INIT_LIST_HEAD(&seed_devices->devices);
1977 INIT_LIST_HEAD(&seed_devices->alloc_list);
1978 mutex_init(&seed_devices->device_list_mutex);
1980 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
1981 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1983 list_for_each_entry(device, &seed_devices->devices, dev_list)
1984 device->fs_devices = seed_devices;
1987 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1988 unlock_chunks(root);
1990 fs_devices->seeding = 0;
1991 fs_devices->num_devices = 0;
1992 fs_devices->open_devices = 0;
1993 fs_devices->missing_devices = 0;
1994 fs_devices->rotating = 0;
1995 fs_devices->seed = seed_devices;
1997 generate_random_uuid(fs_devices->fsid);
1998 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1999 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2000 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2002 super_flags = btrfs_super_flags(disk_super) &
2003 ~BTRFS_SUPER_FLAG_SEEDING;
2004 btrfs_set_super_flags(disk_super, super_flags);
2010 * strore the expected generation for seed devices in device items.
2012 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2013 struct btrfs_root *root)
2015 struct btrfs_path *path;
2016 struct extent_buffer *leaf;
2017 struct btrfs_dev_item *dev_item;
2018 struct btrfs_device *device;
2019 struct btrfs_key key;
2020 u8 fs_uuid[BTRFS_UUID_SIZE];
2021 u8 dev_uuid[BTRFS_UUID_SIZE];
2025 path = btrfs_alloc_path();
2029 root = root->fs_info->chunk_root;
2030 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2032 key.type = BTRFS_DEV_ITEM_KEY;
2035 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2039 leaf = path->nodes[0];
2041 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2042 ret = btrfs_next_leaf(root, path);
2047 leaf = path->nodes[0];
2048 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2049 btrfs_release_path(path);
2053 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2054 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2055 key.type != BTRFS_DEV_ITEM_KEY)
2058 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2059 struct btrfs_dev_item);
2060 devid = btrfs_device_id(leaf, dev_item);
2061 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2063 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2065 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2067 BUG_ON(!device); /* Logic error */
2069 if (device->fs_devices->seeding) {
2070 btrfs_set_device_generation(leaf, dev_item,
2071 device->generation);
2072 btrfs_mark_buffer_dirty(leaf);
2080 btrfs_free_path(path);
2084 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2086 struct request_queue *q;
2087 struct btrfs_trans_handle *trans;
2088 struct btrfs_device *device;
2089 struct block_device *bdev;
2090 struct list_head *devices;
2091 struct super_block *sb = root->fs_info->sb;
2092 struct rcu_string *name;
2094 int seeding_dev = 0;
2097 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
2100 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2101 root->fs_info->bdev_holder);
2103 return PTR_ERR(bdev);
2105 if (root->fs_info->fs_devices->seeding) {
2107 down_write(&sb->s_umount);
2108 mutex_lock(&uuid_mutex);
2111 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2113 devices = &root->fs_info->fs_devices->devices;
2115 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2116 list_for_each_entry(device, devices, dev_list) {
2117 if (device->bdev == bdev) {
2120 &root->fs_info->fs_devices->device_list_mutex);
2124 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2126 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2127 if (IS_ERR(device)) {
2128 /* we can safely leave the fs_devices entry around */
2129 ret = PTR_ERR(device);
2133 name = rcu_string_strdup(device_path, GFP_NOFS);
2139 rcu_assign_pointer(device->name, name);
2141 trans = btrfs_start_transaction(root, 0);
2142 if (IS_ERR(trans)) {
2143 rcu_string_free(device->name);
2145 ret = PTR_ERR(trans);
2149 q = bdev_get_queue(bdev);
2150 if (blk_queue_discard(q))
2151 device->can_discard = 1;
2152 device->writeable = 1;
2153 device->generation = trans->transid;
2154 device->io_width = root->sectorsize;
2155 device->io_align = root->sectorsize;
2156 device->sector_size = root->sectorsize;
2157 device->total_bytes = i_size_read(bdev->bd_inode);
2158 device->disk_total_bytes = device->total_bytes;
2159 device->commit_total_bytes = device->total_bytes;
2160 device->dev_root = root->fs_info->dev_root;
2161 device->bdev = bdev;
2162 device->in_fs_metadata = 1;
2163 device->is_tgtdev_for_dev_replace = 0;
2164 device->mode = FMODE_EXCL;
2165 device->dev_stats_valid = 1;
2166 set_blocksize(device->bdev, 4096);
2169 sb->s_flags &= ~MS_RDONLY;
2170 ret = btrfs_prepare_sprout(root);
2171 BUG_ON(ret); /* -ENOMEM */
2174 device->fs_devices = root->fs_info->fs_devices;
2176 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2178 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
2179 list_add(&device->dev_alloc_list,
2180 &root->fs_info->fs_devices->alloc_list);
2181 root->fs_info->fs_devices->num_devices++;
2182 root->fs_info->fs_devices->open_devices++;
2183 root->fs_info->fs_devices->rw_devices++;
2184 root->fs_info->fs_devices->total_devices++;
2185 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2187 spin_lock(&root->fs_info->free_chunk_lock);
2188 root->fs_info->free_chunk_space += device->total_bytes;
2189 spin_unlock(&root->fs_info->free_chunk_lock);
2191 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2192 root->fs_info->fs_devices->rotating = 1;
2194 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
2195 btrfs_set_super_total_bytes(root->fs_info->super_copy,
2196 tmp + device->total_bytes);
2198 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
2199 btrfs_set_super_num_devices(root->fs_info->super_copy,
2202 /* add sysfs device entry */
2203 btrfs_kobj_add_device(root->fs_info, device);
2206 * we've got more storage, clear any full flags on the space
2209 btrfs_clear_space_info_full(root->fs_info);
2211 unlock_chunks(root);
2212 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2216 ret = init_first_rw_device(trans, root, device);
2217 unlock_chunks(root);
2219 btrfs_abort_transaction(trans, root, ret);
2224 ret = btrfs_add_device(trans, root, device);
2226 btrfs_abort_transaction(trans, root, ret);
2231 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2233 ret = btrfs_finish_sprout(trans, root);
2235 btrfs_abort_transaction(trans, root, ret);
2239 /* Sprouting would change fsid of the mounted root,
2240 * so rename the fsid on the sysfs
2242 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2243 root->fs_info->fsid);
2244 if (kobject_rename(&root->fs_info->super_kobj, fsid_buf))
2248 root->fs_info->num_tolerated_disk_barrier_failures =
2249 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
2250 ret = btrfs_commit_transaction(trans, root);
2253 mutex_unlock(&uuid_mutex);
2254 up_write(&sb->s_umount);
2256 if (ret) /* transaction commit */
2259 ret = btrfs_relocate_sys_chunks(root);
2261 btrfs_error(root->fs_info, ret,
2262 "Failed to relocate sys chunks after "
2263 "device initialization. This can be fixed "
2264 "using the \"btrfs balance\" command.");
2265 trans = btrfs_attach_transaction(root);
2266 if (IS_ERR(trans)) {
2267 if (PTR_ERR(trans) == -ENOENT)
2269 return PTR_ERR(trans);
2271 ret = btrfs_commit_transaction(trans, root);
2274 /* Update ctime/mtime for libblkid */
2275 update_dev_time(device_path);
2279 btrfs_end_transaction(trans, root);
2280 rcu_string_free(device->name);
2281 btrfs_kobj_rm_device(root->fs_info, device);
2284 blkdev_put(bdev, FMODE_EXCL);
2286 mutex_unlock(&uuid_mutex);
2287 up_write(&sb->s_umount);
2292 int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2293 struct btrfs_device *srcdev,
2294 struct btrfs_device **device_out)
2296 struct request_queue *q;
2297 struct btrfs_device *device;
2298 struct block_device *bdev;
2299 struct btrfs_fs_info *fs_info = root->fs_info;
2300 struct list_head *devices;
2301 struct rcu_string *name;
2302 u64 devid = BTRFS_DEV_REPLACE_DEVID;
2306 if (fs_info->fs_devices->seeding) {
2307 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
2311 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2312 fs_info->bdev_holder);
2314 btrfs_err(fs_info, "target device %s is invalid!", device_path);
2315 return PTR_ERR(bdev);
2318 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2320 devices = &fs_info->fs_devices->devices;
2321 list_for_each_entry(device, devices, dev_list) {
2322 if (device->bdev == bdev) {
2323 btrfs_err(fs_info, "target device is in the filesystem!");
2330 if (i_size_read(bdev->bd_inode) <
2331 btrfs_device_get_total_bytes(srcdev)) {
2332 btrfs_err(fs_info, "target device is smaller than source device!");
2338 device = btrfs_alloc_device(NULL, &devid, NULL);
2339 if (IS_ERR(device)) {
2340 ret = PTR_ERR(device);
2344 name = rcu_string_strdup(device_path, GFP_NOFS);
2350 rcu_assign_pointer(device->name, name);
2352 q = bdev_get_queue(bdev);
2353 if (blk_queue_discard(q))
2354 device->can_discard = 1;
2355 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2356 device->writeable = 1;
2357 device->generation = 0;
2358 device->io_width = root->sectorsize;
2359 device->io_align = root->sectorsize;
2360 device->sector_size = root->sectorsize;
2361 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2362 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2363 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
2364 ASSERT(list_empty(&srcdev->resized_list));
2365 device->commit_total_bytes = srcdev->commit_total_bytes;
2366 device->commit_bytes_used = device->bytes_used;
2367 device->dev_root = fs_info->dev_root;
2368 device->bdev = bdev;
2369 device->in_fs_metadata = 1;
2370 device->is_tgtdev_for_dev_replace = 1;
2371 device->mode = FMODE_EXCL;
2372 device->dev_stats_valid = 1;
2373 set_blocksize(device->bdev, 4096);
2374 device->fs_devices = fs_info->fs_devices;
2375 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2376 fs_info->fs_devices->num_devices++;
2377 fs_info->fs_devices->open_devices++;
2378 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2380 *device_out = device;
2384 blkdev_put(bdev, FMODE_EXCL);
2388 void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2389 struct btrfs_device *tgtdev)
2391 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2392 tgtdev->io_width = fs_info->dev_root->sectorsize;
2393 tgtdev->io_align = fs_info->dev_root->sectorsize;
2394 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2395 tgtdev->dev_root = fs_info->dev_root;
2396 tgtdev->in_fs_metadata = 1;
2399 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2400 struct btrfs_device *device)
2403 struct btrfs_path *path;
2404 struct btrfs_root *root;
2405 struct btrfs_dev_item *dev_item;
2406 struct extent_buffer *leaf;
2407 struct btrfs_key key;
2409 root = device->dev_root->fs_info->chunk_root;
2411 path = btrfs_alloc_path();
2415 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2416 key.type = BTRFS_DEV_ITEM_KEY;
2417 key.offset = device->devid;
2419 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2428 leaf = path->nodes[0];
2429 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2431 btrfs_set_device_id(leaf, dev_item, device->devid);
2432 btrfs_set_device_type(leaf, dev_item, device->type);
2433 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2434 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2435 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2436 btrfs_set_device_total_bytes(leaf, dev_item,
2437 btrfs_device_get_disk_total_bytes(device));
2438 btrfs_set_device_bytes_used(leaf, dev_item,
2439 btrfs_device_get_bytes_used(device));
2440 btrfs_mark_buffer_dirty(leaf);
2443 btrfs_free_path(path);
2447 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2448 struct btrfs_device *device, u64 new_size)
2450 struct btrfs_super_block *super_copy =
2451 device->dev_root->fs_info->super_copy;
2452 struct btrfs_fs_devices *fs_devices;
2456 if (!device->writeable)
2459 lock_chunks(device->dev_root);
2460 old_total = btrfs_super_total_bytes(super_copy);
2461 diff = new_size - device->total_bytes;
2463 if (new_size <= device->total_bytes ||
2464 device->is_tgtdev_for_dev_replace) {
2465 unlock_chunks(device->dev_root);
2469 fs_devices = device->dev_root->fs_info->fs_devices;
2471 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2472 device->fs_devices->total_rw_bytes += diff;
2474 btrfs_device_set_total_bytes(device, new_size);
2475 btrfs_device_set_disk_total_bytes(device, new_size);
2476 btrfs_clear_space_info_full(device->dev_root->fs_info);
2477 if (list_empty(&device->resized_list))
2478 list_add_tail(&device->resized_list,
2479 &fs_devices->resized_devices);
2480 unlock_chunks(device->dev_root);
2482 return btrfs_update_device(trans, device);
2485 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2486 struct btrfs_root *root,
2487 u64 chunk_tree, u64 chunk_objectid,
2491 struct btrfs_path *path;
2492 struct btrfs_key key;
2494 root = root->fs_info->chunk_root;
2495 path = btrfs_alloc_path();
2499 key.objectid = chunk_objectid;
2500 key.offset = chunk_offset;
2501 key.type = BTRFS_CHUNK_ITEM_KEY;
2503 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2506 else if (ret > 0) { /* Logic error or corruption */
2507 btrfs_error(root->fs_info, -ENOENT,
2508 "Failed lookup while freeing chunk.");
2513 ret = btrfs_del_item(trans, root, path);
2515 btrfs_error(root->fs_info, ret,
2516 "Failed to delete chunk item.");
2518 btrfs_free_path(path);
2522 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
2525 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2526 struct btrfs_disk_key *disk_key;
2527 struct btrfs_chunk *chunk;
2534 struct btrfs_key key;
2537 array_size = btrfs_super_sys_array_size(super_copy);
2539 ptr = super_copy->sys_chunk_array;
2542 while (cur < array_size) {
2543 disk_key = (struct btrfs_disk_key *)ptr;
2544 btrfs_disk_key_to_cpu(&key, disk_key);
2546 len = sizeof(*disk_key);
2548 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2549 chunk = (struct btrfs_chunk *)(ptr + len);
2550 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2551 len += btrfs_chunk_item_size(num_stripes);
2556 if (key.objectid == chunk_objectid &&
2557 key.offset == chunk_offset) {
2558 memmove(ptr, ptr + len, array_size - (cur + len));
2560 btrfs_set_super_sys_array_size(super_copy, array_size);
2566 unlock_chunks(root);
2570 int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2571 struct btrfs_root *root, u64 chunk_offset)
2573 struct extent_map_tree *em_tree;
2574 struct extent_map *em;
2575 struct btrfs_root *extent_root = root->fs_info->extent_root;
2576 struct map_lookup *map;
2577 u64 dev_extent_len = 0;
2578 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2579 u64 chunk_tree = root->fs_info->chunk_root->objectid;
2583 root = root->fs_info->chunk_root;
2584 em_tree = &root->fs_info->mapping_tree.map_tree;
2586 read_lock(&em_tree->lock);
2587 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2588 read_unlock(&em_tree->lock);
2590 if (!em || em->start > chunk_offset ||
2591 em->start + em->len < chunk_offset) {
2593 * This is a logic error, but we don't want to just rely on the
2594 * user having built with ASSERT enabled, so if ASSERT doens't
2595 * do anything we still error out.
2599 free_extent_map(em);
2602 map = (struct map_lookup *)em->bdev;
2604 for (i = 0; i < map->num_stripes; i++) {
2605 struct btrfs_device *device = map->stripes[i].dev;
2606 ret = btrfs_free_dev_extent(trans, device,
2607 map->stripes[i].physical,
2610 btrfs_abort_transaction(trans, root, ret);
2614 if (device->bytes_used > 0) {
2616 btrfs_device_set_bytes_used(device,
2617 device->bytes_used - dev_extent_len);
2618 spin_lock(&root->fs_info->free_chunk_lock);
2619 root->fs_info->free_chunk_space += dev_extent_len;
2620 spin_unlock(&root->fs_info->free_chunk_lock);
2621 btrfs_clear_space_info_full(root->fs_info);
2622 unlock_chunks(root);
2625 if (map->stripes[i].dev) {
2626 ret = btrfs_update_device(trans, map->stripes[i].dev);
2628 btrfs_abort_transaction(trans, root, ret);
2633 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2636 btrfs_abort_transaction(trans, root, ret);
2640 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2642 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2643 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2645 btrfs_abort_transaction(trans, root, ret);
2650 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2652 btrfs_abort_transaction(trans, extent_root, ret);
2656 write_lock(&em_tree->lock);
2657 remove_extent_mapping(em_tree, em);
2658 write_unlock(&em_tree->lock);
2660 /* once for the tree */
2661 free_extent_map(em);
2664 free_extent_map(em);
2668 static int btrfs_relocate_chunk(struct btrfs_root *root,
2669 u64 chunk_tree, u64 chunk_objectid,
2672 struct btrfs_root *extent_root;
2673 struct btrfs_trans_handle *trans;
2676 root = root->fs_info->chunk_root;
2677 extent_root = root->fs_info->extent_root;
2679 ret = btrfs_can_relocate(extent_root, chunk_offset);
2683 /* step one, relocate all the extents inside this chunk */
2684 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
2688 trans = btrfs_start_transaction(root, 0);
2689 if (IS_ERR(trans)) {
2690 ret = PTR_ERR(trans);
2691 btrfs_std_error(root->fs_info, ret);
2696 * step two, delete the device extents and the
2697 * chunk tree entries
2699 ret = btrfs_remove_chunk(trans, root, chunk_offset);
2700 btrfs_end_transaction(trans, root);
2704 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2706 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2707 struct btrfs_path *path;
2708 struct extent_buffer *leaf;
2709 struct btrfs_chunk *chunk;
2710 struct btrfs_key key;
2711 struct btrfs_key found_key;
2712 u64 chunk_tree = chunk_root->root_key.objectid;
2714 bool retried = false;
2718 path = btrfs_alloc_path();
2723 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2724 key.offset = (u64)-1;
2725 key.type = BTRFS_CHUNK_ITEM_KEY;
2728 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2731 BUG_ON(ret == 0); /* Corruption */
2733 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2740 leaf = path->nodes[0];
2741 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2743 chunk = btrfs_item_ptr(leaf, path->slots[0],
2744 struct btrfs_chunk);
2745 chunk_type = btrfs_chunk_type(leaf, chunk);
2746 btrfs_release_path(path);
2748 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2749 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2758 if (found_key.offset == 0)
2760 key.offset = found_key.offset - 1;
2763 if (failed && !retried) {
2767 } else if (WARN_ON(failed && retried)) {
2771 btrfs_free_path(path);
2775 static int insert_balance_item(struct btrfs_root *root,
2776 struct btrfs_balance_control *bctl)
2778 struct btrfs_trans_handle *trans;
2779 struct btrfs_balance_item *item;
2780 struct btrfs_disk_balance_args disk_bargs;
2781 struct btrfs_path *path;
2782 struct extent_buffer *leaf;
2783 struct btrfs_key key;
2786 path = btrfs_alloc_path();
2790 trans = btrfs_start_transaction(root, 0);
2791 if (IS_ERR(trans)) {
2792 btrfs_free_path(path);
2793 return PTR_ERR(trans);
2796 key.objectid = BTRFS_BALANCE_OBJECTID;
2797 key.type = BTRFS_BALANCE_ITEM_KEY;
2800 ret = btrfs_insert_empty_item(trans, root, path, &key,
2805 leaf = path->nodes[0];
2806 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2808 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2810 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2811 btrfs_set_balance_data(leaf, item, &disk_bargs);
2812 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2813 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2814 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2815 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2817 btrfs_set_balance_flags(leaf, item, bctl->flags);
2819 btrfs_mark_buffer_dirty(leaf);
2821 btrfs_free_path(path);
2822 err = btrfs_commit_transaction(trans, root);
2828 static int del_balance_item(struct btrfs_root *root)
2830 struct btrfs_trans_handle *trans;
2831 struct btrfs_path *path;
2832 struct btrfs_key key;
2835 path = btrfs_alloc_path();
2839 trans = btrfs_start_transaction(root, 0);
2840 if (IS_ERR(trans)) {
2841 btrfs_free_path(path);
2842 return PTR_ERR(trans);
2845 key.objectid = BTRFS_BALANCE_OBJECTID;
2846 key.type = BTRFS_BALANCE_ITEM_KEY;
2849 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2857 ret = btrfs_del_item(trans, root, path);
2859 btrfs_free_path(path);
2860 err = btrfs_commit_transaction(trans, root);
2867 * This is a heuristic used to reduce the number of chunks balanced on
2868 * resume after balance was interrupted.
2870 static void update_balance_args(struct btrfs_balance_control *bctl)
2873 * Turn on soft mode for chunk types that were being converted.
2875 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2876 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2877 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2878 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2879 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2880 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2883 * Turn on usage filter if is not already used. The idea is
2884 * that chunks that we have already balanced should be
2885 * reasonably full. Don't do it for chunks that are being
2886 * converted - that will keep us from relocating unconverted
2887 * (albeit full) chunks.
2889 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2890 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2891 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2892 bctl->data.usage = 90;
2894 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2895 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2896 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2897 bctl->sys.usage = 90;
2899 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2900 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2901 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2902 bctl->meta.usage = 90;
2907 * Should be called with both balance and volume mutexes held to
2908 * serialize other volume operations (add_dev/rm_dev/resize) with
2909 * restriper. Same goes for unset_balance_control.
2911 static void set_balance_control(struct btrfs_balance_control *bctl)
2913 struct btrfs_fs_info *fs_info = bctl->fs_info;
2915 BUG_ON(fs_info->balance_ctl);
2917 spin_lock(&fs_info->balance_lock);
2918 fs_info->balance_ctl = bctl;
2919 spin_unlock(&fs_info->balance_lock);
2922 static void unset_balance_control(struct btrfs_fs_info *fs_info)
2924 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2926 BUG_ON(!fs_info->balance_ctl);
2928 spin_lock(&fs_info->balance_lock);
2929 fs_info->balance_ctl = NULL;
2930 spin_unlock(&fs_info->balance_lock);
2936 * Balance filters. Return 1 if chunk should be filtered out
2937 * (should not be balanced).
2939 static int chunk_profiles_filter(u64 chunk_type,
2940 struct btrfs_balance_args *bargs)
2942 chunk_type = chunk_to_extended(chunk_type) &
2943 BTRFS_EXTENDED_PROFILE_MASK;
2945 if (bargs->profiles & chunk_type)
2951 static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2952 struct btrfs_balance_args *bargs)
2954 struct btrfs_block_group_cache *cache;
2955 u64 chunk_used, user_thresh;
2958 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2959 chunk_used = btrfs_block_group_used(&cache->item);
2961 if (bargs->usage == 0)
2963 else if (bargs->usage > 100)
2964 user_thresh = cache->key.offset;
2966 user_thresh = div_factor_fine(cache->key.offset,
2969 if (chunk_used < user_thresh)
2972 btrfs_put_block_group(cache);
2976 static int chunk_devid_filter(struct extent_buffer *leaf,
2977 struct btrfs_chunk *chunk,
2978 struct btrfs_balance_args *bargs)
2980 struct btrfs_stripe *stripe;
2981 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2984 for (i = 0; i < num_stripes; i++) {
2985 stripe = btrfs_stripe_nr(chunk, i);
2986 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2993 /* [pstart, pend) */
2994 static int chunk_drange_filter(struct extent_buffer *leaf,
2995 struct btrfs_chunk *chunk,
2997 struct btrfs_balance_args *bargs)
2999 struct btrfs_stripe *stripe;
3000 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3006 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3009 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
3010 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3011 factor = num_stripes / 2;
3012 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3013 factor = num_stripes - 1;
3014 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3015 factor = num_stripes - 2;
3017 factor = num_stripes;
3020 for (i = 0; i < num_stripes; i++) {
3021 stripe = btrfs_stripe_nr(chunk, i);
3022 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3025 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3026 stripe_length = btrfs_chunk_length(leaf, chunk);
3027 do_div(stripe_length, factor);
3029 if (stripe_offset < bargs->pend &&
3030 stripe_offset + stripe_length > bargs->pstart)
3037 /* [vstart, vend) */
3038 static int chunk_vrange_filter(struct extent_buffer *leaf,
3039 struct btrfs_chunk *chunk,
3041 struct btrfs_balance_args *bargs)
3043 if (chunk_offset < bargs->vend &&
3044 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3045 /* at least part of the chunk is inside this vrange */
3051 static int chunk_soft_convert_filter(u64 chunk_type,
3052 struct btrfs_balance_args *bargs)
3054 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3057 chunk_type = chunk_to_extended(chunk_type) &
3058 BTRFS_EXTENDED_PROFILE_MASK;
3060 if (bargs->target == chunk_type)
3066 static int should_balance_chunk(struct btrfs_root *root,
3067 struct extent_buffer *leaf,
3068 struct btrfs_chunk *chunk, u64 chunk_offset)
3070 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3071 struct btrfs_balance_args *bargs = NULL;
3072 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3075 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3076 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3080 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3081 bargs = &bctl->data;
3082 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3084 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3085 bargs = &bctl->meta;
3087 /* profiles filter */
3088 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3089 chunk_profiles_filter(chunk_type, bargs)) {
3094 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3095 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3100 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3101 chunk_devid_filter(leaf, chunk, bargs)) {
3105 /* drange filter, makes sense only with devid filter */
3106 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3107 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3112 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3113 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3117 /* soft profile changing mode */
3118 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3119 chunk_soft_convert_filter(chunk_type, bargs)) {
3124 * limited by count, must be the last filter
3126 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3127 if (bargs->limit == 0)
3136 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
3138 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3139 struct btrfs_root *chunk_root = fs_info->chunk_root;
3140 struct btrfs_root *dev_root = fs_info->dev_root;
3141 struct list_head *devices;
3142 struct btrfs_device *device;
3145 struct btrfs_chunk *chunk;
3146 struct btrfs_path *path;
3147 struct btrfs_key key;
3148 struct btrfs_key found_key;
3149 struct btrfs_trans_handle *trans;
3150 struct extent_buffer *leaf;
3153 int enospc_errors = 0;
3154 bool counting = true;
3155 u64 limit_data = bctl->data.limit;
3156 u64 limit_meta = bctl->meta.limit;
3157 u64 limit_sys = bctl->sys.limit;
3159 /* step one make some room on all the devices */
3160 devices = &fs_info->fs_devices->devices;
3161 list_for_each_entry(device, devices, dev_list) {
3162 old_size = btrfs_device_get_total_bytes(device);
3163 size_to_free = div_factor(old_size, 1);
3164 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
3165 if (!device->writeable ||
3166 btrfs_device_get_total_bytes(device) -
3167 btrfs_device_get_bytes_used(device) > size_to_free ||
3168 device->is_tgtdev_for_dev_replace)
3171 ret = btrfs_shrink_device(device, old_size - size_to_free);
3176 trans = btrfs_start_transaction(dev_root, 0);
3177 BUG_ON(IS_ERR(trans));
3179 ret = btrfs_grow_device(trans, device, old_size);
3182 btrfs_end_transaction(trans, dev_root);
3185 /* step two, relocate all the chunks */
3186 path = btrfs_alloc_path();
3192 /* zero out stat counters */
3193 spin_lock(&fs_info->balance_lock);
3194 memset(&bctl->stat, 0, sizeof(bctl->stat));
3195 spin_unlock(&fs_info->balance_lock);
3198 bctl->data.limit = limit_data;
3199 bctl->meta.limit = limit_meta;
3200 bctl->sys.limit = limit_sys;
3202 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3203 key.offset = (u64)-1;
3204 key.type = BTRFS_CHUNK_ITEM_KEY;
3207 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
3208 atomic_read(&fs_info->balance_cancel_req)) {
3213 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3218 * this shouldn't happen, it means the last relocate
3222 BUG(); /* FIXME break ? */
3224 ret = btrfs_previous_item(chunk_root, path, 0,
3225 BTRFS_CHUNK_ITEM_KEY);
3231 leaf = path->nodes[0];
3232 slot = path->slots[0];
3233 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3235 if (found_key.objectid != key.objectid)
3238 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3241 spin_lock(&fs_info->balance_lock);
3242 bctl->stat.considered++;
3243 spin_unlock(&fs_info->balance_lock);
3246 ret = should_balance_chunk(chunk_root, leaf, chunk,
3248 btrfs_release_path(path);
3253 spin_lock(&fs_info->balance_lock);
3254 bctl->stat.expected++;
3255 spin_unlock(&fs_info->balance_lock);
3259 ret = btrfs_relocate_chunk(chunk_root,
3260 chunk_root->root_key.objectid,
3263 if (ret && ret != -ENOSPC)
3265 if (ret == -ENOSPC) {
3268 spin_lock(&fs_info->balance_lock);
3269 bctl->stat.completed++;
3270 spin_unlock(&fs_info->balance_lock);
3273 if (found_key.offset == 0)
3275 key.offset = found_key.offset - 1;
3279 btrfs_release_path(path);
3284 btrfs_free_path(path);
3285 if (enospc_errors) {
3286 btrfs_info(fs_info, "%d enospc errors during balance",
3296 * alloc_profile_is_valid - see if a given profile is valid and reduced
3297 * @flags: profile to validate
3298 * @extended: if true @flags is treated as an extended profile
3300 static int alloc_profile_is_valid(u64 flags, int extended)
3302 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3303 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3305 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3307 /* 1) check that all other bits are zeroed */
3311 /* 2) see if profile is reduced */
3313 return !extended; /* "0" is valid for usual profiles */
3315 /* true if exactly one bit set */
3316 return (flags & (flags - 1)) == 0;
3319 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3321 /* cancel requested || normal exit path */
3322 return atomic_read(&fs_info->balance_cancel_req) ||
3323 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3324 atomic_read(&fs_info->balance_cancel_req) == 0);
3327 static void __cancel_balance(struct btrfs_fs_info *fs_info)
3331 unset_balance_control(fs_info);
3332 ret = del_balance_item(fs_info->tree_root);
3334 btrfs_std_error(fs_info, ret);
3336 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3340 * Should be called with both balance and volume mutexes held
3342 int btrfs_balance(struct btrfs_balance_control *bctl,
3343 struct btrfs_ioctl_balance_args *bargs)
3345 struct btrfs_fs_info *fs_info = bctl->fs_info;
3352 if (btrfs_fs_closing(fs_info) ||
3353 atomic_read(&fs_info->balance_pause_req) ||
3354 atomic_read(&fs_info->balance_cancel_req)) {
3359 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3360 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3364 * In case of mixed groups both data and meta should be picked,
3365 * and identical options should be given for both of them.
3367 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3368 if (mixed && (bctl->flags & allowed)) {
3369 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3370 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3371 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3372 btrfs_err(fs_info, "with mixed groups data and "
3373 "metadata balance options must be the same");
3379 num_devices = fs_info->fs_devices->num_devices;
3380 btrfs_dev_replace_lock(&fs_info->dev_replace);
3381 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3382 BUG_ON(num_devices < 1);
3385 btrfs_dev_replace_unlock(&fs_info->dev_replace);
3386 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
3387 if (num_devices == 1)
3388 allowed |= BTRFS_BLOCK_GROUP_DUP;
3389 else if (num_devices > 1)
3390 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3391 if (num_devices > 2)
3392 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3393 if (num_devices > 3)
3394 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3395 BTRFS_BLOCK_GROUP_RAID6);
3396 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3397 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3398 (bctl->data.target & ~allowed))) {
3399 btrfs_err(fs_info, "unable to start balance with target "
3400 "data profile %llu",
3405 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3406 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3407 (bctl->meta.target & ~allowed))) {
3409 "unable to start balance with target metadata profile %llu",
3414 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3415 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3416 (bctl->sys.target & ~allowed))) {
3418 "unable to start balance with target system profile %llu",
3424 /* allow dup'ed data chunks only in mixed mode */
3425 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3426 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
3427 btrfs_err(fs_info, "dup for data is not allowed");
3432 /* allow to reduce meta or sys integrity only if force set */
3433 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3434 BTRFS_BLOCK_GROUP_RAID10 |
3435 BTRFS_BLOCK_GROUP_RAID5 |
3436 BTRFS_BLOCK_GROUP_RAID6;
3438 seq = read_seqbegin(&fs_info->profiles_lock);
3440 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3441 (fs_info->avail_system_alloc_bits & allowed) &&
3442 !(bctl->sys.target & allowed)) ||
3443 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3444 (fs_info->avail_metadata_alloc_bits & allowed) &&
3445 !(bctl->meta.target & allowed))) {
3446 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3447 btrfs_info(fs_info, "force reducing metadata integrity");
3449 btrfs_err(fs_info, "balance will reduce metadata "
3450 "integrity, use force if you want this");
3455 } while (read_seqretry(&fs_info->profiles_lock, seq));
3457 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3458 int num_tolerated_disk_barrier_failures;
3459 u64 target = bctl->sys.target;
3461 num_tolerated_disk_barrier_failures =
3462 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3463 if (num_tolerated_disk_barrier_failures > 0 &&
3465 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3466 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3467 num_tolerated_disk_barrier_failures = 0;
3468 else if (num_tolerated_disk_barrier_failures > 1 &&
3470 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3471 num_tolerated_disk_barrier_failures = 1;
3473 fs_info->num_tolerated_disk_barrier_failures =
3474 num_tolerated_disk_barrier_failures;
3477 ret = insert_balance_item(fs_info->tree_root, bctl);
3478 if (ret && ret != -EEXIST)
3481 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3482 BUG_ON(ret == -EEXIST);
3483 set_balance_control(bctl);
3485 BUG_ON(ret != -EEXIST);
3486 spin_lock(&fs_info->balance_lock);
3487 update_balance_args(bctl);
3488 spin_unlock(&fs_info->balance_lock);
3491 atomic_inc(&fs_info->balance_running);
3492 mutex_unlock(&fs_info->balance_mutex);
3494 ret = __btrfs_balance(fs_info);
3496 mutex_lock(&fs_info->balance_mutex);
3497 atomic_dec(&fs_info->balance_running);
3499 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3500 fs_info->num_tolerated_disk_barrier_failures =
3501 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3505 memset(bargs, 0, sizeof(*bargs));
3506 update_ioctl_balance_args(fs_info, 0, bargs);
3509 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3510 balance_need_close(fs_info)) {
3511 __cancel_balance(fs_info);
3514 wake_up(&fs_info->balance_wait_q);
3518 if (bctl->flags & BTRFS_BALANCE_RESUME)
3519 __cancel_balance(fs_info);
3522 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3527 static int balance_kthread(void *data)
3529 struct btrfs_fs_info *fs_info = data;
3532 mutex_lock(&fs_info->volume_mutex);
3533 mutex_lock(&fs_info->balance_mutex);
3535 if (fs_info->balance_ctl) {
3536 btrfs_info(fs_info, "continuing balance");
3537 ret = btrfs_balance(fs_info->balance_ctl, NULL);
3540 mutex_unlock(&fs_info->balance_mutex);
3541 mutex_unlock(&fs_info->volume_mutex);
3546 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3548 struct task_struct *tsk;
3550 spin_lock(&fs_info->balance_lock);
3551 if (!fs_info->balance_ctl) {
3552 spin_unlock(&fs_info->balance_lock);
3555 spin_unlock(&fs_info->balance_lock);
3557 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3558 btrfs_info(fs_info, "force skipping balance");
3562 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3563 return PTR_ERR_OR_ZERO(tsk);
3566 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3568 struct btrfs_balance_control *bctl;
3569 struct btrfs_balance_item *item;
3570 struct btrfs_disk_balance_args disk_bargs;
3571 struct btrfs_path *path;
3572 struct extent_buffer *leaf;
3573 struct btrfs_key key;
3576 path = btrfs_alloc_path();
3580 key.objectid = BTRFS_BALANCE_OBJECTID;
3581 key.type = BTRFS_BALANCE_ITEM_KEY;
3584 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3587 if (ret > 0) { /* ret = -ENOENT; */
3592 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3598 leaf = path->nodes[0];
3599 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3601 bctl->fs_info = fs_info;
3602 bctl->flags = btrfs_balance_flags(leaf, item);
3603 bctl->flags |= BTRFS_BALANCE_RESUME;
3605 btrfs_balance_data(leaf, item, &disk_bargs);
3606 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3607 btrfs_balance_meta(leaf, item, &disk_bargs);
3608 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3609 btrfs_balance_sys(leaf, item, &disk_bargs);
3610 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3612 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3614 mutex_lock(&fs_info->volume_mutex);
3615 mutex_lock(&fs_info->balance_mutex);
3617 set_balance_control(bctl);
3619 mutex_unlock(&fs_info->balance_mutex);
3620 mutex_unlock(&fs_info->volume_mutex);
3622 btrfs_free_path(path);
3626 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3630 mutex_lock(&fs_info->balance_mutex);
3631 if (!fs_info->balance_ctl) {
3632 mutex_unlock(&fs_info->balance_mutex);
3636 if (atomic_read(&fs_info->balance_running)) {
3637 atomic_inc(&fs_info->balance_pause_req);
3638 mutex_unlock(&fs_info->balance_mutex);
3640 wait_event(fs_info->balance_wait_q,
3641 atomic_read(&fs_info->balance_running) == 0);
3643 mutex_lock(&fs_info->balance_mutex);
3644 /* we are good with balance_ctl ripped off from under us */
3645 BUG_ON(atomic_read(&fs_info->balance_running));
3646 atomic_dec(&fs_info->balance_pause_req);
3651 mutex_unlock(&fs_info->balance_mutex);
3655 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3657 if (fs_info->sb->s_flags & MS_RDONLY)
3660 mutex_lock(&fs_info->balance_mutex);
3661 if (!fs_info->balance_ctl) {
3662 mutex_unlock(&fs_info->balance_mutex);
3666 atomic_inc(&fs_info->balance_cancel_req);
3668 * if we are running just wait and return, balance item is
3669 * deleted in btrfs_balance in this case
3671 if (atomic_read(&fs_info->balance_running)) {
3672 mutex_unlock(&fs_info->balance_mutex);
3673 wait_event(fs_info->balance_wait_q,
3674 atomic_read(&fs_info->balance_running) == 0);
3675 mutex_lock(&fs_info->balance_mutex);
3677 /* __cancel_balance needs volume_mutex */
3678 mutex_unlock(&fs_info->balance_mutex);
3679 mutex_lock(&fs_info->volume_mutex);
3680 mutex_lock(&fs_info->balance_mutex);
3682 if (fs_info->balance_ctl)
3683 __cancel_balance(fs_info);
3685 mutex_unlock(&fs_info->volume_mutex);
3688 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3689 atomic_dec(&fs_info->balance_cancel_req);
3690 mutex_unlock(&fs_info->balance_mutex);
3694 static int btrfs_uuid_scan_kthread(void *data)
3696 struct btrfs_fs_info *fs_info = data;
3697 struct btrfs_root *root = fs_info->tree_root;
3698 struct btrfs_key key;
3699 struct btrfs_key max_key;
3700 struct btrfs_path *path = NULL;
3702 struct extent_buffer *eb;
3704 struct btrfs_root_item root_item;
3706 struct btrfs_trans_handle *trans = NULL;
3708 path = btrfs_alloc_path();
3715 key.type = BTRFS_ROOT_ITEM_KEY;
3718 max_key.objectid = (u64)-1;
3719 max_key.type = BTRFS_ROOT_ITEM_KEY;
3720 max_key.offset = (u64)-1;
3723 ret = btrfs_search_forward(root, &key, path, 0);
3730 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3731 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3732 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3733 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3736 eb = path->nodes[0];
3737 slot = path->slots[0];
3738 item_size = btrfs_item_size_nr(eb, slot);
3739 if (item_size < sizeof(root_item))
3742 read_extent_buffer(eb, &root_item,
3743 btrfs_item_ptr_offset(eb, slot),
3744 (int)sizeof(root_item));
3745 if (btrfs_root_refs(&root_item) == 0)
3748 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3749 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3753 btrfs_release_path(path);
3755 * 1 - subvol uuid item
3756 * 1 - received_subvol uuid item
3758 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3759 if (IS_ERR(trans)) {
3760 ret = PTR_ERR(trans);
3768 if (!btrfs_is_empty_uuid(root_item.uuid)) {
3769 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3771 BTRFS_UUID_KEY_SUBVOL,
3774 btrfs_warn(fs_info, "uuid_tree_add failed %d",
3780 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
3781 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3782 root_item.received_uuid,
3783 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3786 btrfs_warn(fs_info, "uuid_tree_add failed %d",
3794 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
3800 btrfs_release_path(path);
3801 if (key.offset < (u64)-1) {
3803 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3805 key.type = BTRFS_ROOT_ITEM_KEY;
3806 } else if (key.objectid < (u64)-1) {
3808 key.type = BTRFS_ROOT_ITEM_KEY;
3817 btrfs_free_path(path);
3818 if (trans && !IS_ERR(trans))
3819 btrfs_end_transaction(trans, fs_info->uuid_root);
3821 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
3823 fs_info->update_uuid_tree_gen = 1;
3824 up(&fs_info->uuid_tree_rescan_sem);
3829 * Callback for btrfs_uuid_tree_iterate().
3831 * 0 check succeeded, the entry is not outdated.
3832 * < 0 if an error occured.
3833 * > 0 if the check failed, which means the caller shall remove the entry.
3835 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3836 u8 *uuid, u8 type, u64 subid)
3838 struct btrfs_key key;
3840 struct btrfs_root *subvol_root;
3842 if (type != BTRFS_UUID_KEY_SUBVOL &&
3843 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3846 key.objectid = subid;
3847 key.type = BTRFS_ROOT_ITEM_KEY;
3848 key.offset = (u64)-1;
3849 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3850 if (IS_ERR(subvol_root)) {
3851 ret = PTR_ERR(subvol_root);
3858 case BTRFS_UUID_KEY_SUBVOL:
3859 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3862 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3863 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3873 static int btrfs_uuid_rescan_kthread(void *data)
3875 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3879 * 1st step is to iterate through the existing UUID tree and
3880 * to delete all entries that contain outdated data.
3881 * 2nd step is to add all missing entries to the UUID tree.
3883 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3885 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
3886 up(&fs_info->uuid_tree_rescan_sem);
3889 return btrfs_uuid_scan_kthread(data);
3892 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3894 struct btrfs_trans_handle *trans;
3895 struct btrfs_root *tree_root = fs_info->tree_root;
3896 struct btrfs_root *uuid_root;
3897 struct task_struct *task;
3904 trans = btrfs_start_transaction(tree_root, 2);
3906 return PTR_ERR(trans);
3908 uuid_root = btrfs_create_tree(trans, fs_info,
3909 BTRFS_UUID_TREE_OBJECTID);
3910 if (IS_ERR(uuid_root)) {
3911 btrfs_abort_transaction(trans, tree_root,
3912 PTR_ERR(uuid_root));
3913 return PTR_ERR(uuid_root);
3916 fs_info->uuid_root = uuid_root;
3918 ret = btrfs_commit_transaction(trans, tree_root);
3922 down(&fs_info->uuid_tree_rescan_sem);
3923 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3925 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3926 btrfs_warn(fs_info, "failed to start uuid_scan task");
3927 up(&fs_info->uuid_tree_rescan_sem);
3928 return PTR_ERR(task);
3934 int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3936 struct task_struct *task;
3938 down(&fs_info->uuid_tree_rescan_sem);
3939 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3941 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3942 btrfs_warn(fs_info, "failed to start uuid_rescan task");
3943 up(&fs_info->uuid_tree_rescan_sem);
3944 return PTR_ERR(task);
3951 * shrinking a device means finding all of the device extents past
3952 * the new size, and then following the back refs to the chunks.
3953 * The chunk relocation code actually frees the device extent
3955 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3957 struct btrfs_trans_handle *trans;
3958 struct btrfs_root *root = device->dev_root;
3959 struct btrfs_dev_extent *dev_extent = NULL;
3960 struct btrfs_path *path;
3968 bool retried = false;
3969 struct extent_buffer *l;
3970 struct btrfs_key key;
3971 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
3972 u64 old_total = btrfs_super_total_bytes(super_copy);
3973 u64 old_size = btrfs_device_get_total_bytes(device);
3974 u64 diff = old_size - new_size;
3976 if (device->is_tgtdev_for_dev_replace)
3979 path = btrfs_alloc_path();
3987 btrfs_device_set_total_bytes(device, new_size);
3988 if (device->writeable) {
3989 device->fs_devices->total_rw_bytes -= diff;
3990 spin_lock(&root->fs_info->free_chunk_lock);
3991 root->fs_info->free_chunk_space -= diff;
3992 spin_unlock(&root->fs_info->free_chunk_lock);
3994 unlock_chunks(root);
3997 key.objectid = device->devid;
3998 key.offset = (u64)-1;
3999 key.type = BTRFS_DEV_EXTENT_KEY;
4002 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4006 ret = btrfs_previous_item(root, path, 0, key.type);
4011 btrfs_release_path(path);
4016 slot = path->slots[0];
4017 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4019 if (key.objectid != device->devid) {
4020 btrfs_release_path(path);
4024 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4025 length = btrfs_dev_extent_length(l, dev_extent);
4027 if (key.offset + length <= new_size) {
4028 btrfs_release_path(path);
4032 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
4033 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
4034 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
4035 btrfs_release_path(path);
4037 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
4039 if (ret && ret != -ENOSPC)
4043 } while (key.offset-- > 0);
4045 if (failed && !retried) {
4049 } else if (failed && retried) {
4053 btrfs_device_set_total_bytes(device, old_size);
4054 if (device->writeable)
4055 device->fs_devices->total_rw_bytes += diff;
4056 spin_lock(&root->fs_info->free_chunk_lock);
4057 root->fs_info->free_chunk_space += diff;
4058 spin_unlock(&root->fs_info->free_chunk_lock);
4059 unlock_chunks(root);
4063 /* Shrinking succeeded, else we would be at "done". */
4064 trans = btrfs_start_transaction(root, 0);
4065 if (IS_ERR(trans)) {
4066 ret = PTR_ERR(trans);
4071 btrfs_device_set_disk_total_bytes(device, new_size);
4072 if (list_empty(&device->resized_list))
4073 list_add_tail(&device->resized_list,
4074 &root->fs_info->fs_devices->resized_devices);
4076 WARN_ON(diff > old_total);
4077 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4078 unlock_chunks(root);
4080 /* Now btrfs_update_device() will change the on-disk size. */
4081 ret = btrfs_update_device(trans, device);
4082 btrfs_end_transaction(trans, root);
4084 btrfs_free_path(path);
4088 static int btrfs_add_system_chunk(struct btrfs_root *root,
4089 struct btrfs_key *key,
4090 struct btrfs_chunk *chunk, int item_size)
4092 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
4093 struct btrfs_disk_key disk_key;
4098 array_size = btrfs_super_sys_array_size(super_copy);
4099 if (array_size + item_size + sizeof(disk_key)
4100 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4101 unlock_chunks(root);
4105 ptr = super_copy->sys_chunk_array + array_size;
4106 btrfs_cpu_key_to_disk(&disk_key, key);
4107 memcpy(ptr, &disk_key, sizeof(disk_key));
4108 ptr += sizeof(disk_key);
4109 memcpy(ptr, chunk, item_size);
4110 item_size += sizeof(disk_key);
4111 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4112 unlock_chunks(root);
4118 * sort the devices in descending order by max_avail, total_avail
4120 static int btrfs_cmp_device_info(const void *a, const void *b)
4122 const struct btrfs_device_info *di_a = a;
4123 const struct btrfs_device_info *di_b = b;
4125 if (di_a->max_avail > di_b->max_avail)
4127 if (di_a->max_avail < di_b->max_avail)
4129 if (di_a->total_avail > di_b->total_avail)
4131 if (di_a->total_avail < di_b->total_avail)
4136 static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
4137 [BTRFS_RAID_RAID10] = {
4140 .devs_max = 0, /* 0 == as many as possible */
4142 .devs_increment = 2,
4145 [BTRFS_RAID_RAID1] = {
4150 .devs_increment = 2,
4153 [BTRFS_RAID_DUP] = {
4158 .devs_increment = 1,
4161 [BTRFS_RAID_RAID0] = {
4166 .devs_increment = 1,
4169 [BTRFS_RAID_SINGLE] = {
4174 .devs_increment = 1,
4177 [BTRFS_RAID_RAID5] = {
4182 .devs_increment = 1,
4185 [BTRFS_RAID_RAID6] = {
4190 .devs_increment = 1,
4195 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4197 /* TODO allow them to set a preferred stripe size */
4201 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4203 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
4206 btrfs_set_fs_incompat(info, RAID56);
4209 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4210 - sizeof(struct btrfs_item) \
4211 - sizeof(struct btrfs_chunk)) \
4212 / sizeof(struct btrfs_stripe) + 1)
4214 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4215 - 2 * sizeof(struct btrfs_disk_key) \
4216 - 2 * sizeof(struct btrfs_chunk)) \
4217 / sizeof(struct btrfs_stripe) + 1)
4219 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4220 struct btrfs_root *extent_root, u64 start,
4223 struct btrfs_fs_info *info = extent_root->fs_info;
4224 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4225 struct list_head *cur;
4226 struct map_lookup *map = NULL;
4227 struct extent_map_tree *em_tree;
4228 struct extent_map *em;
4229 struct btrfs_device_info *devices_info = NULL;
4231 int num_stripes; /* total number of stripes to allocate */
4232 int data_stripes; /* number of stripes that count for
4234 int sub_stripes; /* sub_stripes info for map */
4235 int dev_stripes; /* stripes per dev */
4236 int devs_max; /* max devs to use */
4237 int devs_min; /* min devs needed */
4238 int devs_increment; /* ndevs has to be a multiple of this */
4239 int ncopies; /* how many copies to data has */
4241 u64 max_stripe_size;
4245 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
4251 BUG_ON(!alloc_profile_is_valid(type, 0));
4253 if (list_empty(&fs_devices->alloc_list))
4256 index = __get_raid_index(type);
4258 sub_stripes = btrfs_raid_array[index].sub_stripes;
4259 dev_stripes = btrfs_raid_array[index].dev_stripes;
4260 devs_max = btrfs_raid_array[index].devs_max;
4261 devs_min = btrfs_raid_array[index].devs_min;
4262 devs_increment = btrfs_raid_array[index].devs_increment;
4263 ncopies = btrfs_raid_array[index].ncopies;
4265 if (type & BTRFS_BLOCK_GROUP_DATA) {
4266 max_stripe_size = 1024 * 1024 * 1024;
4267 max_chunk_size = 10 * max_stripe_size;
4269 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
4270 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4271 /* for larger filesystems, use larger metadata chunks */
4272 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4273 max_stripe_size = 1024 * 1024 * 1024;
4275 max_stripe_size = 256 * 1024 * 1024;
4276 max_chunk_size = max_stripe_size;
4278 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
4279 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4280 max_stripe_size = 32 * 1024 * 1024;
4281 max_chunk_size = 2 * max_stripe_size;
4283 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
4285 btrfs_err(info, "invalid chunk type 0x%llx requested",
4290 /* we don't want a chunk larger than 10% of writeable space */
4291 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4294 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4299 cur = fs_devices->alloc_list.next;
4302 * in the first pass through the devices list, we gather information
4303 * about the available holes on each device.
4306 while (cur != &fs_devices->alloc_list) {
4307 struct btrfs_device *device;
4311 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4315 if (!device->writeable) {
4317 "BTRFS: read-only device in alloc_list\n");
4321 if (!device->in_fs_metadata ||
4322 device->is_tgtdev_for_dev_replace)
4325 if (device->total_bytes > device->bytes_used)
4326 total_avail = device->total_bytes - device->bytes_used;
4330 /* If there is no space on this device, skip it. */
4331 if (total_avail == 0)
4334 ret = find_free_dev_extent(trans, device,
4335 max_stripe_size * dev_stripes,
4336 &dev_offset, &max_avail);
4337 if (ret && ret != -ENOSPC)
4341 max_avail = max_stripe_size * dev_stripes;
4343 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4346 if (ndevs == fs_devices->rw_devices) {
4347 WARN(1, "%s: found more than %llu devices\n",
4348 __func__, fs_devices->rw_devices);
4351 devices_info[ndevs].dev_offset = dev_offset;
4352 devices_info[ndevs].max_avail = max_avail;
4353 devices_info[ndevs].total_avail = total_avail;
4354 devices_info[ndevs].dev = device;
4359 * now sort the devices by hole size / available space
4361 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4362 btrfs_cmp_device_info, NULL);
4364 /* round down to number of usable stripes */
4365 ndevs -= ndevs % devs_increment;
4367 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4372 if (devs_max && ndevs > devs_max)
4375 * the primary goal is to maximize the number of stripes, so use as many
4376 * devices as possible, even if the stripes are not maximum sized.
4378 stripe_size = devices_info[ndevs-1].max_avail;
4379 num_stripes = ndevs * dev_stripes;
4382 * this will have to be fixed for RAID1 and RAID10 over
4385 data_stripes = num_stripes / ncopies;
4387 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4388 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4389 btrfs_super_stripesize(info->super_copy));
4390 data_stripes = num_stripes - 1;
4392 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4393 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4394 btrfs_super_stripesize(info->super_copy));
4395 data_stripes = num_stripes - 2;
4399 * Use the number of data stripes to figure out how big this chunk
4400 * is really going to be in terms of logical address space,
4401 * and compare that answer with the max chunk size
4403 if (stripe_size * data_stripes > max_chunk_size) {
4404 u64 mask = (1ULL << 24) - 1;
4405 stripe_size = max_chunk_size;
4406 do_div(stripe_size, data_stripes);
4408 /* bump the answer up to a 16MB boundary */
4409 stripe_size = (stripe_size + mask) & ~mask;
4411 /* but don't go higher than the limits we found
4412 * while searching for free extents
4414 if (stripe_size > devices_info[ndevs-1].max_avail)
4415 stripe_size = devices_info[ndevs-1].max_avail;
4418 do_div(stripe_size, dev_stripes);
4420 /* align to BTRFS_STRIPE_LEN */
4421 do_div(stripe_size, raid_stripe_len);
4422 stripe_size *= raid_stripe_len;
4424 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4429 map->num_stripes = num_stripes;
4431 for (i = 0; i < ndevs; ++i) {
4432 for (j = 0; j < dev_stripes; ++j) {
4433 int s = i * dev_stripes + j;
4434 map->stripes[s].dev = devices_info[i].dev;
4435 map->stripes[s].physical = devices_info[i].dev_offset +
4439 map->sector_size = extent_root->sectorsize;
4440 map->stripe_len = raid_stripe_len;
4441 map->io_align = raid_stripe_len;
4442 map->io_width = raid_stripe_len;
4444 map->sub_stripes = sub_stripes;
4446 num_bytes = stripe_size * data_stripes;
4448 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
4450 em = alloc_extent_map();
4456 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
4457 em->bdev = (struct block_device *)map;
4459 em->len = num_bytes;
4460 em->block_start = 0;
4461 em->block_len = em->len;
4462 em->orig_block_len = stripe_size;
4464 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4465 write_lock(&em_tree->lock);
4466 ret = add_extent_mapping(em_tree, em, 0);
4468 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4469 atomic_inc(&em->refs);
4471 write_unlock(&em_tree->lock);
4473 free_extent_map(em);
4477 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4478 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4481 goto error_del_extent;
4483 for (i = 0; i < map->num_stripes; i++) {
4484 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4485 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4488 spin_lock(&extent_root->fs_info->free_chunk_lock);
4489 extent_root->fs_info->free_chunk_space -= (stripe_size *
4491 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4493 free_extent_map(em);
4494 check_raid56_incompat_flag(extent_root->fs_info, type);
4496 kfree(devices_info);
4500 write_lock(&em_tree->lock);
4501 remove_extent_mapping(em_tree, em);
4502 write_unlock(&em_tree->lock);
4504 /* One for our allocation */
4505 free_extent_map(em);
4506 /* One for the tree reference */
4507 free_extent_map(em);
4509 kfree(devices_info);
4513 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4514 struct btrfs_root *extent_root,
4515 u64 chunk_offset, u64 chunk_size)
4517 struct btrfs_key key;
4518 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4519 struct btrfs_device *device;
4520 struct btrfs_chunk *chunk;
4521 struct btrfs_stripe *stripe;
4522 struct extent_map_tree *em_tree;
4523 struct extent_map *em;
4524 struct map_lookup *map;
4531 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4532 read_lock(&em_tree->lock);
4533 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4534 read_unlock(&em_tree->lock);
4537 btrfs_crit(extent_root->fs_info, "unable to find logical "
4538 "%Lu len %Lu", chunk_offset, chunk_size);
4542 if (em->start != chunk_offset || em->len != chunk_size) {
4543 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
4544 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
4545 chunk_size, em->start, em->len);
4546 free_extent_map(em);
4550 map = (struct map_lookup *)em->bdev;
4551 item_size = btrfs_chunk_item_size(map->num_stripes);
4552 stripe_size = em->orig_block_len;
4554 chunk = kzalloc(item_size, GFP_NOFS);
4560 for (i = 0; i < map->num_stripes; i++) {
4561 device = map->stripes[i].dev;
4562 dev_offset = map->stripes[i].physical;
4564 ret = btrfs_update_device(trans, device);
4567 ret = btrfs_alloc_dev_extent(trans, device,
4568 chunk_root->root_key.objectid,
4569 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4570 chunk_offset, dev_offset,
4576 stripe = &chunk->stripe;
4577 for (i = 0; i < map->num_stripes; i++) {
4578 device = map->stripes[i].dev;
4579 dev_offset = map->stripes[i].physical;
4581 btrfs_set_stack_stripe_devid(stripe, device->devid);
4582 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4583 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4587 btrfs_set_stack_chunk_length(chunk, chunk_size);
4588 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4589 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4590 btrfs_set_stack_chunk_type(chunk, map->type);
4591 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4592 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4593 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4594 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4595 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4597 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4598 key.type = BTRFS_CHUNK_ITEM_KEY;
4599 key.offset = chunk_offset;
4601 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
4602 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4604 * TODO: Cleanup of inserted chunk root in case of
4607 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
4613 free_extent_map(em);
4618 * Chunk allocation falls into two parts. The first part does works
4619 * that make the new allocated chunk useable, but not do any operation
4620 * that modifies the chunk tree. The second part does the works that
4621 * require modifying the chunk tree. This division is important for the
4622 * bootstrap process of adding storage to a seed btrfs.
4624 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4625 struct btrfs_root *extent_root, u64 type)
4629 chunk_offset = find_next_chunk(extent_root->fs_info);
4630 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
4633 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
4634 struct btrfs_root *root,
4635 struct btrfs_device *device)
4638 u64 sys_chunk_offset;
4640 struct btrfs_fs_info *fs_info = root->fs_info;
4641 struct btrfs_root *extent_root = fs_info->extent_root;
4644 chunk_offset = find_next_chunk(fs_info);
4645 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
4646 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4651 sys_chunk_offset = find_next_chunk(root->fs_info);
4652 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
4653 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4658 static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4662 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4663 BTRFS_BLOCK_GROUP_RAID10 |
4664 BTRFS_BLOCK_GROUP_RAID5 |
4665 BTRFS_BLOCK_GROUP_DUP)) {
4667 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4676 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4678 struct extent_map *em;
4679 struct map_lookup *map;
4680 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4685 read_lock(&map_tree->map_tree.lock);
4686 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
4687 read_unlock(&map_tree->map_tree.lock);
4691 map = (struct map_lookup *)em->bdev;
4692 for (i = 0; i < map->num_stripes; i++) {
4693 if (map->stripes[i].dev->missing) {
4698 if (!map->stripes[i].dev->writeable) {
4705 * If the number of missing devices is larger than max errors,
4706 * we can not write the data into that chunk successfully, so
4709 if (miss_ndevs > btrfs_chunk_max_errors(map))
4712 free_extent_map(em);
4716 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4718 extent_map_tree_init(&tree->map_tree);
4721 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4723 struct extent_map *em;
4726 write_lock(&tree->map_tree.lock);
4727 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4729 remove_extent_mapping(&tree->map_tree, em);
4730 write_unlock(&tree->map_tree.lock);
4734 free_extent_map(em);
4735 /* once for the tree */
4736 free_extent_map(em);
4740 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
4742 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4743 struct extent_map *em;
4744 struct map_lookup *map;
4745 struct extent_map_tree *em_tree = &map_tree->map_tree;
4748 read_lock(&em_tree->lock);
4749 em = lookup_extent_mapping(em_tree, logical, len);
4750 read_unlock(&em_tree->lock);
4753 * We could return errors for these cases, but that could get ugly and
4754 * we'd probably do the same thing which is just not do anything else
4755 * and exit, so return 1 so the callers don't try to use other copies.
4758 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
4763 if (em->start > logical || em->start + em->len < logical) {
4764 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
4765 "%Lu-%Lu", logical, logical+len, em->start,
4766 em->start + em->len);
4767 free_extent_map(em);
4771 map = (struct map_lookup *)em->bdev;
4772 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4773 ret = map->num_stripes;
4774 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4775 ret = map->sub_stripes;
4776 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4778 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4782 free_extent_map(em);
4784 btrfs_dev_replace_lock(&fs_info->dev_replace);
4785 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4787 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4792 unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4793 struct btrfs_mapping_tree *map_tree,
4796 struct extent_map *em;
4797 struct map_lookup *map;
4798 struct extent_map_tree *em_tree = &map_tree->map_tree;
4799 unsigned long len = root->sectorsize;
4801 read_lock(&em_tree->lock);
4802 em = lookup_extent_mapping(em_tree, logical, len);
4803 read_unlock(&em_tree->lock);
4806 BUG_ON(em->start > logical || em->start + em->len < logical);
4807 map = (struct map_lookup *)em->bdev;
4808 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4809 BTRFS_BLOCK_GROUP_RAID6)) {
4810 len = map->stripe_len * nr_data_stripes(map);
4812 free_extent_map(em);
4816 int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4817 u64 logical, u64 len, int mirror_num)
4819 struct extent_map *em;
4820 struct map_lookup *map;
4821 struct extent_map_tree *em_tree = &map_tree->map_tree;
4824 read_lock(&em_tree->lock);
4825 em = lookup_extent_mapping(em_tree, logical, len);
4826 read_unlock(&em_tree->lock);
4829 BUG_ON(em->start > logical || em->start + em->len < logical);
4830 map = (struct map_lookup *)em->bdev;
4831 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4832 BTRFS_BLOCK_GROUP_RAID6))
4834 free_extent_map(em);
4838 static int find_live_mirror(struct btrfs_fs_info *fs_info,
4839 struct map_lookup *map, int first, int num,
4840 int optimal, int dev_replace_is_ongoing)
4844 struct btrfs_device *srcdev;
4846 if (dev_replace_is_ongoing &&
4847 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4848 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4849 srcdev = fs_info->dev_replace.srcdev;
4854 * try to avoid the drive that is the source drive for a
4855 * dev-replace procedure, only choose it if no other non-missing
4856 * mirror is available
4858 for (tolerance = 0; tolerance < 2; tolerance++) {
4859 if (map->stripes[optimal].dev->bdev &&
4860 (tolerance || map->stripes[optimal].dev != srcdev))
4862 for (i = first; i < first + num; i++) {
4863 if (map->stripes[i].dev->bdev &&
4864 (tolerance || map->stripes[i].dev != srcdev))
4869 /* we couldn't find one that doesn't fail. Just return something
4870 * and the io error handling code will clean up eventually
4875 static inline int parity_smaller(u64 a, u64 b)
4880 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4881 static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4883 struct btrfs_bio_stripe s;
4890 for (i = 0; i < bbio->num_stripes - 1; i++) {
4891 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4892 s = bbio->stripes[i];
4894 bbio->stripes[i] = bbio->stripes[i+1];
4895 raid_map[i] = raid_map[i+1];
4896 bbio->stripes[i+1] = s;
4904 static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
4905 u64 logical, u64 *length,
4906 struct btrfs_bio **bbio_ret,
4907 int mirror_num, u64 **raid_map_ret)
4909 struct extent_map *em;
4910 struct map_lookup *map;
4911 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
4912 struct extent_map_tree *em_tree = &map_tree->map_tree;
4915 u64 stripe_end_offset;
4920 u64 *raid_map = NULL;
4926 struct btrfs_bio *bbio = NULL;
4927 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4928 int dev_replace_is_ongoing = 0;
4929 int num_alloc_stripes;
4930 int patch_the_first_stripe_for_dev_replace = 0;
4931 u64 physical_to_patch_in_first_stripe = 0;
4932 u64 raid56_full_stripe_start = (u64)-1;
4934 read_lock(&em_tree->lock);
4935 em = lookup_extent_mapping(em_tree, logical, *length);
4936 read_unlock(&em_tree->lock);
4939 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
4944 if (em->start > logical || em->start + em->len < logical) {
4945 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
4946 "found %Lu-%Lu", logical, em->start,
4947 em->start + em->len);
4948 free_extent_map(em);
4952 map = (struct map_lookup *)em->bdev;
4953 offset = logical - em->start;
4955 stripe_len = map->stripe_len;
4958 * stripe_nr counts the total number of stripes we have to stride
4959 * to get to this block
4961 do_div(stripe_nr, stripe_len);
4963 stripe_offset = stripe_nr * stripe_len;
4964 BUG_ON(offset < stripe_offset);
4966 /* stripe_offset is the offset of this block in its stripe*/
4967 stripe_offset = offset - stripe_offset;
4969 /* if we're here for raid56, we need to know the stripe aligned start */
4970 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4971 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4972 raid56_full_stripe_start = offset;
4974 /* allow a write of a full stripe, but make sure we don't
4975 * allow straddling of stripes
4977 do_div(raid56_full_stripe_start, full_stripe_len);
4978 raid56_full_stripe_start *= full_stripe_len;
4981 if (rw & REQ_DISCARD) {
4982 /* we don't discard raid56 yet */
4984 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4988 *length = min_t(u64, em->len - offset, *length);
4989 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4991 /* For writes to RAID[56], allow a full stripeset across all disks.
4992 For other RAID types and for RAID[56] reads, just allow a single
4993 stripe (on a single disk). */
4994 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4996 max_len = stripe_len * nr_data_stripes(map) -
4997 (offset - raid56_full_stripe_start);
4999 /* we limit the length of each bio to what fits in a stripe */
5000 max_len = stripe_len - stripe_offset;
5002 *length = min_t(u64, em->len - offset, max_len);
5004 *length = em->len - offset;
5007 /* This is for when we're called from btrfs_merge_bio_hook() and all
5008 it cares about is the length */
5012 btrfs_dev_replace_lock(dev_replace);
5013 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5014 if (!dev_replace_is_ongoing)
5015 btrfs_dev_replace_unlock(dev_replace);
5017 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5018 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5019 dev_replace->tgtdev != NULL) {
5021 * in dev-replace case, for repair case (that's the only
5022 * case where the mirror is selected explicitly when
5023 * calling btrfs_map_block), blocks left of the left cursor
5024 * can also be read from the target drive.
5025 * For REQ_GET_READ_MIRRORS, the target drive is added as
5026 * the last one to the array of stripes. For READ, it also
5027 * needs to be supported using the same mirror number.
5028 * If the requested block is not left of the left cursor,
5029 * EIO is returned. This can happen because btrfs_num_copies()
5030 * returns one more in the dev-replace case.
5032 u64 tmp_length = *length;
5033 struct btrfs_bio *tmp_bbio = NULL;
5034 int tmp_num_stripes;
5035 u64 srcdev_devid = dev_replace->srcdev->devid;
5036 int index_srcdev = 0;
5038 u64 physical_of_found = 0;
5040 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
5041 logical, &tmp_length, &tmp_bbio, 0, NULL);
5043 WARN_ON(tmp_bbio != NULL);
5047 tmp_num_stripes = tmp_bbio->num_stripes;
5048 if (mirror_num > tmp_num_stripes) {
5050 * REQ_GET_READ_MIRRORS does not contain this
5051 * mirror, that means that the requested area
5052 * is not left of the left cursor
5060 * process the rest of the function using the mirror_num
5061 * of the source drive. Therefore look it up first.
5062 * At the end, patch the device pointer to the one of the
5065 for (i = 0; i < tmp_num_stripes; i++) {
5066 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5068 * In case of DUP, in order to keep it
5069 * simple, only add the mirror with the
5070 * lowest physical address
5073 physical_of_found <=
5074 tmp_bbio->stripes[i].physical)
5079 tmp_bbio->stripes[i].physical;
5084 mirror_num = index_srcdev + 1;
5085 patch_the_first_stripe_for_dev_replace = 1;
5086 physical_to_patch_in_first_stripe = physical_of_found;
5095 } else if (mirror_num > map->num_stripes) {
5101 stripe_nr_orig = stripe_nr;
5102 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
5103 do_div(stripe_nr_end, map->stripe_len);
5104 stripe_end_offset = stripe_nr_end * map->stripe_len -
5107 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5108 if (rw & REQ_DISCARD)
5109 num_stripes = min_t(u64, map->num_stripes,
5110 stripe_nr_end - stripe_nr_orig);
5111 stripe_index = do_div(stripe_nr, map->num_stripes);
5112 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5114 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
5115 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
5116 num_stripes = map->num_stripes;
5117 else if (mirror_num)
5118 stripe_index = mirror_num - 1;
5120 stripe_index = find_live_mirror(fs_info, map, 0,
5122 current->pid % map->num_stripes,
5123 dev_replace_is_ongoing);
5124 mirror_num = stripe_index + 1;
5127 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
5128 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
5129 num_stripes = map->num_stripes;
5130 } else if (mirror_num) {
5131 stripe_index = mirror_num - 1;
5136 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5137 int factor = map->num_stripes / map->sub_stripes;
5139 stripe_index = do_div(stripe_nr, factor);
5140 stripe_index *= map->sub_stripes;
5142 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5143 num_stripes = map->sub_stripes;
5144 else if (rw & REQ_DISCARD)
5145 num_stripes = min_t(u64, map->sub_stripes *
5146 (stripe_nr_end - stripe_nr_orig),
5148 else if (mirror_num)
5149 stripe_index += mirror_num - 1;
5151 int old_stripe_index = stripe_index;
5152 stripe_index = find_live_mirror(fs_info, map,
5154 map->sub_stripes, stripe_index +
5155 current->pid % map->sub_stripes,
5156 dev_replace_is_ongoing);
5157 mirror_num = stripe_index - old_stripe_index + 1;
5160 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5161 BTRFS_BLOCK_GROUP_RAID6)) {
5164 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
5168 /* push stripe_nr back to the start of the full stripe */
5169 stripe_nr = raid56_full_stripe_start;
5170 do_div(stripe_nr, stripe_len);
5172 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5174 /* RAID[56] write or recovery. Return all stripes */
5175 num_stripes = map->num_stripes;
5176 max_errors = nr_parity_stripes(map);
5178 raid_map = kmalloc_array(num_stripes, sizeof(u64),
5185 /* Work out the disk rotation on this stripe-set */
5187 rot = do_div(tmp, num_stripes);
5189 /* Fill in the logical address of each stripe */
5190 tmp = stripe_nr * nr_data_stripes(map);
5191 for (i = 0; i < nr_data_stripes(map); i++)
5192 raid_map[(i+rot) % num_stripes] =
5193 em->start + (tmp + i) * map->stripe_len;
5195 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5196 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5197 raid_map[(i+rot+1) % num_stripes] =
5200 *length = map->stripe_len;
5205 * Mirror #0 or #1 means the original data block.
5206 * Mirror #2 is RAID5 parity block.
5207 * Mirror #3 is RAID6 Q block.
5209 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
5211 stripe_index = nr_data_stripes(map) +
5214 /* We distribute the parity blocks across stripes */
5215 tmp = stripe_nr + stripe_index;
5216 stripe_index = do_div(tmp, map->num_stripes);
5217 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5218 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5223 * after this do_div call, stripe_nr is the number of stripes
5224 * on this device we have to walk to find the data, and
5225 * stripe_index is the number of our device in the stripe array
5227 stripe_index = do_div(stripe_nr, map->num_stripes);
5228 mirror_num = stripe_index + 1;
5230 BUG_ON(stripe_index >= map->num_stripes);
5232 num_alloc_stripes = num_stripes;
5233 if (dev_replace_is_ongoing) {
5234 if (rw & (REQ_WRITE | REQ_DISCARD))
5235 num_alloc_stripes <<= 1;
5236 if (rw & REQ_GET_READ_MIRRORS)
5237 num_alloc_stripes++;
5239 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
5245 atomic_set(&bbio->error, 0);
5247 if (rw & REQ_DISCARD) {
5249 int sub_stripes = 0;
5250 u64 stripes_per_dev = 0;
5251 u32 remaining_stripes = 0;
5252 u32 last_stripe = 0;
5255 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5256 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5259 sub_stripes = map->sub_stripes;
5261 factor = map->num_stripes / sub_stripes;
5262 stripes_per_dev = div_u64_rem(stripe_nr_end -
5265 &remaining_stripes);
5266 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5267 last_stripe *= sub_stripes;
5270 for (i = 0; i < num_stripes; i++) {
5271 bbio->stripes[i].physical =
5272 map->stripes[stripe_index].physical +
5273 stripe_offset + stripe_nr * map->stripe_len;
5274 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5276 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5277 BTRFS_BLOCK_GROUP_RAID10)) {
5278 bbio->stripes[i].length = stripes_per_dev *
5281 if (i / sub_stripes < remaining_stripes)
5282 bbio->stripes[i].length +=
5286 * Special for the first stripe and
5289 * |-------|...|-------|
5293 if (i < sub_stripes)
5294 bbio->stripes[i].length -=
5297 if (stripe_index >= last_stripe &&
5298 stripe_index <= (last_stripe +
5300 bbio->stripes[i].length -=
5303 if (i == sub_stripes - 1)
5306 bbio->stripes[i].length = *length;
5309 if (stripe_index == map->num_stripes) {
5310 /* This could only happen for RAID0/10 */
5316 for (i = 0; i < num_stripes; i++) {
5317 bbio->stripes[i].physical =
5318 map->stripes[stripe_index].physical +
5320 stripe_nr * map->stripe_len;
5321 bbio->stripes[i].dev =
5322 map->stripes[stripe_index].dev;
5327 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5328 max_errors = btrfs_chunk_max_errors(map);
5330 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5331 dev_replace->tgtdev != NULL) {
5332 int index_where_to_add;
5333 u64 srcdev_devid = dev_replace->srcdev->devid;
5336 * duplicate the write operations while the dev replace
5337 * procedure is running. Since the copying of the old disk
5338 * to the new disk takes place at run time while the
5339 * filesystem is mounted writable, the regular write
5340 * operations to the old disk have to be duplicated to go
5341 * to the new disk as well.
5342 * Note that device->missing is handled by the caller, and
5343 * that the write to the old disk is already set up in the
5346 index_where_to_add = num_stripes;
5347 for (i = 0; i < num_stripes; i++) {
5348 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5349 /* write to new disk, too */
5350 struct btrfs_bio_stripe *new =
5351 bbio->stripes + index_where_to_add;
5352 struct btrfs_bio_stripe *old =
5355 new->physical = old->physical;
5356 new->length = old->length;
5357 new->dev = dev_replace->tgtdev;
5358 index_where_to_add++;
5362 num_stripes = index_where_to_add;
5363 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5364 dev_replace->tgtdev != NULL) {
5365 u64 srcdev_devid = dev_replace->srcdev->devid;
5366 int index_srcdev = 0;
5368 u64 physical_of_found = 0;
5371 * During the dev-replace procedure, the target drive can
5372 * also be used to read data in case it is needed to repair
5373 * a corrupt block elsewhere. This is possible if the
5374 * requested area is left of the left cursor. In this area,
5375 * the target drive is a full copy of the source drive.
5377 for (i = 0; i < num_stripes; i++) {
5378 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5380 * In case of DUP, in order to keep it
5381 * simple, only add the mirror with the
5382 * lowest physical address
5385 physical_of_found <=
5386 bbio->stripes[i].physical)
5390 physical_of_found = bbio->stripes[i].physical;
5394 u64 length = map->stripe_len;
5396 if (physical_of_found + length <=
5397 dev_replace->cursor_left) {
5398 struct btrfs_bio_stripe *tgtdev_stripe =
5399 bbio->stripes + num_stripes;
5401 tgtdev_stripe->physical = physical_of_found;
5402 tgtdev_stripe->length =
5403 bbio->stripes[index_srcdev].length;
5404 tgtdev_stripe->dev = dev_replace->tgtdev;
5412 bbio->num_stripes = num_stripes;
5413 bbio->max_errors = max_errors;
5414 bbio->mirror_num = mirror_num;
5417 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5418 * mirror_num == num_stripes + 1 && dev_replace target drive is
5419 * available as a mirror
5421 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5422 WARN_ON(num_stripes > 1);
5423 bbio->stripes[0].dev = dev_replace->tgtdev;
5424 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5425 bbio->mirror_num = map->num_stripes + 1;
5428 sort_parity_stripes(bbio, raid_map);
5429 *raid_map_ret = raid_map;
5432 if (dev_replace_is_ongoing)
5433 btrfs_dev_replace_unlock(dev_replace);
5434 free_extent_map(em);
5438 int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
5439 u64 logical, u64 *length,
5440 struct btrfs_bio **bbio_ret, int mirror_num)
5442 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
5446 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5447 u64 chunk_start, u64 physical, u64 devid,
5448 u64 **logical, int *naddrs, int *stripe_len)
5450 struct extent_map_tree *em_tree = &map_tree->map_tree;
5451 struct extent_map *em;
5452 struct map_lookup *map;
5460 read_lock(&em_tree->lock);
5461 em = lookup_extent_mapping(em_tree, chunk_start, 1);
5462 read_unlock(&em_tree->lock);
5465 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
5470 if (em->start != chunk_start) {
5471 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
5472 em->start, chunk_start);
5473 free_extent_map(em);
5476 map = (struct map_lookup *)em->bdev;
5479 rmap_len = map->stripe_len;
5481 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5482 do_div(length, map->num_stripes / map->sub_stripes);
5483 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5484 do_div(length, map->num_stripes);
5485 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5486 BTRFS_BLOCK_GROUP_RAID6)) {
5487 do_div(length, nr_data_stripes(map));
5488 rmap_len = map->stripe_len * nr_data_stripes(map);
5491 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
5492 BUG_ON(!buf); /* -ENOMEM */
5494 for (i = 0; i < map->num_stripes; i++) {
5495 if (devid && map->stripes[i].dev->devid != devid)
5497 if (map->stripes[i].physical > physical ||
5498 map->stripes[i].physical + length <= physical)
5501 stripe_nr = physical - map->stripes[i].physical;
5502 do_div(stripe_nr, map->stripe_len);
5504 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5505 stripe_nr = stripe_nr * map->num_stripes + i;
5506 do_div(stripe_nr, map->sub_stripes);
5507 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5508 stripe_nr = stripe_nr * map->num_stripes + i;
5509 } /* else if RAID[56], multiply by nr_data_stripes().
5510 * Alternatively, just use rmap_len below instead of
5511 * map->stripe_len */
5513 bytenr = chunk_start + stripe_nr * rmap_len;
5514 WARN_ON(nr >= map->num_stripes);
5515 for (j = 0; j < nr; j++) {
5516 if (buf[j] == bytenr)
5520 WARN_ON(nr >= map->num_stripes);
5527 *stripe_len = rmap_len;
5529 free_extent_map(em);
5533 static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5535 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5536 bio_endio_nodec(bio, err);
5538 bio_endio(bio, err);
5542 static void btrfs_end_bio(struct bio *bio, int err)
5544 struct btrfs_bio *bbio = bio->bi_private;
5545 struct btrfs_device *dev = bbio->stripes[0].dev;
5546 int is_orig_bio = 0;
5549 atomic_inc(&bbio->error);
5550 if (err == -EIO || err == -EREMOTEIO) {
5551 unsigned int stripe_index =
5552 btrfs_io_bio(bio)->stripe_index;
5554 BUG_ON(stripe_index >= bbio->num_stripes);
5555 dev = bbio->stripes[stripe_index].dev;
5557 if (bio->bi_rw & WRITE)
5558 btrfs_dev_stat_inc(dev,
5559 BTRFS_DEV_STAT_WRITE_ERRS);
5561 btrfs_dev_stat_inc(dev,
5562 BTRFS_DEV_STAT_READ_ERRS);
5563 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5564 btrfs_dev_stat_inc(dev,
5565 BTRFS_DEV_STAT_FLUSH_ERRS);
5566 btrfs_dev_stat_print_on_error(dev);
5571 if (bio == bbio->orig_bio)
5574 btrfs_bio_counter_dec(bbio->fs_info);
5576 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5579 bio = bbio->orig_bio;
5582 bio->bi_private = bbio->private;
5583 bio->bi_end_io = bbio->end_io;
5584 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5585 /* only send an error to the higher layers if it is
5586 * beyond the tolerance of the btrfs bio
5588 if (atomic_read(&bbio->error) > bbio->max_errors) {
5592 * this bio is actually up to date, we didn't
5593 * go over the max number of errors
5595 set_bit(BIO_UPTODATE, &bio->bi_flags);
5599 btrfs_end_bbio(bbio, bio, err);
5600 } else if (!is_orig_bio) {
5606 * see run_scheduled_bios for a description of why bios are collected for
5609 * This will add one bio to the pending list for a device and make sure
5610 * the work struct is scheduled.
5612 static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5613 struct btrfs_device *device,
5614 int rw, struct bio *bio)
5616 int should_queue = 1;
5617 struct btrfs_pending_bios *pending_bios;
5619 if (device->missing || !device->bdev) {
5620 bio_endio(bio, -EIO);
5624 /* don't bother with additional async steps for reads, right now */
5625 if (!(rw & REQ_WRITE)) {
5627 btrfsic_submit_bio(rw, bio);
5633 * nr_async_bios allows us to reliably return congestion to the
5634 * higher layers. Otherwise, the async bio makes it appear we have
5635 * made progress against dirty pages when we've really just put it
5636 * on a queue for later
5638 atomic_inc(&root->fs_info->nr_async_bios);
5639 WARN_ON(bio->bi_next);
5640 bio->bi_next = NULL;
5643 spin_lock(&device->io_lock);
5644 if (bio->bi_rw & REQ_SYNC)
5645 pending_bios = &device->pending_sync_bios;
5647 pending_bios = &device->pending_bios;
5649 if (pending_bios->tail)
5650 pending_bios->tail->bi_next = bio;
5652 pending_bios->tail = bio;
5653 if (!pending_bios->head)
5654 pending_bios->head = bio;
5655 if (device->running_pending)
5658 spin_unlock(&device->io_lock);
5661 btrfs_queue_work(root->fs_info->submit_workers,
5665 static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5668 struct bio_vec *prev;
5669 struct request_queue *q = bdev_get_queue(bdev);
5670 unsigned int max_sectors = queue_max_sectors(q);
5671 struct bvec_merge_data bvm = {
5673 .bi_sector = sector,
5674 .bi_rw = bio->bi_rw,
5677 if (WARN_ON(bio->bi_vcnt == 0))
5680 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
5681 if (bio_sectors(bio) > max_sectors)
5684 if (!q->merge_bvec_fn)
5687 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
5688 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5693 static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5694 struct bio *bio, u64 physical, int dev_nr,
5697 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5699 bio->bi_private = bbio;
5700 btrfs_io_bio(bio)->stripe_index = dev_nr;
5701 bio->bi_end_io = btrfs_end_bio;
5702 bio->bi_iter.bi_sector = physical >> 9;
5705 struct rcu_string *name;
5708 name = rcu_dereference(dev->name);
5709 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
5710 "(%s id %llu), size=%u\n", rw,
5711 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5712 name->str, dev->devid, bio->bi_iter.bi_size);
5716 bio->bi_bdev = dev->bdev;
5718 btrfs_bio_counter_inc_noblocked(root->fs_info);
5721 btrfs_schedule_bio(root, dev, rw, bio);
5723 btrfsic_submit_bio(rw, bio);
5726 static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5727 struct bio *first_bio, struct btrfs_device *dev,
5728 int dev_nr, int rw, int async)
5730 struct bio_vec *bvec = first_bio->bi_io_vec;
5732 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5733 u64 physical = bbio->stripes[dev_nr].physical;
5736 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5740 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5741 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5742 bvec->bv_offset) < bvec->bv_len) {
5743 u64 len = bio->bi_iter.bi_size;
5745 atomic_inc(&bbio->stripes_pending);
5746 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5754 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5758 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5760 atomic_inc(&bbio->error);
5761 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5762 /* Shoud be the original bio. */
5763 WARN_ON(bio != bbio->orig_bio);
5765 bio->bi_private = bbio->private;
5766 bio->bi_end_io = bbio->end_io;
5767 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5768 bio->bi_iter.bi_sector = logical >> 9;
5770 btrfs_end_bbio(bbio, bio, -EIO);
5774 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5775 int mirror_num, int async_submit)
5777 struct btrfs_device *dev;
5778 struct bio *first_bio = bio;
5779 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
5782 u64 *raid_map = NULL;
5786 struct btrfs_bio *bbio = NULL;
5788 length = bio->bi_iter.bi_size;
5789 map_length = length;
5791 btrfs_bio_counter_inc_blocked(root->fs_info);
5792 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5793 mirror_num, &raid_map);
5795 btrfs_bio_counter_dec(root->fs_info);
5799 total_devs = bbio->num_stripes;
5800 bbio->orig_bio = first_bio;
5801 bbio->private = first_bio->bi_private;
5802 bbio->end_io = first_bio->bi_end_io;
5803 bbio->fs_info = root->fs_info;
5804 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5807 /* In this case, map_length has been set to the length of
5808 a single stripe; not the whole write */
5810 ret = raid56_parity_write(root, bio, bbio,
5811 raid_map, map_length);
5813 ret = raid56_parity_recover(root, bio, bbio,
5814 raid_map, map_length,
5818 * FIXME, replace dosen't support raid56 yet, please fix
5821 btrfs_bio_counter_dec(root->fs_info);
5825 if (map_length < length) {
5826 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5827 logical, length, map_length);
5831 while (dev_nr < total_devs) {
5832 dev = bbio->stripes[dev_nr].dev;
5833 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5834 bbio_error(bbio, first_bio, logical);
5840 * Check and see if we're ok with this bio based on it's size
5841 * and offset with the given device.
5843 if (!bio_size_ok(dev->bdev, first_bio,
5844 bbio->stripes[dev_nr].physical >> 9)) {
5845 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5846 dev_nr, rw, async_submit);
5852 if (dev_nr < total_devs - 1) {
5853 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
5854 BUG_ON(!bio); /* -ENOMEM */
5857 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
5860 submit_stripe_bio(root, bbio, bio,
5861 bbio->stripes[dev_nr].physical, dev_nr, rw,
5865 btrfs_bio_counter_dec(root->fs_info);
5869 struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
5872 struct btrfs_device *device;
5873 struct btrfs_fs_devices *cur_devices;
5875 cur_devices = fs_info->fs_devices;
5876 while (cur_devices) {
5878 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5879 device = __find_device(&cur_devices->devices,
5884 cur_devices = cur_devices->seed;
5889 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5890 struct btrfs_fs_devices *fs_devices,
5891 u64 devid, u8 *dev_uuid)
5893 struct btrfs_device *device;
5895 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5899 list_add(&device->dev_list, &fs_devices->devices);
5900 device->fs_devices = fs_devices;
5901 fs_devices->num_devices++;
5903 device->missing = 1;
5904 fs_devices->missing_devices++;
5910 * btrfs_alloc_device - allocate struct btrfs_device
5911 * @fs_info: used only for generating a new devid, can be NULL if
5912 * devid is provided (i.e. @devid != NULL).
5913 * @devid: a pointer to devid for this device. If NULL a new devid
5915 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5918 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5919 * on error. Returned struct is not linked onto any lists and can be
5920 * destroyed with kfree() right away.
5922 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5926 struct btrfs_device *dev;
5929 if (WARN_ON(!devid && !fs_info))
5930 return ERR_PTR(-EINVAL);
5932 dev = __alloc_device();
5941 ret = find_next_devid(fs_info, &tmp);
5944 return ERR_PTR(ret);
5950 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5952 generate_random_uuid(dev->uuid);
5954 btrfs_init_work(&dev->work, btrfs_submit_helper,
5955 pending_bios_fn, NULL, NULL);
5960 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5961 struct extent_buffer *leaf,
5962 struct btrfs_chunk *chunk)
5964 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5965 struct map_lookup *map;
5966 struct extent_map *em;
5970 u8 uuid[BTRFS_UUID_SIZE];
5975 logical = key->offset;
5976 length = btrfs_chunk_length(leaf, chunk);
5978 read_lock(&map_tree->map_tree.lock);
5979 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
5980 read_unlock(&map_tree->map_tree.lock);
5982 /* already mapped? */
5983 if (em && em->start <= logical && em->start + em->len > logical) {
5984 free_extent_map(em);
5987 free_extent_map(em);
5990 em = alloc_extent_map();
5993 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5994 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5996 free_extent_map(em);
6000 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
6001 em->bdev = (struct block_device *)map;
6002 em->start = logical;
6005 em->block_start = 0;
6006 em->block_len = em->len;
6008 map->num_stripes = num_stripes;
6009 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6010 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6011 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6012 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6013 map->type = btrfs_chunk_type(leaf, chunk);
6014 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6015 for (i = 0; i < num_stripes; i++) {
6016 map->stripes[i].physical =
6017 btrfs_stripe_offset_nr(leaf, chunk, i);
6018 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
6019 read_extent_buffer(leaf, uuid, (unsigned long)
6020 btrfs_stripe_dev_uuid_nr(chunk, i),
6022 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6024 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
6025 free_extent_map(em);
6028 if (!map->stripes[i].dev) {
6029 map->stripes[i].dev =
6030 add_missing_dev(root, root->fs_info->fs_devices,
6032 if (!map->stripes[i].dev) {
6033 free_extent_map(em);
6037 map->stripes[i].dev->in_fs_metadata = 1;
6040 write_lock(&map_tree->map_tree.lock);
6041 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
6042 write_unlock(&map_tree->map_tree.lock);
6043 BUG_ON(ret); /* Tree corruption */
6044 free_extent_map(em);
6049 static void fill_device_from_item(struct extent_buffer *leaf,
6050 struct btrfs_dev_item *dev_item,
6051 struct btrfs_device *device)
6055 device->devid = btrfs_device_id(leaf, dev_item);
6056 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6057 device->total_bytes = device->disk_total_bytes;
6058 device->commit_total_bytes = device->disk_total_bytes;
6059 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
6060 device->commit_bytes_used = device->bytes_used;
6061 device->type = btrfs_device_type(leaf, dev_item);
6062 device->io_align = btrfs_device_io_align(leaf, dev_item);
6063 device->io_width = btrfs_device_io_width(leaf, dev_item);
6064 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
6065 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
6066 device->is_tgtdev_for_dev_replace = 0;
6068 ptr = btrfs_device_uuid(dev_item);
6069 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
6072 static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6075 struct btrfs_fs_devices *fs_devices;
6078 BUG_ON(!mutex_is_locked(&uuid_mutex));
6080 fs_devices = root->fs_info->fs_devices->seed;
6081 while (fs_devices) {
6082 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6085 fs_devices = fs_devices->seed;
6088 fs_devices = find_fsid(fsid);
6090 if (!btrfs_test_opt(root, DEGRADED))
6091 return ERR_PTR(-ENOENT);
6093 fs_devices = alloc_fs_devices(fsid);
6094 if (IS_ERR(fs_devices))
6097 fs_devices->seeding = 1;
6098 fs_devices->opened = 1;
6102 fs_devices = clone_fs_devices(fs_devices);
6103 if (IS_ERR(fs_devices))
6106 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
6107 root->fs_info->bdev_holder);
6109 free_fs_devices(fs_devices);
6110 fs_devices = ERR_PTR(ret);
6114 if (!fs_devices->seeding) {
6115 __btrfs_close_devices(fs_devices);
6116 free_fs_devices(fs_devices);
6117 fs_devices = ERR_PTR(-EINVAL);
6121 fs_devices->seed = root->fs_info->fs_devices->seed;
6122 root->fs_info->fs_devices->seed = fs_devices;
6127 static int read_one_dev(struct btrfs_root *root,
6128 struct extent_buffer *leaf,
6129 struct btrfs_dev_item *dev_item)
6131 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6132 struct btrfs_device *device;
6135 u8 fs_uuid[BTRFS_UUID_SIZE];
6136 u8 dev_uuid[BTRFS_UUID_SIZE];
6138 devid = btrfs_device_id(leaf, dev_item);
6139 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
6141 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
6144 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
6145 fs_devices = open_seed_devices(root, fs_uuid);
6146 if (IS_ERR(fs_devices))
6147 return PTR_ERR(fs_devices);
6150 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
6152 if (!btrfs_test_opt(root, DEGRADED))
6155 btrfs_warn(root->fs_info, "devid %llu missing", devid);
6156 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6160 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6163 if(!device->bdev && !device->missing) {
6165 * this happens when a device that was properly setup
6166 * in the device info lists suddenly goes bad.
6167 * device->bdev is NULL, and so we have to set
6168 * device->missing to one here
6170 device->fs_devices->missing_devices++;
6171 device->missing = 1;
6174 /* Move the device to its own fs_devices */
6175 if (device->fs_devices != fs_devices) {
6176 ASSERT(device->missing);
6178 list_move(&device->dev_list, &fs_devices->devices);
6179 device->fs_devices->num_devices--;
6180 fs_devices->num_devices++;
6182 device->fs_devices->missing_devices--;
6183 fs_devices->missing_devices++;
6185 device->fs_devices = fs_devices;
6189 if (device->fs_devices != root->fs_info->fs_devices) {
6190 BUG_ON(device->writeable);
6191 if (device->generation !=
6192 btrfs_device_generation(leaf, dev_item))
6196 fill_device_from_item(leaf, dev_item, device);
6197 device->in_fs_metadata = 1;
6198 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
6199 device->fs_devices->total_rw_bytes += device->total_bytes;
6200 spin_lock(&root->fs_info->free_chunk_lock);
6201 root->fs_info->free_chunk_space += device->total_bytes -
6203 spin_unlock(&root->fs_info->free_chunk_lock);
6209 int btrfs_read_sys_array(struct btrfs_root *root)
6211 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
6212 struct extent_buffer *sb;
6213 struct btrfs_disk_key *disk_key;
6214 struct btrfs_chunk *chunk;
6216 unsigned long sb_ptr;
6222 struct btrfs_key key;
6224 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
6225 BTRFS_SUPER_INFO_SIZE);
6228 btrfs_set_buffer_uptodate(sb);
6229 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
6231 * The sb extent buffer is artifical and just used to read the system array.
6232 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6233 * pages up-to-date when the page is larger: extent does not cover the
6234 * whole page and consequently check_page_uptodate does not find all
6235 * the page's extents up-to-date (the hole beyond sb),
6236 * write_extent_buffer then triggers a WARN_ON.
6238 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6239 * but sb spans only this function. Add an explicit SetPageUptodate call
6240 * to silence the warning eg. on PowerPC 64.
6242 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
6243 SetPageUptodate(sb->pages[0]);
6245 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
6246 array_size = btrfs_super_sys_array_size(super_copy);
6248 ptr = super_copy->sys_chunk_array;
6249 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
6252 while (cur < array_size) {
6253 disk_key = (struct btrfs_disk_key *)ptr;
6254 btrfs_disk_key_to_cpu(&key, disk_key);
6256 len = sizeof(*disk_key); ptr += len;
6260 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
6261 chunk = (struct btrfs_chunk *)sb_ptr;
6262 ret = read_one_chunk(root, &key, sb, chunk);
6265 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6266 len = btrfs_chunk_item_size(num_stripes);
6275 free_extent_buffer(sb);
6279 int btrfs_read_chunk_tree(struct btrfs_root *root)
6281 struct btrfs_path *path;
6282 struct extent_buffer *leaf;
6283 struct btrfs_key key;
6284 struct btrfs_key found_key;
6288 root = root->fs_info->chunk_root;
6290 path = btrfs_alloc_path();
6294 mutex_lock(&uuid_mutex);
6298 * Read all device items, and then all the chunk items. All
6299 * device items are found before any chunk item (their object id
6300 * is smaller than the lowest possible object id for a chunk
6301 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
6303 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6310 leaf = path->nodes[0];
6311 slot = path->slots[0];
6312 if (slot >= btrfs_header_nritems(leaf)) {
6313 ret = btrfs_next_leaf(root, path);
6320 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6321 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6322 struct btrfs_dev_item *dev_item;
6323 dev_item = btrfs_item_ptr(leaf, slot,
6324 struct btrfs_dev_item);
6325 ret = read_one_dev(root, leaf, dev_item);
6328 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6329 struct btrfs_chunk *chunk;
6330 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6331 ret = read_one_chunk(root, &found_key, leaf, chunk);
6339 unlock_chunks(root);
6340 mutex_unlock(&uuid_mutex);
6342 btrfs_free_path(path);
6346 void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6348 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6349 struct btrfs_device *device;
6351 while (fs_devices) {
6352 mutex_lock(&fs_devices->device_list_mutex);
6353 list_for_each_entry(device, &fs_devices->devices, dev_list)
6354 device->dev_root = fs_info->dev_root;
6355 mutex_unlock(&fs_devices->device_list_mutex);
6357 fs_devices = fs_devices->seed;
6361 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6365 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6366 btrfs_dev_stat_reset(dev, i);
6369 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6371 struct btrfs_key key;
6372 struct btrfs_key found_key;
6373 struct btrfs_root *dev_root = fs_info->dev_root;
6374 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6375 struct extent_buffer *eb;
6378 struct btrfs_device *device;
6379 struct btrfs_path *path = NULL;
6382 path = btrfs_alloc_path();
6388 mutex_lock(&fs_devices->device_list_mutex);
6389 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6391 struct btrfs_dev_stats_item *ptr;
6394 key.type = BTRFS_DEV_STATS_KEY;
6395 key.offset = device->devid;
6396 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6398 __btrfs_reset_dev_stats(device);
6399 device->dev_stats_valid = 1;
6400 btrfs_release_path(path);
6403 slot = path->slots[0];
6404 eb = path->nodes[0];
6405 btrfs_item_key_to_cpu(eb, &found_key, slot);
6406 item_size = btrfs_item_size_nr(eb, slot);
6408 ptr = btrfs_item_ptr(eb, slot,
6409 struct btrfs_dev_stats_item);
6411 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6412 if (item_size >= (1 + i) * sizeof(__le64))
6413 btrfs_dev_stat_set(device, i,
6414 btrfs_dev_stats_value(eb, ptr, i));
6416 btrfs_dev_stat_reset(device, i);
6419 device->dev_stats_valid = 1;
6420 btrfs_dev_stat_print_on_load(device);
6421 btrfs_release_path(path);
6423 mutex_unlock(&fs_devices->device_list_mutex);
6426 btrfs_free_path(path);
6427 return ret < 0 ? ret : 0;
6430 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6431 struct btrfs_root *dev_root,
6432 struct btrfs_device *device)
6434 struct btrfs_path *path;
6435 struct btrfs_key key;
6436 struct extent_buffer *eb;
6437 struct btrfs_dev_stats_item *ptr;
6442 key.type = BTRFS_DEV_STATS_KEY;
6443 key.offset = device->devid;
6445 path = btrfs_alloc_path();
6447 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6449 printk_in_rcu(KERN_WARNING "BTRFS: "
6450 "error %d while searching for dev_stats item for device %s!\n",
6451 ret, rcu_str_deref(device->name));
6456 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6457 /* need to delete old one and insert a new one */
6458 ret = btrfs_del_item(trans, dev_root, path);
6460 printk_in_rcu(KERN_WARNING "BTRFS: "
6461 "delete too small dev_stats item for device %s failed %d!\n",
6462 rcu_str_deref(device->name), ret);
6469 /* need to insert a new item */
6470 btrfs_release_path(path);
6471 ret = btrfs_insert_empty_item(trans, dev_root, path,
6472 &key, sizeof(*ptr));
6474 printk_in_rcu(KERN_WARNING "BTRFS: "
6475 "insert dev_stats item for device %s failed %d!\n",
6476 rcu_str_deref(device->name), ret);
6481 eb = path->nodes[0];
6482 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6483 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6484 btrfs_set_dev_stats_value(eb, ptr, i,
6485 btrfs_dev_stat_read(device, i));
6486 btrfs_mark_buffer_dirty(eb);
6489 btrfs_free_path(path);
6494 * called from commit_transaction. Writes all changed device stats to disk.
6496 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6497 struct btrfs_fs_info *fs_info)
6499 struct btrfs_root *dev_root = fs_info->dev_root;
6500 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6501 struct btrfs_device *device;
6505 mutex_lock(&fs_devices->device_list_mutex);
6506 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6507 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
6510 stats_cnt = atomic_read(&device->dev_stats_ccnt);
6511 ret = update_dev_stat_item(trans, dev_root, device);
6513 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
6515 mutex_unlock(&fs_devices->device_list_mutex);
6520 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6522 btrfs_dev_stat_inc(dev, index);
6523 btrfs_dev_stat_print_on_error(dev);
6526 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
6528 if (!dev->dev_stats_valid)
6530 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6531 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6532 rcu_str_deref(dev->name),
6533 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6534 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6535 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6536 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6537 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6540 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6544 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6545 if (btrfs_dev_stat_read(dev, i) != 0)
6547 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6548 return; /* all values == 0, suppress message */
6550 printk_in_rcu(KERN_INFO "BTRFS: "
6551 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6552 rcu_str_deref(dev->name),
6553 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6554 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6555 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6556 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6557 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6560 int btrfs_get_dev_stats(struct btrfs_root *root,
6561 struct btrfs_ioctl_get_dev_stats *stats)
6563 struct btrfs_device *dev;
6564 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6567 mutex_lock(&fs_devices->device_list_mutex);
6568 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
6569 mutex_unlock(&fs_devices->device_list_mutex);
6572 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
6574 } else if (!dev->dev_stats_valid) {
6575 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
6577 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
6578 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6579 if (stats->nr_items > i)
6581 btrfs_dev_stat_read_and_reset(dev, i);
6583 btrfs_dev_stat_reset(dev, i);
6586 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6587 if (stats->nr_items > i)
6588 stats->values[i] = btrfs_dev_stat_read(dev, i);
6590 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6591 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6595 int btrfs_scratch_superblock(struct btrfs_device *device)
6597 struct buffer_head *bh;
6598 struct btrfs_super_block *disk_super;
6600 bh = btrfs_read_dev_super(device->bdev);
6603 disk_super = (struct btrfs_super_block *)bh->b_data;
6605 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6606 set_buffer_dirty(bh);
6607 sync_dirty_buffer(bh);
6614 * Update the size of all devices, which is used for writing out the
6617 void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6619 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6620 struct btrfs_device *curr, *next;
6622 if (list_empty(&fs_devices->resized_devices))
6625 mutex_lock(&fs_devices->device_list_mutex);
6626 lock_chunks(fs_info->dev_root);
6627 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6629 list_del_init(&curr->resized_list);
6630 curr->commit_total_bytes = curr->disk_total_bytes;
6632 unlock_chunks(fs_info->dev_root);
6633 mutex_unlock(&fs_devices->device_list_mutex);
6636 /* Must be invoked during the transaction commit */
6637 void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6638 struct btrfs_transaction *transaction)
6640 struct extent_map *em;
6641 struct map_lookup *map;
6642 struct btrfs_device *dev;
6645 if (list_empty(&transaction->pending_chunks))
6648 /* In order to kick the device replace finish process */
6650 list_for_each_entry(em, &transaction->pending_chunks, list) {
6651 map = (struct map_lookup *)em->bdev;
6653 for (i = 0; i < map->num_stripes; i++) {
6654 dev = map->stripes[i].dev;
6655 dev->commit_bytes_used = dev->bytes_used;
6658 unlock_chunks(root);