Add gmirror create subcommand, alike to gstripe, gconcat, etc.

It is quite specific mode of operation without storing on-disk metadata.
It can be useful in some cases in combination with some external control
tools handling mirror creation and disks hot-plug.

MFC after:	2 weeks
Sponsored by:	iXsystems, Inc.
This commit is contained in:
Alexander Motin 2016-11-30 09:27:08 +00:00
parent 14c85e527e
commit b6fe583c55
5 changed files with 268 additions and 15 deletions

View File

@ -79,6 +79,16 @@ struct g_command class_commands[] = {
"[-adfFhnv] [-b balance] [-s slice] name\n"
"[-v] -p priority name prov"
},
{ "create", G_FLAG_VERBOSE, NULL,
{
{ 'b', "balance", GMIRROR_BALANCE, G_TYPE_STRING },
{ 'F', "nofailsync", NULL, G_TYPE_BOOL },
{ 'n', "noautosync", NULL, G_TYPE_BOOL },
{ 's', "slice", GMIRROR_SLICE, G_TYPE_NUMBER },
G_OPT_SENTINEL
},
"[-Fnv] [-b balance] [-s slice] name prov ..."
},
{ "deactivate", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
"[-v] name prov ..."
},

View File

@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd December 27, 2013
.Dd November 27, 2016
.Dt GMIRROR 8
.Os
.Sh NAME
@ -43,6 +43,13 @@
.Op Fl v
.Ar prov ...
.Nm
.Cm create
.Op Fl Fnv
.Op Fl b Ar balance
.Op Fl s Ar slice
.Ar name
.Ar prov ...
.Nm
.Cm configure
.Op Fl adfFhnv
.Op Fl b Ar balance
@ -170,6 +177,12 @@ Defaults to 4096 bytes.
.El
.It Cm clear
Clear metadata on the given providers.
.It Cm create
Similar to
.Cm label,
but creates mirror without storing on-disk metadata in last sector.
This special "manual" operation mode assumes some external control to manage
mirror detection after reboot, device hot-plug and other external events.
.It Cm configure
Configure the given device.
.Pp

View File

@ -700,6 +700,8 @@ g_mirror_clear_metadata(struct g_mirror_disk *disk)
g_topology_assert_not();
sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
if (disk->d_softc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
return (0);
error = g_mirror_write_metadata(disk, NULL);
if (error == 0) {
G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
@ -765,6 +767,8 @@ g_mirror_update_metadata(struct g_mirror_disk *disk)
sc = disk->d_softc;
sx_assert(&sc->sc_lock, SX_LOCKED);
if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
return;
if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0)
g_mirror_fill_metadata(sc, disk, &md);
error = g_mirror_write_metadata(disk, &md);
@ -2942,8 +2946,9 @@ g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
return (error);
}
static struct g_geom *
g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
struct g_geom *
g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md,
u_int type)
{
struct g_mirror_softc *sc;
struct g_geom *gp;
@ -2966,6 +2971,7 @@ g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
gp->access = g_mirror_access;
gp->dumpconf = g_mirror_dumpconf;
sc->sc_type = type;
sc->sc_id = md->md_mid;
sc->sc_slice = md->md_slice;
sc->sc_balance = md->md_balance;
@ -3142,6 +3148,8 @@ g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
sc = gp->softc;
if (sc == NULL)
continue;
if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
continue;
if (sc->sc_sync.ds_geom == gp)
continue;
if (strcmp(md.md_name, sc->sc_name) != 0)
@ -3154,7 +3162,7 @@ g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
break;
}
if (gp == NULL) {
gp = g_mirror_create(mp, &md);
gp = g_mirror_create(mp, &md, G_MIRROR_TYPE_AUTOMATIC);
if (gp == NULL) {
G_MIRROR_DEBUG(0, "Cannot create device %s.",
md.md_name);
@ -3302,6 +3310,19 @@ g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
} else {
g_topology_unlock();
sx_xlock(&sc->sc_lock);
sbuf_printf(sb, "%s<Type>", indent);
switch (sc->sc_type) {
case G_MIRROR_TYPE_AUTOMATIC:
sbuf_printf(sb, "AUTOMATIC");
break;
case G_MIRROR_TYPE_MANUAL:
sbuf_printf(sb, "MANUAL");
break;
default:
sbuf_printf(sb, "UNKNOWN");
break;
}
sbuf_printf(sb, "</Type>\n");
sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);

View File

@ -165,11 +165,15 @@ struct g_mirror_event {
#define G_MIRROR_DEVICE_STATE_STARTING 0
#define G_MIRROR_DEVICE_STATE_RUNNING 1
#define G_MIRROR_TYPE_MANUAL 0
#define G_MIRROR_TYPE_AUTOMATIC 1
/* Bump syncid on first write. */
#define G_MIRROR_BUMP_SYNCID 0x1
/* Bump genid immediately. */
#define G_MIRROR_BUMP_GENID 0x2
struct g_mirror_softc {
u_int sc_type; /* Device type (manual/automatic). */
u_int sc_state; /* Device state. */
uint32_t sc_slice; /* Slice size. */
uint8_t sc_balance; /* Balance algorithm. */
@ -220,7 +224,11 @@ struct g_mirror_softc {
};
#define sc_name sc_geom->name
struct g_mirror_metadata;
u_int g_mirror_ndisks(struct g_mirror_softc *sc, int state);
struct g_geom * g_mirror_create(struct g_class *mp,
const struct g_mirror_metadata *md, u_int type);
#define G_MIRROR_DESTROY_SOFT 0
#define G_MIRROR_DESTROY_DELAYED 1
#define G_MIRROR_DESTROY_HARD 2

View File

@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$");
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/bio.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>
#include <sys/malloc.h>
#include <sys/bitstring.h>
@ -300,6 +301,182 @@ g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp)
sx_xunlock(&sc->sc_lock);
}
static void
g_mirror_create_orphan(struct g_consumer *cp)
{
KASSERT(1 == 0, ("%s called while creating %s.", __func__,
cp->provider->name));
}
static void
g_mirror_ctl_create(struct gctl_req *req, struct g_class *mp)
{
struct g_mirror_metadata md;
struct g_geom *gp;
struct g_consumer *cp;
struct g_provider *pp;
struct g_mirror_softc *sc;
struct sbuf *sb;
const char *name;
char param[16];
int *nargs;
intmax_t *val;
int *ival;
const char *sval;
int bal;
unsigned attached, no, sectorsize;
off_t mediasize;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
gctl_error(req, "No '%s' argument.", "nargs");
return;
}
if (*nargs <= 2) {
gctl_error(req, "Too few arguments.");
return;
}
strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
md.md_version = G_MIRROR_VERSION;
name = gctl_get_asciiparam(req, "arg0");
if (name == NULL) {
gctl_error(req, "No 'arg%u' argument.", 0);
return;
}
strlcpy(md.md_name, name, sizeof(md.md_name));
md.md_mid = arc4random();
md.md_all = *nargs - 1;
md.md_genid = 0;
md.md_syncid = 1;
md.md_sync_offset = 0;
val = gctl_get_paraml(req, "slice", sizeof(*val));
if (val == NULL) {
gctl_error(req, "No slice argument.");
return;
}
md.md_slice = *val;
sval = gctl_get_asciiparam(req, "balance");
if (sval == NULL) {
gctl_error(req, "No balance argument.");
return;
}
bal = balance_id(sval);
if (bal < 0) {
gctl_error(req, "Invalid balance algorithm.");
return;
}
md.md_balance = bal;
md.md_mflags = 0;
md.md_dflags = 0;
ival = gctl_get_paraml(req, "noautosync", sizeof(*ival));
if (ival != NULL && *ival)
md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
ival = gctl_get_paraml(req, "nofailsync", sizeof(*ival));
if (ival != NULL && *ival)
md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
/* These fields not used in manual mode. */
bzero(md.md_provider, sizeof(md.md_provider));
md.md_provsize = 0;
g_topology_lock();
mediasize = OFF_MAX;
sectorsize = 0;
gp = g_new_geomf(mp, "%s", md.md_name);
gp->orphan = g_mirror_create_orphan;
cp = g_new_consumer(gp);
for (no = 1; no < *nargs; no++) {
snprintf(param, sizeof(param), "arg%u", no);
name = gctl_get_asciiparam(req, param);
if (name == NULL) {
gctl_error(req, "No 'arg%u' argument.", no);
err:
g_destroy_consumer(cp);
g_destroy_geom(gp);
g_topology_unlock();
return;
}
if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
name += strlen("/dev/");
pp = g_provider_by_name(name);
if (pp == NULL) {
G_MIRROR_DEBUG(1, "Disk %s is invalid.", name);
gctl_error(req, "Disk %s is invalid.", name);
goto err;
}
g_attach(cp, pp);
if (g_access(cp, 1, 0, 0) != 0) {
G_MIRROR_DEBUG(1, "Can't open disk %s.", name);
gctl_error(req, "Can't open disk %s.", name);
err2:
g_detach(cp);
goto err;
}
if (pp->mediasize == 0 || pp->sectorsize == 0) {
G_MIRROR_DEBUG(1, "Disk %s has no media.", name);
gctl_error(req, "Disk %s has no media.", name);
g_access(cp, -1, 0, 0);
goto err2;
}
if (pp->mediasize < mediasize)
mediasize = pp->mediasize;
if (pp->sectorsize > sectorsize)
sectorsize = pp->sectorsize;
g_access(cp, -1, 0, 0);
g_detach(cp);
}
g_destroy_consumer(cp);
g_destroy_geom(gp);
md.md_mediasize = mediasize;
md.md_sectorsize = sectorsize;
md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
gp = g_mirror_create(mp, &md, G_MIRROR_TYPE_MANUAL);
if (gp == NULL) {
gctl_error(req, "Can't create %s.", md.md_name);
g_topology_unlock();
return;
}
sc = gp->softc;
g_topology_unlock();
sx_xlock(&sc->sc_lock);
sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
sb = sbuf_new_auto();
sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
for (attached = 0, no = 1; no < *nargs; no++) {
snprintf(param, sizeof(param), "arg%u", no);
name = gctl_get_asciiparam(req, param);
if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
name += strlen("/dev/");
pp = g_provider_by_name(name);
if (pp == NULL) {
G_MIRROR_DEBUG(1, "Provider %s disappear?!", name);
sbuf_printf(sb, " %s", name);
continue;
}
md.md_did = arc4random();
md.md_priority = no - 1;
if (g_mirror_add_disk(sc, pp, &md) != 0) {
G_MIRROR_DEBUG(1, "Disk %u (%s) not attached to %s.",
no, pp->name, gp->name);
sbuf_printf(sb, " %s", pp->name);
continue;
}
attached++;
}
sbuf_finish(sb);
sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
if (md.md_all != attached ||
(sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_HARD);
gctl_error(req, "%s", sbuf_data(sb));
} else
sx_xunlock(&sc->sc_lock);
sbuf_delete(sb);
}
static void
g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
{
@ -401,6 +578,7 @@ g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
struct g_provider *provider;
struct g_consumer *consumer;
} *disks;
off_t mdsize;
nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
if (nargs == NULL) {
@ -462,16 +640,6 @@ g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
gctl_error(req, "Unknown provider %s.", name);
continue;
}
if (sc->sc_provider->mediasize >
pp->mediasize - pp->sectorsize) {
gctl_error(req, "Provider %s too small.", name);
continue;
}
if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
gctl_error(req, "Invalid sectorsize of provider %s.",
name);
continue;
}
cp = g_new_consumer(sc->sc_geom);
if (g_attach(cp, pp) != 0) {
g_destroy_consumer(cp);
@ -479,9 +647,40 @@ g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
continue;
}
if (g_access(cp, 0, 1, 1) != 0) {
gctl_error(req, "Cannot access provider %s.", name);
err:
g_detach(cp);
g_destroy_consumer(cp);
gctl_error(req, "Cannot access provider %s.", name);
continue;
}
mdsize = (sc->sc_type == G_MIRROR_TYPE_AUTOMATIC) ?
pp->sectorsize : 0;
if (sc->sc_provider->mediasize > pp->mediasize - mdsize) {
gctl_error(req, "Provider %s too small.", name);
err2:
g_access(cp, 0, -1, -1);
goto err;
}
if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
gctl_error(req, "Invalid sectorsize of provider %s.",
name);
goto err2;
}
if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC) {
g_access(cp, 0, -1, -1);
g_detach(cp);
g_destroy_consumer(cp);
g_topology_unlock();
sc->sc_ndisks++;
g_mirror_fill_metadata(sc, NULL, &md);
md.md_priority = *priority;
if (*inactive)
md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
if (g_mirror_add_disk(sc, pp, &md) != 0) {
sc->sc_ndisks--;
gctl_error(req, "Disk %s not inserted.", name);
}
g_topology_lock();
continue;
}
disks[n].provider = pp;
@ -872,6 +1071,8 @@ g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
g_topology_unlock();
if (strcmp(verb, "configure") == 0)
g_mirror_ctl_configure(req, mp);
else if (strcmp(verb, "create") == 0)
g_mirror_ctl_create(req, mp);
else if (strcmp(verb, "rebuild") == 0)
g_mirror_ctl_rebuild(req, mp);
else if (strcmp(verb, "insert") == 0)