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/raid3/g_raid3.h>
48 static struct g_raid3_softc *
49 g_raid3_find_device(struct g_class *mp, const char *name)
51 struct g_raid3_softc *sc;
55 LIST_FOREACH(gp, &mp->geom, geom) {
59 if ((sc->sc_flags & G_RAID3_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_raid3_disk *
73 g_raid3_find_disk(struct g_raid3_softc *sc, const char *name)
75 struct g_raid3_disk *disk;
78 sx_assert(&sc->sc_lock, SX_XLOCKED);
79 if (strncmp(name, "/dev/", 5) == 0)
81 for (n = 0; n < sc->sc_ndisks; n++) {
82 disk = &sc->sc_disks[n];
83 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
85 if (disk->d_consumer == NULL)
87 if (disk->d_consumer->provider == NULL)
89 if (strcmp(disk->d_consumer->provider->name, name) == 0)
96 g_raid3_ctl_configure(struct gctl_req *req, struct g_class *mp)
98 struct g_raid3_softc *sc;
99 struct g_raid3_disk *disk;
101 int *nargs, do_sync = 0, dirty = 1;
102 int *autosync, *noautosync;
103 int *failsync, *nofailsync;
104 int *round_robin, *noround_robin;
105 int *verify, *noverify;
108 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
110 gctl_error(req, "No '%s' argument.", "nargs");
114 gctl_error(req, "Invalid number of arguments.");
117 autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
118 if (autosync == NULL) {
119 gctl_error(req, "No '%s' argument.", "autosync");
122 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
123 if (noautosync == NULL) {
124 gctl_error(req, "No '%s' argument.", "noautosync");
127 if (*autosync && *noautosync) {
128 gctl_error(req, "'%s' and '%s' specified.", "autosync",
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 if (*failsync && *nofailsync) {
143 gctl_error(req, "'%s' and '%s' specified.", "failsync",
147 round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin));
148 if (round_robin == NULL) {
149 gctl_error(req, "No '%s' argument.", "round_robin");
152 noround_robin = gctl_get_paraml(req, "noround_robin",
153 sizeof(*noround_robin));
154 if (noround_robin == NULL) {
155 gctl_error(req, "No '%s' argument.", "noround_robin");
158 if (*round_robin && *noround_robin) {
159 gctl_error(req, "'%s' and '%s' specified.", "round_robin",
163 verify = gctl_get_paraml(req, "verify", sizeof(*verify));
164 if (verify == NULL) {
165 gctl_error(req, "No '%s' argument.", "verify");
168 noverify = gctl_get_paraml(req, "noverify", sizeof(*noverify));
169 if (noverify == NULL) {
170 gctl_error(req, "No '%s' argument.", "noverify");
173 if (*verify && *noverify) {
174 gctl_error(req, "'%s' and '%s' specified.", "verify",
178 if (!*autosync && !*noautosync && !*failsync && !*nofailsync &&
179 !*round_robin && !*noround_robin && !*verify && !*noverify) {
180 gctl_error(req, "Nothing has changed.");
183 name = gctl_get_asciiparam(req, "arg0");
185 gctl_error(req, "No 'arg%u' argument.", 0);
188 sc = g_raid3_find_device(mp, name);
190 gctl_error(req, "No such device: %s.", name);
193 if (g_raid3_ndisks(sc, -1) < sc->sc_ndisks) {
194 gctl_error(req, "Not all disks connected.");
195 sx_xunlock(&sc->sc_lock);
198 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0) {
200 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
205 sc->sc_flags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
207 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0) {
209 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_NOFAILSYNC;
212 sc->sc_flags |= G_RAID3_DEVICE_FLAG_NOFAILSYNC;
216 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0) {
218 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_VERIFY;
221 sc->sc_flags |= G_RAID3_DEVICE_FLAG_VERIFY;
223 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
225 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
228 sc->sc_flags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
230 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
231 (sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
233 * VERIFY and ROUND-ROBIN options are mutally exclusive.
235 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
237 for (n = 0; n < sc->sc_ndisks; n++) {
238 disk = &sc->sc_disks[n];
240 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING)
241 disk->d_flags &= ~G_RAID3_DISK_FLAG_FORCE_SYNC;
244 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
245 g_raid3_update_metadata(disk);
247 if (disk->d_state == G_RAID3_DISK_STATE_STALE) {
249 * XXX: This is probably possible that this
250 * component will not be retasted.
252 g_raid3_event_send(disk,
253 G_RAID3_DISK_STATE_DISCONNECTED,
254 G_RAID3_EVENT_DONTWAIT);
258 sx_xunlock(&sc->sc_lock);
262 g_raid3_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
264 struct g_raid3_metadata md;
265 struct g_raid3_softc *sc;
266 struct g_raid3_disk *disk;
267 struct g_provider *pp;
271 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
273 gctl_error(req, "No '%s' argument.", "nargs");
277 gctl_error(req, "Invalid number of arguments.");
280 name = gctl_get_asciiparam(req, "arg0");
282 gctl_error(req, "No 'arg%u' argument.", 0);
285 sc = g_raid3_find_device(mp, name);
287 gctl_error(req, "No such device: %s.", name);
290 name = gctl_get_asciiparam(req, "arg1");
292 gctl_error(req, "No 'arg%u' argument.", 1);
293 sx_xunlock(&sc->sc_lock);
296 disk = g_raid3_find_disk(sc, name);
298 gctl_error(req, "No such provider: %s.", name);
299 sx_xunlock(&sc->sc_lock);
302 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE &&
303 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) < sc->sc_ndisks) {
304 gctl_error(req, "There is one stale disk already.");
305 sx_xunlock(&sc->sc_lock);
309 * Do rebuild by resetting syncid and disconnecting disk.
310 * It'll be retasted, connected to the device and synchronized.
312 disk->d_sync.ds_syncid = 0;
313 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0)
314 disk->d_flags |= G_RAID3_DISK_FLAG_FORCE_SYNC;
315 g_raid3_update_metadata(disk);
316 pp = disk->d_consumer->provider;
318 error = g_raid3_read_metadata(disk->d_consumer, &md);
320 g_raid3_event_send(disk, G_RAID3_DISK_STATE_DISCONNECTED,
323 gctl_error(req, "Cannot read metadata from %s.", pp->name);
324 sx_xunlock(&sc->sc_lock);
327 error = g_raid3_add_disk(sc, pp, &md);
329 gctl_error(req, "Cannot reconnect component %s.", pp->name);
330 sx_xunlock(&sc->sc_lock);
334 g_raid3_ctl_stop(struct gctl_req *req, struct g_class *mp)
336 struct g_raid3_softc *sc;
337 int *force, *nargs, error;
343 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
345 gctl_error(req, "No '%s' argument.", "nargs");
349 gctl_error(req, "Missing device(s).");
352 force = gctl_get_paraml(req, "force", sizeof(*force));
354 gctl_error(req, "No '%s' argument.", "force");
358 how = G_RAID3_DESTROY_HARD;
360 how = G_RAID3_DESTROY_SOFT;
362 for (i = 0; i < (u_int)*nargs; i++) {
363 snprintf(param, sizeof(param), "arg%u", i);
364 name = gctl_get_asciiparam(req, param);
366 gctl_error(req, "No 'arg%u' argument.", i);
369 sc = g_raid3_find_device(mp, name);
371 gctl_error(req, "No such device: %s.", name);
375 error = g_raid3_destroy(sc, how);
377 gctl_error(req, "Cannot destroy device %s (error=%d).",
378 sc->sc_geom->name, error);
379 sx_xunlock(&sc->sc_lock);
382 /* No need to unlock, because lock is already dead. */
387 g_raid3_ctl_insert_orphan(struct g_consumer *cp)
390 KASSERT(1 == 0, ("%s called while inserting %s.", __func__,
391 cp->provider->name));
395 g_raid3_ctl_insert(struct gctl_req *req, struct g_class *mp)
397 struct g_raid3_metadata md;
398 struct g_raid3_softc *sc;
399 struct g_raid3_disk *disk;
401 struct g_provider *pp;
402 struct g_consumer *cp;
407 int *hardcode, *nargs, error;
409 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
411 gctl_error(req, "No '%s' argument.", "nargs");
415 gctl_error(req, "Invalid number of arguments.");
418 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
419 if (hardcode == NULL) {
420 gctl_error(req, "No '%s' argument.", "hardcode");
423 name = gctl_get_asciiparam(req, "arg1");
425 gctl_error(req, "No 'arg%u' argument.", 1);
428 no = gctl_get_paraml(req, "number", sizeof(*no));
430 gctl_error(req, "No '%s' argument.", "no");
433 if (strncmp(name, "/dev/", 5) == 0)
436 pp = g_provider_by_name(name);
439 gctl_error(req, "Invalid provider.");
442 gp = g_new_geomf(mp, "raid3:insert");
443 gp->orphan = g_raid3_ctl_insert_orphan;
444 cp = g_new_consumer(gp);
445 error = g_attach(cp, pp);
448 gctl_error(req, "Cannot attach to %s.", pp->name);
451 error = g_access(cp, 0, 1, 1);
454 gctl_error(req, "Cannot access %s.", pp->name);
458 name = gctl_get_asciiparam(req, "arg0");
460 gctl_error(req, "No 'arg%u' argument.", 0);
463 sc = g_raid3_find_device(mp, name);
465 gctl_error(req, "No such device: %s.", name);
468 if (*no >= sc->sc_ndisks) {
469 sx_xunlock(&sc->sc_lock);
470 gctl_error(req, "Invalid component number.");
473 disk = &sc->sc_disks[*no];
474 if (disk->d_state != G_RAID3_DISK_STATE_NODISK) {
475 sx_xunlock(&sc->sc_lock);
476 gctl_error(req, "Component %jd is already connected.", *no);
479 if (((sc->sc_sectorsize / (sc->sc_ndisks - 1)) % pp->sectorsize) != 0) {
480 sx_xunlock(&sc->sc_lock);
482 "Cannot insert provider %s, because of its sector size.",
486 compsize = sc->sc_mediasize / (sc->sc_ndisks - 1);
487 if (compsize > pp->mediasize - pp->sectorsize) {
488 sx_xunlock(&sc->sc_lock);
489 gctl_error(req, "Provider %s too small.", pp->name);
492 if (compsize < pp->mediasize - pp->sectorsize) {
494 "warning: %s: only %jd bytes from %jd bytes used.",
495 pp->name, (intmax_t)compsize,
496 (intmax_t)(pp->mediasize - pp->sectorsize));
498 g_raid3_fill_metadata(disk, &md);
499 sx_xunlock(&sc->sc_lock);
503 strlcpy(md.md_provider, pp->name, sizeof(md.md_provider));
505 bzero(md.md_provider, sizeof(md.md_provider));
506 md.md_provsize = pp->mediasize;
507 sector = g_malloc(pp->sectorsize, M_WAITOK);
508 raid3_metadata_encode(&md, sector);
509 error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector,
513 gctl_error(req, "Cannot store metadata on %s.", pp->name);
517 g_access(cp, 0, -1, -1);
518 if (cp->provider != NULL)
520 g_destroy_consumer(cp);
526 g_raid3_ctl_remove(struct gctl_req *req, struct g_class *mp)
528 struct g_raid3_softc *sc;
529 struct g_raid3_disk *disk;
534 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
536 gctl_error(req, "No '%s' argument.", "nargs");
540 gctl_error(req, "Invalid number of arguments.");
543 no = gctl_get_paraml(req, "number", sizeof(*no));
545 gctl_error(req, "No '%s' argument.", "no");
548 name = gctl_get_asciiparam(req, "arg0");
550 gctl_error(req, "No 'arg%u' argument.", 0);
553 sc = g_raid3_find_device(mp, name);
555 gctl_error(req, "No such device: %s.", name);
558 if (*no >= sc->sc_ndisks) {
559 sx_xunlock(&sc->sc_lock);
560 gctl_error(req, "Invalid component number.");
563 disk = &sc->sc_disks[*no];
564 switch (disk->d_state) {
565 case G_RAID3_DISK_STATE_ACTIVE:
567 * When replacing ACTIVE component, all the rest has to be also
570 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) <
572 gctl_error(req, "Cannot replace component number %jd.",
577 case G_RAID3_DISK_STATE_STALE:
578 case G_RAID3_DISK_STATE_SYNCHRONIZING:
579 if (g_raid3_clear_metadata(disk) != 0) {
580 gctl_error(req, "Cannot clear metadata on %s.",
581 g_raid3_get_diskname(disk));
583 g_raid3_event_send(disk,
584 G_RAID3_DISK_STATE_DISCONNECTED,
585 G_RAID3_EVENT_DONTWAIT);
588 case G_RAID3_DISK_STATE_NODISK:
591 gctl_error(req, "Cannot replace component number %jd.", *no);
594 sx_xunlock(&sc->sc_lock);
598 g_raid3_config(struct gctl_req *req, struct g_class *mp, const char *verb)
604 version = gctl_get_paraml(req, "version", sizeof(*version));
605 if (version == NULL) {
606 gctl_error(req, "No '%s' argument.", "version");
609 if (*version != G_RAID3_VERSION) {
610 gctl_error(req, "Userland and kernel parts are out of sync.");
615 if (strcmp(verb, "configure") == 0)
616 g_raid3_ctl_configure(req, mp);
617 else if (strcmp(verb, "insert") == 0)
618 g_raid3_ctl_insert(req, mp);
619 else if (strcmp(verb, "rebuild") == 0)
620 g_raid3_ctl_rebuild(req, mp);
621 else if (strcmp(verb, "remove") == 0)
622 g_raid3_ctl_remove(req, mp);
623 else if (strcmp(verb, "stop") == 0)
624 g_raid3_ctl_stop(req, mp);
626 gctl_error(req, "Unknown verb.");