xref: /freebsd/sys/geom/raid/g_raid_ctl.c (revision 525fe93dc7487a1e63a90f6a2b956abc601963c1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/bio.h>
36 #include <sys/sysctl.h>
37 #include <sys/malloc.h>
38 #include <sys/bitstring.h>
39 #include <vm/uma.h>
40 #include <machine/atomic.h>
41 #include <geom/geom.h>
42 #include <sys/proc.h>
43 #include <sys/kthread.h>
44 #include <geom/raid/g_raid.h>
45 #include "g_raid_md_if.h"
46 
47 static struct g_raid_softc *
48 g_raid_find_node(struct g_class *mp, const char *name)
49 {
50 	struct g_raid_softc *sc;
51 	struct g_geom *gp;
52 	struct g_provider *pp;
53 	struct g_raid_volume *vol;
54 
55 	/* Look for geom with specified name. */
56 	LIST_FOREACH(gp, &mp->geom, geom) {
57 		sc = gp->softc;
58 		if (sc == NULL)
59 			continue;
60 		if (sc->sc_stopping != 0)
61 			continue;
62 		if (strcasecmp(sc->sc_name, name) == 0)
63 			return (sc);
64 	}
65 
66 	/* Look for provider with specified name. */
67 	LIST_FOREACH(gp, &mp->geom, geom) {
68 		sc = gp->softc;
69 		if (sc == NULL)
70 			continue;
71 		if (sc->sc_stopping != 0)
72 			continue;
73 		LIST_FOREACH(pp, &gp->provider, provider) {
74 			if (strcmp(pp->name, name) == 0)
75 				return (sc);
76 			if (strncmp(pp->name, "raid/", 5) == 0 &&
77 			    strcmp(pp->name + 5, name) == 0)
78 				return (sc);
79 		}
80 	}
81 
82 	/* Look for volume with specified name. */
83 	LIST_FOREACH(gp, &mp->geom, geom) {
84 		sc = gp->softc;
85 		if (sc == NULL)
86 			continue;
87 		if (sc->sc_stopping != 0)
88 			continue;
89 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
90 			if (strcmp(vol->v_name, name) == 0)
91 				return (sc);
92 		}
93 	}
94 	return (NULL);
95 }
96 
97 static void
98 g_raid_ctl_label(struct gctl_req *req, struct g_class *mp)
99 {
100 	struct g_geom *geom;
101 	struct g_raid_softc *sc;
102 	const char *format;
103 	int *nargs;
104 	int crstatus, ctlstatus;
105 	char buf[64];
106 
107 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
108 	if (nargs == NULL) {
109 		gctl_error(req, "No '%s' argument.", "nargs");
110 		return;
111 	}
112 	if (*nargs < 4) {
113 		gctl_error(req, "Invalid number of arguments.");
114 		return;
115 	}
116 	format = gctl_get_asciiparam(req, "arg0");
117 	if (format == NULL) {
118 		gctl_error(req, "No format received.");
119 		return;
120 	}
121 	crstatus = g_raid_create_node_format(format, req, &geom);
122 	if (crstatus == G_RAID_MD_TASTE_FAIL) {
123 		gctl_error(req, "Failed to create array with format '%s'.",
124 		    format);
125 		return;
126 	}
127 	sc = (struct g_raid_softc *)geom->softc;
128 	g_topology_unlock();
129 	sx_xlock(&sc->sc_lock);
130 	ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
131 	if (ctlstatus < 0) {
132 		gctl_error(req, "Command failed: %d.", ctlstatus);
133 		if (crstatus == G_RAID_MD_TASTE_NEW)
134 			g_raid_destroy_node(sc, 0);
135 	} else {
136 		if (crstatus == G_RAID_MD_TASTE_NEW)
137 			snprintf(buf, sizeof(buf), "%s created\n", sc->sc_name);
138 		else
139 			snprintf(buf, sizeof(buf), "%s reused\n", sc->sc_name);
140 		gctl_set_param_err(req, "output", buf, strlen(buf) + 1);
141 	}
142 	sx_xunlock(&sc->sc_lock);
143 	g_topology_lock();
144 }
145 
146 static void
147 g_raid_ctl_stop(struct gctl_req *req, struct g_class *mp)
148 {
149 	struct g_raid_softc *sc;
150 	const char *nodename;
151 	int *nargs, *force;
152 	int error, how;
153 
154 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
155 	if (nargs == NULL) {
156 		gctl_error(req, "No '%s' argument.", "nargs");
157 		return;
158 	}
159 	if (*nargs != 1) {
160 		gctl_error(req, "Invalid number of arguments.");
161 		return;
162 	}
163 	nodename = gctl_get_asciiparam(req, "arg0");
164 	if (nodename == NULL) {
165 		gctl_error(req, "No array name received.");
166 		return;
167 	}
168 	sc = g_raid_find_node(mp, nodename);
169 	if (sc == NULL) {
170 		gctl_error(req, "Array '%s' not found.", nodename);
171 		return;
172 	}
173 	force = gctl_get_paraml(req, "force", sizeof(*force));
174 	if (force != NULL && *force)
175 		how = G_RAID_DESTROY_HARD;
176 	else
177 		how = G_RAID_DESTROY_SOFT;
178 	g_topology_unlock();
179 	sx_xlock(&sc->sc_lock);
180 	error = g_raid_destroy(sc, how);
181 	if (error != 0)
182 		gctl_error(req, "Array is busy.");
183 	g_topology_lock();
184 }
185 
186 static void
187 g_raid_ctl_other(struct gctl_req *req, struct g_class *mp)
188 {
189 	struct g_raid_softc *sc;
190 	const char *nodename;
191 	int *nargs;
192 	int ctlstatus;
193 
194 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
195 	if (nargs == NULL) {
196 		gctl_error(req, "No '%s' argument.", "nargs");
197 		return;
198 	}
199 	if (*nargs < 1) {
200 		gctl_error(req, "Invalid number of arguments.");
201 		return;
202 	}
203 	nodename = gctl_get_asciiparam(req, "arg0");
204 	if (nodename == NULL) {
205 		gctl_error(req, "No array name received.");
206 		return;
207 	}
208 	sc = g_raid_find_node(mp, nodename);
209 	if (sc == NULL) {
210 		gctl_error(req, "Array '%s' not found.", nodename);
211 		return;
212 	}
213 	g_topology_unlock();
214 	sx_xlock(&sc->sc_lock);
215 	if (sc->sc_md != NULL) {
216 		ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
217 		if (ctlstatus < 0)
218 			gctl_error(req, "Command failed: %d.", ctlstatus);
219 	}
220 	sx_xunlock(&sc->sc_lock);
221 	g_topology_lock();
222 }
223 
224 void
225 g_raid_ctl(struct gctl_req *req, struct g_class *mp, const char *verb)
226 {
227 	uint32_t *version;
228 
229 	g_topology_assert();
230 
231 	version = gctl_get_paraml(req, "version", sizeof(*version));
232 	if (version == NULL) {
233 		gctl_error(req, "No '%s' argument.", "version");
234 		return;
235 	}
236 	if (*version != G_RAID_VERSION) {
237 		gctl_error(req, "Userland and kernel parts are out of sync.");
238 		return;
239 	}
240 
241 	if (strcmp(verb, "label") == 0)
242 		g_raid_ctl_label(req, mp);
243 	else if (strcmp(verb, "stop") == 0)
244 		g_raid_ctl_stop(req, mp);
245 	else
246 		g_raid_ctl_other(req, mp);
247 }
248