2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004, 2007 Lukas Ertl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR 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 AUTHOR 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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/libkern.h>
35 #include <sys/malloc.h>
37 #include <geom/geom.h>
38 #include <geom/vinum/geom_vinum_var.h>
39 #include <geom/vinum/geom_vinum.h>
41 /* General 'remove' routine. */
43 gv_remove(struct g_geom *gp, struct gctl_req *req)
54 argc = gctl_get_paraml(req, "argc", sizeof(*argc));
56 if (argc == NULL || *argc == 0) {
57 gctl_error(req, "no arguments given");
61 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
63 gctl_error(req, "no flags given");
69 /* XXX config locking */
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);
82 * If this volume has plexes, we want a recursive
85 if (!LIST_EMPTY(&v->plexes) && !(*flags & GV_FLAG_R)) {
86 gctl_error(req, "volume '%s' has attached "
87 "plexes - need recursive removal", v->name);
91 gv_post_event(sc, GV_EVENT_RM_VOLUME, v, NULL, 0, 0);
95 p = gv_find_plex(sc, argv);
98 * If this plex has subdisks, we want a recursive
101 if (!LIST_EMPTY(&p->subdisks) &&
102 !(*flags & GV_FLAG_R)) {
103 gctl_error(req, "plex '%s' has attached "
104 "subdisks - need recursive removal",
109 /* Don't allow removal of the only plex of a volume. */
110 if (p->vol_sc != NULL && p->vol_sc->plexcount == 1) {
111 gctl_error(req, "plex '%s' is still attached "
112 "to volume '%s'", p->name, p->volume);
116 gv_post_event(sc, GV_EVENT_RM_PLEX, p, NULL, 0, 0);
120 s = gv_find_sd(sc, argv);
122 /* Don't allow removal if attached to a plex. */
123 if (s->plex_sc != NULL) {
124 gctl_error(req, "subdisk '%s' is still attached"
125 " to plex '%s'", s->name, s->plex_sc->name);
129 gv_post_event(sc, GV_EVENT_RM_SD, s, NULL, 0, 0);
133 d = gv_find_drive(sc, argv);
134 /* We don't allow to remove open drives. */
135 if (gv_consumer_is_open(d->consumer) &&
136 !(*flags & GV_FLAG_F)) {
137 gctl_error(req, "drive '%s' is open", d->name);
141 /* A drive with subdisks needs a recursive removal. */
142 /* if (!LIST_EMPTY(&d->subdisks) &&
143 !(*flags & GV_FLAG_R)) {
144 gctl_error(req, "drive '%s' still has subdisks"
145 " - need recursive removal", d->name);
149 gv_post_event(sc, GV_EVENT_RM_DRIVE, d, NULL, *flags,
154 gctl_error(req, "unknown object '%s'", argv);
159 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
162 /* Resets configuration */
164 gv_resetconfig(struct gv_softc *sc)
166 struct gv_drive *d, *d2;
167 struct gv_volume *v, *v2;
168 struct gv_plex *p, *p2;
169 struct gv_sd *s, *s2;
171 /* First make sure nothing is open. */
172 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) {
173 if (gv_consumer_is_open(d->consumer)) {
174 return (GV_ERR_ISBUSY);
178 /* Make sure nothing is going on internally. */
179 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) {
180 if (p->flags & (GV_PLEX_REBUILDING | GV_PLEX_GROWING))
181 return (GV_ERR_ISBUSY);
184 /* Then if not, we remove everything. */
185 LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2)
187 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2)
188 gv_rm_drive(sc, d, 0);
189 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2)
191 LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2)
194 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
199 /* Remove a volume. */
201 gv_rm_vol(struct gv_softc *sc, struct gv_volume *v)
203 struct g_provider *pp;
204 struct gv_plex *p, *p2;
206 KASSERT(v != NULL, ("gv_rm_vol: NULL v"));
208 KASSERT(pp != NULL, ("gv_rm_vol: NULL pp"));
210 /* Check if any of our consumers is open. */
211 if (gv_provider_is_open(pp)) {
212 G_VINUM_DEBUG(0, "unable to remove %s: volume still in use",
217 /* Remove the plexes our volume has. */
218 LIST_FOREACH_SAFE(p, &v->plexes, in_volume, p2)
222 LIST_REMOVE(v, volume);
225 /* Get rid of the volume's provider. */
228 g_wither_provider(pp, ENXIO);
235 gv_rm_plex(struct gv_softc *sc, struct gv_plex *p)
238 struct gv_sd *s, *s2;
240 KASSERT(p != NULL, ("gv_rm_plex: NULL p"));
243 /* Check if any of our consumers is open. */
244 if (v != NULL && gv_provider_is_open(v->provider) && v->plexcount < 2) {
245 G_VINUM_DEBUG(0, "unable to remove %s: volume still in use",
250 /* Remove the subdisks our plex has. */
251 LIST_FOREACH_SAFE(s, &p->subdisks, in_plex, s2)
255 /* Clean up and let our geom fade away. */
256 LIST_REMOVE(p, plex);
257 if (p->vol_sc != NULL) {
258 p->vol_sc->plexcount--;
259 LIST_REMOVE(p, in_volume);
261 /* Correctly update the volume size. */
262 gv_update_vol_size(v, gv_vol_size(v));
268 /* Remove a subdisk. */
270 gv_rm_sd(struct gv_softc *sc, struct gv_sd *s)
275 KASSERT(s != NULL, ("gv_rm_sd: NULL s"));
282 LIST_REMOVE(s, in_plex);
285 /* Update the plexsize. */
286 p->size = gv_plex_size(p);
289 /* Update the size of our plex' volume. */
290 gv_update_vol_size(v, gv_vol_size(v));
293 if (s->drive_sc && !(s->drive_sc->flags & GV_DRIVE_REFERENCED))
294 LIST_REMOVE(s, from_drive);
300 /* Remove a drive. */
302 gv_rm_drive(struct gv_softc *sc, struct gv_drive *d, int flags)
304 struct g_consumer *cp;
305 struct gv_freelist *fl, *fl2;
307 struct gv_sd *s, *s2;
312 KASSERT(d != NULL, ("gv_rm_drive: NULL d"));
318 err = g_access(cp, 0, 1, 0);
322 G_VINUM_DEBUG(0, "%s: unable to access '%s', "
323 "errno: %d", __func__, cp->provider->name, err);
327 /* Clear the Vinum Magic. */
328 d->hdr->magic = GV_NOMAGIC;
329 err = gv_write_header(cp, d->hdr);
331 G_VINUM_DEBUG(0, "gv_rm_drive: error writing header to"
332 " '%s', errno: %d", cp->provider->name, err);
335 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
337 g_destroy_consumer(cp);
341 /* Remove all associated subdisks, plexes, volumes. */
342 if (flags & GV_FLAG_R) {
343 if (!LIST_EMPTY(&d->subdisks)) {
344 LIST_FOREACH_SAFE(s, &d->subdisks, from_drive, s2) {
356 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
357 LIST_REMOVE(fl, freelist);
361 LIST_REMOVE(d, drive);
364 /* Put ourself into referenced state if we have subdisks. */
365 if (d->sdcount > 0) {
368 d->flags |= GV_DRIVE_REFERENCED;
369 snprintf(d->device, sizeof(d->device), "???");
372 d->freelist_entries = 0;
373 LIST_FOREACH(s, &d->subdisks, from_drive) {
374 s->flags |= GV_SD_TASTED;
375 gv_set_sd_state(s, GV_SD_DOWN, GV_SETSTATE_FORCE);
377 /* Shuffle around so we keep gv_is_newer happy. */
378 LIST_REMOVE(d, drive);
379 d2 = LIST_FIRST(&sc->drives);
381 LIST_INSERT_HEAD(&sc->drives, d, drive);
383 LIST_INSERT_AFTER(d2, d, drive);