2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS 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 THE AUTHORS 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
42 #include <geom/geom.h>
43 #include <geom/geom_dbg.h>
44 #include <geom/geom_int.h>
45 #include <geom/mirror/g_mirror.h>
47 static struct g_mirror_softc *
48 g_mirror_find_device(struct g_class *mp, const char *name)
50 struct g_mirror_softc *sc;
54 LIST_FOREACH(gp, &mp->geom, geom) {
58 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0)
60 if (strcmp(gp->name, name) == 0 ||
61 strcmp(sc->sc_name, name) == 0) {
63 sx_xlock(&sc->sc_lock);
71 static struct g_mirror_disk *
72 g_mirror_find_disk(struct g_mirror_softc *sc, const char *name)
74 struct g_mirror_disk *disk;
76 sx_assert(&sc->sc_lock, SX_XLOCKED);
77 if (strncmp(name, "/dev/", 5) == 0)
79 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
80 if (disk->d_consumer == NULL)
82 if (disk->d_consumer->provider == NULL)
84 if (strcmp(disk->d_consumer->provider->name, name) == 0)
91 g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp)
93 struct g_mirror_softc *sc;
94 struct g_mirror_disk *disk;
95 const char *name, *balancep, *prov;
96 intmax_t *slicep, *priority;
99 int *autosync, *noautosync, *failsync, *nofailsync, *hardcode, *dynamic;
100 int *nargs, do_sync = 0, dirty = 1, do_priority = 0;
102 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
104 gctl_error(req, "No '%s' argument.", "nargs");
107 if (*nargs != 1 && *nargs != 2) {
108 gctl_error(req, "Invalid number of arguments.");
111 name = gctl_get_asciiparam(req, "arg0");
113 gctl_error(req, "No 'arg%u' argument.", 0);
116 balancep = gctl_get_asciiparam(req, "balance");
117 if (balancep == NULL) {
118 gctl_error(req, "No '%s' argument.", "balance");
121 autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
122 if (autosync == NULL) {
123 gctl_error(req, "No '%s' argument.", "autosync");
126 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
127 if (noautosync == NULL) {
128 gctl_error(req, "No '%s' argument.", "noautosync");
131 failsync = gctl_get_paraml(req, "failsync", sizeof(*failsync));
132 if (failsync == NULL) {
133 gctl_error(req, "No '%s' argument.", "failsync");
136 nofailsync = gctl_get_paraml(req, "nofailsync", sizeof(*nofailsync));
137 if (nofailsync == NULL) {
138 gctl_error(req, "No '%s' argument.", "nofailsync");
141 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
142 if (hardcode == NULL) {
143 gctl_error(req, "No '%s' argument.", "hardcode");
146 dynamic = gctl_get_paraml(req, "dynamic", sizeof(*dynamic));
147 if (dynamic == NULL) {
148 gctl_error(req, "No '%s' argument.", "dynamic");
151 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
152 if (priority == NULL) {
153 gctl_error(req, "No '%s' argument.", "priority");
156 if (*priority < -1 || *priority > 255) {
157 gctl_error(req, "Priority range is 0 to 255, %jd given",
162 * Since we have a priority, we also need a provider now.
163 * Note: be WARNS safe, by always assigning prov and only throw an
164 * error if *priority != -1.
166 prov = gctl_get_asciiparam(req, "arg1");
167 if (*priority > -1) {
169 gctl_error(req, "Priority needs a disk name");
174 if (*autosync && *noautosync) {
175 gctl_error(req, "'%s' and '%s' specified.", "autosync",
179 if (*failsync && *nofailsync) {
180 gctl_error(req, "'%s' and '%s' specified.", "failsync",
184 if (*hardcode && *dynamic) {
185 gctl_error(req, "'%s' and '%s' specified.", "hardcode",
189 sc = g_mirror_find_device(mp, name);
191 gctl_error(req, "No such device: %s.", name);
194 if (*balancep == '\0')
195 balance = sc->sc_balance;
197 if (balance_id(balancep) == -1) {
198 gctl_error(req, "Invalid balance algorithm.");
199 sx_xunlock(&sc->sc_lock);
202 balance = balance_id(balancep);
204 slicep = gctl_get_paraml(req, "slice", sizeof(*slicep));
205 if (slicep == NULL) {
206 gctl_error(req, "No '%s' argument.", "slice");
207 sx_xunlock(&sc->sc_lock);
211 slice = sc->sc_slice;
214 /* Enforce usage() of -p not allowing any other options. */
215 if (do_priority && (*autosync || *noautosync || *failsync ||
216 *nofailsync || *hardcode || *dynamic || *slicep != -1 ||
217 *balancep != '\0')) {
218 sx_xunlock(&sc->sc_lock);
219 gctl_error(req, "only -p accepted when setting priority");
222 if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync &&
223 !*noautosync && !*failsync && !*nofailsync && !*hardcode &&
224 !*dynamic && !do_priority) {
225 sx_xunlock(&sc->sc_lock);
226 gctl_error(req, "Nothing has changed.");
229 if ((!do_priority && *nargs != 1) || (do_priority && *nargs != 2)) {
230 sx_xunlock(&sc->sc_lock);
231 gctl_error(req, "Invalid number of arguments.");
234 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
235 sx_xunlock(&sc->sc_lock);
236 gctl_error(req, "Not all disks connected. Try 'forget' command "
240 sc->sc_balance = balance;
241 sc->sc_slice = slice;
242 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) {
244 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
249 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
251 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0) {
253 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
256 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
260 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
262 * Handle priority first, since we only need one disk, do one
263 * operation on it and then we're done. No need to check other
264 * flags, as usage doesn't allow it.
267 if (strcmp(disk->d_name, prov) == 0) {
268 if (disk->d_priority == *priority)
269 gctl_error(req, "Nothing has changed.");
271 disk->d_priority = *priority;
272 g_mirror_update_metadata(disk);
279 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
280 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
283 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
285 disk->d_flags &= ~G_MIRROR_DISK_FLAG_HARDCODED;
287 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
288 g_mirror_update_metadata(disk);
290 if (disk->d_state == G_MIRROR_DISK_STATE_STALE) {
291 g_mirror_event_send(disk,
292 G_MIRROR_DISK_STATE_DISCONNECTED,
293 G_MIRROR_EVENT_DONTWAIT);
297 sx_xunlock(&sc->sc_lock);
301 g_mirror_create_orphan(struct g_consumer *cp)
304 KASSERT(1 == 0, ("%s called while creating %s.", __func__,
305 cp->provider->name));
309 g_mirror_ctl_create(struct gctl_req *req, struct g_class *mp)
311 struct g_mirror_metadata md;
313 struct g_consumer *cp;
314 struct g_provider *pp;
315 struct g_mirror_softc *sc;
324 unsigned attached, no, sectorsize;
327 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
329 gctl_error(req, "No '%s' argument.", "nargs");
333 gctl_error(req, "Too few arguments.");
337 strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
338 md.md_version = G_MIRROR_VERSION;
339 name = gctl_get_asciiparam(req, "arg0");
341 gctl_error(req, "No 'arg%u' argument.", 0);
344 strlcpy(md.md_name, name, sizeof(md.md_name));
345 md.md_mid = arc4random();
346 md.md_all = *nargs - 1;
349 md.md_sync_offset = 0;
350 val = gctl_get_paraml(req, "slice", sizeof(*val));
352 gctl_error(req, "No slice argument.");
356 sval = gctl_get_asciiparam(req, "balance");
358 gctl_error(req, "No balance argument.");
361 bal = balance_id(sval);
363 gctl_error(req, "Invalid balance algorithm.");
369 ival = gctl_get_paraml(req, "noautosync", sizeof(*ival));
370 if (ival != NULL && *ival)
371 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
372 ival = gctl_get_paraml(req, "nofailsync", sizeof(*ival));
373 if (ival != NULL && *ival)
374 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
375 /* These fields not used in manual mode. */
376 bzero(md.md_provider, sizeof(md.md_provider));
382 gp = g_new_geomf(mp, "%s", md.md_name);
383 gp->orphan = g_mirror_create_orphan;
384 cp = g_new_consumer(gp);
385 for (no = 1; no < *nargs; no++) {
386 snprintf(param, sizeof(param), "arg%u", no);
387 name = gctl_get_asciiparam(req, param);
389 gctl_error(req, "No 'arg%u' argument.", no);
391 g_destroy_consumer(cp);
396 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
397 name += strlen("/dev/");
398 pp = g_provider_by_name(name);
400 G_MIRROR_DEBUG(1, "Disk %s is invalid.", name);
401 gctl_error(req, "Disk %s is invalid.", name);
405 if (g_access(cp, 1, 0, 0) != 0) {
406 G_MIRROR_DEBUG(1, "Can't open disk %s.", name);
407 gctl_error(req, "Can't open disk %s.", name);
412 if (pp->mediasize == 0 || pp->sectorsize == 0) {
413 G_MIRROR_DEBUG(1, "Disk %s has no media.", name);
414 gctl_error(req, "Disk %s has no media.", name);
415 g_access(cp, -1, 0, 0);
418 if (pp->mediasize < mediasize)
419 mediasize = pp->mediasize;
420 if (pp->sectorsize > sectorsize)
421 sectorsize = pp->sectorsize;
422 g_access(cp, -1, 0, 0);
425 g_destroy_consumer(cp);
427 md.md_mediasize = mediasize;
428 md.md_sectorsize = sectorsize;
429 md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
431 gp = g_mirror_create(mp, &md, G_MIRROR_TYPE_MANUAL);
433 gctl_error(req, "Can't create %s.", md.md_name);
440 sx_xlock(&sc->sc_lock);
441 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
442 sb = sbuf_new_auto();
443 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
444 for (attached = 0, no = 1; no < *nargs; no++) {
445 snprintf(param, sizeof(param), "arg%u", no);
446 name = gctl_get_asciiparam(req, param);
447 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
448 name += strlen("/dev/");
449 pp = g_provider_by_name(name);
451 G_MIRROR_DEBUG(1, "Provider %s disappear?!", name);
452 sbuf_printf(sb, " %s", name);
455 md.md_did = arc4random();
456 md.md_priority = no - 1;
457 if (g_mirror_add_disk(sc, pp, &md) != 0) {
458 G_MIRROR_DEBUG(1, "Disk %u (%s) not attached to %s.",
459 no, pp->name, gp->name);
460 sbuf_printf(sb, " %s", pp->name);
466 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
467 if (md.md_all != attached ||
468 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
469 g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_HARD);
470 gctl_error(req, "%s", sbuf_data(sb));
472 sx_xunlock(&sc->sc_lock);
477 g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
479 struct g_mirror_metadata md;
480 struct g_mirror_softc *sc;
481 struct g_mirror_disk *disk;
482 struct g_provider *pp;
488 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
490 gctl_error(req, "No '%s' argument.", "nargs");
494 gctl_error(req, "Too few arguments.");
497 name = gctl_get_asciiparam(req, "arg0");
499 gctl_error(req, "No 'arg%u' argument.", 0);
502 sc = g_mirror_find_device(mp, name);
504 gctl_error(req, "No such device: %s.", name);
507 for (i = 1; i < (u_int)*nargs; i++) {
508 snprintf(param, sizeof(param), "arg%u", i);
509 name = gctl_get_asciiparam(req, param);
511 gctl_error(req, "No 'arg%u' argument.", i);
514 disk = g_mirror_find_disk(sc, name);
516 gctl_error(req, "No such provider: %s.", name);
519 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
520 disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
522 * This is the last active disk. There will be nothing
523 * to rebuild it from, so deny this request.
526 "Provider %s is the last active provider in %s.",
527 name, sc->sc_geom->name);
531 * Do rebuild by resetting syncid, disconnecting the disk and
532 * connecting it again.
534 disk->d_sync.ds_syncid = 0;
535 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
536 disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC;
537 g_mirror_update_metadata(disk);
538 pp = disk->d_consumer->provider;
540 error = g_mirror_read_metadata(disk->d_consumer, &md);
542 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
543 G_MIRROR_EVENT_WAIT);
545 gctl_error(req, "Cannot read metadata from %s.",
549 error = g_mirror_add_disk(sc, pp, &md);
551 gctl_error(req, "Cannot reconnect component %s.",
556 sx_xunlock(&sc->sc_lock);
560 g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
562 struct g_mirror_softc *sc;
563 struct g_mirror_disk *disk;
564 struct g_mirror_metadata md;
565 struct g_provider *pp;
566 struct g_consumer *cp;
572 int error, *nargs, *hardcode, *inactive;
574 struct g_provider *provider;
575 struct g_consumer *consumer;
579 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
581 gctl_error(req, "No '%s' argument.", "nargs");
585 gctl_error(req, "Too few arguments.");
588 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
589 if (priority == NULL) {
590 gctl_error(req, "No '%s' argument.", "priority");
593 inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive));
594 if (inactive == NULL) {
595 gctl_error(req, "No '%s' argument.", "inactive");
598 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
599 if (hardcode == NULL) {
600 gctl_error(req, "No '%s' argument.", "hardcode");
603 name = gctl_get_asciiparam(req, "arg0");
605 gctl_error(req, "No 'arg%u' argument.", 0);
608 sc = g_mirror_find_device(mp, name);
610 gctl_error(req, "No such device: %s.", name);
613 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
614 gctl_error(req, "Not all disks connected.");
615 sx_xunlock(&sc->sc_lock);
619 disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO);
621 for (i = 1, n = 0; i < (u_int)*nargs; i++) {
622 snprintf(param, sizeof(param), "arg%u", i);
623 name = gctl_get_asciiparam(req, param);
625 gctl_error(req, "No 'arg%u' argument.", i);
628 if (g_mirror_find_disk(sc, name) != NULL) {
629 gctl_error(req, "Provider %s already inserted.", name);
632 if (strncmp(name, "/dev/", 5) == 0)
634 pp = g_provider_by_name(name);
636 gctl_error(req, "Unknown provider %s.", name);
639 cp = g_new_consumer(sc->sc_geom);
640 if (g_attach(cp, pp) != 0) {
641 g_destroy_consumer(cp);
642 gctl_error(req, "Cannot attach to provider %s.", name);
645 if (g_access(cp, 0, 1, 1) != 0) {
646 gctl_error(req, "Cannot access provider %s.", name);
649 g_destroy_consumer(cp);
652 mdsize = (sc->sc_type == G_MIRROR_TYPE_AUTOMATIC) ?
654 if (sc->sc_provider->mediasize > pp->mediasize - mdsize) {
655 gctl_error(req, "Provider %s too small.", name);
657 g_access(cp, 0, -1, -1);
660 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
661 gctl_error(req, "Invalid sectorsize of provider %s.",
665 if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC) {
666 g_access(cp, 0, -1, -1);
668 g_destroy_consumer(cp);
671 g_mirror_fill_metadata(sc, NULL, &md);
672 md.md_priority = *priority;
674 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
675 if (g_mirror_add_disk(sc, pp, &md) != 0) {
677 gctl_error(req, "Disk %s not inserted.", name);
682 disks[n].provider = pp;
683 disks[n].consumer = cp;
688 sx_xunlock(&sc->sc_lock);
694 for (i = 0; i < n; i++) {
695 if (disks[i].consumer == NULL)
697 g_mirror_fill_metadata(sc, NULL, &md);
698 md.md_priority = *priority;
700 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
701 pp = disks[i].provider;
703 strlcpy(md.md_provider, pp->name,
704 sizeof(md.md_provider));
706 bzero(md.md_provider, sizeof(md.md_provider));
708 md.md_provsize = pp->mediasize;
709 sector = g_malloc(pp->sectorsize, M_WAITOK);
710 mirror_metadata_encode(&md, sector);
711 error = g_write_data(disks[i].consumer,
712 pp->mediasize - pp->sectorsize, sector, pp->sectorsize);
715 gctl_error(req, "Cannot store metadata on %s.",
717 g_access(disks[i].consumer, 0, -1, -1);
718 g_detach(disks[i].consumer);
719 g_destroy_consumer(disks[i].consumer);
720 disks[i].consumer = NULL;
721 disks[i].provider = NULL;
728 /* All writes failed. */
729 sx_xunlock(&sc->sc_lock);
733 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
734 g_mirror_update_metadata(disk);
737 * Release provider and wait for retaste.
740 for (i = 0; i < n; i++) {
741 if (disks[i].consumer == NULL)
743 g_access(disks[i].consumer, 0, -1, -1);
744 g_detach(disks[i].consumer);
745 g_destroy_consumer(disks[i].consumer);
748 sx_xunlock(&sc->sc_lock);
753 g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp)
755 struct g_mirror_softc *sc;
756 struct g_mirror_disk *disk;
762 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
764 gctl_error(req, "No '%s' argument.", "nargs");
768 gctl_error(req, "Too few arguments.");
771 name = gctl_get_asciiparam(req, "arg0");
773 gctl_error(req, "No 'arg%u' argument.", 0);
776 sc = g_mirror_find_device(mp, name);
778 gctl_error(req, "No such device: %s.", name);
781 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
782 sx_xunlock(&sc->sc_lock);
783 gctl_error(req, "Not all disks connected. Try 'forget' command "
787 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
788 for (i = 1; i < (u_int)*nargs; i++) {
789 snprintf(param, sizeof(param), "arg%u", i);
790 name = gctl_get_asciiparam(req, param);
792 gctl_error(req, "No 'arg%u' argument.", i);
795 disk = g_mirror_find_disk(sc, name);
797 gctl_error(req, "No such provider: %s.", name);
800 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
804 gctl_error(req, "%s: Can't remove the last "
805 "ACTIVE component %s.", sc->sc_geom->name,
810 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY,
811 G_MIRROR_EVENT_DONTWAIT);
813 sx_xunlock(&sc->sc_lock);
817 g_mirror_ctl_resize(struct gctl_req *req, struct g_class *mp)
819 struct g_mirror_softc *sc;
820 struct g_mirror_disk *disk;
822 const char *name, *s;
826 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
828 gctl_error(req, "No '%s' argument.", "nargs");
832 gctl_error(req, "Missing device.");
835 name = gctl_get_asciiparam(req, "arg0");
837 gctl_error(req, "No 'arg%u' argument.", 0);
840 s = gctl_get_asciiparam(req, "size");
842 gctl_error(req, "No '%s' argument.", "size");
845 mediasize = strtouq(s, &x, 0);
846 if (*x != '\0' || mediasize == 0) {
847 gctl_error(req, "Invalid '%s' argument.", "size");
850 sc = g_mirror_find_device(mp, name);
852 gctl_error(req, "No such device: %s.", name);
855 /* Deny shrinking of an opened provider */
856 if ((g_debugflags & G_F_FOOTSHOOTING) == 0 && sc->sc_provider_open > 0) {
857 if (sc->sc_mediasize > mediasize) {
858 gctl_error(req, "Device %s is busy.",
859 sc->sc_provider->name);
860 sx_xunlock(&sc->sc_lock);
864 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
865 if (mediasize > disk->d_consumer->provider->mediasize -
866 disk->d_consumer->provider->sectorsize) {
867 gctl_error(req, "Provider %s is too small.",
869 sx_xunlock(&sc->sc_lock);
873 /* Update the size. */
874 sc->sc_mediasize = mediasize;
875 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
876 g_mirror_update_metadata(disk);
879 g_resize_provider(sc->sc_provider, mediasize);
881 sx_xunlock(&sc->sc_lock);
885 g_mirror_ctl_deactivate(struct gctl_req *req, struct g_class *mp)
887 struct g_mirror_softc *sc;
888 struct g_mirror_disk *disk;
894 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
896 gctl_error(req, "No '%s' argument.", "nargs");
900 gctl_error(req, "Too few arguments.");
903 name = gctl_get_asciiparam(req, "arg0");
905 gctl_error(req, "No 'arg%u' argument.", 0);
908 sc = g_mirror_find_device(mp, name);
910 gctl_error(req, "No such device: %s.", name);
913 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
914 for (i = 1; i < (u_int)*nargs; i++) {
915 snprintf(param, sizeof(param), "arg%u", i);
916 name = gctl_get_asciiparam(req, param);
918 gctl_error(req, "No 'arg%u' argument.", i);
921 disk = g_mirror_find_disk(sc, name);
923 gctl_error(req, "No such provider: %s.", name);
926 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
930 gctl_error(req, "%s: Can't deactivate the "
931 "last ACTIVE component %s.",
932 sc->sc_geom->name, name);
936 disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE;
937 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
938 g_mirror_update_metadata(disk);
939 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
940 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
941 G_MIRROR_EVENT_DONTWAIT);
943 sx_xunlock(&sc->sc_lock);
947 g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp)
949 struct g_mirror_softc *sc;
950 struct g_mirror_disk *disk;
956 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
958 gctl_error(req, "No '%s' argument.", "nargs");
962 gctl_error(req, "Missing device(s).");
966 for (i = 0; i < (u_int)*nargs; i++) {
967 snprintf(param, sizeof(param), "arg%u", i);
968 name = gctl_get_asciiparam(req, param);
970 gctl_error(req, "No 'arg%u' argument.", i);
973 sc = g_mirror_find_device(mp, name);
975 gctl_error(req, "No such device: %s.", name);
978 if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) {
979 sx_xunlock(&sc->sc_lock);
981 "All disks connected in %s, skipping.",
985 sc->sc_ndisks = g_mirror_ndisks(sc, -1);
986 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
987 g_mirror_update_metadata(disk);
989 sx_xunlock(&sc->sc_lock);
994 g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp, int wipe)
996 struct g_mirror_softc *sc;
997 int *force, *nargs, error;
1003 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1004 if (nargs == NULL) {
1005 gctl_error(req, "No '%s' argument.", "nargs");
1009 gctl_error(req, "Missing device(s).");
1012 force = gctl_get_paraml(req, "force", sizeof(*force));
1013 if (force == NULL) {
1014 gctl_error(req, "No '%s' argument.", "force");
1018 how = G_MIRROR_DESTROY_HARD;
1020 how = G_MIRROR_DESTROY_SOFT;
1022 for (i = 0; i < (u_int)*nargs; i++) {
1023 snprintf(param, sizeof(param), "arg%u", i);
1024 name = gctl_get_asciiparam(req, param);
1026 gctl_error(req, "No 'arg%u' argument.", i);
1029 sc = g_mirror_find_device(mp, name);
1031 gctl_error(req, "No such device: %s.", name);
1036 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WIPE;
1037 error = g_mirror_destroy(sc, how);
1039 gctl_error(req, "Cannot destroy device %s (error=%d).",
1040 sc->sc_geom->name, error);
1042 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_WIPE;
1043 sx_xunlock(&sc->sc_lock);
1046 /* No need to unlock, because lock is already dead. */
1051 g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1055 g_topology_assert();
1057 version = gctl_get_paraml(req, "version", sizeof(*version));
1058 if (version == NULL) {
1059 gctl_error(req, "No '%s' argument.", "version");
1062 if (*version != G_MIRROR_VERSION) {
1063 gctl_error(req, "Userland and kernel parts are out of sync.");
1067 g_topology_unlock();
1068 if (strcmp(verb, "configure") == 0)
1069 g_mirror_ctl_configure(req, mp);
1070 else if (strcmp(verb, "create") == 0)
1071 g_mirror_ctl_create(req, mp);
1072 else if (strcmp(verb, "rebuild") == 0)
1073 g_mirror_ctl_rebuild(req, mp);
1074 else if (strcmp(verb, "insert") == 0)
1075 g_mirror_ctl_insert(req, mp);
1076 else if (strcmp(verb, "remove") == 0)
1077 g_mirror_ctl_remove(req, mp);
1078 else if (strcmp(verb, "resize") == 0)
1079 g_mirror_ctl_resize(req, mp);
1080 else if (strcmp(verb, "deactivate") == 0)
1081 g_mirror_ctl_deactivate(req, mp);
1082 else if (strcmp(verb, "forget") == 0)
1083 g_mirror_ctl_forget(req, mp);
1084 else if (strcmp(verb, "stop") == 0)
1085 g_mirror_ctl_stop(req, mp, 0);
1086 else if (strcmp(verb, "destroy") == 0)
1087 g_mirror_ctl_stop(req, mp, 1);
1089 gctl_error(req, "Unknown verb.");