From 14a8657940be34a781222b4b715bd09eb80d1057 Mon Sep 17 00:00:00 2001 From: Mariusz Tkaczyk Date: Thu, 29 Feb 2024 12:52:09 +0100 Subject: [PATCH 25/41] mdadm: introduce sysfs_get_container_devnm() There at least two places where it is done directly, so replace them with function. Print message about creating external array, add "/dev/" prefix to refer directly to devnode. Signed-off-by: Mariusz Tkaczyk --- Create.c | 21 ++++++++++----------- Manage.c | 14 ++++---------- mdadm.h | 2 ++ sysfs.c | 23 +++++++++++++++++++++++ 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Create.c b/Create.c index 7e9170b6..0b776266 100644 --- a/Create.c +++ b/Create.c @@ -1142,24 +1142,23 @@ int Create(struct supertype *st, struct mddev_ident *ident, int subdevs, if (did_default && c->verbose >= 0) { if (is_subarray(info.text_version)) { - char devnm[32]; - char *ep; + char devnm[MD_NAME_MAX]; struct mdinfo *mdi; - strncpy(devnm, info.text_version+1, 32); - devnm[31] = 0; - ep = strchr(devnm, '/'); - if (ep) - *ep = 0; + sysfs_get_container_devnm(&info, devnm); mdi = sysfs_read(-1, devnm, GET_VERSION); + if (!mdi) { + pr_err("Cannot open sysfs for container %s\n", devnm); + goto abort_locked; + } + + pr_info("Creating array inside %s container /dev/%s\n", mdi->text_version, + devnm); - pr_info("Creating array inside %s container %s\n", - mdi?mdi->text_version:"managed", devnm); sysfs_free(mdi); } else - pr_info("Defaulting to version %s metadata\n", - info.text_version); + pr_info("Defaulting to version %s metadata\n", info.text_version); } map_update(&map, fd2devnm(mdfd), info.text_version, diff --git a/Manage.c b/Manage.c index b3e216cb..969d0ea9 100644 --- a/Manage.c +++ b/Manage.c @@ -178,7 +178,7 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry) struct map_ent *map = NULL; struct mdinfo *mdi; char devnm[32]; - char container[32]; + char container[MD_NAME_MAX] = {0}; int err; int count; char buf[SYSFS_MAX_BUF_SIZE]; @@ -192,15 +192,9 @@ int Manage_stop(char *devname, int fd, int verbose, int will_retry) * to stop is probably a bad idea. */ mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_COMPONENT|GET_VERSION); - if (mdi && is_subarray(mdi->text_version)) { - char *sl; - strncpy(container, mdi->text_version+1, sizeof(container)); - container[sizeof(container)-1] = 0; - sl = strchr(container, '/'); - if (sl) - *sl = 0; - } else - container[0] = 0; + if (mdi && is_subarray(mdi->text_version)) + sysfs_get_container_devnm(mdi, container); + close(fd); count = 5; while (((fd = ((devname[0] == '/') diff --git a/mdadm.h b/mdadm.h index cbc586f5..39b86bd0 100644 --- a/mdadm.h +++ b/mdadm.h @@ -777,6 +777,8 @@ enum sysfs_read_flags { #define SYSFS_MAX_BUF_SIZE 64 +extern void sysfs_get_container_devnm(struct mdinfo *mdi, char *buf); + /* If fd >= 0, get the array it is open on, * else use devnm. */ diff --git a/sysfs.c b/sysfs.c index f95ef701..230b842e 100644 --- a/sysfs.c +++ b/sysfs.c @@ -74,6 +74,29 @@ void sysfs_free(struct mdinfo *sra) } } +/** + * sysfs_get_container_devnm() - extract container device name. + * @mdi: md_info describes member array, with GET_VERSION option. + * @buf: buf to fill, must be MD_NAME_MAX. + * + * External array version is in format {/,-}/ + * Extract container_devnm from it and safe it in @buf. + */ +void sysfs_get_container_devnm(struct mdinfo *mdi, char *buf) +{ + char *p; + + assert(is_subarray(mdi->text_version)); + + /* Skip first special sign */ + snprintf(buf, MD_NAME_MAX, "%s", mdi->text_version + 1); + + /* Remove array index */ + p = strchr(buf, '/'); + if (p) + *p = 0; +} + int sysfs_open(char *devnm, char *devname, char *attr) { char fname[MAX_SYSFS_PATH_LEN]; -- 2.40.1