xref: /freebsd/sys/geom/vinum/geom_vinum_rm.c (revision 3494f7c0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  *  Copyright (c) 2004, 2007 Lukas Ertl
5  *  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/param.h>
31 #include <sys/libkern.h>
32 #include <sys/malloc.h>
33 
34 #include <geom/geom.h>
35 #include <geom/geom_dbg.h>
36 #include <geom/vinum/geom_vinum_var.h>
37 #include <geom/vinum/geom_vinum.h>
38 
39 /* General 'remove' routine. */
40 void
41 gv_remove(struct g_geom *gp, struct gctl_req *req)
42 {
43 	struct gv_softc *sc;
44 	struct gv_volume *v;
45 	struct gv_plex *p;
46 	struct gv_sd *s;
47 	struct gv_drive *d;
48 	int *argc, *flags;
49 	char *argv, buf[20];
50 	int i, type;
51 
52 	argc = gctl_get_paraml(req, "argc", sizeof(*argc));
53 
54 	if (argc == NULL || *argc == 0) {
55 		gctl_error(req, "no arguments given");
56 		return;
57 	}
58 
59 	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
60 	if (flags == NULL) {
61 		gctl_error(req, "no flags given");
62 		return;
63 	}
64 
65 	sc = gp->softc;
66 
67 	/* XXX config locking */
68 
69 	for (i = 0; i < *argc; i++) {
70 		snprintf(buf, sizeof(buf), "argv%d", i);
71 		argv = gctl_get_param(req, buf, NULL);
72 		if (argv == NULL)
73 			continue;
74 		type = gv_object_type(sc, argv);
75 		switch (type) {
76 		case GV_TYPE_VOL:
77 			v = gv_find_vol(sc, argv);
78 
79 			/*
80 			 * If this volume has plexes, we want a recursive
81 			 * removal.
82 			 */
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);
86 				return;
87 			}
88 
89 			gv_post_event(sc, GV_EVENT_RM_VOLUME, v, NULL, 0, 0);
90 			break;
91 
92 		case GV_TYPE_PLEX:
93 			p = gv_find_plex(sc, argv);
94 
95 			/*
96 			 * If this plex has subdisks, we want a recursive
97 			 * removal.
98 			 */
99 			if (!LIST_EMPTY(&p->subdisks) &&
100 			    !(*flags & GV_FLAG_R)) {
101 				gctl_error(req, "plex '%s' has attached "
102 				    "subdisks - need recursive removal",
103 				    p->name);
104 				return;
105 			}
106 
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);
111 				return;
112 			}
113 
114 			gv_post_event(sc, GV_EVENT_RM_PLEX, p, NULL, 0, 0);
115 			break;
116 
117 		case GV_TYPE_SD:
118 			s = gv_find_sd(sc, argv);
119 
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);
124 				return;
125 			}
126 
127 			gv_post_event(sc, GV_EVENT_RM_SD, s, NULL, 0, 0);
128 			break;
129 
130 		case GV_TYPE_DRIVE:
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);
136 				return;
137 			}
138 
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);
144 				return;
145 			}*/
146 
147 			gv_post_event(sc, GV_EVENT_RM_DRIVE, d, NULL, *flags,
148 			    0);
149 			break;
150 
151 		default:
152 			gctl_error(req, "unknown object '%s'", argv);
153 			return;
154 		}
155 	}
156 
157 	gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
158 }
159 
160 /* Resets configuration */
161 int
162 gv_resetconfig(struct gv_softc *sc)
163 {
164 	struct gv_drive *d, *d2;
165 	struct gv_volume *v, *v2;
166 	struct gv_plex *p, *p2;
167 	struct gv_sd *s, *s2;
168 
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);
173 		}
174 	}
175 
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);
180 	}
181 
182 	/* Then if not, we remove everything. */
183 	LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2)
184 		gv_rm_sd(sc, s);
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)
188 		gv_rm_plex(sc, p);
189 	LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2)
190 		gv_rm_vol(sc, v);
191 
192 	gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
193 
194 	return (0);
195 }
196 
197 /* Remove a volume. */
198 void
199 gv_rm_vol(struct gv_softc *sc, struct gv_volume *v)
200 {
201 	struct g_provider *pp;
202 	struct gv_plex *p, *p2;
203 
204 	KASSERT(v != NULL, ("gv_rm_vol: NULL v"));
205 	pp = v->provider;
206 	KASSERT(pp != NULL, ("gv_rm_vol: NULL pp"));
207 
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",
211 		    v->name);
212 		return;
213 	}
214 
215 	/* Remove the plexes our volume has. */
216 	LIST_FOREACH_SAFE(p, &v->plexes, in_volume, p2)
217 		gv_rm_plex(sc, p);
218 
219 	/* Clean up. */
220 	LIST_REMOVE(v, volume);
221 	g_free(v);
222 
223 	/* Get rid of the volume's provider. */
224 	if (pp != NULL) {
225 		g_topology_lock();
226 		g_wither_provider(pp, ENXIO);
227 		g_topology_unlock();
228 	}
229 }
230 
231 /* Remove a plex. */
232 void
233 gv_rm_plex(struct gv_softc *sc, struct gv_plex *p)
234 {
235 	struct gv_volume *v;
236 	struct gv_sd *s, *s2;
237 
238 	KASSERT(p != NULL, ("gv_rm_plex: NULL p"));
239 	v = p->vol_sc;
240 
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",
244 		    p->name);
245 		return;
246 	}
247 
248 	/* Remove the subdisks our plex has. */
249 	LIST_FOREACH_SAFE(s, &p->subdisks, in_plex, s2)
250 		gv_rm_sd(sc, s);
251 
252 	v = p->vol_sc;
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);
258 		p->vol_sc = NULL;
259 		/* Correctly update the volume size. */
260 		gv_update_vol_size(v, gv_vol_size(v));
261 	}
262 
263 	g_free(p);
264 }
265 
266 /* Remove a subdisk. */
267 void
268 gv_rm_sd(struct gv_softc *sc, struct gv_sd *s)
269 {
270 	struct gv_plex *p;
271 	struct gv_volume *v;
272 
273 	KASSERT(s != NULL, ("gv_rm_sd: NULL s"));
274 
275 	p = s->plex_sc;
276 	v = NULL;
277 
278 	/* Clean up. */
279 	if (p != NULL) {
280 		LIST_REMOVE(s, in_plex);
281 		s->plex_sc = NULL;
282 		p->sdcount--;
283 		/* Update the plexsize. */
284 		p->size = gv_plex_size(p);
285 		v = p->vol_sc;
286 		if (v != NULL) {
287 			/* Update the size of our plex' volume. */
288 			gv_update_vol_size(v, gv_vol_size(v));
289 		}
290 	}
291 	if (s->drive_sc && !(s->drive_sc->flags & GV_DRIVE_REFERENCED))
292 		LIST_REMOVE(s, from_drive);
293 	LIST_REMOVE(s, sd);
294 	gv_free_sd(s);
295 	g_free(s);
296 }
297 
298 /* Remove a drive. */
299 void
300 gv_rm_drive(struct gv_softc *sc, struct gv_drive *d, int flags)
301 {
302 	struct g_consumer *cp;
303 	struct gv_freelist *fl, *fl2;
304 	struct gv_plex *p;
305 	struct gv_sd *s, *s2;
306 	struct gv_volume *v;
307 	struct gv_drive *d2;
308 	int err;
309 
310 	KASSERT(d != NULL, ("gv_rm_drive: NULL d"));
311 
312 	cp = d->consumer;
313 
314 	if (cp != NULL) {
315 		g_topology_lock();
316 		err = g_access(cp, 0, 1, 0);
317 		g_topology_unlock();
318 
319 		if (err) {
320 			G_VINUM_DEBUG(0, "%s: unable to access '%s', "
321 			    "errno: %d", __func__, cp->provider->name, err);
322 			return;
323 		}
324 
325 		/* Clear the Vinum Magic. */
326 		d->hdr->magic = GV_NOMAGIC;
327 		err = gv_write_header(cp, d->hdr);
328 		if (err)
329 			G_VINUM_DEBUG(0, "gv_rm_drive: error writing header to"
330 			    " '%s', errno: %d", cp->provider->name, err);
331 
332 		g_topology_lock();
333 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
334 		g_detach(cp);
335 		g_destroy_consumer(cp);
336 		g_topology_unlock();
337 	}
338 
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) {
343 				p = s->plex_sc;
344 				if (p != NULL) {
345 					v = p->vol_sc;
346 					if (v != NULL)
347 						gv_rm_vol(sc, v);
348 				}
349 			}
350 		}
351 	}
352 
353 	/* Clean up. */
354 	LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
355 		LIST_REMOVE(fl, freelist);
356 		g_free(fl);
357 	}
358 
359 	LIST_REMOVE(d, drive);
360 	g_free(d->hdr);
361 
362 	/* Put ourself into referenced state if we have subdisks. */
363 	if (d->sdcount > 0) {
364 		d->consumer = NULL;
365 		d->hdr = NULL;
366 		d->flags |= GV_DRIVE_REFERENCED;
367 		snprintf(d->device, sizeof(d->device), "???");
368 		d->size = 0;
369 		d->avail = 0;
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);
374 		}
375 		/* Shuffle around so we keep gv_is_newer happy. */
376 		LIST_REMOVE(d, drive);
377 		d2 = LIST_FIRST(&sc->drives);
378 		if (d2 == NULL)
379 			LIST_INSERT_HEAD(&sc->drives, d, drive);
380 		else
381 			LIST_INSERT_AFTER(d2, d, drive);
382 		return;
383 	}
384 	g_free(d);
385 
386 	gv_save_config(sc);
387 }
388