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>
49 static struct g_raid3_softc *
50 g_raid3_find_device(struct g_class *mp, const char *name)
52 struct g_raid3_softc *sc;
56 LIST_FOREACH(gp, &mp->geom, geom) {
60 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0)
62 if (strcmp(gp->name, name) == 0 ||
63 strcmp(sc->sc_name, name) == 0) {
65 sx_xlock(&sc->sc_lock);
73 static struct g_raid3_disk *
74 g_raid3_find_disk(struct g_raid3_softc *sc, const char *name)
76 struct g_raid3_disk *disk;
79 sx_assert(&sc->sc_lock, SX_XLOCKED);
80 if (strncmp(name, _PATH_DEV, 5) == 0)
82 for (n = 0; n < sc->sc_ndisks; n++) {
83 disk = &sc->sc_disks[n];
84 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
86 if (disk->d_consumer == NULL)
88 if (disk->d_consumer->provider == NULL)
90 if (strcmp(disk->d_consumer->provider->name, name) == 0)
97 g_raid3_ctl_configure(struct gctl_req *req, struct g_class *mp)
99 struct g_raid3_softc *sc;
100 struct g_raid3_disk *disk;
102 int *nargs, do_sync = 0, dirty = 1;
103 int *autosync, *noautosync;
104 int *failsync, *nofailsync;
105 int *round_robin, *noround_robin;
106 int *verify, *noverify;
109 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
111 gctl_error(req, "No '%s' argument.", "nargs");
115 gctl_error(req, "Invalid number of arguments.");
118 autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
119 if (autosync == NULL) {
120 gctl_error(req, "No '%s' argument.", "autosync");
123 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
124 if (noautosync == NULL) {
125 gctl_error(req, "No '%s' argument.", "noautosync");
128 if (*autosync && *noautosync) {
129 gctl_error(req, "'%s' and '%s' specified.", "autosync",
133 failsync = gctl_get_paraml(req, "failsync", sizeof(*failsync));
134 if (failsync == NULL) {
135 gctl_error(req, "No '%s' argument.", "failsync");
138 nofailsync = gctl_get_paraml(req, "nofailsync", sizeof(*nofailsync));
139 if (nofailsync == NULL) {
140 gctl_error(req, "No '%s' argument.", "nofailsync");
143 if (*failsync && *nofailsync) {
144 gctl_error(req, "'%s' and '%s' specified.", "failsync",
148 round_robin = gctl_get_paraml(req, "round_robin", sizeof(*round_robin));
149 if (round_robin == NULL) {
150 gctl_error(req, "No '%s' argument.", "round_robin");
153 noround_robin = gctl_get_paraml(req, "noround_robin",
154 sizeof(*noround_robin));
155 if (noround_robin == NULL) {
156 gctl_error(req, "No '%s' argument.", "noround_robin");
159 if (*round_robin && *noround_robin) {
160 gctl_error(req, "'%s' and '%s' specified.", "round_robin",
164 verify = gctl_get_paraml(req, "verify", sizeof(*verify));
165 if (verify == NULL) {
166 gctl_error(req, "No '%s' argument.", "verify");
169 noverify = gctl_get_paraml(req, "noverify", sizeof(*noverify));
170 if (noverify == NULL) {
171 gctl_error(req, "No '%s' argument.", "noverify");
174 if (*verify && *noverify) {
175 gctl_error(req, "'%s' and '%s' specified.", "verify",
179 if (!*autosync && !*noautosync && !*failsync && !*nofailsync &&
180 !*round_robin && !*noround_robin && !*verify && !*noverify) {
181 gctl_error(req, "Nothing has changed.");
184 name = gctl_get_asciiparam(req, "arg0");
186 gctl_error(req, "No 'arg%u' argument.", 0);
189 sc = g_raid3_find_device(mp, name);
191 gctl_error(req, "No such device: %s.", name);
194 if (g_raid3_ndisks(sc, -1) < sc->sc_ndisks) {
195 gctl_error(req, "Not all disks connected.");
196 sx_xunlock(&sc->sc_lock);
199 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0) {
201 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
206 sc->sc_flags |= G_RAID3_DEVICE_FLAG_NOAUTOSYNC;
208 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0) {
210 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_NOFAILSYNC;
213 sc->sc_flags |= G_RAID3_DEVICE_FLAG_NOFAILSYNC;
217 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0) {
219 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_VERIFY;
222 sc->sc_flags |= G_RAID3_DEVICE_FLAG_VERIFY;
224 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
226 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
229 sc->sc_flags |= G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
231 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
232 (sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
234 * VERIFY and ROUND-ROBIN options are mutally exclusive.
236 sc->sc_flags &= ~G_RAID3_DEVICE_FLAG_ROUND_ROBIN;
238 for (n = 0; n < sc->sc_ndisks; n++) {
239 disk = &sc->sc_disks[n];
241 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING)
242 disk->d_flags &= ~G_RAID3_DISK_FLAG_FORCE_SYNC;
245 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
246 g_raid3_update_metadata(disk);
248 if (disk->d_state == G_RAID3_DISK_STATE_STALE) {
250 * XXX: This is probably possible that this
251 * component will not be retasted.
253 g_raid3_event_send(disk,
254 G_RAID3_DISK_STATE_DISCONNECTED,
255 G_RAID3_EVENT_DONTWAIT);
259 sx_xunlock(&sc->sc_lock);
263 g_raid3_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
265 struct g_raid3_metadata md;
266 struct g_raid3_softc *sc;
267 struct g_raid3_disk *disk;
268 struct g_provider *pp;
272 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
274 gctl_error(req, "No '%s' argument.", "nargs");
278 gctl_error(req, "Invalid number of arguments.");
281 name = gctl_get_asciiparam(req, "arg0");
283 gctl_error(req, "No 'arg%u' argument.", 0);
286 sc = g_raid3_find_device(mp, name);
288 gctl_error(req, "No such device: %s.", name);
291 name = gctl_get_asciiparam(req, "arg1");
293 gctl_error(req, "No 'arg%u' argument.", 1);
294 sx_xunlock(&sc->sc_lock);
297 disk = g_raid3_find_disk(sc, name);
299 gctl_error(req, "No such provider: %s.", name);
300 sx_xunlock(&sc->sc_lock);
303 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE &&
304 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) < sc->sc_ndisks) {
305 gctl_error(req, "There is one stale disk already.");
306 sx_xunlock(&sc->sc_lock);
310 * Do rebuild by resetting syncid and disconnecting disk.
311 * It'll be retasted, connected to the device and synchronized.
313 disk->d_sync.ds_syncid = 0;
314 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0)
315 disk->d_flags |= G_RAID3_DISK_FLAG_FORCE_SYNC;
316 g_raid3_update_metadata(disk);
317 pp = disk->d_consumer->provider;
319 error = g_raid3_read_metadata(disk->d_consumer, &md);
321 g_raid3_event_send(disk, G_RAID3_DISK_STATE_DISCONNECTED,
324 gctl_error(req, "Cannot read metadata from %s.", pp->name);
325 sx_xunlock(&sc->sc_lock);
328 error = g_raid3_add_disk(sc, pp, &md);
330 gctl_error(req, "Cannot reconnect component %s.", pp->name);
331 sx_xunlock(&sc->sc_lock);
335 g_raid3_ctl_stop(struct gctl_req *req, struct g_class *mp)
337 struct g_raid3_softc *sc;
338 int *force, *nargs, error;
344 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
346 gctl_error(req, "No '%s' argument.", "nargs");
350 gctl_error(req, "Missing device(s).");
353 force = gctl_get_paraml(req, "force", sizeof(*force));
355 gctl_error(req, "No '%s' argument.", "force");
359 how = G_RAID3_DESTROY_HARD;
361 how = G_RAID3_DESTROY_SOFT;
363 for (i = 0; i < (u_int)*nargs; i++) {
364 snprintf(param, sizeof(param), "arg%u", i);
365 name = gctl_get_asciiparam(req, param);
367 gctl_error(req, "No 'arg%u' argument.", i);
370 sc = g_raid3_find_device(mp, name);
372 gctl_error(req, "No such device: %s.", name);
376 error = g_raid3_destroy(sc, how);
378 gctl_error(req, "Cannot destroy device %s (error=%d).",
379 sc->sc_geom->name, error);
380 sx_xunlock(&sc->sc_lock);
383 /* No need to unlock, because lock is already dead. */
388 g_raid3_ctl_insert_orphan(struct g_consumer *cp)
391 KASSERT(1 == 0, ("%s called while inserting %s.", __func__,
392 cp->provider->name));
396 g_raid3_ctl_insert(struct gctl_req *req, struct g_class *mp)
398 struct g_raid3_metadata md;
399 struct g_raid3_softc *sc;
400 struct g_raid3_disk *disk;
402 struct g_provider *pp;
403 struct g_consumer *cp;
408 int *hardcode, *nargs, error, autono;
410 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
412 gctl_error(req, "No '%s' argument.", "nargs");
416 gctl_error(req, "Invalid number of arguments.");
419 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
420 if (hardcode == NULL) {
421 gctl_error(req, "No '%s' argument.", "hardcode");
424 pp = gctl_get_provider(req, "arg1");
427 if (gctl_get_param(req, "number", NULL) != NULL)
428 no = gctl_get_paraml(req, "number", sizeof(*no));
431 gp = g_new_geomf(mp, "raid3:insert");
432 gp->orphan = g_raid3_ctl_insert_orphan;
433 cp = g_new_consumer(gp);
434 error = g_attach(cp, pp);
437 gctl_error(req, "Cannot attach to %s.", pp->name);
440 error = g_access(cp, 0, 1, 1);
443 gctl_error(req, "Cannot access %s.", pp->name);
447 name = gctl_get_asciiparam(req, "arg0");
449 gctl_error(req, "No 'arg%u' argument.", 0);
452 sc = g_raid3_find_device(mp, name);
454 gctl_error(req, "No such device: %s.", name);
458 if (*no < 0 || *no >= sc->sc_ndisks) {
459 sx_xunlock(&sc->sc_lock);
460 gctl_error(req, "Invalid component number.");
463 disk = &sc->sc_disks[*no];
464 if (disk->d_state != G_RAID3_DISK_STATE_NODISK) {
465 sx_xunlock(&sc->sc_lock);
466 gctl_error(req, "Component %jd is already connected.",
472 for (autono = 0; autono < sc->sc_ndisks && disk == NULL; autono++)
473 if (sc->sc_disks[autono].d_state ==
474 G_RAID3_DISK_STATE_NODISK)
475 disk = &sc->sc_disks[autono];
477 sx_xunlock(&sc->sc_lock);
478 gctl_error(req, "No disconnected components.");
482 if (((sc->sc_sectorsize / (sc->sc_ndisks - 1)) % pp->sectorsize) != 0) {
483 sx_xunlock(&sc->sc_lock);
485 "Cannot insert provider %s, because of its sector size.",
489 compsize = sc->sc_mediasize / (sc->sc_ndisks - 1);
490 if (compsize > pp->mediasize - pp->sectorsize) {
491 sx_xunlock(&sc->sc_lock);
492 gctl_error(req, "Provider %s too small.", pp->name);
495 if (compsize < pp->mediasize - pp->sectorsize) {
497 "warning: %s: only %jd bytes from %jd bytes used.",
498 pp->name, (intmax_t)compsize,
499 (intmax_t)(pp->mediasize - pp->sectorsize));
501 g_raid3_fill_metadata(disk, &md);
502 sx_xunlock(&sc->sc_lock);
506 strlcpy(md.md_provider, pp->name, sizeof(md.md_provider));
508 bzero(md.md_provider, sizeof(md.md_provider));
509 md.md_provsize = pp->mediasize;
510 sector = g_malloc(pp->sectorsize, M_WAITOK);
511 raid3_metadata_encode(&md, sector);
512 error = g_write_data(cp, pp->mediasize - pp->sectorsize, sector,
516 gctl_error(req, "Cannot store metadata on %s.", pp->name);
520 g_access(cp, 0, -1, -1);
521 if (cp->provider != NULL)
523 g_destroy_consumer(cp);
529 g_raid3_ctl_remove(struct gctl_req *req, struct g_class *mp)
531 struct g_raid3_softc *sc;
532 struct g_raid3_disk *disk;
537 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
539 gctl_error(req, "No '%s' argument.", "nargs");
543 gctl_error(req, "Invalid number of arguments.");
546 no = gctl_get_paraml(req, "number", sizeof(*no));
548 gctl_error(req, "No '%s' argument.", "no");
551 name = gctl_get_asciiparam(req, "arg0");
553 gctl_error(req, "No 'arg%u' argument.", 0);
556 sc = g_raid3_find_device(mp, name);
558 gctl_error(req, "No such device: %s.", name);
561 if (*no >= sc->sc_ndisks) {
562 sx_xunlock(&sc->sc_lock);
563 gctl_error(req, "Invalid component number.");
566 disk = &sc->sc_disks[*no];
567 switch (disk->d_state) {
568 case G_RAID3_DISK_STATE_ACTIVE:
570 * When replacing ACTIVE component, all the rest has to be also
573 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) <
575 gctl_error(req, "Cannot replace component number %jd.",
580 case G_RAID3_DISK_STATE_STALE:
581 case G_RAID3_DISK_STATE_SYNCHRONIZING:
582 if (g_raid3_clear_metadata(disk) != 0) {
583 gctl_error(req, "Cannot clear metadata on %s.",
584 g_raid3_get_diskname(disk));
586 g_raid3_event_send(disk,
587 G_RAID3_DISK_STATE_DISCONNECTED,
588 G_RAID3_EVENT_DONTWAIT);
591 case G_RAID3_DISK_STATE_NODISK:
594 gctl_error(req, "Cannot replace component number %jd.", *no);
597 sx_xunlock(&sc->sc_lock);
601 g_raid3_config(struct gctl_req *req, struct g_class *mp, const char *verb)
607 version = gctl_get_paraml(req, "version", sizeof(*version));
608 if (version == NULL) {
609 gctl_error(req, "No '%s' argument.", "version");
612 if (*version != G_RAID3_VERSION) {
613 gctl_error(req, "Userland and kernel parts are out of sync.");
618 if (strcmp(verb, "configure") == 0)
619 g_raid3_ctl_configure(req, mp);
620 else if (strcmp(verb, "insert") == 0)
621 g_raid3_ctl_insert(req, mp);
622 else if (strcmp(verb, "rebuild") == 0)
623 g_raid3_ctl_rebuild(req, mp);
624 else if (strcmp(verb, "remove") == 0)
625 g_raid3_ctl_remove(req, mp);
626 else if (strcmp(verb, "stop") == 0)
627 g_raid3_ctl_stop(req, mp);
629 gctl_error(req, "Unknown verb.");