2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2004, 2007 Lukas Ertl
5 * Copyright (c) 2007, 2009 Ulf Lilleengen
6 * Copyright (c) 1997, 1998, 1999
7 * Nan Yang Computer Services Limited. All rights reserved.
9 * Parts written by Greg Lehey
11 * This software is distributed under the so-called ``Berkeley
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by Nan Yang Computer
26 * 4. Neither the name of the Company nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * This software is provided ``as is'', and any express or implied
31 * warranties, including, but not limited to, the implied warranties of
32 * merchantability and fitness for a particular purpose are disclaimed.
33 * In no event shall the company or contributors be liable for any
34 * direct, indirect, incidental, special, exemplary, or consequential
35 * damages (including, but not limited to, procurement of substitute
36 * goods or services; loss of use, data, or profits; or business
37 * interruption) however caused and on any theory of liability, whether
38 * in contract, strict liability, or tort (including negligence or
39 * otherwise) arising in any way out of the use of this software, even if
40 * advised of the possibility of such damage.
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/malloc.h>
50 #include <sys/systm.h>
52 #include <geom/geom.h>
53 #include <geom/geom_dbg.h>
54 #include <geom/vinum/geom_vinum_var.h>
55 #include <geom/vinum/geom_vinum.h>
56 #include <geom/vinum/geom_vinum_share.h>
58 int gv_drive_is_newer(struct gv_softc *, struct gv_drive *);
59 static off_t gv_plex_smallest_sd(struct gv_plex *);
62 gv_parse_config(struct gv_softc *sc, char *buf, struct gv_drive *d)
64 char *aptr, *bptr, *cptr;
65 struct gv_volume *v, *v2;
66 struct gv_plex *p, *p2;
68 int error, is_newer, tokens;
69 char *token[GV_MAXARGS];
71 is_newer = gv_drive_is_newer(sc, d);
73 /* Until the end of the string *buf. */
74 for (aptr = buf; *aptr != '\0'; aptr = bptr) {
78 /* Separate input lines. */
84 tokens = gv_tokenize(cptr, token, GV_MAXARGS);
89 if (!strcmp(token[0], "volume")) {
90 v = gv_new_volume(tokens, token);
92 G_VINUM_DEBUG(0, "config parse failed volume");
96 v2 = gv_find_vol(sc, v->name);
100 G_VINUM_DEBUG(2, "newer volume found!");
106 gv_create_volume(sc, v);
108 } else if (!strcmp(token[0], "plex")) {
109 p = gv_new_plex(tokens, token);
111 G_VINUM_DEBUG(0, "config parse failed plex");
115 p2 = gv_find_plex(sc, p->name);
119 p2->state = p->state;
120 G_VINUM_DEBUG(2, "newer plex found!");
126 error = gv_create_plex(sc, p);
130 * These flags were set in gv_create_plex() and are not
131 * needed here (on-disk config parsing).
133 p->flags &= ~GV_PLEX_ADDED;
135 } else if (!strcmp(token[0], "sd")) {
136 s = gv_new_sd(tokens, token);
139 G_VINUM_DEBUG(0, "config parse failed subdisk");
143 s2 = gv_find_sd(sc, s->name);
147 s2->state = s->state;
148 G_VINUM_DEBUG(2, "newer subdisk found!");
155 * Signal that this subdisk was tasted, and could
156 * possibly reference a drive that isn't in our config
159 s->flags |= GV_SD_TASTED;
161 if (s->state == GV_SD_UP)
162 s->flags |= GV_SD_CANGOUP;
164 error = gv_create_sd(sc, s);
169 * This flag was set in gv_create_sd() and is not
170 * needed here (on-disk config parsing).
172 s->flags &= ~GV_SD_NEWBORN;
173 s->flags &= ~GV_SD_GROW;
179 * Format the vinum configuration properly. If ondisk is non-zero then the
180 * configuration is intended to be written to disk later.
183 gv_format_config(struct gv_softc *sc, struct sbuf *sb, int ondisk, char *prefix)
191 * We don't need the drive configuration if we're not writing the
195 LIST_FOREACH(d, &sc->drives, drive) {
196 sbuf_printf(sb, "%sdrive %s device /dev/%s\n", prefix,
201 LIST_FOREACH(v, &sc->volumes, volume) {
203 sbuf_printf(sb, "%s", prefix);
204 sbuf_printf(sb, "volume %s", v->name);
206 sbuf_printf(sb, " state %s", gv_volstate(v->state));
207 sbuf_printf(sb, "\n");
210 LIST_FOREACH(p, &sc->plexes, plex) {
212 sbuf_printf(sb, "%s", prefix);
213 sbuf_printf(sb, "plex name %s org %s ", p->name,
215 if (gv_is_striped(p))
216 sbuf_printf(sb, "%ds ", p->stripesize / 512);
217 if (p->vol_sc != NULL)
218 sbuf_printf(sb, "vol %s", p->volume);
220 sbuf_printf(sb, " state %s", gv_plexstate(p->state));
221 sbuf_printf(sb, "\n");
224 LIST_FOREACH(s, &sc->subdisks, sd) {
226 sbuf_printf(sb, "%s", prefix);
227 sbuf_printf(sb, "sd name %s drive %s len %jds driveoffset "
228 "%jds", s->name, s->drive, s->size / 512,
229 s->drive_offset / 512);
230 if (s->plex_sc != NULL) {
231 sbuf_printf(sb, " plex %s plexoffset %jds", s->plex,
232 s->plex_offset / 512);
235 sbuf_printf(sb, " state %s", gv_sdstate(s->state));
236 sbuf_printf(sb, "\n");
241 gv_plex_smallest_sd(struct gv_plex *p)
246 KASSERT(p != NULL, ("gv_plex_smallest_sd: NULL p"));
248 s = LIST_FIRST(&p->subdisks);
252 LIST_FOREACH(s, &p->subdisks, in_plex) {
253 if (s->size < smallest)
259 /* Walk over plexes in a volume and count how many are down. */
261 gv_plexdown(struct gv_volume *v)
266 KASSERT(v != NULL, ("gv_plexdown: NULL v"));
270 LIST_FOREACH(p, &v->plexes, plex) {
271 if (p->state == GV_PLEX_DOWN)
278 gv_sd_to_plex(struct gv_sd *s, struct gv_plex *p)
281 off_t psizeorig, remainder, smallest;
283 /* If this subdisk was already given to this plex, do nothing. */
287 /* Check correct size of this subdisk. */
288 s2 = LIST_FIRST(&p->subdisks);
289 /* Adjust the subdisk-size if necessary. */
290 if (s2 != NULL && gv_is_striped(p)) {
291 /* First adjust to the stripesize. */
292 remainder = s->size % p->stripesize;
295 G_VINUM_DEBUG(1, "size of sd %s is not a "
296 "multiple of plex stripesize, taking off "
297 "%jd bytes", s->name,
298 (intmax_t)remainder);
299 gv_adjust_freespace(s, remainder);
302 smallest = gv_plex_smallest_sd(p);
303 /* Then take off extra if other subdisks are smaller. */
304 remainder = s->size - smallest;
307 * Don't allow a remainder below zero for running plexes, it's too
308 * painful, and if someone were to accidentally do this, the
309 * resulting array might be smaller than the original... not god
312 if (!(p->flags & GV_PLEX_NEWBORN)) {
313 G_VINUM_DEBUG(0, "sd %s too small for plex %s!",
315 return (GV_ERR_BADSIZE);
317 /* Adjust other subdisks. */
318 LIST_FOREACH(s2, &p->subdisks, in_plex) {
319 G_VINUM_DEBUG(1, "size of sd %s is to big, "
320 "taking off %jd bytes", s->name,
321 (intmax_t)remainder);
322 gv_adjust_freespace(s2, (remainder * -1));
324 } else if (remainder > 0) {
325 G_VINUM_DEBUG(1, "size of sd %s is to big, "
326 "taking off %jd bytes", s->name,
327 (intmax_t)remainder);
328 gv_adjust_freespace(s, remainder);
332 /* Find the correct plex offset for this subdisk, if needed. */
333 if (s->plex_offset == -1) {
335 * First set it to 0 to catch the case where we had a detached
336 * subdisk that didn't get any good offset.
340 LIST_FOREACH(s2, &p->subdisks, in_plex) {
341 if (gv_is_striped(p))
342 s->plex_offset = p->sdcount *
345 s->plex_offset = s2->plex_offset +
351 /* There are no subdisks for this plex yet, just insert it. */
352 if (LIST_EMPTY(&p->subdisks)) {
353 LIST_INSERT_HEAD(&p->subdisks, s, in_plex);
355 /* Insert in correct order, depending on plex_offset. */
357 LIST_FOREACH(s2, &p->subdisks, in_plex) {
358 if (s->plex_offset < s2->plex_offset) {
359 LIST_INSERT_BEFORE(s2, s, in_plex);
361 } else if (LIST_NEXT(s2, in_plex) == NULL) {
362 LIST_INSERT_AFTER(s2, s, in_plex);
369 /* Adjust the size of our plex. We check if the plex misses a subdisk,
370 * so we don't make the plex smaller than it actually should be.
373 p->size = gv_plex_size(p);
374 /* Make sure the size is not changed. */
375 if (p->sddetached > 0) {
376 if (p->size < psizeorig) {
378 /* We make sure wee need another subdisk. */
379 if (p->sddetached == 1)
384 if ((p->org == GV_PLEX_RAID5 ||
385 p->org == GV_PLEX_STRIPED) &&
386 !(p->flags & GV_PLEX_NEWBORN) &&
387 p->state == GV_PLEX_UP) {
388 s->flags |= GV_SD_GROW;
397 gv_update_vol_size(struct gv_volume *v, off_t size)
401 if (v->provider != NULL) {
403 v->provider->mediasize = size;
409 /* Return how many subdisks that constitute the original plex. */
411 gv_sdcount(struct gv_plex *p, int growing)
416 sdcount = p->sdcount;
418 LIST_FOREACH(s, &p->subdisks, in_plex) {
419 if (s->flags & GV_SD_GROW)
427 /* Calculates the plex size. */
429 gv_plex_size(struct gv_plex *p)
435 KASSERT(p != NULL, ("gv_plex_size: NULL p"));
437 /* Adjust the size of our plex. */
439 sdcount = gv_sdcount(p, 1);
442 LIST_FOREACH(s, &p->subdisks, in_plex)
445 case GV_PLEX_STRIPED:
446 s = LIST_FIRST(&p->subdisks);
447 size = ((s != NULL) ? (sdcount * s->size) : 0);
450 s = LIST_FIRST(&p->subdisks);
451 size = ((s != NULL) ? ((sdcount - 1) * s->size) : 0);
458 /* Returns the size of a volume. */
460 gv_vol_size(struct gv_volume *v)
465 KASSERT(v != NULL, ("gv_vol_size: NULL v"));
467 p = LIST_FIRST(&v->plexes);
471 minplexsize = p->size;
472 LIST_FOREACH(p, &v->plexes, in_volume) {
473 if (p->size < minplexsize) {
474 minplexsize = p->size;
477 return (minplexsize);
481 gv_update_plex_config(struct gv_plex *p)
483 struct gv_sd *s, *s2;
485 int required_sds, state;
487 KASSERT(p != NULL, ("gv_update_plex_config: NULL p"));
489 /* The plex was added to an already running volume. */
490 if (p->flags & GV_PLEX_ADDED)
491 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
494 case GV_PLEX_STRIPED:
507 if (p->sdcount < required_sds) {
508 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
512 * The subdisks in striped plexes must all have the same size.
514 s = LIST_FIRST(&p->subdisks);
515 LIST_FOREACH(s2, &p->subdisks, in_plex) {
516 if (s->size != s2->size) {
517 G_VINUM_DEBUG(0, "subdisk size mismatch %s"
518 "(%jd) <> %s (%jd)", s->name, s->size,
520 gv_set_plex_state(p, GV_PLEX_DOWN,
525 LIST_FOREACH(s, &p->subdisks, in_plex) {
526 /* Trim subdisk sizes to match the stripe size. */
527 remainder = s->size % p->stripesize;
529 G_VINUM_DEBUG(1, "size of sd %s is not a "
530 "multiple of plex stripesize, taking off "
531 "%jd bytes", s->name, (intmax_t)remainder);
532 gv_adjust_freespace(s, remainder);
537 p->size = gv_plex_size(p);
539 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
540 else if (p->org == GV_PLEX_RAID5 && p->flags & GV_PLEX_NEWBORN) {
541 LIST_FOREACH(s, &p->subdisks, in_plex)
542 gv_set_sd_state(s, GV_SD_UP, GV_SETSTATE_FORCE);
543 /* If added to a volume, we want the plex to be down. */
544 state = (p->flags & GV_PLEX_ADDED) ? GV_PLEX_DOWN : GV_PLEX_UP;
545 gv_set_plex_state(p, state, GV_SETSTATE_FORCE);
546 p->flags &= ~GV_PLEX_ADDED;
547 } else if (p->flags & GV_PLEX_ADDED) {
548 LIST_FOREACH(s, &p->subdisks, in_plex)
549 gv_set_sd_state(s, GV_SD_STALE, GV_SETSTATE_FORCE);
550 gv_set_plex_state(p, GV_PLEX_DOWN, GV_SETSTATE_FORCE);
551 p->flags &= ~GV_PLEX_ADDED;
552 } else if (p->state == GV_PLEX_UP) {
553 LIST_FOREACH(s, &p->subdisks, in_plex) {
554 if (s->flags & GV_SD_GROW) {
555 gv_set_plex_state(p, GV_PLEX_GROWABLE,
561 /* Our plex is grown up now. */
562 p->flags &= ~GV_PLEX_NEWBORN;
566 * Give a subdisk to a drive, check and adjust several parameters, adjust
570 gv_sd_to_drive(struct gv_sd *s, struct gv_drive *d)
573 struct gv_freelist *fl, *fl2;
579 /* Shortcut for "referenced" drives. */
580 if (d->flags & GV_DRIVE_REFERENCED) {
585 /* Check if this subdisk was already given to this drive. */
586 if (s->drive_sc != NULL) {
587 if (s->drive_sc == d) {
588 if (!(s->flags & GV_SD_TASTED)) {
592 G_VINUM_DEBUG(0, "error giving subdisk '%s' to '%s' "
593 "(already on '%s')", s->name, d->name,
595 return (GV_ERR_ISATTACHED);
599 /* Preliminary checks. */
600 if ((s->size > d->avail) || (d->freelist_entries == 0)) {
601 G_VINUM_DEBUG(0, "not enough space on '%s' for '%s'", d->name,
603 return (GV_ERR_NOSPACE);
606 /* If no size was given for this subdisk, try to auto-size it... */
608 /* Find the largest available slot. */
609 LIST_FOREACH(fl, &d->freelist, freelist) {
610 if (fl->size < s->size)
613 s->drive_offset = fl->offset;
617 /* No good slot found? */
619 G_VINUM_DEBUG(0, "unable to autosize '%s' on '%s'",
621 return (GV_ERR_BADSIZE);
625 * ... or check if we have a free slot that's large enough for the
630 LIST_FOREACH(fl, &d->freelist, freelist) {
631 if (fl->size < s->size)
633 /* Assign drive offset, if not given. */
634 if (s->drive_offset == -1)
635 s->drive_offset = fl->offset;
641 /* Couldn't find a good free slot. */
643 G_VINUM_DEBUG(0, "free slots to small for '%s' on '%s'",
645 return (GV_ERR_NOSPACE);
649 /* No drive offset given, try to calculate it. */
650 if (s->drive_offset == -1) {
651 /* Add offsets and sizes from other subdisks on this drive. */
652 LIST_FOREACH(s2, &d->subdisks, from_drive) {
653 s->drive_offset = s2->drive_offset + s2->size;
657 * If there are no other subdisks yet, then set the default
658 * offset to GV_DATA_START.
660 if (s->drive_offset == -1)
661 s->drive_offset = GV_DATA_START;
663 /* Check if we have a free slot at the given drive offset. */
666 LIST_FOREACH(fl, &d->freelist, freelist) {
667 /* Yes, this subdisk fits. */
668 if ((fl->offset <= s->drive_offset) &&
669 (fl->offset + fl->size >=
670 s->drive_offset + s->size)) {
677 /* Couldn't find a good free slot. */
679 G_VINUM_DEBUG(0, "given drive_offset for '%s' won't fit "
680 "on '%s'", s->name, d->name);
681 return (GV_ERR_NOSPACE);
686 * Now that all parameters are checked and set up, we can give the
687 * subdisk to the drive and adjust the freelist.
690 /* First, adjust the freelist. */
691 LIST_FOREACH(fl, &d->freelist, freelist) {
692 /* Look for the free slot that we have found before. */
696 /* The subdisk starts at the beginning of the free slot. */
697 if (fl->offset == s->drive_offset) {
698 fl->offset += s->size;
701 /* The subdisk uses the whole slot, so remove it. */
703 d->freelist_entries--;
704 LIST_REMOVE(fl, freelist);
707 * The subdisk does not start at the beginning of the free
711 tmp = fl->offset + fl->size;
712 fl->size = s->drive_offset - fl->offset;
715 * The subdisk didn't use the complete rest of the free
716 * slot, so we need to split it.
718 if (s->drive_offset + s->size != tmp) {
719 fl2 = g_malloc(sizeof(*fl2), M_WAITOK | M_ZERO);
720 fl2->offset = s->drive_offset + s->size;
721 fl2->size = tmp - fl2->offset;
722 LIST_INSERT_AFTER(fl, fl2, freelist);
723 d->freelist_entries++;
730 * This is the first subdisk on this drive, just insert it into the
733 if (LIST_EMPTY(&d->subdisks)) {
734 LIST_INSERT_HEAD(&d->subdisks, s, from_drive);
736 /* There are other subdisks, so insert this one in correct order. */
738 LIST_FOREACH(s2, &d->subdisks, from_drive) {
739 if (s->drive_offset < s2->drive_offset) {
740 LIST_INSERT_BEFORE(s2, s, from_drive);
742 } else if (LIST_NEXT(s2, from_drive) == NULL) {
743 LIST_INSERT_AFTER(s2, s, from_drive);
752 s->flags &= ~GV_SD_TASTED;
754 /* Link back from the subdisk to this drive. */
761 gv_free_sd(struct gv_sd *s)
764 struct gv_freelist *fl, *fl2;
766 KASSERT(s != NULL, ("gv_free_sd: NULL s"));
773 * First, find the free slot that's immediately before or after this
777 LIST_FOREACH(fl, &d->freelist, freelist) {
778 if (fl->offset == s->drive_offset + s->size)
780 if (fl->offset + fl->size == s->drive_offset)
784 /* If there is no free slot behind this subdisk, so create one. */
786 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO);
788 fl->offset = s->drive_offset;
790 if (d->freelist_entries == 0) {
791 LIST_INSERT_HEAD(&d->freelist, fl, freelist);
793 LIST_FOREACH(fl2, &d->freelist, freelist) {
794 if (fl->offset < fl2->offset) {
795 LIST_INSERT_BEFORE(fl2, fl, freelist);
797 } else if (LIST_NEXT(fl2, freelist) == NULL) {
798 LIST_INSERT_AFTER(fl2, fl, freelist);
804 d->freelist_entries++;
806 /* Expand the free slot we just found. */
809 if (fl->offset > s->drive_offset)
810 fl->offset = s->drive_offset;
818 gv_adjust_freespace(struct gv_sd *s, off_t remainder)
821 struct gv_freelist *fl, *fl2;
823 KASSERT(s != NULL, ("gv_adjust_freespace: NULL s"));
825 KASSERT(d != NULL, ("gv_adjust_freespace: NULL d"));
827 /* First, find the free slot that's immediately after this subdisk. */
829 LIST_FOREACH(fl, &d->freelist, freelist) {
830 if (fl->offset == s->drive_offset + s->size)
834 /* 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 __diagused;
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 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);