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;
55 LIST_FOREACH(gp, &mp->geom, geom) {
59 if (sc->sc_stopping != 0)
61 if (strcasecmp(sc->sc_name, name) == 0)
68 g_raid_ctl_label(struct gctl_req *req, struct g_class *mp)
71 struct g_raid_softc *sc;
74 int crstatus, ctlstatus;
77 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
79 gctl_error(req, "No '%s' argument.", "nargs");
83 gctl_error(req, "Invalid number of arguments.");
86 format = gctl_get_asciiparam(req, "arg0");
88 gctl_error(req, "No format recieved.");
91 crstatus = g_raid_create_node_format(format, req, &geom);
92 if (crstatus == G_RAID_MD_TASTE_FAIL) {
93 gctl_error(req, "Failed to create array with format '%s'.",
97 sc = (struct g_raid_softc *)geom->softc;
99 sx_xlock(&sc->sc_lock);
100 ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
102 gctl_error(req, "Command failed: %d.", ctlstatus);
103 if (crstatus == G_RAID_MD_TASTE_NEW)
104 g_raid_destroy_node(sc, 0);
106 if (crstatus == G_RAID_MD_TASTE_NEW)
107 snprintf(buf, sizeof(buf), "%s created\n", sc->sc_name);
109 snprintf(buf, sizeof(buf), "%s reused\n", sc->sc_name);
110 gctl_set_param_err(req, "output", buf, strlen(buf) + 1);
112 sx_xunlock(&sc->sc_lock);
117 g_raid_ctl_stop(struct gctl_req *req, struct g_class *mp)
119 struct g_raid_softc *sc;
120 const char *nodename;
124 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
126 gctl_error(req, "No '%s' argument.", "nargs");
130 gctl_error(req, "Invalid number of arguments.");
133 nodename = gctl_get_asciiparam(req, "arg0");
134 if (nodename == NULL) {
135 gctl_error(req, "No array name recieved.");
138 sc = g_raid_find_node(mp, nodename);
140 gctl_error(req, "Array '%s' not found.", nodename);
143 force = gctl_get_paraml(req, "force", sizeof(*force));
144 if (force != NULL && *force)
145 how = G_RAID_DESTROY_HARD;
147 how = G_RAID_DESTROY_SOFT;
149 sx_xlock(&sc->sc_lock);
150 error = g_raid_destroy(sc, how);
152 sx_xunlock(&sc->sc_lock);
157 g_raid_ctl_other(struct gctl_req *req, struct g_class *mp)
159 struct g_raid_softc *sc;
160 const char *nodename;
164 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
166 gctl_error(req, "No '%s' argument.", "nargs");
170 gctl_error(req, "Invalid number of arguments.");
173 nodename = gctl_get_asciiparam(req, "arg0");
174 if (nodename == NULL) {
175 gctl_error(req, "No array name recieved.");
178 sc = g_raid_find_node(mp, nodename);
180 gctl_error(req, "Array '%s' not found.", nodename);
184 sx_xlock(&sc->sc_lock);
185 if (sc->sc_md != NULL) {
186 ctlstatus = G_RAID_MD_CTL(sc->sc_md, req);
188 gctl_error(req, "Command failed: %d.", ctlstatus);
190 sx_xunlock(&sc->sc_lock);
195 g_raid_ctl(struct gctl_req *req, struct g_class *mp, const char *verb)
201 version = gctl_get_paraml(req, "version", sizeof(*version));
202 if (version == NULL) {
203 gctl_error(req, "No '%s' argument.", "version");
206 if (*version != G_RAID_VERSION) {
207 gctl_error(req, "Userland and kernel parts are out of sync.");
211 if (strcmp(verb, "label") == 0)
212 g_raid_ctl_label(req, mp);
213 else if (strcmp(verb, "stop") == 0)
214 g_raid_ctl_stop(req, mp);
216 g_raid_ctl_other(req, mp);