From f7ca2bbe447a5224efb4078fdd1d29910f495b91 Mon Sep 17 00:00:00 2001 From: Maxim Sobolev Date: Mon, 28 Aug 2017 15:54:07 +0000 Subject: [PATCH] Add ability to label md(4) devices. This feature comes from the fact that we rely memory-backed md(4) in our build process heavily. However, if the build goes haywire the allocated resources (i.e. swap and memory-backed md(4)'s) need to be purged. It is extremely useful to have ability to attach arbitrary labels to each of the virtual disks so that they can be identified and GC'ed if neecessary. MFC after: 4 weeks Differential Revision: https://reviews.freebsd.org/D10457 --- sbin/mdconfig/mdconfig.8 | 9 ++++++++- sbin/mdconfig/mdconfig.c | 26 ++++++++++++++++++-------- sys/dev/md/md.c | 16 ++++++++++++++++ sys/sys/mdioctl.h | 3 ++- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/sbin/mdconfig/mdconfig.8 b/sbin/mdconfig/mdconfig.8 index a437e40d58ec..cf05d8f43efb 100644 --- a/sbin/mdconfig/mdconfig.8 +++ b/sbin/mdconfig/mdconfig.8 @@ -37,7 +37,7 @@ .\" .\" $FreeBSD$ .\" -.Dd October 10, 2015 +.Dd August 28, 2017 .Dt MDCONFIG 8 .Os .Sh NAME @@ -55,6 +55,7 @@ .Op Fl u Ar unit .Op Fl x Ar sectors/track .Op Fl y Ar heads/cylinder +.Op Fl L Ar label .Nm .Fl d .Fl u Ar unit @@ -189,6 +190,12 @@ and options can be used to specify a synthetic geometry. This is useful for constructing bootable images for later download to other devices. +.It Fl L Ar label +Associate a label (arbitrary string) with the new memory disk. +The label can then be inspected with +.Bd -literal -offset indent +.Nm Fl l v +.Ed .It Fl o Oo Cm no Oc Ns Ar option Set or reset options. .Bl -tag -width indent diff --git a/sbin/mdconfig/mdconfig.c b/sbin/mdconfig/mdconfig.c index 12c2a82c44d7..a76af2c7fdb0 100644 --- a/sbin/mdconfig/mdconfig.c +++ b/sbin/mdconfig/mdconfig.c @@ -79,7 +79,7 @@ usage(void) fprintf(stderr, "usage: mdconfig -a -t type [-n] [-o [no]option] ... [-f file]\n" -" [-s size] [-S sectorsize] [-u unit]\n" +" [-s size] [-S sectorsize] [-u unit] [-L label]\n" " [-x sectors/track] [-y heads/cylinder]\n" " mdconfig -d -u unit [-o [no]force]\n" " mdconfig -r -u unit -s size [-o [no]force]\n" @@ -102,15 +102,17 @@ main(int argc, char **argv) bzero(&mdio, sizeof(mdio)); mdio.md_file = malloc(PATH_MAX); - if (mdio.md_file == NULL) + mdio.md_label = malloc(PATH_MAX); + if (mdio.md_file == NULL || mdio.md_label == NULL) err(1, "could not allocate memory"); vflag = 0; bzero(mdio.md_file, PATH_MAX); + bzero(mdio.md_label, PATH_MAX); if (argc == 1) usage(); - while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:")) != -1) { + while ((ch = getopt(argc, argv, "ab:df:lno:rs:S:t:u:vx:y:L:")) != -1) { switch (ch) { case 'a': if (action != UNSET && action != ATTACH) @@ -243,6 +245,9 @@ main(int argc, char **argv) case 'y': mdio.md_fwheads = strtoul(optarg, &p, 0); break; + case 'L': + strlcpy(mdio.md_label, optarg, PATH_MAX); + break; default: usage(); } @@ -422,7 +427,8 @@ md_list(const char *units, int opt, const char *fflag) struct gclass *gcl; void *sq; int retcode, ffound, ufound; - char *type, *file, *length; + char *length; + const char *type, *file, *label; type = file = length = NULL; @@ -477,10 +483,14 @@ md_list(const char *units, int opt, const char *fflag) printf("\t%s\t", type); if (length != NULL) md_prthumanval(length); - if (file != NULL) { - printf("\t%s", file); - file = NULL; - } + if (file == NULL) + file = "-"; + printf("\t%s", file); + file = NULL; + label = geom_config_get(gc, "label"); + if (label == NULL) + label = ""; + printf("\t%s", label); } opt |= OPT_DONE; if ((opt & OPT_LIST) && !(opt & OPT_VERBOSE)) diff --git a/sys/dev/md/md.c b/sys/dev/md/md.c index 6a85dc449bd7..d9c41f130235 100644 --- a/sys/dev/md/md.c +++ b/sys/dev/md/md.c @@ -226,6 +226,7 @@ struct md_s { /* MD_VNODE related fields */ struct vnode *vnode; char file[PATH_MAX]; + char label[PATH_MAX]; struct ucred *cred; /* MD_SWAP related fields */ @@ -1645,6 +1646,11 @@ xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread } if (sc == NULL) return (error); + if (mdio->md_label != NULL) + error = copyinstr(mdio->md_label, sc->label, + sizeof(sc->label), NULL); + if (error != 0) + goto err_after_new; if (mdio->md_options & MD_AUTOUNIT) mdio->md_unit = sc->unit; sc->mediasize = mdio->md_mediasize; @@ -1676,6 +1682,7 @@ xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread error = mdcreate_null(sc, mdio, td); break; } +err_after_new: if (error != 0) { mddestroy(sc, td); return (error); @@ -1721,6 +1728,11 @@ xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread mdio->md_options = sc->flags; mdio->md_mediasize = sc->mediasize; mdio->md_sectorsize = sc->sectorsize; + error = 0; + if (mdio->md_label != NULL) { + error = copyout(sc->label, mdio->md_label, + strlen(sc->label) + 1); + } if (sc->type == MD_VNODE || (sc->type == MD_PRELOAD && mdio->md_file != NULL)) error = copyout(sc->file, mdio->md_file, @@ -1873,6 +1885,7 @@ g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, if ((mp->type == MD_VNODE && mp->vnode != NULL) || (mp->type == MD_PRELOAD && mp->file[0] != '\0')) sbuf_printf(sb, " file %s", mp->file); + sbuf_printf(sb, " label %s", mp->label); } else { sbuf_printf(sb, "%s%d\n", indent, mp->unit); @@ -1897,6 +1910,9 @@ g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, g_conf_printf_escaped(sb, "%s", mp->file); sbuf_printf(sb, "\n"); } + sbuf_printf(sb, "%s\n"); } } } diff --git a/sys/sys/mdioctl.h b/sys/sys/mdioctl.h index 9a7642923ccd..8f6a2d7e9d31 100644 --- a/sys/sys/mdioctl.h +++ b/sys/sys/mdioctl.h @@ -49,7 +49,7 @@ enum md_types {MD_MALLOC, MD_PRELOAD, MD_VNODE, MD_SWAP, MD_NULL}; * Ioctl definitions for memory disk pseudo-device. */ -#define MDNPAD 97 +#define MDNPAD 96 struct md_ioctl { unsigned md_version; /* Structure layout version */ unsigned md_unit; /* unit number */ @@ -61,6 +61,7 @@ struct md_ioctl { u_int64_t md_base; /* base address */ int md_fwheads; /* firmware heads */ int md_fwsectors; /* firmware sectors */ + char *md_label; /* label of the device */ int md_pad[MDNPAD]; /* padding for future ideas */ };