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>
48 * Configure, Rebuild, Remove, Deactivate, Forget, and Stop operations do not
49 * seem to depend on any particular g_mirror initialization state.
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);
68 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
69 sx_xunlock(&sc->sc_lock);
79 /* Insert and Resize operations depend on a launched GEOM (sc_provider). */
80 #define GMFL_VALID_FLAGS (M_WAITOK | M_NOWAIT)
81 static struct g_mirror_softc *
82 g_mirror_find_launched_device(struct g_class *mp, const char *name, int flags)
84 struct g_mirror_softc *sc;
87 KASSERT((flags & ~GMFL_VALID_FLAGS) == 0 &&
88 flags != GMFL_VALID_FLAGS && flags != 0,
89 ("%s: Invalid flags %x\n", __func__, (unsigned)flags));
90 #undef GMFL_VALID_FLAGS
93 sc = g_mirror_find_device(mp, name);
96 if (sc->sc_provider != NULL)
98 if (flags & M_NOWAIT) {
99 sx_xunlock(&sc->sc_lock);
104 * This is a dumb hack. G_mirror does not expose any real
105 * wakeup API for observing state changes, and even if it did,
106 * its "RUNNING" state does not actually reflect all softc
107 * elements being initialized.
109 * Revamping g_mirror to have a 3rd, ACTUALLY_RUNNING state and
110 * updating all assertions and sc_state checks is a large work
111 * and would be easy to introduce regressions.
113 * Revamping g_mirror to have a wakeup for state changes would
114 * be difficult if one wanted to capture more than just
115 * sc_state and sc_provider.
117 * For now, just dummy sleep-poll until sc_provider shows up,
118 * the user cancels, or the g_mirror is destroyed.
120 error = sx_sleep(&sc, &sc->sc_lock, PRIBIO | PCATCH | PDROP,
122 if (error != 0 && error != EWOULDBLOCK)
128 static struct g_mirror_disk *
129 g_mirror_find_disk(struct g_mirror_softc *sc, const char *name)
131 struct g_mirror_disk *disk;
133 sx_assert(&sc->sc_lock, SX_XLOCKED);
134 if (strncmp(name, _PATH_DEV, 5) == 0)
136 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
137 if (disk->d_consumer == NULL)
139 if (disk->d_consumer->provider == NULL)
141 if (strcmp(disk->d_consumer->provider->name, name) == 0)
148 g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp)
150 struct g_mirror_softc *sc;
151 struct g_mirror_disk *disk;
152 const char *name, *balancep, *prov;
153 intmax_t *slicep, *priority;
156 int *autosync, *noautosync, *failsync, *nofailsync, *hardcode, *dynamic;
157 int *nargs, do_sync = 0, dirty = 1, do_priority = 0;
159 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
161 gctl_error(req, "No '%s' argument.", "nargs");
164 if (*nargs != 1 && *nargs != 2) {
165 gctl_error(req, "Invalid number of arguments.");
168 name = gctl_get_asciiparam(req, "arg0");
170 gctl_error(req, "No 'arg%u' argument.", 0);
173 balancep = gctl_get_asciiparam(req, "balance");
174 if (balancep == NULL) {
175 gctl_error(req, "No '%s' argument.", "balance");
178 autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
179 if (autosync == NULL) {
180 gctl_error(req, "No '%s' argument.", "autosync");
183 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
184 if (noautosync == NULL) {
185 gctl_error(req, "No '%s' argument.", "noautosync");
188 failsync = gctl_get_paraml(req, "failsync", sizeof(*failsync));
189 if (failsync == NULL) {
190 gctl_error(req, "No '%s' argument.", "failsync");
193 nofailsync = gctl_get_paraml(req, "nofailsync", sizeof(*nofailsync));
194 if (nofailsync == NULL) {
195 gctl_error(req, "No '%s' argument.", "nofailsync");
198 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
199 if (hardcode == NULL) {
200 gctl_error(req, "No '%s' argument.", "hardcode");
203 dynamic = gctl_get_paraml(req, "dynamic", sizeof(*dynamic));
204 if (dynamic == NULL) {
205 gctl_error(req, "No '%s' argument.", "dynamic");
208 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
209 if (priority == NULL) {
210 gctl_error(req, "No '%s' argument.", "priority");
213 if (*priority < -1 || *priority > 255) {
214 gctl_error(req, "Priority range is 0 to 255, %jd given",
219 * Since we have a priority, we also need a provider now.
220 * Note: be WARNS safe, by always assigning prov and only throw an
221 * error if *priority != -1.
223 prov = gctl_get_asciiparam(req, "arg1");
224 if (*priority > -1) {
226 gctl_error(req, "Priority needs a disk name");
231 if (*autosync && *noautosync) {
232 gctl_error(req, "'%s' and '%s' specified.", "autosync",
236 if (*failsync && *nofailsync) {
237 gctl_error(req, "'%s' and '%s' specified.", "failsync",
241 if (*hardcode && *dynamic) {
242 gctl_error(req, "'%s' and '%s' specified.", "hardcode",
246 sc = g_mirror_find_device(mp, name);
248 gctl_error(req, "No such device: %s.", name);
251 if (*balancep == '\0')
252 balance = sc->sc_balance;
254 if (balance_id(balancep) == -1) {
255 gctl_error(req, "Invalid balance algorithm.");
256 sx_xunlock(&sc->sc_lock);
259 balance = balance_id(balancep);
261 slicep = gctl_get_paraml(req, "slice", sizeof(*slicep));
262 if (slicep == NULL) {
263 gctl_error(req, "No '%s' argument.", "slice");
264 sx_xunlock(&sc->sc_lock);
268 slice = sc->sc_slice;
271 /* Enforce usage() of -p not allowing any other options. */
272 if (do_priority && (*autosync || *noautosync || *failsync ||
273 *nofailsync || *hardcode || *dynamic || *slicep != -1 ||
274 *balancep != '\0')) {
275 sx_xunlock(&sc->sc_lock);
276 gctl_error(req, "only -p accepted when setting priority");
279 if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync &&
280 !*noautosync && !*failsync && !*nofailsync && !*hardcode &&
281 !*dynamic && !do_priority) {
282 sx_xunlock(&sc->sc_lock);
283 gctl_error(req, "Nothing has changed.");
286 if ((!do_priority && *nargs != 1) || (do_priority && *nargs != 2)) {
287 sx_xunlock(&sc->sc_lock);
288 gctl_error(req, "Invalid number of arguments.");
291 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
292 sx_xunlock(&sc->sc_lock);
293 gctl_error(req, "Not all disks connected. Try 'forget' command "
297 sc->sc_balance = balance;
298 sc->sc_slice = slice;
299 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) {
301 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
306 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
308 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0) {
310 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
313 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
317 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
319 * Handle priority first, since we only need one disk, do one
320 * operation on it and then we're done. No need to check other
321 * flags, as usage doesn't allow it.
324 if (strcmp(disk->d_name, prov) == 0) {
325 if (disk->d_priority == *priority)
326 gctl_error(req, "Nothing has changed.");
328 disk->d_priority = *priority;
329 g_mirror_update_metadata(disk);
336 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
337 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
340 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
342 disk->d_flags &= ~G_MIRROR_DISK_FLAG_HARDCODED;
344 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
345 g_mirror_update_metadata(disk);
347 if (disk->d_state == G_MIRROR_DISK_STATE_STALE) {
348 g_mirror_event_send(disk,
349 G_MIRROR_DISK_STATE_DISCONNECTED,
350 G_MIRROR_EVENT_DONTWAIT);
354 sx_xunlock(&sc->sc_lock);
358 g_mirror_create_orphan(struct g_consumer *cp)
361 KASSERT(1 == 0, ("%s called while creating %s.", __func__,
362 cp->provider->name));
366 g_mirror_ctl_create(struct gctl_req *req, struct g_class *mp)
368 struct g_mirror_metadata md;
370 struct g_consumer *cp;
371 struct g_provider *pp;
372 struct g_mirror_softc *sc;
381 unsigned attached, no, sectorsize;
384 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
386 gctl_error(req, "No '%s' argument.", "nargs");
390 gctl_error(req, "Too few arguments.");
394 strlcpy(md.md_magic, G_MIRROR_MAGIC, sizeof(md.md_magic));
395 md.md_version = G_MIRROR_VERSION;
396 name = gctl_get_asciiparam(req, "arg0");
398 gctl_error(req, "No 'arg%u' argument.", 0);
401 strlcpy(md.md_name, name, sizeof(md.md_name));
402 md.md_mid = arc4random();
403 md.md_all = *nargs - 1;
406 md.md_sync_offset = 0;
407 val = gctl_get_paraml(req, "slice", sizeof(*val));
409 gctl_error(req, "No slice argument.");
413 sval = gctl_get_asciiparam(req, "balance");
415 gctl_error(req, "No balance argument.");
418 bal = balance_id(sval);
420 gctl_error(req, "Invalid balance algorithm.");
426 ival = gctl_get_paraml(req, "noautosync", sizeof(*ival));
427 if (ival != NULL && *ival)
428 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
429 ival = gctl_get_paraml(req, "nofailsync", sizeof(*ival));
430 if (ival != NULL && *ival)
431 md.md_mflags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
432 /* These fields not used in manual mode. */
433 bzero(md.md_provider, sizeof(md.md_provider));
439 gp = g_new_geomf(mp, "%s", md.md_name);
440 gp->orphan = g_mirror_create_orphan;
441 cp = g_new_consumer(gp);
442 for (no = 1; no < *nargs; no++) {
443 snprintf(param, sizeof(param), "arg%u", no);
444 pp = gctl_get_provider(req, param);
447 g_destroy_consumer(cp);
452 if (g_attach(cp, pp) != 0) {
453 G_MIRROR_DEBUG(1, "Can't attach disk %s.", pp->name);
454 gctl_error(req, "Can't attach disk %s.", pp->name);
457 if (g_access(cp, 1, 0, 0) != 0) {
458 G_MIRROR_DEBUG(1, "Can't open disk %s.", pp->name);
459 gctl_error(req, "Can't open disk %s.", pp->name);
464 if (pp->mediasize == 0 || pp->sectorsize == 0) {
465 G_MIRROR_DEBUG(1, "Disk %s has no media.", pp->name);
466 gctl_error(req, "Disk %s has no media.", pp->name);
467 g_access(cp, -1, 0, 0);
470 if (pp->mediasize < mediasize)
471 mediasize = pp->mediasize;
472 if (pp->sectorsize > sectorsize)
473 sectorsize = pp->sectorsize;
474 g_access(cp, -1, 0, 0);
477 g_destroy_consumer(cp);
479 md.md_mediasize = mediasize;
480 md.md_sectorsize = sectorsize;
481 md.md_mediasize -= (md.md_mediasize % md.md_sectorsize);
483 gp = g_mirror_create(mp, &md, G_MIRROR_TYPE_MANUAL);
485 gctl_error(req, "Can't create %s.", md.md_name);
492 sx_xlock(&sc->sc_lock);
493 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
494 sb = sbuf_new_auto();
495 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
496 for (attached = 0, no = 1; no < *nargs; no++) {
497 snprintf(param, sizeof(param), "arg%u", no);
498 pp = gctl_get_provider(req, param);
500 name = gctl_get_asciiparam(req, param);
502 sbuf_printf(sb, " %s", name);
505 md.md_did = arc4random();
506 md.md_priority = no - 1;
507 if (g_mirror_add_disk(sc, pp, &md) != 0) {
508 G_MIRROR_DEBUG(1, "Disk %u (%s) not attached to %s.",
509 no, pp->name, gp->name);
510 sbuf_printf(sb, " %s", pp->name);
516 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
517 if (md.md_all != attached ||
518 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
519 g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_HARD);
520 gctl_error(req, "%s", sbuf_data(sb));
522 sx_xunlock(&sc->sc_lock);
527 g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
529 struct g_mirror_metadata md;
530 struct g_mirror_softc *sc;
531 struct g_mirror_disk *disk;
532 struct g_provider *pp;
538 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
540 gctl_error(req, "No '%s' argument.", "nargs");
544 gctl_error(req, "Too few arguments.");
547 name = gctl_get_asciiparam(req, "arg0");
549 gctl_error(req, "No 'arg%u' argument.", 0);
552 sc = g_mirror_find_device(mp, name);
554 gctl_error(req, "No such device: %s.", name);
557 for (i = 1; i < (u_int)*nargs; i++) {
558 snprintf(param, sizeof(param), "arg%u", i);
559 name = gctl_get_asciiparam(req, param);
561 gctl_error(req, "No 'arg%u' argument.", i);
564 disk = g_mirror_find_disk(sc, name);
566 gctl_error(req, "No such provider: %s.", name);
569 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
570 disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
572 * This is the last active disk. There will be nothing
573 * to rebuild it from, so deny this request.
576 "Provider %s is the last active provider in %s.",
577 name, sc->sc_geom->name);
581 * Do rebuild by resetting syncid, disconnecting the disk and
582 * connecting it again.
584 disk->d_sync.ds_syncid = 0;
585 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
586 disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC;
587 g_mirror_update_metadata(disk);
588 pp = disk->d_consumer->provider;
590 error = g_mirror_read_metadata(disk->d_consumer, &md);
592 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
593 G_MIRROR_EVENT_WAIT);
595 gctl_error(req, "Cannot read metadata from %s.",
599 error = g_mirror_add_disk(sc, pp, &md);
601 gctl_error(req, "Cannot reconnect component %s.",
606 sx_xunlock(&sc->sc_lock);
610 g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
612 struct g_mirror_softc *sc;
613 struct g_mirror_disk *disk;
614 struct g_mirror_metadata md;
615 struct g_provider *pp;
616 struct g_consumer *cp;
622 int error, *nargs, *hardcode, *inactive;
624 struct g_provider *provider;
625 struct g_consumer *consumer;
629 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
631 gctl_error(req, "No '%s' argument.", "nargs");
635 gctl_error(req, "Too few arguments.");
638 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
639 if (priority == NULL) {
640 gctl_error(req, "No '%s' argument.", "priority");
643 inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive));
644 if (inactive == NULL) {
645 gctl_error(req, "No '%s' argument.", "inactive");
648 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
649 if (hardcode == NULL) {
650 gctl_error(req, "No '%s' argument.", "hardcode");
653 name = gctl_get_asciiparam(req, "arg0");
655 gctl_error(req, "No 'arg%u' argument.", 0);
658 sc = g_mirror_find_launched_device(mp, name, M_WAITOK);
660 gctl_error(req, "No such device: %s.", name);
663 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
664 gctl_error(req, "Not all disks connected.");
665 sx_xunlock(&sc->sc_lock);
669 disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO);
671 for (i = 1, n = 0; i < (u_int)*nargs; i++) {
672 snprintf(param, sizeof(param), "arg%u", i);
673 pp = gctl_get_provider(req, param);
676 if (g_mirror_find_disk(sc, pp->name) != NULL) {
677 gctl_error(req, "Provider %s already inserted.", pp->name);
680 cp = g_new_consumer(sc->sc_geom);
681 if (g_attach(cp, pp) != 0) {
682 g_destroy_consumer(cp);
683 gctl_error(req, "Cannot attach to provider %s.", pp->name);
686 if (g_access(cp, 0, 1, 1) != 0) {
687 gctl_error(req, "Cannot access provider %s.", pp->name);
690 g_destroy_consumer(cp);
693 mdsize = (sc->sc_type == G_MIRROR_TYPE_AUTOMATIC) ?
695 if (sc->sc_provider->mediasize > pp->mediasize - mdsize) {
696 gctl_error(req, "Provider %s too small.", pp->name);
698 g_access(cp, 0, -1, -1);
701 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
702 gctl_error(req, "Invalid sectorsize of provider %s.",
706 if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC) {
707 g_access(cp, 0, -1, -1);
709 g_destroy_consumer(cp);
712 g_mirror_fill_metadata(sc, NULL, &md);
713 md.md_priority = *priority;
715 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
716 if (g_mirror_add_disk(sc, pp, &md) != 0) {
718 gctl_error(req, "Disk %s not inserted.", pp->name);
723 disks[n].provider = pp;
724 disks[n].consumer = cp;
729 sx_xunlock(&sc->sc_lock);
735 for (i = 0; i < n; i++) {
736 if (disks[i].consumer == NULL)
738 g_mirror_fill_metadata(sc, NULL, &md);
739 md.md_priority = *priority;
741 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
742 pp = disks[i].provider;
744 strlcpy(md.md_provider, pp->name,
745 sizeof(md.md_provider));
747 bzero(md.md_provider, sizeof(md.md_provider));
749 md.md_provsize = pp->mediasize;
750 sector = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
751 mirror_metadata_encode(&md, sector);
752 error = g_write_data(disks[i].consumer,
753 pp->mediasize - pp->sectorsize, sector, pp->sectorsize);
756 gctl_error(req, "Cannot store metadata on %s.",
758 g_access(disks[i].consumer, 0, -1, -1);
759 g_detach(disks[i].consumer);
760 g_destroy_consumer(disks[i].consumer);
761 disks[i].consumer = NULL;
762 disks[i].provider = NULL;
769 /* All writes failed. */
770 sx_xunlock(&sc->sc_lock);
774 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
775 g_mirror_update_metadata(disk);
778 * Release provider and wait for retaste.
781 for (i = 0; i < n; i++) {
782 if (disks[i].consumer == NULL)
784 g_access(disks[i].consumer, 0, -1, -1);
785 g_detach(disks[i].consumer);
786 g_destroy_consumer(disks[i].consumer);
789 sx_xunlock(&sc->sc_lock);
794 g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp)
796 struct g_mirror_softc *sc;
797 struct g_mirror_disk *disk;
803 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
805 gctl_error(req, "No '%s' argument.", "nargs");
809 gctl_error(req, "Too few arguments.");
812 name = gctl_get_asciiparam(req, "arg0");
814 gctl_error(req, "No 'arg%u' argument.", 0);
817 sc = g_mirror_find_device(mp, name);
819 gctl_error(req, "No such device: %s.", name);
822 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
823 sx_xunlock(&sc->sc_lock);
824 gctl_error(req, "Not all disks connected. Try 'forget' command "
828 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
829 for (i = 1; i < (u_int)*nargs; i++) {
830 snprintf(param, sizeof(param), "arg%u", i);
831 name = gctl_get_asciiparam(req, param);
833 gctl_error(req, "No 'arg%u' argument.", i);
836 disk = g_mirror_find_disk(sc, name);
838 gctl_error(req, "No such provider: %s.", name);
841 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
845 gctl_error(req, "%s: Can't remove the last "
846 "ACTIVE component %s.", sc->sc_geom->name,
851 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY,
852 G_MIRROR_EVENT_DONTWAIT);
854 sx_xunlock(&sc->sc_lock);
858 g_mirror_ctl_resize(struct gctl_req *req, struct g_class *mp)
860 struct g_mirror_softc *sc;
861 struct g_mirror_disk *disk;
863 const char *name, *s;
867 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
869 gctl_error(req, "No '%s' argument.", "nargs");
873 gctl_error(req, "Missing device.");
876 name = gctl_get_asciiparam(req, "arg0");
878 gctl_error(req, "No 'arg%u' argument.", 0);
881 s = gctl_get_asciiparam(req, "size");
883 gctl_error(req, "No '%s' argument.", "size");
886 mediasize = strtouq(s, &x, 0);
887 if (*x != '\0' || mediasize == 0) {
888 gctl_error(req, "Invalid '%s' argument.", "size");
891 sc = g_mirror_find_launched_device(mp, name, M_WAITOK);
893 gctl_error(req, "No such device: %s.", name);
896 /* Deny shrinking of an opened provider */
897 if ((g_debugflags & G_F_FOOTSHOOTING) == 0 && sc->sc_provider_open > 0) {
898 if (sc->sc_mediasize > mediasize) {
899 gctl_error(req, "Device %s is busy.",
900 sc->sc_provider->name);
901 sx_xunlock(&sc->sc_lock);
905 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
906 if (mediasize > disk->d_consumer->provider->mediasize -
907 disk->d_consumer->provider->sectorsize) {
908 gctl_error(req, "Provider %s is too small.",
910 sx_xunlock(&sc->sc_lock);
914 /* Update the size. */
915 sc->sc_mediasize = mediasize;
916 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
917 g_mirror_update_metadata(disk);
920 g_resize_provider(sc->sc_provider, mediasize);
922 sx_xunlock(&sc->sc_lock);
926 g_mirror_ctl_deactivate(struct gctl_req *req, struct g_class *mp)
928 struct g_mirror_softc *sc;
929 struct g_mirror_disk *disk;
935 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
937 gctl_error(req, "No '%s' argument.", "nargs");
941 gctl_error(req, "Too few arguments.");
944 name = gctl_get_asciiparam(req, "arg0");
946 gctl_error(req, "No 'arg%u' argument.", 0);
949 sc = g_mirror_find_device(mp, name);
951 gctl_error(req, "No such device: %s.", name);
954 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
955 for (i = 1; i < (u_int)*nargs; i++) {
956 snprintf(param, sizeof(param), "arg%u", i);
957 name = gctl_get_asciiparam(req, param);
959 gctl_error(req, "No 'arg%u' argument.", i);
962 disk = g_mirror_find_disk(sc, name);
964 gctl_error(req, "No such provider: %s.", name);
967 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
971 gctl_error(req, "%s: Can't deactivate the "
972 "last ACTIVE component %s.",
973 sc->sc_geom->name, name);
977 disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE;
978 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
979 g_mirror_update_metadata(disk);
980 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
981 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
982 G_MIRROR_EVENT_DONTWAIT);
984 sx_xunlock(&sc->sc_lock);
988 g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp)
990 struct g_mirror_softc *sc;
991 struct g_mirror_disk *disk;
997 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
999 gctl_error(req, "No '%s' argument.", "nargs");
1003 gctl_error(req, "Missing device(s).");
1007 for (i = 0; i < (u_int)*nargs; i++) {
1008 snprintf(param, sizeof(param), "arg%u", i);
1009 name = gctl_get_asciiparam(req, param);
1011 gctl_error(req, "No 'arg%u' argument.", i);
1014 sc = g_mirror_find_device(mp, name);
1016 gctl_error(req, "No such device: %s.", name);
1019 if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) {
1020 sx_xunlock(&sc->sc_lock);
1022 "All disks connected in %s, skipping.",
1026 sc->sc_ndisks = g_mirror_ndisks(sc, -1);
1027 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1028 g_mirror_update_metadata(disk);
1030 sx_xunlock(&sc->sc_lock);
1035 g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp, int wipe)
1037 struct g_mirror_softc *sc;
1038 int *force, *nargs, error;
1044 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1045 if (nargs == NULL) {
1046 gctl_error(req, "No '%s' argument.", "nargs");
1050 gctl_error(req, "Missing device(s).");
1053 force = gctl_get_paraml(req, "force", sizeof(*force));
1054 if (force == NULL) {
1055 gctl_error(req, "No '%s' argument.", "force");
1059 how = G_MIRROR_DESTROY_HARD;
1061 how = G_MIRROR_DESTROY_SOFT;
1063 for (i = 0; i < (u_int)*nargs; i++) {
1064 snprintf(param, sizeof(param), "arg%u", i);
1065 name = gctl_get_asciiparam(req, param);
1067 gctl_error(req, "No 'arg%u' argument.", i);
1070 sc = g_mirror_find_device(mp, name);
1072 gctl_error(req, "No such device: %s.", name);
1077 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WIPE;
1078 error = g_mirror_destroy(sc, how);
1080 gctl_error(req, "Cannot destroy device %s (error=%d).",
1081 sc->sc_geom->name, error);
1083 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_WIPE;
1084 sx_xunlock(&sc->sc_lock);
1087 /* No need to unlock, because lock is already dead. */
1092 g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1096 g_topology_assert();
1098 version = gctl_get_paraml(req, "version", sizeof(*version));
1099 if (version == NULL) {
1100 gctl_error(req, "No '%s' argument.", "version");
1103 if (*version != G_MIRROR_VERSION) {
1104 gctl_error(req, "Userland and kernel parts are out of sync.");
1108 g_topology_unlock();
1109 if (strcmp(verb, "configure") == 0)
1110 g_mirror_ctl_configure(req, mp);
1111 else if (strcmp(verb, "create") == 0)
1112 g_mirror_ctl_create(req, mp);
1113 else if (strcmp(verb, "rebuild") == 0)
1114 g_mirror_ctl_rebuild(req, mp);
1115 else if (strcmp(verb, "insert") == 0)
1116 g_mirror_ctl_insert(req, mp);
1117 else if (strcmp(verb, "remove") == 0)
1118 g_mirror_ctl_remove(req, mp);
1119 else if (strcmp(verb, "resize") == 0)
1120 g_mirror_ctl_resize(req, mp);
1121 else if (strcmp(verb, "deactivate") == 0)
1122 g_mirror_ctl_deactivate(req, mp);
1123 else if (strcmp(verb, "forget") == 0)
1124 g_mirror_ctl_forget(req, mp);
1125 else if (strcmp(verb, "stop") == 0)
1126 g_mirror_ctl_stop(req, mp, 0);
1127 else if (strcmp(verb, "destroy") == 0)
1128 g_mirror_ctl_stop(req, mp, 1);
1130 gctl_error(req, "Unknown verb.");