2 * Copyright (c) 2004 Lukas Ertl
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 AUTHOR 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 AUTHOR 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/libkern.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
36 #include <geom/geom.h>
37 #include <geom/vinum/geom_vinum_var.h>
38 #include <geom/vinum/geom_vinum.h>
39 #include <geom/vinum/geom_vinum_share.h>
41 static int gv_rm_drive(struct gv_softc *, struct gctl_req *,
42 struct gv_drive *, int);
43 static int gv_rm_plex(struct gv_softc *, struct gctl_req *,
44 struct gv_plex *, int);
45 static int gv_rm_vol(struct gv_softc *, struct gctl_req *,
46 struct gv_volume *, int);
48 /* General 'remove' routine. */
50 gv_remove(struct g_geom *gp, struct gctl_req *req)
61 argc = gctl_get_paraml(req, "argc", sizeof(*argc));
62 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
64 if (argc == NULL || *argc == 0) {
65 gctl_error(req, "no arguments given");
71 for (i = 0; i < *argc; i++) {
72 snprintf(buf, sizeof(buf), "argv%d", i);
73 argv = gctl_get_param(req, buf, NULL);
76 type = gv_object_type(sc, argv);
79 v = gv_find_vol(sc, argv);
81 gctl_error(req, "unknown volume '%s'", argv);
84 err = gv_rm_vol(sc, req, v, *flags);
89 p = gv_find_plex(sc, argv);
91 gctl_error(req, "unknown plex '%s'", argv);
94 err = gv_rm_plex(sc, req, p, *flags);
99 s = gv_find_sd(sc, argv);
101 gctl_error(req, "unknown subdisk '%s'", argv);
104 err = gv_rm_sd(sc, req, s, *flags);
109 d = gv_find_drive(sc, argv);
111 gctl_error(req, "unknown drive '%s'", argv);
114 err = gv_rm_drive(sc, req, d, *flags);
119 gctl_error(req, "unknown object '%s'", argv);
124 gv_save_config_all(sc);
127 /* Resets configuration */
129 gv_resetconfig(struct g_geom *gp, struct gctl_req *req)
132 struct gv_drive *d, *d2;
133 struct gv_volume *v, *v2;
134 struct gv_plex *p, *p2;
135 struct gv_sd *s, *s2;
146 /* First loop through to make sure no volumes are up */
147 LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2) {
148 if (gv_is_open(v->geom)) {
149 gctl_error(req, "volume '%s' is busy", v->name);
153 /* Then if not, we remove everything. */
154 LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2)
155 gv_rm_vol(sc, req, v, flags);
156 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2)
157 gv_rm_plex(sc, req, p, flags);
158 LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2)
159 gv_rm_sd(sc, req, s, flags);
160 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2)
161 gv_rm_drive(sc, req, d, flags);
162 gv_save_config_all(sc);
166 /* Remove a volume. */
168 gv_rm_vol(struct gv_softc *sc, struct gctl_req *req, struct gv_volume *v, int flags)
171 struct gv_plex *p, *p2;
175 KASSERT(v != NULL, ("gv_rm_vol: NULL v"));
177 /* If this volume has plexes, we want a recursive removal. */
178 if (!LIST_EMPTY(&v->plexes) && !(flags & GV_FLAG_R)) {
179 gctl_error(req, "volume '%s' has attached plexes", v->name);
185 /* Check if any of our consumers is open. */
186 if (gp != NULL && gv_is_open(gp)) {
187 gctl_error(req, "volume '%s' is busy", v->name);
191 /* Remove the plexes our volume has. */
192 LIST_FOREACH_SAFE(p, &v->plexes, in_volume, p2) {
194 LIST_REMOVE(p, in_volume);
197 err = gv_rm_plex(sc, req, p, flags);
202 /* Clean up and let our geom fade away. */
203 LIST_REMOVE(v, volume);
204 gv_kill_vol_thread(v);
208 g_wither_geom(gp, ENXIO);
216 gv_rm_plex(struct gv_softc *sc, struct gctl_req *req, struct gv_plex *p, int flags)
220 struct gv_sd *s, *s2;
225 KASSERT(p != NULL, ("gv_rm_plex: NULL p"));
227 /* If this plex has subdisks, we want a recursive removal. */
228 if (!LIST_EMPTY(&p->subdisks) && !(flags & GV_FLAG_R)) {
229 gctl_error(req, "plex '%s' has attached subdisks", p->name);
233 if (p->vol_sc != NULL && p->vol_sc->plexcount == 1) {
234 gctl_error(req, "plex '%s' is still attached to volume '%s'",
241 /* Check if any of our consumers is open. */
242 if (gp != NULL && gv_is_open(gp)) {
243 gctl_error(req, "plex '%s' is busy", p->name);
247 /* Remove the subdisks our plex has. */
248 LIST_FOREACH_SAFE(s, &p->subdisks, in_plex, s2) {
250 LIST_REMOVE(s, in_plex);
254 err = gv_rm_sd(sc, req, s, flags);
260 /* Clean up and let our geom fade away. */
261 LIST_REMOVE(p, plex);
262 if (p->vol_sc != NULL) {
263 p->vol_sc->plexcount--;
264 LIST_REMOVE(p, in_volume);
266 /* Correctly update the volume size. */
267 gv_update_vol_size(v, gv_vol_size(v));
270 gv_kill_plex_thread(p);
275 g_wither_geom(gp, ENXIO);
281 /* Remove a subdisk. */
283 gv_rm_sd(struct gv_softc *sc, struct gctl_req *req, struct gv_sd *s, int flags)
285 struct g_provider *pp;
289 KASSERT(s != NULL, ("gv_rm_sd: NULL s"));
297 LIST_REMOVE(s, in_plex);
300 /* Update the plexsize. */
301 p->size = gv_plex_size(p);
304 /* Update the size of our plex' volume. */
305 gv_update_vol_size(v, gv_vol_size(v));
309 LIST_REMOVE(s, from_drive);
314 /* If the subdisk has a provider we need to clean up this one too. */
316 pp->flags |= G_PF_WITHER;
317 g_orphan_provider(pp, ENXIO);
323 /* Remove a drive. */
325 gv_rm_drive(struct gv_softc *sc, struct gctl_req *req, struct gv_drive *d, int flags)
328 struct g_consumer *cp;
329 struct gv_freelist *fl, *fl2;
331 struct gv_sd *s, *s2;
335 KASSERT(d != NULL, ("gv_rm_drive: NULL d"));
337 KASSERT(gp != NULL, ("gv_rm_drive: NULL gp"));
339 /* We don't allow to remove open drives. */
340 if (gv_is_open(gp)) {
341 gctl_error(req, "drive '%s' is open", d->name);
345 /* A drive with subdisks needs a recursive removal. */
346 if (!LIST_EMPTY(&d->subdisks) && !(flags & GV_FLAG_R)) {
347 gctl_error(req, "drive '%s' still has subdisks", d->name);
351 cp = LIST_FIRST(&gp->consumer);
352 err = g_access(cp, 0, 1, 0);
354 printf("GEOM_VINUM: gv_rm_drive: couldn't access '%s', errno: "
355 "%d\n", cp->provider->name, err);
359 /* Clear the Vinum Magic. */
360 d->hdr->magic = GV_NOMAGIC;
362 err = g_write_data(cp, GV_HDR_OFFSET, d->hdr, GV_HDR_LEN);
364 printf("GEOM_VINUM: gv_rm_drive: couldn't write header to '%s'"
365 ", errno: %d\n", cp->provider->name, err);
366 d->hdr->magic = GV_MAGIC;
369 g_access(cp, 0, -1, 0);
371 /* Remove all associated subdisks, plexes, volumes. */
372 if (!LIST_EMPTY(&d->subdisks)) {
373 LIST_FOREACH_SAFE(s, &d->subdisks, from_drive, s2) {
378 gv_rm_vol(sc, req, v, flags);
384 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
385 LIST_REMOVE(fl, freelist);
388 LIST_REMOVE(d, drive);
390 gv_kill_drive_thread(d);
395 gv_save_config_all(sc);
396 g_wither_geom(gp, ENXIO);