2 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <sys/mutex.h>
37 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <sys/bitstring.h>
41 #include <machine/atomic.h>
42 #include <geom/geom.h>
44 #include <sys/kthread.h>
45 #include <geom/raid/g_raid.h>
46 #include "g_raid_md_if.h"
49 static struct g_raid_softc *
50 g_raid_find_node(struct g_class *mp, const char *name)
52 struct g_raid_softc *sc;
54 struct g_provider *pp;
55 struct g_raid_volume *vol;
57 /* Look for geom with specified name. */
58 LIST_FOREACH(gp, &mp->geom, geom) {
62 if (sc->sc_stopping != 0)
64 if (strcasecmp(sc->sc_name, name) == 0)
68 /* Look for provider with specified name. */
69 LIST_FOREACH(gp, &mp->geom, geom) {
73 if (sc->sc_stopping != 0)
75 LIST_FOREACH(pp, &gp->provider, provider) {
76 if (strcmp(pp->name, name) == 0)
78 if (strncmp(pp->name, "raid/", 5) == 0 &&
79 strcmp(pp->name + 5, name) == 0)
84 /* Look for volume with specified name. */
85 LIST_FOREACH(gp, &mp->geom, geom) {
89 if (sc->sc_stopping != 0)
91 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
92 if (strcmp(vol->v_name, name) == 0)
100 g_raid_ctl_label(struct gctl_req *req, struct g_class *mp)
103 struct g_raid_softc *sc;
106 int crstatus, ctlstatus;
109 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
111 gctl_error(req, "No '%s' argument.", "nargs");
115 gctl_error(req, "Invalid number of arguments.");
118 format = gctl_get_asciiparam(req, "arg0");
119 if (format == NULL) {
120 gctl_error(req, "No format recieved.");
123 crstatus = g_raid_create_node_format(format, req, &geom);
124 if (crstatus == G_RAID_MD_TASTE_FAIL) {
125 gctl_error(req, "Failed to create array with format '%s'.",
129 sc = (struct g_raid_softc *)geom->softc;
131 sx_xlock(&sc->sc_lock);
132 ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
134 gctl_error(req, "Command failed: %d.", ctlstatus);
135 if (crstatus == G_RAID_MD_TASTE_NEW)
136 g_raid_destroy_node(sc, 0);
138 if (crstatus == G_RAID_MD_TASTE_NEW)
139 snprintf(buf, sizeof(buf), "%s created\n", sc->sc_name);
141 snprintf(buf, sizeof(buf), "%s reused\n", sc->sc_name);
142 gctl_set_param_err(req, "output", buf, strlen(buf) + 1);
144 sx_xunlock(&sc->sc_lock);
149 g_raid_ctl_stop(struct gctl_req *req, struct g_class *mp)
151 struct g_raid_softc *sc;
152 const char *nodename;
156 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
158 gctl_error(req, "No '%s' argument.", "nargs");
162 gctl_error(req, "Invalid number of arguments.");
165 nodename = gctl_get_asciiparam(req, "arg0");
166 if (nodename == NULL) {
167 gctl_error(req, "No array name recieved.");
170 sc = g_raid_find_node(mp, nodename);
172 gctl_error(req, "Array '%s' not found.", nodename);
175 force = gctl_get_paraml(req, "force", sizeof(*force));
176 if (force != NULL && *force)
177 how = G_RAID_DESTROY_HARD;
179 how = G_RAID_DESTROY_SOFT;
181 sx_xlock(&sc->sc_lock);
182 error = g_raid_destroy(sc, how);
184 sx_xunlock(&sc->sc_lock);
189 g_raid_ctl_other(struct gctl_req *req, struct g_class *mp)
191 struct g_raid_softc *sc;
192 const char *nodename;
196 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
198 gctl_error(req, "No '%s' argument.", "nargs");
202 gctl_error(req, "Invalid number of arguments.");
205 nodename = gctl_get_asciiparam(req, "arg0");
206 if (nodename == NULL) {
207 gctl_error(req, "No array name recieved.");
210 sc = g_raid_find_node(mp, nodename);
212 gctl_error(req, "Array '%s' not found.", nodename);
216 sx_xlock(&sc->sc_lock);
217 if (sc->sc_md != NULL) {
218 ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
220 gctl_error(req, "Command failed: %d.", ctlstatus);
222 sx_xunlock(&sc->sc_lock);
227 g_raid_ctl(struct gctl_req *req, struct g_class *mp, const char *verb)
233 version = gctl_get_paraml(req, "version", sizeof(*version));
234 if (version == NULL) {
235 gctl_error(req, "No '%s' argument.", "version");
238 if (*version != G_RAID_VERSION) {
239 gctl_error(req, "Userland and kernel parts are out of sync.");
243 if (strcmp(verb, "label") == 0)
244 g_raid_ctl_label(req, mp);
245 else if (strcmp(verb, "stop") == 0)
246 g_raid_ctl_stop(req, mp);
248 g_raid_ctl_other(req, mp);