2 * Copyright (c) 2007 Lukas Ertl
3 * Copyright (c) 2007, 2009 Ulf Lilleengen
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
39 #include <geom/geom.h>
40 #include <geom/vinum/geom_vinum_var.h>
41 #include <geom/vinum/geom_vinum.h>
43 #define DEFAULT_STRIPESIZE 262144
46 * Create a new drive object, either by user request, during taste of the drive
47 * itself, or because it was referenced by a subdisk during taste.
50 gv_create_drive(struct gv_softc *sc, struct gv_drive *d)
53 struct g_provider *pp;
54 struct g_consumer *cp, *cp2;
57 struct gv_freelist *fl;
59 KASSERT(d != NULL, ("gv_create_drive: NULL d"));
66 /* The drive already has a consumer if it was tasted before. */
67 if (d->consumer != NULL) {
71 } else if (!(d->flags & GV_DRIVE_REFERENCED)) {
72 if (gv_find_drive(sc, d->name) != NULL) {
73 G_VINUM_DEBUG(0, "drive '%s' already exists", d->name);
75 return (GV_ERR_CREATE);
78 if (gv_find_drive_device(sc, d->device) != NULL) {
79 G_VINUM_DEBUG(0, "provider '%s' already in use by "
81 return (GV_ERR_CREATE);
84 pp = g_provider_by_name(d->device);
86 G_VINUM_DEBUG(0, "create '%s': device '%s' disappeared",
89 return (GV_ERR_CREATE);
93 cp = g_new_consumer(gp);
94 if (g_attach(cp, pp) != 0) {
95 g_destroy_consumer(cp);
97 G_VINUM_DEBUG(0, "create drive '%s': unable to attach",
100 return (GV_ERR_CREATE);
109 * If this was just a "referenced" drive, we're almost finished, but
110 * insert this drive not on the head of the drives list, as
111 * gv_drive_is_newer() expects a "real" drive from LIST_FIRST().
113 if (d->flags & GV_DRIVE_REFERENCED) {
114 snprintf(d->device, sizeof(d->device), "???");
115 d2 = LIST_FIRST(&sc->drives);
117 LIST_INSERT_HEAD(&sc->drives, d, drive);
119 LIST_INSERT_AFTER(d2, d, drive);
124 * Update access counts of the new drive to those of an already
127 LIST_FOREACH(d2, &sc->drives, drive) {
128 if ((d == d2) || (d2->consumer == NULL))
133 if ((cp2->acr || cp2->acw || cp2->ace) &&
134 (g_access(cp, cp2->acr, cp2->acw, cp2->ace) != 0)) {
136 g_destroy_consumer(cp);
138 G_VINUM_DEBUG(0, "create drive '%s': unable to update "
139 "access counts", d->name);
143 return (GV_ERR_CREATE);
149 d->size = pp->mediasize - GV_DATA_START;
152 LIST_INIT(&d->subdisks);
153 LIST_INIT(&d->freelist);
155 /* The header might have been set during taste. */
156 if (d->hdr == NULL) {
157 hdr = g_malloc(sizeof(*hdr), M_WAITOK | M_ZERO);
158 hdr->magic = GV_MAGIC;
159 hdr->config_length = GV_CFG_LEN;
160 getcredhostname(NULL, hdr->label.sysname, GV_HOSTNAME_LEN);
161 strlcpy(hdr->label.name, d->name, sizeof(hdr->label.name));
162 microtime(&hdr->label.date_of_birth);
166 /* We also need a freelist entry. */
167 fl = g_malloc(sizeof(struct gv_freelist), M_WAITOK | M_ZERO);
168 fl->offset = GV_DATA_START;
170 LIST_INSERT_HEAD(&d->freelist, fl, freelist);
171 d->freelist_entries = 1;
173 if (gv_find_drive(sc, d->name) == NULL)
174 LIST_INSERT_HEAD(&sc->drives, d, drive);
176 gv_set_drive_state(d, GV_DRIVE_UP, 0);
181 gv_create_volume(struct gv_softc *sc, struct gv_volume *v)
183 KASSERT(v != NULL, ("gv_create_volume: NULL v"));
186 v->flags |= GV_VOL_NEWBORN;
187 LIST_INIT(&v->plexes);
188 LIST_INSERT_HEAD(&sc->volumes, v, volume);
189 v->wqueue = g_malloc(sizeof(struct bio_queue_head), M_WAITOK | M_ZERO);
190 bioq_init(v->wqueue);
195 gv_create_plex(struct gv_softc *sc, struct gv_plex *p)
199 KASSERT(p != NULL, ("gv_create_plex: NULL p"));
201 /* Find the volume this plex should be attached to. */
202 v = gv_find_vol(sc, p->volume);
204 G_VINUM_DEBUG(0, "create plex '%s': volume '%s' not found",
207 return (GV_ERR_CREATE);
209 if (!(v->flags & GV_VOL_NEWBORN))
210 p->flags |= GV_PLEX_ADDED;
215 p->flags |= GV_PLEX_NEWBORN;
216 LIST_INSERT_HEAD(&v->plexes, p, in_volume);
217 LIST_INIT(&p->subdisks);
218 TAILQ_INIT(&p->packets);
219 LIST_INSERT_HEAD(&sc->plexes, p, plex);
220 p->bqueue = g_malloc(sizeof(struct bio_queue_head), M_WAITOK | M_ZERO);
221 bioq_init(p->bqueue);
222 p->wqueue = g_malloc(sizeof(struct bio_queue_head), M_WAITOK | M_ZERO);
223 bioq_init(p->wqueue);
224 p->rqueue = g_malloc(sizeof(struct bio_queue_head), M_WAITOK | M_ZERO);
225 bioq_init(p->rqueue);
230 gv_create_sd(struct gv_softc *sc, struct gv_sd *s)
235 KASSERT(s != NULL, ("gv_create_sd: NULL s"));
237 /* Find the drive where this subdisk should be put on. */
238 d = gv_find_drive(sc, s->drive);
241 * It's possible that the subdisk references a drive that
242 * doesn't exist yet (during the taste process), so create a
243 * practically empty "referenced" drive.
245 if (s->flags & GV_SD_TASTED) {
246 d = g_malloc(sizeof(struct gv_drive),
248 d->flags |= GV_DRIVE_REFERENCED;
249 strlcpy(d->name, s->drive, sizeof(d->name));
250 gv_create_drive(sc, d);
252 G_VINUM_DEBUG(0, "create sd '%s': drive '%s' not found",
255 return (GV_ERR_CREATE);
259 /* Find the plex where this subdisk belongs to. */
260 p = gv_find_plex(sc, s->plex);
262 G_VINUM_DEBUG(0, "create sd '%s': plex '%s' not found",
265 return (GV_ERR_CREATE);
269 * First we give the subdisk to the drive, to handle autosized
272 if (gv_sd_to_drive(s, d) != 0) {
274 return (GV_ERR_CREATE);
278 * Then, we give the subdisk to the plex; we check if the
279 * given values are correct and maybe adjust them.
281 if (gv_sd_to_plex(s, p) != 0) {
282 G_VINUM_DEBUG(0, "unable to give sd '%s' to plex '%s'",
284 if (s->drive_sc && !(s->drive_sc->flags & GV_DRIVE_REFERENCED))
285 LIST_REMOVE(s, from_drive);
289 * If this subdisk can't be created, we won't create
290 * the attached plex either, if it is also a new one.
292 if (!(p->flags & GV_PLEX_NEWBORN))
293 return (GV_ERR_CREATE);
295 return (GV_ERR_CREATE);
297 s->flags |= GV_SD_NEWBORN;
300 LIST_INSERT_HEAD(&sc->subdisks, s, sd);
306 * Create a concatenated volume from specified drives or drivegroups.
309 gv_concat(struct g_geom *gp, struct gctl_req *req)
316 char *drive, buf[30], *vol;
321 vol = gctl_get_param(req, "name", NULL);
323 gctl_error(req, "volume name not given");
327 drives = gctl_get_paraml(req, "drives", sizeof(*drives));
329 if (drives == NULL) {
330 gctl_error(req, "drive names not given");
334 /* First we create the volume. */
335 v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
336 strlcpy(v->name, vol, sizeof(v->name));
337 v->state = GV_VOL_UP;
338 gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0);
340 /* Then we create the plex. */
341 p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
342 snprintf(p->name, sizeof(p->name), "%s.p%d", v->name, v->plexcount);
343 strlcpy(p->volume, v->name, sizeof(p->volume));
344 p->org = GV_PLEX_CONCAT;
346 gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0);
348 /* Drives are first (right now) priority */
349 for (dcount = 0; dcount < *drives; dcount++) {
350 snprintf(buf, sizeof(buf), "drive%d", dcount);
351 drive = gctl_get_param(req, buf, NULL);
352 d = gv_find_drive(sc, drive);
354 gctl_error(req, "No such drive '%s'", drive);
357 s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
358 snprintf(s->name, sizeof(s->name), "%s.s%d", p->name, dcount);
359 strlcpy(s->plex, p->name, sizeof(s->plex));
360 strlcpy(s->drive, drive, sizeof(s->drive));
362 s->drive_offset = -1;
364 gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0);
366 gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0);
367 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
371 * Create a mirrored volume from specified drives or drivegroups.
374 gv_mirror(struct g_geom *gp, struct gctl_req *req)
381 char *drive, buf[30], *vol;
382 int *drives, *flags, dcount, pcount, scount;
388 vol = gctl_get_param(req, "name", NULL);
390 gctl_error(req, "volume name not given");
394 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
395 drives = gctl_get_paraml(req, "drives", sizeof(*drives));
397 if (drives == NULL) {
398 gctl_error(req, "drive names not given");
402 /* We must have an even number of drives. */
403 if (*drives % 2 != 0) {
404 gctl_error(req, "mirror organization must have an even number "
408 if (*flags & GV_FLAG_S && *drives < 4) {
409 gctl_error(req, "must have at least 4 drives for striped plex");
413 /* First we create the volume. */
414 v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
415 strlcpy(v->name, vol, sizeof(v->name));
416 v->state = GV_VOL_UP;
417 gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0);
419 /* Then we create the plexes. */
420 for (pcount = 0; pcount < 2; pcount++) {
421 p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
422 snprintf(p->name, sizeof(p->name), "%s.p%d", v->name,
424 strlcpy(p->volume, v->name, sizeof(p->volume));
425 if (*flags & GV_FLAG_S) {
426 p->org = GV_PLEX_STRIPED;
427 p->stripesize = DEFAULT_STRIPESIZE;
429 p->org = GV_PLEX_CONCAT;
432 gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0);
435 * We just gives each even drive to plex one, and each odd to
439 for (dcount = pcount; dcount < *drives; dcount += 2) {
440 snprintf(buf, sizeof(buf), "drive%d", dcount);
441 drive = gctl_get_param(req, buf, NULL);
442 d = gv_find_drive(sc, drive);
444 gctl_error(req, "No such drive '%s', aborting",
449 s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
450 snprintf(s->name, sizeof(s->name), "%s.s%d", p->name,
452 strlcpy(s->plex, p->name, sizeof(s->plex));
453 strlcpy(s->drive, drive, sizeof(s->drive));
455 s->drive_offset = -1;
457 gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0);
461 gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0);
462 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
466 gv_raid5(struct g_geom *gp, struct gctl_req *req)
473 int *drives, *flags, dcount;
474 char *vol, *drive, buf[30];
479 vol = gctl_get_param(req, "name", NULL);
481 gctl_error(req, "volume name not given");
484 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
485 drives = gctl_get_paraml(req, "drives", sizeof(*drives));
486 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
488 if (stripesize == NULL) {
489 gctl_error(req, "no stripesize given");
493 if (drives == NULL) {
494 gctl_error(req, "drive names not given");
498 /* We must have at least three drives. */
500 gctl_error(req, "must have at least three drives for this "
501 "plex organisation");
504 /* First we create the volume. */
505 v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
506 strlcpy(v->name, vol, sizeof(v->name));
507 v->state = GV_VOL_UP;
508 gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0);
510 /* Then we create the plex. */
511 p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
512 snprintf(p->name, sizeof(p->name), "%s.p%d", v->name, v->plexcount);
513 strlcpy(p->volume, v->name, sizeof(p->volume));
514 p->org = GV_PLEX_RAID5;
515 p->stripesize = *stripesize;
516 gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0);
518 /* Create subdisks on drives. */
519 for (dcount = 0; dcount < *drives; dcount++) {
520 snprintf(buf, sizeof(buf), "drive%d", dcount);
521 drive = gctl_get_param(req, buf, NULL);
522 d = gv_find_drive(sc, drive);
524 gctl_error(req, "No such drive '%s'", drive);
527 s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
528 snprintf(s->name, sizeof(s->name), "%s.s%d", p->name, dcount);
529 strlcpy(s->plex, p->name, sizeof(s->plex));
530 strlcpy(s->drive, drive, sizeof(s->drive));
532 s->drive_offset = -1;
534 gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0);
536 gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0);
537 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
541 * Create a striped volume from specified drives or drivegroups.
544 gv_stripe(struct g_geom *gp, struct gctl_req *req)
551 char *drive, buf[30], *vol;
552 int *drives, *flags, dcount, pcount;
557 vol = gctl_get_param(req, "name", NULL);
559 gctl_error(req, "volume name not given");
562 flags = gctl_get_paraml(req, "flags", sizeof(*flags));
563 drives = gctl_get_paraml(req, "drives", sizeof(*drives));
565 if (drives == NULL) {
566 gctl_error(req, "drive names not given");
570 /* We must have at least two drives. */
572 gctl_error(req, "must have at least 2 drives");
576 /* First we create the volume. */
577 v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
578 strlcpy(v->name, vol, sizeof(v->name));
579 v->state = GV_VOL_UP;
580 gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0);
582 /* Then we create the plex. */
583 p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
584 snprintf(p->name, sizeof(p->name), "%s.p%d", v->name, v->plexcount);
585 strlcpy(p->volume, v->name, sizeof(p->volume));
586 p->org = GV_PLEX_STRIPED;
587 p->stripesize = 262144;
588 gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0);
590 /* Create subdisks on drives. */
591 for (dcount = 0; dcount < *drives; dcount++) {
592 snprintf(buf, sizeof(buf), "drive%d", dcount);
593 drive = gctl_get_param(req, buf, NULL);
594 d = gv_find_drive(sc, drive);
596 gctl_error(req, "No such drive '%s'", drive);
599 s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
600 snprintf(s->name, sizeof(s->name), "%s.s%d", p->name, dcount);
601 strlcpy(s->plex, p->name, sizeof(s->plex));
602 strlcpy(s->drive, drive, sizeof(s->drive));
604 s->drive_offset = -1;
606 gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0);
608 gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0);
609 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);