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.20 2007/05/15 22:44:12 dillon Exp $
43 #define STATIC static /* nothing while we're testing XXX */
46 #include <sys/sysproto.h> /* for sync(2) */
47 #include <sys/devicestat.h>
49 #include <sys/event.h>
51 #include <sys/reboot.h>
53 extern int total_malloced;
54 extern int malloccount;
55 extern struct mc malloced[];
59 struct dev_ops vinum_ops =
61 { "vinum", VINUM_CDEV_MAJOR, D_DISK | D_KQFILTER },
63 .d_close = vinumclose,
66 .d_ioctl = vinumioctl,
68 .d_kqfilter = vinumkqfilter,
69 .d_strategy = vinumstrategy,
74 /* Called by main() during pseudo-device attachment. */
75 STATIC void vinumattach(void *);
77 STATIC int vinum_modevent(module_t mod, modeventtype_t type, void *unused);
78 STATIC void vinum_initconf(void);
80 struct _vinum_conf vinum_conf; /* configuration information */
81 cdev_t vinum_super_dev;
82 cdev_t vinum_wsuper_dev;
83 cdev_t vinum_daemon_dev;
86 * Called by main() during pseudo-device attachment. All we need
87 * to do is allocate enough space for devices to be configured later, and
91 vinumattach(void *dummy)
93 char *cp, *cp1, *cp2, **drives;
97 /* modload should prevent multiple loads, so this is worth a panic */
98 if ((vinum_conf.flags & VF_LOADED) != 0)
99 panic("vinum: already loaded");
101 log(LOG_INFO, "vinum: loaded\n");
102 vinum_conf.flags |= VF_LOADED; /* we're loaded now */
104 daemonq = NULL; /* initialize daemon's work queue */
108 dev_ops_add(&vinum_ops, 0, 0);
114 * Create superdev, wrongsuperdev, and controld devices.
116 vinum_super_dev = make_dev(&vinum_ops, VINUM_SUPERDEV,
117 UID_ROOT, GID_WHEEL, 0600,
118 VINUM_SUPERDEV_BASE);
119 vinum_wsuper_dev = make_dev(&vinum_ops, VINUM_WRONGSUPERDEV,
120 UID_ROOT, GID_WHEEL, 0600,
121 VINUM_WRONGSUPERDEV_BASE);
122 vinum_daemon_dev = make_dev(&vinum_ops, VINUM_DAEMON_DEV,
123 UID_ROOT, GID_WHEEL, 0600,
124 VINUM_DAEMON_DEV_BASE);
127 * See if the loader has passed us a disk to
128 * read the initial configuration from.
130 if ((cp = kgetenv("vinum.drives")) != NULL) {
131 for (cp1 = cp, i = 0, drives = 0; *cp1 != '\0'; i++) {
133 while (*cp1 != '\0' && *cp1 != ',' && *cp1 != ' ')
137 drives = krealloc(drives, (unsigned long)((i + 1) * sizeof(char *)),
143 rv = vinum_scandisk(drives, i);
145 log(LOG_NOTICE, "vinum_scandisk() returned %d", rv);
147 kfree(drives, M_TEMP);
149 if ((cp = kgetenv("vinum.root")) != NULL) {
150 for (i = 0; i < vinum_conf.volumes_used; i++) {
151 vol = &vinum_conf.volume[i];
152 if ((vol->state == volume_up)
153 && (strcmp (vol->name, cp) == 0)
155 rootdev = make_dev(&vinum_ops, i, UID_ROOT, GID_OPERATOR,
156 0640, VINUM_BASE "vinumroot");
157 log(LOG_INFO, "vinum: using volume %s for root device\n", cp);
165 * Check if we have anything open. If confopen is != 0,
166 * that goes for the super device as well, otherwise
169 * Return 0 if not inactive, 1 if inactive.
172 vinum_inactive(int confopen)
175 int can_do = 1; /* assume we can do it */
177 if (confopen && (vinum_conf.flags & VF_OPEN)) /* open by vinum(8)? */
178 return 0; /* can't do it while we're open */
180 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
181 if ((VOL[i].state > volume_down)
182 && (VOL[i].flags & VF_OPEN)) { /* volume is open */
192 * Free all structures.
193 * If cleardrive is 0, save the configuration; otherwise
194 * remove the configuration from the drive.
196 * Before coming here, ensure that no volumes are open.
199 free_vinum(int cleardrive)
202 int drives_allocated = vinum_conf.drives_allocated;
205 if (cleardrive) { /* remove the vinum config */
206 for (i = 0; i < drives_allocated; i++)
207 remove_drive(i); /* remove the drive */
208 } else { /* keep the config */
209 for (i = 0; i < drives_allocated; i++)
210 free_drive(&DRIVE[i]); /* close files and things */
214 while ((vinum_conf.flags & (VF_STOPPING | VF_DAEMONOPEN))
215 == (VF_STOPPING | VF_DAEMONOPEN)) { /* at least one daemon open, we're stopping */
216 queue_daemon_request(daemonrq_return, (union daemoninfo) 0); /* stop the daemon */
217 tsleep(&vinumclose, 0, "vstop", 1); /* and wait for it */
220 for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
221 struct sd *sd = &vinum_conf.sd[i];
223 destroy_dev(sd->sd_dev);
230 for (i = 0; i < vinum_conf.plexes_allocated; i++) {
231 struct plex *plex = &vinum_conf.plex[i];
233 if (plex->plex_dev) {
234 destroy_dev(plex->plex_dev);
235 plex->plex_dev = NULL;
238 if (plex->state != plex_unallocated) { /* we have real data there */
246 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
247 struct volume *vol = &vinum_conf.volume[i];
250 destroy_dev(vol->vol_dev);
256 bzero(&vinum_conf, sizeof(vinum_conf));
263 vinum_conf.physbufs = nswbuf / 2 + 1;
265 /* allocate space: drives... */
266 DRIVE = (struct drive *) Malloc(sizeof(struct drive) * INITIAL_DRIVES);
267 CHECKALLOC(DRIVE, "vinum: no memory\n");
268 bzero(DRIVE, sizeof(struct drive) * INITIAL_DRIVES);
269 vinum_conf.drives_allocated = INITIAL_DRIVES;
270 vinum_conf.drives_used = 0;
273 VOL = (struct volume *) Malloc(sizeof(struct volume) * INITIAL_VOLUMES);
274 CHECKALLOC(VOL, "vinum: no memory\n");
275 bzero(VOL, sizeof(struct volume) * INITIAL_VOLUMES);
276 vinum_conf.volumes_allocated = INITIAL_VOLUMES;
277 vinum_conf.volumes_used = 0;
280 PLEX = (struct plex *) Malloc(sizeof(struct plex) * INITIAL_PLEXES);
281 CHECKALLOC(PLEX, "vinum: no memory\n");
282 bzero(PLEX, sizeof(struct plex) * INITIAL_PLEXES);
283 vinum_conf.plexes_allocated = INITIAL_PLEXES;
284 vinum_conf.plexes_used = 0;
287 SD = (struct sd *) Malloc(sizeof(struct sd) * INITIAL_SUBDISKS);
288 CHECKALLOC(SD, "vinum: no memory\n");
289 bzero(SD, sizeof(struct sd) * INITIAL_SUBDISKS);
290 vinum_conf.subdisks_allocated = INITIAL_SUBDISKS;
291 vinum_conf.subdisks_used = 0;
295 vinum_modevent(module_t mod, modeventtype_t type, void *unused)
302 if (!vinum_inactive(1)) /* is anything open? */
303 return EBUSY; /* yes, we can't do it */
304 vinum_conf.flags |= VF_STOPPING; /* note that we want to stop */
305 sys_sync(NULL); /* write out buffers */
306 free_vinum(0); /* clean up */
308 if (vinum_super_dev) {
309 destroy_dev(vinum_super_dev);
310 vinum_super_dev = NULL;
312 if (vinum_wsuper_dev) {
313 destroy_dev(vinum_wsuper_dev);
314 vinum_wsuper_dev = NULL;
316 if (vinum_daemon_dev) {
317 destroy_dev(vinum_daemon_dev);
318 vinum_daemon_dev = NULL;
323 if (total_malloced) {
329 for (i = 0; i < malloccount; i++) {
330 if (debug & DEBUG_WARNINGS) /* want to hear about them */
332 "vinum: exiting with %d bytes malloced from %s:%d\n",
337 poke = &((int *) malloced[i].address)
338 [malloced[i].size / (2 * sizeof(int))]; /* middle of the area */
339 if (*poke == 0xdeadc0de) /* already freed */
341 "vinum: exiting with malloc table inconsistency at %p from %s:%d\n",
346 Free(malloced[i].address);
350 dev_ops_remove_all(&vinum_ops);
351 log(LOG_INFO, "vinum: unloaded\n"); /* tell the world */
359 moduledata_t vinum_mod =
362 (modeventhand_t) vinum_modevent,
365 DECLARE_MODULE(vinum, vinum_mod, SI_SUB_RAID, SI_ORDER_MIDDLE);
368 /* Open a vinum object */
370 vinumopen(struct dev_open_args *ap)
372 cdev_t dev = ap->a_head.a_dev;
378 int devminor; /* minor number */
380 devminor = minor(dev);
382 /* First, decide what we're looking at */
383 switch (DEVTYPE(dev)) {
384 case VINUM_VOLUME_TYPE:
386 if (index >= vinum_conf.volumes_allocated)
387 return ENXIO; /* no such device */
390 switch (vol->state) {
391 case volume_unallocated:
396 vol->flags |= VF_OPEN; /* note we're open */
406 case VINUM_PLEX_TYPE:
407 if (Volno(dev) >= vinum_conf.volumes_allocated)
411 case VINUM_RAWPLEX_TYPE:
412 index = Plexno(dev); /* get plex index in vinum_conf */
413 if (index >= vinum_conf.plexes_allocated)
414 return ENXIO; /* no such device */
417 switch (plex->state) {
418 case plex_referenced:
419 case plex_unallocated:
423 plex->flags |= VF_OPEN; /* note we're open */
428 if ((Volno(dev) >= vinum_conf.volumes_allocated) /* no such volume */
429 ||(Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
430 return ENXIO; /* no such device */
434 case VINUM_RAWSD_TYPE:
435 index = Sdno(dev); /* get the subdisk number */
436 if ((index >= vinum_conf.subdisks_allocated) /* not a valid SD entry */
437 ||(SD[index].state < sd_init)) /* or SD is not real */
438 return ENXIO; /* no such device */
442 * Opening a subdisk is always a special operation, so we
443 * ignore the state as long as it represents a real subdisk
451 sd->flags |= VF_OPEN; /* note we're open */
455 case VINUM_SUPERDEV_TYPE:
456 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); /* are we root? */
457 if (error == 0) { /* yes, can do */
458 if (devminor == VINUM_DAEMON_DEV) /* daemon device */
459 vinum_conf.flags |= VF_DAEMONOPEN; /* we're open */
460 else if (devminor == VINUM_SUPERDEV)
461 vinum_conf.flags |= VF_OPEN; /* we're open */
463 error = ENODEV; /* nothing, maybe a debug mismatch */
467 /* Vinum drives are disks. We already have a disk
468 * driver, so don't handle them here */
469 case VINUM_DRIVE_TYPE:
471 return ENODEV; /* don't know what to do with these */
477 vinumclose(struct dev_close_args *ap)
479 cdev_t dev = ap->a_head.a_dev;
484 devminor = minor(dev);
486 /* First, decide what we're looking at */
487 switch (DEVTYPE(dev)) {
488 case VINUM_VOLUME_TYPE:
489 if (index >= vinum_conf.volumes_allocated)
490 return ENXIO; /* no such device */
493 switch (vol->state) {
494 case volume_unallocated:
499 vol->flags &= ~VF_OPEN; /* reset our flags */
509 case VINUM_PLEX_TYPE:
510 if (Volno(dev) >= vinum_conf.volumes_allocated)
514 case VINUM_RAWPLEX_TYPE:
515 index = Plexno(dev); /* get plex index in vinum_conf */
516 if (index >= vinum_conf.plexes_allocated)
517 return ENXIO; /* no such device */
518 PLEX[index].flags &= ~VF_OPEN; /* reset our flags */
522 if ((Volno(dev) >= vinum_conf.volumes_allocated) || /* no such volume */
523 (Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
524 return ENXIO; /* no such device */
527 case VINUM_RAWSD_TYPE:
528 index = Sdno(dev); /* get the subdisk number */
529 if (index >= vinum_conf.subdisks_allocated)
530 return ENXIO; /* no such device */
531 SD[index].flags &= ~VF_OPEN; /* reset our flags */
534 case VINUM_SUPERDEV_TYPE:
536 * don't worry about whether we're root:
537 * nobody else would get this far.
539 if (devminor == VINUM_SUPERDEV) /* normal superdev */
540 vinum_conf.flags &= ~VF_OPEN; /* no longer open */
541 else if (devminor == VINUM_DAEMON_DEV) { /* the daemon device */
542 vinum_conf.flags &= ~VF_DAEMONOPEN; /* no longer open */
543 if (vinum_conf.flags & VF_STOPPING) /* we're stopping, */
544 wakeup(&vinumclose); /* we can continue stopping now */
548 case VINUM_DRIVE_TYPE:
550 return ENODEV; /* don't know what to do with these */
556 vinumsize(struct dev_psize_args *ap)
558 cdev_t dev = ap->a_head.a_dev;
561 vol = &VOL[Volno(dev)];
563 if (vol->state == volume_up) {
564 ap->a_result = (int64_t)vol->size;
572 vinumdump(struct dev_dump_args *ap)
574 /* Not implemented. */
579 vinumpoll(struct dev_poll_args *ap)
581 ap->a_events = seltrue(ap->a_head.a_dev, ap->a_events);
586 vinumfilt_detach(struct knote *kn) {}
589 vinumfilt(struct knote *kn, long hint)
591 cdev_t dev = (cdev_t)kn->kn_hook;
593 if (seltrue(dev, POLLIN | POLLRDNORM))
599 struct filterops vinumfiltops =
600 { 1, NULL, vinumfilt_detach, vinumfilt };
603 vinumkqfilter(struct dev_kqfilter_args *ap)
605 if (ap->a_kn->kn_filter == EVFILT_READ) {
606 ap->a_kn->kn_fop = &vinumfiltops;
607 ap->a_kn->kn_hook = (caddr_t)ap->a_head.a_dev;
610 ap->a_result = EOPNOTSUPP;
616 /* Local Variables: */
617 /* fill-column: 50 */