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>
19 #include "extent_map.h"
21 #include "transaction.h"
22 #include "print-tree.h"
25 #include "async-thread.h"
26 #include "check-integrity.h"
27 #include "rcu-string.h"
28 #include "dev-replace.h"
30 #include "tree-checker.h"
31 #include "space-info.h"
32 #include "block-group.h"
34 const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
35 [BTRFS_RAID_RAID10] = {
38 .devs_max = 0, /* 0 == as many as possible */
40 .tolerated_failures = 1,
44 .raid_name = "raid10",
45 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
46 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
48 [BTRFS_RAID_RAID1] = {
53 .tolerated_failures = 1,
58 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
59 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
61 [BTRFS_RAID_RAID1C3] = {
66 .tolerated_failures = 2,
69 .raid_name = "raid1c3",
70 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3,
71 .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET,
73 [BTRFS_RAID_RAID1C4] = {
78 .tolerated_failures = 3,
81 .raid_name = "raid1c4",
82 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4,
83 .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET,
90 .tolerated_failures = 0,
95 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
98 [BTRFS_RAID_RAID0] = {
103 .tolerated_failures = 0,
107 .raid_name = "raid0",
108 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
111 [BTRFS_RAID_SINGLE] = {
116 .tolerated_failures = 0,
120 .raid_name = "single",
124 [BTRFS_RAID_RAID5] = {
129 .tolerated_failures = 1,
133 .raid_name = "raid5",
134 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
135 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
137 [BTRFS_RAID_RAID6] = {
142 .tolerated_failures = 2,
146 .raid_name = "raid6",
147 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
148 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
152 const char *btrfs_bg_type_to_raid_name(u64 flags)
154 const int index = btrfs_bg_flags_to_raid_index(flags);
156 if (index >= BTRFS_NR_RAID_TYPES)
159 return btrfs_raid_array[index].raid_name;
163 * Fill @buf with textual description of @bg_flags, no more than @size_buf
164 * bytes including terminating null byte.
166 void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
171 u64 flags = bg_flags;
172 u32 size_bp = size_buf;
179 #define DESCRIBE_FLAG(flag, desc) \
181 if (flags & (flag)) { \
182 ret = snprintf(bp, size_bp, "%s|", (desc)); \
183 if (ret < 0 || ret >= size_bp) \
191 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
192 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
193 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
195 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
196 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
197 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
198 btrfs_raid_array[i].raid_name);
202 ret = snprintf(bp, size_bp, "0x%llx|", flags);
206 if (size_bp < size_buf)
207 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
210 * The text is trimmed, it's up to the caller to provide sufficiently
216 static int init_first_rw_device(struct btrfs_trans_handle *trans);
217 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
218 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
219 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
220 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
221 enum btrfs_map_op op,
222 u64 logical, u64 *length,
223 struct btrfs_bio **bbio_ret,
224 int mirror_num, int need_raid_map);
230 * There are several mutexes that protect manipulation of devices and low-level
231 * structures like chunks but not block groups, extents or files
233 * uuid_mutex (global lock)
234 * ------------------------
235 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
236 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
237 * device) or requested by the device= mount option
239 * the mutex can be very coarse and can cover long-running operations
241 * protects: updates to fs_devices counters like missing devices, rw devices,
242 * seeding, structure cloning, opening/closing devices at mount/umount time
244 * global::fs_devs - add, remove, updates to the global list
246 * does not protect: manipulation of the fs_devices::devices list!
248 * btrfs_device::name - renames (write side), read is RCU
250 * fs_devices::device_list_mutex (per-fs, with RCU)
251 * ------------------------------------------------
252 * protects updates to fs_devices::devices, ie. adding and deleting
254 * simple list traversal with read-only actions can be done with RCU protection
256 * may be used to exclude some operations from running concurrently without any
257 * modifications to the list (see write_all_supers)
261 * protects balance structures (status, state) and context accessed from
262 * several places (internally, ioctl)
266 * protects chunks, adding or removing during allocation, trim or when a new
267 * device is added/removed. Additionally it also protects post_commit_list of
268 * individual devices, since they can be added to the transaction's
269 * post_commit_list only with chunk_mutex held.
273 * a big lock that is held by the cleaner thread and prevents running subvolume
274 * cleaning together with relocation or delayed iputs
287 * Exclusive operations, BTRFS_FS_EXCL_OP
288 * ======================================
290 * Maintains the exclusivity of the following operations that apply to the
291 * whole filesystem and cannot run in parallel.
296 * - Device replace (*)
299 * The device operations (as above) can be in one of the following states:
305 * Only device operations marked with (*) can go into the Paused state for the
308 * - ioctl (only Balance can be Paused through ioctl)
309 * - filesystem remounted as read-only
310 * - filesystem unmounted and mounted as read-only
311 * - system power-cycle and filesystem mounted as read-only
312 * - filesystem or device errors leading to forced read-only
314 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
315 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
316 * A device operation in Paused or Running state can be canceled or resumed
317 * either by ioctl (Balance only) or when remounted as read-write.
318 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
322 DEFINE_MUTEX(uuid_mutex);
323 static LIST_HEAD(fs_uuids);
324 struct list_head * __attribute_const__ btrfs_get_fs_uuids(void)
330 * alloc_fs_devices - allocate struct btrfs_fs_devices
331 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
332 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
334 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
335 * The returned struct is not linked onto any lists and can be destroyed with
336 * kfree() right away.
338 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
339 const u8 *metadata_fsid)
341 struct btrfs_fs_devices *fs_devs;
343 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
345 return ERR_PTR(-ENOMEM);
347 mutex_init(&fs_devs->device_list_mutex);
349 INIT_LIST_HEAD(&fs_devs->devices);
350 INIT_LIST_HEAD(&fs_devs->alloc_list);
351 INIT_LIST_HEAD(&fs_devs->fs_list);
353 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
356 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
358 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
363 void btrfs_free_device(struct btrfs_device *device)
365 WARN_ON(!list_empty(&device->post_commit_list));
366 rcu_string_free(device->name);
367 extent_io_tree_release(&device->alloc_state);
368 bio_put(device->flush_bio);
372 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
374 struct btrfs_device *device;
375 WARN_ON(fs_devices->opened);
376 while (!list_empty(&fs_devices->devices)) {
377 device = list_entry(fs_devices->devices.next,
378 struct btrfs_device, dev_list);
379 list_del(&device->dev_list);
380 btrfs_free_device(device);
385 void __exit btrfs_cleanup_fs_uuids(void)
387 struct btrfs_fs_devices *fs_devices;
389 while (!list_empty(&fs_uuids)) {
390 fs_devices = list_entry(fs_uuids.next,
391 struct btrfs_fs_devices, fs_list);
392 list_del(&fs_devices->fs_list);
393 free_fs_devices(fs_devices);
398 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
399 * Returned struct is not linked onto any lists and must be destroyed using
402 static struct btrfs_device *__alloc_device(void)
404 struct btrfs_device *dev;
406 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
408 return ERR_PTR(-ENOMEM);
411 * Preallocate a bio that's always going to be used for flushing device
412 * barriers and matches the device lifespan
414 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
415 if (!dev->flush_bio) {
417 return ERR_PTR(-ENOMEM);
420 INIT_LIST_HEAD(&dev->dev_list);
421 INIT_LIST_HEAD(&dev->dev_alloc_list);
422 INIT_LIST_HEAD(&dev->post_commit_list);
424 atomic_set(&dev->reada_in_flight, 0);
425 atomic_set(&dev->dev_stats_ccnt, 0);
426 btrfs_device_data_ordered_init(dev);
427 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
428 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
429 extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL);
434 static noinline struct btrfs_fs_devices *find_fsid(
435 const u8 *fsid, const u8 *metadata_fsid)
437 struct btrfs_fs_devices *fs_devices;
443 * Handle scanned device having completed its fsid change but
444 * belonging to a fs_devices that was created by first scanning
445 * a device which didn't have its fsid/metadata_uuid changed
446 * at all and the CHANGING_FSID_V2 flag set.
448 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
449 if (fs_devices->fsid_change &&
450 memcmp(metadata_fsid, fs_devices->fsid,
451 BTRFS_FSID_SIZE) == 0 &&
452 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
453 BTRFS_FSID_SIZE) == 0) {
458 * Handle scanned device having completed its fsid change but
459 * belonging to a fs_devices that was created by a device that
460 * has an outdated pair of fsid/metadata_uuid and
461 * CHANGING_FSID_V2 flag set.
463 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
464 if (fs_devices->fsid_change &&
465 memcmp(fs_devices->metadata_uuid,
466 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
467 memcmp(metadata_fsid, fs_devices->metadata_uuid,
468 BTRFS_FSID_SIZE) == 0) {
474 /* Handle non-split brain cases */
475 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
477 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
478 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
479 BTRFS_FSID_SIZE) == 0)
482 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
490 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
491 int flush, struct block_device **bdev,
492 struct buffer_head **bh)
496 *bdev = blkdev_get_by_path(device_path, flags, holder);
499 ret = PTR_ERR(*bdev);
504 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
505 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
507 blkdev_put(*bdev, flags);
510 invalidate_bdev(*bdev);
511 *bh = btrfs_read_dev_super(*bdev);
514 blkdev_put(*bdev, flags);
526 static bool device_path_matched(const char *path, struct btrfs_device *device)
531 found = strcmp(rcu_str_deref(device->name), path);
538 * Search and remove all stale (devices which are not mounted) devices.
539 * When both inputs are NULL, it will search and release all stale devices.
540 * path: Optional. When provided will it release all unmounted devices
541 * matching this path only.
542 * skip_dev: Optional. Will skip this device when searching for the stale
544 * Return: 0 for success or if @path is NULL.
545 * -EBUSY if @path is a mounted device.
546 * -ENOENT if @path does not match any device in the list.
548 static int btrfs_free_stale_devices(const char *path,
549 struct btrfs_device *skip_device)
551 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
552 struct btrfs_device *device, *tmp_device;
558 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
560 mutex_lock(&fs_devices->device_list_mutex);
561 list_for_each_entry_safe(device, tmp_device,
562 &fs_devices->devices, dev_list) {
563 if (skip_device && skip_device == device)
565 if (path && !device->name)
567 if (path && !device_path_matched(path, device))
569 if (fs_devices->opened) {
570 /* for an already deleted device return 0 */
571 if (path && ret != 0)
576 /* delete the stale device */
577 fs_devices->num_devices--;
578 list_del(&device->dev_list);
579 btrfs_free_device(device);
582 if (fs_devices->num_devices == 0)
585 mutex_unlock(&fs_devices->device_list_mutex);
587 if (fs_devices->num_devices == 0) {
588 btrfs_sysfs_remove_fsid(fs_devices);
589 list_del(&fs_devices->fs_list);
590 free_fs_devices(fs_devices);
597 static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
598 struct btrfs_device *device, fmode_t flags,
601 struct request_queue *q;
602 struct block_device *bdev;
603 struct buffer_head *bh;
604 struct btrfs_super_block *disk_super;
613 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
618 disk_super = (struct btrfs_super_block *)bh->b_data;
619 devid = btrfs_stack_device_id(&disk_super->dev_item);
620 if (devid != device->devid)
623 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
626 device->generation = btrfs_super_generation(disk_super);
628 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
629 if (btrfs_super_incompat_flags(disk_super) &
630 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
632 "BTRFS: Invalid seeding and uuid-changed device detected\n");
636 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
637 fs_devices->seeding = true;
639 if (bdev_read_only(bdev))
640 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
642 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
645 q = bdev_get_queue(bdev);
646 if (!blk_queue_nonrot(q))
647 fs_devices->rotating = true;
650 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
651 device->mode = flags;
653 fs_devices->open_devices++;
654 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
655 device->devid != BTRFS_DEV_REPLACE_DEVID) {
656 fs_devices->rw_devices++;
657 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
665 blkdev_put(bdev, flags);
671 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
672 * being created with a disk that has already completed its fsid change.
674 static struct btrfs_fs_devices *find_fsid_inprogress(
675 struct btrfs_super_block *disk_super)
677 struct btrfs_fs_devices *fs_devices;
679 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
680 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
681 BTRFS_FSID_SIZE) != 0 &&
682 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
683 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
692 static struct btrfs_fs_devices *find_fsid_changed(
693 struct btrfs_super_block *disk_super)
695 struct btrfs_fs_devices *fs_devices;
698 * Handles the case where scanned device is part of an fs that had
699 * multiple successful changes of FSID but curently device didn't
700 * observe it. Meaning our fsid will be different than theirs.
702 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
703 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
704 BTRFS_FSID_SIZE) != 0 &&
705 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
706 BTRFS_FSID_SIZE) == 0 &&
707 memcmp(fs_devices->fsid, disk_super->fsid,
708 BTRFS_FSID_SIZE) != 0) {
716 * Add new device to list of registered devices
719 * device pointer which was just added or updated when successful
720 * error pointer when failed
722 static noinline struct btrfs_device *device_list_add(const char *path,
723 struct btrfs_super_block *disk_super,
724 bool *new_device_added)
726 struct btrfs_device *device;
727 struct btrfs_fs_devices *fs_devices = NULL;
728 struct rcu_string *name;
729 u64 found_transid = btrfs_super_generation(disk_super);
730 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
731 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
732 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
733 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
734 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
736 if (fsid_change_in_progress) {
737 if (!has_metadata_uuid) {
739 * When we have an image which has CHANGING_FSID_V2 set
740 * it might belong to either a filesystem which has
741 * disks with completed fsid change or it might belong
742 * to fs with no UUID changes in effect, handle both.
744 fs_devices = find_fsid_inprogress(disk_super);
746 fs_devices = find_fsid(disk_super->fsid, NULL);
748 fs_devices = find_fsid_changed(disk_super);
750 } else if (has_metadata_uuid) {
751 fs_devices = find_fsid(disk_super->fsid,
752 disk_super->metadata_uuid);
754 fs_devices = find_fsid(disk_super->fsid, NULL);
759 if (has_metadata_uuid)
760 fs_devices = alloc_fs_devices(disk_super->fsid,
761 disk_super->metadata_uuid);
763 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
765 if (IS_ERR(fs_devices))
766 return ERR_CAST(fs_devices);
768 fs_devices->fsid_change = fsid_change_in_progress;
770 mutex_lock(&fs_devices->device_list_mutex);
771 list_add(&fs_devices->fs_list, &fs_uuids);
775 mutex_lock(&fs_devices->device_list_mutex);
776 device = btrfs_find_device(fs_devices, devid,
777 disk_super->dev_item.uuid, NULL, false);
780 * If this disk has been pulled into an fs devices created by
781 * a device which had the CHANGING_FSID_V2 flag then replace the
782 * metadata_uuid/fsid values of the fs_devices.
784 if (has_metadata_uuid && fs_devices->fsid_change &&
785 found_transid > fs_devices->latest_generation) {
786 memcpy(fs_devices->fsid, disk_super->fsid,
788 memcpy(fs_devices->metadata_uuid,
789 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
791 fs_devices->fsid_change = false;
796 if (fs_devices->opened) {
797 mutex_unlock(&fs_devices->device_list_mutex);
798 return ERR_PTR(-EBUSY);
801 device = btrfs_alloc_device(NULL, &devid,
802 disk_super->dev_item.uuid);
803 if (IS_ERR(device)) {
804 mutex_unlock(&fs_devices->device_list_mutex);
805 /* we can safely leave the fs_devices entry around */
809 name = rcu_string_strdup(path, GFP_NOFS);
811 btrfs_free_device(device);
812 mutex_unlock(&fs_devices->device_list_mutex);
813 return ERR_PTR(-ENOMEM);
815 rcu_assign_pointer(device->name, name);
817 list_add_rcu(&device->dev_list, &fs_devices->devices);
818 fs_devices->num_devices++;
820 device->fs_devices = fs_devices;
821 *new_device_added = true;
823 if (disk_super->label[0])
825 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
826 disk_super->label, devid, found_transid, path,
827 current->comm, task_pid_nr(current));
830 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
831 disk_super->fsid, devid, found_transid, path,
832 current->comm, task_pid_nr(current));
834 } else if (!device->name || strcmp(device->name->str, path)) {
836 * When FS is already mounted.
837 * 1. If you are here and if the device->name is NULL that
838 * means this device was missing at time of FS mount.
839 * 2. If you are here and if the device->name is different
840 * from 'path' that means either
841 * a. The same device disappeared and reappeared with
843 * b. The missing-disk-which-was-replaced, has
846 * We must allow 1 and 2a above. But 2b would be a spurious
849 * Further in case of 1 and 2a above, the disk at 'path'
850 * would have missed some transaction when it was away and
851 * in case of 2a the stale bdev has to be updated as well.
852 * 2b must not be allowed at all time.
856 * For now, we do allow update to btrfs_fs_device through the
857 * btrfs dev scan cli after FS has been mounted. We're still
858 * tracking a problem where systems fail mount by subvolume id
859 * when we reject replacement on a mounted FS.
861 if (!fs_devices->opened && found_transid < device->generation) {
863 * That is if the FS is _not_ mounted and if you
864 * are here, that means there is more than one
865 * disk with same uuid and devid.We keep the one
866 * with larger generation number or the last-in if
867 * generation are equal.
869 mutex_unlock(&fs_devices->device_list_mutex);
870 return ERR_PTR(-EEXIST);
874 * We are going to replace the device path for a given devid,
875 * make sure it's the same device if the device is mounted
878 struct block_device *path_bdev;
880 path_bdev = lookup_bdev(path);
881 if (IS_ERR(path_bdev)) {
882 mutex_unlock(&fs_devices->device_list_mutex);
883 return ERR_CAST(path_bdev);
886 if (device->bdev != path_bdev) {
888 mutex_unlock(&fs_devices->device_list_mutex);
889 btrfs_warn_in_rcu(device->fs_info,
890 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
891 disk_super->fsid, devid,
892 rcu_str_deref(device->name), path);
893 return ERR_PTR(-EEXIST);
896 btrfs_info_in_rcu(device->fs_info,
897 "device fsid %pU devid %llu moved old:%s new:%s",
898 disk_super->fsid, devid,
899 rcu_str_deref(device->name), path);
902 name = rcu_string_strdup(path, GFP_NOFS);
904 mutex_unlock(&fs_devices->device_list_mutex);
905 return ERR_PTR(-ENOMEM);
907 rcu_string_free(device->name);
908 rcu_assign_pointer(device->name, name);
909 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
910 fs_devices->missing_devices--;
911 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
916 * Unmount does not free the btrfs_device struct but would zero
917 * generation along with most of the other members. So just update
918 * it back. We need it to pick the disk with largest generation
921 if (!fs_devices->opened) {
922 device->generation = found_transid;
923 fs_devices->latest_generation = max_t(u64, found_transid,
924 fs_devices->latest_generation);
927 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
929 mutex_unlock(&fs_devices->device_list_mutex);
933 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
935 struct btrfs_fs_devices *fs_devices;
936 struct btrfs_device *device;
937 struct btrfs_device *orig_dev;
940 fs_devices = alloc_fs_devices(orig->fsid, NULL);
941 if (IS_ERR(fs_devices))
944 mutex_lock(&orig->device_list_mutex);
945 fs_devices->total_devices = orig->total_devices;
947 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
948 struct rcu_string *name;
950 device = btrfs_alloc_device(NULL, &orig_dev->devid,
952 if (IS_ERR(device)) {
953 ret = PTR_ERR(device);
958 * This is ok to do without rcu read locked because we hold the
959 * uuid mutex so nothing we touch in here is going to disappear.
961 if (orig_dev->name) {
962 name = rcu_string_strdup(orig_dev->name->str,
965 btrfs_free_device(device);
969 rcu_assign_pointer(device->name, name);
972 list_add(&device->dev_list, &fs_devices->devices);
973 device->fs_devices = fs_devices;
974 fs_devices->num_devices++;
976 mutex_unlock(&orig->device_list_mutex);
979 mutex_unlock(&orig->device_list_mutex);
980 free_fs_devices(fs_devices);
985 * After we have read the system tree and know devids belonging to
986 * this filesystem, remove the device which does not belong there.
988 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
990 struct btrfs_device *device, *next;
991 struct btrfs_device *latest_dev = NULL;
993 mutex_lock(&uuid_mutex);
995 /* This is the initialized path, it is safe to release the devices. */
996 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
997 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
998 &device->dev_state)) {
999 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1000 &device->dev_state) &&
1002 device->generation > latest_dev->generation)) {
1003 latest_dev = device;
1008 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1010 * In the first step, keep the device which has
1011 * the correct fsid and the devid that is used
1012 * for the dev_replace procedure.
1013 * In the second step, the dev_replace state is
1014 * read from the device tree and it is known
1015 * whether the procedure is really active or
1016 * not, which means whether this device is
1017 * used or whether it should be removed.
1019 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1020 &device->dev_state)) {
1025 blkdev_put(device->bdev, device->mode);
1026 device->bdev = NULL;
1027 fs_devices->open_devices--;
1029 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1030 list_del_init(&device->dev_alloc_list);
1031 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1032 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1033 &device->dev_state))
1034 fs_devices->rw_devices--;
1036 list_del_init(&device->dev_list);
1037 fs_devices->num_devices--;
1038 btrfs_free_device(device);
1041 if (fs_devices->seed) {
1042 fs_devices = fs_devices->seed;
1046 fs_devices->latest_bdev = latest_dev->bdev;
1048 mutex_unlock(&uuid_mutex);
1051 static void btrfs_close_bdev(struct btrfs_device *device)
1056 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1057 sync_blockdev(device->bdev);
1058 invalidate_bdev(device->bdev);
1061 blkdev_put(device->bdev, device->mode);
1064 static void btrfs_close_one_device(struct btrfs_device *device)
1066 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1067 struct btrfs_device *new_device;
1068 struct rcu_string *name;
1071 fs_devices->open_devices--;
1073 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1074 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1075 list_del_init(&device->dev_alloc_list);
1076 fs_devices->rw_devices--;
1079 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
1080 fs_devices->missing_devices--;
1082 btrfs_close_bdev(device);
1084 new_device = btrfs_alloc_device(NULL, &device->devid,
1086 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1088 /* Safe because we are under uuid_mutex */
1090 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1091 BUG_ON(!name); /* -ENOMEM */
1092 rcu_assign_pointer(new_device->name, name);
1095 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1096 new_device->fs_devices = device->fs_devices;
1099 btrfs_free_device(device);
1102 static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
1104 struct btrfs_device *device, *tmp;
1106 if (--fs_devices->opened > 0)
1109 mutex_lock(&fs_devices->device_list_mutex);
1110 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
1111 btrfs_close_one_device(device);
1113 mutex_unlock(&fs_devices->device_list_mutex);
1115 WARN_ON(fs_devices->open_devices);
1116 WARN_ON(fs_devices->rw_devices);
1117 fs_devices->opened = 0;
1118 fs_devices->seeding = false;
1123 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1125 struct btrfs_fs_devices *seed_devices = NULL;
1128 mutex_lock(&uuid_mutex);
1129 ret = close_fs_devices(fs_devices);
1130 if (!fs_devices->opened) {
1131 seed_devices = fs_devices->seed;
1132 fs_devices->seed = NULL;
1134 mutex_unlock(&uuid_mutex);
1136 while (seed_devices) {
1137 fs_devices = seed_devices;
1138 seed_devices = fs_devices->seed;
1139 close_fs_devices(fs_devices);
1140 free_fs_devices(fs_devices);
1145 static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
1146 fmode_t flags, void *holder)
1148 struct btrfs_device *device;
1149 struct btrfs_device *latest_dev = NULL;
1152 flags |= FMODE_EXCL;
1154 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1155 /* Just open everything we can; ignore failures here */
1156 if (btrfs_open_one_device(fs_devices, device, flags, holder))
1160 device->generation > latest_dev->generation)
1161 latest_dev = device;
1163 if (fs_devices->open_devices == 0) {
1167 fs_devices->opened = 1;
1168 fs_devices->latest_bdev = latest_dev->bdev;
1169 fs_devices->total_rw_bytes = 0;
1174 static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1176 struct btrfs_device *dev1, *dev2;
1178 dev1 = list_entry(a, struct btrfs_device, dev_list);
1179 dev2 = list_entry(b, struct btrfs_device, dev_list);
1181 if (dev1->devid < dev2->devid)
1183 else if (dev1->devid > dev2->devid)
1188 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1189 fmode_t flags, void *holder)
1193 lockdep_assert_held(&uuid_mutex);
1195 mutex_lock(&fs_devices->device_list_mutex);
1196 if (fs_devices->opened) {
1197 fs_devices->opened++;
1200 list_sort(NULL, &fs_devices->devices, devid_cmp);
1201 ret = open_fs_devices(fs_devices, flags, holder);
1203 mutex_unlock(&fs_devices->device_list_mutex);
1208 static void btrfs_release_disk_super(struct page *page)
1214 static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1216 struct btrfs_super_block **disk_super)
1221 /* make sure our super fits in the device */
1222 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1225 /* make sure our super fits in the page */
1226 if (sizeof(**disk_super) > PAGE_SIZE)
1229 /* make sure our super doesn't straddle pages on disk */
1230 index = bytenr >> PAGE_SHIFT;
1231 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1234 /* pull in the page with our super */
1235 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1238 if (IS_ERR_OR_NULL(*page))
1243 /* align our pointer to the offset of the super block */
1244 *disk_super = p + offset_in_page(bytenr);
1246 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1247 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1248 btrfs_release_disk_super(*page);
1252 if ((*disk_super)->label[0] &&
1253 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1254 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1259 int btrfs_forget_devices(const char *path)
1263 mutex_lock(&uuid_mutex);
1264 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1265 mutex_unlock(&uuid_mutex);
1271 * Look for a btrfs signature on a device. This may be called out of the mount path
1272 * and we are not allowed to call set_blocksize during the scan. The superblock
1273 * is read via pagecache
1275 struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1278 struct btrfs_super_block *disk_super;
1279 bool new_device_added = false;
1280 struct btrfs_device *device = NULL;
1281 struct block_device *bdev;
1285 lockdep_assert_held(&uuid_mutex);
1288 * we would like to check all the supers, but that would make
1289 * a btrfs mount succeed after a mkfs from a different FS.
1290 * So, we need to add a special mount option to scan for
1291 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1293 bytenr = btrfs_sb_offset(0);
1294 flags |= FMODE_EXCL;
1296 bdev = blkdev_get_by_path(path, flags, holder);
1298 return ERR_CAST(bdev);
1300 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1301 device = ERR_PTR(-EINVAL);
1302 goto error_bdev_put;
1305 device = device_list_add(path, disk_super, &new_device_added);
1306 if (!IS_ERR(device)) {
1307 if (new_device_added)
1308 btrfs_free_stale_devices(path, device);
1311 btrfs_release_disk_super(page);
1314 blkdev_put(bdev, flags);
1320 * Try to find a chunk that intersects [start, start + len] range and when one
1321 * such is found, record the end of it in *start
1323 static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1326 u64 physical_start, physical_end;
1328 lockdep_assert_held(&device->fs_info->chunk_mutex);
1330 if (!find_first_extent_bit(&device->alloc_state, *start,
1331 &physical_start, &physical_end,
1332 CHUNK_ALLOCATED, NULL)) {
1334 if (in_range(physical_start, *start, len) ||
1335 in_range(*start, physical_start,
1336 physical_end - physical_start)) {
1337 *start = physical_end + 1;
1346 * find_free_dev_extent_start - find free space in the specified device
1347 * @device: the device which we search the free space in
1348 * @num_bytes: the size of the free space that we need
1349 * @search_start: the position from which to begin the search
1350 * @start: store the start of the free space.
1351 * @len: the size of the free space. that we find, or the size
1352 * of the max free space if we don't find suitable free space
1354 * this uses a pretty simple search, the expectation is that it is
1355 * called very infrequently and that a given device has a small number
1358 * @start is used to store the start of the free space if we find. But if we
1359 * don't find suitable free space, it will be used to store the start position
1360 * of the max free space.
1362 * @len is used to store the size of the free space that we find.
1363 * But if we don't find suitable free space, it is used to store the size of
1364 * the max free space.
1366 * NOTE: This function will search *commit* root of device tree, and does extra
1367 * check to ensure dev extents are not double allocated.
1368 * This makes the function safe to allocate dev extents but may not report
1369 * correct usable device space, as device extent freed in current transaction
1370 * is not reported as avaiable.
1372 static int find_free_dev_extent_start(struct btrfs_device *device,
1373 u64 num_bytes, u64 search_start, u64 *start,
1376 struct btrfs_fs_info *fs_info = device->fs_info;
1377 struct btrfs_root *root = fs_info->dev_root;
1378 struct btrfs_key key;
1379 struct btrfs_dev_extent *dev_extent;
1380 struct btrfs_path *path;
1385 u64 search_end = device->total_bytes;
1388 struct extent_buffer *l;
1391 * We don't want to overwrite the superblock on the drive nor any area
1392 * used by the boot loader (grub for example), so we make sure to start
1393 * at an offset of at least 1MB.
1395 search_start = max_t(u64, search_start, SZ_1M);
1397 path = btrfs_alloc_path();
1401 max_hole_start = search_start;
1405 if (search_start >= search_end ||
1406 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1411 path->reada = READA_FORWARD;
1412 path->search_commit_root = 1;
1413 path->skip_locking = 1;
1415 key.objectid = device->devid;
1416 key.offset = search_start;
1417 key.type = BTRFS_DEV_EXTENT_KEY;
1419 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1423 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1430 slot = path->slots[0];
1431 if (slot >= btrfs_header_nritems(l)) {
1432 ret = btrfs_next_leaf(root, path);
1440 btrfs_item_key_to_cpu(l, &key, slot);
1442 if (key.objectid < device->devid)
1445 if (key.objectid > device->devid)
1448 if (key.type != BTRFS_DEV_EXTENT_KEY)
1451 if (key.offset > search_start) {
1452 hole_size = key.offset - search_start;
1455 * Have to check before we set max_hole_start, otherwise
1456 * we could end up sending back this offset anyway.
1458 if (contains_pending_extent(device, &search_start,
1460 if (key.offset >= search_start)
1461 hole_size = key.offset - search_start;
1466 if (hole_size > max_hole_size) {
1467 max_hole_start = search_start;
1468 max_hole_size = hole_size;
1472 * If this free space is greater than which we need,
1473 * it must be the max free space that we have found
1474 * until now, so max_hole_start must point to the start
1475 * of this free space and the length of this free space
1476 * is stored in max_hole_size. Thus, we return
1477 * max_hole_start and max_hole_size and go back to the
1480 if (hole_size >= num_bytes) {
1486 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1487 extent_end = key.offset + btrfs_dev_extent_length(l,
1489 if (extent_end > search_start)
1490 search_start = extent_end;
1497 * At this point, search_start should be the end of
1498 * allocated dev extents, and when shrinking the device,
1499 * search_end may be smaller than search_start.
1501 if (search_end > search_start) {
1502 hole_size = search_end - search_start;
1504 if (contains_pending_extent(device, &search_start, hole_size)) {
1505 btrfs_release_path(path);
1509 if (hole_size > max_hole_size) {
1510 max_hole_start = search_start;
1511 max_hole_size = hole_size;
1516 if (max_hole_size < num_bytes)
1522 btrfs_free_path(path);
1523 *start = max_hole_start;
1525 *len = max_hole_size;
1529 int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
1530 u64 *start, u64 *len)
1532 /* FIXME use last free of some kind */
1533 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
1536 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1537 struct btrfs_device *device,
1538 u64 start, u64 *dev_extent_len)
1540 struct btrfs_fs_info *fs_info = device->fs_info;
1541 struct btrfs_root *root = fs_info->dev_root;
1543 struct btrfs_path *path;
1544 struct btrfs_key key;
1545 struct btrfs_key found_key;
1546 struct extent_buffer *leaf = NULL;
1547 struct btrfs_dev_extent *extent = NULL;
1549 path = btrfs_alloc_path();
1553 key.objectid = device->devid;
1555 key.type = BTRFS_DEV_EXTENT_KEY;
1557 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1559 ret = btrfs_previous_item(root, path, key.objectid,
1560 BTRFS_DEV_EXTENT_KEY);
1563 leaf = path->nodes[0];
1564 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1565 extent = btrfs_item_ptr(leaf, path->slots[0],
1566 struct btrfs_dev_extent);
1567 BUG_ON(found_key.offset > start || found_key.offset +
1568 btrfs_dev_extent_length(leaf, extent) < start);
1570 btrfs_release_path(path);
1572 } else if (ret == 0) {
1573 leaf = path->nodes[0];
1574 extent = btrfs_item_ptr(leaf, path->slots[0],
1575 struct btrfs_dev_extent);
1577 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
1581 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1583 ret = btrfs_del_item(trans, root, path);
1585 btrfs_handle_fs_error(fs_info, ret,
1586 "Failed to remove dev extent item");
1588 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
1591 btrfs_free_path(path);
1595 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1596 struct btrfs_device *device,
1597 u64 chunk_offset, u64 start, u64 num_bytes)
1600 struct btrfs_path *path;
1601 struct btrfs_fs_info *fs_info = device->fs_info;
1602 struct btrfs_root *root = fs_info->dev_root;
1603 struct btrfs_dev_extent *extent;
1604 struct extent_buffer *leaf;
1605 struct btrfs_key key;
1607 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
1608 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1609 path = btrfs_alloc_path();
1613 key.objectid = device->devid;
1615 key.type = BTRFS_DEV_EXTENT_KEY;
1616 ret = btrfs_insert_empty_item(trans, root, path, &key,
1621 leaf = path->nodes[0];
1622 extent = btrfs_item_ptr(leaf, path->slots[0],
1623 struct btrfs_dev_extent);
1624 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1625 BTRFS_CHUNK_TREE_OBJECTID);
1626 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1627 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1628 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1630 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1631 btrfs_mark_buffer_dirty(leaf);
1633 btrfs_free_path(path);
1637 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1639 struct extent_map_tree *em_tree;
1640 struct extent_map *em;
1644 em_tree = &fs_info->mapping_tree;
1645 read_lock(&em_tree->lock);
1646 n = rb_last(&em_tree->map.rb_root);
1648 em = rb_entry(n, struct extent_map, rb_node);
1649 ret = em->start + em->len;
1651 read_unlock(&em_tree->lock);
1656 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1660 struct btrfs_key key;
1661 struct btrfs_key found_key;
1662 struct btrfs_path *path;
1664 path = btrfs_alloc_path();
1668 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1669 key.type = BTRFS_DEV_ITEM_KEY;
1670 key.offset = (u64)-1;
1672 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1678 btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");
1683 ret = btrfs_previous_item(fs_info->chunk_root, path,
1684 BTRFS_DEV_ITEMS_OBJECTID,
1685 BTRFS_DEV_ITEM_KEY);
1689 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1691 *devid_ret = found_key.offset + 1;
1695 btrfs_free_path(path);
1700 * the device information is stored in the chunk root
1701 * the btrfs_device struct should be fully filled in
1703 static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
1704 struct btrfs_device *device)
1707 struct btrfs_path *path;
1708 struct btrfs_dev_item *dev_item;
1709 struct extent_buffer *leaf;
1710 struct btrfs_key key;
1713 path = btrfs_alloc_path();
1717 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1718 key.type = BTRFS_DEV_ITEM_KEY;
1719 key.offset = device->devid;
1721 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1722 &key, sizeof(*dev_item));
1726 leaf = path->nodes[0];
1727 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1729 btrfs_set_device_id(leaf, dev_item, device->devid);
1730 btrfs_set_device_generation(leaf, dev_item, 0);
1731 btrfs_set_device_type(leaf, dev_item, device->type);
1732 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1733 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1734 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1735 btrfs_set_device_total_bytes(leaf, dev_item,
1736 btrfs_device_get_disk_total_bytes(device));
1737 btrfs_set_device_bytes_used(leaf, dev_item,
1738 btrfs_device_get_bytes_used(device));
1739 btrfs_set_device_group(leaf, dev_item, 0);
1740 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1741 btrfs_set_device_bandwidth(leaf, dev_item, 0);
1742 btrfs_set_device_start_offset(leaf, dev_item, 0);
1744 ptr = btrfs_device_uuid(dev_item);
1745 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1746 ptr = btrfs_device_fsid(dev_item);
1747 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1748 ptr, BTRFS_FSID_SIZE);
1749 btrfs_mark_buffer_dirty(leaf);
1753 btrfs_free_path(path);
1758 * Function to update ctime/mtime for a given device path.
1759 * Mainly used for ctime/mtime based probe like libblkid.
1761 static void update_dev_time(const char *path_name)
1765 filp = filp_open(path_name, O_RDWR, 0);
1768 file_update_time(filp);
1769 filp_close(filp, NULL);
1772 static int btrfs_rm_dev_item(struct btrfs_device *device)
1774 struct btrfs_root *root = device->fs_info->chunk_root;
1776 struct btrfs_path *path;
1777 struct btrfs_key key;
1778 struct btrfs_trans_handle *trans;
1780 path = btrfs_alloc_path();
1784 trans = btrfs_start_transaction(root, 0);
1785 if (IS_ERR(trans)) {
1786 btrfs_free_path(path);
1787 return PTR_ERR(trans);
1789 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1790 key.type = BTRFS_DEV_ITEM_KEY;
1791 key.offset = device->devid;
1793 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1797 btrfs_abort_transaction(trans, ret);
1798 btrfs_end_transaction(trans);
1802 ret = btrfs_del_item(trans, root, path);
1804 btrfs_abort_transaction(trans, ret);
1805 btrfs_end_transaction(trans);
1809 btrfs_free_path(path);
1811 ret = btrfs_commit_transaction(trans);
1816 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1817 * filesystem. It's up to the caller to adjust that number regarding eg. device
1820 static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1828 seq = read_seqbegin(&fs_info->profiles_lock);
1830 all_avail = fs_info->avail_data_alloc_bits |
1831 fs_info->avail_system_alloc_bits |
1832 fs_info->avail_metadata_alloc_bits;
1833 } while (read_seqretry(&fs_info->profiles_lock, seq));
1835 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1836 if (!(all_avail & btrfs_raid_array[i].bg_flag))
1839 if (num_devices < btrfs_raid_array[i].devs_min) {
1840 int ret = btrfs_raid_array[i].mindev_error;
1850 static struct btrfs_device * btrfs_find_next_active_device(
1851 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
1853 struct btrfs_device *next_device;
1855 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1856 if (next_device != device &&
1857 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1858 && next_device->bdev)
1866 * Helper function to check if the given device is part of s_bdev / latest_bdev
1867 * and replace it with the provided or the next active device, in the context
1868 * where this function called, there should be always be another device (or
1869 * this_dev) which is active.
1871 void __cold btrfs_assign_next_active_device(struct btrfs_device *device,
1872 struct btrfs_device *this_dev)
1874 struct btrfs_fs_info *fs_info = device->fs_info;
1875 struct btrfs_device *next_device;
1878 next_device = this_dev;
1880 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1882 ASSERT(next_device);
1884 if (fs_info->sb->s_bdev &&
1885 (fs_info->sb->s_bdev == device->bdev))
1886 fs_info->sb->s_bdev = next_device->bdev;
1888 if (fs_info->fs_devices->latest_bdev == device->bdev)
1889 fs_info->fs_devices->latest_bdev = next_device->bdev;
1893 * Return btrfs_fs_devices::num_devices excluding the device that's being
1894 * currently replaced.
1896 static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1898 u64 num_devices = fs_info->fs_devices->num_devices;
1900 down_read(&fs_info->dev_replace.rwsem);
1901 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1902 ASSERT(num_devices > 1);
1905 up_read(&fs_info->dev_replace.rwsem);
1910 int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1913 struct btrfs_device *device;
1914 struct btrfs_fs_devices *cur_devices;
1915 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1919 mutex_lock(&uuid_mutex);
1921 num_devices = btrfs_num_devices(fs_info);
1923 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
1927 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
1929 if (IS_ERR(device)) {
1930 if (PTR_ERR(device) == -ENOENT &&
1931 strcmp(device_path, "missing") == 0)
1932 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1934 ret = PTR_ERR(device);
1938 if (btrfs_pinned_by_swapfile(fs_info, device)) {
1939 btrfs_warn_in_rcu(fs_info,
1940 "cannot remove device %s (devid %llu) due to active swapfile",
1941 rcu_str_deref(device->name), device->devid);
1946 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1947 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1951 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1952 fs_info->fs_devices->rw_devices == 1) {
1953 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1957 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1958 mutex_lock(&fs_info->chunk_mutex);
1959 list_del_init(&device->dev_alloc_list);
1960 device->fs_devices->rw_devices--;
1961 mutex_unlock(&fs_info->chunk_mutex);
1964 mutex_unlock(&uuid_mutex);
1965 ret = btrfs_shrink_device(device, 0);
1966 mutex_lock(&uuid_mutex);
1971 * TODO: the superblock still includes this device in its num_devices
1972 * counter although write_all_supers() is not locked out. This
1973 * could give a filesystem state which requires a degraded mount.
1975 ret = btrfs_rm_dev_item(device);
1979 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1980 btrfs_scrub_cancel_dev(device);
1983 * the device list mutex makes sure that we don't change
1984 * the device list while someone else is writing out all
1985 * the device supers. Whoever is writing all supers, should
1986 * lock the device list mutex before getting the number of
1987 * devices in the super block (super_copy). Conversely,
1988 * whoever updates the number of devices in the super block
1989 * (super_copy) should hold the device list mutex.
1993 * In normal cases the cur_devices == fs_devices. But in case
1994 * of deleting a seed device, the cur_devices should point to
1995 * its own fs_devices listed under the fs_devices->seed.
1997 cur_devices = device->fs_devices;
1998 mutex_lock(&fs_devices->device_list_mutex);
1999 list_del_rcu(&device->dev_list);
2001 cur_devices->num_devices--;
2002 cur_devices->total_devices--;
2003 /* Update total_devices of the parent fs_devices if it's seed */
2004 if (cur_devices != fs_devices)
2005 fs_devices->total_devices--;
2007 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
2008 cur_devices->missing_devices--;
2010 btrfs_assign_next_active_device(device, NULL);
2013 cur_devices->open_devices--;
2014 /* remove sysfs entry */
2015 btrfs_sysfs_rm_device_link(fs_devices, device);
2018 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2019 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2020 mutex_unlock(&fs_devices->device_list_mutex);
2023 * at this point, the device is zero sized and detached from
2024 * the devices list. All that's left is to zero out the old
2025 * supers and free the device.
2027 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2028 btrfs_scratch_superblocks(device->bdev, device->name->str);
2030 btrfs_close_bdev(device);
2032 btrfs_free_device(device);
2034 if (cur_devices->open_devices == 0) {
2035 while (fs_devices) {
2036 if (fs_devices->seed == cur_devices) {
2037 fs_devices->seed = cur_devices->seed;
2040 fs_devices = fs_devices->seed;
2042 cur_devices->seed = NULL;
2043 close_fs_devices(cur_devices);
2044 free_fs_devices(cur_devices);
2048 mutex_unlock(&uuid_mutex);
2052 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2053 mutex_lock(&fs_info->chunk_mutex);
2054 list_add(&device->dev_alloc_list,
2055 &fs_devices->alloc_list);
2056 device->fs_devices->rw_devices++;
2057 mutex_unlock(&fs_info->chunk_mutex);
2062 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
2064 struct btrfs_fs_devices *fs_devices;
2066 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
2069 * in case of fs with no seed, srcdev->fs_devices will point
2070 * to fs_devices of fs_info. However when the dev being replaced is
2071 * a seed dev it will point to the seed's local fs_devices. In short
2072 * srcdev will have its correct fs_devices in both the cases.
2074 fs_devices = srcdev->fs_devices;
2076 list_del_rcu(&srcdev->dev_list);
2077 list_del(&srcdev->dev_alloc_list);
2078 fs_devices->num_devices--;
2079 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
2080 fs_devices->missing_devices--;
2082 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
2083 fs_devices->rw_devices--;
2086 fs_devices->open_devices--;
2089 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
2091 struct btrfs_fs_info *fs_info = srcdev->fs_info;
2092 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
2094 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
2095 /* zero out the old super if it is writable */
2096 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2099 btrfs_close_bdev(srcdev);
2101 btrfs_free_device(srcdev);
2103 /* if this is no devs we rather delete the fs_devices */
2104 if (!fs_devices->num_devices) {
2105 struct btrfs_fs_devices *tmp_fs_devices;
2108 * On a mounted FS, num_devices can't be zero unless it's a
2109 * seed. In case of a seed device being replaced, the replace
2110 * target added to the sprout FS, so there will be no more
2111 * device left under the seed FS.
2113 ASSERT(fs_devices->seeding);
2115 tmp_fs_devices = fs_info->fs_devices;
2116 while (tmp_fs_devices) {
2117 if (tmp_fs_devices->seed == fs_devices) {
2118 tmp_fs_devices->seed = fs_devices->seed;
2121 tmp_fs_devices = tmp_fs_devices->seed;
2123 fs_devices->seed = NULL;
2124 close_fs_devices(fs_devices);
2125 free_fs_devices(fs_devices);
2129 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
2131 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
2134 mutex_lock(&fs_devices->device_list_mutex);
2136 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
2139 fs_devices->open_devices--;
2141 fs_devices->num_devices--;
2143 btrfs_assign_next_active_device(tgtdev, NULL);
2145 list_del_rcu(&tgtdev->dev_list);
2147 mutex_unlock(&fs_devices->device_list_mutex);
2150 * The update_dev_time() with in btrfs_scratch_superblocks()
2151 * may lead to a call to btrfs_show_devname() which will try
2152 * to hold device_list_mutex. And here this device
2153 * is already out of device list, so we don't have to hold
2154 * the device_list_mutex lock.
2156 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
2158 btrfs_close_bdev(tgtdev);
2160 btrfs_free_device(tgtdev);
2163 static struct btrfs_device *btrfs_find_device_by_path(
2164 struct btrfs_fs_info *fs_info, const char *device_path)
2167 struct btrfs_super_block *disk_super;
2170 struct block_device *bdev;
2171 struct buffer_head *bh;
2172 struct btrfs_device *device;
2174 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2175 fs_info->bdev_holder, 0, &bdev, &bh);
2177 return ERR_PTR(ret);
2178 disk_super = (struct btrfs_super_block *)bh->b_data;
2179 devid = btrfs_stack_device_id(&disk_super->dev_item);
2180 dev_uuid = disk_super->dev_item.uuid;
2181 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
2182 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2183 disk_super->metadata_uuid, true);
2185 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2186 disk_super->fsid, true);
2190 device = ERR_PTR(-ENOENT);
2191 blkdev_put(bdev, FMODE_READ);
2196 * Lookup a device given by device id, or the path if the id is 0.
2198 struct btrfs_device *btrfs_find_device_by_devspec(
2199 struct btrfs_fs_info *fs_info, u64 devid,
2200 const char *device_path)
2202 struct btrfs_device *device;
2205 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
2208 return ERR_PTR(-ENOENT);
2212 if (!device_path || !device_path[0])
2213 return ERR_PTR(-EINVAL);
2215 if (strcmp(device_path, "missing") == 0) {
2216 /* Find first missing device */
2217 list_for_each_entry(device, &fs_info->fs_devices->devices,
2219 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2220 &device->dev_state) && !device->bdev)
2223 return ERR_PTR(-ENOENT);
2226 return btrfs_find_device_by_path(fs_info, device_path);
2230 * does all the dirty work required for changing file system's UUID.
2232 static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
2234 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2235 struct btrfs_fs_devices *old_devices;
2236 struct btrfs_fs_devices *seed_devices;
2237 struct btrfs_super_block *disk_super = fs_info->super_copy;
2238 struct btrfs_device *device;
2241 lockdep_assert_held(&uuid_mutex);
2242 if (!fs_devices->seeding)
2245 seed_devices = alloc_fs_devices(NULL, NULL);
2246 if (IS_ERR(seed_devices))
2247 return PTR_ERR(seed_devices);
2249 old_devices = clone_fs_devices(fs_devices);
2250 if (IS_ERR(old_devices)) {
2251 kfree(seed_devices);
2252 return PTR_ERR(old_devices);
2255 list_add(&old_devices->fs_list, &fs_uuids);
2257 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2258 seed_devices->opened = 1;
2259 INIT_LIST_HEAD(&seed_devices->devices);
2260 INIT_LIST_HEAD(&seed_devices->alloc_list);
2261 mutex_init(&seed_devices->device_list_mutex);
2263 mutex_lock(&fs_devices->device_list_mutex);
2264 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2266 list_for_each_entry(device, &seed_devices->devices, dev_list)
2267 device->fs_devices = seed_devices;
2269 mutex_lock(&fs_info->chunk_mutex);
2270 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2271 mutex_unlock(&fs_info->chunk_mutex);
2273 fs_devices->seeding = false;
2274 fs_devices->num_devices = 0;
2275 fs_devices->open_devices = 0;
2276 fs_devices->missing_devices = 0;
2277 fs_devices->rotating = false;
2278 fs_devices->seed = seed_devices;
2280 generate_random_uuid(fs_devices->fsid);
2281 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
2282 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2283 mutex_unlock(&fs_devices->device_list_mutex);
2285 super_flags = btrfs_super_flags(disk_super) &
2286 ~BTRFS_SUPER_FLAG_SEEDING;
2287 btrfs_set_super_flags(disk_super, super_flags);
2293 * Store the expected generation for seed devices in device items.
2295 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
2297 struct btrfs_fs_info *fs_info = trans->fs_info;
2298 struct btrfs_root *root = fs_info->chunk_root;
2299 struct btrfs_path *path;
2300 struct extent_buffer *leaf;
2301 struct btrfs_dev_item *dev_item;
2302 struct btrfs_device *device;
2303 struct btrfs_key key;
2304 u8 fs_uuid[BTRFS_FSID_SIZE];
2305 u8 dev_uuid[BTRFS_UUID_SIZE];
2309 path = btrfs_alloc_path();
2313 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2315 key.type = BTRFS_DEV_ITEM_KEY;
2318 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2322 leaf = path->nodes[0];
2324 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2325 ret = btrfs_next_leaf(root, path);
2330 leaf = path->nodes[0];
2331 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2332 btrfs_release_path(path);
2336 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2337 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2338 key.type != BTRFS_DEV_ITEM_KEY)
2341 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2342 struct btrfs_dev_item);
2343 devid = btrfs_device_id(leaf, dev_item);
2344 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2346 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2348 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2350 BUG_ON(!device); /* Logic error */
2352 if (device->fs_devices->seeding) {
2353 btrfs_set_device_generation(leaf, dev_item,
2354 device->generation);
2355 btrfs_mark_buffer_dirty(leaf);
2363 btrfs_free_path(path);
2367 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
2369 struct btrfs_root *root = fs_info->dev_root;
2370 struct request_queue *q;
2371 struct btrfs_trans_handle *trans;
2372 struct btrfs_device *device;
2373 struct block_device *bdev;
2374 struct super_block *sb = fs_info->sb;
2375 struct rcu_string *name;
2376 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2377 u64 orig_super_total_bytes;
2378 u64 orig_super_num_devices;
2379 int seeding_dev = 0;
2381 bool unlocked = false;
2383 if (sb_rdonly(sb) && !fs_devices->seeding)
2386 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2387 fs_info->bdev_holder);
2389 return PTR_ERR(bdev);
2391 if (fs_devices->seeding) {
2393 down_write(&sb->s_umount);
2394 mutex_lock(&uuid_mutex);
2397 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2399 mutex_lock(&fs_devices->device_list_mutex);
2400 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2401 if (device->bdev == bdev) {
2404 &fs_devices->device_list_mutex);
2408 mutex_unlock(&fs_devices->device_list_mutex);
2410 device = btrfs_alloc_device(fs_info, NULL, NULL);
2411 if (IS_ERR(device)) {
2412 /* we can safely leave the fs_devices entry around */
2413 ret = PTR_ERR(device);
2417 name = rcu_string_strdup(device_path, GFP_KERNEL);
2420 goto error_free_device;
2422 rcu_assign_pointer(device->name, name);
2424 trans = btrfs_start_transaction(root, 0);
2425 if (IS_ERR(trans)) {
2426 ret = PTR_ERR(trans);
2427 goto error_free_device;
2430 q = bdev_get_queue(bdev);
2431 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2432 device->generation = trans->transid;
2433 device->io_width = fs_info->sectorsize;
2434 device->io_align = fs_info->sectorsize;
2435 device->sector_size = fs_info->sectorsize;
2436 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2437 fs_info->sectorsize);
2438 device->disk_total_bytes = device->total_bytes;
2439 device->commit_total_bytes = device->total_bytes;
2440 device->fs_info = fs_info;
2441 device->bdev = bdev;
2442 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2443 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2444 device->mode = FMODE_EXCL;
2445 device->dev_stats_valid = 1;
2446 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2449 sb->s_flags &= ~SB_RDONLY;
2450 ret = btrfs_prepare_sprout(fs_info);
2452 btrfs_abort_transaction(trans, ret);
2457 device->fs_devices = fs_devices;
2459 mutex_lock(&fs_devices->device_list_mutex);
2460 mutex_lock(&fs_info->chunk_mutex);
2461 list_add_rcu(&device->dev_list, &fs_devices->devices);
2462 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2463 fs_devices->num_devices++;
2464 fs_devices->open_devices++;
2465 fs_devices->rw_devices++;
2466 fs_devices->total_devices++;
2467 fs_devices->total_rw_bytes += device->total_bytes;
2469 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
2471 if (!blk_queue_nonrot(q))
2472 fs_devices->rotating = true;
2474 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
2475 btrfs_set_super_total_bytes(fs_info->super_copy,
2476 round_down(orig_super_total_bytes + device->total_bytes,
2477 fs_info->sectorsize));
2479 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2480 btrfs_set_super_num_devices(fs_info->super_copy,
2481 orig_super_num_devices + 1);
2483 /* add sysfs device entry */
2484 btrfs_sysfs_add_device_link(fs_devices, device);
2487 * we've got more storage, clear any full flags on the space
2490 btrfs_clear_space_info_full(fs_info);
2492 mutex_unlock(&fs_info->chunk_mutex);
2493 mutex_unlock(&fs_devices->device_list_mutex);
2496 mutex_lock(&fs_info->chunk_mutex);
2497 ret = init_first_rw_device(trans);
2498 mutex_unlock(&fs_info->chunk_mutex);
2500 btrfs_abort_transaction(trans, ret);
2505 ret = btrfs_add_dev_item(trans, device);
2507 btrfs_abort_transaction(trans, ret);
2512 ret = btrfs_finish_sprout(trans);
2514 btrfs_abort_transaction(trans, ret);
2518 btrfs_sysfs_update_sprout_fsid(fs_devices,
2519 fs_info->fs_devices->fsid);
2522 ret = btrfs_commit_transaction(trans);
2525 mutex_unlock(&uuid_mutex);
2526 up_write(&sb->s_umount);
2529 if (ret) /* transaction commit */
2532 ret = btrfs_relocate_sys_chunks(fs_info);
2534 btrfs_handle_fs_error(fs_info, ret,
2535 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2536 trans = btrfs_attach_transaction(root);
2537 if (IS_ERR(trans)) {
2538 if (PTR_ERR(trans) == -ENOENT)
2540 ret = PTR_ERR(trans);
2544 ret = btrfs_commit_transaction(trans);
2547 /* Update ctime/mtime for libblkid */
2548 update_dev_time(device_path);
2552 btrfs_sysfs_rm_device_link(fs_devices, device);
2553 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2554 mutex_lock(&fs_info->chunk_mutex);
2555 list_del_rcu(&device->dev_list);
2556 list_del(&device->dev_alloc_list);
2557 fs_info->fs_devices->num_devices--;
2558 fs_info->fs_devices->open_devices--;
2559 fs_info->fs_devices->rw_devices--;
2560 fs_info->fs_devices->total_devices--;
2561 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2562 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2563 btrfs_set_super_total_bytes(fs_info->super_copy,
2564 orig_super_total_bytes);
2565 btrfs_set_super_num_devices(fs_info->super_copy,
2566 orig_super_num_devices);
2567 mutex_unlock(&fs_info->chunk_mutex);
2568 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2571 sb->s_flags |= SB_RDONLY;
2573 btrfs_end_transaction(trans);
2575 btrfs_free_device(device);
2577 blkdev_put(bdev, FMODE_EXCL);
2578 if (seeding_dev && !unlocked) {
2579 mutex_unlock(&uuid_mutex);
2580 up_write(&sb->s_umount);
2585 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2586 struct btrfs_device *device)
2589 struct btrfs_path *path;
2590 struct btrfs_root *root = device->fs_info->chunk_root;
2591 struct btrfs_dev_item *dev_item;
2592 struct extent_buffer *leaf;
2593 struct btrfs_key key;
2595 path = btrfs_alloc_path();
2599 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2600 key.type = BTRFS_DEV_ITEM_KEY;
2601 key.offset = device->devid;
2603 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2612 leaf = path->nodes[0];
2613 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2615 btrfs_set_device_id(leaf, dev_item, device->devid);
2616 btrfs_set_device_type(leaf, dev_item, device->type);
2617 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2618 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2619 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2620 btrfs_set_device_total_bytes(leaf, dev_item,
2621 btrfs_device_get_disk_total_bytes(device));
2622 btrfs_set_device_bytes_used(leaf, dev_item,
2623 btrfs_device_get_bytes_used(device));
2624 btrfs_mark_buffer_dirty(leaf);
2627 btrfs_free_path(path);
2631 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2632 struct btrfs_device *device, u64 new_size)
2634 struct btrfs_fs_info *fs_info = device->fs_info;
2635 struct btrfs_super_block *super_copy = fs_info->super_copy;
2639 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2642 new_size = round_down(new_size, fs_info->sectorsize);
2644 mutex_lock(&fs_info->chunk_mutex);
2645 old_total = btrfs_super_total_bytes(super_copy);
2646 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
2648 if (new_size <= device->total_bytes ||
2649 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2650 mutex_unlock(&fs_info->chunk_mutex);
2654 btrfs_set_super_total_bytes(super_copy,
2655 round_down(old_total + diff, fs_info->sectorsize));
2656 device->fs_devices->total_rw_bytes += diff;
2658 btrfs_device_set_total_bytes(device, new_size);
2659 btrfs_device_set_disk_total_bytes(device, new_size);
2660 btrfs_clear_space_info_full(device->fs_info);
2661 if (list_empty(&device->post_commit_list))
2662 list_add_tail(&device->post_commit_list,
2663 &trans->transaction->dev_update_list);
2664 mutex_unlock(&fs_info->chunk_mutex);
2666 return btrfs_update_device(trans, device);
2669 static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
2671 struct btrfs_fs_info *fs_info = trans->fs_info;
2672 struct btrfs_root *root = fs_info->chunk_root;
2674 struct btrfs_path *path;
2675 struct btrfs_key key;
2677 path = btrfs_alloc_path();
2681 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2682 key.offset = chunk_offset;
2683 key.type = BTRFS_CHUNK_ITEM_KEY;
2685 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2688 else if (ret > 0) { /* Logic error or corruption */
2689 btrfs_handle_fs_error(fs_info, -ENOENT,
2690 "Failed lookup while freeing chunk.");
2695 ret = btrfs_del_item(trans, root, path);
2697 btrfs_handle_fs_error(fs_info, ret,
2698 "Failed to delete chunk item.");
2700 btrfs_free_path(path);
2704 static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2706 struct btrfs_super_block *super_copy = fs_info->super_copy;
2707 struct btrfs_disk_key *disk_key;
2708 struct btrfs_chunk *chunk;
2715 struct btrfs_key key;
2717 mutex_lock(&fs_info->chunk_mutex);
2718 array_size = btrfs_super_sys_array_size(super_copy);
2720 ptr = super_copy->sys_chunk_array;
2723 while (cur < array_size) {
2724 disk_key = (struct btrfs_disk_key *)ptr;
2725 btrfs_disk_key_to_cpu(&key, disk_key);
2727 len = sizeof(*disk_key);
2729 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2730 chunk = (struct btrfs_chunk *)(ptr + len);
2731 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2732 len += btrfs_chunk_item_size(num_stripes);
2737 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
2738 key.offset == chunk_offset) {
2739 memmove(ptr, ptr + len, array_size - (cur + len));
2741 btrfs_set_super_sys_array_size(super_copy, array_size);
2747 mutex_unlock(&fs_info->chunk_mutex);
2752 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2753 * @logical: Logical block offset in bytes.
2754 * @length: Length of extent in bytes.
2756 * Return: Chunk mapping or ERR_PTR.
2758 struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2759 u64 logical, u64 length)
2761 struct extent_map_tree *em_tree;
2762 struct extent_map *em;
2764 em_tree = &fs_info->mapping_tree;
2765 read_lock(&em_tree->lock);
2766 em = lookup_extent_mapping(em_tree, logical, length);
2767 read_unlock(&em_tree->lock);
2770 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2772 return ERR_PTR(-EINVAL);
2775 if (em->start > logical || em->start + em->len < logical) {
2777 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2778 logical, length, em->start, em->start + em->len);
2779 free_extent_map(em);
2780 return ERR_PTR(-EINVAL);
2783 /* callers are responsible for dropping em's ref. */
2787 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
2789 struct btrfs_fs_info *fs_info = trans->fs_info;
2790 struct extent_map *em;
2791 struct map_lookup *map;
2792 u64 dev_extent_len = 0;
2794 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2796 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
2799 * This is a logic error, but we don't want to just rely on the
2800 * user having built with ASSERT enabled, so if ASSERT doesn't
2801 * do anything we still error out.
2806 map = em->map_lookup;
2807 mutex_lock(&fs_info->chunk_mutex);
2808 check_system_chunk(trans, map->type);
2809 mutex_unlock(&fs_info->chunk_mutex);
2812 * Take the device list mutex to prevent races with the final phase of
2813 * a device replace operation that replaces the device object associated
2814 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2816 mutex_lock(&fs_devices->device_list_mutex);
2817 for (i = 0; i < map->num_stripes; i++) {
2818 struct btrfs_device *device = map->stripes[i].dev;
2819 ret = btrfs_free_dev_extent(trans, device,
2820 map->stripes[i].physical,
2823 mutex_unlock(&fs_devices->device_list_mutex);
2824 btrfs_abort_transaction(trans, ret);
2828 if (device->bytes_used > 0) {
2829 mutex_lock(&fs_info->chunk_mutex);
2830 btrfs_device_set_bytes_used(device,
2831 device->bytes_used - dev_extent_len);
2832 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
2833 btrfs_clear_space_info_full(fs_info);
2834 mutex_unlock(&fs_info->chunk_mutex);
2837 ret = btrfs_update_device(trans, device);
2839 mutex_unlock(&fs_devices->device_list_mutex);
2840 btrfs_abort_transaction(trans, ret);
2844 mutex_unlock(&fs_devices->device_list_mutex);
2846 ret = btrfs_free_chunk(trans, chunk_offset);
2848 btrfs_abort_transaction(trans, ret);
2852 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
2854 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2855 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
2857 btrfs_abort_transaction(trans, ret);
2862 ret = btrfs_remove_block_group(trans, chunk_offset, em);
2864 btrfs_abort_transaction(trans, ret);
2870 free_extent_map(em);
2874 static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2876 struct btrfs_root *root = fs_info->chunk_root;
2877 struct btrfs_trans_handle *trans;
2881 * Prevent races with automatic removal of unused block groups.
2882 * After we relocate and before we remove the chunk with offset
2883 * chunk_offset, automatic removal of the block group can kick in,
2884 * resulting in a failure when calling btrfs_remove_chunk() below.
2886 * Make sure to acquire this mutex before doing a tree search (dev
2887 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2888 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2889 * we release the path used to search the chunk/dev tree and before
2890 * the current task acquires this mutex and calls us.
2892 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
2894 /* step one, relocate all the extents inside this chunk */
2895 btrfs_scrub_pause(fs_info);
2896 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
2897 btrfs_scrub_continue(fs_info);
2901 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2903 if (IS_ERR(trans)) {
2904 ret = PTR_ERR(trans);
2905 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2910 * step two, delete the device extents and the
2911 * chunk tree entries
2913 ret = btrfs_remove_chunk(trans, chunk_offset);
2914 btrfs_end_transaction(trans);
2918 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
2920 struct btrfs_root *chunk_root = fs_info->chunk_root;
2921 struct btrfs_path *path;
2922 struct extent_buffer *leaf;
2923 struct btrfs_chunk *chunk;
2924 struct btrfs_key key;
2925 struct btrfs_key found_key;
2927 bool retried = false;
2931 path = btrfs_alloc_path();
2936 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2937 key.offset = (u64)-1;
2938 key.type = BTRFS_CHUNK_ITEM_KEY;
2941 mutex_lock(&fs_info->delete_unused_bgs_mutex);
2942 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2944 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2947 BUG_ON(ret == 0); /* Corruption */
2949 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2952 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2958 leaf = path->nodes[0];
2959 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2961 chunk = btrfs_item_ptr(leaf, path->slots[0],
2962 struct btrfs_chunk);
2963 chunk_type = btrfs_chunk_type(leaf, chunk);
2964 btrfs_release_path(path);
2966 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2967 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
2973 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2975 if (found_key.offset == 0)
2977 key.offset = found_key.offset - 1;
2980 if (failed && !retried) {
2984 } else if (WARN_ON(failed && retried)) {
2988 btrfs_free_path(path);
2993 * return 1 : allocate a data chunk successfully,
2994 * return <0: errors during allocating a data chunk,
2995 * return 0 : no need to allocate a data chunk.
2997 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3000 struct btrfs_block_group *cache;
3004 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3006 chunk_type = cache->flags;
3007 btrfs_put_block_group(cache);
3009 if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA))
3012 spin_lock(&fs_info->data_sinfo->lock);
3013 bytes_used = fs_info->data_sinfo->bytes_used;
3014 spin_unlock(&fs_info->data_sinfo->lock);
3017 struct btrfs_trans_handle *trans;
3020 trans = btrfs_join_transaction(fs_info->tree_root);
3022 return PTR_ERR(trans);
3024 ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA);
3025 btrfs_end_transaction(trans);
3034 static int insert_balance_item(struct btrfs_fs_info *fs_info,
3035 struct btrfs_balance_control *bctl)
3037 struct btrfs_root *root = fs_info->tree_root;
3038 struct btrfs_trans_handle *trans;
3039 struct btrfs_balance_item *item;
3040 struct btrfs_disk_balance_args disk_bargs;
3041 struct btrfs_path *path;
3042 struct extent_buffer *leaf;
3043 struct btrfs_key key;
3046 path = btrfs_alloc_path();
3050 trans = btrfs_start_transaction(root, 0);
3051 if (IS_ERR(trans)) {
3052 btrfs_free_path(path);
3053 return PTR_ERR(trans);
3056 key.objectid = BTRFS_BALANCE_OBJECTID;
3057 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3060 ret = btrfs_insert_empty_item(trans, root, path, &key,
3065 leaf = path->nodes[0];
3066 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3068 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
3070 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3071 btrfs_set_balance_data(leaf, item, &disk_bargs);
3072 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3073 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3074 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3075 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3077 btrfs_set_balance_flags(leaf, item, bctl->flags);
3079 btrfs_mark_buffer_dirty(leaf);
3081 btrfs_free_path(path);
3082 err = btrfs_commit_transaction(trans);
3088 static int del_balance_item(struct btrfs_fs_info *fs_info)
3090 struct btrfs_root *root = fs_info->tree_root;
3091 struct btrfs_trans_handle *trans;
3092 struct btrfs_path *path;
3093 struct btrfs_key key;
3096 path = btrfs_alloc_path();
3100 trans = btrfs_start_transaction(root, 0);
3101 if (IS_ERR(trans)) {
3102 btrfs_free_path(path);
3103 return PTR_ERR(trans);
3106 key.objectid = BTRFS_BALANCE_OBJECTID;
3107 key.type = BTRFS_TEMPORARY_ITEM_KEY;
3110 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3118 ret = btrfs_del_item(trans, root, path);
3120 btrfs_free_path(path);
3121 err = btrfs_commit_transaction(trans);
3128 * This is a heuristic used to reduce the number of chunks balanced on
3129 * resume after balance was interrupted.
3131 static void update_balance_args(struct btrfs_balance_control *bctl)
3134 * Turn on soft mode for chunk types that were being converted.
3136 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3137 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3138 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3139 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3140 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3141 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3144 * Turn on usage filter if is not already used. The idea is
3145 * that chunks that we have already balanced should be
3146 * reasonably full. Don't do it for chunks that are being
3147 * converted - that will keep us from relocating unconverted
3148 * (albeit full) chunks.
3150 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3151 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3152 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3153 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3154 bctl->data.usage = 90;
3156 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3157 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3158 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3159 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3160 bctl->sys.usage = 90;
3162 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3163 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3164 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3165 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3166 bctl->meta.usage = 90;
3171 * Clear the balance status in fs_info and delete the balance item from disk.
3173 static void reset_balance_state(struct btrfs_fs_info *fs_info)
3175 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3178 BUG_ON(!fs_info->balance_ctl);
3180 spin_lock(&fs_info->balance_lock);
3181 fs_info->balance_ctl = NULL;
3182 spin_unlock(&fs_info->balance_lock);
3185 ret = del_balance_item(fs_info);
3187 btrfs_handle_fs_error(fs_info, ret, NULL);
3191 * Balance filters. Return 1 if chunk should be filtered out
3192 * (should not be balanced).
3194 static int chunk_profiles_filter(u64 chunk_type,
3195 struct btrfs_balance_args *bargs)
3197 chunk_type = chunk_to_extended(chunk_type) &
3198 BTRFS_EXTENDED_PROFILE_MASK;
3200 if (bargs->profiles & chunk_type)
3206 static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3207 struct btrfs_balance_args *bargs)
3209 struct btrfs_block_group *cache;
3211 u64 user_thresh_min;
3212 u64 user_thresh_max;
3215 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3216 chunk_used = cache->used;
3218 if (bargs->usage_min == 0)
3219 user_thresh_min = 0;
3221 user_thresh_min = div_factor_fine(cache->length,
3224 if (bargs->usage_max == 0)
3225 user_thresh_max = 1;
3226 else if (bargs->usage_max > 100)
3227 user_thresh_max = cache->length;
3229 user_thresh_max = div_factor_fine(cache->length,
3232 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3235 btrfs_put_block_group(cache);
3239 static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
3240 u64 chunk_offset, struct btrfs_balance_args *bargs)
3242 struct btrfs_block_group *cache;
3243 u64 chunk_used, user_thresh;
3246 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3247 chunk_used = cache->used;
3249 if (bargs->usage_min == 0)
3251 else if (bargs->usage > 100)
3252 user_thresh = cache->length;
3254 user_thresh = div_factor_fine(cache->length, bargs->usage);
3256 if (chunk_used < user_thresh)
3259 btrfs_put_block_group(cache);
3263 static int chunk_devid_filter(struct extent_buffer *leaf,
3264 struct btrfs_chunk *chunk,
3265 struct btrfs_balance_args *bargs)
3267 struct btrfs_stripe *stripe;
3268 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3271 for (i = 0; i < num_stripes; i++) {
3272 stripe = btrfs_stripe_nr(chunk, i);
3273 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3280 static u64 calc_data_stripes(u64 type, int num_stripes)
3282 const int index = btrfs_bg_flags_to_raid_index(type);
3283 const int ncopies = btrfs_raid_array[index].ncopies;
3284 const int nparity = btrfs_raid_array[index].nparity;
3287 return num_stripes - nparity;
3289 return num_stripes / ncopies;
3292 /* [pstart, pend) */
3293 static int chunk_drange_filter(struct extent_buffer *leaf,
3294 struct btrfs_chunk *chunk,
3295 struct btrfs_balance_args *bargs)
3297 struct btrfs_stripe *stripe;
3298 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3305 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3308 type = btrfs_chunk_type(leaf, chunk);
3309 factor = calc_data_stripes(type, num_stripes);
3311 for (i = 0; i < num_stripes; i++) {
3312 stripe = btrfs_stripe_nr(chunk, i);
3313 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3316 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3317 stripe_length = btrfs_chunk_length(leaf, chunk);
3318 stripe_length = div_u64(stripe_length, factor);
3320 if (stripe_offset < bargs->pend &&
3321 stripe_offset + stripe_length > bargs->pstart)
3328 /* [vstart, vend) */
3329 static int chunk_vrange_filter(struct extent_buffer *leaf,
3330 struct btrfs_chunk *chunk,
3332 struct btrfs_balance_args *bargs)
3334 if (chunk_offset < bargs->vend &&
3335 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3336 /* at least part of the chunk is inside this vrange */
3342 static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3343 struct btrfs_chunk *chunk,
3344 struct btrfs_balance_args *bargs)
3346 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3348 if (bargs->stripes_min <= num_stripes
3349 && num_stripes <= bargs->stripes_max)
3355 static int chunk_soft_convert_filter(u64 chunk_type,
3356 struct btrfs_balance_args *bargs)
3358 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3361 chunk_type = chunk_to_extended(chunk_type) &
3362 BTRFS_EXTENDED_PROFILE_MASK;
3364 if (bargs->target == chunk_type)
3370 static int should_balance_chunk(struct extent_buffer *leaf,
3371 struct btrfs_chunk *chunk, u64 chunk_offset)
3373 struct btrfs_fs_info *fs_info = leaf->fs_info;
3374 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3375 struct btrfs_balance_args *bargs = NULL;
3376 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3379 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3380 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3384 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3385 bargs = &bctl->data;
3386 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3388 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3389 bargs = &bctl->meta;
3391 /* profiles filter */
3392 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3393 chunk_profiles_filter(chunk_type, bargs)) {
3398 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3399 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
3401 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3402 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
3407 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3408 chunk_devid_filter(leaf, chunk, bargs)) {
3412 /* drange filter, makes sense only with devid filter */
3413 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3414 chunk_drange_filter(leaf, chunk, bargs)) {
3419 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3420 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3424 /* stripes filter */
3425 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3426 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3430 /* soft profile changing mode */
3431 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3432 chunk_soft_convert_filter(chunk_type, bargs)) {
3437 * limited by count, must be the last filter
3439 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3440 if (bargs->limit == 0)
3444 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3446 * Same logic as the 'limit' filter; the minimum cannot be
3447 * determined here because we do not have the global information
3448 * about the count of all chunks that satisfy the filters.
3450 if (bargs->limit_max == 0)
3459 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
3461 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3462 struct btrfs_root *chunk_root = fs_info->chunk_root;
3464 struct btrfs_chunk *chunk;
3465 struct btrfs_path *path = NULL;
3466 struct btrfs_key key;
3467 struct btrfs_key found_key;
3468 struct extent_buffer *leaf;
3471 int enospc_errors = 0;
3472 bool counting = true;
3473 /* The single value limit and min/max limits use the same bytes in the */
3474 u64 limit_data = bctl->data.limit;
3475 u64 limit_meta = bctl->meta.limit;
3476 u64 limit_sys = bctl->sys.limit;
3480 int chunk_reserved = 0;
3482 path = btrfs_alloc_path();
3488 /* zero out stat counters */
3489 spin_lock(&fs_info->balance_lock);
3490 memset(&bctl->stat, 0, sizeof(bctl->stat));
3491 spin_unlock(&fs_info->balance_lock);
3495 * The single value limit and min/max limits use the same bytes
3498 bctl->data.limit = limit_data;
3499 bctl->meta.limit = limit_meta;
3500 bctl->sys.limit = limit_sys;
3502 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3503 key.offset = (u64)-1;
3504 key.type = BTRFS_CHUNK_ITEM_KEY;
3507 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
3508 atomic_read(&fs_info->balance_cancel_req)) {
3513 mutex_lock(&fs_info->delete_unused_bgs_mutex);
3514 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3516 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3521 * this shouldn't happen, it means the last relocate
3525 BUG(); /* FIXME break ? */
3527 ret = btrfs_previous_item(chunk_root, path, 0,
3528 BTRFS_CHUNK_ITEM_KEY);
3530 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3535 leaf = path->nodes[0];
3536 slot = path->slots[0];
3537 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3539 if (found_key.objectid != key.objectid) {
3540 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3544 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3545 chunk_type = btrfs_chunk_type(leaf, chunk);
3548 spin_lock(&fs_info->balance_lock);
3549 bctl->stat.considered++;
3550 spin_unlock(&fs_info->balance_lock);
3553 ret = should_balance_chunk(leaf, chunk, found_key.offset);
3555 btrfs_release_path(path);
3557 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3562 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3563 spin_lock(&fs_info->balance_lock);
3564 bctl->stat.expected++;
3565 spin_unlock(&fs_info->balance_lock);
3567 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3569 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3571 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3578 * Apply limit_min filter, no need to check if the LIMITS
3579 * filter is used, limit_min is 0 by default
3581 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3582 count_data < bctl->data.limit_min)
3583 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3584 count_meta < bctl->meta.limit_min)
3585 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3586 count_sys < bctl->sys.limit_min)) {
3587 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3591 if (!chunk_reserved) {
3593 * We may be relocating the only data chunk we have,
3594 * which could potentially end up with losing data's
3595 * raid profile, so lets allocate an empty one in
3598 ret = btrfs_may_alloc_data_chunk(fs_info,
3601 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3603 } else if (ret == 1) {
3608 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3609 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3610 if (ret == -ENOSPC) {
3612 } else if (ret == -ETXTBSY) {
3614 "skipping relocation of block group %llu due to active swapfile",
3620 spin_lock(&fs_info->balance_lock);
3621 bctl->stat.completed++;
3622 spin_unlock(&fs_info->balance_lock);
3625 if (found_key.offset == 0)
3627 key.offset = found_key.offset - 1;
3631 btrfs_release_path(path);
3636 btrfs_free_path(path);
3637 if (enospc_errors) {
3638 btrfs_info(fs_info, "%d enospc errors during balance",
3648 * alloc_profile_is_valid - see if a given profile is valid and reduced
3649 * @flags: profile to validate
3650 * @extended: if true @flags is treated as an extended profile
3652 static int alloc_profile_is_valid(u64 flags, int extended)
3654 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3655 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3657 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3659 /* 1) check that all other bits are zeroed */
3663 /* 2) see if profile is reduced */
3665 return !extended; /* "0" is valid for usual profiles */
3667 return has_single_bit_set(flags);
3670 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3672 /* cancel requested || normal exit path */
3673 return atomic_read(&fs_info->balance_cancel_req) ||
3674 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3675 atomic_read(&fs_info->balance_cancel_req) == 0);
3678 /* Non-zero return value signifies invalidity */
3679 static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3682 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3683 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3684 (bctl_arg->target & ~allowed)));
3688 * Fill @buf with textual description of balance filter flags @bargs, up to
3689 * @size_buf including the terminating null. The output may be trimmed if it
3690 * does not fit into the provided buffer.
3692 static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3696 u32 size_bp = size_buf;
3698 u64 flags = bargs->flags;
3699 char tmp_buf[128] = {'\0'};
3704 #define CHECK_APPEND_NOARG(a) \
3706 ret = snprintf(bp, size_bp, (a)); \
3707 if (ret < 0 || ret >= size_bp) \
3708 goto out_overflow; \
3713 #define CHECK_APPEND_1ARG(a, v1) \
3715 ret = snprintf(bp, size_bp, (a), (v1)); \
3716 if (ret < 0 || ret >= size_bp) \
3717 goto out_overflow; \
3722 #define CHECK_APPEND_2ARG(a, v1, v2) \
3724 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3725 if (ret < 0 || ret >= size_bp) \
3726 goto out_overflow; \
3731 if (flags & BTRFS_BALANCE_ARGS_CONVERT)
3732 CHECK_APPEND_1ARG("convert=%s,",
3733 btrfs_bg_type_to_raid_name(bargs->target));
3735 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3736 CHECK_APPEND_NOARG("soft,");
3738 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3739 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3741 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3744 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3745 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3747 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3748 CHECK_APPEND_2ARG("usage=%u..%u,",
3749 bargs->usage_min, bargs->usage_max);
3751 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3752 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3754 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3755 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3756 bargs->pstart, bargs->pend);
3758 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3759 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3760 bargs->vstart, bargs->vend);
3762 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
3763 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
3765 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
3766 CHECK_APPEND_2ARG("limit=%u..%u,",
3767 bargs->limit_min, bargs->limit_max);
3769 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
3770 CHECK_APPEND_2ARG("stripes=%u..%u,",
3771 bargs->stripes_min, bargs->stripes_max);
3773 #undef CHECK_APPEND_2ARG
3774 #undef CHECK_APPEND_1ARG
3775 #undef CHECK_APPEND_NOARG
3779 if (size_bp < size_buf)
3780 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
3785 static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
3787 u32 size_buf = 1024;
3788 char tmp_buf[192] = {'\0'};
3791 u32 size_bp = size_buf;
3793 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3795 buf = kzalloc(size_buf, GFP_KERNEL);
3801 #define CHECK_APPEND_1ARG(a, v1) \
3803 ret = snprintf(bp, size_bp, (a), (v1)); \
3804 if (ret < 0 || ret >= size_bp) \
3805 goto out_overflow; \
3810 if (bctl->flags & BTRFS_BALANCE_FORCE)
3811 CHECK_APPEND_1ARG("%s", "-f ");
3813 if (bctl->flags & BTRFS_BALANCE_DATA) {
3814 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
3815 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
3818 if (bctl->flags & BTRFS_BALANCE_METADATA) {
3819 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
3820 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
3823 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
3824 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
3825 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
3828 #undef CHECK_APPEND_1ARG
3832 if (size_bp < size_buf)
3833 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
3834 btrfs_info(fs_info, "balance: %s %s",
3835 (bctl->flags & BTRFS_BALANCE_RESUME) ?
3836 "resume" : "start", buf);
3842 * Should be called with balance mutexe held
3844 int btrfs_balance(struct btrfs_fs_info *fs_info,
3845 struct btrfs_balance_control *bctl,
3846 struct btrfs_ioctl_balance_args *bargs)
3848 u64 meta_target, data_target;
3854 bool reducing_redundancy;
3857 if (btrfs_fs_closing(fs_info) ||
3858 atomic_read(&fs_info->balance_pause_req) ||
3859 atomic_read(&fs_info->balance_cancel_req)) {
3864 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3865 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3869 * In case of mixed groups both data and meta should be picked,
3870 * and identical options should be given for both of them.
3872 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3873 if (mixed && (bctl->flags & allowed)) {
3874 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3875 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3876 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3878 "balance: mixed groups data and metadata options must be the same");
3885 * rw_devices will not change at the moment, device add/delete/replace
3886 * are excluded by EXCL_OP
3888 num_devices = fs_info->fs_devices->rw_devices;
3891 * SINGLE profile on-disk has no profile bit, but in-memory we have a
3892 * special bit for it, to make it easier to distinguish. Thus we need
3893 * to set it manually, or balance would refuse the profile.
3895 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
3896 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
3897 if (num_devices >= btrfs_raid_array[i].devs_min)
3898 allowed |= btrfs_raid_array[i].bg_flag;
3900 if (validate_convert_profile(&bctl->data, allowed)) {
3902 "balance: invalid convert data profile %s",
3903 btrfs_bg_type_to_raid_name(bctl->data.target));
3907 if (validate_convert_profile(&bctl->meta, allowed)) {
3909 "balance: invalid convert metadata profile %s",
3910 btrfs_bg_type_to_raid_name(bctl->meta.target));
3914 if (validate_convert_profile(&bctl->sys, allowed)) {
3916 "balance: invalid convert system profile %s",
3917 btrfs_bg_type_to_raid_name(bctl->sys.target));
3923 * Allow to reduce metadata or system integrity only if force set for
3924 * profiles with redundancy (copies, parity)
3927 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
3928 if (btrfs_raid_array[i].ncopies >= 2 ||
3929 btrfs_raid_array[i].tolerated_failures >= 1)
3930 allowed |= btrfs_raid_array[i].bg_flag;
3933 seq = read_seqbegin(&fs_info->profiles_lock);
3935 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3936 (fs_info->avail_system_alloc_bits & allowed) &&
3937 !(bctl->sys.target & allowed)) ||
3938 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3939 (fs_info->avail_metadata_alloc_bits & allowed) &&
3940 !(bctl->meta.target & allowed)))
3941 reducing_redundancy = true;
3943 reducing_redundancy = false;
3945 /* if we're not converting, the target field is uninitialized */
3946 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3947 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3948 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3949 bctl->data.target : fs_info->avail_data_alloc_bits;
3950 } while (read_seqretry(&fs_info->profiles_lock, seq));
3952 if (reducing_redundancy) {
3953 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3955 "balance: force reducing metadata redundancy");
3958 "balance: reduces metadata redundancy, use --force if you want this");
3964 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3965 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
3967 "balance: metadata profile %s has lower redundancy than data profile %s",
3968 btrfs_bg_type_to_raid_name(meta_target),
3969 btrfs_bg_type_to_raid_name(data_target));
3972 if (fs_info->send_in_progress) {
3973 btrfs_warn_rl(fs_info,
3974 "cannot run balance while send operations are in progress (%d in progress)",
3975 fs_info->send_in_progress);
3980 ret = insert_balance_item(fs_info, bctl);
3981 if (ret && ret != -EEXIST)
3984 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3985 BUG_ON(ret == -EEXIST);
3986 BUG_ON(fs_info->balance_ctl);
3987 spin_lock(&fs_info->balance_lock);
3988 fs_info->balance_ctl = bctl;
3989 spin_unlock(&fs_info->balance_lock);
3991 BUG_ON(ret != -EEXIST);
3992 spin_lock(&fs_info->balance_lock);
3993 update_balance_args(bctl);
3994 spin_unlock(&fs_info->balance_lock);
3997 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
3998 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
3999 describe_balance_start_or_resume(fs_info);
4000 mutex_unlock(&fs_info->balance_mutex);
4002 ret = __btrfs_balance(fs_info);
4004 mutex_lock(&fs_info->balance_mutex);
4005 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4006 btrfs_info(fs_info, "balance: paused");
4007 else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
4008 btrfs_info(fs_info, "balance: canceled");
4010 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4012 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
4015 memset(bargs, 0, sizeof(*bargs));
4016 btrfs_update_ioctl_balance_args(fs_info, bargs);
4019 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4020 balance_need_close(fs_info)) {
4021 reset_balance_state(fs_info);
4022 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4025 wake_up(&fs_info->balance_wait_q);
4029 if (bctl->flags & BTRFS_BALANCE_RESUME)
4030 reset_balance_state(fs_info);
4033 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4038 static int balance_kthread(void *data)
4040 struct btrfs_fs_info *fs_info = data;
4043 mutex_lock(&fs_info->balance_mutex);
4044 if (fs_info->balance_ctl)
4045 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
4046 mutex_unlock(&fs_info->balance_mutex);
4051 int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4053 struct task_struct *tsk;
4055 mutex_lock(&fs_info->balance_mutex);
4056 if (!fs_info->balance_ctl) {
4057 mutex_unlock(&fs_info->balance_mutex);
4060 mutex_unlock(&fs_info->balance_mutex);
4062 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
4063 btrfs_info(fs_info, "balance: resume skipped");
4068 * A ro->rw remount sequence should continue with the paused balance
4069 * regardless of who pauses it, system or the user as of now, so set
4072 spin_lock(&fs_info->balance_lock);
4073 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4074 spin_unlock(&fs_info->balance_lock);
4076 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
4077 return PTR_ERR_OR_ZERO(tsk);
4080 int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
4082 struct btrfs_balance_control *bctl;
4083 struct btrfs_balance_item *item;
4084 struct btrfs_disk_balance_args disk_bargs;
4085 struct btrfs_path *path;
4086 struct extent_buffer *leaf;
4087 struct btrfs_key key;
4090 path = btrfs_alloc_path();
4094 key.objectid = BTRFS_BALANCE_OBJECTID;
4095 key.type = BTRFS_TEMPORARY_ITEM_KEY;
4098 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4101 if (ret > 0) { /* ret = -ENOENT; */
4106 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4112 leaf = path->nodes[0];
4113 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4115 bctl->flags = btrfs_balance_flags(leaf, item);
4116 bctl->flags |= BTRFS_BALANCE_RESUME;
4118 btrfs_balance_data(leaf, item, &disk_bargs);
4119 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4120 btrfs_balance_meta(leaf, item, &disk_bargs);
4121 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4122 btrfs_balance_sys(leaf, item, &disk_bargs);
4123 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4126 * This should never happen, as the paused balance state is recovered
4127 * during mount without any chance of other exclusive ops to collide.
4129 * This gives the exclusive op status to balance and keeps in paused
4130 * state until user intervention (cancel or umount). If the ownership
4131 * cannot be assigned, show a message but do not fail. The balance
4132 * is in a paused state and must have fs_info::balance_ctl properly
4135 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4137 "balance: cannot set exclusive op status, resume manually");
4139 mutex_lock(&fs_info->balance_mutex);
4140 BUG_ON(fs_info->balance_ctl);
4141 spin_lock(&fs_info->balance_lock);
4142 fs_info->balance_ctl = bctl;
4143 spin_unlock(&fs_info->balance_lock);
4144 mutex_unlock(&fs_info->balance_mutex);
4146 btrfs_free_path(path);
4150 int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4154 mutex_lock(&fs_info->balance_mutex);
4155 if (!fs_info->balance_ctl) {
4156 mutex_unlock(&fs_info->balance_mutex);
4160 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4161 atomic_inc(&fs_info->balance_pause_req);
4162 mutex_unlock(&fs_info->balance_mutex);
4164 wait_event(fs_info->balance_wait_q,
4165 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4167 mutex_lock(&fs_info->balance_mutex);
4168 /* we are good with balance_ctl ripped off from under us */
4169 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4170 atomic_dec(&fs_info->balance_pause_req);
4175 mutex_unlock(&fs_info->balance_mutex);
4179 int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4181 mutex_lock(&fs_info->balance_mutex);
4182 if (!fs_info->balance_ctl) {
4183 mutex_unlock(&fs_info->balance_mutex);
4188 * A paused balance with the item stored on disk can be resumed at
4189 * mount time if the mount is read-write. Otherwise it's still paused
4190 * and we must not allow cancelling as it deletes the item.
4192 if (sb_rdonly(fs_info->sb)) {
4193 mutex_unlock(&fs_info->balance_mutex);
4197 atomic_inc(&fs_info->balance_cancel_req);
4199 * if we are running just wait and return, balance item is
4200 * deleted in btrfs_balance in this case
4202 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4203 mutex_unlock(&fs_info->balance_mutex);
4204 wait_event(fs_info->balance_wait_q,
4205 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4206 mutex_lock(&fs_info->balance_mutex);
4208 mutex_unlock(&fs_info->balance_mutex);
4210 * Lock released to allow other waiters to continue, we'll
4211 * reexamine the status again.
4213 mutex_lock(&fs_info->balance_mutex);
4215 if (fs_info->balance_ctl) {
4216 reset_balance_state(fs_info);
4217 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4218 btrfs_info(fs_info, "balance: canceled");
4222 BUG_ON(fs_info->balance_ctl ||
4223 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4224 atomic_dec(&fs_info->balance_cancel_req);
4225 mutex_unlock(&fs_info->balance_mutex);
4229 static int btrfs_uuid_scan_kthread(void *data)
4231 struct btrfs_fs_info *fs_info = data;
4232 struct btrfs_root *root = fs_info->tree_root;
4233 struct btrfs_key key;
4234 struct btrfs_path *path = NULL;
4236 struct extent_buffer *eb;
4238 struct btrfs_root_item root_item;
4240 struct btrfs_trans_handle *trans = NULL;
4242 path = btrfs_alloc_path();
4249 key.type = BTRFS_ROOT_ITEM_KEY;
4253 ret = btrfs_search_forward(root, &key, path,
4254 BTRFS_OLDEST_GENERATION);
4261 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4262 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4263 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4264 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4267 eb = path->nodes[0];
4268 slot = path->slots[0];
4269 item_size = btrfs_item_size_nr(eb, slot);
4270 if (item_size < sizeof(root_item))
4273 read_extent_buffer(eb, &root_item,
4274 btrfs_item_ptr_offset(eb, slot),
4275 (int)sizeof(root_item));
4276 if (btrfs_root_refs(&root_item) == 0)
4279 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4280 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4284 btrfs_release_path(path);
4286 * 1 - subvol uuid item
4287 * 1 - received_subvol uuid item
4289 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4290 if (IS_ERR(trans)) {
4291 ret = PTR_ERR(trans);
4299 if (!btrfs_is_empty_uuid(root_item.uuid)) {
4300 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
4301 BTRFS_UUID_KEY_SUBVOL,
4304 btrfs_warn(fs_info, "uuid_tree_add failed %d",
4310 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
4311 ret = btrfs_uuid_tree_add(trans,
4312 root_item.received_uuid,
4313 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4316 btrfs_warn(fs_info, "uuid_tree_add failed %d",
4324 ret = btrfs_end_transaction(trans);
4330 btrfs_release_path(path);
4331 if (key.offset < (u64)-1) {
4333 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4335 key.type = BTRFS_ROOT_ITEM_KEY;
4336 } else if (key.objectid < (u64)-1) {
4338 key.type = BTRFS_ROOT_ITEM_KEY;
4347 btrfs_free_path(path);
4348 if (trans && !IS_ERR(trans))
4349 btrfs_end_transaction(trans);
4351 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
4353 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
4354 up(&fs_info->uuid_tree_rescan_sem);
4359 * Callback for btrfs_uuid_tree_iterate().
4361 * 0 check succeeded, the entry is not outdated.
4362 * < 0 if an error occurred.
4363 * > 0 if the check failed, which means the caller shall remove the entry.
4365 static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4366 u8 *uuid, u8 type, u64 subid)
4368 struct btrfs_key key;
4370 struct btrfs_root *subvol_root;
4372 if (type != BTRFS_UUID_KEY_SUBVOL &&
4373 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4376 key.objectid = subid;
4377 key.type = BTRFS_ROOT_ITEM_KEY;
4378 key.offset = (u64)-1;
4379 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4380 if (IS_ERR(subvol_root)) {
4381 ret = PTR_ERR(subvol_root);
4388 case BTRFS_UUID_KEY_SUBVOL:
4389 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4392 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4393 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4403 static int btrfs_uuid_rescan_kthread(void *data)
4405 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4409 * 1st step is to iterate through the existing UUID tree and
4410 * to delete all entries that contain outdated data.
4411 * 2nd step is to add all missing entries to the UUID tree.
4413 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4415 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
4416 up(&fs_info->uuid_tree_rescan_sem);
4419 return btrfs_uuid_scan_kthread(data);
4422 int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4424 struct btrfs_trans_handle *trans;
4425 struct btrfs_root *tree_root = fs_info->tree_root;
4426 struct btrfs_root *uuid_root;
4427 struct task_struct *task;
4434 trans = btrfs_start_transaction(tree_root, 2);
4436 return PTR_ERR(trans);
4438 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
4439 if (IS_ERR(uuid_root)) {
4440 ret = PTR_ERR(uuid_root);
4441 btrfs_abort_transaction(trans, ret);
4442 btrfs_end_transaction(trans);
4446 fs_info->uuid_root = uuid_root;
4448 ret = btrfs_commit_transaction(trans);
4452 down(&fs_info->uuid_tree_rescan_sem);
4453 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4455 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4456 btrfs_warn(fs_info, "failed to start uuid_scan task");
4457 up(&fs_info->uuid_tree_rescan_sem);
4458 return PTR_ERR(task);
4464 int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4466 struct task_struct *task;
4468 down(&fs_info->uuid_tree_rescan_sem);
4469 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4471 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4472 btrfs_warn(fs_info, "failed to start uuid_rescan task");
4473 up(&fs_info->uuid_tree_rescan_sem);
4474 return PTR_ERR(task);
4481 * shrinking a device means finding all of the device extents past
4482 * the new size, and then following the back refs to the chunks.
4483 * The chunk relocation code actually frees the device extent
4485 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4487 struct btrfs_fs_info *fs_info = device->fs_info;
4488 struct btrfs_root *root = fs_info->dev_root;
4489 struct btrfs_trans_handle *trans;
4490 struct btrfs_dev_extent *dev_extent = NULL;
4491 struct btrfs_path *path;
4497 bool retried = false;
4498 struct extent_buffer *l;
4499 struct btrfs_key key;
4500 struct btrfs_super_block *super_copy = fs_info->super_copy;
4501 u64 old_total = btrfs_super_total_bytes(super_copy);
4502 u64 old_size = btrfs_device_get_total_bytes(device);
4506 new_size = round_down(new_size, fs_info->sectorsize);
4508 diff = round_down(old_size - new_size, fs_info->sectorsize);
4510 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
4513 path = btrfs_alloc_path();
4517 path->reada = READA_BACK;
4519 trans = btrfs_start_transaction(root, 0);
4520 if (IS_ERR(trans)) {
4521 btrfs_free_path(path);
4522 return PTR_ERR(trans);
4525 mutex_lock(&fs_info->chunk_mutex);
4527 btrfs_device_set_total_bytes(device, new_size);
4528 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
4529 device->fs_devices->total_rw_bytes -= diff;
4530 atomic64_sub(diff, &fs_info->free_chunk_space);
4534 * Once the device's size has been set to the new size, ensure all
4535 * in-memory chunks are synced to disk so that the loop below sees them
4536 * and relocates them accordingly.
4538 if (contains_pending_extent(device, &start, diff)) {
4539 mutex_unlock(&fs_info->chunk_mutex);
4540 ret = btrfs_commit_transaction(trans);
4544 mutex_unlock(&fs_info->chunk_mutex);
4545 btrfs_end_transaction(trans);
4549 key.objectid = device->devid;
4550 key.offset = (u64)-1;
4551 key.type = BTRFS_DEV_EXTENT_KEY;
4554 mutex_lock(&fs_info->delete_unused_bgs_mutex);
4555 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4557 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4561 ret = btrfs_previous_item(root, path, 0, key.type);
4563 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4568 btrfs_release_path(path);
4573 slot = path->slots[0];
4574 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4576 if (key.objectid != device->devid) {
4577 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4578 btrfs_release_path(path);
4582 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4583 length = btrfs_dev_extent_length(l, dev_extent);
4585 if (key.offset + length <= new_size) {
4586 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4587 btrfs_release_path(path);
4591 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
4592 btrfs_release_path(path);
4595 * We may be relocating the only data chunk we have,
4596 * which could potentially end up with losing data's
4597 * raid profile, so lets allocate an empty one in
4600 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4602 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4606 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4607 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4608 if (ret == -ENOSPC) {
4611 if (ret == -ETXTBSY) {
4613 "could not shrink block group %llu due to active swapfile",
4618 } while (key.offset-- > 0);
4620 if (failed && !retried) {
4624 } else if (failed && retried) {
4629 /* Shrinking succeeded, else we would be at "done". */
4630 trans = btrfs_start_transaction(root, 0);
4631 if (IS_ERR(trans)) {
4632 ret = PTR_ERR(trans);
4636 mutex_lock(&fs_info->chunk_mutex);
4637 btrfs_device_set_disk_total_bytes(device, new_size);
4638 if (list_empty(&device->post_commit_list))
4639 list_add_tail(&device->post_commit_list,
4640 &trans->transaction->dev_update_list);
4642 WARN_ON(diff > old_total);
4643 btrfs_set_super_total_bytes(super_copy,
4644 round_down(old_total - diff, fs_info->sectorsize));
4645 mutex_unlock(&fs_info->chunk_mutex);
4647 /* Now btrfs_update_device() will change the on-disk size. */
4648 ret = btrfs_update_device(trans, device);
4650 btrfs_abort_transaction(trans, ret);
4651 btrfs_end_transaction(trans);
4653 ret = btrfs_commit_transaction(trans);
4656 btrfs_free_path(path);
4658 mutex_lock(&fs_info->chunk_mutex);
4659 btrfs_device_set_total_bytes(device, old_size);
4660 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
4661 device->fs_devices->total_rw_bytes += diff;
4662 atomic64_add(diff, &fs_info->free_chunk_space);
4663 mutex_unlock(&fs_info->chunk_mutex);
4668 static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
4669 struct btrfs_key *key,
4670 struct btrfs_chunk *chunk, int item_size)
4672 struct btrfs_super_block *super_copy = fs_info->super_copy;
4673 struct btrfs_disk_key disk_key;
4677 mutex_lock(&fs_info->chunk_mutex);
4678 array_size = btrfs_super_sys_array_size(super_copy);
4679 if (array_size + item_size + sizeof(disk_key)
4680 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4681 mutex_unlock(&fs_info->chunk_mutex);
4685 ptr = super_copy->sys_chunk_array + array_size;
4686 btrfs_cpu_key_to_disk(&disk_key, key);
4687 memcpy(ptr, &disk_key, sizeof(disk_key));
4688 ptr += sizeof(disk_key);
4689 memcpy(ptr, chunk, item_size);
4690 item_size += sizeof(disk_key);
4691 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4692 mutex_unlock(&fs_info->chunk_mutex);
4698 * sort the devices in descending order by max_avail, total_avail
4700 static int btrfs_cmp_device_info(const void *a, const void *b)
4702 const struct btrfs_device_info *di_a = a;
4703 const struct btrfs_device_info *di_b = b;
4705 if (di_a->max_avail > di_b->max_avail)
4707 if (di_a->max_avail < di_b->max_avail)
4709 if (di_a->total_avail > di_b->total_avail)
4711 if (di_a->total_avail < di_b->total_avail)
4716 static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4718 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
4721 btrfs_set_fs_incompat(info, RAID56);
4724 static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type)
4726 if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4)))
4729 btrfs_set_fs_incompat(info, RAID1C34);
4732 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4733 u64 start, u64 type)
4735 struct btrfs_fs_info *info = trans->fs_info;
4736 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4737 struct btrfs_device *device;
4738 struct map_lookup *map = NULL;
4739 struct extent_map_tree *em_tree;
4740 struct extent_map *em;
4741 struct btrfs_device_info *devices_info = NULL;
4743 int num_stripes; /* total number of stripes to allocate */
4744 int data_stripes; /* number of stripes that count for
4746 int sub_stripes; /* sub_stripes info for map */
4747 int dev_stripes; /* stripes per dev */
4748 int devs_max; /* max devs to use */
4749 int devs_min; /* min devs needed */
4750 int devs_increment; /* ndevs has to be a multiple of this */
4751 int ncopies; /* how many copies to data has */
4752 int nparity; /* number of stripes worth of bytes to
4753 store parity information */
4755 u64 max_stripe_size;
4764 BUG_ON(!alloc_profile_is_valid(type, 0));
4766 if (list_empty(&fs_devices->alloc_list)) {
4767 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4768 btrfs_debug(info, "%s: no writable device", __func__);
4772 index = btrfs_bg_flags_to_raid_index(type);
4774 sub_stripes = btrfs_raid_array[index].sub_stripes;
4775 dev_stripes = btrfs_raid_array[index].dev_stripes;
4776 devs_max = btrfs_raid_array[index].devs_max;
4778 devs_max = BTRFS_MAX_DEVS(info);
4779 devs_min = btrfs_raid_array[index].devs_min;
4780 devs_increment = btrfs_raid_array[index].devs_increment;
4781 ncopies = btrfs_raid_array[index].ncopies;
4782 nparity = btrfs_raid_array[index].nparity;
4784 if (type & BTRFS_BLOCK_GROUP_DATA) {
4785 max_stripe_size = SZ_1G;
4786 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
4787 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4788 /* for larger filesystems, use larger metadata chunks */
4789 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4790 max_stripe_size = SZ_1G;
4792 max_stripe_size = SZ_256M;
4793 max_chunk_size = max_stripe_size;
4794 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4795 max_stripe_size = SZ_32M;
4796 max_chunk_size = 2 * max_stripe_size;
4797 devs_max = min_t(int, devs_max, BTRFS_MAX_DEVS_SYS_CHUNK);
4799 btrfs_err(info, "invalid chunk type 0x%llx requested",
4804 /* We don't want a chunk larger than 10% of writable space */
4805 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4808 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
4814 * in the first pass through the devices list, we gather information
4815 * about the available holes on each device.
4818 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
4822 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
4824 "BTRFS: read-only device in alloc_list\n");
4828 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4829 &device->dev_state) ||
4830 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
4833 if (device->total_bytes > device->bytes_used)
4834 total_avail = device->total_bytes - device->bytes_used;
4838 /* If there is no space on this device, skip it. */
4839 if (total_avail == 0)
4842 ret = find_free_dev_extent(device,
4843 max_stripe_size * dev_stripes,
4844 &dev_offset, &max_avail);
4845 if (ret && ret != -ENOSPC)
4849 max_avail = max_stripe_size * dev_stripes;
4851 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4852 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4854 "%s: devid %llu has no free space, have=%llu want=%u",
4855 __func__, device->devid, max_avail,
4856 BTRFS_STRIPE_LEN * dev_stripes);
4860 if (ndevs == fs_devices->rw_devices) {
4861 WARN(1, "%s: found more than %llu devices\n",
4862 __func__, fs_devices->rw_devices);
4865 devices_info[ndevs].dev_offset = dev_offset;
4866 devices_info[ndevs].max_avail = max_avail;
4867 devices_info[ndevs].total_avail = total_avail;
4868 devices_info[ndevs].dev = device;
4873 * now sort the devices by hole size / available space
4875 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4876 btrfs_cmp_device_info, NULL);
4879 * Round down to number of usable stripes, devs_increment can be any
4880 * number so we can't use round_down()
4882 ndevs -= ndevs % devs_increment;
4884 if (ndevs < devs_min) {
4886 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4888 "%s: not enough devices with free space: have=%d minimum required=%d",
4889 __func__, ndevs, devs_min);
4894 ndevs = min(ndevs, devs_max);
4897 * The primary goal is to maximize the number of stripes, so use as
4898 * many devices as possible, even if the stripes are not maximum sized.
4900 * The DUP profile stores more than one stripe per device, the
4901 * max_avail is the total size so we have to adjust.
4903 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
4904 num_stripes = ndevs * dev_stripes;
4907 * this will have to be fixed for RAID1 and RAID10 over
4910 data_stripes = (num_stripes - nparity) / ncopies;
4913 * Use the number of data stripes to figure out how big this chunk
4914 * is really going to be in terms of logical address space,
4915 * and compare that answer with the max chunk size. If it's higher,
4916 * we try to reduce stripe_size.
4918 if (stripe_size * data_stripes > max_chunk_size) {
4920 * Reduce stripe_size, round it up to a 16MB boundary again and
4921 * then use it, unless it ends up being even bigger than the
4922 * previous value we had already.
4924 stripe_size = min(round_up(div_u64(max_chunk_size,
4925 data_stripes), SZ_16M),
4929 /* align to BTRFS_STRIPE_LEN */
4930 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
4932 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4937 map->num_stripes = num_stripes;
4939 for (i = 0; i < ndevs; ++i) {
4940 for (j = 0; j < dev_stripes; ++j) {
4941 int s = i * dev_stripes + j;
4942 map->stripes[s].dev = devices_info[i].dev;
4943 map->stripes[s].physical = devices_info[i].dev_offset +
4947 map->stripe_len = BTRFS_STRIPE_LEN;
4948 map->io_align = BTRFS_STRIPE_LEN;
4949 map->io_width = BTRFS_STRIPE_LEN;
4951 map->sub_stripes = sub_stripes;
4953 chunk_size = stripe_size * data_stripes;
4955 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
4957 em = alloc_extent_map();
4963 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
4964 em->map_lookup = map;
4966 em->len = chunk_size;
4967 em->block_start = 0;
4968 em->block_len = em->len;
4969 em->orig_block_len = stripe_size;
4971 em_tree = &info->mapping_tree;
4972 write_lock(&em_tree->lock);
4973 ret = add_extent_mapping(em_tree, em, 0);
4975 write_unlock(&em_tree->lock);
4976 free_extent_map(em);
4979 write_unlock(&em_tree->lock);
4981 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
4983 goto error_del_extent;
4985 for (i = 0; i < map->num_stripes; i++) {
4986 struct btrfs_device *dev = map->stripes[i].dev;
4988 btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size);
4989 if (list_empty(&dev->post_commit_list))
4990 list_add_tail(&dev->post_commit_list,
4991 &trans->transaction->dev_update_list);
4994 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
4996 free_extent_map(em);
4997 check_raid56_incompat_flag(info, type);
4998 check_raid1c34_incompat_flag(info, type);
5000 kfree(devices_info);
5004 write_lock(&em_tree->lock);
5005 remove_extent_mapping(em_tree, em);
5006 write_unlock(&em_tree->lock);
5008 /* One for our allocation */
5009 free_extent_map(em);
5010 /* One for the tree reference */
5011 free_extent_map(em);
5013 kfree(devices_info);
5017 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
5018 u64 chunk_offset, u64 chunk_size)
5020 struct btrfs_fs_info *fs_info = trans->fs_info;
5021 struct btrfs_root *extent_root = fs_info->extent_root;
5022 struct btrfs_root *chunk_root = fs_info->chunk_root;
5023 struct btrfs_key key;
5024 struct btrfs_device *device;
5025 struct btrfs_chunk *chunk;
5026 struct btrfs_stripe *stripe;
5027 struct extent_map *em;
5028 struct map_lookup *map;
5035 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
5039 map = em->map_lookup;
5040 item_size = btrfs_chunk_item_size(map->num_stripes);
5041 stripe_size = em->orig_block_len;
5043 chunk = kzalloc(item_size, GFP_NOFS);
5050 * Take the device list mutex to prevent races with the final phase of
5051 * a device replace operation that replaces the device object associated
5052 * with the map's stripes, because the device object's id can change
5053 * at any time during that final phase of the device replace operation
5054 * (dev-replace.c:btrfs_dev_replace_finishing()).
5056 mutex_lock(&fs_info->fs_devices->device_list_mutex);
5057 for (i = 0; i < map->num_stripes; i++) {
5058 device = map->stripes[i].dev;
5059 dev_offset = map->stripes[i].physical;
5061 ret = btrfs_update_device(trans, device);
5064 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5065 dev_offset, stripe_size);
5070 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
5074 stripe = &chunk->stripe;
5075 for (i = 0; i < map->num_stripes; i++) {
5076 device = map->stripes[i].dev;
5077 dev_offset = map->stripes[i].physical;
5079 btrfs_set_stack_stripe_devid(stripe, device->devid);
5080 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5081 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5084 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
5086 btrfs_set_stack_chunk_length(chunk, chunk_size);
5087 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5088 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5089 btrfs_set_stack_chunk_type(chunk, map->type);
5090 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5091 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5092 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
5093 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
5094 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5096 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5097 key.type = BTRFS_CHUNK_ITEM_KEY;
5098 key.offset = chunk_offset;
5100 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
5101 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5103 * TODO: Cleanup of inserted chunk root in case of
5106 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
5111 free_extent_map(em);
5116 * Chunk allocation falls into two parts. The first part does work
5117 * that makes the new allocated chunk usable, but does not do any operation
5118 * that modifies the chunk tree. The second part does the work that
5119 * requires modifying the chunk tree. This division is important for the
5120 * bootstrap process of adding storage to a seed btrfs.
5122 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
5126 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5127 chunk_offset = find_next_chunk(trans->fs_info);
5128 return __btrfs_alloc_chunk(trans, chunk_offset, type);
5131 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
5133 struct btrfs_fs_info *fs_info = trans->fs_info;
5135 u64 sys_chunk_offset;
5139 chunk_offset = find_next_chunk(fs_info);
5140 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
5141 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
5145 sys_chunk_offset = find_next_chunk(fs_info);
5146 alloc_profile = btrfs_system_alloc_profile(fs_info);
5147 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
5151 static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5153 const int index = btrfs_bg_flags_to_raid_index(map->type);
5155 return btrfs_raid_array[index].tolerated_failures;
5158 int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
5160 struct extent_map *em;
5161 struct map_lookup *map;
5166 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
5170 map = em->map_lookup;
5171 for (i = 0; i < map->num_stripes; i++) {
5172 if (test_bit(BTRFS_DEV_STATE_MISSING,
5173 &map->stripes[i].dev->dev_state)) {
5177 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5178 &map->stripes[i].dev->dev_state)) {
5185 * If the number of missing devices is larger than max errors,
5186 * we can not write the data into that chunk successfully, so
5189 if (miss_ndevs > btrfs_chunk_max_errors(map))
5192 free_extent_map(em);
5196 void btrfs_mapping_tree_free(struct extent_map_tree *tree)
5198 struct extent_map *em;
5201 write_lock(&tree->lock);
5202 em = lookup_extent_mapping(tree, 0, (u64)-1);
5204 remove_extent_mapping(tree, em);
5205 write_unlock(&tree->lock);
5209 free_extent_map(em);
5210 /* once for the tree */
5211 free_extent_map(em);
5215 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5217 struct extent_map *em;
5218 struct map_lookup *map;
5221 em = btrfs_get_chunk_map(fs_info, logical, len);
5224 * We could return errors for these cases, but that could get
5225 * ugly and we'd probably do the same thing which is just not do
5226 * anything else and exit, so return 1 so the callers don't try
5227 * to use other copies.
5231 map = em->map_lookup;
5232 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK))
5233 ret = map->num_stripes;
5234 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5235 ret = map->sub_stripes;
5236 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5238 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5240 * There could be two corrupted data stripes, we need
5241 * to loop retry in order to rebuild the correct data.
5243 * Fail a stripe at a time on every retry except the
5244 * stripe under reconstruction.
5246 ret = map->num_stripes;
5249 free_extent_map(em);
5251 down_read(&fs_info->dev_replace.rwsem);
5252 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5253 fs_info->dev_replace.tgtdev)
5255 up_read(&fs_info->dev_replace.rwsem);
5260 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
5263 struct extent_map *em;
5264 struct map_lookup *map;
5265 unsigned long len = fs_info->sectorsize;
5267 em = btrfs_get_chunk_map(fs_info, logical, len);
5269 if (!WARN_ON(IS_ERR(em))) {
5270 map = em->map_lookup;
5271 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5272 len = map->stripe_len * nr_data_stripes(map);
5273 free_extent_map(em);
5278 int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5280 struct extent_map *em;
5281 struct map_lookup *map;
5284 em = btrfs_get_chunk_map(fs_info, logical, len);
5286 if(!WARN_ON(IS_ERR(em))) {
5287 map = em->map_lookup;
5288 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5290 free_extent_map(em);
5295 static int find_live_mirror(struct btrfs_fs_info *fs_info,
5296 struct map_lookup *map, int first,
5297 int dev_replace_is_ongoing)
5301 int preferred_mirror;
5303 struct btrfs_device *srcdev;
5306 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
5308 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5309 num_stripes = map->sub_stripes;
5311 num_stripes = map->num_stripes;
5313 preferred_mirror = first + current->pid % num_stripes;
5315 if (dev_replace_is_ongoing &&
5316 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5317 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5318 srcdev = fs_info->dev_replace.srcdev;
5323 * try to avoid the drive that is the source drive for a
5324 * dev-replace procedure, only choose it if no other non-missing
5325 * mirror is available
5327 for (tolerance = 0; tolerance < 2; tolerance++) {
5328 if (map->stripes[preferred_mirror].dev->bdev &&
5329 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5330 return preferred_mirror;
5331 for (i = first; i < first + num_stripes; i++) {
5332 if (map->stripes[i].dev->bdev &&
5333 (tolerance || map->stripes[i].dev != srcdev))
5338 /* we couldn't find one that doesn't fail. Just return something
5339 * and the io error handling code will clean up eventually
5341 return preferred_mirror;
5344 static inline int parity_smaller(u64 a, u64 b)
5349 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
5350 static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
5352 struct btrfs_bio_stripe s;
5359 for (i = 0; i < num_stripes - 1; i++) {
5360 if (parity_smaller(bbio->raid_map[i],
5361 bbio->raid_map[i+1])) {
5362 s = bbio->stripes[i];
5363 l = bbio->raid_map[i];
5364 bbio->stripes[i] = bbio->stripes[i+1];
5365 bbio->raid_map[i] = bbio->raid_map[i+1];
5366 bbio->stripes[i+1] = s;
5367 bbio->raid_map[i+1] = l;
5375 static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5377 struct btrfs_bio *bbio = kzalloc(
5378 /* the size of the btrfs_bio */
5379 sizeof(struct btrfs_bio) +
5380 /* plus the variable array for the stripes */
5381 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
5382 /* plus the variable array for the tgt dev */
5383 sizeof(int) * (real_stripes) +
5385 * plus the raid_map, which includes both the tgt dev
5388 sizeof(u64) * (total_stripes),
5389 GFP_NOFS|__GFP_NOFAIL);
5391 atomic_set(&bbio->error, 0);
5392 refcount_set(&bbio->refs, 1);
5397 void btrfs_get_bbio(struct btrfs_bio *bbio)
5399 WARN_ON(!refcount_read(&bbio->refs));
5400 refcount_inc(&bbio->refs);
5403 void btrfs_put_bbio(struct btrfs_bio *bbio)
5407 if (refcount_dec_and_test(&bbio->refs))
5411 /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5413 * Please note that, discard won't be sent to target device of device
5416 static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5417 u64 logical, u64 *length_ret,
5418 struct btrfs_bio **bbio_ret)
5420 struct extent_map *em;
5421 struct map_lookup *map;
5422 struct btrfs_bio *bbio;
5423 u64 length = *length_ret;
5427 u64 stripe_end_offset;
5434 u32 sub_stripes = 0;
5435 u64 stripes_per_dev = 0;
5436 u32 remaining_stripes = 0;
5437 u32 last_stripe = 0;
5441 /* discard always return a bbio */
5444 em = btrfs_get_chunk_map(fs_info, logical, length);
5448 map = em->map_lookup;
5449 /* we don't discard raid56 yet */
5450 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5455 offset = logical - em->start;
5456 length = min_t(u64, em->start + em->len - logical, length);
5457 *length_ret = length;
5459 stripe_len = map->stripe_len;
5461 * stripe_nr counts the total number of stripes we have to stride
5462 * to get to this block
5464 stripe_nr = div64_u64(offset, stripe_len);
5466 /* stripe_offset is the offset of this block in its stripe */
5467 stripe_offset = offset - stripe_nr * stripe_len;
5469 stripe_nr_end = round_up(offset + length, map->stripe_len);
5470 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
5471 stripe_cnt = stripe_nr_end - stripe_nr;
5472 stripe_end_offset = stripe_nr_end * map->stripe_len -
5475 * after this, stripe_nr is the number of stripes on this
5476 * device we have to walk to find the data, and stripe_index is
5477 * the number of our device in the stripe array
5481 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5482 BTRFS_BLOCK_GROUP_RAID10)) {
5483 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5486 sub_stripes = map->sub_stripes;
5488 factor = map->num_stripes / sub_stripes;
5489 num_stripes = min_t(u64, map->num_stripes,
5490 sub_stripes * stripe_cnt);
5491 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5492 stripe_index *= sub_stripes;
5493 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5494 &remaining_stripes);
5495 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5496 last_stripe *= sub_stripes;
5497 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK |
5498 BTRFS_BLOCK_GROUP_DUP)) {
5499 num_stripes = map->num_stripes;
5501 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5505 bbio = alloc_btrfs_bio(num_stripes, 0);
5511 for (i = 0; i < num_stripes; i++) {
5512 bbio->stripes[i].physical =
5513 map->stripes[stripe_index].physical +
5514 stripe_offset + stripe_nr * map->stripe_len;
5515 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5517 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5518 BTRFS_BLOCK_GROUP_RAID10)) {
5519 bbio->stripes[i].length = stripes_per_dev *
5522 if (i / sub_stripes < remaining_stripes)
5523 bbio->stripes[i].length +=
5527 * Special for the first stripe and
5530 * |-------|...|-------|
5534 if (i < sub_stripes)
5535 bbio->stripes[i].length -=
5538 if (stripe_index >= last_stripe &&
5539 stripe_index <= (last_stripe +
5541 bbio->stripes[i].length -=
5544 if (i == sub_stripes - 1)
5547 bbio->stripes[i].length = length;
5551 if (stripe_index == map->num_stripes) {
5558 bbio->map_type = map->type;
5559 bbio->num_stripes = num_stripes;
5561 free_extent_map(em);
5566 * In dev-replace case, for repair case (that's the only case where the mirror
5567 * is selected explicitly when calling btrfs_map_block), blocks left of the
5568 * left cursor can also be read from the target drive.
5570 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5572 * For READ, it also needs to be supported using the same mirror number.
5574 * If the requested block is not left of the left cursor, EIO is returned. This
5575 * can happen because btrfs_num_copies() returns one more in the dev-replace
5578 static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5579 u64 logical, u64 length,
5580 u64 srcdev_devid, int *mirror_num,
5583 struct btrfs_bio *bbio = NULL;
5585 int index_srcdev = 0;
5587 u64 physical_of_found = 0;
5591 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5592 logical, &length, &bbio, 0, 0);
5594 ASSERT(bbio == NULL);
5598 num_stripes = bbio->num_stripes;
5599 if (*mirror_num > num_stripes) {
5601 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5602 * that means that the requested area is not left of the left
5605 btrfs_put_bbio(bbio);
5610 * process the rest of the function using the mirror_num of the source
5611 * drive. Therefore look it up first. At the end, patch the device
5612 * pointer to the one of the target drive.
5614 for (i = 0; i < num_stripes; i++) {
5615 if (bbio->stripes[i].dev->devid != srcdev_devid)
5619 * In case of DUP, in order to keep it simple, only add the
5620 * mirror with the lowest physical address
5623 physical_of_found <= bbio->stripes[i].physical)
5628 physical_of_found = bbio->stripes[i].physical;
5631 btrfs_put_bbio(bbio);
5637 *mirror_num = index_srcdev + 1;
5638 *physical = physical_of_found;
5642 static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5643 struct btrfs_bio **bbio_ret,
5644 struct btrfs_dev_replace *dev_replace,
5645 int *num_stripes_ret, int *max_errors_ret)
5647 struct btrfs_bio *bbio = *bbio_ret;
5648 u64 srcdev_devid = dev_replace->srcdev->devid;
5649 int tgtdev_indexes = 0;
5650 int num_stripes = *num_stripes_ret;
5651 int max_errors = *max_errors_ret;
5654 if (op == BTRFS_MAP_WRITE) {
5655 int index_where_to_add;
5658 * duplicate the write operations while the dev replace
5659 * procedure is running. Since the copying of the old disk to
5660 * the new disk takes place at run time while the filesystem is
5661 * mounted writable, the regular write operations to the old
5662 * disk have to be duplicated to go to the new disk as well.
5664 * Note that device->missing is handled by the caller, and that
5665 * the write to the old disk is already set up in the stripes
5668 index_where_to_add = num_stripes;
5669 for (i = 0; i < num_stripes; i++) {
5670 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5671 /* write to new disk, too */
5672 struct btrfs_bio_stripe *new =
5673 bbio->stripes + index_where_to_add;
5674 struct btrfs_bio_stripe *old =
5677 new->physical = old->physical;
5678 new->length = old->length;
5679 new->dev = dev_replace->tgtdev;
5680 bbio->tgtdev_map[i] = index_where_to_add;
5681 index_where_to_add++;
5686 num_stripes = index_where_to_add;
5687 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5688 int index_srcdev = 0;
5690 u64 physical_of_found = 0;
5693 * During the dev-replace procedure, the target drive can also
5694 * be used to read data in case it is needed to repair a corrupt
5695 * block elsewhere. This is possible if the requested area is
5696 * left of the left cursor. In this area, the target drive is a
5697 * full copy of the source drive.
5699 for (i = 0; i < num_stripes; i++) {
5700 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5702 * In case of DUP, in order to keep it simple,
5703 * only add the mirror with the lowest physical
5707 physical_of_found <=
5708 bbio->stripes[i].physical)
5712 physical_of_found = bbio->stripes[i].physical;
5716 struct btrfs_bio_stripe *tgtdev_stripe =
5717 bbio->stripes + num_stripes;
5719 tgtdev_stripe->physical = physical_of_found;
5720 tgtdev_stripe->length =
5721 bbio->stripes[index_srcdev].length;
5722 tgtdev_stripe->dev = dev_replace->tgtdev;
5723 bbio->tgtdev_map[index_srcdev] = num_stripes;
5730 *num_stripes_ret = num_stripes;
5731 *max_errors_ret = max_errors;
5732 bbio->num_tgtdevs = tgtdev_indexes;
5736 static bool need_full_stripe(enum btrfs_map_op op)
5738 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5742 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5743 * tuple. This information is used to calculate how big a
5744 * particular bio can get before it straddles a stripe.
5746 * @fs_info - the filesystem
5747 * @logical - address that we want to figure out the geometry of
5748 * @len - the length of IO we are going to perform, starting at @logical
5749 * @op - type of operation - write or read
5750 * @io_geom - pointer used to return values
5752 * Returns < 0 in case a chunk for the given logical address cannot be found,
5753 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5755 int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
5756 u64 logical, u64 len, struct btrfs_io_geometry *io_geom)
5758 struct extent_map *em;
5759 struct map_lookup *map;
5764 u64 raid56_full_stripe_start = (u64)-1;
5768 ASSERT(op != BTRFS_MAP_DISCARD);
5770 em = btrfs_get_chunk_map(fs_info, logical, len);
5774 map = em->map_lookup;
5775 /* Offset of this logical address in the chunk */
5776 offset = logical - em->start;
5777 /* Len of a stripe in a chunk */
5778 stripe_len = map->stripe_len;
5779 /* Stripe wher this block falls in */
5780 stripe_nr = div64_u64(offset, stripe_len);
5781 /* Offset of stripe in the chunk */
5782 stripe_offset = stripe_nr * stripe_len;
5783 if (offset < stripe_offset) {
5785 "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5786 stripe_offset, offset, em->start, logical, stripe_len);
5791 /* stripe_offset is the offset of this block in its stripe */
5792 stripe_offset = offset - stripe_offset;
5793 data_stripes = nr_data_stripes(map);
5795 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5796 u64 max_len = stripe_len - stripe_offset;
5799 * In case of raid56, we need to know the stripe aligned start
5801 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5802 unsigned long full_stripe_len = stripe_len * data_stripes;
5803 raid56_full_stripe_start = offset;
5806 * Allow a write of a full stripe, but make sure we
5807 * don't allow straddling of stripes
5809 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5811 raid56_full_stripe_start *= full_stripe_len;
5814 * For writes to RAID[56], allow a full stripeset across
5815 * all disks. For other RAID types and for RAID[56]
5816 * reads, just allow a single stripe (on a single disk).
5818 if (op == BTRFS_MAP_WRITE) {
5819 max_len = stripe_len * data_stripes -
5820 (offset - raid56_full_stripe_start);
5823 len = min_t(u64, em->len - offset, max_len);
5825 len = em->len - offset;
5829 io_geom->offset = offset;
5830 io_geom->stripe_len = stripe_len;
5831 io_geom->stripe_nr = stripe_nr;
5832 io_geom->stripe_offset = stripe_offset;
5833 io_geom->raid56_stripe_offset = raid56_full_stripe_start;
5837 free_extent_map(em);
5841 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5842 enum btrfs_map_op op,
5843 u64 logical, u64 *length,
5844 struct btrfs_bio **bbio_ret,
5845 int mirror_num, int need_raid_map)
5847 struct extent_map *em;
5848 struct map_lookup *map;
5858 int tgtdev_indexes = 0;
5859 struct btrfs_bio *bbio = NULL;
5860 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5861 int dev_replace_is_ongoing = 0;
5862 int num_alloc_stripes;
5863 int patch_the_first_stripe_for_dev_replace = 0;
5864 u64 physical_to_patch_in_first_stripe = 0;
5865 u64 raid56_full_stripe_start = (u64)-1;
5866 struct btrfs_io_geometry geom;
5870 if (op == BTRFS_MAP_DISCARD)
5871 return __btrfs_map_block_for_discard(fs_info, logical,
5874 ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom);
5878 em = btrfs_get_chunk_map(fs_info, logical, *length);
5879 ASSERT(!IS_ERR(em));
5880 map = em->map_lookup;
5883 stripe_len = geom.stripe_len;
5884 stripe_nr = geom.stripe_nr;
5885 stripe_offset = geom.stripe_offset;
5886 raid56_full_stripe_start = geom.raid56_stripe_offset;
5887 data_stripes = nr_data_stripes(map);
5889 down_read(&dev_replace->rwsem);
5890 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5892 * Hold the semaphore for read during the whole operation, write is
5893 * requested at commit time but must wait.
5895 if (!dev_replace_is_ongoing)
5896 up_read(&dev_replace->rwsem);
5898 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5899 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
5900 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5901 dev_replace->srcdev->devid,
5903 &physical_to_patch_in_first_stripe);
5907 patch_the_first_stripe_for_dev_replace = 1;
5908 } else if (mirror_num > map->num_stripes) {
5914 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5915 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5917 if (!need_full_stripe(op))
5919 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
5920 if (need_full_stripe(op))
5921 num_stripes = map->num_stripes;
5922 else if (mirror_num)
5923 stripe_index = mirror_num - 1;
5925 stripe_index = find_live_mirror(fs_info, map, 0,
5926 dev_replace_is_ongoing);
5927 mirror_num = stripe_index + 1;
5930 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
5931 if (need_full_stripe(op)) {
5932 num_stripes = map->num_stripes;
5933 } else if (mirror_num) {
5934 stripe_index = mirror_num - 1;
5939 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5940 u32 factor = map->num_stripes / map->sub_stripes;
5942 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5943 stripe_index *= map->sub_stripes;
5945 if (need_full_stripe(op))
5946 num_stripes = map->sub_stripes;
5947 else if (mirror_num)
5948 stripe_index += mirror_num - 1;
5950 int old_stripe_index = stripe_index;
5951 stripe_index = find_live_mirror(fs_info, map,
5953 dev_replace_is_ongoing);
5954 mirror_num = stripe_index - old_stripe_index + 1;
5957 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5958 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
5959 /* push stripe_nr back to the start of the full stripe */
5960 stripe_nr = div64_u64(raid56_full_stripe_start,
5961 stripe_len * data_stripes);
5963 /* RAID[56] write or recovery. Return all stripes */
5964 num_stripes = map->num_stripes;
5965 max_errors = nr_parity_stripes(map);
5967 *length = map->stripe_len;
5972 * Mirror #0 or #1 means the original data block.
5973 * Mirror #2 is RAID5 parity block.
5974 * Mirror #3 is RAID6 Q block.
5976 stripe_nr = div_u64_rem(stripe_nr,
5977 data_stripes, &stripe_index);
5979 stripe_index = data_stripes + mirror_num - 2;
5981 /* We distribute the parity blocks across stripes */
5982 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5984 if (!need_full_stripe(op) && mirror_num <= 1)
5989 * after this, stripe_nr is the number of stripes on this
5990 * device we have to walk to find the data, and stripe_index is
5991 * the number of our device in the stripe array
5993 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5995 mirror_num = stripe_index + 1;
5997 if (stripe_index >= map->num_stripes) {
5999 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
6000 stripe_index, map->num_stripes);
6005 num_alloc_stripes = num_stripes;
6006 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
6007 if (op == BTRFS_MAP_WRITE)
6008 num_alloc_stripes <<= 1;
6009 if (op == BTRFS_MAP_GET_READ_MIRRORS)
6010 num_alloc_stripes++;
6011 tgtdev_indexes = num_stripes;
6014 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
6019 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
6020 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
6022 /* build raid_map */
6023 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6024 (need_full_stripe(op) || mirror_num > 1)) {
6028 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6029 sizeof(struct btrfs_bio_stripe) *
6031 sizeof(int) * tgtdev_indexes);
6033 /* Work out the disk rotation on this stripe-set */
6034 div_u64_rem(stripe_nr, num_stripes, &rot);
6036 /* Fill in the logical address of each stripe */
6037 tmp = stripe_nr * data_stripes;
6038 for (i = 0; i < data_stripes; i++)
6039 bbio->raid_map[(i+rot) % num_stripes] =
6040 em->start + (tmp + i) * map->stripe_len;
6042 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6043 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6044 bbio->raid_map[(i+rot+1) % num_stripes] =
6049 for (i = 0; i < num_stripes; i++) {
6050 bbio->stripes[i].physical =
6051 map->stripes[stripe_index].physical +
6053 stripe_nr * map->stripe_len;
6054 bbio->stripes[i].dev =
6055 map->stripes[stripe_index].dev;
6059 if (need_full_stripe(op))
6060 max_errors = btrfs_chunk_max_errors(map);
6063 sort_parity_stripes(bbio, num_stripes);
6065 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
6066 need_full_stripe(op)) {
6067 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6072 bbio->map_type = map->type;
6073 bbio->num_stripes = num_stripes;
6074 bbio->max_errors = max_errors;
6075 bbio->mirror_num = mirror_num;
6078 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6079 * mirror_num == num_stripes + 1 && dev_replace target drive is
6080 * available as a mirror
6082 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6083 WARN_ON(num_stripes > 1);
6084 bbio->stripes[0].dev = dev_replace->tgtdev;
6085 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6086 bbio->mirror_num = map->num_stripes + 1;
6089 if (dev_replace_is_ongoing) {
6090 lockdep_assert_held(&dev_replace->rwsem);
6091 /* Unlock and let waiting writers proceed */
6092 up_read(&dev_replace->rwsem);
6094 free_extent_map(em);
6098 int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6099 u64 logical, u64 *length,
6100 struct btrfs_bio **bbio_ret, int mirror_num)
6102 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
6106 /* For Scrub/replace */
6107 int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
6108 u64 logical, u64 *length,
6109 struct btrfs_bio **bbio_ret)
6111 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
6114 int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6115 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
6117 struct extent_map *em;
6118 struct map_lookup *map;
6126 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
6130 map = em->map_lookup;
6132 rmap_len = map->stripe_len;
6134 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
6135 length = div_u64(length, map->num_stripes / map->sub_stripes);
6136 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
6137 length = div_u64(length, map->num_stripes);
6138 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6139 length = div_u64(length, nr_data_stripes(map));
6140 rmap_len = map->stripe_len * nr_data_stripes(map);
6143 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
6144 BUG_ON(!buf); /* -ENOMEM */
6146 for (i = 0; i < map->num_stripes; i++) {
6147 if (map->stripes[i].physical > physical ||
6148 map->stripes[i].physical + length <= physical)
6151 stripe_nr = physical - map->stripes[i].physical;
6152 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
6154 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6155 stripe_nr = stripe_nr * map->num_stripes + i;
6156 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
6157 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6158 stripe_nr = stripe_nr * map->num_stripes + i;
6159 } /* else if RAID[56], multiply by nr_data_stripes().
6160 * Alternatively, just use rmap_len below instead of
6161 * map->stripe_len */
6163 bytenr = chunk_start + stripe_nr * rmap_len;
6164 WARN_ON(nr >= map->num_stripes);
6165 for (j = 0; j < nr; j++) {
6166 if (buf[j] == bytenr)
6170 WARN_ON(nr >= map->num_stripes);
6177 *stripe_len = rmap_len;
6179 free_extent_map(em);
6183 static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
6185 bio->bi_private = bbio->private;
6186 bio->bi_end_io = bbio->end_io;
6189 btrfs_put_bbio(bbio);
6192 static void btrfs_end_bio(struct bio *bio)
6194 struct btrfs_bio *bbio = bio->bi_private;
6195 int is_orig_bio = 0;
6197 if (bio->bi_status) {
6198 atomic_inc(&bbio->error);
6199 if (bio->bi_status == BLK_STS_IOERR ||
6200 bio->bi_status == BLK_STS_TARGET) {
6201 unsigned int stripe_index =
6202 btrfs_io_bio(bio)->stripe_index;
6203 struct btrfs_device *dev;
6205 BUG_ON(stripe_index >= bbio->num_stripes);
6206 dev = bbio->stripes[stripe_index].dev;
6208 if (bio_op(bio) == REQ_OP_WRITE)
6209 btrfs_dev_stat_inc_and_print(dev,
6210 BTRFS_DEV_STAT_WRITE_ERRS);
6211 else if (!(bio->bi_opf & REQ_RAHEAD))
6212 btrfs_dev_stat_inc_and_print(dev,
6213 BTRFS_DEV_STAT_READ_ERRS);
6214 if (bio->bi_opf & REQ_PREFLUSH)
6215 btrfs_dev_stat_inc_and_print(dev,
6216 BTRFS_DEV_STAT_FLUSH_ERRS);
6221 if (bio == bbio->orig_bio)
6224 btrfs_bio_counter_dec(bbio->fs_info);
6226 if (atomic_dec_and_test(&bbio->stripes_pending)) {
6229 bio = bbio->orig_bio;
6232 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
6233 /* only send an error to the higher layers if it is
6234 * beyond the tolerance of the btrfs bio
6236 if (atomic_read(&bbio->error) > bbio->max_errors) {
6237 bio->bi_status = BLK_STS_IOERR;
6240 * this bio is actually up to date, we didn't
6241 * go over the max number of errors
6243 bio->bi_status = BLK_STS_OK;
6246 btrfs_end_bbio(bbio, bio);
6247 } else if (!is_orig_bio) {
6252 static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6253 u64 physical, int dev_nr)
6255 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
6256 struct btrfs_fs_info *fs_info = bbio->fs_info;
6258 bio->bi_private = bbio;
6259 btrfs_io_bio(bio)->stripe_index = dev_nr;
6260 bio->bi_end_io = btrfs_end_bio;
6261 bio->bi_iter.bi_sector = physical >> 9;
6262 btrfs_debug_in_rcu(fs_info,
6263 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6264 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6265 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6266 bio->bi_iter.bi_size);
6267 bio_set_dev(bio, dev->bdev);
6269 btrfs_bio_counter_inc_noblocked(fs_info);
6271 btrfsic_submit_bio(bio);
6274 static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6276 atomic_inc(&bbio->error);
6277 if (atomic_dec_and_test(&bbio->stripes_pending)) {
6278 /* Should be the original bio. */
6279 WARN_ON(bio != bbio->orig_bio);
6281 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
6282 bio->bi_iter.bi_sector = logical >> 9;
6283 if (atomic_read(&bbio->error) > bbio->max_errors)
6284 bio->bi_status = BLK_STS_IOERR;
6286 bio->bi_status = BLK_STS_OK;
6287 btrfs_end_bbio(bbio, bio);
6291 blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6294 struct btrfs_device *dev;
6295 struct bio *first_bio = bio;
6296 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
6302 struct btrfs_bio *bbio = NULL;
6304 length = bio->bi_iter.bi_size;
6305 map_length = length;
6307 btrfs_bio_counter_inc_blocked(fs_info);
6308 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
6309 &map_length, &bbio, mirror_num, 1);
6311 btrfs_bio_counter_dec(fs_info);
6312 return errno_to_blk_status(ret);
6315 total_devs = bbio->num_stripes;
6316 bbio->orig_bio = first_bio;
6317 bbio->private = first_bio->bi_private;
6318 bbio->end_io = first_bio->bi_end_io;
6319 bbio->fs_info = fs_info;
6320 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6322 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
6323 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
6324 /* In this case, map_length has been set to the length of
6325 a single stripe; not the whole write */
6326 if (bio_op(bio) == REQ_OP_WRITE) {
6327 ret = raid56_parity_write(fs_info, bio, bbio,
6330 ret = raid56_parity_recover(fs_info, bio, bbio,
6331 map_length, mirror_num, 1);
6334 btrfs_bio_counter_dec(fs_info);
6335 return errno_to_blk_status(ret);
6338 if (map_length < length) {
6340 "mapping failed logical %llu bio len %llu len %llu",
6341 logical, length, map_length);
6345 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
6346 dev = bbio->stripes[dev_nr].dev;
6347 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6349 (bio_op(first_bio) == REQ_OP_WRITE &&
6350 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
6351 bbio_error(bbio, first_bio, logical);
6355 if (dev_nr < total_devs - 1)
6356 bio = btrfs_bio_clone(first_bio);
6360 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6363 btrfs_bio_counter_dec(fs_info);
6368 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6371 * If devid and uuid are both specified, the match must be exact, otherwise
6372 * only devid is used.
6374 * If @seed is true, traverse through the seed devices.
6376 struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
6377 u64 devid, u8 *uuid, u8 *fsid,
6380 struct btrfs_device *device;
6382 while (fs_devices) {
6384 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6385 list_for_each_entry(device, &fs_devices->devices,
6387 if (device->devid == devid &&
6388 (!uuid || memcmp(device->uuid, uuid,
6389 BTRFS_UUID_SIZE) == 0))
6394 fs_devices = fs_devices->seed;
6401 static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
6402 u64 devid, u8 *dev_uuid)
6404 struct btrfs_device *device;
6406 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6410 list_add(&device->dev_list, &fs_devices->devices);
6411 device->fs_devices = fs_devices;
6412 fs_devices->num_devices++;
6414 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
6415 fs_devices->missing_devices++;
6421 * btrfs_alloc_device - allocate struct btrfs_device
6422 * @fs_info: used only for generating a new devid, can be NULL if
6423 * devid is provided (i.e. @devid != NULL).
6424 * @devid: a pointer to devid for this device. If NULL a new devid
6426 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6429 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6430 * on error. Returned struct is not linked onto any lists and must be
6431 * destroyed with btrfs_free_device.
6433 struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6437 struct btrfs_device *dev;
6440 if (WARN_ON(!devid && !fs_info))
6441 return ERR_PTR(-EINVAL);
6443 dev = __alloc_device();
6452 ret = find_next_devid(fs_info, &tmp);
6454 btrfs_free_device(dev);
6455 return ERR_PTR(ret);
6461 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6463 generate_random_uuid(dev->uuid);
6468 static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
6469 u64 devid, u8 *uuid, bool error)
6472 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6475 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6479 static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6481 int index = btrfs_bg_flags_to_raid_index(type);
6482 int ncopies = btrfs_raid_array[index].ncopies;
6485 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
6486 case BTRFS_BLOCK_GROUP_RAID5:
6487 data_stripes = num_stripes - 1;
6489 case BTRFS_BLOCK_GROUP_RAID6:
6490 data_stripes = num_stripes - 2;
6493 data_stripes = num_stripes / ncopies;
6496 return div_u64(chunk_len, data_stripes);
6499 static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
6500 struct btrfs_chunk *chunk)
6502 struct btrfs_fs_info *fs_info = leaf->fs_info;
6503 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
6504 struct map_lookup *map;
6505 struct extent_map *em;
6509 u8 uuid[BTRFS_UUID_SIZE];
6514 logical = key->offset;
6515 length = btrfs_chunk_length(leaf, chunk);
6516 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6519 * Only need to verify chunk item if we're reading from sys chunk array,
6520 * as chunk item in tree block is already verified by tree-checker.
6522 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
6523 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
6528 read_lock(&map_tree->lock);
6529 em = lookup_extent_mapping(map_tree, logical, 1);
6530 read_unlock(&map_tree->lock);
6532 /* already mapped? */
6533 if (em && em->start <= logical && em->start + em->len > logical) {
6534 free_extent_map(em);
6537 free_extent_map(em);
6540 em = alloc_extent_map();
6543 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
6545 free_extent_map(em);
6549 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
6550 em->map_lookup = map;
6551 em->start = logical;
6554 em->block_start = 0;
6555 em->block_len = em->len;
6557 map->num_stripes = num_stripes;
6558 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6559 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6560 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6561 map->type = btrfs_chunk_type(leaf, chunk);
6562 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6563 map->verified_stripes = 0;
6564 em->orig_block_len = calc_stripe_length(map->type, em->len,
6566 for (i = 0; i < num_stripes; i++) {
6567 map->stripes[i].physical =
6568 btrfs_stripe_offset_nr(leaf, chunk, i);
6569 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
6570 read_extent_buffer(leaf, uuid, (unsigned long)
6571 btrfs_stripe_dev_uuid_nr(chunk, i),
6573 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
6574 devid, uuid, NULL, true);
6575 if (!map->stripes[i].dev &&
6576 !btrfs_test_opt(fs_info, DEGRADED)) {
6577 free_extent_map(em);
6578 btrfs_report_missing_device(fs_info, devid, uuid, true);
6581 if (!map->stripes[i].dev) {
6582 map->stripes[i].dev =
6583 add_missing_dev(fs_info->fs_devices, devid,
6585 if (IS_ERR(map->stripes[i].dev)) {
6586 free_extent_map(em);
6588 "failed to init missing dev %llu: %ld",
6589 devid, PTR_ERR(map->stripes[i].dev));
6590 return PTR_ERR(map->stripes[i].dev);
6592 btrfs_report_missing_device(fs_info, devid, uuid, false);
6594 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6595 &(map->stripes[i].dev->dev_state));
6599 write_lock(&map_tree->lock);
6600 ret = add_extent_mapping(map_tree, em, 0);
6601 write_unlock(&map_tree->lock);
6604 "failed to add chunk map, start=%llu len=%llu: %d",
6605 em->start, em->len, ret);
6607 free_extent_map(em);
6612 static void fill_device_from_item(struct extent_buffer *leaf,
6613 struct btrfs_dev_item *dev_item,
6614 struct btrfs_device *device)
6618 device->devid = btrfs_device_id(leaf, dev_item);
6619 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6620 device->total_bytes = device->disk_total_bytes;
6621 device->commit_total_bytes = device->disk_total_bytes;
6622 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
6623 device->commit_bytes_used = device->bytes_used;
6624 device->type = btrfs_device_type(leaf, dev_item);
6625 device->io_align = btrfs_device_io_align(leaf, dev_item);
6626 device->io_width = btrfs_device_io_width(leaf, dev_item);
6627 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
6628 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
6629 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
6631 ptr = btrfs_device_uuid(dev_item);
6632 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
6635 static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
6638 struct btrfs_fs_devices *fs_devices;
6641 lockdep_assert_held(&uuid_mutex);
6644 fs_devices = fs_info->fs_devices->seed;
6645 while (fs_devices) {
6646 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
6649 fs_devices = fs_devices->seed;
6652 fs_devices = find_fsid(fsid, NULL);
6654 if (!btrfs_test_opt(fs_info, DEGRADED))
6655 return ERR_PTR(-ENOENT);
6657 fs_devices = alloc_fs_devices(fsid, NULL);
6658 if (IS_ERR(fs_devices))
6661 fs_devices->seeding = true;
6662 fs_devices->opened = 1;
6666 fs_devices = clone_fs_devices(fs_devices);
6667 if (IS_ERR(fs_devices))
6670 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
6672 free_fs_devices(fs_devices);
6673 fs_devices = ERR_PTR(ret);
6677 if (!fs_devices->seeding) {
6678 close_fs_devices(fs_devices);
6679 free_fs_devices(fs_devices);
6680 fs_devices = ERR_PTR(-EINVAL);
6684 fs_devices->seed = fs_info->fs_devices->seed;
6685 fs_info->fs_devices->seed = fs_devices;
6690 static int read_one_dev(struct extent_buffer *leaf,
6691 struct btrfs_dev_item *dev_item)
6693 struct btrfs_fs_info *fs_info = leaf->fs_info;
6694 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6695 struct btrfs_device *device;
6698 u8 fs_uuid[BTRFS_FSID_SIZE];
6699 u8 dev_uuid[BTRFS_UUID_SIZE];
6701 devid = btrfs_device_id(leaf, dev_item);
6702 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
6704 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
6707 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
6708 fs_devices = open_seed_devices(fs_info, fs_uuid);
6709 if (IS_ERR(fs_devices))
6710 return PTR_ERR(fs_devices);
6713 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
6716 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6717 btrfs_report_missing_device(fs_info, devid,
6722 device = add_missing_dev(fs_devices, devid, dev_uuid);
6723 if (IS_ERR(device)) {
6725 "failed to add missing dev %llu: %ld",
6726 devid, PTR_ERR(device));
6727 return PTR_ERR(device);
6729 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
6731 if (!device->bdev) {
6732 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6733 btrfs_report_missing_device(fs_info,
6734 devid, dev_uuid, true);
6737 btrfs_report_missing_device(fs_info, devid,
6741 if (!device->bdev &&
6742 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
6744 * this happens when a device that was properly setup
6745 * in the device info lists suddenly goes bad.
6746 * device->bdev is NULL, and so we have to set
6747 * device->missing to one here
6749 device->fs_devices->missing_devices++;
6750 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
6753 /* Move the device to its own fs_devices */
6754 if (device->fs_devices != fs_devices) {
6755 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6756 &device->dev_state));
6758 list_move(&device->dev_list, &fs_devices->devices);
6759 device->fs_devices->num_devices--;
6760 fs_devices->num_devices++;
6762 device->fs_devices->missing_devices--;
6763 fs_devices->missing_devices++;
6765 device->fs_devices = fs_devices;
6769 if (device->fs_devices != fs_info->fs_devices) {
6770 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
6771 if (device->generation !=
6772 btrfs_device_generation(leaf, dev_item))
6776 fill_device_from_item(leaf, dev_item, device);
6777 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
6778 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
6779 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
6780 device->fs_devices->total_rw_bytes += device->total_bytes;
6781 atomic64_add(device->total_bytes - device->bytes_used,
6782 &fs_info->free_chunk_space);
6788 int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
6790 struct btrfs_root *root = fs_info->tree_root;
6791 struct btrfs_super_block *super_copy = fs_info->super_copy;
6792 struct extent_buffer *sb;
6793 struct btrfs_disk_key *disk_key;
6794 struct btrfs_chunk *chunk;
6796 unsigned long sb_array_offset;
6803 struct btrfs_key key;
6805 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
6807 * This will create extent buffer of nodesize, superblock size is
6808 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6809 * overallocate but we can keep it as-is, only the first page is used.
6811 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
6814 set_extent_buffer_uptodate(sb);
6815 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
6817 * The sb extent buffer is artificial and just used to read the system array.
6818 * set_extent_buffer_uptodate() call does not properly mark all it's
6819 * pages up-to-date when the page is larger: extent does not cover the
6820 * whole page and consequently check_page_uptodate does not find all
6821 * the page's extents up-to-date (the hole beyond sb),
6822 * write_extent_buffer then triggers a WARN_ON.
6824 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6825 * but sb spans only this function. Add an explicit SetPageUptodate call
6826 * to silence the warning eg. on PowerPC 64.
6828 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
6829 SetPageUptodate(sb->pages[0]);
6831 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
6832 array_size = btrfs_super_sys_array_size(super_copy);
6834 array_ptr = super_copy->sys_chunk_array;
6835 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6838 while (cur_offset < array_size) {
6839 disk_key = (struct btrfs_disk_key *)array_ptr;
6840 len = sizeof(*disk_key);
6841 if (cur_offset + len > array_size)
6842 goto out_short_read;
6844 btrfs_disk_key_to_cpu(&key, disk_key);
6847 sb_array_offset += len;
6850 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
6852 "unexpected item type %u in sys_array at offset %u",
6853 (u32)key.type, cur_offset);
6858 chunk = (struct btrfs_chunk *)sb_array_offset;
6860 * At least one btrfs_chunk with one stripe must be present,
6861 * exact stripe count check comes afterwards
6863 len = btrfs_chunk_item_size(1);
6864 if (cur_offset + len > array_size)
6865 goto out_short_read;
6867 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6870 "invalid number of stripes %u in sys_array at offset %u",
6871 num_stripes, cur_offset);
6876 type = btrfs_chunk_type(sb, chunk);
6877 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
6879 "invalid chunk type %llu in sys_array at offset %u",
6885 len = btrfs_chunk_item_size(num_stripes);
6886 if (cur_offset + len > array_size)
6887 goto out_short_read;
6889 ret = read_one_chunk(&key, sb, chunk);
6894 sb_array_offset += len;
6897 clear_extent_buffer_uptodate(sb);
6898 free_extent_buffer_stale(sb);
6902 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
6904 clear_extent_buffer_uptodate(sb);
6905 free_extent_buffer_stale(sb);
6910 * Check if all chunks in the fs are OK for read-write degraded mount
6912 * If the @failing_dev is specified, it's accounted as missing.
6914 * Return true if all chunks meet the minimal RW mount requirements.
6915 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6917 bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6918 struct btrfs_device *failing_dev)
6920 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
6921 struct extent_map *em;
6925 read_lock(&map_tree->lock);
6926 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
6927 read_unlock(&map_tree->lock);
6928 /* No chunk at all? Return false anyway */
6934 struct map_lookup *map;
6939 map = em->map_lookup;
6941 btrfs_get_num_tolerated_disk_barrier_failures(
6943 for (i = 0; i < map->num_stripes; i++) {
6944 struct btrfs_device *dev = map->stripes[i].dev;
6946 if (!dev || !dev->bdev ||
6947 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
6948 dev->last_flush_error)
6950 else if (failing_dev && failing_dev == dev)
6953 if (missing > max_tolerated) {
6956 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
6957 em->start, missing, max_tolerated);
6958 free_extent_map(em);
6962 next_start = extent_map_end(em);
6963 free_extent_map(em);
6965 read_lock(&map_tree->lock);
6966 em = lookup_extent_mapping(map_tree, next_start,
6967 (u64)(-1) - next_start);
6968 read_unlock(&map_tree->lock);
6974 int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
6976 struct btrfs_root *root = fs_info->chunk_root;
6977 struct btrfs_path *path;
6978 struct extent_buffer *leaf;
6979 struct btrfs_key key;
6980 struct btrfs_key found_key;
6985 path = btrfs_alloc_path();
6990 * uuid_mutex is needed only if we are mounting a sprout FS
6991 * otherwise we don't need it.
6993 mutex_lock(&uuid_mutex);
6994 mutex_lock(&fs_info->chunk_mutex);
6997 * Read all device items, and then all the chunk items. All
6998 * device items are found before any chunk item (their object id
6999 * is smaller than the lowest possible object id for a chunk
7000 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
7002 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7005 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7009 leaf = path->nodes[0];
7010 slot = path->slots[0];
7011 if (slot >= btrfs_header_nritems(leaf)) {
7012 ret = btrfs_next_leaf(root, path);
7019 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7020 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7021 struct btrfs_dev_item *dev_item;
7022 dev_item = btrfs_item_ptr(leaf, slot,
7023 struct btrfs_dev_item);
7024 ret = read_one_dev(leaf, dev_item);
7028 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7029 struct btrfs_chunk *chunk;
7030 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
7031 ret = read_one_chunk(&found_key, leaf, chunk);
7039 * After loading chunk tree, we've got all device information,
7040 * do another round of validation checks.
7042 if (total_dev != fs_info->fs_devices->total_devices) {
7044 "super_num_devices %llu mismatch with num_devices %llu found here",
7045 btrfs_super_num_devices(fs_info->super_copy),
7050 if (btrfs_super_total_bytes(fs_info->super_copy) <
7051 fs_info->fs_devices->total_rw_bytes) {
7053 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
7054 btrfs_super_total_bytes(fs_info->super_copy),
7055 fs_info->fs_devices->total_rw_bytes);
7061 mutex_unlock(&fs_info->chunk_mutex);
7062 mutex_unlock(&uuid_mutex);
7064 btrfs_free_path(path);
7068 void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7070 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7071 struct btrfs_device *device;
7073 while (fs_devices) {
7074 mutex_lock(&fs_devices->device_list_mutex);
7075 list_for_each_entry(device, &fs_devices->devices, dev_list)
7076 device->fs_info = fs_info;
7077 mutex_unlock(&fs_devices->device_list_mutex);
7079 fs_devices = fs_devices->seed;
7083 static u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
7084 const struct btrfs_dev_stats_item *ptr,
7089 read_extent_buffer(eb, &val,
7090 offsetof(struct btrfs_dev_stats_item, values) +
7091 ((unsigned long)ptr) + (index * sizeof(u64)),
7096 static void btrfs_set_dev_stats_value(struct extent_buffer *eb,
7097 struct btrfs_dev_stats_item *ptr,
7100 write_extent_buffer(eb, &val,
7101 offsetof(struct btrfs_dev_stats_item, values) +
7102 ((unsigned long)ptr) + (index * sizeof(u64)),
7106 int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7108 struct btrfs_key key;
7109 struct btrfs_root *dev_root = fs_info->dev_root;
7110 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7111 struct extent_buffer *eb;
7114 struct btrfs_device *device;
7115 struct btrfs_path *path = NULL;
7118 path = btrfs_alloc_path();
7122 mutex_lock(&fs_devices->device_list_mutex);
7123 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7125 struct btrfs_dev_stats_item *ptr;
7127 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7128 key.type = BTRFS_PERSISTENT_ITEM_KEY;
7129 key.offset = device->devid;
7130 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7132 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7133 btrfs_dev_stat_set(device, i, 0);
7134 device->dev_stats_valid = 1;
7135 btrfs_release_path(path);
7138 slot = path->slots[0];
7139 eb = path->nodes[0];
7140 item_size = btrfs_item_size_nr(eb, slot);
7142 ptr = btrfs_item_ptr(eb, slot,
7143 struct btrfs_dev_stats_item);
7145 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7146 if (item_size >= (1 + i) * sizeof(__le64))
7147 btrfs_dev_stat_set(device, i,
7148 btrfs_dev_stats_value(eb, ptr, i));
7150 btrfs_dev_stat_set(device, i, 0);
7153 device->dev_stats_valid = 1;
7154 btrfs_dev_stat_print_on_load(device);
7155 btrfs_release_path(path);
7157 mutex_unlock(&fs_devices->device_list_mutex);
7159 btrfs_free_path(path);
7160 return ret < 0 ? ret : 0;
7163 static int update_dev_stat_item(struct btrfs_trans_handle *trans,
7164 struct btrfs_device *device)
7166 struct btrfs_fs_info *fs_info = trans->fs_info;
7167 struct btrfs_root *dev_root = fs_info->dev_root;
7168 struct btrfs_path *path;
7169 struct btrfs_key key;
7170 struct extent_buffer *eb;
7171 struct btrfs_dev_stats_item *ptr;
7175 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7176 key.type = BTRFS_PERSISTENT_ITEM_KEY;
7177 key.offset = device->devid;
7179 path = btrfs_alloc_path();
7182 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7184 btrfs_warn_in_rcu(fs_info,
7185 "error %d while searching for dev_stats item for device %s",
7186 ret, rcu_str_deref(device->name));
7191 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7192 /* need to delete old one and insert a new one */
7193 ret = btrfs_del_item(trans, dev_root, path);
7195 btrfs_warn_in_rcu(fs_info,
7196 "delete too small dev_stats item for device %s failed %d",
7197 rcu_str_deref(device->name), ret);
7204 /* need to insert a new item */
7205 btrfs_release_path(path);
7206 ret = btrfs_insert_empty_item(trans, dev_root, path,
7207 &key, sizeof(*ptr));
7209 btrfs_warn_in_rcu(fs_info,
7210 "insert dev_stats item for device %s failed %d",
7211 rcu_str_deref(device->name), ret);
7216 eb = path->nodes[0];
7217 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7218 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7219 btrfs_set_dev_stats_value(eb, ptr, i,
7220 btrfs_dev_stat_read(device, i));
7221 btrfs_mark_buffer_dirty(eb);
7224 btrfs_free_path(path);
7229 * called from commit_transaction. Writes all changed device stats to disk.
7231 int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
7233 struct btrfs_fs_info *fs_info = trans->fs_info;
7234 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7235 struct btrfs_device *device;
7239 mutex_lock(&fs_devices->device_list_mutex);
7240 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7241 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7242 if (!device->dev_stats_valid || stats_cnt == 0)
7247 * There is a LOAD-LOAD control dependency between the value of
7248 * dev_stats_ccnt and updating the on-disk values which requires
7249 * reading the in-memory counters. Such control dependencies
7250 * require explicit read memory barriers.
7252 * This memory barriers pairs with smp_mb__before_atomic in
7253 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7254 * barrier implied by atomic_xchg in
7255 * btrfs_dev_stats_read_and_reset
7259 ret = update_dev_stat_item(trans, device);
7261 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
7263 mutex_unlock(&fs_devices->device_list_mutex);
7268 void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7270 btrfs_dev_stat_inc(dev, index);
7271 btrfs_dev_stat_print_on_error(dev);
7274 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
7276 if (!dev->dev_stats_valid)
7278 btrfs_err_rl_in_rcu(dev->fs_info,
7279 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7280 rcu_str_deref(dev->name),
7281 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7282 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7283 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7284 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7285 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7288 static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7292 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7293 if (btrfs_dev_stat_read(dev, i) != 0)
7295 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7296 return; /* all values == 0, suppress message */
7298 btrfs_info_in_rcu(dev->fs_info,
7299 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7300 rcu_str_deref(dev->name),
7301 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7302 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7303 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7304 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7305 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7308 int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
7309 struct btrfs_ioctl_get_dev_stats *stats)
7311 struct btrfs_device *dev;
7312 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7315 mutex_lock(&fs_devices->device_list_mutex);
7316 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7318 mutex_unlock(&fs_devices->device_list_mutex);
7321 btrfs_warn(fs_info, "get dev_stats failed, device not found");
7323 } else if (!dev->dev_stats_valid) {
7324 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
7326 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
7327 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7328 if (stats->nr_items > i)
7330 btrfs_dev_stat_read_and_reset(dev, i);
7332 btrfs_dev_stat_set(dev, i, 0);
7335 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7336 if (stats->nr_items > i)
7337 stats->values[i] = btrfs_dev_stat_read(dev, i);
7339 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7340 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7344 void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
7346 struct buffer_head *bh;
7347 struct btrfs_super_block *disk_super;
7353 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7356 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7359 disk_super = (struct btrfs_super_block *)bh->b_data;
7361 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7362 set_buffer_dirty(bh);
7363 sync_dirty_buffer(bh);
7367 /* Notify udev that device has changed */
7368 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7370 /* Update ctime/mtime for device path for libblkid */
7371 update_dev_time(device_path);
7375 * Update the size and bytes used for each device where it changed. This is
7376 * delayed since we would otherwise get errors while writing out the
7379 * Must be invoked during transaction commit.
7381 void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
7383 struct btrfs_device *curr, *next;
7385 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
7387 if (list_empty(&trans->dev_update_list))
7391 * We don't need the device_list_mutex here. This list is owned by the
7392 * transaction and the transaction must complete before the device is
7395 mutex_lock(&trans->fs_info->chunk_mutex);
7396 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7398 list_del_init(&curr->post_commit_list);
7399 curr->commit_total_bytes = curr->disk_total_bytes;
7400 curr->commit_bytes_used = curr->bytes_used;
7402 mutex_unlock(&trans->fs_info->chunk_mutex);
7405 void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7407 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7408 while (fs_devices) {
7409 fs_devices->fs_info = fs_info;
7410 fs_devices = fs_devices->seed;
7414 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7416 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7417 while (fs_devices) {
7418 fs_devices->fs_info = NULL;
7419 fs_devices = fs_devices->seed;
7424 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7426 int btrfs_bg_type_to_factor(u64 flags)
7428 const int index = btrfs_bg_flags_to_raid_index(flags);
7430 return btrfs_raid_array[index].ncopies;
7435 static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7436 u64 chunk_offset, u64 devid,
7437 u64 physical_offset, u64 physical_len)
7439 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
7440 struct extent_map *em;
7441 struct map_lookup *map;
7442 struct btrfs_device *dev;
7448 read_lock(&em_tree->lock);
7449 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7450 read_unlock(&em_tree->lock);
7454 "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7455 physical_offset, devid);
7460 map = em->map_lookup;
7461 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7462 if (physical_len != stripe_len) {
7464 "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7465 physical_offset, devid, em->start, physical_len,
7471 for (i = 0; i < map->num_stripes; i++) {
7472 if (map->stripes[i].dev->devid == devid &&
7473 map->stripes[i].physical == physical_offset) {
7475 if (map->verified_stripes >= map->num_stripes) {
7477 "too many dev extents for chunk %llu found",
7482 map->verified_stripes++;
7488 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7489 physical_offset, devid);
7493 /* Make sure no dev extent is beyond device bondary */
7494 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
7496 btrfs_err(fs_info, "failed to find devid %llu", devid);
7501 /* It's possible this device is a dummy for seed device */
7502 if (dev->disk_total_bytes == 0) {
7503 dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7506 btrfs_err(fs_info, "failed to find seed devid %llu",
7513 if (physical_offset + physical_len > dev->disk_total_bytes) {
7515 "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7516 devid, physical_offset, physical_len,
7517 dev->disk_total_bytes);
7522 free_extent_map(em);
7526 static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7528 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
7529 struct extent_map *em;
7530 struct rb_node *node;
7533 read_lock(&em_tree->lock);
7534 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
7535 em = rb_entry(node, struct extent_map, rb_node);
7536 if (em->map_lookup->num_stripes !=
7537 em->map_lookup->verified_stripes) {
7539 "chunk %llu has missing dev extent, have %d expect %d",
7540 em->start, em->map_lookup->verified_stripes,
7541 em->map_lookup->num_stripes);
7547 read_unlock(&em_tree->lock);
7552 * Ensure that all dev extents are mapped to correct chunk, otherwise
7553 * later chunk allocation/free would cause unexpected behavior.
7555 * NOTE: This will iterate through the whole device tree, which should be of
7556 * the same size level as the chunk tree. This slightly increases mount time.
7558 int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7560 struct btrfs_path *path;
7561 struct btrfs_root *root = fs_info->dev_root;
7562 struct btrfs_key key;
7564 u64 prev_dev_ext_end = 0;
7568 key.type = BTRFS_DEV_EXTENT_KEY;
7571 path = btrfs_alloc_path();
7575 path->reada = READA_FORWARD;
7576 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7580 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7581 ret = btrfs_next_item(root, path);
7584 /* No dev extents at all? Not good */
7591 struct extent_buffer *leaf = path->nodes[0];
7592 struct btrfs_dev_extent *dext;
7593 int slot = path->slots[0];
7595 u64 physical_offset;
7599 btrfs_item_key_to_cpu(leaf, &key, slot);
7600 if (key.type != BTRFS_DEV_EXTENT_KEY)
7602 devid = key.objectid;
7603 physical_offset = key.offset;
7605 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7606 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7607 physical_len = btrfs_dev_extent_length(leaf, dext);
7609 /* Check if this dev extent overlaps with the previous one */
7610 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7612 "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7613 devid, physical_offset, prev_dev_ext_end);
7618 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7619 physical_offset, physical_len);
7623 prev_dev_ext_end = physical_offset + physical_len;
7625 ret = btrfs_next_item(root, path);
7634 /* Ensure all chunks have corresponding dev extents */
7635 ret = verify_chunk_dev_extent_mapping(fs_info);
7637 btrfs_free_path(path);
7642 * Check whether the given block group or device is pinned by any inode being
7643 * used as a swapfile.
7645 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7647 struct btrfs_swapfile_pin *sp;
7648 struct rb_node *node;
7650 spin_lock(&fs_info->swapfile_pins_lock);
7651 node = fs_info->swapfile_pins.rb_node;
7653 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7655 node = node->rb_left;
7656 else if (ptr > sp->ptr)
7657 node = node->rb_right;
7661 spin_unlock(&fs_info->swapfile_pins_lock);
7662 return node != NULL;