2 * Copyright (c) 2004, 2007 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/malloc.h>
35 #include <geom/geom.h>
36 #include <geom/vinum/geom_vinum_var.h>
37 #include <geom/vinum/geom_vinum.h>
39 /* General 'remove' routine. */
41 gv_remove(struct g_geom *gp, struct gctl_req *req)
52 argc = gctl_get_paraml(req, "argc", sizeof(*argc));
54 if (argc == NULL || *argc == 0) {
55 gctl_error(req, "no arguments given");
59 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
61 gctl_error(req, "no flags given");
67 /* XXX config locking */
69 for (i = 0; i < *argc; i++) {
70 snprintf(buf, sizeof(buf), "argv%d", i);
71 argv = gctl_get_param(req, buf, NULL);
74 type = gv_object_type(sc, argv);
77 v = gv_find_vol(sc, argv);
80 * If this volume has plexes, we want a recursive
83 if (!LIST_EMPTY(&v->plexes) && !(*flags & GV_FLAG_R)) {
84 gctl_error(req, "volume '%s' has attached "
85 "plexes - need recursive removal", v->name);
89 gv_post_event(sc, GV_EVENT_RM_VOLUME, v, NULL, 0, 0);
93 p = gv_find_plex(sc, argv);
96 * If this plex has subdisks, we want a recursive
99 if (!LIST_EMPTY(&p->subdisks) &&
100 !(*flags & GV_FLAG_R)) {
101 gctl_error(req, "plex '%s' has attached "
102 "subdisks - need recursive removal",
107 /* Don't allow removal of the only plex of a volume. */
108 if (p->vol_sc != NULL && p->vol_sc->plexcount == 1) {
109 gctl_error(req, "plex '%s' is still attached "
110 "to volume '%s'", p->name, p->volume);
114 gv_post_event(sc, GV_EVENT_RM_PLEX, p, NULL, 0, 0);
118 s = gv_find_sd(sc, argv);
120 /* Don't allow removal if attached to a plex. */
121 if (s->plex_sc != NULL) {
122 gctl_error(req, "subdisk '%s' is still attached"
123 " to plex '%s'", s->name, s->plex_sc->name);
127 gv_post_event(sc, GV_EVENT_RM_SD, s, NULL, 0, 0);
131 d = gv_find_drive(sc, argv);
132 /* We don't allow to remove open drives. */
133 if (gv_consumer_is_open(d->consumer) &&
134 !(*flags & GV_FLAG_F)) {
135 gctl_error(req, "drive '%s' is open", d->name);
139 /* A drive with subdisks needs a recursive removal. */
140 /* if (!LIST_EMPTY(&d->subdisks) &&
141 !(*flags & GV_FLAG_R)) {
142 gctl_error(req, "drive '%s' still has subdisks"
143 " - need recursive removal", d->name);
147 gv_post_event(sc, GV_EVENT_RM_DRIVE, d, NULL, *flags,
152 gctl_error(req, "unknown object '%s'", argv);
157 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
160 /* Resets configuration */
162 gv_resetconfig(struct gv_softc *sc)
164 struct gv_drive *d, *d2;
165 struct gv_volume *v, *v2;
166 struct gv_plex *p, *p2;
167 struct gv_sd *s, *s2;
169 /* First make sure nothing is open. */
170 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) {
171 if (gv_consumer_is_open(d->consumer)) {
172 return (GV_ERR_ISBUSY);
176 /* Make sure nothing is going on internally. */
177 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) {
178 if (p->flags & (GV_PLEX_REBUILDING | GV_PLEX_GROWING))
179 return (GV_ERR_ISBUSY);
182 /* Then if not, we remove everything. */
183 LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2)
185 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2)
186 gv_rm_drive(sc, d, 0);
187 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2)
189 LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2)
192 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
197 /* Remove a volume. */
199 gv_rm_vol(struct gv_softc *sc, struct gv_volume *v)
201 struct g_provider *pp;
202 struct gv_plex *p, *p2;
204 KASSERT(v != NULL, ("gv_rm_vol: NULL v"));
206 KASSERT(pp != NULL, ("gv_rm_vol: NULL pp"));
208 /* Check if any of our consumers is open. */
209 if (gv_provider_is_open(pp)) {
210 G_VINUM_DEBUG(0, "unable to remove %s: volume still in use",
215 /* Remove the plexes our volume has. */
216 LIST_FOREACH_SAFE(p, &v->plexes, in_volume, p2)
220 LIST_REMOVE(v, volume);
223 /* Get rid of the volume's provider. */
226 g_wither_provider(pp, ENXIO);
233 gv_rm_plex(struct gv_softc *sc, struct gv_plex *p)
236 struct gv_sd *s, *s2;
238 KASSERT(p != NULL, ("gv_rm_plex: NULL p"));
241 /* Check if any of our consumers is open. */
242 if (v != NULL && gv_provider_is_open(v->provider) && v->plexcount < 2) {
243 G_VINUM_DEBUG(0, "unable to remove %s: volume still in use",
248 /* Remove the subdisks our plex has. */
249 LIST_FOREACH_SAFE(s, &p->subdisks, in_plex, s2)
253 /* Clean up and let our geom fade away. */
254 LIST_REMOVE(p, plex);
255 if (p->vol_sc != NULL) {
256 p->vol_sc->plexcount--;
257 LIST_REMOVE(p, in_volume);
259 /* Correctly update the volume size. */
260 gv_update_vol_size(v, gv_vol_size(v));
266 /* Remove a subdisk. */
268 gv_rm_sd(struct gv_softc *sc, struct gv_sd *s)
273 KASSERT(s != NULL, ("gv_rm_sd: NULL s"));
280 LIST_REMOVE(s, in_plex);
283 /* Update the plexsize. */
284 p->size = gv_plex_size(p);
287 /* Update the size of our plex' volume. */
288 gv_update_vol_size(v, gv_vol_size(v));
291 if (s->drive_sc && !(s->drive_sc->flags & GV_DRIVE_REFERENCED))
292 LIST_REMOVE(s, from_drive);
298 /* Remove a drive. */
300 gv_rm_drive(struct gv_softc *sc, struct gv_drive *d, int flags)
302 struct g_consumer *cp;
303 struct gv_freelist *fl, *fl2;
305 struct gv_sd *s, *s2;
310 KASSERT(d != NULL, ("gv_rm_drive: NULL d"));
316 err = g_access(cp, 0, 1, 0);
320 G_VINUM_DEBUG(0, "%s: unable to access '%s', "
321 "errno: %d", __func__, cp->provider->name, err);
325 /* Clear the Vinum Magic. */
326 d->hdr->magic = GV_NOMAGIC;
327 err = gv_write_header(cp, d->hdr);
329 G_VINUM_DEBUG(0, "gv_rm_drive: error writing header to"
330 " '%s', errno: %d", cp->provider->name, err);
333 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
335 g_destroy_consumer(cp);
339 /* Remove all associated subdisks, plexes, volumes. */
340 if (flags & GV_FLAG_R) {
341 if (!LIST_EMPTY(&d->subdisks)) {
342 LIST_FOREACH_SAFE(s, &d->subdisks, from_drive, s2) {
354 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
355 LIST_REMOVE(fl, freelist);
359 LIST_REMOVE(d, drive);
362 /* Put ourself into referenced state if we have subdisks. */
363 if (d->sdcount > 0) {
366 d->flags |= GV_DRIVE_REFERENCED;
367 snprintf(d->device, sizeof(d->device), "???");
370 d->freelist_entries = 0;
371 LIST_FOREACH(s, &d->subdisks, from_drive) {
372 s->flags |= GV_SD_TASTED;
373 gv_set_sd_state(s, GV_SD_DOWN, GV_SETSTATE_FORCE);
375 /* Shuffle around so we keep gv_is_newer happy. */
376 LIST_REMOVE(d, drive);
377 d2 = LIST_FIRST(&sc->drives);
379 LIST_INSERT_HEAD(&sc->drives, d, drive);
381 LIST_INSERT_AFTER(d2, d, drive);