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 void gv_free_sd(struct gv_sd *);
42 static int gv_rm_drive(struct gv_softc *, struct gctl_req *,
43 struct gv_drive *, int);
44 static int gv_rm_plex(struct gv_softc *, struct gctl_req *,
45 struct gv_plex *, int);
46 static int gv_rm_vol(struct gv_softc *, struct gctl_req *,
47 struct gv_volume *, int);
49 /* General 'remove' routine. */
51 gv_remove(struct g_geom *gp, struct gctl_req *req)
62 argc = gctl_get_paraml(req, "argc", sizeof(*argc));
63 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
65 if (argc == NULL || *argc == 0) {
66 gctl_error(req, "no arguments given");
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);
82 gctl_error(req, "unknown volume '%s'", argv);
85 err = gv_rm_vol(sc, req, v, *flags);
90 p = gv_find_plex(sc, argv);
92 gctl_error(req, "unknown plex '%s'", argv);
95 err = gv_rm_plex(sc, req, p, *flags);
100 s = gv_find_sd(sc, argv);
102 gctl_error(req, "unknown subdisk '%s'", argv);
105 err = gv_rm_sd(sc, req, s, *flags);
110 d = gv_find_drive(sc, argv);
112 gctl_error(req, "unknown drive '%s'", argv);
115 err = gv_rm_drive(sc, req, d, *flags);
120 gctl_error(req, "unknown object '%s'", argv);
125 gv_save_config_all(sc);
128 /* Remove a volume. */
130 gv_rm_vol(struct gv_softc *sc, struct gctl_req *req, struct gv_volume *v, int flags)
133 struct gv_plex *p, *p2;
137 KASSERT(v != NULL, ("gv_rm_vol: NULL v"));
139 /* If this volume has plexes, we want a recursive removal. */
140 if (!LIST_EMPTY(&v->plexes) && !(flags & GV_FLAG_R)) {
141 gctl_error(req, "volume '%s' has attached plexes", v->name);
147 /* Check if any of our consumers is open. */
148 if (gp != NULL && gv_is_open(gp)) {
149 gctl_error(req, "volume '%s' is busy", v->name);
153 /* Remove the plexes our volume has. */
154 LIST_FOREACH_SAFE(p, &v->plexes, in_volume, p2) {
156 LIST_REMOVE(p, in_volume);
159 err = gv_rm_plex(sc, req, p, flags);
164 /* Clean up and let our geom fade away. */
165 LIST_REMOVE(v, volume);
166 gv_kill_vol_thread(v);
170 g_wither_geom(gp, ENXIO);
178 gv_rm_plex(struct gv_softc *sc, struct gctl_req *req, struct gv_plex *p, int flags)
181 struct gv_sd *s, *s2;
186 KASSERT(p != NULL, ("gv_rm_plex: NULL p"));
188 /* If this plex has subdisks, we want a recursive removal. */
189 if (!LIST_EMPTY(&p->subdisks) && !(flags & GV_FLAG_R)) {
190 gctl_error(req, "plex '%s' has attached subdisks", p->name);
194 if (p->vol_sc != NULL && p->vol_sc->plexcount == 1) {
195 gctl_error(req, "plex '%s' is still attached to volume '%s'",
202 /* Check if any of our consumers is open. */
203 if (gp != NULL && gv_is_open(gp)) {
204 gctl_error(req, "plex '%s' is busy", p->name);
208 /* Remove the subdisks our plex has. */
209 LIST_FOREACH_SAFE(s, &p->subdisks, in_plex, s2) {
212 LIST_REMOVE(s, in_plex);
216 err = gv_rm_sd(sc, req, s, flags);
221 /* Clean up and let our geom fade away. */
222 LIST_REMOVE(p, plex);
223 if (p->vol_sc != NULL) {
224 p->vol_sc->plexcount--;
225 LIST_REMOVE(p, in_volume);
229 gv_kill_plex_thread(p);
234 g_wither_geom(gp, ENXIO);
240 /* Remove a subdisk. */
242 gv_rm_sd(struct gv_softc *sc, struct gctl_req *req, struct gv_sd *s, int flags)
244 struct g_provider *pp;
246 KASSERT(s != NULL, ("gv_rm_sd: NULL s"));
252 LIST_REMOVE(s, in_plex);
254 LIST_REMOVE(s, from_drive);
259 /* If the subdisk has a provider we need to clean up this one too. */
261 pp->flags |= G_PF_WITHER;
262 g_orphan_provider(pp, ENXIO);
268 /* Remove a drive. */
270 gv_rm_drive(struct gv_softc *sc, struct gctl_req *req, struct gv_drive *d, int flags)
273 struct g_consumer *cp;
274 struct gv_freelist *fl, *fl2;
276 struct gv_sd *s, *s2;
280 KASSERT(d != NULL, ("gv_rm_drive: NULL d"));
282 KASSERT(gp != NULL, ("gv_rm_drive: NULL gp"));
284 /* We don't allow to remove open drives. */
285 if (gv_is_open(gp)) {
286 gctl_error(req, "drive '%s' is open", d->name);
290 /* A drive with subdisks needs a recursive removal. */
291 if (!LIST_EMPTY(&d->subdisks) && !(flags & GV_FLAG_R)) {
292 gctl_error(req, "drive '%s' still has subdisks", d->name);
296 cp = LIST_FIRST(&gp->consumer);
297 err = g_access(cp, 0, 1, 0);
299 printf("GEOM_VINUM: gv_rm_drive: couldn't access '%s', errno: "
300 "%d\n", cp->provider->name, err);
304 /* Clear the Vinum Magic. */
305 d->hdr->magic = GV_NOMAGIC;
307 err = g_write_data(cp, GV_HDR_OFFSET, d->hdr, GV_HDR_LEN);
309 printf("GEOM_VINUM: gv_rm_drive: couldn't write header to '%s'"
310 ", errno: %d\n", cp->provider->name, err);
311 d->hdr->magic = GV_MAGIC;
314 g_access(cp, 0, -1, 0);
316 /* Remove all associated subdisks, plexes, volumes. */
317 if (!LIST_EMPTY(&d->subdisks)) {
318 LIST_FOREACH_SAFE(s, &d->subdisks, from_drive, s2) {
323 gv_rm_vol(sc, req, v, flags);
329 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
330 LIST_REMOVE(fl, freelist);
333 LIST_REMOVE(d, drive);
335 gv_kill_drive_thread(d);
340 gv_save_config_all(sc);
341 g_wither_geom(gp, ENXIO);
347 gv_free_sd(struct gv_sd *s)
350 struct gv_freelist *fl, *fl2;
352 KASSERT(s != NULL, ("gv_free_sd: NULL s"));
359 * First, find the free slot that's immediately before or after this
363 LIST_FOREACH(fl, &d->freelist, freelist) {
364 if (fl->offset == s->drive_offset + s->size)
366 if (fl->offset + fl->size == s->drive_offset)
370 /* If there is no free slot behind this subdisk, so create one. */
373 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
375 fl->offset = s->drive_offset;
377 if (d->freelist_entries == 0) {
378 LIST_INSERT_HEAD(&d->freelist, fl, freelist);
380 LIST_FOREACH(fl2, &d->freelist, freelist) {
381 if (fl->offset < fl2->offset) {
382 LIST_INSERT_BEFORE(fl2, fl, freelist);
384 } else if (LIST_NEXT(fl2, freelist) == NULL) {
385 LIST_INSERT_AFTER(fl2, fl, freelist);
391 d->freelist_entries++;
393 /* Expand the free slot we just found. */
396 if (fl->offset > s->drive_offset)
397 fl->offset = s->drive_offset;