xref: /freebsd/sys/geom/raid/g_raid_ctl.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
189b17223SAlexander Motin /*-
2*4d846d26SWarner 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/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 static struct g_raid_softc *
5189b17223SAlexander Motin g_raid_find_node(struct g_class *mp, const char *name)
5289b17223SAlexander Motin {
5389b17223SAlexander Motin 	struct g_raid_softc *sc;
5489b17223SAlexander Motin 	struct g_geom *gp;
556871a543SAlexander Motin 	struct g_provider *pp;
566871a543SAlexander Motin 	struct g_raid_volume *vol;
5789b17223SAlexander Motin 
586871a543SAlexander Motin 	/* Look for geom with specified name. */
5989b17223SAlexander Motin 	LIST_FOREACH(gp, &mp->geom, geom) {
6089b17223SAlexander Motin 		sc = gp->softc;
6189b17223SAlexander Motin 		if (sc == NULL)
6289b17223SAlexander Motin 			continue;
6389b17223SAlexander Motin 		if (sc->sc_stopping != 0)
6489b17223SAlexander Motin 			continue;
6589b17223SAlexander Motin 		if (strcasecmp(sc->sc_name, name) == 0)
6689b17223SAlexander Motin 			return (sc);
6789b17223SAlexander Motin 	}
686871a543SAlexander Motin 
696871a543SAlexander Motin 	/* Look for provider with specified name. */
706871a543SAlexander Motin 	LIST_FOREACH(gp, &mp->geom, geom) {
716871a543SAlexander Motin 		sc = gp->softc;
726871a543SAlexander Motin 		if (sc == NULL)
736871a543SAlexander Motin 			continue;
746871a543SAlexander Motin 		if (sc->sc_stopping != 0)
756871a543SAlexander Motin 			continue;
766871a543SAlexander Motin 		LIST_FOREACH(pp, &gp->provider, provider) {
776871a543SAlexander Motin 			if (strcmp(pp->name, name) == 0)
786871a543SAlexander Motin 				return (sc);
796871a543SAlexander Motin 			if (strncmp(pp->name, "raid/", 5) == 0 &&
806871a543SAlexander Motin 			    strcmp(pp->name + 5, name) == 0)
816871a543SAlexander Motin 				return (sc);
826871a543SAlexander Motin 		}
836871a543SAlexander Motin 	}
846871a543SAlexander Motin 
856871a543SAlexander Motin 	/* Look for volume with specified name. */
866871a543SAlexander Motin 	LIST_FOREACH(gp, &mp->geom, geom) {
876871a543SAlexander Motin 		sc = gp->softc;
886871a543SAlexander Motin 		if (sc == NULL)
896871a543SAlexander Motin 			continue;
906871a543SAlexander Motin 		if (sc->sc_stopping != 0)
916871a543SAlexander Motin 			continue;
926871a543SAlexander Motin 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
936871a543SAlexander Motin 			if (strcmp(vol->v_name, name) == 0)
946871a543SAlexander Motin 				return (sc);
956871a543SAlexander Motin 		}
966871a543SAlexander Motin 	}
9789b17223SAlexander Motin 	return (NULL);
9889b17223SAlexander Motin }
9989b17223SAlexander Motin 
10089b17223SAlexander Motin static void
10189b17223SAlexander Motin g_raid_ctl_label(struct gctl_req *req, struct g_class *mp)
10289b17223SAlexander Motin {
10389b17223SAlexander Motin 	struct g_geom *geom;
10489b17223SAlexander Motin 	struct g_raid_softc *sc;
10589b17223SAlexander Motin 	const char *format;
10689b17223SAlexander Motin 	int *nargs;
10789b17223SAlexander Motin 	int crstatus, ctlstatus;
10889b17223SAlexander Motin 	char buf[64];
10989b17223SAlexander Motin 
11089b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
11189b17223SAlexander Motin 	if (nargs == NULL) {
11289b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "nargs");
11389b17223SAlexander Motin 		return;
11489b17223SAlexander Motin 	}
11589b17223SAlexander Motin 	if (*nargs < 4) {
11689b17223SAlexander Motin 		gctl_error(req, "Invalid number of arguments.");
11789b17223SAlexander Motin 		return;
11889b17223SAlexander Motin 	}
11989b17223SAlexander Motin 	format = gctl_get_asciiparam(req, "arg0");
12089b17223SAlexander Motin 	if (format == NULL) {
121310aef32SPedro F. Giffuni 		gctl_error(req, "No format received.");
12289b17223SAlexander Motin 		return;
12389b17223SAlexander Motin 	}
1248df8e26aSAlexander Motin 	crstatus = g_raid_create_node_format(format, req, &geom);
12589b17223SAlexander Motin 	if (crstatus == G_RAID_MD_TASTE_FAIL) {
12689b17223SAlexander Motin 		gctl_error(req, "Failed to create array with format '%s'.",
12789b17223SAlexander Motin 		    format);
12889b17223SAlexander Motin 		return;
12989b17223SAlexander Motin 	}
13089b17223SAlexander Motin 	sc = (struct g_raid_softc *)geom->softc;
13189b17223SAlexander Motin 	g_topology_unlock();
13289b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
13389b17223SAlexander Motin 	ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
13489b17223SAlexander Motin 	if (ctlstatus < 0) {
13589b17223SAlexander Motin 		gctl_error(req, "Command failed: %d.", ctlstatus);
13689b17223SAlexander Motin 		if (crstatus == G_RAID_MD_TASTE_NEW)
13789b17223SAlexander Motin 			g_raid_destroy_node(sc, 0);
13889b17223SAlexander Motin 	} else {
13989b17223SAlexander Motin 		if (crstatus == G_RAID_MD_TASTE_NEW)
14089b17223SAlexander Motin 			snprintf(buf, sizeof(buf), "%s created\n", sc->sc_name);
14189b17223SAlexander Motin 		else
14289b17223SAlexander Motin 			snprintf(buf, sizeof(buf), "%s reused\n", sc->sc_name);
14389b17223SAlexander Motin 		gctl_set_param_err(req, "output", buf, strlen(buf) + 1);
14489b17223SAlexander Motin 	}
14589b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
14689b17223SAlexander Motin 	g_topology_lock();
14789b17223SAlexander Motin }
14889b17223SAlexander Motin 
14989b17223SAlexander Motin static void
15089b17223SAlexander Motin g_raid_ctl_stop(struct gctl_req *req, struct g_class *mp)
15189b17223SAlexander Motin {
15289b17223SAlexander Motin 	struct g_raid_softc *sc;
15389b17223SAlexander Motin 	const char *nodename;
15489b17223SAlexander Motin 	int *nargs, *force;
15589b17223SAlexander Motin 	int error, how;
15689b17223SAlexander Motin 
15789b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
15889b17223SAlexander Motin 	if (nargs == NULL) {
15989b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "nargs");
16089b17223SAlexander Motin 		return;
16189b17223SAlexander Motin 	}
16289b17223SAlexander Motin 	if (*nargs != 1) {
16389b17223SAlexander Motin 		gctl_error(req, "Invalid number of arguments.");
16489b17223SAlexander Motin 		return;
16589b17223SAlexander Motin 	}
16689b17223SAlexander Motin 	nodename = gctl_get_asciiparam(req, "arg0");
16789b17223SAlexander Motin 	if (nodename == NULL) {
168310aef32SPedro F. Giffuni 		gctl_error(req, "No array name received.");
16989b17223SAlexander Motin 		return;
17089b17223SAlexander Motin 	}
17189b17223SAlexander Motin 	sc = g_raid_find_node(mp, nodename);
17289b17223SAlexander Motin 	if (sc == NULL) {
17389b17223SAlexander Motin 		gctl_error(req, "Array '%s' not found.", nodename);
17489b17223SAlexander Motin 		return;
17589b17223SAlexander Motin 	}
17689b17223SAlexander Motin 	force = gctl_get_paraml(req, "force", sizeof(*force));
17789b17223SAlexander Motin 	if (force != NULL && *force)
17889b17223SAlexander Motin 		how = G_RAID_DESTROY_HARD;
17989b17223SAlexander Motin 	else
18089b17223SAlexander Motin 		how = G_RAID_DESTROY_SOFT;
18189b17223SAlexander Motin 	g_topology_unlock();
18289b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
18389b17223SAlexander Motin 	error = g_raid_destroy(sc, how);
18489b17223SAlexander Motin 	if (error != 0)
1858531bb3fSAlexander Motin 		gctl_error(req, "Array is busy.");
18689b17223SAlexander Motin 	g_topology_lock();
18789b17223SAlexander Motin }
18889b17223SAlexander Motin 
18989b17223SAlexander Motin static void
19089b17223SAlexander Motin g_raid_ctl_other(struct gctl_req *req, struct g_class *mp)
19189b17223SAlexander Motin {
19289b17223SAlexander Motin 	struct g_raid_softc *sc;
19389b17223SAlexander Motin 	const char *nodename;
19489b17223SAlexander Motin 	int *nargs;
19589b17223SAlexander Motin 	int ctlstatus;
19689b17223SAlexander Motin 
19789b17223SAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
19889b17223SAlexander Motin 	if (nargs == NULL) {
19989b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "nargs");
20089b17223SAlexander Motin 		return;
20189b17223SAlexander Motin 	}
20289b17223SAlexander Motin 	if (*nargs < 1) {
20389b17223SAlexander Motin 		gctl_error(req, "Invalid number of arguments.");
20489b17223SAlexander Motin 		return;
20589b17223SAlexander Motin 	}
20689b17223SAlexander Motin 	nodename = gctl_get_asciiparam(req, "arg0");
20789b17223SAlexander Motin 	if (nodename == NULL) {
208310aef32SPedro F. Giffuni 		gctl_error(req, "No array name received.");
20989b17223SAlexander Motin 		return;
21089b17223SAlexander Motin 	}
21189b17223SAlexander Motin 	sc = g_raid_find_node(mp, nodename);
21289b17223SAlexander Motin 	if (sc == NULL) {
21389b17223SAlexander Motin 		gctl_error(req, "Array '%s' not found.", nodename);
21489b17223SAlexander Motin 		return;
21589b17223SAlexander Motin 	}
21689b17223SAlexander Motin 	g_topology_unlock();
21789b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
21889b17223SAlexander Motin 	if (sc->sc_md != NULL) {
21989b17223SAlexander Motin 		ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
22089b17223SAlexander Motin 		if (ctlstatus < 0)
22189b17223SAlexander Motin 			gctl_error(req, "Command failed: %d.", ctlstatus);
22289b17223SAlexander Motin 	}
22389b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
22489b17223SAlexander Motin 	g_topology_lock();
22589b17223SAlexander Motin }
22689b17223SAlexander Motin 
22789b17223SAlexander Motin void
22889b17223SAlexander Motin g_raid_ctl(struct gctl_req *req, struct g_class *mp, const char *verb)
22989b17223SAlexander Motin {
23089b17223SAlexander Motin 	uint32_t *version;
23189b17223SAlexander Motin 
23289b17223SAlexander Motin 	g_topology_assert();
23389b17223SAlexander Motin 
23489b17223SAlexander Motin 	version = gctl_get_paraml(req, "version", sizeof(*version));
23589b17223SAlexander Motin 	if (version == NULL) {
23689b17223SAlexander Motin 		gctl_error(req, "No '%s' argument.", "version");
23789b17223SAlexander Motin 		return;
23889b17223SAlexander Motin 	}
23989b17223SAlexander Motin 	if (*version != G_RAID_VERSION) {
24089b17223SAlexander Motin 		gctl_error(req, "Userland and kernel parts are out of sync.");
24189b17223SAlexander Motin 		return;
24289b17223SAlexander Motin 	}
24389b17223SAlexander Motin 
24489b17223SAlexander Motin 	if (strcmp(verb, "label") == 0)
24589b17223SAlexander Motin 		g_raid_ctl_label(req, mp);
24689b17223SAlexander Motin 	else if (strcmp(verb, "stop") == 0)
24789b17223SAlexander Motin 		g_raid_ctl_stop(req, mp);
24889b17223SAlexander Motin 	else
24989b17223SAlexander Motin 		g_raid_ctl_other(req, mp);
25089b17223SAlexander Motin }
251