2 * Copyright (c) 2004-2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/limits.h>
36 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/bitstring.h>
43 #include <machine/atomic.h>
44 #include <geom/geom.h>
45 #include <geom/geom_int.h>
47 #include <sys/kthread.h>
48 #include <geom/mirror/g_mirror.h>
51 static struct g_mirror_softc *
52 g_mirror_find_device(struct g_class *mp, const char *name)
54 struct g_mirror_softc *sc;
58 LIST_FOREACH(gp, &mp->geom, geom) {
62 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0)
64 if (strcmp(gp->name, name) == 0 ||
65 strcmp(sc->sc_name, name) == 0) {
67 sx_xlock(&sc->sc_lock);
75 static struct g_mirror_disk *
76 g_mirror_find_disk(struct g_mirror_softc *sc, const char *name)
78 struct g_mirror_disk *disk;
80 sx_assert(&sc->sc_lock, SX_XLOCKED);
81 if (strncmp(name, "/dev/", 5) == 0)
83 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
84 if (disk->d_consumer == NULL)
86 if (disk->d_consumer->provider == NULL)
88 if (strcmp(disk->d_consumer->provider->name, name) == 0)
95 g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp)
97 struct g_mirror_softc *sc;
98 struct g_mirror_disk *disk;
99 const char *name, *balancep, *prov;
100 intmax_t *slicep, *priority;
103 int *autosync, *noautosync, *failsync, *nofailsync, *hardcode, *dynamic;
104 int *nargs, do_sync = 0, dirty = 1, do_priority = 0;
106 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
108 gctl_error(req, "No '%s' argument.", "nargs");
111 if (*nargs != 1 && *nargs != 2) {
112 gctl_error(req, "Invalid number of arguments.");
115 name = gctl_get_asciiparam(req, "arg0");
117 gctl_error(req, "No 'arg%u' argument.", 0);
120 balancep = gctl_get_asciiparam(req, "balance");
121 if (balancep == NULL) {
122 gctl_error(req, "No '%s' argument.", "balance");
125 autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
126 if (autosync == NULL) {
127 gctl_error(req, "No '%s' argument.", "autosync");
130 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
131 if (noautosync == NULL) {
132 gctl_error(req, "No '%s' argument.", "noautosync");
135 failsync = gctl_get_paraml(req, "failsync", sizeof(*failsync));
136 if (failsync == NULL) {
137 gctl_error(req, "No '%s' argument.", "failsync");
140 nofailsync = gctl_get_paraml(req, "nofailsync", sizeof(*nofailsync));
141 if (nofailsync == NULL) {
142 gctl_error(req, "No '%s' argument.", "nofailsync");
145 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
146 if (hardcode == NULL) {
147 gctl_error(req, "No '%s' argument.", "hardcode");
150 dynamic = gctl_get_paraml(req, "dynamic", sizeof(*dynamic));
151 if (dynamic == NULL) {
152 gctl_error(req, "No '%s' argument.", "dynamic");
155 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
156 if (priority == NULL) {
157 gctl_error(req, "No '%s' argument.", "priority");
160 if (*priority < -1 || *priority > 255) {
161 gctl_error(req, "Priority range is 0 to 255, %jd given",
166 * Since we have a priority, we also need a provider now.
167 * Note: be WARNS safe, by always assigning prov and only throw an
168 * error if *priority != -1.
170 prov = gctl_get_asciiparam(req, "arg1");
171 if (*priority > -1) {
173 gctl_error(req, "Priority needs a disk name");
178 if (*autosync && *noautosync) {
179 gctl_error(req, "'%s' and '%s' specified.", "autosync",
183 if (*failsync && *nofailsync) {
184 gctl_error(req, "'%s' and '%s' specified.", "failsync",
188 if (*hardcode && *dynamic) {
189 gctl_error(req, "'%s' and '%s' specified.", "hardcode",
193 sc = g_mirror_find_device(mp, name);
195 gctl_error(req, "No such device: %s.", name);
198 if (*balancep == '\0')
199 balance = sc->sc_balance;
201 if (balance_id(balancep) == -1) {
202 gctl_error(req, "Invalid balance algorithm.");
203 sx_xunlock(&sc->sc_lock);
206 balance = balance_id(balancep);
208 slicep = gctl_get_paraml(req, "slice", sizeof(*slicep));
209 if (slicep == NULL) {
210 gctl_error(req, "No '%s' argument.", "slice");
211 sx_xunlock(&sc->sc_lock);
215 slice = sc->sc_slice;
218 /* Enforce usage() of -p not allowing any other options. */
219 if (do_priority && (*autosync || *noautosync || *failsync ||
220 *nofailsync || *hardcode || *dynamic || *slicep != -1 ||
221 *balancep != '\0')) {
222 sx_xunlock(&sc->sc_lock);
223 gctl_error(req, "only -p accepted when setting priority");
226 if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync &&
227 !*noautosync && !*failsync && !*nofailsync && !*hardcode &&
228 !*dynamic && !do_priority) {
229 sx_xunlock(&sc->sc_lock);
230 gctl_error(req, "Nothing has changed.");
233 if ((!do_priority && *nargs != 1) || (do_priority && *nargs != 2)) {
234 sx_xunlock(&sc->sc_lock);
235 gctl_error(req, "Invalid number of arguments.");
238 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
239 sx_xunlock(&sc->sc_lock);
240 gctl_error(req, "Not all disks connected. Try 'forget' command "
244 sc->sc_balance = balance;
245 sc->sc_slice = slice;
246 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) {
248 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
253 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
255 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0) {
257 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
260 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
264 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
266 * Handle priority first, since we only need one disk, do one
267 * operation on it and then we're done. No need to check other
268 * flags, as usage doesn't allow it.
271 if (strcmp(disk->d_name, prov) == 0) {
272 if (disk->d_priority == *priority)
273 gctl_error(req, "Nothing has changed.");
275 disk->d_priority = *priority;
276 g_mirror_update_metadata(disk);
283 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
284 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
287 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
289 disk->d_flags &= ~G_MIRROR_DISK_FLAG_HARDCODED;
291 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
292 g_mirror_update_metadata(disk);
294 if (disk->d_state == G_MIRROR_DISK_STATE_STALE) {
295 g_mirror_event_send(disk,
296 G_MIRROR_DISK_STATE_DISCONNECTED,
297 G_MIRROR_EVENT_DONTWAIT);
301 sx_xunlock(&sc->sc_lock);
305 g_mirror_create_orphan(struct g_consumer *cp)
308 KASSERT(1 == 0, ("%s called while creating %s.", __func__,
309 cp->provider->name));
313 g_mirror_ctl_create(struct gctl_req *req, struct g_class *mp)
315 struct g_mirror_metadata md;
317 struct g_consumer *cp;
318 struct g_provider *pp;
319 struct g_mirror_softc *sc;
328 unsigned attached, no, sectorsize;
331 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
333 gctl_error(req, "No '%s' argument.", "nargs");
337 gctl_error(req, "Too few arguments.");
341 strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
342 md.md_version = G_MIRROR_VERSION;
343 name = gctl_get_asciiparam(req, "arg0");
345 gctl_error(req, "No 'arg%u' argument.", 0);
348 strlcpy(md.md_name, name, sizeof(md.md_name));
349 md.md_mid = arc4random();
350 md.md_all = *nargs - 1;
353 md.md_sync_offset = 0;
354 val = gctl_get_paraml(req, "slice", sizeof(*val));
356 gctl_error(req, "No slice argument.");
360 sval = gctl_get_asciiparam(req, "balance");
362 gctl_error(req, "No balance argument.");
365 bal = balance_id(sval);
367 gctl_error(req, "Invalid balance algorithm.");
373 ival = gctl_get_paraml(req, "noautosync", sizeof(*ival));
374 if (ival != NULL && *ival)
375 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
376 ival = gctl_get_paraml(req, "nofailsync", sizeof(*ival));
377 if (ival != NULL && *ival)
378 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
379 /* These fields not used in manual mode. */
380 bzero(md.md_provider, sizeof(md.md_provider));
386 gp = g_new_geomf(mp, "%s", md.md_name);
387 gp->orphan = g_mirror_create_orphan;
388 cp = g_new_consumer(gp);
389 for (no = 1; no < *nargs; no++) {
390 snprintf(param, sizeof(param), "arg%u", no);
391 name = gctl_get_asciiparam(req, param);
393 gctl_error(req, "No 'arg%u' argument.", no);
395 g_destroy_consumer(cp);
400 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
401 name += strlen("/dev/");
402 pp = g_provider_by_name(name);
404 G_MIRROR_DEBUG(1, "Disk %s is invalid.", name);
405 gctl_error(req, "Disk %s is invalid.", name);
409 if (g_access(cp, 1, 0, 0) != 0) {
410 G_MIRROR_DEBUG(1, "Can't open disk %s.", name);
411 gctl_error(req, "Can't open disk %s.", name);
416 if (pp->mediasize == 0 || pp->sectorsize == 0) {
417 G_MIRROR_DEBUG(1, "Disk %s has no media.", name);
418 gctl_error(req, "Disk %s has no media.", name);
419 g_access(cp, -1, 0, 0);
422 if (pp->mediasize < mediasize)
423 mediasize = pp->mediasize;
424 if (pp->sectorsize > sectorsize)
425 sectorsize = pp->sectorsize;
426 g_access(cp, -1, 0, 0);
429 g_destroy_consumer(cp);
431 md.md_mediasize = mediasize;
432 md.md_sectorsize = sectorsize;
433 md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
435 gp = g_mirror_create(mp, &md, G_MIRROR_TYPE_MANUAL);
437 gctl_error(req, "Can't create %s.", md.md_name);
444 sx_xlock(&sc->sc_lock);
445 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
446 sb = sbuf_new_auto();
447 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
448 for (attached = 0, no = 1; no < *nargs; no++) {
449 snprintf(param, sizeof(param), "arg%u", no);
450 name = gctl_get_asciiparam(req, param);
451 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
452 name += strlen("/dev/");
453 pp = g_provider_by_name(name);
455 G_MIRROR_DEBUG(1, "Provider %s disappear?!", name);
456 sbuf_printf(sb, " %s", name);
459 md.md_did = arc4random();
460 md.md_priority = no - 1;
461 if (g_mirror_add_disk(sc, pp, &md) != 0) {
462 G_MIRROR_DEBUG(1, "Disk %u (%s) not attached to %s.",
463 no, pp->name, gp->name);
464 sbuf_printf(sb, " %s", pp->name);
470 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
471 if (md.md_all != attached ||
472 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
473 g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_HARD);
474 gctl_error(req, "%s", sbuf_data(sb));
476 sx_xunlock(&sc->sc_lock);
481 g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
483 struct g_mirror_metadata md;
484 struct g_mirror_softc *sc;
485 struct g_mirror_disk *disk;
486 struct g_provider *pp;
492 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
494 gctl_error(req, "No '%s' argument.", "nargs");
498 gctl_error(req, "Too few arguments.");
501 name = gctl_get_asciiparam(req, "arg0");
503 gctl_error(req, "No 'arg%u' argument.", 0);
506 sc = g_mirror_find_device(mp, name);
508 gctl_error(req, "No such device: %s.", name);
511 for (i = 1; i < (u_int)*nargs; i++) {
512 snprintf(param, sizeof(param), "arg%u", i);
513 name = gctl_get_asciiparam(req, param);
515 gctl_error(req, "No 'arg%u' argument.", i);
518 disk = g_mirror_find_disk(sc, name);
520 gctl_error(req, "No such provider: %s.", name);
523 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
524 disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
526 * This is the last active disk. There will be nothing
527 * to rebuild it from, so deny this request.
530 "Provider %s is the last active provider in %s.",
531 name, sc->sc_geom->name);
535 * Do rebuild by resetting syncid, disconnecting the disk and
536 * connecting it again.
538 disk->d_sync.ds_syncid = 0;
539 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
540 disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC;
541 g_mirror_update_metadata(disk);
542 pp = disk->d_consumer->provider;
544 error = g_mirror_read_metadata(disk->d_consumer, &md);
546 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
547 G_MIRROR_EVENT_WAIT);
549 gctl_error(req, "Cannot read metadata from %s.",
553 error = g_mirror_add_disk(sc, pp, &md);
555 gctl_error(req, "Cannot reconnect component %s.",
560 sx_xunlock(&sc->sc_lock);
564 g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
566 struct g_mirror_softc *sc;
567 struct g_mirror_disk *disk;
568 struct g_mirror_metadata md;
569 struct g_provider *pp;
570 struct g_consumer *cp;
576 int error, *nargs, *hardcode, *inactive;
578 struct g_provider *provider;
579 struct g_consumer *consumer;
583 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
585 gctl_error(req, "No '%s' argument.", "nargs");
589 gctl_error(req, "Too few arguments.");
592 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
593 if (priority == NULL) {
594 gctl_error(req, "No '%s' argument.", "priority");
597 inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive));
598 if (inactive == NULL) {
599 gctl_error(req, "No '%s' argument.", "inactive");
602 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
603 if (hardcode == NULL) {
604 gctl_error(req, "No '%s' argument.", "hardcode");
607 name = gctl_get_asciiparam(req, "arg0");
609 gctl_error(req, "No 'arg%u' argument.", 0);
612 sc = g_mirror_find_device(mp, name);
614 gctl_error(req, "No such device: %s.", name);
617 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
618 gctl_error(req, "Not all disks connected.");
619 sx_xunlock(&sc->sc_lock);
623 disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO);
625 for (i = 1, n = 0; i < (u_int)*nargs; i++) {
626 snprintf(param, sizeof(param), "arg%u", i);
627 name = gctl_get_asciiparam(req, param);
629 gctl_error(req, "No 'arg%u' argument.", i);
632 if (g_mirror_find_disk(sc, name) != NULL) {
633 gctl_error(req, "Provider %s already inserted.", name);
636 if (strncmp(name, "/dev/", 5) == 0)
638 pp = g_provider_by_name(name);
640 gctl_error(req, "Unknown provider %s.", name);
643 cp = g_new_consumer(sc->sc_geom);
644 if (g_attach(cp, pp) != 0) {
645 g_destroy_consumer(cp);
646 gctl_error(req, "Cannot attach to provider %s.", name);
649 if (g_access(cp, 0, 1, 1) != 0) {
650 gctl_error(req, "Cannot access provider %s.", name);
653 g_destroy_consumer(cp);
656 mdsize = (sc->sc_type == G_MIRROR_TYPE_AUTOMATIC) ?
658 if (sc->sc_provider->mediasize > pp->mediasize - mdsize) {
659 gctl_error(req, "Provider %s too small.", name);
661 g_access(cp, 0, -1, -1);
664 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
665 gctl_error(req, "Invalid sectorsize of provider %s.",
669 if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC) {
670 g_access(cp, 0, -1, -1);
672 g_destroy_consumer(cp);
675 g_mirror_fill_metadata(sc, NULL, &md);
676 md.md_priority = *priority;
678 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
679 if (g_mirror_add_disk(sc, pp, &md) != 0) {
681 gctl_error(req, "Disk %s not inserted.", name);
686 disks[n].provider = pp;
687 disks[n].consumer = cp;
692 sx_xunlock(&sc->sc_lock);
698 for (i = 0; i < n; i++) {
699 if (disks[i].consumer == NULL)
701 g_mirror_fill_metadata(sc, NULL, &md);
702 md.md_priority = *priority;
704 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
705 pp = disks[i].provider;
707 strlcpy(md.md_provider, pp->name,
708 sizeof(md.md_provider));
710 bzero(md.md_provider, sizeof(md.md_provider));
712 md.md_provsize = pp->mediasize;
713 sector = g_malloc(pp->sectorsize, M_WAITOK);
714 mirror_metadata_encode(&md, sector);
715 error = g_write_data(disks[i].consumer,
716 pp->mediasize - pp->sectorsize, sector, pp->sectorsize);
719 gctl_error(req, "Cannot store metadata on %s.",
721 g_access(disks[i].consumer, 0, -1, -1);
722 g_detach(disks[i].consumer);
723 g_destroy_consumer(disks[i].consumer);
724 disks[i].consumer = NULL;
725 disks[i].provider = NULL;
732 /* All writes failed. */
733 sx_xunlock(&sc->sc_lock);
737 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
738 g_mirror_update_metadata(disk);
741 * Release provider and wait for retaste.
744 for (i = 0; i < n; i++) {
745 if (disks[i].consumer == NULL)
747 g_access(disks[i].consumer, 0, -1, -1);
748 g_detach(disks[i].consumer);
749 g_destroy_consumer(disks[i].consumer);
752 sx_xunlock(&sc->sc_lock);
757 g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp)
759 struct g_mirror_softc *sc;
760 struct g_mirror_disk *disk;
766 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
768 gctl_error(req, "No '%s' argument.", "nargs");
772 gctl_error(req, "Too few arguments.");
775 name = gctl_get_asciiparam(req, "arg0");
777 gctl_error(req, "No 'arg%u' argument.", 0);
780 sc = g_mirror_find_device(mp, name);
782 gctl_error(req, "No such device: %s.", name);
785 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
786 sx_xunlock(&sc->sc_lock);
787 gctl_error(req, "Not all disks connected. Try 'forget' command "
791 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
792 for (i = 1; i < (u_int)*nargs; i++) {
793 snprintf(param, sizeof(param), "arg%u", i);
794 name = gctl_get_asciiparam(req, param);
796 gctl_error(req, "No 'arg%u' argument.", i);
799 disk = g_mirror_find_disk(sc, name);
801 gctl_error(req, "No such provider: %s.", name);
804 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
808 gctl_error(req, "%s: Can't remove the last "
809 "ACTIVE component %s.", sc->sc_geom->name,
814 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY,
815 G_MIRROR_EVENT_DONTWAIT);
817 sx_xunlock(&sc->sc_lock);
821 g_mirror_ctl_resize(struct gctl_req *req, struct g_class *mp)
823 struct g_mirror_softc *sc;
824 struct g_mirror_disk *disk;
826 const char *name, *s;
830 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
832 gctl_error(req, "No '%s' argument.", "nargs");
836 gctl_error(req, "Missing device.");
839 name = gctl_get_asciiparam(req, "arg0");
841 gctl_error(req, "No 'arg%u' argument.", 0);
844 s = gctl_get_asciiparam(req, "size");
846 gctl_error(req, "No '%s' argument.", "size");
849 mediasize = strtouq(s, &x, 0);
850 if (*x != '\0' || mediasize == 0) {
851 gctl_error(req, "Invalid '%s' argument.", "size");
854 sc = g_mirror_find_device(mp, name);
856 gctl_error(req, "No such device: %s.", name);
859 /* Deny shrinking of an opened provider */
860 if ((g_debugflags & 16) == 0 && sc->sc_provider_open > 0) {
861 if (sc->sc_mediasize > mediasize) {
862 gctl_error(req, "Device %s is busy.",
863 sc->sc_provider->name);
864 sx_xunlock(&sc->sc_lock);
868 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
869 if (mediasize > disk->d_consumer->provider->mediasize -
870 disk->d_consumer->provider->sectorsize) {
871 gctl_error(req, "Provider %s is too small.",
873 sx_xunlock(&sc->sc_lock);
877 /* Update the size. */
878 sc->sc_mediasize = mediasize;
879 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
880 g_mirror_update_metadata(disk);
883 g_resize_provider(sc->sc_provider, mediasize);
885 sx_xunlock(&sc->sc_lock);
889 g_mirror_ctl_deactivate(struct gctl_req *req, struct g_class *mp)
891 struct g_mirror_softc *sc;
892 struct g_mirror_disk *disk;
898 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
900 gctl_error(req, "No '%s' argument.", "nargs");
904 gctl_error(req, "Too few arguments.");
907 name = gctl_get_asciiparam(req, "arg0");
909 gctl_error(req, "No 'arg%u' argument.", 0);
912 sc = g_mirror_find_device(mp, name);
914 gctl_error(req, "No such device: %s.", name);
917 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
918 for (i = 1; i < (u_int)*nargs; i++) {
919 snprintf(param, sizeof(param), "arg%u", i);
920 name = gctl_get_asciiparam(req, param);
922 gctl_error(req, "No 'arg%u' argument.", i);
925 disk = g_mirror_find_disk(sc, name);
927 gctl_error(req, "No such provider: %s.", name);
930 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
934 gctl_error(req, "%s: Can't deactivate the "
935 "last ACTIVE component %s.",
936 sc->sc_geom->name, name);
940 disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE;
941 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
942 g_mirror_update_metadata(disk);
943 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
944 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
945 G_MIRROR_EVENT_DONTWAIT);
947 sx_xunlock(&sc->sc_lock);
951 g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp)
953 struct g_mirror_softc *sc;
954 struct g_mirror_disk *disk;
960 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
962 gctl_error(req, "No '%s' argument.", "nargs");
966 gctl_error(req, "Missing device(s).");
970 for (i = 0; i < (u_int)*nargs; i++) {
971 snprintf(param, sizeof(param), "arg%u", i);
972 name = gctl_get_asciiparam(req, param);
974 gctl_error(req, "No 'arg%u' argument.", i);
977 sc = g_mirror_find_device(mp, name);
979 gctl_error(req, "No such device: %s.", name);
982 if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) {
983 sx_xunlock(&sc->sc_lock);
985 "All disks connected in %s, skipping.",
989 sc->sc_ndisks = g_mirror_ndisks(sc, -1);
990 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
991 g_mirror_update_metadata(disk);
993 sx_xunlock(&sc->sc_lock);
998 g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp, int wipe)
1000 struct g_mirror_softc *sc;
1001 int *force, *nargs, error;
1007 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1008 if (nargs == NULL) {
1009 gctl_error(req, "No '%s' argument.", "nargs");
1013 gctl_error(req, "Missing device(s).");
1016 force = gctl_get_paraml(req, "force", sizeof(*force));
1017 if (force == NULL) {
1018 gctl_error(req, "No '%s' argument.", "force");
1022 how = G_MIRROR_DESTROY_HARD;
1024 how = G_MIRROR_DESTROY_SOFT;
1026 for (i = 0; i < (u_int)*nargs; i++) {
1027 snprintf(param, sizeof(param), "arg%u", i);
1028 name = gctl_get_asciiparam(req, param);
1030 gctl_error(req, "No 'arg%u' argument.", i);
1033 sc = g_mirror_find_device(mp, name);
1035 gctl_error(req, "No such device: %s.", name);
1040 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WIPE;
1041 error = g_mirror_destroy(sc, how);
1043 gctl_error(req, "Cannot destroy device %s (error=%d).",
1044 sc->sc_geom->name, error);
1046 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_WIPE;
1047 sx_xunlock(&sc->sc_lock);
1050 /* No need to unlock, because lock is already dead. */
1055 g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1059 g_topology_assert();
1061 version = gctl_get_paraml(req, "version", sizeof(*version));
1062 if (version == NULL) {
1063 gctl_error(req, "No '%s' argument.", "version");
1066 if (*version != G_MIRROR_VERSION) {
1067 gctl_error(req, "Userland and kernel parts are out of sync.");
1071 g_topology_unlock();
1072 if (strcmp(verb, "configure") == 0)
1073 g_mirror_ctl_configure(req, mp);
1074 else if (strcmp(verb, "create") == 0)
1075 g_mirror_ctl_create(req, mp);
1076 else if (strcmp(verb, "rebuild") == 0)
1077 g_mirror_ctl_rebuild(req, mp);
1078 else if (strcmp(verb, "insert") == 0)
1079 g_mirror_ctl_insert(req, mp);
1080 else if (strcmp(verb, "remove") == 0)
1081 g_mirror_ctl_remove(req, mp);
1082 else if (strcmp(verb, "resize") == 0)
1083 g_mirror_ctl_resize(req, mp);
1084 else if (strcmp(verb, "deactivate") == 0)
1085 g_mirror_ctl_deactivate(req, mp);
1086 else if (strcmp(verb, "forget") == 0)
1087 g_mirror_ctl_forget(req, mp);
1088 else if (strcmp(verb, "stop") == 0)
1089 g_mirror_ctl_stop(req, mp, 0);
1090 else if (strcmp(verb, "destroy") == 0)
1091 g_mirror_ctl_stop(req, mp, 1);
1093 gctl_error(req, "Unknown verb.");