2 * Copyright (c) 2004 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 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 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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
40 #include <geom/geom.h>
41 #include <geom/vinum/geom_vinum_var.h>
42 #include <geom/vinum/geom_vinum.h>
43 #include <geom/vinum/geom_vinum_share.h>
46 SYSCTL_DECL(_kern_geom);
47 SYSCTL_NODE(_kern_geom, OID_AUTO, vinum, CTLFLAG_RW, 0, "GEOM_VINUM stuff");
48 SYSCTL_UINT(_kern_geom_vinum, OID_AUTO, debug, CTLFLAG_RW, &gv_debug, 0,
52 int gv_create(struct g_geom *, struct gctl_req *);
55 gv_orphan(struct g_consumer *cp)
63 KASSERT(cp != NULL, ("gv_orphan: null cp"));
65 KASSERT(gp != NULL, ("gv_orphan: null gp"));
68 g_trace(G_T_TOPOLOGY, "gv_orphan(%s)", gp->name);
70 if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0)
71 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
72 error = cp->provider->error;
76 g_destroy_consumer(cp);
77 if (!LIST_EMPTY(&gp->consumer))
80 g_wither_geom(gp, error);
84 gv_start(struct bio *bp)
89 gp = bp->bio_to->geom;
94 bp2 = g_clone_bio(bp);
96 g_io_deliver(bp, ENOMEM);
98 bp2->bio_done = g_std_done;
99 g_io_request(bp2, LIST_FIRST(&gp->consumer));
103 g_io_deliver(bp, EOPNOTSUPP);
109 gv_access(struct g_provider *pp, int dr, int dw, int de)
112 struct g_consumer *cp;
117 cp = LIST_FIRST(&gp->consumer);
118 error = g_access(cp, dr, dw, de);
123 gv_init(struct g_class *mp)
128 g_trace(G_T_TOPOLOGY, "gv_init(%p)", mp);
130 gp = g_new_geomf(mp, "VINUM");
131 gp->spoiled = gv_orphan;
132 gp->orphan = gv_orphan;
133 gp->access = gv_access;
134 gp->start = gv_start;
135 gp->softc = g_malloc(sizeof(struct gv_softc), M_WAITOK | M_ZERO);
138 LIST_INIT(&sc->drives);
139 LIST_INIT(&sc->subdisks);
140 LIST_INIT(&sc->plexes);
141 LIST_INIT(&sc->volumes);
144 /* Handle userland requests for creating new objects. */
146 gv_create(struct g_geom *gp, struct gctl_req *req)
149 struct gv_drive *d, *d2;
150 struct gv_plex *p, *p2;
151 struct gv_sd *s, *s2;
152 struct gv_volume *v, *v2;
153 struct g_consumer *cp;
154 struct g_provider *pp;
155 int error, i, *drives, *plexes, *subdisks, *volumes;
156 char buf[20], errstr[ERRBUFSIZ];
162 /* Find out how many of each object have been passed in. */
163 volumes = gctl_get_paraml(req, "volumes", sizeof(*volumes));
164 plexes = gctl_get_paraml(req, "plexes", sizeof(*plexes));
165 subdisks = gctl_get_paraml(req, "subdisks", sizeof(*subdisks));
166 drives = gctl_get_paraml(req, "drives", sizeof(*drives));
168 /* First, handle drive definitions ... */
169 for (i = 0; i < *drives; i++) {
170 snprintf(buf, sizeof(buf), "drive%d", i);
171 d2 = gctl_get_paraml(req, buf, sizeof(*d2));
173 d = gv_find_drive(sc, d2->name);
175 gctl_error(req, "drive '%s' is already known",
180 d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
181 bcopy(d2, d, sizeof(*d));
184 * Make sure that the provider specified in the drive
185 * specification is an active GEOM provider.
187 pp = g_provider_by_name(d->device);
189 gctl_error(req, "%s: drive not found", d->device);
193 d->size = pp->mediasize - GV_DATA_START;
196 gv_config_new_drive(d);
198 d->flags |= GV_DRIVE_NEWBORN;
199 LIST_INSERT_HEAD(&sc->drives, d, drive);
202 /* ... then volume definitions ... */
203 for (i = 0; i < *volumes; i++) {
205 snprintf(buf, sizeof(buf), "volume%d", i);
206 v2 = gctl_get_paraml(req, buf, sizeof(*v2));
208 v = gv_find_vol(sc, v2->name);
210 gctl_error(req, "volume '%s' is already known",
215 v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
216 bcopy(v2, v, sizeof(*v));
219 LIST_INIT(&v->plexes);
220 LIST_INSERT_HEAD(&sc->volumes, v, volume);
223 /* ... then plex definitions ... */
224 for (i = 0; i < *plexes; i++) {
226 snprintf(buf, sizeof(buf), "plex%d", i);
227 p2 = gctl_get_paraml(req, buf, sizeof(*p2));
229 p = gv_find_plex(sc, p2->name);
231 gctl_error(req, "plex '%s' is already known", p->name);
235 p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
236 bcopy(p2, p, sizeof(*p));
238 /* Find the volume this plex should be attached to. */
239 v = gv_find_vol(sc, p->volume);
242 p->flags |= GV_PLEX_ADDED;
245 LIST_INSERT_HEAD(&v->plexes, p, in_volume);
249 p->flags |= GV_PLEX_NEWBORN;
250 LIST_INIT(&p->subdisks);
251 LIST_INSERT_HEAD(&sc->plexes, p, plex);
254 /* ... and finally, subdisk definitions. */
255 for (i = 0; i < *subdisks; i++) {
257 snprintf(buf, sizeof(buf), "sd%d", i);
258 s2 = gctl_get_paraml(req, buf, sizeof(*s2));
260 s = gv_find_sd(sc, s2->name);
262 gctl_error(req, "subdisk '%s' is already known",
267 s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
268 bcopy(s2, s, sizeof(*s));
270 /* Find the drive where this subdisk should be put on. */
271 d = gv_find_drive(sc, s->drive);
273 /* drive not found - XXX */
275 printf("FOO: drive '%s' not found\n", s->drive);
280 /* Find the plex where this subdisk belongs to. */
281 p = gv_find_plex(sc, s->plex);
283 /* plex not found - XXX */
285 printf("FOO: plex '%s' not found\n", s->plex);
291 * First we give the subdisk to the drive, to handle autosized
294 error = gv_sd_to_drive(sc, d, s, errstr, sizeof(errstr));
296 gctl_error(req, errstr);
302 * Then, we give the subdisk to the plex; we check if the
303 * given values are correct and maybe adjust them.
305 error = gv_sd_to_plex(p, s, 1);
307 printf("FOO: couldn't give sd '%s' to plex '%s'\n",
310 s->flags |= GV_SD_NEWBORN;
313 LIST_INSERT_HEAD(&sc->subdisks, s, sd);
316 LIST_FOREACH(s, &sc->subdisks, sd)
317 gv_update_sd_state(s);
318 LIST_FOREACH(p, &sc->plexes, plex)
319 gv_update_plex_config(p);
320 LIST_FOREACH(v, &sc->volumes, volume)
321 gv_update_vol_state(v);
324 * Write out the configuration to each drive. If the drive doesn't
325 * have a valid geom_slice geom yet, attach it temporarily to our VINUM
328 LIST_FOREACH(d, &sc->drives, drive) {
329 if (d->geom == NULL) {
331 * XXX if the provider disapears before we get a chance
332 * to write the config out to the drive, should this
333 * be handled any differently?
335 pp = g_provider_by_name(d->device);
337 printf("geom_vinum: %s: drive disapeared?\n",
341 cp = g_new_consumer(gp);
343 gv_save_config(cp, d, sc);
345 g_destroy_consumer(cp);
347 gv_save_config(NULL, d, sc);
348 d->flags &= ~GV_DRIVE_NEWBORN;
355 gv_config(struct gctl_req *req, struct g_class *mp, char const *verb)
364 gp = LIST_FIRST(&mp->geom);
367 if (!strcmp(verb, "list")) {
370 /* Save our configuration back to disk. */
371 } else if (!strcmp(verb, "saveconfig")) {
373 gv_save_config_all(sc);
375 /* Return configuration in string form. */
376 } else if (!strcmp(verb, "getconfig")) {
377 comment = gctl_get_param(req, "comment", NULL);
379 sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
380 gv_format_config(sc, sb, 0, comment);
382 gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1);
385 } else if (!strcmp(verb, "create")) {
388 } else if (!strcmp(verb, "move")) {
391 } else if (!strcmp(verb, "parityop")) {
392 gv_parityop(gp, req);
394 } else if (!strcmp(verb, "remove")) {
397 } else if (!strcmp(verb, "rename")) {
400 } else if (!strcmp(verb, "start")) {
401 gv_start_obj(gp, req);
403 } else if (!strcmp(verb, "setstate")) {
404 gv_setstate(gp, req);
407 gctl_error(req, "Unknown verb parameter");
412 gv_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
415 struct g_consumer *cp;
417 struct gv_drive *d, *d2;
418 struct gv_plex *p, *p2;
419 struct gv_sd *s, *s2;
420 struct gv_volume *v, *v2;
421 struct gv_freelist *fl, *fl2;
423 g_trace(G_T_TOPOLOGY, "gv_destroy_geom: %s", gp->name);
426 KASSERT(gp != NULL, ("gv_destroy_geom: null gp"));
427 KASSERT(gp->softc != NULL, ("gv_destroy_geom: null sc"));
432 * Check if any of our drives is still open; if so, refuse destruction.
434 LIST_FOREACH(d, &sc->drives, drive) {
436 cp = LIST_FIRST(&gp2->consumer);
438 g_access(cp, -1, -1, -1);
443 /* Clean up and deallocate what we allocated. */
444 LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) {
445 LIST_REMOVE(d, drive);
448 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
449 d->freelist_entries--;
450 LIST_REMOVE(fl, freelist);
454 d->geom->softc = NULL;
458 LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2) {
467 LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) {
468 LIST_REMOVE(p, plex);
471 p->geom->softc = NULL;
474 if (p->org == GV_PLEX_RAID5) {
475 mtx_destroy(&p->worklist_mtx);
480 LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2) {
481 LIST_REMOVE(v, volume);
482 v->geom->softc = NULL;
488 g_wither_geom(gp, ENXIO);
493 #define VINUM_CLASS_NAME "VINUM"
495 static struct g_class g_vinum_class = {
496 .name = VINUM_CLASS_NAME,
497 .version = G_VERSION,
499 /*.destroy_geom = gv_destroy_geom,*/
503 DECLARE_GEOM_CLASS(g_vinum_class, g_vinum);