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/ratelimit.h>
12 #include <linux/kthread.h>
13 #include <linux/raid/pq.h>
14 #include <linux/semaphore.h>
15 #include <linux/uuid.h>
16 #include <linux/list_sort.h>
18 #include "extent_map.h"
20 #include "transaction.h"
21 #include "print-tree.h"
24 #include "async-thread.h"
25 #include "check-integrity.h"
26 #include "rcu-string.h"
28 #include "dev-replace.h"
31 const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
32 [BTRFS_RAID_RAID10] = {
35 .devs_max = 0, /* 0 == as many as possible */
37 .tolerated_failures = 1,
41 .raid_name = "raid10",
42 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
43 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
45 [BTRFS_RAID_RAID1] = {
50 .tolerated_failures = 1,
55 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
56 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
63 .tolerated_failures = 0,
68 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
71 [BTRFS_RAID_RAID0] = {
76 .tolerated_failures = 0,
81 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
84 [BTRFS_RAID_SINGLE] = {
89 .tolerated_failures = 0,
93 .raid_name = "single",
97 [BTRFS_RAID_RAID5] = {
102 .tolerated_failures = 1,
106 .raid_name = "raid5",
107 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
108 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
110 [BTRFS_RAID_RAID6] = {
115 .tolerated_failures = 2,
119 .raid_name = "raid6",
120 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
121 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
125 const char *get_raid_name(enum btrfs_raid_types type)
127 if (type >= BTRFS_NR_RAID_TYPES)
130 return btrfs_raid_array[type].raid_name;
133 static int init_first_rw_device(struct btrfs_trans_handle *trans,
134 struct btrfs_fs_info *fs_info);
135 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
136 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
137 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
138 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
139 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
140 enum btrfs_map_op op,
141 u64 logical, u64 *length,
142 struct btrfs_bio **bbio_ret,
143 int mirror_num, int need_raid_map);
149 * There are several mutexes that protect manipulation of devices and low-level
150 * structures like chunks but not block groups, extents or files
152 * uuid_mutex (global lock)
153 * ------------------------
154 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
155 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
156 * device) or requested by the device= mount option
158 * the mutex can be very coarse and can cover long-running operations
160 * protects: updates to fs_devices counters like missing devices, rw devices,
161 * seeding, structure cloning, openning/closing devices at mount/umount time
163 * global::fs_devs - add, remove, updates to the global list
165 * does not protect: manipulation of the fs_devices::devices list!
167 * btrfs_device::name - renames (write side), read is RCU
169 * fs_devices::device_list_mutex (per-fs, with RCU)
170 * ------------------------------------------------
171 * protects updates to fs_devices::devices, ie. adding and deleting
173 * simple list traversal with read-only actions can be done with RCU protection
175 * may be used to exclude some operations from running concurrently without any
176 * modifications to the list (see write_all_supers)
180 * protects balance structures (status, state) and context accessed from
181 * several places (internally, ioctl)
185 * protects chunks, adding or removing during allocation, trim or when a new
186 * device is added/removed
190 * a big lock that is held by the cleaner thread and prevents running subvolume
191 * cleaning together with relocation or delayed iputs
204 * Exclusive operations, BTRFS_FS_EXCL_OP
205 * ======================================
207 * Maintains the exclusivity of the following operations that apply to the
208 * whole filesystem and cannot run in parallel.
213 * - Device replace (*)
216 * The device operations (as above) can be in one of the following states:
222 * Only device operations marked with (*) can go into the Paused state for the
225 * - ioctl (only Balance can be Paused through ioctl)
226 * - filesystem remounted as read-only
227 * - filesystem unmounted and mounted as read-only
228 * - system power-cycle and filesystem mounted as read-only
229 * - filesystem or device errors leading to forced read-only
231 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
232 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
233 * A device operation in Paused or Running state can be canceled or resumed
234 * either by ioctl (Balance only) or when remounted as read-write.
235 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
239 DEFINE_MUTEX(uuid_mutex);
240 static LIST_HEAD(fs_uuids);
241 struct list_head *btrfs_get_fs_uuids(void)
247 * alloc_fs_devices - allocate struct btrfs_fs_devices
248 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
250 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
251 * The returned struct is not linked onto any lists and can be destroyed with
252 * kfree() right away.
254 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
256 struct btrfs_fs_devices *fs_devs;
258 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
260 return ERR_PTR(-ENOMEM);
262 mutex_init(&fs_devs->device_list_mutex);
264 INIT_LIST_HEAD(&fs_devs->devices);
265 INIT_LIST_HEAD(&fs_devs->resized_devices);
266 INIT_LIST_HEAD(&fs_devs->alloc_list);
267 INIT_LIST_HEAD(&fs_devs->fs_list);
269 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
274 void btrfs_free_device(struct btrfs_device *device)
276 rcu_string_free(device->name);
277 bio_put(device->flush_bio);
281 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
283 struct btrfs_device *device;
284 WARN_ON(fs_devices->opened);
285 while (!list_empty(&fs_devices->devices)) {
286 device = list_entry(fs_devices->devices.next,
287 struct btrfs_device, dev_list);
288 list_del(&device->dev_list);
289 btrfs_free_device(device);
294 static void btrfs_kobject_uevent(struct block_device *bdev,
295 enum kobject_action action)
299 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
301 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
303 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
304 &disk_to_dev(bdev->bd_disk)->kobj);
307 void __exit btrfs_cleanup_fs_uuids(void)
309 struct btrfs_fs_devices *fs_devices;
311 while (!list_empty(&fs_uuids)) {
312 fs_devices = list_entry(fs_uuids.next,
313 struct btrfs_fs_devices, fs_list);
314 list_del(&fs_devices->fs_list);
315 free_fs_devices(fs_devices);
320 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
321 * Returned struct is not linked onto any lists and must be destroyed using
324 static struct btrfs_device *__alloc_device(void)
326 struct btrfs_device *dev;
328 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
330 return ERR_PTR(-ENOMEM);
333 * Preallocate a bio that's always going to be used for flushing device
334 * barriers and matches the device lifespan
336 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
337 if (!dev->flush_bio) {
339 return ERR_PTR(-ENOMEM);
342 INIT_LIST_HEAD(&dev->dev_list);
343 INIT_LIST_HEAD(&dev->dev_alloc_list);
344 INIT_LIST_HEAD(&dev->resized_list);
346 spin_lock_init(&dev->io_lock);
348 atomic_set(&dev->reada_in_flight, 0);
349 atomic_set(&dev->dev_stats_ccnt, 0);
350 btrfs_device_data_ordered_init(dev);
351 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
352 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
358 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
361 * If devid and uuid are both specified, the match must be exact, otherwise
362 * only devid is used.
364 static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
365 u64 devid, const u8 *uuid)
367 struct btrfs_device *dev;
369 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
370 if (dev->devid == devid &&
371 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
378 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
380 struct btrfs_fs_devices *fs_devices;
382 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
383 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
390 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
391 int flush, struct block_device **bdev,
392 struct buffer_head **bh)
396 *bdev = blkdev_get_by_path(device_path, flags, holder);
399 ret = PTR_ERR(*bdev);
404 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
405 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
407 blkdev_put(*bdev, flags);
410 invalidate_bdev(*bdev);
411 *bh = btrfs_read_dev_super(*bdev);
414 blkdev_put(*bdev, flags);
426 static void requeue_list(struct btrfs_pending_bios *pending_bios,
427 struct bio *head, struct bio *tail)
430 struct bio *old_head;
432 old_head = pending_bios->head;
433 pending_bios->head = head;
434 if (pending_bios->tail)
435 tail->bi_next = old_head;
437 pending_bios->tail = tail;
441 * we try to collect pending bios for a device so we don't get a large
442 * number of procs sending bios down to the same device. This greatly
443 * improves the schedulers ability to collect and merge the bios.
445 * But, it also turns into a long list of bios to process and that is sure
446 * to eventually make the worker thread block. The solution here is to
447 * make some progress and then put this work struct back at the end of
448 * the list if the block device is congested. This way, multiple devices
449 * can make progress from a single worker thread.
451 static noinline void run_scheduled_bios(struct btrfs_device *device)
453 struct btrfs_fs_info *fs_info = device->fs_info;
455 struct backing_dev_info *bdi;
456 struct btrfs_pending_bios *pending_bios;
460 unsigned long num_run;
461 unsigned long batch_run = 0;
462 unsigned long last_waited = 0;
464 int sync_pending = 0;
465 struct blk_plug plug;
468 * this function runs all the bios we've collected for
469 * a particular device. We don't want to wander off to
470 * another device without first sending all of these down.
471 * So, setup a plug here and finish it off before we return
473 blk_start_plug(&plug);
475 bdi = device->bdev->bd_bdi;
478 spin_lock(&device->io_lock);
483 /* take all the bios off the list at once and process them
484 * later on (without the lock held). But, remember the
485 * tail and other pointers so the bios can be properly reinserted
486 * into the list if we hit congestion
488 if (!force_reg && device->pending_sync_bios.head) {
489 pending_bios = &device->pending_sync_bios;
492 pending_bios = &device->pending_bios;
496 pending = pending_bios->head;
497 tail = pending_bios->tail;
498 WARN_ON(pending && !tail);
501 * if pending was null this time around, no bios need processing
502 * at all and we can stop. Otherwise it'll loop back up again
503 * and do an additional check so no bios are missed.
505 * device->running_pending is used to synchronize with the
508 if (device->pending_sync_bios.head == NULL &&
509 device->pending_bios.head == NULL) {
511 device->running_pending = 0;
514 device->running_pending = 1;
517 pending_bios->head = NULL;
518 pending_bios->tail = NULL;
520 spin_unlock(&device->io_lock);
525 /* we want to work on both lists, but do more bios on the
526 * sync list than the regular list
529 pending_bios != &device->pending_sync_bios &&
530 device->pending_sync_bios.head) ||
531 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
532 device->pending_bios.head)) {
533 spin_lock(&device->io_lock);
534 requeue_list(pending_bios, pending, tail);
539 pending = pending->bi_next;
542 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
545 * if we're doing the sync list, record that our
546 * plug has some sync requests on it
548 * If we're doing the regular list and there are
549 * sync requests sitting around, unplug before
552 if (pending_bios == &device->pending_sync_bios) {
554 } else if (sync_pending) {
555 blk_finish_plug(&plug);
556 blk_start_plug(&plug);
560 btrfsic_submit_bio(cur);
567 * we made progress, there is more work to do and the bdi
568 * is now congested. Back off and let other work structs
571 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
572 fs_info->fs_devices->open_devices > 1) {
573 struct io_context *ioc;
575 ioc = current->io_context;
578 * the main goal here is that we don't want to
579 * block if we're going to be able to submit
580 * more requests without blocking.
582 * This code does two great things, it pokes into
583 * the elevator code from a filesystem _and_
584 * it makes assumptions about how batching works.
586 if (ioc && ioc->nr_batch_requests > 0 &&
587 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
589 ioc->last_waited == last_waited)) {
591 * we want to go through our batch of
592 * requests and stop. So, we copy out
593 * the ioc->last_waited time and test
594 * against it before looping
596 last_waited = ioc->last_waited;
600 spin_lock(&device->io_lock);
601 requeue_list(pending_bios, pending, tail);
602 device->running_pending = 1;
604 spin_unlock(&device->io_lock);
605 btrfs_queue_work(fs_info->submit_workers,
615 spin_lock(&device->io_lock);
616 if (device->pending_bios.head || device->pending_sync_bios.head)
618 spin_unlock(&device->io_lock);
621 blk_finish_plug(&plug);
624 static void pending_bios_fn(struct btrfs_work *work)
626 struct btrfs_device *device;
628 device = container_of(work, struct btrfs_device, work);
629 run_scheduled_bios(device);
633 * Search and remove all stale (devices which are not mounted) devices.
634 * When both inputs are NULL, it will search and release all stale devices.
635 * path: Optional. When provided will it release all unmounted devices
636 * matching this path only.
637 * skip_dev: Optional. Will skip this device when searching for the stale
640 static void btrfs_free_stale_devices(const char *path,
641 struct btrfs_device *skip_device)
643 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
644 struct btrfs_device *device, *tmp_device;
646 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
647 mutex_lock(&fs_devices->device_list_mutex);
648 if (fs_devices->opened) {
649 mutex_unlock(&fs_devices->device_list_mutex);
653 list_for_each_entry_safe(device, tmp_device,
654 &fs_devices->devices, dev_list) {
657 if (skip_device && skip_device == device)
659 if (path && !device->name)
664 not_found = strcmp(rcu_str_deref(device->name),
670 /* delete the stale device */
671 fs_devices->num_devices--;
672 list_del(&device->dev_list);
673 btrfs_free_device(device);
675 if (fs_devices->num_devices == 0)
678 mutex_unlock(&fs_devices->device_list_mutex);
679 if (fs_devices->num_devices == 0) {
680 btrfs_sysfs_remove_fsid(fs_devices);
681 list_del(&fs_devices->fs_list);
682 free_fs_devices(fs_devices);
687 static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
688 struct btrfs_device *device, fmode_t flags,
691 struct request_queue *q;
692 struct block_device *bdev;
693 struct buffer_head *bh;
694 struct btrfs_super_block *disk_super;
703 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
708 disk_super = (struct btrfs_super_block *)bh->b_data;
709 devid = btrfs_stack_device_id(&disk_super->dev_item);
710 if (devid != device->devid)
713 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
716 device->generation = btrfs_super_generation(disk_super);
718 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
719 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
720 fs_devices->seeding = 1;
722 if (bdev_read_only(bdev))
723 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
725 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
728 q = bdev_get_queue(bdev);
729 if (!blk_queue_nonrot(q))
730 fs_devices->rotating = 1;
733 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
734 device->mode = flags;
736 fs_devices->open_devices++;
737 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
738 device->devid != BTRFS_DEV_REPLACE_DEVID) {
739 fs_devices->rw_devices++;
740 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
748 blkdev_put(bdev, flags);
754 * Add new device to list of registered devices
757 * device pointer which was just added or updated when successful
758 * error pointer when failed
760 static noinline struct btrfs_device *device_list_add(const char *path,
761 struct btrfs_super_block *disk_super,
762 bool *new_device_added)
764 struct btrfs_device *device;
765 struct btrfs_fs_devices *fs_devices;
766 struct rcu_string *name;
767 u64 found_transid = btrfs_super_generation(disk_super);
768 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
770 fs_devices = find_fsid(disk_super->fsid);
772 fs_devices = alloc_fs_devices(disk_super->fsid);
773 if (IS_ERR(fs_devices))
774 return ERR_CAST(fs_devices);
776 mutex_lock(&fs_devices->device_list_mutex);
777 list_add(&fs_devices->fs_list, &fs_uuids);
781 mutex_lock(&fs_devices->device_list_mutex);
782 device = find_device(fs_devices, devid,
783 disk_super->dev_item.uuid);
787 if (fs_devices->opened) {
788 mutex_unlock(&fs_devices->device_list_mutex);
789 return ERR_PTR(-EBUSY);
792 device = btrfs_alloc_device(NULL, &devid,
793 disk_super->dev_item.uuid);
794 if (IS_ERR(device)) {
795 mutex_unlock(&fs_devices->device_list_mutex);
796 /* we can safely leave the fs_devices entry around */
800 name = rcu_string_strdup(path, GFP_NOFS);
802 btrfs_free_device(device);
803 mutex_unlock(&fs_devices->device_list_mutex);
804 return ERR_PTR(-ENOMEM);
806 rcu_assign_pointer(device->name, name);
808 list_add_rcu(&device->dev_list, &fs_devices->devices);
809 fs_devices->num_devices++;
811 device->fs_devices = fs_devices;
812 *new_device_added = true;
814 if (disk_super->label[0])
815 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
816 disk_super->label, devid, found_transid, path);
818 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
819 disk_super->fsid, devid, found_transid, path);
821 } else if (!device->name || strcmp(device->name->str, path)) {
823 * When FS is already mounted.
824 * 1. If you are here and if the device->name is NULL that
825 * means this device was missing at time of FS mount.
826 * 2. If you are here and if the device->name is different
827 * from 'path' that means either
828 * a. The same device disappeared and reappeared with
830 * b. The missing-disk-which-was-replaced, has
833 * We must allow 1 and 2a above. But 2b would be a spurious
836 * Further in case of 1 and 2a above, the disk at 'path'
837 * would have missed some transaction when it was away and
838 * in case of 2a the stale bdev has to be updated as well.
839 * 2b must not be allowed at all time.
843 * For now, we do allow update to btrfs_fs_device through the
844 * btrfs dev scan cli after FS has been mounted. We're still
845 * tracking a problem where systems fail mount by subvolume id
846 * when we reject replacement on a mounted FS.
848 if (!fs_devices->opened && found_transid < device->generation) {
850 * That is if the FS is _not_ mounted and if you
851 * are here, that means there is more than one
852 * disk with same uuid and devid.We keep the one
853 * with larger generation number or the last-in if
854 * generation are equal.
856 mutex_unlock(&fs_devices->device_list_mutex);
857 return ERR_PTR(-EEXIST);
861 * We are going to replace the device path for a given devid,
862 * make sure it's the same device if the device is mounted
865 struct block_device *path_bdev;
867 path_bdev = lookup_bdev(path);
868 if (IS_ERR(path_bdev)) {
869 mutex_unlock(&fs_devices->device_list_mutex);
870 return ERR_CAST(path_bdev);
873 if (device->bdev != path_bdev) {
875 mutex_unlock(&fs_devices->device_list_mutex);
876 btrfs_warn_in_rcu(device->fs_info,
877 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
878 disk_super->fsid, devid,
879 rcu_str_deref(device->name), path);
880 return ERR_PTR(-EEXIST);
883 btrfs_info_in_rcu(device->fs_info,
884 "device fsid %pU devid %llu moved old:%s new:%s",
885 disk_super->fsid, devid,
886 rcu_str_deref(device->name), path);
889 name = rcu_string_strdup(path, GFP_NOFS);
891 mutex_unlock(&fs_devices->device_list_mutex);
892 return ERR_PTR(-ENOMEM);
894 rcu_string_free(device->name);
895 rcu_assign_pointer(device->name, name);
896 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
897 fs_devices->missing_devices--;
898 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
903 * Unmount does not free the btrfs_device struct but would zero
904 * generation along with most of the other members. So just update
905 * it back. We need it to pick the disk with largest generation
908 if (!fs_devices->opened)
909 device->generation = found_transid;
911 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
913 mutex_unlock(&fs_devices->device_list_mutex);
917 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
919 struct btrfs_fs_devices *fs_devices;
920 struct btrfs_device *device;
921 struct btrfs_device *orig_dev;
923 fs_devices = alloc_fs_devices(orig->fsid);
924 if (IS_ERR(fs_devices))
927 mutex_lock(&orig->device_list_mutex);
928 fs_devices->total_devices = orig->total_devices;
930 /* We have held the volume lock, it is safe to get the devices. */
931 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
932 struct rcu_string *name;
934 device = btrfs_alloc_device(NULL, &orig_dev->devid,
940 * This is ok to do without rcu read locked because we hold the
941 * uuid mutex so nothing we touch in here is going to disappear.
943 if (orig_dev->name) {
944 name = rcu_string_strdup(orig_dev->name->str,
947 btrfs_free_device(device);
950 rcu_assign_pointer(device->name, name);
953 list_add(&device->dev_list, &fs_devices->devices);
954 device->fs_devices = fs_devices;
955 fs_devices->num_devices++;
957 mutex_unlock(&orig->device_list_mutex);
960 mutex_unlock(&orig->device_list_mutex);
961 free_fs_devices(fs_devices);
962 return ERR_PTR(-ENOMEM);
966 * After we have read the system tree and know devids belonging to
967 * this filesystem, remove the device which does not belong there.
969 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
971 struct btrfs_device *device, *next;
972 struct btrfs_device *latest_dev = NULL;
974 mutex_lock(&uuid_mutex);
976 /* This is the initialized path, it is safe to release the devices. */
977 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
978 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
979 &device->dev_state)) {
980 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
981 &device->dev_state) &&
983 device->generation > latest_dev->generation)) {
989 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
991 * In the first step, keep the device which has
992 * the correct fsid and the devid that is used
993 * for the dev_replace procedure.
994 * In the second step, the dev_replace state is
995 * read from the device tree and it is known
996 * whether the procedure is really active or
997 * not, which means whether this device is
998 * used or whether it should be removed.
1000 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1001 &device->dev_state)) {
1006 blkdev_put(device->bdev, device->mode);
1007 device->bdev = NULL;
1008 fs_devices->open_devices--;
1010 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1011 list_del_init(&device->dev_alloc_list);
1012 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1013 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1014 &device->dev_state))
1015 fs_devices->rw_devices--;
1017 list_del_init(&device->dev_list);
1018 fs_devices->num_devices--;
1019 btrfs_free_device(device);
1022 if (fs_devices->seed) {
1023 fs_devices = fs_devices->seed;
1027 fs_devices->latest_bdev = latest_dev->bdev;
1029 mutex_unlock(&uuid_mutex);
1032 static void free_device_rcu(struct rcu_head *head)
1034 struct btrfs_device *device;
1036 device = container_of(head, struct btrfs_device, rcu);
1037 btrfs_free_device(device);
1040 static void btrfs_close_bdev(struct btrfs_device *device)
1045 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1046 sync_blockdev(device->bdev);
1047 invalidate_bdev(device->bdev);
1050 blkdev_put(device->bdev, device->mode);
1053 static void btrfs_close_one_device(struct btrfs_device *device)
1055 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1056 struct btrfs_device *new_device;
1057 struct rcu_string *name;
1060 fs_devices->open_devices--;
1062 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1063 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1064 list_del_init(&device->dev_alloc_list);
1065 fs_devices->rw_devices--;
1068 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
1069 fs_devices->missing_devices--;
1071 btrfs_close_bdev(device);
1073 new_device = btrfs_alloc_device(NULL, &device->devid,
1075 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1077 /* Safe because we are under uuid_mutex */
1079 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1080 BUG_ON(!name); /* -ENOMEM */
1081 rcu_assign_pointer(new_device->name, name);
1084 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1085 new_device->fs_devices = device->fs_devices;
1087 call_rcu(&device->rcu, free_device_rcu);
1090 static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
1092 struct btrfs_device *device, *tmp;
1094 if (--fs_devices->opened > 0)
1097 mutex_lock(&fs_devices->device_list_mutex);
1098 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
1099 btrfs_close_one_device(device);
1101 mutex_unlock(&fs_devices->device_list_mutex);
1103 WARN_ON(fs_devices->open_devices);
1104 WARN_ON(fs_devices->rw_devices);
1105 fs_devices->opened = 0;
1106 fs_devices->seeding = 0;
1111 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1113 struct btrfs_fs_devices *seed_devices = NULL;
1116 mutex_lock(&uuid_mutex);
1117 ret = close_fs_devices(fs_devices);
1118 if (!fs_devices->opened) {
1119 seed_devices = fs_devices->seed;
1120 fs_devices->seed = NULL;
1122 mutex_unlock(&uuid_mutex);
1124 while (seed_devices) {
1125 fs_devices = seed_devices;
1126 seed_devices = fs_devices->seed;
1127 close_fs_devices(fs_devices);
1128 free_fs_devices(fs_devices);
1133 static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
1134 fmode_t flags, void *holder)
1136 struct btrfs_device *device;
1137 struct btrfs_device *latest_dev = NULL;
1140 flags |= FMODE_EXCL;
1142 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1143 /* Just open everything we can; ignore failures here */
1144 if (btrfs_open_one_device(fs_devices, device, flags, holder))
1148 device->generation > latest_dev->generation)
1149 latest_dev = device;
1151 if (fs_devices->open_devices == 0) {
1155 fs_devices->opened = 1;
1156 fs_devices->latest_bdev = latest_dev->bdev;
1157 fs_devices->total_rw_bytes = 0;
1162 static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1164 struct btrfs_device *dev1, *dev2;
1166 dev1 = list_entry(a, struct btrfs_device, dev_list);
1167 dev2 = list_entry(b, struct btrfs_device, dev_list);
1169 if (dev1->devid < dev2->devid)
1171 else if (dev1->devid > dev2->devid)
1176 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1177 fmode_t flags, void *holder)
1181 lockdep_assert_held(&uuid_mutex);
1183 mutex_lock(&fs_devices->device_list_mutex);
1184 if (fs_devices->opened) {
1185 fs_devices->opened++;
1188 list_sort(NULL, &fs_devices->devices, devid_cmp);
1189 ret = open_fs_devices(fs_devices, flags, holder);
1191 mutex_unlock(&fs_devices->device_list_mutex);
1196 static void btrfs_release_disk_super(struct page *page)
1202 static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1204 struct btrfs_super_block **disk_super)
1209 /* make sure our super fits in the device */
1210 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1213 /* make sure our super fits in the page */
1214 if (sizeof(**disk_super) > PAGE_SIZE)
1217 /* make sure our super doesn't straddle pages on disk */
1218 index = bytenr >> PAGE_SHIFT;
1219 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1222 /* pull in the page with our super */
1223 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1226 if (IS_ERR_OR_NULL(*page))
1231 /* align our pointer to the offset of the super block */
1232 *disk_super = p + (bytenr & ~PAGE_MASK);
1234 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1235 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1236 btrfs_release_disk_super(*page);
1240 if ((*disk_super)->label[0] &&
1241 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1242 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1248 * Look for a btrfs signature on a device. This may be called out of the mount path
1249 * and we are not allowed to call set_blocksize during the scan. The superblock
1250 * is read via pagecache
1252 struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1255 struct btrfs_super_block *disk_super;
1256 bool new_device_added = false;
1257 struct btrfs_device *device = NULL;
1258 struct block_device *bdev;
1262 lockdep_assert_held(&uuid_mutex);
1265 * we would like to check all the supers, but that would make
1266 * a btrfs mount succeed after a mkfs from a different FS.
1267 * So, we need to add a special mount option to scan for
1268 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1270 bytenr = btrfs_sb_offset(0);
1271 flags |= FMODE_EXCL;
1273 bdev = blkdev_get_by_path(path, flags, holder);
1275 return ERR_CAST(bdev);
1277 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1278 device = ERR_PTR(-EINVAL);
1279 goto error_bdev_put;
1282 device = device_list_add(path, disk_super, &new_device_added);
1283 if (!IS_ERR(device)) {
1284 if (new_device_added)
1285 btrfs_free_stale_devices(path, device);
1288 btrfs_release_disk_super(page);
1291 blkdev_put(bdev, flags);
1296 static int contains_pending_extent(struct btrfs_transaction *transaction,
1297 struct btrfs_device *device,
1298 u64 *start, u64 len)
1300 struct btrfs_fs_info *fs_info = device->fs_info;
1301 struct extent_map *em;
1302 struct list_head *search_list = &fs_info->pinned_chunks;
1304 u64 physical_start = *start;
1307 search_list = &transaction->pending_chunks;
1309 list_for_each_entry(em, search_list, list) {
1310 struct map_lookup *map;
1313 map = em->map_lookup;
1314 for (i = 0; i < map->num_stripes; i++) {
1317 if (map->stripes[i].dev != device)
1319 if (map->stripes[i].physical >= physical_start + len ||
1320 map->stripes[i].physical + em->orig_block_len <=
1324 * Make sure that while processing the pinned list we do
1325 * not override our *start with a lower value, because
1326 * we can have pinned chunks that fall within this
1327 * device hole and that have lower physical addresses
1328 * than the pending chunks we processed before. If we
1329 * do not take this special care we can end up getting
1330 * 2 pending chunks that start at the same physical
1331 * device offsets because the end offset of a pinned
1332 * chunk can be equal to the start offset of some
1335 end = map->stripes[i].physical + em->orig_block_len;
1342 if (search_list != &fs_info->pinned_chunks) {
1343 search_list = &fs_info->pinned_chunks;
1352 * find_free_dev_extent_start - find free space in the specified device
1353 * @device: the device which we search the free space in
1354 * @num_bytes: the size of the free space that we need
1355 * @search_start: the position from which to begin the search
1356 * @start: store the start of the free space.
1357 * @len: the size of the free space. that we find, or the size
1358 * of the max free space if we don't find suitable free space
1360 * this uses a pretty simple search, the expectation is that it is
1361 * called very infrequently and that a given device has a small number
1364 * @start is used to store the start of the free space if we find. But if we
1365 * don't find suitable free space, it will be used to store the start position
1366 * of the max free space.
1368 * @len is used to store the size of the free space that we find.
1369 * But if we don't find suitable free space, it is used to store the size of
1370 * the max free space.
1372 int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1373 struct btrfs_device *device, u64 num_bytes,
1374 u64 search_start, u64 *start, u64 *len)
1376 struct btrfs_fs_info *fs_info = device->fs_info;
1377 struct btrfs_root *root = fs_info->dev_root;
1378 struct btrfs_key key;
1379 struct btrfs_dev_extent *dev_extent;
1380 struct btrfs_path *path;
1385 u64 search_end = device->total_bytes;
1388 struct extent_buffer *l;
1391 * We don't want to overwrite the superblock on the drive nor any area
1392 * used by the boot loader (grub for example), so we make sure to start
1393 * at an offset of at least 1MB.
1395 search_start = max_t(u64, search_start, SZ_1M);
1397 path = btrfs_alloc_path();
1401 max_hole_start = search_start;
1405 if (search_start >= search_end ||
1406 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1411 path->reada = READA_FORWARD;
1412 path->search_commit_root = 1;
1413 path->skip_locking = 1;
1415 key.objectid = device->devid;
1416 key.offset = search_start;
1417 key.type = BTRFS_DEV_EXTENT_KEY;
1419 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1423 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1430 slot = path->slots[0];
1431 if (slot >= btrfs_header_nritems(l)) {
1432 ret = btrfs_next_leaf(root, path);
1440 btrfs_item_key_to_cpu(l, &key, slot);
1442 if (key.objectid < device->devid)
1445 if (key.objectid > device->devid)
1448 if (key.type != BTRFS_DEV_EXTENT_KEY)
1451 if (key.offset > search_start) {
1452 hole_size = key.offset - search_start;
1455 * Have to check before we set max_hole_start, otherwise
1456 * we could end up sending back this offset anyway.
1458 if (contains_pending_extent(transaction, device,
1461 if (key.offset >= search_start) {
1462 hole_size = key.offset - search_start;
1469 if (hole_size > max_hole_size) {
1470 max_hole_start = search_start;
1471 max_hole_size = hole_size;
1475 * If this free space is greater than which we need,
1476 * it must be the max free space that we have found
1477 * until now, so max_hole_start must point to the start
1478 * of this free space and the length of this free space
1479 * is stored in max_hole_size. Thus, we return
1480 * max_hole_start and max_hole_size and go back to the
1483 if (hole_size >= num_bytes) {
1489 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1490 extent_end = key.offset + btrfs_dev_extent_length(l,
1492 if (extent_end > search_start)
1493 search_start = extent_end;
1500 * At this point, search_start should be the end of
1501 * allocated dev extents, and when shrinking the device,
1502 * search_end may be smaller than search_start.
1504 if (search_end > search_start) {
1505 hole_size = search_end - search_start;
1507 if (contains_pending_extent(transaction, device, &search_start,
1509 btrfs_release_path(path);
1513 if (hole_size > max_hole_size) {
1514 max_hole_start = search_start;
1515 max_hole_size = hole_size;
1520 if (max_hole_size < num_bytes)
1526 btrfs_free_path(path);
1527 *start = max_hole_start;
1529 *len = max_hole_size;
1533 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1534 struct btrfs_device *device, u64 num_bytes,
1535 u64 *start, u64 *len)
1537 /* FIXME use last free of some kind */
1538 return find_free_dev_extent_start(trans->transaction, device,
1539 num_bytes, 0, start, len);
1542 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1543 struct btrfs_device *device,
1544 u64 start, u64 *dev_extent_len)
1546 struct btrfs_fs_info *fs_info = device->fs_info;
1547 struct btrfs_root *root = fs_info->dev_root;
1549 struct btrfs_path *path;
1550 struct btrfs_key key;
1551 struct btrfs_key found_key;
1552 struct extent_buffer *leaf = NULL;
1553 struct btrfs_dev_extent *extent = NULL;
1555 path = btrfs_alloc_path();
1559 key.objectid = device->devid;
1561 key.type = BTRFS_DEV_EXTENT_KEY;
1563 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1565 ret = btrfs_previous_item(root, path, key.objectid,
1566 BTRFS_DEV_EXTENT_KEY);
1569 leaf = path->nodes[0];
1570 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1571 extent = btrfs_item_ptr(leaf, path->slots[0],
1572 struct btrfs_dev_extent);
1573 BUG_ON(found_key.offset > start || found_key.offset +
1574 btrfs_dev_extent_length(leaf, extent) < start);
1576 btrfs_release_path(path);
1578 } else if (ret == 0) {
1579 leaf = path->nodes[0];
1580 extent = btrfs_item_ptr(leaf, path->slots[0],
1581 struct btrfs_dev_extent);
1583 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
1587 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1589 ret = btrfs_del_item(trans, root, path);
1591 btrfs_handle_fs_error(fs_info, ret,
1592 "Failed to remove dev extent item");
1594 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
1597 btrfs_free_path(path);
1601 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1602 struct btrfs_device *device,
1603 u64 chunk_offset, u64 start, u64 num_bytes)
1606 struct btrfs_path *path;
1607 struct btrfs_fs_info *fs_info = device->fs_info;
1608 struct btrfs_root *root = fs_info->dev_root;
1609 struct btrfs_dev_extent *extent;
1610 struct extent_buffer *leaf;
1611 struct btrfs_key key;
1613 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
1614 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1615 path = btrfs_alloc_path();
1619 key.objectid = device->devid;
1621 key.type = BTRFS_DEV_EXTENT_KEY;
1622 ret = btrfs_insert_empty_item(trans, root, path, &key,
1627 leaf = path->nodes[0];
1628 extent = btrfs_item_ptr(leaf, path->slots[0],
1629 struct btrfs_dev_extent);
1630 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1631 BTRFS_CHUNK_TREE_OBJECTID);
1632 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1633 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1634 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1636 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1637 btrfs_mark_buffer_dirty(leaf);
1639 btrfs_free_path(path);
1643 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1645 struct extent_map_tree *em_tree;
1646 struct extent_map *em;
1650 em_tree = &fs_info->mapping_tree.map_tree;
1651 read_lock(&em_tree->lock);
1652 n = rb_last(&em_tree->map.rb_root);
1654 em = rb_entry(n, struct extent_map, rb_node);
1655 ret = em->start + em->len;
1657 read_unlock(&em_tree->lock);
1662 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1666 struct btrfs_key key;
1667 struct btrfs_key found_key;
1668 struct btrfs_path *path;
1670 path = btrfs_alloc_path();
1674 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1675 key.type = BTRFS_DEV_ITEM_KEY;
1676 key.offset = (u64)-1;
1678 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1682 BUG_ON(ret == 0); /* Corruption */
1684 ret = btrfs_previous_item(fs_info->chunk_root, path,
1685 BTRFS_DEV_ITEMS_OBJECTID,
1686 BTRFS_DEV_ITEM_KEY);
1690 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1692 *devid_ret = found_key.offset + 1;
1696 btrfs_free_path(path);
1701 * the device information is stored in the chunk root
1702 * the btrfs_device struct should be fully filled in
1704 static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
1705 struct btrfs_device *device)
1708 struct btrfs_path *path;
1709 struct btrfs_dev_item *dev_item;
1710 struct extent_buffer *leaf;
1711 struct btrfs_key key;
1714 path = btrfs_alloc_path();
1718 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1719 key.type = BTRFS_DEV_ITEM_KEY;
1720 key.offset = device->devid;
1722 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1723 &key, sizeof(*dev_item));
1727 leaf = path->nodes[0];
1728 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1730 btrfs_set_device_id(leaf, dev_item, device->devid);
1731 btrfs_set_device_generation(leaf, dev_item, 0);
1732 btrfs_set_device_type(leaf, dev_item, device->type);
1733 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1734 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1735 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1736 btrfs_set_device_total_bytes(leaf, dev_item,
1737 btrfs_device_get_disk_total_bytes(device));
1738 btrfs_set_device_bytes_used(leaf, dev_item,
1739 btrfs_device_get_bytes_used(device));
1740 btrfs_set_device_group(leaf, dev_item, 0);
1741 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1742 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1743 btrfs_set_device_start_offset(leaf, dev_item, 0);
1745 ptr = btrfs_device_uuid(dev_item);
1746 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1747 ptr = btrfs_device_fsid(dev_item);
1748 write_extent_buffer(leaf, trans->fs_info->fsid, ptr, BTRFS_FSID_SIZE);
1749 btrfs_mark_buffer_dirty(leaf);
1753 btrfs_free_path(path);
1758 * Function to update ctime/mtime for a given device path.
1759 * Mainly used for ctime/mtime based probe like libblkid.
1761 static void update_dev_time(const char *path_name)
1765 filp = filp_open(path_name, O_RDWR, 0);
1768 file_update_time(filp);
1769 filp_close(filp, NULL);
1772 static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
1773 struct btrfs_device *device)
1775 struct btrfs_root *root = fs_info->chunk_root;
1777 struct btrfs_path *path;
1778 struct btrfs_key key;
1779 struct btrfs_trans_handle *trans;
1781 path = btrfs_alloc_path();
1785 trans = btrfs_start_transaction(root, 0);
1786 if (IS_ERR(trans)) {
1787 btrfs_free_path(path);
1788 return PTR_ERR(trans);
1790 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1791 key.type = BTRFS_DEV_ITEM_KEY;
1792 key.offset = device->devid;
1794 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1798 btrfs_abort_transaction(trans, ret);
1799 btrfs_end_transaction(trans);
1803 ret = btrfs_del_item(trans, root, path);
1805 btrfs_abort_transaction(trans, ret);
1806 btrfs_end_transaction(trans);
1810 btrfs_free_path(path);
1812 ret = btrfs_commit_transaction(trans);
1817 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1818 * filesystem. It's up to the caller to adjust that number regarding eg. device
1821 static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1829 seq = read_seqbegin(&fs_info->profiles_lock);
1831 all_avail = fs_info->avail_data_alloc_bits |
1832 fs_info->avail_system_alloc_bits |
1833 fs_info->avail_metadata_alloc_bits;
1834 } while (read_seqretry(&fs_info->profiles_lock, seq));
1836 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1837 if (!(all_avail & btrfs_raid_array[i].bg_flag))
1840 if (num_devices < btrfs_raid_array[i].devs_min) {
1841 int ret = btrfs_raid_array[i].mindev_error;
1851 static struct btrfs_device * btrfs_find_next_active_device(
1852 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
1854 struct btrfs_device *next_device;
1856 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1857 if (next_device != device &&
1858 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1859 && next_device->bdev)
1867 * Helper function to check if the given device is part of s_bdev / latest_bdev
1868 * and replace it with the provided or the next active device, in the context
1869 * where this function called, there should be always be another device (or
1870 * this_dev) which is active.
1872 void btrfs_assign_next_active_device(struct btrfs_device *device,
1873 struct btrfs_device *this_dev)
1875 struct btrfs_fs_info *fs_info = device->fs_info;
1876 struct btrfs_device *next_device;
1879 next_device = this_dev;
1881 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1883 ASSERT(next_device);
1885 if (fs_info->sb->s_bdev &&
1886 (fs_info->sb->s_bdev == device->bdev))
1887 fs_info->sb->s_bdev = next_device->bdev;
1889 if (fs_info->fs_devices->latest_bdev == device->bdev)
1890 fs_info->fs_devices->latest_bdev = next_device->bdev;
1894 * Return btrfs_fs_devices::num_devices excluding the device that's being
1895 * currently replaced.
1897 static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1899 u64 num_devices = fs_info->fs_devices->num_devices;
1901 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
1902 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1903 ASSERT(num_devices > 1);
1906 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
1911 int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1914 struct btrfs_device *device;
1915 struct btrfs_fs_devices *cur_devices;
1916 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1920 mutex_lock(&uuid_mutex);
1922 num_devices = btrfs_num_devices(fs_info);
1924 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
1928 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
1930 if (IS_ERR(device)) {
1931 if (PTR_ERR(device) == -ENOENT &&
1932 strcmp(device_path, "missing") == 0)
1933 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1935 ret = PTR_ERR(device);
1939 if (btrfs_pinned_by_swapfile(fs_info, device)) {
1940 btrfs_warn_in_rcu(fs_info,
1941 "cannot remove device %s (devid %llu) due to active swapfile",
1942 rcu_str_deref(device->name), device->devid);
1947 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1948 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1952 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1953 fs_info->fs_devices->rw_devices == 1) {
1954 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1958 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1959 mutex_lock(&fs_info->chunk_mutex);
1960 list_del_init(&device->dev_alloc_list);
1961 device->fs_devices->rw_devices--;
1962 mutex_unlock(&fs_info->chunk_mutex);
1965 mutex_unlock(&uuid_mutex);
1966 ret = btrfs_shrink_device(device, 0);
1967 mutex_lock(&uuid_mutex);
1972 * TODO: the superblock still includes this device in its num_devices
1973 * counter although write_all_supers() is not locked out. This
1974 * could give a filesystem state which requires a degraded mount.
1976 ret = btrfs_rm_dev_item(fs_info, device);
1980 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1981 btrfs_scrub_cancel_dev(fs_info, device);
1984 * the device list mutex makes sure that we don't change
1985 * the device list while someone else is writing out all
1986 * the device supers. Whoever is writing all supers, should
1987 * lock the device list mutex before getting the number of
1988 * devices in the super block (super_copy). Conversely,
1989 * whoever updates the number of devices in the super block
1990 * (super_copy) should hold the device list mutex.
1994 * In normal cases the cur_devices == fs_devices. But in case
1995 * of deleting a seed device, the cur_devices should point to
1996 * its own fs_devices listed under the fs_devices->seed.
1998 cur_devices = device->fs_devices;
1999 mutex_lock(&fs_devices->device_list_mutex);
2000 list_del_rcu(&device->dev_list);
2002 cur_devices->num_devices--;
2003 cur_devices->total_devices--;
2004 /* Update total_devices of the parent fs_devices if it's seed */
2005 if (cur_devices != fs_devices)
2006 fs_devices->total_devices--;
2008 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
2009 cur_devices->missing_devices--;
2011 btrfs_assign_next_active_device(device, NULL);
2014 cur_devices->open_devices--;
2015 /* remove sysfs entry */
2016 btrfs_sysfs_rm_device_link(fs_devices, device);
2019 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2020 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2021 mutex_unlock(&fs_devices->device_list_mutex);
2024 * at this point, the device is zero sized and detached from
2025 * the devices list. All that's left is to zero out the old
2026 * supers and free the device.
2028 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2029 btrfs_scratch_superblocks(device->bdev, device->name->str);
2031 btrfs_close_bdev(device);
2032 call_rcu(&device->rcu, free_device_rcu);
2034 if (cur_devices->open_devices == 0) {
2035 while (fs_devices) {
2036 if (fs_devices->seed == cur_devices) {
2037 fs_devices->seed = cur_devices->seed;
2040 fs_devices = fs_devices->seed;
2042 cur_devices->seed = NULL;
2043 close_fs_devices(cur_devices);
2044 free_fs_devices(cur_devices);
2048 mutex_unlock(&uuid_mutex);
2052 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2053 mutex_lock(&fs_info->chunk_mutex);
2054 list_add(&device->dev_alloc_list,
2055 &fs_devices->alloc_list);
2056 device->fs_devices->rw_devices++;
2057 mutex_unlock(&fs_info->chunk_mutex);
2062 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
2064 struct btrfs_fs_devices *fs_devices;
2066 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
2069 * in case of fs with no seed, srcdev->fs_devices will point
2070 * to fs_devices of fs_info. However when the dev being replaced is
2071 * a seed dev it will point to the seed's local fs_devices. In short
2072 * srcdev will have its correct fs_devices in both the cases.
2074 fs_devices = srcdev->fs_devices;
2076 list_del_rcu(&srcdev->dev_list);
2077 list_del(&srcdev->dev_alloc_list);
2078 fs_devices->num_devices--;
2079 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
2080 fs_devices->missing_devices--;
2082 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
2083 fs_devices->rw_devices--;
2086 fs_devices->open_devices--;
2089 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2090 struct btrfs_device *srcdev)
2092 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
2094 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
2095 /* zero out the old super if it is writable */
2096 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2099 btrfs_close_bdev(srcdev);
2100 call_rcu(&srcdev->rcu, free_device_rcu);
2102 /* if this is no devs we rather delete the fs_devices */
2103 if (!fs_devices->num_devices) {
2104 struct btrfs_fs_devices *tmp_fs_devices;
2107 * On a mounted FS, num_devices can't be zero unless it's a
2108 * seed. In case of a seed device being replaced, the replace
2109 * target added to the sprout FS, so there will be no more
2110 * device left under the seed FS.
2112 ASSERT(fs_devices->seeding);
2114 tmp_fs_devices = fs_info->fs_devices;
2115 while (tmp_fs_devices) {
2116 if (tmp_fs_devices->seed == fs_devices) {
2117 tmp_fs_devices->seed = fs_devices->seed;
2120 tmp_fs_devices = tmp_fs_devices->seed;
2122 fs_devices->seed = NULL;
2123 close_fs_devices(fs_devices);
2124 free_fs_devices(fs_devices);
2128 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
2130 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
2133 mutex_lock(&fs_devices->device_list_mutex);
2135 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
2138 fs_devices->open_devices--;
2140 fs_devices->num_devices--;
2142 btrfs_assign_next_active_device(tgtdev, NULL);
2144 list_del_rcu(&tgtdev->dev_list);
2146 mutex_unlock(&fs_devices->device_list_mutex);
2149 * The update_dev_time() with in btrfs_scratch_superblocks()
2150 * may lead to a call to btrfs_show_devname() which will try
2151 * to hold device_list_mutex. And here this device
2152 * is already out of device list, so we don't have to hold
2153 * the device_list_mutex lock.
2155 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
2157 btrfs_close_bdev(tgtdev);
2158 call_rcu(&tgtdev->rcu, free_device_rcu);
2161 static struct btrfs_device *btrfs_find_device_by_path(
2162 struct btrfs_fs_info *fs_info, const char *device_path)
2165 struct btrfs_super_block *disk_super;
2168 struct block_device *bdev;
2169 struct buffer_head *bh;
2170 struct btrfs_device *device;
2172 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2173 fs_info->bdev_holder, 0, &bdev, &bh);
2175 return ERR_PTR(ret);
2176 disk_super = (struct btrfs_super_block *)bh->b_data;
2177 devid = btrfs_stack_device_id(&disk_super->dev_item);
2178 dev_uuid = disk_super->dev_item.uuid;
2179 device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
2182 device = ERR_PTR(-ENOENT);
2183 blkdev_put(bdev, FMODE_READ);
2187 static struct btrfs_device *btrfs_find_device_missing_or_by_path(
2188 struct btrfs_fs_info *fs_info, const char *device_path)
2190 struct btrfs_device *device = NULL;
2191 if (strcmp(device_path, "missing") == 0) {
2192 struct list_head *devices;
2193 struct btrfs_device *tmp;
2195 devices = &fs_info->fs_devices->devices;
2196 list_for_each_entry(tmp, devices, dev_list) {
2197 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2198 &tmp->dev_state) && !tmp->bdev) {
2205 return ERR_PTR(-ENOENT);
2207 device = btrfs_find_device_by_path(fs_info, device_path);
2214 * Lookup a device given by device id, or the path if the id is 0.
2216 struct btrfs_device *btrfs_find_device_by_devspec(
2217 struct btrfs_fs_info *fs_info, u64 devid, const char *devpath)
2219 struct btrfs_device *device;
2222 device = btrfs_find_device(fs_info, devid, NULL, NULL);
2224 return ERR_PTR(-ENOENT);
2226 if (!devpath || !devpath[0])
2227 return ERR_PTR(-EINVAL);
2228 device = btrfs_find_device_missing_or_by_path(fs_info, devpath);
2234 * does all the dirty work required for changing file system's UUID.
2236 static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
2238 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2239 struct btrfs_fs_devices *old_devices;
2240 struct btrfs_fs_devices *seed_devices;
2241 struct btrfs_super_block *disk_super = fs_info->super_copy;
2242 struct btrfs_device *device;
2245 lockdep_assert_held(&uuid_mutex);
2246 if (!fs_devices->seeding)
2249 seed_devices = alloc_fs_devices(NULL);
2250 if (IS_ERR(seed_devices))
2251 return PTR_ERR(seed_devices);
2253 old_devices = clone_fs_devices(fs_devices);
2254 if (IS_ERR(old_devices)) {
2255 kfree(seed_devices);
2256 return PTR_ERR(old_devices);
2259 list_add(&old_devices->fs_list, &fs_uuids);
2261 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2262 seed_devices->opened = 1;
2263 INIT_LIST_HEAD(&seed_devices->devices);
2264 INIT_LIST_HEAD(&seed_devices->alloc_list);
2265 mutex_init(&seed_devices->device_list_mutex);
2267 mutex_lock(&fs_devices->device_list_mutex);
2268 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2270 list_for_each_entry(device, &seed_devices->devices, dev_list)
2271 device->fs_devices = seed_devices;
2273 mutex_lock(&fs_info->chunk_mutex);
2274 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2275 mutex_unlock(&fs_info->chunk_mutex);
2277 fs_devices->seeding = 0;
2278 fs_devices->num_devices = 0;
2279 fs_devices->open_devices = 0;
2280 fs_devices->missing_devices = 0;
2281 fs_devices->rotating = 0;
2282 fs_devices->seed = seed_devices;
2284 generate_random_uuid(fs_devices->fsid);
2285 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2286 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2287 mutex_unlock(&fs_devices->device_list_mutex);
2289 super_flags = btrfs_super_flags(disk_super) &
2290 ~BTRFS_SUPER_FLAG_SEEDING;
2291 btrfs_set_super_flags(disk_super, super_flags);
2297 * Store the expected generation for seed devices in device items.
2299 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2300 struct btrfs_fs_info *fs_info)
2302 struct btrfs_root *root = fs_info->chunk_root;
2303 struct btrfs_path *path;
2304 struct extent_buffer *leaf;
2305 struct btrfs_dev_item *dev_item;
2306 struct btrfs_device *device;
2307 struct btrfs_key key;
2308 u8 fs_uuid[BTRFS_FSID_SIZE];
2309 u8 dev_uuid[BTRFS_UUID_SIZE];
2313 path = btrfs_alloc_path();
2317 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2319 key.type = BTRFS_DEV_ITEM_KEY;
2322 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2326 leaf = path->nodes[0];
2328 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2329 ret = btrfs_next_leaf(root, path);
2334 leaf = path->nodes[0];
2335 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2336 btrfs_release_path(path);
2340 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2341 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2342 key.type != BTRFS_DEV_ITEM_KEY)
2345 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2346 struct btrfs_dev_item);
2347 devid = btrfs_device_id(leaf, dev_item);
2348 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2350 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2352 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
2353 BUG_ON(!device); /* Logic error */
2355 if (device->fs_devices->seeding) {
2356 btrfs_set_device_generation(leaf, dev_item,
2357 device->generation);
2358 btrfs_mark_buffer_dirty(leaf);
2366 btrfs_free_path(path);
2370 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
2372 struct btrfs_root *root = fs_info->dev_root;
2373 struct request_queue *q;
2374 struct btrfs_trans_handle *trans;
2375 struct btrfs_device *device;
2376 struct block_device *bdev;
2377 struct super_block *sb = fs_info->sb;
2378 struct rcu_string *name;
2379 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2380 u64 orig_super_total_bytes;
2381 u64 orig_super_num_devices;
2382 int seeding_dev = 0;
2384 bool unlocked = false;
2386 if (sb_rdonly(sb) && !fs_devices->seeding)
2389 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2390 fs_info->bdev_holder);
2392 return PTR_ERR(bdev);
2394 if (fs_devices->seeding) {
2396 down_write(&sb->s_umount);
2397 mutex_lock(&uuid_mutex);
2400 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2402 mutex_lock(&fs_devices->device_list_mutex);
2403 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2404 if (device->bdev == bdev) {
2407 &fs_devices->device_list_mutex);
2411 mutex_unlock(&fs_devices->device_list_mutex);
2413 device = btrfs_alloc_device(fs_info, NULL, NULL);
2414 if (IS_ERR(device)) {
2415 /* we can safely leave the fs_devices entry around */
2416 ret = PTR_ERR(device);
2420 name = rcu_string_strdup(device_path, GFP_KERNEL);
2423 goto error_free_device;
2425 rcu_assign_pointer(device->name, name);
2427 trans = btrfs_start_transaction(root, 0);
2428 if (IS_ERR(trans)) {
2429 ret = PTR_ERR(trans);
2430 goto error_free_device;
2433 q = bdev_get_queue(bdev);
2434 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2435 device->generation = trans->transid;
2436 device->io_width = fs_info->sectorsize;
2437 device->io_align = fs_info->sectorsize;
2438 device->sector_size = fs_info->sectorsize;
2439 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2440 fs_info->sectorsize);
2441 device->disk_total_bytes = device->total_bytes;
2442 device->commit_total_bytes = device->total_bytes;
2443 device->fs_info = fs_info;
2444 device->bdev = bdev;
2445 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2446 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2447 device->mode = FMODE_EXCL;
2448 device->dev_stats_valid = 1;
2449 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2452 sb->s_flags &= ~SB_RDONLY;
2453 ret = btrfs_prepare_sprout(fs_info);
2455 btrfs_abort_transaction(trans, ret);
2460 device->fs_devices = fs_devices;
2462 mutex_lock(&fs_devices->device_list_mutex);
2463 mutex_lock(&fs_info->chunk_mutex);
2464 list_add_rcu(&device->dev_list, &fs_devices->devices);
2465 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2466 fs_devices->num_devices++;
2467 fs_devices->open_devices++;
2468 fs_devices->rw_devices++;
2469 fs_devices->total_devices++;
2470 fs_devices->total_rw_bytes += device->total_bytes;
2472 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
2474 if (!blk_queue_nonrot(q))
2475 fs_devices->rotating = 1;
2477 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
2478 btrfs_set_super_total_bytes(fs_info->super_copy,
2479 round_down(orig_super_total_bytes + device->total_bytes,
2480 fs_info->sectorsize));
2482 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2483 btrfs_set_super_num_devices(fs_info->super_copy,
2484 orig_super_num_devices + 1);
2486 /* add sysfs device entry */
2487 btrfs_sysfs_add_device_link(fs_devices, device);
2490 * we've got more storage, clear any full flags on the space
2493 btrfs_clear_space_info_full(fs_info);
2495 mutex_unlock(&fs_info->chunk_mutex);
2496 mutex_unlock(&fs_devices->device_list_mutex);
2499 mutex_lock(&fs_info->chunk_mutex);
2500 ret = init_first_rw_device(trans, fs_info);
2501 mutex_unlock(&fs_info->chunk_mutex);
2503 btrfs_abort_transaction(trans, ret);
2508 ret = btrfs_add_dev_item(trans, device);
2510 btrfs_abort_transaction(trans, ret);
2515 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2517 ret = btrfs_finish_sprout(trans, fs_info);
2519 btrfs_abort_transaction(trans, ret);
2523 /* Sprouting would change fsid of the mounted root,
2524 * so rename the fsid on the sysfs
2526 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2528 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
2530 "sysfs: failed to create fsid for sprout");
2533 ret = btrfs_commit_transaction(trans);
2536 mutex_unlock(&uuid_mutex);
2537 up_write(&sb->s_umount);
2540 if (ret) /* transaction commit */
2543 ret = btrfs_relocate_sys_chunks(fs_info);
2545 btrfs_handle_fs_error(fs_info, ret,
2546 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2547 trans = btrfs_attach_transaction(root);
2548 if (IS_ERR(trans)) {
2549 if (PTR_ERR(trans) == -ENOENT)
2551 ret = PTR_ERR(trans);
2555 ret = btrfs_commit_transaction(trans);
2558 /* Update ctime/mtime for libblkid */
2559 update_dev_time(device_path);
2563 btrfs_sysfs_rm_device_link(fs_devices, device);
2564 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2565 mutex_lock(&fs_info->chunk_mutex);
2566 list_del_rcu(&device->dev_list);
2567 list_del(&device->dev_alloc_list);
2568 fs_info->fs_devices->num_devices--;
2569 fs_info->fs_devices->open_devices--;
2570 fs_info->fs_devices->rw_devices--;
2571 fs_info->fs_devices->total_devices--;
2572 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2573 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2574 btrfs_set_super_total_bytes(fs_info->super_copy,
2575 orig_super_total_bytes);
2576 btrfs_set_super_num_devices(fs_info->super_copy,
2577 orig_super_num_devices);
2578 mutex_unlock(&fs_info->chunk_mutex);
2579 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2582 sb->s_flags |= SB_RDONLY;
2584 btrfs_end_transaction(trans);
2586 btrfs_free_device(device);
2588 blkdev_put(bdev, FMODE_EXCL);
2589 if (seeding_dev && !unlocked) {
2590 mutex_unlock(&uuid_mutex);
2591 up_write(&sb->s_umount);
2596 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2597 struct btrfs_device *device)
2600 struct btrfs_path *path;
2601 struct btrfs_root *root = device->fs_info->chunk_root;
2602 struct btrfs_dev_item *dev_item;
2603 struct extent_buffer *leaf;
2604 struct btrfs_key key;
2606 path = btrfs_alloc_path();
2610 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2611 key.type = BTRFS_DEV_ITEM_KEY;
2612 key.offset = device->devid;
2614 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2623 leaf = path->nodes[0];
2624 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2626 btrfs_set_device_id(leaf, dev_item, device->devid);
2627 btrfs_set_device_type(leaf, dev_item, device->type);
2628 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2629 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2630 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2631 btrfs_set_device_total_bytes(leaf, dev_item,
2632 btrfs_device_get_disk_total_bytes(device));
2633 btrfs_set_device_bytes_used(leaf, dev_item,
2634 btrfs_device_get_bytes_used(device));
2635 btrfs_mark_buffer_dirty(leaf);
2638 btrfs_free_path(path);
2642 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2643 struct btrfs_device *device, u64 new_size)
2645 struct btrfs_fs_info *fs_info = device->fs_info;
2646 struct btrfs_super_block *super_copy = fs_info->super_copy;
2647 struct btrfs_fs_devices *fs_devices;
2651 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2654 new_size = round_down(new_size, fs_info->sectorsize);
2656 mutex_lock(&fs_info->chunk_mutex);
2657 old_total = btrfs_super_total_bytes(super_copy);
2658 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
2660 if (new_size <= device->total_bytes ||
2661 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2662 mutex_unlock(&fs_info->chunk_mutex);
2666 fs_devices = fs_info->fs_devices;
2668 btrfs_set_super_total_bytes(super_copy,
2669 round_down(old_total + diff, fs_info->sectorsize));
2670 device->fs_devices->total_rw_bytes += diff;
2672 btrfs_device_set_total_bytes(device, new_size);
2673 btrfs_device_set_disk_total_bytes(device, new_size);
2674 btrfs_clear_space_info_full(device->fs_info);
2675 if (list_empty(&device->resized_list))
2676 list_add_tail(&device->resized_list,
2677 &fs_devices->resized_devices);
2678 mutex_unlock(&fs_info->chunk_mutex);
2680 return btrfs_update_device(trans, device);
2683 static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
2685 struct btrfs_fs_info *fs_info = trans->fs_info;
2686 struct btrfs_root *root = fs_info->chunk_root;
2688 struct btrfs_path *path;
2689 struct btrfs_key key;
2691 path = btrfs_alloc_path();
2695 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2696 key.offset = chunk_offset;
2697 key.type = BTRFS_CHUNK_ITEM_KEY;
2699 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2702 else if (ret > 0) { /* Logic error or corruption */
2703 btrfs_handle_fs_error(fs_info, -ENOENT,
2704 "Failed lookup while freeing chunk.");
2709 ret = btrfs_del_item(trans, root, path);
2711 btrfs_handle_fs_error(fs_info, ret,
2712 "Failed to delete chunk item.");
2714 btrfs_free_path(path);
2718 static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2720 struct btrfs_super_block *super_copy = fs_info->super_copy;
2721 struct btrfs_disk_key *disk_key;
2722 struct btrfs_chunk *chunk;
2729 struct btrfs_key key;
2731 mutex_lock(&fs_info->chunk_mutex);
2732 array_size = btrfs_super_sys_array_size(super_copy);
2734 ptr = super_copy->sys_chunk_array;
2737 while (cur < array_size) {
2738 disk_key = (struct btrfs_disk_key *)ptr;
2739 btrfs_disk_key_to_cpu(&key, disk_key);
2741 len = sizeof(*disk_key);
2743 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2744 chunk = (struct btrfs_chunk *)(ptr + len);
2745 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2746 len += btrfs_chunk_item_size(num_stripes);
2751 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
2752 key.offset == chunk_offset) {
2753 memmove(ptr, ptr + len, array_size - (cur + len));
2755 btrfs_set_super_sys_array_size(super_copy, array_size);
2761 mutex_unlock(&fs_info->chunk_mutex);
2766 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2767 * @logical: Logical block offset in bytes.
2768 * @length: Length of extent in bytes.
2770 * Return: Chunk mapping or ERR_PTR.
2772 struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2773 u64 logical, u64 length)
2775 struct extent_map_tree *em_tree;
2776 struct extent_map *em;
2778 em_tree = &fs_info->mapping_tree.map_tree;
2779 read_lock(&em_tree->lock);
2780 em = lookup_extent_mapping(em_tree, logical, length);
2781 read_unlock(&em_tree->lock);
2784 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2786 return ERR_PTR(-EINVAL);
2789 if (em->start > logical || em->start + em->len < logical) {
2791 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2792 logical, length, em->start, em->start + em->len);
2793 free_extent_map(em);
2794 return ERR_PTR(-EINVAL);
2797 /* callers are responsible for dropping em's ref. */
2801 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
2803 struct btrfs_fs_info *fs_info = trans->fs_info;
2804 struct extent_map *em;
2805 struct map_lookup *map;
2806 u64 dev_extent_len = 0;
2808 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2810 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
2813 * This is a logic error, but we don't want to just rely on the
2814 * user having built with ASSERT enabled, so if ASSERT doesn't
2815 * do anything we still error out.
2820 map = em->map_lookup;
2821 mutex_lock(&fs_info->chunk_mutex);
2822 check_system_chunk(trans, map->type);
2823 mutex_unlock(&fs_info->chunk_mutex);
2826 * Take the device list mutex to prevent races with the final phase of
2827 * a device replace operation that replaces the device object associated
2828 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2830 mutex_lock(&fs_devices->device_list_mutex);
2831 for (i = 0; i < map->num_stripes; i++) {
2832 struct btrfs_device *device = map->stripes[i].dev;
2833 ret = btrfs_free_dev_extent(trans, device,
2834 map->stripes[i].physical,
2837 mutex_unlock(&fs_devices->device_list_mutex);
2838 btrfs_abort_transaction(trans, ret);
2842 if (device->bytes_used > 0) {
2843 mutex_lock(&fs_info->chunk_mutex);
2844 btrfs_device_set_bytes_used(device,
2845 device->bytes_used - dev_extent_len);
2846 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
2847 btrfs_clear_space_info_full(fs_info);
2848 mutex_unlock(&fs_info->chunk_mutex);
2851 if (map->stripes[i].dev) {
2852 ret = btrfs_update_device(trans, map->stripes[i].dev);
2854 mutex_unlock(&fs_devices->device_list_mutex);
2855 btrfs_abort_transaction(trans, ret);
2860 mutex_unlock(&fs_devices->device_list_mutex);
2862 ret = btrfs_free_chunk(trans, chunk_offset);
2864 btrfs_abort_transaction(trans, ret);
2868 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
2870 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2871 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
2873 btrfs_abort_transaction(trans, ret);
2878 ret = btrfs_remove_block_group(trans, chunk_offset, em);
2880 btrfs_abort_transaction(trans, ret);
2886 free_extent_map(em);
2890 static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2892 struct btrfs_root *root = fs_info->chunk_root;
2893 struct btrfs_trans_handle *trans;
2897 * Prevent races with automatic removal of unused block groups.
2898 * After we relocate and before we remove the chunk with offset
2899 * chunk_offset, automatic removal of the block group can kick in,
2900 * resulting in a failure when calling btrfs_remove_chunk() below.
2902 * Make sure to acquire this mutex before doing a tree search (dev
2903 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2904 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2905 * we release the path used to search the chunk/dev tree and before
2906 * the current task acquires this mutex and calls us.
2908 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
2910 ret = btrfs_can_relocate(fs_info, chunk_offset);
2914 /* step one, relocate all the extents inside this chunk */
2915 btrfs_scrub_pause(fs_info);
2916 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
2917 btrfs_scrub_continue(fs_info);
2922 * We add the kobjects here (and after forcing data chunk creation)
2923 * since relocation is the only place we'll create chunks of a new
2924 * type at runtime. The only place where we'll remove the last
2925 * chunk of a type is the call immediately below this one. Even
2926 * so, we're protected against races with the cleaner thread since
2927 * we're covered by the delete_unused_bgs_mutex.
2929 btrfs_add_raid_kobjects(fs_info);
2931 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2933 if (IS_ERR(trans)) {
2934 ret = PTR_ERR(trans);
2935 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2940 * step two, delete the device extents and the
2941 * chunk tree entries
2943 ret = btrfs_remove_chunk(trans, chunk_offset);
2944 btrfs_end_transaction(trans);
2948 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
2950 struct btrfs_root *chunk_root = fs_info->chunk_root;
2951 struct btrfs_path *path;
2952 struct extent_buffer *leaf;
2953 struct btrfs_chunk *chunk;
2954 struct btrfs_key key;
2955 struct btrfs_key found_key;
2957 bool retried = false;
2961 path = btrfs_alloc_path();
2966 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2967 key.offset = (u64)-1;
2968 key.type = BTRFS_CHUNK_ITEM_KEY;
2971 mutex_lock(&fs_info->delete_unused_bgs_mutex);
2972 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2974 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2977 BUG_ON(ret == 0); /* Corruption */
2979 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2982 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2988 leaf = path->nodes[0];
2989 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2991 chunk = btrfs_item_ptr(leaf, path->slots[0],
2992 struct btrfs_chunk);
2993 chunk_type = btrfs_chunk_type(leaf, chunk);
2994 btrfs_release_path(path);
2996 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2997 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3003 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3005 if (found_key.offset == 0)
3007 key.offset = found_key.offset - 1;
3010 if (failed && !retried) {
3014 } else if (WARN_ON(failed && retried)) {
3018 btrfs_free_path(path);
3023 * return 1 : allocate a data chunk successfully,
3024 * return <0: errors during allocating a data chunk,
3025 * return 0 : no need to allocate a data chunk.
3027 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3030 struct btrfs_block_group_cache *cache;
3034 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3036 chunk_type = cache->flags;
3037 btrfs_put_block_group(cache);
3039 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3040 spin_lock(&fs_info->data_sinfo->lock);
3041 bytes_used = fs_info->data_sinfo->bytes_used;
3042 spin_unlock(&fs_info->data_sinfo->lock);
3045 struct btrfs_trans_handle *trans;
3048 trans = btrfs_join_transaction(fs_info->tree_root);
3050 return PTR_ERR(trans);
3052 ret = btrfs_force_chunk_alloc(trans,
3053 BTRFS_BLOCK_GROUP_DATA);
3054 btrfs_end_transaction(trans);
3058 btrfs_add_raid_kobjects(fs_info);
3066 static int insert_balance_item(struct btrfs_fs_info *fs_info,
3067 struct btrfs_balance_control *bctl)
3069 struct btrfs_root *root = fs_info->tree_root;
3070 struct btrfs_trans_handle *trans;
3071 struct btrfs_balance_item *item;
3072 struct btrfs_disk_balance_args disk_bargs;
3073 struct btrfs_path *path;
3074 struct extent_buffer *leaf;
3075 struct btrfs_key key;
3078 path = btrfs_alloc_path();
3082 trans = btrfs_start_transaction(root, 0);
3083 if (IS_ERR(trans)) {
3084 btrfs_free_path(path);
3085 return PTR_ERR(trans);
3088 key.objectid = BTRFS_BALANCE_OBJECTID;
3089 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3092 ret = btrfs_insert_empty_item(trans, root, path, &key,
3097 leaf = path->nodes[0];
3098 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3100 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
3102 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3103 btrfs_set_balance_data(leaf, item, &disk_bargs);
3104 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3105 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3106 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3107 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3109 btrfs_set_balance_flags(leaf, item, bctl->flags);
3111 btrfs_mark_buffer_dirty(leaf);
3113 btrfs_free_path(path);
3114 err = btrfs_commit_transaction(trans);
3120 static int del_balance_item(struct btrfs_fs_info *fs_info)
3122 struct btrfs_root *root = fs_info->tree_root;
3123 struct btrfs_trans_handle *trans;
3124 struct btrfs_path *path;
3125 struct btrfs_key key;
3128 path = btrfs_alloc_path();
3132 trans = btrfs_start_transaction(root, 0);
3133 if (IS_ERR(trans)) {
3134 btrfs_free_path(path);
3135 return PTR_ERR(trans);
3138 key.objectid = BTRFS_BALANCE_OBJECTID;
3139 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3142 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3150 ret = btrfs_del_item(trans, root, path);
3152 btrfs_free_path(path);
3153 err = btrfs_commit_transaction(trans);
3160 * This is a heuristic used to reduce the number of chunks balanced on
3161 * resume after balance was interrupted.
3163 static void update_balance_args(struct btrfs_balance_control *bctl)
3166 * Turn on soft mode for chunk types that were being converted.
3168 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3169 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3170 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3171 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3172 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3173 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3176 * Turn on usage filter if is not already used. The idea is
3177 * that chunks that we have already balanced should be
3178 * reasonably full. Don't do it for chunks that are being
3179 * converted - that will keep us from relocating unconverted
3180 * (albeit full) chunks.
3182 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3183 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3184 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3185 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3186 bctl->data.usage = 90;
3188 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3189 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3190 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3191 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3192 bctl->sys.usage = 90;
3194 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3195 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3196 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3197 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3198 bctl->meta.usage = 90;
3203 * Clear the balance status in fs_info and delete the balance item from disk.
3205 static void reset_balance_state(struct btrfs_fs_info *fs_info)
3207 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3210 BUG_ON(!fs_info->balance_ctl);
3212 spin_lock(&fs_info->balance_lock);
3213 fs_info->balance_ctl = NULL;
3214 spin_unlock(&fs_info->balance_lock);
3217 ret = del_balance_item(fs_info);
3219 btrfs_handle_fs_error(fs_info, ret, NULL);
3223 * Balance filters. Return 1 if chunk should be filtered out
3224 * (should not be balanced).
3226 static int chunk_profiles_filter(u64 chunk_type,
3227 struct btrfs_balance_args *bargs)
3229 chunk_type = chunk_to_extended(chunk_type) &
3230 BTRFS_EXTENDED_PROFILE_MASK;
3232 if (bargs->profiles & chunk_type)
3238 static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3239 struct btrfs_balance_args *bargs)
3241 struct btrfs_block_group_cache *cache;