Skip to content

Commit c25d508

Browse files
asjSomasundaram Krishnasamy
authored andcommitted
btrfs: merge btrfs_find_device and find_device
commit 09ba3bc upstream. 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]> [4.19.y backport notes: Vikash : - To apply this patch, a portion of commit e4319cd was used to change the first argument of function "btrfs_find_device" from "struct btrfs_fs_info" to "struct btrfs_fs_devices". Signed-off-by: Vikash Bansal <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Orabug: 31764618 CVE: CVE-2019-18885 (cherry picked from commit 8cb9b069fa631b613bbbd6f63887190e55cafa3c) Signed-off-by: Dan Duval <[email protected]> Reviewed-by: John Donnelly <[email protected]> Conflicts: fs/btrfs/ioctl.c fs/btrfs/scrub.c fs/btrfs/volumes.c Signed-off-by: Somasundaram Krishnasamy <[email protected]>
1 parent 66093e8 commit c25d508

File tree

5 files changed

+50
-51
lines changed

5 files changed

+50
-51
lines changed

fs/btrfs/dev-replace.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
132132
break;
133133
case BTRFS_IOCTL_DEV_REPLACE_STATE_STARTED:
134134
case BTRFS_IOCTL_DEV_REPLACE_STATE_SUSPENDED:
135-
dev_replace->srcdev = btrfs_find_device(fs_info, src_devid,
136-
NULL, NULL);
137-
dev_replace->tgtdev = btrfs_find_device(fs_info,
135+
dev_replace->srcdev = btrfs_find_device(fs_info->fs_devices,
136+
src_devid, NULL, NULL, true);
137+
dev_replace->tgtdev = btrfs_find_device(fs_info->fs_devices,
138138
BTRFS_DEV_REPLACE_DEVID,
139-
NULL, NULL);
139+
NULL, NULL, true);
140140
/*
141141
* allow 'btrfs dev replace_cancel' if src/tgt device is
142142
* missing

fs/btrfs/ioctl.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
15301530
btrfs_info(fs_info, "resizing devid %llu", devid);
15311531
}
15321532

1533-
device = btrfs_find_device(fs_info, devid, NULL, NULL);
1533+
device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
15341534
if (!device) {
15351535
btrfs_info(fs_info, "resizer unable to find device %llu",
15361536
devid);
@@ -2828,7 +2828,8 @@ static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
28282828
s_uuid = di_args->uuid;
28292829

28302830
mutex_lock(&fs_devices->device_list_mutex);
2831-
dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
2831+
dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
2832+
NULL, true);
28322833

28332834
if (!dev) {
28342835
ret = -ENODEV;

fs/btrfs/scrub.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4131,7 +4131,7 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
41314131

41324132

41334133
mutex_lock(&fs_info->fs_devices->device_list_mutex);
4134-
dev = btrfs_find_device(fs_info, devid, NULL, NULL);
4134+
dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
41354135
if (!dev || (dev->missing && !is_dev_replace)) {
41364136
mutex_unlock(&fs_info->fs_devices->device_list_mutex);
41374137
return -ENODEV;
@@ -4296,7 +4296,7 @@ int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
42964296
struct scrub_ctx *sctx = NULL;
42974297

42984298
mutex_lock(&fs_info->fs_devices->device_list_mutex);
4299-
dev = btrfs_find_device(fs_info, devid, NULL, NULL);
4299+
dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
43004300
if (dev)
43014301
sctx = dev->scrub_device;
43024302
if (sctx)

fs/btrfs/volumes.c

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -253,28 +253,6 @@ static struct btrfs_device *__alloc_device(void)
253253
return dev;
254254
}
255255

256-
/*
257-
* Find a device specified by @devid or @uuid in the list of @fs_devices, or
258-
* return NULL.
259-
*
260-
* If devid and uuid are both specified, the match must be exact, otherwise
261-
* only devid is used.
262-
*/
263-
static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
264-
u64 devid, const u8 *uuid)
265-
{
266-
struct list_head *head = &fs_devices->devices;
267-
struct btrfs_device *dev;
268-
269-
list_for_each_entry(dev, head, dev_list) {
270-
if (dev->devid == devid &&
271-
(!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
272-
return dev;
273-
}
274-
}
275-
return NULL;
276-
}
277-
278256
static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
279257
{
280258
struct btrfs_fs_devices *fs_devices;
@@ -629,8 +607,8 @@ static noinline int device_list_add(const char *path,
629607

630608
device = NULL;
631609
} else {
632-
device = find_device(fs_devices, devid,
633-
disk_super->dev_item.uuid);
610+
device = btrfs_find_device(fs_devices, devid,
611+
disk_super->dev_item.uuid, NULL, false);
634612
}
635613

636614
if (!device) {
@@ -2115,7 +2093,8 @@ static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
21152093
disk_super = (struct btrfs_super_block *)bh->b_data;
21162094
devid = btrfs_stack_device_id(&disk_super->dev_item);
21172095
dev_uuid = disk_super->dev_item.uuid;
2118-
*device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
2096+
*device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2097+
disk_super->fsid, true);
21192098
brelse(bh);
21202099
if (!*device)
21212100
ret = -ENOENT;
@@ -2164,7 +2143,8 @@ int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
21642143

21652144
if (devid) {
21662145
ret = 0;
2167-
*device = btrfs_find_device(fs_info, devid, NULL, NULL);
2146+
*device = btrfs_find_device(fs_info->fs_devices, devid,
2147+
NULL, NULL, true);
21682148
if (!*device)
21692149
ret = -ENOENT;
21702150
} else {
@@ -2296,7 +2276,8 @@ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
22962276
BTRFS_UUID_SIZE);
22972277
read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
22982278
BTRFS_FSID_SIZE);
2299-
device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
2279+
device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
2280+
fs_uuid, true);
23002281
BUG_ON(!device); /* Logic error */
23012282

23022283
if (device->fs_devices->seeding) {
@@ -6300,21 +6281,36 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
63006281
return BLK_STS_OK;
63016282
}
63026283

6303-
struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
6304-
u8 *uuid, u8 *fsid)
6284+
/*
6285+
* Find a device specified by @devid or @uuid in the list of @fs_devices, or
6286+
* return NULL.
6287+
*
6288+
* If devid and uuid are both specified, the match must be exact, otherwise
6289+
* only devid is used.
6290+
*
6291+
* If @seed is true, traverse through the seed devices.
6292+
*/
6293+
struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
6294+
u64 devid, u8 *uuid, u8 *fsid,
6295+
bool seed)
63056296
{
63066297
struct btrfs_device *device;
6307-
struct btrfs_fs_devices *cur_devices;
63086298

6309-
cur_devices = fs_info->fs_devices;
6310-
while (cur_devices) {
6299+
while (fs_devices) {
63116300
if (!fsid ||
6312-
!memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
6313-
device = find_device(cur_devices, devid, uuid);
6314-
if (device)
6315-
return device;
6301+
!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
6302+
list_for_each_entry(device, &fs_devices->devices,
6303+
dev_list) {
6304+
if (device->devid == devid &&
6305+
(!uuid || memcmp(device->uuid, uuid,
6306+
BTRFS_UUID_SIZE) == 0))
6307+
return device;
6308+
}
63166309
}
6317-
cur_devices = cur_devices->seed;
6310+
if (seed)
6311+
fs_devices = fs_devices->seed;
6312+
else
6313+
return NULL;
63186314
}
63196315
return NULL;
63206316
}
@@ -6547,8 +6543,8 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
65476543
read_extent_buffer(leaf, uuid, (unsigned long)
65486544
btrfs_stripe_dev_uuid_nr(chunk, i),
65496545
BTRFS_UUID_SIZE);
6550-
map->stripes[i].dev = btrfs_find_device(fs_info, devid,
6551-
uuid, NULL);
6546+
map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
6547+
devid, uuid, NULL, true);
65526548
if (!map->stripes[i].dev &&
65536549
!btrfs_test_opt(fs_info, DEGRADED)) {
65546550
free_extent_map(em);
@@ -6683,7 +6679,8 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
66836679
return PTR_ERR(fs_devices);
66846680
}
66856681

6686-
device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
6682+
device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
6683+
fs_uuid, true);
66876684
if (!device) {
66886685
if (!btrfs_test_opt(fs_info, DEGRADED)) {
66896686
btrfs_report_missing_device(fs_info, devid, dev_uuid);
@@ -7256,7 +7253,8 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
72567253
int i;
72577254

72587255
mutex_lock(&fs_devices->device_list_mutex);
7259-
dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
7256+
dev = btrfs_find_device(fs_info->fs_devices, stats->devid,
7257+
NULL, NULL, true);
72607258
mutex_unlock(&fs_devices->device_list_mutex);
72617259

72627260
if (!dev) {

fs/btrfs/volumes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ void btrfs_cleanup_fs_uuids(void);
446446
int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
447447
int btrfs_grow_device(struct btrfs_trans_handle *trans,
448448
struct btrfs_device *device, u64 new_size);
449-
struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
450-
u8 *uuid, u8 *fsid);
449+
struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
450+
u64 devid, u8 *uuid, u8 *fsid, bool seed);
451451
int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
452452
int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *path);
453453
int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,

0 commit comments

Comments
 (0)