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"
51 static struct g_raid_softc *
52 g_raid_find_node(struct g_class *mp, const char *name)
54 struct g_raid_softc *sc;
56 struct g_provider *pp;
57 struct g_raid_volume *vol;
59 /* Look for geom with specified name. */
60 LIST_FOREACH(gp, &mp->geom, geom) {
64 if (sc->sc_stopping != 0)
66 if (strcasecmp(sc->sc_name, name) == 0)
70 /* Look for provider with specified name. */
71 LIST_FOREACH(gp, &mp->geom, geom) {
75 if (sc->sc_stopping != 0)
77 LIST_FOREACH(pp, &gp->provider, provider) {
78 if (strcmp(pp->name, name) == 0)
80 if (strncmp(pp->name, "raid/", 5) == 0 &&
81 strcmp(pp->name + 5, name) == 0)
86 /* Look for volume with specified name. */
87 LIST_FOREACH(gp, &mp->geom, geom) {
91 if (sc->sc_stopping != 0)
93 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
94 if (strcmp(vol->v_name, name) == 0)
102 g_raid_ctl_label(struct gctl_req *req, struct g_class *mp)
105 struct g_raid_softc *sc;
108 int crstatus, ctlstatus;
111 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
113 gctl_error(req, "No '%s' argument.", "nargs");
117 gctl_error(req, "Invalid number of arguments.");
120 format = gctl_get_asciiparam(req, "arg0");
121 if (format == NULL) {
122 gctl_error(req, "No format received.");
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'.",
131 sc = (struct g_raid_softc *)geom->softc;
133 sx_xlock(&sc->sc_lock);
134 ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
136 gctl_error(req, "Command failed: %d.", ctlstatus);
137 if (crstatus == G_RAID_MD_TASTE_NEW)
138 g_raid_destroy_node(sc, 0);
140 if (crstatus == G_RAID_MD_TASTE_NEW)
141 snprintf(buf, sizeof(buf), "%s created\n", sc->sc_name);
143 snprintf(buf, sizeof(buf), "%s reused\n", sc->sc_name);
144 gctl_set_param_err(req, "output", buf, strlen(buf) + 1);
146 sx_xunlock(&sc->sc_lock);
151 g_raid_ctl_stop(struct gctl_req *req, struct g_class *mp)
153 struct g_raid_softc *sc;
154 const char *nodename;
158 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
160 gctl_error(req, "No '%s' argument.", "nargs");
164 gctl_error(req, "Invalid number of arguments.");
167 nodename = gctl_get_asciiparam(req, "arg0");
168 if (nodename == NULL) {
169 gctl_error(req, "No array name received.");
172 sc = g_raid_find_node(mp, nodename);
174 gctl_error(req, "Array '%s' not found.", nodename);
177 force = gctl_get_paraml(req, "force", sizeof(*force));
178 if (force != NULL && *force)
179 how = G_RAID_DESTROY_HARD;
181 how = G_RAID_DESTROY_SOFT;
183 sx_xlock(&sc->sc_lock);
184 error = g_raid_destroy(sc, how);
186 gctl_error(req, "Array is busy.");
191 g_raid_ctl_other(struct gctl_req *req, struct g_class *mp)
193 struct g_raid_softc *sc;
194 const char *nodename;
198 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
200 gctl_error(req, "No '%s' argument.", "nargs");
204 gctl_error(req, "Invalid number of arguments.");
207 nodename = gctl_get_asciiparam(req, "arg0");
208 if (nodename == NULL) {
209 gctl_error(req, "No array name received.");
212 sc = g_raid_find_node(mp, nodename);
214 gctl_error(req, "Array '%s' not found.", nodename);
218 sx_xlock(&sc->sc_lock);
219 if (sc->sc_md != NULL) {
220 ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
222 gctl_error(req, "Command failed: %d.", ctlstatus);
224 sx_xunlock(&sc->sc_lock);
229 g_raid_ctl(struct gctl_req *req, struct g_class *mp, const char *verb)
235 version = gctl_get_paraml(req, "version", sizeof(*version));
236 if (version == NULL) {
237 gctl_error(req, "No '%s' argument.", "version");
240 if (*version != G_RAID_VERSION) {
241 gctl_error(req, "Userland and kernel parts are out of sync.");
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);
250 g_raid_ctl_other(req, mp);