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 $
40 * $DragonFly: src/sys/dev/raid/vinum/vinum.c,v 1.5 2003/07/19 21:14:31 dillon Exp $
43 #define STATIC static /* nothing while we're testing XXX */
45 #include <dev/vinum/vinumhdr.h>
46 #include <sys/sysproto.h> /* for sync(2) */
47 #include <sys/devicestat.h>
49 #include <sys/reboot.h>
51 extern int total_malloced;
52 extern int malloccount;
53 extern struct mc malloced[];
55 #include <dev/vinum/request.h>
57 STATIC struct cdevsw vinum_cdevsw =
59 vinumopen, vinumclose, physread, physwrite,
60 vinumioctl, seltrue, nommap, vinumstrategy,
61 "vinum", VINUM_CDEV_MAJOR, vinumdump, vinumsize,
62 D_DISK, VINUM_BDEV_MAJOR
65 /* Called by main() during pseudo-device attachment. */
66 STATIC void vinumattach(void *);
68 STATIC int vinum_modevent(module_t mod, modeventtype_t type, void *unused);
70 struct _vinum_conf vinum_conf; /* configuration information */
73 * Called by main() during pseudo-device attachment. All we need
74 * to do is allocate enough space for devices to be configured later, and
78 vinumattach(void *dummy)
80 char *cp, *cp1, *cp2, **drives;
84 /* modload should prevent multiple loads, so this is worth a panic */
85 if ((vinum_conf.flags & VF_LOADED) != 0)
86 panic("vinum: already loaded");
88 log(LOG_INFO, "vinum: loaded\n");
89 vinum_conf.flags |= VF_LOADED; /* we're loaded now */
91 daemonq = NULL; /* initialize daemon's work queue */
94 cdevsw_add(&vinum_cdevsw); /* add the cdevsw entry */
96 /* allocate space: drives... */
97 DRIVE = (struct drive *) Malloc(sizeof(struct drive) * INITIAL_DRIVES);
98 CHECKALLOC(DRIVE, "vinum: no memory\n");
99 bzero(DRIVE, sizeof(struct drive) * INITIAL_DRIVES);
100 vinum_conf.drives_allocated = INITIAL_DRIVES; /* number of drive slots allocated */
101 vinum_conf.drives_used = 0; /* and number in use */
104 VOL = (struct volume *) Malloc(sizeof(struct volume) * INITIAL_VOLUMES);
105 CHECKALLOC(VOL, "vinum: no memory\n");
106 bzero(VOL, sizeof(struct volume) * INITIAL_VOLUMES);
107 vinum_conf.volumes_allocated = INITIAL_VOLUMES; /* number of volume slots allocated */
108 vinum_conf.volumes_used = 0; /* and number in use */
111 PLEX = (struct plex *) Malloc(sizeof(struct plex) * INITIAL_PLEXES);
112 CHECKALLOC(PLEX, "vinum: no memory\n");
113 bzero(PLEX, sizeof(struct plex) * INITIAL_PLEXES);
114 vinum_conf.plexes_allocated = INITIAL_PLEXES; /* number of plex slots allocated */
115 vinum_conf.plexes_used = 0; /* and number in use */
118 SD = (struct sd *) Malloc(sizeof(struct sd) * INITIAL_SUBDISKS);
119 CHECKALLOC(SD, "vinum: no memory\n");
120 bzero(SD, sizeof(struct sd) * INITIAL_SUBDISKS);
121 vinum_conf.subdisks_allocated = INITIAL_SUBDISKS; /* number of sd slots allocated */
122 vinum_conf.subdisks_used = 0; /* and number in use */
125 * See if the loader has passed us a disk to
126 * read the initial configuration from.
128 if ((cp = getenv("vinum.drives")) != NULL) {
129 for (cp1 = cp, i = 0, drives = 0; *cp1 != '\0'; i++) {
131 while (*cp1 != '\0' && *cp1 != ',' && *cp1 != ' ')
135 drives = realloc(drives, (unsigned long)((i + 1) * sizeof(char *)),
141 rv = vinum_scandisk(drives, i);
143 log(LOG_NOTICE, "vinum_scandisk() returned %d", rv);
145 free(drives, M_TEMP);
147 if ((cp = getenv("vinum.root")) != NULL) {
148 for (i = 0; i < vinum_conf.volumes_used; i++) {
149 vol = &vinum_conf.volume[i];
150 if ((vol->state == volume_up)
151 && (strcmp (vol->name, cp) == 0) ) {
152 rootdev = makedev(VINUM_CDEV_MAJOR, i);
153 log(LOG_INFO, "vinum: using volume %s for root device\n", cp);
161 * Check if we have anything open. If confopen is != 0,
162 * that goes for the super device as well, otherwise
165 * Return 0 if not inactive, 1 if inactive.
168 vinum_inactive(int confopen)
171 int can_do = 1; /* assume we can do it */
173 if (confopen && (vinum_conf.flags & VF_OPEN)) /* open by vinum(8)? */
174 return 0; /* can't do it while we're open */
176 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
177 if ((VOL[i].state > volume_down)
178 && (VOL[i].flags & VF_OPEN)) { /* volume is open */
188 * Free all structures.
189 * If cleardrive is 0, save the configuration; otherwise
190 * remove the configuration from the drive.
192 * Before coming here, ensure that no volumes are open.
195 free_vinum(int cleardrive)
198 int drives_allocated = vinum_conf.drives_allocated;
201 if (cleardrive) { /* remove the vinum config */
202 for (i = 0; i < drives_allocated; i++)
203 remove_drive(i); /* remove the drive */
204 } else { /* keep the config */
205 for (i = 0; i < drives_allocated; i++)
206 free_drive(&DRIVE[i]); /* close files and things */
210 while ((vinum_conf.flags & (VF_STOPPING | VF_DAEMONOPEN))
211 == (VF_STOPPING | VF_DAEMONOPEN)) { /* at least one daemon open, we're stopping */
212 queue_daemon_request(daemonrq_return, (union daemoninfo) 0); /* stop the daemon */
213 tsleep(&vinumclose, 0, "vstop", 1); /* and wait for it */
218 for (i = 0; i < vinum_conf.plexes_allocated; i++) {
219 struct plex *plex = &vinum_conf.plex[i];
221 if (plex->state != plex_unallocated) { /* we have real data there */
230 bzero(&vinum_conf, sizeof(vinum_conf));
234 vinum_modevent(module_t mod, modeventtype_t type, void *unused)
236 struct sync_args dummyarg = {0};
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(&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 */
296 vinumopen(dev_t dev, int flags, int fmt, d_thread_t *td)
303 int devminor; /* minor number */
305 devminor = minor(dev);
307 /* First, decide what we're looking at */
308 switch (DEVTYPE(dev)) {
309 case VINUM_VOLUME_TYPE:
311 if (index >= vinum_conf.volumes_allocated)
312 return ENXIO; /* no such device */
315 switch (vol->state) {
316 case volume_unallocated:
321 vol->flags |= VF_OPEN; /* note we're open */
331 case VINUM_PLEX_TYPE:
332 if (Volno(dev) >= vinum_conf.volumes_allocated)
336 case VINUM_RAWPLEX_TYPE:
337 index = Plexno(dev); /* get plex index in vinum_conf */
338 if (index >= vinum_conf.plexes_allocated)
339 return ENXIO; /* no such device */
342 switch (plex->state) {
343 case plex_referenced:
344 case plex_unallocated:
348 plex->flags |= VF_OPEN; /* note we're open */
353 if ((Volno(dev) >= vinum_conf.volumes_allocated) /* no such volume */
354 ||(Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
355 return ENXIO; /* no such device */
359 case VINUM_RAWSD_TYPE:
360 index = Sdno(dev); /* get the subdisk number */
361 if ((index >= vinum_conf.subdisks_allocated) /* not a valid SD entry */
362 ||(SD[index].state < sd_init)) /* or SD is not real */
363 return ENXIO; /* no such device */
367 * Opening a subdisk is always a special operation, so we
368 * ignore the state as long as it represents a real subdisk
376 sd->flags |= VF_OPEN; /* note we're open */
380 case VINUM_SUPERDEV_TYPE:
381 error = suser(td); /* are we root? */
382 if (error == 0) { /* yes, can do */
383 if (devminor == VINUM_DAEMON_DEV) /* daemon device */
384 vinum_conf.flags |= VF_DAEMONOPEN; /* we're open */
385 else if (devminor == VINUM_SUPERDEV)
386 vinum_conf.flags |= VF_OPEN; /* we're open */
388 error = ENODEV; /* nothing, maybe a debug mismatch */
392 /* Vinum drives are disks. We already have a disk
393 * driver, so don't handle them here */
394 case VINUM_DRIVE_TYPE:
396 return ENODEV; /* don't know what to do with these */
402 vinumclose(dev_t dev, int flags, int fmt, d_thread_t *td)
408 devminor = minor(dev);
410 /* First, decide what we're looking at */
411 switch (DEVTYPE(dev)) {
412 case VINUM_VOLUME_TYPE:
413 if (index >= vinum_conf.volumes_allocated)
414 return ENXIO; /* no such device */
417 switch (vol->state) {
418 case volume_unallocated:
423 vol->flags &= ~VF_OPEN; /* reset our flags */
433 case VINUM_PLEX_TYPE:
434 if (Volno(dev) >= vinum_conf.volumes_allocated)
438 case VINUM_RAWPLEX_TYPE:
439 index = Plexno(dev); /* get plex index in vinum_conf */
440 if (index >= vinum_conf.plexes_allocated)
441 return ENXIO; /* no such device */
442 PLEX[index].flags &= ~VF_OPEN; /* reset our flags */
446 if ((Volno(dev) >= vinum_conf.volumes_allocated) || /* no such volume */
447 (Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
448 return ENXIO; /* no such device */
451 case VINUM_RAWSD_TYPE:
452 index = Sdno(dev); /* get the subdisk number */
453 if (index >= vinum_conf.subdisks_allocated)
454 return ENXIO; /* no such device */
455 SD[index].flags &= ~VF_OPEN; /* reset our flags */
458 case VINUM_SUPERDEV_TYPE:
460 * don't worry about whether we're root:
461 * nobody else would get this far.
463 if (devminor == VINUM_SUPERDEV) /* normal superdev */
464 vinum_conf.flags &= ~VF_OPEN; /* no longer open */
465 else if (devminor == VINUM_DAEMON_DEV) { /* the daemon device */
466 vinum_conf.flags &= ~VF_DAEMONOPEN; /* no longer open */
467 if (vinum_conf.flags & VF_STOPPING) /* we're stopping, */
468 wakeup(&vinumclose); /* we can continue stopping now */
472 case VINUM_DRIVE_TYPE:
474 return ENODEV; /* don't know what to do with these */
485 vol = &VOL[Volno(dev)];
487 if (vol->state == volume_up)
490 return 0; /* err on the size of conservatism */
498 /* Not implemented. */
502 /* Local Variables: */
503 /* fill-column: 50 */