2 * Copyright (c) 2004, 2007 Lukas Ertl
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 AUTHOR 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 AUTHOR 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/libkern.h>
31 #include <sys/malloc.h>
33 #include <geom/geom.h>
34 #include <geom/vinum/geom_vinum_var.h>
35 #include <geom/vinum/geom_vinum.h>
36 #include <geom/vinum/geom_vinum_share.h>
39 gv_setstate(struct g_geom *gp, struct gctl_req *req)
50 obj = gctl_get_param(req, "object", NULL);
52 gctl_error(req, "no object given");
56 state = gctl_get_param(req, "state", NULL);
58 gctl_error(req, "no state given");
62 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
64 gctl_error(req, "no flags given");
68 if (*flags & GV_FLAG_F)
69 f = GV_SETSTATE_FORCE;
72 type = gv_object_type(sc, obj);
75 if (gv_volstatei(state) < 0) {
76 gctl_error(req, "invalid volume state '%s'", state);
79 v = gv_find_vol(sc, obj);
80 gv_post_event(sc, GV_EVENT_SET_VOL_STATE, v, NULL,
81 gv_volstatei(state), f);
85 if (gv_plexstatei(state) < 0) {
86 gctl_error(req, "invalid plex state '%s'", state);
89 p = gv_find_plex(sc, obj);
90 gv_post_event(sc, GV_EVENT_SET_PLEX_STATE, p, NULL,
91 gv_plexstatei(state), f);
95 if (gv_sdstatei(state) < 0) {
96 gctl_error(req, "invalid subdisk state '%s'", state);
99 s = gv_find_sd(sc, obj);
100 gv_post_event(sc, GV_EVENT_SET_SD_STATE, s, NULL,
101 gv_sdstatei(state), f);
105 if (gv_drivestatei(state) < 0) {
106 gctl_error(req, "invalid drive state '%s'", state);
109 d = gv_find_drive(sc, obj);
110 gv_post_event(sc, GV_EVENT_SET_DRIVE_STATE, d, NULL,
111 gv_drivestatei(state), f);
115 gctl_error(req, "unknown object '%s'", obj);
120 /* Update drive state; return 0 if the state changes, otherwise error. */
122 gv_set_drive_state(struct gv_drive *d, int newstate, int flags)
127 KASSERT(d != NULL, ("gv_set_drive_state: NULL d"));
131 if (newstate == oldstate)
134 /* We allow to take down an open drive only with force. */
135 if ((newstate == GV_DRIVE_DOWN) && gv_consumer_is_open(d->consumer) &&
136 (!(flags & GV_SETSTATE_FORCE)))
137 return (GV_ERR_ISBUSY);
141 if (d->state != oldstate) {
142 LIST_FOREACH(s, &d->subdisks, from_drive)
143 gv_update_sd_state(s);
146 /* Save the config back to disk. */
147 if (flags & GV_SETSTATE_CONFIG)
148 gv_save_config(d->vinumconf);
154 gv_set_sd_state(struct gv_sd *s, int newstate, int flags)
158 int oldstate, status;
160 KASSERT(s != NULL, ("gv_set_sd_state: NULL s"));
164 /* We are optimistic and assume it will work. */
167 if (newstate == oldstate)
173 * If we're attached to a plex, we won't go down without use of
176 if ((s->plex_sc != NULL) && !(flags & GV_SETSTATE_FORCE))
177 return (GV_ERR_ISATTACHED);
181 case GV_SD_INITIALIZING:
183 * Only do this if we're forced, since it usually is done
184 * internally, and then we do use the force flag.
186 if (!flags & GV_SETSTATE_FORCE)
187 return (GV_ERR_SETSTATE);
191 /* We can't bring the subdisk up if our drive is dead. */
193 if ((d == NULL) || (d->state != GV_DRIVE_UP))
194 return (GV_ERR_SETSTATE);
196 /* Check from where we want to be brought up. */
199 case GV_SD_INITIALIZING:
201 * The subdisk was initializing. We allow it to be
208 * The subdisk is currently down. We allow it to be
209 * brought up if it is not attached to a plex.
216 * If this subdisk is attached to a plex, we allow it
217 * to be brought up if the plex if it's not a RAID5
218 * plex, otherwise it's made 'stale'.
221 if (p->org != GV_PLEX_RAID5)
223 else if (s->flags & GV_SD_CANGOUP) {
224 s->flags &= ~GV_SD_CANGOUP;
226 } else if (flags & GV_SETSTATE_FORCE)
229 s->state = GV_SD_STALE;
231 status = GV_ERR_SETSTATE;
236 * A stale subdisk can be brought up only if it's part
237 * of a concat or striped plex that's the only one in a
238 * volume, or if the subdisk isn't attached to a plex.
239 * Otherwise it needs to be revived or initialized
243 if (p == NULL || flags & GV_SETSTATE_FORCE)
246 if ((p->org != GV_PLEX_RAID5 &&
247 p->vol_sc->plexcount == 1) ||
248 (p->flags & GV_PLEX_SYNCING &&
250 p->org == GV_PLEX_RAID5))
253 return (GV_ERR_SETSTATE);
256 return (GV_ERR_INVSTATE);
260 /* Other state transitions are only possible with force. */
262 if (!(flags & GV_SETSTATE_FORCE))
263 return (GV_ERR_SETSTATE);
266 /* We can change the state and do it. */
270 /* Update our plex, if we're attached to one. */
271 if (s->plex_sc != NULL)
272 gv_update_plex_state(s->plex_sc);
274 /* Save the config back to disk. */
275 if (flags & GV_SETSTATE_CONFIG)
276 gv_save_config(s->vinumconf);
282 gv_set_plex_state(struct gv_plex *p, int newstate, int flags)
285 int oldstate, plexdown;
287 KASSERT(p != NULL, ("gv_set_plex_state: NULL p"));
293 if (newstate == oldstate)
298 /* Let update_plex handle if the plex can come up */
299 gv_update_plex_state(p);
300 if (p->state != GV_PLEX_UP && !(flags & GV_SETSTATE_FORCE))
301 return (GV_ERR_SETSTATE);
306 * Set state to GV_PLEX_DOWN only if no-one is using the plex,
307 * or if the state is forced.
310 /* If the only one up, force is needed. */
311 plexdown = gv_plexdown(v);
312 if ((v->plexcount == 1 ||
313 (v->plexcount - plexdown == 1)) &&
314 ((flags & GV_SETSTATE_FORCE) == 0))
315 return (GV_ERR_SETSTATE);
319 case GV_PLEX_DEGRADED:
320 /* Only used internally, so we have to be forced. */
321 if (flags & GV_SETSTATE_FORCE)
326 /* Update our volume if we have one. */
328 gv_update_vol_state(v);
331 if (flags & GV_SETSTATE_CONFIG)
332 gv_save_config(p->vinumconf);
337 gv_set_vol_state(struct gv_volume *v, int newstate, int flags)
341 KASSERT(v != NULL, ("gv_set_vol_state: NULL v"));
345 if (newstate == oldstate)
350 /* Let update handle if the volume can come up. */
351 gv_update_vol_state(v);
352 if (v->state != GV_VOL_UP && !(flags & GV_SETSTATE_FORCE))
353 return (GV_ERR_SETSTATE);
358 * Set state to GV_VOL_DOWN only if no-one is using the volume,
359 * or if the state should be forced.
361 if (!gv_provider_is_open(v->provider) &&
362 !(flags & GV_SETSTATE_FORCE))
363 return (GV_ERR_ISBUSY);
368 if (flags & GV_SETSTATE_CONFIG)
369 gv_save_config(v->vinumconf);
373 /* Update the state of a subdisk based on its environment. */
375 gv_update_sd_state(struct gv_sd *s)
380 KASSERT(s != NULL, ("gv_update_sd_state: NULL s"));
382 KASSERT(d != NULL, ("gv_update_sd_state: NULL d"));
386 /* If our drive isn't up we cannot be up either. */
387 if (d->state != GV_DRIVE_UP) {
388 s->state = GV_SD_DOWN;
389 /* If this subdisk was just created, we assume it is good.*/
390 } else if (s->flags & GV_SD_NEWBORN) {
392 s->flags &= ~GV_SD_NEWBORN;
393 } else if (s->state != GV_SD_UP) {
394 if (s->flags & GV_SD_CANGOUP) {
396 s->flags &= ~GV_SD_CANGOUP;
398 s->state = GV_SD_STALE;
402 if (s->state != oldstate)
403 G_VINUM_DEBUG(1, "subdisk %s state change: %s -> %s", s->name,
404 gv_sdstate(oldstate), gv_sdstate(s->state));
406 /* Update the plex, if we have one. */
407 if (s->plex_sc != NULL)
408 gv_update_plex_state(s->plex_sc);
411 /* Update the state of a plex based on its environment. */
413 gv_update_plex_state(struct gv_plex *p)
419 KASSERT(p != NULL, ("gv_update_plex_state: NULL p"));
423 /* First, check the state of our subdisks. */
424 sdstates = gv_sdstatemap(p);
426 /* If all subdisks are up, our plex can be up, too. */
427 if (sdstates == GV_SD_UPSTATE)
428 p->state = GV_PLEX_UP;
430 /* One or more of our subdisks are down. */
431 else if (sdstates & GV_SD_DOWNSTATE) {
432 /* A RAID5 plex can handle one dead subdisk. */
433 if ((p->org == GV_PLEX_RAID5) && (p->sddown == 1))
434 p->state = GV_PLEX_DEGRADED;
436 p->state = GV_PLEX_DOWN;
438 /* Some of our subdisks are initializing. */
439 } else if (sdstates & GV_SD_INITSTATE) {
441 if (p->flags & GV_PLEX_SYNCING ||
442 p->flags & GV_PLEX_REBUILDING)
443 p->state = GV_PLEX_DEGRADED;
445 p->state = GV_PLEX_DOWN;
447 p->state = GV_PLEX_DOWN;
449 if (p->state == GV_PLEX_UP) {
450 LIST_FOREACH(s, &p->subdisks, in_plex) {
451 if (s->flags & GV_SD_GROW) {
452 p->state = GV_PLEX_GROWABLE;
458 if (p->state != oldstate)
459 G_VINUM_DEBUG(1, "plex %s state change: %s -> %s", p->name,
460 gv_plexstate(oldstate), gv_plexstate(p->state));
462 /* Update our volume, if we have one. */
463 if (p->vol_sc != NULL)
464 gv_update_vol_state(p->vol_sc);
467 /* Update the volume state based on its plexes. */
469 gv_update_vol_state(struct gv_volume *v)
473 KASSERT(v != NULL, ("gv_update_vol_state: NULL v"));
475 /* The volume can't be up without plexes. */
476 if (v->plexcount == 0) {
477 v->state = GV_VOL_DOWN;
481 LIST_FOREACH(p, &v->plexes, in_volume) {
482 /* One of our plexes is accessible, and so are we. */
483 if (p->state > GV_PLEX_DEGRADED) {
484 v->state = GV_VOL_UP;
487 /* We can handle a RAID5 plex with one dead subdisk as well. */
488 } else if ((p->org == GV_PLEX_RAID5) &&
489 (p->state == GV_PLEX_DEGRADED)) {
490 v->state = GV_VOL_UP;
495 /* Not one of our plexes is up, so we can't be either. */
496 v->state = GV_VOL_DOWN;
499 /* Return a state map for the subdisks of a plex. */
501 gv_sdstatemap(struct gv_plex *p)
506 KASSERT(p != NULL, ("gv_sdstatemap: NULL p"));
509 p->sddown = 0; /* No subdisks down yet. */
511 LIST_FOREACH(s, &p->subdisks, in_plex) {
515 statemap |= GV_SD_DOWNSTATE;
516 p->sddown++; /* Another unusable subdisk. */
520 statemap |= GV_SD_UPSTATE;
523 case GV_SD_INITIALIZING:
524 statemap |= GV_SD_INITSTATE;
528 statemap |= GV_SD_INITSTATE;
529 p->sddown++; /* XXX: Another unusable subdisk? */