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>
35 #include <sys/mutex.h>
37 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <sys/bitstring.h>
41 #include <machine/atomic.h>
42 #include <geom/geom.h>
44 #include <sys/kthread.h>
45 #include <geom/mirror/g_mirror.h>
48 static struct g_mirror_softc *
49 g_mirror_find_device(struct g_class *mp, const char *name)
51 struct g_mirror_softc *sc;
55 LIST_FOREACH(gp, &mp->geom, geom) {
59 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0)
61 if (strcmp(gp->name, name) == 0 ||
62 strcmp(sc->sc_name, name) == 0) {
64 sx_xlock(&sc->sc_lock);
72 static struct g_mirror_disk *
73 g_mirror_find_disk(struct g_mirror_softc *sc, const char *name)
75 struct g_mirror_disk *disk;
77 sx_assert(&sc->sc_lock, SX_XLOCKED);
78 if (strncmp(name, "/dev/", 5) == 0)
80 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
81 if (disk->d_consumer == NULL)
83 if (disk->d_consumer->provider == NULL)
85 if (strcmp(disk->d_consumer->provider->name, name) == 0)
92 g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp)
94 struct g_mirror_softc *sc;
95 struct g_mirror_disk *disk;
96 const char *name, *balancep, *prov;
97 intmax_t *slicep, *priority;
100 int *autosync, *noautosync, *failsync, *nofailsync, *hardcode, *dynamic;
101 int *nargs, do_sync = 0, dirty = 1, do_priority = 0;
103 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
105 gctl_error(req, "No '%s' argument.", "nargs");
108 if (*nargs != 1 && *nargs != 2) {
109 gctl_error(req, "Invalid number of arguments.");
112 name = gctl_get_asciiparam(req, "arg0");
114 gctl_error(req, "No 'arg%u' argument.", 0);
117 balancep = gctl_get_asciiparam(req, "balance");
118 if (balancep == NULL) {
119 gctl_error(req, "No '%s' argument.", "balance");
122 autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
123 if (autosync == NULL) {
124 gctl_error(req, "No '%s' argument.", "autosync");
127 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
128 if (noautosync == NULL) {
129 gctl_error(req, "No '%s' argument.", "noautosync");
132 failsync = gctl_get_paraml(req, "failsync", sizeof(*failsync));
133 if (failsync == NULL) {
134 gctl_error(req, "No '%s' argument.", "failsync");
137 nofailsync = gctl_get_paraml(req, "nofailsync", sizeof(*nofailsync));
138 if (nofailsync == NULL) {
139 gctl_error(req, "No '%s' argument.", "nofailsync");
142 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
143 if (hardcode == NULL) {
144 gctl_error(req, "No '%s' argument.", "hardcode");
147 dynamic = gctl_get_paraml(req, "dynamic", sizeof(*dynamic));
148 if (dynamic == NULL) {
149 gctl_error(req, "No '%s' argument.", "dynamic");
152 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
153 if (priority == NULL) {
154 gctl_error(req, "No '%s' argument.", "priority");
157 if (*priority < -1 || *priority > 255) {
158 gctl_error(req, "Priority range is 0 to 255, %jd given",
163 * Since we have a priority, we also need a provider now.
164 * Note: be WARNS safe, by always assigning prov and only throw an
165 * error if *priority != -1.
167 prov = gctl_get_asciiparam(req, "arg1");
168 if (*priority > -1) {
170 gctl_error(req, "Priority needs a disk name");
175 if (*autosync && *noautosync) {
176 gctl_error(req, "'%s' and '%s' specified.", "autosync",
180 if (*failsync && *nofailsync) {
181 gctl_error(req, "'%s' and '%s' specified.", "failsync",
185 if (*hardcode && *dynamic) {
186 gctl_error(req, "'%s' and '%s' specified.", "hardcode",
190 sc = g_mirror_find_device(mp, name);
192 gctl_error(req, "No such device: %s.", name);
195 if (*balancep == '\0')
196 balance = sc->sc_balance;
198 if (balance_id(balancep) == -1) {
199 gctl_error(req, "Invalid balance algorithm.");
200 sx_xunlock(&sc->sc_lock);
203 balance = balance_id(balancep);
205 slicep = gctl_get_paraml(req, "slice", sizeof(*slicep));
206 if (slicep == NULL) {
207 gctl_error(req, "No '%s' argument.", "slice");
208 sx_xunlock(&sc->sc_lock);
212 slice = sc->sc_slice;
215 /* Enforce usage() of -p not allowing any other options. */
216 if (do_priority && (*autosync || *noautosync || *failsync ||
217 *nofailsync || *hardcode || *dynamic || *slicep != -1 ||
218 *balancep != '\0')) {
219 sx_xunlock(&sc->sc_lock);
220 gctl_error(req, "only -p accepted when setting priority");
223 if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync &&
224 !*noautosync && !*failsync && !*nofailsync && !*hardcode &&
225 !*dynamic && !do_priority) {
226 sx_xunlock(&sc->sc_lock);
227 gctl_error(req, "Nothing has changed.");
230 if ((!do_priority && *nargs != 1) || (do_priority && *nargs != 2)) {
231 sx_xunlock(&sc->sc_lock);
232 gctl_error(req, "Invalid number of arguments.");
235 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
236 sx_xunlock(&sc->sc_lock);
237 gctl_error(req, "Not all disks connected. Try 'forget' command "
241 sc->sc_balance = balance;
242 sc->sc_slice = slice;
243 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) {
245 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
250 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
252 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0) {
254 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
257 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
261 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
263 * Handle priority first, since we only need one disk, do one
264 * operation on it and then we're done. No need to check other
265 * flags, as usage doesn't allow it.
268 if (strcmp(disk->d_name, prov) == 0) {
269 if (disk->d_priority == *priority)
270 gctl_error(req, "Nothing has changed.");
272 disk->d_priority = *priority;
273 g_mirror_update_metadata(disk);
280 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
281 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
284 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
286 disk->d_flags &= ~G_MIRROR_DISK_FLAG_HARDCODED;
288 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
289 g_mirror_update_metadata(disk);
291 if (disk->d_state == G_MIRROR_DISK_STATE_STALE) {
292 g_mirror_event_send(disk,
293 G_MIRROR_DISK_STATE_DISCONNECTED,
294 G_MIRROR_EVENT_DONTWAIT);
298 sx_xunlock(&sc->sc_lock);
302 g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
304 struct g_mirror_metadata md;
305 struct g_mirror_softc *sc;
306 struct g_mirror_disk *disk;
307 struct g_provider *pp;
313 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
315 gctl_error(req, "No '%s' argument.", "nargs");
319 gctl_error(req, "Too few arguments.");
322 name = gctl_get_asciiparam(req, "arg0");
324 gctl_error(req, "No 'arg%u' argument.", 0);
327 sc = g_mirror_find_device(mp, name);
329 gctl_error(req, "No such device: %s.", name);
332 for (i = 1; i < (u_int)*nargs; i++) {
333 snprintf(param, sizeof(param), "arg%u", i);
334 name = gctl_get_asciiparam(req, param);
336 gctl_error(req, "No 'arg%u' argument.", i);
339 disk = g_mirror_find_disk(sc, name);
341 gctl_error(req, "No such provider: %s.", name);
344 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
345 disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
347 * This is the last active disk. There will be nothing
348 * to rebuild it from, so deny this request.
351 "Provider %s is the last active provider in %s.",
352 name, sc->sc_geom->name);
356 * Do rebuild by resetting syncid, disconnecting the disk and
357 * connecting it again.
359 disk->d_sync.ds_syncid = 0;
360 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
361 disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC;
362 g_mirror_update_metadata(disk);
363 pp = disk->d_consumer->provider;
365 error = g_mirror_read_metadata(disk->d_consumer, &md);
367 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
368 G_MIRROR_EVENT_WAIT);
370 gctl_error(req, "Cannot read metadata from %s.",
374 error = g_mirror_add_disk(sc, pp, &md);
376 gctl_error(req, "Cannot reconnect component %s.",
381 sx_xunlock(&sc->sc_lock);
385 g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
387 struct g_mirror_softc *sc;
388 struct g_mirror_disk *disk;
389 struct g_mirror_metadata md;
390 struct g_provider *pp;
391 struct g_consumer *cp;
397 int error, *nargs, *hardcode, *inactive;
399 struct g_provider *provider;
400 struct g_consumer *consumer;
403 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
405 gctl_error(req, "No '%s' argument.", "nargs");
409 gctl_error(req, "Too few arguments.");
412 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
413 if (priority == NULL) {
414 gctl_error(req, "No '%s' argument.", "priority");
417 inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive));
418 if (inactive == NULL) {
419 gctl_error(req, "No '%s' argument.", "inactive");
422 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
423 if (hardcode == NULL) {
424 gctl_error(req, "No '%s' argument.", "hardcode");
427 name = gctl_get_asciiparam(req, "arg0");
429 gctl_error(req, "No 'arg%u' argument.", 0);
432 sc = g_mirror_find_device(mp, name);
434 gctl_error(req, "No such device: %s.", name);
437 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
438 gctl_error(req, "Not all disks connected.");
439 sx_xunlock(&sc->sc_lock);
443 disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO);
445 for (i = 1, n = 0; i < (u_int)*nargs; i++) {
446 snprintf(param, sizeof(param), "arg%u", i);
447 name = gctl_get_asciiparam(req, param);
449 gctl_error(req, "No 'arg%u' argument.", i);
452 if (g_mirror_find_disk(sc, name) != NULL) {
453 gctl_error(req, "Provider %s already inserted.", name);
456 if (strncmp(name, "/dev/", 5) == 0)
458 pp = g_provider_by_name(name);
460 gctl_error(req, "Unknown provider %s.", name);
463 if (sc->sc_provider->mediasize >
464 pp->mediasize - pp->sectorsize) {
465 gctl_error(req, "Provider %s too small.", name);
468 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
469 gctl_error(req, "Invalid sectorsize of provider %s.",
473 cp = g_new_consumer(sc->sc_geom);
474 if (g_attach(cp, pp) != 0) {
475 g_destroy_consumer(cp);
476 gctl_error(req, "Cannot attach to provider %s.", name);
479 if (g_access(cp, 0, 1, 1) != 0) {
481 g_destroy_consumer(cp);
482 gctl_error(req, "Cannot access provider %s.", name);
485 disks[n].provider = pp;
486 disks[n].consumer = cp;
491 sx_xunlock(&sc->sc_lock);
497 for (i = 0; i < n; i++) {
498 if (disks[i].consumer == NULL)
500 g_mirror_fill_metadata(sc, NULL, &md);
501 md.md_priority = *priority;
503 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
504 pp = disks[i].provider;
506 strlcpy(md.md_provider, pp->name,
507 sizeof(md.md_provider));
509 bzero(md.md_provider, sizeof(md.md_provider));
511 md.md_provsize = pp->mediasize;
512 sector = g_malloc(pp->sectorsize, M_WAITOK);
513 mirror_metadata_encode(&md, sector);
514 error = g_write_data(disks[i].consumer,
515 pp->mediasize - pp->sectorsize, sector, pp->sectorsize);
518 gctl_error(req, "Cannot store metadata on %s.",
520 g_access(disks[i].consumer, 0, -1, -1);
521 g_detach(disks[i].consumer);
522 g_destroy_consumer(disks[i].consumer);
523 disks[i].consumer = NULL;
524 disks[i].provider = NULL;
531 /* All writes failed. */
532 sx_xunlock(&sc->sc_lock);
536 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
537 g_mirror_update_metadata(disk);
540 * Release provider and wait for retaste.
543 for (i = 0; i < n; i++) {
544 if (disks[i].consumer == NULL)
546 g_access(disks[i].consumer, 0, -1, -1);
547 g_detach(disks[i].consumer);
548 g_destroy_consumer(disks[i].consumer);
551 sx_xunlock(&sc->sc_lock);
556 g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp)
558 struct g_mirror_softc *sc;
559 struct g_mirror_disk *disk;
565 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
567 gctl_error(req, "No '%s' argument.", "nargs");
571 gctl_error(req, "Too few arguments.");
574 name = gctl_get_asciiparam(req, "arg0");
576 gctl_error(req, "No 'arg%u' argument.", 0);
579 sc = g_mirror_find_device(mp, name);
581 gctl_error(req, "No such device: %s.", name);
584 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
585 sx_xunlock(&sc->sc_lock);
586 gctl_error(req, "Not all disks connected. Try 'forget' command "
590 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
591 for (i = 1; i < (u_int)*nargs; i++) {
592 snprintf(param, sizeof(param), "arg%u", i);
593 name = gctl_get_asciiparam(req, param);
595 gctl_error(req, "No 'arg%u' argument.", i);
598 disk = g_mirror_find_disk(sc, name);
600 gctl_error(req, "No such provider: %s.", name);
603 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
607 gctl_error(req, "%s: Can't remove the last "
608 "ACTIVE component %s.", sc->sc_geom->name,
613 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY,
614 G_MIRROR_EVENT_DONTWAIT);
616 sx_xunlock(&sc->sc_lock);
620 g_mirror_ctl_deactivate(struct gctl_req *req, struct g_class *mp)
622 struct g_mirror_softc *sc;
623 struct g_mirror_disk *disk;
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 name = gctl_get_asciiparam(req, "arg0");
640 gctl_error(req, "No 'arg%u' argument.", 0);
643 sc = g_mirror_find_device(mp, name);
645 gctl_error(req, "No such device: %s.", name);
648 active = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
649 for (i = 1; i < (u_int)*nargs; i++) {
650 snprintf(param, sizeof(param), "arg%u", i);
651 name = gctl_get_asciiparam(req, param);
653 gctl_error(req, "No 'arg%u' argument.", i);
656 disk = g_mirror_find_disk(sc, name);
658 gctl_error(req, "No such provider: %s.", name);
661 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
665 gctl_error(req, "%s: Can't deactivate the "
666 "last ACTIVE component %s.",
667 sc->sc_geom->name, name);
671 disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE;
672 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
673 g_mirror_update_metadata(disk);
674 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
675 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
676 G_MIRROR_EVENT_DONTWAIT);
678 sx_xunlock(&sc->sc_lock);
682 g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp)
684 struct g_mirror_softc *sc;
685 struct g_mirror_disk *disk;
691 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
693 gctl_error(req, "No '%s' argument.", "nargs");
697 gctl_error(req, "Missing device(s).");
701 for (i = 0; i < (u_int)*nargs; i++) {
702 snprintf(param, sizeof(param), "arg%u", i);
703 name = gctl_get_asciiparam(req, param);
705 gctl_error(req, "No 'arg%u' argument.", i);
708 sc = g_mirror_find_device(mp, name);
710 gctl_error(req, "No such device: %s.", name);
713 if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) {
714 sx_xunlock(&sc->sc_lock);
716 "All disks connected in %s, skipping.",
720 sc->sc_ndisks = g_mirror_ndisks(sc, -1);
721 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
722 g_mirror_update_metadata(disk);
724 sx_xunlock(&sc->sc_lock);
729 g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp, int wipe)
731 struct g_mirror_softc *sc;
732 int *force, *nargs, error;
738 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
740 gctl_error(req, "No '%s' argument.", "nargs");
744 gctl_error(req, "Missing device(s).");
747 force = gctl_get_paraml(req, "force", sizeof(*force));
749 gctl_error(req, "No '%s' argument.", "force");
753 how = G_MIRROR_DESTROY_HARD;
755 how = G_MIRROR_DESTROY_SOFT;
757 for (i = 0; i < (u_int)*nargs; i++) {
758 snprintf(param, sizeof(param), "arg%u", i);
759 name = gctl_get_asciiparam(req, param);
761 gctl_error(req, "No 'arg%u' argument.", i);
764 sc = g_mirror_find_device(mp, name);
766 gctl_error(req, "No such device: %s.", name);
771 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WIPE;
772 error = g_mirror_destroy(sc, how);
774 gctl_error(req, "Cannot destroy device %s (error=%d).",
775 sc->sc_geom->name, error);
777 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_WIPE;
778 sx_xunlock(&sc->sc_lock);
781 /* No need to unlock, because lock is already dead. */
786 g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
792 version = gctl_get_paraml(req, "version", sizeof(*version));
793 if (version == NULL) {
794 gctl_error(req, "No '%s' argument.", "version");
797 if (*version != G_MIRROR_VERSION) {
798 gctl_error(req, "Userland and kernel parts are out of sync.");
803 if (strcmp(verb, "configure") == 0)
804 g_mirror_ctl_configure(req, mp);
805 else if (strcmp(verb, "rebuild") == 0)
806 g_mirror_ctl_rebuild(req, mp);
807 else if (strcmp(verb, "insert") == 0)
808 g_mirror_ctl_insert(req, mp);
809 else if (strcmp(verb, "remove") == 0)
810 g_mirror_ctl_remove(req, mp);
811 else if (strcmp(verb, "deactivate") == 0)
812 g_mirror_ctl_deactivate(req, mp);
813 else if (strcmp(verb, "forget") == 0)
814 g_mirror_ctl_forget(req, mp);
815 else if (strcmp(verb, "stop") == 0)
816 g_mirror_ctl_stop(req, mp, 0);
817 else if (strcmp(verb, "destroy") == 0)
818 g_mirror_ctl_stop(req, mp, 1);
820 gctl_error(req, "Unknown verb.");