1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/buffer_head.h>
10 #include <linux/blkdev.h>
11 #include <linux/iocontext.h>
12 #include <linux/capability.h>
13 #include <linux/ratelimit.h>
14 #include <linux/kthread.h>
15 #include <linux/raid/pq.h>
16 #include <linux/semaphore.h>
17 #include <linux/uuid.h>
18 #include <linux/list_sort.h>
19 #include <asm/div64.h>
21 #include "extent_map.h"
23 #include "transaction.h"
24 #include "print-tree.h"
27 #include "async-thread.h"
28 #include "check-integrity.h"
29 #include "rcu-string.h"
31 #include "dev-replace.h"
34 const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
35 [BTRFS_RAID_RAID10] = {
38 .devs_max = 0, /* 0 == as many as possible */
40 .tolerated_failures = 1,
44 [BTRFS_RAID_RAID1] = {
49 .tolerated_failures = 1,
58 .tolerated_failures = 0,
62 [BTRFS_RAID_RAID0] = {
67 .tolerated_failures = 0,
71 [BTRFS_RAID_SINGLE] = {
76 .tolerated_failures = 0,
80 [BTRFS_RAID_RAID5] = {
85 .tolerated_failures = 1,
89 [BTRFS_RAID_RAID6] = {
94 .tolerated_failures = 2,
100 const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
101 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
102 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
103 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
104 [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0,
105 [BTRFS_RAID_SINGLE] = 0,
106 [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5,
107 [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6,
111 * Table to convert BTRFS_RAID_* to the error code if minimum number of devices
112 * condition is not met. Zero means there's no corresponding
113 * BTRFS_ERROR_DEV_*_NOT_MET value.
115 const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = {
116 [BTRFS_RAID_RAID10] = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
117 [BTRFS_RAID_RAID1] = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
118 [BTRFS_RAID_DUP] = 0,
119 [BTRFS_RAID_RAID0] = 0,
120 [BTRFS_RAID_SINGLE] = 0,
121 [BTRFS_RAID_RAID5] = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
122 [BTRFS_RAID_RAID6] = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
125 static int init_first_rw_device(struct btrfs_trans_handle *trans,
126 struct btrfs_fs_info *fs_info);
127 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
128 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
129 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
130 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
131 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
132 enum btrfs_map_op op,
133 u64 logical, u64 *length,
134 struct btrfs_bio **bbio_ret,
135 int mirror_num, int need_raid_map);
141 * There are several mutexes that protect manipulation of devices and low-level
142 * structures like chunks but not block groups, extents or files
144 * uuid_mutex (global lock)
145 * ------------------------
146 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
147 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
148 * device) or requested by the device= mount option
150 * the mutex can be very coarse and can cover long-running operations
152 * protects: updates to fs_devices counters like missing devices, rw devices,
153 * seeding, structure cloning, openning/closing devices at mount/umount time
155 * global::fs_devs - add, remove, updates to the global list
157 * does not protect: manipulation of the fs_devices::devices list!
159 * btrfs_device::name - renames (write side), read is RCU
161 * fs_devices::device_list_mutex (per-fs, with RCU)
162 * ------------------------------------------------
163 * protects updates to fs_devices::devices, ie. adding and deleting
165 * simple list traversal with read-only actions can be done with RCU protection
167 * may be used to exclude some operations from running concurrently without any
168 * modifications to the list (see write_all_supers)
172 * coarse lock owned by a mounted filesystem; used to exclude some operations
173 * that cannot run in parallel and affect the higher-level properties of the
174 * filesystem like: device add/deleting/resize/replace, or balance
178 * protects balance structures (status, state) and context accessed from
179 * several places (internally, ioctl)
183 * protects chunks, adding or removing during allocation, trim or when a new
184 * device is added/removed
188 * a big lock that is held by the cleaner thread and prevents running subvolume
189 * cleaning together with relocation or delayed iputs
202 DEFINE_MUTEX(uuid_mutex);
203 static LIST_HEAD(fs_uuids);
204 struct list_head *btrfs_get_fs_uuids(void)
210 * alloc_fs_devices - allocate struct btrfs_fs_devices
211 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
213 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
214 * The returned struct is not linked onto any lists and can be destroyed with
215 * kfree() right away.
217 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
219 struct btrfs_fs_devices *fs_devs;
221 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
223 return ERR_PTR(-ENOMEM);
225 mutex_init(&fs_devs->device_list_mutex);
227 INIT_LIST_HEAD(&fs_devs->devices);
228 INIT_LIST_HEAD(&fs_devs->resized_devices);
229 INIT_LIST_HEAD(&fs_devs->alloc_list);
230 INIT_LIST_HEAD(&fs_devs->list);
232 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
237 static void free_device(struct btrfs_device *device)
239 rcu_string_free(device->name);
240 bio_put(device->flush_bio);
244 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
246 struct btrfs_device *device;
247 WARN_ON(fs_devices->opened);
248 while (!list_empty(&fs_devices->devices)) {
249 device = list_entry(fs_devices->devices.next,
250 struct btrfs_device, dev_list);
251 list_del(&device->dev_list);
257 static void btrfs_kobject_uevent(struct block_device *bdev,
258 enum kobject_action action)
262 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
264 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
266 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
267 &disk_to_dev(bdev->bd_disk)->kobj);
270 void __exit btrfs_cleanup_fs_uuids(void)
272 struct btrfs_fs_devices *fs_devices;
274 while (!list_empty(&fs_uuids)) {
275 fs_devices = list_entry(fs_uuids.next,
276 struct btrfs_fs_devices, list);
277 list_del(&fs_devices->list);
278 free_fs_devices(fs_devices);
283 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
284 * Returned struct is not linked onto any lists and must be destroyed using
287 static struct btrfs_device *__alloc_device(void)
289 struct btrfs_device *dev;
291 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
293 return ERR_PTR(-ENOMEM);
296 * Preallocate a bio that's always going to be used for flushing device
297 * barriers and matches the device lifespan
299 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
300 if (!dev->flush_bio) {
302 return ERR_PTR(-ENOMEM);
305 INIT_LIST_HEAD(&dev->dev_list);
306 INIT_LIST_HEAD(&dev->dev_alloc_list);
307 INIT_LIST_HEAD(&dev->resized_list);
309 spin_lock_init(&dev->io_lock);
311 atomic_set(&dev->reada_in_flight, 0);
312 atomic_set(&dev->dev_stats_ccnt, 0);
313 btrfs_device_data_ordered_init(dev);
314 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
315 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
321 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
324 * If devid and uuid are both specified, the match must be exact, otherwise
325 * only devid is used.
327 static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
328 u64 devid, const u8 *uuid)
330 struct list_head *head = &fs_devices->devices;
331 struct btrfs_device *dev;
333 list_for_each_entry(dev, head, dev_list) {
334 if (dev->devid == devid &&
335 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
342 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
344 struct btrfs_fs_devices *fs_devices;
346 list_for_each_entry(fs_devices, &fs_uuids, list) {
347 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
354 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
355 int flush, struct block_device **bdev,
356 struct buffer_head **bh)
360 *bdev = blkdev_get_by_path(device_path, flags, holder);
363 ret = PTR_ERR(*bdev);
368 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
369 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
371 blkdev_put(*bdev, flags);
374 invalidate_bdev(*bdev);
375 *bh = btrfs_read_dev_super(*bdev);
378 blkdev_put(*bdev, flags);
390 static void requeue_list(struct btrfs_pending_bios *pending_bios,
391 struct bio *head, struct bio *tail)
394 struct bio *old_head;
396 old_head = pending_bios->head;
397 pending_bios->head = head;
398 if (pending_bios->tail)
399 tail->bi_next = old_head;
401 pending_bios->tail = tail;
405 * we try to collect pending bios for a device so we don't get a large
406 * number of procs sending bios down to the same device. This greatly
407 * improves the schedulers ability to collect and merge the bios.
409 * But, it also turns into a long list of bios to process and that is sure
410 * to eventually make the worker thread block. The solution here is to
411 * make some progress and then put this work struct back at the end of
412 * the list if the block device is congested. This way, multiple devices
413 * can make progress from a single worker thread.
415 static noinline void run_scheduled_bios(struct btrfs_device *device)
417 struct btrfs_fs_info *fs_info = device->fs_info;
419 struct backing_dev_info *bdi;
420 struct btrfs_pending_bios *pending_bios;
424 unsigned long num_run;
425 unsigned long batch_run = 0;
426 unsigned long last_waited = 0;
428 int sync_pending = 0;
429 struct blk_plug plug;
432 * this function runs all the bios we've collected for
433 * a particular device. We don't want to wander off to
434 * another device without first sending all of these down.
435 * So, setup a plug here and finish it off before we return
437 blk_start_plug(&plug);
439 bdi = device->bdev->bd_bdi;
442 spin_lock(&device->io_lock);
447 /* take all the bios off the list at once and process them
448 * later on (without the lock held). But, remember the
449 * tail and other pointers so the bios can be properly reinserted
450 * into the list if we hit congestion
452 if (!force_reg && device->pending_sync_bios.head) {
453 pending_bios = &device->pending_sync_bios;
456 pending_bios = &device->pending_bios;
460 pending = pending_bios->head;
461 tail = pending_bios->tail;
462 WARN_ON(pending && !tail);
465 * if pending was null this time around, no bios need processing
466 * at all and we can stop. Otherwise it'll loop back up again
467 * and do an additional check so no bios are missed.
469 * device->running_pending is used to synchronize with the
472 if (device->pending_sync_bios.head == NULL &&
473 device->pending_bios.head == NULL) {
475 device->running_pending = 0;
478 device->running_pending = 1;
481 pending_bios->head = NULL;
482 pending_bios->tail = NULL;
484 spin_unlock(&device->io_lock);
489 /* we want to work on both lists, but do more bios on the
490 * sync list than the regular list
493 pending_bios != &device->pending_sync_bios &&
494 device->pending_sync_bios.head) ||
495 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
496 device->pending_bios.head)) {
497 spin_lock(&device->io_lock);
498 requeue_list(pending_bios, pending, tail);
503 pending = pending->bi_next;
506 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
509 * if we're doing the sync list, record that our
510 * plug has some sync requests on it
512 * If we're doing the regular list and there are
513 * sync requests sitting around, unplug before
516 if (pending_bios == &device->pending_sync_bios) {
518 } else if (sync_pending) {
519 blk_finish_plug(&plug);
520 blk_start_plug(&plug);
524 btrfsic_submit_bio(cur);
531 * we made progress, there is more work to do and the bdi
532 * is now congested. Back off and let other work structs
535 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
536 fs_info->fs_devices->open_devices > 1) {
537 struct io_context *ioc;
539 ioc = current->io_context;
542 * the main goal here is that we don't want to
543 * block if we're going to be able to submit
544 * more requests without blocking.
546 * This code does two great things, it pokes into
547 * the elevator code from a filesystem _and_
548 * it makes assumptions about how batching works.
550 if (ioc && ioc->nr_batch_requests > 0 &&
551 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
553 ioc->last_waited == last_waited)) {
555 * we want to go through our batch of
556 * requests and stop. So, we copy out
557 * the ioc->last_waited time and test
558 * against it before looping
560 last_waited = ioc->last_waited;
564 spin_lock(&device->io_lock);
565 requeue_list(pending_bios, pending, tail);
566 device->running_pending = 1;
568 spin_unlock(&device->io_lock);
569 btrfs_queue_work(fs_info->submit_workers,
579 spin_lock(&device->io_lock);
580 if (device->pending_bios.head || device->pending_sync_bios.head)
582 spin_unlock(&device->io_lock);
585 blk_finish_plug(&plug);
588 static void pending_bios_fn(struct btrfs_work *work)
590 struct btrfs_device *device;
592 device = container_of(work, struct btrfs_device, work);
593 run_scheduled_bios(device);
597 * Search and remove all stale (devices which are not mounted) devices.
598 * When both inputs are NULL, it will search and release all stale devices.
599 * path: Optional. When provided will it release all unmounted devices
600 * matching this path only.
601 * skip_dev: Optional. Will skip this device when searching for the stale
604 static void btrfs_free_stale_devices(const char *path,
605 struct btrfs_device *skip_dev)
607 struct btrfs_fs_devices *fs_devs, *tmp_fs_devs;
608 struct btrfs_device *dev, *tmp_dev;
610 list_for_each_entry_safe(fs_devs, tmp_fs_devs, &fs_uuids, list) {
615 list_for_each_entry_safe(dev, tmp_dev,
616 &fs_devs->devices, dev_list) {
619 if (skip_dev && skip_dev == dev)
621 if (path && !dev->name)
626 not_found = strcmp(rcu_str_deref(dev->name),
632 /* delete the stale device */
633 if (fs_devs->num_devices == 1) {
634 btrfs_sysfs_remove_fsid(fs_devs);
635 list_del(&fs_devs->list);
636 free_fs_devices(fs_devs);
639 fs_devs->num_devices--;
640 list_del(&dev->dev_list);
647 static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
648 struct btrfs_device *device, fmode_t flags,
651 struct request_queue *q;
652 struct block_device *bdev;
653 struct buffer_head *bh;
654 struct btrfs_super_block *disk_super;
663 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
668 disk_super = (struct btrfs_super_block *)bh->b_data;
669 devid = btrfs_stack_device_id(&disk_super->dev_item);
670 if (devid != device->devid)
673 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
676 device->generation = btrfs_super_generation(disk_super);
678 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
679 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
680 fs_devices->seeding = 1;
682 if (bdev_read_only(bdev))
683 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
685 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
688 q = bdev_get_queue(bdev);
689 if (!blk_queue_nonrot(q))
690 fs_devices->rotating = 1;
693 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
694 device->mode = flags;
696 fs_devices->open_devices++;
697 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
698 device->devid != BTRFS_DEV_REPLACE_DEVID) {
699 fs_devices->rw_devices++;
700 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
708 blkdev_put(bdev, flags);
714 * Add new device to list of registered devices
717 * device pointer which was just added or updated when successful
718 * error pointer when failed
720 static noinline struct btrfs_device *device_list_add(const char *path,
721 struct btrfs_super_block *disk_super)
723 struct btrfs_device *device;
724 struct btrfs_fs_devices *fs_devices;
725 struct rcu_string *name;
726 u64 found_transid = btrfs_super_generation(disk_super);
727 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
729 fs_devices = find_fsid(disk_super->fsid);
731 fs_devices = alloc_fs_devices(disk_super->fsid);
732 if (IS_ERR(fs_devices))
733 return ERR_CAST(fs_devices);
735 list_add(&fs_devices->list, &fs_uuids);
739 device = find_device(fs_devices, devid,
740 disk_super->dev_item.uuid);
744 if (fs_devices->opened)
745 return ERR_PTR(-EBUSY);
747 device = btrfs_alloc_device(NULL, &devid,
748 disk_super->dev_item.uuid);
749 if (IS_ERR(device)) {
750 /* we can safely leave the fs_devices entry around */
754 name = rcu_string_strdup(path, GFP_NOFS);
757 return ERR_PTR(-ENOMEM);
759 rcu_assign_pointer(device->name, name);
761 mutex_lock(&fs_devices->device_list_mutex);
762 list_add_rcu(&device->dev_list, &fs_devices->devices);
763 fs_devices->num_devices++;
764 mutex_unlock(&fs_devices->device_list_mutex);
766 device->fs_devices = fs_devices;
767 btrfs_free_stale_devices(path, device);
769 if (disk_super->label[0])
770 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
771 disk_super->label, devid, found_transid, path);
773 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
774 disk_super->fsid, devid, found_transid, path);
776 } else if (!device->name || strcmp(device->name->str, path)) {
778 * When FS is already mounted.
779 * 1. If you are here and if the device->name is NULL that
780 * means this device was missing at time of FS mount.
781 * 2. If you are here and if the device->name is different
782 * from 'path' that means either
783 * a. The same device disappeared and reappeared with
785 * b. The missing-disk-which-was-replaced, has
788 * We must allow 1 and 2a above. But 2b would be a spurious
791 * Further in case of 1 and 2a above, the disk at 'path'
792 * would have missed some transaction when it was away and
793 * in case of 2a the stale bdev has to be updated as well.
794 * 2b must not be allowed at all time.
798 * For now, we do allow update to btrfs_fs_device through the
799 * btrfs dev scan cli after FS has been mounted. We're still
800 * tracking a problem where systems fail mount by subvolume id
801 * when we reject replacement on a mounted FS.
803 if (!fs_devices->opened && found_transid < device->generation) {
805 * That is if the FS is _not_ mounted and if you
806 * are here, that means there is more than one
807 * disk with same uuid and devid.We keep the one
808 * with larger generation number or the last-in if
809 * generation are equal.
811 return ERR_PTR(-EEXIST);
814 name = rcu_string_strdup(path, GFP_NOFS);
816 return ERR_PTR(-ENOMEM);
817 rcu_string_free(device->name);
818 rcu_assign_pointer(device->name, name);
819 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
820 fs_devices->missing_devices--;
821 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
826 * Unmount does not free the btrfs_device struct but would zero
827 * generation along with most of the other members. So just update
828 * it back. We need it to pick the disk with largest generation
831 if (!fs_devices->opened)
832 device->generation = found_transid;
834 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
839 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
841 struct btrfs_fs_devices *fs_devices;
842 struct btrfs_device *device;
843 struct btrfs_device *orig_dev;
845 fs_devices = alloc_fs_devices(orig->fsid);
846 if (IS_ERR(fs_devices))
849 mutex_lock(&orig->device_list_mutex);
850 fs_devices->total_devices = orig->total_devices;
852 /* We have held the volume lock, it is safe to get the devices. */
853 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
854 struct rcu_string *name;
856 device = btrfs_alloc_device(NULL, &orig_dev->devid,
862 * This is ok to do without rcu read locked because we hold the
863 * uuid mutex so nothing we touch in here is going to disappear.
865 if (orig_dev->name) {
866 name = rcu_string_strdup(orig_dev->name->str,
872 rcu_assign_pointer(device->name, name);
875 list_add(&device->dev_list, &fs_devices->devices);
876 device->fs_devices = fs_devices;
877 fs_devices->num_devices++;
879 mutex_unlock(&orig->device_list_mutex);
882 mutex_unlock(&orig->device_list_mutex);
883 free_fs_devices(fs_devices);
884 return ERR_PTR(-ENOMEM);
888 * After we have read the system tree and know devids belonging to
889 * this filesystem, remove the device which does not belong there.
891 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
893 struct btrfs_device *device, *next;
894 struct btrfs_device *latest_dev = NULL;
896 mutex_lock(&uuid_mutex);
898 /* This is the initialized path, it is safe to release the devices. */
899 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
900 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
901 &device->dev_state)) {
902 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
903 &device->dev_state) &&
905 device->generation > latest_dev->generation)) {
911 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
913 * In the first step, keep the device which has
914 * the correct fsid and the devid that is used
915 * for the dev_replace procedure.
916 * In the second step, the dev_replace state is
917 * read from the device tree and it is known
918 * whether the procedure is really active or
919 * not, which means whether this device is
920 * used or whether it should be removed.
922 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
923 &device->dev_state)) {
928 blkdev_put(device->bdev, device->mode);
930 fs_devices->open_devices--;
932 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
933 list_del_init(&device->dev_alloc_list);
934 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
935 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
937 fs_devices->rw_devices--;
939 list_del_init(&device->dev_list);
940 fs_devices->num_devices--;
944 if (fs_devices->seed) {
945 fs_devices = fs_devices->seed;
949 fs_devices->latest_bdev = latest_dev->bdev;
951 mutex_unlock(&uuid_mutex);
954 static void free_device_rcu(struct rcu_head *head)
956 struct btrfs_device *device;
958 device = container_of(head, struct btrfs_device, rcu);
962 static void btrfs_close_bdev(struct btrfs_device *device)
967 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
968 sync_blockdev(device->bdev);
969 invalidate_bdev(device->bdev);
972 blkdev_put(device->bdev, device->mode);
975 static void btrfs_prepare_close_one_device(struct btrfs_device *device)
977 struct btrfs_fs_devices *fs_devices = device->fs_devices;
978 struct btrfs_device *new_device;
979 struct rcu_string *name;
982 fs_devices->open_devices--;
984 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
985 device->devid != BTRFS_DEV_REPLACE_DEVID) {
986 list_del_init(&device->dev_alloc_list);
987 fs_devices->rw_devices--;
990 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
991 fs_devices->missing_devices--;
993 new_device = btrfs_alloc_device(NULL, &device->devid,
995 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
997 /* Safe because we are under uuid_mutex */
999 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1000 BUG_ON(!name); /* -ENOMEM */
1001 rcu_assign_pointer(new_device->name, name);
1004 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1005 new_device->fs_devices = device->fs_devices;
1008 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1010 struct btrfs_device *device, *tmp;
1011 struct list_head pending_put;
1013 INIT_LIST_HEAD(&pending_put);
1015 if (--fs_devices->opened > 0)
1018 mutex_lock(&fs_devices->device_list_mutex);
1019 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
1020 btrfs_prepare_close_one_device(device);
1021 list_add(&device->dev_list, &pending_put);
1023 mutex_unlock(&fs_devices->device_list_mutex);
1026 * btrfs_show_devname() is using the device_list_mutex,
1027 * sometimes call to blkdev_put() leads vfs calling
1028 * into this func. So do put outside of device_list_mutex,
1031 while (!list_empty(&pending_put)) {
1032 device = list_first_entry(&pending_put,
1033 struct btrfs_device, dev_list);
1034 list_del(&device->dev_list);
1035 btrfs_close_bdev(device);
1036 call_rcu(&device->rcu, free_device_rcu);
1039 WARN_ON(fs_devices->open_devices);
1040 WARN_ON(fs_devices->rw_devices);
1041 fs_devices->opened = 0;
1042 fs_devices->seeding = 0;
1047 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1049 struct btrfs_fs_devices *seed_devices = NULL;
1052 mutex_lock(&uuid_mutex);
1053 ret = __btrfs_close_devices(fs_devices);
1054 if (!fs_devices->opened) {
1055 seed_devices = fs_devices->seed;
1056 fs_devices->seed = NULL;
1058 mutex_unlock(&uuid_mutex);
1060 while (seed_devices) {
1061 fs_devices = seed_devices;
1062 seed_devices = fs_devices->seed;
1063 __btrfs_close_devices(fs_devices);
1064 free_fs_devices(fs_devices);
1069 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1070 fmode_t flags, void *holder)
1072 struct list_head *head = &fs_devices->devices;
1073 struct btrfs_device *device;
1074 struct btrfs_device *latest_dev = NULL;
1077 flags |= FMODE_EXCL;
1079 list_for_each_entry(device, head, dev_list) {
1080 /* Just open everything we can; ignore failures here */
1081 if (btrfs_open_one_device(fs_devices, device, flags, holder))
1085 device->generation > latest_dev->generation)
1086 latest_dev = device;
1088 if (fs_devices->open_devices == 0) {
1092 fs_devices->opened = 1;
1093 fs_devices->latest_bdev = latest_dev->bdev;
1094 fs_devices->total_rw_bytes = 0;
1099 static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1101 struct btrfs_device *dev1, *dev2;
1103 dev1 = list_entry(a, struct btrfs_device, dev_list);
1104 dev2 = list_entry(b, struct btrfs_device, dev_list);
1106 if (dev1->devid < dev2->devid)
1108 else if (dev1->devid > dev2->devid)
1113 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1114 fmode_t flags, void *holder)
1118 mutex_lock(&uuid_mutex);
1119 if (fs_devices->opened) {
1120 fs_devices->opened++;
1123 list_sort(NULL, &fs_devices->devices, devid_cmp);
1124 ret = __btrfs_open_devices(fs_devices, flags, holder);
1126 mutex_unlock(&uuid_mutex);
1130 static void btrfs_release_disk_super(struct page *page)
1136 static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1138 struct btrfs_super_block **disk_super)
1143 /* make sure our super fits in the device */
1144 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1147 /* make sure our super fits in the page */
1148 if (sizeof(**disk_super) > PAGE_SIZE)
1151 /* make sure our super doesn't straddle pages on disk */
1152 index = bytenr >> PAGE_SHIFT;
1153 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1156 /* pull in the page with our super */
1157 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1160 if (IS_ERR_OR_NULL(*page))
1165 /* align our pointer to the offset of the super block */
1166 *disk_super = p + (bytenr & ~PAGE_MASK);
1168 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1169 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1170 btrfs_release_disk_super(*page);
1174 if ((*disk_super)->label[0] &&
1175 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1176 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1182 * Look for a btrfs signature on a device. This may be called out of the mount path
1183 * and we are not allowed to call set_blocksize during the scan. The superblock
1184 * is read via pagecache
1186 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
1187 struct btrfs_fs_devices **fs_devices_ret)
1189 struct btrfs_super_block *disk_super;
1190 struct btrfs_device *device;
1191 struct block_device *bdev;
1197 * we would like to check all the supers, but that would make
1198 * a btrfs mount succeed after a mkfs from a different FS.
1199 * So, we need to add a special mount option to scan for
1200 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1202 bytenr = btrfs_sb_offset(0);
1203 flags |= FMODE_EXCL;
1204 mutex_lock(&uuid_mutex);
1206 bdev = blkdev_get_by_path(path, flags, holder);
1208 ret = PTR_ERR(bdev);
1212 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1214 goto error_bdev_put;
1217 device = device_list_add(path, disk_super);
1219 ret = PTR_ERR(device);
1221 *fs_devices_ret = device->fs_devices;
1223 btrfs_release_disk_super(page);
1226 blkdev_put(bdev, flags);
1228 mutex_unlock(&uuid_mutex);
1232 /* helper to account the used device space in the range */
1233 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1234 u64 end, u64 *length)
1236 struct btrfs_key key;
1237 struct btrfs_root *root = device->fs_info->dev_root;
1238 struct btrfs_dev_extent *dev_extent;
1239 struct btrfs_path *path;
1243 struct extent_buffer *l;
1247 if (start >= device->total_bytes ||
1248 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
1251 path = btrfs_alloc_path();
1254 path->reada = READA_FORWARD;
1256 key.objectid = device->devid;
1258 key.type = BTRFS_DEV_EXTENT_KEY;
1260 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1264 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1271 slot = path->slots[0];
1272 if (slot >= btrfs_header_nritems(l)) {
1273 ret = btrfs_next_leaf(root, path);
1281 btrfs_item_key_to_cpu(l, &key, slot);
1283 if (key.objectid < device->devid)
1286 if (key.objectid > device->devid)
1289 if (key.type != BTRFS_DEV_EXTENT_KEY)
1292 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1293 extent_end = key.offset + btrfs_dev_extent_length(l,
1295 if (key.offset <= start && extent_end > end) {
1296 *length = end - start + 1;
1298 } else if (key.offset <= start && extent_end > start)
1299 *length += extent_end - start;
1300 else if (key.offset > start && extent_end <= end)
1301 *length += extent_end - key.offset;
1302 else if (key.offset > start && key.offset <= end) {
1303 *length += end - key.offset + 1;
1305 } else if (key.offset > end)
1313 btrfs_free_path(path);
1317 static int contains_pending_extent(struct btrfs_transaction *transaction,
1318 struct btrfs_device *device,
1319 u64 *start, u64 len)
1321 struct btrfs_fs_info *fs_info = device->fs_info;
1322 struct extent_map *em;
1323 struct list_head *search_list = &fs_info->pinned_chunks;
1325 u64 physical_start = *start;
1328 search_list = &transaction->pending_chunks;
1330 list_for_each_entry(em, search_list, list) {
1331 struct map_lookup *map;
1334 map = em->map_lookup;
1335 for (i = 0; i < map->num_stripes; i++) {
1338 if (map->stripes[i].dev != device)
1340 if (map->stripes[i].physical >= physical_start + len ||
1341 map->stripes[i].physical + em->orig_block_len <=
1345 * Make sure that while processing the pinned list we do
1346 * not override our *start with a lower value, because
1347 * we can have pinned chunks that fall within this
1348 * device hole and that have lower physical addresses
1349 * than the pending chunks we processed before. If we
1350 * do not take this special care we can end up getting
1351 * 2 pending chunks that start at the same physical
1352 * device offsets because the end offset of a pinned
1353 * chunk can be equal to the start offset of some
1356 end = map->stripes[i].physical + em->orig_block_len;
1363 if (search_list != &fs_info->pinned_chunks) {
1364 search_list = &fs_info->pinned_chunks;
1373 * find_free_dev_extent_start - find free space in the specified device
1374 * @device: the device which we search the free space in
1375 * @num_bytes: the size of the free space that we need
1376 * @search_start: the position from which to begin the search
1377 * @start: store the start of the free space.
1378 * @len: the size of the free space. that we find, or the size
1379 * of the max free space if we don't find suitable free space
1381 * this uses a pretty simple search, the expectation is that it is
1382 * called very infrequently and that a given device has a small number
1385 * @start is used to store the start of the free space if we find. But if we
1386 * don't find suitable free space, it will be used to store the start position
1387 * of the max free space.
1389 * @len is used to store the size of the free space that we find.
1390 * But if we don't find suitable free space, it is used to store the size of
1391 * the max free space.
1393 int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1394 struct btrfs_device *device, u64 num_bytes,
1395 u64 search_start, u64 *start, u64 *len)
1397 struct btrfs_fs_info *fs_info = device->fs_info;
1398 struct btrfs_root *root = fs_info->dev_root;
1399 struct btrfs_key key;
1400 struct btrfs_dev_extent *dev_extent;
1401 struct btrfs_path *path;
1406 u64 search_end = device->total_bytes;
1409 struct extent_buffer *l;
1412 * We don't want to overwrite the superblock on the drive nor any area
1413 * used by the boot loader (grub for example), so we make sure to start
1414 * at an offset of at least 1MB.
1416 search_start = max_t(u64, search_start, SZ_1M);
1418 path = btrfs_alloc_path();
1422 max_hole_start = search_start;
1426 if (search_start >= search_end ||
1427 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1432 path->reada = READA_FORWARD;
1433 path->search_commit_root = 1;
1434 path->skip_locking = 1;
1436 key.objectid = device->devid;
1437 key.offset = search_start;
1438 key.type = BTRFS_DEV_EXTENT_KEY;
1440 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1444 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1451 slot = path->slots[0];
1452 if (slot >= btrfs_header_nritems(l)) {
1453 ret = btrfs_next_leaf(root, path);
1461 btrfs_item_key_to_cpu(l, &key, slot);
1463 if (key.objectid < device->devid)
1466 if (key.objectid > device->devid)
1469 if (key.type != BTRFS_DEV_EXTENT_KEY)
1472 if (key.offset > search_start) {
1473 hole_size = key.offset - search_start;
1476 * Have to check before we set max_hole_start, otherwise
1477 * we could end up sending back this offset anyway.
1479 if (contains_pending_extent(transaction, device,
1482 if (key.offset >= search_start) {
1483 hole_size = key.offset - search_start;
1490 if (hole_size > max_hole_size) {
1491 max_hole_start = search_start;
1492 max_hole_size = hole_size;
1496 * If this free space is greater than which we need,
1497 * it must be the max free space that we have found
1498 * until now, so max_hole_start must point to the start
1499 * of this free space and the length of this free space
1500 * is stored in max_hole_size. Thus, we return
1501 * max_hole_start and max_hole_size and go back to the
1504 if (hole_size >= num_bytes) {
1510 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1511 extent_end = key.offset + btrfs_dev_extent_length(l,
1513 if (extent_end > search_start)
1514 search_start = extent_end;
1521 * At this point, search_start should be the end of
1522 * allocated dev extents, and when shrinking the device,
1523 * search_end may be smaller than search_start.
1525 if (search_end > search_start) {
1526 hole_size = search_end - search_start;
1528 if (contains_pending_extent(transaction, device, &search_start,
1530 btrfs_release_path(path);
1534 if (hole_size > max_hole_size) {
1535 max_hole_start = search_start;
1536 max_hole_size = hole_size;
1541 if (max_hole_size < num_bytes)
1547 btrfs_free_path(path);
1548 *start = max_hole_start;
1550 *len = max_hole_size;
1554 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1555 struct btrfs_device *device, u64 num_bytes,
1556 u64 *start, u64 *len)
1558 /* FIXME use last free of some kind */
1559 return find_free_dev_extent_start(trans->transaction, device,
1560 num_bytes, 0, start, len);
1563 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1564 struct btrfs_device *device,
1565 u64 start, u64 *dev_extent_len)
1567 struct btrfs_fs_info *fs_info = device->fs_info;
1568 struct btrfs_root *root = fs_info->dev_root;
1570 struct btrfs_path *path;
1571 struct btrfs_key key;
1572 struct btrfs_key found_key;
1573 struct extent_buffer *leaf = NULL;
1574 struct btrfs_dev_extent *extent = NULL;
1576 path = btrfs_alloc_path();
1580 key.objectid = device->devid;
1582 key.type = BTRFS_DEV_EXTENT_KEY;
1584 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1586 ret = btrfs_previous_item(root, path, key.objectid,
1587 BTRFS_DEV_EXTENT_KEY);
1590 leaf = path->nodes[0];
1591 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1592 extent = btrfs_item_ptr(leaf, path->slots[0],
1593 struct btrfs_dev_extent);
1594 BUG_ON(found_key.offset > start || found_key.offset +
1595 btrfs_dev_extent_length(leaf, extent) < start);
1597 btrfs_release_path(path);
1599 } else if (ret == 0) {
1600 leaf = path->nodes[0];
1601 extent = btrfs_item_ptr(leaf, path->slots[0],
1602 struct btrfs_dev_extent);
1604 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
1608 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1610 ret = btrfs_del_item(trans, root, path);
1612 btrfs_handle_fs_error(fs_info, ret,
1613 "Failed to remove dev extent item");
1615 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
1618 btrfs_free_path(path);
1622 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1623 struct btrfs_device *device,
1624 u64 chunk_offset, u64 start, u64 num_bytes)
1627 struct btrfs_path *path;
1628 struct btrfs_fs_info *fs_info = device->fs_info;
1629 struct btrfs_root *root = fs_info->dev_root;
1630 struct btrfs_dev_extent *extent;
1631 struct extent_buffer *leaf;
1632 struct btrfs_key key;
1634 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
1635 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1636 path = btrfs_alloc_path();
1640 key.objectid = device->devid;
1642 key.type = BTRFS_DEV_EXTENT_KEY;
1643 ret = btrfs_insert_empty_item(trans, root, path, &key,
1648 leaf = path->nodes[0];
1649 extent = btrfs_item_ptr(leaf, path->slots[0],
1650 struct btrfs_dev_extent);
1651 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1652 BTRFS_CHUNK_TREE_OBJECTID);
1653 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1654 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1655 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1657 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1658 btrfs_mark_buffer_dirty(leaf);
1660 btrfs_free_path(path);
1664 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1666 struct extent_map_tree *em_tree;
1667 struct extent_map *em;
1671 em_tree = &fs_info->mapping_tree.map_tree;
1672 read_lock(&em_tree->lock);
1673 n = rb_last(&em_tree->map);
1675 em = rb_entry(n, struct extent_map, rb_node);
1676 ret = em->start + em->len;
1678 read_unlock(&em_tree->lock);
1683 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1687 struct btrfs_key key;
1688 struct btrfs_key found_key;
1689 struct btrfs_path *path;
1691 path = btrfs_alloc_path();
1695 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1696 key.type = BTRFS_DEV_ITEM_KEY;
1697 key.offset = (u64)-1;
1699 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1703 BUG_ON(ret == 0); /* Corruption */
1705 ret = btrfs_previous_item(fs_info->chunk_root, path,
1706 BTRFS_DEV_ITEMS_OBJECTID,
1707 BTRFS_DEV_ITEM_KEY);
1711 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1713 *devid_ret = found_key.offset + 1;
1717 btrfs_free_path(path);
1722 * the device information is stored in the chunk root
1723 * the btrfs_device struct should be fully filled in
1725 static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
1726 struct btrfs_fs_info *fs_info,
1727 struct btrfs_device *device)
1729 struct btrfs_root *root = fs_info->chunk_root;
1731 struct btrfs_path *path;
1732 struct btrfs_dev_item *dev_item;
1733 struct extent_buffer *leaf;
1734 struct btrfs_key key;
1737 path = btrfs_alloc_path();
1741 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1742 key.type = BTRFS_DEV_ITEM_KEY;
1743 key.offset = device->devid;
1745 ret = btrfs_insert_empty_item(trans, root, path, &key,
1750 leaf = path->nodes[0];
1751 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1753 btrfs_set_device_id(leaf, dev_item, device->devid);
1754 btrfs_set_device_generation(leaf, dev_item, 0);
1755 btrfs_set_device_type(leaf, dev_item, device->type);
1756 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1757 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1758 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1759 btrfs_set_device_total_bytes(leaf, dev_item,
1760 btrfs_device_get_disk_total_bytes(device));
1761 btrfs_set_device_bytes_used(leaf, dev_item,
1762 btrfs_device_get_bytes_used(device));
1763 btrfs_set_device_group(leaf, dev_item, 0);
1764 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1765 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1766 btrfs_set_device_start_offset(leaf, dev_item, 0);
1768 ptr = btrfs_device_uuid(dev_item);
1769 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1770 ptr = btrfs_device_fsid(dev_item);
1771 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
1772 btrfs_mark_buffer_dirty(leaf);
1776 btrfs_free_path(path);
1781 * Function to update ctime/mtime for a given device path.
1782 * Mainly used for ctime/mtime based probe like libblkid.
1784 static void update_dev_time(const char *path_name)
1788 filp = filp_open(path_name, O_RDWR, 0);
1791 file_update_time(filp);
1792 filp_close(filp, NULL);
1795 static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
1796 struct btrfs_device *device)
1798 struct btrfs_root *root = fs_info->chunk_root;
1800 struct btrfs_path *path;
1801 struct btrfs_key key;
1802 struct btrfs_trans_handle *trans;
1804 path = btrfs_alloc_path();
1808 trans = btrfs_start_transaction(root, 0);
1809 if (IS_ERR(trans)) {
1810 btrfs_free_path(path);
1811 return PTR_ERR(trans);
1813 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1814 key.type = BTRFS_DEV_ITEM_KEY;
1815 key.offset = device->devid;
1817 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1821 btrfs_abort_transaction(trans, ret);
1822 btrfs_end_transaction(trans);
1826 ret = btrfs_del_item(trans, root, path);
1828 btrfs_abort_transaction(trans, ret);
1829 btrfs_end_transaction(trans);
1833 btrfs_free_path(path);
1835 ret = btrfs_commit_transaction(trans);
1840 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1841 * filesystem. It's up to the caller to adjust that number regarding eg. device
1844 static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1852 seq = read_seqbegin(&fs_info->profiles_lock);
1854 all_avail = fs_info->avail_data_alloc_bits |
1855 fs_info->avail_system_alloc_bits |
1856 fs_info->avail_metadata_alloc_bits;
1857 } while (read_seqretry(&fs_info->profiles_lock, seq));
1859 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1860 if (!(all_avail & btrfs_raid_group[i]))
1863 if (num_devices < btrfs_raid_array[i].devs_min) {
1864 int ret = btrfs_raid_mindev_error[i];
1874 static struct btrfs_device * btrfs_find_next_active_device(
1875 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
1877 struct btrfs_device *next_device;
1879 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1880 if (next_device != device &&
1881 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1882 && next_device->bdev)
1890 * Helper function to check if the given device is part of s_bdev / latest_bdev
1891 * and replace it with the provided or the next active device, in the context
1892 * where this function called, there should be always be another device (or
1893 * this_dev) which is active.
1895 void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1896 struct btrfs_device *device, struct btrfs_device *this_dev)
1898 struct btrfs_device *next_device;
1901 next_device = this_dev;
1903 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1905 ASSERT(next_device);
1907 if (fs_info->sb->s_bdev &&
1908 (fs_info->sb->s_bdev == device->bdev))
1909 fs_info->sb->s_bdev = next_device->bdev;
1911 if (fs_info->fs_devices->latest_bdev == device->bdev)
1912 fs_info->fs_devices->latest_bdev = next_device->bdev;
1915 int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1918 struct btrfs_device *device;
1919 struct btrfs_fs_devices *cur_devices;
1923 mutex_lock(&fs_info->volume_mutex);
1924 mutex_lock(&uuid_mutex);
1926 num_devices = fs_info->fs_devices->num_devices;
1927 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
1928 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1929 WARN_ON(num_devices < 1);
1932 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
1934 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
1938 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1943 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1944 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1948 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1949 fs_info->fs_devices->rw_devices == 1) {
1950 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1954 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1955 mutex_lock(&fs_info->chunk_mutex);
1956 list_del_init(&device->dev_alloc_list);
1957 device->fs_devices->rw_devices--;
1958 mutex_unlock(&fs_info->chunk_mutex);
1961 mutex_unlock(&uuid_mutex);
1962 ret = btrfs_shrink_device(device, 0);
1963 mutex_lock(&uuid_mutex);
1968 * TODO: the superblock still includes this device in its num_devices
1969 * counter although write_all_supers() is not locked out. This
1970 * could give a filesystem state which requires a degraded mount.
1972 ret = btrfs_rm_dev_item(fs_info, device);
1976 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1977 btrfs_scrub_cancel_dev(fs_info, device);
1980 * the device list mutex makes sure that we don't change
1981 * the device list while someone else is writing out all
1982 * the device supers. Whoever is writing all supers, should
1983 * lock the device list mutex before getting the number of
1984 * devices in the super block (super_copy). Conversely,
1985 * whoever updates the number of devices in the super block
1986 * (super_copy) should hold the device list mutex.
1989 cur_devices = device->fs_devices;
1990 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1991 list_del_rcu(&device->dev_list);
1993 device->fs_devices->num_devices--;
1994 device->fs_devices->total_devices--;
1996 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
1997 device->fs_devices->missing_devices--;
1999 btrfs_assign_next_active_device(fs_info, device, NULL);
2002 device->fs_devices->open_devices--;
2003 /* remove sysfs entry */
2004 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
2007 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2008 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2009 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2012 * at this point, the device is zero sized and detached from
2013 * the devices list. All that's left is to zero out the old
2014 * supers and free the device.
2016 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2017 btrfs_scratch_superblocks(device->bdev, device->name->str);
2019 btrfs_close_bdev(device);
2020 call_rcu(&device->rcu, free_device_rcu);
2022 if (cur_devices->open_devices == 0) {
2023 struct btrfs_fs_devices *fs_devices;
2024 fs_devices = fs_info->fs_devices;
2025 while (fs_devices) {
2026 if (fs_devices->seed == cur_devices) {
2027 fs_devices->seed = cur_devices->seed;
2030 fs_devices = fs_devices->seed;
2032 cur_devices->seed = NULL;
2033 __btrfs_close_devices(cur_devices);
2034 free_fs_devices(cur_devices);
2038 mutex_unlock(&uuid_mutex);
2039 mutex_unlock(&fs_info->volume_mutex);
2043 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2044 mutex_lock(&fs_info->chunk_mutex);
2045 list_add(&device->dev_alloc_list,
2046 &fs_info->fs_devices->alloc_list);
2047 device->fs_devices->rw_devices++;
2048 mutex_unlock(&fs_info->chunk_mutex);
2053 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2054 struct btrfs_device *srcdev)
2056 struct btrfs_fs_devices *fs_devices;
2058 lockdep_assert_held(&fs_info->fs_devices->device_list_mutex);
2061 * in case of fs with no seed, srcdev->fs_devices will point
2062 * to fs_devices of fs_info. However when the dev being replaced is
2063 * a seed dev it will point to the seed's local fs_devices. In short
2064 * srcdev will have its correct fs_devices in both the cases.
2066 fs_devices = srcdev->fs_devices;
2068 list_del_rcu(&srcdev->dev_list);
2069 list_del(&srcdev->dev_alloc_list);
2070 fs_devices->num_devices--;
2071 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
2072 fs_devices->missing_devices--;
2074 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
2075 fs_devices->rw_devices--;
2078 fs_devices->open_devices--;
2081 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2082 struct btrfs_device *srcdev)
2084 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
2086 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
2087 /* zero out the old super if it is writable */
2088 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2091 btrfs_close_bdev(srcdev);
2092 call_rcu(&srcdev->rcu, free_device_rcu);
2094 /* if this is no devs we rather delete the fs_devices */
2095 if (!fs_devices->num_devices) {
2096 struct btrfs_fs_devices *tmp_fs_devices;
2099 * On a mounted FS, num_devices can't be zero unless it's a
2100 * seed. In case of a seed device being replaced, the replace
2101 * target added to the sprout FS, so there will be no more
2102 * device left under the seed FS.
2104 ASSERT(fs_devices->seeding);
2106 tmp_fs_devices = fs_info->fs_devices;
2107 while (tmp_fs_devices) {
2108 if (tmp_fs_devices->seed == fs_devices) {
2109 tmp_fs_devices->seed = fs_devices->seed;
2112 tmp_fs_devices = tmp_fs_devices->seed;
2114 fs_devices->seed = NULL;
2115 __btrfs_close_devices(fs_devices);
2116 free_fs_devices(fs_devices);
2120 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2121 struct btrfs_device *tgtdev)
2123 mutex_lock(&uuid_mutex);
2125 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2127 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
2130 fs_info->fs_devices->open_devices--;
2132 fs_info->fs_devices->num_devices--;
2134 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
2136 list_del_rcu(&tgtdev->dev_list);
2138 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2139 mutex_unlock(&uuid_mutex);
2142 * The update_dev_time() with in btrfs_scratch_superblocks()
2143 * may lead to a call to btrfs_show_devname() which will try
2144 * to hold device_list_mutex. And here this device
2145 * is already out of device list, so we don't have to hold
2146 * the device_list_mutex lock.
2148 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
2150 btrfs_close_bdev(tgtdev);
2151 call_rcu(&tgtdev->rcu, free_device_rcu);
2154 static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
2155 const char *device_path,
2156 struct btrfs_device **device)
2159 struct btrfs_super_block *disk_super;
2162 struct block_device *bdev;
2163 struct buffer_head *bh;
2166 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2167 fs_info->bdev_holder, 0, &bdev, &bh);
2170 disk_super = (struct btrfs_super_block *)bh->b_data;
2171 devid = btrfs_stack_device_id(&disk_super->dev_item);
2172 dev_uuid = disk_super->dev_item.uuid;
2173 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
2177 blkdev_put(bdev, FMODE_READ);
2181 int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
2182 const char *device_path,
2183 struct btrfs_device **device)
2186 if (strcmp(device_path, "missing") == 0) {
2187 struct list_head *devices;
2188 struct btrfs_device *tmp;
2190 devices = &fs_info->fs_devices->devices;
2192 * It is safe to read the devices since the volume_mutex
2193 * is held by the caller.
2195 list_for_each_entry(tmp, devices, dev_list) {
2196 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2197 &tmp->dev_state) && !tmp->bdev) {
2204 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2208 return btrfs_find_device_by_path(fs_info, device_path, device);
2213 * Lookup a device given by device id, or the path if the id is 0.
2215 int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
2216 const char *devpath,
2217 struct btrfs_device **device)
2223 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
2227 if (!devpath || !devpath[0])
2230 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
2237 * does all the dirty work required for changing file system's UUID.
2239 static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
2241 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2242 struct btrfs_fs_devices *old_devices;
2243 struct btrfs_fs_devices *seed_devices;
2244 struct btrfs_super_block *disk_super = fs_info->super_copy;
2245 struct btrfs_device *device;
2248 lockdep_assert_held(&uuid_mutex);
2249 if (!fs_devices->seeding)
2252 seed_devices = alloc_fs_devices(NULL);
2253 if (IS_ERR(seed_devices))
2254 return PTR_ERR(seed_devices);
2256 old_devices = clone_fs_devices(fs_devices);
2257 if (IS_ERR(old_devices)) {
2258 kfree(seed_devices);
2259 return PTR_ERR(old_devices);
2262 list_add(&old_devices->list, &fs_uuids);
2264 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2265 seed_devices->opened = 1;
2266 INIT_LIST_HEAD(&seed_devices->devices);
2267 INIT_LIST_HEAD(&seed_devices->alloc_list);
2268 mutex_init(&seed_devices->device_list_mutex);
2270 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2271 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2273 list_for_each_entry(device, &seed_devices->devices, dev_list)
2274 device->fs_devices = seed_devices;
2276 mutex_lock(&fs_info->chunk_mutex);
2277 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2278 mutex_unlock(&fs_info->chunk_mutex);
2280 fs_devices->seeding = 0;
2281 fs_devices->num_devices = 0;
2282 fs_devices->open_devices = 0;
2283 fs_devices->missing_devices = 0;
2284 fs_devices->rotating = 0;
2285 fs_devices->seed = seed_devices;
2287 generate_random_uuid(fs_devices->fsid);
2288 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2289 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2290 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2292 super_flags = btrfs_super_flags(disk_super) &
2293 ~BTRFS_SUPER_FLAG_SEEDING;
2294 btrfs_set_super_flags(disk_super, super_flags);
2300 * Store the expected generation for seed devices in device items.
2302 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2303 struct btrfs_fs_info *fs_info)
2305 struct btrfs_root *root = fs_info->chunk_root;
2306 struct btrfs_path *path;
2307 struct extent_buffer *leaf;
2308 struct btrfs_dev_item *dev_item;
2309 struct btrfs_device *device;
2310 struct btrfs_key key;
2311 u8 fs_uuid[BTRFS_FSID_SIZE];
2312 u8 dev_uuid[BTRFS_UUID_SIZE];
2316 path = btrfs_alloc_path();
2320 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2322 key.type = BTRFS_DEV_ITEM_KEY;
2325 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2329 leaf = path->nodes[0];
2331 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2332 ret = btrfs_next_leaf(root, path);
2337 leaf = path->nodes[0];
2338 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2339 btrfs_release_path(path);
2343 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2344 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2345 key.type != BTRFS_DEV_ITEM_KEY)
2348 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2349 struct btrfs_dev_item);
2350 devid = btrfs_device_id(leaf, dev_item);
2351 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2353 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2355 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
2356 BUG_ON(!device); /* Logic error */
2358 if (device->fs_devices->seeding) {
2359 btrfs_set_device_generation(leaf, dev_item,
2360 device->generation);
2361 btrfs_mark_buffer_dirty(leaf);
2369 btrfs_free_path(path);
2373 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
2375 struct btrfs_root *root = fs_info->dev_root;
2376 struct request_queue *q;
2377 struct btrfs_trans_handle *trans;
2378 struct btrfs_device *device;
2379 struct block_device *bdev;
2380 struct list_head *devices;
2381 struct super_block *sb = fs_info->sb;
2382 struct rcu_string *name;
2384 int seeding_dev = 0;
2386 bool unlocked = false;
2388 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
2391 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2392 fs_info->bdev_holder);
2394 return PTR_ERR(bdev);
2396 if (fs_info->fs_devices->seeding) {
2398 down_write(&sb->s_umount);
2399 mutex_lock(&uuid_mutex);
2402 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2404 devices = &fs_info->fs_devices->devices;
2406 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2407 list_for_each_entry(device, devices, dev_list) {
2408 if (device->bdev == bdev) {
2411 &fs_info->fs_devices->device_list_mutex);
2415 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2417 device = btrfs_alloc_device(fs_info, NULL, NULL);
2418 if (IS_ERR(device)) {
2419 /* we can safely leave the fs_devices entry around */
2420 ret = PTR_ERR(device);
2424 name = rcu_string_strdup(device_path, GFP_KERNEL);
2427 goto error_free_device;
2429 rcu_assign_pointer(device->name, name);
2431 trans = btrfs_start_transaction(root, 0);
2432 if (IS_ERR(trans)) {
2433 ret = PTR_ERR(trans);
2434 goto error_free_device;
2437 q = bdev_get_queue(bdev);
2438 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2439 device->generation = trans->transid;
2440 device->io_width = fs_info->sectorsize;
2441 device->io_align = fs_info->sectorsize;
2442 device->sector_size = fs_info->sectorsize;
2443 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2444 fs_info->sectorsize);
2445 device->disk_total_bytes = device->total_bytes;
2446 device->commit_total_bytes = device->total_bytes;
2447 device->fs_info = fs_info;
2448 device->bdev = bdev;
2449 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2450 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2451 device->mode = FMODE_EXCL;
2452 device->dev_stats_valid = 1;
2453 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2456 sb->s_flags &= ~SB_RDONLY;
2457 ret = btrfs_prepare_sprout(fs_info);
2459 btrfs_abort_transaction(trans, ret);
2464 device->fs_devices = fs_info->fs_devices;
2466 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2467 mutex_lock(&fs_info->chunk_mutex);
2468 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
2469 list_add(&device->dev_alloc_list,
2470 &fs_info->fs_devices->alloc_list);
2471 fs_info->fs_devices->num_devices++;
2472 fs_info->fs_devices->open_devices++;
2473 fs_info->fs_devices->rw_devices++;
2474 fs_info->fs_devices->total_devices++;
2475 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2477 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
2479 if (!blk_queue_nonrot(q))
2480 fs_info->fs_devices->rotating = 1;
2482 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2483 btrfs_set_super_total_bytes(fs_info->super_copy,
2484 round_down(tmp + device->total_bytes, fs_info->sectorsize));
2486 tmp = btrfs_super_num_devices(fs_info->super_copy);
2487 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
2489 /* add sysfs device entry */
2490 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
2493 * we've got more storage, clear any full flags on the space
2496 btrfs_clear_space_info_full(fs_info);
2498 mutex_unlock(&fs_info->chunk_mutex);
2499 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2502 mutex_lock(&fs_info->chunk_mutex);
2503 ret = init_first_rw_device(trans, fs_info);
2504 mutex_unlock(&fs_info->chunk_mutex);
2506 btrfs_abort_transaction(trans, ret);
2511 ret = btrfs_add_dev_item(trans, fs_info, device);
2513 btrfs_abort_transaction(trans, ret);
2518 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2520 ret = btrfs_finish_sprout(trans, fs_info);
2522 btrfs_abort_transaction(trans, ret);
2526 /* Sprouting would change fsid of the mounted root,
2527 * so rename the fsid on the sysfs
2529 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2531 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2533 "sysfs: failed to create fsid for sprout");
2536 ret = btrfs_commit_transaction(trans);
2539 mutex_unlock(&uuid_mutex);
2540 up_write(&sb->s_umount);
2543 if (ret) /* transaction commit */
2546 ret = btrfs_relocate_sys_chunks(fs_info);
2548 btrfs_handle_fs_error(fs_info, ret,
2549 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2550 trans = btrfs_attach_transaction(root);
2551 if (IS_ERR(trans)) {
2552 if (PTR_ERR(trans) == -ENOENT)
2554 ret = PTR_ERR(trans);
2558 ret = btrfs_commit_transaction(trans);
2561 /* Update ctime/mtime for libblkid */
2562 update_dev_time(device_path);
2566 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
2569 sb->s_flags |= SB_RDONLY;
2571 btrfs_end_transaction(trans);
2573 free_device(device);
2575 blkdev_put(bdev, FMODE_EXCL);
2576 if (seeding_dev && !unlocked) {
2577 mutex_unlock(&uuid_mutex);
2578 up_write(&sb->s_umount);
2583 int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2584 const char *device_path,
2585 struct btrfs_device *srcdev,
2586 struct btrfs_device **device_out)
2588 struct btrfs_device *device;
2589 struct block_device *bdev;
2590 struct list_head *devices;
2591 struct rcu_string *name;
2592 u64 devid = BTRFS_DEV_REPLACE_DEVID;
2596 if (fs_info->fs_devices->seeding) {
2597 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
2601 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2602 fs_info->bdev_holder);
2604 btrfs_err(fs_info, "target device %s is invalid!", device_path);
2605 return PTR_ERR(bdev);
2608 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2610 devices = &fs_info->fs_devices->devices;
2611 list_for_each_entry(device, devices, dev_list) {
2612 if (device->bdev == bdev) {
2614 "target device is in the filesystem!");
2621 if (i_size_read(bdev->bd_inode) <
2622 btrfs_device_get_total_bytes(srcdev)) {
2624 "target device is smaller than source device!");
2630 device = btrfs_alloc_device(NULL, &devid, NULL);
2631 if (IS_ERR(device)) {
2632 ret = PTR_ERR(device);
2636 name = rcu_string_strdup(device_path, GFP_KERNEL);
2638 free_device(device);
2642 rcu_assign_pointer(device->name, name);
2644 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2645 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2646 device->generation = 0;
2647 device->io_width = fs_info->sectorsize;
2648 device->io_align = fs_info->sectorsize;
2649 device->sector_size = fs_info->sectorsize;
2650 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2651 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2652 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
2653 device->commit_total_bytes = srcdev->commit_total_bytes;
2654 device->commit_bytes_used = device->bytes_used;
2655 device->fs_info = fs_info;
2656 device->bdev = bdev;
2657 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2658 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2659 device->mode = FMODE_EXCL;
2660 device->dev_stats_valid = 1;
2661 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2662 device->fs_devices = fs_info->fs_devices;
2663 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2664 fs_info->fs_devices->num_devices++;
2665 fs_info->fs_devices->open_devices++;
2666 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2668 *device_out = device;
2672 blkdev_put(bdev, FMODE_EXCL);
2676 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2677 struct btrfs_device *device)
2680 struct btrfs_path *path;
2681 struct btrfs_root *root = device->fs_info->chunk_root;
2682 struct btrfs_dev_item *dev_item;
2683 struct extent_buffer *leaf;
2684 struct btrfs_key key;
2686 path = btrfs_alloc_path();
2690 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2691 key.type = BTRFS_DEV_ITEM_KEY;
2692 key.offset = device->devid;
2694 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2703 leaf = path->nodes[0];
2704 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2706 btrfs_set_device_id(leaf, dev_item, device->devid);
2707 btrfs_set_device_type(leaf, dev_item, device->type);
2708 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2709 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2710 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2711 btrfs_set_device_total_bytes(leaf, dev_item,
2712 btrfs_device_get_disk_total_bytes(device));
2713 btrfs_set_device_bytes_used(leaf, dev_item,
2714 btrfs_device_get_bytes_used(device));
2715 btrfs_mark_buffer_dirty(leaf);
2718 btrfs_free_path(path);
2722 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2723 struct btrfs_device *device, u64 new_size)
2725 struct btrfs_fs_info *fs_info = device->fs_info;
2726 struct btrfs_super_block *super_copy = fs_info->super_copy;
2727 struct btrfs_fs_devices *fs_devices;
2731 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2734 new_size = round_down(new_size, fs_info->sectorsize);
2736 mutex_lock(&fs_info->chunk_mutex);
2737 old_total = btrfs_super_total_bytes(super_copy);
2738 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
2740 if (new_size <= device->total_bytes ||
2741 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2742 mutex_unlock(&fs_info->chunk_mutex);
2746 fs_devices = fs_info->fs_devices;
2748 btrfs_set_super_total_bytes(super_copy,
2749 round_down(old_total + diff, fs_info->sectorsize));
2750 device->fs_devices->total_rw_bytes += diff;
2752 btrfs_device_set_total_bytes(device, new_size);
2753 btrfs_device_set_disk_total_bytes(device, new_size);
2754 btrfs_clear_space_info_full(device->fs_info);
2755 if (list_empty(&device->resized_list))
2756 list_add_tail(&device->resized_list,
2757 &fs_devices->resized_devices);
2758 mutex_unlock(&fs_info->chunk_mutex);
2760 return btrfs_update_device(trans, device);
2763 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2764 struct btrfs_fs_info *fs_info, u64 chunk_offset)
2766 struct btrfs_root *root = fs_info->chunk_root;
2768 struct btrfs_path *path;
2769 struct btrfs_key key;
2771 path = btrfs_alloc_path();
2775 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2776 key.offset = chunk_offset;
2777 key.type = BTRFS_CHUNK_ITEM_KEY;
2779 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2782 else if (ret > 0) { /* Logic error or corruption */
2783 btrfs_handle_fs_error(fs_info, -ENOENT,
2784 "Failed lookup while freeing chunk.");
2789 ret = btrfs_del_item(trans, root, path);
2791 btrfs_handle_fs_error(fs_info, ret,
2792 "Failed to delete chunk item.");
2794 btrfs_free_path(path);
2798 static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2800 struct btrfs_super_block *super_copy = fs_info->super_copy;
2801 struct btrfs_disk_key *disk_key;
2802 struct btrfs_chunk *chunk;
2809 struct btrfs_key key;
2811 mutex_lock(&fs_info->chunk_mutex);
2812 array_size = btrfs_super_sys_array_size(super_copy);
2814 ptr = super_copy->sys_chunk_array;
2817 while (cur < array_size) {
2818 disk_key = (struct btrfs_disk_key *)ptr;
2819 btrfs_disk_key_to_cpu(&key, disk_key);
2821 len = sizeof(*disk_key);
2823 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2824 chunk = (struct btrfs_chunk *)(ptr + len);
2825 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2826 len += btrfs_chunk_item_size(num_stripes);
2831 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
2832 key.offset == chunk_offset) {
2833 memmove(ptr, ptr + len, array_size - (cur + len));
2835 btrfs_set_super_sys_array_size(super_copy, array_size);
2841 mutex_unlock(&fs_info->chunk_mutex);
2845 static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2846 u64 logical, u64 length)
2848 struct extent_map_tree *em_tree;
2849 struct extent_map *em;
2851 em_tree = &fs_info->mapping_tree.map_tree;
2852 read_lock(&em_tree->lock);
2853 em = lookup_extent_mapping(em_tree, logical, length);
2854 read_unlock(&em_tree->lock);
2857 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2859 return ERR_PTR(-EINVAL);
2862 if (em->start > logical || em->start + em->len < logical) {
2864 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2865 logical, length, em->start, em->start + em->len);
2866 free_extent_map(em);
2867 return ERR_PTR(-EINVAL);
2870 /* callers are responsible for dropping em's ref. */
2874 int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2875 struct btrfs_fs_info *fs_info, u64 chunk_offset)
2877 struct extent_map *em;
2878 struct map_lookup *map;
2879 u64 dev_extent_len = 0;
2881 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2883 em = get_chunk_map(fs_info, chunk_offset, 1);
2886 * This is a logic error, but we don't want to just rely on the
2887 * user having built with ASSERT enabled, so if ASSERT doesn't
2888 * do anything we still error out.
2893 map = em->map_lookup;
2894 mutex_lock(&fs_info->chunk_mutex);
2895 check_system_chunk(trans, fs_info, map->type);
2896 mutex_unlock(&fs_info->chunk_mutex);
2899 * Take the device list mutex to prevent races with the final phase of
2900 * a device replace operation that replaces the device object associated
2901 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2903 mutex_lock(&fs_devices->device_list_mutex);
2904 for (i = 0; i < map->num_stripes; i++) {
2905 struct btrfs_device *device = map->stripes[i].dev;
2906 ret = btrfs_free_dev_extent(trans, device,
2907 map->stripes[i].physical,
2910 mutex_unlock(&fs_devices->device_list_mutex);
2911 btrfs_abort_transaction(trans, ret);
2915 if (device->bytes_used > 0) {
2916 mutex_lock(&fs_info->chunk_mutex);
2917 btrfs_device_set_bytes_used(device,
2918 device->bytes_used - dev_extent_len);
2919 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
2920 btrfs_clear_space_info_full(fs_info);
2921 mutex_unlock(&fs_info->chunk_mutex);
2924 if (map->stripes[i].dev) {
2925 ret = btrfs_update_device(trans, map->stripes[i].dev);
2927 mutex_unlock(&fs_devices->device_list_mutex);
2928 btrfs_abort_transaction(trans, ret);
2933 mutex_unlock(&fs_devices->device_list_mutex);
2935 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
2937 btrfs_abort_transaction(trans, ret);
2941 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
2943 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2944 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
2946 btrfs_abort_transaction(trans, ret);
2951 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
2953 btrfs_abort_transaction(trans, ret);
2959 free_extent_map(em);
2963 static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2965 struct btrfs_root *root = fs_info->chunk_root;
2966 struct btrfs_trans_handle *trans;
2970 * Prevent races with automatic removal of unused block groups.
2971 * After we relocate and before we remove the chunk with offset
2972 * chunk_offset, automatic removal of the block group can kick in,
2973 * resulting in a failure when calling btrfs_remove_chunk() below.
2975 * Make sure to acquire this mutex before doing a tree search (dev
2976 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2977 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2978 * we release the path used to search the chunk/dev tree and before
2979 * the current task acquires this mutex and calls us.
2981 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
2983 ret = btrfs_can_relocate(fs_info, chunk_offset);
2987 /* step one, relocate all the extents inside this chunk */
2988 btrfs_scrub_pause(fs_info);
2989 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
2990 btrfs_scrub_continue(fs_info);
2995 * We add the kobjects here (and after forcing data chunk creation)
2996 * since relocation is the only place we'll create chunks of a new
2997 * type at runtime. The only place where we'll remove the last
2998 * chunk of a type is the call immediately below this one. Even
2999 * so, we're protected against races with the cleaner thread since
3000 * we're covered by the delete_unused_bgs_mutex.
3002 btrfs_add_raid_kobjects(fs_info);
3004 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3006 if (IS_ERR(trans)) {
3007 ret = PTR_ERR(trans);
3008 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3013 * step two, delete the device extents and the
3014 * chunk tree entries
3016 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
3017 btrfs_end_transaction(trans);
3021 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
3023 struct btrfs_root *chunk_root = fs_info->chunk_root;
3024 struct btrfs_path *path;
3025 struct extent_buffer *leaf;
3026 struct btrfs_chunk *chunk;
3027 struct btrfs_key key;
3028 struct btrfs_key found_key;
3030 bool retried = false;
3034 path = btrfs_alloc_path();
3039 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3040 key.offset = (u64)-1;
3041 key.type = BTRFS_CHUNK_ITEM_KEY;
3044 mutex_lock(&fs_info->delete_unused_bgs_mutex);
3045 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3047 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3050 BUG_ON(ret == 0); /* Corruption */
3052 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3055 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3061 leaf = path->nodes[0];
3062 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3064 chunk = btrfs_item_ptr(leaf, path->slots[0],
3065 struct btrfs_chunk);
3066 chunk_type = btrfs_chunk_type(leaf, chunk);
3067 btrfs_release_path(path);
3069 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
3070 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3076 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3078 if (found_key.offset == 0)
3080 key.offset = found_key.offset - 1;
3083 if (failed && !retried) {
3087 } else if (WARN_ON(failed && retried)) {
3091 btrfs_free_path(path);
3096 * return 1 : allocate a data chunk successfully,
3097 * return <0: errors during allocating a data chunk,
3098 * return 0 : no need to allocate a data chunk.
3100 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3103 struct btrfs_block_group_cache *cache;
3107 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3109 chunk_type = cache->flags;
3110 btrfs_put_block_group(cache);
3112 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3113 spin_lock(&fs_info->data_sinfo->lock);
3114 bytes_used = fs_info->data_sinfo->bytes_used;
3115 spin_unlock(&fs_info->data_sinfo->lock);
3118 struct btrfs_trans_handle *trans;
3121 trans = btrfs_join_transaction(fs_info->tree_root);
3123 return PTR_ERR(trans);
3125 ret = btrfs_force_chunk_alloc(trans, fs_info,
3126 BTRFS_BLOCK_GROUP_DATA);
3127 btrfs_end_transaction(trans);
3131 btrfs_add_raid_kobjects(fs_info);
3139 static int insert_balance_item(struct btrfs_fs_info *fs_info,
3140 struct btrfs_balance_control *bctl)
3142 struct btrfs_root *root = fs_info->tree_root;
3143 struct btrfs_trans_handle *trans;
3144 struct btrfs_balance_item *item;
3145 struct btrfs_disk_balance_args disk_bargs;
3146 struct btrfs_path *path;
3147 struct extent_buffer *leaf;
3148 struct btrfs_key key;
3151 path = btrfs_alloc_path();
3155 trans = btrfs_start_transaction(root, 0);
3156 if (IS_ERR(trans)) {
3157 btrfs_free_path(path);
3158 return PTR_ERR(trans);
3161 key.objectid = BTRFS_BALANCE_OBJECTID;
3162 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3165 ret = btrfs_insert_empty_item(trans, root, path, &key,
3170 leaf = path->nodes[0];
3171 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3173 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
3175 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3176 btrfs_set_balance_data(leaf, item, &disk_bargs);
3177 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3178 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3179 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3180 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3182 btrfs_set_balance_flags(leaf, item, bctl->flags);
3184 btrfs_mark_buffer_dirty(leaf);
3186 btrfs_free_path(path);
3187 err = btrfs_commit_transaction(trans);
3193 static int del_balance_item(struct btrfs_fs_info *fs_info)
3195 struct btrfs_root *root = fs_info->tree_root;
3196 struct btrfs_trans_handle *trans;
3197 struct btrfs_path *path;
3198 struct btrfs_key key;
3201 path = btrfs_alloc_path();
3205 trans = btrfs_start_transaction(root, 0);
3206 if (IS_ERR(trans)) {
3207 btrfs_free_path(path);
3208 return PTR_ERR(trans);
3211 key.objectid = BTRFS_BALANCE_OBJECTID;
3212 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3215 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3223 ret = btrfs_del_item(trans, root, path);
3225 btrfs_free_path(path);
3226 err = btrfs_commit_transaction(trans);
3233 * This is a heuristic used to reduce the number of chunks balanced on
3234 * resume after balance was interrupted.
3236 static void update_balance_args(struct btrfs_balance_control *bctl)
3239 * Turn on soft mode for chunk types that were being converted.
3241 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3242 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3243 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3244 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3245 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3246 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;