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