Skip to content

Commit

Permalink
init: refactor devt_from_partuuid
Browse files Browse the repository at this point in the history
The code in devt_from_partuuid is very convoluted.  Refactor a bit by
sanitizing the goto and variable name usage.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Christoph Hellwig authored and Jens Axboe committed Dec 1, 2020
1 parent c2637e8 commit e036bb8
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions init/do_mounts.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,10 @@ static int match_dev_by_uuid(struct device *dev, const void *data)
*/
static dev_t devt_from_partuuid(const char *uuid_str)
{
dev_t res = 0;
struct uuidcmp cmp;
struct device *dev = NULL;
struct gendisk *disk;
struct hd_struct *part;
dev_t devt = 0;
int offset = 0;
bool clear_root_wait = false;
char *slash;

cmp.uuid = uuid_str;
Expand All @@ -120,52 +117,49 @@ static dev_t devt_from_partuuid(const char *uuid_str)
/* Check for optional partition number offset attributes. */
if (slash) {
char c = 0;

/* Explicitly fail on poor PARTUUID syntax. */
if (sscanf(slash + 1,
"PARTNROFF=%d%c", &offset, &c) != 1) {
clear_root_wait = true;
goto done;
}
if (sscanf(slash + 1, "PARTNROFF=%d%c", &offset, &c) != 1)
goto clear_root_wait;
cmp.len = slash - uuid_str;
} else {
cmp.len = strlen(uuid_str);
}

if (!cmp.len) {
clear_root_wait = true;
goto done;
}
if (!cmp.len)
goto clear_root_wait;

dev = class_find_device(&block_class, NULL, &cmp,
&match_dev_by_uuid);
dev = class_find_device(&block_class, NULL, &cmp, &match_dev_by_uuid);
if (!dev)
goto done;

res = dev->devt;
return 0;

/* Attempt to find the partition by offset. */
if (!offset)
goto no_offset;
if (offset) {
/*
* Attempt to find the requested partition by adding an offset
* to the partition number found by UUID.
*/
struct hd_struct *part;

res = 0;
disk = part_to_disk(dev_to_part(dev));
part = disk_get_part(disk, dev_to_part(dev)->partno + offset);
if (part) {
res = part_devt(part);
put_device(part_to_dev(part));
part = disk_get_part(dev_to_disk(dev),
dev_to_part(dev)->partno + offset);
if (part) {
devt = part_devt(part);
put_device(part_to_dev(part));
}
} else {
devt = dev->devt;
}

no_offset:
put_device(dev);
done:
if (clear_root_wait) {
pr_err("VFS: PARTUUID= is invalid.\n"
"Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
if (root_wait)
pr_err("Disabling rootwait; root= is invalid.\n");
root_wait = 0;
}
return res;
return devt;

clear_root_wait:
pr_err("VFS: PARTUUID= is invalid.\n"
"Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
if (root_wait)
pr_err("Disabling rootwait; root= is invalid.\n");
root_wait = 0;
return 0;
}

/**
Expand Down

0 comments on commit e036bb8

Please sign in to comment.