2 * Copyright (c) 2004, 2007 Lukas Ertl
3 * Copyright (c) 2007, 2009 Ulf Lilleengen
4 * Copyright (c) 1997, 1998, 1999
5 * Nan Yang Computer Services Limited. All rights reserved.
7 * Parts written by Greg Lehey
9 * This software is distributed under the so-called ``Berkeley
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Nan Yang Computer
24 * 4. Neither the name of the Company nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * This software is provided ``as is'', and any express or implied
29 * warranties, including, but not limited to, the implied warranties of
30 * merchantability and fitness for a particular purpose are disclaimed.
31 * In no event shall the company or contributors be liable for any
32 * direct, indirect, incidental, special, exemplary, or consequential
33 * damages (including, but not limited to, procurement of substitute
34 * goods or services; loss of use, data, or profits; or business
35 * interruption) however caused and on any theory of liability, whether
36 * in contract, strict liability, or tort (including negligence or
37 * otherwise) arising in any way out of the use of this software, even if
38 * advised of the possibility of such damage.
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/malloc.h>
48 #include <sys/systm.h>
50 #include <geom/geom.h>
51 #include <geom/vinum/geom_vinum_var.h>
52 #include <geom/vinum/geom_vinum.h>
53 #include <geom/vinum/geom_vinum_share.h>
55 int gv_drive_is_newer(struct gv_softc *, struct gv_drive *);
56 static off_t gv_plex_smallest_sd(struct gv_plex *);
59 gv_parse_config(struct gv_softc *sc, char *buf, struct gv_drive *d)
61 char *aptr, *bptr, *cptr;
62 struct gv_volume *v, *v2;
63 struct gv_plex *p, *p2;
65 int error, is_newer, tokens;
66 char *token[GV_MAXARGS];
68 is_newer = gv_drive_is_newer(sc, d);
70 /* Until the end of the string *buf. */
71 for (aptr = buf; *aptr != '\0'; aptr = bptr) {
75 /* Seperate input lines. */
81 tokens = gv_tokenize(cptr, token, GV_MAXARGS);
86 if (!strcmp(token[0], "volume")) {
87 v = gv_new_volume(tokens, token);
89 G_VINUM_DEBUG(0, "config parse failed volume");
93 v2 = gv_find_vol(sc, v->name);
97 G_VINUM_DEBUG(2, "newer volume found!");
103 gv_create_volume(sc, v);
105 } else if (!strcmp(token[0], "plex")) {
106 p = gv_new_plex(tokens, token);
108 G_VINUM_DEBUG(0, "config parse failed plex");
112 p2 = gv_find_plex(sc, p->name);
116 p2->state = p->state;
117 G_VINUM_DEBUG(2, "newer plex found!");
123 error = gv_create_plex(sc, p);
127 * These flags were set in gv_create_plex() and are not
128 * needed here (on-disk config parsing).
130 p->flags &= ~GV_PLEX_ADDED;
132 } else if (!strcmp(token[0], "sd")) {
133 s = gv_new_sd(tokens, token);
136 G_VINUM_DEBUG(0, "config parse failed subdisk");
140 s2 = gv_find_sd(sc, s->name);
144 s2->state = s->state;
145 G_VINUM_DEBUG(2, "newer subdisk found!");
152 * Signal that this subdisk was tasted, and could
153 * possibly reference a drive that isn't in our config
156 s->flags |= GV_SD_TASTED;
158 if (s->state == GV_SD_UP)
159 s->flags |= GV_SD_CANGOUP;
161 error = gv_create_sd(sc, s);
166 * This flag was set in gv_create_sd() and is not
167 * needed here (on-disk config parsing).
169 s->flags &= ~GV_SD_NEWBORN;
170 s->flags &= ~GV_SD_GROW;
176 * Format the vinum configuration properly. If ondisk is non-zero then the
177 * configuration is intended to be written to disk later.
180 gv_format_config(struct gv_softc *sc, struct sbuf *sb, int ondisk, char *prefix)
188 * We don't need the drive configuration if we're not writing the
192 LIST_FOREACH(d, &sc->drives, drive) {
193 sbuf_printf(sb, "%sdrive %s device /dev/%s\n", prefix,
198 LIST_FOREACH(v, &sc->volumes, volume) {
200 sbuf_printf(sb, "%s", prefix);
201 sbuf_printf(sb, "volume %s", v->name);
203 sbuf_printf(sb, " state %s", gv_volstate(v->state));
204 sbuf_printf(sb, "\n");
207 LIST_FOREACH(p, &sc->plexes, plex) {
209 sbuf_printf(sb, "%s", prefix);
210 sbuf_printf(sb, "plex name %s org %s ", p->name,
212 if (gv_is_striped(p))
213 sbuf_printf(sb, "%ds ", p->stripesize / 512);
214 if (p->vol_sc != NULL)
215 sbuf_printf(sb, "vol %s", p->volume);
217 sbuf_printf(sb, " state %s", gv_plexstate(p->state));
218 sbuf_printf(sb, "\n");
221 LIST_FOREACH(s, &sc->subdisks, sd) {
223 sbuf_printf(sb, "%s", prefix);
224 sbuf_printf(sb, "sd name %s drive %s len %jds driveoffset "
225 "%jds", s->name, s->drive, s->size / 512,
226 s->drive_offset / 512);
227 if (s->plex_sc != NULL) {
228 sbuf_printf(sb, " plex %s plexoffset %jds", s->plex,
229 s->plex_offset / 512);
232 sbuf_printf(sb, " state %s", gv_sdstate(s->state));
233 sbuf_printf(sb, "\n");
238 gv_plex_smallest_sd(struct gv_plex *p)
243 KASSERT(p != NULL, ("gv_plex_smallest_sd: NULL p"));
245 s = LIST_FIRST(&p->subdisks);
249 LIST_FOREACH(s, &p->subdisks, in_plex) {
250 if (s->size < smallest)
256 /* Walk over plexes in a volume and count how many are down. */
258 gv_plexdown(struct gv_volume *v)
263 KASSERT(v != NULL, ("gv_plexdown: NULL v"));
267 LIST_FOREACH(p, &v->plexes, plex) {
268 if (p->state == GV_PLEX_DOWN)
275 gv_sd_to_plex(struct gv_sd *s, struct gv_plex *p)
278 off_t psizeorig, remainder, smallest;
280 /* If this subdisk was already given to this plex, do nothing. */
284 /* Check correct size of this subdisk. */
285 s2 = LIST_FIRST(&p->subdisks);
286 /* Adjust the subdisk-size if necessary. */
287 if (s2 != NULL && gv_is_striped(p)) {
288 /* First adjust to the stripesize. */
289 remainder = s->size % p->stripesize;
292 G_VINUM_DEBUG(1, "size of sd %s is not a "
293 "multiple of plex stripesize, taking off "
294 "%jd bytes", s->name,
295 (intmax_t)remainder);
296 gv_adjust_freespace(s, remainder);
299 smallest = gv_plex_smallest_sd(p);
300 /* Then take off extra if other subdisks are smaller. */
301 remainder = s->size - smallest;
304 * Don't allow a remainder below zero for running plexes, it's too
305 * painful, and if someone were to accidentally do this, the
306 * resulting array might be smaller than the original... not god
309 if (!(p->flags & GV_PLEX_NEWBORN)) {
310 G_VINUM_DEBUG(0, "sd %s too small for plex %s!",
312 return (GV_ERR_BADSIZE);
314 /* Adjust other subdisks. */
315 LIST_FOREACH(s2, &p->subdisks, in_plex) {
316 G_VINUM_DEBUG(1, "size of sd %s is to big, "
317 "taking off %jd bytes", s->name,
318 (intmax_t)remainder);
319 gv_adjust_freespace(s2, (remainder * -1));
321 } else if (remainder > 0) {
322 G_VINUM_DEBUG(1, "size of sd %s is to big, "
323 "taking off %jd bytes", s->name,
324 (intmax_t)remainder);
325 gv_adjust_freespace(s, remainder);
329 /* Find the correct plex offset for this subdisk, if needed. */
330 if (s->plex_offset == -1) {
332 * First set it to 0 to catch the case where we had a detached
333 * subdisk that didn't get any good offset.
337 LIST_FOREACH(s2, &p->subdisks, in_plex) {
338 if (gv_is_striped(p))
339 s->plex_offset = p->sdcount *
342 s->plex_offset = s2->plex_offset +
348 /* There are no subdisks for this plex yet, just insert it. */
349 if (LIST_EMPTY(&p->subdisks)) {
350 LIST_INSERT_HEAD(&p->subdisks, s, in_plex);
352 /* Insert in correct order, depending on plex_offset. */
354 LIST_FOREACH(s2, &p->subdisks, in_plex) {
355 if (s->plex_offset < s2->plex_offset) {
356 LIST_INSERT_BEFORE(s2, s, in_plex);
358 } else if (LIST_NEXT(s2, in_plex) == NULL) {
359 LIST_INSERT_AFTER(s2, s, in_plex);
366 /* Adjust the size of our plex. We check if the plex misses a subdisk,
367 * so we don't make the plex smaller than it actually should be.
370 p->size = gv_plex_size(p);
371 /* Make sure the size is not changed. */
372 if (p->sddetached > 0) {
373 if (p->size < psizeorig) {
375 /* We make sure wee need another subdisk. */
376 if (p->sddetached == 1)
381 if ((p->org == GV_PLEX_RAID5 ||
382 p->org == GV_PLEX_STRIPED) &&
383 !(p->flags & GV_PLEX_NEWBORN) &&
384 p->state == GV_PLEX_UP) {
385 s->flags |= GV_SD_GROW;
394 gv_update_vol_size(struct gv_volume *v, off_t size)
398 if (v->provider != NULL) {
400 v->provider->mediasize = size;
406 /* Return how many subdisks that constitute the original plex. */
408 gv_sdcount(struct gv_plex *p, int growing)
413 sdcount = p->sdcount;
415 LIST_FOREACH(s, &p->subdisks, in_plex) {
416 if (s->flags & GV_SD_GROW)
424 /* Calculates the plex size. */
426 gv_plex_size(struct gv_plex *p)
432 KASSERT(p != NULL, ("gv_plex_size: NULL p"));
434 /* Adjust the size of our plex. */
436 sdcount = gv_sdcount(p, 1);
439 LIST_FOREACH(s, &p->subdisks, in_plex)
442 case GV_PLEX_STRIPED:
443 s = LIST_FIRST(&p->subdisks);
444 size = ((s != NULL) ? (sdcount * s->size) : 0);
447 s = LIST_FIRST(&p->subdisks);
448 size = ((s != NULL) ? ((sdcount - 1) * s->size) : 0);
455 /* Returns the size of a volume. */
457 gv_vol_size(struct gv_volume *v)
462 KASSERT(v != NULL, ("gv_vol_size: NULL v"));
464 p = LIST_FIRST(&v->plexes);
468 minplexsize = p->size;
469 LIST_FOREACH(p, &v->plexes, in_volume) {
470 if (p->size < minplexsize) {
471 minplexsize = p->size;
474 return (minplexsize);
478 gv_update_plex_config(struct gv_plex *p)
480 struct gv_sd *s, *s2;
482 int required_sds, state;
484 KASSERT(p != NULL, ("gv_update_plex_config: NULL p"));
486 /* The plex was added to an already running volume. */
487 if (p->flags & GV_PLEX_ADDED)
488 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
491 case GV_PLEX_STRIPED:
504 if (p->sdcount < required_sds) {
505 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
509 * The subdisks in striped plexes must all have the same size.
511 s = LIST_FIRST(&p->subdisks);
512 LIST_FOREACH(s2, &p->subdisks, in_plex) {
513 if (s->size != s2->size) {
514 G_VINUM_DEBUG(0, "subdisk size mismatch %s"
515 "(%jd) <> %s (%jd)", s->name, s->size,
517 gv_set_plex_state(p, GV_PLEX_DOWN,
522 LIST_FOREACH(s, &p->subdisks, in_plex) {
523 /* Trim subdisk sizes to match the stripe size. */
524 remainder = s->size % p->stripesize;
526 G_VINUM_DEBUG(1, "size of sd %s is not a "
527 "multiple of plex stripesize, taking off "
528 "%jd bytes", s->name, (intmax_t)remainder);
529 gv_adjust_freespace(s, remainder);
534 p->size = gv_plex_size(p);
536 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
537 else if (p->org == GV_PLEX_RAID5 && p->flags & GV_PLEX_NEWBORN) {
538 LIST_FOREACH(s, &p->subdisks, in_plex)
539 gv_set_sd_state(s, GV_SD_UP, GV_SETSTATE_FORCE);
540 /* If added to a volume, we want the plex to be down. */
541 state = (p->flags & GV_PLEX_ADDED) ? GV_PLEX_DOWN : GV_PLEX_UP;
542 gv_set_plex_state(p, state, GV_SETSTATE_FORCE);
543 p->flags &= ~GV_PLEX_ADDED;
544 } else if (p->flags & GV_PLEX_ADDED) {
545 LIST_FOREACH(s, &p->subdisks, in_plex)
546 gv_set_sd_state(s, GV_SD_STALE, GV_SETSTATE_FORCE);
547 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
548 p->flags &= ~GV_PLEX_ADDED;
549 } else if (p->state == GV_PLEX_UP) {
550 LIST_FOREACH(s, &p->subdisks, in_plex) {
551 if (s->flags & GV_SD_GROW) {
552 gv_set_plex_state(p, GV_PLEX_GROWABLE,
558 /* Our plex is grown up now. */
559 p->flags &= ~GV_PLEX_NEWBORN;
563 * Give a subdisk to a drive, check and adjust several parameters, adjust
567 gv_sd_to_drive(struct gv_sd *s, struct gv_drive *d)
570 struct gv_freelist *fl, *fl2;
576 /* Shortcut for "referenced" drives. */
577 if (d->flags & GV_DRIVE_REFERENCED) {
582 /* Check if this subdisk was already given to this drive. */
583 if (s->drive_sc != NULL) {
584 if (s->drive_sc == d) {
585 if (!(s->flags & GV_SD_TASTED)) {
589 G_VINUM_DEBUG(0, "error giving subdisk '%s' to '%s' "
590 "(already on '%s')", s->name, d->name,
592 return (GV_ERR_ISATTACHED);
596 /* Preliminary checks. */
597 if ((s->size > d->avail) || (d->freelist_entries == 0)) {
598 G_VINUM_DEBUG(0, "not enough space on '%s' for '%s'", d->name,
600 return (GV_ERR_NOSPACE);
603 /* If no size was given for this subdisk, try to auto-size it... */
605 /* Find the largest available slot. */
606 LIST_FOREACH(fl, &d->freelist, freelist) {
607 if (fl->size < s->size)
610 s->drive_offset = fl->offset;
614 /* No good slot found? */
616 G_VINUM_DEBUG(0, "unable to autosize '%s' on '%s'",
618 return (GV_ERR_BADSIZE);
622 * ... or check if we have a free slot that's large enough for the
627 LIST_FOREACH(fl, &d->freelist, freelist) {
628 if (fl->size < s->size)
630 /* Assign drive offset, if not given. */
631 if (s->drive_offset == -1)
632 s->drive_offset = fl->offset;
638 /* Couldn't find a good free slot. */
640 G_VINUM_DEBUG(0, "free slots to small for '%s' on '%s'",
642 return (GV_ERR_NOSPACE);
646 /* No drive offset given, try to calculate it. */
647 if (s->drive_offset == -1) {
649 /* Add offsets and sizes from other subdisks on this drive. */
650 LIST_FOREACH(s2, &d->subdisks, from_drive) {
651 s->drive_offset = s2->drive_offset + s2->size;
655 * If there are no other subdisks yet, then set the default
656 * offset to GV_DATA_START.
658 if (s->drive_offset == -1)
659 s->drive_offset = GV_DATA_START;
661 /* Check if we have a free slot at the given drive offset. */
664 LIST_FOREACH(fl, &d->freelist, freelist) {
665 /* Yes, this subdisk fits. */
666 if ((fl->offset <= s->drive_offset) &&
667 (fl->offset + fl->size >=
668 s->drive_offset + s->size)) {
675 /* Couldn't find a good free slot. */
677 G_VINUM_DEBUG(0, "given drive_offset for '%s' won't fit "
678 "on '%s'", s->name, d->name);
679 return (GV_ERR_NOSPACE);
684 * Now that all parameters are checked and set up, we can give the
685 * subdisk to the drive and adjust the freelist.
688 /* First, adjust the freelist. */
689 LIST_FOREACH(fl, &d->freelist, freelist) {
690 /* Look for the free slot that we have found before. */
694 /* The subdisk starts at the beginning of the free slot. */
695 if (fl->offset == s->drive_offset) {
696 fl->offset += s->size;
699 /* The subdisk uses the whole slot, so remove it. */
701 d->freelist_entries--;
702 LIST_REMOVE(fl, freelist);
705 * The subdisk does not start at the beginning of the free
709 tmp = fl->offset + fl->size;
710 fl->size = s->drive_offset - fl->offset;
713 * The subdisk didn't use the complete rest of the free
714 * slot, so we need to split it.
716 if (s->drive_offset + s->size != tmp) {
717 fl2 = g_malloc(sizeof(*fl2), M_WAITOK | M_ZERO);
718 fl2->offset = s->drive_offset + s->size;
719 fl2->size = tmp - fl2->offset;
720 LIST_INSERT_AFTER(fl, fl2, freelist);
721 d->freelist_entries++;
728 * This is the first subdisk on this drive, just insert it into the
731 if (LIST_EMPTY(&d->subdisks)) {
732 LIST_INSERT_HEAD(&d->subdisks, s, from_drive);
734 /* There are other subdisks, so insert this one in correct order. */
736 LIST_FOREACH(s2, &d->subdisks, from_drive) {
737 if (s->drive_offset < s2->drive_offset) {
738 LIST_INSERT_BEFORE(s2, s, from_drive);
740 } else if (LIST_NEXT(s2, from_drive) == NULL) {
741 LIST_INSERT_AFTER(s2, s, from_drive);
750 s->flags &= ~GV_SD_TASTED;
752 /* Link back from the subdisk to this drive. */
759 gv_free_sd(struct gv_sd *s)
762 struct gv_freelist *fl, *fl2;
764 KASSERT(s != NULL, ("gv_free_sd: NULL s"));
771 * First, find the free slot that's immediately before or after this
775 LIST_FOREACH(fl, &d->freelist, freelist) {
776 if (fl->offset == s->drive_offset + s->size)
778 if (fl->offset + fl->size == s->drive_offset)
782 /* If there is no free slot behind this subdisk, so create one. */
785 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
787 fl->offset = s->drive_offset;
789 if (d->freelist_entries == 0) {
790 LIST_INSERT_HEAD(&d->freelist, fl, freelist);
792 LIST_FOREACH(fl2, &d->freelist, freelist) {
793 if (fl->offset < fl2->offset) {
794 LIST_INSERT_BEFORE(fl2, fl, freelist);
796 } else if (LIST_NEXT(fl2, freelist) == NULL) {
797 LIST_INSERT_AFTER(fl2, fl, freelist);
803 d->freelist_entries++;
805 /* Expand the free slot we just found. */
808 if (fl->offset > s->drive_offset)
809 fl->offset = s->drive_offset;
817 gv_adjust_freespace(struct gv_sd *s, off_t remainder)
820 struct gv_freelist *fl, *fl2;
822 KASSERT(s != NULL, ("gv_adjust_freespace: NULL s"));
824 KASSERT(d != NULL, ("gv_adjust_freespace: NULL d"));
826 /* First, find the free slot that's immediately after this subdisk. */
828 LIST_FOREACH(fl, &d->freelist, freelist) {
829 if (fl->offset == s->drive_offset + s->size)
833 /* If there is no free slot behind this subdisk, so create one. */
836 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
837 fl->size = remainder;
838 fl->offset = s->drive_offset + s->size - remainder;
840 if (d->freelist_entries == 0) {
841 LIST_INSERT_HEAD(&d->freelist, fl, freelist);
843 LIST_FOREACH(fl2, &d->freelist, freelist) {
844 if (fl->offset < fl2->offset) {
845 LIST_INSERT_BEFORE(fl2, fl, freelist);
847 } else if (LIST_NEXT(fl2, freelist) == NULL) {
848 LIST_INSERT_AFTER(fl2, fl, freelist);
854 d->freelist_entries++;
856 /* Expand the free slot we just found. */
858 fl->offset -= remainder;
859 fl->size += remainder;
862 s->size -= remainder;
863 d->avail += remainder;
866 /* Check if the given plex is a striped one. */
868 gv_is_striped(struct gv_plex *p)
870 KASSERT(p != NULL, ("gv_is_striped: NULL p"));
872 case GV_PLEX_STRIPED:
880 /* Find a volume by name. */
882 gv_find_vol(struct gv_softc *sc, char *name)
886 LIST_FOREACH(v, &sc->volumes, volume) {
887 if (!strncmp(v->name, name, GV_MAXVOLNAME))
894 /* Find a plex by name. */
896 gv_find_plex(struct gv_softc *sc, char *name)
900 LIST_FOREACH(p, &sc->plexes, plex) {
901 if (!strncmp(p->name, name, GV_MAXPLEXNAME))
908 /* Find a subdisk by name. */
910 gv_find_sd(struct gv_softc *sc, char *name)
914 LIST_FOREACH(s, &sc->subdisks, sd) {
915 if (!strncmp(s->name, name, GV_MAXSDNAME))
922 /* Find a drive by name. */
924 gv_find_drive(struct gv_softc *sc, char *name)
928 LIST_FOREACH(d, &sc->drives, drive) {
929 if (!strncmp(d->name, name, GV_MAXDRIVENAME))
936 /* Find a drive given a device. */
938 gv_find_drive_device(struct gv_softc *sc, char *device)
942 LIST_FOREACH(d, &sc->drives, drive) {
943 if(!strcmp(d->device, device))
950 /* Check if any consumer of the given geom is open. */
952 gv_consumer_is_open(struct g_consumer *cp)
957 if (cp->acr || cp->acw || cp->ace)
964 gv_provider_is_open(struct g_provider *pp)
969 if (pp->acr || pp->acw || pp->ace)
976 * Compare the modification dates of the drives.
977 * Return 1 if a > b, 0 otherwise.
980 gv_drive_is_newer(struct gv_softc *sc, struct gv_drive *d)
983 struct timeval *a, *b;
985 KASSERT(!LIST_EMPTY(&sc->drives),
986 ("gv_is_drive_newer: empty drive list"));
988 a = &d->hdr->label.last_update;
989 LIST_FOREACH(d2, &sc->drives, drive) {
990 if ((d == d2) || (d2->state != GV_DRIVE_UP) ||
993 b = &d2->hdr->label.last_update;
994 if (timevalcmp(a, b, >))
1001 /* Return the type of object identified by string 'name'. */
1003 gv_object_type(struct gv_softc *sc, char *name)
1008 struct gv_volume *v;
1010 LIST_FOREACH(v, &sc->volumes, volume) {
1011 if (!strncmp(v->name, name, GV_MAXVOLNAME))
1012 return (GV_TYPE_VOL);
1015 LIST_FOREACH(p, &sc->plexes, plex) {
1016 if (!strncmp(p->name, name, GV_MAXPLEXNAME))
1017 return (GV_TYPE_PLEX);
1020 LIST_FOREACH(s, &sc->subdisks, sd) {
1021 if (!strncmp(s->name, name, GV_MAXSDNAME))
1022 return (GV_TYPE_SD);
1025 LIST_FOREACH(d, &sc->drives, drive) {
1026 if (!strncmp(d->name, name, GV_MAXDRIVENAME))
1027 return (GV_TYPE_DRIVE);
1030 return (GV_ERR_NOTFOUND);
1034 gv_setup_objects(struct gv_softc *sc)
1036 struct g_provider *pp;
1037 struct gv_volume *v;
1042 LIST_FOREACH(s, &sc->subdisks, sd) {
1043 d = gv_find_drive(sc, s->drive);
1045 gv_sd_to_drive(s, d);
1046 p = gv_find_plex(sc, s->plex);
1048 gv_sd_to_plex(s, p);
1049 gv_update_sd_state(s);
1052 LIST_FOREACH(p, &sc->plexes, plex) {
1053 gv_update_plex_config(p);
1054 v = gv_find_vol(sc, p->volume);
1055 if (v != NULL && p->vol_sc != v) {
1058 LIST_INSERT_HEAD(&v->plexes, p, in_volume);
1060 gv_update_plex_config(p);
1063 LIST_FOREACH(v, &sc->volumes, volume) {
1064 v->size = gv_vol_size(v);
1065 if (v->provider == NULL) {
1067 pp = g_new_providerf(sc->geom, "gvinum/%s", v->name);
1068 pp->mediasize = v->size;
1069 pp->sectorsize = 512; /* XXX */
1070 g_error_provider(pp, 0);
1073 g_topology_unlock();
1074 } else if (v->provider->mediasize != v->size) {
1076 v->provider->mediasize = v->size;
1077 g_topology_unlock();
1079 v->flags &= ~GV_VOL_NEWBORN;
1080 gv_update_vol_state(v);
1085 gv_cleanup(struct gv_softc *sc)
1087 struct gv_volume *v, *v2;
1088 struct gv_plex *p, *p2;
1089 struct gv_sd *s, *s2;
1090 struct gv_drive *d, *d2;
1091 struct gv_freelist *fl, *fl2;
1093 mtx_lock(&sc->config_mtx);
1094 LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2) {
1095 LIST_REMOVE(v, volume);
1099 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) {
1100 LIST_REMOVE(p, plex);
1106 LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2) {
1110 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) {
1111 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
1112 LIST_REMOVE(fl, freelist);
1115 LIST_REMOVE(d, drive);
1119 mtx_destroy(&sc->config_mtx);
1122 /* General 'attach' routine. */
1124 gv_attach_plex(struct gv_plex *p, struct gv_volume *v, int rename)
1127 struct gv_softc *sc;
1129 g_topology_assert();
1132 KASSERT(sc != NULL, ("NULL sc"));
1134 if (p->vol_sc != NULL) {
1135 G_VINUM_DEBUG(1, "unable to attach %s: already attached to %s",
1136 p->name, p->volume);
1137 return (GV_ERR_ISATTACHED);
1140 /* Stale all subdisks of this plex. */
1141 LIST_FOREACH(s, &p->subdisks, in_plex) {
1142 if (s->state != GV_SD_STALE)
1143 gv_set_sd_state(s, GV_SD_STALE, GV_SETSTATE_FORCE);
1145 /* Attach to volume. Make sure volume is not up and running. */
1146 if (gv_provider_is_open(v->provider)) {
1147 G_VINUM_DEBUG(1, "unable to attach %s: volume %s is busy",
1149 return (GV_ERR_ISBUSY);
1152 strlcpy(p->volume, v->name, sizeof(p->volume));
1155 snprintf(p->name, sizeof(p->name), "%s.p%d", v->name,
1158 LIST_INSERT_HEAD(&v->plexes, p, in_volume);
1160 /* Get plex up again. */
1161 gv_update_vol_size(v, gv_vol_size(v));
1162 gv_set_plex_state(p, GV_PLEX_UP, 0);
1163 gv_save_config(p->vinumconf);
1168 gv_attach_sd(struct gv_sd *s, struct gv_plex *p, off_t offset, int rename)
1173 g_topology_assert();
1175 /* If subdisk is attached, don't do it. */
1176 if (s->plex_sc != NULL) {
1177 G_VINUM_DEBUG(1, "unable to attach %s: already attached to %s",
1179 return (GV_ERR_ISATTACHED);
1182 gv_set_sd_state(s, GV_SD_STALE, GV_SETSTATE_FORCE);
1183 /* First check that this subdisk has a correct offset. If none other
1184 * starts at the same, and it's correct module stripesize, it is */
1185 if (offset != -1 && offset % p->stripesize != 0)
1186 return (GV_ERR_BADOFFSET);
1187 LIST_FOREACH(s2, &p->subdisks, in_plex) {
1188 if (s2->plex_offset == offset)
1189 return (GV_ERR_BADOFFSET);
1192 /* Attach the subdisk to the plex at given offset. */
1193 s->plex_offset = offset;
1194 strlcpy(s->plex, p->name, sizeof(s->plex));
1196 sdcount = p->sdcount;
1197 error = gv_sd_to_plex(s, p);
1200 gv_update_plex_config(p);
1203 snprintf(s->name, sizeof(s->name), "%s.s%d", s->plex,
1206 if (p->vol_sc != NULL)
1207 gv_update_vol_size(p->vol_sc, gv_vol_size(p->vol_sc));
1208 gv_save_config(p->vinumconf);
1209 /* We don't update the subdisk state since the user might have to
1210 * initiate a rebuild/sync first. */
1214 /* Detach a plex from a volume. */
1216 gv_detach_plex(struct gv_plex *p, int flags)
1218 struct gv_volume *v;
1220 g_topology_assert();
1224 G_VINUM_DEBUG(1, "unable to detach %s: already detached",
1226 return (0); /* Not an error. */
1230 * Only proceed if forced or volume inactive.
1232 if (!(flags & GV_FLAG_F) && (gv_provider_is_open(v->provider) ||
1233 p->state == GV_PLEX_UP)) {
1234 G_VINUM_DEBUG(1, "unable to detach %s: volume %s is busy",
1235 p->name, p->volume);
1236 return (GV_ERR_ISBUSY);
1239 /* Make sure someone don't read us when gone. */
1240 v->last_read_plex = NULL;
1241 LIST_REMOVE(p, in_volume);
1243 memset(p->volume, 0, GV_MAXVOLNAME);
1244 gv_update_vol_size(v, gv_vol_size(v));
1245 gv_save_config(p->vinumconf);
1249 /* Detach a subdisk from a plex. */
1251 gv_detach_sd(struct gv_sd *s, int flags)
1255 g_topology_assert();
1259 G_VINUM_DEBUG(1, "unable to detach %s: already detached",
1261 return (0); /* Not an error. */
1265 * Don't proceed if we're not forcing, and the plex is up, or degraded
1266 * with this subdisk up.
1268 if (!(flags & GV_FLAG_F) && ((p->state > GV_PLEX_DEGRADED) ||
1269 ((p->state == GV_PLEX_DEGRADED) && (s->state == GV_SD_UP)))) {
1270 G_VINUM_DEBUG(1, "unable to detach %s: plex %s is busy",
1272 return (GV_ERR_ISBUSY);
1275 LIST_REMOVE(s, in_plex);
1277 memset(s->plex, 0, GV_MAXPLEXNAME);
1279 gv_save_config(s->vinumconf);