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