2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/bitstring.h>
43 #include <machine/atomic.h>
44 #include <geom/geom.h>
46 #include <sys/kthread.h>
47 #include <geom/raid/g_raid.h>
48 #include "g_raid_md_if.h"
50 static struct g_raid_softc *
51 g_raid_find_node(struct g_class *mp, const char *name)
53 struct g_raid_softc *sc;
55 struct g_provider *pp;
56 struct g_raid_volume *vol;
58 /* Look for geom with specified name. */
59 LIST_FOREACH(gp, &mp->geom, geom) {
63 if (sc->sc_stopping != 0)
65 if (strcasecmp(sc->sc_name, name) == 0)
69 /* Look for provider with specified name. */
70 LIST_FOREACH(gp, &mp->geom, geom) {
74 if (sc->sc_stopping != 0)
76 LIST_FOREACH(pp, &gp->provider, provider) {
77 if (strcmp(pp->name, name) == 0)
79 if (strncmp(pp->name, "raid/", 5) == 0 &&
80 strcmp(pp->name + 5, name) == 0)
85 /* Look for volume with specified name. */
86 LIST_FOREACH(gp, &mp->geom, geom) {
90 if (sc->sc_stopping != 0)
92 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
93 if (strcmp(vol->v_name, name) == 0)
101 g_raid_ctl_label(struct gctl_req *req, struct g_class *mp)
104 struct g_raid_softc *sc;
107 int crstatus, ctlstatus;
110 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
112 gctl_error(req, "No '%s' argument.", "nargs");
116 gctl_error(req, "Invalid number of arguments.");
119 format = gctl_get_asciiparam(req, "arg0");
120 if (format == NULL) {
121 gctl_error(req, "No format received.");
124 crstatus = g_raid_create_node_format(format, req, &geom);
125 if (crstatus == G_RAID_MD_TASTE_FAIL) {
126 gctl_error(req, "Failed to create array with format '%s'.",
130 sc = (struct g_raid_softc *)geom->softc;
132 sx_xlock(&sc->sc_lock);
133 ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
135 gctl_error(req, "Command failed: %d.", ctlstatus);
136 if (crstatus == G_RAID_MD_TASTE_NEW)
137 g_raid_destroy_node(sc, 0);
139 if (crstatus == G_RAID_MD_TASTE_NEW)
140 snprintf(buf, sizeof(buf), "%s created\n", sc->sc_name);
142 snprintf(buf, sizeof(buf), "%s reused\n", sc->sc_name);
143 gctl_set_param_err(req, "output", buf, strlen(buf) + 1);
145 sx_xunlock(&sc->sc_lock);
150 g_raid_ctl_stop(struct gctl_req *req, struct g_class *mp)
152 struct g_raid_softc *sc;
153 const char *nodename;
157 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
159 gctl_error(req, "No '%s' argument.", "nargs");
163 gctl_error(req, "Invalid number of arguments.");
166 nodename = gctl_get_asciiparam(req, "arg0");
167 if (nodename == NULL) {
168 gctl_error(req, "No array name received.");
171 sc = g_raid_find_node(mp, nodename);
173 gctl_error(req, "Array '%s' not found.", nodename);
176 force = gctl_get_paraml(req, "force", sizeof(*force));
177 if (force != NULL && *force)
178 how = G_RAID_DESTROY_HARD;
180 how = G_RAID_DESTROY_SOFT;
182 sx_xlock(&sc->sc_lock);
183 error = g_raid_destroy(sc, how);
185 gctl_error(req, "Array is busy.");
190 g_raid_ctl_other(struct gctl_req *req, struct g_class *mp)
192 struct g_raid_softc *sc;
193 const char *nodename;
197 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
199 gctl_error(req, "No '%s' argument.", "nargs");
203 gctl_error(req, "Invalid number of arguments.");
206 nodename = gctl_get_asciiparam(req, "arg0");
207 if (nodename == NULL) {
208 gctl_error(req, "No array name received.");
211 sc = g_raid_find_node(mp, nodename);
213 gctl_error(req, "Array '%s' not found.", nodename);
217 sx_xlock(&sc->sc_lock);
218 if (sc->sc_md != NULL) {
219 ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
221 gctl_error(req, "Command failed: %d.", ctlstatus);
223 sx_xunlock(&sc->sc_lock);
228 g_raid_ctl(struct gctl_req *req, struct g_class *mp, const char *verb)
234 version = gctl_get_paraml(req, "version", sizeof(*version));
235 if (version == NULL) {
236 gctl_error(req, "No '%s' argument.", "version");
239 if (*version != G_RAID_VERSION) {
240 gctl_error(req, "Userland and kernel parts are out of sync.");
244 if (strcmp(verb, "label") == 0)
245 g_raid_ctl_label(req, mp);
246 else if (strcmp(verb, "stop") == 0)
247 g_raid_ctl_stop(req, mp);
249 g_raid_ctl_other(req, mp);