xref: /freebsd/sys/geom/raid/g_raid_ctl.c (revision fdafd315)
189b17223SAlexander Motin /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
33728855aSPedro F. Giffuni  *
489b17223SAlexander Motin  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
589b17223SAlexander Motin  * All rights reserved.
689b17223SAlexander Motin  *
789b17223SAlexander Motin  * Redistribution and use in source and binary forms, with or without
889b17223SAlexander Motin  * modification, are permitted provided that the following conditions
989b17223SAlexander Motin  * are met:
1089b17223SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
1189b17223SAlexander Motin  *    notice, this list of conditions and the following disclaimer.
1289b17223SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
1389b17223SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
1489b17223SAlexander Motin  *    documentation and/or other materials provided with the distribution.
1589b17223SAlexander Motin  *
1689b17223SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1789b17223SAlexander Motin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1889b17223SAlexander Motin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1989b17223SAlexander Motin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2089b17223SAlexander Motin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2189b17223SAlexander Motin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2289b17223SAlexander Motin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2389b17223SAlexander Motin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2489b17223SAlexander Motin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2589b17223SAlexander Motin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2689b17223SAlexander Motin  * SUCH DAMAGE.
2789b17223SAlexander Motin  */
2889b17223SAlexander Motin 
2989b17223SAlexander Motin #include <sys/param.h>
3089b17223SAlexander Motin #include <sys/systm.h>
3189b17223SAlexander Motin #include <sys/kernel.h>
3289b17223SAlexander Motin #include <sys/module.h>
3389b17223SAlexander Motin #include <sys/lock.h>
3489b17223SAlexander Motin #include <sys/mutex.h>
3589b17223SAlexander Motin #include <sys/bio.h>
3689b17223SAlexander Motin #include <sys/sysctl.h>
3789b17223SAlexander Motin #include <sys/malloc.h>
3889b17223SAlexander Motin #include <sys/bitstring.h>
3989b17223SAlexander Motin #include <vm/uma.h>
4089b17223SAlexander Motin #include <machine/atomic.h>
4189b17223SAlexander Motin #include <geom/geom.h>
4289b17223SAlexander Motin #include <sys/proc.h>
4389b17223SAlexander Motin #include <sys/kthread.h>
4489b17223SAlexander Motin #include <geom/raid/g_raid.h>
4589b17223SAlexander Motin #include "g_raid_md_if.h"
4689b17223SAlexander Motin 
4789b17223SAlexander Motin static struct g_raid_softc *
g_raid_find_node(struct g_class * mp,const char * name)4889b17223SAlexander Motin g_raid_find_node(struct g_class *mp, const char *name)
4989b17223SAlexander Motin {
5089b17223SAlexander Motin 	struct g_raid_softc *sc;
5189b17223SAlexander Motin 	struct g_geom *gp;
526871a543SAlexander Motin 	struct g_provider *pp;
536871a543SAlexander Motin 	struct g_raid_volume *vol;
5489b17223SAlexander Motin 
556871a543SAlexander Motin 	/* Look for geom with specified name. */
5689b17223SAlexander Motin 	LIST_FOREACH(gp, &mp->geom, geom) {
5789b17223SAlexander Motin 		sc = gp->softc;
5889b17223SAlexander Motin 		if (sc == NULL)
5989b17223SAlexander Motin 			continue;
6089b17223SAlexander Motin 		if (sc->sc_stopping != 0)
6189b17223SAlexander Motin 			continue;
6289b17223SAlexander Motin 		if (strcasecmp(sc->sc_name, name) == 0)
6389b17223SAlexander Motin 			return (sc);
6489b17223SAlexander Motin 	}
656871a543SAlexander Motin 
666871a543SAlexander Motin 	/* Look for provider with specified name. */
676871a543SAlexander Motin 	LIST_FOREACH(gp, &mp->geom, geom) {
686871a543SAlexander Motin 		sc = gp->softc;
696871a543SAlexander Motin 		if (sc == NULL)
706871a543SAlexander Motin 			continue;
716871a543SAlexander Motin 		if (sc->sc_stopping != 0)
726871a543SAlexander Motin 			continue;
736871a543SAlexander Motin 		LIST_FOREACH(pp, &gp->provider, provider) {
746871a543SAlexander Motin 			if (strcmp(pp->name, name) == 0)
756871a543SAlexander Motin 				return (sc);
766871a543SAlexander Motin 			if (strncmp(pp->name, "raid/", 5) == 0 &&
776871a543SAlexander Motin 			    strcmp(pp->name + 5, name) == 0)
786871a543SAlexander Motin 				return (sc);
796871a543SAlexander Motin 		}
806871a543SAlexander Motin 	}
816871a543SAlexander Motin 
826871a543SAlexander Motin 	/* Look for volume with specified name. */
836871a543SAlexander Motin 	LIST_FOREACH(gp, &mp->geom, geom) {
846871a543SAlexander Motin 		sc = gp->softc;
856871a543SAlexander Motin 		if (sc == NULL)
866871a543SAlexander Motin 			continue;
876871a543SAlexander Motin 		if (sc->sc_stopping != 0)
886871a543SAlexander Motin 			continue;
896871a543SAlexander Motin 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
906871a543SAlexander Motin 			if (strcmp(vol->v_name, name) == 0)
916871a543SAlexander Motin 				return (sc);
926871a543SAlexander Motin 		}
936871a543SAlexander Motin 	}
9489b17223SAlexander Motin 	return (NULL);
9589b17223SAlexander Motin }
9689b17223SAlexander Motin 
9789b17223SAlexander Motin static void
g_raid_ctl_label(struct gctl_req * req,struct g_class * mp)9889b17223SAlexander Motin g_raid_ctl_label(struct gctl_req *req, struct g_class *mp)
9989b17223SAlexander Motin {
10089b17223SAlexander Motin 	struct g_geom *geom;
10189b17223SAlexander Motin 	struct g_raid_softc *sc;
10289b17223SAlexander Motin 	const char *format;
10389b17223SAlexander Motin 	int *nargs;
10489b17223SAlexander Motin 	int crstatus, ctlstatus;
10589b17223SAlexander Motin 	char buf[64];
10689b17223SAlexander Motin 
10789b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
10889b17223SAlexander Motin 	if (nargs == NULL) {
10989b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "nargs");
11089b17223SAlexander Motin 		return;
11189b17223SAlexander Motin 	}
11289b17223SAlexander Motin 	if (*nargs < 4) {
11389b17223SAlexander Motin 		gctl_error(req, "Invalid number of arguments.");
11489b17223SAlexander Motin 		return;
11589b17223SAlexander Motin 	}
11689b17223SAlexander Motin 	format = gctl_get_asciiparam(req, "arg0");
11789b17223SAlexander Motin 	if (format == NULL) {
118310aef32SPedro F. Giffuni 		gctl_error(req, "No format received.");
11989b17223SAlexander Motin 		return;
12089b17223SAlexander Motin 	}
1218df8e26aSAlexander Motin 	crstatus = g_raid_create_node_format(format, req, &geom);
12289b17223SAlexander Motin 	if (crstatus == G_RAID_MD_TASTE_FAIL) {
12389b17223SAlexander Motin 		gctl_error(req, "Failed to create array with format '%s'.",
12489b17223SAlexander Motin 		    format);
12589b17223SAlexander Motin 		return;
12689b17223SAlexander Motin 	}
12789b17223SAlexander Motin 	sc = (struct g_raid_softc *)geom->softc;
12889b17223SAlexander Motin 	g_topology_unlock();
12989b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
13089b17223SAlexander Motin 	ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
13189b17223SAlexander Motin 	if (ctlstatus < 0) {
13289b17223SAlexander Motin 		gctl_error(req, "Command failed: %d.", ctlstatus);
13389b17223SAlexander Motin 		if (crstatus == G_RAID_MD_TASTE_NEW)
13489b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
13589b17223SAlexander Motin 	} else {
13689b17223SAlexander Motin 		if (crstatus == G_RAID_MD_TASTE_NEW)
13789b17223SAlexander Motin 			snprintf(buf, sizeof(buf), "%s created\n", sc->sc_name);
13889b17223SAlexander Motin 		else
13989b17223SAlexander Motin 			snprintf(buf, sizeof(buf), "%s reused\n", sc->sc_name);
14089b17223SAlexander Motin 		gctl_set_param_err(req, "output", buf, strlen(buf) + 1);
14189b17223SAlexander Motin 	}
14289b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
14389b17223SAlexander Motin 	g_topology_lock();
14489b17223SAlexander Motin }
14589b17223SAlexander Motin 
14689b17223SAlexander Motin static void
g_raid_ctl_stop(struct gctl_req * req,struct g_class * mp)14789b17223SAlexander Motin g_raid_ctl_stop(struct gctl_req *req, struct g_class *mp)
14889b17223SAlexander Motin {
14989b17223SAlexander Motin 	struct g_raid_softc *sc;
15089b17223SAlexander Motin 	const char *nodename;
15189b17223SAlexander Motin 	int *nargs, *force;
15289b17223SAlexander Motin 	int error, how;
15389b17223SAlexander Motin 
15489b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
15589b17223SAlexander Motin 	if (nargs == NULL) {
15689b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "nargs");
15789b17223SAlexander Motin 		return;
15889b17223SAlexander Motin 	}
15989b17223SAlexander Motin 	if (*nargs != 1) {
16089b17223SAlexander Motin 		gctl_error(req, "Invalid number of arguments.");
16189b17223SAlexander Motin 		return;
16289b17223SAlexander Motin 	}
16389b17223SAlexander Motin 	nodename = gctl_get_asciiparam(req, "arg0");
16489b17223SAlexander Motin 	if (nodename == NULL) {
165310aef32SPedro F. Giffuni 		gctl_error(req, "No array name received.");
16689b17223SAlexander Motin 		return;
16789b17223SAlexander Motin 	}
16889b17223SAlexander Motin 	sc = g_raid_find_node(mp, nodename);
16989b17223SAlexander Motin 	if (sc == NULL) {
17089b17223SAlexander Motin 		gctl_error(req, "Array '%s' not found.", nodename);
17189b17223SAlexander Motin 		return;
17289b17223SAlexander Motin 	}
17389b17223SAlexander Motin 	force = gctl_get_paraml(req, "force", sizeof(*force));
17489b17223SAlexander Motin 	if (force != NULL && *force)
17589b17223SAlexander Motin 		how = G_RAID_DESTROY_HARD;
17689b17223SAlexander Motin 	else
17789b17223SAlexander Motin 		how = G_RAID_DESTROY_SOFT;
17889b17223SAlexander Motin 	g_topology_unlock();
17989b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
18089b17223SAlexander Motin 	error = g_raid_destroy(sc, how);
18189b17223SAlexander Motin 	if (error != 0)
1828531bb3fSAlexander Motin 		gctl_error(req, "Array is busy.");
18389b17223SAlexander Motin 	g_topology_lock();
18489b17223SAlexander Motin }
18589b17223SAlexander Motin 
18689b17223SAlexander Motin static void
g_raid_ctl_other(struct gctl_req * req,struct g_class * mp)18789b17223SAlexander Motin g_raid_ctl_other(struct gctl_req *req, struct g_class *mp)
18889b17223SAlexander Motin {
18989b17223SAlexander Motin 	struct g_raid_softc *sc;
19089b17223SAlexander Motin 	const char *nodename;
19189b17223SAlexander Motin 	int *nargs;
19289b17223SAlexander Motin 	int ctlstatus;
19389b17223SAlexander Motin 
19489b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
19589b17223SAlexander Motin 	if (nargs == NULL) {
19689b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "nargs");
19789b17223SAlexander Motin 		return;
19889b17223SAlexander Motin 	}
19989b17223SAlexander Motin 	if (*nargs < 1) {
20089b17223SAlexander Motin 		gctl_error(req, "Invalid number of arguments.");
20189b17223SAlexander Motin 		return;
20289b17223SAlexander Motin 	}
20389b17223SAlexander Motin 	nodename = gctl_get_asciiparam(req, "arg0");
20489b17223SAlexander Motin 	if (nodename == NULL) {
205310aef32SPedro F. Giffuni 		gctl_error(req, "No array name received.");
20689b17223SAlexander Motin 		return;
20789b17223SAlexander Motin 	}
20889b17223SAlexander Motin 	sc = g_raid_find_node(mp, nodename);
20989b17223SAlexander Motin 	if (sc == NULL) {
21089b17223SAlexander Motin 		gctl_error(req, "Array '%s' not found.", nodename);
21189b17223SAlexander Motin 		return;
21289b17223SAlexander Motin 	}
21389b17223SAlexander Motin 	g_topology_unlock();
21489b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
21589b17223SAlexander Motin 	if (sc->sc_md != NULL) {
21689b17223SAlexander Motin 		ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
21789b17223SAlexander Motin 		if (ctlstatus < 0)
21889b17223SAlexander Motin 			gctl_error(req, "Command failed: %d.", ctlstatus);
21989b17223SAlexander Motin 	}
22089b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
22189b17223SAlexander Motin 	g_topology_lock();
22289b17223SAlexander Motin }
22389b17223SAlexander Motin 
22489b17223SAlexander Motin void
g_raid_ctl(struct gctl_req * req,struct g_class * mp,const char * verb)22589b17223SAlexander Motin g_raid_ctl(struct gctl_req *req, struct g_class *mp, const char *verb)
22689b17223SAlexander Motin {
22789b17223SAlexander Motin 	uint32_t *version;
22889b17223SAlexander Motin 
22989b17223SAlexander Motin 	g_topology_assert();
23089b17223SAlexander Motin 
23189b17223SAlexander Motin 	version = gctl_get_paraml(req, "version", sizeof(*version));
23289b17223SAlexander Motin 	if (version == NULL) {
23389b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "version");
23489b17223SAlexander Motin 		return;
23589b17223SAlexander Motin 	}
23689b17223SAlexander Motin 	if (*version != G_RAID_VERSION) {
23789b17223SAlexander Motin 		gctl_error(req, "Userland and kernel parts are out of sync.");
23889b17223SAlexander Motin 		return;
23989b17223SAlexander Motin 	}
24089b17223SAlexander Motin 
24189b17223SAlexander Motin 	if (strcmp(verb, "label") == 0)
24289b17223SAlexander Motin 		g_raid_ctl_label(req, mp);
24389b17223SAlexander Motin 	else if (strcmp(verb, "stop") == 0)
24489b17223SAlexander Motin 		g_raid_ctl_stop(req, mp);
24589b17223SAlexander Motin 	else
24689b17223SAlexander Motin 		g_raid_ctl_other(req, mp);
24789b17223SAlexander Motin }
248