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/geom_dbg.h>
39 #include <geom/vinum/geom_vinum_var.h>
40 #include <geom/vinum/geom_vinum.h>
42 /* General 'remove' routine. */
44 gv_remove(struct g_geom *gp, struct gctl_req *req)
55 argc = gctl_get_paraml(req, "argc", sizeof(*argc));
57 if (argc == NULL || *argc == 0) {
58 gctl_error(req, "no arguments given");
62 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
64 gctl_error(req, "no flags given");
70 /* XXX config locking */
72 for (i = 0; i < *argc; i++) {
73 snprintf(buf, sizeof(buf), "argv%d", i);
74 argv = gctl_get_param(req, buf, NULL);
77 type = gv_object_type(sc, argv);
80 v = gv_find_vol(sc, argv);
83 * If this volume has plexes, we want a recursive
86 if (!LIST_EMPTY(&v->plexes) && !(*flags & GV_FLAG_R)) {
87 gctl_error(req, "volume '%s' has attached "
88 "plexes - need recursive removal", v->name);
92 gv_post_event(sc, GV_EVENT_RM_VOLUME, v, NULL, 0, 0);
96 p = gv_find_plex(sc, argv);
99 * If this plex has subdisks, we want a recursive
102 if (!LIST_EMPTY(&p->subdisks) &&
103 !(*flags & GV_FLAG_R)) {
104 gctl_error(req, "plex '%s' has attached "
105 "subdisks - need recursive removal",
110 /* Don't allow removal of the only plex of a volume. */
111 if (p->vol_sc != NULL && p->vol_sc->plexcount == 1) {
112 gctl_error(req, "plex '%s' is still attached "
113 "to volume '%s'", p->name, p->volume);
117 gv_post_event(sc, GV_EVENT_RM_PLEX, p, NULL, 0, 0);
121 s = gv_find_sd(sc, argv);
123 /* Don't allow removal if attached to a plex. */
124 if (s->plex_sc != NULL) {
125 gctl_error(req, "subdisk '%s' is still attached"
126 " to plex '%s'", s->name, s->plex_sc->name);
130 gv_post_event(sc, GV_EVENT_RM_SD, s, NULL, 0, 0);
134 d = gv_find_drive(sc, argv);
135 /* We don't allow to remove open drives. */
136 if (gv_consumer_is_open(d->consumer) &&
137 !(*flags & GV_FLAG_F)) {
138 gctl_error(req, "drive '%s' is open", d->name);
142 /* A drive with subdisks needs a recursive removal. */
143 /* if (!LIST_EMPTY(&d->subdisks) &&
144 !(*flags & GV_FLAG_R)) {
145 gctl_error(req, "drive '%s' still has subdisks"
146 " - need recursive removal", d->name);
150 gv_post_event(sc, GV_EVENT_RM_DRIVE, d, NULL, *flags,
155 gctl_error(req, "unknown object '%s'", argv);
160 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
163 /* Resets configuration */
165 gv_resetconfig(struct gv_softc *sc)
167 struct gv_drive *d, *d2;
168 struct gv_volume *v, *v2;
169 struct gv_plex *p, *p2;
170 struct gv_sd *s, *s2;
172 /* First make sure nothing is open. */
173 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) {
174 if (gv_consumer_is_open(d->consumer)) {
175 return (GV_ERR_ISBUSY);
179 /* Make sure nothing is going on internally. */
180 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) {
181 if (p->flags & (GV_PLEX_REBUILDING | GV_PLEX_GROWING))
182 return (GV_ERR_ISBUSY);
185 /* Then if not, we remove everything. */
186 LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2)
188 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2)
189 gv_rm_drive(sc, d, 0);
190 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2)
192 LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2)
195 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
200 /* Remove a volume. */
202 gv_rm_vol(struct gv_softc *sc, struct gv_volume *v)
204 struct g_provider *pp;
205 struct gv_plex *p, *p2;
207 KASSERT(v != NULL, ("gv_rm_vol: NULL v"));
209 KASSERT(pp != NULL, ("gv_rm_vol: NULL pp"));
211 /* Check if any of our consumers is open. */
212 if (gv_provider_is_open(pp)) {
213 G_VINUM_DEBUG(0, "unable to remove %s: volume still in use",
218 /* Remove the plexes our volume has. */
219 LIST_FOREACH_SAFE(p, &v->plexes, in_volume, p2)
223 LIST_REMOVE(v, volume);
226 /* Get rid of the volume's provider. */
229 g_wither_provider(pp, ENXIO);
236 gv_rm_plex(struct gv_softc *sc, struct gv_plex *p)
239 struct gv_sd *s, *s2;
241 KASSERT(p != NULL, ("gv_rm_plex: NULL p"));
244 /* Check if any of our consumers is open. */
245 if (v != NULL && gv_provider_is_open(v->provider) && v->plexcount < 2) {
246 G_VINUM_DEBUG(0, "unable to remove %s: volume still in use",
251 /* Remove the subdisks our plex has. */
252 LIST_FOREACH_SAFE(s, &p->subdisks, in_plex, s2)
256 /* Clean up and let our geom fade away. */
257 LIST_REMOVE(p, plex);
258 if (p->vol_sc != NULL) {
259 p->vol_sc->plexcount--;
260 LIST_REMOVE(p, in_volume);
262 /* Correctly update the volume size. */
263 gv_update_vol_size(v, gv_vol_size(v));
269 /* Remove a subdisk. */
271 gv_rm_sd(struct gv_softc *sc, struct gv_sd *s)
276 KASSERT(s != NULL, ("gv_rm_sd: NULL s"));
283 LIST_REMOVE(s, in_plex);
286 /* Update the plexsize. */
287 p->size = gv_plex_size(p);
290 /* Update the size of our plex' volume. */
291 gv_update_vol_size(v, gv_vol_size(v));
294 if (s->drive_sc && !(s->drive_sc->flags & GV_DRIVE_REFERENCED))
295 LIST_REMOVE(s, from_drive);
301 /* Remove a drive. */
303 gv_rm_drive(struct gv_softc *sc, struct gv_drive *d, int flags)
305 struct g_consumer *cp;
306 struct gv_freelist *fl, *fl2;
308 struct gv_sd *s, *s2;
313 KASSERT(d != NULL, ("gv_rm_drive: NULL d"));
319 err = g_access(cp, 0, 1, 0);
323 G_VINUM_DEBUG(0, "%s: unable to access '%s', "
324 "errno: %d", __func__, cp->provider->name, err);
328 /* Clear the Vinum Magic. */
329 d->hdr->magic = GV_NOMAGIC;
330 err = gv_write_header(cp, d->hdr);
332 G_VINUM_DEBUG(0, "gv_rm_drive: error writing header to"
333 " '%s', errno: %d", cp->provider->name, err);
336 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
338 g_destroy_consumer(cp);
342 /* Remove all associated subdisks, plexes, volumes. */
343 if (flags & GV_FLAG_R) {
344 if (!LIST_EMPTY(&d->subdisks)) {
345 LIST_FOREACH_SAFE(s, &d->subdisks, from_drive, s2) {
357 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
358 LIST_REMOVE(fl, freelist);
362 LIST_REMOVE(d, drive);
365 /* Put ourself into referenced state if we have subdisks. */
366 if (d->sdcount > 0) {
369 d->flags |= GV_DRIVE_REFERENCED;
370 snprintf(d->device, sizeof(d->device), "???");
373 d->freelist_entries = 0;
374 LIST_FOREACH(s, &d->subdisks, from_drive) {
375 s->flags |= GV_SD_TASTED;
376 gv_set_sd_state(s, GV_SD_DOWN, GV_SETSTATE_FORCE);
378 /* Shuffle around so we keep gv_is_newer happy. */
379 LIST_REMOVE(d, drive);
380 d2 = LIST_FIRST(&sc->drives);
382 LIST_INSERT_HEAD(&sc->drives, d, drive);
384 LIST_INSERT_AFTER(d2, d, drive);