xref: /freebsd/sys/geom/raid/g_raid_ctl.c (revision 3728855a0f66545c4aa90dc0d478dc403bdfa667)
189b17223SAlexander Motin /*-
2*3728855aSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*3728855aSPedro 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/cdefs.h>
3089b17223SAlexander Motin __FBSDID("$FreeBSD$");
3189b17223SAlexander Motin 
3289b17223SAlexander Motin #include <sys/param.h>
3389b17223SAlexander Motin #include <sys/systm.h>
3489b17223SAlexander Motin #include <sys/kernel.h>
3589b17223SAlexander Motin #include <sys/module.h>
3689b17223SAlexander Motin #include <sys/lock.h>
3789b17223SAlexander Motin #include <sys/mutex.h>
3889b17223SAlexander Motin #include <sys/bio.h>
3989b17223SAlexander Motin #include <sys/sysctl.h>
4089b17223SAlexander Motin #include <sys/malloc.h>
4189b17223SAlexander Motin #include <sys/bitstring.h>
4289b17223SAlexander Motin #include <vm/uma.h>
4389b17223SAlexander Motin #include <machine/atomic.h>
4489b17223SAlexander Motin #include <geom/geom.h>
4589b17223SAlexander Motin #include <sys/proc.h>
4689b17223SAlexander Motin #include <sys/kthread.h>
4789b17223SAlexander Motin #include <geom/raid/g_raid.h>
4889b17223SAlexander Motin #include "g_raid_md_if.h"
4989b17223SAlexander Motin 
5089b17223SAlexander Motin 
5189b17223SAlexander Motin static struct g_raid_softc *
5289b17223SAlexander Motin g_raid_find_node(struct g_class *mp, const char *name)
5389b17223SAlexander Motin {
5489b17223SAlexander Motin 	struct g_raid_softc *sc;
5589b17223SAlexander Motin 	struct g_geom *gp;
566871a543SAlexander Motin 	struct g_provider *pp;
576871a543SAlexander Motin 	struct g_raid_volume *vol;
5889b17223SAlexander Motin 
596871a543SAlexander Motin 	/* Look for geom with specified name. */
6089b17223SAlexander Motin 	LIST_FOREACH(gp, &mp->geom, geom) {
6189b17223SAlexander Motin 		sc = gp->softc;
6289b17223SAlexander Motin 		if (sc == NULL)
6389b17223SAlexander Motin 			continue;
6489b17223SAlexander Motin 		if (sc->sc_stopping != 0)
6589b17223SAlexander Motin 			continue;
6689b17223SAlexander Motin 		if (strcasecmp(sc->sc_name, name) == 0)
6789b17223SAlexander Motin 			return (sc);
6889b17223SAlexander Motin 	}
696871a543SAlexander Motin 
706871a543SAlexander Motin 	/* Look for provider with specified name. */
716871a543SAlexander Motin 	LIST_FOREACH(gp, &mp->geom, geom) {
726871a543SAlexander Motin 		sc = gp->softc;
736871a543SAlexander Motin 		if (sc == NULL)
746871a543SAlexander Motin 			continue;
756871a543SAlexander Motin 		if (sc->sc_stopping != 0)
766871a543SAlexander Motin 			continue;
776871a543SAlexander Motin 		LIST_FOREACH(pp, &gp->provider, provider) {
786871a543SAlexander Motin 			if (strcmp(pp->name, name) == 0)
796871a543SAlexander Motin 				return (sc);
806871a543SAlexander Motin 			if (strncmp(pp->name, "raid/", 5) == 0 &&
816871a543SAlexander Motin 			    strcmp(pp->name + 5, name) == 0)
826871a543SAlexander Motin 				return (sc);
836871a543SAlexander Motin 		}
846871a543SAlexander Motin 	}
856871a543SAlexander Motin 
866871a543SAlexander Motin 	/* Look for volume with specified name. */
876871a543SAlexander Motin 	LIST_FOREACH(gp, &mp->geom, geom) {
886871a543SAlexander Motin 		sc = gp->softc;
896871a543SAlexander Motin 		if (sc == NULL)
906871a543SAlexander Motin 			continue;
916871a543SAlexander Motin 		if (sc->sc_stopping != 0)
926871a543SAlexander Motin 			continue;
936871a543SAlexander Motin 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
946871a543SAlexander Motin 			if (strcmp(vol->v_name, name) == 0)
956871a543SAlexander Motin 				return (sc);
966871a543SAlexander Motin 		}
976871a543SAlexander Motin 	}
9889b17223SAlexander Motin 	return (NULL);
9989b17223SAlexander Motin }
10089b17223SAlexander Motin 
10189b17223SAlexander Motin static void
10289b17223SAlexander Motin g_raid_ctl_label(struct gctl_req *req, struct g_class *mp)
10389b17223SAlexander Motin {
10489b17223SAlexander Motin 	struct g_geom *geom;
10589b17223SAlexander Motin 	struct g_raid_softc *sc;
10689b17223SAlexander Motin 	const char *format;
10789b17223SAlexander Motin 	int *nargs;
10889b17223SAlexander Motin 	int crstatus, ctlstatus;
10989b17223SAlexander Motin 	char buf[64];
11089b17223SAlexander Motin 
11189b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
11289b17223SAlexander Motin 	if (nargs == NULL) {
11389b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "nargs");
11489b17223SAlexander Motin 		return;
11589b17223SAlexander Motin 	}
11689b17223SAlexander Motin 	if (*nargs < 4) {
11789b17223SAlexander Motin 		gctl_error(req, "Invalid number of arguments.");
11889b17223SAlexander Motin 		return;
11989b17223SAlexander Motin 	}
12089b17223SAlexander Motin 	format = gctl_get_asciiparam(req, "arg0");
12189b17223SAlexander Motin 	if (format == NULL) {
122310aef32SPedro F. Giffuni 		gctl_error(req, "No format received.");
12389b17223SAlexander Motin 		return;
12489b17223SAlexander Motin 	}
1258df8e26aSAlexander Motin 	crstatus = g_raid_create_node_format(format, req, &geom);
12689b17223SAlexander Motin 	if (crstatus == G_RAID_MD_TASTE_FAIL) {
12789b17223SAlexander Motin 		gctl_error(req, "Failed to create array with format '%s'.",
12889b17223SAlexander Motin 		    format);
12989b17223SAlexander Motin 		return;
13089b17223SAlexander Motin 	}
13189b17223SAlexander Motin 	sc = (struct g_raid_softc *)geom->softc;
13289b17223SAlexander Motin 	g_topology_unlock();
13389b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
13489b17223SAlexander Motin 	ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
13589b17223SAlexander Motin 	if (ctlstatus < 0) {
13689b17223SAlexander Motin 		gctl_error(req, "Command failed: %d.", ctlstatus);
13789b17223SAlexander Motin 		if (crstatus == G_RAID_MD_TASTE_NEW)
13889b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
13989b17223SAlexander Motin 	} else {
14089b17223SAlexander Motin 		if (crstatus == G_RAID_MD_TASTE_NEW)
14189b17223SAlexander Motin 			snprintf(buf, sizeof(buf), "%s created\n", sc->sc_name);
14289b17223SAlexander Motin 		else
14389b17223SAlexander Motin 			snprintf(buf, sizeof(buf), "%s reused\n", sc->sc_name);
14489b17223SAlexander Motin 		gctl_set_param_err(req, "output", buf, strlen(buf) + 1);
14589b17223SAlexander Motin 	}
14689b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
14789b17223SAlexander Motin 	g_topology_lock();
14889b17223SAlexander Motin }
14989b17223SAlexander Motin 
15089b17223SAlexander Motin static void
15189b17223SAlexander Motin g_raid_ctl_stop(struct gctl_req *req, struct g_class *mp)
15289b17223SAlexander Motin {
15389b17223SAlexander Motin 	struct g_raid_softc *sc;
15489b17223SAlexander Motin 	const char *nodename;
15589b17223SAlexander Motin 	int *nargs, *force;
15689b17223SAlexander Motin 	int error, how;
15789b17223SAlexander Motin 
15889b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
15989b17223SAlexander Motin 	if (nargs == NULL) {
16089b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "nargs");
16189b17223SAlexander Motin 		return;
16289b17223SAlexander Motin 	}
16389b17223SAlexander Motin 	if (*nargs != 1) {
16489b17223SAlexander Motin 		gctl_error(req, "Invalid number of arguments.");
16589b17223SAlexander Motin 		return;
16689b17223SAlexander Motin 	}
16789b17223SAlexander Motin 	nodename = gctl_get_asciiparam(req, "arg0");
16889b17223SAlexander Motin 	if (nodename == NULL) {
169310aef32SPedro F. Giffuni 		gctl_error(req, "No array name received.");
17089b17223SAlexander Motin 		return;
17189b17223SAlexander Motin 	}
17289b17223SAlexander Motin 	sc = g_raid_find_node(mp, nodename);
17389b17223SAlexander Motin 	if (sc == NULL) {
17489b17223SAlexander Motin 		gctl_error(req, "Array '%s' not found.", nodename);
17589b17223SAlexander Motin 		return;
17689b17223SAlexander Motin 	}
17789b17223SAlexander Motin 	force = gctl_get_paraml(req, "force", sizeof(*force));
17889b17223SAlexander Motin 	if (force != NULL && *force)
17989b17223SAlexander Motin 		how = G_RAID_DESTROY_HARD;
18089b17223SAlexander Motin 	else
18189b17223SAlexander Motin 		how = G_RAID_DESTROY_SOFT;
18289b17223SAlexander Motin 	g_topology_unlock();
18389b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
18489b17223SAlexander Motin 	error = g_raid_destroy(sc, how);
18589b17223SAlexander Motin 	if (error != 0)
1868531bb3fSAlexander Motin 		gctl_error(req, "Array is busy.");
18789b17223SAlexander Motin 	g_topology_lock();
18889b17223SAlexander Motin }
18989b17223SAlexander Motin 
19089b17223SAlexander Motin static void
19189b17223SAlexander Motin g_raid_ctl_other(struct gctl_req *req, struct g_class *mp)
19289b17223SAlexander Motin {
19389b17223SAlexander Motin 	struct g_raid_softc *sc;
19489b17223SAlexander Motin 	const char *nodename;
19589b17223SAlexander Motin 	int *nargs;
19689b17223SAlexander Motin 	int ctlstatus;
19789b17223SAlexander Motin 
19889b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
19989b17223SAlexander Motin 	if (nargs == NULL) {
20089b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "nargs");
20189b17223SAlexander Motin 		return;
20289b17223SAlexander Motin 	}
20389b17223SAlexander Motin 	if (*nargs < 1) {
20489b17223SAlexander Motin 		gctl_error(req, "Invalid number of arguments.");
20589b17223SAlexander Motin 		return;
20689b17223SAlexander Motin 	}
20789b17223SAlexander Motin 	nodename = gctl_get_asciiparam(req, "arg0");
20889b17223SAlexander Motin 	if (nodename == NULL) {
209310aef32SPedro F. Giffuni 		gctl_error(req, "No array name received.");
21089b17223SAlexander Motin 		return;
21189b17223SAlexander Motin 	}
21289b17223SAlexander Motin 	sc = g_raid_find_node(mp, nodename);
21389b17223SAlexander Motin 	if (sc == NULL) {
21489b17223SAlexander Motin 		gctl_error(req, "Array '%s' not found.", nodename);
21589b17223SAlexander Motin 		return;
21689b17223SAlexander Motin 	}
21789b17223SAlexander Motin 	g_topology_unlock();
21889b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
21989b17223SAlexander Motin 	if (sc->sc_md != NULL) {
22089b17223SAlexander Motin 		ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
22189b17223SAlexander Motin 		if (ctlstatus < 0)
22289b17223SAlexander Motin 			gctl_error(req, "Command failed: %d.", ctlstatus);
22389b17223SAlexander Motin 	}
22489b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
22589b17223SAlexander Motin 	g_topology_lock();
22689b17223SAlexander Motin }
22789b17223SAlexander Motin 
22889b17223SAlexander Motin void
22989b17223SAlexander Motin g_raid_ctl(struct gctl_req *req, struct g_class *mp, const char *verb)
23089b17223SAlexander Motin {
23189b17223SAlexander Motin 	uint32_t *version;
23289b17223SAlexander Motin 
23389b17223SAlexander Motin 	g_topology_assert();
23489b17223SAlexander Motin 
23589b17223SAlexander Motin 	version = gctl_get_paraml(req, "version", sizeof(*version));
23689b17223SAlexander Motin 	if (version == NULL) {
23789b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "version");
23889b17223SAlexander Motin 		return;
23989b17223SAlexander Motin 	}
24089b17223SAlexander Motin 	if (*version != G_RAID_VERSION) {
24189b17223SAlexander Motin 		gctl_error(req, "Userland and kernel parts are out of sync.");
24289b17223SAlexander Motin 		return;
24389b17223SAlexander Motin 	}
24489b17223SAlexander Motin 
24589b17223SAlexander Motin 	if (strcmp(verb, "label") == 0)
24689b17223SAlexander Motin 		g_raid_ctl_label(req, mp);
24789b17223SAlexander Motin 	else if (strcmp(verb, "stop") == 0)
24889b17223SAlexander Motin 		g_raid_ctl_stop(req, mp);
24989b17223SAlexander Motin 	else
25089b17223SAlexander Motin 		g_raid_ctl_other(req, mp);
25189b17223SAlexander Motin }
252