File:  [DragonFly] / src / sys / dev / raid / vinum / vinum.c
Revision 1.10: download - view: text, annotated - select for diffs
Thu May 13 23:49:19 2004 UTC (10 years, 7 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
device switch 1/many: Remove d_autoq, add d_clone (where d_autoq was).

d_autoq was used to allow the device port dispatch to mix old-style synchronous
calls with new style messaging calls within a particular device.  It was never
used for that purpose.

d_clone will be more fully implemented as work continues.  We are going to
install d_port in the dev_t (struct specinfo) structure itself and d_clone
will be needed to allow devices to 'revector' the port on a minor-number
by minor-number basis, in particular allowing minor numbers to be directly
dispatched to distinct threads.  This is something we will be needing later
on.

    1: /*-
    2:  * Copyright (c) 1997, 1998
    3:  *	Nan Yang Computer Services Limited.  All rights reserved.
    4:  *
    5:  *  Written by Greg Lehey
    6:  *
    7:  *  This software is distributed under the so-called ``Berkeley
    8:  *  License'':
    9:  *
   10:  * Redistribution and use in source and binary forms, with or without
   11:  * modification, are permitted provided that the following conditions
   12:  * are met:
   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
   21:  *      Services Limited.
   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.
   25:  *
   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.
   37:  *
   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.10 2004/05/13 23:49:19 dillon Exp $
   41:  */
   42: 
   43: #define STATIC static					    /* nothing while we're testing XXX */
   44: 
   45: #include "vinumhdr.h"
   46: #include <sys/sysproto.h>				    /* for sync(2) */
   47: #include <sys/devicestat.h>
   48: #ifdef VINUMDEBUG
   49: #include <sys/reboot.h>
   50: int debug = 0;
   51: extern int total_malloced;
   52: extern int malloccount;
   53: extern struct mc malloced[];
   54: #endif
   55: #include "request.h"
   56: 
   57: STATIC struct cdevsw vinum_cdevsw =
   58: {
   59:     /* name */ "vinum",
   60:     /* cmaj */	VINUM_CDEV_MAJOR, 
   61:     /* flags */ D_DISK,
   62:     /* port */	NULL,
   63:     /* clone */	NULL,
   64: 
   65:     vinumopen, vinumclose, physread, physwrite,
   66:     vinumioctl, seltrue, nommap, vinumstrategy,
   67:     vinumdump, vinumsize,
   68: };
   69: 
   70: /* Called by main() during pseudo-device attachment. */
   71: STATIC void vinumattach(void *);
   72: 
   73: STATIC int vinum_modevent(module_t mod, modeventtype_t type, void *unused);
   74: 
   75: struct _vinum_conf vinum_conf;				    /* configuration information */
   76: 
   77: /*
   78:  * Called by main() during pseudo-device attachment.  All we need
   79:  * to do is allocate enough space for devices to be configured later, and
   80:  * add devsw entries.
   81:  */
   82: void
   83: vinumattach(void *dummy)
   84: {
   85:     char *cp, *cp1, *cp2, **drives;
   86:     int i, rv;
   87:     struct volume *vol;
   88: 
   89:     /* modload should prevent multiple loads, so this is worth a panic */
   90:     if ((vinum_conf.flags & VF_LOADED) != 0)
   91: 	panic("vinum: already loaded");
   92: 
   93:     log(LOG_INFO, "vinum: loaded\n");
   94:     vinum_conf.flags |= VF_LOADED;			    /* we're loaded now */
   95: 
   96:     daemonq = NULL;					    /* initialize daemon's work queue */
   97:     dqend = NULL;
   98: 
   99:     cdevsw_add(&vinum_cdevsw);				    /* add the cdevsw entry */
  100: 
  101:     /* allocate space: drives... */
  102:     DRIVE = (struct drive *) Malloc(sizeof(struct drive) * INITIAL_DRIVES);
  103:     CHECKALLOC(DRIVE, "vinum: no memory\n");
  104:     bzero(DRIVE, sizeof(struct drive) * INITIAL_DRIVES);
  105:     vinum_conf.drives_allocated = INITIAL_DRIVES;	    /* number of drive slots allocated */
  106:     vinum_conf.drives_used = 0;				    /* and number in use */
  107: 
  108:     /* volumes, ... */
  109:     VOL = (struct volume *) Malloc(sizeof(struct volume) * INITIAL_VOLUMES);
  110:     CHECKALLOC(VOL, "vinum: no memory\n");
  111:     bzero(VOL, sizeof(struct volume) * INITIAL_VOLUMES);
  112:     vinum_conf.volumes_allocated = INITIAL_VOLUMES;	    /* number of volume slots allocated */
  113:     vinum_conf.volumes_used = 0;			    /* and number in use */
  114: 
  115:     /* plexes, ... */
  116:     PLEX = (struct plex *) Malloc(sizeof(struct plex) * INITIAL_PLEXES);
  117:     CHECKALLOC(PLEX, "vinum: no memory\n");
  118:     bzero(PLEX, sizeof(struct plex) * INITIAL_PLEXES);
  119:     vinum_conf.plexes_allocated = INITIAL_PLEXES;	    /* number of plex slots allocated */
  120:     vinum_conf.plexes_used = 0;				    /* and number in use */
  121: 
  122:     /* and subdisks */
  123:     SD = (struct sd *) Malloc(sizeof(struct sd) * INITIAL_SUBDISKS);
  124:     CHECKALLOC(SD, "vinum: no memory\n");
  125:     bzero(SD, sizeof(struct sd) * INITIAL_SUBDISKS);
  126:     vinum_conf.subdisks_allocated = INITIAL_SUBDISKS;	    /* number of sd slots allocated */
  127:     vinum_conf.subdisks_used = 0;			    /* and number in use */
  128: 
  129:     /*
  130:      * See if the loader has passed us a disk to
  131:      * read the initial configuration from.
  132:      */
  133:     if ((cp = getenv("vinum.drives")) != NULL) {
  134: 	for (cp1 = cp, i = 0, drives = 0; *cp1 != '\0'; i++) {
  135: 	    cp2 = cp1;
  136: 	    while (*cp1 != '\0' && *cp1 != ',' && *cp1 != ' ')
  137: 		cp1++;
  138: 	    if (*cp1 != '\0')
  139: 		*cp1++ = '\0';
  140: 	    drives = realloc(drives, (unsigned long)((i + 1) * sizeof(char *)),
  141: 			     M_TEMP, M_WAITOK);
  142: 	    drives[i] = cp2;
  143: 	}
  144: 	if (i == 0)
  145: 	    goto bailout;
  146: 	rv = vinum_scandisk(drives, i);
  147: 	if (rv)
  148: 	    log(LOG_NOTICE, "vinum_scandisk() returned %d", rv);
  149:     bailout:
  150: 	free(drives, M_TEMP);
  151:     }
  152:     if ((cp = getenv("vinum.root")) != NULL) {
  153: 	for (i = 0; i < vinum_conf.volumes_used; i++) {
  154: 	    vol = &vinum_conf.volume[i];
  155: 	    if ((vol->state == volume_up)
  156: 		&& (strcmp (vol->name, cp) == 0) ) {
  157: 		rootdev = makedev(VINUM_CDEV_MAJOR, i); 
  158: 		log(LOG_INFO, "vinum: using volume %s for root device\n", cp);
  159: 		break;
  160: 	    }
  161: 	}
  162:     }
  163: }
  164: 
  165: /*
  166:  * Check if we have anything open.  If confopen is != 0,
  167:  * that goes for the super device as well, otherwise
  168:  * only for volumes.
  169:  *
  170:  * Return 0 if not inactive, 1 if inactive.
  171:  */
  172: int
  173: vinum_inactive(int confopen)
  174: {
  175:     int i;
  176:     int can_do = 1;					    /* assume we can do it */
  177: 
  178:     if (confopen && (vinum_conf.flags & VF_OPEN))	    /* open by vinum(8)? */
  179: 	return 0;					    /* can't do it while we're open */
  180:     lock_config();
  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 */
  184: 	    can_do = 0;
  185: 	    break;
  186: 	}
  187:     }
  188:     unlock_config();
  189:     return can_do;
  190: }
  191: 
  192: /*
  193:  * Free all structures.
  194:  * If cleardrive is 0, save the configuration; otherwise
  195:  * remove the configuration from the drive.
  196:  *
  197:  * Before coming here, ensure that no volumes are open.
  198:  */
  199: void
  200: free_vinum(int cleardrive)
  201: {
  202:     int i;
  203:     int drives_allocated = vinum_conf.drives_allocated;
  204: 
  205:     if (DRIVE != NULL) {
  206: 	if (cleardrive) {				    /* remove the vinum config */
  207: 	    for (i = 0; i < drives_allocated; i++)
  208: 		remove_drive(i);			    /* remove the drive */
  209: 	} else {					    /* keep the config */
  210: 	    for (i = 0; i < drives_allocated; i++)
  211: 		free_drive(&DRIVE[i]);			    /* close files and things */
  212: 	}
  213: 	Free(DRIVE);
  214:     }
  215:     while ((vinum_conf.flags & (VF_STOPPING | VF_DAEMONOPEN))
  216: 	== (VF_STOPPING | VF_DAEMONOPEN)) {		    /* at least one daemon open, we're stopping */
  217: 	queue_daemon_request(daemonrq_return, (union daemoninfo) 0); /* stop the daemon */
  218: 	tsleep(&vinumclose, 0, "vstop", 1);		    /* and wait for it */
  219:     }
  220:     if (SD != NULL)
  221: 	Free(SD);
  222:     if (PLEX != NULL) {
  223: 	for (i = 0; i < vinum_conf.plexes_allocated; i++) {
  224: 	    struct plex *plex = &vinum_conf.plex[i];
  225: 
  226: 	    if (plex->state != plex_unallocated) {	    /* we have real data there */
  227: 		if (plex->sdnos)
  228: 		    Free(plex->sdnos);
  229: 	    }
  230: 	}
  231: 	Free(PLEX);
  232:     }
  233:     if (VOL != NULL)
  234: 	Free(VOL);
  235:     bzero(&vinum_conf, sizeof(vinum_conf));
  236: }
  237: 
  238: STATIC int
  239: vinum_modevent(module_t mod, modeventtype_t type, void *unused)
  240: {
  241:     switch (type) {
  242:     case MOD_LOAD:
  243: 	vinumattach(NULL);
  244: 	return 0;					    /* OK */
  245:     case MOD_UNLOAD:
  246: 	if (!vinum_inactive(1))				    /* is anything open? */
  247: 	    return EBUSY;				    /* yes, we can't do it */
  248: 	vinum_conf.flags |= VF_STOPPING;		    /* note that we want to stop */
  249: 	sync(NULL);			    /* write out buffers */
  250: 	free_vinum(0);					    /* clean up */
  251: #ifdef VINUMDEBUG
  252: 	if (total_malloced) {
  253: 	    int i;
  254: #ifdef INVARIANTS
  255: 	    int *poke;
  256: #endif
  257: 
  258: 	    for (i = 0; i < malloccount; i++) {
  259: 		if (debug & DEBUG_WARNINGS)		    /* want to hear about them */
  260: 		    log(LOG_WARNING,
  261: 			"vinum: exiting with %d bytes malloced from %s:%d\n",
  262: 			malloced[i].size,
  263: 			malloced[i].file,
  264: 			malloced[i].line);
  265: #ifdef INVARIANTS
  266: 		poke = &((int *) malloced[i].address)
  267: 		    [malloced[i].size / (2 * sizeof(int))]; /* middle of the area */
  268: 		if (*poke == 0xdeadc0de)		    /* already freed */
  269: 		    log(LOG_ERR,
  270: 			"vinum: exiting with malloc table inconsistency at %p from %s:%d\n",
  271: 			malloced[i].address,
  272: 			malloced[i].file,
  273: 			malloced[i].line);
  274: #endif
  275: 		Free(malloced[i].address);
  276: 	    }
  277: 	}
  278: #endif
  279: 	cdevsw_remove(&vinum_cdevsw);
  280: 	log(LOG_INFO, "vinum: unloaded\n");		    /* tell the world */
  281: 	return 0;
  282:     default:
  283: 	break;
  284:     }
  285:     return 0;
  286: }
  287: 
  288: moduledata_t vinum_mod =
  289: {
  290:     "vinum",
  291:     (modeventhand_t) vinum_modevent,
  292:     0
  293: };
  294: DECLARE_MODULE(vinum, vinum_mod, SI_SUB_RAID, SI_ORDER_MIDDLE);
  295: 
  296: /* ARGSUSED */
  297: /* Open a vinum object */
  298: int
  299: vinumopen(dev_t dev, int flags, int fmt, d_thread_t *td)
  300: {
  301:     int error;
  302:     unsigned int index;
  303:     struct volume *vol;
  304:     struct plex *plex;
  305:     struct sd *sd;
  306:     int devminor;					    /* minor number */
  307: 
  308:     devminor = minor(dev);
  309:     error = 0;
  310:     /* First, decide what we're looking at */
  311:     switch (DEVTYPE(dev)) {
  312:     case VINUM_VOLUME_TYPE:
  313: 	index = Volno(dev);
  314: 	if (index >= vinum_conf.volumes_allocated)
  315: 	    return ENXIO;				    /* no such device */
  316: 	vol = &VOL[index];
  317: 
  318: 	switch (vol->state) {
  319: 	case volume_unallocated:
  320: 	case volume_uninit:
  321: 	    return ENXIO;
  322: 
  323: 	case volume_up:
  324: 	    vol->flags |= VF_OPEN;			    /* note we're open */
  325: 	    return 0;
  326: 
  327: 	case volume_down:
  328: 	    return EIO;
  329: 
  330: 	default:
  331: 	    return EINVAL;
  332: 	}
  333: 
  334:     case VINUM_PLEX_TYPE:
  335: 	if (Volno(dev) >= vinum_conf.volumes_allocated)
  336: 	    return ENXIO;
  337: 	/* FALLTHROUGH */
  338: 
  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 */
  343: 	plex = &PLEX[index];
  344: 
  345: 	switch (plex->state) {
  346: 	case plex_referenced:
  347: 	case plex_unallocated:
  348: 	    return EINVAL;
  349: 
  350: 	default:
  351: 	    plex->flags |= VF_OPEN;			    /* note we're open */
  352: 	    return 0;
  353: 	}
  354: 
  355:     case VINUM_SD_TYPE:
  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 */
  359: 
  360: 	/* FALLTHROUGH */
  361: 
  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 */
  367: 	sd = &SD[index];
  368: 
  369: 	/*
  370: 	 * Opening a subdisk is always a special operation, so we
  371: 	 * ignore the state as long as it represents a real subdisk
  372: 	 */
  373: 	switch (sd->state) {
  374: 	case sd_unallocated:
  375: 	case sd_uninit:
  376: 	    return EINVAL;
  377: 
  378: 	default:
  379: 	    sd->flags |= VF_OPEN;			    /* note we're open */
  380: 	    return 0;
  381: 	}
  382: 
  383:     case VINUM_SUPERDEV_TYPE:
  384: 	error = suser(td);		    /* 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 */
  390: 	    else
  391: 		error = ENODEV;				    /* nothing, maybe a debug mismatch */
  392: 	}
  393: 	return error;
  394: 
  395: 	/* Vinum drives are disks.  We already have a disk
  396: 	 * driver, so don't handle them here */
  397:     case VINUM_DRIVE_TYPE:
  398:     default:
  399: 	return ENODEV;					    /* don't know what to do with these */
  400:     }
  401: }
  402: 
  403: /* ARGSUSED */
  404: int
  405: vinumclose(dev_t dev, int flags, int fmt, d_thread_t *td)
  406: {
  407:     unsigned int index;
  408:     struct volume *vol;
  409:     int devminor;
  410: 
  411:     devminor = minor(dev);
  412:     index = Volno(dev);
  413:     /* First, decide what we're looking at */
  414:     switch (DEVTYPE(dev)) {
  415:     case VINUM_VOLUME_TYPE:
  416: 	if (index >= vinum_conf.volumes_allocated)
  417: 	    return ENXIO;				    /* no such device */
  418: 	vol = &VOL[index];
  419: 
  420: 	switch (vol->state) {
  421: 	case volume_unallocated:
  422: 	case volume_uninit:
  423: 	    return ENXIO;
  424: 
  425: 	case volume_up:
  426: 	    vol->flags &= ~VF_OPEN;			    /* reset our flags */
  427: 	    return 0;
  428: 
  429: 	case volume_down:
  430: 	    return EIO;
  431: 
  432: 	default:
  433: 	    return EINVAL;
  434: 	}
  435: 
  436:     case VINUM_PLEX_TYPE:
  437: 	if (Volno(dev) >= vinum_conf.volumes_allocated)
  438: 	    return ENXIO;
  439: 	/* FALLTHROUGH */
  440: 
  441:     case VINUM_RAWPLEX_TYPE:
  442: 	index = Plexno(dev);				    /* get plex index in vinum_conf */
  443: 	if (index >= vinum_conf.plexes_allocated)
  444: 	    return ENXIO;				    /* no such device */
  445: 	PLEX[index].flags &= ~VF_OPEN;			    /* reset our flags */
  446: 	return 0;
  447: 
  448:     case VINUM_SD_TYPE:
  449: 	if ((Volno(dev) >= vinum_conf.volumes_allocated) || /* no such volume */
  450: 	    (Plexno(dev) >= vinum_conf.plexes_allocated))   /* or no such plex */
  451: 	    return ENXIO;				    /* no such device */
  452: 	/* FALLTHROUGH */
  453: 
  454:     case VINUM_RAWSD_TYPE:
  455: 	index = Sdno(dev);				    /* get the subdisk number */
  456: 	if (index >= vinum_conf.subdisks_allocated)
  457: 	    return ENXIO;				    /* no such device */
  458: 	SD[index].flags &= ~VF_OPEN;			    /* reset our flags */
  459: 	return 0;
  460: 
  461:     case VINUM_SUPERDEV_TYPE:
  462: 	/*
  463: 	 * don't worry about whether we're root:
  464: 	 * nobody else would get this far.
  465: 	 */
  466: 	if (devminor == VINUM_SUPERDEV)			    /* normal superdev */
  467: 	    vinum_conf.flags &= ~VF_OPEN;		    /* no longer open */
  468: 	else if (devminor == VINUM_DAEMON_DEV) {	    /* the daemon device */
  469: 	    vinum_conf.flags &= ~VF_DAEMONOPEN;		    /* no longer open */
  470: 	    if (vinum_conf.flags & VF_STOPPING)		    /* we're stopping, */
  471: 		wakeup(&vinumclose);			    /* we can continue stopping now */
  472: 	}
  473: 	return 0;
  474: 
  475:     case VINUM_DRIVE_TYPE:
  476:     default:
  477: 	return ENODEV;					    /* don't know what to do with these */
  478:     }
  479: }
  480: 
  481: /* size routine */
  482: int
  483: vinumsize(dev_t dev)
  484: {
  485:     struct volume *vol;
  486:     int size;
  487: 
  488:     vol = &VOL[Volno(dev)];
  489: 
  490:     if (vol->state == volume_up)
  491: 	size = vol->size;
  492:     else
  493: 	return 0;					    /* err on the size of conservatism */
  494: 
  495:     return size;
  496: }
  497: 
  498: int
  499: vinumdump(dev_t dev)
  500: {
  501:     /* Not implemented. */
  502:     return ENXIO;
  503: }
  504: 
  505: /* Local Variables: */
  506: /* fill-column: 50 */
  507: /* End: */