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>
47 #include <sys/systm.h>
49 #include <geom/geom.h>
50 #include <geom/vinum/geom_vinum_var.h>
51 #include <geom/vinum/geom_vinum.h>
52 #include <geom/vinum/geom_vinum_share.h>
54 int gv_drive_is_newer(struct gv_softc *, struct gv_drive *);
55 static off_t gv_plex_smallest_sd(struct gv_plex *);
58 gv_parse_config(struct gv_softc *sc, char *buf, struct gv_drive *d)
60 char *aptr, *bptr, *cptr;
61 struct gv_volume *v, *v2;
62 struct gv_plex *p, *p2;
64 int error, is_newer, tokens;
65 char *token[GV_MAXARGS];
67 is_newer = gv_drive_is_newer(sc, d);
69 /* Until the end of the string *buf. */
70 for (aptr = buf; *aptr != '\0'; aptr = bptr) {
74 /* Seperate input lines. */
80 tokens = gv_tokenize(cptr, token, GV_MAXARGS);
85 if (!strcmp(token[0], "volume")) {
86 v = gv_new_volume(tokens, token);
88 G_VINUM_DEBUG(0, "config parse failed volume");
92 v2 = gv_find_vol(sc, v->name);
96 G_VINUM_DEBUG(2, "newer volume found!");
102 gv_create_volume(sc, v);
104 } else if (!strcmp(token[0], "plex")) {
105 p = gv_new_plex(tokens, token);
107 G_VINUM_DEBUG(0, "config parse failed plex");
111 p2 = gv_find_plex(sc, p->name);
115 p2->state = p->state;
116 G_VINUM_DEBUG(2, "newer plex found!");
122 error = gv_create_plex(sc, p);
126 * These flags were set in gv_create_plex() and are not
127 * needed here (on-disk config parsing).
129 p->flags &= ~GV_PLEX_ADDED;
131 } else if (!strcmp(token[0], "sd")) {
132 s = gv_new_sd(tokens, token);
135 G_VINUM_DEBUG(0, "config parse failed subdisk");
139 s2 = gv_find_sd(sc, s->name);
143 s2->state = s->state;
144 G_VINUM_DEBUG(2, "newer subdisk found!");
151 * Signal that this subdisk was tasted, and could
152 * possibly reference a drive that isn't in our config
155 s->flags |= GV_SD_TASTED;
157 if (s->state == GV_SD_UP)
158 s->flags |= GV_SD_CANGOUP;
160 error = gv_create_sd(sc, s);
165 * This flag was set in gv_create_sd() and is not
166 * needed here (on-disk config parsing).
168 s->flags &= ~GV_SD_NEWBORN;
169 s->flags &= ~GV_SD_GROW;
175 * Format the vinum configuration properly. If ondisk is non-zero then the
176 * configuration is intended to be written to disk later.
179 gv_format_config(struct gv_softc *sc, struct sbuf *sb, int ondisk, char *prefix)
187 * We don't need the drive configuration if we're not writing the
191 LIST_FOREACH(d, &sc->drives, drive) {
192 sbuf_printf(sb, "%sdrive %s device /dev/%s\n", prefix,
197 LIST_FOREACH(v, &sc->volumes, volume) {
199 sbuf_printf(sb, "%s", prefix);
200 sbuf_printf(sb, "volume %s", v->name);
202 sbuf_printf(sb, " state %s", gv_volstate(v->state));
203 sbuf_printf(sb, "\n");
206 LIST_FOREACH(p, &sc->plexes, plex) {
208 sbuf_printf(sb, "%s", prefix);
209 sbuf_printf(sb, "plex name %s org %s ", p->name,
211 if (gv_is_striped(p))
212 sbuf_printf(sb, "%ds ", p->stripesize / 512);
213 if (p->vol_sc != NULL)
214 sbuf_printf(sb, "vol %s", p->volume);
216 sbuf_printf(sb, " state %s", gv_plexstate(p->state));
217 sbuf_printf(sb, "\n");
220 LIST_FOREACH(s, &sc->subdisks, sd) {
222 sbuf_printf(sb, "%s", prefix);
223 sbuf_printf(sb, "sd name %s drive %s len %jds driveoffset "
224 "%jds", s->name, s->drive, s->size / 512,
225 s->drive_offset / 512);
226 if (s->plex_sc != NULL) {
227 sbuf_printf(sb, " plex %s plexoffset %jds", s->plex,
228 s->plex_offset / 512);
231 sbuf_printf(sb, " state %s", gv_sdstate(s->state));
232 sbuf_printf(sb, "\n");
237 gv_plex_smallest_sd(struct gv_plex *p)
242 KASSERT(p != NULL, ("gv_plex_smallest_sd: NULL p"));
244 s = LIST_FIRST(&p->subdisks);
248 LIST_FOREACH(s, &p->subdisks, in_plex) {
249 if (s->size < smallest)
255 /* Walk over plexes in a volume and count how many are down. */
257 gv_plexdown(struct gv_volume *v)
262 KASSERT(v != NULL, ("gv_plexdown: NULL v"));
266 LIST_FOREACH(p, &v->plexes, plex) {
267 if (p->state == GV_PLEX_DOWN)
274 gv_sd_to_plex(struct gv_sd *s, struct gv_plex *p)
277 off_t psizeorig, remainder, smallest;
279 /* If this subdisk was already given to this plex, do nothing. */
283 /* Check correct size of this subdisk. */
284 s2 = LIST_FIRST(&p->subdisks);
285 /* Adjust the subdisk-size if necessary. */
286 if (s2 != NULL && gv_is_striped(p)) {
287 /* First adjust to the stripesize. */
288 remainder = s->size % p->stripesize;
291 G_VINUM_DEBUG(1, "size of sd %s is not a "
292 "multiple of plex stripesize, taking off "
293 "%jd bytes", s->name,
294 (intmax_t)remainder);
295 gv_adjust_freespace(s, remainder);
298 smallest = gv_plex_smallest_sd(p);
299 /* Then take off extra if other subdisks are smaller. */
300 remainder = s->size - smallest;
303 * Don't allow a remainder below zero for running plexes, it's too
304 * painful, and if someone were to accidentally do this, the
305 * resulting array might be smaller than the original... not god
308 if (!(p->flags & GV_PLEX_NEWBORN)) {
309 G_VINUM_DEBUG(0, "sd %s too small for plex %s!",
311 return (GV_ERR_BADSIZE);
313 /* Adjust other subdisks. */
314 LIST_FOREACH(s2, &p->subdisks, in_plex) {
315 G_VINUM_DEBUG(1, "size of sd %s is to big, "
316 "taking off %jd bytes", s->name,
317 (intmax_t)remainder);
318 gv_adjust_freespace(s2, (remainder * -1));
320 } else if (remainder > 0) {
321 G_VINUM_DEBUG(1, "size of sd %s is to big, "
322 "taking off %jd bytes", s->name,
323 (intmax_t)remainder);
324 gv_adjust_freespace(s, remainder);
328 /* Find the correct plex offset for this subdisk, if needed. */
329 if (s->plex_offset == -1) {
331 * First set it to 0 to catch the case where we had a detached
332 * subdisk that didn't get any good offset.
336 LIST_FOREACH(s2, &p->subdisks, in_plex) {
337 if (gv_is_striped(p))
338 s->plex_offset = p->sdcount *
341 s->plex_offset = s2->plex_offset +
347 /* There are no subdisks for this plex yet, just insert it. */
348 if (LIST_EMPTY(&p->subdisks)) {
349 LIST_INSERT_HEAD(&p->subdisks, s, in_plex);
351 /* Insert in correct order, depending on plex_offset. */
353 LIST_FOREACH(s2, &p->subdisks, in_plex) {
354 if (s->plex_offset < s2->plex_offset) {
355 LIST_INSERT_BEFORE(s2, s, in_plex);
357 } else if (LIST_NEXT(s2, in_plex) == NULL) {
358 LIST_INSERT_AFTER(s2, s, in_plex);
365 /* Adjust the size of our plex. We check if the plex misses a subdisk,
366 * so we don't make the plex smaller than it actually should be.
369 p->size = gv_plex_size(p);
370 /* Make sure the size is not changed. */
371 if (p->sddetached > 0) {
372 if (p->size < psizeorig) {
374 /* We make sure wee need another subdisk. */
375 if (p->sddetached == 1)
380 if ((p->org == GV_PLEX_RAID5 ||
381 p->org == GV_PLEX_STRIPED) &&
382 !(p->flags & GV_PLEX_NEWBORN) &&
383 p->state == GV_PLEX_UP) {
384 s->flags |= GV_SD_GROW;
393 gv_update_vol_size(struct gv_volume *v, off_t size)
397 if (v->provider != NULL) {
399 v->provider->mediasize = size;
405 /* Return how many subdisks that constitute the original plex. */
407 gv_sdcount(struct gv_plex *p, int growing)
412 sdcount = p->sdcount;
414 LIST_FOREACH(s, &p->subdisks, in_plex) {
415 if (s->flags & GV_SD_GROW)
423 /* Calculates the plex size. */
425 gv_plex_size(struct gv_plex *p)
431 KASSERT(p != NULL, ("gv_plex_size: NULL p"));
433 /* Adjust the size of our plex. */
435 sdcount = gv_sdcount(p, 1);
438 LIST_FOREACH(s, &p->subdisks, in_plex)
441 case GV_PLEX_STRIPED:
442 s = LIST_FIRST(&p->subdisks);
443 size = ((s != NULL) ? (sdcount * s->size) : 0);
446 s = LIST_FIRST(&p->subdisks);
447 size = ((s != NULL) ? ((sdcount - 1) * s->size) : 0);
454 /* Returns the size of a volume. */
456 gv_vol_size(struct gv_volume *v)
461 KASSERT(v != NULL, ("gv_vol_size: NULL v"));
463 p = LIST_FIRST(&v->plexes);
467 minplexsize = p->size;
468 LIST_FOREACH(p, &v->plexes, in_volume) {
469 if (p->size < minplexsize) {
470 minplexsize = p->size;
473 return (minplexsize);
477 gv_update_plex_config(struct gv_plex *p)
479 struct gv_sd *s, *s2;
481 int required_sds, state;
483 KASSERT(p != NULL, ("gv_update_plex_config: NULL p"));
485 /* The plex was added to an already running volume. */
486 if (p->flags & GV_PLEX_ADDED)
487 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
490 case GV_PLEX_STRIPED:
503 if (p->sdcount < required_sds) {
504 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
508 * The subdisks in striped plexes must all have the same size.
510 s = LIST_FIRST(&p->subdisks);
511 LIST_FOREACH(s2, &p->subdisks, in_plex) {
512 if (s->size != s2->size) {
513 G_VINUM_DEBUG(0, "subdisk size mismatch %s"
514 "(%jd) <> %s (%jd)", s->name, s->size,
516 gv_set_plex_state(p, GV_PLEX_DOWN,
521 LIST_FOREACH(s, &p->subdisks, in_plex) {
522 /* Trim subdisk sizes to match the stripe size. */
523 remainder = s->size % p->stripesize;
525 G_VINUM_DEBUG(1, "size of sd %s is not a "
526 "multiple of plex stripesize, taking off "
527 "%jd bytes", s->name, (intmax_t)remainder);
528 gv_adjust_freespace(s, remainder);
533 p->size = gv_plex_size(p);
535 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
536 else if (p->org == GV_PLEX_RAID5 && p->flags & GV_PLEX_NEWBORN) {
537 LIST_FOREACH(s, &p->subdisks, in_plex)
538 gv_set_sd_state(s, GV_SD_UP, GV_SETSTATE_FORCE);
539 /* If added to a volume, we want the plex to be down. */
540 state = (p->flags & GV_PLEX_ADDED) ? GV_PLEX_DOWN : GV_PLEX_UP;
541 gv_set_plex_state(p, state, GV_SETSTATE_FORCE);
542 p->flags &= ~GV_PLEX_ADDED;
543 } else if (p->flags & GV_PLEX_ADDED) {
544 LIST_FOREACH(s, &p->subdisks, in_plex)
545 gv_set_sd_state(s, GV_SD_STALE, GV_SETSTATE_FORCE);
546 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
547 p->flags &= ~GV_PLEX_ADDED;
548 } else if (p->state == GV_PLEX_UP) {
549 LIST_FOREACH(s, &p->subdisks, in_plex) {
550 if (s->flags & GV_SD_GROW) {
551 gv_set_plex_state(p, GV_PLEX_GROWABLE,
557 /* Our plex is grown up now. */
558 p->flags &= ~GV_PLEX_NEWBORN;
562 * Give a subdisk to a drive, check and adjust several parameters, adjust
566 gv_sd_to_drive(struct gv_sd *s, struct gv_drive *d)
569 struct gv_freelist *fl, *fl2;
575 /* Shortcut for "referenced" drives. */
576 if (d->flags & GV_DRIVE_REFERENCED) {
581 /* Check if this subdisk was already given to this drive. */
582 if (s->drive_sc != NULL) {
583 if (s->drive_sc == d) {
584 if (!(s->flags & GV_SD_TASTED)) {
588 G_VINUM_DEBUG(0, "can't give sd '%s' to '%s' "
589 "(already on '%s')", s->name, d->name,
591 return (GV_ERR_ISATTACHED);
595 /* Preliminary checks. */
596 if ((s->size > d->avail) || (d->freelist_entries == 0)) {
597 G_VINUM_DEBUG(0, "not enough space on '%s' for '%s'", d->name,
599 return (GV_ERR_NOSPACE);
602 /* If no size was given for this subdisk, try to auto-size it... */
604 /* Find the largest available slot. */
605 LIST_FOREACH(fl, &d->freelist, freelist) {
606 if (fl->size < s->size)
609 s->drive_offset = fl->offset;
613 /* No good slot found? */
615 G_VINUM_DEBUG(0, "couldn't autosize '%s' on '%s'",
617 return (GV_ERR_BADSIZE);
621 * ... or check if we have a free slot that's large enough for the
626 LIST_FOREACH(fl, &d->freelist, freelist) {
627 if (fl->size < s->size)
629 /* Assign drive offset, if not given. */
630 if (s->drive_offset == -1)
631 s->drive_offset = fl->offset;
637 /* Couldn't find a good free slot. */
639 G_VINUM_DEBUG(0, "free slots to small for '%s' on '%s'",
641 return (GV_ERR_NOSPACE);
645 /* No drive offset given, try to calculate it. */
646 if (s->drive_offset == -1) {
648 /* Add offsets and sizes from other subdisks on this drive. */
649 LIST_FOREACH(s2, &d->subdisks, from_drive) {
650 s->drive_offset = s2->drive_offset + s2->size;
654 * If there are no other subdisks yet, then set the default
655 * offset to GV_DATA_START.
657 if (s->drive_offset == -1)
658 s->drive_offset = GV_DATA_START;
660 /* Check if we have a free slot at the given drive offset. */
663 LIST_FOREACH(fl, &d->freelist, freelist) {
664 /* Yes, this subdisk fits. */
665 if ((fl->offset <= s->drive_offset) &&
666 (fl->offset + fl->size >=
667 s->drive_offset + s->size)) {
674 /* Couldn't find a good free slot. */
676 G_VINUM_DEBUG(0, "given drive_offset for '%s' won't fit "
677 "on '%s'", s->name, d->name);
678 return (GV_ERR_NOSPACE);
683 * Now that all parameters are checked and set up, we can give the
684 * subdisk to the drive and adjust the freelist.
687 /* First, adjust the freelist. */
688 LIST_FOREACH(fl, &d->freelist, freelist) {
689 /* Look for the free slot that we have found before. */
693 /* The subdisk starts at the beginning of the free slot. */
694 if (fl->offset == s->drive_offset) {
695 fl->offset += s->size;
698 /* The subdisk uses the whole slot, so remove it. */
700 d->freelist_entries--;
701 LIST_REMOVE(fl, freelist);
704 * The subdisk does not start at the beginning of the free
708 tmp = fl->offset + fl->size;
709 fl->size = s->drive_offset - fl->offset;
712 * The subdisk didn't use the complete rest of the free
713 * slot, so we need to split it.
715 if (s->drive_offset + s->size != tmp) {
716 fl2 = g_malloc(sizeof(*fl2), M_WAITOK | M_ZERO);
717 fl2->offset = s->drive_offset + s->size;
718 fl2->size = tmp - fl2->offset;
719 LIST_INSERT_AFTER(fl, fl2, freelist);
720 d->freelist_entries++;
727 * This is the first subdisk on this drive, just insert it into the
730 if (LIST_EMPTY(&d->subdisks)) {
731 LIST_INSERT_HEAD(&d->subdisks, s, from_drive);
733 /* There are other subdisks, so insert this one in correct order. */
735 LIST_FOREACH(s2, &d->subdisks, from_drive) {
736 if (s->drive_offset < s2->drive_offset) {
737 LIST_INSERT_BEFORE(s2, s, from_drive);
739 } else if (LIST_NEXT(s2, from_drive) == NULL) {
740 LIST_INSERT_AFTER(s2, s, from_drive);
749 s->flags &= ~GV_SD_TASTED;
751 /* Link back from the subdisk to this drive. */
758 gv_free_sd(struct gv_sd *s)
761 struct gv_freelist *fl, *fl2;
763 KASSERT(s != NULL, ("gv_free_sd: NULL s"));
770 * First, find the free slot that's immediately before or after this
774 LIST_FOREACH(fl, &d->freelist, freelist) {
775 if (fl->offset == s->drive_offset + s->size)
777 if (fl->offset + fl->size == s->drive_offset)
781 /* If there is no free slot behind this subdisk, so create one. */
784 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
786 fl->offset = s->drive_offset;
788 if (d->freelist_entries == 0) {
789 LIST_INSERT_HEAD(&d->freelist, fl, freelist);
791 LIST_FOREACH(fl2, &d->freelist, freelist) {
792 if (fl->offset < fl2->offset) {
793 LIST_INSERT_BEFORE(fl2, fl, freelist);
795 } else if (LIST_NEXT(fl2, freelist) == NULL) {
796 LIST_INSERT_AFTER(fl2, fl, freelist);
802 d->freelist_entries++;
804 /* Expand the free slot we just found. */
807 if (fl->offset > s->drive_offset)
808 fl->offset = s->drive_offset;
816 gv_adjust_freespace(struct gv_sd *s, off_t remainder)
819 struct gv_freelist *fl, *fl2;
821 KASSERT(s != NULL, ("gv_adjust_freespace: NULL s"));
823 KASSERT(d != NULL, ("gv_adjust_freespace: NULL d"));
825 /* First, find the free slot that's immediately after this subdisk. */
827 LIST_FOREACH(fl, &d->freelist, freelist) {
828 if (fl->offset == s->drive_offset + s->size)
832 /* If there is no free slot behind this subdisk, so create one. */
835 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
836 fl->size = remainder;
837 fl->offset = s->drive_offset + s->size - remainder;
839 if (d->freelist_entries == 0) {
840 LIST_INSERT_HEAD(&d->freelist, fl, freelist);
842 LIST_FOREACH(fl2, &d->freelist, freelist) {
843 if (fl->offset < fl2->offset) {
844 LIST_INSERT_BEFORE(fl2, fl, freelist);
846 } else if (LIST_NEXT(fl2, freelist) == NULL) {
847 LIST_INSERT_AFTER(fl2, fl, freelist);
853 d->freelist_entries++;
855 /* Expand the free slot we just found. */
857 fl->offset -= remainder;
858 fl->size += remainder;
861 s->size -= remainder;
862 d->avail += remainder;
865 /* Check if the given plex is a striped one. */
867 gv_is_striped(struct gv_plex *p)
869 KASSERT(p != NULL, ("gv_is_striped: NULL p"));
871 case GV_PLEX_STRIPED:
879 /* Find a volume by name. */
881 gv_find_vol(struct gv_softc *sc, char *name)
885 LIST_FOREACH(v, &sc->volumes, volume) {
886 if (!strncmp(v->name, name, GV_MAXVOLNAME))
893 /* Find a plex by name. */
895 gv_find_plex(struct gv_softc *sc, char *name)
899 LIST_FOREACH(p, &sc->plexes, plex) {
900 if (!strncmp(p->name, name, GV_MAXPLEXNAME))
907 /* Find a subdisk by name. */
909 gv_find_sd(struct gv_softc *sc, char *name)
913 LIST_FOREACH(s, &sc->subdisks, sd) {
914 if (!strncmp(s->name, name, GV_MAXSDNAME))
921 /* Find a drive by name. */
923 gv_find_drive(struct gv_softc *sc, char *name)
927 LIST_FOREACH(d, &sc->drives, drive) {
928 if (!strncmp(d->name, name, GV_MAXDRIVENAME))
935 /* Find a drive given a device. */
937 gv_find_drive_device(struct gv_softc *sc, char *device)
941 LIST_FOREACH(d, &sc->drives, drive) {
942 if(!strcmp(d->device, device))
949 /* Check if any consumer of the given geom is open. */
951 gv_consumer_is_open(struct g_consumer *cp)
956 if (cp->acr || cp->acw || cp->ace)
963 gv_provider_is_open(struct g_provider *pp)
968 if (pp->acr || pp->acw || pp->ace)
975 * Compare the modification dates of the drives.
976 * Return 1 if a > b, 0 otherwise.
979 gv_drive_is_newer(struct gv_softc *sc, struct gv_drive *d)
982 struct timeval *a, *b;
984 KASSERT(!LIST_EMPTY(&sc->drives),
985 ("gv_is_drive_newer: empty drive list"));
987 a = &d->hdr->label.last_update;
988 LIST_FOREACH(d2, &sc->drives, drive) {
989 if ((d == d2) || (d2->state != GV_DRIVE_UP) ||
992 b = &d2->hdr->label.last_update;
993 if (timevalcmp(a, b, >))
1000 /* Return the type of object identified by string 'name'. */
1002 gv_object_type(struct gv_softc *sc, char *name)
1007 struct gv_volume *v;
1009 LIST_FOREACH(v, &sc->volumes, volume) {
1010 if (!strncmp(v->name, name, GV_MAXVOLNAME))
1011 return (GV_TYPE_VOL);
1014 LIST_FOREACH(p, &sc->plexes, plex) {
1015 if (!strncmp(p->name, name, GV_MAXPLEXNAME))
1016 return (GV_TYPE_PLEX);
1019 LIST_FOREACH(s, &sc->subdisks, sd) {
1020 if (!strncmp(s->name, name, GV_MAXSDNAME))
1021 return (GV_TYPE_SD);
1024 LIST_FOREACH(d, &sc->drives, drive) {
1025 if (!strncmp(d->name, name, GV_MAXDRIVENAME))
1026 return (GV_TYPE_DRIVE);
1029 return (GV_ERR_NOTFOUND);
1033 gv_setup_objects(struct gv_softc *sc)
1035 struct g_provider *pp;
1036 struct gv_volume *v;
1041 LIST_FOREACH(s, &sc->subdisks, sd) {
1042 d = gv_find_drive(sc, s->drive);
1044 gv_sd_to_drive(s, d);
1045 p = gv_find_plex(sc, s->plex);
1047 gv_sd_to_plex(s, p);
1048 gv_update_sd_state(s);
1051 LIST_FOREACH(p, &sc->plexes, plex) {
1052 gv_update_plex_config(p);
1053 v = gv_find_vol(sc, p->volume);
1054 if (v != NULL && p->vol_sc != v) {
1057 LIST_INSERT_HEAD(&v->plexes, p, in_volume);
1059 gv_update_plex_config(p);
1062 LIST_FOREACH(v, &sc->volumes, volume) {
1063 v->size = gv_vol_size(v);
1064 if (v->provider == NULL) {
1066 pp = g_new_providerf(sc->geom, "gvinum/%s", v->name);
1067 pp->mediasize = v->size;
1068 pp->sectorsize = 512; /* XXX */
1069 g_error_provider(pp, 0);
1072 g_topology_unlock();
1073 } else if (v->provider->mediasize != v->size) {
1075 v->provider->mediasize = v->size;
1076 g_topology_unlock();
1078 v->flags &= ~GV_VOL_NEWBORN;
1079 gv_update_vol_state(v);
1084 gv_cleanup(struct gv_softc *sc)
1086 struct gv_volume *v, *v2;
1087 struct gv_plex *p, *p2;
1088 struct gv_sd *s, *s2;
1089 struct gv_drive *d, *d2;
1090 struct gv_freelist *fl, *fl2;
1092 mtx_lock(&sc->config_mtx);
1093 LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2) {
1094 LIST_REMOVE(v, volume);
1098 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) {
1099 LIST_REMOVE(p, plex);
1105 LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2) {
1109 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) {
1110 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
1111 LIST_REMOVE(fl, freelist);
1114 LIST_REMOVE(d, drive);
1118 mtx_destroy(&sc->config_mtx);
1121 /* General 'attach' routine. */
1123 gv_attach_plex(struct gv_plex *p, struct gv_volume *v, int rename)
1126 struct gv_softc *sc;
1128 g_topology_assert();
1131 KASSERT(sc != NULL, ("NULL sc"));
1133 if (p->vol_sc != NULL) {
1134 G_VINUM_DEBUG(1, "unable to attach %s: already attached to %s",
1135 p->name, p->volume);
1136 return (GV_ERR_ISATTACHED);
1139 /* Stale all subdisks of this plex. */
1140 LIST_FOREACH(s, &p->subdisks, in_plex) {
1141 if (s->state != GV_SD_STALE)
1142 gv_set_sd_state(s, GV_SD_STALE, GV_SETSTATE_FORCE);
1144 /* Attach to volume. Make sure volume is not up and running. */
1145 if (gv_provider_is_open(v->provider)) {
1146 G_VINUM_DEBUG(1, "unable to attach %s: volume %s is busy",
1148 return (GV_ERR_ISBUSY);
1151 strlcpy(p->volume, v->name, sizeof(p->volume));
1154 snprintf(p->name, sizeof(p->name), "%s.p%d", v->name,
1157 LIST_INSERT_HEAD(&v->plexes, p, in_volume);
1159 /* Get plex up again. */
1160 gv_update_vol_size(v, gv_vol_size(v));
1161 gv_set_plex_state(p, GV_PLEX_UP, 0);
1162 gv_save_config(p->vinumconf);
1167 gv_attach_sd(struct gv_sd *s, struct gv_plex *p, off_t offset, int rename)
1172 g_topology_assert();
1174 /* If subdisk is attached, don't do it. */
1175 if (s->plex_sc != NULL) {
1176 G_VINUM_DEBUG(1, "unable to attach %s: already attached to %s",
1178 return (GV_ERR_ISATTACHED);
1181 gv_set_sd_state(s, GV_SD_STALE, GV_SETSTATE_FORCE);
1182 /* First check that this subdisk has a correct offset. If none other
1183 * starts at the same, and it's correct module stripesize, it is */
1184 if (offset != -1 && offset % p->stripesize != 0)
1185 return (GV_ERR_BADOFFSET);
1186 LIST_FOREACH(s2, &p->subdisks, in_plex) {
1187 if (s2->plex_offset == offset)
1188 return (GV_ERR_BADOFFSET);
1191 /* Attach the subdisk to the plex at given offset. */
1192 s->plex_offset = offset;
1193 strlcpy(s->plex, p->name, sizeof(s->plex));
1195 sdcount = p->sdcount;
1196 error = gv_sd_to_plex(s, p);
1199 gv_update_plex_config(p);
1202 snprintf(s->name, sizeof(s->name), "%s.s%d", s->plex,
1205 if (p->vol_sc != NULL)
1206 gv_update_vol_size(p->vol_sc, gv_vol_size(p->vol_sc));
1207 gv_save_config(p->vinumconf);
1208 /* We don't update the subdisk state since the user might have to
1209 * initiate a rebuild/sync first. */
1213 /* Detach a plex from a volume. */
1215 gv_detach_plex(struct gv_plex *p, int flags)
1217 struct gv_volume *v;
1219 g_topology_assert();
1223 G_VINUM_DEBUG(1, "unable to detach %s: already detached",
1225 return (0); /* Not an error. */
1229 * Only proceed if forced or volume inactive.
1231 if (!(flags & GV_FLAG_F) && (gv_provider_is_open(v->provider) ||
1232 p->state == GV_PLEX_UP)) {
1233 G_VINUM_DEBUG(1, "unable to detach %s: volume %s is busy",
1234 p->name, p->volume);
1235 return (GV_ERR_ISBUSY);
1238 /* Make sure someone don't read us when gone. */
1239 v->last_read_plex = NULL;
1240 LIST_REMOVE(p, in_volume);
1242 memset(p->volume, 0, GV_MAXVOLNAME);
1243 gv_update_vol_size(v, gv_vol_size(v));
1244 gv_save_config(p->vinumconf);
1248 /* Detach a subdisk from a plex. */
1250 gv_detach_sd(struct gv_sd *s, int flags)
1254 g_topology_assert();
1258 G_VINUM_DEBUG(1, "unable to detach %s: already detached",
1260 return (0); /* Not an error. */
1264 * Don't proceed if we're not forcing, and the plex is up, or degraded
1265 * with this subdisk up.
1267 if (!(flags & GV_FLAG_F) && ((p->state > GV_PLEX_DEGRADED) ||
1268 ((p->state == GV_PLEX_DEGRADED) && (s->state == GV_SD_UP)))) {
1269 G_VINUM_DEBUG(1, "unable to detach %s: plex %s is busy",
1271 return (GV_ERR_ISBUSY);
1274 LIST_REMOVE(s, in_plex);
1276 memset(s->plex, 0, GV_MAXPLEXNAME);
1278 gv_save_config(s->vinumconf);