2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2006 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>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/bitstring.h>
43 #include <machine/atomic.h>
44 #include <geom/geom.h>
46 #include <sys/kthread.h>
47 #include <geom/raid3/g_raid3.h>
50 static struct g_raid3_softc *
51 g_raid3_find_device(struct g_class *mp, const char *name)
53 struct g_raid3_softc *sc;
57 LIST_FOREACH(gp, &mp->geom, geom) {
61 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0)
63 if (strcmp(gp->name, name) == 0 ||
64 strcmp(sc->sc_name, name) == 0) {
66 sx_xlock(&sc->sc_lock);
74 static struct g_raid3_disk *
75 g_raid3_find_disk(struct g_raid3_softc *sc, const char *name)
77 struct g_raid3_disk *disk;
80 sx_assert(&sc->sc_lock, SX_XLOCKED);
81 if (strncmp(name, "/dev/", 5) == 0)
83 for (n = 0; n < sc->sc_ndisks; n++) {
84 disk = &sc->sc_disks[n];
85 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
87 if (disk->d_consumer == NULL)
89 if (disk->d_consumer->provider == NULL)
91 if (strcmp(disk->d_consumer->provider->name, name) == 0)
98 g_raid3_ctl_configure(struct gctl_req *req, struct g_class *mp)
100 struct g_raid3_softc *sc;
101 struct g_raid3_disk *disk;
103 int *nargs, do_sync = 0, dirty = 1;
104 int *autosync, *noautosync;
105 int *failsync, *nofailsync;
106 int *round_robin, *noround_robin;
107 int *verify, *noverify;
110 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
112 gctl_error(req, "No '%s' argument.", "nargs");
116 gctl_error(req, "Invalid number of arguments.");
119 autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
120 if (autosync == NULL) {
121 gctl_error(req, "No '%s' argument.", "autosync");
124 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
125 if (noautosync == NULL) {
126 gctl_error(req, "No '%s' argument.", "noautosync");
129 if (*autosync && *noautosync) {
130 gctl_error(req, "'%s' and '%s' specified.", "autosync",
134 failsync = gctl_get_paraml(req, "failsync", sizeof(*failsync));
135 if (failsync == NULL) {
136 gctl_error(req, "No '%s' argument.", "failsync");
139 nofailsync = gctl_get_paraml(req, "nofailsync", sizeof(*nofailsync));
140 if (nofailsync == NULL) {
141 gctl_error(req, "No '%s' argument.", "nofailsync");
144 if (*failsync && *nofailsync) {
145 gctl_error(req, "'%s' and '%s' specified.", "failsync",
149 round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin));
150 if (round_robin == NULL) {
151 gctl_error(req, "No '%s' argument.", "round_robin");
154 noround_robin = gctl_get_paraml(req, "noround_robin",
155 sizeof(*noround_robin));
156 if (noround_robin == NULL) {
157 gctl_error(req, "No '%s' argument.", "noround_robin");
160 if (*round_robin && *noround_robin) {
161 gctl_error(req, "'%s' and '%s' specified.", "round_robin",
165 verify = gctl_get_paraml(req, "verify", sizeof(*verify));
166 if (verify == NULL) {
167 gctl_error(req, "No '%s' argument.", "verify");
170 noverify = gctl_get_paraml(req, "noverify", sizeof(*noverify));
171 if (noverify == NULL) {
172 gctl_error(req, "No '%s' argument.", "noverify");
175 if (*verify && *noverify) {
176 gctl_error(req, "'%s' and '%s' specified.", "verify",
180 if (!*autosync && !*noautosync && !*failsync && !*nofailsync &&
181 !*round_robin && !*noround_robin && !*verify && !*noverify) {
182 gctl_error(req, "Nothing has changed.");
185 name = gctl_get_asciiparam(req, "arg0");
187 gctl_error(req, "No 'arg%u' argument.", 0);
190 sc = g_raid3_find_device(mp, name);
192 gctl_error(req, "No such device: %s.", name);
195 if (g_raid3_ndisks(sc, -1) < sc->sc_ndisks) {
196 gctl_error(req, "Not all disks connected.");
197 sx_xunlock(&sc->sc_lock);
200 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0) {
202 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
207 sc->sc_flags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
209 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0) {
211 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_NOFAILSYNC;
214 sc->sc_flags |= G_RAID3_DEVICE_FLAG_NOFAILSYNC;
218 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0) {
220 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_VERIFY;
223 sc->sc_flags |= G_RAID3_DEVICE_FLAG_VERIFY;
225 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
227 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
230 sc->sc_flags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
232 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
233 (sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
235 * VERIFY and ROUND-ROBIN options are mutally exclusive.
237 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
239 for (n = 0; n < sc->sc_ndisks; n++) {
240 disk = &sc->sc_disks[n];
242 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING)
243 disk->d_flags &= ~G_RAID3_DISK_FLAG_FORCE_SYNC;
246 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
247 g_raid3_update_metadata(disk);
249 if (disk->d_state == G_RAID3_DISK_STATE_STALE) {
251 * XXX: This is probably possible that this
252 * component will not be retasted.
254 g_raid3_event_send(disk,
255 G_RAID3_DISK_STATE_DISCONNECTED,
256 G_RAID3_EVENT_DONTWAIT);
260 sx_xunlock(&sc->sc_lock);
264 g_raid3_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
266 struct g_raid3_metadata md;
267 struct g_raid3_softc *sc;
268 struct g_raid3_disk *disk;
269 struct g_provider *pp;
273 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
275 gctl_error(req, "No '%s' argument.", "nargs");
279 gctl_error(req, "Invalid number of arguments.");
282 name = gctl_get_asciiparam(req, "arg0");
284 gctl_error(req, "No 'arg%u' argument.", 0);
287 sc = g_raid3_find_device(mp, name);
289 gctl_error(req, "No such device: %s.", name);
292 name = gctl_get_asciiparam(req, "arg1");
294 gctl_error(req, "No 'arg%u' argument.", 1);
295 sx_xunlock(&sc->sc_lock);
298 disk = g_raid3_find_disk(sc, name);
300 gctl_error(req, "No such provider: %s.", name);
301 sx_xunlock(&sc->sc_lock);
304 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE &&
305 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) < sc->sc_ndisks) {
306 gctl_error(req, "There is one stale disk already.");
307 sx_xunlock(&sc->sc_lock);
311 * Do rebuild by resetting syncid and disconnecting disk.
312 * It'll be retasted, connected to the device and synchronized.
314 disk->d_sync.ds_syncid = 0;
315 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0)
316 disk->d_flags |= G_RAID3_DISK_FLAG_FORCE_SYNC;
317 g_raid3_update_metadata(disk);
318 pp = disk->d_consumer->provider;
320 error = g_raid3_read_metadata(disk->d_consumer, &md);
322 g_raid3_event_send(disk, G_RAID3_DISK_STATE_DISCONNECTED,
325 gctl_error(req, "Cannot read metadata from %s.", pp->name);
326 sx_xunlock(&sc->sc_lock);
329 error = g_raid3_add_disk(sc, pp, &md);
331 gctl_error(req, "Cannot reconnect component %s.", pp->name);
332 sx_xunlock(&sc->sc_lock);
336 g_raid3_ctl_stop(struct gctl_req *req, struct g_class *mp)
338 struct g_raid3_softc *sc;
339 int *force, *nargs, error;
345 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
347 gctl_error(req, "No '%s' argument.", "nargs");
351 gctl_error(req, "Missing device(s).");
354 force = gctl_get_paraml(req, "force", sizeof(*force));
356 gctl_error(req, "No '%s' argument.", "force");
360 how = G_RAID3_DESTROY_HARD;
362 how = G_RAID3_DESTROY_SOFT;
364 for (i = 0; i < (u_int)*nargs; i++) {
365 snprintf(param, sizeof(param), "arg%u", i);
366 name = gctl_get_asciiparam(req, param);
368 gctl_error(req, "No 'arg%u' argument.", i);
371 sc = g_raid3_find_device(mp, name);
373 gctl_error(req, "No such device: %s.", name);
377 error = g_raid3_destroy(sc, how);
379 gctl_error(req, "Cannot destroy device %s (error=%d).",
380 sc->sc_geom->name, error);
381 sx_xunlock(&sc->sc_lock);
384 /* No need to unlock, because lock is already dead. */
389 g_raid3_ctl_insert_orphan(struct g_consumer *cp)
392 KASSERT(1 == 0, ("%s called while inserting %s.", __func__,
393 cp->provider->name));
397 g_raid3_ctl_insert(struct gctl_req *req, struct g_class *mp)
399 struct g_raid3_metadata md;
400 struct g_raid3_softc *sc;
401 struct g_raid3_disk *disk;
403 struct g_provider *pp;
404 struct g_consumer *cp;
409 int *hardcode, *nargs, error, autono;
411 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
413 gctl_error(req, "No '%s' argument.", "nargs");
417 gctl_error(req, "Invalid number of arguments.");
420 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
421 if (hardcode == NULL) {
422 gctl_error(req, "No '%s' argument.", "hardcode");
425 name = gctl_get_asciiparam(req, "arg1");
427 gctl_error(req, "No 'arg%u' argument.", 1);
430 if (gctl_get_param(req, "number", NULL) != NULL)
431 no = gctl_get_paraml(req, "number", sizeof(*no));
434 if (strncmp(name, "/dev/", 5) == 0)
437 pp = g_provider_by_name(name);
440 gctl_error(req, "Invalid provider.");
443 gp = g_new_geomf(mp, "raid3:insert");
444 gp->orphan = g_raid3_ctl_insert_orphan;
445 cp = g_new_consumer(gp);
446 error = g_attach(cp, pp);
449 gctl_error(req, "Cannot attach to %s.", pp->name);
452 error = g_access(cp, 0, 1, 1);
455 gctl_error(req, "Cannot access %s.", pp->name);
459 name = gctl_get_asciiparam(req, "arg0");
461 gctl_error(req, "No 'arg%u' argument.", 0);
464 sc = g_raid3_find_device(mp, name);
466 gctl_error(req, "No such device: %s.", name);
470 if (*no < 0 || *no >= sc->sc_ndisks) {
471 sx_xunlock(&sc->sc_lock);
472 gctl_error(req, "Invalid component number.");
475 disk = &sc->sc_disks[*no];
476 if (disk->d_state != G_RAID3_DISK_STATE_NODISK) {
477 sx_xunlock(&sc->sc_lock);
478 gctl_error(req, "Component %jd is already connected.",
484 for (autono = 0; autono < sc->sc_ndisks && disk == NULL; autono++)
485 if (sc->sc_disks[autono].d_state ==
486 G_RAID3_DISK_STATE_NODISK)
487 disk = &sc->sc_disks[autono];
489 sx_xunlock(&sc->sc_lock);
490 gctl_error(req, "No disconnected components.");
494 if (((sc->sc_sectorsize / (sc->sc_ndisks - 1)) % pp->sectorsize) != 0) {
495 sx_xunlock(&sc->sc_lock);
497 "Cannot insert provider %s, because of its sector size.",
501 compsize = sc->sc_mediasize / (sc->sc_ndisks - 1);
502 if (compsize > pp->mediasize - pp->sectorsize) {
503 sx_xunlock(&sc->sc_lock);
504 gctl_error(req, "Provider %s too small.", pp->name);
507 if (compsize < pp->mediasize - pp->sectorsize) {
509 "warning: %s: only %jd bytes from %jd bytes used.",
510 pp->name, (intmax_t)compsize,
511 (intmax_t)(pp->mediasize - pp->sectorsize));
513 g_raid3_fill_metadata(disk, &md);
514 sx_xunlock(&sc->sc_lock);
518 strlcpy(md.md_provider, pp->name, sizeof(md.md_provider));
520 bzero(md.md_provider, sizeof(md.md_provider));
521 md.md_provsize = pp->mediasize;
522 sector = g_malloc(pp->sectorsize, M_WAITOK);
523 raid3_metadata_encode(&md, sector);
524 error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector,
528 gctl_error(req, "Cannot store metadata on %s.", pp->name);
532 g_access(cp, 0, -1, -1);
533 if (cp->provider != NULL)
535 g_destroy_consumer(cp);
541 g_raid3_ctl_remove(struct gctl_req *req, struct g_class *mp)
543 struct g_raid3_softc *sc;
544 struct g_raid3_disk *disk;
549 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
551 gctl_error(req, "No '%s' argument.", "nargs");
555 gctl_error(req, "Invalid number of arguments.");
558 no = gctl_get_paraml(req, "number", sizeof(*no));
560 gctl_error(req, "No '%s' argument.", "no");
563 name = gctl_get_asciiparam(req, "arg0");
565 gctl_error(req, "No 'arg%u' argument.", 0);
568 sc = g_raid3_find_device(mp, name);
570 gctl_error(req, "No such device: %s.", name);
573 if (*no >= sc->sc_ndisks) {
574 sx_xunlock(&sc->sc_lock);
575 gctl_error(req, "Invalid component number.");
578 disk = &sc->sc_disks[*no];
579 switch (disk->d_state) {
580 case G_RAID3_DISK_STATE_ACTIVE:
582 * When replacing ACTIVE component, all the rest has to be also
585 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) <
587 gctl_error(req, "Cannot replace component number %jd.",
592 case G_RAID3_DISK_STATE_STALE:
593 case G_RAID3_DISK_STATE_SYNCHRONIZING:
594 if (g_raid3_clear_metadata(disk) != 0) {
595 gctl_error(req, "Cannot clear metadata on %s.",
596 g_raid3_get_diskname(disk));
598 g_raid3_event_send(disk,
599 G_RAID3_DISK_STATE_DISCONNECTED,
600 G_RAID3_EVENT_DONTWAIT);
603 case G_RAID3_DISK_STATE_NODISK:
606 gctl_error(req, "Cannot replace component number %jd.", *no);
609 sx_xunlock(&sc->sc_lock);
613 g_raid3_config(struct gctl_req *req, struct g_class *mp, const char *verb)
619 version = gctl_get_paraml(req, "version", sizeof(*version));
620 if (version == NULL) {
621 gctl_error(req, "No '%s' argument.", "version");
624 if (*version != G_RAID3_VERSION) {
625 gctl_error(req, "Userland and kernel parts are out of sync.");
630 if (strcmp(verb, "configure") == 0)
631 g_raid3_ctl_configure(req, mp);
632 else if (strcmp(verb, "insert") == 0)
633 g_raid3_ctl_insert(req, mp);
634 else if (strcmp(verb, "rebuild") == 0)
635 g_raid3_ctl_rebuild(req, mp);
636 else if (strcmp(verb, "remove") == 0)
637 g_raid3_ctl_remove(req, mp);
638 else if (strcmp(verb, "stop") == 0)
639 g_raid3_ctl_stop(req, mp);
641 gctl_error(req, "Unknown verb.");