xref: /freebsd/sys/geom/mirror/g_mirror_ctl.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 /*-
2  * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS 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 THE AUTHORS 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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/bio.h>
37 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <sys/bitstring.h>
40 #include <vm/uma.h>
41 #include <machine/atomic.h>
42 #include <geom/geom.h>
43 #include <sys/proc.h>
44 #include <sys/kthread.h>
45 #include <geom/mirror/g_mirror.h>
46 
47 
48 static struct g_mirror_softc *
49 g_mirror_find_device(struct g_class *mp, const char *name)
50 {
51 	struct g_mirror_softc *sc;
52 	struct g_geom *gp;
53 
54 	g_topology_lock();
55 	LIST_FOREACH(gp, &mp->geom, geom) {
56 		sc = gp->softc;
57 		if (sc == NULL)
58 			continue;
59 		if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0)
60 			continue;
61 		if (strcmp(gp->name, name) == 0 ||
62 		    strcmp(sc->sc_name, name) == 0) {
63 			g_topology_unlock();
64 			sx_xlock(&sc->sc_lock);
65 			return (sc);
66 		}
67 	}
68 	g_topology_unlock();
69 	return (NULL);
70 }
71 
72 static struct g_mirror_disk *
73 g_mirror_find_disk(struct g_mirror_softc *sc, const char *name)
74 {
75 	struct g_mirror_disk *disk;
76 
77 	sx_assert(&sc->sc_lock, SX_XLOCKED);
78 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
79 		if (disk->d_consumer == NULL)
80 			continue;
81 		if (disk->d_consumer->provider == NULL)
82 			continue;
83 		if (strcmp(disk->d_consumer->provider->name, name) == 0)
84 			return (disk);
85 	}
86 	return (NULL);
87 }
88 
89 static void
90 g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp)
91 {
92 	struct g_mirror_softc *sc;
93 	struct g_mirror_disk *disk;
94 	const char *name, *balancep;
95 	intmax_t *slicep;
96 	uint32_t slice;
97 	uint8_t balance;
98 	int *nargs, *autosync, *noautosync, *hardcode, *dynamic, do_sync = 0;
99 
100 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
101 	if (nargs == NULL) {
102 		gctl_error(req, "No '%s' argument.", "nargs");
103 		return;
104 	}
105 	if (*nargs != 1) {
106 		gctl_error(req, "Invalid number of arguments.");
107 		return;
108 	}
109 	name = gctl_get_asciiparam(req, "arg0");
110 	if (name == NULL) {
111 		gctl_error(req, "No 'arg%u' argument.", 0);
112 		return;
113 	}
114 	balancep = gctl_get_asciiparam(req, "balance");
115 	if (balancep == NULL) {
116 		gctl_error(req, "No '%s' argument.", "balance");
117 		return;
118 	}
119 	autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
120 	if (autosync == NULL) {
121 		gctl_error(req, "No '%s' argument.", "autosync");
122 		return;
123 	}
124 	noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
125 	if (noautosync == NULL) {
126 		gctl_error(req, "No '%s' argument.", "noautosync");
127 		return;
128 	}
129 	hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
130 	if (hardcode == NULL) {
131 		gctl_error(req, "No '%s' argument.", "hardcode");
132 		return;
133 	}
134 	dynamic = gctl_get_paraml(req, "dynamic", sizeof(*dynamic));
135 	if (dynamic == NULL) {
136 		gctl_error(req, "No '%s' argument.", "dynamic");
137 		return;
138 	}
139 	if (*autosync && *noautosync) {
140 		gctl_error(req, "'%s' and '%s' specified.", "autosync",
141 		    "noautosync");
142 		return;
143 	}
144 	if (*hardcode && *dynamic) {
145 		gctl_error(req, "'%s' and '%s' specified.", "hardcode",
146 		    "dynamic");
147 		return;
148 	}
149 	sc = g_mirror_find_device(mp, name);
150 	if (sc == NULL) {
151 		gctl_error(req, "No such device: %s.", name);
152 		return;
153 	}
154 	if (strcmp(balancep, "none") == 0)
155 		balance = sc->sc_balance;
156 	else {
157 		if (balance_id(balancep) == -1) {
158 			gctl_error(req, "Invalid balance algorithm.");
159 			sx_xunlock(&sc->sc_lock);
160 			return;
161 		}
162 		balance = balance_id(balancep);
163 	}
164 	slicep = gctl_get_paraml(req, "slice", sizeof(*slicep));
165 	if (slicep == NULL) {
166 		gctl_error(req, "No '%s' argument.", "slice");
167 		sx_xunlock(&sc->sc_lock);
168 		return;
169 	}
170 	if (*slicep == -1)
171 		slice = sc->sc_slice;
172 	else
173 		slice = *slicep;
174 	if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
175 		sx_xunlock(&sc->sc_lock);
176 		gctl_error(req, "Not all disks connected. Try 'forget' command "
177 		    "first.");
178 		return;
179 	}
180 	if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync &&
181 	    !*noautosync && !*hardcode && !*dynamic) {
182 		sx_xunlock(&sc->sc_lock);
183 		gctl_error(req, "Nothing has changed.");
184 		return;
185 	}
186 	sc->sc_balance = balance;
187 	sc->sc_slice = slice;
188 	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) {
189 		if (*autosync) {
190 			sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
191 			do_sync = 1;
192 		}
193 	} else {
194 		if (*noautosync)
195 			sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
196 	}
197 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
198 		if (do_sync) {
199 			if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
200 				disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
201 		}
202 		if (*hardcode)
203 			disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
204 		else if (*dynamic)
205 			disk->d_flags &= ~G_MIRROR_DISK_FLAG_HARDCODED;
206 		g_mirror_update_metadata(disk);
207 		if (do_sync) {
208 			if (disk->d_state == G_MIRROR_DISK_STATE_STALE) {
209 				g_mirror_event_send(disk,
210 				    G_MIRROR_DISK_STATE_DISCONNECTED,
211 				    G_MIRROR_EVENT_DONTWAIT);
212 			}
213 		}
214 	}
215 	sx_xunlock(&sc->sc_lock);
216 }
217 
218 static void
219 g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
220 {
221 	struct g_mirror_metadata md;
222 	struct g_mirror_softc *sc;
223 	struct g_mirror_disk *disk;
224 	struct g_provider *pp;
225 	const char *name;
226 	char param[16];
227 	int error, *nargs;
228 	u_int i;
229 
230 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
231 	if (nargs == NULL) {
232 		gctl_error(req, "No '%s' argument.", "nargs");
233 		return;
234 	}
235 	if (*nargs < 2) {
236 		gctl_error(req, "Too few arguments.");
237 		return;
238 	}
239 	name = gctl_get_asciiparam(req, "arg0");
240 	if (name == NULL) {
241 		gctl_error(req, "No 'arg%u' argument.", 0);
242 		return;
243 	}
244 	sc = g_mirror_find_device(mp, name);
245 	if (sc == NULL) {
246 		gctl_error(req, "No such device: %s.", name);
247 		return;
248 	}
249 	for (i = 1; i < (u_int)*nargs; i++) {
250 		snprintf(param, sizeof(param), "arg%u", i);
251 		name = gctl_get_asciiparam(req, param);
252 		if (name == NULL) {
253 			gctl_error(req, "No 'arg%u' argument.", i);
254 			continue;
255 		}
256 		disk = g_mirror_find_disk(sc, name);
257 		if (disk == NULL) {
258 			gctl_error(req, "No such provider: %s.", name);
259 			continue;
260 		}
261 		if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
262 		    disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
263 			/*
264 			 * This is the last active disk. There will be nothing
265 			 * to rebuild it from, so deny this request.
266 			 */
267 			gctl_error(req,
268 			    "Provider %s is the last active provider in %s.",
269 			    name, sc->sc_geom->name);
270 			break;
271 		}
272 		/*
273 		 * Do rebuild by resetting syncid, disconnecting the disk and
274 		 * connecting it again.
275 		 */
276 		disk->d_sync.ds_syncid = 0;
277 		if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
278 			disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC;
279 		g_mirror_update_metadata(disk);
280 		pp = disk->d_consumer->provider;
281 		g_topology_lock();
282 		error = g_mirror_read_metadata(disk->d_consumer, &md);
283 		g_topology_unlock();
284 		g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
285 		    G_MIRROR_EVENT_WAIT);
286 		if (error != 0) {
287 			gctl_error(req, "Cannot read metadata from %s.",
288 			    pp->name);
289 			continue;
290 		}
291 		error = g_mirror_add_disk(sc, pp, &md);
292 		if (error != 0) {
293 			gctl_error(req, "Cannot reconnect component %s.",
294 			    pp->name);
295 			continue;
296 		}
297 	}
298 	sx_xunlock(&sc->sc_lock);
299 }
300 
301 static void
302 g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
303 {
304 	struct g_mirror_softc *sc;
305 	struct g_mirror_disk *disk;
306 	struct g_mirror_metadata md;
307 	struct g_provider *pp;
308 	struct g_consumer *cp;
309 	intmax_t *priority;
310 	const char *name;
311 	char param[16];
312 	u_char *sector;
313 	u_int i, n;
314 	int error, *nargs, *hardcode, *inactive;
315 	struct {
316 		struct g_provider	*provider;
317 		struct g_consumer	*consumer;
318 	} *disks;
319 
320 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
321 	if (nargs == NULL) {
322 		gctl_error(req, "No '%s' argument.", "nargs");
323 		return;
324 	}
325 	if (*nargs < 2) {
326 		gctl_error(req, "Too few arguments.");
327 		return;
328 	}
329 	priority = gctl_get_paraml(req, "priority", sizeof(*priority));
330 	if (priority == NULL) {
331 		gctl_error(req, "No '%s' argument.", "priority");
332 		return;
333 	}
334 	inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive));
335 	if (inactive == NULL) {
336 		gctl_error(req, "No '%s' argument.", "inactive");
337 		return;
338 	}
339 	hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
340 	if (hardcode == NULL) {
341 		gctl_error(req, "No '%s' argument.", "hardcode");
342 		return;
343 	}
344 	name = gctl_get_asciiparam(req, "arg0");
345 	if (name == NULL) {
346 		gctl_error(req, "No 'arg%u' argument.", 0);
347 		return;
348 	}
349 	sc = g_mirror_find_device(mp, name);
350 	if (sc == NULL) {
351 		gctl_error(req, "No such device: %s.", name);
352 		return;
353 	}
354 	if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
355 		gctl_error(req, "Not all disks connected.");
356 		sx_xunlock(&sc->sc_lock);
357 		return;
358 	}
359 
360 	disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO);
361 	g_topology_lock();
362 	for (i = 1, n = 0; i < (u_int)*nargs; i++) {
363 		snprintf(param, sizeof(param), "arg%u", i);
364 		name = gctl_get_asciiparam(req, param);
365 		if (name == NULL) {
366 			gctl_error(req, "No 'arg%u' argument.", i);
367 			continue;
368 		}
369 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
370 			name += strlen("/dev/");
371 		if (g_mirror_find_disk(sc, name) != NULL) {
372 			gctl_error(req, "Provider %s already inserted.", name);
373 			continue;
374 		}
375 		pp = g_provider_by_name(name);
376 		if (pp == NULL) {
377 			gctl_error(req, "Unknown provider %s.", name);
378 			continue;
379 		}
380 		if (sc->sc_provider->mediasize >
381 		    pp->mediasize - pp->sectorsize) {
382 			gctl_error(req, "Provider %s too small.", name);
383 			continue;
384 		}
385 		if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
386 			gctl_error(req, "Invalid sectorsize of provider %s.",
387 			    name);
388 			continue;
389 		}
390 		cp = g_new_consumer(sc->sc_geom);
391 		if (g_attach(cp, pp) != 0) {
392 			g_destroy_consumer(cp);
393 			gctl_error(req, "Cannot attach to provider %s.", name);
394 			continue;
395 		}
396 		if (g_access(cp, 0, 1, 1) != 0) {
397 			g_detach(cp);
398 			g_destroy_consumer(cp);
399 			gctl_error(req, "Cannot access provider %s.", name);
400 			continue;
401 		}
402 		disks[n].provider = pp;
403 		disks[n].consumer = cp;
404 		n++;
405 	}
406 	if (n == 0) {
407 		g_topology_unlock();
408 		sx_xunlock(&sc->sc_lock);
409 		g_free(disks);
410 		return;
411 	}
412 	sc->sc_ndisks += n;
413 again:
414 	for (i = 0; i < n; i++) {
415 		if (disks[i].consumer == NULL)
416 			continue;
417 		g_mirror_fill_metadata(sc, NULL, &md);
418 		md.md_priority = *priority;
419 		if (*inactive)
420 			md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
421 		pp = disks[i].provider;
422 		if (*hardcode) {
423 			strlcpy(md.md_provider, pp->name,
424 			    sizeof(md.md_provider));
425 		} else {
426 			bzero(md.md_provider, sizeof(md.md_provider));
427 		}
428 		md.md_provsize = pp->mediasize;
429 		sector = g_malloc(pp->sectorsize, M_WAITOK);
430 		mirror_metadata_encode(&md, sector);
431 		error = g_write_data(disks[i].consumer,
432 		    pp->mediasize - pp->sectorsize, sector, pp->sectorsize);
433 		g_free(sector);
434 		if (error != 0) {
435 			gctl_error(req, "Cannot store metadata on %s.",
436 			    pp->name);
437 			g_access(disks[i].consumer, 0, -1, -1);
438 			g_detach(disks[i].consumer);
439 			g_destroy_consumer(disks[i].consumer);
440 			disks[i].consumer = NULL;
441 			disks[i].provider = NULL;
442 			sc->sc_ndisks--;
443 			goto again;
444 		}
445 	}
446 	g_topology_unlock();
447 	if (i == 0) {
448 		/* All writes failed. */
449 		sx_xunlock(&sc->sc_lock);
450 		g_free(disks);
451 		return;
452 	}
453 	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
454 		g_mirror_update_metadata(disk);
455 	}
456 	/*
457 	 * Release provider and wait for retaste.
458 	 */
459 	g_topology_lock();
460 	for (i = 0; i < n; i++) {
461 		if (disks[i].consumer == NULL)
462 			continue;
463 		g_access(disks[i].consumer, 0, -1, -1);
464 		g_detach(disks[i].consumer);
465 		g_destroy_consumer(disks[i].consumer);
466 	}
467 	g_topology_unlock();
468 	sx_xunlock(&sc->sc_lock);
469 	g_free(disks);
470 }
471 
472 static void
473 g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp)
474 {
475 	struct g_mirror_softc *sc;
476 	struct g_mirror_disk *disk;
477 	const char *name;
478 	char param[16];
479 	int *nargs;
480 	u_int i;
481 
482 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
483 	if (nargs == NULL) {
484 		gctl_error(req, "No '%s' argument.", "nargs");
485 		return;
486 	}
487 	if (*nargs < 2) {
488 		gctl_error(req, "Too few arguments.");
489 		return;
490 	}
491 	name = gctl_get_asciiparam(req, "arg0");
492 	if (name == NULL) {
493 		gctl_error(req, "No 'arg%u' argument.", 0);
494 		return;
495 	}
496 	sc = g_mirror_find_device(mp, name);
497 	if (sc == NULL) {
498 		gctl_error(req, "No such device: %s.", name);
499 		return;
500 	}
501 	if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
502 		sx_xunlock(&sc->sc_lock);
503 		gctl_error(req, "Not all disks connected. Try 'forget' command "
504 		    "first.");
505 		return;
506 	}
507 	for (i = 1; i < (u_int)*nargs; i++) {
508 		snprintf(param, sizeof(param), "arg%u", i);
509 		name = gctl_get_asciiparam(req, param);
510 		if (name == NULL) {
511 			gctl_error(req, "No 'arg%u' argument.", i);
512 			continue;
513 		}
514 		disk = g_mirror_find_disk(sc, name);
515 		if (disk == NULL) {
516 			gctl_error(req, "No such provider: %s.", name);
517 			continue;
518 		}
519 		g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY,
520 		    G_MIRROR_EVENT_DONTWAIT);
521 	}
522 	sx_xunlock(&sc->sc_lock);
523 }
524 
525 static void
526 g_mirror_ctl_deactivate(struct gctl_req *req, struct g_class *mp)
527 {
528 	struct g_mirror_softc *sc;
529 	struct g_mirror_disk *disk;
530 	const char *name;
531 	char param[16];
532 	int *nargs;
533 	u_int i;
534 
535 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
536 	if (nargs == NULL) {
537 		gctl_error(req, "No '%s' argument.", "nargs");
538 		return;
539 	}
540 	if (*nargs < 2) {
541 		gctl_error(req, "Too few arguments.");
542 		return;
543 	}
544 	name = gctl_get_asciiparam(req, "arg0");
545 	if (name == NULL) {
546 		gctl_error(req, "No 'arg%u' argument.", 0);
547 		return;
548 	}
549 	sc = g_mirror_find_device(mp, name);
550 	if (sc == NULL) {
551 		gctl_error(req, "No such device: %s.", name);
552 		return;
553 	}
554 	for (i = 1; i < (u_int)*nargs; i++) {
555 		snprintf(param, sizeof(param), "arg%u", i);
556 		name = gctl_get_asciiparam(req, param);
557 		if (name == NULL) {
558 			gctl_error(req, "No 'arg%u' argument.", i);
559 			continue;
560 		}
561 		disk = g_mirror_find_disk(sc, name);
562 		if (disk == NULL) {
563 			gctl_error(req, "No such provider: %s.", name);
564 			continue;
565 		}
566 		disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE;
567 		disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
568 		g_mirror_update_metadata(disk);
569 		sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
570 		g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
571 		    G_MIRROR_EVENT_DONTWAIT);
572 	}
573 	sx_xunlock(&sc->sc_lock);
574 }
575 
576 static void
577 g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp)
578 {
579 	struct g_mirror_softc *sc;
580 	struct g_mirror_disk *disk;
581 	const char *name;
582 	char param[16];
583 	int *nargs;
584 	u_int i;
585 
586 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
587 	if (nargs == NULL) {
588 		gctl_error(req, "No '%s' argument.", "nargs");
589 		return;
590 	}
591 	if (*nargs < 1) {
592 		gctl_error(req, "Missing device(s).");
593 		return;
594 	}
595 
596 	for (i = 0; i < (u_int)*nargs; i++) {
597 		snprintf(param, sizeof(param), "arg%u", i);
598 		name = gctl_get_asciiparam(req, param);
599 		if (name == NULL) {
600 			gctl_error(req, "No 'arg%u' argument.", i);
601 			return;
602 		}
603 		sc = g_mirror_find_device(mp, name);
604 		if (sc == NULL) {
605 			gctl_error(req, "No such device: %s.", name);
606 			return;
607 		}
608 		if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) {
609 			sx_xunlock(&sc->sc_lock);
610 			G_MIRROR_DEBUG(1,
611 			    "All disks connected in %s, skipping.",
612 			    sc->sc_name);
613 			continue;
614 		}
615 		sc->sc_ndisks = g_mirror_ndisks(sc, -1);
616 		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
617 			g_mirror_update_metadata(disk);
618 		}
619 		sx_xunlock(&sc->sc_lock);
620 	}
621 }
622 
623 static void
624 g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp)
625 {
626 	struct g_mirror_softc *sc;
627 	int *force, *nargs, error;
628 	const char *name;
629 	char param[16];
630 	u_int i;
631 
632 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
633 	if (nargs == NULL) {
634 		gctl_error(req, "No '%s' argument.", "nargs");
635 		return;
636 	}
637 	if (*nargs < 1) {
638 		gctl_error(req, "Missing device(s).");
639 		return;
640 	}
641 	force = gctl_get_paraml(req, "force", sizeof(*force));
642 	if (force == NULL) {
643 		gctl_error(req, "No '%s' argument.", "force");
644 		return;
645 	}
646 
647 	for (i = 0; i < (u_int)*nargs; i++) {
648 		snprintf(param, sizeof(param), "arg%u", i);
649 		name = gctl_get_asciiparam(req, param);
650 		if (name == NULL) {
651 			gctl_error(req, "No 'arg%u' argument.", i);
652 			return;
653 		}
654 		sc = g_mirror_find_device(mp, name);
655 		if (sc == NULL) {
656 			gctl_error(req, "No such device: %s.", name);
657 			return;
658 		}
659 		error = g_mirror_destroy(sc, *force);
660 		if (error != 0) {
661 			gctl_error(req, "Cannot destroy device %s (error=%d).",
662 			    sc->sc_geom->name, error);
663 			sx_xunlock(&sc->sc_lock);
664 			return;
665 		}
666 		/* No need to unlock, because lock is already dead. */
667 	}
668 }
669 
670 void
671 g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
672 {
673 	uint32_t *version;
674 
675 	g_topology_assert();
676 
677 	version = gctl_get_paraml(req, "version", sizeof(*version));
678 	if (version == NULL) {
679 		gctl_error(req, "No '%s' argument.", "version");
680 		return;
681 	}
682 	if (*version != G_MIRROR_VERSION) {
683 		gctl_error(req, "Userland and kernel parts are out of sync.");
684 		return;
685 	}
686 
687 	g_topology_unlock();
688 	if (strcmp(verb, "configure") == 0)
689 		g_mirror_ctl_configure(req, mp);
690 	else if (strcmp(verb, "rebuild") == 0)
691 		g_mirror_ctl_rebuild(req, mp);
692 	else if (strcmp(verb, "insert") == 0)
693 		g_mirror_ctl_insert(req, mp);
694 	else if (strcmp(verb, "remove") == 0)
695 		g_mirror_ctl_remove(req, mp);
696 	else if (strcmp(verb, "deactivate") == 0)
697 		g_mirror_ctl_deactivate(req, mp);
698 	else if (strcmp(verb, "forget") == 0)
699 		g_mirror_ctl_forget(req, mp);
700 	else if (strcmp(verb, "stop") == 0)
701 		g_mirror_ctl_stop(req, mp);
702 	else
703 		gctl_error(req, "Unknown verb.");
704 	g_topology_lock();
705 }
706