]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
mdadm: introduce sysfs_get_container_devnm()
authorMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Thu, 29 Feb 2024 11:52:09 +0000 (12:52 +0100)
committerMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Mon, 11 Mar 2024 10:07:03 +0000 (11:07 +0100)
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 <mariusz.tkaczyk@linux.intel.com>
Create.c
Manage.c
mdadm.h
sysfs.c

index 7e9170b6a1ac3b1125a54018d9f026d705af31d7..0b7762661c768d102532f6129d1efae2501ee7ce 100644 (file)
--- 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,
index b3e216cbcec678f7a9eb5ec69364b8eff8b99f81..969d0ea9d81fa7b12805ae144cf9ab5a5cfb6fab 100644 (file)
--- 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 cbc586f5e3ef8e8ae199fb6bd14fc53583ee4d86..39b86bd08029979ab5ae15e79dcee5afc8487ac0 100644 (file)
--- 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 f95ef7013e845b62791f499acd08d5f0abe315fd..230b842e41171191ed58f015571b78fccadff03c 100644 (file)
--- 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 {/,-}<container_devnm>/<array_index>
+ * 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];