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.34 2001/05/22 04:07:22 grog Exp grog $
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 struct cdevsw vinum_cdevsw =
58 vinumopen, vinumclose, physread, physwrite,
59 vinumioctl, seltrue, nommap, vinumstrategy,
60 "vinum", VINUM_CDEV_MAJOR, nodump, vinumsize,
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 */
71 dev_t vinum_daemon_dev;
72 dev_t vinum_super_dev;
75 * Called by main() during pseudo-device attachment. All we need
76 * to do is allocate enough space for devices to be configured later, and
80 vinumattach(void *dummy)
82 /* modload should prevent multiple loads, so this is worth a panic */
83 if ((vinum_conf.flags & VF_LOADED) != 0)
84 panic("vinum: already loaded");
86 log(LOG_INFO, "vinum: loaded\n");
88 vinum_conf.flags |= VF_LOADED | VF_HASDEBUG; /* we're loaded now, and we support debug */
90 vinum_conf.flags |= VF_LOADED; /* we're loaded now */
93 daemonq = NULL; /* initialize daemon's work queue */
96 vinum_daemon_dev = make_dev(&vinum_cdevsw,
102 vinum_super_dev = make_dev(&vinum_cdevsw,
109 vinum_conf.version = VINUMVERSION; /* note what version we are */
111 /* allocate space: drives... */
112 DRIVE = (struct drive *) Malloc(sizeof(struct drive) * INITIAL_DRIVES);
113 CHECKALLOC(DRIVE, "vinum: no memory\n");
114 bzero(DRIVE, sizeof(struct drive) * INITIAL_DRIVES);
115 vinum_conf.drives_allocated = INITIAL_DRIVES; /* number of drive slots allocated */
116 vinum_conf.drives_used = 0; /* and number in use */
119 VOL = (struct volume *) Malloc(sizeof(struct volume) * INITIAL_VOLUMES);
120 CHECKALLOC(VOL, "vinum: no memory\n");
121 bzero(VOL, sizeof(struct volume) * INITIAL_VOLUMES);
122 vinum_conf.volumes_allocated = INITIAL_VOLUMES; /* number of volume slots allocated */
123 vinum_conf.volumes_used = 0; /* and number in use */
126 PLEX = (struct plex *) Malloc(sizeof(struct plex) * INITIAL_PLEXES);
127 CHECKALLOC(PLEX, "vinum: no memory\n");
128 bzero(PLEX, sizeof(struct plex) * INITIAL_PLEXES);
129 vinum_conf.plexes_allocated = INITIAL_PLEXES; /* number of plex slots allocated */
130 vinum_conf.plexes_used = 0; /* and number in use */
133 SD = (struct sd *) Malloc(sizeof(struct sd) * INITIAL_SUBDISKS);
134 CHECKALLOC(SD, "vinum: no memory\n");
135 bzero(SD, sizeof(struct sd) * INITIAL_SUBDISKS);
136 vinum_conf.subdisks_allocated = INITIAL_SUBDISKS; /* number of sd slots allocated */
137 vinum_conf.subdisks_used = 0; /* and number in use */
141 * Check if we have anything open. If confopen is != 0,
142 * that goes for the super device as well, otherwise
145 * Return 0 if not inactive, 1 if inactive.
148 vinum_inactive(int confopen)
151 int can_do = 1; /* assume we can do it */
153 if (confopen && (vinum_conf.flags & VF_OPEN)) /* open by vinum(8)? */
154 return 0; /* can't do it while we're open */
156 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
157 if ((VOL[i].state > volume_down)
158 && (VOL[i].flags & VF_OPEN)) { /* volume is open */
168 * Free all structures.
169 * If cleardrive is 0, save the configuration; otherwise
170 * remove the configuration from the drive.
172 * Before coming here, ensure that no volumes are open.
175 free_vinum(int cleardrive)
178 int drives_allocated = vinum_conf.drives_allocated;
181 if (cleardrive) { /* remove the vinum config */
182 for (i = 0; i < drives_allocated; i++)
183 remove_drive(i); /* remove the drive */
184 } else { /* keep the config */
185 for (i = 0; i < drives_allocated; i++)
186 free_drive(&DRIVE[i]); /* close files and things */
190 while ((vinum_conf.flags & (VF_STOPPING | VF_DAEMONOPEN))
191 == (VF_STOPPING | VF_DAEMONOPEN)) { /* at least one daemon open, we're stopping */
192 queue_daemon_request(daemonrq_return, (union daemoninfo) 0); /* stop the daemon */
193 tsleep(&vinumclose, PUSER, "vstop", 1); /* and wait for it */
196 for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
197 struct sd *sd = &SD[i];
199 if (sd->state != sd_unallocated)
205 for (i = 0; i < vinum_conf.plexes_allocated; i++) {
206 struct plex *plex = &PLEX[i];
208 if (plex->state != plex_unallocated) /* we have real data there */
214 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
215 struct volume *volume = &VOL[i];
217 if (volume->state != volume_unallocated)
222 bzero(&vinum_conf, sizeof(vinum_conf));
226 vinum_modevent(module_t mod, modeventtype_t type, void *unused)
228 struct sync_args dummyarg =
236 if (!vinum_inactive(1)) /* is anything open? */
237 return EBUSY; /* yes, we can't do it */
238 vinum_conf.flags |= VF_STOPPING; /* note that we want to stop */
239 sync(curthread, &dummyarg); /* write out buffers */
240 free_vinum(0); /* clean up */
242 if (total_malloced) {
248 for (i = 0; i < malloccount; i++) {
249 if (debug & DEBUG_WARNINGS) /* want to hear about them */
251 "vinum: exiting with %d bytes malloced from %s:%d\n",
256 poke = &((int *) malloced[i].address)
257 [malloced[i].size / (2 * sizeof(int))]; /* middle of the area */
258 if (*poke == 0xdeadc0de) /* already freed */
260 "vinum: exiting with malloc table inconsistency at %p from %s:%d\n",
265 Free(malloced[i].address);
269 destroy_dev(vinum_daemon_dev); /* daemon device */
270 destroy_dev(vinum_super_dev);
271 log(LOG_INFO, "vinum: unloaded\n"); /* tell the world */
279 static moduledata_t vinum_mod =
282 (modeventhand_t) vinum_modevent,
285 DECLARE_MODULE(vinum, vinum_mod, SI_SUB_VINUM, SI_ORDER_MIDDLE);
288 /* Open a vinum object */
300 int devminor; /* minor number */
302 devminor = minor(dev);
304 /* First, decide what we're looking at */
305 switch (DEVTYPE(dev)) {
306 case VINUM_VOLUME_TYPE:
308 if (index >= vinum_conf.volumes_allocated)
309 return ENXIO; /* no such device */
312 switch (vol->state) {
313 case volume_unallocated:
318 vol->flags |= VF_OPEN; /* note we're open */
328 case VINUM_PLEX_TYPE:
329 if (Volno(dev) >= vinum_conf.volumes_allocated)
333 case VINUM_RAWPLEX_TYPE:
334 index = Plexno(dev); /* get plex index in vinum_conf */
335 if (index >= vinum_conf.plexes_allocated)
336 return ENXIO; /* no such device */
339 switch (plex->state) {
340 case plex_referenced:
341 case plex_unallocated:
345 plex->flags |= VF_OPEN; /* note we're open */
350 if ((Volno(dev) >= vinum_conf.volumes_allocated) /* no such volume */
351 ||(Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
352 return ENXIO; /* no such device */
356 case VINUM_RAWSD_TYPE:
357 index = Sdno(dev); /* get the subdisk number */
358 if ((index >= vinum_conf.subdisks_allocated) /* not a valid SD entry */
359 ||(SD[index].state < sd_init)) /* or SD is not real */
360 return ENXIO; /* no such device */
364 * Opening a subdisk is always a special operation, so we
365 * ignore the state as long as it represents a real subdisk
373 sd->flags |= VF_OPEN; /* note we're open */
377 case VINUM_SUPERDEV_TYPE:
378 error = suser(td); /* are we root? */
379 if (error == 0) { /* yes, can do */
380 if (devminor == VINUM_DAEMON_DEV) /* daemon device */
381 vinum_conf.flags |= VF_DAEMONOPEN; /* we're open */
382 else if (devminor == VINUM_SUPERDEV)
383 vinum_conf.flags |= VF_OPEN; /* we're open */
385 error = ENODEV; /* nothing, maybe a debug mismatch */
389 /* Vinum drives are disks. We already have a disk
390 * driver, so don't handle them here */
391 case VINUM_DRIVE_TYPE:
393 return ENODEV; /* don't know what to do with these */
399 vinumclose(dev_t dev,
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 */
495 /* Local Variables: */
496 /* fill-column: 50 */