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