]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
mdadm: define DEV_MD_DIR
authorMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Thu, 23 Mar 2023 16:50:14 +0000 (17:50 +0100)
committerJes Sorensen <jes@trained-monkey.org>
Mon, 8 May 2023 20:23:45 +0000 (16:23 -0400)
It is used many times. Additionally define _LEN to avoid repeated
strlen() calls when length is needed.

Signed-off-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
13 files changed:
Create.c
Detail.c
Incremental.c
Monitor.c
config.c
lib.c
mapfile.c
mdadm.h
mdopen.c
super-ddf.c
super-intel.c
super1.c
sysfs.c

index aa0472ddc1be59f426c8387ddfd14ee8c52365e5..ea6a4745f38a101c8527899e4425d51dc29ca23f 100644 (file)
--- a/Create.c
+++ b/Create.c
@@ -1024,10 +1024,9 @@ int Create(struct supertype *st, char *mddev,
         * it could be in conflict with already existing device
         * e.g. container, array
         */
-       if (strncmp(chosen_name, "/dev/md/", 8) == 0 &&
-           map_by_name(&map, chosen_name+8) != NULL) {
-               pr_err("Array name %s is in use already.\n",
-                       chosen_name);
+       if (strncmp(chosen_name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0 &&
+           map_by_name(&map, chosen_name + DEV_MD_DIR_LEN)) {
+               pr_err("Array name %s is in use already.\n", chosen_name);
                close(mdfd);
                map_unlock(&map);
                udev_unblock();
index 4ef2646075665503e60b4880f5ecc284cf5f6035..206d88e3477611d3bcc0dabb9904f98b8539f2b9 100644 (file)
--- a/Detail.c
+++ b/Detail.c
@@ -254,10 +254,9 @@ int Detail(char *dev, struct context *c)
                        fname_from_uuid(st, info, nbuf, ':');
                        printf("MD_UUID=%s\n", nbuf + 5);
                        mp = map_by_uuid(&map, info->uuid);
-                       if (mp && mp->path &&
-                           strncmp(mp->path, "/dev/md/", 8) == 0) {
+                       if (mp && mp->path && strncmp(mp->path, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0) {
                                printf("MD_DEVNAME=");
-                               print_escape(mp->path + 8);
+                               print_escape(mp->path + DEV_MD_DIR_LEN);
                                putchar('\n');
                        }
 
@@ -273,9 +272,9 @@ int Detail(char *dev, struct context *c)
                                printf("MD_UUID=%s\n", nbuf+5);
                        }
                        if (mp && mp->path &&
-                           strncmp(mp->path, "/dev/md/", 8) == 0) {
+                           strncmp(mp->path, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0) {
                                printf("MD_DEVNAME=");
-                               print_escape(mp->path+8);
+                               print_escape(mp->path + DEV_MD_DIR_LEN);
                                putchar('\n');
                        }
                        map_free(map);
index 49a71f72af9513f494c82ce97bc0a4e61d8ce624..59b850f1a24e63e86f76911ecfe6502676664f03 100644 (file)
@@ -460,8 +460,8 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
                        info.array.working_disks ++;
 
        }
-       if (strncmp(chosen_name, "/dev/md/", 8) == 0)
-               md_devname = chosen_name+8;
+       if (strncmp(chosen_name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0)
+               md_devname = chosen_name + DEV_MD_DIR_LEN;
        else
                md_devname = chosen_name;
        if (c->export) {
index 4491818454b2ebfcb673981de985055bfbfd2e6a..3273f2fb4cc41d919050130543ef4ea7caa585fa 100644 (file)
--- a/Monitor.c
+++ b/Monitor.c
 #define EVENT_NAME_MAX 32
 #define AUTOREBUILD_PID_PATH MDMON_DIR "/autorebuild.pid"
 
+/**
+ * struct state - external array or container properties.
+ * @devname: has length of %DEV_MD_DIR + device name + terminating byte
+ * @devnm: to sync with mdstat info
+ * @parent_devnm: or subarray, devnm of parent, for others, ""
+ * @subarray: for a container it is a link to first subarray, for a subarray it is a link to next
+ *           subarray in the same container
+ * @parent: for a subarray it is a link to its container
+ */
 struct state {
-       char devname[MD_NAME_MAX + sizeof("/dev/md/")]; /* length of "/dev/md/" + device name + terminating byte*/
-       char devnm[MD_NAME_MAX];        /* to sync with mdstat info */
+       char devname[MD_NAME_MAX + sizeof(DEV_MD_DIR)];
+       char devnm[MD_NAME_MAX];
        unsigned int utime;
        int err;
        char *spare_group;
@@ -49,15 +58,10 @@ struct state {
        int devstate[MAX_DISKS];
        dev_t devid[MAX_DISKS];
        int percent;
-       char parent_devnm[MD_NAME_MAX]; /* For subarray, devnm of parent.
-                                       * For others, ""
-                                       */
+       char parent_devnm[MD_NAME_MAX];
        struct supertype *metadata;
-       struct state *subarray;/* for a container it is a link to first subarray
-                               * for a subarray it is a link to next subarray
-                               * in the same container */
-       struct state *parent;  /* for a subarray it is a link to its container
-                               */
+       struct state *subarray;
+       struct state *parent;
        struct state *next;
 };
 
@@ -252,8 +256,8 @@ int Monitor(struct mddev_dev *devlist,
                                continue;
 
                        st = xcalloc(1, sizeof *st);
-                       snprintf(st->devname, MD_NAME_MAX + sizeof("/dev/md/"),
-                                "/dev/md/%s", basename(mdlist->devname));
+                       snprintf(st->devname, MD_NAME_MAX + sizeof(DEV_MD_DIR), DEV_MD_DIR "%s",
+                                basename(mdlist->devname));
                        st->next = statelist;
                        st->devnm[0] = 0;
                        st->percent = RESYNC_UNKNOWN;
@@ -274,7 +278,7 @@ int Monitor(struct mddev_dev *devlist,
 
                        st = xcalloc(1, sizeof *st);
                        mdlist = conf_get_ident(dv->devname);
-                       snprintf(st->devname, MD_NAME_MAX + sizeof("/dev/md/"), "%s", dv->devname);
+                       snprintf(st->devname, MD_NAME_MAX + sizeof(DEV_MD_DIR), "%s", dv->devname);
                        st->next = statelist;
                        st->devnm[0] = 0;
                        st->percent = RESYNC_UNKNOWN;
@@ -942,7 +946,7 @@ static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist)
                                continue;
                        }
 
-                       snprintf(st->devname, MD_NAME_MAX + sizeof("/dev/md/"), "%s", name);
+                       snprintf(st->devname, MD_NAME_MAX + sizeof(DEV_MD_DIR), "%s", name);
                        if ((fd = open(st->devname, O_RDONLY)) < 0 ||
                            md_get_array_info(fd, &array) < 0) {
                                /* no such array */
index eeedd0c6cde966db0aa31b4f92f629692a0b8ee3..59d5bfb684c7fd2064f24c4a1107d3da454c61ae 100644 (file)
--- a/config.c
+++ b/config.c
@@ -405,7 +405,7 @@ void arrayline(char *line)
                         *  or anything that doesn't start '/' or '<'
                         */
                        if (strcasecmp(w, "<ignore>") == 0 ||
-                           strncmp(w, "/dev/md/", 8) == 0 ||
+                           strncmp(w, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0 ||
                            (w[0] != '/' && w[0] != '<') ||
                            (strncmp(w, "/dev/md", 7) == 0 &&
                             is_number(w + 7)) ||
@@ -1102,13 +1102,13 @@ int devname_matches(char *name, char *match)
         *  mdNN with NN
         * then just strcmp
         */
-       if (strncmp(name, "/dev/md/", 8) == 0)
-               name += 8;
+       if (strncmp(name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0)
+               name += DEV_MD_DIR_LEN;
        else if (strncmp(name, "/dev/", 5) == 0)
                name += 5;
 
-       if (strncmp(match, "/dev/md/", 8) == 0)
-               match += 8;
+       if (strncmp(match, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0)
+               match += DEV_MD_DIR_LEN;
        else if (strncmp(match, "/dev/", 5) == 0)
                match += 5;
 
diff --git a/lib.c b/lib.c
index e395b28d7d078476013cc555441f45810c1eb29c..65ea51e0d66a723ca78cbe3cddbd1b6d52703c91 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -313,7 +313,7 @@ char *map_dev_preferred(int major, int minor, int create,
 
        for (p = devlist; p; p = p->next)
                if (p->major == major && p->minor == minor) {
-                       if (strncmp(p->name, "/dev/md/",8) == 0 ||
+                       if (strncmp(p->name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0 ||
                            (prefer && strstr(p->name, prefer))) {
                                if (preferred == NULL ||
                                    strlen(p->name) < strlen(preferred))
index ac351768c0bdc73b0a822e22868a0abc6fcabea3..34fea179b4c3cca649bf1cf657703eb99ba44eac 100644 (file)
--- a/mapfile.c
+++ b/mapfile.c
@@ -320,9 +320,9 @@ struct map_ent *map_by_name(struct map_ent **map, char *name)
        for (mp = *map ; mp ; mp = mp->next) {
                if (!mp->path)
                        continue;
-               if (strncmp(mp->path, "/dev/md/", 8) != 0)
+               if (strncmp(mp->path, DEV_MD_DIR, DEV_MD_DIR_LEN) != 0)
                        continue;
-               if (strcmp(mp->path+8, name) != 0)
+               if (strcmp(mp->path + DEV_MD_DIR_LEN, name) != 0)
                        continue;
                if (!mddev_busy(mp->devnm)) {
                        mp->bad = 1;
@@ -413,7 +413,7 @@ void RebuildMap(void)
                        devid = devnm2devid(md->devnm);
                        path = map_dev(major(devid), minor(devid), 0);
                        if (path == NULL ||
-                           strncmp(path, "/dev/md/", 8) != 0) {
+                           strncmp(path, DEV_MD_DIR, DEV_MD_DIR_LEN) != 0) {
                                /* We would really like a name that provides
                                 * an MD_DEVNAME for udev.
                                 * The name needs to be unique both in /dev/md/
@@ -434,7 +434,7 @@ void RebuildMap(void)
                                if (match && match->devname && match->devname[0] == '/') {
                                        path = match->devname;
                                        if (path[0] != '/') {
-                                               strcpy(namebuf, "/dev/md/");
+                                               strcpy(namebuf, DEV_MD_DIR);
                                                strcat(namebuf, path);
                                                path = namebuf;
                                        }
@@ -478,10 +478,10 @@ void RebuildMap(void)
 
                                        while (conflict) {
                                                if (unum >= 0)
-                                                       sprintf(namebuf, "/dev/md/%s%s%d",
+                                                       sprintf(namebuf, DEV_MD_DIR "%s%s%d",
                                                                name, sep, unum);
                                                else
-                                                       sprintf(namebuf, "/dev/md/%s",
+                                                       sprintf(namebuf, DEV_MD_DIR "%s",
                                                                name);
                                                unum++;
                                                if (lstat(namebuf, &stb) != 0 &&
diff --git a/mdadm.h b/mdadm.h
index 0d99544526fc88f9968a2b42d3b50ff2d499b010..67d73f963887cc086bbf7ffffb07b648780d35c2 100644 (file)
--- a/mdadm.h
+++ b/mdadm.h
@@ -100,6 +100,14 @@ struct dlm_lksb {
 #define DEFAULT_BITMAP_DELAY 5
 #define DEFAULT_MAX_WRITE_BEHIND 256
 
+/* DEV_MD_DIR points to named MD devices directory.
+ * DEV_MD_DIR_LEN is a length with Null byte excluded.
+ */
+#ifndef DEV_MD_DIR
+#define DEV_MD_DIR "/dev/md/"
+#define DEV_MD_DIR_LEN (sizeof(DEV_MD_DIR) - 1)
+#endif /* DEV_MD_DIR */
+
 /* MAP_DIR should be somewhere that persists across the pivotroot
  * from early boot to late boot.
  * /run  seems to have emerged as the best standard.
index 810f79a3d19adc2d72b2a47c64819e02399885cd..6c3bdb6adee76498b88955681ea5e1d229310627 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
@@ -188,12 +188,12 @@ int create_mddev(char *dev, char *name, int autof, int trustworthy,
        parts = autof >> 3;
        autof &= 7;
 
-       strcpy(chosen, "/dev/md/");
+       strcpy(chosen, DEV_MD_DIR);
        cname = chosen + strlen(chosen);
 
        if (dev) {
-               if (strncmp(dev, "/dev/md/", 8) == 0) {
-                       strcpy(cname, dev+8);
+               if (strncmp(dev, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0) {
+                       strcpy(cname, dev + DEV_MD_DIR_LEN);
                } else if (strncmp(dev, "/dev/", 5) == 0) {
                        char *e = dev + strlen(dev);
                        while (e > dev && isdigit(e[-1]))
index b86c6acdb9d94e170a32957ec133092f88459062..7213284e0a596069c3a378e99094a19351a82b1f 100644 (file)
@@ -1648,7 +1648,7 @@ static void brief_examine_subarrays_ddf(struct supertype *st, int verbose)
                fname_from_uuid(st, &info, nbuf1, ':');
                _ddf_array_name(namebuf, ddf, i);
                printf("ARRAY%s%s container=%s member=%d UUID=%s\n",
-                      namebuf[0] == '\0' ? "" : " /dev/md/", namebuf,
+                      namebuf[0] == '\0' ? "" : " " DEV_MD_DIR, namebuf,
                       nbuf+5, i, nbuf1+5);
        }
 }
index a5c86cb2a3901696281a4bfacfd0207b683c238d..aaf6659ef505f63472f4fdc3b78a18f2b8a6ec7f 100644 (file)
@@ -2309,7 +2309,7 @@ static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
                super->current_vol = i;
                getinfo_super_imsm(st, &info, NULL);
                fname_from_uuid(st, &info, nbuf1, ':');
-               printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
+               printf("ARRAY " DEV_MD_DIR "%.16s container=%s member=%d UUID=%s\n",
                       dev->volume, nbuf + 5, i, nbuf1 + 5);
        }
 }
index 938c3a68ce80b12e9525babbb9fc382aa0e74a9c..856b0208266237311bbbcc08f0dbe2909643ecd1 100644 (file)
--- a/super1.c
+++ b/super1.c
@@ -645,8 +645,7 @@ static void brief_examine_super1(struct supertype *st, int verbose)
 
        printf("ARRAY ");
        if (nm) {
-               printf("/dev/md/");
-               print_escape(nm);
+               printf(DEV_MD_DIR "%s", nm);
                putchar(' ');
        }
        if (verbose && c)
diff --git a/sysfs.c b/sysfs.c
index ca1d888f21ced33c8772a9a468cd04668f953b86..94d02f53a768e64b883ab76f07d1ee9729ea54af 100644 (file)
--- a/sysfs.c
+++ b/sysfs.c
@@ -1114,7 +1114,7 @@ void sysfsline(char *line)
                if (strncasecmp(w, "name=", 5) == 0) {
                        char *devname = w + 5;
 
-                       if (strncmp(devname, "/dev/md/", 8) == 0) {
+                       if (strncmp(devname, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0) {
                                if (sr->devname)
                                        pr_err("Only give one device per SYSFS line: %s\n",
                                                devname);