2 * Copyright (c) 1997, 1998
3 * Nan Yang Computer Services Limited. All rights reserved.
5 * Written by Greg Lehey
7 * This software is distributed under the so-called ``Berkeley
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Nan Yang Computer
22 * 4. Neither the name of the Company nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * This software is provided ``as is'', and any express or implied
27 * warranties, including, but not limited to, the implied warranties of
28 * merchantability and fitness for a particular purpose are disclaimed.
29 * In no event shall the company or contributors be liable for any
30 * direct, indirect, incidental, special, exemplary, or consequential
31 * damages (including, but not limited to, procurement of substitute
32 * goods or services; loss of use, data, or profits; or business
33 * interruption) however caused and on any theory of liability, whether
34 * in contract, strict liability, or tort (including negligence or
35 * otherwise) arising in any way out of the use of this software, even if
36 * advised of the possibility of such damage.
38 * $Id: vinum.c,v 1.33 2001/01/09 06:19:15 grog Exp grog $
39 * $FreeBSD: src/sys/dev/vinum/vinum.c,v 1.38.2.3 2003/01/07 12:14:16 joerg Exp $
42 #define STATIC static /* nothing while we're testing XXX */
44 #include <dev/vinum/vinumhdr.h>
45 #include <sys/sysproto.h> /* for sync(2) */
46 #include <sys/devicestat.h>
48 #include <sys/reboot.h>
50 extern int total_malloced;
51 extern int malloccount;
52 extern struct mc malloced[];
54 #include <dev/vinum/request.h>
56 STATIC struct cdevsw vinum_cdevsw =
58 vinumopen, vinumclose, physread, physwrite,
59 vinumioctl, seltrue, nommap, vinumstrategy,
60 "vinum", VINUM_CDEV_MAJOR, vinumdump, vinumsize,
61 D_DISK, VINUM_BDEV_MAJOR
64 /* Called by main() during pseudo-device attachment. */
65 STATIC void vinumattach(void *);
67 STATIC int vinum_modevent(module_t mod, modeventtype_t type, void *unused);
69 struct _vinum_conf vinum_conf; /* configuration information */
72 * Called by main() during pseudo-device attachment. All we need
73 * to do is allocate enough space for devices to be configured later, and
77 vinumattach(void *dummy)
79 char *cp, *cp1, *cp2, **drives;
83 /* modload should prevent multiple loads, so this is worth a panic */
84 if ((vinum_conf.flags & VF_LOADED) != 0)
85 panic("vinum: already loaded");
87 log(LOG_INFO, "vinum: loaded\n");
88 vinum_conf.flags |= VF_LOADED; /* we're loaded now */
90 daemonq = NULL; /* initialize daemon's work queue */
93 cdevsw_add(&vinum_cdevsw); /* add the cdevsw entry */
95 /* allocate space: drives... */
96 DRIVE = (struct drive *) Malloc(sizeof(struct drive) * INITIAL_DRIVES);
97 CHECKALLOC(DRIVE, "vinum: no memory\n");
98 bzero(DRIVE, sizeof(struct drive) * INITIAL_DRIVES);
99 vinum_conf.drives_allocated = INITIAL_DRIVES; /* number of drive slots allocated */
100 vinum_conf.drives_used = 0; /* and number in use */
103 VOL = (struct volume *) Malloc(sizeof(struct volume) * INITIAL_VOLUMES);
104 CHECKALLOC(VOL, "vinum: no memory\n");
105 bzero(VOL, sizeof(struct volume) * INITIAL_VOLUMES);
106 vinum_conf.volumes_allocated = INITIAL_VOLUMES; /* number of volume slots allocated */
107 vinum_conf.volumes_used = 0; /* and number in use */
110 PLEX = (struct plex *) Malloc(sizeof(struct plex) * INITIAL_PLEXES);
111 CHECKALLOC(PLEX, "vinum: no memory\n");
112 bzero(PLEX, sizeof(struct plex) * INITIAL_PLEXES);
113 vinum_conf.plexes_allocated = INITIAL_PLEXES; /* number of plex slots allocated */
114 vinum_conf.plexes_used = 0; /* and number in use */
117 SD = (struct sd *) Malloc(sizeof(struct sd) * INITIAL_SUBDISKS);
118 CHECKALLOC(SD, "vinum: no memory\n");
119 bzero(SD, sizeof(struct sd) * INITIAL_SUBDISKS);
120 vinum_conf.subdisks_allocated = INITIAL_SUBDISKS; /* number of sd slots allocated */
121 vinum_conf.subdisks_used = 0; /* and number in use */
124 * See if the loader has passed us a disk to
125 * read the initial configuration from.
127 if ((cp = getenv("vinum.drives")) != NULL) {
128 for (cp1 = cp, i = 0, drives = 0; *cp1 != '\0'; i++) {
130 while (*cp1 != '\0' && *cp1 != ',' && *cp1 != ' ')
134 drives = realloc(drives, (unsigned long)((i + 1) * sizeof(char *)),
140 rv = vinum_scandisk(drives, i);
142 log(LOG_NOTICE, "vinum_scandisk() returned %d", rv);
144 free(drives, M_TEMP);
146 if ((cp = getenv("vinum.root")) != NULL) {
147 for (i = 0; i < vinum_conf.volumes_used; i++) {
148 vol = &vinum_conf.volume[i];
149 if ((vol->state == volume_up)
150 && (strcmp (vol->name, cp) == 0) ) {
151 rootdev = makedev(VINUM_CDEV_MAJOR, i);
152 log(LOG_INFO, "vinum: using volume %s for root device\n", cp);
160 * Check if we have anything open. If confopen is != 0,
161 * that goes for the super device as well, otherwise
164 * Return 0 if not inactive, 1 if inactive.
167 vinum_inactive(int confopen)
170 int can_do = 1; /* assume we can do it */
172 if (confopen && (vinum_conf.flags & VF_OPEN)) /* open by vinum(8)? */
173 return 0; /* can't do it while we're open */
175 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
176 if ((VOL[i].state > volume_down)
177 && (VOL[i].flags & VF_OPEN)) { /* volume is open */
187 * Free all structures.
188 * If cleardrive is 0, save the configuration; otherwise
189 * remove the configuration from the drive.
191 * Before coming here, ensure that no volumes are open.
194 free_vinum(int cleardrive)
197 int drives_allocated = vinum_conf.drives_allocated;
200 if (cleardrive) { /* remove the vinum config */
201 for (i = 0; i < drives_allocated; i++)
202 remove_drive(i); /* remove the drive */
203 } else { /* keep the config */
204 for (i = 0; i < drives_allocated; i++)
205 free_drive(&DRIVE[i]); /* close files and things */
209 while ((vinum_conf.flags & (VF_STOPPING | VF_DAEMONOPEN))
210 == (VF_STOPPING | VF_DAEMONOPEN)) { /* at least one daemon open, we're stopping */
211 queue_daemon_request(daemonrq_return, (union daemoninfo) 0); /* stop the daemon */
212 tsleep(&vinumclose, PUSER, "vstop", 1); /* and wait for it */
217 for (i = 0; i < vinum_conf.plexes_allocated; i++) {
218 struct plex *plex = &vinum_conf.plex[i];
220 if (plex->state != plex_unallocated) { /* we have real data there */
229 bzero(&vinum_conf, sizeof(vinum_conf));
233 vinum_modevent(module_t mod, modeventtype_t type, void *unused)
235 struct sync_args dummyarg =
243 if (!vinum_inactive(1)) /* is anything open? */
244 return EBUSY; /* yes, we can't do it */
245 vinum_conf.flags |= VF_STOPPING; /* note that we want to stop */
246 sync(curproc, &dummyarg); /* write out buffers */
247 free_vinum(0); /* clean up */
249 if (total_malloced) {
255 for (i = 0; i < malloccount; i++) {
256 if (debug & DEBUG_WARNINGS) /* want to hear about them */
258 "vinum: exiting with %d bytes malloced from %s:%d\n",
263 poke = &((int *) malloced[i].address)
264 [malloced[i].size / (2 * sizeof(int))]; /* middle of the area */
265 if (*poke == 0xdeadc0de) /* already freed */
267 "vinum: exiting with malloc table inconsistency at %p from %s:%d\n",
272 Free(malloced[i].address);
276 cdevsw_remove(&vinum_cdevsw);
277 log(LOG_INFO, "vinum: unloaded\n"); /* tell the world */
285 moduledata_t vinum_mod =
288 (modeventhand_t) vinum_modevent,
291 DECLARE_MODULE(vinum, vinum_mod, SI_SUB_VINUM, SI_ORDER_MIDDLE);
294 /* Open a vinum object */
306 int devminor; /* minor number */
308 devminor = minor(dev);
310 /* First, decide what we're looking at */
311 switch (DEVTYPE(dev)) {
312 case VINUM_VOLUME_TYPE:
314 if (index >= vinum_conf.volumes_allocated)
315 return ENXIO; /* no such device */
318 switch (vol->state) {
319 case volume_unallocated:
324 vol->flags |= VF_OPEN; /* note we're open */
334 case VINUM_PLEX_TYPE:
335 if (Volno(dev) >= vinum_conf.volumes_allocated)
339 case VINUM_RAWPLEX_TYPE:
340 index = Plexno(dev); /* get plex index in vinum_conf */
341 if (index >= vinum_conf.plexes_allocated)
342 return ENXIO; /* no such device */
345 switch (plex->state) {
346 case plex_referenced:
347 case plex_unallocated:
351 plex->flags |= VF_OPEN; /* note we're open */
356 if ((Volno(dev) >= vinum_conf.volumes_allocated) /* no such volume */
357 ||(Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
358 return ENXIO; /* no such device */
362 case VINUM_RAWSD_TYPE:
363 index = Sdno(dev); /* get the subdisk number */
364 if ((index >= vinum_conf.subdisks_allocated) /* not a valid SD entry */
365 ||(SD[index].state < sd_init)) /* or SD is not real */
366 return ENXIO; /* no such device */
370 * Opening a subdisk is always a special operation, so we
371 * ignore the state as long as it represents a real subdisk
379 sd->flags |= VF_OPEN; /* note we're open */
383 case VINUM_SUPERDEV_TYPE:
384 error = suser(p); /* are we root? */
385 if (error == 0) { /* yes, can do */
386 if (devminor == VINUM_DAEMON_DEV) /* daemon device */
387 vinum_conf.flags |= VF_DAEMONOPEN; /* we're open */
388 else if (devminor == VINUM_SUPERDEV)
389 vinum_conf.flags |= VF_OPEN; /* we're open */
391 error = ENODEV; /* nothing, maybe a debug mismatch */
395 /* Vinum drives are disks. We already have a disk
396 * driver, so don't handle them here */
397 case VINUM_DRIVE_TYPE:
399 return ENODEV; /* don't know what to do with these */
405 vinumclose(dev_t dev,
414 devminor = minor(dev);
416 /* First, decide what we're looking at */
417 switch (DEVTYPE(dev)) {
418 case VINUM_VOLUME_TYPE:
419 if (index >= vinum_conf.volumes_allocated)
420 return ENXIO; /* no such device */
423 switch (vol->state) {
424 case volume_unallocated:
429 vol->flags &= ~VF_OPEN; /* reset our flags */
439 case VINUM_PLEX_TYPE:
440 if (Volno(dev) >= vinum_conf.volumes_allocated)
444 case VINUM_RAWPLEX_TYPE:
445 index = Plexno(dev); /* get plex index in vinum_conf */
446 if (index >= vinum_conf.plexes_allocated)
447 return ENXIO; /* no such device */
448 PLEX[index].flags &= ~VF_OPEN; /* reset our flags */
452 if ((Volno(dev) >= vinum_conf.volumes_allocated) || /* no such volume */
453 (Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
454 return ENXIO; /* no such device */
457 case VINUM_RAWSD_TYPE:
458 index = Sdno(dev); /* get the subdisk number */
459 if (index >= vinum_conf.subdisks_allocated)
460 return ENXIO; /* no such device */
461 SD[index].flags &= ~VF_OPEN; /* reset our flags */
464 case VINUM_SUPERDEV_TYPE:
466 * don't worry about whether we're root:
467 * nobody else would get this far.
469 if (devminor == VINUM_SUPERDEV) /* normal superdev */
470 vinum_conf.flags &= ~VF_OPEN; /* no longer open */
471 else if (devminor == VINUM_DAEMON_DEV) { /* the daemon device */
472 vinum_conf.flags &= ~VF_DAEMONOPEN; /* no longer open */
473 if (vinum_conf.flags & VF_STOPPING) /* we're stopping, */
474 wakeup(&vinumclose); /* we can continue stopping now */
478 case VINUM_DRIVE_TYPE:
480 return ENODEV; /* don't know what to do with these */
491 vol = &VOL[Volno(dev)];
493 if (vol->state == volume_up)
496 return 0; /* err on the size of conservatism */
504 /* Not implemented. */
508 /* Local Variables: */
509 /* fill-column: 50 */