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 */
45 #include <sys/sysproto.h> /* for sync(2) */
46 #include <sys/devicestat.h>
47 #include <sys/poll.h> /* XXX: poll ops used in kq filters */
48 #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", 0, D_DISK },
63 .d_close = vinumclose,
66 .d_ioctl = vinumioctl,
67 .d_kqfilter = vinumkqfilter,
68 .d_strategy = vinumstrategy,
73 /* Called by main() during pseudo-device attachment. */
74 STATIC void vinumattach(void *);
76 STATIC int vinum_modevent(module_t mod, modeventtype_t type, void *unused);
77 STATIC void vinum_initconf(void);
79 struct _vinum_conf vinum_conf; /* configuration information */
80 cdev_t vinum_super_dev;
81 cdev_t vinum_wsuper_dev;
82 cdev_t vinum_daemon_dev;
85 * Called by main() during pseudo-device attachment. All we need
86 * to do is allocate enough space for devices to be configured later, and
90 vinumattach(void *dummy)
92 char *cp, *cp1, *cp2, **drives;
96 /* modload should prevent multiple loads, so this is worth a panic */
97 if ((vinum_conf.flags & VF_LOADED) != 0)
98 panic("vinum: already loaded");
100 log(LOG_INFO, "vinum: loaded\n");
101 vinum_conf.flags |= VF_LOADED; /* we're loaded now */
103 daemonq = NULL; /* initialize daemon's work queue */
107 dev_ops_add(&vinum_ops, 0, 0);
113 * Create superdev, wrongsuperdev, and controld devices.
115 vinum_super_dev = make_dev(&vinum_ops, VINUM_SUPERDEV,
116 UID_ROOT, GID_WHEEL, 0600,
117 VINUM_SUPERDEV_BASE);
118 vinum_wsuper_dev = make_dev(&vinum_ops, VINUM_WRONGSUPERDEV,
119 UID_ROOT, GID_WHEEL, 0600,
120 VINUM_WRONGSUPERDEV_BASE);
121 vinum_daemon_dev = make_dev(&vinum_ops, VINUM_DAEMON_DEV,
122 UID_ROOT, GID_WHEEL, 0600,
123 VINUM_DAEMON_DEV_BASE);
126 * See if the loader has passed us a disk to
127 * read the initial configuration from.
129 if ((cp = kgetenv("vinum.drives")) != NULL) {
130 for (cp1 = cp, i = 0, drives = NULL; *cp1 != '\0'; i++) {
132 while (*cp1 != '\0' && *cp1 != ',' && *cp1 != ' ')
136 drives = krealloc(drives, (unsigned long)((i + 1) * sizeof(char *)),
142 rv = vinum_scandisk(drives, i);
144 log(LOG_NOTICE, "vinum_scandisk() returned %d", rv);
146 kfree(drives, M_TEMP);
148 if ((cp = kgetenv("vinum.root")) != NULL) {
149 for (i = 0; i < vinum_conf.volumes_used; i++) {
150 vol = &vinum_conf.volume[i];
151 if ((vol->state == volume_up)
152 && (strcmp (vol->name, cp) == 0)
154 rootdev = make_dev(&vinum_ops, i, UID_ROOT, GID_OPERATOR,
155 0640, VINUM_BASE "vinumroot");
156 udev_dict_set_cstr(rootdev, "subsystem", "raid");
157 udev_dict_set_cstr(rootdev, "disk-type", "raid");
158 log(LOG_INFO, "vinum: using volume %s for root device\n", cp);
166 * Check if we have anything open. If confopen is != 0,
167 * that goes for the super device as well, otherwise
170 * Return 0 if not inactive, 1 if inactive.
173 vinum_inactive(int confopen)
176 int can_do = 1; /* assume we can do it */
178 if (confopen && (vinum_conf.flags & VF_OPEN)) /* open by vinum(8)? */
179 return 0; /* can't do it while we're open */
181 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
182 if ((VOL[i].state > volume_down)
183 && (VOL[i].flags & VF_OPEN)) { /* volume is open */
193 * Free all structures.
194 * If cleardrive is 0, save the configuration; otherwise
195 * remove the configuration from the drive.
197 * Before coming here, ensure that no volumes are open.
200 free_vinum(int cleardrive)
202 union daemoninfo di = { .nothing = 0 };
204 int drives_allocated = vinum_conf.drives_allocated;
207 if (cleardrive) { /* remove the vinum config */
208 for (i = 0; i < drives_allocated; i++)
209 remove_drive(i); /* remove the drive */
210 } else { /* keep the config */
211 for (i = 0; i < drives_allocated; i++)
212 free_drive(&DRIVE[i]); /* close files and things */
216 while ((vinum_conf.flags & (VF_STOPPING | VF_DAEMONOPEN))
217 == (VF_STOPPING | VF_DAEMONOPEN)) { /* at least one daemon open, we're stopping */
218 queue_daemon_request(daemonrq_return, di); /* stop the daemon */
219 tsleep(&vinumclose, 0, "vstop", 1); /* and wait for it */
222 for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
223 struct sd *sd = &vinum_conf.sd[i];
225 destroy_dev(sd->sd_dev);
232 for (i = 0; i < vinum_conf.plexes_allocated; i++) {
233 struct plex *plex = &vinum_conf.plex[i];
235 if (plex->plex_dev) {
236 destroy_dev(plex->plex_dev);
237 plex->plex_dev = NULL;
240 if (plex->state != plex_unallocated) { /* we have real data there */
248 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
249 struct volume *vol = &vinum_conf.volume[i];
252 destroy_dev(vol->vol_dev);
258 bzero(&vinum_conf, sizeof(vinum_conf));
265 vinum_conf.physbufs = nswbuf / 2 + 1;
267 /* allocate space: drives... */
268 DRIVE = (struct drive *) Malloc(sizeof(struct drive) * INITIAL_DRIVES);
269 CHECKALLOC(DRIVE, "vinum: no memory\n");
270 bzero(DRIVE, sizeof(struct drive) * INITIAL_DRIVES);
271 vinum_conf.drives_allocated = INITIAL_DRIVES;
272 vinum_conf.drives_used = 0;
275 VOL = (struct volume *) Malloc(sizeof(struct volume) * INITIAL_VOLUMES);
276 CHECKALLOC(VOL, "vinum: no memory\n");
277 bzero(VOL, sizeof(struct volume) * INITIAL_VOLUMES);
278 vinum_conf.volumes_allocated = INITIAL_VOLUMES;
279 vinum_conf.volumes_used = 0;
282 PLEX = (struct plex *) Malloc(sizeof(struct plex) * INITIAL_PLEXES);
283 CHECKALLOC(PLEX, "vinum: no memory\n");
284 bzero(PLEX, sizeof(struct plex) * INITIAL_PLEXES);
285 vinum_conf.plexes_allocated = INITIAL_PLEXES;
286 vinum_conf.plexes_used = 0;
289 SD = (struct sd *) Malloc(sizeof(struct sd) * INITIAL_SUBDISKS);
290 CHECKALLOC(SD, "vinum: no memory\n");
291 bzero(SD, sizeof(struct sd) * INITIAL_SUBDISKS);
292 vinum_conf.subdisks_allocated = INITIAL_SUBDISKS;
293 vinum_conf.subdisks_used = 0;
297 vinum_modevent(module_t mod, modeventtype_t type, void *unused)
304 if (!vinum_inactive(1)) /* is anything open? */
305 return EBUSY; /* yes, we can't do it */
306 vinum_conf.flags |= VF_STOPPING; /* note that we want to stop */
307 sys_sync(NULL); /* write out buffers */
308 free_vinum(0); /* clean up */
310 if (vinum_super_dev) {
311 destroy_dev(vinum_super_dev);
312 vinum_super_dev = NULL;
314 if (vinum_wsuper_dev) {
315 destroy_dev(vinum_wsuper_dev);
316 vinum_wsuper_dev = NULL;
318 if (vinum_daemon_dev) {
319 destroy_dev(vinum_daemon_dev);
320 vinum_daemon_dev = NULL;
325 if (total_malloced) {
331 for (i = 0; i < malloccount; i++) {
332 if (debug & DEBUG_WARNINGS) /* want to hear about them */
334 "vinum: exiting with %d bytes malloced from %s:%d\n",
339 poke = &((int *) malloced[i].address)
340 [malloced[i].size / (2 * sizeof(int))]; /* middle of the area */
341 if (*poke == 0xdeadc0de) /* already freed */
343 "vinum: exiting with malloc table inconsistency at %p from %s:%d\n",
348 Free(malloced[i].address);
352 dev_ops_remove_all(&vinum_ops);
353 log(LOG_INFO, "vinum: unloaded\n"); /* tell the world */
361 moduledata_t vinum_mod =
364 (modeventhand_t) vinum_modevent,
367 DECLARE_MODULE(vinum, vinum_mod, SI_SUB_RAID, SI_ORDER_MIDDLE);
370 /* Open a vinum object */
372 vinumopen(struct dev_open_args *ap)
374 cdev_t dev = ap->a_head.a_dev;
380 int devminor; /* minor number */
382 devminor = minor(dev);
384 /* First, decide what we're looking at */
385 switch (DEVTYPE(dev)) {
386 case VINUM_VOLUME_TYPE:
388 if (index >= vinum_conf.volumes_allocated)
389 return ENXIO; /* no such device */
392 switch (vol->state) {
393 case volume_unallocated:
398 vol->flags |= VF_OPEN; /* note we're open */
408 case VINUM_PLEX_TYPE:
409 if (Volno(dev) >= vinum_conf.volumes_allocated)
413 case VINUM_RAWPLEX_TYPE:
414 index = Plexno(dev); /* get plex index in vinum_conf */
415 if (index >= vinum_conf.plexes_allocated)
416 return ENXIO; /* no such device */
419 switch (plex->state) {
420 case plex_referenced:
421 case plex_unallocated:
425 plex->flags |= VF_OPEN; /* note we're open */
430 if ((Volno(dev) >= vinum_conf.volumes_allocated) /* no such volume */
431 ||(Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
432 return ENXIO; /* no such device */
436 case VINUM_RAWSD_TYPE:
437 index = Sdno(dev); /* get the subdisk number */
438 if ((index >= vinum_conf.subdisks_allocated) /* not a valid SD entry */
439 ||(SD[index].state < sd_init)) /* or SD is not real */
440 return ENXIO; /* no such device */
444 * Opening a subdisk is always a special operation, so we
445 * ignore the state as long as it represents a real subdisk
453 sd->flags |= VF_OPEN; /* note we're open */
457 case VINUM_SUPERDEV_TYPE:
458 error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0); /* are we root? */
459 if (error == 0) { /* yes, can do */
460 if (devminor == VINUM_DAEMON_DEV) /* daemon device */
461 vinum_conf.flags |= VF_DAEMONOPEN; /* we're open */
462 else if (devminor == VINUM_SUPERDEV)
463 vinum_conf.flags |= VF_OPEN; /* we're open */
465 error = ENODEV; /* nothing, maybe a debug mismatch */
469 /* Vinum drives are disks. We already have a disk
470 * driver, so don't handle them here */
471 case VINUM_DRIVE_TYPE:
473 return ENODEV; /* don't know what to do with these */
479 vinumclose(struct dev_close_args *ap)
481 cdev_t dev = ap->a_head.a_dev;
486 devminor = minor(dev);
488 /* First, decide what we're looking at */
489 switch (DEVTYPE(dev)) {
490 case VINUM_VOLUME_TYPE:
491 if (index >= vinum_conf.volumes_allocated)
492 return ENXIO; /* no such device */
495 switch (vol->state) {
496 case volume_unallocated:
501 vol->flags &= ~VF_OPEN; /* reset our flags */
511 case VINUM_PLEX_TYPE:
512 if (Volno(dev) >= vinum_conf.volumes_allocated)
516 case VINUM_RAWPLEX_TYPE:
517 index = Plexno(dev); /* get plex index in vinum_conf */
518 if (index >= vinum_conf.plexes_allocated)
519 return ENXIO; /* no such device */
520 PLEX[index].flags &= ~VF_OPEN; /* reset our flags */
524 if ((Volno(dev) >= vinum_conf.volumes_allocated) || /* no such volume */
525 (Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
526 return ENXIO; /* no such device */
529 case VINUM_RAWSD_TYPE:
530 index = Sdno(dev); /* get the subdisk number */
531 if (index >= vinum_conf.subdisks_allocated)
532 return ENXIO; /* no such device */
533 SD[index].flags &= ~VF_OPEN; /* reset our flags */
536 case VINUM_SUPERDEV_TYPE:
538 * don't worry about whether we're root:
539 * nobody else would get this far.
541 if (devminor == VINUM_SUPERDEV) /* normal superdev */
542 vinum_conf.flags &= ~VF_OPEN; /* no longer open */
543 else if (devminor == VINUM_DAEMON_DEV) { /* the daemon device */
544 vinum_conf.flags &= ~VF_DAEMONOPEN; /* no longer open */
545 if (vinum_conf.flags & VF_STOPPING) /* we're stopping, */
546 wakeup(&vinumclose); /* we can continue stopping now */
550 case VINUM_DRIVE_TYPE:
552 return ENODEV; /* don't know what to do with these */
558 vinumsize(struct dev_psize_args *ap)
560 cdev_t dev = ap->a_head.a_dev;
563 vol = &VOL[Volno(dev)];
565 if (vol->state == volume_up) {
566 ap->a_result = (int64_t)vol->size;
574 vinumdump(struct dev_dump_args *ap)
576 /* Not implemented. */
581 vinumfilt_detach(struct knote *kn) {}
584 vinumfilt_rd(struct knote *kn, long hint)
586 cdev_t dev = (cdev_t)kn->kn_hook;
588 if (seltrue(dev, POLLIN | POLLRDNORM))
595 vinumfilt_wr(struct knote *kn, long hint)
597 /* Writing is always OK */
601 struct filterops vinumfiltops_rd =
602 { FILTEROP_ISFD, NULL, vinumfilt_detach, vinumfilt_rd };
603 struct filterops vinumfiltops_wr =
604 { FILTEROP_ISFD, NULL, vinumfilt_detach, vinumfilt_wr };
607 vinumkqfilter(struct dev_kqfilter_args *ap)
609 if (ap->a_kn->kn_filter == EVFILT_READ) {
610 ap->a_kn->kn_fop = &vinumfiltops_rd;
611 ap->a_kn->kn_hook = (caddr_t)ap->a_head.a_dev;
613 } else if (ap->a_kn->kn_filter == EVFILT_WRITE) {
614 ap->a_kn->kn_fop = &vinumfiltops_wr;
617 ap->a_result = EOPNOTSUPP;
623 /* Local Variables: */
624 /* fill-column: 50 */