xref: /freebsd/sys/geom/vinum/geom_vinum_rm.c (revision edf8578117e8844e02c0121147f45e4609b30680)
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/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/libkern.h>
33 #include <sys/malloc.h>
34 
35 #include <geom/geom.h>
36 #include <geom/geom_dbg.h>
37 #include <geom/vinum/geom_vinum_var.h>
38 #include <geom/vinum/geom_vinum.h>
39 
40 /* General 'remove' routine. */
41 void
42 gv_remove(struct g_geom *gp, struct gctl_req *req)
43 {
44 	struct gv_softc *sc;
45 	struct gv_volume *v;
46 	struct gv_plex *p;
47 	struct gv_sd *s;
48 	struct gv_drive *d;
49 	int *argc, *flags;
50 	char *argv, buf[20];
51 	int i, type;
52 
53 	argc = gctl_get_paraml(req, "argc", sizeof(*argc));
54 
55 	if (argc == NULL || *argc == 0) {
56 		gctl_error(req, "no arguments given");
57 		return;
58 	}
59 
60 	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
61 	if (flags == NULL) {
62 		gctl_error(req, "no flags given");
63 		return;
64 	}
65 
66 	sc = gp->softc;
67 
68 	/* XXX config locking */
69 
70 	for (i = 0; i < *argc; i++) {
71 		snprintf(buf, sizeof(buf), "argv%d", i);
72 		argv = gctl_get_param(req, buf, NULL);
73 		if (argv == NULL)
74 			continue;
75 		type = gv_object_type(sc, argv);
76 		switch (type) {
77 		case GV_TYPE_VOL:
78 			v = gv_find_vol(sc, argv);
79 
80 			/*
81 			 * If this volume has plexes, we want a recursive
82 			 * removal.
83 			 */
84 			if (!LIST_EMPTY(&v->plexes) && !(*flags & GV_FLAG_R)) {
85 				gctl_error(req, "volume '%s' has attached "
86 				    "plexes - need recursive removal", v->name);
87 				return;
88 			}
89 
90 			gv_post_event(sc, GV_EVENT_RM_VOLUME, v, NULL, 0, 0);
91 			break;
92 
93 		case GV_TYPE_PLEX:
94 			p = gv_find_plex(sc, argv);
95 
96 			/*
97 			 * If this plex has subdisks, we want a recursive
98 			 * removal.
99 			 */
100 			if (!LIST_EMPTY(&p->subdisks) &&
101 			    !(*flags & GV_FLAG_R)) {
102 				gctl_error(req, "plex '%s' has attached "
103 				    "subdisks - need recursive removal",
104 				    p->name);
105 				return;
106 			}
107 
108 			/* Don't allow removal of the only plex of a volume. */
109 			if (p->vol_sc != NULL && p->vol_sc->plexcount == 1) {
110 				gctl_error(req, "plex '%s' is still attached "
111 				    "to volume '%s'", p->name, p->volume);
112 				return;
113 			}
114 
115 			gv_post_event(sc, GV_EVENT_RM_PLEX, p, NULL, 0, 0);
116 			break;
117 
118 		case GV_TYPE_SD:
119 			s = gv_find_sd(sc, argv);
120 
121 			/* Don't allow removal if attached to a plex. */
122 			if (s->plex_sc != NULL) {
123 				gctl_error(req, "subdisk '%s' is still attached"
124 				    " to plex '%s'", s->name, s->plex_sc->name);
125 				return;
126 			}
127 
128 			gv_post_event(sc, GV_EVENT_RM_SD, s, NULL, 0, 0);
129 			break;
130 
131 		case GV_TYPE_DRIVE:
132 			d = gv_find_drive(sc, argv);
133 			/* We don't allow to remove open drives. */
134 			if (gv_consumer_is_open(d->consumer) &&
135 			    !(*flags & GV_FLAG_F)) {
136 				gctl_error(req, "drive '%s' is open", d->name);
137 				return;
138 			}
139 
140 			/* A drive with subdisks needs a recursive removal. */
141 /*			if (!LIST_EMPTY(&d->subdisks) &&
142 			    !(*flags & GV_FLAG_R)) {
143 				gctl_error(req, "drive '%s' still has subdisks"
144 				    " - need recursive removal", d->name);
145 				return;
146 			}*/
147 
148 			gv_post_event(sc, GV_EVENT_RM_DRIVE, d, NULL, *flags,
149 			    0);
150 			break;
151 
152 		default:
153 			gctl_error(req, "unknown object '%s'", argv);
154 			return;
155 		}
156 	}
157 
158 	gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
159 }
160 
161 /* Resets configuration */
162 int
163 gv_resetconfig(struct gv_softc *sc)
164 {
165 	struct gv_drive *d, *d2;
166 	struct gv_volume *v, *v2;
167 	struct gv_plex *p, *p2;
168 	struct gv_sd *s, *s2;
169 
170 	/* First make sure nothing is open. */
171         LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) {
172 		if (gv_consumer_is_open(d->consumer)) {
173 			return (GV_ERR_ISBUSY);
174 		}
175 	}
176 
177 	/* Make sure nothing is going on internally. */
178 	LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) {
179 		if (p->flags & (GV_PLEX_REBUILDING | GV_PLEX_GROWING))
180 			return (GV_ERR_ISBUSY);
181 	}
182 
183 	/* Then if not, we remove everything. */
184 	LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2)
185 		gv_rm_sd(sc, s);
186 	LIST_FOREACH_SAFE(d, &sc->drives, drive, d2)
187 		gv_rm_drive(sc, d, 0);
188 	LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2)
189 		gv_rm_plex(sc, p);
190 	LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2)
191 		gv_rm_vol(sc, v);
192 
193 	gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
194 
195 	return (0);
196 }
197 
198 /* Remove a volume. */
199 void
200 gv_rm_vol(struct gv_softc *sc, struct gv_volume *v)
201 {
202 	struct g_provider *pp;
203 	struct gv_plex *p, *p2;
204 
205 	KASSERT(v != NULL, ("gv_rm_vol: NULL v"));
206 	pp = v->provider;
207 	KASSERT(pp != NULL, ("gv_rm_vol: NULL pp"));
208 
209 	/* Check if any of our consumers is open. */
210 	if (gv_provider_is_open(pp)) {
211 		G_VINUM_DEBUG(0, "unable to remove %s: volume still in use",
212 		    v->name);
213 		return;
214 	}
215 
216 	/* Remove the plexes our volume has. */
217 	LIST_FOREACH_SAFE(p, &v->plexes, in_volume, p2)
218 		gv_rm_plex(sc, p);
219 
220 	/* Clean up. */
221 	LIST_REMOVE(v, volume);
222 	g_free(v);
223 
224 	/* Get rid of the volume's provider. */
225 	if (pp != NULL) {
226 		g_topology_lock();
227 		g_wither_provider(pp, ENXIO);
228 		g_topology_unlock();
229 	}
230 }
231 
232 /* Remove a plex. */
233 void
234 gv_rm_plex(struct gv_softc *sc, struct gv_plex *p)
235 {
236 	struct gv_volume *v;
237 	struct gv_sd *s, *s2;
238 
239 	KASSERT(p != NULL, ("gv_rm_plex: NULL p"));
240 	v = p->vol_sc;
241 
242 	/* Check if any of our consumers is open. */
243 	if (v != NULL && gv_provider_is_open(v->provider) && v->plexcount < 2) {
244 		G_VINUM_DEBUG(0, "unable to remove %s: volume still in use",
245 		    p->name);
246 		return;
247 	}
248 
249 	/* Remove the subdisks our plex has. */
250 	LIST_FOREACH_SAFE(s, &p->subdisks, in_plex, s2)
251 		gv_rm_sd(sc, s);
252 
253 	v = p->vol_sc;
254 	/* Clean up and let our geom fade away. */
255 	LIST_REMOVE(p, plex);
256 	if (p->vol_sc != NULL) {
257 		p->vol_sc->plexcount--;
258 		LIST_REMOVE(p, in_volume);
259 		p->vol_sc = NULL;
260 		/* Correctly update the volume size. */
261 		gv_update_vol_size(v, gv_vol_size(v));
262 	}
263 
264 	g_free(p);
265 }
266 
267 /* Remove a subdisk. */
268 void
269 gv_rm_sd(struct gv_softc *sc, struct gv_sd *s)
270 {
271 	struct gv_plex *p;
272 	struct gv_volume *v;
273 
274 	KASSERT(s != NULL, ("gv_rm_sd: NULL s"));
275 
276 	p = s->plex_sc;
277 	v = NULL;
278 
279 	/* Clean up. */
280 	if (p != NULL) {
281 		LIST_REMOVE(s, in_plex);
282 		s->plex_sc = NULL;
283 		p->sdcount--;
284 		/* Update the plexsize. */
285 		p->size = gv_plex_size(p);
286 		v = p->vol_sc;
287 		if (v != NULL) {
288 			/* Update the size of our plex' volume. */
289 			gv_update_vol_size(v, gv_vol_size(v));
290 		}
291 	}
292 	if (s->drive_sc && !(s->drive_sc->flags & GV_DRIVE_REFERENCED))
293 		LIST_REMOVE(s, from_drive);
294 	LIST_REMOVE(s, sd);
295 	gv_free_sd(s);
296 	g_free(s);
297 }
298 
299 /* Remove a drive. */
300 void
301 gv_rm_drive(struct gv_softc *sc, struct gv_drive *d, int flags)
302 {
303 	struct g_consumer *cp;
304 	struct gv_freelist *fl, *fl2;
305 	struct gv_plex *p;
306 	struct gv_sd *s, *s2;
307 	struct gv_volume *v;
308 	struct gv_drive *d2;
309 	int err;
310 
311 	KASSERT(d != NULL, ("gv_rm_drive: NULL d"));
312 
313 	cp = d->consumer;
314 
315 	if (cp != NULL) {
316 		g_topology_lock();
317 		err = g_access(cp, 0, 1, 0);
318 		g_topology_unlock();
319 
320 		if (err) {
321 			G_VINUM_DEBUG(0, "%s: unable to access '%s', "
322 			    "errno: %d", __func__, cp->provider->name, err);
323 			return;
324 		}
325 
326 		/* Clear the Vinum Magic. */
327 		d->hdr->magic = GV_NOMAGIC;
328 		err = gv_write_header(cp, d->hdr);
329 		if (err)
330 			G_VINUM_DEBUG(0, "gv_rm_drive: error writing header to"
331 			    " '%s', errno: %d", cp->provider->name, err);
332 
333 		g_topology_lock();
334 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
335 		g_detach(cp);
336 		g_destroy_consumer(cp);
337 		g_topology_unlock();
338 	}
339 
340 	/* Remove all associated subdisks, plexes, volumes. */
341 	if (flags & GV_FLAG_R) {
342 		if (!LIST_EMPTY(&d->subdisks)) {
343 			LIST_FOREACH_SAFE(s, &d->subdisks, from_drive, s2) {
344 				p = s->plex_sc;
345 				if (p != NULL) {
346 					v = p->vol_sc;
347 					if (v != NULL)
348 						gv_rm_vol(sc, v);
349 				}
350 			}
351 		}
352 	}
353 
354 	/* Clean up. */
355 	LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
356 		LIST_REMOVE(fl, freelist);
357 		g_free(fl);
358 	}
359 
360 	LIST_REMOVE(d, drive);
361 	g_free(d->hdr);
362 
363 	/* Put ourself into referenced state if we have subdisks. */
364 	if (d->sdcount > 0) {
365 		d->consumer = NULL;
366 		d->hdr = NULL;
367 		d->flags |= GV_DRIVE_REFERENCED;
368 		snprintf(d->device, sizeof(d->device), "???");
369 		d->size = 0;
370 		d->avail = 0;
371 		d->freelist_entries = 0;
372 		LIST_FOREACH(s, &d->subdisks, from_drive) {
373 			s->flags |= GV_SD_TASTED;
374 			gv_set_sd_state(s, GV_SD_DOWN, GV_SETSTATE_FORCE);
375 		}
376 		/* Shuffle around so we keep gv_is_newer happy. */
377 		LIST_REMOVE(d, drive);
378 		d2 = LIST_FIRST(&sc->drives);
379 		if (d2 == NULL)
380 			LIST_INSERT_HEAD(&sc->drives, d, drive);
381 		else
382 			LIST_INSERT_AFTER(d2, d, drive);
383 		return;
384 	}
385 	g_free(d);
386 
387 	gv_save_config(sc);
388 }
389