]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/geom/vinum/geom_vinum.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sys / geom / vinum / geom_vinum.c
1 /*-
2  *  Copyright (c) 2004 Lukas Ertl
3  *  All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  * 
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
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/bio.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
39
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>
44
45 #if 0
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,
49     "Debug level");
50 #endif
51
52 int     gv_create(struct g_geom *, struct gctl_req *);
53
54 static void
55 gv_orphan(struct g_consumer *cp)
56 {
57         struct g_geom *gp;
58         struct gv_softc *sc;
59         int error;
60         
61         g_topology_assert();
62
63         KASSERT(cp != NULL, ("gv_orphan: null cp"));
64         gp = cp->geom;
65         KASSERT(gp != NULL, ("gv_orphan: null gp"));
66         sc = gp->softc;
67
68         g_trace(G_T_TOPOLOGY, "gv_orphan(%s)", gp->name);
69
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;
73         if (error == 0)
74                 error = ENXIO;
75         g_detach(cp);
76         g_destroy_consumer(cp);
77         if (!LIST_EMPTY(&gp->consumer))
78                 return;
79         g_free(sc);
80         g_wither_geom(gp, error);
81 }
82
83 static void
84 gv_start(struct bio *bp)
85 {
86         struct bio *bp2;
87         struct g_geom *gp;
88         
89         gp = bp->bio_to->geom;
90         switch(bp->bio_cmd) {
91         case BIO_READ:
92         case BIO_WRITE:
93         case BIO_DELETE:
94                 bp2 = g_clone_bio(bp);
95                 if (bp2 == NULL)
96                         g_io_deliver(bp, ENOMEM);
97                 else {
98                         bp2->bio_done = g_std_done;
99                         g_io_request(bp2, LIST_FIRST(&gp->consumer));
100                 }
101                 return;
102         default:
103                 g_io_deliver(bp, EOPNOTSUPP);
104                 return;
105         }
106 }
107
108 static int
109 gv_access(struct g_provider *pp, int dr, int dw, int de)
110 {
111         struct g_geom *gp;
112         struct g_consumer *cp;
113         int error;
114         
115         gp = pp->geom;
116         error = ENXIO;
117         cp = LIST_FIRST(&gp->consumer);
118         error = g_access(cp, dr, dw, de);
119         return (error);
120 }
121
122 static void
123 gv_init(struct g_class *mp)
124 {
125         struct g_geom *gp;
126         struct gv_softc *sc;
127
128         g_trace(G_T_TOPOLOGY, "gv_init(%p)", mp);
129
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);
136         sc = gp->softc;
137         sc->geom = gp;
138         LIST_INIT(&sc->drives);
139         LIST_INIT(&sc->subdisks);
140         LIST_INIT(&sc->plexes);
141         LIST_INIT(&sc->volumes);
142 }
143
144 /* Handle userland requests for creating new objects. */
145 int
146 gv_create(struct g_geom *gp, struct gctl_req *req)
147 {
148         struct gv_softc *sc;
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];
157
158         g_topology_assert();
159
160         sc = gp->softc;
161
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));
167
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));
172
173                 d = gv_find_drive(sc, d2->name);
174                 if (d != NULL) {
175                         gctl_error(req, "drive '%s' is already known",
176                             d->name);
177                         continue;
178                 }
179
180                 d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
181                 bcopy(d2, d, sizeof(*d));
182
183                 /*
184                  * Make sure that the provider specified in the drive
185                  * specification is an active GEOM provider.
186                  */
187                 pp = g_provider_by_name(d->device);
188                 if (pp == NULL) {
189                         gctl_error(req, "%s: drive not found", d->device);
190                         g_free(d);
191                         return (-1);
192                 }
193                 d->size = pp->mediasize - GV_DATA_START;
194                 d->avail = d->size;
195
196                 gv_config_new_drive(d);
197
198                 d->flags |= GV_DRIVE_NEWBORN;
199                 LIST_INSERT_HEAD(&sc->drives, d, drive);
200         }
201
202         /* ... then volume definitions ... */
203         for (i = 0; i < *volumes; i++) {
204                 error = 0;
205                 snprintf(buf, sizeof(buf), "volume%d", i);
206                 v2 = gctl_get_paraml(req, buf, sizeof(*v2));
207
208                 v = gv_find_vol(sc, v2->name);
209                 if (v != NULL) {
210                         gctl_error(req, "volume '%s' is already known",
211                             v->name);
212                         return (-1);
213                 }
214
215                 v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
216                 bcopy(v2, v, sizeof(*v));
217
218                 v->vinumconf = sc;
219                 LIST_INIT(&v->plexes);
220                 LIST_INSERT_HEAD(&sc->volumes, v, volume);
221         }
222
223         /* ... then plex definitions ... */
224         for (i = 0; i < *plexes; i++) {
225                 error = 0;
226                 snprintf(buf, sizeof(buf), "plex%d", i);
227                 p2 = gctl_get_paraml(req, buf, sizeof(*p2));
228
229                 p = gv_find_plex(sc, p2->name);
230                 if (p != NULL) {
231                         gctl_error(req, "plex '%s' is already known", p->name);
232                         return (-1);
233                 }
234
235                 p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
236                 bcopy(p2, p, sizeof(*p));
237
238                 /* Find the volume this plex should be attached to. */
239                 v = gv_find_vol(sc, p->volume);
240                 if (v != NULL) {
241                         if (v->plexcount)
242                                 p->flags |= GV_PLEX_ADDED;
243                         p->vol_sc = v;
244                         v->plexcount++;
245                         LIST_INSERT_HEAD(&v->plexes, p, in_volume);
246                 }
247
248                 p->vinumconf = sc;
249                 p->flags |= GV_PLEX_NEWBORN;
250                 LIST_INIT(&p->subdisks);
251                 LIST_INSERT_HEAD(&sc->plexes, p, plex);
252         }
253
254         /* ... and finally, subdisk definitions. */
255         for (i = 0; i < *subdisks; i++) {
256                 error = 0;
257                 snprintf(buf, sizeof(buf), "sd%d", i);
258                 s2 = gctl_get_paraml(req, buf, sizeof(*s2));
259
260                 s = gv_find_sd(sc, s2->name);
261                 if (s != NULL) {
262                         gctl_error(req, "subdisk '%s' is already known",
263                             s->name);
264                         return (-1);
265                 }
266
267                 s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
268                 bcopy(s2, s, sizeof(*s));
269
270                 /* Find the drive where this subdisk should be put on. */
271                 d = gv_find_drive(sc, s->drive);
272
273                 /* drive not found - XXX */
274                 if (d == NULL) {
275                         printf("FOO: drive '%s' not found\n", s->drive);
276                         g_free(s);
277                         continue;
278                 }
279
280                 /* Find the plex where this subdisk belongs to. */
281                 p = gv_find_plex(sc, s->plex);
282
283                 /* plex not found - XXX */
284                 if (p == NULL) {
285                         printf("FOO: plex '%s' not found\n", s->plex);
286                         g_free(s);
287                         continue;
288                 }
289
290                 /*
291                  * First we give the subdisk to the drive, to handle autosized
292                  * values ...
293                  */
294                 error = gv_sd_to_drive(sc, d, s, errstr, sizeof(errstr));
295                 if (error) {
296                         gctl_error(req, errstr);
297                         g_free(s);
298                         continue;
299                 }
300
301                 /*
302                  * Then, we give the subdisk to the plex; we check if the
303                  * given values are correct and maybe adjust them.
304                  */
305                 error = gv_sd_to_plex(p, s, 1);
306                 if (error) {
307                         printf("FOO: couldn't give sd '%s' to plex '%s'\n",
308                             s->name, p->name);
309                 }
310                 s->flags |= GV_SD_NEWBORN;
311
312                 s->vinumconf = sc;
313                 LIST_INSERT_HEAD(&sc->subdisks, s, sd);
314         }
315
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);
322
323         /*
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
326          * geom.
327          */
328         LIST_FOREACH(d, &sc->drives, drive) {
329                 if (d->geom == NULL) {
330                         /*
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?
334                          */
335                         pp = g_provider_by_name(d->device);
336                         if (pp == NULL) {
337                                 printf("geom_vinum: %s: drive disapeared?\n",
338                                     d->device);
339                                 continue;
340                         }
341                         cp = g_new_consumer(gp);
342                         g_attach(cp, pp);
343                         gv_save_config(cp, d, sc);
344                         g_detach(cp);
345                         g_destroy_consumer(cp);
346                 } else
347                         gv_save_config(NULL, d, sc);
348                 d->flags &= ~GV_DRIVE_NEWBORN;
349         }
350
351         return (0);
352 }
353
354 static void
355 gv_config(struct gctl_req *req, struct g_class *mp, char const *verb)
356 {
357         struct g_geom *gp;
358         struct gv_softc *sc;
359         struct sbuf *sb;
360         char *comment;
361
362         g_topology_assert();
363
364         gp = LIST_FIRST(&mp->geom);
365         sc = gp->softc;
366
367         if (!strcmp(verb, "list")) {
368                 gv_list(gp, req);
369
370         /* Save our configuration back to disk. */
371         } else if (!strcmp(verb, "saveconfig")) {
372
373                 gv_save_config_all(sc);
374
375         /* Return configuration in string form. */
376         } else if (!strcmp(verb, "getconfig")) {
377                 comment = gctl_get_param(req, "comment", NULL);
378
379                 sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
380                 gv_format_config(sc, sb, 0, comment);
381                 sbuf_finish(sb);
382                 gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1);
383                 sbuf_delete(sb);
384
385         } else if (!strcmp(verb, "create")) {
386                 gv_create(gp, req);
387
388         } else if (!strcmp(verb, "move")) {
389                 gv_move(gp, req);
390
391         } else if (!strcmp(verb, "parityop")) {
392                 gv_parityop(gp, req);
393
394         } else if (!strcmp(verb, "remove")) {
395                 gv_remove(gp, req);
396
397         } else if (!strcmp(verb, "rename")) {
398                 gv_rename(gp, req);
399
400         } else if (!strcmp(verb, "start")) {
401                 gv_start_obj(gp, req);
402
403         } else if (!strcmp(verb, "setstate")) {
404                 gv_setstate(gp, req);
405
406         } else
407                 gctl_error(req, "Unknown verb parameter");
408 }
409
410 #if 0
411 static int
412 gv_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
413 {
414         struct g_geom *gp2;
415         struct g_consumer *cp;
416         struct gv_softc *sc;
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;
422
423         g_trace(G_T_TOPOLOGY, "gv_destroy_geom: %s", gp->name);
424         g_topology_assert();
425
426         KASSERT(gp != NULL, ("gv_destroy_geom: null gp"));
427         KASSERT(gp->softc != NULL, ("gv_destroy_geom: null sc"));
428
429         sc = gp->softc;
430
431         /*
432          * Check if any of our drives is still open; if so, refuse destruction.
433          */
434         LIST_FOREACH(d, &sc->drives, drive) {
435                 gp2 = d->geom;
436                 cp = LIST_FIRST(&gp2->consumer);
437                 if (cp != NULL)
438                         g_access(cp, -1, -1, -1);
439                 if (gv_is_open(gp2))
440                         return (EBUSY);
441         }
442
443         /* Clean up and deallocate what we allocated. */
444         LIST_FOREACH_SAFE(d, &sc->drives, drive, d2) {
445                 LIST_REMOVE(d, drive);
446                 g_free(d->hdr);
447                 d->hdr = NULL;
448                 LIST_FOREACH_SAFE(fl, &d->freelist, freelist, fl2) {
449                         d->freelist_entries--;
450                         LIST_REMOVE(fl, freelist);
451                         g_free(fl);
452                         fl = NULL;
453                 }
454                 d->geom->softc = NULL;
455                 g_free(d);
456         }
457
458         LIST_FOREACH_SAFE(s, &sc->subdisks, sd, s2) {
459                 LIST_REMOVE(s, sd);
460                 s->drive_sc = NULL;
461                 s->plex_sc = NULL;
462                 s->provider = NULL;
463                 s->consumer = NULL;
464                 g_free(s);
465         }
466
467         LIST_FOREACH_SAFE(p, &sc->plexes, plex, p2) {
468                 LIST_REMOVE(p, plex);
469                 gv_kill_thread(p);
470                 p->vol_sc = NULL;
471                 p->geom->softc = NULL;
472                 p->provider = NULL;
473                 p->consumer = NULL;
474                 if (p->org == GV_PLEX_RAID5) {
475                         mtx_destroy(&p->worklist_mtx);
476                 }
477                 g_free(p);
478         }
479
480         LIST_FOREACH_SAFE(v, &sc->volumes, volume, v2) {
481                 LIST_REMOVE(v, volume);
482                 v->geom->softc = NULL;
483                 g_free(v);
484         }
485
486         gp->softc = NULL;
487         g_free(sc);
488         g_wither_geom(gp, ENXIO);
489         return (0);
490 }
491 #endif
492
493 #define VINUM_CLASS_NAME "VINUM"
494
495 static struct g_class g_vinum_class     = {
496         .name = VINUM_CLASS_NAME,
497         .version = G_VERSION,
498         .init = gv_init,
499         /*.destroy_geom = gv_destroy_geom,*/
500         .ctlreq = gv_config,
501 };
502
503 DECLARE_GEOM_CLASS(g_vinum_class, g_vinum);