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;
134 * Fill @buf with textual description of @bg_flags, no more than @size_buf
135 * bytes including terminating null byte.
137 void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
142 u64 flags = bg_flags;
143 u32 size_bp = size_buf;
150 #define DESCRIBE_FLAG(flag, desc) \
152 if (flags & (flag)) { \
153 ret = snprintf(bp, size_bp, "%s|", (desc)); \
154 if (ret < 0 || ret >= size_bp) \
162 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
163 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
164 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
166 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
167 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
168 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
169 btrfs_raid_array[i].raid_name);
173 ret = snprintf(bp, size_bp, "0x%llx|", flags);
177 if (size_bp < size_buf)
178 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
181 * The text is trimmed, it's up to the caller to provide sufficiently
187 static int init_first_rw_device(struct btrfs_trans_handle *trans,
188 struct btrfs_fs_info *fs_info);
189 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
190 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
191 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
192 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
193 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
194 enum btrfs_map_op op,
195 u64 logical, u64 *length,
196 struct btrfs_bio **bbio_ret,
197 int mirror_num, int need_raid_map);
203 * There are several mutexes that protect manipulation of devices and low-level
204 * structures like chunks but not block groups, extents or files
206 * uuid_mutex (global lock)
207 * ------------------------
208 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
209 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
210 * device) or requested by the device= mount option
212 * the mutex can be very coarse and can cover long-running operations
214 * protects: updates to fs_devices counters like missing devices, rw devices,
215 * seeding, structure cloning, opening/closing devices at mount/umount time
217 * global::fs_devs - add, remove, updates to the global list
219 * does not protect: manipulation of the fs_devices::devices list!
221 * btrfs_device::name - renames (write side), read is RCU
223 * fs_devices::device_list_mutex (per-fs, with RCU)
224 * ------------------------------------------------
225 * protects updates to fs_devices::devices, ie. adding and deleting
227 * simple list traversal with read-only actions can be done with RCU protection
229 * may be used to exclude some operations from running concurrently without any
230 * modifications to the list (see write_all_supers)
234 * protects balance structures (status, state) and context accessed from
235 * several places (internally, ioctl)
239 * protects chunks, adding or removing during allocation, trim or when a new
240 * device is added/removed
244 * a big lock that is held by the cleaner thread and prevents running subvolume
245 * cleaning together with relocation or delayed iputs
258 * Exclusive operations, BTRFS_FS_EXCL_OP
259 * ======================================
261 * Maintains the exclusivity of the following operations that apply to the
262 * whole filesystem and cannot run in parallel.
267 * - Device replace (*)
270 * The device operations (as above) can be in one of the following states:
276 * Only device operations marked with (*) can go into the Paused state for the
279 * - ioctl (only Balance can be Paused through ioctl)
280 * - filesystem remounted as read-only
281 * - filesystem unmounted and mounted as read-only
282 * - system power-cycle and filesystem mounted as read-only
283 * - filesystem or device errors leading to forced read-only
285 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
286 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
287 * A device operation in Paused or Running state can be canceled or resumed
288 * either by ioctl (Balance only) or when remounted as read-write.
289 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
293 DEFINE_MUTEX(uuid_mutex);
294 static LIST_HEAD(fs_uuids);
295 struct list_head *btrfs_get_fs_uuids(void)
301 * alloc_fs_devices - allocate struct btrfs_fs_devices
302 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
303 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
305 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
306 * The returned struct is not linked onto any lists and can be destroyed with
307 * kfree() right away.
309 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
310 const u8 *metadata_fsid)
312 struct btrfs_fs_devices *fs_devs;
314 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
316 return ERR_PTR(-ENOMEM);
318 mutex_init(&fs_devs->device_list_mutex);
320 INIT_LIST_HEAD(&fs_devs->devices);
321 INIT_LIST_HEAD(&fs_devs->resized_devices);
322 INIT_LIST_HEAD(&fs_devs->alloc_list);
323 INIT_LIST_HEAD(&fs_devs->fs_list);
325 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
328 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
330 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
335 void btrfs_free_device(struct btrfs_device *device)
337 rcu_string_free(device->name);
338 bio_put(device->flush_bio);
342 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
344 struct btrfs_device *device;
345 WARN_ON(fs_devices->opened);
346 while (!list_empty(&fs_devices->devices)) {
347 device = list_entry(fs_devices->devices.next,
348 struct btrfs_device, dev_list);
349 list_del(&device->dev_list);
350 btrfs_free_device(device);
355 static void btrfs_kobject_uevent(struct block_device *bdev,
356 enum kobject_action action)
360 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
362 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
364 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
365 &disk_to_dev(bdev->bd_disk)->kobj);
368 void __exit btrfs_cleanup_fs_uuids(void)
370 struct btrfs_fs_devices *fs_devices;
372 while (!list_empty(&fs_uuids)) {
373 fs_devices = list_entry(fs_uuids.next,
374 struct btrfs_fs_devices, fs_list);
375 list_del(&fs_devices->fs_list);
376 free_fs_devices(fs_devices);
381 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
382 * Returned struct is not linked onto any lists and must be destroyed using
385 static struct btrfs_device *__alloc_device(void)
387 struct btrfs_device *dev;
389 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
391 return ERR_PTR(-ENOMEM);
394 * Preallocate a bio that's always going to be used for flushing device
395 * barriers and matches the device lifespan
397 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
398 if (!dev->flush_bio) {
400 return ERR_PTR(-ENOMEM);
403 INIT_LIST_HEAD(&dev->dev_list);
404 INIT_LIST_HEAD(&dev->dev_alloc_list);
405 INIT_LIST_HEAD(&dev->resized_list);
407 spin_lock_init(&dev->io_lock);
409 atomic_set(&dev->reada_in_flight, 0);
410 atomic_set(&dev->dev_stats_ccnt, 0);
411 btrfs_device_data_ordered_init(dev);
412 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
413 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
418 static noinline struct btrfs_fs_devices *find_fsid(
419 const u8 *fsid, const u8 *metadata_fsid)
421 struct btrfs_fs_devices *fs_devices;
427 * Handle scanned device having completed its fsid change but
428 * belonging to a fs_devices that was created by first scanning
429 * a device which didn't have its fsid/metadata_uuid changed
430 * at all and the CHANGING_FSID_V2 flag set.
432 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
433 if (fs_devices->fsid_change &&
434 memcmp(metadata_fsid, fs_devices->fsid,
435 BTRFS_FSID_SIZE) == 0 &&
436 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
437 BTRFS_FSID_SIZE) == 0) {
442 * Handle scanned device having completed its fsid change but
443 * belonging to a fs_devices that was created by a device that
444 * has an outdated pair of fsid/metadata_uuid and
445 * CHANGING_FSID_V2 flag set.
447 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
448 if (fs_devices->fsid_change &&
449 memcmp(fs_devices->metadata_uuid,
450 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
451 memcmp(metadata_fsid, fs_devices->metadata_uuid,
452 BTRFS_FSID_SIZE) == 0) {
458 /* Handle non-split brain cases */
459 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
461 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
462 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
463 BTRFS_FSID_SIZE) == 0)
466 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
474 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
475 int flush, struct block_device **bdev,
476 struct buffer_head **bh)
480 *bdev = blkdev_get_by_path(device_path, flags, holder);
483 ret = PTR_ERR(*bdev);
488 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
489 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
491 blkdev_put(*bdev, flags);
494 invalidate_bdev(*bdev);
495 *bh = btrfs_read_dev_super(*bdev);
498 blkdev_put(*bdev, flags);
510 static void requeue_list(struct btrfs_pending_bios *pending_bios,
511 struct bio *head, struct bio *tail)
514 struct bio *old_head;
516 old_head = pending_bios->head;
517 pending_bios->head = head;
518 if (pending_bios->tail)
519 tail->bi_next = old_head;
521 pending_bios->tail = tail;
525 * we try to collect pending bios for a device so we don't get a large
526 * number of procs sending bios down to the same device. This greatly
527 * improves the schedulers ability to collect and merge the bios.
529 * But, it also turns into a long list of bios to process and that is sure
530 * to eventually make the worker thread block. The solution here is to
531 * make some progress and then put this work struct back at the end of
532 * the list if the block device is congested. This way, multiple devices
533 * can make progress from a single worker thread.
535 static noinline void run_scheduled_bios(struct btrfs_device *device)
537 struct btrfs_fs_info *fs_info = device->fs_info;
539 struct backing_dev_info *bdi;
540 struct btrfs_pending_bios *pending_bios;
544 unsigned long num_run;
545 unsigned long batch_run = 0;
546 unsigned long last_waited = 0;
548 int sync_pending = 0;
549 struct blk_plug plug;
552 * this function runs all the bios we've collected for
553 * a particular device. We don't want to wander off to
554 * another device without first sending all of these down.
555 * So, setup a plug here and finish it off before we return
557 blk_start_plug(&plug);
559 bdi = device->bdev->bd_bdi;
562 spin_lock(&device->io_lock);
567 /* take all the bios off the list at once and process them
568 * later on (without the lock held). But, remember the
569 * tail and other pointers so the bios can be properly reinserted
570 * into the list if we hit congestion
572 if (!force_reg && device->pending_sync_bios.head) {
573 pending_bios = &device->pending_sync_bios;
576 pending_bios = &device->pending_bios;
580 pending = pending_bios->head;
581 tail = pending_bios->tail;
582 WARN_ON(pending && !tail);
585 * if pending was null this time around, no bios need processing
586 * at all and we can stop. Otherwise it'll loop back up again
587 * and do an additional check so no bios are missed.
589 * device->running_pending is used to synchronize with the
592 if (device->pending_sync_bios.head == NULL &&
593 device->pending_bios.head == NULL) {
595 device->running_pending = 0;
598 device->running_pending = 1;
601 pending_bios->head = NULL;
602 pending_bios->tail = NULL;
604 spin_unlock(&device->io_lock);
609 /* we want to work on both lists, but do more bios on the
610 * sync list than the regular list
613 pending_bios != &device->pending_sync_bios &&
614 device->pending_sync_bios.head) ||
615 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
616 device->pending_bios.head)) {
617 spin_lock(&device->io_lock);
618 requeue_list(pending_bios, pending, tail);
623 pending = pending->bi_next;
626 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
629 * if we're doing the sync list, record that our
630 * plug has some sync requests on it
632 * If we're doing the regular list and there are
633 * sync requests sitting around, unplug before
636 if (pending_bios == &device->pending_sync_bios) {
638 } else if (sync_pending) {
639 blk_finish_plug(&plug);
640 blk_start_plug(&plug);
644 btrfsic_submit_bio(cur);
651 * we made progress, there is more work to do and the bdi
652 * is now congested. Back off and let other work structs
655 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
656 fs_info->fs_devices->open_devices > 1) {
657 struct io_context *ioc;
659 ioc = current->io_context;
662 * the main goal here is that we don't want to
663 * block if we're going to be able to submit
664 * more requests without blocking.
666 * This code does two great things, it pokes into
667 * the elevator code from a filesystem _and_
668 * it makes assumptions about how batching works.
670 if (ioc && ioc->nr_batch_requests > 0 &&
671 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
673 ioc->last_waited == last_waited)) {
675 * we want to go through our batch of
676 * requests and stop. So, we copy out
677 * the ioc->last_waited time and test
678 * against it before looping
680 last_waited = ioc->last_waited;
684 spin_lock(&device->io_lock);
685 requeue_list(pending_bios, pending, tail);
686 device->running_pending = 1;
688 spin_unlock(&device->io_lock);
689 btrfs_queue_work(fs_info->submit_workers,
699 spin_lock(&device->io_lock);
700 if (device->pending_bios.head || device->pending_sync_bios.head)
702 spin_unlock(&device->io_lock);
705 blk_finish_plug(&plug);
708 static void pending_bios_fn(struct btrfs_work *work)
710 struct btrfs_device *device;
712 device = container_of(work, struct btrfs_device, work);
713 run_scheduled_bios(device);
716 static bool device_path_matched(const char *path, struct btrfs_device *device)
721 found = strcmp(rcu_str_deref(device->name), path);
728 * Search and remove all stale (devices which are not mounted) devices.
729 * When both inputs are NULL, it will search and release all stale devices.
730 * path: Optional. When provided will it release all unmounted devices
731 * matching this path only.
732 * skip_dev: Optional. Will skip this device when searching for the stale
734 * Return: 0 for success or if @path is NULL.
735 * -EBUSY if @path is a mounted device.
736 * -ENOENT if @path does not match any device in the list.
738 static int btrfs_free_stale_devices(const char *path,
739 struct btrfs_device *skip_device)
741 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
742 struct btrfs_device *device, *tmp_device;
748 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
750 mutex_lock(&fs_devices->device_list_mutex);
751 list_for_each_entry_safe(device, tmp_device,
752 &fs_devices->devices, dev_list) {
753 if (skip_device && skip_device == device)
755 if (path && !device->name)
757 if (path && !device_path_matched(path, device))
759 if (fs_devices->opened) {
760 /* for an already deleted device return 0 */
761 if (path && ret != 0)
766 /* delete the stale device */
767 fs_devices->num_devices--;
768 list_del(&device->dev_list);
769 btrfs_free_device(device);
772 if (fs_devices->num_devices == 0)
775 mutex_unlock(&fs_devices->device_list_mutex);
777 if (fs_devices->num_devices == 0) {
778 btrfs_sysfs_remove_fsid(fs_devices);
779 list_del(&fs_devices->fs_list);
780 free_fs_devices(fs_devices);
787 static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
788 struct btrfs_device *device, fmode_t flags,
791 struct request_queue *q;
792 struct block_device *bdev;
793 struct buffer_head *bh;
794 struct btrfs_super_block *disk_super;
803 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
808 disk_super = (struct btrfs_super_block *)bh->b_data;
809 devid = btrfs_stack_device_id(&disk_super->dev_item);
810 if (devid != device->devid)
813 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
816 device->generation = btrfs_super_generation(disk_super);
818 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
819 if (btrfs_super_incompat_flags(disk_super) &
820 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
822 "BTRFS: Invalid seeding and uuid-changed device detected\n");
826 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
827 fs_devices->seeding = 1;
829 if (bdev_read_only(bdev))
830 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
832 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
835 q = bdev_get_queue(bdev);
836 if (!blk_queue_nonrot(q))
837 fs_devices->rotating = 1;
840 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
841 device->mode = flags;
843 fs_devices->open_devices++;
844 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
845 device->devid != BTRFS_DEV_REPLACE_DEVID) {
846 fs_devices->rw_devices++;
847 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
855 blkdev_put(bdev, flags);
861 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
862 * being created with a disk that has already completed its fsid change.
864 static struct btrfs_fs_devices *find_fsid_inprogress(
865 struct btrfs_super_block *disk_super)
867 struct btrfs_fs_devices *fs_devices;
869 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
870 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
871 BTRFS_FSID_SIZE) != 0 &&
872 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
873 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
882 static struct btrfs_fs_devices *find_fsid_changed(
883 struct btrfs_super_block *disk_super)
885 struct btrfs_fs_devices *fs_devices;
888 * Handles the case where scanned device is part of an fs that had
889 * multiple successful changes of FSID but curently device didn't
890 * observe it. Meaning our fsid will be different than theirs.
892 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
893 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
894 BTRFS_FSID_SIZE) != 0 &&
895 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
896 BTRFS_FSID_SIZE) == 0 &&
897 memcmp(fs_devices->fsid, disk_super->fsid,
898 BTRFS_FSID_SIZE) != 0) {
906 * Add new device to list of registered devices
909 * device pointer which was just added or updated when successful
910 * error pointer when failed
912 static noinline struct btrfs_device *device_list_add(const char *path,
913 struct btrfs_super_block *disk_super,
914 bool *new_device_added)
916 struct btrfs_device *device;
917 struct btrfs_fs_devices *fs_devices = NULL;
918 struct rcu_string *name;
919 u64 found_transid = btrfs_super_generation(disk_super);
920 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
921 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
922 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
923 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
924 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
926 if (fsid_change_in_progress) {
927 if (!has_metadata_uuid) {
929 * When we have an image which has CHANGING_FSID_V2 set
930 * it might belong to either a filesystem which has
931 * disks with completed fsid change or it might belong
932 * to fs with no UUID changes in effect, handle both.
934 fs_devices = find_fsid_inprogress(disk_super);
936 fs_devices = find_fsid(disk_super->fsid, NULL);
938 fs_devices = find_fsid_changed(disk_super);
940 } else if (has_metadata_uuid) {
941 fs_devices = find_fsid(disk_super->fsid,
942 disk_super->metadata_uuid);
944 fs_devices = find_fsid(disk_super->fsid, NULL);
949 if (has_metadata_uuid)
950 fs_devices = alloc_fs_devices(disk_super->fsid,
951 disk_super->metadata_uuid);
953 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
955 if (IS_ERR(fs_devices))
956 return ERR_CAST(fs_devices);
958 fs_devices->fsid_change = fsid_change_in_progress;
960 mutex_lock(&fs_devices->device_list_mutex);
961 list_add(&fs_devices->fs_list, &fs_uuids);
965 mutex_lock(&fs_devices->device_list_mutex);
966 device = btrfs_find_device(fs_devices, devid,
967 disk_super->dev_item.uuid, NULL, false);
970 * If this disk has been pulled into an fs devices created by
971 * a device which had the CHANGING_FSID_V2 flag then replace the
972 * metadata_uuid/fsid values of the fs_devices.
974 if (has_metadata_uuid && fs_devices->fsid_change &&
975 found_transid > fs_devices->latest_generation) {
976 memcpy(fs_devices->fsid, disk_super->fsid,
978 memcpy(fs_devices->metadata_uuid,
979 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
981 fs_devices->fsid_change = false;
986 if (fs_devices->opened) {
987 mutex_unlock(&fs_devices->device_list_mutex);
988 return ERR_PTR(-EBUSY);
991 device = btrfs_alloc_device(NULL, &devid,
992 disk_super->dev_item.uuid);
993 if (IS_ERR(device)) {
994 mutex_unlock(&fs_devices->device_list_mutex);
995 /* we can safely leave the fs_devices entry around */
999 name = rcu_string_strdup(path, GFP_NOFS);
1001 btrfs_free_device(device);
1002 mutex_unlock(&fs_devices->device_list_mutex);
1003 return ERR_PTR(-ENOMEM);
1005 rcu_assign_pointer(device->name, name);
1007 list_add_rcu(&device->dev_list, &fs_devices->devices);
1008 fs_devices->num_devices++;
1010 device->fs_devices = fs_devices;
1011 *new_device_added = true;
1013 if (disk_super->label[0])
1014 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
1015 disk_super->label, devid, found_transid, path);
1017 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
1018 disk_super->fsid, devid, found_transid, path);
1020 } else if (!device->name || strcmp(device->name->str, path)) {
1022 * When FS is already mounted.
1023 * 1. If you are here and if the device->name is NULL that
1024 * means this device was missing at time of FS mount.
1025 * 2. If you are here and if the device->name is different
1026 * from 'path' that means either
1027 * a. The same device disappeared and reappeared with
1028 * different name. or
1029 * b. The missing-disk-which-was-replaced, has
1032 * We must allow 1 and 2a above. But 2b would be a spurious
1033 * and unintentional.
1035 * Further in case of 1 and 2a above, the disk at 'path'
1036 * would have missed some transaction when it was away and
1037 * in case of 2a the stale bdev has to be updated as well.
1038 * 2b must not be allowed at all time.
1042 * For now, we do allow update to btrfs_fs_device through the
1043 * btrfs dev scan cli after FS has been mounted. We're still
1044 * tracking a problem where systems fail mount by subvolume id
1045 * when we reject replacement on a mounted FS.
1047 if (!fs_devices->opened && found_transid < device->generation) {
1049 * That is if the FS is _not_ mounted and if you
1050 * are here, that means there is more than one
1051 * disk with same uuid and devid.We keep the one
1052 * with larger generation number or the last-in if
1053 * generation are equal.
1055 mutex_unlock(&fs_devices->device_list_mutex);
1056 return ERR_PTR(-EEXIST);
1060 * We are going to replace the device path for a given devid,
1061 * make sure it's the same device if the device is mounted
1064 struct block_device *path_bdev;
1066 path_bdev = lookup_bdev(path);
1067 if (IS_ERR(path_bdev)) {
1068 mutex_unlock(&fs_devices->device_list_mutex);
1069 return ERR_CAST(path_bdev);
1072 if (device->bdev != path_bdev) {
1074 mutex_unlock(&fs_devices->device_list_mutex);
1075 btrfs_warn_in_rcu(device->fs_info,
1076 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
1077 disk_super->fsid, devid,
1078 rcu_str_deref(device->name), path);
1079 return ERR_PTR(-EEXIST);
1082 btrfs_info_in_rcu(device->fs_info,
1083 "device fsid %pU devid %llu moved old:%s new:%s",
1084 disk_super->fsid, devid,
1085 rcu_str_deref(device->name), path);
1088 name = rcu_string_strdup(path, GFP_NOFS);
1090 mutex_unlock(&fs_devices->device_list_mutex);
1091 return ERR_PTR(-ENOMEM);
1093 rcu_string_free(device->name);
1094 rcu_assign_pointer(device->name, name);
1095 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
1096 fs_devices->missing_devices--;
1097 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1102 * Unmount does not free the btrfs_device struct but would zero
1103 * generation along with most of the other members. So just update
1104 * it back. We need it to pick the disk with largest generation
1107 if (!fs_devices->opened) {
1108 device->generation = found_transid;
1109 fs_devices->latest_generation = max_t(u64, found_transid,
1110 fs_devices->latest_generation);
1113 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
1115 mutex_unlock(&fs_devices->device_list_mutex);
1119 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
1121 struct btrfs_fs_devices *fs_devices;
1122 struct btrfs_device *device;
1123 struct btrfs_device *orig_dev;
1125 fs_devices = alloc_fs_devices(orig->fsid, NULL);
1126 if (IS_ERR(fs_devices))
1129 mutex_lock(&orig->device_list_mutex);
1130 fs_devices->total_devices = orig->total_devices;
1132 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
1133 struct rcu_string *name;
1135 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1141 * This is ok to do without rcu read locked because we hold the
1142 * uuid mutex so nothing we touch in here is going to disappear.
1144 if (orig_dev->name) {
1145 name = rcu_string_strdup(orig_dev->name->str,
1148 btrfs_free_device(device);
1151 rcu_assign_pointer(device->name, name);
1154 list_add(&device->dev_list, &fs_devices->devices);
1155 device->fs_devices = fs_devices;
1156 fs_devices->num_devices++;
1158 mutex_unlock(&orig->device_list_mutex);
1161 mutex_unlock(&orig->device_list_mutex);
1162 free_fs_devices(fs_devices);
1163 return ERR_PTR(-ENOMEM);
1167 * After we have read the system tree and know devids belonging to
1168 * this filesystem, remove the device which does not belong there.
1170 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
1172 struct btrfs_device *device, *next;
1173 struct btrfs_device *latest_dev = NULL;
1175 mutex_lock(&uuid_mutex);
1177 /* This is the initialized path, it is safe to release the devices. */
1178 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
1179 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1180 &device->dev_state)) {
1181 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1182 &device->dev_state) &&
1184 device->generation > latest_dev->generation)) {
1185 latest_dev = device;
1190 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1192 * In the first step, keep the device which has
1193 * the correct fsid and the devid that is used
1194 * for the dev_replace procedure.
1195 * In the second step, the dev_replace state is
1196 * read from the device tree and it is known
1197 * whether the procedure is really active or
1198 * not, which means whether this device is
1199 * used or whether it should be removed.
1201 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1202 &device->dev_state)) {
1207 blkdev_put(device->bdev, device->mode);
1208 device->bdev = NULL;
1209 fs_devices->open_devices--;
1211 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1212 list_del_init(&device->dev_alloc_list);
1213 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1214 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1215 &device->dev_state))
1216 fs_devices->rw_devices--;
1218 list_del_init(&device->dev_list);
1219 fs_devices->num_devices--;
1220 btrfs_free_device(device);
1223 if (fs_devices->seed) {
1224 fs_devices = fs_devices->seed;
1228 fs_devices->latest_bdev = latest_dev->bdev;
1230 mutex_unlock(&uuid_mutex);
1233 static void free_device_rcu(struct rcu_head *head)
1235 struct btrfs_device *device;
1237 device = container_of(head, struct btrfs_device, rcu);
1238 btrfs_free_device(device);
1241 static void btrfs_close_bdev(struct btrfs_device *device)
1246 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1247 sync_blockdev(device->bdev);
1248 invalidate_bdev(device->bdev);
1251 blkdev_put(device->bdev, device->mode);
1254 static void btrfs_close_one_device(struct btrfs_device *device)
1256 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1257 struct btrfs_device *new_device;
1258 struct rcu_string *name;
1261 fs_devices->open_devices--;
1263 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1264 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1265 list_del_init(&device->dev_alloc_list);
1266 fs_devices->rw_devices--;
1269 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
1270 fs_devices->missing_devices--;
1272 btrfs_close_bdev(device);
1274 new_device = btrfs_alloc_device(NULL, &device->devid,
1276 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1278 /* Safe because we are under uuid_mutex */
1280 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1281 BUG_ON(!name); /* -ENOMEM */
1282 rcu_assign_pointer(new_device->name, name);
1285 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1286 new_device->fs_devices = device->fs_devices;
1288 call_rcu(&device->rcu, free_device_rcu);
1291 static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
1293 struct btrfs_device *device, *tmp;
1295 if (--fs_devices->opened > 0)
1298 mutex_lock(&fs_devices->device_list_mutex);
1299 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
1300 btrfs_close_one_device(device);
1302 mutex_unlock(&fs_devices->device_list_mutex);
1304 WARN_ON(fs_devices->open_devices);
1305 WARN_ON(fs_devices->rw_devices);
1306 fs_devices->opened = 0;
1307 fs_devices->seeding = 0;
1312 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1314 struct btrfs_fs_devices *seed_devices = NULL;
1317 mutex_lock(&uuid_mutex);
1318 ret = close_fs_devices(fs_devices);
1319 if (!fs_devices->opened) {
1320 seed_devices = fs_devices->seed;
1321 fs_devices->seed = NULL;
1323 mutex_unlock(&uuid_mutex);
1325 while (seed_devices) {
1326 fs_devices = seed_devices;
1327 seed_devices = fs_devices->seed;
1328 close_fs_devices(fs_devices);
1329 free_fs_devices(fs_devices);
1334 static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
1335 fmode_t flags, void *holder)
1337 struct btrfs_device *device;
1338 struct btrfs_device *latest_dev = NULL;
1341 flags |= FMODE_EXCL;
1343 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1344 /* Just open everything we can; ignore failures here */
1345 if (btrfs_open_one_device(fs_devices, device, flags, holder))
1349 device->generation > latest_dev->generation)
1350 latest_dev = device;
1352 if (fs_devices->open_devices == 0) {
1356 fs_devices->opened = 1;
1357 fs_devices->latest_bdev = latest_dev->bdev;
1358 fs_devices->total_rw_bytes = 0;
1363 static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1365 struct btrfs_device *dev1, *dev2;
1367 dev1 = list_entry(a, struct btrfs_device, dev_list);
1368 dev2 = list_entry(b, struct btrfs_device, dev_list);
1370 if (dev1->devid < dev2->devid)
1372 else if (dev1->devid > dev2->devid)
1377 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1378 fmode_t flags, void *holder)
1382 lockdep_assert_held(&uuid_mutex);
1384 mutex_lock(&fs_devices->device_list_mutex);
1385 if (fs_devices->opened) {
1386 fs_devices->opened++;
1389 list_sort(NULL, &fs_devices->devices, devid_cmp);
1390 ret = open_fs_devices(fs_devices, flags, holder);
1392 mutex_unlock(&fs_devices->device_list_mutex);
1397 static void btrfs_release_disk_super(struct page *page)
1403 static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1405 struct btrfs_super_block **disk_super)
1410 /* make sure our super fits in the device */
1411 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1414 /* make sure our super fits in the page */
1415 if (sizeof(**disk_super) > PAGE_SIZE)
1418 /* make sure our super doesn't straddle pages on disk */
1419 index = bytenr >> PAGE_SHIFT;
1420 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1423 /* pull in the page with our super */
1424 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1427 if (IS_ERR_OR_NULL(*page))
1432 /* align our pointer to the offset of the super block */
1433 *disk_super = p + offset_in_page(bytenr);
1435 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1436 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1437 btrfs_release_disk_super(*page);
1441 if ((*disk_super)->label[0] &&
1442 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1443 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1448 int btrfs_forget_devices(const char *path)
1452 mutex_lock(&uuid_mutex);
1453 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1454 mutex_unlock(&uuid_mutex);
1460 * Look for a btrfs signature on a device. This may be called out of the mount path
1461 * and we are not allowed to call set_blocksize during the scan. The superblock
1462 * is read via pagecache
1464 struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1467 struct btrfs_super_block *disk_super;
1468 bool new_device_added = false;
1469 struct btrfs_device *device = NULL;
1470 struct block_device *bdev;
1474 lockdep_assert_held(&uuid_mutex);
1477 * we would like to check all the supers, but that would make
1478 * a btrfs mount succeed after a mkfs from a different FS.
1479 * So, we need to add a special mount option to scan for
1480 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1482 bytenr = btrfs_sb_offset(0);
1483 flags |= FMODE_EXCL;
1485 bdev = blkdev_get_by_path(path, flags, holder);
1487 return ERR_CAST(bdev);
1489 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1490 device = ERR_PTR(-EINVAL);
1491 goto error_bdev_put;
1494 device = device_list_add(path, disk_super, &new_device_added);
1495 if (!IS_ERR(device)) {
1496 if (new_device_added)
1497 btrfs_free_stale_devices(path, device);
1500 btrfs_release_disk_super(page);
1503 blkdev_put(bdev, flags);
1508 static int contains_pending_extent(struct btrfs_transaction *transaction,
1509 struct btrfs_device *device,
1510 u64 *start, u64 len)
1512 struct btrfs_fs_info *fs_info = device->fs_info;
1513 struct extent_map *em;
1514 struct list_head *search_list = &fs_info->pinned_chunks;
1516 u64 physical_start = *start;
1519 search_list = &transaction->pending_chunks;
1521 list_for_each_entry(em, search_list, list) {
1522 struct map_lookup *map;
1525 map = em->map_lookup;
1526 for (i = 0; i < map->num_stripes; i++) {
1529 if (map->stripes[i].dev != device)
1531 if (map->stripes[i].physical >= physical_start + len ||
1532 map->stripes[i].physical + em->orig_block_len <=
1536 * Make sure that while processing the pinned list we do
1537 * not override our *start with a lower value, because
1538 * we can have pinned chunks that fall within this
1539 * device hole and that have lower physical addresses
1540 * than the pending chunks we processed before. If we
1541 * do not take this special care we can end up getting
1542 * 2 pending chunks that start at the same physical
1543 * device offsets because the end offset of a pinned
1544 * chunk can be equal to the start offset of some
1547 end = map->stripes[i].physical + em->orig_block_len;
1554 if (search_list != &fs_info->pinned_chunks) {
1555 search_list = &fs_info->pinned_chunks;
1564 * find_free_dev_extent_start - find free space in the specified device
1565 * @device: the device which we search the free space in
1566 * @num_bytes: the size of the free space that we need
1567 * @search_start: the position from which to begin the search
1568 * @start: store the start of the free space.
1569 * @len: the size of the free space. that we find, or the size
1570 * of the max free space if we don't find suitable free space
1572 * this uses a pretty simple search, the expectation is that it is
1573 * called very infrequently and that a given device has a small number
1576 * @start is used to store the start of the free space if we find. But if we
1577 * don't find suitable free space, it will be used to store the start position
1578 * of the max free space.
1580 * @len is used to store the size of the free space that we find.
1581 * But if we don't find suitable free space, it is used to store the size of
1582 * the max free space.
1584 int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1585 struct btrfs_device *device, u64 num_bytes,
1586 u64 search_start, u64 *start, u64 *len)
1588 struct btrfs_fs_info *fs_info = device->fs_info;
1589 struct btrfs_root *root = fs_info->dev_root;
1590 struct btrfs_key key;
1591 struct btrfs_dev_extent *dev_extent;
1592 struct btrfs_path *path;
1597 u64 search_end = device->total_bytes;
1600 struct extent_buffer *l;
1603 * We don't want to overwrite the superblock on the drive nor any area
1604 * used by the boot loader (grub for example), so we make sure to start
1605 * at an offset of at least 1MB.
1607 search_start = max_t(u64, search_start, SZ_1M);
1609 path = btrfs_alloc_path();
1613 max_hole_start = search_start;
1617 if (search_start >= search_end ||
1618 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1623 path->reada = READA_FORWARD;
1624 path->search_commit_root = 1;
1625 path->skip_locking = 1;
1627 key.objectid = device->devid;
1628 key.offset = search_start;
1629 key.type = BTRFS_DEV_EXTENT_KEY;
1631 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1635 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1642 slot = path->slots[0];
1643 if (slot >= btrfs_header_nritems(l)) {
1644 ret = btrfs_next_leaf(root, path);
1652 btrfs_item_key_to_cpu(l, &key, slot);
1654 if (key.objectid < device->devid)
1657 if (key.objectid > device->devid)
1660 if (key.type != BTRFS_DEV_EXTENT_KEY)
1663 if (key.offset > search_start) {
1664 hole_size = key.offset - search_start;
1667 * Have to check before we set max_hole_start, otherwise
1668 * we could end up sending back this offset anyway.
1670 if (contains_pending_extent(transaction, device,
1673 if (key.offset >= search_start) {
1674 hole_size = key.offset - search_start;
1681 if (hole_size > max_hole_size) {
1682 max_hole_start = search_start;
1683 max_hole_size = hole_size;
1687 * If this free space is greater than which we need,
1688 * it must be the max free space that we have found
1689 * until now, so max_hole_start must point to the start
1690 * of this free space and the length of this free space
1691 * is stored in max_hole_size. Thus, we return
1692 * max_hole_start and max_hole_size and go back to the
1695 if (hole_size >= num_bytes) {
1701 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1702 extent_end = key.offset + btrfs_dev_extent_length(l,
1704 if (extent_end > search_start)
1705 search_start = extent_end;
1712 * At this point, search_start should be the end of
1713 * allocated dev extents, and when shrinking the device,
1714 * search_end may be smaller than search_start.
1716 if (search_end > search_start) {
1717 hole_size = search_end - search_start;
1719 if (contains_pending_extent(transaction, device, &search_start,
1721 btrfs_release_path(path);
1725 if (hole_size > max_hole_size) {
1726 max_hole_start = search_start;
1727 max_hole_size = hole_size;
1732 if (max_hole_size < num_bytes)
1738 btrfs_free_path(path);
1739 *start = max_hole_start;
1741 *len = max_hole_size;
1745 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1746 struct btrfs_device *device, u64 num_bytes,
1747 u64 *start, u64 *len)
1749 /* FIXME use last free of some kind */
1750 return find_free_dev_extent_start(trans->transaction, device,
1751 num_bytes, 0, start, len);
1754 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1755 struct btrfs_device *device,
1756 u64 start, u64 *dev_extent_len)
1758 struct btrfs_fs_info *fs_info = device->fs_info;
1759 struct btrfs_root *root = fs_info->dev_root;
1761 struct btrfs_path *path;
1762 struct btrfs_key key;
1763 struct btrfs_key found_key;
1764 struct extent_buffer *leaf = NULL;
1765 struct btrfs_dev_extent *extent = NULL;
1767 path = btrfs_alloc_path();
1771 key.objectid = device->devid;
1773 key.type = BTRFS_DEV_EXTENT_KEY;
1775 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1777 ret = btrfs_previous_item(root, path, key.objectid,
1778 BTRFS_DEV_EXTENT_KEY);
1781 leaf = path->nodes[0];
1782 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1783 extent = btrfs_item_ptr(leaf, path->slots[0],
1784 struct btrfs_dev_extent);
1785 BUG_ON(found_key.offset > start || found_key.offset +
1786 btrfs_dev_extent_length(leaf, extent) < start);
1788 btrfs_release_path(path);
1790 } else if (ret == 0) {
1791 leaf = path->nodes[0];
1792 extent = btrfs_item_ptr(leaf, path->slots[0],
1793 struct btrfs_dev_extent);
1795 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
1799 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1801 ret = btrfs_del_item(trans, root, path);
1803 btrfs_handle_fs_error(fs_info, ret,
1804 "Failed to remove dev extent item");
1806 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
1809 btrfs_free_path(path);
1813 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1814 struct btrfs_device *device,
1815 u64 chunk_offset, u64 start, u64 num_bytes)
1818 struct btrfs_path *path;
1819 struct btrfs_fs_info *fs_info = device->fs_info;
1820 struct btrfs_root *root = fs_info->dev_root;
1821 struct btrfs_dev_extent *extent;
1822 struct extent_buffer *leaf;
1823 struct btrfs_key key;
1825 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
1826 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1827 path = btrfs_alloc_path();
1831 key.objectid = device->devid;
1833 key.type = BTRFS_DEV_EXTENT_KEY;
1834 ret = btrfs_insert_empty_item(trans, root, path, &key,
1839 leaf = path->nodes[0];
1840 extent = btrfs_item_ptr(leaf, path->slots[0],
1841 struct btrfs_dev_extent);
1842 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1843 BTRFS_CHUNK_TREE_OBJECTID);
1844 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1845 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1846 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1848 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1849 btrfs_mark_buffer_dirty(leaf);
1851 btrfs_free_path(path);
1855 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1857 struct extent_map_tree *em_tree;
1858 struct extent_map *em;
1862 em_tree = &fs_info->mapping_tree.map_tree;
1863 read_lock(&em_tree->lock);
1864 n = rb_last(&em_tree->map.rb_root);
1866 em = rb_entry(n, struct extent_map, rb_node);
1867 ret = em->start + em->len;
1869 read_unlock(&em_tree->lock);
1874 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1878 struct btrfs_key key;
1879 struct btrfs_key found_key;
1880 struct btrfs_path *path;
1882 path = btrfs_alloc_path();
1886 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1887 key.type = BTRFS_DEV_ITEM_KEY;
1888 key.offset = (u64)-1;
1890 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1894 BUG_ON(ret == 0); /* Corruption */
1896 ret = btrfs_previous_item(fs_info->chunk_root, path,
1897 BTRFS_DEV_ITEMS_OBJECTID,
1898 BTRFS_DEV_ITEM_KEY);
1902 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1904 *devid_ret = found_key.offset + 1;
1908 btrfs_free_path(path);
1913 * the device information is stored in the chunk root
1914 * the btrfs_device struct should be fully filled in
1916 static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
1917 struct btrfs_device *device)
1920 struct btrfs_path *path;
1921 struct btrfs_dev_item *dev_item;
1922 struct extent_buffer *leaf;
1923 struct btrfs_key key;
1926 path = btrfs_alloc_path();
1930 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1931 key.type = BTRFS_DEV_ITEM_KEY;
1932 key.offset = device->devid;
1934 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1935 &key, sizeof(*dev_item));
1939 leaf = path->nodes[0];
1940 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1942 btrfs_set_device_id(leaf, dev_item, device->devid);
1943 btrfs_set_device_generation(leaf, dev_item, 0);
1944 btrfs_set_device_type(leaf, dev_item, device->type);
1945 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1946 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1947 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1948 btrfs_set_device_total_bytes(leaf, dev_item,
1949 btrfs_device_get_disk_total_bytes(device));
1950 btrfs_set_device_bytes_used(leaf, dev_item,
1951 btrfs_device_get_bytes_used(device));
1952 btrfs_set_device_group(leaf, dev_item, 0);
1953 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1954 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1955 btrfs_set_device_start_offset(leaf, dev_item, 0);
1957 ptr = btrfs_device_uuid(dev_item);
1958 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1959 ptr = btrfs_device_fsid(dev_item);
1960 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1961 ptr, BTRFS_FSID_SIZE);
1962 btrfs_mark_buffer_dirty(leaf);
1966 btrfs_free_path(path);
1971 * Function to update ctime/mtime for a given device path.
1972 * Mainly used for ctime/mtime based probe like libblkid.
1974 static void update_dev_time(const char *path_name)
1978 filp = filp_open(path_name, O_RDWR, 0);
1981 file_update_time(filp);
1982 filp_close(filp, NULL);
1985 static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
1986 struct btrfs_device *device)
1988 struct btrfs_root *root = fs_info->chunk_root;
1990 struct btrfs_path *path;
1991 struct btrfs_key key;
1992 struct btrfs_trans_handle *trans;
1994 path = btrfs_alloc_path();
1998 trans = btrfs_start_transaction(root, 0);
1999 if (IS_ERR(trans)) {
2000 btrfs_free_path(path);
2001 return PTR_ERR(trans);
2003 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2004 key.type = BTRFS_DEV_ITEM_KEY;
2005 key.offset = device->devid;
2007 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2011 btrfs_abort_transaction(trans, ret);
2012 btrfs_end_transaction(trans);
2016 ret = btrfs_del_item(trans, root, path);
2018 btrfs_abort_transaction(trans, ret);
2019 btrfs_end_transaction(trans);
2023 btrfs_free_path(path);
2025 ret = btrfs_commit_transaction(trans);
2030 * Verify that @num_devices satisfies the RAID profile constraints in the whole
2031 * filesystem. It's up to the caller to adjust that number regarding eg. device
2034 static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
2042 seq = read_seqbegin(&fs_info->profiles_lock);
2044 all_avail = fs_info->avail_data_alloc_bits |
2045 fs_info->avail_system_alloc_bits |
2046 fs_info->avail_metadata_alloc_bits;
2047 } while (read_seqretry(&fs_info->profiles_lock, seq));
2049 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2050 if (!(all_avail & btrfs_raid_array[i].bg_flag))
2053 if (num_devices < btrfs_raid_array[i].devs_min) {
2054 int ret = btrfs_raid_array[i].mindev_error;
2064 static struct btrfs_device * btrfs_find_next_active_device(
2065 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
2067 struct btrfs_device *next_device;
2069 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
2070 if (next_device != device &&
2071 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
2072 && next_device->bdev)
2080 * Helper function to check if the given device is part of s_bdev / latest_bdev
2081 * and replace it with the provided or the next active device, in the context
2082 * where this function called, there should be always be another device (or
2083 * this_dev) which is active.
2085 void btrfs_assign_next_active_device(struct btrfs_device *device,
2086 struct btrfs_device *this_dev)
2088 struct btrfs_fs_info *fs_info = device->fs_info;
2089 struct btrfs_device *next_device;
2092 next_device = this_dev;
2094 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
2096 ASSERT(next_device);
2098 if (fs_info->sb->s_bdev &&
2099 (fs_info->sb->s_bdev == device->bdev))
2100 fs_info->sb->s_bdev = next_device->bdev;
2102 if (fs_info->fs_devices->latest_bdev == device->bdev)
2103 fs_info->fs_devices->latest_bdev = next_device->bdev;
2107 * Return btrfs_fs_devices::num_devices excluding the device that's being
2108 * currently replaced.
2110 static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
2112 u64 num_devices = fs_info->fs_devices->num_devices;
2114 down_read(&fs_info->dev_replace.rwsem);
2115 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
2116 ASSERT(num_devices > 1);
2119 up_read(&fs_info->dev_replace.rwsem);
2124 int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2127 struct btrfs_device *device;
2128 struct btrfs_fs_devices *cur_devices;
2129 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2133 mutex_lock(&uuid_mutex);
2135 num_devices = btrfs_num_devices(fs_info);
2137 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
2141 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2143 if (IS_ERR(device)) {
2144 if (PTR_ERR(device) == -ENOENT &&
2145 strcmp(device_path, "missing") == 0)
2146 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2148 ret = PTR_ERR(device);
2152 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2153 btrfs_warn_in_rcu(fs_info,
2154 "cannot remove device %s (devid %llu) due to active swapfile",
2155 rcu_str_deref(device->name), device->devid);
2160 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2161 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
2165 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2166 fs_info->fs_devices->rw_devices == 1) {
2167 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
2171 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2172 mutex_lock(&fs_info->chunk_mutex);
2173 list_del_init(&device->dev_alloc_list);
2174 device->fs_devices->rw_devices--;
2175 mutex_unlock(&fs_info->chunk_mutex);
2178 mutex_unlock(&uuid_mutex);
2179 ret = btrfs_shrink_device(device, 0);
2180 mutex_lock(&uuid_mutex);
2185 * TODO: the superblock still includes this device in its num_devices
2186 * counter although write_all_supers() is not locked out. This
2187 * could give a filesystem state which requires a degraded mount.
2189 ret = btrfs_rm_dev_item(fs_info, device);
2193 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2194 btrfs_scrub_cancel_dev(fs_info, device);
2197 * the device list mutex makes sure that we don't change
2198 * the device list while someone else is writing out all
2199 * the device supers. Whoever is writing all supers, should
2200 * lock the device list mutex before getting the number of
2201 * devices in the super block (super_copy). Conversely,
2202 * whoever updates the number of devices in the super block
2203 * (super_copy) should hold the device list mutex.
2207 * In normal cases the cur_devices == fs_devices. But in case
2208 * of deleting a seed device, the cur_devices should point to
2209 * its own fs_devices listed under the fs_devices->seed.
2211 cur_devices = device->fs_devices;
2212 mutex_lock(&fs_devices->device_list_mutex);
2213 list_del_rcu(&device->dev_list);
2215 cur_devices->num_devices--;
2216 cur_devices->total_devices--;
2217 /* Update total_devices of the parent fs_devices if it's seed */
2218 if (cur_devices != fs_devices)
2219 fs_devices->total_devices--;
2221 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
2222 cur_devices->missing_devices--;
2224 btrfs_assign_next_active_device(device, NULL);
2227 cur_devices->open_devices--;
2228 /* remove sysfs entry */
2229 btrfs_sysfs_rm_device_link(fs_devices, device);
2232 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2233 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2234 mutex_unlock(&fs_devices->device_list_mutex);
2237 * at this point, the device is zero sized and detached from
2238 * the devices list. All that's left is to zero out the old
2239 * supers and free the device.
2241 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2242 btrfs_scratch_superblocks(device->bdev, device->name->str);
2244 btrfs_close_bdev(device);
2245 call_rcu(&device->rcu, free_device_rcu);
2247 if (cur_devices->open_devices == 0) {
2248 while (fs_devices) {
2249 if (fs_devices->seed == cur_devices) {
2250 fs_devices->seed = cur_devices->seed;
2253 fs_devices = fs_devices->seed;
2255 cur_devices->seed = NULL;
2256 close_fs_devices(cur_devices);
2257 free_fs_devices(cur_devices);
2261 mutex_unlock(&uuid_mutex);
2265 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2266 mutex_lock(&fs_info->chunk_mutex);
2267 list_add(&device->dev_alloc_list,
2268 &fs_devices->alloc_list);
2269 device->fs_devices->rw_devices++;
2270 mutex_unlock(&fs_info->chunk_mutex);
2275 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
2277 struct btrfs_fs_devices *fs_devices;
2279 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
2282 * in case of fs with no seed, srcdev->fs_devices will point
2283 * to fs_devices of fs_info. However when the dev being replaced is
2284 * a seed dev it will point to the seed's local fs_devices. In short
2285 * srcdev will have its correct fs_devices in both the cases.
2287 fs_devices = srcdev->fs_devices;
2289 list_del_rcu(&srcdev->dev_list);
2290 list_del(&srcdev->dev_alloc_list);
2291 fs_devices->num_devices--;
2292 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
2293 fs_devices->missing_devices--;
2295 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
2296 fs_devices->rw_devices--;
2299 fs_devices->open_devices--;
2302 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2303 struct btrfs_device *srcdev)
2305 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
2307 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
2308 /* zero out the old super if it is writable */
2309 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2312 btrfs_close_bdev(srcdev);
2313 call_rcu(&srcdev->rcu, free_device_rcu);
2315 /* if this is no devs we rather delete the fs_devices */
2316 if (!fs_devices->num_devices) {
2317 struct btrfs_fs_devices *tmp_fs_devices;
2320 * On a mounted FS, num_devices can't be zero unless it's a
2321 * seed. In case of a seed device being replaced, the replace
2322 * target added to the sprout FS, so there will be no more
2323 * device left under the seed FS.
2325 ASSERT(fs_devices->seeding);
2327 tmp_fs_devices = fs_info->fs_devices;
2328 while (tmp_fs_devices) {
2329 if (tmp_fs_devices->seed == fs_devices) {
2330 tmp_fs_devices->seed = fs_devices->seed;
2333 tmp_fs_devices = tmp_fs_devices->seed;
2335 fs_devices->seed = NULL;
2336 close_fs_devices(fs_devices);
2337 free_fs_devices(fs_devices);
2341 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
2343 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
2346 mutex_lock(&fs_devices->device_list_mutex);
2348 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
2351 fs_devices->open_devices--;
2353 fs_devices->num_devices--;
2355 btrfs_assign_next_active_device(tgtdev, NULL);
2357 list_del_rcu(&tgtdev->dev_list);
2359 mutex_unlock(&fs_devices->device_list_mutex);
2362 * The update_dev_time() with in btrfs_scratch_superblocks()
2363 * may lead to a call to btrfs_show_devname() which will try
2364 * to hold device_list_mutex. And here this device
2365 * is already out of device list, so we don't have to hold
2366 * the device_list_mutex lock.
2368 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
2370 btrfs_close_bdev(tgtdev);
2371 call_rcu(&tgtdev->rcu, free_device_rcu);
2374 static struct btrfs_device *btrfs_find_device_by_path(
2375 struct btrfs_fs_info *fs_info, const char *device_path)
2378 struct btrfs_super_block *disk_super;
2381 struct block_device *bdev;
2382 struct buffer_head *bh;
2383 struct btrfs_device *device;
2385 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2386 fs_info->bdev_holder, 0, &bdev, &bh);
2388 return ERR_PTR(ret);
2389 disk_super = (struct btrfs_super_block *)bh->b_data;
2390 devid = btrfs_stack_device_id(&disk_super->dev_item);
2391 dev_uuid = disk_super->dev_item.uuid;
2392 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
2393 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2394 disk_super->metadata_uuid, true);
2396 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2397 disk_super->fsid, true);
2401 device = ERR_PTR(-ENOENT);
2402 blkdev_put(bdev, FMODE_READ);
2407 * Lookup a device given by device id, or the path if the id is 0.
2409 struct btrfs_device *btrfs_find_device_by_devspec(
2410 struct btrfs_fs_info *fs_info, u64 devid,
2411 const char *device_path)
2413 struct btrfs_device *device;
2416 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
2419 return ERR_PTR(-ENOENT);
2423 if (!device_path || !device_path[0])
2424 return ERR_PTR(-EINVAL);
2426 if (strcmp(device_path, "missing") == 0) {
2427 /* Find first missing device */
2428 list_for_each_entry(device, &fs_info->fs_devices->devices,
2430 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2431 &device->dev_state) && !device->bdev)
2434 return ERR_PTR(-ENOENT);
2437 return btrfs_find_device_by_path(fs_info, device_path);
2441 * does all the dirty work required for changing file system's UUID.
2443 static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
2445 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2446 struct btrfs_fs_devices *old_devices;
2447 struct btrfs_fs_devices *seed_devices;
2448 struct btrfs_super_block *disk_super = fs_info->super_copy;
2449 struct btrfs_device *device;
2452 lockdep_assert_held(&uuid_mutex);
2453 if (!fs_devices->seeding)
2456 seed_devices = alloc_fs_devices(NULL, NULL);
2457 if (IS_ERR(seed_devices))
2458 return PTR_ERR(seed_devices);
2460 old_devices = clone_fs_devices(fs_devices);
2461 if (IS_ERR(old_devices)) {
2462 kfree(seed_devices);
2463 return PTR_ERR(old_devices);
2466 list_add(&old_devices->fs_list, &fs_uuids);
2468 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2469 seed_devices->opened = 1;
2470 INIT_LIST_HEAD(&seed_devices->devices);
2471 INIT_LIST_HEAD(&seed_devices->alloc_list);
2472 mutex_init(&seed_devices->device_list_mutex);
2474 mutex_lock(&fs_devices->device_list_mutex);
2475 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2477 list_for_each_entry(device, &seed_devices->devices, dev_list)
2478 device->fs_devices = seed_devices;
2480 mutex_lock(&fs_info->chunk_mutex);
2481 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2482 mutex_unlock(&fs_info->chunk_mutex);
2484 fs_devices->seeding = 0;
2485 fs_devices->num_devices = 0;
2486 fs_devices->open_devices = 0;
2487 fs_devices->missing_devices = 0;
2488 fs_devices->rotating = 0;
2489 fs_devices->seed = seed_devices;
2491 generate_random_uuid(fs_devices->fsid);
2492 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
2493 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2494 mutex_unlock(&fs_devices->device_list_mutex);
2496 super_flags = btrfs_super_flags(disk_super) &
2497 ~BTRFS_SUPER_FLAG_SEEDING;
2498 btrfs_set_super_flags(disk_super, super_flags);
2504 * Store the expected generation for seed devices in device items.
2506 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2507 struct btrfs_fs_info *fs_info)
2509 struct btrfs_root *root = fs_info->chunk_root;
2510 struct btrfs_path *path;
2511 struct extent_buffer *leaf;
2512 struct btrfs_dev_item *dev_item;
2513 struct btrfs_device *device;
2514 struct btrfs_key key;
2515 u8 fs_uuid[BTRFS_FSID_SIZE];
2516 u8 dev_uuid[BTRFS_UUID_SIZE];
2520 path = btrfs_alloc_path();
2524 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2526 key.type = BTRFS_DEV_ITEM_KEY;
2529 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2533 leaf = path->nodes[0];
2535 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2536 ret = btrfs_next_leaf(root, path);
2541 leaf = path->nodes[0];
2542 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2543 btrfs_release_path(path);
2547 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2548 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2549 key.type != BTRFS_DEV_ITEM_KEY)
2552 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2553 struct btrfs_dev_item);
2554 devid = btrfs_device_id(leaf, dev_item);
2555 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2557 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2559 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2561 BUG_ON(!device); /* Logic error */
2563 if (device->fs_devices->seeding) {
2564 btrfs_set_device_generation(leaf, dev_item,
2565 device->generation);
2566 btrfs_mark_buffer_dirty(leaf);
2574 btrfs_free_path(path);
2578 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
2580 struct btrfs_root *root = fs_info->dev_root;
2581 struct request_queue *q;
2582 struct btrfs_trans_handle *trans;
2583 struct btrfs_device *device;
2584 struct block_device *bdev;
2585 struct super_block *sb = fs_info->sb;
2586 struct rcu_string *name;
2587 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2588 u64 orig_super_total_bytes;
2589 u64 orig_super_num_devices;
2590 int seeding_dev = 0;
2592 bool unlocked = false;
2594 if (sb_rdonly(sb) && !fs_devices->seeding)
2597 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2598 fs_info->bdev_holder);
2600 return PTR_ERR(bdev);
2602 if (fs_devices->seeding) {
2604 down_write(&sb->s_umount);
2605 mutex_lock(&uuid_mutex);
2608 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2610 mutex_lock(&fs_devices->device_list_mutex);
2611 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2612 if (device->bdev == bdev) {
2615 &fs_devices->device_list_mutex);
2619 mutex_unlock(&fs_devices->device_list_mutex);
2621 device = btrfs_alloc_device(fs_info, NULL, NULL);
2622 if (IS_ERR(device)) {
2623 /* we can safely leave the fs_devices entry around */
2624 ret = PTR_ERR(device);
2628 name = rcu_string_strdup(device_path, GFP_KERNEL);
2631 goto error_free_device;
2633 rcu_assign_pointer(device->name, name);
2635 trans = btrfs_start_transaction(root, 0);
2636 if (IS_ERR(trans)) {
2637 ret = PTR_ERR(trans);
2638 goto error_free_device;
2641 q = bdev_get_queue(bdev);
2642 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2643 device->generation = trans->transid;
2644 device->io_width = fs_info->sectorsize;
2645 device->io_align = fs_info->sectorsize;
2646 device->sector_size = fs_info->sectorsize;
2647 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2648 fs_info->sectorsize);
2649 device->disk_total_bytes = device->total_bytes;
2650 device->commit_total_bytes = device->total_bytes;
2651 device->fs_info = fs_info;
2652 device->bdev = bdev;
2653 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2654 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2655 device->mode = FMODE_EXCL;
2656 device->dev_stats_valid = 1;
2657 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2660 sb->s_flags &= ~SB_RDONLY;
2661 ret = btrfs_prepare_sprout(fs_info);
2663 btrfs_abort_transaction(trans, ret);
2668 device->fs_devices = fs_devices;
2670 mutex_lock(&fs_devices->device_list_mutex);
2671 mutex_lock(&fs_info->chunk_mutex);
2672 list_add_rcu(&device->dev_list, &fs_devices->devices);
2673 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2674 fs_devices->num_devices++;
2675 fs_devices->open_devices++;
2676 fs_devices->rw_devices++;
2677 fs_devices->total_devices++;
2678 fs_devices->total_rw_bytes += device->total_bytes;
2680 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
2682 if (!blk_queue_nonrot(q))
2683 fs_devices->rotating = 1;
2685 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
2686 btrfs_set_super_total_bytes(fs_info->super_copy,
2687 round_down(orig_super_total_bytes + device->total_bytes,
2688 fs_info->sectorsize));
2690 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2691 btrfs_set_super_num_devices(fs_info->super_copy,
2692 orig_super_num_devices + 1);
2694 /* add sysfs device entry */
2695 btrfs_sysfs_add_device_link(fs_devices, device);
2698 * we've got more storage, clear any full flags on the space
2701 btrfs_clear_space_info_full(fs_info);
2703 mutex_unlock(&fs_info->chunk_mutex);
2704 mutex_unlock(&fs_devices->device_list_mutex);
2707 mutex_lock(&fs_info->chunk_mutex);
2708 ret = init_first_rw_device(trans, fs_info);
2709 mutex_unlock(&fs_info->chunk_mutex);
2711 btrfs_abort_transaction(trans, ret);
2716 ret = btrfs_add_dev_item(trans, device);
2718 btrfs_abort_transaction(trans, ret);
2723 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2725 ret = btrfs_finish_sprout(trans, fs_info);
2727 btrfs_abort_transaction(trans, ret);
2731 /* Sprouting would change fsid of the mounted root,
2732 * so rename the fsid on the sysfs
2734 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2735 fs_info->fs_devices->fsid);
2736 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
2738 "sysfs: failed to create fsid for sprout");
2741 ret = btrfs_commit_transaction(trans);
2744 mutex_unlock(&uuid_mutex);
2745 up_write(&sb->s_umount);
2748 if (ret) /* transaction commit */
2751 ret = btrfs_relocate_sys_chunks(fs_info);
2753 btrfs_handle_fs_error(fs_info, ret,
2754 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2755 trans = btrfs_attach_transaction(root);
2756 if (IS_ERR(trans)) {
2757 if (PTR_ERR(trans) == -ENOENT)
2759 ret = PTR_ERR(trans);
2763 ret = btrfs_commit_transaction(trans);
2766 /* Update ctime/mtime for libblkid */
2767 update_dev_time(device_path);
2771 btrfs_sysfs_rm_device_link(fs_devices, device);
2772 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2773 mutex_lock(&fs_info->chunk_mutex);
2774 list_del_rcu(&device->dev_list);
2775 list_del(&device->dev_alloc_list);
2776 fs_info->fs_devices->num_devices--;
2777 fs_info->fs_devices->open_devices--;
2778 fs_info->fs_devices->rw_devices--;
2779 fs_info->fs_devices->total_devices--;
2780 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2781 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2782 btrfs_set_super_total_bytes(fs_info->super_copy,
2783 orig_super_total_bytes);
2784 btrfs_set_super_num_devices(fs_info->super_copy,
2785 orig_super_num_devices);
2786 mutex_unlock(&fs_info->chunk_mutex);
2787 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2790 sb->s_flags |= SB_RDONLY;
2792 btrfs_end_transaction(trans);
2794 btrfs_free_device(device);
2796 blkdev_put(bdev, FMODE_EXCL);
2797 if (seeding_dev && !unlocked) {
2798 mutex_unlock(&uuid_mutex);
2799 up_write(&sb->s_umount);
2804 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2805 struct btrfs_device *device)
2808 struct btrfs_path *path;
2809 struct btrfs_root *root = device->fs_info->chunk_root;
2810 struct btrfs_dev_item *dev_item;
2811 struct extent_buffer *leaf;
2812 struct btrfs_key key;
2814 path = btrfs_alloc_path();
2818 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2819 key.type = BTRFS_DEV_ITEM_KEY;
2820 key.offset = device->devid;
2822 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2831 leaf = path->nodes[0];
2832 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2834 btrfs_set_device_id(leaf, dev_item, device->devid);
2835 btrfs_set_device_type(leaf, dev_item, device->type);
2836 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2837 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2838 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2839 btrfs_set_device_total_bytes(leaf, dev_item,
2840 btrfs_device_get_disk_total_bytes(device));
2841 btrfs_set_device_bytes_used(leaf, dev_item,
2842 btrfs_device_get_bytes_used(device));
2843 btrfs_mark_buffer_dirty(leaf);
2846 btrfs_free_path(path);
2850 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2851 struct btrfs_device *device, u64 new_size)
2853 struct btrfs_fs_info *fs_info = device->fs_info;
2854 struct btrfs_super_block *super_copy = fs_info->super_copy;
2855 struct btrfs_fs_devices *fs_devices;
2859 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2862 new_size = round_down(new_size, fs_info->sectorsize);
2864 mutex_lock(&fs_info->chunk_mutex);
2865 old_total = btrfs_super_total_bytes(super_copy);
2866 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
2868 if (new_size <= device->total_bytes ||
2869 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2870 mutex_unlock(&fs_info->chunk_mutex);
2874 fs_devices = fs_info->fs_devices;
2876 btrfs_set_super_total_bytes(super_copy,
2877 round_down(old_total + diff, fs_info->sectorsize));
2878 device->fs_devices->total_rw_bytes += diff;
2880 btrfs_device_set_total_bytes(device, new_size);
2881 btrfs_device_set_disk_total_bytes(device, new_size);
2882 btrfs_clear_space_info_full(device->fs_info);
2883 if (list_empty(&device->resized_list))
2884 list_add_tail(&device->resized_list,
2885 &fs_devices->resized_devices);
2886 mutex_unlock(&fs_info->chunk_mutex);
2888 return btrfs_update_device(trans, device);
2891 static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
2893 struct btrfs_fs_info *fs_info = trans->fs_info;
2894 struct btrfs_root *root = fs_info->chunk_root;
2896 struct btrfs_path *path;
2897 struct btrfs_key key;
2899 path = btrfs_alloc_path();
2903 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2904 key.offset = chunk_offset;
2905 key.type = BTRFS_CHUNK_ITEM_KEY;
2907 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2910 else if (ret > 0) { /* Logic error or corruption */
2911 btrfs_handle_fs_error(fs_info, -ENOENT,
2912 "Failed lookup while freeing chunk.");
2917 ret = btrfs_del_item(trans, root, path);
2919 btrfs_handle_fs_error(fs_info, ret,
2920 "Failed to delete chunk item.");
2922 btrfs_free_path(path);
2926 static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2928 struct btrfs_super_block *super_copy = fs_info->super_copy;
2929 struct btrfs_disk_key *disk_key;
2930 struct btrfs_chunk *chunk;
2937 struct btrfs_key key;
2939 mutex_lock(&fs_info->chunk_mutex);
2940 array_size = btrfs_super_sys_array_size(super_copy);
2942 ptr = super_copy->sys_chunk_array;
2945 while (cur < array_size) {
2946 disk_key = (struct btrfs_disk_key *)ptr;
2947 btrfs_disk_key_to_cpu(&key, disk_key);
2949 len = sizeof(*disk_key);
2951 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2952 chunk = (struct btrfs_chunk *)(ptr + len);
2953 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2954 len += btrfs_chunk_item_size(num_stripes);
2959 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
2960 key.offset == chunk_offset) {
2961 memmove(ptr, ptr + len, array_size - (cur + len));
2963 btrfs_set_super_sys_array_size(super_copy, array_size);
2969 mutex_unlock(&fs_info->chunk_mutex);
2974 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2975 * @logical: Logical block offset in bytes.
2976 * @length: Length of extent in bytes.
2978 * Return: Chunk mapping or ERR_PTR.
2980 struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2981 u64 logical, u64 length)
2983 struct extent_map_tree *em_tree;
2984 struct extent_map *em;
2986 em_tree = &fs_info->mapping_tree.map_tree;
2987 read_lock(&em_tree->lock);
2988 em = lookup_extent_mapping(em_tree, logical, length);
2989 read_unlock(&em_tree->lock);
2992 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2994 return ERR_PTR(-EINVAL);
2997 if (em->start > logical || em->start + em->len < logical) {
2999 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
3000 logical, length, em->start, em->start + em->len);
3001 free_extent_map(em);
3002 return ERR_PTR(-EINVAL);
3005 /* callers are responsible for dropping em's ref. */
3009 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
3011 struct btrfs_fs_info *fs_info = trans->fs_info;
3012 struct extent_map *em;
3013 struct map_lookup *map;
3014 u64 dev_extent_len = 0;
3016 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
3018 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
3021 * This is a logic error, but we don't want to just rely on the
3022 * user having built with ASSERT enabled, so if ASSERT doesn't
3023 * do anything we still error out.
3028 map = em->map_lookup;
3029 mutex_lock(&fs_info->chunk_mutex);
3030 check_system_chunk(trans, map->type);
3031 mutex_unlock(&fs_info->chunk_mutex);
3034 * Take the device list mutex to prevent races with the final phase of
3035 * a device replace operation that replaces the device object associated
3036 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
3038 mutex_lock(&fs_devices->device_list_mutex);
3039 for (i = 0; i < map->num_stripes; i++) {
3040 struct btrfs_device *device = map->stripes[i].dev;
3041 ret = btrfs_free_dev_extent(trans, device,
3042 map->stripes[i].physical,
3045 mutex_unlock(&fs_devices->device_list_mutex);
3046 btrfs_abort_transaction(trans, ret);
3050 if (device->bytes_used > 0) {
3051 mutex_lock(&fs_info->chunk_mutex);
3052 btrfs_device_set_bytes_used(device,
3053 device->bytes_used - dev_extent_len);
3054 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
3055 btrfs_clear_space_info_full(fs_info);
3056 mutex_unlock(&fs_info->chunk_mutex);
3059 ret = btrfs_update_device(trans, device);
3061 mutex_unlock(&fs_devices->device_list_mutex);
3062 btrfs_abort_transaction(trans, ret);
3066 mutex_unlock(&fs_devices->device_list_mutex);
3068 ret = btrfs_free_chunk(trans, chunk_offset);
3070 btrfs_abort_transaction(trans, ret);
3074 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
3076 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3077 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
3079 btrfs_abort_transaction(trans, ret);
3084 ret = btrfs_remove_block_group(trans, chunk_offset, em);
3086 btrfs_abort_transaction(trans, ret);
3092 free_extent_map(em);
3096 static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
3098 struct btrfs_root *root = fs_info->chunk_root;
3099 struct btrfs_trans_handle *trans;
3103 * Prevent races with automatic removal of unused block groups.
3104 * After we relocate and before we remove the chunk with offset
3105 * chunk_offset, automatic removal of the block group can kick in,
3106 * resulting in a failure when calling btrfs_remove_chunk() below.
3108 * Make sure to acquire this mutex before doing a tree search (dev
3109 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3110 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3111 * we release the path used to search the chunk/dev tree and before
3112 * the current task acquires this mutex and calls us.
3114 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
3116 ret = btrfs_can_relocate(fs_info, chunk_offset);
3120 /* step one, relocate all the extents inside this chunk */
3121 btrfs_scrub_pause(fs_info);
3122 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
3123 btrfs_scrub_continue(fs_info);
3128 * We add the kobjects here (and after forcing data chunk creation)
3129 * since relocation is the only place we'll create chunks of a new
3130 * type at runtime. The only place where we'll remove the last
3131 * chunk of a type is the call immediately below this one. Even
3132 * so, we're protected against races with the cleaner thread since
3133 * we're covered by the delete_unused_bgs_mutex.
3135 btrfs_add_raid_kobjects(fs_info);
3137 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3139 if (IS_ERR(trans)) {
3140 ret = PTR_ERR(trans);
3141 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3146 * step two, delete the device extents and the
3147 * chunk tree entries
3149 ret = btrfs_remove_chunk(trans, chunk_offset);
3150 btrfs_end_transaction(trans);
3154 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
3156 struct btrfs_root *chunk_root = fs_info->chunk_root;
3157 struct btrfs_path *path;
3158 struct extent_buffer *leaf;
3159 struct btrfs_chunk *chunk;
3160 struct btrfs_key key;
3161 struct btrfs_key found_key;
3163 bool retried = false;
3167 path = btrfs_alloc_path();
3172 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3173 key.offset = (u64)-1;
3174 key.type = BTRFS_CHUNK_ITEM_KEY;
3177 mutex_lock(&fs_info->delete_unused_bgs_mutex);
3178 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3180 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3183 BUG_ON(ret == 0); /* Corruption */
3185 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3188 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3194 leaf = path->nodes[0];
3195 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3197 chunk = btrfs_item_ptr(leaf, path->slots[0],
3198 struct btrfs_chunk);
3199 chunk_type = btrfs_chunk_type(leaf, chunk);
3200 btrfs_release_path(path);
3202 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
3203 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3209 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3211 if (found_key.offset == 0)
3213 key.offset = found_key.offset - 1;
3216 if (failed && !retried) {
3220 } else if (WARN_ON(failed && retried)) {
3224 btrfs_free_path(path);
3229 * return 1 : allocate a data chunk successfully,
3230 * return <0: errors during allocating a data chunk,
3231 * return 0 : no need to allocate a data chunk.
3233 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3236 struct btrfs_block_group_cache *cache;
3240 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3242 chunk_type = cache->flags;
3243 btrfs_put_block_group(cache);
3245 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3246 spin_lock(&fs_info->data_sinfo->lock);
3247 bytes_used = fs_info->data_sinfo->bytes_used;
3248 spin_unlock(&fs_info->data_sinfo->lock);
3251 struct btrfs_trans_handle *trans;
3254 trans = btrfs_join_transaction(fs_info->tree_root);
3256 return PTR_ERR(trans);
3258 ret = btrfs_force_chunk_alloc(trans,
3259 BTRFS_BLOCK_GROUP_DATA);
3260 btrfs_end_transaction(trans);
3264 btrfs_add_raid_kobjects(fs_info);
3272 static int insert_balance_item(struct btrfs_fs_info *fs_info,
3273 struct btrfs_balance_control *bctl)
3275 struct btrfs_root *root = fs_info->tree_root;
3276 struct btrfs_trans_handle *trans;
3277 struct btrfs_balance_item *item;
3278 struct btrfs_disk_balance_args disk_bargs;
3279 struct btrfs_path *path;
3280 struct extent_buffer *leaf;
3281 struct btrfs_key key;
3284 path = btrfs_alloc_path();
3288 trans = btrfs_start_transaction(root, 0);
3289 if (IS_ERR(trans)) {
3290 btrfs_free_path(path);
3291 return PTR_ERR(trans);
3294 key.objectid = BTRFS_BALANCE_OBJECTID;
3295 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3298 ret = btrfs_insert_empty_item(trans, root, path, &key,
3303 leaf = path->nodes[0];
3304 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3306 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
3308 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3309 btrfs_set_balance_data(leaf, item, &disk_bargs);
3310 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3311 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3312 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3313 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3315 btrfs_set_balance_flags(leaf, item, bctl->flags);
3317 btrfs_mark_buffer_dirty(leaf);
3319 btrfs_free_path(path);
3320 err = btrfs_commit_transaction(trans);
3326 static int del_balance_item(struct btrfs_fs_info *fs_info)
3328 struct btrfs_root *root = fs_info->tree_root;
3329 struct btrfs_trans_handle *trans;
3330 struct btrfs_path *path;
3331 struct btrfs_key key;
3334 path = btrfs_alloc_path();
3338 trans = btrfs_start_transaction(root, 0);
3339 if (IS_ERR(trans)) {
3340 btrfs_free_path(path);
3341 return PTR_ERR(trans);
3344 key.objectid = BTRFS_BALANCE_OBJECTID;
3345 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3348 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3356 ret = btrfs_del_item(trans, root, path);
3358 btrfs_free_path(path);
3359 err = btrfs_commit_transaction(trans);
3366 * This is a heuristic used to reduce the number of chunks balanced on
3367 * resume after balance was interrupted.
3369 static void update_balance_args(struct btrfs_balance_control *bctl)
3372 * Turn on soft mode for chunk types that were being converted.
3374 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3375 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3376 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3377 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3378 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3379 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3382 * Turn on usage filter if is not already used. The idea is
3383 * that chunks that we have already balanced should be
3384 * reasonably full. Don't do it for chunks that are being
3385 * converted - that will keep us from relocating unconverted
3386 * (albeit full) chunks.
3388 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3389 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3390 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3391 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3392 bctl->data.usage = 90;
3394 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3395 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3396 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3397 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3398 bctl->sys.usage = 90;
3400 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3401 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3402 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3403 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3404 bctl->meta.usage = 90;
3409 * Clear the balance status in fs_info and delete the balance item from disk.
3411 static void reset_balance_state(struct btrfs_fs_info *fs_info)
3413 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3416 BUG_ON(!fs_info->balance_ctl);
3418 spin_lock(&fs_info->balance_lock);
3419 fs_info->balance_ctl = NULL;
3420 spin_unlock(&fs_info->balance_lock);
3423 ret = del_balance_item(fs_info);
3425 btrfs_handle_fs_error(fs_info, ret, NULL);
3429 * Balance filters. Return 1 if chunk should be filtered out
3430 * (should not be balanced).
3432 static int chunk_profiles_filter(u64 chunk_type,
3433 struct btrfs_balance_args *bargs)
3435 chunk_type = chunk_to_extended(chunk_type) &
3436 BTRFS_EXTENDED_PROFILE_MASK;
3438 if (bargs->profiles & chunk_type)
3444 static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3445 struct btrfs_balance_args *bargs)
3447 struct btrfs_block_group_cache *cache;
3449 u64 user_thresh_min;
3450 u64 user_thresh_max;
3453 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3454 chunk_used = btrfs_block_group_used(&cache->item);
3456 if (bargs->usage_min == 0)
3457 user_thresh_min = 0;
3459 user_thresh_min = div_factor_fine(cache->key.offset,
3462 if (bargs->usage_max == 0)
3463 user_thresh_max = 1;
3464 else if (bargs->usage_max > 100)
3465 user_thresh_max = cache->key.offset;
3467 user_thresh_max = div_factor_fine(cache->key.offset,
3470 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3473 btrfs_put_block_group(cache);
3477 static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
3478 u64 chunk_offset, struct btrfs_balance_args *bargs)
3480 struct btrfs_block_group_cache *cache;
3481 u64 chunk_used, user_thresh;
3484 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3485 chunk_used = btrfs_block_group_used(&cache->item);
3487 if (bargs->usage_min == 0)
3489 else if (bargs->usage > 100)
3490 user_thresh = cache->key.offset;
3492 user_thresh = div_factor_fine(cache->key.offset,
3495 if (chunk_used < user_thresh)
3498 btrfs_put_block_group(cache);
3502 static int chunk_devid_filter(struct extent_buffer *leaf,
3503 struct btrfs_chunk *chunk,
3504 struct btrfs_balance_args *bargs)
3506 struct btrfs_stripe *stripe;
3507 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3510 for (i = 0; i < num_stripes; i++) {
3511 stripe = btrfs_stripe_nr(chunk, i);
3512 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3519 /* [pstart, pend) */
3520 static int chunk_drange_filter(struct extent_buffer *leaf,
3521 struct btrfs_chunk *chunk,
3522 struct btrfs_balance_args *bargs)
3524 struct btrfs_stripe *stripe;
3525 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3531 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3534 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
3535 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3536 factor = num_stripes / 2;
3537 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3538 factor = num_stripes - 1;
3539 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3540 factor = num_stripes - 2;
3542 factor = num_stripes;
3545 for (i = 0; i < num_stripes; i++) {
3546 stripe = btrfs_stripe_nr(chunk, i);
3547 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3550 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3551 stripe_length = btrfs_chunk_length(leaf, chunk);
3552 stripe_length = div_u64(stripe_length, factor);
3554 if (stripe_offset < bargs->pend &&
3555 stripe_offset + stripe_length > bargs->pstart)
3562 /* [vstart, vend) */
3563 static int chunk_vrange_filter(struct extent_buffer *leaf,
3564 struct btrfs_chunk *chunk,
3566 struct btrfs_balance_args *bargs)
3568 if (chunk_offset < bargs->vend &&
3569 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3570 /* at least part of the chunk is inside this vrange */
3576 static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3577 struct btrfs_chunk *chunk,
3578 struct btrfs_balance_args *bargs)
3580 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3582 if (bargs->stripes_min <= num_stripes
3583 && num_stripes <= bargs->stripes_max)
3589 static int chunk_soft_convert_filter(u64 chunk_type,
3590 struct btrfs_balance_args *bargs)
3592 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3595 chunk_type = chunk_to_extended(chunk_type) &
3596 BTRFS_EXTENDED_PROFILE_MASK;
3598 if (bargs->target == chunk_type)
3604 static int should_balance_chunk(struct btrfs_fs_info *fs_info,
3605 struct extent_buffer *leaf,
3606 struct btrfs_chunk *chunk, u64 chunk_offset)
3608 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3609 struct btrfs_balance_args *bargs = NULL;
3610 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3613 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3614 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3618 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3619 bargs = &bctl->data;
3620 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3622 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3623 bargs = &bctl->meta;
3625 /* profiles filter */
3626 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3627 chunk_profiles_filter(chunk_type, bargs)) {
3632 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3633 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
3635 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3636 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
3641 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3642 chunk_devid_filter(leaf, chunk, bargs)) {
3646 /* drange filter, makes sense only with devid filter */
3647 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3648 chunk_drange_filter(leaf, chunk, bargs)) {
3653 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3654 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3658 /* stripes filter */
3659 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3660 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3664 /* soft profile changing mode */
3665 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3666 chunk_soft_convert_filter(chunk_type, bargs)) {
3671 * limited by count, must be the last filter
3673 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3674 if (bargs->limit == 0)
3678 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3680 * Same logic as the 'limit' filter; the minimum cannot be
3681 * determined here because we do not have the global information
3682 * about the count of all chunks that satisfy the filters.
3684 if (bargs->limit_max == 0)
3693 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
3695 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3696 struct btrfs_root *chunk_root = fs_info->chunk_root;
3698 struct btrfs_chunk *chunk;
3699 struct btrfs_path *path = NULL;
3700 struct btrfs_key key;
3701 struct btrfs_key found_key;
3702 struct extent_buffer *leaf;
3705 int enospc_errors = 0;
3706 bool counting = true;
3707 /* The single value limit and min/max limits use the same bytes in the */
3708 u64 limit_data = bctl->data.limit;
3709 u64 limit_meta = bctl->meta.limit;
3710 u64 limit_sys = bctl->sys.limit;
3714 int chunk_reserved = 0;
3716 path = btrfs_alloc_path();
3722 /* zero out stat counters */
3723 spin_lock(&fs_info->balance_lock);
3724 memset(&bctl->stat, 0, sizeof(bctl->stat));
3725 spin_unlock(&fs_info->balance_lock);
3729 * The single value limit and min/max limits use the same bytes
3732 bctl->data.limit = limit_data;
3733 bctl->meta.limit = limit_meta;
3734 bctl->sys.limit = limit_sys;
3736 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3737 key.offset = (u64)-1;
3738 key.type = BTRFS_CHUNK_ITEM_KEY;
3741 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
3742 atomic_read(&fs_info->balance_cancel_req)) {
3747 mutex_lock(&fs_info->delete_unused_bgs_mutex);
3748 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3750 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3755 * this shouldn't happen, it means the last relocate
3759 BUG(); /* FIXME break ? */
3761 ret = btrfs_previous_item(chunk_root, path, 0,
3762 BTRFS_CHUNK_ITEM_KEY);
3764 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3769 leaf = path->nodes[0];
3770 slot = path->slots[0];
3771 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3773 if (found_key.objectid != key.objectid) {
3774 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3778 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3779 chunk_type = btrfs_chunk_type(leaf, chunk);
3782 spin_lock(&fs_info->balance_lock);
3783 bctl->stat.considered++;
3784 spin_unlock(&fs_info->balance_lock);
3787 ret = should_balance_chunk(fs_info, leaf, chunk,
3790 btrfs_release_path(path);
3792 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3797 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3798 spin_lock(&fs_info->balance_lock);
3799 bctl->stat.expected++;
3800 spin_unlock(&fs_info->balance_lock);
3802 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3804 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3806 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3813 * Apply limit_min filter, no need to check if the LIMITS
3814 * filter is used, limit_min is 0 by default
3816 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3817 count_data < bctl->data.limit_min)
3818 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3819 count_meta < bctl->meta.limit_min)
3820 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3821 count_sys < bctl->sys.limit_min)) {
3822 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3826 if (!chunk_reserved) {
3828 * We may be relocating the only data chunk we have,
3829 * which could potentially end up with losing data's
3830 * raid profile, so lets allocate an empty one in
3833 ret = btrfs_may_alloc_data_chunk(fs_info,
3836 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3838 } else if (ret == 1) {
3843 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3844 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3845 if (ret == -ENOSPC) {
3847 } else if (ret == -ETXTBSY) {
3849 "skipping relocation of block group %llu due to active swapfile",
3855 spin_lock(&fs_info->balance_lock);
3856 bctl->stat.completed++;
3857 spin_unlock(&fs_info->balance_lock);
3860 if (found_key.offset == 0)
3862 key.offset = found_key.offset - 1;
3866 btrfs_release_path(path);
3871 btrfs_free_path(path);
3872 if (enospc_errors) {
3873 btrfs_info(fs_info, "%d enospc errors during balance",
3883 * alloc_profile_is_valid - see if a given profile is valid and reduced
3884 * @flags: profile to validate
3885 * @extended: if true @flags is treated as an extended profile
3887 static int alloc_profile_is_valid(u64 flags, int extended)
3889 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3890 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3892 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3894 /* 1) check that all other bits are zeroed */
3898 /* 2) see if profile is reduced */
3900 return !extended; /* "0" is valid for usual profiles */
3902 /* true if exactly one bit set */
3903 return is_power_of_2(flags);
3906 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3908 /* cancel requested || normal exit path */
3909 return atomic_read(&fs_info->balance_cancel_req) ||
3910 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3911 atomic_read(&fs_info->balance_cancel_req) == 0);
3914 /* Non-zero return value signifies invalidity */
3915 static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3918 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3919 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3920 (bctl_arg->target & ~allowed)));
3924 * Fill @buf with textual description of balance filter flags @bargs, up to
3925 * @size_buf including the terminating null. The output may be trimmed if it
3926 * does not fit into the provided buffer.
3928 static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3932 u32 size_bp = size_buf;
3934 u64 flags = bargs->flags;
3935 char tmp_buf[128] = {'\0'};
3940 #define CHECK_APPEND_NOARG(a) \
3942 ret = snprintf(bp, size_bp, (a)); \
3943 if (ret < 0 || ret >= size_bp) \
3944 goto out_overflow; \
3949 #define CHECK_APPEND_1ARG(a, v1) \
3951 ret = snprintf(bp, size_bp, (a), (v1)); \
3952 if (ret < 0 || ret >= size_bp) \
3953 goto out_overflow; \
3958 #define CHECK_APPEND_2ARG(a, v1, v2) \
3960 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3961 if (ret < 0 || ret >= size_bp) \
3962 goto out_overflow; \
3967 if (flags & BTRFS_BALANCE_ARGS_CONVERT) {
3968 int index = btrfs_bg_flags_to_raid_index(bargs->target);
3970 CHECK_APPEND_1ARG("convert=%s,", get_raid_name(index));
3973 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3974 CHECK_APPEND_NOARG("soft,");
3976 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3977 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3979 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3982 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3983 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3985 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3986 CHECK_APPEND_2ARG("usage=%u..%u,",
3987 bargs->usage_min, bargs->usage_max);
3989 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3990 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3992 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3993 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3994 bargs->pstart, bargs->pend);
3996 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3997 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3998 bargs->vstart, bargs->vend);
4000 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
4001 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
4003 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
4004 CHECK_APPEND_2ARG("limit=%u..%u,",
4005 bargs->limit_min, bargs->limit_max);
4007 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
4008 CHECK_APPEND_2ARG("stripes=%u..%u,",
4009 bargs->stripes_min, bargs->stripes_max);
4011 #undef CHECK_APPEND_2ARG
4012 #undef CHECK_APPEND_1ARG
4013 #undef CHECK_APPEND_NOARG
4017 if (size_bp < size_buf)
4018 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
4023 static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
4025 u32 size_buf = 1024;
4026 char tmp_buf[192] = {'\0'};
4029 u32 size_bp = size_buf;
4031 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4033 buf = kzalloc(size_buf, GFP_KERNEL);
4039 #define CHECK_APPEND_1ARG(a, v1) \
4041 ret = snprintf(bp, size_bp, (a), (v1)); \
4042 if (ret < 0 || ret >= size_bp) \
4043 goto out_overflow; \
4048 if (bctl->flags & BTRFS_BALANCE_FORCE)
4049 CHECK_APPEND_1ARG("%s", "-f ");
4051 if (bctl->flags & BTRFS_BALANCE_DATA) {
4052 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
4053 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
4056 if (bctl->flags & BTRFS_BALANCE_METADATA) {
4057 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
4058 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
4061 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
4062 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
4063 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
4066 #undef CHECK_APPEND_1ARG
4070 if (size_bp < size_buf)
4071 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
4072 btrfs_info(fs_info, "balance: %s %s",
4073 (bctl->flags & BTRFS_BALANCE_RESUME) ?
4074 "resume" : "start", buf);
4080 * Should be called with balance mutexe held
4082 int btrfs_balance(struct btrfs_fs_info *fs_info,
4083 struct btrfs_balance_control *bctl,
4084 struct btrfs_ioctl_balance_args *bargs)
4086 u64 meta_target, data_target;
4092 bool reducing_integrity;
4094 if (btrfs_fs_closing(fs_info) ||
4095 atomic_read(&fs_info->balance_pause_req) ||
4096 atomic_read(&fs_info->balance_cancel_req)) {
4101 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4102 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4106 * In case of mixed groups both data and meta should be picked,
4107 * and identical options should be given for both of them.
4109 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4110 if (mixed && (bctl->flags & allowed)) {
4111 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4112 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4113 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
4115 "balance: mixed groups data and metadata options must be the same");
4121 num_devices = btrfs_num_devices(fs_info);
4123 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
4124 if (num_devices > 1)
4125 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
4126 if (num_devices > 2)
4127 allowed |= BTRFS_BLOCK_GROUP_RAID5;
4128 if (num_devices > 3)
4129 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
4130 BTRFS_BLOCK_GROUP_RAID6);
4131 if (validate_convert_profile(&bctl->data, allowed)) {
4132 int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
4135 "balance: invalid convert data profile %s",
4136 get_raid_name(index));
4140 if (validate_convert_profile(&bctl->meta, allowed)) {
4141 int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
4144 "balance: invalid convert metadata profile %s",
4145 get_raid_name(index));
4149 if (validate_convert_profile(&bctl->sys, allowed)) {
4150 int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
4153 "balance: invalid convert system profile %s",
4154 get_raid_name(index));
4159 /* allow to reduce meta or sys integrity only if force set */
4160 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
4161 BTRFS_BLOCK_GROUP_RAID10 |
4162 BTRFS_BLOCK_GROUP_RAID5 |
4163 BTRFS_BLOCK_GROUP_RAID6;
4165 seq = read_seqbegin(&fs_info->profiles_lock);
4167 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4168 (fs_info->avail_system_alloc_bits & allowed) &&
4169 !(bctl->sys.target & allowed)) ||
4170 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4171 (fs_info->avail_metadata_alloc_bits & allowed) &&
4172 !(bctl->meta.target & allowed)))
4173 reducing_integrity = true;
4175 reducing_integrity = false;
4177 /* if we're not converting, the target field is uninitialized */
4178 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4179 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4180 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4181 bctl->data.target : fs_info->avail_data_alloc_bits;
4182 } while (read_seqretry(&fs_info->profiles_lock, seq));
4184 if (reducing_integrity) {
4185 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4187 "balance: force reducing metadata integrity");
4190 "balance: reduces metadata integrity, use --force if you want this");
4196 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4197 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
4198 int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
4199 int data_index = btrfs_bg_flags_to_raid_index(data_target);
4202 "balance: metadata profile %s has lower redundancy than data profile %s",
4203 get_raid_name(meta_index), get_raid_name(data_index));
4206 ret = insert_balance_item(fs_info, bctl);
4207 if (ret && ret != -EEXIST)
4210 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4211 BUG_ON(ret == -EEXIST);
4212 BUG_ON(fs_info->balance_ctl);
4213 spin_lock(&fs_info->balance_lock);
4214 fs_info->balance_ctl = bctl;
4215 spin_unlock(&fs_info->balance_lock);
4217 BUG_ON(ret != -EEXIST);
4218 spin_lock(&fs_info->balance_lock);
4219 update_balance_args(bctl);
4220 spin_unlock(&fs_info->balance_lock);
4223 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4224 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
4225 describe_balance_start_or_resume(fs_info);
4226 mutex_unlock(&fs_info->balance_mutex);
4228 ret = __btrfs_balance(fs_info);
4230 mutex_lock(&fs_info->balance_mutex);
4231 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4232 btrfs_info(fs_info, "balance: paused");
4233 else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
4234 btrfs_info(fs_info, "balance: canceled");
4236 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4238 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
4241 memset(bargs, 0, sizeof(*bargs));
4242 btrfs_update_ioctl_balance_args(fs_info, bargs);
4245 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4246 balance_need_close(fs_info)) {
4247 reset_balance_state(fs_info);
4248 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4251 wake_up(&fs_info->balance_wait_q);
4255 if (bctl->flags & BTRFS_BALANCE_RESUME)
4256 reset_balance_state(fs_info);
4259 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4264 static int balance_kthread(void *data)
4266 struct btrfs_fs_info *fs_info = data;
4269 mutex_lock(&fs_info->balance_mutex);
4270 if (fs_info->balance_ctl)
4271 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
4272 mutex_unlock(&fs_info->balance_mutex);
4277 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4279 struct task_struct *tsk;
4281 mutex_lock(&fs_info->balance_mutex);
4282 if (!fs_info->balance_ctl) {
4283 mutex_unlock(&fs_info->balance_mutex);
4286 mutex_unlock(&fs_info->balance_mutex);
4288 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
4289 btrfs_info(fs_info, "balance: resume skipped");
4294 * A ro->rw remount sequence should continue with the paused balance
4295 * regardless of who pauses it, system or the user as of now, so set
4298 spin_lock(&fs_info->balance_lock);
4299 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4300 spin_unlock(&fs_info->balance_lock);
4302 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
4303 return PTR_ERR_OR_ZERO(tsk);
4306 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
4308 struct btrfs_balance_control *bctl;
4309 struct btrfs_balance_item *item;
4310 struct btrfs_disk_balance_args disk_bargs;
4311 struct btrfs_path *path;
4312 struct extent_buffer *leaf;
4313 struct btrfs_key key;
4316 path = btrfs_alloc_path();
4320 key.objectid = BTRFS_BALANCE_OBJECTID;
4321 key.type = BTRFS_TEMPORARY_ITEM_KEY;
4324 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4327 if (ret > 0) { /* ret = -ENOENT; */
4332 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4338 leaf = path->nodes[0];
4339 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4341 bctl->flags = btrfs_balance_flags(leaf, item);
4342 bctl->flags |= BTRFS_BALANCE_RESUME;
4344 btrfs_balance_data(leaf, item, &disk_bargs);
4345 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4346 btrfs_balance_meta(leaf, item, &disk_bargs);
4347 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4348 btrfs_balance_sys(leaf, item, &disk_bargs);
4349 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4352 * This should never happen, as the paused balance state is recovered
4353 * during mount without any chance of other exclusive ops to collide.
4355 * This gives the exclusive op status to balance and keeps in paused
4356 * state until user intervention (cancel or umount). If the ownership
4357 * cannot be assigned, show a message but do not fail. The balance
4358 * is in a paused state and must have fs_info::balance_ctl properly
4361 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4363 "balance: cannot set exclusive op status, resume manually");
4365 mutex_lock(&fs_info->balance_mutex);
4366 BUG_ON(fs_info->balance_ctl);
4367 spin_lock(&fs_info->balance_lock);
4368 fs_info->balance_ctl = bctl;
4369 spin_unlock(&fs_info->balance_lock);
4370 mutex_unlock(&fs_info->balance_mutex);
4372 btrfs_free_path(path);
4376 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4380 mutex_lock(&fs_info->balance_mutex);
4381 if (!fs_info->balance_ctl) {
4382 mutex_unlock(&fs_info->balance_mutex);
4386 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4387 atomic_inc(&fs_info->balance_pause_req);
4388 mutex_unlock(&fs_info->balance_mutex);
4390 wait_event(fs_info->balance_wait_q,
4391 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4393 mutex_lock(&fs_info->balance_mutex);
4394 /* we are good with balance_ctl ripped off from under us */
4395 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4396 atomic_dec(&fs_info->balance_pause_req);
4401 mutex_unlock(&fs_info->balance_mutex);
4405 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4407 mutex_lock(&fs_info->balance_mutex);
4408 if (!fs_info->balance_ctl) {
4409 mutex_unlock(&fs_info->balance_mutex);
4414 * A paused balance with the item stored on disk can be resumed at
4415 * mount time if the mount is read-write. Otherwise it's still paused
4416 * and we must not allow cancelling as it deletes the item.
4418 if (sb_rdonly(fs_info->sb)) {
4419 mutex_unlock(&fs_info->balance_mutex);
4423 atomic_inc(&fs_info->balance_cancel_req);
4425 * if we are running just wait and return, balance item is
4426 * deleted in btrfs_balance in this case
4428 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4429 mutex_unlock(&fs_info->balance_mutex);
4430 wait_event(fs_info->balance_wait_q,
4431 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4432 mutex_lock(&fs_info->balance_mutex);
4434 mutex_unlock(&fs_info->balance_mutex);
4436 * Lock released to allow other waiters to continue, we'll
4437 * reexamine the status again.
4439 mutex_lock(&fs_info->balance_mutex);
4441 if (fs_info->balance_ctl) {
4442 reset_balance_state(fs_info);
4443 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4444 btrfs_info(fs_info, "balance: canceled");
4448 BUG_ON(fs_info->balance_ctl ||
4449 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4450 atomic_dec(&fs_info->balance_cancel_req);
4451 mutex_unlock(&fs_info->balance_mutex);
4455 static int btrfs_uuid_scan_kthread(void *data)
4457 struct btrfs_fs_info *fs_info = data;
4458 struct btrfs_root *root = fs_info->tree_root;
4459 struct btrfs_key key;
4460 struct btrfs_path *path = NULL;
4462 struct extent_buffer *eb;
4464 struct btrfs_root_item root_item;
4466 struct btrfs_trans_handle *trans = NULL;
4468 path = btrfs_alloc_path();
4475 key.type = BTRFS_ROOT_ITEM_KEY;
4479 ret = btrfs_search_forward(root, &key, path,
4480 BTRFS_OLDEST_GENERATION);
4487 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4488 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4489 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4490 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4493 eb = path->nodes[0];
4494 slot = path->slots[0];
4495 item_size = btrfs_item_size_nr(eb, slot);
4496 if (item_size < sizeof(root_item))
4499 read_extent_buffer(eb, &root_item,
4500 btrfs_item_ptr_offset(eb, slot),
4501 (int)sizeof(root_item));
4502 if (btrfs_root_refs(&root_item) == 0)
4505 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4506 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4510 btrfs_release_path(path);
4512 * 1 - subvol uuid item
4513 * 1 - received_subvol uuid item
4515 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4516 if (IS_ERR(trans)) {
4517 ret = PTR_ERR(trans);
4525 if (!btrfs_is_empty_uuid(root_item.uuid)) {
4526 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
4527 BTRFS_UUID_KEY_SUBVOL,
4530 btrfs_warn(fs_info, "uuid_tree_add failed %d",
4536 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
4537 ret = btrfs_uuid_tree_add(trans,
4538 root_item.received_uuid,
4539 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4542 btrfs_warn(fs_info, "uuid_tree_add failed %d",
4550 ret = btrfs_end_transaction(trans);
4556 btrfs_release_path(path);
4557 if (key.offset < (u64)-1) {
4559 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4561 key.type = BTRFS_ROOT_ITEM_KEY;
4562 } else if (key.objectid < (u64)-1) {
4564 key.type = BTRFS_ROOT_ITEM_KEY;
4573 btrfs_free_path(path);
4574 if (trans && !IS_ERR(trans))
4575 btrfs_end_transaction(trans);
4577 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
4579 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
4580 up(&fs_info->uuid_tree_rescan_sem);
4585 * Callback for btrfs_uuid_tree_iterate().
4587 * 0 check succeeded, the entry is not outdated.
4588 * < 0 if an error occurred.
4589 * > 0 if the check failed, which means the caller shall remove the entry.
4591 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4592 u8 *uuid, u8 type, u64 subid)
4594 struct btrfs_key key;
4596 struct btrfs_root *subvol_root;
4598 if (type != BTRFS_UUID_KEY_SUBVOL &&
4599 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4602 key.objectid = subid;
4603 key.type = BTRFS_ROOT_ITEM_KEY;
4604 key.offset = (u64)-1;
4605 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4606 if (IS_ERR(subvol_root)) {
4607 ret = PTR_ERR(subvol_root);
4614 case BTRFS_UUID_KEY_SUBVOL:
4615 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4618 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4619 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4629 static int btrfs_uuid_rescan_kthread(void *data)
4631 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4635 * 1st step is to iterate through the existing UUID tree and
4636 * to delete all entries that contain outdated data.
4637 * 2nd step is to add all missing entries to the UUID tree.
4639 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4641 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
4642 up(&fs_info->uuid_tree_rescan_sem);
4645 return btrfs_uuid_scan_kthread(data);
4648 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4650 struct btrfs_trans_handle *trans;
4651 struct btrfs_root *tree_root = fs_info->tree_root;
4652 struct btrfs_root *uuid_root;
4653 struct task_struct *task;
4660 trans = btrfs_start_transaction(tree_root, 2);
4662 return PTR_ERR(trans);
4664 uuid_root = btrfs_create_tree(trans, fs_info,
4665 BTRFS_UUID_TREE_OBJECTID);
4666 if (IS_ERR(uuid_root)) {
4667 ret = PTR_ERR(uuid_root);
4668 btrfs_abort_transaction(trans, ret);
4669 btrfs_end_transaction(trans);
4673 fs_info->uuid_root = uuid_root;
4675 ret = btrfs_commit_transaction(trans);
4679 down(&fs_info->uuid_tree_rescan_sem);
4680 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4682 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4683 btrfs_warn(fs_info, "failed to start uuid_scan task");
4684 up(&fs_info->uuid_tree_rescan_sem);
4685 return PTR_ERR(task);
4691 int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4693 struct task_struct *task;
4695 down(&fs_info->uuid_tree_rescan_sem);
4696 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4698 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4699 btrfs_warn(fs_info, "failed to start uuid_rescan task");
4700 up(&fs_info->uuid_tree_rescan_sem);
4701 return PTR_ERR(task);
4708 * shrinking a device means finding all of the device extents past
4709 * the new size, and then following the back refs to the chunks.
4710 * The chunk relocation code actually frees the device extent
4712 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4714 struct btrfs_fs_info *fs_info = device->fs_info;
4715 struct btrfs_root *root = fs_info->dev_root;
4716 struct btrfs_trans_handle *trans;
4717 struct btrfs_dev_extent *dev_extent = NULL;
4718 struct btrfs_path *path;
4724 bool retried = false;
4725 bool checked_pending_chunks = false;
4726 struct extent_buffer *l;
4727 struct btrfs_key key;
4728 struct btrfs_super_block *super_copy = fs_info->super_copy;
4729 u64 old_total = btrfs_super_total_bytes(super_copy);
4730 u64 old_size = btrfs_device_get_total_bytes(device);
4733 new_size = round_down(new_size, fs_info->sectorsize);
4734 diff = round_down(old_size - new_size, fs_info->sectorsize);
4736 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
4739 path = btrfs_alloc_path();
4743 path->reada = READA_BACK;
4745 mutex_lock(&fs_info->chunk_mutex);
4747 btrfs_device_set_total_bytes(device, new_size);
4748 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
4749 device->fs_devices->total_rw_bytes -= diff;
4750 atomic64_sub(diff, &fs_info->free_chunk_space);
4752 mutex_unlock(&fs_info->chunk_mutex);
4755 key.objectid = device->devid;
4756 key.offset = (u64)-1;
4757 key.type = BTRFS_DEV_EXTENT_KEY;
4760 mutex_lock(&fs_info->delete_unused_bgs_mutex);
4761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4763 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4767 ret = btrfs_previous_item(root, path, 0, key.type);
4769 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4774 btrfs_release_path(path);
4779 slot = path->slots[0];
4780 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4782 if (key.objectid != device->devid) {
4783 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4784 btrfs_release_path(path);
4788 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4789 length = btrfs_dev_extent_length(l, dev_extent);
4791 if (key.offset + length <= new_size) {
4792 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4793 btrfs_release_path(path);
4797 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
4798 btrfs_release_path(path);
4801 * We may be relocating the only data chunk we have,
4802 * which could potentially end up with losing data's
4803 * raid profile, so lets allocate an empty one in
4806 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4808 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4812 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4813 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4814 if (ret == -ENOSPC) {
4817 if (ret == -ETXTBSY) {
4819 "could not shrink block group %llu due to active swapfile",
4824 } while (key.offset-- > 0);
4826 if (failed && !retried) {
4830 } else if (failed && retried) {
4835 /* Shrinking succeeded, else we would be at "done". */
4836 trans = btrfs_start_transaction(root, 0);
4837 if (IS_ERR(trans)) {
4838 ret = PTR_ERR(trans);
4842 mutex_lock(&fs_info->chunk_mutex);
4845 * We checked in the above loop all device extents that were already in
4846 * the device tree. However before we have updated the device's
4847 * total_bytes to the new size, we might have had chunk allocations that
4848 * have not complete yet (new block groups attached to transaction
4849 * handles), and therefore their device extents were not yet in the
4850 * device tree and we missed them in the loop above. So if we have any
4851 * pending chunk using a device extent that overlaps the device range
4852 * that we can not use anymore, commit the current transaction and
4853 * repeat the search on the device tree - this way we guarantee we will
4854 * not have chunks using device extents that end beyond 'new_size'.
4856 if (!checked_pending_chunks) {
4857 u64 start = new_size;
4858 u64 len = old_size - new_size;
4860 if (contains_pending_extent(trans->transaction, device,
4862 mutex_unlock(&fs_info->chunk_mutex);
4863 checked_pending_chunks = true;
4866 ret = btrfs_commit_transaction(trans);
4873 btrfs_device_set_disk_total_bytes(device, new_size);
4874 if (list_empty(&device->resized_list))
4875 list_add_tail(&device->resized_list,
4876 &fs_info->fs_devices->resized_devices);
4878 WARN_ON(diff > old_total);
4879 btrfs_set_super_total_bytes(super_copy,
4880 round_down(old_total - diff, fs_info->sectorsize));
4881 mutex_unlock(&fs_info->chunk_mutex);
4883 /* Now btrfs_update_device() will change the on-disk size. */
4884 ret = btrfs_update_device(trans, device);
4886 btrfs_abort_transaction(trans, ret);
4887 btrfs_end_transaction(trans);
4889 ret = btrfs_commit_transaction(trans);
4892 btrfs_free_path(path);
4894 mutex_lock(&fs_info->chunk_mutex);
4895 btrfs_device_set_total_bytes(device, old_size);
4896 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
4897 device->fs_devices->total_rw_bytes += diff;
4898 atomic64_add(diff, &fs_info->free_chunk_space);
4899 mutex_unlock(&fs_info->chunk_mutex);
4904 static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
4905 struct btrfs_key *key,
4906 struct btrfs_chunk *chunk, int item_size)
4908 struct btrfs_super_block *super_copy = fs_info->super_copy;
4909 struct btrfs_disk_key disk_key;
4913 mutex_lock(&fs_info->chunk_mutex);
4914 array_size = btrfs_super_sys_array_size(super_copy);
4915 if (array_size + item_size + sizeof(disk_key)
4916 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4917 mutex_unlock(&fs_info->chunk_mutex);
4921 ptr = super_copy->sys_chunk_array + array_size;
4922 btrfs_cpu_key_to_disk(&disk_key, key);
4923 memcpy(ptr, &disk_key, sizeof(disk_key));
4924 ptr += sizeof(disk_key);
4925 memcpy(ptr, chunk, item_size);
4926 item_size += sizeof(disk_key);
4927 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4928 mutex_unlock(&fs_info->chunk_mutex);
4934 * sort the devices in descending order by max_avail, total_avail
4936 static int btrfs_cmp_device_info(const void *a, const void *b)
4938 const struct btrfs_device_info *di_a = a;
4939 const struct btrfs_device_info *di_b = b;
4941 if (di_a->max_avail > di_b->max_avail)
4943 if (di_a->max_avail < di_b->max_avail)
4945 if (di_a->total_avail > di_b->total_avail)
4947 if (di_a->total_avail < di_b->total_avail)
4952 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4954 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
4957 btrfs_set_fs_incompat(info, RAID56);
4960 #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
4961 - sizeof(struct btrfs_chunk)) \
4962 / sizeof(struct btrfs_stripe) + 1)
4964 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4965 - 2 * sizeof(struct btrfs_disk_key) \
4966 - 2 * sizeof(struct btrfs_chunk)) \
4967 / sizeof(struct btrfs_stripe) + 1)
4969 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4970 u64 start, u64 type)
4972 struct btrfs_fs_info *info = trans->fs_info;
4973 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4974 struct btrfs_device *device;
4975 struct map_lookup *map = NULL;
4976 struct extent_map_tree *em_tree;
4977 struct extent_map *em;
4978 struct btrfs_device_info *devices_info = NULL;
4980 int num_stripes; /* total number of stripes to allocate */
4981 int data_stripes; /* number of stripes that count for
4983 int sub_stripes; /* sub_stripes info for map */
4984 int dev_stripes; /* stripes per dev */
4985 int devs_max; /* max devs to use */
4986 int devs_min; /* min devs needed */
4987 int devs_increment; /* ndevs has to be a multiple of this */
4988 int ncopies; /* how many copies to data has */
4989 int nparity; /* number of stripes worth of bytes to
4990 store parity information */
4992 u64 max_stripe_size;
5001 BUG_ON(!alloc_profile_is_valid(type, 0));
5003 if (list_empty(&fs_devices->alloc_list)) {
5004 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5005 btrfs_debug(info, "%s: no writable device", __func__);
5009 index = btrfs_bg_flags_to_raid_index(type);
5011 sub_stripes = btrfs_raid_array[index].sub_stripes;
5012 dev_stripes = btrfs_raid_array[index].dev_stripes;
5013 devs_max = btrfs_raid_array[index].devs_max;
5014 devs_min = btrfs_raid_array[index].devs_min;
5015 devs_increment = btrfs_raid_array[index].devs_increment;
5016 ncopies = btrfs_raid_array[index].ncopies;
5017 nparity = btrfs_raid_array[index].nparity;
5019 if (type & BTRFS_BLOCK_GROUP_DATA) {
5020 max_stripe_size = SZ_1G;
5021 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
5023 devs_max = BTRFS_MAX_DEVS(info);
5024 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
5025 /* for larger filesystems, use larger metadata chunks */
5026 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
5027 max_stripe_size = SZ_1G;
5029 max_stripe_size = SZ_256M;
5030 max_chunk_size = max_stripe_size;
5032 devs_max = BTRFS_MAX_DEVS(info);
5033 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
5034 max_stripe_size = SZ_32M;
5035 max_chunk_size = 2 * max_stripe_size;
5037 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
5039 btrfs_err(info, "invalid chunk type 0x%llx requested",
5044 /* We don't want a chunk larger than 10% of writable space */
5045 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
5048 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
5054 * in the first pass through the devices list, we gather information
5055 * about the available holes on each device.
5058 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
5062 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
5064 "BTRFS: read-only device in alloc_list\n");
5068 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
5069 &device->dev_state) ||
5070 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
5073 if (device->total_bytes > device->bytes_used)
5074 total_avail = device->total_bytes - device->bytes_used;
5078 /* If there is no space on this device, skip it. */
5079 if (total_avail == 0)
5082 ret = find_free_dev_extent(trans, device,
5083 max_stripe_size * dev_stripes,
5084 &dev_offset, &max_avail);
5085 if (ret && ret != -ENOSPC)
5089 max_avail = max_stripe_size * dev_stripes;
5091 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
5092 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5094 "%s: devid %llu has no free space, have=%llu want=%u",
5095 __func__, device->devid, max_avail,
5096 BTRFS_STRIPE_LEN * dev_stripes);
5100 if (ndevs == fs_devices->rw_devices) {
5101 WARN(1, "%s: found more than %llu devices\n",
5102 __func__, fs_devices->rw_devices);
5105 devices_info[ndevs].dev_offset = dev_offset;
5106 devices_info[ndevs].max_avail = max_avail;
5107 devices_info[ndevs].total_avail = total_avail;
5108 devices_info[ndevs].dev = device;
5113 * now sort the devices by hole size / available space
5115 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
5116 btrfs_cmp_device_info, NULL);
5118 /* round down to number of usable stripes */
5119 ndevs = round_down(ndevs, devs_increment);
5121 if (ndevs < devs_min) {
5123 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5125 "%s: not enough devices with free space: have=%d minimum required=%d",
5126 __func__, ndevs, devs_min);
5131 ndevs = min(ndevs, devs_max);
5134 * The primary goal is to maximize the number of stripes, so use as
5135 * many devices as possible, even if the stripes are not maximum sized.
5137 * The DUP profile stores more than one stripe per device, the
5138 * max_avail is the total size so we have to adjust.
5140 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
5141 num_stripes = ndevs * dev_stripes;
5144 * this will have to be fixed for RAID1 and RAID10 over
5147 data_stripes = (num_stripes - nparity) / ncopies;
5150 * Use the number of data stripes to figure out how big this chunk
5151 * is really going to be in terms of logical address space,
5152 * and compare that answer with the max chunk size. If it's higher,
5153 * we try to reduce stripe_size.
5155 if (stripe_size * data_stripes > max_chunk_size) {
5157 * Reduce stripe_size, round it up to a 16MB boundary again and
5158 * then use it, unless it ends up being even bigger than the
5159 * previous value we had already.
5161 stripe_size = min(round_up(div_u64(max_chunk_size,
5162 data_stripes), SZ_16M),
5166 /* align to BTRFS_STRIPE_LEN */
5167 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
5169 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5174 map->num_stripes = num_stripes;
5176 for (i = 0; i < ndevs; ++i) {
5177 for (j = 0; j < dev_stripes; ++j) {
5178 int s = i * dev_stripes + j;
5179 map->stripes[s].dev = devices_info[i].dev;
5180 map->stripes[s].physical = devices_info[i].dev_offset +
5184 map->stripe_len = BTRFS_STRIPE_LEN;
5185 map->io_align = BTRFS_STRIPE_LEN;
5186 map->io_width = BTRFS_STRIPE_LEN;
5188 map->sub_stripes = sub_stripes;
5190 chunk_size = stripe_size * data_stripes;
5192 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
5194 em = alloc_extent_map();
5200 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
5201 em->map_lookup = map;
5203 em->len = chunk_size;
5204 em->block_start = 0;
5205 em->block_len = em->len;
5206 em->orig_block_len = stripe_size;
5208 em_tree = &info->mapping_tree.map_tree;
5209 write_lock(&em_tree->lock);
5210 ret = add_extent_mapping(em_tree, em, 0);
5212 write_unlock(&em_tree->lock);
5213 free_extent_map(em);
5217 list_add_tail(&em->list, &trans->transaction->pending_chunks);
5218 refcount_inc(&em->refs);
5219 write_unlock(&em_tree->lock);
5221 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
5223 goto error_del_extent;
5225 for (i = 0; i < map->num_stripes; i++)
5226 btrfs_device_set_bytes_used(map->stripes[i].dev,
5227 map->stripes[i].dev->bytes_used + stripe_size);
5229 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
5231 free_extent_map(em);
5232 check_raid56_incompat_flag(info, type);
5234 kfree(devices_info);
5238 write_lock(&em_tree->lock);
5239 remove_extent_mapping(em_tree, em);
5240 write_unlock(&em_tree->lock);
5242 /* One for our allocation */
5243 free_extent_map(em);
5244 /* One for the tree reference */
5245 free_extent_map(em);
5246 /* One for the pending_chunks list reference */
5247 free_extent_map(em);
5249 kfree(devices_info);
5253 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
5254 u64 chunk_offset, u64 chunk_size)
5256 struct btrfs_fs_info *fs_info = trans->fs_info;
5257 struct btrfs_root *extent_root = fs_info->extent_root;
5258 struct btrfs_root *chunk_root = fs_info->chunk_root;
5259 struct btrfs_key key;
5260 struct btrfs_device *device;
5261 struct btrfs_chunk *chunk;
5262 struct btrfs_stripe *stripe;
5263 struct extent_map *em;
5264 struct map_lookup *map;
5271 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
5275 map = em->map_lookup;
5276 item_size = btrfs_chunk_item_size(map->num_stripes);
5277 stripe_size = em->orig_block_len;
5279 chunk = kzalloc(item_size, GFP_NOFS);
5286 * Take the device list mutex to prevent races with the final phase of
5287 * a device replace operation that replaces the device object associated
5288 * with the map's stripes, because the device object's id can change
5289 * at any time during that final phase of the device replace operation
5290 * (dev-replace.c:btrfs_dev_replace_finishing()).
5292 mutex_lock(&fs_info->fs_devices->device_list_mutex);
5293 for (i = 0; i < map->num_stripes; i++) {
5294 device = map->stripes[i].dev;
5295 dev_offset = map->stripes[i].physical;
5297 ret = btrfs_update_device(trans, device);
5300 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5301 dev_offset, stripe_size);
5306 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
5310 stripe = &chunk->stripe;
5311 for (i = 0; i < map->num_stripes; i++) {
5312 device = map->stripes[i].dev;
5313 dev_offset = map->stripes[i].physical;
5315 btrfs_set_stack_stripe_devid(stripe, device->devid);
5316 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5317 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5320 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
5322 btrfs_set_stack_chunk_length(chunk, chunk_size);
5323 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5324 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5325 btrfs_set_stack_chunk_type(chunk, map->type);
5326 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5327 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5328 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
5329 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
5330 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5332 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5333 key.type = BTRFS_CHUNK_ITEM_KEY;
5334 key.offset = chunk_offset;
5336 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
5337 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5339 * TODO: Cleanup of inserted chunk root in case of
5342 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
5347 free_extent_map(em);
5352 * Chunk allocation falls into two parts. The first part does work
5353 * that makes the new allocated chunk usable, but does not do any operation
5354 * that modifies the chunk tree. The second part does the work that
5355 * requires modifying the chunk tree. This division is important for the
5356 * bootstrap process of adding storage to a seed btrfs.
5358 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
5362 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5363 chunk_offset = find_next_chunk(trans->fs_info);
5364 return __btrfs_alloc_chunk(trans, chunk_offset, type);
5367 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
5368 struct btrfs_fs_info *fs_info)
5371 u64 sys_chunk_offset;
5375 chunk_offset = find_next_chunk(fs_info);
5376 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
5377 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
5381 sys_chunk_offset = find_next_chunk(fs_info);
5382 alloc_profile = btrfs_system_alloc_profile(fs_info);
5383 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
5387 static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5391 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5392 BTRFS_BLOCK_GROUP_RAID10 |
5393 BTRFS_BLOCK_GROUP_RAID5 |
5394 BTRFS_BLOCK_GROUP_DUP)) {
5396 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5405 int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
5407 struct extent_map *em;
5408 struct map_lookup *map;
5413 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
5417 map = em->map_lookup;
5418 for (i = 0; i < map->num_stripes; i++) {
5419 if (test_bit(BTRFS_DEV_STATE_MISSING,
5420 &map->stripes[i].dev->dev_state)) {
5424 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5425 &map->stripes[i].dev->dev_state)) {
5432 * If the number of missing devices is larger than max errors,
5433 * we can not write the data into that chunk successfully, so
5436 if (miss_ndevs > btrfs_chunk_max_errors(map))
5439 free_extent_map(em);
5443 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5445 extent_map_tree_init(&tree->map_tree);
5448 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5450 struct extent_map *em;
5453 write_lock(&tree->map_tree.lock);
5454 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5456 remove_extent_mapping(&tree->map_tree, em);
5457 write_unlock(&tree->map_tree.lock);
5461 free_extent_map(em);
5462 /* once for the tree */
5463 free_extent_map(em);
5467 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5469 struct extent_map *em;
5470 struct map_lookup *map;
5473 em = btrfs_get_chunk_map(fs_info, logical, len);
5476 * We could return errors for these cases, but that could get
5477 * ugly and we'd probably do the same thing which is just not do
5478 * anything else and exit, so return 1 so the callers don't try
5479 * to use other copies.
5483 map = em->map_lookup;
5484 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5485 ret = map->num_stripes;
5486 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5487 ret = map->sub_stripes;
5488 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5490 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5492 * There could be two corrupted data stripes, we need
5493 * to loop retry in order to rebuild the correct data.
5495 * Fail a stripe at a time on every retry except the
5496 * stripe under reconstruction.
5498 ret = map->num_stripes;
5501 free_extent_map(em);
5503 down_read(&fs_info->dev_replace.rwsem);
5504 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5505 fs_info->dev_replace.tgtdev)
5507 up_read(&fs_info->dev_replace.rwsem);
5512 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
5515 struct extent_map *em;
5516 struct map_lookup *map;
5517 unsigned long len = fs_info->sectorsize;
5519 em = btrfs_get_chunk_map(fs_info, logical, len);
5521 if (!WARN_ON(IS_ERR(em))) {
5522 map = em->map_lookup;
5523 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5524 len = map->stripe_len * nr_data_stripes(map);
5525 free_extent_map(em);
5530 int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5532 struct extent_map *em;
5533 struct map_lookup *map;
5536 em = btrfs_get_chunk_map(fs_info, logical, len);
5538 if(!WARN_ON(IS_ERR(em))) {
5539 map = em->map_lookup;
5540 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5542 free_extent_map(em);
5547 static int find_live_mirror(struct btrfs_fs_info *fs_info,
5548 struct map_lookup *map, int first,
5549 int dev_replace_is_ongoing)
5553 int preferred_mirror;
5555 struct btrfs_device *srcdev;
5558 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5560 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5561 num_stripes = map->sub_stripes;
5563 num_stripes = map->num_stripes;
5565 preferred_mirror = first + current->pid % num_stripes;
5567 if (dev_replace_is_ongoing &&
5568 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5569 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5570 srcdev = fs_info->dev_replace.srcdev;
5575 * try to avoid the drive that is the source drive for a
5576 * dev-replace procedure, only choose it if no other non-missing
5577 * mirror is available
5579 for (tolerance = 0; tolerance < 2; tolerance++) {
5580 if (map->stripes[preferred_mirror].dev->bdev &&
5581 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5582 return preferred_mirror;
5583 for (i = first; i < first + num_stripes; i++) {
5584 if (map->stripes[i].dev->bdev &&
5585 (tolerance || map->stripes[i].dev != srcdev))
5590 /* we couldn't find one that doesn't fail. Just return something
5591 * and the io error handling code will clean up eventually
5593 return preferred_mirror;
5596 static inline int parity_smaller(u64 a, u64 b)
5601 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
5602 static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
5604 struct btrfs_bio_stripe s;
5611 for (i = 0; i < num_stripes - 1; i++) {
5612 if (parity_smaller(bbio->raid_map[i],
5613 bbio->raid_map[i+1])) {
5614 s = bbio->stripes[i];
5615 l = bbio->raid_map[i];
5616 bbio->stripes[i] = bbio->stripes[i+1];
5617 bbio->raid_map[i] = bbio->raid_map[i+1];
5618 bbio->stripes[i+1] = s;
5619 bbio->raid_map[i+1] = l;
5627 static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5629 struct btrfs_bio *bbio = kzalloc(
5630 /* the size of the btrfs_bio */
5631 sizeof(struct btrfs_bio) +
5632 /* plus the variable array for the stripes */
5633 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
5634 /* plus the variable array for the tgt dev */
5635 sizeof(int) * (real_stripes) +
5637 * plus the raid_map, which includes both the tgt dev
5640 sizeof(u64) * (total_stripes),
5641 GFP_NOFS|__GFP_NOFAIL);
5643 atomic_set(&bbio->error, 0);
5644 refcount_set(&bbio->refs, 1);
5649 void btrfs_get_bbio(struct btrfs_bio *bbio)
5651 WARN_ON(!refcount_read(&bbio->refs));
5652 refcount_inc(&bbio->refs);
5655 void btrfs_put_bbio(struct btrfs_bio *bbio)
5659 if (refcount_dec_and_test(&bbio->refs))
5663 /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5665 * Please note that, discard won't be sent to target device of device
5668 static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5669 u64 logical, u64 length,
5670 struct btrfs_bio **bbio_ret)
5672 struct extent_map *em;
5673 struct map_lookup *map;
5674 struct btrfs_bio *bbio;
5678 u64 stripe_end_offset;
5685 u32 sub_stripes = 0;
5686 u64 stripes_per_dev = 0;
5687 u32 remaining_stripes = 0;
5688 u32 last_stripe = 0;
5692 /* discard always return a bbio */
5695 em = btrfs_get_chunk_map(fs_info, logical, length);
5699 map = em->map_lookup;
5700 /* we don't discard raid56 yet */
5701 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5706 offset = logical - em->start;
5707 length = min_t(u64, em->len - offset, length);
5709 stripe_len = map->stripe_len;
5711 * stripe_nr counts the total number of stripes we have to stride
5712 * to get to this block
5714 stripe_nr = div64_u64(offset, stripe_len);
5716 /* stripe_offset is the offset of this block in its stripe */
5717 stripe_offset = offset - stripe_nr * stripe_len;
5719 stripe_nr_end = round_up(offset + length, map->stripe_len);
5720 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
5721 stripe_cnt = stripe_nr_end - stripe_nr;
5722 stripe_end_offset = stripe_nr_end * map->stripe_len -
5725 * after this, stripe_nr is the number of stripes on this
5726 * device we have to walk to find the data, and stripe_index is
5727 * the number of our device in the stripe array
5731 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5732 BTRFS_BLOCK_GROUP_RAID10)) {
5733 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5736 sub_stripes = map->sub_stripes;
5738 factor = map->num_stripes / sub_stripes;
5739 num_stripes = min_t(u64, map->num_stripes,
5740 sub_stripes * stripe_cnt);
5741 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5742 stripe_index *= sub_stripes;
5743 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5744 &remaining_stripes);
5745 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5746 last_stripe *= sub_stripes;
5747 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5748 BTRFS_BLOCK_GROUP_DUP)) {
5749 num_stripes = map->num_stripes;
5751 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5755 bbio = alloc_btrfs_bio(num_stripes, 0);
5761 for (i = 0; i < num_stripes; i++) {
5762 bbio->stripes[i].physical =
5763 map->stripes[stripe_index].physical +
5764 stripe_offset + stripe_nr * map->stripe_len;
5765 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5767 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5768 BTRFS_BLOCK_GROUP_RAID10)) {
5769 bbio->stripes[i].length = stripes_per_dev *
5772 if (i / sub_stripes < remaining_stripes)
5773 bbio->stripes[i].length +=
5777 * Special for the first stripe and
5780 * |-------|...|-------|
5784 if (i < sub_stripes)
5785 bbio->stripes[i].length -=
5788 if (stripe_index >= last_stripe &&
5789 stripe_index <= (last_stripe +
5791 bbio->stripes[i].length -=
5794 if (i == sub_stripes - 1)
5797 bbio->stripes[i].length = length;
5801 if (stripe_index == map->num_stripes) {
5808 bbio->map_type = map->type;
5809 bbio->num_stripes = num_stripes;
5811 free_extent_map(em);
5816 * In dev-replace case, for repair case (that's the only case where the mirror
5817 * is selected explicitly when calling btrfs_map_block), blocks left of the
5818 * left cursor can also be read from the target drive.
5820 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5822 * For READ, it also needs to be supported using the same mirror number.
5824 * If the requested block is not left of the left cursor, EIO is returned. This
5825 * can happen because btrfs_num_copies() returns one more in the dev-replace
5828 static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5829 u64 logical, u64 length,
5830 u64 srcdev_devid, int *mirror_num,
5833 struct btrfs_bio *bbio = NULL;
5835 int index_srcdev = 0;
5837 u64 physical_of_found = 0;
5841 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5842 logical, &length, &bbio, 0, 0);
5844 ASSERT(bbio == NULL);
5848 num_stripes = bbio->num_stripes;
5849 if (*mirror_num > num_stripes) {
5851 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5852 * that means that the requested area is not left of the left
5855 btrfs_put_bbio(bbio);
5860 * process the rest of the function using the mirror_num of the source
5861 * drive. Therefore look it up first. At the end, patch the device
5862 * pointer to the one of the target drive.
5864 for (i = 0; i < num_stripes; i++) {
5865 if (bbio->stripes[i].dev->devid != srcdev_devid)
5869 * In case of DUP, in order to keep it simple, only add the
5870 * mirror with the lowest physical address
5873 physical_of_found <= bbio->stripes[i].physical)
5878 physical_of_found = bbio->stripes[i].physical;
5881 btrfs_put_bbio(bbio);
5887 *mirror_num = index_srcdev + 1;
5888 *physical = physical_of_found;
5892 static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5893 struct btrfs_bio **bbio_ret,
5894 struct btrfs_dev_replace *dev_replace,
5895 int *num_stripes_ret, int *max_errors_ret)
5897 struct btrfs_bio *bbio = *bbio_ret;
5898 u64 srcdev_devid = dev_replace->srcdev->devid;
5899 int tgtdev_indexes = 0;
5900 int num_stripes = *num_stripes_ret;
5901 int max_errors = *max_errors_ret;
5904 if (op == BTRFS_MAP_WRITE) {
5905 int index_where_to_add;
5908 * duplicate the write operations while the dev replace
5909 * procedure is running. Since the copying of the old disk to
5910 * the new disk takes place at run time while the filesystem is
5911 * mounted writable, the regular write operations to the old
5912 * disk have to be duplicated to go to the new disk as well.
5914 * Note that device->missing is handled by the caller, and that
5915 * the write to the old disk is already set up in the stripes
5918 index_where_to_add = num_stripes;
5919 for (i = 0; i < num_stripes; i++) {
5920 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5921 /* write to new disk, too */
5922 struct btrfs_bio_stripe *new =
5923 bbio->stripes + index_where_to_add;
5924 struct btrfs_bio_stripe *old =
5927 new->physical = old->physical;
5928 new->length = old->length;
5929 new->dev = dev_replace->tgtdev;
5930 bbio->tgtdev_map[i] = index_where_to_add;
5931 index_where_to_add++;
5936 num_stripes = index_where_to_add;
5937 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5938 int index_srcdev = 0;
5940 u64 physical_of_found = 0;
5943 * During the dev-replace procedure, the target drive can also
5944 * be used to read data in case it is needed to repair a corrupt
5945 * block elsewhere. This is possible if the requested area is
5946 * left of the left cursor. In this area, the target drive is a
5947 * full copy of the source drive.
5949 for (i = 0; i < num_stripes; i++) {
5950 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5952 * In case of DUP, in order to keep it simple,
5953 * only add the mirror with the lowest physical
5957 physical_of_found <=
5958 bbio->stripes[i].physical)
5962 physical_of_found = bbio->stripes[i].physical;
5966 struct btrfs_bio_stripe *tgtdev_stripe =
5967 bbio->stripes + num_stripes;
5969 tgtdev_stripe->physical = physical_of_found;
5970 tgtdev_stripe->length =
5971 bbio->stripes[index_srcdev].length;
5972 tgtdev_stripe->dev = dev_replace->tgtdev;
5973 bbio->tgtdev_map[index_srcdev] = num_stripes;
5980 *num_stripes_ret = num_stripes;
5981 *max_errors_ret = max_errors;
5982 bbio->num_tgtdevs = tgtdev_indexes;
5986 static bool need_full_stripe(enum btrfs_map_op op)
5988 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5991 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5992 enum btrfs_map_op op,
5993 u64 logical, u64 *length,
5994 struct btrfs_bio **bbio_ret,
5995 int mirror_num, int need_raid_map)
5997 struct extent_map *em;
5998 struct map_lookup *map;
6008 int tgtdev_indexes = 0;
6009 struct btrfs_bio *bbio = NULL;
6010 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6011 int dev_replace_is_ongoing = 0;
6012 int num_alloc_stripes;
6013 int patch_the_first_stripe_for_dev_replace = 0;
6014 u64 physical_to_patch_in_first_stripe = 0;
6015 u64 raid56_full_stripe_start = (u64)-1;
6017 if (op == BTRFS_MAP_DISCARD)
6018 return __btrfs_map_block_for_discard(fs_info, logical,
6021 em = btrfs_get_chunk_map(fs_info, logical, *length);
6025 map = em->map_lookup;
6026 offset = logical - em->start;
6028 stripe_len = map->stripe_len;
6031 * stripe_nr counts the total number of stripes we have to stride
6032 * to get to this block
6034 stripe_nr = div64_u64(stripe_nr, stripe_len);
6036 stripe_offset = stripe_nr * stripe_len;
6037 if (offset < stripe_offset) {
6039 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
6040 stripe_offset, offset, em->start, logical,
6042 free_extent_map(em);
6046 /* stripe_offset is the offset of this block in its stripe*/
6047 stripe_offset = offset - stripe_offset;
6049 /* if we're here for raid56, we need to know the stripe aligned start */
6050 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6051 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
6052 raid56_full_stripe_start = offset;
6054 /* allow a write of a full stripe, but make sure we don't
6055 * allow straddling of stripes
6057 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
6059 raid56_full_stripe_start *= full_stripe_len;
6062 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
6064 /* For writes to RAID[56], allow a full stripeset across all disks.
6065 For other RAID types and for RAID[56] reads, just allow a single
6066 stripe (on a single disk). */
6067 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
6068 (op == BTRFS_MAP_WRITE)) {
6069 max_len = stripe_len * nr_data_stripes(map) -
6070 (offset - raid56_full_stripe_start);
6072 /* we limit the length of each bio to what fits in a stripe */
6073 max_len = stripe_len - stripe_offset;
6075 *length = min_t(u64, em->len - offset, max_len);
6077 *length = em->len - offset;
6081 * This is for when we're called from btrfs_bio_fits_in_stripe and all
6082 * it cares about is the length
6087 down_read(&dev_replace->rwsem);
6088 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
6090 * Hold the semaphore for read during the whole operation, write is
6091 * requested at commit time but must wait.
6093 if (!dev_replace_is_ongoing)
6094 up_read(&dev_replace->rwsem);
6096 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
6097 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
6098 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6099 dev_replace->srcdev->devid,
6101 &physical_to_patch_in_first_stripe);
6105 patch_the_first_stripe_for_dev_replace = 1;
6106 } else if (mirror_num > map->num_stripes) {
6112 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6113 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6115 if (!need_full_stripe(op))
6117 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
6118 if (need_full_stripe(op))
6119 num_stripes = map->num_stripes;
6120 else if (mirror_num)
6121 stripe_index = mirror_num - 1;
6123 stripe_index = find_live_mirror(fs_info, map, 0,
6124 dev_replace_is_ongoing);
6125 mirror_num = stripe_index + 1;
6128 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
6129 if (need_full_stripe(op)) {
6130 num_stripes = map->num_stripes;
6131 } else if (mirror_num) {
6132 stripe_index = mirror_num - 1;
6137 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6138 u32 factor = map->num_stripes / map->sub_stripes;
6140 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
6141 stripe_index *= map->sub_stripes;
6143 if (need_full_stripe(op))
6144 num_stripes = map->sub_stripes;
6145 else if (mirror_num)
6146 stripe_index += mirror_num - 1;
6148 int old_stripe_index = stripe_index;
6149 stripe_index = find_live_mirror(fs_info, map,
6151 dev_replace_is_ongoing);
6152 mirror_num = stripe_index - old_stripe_index + 1;
6155 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6156 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
6157 /* push stripe_nr back to the start of the full stripe */
6158 stripe_nr = div64_u64(raid56_full_stripe_start,
6159 stripe_len * nr_data_stripes(map));
6161 /* RAID[56] write or recovery. Return all stripes */
6162 num_stripes = map->num_stripes;
6163 max_errors = nr_parity_stripes(map);
6165 *length = map->stripe_len;
6170 * Mirror #0 or #1 means the original data block.
6171 * Mirror #2 is RAID5 parity block.
6172 * Mirror #3 is RAID6 Q block.
6174 stripe_nr = div_u64_rem(stripe_nr,
6175 nr_data_stripes(map), &stripe_index);
6177 stripe_index = nr_data_stripes(map) +
6180 /* We distribute the parity blocks across stripes */
6181 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6183 if (!need_full_stripe(op) && mirror_num <= 1)
6188 * after this, stripe_nr is the number of stripes on this
6189 * device we have to walk to find the data, and stripe_index is
6190 * the number of our device in the stripe array
6192 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6194 mirror_num = stripe_index + 1;
6196 if (stripe_index >= map->num_stripes) {
6198 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
6199 stripe_index, map->num_stripes);
6204 num_alloc_stripes = num_stripes;
6205 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
6206 if (op == BTRFS_MAP_WRITE)
6207 num_alloc_stripes <<= 1;
6208 if (op == BTRFS_MAP_GET_READ_MIRRORS)
6209 num_alloc_stripes++;
6210 tgtdev_indexes = num_stripes;
6213 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
6218 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
6219 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
6221 /* build raid_map */
6222 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6223 (need_full_stripe(op) || mirror_num > 1)) {
6227 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6228 sizeof(struct btrfs_bio_stripe) *
6230 sizeof(int) * tgtdev_indexes);
6232 /* Work out the disk rotation on this stripe-set */
6233 div_u64_rem(stripe_nr, num_stripes, &rot);
6235 /* Fill in the logical address of each stripe */
6236 tmp = stripe_nr * nr_data_stripes(map);
6237 for (i = 0; i < nr_data_stripes(map); i++)
6238 bbio->raid_map[(i+rot) % num_stripes] =
6239 em->start + (tmp + i) * map->stripe_len;
6241 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6242 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6243 bbio->raid_map[(i+rot+1) % num_stripes] =
6248 for (i = 0; i < num_stripes; i++) {
6249 bbio->stripes[i].physical =
6250 map->stripes[stripe_index].physical +
6252 stripe_nr * map->stripe_len;
6253 bbio->stripes[i].dev =
6254 map->stripes[stripe_index].dev;
6258 if (need_full_stripe(op))
6259 max_errors = btrfs_chunk_max_errors(map);
6262 sort_parity_stripes(bbio, num_stripes);
6264 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
6265 need_full_stripe(op)) {
6266 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6271 bbio->map_type = map->type;
6272 bbio->num_stripes = num_stripes;
6273 bbio->max_errors = max_errors;
6274 bbio->mirror_num = mirror_num;
6277 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6278 * mirror_num == num_stripes + 1 && dev_replace target drive is
6279 * available as a mirror
6281 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6282 WARN_ON(num_stripes > 1);
6283 bbio->stripes[0].dev = dev_replace->tgtdev;
6284 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6285 bbio->mirror_num = map->num_stripes + 1;
6288 if (dev_replace_is_ongoing) {
6289 lockdep_assert_held(&dev_replace->rwsem);
6290 /* Unlock and let waiting writers proceed */
6291 up_read(&dev_replace->rwsem);
6293 free_extent_map(em);
6297 int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6298 u64 logical, u64 *length,
6299 struct btrfs_bio **bbio_ret, int mirror_num)
6301 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
6305 /* For Scrub/replace */
6306 int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6307 u64 logical, u64 *length,
6308 struct btrfs_bio **bbio_ret)
6310 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
6313 int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6314 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
6316 struct extent_map *em;
6317 struct map_lookup *map;
6325 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
6329 map = em->map_lookup;
6331 rmap_len = map->stripe_len;
6333 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
6334 length = div_u64(length, map->num_stripes / map->sub_stripes);
6335 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
6336 length = div_u64(length, map->num_stripes);
6337 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6338 length = div_u64(length, nr_data_stripes(map));
6339 rmap_len = map->stripe_len * nr_data_stripes(map);
6342 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
6343 BUG_ON(!buf); /* -ENOMEM */
6345 for (i = 0; i < map->num_stripes; i++) {
6346 if (map->stripes[i].physical > physical ||
6347 map->stripes[i].physical + length <= physical)
6350 stripe_nr = physical - map->stripes[i].physical;
6351 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
6353 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6354 stripe_nr = stripe_nr * map->num_stripes + i;
6355 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
6356 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6357 stripe_nr = stripe_nr * map->num_stripes + i;
6358 } /* else if RAID[56], multiply by nr_data_stripes().
6359 * Alternatively, just use rmap_len below instead of
6360 * map->stripe_len */
6362 bytenr = chunk_start + stripe_nr * rmap_len;
6363 WARN_ON(nr >= map->num_stripes);
6364 for (j = 0; j < nr; j++) {
6365 if (buf[j] == bytenr)
6369 WARN_ON(nr >= map->num_stripes);
6376 *stripe_len = rmap_len;
6378 free_extent_map(em);
6382 static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
6384 bio->bi_private = bbio->private;
6385 bio->bi_end_io = bbio->end_io;
6388 btrfs_put_bbio(bbio);
6391 static void btrfs_end_bio(struct bio *bio)
6393 struct btrfs_bio *bbio = bio->bi_private;
6394 int is_orig_bio = 0;
6396 if (bio->bi_status) {
6397 atomic_inc(&bbio->error);
6398 if (bio->bi_status == BLK_STS_IOERR ||
6399 bio->bi_status == BLK_STS_TARGET) {
6400 unsigned int stripe_index =
6401 btrfs_io_bio(bio)->stripe_index;
6402 struct btrfs_device *dev;
6404 BUG_ON(stripe_index >= bbio->num_stripes);
6405 dev = bbio->stripes[stripe_index].dev;
6407 if (bio_op(bio) == REQ_OP_WRITE)
6408 btrfs_dev_stat_inc_and_print(dev,
6409 BTRFS_DEV_STAT_WRITE_ERRS);
6411 btrfs_dev_stat_inc_and_print(dev,
6412 BTRFS_DEV_STAT_READ_ERRS);
6413 if (bio->bi_opf & REQ_PREFLUSH)
6414 btrfs_dev_stat_inc_and_print(dev,
6415 BTRFS_DEV_STAT_FLUSH_ERRS);
6420 if (bio == bbio->orig_bio)
6423 btrfs_bio_counter_dec(bbio->fs_info);
6425 if (atomic_dec_and_test(&bbio->stripes_pending)) {
6428 bio = bbio->orig_bio;
6431 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
6432 /* only send an error to the higher layers if it is
6433 * beyond the tolerance of the btrfs bio
6435 if (atomic_read(&bbio->error) > bbio->max_errors) {
6436 bio->bi_status = BLK_STS_IOERR;
6439 * this bio is actually up to date, we didn't
6440 * go over the max number of errors
6442 bio->bi_status = BLK_STS_OK;
6445 btrfs_end_bbio(bbio, bio);
6446 } else if (!is_orig_bio) {
6452 * see run_scheduled_bios for a description of why bios are collected for
6455 * This will add one bio to the pending list for a device and make sure
6456 * the work struct is scheduled.
6458 static noinline void btrfs_schedule_bio(struct btrfs_device *device,
6461 struct btrfs_fs_info *fs_info = device->fs_info;
6462 int should_queue = 1;
6463 struct btrfs_pending_bios *pending_bios;
6465 /* don't bother with additional async steps for reads, right now */
6466 if (bio_op(bio) == REQ_OP_READ) {
6467 btrfsic_submit_bio(bio);
6471 WARN_ON(bio->bi_next);
6472 bio->bi_next = NULL;
6474 spin_lock(&device->io_lock);
6475 if (op_is_sync(bio->bi_opf))
6476 pending_bios = &device->pending_sync_bios;
6478 pending_bios = &device->pending_bios;
6480 if (pending_bios->tail)
6481 pending_bios->tail->bi_next = bio;
6483 pending_bios->tail = bio;
6484 if (!pending_bios->head)
6485 pending_bios->head = bio;
6486 if (device->running_pending)
6489 spin_unlock(&device->io_lock);
6492 btrfs_queue_work(fs_info->submit_workers, &device->work);
6495 static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6496 u64 physical, int dev_nr, int async)
6498 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
6499 struct btrfs_fs_info *fs_info = bbio->fs_info;
6501 bio->bi_private = bbio;
6502 btrfs_io_bio(bio)->stripe_index = dev_nr;
6503 bio->bi_end_io = btrfs_end_bio;
6504 bio->bi_iter.bi_sector = physical >> 9;
6505 btrfs_debug_in_rcu(fs_info,
6506 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6507 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6508 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6509 bio->bi_iter.bi_size);
6510 bio_set_dev(bio, dev->bdev);
6512 btrfs_bio_counter_inc_noblocked(fs_info);
6515 btrfs_schedule_bio(dev, bio);
6517 btrfsic_submit_bio(bio);
6520 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6522 atomic_inc(&bbio->error);
6523 if (atomic_dec_and_test(&bbio->stripes_pending)) {
6524 /* Should be the original bio. */
6525 WARN_ON(bio != bbio->orig_bio);
6527 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
6528 bio->bi_iter.bi_sector = logical >> 9;
6529 if (atomic_read(&bbio->error) > bbio->max_errors)
6530 bio->bi_status = BLK_STS_IOERR;
6532 bio->bi_status = BLK_STS_OK;
6533 btrfs_end_bbio(bbio, bio);
6537 blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6538 int mirror_num, int async_submit)
6540 struct btrfs_device *dev;
6541 struct bio *first_bio = bio;
6542 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
6548 struct btrfs_bio *bbio = NULL;
6550 length = bio->bi_iter.bi_size;
6551 map_length = length;
6553 btrfs_bio_counter_inc_blocked(fs_info);
6554 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
6555 &map_length, &bbio, mirror_num, 1);
6557 btrfs_bio_counter_dec(fs_info);
6558 return errno_to_blk_status(ret);
6561 total_devs = bbio->num_stripes;
6562 bbio->orig_bio = first_bio;
6563 bbio->private = first_bio->bi_private;
6564 bbio->end_io = first_bio->bi_end_io;
6565 bbio->fs_info = fs_info;
6566 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6568 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
6569 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
6570 /* In this case, map_length has been set to the length of
6571 a single stripe; not the whole write */
6572 if (bio_op(bio) == REQ_OP_WRITE) {
6573 ret = raid56_parity_write(fs_info, bio, bbio,
6576 ret = raid56_parity_recover(fs_info, bio, bbio,
6577 map_length, mirror_num, 1);
6580 btrfs_bio_counter_dec(fs_info);
6581 return errno_to_blk_status(ret);
6584 if (map_length < length) {
6586 "mapping failed logical %llu bio len %llu len %llu",
6587 logical, length, map_length);
6591 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
6592 dev = bbio->stripes[dev_nr].dev;
6593 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6595 (bio_op(first_bio) == REQ_OP_WRITE &&
6596 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
6597 bbio_error(bbio, first_bio, logical);
6601 if (dev_nr < total_devs - 1)
6602 bio = btrfs_bio_clone(first_bio);
6606 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6607 dev_nr, async_submit);
6609 btrfs_bio_counter_dec(fs_info);
6614 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6617 * If devid and uuid are both specified, the match must be exact, otherwise
6618 * only devid is used.
6620 * If @seed is true, traverse through the seed devices.
6622 struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
6623 u64 devid, u8 *uuid, u8 *fsid,
6626 struct btrfs_device *device;
6628 while (fs_devices) {
6630 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6631 list_for_each_entry(device, &fs_devices->devices,
6633 if (device->devid == devid &&
6634 (!uuid || memcmp(device->uuid, uuid,
6635 BTRFS_UUID_SIZE) == 0))
6640 fs_devices = fs_devices->seed;
6647 static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
6648 u64 devid, u8 *dev_uuid)
6650 struct btrfs_device *device;
6652 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6656 list_add(&device->dev_list, &fs_devices->devices);
6657 device->fs_devices = fs_devices;
6658 fs_devices->num_devices++;
6660 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
6661 fs_devices->missing_devices++;
6667 * btrfs_alloc_device - allocate struct btrfs_device
6668 * @fs_info: used only for generating a new devid, can be NULL if
6669 * devid is provided (i.e. @devid != NULL).
6670 * @devid: a pointer to devid for this device. If NULL a new devid
6672 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6675 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6676 * on error. Returned struct is not linked onto any lists and must be
6677 * destroyed with btrfs_free_device.
6679 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6683 struct btrfs_device *dev;
6686 if (WARN_ON(!devid && !fs_info))
6687 return ERR_PTR(-EINVAL);
6689 dev = __alloc_device();
6698 ret = find_next_devid(fs_info, &tmp);
6700 btrfs_free_device(dev);
6701 return ERR_PTR(ret);
6707 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6709 generate_random_uuid(dev->uuid);
6711 btrfs_init_work(&dev->work, btrfs_submit_helper,
6712 pending_bios_fn, NULL, NULL);
6717 /* Return -EIO if any error, otherwise return 0. */
6718 static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
6719 struct extent_buffer *leaf,
6720 struct btrfs_chunk *chunk, u64 logical)
6730 length = btrfs_chunk_length(leaf, chunk);
6731 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6732 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6733 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6734 type = btrfs_chunk_type(leaf, chunk);
6737 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
6741 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6742 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
6745 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6746 btrfs_err(fs_info, "invalid chunk sectorsize %u",
6747 btrfs_chunk_sector_size(leaf, chunk));
6750 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6751 btrfs_err(fs_info, "invalid chunk length %llu", length);
6754 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
6755 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
6759 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6761 btrfs_err(fs_info, "unrecognized chunk type: %llu",
6762 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6763 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6764 btrfs_chunk_type(leaf, chunk));
6768 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
6769 btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type);
6773 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
6774 (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
6776 "system chunk with data or metadata type: 0x%llx", type);
6780 features = btrfs_super_incompat_flags(fs_info->super_copy);
6781 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
6785 if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
6786 (type & BTRFS_BLOCK_GROUP_DATA)) {
6788 "mixed chunk type in non-mixed mode: 0x%llx", type);
6793 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6794 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) ||
6795 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6796 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6797 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) ||
6798 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6799 num_stripes != 1)) {
6801 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6802 num_stripes, sub_stripes,
6803 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6810 static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
6811 u64 devid, u8 *uuid, bool error)
6814 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6817 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6821 static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
6822 struct extent_buffer *leaf,
6823 struct btrfs_chunk *chunk)
6825 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6826 struct map_lookup *map;
6827 struct extent_map *em;
6831 u8 uuid[BTRFS_UUID_SIZE];
6836 logical = key->offset;
6837 length = btrfs_chunk_length(leaf, chunk);
6838 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6840 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
6844 read_lock(&map_tree->map_tree.lock);
6845 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
6846 read_unlock(&map_tree->map_tree.lock);
6848 /* already mapped? */
6849 if (em && em->start <= logical && em->start + em->len > logical) {
6850 free_extent_map(em);
6853 free_extent_map(em);
6856 em = alloc_extent_map();
6859 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
6861 free_extent_map(em);
6865 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
6866 em->map_lookup = map;
6867 em->start = logical;
6870 em->block_start = 0;
6871 em->block_len = em->len;
6873 map->num_stripes = num_stripes;
6874 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6875 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6876 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6877 map->type = btrfs_chunk_type(leaf, chunk);
6878 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6879 map->verified_stripes = 0;
6880 for (i = 0; i < num_stripes; i++) {
6881 map->stripes[i].physical =
6882 btrfs_stripe_offset_nr(leaf, chunk, i);
6883 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
6884 read_extent_buffer(leaf, uuid, (unsigned long)
6885 btrfs_stripe_dev_uuid_nr(chunk, i),
6887 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
6888 devid, uuid, NULL, true);
6889 if (!map->stripes[i].dev &&
6890 !btrfs_test_opt(fs_info, DEGRADED)) {
6891 free_extent_map(em);
6892 btrfs_report_missing_device(fs_info, devid, uuid, true);
6895 if (!map->stripes[i].dev) {
6896 map->stripes[i].dev =
6897 add_missing_dev(fs_info->fs_devices, devid,
6899 if (IS_ERR(map->stripes[i].dev)) {
6900 free_extent_map(em);
6902 "failed to init missing dev %llu: %ld",
6903 devid, PTR_ERR(map->stripes[i].dev));
6904 return PTR_ERR(map->stripes[i].dev);
6906 btrfs_report_missing_device(fs_info, devid, uuid, false);
6908 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6909 &(map->stripes[i].dev->dev_state));
6913 write_lock(&map_tree->map_tree.lock);
6914 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
6915 write_unlock(&map_tree->map_tree.lock);
6918 "failed to add chunk map, start=%llu len=%llu: %d",
6919 em->start, em->len, ret);
6921 free_extent_map(em);
6926 static void fill_device_from_item(struct extent_buffer *leaf,
6927 struct btrfs_dev_item *dev_item,
6928 struct btrfs_device *device)
6932 device->devid = btrfs_device_id(leaf, dev_item);
6933 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6934 device->total_bytes = device->disk_total_bytes;
6935 device->commit_total_bytes = device->disk_total_bytes;
6936 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
6937 device->commit_bytes_used = device->bytes_used;
6938 device->type = btrfs_device_type(leaf, dev_item);
6939 device->io_align = btrfs_device_io_align(leaf, dev_item);
6940 device->io_width = btrfs_device_io_width(leaf, dev_item);
6941 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
6942 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
6943 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
6945 ptr = btrfs_device_uuid(dev_item);
6946 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
6949 static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
6952 struct btrfs_fs_devices *fs_devices;
6955 lockdep_assert_held(&uuid_mutex);
6958 fs_devices = fs_info->fs_devices->seed;
6959 while (fs_devices) {
6960 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
6963 fs_devices = fs_devices->seed;
6966 fs_devices = find_fsid(fsid, NULL);
6968 if (!btrfs_test_opt(fs_info, DEGRADED))
6969 return ERR_PTR(-ENOENT);
6971 fs_devices = alloc_fs_devices(fsid, NULL);
6972 if (IS_ERR(fs_devices))
6975 fs_devices->seeding = 1;
6976 fs_devices->opened = 1;
6980 fs_devices = clone_fs_devices(fs_devices);
6981 if (IS_ERR(fs_devices))
6984 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
6986 free_fs_devices(fs_devices);
6987 fs_devices = ERR_PTR(ret);
6991 if (!fs_devices->seeding) {
6992 close_fs_devices(fs_devices);
6993 free_fs_devices(fs_devices);
6994 fs_devices = ERR_PTR(-EINVAL);
6998 fs_devices->seed = fs_info->fs_devices->seed;
6999 fs_info->fs_devices->seed = fs_devices;
7004 static int read_one_dev(struct btrfs_fs_info *fs_info,
7005 struct extent_buffer *leaf,
7006 struct btrfs_dev_item *dev_item)
7008 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7009 struct btrfs_device *device;
7012 u8 fs_uuid[BTRFS_FSID_SIZE];
7013 u8 dev_uuid[BTRFS_UUID_SIZE];
7015 devid = btrfs_device_id(leaf, dev_item);
7016 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
7018 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
7021 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
7022 fs_devices = open_seed_devices(fs_info, fs_uuid);
7023 if (IS_ERR(fs_devices))
7024 return PTR_ERR(fs_devices);
7027 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
7030 if (!btrfs_test_opt(fs_info, DEGRADED)) {
7031 btrfs_report_missing_device(fs_info, devid,
7036 device = add_missing_dev(fs_devices, devid, dev_uuid);
7037 if (IS_ERR(device)) {
7039 "failed to add missing dev %llu: %ld",
7040 devid, PTR_ERR(device));
7041 return PTR_ERR(device);
7043 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
7045 if (!device->bdev) {
7046 if (!btrfs_test_opt(fs_info, DEGRADED)) {
7047 btrfs_report_missing_device(fs_info,
7048 devid, dev_uuid, true);
7051 btrfs_report_missing_device(fs_info, devid,
7055 if (!device->bdev &&
7056 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
7058 * this happens when a device that was properly setup
7059 * in the device info lists suddenly goes bad.
7060 * device->bdev is NULL, and so we have to set
7061 * device->missing to one here
7063 device->fs_devices->missing_devices++;
7064 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
7067 /* Move the device to its own fs_devices */
7068 if (device->fs_devices != fs_devices) {
7069 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
7070 &device->dev_state));
7072 list_move(&device->dev_list, &fs_devices->devices);
7073 device->fs_devices->num_devices--;
7074 fs_devices->num_devices++;
7076 device->fs_devices->missing_devices--;
7077 fs_devices->missing_devices++;
7079 device->fs_devices = fs_devices;
7083 if (device->fs_devices != fs_info->fs_devices) {
7084 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
7085 if (device->generation !=
7086 btrfs_device_generation(leaf, dev_item))
7090 fill_device_from_item(leaf, dev_item, device);
7091 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
7092 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
7093 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
7094 device->fs_devices->total_rw_bytes += device->total_bytes;
7095 atomic64_add(device->total_bytes - device->bytes_used,
7096 &fs_info->free_chunk_space);
7102 int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
7104 struct btrfs_root *root = fs_info->tree_root;
7105 struct btrfs_super_block *super_copy = fs_info->super_copy;
7106 struct extent_buffer *sb;
7107 struct btrfs_disk_key *disk_key;
7108 struct btrfs_chunk *chunk;
7110 unsigned long sb_array_offset;
7117 struct btrfs_key key;
7119 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
7121 * This will create extent buffer of nodesize, superblock size is
7122 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
7123 * overallocate but we can keep it as-is, only the first page is used.
7125 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
7128 set_extent_buffer_uptodate(sb);
7129 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
7131 * The sb extent buffer is artificial and just used to read the system array.
7132 * set_extent_buffer_uptodate() call does not properly mark all it's
7133 * pages up-to-date when the page is larger: extent does not cover the
7134 * whole page and consequently check_page_uptodate does not find all
7135 * the page's extents up-to-date (the hole beyond sb),
7136 * write_extent_buffer then triggers a WARN_ON.
7138 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
7139 * but sb spans only this function. Add an explicit SetPageUptodate call
7140 * to silence the warning eg. on PowerPC 64.
7142 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
7143 SetPageUptodate(sb->pages[0]);
7145 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
7146 array_size = btrfs_super_sys_array_size(super_copy);
7148 array_ptr = super_copy->sys_chunk_array;
7149 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
7152 while (cur_offset < array_size) {
7153 disk_key = (struct btrfs_disk_key *)array_ptr;
7154 len = sizeof(*disk_key);
7155 if (cur_offset + len > array_size)
7156 goto out_short_read;
7158 btrfs_disk_key_to_cpu(&key, disk_key);
7161 sb_array_offset += len;
7164 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
7165 chunk = (struct btrfs_chunk *)sb_array_offset;
7167 * At least one btrfs_chunk with one stripe must be
7168 * present, exact stripe count check comes afterwards
7170 len = btrfs_chunk_item_size(1);
7171 if (cur_offset + len > array_size)
7172 goto out_short_read;
7174 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
7177 "invalid number of stripes %u in sys_array at offset %u",
7178 num_stripes, cur_offset);
7183 type = btrfs_chunk_type(sb, chunk);
7184 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
7186 "invalid chunk type %llu in sys_array at offset %u",
7192 len = btrfs_chunk_item_size(num_stripes);
7193 if (cur_offset + len > array_size)
7194 goto out_short_read;
7196 ret = read_one_chunk(fs_info, &key, sb, chunk);
7201 "unexpected item type %u in sys_array at offset %u",
7202 (u32)key.type, cur_offset);
7207 sb_array_offset += len;
7210 clear_extent_buffer_uptodate(sb);
7211 free_extent_buffer_stale(sb);
7215 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
7217 clear_extent_buffer_uptodate(sb);
7218 free_extent_buffer_stale(sb);
7223 * Check if all chunks in the fs are OK for read-write degraded mount
7225 * If the @failing_dev is specified, it's accounted as missing.
7227 * Return true if all chunks meet the minimal RW mount requirements.
7228 * Return false if any chunk doesn't meet the minimal RW mount requirements.
7230 bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7231 struct btrfs_device *failing_dev)
7233 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
7234 struct extent_map *em;
7238 read_lock(&map_tree->map_tree.lock);
7239 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
7240 read_unlock(&map_tree->map_tree.lock);
7241 /* No chunk at all? Return false anyway */
7247 struct map_lookup *map;
7252 map = em->map_lookup;
7254 btrfs_get_num_tolerated_disk_barrier_failures(
7256 for (i = 0; i < map->num_stripes; i++) {
7257 struct btrfs_device *dev = map->stripes[i].dev;
7259 if (!dev || !dev->bdev ||
7260 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
7261 dev->last_flush_error)
7263 else if (failing_dev && failing_dev == dev)
7266 if (missing > max_tolerated) {
7269 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
7270 em->start, missing, max_tolerated);
7271 free_extent_map(em);
7275 next_start = extent_map_end(em);
7276 free_extent_map(em);
7278 read_lock(&map_tree->map_tree.lock);
7279 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
7280 (u64)(-1) - next_start);
7281 read_unlock(&map_tree->map_tree.lock);
7287 int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
7289 struct btrfs_root *root = fs_info->chunk_root;
7290 struct btrfs_path *path;
7291 struct extent_buffer *leaf;
7292 struct btrfs_key key;
7293 struct btrfs_key found_key;
7298 path = btrfs_alloc_path();
7303 * uuid_mutex is needed only if we are mounting a sprout FS
7304 * otherwise we don't need it.
7306 mutex_lock(&uuid_mutex);
7307 mutex_lock(&fs_info->chunk_mutex);
7310 * Read all device items, and then all the chunk items. All
7311 * device items are found before any chunk item (their object id
7312 * is smaller than the lowest possible object id for a chunk
7313 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
7315 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7322 leaf = path->nodes[0];
7323 slot = path->slots[0];
7324 if (slot >= btrfs_header_nritems(leaf)) {
7325 ret = btrfs_next_leaf(root, path);
7332 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7333 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7334 struct btrfs_dev_item *dev_item;
7335 dev_item = btrfs_item_ptr(leaf, slot,
7336 struct btrfs_dev_item);
7337 ret = read_one_dev(fs_info, leaf, dev_item);
7341 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7342 struct btrfs_chunk *chunk;
7343 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
7344 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
7352 * After loading chunk tree, we've got all device information,
7353 * do another round of validation checks.
7355 if (total_dev != fs_info->fs_devices->total_devices) {
7357 "super_num_devices %llu mismatch with num_devices %llu found here",
7358 btrfs_super_num_devices(fs_info->super_copy),
7363 if (btrfs_super_total_bytes(fs_info->super_copy) <
7364 fs_info->fs_devices->total_rw_bytes) {
7366 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
7367 btrfs_super_total_bytes(fs_info->super_copy),
7368 fs_info->fs_devices->total_rw_bytes);
7374 mutex_unlock(&fs_info->chunk_mutex);
7375 mutex_unlock(&uuid_mutex);
7377 btrfs_free_path(path);
7381 void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7383 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7384 struct btrfs_device *device;
7386 while (fs_devices) {
7387 mutex_lock(&fs_devices->device_list_mutex);
7388 list_for_each_entry(device, &fs_devices->devices, dev_list)
7389 device->fs_info = fs_info;
7390 mutex_unlock(&fs_devices->device_list_mutex);
7392 fs_devices = fs_devices->seed;
7396 static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7400 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7401 btrfs_dev_stat_reset(dev, i);
7404 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7406 struct btrfs_key key;
7407 struct btrfs_key found_key;
7408 struct btrfs_root *dev_root = fs_info->dev_root;
7409 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7410 struct extent_buffer *eb;
7413 struct btrfs_device *device;
7414 struct btrfs_path *path = NULL;
7417 path = btrfs_alloc_path();
7423 mutex_lock(&fs_devices->device_list_mutex);
7424 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7426 struct btrfs_dev_stats_item *ptr;
7428 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7429 key.type = BTRFS_PERSISTENT_ITEM_KEY;
7430 key.offset = device->devid;
7431 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7433 __btrfs_reset_dev_stats(device);
7434 device->dev_stats_valid = 1;
7435 btrfs_release_path(path);
7438 slot = path->slots[0];
7439 eb = path->nodes[0];
7440 btrfs_item_key_to_cpu(eb, &found_key, slot);
7441 item_size = btrfs_item_size_nr(eb, slot);
7443 ptr = btrfs_item_ptr(eb, slot,
7444 struct btrfs_dev_stats_item);
7446 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7447 if (item_size >= (1 + i) * sizeof(__le64))
7448 btrfs_dev_stat_set(device, i,
7449 btrfs_dev_stats_value(eb, ptr, i));
7451 btrfs_dev_stat_reset(device, i);
7454 device->dev_stats_valid = 1;
7455 btrfs_dev_stat_print_on_load(device);
7456 btrfs_release_path(path);
7458 mutex_unlock(&fs_devices->device_list_mutex);
7461 btrfs_free_path(path);
7462 return ret < 0 ? ret : 0;
7465 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
7466 struct btrfs_device *device)
7468 struct btrfs_fs_info *fs_info = trans->fs_info;
7469 struct btrfs_root *dev_root = fs_info->dev_root;
7470 struct btrfs_path *path;
7471 struct btrfs_key key;
7472 struct extent_buffer *eb;
7473 struct btrfs_dev_stats_item *ptr;
7477 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7478 key.type = BTRFS_PERSISTENT_ITEM_KEY;
7479 key.offset = device->devid;
7481 path = btrfs_alloc_path();
7484 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7486 btrfs_warn_in_rcu(fs_info,
7487 "error %d while searching for dev_stats item for device %s",
7488 ret, rcu_str_deref(device->name));
7493 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7494 /* need to delete old one and insert a new one */
7495 ret = btrfs_del_item(trans, dev_root, path);
7497 btrfs_warn_in_rcu(fs_info,
7498 "delete too small dev_stats item for device %s failed %d",
7499 rcu_str_deref(device->name), ret);
7506 /* need to insert a new item */
7507 btrfs_release_path(path);
7508 ret = btrfs_insert_empty_item(trans, dev_root, path,
7509 &key, sizeof(*ptr));
7511 btrfs_warn_in_rcu(fs_info,
7512 "insert dev_stats item for device %s failed %d",
7513 rcu_str_deref(device->name), ret);
7518 eb = path->nodes[0];
7519 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7520 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7521 btrfs_set_dev_stats_value(eb, ptr, i,
7522 btrfs_dev_stat_read(device, i));
7523 btrfs_mark_buffer_dirty(eb);
7526 btrfs_free_path(path);
7531 * called from commit_transaction. Writes all changed device stats to disk.
7533 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7534 struct btrfs_fs_info *fs_info)
7536 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7537 struct btrfs_device *device;
7541 mutex_lock(&fs_devices->device_list_mutex);
7542 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7543 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7544 if (!device->dev_stats_valid || stats_cnt == 0)
7549 * There is a LOAD-LOAD control dependency between the value of
7550 * dev_stats_ccnt and updating the on-disk values which requires
7551 * reading the in-memory counters. Such control dependencies
7552 * require explicit read memory barriers.
7554 * This memory barriers pairs with smp_mb__before_atomic in
7555 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7556 * barrier implied by atomic_xchg in
7557 * btrfs_dev_stats_read_and_reset
7561 ret = update_dev_stat_item(trans, device);
7563 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
7565 mutex_unlock(&fs_devices->device_list_mutex);
7570 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7572 btrfs_dev_stat_inc(dev, index);
7573 btrfs_dev_stat_print_on_error(dev);
7576 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
7578 if (!dev->dev_stats_valid)
7580 btrfs_err_rl_in_rcu(dev->fs_info,
7581 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7582 rcu_str_deref(dev->name),
7583 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7584 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7585 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7586 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7587 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7590 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7594 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7595 if (btrfs_dev_stat_read(dev, i) != 0)
7597 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7598 return; /* all values == 0, suppress message */
7600 btrfs_info_in_rcu(dev->fs_info,
7601 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7602 rcu_str_deref(dev->name),
7603 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7604 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7605 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7606 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7607 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7610 int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
7611 struct btrfs_ioctl_get_dev_stats *stats)
7613 struct btrfs_device *dev;
7614 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7617 mutex_lock(&fs_devices->device_list_mutex);
7618 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7620 mutex_unlock(&fs_devices->device_list_mutex);
7623 btrfs_warn(fs_info, "get dev_stats failed, device not found");
7625 } else if (!dev->dev_stats_valid) {
7626 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
7628 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
7629 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7630 if (stats->nr_items > i)
7632 btrfs_dev_stat_read_and_reset(dev, i);
7634 btrfs_dev_stat_reset(dev, i);
7637 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7638 if (stats->nr_items > i)
7639 stats->values[i] = btrfs_dev_stat_read(dev, i);
7641 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7642 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7646 void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
7648 struct buffer_head *bh;
7649 struct btrfs_super_block *disk_super;
7655 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7658 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7661 disk_super = (struct btrfs_super_block *)bh->b_data;
7663 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7664 set_buffer_dirty(bh);
7665 sync_dirty_buffer(bh);
7669 /* Notify udev that device has changed */
7670 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7672 /* Update ctime/mtime for device path for libblkid */
7673 update_dev_time(device_path);
7677 * Update the size of all devices, which is used for writing out the
7680 void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7682 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7683 struct btrfs_device *curr, *next;
7685 if (list_empty(&fs_devices->resized_devices))
7688 mutex_lock(&fs_devices->device_list_mutex);
7689 mutex_lock(&fs_info->chunk_mutex);
7690 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7692 list_del_init(&curr->resized_list);
7693 curr->commit_total_bytes = curr->disk_total_bytes;
7695 mutex_unlock(&fs_info->chunk_mutex);
7696 mutex_unlock(&fs_devices->device_list_mutex);
7699 /* Must be invoked during the transaction commit */
7700 void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
7702 struct btrfs_fs_info *fs_info = trans->fs_info;
7703 struct extent_map *em;
7704 struct map_lookup *map;
7705 struct btrfs_device *dev;
7708 if (list_empty(&trans->pending_chunks))
7711 /* In order to kick the device replace finish process */
7712 mutex_lock(&fs_info->chunk_mutex);
7713 list_for_each_entry(em, &trans->pending_chunks, list) {
7714 map = em->map_lookup;
7716 for (i = 0; i < map->num_stripes; i++) {
7717 dev = map->stripes[i].dev;
7718 dev->commit_bytes_used = dev->bytes_used;
7721 mutex_unlock(&fs_info->chunk_mutex);
7724 void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7726 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7727 while (fs_devices) {
7728 fs_devices->fs_info = fs_info;
7729 fs_devices = fs_devices->seed;
7733 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7735 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7736 while (fs_devices) {
7737 fs_devices->fs_info = NULL;
7738 fs_devices = fs_devices->seed;
7743 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7745 int btrfs_bg_type_to_factor(u64 flags)
7747 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
7748 BTRFS_BLOCK_GROUP_RAID10))
7754 static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
7756 int index = btrfs_bg_flags_to_raid_index(type);
7757 int ncopies = btrfs_raid_array[index].ncopies;
7760 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
7761 case BTRFS_BLOCK_GROUP_RAID5:
7762 data_stripes = num_stripes - 1;
7764 case BTRFS_BLOCK_GROUP_RAID6:
7765 data_stripes = num_stripes - 2;
7768 data_stripes = num_stripes / ncopies;
7771 return div_u64(chunk_len, data_stripes);
7774 static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7775 u64 chunk_offset, u64 devid,
7776 u64 physical_offset, u64 physical_len)
7778 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7779 struct extent_map *em;
7780 struct map_lookup *map;
7781 struct btrfs_device *dev;
7787 read_lock(&em_tree->lock);
7788 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7789 read_unlock(&em_tree->lock);
7793 "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7794 physical_offset, devid);
7799 map = em->map_lookup;
7800 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7801 if (physical_len != stripe_len) {
7803 "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7804 physical_offset, devid, em->start, physical_len,
7810 for (i = 0; i < map->num_stripes; i++) {
7811 if (map->stripes[i].dev->devid == devid &&
7812 map->stripes[i].physical == physical_offset) {
7814 if (map->verified_stripes >= map->num_stripes) {
7816 "too many dev extents for chunk %llu found",
7821 map->verified_stripes++;
7827 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7828 physical_offset, devid);
7832 /* Make sure no dev extent is beyond device bondary */
7833 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
7835 btrfs_err(fs_info, "failed to find devid %llu", devid);
7840 /* It's possible this device is a dummy for seed device */
7841 if (dev->disk_total_bytes == 0) {
7842 dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7845 btrfs_err(fs_info, "failed to find seed devid %llu",
7852 if (physical_offset + physical_len > dev->disk_total_bytes) {
7854 "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7855 devid, physical_offset, physical_len,
7856 dev->disk_total_bytes);
7861 free_extent_map(em);
7865 static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7867 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7868 struct extent_map *em;
7869 struct rb_node *node;
7872 read_lock(&em_tree->lock);
7873 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
7874 em = rb_entry(node, struct extent_map, rb_node);
7875 if (em->map_lookup->num_stripes !=
7876 em->map_lookup->verified_stripes) {
7878 "chunk %llu has missing dev extent, have %d expect %d",
7879 em->start, em->map_lookup->verified_stripes,
7880 em->map_lookup->num_stripes);
7886 read_unlock(&em_tree->lock);
7891 * Ensure that all dev extents are mapped to correct chunk, otherwise
7892 * later chunk allocation/free would cause unexpected behavior.
7894 * NOTE: This will iterate through the whole device tree, which should be of
7895 * the same size level as the chunk tree. This slightly increases mount time.
7897 int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7899 struct btrfs_path *path;
7900 struct btrfs_root *root = fs_info->dev_root;
7901 struct btrfs_key key;
7903 u64 prev_dev_ext_end = 0;
7907 key.type = BTRFS_DEV_EXTENT_KEY;
7910 path = btrfs_alloc_path();
7914 path->reada = READA_FORWARD;
7915 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7919 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7920 ret = btrfs_next_item(root, path);
7923 /* No dev extents at all? Not good */
7930 struct extent_buffer *leaf = path->nodes[0];
7931 struct btrfs_dev_extent *dext;
7932 int slot = path->slots[0];
7934 u64 physical_offset;
7938 btrfs_item_key_to_cpu(leaf, &key, slot);
7939 if (key.type != BTRFS_DEV_EXTENT_KEY)
7941 devid = key.objectid;
7942 physical_offset = key.offset;
7944 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7945 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7946 physical_len = btrfs_dev_extent_length(leaf, dext);
7948 /* Check if this dev extent overlaps with the previous one */
7949 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7951 "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7952 devid, physical_offset, prev_dev_ext_end);
7957 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7958 physical_offset, physical_len);
7962 prev_dev_ext_end = physical_offset + physical_len;
7964 ret = btrfs_next_item(root, path);
7973 /* Ensure all chunks have corresponding dev extents */
7974 ret = verify_chunk_dev_extent_mapping(fs_info);
7976 btrfs_free_path(path);
7981 * Check whether the given block group or device is pinned by any inode being
7982 * used as a swapfile.
7984 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7986 struct btrfs_swapfile_pin *sp;
7987 struct rb_node *node;
7989 spin_lock(&fs_info->swapfile_pins_lock);
7990 node = fs_info->swapfile_pins.rb_node;
7992 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7994 node = node->rb_left;
7995 else if (ptr > sp->ptr)
7996 node = node->rb_right;
8000 spin_unlock(&fs_info->swapfile_pins_lock);
8001 return node != NULL;