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