2 * Copyright (c) 2004-2006 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;
100 int *autosync, *noautosync, *failsync, *nofailsync, *hardcode, *dynamic;
101 int *nargs, do_sync = 0, dirty = 1;
103 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
105 gctl_error(req, "No '%s' argument.", "nargs");
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 if (*autosync && *noautosync) {
153 gctl_error(req, "'%s' and '%s' specified.", "autosync",
157 if (*failsync && *nofailsync) {
158 gctl_error(req, "'%s' and '%s' specified.", "failsync",
162 if (*hardcode && *dynamic) {
163 gctl_error(req, "'%s' and '%s' specified.", "hardcode",
167 sc = g_mirror_find_device(mp, name);
169 gctl_error(req, "No such device: %s.", name);
172 if (strcmp(balancep, "none") == 0)
173 balance = sc->sc_balance;
175 if (balance_id(balancep) == -1) {
176 gctl_error(req, "Invalid balance algorithm.");
177 sx_xunlock(&sc->sc_lock);
180 balance = balance_id(balancep);
182 slicep = gctl_get_paraml(req, "slice", sizeof(*slicep));
183 if (slicep == NULL) {
184 gctl_error(req, "No '%s' argument.", "slice");
185 sx_xunlock(&sc->sc_lock);
189 slice = sc->sc_slice;
192 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
193 sx_xunlock(&sc->sc_lock);
194 gctl_error(req, "Not all disks connected. Try 'forget' command "
198 if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync &&
199 !*noautosync && !*failsync && !*nofailsync && !*hardcode &&
201 sx_xunlock(&sc->sc_lock);
202 gctl_error(req, "Nothing has changed.");
205 sc->sc_balance = balance;
206 sc->sc_slice = slice;
207 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) {
209 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
214 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
216 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0) {
218 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
221 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOFAILSYNC;
225 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
227 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
228 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
231 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
233 disk->d_flags &= ~G_MIRROR_DISK_FLAG_HARDCODED;
235 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
236 g_mirror_update_metadata(disk);
238 if (disk->d_state == G_MIRROR_DISK_STATE_STALE) {
239 g_mirror_event_send(disk,
240 G_MIRROR_DISK_STATE_DISCONNECTED,
241 G_MIRROR_EVENT_DONTWAIT);
245 sx_xunlock(&sc->sc_lock);
249 g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
251 struct g_mirror_metadata md;
252 struct g_mirror_softc *sc;
253 struct g_mirror_disk *disk;
254 struct g_provider *pp;
260 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
262 gctl_error(req, "No '%s' argument.", "nargs");
266 gctl_error(req, "Too few arguments.");
269 name = gctl_get_asciiparam(req, "arg0");
271 gctl_error(req, "No 'arg%u' argument.", 0);
274 sc = g_mirror_find_device(mp, name);
276 gctl_error(req, "No such device: %s.", name);
279 for (i = 1; i < (u_int)*nargs; i++) {
280 snprintf(param, sizeof(param), "arg%u", i);
281 name = gctl_get_asciiparam(req, param);
283 gctl_error(req, "No 'arg%u' argument.", i);
286 disk = g_mirror_find_disk(sc, name);
288 gctl_error(req, "No such provider: %s.", name);
291 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
292 disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
294 * This is the last active disk. There will be nothing
295 * to rebuild it from, so deny this request.
298 "Provider %s is the last active provider in %s.",
299 name, sc->sc_geom->name);
303 * Do rebuild by resetting syncid, disconnecting the disk and
304 * connecting it again.
306 disk->d_sync.ds_syncid = 0;
307 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
308 disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC;
309 g_mirror_update_metadata(disk);
310 pp = disk->d_consumer->provider;
312 error = g_mirror_read_metadata(disk->d_consumer, &md);
314 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
315 G_MIRROR_EVENT_WAIT);
317 gctl_error(req, "Cannot read metadata from %s.",
321 error = g_mirror_add_disk(sc, pp, &md);
323 gctl_error(req, "Cannot reconnect component %s.",
328 sx_xunlock(&sc->sc_lock);
332 g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
334 struct g_mirror_softc *sc;
335 struct g_mirror_disk *disk;
336 struct g_mirror_metadata md;
337 struct g_provider *pp;
338 struct g_consumer *cp;
344 int error, *nargs, *hardcode, *inactive;
346 struct g_provider *provider;
347 struct g_consumer *consumer;
350 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
352 gctl_error(req, "No '%s' argument.", "nargs");
356 gctl_error(req, "Too few arguments.");
359 priority = gctl_get_paraml(req, "priority", sizeof(*priority));
360 if (priority == NULL) {
361 gctl_error(req, "No '%s' argument.", "priority");
364 inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive));
365 if (inactive == NULL) {
366 gctl_error(req, "No '%s' argument.", "inactive");
369 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
370 if (hardcode == NULL) {
371 gctl_error(req, "No '%s' argument.", "hardcode");
374 name = gctl_get_asciiparam(req, "arg0");
376 gctl_error(req, "No 'arg%u' argument.", 0);
379 sc = g_mirror_find_device(mp, name);
381 gctl_error(req, "No such device: %s.", name);
384 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
385 gctl_error(req, "Not all disks connected.");
386 sx_xunlock(&sc->sc_lock);
390 disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO);
392 for (i = 1, n = 0; i < (u_int)*nargs; i++) {
393 snprintf(param, sizeof(param), "arg%u", i);
394 name = gctl_get_asciiparam(req, param);
396 gctl_error(req, "No 'arg%u' argument.", i);
399 if (g_mirror_find_disk(sc, name) != NULL) {
400 gctl_error(req, "Provider %s already inserted.", name);
403 if (strncmp(name, "/dev/", 5) == 0)
405 pp = g_provider_by_name(name);
407 gctl_error(req, "Unknown provider %s.", name);
410 if (sc->sc_provider->mediasize >
411 pp->mediasize - pp->sectorsize) {
412 gctl_error(req, "Provider %s too small.", name);
415 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
416 gctl_error(req, "Invalid sectorsize of provider %s.",
420 cp = g_new_consumer(sc->sc_geom);
421 if (g_attach(cp, pp) != 0) {
422 g_destroy_consumer(cp);
423 gctl_error(req, "Cannot attach to provider %s.", name);
426 if (g_access(cp, 0, 1, 1) != 0) {
428 g_destroy_consumer(cp);
429 gctl_error(req, "Cannot access provider %s.", name);
432 disks[n].provider = pp;
433 disks[n].consumer = cp;
438 sx_xunlock(&sc->sc_lock);
444 for (i = 0; i < n; i++) {
445 if (disks[i].consumer == NULL)
447 g_mirror_fill_metadata(sc, NULL, &md);
448 md.md_priority = *priority;
450 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
451 pp = disks[i].provider;
453 strlcpy(md.md_provider, pp->name,
454 sizeof(md.md_provider));
456 bzero(md.md_provider, sizeof(md.md_provider));
458 md.md_provsize = pp->mediasize;
459 sector = g_malloc(pp->sectorsize, M_WAITOK);
460 mirror_metadata_encode(&md, sector);
461 error = g_write_data(disks[i].consumer,
462 pp->mediasize - pp->sectorsize, sector, pp->sectorsize);
465 gctl_error(req, "Cannot store metadata on %s.",
467 g_access(disks[i].consumer, 0, -1, -1);
468 g_detach(disks[i].consumer);
469 g_destroy_consumer(disks[i].consumer);
470 disks[i].consumer = NULL;
471 disks[i].provider = NULL;
478 /* All writes failed. */
479 sx_xunlock(&sc->sc_lock);
483 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
484 g_mirror_update_metadata(disk);
487 * Release provider and wait for retaste.
490 for (i = 0; i < n; i++) {
491 if (disks[i].consumer == NULL)
493 g_access(disks[i].consumer, 0, -1, -1);
494 g_detach(disks[i].consumer);
495 g_destroy_consumer(disks[i].consumer);
498 sx_xunlock(&sc->sc_lock);
503 g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp)
505 struct g_mirror_softc *sc;
506 struct g_mirror_disk *disk;
512 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
514 gctl_error(req, "No '%s' argument.", "nargs");
518 gctl_error(req, "Too few arguments.");
521 name = gctl_get_asciiparam(req, "arg0");
523 gctl_error(req, "No 'arg%u' argument.", 0);
526 sc = g_mirror_find_device(mp, name);
528 gctl_error(req, "No such device: %s.", name);
531 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
532 sx_xunlock(&sc->sc_lock);
533 gctl_error(req, "Not all disks connected. Try 'forget' command "
537 for (i = 1; i < (u_int)*nargs; i++) {
538 snprintf(param, sizeof(param), "arg%u", i);
539 name = gctl_get_asciiparam(req, param);
541 gctl_error(req, "No 'arg%u' argument.", i);
544 disk = g_mirror_find_disk(sc, name);
546 gctl_error(req, "No such provider: %s.", name);
549 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY,
550 G_MIRROR_EVENT_DONTWAIT);
552 sx_xunlock(&sc->sc_lock);
556 g_mirror_ctl_deactivate(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 for (i = 1; i < (u_int)*nargs; i++) {
585 snprintf(param, sizeof(param), "arg%u", i);
586 name = gctl_get_asciiparam(req, param);
588 gctl_error(req, "No 'arg%u' argument.", i);
591 disk = g_mirror_find_disk(sc, name);
593 gctl_error(req, "No such provider: %s.", name);
596 disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE;
597 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
598 g_mirror_update_metadata(disk);
599 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
600 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
601 G_MIRROR_EVENT_DONTWAIT);
603 sx_xunlock(&sc->sc_lock);
607 g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp)
609 struct g_mirror_softc *sc;
610 struct g_mirror_disk *disk;
616 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
618 gctl_error(req, "No '%s' argument.", "nargs");
622 gctl_error(req, "Missing device(s).");
626 for (i = 0; i < (u_int)*nargs; i++) {
627 snprintf(param, sizeof(param), "arg%u", i);
628 name = gctl_get_asciiparam(req, param);
630 gctl_error(req, "No 'arg%u' argument.", i);
633 sc = g_mirror_find_device(mp, name);
635 gctl_error(req, "No such device: %s.", name);
638 if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) {
639 sx_xunlock(&sc->sc_lock);
641 "All disks connected in %s, skipping.",
645 sc->sc_ndisks = g_mirror_ndisks(sc, -1);
646 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
647 g_mirror_update_metadata(disk);
649 sx_xunlock(&sc->sc_lock);
654 g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp)
656 struct g_mirror_softc *sc;
657 int *force, *nargs, error;
663 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
665 gctl_error(req, "No '%s' argument.", "nargs");
669 gctl_error(req, "Missing device(s).");
672 force = gctl_get_paraml(req, "force", sizeof(*force));
674 gctl_error(req, "No '%s' argument.", "force");
678 how = G_MIRROR_DESTROY_HARD;
680 how = G_MIRROR_DESTROY_SOFT;
682 for (i = 0; i < (u_int)*nargs; i++) {
683 snprintf(param, sizeof(param), "arg%u", i);
684 name = gctl_get_asciiparam(req, param);
686 gctl_error(req, "No 'arg%u' argument.", i);
689 sc = g_mirror_find_device(mp, name);
691 gctl_error(req, "No such device: %s.", name);
695 error = g_mirror_destroy(sc, how);
697 gctl_error(req, "Cannot destroy device %s (error=%d).",
698 sc->sc_geom->name, error);
699 sx_xunlock(&sc->sc_lock);
702 /* No need to unlock, because lock is already dead. */
707 g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
713 version = gctl_get_paraml(req, "version", sizeof(*version));
714 if (version == NULL) {
715 gctl_error(req, "No '%s' argument.", "version");
718 if (*version != G_MIRROR_VERSION) {
719 gctl_error(req, "Userland and kernel parts are out of sync.");
724 if (strcmp(verb, "configure") == 0)
725 g_mirror_ctl_configure(req, mp);
726 else if (strcmp(verb, "rebuild") == 0)
727 g_mirror_ctl_rebuild(req, mp);
728 else if (strcmp(verb, "insert") == 0)
729 g_mirror_ctl_insert(req, mp);
730 else if (strcmp(verb, "remove") == 0)
731 g_mirror_ctl_remove(req, mp);
732 else if (strcmp(verb, "deactivate") == 0)
733 g_mirror_ctl_deactivate(req, mp);
734 else if (strcmp(verb, "forget") == 0)
735 g_mirror_ctl_forget(req, mp);
736 else if (strcmp(verb, "stop") == 0)
737 g_mirror_ctl_stop(req, mp);
739 gctl_error(req, "Unknown verb.");