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, autono;
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 if (gctl_get_param(req, "number", NULL) != NULL)
429 no = gctl_get_paraml(req, "number", sizeof(*no));
432 if (strncmp(name, "/dev/", 5) == 0)
435 pp = g_provider_by_name(name);
438 gctl_error(req, "Invalid provider.");
441 gp = g_new_geomf(mp, "raid3:insert");
442 gp->orphan = g_raid3_ctl_insert_orphan;
443 cp = g_new_consumer(gp);
444 error = g_attach(cp, pp);
447 gctl_error(req, "Cannot attach to %s.", pp->name);
450 error = g_access(cp, 0, 1, 1);
453 gctl_error(req, "Cannot access %s.", pp->name);
457 name = gctl_get_asciiparam(req, "arg0");
459 gctl_error(req, "No 'arg%u' argument.", 0);
462 sc = g_raid3_find_device(mp, name);
464 gctl_error(req, "No such device: %s.", name);
468 if (*no < 0 || *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.",
482 for (autono = 0; autono < sc->sc_ndisks && disk == NULL; autono++)
483 if (sc->sc_disks[autono].d_state ==
484 G_RAID3_DISK_STATE_NODISK)
485 disk = &sc->sc_disks[autono];
487 sx_xunlock(&sc->sc_lock);
488 gctl_error(req, "No disconnected components.");
492 if (((sc->sc_sectorsize / (sc->sc_ndisks - 1)) % pp->sectorsize) != 0) {
493 sx_xunlock(&sc->sc_lock);
495 "Cannot insert provider %s, because of its sector size.",
499 compsize = sc->sc_mediasize / (sc->sc_ndisks - 1);
500 if (compsize > pp->mediasize - pp->sectorsize) {
501 sx_xunlock(&sc->sc_lock);
502 gctl_error(req, "Provider %s too small.", pp->name);
505 if (compsize < pp->mediasize - pp->sectorsize) {
507 "warning: %s: only %jd bytes from %jd bytes used.",
508 pp->name, (intmax_t)compsize,
509 (intmax_t)(pp->mediasize - pp->sectorsize));
511 g_raid3_fill_metadata(disk, &md);
512 sx_xunlock(&sc->sc_lock);
516 strlcpy(md.md_provider, pp->name, sizeof(md.md_provider));
518 bzero(md.md_provider, sizeof(md.md_provider));
519 md.md_provsize = pp->mediasize;
520 sector = g_malloc(pp->sectorsize, M_WAITOK);
521 raid3_metadata_encode(&md, sector);
522 error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector,
526 gctl_error(req, "Cannot store metadata on %s.", pp->name);
530 g_access(cp, 0, -1, -1);
531 if (cp->provider != NULL)
533 g_destroy_consumer(cp);
539 g_raid3_ctl_remove(struct gctl_req *req, struct g_class *mp)
541 struct g_raid3_softc *sc;
542 struct g_raid3_disk *disk;
547 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
549 gctl_error(req, "No '%s' argument.", "nargs");
553 gctl_error(req, "Invalid number of arguments.");
556 no = gctl_get_paraml(req, "number", sizeof(*no));
558 gctl_error(req, "No '%s' argument.", "no");
561 name = gctl_get_asciiparam(req, "arg0");
563 gctl_error(req, "No 'arg%u' argument.", 0);
566 sc = g_raid3_find_device(mp, name);
568 gctl_error(req, "No such device: %s.", name);
571 if (*no >= sc->sc_ndisks) {
572 sx_xunlock(&sc->sc_lock);
573 gctl_error(req, "Invalid component number.");
576 disk = &sc->sc_disks[*no];
577 switch (disk->d_state) {
578 case G_RAID3_DISK_STATE_ACTIVE:
580 * When replacing ACTIVE component, all the rest has to be also
583 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) <
585 gctl_error(req, "Cannot replace component number %jd.",
590 case G_RAID3_DISK_STATE_STALE:
591 case G_RAID3_DISK_STATE_SYNCHRONIZING:
592 if (g_raid3_clear_metadata(disk) != 0) {
593 gctl_error(req, "Cannot clear metadata on %s.",
594 g_raid3_get_diskname(disk));
596 g_raid3_event_send(disk,
597 G_RAID3_DISK_STATE_DISCONNECTED,
598 G_RAID3_EVENT_DONTWAIT);
601 case G_RAID3_DISK_STATE_NODISK:
604 gctl_error(req, "Cannot replace component number %jd.", *no);
607 sx_xunlock(&sc->sc_lock);
611 g_raid3_config(struct gctl_req *req, struct g_class *mp, const char *verb)
617 version = gctl_get_paraml(req, "version", sizeof(*version));
618 if (version == NULL) {
619 gctl_error(req, "No '%s' argument.", "version");
622 if (*version != G_RAID3_VERSION) {
623 gctl_error(req, "Userland and kernel parts are out of sync.");
628 if (strcmp(verb, "configure") == 0)
629 g_raid3_ctl_configure(req, mp);
630 else if (strcmp(verb, "insert") == 0)
631 g_raid3_ctl_insert(req, mp);
632 else if (strcmp(verb, "rebuild") == 0)
633 g_raid3_ctl_rebuild(req, mp);
634 else if (strcmp(verb, "remove") == 0)
635 g_raid3_ctl_remove(req, mp);
636 else if (strcmp(verb, "stop") == 0)
637 g_raid3_ctl_stop(req, mp);
639 gctl_error(req, "Unknown verb.");