1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/sched/mm.h>
9 #include <linux/slab.h>
10 #include <linux/blkdev.h>
11 #include <linux/ratelimit.h>
12 #include <linux/kthread.h>
13 #include <linux/raid/pq.h>
14 #include <linux/semaphore.h>
15 #include <linux/uuid.h>
16 #include <linux/list_sort.h>
19 #include "extent_map.h"
21 #include "transaction.h"
22 #include "print-tree.h"
25 #include "async-thread.h"
26 #include "check-integrity.h"
27 #include "rcu-string.h"
28 #include "dev-replace.h"
30 #include "tree-checker.h"
31 #include "space-info.h"
32 #include "block-group.h"
35 const struct btrfs_raid_attr btrfs_raid_array
[BTRFS_NR_RAID_TYPES
] = {
36 [BTRFS_RAID_RAID10
] = {
39 .devs_max
= 0, /* 0 == as many as possible */
41 .tolerated_failures
= 1,
45 .raid_name
= "raid10",
46 .bg_flag
= BTRFS_BLOCK_GROUP_RAID10
,
47 .mindev_error
= BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET
,
49 [BTRFS_RAID_RAID1
] = {
54 .tolerated_failures
= 1,
59 .bg_flag
= BTRFS_BLOCK_GROUP_RAID1
,
60 .mindev_error
= BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET
,
62 [BTRFS_RAID_RAID1C3
] = {
67 .tolerated_failures
= 2,
71 .raid_name
= "raid1c3",
72 .bg_flag
= BTRFS_BLOCK_GROUP_RAID1C3
,
73 .mindev_error
= BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET
,
75 [BTRFS_RAID_RAID1C4
] = {
80 .tolerated_failures
= 3,
84 .raid_name
= "raid1c4",
85 .bg_flag
= BTRFS_BLOCK_GROUP_RAID1C4
,
86 .mindev_error
= BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET
,
93 .tolerated_failures
= 0,
98 .bg_flag
= BTRFS_BLOCK_GROUP_DUP
,
101 [BTRFS_RAID_RAID0
] = {
106 .tolerated_failures
= 0,
110 .raid_name
= "raid0",
111 .bg_flag
= BTRFS_BLOCK_GROUP_RAID0
,
114 [BTRFS_RAID_SINGLE
] = {
119 .tolerated_failures
= 0,
123 .raid_name
= "single",
127 [BTRFS_RAID_RAID5
] = {
132 .tolerated_failures
= 1,
136 .raid_name
= "raid5",
137 .bg_flag
= BTRFS_BLOCK_GROUP_RAID5
,
138 .mindev_error
= BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET
,
140 [BTRFS_RAID_RAID6
] = {
145 .tolerated_failures
= 2,
149 .raid_name
= "raid6",
150 .bg_flag
= BTRFS_BLOCK_GROUP_RAID6
,
151 .mindev_error
= BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET
,
155 const char *btrfs_bg_type_to_raid_name(u64 flags
)
157 const int index
= btrfs_bg_flags_to_raid_index(flags
);
159 if (index
>= BTRFS_NR_RAID_TYPES
)
162 return btrfs_raid_array
[index
].raid_name
;
166 * Fill @buf with textual description of @bg_flags, no more than @size_buf
167 * bytes including terminating null byte.
169 void btrfs_describe_block_groups(u64 bg_flags
, char *buf
, u32 size_buf
)
174 u64 flags
= bg_flags
;
175 u32 size_bp
= size_buf
;
182 #define DESCRIBE_FLAG(flag, desc) \
184 if (flags & (flag)) { \
185 ret = snprintf(bp, size_bp, "%s|", (desc)); \
186 if (ret < 0 || ret >= size_bp) \
194 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA
, "data");
195 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM
, "system");
196 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA
, "metadata");
198 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE
, "single");
199 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++)
200 DESCRIBE_FLAG(btrfs_raid_array
[i
].bg_flag
,
201 btrfs_raid_array
[i
].raid_name
);
205 ret
= snprintf(bp
, size_bp
, "0x%llx|", flags
);
209 if (size_bp
< size_buf
)
210 buf
[size_buf
- size_bp
- 1] = '\0'; /* remove last | */
213 * The text is trimmed, it's up to the caller to provide sufficiently
219 static int init_first_rw_device(struct btrfs_trans_handle
*trans
);
220 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info
*fs_info
);
221 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
);
222 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*device
);
223 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
,
224 enum btrfs_map_op op
,
225 u64 logical
, u64
*length
,
226 struct btrfs_bio
**bbio_ret
,
227 int mirror_num
, int need_raid_map
);
233 * There are several mutexes that protect manipulation of devices and low-level
234 * structures like chunks but not block groups, extents or files
236 * uuid_mutex (global lock)
237 * ------------------------
238 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
239 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
240 * device) or requested by the device= mount option
242 * the mutex can be very coarse and can cover long-running operations
244 * protects: updates to fs_devices counters like missing devices, rw devices,
245 * seeding, structure cloning, opening/closing devices at mount/umount time
247 * global::fs_devs - add, remove, updates to the global list
249 * does not protect: manipulation of the fs_devices::devices list in general
250 * but in mount context it could be used to exclude list modifications by eg.
253 * btrfs_device::name - renames (write side), read is RCU
255 * fs_devices::device_list_mutex (per-fs, with RCU)
256 * ------------------------------------------------
257 * protects updates to fs_devices::devices, ie. adding and deleting
259 * simple list traversal with read-only actions can be done with RCU protection
261 * may be used to exclude some operations from running concurrently without any
262 * modifications to the list (see write_all_supers)
264 * Is not required at mount and close times, because our device list is
265 * protected by the uuid_mutex at that point.
269 * protects balance structures (status, state) and context accessed from
270 * several places (internally, ioctl)
274 * protects chunks, adding or removing during allocation, trim or when a new
275 * device is added/removed. Additionally it also protects post_commit_list of
276 * individual devices, since they can be added to the transaction's
277 * post_commit_list only with chunk_mutex held.
281 * a big lock that is held by the cleaner thread and prevents running subvolume
282 * cleaning together with relocation or delayed iputs
294 * Exclusive operations
295 * ====================
297 * Maintains the exclusivity of the following operations that apply to the
298 * whole filesystem and cannot run in parallel.
303 * - Device replace (*)
306 * The device operations (as above) can be in one of the following states:
312 * Only device operations marked with (*) can go into the Paused state for the
315 * - ioctl (only Balance can be Paused through ioctl)
316 * - filesystem remounted as read-only
317 * - filesystem unmounted and mounted as read-only
318 * - system power-cycle and filesystem mounted as read-only
319 * - filesystem or device errors leading to forced read-only
321 * The status of exclusive operation is set and cleared atomically.
322 * During the course of Paused state, fs_info::exclusive_operation remains set.
323 * A device operation in Paused or Running state can be canceled or resumed
324 * either by ioctl (Balance only) or when remounted as read-write.
325 * The exclusive status is cleared when the device operation is canceled or
329 DEFINE_MUTEX(uuid_mutex
);
330 static LIST_HEAD(fs_uuids
);
331 struct list_head
* __attribute_const__
btrfs_get_fs_uuids(void)
337 * alloc_fs_devices - allocate struct btrfs_fs_devices
338 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
339 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
341 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
342 * The returned struct is not linked onto any lists and can be destroyed with
343 * kfree() right away.
345 static struct btrfs_fs_devices
*alloc_fs_devices(const u8
*fsid
,
346 const u8
*metadata_fsid
)
348 struct btrfs_fs_devices
*fs_devs
;
350 fs_devs
= kzalloc(sizeof(*fs_devs
), GFP_KERNEL
);
352 return ERR_PTR(-ENOMEM
);
354 mutex_init(&fs_devs
->device_list_mutex
);
356 INIT_LIST_HEAD(&fs_devs
->devices
);
357 INIT_LIST_HEAD(&fs_devs
->alloc_list
);
358 INIT_LIST_HEAD(&fs_devs
->fs_list
);
359 INIT_LIST_HEAD(&fs_devs
->seed_list
);
361 memcpy(fs_devs
->fsid
, fsid
, BTRFS_FSID_SIZE
);
364 memcpy(fs_devs
->metadata_uuid
, metadata_fsid
, BTRFS_FSID_SIZE
);
366 memcpy(fs_devs
->metadata_uuid
, fsid
, BTRFS_FSID_SIZE
);
371 void btrfs_free_device(struct btrfs_device
*device
)
373 WARN_ON(!list_empty(&device
->post_commit_list
));
374 rcu_string_free(device
->name
);
375 extent_io_tree_release(&device
->alloc_state
);
376 bio_put(device
->flush_bio
);
380 static void free_fs_devices(struct btrfs_fs_devices
*fs_devices
)
382 struct btrfs_device
*device
;
383 WARN_ON(fs_devices
->opened
);
384 while (!list_empty(&fs_devices
->devices
)) {
385 device
= list_entry(fs_devices
->devices
.next
,
386 struct btrfs_device
, dev_list
);
387 list_del(&device
->dev_list
);
388 btrfs_free_device(device
);
393 void __exit
btrfs_cleanup_fs_uuids(void)
395 struct btrfs_fs_devices
*fs_devices
;
397 while (!list_empty(&fs_uuids
)) {
398 fs_devices
= list_entry(fs_uuids
.next
,
399 struct btrfs_fs_devices
, fs_list
);
400 list_del(&fs_devices
->fs_list
);
401 free_fs_devices(fs_devices
);
406 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
407 * Returned struct is not linked onto any lists and must be destroyed using
410 static struct btrfs_device
*__alloc_device(struct btrfs_fs_info
*fs_info
)
412 struct btrfs_device
*dev
;
414 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
416 return ERR_PTR(-ENOMEM
);
419 * Preallocate a bio that's always going to be used for flushing device
420 * barriers and matches the device lifespan
422 dev
->flush_bio
= bio_alloc_bioset(GFP_KERNEL
, 0, NULL
);
423 if (!dev
->flush_bio
) {
425 return ERR_PTR(-ENOMEM
);
428 INIT_LIST_HEAD(&dev
->dev_list
);
429 INIT_LIST_HEAD(&dev
->dev_alloc_list
);
430 INIT_LIST_HEAD(&dev
->post_commit_list
);
432 atomic_set(&dev
->reada_in_flight
, 0);
433 atomic_set(&dev
->dev_stats_ccnt
, 0);
434 btrfs_device_data_ordered_init(dev
, fs_info
);
435 INIT_RADIX_TREE(&dev
->reada_zones
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
436 INIT_RADIX_TREE(&dev
->reada_extents
, GFP_NOFS
& ~__GFP_DIRECT_RECLAIM
);
437 extent_io_tree_init(fs_info
, &dev
->alloc_state
,
438 IO_TREE_DEVICE_ALLOC_STATE
, NULL
);
443 static noinline
struct btrfs_fs_devices
*find_fsid(
444 const u8
*fsid
, const u8
*metadata_fsid
)
446 struct btrfs_fs_devices
*fs_devices
;
450 /* Handle non-split brain cases */
451 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
453 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0
454 && memcmp(metadata_fsid
, fs_devices
->metadata_uuid
,
455 BTRFS_FSID_SIZE
) == 0)
458 if (memcmp(fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
) == 0)
465 static struct btrfs_fs_devices
*find_fsid_with_metadata_uuid(
466 struct btrfs_super_block
*disk_super
)
469 struct btrfs_fs_devices
*fs_devices
;
472 * Handle scanned device having completed its fsid change but
473 * belonging to a fs_devices that was created by first scanning
474 * a device which didn't have its fsid/metadata_uuid changed
475 * at all and the CHANGING_FSID_V2 flag set.
477 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
478 if (fs_devices
->fsid_change
&&
479 memcmp(disk_super
->metadata_uuid
, fs_devices
->fsid
,
480 BTRFS_FSID_SIZE
) == 0 &&
481 memcmp(fs_devices
->fsid
, fs_devices
->metadata_uuid
,
482 BTRFS_FSID_SIZE
) == 0) {
487 * Handle scanned device having completed its fsid change but
488 * belonging to a fs_devices that was created by a device that
489 * has an outdated pair of fsid/metadata_uuid and
490 * CHANGING_FSID_V2 flag set.
492 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
493 if (fs_devices
->fsid_change
&&
494 memcmp(fs_devices
->metadata_uuid
,
495 fs_devices
->fsid
, BTRFS_FSID_SIZE
) != 0 &&
496 memcmp(disk_super
->metadata_uuid
, fs_devices
->metadata_uuid
,
497 BTRFS_FSID_SIZE
) == 0) {
502 return find_fsid(disk_super
->fsid
, disk_super
->metadata_uuid
);
507 btrfs_get_bdev_and_sb(const char *device_path
, fmode_t flags
, void *holder
,
508 int flush
, struct block_device
**bdev
,
509 struct btrfs_super_block
**disk_super
)
513 *bdev
= blkdev_get_by_path(device_path
, flags
, holder
);
516 ret
= PTR_ERR(*bdev
);
521 filemap_write_and_wait((*bdev
)->bd_inode
->i_mapping
);
522 ret
= set_blocksize(*bdev
, BTRFS_BDEV_BLOCKSIZE
);
524 blkdev_put(*bdev
, flags
);
527 invalidate_bdev(*bdev
);
528 *disk_super
= btrfs_read_dev_super(*bdev
);
529 if (IS_ERR(*disk_super
)) {
530 ret
= PTR_ERR(*disk_super
);
531 blkdev_put(*bdev
, flags
);
542 static bool device_path_matched(const char *path
, struct btrfs_device
*device
)
547 found
= strcmp(rcu_str_deref(device
->name
), path
);
554 * Search and remove all stale (devices which are not mounted) devices.
555 * When both inputs are NULL, it will search and release all stale devices.
556 * path: Optional. When provided will it release all unmounted devices
557 * matching this path only.
558 * skip_dev: Optional. Will skip this device when searching for the stale
560 * Return: 0 for success or if @path is NULL.
561 * -EBUSY if @path is a mounted device.
562 * -ENOENT if @path does not match any device in the list.
564 static int btrfs_free_stale_devices(const char *path
,
565 struct btrfs_device
*skip_device
)
567 struct btrfs_fs_devices
*fs_devices
, *tmp_fs_devices
;
568 struct btrfs_device
*device
, *tmp_device
;
574 list_for_each_entry_safe(fs_devices
, tmp_fs_devices
, &fs_uuids
, fs_list
) {
576 mutex_lock(&fs_devices
->device_list_mutex
);
577 list_for_each_entry_safe(device
, tmp_device
,
578 &fs_devices
->devices
, dev_list
) {
579 if (skip_device
&& skip_device
== device
)
581 if (path
&& !device
->name
)
583 if (path
&& !device_path_matched(path
, device
))
585 if (fs_devices
->opened
) {
586 /* for an already deleted device return 0 */
587 if (path
&& ret
!= 0)
592 /* delete the stale device */
593 fs_devices
->num_devices
--;
594 list_del(&device
->dev_list
);
595 btrfs_free_device(device
);
599 mutex_unlock(&fs_devices
->device_list_mutex
);
601 if (fs_devices
->num_devices
== 0) {
602 btrfs_sysfs_remove_fsid(fs_devices
);
603 list_del(&fs_devices
->fs_list
);
604 free_fs_devices(fs_devices
);
612 * This is only used on mount, and we are protected from competing things
613 * messing with our fs_devices by the uuid_mutex, thus we do not need the
614 * fs_devices->device_list_mutex here.
616 static int btrfs_open_one_device(struct btrfs_fs_devices
*fs_devices
,
617 struct btrfs_device
*device
, fmode_t flags
,
620 struct request_queue
*q
;
621 struct block_device
*bdev
;
622 struct btrfs_super_block
*disk_super
;
631 ret
= btrfs_get_bdev_and_sb(device
->name
->str
, flags
, holder
, 1,
636 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
637 if (devid
!= device
->devid
)
638 goto error_free_page
;
640 if (memcmp(device
->uuid
, disk_super
->dev_item
.uuid
, BTRFS_UUID_SIZE
))
641 goto error_free_page
;
643 device
->generation
= btrfs_super_generation(disk_super
);
645 if (btrfs_super_flags(disk_super
) & BTRFS_SUPER_FLAG_SEEDING
) {
646 if (btrfs_super_incompat_flags(disk_super
) &
647 BTRFS_FEATURE_INCOMPAT_METADATA_UUID
) {
649 "BTRFS: Invalid seeding and uuid-changed device detected\n");
650 goto error_free_page
;
653 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
654 fs_devices
->seeding
= true;
656 if (bdev_read_only(bdev
))
657 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
659 set_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
662 q
= bdev_get_queue(bdev
);
663 if (!blk_queue_nonrot(q
))
664 fs_devices
->rotating
= true;
667 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
668 device
->mode
= flags
;
670 fs_devices
->open_devices
++;
671 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
672 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
673 fs_devices
->rw_devices
++;
674 list_add_tail(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
676 btrfs_release_disk_super(disk_super
);
681 btrfs_release_disk_super(disk_super
);
682 blkdev_put(bdev
, flags
);
688 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
689 * being created with a disk that has already completed its fsid change. Such
690 * disk can belong to an fs which has its FSID changed or to one which doesn't.
691 * Handle both cases here.
693 static struct btrfs_fs_devices
*find_fsid_inprogress(
694 struct btrfs_super_block
*disk_super
)
696 struct btrfs_fs_devices
*fs_devices
;
698 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
699 if (memcmp(fs_devices
->metadata_uuid
, fs_devices
->fsid
,
700 BTRFS_FSID_SIZE
) != 0 &&
701 memcmp(fs_devices
->metadata_uuid
, disk_super
->fsid
,
702 BTRFS_FSID_SIZE
) == 0 && !fs_devices
->fsid_change
) {
707 return find_fsid(disk_super
->fsid
, NULL
);
711 static struct btrfs_fs_devices
*find_fsid_changed(
712 struct btrfs_super_block
*disk_super
)
714 struct btrfs_fs_devices
*fs_devices
;
717 * Handles the case where scanned device is part of an fs that had
718 * multiple successful changes of FSID but curently device didn't
719 * observe it. Meaning our fsid will be different than theirs. We need
720 * to handle two subcases :
721 * 1 - The fs still continues to have different METADATA/FSID uuids.
722 * 2 - The fs is switched back to its original FSID (METADATA/FSID
725 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
727 if (memcmp(fs_devices
->metadata_uuid
, fs_devices
->fsid
,
728 BTRFS_FSID_SIZE
) != 0 &&
729 memcmp(fs_devices
->metadata_uuid
, disk_super
->metadata_uuid
,
730 BTRFS_FSID_SIZE
) == 0 &&
731 memcmp(fs_devices
->fsid
, disk_super
->fsid
,
732 BTRFS_FSID_SIZE
) != 0)
735 /* Unchanged UUIDs */
736 if (memcmp(fs_devices
->metadata_uuid
, fs_devices
->fsid
,
737 BTRFS_FSID_SIZE
) == 0 &&
738 memcmp(fs_devices
->fsid
, disk_super
->metadata_uuid
,
739 BTRFS_FSID_SIZE
) == 0)
746 static struct btrfs_fs_devices
*find_fsid_reverted_metadata(
747 struct btrfs_super_block
*disk_super
)
749 struct btrfs_fs_devices
*fs_devices
;
752 * Handle the case where the scanned device is part of an fs whose last
753 * metadata UUID change reverted it to the original FSID. At the same
754 * time * fs_devices was first created by another constitutent device
755 * which didn't fully observe the operation. This results in an
756 * btrfs_fs_devices created with metadata/fsid different AND
757 * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
758 * fs_devices equal to the FSID of the disk.
760 list_for_each_entry(fs_devices
, &fs_uuids
, fs_list
) {
761 if (memcmp(fs_devices
->fsid
, fs_devices
->metadata_uuid
,
762 BTRFS_FSID_SIZE
) != 0 &&
763 memcmp(fs_devices
->metadata_uuid
, disk_super
->fsid
,
764 BTRFS_FSID_SIZE
) == 0 &&
765 fs_devices
->fsid_change
)
772 * Add new device to list of registered devices
775 * device pointer which was just added or updated when successful
776 * error pointer when failed
778 static noinline
struct btrfs_device
*device_list_add(const char *path
,
779 struct btrfs_super_block
*disk_super
,
780 bool *new_device_added
)
782 struct btrfs_device
*device
;
783 struct btrfs_fs_devices
*fs_devices
= NULL
;
784 struct rcu_string
*name
;
785 u64 found_transid
= btrfs_super_generation(disk_super
);
786 u64 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
787 bool has_metadata_uuid
= (btrfs_super_incompat_flags(disk_super
) &
788 BTRFS_FEATURE_INCOMPAT_METADATA_UUID
);
789 bool fsid_change_in_progress
= (btrfs_super_flags(disk_super
) &
790 BTRFS_SUPER_FLAG_CHANGING_FSID_V2
);
792 if (fsid_change_in_progress
) {
793 if (!has_metadata_uuid
)
794 fs_devices
= find_fsid_inprogress(disk_super
);
796 fs_devices
= find_fsid_changed(disk_super
);
797 } else if (has_metadata_uuid
) {
798 fs_devices
= find_fsid_with_metadata_uuid(disk_super
);
800 fs_devices
= find_fsid_reverted_metadata(disk_super
);
802 fs_devices
= find_fsid(disk_super
->fsid
, NULL
);
807 if (has_metadata_uuid
)
808 fs_devices
= alloc_fs_devices(disk_super
->fsid
,
809 disk_super
->metadata_uuid
);
811 fs_devices
= alloc_fs_devices(disk_super
->fsid
, NULL
);
813 if (IS_ERR(fs_devices
))
814 return ERR_CAST(fs_devices
);
816 fs_devices
->fsid_change
= fsid_change_in_progress
;
818 mutex_lock(&fs_devices
->device_list_mutex
);
819 list_add(&fs_devices
->fs_list
, &fs_uuids
);
823 mutex_lock(&fs_devices
->device_list_mutex
);
824 device
= btrfs_find_device(fs_devices
, devid
,
825 disk_super
->dev_item
.uuid
, NULL
, false);
828 * If this disk has been pulled into an fs devices created by
829 * a device which had the CHANGING_FSID_V2 flag then replace the
830 * metadata_uuid/fsid values of the fs_devices.
832 if (fs_devices
->fsid_change
&&
833 found_transid
> fs_devices
->latest_generation
) {
834 memcpy(fs_devices
->fsid
, disk_super
->fsid
,
837 if (has_metadata_uuid
)
838 memcpy(fs_devices
->metadata_uuid
,
839 disk_super
->metadata_uuid
,
842 memcpy(fs_devices
->metadata_uuid
,
843 disk_super
->fsid
, BTRFS_FSID_SIZE
);
845 fs_devices
->fsid_change
= false;
850 if (fs_devices
->opened
) {
851 mutex_unlock(&fs_devices
->device_list_mutex
);
852 return ERR_PTR(-EBUSY
);
855 device
= btrfs_alloc_device(NULL
, &devid
,
856 disk_super
->dev_item
.uuid
);
857 if (IS_ERR(device
)) {
858 mutex_unlock(&fs_devices
->device_list_mutex
);
859 /* we can safely leave the fs_devices entry around */
863 name
= rcu_string_strdup(path
, GFP_NOFS
);
865 btrfs_free_device(device
);
866 mutex_unlock(&fs_devices
->device_list_mutex
);
867 return ERR_PTR(-ENOMEM
);
869 rcu_assign_pointer(device
->name
, name
);
871 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
872 fs_devices
->num_devices
++;
874 device
->fs_devices
= fs_devices
;
875 *new_device_added
= true;
877 if (disk_super
->label
[0])
879 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
880 disk_super
->label
, devid
, found_transid
, path
,
881 current
->comm
, task_pid_nr(current
));
884 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
885 disk_super
->fsid
, devid
, found_transid
, path
,
886 current
->comm
, task_pid_nr(current
));
888 } else if (!device
->name
|| strcmp(device
->name
->str
, path
)) {
890 * When FS is already mounted.
891 * 1. If you are here and if the device->name is NULL that
892 * means this device was missing at time of FS mount.
893 * 2. If you are here and if the device->name is different
894 * from 'path' that means either
895 * a. The same device disappeared and reappeared with
897 * b. The missing-disk-which-was-replaced, has
900 * We must allow 1 and 2a above. But 2b would be a spurious
903 * Further in case of 1 and 2a above, the disk at 'path'
904 * would have missed some transaction when it was away and
905 * in case of 2a the stale bdev has to be updated as well.
906 * 2b must not be allowed at all time.
910 * For now, we do allow update to btrfs_fs_device through the
911 * btrfs dev scan cli after FS has been mounted. We're still
912 * tracking a problem where systems fail mount by subvolume id
913 * when we reject replacement on a mounted FS.
915 if (!fs_devices
->opened
&& found_transid
< device
->generation
) {
917 * That is if the FS is _not_ mounted and if you
918 * are here, that means there is more than one
919 * disk with same uuid and devid.We keep the one
920 * with larger generation number or the last-in if
921 * generation are equal.
923 mutex_unlock(&fs_devices
->device_list_mutex
);
924 return ERR_PTR(-EEXIST
);
928 * We are going to replace the device path for a given devid,
929 * make sure it's the same device if the device is mounted
932 struct block_device
*path_bdev
;
934 path_bdev
= lookup_bdev(path
);
935 if (IS_ERR(path_bdev
)) {
936 mutex_unlock(&fs_devices
->device_list_mutex
);
937 return ERR_CAST(path_bdev
);
940 if (device
->bdev
!= path_bdev
) {
942 mutex_unlock(&fs_devices
->device_list_mutex
);
943 btrfs_warn_in_rcu(device
->fs_info
,
944 "duplicate device %s devid %llu generation %llu scanned by %s (%d)",
945 path
, devid
, found_transid
,
947 task_pid_nr(current
));
948 return ERR_PTR(-EEXIST
);
951 btrfs_info_in_rcu(device
->fs_info
,
952 "devid %llu device path %s changed to %s scanned by %s (%d)",
953 devid
, rcu_str_deref(device
->name
),
955 task_pid_nr(current
));
958 name
= rcu_string_strdup(path
, GFP_NOFS
);
960 mutex_unlock(&fs_devices
->device_list_mutex
);
961 return ERR_PTR(-ENOMEM
);
963 rcu_string_free(device
->name
);
964 rcu_assign_pointer(device
->name
, name
);
965 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
)) {
966 fs_devices
->missing_devices
--;
967 clear_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
972 * Unmount does not free the btrfs_device struct but would zero
973 * generation along with most of the other members. So just update
974 * it back. We need it to pick the disk with largest generation
977 if (!fs_devices
->opened
) {
978 device
->generation
= found_transid
;
979 fs_devices
->latest_generation
= max_t(u64
, found_transid
,
980 fs_devices
->latest_generation
);
983 fs_devices
->total_devices
= btrfs_super_num_devices(disk_super
);
985 mutex_unlock(&fs_devices
->device_list_mutex
);
989 static struct btrfs_fs_devices
*clone_fs_devices(struct btrfs_fs_devices
*orig
)
991 struct btrfs_fs_devices
*fs_devices
;
992 struct btrfs_device
*device
;
993 struct btrfs_device
*orig_dev
;
996 fs_devices
= alloc_fs_devices(orig
->fsid
, NULL
);
997 if (IS_ERR(fs_devices
))
1000 mutex_lock(&orig
->device_list_mutex
);
1001 fs_devices
->total_devices
= orig
->total_devices
;
1003 list_for_each_entry(orig_dev
, &orig
->devices
, dev_list
) {
1004 struct rcu_string
*name
;
1006 device
= btrfs_alloc_device(NULL
, &orig_dev
->devid
,
1008 if (IS_ERR(device
)) {
1009 ret
= PTR_ERR(device
);
1014 * This is ok to do without rcu read locked because we hold the
1015 * uuid mutex so nothing we touch in here is going to disappear.
1017 if (orig_dev
->name
) {
1018 name
= rcu_string_strdup(orig_dev
->name
->str
,
1021 btrfs_free_device(device
);
1025 rcu_assign_pointer(device
->name
, name
);
1028 list_add(&device
->dev_list
, &fs_devices
->devices
);
1029 device
->fs_devices
= fs_devices
;
1030 fs_devices
->num_devices
++;
1032 mutex_unlock(&orig
->device_list_mutex
);
1035 mutex_unlock(&orig
->device_list_mutex
);
1036 free_fs_devices(fs_devices
);
1037 return ERR_PTR(ret
);
1040 static void __btrfs_free_extra_devids(struct btrfs_fs_devices
*fs_devices
,
1041 int step
, struct btrfs_device
**latest_dev
)
1043 struct btrfs_device
*device
, *next
;
1045 /* This is the initialized path, it is safe to release the devices. */
1046 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
1047 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
)) {
1048 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
,
1049 &device
->dev_state
) &&
1050 !test_bit(BTRFS_DEV_STATE_MISSING
,
1051 &device
->dev_state
) &&
1053 device
->generation
> (*latest_dev
)->generation
)) {
1054 *latest_dev
= device
;
1060 * We have already validated the presence of BTRFS_DEV_REPLACE_DEVID,
1061 * in btrfs_init_dev_replace() so just continue.
1063 if (device
->devid
== BTRFS_DEV_REPLACE_DEVID
)
1067 blkdev_put(device
->bdev
, device
->mode
);
1068 device
->bdev
= NULL
;
1069 fs_devices
->open_devices
--;
1071 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1072 list_del_init(&device
->dev_alloc_list
);
1073 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
1075 list_del_init(&device
->dev_list
);
1076 fs_devices
->num_devices
--;
1077 btrfs_free_device(device
);
1083 * After we have read the system tree and know devids belonging to this
1084 * filesystem, remove the device which does not belong there.
1086 void btrfs_free_extra_devids(struct btrfs_fs_devices
*fs_devices
, int step
)
1088 struct btrfs_device
*latest_dev
= NULL
;
1089 struct btrfs_fs_devices
*seed_dev
;
1091 mutex_lock(&uuid_mutex
);
1092 __btrfs_free_extra_devids(fs_devices
, step
, &latest_dev
);
1094 list_for_each_entry(seed_dev
, &fs_devices
->seed_list
, seed_list
)
1095 __btrfs_free_extra_devids(seed_dev
, step
, &latest_dev
);
1097 fs_devices
->latest_bdev
= latest_dev
->bdev
;
1099 mutex_unlock(&uuid_mutex
);
1102 static void btrfs_close_bdev(struct btrfs_device
*device
)
1107 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
1108 sync_blockdev(device
->bdev
);
1109 invalidate_bdev(device
->bdev
);
1112 blkdev_put(device
->bdev
, device
->mode
);
1115 static void btrfs_close_one_device(struct btrfs_device
*device
)
1117 struct btrfs_fs_devices
*fs_devices
= device
->fs_devices
;
1119 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
1120 device
->devid
!= BTRFS_DEV_REPLACE_DEVID
) {
1121 list_del_init(&device
->dev_alloc_list
);
1122 fs_devices
->rw_devices
--;
1125 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
))
1126 fs_devices
->missing_devices
--;
1128 btrfs_close_bdev(device
);
1130 fs_devices
->open_devices
--;
1131 device
->bdev
= NULL
;
1133 clear_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
1135 device
->fs_info
= NULL
;
1136 atomic_set(&device
->dev_stats_ccnt
, 0);
1137 extent_io_tree_release(&device
->alloc_state
);
1139 /* Verify the device is back in a pristine state */
1140 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT
, &device
->dev_state
));
1141 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
));
1142 ASSERT(list_empty(&device
->dev_alloc_list
));
1143 ASSERT(list_empty(&device
->post_commit_list
));
1144 ASSERT(atomic_read(&device
->reada_in_flight
) == 0);
1147 static void close_fs_devices(struct btrfs_fs_devices
*fs_devices
)
1149 struct btrfs_device
*device
, *tmp
;
1151 lockdep_assert_held(&uuid_mutex
);
1153 if (--fs_devices
->opened
> 0)
1156 list_for_each_entry_safe(device
, tmp
, &fs_devices
->devices
, dev_list
)
1157 btrfs_close_one_device(device
);
1159 WARN_ON(fs_devices
->open_devices
);
1160 WARN_ON(fs_devices
->rw_devices
);
1161 fs_devices
->opened
= 0;
1162 fs_devices
->seeding
= false;
1163 fs_devices
->fs_info
= NULL
;
1166 void btrfs_close_devices(struct btrfs_fs_devices
*fs_devices
)
1169 struct btrfs_fs_devices
*tmp
;
1171 mutex_lock(&uuid_mutex
);
1172 close_fs_devices(fs_devices
);
1173 if (!fs_devices
->opened
)
1174 list_splice_init(&fs_devices
->seed_list
, &list
);
1176 list_for_each_entry_safe(fs_devices
, tmp
, &list
, seed_list
) {
1177 close_fs_devices(fs_devices
);
1178 list_del(&fs_devices
->seed_list
);
1179 free_fs_devices(fs_devices
);
1181 mutex_unlock(&uuid_mutex
);
1184 static int open_fs_devices(struct btrfs_fs_devices
*fs_devices
,
1185 fmode_t flags
, void *holder
)
1187 struct btrfs_device
*device
;
1188 struct btrfs_device
*latest_dev
= NULL
;
1189 struct btrfs_device
*tmp_device
;
1191 flags
|= FMODE_EXCL
;
1193 list_for_each_entry_safe(device
, tmp_device
, &fs_devices
->devices
,
1197 ret
= btrfs_open_one_device(fs_devices
, device
, flags
, holder
);
1199 (!latest_dev
|| device
->generation
> latest_dev
->generation
)) {
1200 latest_dev
= device
;
1201 } else if (ret
== -ENODATA
) {
1202 fs_devices
->num_devices
--;
1203 list_del(&device
->dev_list
);
1204 btrfs_free_device(device
);
1207 if (fs_devices
->open_devices
== 0)
1210 fs_devices
->opened
= 1;
1211 fs_devices
->latest_bdev
= latest_dev
->bdev
;
1212 fs_devices
->total_rw_bytes
= 0;
1213 fs_devices
->chunk_alloc_policy
= BTRFS_CHUNK_ALLOC_REGULAR
;
1218 static int devid_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1220 struct btrfs_device
*dev1
, *dev2
;
1222 dev1
= list_entry(a
, struct btrfs_device
, dev_list
);
1223 dev2
= list_entry(b
, struct btrfs_device
, dev_list
);
1225 if (dev1
->devid
< dev2
->devid
)
1227 else if (dev1
->devid
> dev2
->devid
)
1232 int btrfs_open_devices(struct btrfs_fs_devices
*fs_devices
,
1233 fmode_t flags
, void *holder
)
1237 lockdep_assert_held(&uuid_mutex
);
1239 * The device_list_mutex cannot be taken here in case opening the
1240 * underlying device takes further locks like bd_mutex.
1242 * We also don't need the lock here as this is called during mount and
1243 * exclusion is provided by uuid_mutex
1246 if (fs_devices
->opened
) {
1247 fs_devices
->opened
++;
1250 list_sort(NULL
, &fs_devices
->devices
, devid_cmp
);
1251 ret
= open_fs_devices(fs_devices
, flags
, holder
);
1257 void btrfs_release_disk_super(struct btrfs_super_block
*super
)
1259 struct page
*page
= virt_to_page(super
);
1264 static struct btrfs_super_block
*btrfs_read_disk_super(struct block_device
*bdev
,
1267 struct btrfs_super_block
*disk_super
;
1272 /* make sure our super fits in the device */
1273 if (bytenr
+ PAGE_SIZE
>= i_size_read(bdev
->bd_inode
))
1274 return ERR_PTR(-EINVAL
);
1276 /* make sure our super fits in the page */
1277 if (sizeof(*disk_super
) > PAGE_SIZE
)
1278 return ERR_PTR(-EINVAL
);
1280 /* make sure our super doesn't straddle pages on disk */
1281 index
= bytenr
>> PAGE_SHIFT
;
1282 if ((bytenr
+ sizeof(*disk_super
) - 1) >> PAGE_SHIFT
!= index
)
1283 return ERR_PTR(-EINVAL
);
1285 /* pull in the page with our super */
1286 page
= read_cache_page_gfp(bdev
->bd_inode
->i_mapping
, index
, GFP_KERNEL
);
1289 return ERR_CAST(page
);
1291 p
= page_address(page
);
1293 /* align our pointer to the offset of the super block */
1294 disk_super
= p
+ offset_in_page(bytenr
);
1296 if (btrfs_super_bytenr(disk_super
) != bytenr
||
1297 btrfs_super_magic(disk_super
) != BTRFS_MAGIC
) {
1298 btrfs_release_disk_super(p
);
1299 return ERR_PTR(-EINVAL
);
1302 if (disk_super
->label
[0] && disk_super
->label
[BTRFS_LABEL_SIZE
- 1])
1303 disk_super
->label
[BTRFS_LABEL_SIZE
- 1] = 0;
1308 int btrfs_forget_devices(const char *path
)
1312 mutex_lock(&uuid_mutex
);
1313 ret
= btrfs_free_stale_devices(strlen(path
) ? path
: NULL
, NULL
);
1314 mutex_unlock(&uuid_mutex
);
1320 * Look for a btrfs signature on a device. This may be called out of the mount path
1321 * and we are not allowed to call set_blocksize during the scan. The superblock
1322 * is read via pagecache
1324 struct btrfs_device
*btrfs_scan_one_device(const char *path
, fmode_t flags
,
1327 struct btrfs_super_block
*disk_super
;
1328 bool new_device_added
= false;
1329 struct btrfs_device
*device
= NULL
;
1330 struct block_device
*bdev
;
1333 lockdep_assert_held(&uuid_mutex
);
1336 * we would like to check all the supers, but that would make
1337 * a btrfs mount succeed after a mkfs from a different FS.
1338 * So, we need to add a special mount option to scan for
1339 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1341 bytenr
= btrfs_sb_offset(0);
1342 flags
|= FMODE_EXCL
;
1344 bdev
= blkdev_get_by_path(path
, flags
, holder
);
1346 return ERR_CAST(bdev
);
1348 disk_super
= btrfs_read_disk_super(bdev
, bytenr
);
1349 if (IS_ERR(disk_super
)) {
1350 device
= ERR_CAST(disk_super
);
1351 goto error_bdev_put
;
1354 device
= device_list_add(path
, disk_super
, &new_device_added
);
1355 if (!IS_ERR(device
)) {
1356 if (new_device_added
)
1357 btrfs_free_stale_devices(path
, device
);
1360 btrfs_release_disk_super(disk_super
);
1363 blkdev_put(bdev
, flags
);
1369 * Try to find a chunk that intersects [start, start + len] range and when one
1370 * such is found, record the end of it in *start
1372 static bool contains_pending_extent(struct btrfs_device
*device
, u64
*start
,
1375 u64 physical_start
, physical_end
;
1377 lockdep_assert_held(&device
->fs_info
->chunk_mutex
);
1379 if (!find_first_extent_bit(&device
->alloc_state
, *start
,
1380 &physical_start
, &physical_end
,
1381 CHUNK_ALLOCATED
, NULL
)) {
1383 if (in_range(physical_start
, *start
, len
) ||
1384 in_range(*start
, physical_start
,
1385 physical_end
- physical_start
)) {
1386 *start
= physical_end
+ 1;
1393 static u64
dev_extent_search_start(struct btrfs_device
*device
, u64 start
)
1395 switch (device
->fs_devices
->chunk_alloc_policy
) {
1396 case BTRFS_CHUNK_ALLOC_REGULAR
:
1398 * We don't want to overwrite the superblock on the drive nor
1399 * any area used by the boot loader (grub for example), so we
1400 * make sure to start at an offset of at least 1MB.
1402 return max_t(u64
, start
, SZ_1M
);
1409 * dev_extent_hole_check - check if specified hole is suitable for allocation
1410 * @device: the device which we have the hole
1411 * @hole_start: starting position of the hole
1412 * @hole_size: the size of the hole
1413 * @num_bytes: the size of the free space that we need
1415 * This function may modify @hole_start and @hole_end to reflect the suitable
1416 * position for allocation. Returns 1 if hole position is updated, 0 otherwise.
1418 static bool dev_extent_hole_check(struct btrfs_device
*device
, u64
*hole_start
,
1419 u64
*hole_size
, u64 num_bytes
)
1421 bool changed
= false;
1422 u64 hole_end
= *hole_start
+ *hole_size
;
1425 * Check before we set max_hole_start, otherwise we could end up
1426 * sending back this offset anyway.
1428 if (contains_pending_extent(device
, hole_start
, *hole_size
)) {
1429 if (hole_end
>= *hole_start
)
1430 *hole_size
= hole_end
- *hole_start
;
1436 switch (device
->fs_devices
->chunk_alloc_policy
) {
1437 case BTRFS_CHUNK_ALLOC_REGULAR
:
1438 /* No extra check */
1448 * find_free_dev_extent_start - find free space in the specified device
1449 * @device: the device which we search the free space in
1450 * @num_bytes: the size of the free space that we need
1451 * @search_start: the position from which to begin the search
1452 * @start: store the start of the free space.
1453 * @len: the size of the free space. that we find, or the size
1454 * of the max free space if we don't find suitable free space
1456 * this uses a pretty simple search, the expectation is that it is
1457 * called very infrequently and that a given device has a small number
1460 * @start is used to store the start of the free space if we find. But if we
1461 * don't find suitable free space, it will be used to store the start position
1462 * of the max free space.
1464 * @len is used to store the size of the free space that we find.
1465 * But if we don't find suitable free space, it is used to store the size of
1466 * the max free space.
1468 * NOTE: This function will search *commit* root of device tree, and does extra
1469 * check to ensure dev extents are not double allocated.
1470 * This makes the function safe to allocate dev extents but may not report
1471 * correct usable device space, as device extent freed in current transaction
1472 * is not reported as avaiable.
1474 static int find_free_dev_extent_start(struct btrfs_device
*device
,
1475 u64 num_bytes
, u64 search_start
, u64
*start
,
1478 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1479 struct btrfs_root
*root
= fs_info
->dev_root
;
1480 struct btrfs_key key
;
1481 struct btrfs_dev_extent
*dev_extent
;
1482 struct btrfs_path
*path
;
1487 u64 search_end
= device
->total_bytes
;
1490 struct extent_buffer
*l
;
1492 search_start
= dev_extent_search_start(device
, search_start
);
1494 path
= btrfs_alloc_path();
1498 max_hole_start
= search_start
;
1502 if (search_start
>= search_end
||
1503 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
1508 path
->reada
= READA_FORWARD
;
1509 path
->search_commit_root
= 1;
1510 path
->skip_locking
= 1;
1512 key
.objectid
= device
->devid
;
1513 key
.offset
= search_start
;
1514 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1516 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1520 ret
= btrfs_previous_item(root
, path
, key
.objectid
, key
.type
);
1527 slot
= path
->slots
[0];
1528 if (slot
>= btrfs_header_nritems(l
)) {
1529 ret
= btrfs_next_leaf(root
, path
);
1537 btrfs_item_key_to_cpu(l
, &key
, slot
);
1539 if (key
.objectid
< device
->devid
)
1542 if (key
.objectid
> device
->devid
)
1545 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
1548 if (key
.offset
> search_start
) {
1549 hole_size
= key
.offset
- search_start
;
1550 dev_extent_hole_check(device
, &search_start
, &hole_size
,
1553 if (hole_size
> max_hole_size
) {
1554 max_hole_start
= search_start
;
1555 max_hole_size
= hole_size
;
1559 * If this free space is greater than which we need,
1560 * it must be the max free space that we have found
1561 * until now, so max_hole_start must point to the start
1562 * of this free space and the length of this free space
1563 * is stored in max_hole_size. Thus, we return
1564 * max_hole_start and max_hole_size and go back to the
1567 if (hole_size
>= num_bytes
) {
1573 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
1574 extent_end
= key
.offset
+ btrfs_dev_extent_length(l
,
1576 if (extent_end
> search_start
)
1577 search_start
= extent_end
;
1584 * At this point, search_start should be the end of
1585 * allocated dev extents, and when shrinking the device,
1586 * search_end may be smaller than search_start.
1588 if (search_end
> search_start
) {
1589 hole_size
= search_end
- search_start
;
1590 if (dev_extent_hole_check(device
, &search_start
, &hole_size
,
1592 btrfs_release_path(path
);
1596 if (hole_size
> max_hole_size
) {
1597 max_hole_start
= search_start
;
1598 max_hole_size
= hole_size
;
1603 if (max_hole_size
< num_bytes
)
1609 btrfs_free_path(path
);
1610 *start
= max_hole_start
;
1612 *len
= max_hole_size
;
1616 int find_free_dev_extent(struct btrfs_device
*device
, u64 num_bytes
,
1617 u64
*start
, u64
*len
)
1619 /* FIXME use last free of some kind */
1620 return find_free_dev_extent_start(device
, num_bytes
, 0, start
, len
);
1623 static int btrfs_free_dev_extent(struct btrfs_trans_handle
*trans
,
1624 struct btrfs_device
*device
,
1625 u64 start
, u64
*dev_extent_len
)
1627 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1628 struct btrfs_root
*root
= fs_info
->dev_root
;
1630 struct btrfs_path
*path
;
1631 struct btrfs_key key
;
1632 struct btrfs_key found_key
;
1633 struct extent_buffer
*leaf
= NULL
;
1634 struct btrfs_dev_extent
*extent
= NULL
;
1636 path
= btrfs_alloc_path();
1640 key
.objectid
= device
->devid
;
1642 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1644 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1646 ret
= btrfs_previous_item(root
, path
, key
.objectid
,
1647 BTRFS_DEV_EXTENT_KEY
);
1650 leaf
= path
->nodes
[0];
1651 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1652 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1653 struct btrfs_dev_extent
);
1654 BUG_ON(found_key
.offset
> start
|| found_key
.offset
+
1655 btrfs_dev_extent_length(leaf
, extent
) < start
);
1657 btrfs_release_path(path
);
1659 } else if (ret
== 0) {
1660 leaf
= path
->nodes
[0];
1661 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1662 struct btrfs_dev_extent
);
1664 btrfs_handle_fs_error(fs_info
, ret
, "Slot search failed");
1668 *dev_extent_len
= btrfs_dev_extent_length(leaf
, extent
);
1670 ret
= btrfs_del_item(trans
, root
, path
);
1672 btrfs_handle_fs_error(fs_info
, ret
,
1673 "Failed to remove dev extent item");
1675 set_bit(BTRFS_TRANS_HAVE_FREE_BGS
, &trans
->transaction
->flags
);
1678 btrfs_free_path(path
);
1682 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle
*trans
,
1683 struct btrfs_device
*device
,
1684 u64 chunk_offset
, u64 start
, u64 num_bytes
)
1687 struct btrfs_path
*path
;
1688 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1689 struct btrfs_root
*root
= fs_info
->dev_root
;
1690 struct btrfs_dev_extent
*extent
;
1691 struct extent_buffer
*leaf
;
1692 struct btrfs_key key
;
1694 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
));
1695 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
));
1696 path
= btrfs_alloc_path();
1700 key
.objectid
= device
->devid
;
1702 key
.type
= BTRFS_DEV_EXTENT_KEY
;
1703 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
1708 leaf
= path
->nodes
[0];
1709 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
1710 struct btrfs_dev_extent
);
1711 btrfs_set_dev_extent_chunk_tree(leaf
, extent
,
1712 BTRFS_CHUNK_TREE_OBJECTID
);
1713 btrfs_set_dev_extent_chunk_objectid(leaf
, extent
,
1714 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
1715 btrfs_set_dev_extent_chunk_offset(leaf
, extent
, chunk_offset
);
1717 btrfs_set_dev_extent_length(leaf
, extent
, num_bytes
);
1718 btrfs_mark_buffer_dirty(leaf
);
1720 btrfs_free_path(path
);
1724 static u64
find_next_chunk(struct btrfs_fs_info
*fs_info
)
1726 struct extent_map_tree
*em_tree
;
1727 struct extent_map
*em
;
1731 em_tree
= &fs_info
->mapping_tree
;
1732 read_lock(&em_tree
->lock
);
1733 n
= rb_last(&em_tree
->map
.rb_root
);
1735 em
= rb_entry(n
, struct extent_map
, rb_node
);
1736 ret
= em
->start
+ em
->len
;
1738 read_unlock(&em_tree
->lock
);
1743 static noinline
int find_next_devid(struct btrfs_fs_info
*fs_info
,
1747 struct btrfs_key key
;
1748 struct btrfs_key found_key
;
1749 struct btrfs_path
*path
;
1751 path
= btrfs_alloc_path();
1755 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1756 key
.type
= BTRFS_DEV_ITEM_KEY
;
1757 key
.offset
= (u64
)-1;
1759 ret
= btrfs_search_slot(NULL
, fs_info
->chunk_root
, &key
, path
, 0, 0);
1765 btrfs_err(fs_info
, "corrupted chunk tree devid -1 matched");
1770 ret
= btrfs_previous_item(fs_info
->chunk_root
, path
,
1771 BTRFS_DEV_ITEMS_OBJECTID
,
1772 BTRFS_DEV_ITEM_KEY
);
1776 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1778 *devid_ret
= found_key
.offset
+ 1;
1782 btrfs_free_path(path
);
1787 * the device information is stored in the chunk root
1788 * the btrfs_device struct should be fully filled in
1790 static int btrfs_add_dev_item(struct btrfs_trans_handle
*trans
,
1791 struct btrfs_device
*device
)
1794 struct btrfs_path
*path
;
1795 struct btrfs_dev_item
*dev_item
;
1796 struct extent_buffer
*leaf
;
1797 struct btrfs_key key
;
1800 path
= btrfs_alloc_path();
1804 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1805 key
.type
= BTRFS_DEV_ITEM_KEY
;
1806 key
.offset
= device
->devid
;
1808 ret
= btrfs_insert_empty_item(trans
, trans
->fs_info
->chunk_root
, path
,
1809 &key
, sizeof(*dev_item
));
1813 leaf
= path
->nodes
[0];
1814 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
1816 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
1817 btrfs_set_device_generation(leaf
, dev_item
, 0);
1818 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
1819 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
1820 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
1821 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
1822 btrfs_set_device_total_bytes(leaf
, dev_item
,
1823 btrfs_device_get_disk_total_bytes(device
));
1824 btrfs_set_device_bytes_used(leaf
, dev_item
,
1825 btrfs_device_get_bytes_used(device
));
1826 btrfs_set_device_group(leaf
, dev_item
, 0);
1827 btrfs_set_device_seek_speed(leaf
, dev_item
, 0);
1828 btrfs_set_device_bandwidth(leaf
, dev_item
, 0);
1829 btrfs_set_device_start_offset(leaf
, dev_item
, 0);
1831 ptr
= btrfs_device_uuid(dev_item
);
1832 write_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
1833 ptr
= btrfs_device_fsid(dev_item
);
1834 write_extent_buffer(leaf
, trans
->fs_info
->fs_devices
->metadata_uuid
,
1835 ptr
, BTRFS_FSID_SIZE
);
1836 btrfs_mark_buffer_dirty(leaf
);
1840 btrfs_free_path(path
);
1845 * Function to update ctime/mtime for a given device path.
1846 * Mainly used for ctime/mtime based probe like libblkid.
1848 static void update_dev_time(const char *path_name
)
1852 filp
= filp_open(path_name
, O_RDWR
, 0);
1855 file_update_time(filp
);
1856 filp_close(filp
, NULL
);
1859 static int btrfs_rm_dev_item(struct btrfs_device
*device
)
1861 struct btrfs_root
*root
= device
->fs_info
->chunk_root
;
1863 struct btrfs_path
*path
;
1864 struct btrfs_key key
;
1865 struct btrfs_trans_handle
*trans
;
1867 path
= btrfs_alloc_path();
1871 trans
= btrfs_start_transaction(root
, 0);
1872 if (IS_ERR(trans
)) {
1873 btrfs_free_path(path
);
1874 return PTR_ERR(trans
);
1876 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
1877 key
.type
= BTRFS_DEV_ITEM_KEY
;
1878 key
.offset
= device
->devid
;
1880 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
1884 btrfs_abort_transaction(trans
, ret
);
1885 btrfs_end_transaction(trans
);
1889 ret
= btrfs_del_item(trans
, root
, path
);
1891 btrfs_abort_transaction(trans
, ret
);
1892 btrfs_end_transaction(trans
);
1896 btrfs_free_path(path
);
1898 ret
= btrfs_commit_transaction(trans
);
1903 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1904 * filesystem. It's up to the caller to adjust that number regarding eg. device
1907 static int btrfs_check_raid_min_devices(struct btrfs_fs_info
*fs_info
,
1915 seq
= read_seqbegin(&fs_info
->profiles_lock
);
1917 all_avail
= fs_info
->avail_data_alloc_bits
|
1918 fs_info
->avail_system_alloc_bits
|
1919 fs_info
->avail_metadata_alloc_bits
;
1920 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
1922 for (i
= 0; i
< BTRFS_NR_RAID_TYPES
; i
++) {
1923 if (!(all_avail
& btrfs_raid_array
[i
].bg_flag
))
1926 if (num_devices
< btrfs_raid_array
[i
].devs_min
) {
1927 int ret
= btrfs_raid_array
[i
].mindev_error
;
1937 static struct btrfs_device
* btrfs_find_next_active_device(
1938 struct btrfs_fs_devices
*fs_devs
, struct btrfs_device
*device
)
1940 struct btrfs_device
*next_device
;
1942 list_for_each_entry(next_device
, &fs_devs
->devices
, dev_list
) {
1943 if (next_device
!= device
&&
1944 !test_bit(BTRFS_DEV_STATE_MISSING
, &next_device
->dev_state
)
1945 && next_device
->bdev
)
1953 * Helper function to check if the given device is part of s_bdev / latest_bdev
1954 * and replace it with the provided or the next active device, in the context
1955 * where this function called, there should be always be another device (or
1956 * this_dev) which is active.
1958 void __cold
btrfs_assign_next_active_device(struct btrfs_device
*device
,
1959 struct btrfs_device
*next_device
)
1961 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
1964 next_device
= btrfs_find_next_active_device(fs_info
->fs_devices
,
1966 ASSERT(next_device
);
1968 if (fs_info
->sb
->s_bdev
&&
1969 (fs_info
->sb
->s_bdev
== device
->bdev
))
1970 fs_info
->sb
->s_bdev
= next_device
->bdev
;
1972 if (fs_info
->fs_devices
->latest_bdev
== device
->bdev
)
1973 fs_info
->fs_devices
->latest_bdev
= next_device
->bdev
;
1977 * Return btrfs_fs_devices::num_devices excluding the device that's being
1978 * currently replaced.
1980 static u64
btrfs_num_devices(struct btrfs_fs_info
*fs_info
)
1982 u64 num_devices
= fs_info
->fs_devices
->num_devices
;
1984 down_read(&fs_info
->dev_replace
.rwsem
);
1985 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
)) {
1986 ASSERT(num_devices
> 1);
1989 up_read(&fs_info
->dev_replace
.rwsem
);
1994 void btrfs_scratch_superblocks(struct btrfs_fs_info
*fs_info
,
1995 struct block_device
*bdev
,
1996 const char *device_path
)
1998 struct btrfs_super_block
*disk_super
;
2004 for (copy_num
= 0; copy_num
< BTRFS_SUPER_MIRROR_MAX
; copy_num
++) {
2008 disk_super
= btrfs_read_dev_one_super(bdev
, copy_num
);
2009 if (IS_ERR(disk_super
))
2012 memset(&disk_super
->magic
, 0, sizeof(disk_super
->magic
));
2014 page
= virt_to_page(disk_super
);
2015 set_page_dirty(page
);
2017 /* write_on_page() unlocks the page */
2018 ret
= write_one_page(page
);
2021 "error clearing superblock number %d (%d)",
2023 btrfs_release_disk_super(disk_super
);
2027 /* Notify udev that device has changed */
2028 btrfs_kobject_uevent(bdev
, KOBJ_CHANGE
);
2030 /* Update ctime/mtime for device path for libblkid */
2031 update_dev_time(device_path
);
2034 int btrfs_rm_device(struct btrfs_fs_info
*fs_info
, const char *device_path
,
2037 struct btrfs_device
*device
;
2038 struct btrfs_fs_devices
*cur_devices
;
2039 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2043 mutex_lock(&uuid_mutex
);
2045 num_devices
= btrfs_num_devices(fs_info
);
2047 ret
= btrfs_check_raid_min_devices(fs_info
, num_devices
- 1);
2051 device
= btrfs_find_device_by_devspec(fs_info
, devid
, device_path
);
2053 if (IS_ERR(device
)) {
2054 if (PTR_ERR(device
) == -ENOENT
&&
2055 strcmp(device_path
, "missing") == 0)
2056 ret
= BTRFS_ERROR_DEV_MISSING_NOT_FOUND
;
2058 ret
= PTR_ERR(device
);
2062 if (btrfs_pinned_by_swapfile(fs_info
, device
)) {
2063 btrfs_warn_in_rcu(fs_info
,
2064 "cannot remove device %s (devid %llu) due to active swapfile",
2065 rcu_str_deref(device
->name
), device
->devid
);
2070 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
2071 ret
= BTRFS_ERROR_DEV_TGT_REPLACE
;
2075 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
2076 fs_info
->fs_devices
->rw_devices
== 1) {
2077 ret
= BTRFS_ERROR_DEV_ONLY_WRITABLE
;
2081 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
2082 mutex_lock(&fs_info
->chunk_mutex
);
2083 list_del_init(&device
->dev_alloc_list
);
2084 device
->fs_devices
->rw_devices
--;
2085 mutex_unlock(&fs_info
->chunk_mutex
);
2088 mutex_unlock(&uuid_mutex
);
2089 ret
= btrfs_shrink_device(device
, 0);
2091 btrfs_reada_remove_dev(device
);
2092 mutex_lock(&uuid_mutex
);
2097 * TODO: the superblock still includes this device in its num_devices
2098 * counter although write_all_supers() is not locked out. This
2099 * could give a filesystem state which requires a degraded mount.
2101 ret
= btrfs_rm_dev_item(device
);
2105 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
2106 btrfs_scrub_cancel_dev(device
);
2109 * the device list mutex makes sure that we don't change
2110 * the device list while someone else is writing out all
2111 * the device supers. Whoever is writing all supers, should
2112 * lock the device list mutex before getting the number of
2113 * devices in the super block (super_copy). Conversely,
2114 * whoever updates the number of devices in the super block
2115 * (super_copy) should hold the device list mutex.
2119 * In normal cases the cur_devices == fs_devices. But in case
2120 * of deleting a seed device, the cur_devices should point to
2121 * its own fs_devices listed under the fs_devices->seed.
2123 cur_devices
= device
->fs_devices
;
2124 mutex_lock(&fs_devices
->device_list_mutex
);
2125 list_del_rcu(&device
->dev_list
);
2127 cur_devices
->num_devices
--;
2128 cur_devices
->total_devices
--;
2129 /* Update total_devices of the parent fs_devices if it's seed */
2130 if (cur_devices
!= fs_devices
)
2131 fs_devices
->total_devices
--;
2133 if (test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
))
2134 cur_devices
->missing_devices
--;
2136 btrfs_assign_next_active_device(device
, NULL
);
2139 cur_devices
->open_devices
--;
2140 /* remove sysfs entry */
2141 btrfs_sysfs_remove_device(device
);
2144 num_devices
= btrfs_super_num_devices(fs_info
->super_copy
) - 1;
2145 btrfs_set_super_num_devices(fs_info
->super_copy
, num_devices
);
2146 mutex_unlock(&fs_devices
->device_list_mutex
);
2149 * at this point, the device is zero sized and detached from
2150 * the devices list. All that's left is to zero out the old
2151 * supers and free the device.
2153 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
2154 btrfs_scratch_superblocks(fs_info
, device
->bdev
,
2157 btrfs_close_bdev(device
);
2159 btrfs_free_device(device
);
2161 if (cur_devices
->open_devices
== 0) {
2162 list_del_init(&cur_devices
->seed_list
);
2163 close_fs_devices(cur_devices
);
2164 free_fs_devices(cur_devices
);
2168 mutex_unlock(&uuid_mutex
);
2172 btrfs_reada_undo_remove_dev(device
);
2173 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
2174 mutex_lock(&fs_info
->chunk_mutex
);
2175 list_add(&device
->dev_alloc_list
,
2176 &fs_devices
->alloc_list
);
2177 device
->fs_devices
->rw_devices
++;
2178 mutex_unlock(&fs_info
->chunk_mutex
);
2183 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device
*srcdev
)
2185 struct btrfs_fs_devices
*fs_devices
;
2187 lockdep_assert_held(&srcdev
->fs_info
->fs_devices
->device_list_mutex
);
2190 * in case of fs with no seed, srcdev->fs_devices will point
2191 * to fs_devices of fs_info. However when the dev being replaced is
2192 * a seed dev it will point to the seed's local fs_devices. In short
2193 * srcdev will have its correct fs_devices in both the cases.
2195 fs_devices
= srcdev
->fs_devices
;
2197 list_del_rcu(&srcdev
->dev_list
);
2198 list_del(&srcdev
->dev_alloc_list
);
2199 fs_devices
->num_devices
--;
2200 if (test_bit(BTRFS_DEV_STATE_MISSING
, &srcdev
->dev_state
))
2201 fs_devices
->missing_devices
--;
2203 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &srcdev
->dev_state
))
2204 fs_devices
->rw_devices
--;
2207 fs_devices
->open_devices
--;
2210 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device
*srcdev
)
2212 struct btrfs_fs_devices
*fs_devices
= srcdev
->fs_devices
;
2214 mutex_lock(&uuid_mutex
);
2216 btrfs_close_bdev(srcdev
);
2218 btrfs_free_device(srcdev
);
2220 /* if this is no devs we rather delete the fs_devices */
2221 if (!fs_devices
->num_devices
) {
2223 * On a mounted FS, num_devices can't be zero unless it's a
2224 * seed. In case of a seed device being replaced, the replace
2225 * target added to the sprout FS, so there will be no more
2226 * device left under the seed FS.
2228 ASSERT(fs_devices
->seeding
);
2230 list_del_init(&fs_devices
->seed_list
);
2231 close_fs_devices(fs_devices
);
2232 free_fs_devices(fs_devices
);
2234 mutex_unlock(&uuid_mutex
);
2237 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device
*tgtdev
)
2239 struct btrfs_fs_devices
*fs_devices
= tgtdev
->fs_info
->fs_devices
;
2241 mutex_lock(&fs_devices
->device_list_mutex
);
2243 btrfs_sysfs_remove_device(tgtdev
);
2246 fs_devices
->open_devices
--;
2248 fs_devices
->num_devices
--;
2250 btrfs_assign_next_active_device(tgtdev
, NULL
);
2252 list_del_rcu(&tgtdev
->dev_list
);
2254 mutex_unlock(&fs_devices
->device_list_mutex
);
2257 * The update_dev_time() with in btrfs_scratch_superblocks()
2258 * may lead to a call to btrfs_show_devname() which will try
2259 * to hold device_list_mutex. And here this device
2260 * is already out of device list, so we don't have to hold
2261 * the device_list_mutex lock.
2263 btrfs_scratch_superblocks(tgtdev
->fs_info
, tgtdev
->bdev
,
2266 btrfs_close_bdev(tgtdev
);
2268 btrfs_free_device(tgtdev
);
2271 static struct btrfs_device
*btrfs_find_device_by_path(
2272 struct btrfs_fs_info
*fs_info
, const char *device_path
)
2275 struct btrfs_super_block
*disk_super
;
2278 struct block_device
*bdev
;
2279 struct btrfs_device
*device
;
2281 ret
= btrfs_get_bdev_and_sb(device_path
, FMODE_READ
,
2282 fs_info
->bdev_holder
, 0, &bdev
, &disk_super
);
2284 return ERR_PTR(ret
);
2286 devid
= btrfs_stack_device_id(&disk_super
->dev_item
);
2287 dev_uuid
= disk_super
->dev_item
.uuid
;
2288 if (btrfs_fs_incompat(fs_info
, METADATA_UUID
))
2289 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
2290 disk_super
->metadata_uuid
, true);
2292 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
2293 disk_super
->fsid
, true);
2295 btrfs_release_disk_super(disk_super
);
2297 device
= ERR_PTR(-ENOENT
);
2298 blkdev_put(bdev
, FMODE_READ
);
2303 * Lookup a device given by device id, or the path if the id is 0.
2305 struct btrfs_device
*btrfs_find_device_by_devspec(
2306 struct btrfs_fs_info
*fs_info
, u64 devid
,
2307 const char *device_path
)
2309 struct btrfs_device
*device
;
2312 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, NULL
,
2315 return ERR_PTR(-ENOENT
);
2319 if (!device_path
|| !device_path
[0])
2320 return ERR_PTR(-EINVAL
);
2322 if (strcmp(device_path
, "missing") == 0) {
2323 /* Find first missing device */
2324 list_for_each_entry(device
, &fs_info
->fs_devices
->devices
,
2326 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
2327 &device
->dev_state
) && !device
->bdev
)
2330 return ERR_PTR(-ENOENT
);
2333 return btrfs_find_device_by_path(fs_info
, device_path
);
2337 * does all the dirty work required for changing file system's UUID.
2339 static int btrfs_prepare_sprout(struct btrfs_fs_info
*fs_info
)
2341 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2342 struct btrfs_fs_devices
*old_devices
;
2343 struct btrfs_fs_devices
*seed_devices
;
2344 struct btrfs_super_block
*disk_super
= fs_info
->super_copy
;
2345 struct btrfs_device
*device
;
2348 lockdep_assert_held(&uuid_mutex
);
2349 if (!fs_devices
->seeding
)
2353 * Private copy of the seed devices, anchored at
2354 * fs_info->fs_devices->seed_list
2356 seed_devices
= alloc_fs_devices(NULL
, NULL
);
2357 if (IS_ERR(seed_devices
))
2358 return PTR_ERR(seed_devices
);
2361 * It's necessary to retain a copy of the original seed fs_devices in
2362 * fs_uuids so that filesystems which have been seeded can successfully
2363 * reference the seed device from open_seed_devices. This also supports
2366 old_devices
= clone_fs_devices(fs_devices
);
2367 if (IS_ERR(old_devices
)) {
2368 kfree(seed_devices
);
2369 return PTR_ERR(old_devices
);
2372 list_add(&old_devices
->fs_list
, &fs_uuids
);
2374 memcpy(seed_devices
, fs_devices
, sizeof(*seed_devices
));
2375 seed_devices
->opened
= 1;
2376 INIT_LIST_HEAD(&seed_devices
->devices
);
2377 INIT_LIST_HEAD(&seed_devices
->alloc_list
);
2378 mutex_init(&seed_devices
->device_list_mutex
);
2380 mutex_lock(&fs_devices
->device_list_mutex
);
2381 list_splice_init_rcu(&fs_devices
->devices
, &seed_devices
->devices
,
2383 list_for_each_entry(device
, &seed_devices
->devices
, dev_list
)
2384 device
->fs_devices
= seed_devices
;
2386 fs_devices
->seeding
= false;
2387 fs_devices
->num_devices
= 0;
2388 fs_devices
->open_devices
= 0;
2389 fs_devices
->missing_devices
= 0;
2390 fs_devices
->rotating
= false;
2391 list_add(&seed_devices
->seed_list
, &fs_devices
->seed_list
);
2393 generate_random_uuid(fs_devices
->fsid
);
2394 memcpy(fs_devices
->metadata_uuid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2395 memcpy(disk_super
->fsid
, fs_devices
->fsid
, BTRFS_FSID_SIZE
);
2396 mutex_unlock(&fs_devices
->device_list_mutex
);
2398 super_flags
= btrfs_super_flags(disk_super
) &
2399 ~BTRFS_SUPER_FLAG_SEEDING
;
2400 btrfs_set_super_flags(disk_super
, super_flags
);
2406 * Store the expected generation for seed devices in device items.
2408 static int btrfs_finish_sprout(struct btrfs_trans_handle
*trans
)
2410 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2411 struct btrfs_root
*root
= fs_info
->chunk_root
;
2412 struct btrfs_path
*path
;
2413 struct extent_buffer
*leaf
;
2414 struct btrfs_dev_item
*dev_item
;
2415 struct btrfs_device
*device
;
2416 struct btrfs_key key
;
2417 u8 fs_uuid
[BTRFS_FSID_SIZE
];
2418 u8 dev_uuid
[BTRFS_UUID_SIZE
];
2422 path
= btrfs_alloc_path();
2426 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2428 key
.type
= BTRFS_DEV_ITEM_KEY
;
2431 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2435 leaf
= path
->nodes
[0];
2437 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
2438 ret
= btrfs_next_leaf(root
, path
);
2443 leaf
= path
->nodes
[0];
2444 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2445 btrfs_release_path(path
);
2449 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2450 if (key
.objectid
!= BTRFS_DEV_ITEMS_OBJECTID
||
2451 key
.type
!= BTRFS_DEV_ITEM_KEY
)
2454 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2455 struct btrfs_dev_item
);
2456 devid
= btrfs_device_id(leaf
, dev_item
);
2457 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
2459 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
2461 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
2463 BUG_ON(!device
); /* Logic error */
2465 if (device
->fs_devices
->seeding
) {
2466 btrfs_set_device_generation(leaf
, dev_item
,
2467 device
->generation
);
2468 btrfs_mark_buffer_dirty(leaf
);
2476 btrfs_free_path(path
);
2480 int btrfs_init_new_device(struct btrfs_fs_info
*fs_info
, const char *device_path
)
2482 struct btrfs_root
*root
= fs_info
->dev_root
;
2483 struct request_queue
*q
;
2484 struct btrfs_trans_handle
*trans
;
2485 struct btrfs_device
*device
;
2486 struct block_device
*bdev
;
2487 struct super_block
*sb
= fs_info
->sb
;
2488 struct rcu_string
*name
;
2489 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2490 u64 orig_super_total_bytes
;
2491 u64 orig_super_num_devices
;
2492 int seeding_dev
= 0;
2494 bool locked
= false;
2496 if (sb_rdonly(sb
) && !fs_devices
->seeding
)
2499 bdev
= blkdev_get_by_path(device_path
, FMODE_WRITE
| FMODE_EXCL
,
2500 fs_info
->bdev_holder
);
2502 return PTR_ERR(bdev
);
2504 if (fs_devices
->seeding
) {
2506 down_write(&sb
->s_umount
);
2507 mutex_lock(&uuid_mutex
);
2511 sync_blockdev(bdev
);
2514 list_for_each_entry_rcu(device
, &fs_devices
->devices
, dev_list
) {
2515 if (device
->bdev
== bdev
) {
2523 device
= btrfs_alloc_device(fs_info
, NULL
, NULL
);
2524 if (IS_ERR(device
)) {
2525 /* we can safely leave the fs_devices entry around */
2526 ret
= PTR_ERR(device
);
2530 name
= rcu_string_strdup(device_path
, GFP_KERNEL
);
2533 goto error_free_device
;
2535 rcu_assign_pointer(device
->name
, name
);
2537 trans
= btrfs_start_transaction(root
, 0);
2538 if (IS_ERR(trans
)) {
2539 ret
= PTR_ERR(trans
);
2540 goto error_free_device
;
2543 q
= bdev_get_queue(bdev
);
2544 set_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
);
2545 device
->generation
= trans
->transid
;
2546 device
->io_width
= fs_info
->sectorsize
;
2547 device
->io_align
= fs_info
->sectorsize
;
2548 device
->sector_size
= fs_info
->sectorsize
;
2549 device
->total_bytes
= round_down(i_size_read(bdev
->bd_inode
),
2550 fs_info
->sectorsize
);
2551 device
->disk_total_bytes
= device
->total_bytes
;
2552 device
->commit_total_bytes
= device
->total_bytes
;
2553 device
->fs_info
= fs_info
;
2554 device
->bdev
= bdev
;
2555 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
2556 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
2557 device
->mode
= FMODE_EXCL
;
2558 device
->dev_stats_valid
= 1;
2559 set_blocksize(device
->bdev
, BTRFS_BDEV_BLOCKSIZE
);
2562 sb
->s_flags
&= ~SB_RDONLY
;
2563 ret
= btrfs_prepare_sprout(fs_info
);
2565 btrfs_abort_transaction(trans
, ret
);
2570 device
->fs_devices
= fs_devices
;
2572 mutex_lock(&fs_devices
->device_list_mutex
);
2573 mutex_lock(&fs_info
->chunk_mutex
);
2574 list_add_rcu(&device
->dev_list
, &fs_devices
->devices
);
2575 list_add(&device
->dev_alloc_list
, &fs_devices
->alloc_list
);
2576 fs_devices
->num_devices
++;
2577 fs_devices
->open_devices
++;
2578 fs_devices
->rw_devices
++;
2579 fs_devices
->total_devices
++;
2580 fs_devices
->total_rw_bytes
+= device
->total_bytes
;
2582 atomic64_add(device
->total_bytes
, &fs_info
->free_chunk_space
);
2584 if (!blk_queue_nonrot(q
))
2585 fs_devices
->rotating
= true;
2587 orig_super_total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
2588 btrfs_set_super_total_bytes(fs_info
->super_copy
,
2589 round_down(orig_super_total_bytes
+ device
->total_bytes
,
2590 fs_info
->sectorsize
));
2592 orig_super_num_devices
= btrfs_super_num_devices(fs_info
->super_copy
);
2593 btrfs_set_super_num_devices(fs_info
->super_copy
,
2594 orig_super_num_devices
+ 1);
2597 * we've got more storage, clear any full flags on the space
2600 btrfs_clear_space_info_full(fs_info
);
2602 mutex_unlock(&fs_info
->chunk_mutex
);
2604 /* Add sysfs device entry */
2605 btrfs_sysfs_add_device(device
);
2607 mutex_unlock(&fs_devices
->device_list_mutex
);
2610 mutex_lock(&fs_info
->chunk_mutex
);
2611 ret
= init_first_rw_device(trans
);
2612 mutex_unlock(&fs_info
->chunk_mutex
);
2614 btrfs_abort_transaction(trans
, ret
);
2619 ret
= btrfs_add_dev_item(trans
, device
);
2621 btrfs_abort_transaction(trans
, ret
);
2626 ret
= btrfs_finish_sprout(trans
);
2628 btrfs_abort_transaction(trans
, ret
);
2633 * fs_devices now represents the newly sprouted filesystem and
2634 * its fsid has been changed by btrfs_prepare_sprout
2636 btrfs_sysfs_update_sprout_fsid(fs_devices
);
2639 ret
= btrfs_commit_transaction(trans
);
2642 mutex_unlock(&uuid_mutex
);
2643 up_write(&sb
->s_umount
);
2646 if (ret
) /* transaction commit */
2649 ret
= btrfs_relocate_sys_chunks(fs_info
);
2651 btrfs_handle_fs_error(fs_info
, ret
,
2652 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2653 trans
= btrfs_attach_transaction(root
);
2654 if (IS_ERR(trans
)) {
2655 if (PTR_ERR(trans
) == -ENOENT
)
2657 ret
= PTR_ERR(trans
);
2661 ret
= btrfs_commit_transaction(trans
);
2665 * Now that we have written a new super block to this device, check all
2666 * other fs_devices list if device_path alienates any other scanned
2668 * We can ignore the return value as it typically returns -EINVAL and
2669 * only succeeds if the device was an alien.
2671 btrfs_forget_devices(device_path
);
2673 /* Update ctime/mtime for blkid or udev */
2674 update_dev_time(device_path
);
2679 btrfs_sysfs_remove_device(device
);
2680 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
2681 mutex_lock(&fs_info
->chunk_mutex
);
2682 list_del_rcu(&device
->dev_list
);
2683 list_del(&device
->dev_alloc_list
);
2684 fs_info
->fs_devices
->num_devices
--;
2685 fs_info
->fs_devices
->open_devices
--;
2686 fs_info
->fs_devices
->rw_devices
--;
2687 fs_info
->fs_devices
->total_devices
--;
2688 fs_info
->fs_devices
->total_rw_bytes
-= device
->total_bytes
;
2689 atomic64_sub(device
->total_bytes
, &fs_info
->free_chunk_space
);
2690 btrfs_set_super_total_bytes(fs_info
->super_copy
,
2691 orig_super_total_bytes
);
2692 btrfs_set_super_num_devices(fs_info
->super_copy
,
2693 orig_super_num_devices
);
2694 mutex_unlock(&fs_info
->chunk_mutex
);
2695 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
2698 sb
->s_flags
|= SB_RDONLY
;
2700 btrfs_end_transaction(trans
);
2702 btrfs_free_device(device
);
2704 blkdev_put(bdev
, FMODE_EXCL
);
2706 mutex_unlock(&uuid_mutex
);
2707 up_write(&sb
->s_umount
);
2712 static noinline
int btrfs_update_device(struct btrfs_trans_handle
*trans
,
2713 struct btrfs_device
*device
)
2716 struct btrfs_path
*path
;
2717 struct btrfs_root
*root
= device
->fs_info
->chunk_root
;
2718 struct btrfs_dev_item
*dev_item
;
2719 struct extent_buffer
*leaf
;
2720 struct btrfs_key key
;
2722 path
= btrfs_alloc_path();
2726 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
2727 key
.type
= BTRFS_DEV_ITEM_KEY
;
2728 key
.offset
= device
->devid
;
2730 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2739 leaf
= path
->nodes
[0];
2740 dev_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dev_item
);
2742 btrfs_set_device_id(leaf
, dev_item
, device
->devid
);
2743 btrfs_set_device_type(leaf
, dev_item
, device
->type
);
2744 btrfs_set_device_io_align(leaf
, dev_item
, device
->io_align
);
2745 btrfs_set_device_io_width(leaf
, dev_item
, device
->io_width
);
2746 btrfs_set_device_sector_size(leaf
, dev_item
, device
->sector_size
);
2747 btrfs_set_device_total_bytes(leaf
, dev_item
,
2748 btrfs_device_get_disk_total_bytes(device
));
2749 btrfs_set_device_bytes_used(leaf
, dev_item
,
2750 btrfs_device_get_bytes_used(device
));
2751 btrfs_mark_buffer_dirty(leaf
);
2754 btrfs_free_path(path
);
2758 int btrfs_grow_device(struct btrfs_trans_handle
*trans
,
2759 struct btrfs_device
*device
, u64 new_size
)
2761 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
2762 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
2766 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
2769 new_size
= round_down(new_size
, fs_info
->sectorsize
);
2771 mutex_lock(&fs_info
->chunk_mutex
);
2772 old_total
= btrfs_super_total_bytes(super_copy
);
2773 diff
= round_down(new_size
- device
->total_bytes
, fs_info
->sectorsize
);
2775 if (new_size
<= device
->total_bytes
||
2776 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
2777 mutex_unlock(&fs_info
->chunk_mutex
);
2781 btrfs_set_super_total_bytes(super_copy
,
2782 round_down(old_total
+ diff
, fs_info
->sectorsize
));
2783 device
->fs_devices
->total_rw_bytes
+= diff
;
2785 btrfs_device_set_total_bytes(device
, new_size
);
2786 btrfs_device_set_disk_total_bytes(device
, new_size
);
2787 btrfs_clear_space_info_full(device
->fs_info
);
2788 if (list_empty(&device
->post_commit_list
))
2789 list_add_tail(&device
->post_commit_list
,
2790 &trans
->transaction
->dev_update_list
);
2791 mutex_unlock(&fs_info
->chunk_mutex
);
2793 return btrfs_update_device(trans
, device
);
2796 static int btrfs_free_chunk(struct btrfs_trans_handle
*trans
, u64 chunk_offset
)
2798 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2799 struct btrfs_root
*root
= fs_info
->chunk_root
;
2801 struct btrfs_path
*path
;
2802 struct btrfs_key key
;
2804 path
= btrfs_alloc_path();
2808 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2809 key
.offset
= chunk_offset
;
2810 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
2812 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2815 else if (ret
> 0) { /* Logic error or corruption */
2816 btrfs_handle_fs_error(fs_info
, -ENOENT
,
2817 "Failed lookup while freeing chunk.");
2822 ret
= btrfs_del_item(trans
, root
, path
);
2824 btrfs_handle_fs_error(fs_info
, ret
,
2825 "Failed to delete chunk item.");
2827 btrfs_free_path(path
);
2831 static int btrfs_del_sys_chunk(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
2833 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
2834 struct btrfs_disk_key
*disk_key
;
2835 struct btrfs_chunk
*chunk
;
2842 struct btrfs_key key
;
2844 mutex_lock(&fs_info
->chunk_mutex
);
2845 array_size
= btrfs_super_sys_array_size(super_copy
);
2847 ptr
= super_copy
->sys_chunk_array
;
2850 while (cur
< array_size
) {
2851 disk_key
= (struct btrfs_disk_key
*)ptr
;
2852 btrfs_disk_key_to_cpu(&key
, disk_key
);
2854 len
= sizeof(*disk_key
);
2856 if (key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
2857 chunk
= (struct btrfs_chunk
*)(ptr
+ len
);
2858 num_stripes
= btrfs_stack_chunk_num_stripes(chunk
);
2859 len
+= btrfs_chunk_item_size(num_stripes
);
2864 if (key
.objectid
== BTRFS_FIRST_CHUNK_TREE_OBJECTID
&&
2865 key
.offset
== chunk_offset
) {
2866 memmove(ptr
, ptr
+ len
, array_size
- (cur
+ len
));
2868 btrfs_set_super_sys_array_size(super_copy
, array_size
);
2874 mutex_unlock(&fs_info
->chunk_mutex
);
2879 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2880 * @logical: Logical block offset in bytes.
2881 * @length: Length of extent in bytes.
2883 * Return: Chunk mapping or ERR_PTR.
2885 struct extent_map
*btrfs_get_chunk_map(struct btrfs_fs_info
*fs_info
,
2886 u64 logical
, u64 length
)
2888 struct extent_map_tree
*em_tree
;
2889 struct extent_map
*em
;
2891 em_tree
= &fs_info
->mapping_tree
;
2892 read_lock(&em_tree
->lock
);
2893 em
= lookup_extent_mapping(em_tree
, logical
, length
);
2894 read_unlock(&em_tree
->lock
);
2897 btrfs_crit(fs_info
, "unable to find logical %llu length %llu",
2899 return ERR_PTR(-EINVAL
);
2902 if (em
->start
> logical
|| em
->start
+ em
->len
< logical
) {
2904 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2905 logical
, length
, em
->start
, em
->start
+ em
->len
);
2906 free_extent_map(em
);
2907 return ERR_PTR(-EINVAL
);
2910 /* callers are responsible for dropping em's ref. */
2914 int btrfs_remove_chunk(struct btrfs_trans_handle
*trans
, u64 chunk_offset
)
2916 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
2917 struct extent_map
*em
;
2918 struct map_lookup
*map
;
2919 u64 dev_extent_len
= 0;
2921 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
2923 em
= btrfs_get_chunk_map(fs_info
, chunk_offset
, 1);
2926 * This is a logic error, but we don't want to just rely on the
2927 * user having built with ASSERT enabled, so if ASSERT doesn't
2928 * do anything we still error out.
2933 map
= em
->map_lookup
;
2934 mutex_lock(&fs_info
->chunk_mutex
);
2935 check_system_chunk(trans
, map
->type
);
2936 mutex_unlock(&fs_info
->chunk_mutex
);
2939 * Take the device list mutex to prevent races with the final phase of
2940 * a device replace operation that replaces the device object associated
2941 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2943 mutex_lock(&fs_devices
->device_list_mutex
);
2944 for (i
= 0; i
< map
->num_stripes
; i
++) {
2945 struct btrfs_device
*device
= map
->stripes
[i
].dev
;
2946 ret
= btrfs_free_dev_extent(trans
, device
,
2947 map
->stripes
[i
].physical
,
2950 mutex_unlock(&fs_devices
->device_list_mutex
);
2951 btrfs_abort_transaction(trans
, ret
);
2955 if (device
->bytes_used
> 0) {
2956 mutex_lock(&fs_info
->chunk_mutex
);
2957 btrfs_device_set_bytes_used(device
,
2958 device
->bytes_used
- dev_extent_len
);
2959 atomic64_add(dev_extent_len
, &fs_info
->free_chunk_space
);
2960 btrfs_clear_space_info_full(fs_info
);
2961 mutex_unlock(&fs_info
->chunk_mutex
);
2964 ret
= btrfs_update_device(trans
, device
);
2966 mutex_unlock(&fs_devices
->device_list_mutex
);
2967 btrfs_abort_transaction(trans
, ret
);
2971 mutex_unlock(&fs_devices
->device_list_mutex
);
2973 ret
= btrfs_free_chunk(trans
, chunk_offset
);
2975 btrfs_abort_transaction(trans
, ret
);
2979 trace_btrfs_chunk_free(fs_info
, map
, chunk_offset
, em
->len
);
2981 if (map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2982 ret
= btrfs_del_sys_chunk(fs_info
, chunk_offset
);
2984 btrfs_abort_transaction(trans
, ret
);
2989 ret
= btrfs_remove_block_group(trans
, chunk_offset
, em
);
2991 btrfs_abort_transaction(trans
, ret
);
2997 free_extent_map(em
);
3001 static int btrfs_relocate_chunk(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
3003 struct btrfs_root
*root
= fs_info
->chunk_root
;
3004 struct btrfs_trans_handle
*trans
;
3005 struct btrfs_block_group
*block_group
;
3009 * Prevent races with automatic removal of unused block groups.
3010 * After we relocate and before we remove the chunk with offset
3011 * chunk_offset, automatic removal of the block group can kick in,
3012 * resulting in a failure when calling btrfs_remove_chunk() below.
3014 * Make sure to acquire this mutex before doing a tree search (dev
3015 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3016 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3017 * we release the path used to search the chunk/dev tree and before
3018 * the current task acquires this mutex and calls us.
3020 lockdep_assert_held(&fs_info
->delete_unused_bgs_mutex
);
3022 /* step one, relocate all the extents inside this chunk */
3023 btrfs_scrub_pause(fs_info
);
3024 ret
= btrfs_relocate_block_group(fs_info
, chunk_offset
);
3025 btrfs_scrub_continue(fs_info
);
3029 block_group
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3032 btrfs_discard_cancel_work(&fs_info
->discard_ctl
, block_group
);
3033 btrfs_put_block_group(block_group
);
3035 trans
= btrfs_start_trans_remove_block_group(root
->fs_info
,
3037 if (IS_ERR(trans
)) {
3038 ret
= PTR_ERR(trans
);
3039 btrfs_handle_fs_error(root
->fs_info
, ret
, NULL
);
3044 * step two, delete the device extents and the
3045 * chunk tree entries
3047 ret
= btrfs_remove_chunk(trans
, chunk_offset
);
3048 btrfs_end_transaction(trans
);
3052 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info
*fs_info
)
3054 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3055 struct btrfs_path
*path
;
3056 struct extent_buffer
*leaf
;
3057 struct btrfs_chunk
*chunk
;
3058 struct btrfs_key key
;
3059 struct btrfs_key found_key
;
3061 bool retried
= false;
3065 path
= btrfs_alloc_path();
3070 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3071 key
.offset
= (u64
)-1;
3072 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3075 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
3076 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3078 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3081 BUG_ON(ret
== 0); /* Corruption */
3083 ret
= btrfs_previous_item(chunk_root
, path
, key
.objectid
,
3086 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3092 leaf
= path
->nodes
[0];
3093 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3095 chunk
= btrfs_item_ptr(leaf
, path
->slots
[0],
3096 struct btrfs_chunk
);
3097 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3098 btrfs_release_path(path
);
3100 if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
3101 ret
= btrfs_relocate_chunk(fs_info
, found_key
.offset
);
3107 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3109 if (found_key
.offset
== 0)
3111 key
.offset
= found_key
.offset
- 1;
3114 if (failed
&& !retried
) {
3118 } else if (WARN_ON(failed
&& retried
)) {
3122 btrfs_free_path(path
);
3127 * return 1 : allocate a data chunk successfully,
3128 * return <0: errors during allocating a data chunk,
3129 * return 0 : no need to allocate a data chunk.
3131 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info
*fs_info
,
3134 struct btrfs_block_group
*cache
;
3138 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3140 chunk_type
= cache
->flags
;
3141 btrfs_put_block_group(cache
);
3143 if (!(chunk_type
& BTRFS_BLOCK_GROUP_DATA
))
3146 spin_lock(&fs_info
->data_sinfo
->lock
);
3147 bytes_used
= fs_info
->data_sinfo
->bytes_used
;
3148 spin_unlock(&fs_info
->data_sinfo
->lock
);
3151 struct btrfs_trans_handle
*trans
;
3154 trans
= btrfs_join_transaction(fs_info
->tree_root
);
3156 return PTR_ERR(trans
);
3158 ret
= btrfs_force_chunk_alloc(trans
, BTRFS_BLOCK_GROUP_DATA
);
3159 btrfs_end_transaction(trans
);
3168 static int insert_balance_item(struct btrfs_fs_info
*fs_info
,
3169 struct btrfs_balance_control
*bctl
)
3171 struct btrfs_root
*root
= fs_info
->tree_root
;
3172 struct btrfs_trans_handle
*trans
;
3173 struct btrfs_balance_item
*item
;
3174 struct btrfs_disk_balance_args disk_bargs
;
3175 struct btrfs_path
*path
;
3176 struct extent_buffer
*leaf
;
3177 struct btrfs_key key
;
3180 path
= btrfs_alloc_path();
3184 trans
= btrfs_start_transaction(root
, 0);
3185 if (IS_ERR(trans
)) {
3186 btrfs_free_path(path
);
3187 return PTR_ERR(trans
);
3190 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3191 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3194 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
3199 leaf
= path
->nodes
[0];
3200 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
3202 memzero_extent_buffer(leaf
, (unsigned long)item
, sizeof(*item
));
3204 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->data
);
3205 btrfs_set_balance_data(leaf
, item
, &disk_bargs
);
3206 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->meta
);
3207 btrfs_set_balance_meta(leaf
, item
, &disk_bargs
);
3208 btrfs_cpu_balance_args_to_disk(&disk_bargs
, &bctl
->sys
);
3209 btrfs_set_balance_sys(leaf
, item
, &disk_bargs
);
3211 btrfs_set_balance_flags(leaf
, item
, bctl
->flags
);
3213 btrfs_mark_buffer_dirty(leaf
);
3215 btrfs_free_path(path
);
3216 err
= btrfs_commit_transaction(trans
);
3222 static int del_balance_item(struct btrfs_fs_info
*fs_info
)
3224 struct btrfs_root
*root
= fs_info
->tree_root
;
3225 struct btrfs_trans_handle
*trans
;
3226 struct btrfs_path
*path
;
3227 struct btrfs_key key
;
3230 path
= btrfs_alloc_path();
3234 trans
= btrfs_start_transaction_fallback_global_rsv(root
, 0);
3235 if (IS_ERR(trans
)) {
3236 btrfs_free_path(path
);
3237 return PTR_ERR(trans
);
3240 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
3241 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
3244 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3252 ret
= btrfs_del_item(trans
, root
, path
);
3254 btrfs_free_path(path
);
3255 err
= btrfs_commit_transaction(trans
);
3262 * This is a heuristic used to reduce the number of chunks balanced on
3263 * resume after balance was interrupted.
3265 static void update_balance_args(struct btrfs_balance_control
*bctl
)
3268 * Turn on soft mode for chunk types that were being converted.
3270 if (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3271 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3272 if (bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3273 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3274 if (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3275 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_SOFT
;
3278 * Turn on usage filter if is not already used. The idea is
3279 * that chunks that we have already balanced should be
3280 * reasonably full. Don't do it for chunks that are being
3281 * converted - that will keep us from relocating unconverted
3282 * (albeit full) chunks.
3284 if (!(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3285 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3286 !(bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3287 bctl
->data
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3288 bctl
->data
.usage
= 90;
3290 if (!(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3291 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3292 !(bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3293 bctl
->sys
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3294 bctl
->sys
.usage
= 90;
3296 if (!(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3297 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3298 !(bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
)) {
3299 bctl
->meta
.flags
|= BTRFS_BALANCE_ARGS_USAGE
;
3300 bctl
->meta
.usage
= 90;
3305 * Clear the balance status in fs_info and delete the balance item from disk.
3307 static void reset_balance_state(struct btrfs_fs_info
*fs_info
)
3309 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3312 BUG_ON(!fs_info
->balance_ctl
);
3314 spin_lock(&fs_info
->balance_lock
);
3315 fs_info
->balance_ctl
= NULL
;
3316 spin_unlock(&fs_info
->balance_lock
);
3319 ret
= del_balance_item(fs_info
);
3321 btrfs_handle_fs_error(fs_info
, ret
, NULL
);
3325 * Balance filters. Return 1 if chunk should be filtered out
3326 * (should not be balanced).
3328 static int chunk_profiles_filter(u64 chunk_type
,
3329 struct btrfs_balance_args
*bargs
)
3331 chunk_type
= chunk_to_extended(chunk_type
) &
3332 BTRFS_EXTENDED_PROFILE_MASK
;
3334 if (bargs
->profiles
& chunk_type
)
3340 static int chunk_usage_range_filter(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
,
3341 struct btrfs_balance_args
*bargs
)
3343 struct btrfs_block_group
*cache
;
3345 u64 user_thresh_min
;
3346 u64 user_thresh_max
;
3349 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3350 chunk_used
= cache
->used
;
3352 if (bargs
->usage_min
== 0)
3353 user_thresh_min
= 0;
3355 user_thresh_min
= div_factor_fine(cache
->length
,
3358 if (bargs
->usage_max
== 0)
3359 user_thresh_max
= 1;
3360 else if (bargs
->usage_max
> 100)
3361 user_thresh_max
= cache
->length
;
3363 user_thresh_max
= div_factor_fine(cache
->length
,
3366 if (user_thresh_min
<= chunk_used
&& chunk_used
< user_thresh_max
)
3369 btrfs_put_block_group(cache
);
3373 static int chunk_usage_filter(struct btrfs_fs_info
*fs_info
,
3374 u64 chunk_offset
, struct btrfs_balance_args
*bargs
)
3376 struct btrfs_block_group
*cache
;
3377 u64 chunk_used
, user_thresh
;
3380 cache
= btrfs_lookup_block_group(fs_info
, chunk_offset
);
3381 chunk_used
= cache
->used
;
3383 if (bargs
->usage_min
== 0)
3385 else if (bargs
->usage
> 100)
3386 user_thresh
= cache
->length
;
3388 user_thresh
= div_factor_fine(cache
->length
, bargs
->usage
);
3390 if (chunk_used
< user_thresh
)
3393 btrfs_put_block_group(cache
);
3397 static int chunk_devid_filter(struct extent_buffer
*leaf
,
3398 struct btrfs_chunk
*chunk
,
3399 struct btrfs_balance_args
*bargs
)
3401 struct btrfs_stripe
*stripe
;
3402 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3405 for (i
= 0; i
< num_stripes
; i
++) {
3406 stripe
= btrfs_stripe_nr(chunk
, i
);
3407 if (btrfs_stripe_devid(leaf
, stripe
) == bargs
->devid
)
3414 static u64
calc_data_stripes(u64 type
, int num_stripes
)
3416 const int index
= btrfs_bg_flags_to_raid_index(type
);
3417 const int ncopies
= btrfs_raid_array
[index
].ncopies
;
3418 const int nparity
= btrfs_raid_array
[index
].nparity
;
3421 return num_stripes
- nparity
;
3423 return num_stripes
/ ncopies
;
3426 /* [pstart, pend) */
3427 static int chunk_drange_filter(struct extent_buffer
*leaf
,
3428 struct btrfs_chunk
*chunk
,
3429 struct btrfs_balance_args
*bargs
)
3431 struct btrfs_stripe
*stripe
;
3432 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3439 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
))
3442 type
= btrfs_chunk_type(leaf
, chunk
);
3443 factor
= calc_data_stripes(type
, num_stripes
);
3445 for (i
= 0; i
< num_stripes
; i
++) {
3446 stripe
= btrfs_stripe_nr(chunk
, i
);
3447 if (btrfs_stripe_devid(leaf
, stripe
) != bargs
->devid
)
3450 stripe_offset
= btrfs_stripe_offset(leaf
, stripe
);
3451 stripe_length
= btrfs_chunk_length(leaf
, chunk
);
3452 stripe_length
= div_u64(stripe_length
, factor
);
3454 if (stripe_offset
< bargs
->pend
&&
3455 stripe_offset
+ stripe_length
> bargs
->pstart
)
3462 /* [vstart, vend) */
3463 static int chunk_vrange_filter(struct extent_buffer
*leaf
,
3464 struct btrfs_chunk
*chunk
,
3466 struct btrfs_balance_args
*bargs
)
3468 if (chunk_offset
< bargs
->vend
&&
3469 chunk_offset
+ btrfs_chunk_length(leaf
, chunk
) > bargs
->vstart
)
3470 /* at least part of the chunk is inside this vrange */
3476 static int chunk_stripes_range_filter(struct extent_buffer
*leaf
,
3477 struct btrfs_chunk
*chunk
,
3478 struct btrfs_balance_args
*bargs
)
3480 int num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
3482 if (bargs
->stripes_min
<= num_stripes
3483 && num_stripes
<= bargs
->stripes_max
)
3489 static int chunk_soft_convert_filter(u64 chunk_type
,
3490 struct btrfs_balance_args
*bargs
)
3492 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
3495 chunk_type
= chunk_to_extended(chunk_type
) &
3496 BTRFS_EXTENDED_PROFILE_MASK
;
3498 if (bargs
->target
== chunk_type
)
3504 static int should_balance_chunk(struct extent_buffer
*leaf
,
3505 struct btrfs_chunk
*chunk
, u64 chunk_offset
)
3507 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
3508 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3509 struct btrfs_balance_args
*bargs
= NULL
;
3510 u64 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3513 if (!((chunk_type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) &
3514 (bctl
->flags
& BTRFS_BALANCE_TYPE_MASK
))) {
3518 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3519 bargs
= &bctl
->data
;
3520 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3522 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3523 bargs
= &bctl
->meta
;
3525 /* profiles filter */
3526 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_PROFILES
) &&
3527 chunk_profiles_filter(chunk_type
, bargs
)) {
3532 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE
) &&
3533 chunk_usage_filter(fs_info
, chunk_offset
, bargs
)) {
3535 } else if ((bargs
->flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
) &&
3536 chunk_usage_range_filter(fs_info
, chunk_offset
, bargs
)) {
3541 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DEVID
) &&
3542 chunk_devid_filter(leaf
, chunk
, bargs
)) {
3546 /* drange filter, makes sense only with devid filter */
3547 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_DRANGE
) &&
3548 chunk_drange_filter(leaf
, chunk
, bargs
)) {
3553 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_VRANGE
) &&
3554 chunk_vrange_filter(leaf
, chunk
, chunk_offset
, bargs
)) {
3558 /* stripes filter */
3559 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_STRIPES_RANGE
) &&
3560 chunk_stripes_range_filter(leaf
, chunk
, bargs
)) {
3564 /* soft profile changing mode */
3565 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_SOFT
) &&
3566 chunk_soft_convert_filter(chunk_type
, bargs
)) {
3571 * limited by count, must be the last filter
3573 if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT
)) {
3574 if (bargs
->limit
== 0)
3578 } else if ((bargs
->flags
& BTRFS_BALANCE_ARGS_LIMIT_RANGE
)) {
3580 * Same logic as the 'limit' filter; the minimum cannot be
3581 * determined here because we do not have the global information
3582 * about the count of all chunks that satisfy the filters.
3584 if (bargs
->limit_max
== 0)
3593 static int __btrfs_balance(struct btrfs_fs_info
*fs_info
)
3595 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3596 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
3598 struct btrfs_chunk
*chunk
;
3599 struct btrfs_path
*path
= NULL
;
3600 struct btrfs_key key
;
3601 struct btrfs_key found_key
;
3602 struct extent_buffer
*leaf
;
3605 int enospc_errors
= 0;
3606 bool counting
= true;
3607 /* The single value limit and min/max limits use the same bytes in the */
3608 u64 limit_data
= bctl
->data
.limit
;
3609 u64 limit_meta
= bctl
->meta
.limit
;
3610 u64 limit_sys
= bctl
->sys
.limit
;
3614 int chunk_reserved
= 0;
3616 path
= btrfs_alloc_path();
3622 /* zero out stat counters */
3623 spin_lock(&fs_info
->balance_lock
);
3624 memset(&bctl
->stat
, 0, sizeof(bctl
->stat
));
3625 spin_unlock(&fs_info
->balance_lock
);
3629 * The single value limit and min/max limits use the same bytes
3632 bctl
->data
.limit
= limit_data
;
3633 bctl
->meta
.limit
= limit_meta
;
3634 bctl
->sys
.limit
= limit_sys
;
3636 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
3637 key
.offset
= (u64
)-1;
3638 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
3641 if ((!counting
&& atomic_read(&fs_info
->balance_pause_req
)) ||
3642 atomic_read(&fs_info
->balance_cancel_req
)) {
3647 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
3648 ret
= btrfs_search_slot(NULL
, chunk_root
, &key
, path
, 0, 0);
3650 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3655 * this shouldn't happen, it means the last relocate
3659 BUG(); /* FIXME break ? */
3661 ret
= btrfs_previous_item(chunk_root
, path
, 0,
3662 BTRFS_CHUNK_ITEM_KEY
);
3664 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3669 leaf
= path
->nodes
[0];
3670 slot
= path
->slots
[0];
3671 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3673 if (found_key
.objectid
!= key
.objectid
) {
3674 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3678 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
3679 chunk_type
= btrfs_chunk_type(leaf
, chunk
);
3682 spin_lock(&fs_info
->balance_lock
);
3683 bctl
->stat
.considered
++;
3684 spin_unlock(&fs_info
->balance_lock
);
3687 ret
= should_balance_chunk(leaf
, chunk
, found_key
.offset
);
3689 btrfs_release_path(path
);
3691 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3696 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3697 spin_lock(&fs_info
->balance_lock
);
3698 bctl
->stat
.expected
++;
3699 spin_unlock(&fs_info
->balance_lock
);
3701 if (chunk_type
& BTRFS_BLOCK_GROUP_DATA
)
3703 else if (chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
)
3705 else if (chunk_type
& BTRFS_BLOCK_GROUP_METADATA
)
3712 * Apply limit_min filter, no need to check if the LIMITS
3713 * filter is used, limit_min is 0 by default
3715 if (((chunk_type
& BTRFS_BLOCK_GROUP_DATA
) &&
3716 count_data
< bctl
->data
.limit_min
)
3717 || ((chunk_type
& BTRFS_BLOCK_GROUP_METADATA
) &&
3718 count_meta
< bctl
->meta
.limit_min
)
3719 || ((chunk_type
& BTRFS_BLOCK_GROUP_SYSTEM
) &&
3720 count_sys
< bctl
->sys
.limit_min
)) {
3721 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3725 if (!chunk_reserved
) {
3727 * We may be relocating the only data chunk we have,
3728 * which could potentially end up with losing data's
3729 * raid profile, so lets allocate an empty one in
3732 ret
= btrfs_may_alloc_data_chunk(fs_info
,
3735 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3737 } else if (ret
== 1) {
3742 ret
= btrfs_relocate_chunk(fs_info
, found_key
.offset
);
3743 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
3744 if (ret
== -ENOSPC
) {
3746 } else if (ret
== -ETXTBSY
) {
3748 "skipping relocation of block group %llu due to active swapfile",
3754 spin_lock(&fs_info
->balance_lock
);
3755 bctl
->stat
.completed
++;
3756 spin_unlock(&fs_info
->balance_lock
);
3759 if (found_key
.offset
== 0)
3761 key
.offset
= found_key
.offset
- 1;
3765 btrfs_release_path(path
);
3770 btrfs_free_path(path
);
3771 if (enospc_errors
) {
3772 btrfs_info(fs_info
, "%d enospc errors during balance",
3782 * alloc_profile_is_valid - see if a given profile is valid and reduced
3783 * @flags: profile to validate
3784 * @extended: if true @flags is treated as an extended profile
3786 static int alloc_profile_is_valid(u64 flags
, int extended
)
3788 u64 mask
= (extended
? BTRFS_EXTENDED_PROFILE_MASK
:
3789 BTRFS_BLOCK_GROUP_PROFILE_MASK
);
3791 flags
&= ~BTRFS_BLOCK_GROUP_TYPE_MASK
;
3793 /* 1) check that all other bits are zeroed */
3797 /* 2) see if profile is reduced */
3799 return !extended
; /* "0" is valid for usual profiles */
3801 return has_single_bit_set(flags
);
3804 static inline int balance_need_close(struct btrfs_fs_info
*fs_info
)
3806 /* cancel requested || normal exit path */
3807 return atomic_read(&fs_info
->balance_cancel_req
) ||
3808 (atomic_read(&fs_info
->balance_pause_req
) == 0 &&
3809 atomic_read(&fs_info
->balance_cancel_req
) == 0);
3813 * Validate target profile against allowed profiles and return true if it's OK.
3814 * Otherwise print the error message and return false.
3816 static inline int validate_convert_profile(struct btrfs_fs_info
*fs_info
,
3817 const struct btrfs_balance_args
*bargs
,
3818 u64 allowed
, const char *type
)
3820 if (!(bargs
->flags
& BTRFS_BALANCE_ARGS_CONVERT
))
3823 /* Profile is valid and does not have bits outside of the allowed set */
3824 if (alloc_profile_is_valid(bargs
->target
, 1) &&
3825 (bargs
->target
& ~allowed
) == 0)
3828 btrfs_err(fs_info
, "balance: invalid convert %s profile %s",
3829 type
, btrfs_bg_type_to_raid_name(bargs
->target
));
3834 * Fill @buf with textual description of balance filter flags @bargs, up to
3835 * @size_buf including the terminating null. The output may be trimmed if it
3836 * does not fit into the provided buffer.
3838 static void describe_balance_args(struct btrfs_balance_args
*bargs
, char *buf
,
3842 u32 size_bp
= size_buf
;
3844 u64 flags
= bargs
->flags
;
3845 char tmp_buf
[128] = {'\0'};
3850 #define CHECK_APPEND_NOARG(a) \
3852 ret = snprintf(bp, size_bp, (a)); \
3853 if (ret < 0 || ret >= size_bp) \
3854 goto out_overflow; \
3859 #define CHECK_APPEND_1ARG(a, v1) \
3861 ret = snprintf(bp, size_bp, (a), (v1)); \
3862 if (ret < 0 || ret >= size_bp) \
3863 goto out_overflow; \
3868 #define CHECK_APPEND_2ARG(a, v1, v2) \
3870 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3871 if (ret < 0 || ret >= size_bp) \
3872 goto out_overflow; \
3877 if (flags
& BTRFS_BALANCE_ARGS_CONVERT
)
3878 CHECK_APPEND_1ARG("convert=%s,",
3879 btrfs_bg_type_to_raid_name(bargs
->target
));
3881 if (flags
& BTRFS_BALANCE_ARGS_SOFT
)
3882 CHECK_APPEND_NOARG("soft,");
3884 if (flags
& BTRFS_BALANCE_ARGS_PROFILES
) {
3885 btrfs_describe_block_groups(bargs
->profiles
, tmp_buf
,
3887 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf
);
3890 if (flags
& BTRFS_BALANCE_ARGS_USAGE
)
3891 CHECK_APPEND_1ARG("usage=%llu,", bargs
->usage
);
3893 if (flags
& BTRFS_BALANCE_ARGS_USAGE_RANGE
)
3894 CHECK_APPEND_2ARG("usage=%u..%u,",
3895 bargs
->usage_min
, bargs
->usage_max
);
3897 if (flags
& BTRFS_BALANCE_ARGS_DEVID
)
3898 CHECK_APPEND_1ARG("devid=%llu,", bargs
->devid
);
3900 if (flags
& BTRFS_BALANCE_ARGS_DRANGE
)
3901 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3902 bargs
->pstart
, bargs
->pend
);
3904 if (flags
& BTRFS_BALANCE_ARGS_VRANGE
)
3905 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3906 bargs
->vstart
, bargs
->vend
);
3908 if (flags
& BTRFS_BALANCE_ARGS_LIMIT
)
3909 CHECK_APPEND_1ARG("limit=%llu,", bargs
->limit
);
3911 if (flags
& BTRFS_BALANCE_ARGS_LIMIT_RANGE
)
3912 CHECK_APPEND_2ARG("limit=%u..%u,",
3913 bargs
->limit_min
, bargs
->limit_max
);
3915 if (flags
& BTRFS_BALANCE_ARGS_STRIPES_RANGE
)
3916 CHECK_APPEND_2ARG("stripes=%u..%u,",
3917 bargs
->stripes_min
, bargs
->stripes_max
);
3919 #undef CHECK_APPEND_2ARG
3920 #undef CHECK_APPEND_1ARG
3921 #undef CHECK_APPEND_NOARG
3925 if (size_bp
< size_buf
)
3926 buf
[size_buf
- size_bp
- 1] = '\0'; /* remove last , */
3931 static void describe_balance_start_or_resume(struct btrfs_fs_info
*fs_info
)
3933 u32 size_buf
= 1024;
3934 char tmp_buf
[192] = {'\0'};
3937 u32 size_bp
= size_buf
;
3939 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3941 buf
= kzalloc(size_buf
, GFP_KERNEL
);
3947 #define CHECK_APPEND_1ARG(a, v1) \
3949 ret = snprintf(bp, size_bp, (a), (v1)); \
3950 if (ret < 0 || ret >= size_bp) \
3951 goto out_overflow; \
3956 if (bctl
->flags
& BTRFS_BALANCE_FORCE
)
3957 CHECK_APPEND_1ARG("%s", "-f ");
3959 if (bctl
->flags
& BTRFS_BALANCE_DATA
) {
3960 describe_balance_args(&bctl
->data
, tmp_buf
, sizeof(tmp_buf
));
3961 CHECK_APPEND_1ARG("-d%s ", tmp_buf
);
3964 if (bctl
->flags
& BTRFS_BALANCE_METADATA
) {
3965 describe_balance_args(&bctl
->meta
, tmp_buf
, sizeof(tmp_buf
));
3966 CHECK_APPEND_1ARG("-m%s ", tmp_buf
);
3969 if (bctl
->flags
& BTRFS_BALANCE_SYSTEM
) {
3970 describe_balance_args(&bctl
->sys
, tmp_buf
, sizeof(tmp_buf
));
3971 CHECK_APPEND_1ARG("-s%s ", tmp_buf
);
3974 #undef CHECK_APPEND_1ARG
3978 if (size_bp
< size_buf
)
3979 buf
[size_buf
- size_bp
- 1] = '\0'; /* remove last " " */
3980 btrfs_info(fs_info
, "balance: %s %s",
3981 (bctl
->flags
& BTRFS_BALANCE_RESUME
) ?
3982 "resume" : "start", buf
);
3988 * Should be called with balance mutexe held
3990 int btrfs_balance(struct btrfs_fs_info
*fs_info
,
3991 struct btrfs_balance_control
*bctl
,
3992 struct btrfs_ioctl_balance_args
*bargs
)
3994 u64 meta_target
, data_target
;
4000 bool reducing_redundancy
;
4003 if (btrfs_fs_closing(fs_info
) ||
4004 atomic_read(&fs_info
->balance_pause_req
) ||
4005 btrfs_should_cancel_balance(fs_info
)) {
4010 allowed
= btrfs_super_incompat_flags(fs_info
->super_copy
);
4011 if (allowed
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
4015 * In case of mixed groups both data and meta should be picked,
4016 * and identical options should be given for both of them.
4018 allowed
= BTRFS_BALANCE_DATA
| BTRFS_BALANCE_METADATA
;
4019 if (mixed
&& (bctl
->flags
& allowed
)) {
4020 if (!(bctl
->flags
& BTRFS_BALANCE_DATA
) ||
4021 !(bctl
->flags
& BTRFS_BALANCE_METADATA
) ||
4022 memcmp(&bctl
->data
, &bctl
->meta
, sizeof(bctl
->data
))) {
4024 "balance: mixed groups data and metadata options must be the same");
4031 * rw_devices will not change at the moment, device add/delete/replace
4034 num_devices
= fs_info
->fs_devices
->rw_devices
;
4037 * SINGLE profile on-disk has no profile bit, but in-memory we have a
4038 * special bit for it, to make it easier to distinguish. Thus we need
4039 * to set it manually, or balance would refuse the profile.
4041 allowed
= BTRFS_AVAIL_ALLOC_BIT_SINGLE
;
4042 for (i
= 0; i
< ARRAY_SIZE(btrfs_raid_array
); i
++)
4043 if (num_devices
>= btrfs_raid_array
[i
].devs_min
)
4044 allowed
|= btrfs_raid_array
[i
].bg_flag
;
4046 if (!validate_convert_profile(fs_info
, &bctl
->data
, allowed
, "data") ||
4047 !validate_convert_profile(fs_info
, &bctl
->meta
, allowed
, "metadata") ||
4048 !validate_convert_profile(fs_info
, &bctl
->sys
, allowed
, "system")) {
4054 * Allow to reduce metadata or system integrity only if force set for
4055 * profiles with redundancy (copies, parity)
4058 for (i
= 0; i
< ARRAY_SIZE(btrfs_raid_array
); i
++) {
4059 if (btrfs_raid_array
[i
].ncopies
>= 2 ||
4060 btrfs_raid_array
[i
].tolerated_failures
>= 1)
4061 allowed
|= btrfs_raid_array
[i
].bg_flag
;
4064 seq
= read_seqbegin(&fs_info
->profiles_lock
);
4066 if (((bctl
->sys
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
4067 (fs_info
->avail_system_alloc_bits
& allowed
) &&
4068 !(bctl
->sys
.target
& allowed
)) ||
4069 ((bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) &&
4070 (fs_info
->avail_metadata_alloc_bits
& allowed
) &&
4071 !(bctl
->meta
.target
& allowed
)))
4072 reducing_redundancy
= true;
4074 reducing_redundancy
= false;
4076 /* if we're not converting, the target field is uninitialized */
4077 meta_target
= (bctl
->meta
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) ?
4078 bctl
->meta
.target
: fs_info
->avail_metadata_alloc_bits
;
4079 data_target
= (bctl
->data
.flags
& BTRFS_BALANCE_ARGS_CONVERT
) ?
4080 bctl
->data
.target
: fs_info
->avail_data_alloc_bits
;
4081 } while (read_seqretry(&fs_info
->profiles_lock
, seq
));
4083 if (reducing_redundancy
) {
4084 if (bctl
->flags
& BTRFS_BALANCE_FORCE
) {
4086 "balance: force reducing metadata redundancy");
4089 "balance: reduces metadata redundancy, use --force if you want this");
4095 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target
) <
4096 btrfs_get_num_tolerated_disk_barrier_failures(data_target
)) {
4098 "balance: metadata profile %s has lower redundancy than data profile %s",
4099 btrfs_bg_type_to_raid_name(meta_target
),
4100 btrfs_bg_type_to_raid_name(data_target
));
4103 if (fs_info
->send_in_progress
) {
4104 btrfs_warn_rl(fs_info
,
4105 "cannot run balance while send operations are in progress (%d in progress)",
4106 fs_info
->send_in_progress
);
4111 ret
= insert_balance_item(fs_info
, bctl
);
4112 if (ret
&& ret
!= -EEXIST
)
4115 if (!(bctl
->flags
& BTRFS_BALANCE_RESUME
)) {
4116 BUG_ON(ret
== -EEXIST
);
4117 BUG_ON(fs_info
->balance_ctl
);
4118 spin_lock(&fs_info
->balance_lock
);
4119 fs_info
->balance_ctl
= bctl
;
4120 spin_unlock(&fs_info
->balance_lock
);
4122 BUG_ON(ret
!= -EEXIST
);
4123 spin_lock(&fs_info
->balance_lock
);
4124 update_balance_args(bctl
);
4125 spin_unlock(&fs_info
->balance_lock
);
4128 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4129 set_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
);
4130 describe_balance_start_or_resume(fs_info
);
4131 mutex_unlock(&fs_info
->balance_mutex
);
4133 ret
= __btrfs_balance(fs_info
);
4135 mutex_lock(&fs_info
->balance_mutex
);
4136 if (ret
== -ECANCELED
&& atomic_read(&fs_info
->balance_pause_req
))
4137 btrfs_info(fs_info
, "balance: paused");
4139 * Balance can be canceled by:
4141 * - Regular cancel request
4142 * Then ret == -ECANCELED and balance_cancel_req > 0
4144 * - Fatal signal to "btrfs" process
4145 * Either the signal caught by wait_reserve_ticket() and callers
4146 * got -EINTR, or caught by btrfs_should_cancel_balance() and
4148 * Either way, in this case balance_cancel_req = 0, and
4149 * ret == -EINTR or ret == -ECANCELED.
4151 * So here we only check the return value to catch canceled balance.
4153 else if (ret
== -ECANCELED
|| ret
== -EINTR
)
4154 btrfs_info(fs_info
, "balance: canceled");
4156 btrfs_info(fs_info
, "balance: ended with status: %d", ret
);
4158 clear_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
);
4161 memset(bargs
, 0, sizeof(*bargs
));
4162 btrfs_update_ioctl_balance_args(fs_info
, bargs
);
4165 if ((ret
&& ret
!= -ECANCELED
&& ret
!= -ENOSPC
) ||
4166 balance_need_close(fs_info
)) {
4167 reset_balance_state(fs_info
);
4168 btrfs_exclop_finish(fs_info
);
4171 wake_up(&fs_info
->balance_wait_q
);
4175 if (bctl
->flags
& BTRFS_BALANCE_RESUME
)
4176 reset_balance_state(fs_info
);
4179 btrfs_exclop_finish(fs_info
);
4184 static int balance_kthread(void *data
)
4186 struct btrfs_fs_info
*fs_info
= data
;
4189 mutex_lock(&fs_info
->balance_mutex
);
4190 if (fs_info
->balance_ctl
)
4191 ret
= btrfs_balance(fs_info
, fs_info
->balance_ctl
, NULL
);
4192 mutex_unlock(&fs_info
->balance_mutex
);
4197 int btrfs_resume_balance_async(struct btrfs_fs_info
*fs_info
)
4199 struct task_struct
*tsk
;
4201 mutex_lock(&fs_info
->balance_mutex
);
4202 if (!fs_info
->balance_ctl
) {
4203 mutex_unlock(&fs_info
->balance_mutex
);
4206 mutex_unlock(&fs_info
->balance_mutex
);
4208 if (btrfs_test_opt(fs_info
, SKIP_BALANCE
)) {
4209 btrfs_info(fs_info
, "balance: resume skipped");
4214 * A ro->rw remount sequence should continue with the paused balance
4215 * regardless of who pauses it, system or the user as of now, so set
4218 spin_lock(&fs_info
->balance_lock
);
4219 fs_info
->balance_ctl
->flags
|= BTRFS_BALANCE_RESUME
;
4220 spin_unlock(&fs_info
->balance_lock
);
4222 tsk
= kthread_run(balance_kthread
, fs_info
, "btrfs-balance");
4223 return PTR_ERR_OR_ZERO(tsk
);
4226 int btrfs_recover_balance(struct btrfs_fs_info
*fs_info
)
4228 struct btrfs_balance_control
*bctl
;
4229 struct btrfs_balance_item
*item
;
4230 struct btrfs_disk_balance_args disk_bargs
;
4231 struct btrfs_path
*path
;
4232 struct extent_buffer
*leaf
;
4233 struct btrfs_key key
;
4236 path
= btrfs_alloc_path();
4240 key
.objectid
= BTRFS_BALANCE_OBJECTID
;
4241 key
.type
= BTRFS_TEMPORARY_ITEM_KEY
;
4244 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
4247 if (ret
> 0) { /* ret = -ENOENT; */
4252 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
4258 leaf
= path
->nodes
[0];
4259 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_balance_item
);
4261 bctl
->flags
= btrfs_balance_flags(leaf
, item
);
4262 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
4264 btrfs_balance_data(leaf
, item
, &disk_bargs
);
4265 btrfs_disk_balance_args_to_cpu(&bctl
->data
, &disk_bargs
);
4266 btrfs_balance_meta(leaf
, item
, &disk_bargs
);
4267 btrfs_disk_balance_args_to_cpu(&bctl
->meta
, &disk_bargs
);
4268 btrfs_balance_sys(leaf
, item
, &disk_bargs
);
4269 btrfs_disk_balance_args_to_cpu(&bctl
->sys
, &disk_bargs
);
4272 * This should never happen, as the paused balance state is recovered
4273 * during mount without any chance of other exclusive ops to collide.
4275 * This gives the exclusive op status to balance and keeps in paused
4276 * state until user intervention (cancel or umount). If the ownership
4277 * cannot be assigned, show a message but do not fail. The balance
4278 * is in a paused state and must have fs_info::balance_ctl properly
4281 if (!btrfs_exclop_start(fs_info
, BTRFS_EXCLOP_BALANCE
))
4283 "balance: cannot set exclusive op status, resume manually");
4285 mutex_lock(&fs_info
->balance_mutex
);
4286 BUG_ON(fs_info
->balance_ctl
);
4287 spin_lock(&fs_info
->balance_lock
);
4288 fs_info
->balance_ctl
= bctl
;
4289 spin_unlock(&fs_info
->balance_lock
);
4290 mutex_unlock(&fs_info
->balance_mutex
);
4292 btrfs_free_path(path
);
4296 int btrfs_pause_balance(struct btrfs_fs_info
*fs_info
)
4300 mutex_lock(&fs_info
->balance_mutex
);
4301 if (!fs_info
->balance_ctl
) {
4302 mutex_unlock(&fs_info
->balance_mutex
);
4306 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4307 atomic_inc(&fs_info
->balance_pause_req
);
4308 mutex_unlock(&fs_info
->balance_mutex
);
4310 wait_event(fs_info
->balance_wait_q
,
4311 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4313 mutex_lock(&fs_info
->balance_mutex
);
4314 /* we are good with balance_ctl ripped off from under us */
4315 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4316 atomic_dec(&fs_info
->balance_pause_req
);
4321 mutex_unlock(&fs_info
->balance_mutex
);
4325 int btrfs_cancel_balance(struct btrfs_fs_info
*fs_info
)
4327 mutex_lock(&fs_info
->balance_mutex
);
4328 if (!fs_info
->balance_ctl
) {
4329 mutex_unlock(&fs_info
->balance_mutex
);
4334 * A paused balance with the item stored on disk can be resumed at
4335 * mount time if the mount is read-write. Otherwise it's still paused
4336 * and we must not allow cancelling as it deletes the item.
4338 if (sb_rdonly(fs_info
->sb
)) {
4339 mutex_unlock(&fs_info
->balance_mutex
);
4343 atomic_inc(&fs_info
->balance_cancel_req
);
4345 * if we are running just wait and return, balance item is
4346 * deleted in btrfs_balance in this case
4348 if (test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
)) {
4349 mutex_unlock(&fs_info
->balance_mutex
);
4350 wait_event(fs_info
->balance_wait_q
,
4351 !test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4352 mutex_lock(&fs_info
->balance_mutex
);
4354 mutex_unlock(&fs_info
->balance_mutex
);
4356 * Lock released to allow other waiters to continue, we'll
4357 * reexamine the status again.
4359 mutex_lock(&fs_info
->balance_mutex
);
4361 if (fs_info
->balance_ctl
) {
4362 reset_balance_state(fs_info
);
4363 btrfs_exclop_finish(fs_info
);
4364 btrfs_info(fs_info
, "balance: canceled");
4368 BUG_ON(fs_info
->balance_ctl
||
4369 test_bit(BTRFS_FS_BALANCE_RUNNING
, &fs_info
->flags
));
4370 atomic_dec(&fs_info
->balance_cancel_req
);
4371 mutex_unlock(&fs_info
->balance_mutex
);
4375 int btrfs_uuid_scan_kthread(void *data
)
4377 struct btrfs_fs_info
*fs_info
= data
;
4378 struct btrfs_root
*root
= fs_info
->tree_root
;
4379 struct btrfs_key key
;
4380 struct btrfs_path
*path
= NULL
;
4382 struct extent_buffer
*eb
;
4384 struct btrfs_root_item root_item
;
4386 struct btrfs_trans_handle
*trans
= NULL
;
4387 bool closing
= false;
4389 path
= btrfs_alloc_path();
4396 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4400 if (btrfs_fs_closing(fs_info
)) {
4404 ret
= btrfs_search_forward(root
, &key
, path
,
4405 BTRFS_OLDEST_GENERATION
);
4412 if (key
.type
!= BTRFS_ROOT_ITEM_KEY
||
4413 (key
.objectid
< BTRFS_FIRST_FREE_OBJECTID
&&
4414 key
.objectid
!= BTRFS_FS_TREE_OBJECTID
) ||
4415 key
.objectid
> BTRFS_LAST_FREE_OBJECTID
)
4418 eb
= path
->nodes
[0];
4419 slot
= path
->slots
[0];
4420 item_size
= btrfs_item_size_nr(eb
, slot
);
4421 if (item_size
< sizeof(root_item
))
4424 read_extent_buffer(eb
, &root_item
,
4425 btrfs_item_ptr_offset(eb
, slot
),
4426 (int)sizeof(root_item
));
4427 if (btrfs_root_refs(&root_item
) == 0)
4430 if (!btrfs_is_empty_uuid(root_item
.uuid
) ||
4431 !btrfs_is_empty_uuid(root_item
.received_uuid
)) {
4435 btrfs_release_path(path
);
4437 * 1 - subvol uuid item
4438 * 1 - received_subvol uuid item
4440 trans
= btrfs_start_transaction(fs_info
->uuid_root
, 2);
4441 if (IS_ERR(trans
)) {
4442 ret
= PTR_ERR(trans
);
4450 btrfs_release_path(path
);
4451 if (!btrfs_is_empty_uuid(root_item
.uuid
)) {
4452 ret
= btrfs_uuid_tree_add(trans
, root_item
.uuid
,
4453 BTRFS_UUID_KEY_SUBVOL
,
4456 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
4462 if (!btrfs_is_empty_uuid(root_item
.received_uuid
)) {
4463 ret
= btrfs_uuid_tree_add(trans
,
4464 root_item
.received_uuid
,
4465 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4468 btrfs_warn(fs_info
, "uuid_tree_add failed %d",
4475 btrfs_release_path(path
);
4477 ret
= btrfs_end_transaction(trans
);
4483 if (key
.offset
< (u64
)-1) {
4485 } else if (key
.type
< BTRFS_ROOT_ITEM_KEY
) {
4487 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4488 } else if (key
.objectid
< (u64
)-1) {
4490 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4499 btrfs_free_path(path
);
4500 if (trans
&& !IS_ERR(trans
))
4501 btrfs_end_transaction(trans
);
4503 btrfs_warn(fs_info
, "btrfs_uuid_scan_kthread failed %d", ret
);
4505 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN
, &fs_info
->flags
);
4506 up(&fs_info
->uuid_tree_rescan_sem
);
4510 int btrfs_create_uuid_tree(struct btrfs_fs_info
*fs_info
)
4512 struct btrfs_trans_handle
*trans
;
4513 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
4514 struct btrfs_root
*uuid_root
;
4515 struct task_struct
*task
;
4522 trans
= btrfs_start_transaction(tree_root
, 2);
4524 return PTR_ERR(trans
);
4526 uuid_root
= btrfs_create_tree(trans
, BTRFS_UUID_TREE_OBJECTID
);
4527 if (IS_ERR(uuid_root
)) {
4528 ret
= PTR_ERR(uuid_root
);
4529 btrfs_abort_transaction(trans
, ret
);
4530 btrfs_end_transaction(trans
);
4534 fs_info
->uuid_root
= uuid_root
;
4536 ret
= btrfs_commit_transaction(trans
);
4540 down(&fs_info
->uuid_tree_rescan_sem
);
4541 task
= kthread_run(btrfs_uuid_scan_kthread
, fs_info
, "btrfs-uuid");
4543 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
4544 btrfs_warn(fs_info
, "failed to start uuid_scan task");
4545 up(&fs_info
->uuid_tree_rescan_sem
);
4546 return PTR_ERR(task
);
4553 * shrinking a device means finding all of the device extents past
4554 * the new size, and then following the back refs to the chunks.
4555 * The chunk relocation code actually frees the device extent
4557 int btrfs_shrink_device(struct btrfs_device
*device
, u64 new_size
)
4559 struct btrfs_fs_info
*fs_info
= device
->fs_info
;
4560 struct btrfs_root
*root
= fs_info
->dev_root
;
4561 struct btrfs_trans_handle
*trans
;
4562 struct btrfs_dev_extent
*dev_extent
= NULL
;
4563 struct btrfs_path
*path
;
4569 bool retried
= false;
4570 struct extent_buffer
*l
;
4571 struct btrfs_key key
;
4572 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
4573 u64 old_total
= btrfs_super_total_bytes(super_copy
);
4574 u64 old_size
= btrfs_device_get_total_bytes(device
);
4578 new_size
= round_down(new_size
, fs_info
->sectorsize
);
4580 diff
= round_down(old_size
- new_size
, fs_info
->sectorsize
);
4582 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
4585 path
= btrfs_alloc_path();
4589 path
->reada
= READA_BACK
;
4591 trans
= btrfs_start_transaction(root
, 0);
4592 if (IS_ERR(trans
)) {
4593 btrfs_free_path(path
);
4594 return PTR_ERR(trans
);
4597 mutex_lock(&fs_info
->chunk_mutex
);
4599 btrfs_device_set_total_bytes(device
, new_size
);
4600 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
4601 device
->fs_devices
->total_rw_bytes
-= diff
;
4602 atomic64_sub(diff
, &fs_info
->free_chunk_space
);
4606 * Once the device's size has been set to the new size, ensure all
4607 * in-memory chunks are synced to disk so that the loop below sees them
4608 * and relocates them accordingly.
4610 if (contains_pending_extent(device
, &start
, diff
)) {
4611 mutex_unlock(&fs_info
->chunk_mutex
);
4612 ret
= btrfs_commit_transaction(trans
);
4616 mutex_unlock(&fs_info
->chunk_mutex
);
4617 btrfs_end_transaction(trans
);
4621 key
.objectid
= device
->devid
;
4622 key
.offset
= (u64
)-1;
4623 key
.type
= BTRFS_DEV_EXTENT_KEY
;
4626 mutex_lock(&fs_info
->delete_unused_bgs_mutex
);
4627 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4629 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4633 ret
= btrfs_previous_item(root
, path
, 0, key
.type
);
4635 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4640 btrfs_release_path(path
);
4645 slot
= path
->slots
[0];
4646 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
4648 if (key
.objectid
!= device
->devid
) {
4649 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4650 btrfs_release_path(path
);
4654 dev_extent
= btrfs_item_ptr(l
, slot
, struct btrfs_dev_extent
);
4655 length
= btrfs_dev_extent_length(l
, dev_extent
);
4657 if (key
.offset
+ length
<= new_size
) {
4658 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4659 btrfs_release_path(path
);
4663 chunk_offset
= btrfs_dev_extent_chunk_offset(l
, dev_extent
);
4664 btrfs_release_path(path
);
4667 * We may be relocating the only data chunk we have,
4668 * which could potentially end up with losing data's
4669 * raid profile, so lets allocate an empty one in
4672 ret
= btrfs_may_alloc_data_chunk(fs_info
, chunk_offset
);
4674 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4678 ret
= btrfs_relocate_chunk(fs_info
, chunk_offset
);
4679 mutex_unlock(&fs_info
->delete_unused_bgs_mutex
);
4680 if (ret
== -ENOSPC
) {
4683 if (ret
== -ETXTBSY
) {
4685 "could not shrink block group %llu due to active swapfile",
4690 } while (key
.offset
-- > 0);
4692 if (failed
&& !retried
) {
4696 } else if (failed
&& retried
) {
4701 /* Shrinking succeeded, else we would be at "done". */
4702 trans
= btrfs_start_transaction(root
, 0);
4703 if (IS_ERR(trans
)) {
4704 ret
= PTR_ERR(trans
);
4708 mutex_lock(&fs_info
->chunk_mutex
);
4709 /* Clear all state bits beyond the shrunk device size */
4710 clear_extent_bits(&device
->alloc_state
, new_size
, (u64
)-1,
4713 btrfs_device_set_disk_total_bytes(device
, new_size
);
4714 if (list_empty(&device
->post_commit_list
))
4715 list_add_tail(&device
->post_commit_list
,
4716 &trans
->transaction
->dev_update_list
);
4718 WARN_ON(diff
> old_total
);
4719 btrfs_set_super_total_bytes(super_copy
,
4720 round_down(old_total
- diff
, fs_info
->sectorsize
));
4721 mutex_unlock(&fs_info
->chunk_mutex
);
4723 /* Now btrfs_update_device() will change the on-disk size. */
4724 ret
= btrfs_update_device(trans
, device
);
4726 btrfs_abort_transaction(trans
, ret
);
4727 btrfs_end_transaction(trans
);
4729 ret
= btrfs_commit_transaction(trans
);
4732 btrfs_free_path(path
);
4734 mutex_lock(&fs_info
->chunk_mutex
);
4735 btrfs_device_set_total_bytes(device
, old_size
);
4736 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
))
4737 device
->fs_devices
->total_rw_bytes
+= diff
;
4738 atomic64_add(diff
, &fs_info
->free_chunk_space
);
4739 mutex_unlock(&fs_info
->chunk_mutex
);
4744 static int btrfs_add_system_chunk(struct btrfs_fs_info
*fs_info
,
4745 struct btrfs_key
*key
,
4746 struct btrfs_chunk
*chunk
, int item_size
)
4748 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
4749 struct btrfs_disk_key disk_key
;
4753 mutex_lock(&fs_info
->chunk_mutex
);
4754 array_size
= btrfs_super_sys_array_size(super_copy
);
4755 if (array_size
+ item_size
+ sizeof(disk_key
)
4756 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE
) {
4757 mutex_unlock(&fs_info
->chunk_mutex
);
4761 ptr
= super_copy
->sys_chunk_array
+ array_size
;
4762 btrfs_cpu_key_to_disk(&disk_key
, key
);
4763 memcpy(ptr
, &disk_key
, sizeof(disk_key
));
4764 ptr
+= sizeof(disk_key
);
4765 memcpy(ptr
, chunk
, item_size
);
4766 item_size
+= sizeof(disk_key
);
4767 btrfs_set_super_sys_array_size(super_copy
, array_size
+ item_size
);
4768 mutex_unlock(&fs_info
->chunk_mutex
);
4774 * sort the devices in descending order by max_avail, total_avail
4776 static int btrfs_cmp_device_info(const void *a
, const void *b
)
4778 const struct btrfs_device_info
*di_a
= a
;
4779 const struct btrfs_device_info
*di_b
= b
;
4781 if (di_a
->max_avail
> di_b
->max_avail
)
4783 if (di_a
->max_avail
< di_b
->max_avail
)
4785 if (di_a
->total_avail
> di_b
->total_avail
)
4787 if (di_a
->total_avail
< di_b
->total_avail
)
4792 static void check_raid56_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4794 if (!(type
& BTRFS_BLOCK_GROUP_RAID56_MASK
))
4797 btrfs_set_fs_incompat(info
, RAID56
);
4800 static void check_raid1c34_incompat_flag(struct btrfs_fs_info
*info
, u64 type
)
4802 if (!(type
& (BTRFS_BLOCK_GROUP_RAID1C3
| BTRFS_BLOCK_GROUP_RAID1C4
)))
4805 btrfs_set_fs_incompat(info
, RAID1C34
);
4809 * Structure used internally for __btrfs_alloc_chunk() function.
4810 * Wraps needed parameters.
4812 struct alloc_chunk_ctl
{
4815 /* Total number of stripes to allocate */
4817 /* sub_stripes info for map */
4819 /* Stripes per device */
4821 /* Maximum number of devices to use */
4823 /* Minimum number of devices to use */
4825 /* ndevs has to be a multiple of this */
4827 /* Number of copies */
4829 /* Number of stripes worth of bytes to store parity information */
4831 u64 max_stripe_size
;
4839 static void init_alloc_chunk_ctl_policy_regular(
4840 struct btrfs_fs_devices
*fs_devices
,
4841 struct alloc_chunk_ctl
*ctl
)
4843 u64 type
= ctl
->type
;
4845 if (type
& BTRFS_BLOCK_GROUP_DATA
) {
4846 ctl
->max_stripe_size
= SZ_1G
;
4847 ctl
->max_chunk_size
= BTRFS_MAX_DATA_CHUNK_SIZE
;
4848 } else if (type
& BTRFS_BLOCK_GROUP_METADATA
) {
4849 /* For larger filesystems, use larger metadata chunks */
4850 if (fs_devices
->total_rw_bytes
> 50ULL * SZ_1G
)
4851 ctl
->max_stripe_size
= SZ_1G
;
4853 ctl
->max_stripe_size
= SZ_256M
;
4854 ctl
->max_chunk_size
= ctl
->max_stripe_size
;
4855 } else if (type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
4856 ctl
->max_stripe_size
= SZ_32M
;
4857 ctl
->max_chunk_size
= 2 * ctl
->max_stripe_size
;
4858 ctl
->devs_max
= min_t(int, ctl
->devs_max
,
4859 BTRFS_MAX_DEVS_SYS_CHUNK
);
4864 /* We don't want a chunk larger than 10% of writable space */
4865 ctl
->max_chunk_size
= min(div_factor(fs_devices
->total_rw_bytes
, 1),
4866 ctl
->max_chunk_size
);
4867 ctl
->dev_extent_min
= BTRFS_STRIPE_LEN
* ctl
->dev_stripes
;
4870 static void init_alloc_chunk_ctl(struct btrfs_fs_devices
*fs_devices
,
4871 struct alloc_chunk_ctl
*ctl
)
4873 int index
= btrfs_bg_flags_to_raid_index(ctl
->type
);
4875 ctl
->sub_stripes
= btrfs_raid_array
[index
].sub_stripes
;
4876 ctl
->dev_stripes
= btrfs_raid_array
[index
].dev_stripes
;
4877 ctl
->devs_max
= btrfs_raid_array
[index
].devs_max
;
4879 ctl
->devs_max
= BTRFS_MAX_DEVS(fs_devices
->fs_info
);
4880 ctl
->devs_min
= btrfs_raid_array
[index
].devs_min
;
4881 ctl
->devs_increment
= btrfs_raid_array
[index
].devs_increment
;
4882 ctl
->ncopies
= btrfs_raid_array
[index
].ncopies
;
4883 ctl
->nparity
= btrfs_raid_array
[index
].nparity
;
4886 switch (fs_devices
->chunk_alloc_policy
) {
4887 case BTRFS_CHUNK_ALLOC_REGULAR
:
4888 init_alloc_chunk_ctl_policy_regular(fs_devices
, ctl
);
4895 static int gather_device_info(struct btrfs_fs_devices
*fs_devices
,
4896 struct alloc_chunk_ctl
*ctl
,
4897 struct btrfs_device_info
*devices_info
)
4899 struct btrfs_fs_info
*info
= fs_devices
->fs_info
;
4900 struct btrfs_device
*device
;
4902 u64 dev_extent_want
= ctl
->max_stripe_size
* ctl
->dev_stripes
;
4909 * in the first pass through the devices list, we gather information
4910 * about the available holes on each device.
4912 list_for_each_entry(device
, &fs_devices
->alloc_list
, dev_alloc_list
) {
4913 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
)) {
4915 "BTRFS: read-only device in alloc_list\n");
4919 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
4920 &device
->dev_state
) ||
4921 test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
))
4924 if (device
->total_bytes
> device
->bytes_used
)
4925 total_avail
= device
->total_bytes
- device
->bytes_used
;
4929 /* If there is no space on this device, skip it. */
4930 if (total_avail
< ctl
->dev_extent_min
)
4933 ret
= find_free_dev_extent(device
, dev_extent_want
, &dev_offset
,
4935 if (ret
&& ret
!= -ENOSPC
)
4939 max_avail
= dev_extent_want
;
4941 if (max_avail
< ctl
->dev_extent_min
) {
4942 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
4944 "%s: devid %llu has no free space, have=%llu want=%llu",
4945 __func__
, device
->devid
, max_avail
,
4946 ctl
->dev_extent_min
);
4950 if (ndevs
== fs_devices
->rw_devices
) {
4951 WARN(1, "%s: found more than %llu devices\n",
4952 __func__
, fs_devices
->rw_devices
);
4955 devices_info
[ndevs
].dev_offset
= dev_offset
;
4956 devices_info
[ndevs
].max_avail
= max_avail
;
4957 devices_info
[ndevs
].total_avail
= total_avail
;
4958 devices_info
[ndevs
].dev
= device
;
4964 * now sort the devices by hole size / available space
4966 sort(devices_info
, ndevs
, sizeof(struct btrfs_device_info
),
4967 btrfs_cmp_device_info
, NULL
);
4972 static int decide_stripe_size_regular(struct alloc_chunk_ctl
*ctl
,
4973 struct btrfs_device_info
*devices_info
)
4975 /* Number of stripes that count for block group size */
4979 * The primary goal is to maximize the number of stripes, so use as
4980 * many devices as possible, even if the stripes are not maximum sized.
4982 * The DUP profile stores more than one stripe per device, the
4983 * max_avail is the total size so we have to adjust.
4985 ctl
->stripe_size
= div_u64(devices_info
[ctl
->ndevs
- 1].max_avail
,
4987 ctl
->num_stripes
= ctl
->ndevs
* ctl
->dev_stripes
;
4989 /* This will have to be fixed for RAID1 and RAID10 over more drives */
4990 data_stripes
= (ctl
->num_stripes
- ctl
->nparity
) / ctl
->ncopies
;
4993 * Use the number of data stripes to figure out how big this chunk is
4994 * really going to be in terms of logical address space, and compare
4995 * that answer with the max chunk size. If it's higher, we try to
4996 * reduce stripe_size.
4998 if (ctl
->stripe_size
* data_stripes
> ctl
->max_chunk_size
) {
5000 * Reduce stripe_size, round it up to a 16MB boundary again and
5001 * then use it, unless it ends up being even bigger than the
5002 * previous value we had already.
5004 ctl
->stripe_size
= min(round_up(div_u64(ctl
->max_chunk_size
,
5005 data_stripes
), SZ_16M
),
5009 /* Align to BTRFS_STRIPE_LEN */
5010 ctl
->stripe_size
= round_down(ctl
->stripe_size
, BTRFS_STRIPE_LEN
);
5011 ctl
->chunk_size
= ctl
->stripe_size
* data_stripes
;
5016 static int decide_stripe_size(struct btrfs_fs_devices
*fs_devices
,
5017 struct alloc_chunk_ctl
*ctl
,
5018 struct btrfs_device_info
*devices_info
)
5020 struct btrfs_fs_info
*info
= fs_devices
->fs_info
;
5023 * Round down to number of usable stripes, devs_increment can be any
5024 * number so we can't use round_down() that requires power of 2, while
5025 * rounddown is safe.
5027 ctl
->ndevs
= rounddown(ctl
->ndevs
, ctl
->devs_increment
);
5029 if (ctl
->ndevs
< ctl
->devs_min
) {
5030 if (btrfs_test_opt(info
, ENOSPC_DEBUG
)) {
5032 "%s: not enough devices with free space: have=%d minimum required=%d",
5033 __func__
, ctl
->ndevs
, ctl
->devs_min
);
5038 ctl
->ndevs
= min(ctl
->ndevs
, ctl
->devs_max
);
5040 switch (fs_devices
->chunk_alloc_policy
) {
5041 case BTRFS_CHUNK_ALLOC_REGULAR
:
5042 return decide_stripe_size_regular(ctl
, devices_info
);
5048 static int create_chunk(struct btrfs_trans_handle
*trans
,
5049 struct alloc_chunk_ctl
*ctl
,
5050 struct btrfs_device_info
*devices_info
)
5052 struct btrfs_fs_info
*info
= trans
->fs_info
;
5053 struct map_lookup
*map
= NULL
;
5054 struct extent_map_tree
*em_tree
;
5055 struct extent_map
*em
;
5056 u64 start
= ctl
->start
;
5057 u64 type
= ctl
->type
;
5062 map
= kmalloc(map_lookup_size(ctl
->num_stripes
), GFP_NOFS
);
5065 map
->num_stripes
= ctl
->num_stripes
;
5067 for (i
= 0; i
< ctl
->ndevs
; ++i
) {
5068 for (j
= 0; j
< ctl
->dev_stripes
; ++j
) {
5069 int s
= i
* ctl
->dev_stripes
+ j
;
5070 map
->stripes
[s
].dev
= devices_info
[i
].dev
;
5071 map
->stripes
[s
].physical
= devices_info
[i
].dev_offset
+
5072 j
* ctl
->stripe_size
;
5075 map
->stripe_len
= BTRFS_STRIPE_LEN
;
5076 map
->io_align
= BTRFS_STRIPE_LEN
;
5077 map
->io_width
= BTRFS_STRIPE_LEN
;
5079 map
->sub_stripes
= ctl
->sub_stripes
;
5081 trace_btrfs_chunk_alloc(info
, map
, start
, ctl
->chunk_size
);
5083 em
= alloc_extent_map();
5088 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
5089 em
->map_lookup
= map
;
5091 em
->len
= ctl
->chunk_size
;
5092 em
->block_start
= 0;
5093 em
->block_len
= em
->len
;
5094 em
->orig_block_len
= ctl
->stripe_size
;
5096 em_tree
= &info
->mapping_tree
;
5097 write_lock(&em_tree
->lock
);
5098 ret
= add_extent_mapping(em_tree
, em
, 0);
5100 write_unlock(&em_tree
->lock
);
5101 free_extent_map(em
);
5104 write_unlock(&em_tree
->lock
);
5106 ret
= btrfs_make_block_group(trans
, 0, type
, start
, ctl
->chunk_size
);
5108 goto error_del_extent
;
5110 for (i
= 0; i
< map
->num_stripes
; i
++) {
5111 struct btrfs_device
*dev
= map
->stripes
[i
].dev
;
5113 btrfs_device_set_bytes_used(dev
,
5114 dev
->bytes_used
+ ctl
->stripe_size
);
5115 if (list_empty(&dev
->post_commit_list
))
5116 list_add_tail(&dev
->post_commit_list
,
5117 &trans
->transaction
->dev_update_list
);
5120 atomic64_sub(ctl
->stripe_size
* map
->num_stripes
,
5121 &info
->free_chunk_space
);
5123 free_extent_map(em
);
5124 check_raid56_incompat_flag(info
, type
);
5125 check_raid1c34_incompat_flag(info
, type
);
5130 write_lock(&em_tree
->lock
);
5131 remove_extent_mapping(em_tree
, em
);
5132 write_unlock(&em_tree
->lock
);
5134 /* One for our allocation */
5135 free_extent_map(em
);
5136 /* One for the tree reference */
5137 free_extent_map(em
);
5142 int btrfs_alloc_chunk(struct btrfs_trans_handle
*trans
, u64 type
)
5144 struct btrfs_fs_info
*info
= trans
->fs_info
;
5145 struct btrfs_fs_devices
*fs_devices
= info
->fs_devices
;
5146 struct btrfs_device_info
*devices_info
= NULL
;
5147 struct alloc_chunk_ctl ctl
;
5150 lockdep_assert_held(&info
->chunk_mutex
);
5152 if (!alloc_profile_is_valid(type
, 0)) {
5157 if (list_empty(&fs_devices
->alloc_list
)) {
5158 if (btrfs_test_opt(info
, ENOSPC_DEBUG
))
5159 btrfs_debug(info
, "%s: no writable device", __func__
);
5163 if (!(type
& BTRFS_BLOCK_GROUP_TYPE_MASK
)) {
5164 btrfs_err(info
, "invalid chunk type 0x%llx requested", type
);
5169 ctl
.start
= find_next_chunk(info
);
5171 init_alloc_chunk_ctl(fs_devices
, &ctl
);
5173 devices_info
= kcalloc(fs_devices
->rw_devices
, sizeof(*devices_info
),
5178 ret
= gather_device_info(fs_devices
, &ctl
, devices_info
);
5182 ret
= decide_stripe_size(fs_devices
, &ctl
, devices_info
);
5186 ret
= create_chunk(trans
, &ctl
, devices_info
);
5189 kfree(devices_info
);
5194 * Chunk allocation falls into two parts. The first part does work
5195 * that makes the new allocated chunk usable, but does not do any operation
5196 * that modifies the chunk tree. The second part does the work that
5197 * requires modifying the chunk tree. This division is important for the
5198 * bootstrap process of adding storage to a seed btrfs.
5200 int btrfs_finish_chunk_alloc(struct btrfs_trans_handle
*trans
,
5201 u64 chunk_offset
, u64 chunk_size
)
5203 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
5204 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
5205 struct btrfs_root
*chunk_root
= fs_info
->chunk_root
;
5206 struct btrfs_key key
;
5207 struct btrfs_device
*device
;
5208 struct btrfs_chunk
*chunk
;
5209 struct btrfs_stripe
*stripe
;
5210 struct extent_map
*em
;
5211 struct map_lookup
*map
;
5218 em
= btrfs_get_chunk_map(fs_info
, chunk_offset
, chunk_size
);
5222 map
= em
->map_lookup
;
5223 item_size
= btrfs_chunk_item_size(map
->num_stripes
);
5224 stripe_size
= em
->orig_block_len
;
5226 chunk
= kzalloc(item_size
, GFP_NOFS
);
5233 * Take the device list mutex to prevent races with the final phase of
5234 * a device replace operation that replaces the device object associated
5235 * with the map's stripes, because the device object's id can change
5236 * at any time during that final phase of the device replace operation
5237 * (dev-replace.c:btrfs_dev_replace_finishing()).
5239 mutex_lock(&fs_info
->fs_devices
->device_list_mutex
);
5240 for (i
= 0; i
< map
->num_stripes
; i
++) {
5241 device
= map
->stripes
[i
].dev
;
5242 dev_offset
= map
->stripes
[i
].physical
;
5244 ret
= btrfs_update_device(trans
, device
);
5247 ret
= btrfs_alloc_dev_extent(trans
, device
, chunk_offset
,
5248 dev_offset
, stripe_size
);
5253 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
5257 stripe
= &chunk
->stripe
;
5258 for (i
= 0; i
< map
->num_stripes
; i
++) {
5259 device
= map
->stripes
[i
].dev
;
5260 dev_offset
= map
->stripes
[i
].physical
;
5262 btrfs_set_stack_stripe_devid(stripe
, device
->devid
);
5263 btrfs_set_stack_stripe_offset(stripe
, dev_offset
);
5264 memcpy(stripe
->dev_uuid
, device
->uuid
, BTRFS_UUID_SIZE
);
5267 mutex_unlock(&fs_info
->fs_devices
->device_list_mutex
);
5269 btrfs_set_stack_chunk_length(chunk
, chunk_size
);
5270 btrfs_set_stack_chunk_owner(chunk
, extent_root
->root_key
.objectid
);
5271 btrfs_set_stack_chunk_stripe_len(chunk
, map
->stripe_len
);
5272 btrfs_set_stack_chunk_type(chunk
, map
->type
);
5273 btrfs_set_stack_chunk_num_stripes(chunk
, map
->num_stripes
);
5274 btrfs_set_stack_chunk_io_align(chunk
, map
->stripe_len
);
5275 btrfs_set_stack_chunk_io_width(chunk
, map
->stripe_len
);
5276 btrfs_set_stack_chunk_sector_size(chunk
, fs_info
->sectorsize
);
5277 btrfs_set_stack_chunk_sub_stripes(chunk
, map
->sub_stripes
);
5279 key
.objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
5280 key
.type
= BTRFS_CHUNK_ITEM_KEY
;
5281 key
.offset
= chunk_offset
;
5283 ret
= btrfs_insert_item(trans
, chunk_root
, &key
, chunk
, item_size
);
5284 if (ret
== 0 && map
->type
& BTRFS_BLOCK_GROUP_SYSTEM
) {
5286 * TODO: Cleanup of inserted chunk root in case of
5289 ret
= btrfs_add_system_chunk(fs_info
, &key
, chunk
, item_size
);
5294 free_extent_map(em
);
5298 static noinline
int init_first_rw_device(struct btrfs_trans_handle
*trans
)
5300 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
5304 alloc_profile
= btrfs_metadata_alloc_profile(fs_info
);
5305 ret
= btrfs_alloc_chunk(trans
, alloc_profile
);
5309 alloc_profile
= btrfs_system_alloc_profile(fs_info
);
5310 ret
= btrfs_alloc_chunk(trans
, alloc_profile
);
5314 static inline int btrfs_chunk_max_errors(struct map_lookup
*map
)
5316 const int index
= btrfs_bg_flags_to_raid_index(map
->type
);
5318 return btrfs_raid_array
[index
].tolerated_failures
;
5321 int btrfs_chunk_readonly(struct btrfs_fs_info
*fs_info
, u64 chunk_offset
)
5323 struct extent_map
*em
;
5324 struct map_lookup
*map
;
5329 em
= btrfs_get_chunk_map(fs_info
, chunk_offset
, 1);
5333 map
= em
->map_lookup
;
5334 for (i
= 0; i
< map
->num_stripes
; i
++) {
5335 if (test_bit(BTRFS_DEV_STATE_MISSING
,
5336 &map
->stripes
[i
].dev
->dev_state
)) {
5340 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE
,
5341 &map
->stripes
[i
].dev
->dev_state
)) {
5348 * If the number of missing devices is larger than max errors,
5349 * we can not write the data into that chunk successfully, so
5352 if (miss_ndevs
> btrfs_chunk_max_errors(map
))
5355 free_extent_map(em
);
5359 void btrfs_mapping_tree_free(struct extent_map_tree
*tree
)
5361 struct extent_map
*em
;
5364 write_lock(&tree
->lock
);
5365 em
= lookup_extent_mapping(tree
, 0, (u64
)-1);
5367 remove_extent_mapping(tree
, em
);
5368 write_unlock(&tree
->lock
);
5372 free_extent_map(em
);
5373 /* once for the tree */
5374 free_extent_map(em
);
5378 int btrfs_num_copies(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
5380 struct extent_map
*em
;
5381 struct map_lookup
*map
;
5384 em
= btrfs_get_chunk_map(fs_info
, logical
, len
);
5387 * We could return errors for these cases, but that could get
5388 * ugly and we'd probably do the same thing which is just not do
5389 * anything else and exit, so return 1 so the callers don't try
5390 * to use other copies.
5394 map
= em
->map_lookup
;
5395 if (map
->type
& (BTRFS_BLOCK_GROUP_DUP
| BTRFS_BLOCK_GROUP_RAID1_MASK
))
5396 ret
= map
->num_stripes
;
5397 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5398 ret
= map
->sub_stripes
;
5399 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID5
)
5401 else if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
5403 * There could be two corrupted data stripes, we need
5404 * to loop retry in order to rebuild the correct data.
5406 * Fail a stripe at a time on every retry except the
5407 * stripe under reconstruction.
5409 ret
= map
->num_stripes
;
5412 free_extent_map(em
);
5414 down_read(&fs_info
->dev_replace
.rwsem
);
5415 if (btrfs_dev_replace_is_ongoing(&fs_info
->dev_replace
) &&
5416 fs_info
->dev_replace
.tgtdev
)
5418 up_read(&fs_info
->dev_replace
.rwsem
);
5423 unsigned long btrfs_full_stripe_len(struct btrfs_fs_info
*fs_info
,
5426 struct extent_map
*em
;
5427 struct map_lookup
*map
;
5428 unsigned long len
= fs_info
->sectorsize
;
5430 em
= btrfs_get_chunk_map(fs_info
, logical
, len
);
5432 if (!WARN_ON(IS_ERR(em
))) {
5433 map
= em
->map_lookup
;
5434 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
5435 len
= map
->stripe_len
* nr_data_stripes(map
);
5436 free_extent_map(em
);
5441 int btrfs_is_parity_mirror(struct btrfs_fs_info
*fs_info
, u64 logical
, u64 len
)
5443 struct extent_map
*em
;
5444 struct map_lookup
*map
;
5447 em
= btrfs_get_chunk_map(fs_info
, logical
, len
);
5449 if(!WARN_ON(IS_ERR(em
))) {
5450 map
= em
->map_lookup
;
5451 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
)
5453 free_extent_map(em
);
5458 static int find_live_mirror(struct btrfs_fs_info
*fs_info
,
5459 struct map_lookup
*map
, int first
,
5460 int dev_replace_is_ongoing
)
5464 int preferred_mirror
;
5466 struct btrfs_device
*srcdev
;
5469 (BTRFS_BLOCK_GROUP_RAID1_MASK
| BTRFS_BLOCK_GROUP_RAID10
)));
5471 if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
)
5472 num_stripes
= map
->sub_stripes
;
5474 num_stripes
= map
->num_stripes
;
5476 preferred_mirror
= first
+ current
->pid
% num_stripes
;
5478 if (dev_replace_is_ongoing
&&
5479 fs_info
->dev_replace
.cont_reading_from_srcdev_mode
==
5480 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID
)
5481 srcdev
= fs_info
->dev_replace
.srcdev
;
5486 * try to avoid the drive that is the source drive for a
5487 * dev-replace procedure, only choose it if no other non-missing
5488 * mirror is available
5490 for (tolerance
= 0; tolerance
< 2; tolerance
++) {
5491 if (map
->stripes
[preferred_mirror
].dev
->bdev
&&
5492 (tolerance
|| map
->stripes
[preferred_mirror
].dev
!= srcdev
))
5493 return preferred_mirror
;
5494 for (i
= first
; i
< first
+ num_stripes
; i
++) {
5495 if (map
->stripes
[i
].dev
->bdev
&&
5496 (tolerance
|| map
->stripes
[i
].dev
!= srcdev
))
5501 /* we couldn't find one that doesn't fail. Just return something
5502 * and the io error handling code will clean up eventually
5504 return preferred_mirror
;
5507 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
5508 static void sort_parity_stripes(struct btrfs_bio
*bbio
, int num_stripes
)
5515 for (i
= 0; i
< num_stripes
- 1; i
++) {
5516 /* Swap if parity is on a smaller index */
5517 if (bbio
->raid_map
[i
] > bbio
->raid_map
[i
+ 1]) {
5518 swap(bbio
->stripes
[i
], bbio
->stripes
[i
+ 1]);
5519 swap(bbio
->raid_map
[i
], bbio
->raid_map
[i
+ 1]);
5526 static struct btrfs_bio
*alloc_btrfs_bio(int total_stripes
, int real_stripes
)
5528 struct btrfs_bio
*bbio
= kzalloc(
5529 /* the size of the btrfs_bio */
5530 sizeof(struct btrfs_bio
) +
5531 /* plus the variable array for the stripes */
5532 sizeof(struct btrfs_bio_stripe
) * (total_stripes
) +
5533 /* plus the variable array for the tgt dev */
5534 sizeof(int) * (real_stripes
) +
5536 * plus the raid_map, which includes both the tgt dev
5539 sizeof(u64
) * (total_stripes
),
5540 GFP_NOFS
|__GFP_NOFAIL
);
5542 atomic_set(&bbio
->error
, 0);
5543 refcount_set(&bbio
->refs
, 1);
5545 bbio
->tgtdev_map
= (int *)(bbio
->stripes
+ total_stripes
);
5546 bbio
->raid_map
= (u64
*)(bbio
->tgtdev_map
+ real_stripes
);
5551 void btrfs_get_bbio(struct btrfs_bio
*bbio
)
5553 WARN_ON(!refcount_read(&bbio
->refs
));
5554 refcount_inc(&bbio
->refs
);
5557 void btrfs_put_bbio(struct btrfs_bio
*bbio
)
5561 if (refcount_dec_and_test(&bbio
->refs
))
5565 /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5567 * Please note that, discard won't be sent to target device of device
5570 static int __btrfs_map_block_for_discard(struct btrfs_fs_info
*fs_info
,
5571 u64 logical
, u64
*length_ret
,
5572 struct btrfs_bio
**bbio_ret
)
5574 struct extent_map
*em
;
5575 struct map_lookup
*map
;
5576 struct btrfs_bio
*bbio
;
5577 u64 length
= *length_ret
;
5581 u64 stripe_end_offset
;
5588 u32 sub_stripes
= 0;
5589 u64 stripes_per_dev
= 0;
5590 u32 remaining_stripes
= 0;
5591 u32 last_stripe
= 0;
5595 /* discard always return a bbio */
5598 em
= btrfs_get_chunk_map(fs_info
, logical
, length
);
5602 map
= em
->map_lookup
;
5603 /* we don't discard raid56 yet */
5604 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5609 offset
= logical
- em
->start
;
5610 length
= min_t(u64
, em
->start
+ em
->len
- logical
, length
);
5611 *length_ret
= length
;
5613 stripe_len
= map
->stripe_len
;
5615 * stripe_nr counts the total number of stripes we have to stride
5616 * to get to this block
5618 stripe_nr
= div64_u64(offset
, stripe_len
);
5620 /* stripe_offset is the offset of this block in its stripe */
5621 stripe_offset
= offset
- stripe_nr
* stripe_len
;
5623 stripe_nr_end
= round_up(offset
+ length
, map
->stripe_len
);
5624 stripe_nr_end
= div64_u64(stripe_nr_end
, map
->stripe_len
);
5625 stripe_cnt
= stripe_nr_end
- stripe_nr
;
5626 stripe_end_offset
= stripe_nr_end
* map
->stripe_len
-
5629 * after this, stripe_nr is the number of stripes on this
5630 * device we have to walk to find the data, and stripe_index is
5631 * the number of our device in the stripe array
5635 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5636 BTRFS_BLOCK_GROUP_RAID10
)) {
5637 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
)
5640 sub_stripes
= map
->sub_stripes
;
5642 factor
= map
->num_stripes
/ sub_stripes
;
5643 num_stripes
= min_t(u64
, map
->num_stripes
,
5644 sub_stripes
* stripe_cnt
);
5645 stripe_nr
= div_u64_rem(stripe_nr
, factor
, &stripe_index
);
5646 stripe_index
*= sub_stripes
;
5647 stripes_per_dev
= div_u64_rem(stripe_cnt
, factor
,
5648 &remaining_stripes
);
5649 div_u64_rem(stripe_nr_end
- 1, factor
, &last_stripe
);
5650 last_stripe
*= sub_stripes
;
5651 } else if (map
->type
& (BTRFS_BLOCK_GROUP_RAID1_MASK
|
5652 BTRFS_BLOCK_GROUP_DUP
)) {
5653 num_stripes
= map
->num_stripes
;
5655 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
5659 bbio
= alloc_btrfs_bio(num_stripes
, 0);
5665 for (i
= 0; i
< num_stripes
; i
++) {
5666 bbio
->stripes
[i
].physical
=
5667 map
->stripes
[stripe_index
].physical
+
5668 stripe_offset
+ stripe_nr
* map
->stripe_len
;
5669 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
5671 if (map
->type
& (BTRFS_BLOCK_GROUP_RAID0
|
5672 BTRFS_BLOCK_GROUP_RAID10
)) {
5673 bbio
->stripes
[i
].length
= stripes_per_dev
*
5676 if (i
/ sub_stripes
< remaining_stripes
)
5677 bbio
->stripes
[i
].length
+=
5681 * Special for the first stripe and
5684 * |-------|...|-------|
5688 if (i
< sub_stripes
)
5689 bbio
->stripes
[i
].length
-=
5692 if (stripe_index
>= last_stripe
&&
5693 stripe_index
<= (last_stripe
+
5695 bbio
->stripes
[i
].length
-=
5698 if (i
== sub_stripes
- 1)
5701 bbio
->stripes
[i
].length
= length
;
5705 if (stripe_index
== map
->num_stripes
) {
5712 bbio
->map_type
= map
->type
;
5713 bbio
->num_stripes
= num_stripes
;
5715 free_extent_map(em
);
5720 * In dev-replace case, for repair case (that's the only case where the mirror
5721 * is selected explicitly when calling btrfs_map_block), blocks left of the
5722 * left cursor can also be read from the target drive.
5724 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5726 * For READ, it also needs to be supported using the same mirror number.
5728 * If the requested block is not left of the left cursor, EIO is returned. This
5729 * can happen because btrfs_num_copies() returns one more in the dev-replace
5732 static int get_extra_mirror_from_replace(struct btrfs_fs_info
*fs_info
,
5733 u64 logical
, u64 length
,
5734 u64 srcdev_devid
, int *mirror_num
,
5737 struct btrfs_bio
*bbio
= NULL
;
5739 int index_srcdev
= 0;
5741 u64 physical_of_found
= 0;
5745 ret
= __btrfs_map_block(fs_info
, BTRFS_MAP_GET_READ_MIRRORS
,
5746 logical
, &length
, &bbio
, 0, 0);
5748 ASSERT(bbio
== NULL
);
5752 num_stripes
= bbio
->num_stripes
;
5753 if (*mirror_num
> num_stripes
) {
5755 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5756 * that means that the requested area is not left of the left
5759 btrfs_put_bbio(bbio
);
5764 * process the rest of the function using the mirror_num of the source
5765 * drive. Therefore look it up first. At the end, patch the device
5766 * pointer to the one of the target drive.
5768 for (i
= 0; i
< num_stripes
; i
++) {
5769 if (bbio
->stripes
[i
].dev
->devid
!= srcdev_devid
)
5773 * In case of DUP, in order to keep it simple, only add the
5774 * mirror with the lowest physical address
5777 physical_of_found
<= bbio
->stripes
[i
].physical
)
5782 physical_of_found
= bbio
->stripes
[i
].physical
;
5785 btrfs_put_bbio(bbio
);
5791 *mirror_num
= index_srcdev
+ 1;
5792 *physical
= physical_of_found
;
5796 static void handle_ops_on_dev_replace(enum btrfs_map_op op
,
5797 struct btrfs_bio
**bbio_ret
,
5798 struct btrfs_dev_replace
*dev_replace
,
5799 int *num_stripes_ret
, int *max_errors_ret
)
5801 struct btrfs_bio
*bbio
= *bbio_ret
;
5802 u64 srcdev_devid
= dev_replace
->srcdev
->devid
;
5803 int tgtdev_indexes
= 0;
5804 int num_stripes
= *num_stripes_ret
;
5805 int max_errors
= *max_errors_ret
;
5808 if (op
== BTRFS_MAP_WRITE
) {
5809 int index_where_to_add
;
5812 * duplicate the write operations while the dev replace
5813 * procedure is running. Since the copying of the old disk to
5814 * the new disk takes place at run time while the filesystem is
5815 * mounted writable, the regular write operations to the old
5816 * disk have to be duplicated to go to the new disk as well.
5818 * Note that device->missing is handled by the caller, and that
5819 * the write to the old disk is already set up in the stripes
5822 index_where_to_add
= num_stripes
;
5823 for (i
= 0; i
< num_stripes
; i
++) {
5824 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5825 /* write to new disk, too */
5826 struct btrfs_bio_stripe
*new =
5827 bbio
->stripes
+ index_where_to_add
;
5828 struct btrfs_bio_stripe
*old
=
5831 new->physical
= old
->physical
;
5832 new->length
= old
->length
;
5833 new->dev
= dev_replace
->tgtdev
;
5834 bbio
->tgtdev_map
[i
] = index_where_to_add
;
5835 index_where_to_add
++;
5840 num_stripes
= index_where_to_add
;
5841 } else if (op
== BTRFS_MAP_GET_READ_MIRRORS
) {
5842 int index_srcdev
= 0;
5844 u64 physical_of_found
= 0;
5847 * During the dev-replace procedure, the target drive can also
5848 * be used to read data in case it is needed to repair a corrupt
5849 * block elsewhere. This is possible if the requested area is
5850 * left of the left cursor. In this area, the target drive is a
5851 * full copy of the source drive.
5853 for (i
= 0; i
< num_stripes
; i
++) {
5854 if (bbio
->stripes
[i
].dev
->devid
== srcdev_devid
) {
5856 * In case of DUP, in order to keep it simple,
5857 * only add the mirror with the lowest physical
5861 physical_of_found
<=
5862 bbio
->stripes
[i
].physical
)
5866 physical_of_found
= bbio
->stripes
[i
].physical
;
5870 struct btrfs_bio_stripe
*tgtdev_stripe
=
5871 bbio
->stripes
+ num_stripes
;
5873 tgtdev_stripe
->physical
= physical_of_found
;
5874 tgtdev_stripe
->length
=
5875 bbio
->stripes
[index_srcdev
].length
;
5876 tgtdev_stripe
->dev
= dev_replace
->tgtdev
;
5877 bbio
->tgtdev_map
[index_srcdev
] = num_stripes
;
5884 *num_stripes_ret
= num_stripes
;
5885 *max_errors_ret
= max_errors
;
5886 bbio
->num_tgtdevs
= tgtdev_indexes
;
5890 static bool need_full_stripe(enum btrfs_map_op op
)
5892 return (op
== BTRFS_MAP_WRITE
|| op
== BTRFS_MAP_GET_READ_MIRRORS
);
5896 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5897 * tuple. This information is used to calculate how big a
5898 * particular bio can get before it straddles a stripe.
5900 * @fs_info - the filesystem
5901 * @logical - address that we want to figure out the geometry of
5902 * @len - the length of IO we are going to perform, starting at @logical
5903 * @op - type of operation - write or read
5904 * @io_geom - pointer used to return values
5906 * Returns < 0 in case a chunk for the given logical address cannot be found,
5907 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5909 int btrfs_get_io_geometry(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
5910 u64 logical
, u64 len
, struct btrfs_io_geometry
*io_geom
)
5912 struct extent_map
*em
;
5913 struct map_lookup
*map
;
5918 u64 raid56_full_stripe_start
= (u64
)-1;
5922 ASSERT(op
!= BTRFS_MAP_DISCARD
);
5924 em
= btrfs_get_chunk_map(fs_info
, logical
, len
);
5928 map
= em
->map_lookup
;
5929 /* Offset of this logical address in the chunk */
5930 offset
= logical
- em
->start
;
5931 /* Len of a stripe in a chunk */
5932 stripe_len
= map
->stripe_len
;
5933 /* Stripe wher this block falls in */
5934 stripe_nr
= div64_u64(offset
, stripe_len
);
5935 /* Offset of stripe in the chunk */
5936 stripe_offset
= stripe_nr
* stripe_len
;
5937 if (offset
< stripe_offset
) {
5939 "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5940 stripe_offset
, offset
, em
->start
, logical
, stripe_len
);
5945 /* stripe_offset is the offset of this block in its stripe */
5946 stripe_offset
= offset
- stripe_offset
;
5947 data_stripes
= nr_data_stripes(map
);
5949 if (map
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
5950 u64 max_len
= stripe_len
- stripe_offset
;
5953 * In case of raid56, we need to know the stripe aligned start
5955 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
5956 unsigned long full_stripe_len
= stripe_len
* data_stripes
;
5957 raid56_full_stripe_start
= offset
;
5960 * Allow a write of a full stripe, but make sure we
5961 * don't allow straddling of stripes
5963 raid56_full_stripe_start
= div64_u64(raid56_full_stripe_start
,
5965 raid56_full_stripe_start
*= full_stripe_len
;
5968 * For writes to RAID[56], allow a full stripeset across
5969 * all disks. For other RAID types and for RAID[56]
5970 * reads, just allow a single stripe (on a single disk).
5972 if (op
== BTRFS_MAP_WRITE
) {
5973 max_len
= stripe_len
* data_stripes
-
5974 (offset
- raid56_full_stripe_start
);
5977 len
= min_t(u64
, em
->len
- offset
, max_len
);
5979 len
= em
->len
- offset
;
5983 io_geom
->offset
= offset
;
5984 io_geom
->stripe_len
= stripe_len
;
5985 io_geom
->stripe_nr
= stripe_nr
;
5986 io_geom
->stripe_offset
= stripe_offset
;
5987 io_geom
->raid56_stripe_offset
= raid56_full_stripe_start
;
5991 free_extent_map(em
);
5995 static int __btrfs_map_block(struct btrfs_fs_info
*fs_info
,
5996 enum btrfs_map_op op
,
5997 u64 logical
, u64
*length
,
5998 struct btrfs_bio
**bbio_ret
,
5999 int mirror_num
, int need_raid_map
)
6001 struct extent_map
*em
;
6002 struct map_lookup
*map
;
6012 int tgtdev_indexes
= 0;
6013 struct btrfs_bio
*bbio
= NULL
;
6014 struct btrfs_dev_replace
*dev_replace
= &fs_info
->dev_replace
;
6015 int dev_replace_is_ongoing
= 0;
6016 int num_alloc_stripes
;
6017 int patch_the_first_stripe_for_dev_replace
= 0;
6018 u64 physical_to_patch_in_first_stripe
= 0;
6019 u64 raid56_full_stripe_start
= (u64
)-1;
6020 struct btrfs_io_geometry geom
;
6023 ASSERT(op
!= BTRFS_MAP_DISCARD
);
6025 ret
= btrfs_get_io_geometry(fs_info
, op
, logical
, *length
, &geom
);
6029 em
= btrfs_get_chunk_map(fs_info
, logical
, *length
);
6030 ASSERT(!IS_ERR(em
));
6031 map
= em
->map_lookup
;
6034 stripe_len
= geom
.stripe_len
;
6035 stripe_nr
= geom
.stripe_nr
;
6036 stripe_offset
= geom
.stripe_offset
;
6037 raid56_full_stripe_start
= geom
.raid56_stripe_offset
;
6038 data_stripes
= nr_data_stripes(map
);
6040 down_read(&dev_replace
->rwsem
);
6041 dev_replace_is_ongoing
= btrfs_dev_replace_is_ongoing(dev_replace
);
6043 * Hold the semaphore for read during the whole operation, write is
6044 * requested at commit time but must wait.
6046 if (!dev_replace_is_ongoing
)
6047 up_read(&dev_replace
->rwsem
);
6049 if (dev_replace_is_ongoing
&& mirror_num
== map
->num_stripes
+ 1 &&
6050 !need_full_stripe(op
) && dev_replace
->tgtdev
!= NULL
) {
6051 ret
= get_extra_mirror_from_replace(fs_info
, logical
, *length
,
6052 dev_replace
->srcdev
->devid
,
6054 &physical_to_patch_in_first_stripe
);
6058 patch_the_first_stripe_for_dev_replace
= 1;
6059 } else if (mirror_num
> map
->num_stripes
) {
6065 if (map
->type
& BTRFS_BLOCK_GROUP_RAID0
) {
6066 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
6068 if (!need_full_stripe(op
))
6070 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID1_MASK
) {
6071 if (need_full_stripe(op
))
6072 num_stripes
= map
->num_stripes
;
6073 else if (mirror_num
)
6074 stripe_index
= mirror_num
- 1;
6076 stripe_index
= find_live_mirror(fs_info
, map
, 0,
6077 dev_replace_is_ongoing
);
6078 mirror_num
= stripe_index
+ 1;
6081 } else if (map
->type
& BTRFS_BLOCK_GROUP_DUP
) {
6082 if (need_full_stripe(op
)) {
6083 num_stripes
= map
->num_stripes
;
6084 } else if (mirror_num
) {
6085 stripe_index
= mirror_num
- 1;
6090 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID10
) {
6091 u32 factor
= map
->num_stripes
/ map
->sub_stripes
;
6093 stripe_nr
= div_u64_rem(stripe_nr
, factor
, &stripe_index
);
6094 stripe_index
*= map
->sub_stripes
;
6096 if (need_full_stripe(op
))
6097 num_stripes
= map
->sub_stripes
;
6098 else if (mirror_num
)
6099 stripe_index
+= mirror_num
- 1;
6101 int old_stripe_index
= stripe_index
;
6102 stripe_index
= find_live_mirror(fs_info
, map
,
6104 dev_replace_is_ongoing
);
6105 mirror_num
= stripe_index
- old_stripe_index
+ 1;
6108 } else if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) {
6109 if (need_raid_map
&& (need_full_stripe(op
) || mirror_num
> 1)) {
6110 /* push stripe_nr back to the start of the full stripe */
6111 stripe_nr
= div64_u64(raid56_full_stripe_start
,
6112 stripe_len
* data_stripes
);
6114 /* RAID[56] write or recovery. Return all stripes */
6115 num_stripes
= map
->num_stripes
;
6116 max_errors
= nr_parity_stripes(map
);
6118 *length
= map
->stripe_len
;
6123 * Mirror #0 or #1 means the original data block.
6124 * Mirror #2 is RAID5 parity block.
6125 * Mirror #3 is RAID6 Q block.
6127 stripe_nr
= div_u64_rem(stripe_nr
,
6128 data_stripes
, &stripe_index
);
6130 stripe_index
= data_stripes
+ mirror_num
- 2;
6132 /* We distribute the parity blocks across stripes */
6133 div_u64_rem(stripe_nr
+ stripe_index
, map
->num_stripes
,
6135 if (!need_full_stripe(op
) && mirror_num
<= 1)
6140 * after this, stripe_nr is the number of stripes on this
6141 * device we have to walk to find the data, and stripe_index is
6142 * the number of our device in the stripe array
6144 stripe_nr
= div_u64_rem(stripe_nr
, map
->num_stripes
,
6146 mirror_num
= stripe_index
+ 1;
6148 if (stripe_index
>= map
->num_stripes
) {
6150 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
6151 stripe_index
, map
->num_stripes
);
6156 num_alloc_stripes
= num_stripes
;
6157 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
) {
6158 if (op
== BTRFS_MAP_WRITE
)
6159 num_alloc_stripes
<<= 1;
6160 if (op
== BTRFS_MAP_GET_READ_MIRRORS
)
6161 num_alloc_stripes
++;
6162 tgtdev_indexes
= num_stripes
;
6165 bbio
= alloc_btrfs_bio(num_alloc_stripes
, tgtdev_indexes
);
6171 for (i
= 0; i
< num_stripes
; i
++) {
6172 bbio
->stripes
[i
].physical
= map
->stripes
[stripe_index
].physical
+
6173 stripe_offset
+ stripe_nr
* map
->stripe_len
;
6174 bbio
->stripes
[i
].dev
= map
->stripes
[stripe_index
].dev
;
6178 /* build raid_map */
6179 if (map
->type
& BTRFS_BLOCK_GROUP_RAID56_MASK
&& need_raid_map
&&
6180 (need_full_stripe(op
) || mirror_num
> 1)) {
6184 /* Work out the disk rotation on this stripe-set */
6185 div_u64_rem(stripe_nr
, num_stripes
, &rot
);
6187 /* Fill in the logical address of each stripe */
6188 tmp
= stripe_nr
* data_stripes
;
6189 for (i
= 0; i
< data_stripes
; i
++)
6190 bbio
->raid_map
[(i
+rot
) % num_stripes
] =
6191 em
->start
+ (tmp
+ i
) * map
->stripe_len
;
6193 bbio
->raid_map
[(i
+rot
) % map
->num_stripes
] = RAID5_P_STRIPE
;
6194 if (map
->type
& BTRFS_BLOCK_GROUP_RAID6
)
6195 bbio
->raid_map
[(i
+rot
+1) % num_stripes
] =
6198 sort_parity_stripes(bbio
, num_stripes
);
6201 if (need_full_stripe(op
))
6202 max_errors
= btrfs_chunk_max_errors(map
);
6204 if (dev_replace_is_ongoing
&& dev_replace
->tgtdev
!= NULL
&&
6205 need_full_stripe(op
)) {
6206 handle_ops_on_dev_replace(op
, &bbio
, dev_replace
, &num_stripes
,
6211 bbio
->map_type
= map
->type
;
6212 bbio
->num_stripes
= num_stripes
;
6213 bbio
->max_errors
= max_errors
;
6214 bbio
->mirror_num
= mirror_num
;
6217 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6218 * mirror_num == num_stripes + 1 && dev_replace target drive is
6219 * available as a mirror
6221 if (patch_the_first_stripe_for_dev_replace
&& num_stripes
> 0) {
6222 WARN_ON(num_stripes
> 1);
6223 bbio
->stripes
[0].dev
= dev_replace
->tgtdev
;
6224 bbio
->stripes
[0].physical
= physical_to_patch_in_first_stripe
;
6225 bbio
->mirror_num
= map
->num_stripes
+ 1;
6228 if (dev_replace_is_ongoing
) {
6229 lockdep_assert_held(&dev_replace
->rwsem
);
6230 /* Unlock and let waiting writers proceed */
6231 up_read(&dev_replace
->rwsem
);
6233 free_extent_map(em
);
6237 int btrfs_map_block(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
6238 u64 logical
, u64
*length
,
6239 struct btrfs_bio
**bbio_ret
, int mirror_num
)
6241 if (op
== BTRFS_MAP_DISCARD
)
6242 return __btrfs_map_block_for_discard(fs_info
, logical
,
6245 return __btrfs_map_block(fs_info
, op
, logical
, length
, bbio_ret
,
6249 /* For Scrub/replace */
6250 int btrfs_map_sblock(struct btrfs_fs_info
*fs_info
, enum btrfs_map_op op
,
6251 u64 logical
, u64
*length
,
6252 struct btrfs_bio
**bbio_ret
)
6254 return __btrfs_map_block(fs_info
, op
, logical
, length
, bbio_ret
, 0, 1);
6257 static inline void btrfs_end_bbio(struct btrfs_bio
*bbio
, struct bio
*bio
)
6259 bio
->bi_private
= bbio
->private;
6260 bio
->bi_end_io
= bbio
->end_io
;
6263 btrfs_put_bbio(bbio
);
6266 static void btrfs_end_bio(struct bio
*bio
)
6268 struct btrfs_bio
*bbio
= bio
->bi_private
;
6269 int is_orig_bio
= 0;
6271 if (bio
->bi_status
) {
6272 atomic_inc(&bbio
->error
);
6273 if (bio
->bi_status
== BLK_STS_IOERR
||
6274 bio
->bi_status
== BLK_STS_TARGET
) {
6275 struct btrfs_device
*dev
= btrfs_io_bio(bio
)->device
;
6278 if (bio_op(bio
) == REQ_OP_WRITE
)
6279 btrfs_dev_stat_inc_and_print(dev
,
6280 BTRFS_DEV_STAT_WRITE_ERRS
);
6281 else if (!(bio
->bi_opf
& REQ_RAHEAD
))
6282 btrfs_dev_stat_inc_and_print(dev
,
6283 BTRFS_DEV_STAT_READ_ERRS
);
6284 if (bio
->bi_opf
& REQ_PREFLUSH
)
6285 btrfs_dev_stat_inc_and_print(dev
,
6286 BTRFS_DEV_STAT_FLUSH_ERRS
);
6290 if (bio
== bbio
->orig_bio
)
6293 btrfs_bio_counter_dec(bbio
->fs_info
);
6295 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
6298 bio
= bbio
->orig_bio
;
6301 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
6302 /* only send an error to the higher layers if it is
6303 * beyond the tolerance of the btrfs bio
6305 if (atomic_read(&bbio
->error
) > bbio
->max_errors
) {
6306 bio
->bi_status
= BLK_STS_IOERR
;
6309 * this bio is actually up to date, we didn't
6310 * go over the max number of errors
6312 bio
->bi_status
= BLK_STS_OK
;
6315 btrfs_end_bbio(bbio
, bio
);
6316 } else if (!is_orig_bio
) {
6321 static void submit_stripe_bio(struct btrfs_bio
*bbio
, struct bio
*bio
,
6322 u64 physical
, struct btrfs_device
*dev
)
6324 struct btrfs_fs_info
*fs_info
= bbio
->fs_info
;
6326 bio
->bi_private
= bbio
;
6327 btrfs_io_bio(bio
)->device
= dev
;
6328 bio
->bi_end_io
= btrfs_end_bio
;
6329 bio
->bi_iter
.bi_sector
= physical
>> 9;
6330 btrfs_debug_in_rcu(fs_info
,
6331 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6332 bio_op(bio
), bio
->bi_opf
, (u64
)bio
->bi_iter
.bi_sector
,
6333 (unsigned long)dev
->bdev
->bd_dev
, rcu_str_deref(dev
->name
),
6334 dev
->devid
, bio
->bi_iter
.bi_size
);
6335 bio_set_dev(bio
, dev
->bdev
);
6337 btrfs_bio_counter_inc_noblocked(fs_info
);
6339 btrfsic_submit_bio(bio
);
6342 static void bbio_error(struct btrfs_bio
*bbio
, struct bio
*bio
, u64 logical
)
6344 atomic_inc(&bbio
->error
);
6345 if (atomic_dec_and_test(&bbio
->stripes_pending
)) {
6346 /* Should be the original bio. */
6347 WARN_ON(bio
!= bbio
->orig_bio
);
6349 btrfs_io_bio(bio
)->mirror_num
= bbio
->mirror_num
;
6350 bio
->bi_iter
.bi_sector
= logical
>> 9;
6351 if (atomic_read(&bbio
->error
) > bbio
->max_errors
)
6352 bio
->bi_status
= BLK_STS_IOERR
;
6354 bio
->bi_status
= BLK_STS_OK
;
6355 btrfs_end_bbio(bbio
, bio
);
6359 blk_status_t
btrfs_map_bio(struct btrfs_fs_info
*fs_info
, struct bio
*bio
,
6362 struct btrfs_device
*dev
;
6363 struct bio
*first_bio
= bio
;
6364 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
6370 struct btrfs_bio
*bbio
= NULL
;
6372 length
= bio
->bi_iter
.bi_size
;
6373 map_length
= length
;
6375 btrfs_bio_counter_inc_blocked(fs_info
);
6376 ret
= __btrfs_map_block(fs_info
, btrfs_op(bio
), logical
,
6377 &map_length
, &bbio
, mirror_num
, 1);
6379 btrfs_bio_counter_dec(fs_info
);
6380 return errno_to_blk_status(ret
);
6383 total_devs
= bbio
->num_stripes
;
6384 bbio
->orig_bio
= first_bio
;
6385 bbio
->private = first_bio
->bi_private
;
6386 bbio
->end_io
= first_bio
->bi_end_io
;
6387 bbio
->fs_info
= fs_info
;
6388 atomic_set(&bbio
->stripes_pending
, bbio
->num_stripes
);
6390 if ((bbio
->map_type
& BTRFS_BLOCK_GROUP_RAID56_MASK
) &&
6391 ((bio_op(bio
) == REQ_OP_WRITE
) || (mirror_num
> 1))) {
6392 /* In this case, map_length has been set to the length of
6393 a single stripe; not the whole write */
6394 if (bio_op(bio
) == REQ_OP_WRITE
) {
6395 ret
= raid56_parity_write(fs_info
, bio
, bbio
,
6398 ret
= raid56_parity_recover(fs_info
, bio
, bbio
,
6399 map_length
, mirror_num
, 1);
6402 btrfs_bio_counter_dec(fs_info
);
6403 return errno_to_blk_status(ret
);
6406 if (map_length
< length
) {
6408 "mapping failed logical %llu bio len %llu len %llu",
6409 logical
, length
, map_length
);
6413 for (dev_nr
= 0; dev_nr
< total_devs
; dev_nr
++) {
6414 dev
= bbio
->stripes
[dev_nr
].dev
;
6415 if (!dev
|| !dev
->bdev
|| test_bit(BTRFS_DEV_STATE_MISSING
,
6417 (bio_op(first_bio
) == REQ_OP_WRITE
&&
6418 !test_bit(BTRFS_DEV_STATE_WRITEABLE
, &dev
->dev_state
))) {
6419 bbio_error(bbio
, first_bio
, logical
);
6423 if (dev_nr
< total_devs
- 1)
6424 bio
= btrfs_bio_clone(first_bio
);
6428 submit_stripe_bio(bbio
, bio
, bbio
->stripes
[dev_nr
].physical
, dev
);
6430 btrfs_bio_counter_dec(fs_info
);
6435 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6438 * If devid and uuid are both specified, the match must be exact, otherwise
6439 * only devid is used.
6441 * If @seed is true, traverse through the seed devices.
6443 struct btrfs_device
*btrfs_find_device(struct btrfs_fs_devices
*fs_devices
,
6444 u64 devid
, u8
*uuid
, u8
*fsid
,
6447 struct btrfs_device
*device
;
6448 struct btrfs_fs_devices
*seed_devs
;
6450 if (!fsid
|| !memcmp(fs_devices
->metadata_uuid
, fsid
, BTRFS_FSID_SIZE
)) {
6451 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
6452 if (device
->devid
== devid
&&
6453 (!uuid
|| memcmp(device
->uuid
, uuid
,
6454 BTRFS_UUID_SIZE
) == 0))
6459 list_for_each_entry(seed_devs
, &fs_devices
->seed_list
, seed_list
) {
6461 !memcmp(seed_devs
->metadata_uuid
, fsid
, BTRFS_FSID_SIZE
)) {
6462 list_for_each_entry(device
, &seed_devs
->devices
,
6464 if (device
->devid
== devid
&&
6465 (!uuid
|| memcmp(device
->uuid
, uuid
,
6466 BTRFS_UUID_SIZE
) == 0))
6475 static struct btrfs_device
*add_missing_dev(struct btrfs_fs_devices
*fs_devices
,
6476 u64 devid
, u8
*dev_uuid
)
6478 struct btrfs_device
*device
;
6479 unsigned int nofs_flag
;
6482 * We call this under the chunk_mutex, so we want to use NOFS for this
6483 * allocation, however we don't want to change btrfs_alloc_device() to
6484 * always do NOFS because we use it in a lot of other GFP_KERNEL safe
6487 nofs_flag
= memalloc_nofs_save();
6488 device
= btrfs_alloc_device(NULL
, &devid
, dev_uuid
);
6489 memalloc_nofs_restore(nofs_flag
);
6493 list_add(&device
->dev_list
, &fs_devices
->devices
);
6494 device
->fs_devices
= fs_devices
;
6495 fs_devices
->num_devices
++;
6497 set_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
6498 fs_devices
->missing_devices
++;
6504 * btrfs_alloc_device - allocate struct btrfs_device
6505 * @fs_info: used only for generating a new devid, can be NULL if
6506 * devid is provided (i.e. @devid != NULL).
6507 * @devid: a pointer to devid for this device. If NULL a new devid
6509 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6512 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6513 * on error. Returned struct is not linked onto any lists and must be
6514 * destroyed with btrfs_free_device.
6516 struct btrfs_device
*btrfs_alloc_device(struct btrfs_fs_info
*fs_info
,
6520 struct btrfs_device
*dev
;
6523 if (WARN_ON(!devid
&& !fs_info
))
6524 return ERR_PTR(-EINVAL
);
6526 dev
= __alloc_device(fs_info
);
6535 ret
= find_next_devid(fs_info
, &tmp
);
6537 btrfs_free_device(dev
);
6538 return ERR_PTR(ret
);
6544 memcpy(dev
->uuid
, uuid
, BTRFS_UUID_SIZE
);
6546 generate_random_uuid(dev
->uuid
);
6551 static void btrfs_report_missing_device(struct btrfs_fs_info
*fs_info
,
6552 u64 devid
, u8
*uuid
, bool error
)
6555 btrfs_err_rl(fs_info
, "devid %llu uuid %pU is missing",
6558 btrfs_warn_rl(fs_info
, "devid %llu uuid %pU is missing",
6562 static u64
calc_stripe_length(u64 type
, u64 chunk_len
, int num_stripes
)
6564 int index
= btrfs_bg_flags_to_raid_index(type
);
6565 int ncopies
= btrfs_raid_array
[index
].ncopies
;
6566 const int nparity
= btrfs_raid_array
[index
].nparity
;
6570 data_stripes
= num_stripes
- nparity
;
6572 data_stripes
= num_stripes
/ ncopies
;
6574 return div_u64(chunk_len
, data_stripes
);
6577 static int read_one_chunk(struct btrfs_key
*key
, struct extent_buffer
*leaf
,
6578 struct btrfs_chunk
*chunk
)
6580 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
6581 struct extent_map_tree
*map_tree
= &fs_info
->mapping_tree
;
6582 struct map_lookup
*map
;
6583 struct extent_map
*em
;
6587 u8 uuid
[BTRFS_UUID_SIZE
];
6592 logical
= key
->offset
;
6593 length
= btrfs_chunk_length(leaf
, chunk
);
6594 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
6597 * Only need to verify chunk item if we're reading from sys chunk array,
6598 * as chunk item in tree block is already verified by tree-checker.
6600 if (leaf
->start
== BTRFS_SUPER_INFO_OFFSET
) {
6601 ret
= btrfs_check_chunk_valid(leaf
, chunk
, logical
);
6606 read_lock(&map_tree
->lock
);
6607 em
= lookup_extent_mapping(map_tree
, logical
, 1);
6608 read_unlock(&map_tree
->lock
);
6610 /* already mapped? */
6611 if (em
&& em
->start
<= logical
&& em
->start
+ em
->len
> logical
) {
6612 free_extent_map(em
);
6615 free_extent_map(em
);
6618 em
= alloc_extent_map();
6621 map
= kmalloc(map_lookup_size(num_stripes
), GFP_NOFS
);
6623 free_extent_map(em
);
6627 set_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
);
6628 em
->map_lookup
= map
;
6629 em
->start
= logical
;
6632 em
->block_start
= 0;
6633 em
->block_len
= em
->len
;
6635 map
->num_stripes
= num_stripes
;
6636 map
->io_width
= btrfs_chunk_io_width(leaf
, chunk
);
6637 map
->io_align
= btrfs_chunk_io_align(leaf
, chunk
);
6638 map
->stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
6639 map
->type
= btrfs_chunk_type(leaf
, chunk
);
6640 map
->sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
6641 map
->verified_stripes
= 0;
6642 em
->orig_block_len
= calc_stripe_length(map
->type
, em
->len
,
6644 for (i
= 0; i
< num_stripes
; i
++) {
6645 map
->stripes
[i
].physical
=
6646 btrfs_stripe_offset_nr(leaf
, chunk
, i
);
6647 devid
= btrfs_stripe_devid_nr(leaf
, chunk
, i
);
6648 read_extent_buffer(leaf
, uuid
, (unsigned long)
6649 btrfs_stripe_dev_uuid_nr(chunk
, i
),
6651 map
->stripes
[i
].dev
= btrfs_find_device(fs_info
->fs_devices
,
6652 devid
, uuid
, NULL
, true);
6653 if (!map
->stripes
[i
].dev
&&
6654 !btrfs_test_opt(fs_info
, DEGRADED
)) {
6655 free_extent_map(em
);
6656 btrfs_report_missing_device(fs_info
, devid
, uuid
, true);
6659 if (!map
->stripes
[i
].dev
) {
6660 map
->stripes
[i
].dev
=
6661 add_missing_dev(fs_info
->fs_devices
, devid
,
6663 if (IS_ERR(map
->stripes
[i
].dev
)) {
6664 free_extent_map(em
);
6666 "failed to init missing dev %llu: %ld",
6667 devid
, PTR_ERR(map
->stripes
[i
].dev
));
6668 return PTR_ERR(map
->stripes
[i
].dev
);
6670 btrfs_report_missing_device(fs_info
, devid
, uuid
, false);
6672 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
,
6673 &(map
->stripes
[i
].dev
->dev_state
));
6677 write_lock(&map_tree
->lock
);
6678 ret
= add_extent_mapping(map_tree
, em
, 0);
6679 write_unlock(&map_tree
->lock
);
6682 "failed to add chunk map, start=%llu len=%llu: %d",
6683 em
->start
, em
->len
, ret
);
6685 free_extent_map(em
);
6690 static void fill_device_from_item(struct extent_buffer
*leaf
,
6691 struct btrfs_dev_item
*dev_item
,
6692 struct btrfs_device
*device
)
6696 device
->devid
= btrfs_device_id(leaf
, dev_item
);
6697 device
->disk_total_bytes
= btrfs_device_total_bytes(leaf
, dev_item
);
6698 device
->total_bytes
= device
->disk_total_bytes
;
6699 device
->commit_total_bytes
= device
->disk_total_bytes
;
6700 device
->bytes_used
= btrfs_device_bytes_used(leaf
, dev_item
);
6701 device
->commit_bytes_used
= device
->bytes_used
;
6702 device
->type
= btrfs_device_type(leaf
, dev_item
);
6703 device
->io_align
= btrfs_device_io_align(leaf
, dev_item
);
6704 device
->io_width
= btrfs_device_io_width(leaf
, dev_item
);
6705 device
->sector_size
= btrfs_device_sector_size(leaf
, dev_item
);
6706 WARN_ON(device
->devid
== BTRFS_DEV_REPLACE_DEVID
);
6707 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
);
6709 ptr
= btrfs_device_uuid(dev_item
);
6710 read_extent_buffer(leaf
, device
->uuid
, ptr
, BTRFS_UUID_SIZE
);
6713 static struct btrfs_fs_devices
*open_seed_devices(struct btrfs_fs_info
*fs_info
,
6716 struct btrfs_fs_devices
*fs_devices
;
6719 lockdep_assert_held(&uuid_mutex
);
6722 /* This will match only for multi-device seed fs */
6723 list_for_each_entry(fs_devices
, &fs_info
->fs_devices
->seed_list
, seed_list
)
6724 if (!memcmp(fs_devices
->fsid
, fsid
, BTRFS_FSID_SIZE
))
6728 fs_devices
= find_fsid(fsid
, NULL
);
6730 if (!btrfs_test_opt(fs_info
, DEGRADED
))
6731 return ERR_PTR(-ENOENT
);
6733 fs_devices
= alloc_fs_devices(fsid
, NULL
);
6734 if (IS_ERR(fs_devices
))
6737 fs_devices
->seeding
= true;
6738 fs_devices
->opened
= 1;
6743 * Upon first call for a seed fs fsid, just create a private copy of the
6744 * respective fs_devices and anchor it at fs_info->fs_devices->seed_list
6746 fs_devices
= clone_fs_devices(fs_devices
);
6747 if (IS_ERR(fs_devices
))
6750 ret
= open_fs_devices(fs_devices
, FMODE_READ
, fs_info
->bdev_holder
);
6752 free_fs_devices(fs_devices
);
6753 return ERR_PTR(ret
);
6756 if (!fs_devices
->seeding
) {
6757 close_fs_devices(fs_devices
);
6758 free_fs_devices(fs_devices
);
6759 return ERR_PTR(-EINVAL
);
6762 list_add(&fs_devices
->seed_list
, &fs_info
->fs_devices
->seed_list
);
6767 static int read_one_dev(struct extent_buffer
*leaf
,
6768 struct btrfs_dev_item
*dev_item
)
6770 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
6771 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
6772 struct btrfs_device
*device
;
6775 u8 fs_uuid
[BTRFS_FSID_SIZE
];
6776 u8 dev_uuid
[BTRFS_UUID_SIZE
];
6778 devid
= btrfs_device_id(leaf
, dev_item
);
6779 read_extent_buffer(leaf
, dev_uuid
, btrfs_device_uuid(dev_item
),
6781 read_extent_buffer(leaf
, fs_uuid
, btrfs_device_fsid(dev_item
),
6784 if (memcmp(fs_uuid
, fs_devices
->metadata_uuid
, BTRFS_FSID_SIZE
)) {
6785 fs_devices
= open_seed_devices(fs_info
, fs_uuid
);
6786 if (IS_ERR(fs_devices
))
6787 return PTR_ERR(fs_devices
);
6790 device
= btrfs_find_device(fs_info
->fs_devices
, devid
, dev_uuid
,
6793 if (!btrfs_test_opt(fs_info
, DEGRADED
)) {
6794 btrfs_report_missing_device(fs_info
, devid
,
6799 device
= add_missing_dev(fs_devices
, devid
, dev_uuid
);
6800 if (IS_ERR(device
)) {
6802 "failed to add missing dev %llu: %ld",
6803 devid
, PTR_ERR(device
));
6804 return PTR_ERR(device
);
6806 btrfs_report_missing_device(fs_info
, devid
, dev_uuid
, false);
6808 if (!device
->bdev
) {
6809 if (!btrfs_test_opt(fs_info
, DEGRADED
)) {
6810 btrfs_report_missing_device(fs_info
,
6811 devid
, dev_uuid
, true);
6814 btrfs_report_missing_device(fs_info
, devid
,
6818 if (!device
->bdev
&&
6819 !test_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
)) {
6821 * this happens when a device that was properly setup
6822 * in the device info lists suddenly goes bad.
6823 * device->bdev is NULL, and so we have to set
6824 * device->missing to one here
6826 device
->fs_devices
->missing_devices
++;
6827 set_bit(BTRFS_DEV_STATE_MISSING
, &device
->dev_state
);
6830 /* Move the device to its own fs_devices */
6831 if (device
->fs_devices
!= fs_devices
) {
6832 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING
,
6833 &device
->dev_state
));
6835 list_move(&device
->dev_list
, &fs_devices
->devices
);
6836 device
->fs_devices
->num_devices
--;
6837 fs_devices
->num_devices
++;
6839 device
->fs_devices
->missing_devices
--;
6840 fs_devices
->missing_devices
++;
6842 device
->fs_devices
= fs_devices
;
6846 if (device
->fs_devices
!= fs_info
->fs_devices
) {
6847 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
));
6848 if (device
->generation
!=
6849 btrfs_device_generation(leaf
, dev_item
))
6853 fill_device_from_item(leaf
, dev_item
, device
);
6854 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA
, &device
->dev_state
);
6855 if (test_bit(BTRFS_DEV_STATE_WRITEABLE
, &device
->dev_state
) &&
6856 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT
, &device
->dev_state
)) {
6857 device
->fs_devices
->total_rw_bytes
+= device
->total_bytes
;
6858 atomic64_add(device
->total_bytes
- device
->bytes_used
,
6859 &fs_info
->free_chunk_space
);
6865 int btrfs_read_sys_array(struct btrfs_fs_info
*fs_info
)
6867 struct btrfs_root
*root
= fs_info
->tree_root
;
6868 struct btrfs_super_block
*super_copy
= fs_info
->super_copy
;
6869 struct extent_buffer
*sb
;
6870 struct btrfs_disk_key
*disk_key
;
6871 struct btrfs_chunk
*chunk
;
6873 unsigned long sb_array_offset
;
6880 struct btrfs_key key
;
6882 ASSERT(BTRFS_SUPER_INFO_SIZE
<= fs_info
->nodesize
);
6884 * This will create extent buffer of nodesize, superblock size is
6885 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6886 * overallocate but we can keep it as-is, only the first page is used.
6888 sb
= btrfs_find_create_tree_block(fs_info
, BTRFS_SUPER_INFO_OFFSET
);
6891 set_extent_buffer_uptodate(sb
);
6892 btrfs_set_buffer_lockdep_class(root
->root_key
.objectid
, sb
, 0);
6894 * The sb extent buffer is artificial and just used to read the system array.
6895 * set_extent_buffer_uptodate() call does not properly mark all it's
6896 * pages up-to-date when the page is larger: extent does not cover the
6897 * whole page and consequently check_page_uptodate does not find all
6898 * the page's extents up-to-date (the hole beyond sb),
6899 * write_extent_buffer then triggers a WARN_ON.
6901 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6902 * but sb spans only this function. Add an explicit SetPageUptodate call
6903 * to silence the warning eg. on PowerPC 64.
6905 if (PAGE_SIZE
> BTRFS_SUPER_INFO_SIZE
)
6906 SetPageUptodate(sb
->pages
[0]);
6908 write_extent_buffer(sb
, super_copy
, 0, BTRFS_SUPER_INFO_SIZE
);
6909 array_size
= btrfs_super_sys_array_size(super_copy
);
6911 array_ptr
= super_copy
->sys_chunk_array
;
6912 sb_array_offset
= offsetof(struct btrfs_super_block
, sys_chunk_array
);
6915 while (cur_offset
< array_size
) {
6916 disk_key
= (struct btrfs_disk_key
*)array_ptr
;
6917 len
= sizeof(*disk_key
);
6918 if (cur_offset
+ len
> array_size
)
6919 goto out_short_read
;
6921 btrfs_disk_key_to_cpu(&key
, disk_key
);
6924 sb_array_offset
+= len
;
6927 if (key
.type
!= BTRFS_CHUNK_ITEM_KEY
) {
6929 "unexpected item type %u in sys_array at offset %u",
6930 (u32
)key
.type
, cur_offset
);
6935 chunk
= (struct btrfs_chunk
*)sb_array_offset
;
6937 * At least one btrfs_chunk with one stripe must be present,
6938 * exact stripe count check comes afterwards
6940 len
= btrfs_chunk_item_size(1);
6941 if (cur_offset
+ len
> array_size
)
6942 goto out_short_read
;
6944 num_stripes
= btrfs_chunk_num_stripes(sb
, chunk
);
6947 "invalid number of stripes %u in sys_array at offset %u",
6948 num_stripes
, cur_offset
);
6953 type
= btrfs_chunk_type(sb
, chunk
);
6954 if ((type
& BTRFS_BLOCK_GROUP_SYSTEM
) == 0) {
6956 "invalid chunk type %llu in sys_array at offset %u",
6962 len
= btrfs_chunk_item_size(num_stripes
);
6963 if (cur_offset
+ len
> array_size
)
6964 goto out_short_read
;
6966 ret
= read_one_chunk(&key
, sb
, chunk
);
6971 sb_array_offset
+= len
;
6974 clear_extent_buffer_uptodate(sb
);
6975 free_extent_buffer_stale(sb
);
6979 btrfs_err(fs_info
, "sys_array too short to read %u bytes at offset %u",
6981 clear_extent_buffer_uptodate(sb
);
6982 free_extent_buffer_stale(sb
);
6987 * Check if all chunks in the fs are OK for read-write degraded mount
6989 * If the @failing_dev is specified, it's accounted as missing.
6991 * Return true if all chunks meet the minimal RW mount requirements.
6992 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6994 bool btrfs_check_rw_degradable(struct btrfs_fs_info
*fs_info
,
6995 struct btrfs_device
*failing_dev
)
6997 struct extent_map_tree
*map_tree
= &fs_info
->mapping_tree
;
6998 struct extent_map
*em
;
7002 read_lock(&map_tree
->lock
);
7003 em
= lookup_extent_mapping(map_tree
, 0, (u64
)-1);
7004 read_unlock(&map_tree
->lock
);
7005 /* No chunk at all? Return false anyway */
7011 struct map_lookup
*map
;
7016 map
= em
->map_lookup
;
7018 btrfs_get_num_tolerated_disk_barrier_failures(
7020 for (i
= 0; i
< map
->num_stripes
; i
++) {
7021 struct btrfs_device
*dev
= map
->stripes
[i
].dev
;
7023 if (!dev
|| !dev
->bdev
||
7024 test_bit(BTRFS_DEV_STATE_MISSING
, &dev
->dev_state
) ||
7025 dev
->last_flush_error
)
7027 else if (failing_dev
&& failing_dev
== dev
)
7030 if (missing
> max_tolerated
) {
7033 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
7034 em
->start
, missing
, max_tolerated
);
7035 free_extent_map(em
);
7039 next_start
= extent_map_end(em
);
7040 free_extent_map(em
);
7042 read_lock(&map_tree
->lock
);
7043 em
= lookup_extent_mapping(map_tree
, next_start
,
7044 (u64
)(-1) - next_start
);
7045 read_unlock(&map_tree
->lock
);
7051 static void readahead_tree_node_children(struct extent_buffer
*node
)
7054 const int nr_items
= btrfs_header_nritems(node
);
7056 for (i
= 0; i
< nr_items
; i
++) {
7059 start
= btrfs_node_blockptr(node
, i
);
7060 readahead_tree_block(node
->fs_info
, start
);
7064 int btrfs_read_chunk_tree(struct btrfs_fs_info
*fs_info
)
7066 struct btrfs_root
*root
= fs_info
->chunk_root
;
7067 struct btrfs_path
*path
;
7068 struct extent_buffer
*leaf
;
7069 struct btrfs_key key
;
7070 struct btrfs_key found_key
;
7074 u64 last_ra_node
= 0;
7076 path
= btrfs_alloc_path();
7081 * uuid_mutex is needed only if we are mounting a sprout FS
7082 * otherwise we don't need it.
7084 mutex_lock(&uuid_mutex
);
7087 * It is possible for mount and umount to race in such a way that
7088 * we execute this code path, but open_fs_devices failed to clear
7089 * total_rw_bytes. We certainly want it cleared before reading the
7090 * device items, so clear it here.
7092 fs_info
->fs_devices
->total_rw_bytes
= 0;
7095 * Read all device items, and then all the chunk items. All
7096 * device items are found before any chunk item (their object id
7097 * is smaller than the lowest possible object id for a chunk
7098 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
7100 key
.objectid
= BTRFS_DEV_ITEMS_OBJECTID
;
7103 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7107 struct extent_buffer
*node
;
7109 leaf
= path
->nodes
[0];
7110 slot
= path
->slots
[0];
7111 if (slot
>= btrfs_header_nritems(leaf
)) {
7112 ret
= btrfs_next_leaf(root
, path
);
7120 * The nodes on level 1 are not locked but we don't need to do
7121 * that during mount time as nothing else can access the tree
7123 node
= path
->nodes
[1];
7125 if (last_ra_node
!= node
->start
) {
7126 readahead_tree_node_children(node
);
7127 last_ra_node
= node
->start
;
7130 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
7131 if (found_key
.type
== BTRFS_DEV_ITEM_KEY
) {
7132 struct btrfs_dev_item
*dev_item
;
7133 dev_item
= btrfs_item_ptr(leaf
, slot
,
7134 struct btrfs_dev_item
);
7135 ret
= read_one_dev(leaf
, dev_item
);
7139 } else if (found_key
.type
== BTRFS_CHUNK_ITEM_KEY
) {
7140 struct btrfs_chunk
*chunk
;
7141 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
7142 mutex_lock(&fs_info
->chunk_mutex
);
7143 ret
= read_one_chunk(&found_key
, leaf
, chunk
);
7144 mutex_unlock(&fs_info
->chunk_mutex
);
7152 * After loading chunk tree, we've got all device information,
7153 * do another round of validation checks.
7155 if (total_dev
!= fs_info
->fs_devices
->total_devices
) {
7157 "super_num_devices %llu mismatch with num_devices %llu found here",
7158 btrfs_super_num_devices(fs_info
->super_copy
),
7163 if (btrfs_super_total_bytes(fs_info
->super_copy
) <
7164 fs_info
->fs_devices
->total_rw_bytes
) {
7166 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
7167 btrfs_super_total_bytes(fs_info
->super_copy
),
7168 fs_info
->fs_devices
->total_rw_bytes
);
7174 mutex_unlock(&uuid_mutex
);
7176 btrfs_free_path(path
);
7180 void btrfs_init_devices_late(struct btrfs_fs_info
*fs_info
)
7182 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
, *seed_devs
;
7183 struct btrfs_device
*device
;
7185 fs_devices
->fs_info
= fs_info
;
7187 mutex_lock(&fs_devices
->device_list_mutex
);
7188 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
7189 device
->fs_info
= fs_info
;
7191 list_for_each_entry(seed_devs
, &fs_devices
->seed_list
, seed_list
) {
7192 list_for_each_entry(device
, &seed_devs
->devices
, dev_list
)
7193 device
->fs_info
= fs_info
;
7195 seed_devs
->fs_info
= fs_info
;
7197 mutex_unlock(&fs_devices
->device_list_mutex
);
7200 static u64
btrfs_dev_stats_value(const struct extent_buffer
*eb
,
7201 const struct btrfs_dev_stats_item
*ptr
,
7206 read_extent_buffer(eb
, &val
,
7207 offsetof(struct btrfs_dev_stats_item
, values
) +
7208 ((unsigned long)ptr
) + (index
* sizeof(u64
)),
7213 static void btrfs_set_dev_stats_value(struct extent_buffer
*eb
,
7214 struct btrfs_dev_stats_item
*ptr
,
7217 write_extent_buffer(eb
, &val
,
7218 offsetof(struct btrfs_dev_stats_item
, values
) +
7219 ((unsigned long)ptr
) + (index
* sizeof(u64
)),
7223 static int btrfs_device_init_dev_stats(struct btrfs_device
*device
,
7224 struct btrfs_path
*path
)
7226 struct btrfs_dev_stats_item
*ptr
;
7227 struct extent_buffer
*eb
;
7228 struct btrfs_key key
;
7232 key
.objectid
= BTRFS_DEV_STATS_OBJECTID
;
7233 key
.type
= BTRFS_PERSISTENT_ITEM_KEY
;
7234 key
.offset
= device
->devid
;
7235 ret
= btrfs_search_slot(NULL
, device
->fs_info
->dev_root
, &key
, path
, 0, 0);
7237 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7238 btrfs_dev_stat_set(device
, i
, 0);
7239 device
->dev_stats_valid
= 1;
7240 btrfs_release_path(path
);
7241 return ret
< 0 ? ret
: 0;
7243 slot
= path
->slots
[0];
7244 eb
= path
->nodes
[0];
7245 item_size
= btrfs_item_size_nr(eb
, slot
);
7247 ptr
= btrfs_item_ptr(eb
, slot
, struct btrfs_dev_stats_item
);
7249 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
7250 if (item_size
>= (1 + i
) * sizeof(__le64
))
7251 btrfs_dev_stat_set(device
, i
,
7252 btrfs_dev_stats_value(eb
, ptr
, i
));
7254 btrfs_dev_stat_set(device
, i
, 0);
7257 device
->dev_stats_valid
= 1;
7258 btrfs_dev_stat_print_on_load(device
);
7259 btrfs_release_path(path
);
7264 int btrfs_init_dev_stats(struct btrfs_fs_info
*fs_info
)
7266 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
, *seed_devs
;
7267 struct btrfs_device
*device
;
7268 struct btrfs_path
*path
= NULL
;
7271 path
= btrfs_alloc_path();
7275 mutex_lock(&fs_devices
->device_list_mutex
);
7276 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
7277 ret
= btrfs_device_init_dev_stats(device
, path
);
7281 list_for_each_entry(seed_devs
, &fs_devices
->seed_list
, seed_list
) {
7282 list_for_each_entry(device
, &seed_devs
->devices
, dev_list
) {
7283 ret
= btrfs_device_init_dev_stats(device
, path
);
7289 mutex_unlock(&fs_devices
->device_list_mutex
);
7291 btrfs_free_path(path
);
7295 static int update_dev_stat_item(struct btrfs_trans_handle
*trans
,
7296 struct btrfs_device
*device
)
7298 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
7299 struct btrfs_root
*dev_root
= fs_info
->dev_root
;
7300 struct btrfs_path
*path
;
7301 struct btrfs_key key
;
7302 struct extent_buffer
*eb
;
7303 struct btrfs_dev_stats_item
*ptr
;
7307 key
.objectid
= BTRFS_DEV_STATS_OBJECTID
;
7308 key
.type
= BTRFS_PERSISTENT_ITEM_KEY
;
7309 key
.offset
= device
->devid
;
7311 path
= btrfs_alloc_path();
7314 ret
= btrfs_search_slot(trans
, dev_root
, &key
, path
, -1, 1);
7316 btrfs_warn_in_rcu(fs_info
,
7317 "error %d while searching for dev_stats item for device %s",
7318 ret
, rcu_str_deref(device
->name
));
7323 btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]) < sizeof(*ptr
)) {
7324 /* need to delete old one and insert a new one */
7325 ret
= btrfs_del_item(trans
, dev_root
, path
);
7327 btrfs_warn_in_rcu(fs_info
,
7328 "delete too small dev_stats item for device %s failed %d",
7329 rcu_str_deref(device
->name
), ret
);
7336 /* need to insert a new item */
7337 btrfs_release_path(path
);
7338 ret
= btrfs_insert_empty_item(trans
, dev_root
, path
,
7339 &key
, sizeof(*ptr
));
7341 btrfs_warn_in_rcu(fs_info
,
7342 "insert dev_stats item for device %s failed %d",
7343 rcu_str_deref(device
->name
), ret
);
7348 eb
= path
->nodes
[0];
7349 ptr
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_dev_stats_item
);
7350 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7351 btrfs_set_dev_stats_value(eb
, ptr
, i
,
7352 btrfs_dev_stat_read(device
, i
));
7353 btrfs_mark_buffer_dirty(eb
);
7356 btrfs_free_path(path
);
7361 * called from commit_transaction. Writes all changed device stats to disk.
7363 int btrfs_run_dev_stats(struct btrfs_trans_handle
*trans
)
7365 struct btrfs_fs_info
*fs_info
= trans
->fs_info
;
7366 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7367 struct btrfs_device
*device
;
7371 mutex_lock(&fs_devices
->device_list_mutex
);
7372 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
7373 stats_cnt
= atomic_read(&device
->dev_stats_ccnt
);
7374 if (!device
->dev_stats_valid
|| stats_cnt
== 0)
7379 * There is a LOAD-LOAD control dependency between the value of
7380 * dev_stats_ccnt and updating the on-disk values which requires
7381 * reading the in-memory counters. Such control dependencies
7382 * require explicit read memory barriers.
7384 * This memory barriers pairs with smp_mb__before_atomic in
7385 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7386 * barrier implied by atomic_xchg in
7387 * btrfs_dev_stats_read_and_reset
7391 ret
= update_dev_stat_item(trans
, device
);
7393 atomic_sub(stats_cnt
, &device
->dev_stats_ccnt
);
7395 mutex_unlock(&fs_devices
->device_list_mutex
);
7400 void btrfs_dev_stat_inc_and_print(struct btrfs_device
*dev
, int index
)
7402 btrfs_dev_stat_inc(dev
, index
);
7403 btrfs_dev_stat_print_on_error(dev
);
7406 static void btrfs_dev_stat_print_on_error(struct btrfs_device
*dev
)
7408 if (!dev
->dev_stats_valid
)
7410 btrfs_err_rl_in_rcu(dev
->fs_info
,
7411 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7412 rcu_str_deref(dev
->name
),
7413 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
7414 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
7415 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
7416 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
7417 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
7420 static void btrfs_dev_stat_print_on_load(struct btrfs_device
*dev
)
7424 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7425 if (btrfs_dev_stat_read(dev
, i
) != 0)
7427 if (i
== BTRFS_DEV_STAT_VALUES_MAX
)
7428 return; /* all values == 0, suppress message */
7430 btrfs_info_in_rcu(dev
->fs_info
,
7431 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
7432 rcu_str_deref(dev
->name
),
7433 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_WRITE_ERRS
),
7434 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_READ_ERRS
),
7435 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_FLUSH_ERRS
),
7436 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_CORRUPTION_ERRS
),
7437 btrfs_dev_stat_read(dev
, BTRFS_DEV_STAT_GENERATION_ERRS
));
7440 int btrfs_get_dev_stats(struct btrfs_fs_info
*fs_info
,
7441 struct btrfs_ioctl_get_dev_stats
*stats
)
7443 struct btrfs_device
*dev
;
7444 struct btrfs_fs_devices
*fs_devices
= fs_info
->fs_devices
;
7447 mutex_lock(&fs_devices
->device_list_mutex
);
7448 dev
= btrfs_find_device(fs_info
->fs_devices
, stats
->devid
, NULL
, NULL
,
7450 mutex_unlock(&fs_devices
->device_list_mutex
);
7453 btrfs_warn(fs_info
, "get dev_stats failed, device not found");
7455 } else if (!dev
->dev_stats_valid
) {
7456 btrfs_warn(fs_info
, "get dev_stats failed, not yet valid");
7458 } else if (stats
->flags
& BTRFS_DEV_STATS_RESET
) {
7459 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++) {
7460 if (stats
->nr_items
> i
)
7462 btrfs_dev_stat_read_and_reset(dev
, i
);
7464 btrfs_dev_stat_set(dev
, i
, 0);
7466 btrfs_info(fs_info
, "device stats zeroed by %s (%d)",
7467 current
->comm
, task_pid_nr(current
));
7469 for (i
= 0; i
< BTRFS_DEV_STAT_VALUES_MAX
; i
++)
7470 if (stats
->nr_items
> i
)
7471 stats
->values
[i
] = btrfs_dev_stat_read(dev
, i
);
7473 if (stats
->nr_items
> BTRFS_DEV_STAT_VALUES_MAX
)
7474 stats
->nr_items
= BTRFS_DEV_STAT_VALUES_MAX
;
7479 * Update the size and bytes used for each device where it changed. This is
7480 * delayed since we would otherwise get errors while writing out the
7483 * Must be invoked during transaction commit.
7485 void btrfs_commit_device_sizes(struct btrfs_transaction
*trans
)
7487 struct btrfs_device
*curr
, *next
;
7489 ASSERT(trans
->state
== TRANS_STATE_COMMIT_DOING
);
7491 if (list_empty(&trans
->dev_update_list
))
7495 * We don't need the device_list_mutex here. This list is owned by the
7496 * transaction and the transaction must complete before the device is
7499 mutex_lock(&trans
->fs_info
->chunk_mutex
);
7500 list_for_each_entry_safe(curr
, next
, &trans
->dev_update_list
,
7502 list_del_init(&curr
->post_commit_list
);
7503 curr
->commit_total_bytes
= curr
->disk_total_bytes
;
7504 curr
->commit_bytes_used
= curr
->bytes_used
;
7506 mutex_unlock(&trans
->fs_info
->chunk_mutex
);
7510 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7512 int btrfs_bg_type_to_factor(u64 flags
)
7514 const int index
= btrfs_bg_flags_to_raid_index(flags
);
7516 return btrfs_raid_array
[index
].ncopies
;
7521 static int verify_one_dev_extent(struct btrfs_fs_info
*fs_info
,
7522 u64 chunk_offset
, u64 devid
,
7523 u64 physical_offset
, u64 physical_len
)
7525 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
;
7526 struct extent_map
*em
;
7527 struct map_lookup
*map
;
7528 struct btrfs_device
*dev
;
7534 read_lock(&em_tree
->lock
);
7535 em
= lookup_extent_mapping(em_tree
, chunk_offset
, 1);
7536 read_unlock(&em_tree
->lock
);
7540 "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7541 physical_offset
, devid
);
7546 map
= em
->map_lookup
;
7547 stripe_len
= calc_stripe_length(map
->type
, em
->len
, map
->num_stripes
);
7548 if (physical_len
!= stripe_len
) {
7550 "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7551 physical_offset
, devid
, em
->start
, physical_len
,
7557 for (i
= 0; i
< map
->num_stripes
; i
++) {
7558 if (map
->stripes
[i
].dev
->devid
== devid
&&
7559 map
->stripes
[i
].physical
== physical_offset
) {
7561 if (map
->verified_stripes
>= map
->num_stripes
) {
7563 "too many dev extents for chunk %llu found",
7568 map
->verified_stripes
++;
7574 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7575 physical_offset
, devid
);
7579 /* Make sure no dev extent is beyond device bondary */
7580 dev
= btrfs_find_device(fs_info
->fs_devices
, devid
, NULL
, NULL
, true);
7582 btrfs_err(fs_info
, "failed to find devid %llu", devid
);
7587 /* It's possible this device is a dummy for seed device */
7588 if (dev
->disk_total_bytes
== 0) {
7589 struct btrfs_fs_devices
*devs
;
7591 devs
= list_first_entry(&fs_info
->fs_devices
->seed_list
,
7592 struct btrfs_fs_devices
, seed_list
);
7593 dev
= btrfs_find_device(devs
, devid
, NULL
, NULL
, false);
7595 btrfs_err(fs_info
, "failed to find seed devid %llu",
7602 if (physical_offset
+ physical_len
> dev
->disk_total_bytes
) {
7604 "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7605 devid
, physical_offset
, physical_len
,
7606 dev
->disk_total_bytes
);
7611 free_extent_map(em
);
7615 static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info
*fs_info
)
7617 struct extent_map_tree
*em_tree
= &fs_info
->mapping_tree
;
7618 struct extent_map
*em
;
7619 struct rb_node
*node
;
7622 read_lock(&em_tree
->lock
);
7623 for (node
= rb_first_cached(&em_tree
->map
); node
; node
= rb_next(node
)) {
7624 em
= rb_entry(node
, struct extent_map
, rb_node
);
7625 if (em
->map_lookup
->num_stripes
!=
7626 em
->map_lookup
->verified_stripes
) {
7628 "chunk %llu has missing dev extent, have %d expect %d",
7629 em
->start
, em
->map_lookup
->verified_stripes
,
7630 em
->map_lookup
->num_stripes
);
7636 read_unlock(&em_tree
->lock
);
7641 * Ensure that all dev extents are mapped to correct chunk, otherwise
7642 * later chunk allocation/free would cause unexpected behavior.
7644 * NOTE: This will iterate through the whole device tree, which should be of
7645 * the same size level as the chunk tree. This slightly increases mount time.
7647 int btrfs_verify_dev_extents(struct btrfs_fs_info
*fs_info
)
7649 struct btrfs_path
*path
;
7650 struct btrfs_root
*root
= fs_info
->dev_root
;
7651 struct btrfs_key key
;
7653 u64 prev_dev_ext_end
= 0;
7657 key
.type
= BTRFS_DEV_EXTENT_KEY
;
7660 path
= btrfs_alloc_path();
7664 path
->reada
= READA_FORWARD
;
7665 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
7669 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
7670 ret
= btrfs_next_item(root
, path
);
7673 /* No dev extents at all? Not good */
7680 struct extent_buffer
*leaf
= path
->nodes
[0];
7681 struct btrfs_dev_extent
*dext
;
7682 int slot
= path
->slots
[0];
7684 u64 physical_offset
;
7688 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7689 if (key
.type
!= BTRFS_DEV_EXTENT_KEY
)
7691 devid
= key
.objectid
;
7692 physical_offset
= key
.offset
;
7694 dext
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dev_extent
);
7695 chunk_offset
= btrfs_dev_extent_chunk_offset(leaf
, dext
);
7696 physical_len
= btrfs_dev_extent_length(leaf
, dext
);
7698 /* Check if this dev extent overlaps with the previous one */
7699 if (devid
== prev_devid
&& physical_offset
< prev_dev_ext_end
) {
7701 "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7702 devid
, physical_offset
, prev_dev_ext_end
);
7707 ret
= verify_one_dev_extent(fs_info
, chunk_offset
, devid
,
7708 physical_offset
, physical_len
);
7712 prev_dev_ext_end
= physical_offset
+ physical_len
;
7714 ret
= btrfs_next_item(root
, path
);
7723 /* Ensure all chunks have corresponding dev extents */
7724 ret
= verify_chunk_dev_extent_mapping(fs_info
);
7726 btrfs_free_path(path
);
7731 * Check whether the given block group or device is pinned by any inode being
7732 * used as a swapfile.
7734 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info
*fs_info
, void *ptr
)
7736 struct btrfs_swapfile_pin
*sp
;
7737 struct rb_node
*node
;
7739 spin_lock(&fs_info
->swapfile_pins_lock
);
7740 node
= fs_info
->swapfile_pins
.rb_node
;
7742 sp
= rb_entry(node
, struct btrfs_swapfile_pin
, node
);
7744 node
= node
->rb_left
;
7745 else if (ptr
> sp
->ptr
)
7746 node
= node
->rb_right
;
7750 spin_unlock(&fs_info
->swapfile_pins_lock
);
7751 return node
!= NULL
;