Skip to content

Commit 09ba3bc

Browse files
asjkdave
authored andcommitted
btrfs: merge btrfs_find_device and find_device
Both btrfs_find_device() and find_device() does the same thing except that the latter does not take the seed device onto account in the device scanning context. We can merge them. Signed-off-by: Anand Jain <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 70bc708 commit 09ba3bc

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

fs/btrfs/dev-replace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
112112
case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
113113
case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
114114
dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
115-
src_devid, NULL, NULL);
115+
src_devid, NULL, NULL, true);
116116
dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
117117
BTRFS_DEV_REPLACE_DEVID,
118-
NULL, NULL);
118+
NULL, NULL, true);
119119
/*
120120
* allow 'btrfs dev replace_cancel' if src/tgt device is
121121
* missing

fs/btrfs/ioctl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
16421642
btrfs_info(fs_info, "resizing devid %llu", devid);
16431643
}
16441644

1645-
device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
1645+
device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
16461646
if (!device) {
16471647
btrfs_info(fs_info, "resizer unable to find device %llu",
16481648
devid);
@@ -3179,7 +3179,7 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
31793179

31803180
rcu_read_lock();
31813181
dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3182-
NULL);
3182+
NULL, true);
31833183

31843184
if (!dev) {
31853185
ret = -ENODEV;

fs/btrfs/scrub.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3835,7 +3835,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
38353835
return PTR_ERR(sctx);
38363836

38373837
mutex_lock(&fs_info->fs_devices->device_list_mutex);
3838-
dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
3838+
dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
38393839
if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) &&
38403840
!is_dev_replace)) {
38413841
mutex_unlock(&fs_info->fs_devices->device_list_mutex);
@@ -4012,7 +4012,7 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
40124012
struct scrub_ctx *sctx = NULL;
40134013

40144014
mutex_lock(&fs_info->fs_devices->device_list_mutex);
4015-
dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
4015+
dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
40164016
if (dev)
40174017
sctx = dev->scrub_ctx;
40184018
if (sctx)

fs/btrfs/volumes.c

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -415,27 +415,6 @@ static struct btrfs_device *__alloc_device(void)
415415
return dev;
416416
}
417417

418-
/*
419-
* Find a device specified by @devid or @uuid in the list of @fs_devices, or
420-
* return NULL.
421-
*
422-
* If devid and uuid are both specified, the match must be exact, otherwise
423-
* only devid is used.
424-
*/
425-
static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
426-
u64 devid, const u8 *uuid)
427-
{
428-
struct btrfs_device *dev;
429-
430-
list_for_each_entry(dev, &fs_devices->devices, dev_list) {
431-
if (dev->devid == devid &&
432-
(!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
433-
return dev;
434-
}
435-
}
436-
return NULL;
437-
}
438-
439418
static noinline struct btrfs_fs_devices *find_fsid(
440419
const u8 *fsid, const u8 *metadata_fsid)
441420
{
@@ -984,8 +963,8 @@ static noinline struct btrfs_device *device_list_add(const char *path,
984963
device = NULL;
985964
} else {
986965
mutex_lock(&fs_devices->device_list_mutex);
987-
device = find_device(fs_devices, devid,
988-
disk_super->dev_item.uuid);
966+
device = btrfs_find_device(fs_devices, devid,
967+
disk_super->dev_item.uuid, NULL, false);
989968

990969
/*
991970
* If this disk has been pulled into an fs devices created by
@@ -2402,10 +2381,10 @@ static struct btrfs_device *btrfs_find_device_by_path(
24022381
dev_uuid = disk_super->dev_item.uuid;
24032382
if (btrfs_fs_incompat(fs_info, METADATA_UUID))
24042383
device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2405-
disk_super->metadata_uuid);
2384+
disk_super->metadata_uuid, true);
24062385
else
24072386
device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2408-
disk_super->fsid);
2387+
disk_super->fsid, true);
24092388

24102389
brelse(bh);
24112390
if (!device)
@@ -2425,7 +2404,7 @@ struct btrfs_device *btrfs_find_device_by_devspec(
24252404

24262405
if (devid) {
24272406
device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
2428-
NULL);
2407+
NULL, true);
24292408
if (!device)
24302409
return ERR_PTR(-ENOENT);
24312410
return device;
@@ -2568,7 +2547,7 @@ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
25682547
read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
25692548
BTRFS_FSID_SIZE);
25702549
device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2571-
fs_uuid);
2550+
fs_uuid, true);
25722551
BUG_ON(!device); /* Logic error */
25732552

25742553
if (device->fs_devices->seeding) {
@@ -6621,19 +6600,36 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
66216600
return BLK_STS_OK;
66226601
}
66236602

6603+
/*
6604+
* Find a device specified by @devid or @uuid in the list of @fs_devices, or
6605+
* return NULL.
6606+
*
6607+
* If devid and uuid are both specified, the match must be exact, otherwise
6608+
* only devid is used.
6609+
*
6610+
* If @seed is true, traverse through the seed devices.
6611+
*/
66246612
struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
6625-
u64 devid, u8 *uuid, u8 *fsid)
6613+
u64 devid, u8 *uuid, u8 *fsid,
6614+
bool seed)
66266615
{
66276616
struct btrfs_device *device;
66286617

66296618
while (fs_devices) {
66306619
if (!fsid ||
66316620
!memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6632-
device = find_device(fs_devices, devid, uuid);
6633-
if (device)
6634-
return device;
6621+
list_for_each_entry(device, &fs_devices->devices,
6622+
dev_list) {
6623+
if (device->devid == devid &&
6624+
(!uuid || memcmp(device->uuid, uuid,
6625+
BTRFS_UUID_SIZE) == 0))
6626+
return device;
6627+
}
66356628
}
6636-
fs_devices = fs_devices->seed;
6629+
if (seed)
6630+
fs_devices = fs_devices->seed;
6631+
else
6632+
return NULL;
66376633
}
66386634
return NULL;
66396635
}
@@ -6879,7 +6875,7 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
68796875
btrfs_stripe_dev_uuid_nr(chunk, i),
68806876
BTRFS_UUID_SIZE);
68816877
map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
6882-
devid, uuid, NULL);
6878+
devid, uuid, NULL, true);
68836879
if (!map->stripes[i].dev &&
68846880
!btrfs_test_opt(fs_info, DEGRADED)) {
68856881
free_extent_map(em);
@@ -7019,7 +7015,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
70197015
}
70207016

70217017
device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
7022-
fs_uuid);
7018+
fs_uuid, true);
70237019
if (!device) {
70247020
if (!btrfs_test_opt(fs_info, DEGRADED)) {
70257021
btrfs_report_missing_device(fs_info, devid,
@@ -7609,7 +7605,8 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
76097605
int i;
76107606

76117607
mutex_lock(&fs_devices->device_list_mutex);
7612-
dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL);
7608+
dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7609+
true);
76137610
mutex_unlock(&fs_devices->device_list_mutex);
76147611

76157612
if (!dev) {
@@ -7823,7 +7820,7 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
78237820
}
78247821

78257822
/* Make sure no dev extent is beyond device bondary */
7826-
dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
7823+
dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
78277824
if (!dev) {
78287825
btrfs_err(fs_info, "failed to find devid %llu", devid);
78297826
ret = -EUCLEAN;
@@ -7832,7 +7829,8 @@ static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
78327829

78337830
/* It's possible this device is a dummy for seed device */
78347831
if (dev->disk_total_bytes == 0) {
7835-
dev = find_device(fs_info->fs_devices->seed, devid, NULL);
7832+
dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7833+
NULL, false);
78367834
if (!dev) {
78377835
btrfs_err(fs_info, "failed to find seed devid %llu",
78387836
devid);

fs/btrfs/volumes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
434434
int btrfs_grow_device(struct btrfs_trans_handle *trans,
435435
struct btrfs_device *device, u64 new_size);
436436
struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
437-
u64 devid, u8 *uuid, u8 *fsid);
437+
u64 devid, u8 *uuid, u8 *fsid, bool seed);
438438
int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
439439
int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
440440
int btrfs_balance(struct btrfs_fs_info *fs_info,

0 commit comments

Comments
 (0)