To have it better understandable add definitions for that.
Definition is placed in md_common.h because it is corresponding to both
mdstat and sysfs implementations. Probably more shared properties will
be defined in the future.
The MD_EXTERNAL_PREFIX_LEN and MD_VERSION_BLOCKED_IDX are the same
but are defined for different purpose. One is specyfing size
of string but the second is specyfing index in the array.
Also, replaces magic "external" used in code.
No functional changes intended.
Signed-off-by: Mariusz Tkaczyk <mtkaczyk@kernel.org>
continue;
/* Skip first char - it can be '/' or '-' */
- if (strcmp(&ent->metadata_version[10], metadata_version + 1) == 0) {
+ if (strcmp(&ent->metadata_version[MD_VER_EXT_LEN + 1], metadata_version + 1) == 0) {
busy = 1;
break;
}
de->d_name);
if (load_sys(path, vbuf, sizeof(vbuf)) < 0)
continue;
- if (strncmp(vbuf, "external:", 9) ||
- !is_subarray(vbuf + 9) ||
- strncmp(vbuf + 10, sra->sys_name, nlen) ||
- vbuf[10 + nlen] != '/')
+ if (strncmp(vbuf, MD_VER_EXT, MD_VER_EXT_LEN) ||
+ !is_subarray(vbuf + MD_VER_EXT_LEN) ||
+ strncmp(vbuf + MD_VER_EXT_LEN + 1, sra->sys_name, nlen) ||
+ vbuf[MD_VER_EXT_LEN + 1 + nlen] != '/')
continue;
devid = devnm2devid(de->d_name);
printf(" %s",
if (!is_container_member(e, container))
continue;
/* frozen array is not idle*/
- if (e->percent >= 0 || e->metadata_version[9] == '-') {
+ if (e->percent >= 0 || e->metadata_version[MD_VER_BLOCKED_IDX] == '-') {
is_idle = 0;
break;
}
mdi->array.major_version == -1 &&
is_subarray(mdi->text_version)) {
char vers[64];
- strcpy(vers, "external:");
+ strcpy(vers, MD_VER_EXT);
strcat(vers, mdi->text_version);
if (readonly > 0) {
int rv;
/* We set readonly ourselves. */
- vers[9] = '-';
+ vers[MD_VER_BLOCKED_IDX] = '-';
sysfs_set_str(mdi, NULL, "metadata_version", vers);
close_fd(&fd);
pr_err("failed to set readonly for %s: %s\n",
devname, strerror(errno));
- vers[9] = mdi->text_version[0];
+ vers[MD_VER_BLOCKED_IDX] = mdi->text_version[0];
sysfs_set_str(mdi, NULL, "metadata_version", vers);
rv = 1;
goto out;
} else {
char *cp;
/* We cannot set read/write - must signal mdmon */
- vers[9] = '/';
+ vers[MD_VER_BLOCKED_IDX] = '/';
sysfs_set_str(mdi, NULL, "metadata_version", vers);
- cp = strchr(vers+10, '/');
+ cp = strchr(vers + MD_VER_BLOCKED_IDX + 1, '/');
if (cp)
*cp = 0;
- ping_monitor(vers+10);
+ ping_monitor(vers + MD_VER_BLOCKED_IDX + 1);
if (mdi->array.level <= 0)
sysfs_set_str(mdi, NULL, "array_state", "active");
}
mds = mdstat_read(0, 0);
for (m = mds; m; m = m->next)
if (is_mdstat_ent_external(m) &&
- metadata_container_matches(m->metadata_version + 9, devnm)) {
+ metadata_container_matches(m->metadata_version + MD_VER_EXT_LEN,
+ devnm)) {
if (verbose >= 0)
pr_err("Cannot stop container %s: member %s still active\n",
devname, m->devnm);
--- /dev/null
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/*
+ * md_common - constants shared between md driver abstractions.
+ */
+
+#ifndef MD_COMMON_H
+#define MD_COMMON_H
+
+/*
+ * Prefix used by the md driver version to mark an externally-managed array or
+ * container. The full form is:
+ *
+ * "external:" [/-] containername [/subarray]
+ *
+ * The '/' or '-' separator distinguishes normal read-write arrays from those
+ * that mdmon must not reconfigure (read-only, reshaping, etc.).
+ */
+#define MD_EXT "external" /* The external keyword used in various contexts */
+
+#define MD_VER_EXT "external:" /* The "external:" version const prefix */
+#define MD_VER_EXT_LEN (sizeof(MD_VER_EXT) - 1) /* Length of external prefix */
+
+/* Index of the character that blocks the version.
+ * It is the one after the prefix so the array index is MD_VER_EXT_LEN
+ */
+#define MD_VER_BLOCKED_IDX (MD_VER_EXT_LEN)
+
+#endif
if (is_mdstat_ent_subarray(mse)) {
char *sl;
- snprintf(st->parent_devnm, MD_NAME_MAX, "%s", mse->metadata_version + 10);
+ snprintf(st->parent_devnm, MD_NAME_MAX, "%s",
+ mse->metadata_version + MD_VER_EXT_LEN + 1);
sl = strchr(st->parent_devnm, '/');
if (sl)
*sl = 0;
char *sl;
snprintf(st->parent_devnm, MD_NAME_MAX, "%s",
- mse->metadata_version + 10);
+ mse->metadata_version + MD_VER_EXT_LEN + 1);
sl = strchr(st->parent_devnm, '/');
if (sl)
*sl = 0;
}
if (!e || e->percent == RESYNC_NONE) {
if (e && is_mdstat_ent_external(e)) {
- if (is_subarray(&e->metadata_version[9]))
- ping_monitor(&e->metadata_version[9]);
+ if (is_subarray(&e->metadata_version[MD_VER_EXT_LEN]))
+ ping_monitor(&e->metadata_version[MD_VER_EXT_LEN]);
else
ping_monitor(devnm);
}
if (!ent->metadata_version)
return false;
- if (strncmp(ent->metadata_version, "external:", 9) == 0)
+ if (strncmp(ent->metadata_version, MD_VER_EXT, MD_VER_EXT_LEN) == 0)
return true;
return false;
}
bool is_mdstat_ent_subarray(struct mdstat_ent *ent)
{
- if (is_mdstat_ent_external(ent) && is_subarray(ent->metadata_version + 9))
+ if (is_mdstat_ent_external(ent) && is_subarray(ent->metadata_version + MD_VER_EXT_LEN))
return true;
return false;
}
bool is_container_member(struct mdstat_ent *mdstat, char *container)
{
if (is_mdstat_ent_external(mdstat) &&
- metadata_container_matches(mdstat->metadata_version + 9, container))
+ metadata_container_matches(mdstat->metadata_version + MD_VER_EXT_LEN, container))
return true;
return false;
if (!is_mdstat_ent_external(ent))
continue;
- if (!metadata_container_matches(ent->metadata_version + 9, container))
+ if (!metadata_container_matches(ent->metadata_version + MD_VER_EXT_LEN, container))
continue;
- if (!metadata_subdev_matches(ent->metadata_version + 9, subdev))
+ if (!metadata_subdev_matches(ent->metadata_version + MD_VER_EXT_LEN, subdev))
continue;
break;
#include <stdbool.h>
#include <string.h>
+#include "md_common.h"
+
struct mdstat_ent {
char devnm[32];
static inline char *to_subarray(struct mdstat_ent *ent, char *container)
{
- return &ent->metadata_version[10+strlen(container)+1];
+ /* Skip: "external:" prefix, [/-] separator, container name, '/'. */
+ return &ent->metadata_version[MD_VER_EXT_LEN + 1 + strlen(container) + 1];
}
#endif
int rc = 0;
if (sra) {
- sprintf(buf, "external:%s\n", sra->text_version);
+ sprintf(buf, MD_VER_EXT "%s\n", sra->text_version);
buf[9] = '/';
} else
buf[9] = '-';
char buf[64];
int rc = 0;
- sprintf(buf, "external:%s\n", sra->text_version);
+ sprintf(buf, MD_VER_EXT "%s\n", sra->text_version);
buf[9] = '-';
if (sysfs_set_str(sra, NULL, "metadata_version", buf))
rc = -1;
int found;
for (memb = mdstat ; memb ; memb = memb->next) {
- if (is_mdstat_ent_external(memb) && !is_subarray(memb->metadata_version + 9) &&
- strcmp(&memb->metadata_version[9], name) == 0 && memb->members) {
+ if (is_mdstat_ent_external(memb) &&
+ !is_subarray(memb->metadata_version + MD_VER_EXT_LEN) &&
+ strcmp(&memb->metadata_version[MD_VER_EXT_LEN], name) == 0 && memb->members) {
struct dev_member *dev = memb->members;
int fd = -1;
* related to the device's first lba (in opposition to the "internal" case
* when this value is related to the beginning of the superblock).
*/
- if (sysfs_set_str(info, NULL, "bitmap/metadata", "external")) {
+ if (sysfs_set_str(info, NULL, "bitmap/metadata", MD_EXT)) {
dprintf("failed to set bitmap/metadata\n");
return -1;
}
*/
#include "mdadm.h"
+#include "md_common.h"
#include "dlink.h"
#include "xmalloc.h"
sra->array.major_version =
sra->array.minor_version = -1;
strcpy(sra->text_version, "");
- } else if (strncmp(buf, "external:", 9) == 0) {
+ } else if (strncmp(buf, MD_VER_EXT, MD_VER_EXT_LEN) == 0) {
sra->array.major_version = -1;
sra->array.minor_version = -2;
- strcpy(sra->text_version, buf+9);
+ strcpy(sra->text_version, buf + MD_VER_EXT_LEN);
sra->text_version[sizeof(sra->text_version) - 1] = '\0';
} else {
sscanf(buf, "%d.%d",
info->array.minor_version == -2) {
char buf[SYSFS_MAX_BUF_SIZE];
- strcat(strcpy(ver, "external:"), info->text_version);
+ strcat(strcpy(ver, MD_VER_EXT), info->text_version);
/* meta version might already be set if we are setting
* new geometry for a reshape. In that case we don't
*/
if (sysfs_get_str(info, NULL, "metadata_version",
buf, sizeof(buf)) > 0)
- if (strlen(buf) >= 9 && buf[9] == '-')
- ver[9] = '-';
+ if (strlen(buf) >= MD_VER_BLOCKED_IDX &&
+ buf[MD_VER_BLOCKED_IDX] == '-')
+ ver[MD_VER_BLOCKED_IDX] = '-';
if (sysfs_set_str(info, NULL, "metadata_version", ver) < 0) {
pr_err("This kernel does not support external metadata.\n");
*/
#include "mdadm.h"
+#include "md_common.h"
#include "md_p.h"
#include "xmalloc.h"
if (n <= 0 || (unsigned)n >= sizeof(buf))
continue;
buf[n] = 0;
- if (strncmp(buf, "external", 8) != 0 ||
+ if (strncmp(buf, MD_EXT, strlen(MD_EXT)) != 0 ||
n < 10 ||
buf[9] == '/')
continue;