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/blkdev.h>
10 #include <linux/ratelimit.h>
11 #include <linux/kthread.h>
12 #include <linux/raid/pq.h>
13 #include <linux/semaphore.h>
14 #include <linux/uuid.h>
15 #include <linux/list_sort.h>
18 #include "extent_map.h"
20 #include "transaction.h"
21 #include "print-tree.h"
24 #include "async-thread.h"
25 #include "check-integrity.h"
26 #include "rcu-string.h"
27 #include "dev-replace.h"
29 #include "tree-checker.h"
30 #include "space-info.h"
31 #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,
70 .raid_name
= "raid1c3",
71 .bg_flag
= BTRFS_BLOCK_GROUP_RAID1C3
,
72 .mindev_error
= BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET
,
74 [BTRFS_RAID_RAID1C4
] = {
79 .tolerated_failures
= 3,
83 .raid_name
= "raid1c4",
84 .bg_flag
= BTRFS_BLOCK_GROUP_RAID1C4
,
85 .mindev_error
= BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET
,
92 .tolerated_failures
= 0,
97 .bg_flag
= BTRFS_BLOCK_GROUP_DUP
,
100 [BTRFS_RAID_RAID0
] = {
105 .tolerated_failures
= 0,
109 .raid_name
= "raid0",
110 .bg_flag
= BTRFS_BLOCK_GROUP_RAID0
,
113 [BTRFS_RAID_SINGLE
] = {
118 .tolerated_failures
= 0,
122 .raid_name
= "single",
126 [BTRFS_RAID_RAID5
] = {
131 .tolerated_failures
= 1,
135 .raid_name
= "raid5",
136 .bg_flag
= BTRFS_BLOCK_GROUP_RAID5
,
137 .mindev_error
= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET
,
139 [BTRFS_RAID_RAID6
] = {
144 .tolerated_failures
= 2,
148 .raid_name
= "raid6",
149 .bg_flag
= BTRFS_BLOCK_GROUP_RAID6
,
150 .mindev_error
= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET
,
154 const char *btrfs_bg_type_to_raid_name(u64 flags
)
156 const int index
= btrfs_bg_flags_to_raid_index(flags
);
158 if (index
>= BTRFS_NR_RAID_TYPES
)
161 return btrfs_raid_array
[index
].raid_name
;
165 * Fill @buf with textual description of @bg_flags, no more than @size_buf
166 * bytes including terminating null byte.
168 void btrfs_describe_block_groups(u64 bg_flags
, char *buf
, u32 size_buf
)
173 u64 flags
= bg_flags
;
174 u32 size_bp
= size_buf
;
181 #define DESCRIBE_FLAG(flag, desc) \
183 if (flags & (flag)) { \
184 ret = snprintf(bp, size_bp, "%s|", (desc)); \
185 if (ret < 0 || ret >= size_bp) \
193 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA
, "data");
194 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM
, "system");
195 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA
, "metadata");
197 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE
, "single");
198 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
199 DESCRIBE_FLAG(btrfs_raid_array
[i
].bg_flag
,
200 btrfs_raid_array
[i
].raid_name
);
204 ret
= snprintf(bp
, size_bp
, "0x%llx|", flags
);
208 if (size_bp
< size_buf
)
209 buf
[size_buf
- size_bp
- 1] = '\0'; /* remove last | */
212 * The text is trimmed, it's up to the caller to provide sufficiently
218 static int init_first_rw_device(struct btrfs_trans_handle
*trans
);
219 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info
*fs_info
);
220 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
);
221 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
222 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
,
223 enum btrfs_map_op op
,
224 u64 logical
, u64
*length
,
225 struct btrfs_bio
**bbio_ret
,
226 int mirror_num
, int need_raid_map
);
232 * There are several mutexes that protect manipulation of devices and low-level
233 * structures like chunks but not block groups, extents or files
235 * uuid_mutex (global lock)
236 * ------------------------
237 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
238 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
239 * device) or requested by the device= mount option
241 * the mutex can be very coarse and can cover long-running operations
243 * protects: updates to fs_devices counters like missing devices, rw devices,
244 * seeding, structure cloning, opening/closing devices at mount/umount time
246 * global::fs_devs - add, remove, updates to the global list
248 * does not protect: manipulation of the fs_devices::devices list!
250 * btrfs_device::name - renames (write side), read is RCU
252 * fs_devices::device_list_mutex (per-fs, with RCU)
253 * ------------------------------------------------
254 * protects updates to fs_devices::devices, ie. adding and deleting
256 * simple list traversal with read-only actions can be done with RCU protection
258 * may be used to exclude some operations from running concurrently without any
259 * modifications to the list (see write_all_supers)
263 * protects balance structures (status, state) and context accessed from
264 * several places (internally, ioctl)
268 * protects chunks, adding or removing during allocation, trim or when a new
269 * device is added/removed. Additionally it also protects post_commit_list of
270 * individual devices, since they can be added to the transaction's
271 * post_commit_list only with chunk_mutex held.
275 * a big lock that is held by the cleaner thread and prevents running subvolume
276 * cleaning together with relocation or delayed iputs
289 * Exclusive operations, BTRFS_FS_EXCL_OP
290 * ======================================
292 * Maintains the exclusivity of the following operations that apply to the
293 * whole filesystem and cannot run in parallel.
298 * - Device replace (*)
301 * The device operations (as above) can be in one of the following states:
307 * Only device operations marked with (*) can go into the Paused state for the
310 * - ioctl (only Balance can be Paused through ioctl)
311 * - filesystem remounted as read-only
312 * - filesystem unmounted and mounted as read-only
313 * - system power-cycle and filesystem mounted as read-only
314 * - filesystem or device errors leading to forced read-only
316 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
317 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
318 * A device operation in Paused or Running state can be canceled or resumed
319 * either by ioctl (Balance only) or when remounted as read-write.
320 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
324 DEFINE_MUTEX(uuid_mutex
);
325 static LIST_HEAD(fs_uuids
);
326 struct list_head
* __attribute_const__
btrfs_get_fs_uuids(void)
332 * alloc_fs_devices - allocate struct btrfs_fs_devices
333 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
334 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
336 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
337 * The returned struct is not linked onto any lists and can be destroyed with
338 * kfree() right away.
340 static struct btrfs_fs_devices
*alloc_fs_devices(const u8
*fsid
,
341 const u8
*metadata_fsid
)
343 struct btrfs_fs_devices
*fs_devs
;
345 fs_devs
= kzalloc(sizeof(*fs_devs
), GFP_KERNEL
);
347 return ERR_PTR(-ENOMEM
);
349 mutex_init(&fs_devs
->device_list_mutex
);
351 INIT_LIST_HEAD(&fs_devs
->devices
);
352 INIT_LIST_HEAD(&fs_devs
->alloc_list
);
353 INIT_LIST_HEAD(&fs_devs
->fs_list
);
355 memcpy(fs_devs
->fsid
, fsid
, BTRFS_FSID_SIZE
);
358 memcpy(fs_devs
->metadata_uuid
, metadata_fsid
, BTRFS_FSID_SIZE
);
360 memcpy(fs_devs
->metadata_uuid
, fsid
, BTRFS_FSID_SIZE
);
365 void btrfs_free_device(struct btrfs_device
*device
)
367 WARN_ON(!list_empty(&device
->post_commit_list
));
368 rcu_string_free(device
->name
);
369 extent_io_tree_release(&device
->alloc_state
);
370 bio_put(device
->flush_bio
);
374 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
376 struct btrfs_device
*device
;
377 WARN_ON(fs_devices
->opened
);
378 while (!list_empty(&fs_devices
->devices
)) {
379 device
= list_entry(fs_devices
->devices
.next
,
380 struct btrfs_device
, dev_list
);
381 list_del(&device
->dev_list
);
382 btrfs_free_device(device
);
387 void __exit
btrfs_cleanup_fs_uuids(void)
389 struct btrfs_fs_devices
*fs_devices
;
391 while (!list_empty(&fs_uuids
)) {
392 fs_devices
= list_entry(fs_uuids
.next
,
393 struct btrfs_fs_devices
, fs_list
);
394 list_del(&fs_devices
->fs_list
);
395 free_fs_devices(fs_devices
);
400 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
401 * Returned struct is not linked onto any lists and must be destroyed using
404 static struct btrfs_device
*__alloc_device(void)
406 struct btrfs_device
*dev
;
408 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
410 return ERR_PTR(-ENOMEM
);
413 * Preallocate a bio that's always going to be used for flushing device
414 * barriers and matches the device lifespan
416 dev
->flush_bio
= bio_alloc_bioset(GFP_KERNEL
, 0, NULL
);
417 if (!dev
->flush_bio
) {
419 return ERR_PTR(-ENOMEM
);
422 INIT_LIST_HEAD(&dev
->dev_list
);
423 INIT_LIST_HEAD(&dev
->dev_alloc_list
);
424 INIT_LIST_HEAD(&dev
->post_commit_list
);
426 atomic_set(&dev
->reada_in_flight
, 0);
427 atomic_set(&dev
->dev_stats_ccnt
, 0);
428 btrfs_device_data_ordered_init(dev
);
429 INIT_RADIX_TREE(&dev
->reada_zones
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
430 INIT_RADIX_TREE(&dev
->reada_extents
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
431 extent_io_tree_init(NULL
, &dev
->alloc_state
, 0, NULL
);
436 static noinline
struct btrfs_fs_devices
*find_fsid(
437 const u8
*fsid
, const u8
*metadata_fsid
)
439 struct btrfs_fs_devices
*fs_devices
;
443 /* Handle non-split brain cases */
444 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
446 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0
447 && memcmp(metadata_fsid
, fs_devices
->metadata_uuid
,
448 BTRFS_FSID_SIZE
) == 0)
451 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
458 static struct btrfs_fs_devices
*find_fsid_with_metadata_uuid(
459 struct btrfs_super_block
*disk_super
)
462 struct btrfs_fs_devices
*fs_devices
;
465 * Handle scanned device having completed its fsid change but
466 * belonging to a fs_devices that was created by first scanning
467 * a device which didn't have its fsid/metadata_uuid changed
468 * at all and the CHANGING_FSID_V2 flag set.
470 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
471 if (fs_devices
->fsid_change
&&
472 memcmp(disk_super
->metadata_uuid
, fs_devices
->fsid
,
473 BTRFS_FSID_SIZE
) == 0 &&
474 memcmp(fs_devices
->fsid
, fs_devices
->metadata_uuid
,
475 BTRFS_FSID_SIZE
) == 0) {
480 * Handle scanned device having completed its fsid change but
481 * belonging to a fs_devices that was created by a device that
482 * has an outdated pair of fsid/metadata_uuid and
483 * CHANGING_FSID_V2 flag set.
485 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
486 if (fs_devices
->fsid_change
&&
487 memcmp(fs_devices
->metadata_uuid
,
488 fs_devices
->fsid
, BTRFS_FSID_SIZE
) != 0 &&
489 memcmp(disk_super
->metadata_uuid
, fs_devices
->metadata_uuid
,
490 BTRFS_FSID_SIZE
) == 0) {
495 return find_fsid(disk_super
->fsid
, disk_super
->metadata_uuid
);
500 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
501 int flush
, struct block_device
**bdev
,
502 struct btrfs_super_block
**disk_super
)
506 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
509 ret
= PTR_ERR(*bdev
);
514 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
515 ret
= set_blocksize(*bdev
, BTRFS_BDEV_BLOCKSIZE
);
517 blkdev_put(*bdev
, flags
);
520 invalidate_bdev(*bdev
);
521 *disk_super
= btrfs_read_dev_super(*bdev
);
522 if (IS_ERR(*disk_super
)) {
523 ret
= PTR_ERR(*disk_super
);
524 blkdev_put(*bdev
, flags
);
535 static bool device_path_matched(const char *path
, struct btrfs_device
*device
)
540 found
= strcmp(rcu_str_deref(device
->name
), path
);
547 * Search and remove all stale (devices which are not mounted) devices.
548 * When both inputs are NULL, it will search and release all stale devices.
549 * path: Optional. When provided will it release all unmounted devices
550 * matching this path only.
551 * skip_dev: Optional. Will skip this device when searching for the stale
553 * Return: 0 for success or if @path is NULL.
554 * -EBUSY if @path is a mounted device.
555 * -ENOENT if @path does not match any device in the list.
557 static int btrfs_free_stale_devices(const char *path
,
558 struct btrfs_device
*skip_device
)
560 struct btrfs_fs_devices
*fs_devices
, *tmp_fs_devices
;
561 struct btrfs_device
*device
, *tmp_device
;
567 list_for_each_entry_safe(fs_devices
, tmp_fs_devices
, &fs_uuids
, fs_list
) {
569 mutex_lock(&fs_devices
->device_list_mutex
);
570 list_for_each_entry_safe(device
, tmp_device
,
571 &fs_devices
->devices
, dev_list
) {
572 if (skip_device
&& skip_device
== device
)
574 if (path
&& !device
->name
)
576 if (path
&& !device_path_matched(path
, device
))
578 if (fs_devices
->opened
) {
579 /* for an already deleted device return 0 */
580 if (path
&& ret
!= 0)
585 /* delete the stale device */
586 fs_devices
->num_devices
--;
587 list_del(&device
->dev_list
);
588 btrfs_free_device(device
);
591 if (fs_devices
->num_devices
== 0)
594 mutex_unlock(&fs_devices
->device_list_mutex
);
596 if (fs_devices
->num_devices
== 0) {
597 btrfs_sysfs_remove_fsid(fs_devices
);
598 list_del(&fs_devices
->fs_list
);
599 free_fs_devices(fs_devices
);
606 static int btrfs_open_one_device(struct btrfs_fs_devices
*fs_devices
,
607 struct btrfs_device
*device
, fmode_t flags
,
610 struct request_queue
*q
;
611 struct block_device
*bdev
;
612 struct btrfs_super_block
*disk_super
;
621 ret
= btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
626 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
627 if (devid
!= device
->devid
)
628 goto error_free_page
;
630 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
, BTRFS_UUID_SIZE
))
631 goto error_free_page
;
633 device
->generation
= btrfs_super_generation(disk_super
);
635 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
636 if (btrfs_super_incompat_flags(disk_super
) &
637 BTRFS_FEATURE_INCOMPAT_METADATA_UUID
) {
639 "BTRFS: Invalid seeding and uuid-changed device detected\n");
640 goto error_free_page
;
643 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
644 fs_devices
->seeding
= true;
646 if (bdev_read_only(bdev
))
647 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
649 set_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
652 q
= bdev_get_queue(bdev
);
653 if (!blk_queue_nonrot(q
))
654 fs_devices
->rotating
= true;
657 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
658 device
->mode
= flags
;
660 fs_devices
->open_devices
++;
661 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
662 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
663 fs_devices
->rw_devices
++;
664 list_add_tail(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
666 btrfs_release_disk_super(disk_super
);
671 btrfs_release_disk_super(disk_super
);
672 blkdev_put(bdev
, flags
);
678 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
679 * being created with a disk that has already completed its fsid change. Such
680 * disk can belong to an fs which has its FSID changed or to one which doesn't.
681 * Handle both cases here.
683 static struct btrfs_fs_devices
*find_fsid_inprogress(
684 struct btrfs_super_block
*disk_super
)
686 struct btrfs_fs_devices
*fs_devices
;
688 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
689 if (memcmp(fs_devices
->metadata_uuid
, fs_devices
->fsid
,
690 BTRFS_FSID_SIZE
) != 0 &&
691 memcmp(fs_devices
->metadata_uuid
, disk_super
->fsid
,
692 BTRFS_FSID_SIZE
) == 0 && !fs_devices
->fsid_change
) {
697 return find_fsid(disk_super
->fsid
, NULL
);
701 static struct btrfs_fs_devices
*find_fsid_changed(
702 struct btrfs_super_block
*disk_super
)
704 struct btrfs_fs_devices
*fs_devices
;
707 * Handles the case where scanned device is part of an fs that had
708 * multiple successful changes of FSID but curently device didn't
709 * observe it. Meaning our fsid will be different than theirs. We need
710 * to handle two subcases :
711 * 1 - The fs still continues to have different METADATA/FSID uuids.
712 * 2 - The fs is switched back to its original FSID (METADATA/FSID
715 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
717 if (memcmp(fs_devices
->metadata_uuid
, fs_devices
->fsid
,
718 BTRFS_FSID_SIZE
) != 0 &&
719 memcmp(fs_devices
->metadata_uuid
, disk_super
->metadata_uuid
,
720 BTRFS_FSID_SIZE
) == 0 &&
721 memcmp(fs_devices
->fsid
, disk_super
->fsid
,
722 BTRFS_FSID_SIZE
) != 0)
725 /* Unchanged UUIDs */
726 if (memcmp(fs_devices
->metadata_uuid
, fs_devices
->fsid
,
727 BTRFS_FSID_SIZE
) == 0 &&
728 memcmp(fs_devices
->fsid
, disk_super
->metadata_uuid
,
729 BTRFS_FSID_SIZE
) == 0)
736 static struct btrfs_fs_devices
*find_fsid_reverted_metadata(
737 struct btrfs_super_block
*disk_super
)
739 struct btrfs_fs_devices
*fs_devices
;
742 * Handle the case where the scanned device is part of an fs whose last
743 * metadata UUID change reverted it to the original FSID. At the same
744 * time * fs_devices was first created by another constitutent device
745 * which didn't fully observe the operation. This results in an
746 * btrfs_fs_devices created with metadata/fsid different AND
747 * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
748 * fs_devices equal to the FSID of the disk.
750 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
751 if (memcmp(fs_devices
->fsid
, fs_devices
->metadata_uuid
,
752 BTRFS_FSID_SIZE
) != 0 &&
753 memcmp(fs_devices
->metadata_uuid
, disk_super
->fsid
,
754 BTRFS_FSID_SIZE
) == 0 &&
755 fs_devices
->fsid_change
)
762 * Add new device to list of registered devices
765 * device pointer which was just added or updated when successful
766 * error pointer when failed
768 static noinline
struct btrfs_device
*device_list_add(const char *path
,
769 struct btrfs_super_block
*disk_super
,
770 bool *new_device_added
)
772 struct btrfs_device
*device
;
773 struct btrfs_fs_devices
*fs_devices
= NULL
;
774 struct rcu_string
*name
;
775 u64 found_transid
= btrfs_super_generation(disk_super
);
776 u64 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
777 bool has_metadata_uuid
= (btrfs_super_incompat_flags(disk_super
) &
778 BTRFS_FEATURE_INCOMPAT_METADATA_UUID
);
779 bool fsid_change_in_progress
= (btrfs_super_flags(disk_super
) &
780 BTRFS_SUPER_FLAG_CHANGING_FSID_V2
);
782 if (fsid_change_in_progress
) {
783 if (!has_metadata_uuid
)
784 fs_devices
= find_fsid_inprogress(disk_super
);
786 fs_devices
= find_fsid_changed(disk_super
);
787 } else if (has_metadata_uuid
) {
788 fs_devices
= find_fsid_with_metadata_uuid(disk_super
);
790 fs_devices
= find_fsid_reverted_metadata(disk_super
);
792 fs_devices
= find_fsid(disk_super
->fsid
, NULL
);
797 if (has_metadata_uuid
)
798 fs_devices
= alloc_fs_devices(disk_super
->fsid
,
799 disk_super
->metadata_uuid
);
801 fs_devices
= alloc_fs_devices(disk_super
->fsid
, NULL
);
803 if (IS_ERR(fs_devices
))
804 return ERR_CAST(fs_devices
);
806 fs_devices
->fsid_change
= fsid_change_in_progress
;
808 mutex_lock(&fs_devices
->device_list_mutex
);
809 list_add(&fs_devices
->fs_list
, &fs_uuids
);
813 mutex_lock(&fs_devices
->device_list_mutex
);
814 device
= btrfs_find_device(fs_devices
, devid
,
815 disk_super
->dev_item
.uuid
, NULL
, false);
818 * If this disk has been pulled into an fs devices created by
819 * a device which had the CHANGING_FSID_V2 flag then replace the
820 * metadata_uuid/fsid values of the fs_devices.
822 if (fs_devices
->fsid_change
&&
823 found_transid
> fs_devices
->latest_generation
) {
824 memcpy(fs_devices
->fsid
, disk_super
->fsid
,
827 if (has_metadata_uuid
)
828 memcpy(fs_devices
->metadata_uuid
,
829 disk_super
->metadata_uuid
,
832 memcpy(fs_devices
->metadata_uuid
,
833 disk_super
->fsid
, BTRFS_FSID_SIZE
);
835 fs_devices
->fsid_change
= false;
840 if (fs_devices
->opened
) {
841 mutex_unlock(&fs_devices
->device_list_mutex
);
842 return ERR_PTR(-EBUSY
);
845 device
= btrfs_alloc_device(NULL
, &devid
,
846 disk_super
->dev_item
.uuid
);
847 if (IS_ERR(device
)) {
848 mutex_unlock(&fs_devices
->device_list_mutex
);
849 /* we can safely leave the fs_devices entry around */
853 name
= rcu_string_strdup(path
, GFP_NOFS
);
855 btrfs_free_device(device
);
856 mutex_unlock(&fs_devices
->device_list_mutex
);
857 return ERR_PTR(-ENOMEM
);
859 rcu_assign_pointer(device
->name
, name
);
861 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
862 fs_devices
->num_devices
++;
864 device
->fs_devices
= fs_devices
;
865 *new_device_added
= true;
867 if (disk_super
->label
[0])
869 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
870 disk_super
->label
, devid
, found_transid
, path
,
871 current
->comm
, task_pid_nr(current
));
874 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
875 disk_super
->fsid
, devid
, found_transid
, path
,
876 current
->comm
, task_pid_nr(current
));
878 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
880 * When FS is already mounted.
881 * 1. If you are here and if the device->name is NULL that
882 * means this device was missing at time of FS mount.
883 * 2. If you are here and if the device->name is different
884 * from 'path' that means either
885 * a. The same device disappeared and reappeared with
887 * b. The missing-disk-which-was-replaced, has
890 * We must allow 1 and 2a above. But 2b would be a spurious
893 * Further in case of 1 and 2a above, the disk at 'path'
894 * would have missed some transaction when it was away and
895 * in case of 2a the stale bdev has to be updated as well.
896 * 2b must not be allowed at all time.
900 * For now, we do allow update to btrfs_fs_device through the
901 * btrfs dev scan cli after FS has been mounted. We're still
902 * tracking a problem where systems fail mount by subvolume id
903 * when we reject replacement on a mounted FS.
905 if (!fs_devices
->opened
&& found_transid
< device
->generation
) {
907 * That is if the FS is _not_ mounted and if you
908 * are here, that means there is more than one
909 * disk with same uuid and devid.We keep the one
910 * with larger generation number or the last-in if
911 * generation are equal.
913 mutex_unlock(&fs_devices
->device_list_mutex
);
914 return ERR_PTR(-EEXIST
);
918 * We are going to replace the device path for a given devid,
919 * make sure it's the same device if the device is mounted
922 struct block_device
*path_bdev
;
924 path_bdev
= lookup_bdev(path
);
925 if (IS_ERR(path_bdev
)) {
926 mutex_unlock(&fs_devices
->device_list_mutex
);
927 return ERR_CAST(path_bdev
);
930 if (device
->bdev
!= path_bdev
) {
932 mutex_unlock(&fs_devices
->device_list_mutex
);
933 btrfs_warn_in_rcu(device
->fs_info
,
934 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
935 disk_super
->fsid
, devid
,
936 rcu_str_deref(device
->name
), path
);
937 return ERR_PTR(-EEXIST
);
940 btrfs_info_in_rcu(device
->fs_info
,
941 "device fsid %pU devid %llu moved old:%s new:%s",
942 disk_super
->fsid
, devid
,
943 rcu_str_deref(device
->name
), path
);
946 name
= rcu_string_strdup(path
, GFP_NOFS
);
948 mutex_unlock(&fs_devices
->device_list_mutex
);
949 return ERR_PTR(-ENOMEM
);
951 rcu_string_free(device
->name
);
952 rcu_assign_pointer(device
->name
, name
);
953 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
)) {
954 fs_devices
->missing_devices
--;
955 clear_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
960 * Unmount does not free the btrfs_device struct but would zero
961 * generation along with most of the other members. So just update
962 * it back. We need it to pick the disk with largest generation
965 if (!fs_devices
->opened
) {
966 device
->generation
= found_transid
;
967 fs_devices
->latest_generation
= max_t(u64
, found_transid
,
968 fs_devices
->latest_generation
);
971 fs_devices
->total_devices
= btrfs_super_num_devices(disk_super
);
973 mutex_unlock(&fs_devices
->device_list_mutex
);
977 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
979 struct btrfs_fs_devices
*fs_devices
;
980 struct btrfs_device
*device
;
981 struct btrfs_device
*orig_dev
;
984 fs_devices
= alloc_fs_devices(orig
->fsid
, NULL
);
985 if (IS_ERR(fs_devices
))
988 mutex_lock(&orig
->device_list_mutex
);
989 fs_devices
->total_devices
= orig
->total_devices
;
991 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
992 struct rcu_string
*name
;
994 device
= btrfs_alloc_device(NULL
, &orig_dev
->devid
,
996 if (IS_ERR(device
)) {
997 ret
= PTR_ERR(device
);
1002 * This is ok to do without rcu read locked because we hold the
1003 * uuid mutex so nothing we touch in here is going to disappear.
1005 if (orig_dev
->name
) {
1006 name
= rcu_string_strdup(orig_dev
->name
->str
,
1009 btrfs_free_device(device
);
1013 rcu_assign_pointer(device
->name
, name
);
1016 list_add(&device
->dev_list
, &fs_devices
->devices
);
1017 device
->fs_devices
= fs_devices
;
1018 fs_devices
->num_devices
++;
1020 mutex_unlock(&orig
->device_list_mutex
);
1023 mutex_unlock(&orig
->device_list_mutex
);
1024 free_fs_devices(fs_devices
);
1025 return ERR_PTR(ret
);
1029 * After we have read the system tree and know devids belonging to
1030 * this filesystem, remove the device which does not belong there.
1032 void btrfs_free_extra_devids(struct btrfs_fs_devices
*fs_devices
, int step
)
1034 struct btrfs_device
*device
, *next
;
1035 struct btrfs_device
*latest_dev
= NULL
;
1037 mutex_lock(&uuid_mutex
);
1039 /* This is the initialized path, it is safe to release the devices. */
1040 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
1041 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
1042 &device
->dev_state
)) {
1043 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
1044 &device
->dev_state
) &&
1046 device
->generation
> latest_dev
->generation
)) {
1047 latest_dev
= device
;
1052 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
) {
1054 * In the first step, keep the device which has
1055 * the correct fsid and the devid that is used
1056 * for the dev_replace procedure.
1057 * In the second step, the dev_replace state is
1058 * read from the device tree and it is known
1059 * whether the procedure is really active or
1060 * not, which means whether this device is
1061 * used or whether it should be removed.
1063 if (step
== 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
1064 &device
->dev_state
)) {
1069 blkdev_put(device
->bdev
, device
->mode
);
1070 device
->bdev
= NULL
;
1071 fs_devices
->open_devices
--;
1073 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1074 list_del_init(&device
->dev_alloc_list
);
1075 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
1076 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
1077 &device
->dev_state
))
1078 fs_devices
->rw_devices
--;
1080 list_del_init(&device
->dev_list
);
1081 fs_devices
->num_devices
--;
1082 btrfs_free_device(device
);
1085 if (fs_devices
->seed
) {
1086 fs_devices
= fs_devices
->seed
;
1090 fs_devices
->latest_bdev
= latest_dev
->bdev
;
1092 mutex_unlock(&uuid_mutex
);
1095 static void btrfs_close_bdev(struct btrfs_device
*device
)
1100 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1101 sync_blockdev(device
->bdev
);
1102 invalidate_bdev(device
->bdev
);
1105 blkdev_put(device
->bdev
, device
->mode
);
1108 static void btrfs_close_one_device(struct btrfs_device
*device
)
1110 struct btrfs_fs_devices
*fs_devices
= device
->fs_devices
;
1112 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
1113 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
1114 list_del_init(&device
->dev_alloc_list
);
1115 fs_devices
->rw_devices
--;
1118 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
))
1119 fs_devices
->missing_devices
--;
1121 btrfs_close_bdev(device
);
1123 fs_devices
->open_devices
--;
1124 device
->bdev
= NULL
;
1126 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
1128 device
->fs_info
= NULL
;
1129 atomic_set(&device
->dev_stats_ccnt
, 0);
1130 extent_io_tree_release(&device
->alloc_state
);
1132 /* Verify the device is back in a pristine state */
1133 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT
, &device
->dev_state
));
1134 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
));
1135 ASSERT(list_empty(&device
->dev_alloc_list
));
1136 ASSERT(list_empty(&device
->post_commit_list
));
1137 ASSERT(atomic_read(&device
->reada_in_flight
) == 0);
1140 static int close_fs_devices(struct btrfs_fs_devices
*fs_devices
)
1142 struct btrfs_device
*device
, *tmp
;
1144 if (--fs_devices
->opened
> 0)
1147 mutex_lock(&fs_devices
->device_list_mutex
);
1148 list_for_each_entry_safe(device
, tmp
, &fs_devices
->devices
, dev_list
) {
1149 btrfs_close_one_device(device
);
1151 mutex_unlock(&fs_devices
->device_list_mutex
);
1153 WARN_ON(fs_devices
->open_devices
);
1154 WARN_ON(fs_devices
->rw_devices
);
1155 fs_devices
->opened
= 0;
1156 fs_devices
->seeding
= false;
1161 int btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
1163 struct btrfs_fs_devices
*seed_devices
= NULL
;
1166 mutex_lock(&uuid_mutex
);
1167 ret
= close_fs_devices(fs_devices
);
1168 if (!fs_devices
->opened
) {
1169 seed_devices
= fs_devices
->seed
;
1170 fs_devices
->seed
= NULL
;
1172 mutex_unlock(&uuid_mutex
);
1174 while (seed_devices
) {
1175 fs_devices
= seed_devices
;
1176 seed_devices
= fs_devices
->seed
;
1177 close_fs_devices(fs_devices
);
1178 free_fs_devices(fs_devices
);
1183 static int open_fs_devices(struct btrfs_fs_devices
*fs_devices
,
1184 fmode_t flags
, void *holder
)
1186 struct btrfs_device
*device
;
1187 struct btrfs_device
*latest_dev
= NULL
;
1190 flags
|= FMODE_EXCL
;
1192 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
1193 /* Just open everything we can; ignore failures here */
1194 if (btrfs_open_one_device(fs_devices
, device
, flags
, holder
))
1198 device
->generation
> latest_dev
->generation
)
1199 latest_dev
= device
;
1201 if (fs_devices
->open_devices
== 0) {
1205 fs_devices
->opened
= 1;
1206 fs_devices
->latest_bdev
= latest_dev
->bdev
;
1207 fs_devices
->total_rw_bytes
= 0;
1208 fs_devices
->chunk_alloc_policy
= BTRFS_CHUNK_ALLOC_REGULAR
;
1213 static int devid_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1215 struct btrfs_device
*dev1
, *dev2
;
1217 dev1
= list_entry(a
, struct btrfs_device
, dev_list
);
1218 dev2
= list_entry(b
, struct btrfs_device
, dev_list
);
1220 if (dev1
->devid
< dev2
->devid
)
1222 else if (dev1
->devid
> dev2
->devid
)
1227 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
1228 fmode_t flags
, void *holder
)
1232 lockdep_assert_held(&uuid_mutex
);
1234 mutex_lock(&fs_devices
->device_list_mutex
);
1235 if (fs_devices
->opened
) {
1236 fs_devices
->opened
++;
1239 list_sort(NULL
, &fs_devices
->devices
, devid_cmp
);
1240 ret
= open_fs_devices(fs_devices
, flags
, holder
);
1242 mutex_unlock(&fs_devices
->device_list_mutex
);
1247 void btrfs_release_disk_super(struct btrfs_super_block
*super
)
1249 struct page
*page
= virt_to_page(super
);
1254 static int btrfs_read_disk_super(struct block_device
*bdev
, u64 bytenr
,
1256 struct btrfs_super_block
**disk_super
)
1261 /* make sure our super fits in the device */
1262 if (bytenr
+ PAGE_SIZE
>= i_size_read(bdev
->bd_inode
))
1265 /* make sure our super fits in the page */
1266 if (sizeof(**disk_super
) > PAGE_SIZE
)
1269 /* make sure our super doesn't straddle pages on disk */
1270 index
= bytenr
>> PAGE_SHIFT
;
1271 if ((bytenr
+ sizeof(**disk_super
) - 1) >> PAGE_SHIFT
!= index
)
1274 /* pull in the page with our super */
1275 *page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
,
1281 p
= page_address(*page
);
1283 /* align our pointer to the offset of the super block */
1284 *disk_super
= p
+ offset_in_page(bytenr
);
1286 if (btrfs_super_bytenr(*disk_super
) != bytenr
||
1287 btrfs_super_magic(*disk_super
) != BTRFS_MAGIC
) {
1288 btrfs_release_disk_super(p
);
1292 if ((*disk_super
)->label
[0] &&
1293 (*disk_super
)->label
[BTRFS_LABEL_SIZE
- 1])
1294 (*disk_super
)->label
[BTRFS_LABEL_SIZE
- 1] = '\0';
1299 int btrfs_forget_devices(const char *path
)
1303 mutex_lock(&uuid_mutex
);
1304 ret
= btrfs_free_stale_devices(strlen(path
) ? path
: NULL
, NULL
);
1305 mutex_unlock(&uuid_mutex
);
1311 * Look for a btrfs signature on a device. This may be called out of the mount path
1312 * and we are not allowed to call set_blocksize during the scan. The superblock
1313 * is read via pagecache
1315 struct btrfs_device
*btrfs_scan_one_device(const char *path
, fmode_t flags
,
1318 struct btrfs_super_block
*disk_super
;
1319 bool new_device_added
= false;
1320 struct btrfs_device
*device
= NULL
;
1321 struct block_device
*bdev
;
1325 lockdep_assert_held(&uuid_mutex
);
1328 * we would like to check all the supers, but that would make
1329 * a btrfs mount succeed after a mkfs from a different FS.
1330 * So, we need to add a special mount option to scan for
1331 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1333 bytenr
= btrfs_sb_offset(0);
1334 flags
|= FMODE_EXCL
;
1336 bdev
= blkdev_get_by_path(path
, flags
, holder
);
1338 return ERR_CAST(bdev
);
1340 if (btrfs_read_disk_super(bdev
, bytenr
, &page
, &disk_super
)) {
1341 device
= ERR_PTR(-EINVAL
);
1342 goto error_bdev_put
;
1345 device
= device_list_add(path
, disk_super
, &new_device_added
);
1346 if (!IS_ERR(device
)) {
1347 if (new_device_added
)
1348 btrfs_free_stale_devices(path
, device
);
1351 btrfs_release_disk_super(disk_super
);
1354 blkdev_put(bdev
, flags
);
1360 * Try to find a chunk that intersects [start, start + len] range and when one
1361 * such is found, record the end of it in *start
1363 static bool contains_pending_extent(struct btrfs_device
*device
, u64
*start
,
1366 u64 physical_start
, physical_end
;
1368 lockdep_assert_held(&device
->fs_info
->chunk_mutex
);
1370 if (!find_first_extent_bit(&device
->alloc_state
, *start
,
1371 &physical_start
, &physical_end
,
1372 CHUNK_ALLOCATED
, NULL
)) {
1374 if (in_range(physical_start
, *start
, len
) ||
1375 in_range(*start
, physical_start
,
1376 physical_end
- physical_start
)) {
1377 *start
= physical_end
+ 1;
1384 static u64
dev_extent_search_start(struct btrfs_device
*device
, u64 start
)
1386 switch (device
->fs_devices
->chunk_alloc_policy
) {
1387 case BTRFS_CHUNK_ALLOC_REGULAR
:
1389 * We don't want to overwrite the superblock on the drive nor
1390 * any area used by the boot loader (grub for example), so we
1391 * make sure to start at an offset of at least 1MB.
1393 return max_t(u64
, start
, SZ_1M
);
1400 * dev_extent_hole_check - check if specified hole is suitable for allocation
1401 * @device: the device which we have the hole
1402 * @hole_start: starting position of the hole
1403 * @hole_size: the size of the hole
1404 * @num_bytes: the size of the free space that we need
1406 * This function may modify @hole_start and @hole_end to reflect the suitable
1407 * position for allocation. Returns 1 if hole position is updated, 0 otherwise.
1409 static bool dev_extent_hole_check(struct btrfs_device
*device
, u64
*hole_start
,
1410 u64
*hole_size
, u64 num_bytes
)
1412 bool changed
= false;
1413 u64 hole_end
= *hole_start
+ *hole_size
;
1416 * Check before we set max_hole_start, otherwise we could end up
1417 * sending back this offset anyway.
1419 if (contains_pending_extent(device
, hole_start
, *hole_size
)) {
1420 if (hole_end
>= *hole_start
)
1421 *hole_size
= hole_end
- *hole_start
;
1427 switch (device
->fs_devices
->chunk_alloc_policy
) {
1428 case BTRFS_CHUNK_ALLOC_REGULAR
:
1429 /* No extra check */
1439 * find_free_dev_extent_start - find free space in the specified device
1440 * @device: the device which we search the free space in
1441 * @num_bytes: the size of the free space that we need
1442 * @search_start: the position from which to begin the search
1443 * @start: store the start of the free space.
1444 * @len: the size of the free space. that we find, or the size
1445 * of the max free space if we don't find suitable free space
1447 * this uses a pretty simple search, the expectation is that it is
1448 * called very infrequently and that a given device has a small number
1451 * @start is used to store the start of the free space if we find. But if we
1452 * don't find suitable free space, it will be used to store the start position
1453 * of the max free space.
1455 * @len is used to store the size of the free space that we find.
1456 * But if we don't find suitable free space, it is used to store the size of
1457 * the max free space.
1459 * NOTE: This function will search *commit* root of device tree, and does extra
1460 * check to ensure dev extents are not double allocated.
1461 * This makes the function safe to allocate dev extents but may not report
1462 * correct usable device space, as device extent freed in current transaction
1463 * is not reported as avaiable.
1465 static int find_free_dev_extent_start(struct btrfs_device
*device
,
1466 u64 num_bytes
, u64 search_start
, u64
*start
,
1469 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1470 struct btrfs_root
*root
= fs_info
->dev_root
;
1471 struct btrfs_key key
;
1472 struct btrfs_dev_extent
*dev_extent
;
1473 struct btrfs_path
*path
;
1478 u64 search_end
= device
->total_bytes
;
1481 struct extent_buffer
*l
;
1483 search_start
= dev_extent_search_start(device
, search_start
);
1485 path
= btrfs_alloc_path();
1489 max_hole_start
= search_start
;
1493 if (search_start
>= search_end
||
1494 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
1499 path
->reada
= READA_FORWARD
;
1500 path
->search_commit_root
= 1;
1501 path
->skip_locking
= 1;
1503 key
.objectid
= device
->devid
;
1504 key
.offset
= search_start
;
1505 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1507 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1511 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1518 slot
= path
->slots
[0];
1519 if (slot
>= btrfs_header_nritems(l
)) {
1520 ret
= btrfs_next_leaf(root
, path
);
1528 btrfs_item_key_to_cpu(l
, &key
, slot
);
1530 if (key
.objectid
< device
->devid
)
1533 if (key
.objectid
> device
->devid
)
1536 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1539 if (key
.offset
> search_start
) {
1540 hole_size
= key
.offset
- search_start
;
1541 dev_extent_hole_check(device
, &search_start
, &hole_size
,
1544 if (hole_size
> max_hole_size
) {
1545 max_hole_start
= search_start
;
1546 max_hole_size
= hole_size
;
1550 * If this free space is greater than which we need,
1551 * it must be the max free space that we have found
1552 * until now, so max_hole_start must point to the start
1553 * of this free space and the length of this free space
1554 * is stored in max_hole_size. Thus, we return
1555 * max_hole_start and max_hole_size and go back to the
1558 if (hole_size
>= num_bytes
) {
1564 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1565 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1567 if (extent_end
> search_start
)
1568 search_start
= extent_end
;
1575 * At this point, search_start should be the end of
1576 * allocated dev extents, and when shrinking the device,
1577 * search_end may be smaller than search_start.
1579 if (search_end
> search_start
) {
1580 hole_size
= search_end
- search_start
;
1581 if (dev_extent_hole_check(device
, &search_start
, &hole_size
,
1583 btrfs_release_path(path
);
1587 if (hole_size
> max_hole_size
) {
1588 max_hole_start
= search_start
;
1589 max_hole_size
= hole_size
;
1594 if (max_hole_size
< num_bytes
)
1600 btrfs_free_path(path
);
1601 *start
= max_hole_start
;
1603 *len
= max_hole_size
;
1607 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
1608 u64
*start
, u64
*len
)
1610 /* FIXME use last free of some kind */
1611 return find_free_dev_extent_start(device
, num_bytes
, 0, start
, len
);
1614 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1615 struct btrfs_device
*device
,
1616 u64 start
, u64
*dev_extent_len
)
1618 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1619 struct btrfs_root
*root
= fs_info
->dev_root
;
1621 struct btrfs_path
*path
;
1622 struct btrfs_key key
;
1623 struct btrfs_key found_key
;
1624 struct extent_buffer
*leaf
= NULL
;
1625 struct btrfs_dev_extent
*extent
= NULL
;
1627 path
= btrfs_alloc_path();
1631 key
.objectid
= device
->devid
;
1633 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1635 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1637 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1638 BTRFS_DEV_EXTENT_KEY
);
1641 leaf
= path
->nodes
[0];
1642 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1643 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1644 struct btrfs_dev_extent
);
1645 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1646 btrfs_dev_extent_length(leaf
, extent
) < start
);
1648 btrfs_release_path(path
);
1650 } else if (ret
== 0) {
1651 leaf
= path
->nodes
[0];
1652 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1653 struct btrfs_dev_extent
);
1655 btrfs_handle_fs_error(fs_info
, ret
, "Slot search failed");
1659 *dev_extent_len
= btrfs_dev_extent_length(leaf
, extent
);
1661 ret
= btrfs_del_item(trans
, root
, path
);
1663 btrfs_handle_fs_error(fs_info
, ret
,
1664 "Failed to remove dev extent item");
1666 set_bit(BTRFS_TRANS_HAVE_FREE_BGS
, &trans
->transaction
->flags
);
1669 btrfs_free_path(path
);
1673 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1674 struct btrfs_device
*device
,
1675 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1678 struct btrfs_path
*path
;
1679 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1680 struct btrfs_root
*root
= fs_info
->dev_root
;
1681 struct btrfs_dev_extent
*extent
;
1682 struct extent_buffer
*leaf
;
1683 struct btrfs_key key
;
1685 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
));
1686 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
));
1687 path
= btrfs_alloc_path();
1691 key
.objectid
= device
->devid
;
1693 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1694 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1699 leaf
= path
->nodes
[0];
1700 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1701 struct btrfs_dev_extent
);
1702 btrfs_set_dev_extent_chunk_tree(leaf
, extent
,
1703 BTRFS_CHUNK_TREE_OBJECTID
);
1704 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
,
1705 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
1706 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1708 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1709 btrfs_mark_buffer_dirty(leaf
);
1711 btrfs_free_path(path
);
1715 static u64
find_next_chunk(struct btrfs_fs_info
*fs_info
)
1717 struct extent_map_tree
*em_tree
;
1718 struct extent_map
*em
;
1722 em_tree
= &fs_info
->mapping_tree
;
1723 read_lock(&em_tree
->lock
);
1724 n
= rb_last(&em_tree
->map
.rb_root
);
1726 em
= rb_entry(n
, struct extent_map
, rb_node
);
1727 ret
= em
->start
+ em
->len
;
1729 read_unlock(&em_tree
->lock
);
1734 static noinline
int find_next_devid(struct btrfs_fs_info
*fs_info
,
1738 struct btrfs_key key
;
1739 struct btrfs_key found_key
;
1740 struct btrfs_path
*path
;
1742 path
= btrfs_alloc_path();
1746 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1747 key
.type
= BTRFS_DEV_ITEM_KEY
;
1748 key
.offset
= (u64
)-1;
1750 ret
= btrfs_search_slot(NULL
, fs_info
->chunk_root
, &key
, path
, 0, 0);
1756 btrfs_err(fs_info
, "corrupted chunk tree devid -1 matched");
1761 ret
= btrfs_previous_item(fs_info
->chunk_root
, path
,
1762 BTRFS_DEV_ITEMS_OBJECTID
,
1763 BTRFS_DEV_ITEM_KEY
);
1767 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1769 *devid_ret
= found_key
.offset
+ 1;
1773 btrfs_free_path(path
);
1778 * the device information is stored in the chunk root
1779 * the btrfs_device struct should be fully filled in
1781 static int btrfs_add_dev_item(struct btrfs_trans_handle
*trans
,
1782 struct btrfs_device
*device
)
1785 struct btrfs_path
*path
;
1786 struct btrfs_dev_item
*dev_item
;
1787 struct extent_buffer
*leaf
;
1788 struct btrfs_key key
;
1791 path
= btrfs_alloc_path();
1795 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1796 key
.type
= BTRFS_DEV_ITEM_KEY
;
1797 key
.offset
= device
->devid
;
1799 ret
= btrfs_insert_empty_item(trans
, trans
->fs_info
->chunk_root
, path
,
1800 &key
, sizeof(*dev_item
));
1804 leaf
= path
->nodes
[0];
1805 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1807 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1808 btrfs_set_device_generation(leaf
, dev_item
, 0);
1809 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1810 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1811 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1812 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1813 btrfs_set_device_total_bytes(leaf
, dev_item
,
1814 btrfs_device_get_disk_total_bytes(device
));
1815 btrfs_set_device_bytes_used(leaf
, dev_item
,
1816 btrfs_device_get_bytes_used(device
));
1817 btrfs_set_device_group(leaf
, dev_item
, 0);
1818 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1819 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1820 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1822 ptr
= btrfs_device_uuid(dev_item
);
1823 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1824 ptr
= btrfs_device_fsid(dev_item
);
1825 write_extent_buffer(leaf
, trans
->fs_info
->fs_devices
->metadata_uuid
,
1826 ptr
, BTRFS_FSID_SIZE
);
1827 btrfs_mark_buffer_dirty(leaf
);
1831 btrfs_free_path(path
);
1836 * Function to update ctime/mtime for a given device path.
1837 * Mainly used for ctime/mtime based probe like libblkid.
1839 static void update_dev_time(const char *path_name
)
1843 filp
= filp_open(path_name
, O_RDWR
, 0);
1846 file_update_time(filp
);
1847 filp_close(filp
, NULL
);
1850 static int btrfs_rm_dev_item(struct btrfs_device
*device
)
1852 struct btrfs_root
*root
= device
->fs_info
->chunk_root
;
1854 struct btrfs_path
*path
;
1855 struct btrfs_key key
;
1856 struct btrfs_trans_handle
*trans
;
1858 path
= btrfs_alloc_path();
1862 trans
= btrfs_start_transaction(root
, 0);
1863 if (IS_ERR(trans
)) {
1864 btrfs_free_path(path
);
1865 return PTR_ERR(trans
);
1867 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1868 key
.type
= BTRFS_DEV_ITEM_KEY
;
1869 key
.offset
= device
->devid
;
1871 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1875 btrfs_abort_transaction(trans
, ret
);
1876 btrfs_end_transaction(trans
);
1880 ret
= btrfs_del_item(trans
, root
, path
);
1882 btrfs_abort_transaction(trans
, ret
);
1883 btrfs_end_transaction(trans
);
1887 btrfs_free_path(path
);
1889 ret
= btrfs_commit_transaction(trans
);
1894 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1895 * filesystem. It's up to the caller to adjust that number regarding eg. device
1898 static int btrfs_check_raid_min_devices(struct btrfs_fs_info
*fs_info
,
1906 seq
= read_seqbegin(&fs_info
->profiles_lock
);
1908 all_avail
= fs_info
->avail_data_alloc_bits
|
1909 fs_info
->avail_system_alloc_bits
|
1910 fs_info
->avail_metadata_alloc_bits
;
1911 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
1913 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
1914 if (!(all_avail
& btrfs_raid_array
[i
].bg_flag
))
1917 if (num_devices
< btrfs_raid_array
[i
].devs_min
) {
1918 int ret
= btrfs_raid_array
[i
].mindev_error
;
1928 static struct btrfs_device
* btrfs_find_next_active_device(
1929 struct btrfs_fs_devices
*fs_devs
, struct btrfs_device
*device
)
1931 struct btrfs_device
*next_device
;
1933 list_for_each_entry(next_device
, &fs_devs
->devices
, dev_list
) {
1934 if (next_device
!= device
&&
1935 !test_bit(BTRFS_DEV_STATE_MISSING
, &next_device
->dev_state
)
1936 && next_device
->bdev
)
1944 * Helper function to check if the given device is part of s_bdev / latest_bdev
1945 * and replace it with the provided or the next active device, in the context
1946 * where this function called, there should be always be another device (or
1947 * this_dev) which is active.
1949 void __cold
btrfs_assign_next_active_device(struct btrfs_device
*device
,
1950 struct btrfs_device
*this_dev
)
1952 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1953 struct btrfs_device
*next_device
;
1956 next_device
= this_dev
;
1958 next_device
= btrfs_find_next_active_device(fs_info
->fs_devices
,
1960 ASSERT(next_device
);
1962 if (fs_info
->sb
->s_bdev
&&
1963 (fs_info
->sb
->s_bdev
== device
->bdev
))
1964 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1966 if (fs_info
->fs_devices
->latest_bdev
== device
->bdev
)
1967 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1971 * Return btrfs_fs_devices::num_devices excluding the device that's being
1972 * currently replaced.
1974 static u64
btrfs_num_devices(struct btrfs_fs_info
*fs_info
)
1976 u64 num_devices
= fs_info
->fs_devices
->num_devices
;
1978 down_read(&fs_info
->dev_replace
.rwsem
);
1979 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
1980 ASSERT(num_devices
> 1);
1983 up_read(&fs_info
->dev_replace
.rwsem
);
1988 static void btrfs_scratch_superblocks(struct btrfs_fs_info
*fs_info
,
1989 struct block_device
*bdev
,
1990 const char *device_path
)
1992 struct btrfs_super_block
*disk_super
;
1998 for (copy_num
= 0; copy_num
< BTRFS_SUPER_MIRROR_MAX
; copy_num
++) {
2002 disk_super
= btrfs_read_dev_one_super(bdev
, copy_num
);
2003 if (IS_ERR(disk_super
))
2006 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
2008 page
= virt_to_page(disk_super
);
2009 set_page_dirty(page
);
2011 /* write_on_page() unlocks the page */
2012 ret
= write_one_page(page
);
2015 "error clearing superblock number %d (%d)",
2017 btrfs_release_disk_super(disk_super
);
2021 /* Notify udev that device has changed */
2022 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
2024 /* Update ctime/mtime for device path for libblkid */
2025 update_dev_time(device_path
);
2028 int btrfs_rm_device(struct btrfs_fs_info
*fs_info
, const char *device_path
,
2031 struct btrfs_device
*device
;
2032 struct btrfs_fs_devices
*cur_devices
;
2033 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2037 mutex_lock(&uuid_mutex
);
2039 num_devices
= btrfs_num_devices(fs_info
);
2041 ret
= btrfs_check_raid_min_devices(fs_info
, num_devices
- 1);
2045 device
= btrfs_find_device_by_devspec(fs_info
, devid
, device_path
);
2047 if (IS_ERR(device
)) {
2048 if (PTR_ERR(device
) == -ENOENT
&&
2049 strcmp(device_path
, "missing") == 0)
2050 ret
= BTRFS_ERROR_DEV_MISSING_NOT_FOUND
;
2052 ret
= PTR_ERR(device
);
2056 if (btrfs_pinned_by_swapfile(fs_info
, device
)) {
2057 btrfs_warn_in_rcu(fs_info
,
2058 "cannot remove device %s (devid %llu) due to active swapfile",
2059 rcu_str_deref(device
->name
), device
->devid
);
2064 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
2065 ret
= BTRFS_ERROR_DEV_TGT_REPLACE
;
2069 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
2070 fs_info
->fs_devices
->rw_devices
== 1) {
2071 ret
= BTRFS_ERROR_DEV_ONLY_WRITABLE
;
2075 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
2076 mutex_lock(&fs_info
->chunk_mutex
);
2077 list_del_init(&device
->dev_alloc_list
);
2078 device
->fs_devices
->rw_devices
--;
2079 mutex_unlock(&fs_info
->chunk_mutex
);
2082 mutex_unlock(&uuid_mutex
);
2083 ret
= btrfs_shrink_device(device
, 0);
2084 mutex_lock(&uuid_mutex
);
2089 * TODO: the superblock still includes this device in its num_devices
2090 * counter although write_all_supers() is not locked out. This
2091 * could give a filesystem state which requires a degraded mount.
2093 ret
= btrfs_rm_dev_item(device
);
2097 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
2098 btrfs_scrub_cancel_dev(device
);
2101 * the device list mutex makes sure that we don't change
2102 * the device list while someone else is writing out all
2103 * the device supers. Whoever is writing all supers, should
2104 * lock the device list mutex before getting the number of
2105 * devices in the super block (super_copy). Conversely,
2106 * whoever updates the number of devices in the super block
2107 * (super_copy) should hold the device list mutex.
2111 * In normal cases the cur_devices == fs_devices. But in case
2112 * of deleting a seed device, the cur_devices should point to
2113 * its own fs_devices listed under the fs_devices->seed.
2115 cur_devices
= device
->fs_devices
;
2116 mutex_lock(&fs_devices
->device_list_mutex
);
2117 list_del_rcu(&device
->dev_list
);
2119 cur_devices
->num_devices
--;
2120 cur_devices
->total_devices
--;
2121 /* Update total_devices of the parent fs_devices if it's seed */
2122 if (cur_devices
!= fs_devices
)
2123 fs_devices
->total_devices
--;
2125 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
))
2126 cur_devices
->missing_devices
--;
2128 btrfs_assign_next_active_device(device
, NULL
);
2131 cur_devices
->open_devices
--;
2132 /* remove sysfs entry */
2133 btrfs_sysfs_remove_devices_dir(fs_devices
, device
);
2136 num_devices
= btrfs_super_num_devices(fs_info
->super_copy
) - 1;
2137 btrfs_set_super_num_devices(fs_info
->super_copy
, num_devices
);
2138 mutex_unlock(&fs_devices
->device_list_mutex
);
2141 * at this point, the device is zero sized and detached from
2142 * the devices list. All that's left is to zero out the old
2143 * supers and free the device.
2145 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
2146 btrfs_scratch_superblocks(fs_info
, device
->bdev
,
2149 btrfs_close_bdev(device
);
2151 btrfs_free_device(device
);
2153 if (cur_devices
->open_devices
== 0) {
2154 while (fs_devices
) {
2155 if (fs_devices
->seed
== cur_devices
) {
2156 fs_devices
->seed
= cur_devices
->seed
;
2159 fs_devices
= fs_devices
->seed
;
2161 cur_devices
->seed
= NULL
;
2162 close_fs_devices(cur_devices
);
2163 free_fs_devices(cur_devices
);
2167 mutex_unlock(&uuid_mutex
);
2171 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
2172 mutex_lock(&fs_info
->chunk_mutex
);
2173 list_add(&device
->dev_alloc_list
,
2174 &fs_devices
->alloc_list
);
2175 device
->fs_devices
->rw_devices
++;
2176 mutex_unlock(&fs_info
->chunk_mutex
);
2181 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device
*srcdev
)
2183 struct btrfs_fs_devices
*fs_devices
;
2185 lockdep_assert_held(&srcdev
->fs_info
->fs_devices
->device_list_mutex
);
2188 * in case of fs with no seed, srcdev->fs_devices will point
2189 * to fs_devices of fs_info. However when the dev being replaced is
2190 * a seed dev it will point to the seed's local fs_devices. In short
2191 * srcdev will have its correct fs_devices in both the cases.
2193 fs_devices
= srcdev
->fs_devices
;
2195 list_del_rcu(&srcdev
->dev_list
);
2196 list_del(&srcdev
->dev_alloc_list
);
2197 fs_devices
->num_devices
--;
2198 if (test_bit(BTRFS_DEV_STATE_MISSING
, &srcdev
->dev_state
))
2199 fs_devices
->missing_devices
--;
2201 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &srcdev
->dev_state
))
2202 fs_devices
->rw_devices
--;
2205 fs_devices
->open_devices
--;
2208 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device
*srcdev
)
2210 struct btrfs_fs_info
*fs_info
= srcdev
->fs_info
;
2211 struct btrfs_fs_devices
*fs_devices
= srcdev
->fs_devices
;
2213 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &srcdev
->dev_state
)) {
2214 /* zero out the old super if it is writable */
2215 btrfs_scratch_superblocks(fs_info
, srcdev
->bdev
,
2219 btrfs_close_bdev(srcdev
);
2221 btrfs_free_device(srcdev
);
2223 /* if this is no devs we rather delete the fs_devices */
2224 if (!fs_devices
->num_devices
) {
2225 struct btrfs_fs_devices
*tmp_fs_devices
;
2228 * On a mounted FS, num_devices can't be zero unless it's a
2229 * seed. In case of a seed device being replaced, the replace
2230 * target added to the sprout FS, so there will be no more
2231 * device left under the seed FS.
2233 ASSERT(fs_devices
->seeding
);
2235 tmp_fs_devices
= fs_info
->fs_devices
;
2236 while (tmp_fs_devices
) {
2237 if (tmp_fs_devices
->seed
== fs_devices
) {
2238 tmp_fs_devices
->seed
= fs_devices
->seed
;
2241 tmp_fs_devices
= tmp_fs_devices
->seed
;
2243 fs_devices
->seed
= NULL
;
2244 close_fs_devices(fs_devices
);
2245 free_fs_devices(fs_devices
);
2249 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device
*tgtdev
)
2251 struct btrfs_fs_devices
*fs_devices
= tgtdev
->fs_info
->fs_devices
;
2253 mutex_lock(&fs_devices
->device_list_mutex
);
2255 btrfs_sysfs_remove_devices_dir(fs_devices
, tgtdev
);
2258 fs_devices
->open_devices
--;
2260 fs_devices
->num_devices
--;
2262 btrfs_assign_next_active_device(tgtdev
, NULL
);
2264 list_del_rcu(&tgtdev
->dev_list
);
2266 mutex_unlock(&fs_devices
->device_list_mutex
);
2269 * The update_dev_time() with in btrfs_scratch_superblocks()
2270 * may lead to a call to btrfs_show_devname() which will try
2271 * to hold device_list_mutex. And here this device
2272 * is already out of device list, so we don't have to hold
2273 * the device_list_mutex lock.
2275 btrfs_scratch_superblocks(tgtdev
->fs_info
, tgtdev
->bdev
,
2278 btrfs_close_bdev(tgtdev
);
2280 btrfs_free_device(tgtdev
);
2283 static struct btrfs_device
*btrfs_find_device_by_path(
2284 struct btrfs_fs_info
*fs_info
, const char *device_path
)
2287 struct btrfs_super_block
*disk_super
;
2290 struct block_device
*bdev
;
2291 struct btrfs_device
*device
;
2293 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
2294 fs_info
->bdev_holder
, 0, &bdev
, &disk_super
);
2296 return ERR_PTR(ret
);
2298 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
2299 dev_uuid
= disk_super
->dev_item
.uuid
;
2300 if (btrfs_fs_incompat(fs_info
, METADATA_UUID
))
2301 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
2302 disk_super
->metadata_uuid
, true);
2304 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
2305 disk_super
->fsid
, true);
2307 btrfs_release_disk_super(disk_super
);
2309 device
= ERR_PTR(-ENOENT
);
2310 blkdev_put(bdev
, FMODE_READ
);
2315 * Lookup a device given by device id, or the path if the id is 0.
2317 struct btrfs_device
*btrfs_find_device_by_devspec(
2318 struct btrfs_fs_info
*fs_info
, u64 devid
,
2319 const char *device_path
)
2321 struct btrfs_device
*device
;
2324 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, NULL
,
2327 return ERR_PTR(-ENOENT
);
2331 if (!device_path
|| !device_path
[0])
2332 return ERR_PTR(-EINVAL
);
2334 if (strcmp(device_path
, "missing") == 0) {
2335 /* Find first missing device */
2336 list_for_each_entry(device
, &fs_info
->fs_devices
->devices
,
2338 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
2339 &device
->dev_state
) && !device
->bdev
)
2342 return ERR_PTR(-ENOENT
);
2345 return btrfs_find_device_by_path(fs_info
, device_path
);
2349 * does all the dirty work required for changing file system's UUID.
2351 static int btrfs_prepare_sprout(struct btrfs_fs_info
*fs_info
)
2353 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2354 struct btrfs_fs_devices
*old_devices
;
2355 struct btrfs_fs_devices
*seed_devices
;
2356 struct btrfs_super_block
*disk_super
= fs_info
->super_copy
;
2357 struct btrfs_device
*device
;
2360 lockdep_assert_held(&uuid_mutex
);
2361 if (!fs_devices
->seeding
)
2364 seed_devices
= alloc_fs_devices(NULL
, NULL
);
2365 if (IS_ERR(seed_devices
))
2366 return PTR_ERR(seed_devices
);
2368 old_devices
= clone_fs_devices(fs_devices
);
2369 if (IS_ERR(old_devices
)) {
2370 kfree(seed_devices
);
2371 return PTR_ERR(old_devices
);
2374 list_add(&old_devices
->fs_list
, &fs_uuids
);
2376 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
2377 seed_devices
->opened
= 1;
2378 INIT_LIST_HEAD(&seed_devices
->devices
);
2379 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
2380 mutex_init(&seed_devices
->device_list_mutex
);
2382 mutex_lock(&fs_devices
->device_list_mutex
);
2383 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
2385 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
)
2386 device
->fs_devices
= seed_devices
;
2388 mutex_lock(&fs_info
->chunk_mutex
);
2389 list_splice_init(&fs_devices
->alloc_list
, &seed_devices
->alloc_list
);
2390 mutex_unlock(&fs_info
->chunk_mutex
);
2392 fs_devices
->seeding
= false;
2393 fs_devices
->num_devices
= 0;
2394 fs_devices
->open_devices
= 0;
2395 fs_devices
->missing_devices
= 0;
2396 fs_devices
->rotating
= false;
2397 fs_devices
->seed
= seed_devices
;
2399 generate_random_uuid(fs_devices
->fsid
);
2400 memcpy(fs_devices
->metadata_uuid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2401 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2402 mutex_unlock(&fs_devices
->device_list_mutex
);
2404 super_flags
= btrfs_super_flags(disk_super
) &
2405 ~BTRFS_SUPER_FLAG_SEEDING
;
2406 btrfs_set_super_flags(disk_super
, super_flags
);
2412 * Store the expected generation for seed devices in device items.
2414 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
)
2416 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2417 struct btrfs_root
*root
= fs_info
->chunk_root
;
2418 struct btrfs_path
*path
;
2419 struct extent_buffer
*leaf
;
2420 struct btrfs_dev_item
*dev_item
;
2421 struct btrfs_device
*device
;
2422 struct btrfs_key key
;
2423 u8 fs_uuid
[BTRFS_FSID_SIZE
];
2424 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2428 path
= btrfs_alloc_path();
2432 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2434 key
.type
= BTRFS_DEV_ITEM_KEY
;
2437 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2441 leaf
= path
->nodes
[0];
2443 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
2444 ret
= btrfs_next_leaf(root
, path
);
2449 leaf
= path
->nodes
[0];
2450 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2451 btrfs_release_path(path
);
2455 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2456 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
2457 key
.type
!= BTRFS_DEV_ITEM_KEY
)
2460 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2461 struct btrfs_dev_item
);
2462 devid
= btrfs_device_id(leaf
, dev_item
);
2463 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
2465 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
2467 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
2469 BUG_ON(!device
); /* Logic error */
2471 if (device
->fs_devices
->seeding
) {
2472 btrfs_set_device_generation(leaf
, dev_item
,
2473 device
->generation
);
2474 btrfs_mark_buffer_dirty(leaf
);
2482 btrfs_free_path(path
);
2486 int btrfs_init_new_device(struct btrfs_fs_info
*fs_info
, const char *device_path
)
2488 struct btrfs_root
*root
= fs_info
->dev_root
;
2489 struct request_queue
*q
;
2490 struct btrfs_trans_handle
*trans
;
2491 struct btrfs_device
*device
;
2492 struct block_device
*bdev
;
2493 struct super_block
*sb
= fs_info
->sb
;
2494 struct rcu_string
*name
;
2495 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2496 u64 orig_super_total_bytes
;
2497 u64 orig_super_num_devices
;
2498 int seeding_dev
= 0;
2500 bool unlocked
= false;
2502 if (sb_rdonly(sb
) && !fs_devices
->seeding
)
2505 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2506 fs_info
->bdev_holder
);
2508 return PTR_ERR(bdev
);
2510 if (fs_devices
->seeding
) {
2512 down_write(&sb
->s_umount
);
2513 mutex_lock(&uuid_mutex
);
2516 filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
2518 mutex_lock(&fs_devices
->device_list_mutex
);
2519 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
2520 if (device
->bdev
== bdev
) {
2523 &fs_devices
->device_list_mutex
);
2527 mutex_unlock(&fs_devices
->device_list_mutex
);
2529 device
= btrfs_alloc_device(fs_info
, NULL
, NULL
);
2530 if (IS_ERR(device
)) {
2531 /* we can safely leave the fs_devices entry around */
2532 ret
= PTR_ERR(device
);
2536 name
= rcu_string_strdup(device_path
, GFP_KERNEL
);
2539 goto error_free_device
;
2541 rcu_assign_pointer(device
->name
, name
);
2543 trans
= btrfs_start_transaction(root
, 0);
2544 if (IS_ERR(trans
)) {
2545 ret
= PTR_ERR(trans
);
2546 goto error_free_device
;
2549 q
= bdev_get_queue(bdev
);
2550 set_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
2551 device
->generation
= trans
->transid
;
2552 device
->io_width
= fs_info
->sectorsize
;
2553 device
->io_align
= fs_info
->sectorsize
;
2554 device
->sector_size
= fs_info
->sectorsize
;
2555 device
->total_bytes
= round_down(i_size_read(bdev
->bd_inode
),
2556 fs_info
->sectorsize
);
2557 device
->disk_total_bytes
= device
->total_bytes
;
2558 device
->commit_total_bytes
= device
->total_bytes
;
2559 device
->fs_info
= fs_info
;
2560 device
->bdev
= bdev
;
2561 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
2562 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
2563 device
->mode
= FMODE_EXCL
;
2564 device
->dev_stats_valid
= 1;
2565 set_blocksize(device
->bdev
, BTRFS_BDEV_BLOCKSIZE
);
2568 sb
->s_flags
&= ~SB_RDONLY
;
2569 ret
= btrfs_prepare_sprout(fs_info
);
2571 btrfs_abort_transaction(trans
, ret
);
2576 device
->fs_devices
= fs_devices
;
2578 mutex_lock(&fs_devices
->device_list_mutex
);
2579 mutex_lock(&fs_info
->chunk_mutex
);
2580 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
2581 list_add(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
2582 fs_devices
->num_devices
++;
2583 fs_devices
->open_devices
++;
2584 fs_devices
->rw_devices
++;
2585 fs_devices
->total_devices
++;
2586 fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2588 atomic64_add(device
->total_bytes
, &fs_info
->free_chunk_space
);
2590 if (!blk_queue_nonrot(q
))
2591 fs_devices
->rotating
= true;
2593 orig_super_total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
2594 btrfs_set_super_total_bytes(fs_info
->super_copy
,
2595 round_down(orig_super_total_bytes
+ device
->total_bytes
,
2596 fs_info
->sectorsize
));
2598 orig_super_num_devices
= btrfs_super_num_devices(fs_info
->super_copy
);
2599 btrfs_set_super_num_devices(fs_info
->super_copy
,
2600 orig_super_num_devices
+ 1);
2602 /* add sysfs device entry */
2603 btrfs_sysfs_add_devices_dir(fs_devices
, device
);
2606 * we've got more storage, clear any full flags on the space
2609 btrfs_clear_space_info_full(fs_info
);
2611 mutex_unlock(&fs_info
->chunk_mutex
);
2612 mutex_unlock(&fs_devices
->device_list_mutex
);
2615 mutex_lock(&fs_info
->chunk_mutex
);
2616 ret
= init_first_rw_device(trans
);
2617 mutex_unlock(&fs_info
->chunk_mutex
);
2619 btrfs_abort_transaction(trans
, ret
);
2624 ret
= btrfs_add_dev_item(trans
, device
);
2626 btrfs_abort_transaction(trans
, ret
);
2631 ret
= btrfs_finish_sprout(trans
);
2633 btrfs_abort_transaction(trans
, ret
);
2637 btrfs_sysfs_update_sprout_fsid(fs_devices
,
2638 fs_info
->fs_devices
->fsid
);
2641 ret
= btrfs_commit_transaction(trans
);
2644 mutex_unlock(&uuid_mutex
);
2645 up_write(&sb
->s_umount
);
2648 if (ret
) /* transaction commit */
2651 ret
= btrfs_relocate_sys_chunks(fs_info
);
2653 btrfs_handle_fs_error(fs_info
, ret
,
2654 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2655 trans
= btrfs_attach_transaction(root
);
2656 if (IS_ERR(trans
)) {
2657 if (PTR_ERR(trans
) == -ENOENT
)
2659 ret
= PTR_ERR(trans
);
2663 ret
= btrfs_commit_transaction(trans
);
2666 /* Update ctime/mtime for libblkid */
2667 update_dev_time(device_path
);
2671 btrfs_sysfs_remove_devices_dir(fs_devices
, device
);
2672 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
2673 mutex_lock(&fs_info
->chunk_mutex
);
2674 list_del_rcu(&device
->dev_list
);
2675 list_del(&device
->dev_alloc_list
);
2676 fs_info
->fs_devices
->num_devices
--;
2677 fs_info
->fs_devices
->open_devices
--;
2678 fs_info
->fs_devices
->rw_devices
--;
2679 fs_info
->fs_devices
->total_devices
--;
2680 fs_info
->fs_devices
->total_rw_bytes
-= device
->total_bytes
;
2681 atomic64_sub(device
->total_bytes
, &fs_info
->free_chunk_space
);
2682 btrfs_set_super_total_bytes(fs_info
->super_copy
,
2683 orig_super_total_bytes
);
2684 btrfs_set_super_num_devices(fs_info
->super_copy
,
2685 orig_super_num_devices
);
2686 mutex_unlock(&fs_info
->chunk_mutex
);
2687 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
2690 sb
->s_flags
|= SB_RDONLY
;
2692 btrfs_end_transaction(trans
);
2694 btrfs_free_device(device
);
2696 blkdev_put(bdev
, FMODE_EXCL
);
2697 if (seeding_dev
&& !unlocked
) {
2698 mutex_unlock(&uuid_mutex
);
2699 up_write(&sb
->s_umount
);
2704 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2705 struct btrfs_device
*device
)
2708 struct btrfs_path
*path
;
2709 struct btrfs_root
*root
= device
->fs_info
->chunk_root
;
2710 struct btrfs_dev_item
*dev_item
;
2711 struct extent_buffer
*leaf
;
2712 struct btrfs_key key
;
2714 path
= btrfs_alloc_path();
2718 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2719 key
.type
= BTRFS_DEV_ITEM_KEY
;
2720 key
.offset
= device
->devid
;
2722 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2731 leaf
= path
->nodes
[0];
2732 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2734 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2735 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2736 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2737 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2738 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2739 btrfs_set_device_total_bytes(leaf
, dev_item
,
2740 btrfs_device_get_disk_total_bytes(device
));
2741 btrfs_set_device_bytes_used(leaf
, dev_item
,
2742 btrfs_device_get_bytes_used(device
));
2743 btrfs_mark_buffer_dirty(leaf
);
2746 btrfs_free_path(path
);
2750 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2751 struct btrfs_device
*device
, u64 new_size
)
2753 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
2754 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
2758 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
2761 new_size
= round_down(new_size
, fs_info
->sectorsize
);
2763 mutex_lock(&fs_info
->chunk_mutex
);
2764 old_total
= btrfs_super_total_bytes(super_copy
);
2765 diff
= round_down(new_size
- device
->total_bytes
, fs_info
->sectorsize
);
2767 if (new_size
<= device
->total_bytes
||
2768 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
2769 mutex_unlock(&fs_info
->chunk_mutex
);
2773 btrfs_set_super_total_bytes(super_copy
,
2774 round_down(old_total
+ diff
, fs_info
->sectorsize
));
2775 device
->fs_devices
->total_rw_bytes
+= diff
;
2777 btrfs_device_set_total_bytes(device
, new_size
);
2778 btrfs_device_set_disk_total_bytes(device
, new_size
);
2779 btrfs_clear_space_info_full(device
->fs_info
);
2780 if (list_empty(&device
->post_commit_list
))
2781 list_add_tail(&device
->post_commit_list
,
2782 &trans
->transaction
->dev_update_list
);
2783 mutex_unlock(&fs_info
->chunk_mutex
);
2785 return btrfs_update_device(trans
, device
);
2788 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
, u64 chunk_offset
)
2790 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2791 struct btrfs_root
*root
= fs_info
->chunk_root
;
2793 struct btrfs_path
*path
;
2794 struct btrfs_key key
;
2796 path
= btrfs_alloc_path();
2800 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2801 key
.offset
= chunk_offset
;
2802 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2804 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2807 else if (ret
> 0) { /* Logic error or corruption */
2808 btrfs_handle_fs_error(fs_info
, -ENOENT
,
2809 "Failed lookup while freeing chunk.");
2814 ret
= btrfs_del_item(trans
, root
, path
);
2816 btrfs_handle_fs_error(fs_info
, ret
,
2817 "Failed to delete chunk item.");
2819 btrfs_free_path(path
);
2823 static int btrfs_del_sys_chunk(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
2825 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
2826 struct btrfs_disk_key
*disk_key
;
2827 struct btrfs_chunk
*chunk
;
2834 struct btrfs_key key
;
2836 mutex_lock(&fs_info
->chunk_mutex
);
2837 array_size
= btrfs_super_sys_array_size(super_copy
);
2839 ptr
= super_copy
->sys_chunk_array
;
2842 while (cur
< array_size
) {
2843 disk_key
= (struct btrfs_disk_key
*)ptr
;
2844 btrfs_disk_key_to_cpu(&key
, disk_key
);
2846 len
= sizeof(*disk_key
);
2848 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2849 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2850 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2851 len
+= btrfs_chunk_item_size(num_stripes
);
2856 if (key
.objectid
== BTRFS_FIRST_CHUNK_TREE_OBJECTID
&&
2857 key
.offset
== chunk_offset
) {
2858 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2860 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2866 mutex_unlock(&fs_info
->chunk_mutex
);
2871 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2872 * @logical: Logical block offset in bytes.
2873 * @length: Length of extent in bytes.
2875 * Return: Chunk mapping or ERR_PTR.
2877 struct extent_map
*btrfs_get_chunk_map(struct btrfs_fs_info
*fs_info
,
2878 u64 logical
, u64 length
)
2880 struct extent_map_tree
*em_tree
;
2881 struct extent_map
*em
;
2883 em_tree
= &fs_info
->mapping_tree
;
2884 read_lock(&em_tree
->lock
);
2885 em
= lookup_extent_mapping(em_tree
, logical
, length
);
2886 read_unlock(&em_tree
->lock
);
2889 btrfs_crit(fs_info
, "unable to find logical %llu length %llu",
2891 return ERR_PTR(-EINVAL
);
2894 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
2896 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2897 logical
, length
, em
->start
, em
->start
+ em
->len
);
2898 free_extent_map(em
);
2899 return ERR_PTR(-EINVAL
);
2902 /* callers are responsible for dropping em's ref. */
2906 int btrfs_remove_chunk(struct btrfs_trans_handle
*trans
, u64 chunk_offset
)
2908 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2909 struct extent_map
*em
;
2910 struct map_lookup
*map
;
2911 u64 dev_extent_len
= 0;
2913 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2915 em
= btrfs_get_chunk_map(fs_info
, chunk_offset
, 1);
2918 * This is a logic error, but we don't want to just rely on the
2919 * user having built with ASSERT enabled, so if ASSERT doesn't
2920 * do anything we still error out.
2925 map
= em
->map_lookup
;
2926 mutex_lock(&fs_info
->chunk_mutex
);
2927 check_system_chunk(trans
, map
->type
);
2928 mutex_unlock(&fs_info
->chunk_mutex
);
2931 * Take the device list mutex to prevent races with the final phase of
2932 * a device replace operation that replaces the device object associated
2933 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2935 mutex_lock(&fs_devices
->device_list_mutex
);
2936 for (i
= 0; i
< map
->num_stripes
; i
++) {
2937 struct btrfs_device
*device
= map
->stripes
[i
].dev
;
2938 ret
= btrfs_free_dev_extent(trans
, device
,
2939 map
->stripes
[i
].physical
,
2942 mutex_unlock(&fs_devices
->device_list_mutex
);
2943 btrfs_abort_transaction(trans
, ret
);
2947 if (device
->bytes_used
> 0) {
2948 mutex_lock(&fs_info
->chunk_mutex
);
2949 btrfs_device_set_bytes_used(device
,
2950 device
->bytes_used
- dev_extent_len
);
2951 atomic64_add(dev_extent_len
, &fs_info
->free_chunk_space
);
2952 btrfs_clear_space_info_full(fs_info
);
2953 mutex_unlock(&fs_info
->chunk_mutex
);
2956 ret
= btrfs_update_device(trans
, device
);
2958 mutex_unlock(&fs_devices
->device_list_mutex
);
2959 btrfs_abort_transaction(trans
, ret
);
2963 mutex_unlock(&fs_devices
->device_list_mutex
);
2965 ret
= btrfs_free_chunk(trans
, chunk_offset
);
2967 btrfs_abort_transaction(trans
, ret
);
2971 trace_btrfs_chunk_free(fs_info
, map
, chunk_offset
, em
->len
);
2973 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2974 ret
= btrfs_del_sys_chunk(fs_info
, chunk_offset
);
2976 btrfs_abort_transaction(trans
, ret
);
2981 ret
= btrfs_remove_block_group(trans
, chunk_offset
, em
);
2983 btrfs_abort_transaction(trans
, ret
);
2989 free_extent_map(em
);
2993 static int btrfs_relocate_chunk(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
2995 struct btrfs_root
*root
= fs_info
->chunk_root
;
2996 struct btrfs_trans_handle
*trans
;
2997 struct btrfs_block_group
*block_group
;
3001 * Prevent races with automatic removal of unused block groups.
3002 * After we relocate and before we remove the chunk with offset
3003 * chunk_offset, automatic removal of the block group can kick in,
3004 * resulting in a failure when calling btrfs_remove_chunk() below.
3006 * Make sure to acquire this mutex before doing a tree search (dev
3007 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3008 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3009 * we release the path used to search the chunk/dev tree and before
3010 * the current task acquires this mutex and calls us.
3012 lockdep_assert_held(&fs_info
->delete_unused_bgs_mutex
);
3014 /* step one, relocate all the extents inside this chunk */
3015 btrfs_scrub_pause(fs_info
);
3016 ret
= btrfs_relocate_block_group(fs_info
, chunk_offset
);
3017 btrfs_scrub_continue(fs_info
);
3021 block_group
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3024 btrfs_discard_cancel_work(&fs_info
->discard_ctl
, block_group
);
3025 btrfs_put_block_group(block_group
);
3027 trans
= btrfs_start_trans_remove_block_group(root
->fs_info
,
3029 if (IS_ERR(trans
)) {
3030 ret
= PTR_ERR(trans
);
3031 btrfs_handle_fs_error(root
->fs_info
, ret
, NULL
);
3036 * step two, delete the device extents and the
3037 * chunk tree entries
3039 ret
= btrfs_remove_chunk(trans
, chunk_offset
);
3040 btrfs_end_transaction(trans
);
3044 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info
*fs_info
)
3046 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3047 struct btrfs_path
*path
;
3048 struct extent_buffer
*leaf
;
3049 struct btrfs_chunk
*chunk
;
3050 struct btrfs_key key
;
3051 struct btrfs_key found_key
;
3053 bool retried
= false;
3057 path
= btrfs_alloc_path();
3062 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3063 key
.offset
= (u64
)-1;
3064 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3067 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
3068 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3070 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3073 BUG_ON(ret
== 0); /* Corruption */
3075 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
3078 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3084 leaf
= path
->nodes
[0];
3085 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3087 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
3088 struct btrfs_chunk
);
3089 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3090 btrfs_release_path(path
);
3092 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3093 ret
= btrfs_relocate_chunk(fs_info
, found_key
.offset
);
3099 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3101 if (found_key
.offset
== 0)
3103 key
.offset
= found_key
.offset
- 1;
3106 if (failed
&& !retried
) {
3110 } else if (WARN_ON(failed
&& retried
)) {
3114 btrfs_free_path(path
);
3119 * return 1 : allocate a data chunk successfully,
3120 * return <0: errors during allocating a data chunk,
3121 * return 0 : no need to allocate a data chunk.
3123 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info
*fs_info
,
3126 struct btrfs_block_group
*cache
;
3130 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3132 chunk_type
= cache
->flags
;
3133 btrfs_put_block_group(cache
);
3135 if (!(chunk_type
& BTRFS_BLOCK_GROUP_DATA
))
3138 spin_lock(&fs_info
->data_sinfo
->lock
);
3139 bytes_used
= fs_info
->data_sinfo
->bytes_used
;
3140 spin_unlock(&fs_info
->data_sinfo
->lock
);
3143 struct btrfs_trans_handle
*trans
;
3146 trans
= btrfs_join_transaction(fs_info
->tree_root
);
3148 return PTR_ERR(trans
);
3150 ret
= btrfs_force_chunk_alloc(trans
, BTRFS_BLOCK_GROUP_DATA
);
3151 btrfs_end_transaction(trans
);
3160 static int insert_balance_item(struct btrfs_fs_info
*fs_info
,
3161 struct btrfs_balance_control
*bctl
)
3163 struct btrfs_root
*root
= fs_info
->tree_root
;
3164 struct btrfs_trans_handle
*trans
;
3165 struct btrfs_balance_item
*item
;
3166 struct btrfs_disk_balance_args disk_bargs
;
3167 struct btrfs_path
*path
;
3168 struct extent_buffer
*leaf
;
3169 struct btrfs_key key
;
3172 path
= btrfs_alloc_path();
3176 trans
= btrfs_start_transaction(root
, 0);
3177 if (IS_ERR(trans
)) {
3178 btrfs_free_path(path
);
3179 return PTR_ERR(trans
);
3182 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3183 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3186 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3191 leaf
= path
->nodes
[0];
3192 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3194 memzero_extent_buffer(leaf
, (unsigned long)item
, sizeof(*item
));
3196 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
3197 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
3198 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
3199 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
3200 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
3201 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
3203 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
3205 btrfs_mark_buffer_dirty(leaf
);
3207 btrfs_free_path(path
);
3208 err
= btrfs_commit_transaction(trans
);
3214 static int del_balance_item(struct btrfs_fs_info
*fs_info
)
3216 struct btrfs_root
*root
= fs_info
->tree_root
;
3217 struct btrfs_trans_handle
*trans
;
3218 struct btrfs_path
*path
;
3219 struct btrfs_key key
;
3222 path
= btrfs_alloc_path();
3226 trans
= btrfs_start_transaction(root
, 0);
3227 if (IS_ERR(trans
)) {
3228 btrfs_free_path(path
);
3229 return PTR_ERR(trans
);
3232 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3233 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3236 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3244 ret
= btrfs_del_item(trans
, root
, path
);
3246 btrfs_free_path(path
);
3247 err
= btrfs_commit_transaction(trans
);
3254 * This is a heuristic used to reduce the number of chunks balanced on
3255 * resume after balance was interrupted.
3257 static void update_balance_args(struct btrfs_balance_control
*bctl
)
3260 * Turn on soft mode for chunk types that were being converted.
3262 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3263 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3264 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3265 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3266 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3267 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3270 * Turn on usage filter if is not already used. The idea is
3271 * that chunks that we have already balanced should be
3272 * reasonably full. Don't do it for chunks that are being
3273 * converted - that will keep us from relocating unconverted
3274 * (albeit full) chunks.
3276 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3277 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3278 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3279 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3280 bctl
->data
.usage
= 90;
3282 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3283 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3284 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3285 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3286 bctl
->sys
.usage
= 90;
3288 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3289 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3290 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3291 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3292 bctl
->meta
.usage
= 90;
3297 * Clear the balance status in fs_info and delete the balance item from disk.
3299 static void reset_balance_state(struct btrfs_fs_info
*fs_info
)
3301 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3304 BUG_ON(!fs_info
->balance_ctl
);
3306 spin_lock(&fs_info
->balance_lock
);
3307 fs_info
->balance_ctl
= NULL
;
3308 spin_unlock(&fs_info
->balance_lock
);
3311 ret
= del_balance_item(fs_info
);
3313 btrfs_handle_fs_error(fs_info
, ret
, NULL
);
3317 * Balance filters. Return 1 if chunk should be filtered out
3318 * (should not be balanced).
3320 static int chunk_profiles_filter(u64 chunk_type
,
3321 struct btrfs_balance_args
*bargs
)
3323 chunk_type
= chunk_to_extended(chunk_type
) &
3324 BTRFS_EXTENDED_PROFILE_MASK
;
3326 if (bargs
->profiles
& chunk_type
)
3332 static int chunk_usage_range_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
3333 struct btrfs_balance_args
*bargs
)
3335 struct btrfs_block_group
*cache
;
3337 u64 user_thresh_min
;
3338 u64 user_thresh_max
;
3341 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3342 chunk_used
= cache
->used
;
3344 if (bargs
->usage_min
== 0)
3345 user_thresh_min
= 0;
3347 user_thresh_min
= div_factor_fine(cache
->length
,
3350 if (bargs
->usage_max
== 0)
3351 user_thresh_max
= 1;
3352 else if (bargs
->usage_max
> 100)
3353 user_thresh_max
= cache
->length
;
3355 user_thresh_max
= div_factor_fine(cache
->length
,
3358 if (user_thresh_min
<= chunk_used
&& chunk_used
< user_thresh_max
)
3361 btrfs_put_block_group(cache
);
3365 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
,
3366 u64 chunk_offset
, struct btrfs_balance_args
*bargs
)
3368 struct btrfs_block_group
*cache
;
3369 u64 chunk_used
, user_thresh
;
3372 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3373 chunk_used
= cache
->used
;
3375 if (bargs
->usage_min
== 0)
3377 else if (bargs
->usage
> 100)
3378 user_thresh
= cache
->length
;
3380 user_thresh
= div_factor_fine(cache
->length
, bargs
->usage
);
3382 if (chunk_used
< user_thresh
)
3385 btrfs_put_block_group(cache
);
3389 static int chunk_devid_filter(struct extent_buffer
*leaf
,
3390 struct btrfs_chunk
*chunk
,
3391 struct btrfs_balance_args
*bargs
)
3393 struct btrfs_stripe
*stripe
;
3394 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3397 for (i
= 0; i
< num_stripes
; i
++) {
3398 stripe
= btrfs_stripe_nr(chunk
, i
);
3399 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
3406 static u64
calc_data_stripes(u64 type
, int num_stripes
)
3408 const int index
= btrfs_bg_flags_to_raid_index(type
);
3409 const int ncopies
= btrfs_raid_array
[index
].ncopies
;
3410 const int nparity
= btrfs_raid_array
[index
].nparity
;
3413 return num_stripes
- nparity
;
3415 return num_stripes
/ ncopies
;
3418 /* [pstart, pend) */
3419 static int chunk_drange_filter(struct extent_buffer
*leaf
,
3420 struct btrfs_chunk
*chunk
,
3421 struct btrfs_balance_args
*bargs
)
3423 struct btrfs_stripe
*stripe
;
3424 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3431 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
3434 type
= btrfs_chunk_type(leaf
, chunk
);
3435 factor
= calc_data_stripes(type
, num_stripes
);
3437 for (i
= 0; i
< num_stripes
; i
++) {
3438 stripe
= btrfs_stripe_nr(chunk
, i
);
3439 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
3442 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
3443 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
3444 stripe_length
= div_u64(stripe_length
, factor
);
3446 if (stripe_offset
< bargs
->pend
&&
3447 stripe_offset
+ stripe_length
> bargs
->pstart
)
3454 /* [vstart, vend) */
3455 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
3456 struct btrfs_chunk
*chunk
,
3458 struct btrfs_balance_args
*bargs
)
3460 if (chunk_offset
< bargs
->vend
&&
3461 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
3462 /* at least part of the chunk is inside this vrange */
3468 static int chunk_stripes_range_filter(struct extent_buffer
*leaf
,
3469 struct btrfs_chunk
*chunk
,
3470 struct btrfs_balance_args
*bargs
)
3472 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3474 if (bargs
->stripes_min
<= num_stripes
3475 && num_stripes
<= bargs
->stripes_max
)
3481 static int chunk_soft_convert_filter(u64 chunk_type
,
3482 struct btrfs_balance_args
*bargs
)
3484 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
3487 chunk_type
= chunk_to_extended(chunk_type
) &
3488 BTRFS_EXTENDED_PROFILE_MASK
;
3490 if (bargs
->target
== chunk_type
)
3496 static int should_balance_chunk(struct extent_buffer
*leaf
,
3497 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
3499 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
3500 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3501 struct btrfs_balance_args
*bargs
= NULL
;
3502 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3505 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
3506 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
3510 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3511 bargs
= &bctl
->data
;
3512 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3514 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3515 bargs
= &bctl
->meta
;
3517 /* profiles filter */
3518 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
3519 chunk_profiles_filter(chunk_type
, bargs
)) {
3524 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3525 chunk_usage_filter(fs_info
, chunk_offset
, bargs
)) {
3527 } else if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3528 chunk_usage_range_filter(fs_info
, chunk_offset
, bargs
)) {
3533 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
3534 chunk_devid_filter(leaf
, chunk
, bargs
)) {
3538 /* drange filter, makes sense only with devid filter */
3539 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
3540 chunk_drange_filter(leaf
, chunk
, bargs
)) {
3545 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
3546 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3550 /* stripes filter */
3551 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_STRIPES_RANGE
) &&
3552 chunk_stripes_range_filter(leaf
, chunk
, bargs
)) {
3556 /* soft profile changing mode */
3557 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
3558 chunk_soft_convert_filter(chunk_type
, bargs
)) {
3563 * limited by count, must be the last filter
3565 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT
)) {
3566 if (bargs
->limit
== 0)
3570 } else if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT_RANGE
)) {
3572 * Same logic as the 'limit' filter; the minimum cannot be
3573 * determined here because we do not have the global information
3574 * about the count of all chunks that satisfy the filters.
3576 if (bargs
->limit_max
== 0)
3585 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
3587 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3588 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3590 struct btrfs_chunk
*chunk
;
3591 struct btrfs_path
*path
= NULL
;
3592 struct btrfs_key key
;
3593 struct btrfs_key found_key
;
3594 struct extent_buffer
*leaf
;
3597 int enospc_errors
= 0;
3598 bool counting
= true;
3599 /* The single value limit and min/max limits use the same bytes in the */
3600 u64 limit_data
= bctl
->data
.limit
;
3601 u64 limit_meta
= bctl
->meta
.limit
;
3602 u64 limit_sys
= bctl
->sys
.limit
;
3606 int chunk_reserved
= 0;
3608 path
= btrfs_alloc_path();
3614 /* zero out stat counters */
3615 spin_lock(&fs_info
->balance_lock
);
3616 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
3617 spin_unlock(&fs_info
->balance_lock
);
3621 * The single value limit and min/max limits use the same bytes
3624 bctl
->data
.limit
= limit_data
;
3625 bctl
->meta
.limit
= limit_meta
;
3626 bctl
->sys
.limit
= limit_sys
;
3628 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3629 key
.offset
= (u64
)-1;
3630 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3633 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
3634 atomic_read(&fs_info
->balance_cancel_req
)) {
3639 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
3640 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3642 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3647 * this shouldn't happen, it means the last relocate
3651 BUG(); /* FIXME break ? */
3653 ret
= btrfs_previous_item(chunk_root
, path
, 0,
3654 BTRFS_CHUNK_ITEM_KEY
);
3656 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3661 leaf
= path
->nodes
[0];
3662 slot
= path
->slots
[0];
3663 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3665 if (found_key
.objectid
!= key
.objectid
) {
3666 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3670 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3671 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3674 spin_lock(&fs_info
->balance_lock
);
3675 bctl
->stat
.considered
++;
3676 spin_unlock(&fs_info
->balance_lock
);
3679 ret
= should_balance_chunk(leaf
, chunk
, found_key
.offset
);
3681 btrfs_release_path(path
);
3683 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3688 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3689 spin_lock(&fs_info
->balance_lock
);
3690 bctl
->stat
.expected
++;
3691 spin_unlock(&fs_info
->balance_lock
);
3693 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3695 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3697 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3704 * Apply limit_min filter, no need to check if the LIMITS
3705 * filter is used, limit_min is 0 by default
3707 if (((chunk_type
& BTRFS_BLOCK_GROUP_DATA
) &&
3708 count_data
< bctl
->data
.limit_min
)
3709 || ((chunk_type
& BTRFS_BLOCK_GROUP_METADATA
) &&
3710 count_meta
< bctl
->meta
.limit_min
)
3711 || ((chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) &&
3712 count_sys
< bctl
->sys
.limit_min
)) {
3713 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3717 if (!chunk_reserved
) {
3719 * We may be relocating the only data chunk we have,
3720 * which could potentially end up with losing data's
3721 * raid profile, so lets allocate an empty one in
3724 ret
= btrfs_may_alloc_data_chunk(fs_info
,
3727 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3729 } else if (ret
== 1) {
3734 ret
= btrfs_relocate_chunk(fs_info
, found_key
.offset
);
3735 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3736 if (ret
== -ENOSPC
) {
3738 } else if (ret
== -ETXTBSY
) {
3740 "skipping relocation of block group %llu due to active swapfile",
3746 spin_lock(&fs_info
->balance_lock
);
3747 bctl
->stat
.completed
++;
3748 spin_unlock(&fs_info
->balance_lock
);
3751 if (found_key
.offset
== 0)
3753 key
.offset
= found_key
.offset
- 1;
3757 btrfs_release_path(path
);
3762 btrfs_free_path(path
);
3763 if (enospc_errors
) {
3764 btrfs_info(fs_info
, "%d enospc errors during balance",
3774 * alloc_profile_is_valid - see if a given profile is valid and reduced
3775 * @flags: profile to validate
3776 * @extended: if true @flags is treated as an extended profile
3778 static int alloc_profile_is_valid(u64 flags
, int extended
)
3780 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3781 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3783 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3785 /* 1) check that all other bits are zeroed */
3789 /* 2) see if profile is reduced */
3791 return !extended
; /* "0" is valid for usual profiles */
3793 return has_single_bit_set(flags
);
3796 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3798 /* cancel requested || normal exit path */
3799 return atomic_read(&fs_info
->balance_cancel_req
) ||
3800 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3801 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3805 * Validate target profile against allowed profiles and return true if it's OK.
3806 * Otherwise print the error message and return false.
3808 static inline int validate_convert_profile(struct btrfs_fs_info
*fs_info
,
3809 const struct btrfs_balance_args
*bargs
,
3810 u64 allowed
, const char *type
)
3812 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
3815 /* Profile is valid and does not have bits outside of the allowed set */
3816 if (alloc_profile_is_valid(bargs
->target
, 1) &&
3817 (bargs
->target
& ~allowed
) == 0)
3820 btrfs_err(fs_info
, "balance: invalid convert %s profile %s",
3821 type
, btrfs_bg_type_to_raid_name(bargs
->target
));
3826 * Fill @buf with textual description of balance filter flags @bargs, up to
3827 * @size_buf including the terminating null. The output may be trimmed if it
3828 * does not fit into the provided buffer.
3830 static void describe_balance_args(struct btrfs_balance_args
*bargs
, char *buf
,
3834 u32 size_bp
= size_buf
;
3836 u64 flags
= bargs
->flags
;
3837 char tmp_buf
[128] = {'\0'};
3842 #define CHECK_APPEND_NOARG(a) \
3844 ret = snprintf(bp, size_bp, (a)); \
3845 if (ret < 0 || ret >= size_bp) \
3846 goto out_overflow; \
3851 #define CHECK_APPEND_1ARG(a, v1) \
3853 ret = snprintf(bp, size_bp, (a), (v1)); \
3854 if (ret < 0 || ret >= size_bp) \
3855 goto out_overflow; \
3860 #define CHECK_APPEND_2ARG(a, v1, v2) \
3862 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3863 if (ret < 0 || ret >= size_bp) \
3864 goto out_overflow; \
3869 if (flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3870 CHECK_APPEND_1ARG("convert=%s,",
3871 btrfs_bg_type_to_raid_name(bargs
->target
));
3873 if (flags
& BTRFS_BALANCE_ARGS_SOFT
)
3874 CHECK_APPEND_NOARG("soft,");
3876 if (flags
& BTRFS_BALANCE_ARGS_PROFILES
) {
3877 btrfs_describe_block_groups(bargs
->profiles
, tmp_buf
,
3879 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf
);
3882 if (flags
& BTRFS_BALANCE_ARGS_USAGE
)
3883 CHECK_APPEND_1ARG("usage=%llu,", bargs
->usage
);
3885 if (flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
)
3886 CHECK_APPEND_2ARG("usage=%u..%u,",
3887 bargs
->usage_min
, bargs
->usage_max
);
3889 if (flags
& BTRFS_BALANCE_ARGS_DEVID
)
3890 CHECK_APPEND_1ARG("devid=%llu,", bargs
->devid
);
3892 if (flags
& BTRFS_BALANCE_ARGS_DRANGE
)
3893 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3894 bargs
->pstart
, bargs
->pend
);
3896 if (flags
& BTRFS_BALANCE_ARGS_VRANGE
)
3897 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3898 bargs
->vstart
, bargs
->vend
);
3900 if (flags
& BTRFS_BALANCE_ARGS_LIMIT
)
3901 CHECK_APPEND_1ARG("limit=%llu,", bargs
->limit
);
3903 if (flags
& BTRFS_BALANCE_ARGS_LIMIT_RANGE
)
3904 CHECK_APPEND_2ARG("limit=%u..%u,",
3905 bargs
->limit_min
, bargs
->limit_max
);
3907 if (flags
& BTRFS_BALANCE_ARGS_STRIPES_RANGE
)
3908 CHECK_APPEND_2ARG("stripes=%u..%u,",
3909 bargs
->stripes_min
, bargs
->stripes_max
);
3911 #undef CHECK_APPEND_2ARG
3912 #undef CHECK_APPEND_1ARG
3913 #undef CHECK_APPEND_NOARG
3917 if (size_bp
< size_buf
)
3918 buf
[size_buf
- size_bp
- 1] = '\0'; /* remove last , */
3923 static void describe_balance_start_or_resume(struct btrfs_fs_info
*fs_info
)
3925 u32 size_buf
= 1024;
3926 char tmp_buf
[192] = {'\0'};
3929 u32 size_bp
= size_buf
;
3931 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3933 buf
= kzalloc(size_buf
, GFP_KERNEL
);
3939 #define CHECK_APPEND_1ARG(a, v1) \
3941 ret = snprintf(bp, size_bp, (a), (v1)); \
3942 if (ret < 0 || ret >= size_bp) \
3943 goto out_overflow; \
3948 if (bctl
->flags
& BTRFS_BALANCE_FORCE
)
3949 CHECK_APPEND_1ARG("%s", "-f ");
3951 if (bctl
->flags
& BTRFS_BALANCE_DATA
) {
3952 describe_balance_args(&bctl
->data
, tmp_buf
, sizeof(tmp_buf
));
3953 CHECK_APPEND_1ARG("-d%s ", tmp_buf
);
3956 if (bctl
->flags
& BTRFS_BALANCE_METADATA
) {
3957 describe_balance_args(&bctl
->meta
, tmp_buf
, sizeof(tmp_buf
));
3958 CHECK_APPEND_1ARG("-m%s ", tmp_buf
);
3961 if (bctl
->flags
& BTRFS_BALANCE_SYSTEM
) {
3962 describe_balance_args(&bctl
->sys
, tmp_buf
, sizeof(tmp_buf
));
3963 CHECK_APPEND_1ARG("-s%s ", tmp_buf
);
3966 #undef CHECK_APPEND_1ARG
3970 if (size_bp
< size_buf
)
3971 buf
[size_buf
- size_bp
- 1] = '\0'; /* remove last " " */
3972 btrfs_info(fs_info
, "balance: %s %s",
3973 (bctl
->flags
& BTRFS_BALANCE_RESUME
) ?
3974 "resume" : "start", buf
);
3980 * Should be called with balance mutexe held
3982 int btrfs_balance(struct btrfs_fs_info
*fs_info
,
3983 struct btrfs_balance_control
*bctl
,
3984 struct btrfs_ioctl_balance_args
*bargs
)
3986 u64 meta_target
, data_target
;
3992 bool reducing_redundancy
;
3995 if (btrfs_fs_closing(fs_info
) ||
3996 atomic_read(&fs_info
->balance_pause_req
) ||
3997 btrfs_should_cancel_balance(fs_info
)) {
4002 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
4003 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
4007 * In case of mixed groups both data and meta should be picked,
4008 * and identical options should be given for both of them.
4010 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
4011 if (mixed
&& (bctl
->flags
& allowed
)) {
4012 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
4013 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
4014 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
4016 "balance: mixed groups data and metadata options must be the same");
4023 * rw_devices will not change at the moment, device add/delete/replace
4024 * are excluded by EXCL_OP
4026 num_devices
= fs_info
->fs_devices
->rw_devices
;
4029 * SINGLE profile on-disk has no profile bit, but in-memory we have a
4030 * special bit for it, to make it easier to distinguish. Thus we need
4031 * to set it manually, or balance would refuse the profile.
4033 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
4034 for (i
= 0; i
< ARRAY_SIZE(btrfs_raid_array
); i
++)
4035 if (num_devices
>= btrfs_raid_array
[i
].devs_min
)
4036 allowed
|= btrfs_raid_array
[i
].bg_flag
;
4038 if (!validate_convert_profile(fs_info
, &bctl
->data
, allowed
, "data") ||
4039 !validate_convert_profile(fs_info
, &bctl
->meta
, allowed
, "metadata") ||
4040 !validate_convert_profile(fs_info
, &bctl
->sys
, allowed
, "system")) {
4046 * Allow to reduce metadata or system integrity only if force set for
4047 * profiles with redundancy (copies, parity)
4050 for (i
= 0; i
< ARRAY_SIZE(btrfs_raid_array
); i
++) {
4051 if (btrfs_raid_array
[i
].ncopies
>= 2 ||
4052 btrfs_raid_array
[i
].tolerated_failures
>= 1)
4053 allowed
|= btrfs_raid_array
[i
].bg_flag
;
4056 seq
= read_seqbegin(&fs_info
->profiles_lock
);
4058 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
4059 (fs_info
->avail_system_alloc_bits
& allowed
) &&
4060 !(bctl
->sys
.target
& allowed
)) ||
4061 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
4062 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
4063 !(bctl
->meta
.target
& allowed
)))
4064 reducing_redundancy
= true;
4066 reducing_redundancy
= false;
4068 /* if we're not converting, the target field is uninitialized */
4069 meta_target
= (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) ?
4070 bctl
->meta
.target
: fs_info
->avail_metadata_alloc_bits
;
4071 data_target
= (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) ?
4072 bctl
->data
.target
: fs_info
->avail_data_alloc_bits
;
4073 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
4075 if (reducing_redundancy
) {
4076 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
4078 "balance: force reducing metadata redundancy");
4081 "balance: reduces metadata redundancy, use --force if you want this");
4087 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target
) <
4088 btrfs_get_num_tolerated_disk_barrier_failures(data_target
)) {
4090 "balance: metadata profile %s has lower redundancy than data profile %s",
4091 btrfs_bg_type_to_raid_name(meta_target
),
4092 btrfs_bg_type_to_raid_name(data_target
));
4095 if (fs_info
->send_in_progress
) {
4096 btrfs_warn_rl(fs_info
,
4097 "cannot run balance while send operations are in progress (%d in progress)",
4098 fs_info
->send_in_progress
);
4103 ret
= insert_balance_item(fs_info
, bctl
);
4104 if (ret
&& ret
!= -EEXIST
)
4107 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
4108 BUG_ON(ret
== -EEXIST
);
4109 BUG_ON(fs_info
->balance_ctl
);
4110 spin_lock(&fs_info
->balance_lock
);
4111 fs_info
->balance_ctl
= bctl
;
4112 spin_unlock(&fs_info
->balance_lock
);
4114 BUG_ON(ret
!= -EEXIST
);
4115 spin_lock(&fs_info
->balance_lock
);
4116 update_balance_args(bctl
);
4117 spin_unlock(&fs_info
->balance_lock
);
4120 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4121 set_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
);
4122 describe_balance_start_or_resume(fs_info
);
4123 mutex_unlock(&fs_info
->balance_mutex
);
4125 ret
= __btrfs_balance(fs_info
);
4127 mutex_lock(&fs_info
->balance_mutex
);
4128 if (ret
== -ECANCELED
&& atomic_read(&fs_info
->balance_pause_req
))
4129 btrfs_info(fs_info
, "balance: paused");
4130 else if (ret
== -ECANCELED
&& atomic_read(&fs_info
->balance_cancel_req
))
4131 btrfs_info(fs_info
, "balance: canceled");
4133 btrfs_info(fs_info
, "balance: ended with status: %d", ret
);
4135 clear_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
);
4138 memset(bargs
, 0, sizeof(*bargs
));
4139 btrfs_update_ioctl_balance_args(fs_info
, bargs
);
4142 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
4143 balance_need_close(fs_info
)) {
4144 reset_balance_state(fs_info
);
4145 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
4148 wake_up(&fs_info
->balance_wait_q
);
4152 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
4153 reset_balance_state(fs_info
);
4156 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
4161 static int balance_kthread(void *data
)
4163 struct btrfs_fs_info
*fs_info
= data
;
4166 mutex_lock(&fs_info
->balance_mutex
);
4167 if (fs_info
->balance_ctl
)
4168 ret
= btrfs_balance(fs_info
, fs_info
->balance_ctl
, NULL
);
4169 mutex_unlock(&fs_info
->balance_mutex
);
4174 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
4176 struct task_struct
*tsk
;
4178 mutex_lock(&fs_info
->balance_mutex
);
4179 if (!fs_info
->balance_ctl
) {
4180 mutex_unlock(&fs_info
->balance_mutex
);
4183 mutex_unlock(&fs_info
->balance_mutex
);
4185 if (btrfs_test_opt(fs_info
, SKIP_BALANCE
)) {
4186 btrfs_info(fs_info
, "balance: resume skipped");
4191 * A ro->rw remount sequence should continue with the paused balance
4192 * regardless of who pauses it, system or the user as of now, so set
4195 spin_lock(&fs_info
->balance_lock
);
4196 fs_info
->balance_ctl
->flags
|= BTRFS_BALANCE_RESUME
;
4197 spin_unlock(&fs_info
->balance_lock
);
4199 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
4200 return PTR_ERR_OR_ZERO(tsk
);
4203 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
4205 struct btrfs_balance_control
*bctl
;
4206 struct btrfs_balance_item
*item
;
4207 struct btrfs_disk_balance_args disk_bargs
;
4208 struct btrfs_path
*path
;
4209 struct extent_buffer
*leaf
;
4210 struct btrfs_key key
;
4213 path
= btrfs_alloc_path();
4217 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
4218 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
4221 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
4224 if (ret
> 0) { /* ret = -ENOENT; */
4229 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
4235 leaf
= path
->nodes
[0];
4236 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
4238 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
4239 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
4241 btrfs_balance_data(leaf
, item
, &disk_bargs
);
4242 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
4243 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
4244 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
4245 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
4246 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
4249 * This should never happen, as the paused balance state is recovered
4250 * during mount without any chance of other exclusive ops to collide.
4252 * This gives the exclusive op status to balance and keeps in paused
4253 * state until user intervention (cancel or umount). If the ownership
4254 * cannot be assigned, show a message but do not fail. The balance
4255 * is in a paused state and must have fs_info::balance_ctl properly
4258 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
))
4260 "balance: cannot set exclusive op status, resume manually");
4262 mutex_lock(&fs_info
->balance_mutex
);
4263 BUG_ON(fs_info
->balance_ctl
);
4264 spin_lock(&fs_info
->balance_lock
);
4265 fs_info
->balance_ctl
= bctl
;
4266 spin_unlock(&fs_info
->balance_lock
);
4267 mutex_unlock(&fs_info
->balance_mutex
);
4269 btrfs_free_path(path
);
4273 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
4277 mutex_lock(&fs_info
->balance_mutex
);
4278 if (!fs_info
->balance_ctl
) {
4279 mutex_unlock(&fs_info
->balance_mutex
);
4283 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4284 atomic_inc(&fs_info
->balance_pause_req
);
4285 mutex_unlock(&fs_info
->balance_mutex
);
4287 wait_event(fs_info
->balance_wait_q
,
4288 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4290 mutex_lock(&fs_info
->balance_mutex
);
4291 /* we are good with balance_ctl ripped off from under us */
4292 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4293 atomic_dec(&fs_info
->balance_pause_req
);
4298 mutex_unlock(&fs_info
->balance_mutex
);
4302 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
4304 mutex_lock(&fs_info
->balance_mutex
);
4305 if (!fs_info
->balance_ctl
) {
4306 mutex_unlock(&fs_info
->balance_mutex
);
4311 * A paused balance with the item stored on disk can be resumed at
4312 * mount time if the mount is read-write. Otherwise it's still paused
4313 * and we must not allow cancelling as it deletes the item.
4315 if (sb_rdonly(fs_info
->sb
)) {
4316 mutex_unlock(&fs_info
->balance_mutex
);
4320 atomic_inc(&fs_info
->balance_cancel_req
);
4322 * if we are running just wait and return, balance item is
4323 * deleted in btrfs_balance in this case
4325 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4326 mutex_unlock(&fs_info
->balance_mutex
);
4327 wait_event(fs_info
->balance_wait_q
,
4328 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4329 mutex_lock(&fs_info
->balance_mutex
);
4331 mutex_unlock(&fs_info
->balance_mutex
);
4333 * Lock released to allow other waiters to continue, we'll
4334 * reexamine the status again.
4336 mutex_lock(&fs_info
->balance_mutex
);
4338 if (fs_info
->balance_ctl
) {
4339 reset_balance_state(fs_info
);
4340 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
4341 btrfs_info(fs_info
, "balance: canceled");
4345 BUG_ON(fs_info
->balance_ctl
||
4346 test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4347 atomic_dec(&fs_info
->balance_cancel_req
);
4348 mutex_unlock(&fs_info
->balance_mutex
);
4352 int btrfs_uuid_scan_kthread(void *data
)
4354 struct btrfs_fs_info
*fs_info
= data
;
4355 struct btrfs_root
*root
= fs_info
->tree_root
;
4356 struct btrfs_key key
;
4357 struct btrfs_path
*path
= NULL
;
4359 struct extent_buffer
*eb
;
4361 struct btrfs_root_item root_item
;
4363 struct btrfs_trans_handle
*trans
= NULL
;
4364 bool closing
= false;
4366 path
= btrfs_alloc_path();
4373 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4377 if (btrfs_fs_closing(fs_info
)) {
4381 ret
= btrfs_search_forward(root
, &key
, path
,
4382 BTRFS_OLDEST_GENERATION
);
4389 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
||
4390 (key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
4391 key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) ||
4392 key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
4395 eb
= path
->nodes
[0];
4396 slot
= path
->slots
[0];
4397 item_size
= btrfs_item_size_nr(eb
, slot
);
4398 if (item_size
< sizeof(root_item
))
4401 read_extent_buffer(eb
, &root_item
,
4402 btrfs_item_ptr_offset(eb
, slot
),
4403 (int)sizeof(root_item
));
4404 if (btrfs_root_refs(&root_item
) == 0)
4407 if (!btrfs_is_empty_uuid(root_item
.uuid
) ||
4408 !btrfs_is_empty_uuid(root_item
.received_uuid
)) {
4412 btrfs_release_path(path
);
4414 * 1 - subvol uuid item
4415 * 1 - received_subvol uuid item
4417 trans
= btrfs_start_transaction(fs_info
->uuid_root
, 2);
4418 if (IS_ERR(trans
)) {
4419 ret
= PTR_ERR(trans
);
4427 if (!btrfs_is_empty_uuid(root_item
.uuid
)) {
4428 ret
= btrfs_uuid_tree_add(trans
, root_item
.uuid
,
4429 BTRFS_UUID_KEY_SUBVOL
,
4432 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
4438 if (!btrfs_is_empty_uuid(root_item
.received_uuid
)) {
4439 ret
= btrfs_uuid_tree_add(trans
,
4440 root_item
.received_uuid
,
4441 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4444 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
4452 ret
= btrfs_end_transaction(trans
);
4458 btrfs_release_path(path
);
4459 if (key
.offset
< (u64
)-1) {
4461 } else if (key
.type
< BTRFS_ROOT_ITEM_KEY
) {
4463 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4464 } else if (key
.objectid
< (u64
)-1) {
4466 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4475 btrfs_free_path(path
);
4476 if (trans
&& !IS_ERR(trans
))
4477 btrfs_end_transaction(trans
);
4479 btrfs_warn(fs_info
, "btrfs_uuid_scan_kthread failed %d", ret
);
4481 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN
, &fs_info
->flags
);
4482 up(&fs_info
->uuid_tree_rescan_sem
);
4486 int btrfs_create_uuid_tree(struct btrfs_fs_info
*fs_info
)
4488 struct btrfs_trans_handle
*trans
;
4489 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
4490 struct btrfs_root
*uuid_root
;
4491 struct task_struct
*task
;
4498 trans
= btrfs_start_transaction(tree_root
, 2);
4500 return PTR_ERR(trans
);
4502 uuid_root
= btrfs_create_tree(trans
, BTRFS_UUID_TREE_OBJECTID
);
4503 if (IS_ERR(uuid_root
)) {
4504 ret
= PTR_ERR(uuid_root
);
4505 btrfs_abort_transaction(trans
, ret
);
4506 btrfs_end_transaction(trans
);
4510 fs_info
->uuid_root
= uuid_root
;
4512 ret
= btrfs_commit_transaction(trans
);
4516 down(&fs_info
->uuid_tree_rescan_sem
);
4517 task
= kthread_run(btrfs_uuid_scan_kthread
, fs_info
, "btrfs-uuid");
4519 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4520 btrfs_warn(fs_info
, "failed to start uuid_scan task");
4521 up(&fs_info
->uuid_tree_rescan_sem
);
4522 return PTR_ERR(task
);
4529 * shrinking a device means finding all of the device extents past
4530 * the new size, and then following the back refs to the chunks.
4531 * The chunk relocation code actually frees the device extent
4533 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
4535 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
4536 struct btrfs_root
*root
= fs_info
->dev_root
;
4537 struct btrfs_trans_handle
*trans
;
4538 struct btrfs_dev_extent
*dev_extent
= NULL
;
4539 struct btrfs_path
*path
;
4545 bool retried
= false;
4546 struct extent_buffer
*l
;
4547 struct btrfs_key key
;
4548 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
4549 u64 old_total
= btrfs_super_total_bytes(super_copy
);
4550 u64 old_size
= btrfs_device_get_total_bytes(device
);
4554 new_size
= round_down(new_size
, fs_info
->sectorsize
);
4556 diff
= round_down(old_size
- new_size
, fs_info
->sectorsize
);
4558 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
4561 path
= btrfs_alloc_path();
4565 path
->reada
= READA_BACK
;
4567 trans
= btrfs_start_transaction(root
, 0);
4568 if (IS_ERR(trans
)) {
4569 btrfs_free_path(path
);
4570 return PTR_ERR(trans
);
4573 mutex_lock(&fs_info
->chunk_mutex
);
4575 btrfs_device_set_total_bytes(device
, new_size
);
4576 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
4577 device
->fs_devices
->total_rw_bytes
-= diff
;
4578 atomic64_sub(diff
, &fs_info
->free_chunk_space
);
4582 * Once the device's size has been set to the new size, ensure all
4583 * in-memory chunks are synced to disk so that the loop below sees them
4584 * and relocates them accordingly.
4586 if (contains_pending_extent(device
, &start
, diff
)) {
4587 mutex_unlock(&fs_info
->chunk_mutex
);
4588 ret
= btrfs_commit_transaction(trans
);
4592 mutex_unlock(&fs_info
->chunk_mutex
);
4593 btrfs_end_transaction(trans
);
4597 key
.objectid
= device
->devid
;
4598 key
.offset
= (u64
)-1;
4599 key
.type
= BTRFS_DEV_EXTENT_KEY
;
4602 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
4603 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4605 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4609 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
4611 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4616 btrfs_release_path(path
);
4621 slot
= path
->slots
[0];
4622 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
4624 if (key
.objectid
!= device
->devid
) {
4625 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4626 btrfs_release_path(path
);
4630 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
4631 length
= btrfs_dev_extent_length(l
, dev_extent
);
4633 if (key
.offset
+ length
<= new_size
) {
4634 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4635 btrfs_release_path(path
);
4639 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
4640 btrfs_release_path(path
);
4643 * We may be relocating the only data chunk we have,
4644 * which could potentially end up with losing data's
4645 * raid profile, so lets allocate an empty one in
4648 ret
= btrfs_may_alloc_data_chunk(fs_info
, chunk_offset
);
4650 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4654 ret
= btrfs_relocate_chunk(fs_info
, chunk_offset
);
4655 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4656 if (ret
== -ENOSPC
) {
4659 if (ret
== -ETXTBSY
) {
4661 "could not shrink block group %llu due to active swapfile",
4666 } while (key
.offset
-- > 0);
4668 if (failed
&& !retried
) {
4672 } else if (failed
&& retried
) {
4677 /* Shrinking succeeded, else we would be at "done". */
4678 trans
= btrfs_start_transaction(root
, 0);
4679 if (IS_ERR(trans
)) {
4680 ret
= PTR_ERR(trans
);
4684 mutex_lock(&fs_info
->chunk_mutex
);
4685 btrfs_device_set_disk_total_bytes(device
, new_size
);
4686 if (list_empty(&device
->post_commit_list
))
4687 list_add_tail(&device
->post_commit_list
,
4688 &trans
->transaction
->dev_update_list
);
4690 WARN_ON(diff
> old_total
);
4691 btrfs_set_super_total_bytes(super_copy
,
4692 round_down(old_total
- diff
, fs_info
->sectorsize
));
4693 mutex_unlock(&fs_info
->chunk_mutex
);
4695 /* Now btrfs_update_device() will change the on-disk size. */
4696 ret
= btrfs_update_device(trans
, device
);
4698 btrfs_abort_transaction(trans
, ret
);
4699 btrfs_end_transaction(trans
);
4701 ret
= btrfs_commit_transaction(trans
);
4704 btrfs_free_path(path
);
4706 mutex_lock(&fs_info
->chunk_mutex
);
4707 btrfs_device_set_total_bytes(device
, old_size
);
4708 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
4709 device
->fs_devices
->total_rw_bytes
+= diff
;
4710 atomic64_add(diff
, &fs_info
->free_chunk_space
);
4711 mutex_unlock(&fs_info
->chunk_mutex
);
4716 static int btrfs_add_system_chunk(struct btrfs_fs_info
*fs_info
,
4717 struct btrfs_key
*key
,
4718 struct btrfs_chunk
*chunk
, int item_size
)
4720 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
4721 struct btrfs_disk_key disk_key
;
4725 mutex_lock(&fs_info
->chunk_mutex
);
4726 array_size
= btrfs_super_sys_array_size(super_copy
);
4727 if (array_size
+ item_size
+ sizeof(disk_key
)
4728 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
) {
4729 mutex_unlock(&fs_info
->chunk_mutex
);
4733 ptr
= super_copy
->sys_chunk_array
+ array_size
;
4734 btrfs_cpu_key_to_disk(&disk_key
, key
);
4735 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
4736 ptr
+= sizeof(disk_key
);
4737 memcpy(ptr
, chunk
, item_size
);
4738 item_size
+= sizeof(disk_key
);
4739 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
4740 mutex_unlock(&fs_info
->chunk_mutex
);
4746 * sort the devices in descending order by max_avail, total_avail
4748 static int btrfs_cmp_device_info(const void *a
, const void *b
)
4750 const struct btrfs_device_info
*di_a
= a
;
4751 const struct btrfs_device_info
*di_b
= b
;
4753 if (di_a
->max_avail
> di_b
->max_avail
)
4755 if (di_a
->max_avail
< di_b
->max_avail
)
4757 if (di_a
->total_avail
> di_b
->total_avail
)
4759 if (di_a
->total_avail
< di_b
->total_avail
)
4764 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4766 if (!(type
& BTRFS_BLOCK_GROUP_RAID56_MASK
))
4769 btrfs_set_fs_incompat(info
, RAID56
);
4772 static void check_raid1c34_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4774 if (!(type
& (BTRFS_BLOCK_GROUP_RAID1C3
| BTRFS_BLOCK_GROUP_RAID1C4
)))
4777 btrfs_set_fs_incompat(info
, RAID1C34
);
4781 * Structure used internally for __btrfs_alloc_chunk() function.
4782 * Wraps needed parameters.
4784 struct alloc_chunk_ctl
{
4787 /* Total number of stripes to allocate */
4789 /* sub_stripes info for map */
4791 /* Stripes per device */
4793 /* Maximum number of devices to use */
4795 /* Minimum number of devices to use */
4797 /* ndevs has to be a multiple of this */
4799 /* Number of copies */
4801 /* Number of stripes worth of bytes to store parity information */
4803 u64 max_stripe_size
;
4811 static void init_alloc_chunk_ctl_policy_regular(
4812 struct btrfs_fs_devices
*fs_devices
,
4813 struct alloc_chunk_ctl
*ctl
)
4815 u64 type
= ctl
->type
;
4817 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
4818 ctl
->max_stripe_size
= SZ_1G
;
4819 ctl
->max_chunk_size
= BTRFS_MAX_DATA_CHUNK_SIZE
;
4820 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
4821 /* For larger filesystems, use larger metadata chunks */
4822 if (fs_devices
->total_rw_bytes
> 50ULL * SZ_1G
)
4823 ctl
->max_stripe_size
= SZ_1G
;
4825 ctl
->max_stripe_size
= SZ_256M
;
4826 ctl
->max_chunk_size
= ctl
->max_stripe_size
;
4827 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4828 ctl
->max_stripe_size
= SZ_32M
;
4829 ctl
->max_chunk_size
= 2 * ctl
->max_stripe_size
;
4830 ctl
->devs_max
= min_t(int, ctl
->devs_max
,
4831 BTRFS_MAX_DEVS_SYS_CHUNK
);
4836 /* We don't want a chunk larger than 10% of writable space */
4837 ctl
->max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
4838 ctl
->max_chunk_size
);
4839 ctl
->dev_extent_min
= BTRFS_STRIPE_LEN
* ctl
->dev_stripes
;
4842 static void init_alloc_chunk_ctl(struct btrfs_fs_devices
*fs_devices
,
4843 struct alloc_chunk_ctl
*ctl
)
4845 int index
= btrfs_bg_flags_to_raid_index(ctl
->type
);
4847 ctl
->sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
4848 ctl
->dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
4849 ctl
->devs_max
= btrfs_raid_array
[index
].devs_max
;
4851 ctl
->devs_max
= BTRFS_MAX_DEVS(fs_devices
->fs_info
);
4852 ctl
->devs_min
= btrfs_raid_array
[index
].devs_min
;
4853 ctl
->devs_increment
= btrfs_raid_array
[index
].devs_increment
;
4854 ctl
->ncopies
= btrfs_raid_array
[index
].ncopies
;
4855 ctl
->nparity
= btrfs_raid_array
[index
].nparity
;
4858 switch (fs_devices
->chunk_alloc_policy
) {
4859 case BTRFS_CHUNK_ALLOC_REGULAR
:
4860 init_alloc_chunk_ctl_policy_regular(fs_devices
, ctl
);
4867 static int gather_device_info(struct btrfs_fs_devices
*fs_devices
,
4868 struct alloc_chunk_ctl
*ctl
,
4869 struct btrfs_device_info
*devices_info
)
4871 struct btrfs_fs_info
*info
= fs_devices
->fs_info
;
4872 struct btrfs_device
*device
;
4874 u64 dev_extent_want
= ctl
->max_stripe_size
* ctl
->dev_stripes
;
4881 * in the first pass through the devices list, we gather information
4882 * about the available holes on each device.
4884 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
4885 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
4887 "BTRFS: read-only device in alloc_list\n");
4891 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
4892 &device
->dev_state
) ||
4893 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
4896 if (device
->total_bytes
> device
->bytes_used
)
4897 total_avail
= device
->total_bytes
- device
->bytes_used
;
4901 /* If there is no space on this device, skip it. */
4902 if (total_avail
< ctl
->dev_extent_min
)
4905 ret
= find_free_dev_extent(device
, dev_extent_want
, &dev_offset
,
4907 if (ret
&& ret
!= -ENOSPC
)
4911 max_avail
= dev_extent_want
;
4913 if (max_avail
< ctl
->dev_extent_min
) {
4914 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
4916 "%s: devid %llu has no free space, have=%llu want=%llu",
4917 __func__
, device
->devid
, max_avail
,
4918 ctl
->dev_extent_min
);
4922 if (ndevs
== fs_devices
->rw_devices
) {
4923 WARN(1, "%s: found more than %llu devices\n",
4924 __func__
, fs_devices
->rw_devices
);
4927 devices_info
[ndevs
].dev_offset
= dev_offset
;
4928 devices_info
[ndevs
].max_avail
= max_avail
;
4929 devices_info
[ndevs
].total_avail
= total_avail
;
4930 devices_info
[ndevs
].dev
= device
;
4936 * now sort the devices by hole size / available space
4938 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
4939 btrfs_cmp_device_info
, NULL
);
4944 static int decide_stripe_size_regular(struct alloc_chunk_ctl
*ctl
,
4945 struct btrfs_device_info
*devices_info
)
4947 /* Number of stripes that count for block group size */
4951 * The primary goal is to maximize the number of stripes, so use as
4952 * many devices as possible, even if the stripes are not maximum sized.
4954 * The DUP profile stores more than one stripe per device, the
4955 * max_avail is the total size so we have to adjust.
4957 ctl
->stripe_size
= div_u64(devices_info
[ctl
->ndevs
- 1].max_avail
,
4959 ctl
->num_stripes
= ctl
->ndevs
* ctl
->dev_stripes
;
4961 /* This will have to be fixed for RAID1 and RAID10 over more drives */
4962 data_stripes
= (ctl
->num_stripes
- ctl
->nparity
) / ctl
->ncopies
;
4965 * Use the number of data stripes to figure out how big this chunk is
4966 * really going to be in terms of logical address space, and compare
4967 * that answer with the max chunk size. If it's higher, we try to
4968 * reduce stripe_size.
4970 if (ctl
->stripe_size
* data_stripes
> ctl
->max_chunk_size
) {
4972 * Reduce stripe_size, round it up to a 16MB boundary again and
4973 * then use it, unless it ends up being even bigger than the
4974 * previous value we had already.
4976 ctl
->stripe_size
= min(round_up(div_u64(ctl
->max_chunk_size
,
4977 data_stripes
), SZ_16M
),
4981 /* Align to BTRFS_STRIPE_LEN */
4982 ctl
->stripe_size
= round_down(ctl
->stripe_size
, BTRFS_STRIPE_LEN
);
4983 ctl
->chunk_size
= ctl
->stripe_size
* data_stripes
;
4988 static int decide_stripe_size(struct btrfs_fs_devices
*fs_devices
,
4989 struct alloc_chunk_ctl
*ctl
,
4990 struct btrfs_device_info
*devices_info
)
4992 struct btrfs_fs_info
*info
= fs_devices
->fs_info
;
4995 * Round down to number of usable stripes, devs_increment can be any
4996 * number so we can't use round_down() that requires power of 2, while
4997 * rounddown is safe.
4999 ctl
->ndevs
= rounddown(ctl
->ndevs
, ctl
->devs_increment
);
5001 if (ctl
->ndevs
< ctl
->devs_min
) {
5002 if (btrfs_test_opt(info
, ENOSPC_DEBUG
)) {
5004 "%s: not enough devices with free space: have=%d minimum required=%d",
5005 __func__
, ctl
->ndevs
, ctl
->devs_min
);
5010 ctl
->ndevs
= min(ctl
->ndevs
, ctl
->devs_max
);
5012 switch (fs_devices
->chunk_alloc_policy
) {
5013 case BTRFS_CHUNK_ALLOC_REGULAR
:
5014 return decide_stripe_size_regular(ctl
, devices_info
);
5020 static int create_chunk(struct btrfs_trans_handle
*trans
,
5021 struct alloc_chunk_ctl
*ctl
,
5022 struct btrfs_device_info
*devices_info
)
5024 struct btrfs_fs_info
*info
= trans
->fs_info
;
5025 struct map_lookup
*map
= NULL
;
5026 struct extent_map_tree
*em_tree
;
5027 struct extent_map
*em
;
5028 u64 start
= ctl
->start
;
5029 u64 type
= ctl
->type
;
5034 map
= kmalloc(map_lookup_size(ctl
->num_stripes
), GFP_NOFS
);
5037 map
->num_stripes
= ctl
->num_stripes
;
5039 for (i
= 0; i
< ctl
->ndevs
; ++i
) {
5040 for (j
= 0; j
< ctl
->dev_stripes
; ++j
) {
5041 int s
= i
* ctl
->dev_stripes
+ j
;
5042 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
5043 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
5044 j
* ctl
->stripe_size
;
5047 map
->stripe_len
= BTRFS_STRIPE_LEN
;
5048 map
->io_align
= BTRFS_STRIPE_LEN
;
5049 map
->io_width
= BTRFS_STRIPE_LEN
;
5051 map
->sub_stripes
= ctl
->sub_stripes
;
5053 trace_btrfs_chunk_alloc(info
, map
, start
, ctl
->chunk_size
);
5055 em
= alloc_extent_map();
5060 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
5061 em
->map_lookup
= map
;
5063 em
->len
= ctl
->chunk_size
;
5064 em
->block_start
= 0;
5065 em
->block_len
= em
->len
;
5066 em
->orig_block_len
= ctl
->stripe_size
;
5068 em_tree
= &info
->mapping_tree
;
5069 write_lock(&em_tree
->lock
);
5070 ret
= add_extent_mapping(em_tree
, em
, 0);
5072 write_unlock(&em_tree
->lock
);
5073 free_extent_map(em
);
5076 write_unlock(&em_tree
->lock
);
5078 ret
= btrfs_make_block_group(trans
, 0, type
, start
, ctl
->chunk_size
);
5080 goto error_del_extent
;
5082 for (i
= 0; i
< map
->num_stripes
; i
++) {
5083 struct btrfs_device
*dev
= map
->stripes
[i
].dev
;
5085 btrfs_device_set_bytes_used(dev
,
5086 dev
->bytes_used
+ ctl
->stripe_size
);
5087 if (list_empty(&dev
->post_commit_list
))
5088 list_add_tail(&dev
->post_commit_list
,
5089 &trans
->transaction
->dev_update_list
);
5092 atomic64_sub(ctl
->stripe_size
* map
->num_stripes
,
5093 &info
->free_chunk_space
);
5095 free_extent_map(em
);
5096 check_raid56_incompat_flag(info
, type
);
5097 check_raid1c34_incompat_flag(info
, type
);
5102 write_lock(&em_tree
->lock
);
5103 remove_extent_mapping(em_tree
, em
);
5104 write_unlock(&em_tree
->lock
);
5106 /* One for our allocation */
5107 free_extent_map(em
);
5108 /* One for the tree reference */
5109 free_extent_map(em
);
5114 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
, u64 type
)
5116 struct btrfs_fs_info
*info
= trans
->fs_info
;
5117 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
5118 struct btrfs_device_info
*devices_info
= NULL
;
5119 struct alloc_chunk_ctl ctl
;
5122 lockdep_assert_held(&info
->chunk_mutex
);
5124 if (!alloc_profile_is_valid(type
, 0)) {
5129 if (list_empty(&fs_devices
->alloc_list
)) {
5130 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
5131 btrfs_debug(info
, "%s: no writable device", __func__
);
5135 if (!(type
& BTRFS_BLOCK_GROUP_TYPE_MASK
)) {
5136 btrfs_err(info
, "invalid chunk type 0x%llx requested", type
);
5141 ctl
.start
= find_next_chunk(info
);
5143 init_alloc_chunk_ctl(fs_devices
, &ctl
);
5145 devices_info
= kcalloc(fs_devices
->rw_devices
, sizeof(*devices_info
),
5150 ret
= gather_device_info(fs_devices
, &ctl
, devices_info
);
5154 ret
= decide_stripe_size(fs_devices
, &ctl
, devices_info
);
5158 ret
= create_chunk(trans
, &ctl
, devices_info
);
5161 kfree(devices_info
);
5166 * Chunk allocation falls into two parts. The first part does work
5167 * that makes the new allocated chunk usable, but does not do any operation
5168 * that modifies the chunk tree. The second part does the work that
5169 * requires modifying the chunk tree. This division is important for the
5170 * bootstrap process of adding storage to a seed btrfs.
5172 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
5173 u64 chunk_offset
, u64 chunk_size
)
5175 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
5176 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
5177 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
5178 struct btrfs_key key
;
5179 struct btrfs_device
*device
;
5180 struct btrfs_chunk
*chunk
;
5181 struct btrfs_stripe
*stripe
;
5182 struct extent_map
*em
;
5183 struct map_lookup
*map
;
5190 em
= btrfs_get_chunk_map(fs_info
, chunk_offset
, chunk_size
);
5194 map
= em
->map_lookup
;
5195 item_size
= btrfs_chunk_item_size(map
->num_stripes
);
5196 stripe_size
= em
->orig_block_len
;
5198 chunk
= kzalloc(item_size
, GFP_NOFS
);
5205 * Take the device list mutex to prevent races with the final phase of
5206 * a device replace operation that replaces the device object associated
5207 * with the map's stripes, because the device object's id can change
5208 * at any time during that final phase of the device replace operation
5209 * (dev-replace.c:btrfs_dev_replace_finishing()).
5211 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
5212 for (i
= 0; i
< map
->num_stripes
; i
++) {
5213 device
= map
->stripes
[i
].dev
;
5214 dev_offset
= map
->stripes
[i
].physical
;
5216 ret
= btrfs_update_device(trans
, device
);
5219 ret
= btrfs_alloc_dev_extent(trans
, device
, chunk_offset
,
5220 dev_offset
, stripe_size
);
5225 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
5229 stripe
= &chunk
->stripe
;
5230 for (i
= 0; i
< map
->num_stripes
; i
++) {
5231 device
= map
->stripes
[i
].dev
;
5232 dev_offset
= map
->stripes
[i
].physical
;
5234 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
5235 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
5236 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
5239 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
5241 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
5242 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
5243 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
5244 btrfs_set_stack_chunk_type(chunk
, map
->type
);
5245 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
5246 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
5247 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
5248 btrfs_set_stack_chunk_sector_size(chunk
, fs_info
->sectorsize
);
5249 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
5251 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
5252 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
5253 key
.offset
= chunk_offset
;
5255 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
5256 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
5258 * TODO: Cleanup of inserted chunk root in case of
5261 ret
= btrfs_add_system_chunk(fs_info
, &key
, chunk
, item_size
);
5266 free_extent_map(em
);
5270 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
)
5272 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
5276 alloc_profile
= btrfs_metadata_alloc_profile(fs_info
);
5277 ret
= btrfs_alloc_chunk(trans
, alloc_profile
);
5281 alloc_profile
= btrfs_system_alloc_profile(fs_info
);
5282 ret
= btrfs_alloc_chunk(trans
, alloc_profile
);
5286 static inline int btrfs_chunk_max_errors(struct map_lookup
*map
)
5288 const int index
= btrfs_bg_flags_to_raid_index(map
->type
);
5290 return btrfs_raid_array
[index
].tolerated_failures
;
5293 int btrfs_chunk_readonly(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
5295 struct extent_map
*em
;
5296 struct map_lookup
*map
;
5301 em
= btrfs_get_chunk_map(fs_info
, chunk_offset
, 1);
5305 map
= em
->map_lookup
;
5306 for (i
= 0; i
< map
->num_stripes
; i
++) {
5307 if (test_bit(BTRFS_DEV_STATE_MISSING
,
5308 &map
->stripes
[i
].dev
->dev_state
)) {
5312 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
,
5313 &map
->stripes
[i
].dev
->dev_state
)) {
5320 * If the number of missing devices is larger than max errors,
5321 * we can not write the data into that chunk successfully, so
5324 if (miss_ndevs
> btrfs_chunk_max_errors(map
))
5327 free_extent_map(em
);
5331 void btrfs_mapping_tree_free(struct extent_map_tree
*tree
)
5333 struct extent_map
*em
;
5336 write_lock(&tree
->lock
);
5337 em
= lookup_extent_mapping(tree
, 0, (u64
)-1);
5339 remove_extent_mapping(tree
, em
);
5340 write_unlock(&tree
->lock
);
5344 free_extent_map(em
);
5345 /* once for the tree */
5346 free_extent_map(em
);
5350 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
5352 struct extent_map
*em
;
5353 struct map_lookup
*map
;
5356 em
= btrfs_get_chunk_map(fs_info
, logical
, len
);
5359 * We could return errors for these cases, but that could get
5360 * ugly and we'd probably do the same thing which is just not do
5361 * anything else and exit, so return 1 so the callers don't try
5362 * to use other copies.
5366 map
= em
->map_lookup
;
5367 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1_MASK
))
5368 ret
= map
->num_stripes
;
5369 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5370 ret
= map
->sub_stripes
;
5371 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
5373 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5375 * There could be two corrupted data stripes, we need
5376 * to loop retry in order to rebuild the correct data.
5378 * Fail a stripe at a time on every retry except the
5379 * stripe under reconstruction.
5381 ret
= map
->num_stripes
;
5384 free_extent_map(em
);
5386 down_read(&fs_info
->dev_replace
.rwsem
);
5387 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
) &&
5388 fs_info
->dev_replace
.tgtdev
)
5390 up_read(&fs_info
->dev_replace
.rwsem
);
5395 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info
*fs_info
,
5398 struct extent_map
*em
;
5399 struct map_lookup
*map
;
5400 unsigned long len
= fs_info
->sectorsize
;
5402 em
= btrfs_get_chunk_map(fs_info
, logical
, len
);
5404 if (!WARN_ON(IS_ERR(em
))) {
5405 map
= em
->map_lookup
;
5406 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
5407 len
= map
->stripe_len
* nr_data_stripes(map
);
5408 free_extent_map(em
);
5413 int btrfs_is_parity_mirror(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
5415 struct extent_map
*em
;
5416 struct map_lookup
*map
;
5419 em
= btrfs_get_chunk_map(fs_info
, logical
, len
);
5421 if(!WARN_ON(IS_ERR(em
))) {
5422 map
= em
->map_lookup
;
5423 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
5425 free_extent_map(em
);
5430 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
5431 struct map_lookup
*map
, int first
,
5432 int dev_replace_is_ongoing
)
5436 int preferred_mirror
;
5438 struct btrfs_device
*srcdev
;
5441 (BTRFS_BLOCK_GROUP_RAID1_MASK
| BTRFS_BLOCK_GROUP_RAID10
)));
5443 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5444 num_stripes
= map
->sub_stripes
;
5446 num_stripes
= map
->num_stripes
;
5448 preferred_mirror
= first
+ current
->pid
% num_stripes
;
5450 if (dev_replace_is_ongoing
&&
5451 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
5452 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
5453 srcdev
= fs_info
->dev_replace
.srcdev
;
5458 * try to avoid the drive that is the source drive for a
5459 * dev-replace procedure, only choose it if no other non-missing
5460 * mirror is available
5462 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
5463 if (map
->stripes
[preferred_mirror
].dev
->bdev
&&
5464 (tolerance
|| map
->stripes
[preferred_mirror
].dev
!= srcdev
))
5465 return preferred_mirror
;
5466 for (i
= first
; i
< first
+ num_stripes
; i
++) {
5467 if (map
->stripes
[i
].dev
->bdev
&&
5468 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
5473 /* we couldn't find one that doesn't fail. Just return something
5474 * and the io error handling code will clean up eventually
5476 return preferred_mirror
;
5479 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
5480 static void sort_parity_stripes(struct btrfs_bio
*bbio
, int num_stripes
)
5487 for (i
= 0; i
< num_stripes
- 1; i
++) {
5488 /* Swap if parity is on a smaller index */
5489 if (bbio
->raid_map
[i
] > bbio
->raid_map
[i
+ 1]) {
5490 swap(bbio
->stripes
[i
], bbio
->stripes
[i
+ 1]);
5491 swap(bbio
->raid_map
[i
], bbio
->raid_map
[i
+ 1]);
5498 static struct btrfs_bio
*alloc_btrfs_bio(int total_stripes
, int real_stripes
)
5500 struct btrfs_bio
*bbio
= kzalloc(
5501 /* the size of the btrfs_bio */
5502 sizeof(struct btrfs_bio
) +
5503 /* plus the variable array for the stripes */
5504 sizeof(struct btrfs_bio_stripe
) * (total_stripes
) +
5505 /* plus the variable array for the tgt dev */
5506 sizeof(int) * (real_stripes
) +
5508 * plus the raid_map, which includes both the tgt dev
5511 sizeof(u64
) * (total_stripes
),
5512 GFP_NOFS
|__GFP_NOFAIL
);
5514 atomic_set(&bbio
->error
, 0);
5515 refcount_set(&bbio
->refs
, 1);
5520 void btrfs_get_bbio(struct btrfs_bio
*bbio
)
5522 WARN_ON(!refcount_read(&bbio
->refs
));
5523 refcount_inc(&bbio
->refs
);
5526 void btrfs_put_bbio(struct btrfs_bio
*bbio
)
5530 if (refcount_dec_and_test(&bbio
->refs
))
5534 /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5536 * Please note that, discard won't be sent to target device of device
5539 static int __btrfs_map_block_for_discard(struct btrfs_fs_info
*fs_info
,
5540 u64 logical
, u64
*length_ret
,
5541 struct btrfs_bio
**bbio_ret
)
5543 struct extent_map
*em
;
5544 struct map_lookup
*map
;
5545 struct btrfs_bio
*bbio
;
5546 u64 length
= *length_ret
;
5550 u64 stripe_end_offset
;
5557 u32 sub_stripes
= 0;
5558 u64 stripes_per_dev
= 0;
5559 u32 remaining_stripes
= 0;
5560 u32 last_stripe
= 0;
5564 /* discard always return a bbio */
5567 em
= btrfs_get_chunk_map(fs_info
, logical
, length
);
5571 map
= em
->map_lookup
;
5572 /* we don't discard raid56 yet */
5573 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5578 offset
= logical
- em
->start
;
5579 length
= min_t(u64
, em
->start
+ em
->len
- logical
, length
);
5580 *length_ret
= length
;
5582 stripe_len
= map
->stripe_len
;
5584 * stripe_nr counts the total number of stripes we have to stride
5585 * to get to this block
5587 stripe_nr
= div64_u64(offset
, stripe_len
);
5589 /* stripe_offset is the offset of this block in its stripe */
5590 stripe_offset
= offset
- stripe_nr
* stripe_len
;
5592 stripe_nr_end
= round_up(offset
+ length
, map
->stripe_len
);
5593 stripe_nr_end
= div64_u64(stripe_nr_end
, map
->stripe_len
);
5594 stripe_cnt
= stripe_nr_end
- stripe_nr
;
5595 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
5598 * after this, stripe_nr is the number of stripes on this
5599 * device we have to walk to find the data, and stripe_index is
5600 * the number of our device in the stripe array
5604 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5605 BTRFS_BLOCK_GROUP_RAID10
)) {
5606 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5609 sub_stripes
= map
->sub_stripes
;
5611 factor
= map
->num_stripes
/ sub_stripes
;
5612 num_stripes
= min_t(u64
, map
->num_stripes
,
5613 sub_stripes
* stripe_cnt
);
5614 stripe_nr
= div_u64_rem(stripe_nr
, factor
, &stripe_index
);
5615 stripe_index
*= sub_stripes
;
5616 stripes_per_dev
= div_u64_rem(stripe_cnt
, factor
,
5617 &remaining_stripes
);
5618 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
5619 last_stripe
*= sub_stripes
;
5620 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1_MASK
|
5621 BTRFS_BLOCK_GROUP_DUP
)) {
5622 num_stripes
= map
->num_stripes
;
5624 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5628 bbio
= alloc_btrfs_bio(num_stripes
, 0);
5634 for (i
= 0; i
< num_stripes
; i
++) {
5635 bbio
->stripes
[i
].physical
=
5636 map
->stripes
[stripe_index
].physical
+
5637 stripe_offset
+ stripe_nr
* map
->stripe_len
;
5638 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
5640 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5641 BTRFS_BLOCK_GROUP_RAID10
)) {
5642 bbio
->stripes
[i
].length
= stripes_per_dev
*
5645 if (i
/ sub_stripes
< remaining_stripes
)
5646 bbio
->stripes
[i
].length
+=
5650 * Special for the first stripe and
5653 * |-------|...|-------|
5657 if (i
< sub_stripes
)
5658 bbio
->stripes
[i
].length
-=
5661 if (stripe_index
>= last_stripe
&&
5662 stripe_index
<= (last_stripe
+
5664 bbio
->stripes
[i
].length
-=
5667 if (i
== sub_stripes
- 1)
5670 bbio
->stripes
[i
].length
= length
;
5674 if (stripe_index
== map
->num_stripes
) {
5681 bbio
->map_type
= map
->type
;
5682 bbio
->num_stripes
= num_stripes
;
5684 free_extent_map(em
);
5689 * In dev-replace case, for repair case (that's the only case where the mirror
5690 * is selected explicitly when calling btrfs_map_block), blocks left of the
5691 * left cursor can also be read from the target drive.
5693 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5695 * For READ, it also needs to be supported using the same mirror number.
5697 * If the requested block is not left of the left cursor, EIO is returned. This
5698 * can happen because btrfs_num_copies() returns one more in the dev-replace
5701 static int get_extra_mirror_from_replace(struct btrfs_fs_info
*fs_info
,
5702 u64 logical
, u64 length
,
5703 u64 srcdev_devid
, int *mirror_num
,
5706 struct btrfs_bio
*bbio
= NULL
;
5708 int index_srcdev
= 0;
5710 u64 physical_of_found
= 0;
5714 ret
= __btrfs_map_block(fs_info
, BTRFS_MAP_GET_READ_MIRRORS
,
5715 logical
, &length
, &bbio
, 0, 0);
5717 ASSERT(bbio
== NULL
);
5721 num_stripes
= bbio
->num_stripes
;
5722 if (*mirror_num
> num_stripes
) {
5724 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5725 * that means that the requested area is not left of the left
5728 btrfs_put_bbio(bbio
);
5733 * process the rest of the function using the mirror_num of the source
5734 * drive. Therefore look it up first. At the end, patch the device
5735 * pointer to the one of the target drive.
5737 for (i
= 0; i
< num_stripes
; i
++) {
5738 if (bbio
->stripes
[i
].dev
->devid
!= srcdev_devid
)
5742 * In case of DUP, in order to keep it simple, only add the
5743 * mirror with the lowest physical address
5746 physical_of_found
<= bbio
->stripes
[i
].physical
)
5751 physical_of_found
= bbio
->stripes
[i
].physical
;
5754 btrfs_put_bbio(bbio
);
5760 *mirror_num
= index_srcdev
+ 1;
5761 *physical
= physical_of_found
;
5765 static void handle_ops_on_dev_replace(enum btrfs_map_op op
,
5766 struct btrfs_bio
**bbio_ret
,
5767 struct btrfs_dev_replace
*dev_replace
,
5768 int *num_stripes_ret
, int *max_errors_ret
)
5770 struct btrfs_bio
*bbio
= *bbio_ret
;
5771 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5772 int tgtdev_indexes
= 0;
5773 int num_stripes
= *num_stripes_ret
;
5774 int max_errors
= *max_errors_ret
;
5777 if (op
== BTRFS_MAP_WRITE
) {
5778 int index_where_to_add
;
5781 * duplicate the write operations while the dev replace
5782 * procedure is running. Since the copying of the old disk to
5783 * the new disk takes place at run time while the filesystem is
5784 * mounted writable, the regular write operations to the old
5785 * disk have to be duplicated to go to the new disk as well.
5787 * Note that device->missing is handled by the caller, and that
5788 * the write to the old disk is already set up in the stripes
5791 index_where_to_add
= num_stripes
;
5792 for (i
= 0; i
< num_stripes
; i
++) {
5793 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5794 /* write to new disk, too */
5795 struct btrfs_bio_stripe
*new =
5796 bbio
->stripes
+ index_where_to_add
;
5797 struct btrfs_bio_stripe
*old
=
5800 new->physical
= old
->physical
;
5801 new->length
= old
->length
;
5802 new->dev
= dev_replace
->tgtdev
;
5803 bbio
->tgtdev_map
[i
] = index_where_to_add
;
5804 index_where_to_add
++;
5809 num_stripes
= index_where_to_add
;
5810 } else if (op
== BTRFS_MAP_GET_READ_MIRRORS
) {
5811 int index_srcdev
= 0;
5813 u64 physical_of_found
= 0;
5816 * During the dev-replace procedure, the target drive can also
5817 * be used to read data in case it is needed to repair a corrupt
5818 * block elsewhere. This is possible if the requested area is
5819 * left of the left cursor. In this area, the target drive is a
5820 * full copy of the source drive.
5822 for (i
= 0; i
< num_stripes
; i
++) {
5823 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5825 * In case of DUP, in order to keep it simple,
5826 * only add the mirror with the lowest physical
5830 physical_of_found
<=
5831 bbio
->stripes
[i
].physical
)
5835 physical_of_found
= bbio
->stripes
[i
].physical
;
5839 struct btrfs_bio_stripe
*tgtdev_stripe
=
5840 bbio
->stripes
+ num_stripes
;
5842 tgtdev_stripe
->physical
= physical_of_found
;
5843 tgtdev_stripe
->length
=
5844 bbio
->stripes
[index_srcdev
].length
;
5845 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
5846 bbio
->tgtdev_map
[index_srcdev
] = num_stripes
;
5853 *num_stripes_ret
= num_stripes
;
5854 *max_errors_ret
= max_errors
;
5855 bbio
->num_tgtdevs
= tgtdev_indexes
;
5859 static bool need_full_stripe(enum btrfs_map_op op
)
5861 return (op
== BTRFS_MAP_WRITE
|| op
== BTRFS_MAP_GET_READ_MIRRORS
);
5865 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5866 * tuple. This information is used to calculate how big a
5867 * particular bio can get before it straddles a stripe.
5869 * @fs_info - the filesystem
5870 * @logical - address that we want to figure out the geometry of
5871 * @len - the length of IO we are going to perform, starting at @logical
5872 * @op - type of operation - write or read
5873 * @io_geom - pointer used to return values
5875 * Returns < 0 in case a chunk for the given logical address cannot be found,
5876 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5878 int btrfs_get_io_geometry(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
5879 u64 logical
, u64 len
, struct btrfs_io_geometry
*io_geom
)
5881 struct extent_map
*em
;
5882 struct map_lookup
*map
;
5887 u64 raid56_full_stripe_start
= (u64
)-1;
5891 ASSERT(op
!= BTRFS_MAP_DISCARD
);
5893 em
= btrfs_get_chunk_map(fs_info
, logical
, len
);
5897 map
= em
->map_lookup
;
5898 /* Offset of this logical address in the chunk */
5899 offset
= logical
- em
->start
;
5900 /* Len of a stripe in a chunk */
5901 stripe_len
= map
->stripe_len
;
5902 /* Stripe wher this block falls in */
5903 stripe_nr
= div64_u64(offset
, stripe_len
);
5904 /* Offset of stripe in the chunk */
5905 stripe_offset
= stripe_nr
* stripe_len
;
5906 if (offset
< stripe_offset
) {
5908 "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5909 stripe_offset
, offset
, em
->start
, logical
, stripe_len
);
5914 /* stripe_offset is the offset of this block in its stripe */
5915 stripe_offset
= offset
- stripe_offset
;
5916 data_stripes
= nr_data_stripes(map
);
5918 if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
5919 u64 max_len
= stripe_len
- stripe_offset
;
5922 * In case of raid56, we need to know the stripe aligned start
5924 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5925 unsigned long full_stripe_len
= stripe_len
* data_stripes
;
5926 raid56_full_stripe_start
= offset
;
5929 * Allow a write of a full stripe, but make sure we
5930 * don't allow straddling of stripes
5932 raid56_full_stripe_start
= div64_u64(raid56_full_stripe_start
,
5934 raid56_full_stripe_start
*= full_stripe_len
;
5937 * For writes to RAID[56], allow a full stripeset across
5938 * all disks. For other RAID types and for RAID[56]
5939 * reads, just allow a single stripe (on a single disk).
5941 if (op
== BTRFS_MAP_WRITE
) {
5942 max_len
= stripe_len
* data_stripes
-
5943 (offset
- raid56_full_stripe_start
);
5946 len
= min_t(u64
, em
->len
- offset
, max_len
);
5948 len
= em
->len
- offset
;
5952 io_geom
->offset
= offset
;
5953 io_geom
->stripe_len
= stripe_len
;
5954 io_geom
->stripe_nr
= stripe_nr
;
5955 io_geom
->stripe_offset
= stripe_offset
;
5956 io_geom
->raid56_stripe_offset
= raid56_full_stripe_start
;
5960 free_extent_map(em
);
5964 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
,
5965 enum btrfs_map_op op
,
5966 u64 logical
, u64
*length
,
5967 struct btrfs_bio
**bbio_ret
,
5968 int mirror_num
, int need_raid_map
)
5970 struct extent_map
*em
;
5971 struct map_lookup
*map
;
5981 int tgtdev_indexes
= 0;
5982 struct btrfs_bio
*bbio
= NULL
;
5983 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
5984 int dev_replace_is_ongoing
= 0;
5985 int num_alloc_stripes
;
5986 int patch_the_first_stripe_for_dev_replace
= 0;
5987 u64 physical_to_patch_in_first_stripe
= 0;
5988 u64 raid56_full_stripe_start
= (u64
)-1;
5989 struct btrfs_io_geometry geom
;
5992 ASSERT(op
!= BTRFS_MAP_DISCARD
);
5994 ret
= btrfs_get_io_geometry(fs_info
, op
, logical
, *length
, &geom
);
5998 em
= btrfs_get_chunk_map(fs_info
, logical
, *length
);
5999 ASSERT(!IS_ERR(em
));
6000 map
= em
->map_lookup
;
6003 stripe_len
= geom
.stripe_len
;
6004 stripe_nr
= geom
.stripe_nr
;
6005 stripe_offset
= geom
.stripe_offset
;
6006 raid56_full_stripe_start
= geom
.raid56_stripe_offset
;
6007 data_stripes
= nr_data_stripes(map
);
6009 down_read(&dev_replace
->rwsem
);
6010 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
6012 * Hold the semaphore for read during the whole operation, write is
6013 * requested at commit time but must wait.
6015 if (!dev_replace_is_ongoing
)
6016 up_read(&dev_replace
->rwsem
);
6018 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
6019 !need_full_stripe(op
) && dev_replace
->tgtdev
!= NULL
) {
6020 ret
= get_extra_mirror_from_replace(fs_info
, logical
, *length
,
6021 dev_replace
->srcdev
->devid
,
6023 &physical_to_patch_in_first_stripe
);
6027 patch_the_first_stripe_for_dev_replace
= 1;
6028 } else if (mirror_num
> map
->num_stripes
) {
6034 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
6035 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
6037 if (!need_full_stripe(op
))
6039 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1_MASK
) {
6040 if (need_full_stripe(op
))
6041 num_stripes
= map
->num_stripes
;
6042 else if (mirror_num
)
6043 stripe_index
= mirror_num
- 1;
6045 stripe_index
= find_live_mirror(fs_info
, map
, 0,
6046 dev_replace_is_ongoing
);
6047 mirror_num
= stripe_index
+ 1;
6050 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
6051 if (need_full_stripe(op
)) {
6052 num_stripes
= map
->num_stripes
;
6053 } else if (mirror_num
) {
6054 stripe_index
= mirror_num
- 1;
6059 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
6060 u32 factor
= map
->num_stripes
/ map
->sub_stripes
;
6062 stripe_nr
= div_u64_rem(stripe_nr
, factor
, &stripe_index
);
6063 stripe_index
*= map
->sub_stripes
;
6065 if (need_full_stripe(op
))
6066 num_stripes
= map
->sub_stripes
;
6067 else if (mirror_num
)
6068 stripe_index
+= mirror_num
- 1;
6070 int old_stripe_index
= stripe_index
;
6071 stripe_index
= find_live_mirror(fs_info
, map
,
6073 dev_replace_is_ongoing
);
6074 mirror_num
= stripe_index
- old_stripe_index
+ 1;
6077 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
6078 if (need_raid_map
&& (need_full_stripe(op
) || mirror_num
> 1)) {
6079 /* push stripe_nr back to the start of the full stripe */
6080 stripe_nr
= div64_u64(raid56_full_stripe_start
,
6081 stripe_len
* data_stripes
);
6083 /* RAID[56] write or recovery. Return all stripes */
6084 num_stripes
= map
->num_stripes
;
6085 max_errors
= nr_parity_stripes(map
);
6087 *length
= map
->stripe_len
;
6092 * Mirror #0 or #1 means the original data block.
6093 * Mirror #2 is RAID5 parity block.
6094 * Mirror #3 is RAID6 Q block.
6096 stripe_nr
= div_u64_rem(stripe_nr
,
6097 data_stripes
, &stripe_index
);
6099 stripe_index
= data_stripes
+ mirror_num
- 2;
6101 /* We distribute the parity blocks across stripes */
6102 div_u64_rem(stripe_nr
+ stripe_index
, map
->num_stripes
,
6104 if (!need_full_stripe(op
) && mirror_num
<= 1)
6109 * after this, stripe_nr is the number of stripes on this
6110 * device we have to walk to find the data, and stripe_index is
6111 * the number of our device in the stripe array
6113 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
6115 mirror_num
= stripe_index
+ 1;
6117 if (stripe_index
>= map
->num_stripes
) {
6119 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
6120 stripe_index
, map
->num_stripes
);
6125 num_alloc_stripes
= num_stripes
;
6126 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
) {
6127 if (op
== BTRFS_MAP_WRITE
)
6128 num_alloc_stripes
<<= 1;
6129 if (op
== BTRFS_MAP_GET_READ_MIRRORS
)
6130 num_alloc_stripes
++;
6131 tgtdev_indexes
= num_stripes
;
6134 bbio
= alloc_btrfs_bio(num_alloc_stripes
, tgtdev_indexes
);
6139 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
)
6140 bbio
->tgtdev_map
= (int *)(bbio
->stripes
+ num_alloc_stripes
);
6142 /* build raid_map */
6143 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
&& need_raid_map
&&
6144 (need_full_stripe(op
) || mirror_num
> 1)) {
6148 bbio
->raid_map
= (u64
*)((void *)bbio
->stripes
+
6149 sizeof(struct btrfs_bio_stripe
) *
6151 sizeof(int) * tgtdev_indexes
);
6153 /* Work out the disk rotation on this stripe-set */
6154 div_u64_rem(stripe_nr
, num_stripes
, &rot
);
6156 /* Fill in the logical address of each stripe */
6157 tmp
= stripe_nr
* data_stripes
;
6158 for (i
= 0; i
< data_stripes
; i
++)
6159 bbio
->raid_map
[(i
+rot
) % num_stripes
] =
6160 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
6162 bbio
->raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
6163 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
6164 bbio
->raid_map
[(i
+rot
+1) % num_stripes
] =
6169 for (i
= 0; i
< num_stripes
; i
++) {
6170 bbio
->stripes
[i
].physical
=
6171 map
->stripes
[stripe_index
].physical
+
6173 stripe_nr
* map
->stripe_len
;
6174 bbio
->stripes
[i
].dev
=
6175 map
->stripes
[stripe_index
].dev
;
6179 if (need_full_stripe(op
))
6180 max_errors
= btrfs_chunk_max_errors(map
);
6183 sort_parity_stripes(bbio
, num_stripes
);
6185 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
&&
6186 need_full_stripe(op
)) {
6187 handle_ops_on_dev_replace(op
, &bbio
, dev_replace
, &num_stripes
,
6192 bbio
->map_type
= map
->type
;
6193 bbio
->num_stripes
= num_stripes
;
6194 bbio
->max_errors
= max_errors
;
6195 bbio
->mirror_num
= mirror_num
;
6198 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6199 * mirror_num == num_stripes + 1 && dev_replace target drive is
6200 * available as a mirror
6202 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
6203 WARN_ON(num_stripes
> 1);
6204 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
6205 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
6206 bbio
->mirror_num
= map
->num_stripes
+ 1;
6209 if (dev_replace_is_ongoing
) {
6210 lockdep_assert_held(&dev_replace
->rwsem
);
6211 /* Unlock and let waiting writers proceed */
6212 up_read(&dev_replace
->rwsem
);
6214 free_extent_map(em
);
6218 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
6219 u64 logical
, u64
*length
,
6220 struct btrfs_bio
**bbio_ret
, int mirror_num
)
6222 if (op
== BTRFS_MAP_DISCARD
)
6223 return __btrfs_map_block_for_discard(fs_info
, logical
,
6226 return __btrfs_map_block(fs_info
, op
, logical
, length
, bbio_ret
,
6230 /* For Scrub/replace */
6231 int btrfs_map_sblock(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
6232 u64 logical
, u64
*length
,
6233 struct btrfs_bio
**bbio_ret
)
6235 return __btrfs_map_block(fs_info
, op
, logical
, length
, bbio_ret
, 0, 1);
6238 static inline void btrfs_end_bbio(struct btrfs_bio
*bbio
, struct bio
*bio
)
6240 bio
->bi_private
= bbio
->private;
6241 bio
->bi_end_io
= bbio
->end_io
;
6244 btrfs_put_bbio(bbio
);
6247 static void btrfs_end_bio(struct bio
*bio
)
6249 struct btrfs_bio
*bbio
= bio
->bi_private
;
6250 int is_orig_bio
= 0;
6252 if (bio
->bi_status
) {
6253 atomic_inc(&bbio
->error
);
6254 if (bio
->bi_status
== BLK_STS_IOERR
||
6255 bio
->bi_status
== BLK_STS_TARGET
) {
6256 unsigned int stripe_index
=
6257 btrfs_io_bio(bio
)->stripe_index
;
6258 struct btrfs_device
*dev
;
6260 BUG_ON(stripe_index
>= bbio
->num_stripes
);
6261 dev
= bbio
->stripes
[stripe_index
].dev
;
6263 if (bio_op(bio
) == REQ_OP_WRITE
)
6264 btrfs_dev_stat_inc_and_print(dev
,
6265 BTRFS_DEV_STAT_WRITE_ERRS
);
6266 else if (!(bio
->bi_opf
& REQ_RAHEAD
))
6267 btrfs_dev_stat_inc_and_print(dev
,
6268 BTRFS_DEV_STAT_READ_ERRS
);
6269 if (bio
->bi_opf
& REQ_PREFLUSH
)
6270 btrfs_dev_stat_inc_and_print(dev
,
6271 BTRFS_DEV_STAT_FLUSH_ERRS
);
6276 if (bio
== bbio
->orig_bio
)
6279 btrfs_bio_counter_dec(bbio
->fs_info
);
6281 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
6284 bio
= bbio
->orig_bio
;
6287 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
6288 /* only send an error to the higher layers if it is
6289 * beyond the tolerance of the btrfs bio
6291 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
6292 bio
->bi_status
= BLK_STS_IOERR
;
6295 * this bio is actually up to date, we didn't
6296 * go over the max number of errors
6298 bio
->bi_status
= BLK_STS_OK
;
6301 btrfs_end_bbio(bbio
, bio
);
6302 } else if (!is_orig_bio
) {
6307 static void submit_stripe_bio(struct btrfs_bio
*bbio
, struct bio
*bio
,
6308 u64 physical
, int dev_nr
)
6310 struct btrfs_device
*dev
= bbio
->stripes
[dev_nr
].dev
;
6311 struct btrfs_fs_info
*fs_info
= bbio
->fs_info
;
6313 bio
->bi_private
= bbio
;
6314 btrfs_io_bio(bio
)->stripe_index
= dev_nr
;
6315 bio
->bi_end_io
= btrfs_end_bio
;
6316 bio
->bi_iter
.bi_sector
= physical
>> 9;
6317 btrfs_debug_in_rcu(fs_info
,
6318 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6319 bio_op(bio
), bio
->bi_opf
, (u64
)bio
->bi_iter
.bi_sector
,
6320 (unsigned long)dev
->bdev
->bd_dev
, rcu_str_deref(dev
->name
),
6321 dev
->devid
, bio
->bi_iter
.bi_size
);
6322 bio_set_dev(bio
, dev
->bdev
);
6324 btrfs_bio_counter_inc_noblocked(fs_info
);
6326 btrfsic_submit_bio(bio
);
6329 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
6331 atomic_inc(&bbio
->error
);
6332 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
6333 /* Should be the original bio. */
6334 WARN_ON(bio
!= bbio
->orig_bio
);
6336 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
6337 bio
->bi_iter
.bi_sector
= logical
>> 9;
6338 if (atomic_read(&bbio
->error
) > bbio
->max_errors
)
6339 bio
->bi_status
= BLK_STS_IOERR
;
6341 bio
->bi_status
= BLK_STS_OK
;
6342 btrfs_end_bbio(bbio
, bio
);
6346 blk_status_t
btrfs_map_bio(struct btrfs_fs_info
*fs_info
, struct bio
*bio
,
6349 struct btrfs_device
*dev
;
6350 struct bio
*first_bio
= bio
;
6351 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
6357 struct btrfs_bio
*bbio
= NULL
;
6359 length
= bio
->bi_iter
.bi_size
;
6360 map_length
= length
;
6362 btrfs_bio_counter_inc_blocked(fs_info
);
6363 ret
= __btrfs_map_block(fs_info
, btrfs_op(bio
), logical
,
6364 &map_length
, &bbio
, mirror_num
, 1);
6366 btrfs_bio_counter_dec(fs_info
);
6367 return errno_to_blk_status(ret
);
6370 total_devs
= bbio
->num_stripes
;
6371 bbio
->orig_bio
= first_bio
;
6372 bbio
->private = first_bio
->bi_private
;
6373 bbio
->end_io
= first_bio
->bi_end_io
;
6374 bbio
->fs_info
= fs_info
;
6375 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
6377 if ((bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) &&
6378 ((bio_op(bio
) == REQ_OP_WRITE
) || (mirror_num
> 1))) {
6379 /* In this case, map_length has been set to the length of
6380 a single stripe; not the whole write */
6381 if (bio_op(bio
) == REQ_OP_WRITE
) {
6382 ret
= raid56_parity_write(fs_info
, bio
, bbio
,
6385 ret
= raid56_parity_recover(fs_info
, bio
, bbio
,
6386 map_length
, mirror_num
, 1);
6389 btrfs_bio_counter_dec(fs_info
);
6390 return errno_to_blk_status(ret
);
6393 if (map_length
< length
) {
6395 "mapping failed logical %llu bio len %llu len %llu",
6396 logical
, length
, map_length
);
6400 for (dev_nr
= 0; dev_nr
< total_devs
; dev_nr
++) {
6401 dev
= bbio
->stripes
[dev_nr
].dev
;
6402 if (!dev
|| !dev
->bdev
|| test_bit(BTRFS_DEV_STATE_MISSING
,
6404 (bio_op(first_bio
) == REQ_OP_WRITE
&&
6405 !test_bit(BTRFS_DEV_STATE_WRITEABLE
, &dev
->dev_state
))) {
6406 bbio_error(bbio
, first_bio
, logical
);
6410 if (dev_nr
< total_devs
- 1)
6411 bio
= btrfs_bio_clone(first_bio
);
6415 submit_stripe_bio(bbio
, bio
, bbio
->stripes
[dev_nr
].physical
,
6418 btrfs_bio_counter_dec(fs_info
);
6423 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6426 * If devid and uuid are both specified, the match must be exact, otherwise
6427 * only devid is used.
6429 * If @seed is true, traverse through the seed devices.
6431 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_devices
*fs_devices
,
6432 u64 devid
, u8
*uuid
, u8
*fsid
,
6435 struct btrfs_device
*device
;
6437 while (fs_devices
) {
6439 !memcmp(fs_devices
->metadata_uuid
, fsid
, BTRFS_FSID_SIZE
)) {
6440 list_for_each_entry(device
, &fs_devices
->devices
,
6442 if (device
->devid
== devid
&&
6443 (!uuid
|| memcmp(device
->uuid
, uuid
,
6444 BTRFS_UUID_SIZE
) == 0))
6449 fs_devices
= fs_devices
->seed
;
6456 static struct btrfs_device
*add_missing_dev(struct btrfs_fs_devices
*fs_devices
,
6457 u64 devid
, u8
*dev_uuid
)
6459 struct btrfs_device
*device
;
6461 device
= btrfs_alloc_device(NULL
, &devid
, dev_uuid
);
6465 list_add(&device
->dev_list
, &fs_devices
->devices
);
6466 device
->fs_devices
= fs_devices
;
6467 fs_devices
->num_devices
++;
6469 set_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
6470 fs_devices
->missing_devices
++;
6476 * btrfs_alloc_device - allocate struct btrfs_device
6477 * @fs_info: used only for generating a new devid, can be NULL if
6478 * devid is provided (i.e. @devid != NULL).
6479 * @devid: a pointer to devid for this device. If NULL a new devid
6481 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6484 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6485 * on error. Returned struct is not linked onto any lists and must be
6486 * destroyed with btrfs_free_device.
6488 struct btrfs_device
*btrfs_alloc_device(struct btrfs_fs_info
*fs_info
,
6492 struct btrfs_device
*dev
;
6495 if (WARN_ON(!devid
&& !fs_info
))
6496 return ERR_PTR(-EINVAL
);
6498 dev
= __alloc_device();
6507 ret
= find_next_devid(fs_info
, &tmp
);
6509 btrfs_free_device(dev
);
6510 return ERR_PTR(ret
);
6516 memcpy(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
);
6518 generate_random_uuid(dev
->uuid
);
6523 static void btrfs_report_missing_device(struct btrfs_fs_info
*fs_info
,
6524 u64 devid
, u8
*uuid
, bool error
)
6527 btrfs_err_rl(fs_info
, "devid %llu uuid %pU is missing",
6530 btrfs_warn_rl(fs_info
, "devid %llu uuid %pU is missing",
6534 static u64
calc_stripe_length(u64 type
, u64 chunk_len
, int num_stripes
)
6536 int index
= btrfs_bg_flags_to_raid_index(type
);
6537 int ncopies
= btrfs_raid_array
[index
].ncopies
;
6538 const int nparity
= btrfs_raid_array
[index
].nparity
;
6542 data_stripes
= num_stripes
- nparity
;
6544 data_stripes
= num_stripes
/ ncopies
;
6546 return div_u64(chunk_len
, data_stripes
);
6549 static int read_one_chunk(struct btrfs_key
*key
, struct extent_buffer
*leaf
,
6550 struct btrfs_chunk
*chunk
)
6552 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
6553 struct extent_map_tree
*map_tree
= &fs_info
->mapping_tree
;
6554 struct map_lookup
*map
;
6555 struct extent_map
*em
;
6559 u8 uuid
[BTRFS_UUID_SIZE
];
6564 logical
= key
->offset
;
6565 length
= btrfs_chunk_length(leaf
, chunk
);
6566 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
6569 * Only need to verify chunk item if we're reading from sys chunk array,
6570 * as chunk item in tree block is already verified by tree-checker.
6572 if (leaf
->start
== BTRFS_SUPER_INFO_OFFSET
) {
6573 ret
= btrfs_check_chunk_valid(leaf
, chunk
, logical
);
6578 read_lock(&map_tree
->lock
);
6579 em
= lookup_extent_mapping(map_tree
, logical
, 1);
6580 read_unlock(&map_tree
->lock
);
6582 /* already mapped? */
6583 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
6584 free_extent_map(em
);
6587 free_extent_map(em
);
6590 em
= alloc_extent_map();
6593 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
6595 free_extent_map(em
);
6599 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
6600 em
->map_lookup
= map
;
6601 em
->start
= logical
;
6604 em
->block_start
= 0;
6605 em
->block_len
= em
->len
;
6607 map
->num_stripes
= num_stripes
;
6608 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
6609 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
6610 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
6611 map
->type
= btrfs_chunk_type(leaf
, chunk
);
6612 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
6613 map
->verified_stripes
= 0;
6614 em
->orig_block_len
= calc_stripe_length(map
->type
, em
->len
,
6616 for (i
= 0; i
< num_stripes
; i
++) {
6617 map
->stripes
[i
].physical
=
6618 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
6619 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
6620 read_extent_buffer(leaf
, uuid
, (unsigned long)
6621 btrfs_stripe_dev_uuid_nr(chunk
, i
),
6623 map
->stripes
[i
].dev
= btrfs_find_device(fs_info
->fs_devices
,
6624 devid
, uuid
, NULL
, true);
6625 if (!map
->stripes
[i
].dev
&&
6626 !btrfs_test_opt(fs_info
, DEGRADED
)) {
6627 free_extent_map(em
);
6628 btrfs_report_missing_device(fs_info
, devid
, uuid
, true);
6631 if (!map
->stripes
[i
].dev
) {
6632 map
->stripes
[i
].dev
=
6633 add_missing_dev(fs_info
->fs_devices
, devid
,
6635 if (IS_ERR(map
->stripes
[i
].dev
)) {
6636 free_extent_map(em
);
6638 "failed to init missing dev %llu: %ld",
6639 devid
, PTR_ERR(map
->stripes
[i
].dev
));
6640 return PTR_ERR(map
->stripes
[i
].dev
);
6642 btrfs_report_missing_device(fs_info
, devid
, uuid
, false);
6644 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
6645 &(map
->stripes
[i
].dev
->dev_state
));
6649 write_lock(&map_tree
->lock
);
6650 ret
= add_extent_mapping(map_tree
, em
, 0);
6651 write_unlock(&map_tree
->lock
);
6654 "failed to add chunk map, start=%llu len=%llu: %d",
6655 em
->start
, em
->len
, ret
);
6657 free_extent_map(em
);
6662 static void fill_device_from_item(struct extent_buffer
*leaf
,
6663 struct btrfs_dev_item
*dev_item
,
6664 struct btrfs_device
*device
)
6668 device
->devid
= btrfs_device_id(leaf
, dev_item
);
6669 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
6670 device
->total_bytes
= device
->disk_total_bytes
;
6671 device
->commit_total_bytes
= device
->disk_total_bytes
;
6672 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
6673 device
->commit_bytes_used
= device
->bytes_used
;
6674 device
->type
= btrfs_device_type(leaf
, dev_item
);
6675 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
6676 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
6677 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
6678 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
6679 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
6681 ptr
= btrfs_device_uuid(dev_item
);
6682 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
6685 static struct btrfs_fs_devices
*open_seed_devices(struct btrfs_fs_info
*fs_info
,
6688 struct btrfs_fs_devices
*fs_devices
;
6691 lockdep_assert_held(&uuid_mutex
);
6694 fs_devices
= fs_info
->fs_devices
->seed
;
6695 while (fs_devices
) {
6696 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_FSID_SIZE
))
6699 fs_devices
= fs_devices
->seed
;
6702 fs_devices
= find_fsid(fsid
, NULL
);
6704 if (!btrfs_test_opt(fs_info
, DEGRADED
))
6705 return ERR_PTR(-ENOENT
);
6707 fs_devices
= alloc_fs_devices(fsid
, NULL
);
6708 if (IS_ERR(fs_devices
))
6711 fs_devices
->seeding
= true;
6712 fs_devices
->opened
= 1;
6716 fs_devices
= clone_fs_devices(fs_devices
);
6717 if (IS_ERR(fs_devices
))
6720 ret
= open_fs_devices(fs_devices
, FMODE_READ
, fs_info
->bdev_holder
);
6722 free_fs_devices(fs_devices
);
6723 fs_devices
= ERR_PTR(ret
);
6727 if (!fs_devices
->seeding
) {
6728 close_fs_devices(fs_devices
);
6729 free_fs_devices(fs_devices
);
6730 fs_devices
= ERR_PTR(-EINVAL
);
6734 fs_devices
->seed
= fs_info
->fs_devices
->seed
;
6735 fs_info
->fs_devices
->seed
= fs_devices
;
6740 static int read_one_dev(struct extent_buffer
*leaf
,
6741 struct btrfs_dev_item
*dev_item
)
6743 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
6744 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6745 struct btrfs_device
*device
;
6748 u8 fs_uuid
[BTRFS_FSID_SIZE
];
6749 u8 dev_uuid
[BTRFS_UUID_SIZE
];
6751 devid
= btrfs_device_id(leaf
, dev_item
);
6752 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
6754 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
6757 if (memcmp(fs_uuid
, fs_devices
->metadata_uuid
, BTRFS_FSID_SIZE
)) {
6758 fs_devices
= open_seed_devices(fs_info
, fs_uuid
);
6759 if (IS_ERR(fs_devices
))
6760 return PTR_ERR(fs_devices
);
6763 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
6766 if (!btrfs_test_opt(fs_info
, DEGRADED
)) {
6767 btrfs_report_missing_device(fs_info
, devid
,
6772 device
= add_missing_dev(fs_devices
, devid
, dev_uuid
);
6773 if (IS_ERR(device
)) {
6775 "failed to add missing dev %llu: %ld",
6776 devid
, PTR_ERR(device
));
6777 return PTR_ERR(device
);
6779 btrfs_report_missing_device(fs_info
, devid
, dev_uuid
, false);
6781 if (!device
->bdev
) {
6782 if (!btrfs_test_opt(fs_info
, DEGRADED
)) {
6783 btrfs_report_missing_device(fs_info
,
6784 devid
, dev_uuid
, true);
6787 btrfs_report_missing_device(fs_info
, devid
,
6791 if (!device
->bdev
&&
6792 !test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
)) {
6794 * this happens when a device that was properly setup
6795 * in the device info lists suddenly goes bad.
6796 * device->bdev is NULL, and so we have to set
6797 * device->missing to one here
6799 device
->fs_devices
->missing_devices
++;
6800 set_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
6803 /* Move the device to its own fs_devices */
6804 if (device
->fs_devices
!= fs_devices
) {
6805 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING
,
6806 &device
->dev_state
));
6808 list_move(&device
->dev_list
, &fs_devices
->devices
);
6809 device
->fs_devices
->num_devices
--;
6810 fs_devices
->num_devices
++;
6812 device
->fs_devices
->missing_devices
--;
6813 fs_devices
->missing_devices
++;
6815 device
->fs_devices
= fs_devices
;
6819 if (device
->fs_devices
!= fs_info
->fs_devices
) {
6820 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
));
6821 if (device
->generation
!=
6822 btrfs_device_generation(leaf
, dev_item
))
6826 fill_device_from_item(leaf
, dev_item
, device
);
6827 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
6828 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
6829 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
6830 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
6831 atomic64_add(device
->total_bytes
- device
->bytes_used
,
6832 &fs_info
->free_chunk_space
);
6838 int btrfs_read_sys_array(struct btrfs_fs_info
*fs_info
)
6840 struct btrfs_root
*root
= fs_info
->tree_root
;
6841 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
6842 struct extent_buffer
*sb
;
6843 struct btrfs_disk_key
*disk_key
;
6844 struct btrfs_chunk
*chunk
;
6846 unsigned long sb_array_offset
;
6853 struct btrfs_key key
;
6855 ASSERT(BTRFS_SUPER_INFO_SIZE
<= fs_info
->nodesize
);
6857 * This will create extent buffer of nodesize, superblock size is
6858 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6859 * overallocate but we can keep it as-is, only the first page is used.
6861 sb
= btrfs_find_create_tree_block(fs_info
, BTRFS_SUPER_INFO_OFFSET
);
6864 set_extent_buffer_uptodate(sb
);
6865 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
6867 * The sb extent buffer is artificial and just used to read the system array.
6868 * set_extent_buffer_uptodate() call does not properly mark all it's
6869 * pages up-to-date when the page is larger: extent does not cover the
6870 * whole page and consequently check_page_uptodate does not find all
6871 * the page's extents up-to-date (the hole beyond sb),
6872 * write_extent_buffer then triggers a WARN_ON.
6874 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6875 * but sb spans only this function. Add an explicit SetPageUptodate call
6876 * to silence the warning eg. on PowerPC 64.
6878 if (PAGE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
6879 SetPageUptodate(sb
->pages
[0]);
6881 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
6882 array_size
= btrfs_super_sys_array_size(super_copy
);
6884 array_ptr
= super_copy
->sys_chunk_array
;
6885 sb_array_offset
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
6888 while (cur_offset
< array_size
) {
6889 disk_key
= (struct btrfs_disk_key
*)array_ptr
;
6890 len
= sizeof(*disk_key
);
6891 if (cur_offset
+ len
> array_size
)
6892 goto out_short_read
;
6894 btrfs_disk_key_to_cpu(&key
, disk_key
);
6897 sb_array_offset
+= len
;
6900 if (key
.type
!= BTRFS_CHUNK_ITEM_KEY
) {
6902 "unexpected item type %u in sys_array at offset %u",
6903 (u32
)key
.type
, cur_offset
);
6908 chunk
= (struct btrfs_chunk
*)sb_array_offset
;
6910 * At least one btrfs_chunk with one stripe must be present,
6911 * exact stripe count check comes afterwards
6913 len
= btrfs_chunk_item_size(1);
6914 if (cur_offset
+ len
> array_size
)
6915 goto out_short_read
;
6917 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
6920 "invalid number of stripes %u in sys_array at offset %u",
6921 num_stripes
, cur_offset
);
6926 type
= btrfs_chunk_type(sb
, chunk
);
6927 if ((type
& BTRFS_BLOCK_GROUP_SYSTEM
) == 0) {
6929 "invalid chunk type %llu in sys_array at offset %u",
6935 len
= btrfs_chunk_item_size(num_stripes
);
6936 if (cur_offset
+ len
> array_size
)
6937 goto out_short_read
;
6939 ret
= read_one_chunk(&key
, sb
, chunk
);
6944 sb_array_offset
+= len
;
6947 clear_extent_buffer_uptodate(sb
);
6948 free_extent_buffer_stale(sb
);
6952 btrfs_err(fs_info
, "sys_array too short to read %u bytes at offset %u",
6954 clear_extent_buffer_uptodate(sb
);
6955 free_extent_buffer_stale(sb
);
6960 * Check if all chunks in the fs are OK for read-write degraded mount
6962 * If the @failing_dev is specified, it's accounted as missing.
6964 * Return true if all chunks meet the minimal RW mount requirements.
6965 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6967 bool btrfs_check_rw_degradable(struct btrfs_fs_info
*fs_info
,
6968 struct btrfs_device
*failing_dev
)
6970 struct extent_map_tree
*map_tree
= &fs_info
->mapping_tree
;
6971 struct extent_map
*em
;
6975 read_lock(&map_tree
->lock
);
6976 em
= lookup_extent_mapping(map_tree
, 0, (u64
)-1);
6977 read_unlock(&map_tree
->lock
);
6978 /* No chunk at all? Return false anyway */
6984 struct map_lookup
*map
;
6989 map
= em
->map_lookup
;
6991 btrfs_get_num_tolerated_disk_barrier_failures(
6993 for (i
= 0; i
< map
->num_stripes
; i
++) {
6994 struct btrfs_device
*dev
= map
->stripes
[i
].dev
;
6996 if (!dev
|| !dev
->bdev
||
6997 test_bit(BTRFS_DEV_STATE_MISSING
, &dev
->dev_state
) ||
6998 dev
->last_flush_error
)
7000 else if (failing_dev
&& failing_dev
== dev
)
7003 if (missing
> max_tolerated
) {
7006 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
7007 em
->start
, missing
, max_tolerated
);
7008 free_extent_map(em
);
7012 next_start
= extent_map_end(em
);
7013 free_extent_map(em
);
7015 read_lock(&map_tree
->lock
);
7016 em
= lookup_extent_mapping(map_tree
, next_start
,
7017 (u64
)(-1) - next_start
);
7018 read_unlock(&map_tree
->lock
);
7024 int btrfs_read_chunk_tree(struct btrfs_fs_info
*fs_info
)
7026 struct btrfs_root
*root
= fs_info
->chunk_root
;
7027 struct btrfs_path
*path
;
7028 struct extent_buffer
*leaf
;
7029 struct btrfs_key key
;
7030 struct btrfs_key found_key
;
7035 path
= btrfs_alloc_path();
7040 * uuid_mutex is needed only if we are mounting a sprout FS
7041 * otherwise we don't need it.
7043 mutex_lock(&uuid_mutex
);
7044 mutex_lock(&fs_info
->chunk_mutex
);
7047 * Read all device items, and then all the chunk items. All
7048 * device items are found before any chunk item (their object id
7049 * is smaller than the lowest possible object id for a chunk
7050 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
7052 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
7055 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7059 leaf
= path
->nodes
[0];
7060 slot
= path
->slots
[0];
7061 if (slot
>= btrfs_header_nritems(leaf
)) {
7062 ret
= btrfs_next_leaf(root
, path
);
7069 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
7070 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
7071 struct btrfs_dev_item
*dev_item
;
7072 dev_item
= btrfs_item_ptr(leaf
, slot
,
7073 struct btrfs_dev_item
);
7074 ret
= read_one_dev(leaf
, dev_item
);
7078 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
7079 struct btrfs_chunk
*chunk
;
7080 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
7081 ret
= read_one_chunk(&found_key
, leaf
, chunk
);
7089 * After loading chunk tree, we've got all device information,
7090 * do another round of validation checks.
7092 if (total_dev
!= fs_info
->fs_devices
->total_devices
) {
7094 "super_num_devices %llu mismatch with num_devices %llu found here",
7095 btrfs_super_num_devices(fs_info
->super_copy
),
7100 if (btrfs_super_total_bytes(fs_info
->super_copy
) <
7101 fs_info
->fs_devices
->total_rw_bytes
) {
7103 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
7104 btrfs_super_total_bytes(fs_info
->super_copy
),
7105 fs_info
->fs_devices
->total_rw_bytes
);
7111 mutex_unlock(&fs_info
->chunk_mutex
);
7112 mutex_unlock(&uuid_mutex
);
7114 btrfs_free_path(path
);
7118 void btrfs_init_devices_late(struct btrfs_fs_info
*fs_info
)
7120 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7121 struct btrfs_device
*device
;
7123 while (fs_devices
) {
7124 mutex_lock(&fs_devices
->device_list_mutex
);
7125 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
7126 device
->fs_info
= fs_info
;
7127 mutex_unlock(&fs_devices
->device_list_mutex
);
7129 fs_devices
= fs_devices
->seed
;
7133 static u64
btrfs_dev_stats_value(const struct extent_buffer
*eb
,
7134 const struct btrfs_dev_stats_item
*ptr
,
7139 read_extent_buffer(eb
, &val
,
7140 offsetof(struct btrfs_dev_stats_item
, values
) +
7141 ((unsigned long)ptr
) + (index
* sizeof(u64
)),
7146 static void btrfs_set_dev_stats_value(struct extent_buffer
*eb
,
7147 struct btrfs_dev_stats_item
*ptr
,
7150 write_extent_buffer(eb
, &val
,
7151 offsetof(struct btrfs_dev_stats_item
, values
) +
7152 ((unsigned long)ptr
) + (index
* sizeof(u64
)),
7156 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
7158 struct btrfs_key key
;
7159 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
7160 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7161 struct extent_buffer
*eb
;
7164 struct btrfs_device
*device
;
7165 struct btrfs_path
*path
= NULL
;
7168 path
= btrfs_alloc_path();
7172 mutex_lock(&fs_devices
->device_list_mutex
);
7173 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
7175 struct btrfs_dev_stats_item
*ptr
;
7177 key
.objectid
= BTRFS_DEV_STATS_OBJECTID
;
7178 key
.type
= BTRFS_PERSISTENT_ITEM_KEY
;
7179 key
.offset
= device
->devid
;
7180 ret
= btrfs_search_slot(NULL
, dev_root
, &key
, path
, 0, 0);
7182 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7183 btrfs_dev_stat_set(device
, i
, 0);
7184 device
->dev_stats_valid
= 1;
7185 btrfs_release_path(path
);
7188 slot
= path
->slots
[0];
7189 eb
= path
->nodes
[0];
7190 item_size
= btrfs_item_size_nr(eb
, slot
);
7192 ptr
= btrfs_item_ptr(eb
, slot
,
7193 struct btrfs_dev_stats_item
);
7195 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
7196 if (item_size
>= (1 + i
) * sizeof(__le64
))
7197 btrfs_dev_stat_set(device
, i
,
7198 btrfs_dev_stats_value(eb
, ptr
, i
));
7200 btrfs_dev_stat_set(device
, i
, 0);
7203 device
->dev_stats_valid
= 1;
7204 btrfs_dev_stat_print_on_load(device
);
7205 btrfs_release_path(path
);
7207 mutex_unlock(&fs_devices
->device_list_mutex
);
7209 btrfs_free_path(path
);
7210 return ret
< 0 ? ret
: 0;
7213 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
7214 struct btrfs_device
*device
)
7216 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
7217 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
7218 struct btrfs_path
*path
;
7219 struct btrfs_key key
;
7220 struct extent_buffer
*eb
;
7221 struct btrfs_dev_stats_item
*ptr
;
7225 key
.objectid
= BTRFS_DEV_STATS_OBJECTID
;
7226 key
.type
= BTRFS_PERSISTENT_ITEM_KEY
;
7227 key
.offset
= device
->devid
;
7229 path
= btrfs_alloc_path();
7232 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
7234 btrfs_warn_in_rcu(fs_info
,
7235 "error %d while searching for dev_stats item for device %s",
7236 ret
, rcu_str_deref(device
->name
));
7241 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
7242 /* need to delete old one and insert a new one */
7243 ret
= btrfs_del_item(trans
, dev_root
, path
);
7245 btrfs_warn_in_rcu(fs_info
,
7246 "delete too small dev_stats item for device %s failed %d",
7247 rcu_str_deref(device
->name
), ret
);
7254 /* need to insert a new item */
7255 btrfs_release_path(path
);
7256 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
7257 &key
, sizeof(*ptr
));
7259 btrfs_warn_in_rcu(fs_info
,
7260 "insert dev_stats item for device %s failed %d",
7261 rcu_str_deref(device
->name
), ret
);
7266 eb
= path
->nodes
[0];
7267 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
7268 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7269 btrfs_set_dev_stats_value(eb
, ptr
, i
,
7270 btrfs_dev_stat_read(device
, i
));
7271 btrfs_mark_buffer_dirty(eb
);
7274 btrfs_free_path(path
);
7279 * called from commit_transaction. Writes all changed device stats to disk.
7281 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
)
7283 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
7284 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7285 struct btrfs_device
*device
;
7289 mutex_lock(&fs_devices
->device_list_mutex
);
7290 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
7291 stats_cnt
= atomic_read(&device
->dev_stats_ccnt
);
7292 if (!device
->dev_stats_valid
|| stats_cnt
== 0)
7297 * There is a LOAD-LOAD control dependency between the value of
7298 * dev_stats_ccnt and updating the on-disk values which requires
7299 * reading the in-memory counters. Such control dependencies
7300 * require explicit read memory barriers.
7302 * This memory barriers pairs with smp_mb__before_atomic in
7303 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7304 * barrier implied by atomic_xchg in
7305 * btrfs_dev_stats_read_and_reset
7309 ret
= update_dev_stat_item(trans
, device
);
7311 atomic_sub(stats_cnt
, &device
->dev_stats_ccnt
);
7313 mutex_unlock(&fs_devices
->device_list_mutex
);
7318 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
7320 btrfs_dev_stat_inc(dev
, index
);
7321 btrfs_dev_stat_print_on_error(dev
);
7324 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
7326 if (!dev
->dev_stats_valid
)
7328 btrfs_err_rl_in_rcu(dev
->fs_info
,
7329 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7330 rcu_str_deref(dev
->name
),
7331 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
7332 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
7333 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
7334 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
7335 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
7338 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
7342 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7343 if (btrfs_dev_stat_read(dev
, i
) != 0)
7345 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
7346 return; /* all values == 0, suppress message */
7348 btrfs_info_in_rcu(dev
->fs_info
,
7349 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7350 rcu_str_deref(dev
->name
),
7351 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
7352 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
7353 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
7354 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
7355 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
7358 int btrfs_get_dev_stats(struct btrfs_fs_info
*fs_info
,
7359 struct btrfs_ioctl_get_dev_stats
*stats
)
7361 struct btrfs_device
*dev
;
7362 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7365 mutex_lock(&fs_devices
->device_list_mutex
);
7366 dev
= btrfs_find_device(fs_info
->fs_devices
, stats
->devid
, NULL
, NULL
,
7368 mutex_unlock(&fs_devices
->device_list_mutex
);
7371 btrfs_warn(fs_info
, "get dev_stats failed, device not found");
7373 } else if (!dev
->dev_stats_valid
) {
7374 btrfs_warn(fs_info
, "get dev_stats failed, not yet valid");
7376 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
7377 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
7378 if (stats
->nr_items
> i
)
7380 btrfs_dev_stat_read_and_reset(dev
, i
);
7382 btrfs_dev_stat_set(dev
, i
, 0);
7384 btrfs_info(fs_info
, "device stats zeroed by %s (%d)",
7385 current
->comm
, task_pid_nr(current
));
7387 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7388 if (stats
->nr_items
> i
)
7389 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
7391 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
7392 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
7397 * Update the size and bytes used for each device where it changed. This is
7398 * delayed since we would otherwise get errors while writing out the
7401 * Must be invoked during transaction commit.
7403 void btrfs_commit_device_sizes(struct btrfs_transaction
*trans
)
7405 struct btrfs_device
*curr
, *next
;
7407 ASSERT(trans
->state
== TRANS_STATE_COMMIT_DOING
);
7409 if (list_empty(&trans
->dev_update_list
))
7413 * We don't need the device_list_mutex here. This list is owned by the
7414 * transaction and the transaction must complete before the device is
7417 mutex_lock(&trans
->fs_info
->chunk_mutex
);
7418 list_for_each_entry_safe(curr
, next
, &trans
->dev_update_list
,
7420 list_del_init(&curr
->post_commit_list
);
7421 curr
->commit_total_bytes
= curr
->disk_total_bytes
;
7422 curr
->commit_bytes_used
= curr
->bytes_used
;
7424 mutex_unlock(&trans
->fs_info
->chunk_mutex
);
7427 void btrfs_set_fs_info_ptr(struct btrfs_fs_info
*fs_info
)
7429 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7430 while (fs_devices
) {
7431 fs_devices
->fs_info
= fs_info
;
7432 fs_devices
= fs_devices
->seed
;
7436 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info
*fs_info
)
7438 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7439 while (fs_devices
) {
7440 fs_devices
->fs_info
= NULL
;
7441 fs_devices
= fs_devices
->seed
;
7446 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7448 int btrfs_bg_type_to_factor(u64 flags
)
7450 const int index
= btrfs_bg_flags_to_raid_index(flags
);
7452 return btrfs_raid_array
[index
].ncopies
;
7457 static int verify_one_dev_extent(struct btrfs_fs_info
*fs_info
,
7458 u64 chunk_offset
, u64 devid
,
7459 u64 physical_offset
, u64 physical_len
)
7461 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
;
7462 struct extent_map
*em
;
7463 struct map_lookup
*map
;
7464 struct btrfs_device
*dev
;
7470 read_lock(&em_tree
->lock
);
7471 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
7472 read_unlock(&em_tree
->lock
);
7476 "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7477 physical_offset
, devid
);
7482 map
= em
->map_lookup
;
7483 stripe_len
= calc_stripe_length(map
->type
, em
->len
, map
->num_stripes
);
7484 if (physical_len
!= stripe_len
) {
7486 "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7487 physical_offset
, devid
, em
->start
, physical_len
,
7493 for (i
= 0; i
< map
->num_stripes
; i
++) {
7494 if (map
->stripes
[i
].dev
->devid
== devid
&&
7495 map
->stripes
[i
].physical
== physical_offset
) {
7497 if (map
->verified_stripes
>= map
->num_stripes
) {
7499 "too many dev extents for chunk %llu found",
7504 map
->verified_stripes
++;
7510 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7511 physical_offset
, devid
);
7515 /* Make sure no dev extent is beyond device bondary */
7516 dev
= btrfs_find_device(fs_info
->fs_devices
, devid
, NULL
, NULL
, true);
7518 btrfs_err(fs_info
, "failed to find devid %llu", devid
);
7523 /* It's possible this device is a dummy for seed device */
7524 if (dev
->disk_total_bytes
== 0) {
7525 dev
= btrfs_find_device(fs_info
->fs_devices
->seed
, devid
, NULL
,
7528 btrfs_err(fs_info
, "failed to find seed devid %llu",
7535 if (physical_offset
+ physical_len
> dev
->disk_total_bytes
) {
7537 "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7538 devid
, physical_offset
, physical_len
,
7539 dev
->disk_total_bytes
);
7544 free_extent_map(em
);
7548 static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info
*fs_info
)
7550 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
;
7551 struct extent_map
*em
;
7552 struct rb_node
*node
;
7555 read_lock(&em_tree
->lock
);
7556 for (node
= rb_first_cached(&em_tree
->map
); node
; node
= rb_next(node
)) {
7557 em
= rb_entry(node
, struct extent_map
, rb_node
);
7558 if (em
->map_lookup
->num_stripes
!=
7559 em
->map_lookup
->verified_stripes
) {
7561 "chunk %llu has missing dev extent, have %d expect %d",
7562 em
->start
, em
->map_lookup
->verified_stripes
,
7563 em
->map_lookup
->num_stripes
);
7569 read_unlock(&em_tree
->lock
);
7574 * Ensure that all dev extents are mapped to correct chunk, otherwise
7575 * later chunk allocation/free would cause unexpected behavior.
7577 * NOTE: This will iterate through the whole device tree, which should be of
7578 * the same size level as the chunk tree. This slightly increases mount time.
7580 int btrfs_verify_dev_extents(struct btrfs_fs_info
*fs_info
)
7582 struct btrfs_path
*path
;
7583 struct btrfs_root
*root
= fs_info
->dev_root
;
7584 struct btrfs_key key
;
7586 u64 prev_dev_ext_end
= 0;
7590 key
.type
= BTRFS_DEV_EXTENT_KEY
;
7593 path
= btrfs_alloc_path();
7597 path
->reada
= READA_FORWARD
;
7598 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7602 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
7603 ret
= btrfs_next_item(root
, path
);
7606 /* No dev extents at all? Not good */
7613 struct extent_buffer
*leaf
= path
->nodes
[0];
7614 struct btrfs_dev_extent
*dext
;
7615 int slot
= path
->slots
[0];
7617 u64 physical_offset
;
7621 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7622 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
7624 devid
= key
.objectid
;
7625 physical_offset
= key
.offset
;
7627 dext
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dev_extent
);
7628 chunk_offset
= btrfs_dev_extent_chunk_offset(leaf
, dext
);
7629 physical_len
= btrfs_dev_extent_length(leaf
, dext
);
7631 /* Check if this dev extent overlaps with the previous one */
7632 if (devid
== prev_devid
&& physical_offset
< prev_dev_ext_end
) {
7634 "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7635 devid
, physical_offset
, prev_dev_ext_end
);
7640 ret
= verify_one_dev_extent(fs_info
, chunk_offset
, devid
,
7641 physical_offset
, physical_len
);
7645 prev_dev_ext_end
= physical_offset
+ physical_len
;
7647 ret
= btrfs_next_item(root
, path
);
7656 /* Ensure all chunks have corresponding dev extents */
7657 ret
= verify_chunk_dev_extent_mapping(fs_info
);
7659 btrfs_free_path(path
);
7664 * Check whether the given block group or device is pinned by any inode being
7665 * used as a swapfile.
7667 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info
*fs_info
, void *ptr
)
7669 struct btrfs_swapfile_pin
*sp
;
7670 struct rb_node
*node
;
7672 spin_lock(&fs_info
->swapfile_pins_lock
);
7673 node
= fs_info
->swapfile_pins
.rb_node
;
7675 sp
= rb_entry(node
, struct btrfs_swapfile_pin
, node
);
7677 node
= node
->rb_left
;
7678 else if (ptr
> sp
->ptr
)
7679 node
= node
->rb_right
;
7683 spin_unlock(&fs_info
->swapfile_pins_lock
);
7684 return node
!= NULL
;