g_raid_ctl.c revision 234940
1/*-
2 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/raid/g_raid_ctl.c 234940 2012-05-03 05:32:56Z mav $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/module.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/bio.h>
37#include <sys/sysctl.h>
38#include <sys/malloc.h>
39#include <sys/bitstring.h>
40#include <vm/uma.h>
41#include <machine/atomic.h>
42#include <geom/geom.h>
43#include <sys/proc.h>
44#include <sys/kthread.h>
45#include <geom/raid/g_raid.h>
46#include "g_raid_md_if.h"
47
48
49static struct g_raid_softc *
50g_raid_find_node(struct g_class *mp, const char *name)
51{
52	struct g_raid_softc *sc;
53	struct g_geom *gp;
54
55	LIST_FOREACH(gp, &mp->geom, geom) {
56		sc = gp->softc;
57		if (sc == NULL)
58			continue;
59		if (sc->sc_stopping != 0)
60			continue;
61		if (strcasecmp(sc->sc_name, name) == 0)
62			return (sc);
63	}
64	return (NULL);
65}
66
67static void
68g_raid_ctl_label(struct gctl_req *req, struct g_class *mp)
69{
70	struct g_geom *geom;
71	struct g_raid_softc *sc;
72	const char *format;
73	int *nargs;
74	int crstatus, ctlstatus;
75	char buf[64];
76
77	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
78	if (nargs == NULL) {
79		gctl_error(req, "No '%s' argument.", "nargs");
80		return;
81	}
82	if (*nargs < 4) {
83		gctl_error(req, "Invalid number of arguments.");
84		return;
85	}
86	format = gctl_get_asciiparam(req, "arg0");
87	if (format == NULL) {
88		gctl_error(req, "No format recieved.");
89		return;
90	}
91	crstatus = g_raid_create_node_format(format, req, &geom);
92	if (crstatus == G_RAID_MD_TASTE_FAIL) {
93		gctl_error(req, "Failed to create array with format '%s'.",
94		    format);
95		return;
96	}
97	sc = (struct g_raid_softc *)geom->softc;
98	g_topology_unlock();
99	sx_xlock(&sc->sc_lock);
100	ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
101	if (ctlstatus < 0) {
102		gctl_error(req, "Command failed: %d.", ctlstatus);
103		if (crstatus == G_RAID_MD_TASTE_NEW)
104			g_raid_destroy_node(sc, 0);
105	} else {
106		if (crstatus == G_RAID_MD_TASTE_NEW)
107			snprintf(buf, sizeof(buf), "%s created\n", sc->sc_name);
108		else
109			snprintf(buf, sizeof(buf), "%s reused\n", sc->sc_name);
110		gctl_set_param_err(req, "output", buf, strlen(buf) + 1);
111	}
112	sx_xunlock(&sc->sc_lock);
113	g_topology_lock();
114}
115
116static void
117g_raid_ctl_stop(struct gctl_req *req, struct g_class *mp)
118{
119	struct g_raid_softc *sc;
120	const char *nodename;
121	int *nargs, *force;
122	int error, how;
123
124	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
125	if (nargs == NULL) {
126		gctl_error(req, "No '%s' argument.", "nargs");
127		return;
128	}
129	if (*nargs != 1) {
130		gctl_error(req, "Invalid number of arguments.");
131		return;
132	}
133	nodename = gctl_get_asciiparam(req, "arg0");
134	if (nodename == NULL) {
135		gctl_error(req, "No array name recieved.");
136		return;
137	}
138	sc = g_raid_find_node(mp, nodename);
139	if (sc == NULL) {
140		gctl_error(req, "Array '%s' not found.", nodename);
141		return;
142	}
143	force = gctl_get_paraml(req, "force", sizeof(*force));
144	if (force != NULL && *force)
145		how = G_RAID_DESTROY_HARD;
146	else
147		how = G_RAID_DESTROY_SOFT;
148	g_topology_unlock();
149	sx_xlock(&sc->sc_lock);
150	error = g_raid_destroy(sc, how);
151	if (error != 0)
152		sx_xunlock(&sc->sc_lock);
153	g_topology_lock();
154}
155
156static void
157g_raid_ctl_other(struct gctl_req *req, struct g_class *mp)
158{
159	struct g_raid_softc *sc;
160	const char *nodename;
161	int *nargs;
162	int ctlstatus;
163
164	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
165	if (nargs == NULL) {
166		gctl_error(req, "No '%s' argument.", "nargs");
167		return;
168	}
169	if (*nargs < 1) {
170		gctl_error(req, "Invalid number of arguments.");
171		return;
172	}
173	nodename = gctl_get_asciiparam(req, "arg0");
174	if (nodename == NULL) {
175		gctl_error(req, "No array name recieved.");
176		return;
177	}
178	sc = g_raid_find_node(mp, nodename);
179	if (sc == NULL) {
180		gctl_error(req, "Array '%s' not found.", nodename);
181		return;
182	}
183	g_topology_unlock();
184	sx_xlock(&sc->sc_lock);
185	if (sc->sc_md != NULL) {
186		ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
187		if (ctlstatus < 0)
188			gctl_error(req, "Command failed: %d.", ctlstatus);
189	}
190	sx_xunlock(&sc->sc_lock);
191	g_topology_lock();
192}
193
194void
195g_raid_ctl(struct gctl_req *req, struct g_class *mp, const char *verb)
196{
197	uint32_t *version;
198
199	g_topology_assert();
200
201	version = gctl_get_paraml(req, "version", sizeof(*version));
202	if (version == NULL) {
203		gctl_error(req, "No '%s' argument.", "version");
204		return;
205	}
206	if (*version != G_RAID_VERSION) {
207		gctl_error(req, "Userland and kernel parts are out of sync.");
208		return;
209	}
210
211	if (strcmp(verb, "label") == 0)
212		g_raid_ctl_label(req, mp);
213	else if (strcmp(verb, "stop") == 0)
214		g_raid_ctl_stop(req, mp);
215	else
216		g_raid_ctl_other(req, mp);
217}
218