From 01e014a24602ff76920efbcf5f0c684744e7f59e Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Fri, 24 May 2024 01:07:47 +0200 Subject: [PATCH] device: use device_get_uuid Replace call to get_dm_uuid_from_sysfs() with use of device_get_uuid() which gets the same information, but instead of several syscalls it need either 1 or even 0 when the information is cached with newer kernels. --- lib/device/dev-cache.c | 15 ++------------- lib/device/dev-cache.h | 1 - lib/device/dev-type.c | 40 ++++++++++------------------------------ tools/vgchange.c | 2 +- tools/vgimportdevices.c | 7 ++++--- 5 files changed, 17 insertions(+), 48 deletions(-) diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c index 6c76d035e..c4fdbe1fc 100644 --- a/lib/device/dev-cache.c +++ b/lib/device/dev-cache.c @@ -21,6 +21,7 @@ #include "lib/config/config.h" #include "lib/commands/toolcontext.h" #include "device_mapper/misc/dm-ioctl.h" +#include "lib/activate/activate.h" #include "lib/misc/lvm-string.h" #ifdef UDEV_SYNC_SUPPORT @@ -453,18 +454,6 @@ out: return r; } -int get_dm_uuid_from_sysfs(char *buf, size_t buf_size, int major, int minor) -{ - char path[PATH_MAX]; - - if (dm_snprintf(path, sizeof(path), "%sdev/block/%d:%d/dm/uuid", dm_sysfs_dir(), major, minor) < 0) { - log_error("%d:%d: dm_snprintf failed for path to sysfs dm directory.", major, minor); - return 0; - } - - return get_sysfs_value(path, buf, buf_size, 0); -} - static struct dm_list *_get_or_add_list_by_index_key(struct dm_hash_table *idx, const char *key) { struct dm_list *list; @@ -574,7 +563,7 @@ static int _get_vgid_and_lvid_for_dev(struct cmd_context *cmd, struct device *de char uuid[DM_UUID_LEN]; size_t uuid_len; - if (!get_dm_uuid_from_sysfs(uuid, sizeof(uuid), (int) MAJOR(dev->dev), (int) MINOR(dev->dev))) + if (!device_get_uuid(cmd, MAJOR(dev->dev), MINOR(dev->dev), uuid, sizeof(uuid))) return_0; uuid_len = strlen(uuid); diff --git a/lib/device/dev-cache.h b/lib/device/dev-cache.h index 80a75c5de..e6d0364ff 100644 --- a/lib/device/dev-cache.h +++ b/lib/device/dev-cache.h @@ -76,7 +76,6 @@ bool dev_cache_has_md_with_end_superblock(struct dev_types *dt); int get_sysfs_value(const char *path, char *buf, size_t buf_size, int error_if_no_value); int get_sysfs_binary(const char *path, char *buf, size_t buf_size, int *retlen); -int get_dm_uuid_from_sysfs(char *buf, size_t buf_size, int major, int minor); int setup_devices_file(struct cmd_context *cmd); int setup_devices(struct cmd_context *cmd); diff --git a/lib/device/dev-type.c b/lib/device/dev-type.c index 08a96d198..7e8f491df 100644 --- a/lib/device/dev-type.c +++ b/lib/device/dev-type.c @@ -22,6 +22,7 @@ #include "lib/device/bcache.h" #include "lib/label/label.h" #include "lib/commands/toolcontext.h" +#include "lib/activate/activate.h" #include "device_mapper/misc/dm-ioctl.h" #ifdef BLKID_WIPING_SUPPORT @@ -52,31 +53,14 @@ int dev_is_nvme(struct dev_types *dt, struct device *dev) int dev_is_lv(struct cmd_context *cmd, struct device *dev) { - FILE *fp; - char path[PATH_MAX]; - char buffer[64]; - int ret = 0; + char buffer[128]; - if (dm_snprintf(path, sizeof(path), "%sdev/block/%d:%d/dm/uuid", - dm_sysfs_dir(), - (int) MAJOR(dev->dev), - (int) MINOR(dev->dev)) < 0) { - log_warn("Sysfs dm uuid path for %s is too long.", dev_name(dev)); - return 0; - } + if (device_get_uuid(cmd, MAJOR(dev->dev), MINOR(dev->dev), + buffer, sizeof(buffer)) && + !strncmp(buffer, UUID_PREFIX, sizeof(UUID_PREFIX) - 1)) + return 1; - if (!(fp = fopen(path, "r"))) - return 0; - - if (!fgets(buffer, sizeof(buffer), fp)) - log_debug("Failed to read %s.", path); - else if (!strncmp(buffer, "LVM-", 4)) - ret = 1; - - if (fclose(fp)) - log_sys_debug("fclose", path); - - return ret; + return 0; } int dev_is_used_by_active_lv(struct cmd_context *cmd, struct device *dev, int *used_by_lv_count, @@ -140,14 +124,10 @@ int dev_is_used_by_active_lv(struct cmd_context *cmd, struct device *dev, int *u /* * if "dm-1" is a dm device, then check if it's an LVM LV - * by reading /sys/block//dm/uuid and seeing - * if the uuid begins with LVM- - * UUID_PREFIX is "LVM-" + * by reading DM status and seeing if the uuid begins + * with UUID_PREFIX ("LVM-") */ - - dm_uuid[0] = '\0'; - - if (!get_dm_uuid_from_sysfs(dm_uuid, sizeof(dm_uuid), dm_dev_major, dm_dev_minor)) + if (!device_get_uuid(cmd, dm_dev_major, dm_dev_minor, dm_uuid, sizeof(dm_uuid))) continue; if (!strncmp(dm_uuid, UUID_PREFIX, 4)) diff --git a/tools/vgchange.c b/tools/vgchange.c index 2004d6e92..6a170c430 100644 --- a/tools/vgchange.c +++ b/tools/vgchange.c @@ -869,7 +869,7 @@ static void _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out) if (stat(me->mnt_dir, &info) < 0) return; - if (!get_dm_uuid_from_sysfs(dm_uuid, sizeof(dm_uuid), (int)MAJOR(info.st_dev), (int)MINOR(info.st_dev))) + if (!device_get_uuid(cmd, MAJOR(info.st_dev), MINOR(info.st_dev), dm_uuid, sizeof(dm_uuid))) return; log_debug("Found root dm_uuid %s", dm_uuid); diff --git a/tools/vgimportdevices.c b/tools/vgimportdevices.c index 70d12e500..b4925bddc 100644 --- a/tools/vgimportdevices.c +++ b/tools/vgimportdevices.c @@ -16,6 +16,7 @@ #include "lib/cache/lvmcache.h" #include "lib/device/device_id.h" #include "device_mapper/misc/dm-ioctl.h" +#include "lib/activate/activate.h" /* coverity[unnecessary_header] needed for MuslC */ #include #include @@ -151,14 +152,14 @@ static int _get_rootvg_dev(struct cmd_context *cmd, char **dm_uuid_out, int *ski if (stat(me->mnt_dir, &info) < 0) return_0; - if (!get_dm_uuid_from_sysfs(dm_uuid, sizeof(dm_uuid), (int)MAJOR(info.st_dev), (int)MINOR(info.st_dev))) + if (!device_get_uuid(cmd, MAJOR(info.st_dev), MINOR(info.st_dev), dm_uuid, sizeof(dm_uuid))) return_0; /* UUID_PREFIX = "LVM-" */ - if (strncmp(dm_uuid, UUID_PREFIX, 4)) + if (strncmp(dm_uuid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1)) return_0; - if (strlen(dm_uuid) < 4 + ID_LEN) + if (strlen(dm_uuid) < sizeof(UUID_PREFIX) - 1 + ID_LEN) return_0; *dm_uuid_out = dm_pool_strdup(cmd->mem, dm_uuid);