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_int.h>
44 #include <geom/mirror/g_mirror.h>
46 static struct g_mirror_softc *
47 g_mirror_find_device(struct g_class *mp, const char *name)
49 struct g_mirror_softc *sc;
53 LIST_FOREACH(gp, &mp->geom, geom) {
57 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0)
59 if (strcmp(gp->name, name) == 0 ||
60 strcmp(sc->sc_name, name) == 0) {
62 sx_xlock(&sc->sc_lock);
70 static struct g_mirror_disk *
71 g_mirror_find_disk(struct g_mirror_softc *sc, const char *name)
73 struct g_mirror_disk *disk;
75 sx_assert(&sc->sc_lock, SX_XLOCKED);
76 if (strncmp(name, "/dev/", 5) == 0)
78 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
79 if (disk->d_consumer == NULL)
81 if (disk->d_consumer->provider == NULL)
83 if (strcmp(disk->d_consumer->provider->name, name) == 0)
90 g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp)
92 struct g_mirror_softc *sc;
93 struct g_mirror_disk *disk;
94 const char *name, *balancep, *prov;
95 intmax_t *slicep, *priority;
98 int *autosync, *noautosync, *failsync, *nofailsync, *hardcode, *dynamic;
99 int *nargs, do_sync = 0, dirty = 1, do_priority = 0;
101 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
103 gctl_error(req, "No '%s' argument.", "nargs");
106 if (*nargs != 1 && *nargs != 2) {
107 gctl_error(req, "Invalid number of arguments.");
110 name = gctl_get_asciiparam(req, "arg0");
112 gctl_error(req, "No 'arg%u' argument.", 0);
115 balancep = gctl_get_asciiparam(req, "balance");
116 if (balancep == NULL) {
117 gctl_error(req, "No '%s' argument.", "balance");
120 autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
121 if (autosync == NULL) {
122 gctl_error(req, "No '%s' argument.", "autosync");
125 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
126 if (noautosync == NULL) {
127 gctl_error(req, "No '%s' argument.", "noautosync");
130 failsync = gctl_get_paraml(req, "failsync", sizeof(*failsync));
131 if (failsync == NULL) {
132 gctl_error(req, "No '%s' argument.", "failsync");
135 nofailsync = gctl_get_paraml(req, "nofailsync", sizeof(*nofailsync));
136 if (nofailsync == NULL) {
137 gctl_error(req, "No '%s' argument.", "nofailsync");
140 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
141 if (hardcode == NULL) {
142 gctl_error(req, "No '%s' argument.", "hardcode");
145 dynamic = gctl_get_paraml(req, "dynamic", sizeof(*dynamic));
146 if (dynamic == NULL) {
147 gctl_error(req, "No '%s' argument.", "dynamic");
150 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
151 if (priority == NULL) {
152 gctl_error(req, "No '%s' argument.", "priority");
155 if (*priority < -1 || *priority > 255) {
156 gctl_error(req, "Priority range is 0 to 255, %jd given",
161 * Since we have a priority, we also need a provider now.
162 * Note: be WARNS safe, by always assigning prov and only throw an
163 * error if *priority != -1.
165 prov = gctl_get_asciiparam(req, "arg1");
166 if (*priority > -1) {
168 gctl_error(req, "Priority needs a disk name");
173 if (*autosync && *noautosync) {
174 gctl_error(req, "'%s' and '%s' specified.", "autosync",
178 if (*failsync && *nofailsync) {
179 gctl_error(req, "'%s' and '%s' specified.", "failsync",
183 if (*hardcode && *dynamic) {
184 gctl_error(req, "'%s' and '%s' specified.", "hardcode",
188 sc = g_mirror_find_device(mp, name);
190 gctl_error(req, "No such device: %s.", name);
193 if (*balancep == '\0')
194 balance = sc->sc_balance;
196 if (balance_id(balancep) == -1) {
197 gctl_error(req, "Invalid balance algorithm.");
198 sx_xunlock(&sc->sc_lock);
201 balance = balance_id(balancep);
203 slicep = gctl_get_paraml(req, "slice", sizeof(*slicep));
204 if (slicep == NULL) {
205 gctl_error(req, "No '%s' argument.", "slice");
206 sx_xunlock(&sc->sc_lock);
210 slice = sc->sc_slice;
213 /* Enforce usage() of -p not allowing any other options. */
214 if (do_priority && (*autosync || *noautosync || *failsync ||
215 *nofailsync || *hardcode || *dynamic || *slicep != -1 ||
216 *balancep != '\0')) {
217 sx_xunlock(&sc->sc_lock);
218 gctl_error(req, "only -p accepted when setting priority");
221 if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync &&
222 !*noautosync && !*failsync && !*nofailsync && !*hardcode &&
223 !*dynamic && !do_priority) {
224 sx_xunlock(&sc->sc_lock);
225 gctl_error(req, "Nothing has changed.");
228 if ((!do_priority && *nargs != 1) || (do_priority && *nargs != 2)) {
229 sx_xunlock(&sc->sc_lock);
230 gctl_error(req, "Invalid number of arguments.");
233 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
234 sx_xunlock(&sc->sc_lock);
235 gctl_error(req, "Not all disks connected. Try 'forget' command "
239 sc->sc_balance = balance;
240 sc->sc_slice = slice;
241 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) {
243 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
248 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
250 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0) {
252 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
255 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
259 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
261 * Handle priority first, since we only need one disk, do one
262 * operation on it and then we're done. No need to check other
263 * flags, as usage doesn't allow it.
266 if (strcmp(disk->d_name, prov) == 0) {
267 if (disk->d_priority == *priority)
268 gctl_error(req, "Nothing has changed.");
270 disk->d_priority = *priority;
271 g_mirror_update_metadata(disk);
278 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
279 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
282 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
284 disk->d_flags &= ~G_MIRROR_DISK_FLAG_HARDCODED;
286 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
287 g_mirror_update_metadata(disk);
289 if (disk->d_state == G_MIRROR_DISK_STATE_STALE) {
290 g_mirror_event_send(disk,
291 G_MIRROR_DISK_STATE_DISCONNECTED,
292 G_MIRROR_EVENT_DONTWAIT);
296 sx_xunlock(&sc->sc_lock);
300 g_mirror_create_orphan(struct g_consumer *cp)
303 KASSERT(1 == 0, ("%s called while creating %s.", __func__,
304 cp->provider->name));
308 g_mirror_ctl_create(struct gctl_req *req, struct g_class *mp)
310 struct g_mirror_metadata md;
312 struct g_consumer *cp;
313 struct g_provider *pp;
314 struct g_mirror_softc *sc;
323 unsigned attached, no, sectorsize;
326 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
328 gctl_error(req, "No '%s' argument.", "nargs");
332 gctl_error(req, "Too few arguments.");
336 strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
337 md.md_version = G_MIRROR_VERSION;
338 name = gctl_get_asciiparam(req, "arg0");
340 gctl_error(req, "No 'arg%u' argument.", 0);
343 strlcpy(md.md_name, name, sizeof(md.md_name));
344 md.md_mid = arc4random();
345 md.md_all = *nargs - 1;
348 md.md_sync_offset = 0;
349 val = gctl_get_paraml(req, "slice", sizeof(*val));
351 gctl_error(req, "No slice argument.");
355 sval = gctl_get_asciiparam(req, "balance");
357 gctl_error(req, "No balance argument.");
360 bal = balance_id(sval);
362 gctl_error(req, "Invalid balance algorithm.");
368 ival = gctl_get_paraml(req, "noautosync", sizeof(*ival));
369 if (ival != NULL && *ival)
370 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
371 ival = gctl_get_paraml(req, "nofailsync", sizeof(*ival));
372 if (ival != NULL && *ival)
373 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
374 /* These fields not used in manual mode. */
375 bzero(md.md_provider, sizeof(md.md_provider));
381 gp = g_new_geomf(mp, "%s", md.md_name);
382 gp->orphan = g_mirror_create_orphan;
383 cp = g_new_consumer(gp);
384 for (no = 1; no < *nargs; no++) {
385 snprintf(param, sizeof(param), "arg%u", no);
386 name = gctl_get_asciiparam(req, param);
388 gctl_error(req, "No 'arg%u' argument.", no);
390 g_destroy_consumer(cp);
395 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
396 name += strlen("/dev/");
397 pp = g_provider_by_name(name);
399 G_MIRROR_DEBUG(1, "Disk %s is invalid.", name);
400 gctl_error(req, "Disk %s is invalid.", name);
404 if (g_access(cp, 1, 0, 0) != 0) {
405 G_MIRROR_DEBUG(1, "Can't open disk %s.", name);
406 gctl_error(req, "Can't open disk %s.", name);
411 if (pp->mediasize == 0 || pp->sectorsize == 0) {
412 G_MIRROR_DEBUG(1, "Disk %s has no media.", name);
413 gctl_error(req, "Disk %s has no media.", name);
414 g_access(cp, -1, 0, 0);
417 if (pp->mediasize < mediasize)
418 mediasize = pp->mediasize;
419 if (pp->sectorsize > sectorsize)
420 sectorsize = pp->sectorsize;
421 g_access(cp, -1, 0, 0);
424 g_destroy_consumer(cp);
426 md.md_mediasize = mediasize;
427 md.md_sectorsize = sectorsize;
428 md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
430 gp = g_mirror_create(mp, &md, G_MIRROR_TYPE_MANUAL);
432 gctl_error(req, "Can't create %s.", md.md_name);
439 sx_xlock(&sc->sc_lock);
440 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
441 sb = sbuf_new_auto();
442 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
443 for (attached = 0, no = 1; no < *nargs; no++) {
444 snprintf(param, sizeof(param), "arg%u", no);
445 name = gctl_get_asciiparam(req, param);
446 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
447 name += strlen("/dev/");
448 pp = g_provider_by_name(name);
450 G_MIRROR_DEBUG(1, "Provider %s disappear?!", name);
451 sbuf_printf(sb, " %s", name);
454 md.md_did = arc4random();
455 md.md_priority = no - 1;
456 if (g_mirror_add_disk(sc, pp, &md) != 0) {
457 G_MIRROR_DEBUG(1, "Disk %u (%s) not attached to %s.",
458 no, pp->name, gp->name);
459 sbuf_printf(sb, " %s", pp->name);
465 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
466 if (md.md_all != attached ||
467 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
468 g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_HARD);
469 gctl_error(req, "%s", sbuf_data(sb));
471 sx_xunlock(&sc->sc_lock);
476 g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
478 struct g_mirror_metadata md;
479 struct g_mirror_softc *sc;
480 struct g_mirror_disk *disk;
481 struct g_provider *pp;
487 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
489 gctl_error(req, "No '%s' argument.", "nargs");
493 gctl_error(req, "Too few arguments.");
496 name = gctl_get_asciiparam(req, "arg0");
498 gctl_error(req, "No 'arg%u' argument.", 0);
501 sc = g_mirror_find_device(mp, name);
503 gctl_error(req, "No such device: %s.", name);
506 for (i = 1; i < (u_int)*nargs; i++) {
507 snprintf(param, sizeof(param), "arg%u", i);
508 name = gctl_get_asciiparam(req, param);
510 gctl_error(req, "No 'arg%u' argument.", i);
513 disk = g_mirror_find_disk(sc, name);
515 gctl_error(req, "No such provider: %s.", name);
518 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
519 disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
521 * This is the last active disk. There will be nothing
522 * to rebuild it from, so deny this request.
525 "Provider %s is the last active provider in %s.",
526 name, sc->sc_geom->name);
530 * Do rebuild by resetting syncid, disconnecting the disk and
531 * connecting it again.
533 disk->d_sync.ds_syncid = 0;
534 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
535 disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC;
536 g_mirror_update_metadata(disk);
537 pp = disk->d_consumer->provider;
539 error = g_mirror_read_metadata(disk->d_consumer, &md);
541 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
542 G_MIRROR_EVENT_WAIT);
544 gctl_error(req, "Cannot read metadata from %s.",
548 error = g_mirror_add_disk(sc, pp, &md);
550 gctl_error(req, "Cannot reconnect component %s.",
555 sx_xunlock(&sc->sc_lock);
559 g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
561 struct g_mirror_softc *sc;
562 struct g_mirror_disk *disk;
563 struct g_mirror_metadata md;
564 struct g_provider *pp;
565 struct g_consumer *cp;
571 int error, *nargs, *hardcode, *inactive;
573 struct g_provider *provider;
574 struct g_consumer *consumer;
578 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
580 gctl_error(req, "No '%s' argument.", "nargs");
584 gctl_error(req, "Too few arguments.");
587 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
588 if (priority == NULL) {
589 gctl_error(req, "No '%s' argument.", "priority");
592 inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive));
593 if (inactive == NULL) {
594 gctl_error(req, "No '%s' argument.", "inactive");
597 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
598 if (hardcode == NULL) {
599 gctl_error(req, "No '%s' argument.", "hardcode");
602 name = gctl_get_asciiparam(req, "arg0");
604 gctl_error(req, "No 'arg%u' argument.", 0);
607 sc = g_mirror_find_device(mp, name);
609 gctl_error(req, "No such device: %s.", name);
612 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
613 gctl_error(req, "Not all disks connected.");
614 sx_xunlock(&sc->sc_lock);
618 disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO);
620 for (i = 1, n = 0; i < (u_int)*nargs; i++) {
621 snprintf(param, sizeof(param), "arg%u", i);
622 name = gctl_get_asciiparam(req, param);
624 gctl_error(req, "No 'arg%u' argument.", i);
627 if (g_mirror_find_disk(sc, name) != NULL) {
628 gctl_error(req, "Provider %s already inserted.", name);
631 if (strncmp(name, "/dev/", 5) == 0)
633 pp = g_provider_by_name(name);
635 gctl_error(req, "Unknown provider %s.", name);
638 cp = g_new_consumer(sc->sc_geom);
639 if (g_attach(cp, pp) != 0) {
640 g_destroy_consumer(cp);
641 gctl_error(req, "Cannot attach to provider %s.", name);
644 if (g_access(cp, 0, 1, 1) != 0) {
645 gctl_error(req, "Cannot access provider %s.", name);
648 g_destroy_consumer(cp);
651 mdsize = (sc->sc_type == G_MIRROR_TYPE_AUTOMATIC) ?
653 if (sc->sc_provider->mediasize > pp->mediasize - mdsize) {
654 gctl_error(req, "Provider %s too small.", name);
656 g_access(cp, 0, -1, -1);
659 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
660 gctl_error(req, "Invalid sectorsize of provider %s.",
664 if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC) {
665 g_access(cp, 0, -1, -1);
667 g_destroy_consumer(cp);
670 g_mirror_fill_metadata(sc, NULL, &md);
671 md.md_priority = *priority;
673 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
674 if (g_mirror_add_disk(sc, pp, &md) != 0) {
676 gctl_error(req, "Disk %s not inserted.", name);
681 disks[n].provider = pp;
682 disks[n].consumer = cp;
687 sx_xunlock(&sc->sc_lock);
693 for (i = 0; i < n; i++) {
694 if (disks[i].consumer == NULL)
696 g_mirror_fill_metadata(sc, NULL, &md);
697 md.md_priority = *priority;
699 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
700 pp = disks[i].provider;
702 strlcpy(md.md_provider, pp->name,
703 sizeof(md.md_provider));
705 bzero(md.md_provider, sizeof(md.md_provider));
707 md.md_provsize = pp->mediasize;
708 sector = g_malloc(pp->sectorsize, M_WAITOK);
709 mirror_metadata_encode(&md, sector);
710 error = g_write_data(disks[i].consumer,
711 pp->mediasize - pp->sectorsize, sector, pp->sectorsize);
714 gctl_error(req, "Cannot store metadata on %s.",
716 g_access(disks[i].consumer, 0, -1, -1);
717 g_detach(disks[i].consumer);
718 g_destroy_consumer(disks[i].consumer);
719 disks[i].consumer = NULL;
720 disks[i].provider = NULL;
727 /* All writes failed. */
728 sx_xunlock(&sc->sc_lock);
732 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
733 g_mirror_update_metadata(disk);
736 * Release provider and wait for retaste.
739 for (i = 0; i < n; i++) {
740 if (disks[i].consumer == NULL)
742 g_access(disks[i].consumer, 0, -1, -1);
743 g_detach(disks[i].consumer);
744 g_destroy_consumer(disks[i].consumer);
747 sx_xunlock(&sc->sc_lock);
752 g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp)
754 struct g_mirror_softc *sc;
755 struct g_mirror_disk *disk;
761 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
763 gctl_error(req, "No '%s' argument.", "nargs");
767 gctl_error(req, "Too few arguments.");
770 name = gctl_get_asciiparam(req, "arg0");
772 gctl_error(req, "No 'arg%u' argument.", 0);
775 sc = g_mirror_find_device(mp, name);
777 gctl_error(req, "No such device: %s.", name);
780 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
781 sx_xunlock(&sc->sc_lock);
782 gctl_error(req, "Not all disks connected. Try 'forget' command "
786 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
787 for (i = 1; i < (u_int)*nargs; i++) {
788 snprintf(param, sizeof(param), "arg%u", i);
789 name = gctl_get_asciiparam(req, param);
791 gctl_error(req, "No 'arg%u' argument.", i);
794 disk = g_mirror_find_disk(sc, name);
796 gctl_error(req, "No such provider: %s.", name);
799 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
803 gctl_error(req, "%s: Can't remove the last "
804 "ACTIVE component %s.", sc->sc_geom->name,
809 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY,
810 G_MIRROR_EVENT_DONTWAIT);
812 sx_xunlock(&sc->sc_lock);
816 g_mirror_ctl_resize(struct gctl_req *req, struct g_class *mp)
818 struct g_mirror_softc *sc;
819 struct g_mirror_disk *disk;
821 const char *name, *s;
825 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
827 gctl_error(req, "No '%s' argument.", "nargs");
831 gctl_error(req, "Missing device.");
834 name = gctl_get_asciiparam(req, "arg0");
836 gctl_error(req, "No 'arg%u' argument.", 0);
839 s = gctl_get_asciiparam(req, "size");
841 gctl_error(req, "No '%s' argument.", "size");
844 mediasize = strtouq(s, &x, 0);
845 if (*x != '\0' || mediasize == 0) {
846 gctl_error(req, "Invalid '%s' argument.", "size");
849 sc = g_mirror_find_device(mp, name);
851 gctl_error(req, "No such device: %s.", name);
854 /* Deny shrinking of an opened provider */
855 if ((g_debugflags & 16) == 0 && sc->sc_provider_open > 0) {
856 if (sc->sc_mediasize > mediasize) {
857 gctl_error(req, "Device %s is busy.",
858 sc->sc_provider->name);
859 sx_xunlock(&sc->sc_lock);
863 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
864 if (mediasize > disk->d_consumer->provider->mediasize -
865 disk->d_consumer->provider->sectorsize) {
866 gctl_error(req, "Provider %s is too small.",
868 sx_xunlock(&sc->sc_lock);
872 /* Update the size. */
873 sc->sc_mediasize = mediasize;
874 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
875 g_mirror_update_metadata(disk);
878 g_resize_provider(sc->sc_provider, mediasize);
880 sx_xunlock(&sc->sc_lock);
884 g_mirror_ctl_deactivate(struct gctl_req *req, struct g_class *mp)
886 struct g_mirror_softc *sc;
887 struct g_mirror_disk *disk;
893 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
895 gctl_error(req, "No '%s' argument.", "nargs");
899 gctl_error(req, "Too few arguments.");
902 name = gctl_get_asciiparam(req, "arg0");
904 gctl_error(req, "No 'arg%u' argument.", 0);
907 sc = g_mirror_find_device(mp, name);
909 gctl_error(req, "No such device: %s.", name);
912 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
913 for (i = 1; i < (u_int)*nargs; i++) {
914 snprintf(param, sizeof(param), "arg%u", i);
915 name = gctl_get_asciiparam(req, param);
917 gctl_error(req, "No 'arg%u' argument.", i);
920 disk = g_mirror_find_disk(sc, name);
922 gctl_error(req, "No such provider: %s.", name);
925 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
929 gctl_error(req, "%s: Can't deactivate the "
930 "last ACTIVE component %s.",
931 sc->sc_geom->name, name);
935 disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE;
936 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
937 g_mirror_update_metadata(disk);
938 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
939 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
940 G_MIRROR_EVENT_DONTWAIT);
942 sx_xunlock(&sc->sc_lock);
946 g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp)
948 struct g_mirror_softc *sc;
949 struct g_mirror_disk *disk;
955 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
957 gctl_error(req, "No '%s' argument.", "nargs");
961 gctl_error(req, "Missing device(s).");
965 for (i = 0; i < (u_int)*nargs; i++) {
966 snprintf(param, sizeof(param), "arg%u", i);
967 name = gctl_get_asciiparam(req, param);
969 gctl_error(req, "No 'arg%u' argument.", i);
972 sc = g_mirror_find_device(mp, name);
974 gctl_error(req, "No such device: %s.", name);
977 if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) {
978 sx_xunlock(&sc->sc_lock);
980 "All disks connected in %s, skipping.",
984 sc->sc_ndisks = g_mirror_ndisks(sc, -1);
985 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
986 g_mirror_update_metadata(disk);
988 sx_xunlock(&sc->sc_lock);
993 g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp, int wipe)
995 struct g_mirror_softc *sc;
996 int *force, *nargs, error;
1002 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1003 if (nargs == NULL) {
1004 gctl_error(req, "No '%s' argument.", "nargs");
1008 gctl_error(req, "Missing device(s).");
1011 force = gctl_get_paraml(req, "force", sizeof(*force));
1012 if (force == NULL) {
1013 gctl_error(req, "No '%s' argument.", "force");
1017 how = G_MIRROR_DESTROY_HARD;
1019 how = G_MIRROR_DESTROY_SOFT;
1021 for (i = 0; i < (u_int)*nargs; i++) {
1022 snprintf(param, sizeof(param), "arg%u", i);
1023 name = gctl_get_asciiparam(req, param);
1025 gctl_error(req, "No 'arg%u' argument.", i);
1028 sc = g_mirror_find_device(mp, name);
1030 gctl_error(req, "No such device: %s.", name);
1035 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WIPE;
1036 error = g_mirror_destroy(sc, how);
1038 gctl_error(req, "Cannot destroy device %s (error=%d).",
1039 sc->sc_geom->name, error);
1041 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_WIPE;
1042 sx_xunlock(&sc->sc_lock);
1045 /* No need to unlock, because lock is already dead. */
1050 g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1054 g_topology_assert();
1056 version = gctl_get_paraml(req, "version", sizeof(*version));
1057 if (version == NULL) {
1058 gctl_error(req, "No '%s' argument.", "version");
1061 if (*version != G_MIRROR_VERSION) {
1062 gctl_error(req, "Userland and kernel parts are out of sync.");
1066 g_topology_unlock();
1067 if (strcmp(verb, "configure") == 0)
1068 g_mirror_ctl_configure(req, mp);
1069 else if (strcmp(verb, "create") == 0)
1070 g_mirror_ctl_create(req, mp);
1071 else if (strcmp(verb, "rebuild") == 0)
1072 g_mirror_ctl_rebuild(req, mp);
1073 else if (strcmp(verb, "insert") == 0)
1074 g_mirror_ctl_insert(req, mp);
1075 else if (strcmp(verb, "remove") == 0)
1076 g_mirror_ctl_remove(req, mp);
1077 else if (strcmp(verb, "resize") == 0)
1078 g_mirror_ctl_resize(req, mp);
1079 else if (strcmp(verb, "deactivate") == 0)
1080 g_mirror_ctl_deactivate(req, mp);
1081 else if (strcmp(verb, "forget") == 0)
1082 g_mirror_ctl_forget(req, mp);
1083 else if (strcmp(verb, "stop") == 0)
1084 g_mirror_ctl_stop(req, mp, 0);
1085 else if (strcmp(verb, "destroy") == 0)
1086 g_mirror_ctl_stop(req, mp, 1);
1088 gctl_error(req, "Unknown verb.");