diff --git a/sys/alpha/alpha/promcons.c b/sys/alpha/alpha/promcons.c index 649df02f7e75..4a54311837d0 100644 --- a/sys/alpha/alpha/promcons.c +++ b/sys/alpha/alpha/promcons.c @@ -1,247 +1,257 @@ /* $FreeBSD$ */ /* $NetBSD: promcons.c,v 1.13 1998/03/21 22:52:59 mycroft Exp $ */ /* * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. * All rights reserved. * * Author: Chris G. Demetriou * * Permission to use, copy, modify and distribute this software and * its documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie the * rights to redistribute these changes. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define _PMAP_MAY_USE_PROM_CONSOLE /* XXX for now */ #ifdef _PMAP_MAY_USE_PROM_CONSOLE #define PROM_POLL_HZ 50 static d_open_t promopen; static d_close_t promclose; static d_ioctl_t promioctl; #define CDEV_MAJOR 97 static struct cdevsw prom_cdevsw = { /* open */ promopen, /* close */ promclose, /* read */ ttyread, /* write */ ttywrite, /* ioctl */ promioctl, /* poll */ ttypoll, /* mmap */ nommap, /* strategy */ nostrategy, /* name */ "prom", /* maj */ CDEV_MAJOR, /* dump */ nodump, /* psize */ nopsize, /* flags */ 0, /* bmaj */ -1 }; static struct tty prom_tty[1]; static int polltime; static struct callout_handle promtimeouthandle = CALLOUT_HANDLE_INITIALIZER(&promtimeouthandle); void promstart __P((struct tty *)); void promtimeout __P((void *)); int promparam __P((struct tty *, struct termios *)); void promstop __P((struct tty *, int)); int promopen(dev, flag, mode, p) dev_t dev; int flag, mode; struct proc *p; { int unit = minor(dev); struct tty *tp; int s; int error = 0, setuptimeout = 0; if (!pmap_uses_prom_console() || unit >= 1) return ENXIO; s = spltty(); tp = &prom_tty[unit]; dev->si_tty = tp; tp->t_oproc = promstart; tp->t_param = promparam; tp->t_stop = promstop; tp->t_dev = dev; if ((tp->t_state & TS_ISOPEN) == 0) { tp->t_state |= TS_CARR_ON; ttychars(tp); tp->t_iflag = TTYDEF_IFLAG; tp->t_oflag = TTYDEF_OFLAG; tp->t_cflag = TTYDEF_CFLAG|CLOCAL; tp->t_lflag = TTYDEF_LFLAG; tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; ttsetwater(tp); setuptimeout = 1; } else if (tp->t_state & TS_XCLUDE && suser(p)) { splx(s); return EBUSY; } splx(s); error = (*linesw[tp->t_line].l_open)(dev, tp); if (error == 0 && setuptimeout) { polltime = hz / PROM_POLL_HZ; if (polltime < 1) polltime = 1; promtimeouthandle = timeout(promtimeout, tp, polltime); } return error; } int promclose(dev, flag, mode, p) dev_t dev; int flag, mode; struct proc *p; { int unit = minor(dev); struct tty *tp = &prom_tty[unit]; untimeout(promtimeout, tp, promtimeouthandle); (*linesw[tp->t_line].l_close)(tp, flag); ttyclose(tp); return 0; } int promioctl(dev, cmd, data, flag, p) dev_t dev; u_long cmd; caddr_t data; int flag; struct proc *p; { int unit = minor(dev); struct tty *tp = &prom_tty[unit]; int error; error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); if (error != ENOIOCTL) return error; error = ttioctl(tp, cmd, data, flag); if (error != ENOIOCTL) return error; return ENOTTY; } int promparam(tp, t) struct tty *tp; struct termios *t; { return 0; } void promstart(tp) struct tty *tp; { int s; s = spltty(); if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { ttwwakeup(tp); splx(s); return; } tp->t_state |= TS_BUSY; while (tp->t_outq.c_cc != 0) promcnputc(tp->t_dev, getc(&tp->t_outq)); tp->t_state &= ~TS_BUSY; ttwwakeup(tp); splx(s); } /* * Stop output on a line. */ void promstop(tp, flag) struct tty *tp; int flag; { int s; s = spltty(); if (tp->t_state & TS_BUSY) if ((tp->t_state & TS_TTSTOP) == 0) tp->t_state |= TS_FLUSH; splx(s); } void promtimeout(v) void *v; { struct tty *tp = v; int c; while ((c = promcncheckc(tp->t_dev)) != -1) { if (tp->t_state & TS_ISOPEN) (*linesw[tp->t_line].l_rint)(c, tp); } promtimeouthandle = timeout(promtimeout, tp, polltime); } -DEV_MODULE(prom, CDEV_MAJOR, NOMAJ, prom_cdevsw, 0, 0); +static int +prom_modevent(module_t mod, int type, void *data) +{ + if (type == MOD_LOAD) { + cdevsw_add(&prom_cdevsw); + return(0); + } + return(EOPNOTSUPP); +} + +DEV_MODULE(prom, prom_modevent, 0); #endif /* _PMAP_MAY_USE_PROM_CONSOLE */ diff --git a/sys/dev/ccd/ccd.c b/sys/dev/ccd/ccd.c index c4cc2bd10d9e..75ee082fba29 100644 --- a/sys/dev/ccd/ccd.c +++ b/sys/dev/ccd/ccd.c @@ -1,1767 +1,1768 @@ /* $FreeBSD$ */ /* $NetBSD: ccd.c,v 1.22 1995/12/08 19:13:26 thorpej Exp $ */ /* * Copyright (c) 1995 Jason R. Thorpe. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the NetBSD Project * by Jason R. Thorpe. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Copyright (c) 1988 University of Utah. * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * the Systems Programming Group of the University of Utah Computer * Science Department. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from: Utah $Hdr: cd.c 1.6 90/11/28$ * * @(#)cd.c 8.2 (Berkeley) 11/16/93 */ /* * "Concatenated" disk driver. * * Dynamic configuration and disklabel support by: * Jason R. Thorpe * Numerical Aerodynamic Simulation Facility * Mail Stop 258-6 * NASA Ames Research Center * Moffett Field, CA 94035 */ #include "ccd.h" #if NCCD > 0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(CCDDEBUG) && !defined(DEBUG) #define DEBUG #endif #ifdef DEBUG #define CCDB_FOLLOW 0x01 #define CCDB_INIT 0x02 #define CCDB_IO 0x04 #define CCDB_LABEL 0x08 #define CCDB_VNODE 0x10 static int ccddebug = CCDB_FOLLOW | CCDB_INIT | CCDB_IO | CCDB_LABEL | CCDB_VNODE; SYSCTL_INT(_debug, OID_AUTO, ccddebug, CTLFLAG_RW, &ccddebug, 0, ""); #undef DEBUG #endif #define ccdunit(x) dkunit(x) #define ccdpart(x) dkpart(x) /* This is how mirroring works (only writes are special): When initiating a write, ccdbuffer() returns two "struct ccdbuf *"s linked together by the cb_mirror field. "cb_pflags & CCDPF_MIRROR_DONE" is set to 0 on both of them. When a component returns to ccdiodone(), it checks if "cb_pflags & CCDPF_MIRROR_DONE" is set or not. If not, it sets the partner's flag and returns. If it is, it means its partner has already returned, so it will go to the regular cleanup. */ struct ccdbuf { struct buf cb_buf; /* new I/O buf */ struct buf *cb_obp; /* ptr. to original I/O buf */ struct ccdbuf *cb_freenext; /* free list link */ int cb_unit; /* target unit */ int cb_comp; /* target component */ int cb_pflags; /* mirror/parity status flag */ struct ccdbuf *cb_mirror; /* mirror counterpart */ }; /* bits in cb_pflags */ #define CCDPF_MIRROR_DONE 1 /* if set, mirror counterpart is done */ #define CCDLABELDEV(dev) \ (makedev(major((dev)), dkmakeminor(ccdunit((dev)), 0, RAW_PART))) static d_open_t ccdopen; static d_close_t ccdclose; static d_strategy_t ccdstrategy; static d_ioctl_t ccdioctl; static d_dump_t ccddump; static d_psize_t ccdsize; #define NCCDFREEHIWAT 16 #define CDEV_MAJOR 74 #define BDEV_MAJOR 21 static struct cdevsw ccd_cdevsw = { /* open */ ccdopen, /* close */ ccdclose, /* read */ physread, /* write */ physwrite, /* ioctl */ ccdioctl, /* poll */ nopoll, /* mmap */ nommap, /* strategy */ ccdstrategy, /* name */ "ccd", /* maj */ CDEV_MAJOR, /* dump */ ccddump, /* psize */ ccdsize, /* flags */ D_DISK, /* bmaj */ BDEV_MAJOR }; /* called during module initialization */ static void ccdattach __P((void)); static int ccd_modevent __P((module_t, int, void *)); /* called by biodone() at interrupt time */ static void ccdiodone __P((struct ccdbuf *cbp)); static void ccdstart __P((struct ccd_softc *, struct buf *)); static void ccdinterleave __P((struct ccd_softc *, int)); static void ccdintr __P((struct ccd_softc *, struct buf *)); static int ccdinit __P((struct ccddevice *, char **, struct proc *)); static int ccdlookup __P((char *, struct proc *p, struct vnode **)); static void ccdbuffer __P((struct ccdbuf **ret, struct ccd_softc *, struct buf *, daddr_t, caddr_t, long)); static void ccdgetdisklabel __P((dev_t)); static void ccdmakedisklabel __P((struct ccd_softc *)); static int ccdlock __P((struct ccd_softc *)); static void ccdunlock __P((struct ccd_softc *)); #ifdef DEBUG static void printiinfo __P((struct ccdiinfo *)); #endif /* Non-private for the benefit of libkvm. */ struct ccd_softc *ccd_softc; struct ccddevice *ccddevs; struct ccdbuf *ccdfreebufs; static int numccdfreebufs; static int numccd = 0; /* * getccdbuf() - Allocate and zero a ccd buffer. * * This routine is called at splbio(). */ static __inline struct ccdbuf * getccdbuf(struct ccdbuf *cpy) { struct ccdbuf *cbp; /* * Allocate from freelist or malloc as necessary */ if ((cbp = ccdfreebufs) != NULL) { ccdfreebufs = cbp->cb_freenext; --numccdfreebufs; } else { cbp = malloc(sizeof(struct ccdbuf), M_DEVBUF, M_WAITOK); } /* * Used by mirroring code */ if (cpy) bcopy(cpy, cbp, sizeof(struct ccdbuf)); else bzero(cbp, sizeof(struct ccdbuf)); /* * independant struct buf initialization */ LIST_INIT(&cbp->cb_buf.b_dep); BUF_LOCKINIT(&cbp->cb_buf); BUF_LOCK(&cbp->cb_buf, LK_EXCLUSIVE); BUF_KERNPROC(&cbp->cb_buf); return(cbp); } /* * putccdbuf() - Free a ccd buffer. * * This routine is called at splbio(). */ static __inline void putccdbuf(struct ccdbuf *cbp) { BUF_UNLOCK(&cbp->cb_buf); BUF_LOCKFREE(&cbp->cb_buf); if (numccdfreebufs < NCCDFREEHIWAT) { cbp->cb_freenext = ccdfreebufs; ccdfreebufs = cbp; ++numccdfreebufs; } else { free((caddr_t)cbp, M_DEVBUF); } } /* * Number of blocks to untouched in front of a component partition. * This is to avoid violating its disklabel area when it starts at the * beginning of the slice. */ #if !defined(CCD_OFFSET) #define CCD_OFFSET 16 #endif /* * Called by main() during pseudo-device attachment. All we need * to do is allocate enough space for devices to be configured later, and * add devsw entries. */ static void ccdattach() { int i; int num = NCCD; if (num > 1) printf("ccd0-%d: Concatenated disk drivers\n", num-1); else printf("ccd0: Concatenated disk driver\n"); ccd_softc = (struct ccd_softc *)malloc(num * sizeof(struct ccd_softc), M_DEVBUF, M_NOWAIT); ccddevs = (struct ccddevice *)malloc(num * sizeof(struct ccddevice), M_DEVBUF, M_NOWAIT); if ((ccd_softc == NULL) || (ccddevs == NULL)) { printf("WARNING: no memory for concatenated disks\n"); if (ccd_softc != NULL) free(ccd_softc, M_DEVBUF); if (ccddevs != NULL) free(ccddevs, M_DEVBUF); return; } numccd = num; bzero(ccd_softc, num * sizeof(struct ccd_softc)); bzero(ccddevs, num * sizeof(struct ccddevice)); + cdevsw_add(&ccd_cdevsw); /* XXX: is this necessary? */ for (i = 0; i < numccd; ++i) ccddevs[i].ccd_dk = -1; } static int ccd_modevent(mod, type, data) module_t mod; int type; void *data; { int error = 0; switch (type) { case MOD_LOAD: ccdattach(); break; case MOD_UNLOAD: printf("ccd0: Unload not supported!\n"); error = EOPNOTSUPP; break; default: /* MOD_SHUTDOWN etc */ break; } return (error); } -DEV_MODULE(ccd, CDEV_MAJOR, BDEV_MAJOR, ccd_cdevsw, ccd_modevent, NULL); +DEV_MODULE(ccd, ccd_modevent, NULL); static int ccdinit(ccd, cpaths, p) struct ccddevice *ccd; char **cpaths; struct proc *p; { struct ccd_softc *cs = &ccd_softc[ccd->ccd_unit]; struct ccdcinfo *ci = NULL; /* XXX */ size_t size; int ix; struct vnode *vp; size_t minsize; int maxsecsize; struct partinfo dpart; struct ccdgeom *ccg = &cs->sc_geom; char tmppath[MAXPATHLEN]; int error = 0; #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccdinit: unit %d\n", ccd->ccd_unit); #endif cs->sc_size = 0; cs->sc_ileave = ccd->ccd_interleave; cs->sc_nccdisks = ccd->ccd_ndev; /* Allocate space for the component info. */ cs->sc_cinfo = malloc(cs->sc_nccdisks * sizeof(struct ccdcinfo), M_DEVBUF, M_WAITOK); /* * Verify that each component piece exists and record * relevant information about it. */ maxsecsize = 0; minsize = 0; for (ix = 0; ix < cs->sc_nccdisks; ix++) { vp = ccd->ccd_vpp[ix]; ci = &cs->sc_cinfo[ix]; ci->ci_vp = vp; /* * Copy in the pathname of the component. */ bzero(tmppath, sizeof(tmppath)); /* sanity */ if ((error = copyinstr(cpaths[ix], tmppath, MAXPATHLEN, &ci->ci_pathlen)) != 0) { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: can't copy path, error = %d\n", ccd->ccd_unit, error); #endif goto fail; } ci->ci_path = malloc(ci->ci_pathlen, M_DEVBUF, M_WAITOK); bcopy(tmppath, ci->ci_path, ci->ci_pathlen); ci->ci_dev = vn_todev(vp); /* * Get partition information for the component. */ if ((error = VOP_IOCTL(vp, DIOCGPART, (caddr_t)&dpart, FREAD, p->p_ucred, p)) != 0) { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: %s: ioctl failed, error = %d\n", ccd->ccd_unit, ci->ci_path, error); #endif goto fail; } if (dpart.part->p_fstype == FS_BSDFFS) { maxsecsize = ((dpart.disklab->d_secsize > maxsecsize) ? dpart.disklab->d_secsize : maxsecsize); size = dpart.part->p_size - CCD_OFFSET; } else { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: %s: incorrect partition type\n", ccd->ccd_unit, ci->ci_path); #endif error = EFTYPE; goto fail; } /* * Calculate the size, truncating to an interleave * boundary if necessary. */ if (cs->sc_ileave > 1) size -= size % cs->sc_ileave; if (size == 0) { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: %s: size == 0\n", ccd->ccd_unit, ci->ci_path); #endif error = ENODEV; goto fail; } if (minsize == 0 || size < minsize) minsize = size; ci->ci_size = size; cs->sc_size += size; } /* * Don't allow the interleave to be smaller than * the biggest component sector. */ if ((cs->sc_ileave > 0) && (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: interleave must be at least %d\n", ccd->ccd_unit, (maxsecsize / DEV_BSIZE)); #endif error = EINVAL; goto fail; } /* * If uniform interleave is desired set all sizes to that of * the smallest component. This will guarentee that a single * interleave table is generated. * * Lost space must be taken into account when calculating the * overall size. Half the space is lost when CCDF_MIRROR is * specified. One disk is lost when CCDF_PARITY is specified. */ if (ccd->ccd_flags & CCDF_UNIFORM) { for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++) { ci->ci_size = minsize; } if (ccd->ccd_flags & CCDF_MIRROR) { /* * Check to see if an even number of components * have been specified. The interleave must also * be non-zero in order for us to be able to * guarentee the topology. */ if (cs->sc_nccdisks % 2) { printf("ccd%d: mirroring requires an even number of disks\n", ccd->ccd_unit ); error = EINVAL; goto fail; } if (cs->sc_ileave == 0) { printf("ccd%d: an interleave must be specified when mirroring\n", ccd->ccd_unit); error = EINVAL; goto fail; } cs->sc_size = (cs->sc_nccdisks/2) * minsize; } else if (ccd->ccd_flags & CCDF_PARITY) { cs->sc_size = (cs->sc_nccdisks-1) * minsize; } else { if (cs->sc_ileave == 0) { printf("ccd%d: an interleave must be specified when using parity\n", ccd->ccd_unit); error = EINVAL; goto fail; } cs->sc_size = cs->sc_nccdisks * minsize; } } /* * Construct the interleave table. */ ccdinterleave(cs, ccd->ccd_unit); /* * Create pseudo-geometry based on 1MB cylinders. It's * pretty close. */ ccg->ccg_secsize = maxsecsize; ccg->ccg_ntracks = 1; ccg->ccg_nsectors = 1024 * 1024 / ccg->ccg_secsize; ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors; /* * Add an devstat entry for this device. */ devstat_add_entry(&cs->device_stats, "ccd", ccd->ccd_unit, ccg->ccg_secsize, DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_ASC0 |DEVSTAT_TYPE_IF_OTHER, DEVSTAT_PRIORITY_CCD); cs->sc_flags |= CCDF_INITED; cs->sc_cflags = ccd->ccd_flags; /* So we can find out later... */ cs->sc_unit = ccd->ccd_unit; return (0); fail: while (ci > cs->sc_cinfo) { ci--; free(ci->ci_path, M_DEVBUF); } free(cs->sc_cinfo, M_DEVBUF); return (error); } static void ccdinterleave(cs, unit) struct ccd_softc *cs; int unit; { struct ccdcinfo *ci, *smallci; struct ccdiinfo *ii; daddr_t bn, lbn; int ix; u_long size; #ifdef DEBUG if (ccddebug & CCDB_INIT) printf("ccdinterleave(%x): ileave %d\n", cs, cs->sc_ileave); #endif /* * Allocate an interleave table. The worst case occurs when each * of N disks is of a different size, resulting in N interleave * tables. * * Chances are this is too big, but we don't care. */ size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo); cs->sc_itable = (struct ccdiinfo *)malloc(size, M_DEVBUF, M_WAITOK); bzero((caddr_t)cs->sc_itable, size); /* * Trivial case: no interleave (actually interleave of disk size). * Each table entry represents a single component in its entirety. * * An interleave of 0 may not be used with a mirror or parity setup. */ if (cs->sc_ileave == 0) { bn = 0; ii = cs->sc_itable; for (ix = 0; ix < cs->sc_nccdisks; ix++) { /* Allocate space for ii_index. */ ii->ii_index = malloc(sizeof(int), M_DEVBUF, M_WAITOK); ii->ii_ndisk = 1; ii->ii_startblk = bn; ii->ii_startoff = 0; ii->ii_index[0] = ix; bn += cs->sc_cinfo[ix].ci_size; ii++; } ii->ii_ndisk = 0; #ifdef DEBUG if (ccddebug & CCDB_INIT) printiinfo(cs->sc_itable); #endif return; } /* * The following isn't fast or pretty; it doesn't have to be. */ size = 0; bn = lbn = 0; for (ii = cs->sc_itable; ; ii++) { /* * Allocate space for ii_index. We might allocate more then * we use. */ ii->ii_index = malloc((sizeof(int) * cs->sc_nccdisks), M_DEVBUF, M_WAITOK); /* * Locate the smallest of the remaining components */ smallci = NULL; for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++) { if (ci->ci_size > size && (smallci == NULL || ci->ci_size < smallci->ci_size)) { smallci = ci; } } /* * Nobody left, all done */ if (smallci == NULL) { ii->ii_ndisk = 0; break; } /* * Record starting logical block using an sc_ileave blocksize. */ ii->ii_startblk = bn / cs->sc_ileave; /* * Record starting comopnent block using an sc_ileave * blocksize. This value is relative to the beginning of * a component disk. */ ii->ii_startoff = lbn; /* * Determine how many disks take part in this interleave * and record their indices. */ ix = 0; for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++) { if (ci->ci_size >= smallci->ci_size) { ii->ii_index[ix++] = ci - cs->sc_cinfo; } } ii->ii_ndisk = ix; bn += ix * (smallci->ci_size - size); lbn = smallci->ci_size / cs->sc_ileave; size = smallci->ci_size; } #ifdef DEBUG if (ccddebug & CCDB_INIT) printiinfo(cs->sc_itable); #endif } /* ARGSUSED */ static int ccdopen(dev, flags, fmt, p) dev_t dev; int flags, fmt; struct proc *p; { int unit = ccdunit(dev); struct ccd_softc *cs; struct disklabel *lp; int error = 0, part, pmask; #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdopen(%x, %x)\n", dev, flags); #endif if (unit >= numccd) return (ENXIO); cs = &ccd_softc[unit]; if ((error = ccdlock(cs)) != 0) return (error); lp = &cs->sc_label; part = ccdpart(dev); pmask = (1 << part); /* * If we're initialized, check to see if there are any other * open partitions. If not, then it's safe to update * the in-core disklabel. */ if ((cs->sc_flags & CCDF_INITED) && (cs->sc_openmask == 0)) ccdgetdisklabel(dev); /* Check that the partition exists. */ if (part != RAW_PART && ((part >= lp->d_npartitions) || (lp->d_partitions[part].p_fstype == FS_UNUSED))) { error = ENXIO; goto done; } /* Prevent our unit from being unconfigured while open. */ switch (fmt) { case S_IFCHR: cs->sc_copenmask |= pmask; break; case S_IFBLK: cs->sc_bopenmask |= pmask; break; } cs->sc_openmask = cs->sc_copenmask | cs->sc_bopenmask; done: ccdunlock(cs); return (0); } /* ARGSUSED */ static int ccdclose(dev, flags, fmt, p) dev_t dev; int flags, fmt; struct proc *p; { int unit = ccdunit(dev); struct ccd_softc *cs; int error = 0, part; #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdclose(%x, %x)\n", dev, flags); #endif if (unit >= numccd) return (ENXIO); cs = &ccd_softc[unit]; if ((error = ccdlock(cs)) != 0) return (error); part = ccdpart(dev); /* ...that much closer to allowing unconfiguration... */ switch (fmt) { case S_IFCHR: cs->sc_copenmask &= ~(1 << part); break; case S_IFBLK: cs->sc_bopenmask &= ~(1 << part); break; } cs->sc_openmask = cs->sc_copenmask | cs->sc_bopenmask; ccdunlock(cs); return (0); } static void ccdstrategy(bp) struct buf *bp; { int unit = ccdunit(bp->b_dev); struct ccd_softc *cs = &ccd_softc[unit]; int s; int wlabel; struct disklabel *lp; #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdstrategy(%x): unit %d\n", bp, unit); #endif if ((cs->sc_flags & CCDF_INITED) == 0) { bp->b_error = ENXIO; bp->b_flags |= B_ERROR; goto done; } /* If it's a nil transfer, wake up the top half now. */ if (bp->b_bcount == 0) goto done; lp = &cs->sc_label; /* * Do bounds checking and adjust transfer. If there's an * error, the bounds check will flag that for us. */ wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING); if (ccdpart(bp->b_dev) != RAW_PART) { if (bounds_check_with_label(bp, lp, wlabel) <= 0) goto done; } else { int pbn; /* in sc_secsize chunks */ long sz; /* in sc_secsize chunks */ pbn = bp->b_blkno / (cs->sc_geom.ccg_secsize / DEV_BSIZE); sz = howmany(bp->b_bcount, cs->sc_geom.ccg_secsize); /* * If out of bounds return an error. If at the EOF point, * simply read or write less. */ if (pbn < 0 || pbn >= cs->sc_size) { bp->b_resid = bp->b_bcount; if (pbn != cs->sc_size) { bp->b_error = EINVAL; bp->b_flags |= B_ERROR | B_INVAL; } goto done; } /* * If the request crosses EOF, truncate the request. */ if (pbn + sz > cs->sc_size) { bp->b_bcount = (cs->sc_size - pbn) * cs->sc_geom.ccg_secsize; } } bp->b_resid = bp->b_bcount; /* * "Start" the unit. */ s = splbio(); ccdstart(cs, bp); splx(s); return; done: biodone(bp); } static void ccdstart(cs, bp) struct ccd_softc *cs; struct buf *bp; { long bcount, rcount; struct ccdbuf *cbp[4]; /* XXX! : 2 reads and 2 writes for RAID 4/5 */ caddr_t addr; daddr_t bn; struct partition *pp; #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdstart(%x, %x)\n", cs, bp); #endif /* Record the transaction start */ devstat_start_transaction(&cs->device_stats); /* * Translate the partition-relative block number to an absolute. */ bn = bp->b_blkno; if (ccdpart(bp->b_dev) != RAW_PART) { pp = &cs->sc_label.d_partitions[ccdpart(bp->b_dev)]; bn += pp->p_offset; } /* * Allocate component buffers and fire off the requests */ addr = bp->b_data; for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) { ccdbuffer(cbp, cs, bp, bn, addr, bcount); rcount = cbp[0]->cb_buf.b_bcount; if (cs->sc_cflags & CCDF_MIRROR) { /* * Mirroring. Writes go to both disks, reads are * taken from whichever disk seems most appropriate. * * We attempt to localize reads to the disk whos arm * is nearest the read request. We ignore seeks due * to writes when making this determination and we * also try to avoid hogging. */ if ((cbp[0]->cb_buf.b_flags & B_READ) == 0) { cbp[0]->cb_buf.b_vp->v_numoutput++; cbp[1]->cb_buf.b_vp->v_numoutput++; VOP_STRATEGY(cbp[0]->cb_buf.b_vp, &cbp[0]->cb_buf); VOP_STRATEGY(cbp[1]->cb_buf.b_vp, &cbp[1]->cb_buf); } else { int pick = cs->sc_pick; daddr_t range = cs->sc_size / 16; if (bn < cs->sc_blk[pick] - range || bn > cs->sc_blk[pick] + range ) { cs->sc_pick = pick = 1 - pick; } cs->sc_blk[pick] = bn + btodb(rcount); VOP_STRATEGY(cbp[pick]->cb_buf.b_vp, &cbp[pick]->cb_buf); } } else { /* * Not mirroring */ if ((cbp[0]->cb_buf.b_flags & B_READ) == 0) cbp[0]->cb_buf.b_vp->v_numoutput++; VOP_STRATEGY(cbp[0]->cb_buf.b_vp, &cbp[0]->cb_buf); } bn += btodb(rcount); addr += rcount; } } /* * Build a component buffer header. */ static void ccdbuffer(cb, cs, bp, bn, addr, bcount) struct ccdbuf **cb; struct ccd_softc *cs; struct buf *bp; daddr_t bn; caddr_t addr; long bcount; { struct ccdcinfo *ci, *ci2 = NULL; /* XXX */ struct ccdbuf *cbp; daddr_t cbn, cboff; off_t cbc; #ifdef DEBUG if (ccddebug & CCDB_IO) printf("ccdbuffer(%x, %x, %d, %x, %d)\n", cs, bp, bn, addr, bcount); #endif /* * Determine which component bn falls in. */ cbn = bn; cboff = 0; if (cs->sc_ileave == 0) { /* * Serially concatenated and neither a mirror nor a parity * config. This is a special case. */ daddr_t sblk; sblk = 0; for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++) sblk += ci->ci_size; cbn -= sblk; } else { struct ccdiinfo *ii; int ccdisk, off; /* * Calculate cbn, the logical superblock (sc_ileave chunks), * and cboff, a normal block offset (DEV_BSIZE chunks) relative * to cbn. */ cboff = cbn % cs->sc_ileave; /* DEV_BSIZE gran */ cbn = cbn / cs->sc_ileave; /* DEV_BSIZE * ileave gran */ /* * Figure out which interleave table to use. */ for (ii = cs->sc_itable; ii->ii_ndisk; ii++) { if (ii->ii_startblk > cbn) break; } ii--; /* * off is the logical superblock relative to the beginning * of this interleave block. */ off = cbn - ii->ii_startblk; /* * We must calculate which disk component to use (ccdisk), * and recalculate cbn to be the superblock relative to * the beginning of the component. This is typically done by * adding 'off' and ii->ii_startoff together. However, 'off' * must typically be divided by the number of components in * this interleave array to be properly convert it from a * CCD-relative logical superblock number to a * component-relative superblock number. */ if (ii->ii_ndisk == 1) { /* * When we have just one disk, it can't be a mirror * or a parity config. */ ccdisk = ii->ii_index[0]; cbn = ii->ii_startoff + off; } else { if (cs->sc_cflags & CCDF_MIRROR) { /* * We have forced a uniform mapping, resulting * in a single interleave array. We double * up on the first half of the available * components and our mirror is in the second * half. This only works with a single * interleave array because doubling up * doubles the number of sectors, so there * cannot be another interleave array because * the next interleave array's calculations * would be off. */ int ndisk2 = ii->ii_ndisk / 2; ccdisk = ii->ii_index[off % ndisk2]; cbn = ii->ii_startoff + off / ndisk2; ci2 = &cs->sc_cinfo[ccdisk + ndisk2]; } else if (cs->sc_cflags & CCDF_PARITY) { /* * XXX not implemented yet */ int ndisk2 = ii->ii_ndisk - 1; ccdisk = ii->ii_index[off % ndisk2]; cbn = ii->ii_startoff + off / ndisk2; if (cbn % ii->ii_ndisk <= ccdisk) ccdisk++; } else { ccdisk = ii->ii_index[off % ii->ii_ndisk]; cbn = ii->ii_startoff + off / ii->ii_ndisk; } } ci = &cs->sc_cinfo[ccdisk]; /* * Convert cbn from a superblock to a normal block so it * can be used to calculate (along with cboff) the normal * block index into this particular disk. */ cbn *= cs->sc_ileave; } /* * Fill in the component buf structure. */ cbp = getccdbuf(NULL); cbp->cb_buf.b_flags = bp->b_flags | B_CALL; cbp->cb_buf.b_iodone = (void (*)(struct buf *))ccdiodone; cbp->cb_buf.b_dev = ci->ci_dev; /* XXX */ cbp->cb_buf.b_blkno = cbn + cboff + CCD_OFFSET; cbp->cb_buf.b_offset = dbtob(cbn + cboff + CCD_OFFSET); cbp->cb_buf.b_data = addr; cbp->cb_buf.b_vp = ci->ci_vp; if (cs->sc_ileave == 0) cbc = dbtob((off_t)(ci->ci_size - cbn)); else cbc = dbtob((off_t)(cs->sc_ileave - cboff)); cbp->cb_buf.b_bcount = (cbc < bcount) ? cbc : bcount; cbp->cb_buf.b_bufsize = cbp->cb_buf.b_bcount; /* * context for ccdiodone */ cbp->cb_obp = bp; cbp->cb_unit = cs - ccd_softc; cbp->cb_comp = ci - cs->sc_cinfo; #ifdef DEBUG if (ccddebug & CCDB_IO) printf(" dev %x(u%d): cbp %x bn %d addr %x bcnt %d\n", ci->ci_dev, ci-cs->sc_cinfo, cbp, cbp->cb_buf.b_blkno, cbp->cb_buf.b_data, cbp->cb_buf.b_bcount); #endif cb[0] = cbp; /* * Note: both I/O's setup when reading from mirror, but only one * will be executed. */ if (cs->sc_cflags & CCDF_MIRROR) { /* mirror, setup second I/O */ cbp = getccdbuf(cb[0]); cbp->cb_buf.b_dev = ci2->ci_dev; cbp->cb_buf.b_vp = ci2->ci_vp; cbp->cb_comp = ci2 - cs->sc_cinfo; cb[1] = cbp; /* link together the ccdbuf's and clear "mirror done" flag */ cb[0]->cb_mirror = cb[1]; cb[1]->cb_mirror = cb[0]; cb[0]->cb_pflags &= ~CCDPF_MIRROR_DONE; cb[1]->cb_pflags &= ~CCDPF_MIRROR_DONE; } } static void ccdintr(cs, bp) struct ccd_softc *cs; struct buf *bp; { #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdintr(%x, %x)\n", cs, bp); #endif /* * Request is done for better or worse, wakeup the top half. */ if (bp->b_flags & B_ERROR) bp->b_resid = bp->b_bcount; devstat_end_transaction_buf(&cs->device_stats, bp); biodone(bp); } /* * Called at interrupt time. * Mark the component as done and if all components are done, * take a ccd interrupt. */ static void ccdiodone(cbp) struct ccdbuf *cbp; { struct buf *bp = cbp->cb_obp; int unit = cbp->cb_unit; int count, s; s = splbio(); #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdiodone(%x)\n", cbp); if (ccddebug & CCDB_IO) { printf("ccdiodone: bp %x bcount %d resid %d\n", bp, bp->b_bcount, bp->b_resid); printf(" dev %x(u%d), cbp %x bn %d addr %x bcnt %d\n", cbp->cb_buf.b_dev, cbp->cb_comp, cbp, cbp->cb_buf.b_blkno, cbp->cb_buf.b_data, cbp->cb_buf.b_bcount); } #endif /* * If an error occured, report it. If this is a mirrored * configuration and the first of two possible reads, do not * set the error in the bp yet because the second read may * succeed. */ if (cbp->cb_buf.b_flags & B_ERROR) { const char *msg = ""; if ((ccd_softc[unit].sc_cflags & CCDF_MIRROR) && (cbp->cb_buf.b_flags & B_READ) && (cbp->cb_pflags & CCDPF_MIRROR_DONE) == 0) { /* * We will try our read on the other disk down * below, also reverse the default pick so if we * are doing a scan we do not keep hitting the * bad disk first. */ struct ccd_softc *cs = &ccd_softc[unit]; msg = ", trying other disk"; cs->sc_pick = 1 - cs->sc_pick; cs->sc_blk[cs->sc_pick] = bp->b_blkno; } else { bp->b_flags |= B_ERROR; bp->b_error = cbp->cb_buf.b_error ? cbp->cb_buf.b_error : EIO; } printf("ccd%d: error %d on component %d block %d (ccd block %d)%s\n", unit, bp->b_error, cbp->cb_comp, (int)cbp->cb_buf.b_blkno, bp->b_blkno, msg); } /* * Process mirror. If we are writing, I/O has been initiated on both * buffers and we fall through only after both are finished. * * If we are reading only one I/O is initiated at a time. If an * error occurs we initiate the second I/O and return, otherwise * we free the second I/O without initiating it. */ if (ccd_softc[unit].sc_cflags & CCDF_MIRROR) { if ((cbp->cb_buf.b_flags & B_READ) == 0) { /* * When writing, handshake with the second buffer * to determine when both are done. If both are not * done, return here. */ if ((cbp->cb_pflags & CCDPF_MIRROR_DONE) == 0) { cbp->cb_mirror->cb_pflags |= CCDPF_MIRROR_DONE; putccdbuf(cbp); splx(s); return; } } else { /* * When reading, either dispose of the second buffer * or initiate I/O on the second buffer if an error * occured with this one. */ if ((cbp->cb_pflags & CCDPF_MIRROR_DONE) == 0) { if (cbp->cb_buf.b_flags & B_ERROR) { cbp->cb_mirror->cb_pflags |= CCDPF_MIRROR_DONE; VOP_STRATEGY( cbp->cb_mirror->cb_buf.b_vp, &cbp->cb_mirror->cb_buf ); putccdbuf(cbp); splx(s); return; } else { putccdbuf(cbp->cb_mirror); /* fall through */ } } } } /* * use b_bufsize to determine how big the original request was rather * then b_bcount, because b_bcount may have been truncated for EOF. * * XXX We check for an error, but we do not test the resid for an * aligned EOF condition. This may result in character & block * device access not recognizing EOF properly when read or written * sequentially, but will not effect filesystems. */ count = cbp->cb_buf.b_bufsize; putccdbuf(cbp); /* * If all done, "interrupt". */ bp->b_resid -= count; if (bp->b_resid < 0) panic("ccdiodone: count"); if (bp->b_resid == 0) ccdintr(&ccd_softc[unit], bp); splx(s); } static int ccdioctl(dev, cmd, data, flag, p) dev_t dev; u_long cmd; caddr_t data; int flag; struct proc *p; { int unit = ccdunit(dev); int i, j, lookedup = 0, error = 0; int part, pmask, s; struct ccd_softc *cs; struct ccd_ioctl *ccio = (struct ccd_ioctl *)data; struct ccddevice ccd; char **cpp; struct vnode **vpp; if (unit >= numccd) return (ENXIO); cs = &ccd_softc[unit]; bzero(&ccd, sizeof(ccd)); switch (cmd) { case CCDIOCSET: if (cs->sc_flags & CCDF_INITED) return (EBUSY); if ((flag & FWRITE) == 0) return (EBADF); if ((error = ccdlock(cs)) != 0) return (error); /* Fill in some important bits. */ ccd.ccd_unit = unit; ccd.ccd_interleave = ccio->ccio_ileave; if (ccd.ccd_interleave == 0 && ((ccio->ccio_flags & CCDF_MIRROR) || (ccio->ccio_flags & CCDF_PARITY))) { printf("ccd%d: disabling mirror/parity, interleave is 0\n", unit); ccio->ccio_flags &= ~(CCDF_MIRROR | CCDF_PARITY); } if ((ccio->ccio_flags & CCDF_MIRROR) && (ccio->ccio_flags & CCDF_PARITY)) { printf("ccd%d: can't specify both mirror and parity, using mirror\n", unit); ccio->ccio_flags &= ~CCDF_PARITY; } if ((ccio->ccio_flags & (CCDF_MIRROR | CCDF_PARITY)) && !(ccio->ccio_flags & CCDF_UNIFORM)) { printf("ccd%d: mirror/parity forces uniform flag\n", unit); ccio->ccio_flags |= CCDF_UNIFORM; } ccd.ccd_flags = ccio->ccio_flags & CCDF_USERMASK; /* * Allocate space for and copy in the array of * componet pathnames and device numbers. */ cpp = malloc(ccio->ccio_ndisks * sizeof(char *), M_DEVBUF, M_WAITOK); vpp = malloc(ccio->ccio_ndisks * sizeof(struct vnode *), M_DEVBUF, M_WAITOK); error = copyin((caddr_t)ccio->ccio_disks, (caddr_t)cpp, ccio->ccio_ndisks * sizeof(char **)); if (error) { free(vpp, M_DEVBUF); free(cpp, M_DEVBUF); ccdunlock(cs); return (error); } #ifdef DEBUG if (ccddebug & CCDB_INIT) for (i = 0; i < ccio->ccio_ndisks; ++i) printf("ccdioctl: component %d: 0x%x\n", i, cpp[i]); #endif for (i = 0; i < ccio->ccio_ndisks; ++i) { #ifdef DEBUG if (ccddebug & CCDB_INIT) printf("ccdioctl: lookedup = %d\n", lookedup); #endif if ((error = ccdlookup(cpp[i], p, &vpp[i])) != 0) { for (j = 0; j < lookedup; ++j) (void)vn_close(vpp[j], FREAD|FWRITE, p->p_ucred, p); free(vpp, M_DEVBUF); free(cpp, M_DEVBUF); ccdunlock(cs); return (error); } ++lookedup; } ccd.ccd_cpp = cpp; ccd.ccd_vpp = vpp; ccd.ccd_ndev = ccio->ccio_ndisks; /* * Initialize the ccd. Fills in the softc for us. */ if ((error = ccdinit(&ccd, cpp, p)) != 0) { for (j = 0; j < lookedup; ++j) (void)vn_close(vpp[j], FREAD|FWRITE, p->p_ucred, p); bzero(&ccd_softc[unit], sizeof(struct ccd_softc)); free(vpp, M_DEVBUF); free(cpp, M_DEVBUF); ccdunlock(cs); return (error); } /* * The ccd has been successfully initialized, so * we can place it into the array and read the disklabel. */ bcopy(&ccd, &ccddevs[unit], sizeof(ccd)); ccio->ccio_unit = unit; ccio->ccio_size = cs->sc_size; ccdgetdisklabel(dev); ccdunlock(cs); break; case CCDIOCCLR: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); if ((flag & FWRITE) == 0) return (EBADF); if ((error = ccdlock(cs)) != 0) return (error); /* * Don't unconfigure if any other partitions are open * or if both the character and block flavors of this * partition are open. */ part = ccdpart(dev); pmask = (1 << part); if ((cs->sc_openmask & ~pmask) || ((cs->sc_bopenmask & pmask) && (cs->sc_copenmask & pmask))) { ccdunlock(cs); return (EBUSY); } /* * Free ccd_softc information and clear entry. */ /* Close the components and free their pathnames. */ for (i = 0; i < cs->sc_nccdisks; ++i) { /* * XXX: this close could potentially fail and * cause Bad Things. Maybe we need to force * the close to happen? */ #ifdef DEBUG if (ccddebug & CCDB_VNODE) vprint("CCDIOCCLR: vnode info", cs->sc_cinfo[i].ci_vp); #endif (void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE, p->p_ucred, p); free(cs->sc_cinfo[i].ci_path, M_DEVBUF); } /* Free interleave index. */ for (i = 0; cs->sc_itable[i].ii_ndisk; ++i) free(cs->sc_itable[i].ii_index, M_DEVBUF); /* Free component info and interleave table. */ free(cs->sc_cinfo, M_DEVBUF); free(cs->sc_itable, M_DEVBUF); cs->sc_flags &= ~CCDF_INITED; /* * Free ccddevice information and clear entry. */ free(ccddevs[unit].ccd_cpp, M_DEVBUF); free(ccddevs[unit].ccd_vpp, M_DEVBUF); ccd.ccd_dk = -1; bcopy(&ccd, &ccddevs[unit], sizeof(ccd)); /* * And remove the devstat entry. */ devstat_remove_entry(&cs->device_stats); /* This must be atomic. */ s = splhigh(); ccdunlock(cs); bzero(cs, sizeof(struct ccd_softc)); splx(s); break; case DIOCGDINFO: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); *(struct disklabel *)data = cs->sc_label; break; case DIOCGPART: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); ((struct partinfo *)data)->disklab = &cs->sc_label; ((struct partinfo *)data)->part = &cs->sc_label.d_partitions[ccdpart(dev)]; break; case DIOCWDINFO: case DIOCSDINFO: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); if ((flag & FWRITE) == 0) return (EBADF); if ((error = ccdlock(cs)) != 0) return (error); cs->sc_flags |= CCDF_LABELLING; error = setdisklabel(&cs->sc_label, (struct disklabel *)data, 0); if (error == 0) { if (cmd == DIOCWDINFO) error = writedisklabel(CCDLABELDEV(dev), &cs->sc_label); } cs->sc_flags &= ~CCDF_LABELLING; ccdunlock(cs); if (error) return (error); break; case DIOCWLABEL: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); if ((flag & FWRITE) == 0) return (EBADF); if (*(int *)data != 0) cs->sc_flags |= CCDF_WLABEL; else cs->sc_flags &= ~CCDF_WLABEL; break; default: return (ENOTTY); } return (0); } static int ccdsize(dev) dev_t dev; { struct ccd_softc *cs; int part, size; if (ccdopen(dev, 0, S_IFBLK, curproc)) return (-1); cs = &ccd_softc[ccdunit(dev)]; part = ccdpart(dev); if ((cs->sc_flags & CCDF_INITED) == 0) return (-1); if (cs->sc_label.d_partitions[part].p_fstype != FS_SWAP) size = -1; else size = cs->sc_label.d_partitions[part].p_size; if (ccdclose(dev, 0, S_IFBLK, curproc)) return (-1); return (size); } static int ccddump(dev) dev_t dev; { /* Not implemented. */ return ENXIO; } /* * Lookup the provided name in the filesystem. If the file exists, * is a valid block device, and isn't being used by anyone else, * set *vpp to the file's vnode. */ static int ccdlookup(path, p, vpp) char *path; struct proc *p; struct vnode **vpp; /* result */ { struct nameidata nd; struct vnode *vp; struct vattr va; int error; NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p); if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) { #ifdef DEBUG if (ccddebug & CCDB_FOLLOW|CCDB_INIT) printf("ccdlookup: vn_open error = %d\n", error); #endif return (error); } vp = nd.ni_vp; if (vp->v_usecount > 1) { VOP_UNLOCK(vp, 0, p); (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p); return (EBUSY); } if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) { #ifdef DEBUG if (ccddebug & CCDB_FOLLOW|CCDB_INIT) printf("ccdlookup: getattr error = %d\n", error); #endif VOP_UNLOCK(vp, 0, p); (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p); return (error); } /* XXX: eventually we should handle VREG, too. */ if (va.va_type != VBLK) { VOP_UNLOCK(vp, 0, p); (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p); return (ENOTBLK); } #ifdef DEBUG if (ccddebug & CCDB_VNODE) vprint("ccdlookup: vnode info", vp); #endif VOP_UNLOCK(vp, 0, p); *vpp = vp; return (0); } /* * Read the disklabel from the ccd. If one is not present, fake one * up. */ static void ccdgetdisklabel(dev) dev_t dev; { int unit = ccdunit(dev); struct ccd_softc *cs = &ccd_softc[unit]; char *errstring; struct disklabel *lp = &cs->sc_label; struct ccdgeom *ccg = &cs->sc_geom; bzero(lp, sizeof(*lp)); lp->d_secperunit = cs->sc_size; lp->d_secsize = ccg->ccg_secsize; lp->d_nsectors = ccg->ccg_nsectors; lp->d_ntracks = ccg->ccg_ntracks; lp->d_ncylinders = ccg->ccg_ncylinders; lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename)); lp->d_type = DTYPE_CCD; strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname)); lp->d_rpm = 3600; lp->d_interleave = 1; lp->d_flags = 0; lp->d_partitions[RAW_PART].p_offset = 0; lp->d_partitions[RAW_PART].p_size = cs->sc_size; lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; lp->d_npartitions = RAW_PART + 1; lp->d_bbsize = BBSIZE; /* XXX */ lp->d_sbsize = SBSIZE; /* XXX */ lp->d_magic = DISKMAGIC; lp->d_magic2 = DISKMAGIC; lp->d_checksum = dkcksum(&cs->sc_label); /* * Call the generic disklabel extraction routine. */ errstring = readdisklabel(CCDLABELDEV(dev), &cs->sc_label); if (errstring != NULL) ccdmakedisklabel(cs); #ifdef DEBUG /* It's actually extremely common to have unlabeled ccds. */ if (ccddebug & CCDB_LABEL) if (errstring != NULL) printf("ccd%d: %s\n", unit, errstring); #endif } /* * Take care of things one might want to take care of in the event * that a disklabel isn't present. */ static void ccdmakedisklabel(cs) struct ccd_softc *cs; { struct disklabel *lp = &cs->sc_label; /* * For historical reasons, if there's no disklabel present * the raw partition must be marked FS_BSDFFS. */ lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS; strncpy(lp->d_packname, "default label", sizeof(lp->d_packname)); } /* * Wait interruptibly for an exclusive lock. * * XXX * Several drivers do this; it should be abstracted and made MP-safe. */ static int ccdlock(cs) struct ccd_softc *cs; { int error; while ((cs->sc_flags & CCDF_LOCKED) != 0) { cs->sc_flags |= CCDF_WANTED; if ((error = tsleep(cs, PRIBIO | PCATCH, "ccdlck", 0)) != 0) return (error); } cs->sc_flags |= CCDF_LOCKED; return (0); } /* * Unlock and wake up any waiters. */ static void ccdunlock(cs) struct ccd_softc *cs; { cs->sc_flags &= ~CCDF_LOCKED; if ((cs->sc_flags & CCDF_WANTED) != 0) { cs->sc_flags &= ~CCDF_WANTED; wakeup(cs); } } #ifdef DEBUG static void printiinfo(ii) struct ccdiinfo *ii; { int ix, i; for (ix = 0; ii->ii_ndisk; ix++, ii++) { printf(" itab[%d]: #dk %d sblk %d soff %d", ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff); for (i = 0; i < ii->ii_ndisk; i++) printf(" %d", ii->ii_index[i]); printf("\n"); } } #endif #endif /* NCCD > 0 */ /* Local Variables: */ /* c-argdecl-indent: 8 */ /* c-continued-statement-offset: 8 */ /* c-indent-level: 8 */ /* End: */ diff --git a/sys/dev/vn/vn.c b/sys/dev/vn/vn.c index dab0ca16c34b..0730696ad82d 100644 --- a/sys/dev/vn/vn.c +++ b/sys/dev/vn/vn.c @@ -1,767 +1,768 @@ /* * Copyright (c) 1988 University of Utah. * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * the Systems Programming Group of the University of Utah Computer * Science Department. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from: Utah Hdr: vn.c 1.13 94/04/02 * * from: @(#)vn.c 8.6 (Berkeley) 4/1/94 * $FreeBSD$ */ /* * Vnode disk driver. * * Block/character interface to a vnode. Allows one to treat a file * as a disk (e.g. build a filesystem in it, mount it, etc.). * * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode * instead of a simple VOP_RDWR. We do this to avoid distorting the * local buffer cache. * * NOTE 2: There is a security issue involved with this driver. * Once mounted all access to the contents of the "mapped" file via * the special file is controlled by the permissions on the special * file, the protection of the mapped file is ignored (effectively, * by using root credentials in all transactions). * * NOTE 3: Doesn't interact with leases, should it? */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static d_ioctl_t vnioctl; static d_open_t vnopen; static d_close_t vnclose; static d_psize_t vnsize; static d_strategy_t vnstrategy; #define CDEV_MAJOR 43 #define BDEV_MAJOR 15 #define VN_BSIZE_BEST 8192 /* * cdevsw * D_DISK we want to look like a disk * D_CANFREE We support B_FREEBUF */ static struct cdevsw vn_cdevsw = { /* open */ vnopen, /* close */ vnclose, /* read */ physread, /* write */ physwrite, /* ioctl */ vnioctl, /* poll */ nopoll, /* mmap */ nommap, /* strategy */ vnstrategy, /* name */ "vn", /* maj */ CDEV_MAJOR, /* dump */ nodump, /* psize */ vnsize, /* flags */ D_DISK|D_CANFREE, /* bmaj */ BDEV_MAJOR }; #define getvnbuf() \ ((struct buf *)malloc(sizeof(struct buf), M_DEVBUF, M_WAITOK)) #define putvnbuf(bp) \ free((caddr_t)(bp), M_DEVBUF) struct vn_softc { int sc_unit; int sc_flags; /* flags */ int sc_size; /* size of vn, sc_secsize scale */ int sc_secsize; /* sector size */ struct diskslices *sc_slices; struct vnode *sc_vp; /* vnode if not NULL */ vm_object_t sc_object; /* backing object if not NULL */ struct ucred *sc_cred; /* credentials */ int sc_maxactive; /* max # of active requests */ struct buf sc_tab; /* transfer queue */ u_long sc_options; /* options */ SLIST_ENTRY(vn_softc) sc_list; }; static SLIST_HEAD(, vn_softc) vn_list; /* sc_flags */ #define VNF_INITED 0x01 #define VNF_READONLY 0x02 static u_long vn_options; #define IFOPT(vn,opt) if (((vn)->sc_options|vn_options) & (opt)) #define TESTOPT(vn,opt) (((vn)->sc_options|vn_options) & (opt)) static int vnsetcred (struct vn_softc *vn, struct ucred *cred); static void vnclear (struct vn_softc *vn); static int vn_modevent (module_t, int, void *); static int vniocattach_file (struct vn_softc *, struct vn_ioctl *, dev_t dev, int flag, struct proc *p); static int vniocattach_swap (struct vn_softc *, struct vn_ioctl *, dev_t dev, int flag, struct proc *p); static int vnclose(dev_t dev, int flags, int mode, struct proc *p) { struct vn_softc *vn = dev->si_drv1; IFOPT(vn, VN_LABELS) if (vn->sc_slices != NULL) dsclose(dev, mode, vn->sc_slices); return (0); } static struct vn_softc * vnfindvn(dev_t dev) { int unit; struct vn_softc *vn; unit = dkunit(dev); vn = dev->si_drv1; if (!vn) { SLIST_FOREACH(vn, &vn_list, sc_list) { if (vn->sc_unit == unit) { dev->si_drv1 = vn; break; } } } if (!vn) { vn = malloc(sizeof *vn, M_DEVBUF, M_WAITOK); if (!vn) return (NULL); bzero(vn, sizeof *vn); vn->sc_unit = unit; dev->si_drv1 = vn; make_dev(&vn_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0640, "vn%d", unit); SLIST_INSERT_HEAD(&vn_list, vn, sc_list); } return (vn); } static int vnopen(dev_t dev, int flags, int mode, struct proc *p) { struct vn_softc *vn; /* * Locate preexisting device */ if ((vn = dev->si_drv1) == NULL) vn = vnfindvn(dev); /* * Update si_bsize fields for device. This data will be overriden by * the slice/parition code for vn accesses through partitions, and * used directly if you open the 'whole disk' device. * * si_bsize_best must be reinitialized in case VN has been * reconfigured, plus make it at least VN_BSIZE_BEST for efficiency. */ dev->si_bsize_phys = vn->sc_secsize; dev->si_bsize_best = vn->sc_secsize; if (dev->si_bsize_best < VN_BSIZE_BEST) dev->si_bsize_best = VN_BSIZE_BEST; if ((flags & FWRITE) && (vn->sc_flags & VNF_READONLY)) return (EACCES); IFOPT(vn, VN_FOLLOW) printf("vnopen(%s, 0x%x, 0x%x, %p)\n", devtoname(dev), flags, mode, (void *)p); /* * Initialize label */ IFOPT(vn, VN_LABELS) { if (vn->sc_flags & VNF_INITED) { struct disklabel label; /* Build label for whole disk. */ bzero(&label, sizeof label); label.d_secsize = vn->sc_secsize; label.d_nsectors = 32; label.d_ntracks = 64 / (vn->sc_secsize / DEV_BSIZE); label.d_secpercyl = label.d_nsectors * label.d_ntracks; label.d_ncylinders = vn->sc_size / label.d_secpercyl; label.d_secperunit = vn->sc_size; label.d_partitions[RAW_PART].p_size = vn->sc_size; return (dsopen(dev, mode, 0, &vn->sc_slices, &label)); } if (dkslice(dev) != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART || mode != S_IFCHR) { return (ENXIO); } } return(0); } /* * vnstrategy: * * Run strategy routine for VN device. We use VOP_READ/VOP_WRITE calls * for vnode-backed vn's, and the new vm_pager_strategy() call for * vm_object-backed vn's. * * Currently B_ASYNC is only partially handled - for OBJT_SWAP I/O only. * * NOTE: bp->b_blkno is DEV_BSIZE'd. We must generate bp->b_pblkno for * our uio or vn_pager_strategy() call that is vn->sc_secsize'd */ static void vnstrategy(struct buf *bp) { int unit; struct vn_softc *vn; int error; int isvplocked = 0; struct uio auio; struct iovec aiov; unit = dkunit(bp->b_dev); vn = bp->b_dev->si_drv1; if (!vn) vn = vnfindvn(bp->b_dev); IFOPT(vn, VN_DEBUG) printf("vnstrategy(%p): unit %d\n", bp, unit); if ((vn->sc_flags & VNF_INITED) == 0) { bp->b_error = ENXIO; bp->b_flags |= B_ERROR; biodone(bp); return; } bp->b_resid = bp->b_bcount; IFOPT(vn, VN_LABELS) { if (vn->sc_slices != NULL && dscheck(bp, vn->sc_slices) <= 0) { bp->b_flags |= B_INVAL; biodone(bp); return; } } else { int pbn; /* in sc_secsize chunks */ long sz; /* in sc_secsize chunks */ pbn = bp->b_blkno / (vn->sc_secsize / DEV_BSIZE); sz = howmany(bp->b_bcount, vn->sc_secsize); /* * If out of bounds return an error. If at the EOF point, * simply read or write less. */ if (pbn < 0 || pbn >= vn->sc_size) { if (pbn != vn->sc_size) { bp->b_error = EINVAL; bp->b_flags |= B_ERROR | B_INVAL; } biodone(bp); return; } /* * If the request crosses EOF, truncate the request. */ if (pbn + sz > vn->sc_size) { bp->b_bcount = (vn->sc_size - pbn) * vn->sc_secsize; bp->b_resid = bp->b_bcount; } bp->b_pblkno = pbn; } if (vn->sc_vp && (bp->b_flags & B_FREEBUF)) { /* * Not handled for vnode-backed element yet. */ biodone(bp); } else if (vn->sc_vp) { /* * VNODE I/O * * If an error occurs, we set B_ERROR but we do not set * B_INVAL because (for a write anyway), the buffer is * still valid. */ aiov.iov_base = bp->b_data; aiov.iov_len = bp->b_bcount; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = (vm_ooffset_t)bp->b_pblkno * vn->sc_secsize; auio.uio_segflg = UIO_SYSSPACE; if( bp->b_flags & B_READ) auio.uio_rw = UIO_READ; else auio.uio_rw = UIO_WRITE; auio.uio_resid = bp->b_bcount; auio.uio_procp = curproc; if (!VOP_ISLOCKED(vn->sc_vp)) { isvplocked = 1; vn_lock(vn->sc_vp, LK_EXCLUSIVE | LK_RETRY, curproc); } if( bp->b_flags & B_READ) error = VOP_READ(vn->sc_vp, &auio, 0, vn->sc_cred); else error = VOP_WRITE(vn->sc_vp, &auio, 0, vn->sc_cred); if (isvplocked) { VOP_UNLOCK(vn->sc_vp, 0, curproc); isvplocked = 0; } bp->b_resid = auio.uio_resid; if (error) { bp->b_error = error; bp->b_flags |= B_ERROR; } biodone(bp); } else if (vn->sc_object) { /* * OBJT_SWAP I/O * * ( handles read, write, freebuf ) * * Note: if we pre-reserved swap, B_FREEBUF is disabled */ KASSERT((bp->b_bufsize & (vn->sc_secsize - 1)) == 0, ("vnstrategy: buffer %p to small for physio", bp)); if ((bp->b_flags & B_FREEBUF) && TESTOPT(vn, VN_RESERVE)) { biodone(bp); } else { vm_pager_strategy(vn->sc_object, bp); } } else { bp->b_flags |= B_ERROR; bp->b_error = EINVAL; biodone(bp); } } /* ARGSUSED */ static int vnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) { struct vn_softc *vn; struct vn_ioctl *vio; int error; u_long *f; vn = dev->si_drv1; IFOPT(vn,VN_FOLLOW) printf("vnioctl(%s, 0x%lx, %p, 0x%x, %p): unit %d\n", devtoname(dev), cmd, (void *)data, flag, (void *)p, dkunit(dev)); switch (cmd) { case VNIOCATTACH: case VNIOCDETACH: case VNIOCGSET: case VNIOCGCLEAR: case VNIOCUSET: case VNIOCUCLEAR: goto vn_specific; } IFOPT(vn,VN_LABELS) { if (vn->sc_slices != NULL) { error = dsioctl(dev, cmd, data, flag, &vn->sc_slices); if (error != ENOIOCTL) return (error); } if (dkslice(dev) != WHOLE_DISK_SLICE || dkpart(dev) != RAW_PART) return (ENOTTY); } vn_specific: error = suser(p); if (error) return (error); vio = (struct vn_ioctl *)data; f = (u_long*)data; switch (cmd) { case VNIOCATTACH: if (vn->sc_flags & VNF_INITED) return(EBUSY); if (vio->vn_file == NULL) error = vniocattach_swap(vn, vio, dev, flag, p); else error = vniocattach_file(vn, vio, dev, flag, p); break; case VNIOCDETACH: if ((vn->sc_flags & VNF_INITED) == 0) return(ENXIO); /* * XXX handle i/o in progress. Return EBUSY, or wait, or * flush the i/o. * XXX handle multiple opens of the device. Return EBUSY, * or revoke the fd's. * How are these problems handled for removable and failing * hardware devices? (Hint: They are not) */ vnclear(vn); IFOPT(vn, VN_FOLLOW) printf("vnioctl: CLRed\n"); break; case VNIOCGSET: vn_options |= *f; *f = vn_options; break; case VNIOCGCLEAR: vn_options &= ~(*f); *f = vn_options; break; case VNIOCUSET: vn->sc_options |= *f; *f = vn->sc_options; break; case VNIOCUCLEAR: vn->sc_options &= ~(*f); *f = vn->sc_options; break; default: error = ENOTTY; break; } return(error); } /* * vniocattach_file: * * Attach a file to a VN partition. Return the size in the vn_size * field. */ static int vniocattach_file(vn, vio, dev, flag, p) struct vn_softc *vn; struct vn_ioctl *vio; dev_t dev; int flag; struct proc *p; { struct vattr vattr; struct nameidata nd; int error, flags; flags = FREAD|FWRITE; NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vn_file, p); error = vn_open(&nd, flags, 0); if (error) { if (error != EACCES && error != EPERM && error != EROFS) return (error); flags &= ~FWRITE; NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vn_file, p); error = vn_open(&nd, flags, 0); if (error) return (error); } if (nd.ni_vp->v_type != VREG || (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p))) { VOP_UNLOCK(nd.ni_vp, 0, p); (void) vn_close(nd.ni_vp, flags, p->p_ucred, p); return (error ? error : EINVAL); } VOP_UNLOCK(nd.ni_vp, 0, p); vn->sc_secsize = DEV_BSIZE; vn->sc_vp = nd.ni_vp; /* * If the size is specified, override the file attributes. Note that * the vn_size argument is in PAGE_SIZE sized blocks. */ if (vio->vn_size) vn->sc_size = (quad_t)vio->vn_size * PAGE_SIZE / vn->sc_secsize; else vn->sc_size = vattr.va_size / vn->sc_secsize; error = vnsetcred(vn, p->p_ucred); if (error) { (void) vn_close(nd.ni_vp, flags, p->p_ucred, p); return(error); } vn->sc_flags |= VNF_INITED; if (flags == FREAD) vn->sc_flags |= VNF_READONLY; IFOPT(vn, VN_LABELS) { /* * Reopen so that `ds' knows which devices are open. * If this is the first VNIOCSET, then we've * guaranteed that the device is the cdev and that * no other slices or labels are open. Otherwise, * we rely on VNIOCCLR not being abused. */ error = vnopen(dev, flag, S_IFCHR, p); if (error) vnclear(vn); } IFOPT(vn, VN_FOLLOW) printf("vnioctl: SET vp %p size %x blks\n", vn->sc_vp, vn->sc_size); return(0); } /* * vniocattach_swap: * * Attach swap backing store to a VN partition of the size specified * in vn_size. */ static int vniocattach_swap(vn, vio, dev, flag, p) struct vn_softc *vn; struct vn_ioctl *vio; dev_t dev; int flag; struct proc *p; { int error; /* * Range check. Disallow negative sizes or any size less then the * size of a page. Then round to a page. */ if (vio->vn_size <= 0) return(EDOM); /* * Allocate an OBJT_SWAP object. * * sc_secsize is PAGE_SIZE'd * * vio->vn_size is in PAGE_SIZE'd chunks. * sc_size must be in PAGE_SIZE'd chunks. * Note the truncation. */ vn->sc_secsize = PAGE_SIZE; vn->sc_size = vio->vn_size; vn->sc_object = vm_pager_allocate(OBJT_SWAP, NULL, vn->sc_secsize * (vm_ooffset_t)vio->vn_size, VM_PROT_DEFAULT, 0); IFOPT(vn, VN_RESERVE) { if (swap_pager_reserve(vn->sc_object, 0, vn->sc_size) < 0) { vm_pager_deallocate(vn->sc_object); vn->sc_object = NULL; return(EDOM); } } vn->sc_flags |= VNF_INITED; error = vnsetcred(vn, p->p_ucred); if (error == 0) { IFOPT(vn, VN_LABELS) { /* * Reopen so that `ds' knows which devices are open. * If this is the first VNIOCSET, then we've * guaranteed that the device is the cdev and that * no other slices or labels are open. Otherwise, * we rely on VNIOCCLR not being abused. */ error = vnopen(dev, flag, S_IFCHR, p); } } if (error == 0) { IFOPT(vn, VN_FOLLOW) { printf("vnioctl: SET vp %p size %x\n", vn->sc_vp, vn->sc_size); } } if (error) vnclear(vn); return(error); } /* * Duplicate the current processes' credentials. Since we are called only * as the result of a SET ioctl and only root can do that, any future access * to this "disk" is essentially as root. Note that credentials may change * if some other uid can write directly to the mapped file (NFS). */ int vnsetcred(struct vn_softc *vn, struct ucred *cred) { struct uio auio; struct iovec aiov; char *tmpbuf; int error = 0; /* * Set credits in our softc */ if (vn->sc_cred) crfree(vn->sc_cred); vn->sc_cred = crdup(cred); /* * Horrible kludge to establish credentials for NFS XXX. */ if (vn->sc_vp) { tmpbuf = malloc(vn->sc_secsize, M_TEMP, M_WAITOK); aiov.iov_base = tmpbuf; aiov.iov_len = vn->sc_secsize; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_offset = 0; auio.uio_rw = UIO_READ; auio.uio_segflg = UIO_SYSSPACE; auio.uio_resid = aiov.iov_len; vn_lock(vn->sc_vp, LK_EXCLUSIVE | LK_RETRY, curproc); error = VOP_READ(vn->sc_vp, &auio, 0, vn->sc_cred); VOP_UNLOCK(vn->sc_vp, 0, curproc); free(tmpbuf, M_TEMP); } return (error); } void vnclear(struct vn_softc *vn) { struct proc *p = curproc; /* XXX */ IFOPT(vn, VN_FOLLOW) printf("vnclear(%p): vp=%p\n", vn, vn->sc_vp); if (vn->sc_slices != NULL) dsgone(&vn->sc_slices); vn->sc_flags &= ~VNF_INITED; if (vn->sc_vp != NULL) { (void)vn_close(vn->sc_vp, vn->sc_flags & VNF_READONLY ? FREAD : (FREAD|FWRITE), vn->sc_cred, p); vn->sc_vp = NULL; } vn->sc_flags &= ~VNF_READONLY; if (vn->sc_cred) { crfree(vn->sc_cred); vn->sc_cred = NULL; } if (vn->sc_object != NULL) { vm_pager_deallocate(vn->sc_object); vn->sc_object = NULL; } vn->sc_size = 0; } static int vnsize(dev_t dev) { struct vn_softc *vn; vn = dev->si_drv1; if (!vn) return(-1); if ((vn->sc_flags & VNF_INITED) == 0) return(-1); return(vn->sc_size); } static int vn_modevent(module_t mod, int type, void *data) { struct vn_softc *vn; switch (type) { case MOD_LOAD: + cdevsw_add(&vn_cdevsw); break; case MOD_UNLOAD: /* fall through */ case MOD_SHUTDOWN: for (;;) { vn = SLIST_FIRST(&vn_list); if (!vn) break; SLIST_REMOVE_HEAD(&vn_list, sc_list); if (vn->sc_flags & VNF_INITED) vnclear(vn); free(vn, M_DEVBUF); } break; default: break; } return 0; } -DEV_MODULE(vn, CDEV_MAJOR, BDEV_MAJOR, vn_cdevsw, vn_modevent, 0); +DEV_MODULE(vn, vn_modevent, 0); diff --git a/sys/geom/geom_ccd.c b/sys/geom/geom_ccd.c index c4cc2bd10d9e..75ee082fba29 100644 --- a/sys/geom/geom_ccd.c +++ b/sys/geom/geom_ccd.c @@ -1,1767 +1,1768 @@ /* $FreeBSD$ */ /* $NetBSD: ccd.c,v 1.22 1995/12/08 19:13:26 thorpej Exp $ */ /* * Copyright (c) 1995 Jason R. Thorpe. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the NetBSD Project * by Jason R. Thorpe. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Copyright (c) 1988 University of Utah. * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * the Systems Programming Group of the University of Utah Computer * Science Department. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from: Utah $Hdr: cd.c 1.6 90/11/28$ * * @(#)cd.c 8.2 (Berkeley) 11/16/93 */ /* * "Concatenated" disk driver. * * Dynamic configuration and disklabel support by: * Jason R. Thorpe * Numerical Aerodynamic Simulation Facility * Mail Stop 258-6 * NASA Ames Research Center * Moffett Field, CA 94035 */ #include "ccd.h" #if NCCD > 0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(CCDDEBUG) && !defined(DEBUG) #define DEBUG #endif #ifdef DEBUG #define CCDB_FOLLOW 0x01 #define CCDB_INIT 0x02 #define CCDB_IO 0x04 #define CCDB_LABEL 0x08 #define CCDB_VNODE 0x10 static int ccddebug = CCDB_FOLLOW | CCDB_INIT | CCDB_IO | CCDB_LABEL | CCDB_VNODE; SYSCTL_INT(_debug, OID_AUTO, ccddebug, CTLFLAG_RW, &ccddebug, 0, ""); #undef DEBUG #endif #define ccdunit(x) dkunit(x) #define ccdpart(x) dkpart(x) /* This is how mirroring works (only writes are special): When initiating a write, ccdbuffer() returns two "struct ccdbuf *"s linked together by the cb_mirror field. "cb_pflags & CCDPF_MIRROR_DONE" is set to 0 on both of them. When a component returns to ccdiodone(), it checks if "cb_pflags & CCDPF_MIRROR_DONE" is set or not. If not, it sets the partner's flag and returns. If it is, it means its partner has already returned, so it will go to the regular cleanup. */ struct ccdbuf { struct buf cb_buf; /* new I/O buf */ struct buf *cb_obp; /* ptr. to original I/O buf */ struct ccdbuf *cb_freenext; /* free list link */ int cb_unit; /* target unit */ int cb_comp; /* target component */ int cb_pflags; /* mirror/parity status flag */ struct ccdbuf *cb_mirror; /* mirror counterpart */ }; /* bits in cb_pflags */ #define CCDPF_MIRROR_DONE 1 /* if set, mirror counterpart is done */ #define CCDLABELDEV(dev) \ (makedev(major((dev)), dkmakeminor(ccdunit((dev)), 0, RAW_PART))) static d_open_t ccdopen; static d_close_t ccdclose; static d_strategy_t ccdstrategy; static d_ioctl_t ccdioctl; static d_dump_t ccddump; static d_psize_t ccdsize; #define NCCDFREEHIWAT 16 #define CDEV_MAJOR 74 #define BDEV_MAJOR 21 static struct cdevsw ccd_cdevsw = { /* open */ ccdopen, /* close */ ccdclose, /* read */ physread, /* write */ physwrite, /* ioctl */ ccdioctl, /* poll */ nopoll, /* mmap */ nommap, /* strategy */ ccdstrategy, /* name */ "ccd", /* maj */ CDEV_MAJOR, /* dump */ ccddump, /* psize */ ccdsize, /* flags */ D_DISK, /* bmaj */ BDEV_MAJOR }; /* called during module initialization */ static void ccdattach __P((void)); static int ccd_modevent __P((module_t, int, void *)); /* called by biodone() at interrupt time */ static void ccdiodone __P((struct ccdbuf *cbp)); static void ccdstart __P((struct ccd_softc *, struct buf *)); static void ccdinterleave __P((struct ccd_softc *, int)); static void ccdintr __P((struct ccd_softc *, struct buf *)); static int ccdinit __P((struct ccddevice *, char **, struct proc *)); static int ccdlookup __P((char *, struct proc *p, struct vnode **)); static void ccdbuffer __P((struct ccdbuf **ret, struct ccd_softc *, struct buf *, daddr_t, caddr_t, long)); static void ccdgetdisklabel __P((dev_t)); static void ccdmakedisklabel __P((struct ccd_softc *)); static int ccdlock __P((struct ccd_softc *)); static void ccdunlock __P((struct ccd_softc *)); #ifdef DEBUG static void printiinfo __P((struct ccdiinfo *)); #endif /* Non-private for the benefit of libkvm. */ struct ccd_softc *ccd_softc; struct ccddevice *ccddevs; struct ccdbuf *ccdfreebufs; static int numccdfreebufs; static int numccd = 0; /* * getccdbuf() - Allocate and zero a ccd buffer. * * This routine is called at splbio(). */ static __inline struct ccdbuf * getccdbuf(struct ccdbuf *cpy) { struct ccdbuf *cbp; /* * Allocate from freelist or malloc as necessary */ if ((cbp = ccdfreebufs) != NULL) { ccdfreebufs = cbp->cb_freenext; --numccdfreebufs; } else { cbp = malloc(sizeof(struct ccdbuf), M_DEVBUF, M_WAITOK); } /* * Used by mirroring code */ if (cpy) bcopy(cpy, cbp, sizeof(struct ccdbuf)); else bzero(cbp, sizeof(struct ccdbuf)); /* * independant struct buf initialization */ LIST_INIT(&cbp->cb_buf.b_dep); BUF_LOCKINIT(&cbp->cb_buf); BUF_LOCK(&cbp->cb_buf, LK_EXCLUSIVE); BUF_KERNPROC(&cbp->cb_buf); return(cbp); } /* * putccdbuf() - Free a ccd buffer. * * This routine is called at splbio(). */ static __inline void putccdbuf(struct ccdbuf *cbp) { BUF_UNLOCK(&cbp->cb_buf); BUF_LOCKFREE(&cbp->cb_buf); if (numccdfreebufs < NCCDFREEHIWAT) { cbp->cb_freenext = ccdfreebufs; ccdfreebufs = cbp; ++numccdfreebufs; } else { free((caddr_t)cbp, M_DEVBUF); } } /* * Number of blocks to untouched in front of a component partition. * This is to avoid violating its disklabel area when it starts at the * beginning of the slice. */ #if !defined(CCD_OFFSET) #define CCD_OFFSET 16 #endif /* * Called by main() during pseudo-device attachment. All we need * to do is allocate enough space for devices to be configured later, and * add devsw entries. */ static void ccdattach() { int i; int num = NCCD; if (num > 1) printf("ccd0-%d: Concatenated disk drivers\n", num-1); else printf("ccd0: Concatenated disk driver\n"); ccd_softc = (struct ccd_softc *)malloc(num * sizeof(struct ccd_softc), M_DEVBUF, M_NOWAIT); ccddevs = (struct ccddevice *)malloc(num * sizeof(struct ccddevice), M_DEVBUF, M_NOWAIT); if ((ccd_softc == NULL) || (ccddevs == NULL)) { printf("WARNING: no memory for concatenated disks\n"); if (ccd_softc != NULL) free(ccd_softc, M_DEVBUF); if (ccddevs != NULL) free(ccddevs, M_DEVBUF); return; } numccd = num; bzero(ccd_softc, num * sizeof(struct ccd_softc)); bzero(ccddevs, num * sizeof(struct ccddevice)); + cdevsw_add(&ccd_cdevsw); /* XXX: is this necessary? */ for (i = 0; i < numccd; ++i) ccddevs[i].ccd_dk = -1; } static int ccd_modevent(mod, type, data) module_t mod; int type; void *data; { int error = 0; switch (type) { case MOD_LOAD: ccdattach(); break; case MOD_UNLOAD: printf("ccd0: Unload not supported!\n"); error = EOPNOTSUPP; break; default: /* MOD_SHUTDOWN etc */ break; } return (error); } -DEV_MODULE(ccd, CDEV_MAJOR, BDEV_MAJOR, ccd_cdevsw, ccd_modevent, NULL); +DEV_MODULE(ccd, ccd_modevent, NULL); static int ccdinit(ccd, cpaths, p) struct ccddevice *ccd; char **cpaths; struct proc *p; { struct ccd_softc *cs = &ccd_softc[ccd->ccd_unit]; struct ccdcinfo *ci = NULL; /* XXX */ size_t size; int ix; struct vnode *vp; size_t minsize; int maxsecsize; struct partinfo dpart; struct ccdgeom *ccg = &cs->sc_geom; char tmppath[MAXPATHLEN]; int error = 0; #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccdinit: unit %d\n", ccd->ccd_unit); #endif cs->sc_size = 0; cs->sc_ileave = ccd->ccd_interleave; cs->sc_nccdisks = ccd->ccd_ndev; /* Allocate space for the component info. */ cs->sc_cinfo = malloc(cs->sc_nccdisks * sizeof(struct ccdcinfo), M_DEVBUF, M_WAITOK); /* * Verify that each component piece exists and record * relevant information about it. */ maxsecsize = 0; minsize = 0; for (ix = 0; ix < cs->sc_nccdisks; ix++) { vp = ccd->ccd_vpp[ix]; ci = &cs->sc_cinfo[ix]; ci->ci_vp = vp; /* * Copy in the pathname of the component. */ bzero(tmppath, sizeof(tmppath)); /* sanity */ if ((error = copyinstr(cpaths[ix], tmppath, MAXPATHLEN, &ci->ci_pathlen)) != 0) { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: can't copy path, error = %d\n", ccd->ccd_unit, error); #endif goto fail; } ci->ci_path = malloc(ci->ci_pathlen, M_DEVBUF, M_WAITOK); bcopy(tmppath, ci->ci_path, ci->ci_pathlen); ci->ci_dev = vn_todev(vp); /* * Get partition information for the component. */ if ((error = VOP_IOCTL(vp, DIOCGPART, (caddr_t)&dpart, FREAD, p->p_ucred, p)) != 0) { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: %s: ioctl failed, error = %d\n", ccd->ccd_unit, ci->ci_path, error); #endif goto fail; } if (dpart.part->p_fstype == FS_BSDFFS) { maxsecsize = ((dpart.disklab->d_secsize > maxsecsize) ? dpart.disklab->d_secsize : maxsecsize); size = dpart.part->p_size - CCD_OFFSET; } else { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: %s: incorrect partition type\n", ccd->ccd_unit, ci->ci_path); #endif error = EFTYPE; goto fail; } /* * Calculate the size, truncating to an interleave * boundary if necessary. */ if (cs->sc_ileave > 1) size -= size % cs->sc_ileave; if (size == 0) { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: %s: size == 0\n", ccd->ccd_unit, ci->ci_path); #endif error = ENODEV; goto fail; } if (minsize == 0 || size < minsize) minsize = size; ci->ci_size = size; cs->sc_size += size; } /* * Don't allow the interleave to be smaller than * the biggest component sector. */ if ((cs->sc_ileave > 0) && (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) { #ifdef DEBUG if (ccddebug & (CCDB_FOLLOW|CCDB_INIT)) printf("ccd%d: interleave must be at least %d\n", ccd->ccd_unit, (maxsecsize / DEV_BSIZE)); #endif error = EINVAL; goto fail; } /* * If uniform interleave is desired set all sizes to that of * the smallest component. This will guarentee that a single * interleave table is generated. * * Lost space must be taken into account when calculating the * overall size. Half the space is lost when CCDF_MIRROR is * specified. One disk is lost when CCDF_PARITY is specified. */ if (ccd->ccd_flags & CCDF_UNIFORM) { for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++) { ci->ci_size = minsize; } if (ccd->ccd_flags & CCDF_MIRROR) { /* * Check to see if an even number of components * have been specified. The interleave must also * be non-zero in order for us to be able to * guarentee the topology. */ if (cs->sc_nccdisks % 2) { printf("ccd%d: mirroring requires an even number of disks\n", ccd->ccd_unit ); error = EINVAL; goto fail; } if (cs->sc_ileave == 0) { printf("ccd%d: an interleave must be specified when mirroring\n", ccd->ccd_unit); error = EINVAL; goto fail; } cs->sc_size = (cs->sc_nccdisks/2) * minsize; } else if (ccd->ccd_flags & CCDF_PARITY) { cs->sc_size = (cs->sc_nccdisks-1) * minsize; } else { if (cs->sc_ileave == 0) { printf("ccd%d: an interleave must be specified when using parity\n", ccd->ccd_unit); error = EINVAL; goto fail; } cs->sc_size = cs->sc_nccdisks * minsize; } } /* * Construct the interleave table. */ ccdinterleave(cs, ccd->ccd_unit); /* * Create pseudo-geometry based on 1MB cylinders. It's * pretty close. */ ccg->ccg_secsize = maxsecsize; ccg->ccg_ntracks = 1; ccg->ccg_nsectors = 1024 * 1024 / ccg->ccg_secsize; ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors; /* * Add an devstat entry for this device. */ devstat_add_entry(&cs->device_stats, "ccd", ccd->ccd_unit, ccg->ccg_secsize, DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_ASC0 |DEVSTAT_TYPE_IF_OTHER, DEVSTAT_PRIORITY_CCD); cs->sc_flags |= CCDF_INITED; cs->sc_cflags = ccd->ccd_flags; /* So we can find out later... */ cs->sc_unit = ccd->ccd_unit; return (0); fail: while (ci > cs->sc_cinfo) { ci--; free(ci->ci_path, M_DEVBUF); } free(cs->sc_cinfo, M_DEVBUF); return (error); } static void ccdinterleave(cs, unit) struct ccd_softc *cs; int unit; { struct ccdcinfo *ci, *smallci; struct ccdiinfo *ii; daddr_t bn, lbn; int ix; u_long size; #ifdef DEBUG if (ccddebug & CCDB_INIT) printf("ccdinterleave(%x): ileave %d\n", cs, cs->sc_ileave); #endif /* * Allocate an interleave table. The worst case occurs when each * of N disks is of a different size, resulting in N interleave * tables. * * Chances are this is too big, but we don't care. */ size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo); cs->sc_itable = (struct ccdiinfo *)malloc(size, M_DEVBUF, M_WAITOK); bzero((caddr_t)cs->sc_itable, size); /* * Trivial case: no interleave (actually interleave of disk size). * Each table entry represents a single component in its entirety. * * An interleave of 0 may not be used with a mirror or parity setup. */ if (cs->sc_ileave == 0) { bn = 0; ii = cs->sc_itable; for (ix = 0; ix < cs->sc_nccdisks; ix++) { /* Allocate space for ii_index. */ ii->ii_index = malloc(sizeof(int), M_DEVBUF, M_WAITOK); ii->ii_ndisk = 1; ii->ii_startblk = bn; ii->ii_startoff = 0; ii->ii_index[0] = ix; bn += cs->sc_cinfo[ix].ci_size; ii++; } ii->ii_ndisk = 0; #ifdef DEBUG if (ccddebug & CCDB_INIT) printiinfo(cs->sc_itable); #endif return; } /* * The following isn't fast or pretty; it doesn't have to be. */ size = 0; bn = lbn = 0; for (ii = cs->sc_itable; ; ii++) { /* * Allocate space for ii_index. We might allocate more then * we use. */ ii->ii_index = malloc((sizeof(int) * cs->sc_nccdisks), M_DEVBUF, M_WAITOK); /* * Locate the smallest of the remaining components */ smallci = NULL; for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++) { if (ci->ci_size > size && (smallci == NULL || ci->ci_size < smallci->ci_size)) { smallci = ci; } } /* * Nobody left, all done */ if (smallci == NULL) { ii->ii_ndisk = 0; break; } /* * Record starting logical block using an sc_ileave blocksize. */ ii->ii_startblk = bn / cs->sc_ileave; /* * Record starting comopnent block using an sc_ileave * blocksize. This value is relative to the beginning of * a component disk. */ ii->ii_startoff = lbn; /* * Determine how many disks take part in this interleave * and record their indices. */ ix = 0; for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++) { if (ci->ci_size >= smallci->ci_size) { ii->ii_index[ix++] = ci - cs->sc_cinfo; } } ii->ii_ndisk = ix; bn += ix * (smallci->ci_size - size); lbn = smallci->ci_size / cs->sc_ileave; size = smallci->ci_size; } #ifdef DEBUG if (ccddebug & CCDB_INIT) printiinfo(cs->sc_itable); #endif } /* ARGSUSED */ static int ccdopen(dev, flags, fmt, p) dev_t dev; int flags, fmt; struct proc *p; { int unit = ccdunit(dev); struct ccd_softc *cs; struct disklabel *lp; int error = 0, part, pmask; #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdopen(%x, %x)\n", dev, flags); #endif if (unit >= numccd) return (ENXIO); cs = &ccd_softc[unit]; if ((error = ccdlock(cs)) != 0) return (error); lp = &cs->sc_label; part = ccdpart(dev); pmask = (1 << part); /* * If we're initialized, check to see if there are any other * open partitions. If not, then it's safe to update * the in-core disklabel. */ if ((cs->sc_flags & CCDF_INITED) && (cs->sc_openmask == 0)) ccdgetdisklabel(dev); /* Check that the partition exists. */ if (part != RAW_PART && ((part >= lp->d_npartitions) || (lp->d_partitions[part].p_fstype == FS_UNUSED))) { error = ENXIO; goto done; } /* Prevent our unit from being unconfigured while open. */ switch (fmt) { case S_IFCHR: cs->sc_copenmask |= pmask; break; case S_IFBLK: cs->sc_bopenmask |= pmask; break; } cs->sc_openmask = cs->sc_copenmask | cs->sc_bopenmask; done: ccdunlock(cs); return (0); } /* ARGSUSED */ static int ccdclose(dev, flags, fmt, p) dev_t dev; int flags, fmt; struct proc *p; { int unit = ccdunit(dev); struct ccd_softc *cs; int error = 0, part; #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdclose(%x, %x)\n", dev, flags); #endif if (unit >= numccd) return (ENXIO); cs = &ccd_softc[unit]; if ((error = ccdlock(cs)) != 0) return (error); part = ccdpart(dev); /* ...that much closer to allowing unconfiguration... */ switch (fmt) { case S_IFCHR: cs->sc_copenmask &= ~(1 << part); break; case S_IFBLK: cs->sc_bopenmask &= ~(1 << part); break; } cs->sc_openmask = cs->sc_copenmask | cs->sc_bopenmask; ccdunlock(cs); return (0); } static void ccdstrategy(bp) struct buf *bp; { int unit = ccdunit(bp->b_dev); struct ccd_softc *cs = &ccd_softc[unit]; int s; int wlabel; struct disklabel *lp; #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdstrategy(%x): unit %d\n", bp, unit); #endif if ((cs->sc_flags & CCDF_INITED) == 0) { bp->b_error = ENXIO; bp->b_flags |= B_ERROR; goto done; } /* If it's a nil transfer, wake up the top half now. */ if (bp->b_bcount == 0) goto done; lp = &cs->sc_label; /* * Do bounds checking and adjust transfer. If there's an * error, the bounds check will flag that for us. */ wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING); if (ccdpart(bp->b_dev) != RAW_PART) { if (bounds_check_with_label(bp, lp, wlabel) <= 0) goto done; } else { int pbn; /* in sc_secsize chunks */ long sz; /* in sc_secsize chunks */ pbn = bp->b_blkno / (cs->sc_geom.ccg_secsize / DEV_BSIZE); sz = howmany(bp->b_bcount, cs->sc_geom.ccg_secsize); /* * If out of bounds return an error. If at the EOF point, * simply read or write less. */ if (pbn < 0 || pbn >= cs->sc_size) { bp->b_resid = bp->b_bcount; if (pbn != cs->sc_size) { bp->b_error = EINVAL; bp->b_flags |= B_ERROR | B_INVAL; } goto done; } /* * If the request crosses EOF, truncate the request. */ if (pbn + sz > cs->sc_size) { bp->b_bcount = (cs->sc_size - pbn) * cs->sc_geom.ccg_secsize; } } bp->b_resid = bp->b_bcount; /* * "Start" the unit. */ s = splbio(); ccdstart(cs, bp); splx(s); return; done: biodone(bp); } static void ccdstart(cs, bp) struct ccd_softc *cs; struct buf *bp; { long bcount, rcount; struct ccdbuf *cbp[4]; /* XXX! : 2 reads and 2 writes for RAID 4/5 */ caddr_t addr; daddr_t bn; struct partition *pp; #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdstart(%x, %x)\n", cs, bp); #endif /* Record the transaction start */ devstat_start_transaction(&cs->device_stats); /* * Translate the partition-relative block number to an absolute. */ bn = bp->b_blkno; if (ccdpart(bp->b_dev) != RAW_PART) { pp = &cs->sc_label.d_partitions[ccdpart(bp->b_dev)]; bn += pp->p_offset; } /* * Allocate component buffers and fire off the requests */ addr = bp->b_data; for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) { ccdbuffer(cbp, cs, bp, bn, addr, bcount); rcount = cbp[0]->cb_buf.b_bcount; if (cs->sc_cflags & CCDF_MIRROR) { /* * Mirroring. Writes go to both disks, reads are * taken from whichever disk seems most appropriate. * * We attempt to localize reads to the disk whos arm * is nearest the read request. We ignore seeks due * to writes when making this determination and we * also try to avoid hogging. */ if ((cbp[0]->cb_buf.b_flags & B_READ) == 0) { cbp[0]->cb_buf.b_vp->v_numoutput++; cbp[1]->cb_buf.b_vp->v_numoutput++; VOP_STRATEGY(cbp[0]->cb_buf.b_vp, &cbp[0]->cb_buf); VOP_STRATEGY(cbp[1]->cb_buf.b_vp, &cbp[1]->cb_buf); } else { int pick = cs->sc_pick; daddr_t range = cs->sc_size / 16; if (bn < cs->sc_blk[pick] - range || bn > cs->sc_blk[pick] + range ) { cs->sc_pick = pick = 1 - pick; } cs->sc_blk[pick] = bn + btodb(rcount); VOP_STRATEGY(cbp[pick]->cb_buf.b_vp, &cbp[pick]->cb_buf); } } else { /* * Not mirroring */ if ((cbp[0]->cb_buf.b_flags & B_READ) == 0) cbp[0]->cb_buf.b_vp->v_numoutput++; VOP_STRATEGY(cbp[0]->cb_buf.b_vp, &cbp[0]->cb_buf); } bn += btodb(rcount); addr += rcount; } } /* * Build a component buffer header. */ static void ccdbuffer(cb, cs, bp, bn, addr, bcount) struct ccdbuf **cb; struct ccd_softc *cs; struct buf *bp; daddr_t bn; caddr_t addr; long bcount; { struct ccdcinfo *ci, *ci2 = NULL; /* XXX */ struct ccdbuf *cbp; daddr_t cbn, cboff; off_t cbc; #ifdef DEBUG if (ccddebug & CCDB_IO) printf("ccdbuffer(%x, %x, %d, %x, %d)\n", cs, bp, bn, addr, bcount); #endif /* * Determine which component bn falls in. */ cbn = bn; cboff = 0; if (cs->sc_ileave == 0) { /* * Serially concatenated and neither a mirror nor a parity * config. This is a special case. */ daddr_t sblk; sblk = 0; for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++) sblk += ci->ci_size; cbn -= sblk; } else { struct ccdiinfo *ii; int ccdisk, off; /* * Calculate cbn, the logical superblock (sc_ileave chunks), * and cboff, a normal block offset (DEV_BSIZE chunks) relative * to cbn. */ cboff = cbn % cs->sc_ileave; /* DEV_BSIZE gran */ cbn = cbn / cs->sc_ileave; /* DEV_BSIZE * ileave gran */ /* * Figure out which interleave table to use. */ for (ii = cs->sc_itable; ii->ii_ndisk; ii++) { if (ii->ii_startblk > cbn) break; } ii--; /* * off is the logical superblock relative to the beginning * of this interleave block. */ off = cbn - ii->ii_startblk; /* * We must calculate which disk component to use (ccdisk), * and recalculate cbn to be the superblock relative to * the beginning of the component. This is typically done by * adding 'off' and ii->ii_startoff together. However, 'off' * must typically be divided by the number of components in * this interleave array to be properly convert it from a * CCD-relative logical superblock number to a * component-relative superblock number. */ if (ii->ii_ndisk == 1) { /* * When we have just one disk, it can't be a mirror * or a parity config. */ ccdisk = ii->ii_index[0]; cbn = ii->ii_startoff + off; } else { if (cs->sc_cflags & CCDF_MIRROR) { /* * We have forced a uniform mapping, resulting * in a single interleave array. We double * up on the first half of the available * components and our mirror is in the second * half. This only works with a single * interleave array because doubling up * doubles the number of sectors, so there * cannot be another interleave array because * the next interleave array's calculations * would be off. */ int ndisk2 = ii->ii_ndisk / 2; ccdisk = ii->ii_index[off % ndisk2]; cbn = ii->ii_startoff + off / ndisk2; ci2 = &cs->sc_cinfo[ccdisk + ndisk2]; } else if (cs->sc_cflags & CCDF_PARITY) { /* * XXX not implemented yet */ int ndisk2 = ii->ii_ndisk - 1; ccdisk = ii->ii_index[off % ndisk2]; cbn = ii->ii_startoff + off / ndisk2; if (cbn % ii->ii_ndisk <= ccdisk) ccdisk++; } else { ccdisk = ii->ii_index[off % ii->ii_ndisk]; cbn = ii->ii_startoff + off / ii->ii_ndisk; } } ci = &cs->sc_cinfo[ccdisk]; /* * Convert cbn from a superblock to a normal block so it * can be used to calculate (along with cboff) the normal * block index into this particular disk. */ cbn *= cs->sc_ileave; } /* * Fill in the component buf structure. */ cbp = getccdbuf(NULL); cbp->cb_buf.b_flags = bp->b_flags | B_CALL; cbp->cb_buf.b_iodone = (void (*)(struct buf *))ccdiodone; cbp->cb_buf.b_dev = ci->ci_dev; /* XXX */ cbp->cb_buf.b_blkno = cbn + cboff + CCD_OFFSET; cbp->cb_buf.b_offset = dbtob(cbn + cboff + CCD_OFFSET); cbp->cb_buf.b_data = addr; cbp->cb_buf.b_vp = ci->ci_vp; if (cs->sc_ileave == 0) cbc = dbtob((off_t)(ci->ci_size - cbn)); else cbc = dbtob((off_t)(cs->sc_ileave - cboff)); cbp->cb_buf.b_bcount = (cbc < bcount) ? cbc : bcount; cbp->cb_buf.b_bufsize = cbp->cb_buf.b_bcount; /* * context for ccdiodone */ cbp->cb_obp = bp; cbp->cb_unit = cs - ccd_softc; cbp->cb_comp = ci - cs->sc_cinfo; #ifdef DEBUG if (ccddebug & CCDB_IO) printf(" dev %x(u%d): cbp %x bn %d addr %x bcnt %d\n", ci->ci_dev, ci-cs->sc_cinfo, cbp, cbp->cb_buf.b_blkno, cbp->cb_buf.b_data, cbp->cb_buf.b_bcount); #endif cb[0] = cbp; /* * Note: both I/O's setup when reading from mirror, but only one * will be executed. */ if (cs->sc_cflags & CCDF_MIRROR) { /* mirror, setup second I/O */ cbp = getccdbuf(cb[0]); cbp->cb_buf.b_dev = ci2->ci_dev; cbp->cb_buf.b_vp = ci2->ci_vp; cbp->cb_comp = ci2 - cs->sc_cinfo; cb[1] = cbp; /* link together the ccdbuf's and clear "mirror done" flag */ cb[0]->cb_mirror = cb[1]; cb[1]->cb_mirror = cb[0]; cb[0]->cb_pflags &= ~CCDPF_MIRROR_DONE; cb[1]->cb_pflags &= ~CCDPF_MIRROR_DONE; } } static void ccdintr(cs, bp) struct ccd_softc *cs; struct buf *bp; { #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdintr(%x, %x)\n", cs, bp); #endif /* * Request is done for better or worse, wakeup the top half. */ if (bp->b_flags & B_ERROR) bp->b_resid = bp->b_bcount; devstat_end_transaction_buf(&cs->device_stats, bp); biodone(bp); } /* * Called at interrupt time. * Mark the component as done and if all components are done, * take a ccd interrupt. */ static void ccdiodone(cbp) struct ccdbuf *cbp; { struct buf *bp = cbp->cb_obp; int unit = cbp->cb_unit; int count, s; s = splbio(); #ifdef DEBUG if (ccddebug & CCDB_FOLLOW) printf("ccdiodone(%x)\n", cbp); if (ccddebug & CCDB_IO) { printf("ccdiodone: bp %x bcount %d resid %d\n", bp, bp->b_bcount, bp->b_resid); printf(" dev %x(u%d), cbp %x bn %d addr %x bcnt %d\n", cbp->cb_buf.b_dev, cbp->cb_comp, cbp, cbp->cb_buf.b_blkno, cbp->cb_buf.b_data, cbp->cb_buf.b_bcount); } #endif /* * If an error occured, report it. If this is a mirrored * configuration and the first of two possible reads, do not * set the error in the bp yet because the second read may * succeed. */ if (cbp->cb_buf.b_flags & B_ERROR) { const char *msg = ""; if ((ccd_softc[unit].sc_cflags & CCDF_MIRROR) && (cbp->cb_buf.b_flags & B_READ) && (cbp->cb_pflags & CCDPF_MIRROR_DONE) == 0) { /* * We will try our read on the other disk down * below, also reverse the default pick so if we * are doing a scan we do not keep hitting the * bad disk first. */ struct ccd_softc *cs = &ccd_softc[unit]; msg = ", trying other disk"; cs->sc_pick = 1 - cs->sc_pick; cs->sc_blk[cs->sc_pick] = bp->b_blkno; } else { bp->b_flags |= B_ERROR; bp->b_error = cbp->cb_buf.b_error ? cbp->cb_buf.b_error : EIO; } printf("ccd%d: error %d on component %d block %d (ccd block %d)%s\n", unit, bp->b_error, cbp->cb_comp, (int)cbp->cb_buf.b_blkno, bp->b_blkno, msg); } /* * Process mirror. If we are writing, I/O has been initiated on both * buffers and we fall through only after both are finished. * * If we are reading only one I/O is initiated at a time. If an * error occurs we initiate the second I/O and return, otherwise * we free the second I/O without initiating it. */ if (ccd_softc[unit].sc_cflags & CCDF_MIRROR) { if ((cbp->cb_buf.b_flags & B_READ) == 0) { /* * When writing, handshake with the second buffer * to determine when both are done. If both are not * done, return here. */ if ((cbp->cb_pflags & CCDPF_MIRROR_DONE) == 0) { cbp->cb_mirror->cb_pflags |= CCDPF_MIRROR_DONE; putccdbuf(cbp); splx(s); return; } } else { /* * When reading, either dispose of the second buffer * or initiate I/O on the second buffer if an error * occured with this one. */ if ((cbp->cb_pflags & CCDPF_MIRROR_DONE) == 0) { if (cbp->cb_buf.b_flags & B_ERROR) { cbp->cb_mirror->cb_pflags |= CCDPF_MIRROR_DONE; VOP_STRATEGY( cbp->cb_mirror->cb_buf.b_vp, &cbp->cb_mirror->cb_buf ); putccdbuf(cbp); splx(s); return; } else { putccdbuf(cbp->cb_mirror); /* fall through */ } } } } /* * use b_bufsize to determine how big the original request was rather * then b_bcount, because b_bcount may have been truncated for EOF. * * XXX We check for an error, but we do not test the resid for an * aligned EOF condition. This may result in character & block * device access not recognizing EOF properly when read or written * sequentially, but will not effect filesystems. */ count = cbp->cb_buf.b_bufsize; putccdbuf(cbp); /* * If all done, "interrupt". */ bp->b_resid -= count; if (bp->b_resid < 0) panic("ccdiodone: count"); if (bp->b_resid == 0) ccdintr(&ccd_softc[unit], bp); splx(s); } static int ccdioctl(dev, cmd, data, flag, p) dev_t dev; u_long cmd; caddr_t data; int flag; struct proc *p; { int unit = ccdunit(dev); int i, j, lookedup = 0, error = 0; int part, pmask, s; struct ccd_softc *cs; struct ccd_ioctl *ccio = (struct ccd_ioctl *)data; struct ccddevice ccd; char **cpp; struct vnode **vpp; if (unit >= numccd) return (ENXIO); cs = &ccd_softc[unit]; bzero(&ccd, sizeof(ccd)); switch (cmd) { case CCDIOCSET: if (cs->sc_flags & CCDF_INITED) return (EBUSY); if ((flag & FWRITE) == 0) return (EBADF); if ((error = ccdlock(cs)) != 0) return (error); /* Fill in some important bits. */ ccd.ccd_unit = unit; ccd.ccd_interleave = ccio->ccio_ileave; if (ccd.ccd_interleave == 0 && ((ccio->ccio_flags & CCDF_MIRROR) || (ccio->ccio_flags & CCDF_PARITY))) { printf("ccd%d: disabling mirror/parity, interleave is 0\n", unit); ccio->ccio_flags &= ~(CCDF_MIRROR | CCDF_PARITY); } if ((ccio->ccio_flags & CCDF_MIRROR) && (ccio->ccio_flags & CCDF_PARITY)) { printf("ccd%d: can't specify both mirror and parity, using mirror\n", unit); ccio->ccio_flags &= ~CCDF_PARITY; } if ((ccio->ccio_flags & (CCDF_MIRROR | CCDF_PARITY)) && !(ccio->ccio_flags & CCDF_UNIFORM)) { printf("ccd%d: mirror/parity forces uniform flag\n", unit); ccio->ccio_flags |= CCDF_UNIFORM; } ccd.ccd_flags = ccio->ccio_flags & CCDF_USERMASK; /* * Allocate space for and copy in the array of * componet pathnames and device numbers. */ cpp = malloc(ccio->ccio_ndisks * sizeof(char *), M_DEVBUF, M_WAITOK); vpp = malloc(ccio->ccio_ndisks * sizeof(struct vnode *), M_DEVBUF, M_WAITOK); error = copyin((caddr_t)ccio->ccio_disks, (caddr_t)cpp, ccio->ccio_ndisks * sizeof(char **)); if (error) { free(vpp, M_DEVBUF); free(cpp, M_DEVBUF); ccdunlock(cs); return (error); } #ifdef DEBUG if (ccddebug & CCDB_INIT) for (i = 0; i < ccio->ccio_ndisks; ++i) printf("ccdioctl: component %d: 0x%x\n", i, cpp[i]); #endif for (i = 0; i < ccio->ccio_ndisks; ++i) { #ifdef DEBUG if (ccddebug & CCDB_INIT) printf("ccdioctl: lookedup = %d\n", lookedup); #endif if ((error = ccdlookup(cpp[i], p, &vpp[i])) != 0) { for (j = 0; j < lookedup; ++j) (void)vn_close(vpp[j], FREAD|FWRITE, p->p_ucred, p); free(vpp, M_DEVBUF); free(cpp, M_DEVBUF); ccdunlock(cs); return (error); } ++lookedup; } ccd.ccd_cpp = cpp; ccd.ccd_vpp = vpp; ccd.ccd_ndev = ccio->ccio_ndisks; /* * Initialize the ccd. Fills in the softc for us. */ if ((error = ccdinit(&ccd, cpp, p)) != 0) { for (j = 0; j < lookedup; ++j) (void)vn_close(vpp[j], FREAD|FWRITE, p->p_ucred, p); bzero(&ccd_softc[unit], sizeof(struct ccd_softc)); free(vpp, M_DEVBUF); free(cpp, M_DEVBUF); ccdunlock(cs); return (error); } /* * The ccd has been successfully initialized, so * we can place it into the array and read the disklabel. */ bcopy(&ccd, &ccddevs[unit], sizeof(ccd)); ccio->ccio_unit = unit; ccio->ccio_size = cs->sc_size; ccdgetdisklabel(dev); ccdunlock(cs); break; case CCDIOCCLR: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); if ((flag & FWRITE) == 0) return (EBADF); if ((error = ccdlock(cs)) != 0) return (error); /* * Don't unconfigure if any other partitions are open * or if both the character and block flavors of this * partition are open. */ part = ccdpart(dev); pmask = (1 << part); if ((cs->sc_openmask & ~pmask) || ((cs->sc_bopenmask & pmask) && (cs->sc_copenmask & pmask))) { ccdunlock(cs); return (EBUSY); } /* * Free ccd_softc information and clear entry. */ /* Close the components and free their pathnames. */ for (i = 0; i < cs->sc_nccdisks; ++i) { /* * XXX: this close could potentially fail and * cause Bad Things. Maybe we need to force * the close to happen? */ #ifdef DEBUG if (ccddebug & CCDB_VNODE) vprint("CCDIOCCLR: vnode info", cs->sc_cinfo[i].ci_vp); #endif (void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE, p->p_ucred, p); free(cs->sc_cinfo[i].ci_path, M_DEVBUF); } /* Free interleave index. */ for (i = 0; cs->sc_itable[i].ii_ndisk; ++i) free(cs->sc_itable[i].ii_index, M_DEVBUF); /* Free component info and interleave table. */ free(cs->sc_cinfo, M_DEVBUF); free(cs->sc_itable, M_DEVBUF); cs->sc_flags &= ~CCDF_INITED; /* * Free ccddevice information and clear entry. */ free(ccddevs[unit].ccd_cpp, M_DEVBUF); free(ccddevs[unit].ccd_vpp, M_DEVBUF); ccd.ccd_dk = -1; bcopy(&ccd, &ccddevs[unit], sizeof(ccd)); /* * And remove the devstat entry. */ devstat_remove_entry(&cs->device_stats); /* This must be atomic. */ s = splhigh(); ccdunlock(cs); bzero(cs, sizeof(struct ccd_softc)); splx(s); break; case DIOCGDINFO: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); *(struct disklabel *)data = cs->sc_label; break; case DIOCGPART: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); ((struct partinfo *)data)->disklab = &cs->sc_label; ((struct partinfo *)data)->part = &cs->sc_label.d_partitions[ccdpart(dev)]; break; case DIOCWDINFO: case DIOCSDINFO: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); if ((flag & FWRITE) == 0) return (EBADF); if ((error = ccdlock(cs)) != 0) return (error); cs->sc_flags |= CCDF_LABELLING; error = setdisklabel(&cs->sc_label, (struct disklabel *)data, 0); if (error == 0) { if (cmd == DIOCWDINFO) error = writedisklabel(CCDLABELDEV(dev), &cs->sc_label); } cs->sc_flags &= ~CCDF_LABELLING; ccdunlock(cs); if (error) return (error); break; case DIOCWLABEL: if ((cs->sc_flags & CCDF_INITED) == 0) return (ENXIO); if ((flag & FWRITE) == 0) return (EBADF); if (*(int *)data != 0) cs->sc_flags |= CCDF_WLABEL; else cs->sc_flags &= ~CCDF_WLABEL; break; default: return (ENOTTY); } return (0); } static int ccdsize(dev) dev_t dev; { struct ccd_softc *cs; int part, size; if (ccdopen(dev, 0, S_IFBLK, curproc)) return (-1); cs = &ccd_softc[ccdunit(dev)]; part = ccdpart(dev); if ((cs->sc_flags & CCDF_INITED) == 0) return (-1); if (cs->sc_label.d_partitions[part].p_fstype != FS_SWAP) size = -1; else size = cs->sc_label.d_partitions[part].p_size; if (ccdclose(dev, 0, S_IFBLK, curproc)) return (-1); return (size); } static int ccddump(dev) dev_t dev; { /* Not implemented. */ return ENXIO; } /* * Lookup the provided name in the filesystem. If the file exists, * is a valid block device, and isn't being used by anyone else, * set *vpp to the file's vnode. */ static int ccdlookup(path, p, vpp) char *path; struct proc *p; struct vnode **vpp; /* result */ { struct nameidata nd; struct vnode *vp; struct vattr va; int error; NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p); if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) { #ifdef DEBUG if (ccddebug & CCDB_FOLLOW|CCDB_INIT) printf("ccdlookup: vn_open error = %d\n", error); #endif return (error); } vp = nd.ni_vp; if (vp->v_usecount > 1) { VOP_UNLOCK(vp, 0, p); (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p); return (EBUSY); } if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) { #ifdef DEBUG if (ccddebug & CCDB_FOLLOW|CCDB_INIT) printf("ccdlookup: getattr error = %d\n", error); #endif VOP_UNLOCK(vp, 0, p); (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p); return (error); } /* XXX: eventually we should handle VREG, too. */ if (va.va_type != VBLK) { VOP_UNLOCK(vp, 0, p); (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p); return (ENOTBLK); } #ifdef DEBUG if (ccddebug & CCDB_VNODE) vprint("ccdlookup: vnode info", vp); #endif VOP_UNLOCK(vp, 0, p); *vpp = vp; return (0); } /* * Read the disklabel from the ccd. If one is not present, fake one * up. */ static void ccdgetdisklabel(dev) dev_t dev; { int unit = ccdunit(dev); struct ccd_softc *cs = &ccd_softc[unit]; char *errstring; struct disklabel *lp = &cs->sc_label; struct ccdgeom *ccg = &cs->sc_geom; bzero(lp, sizeof(*lp)); lp->d_secperunit = cs->sc_size; lp->d_secsize = ccg->ccg_secsize; lp->d_nsectors = ccg->ccg_nsectors; lp->d_ntracks = ccg->ccg_ntracks; lp->d_ncylinders = ccg->ccg_ncylinders; lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename)); lp->d_type = DTYPE_CCD; strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname)); lp->d_rpm = 3600; lp->d_interleave = 1; lp->d_flags = 0; lp->d_partitions[RAW_PART].p_offset = 0; lp->d_partitions[RAW_PART].p_size = cs->sc_size; lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; lp->d_npartitions = RAW_PART + 1; lp->d_bbsize = BBSIZE; /* XXX */ lp->d_sbsize = SBSIZE; /* XXX */ lp->d_magic = DISKMAGIC; lp->d_magic2 = DISKMAGIC; lp->d_checksum = dkcksum(&cs->sc_label); /* * Call the generic disklabel extraction routine. */ errstring = readdisklabel(CCDLABELDEV(dev), &cs->sc_label); if (errstring != NULL) ccdmakedisklabel(cs); #ifdef DEBUG /* It's actually extremely common to have unlabeled ccds. */ if (ccddebug & CCDB_LABEL) if (errstring != NULL) printf("ccd%d: %s\n", unit, errstring); #endif } /* * Take care of things one might want to take care of in the event * that a disklabel isn't present. */ static void ccdmakedisklabel(cs) struct ccd_softc *cs; { struct disklabel *lp = &cs->sc_label; /* * For historical reasons, if there's no disklabel present * the raw partition must be marked FS_BSDFFS. */ lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS; strncpy(lp->d_packname, "default label", sizeof(lp->d_packname)); } /* * Wait interruptibly for an exclusive lock. * * XXX * Several drivers do this; it should be abstracted and made MP-safe. */ static int ccdlock(cs) struct ccd_softc *cs; { int error; while ((cs->sc_flags & CCDF_LOCKED) != 0) { cs->sc_flags |= CCDF_WANTED; if ((error = tsleep(cs, PRIBIO | PCATCH, "ccdlck", 0)) != 0) return (error); } cs->sc_flags |= CCDF_LOCKED; return (0); } /* * Unlock and wake up any waiters. */ static void ccdunlock(cs) struct ccd_softc *cs; { cs->sc_flags &= ~CCDF_LOCKED; if ((cs->sc_flags & CCDF_WANTED) != 0) { cs->sc_flags &= ~CCDF_WANTED; wakeup(cs); } } #ifdef DEBUG static void printiinfo(ii) struct ccdiinfo *ii; { int ix, i; for (ix = 0; ii->ii_ndisk; ix++, ii++) { printf(" itab[%d]: #dk %d sblk %d soff %d", ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff); for (i = 0; i < ii->ii_ndisk; i++) printf(" %d", ii->ii_index[i]); printf("\n"); } } #endif #endif /* NCCD > 0 */ /* Local Variables: */ /* c-argdecl-indent: 8 */ /* c-continued-statement-offset: 8 */ /* c-indent-level: 8 */ /* End: */ diff --git a/sys/kern/kern_conf.c b/sys/kern/kern_conf.c index ed3a0b5960b1..a5229ec7b498 100644 --- a/sys/kern/kern_conf.c +++ b/sys/kern/kern_conf.c @@ -1,391 +1,374 @@ /*- * Parts Copyright (c) 1995 Terrence R. Lambert * Copyright (c) 1995 Julian R. Elischer * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Terrence R. Lambert. * 4. The name Terrence R. Lambert may not be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY Julian R. Elischer ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #define cdevsw_ALLOCSTART (NUMCDEVSW/2) struct cdevsw *cdevsw[NUMCDEVSW]; static int bmaj2cmaj[NUMCDEVSW]; MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage"); /* * This is the number of hash-buckets. Experiements with 'real-life' * udev_t's show that a prime halfway between two powers of two works * best. */ #define DEVT_HASH 83 /* The number of dev_t's we can create before malloc(9) kick in. */ #define DEVT_STASH 50 static struct specinfo devt_stash[DEVT_STASH]; static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH]; static LIST_HEAD(, specinfo) dev_free; devfs_create_t *devfs_create_hook; devfs_remove_t *devfs_remove_hook; static int free_devt; SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, ""); /* * Routine to convert from character to block device number. * * A minimal stub routine can always return NODEV. */ dev_t chrtoblk(dev_t dev) { struct cdevsw *cd; if((cd = devsw(dev)) != NULL) { if (cd->d_bmaj != -1) return(makebdev(cd->d_bmaj,minor(dev))); } return(NODEV); } struct cdevsw * devsw(dev_t dev) { if (dev->si_devsw) return (dev->si_devsw); return(cdevsw[major(dev)]); } /* * Add a cdevsw entry */ int cdevsw_add(struct cdevsw *newentry) { int i; static int setup; if (!setup) { for (i = 0; i < NUMCDEVSW; i++) if (!bmaj2cmaj[i]) bmaj2cmaj[i] = 254; setup++; } if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) { printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", newentry->d_name, newentry->d_maj); return (EINVAL); } if (newentry->d_bmaj >= NUMCDEVSW) { printf("%s: ERROR: driver has bogus cdevsw->d_bmaj = %d\n", newentry->d_name, newentry->d_bmaj); return (EINVAL); } if (newentry->d_bmaj >= 0 && (newentry->d_flags & D_DISK) == 0) { printf("ERROR: \"%s\" bmaj but is not a disk\n", newentry->d_name); return (EINVAL); } if (cdevsw[newentry->d_maj]) { printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n", newentry->d_name, cdevsw[newentry->d_maj]->d_name); } cdevsw[newentry->d_maj] = newentry; if (newentry->d_bmaj < 0) return (0); if (bmaj2cmaj[newentry->d_bmaj] != 254) { printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n", newentry->d_name, cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name); } bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj; return (0); } /* * Remove a cdevsw entry */ int cdevsw_remove(struct cdevsw *oldentry) { if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) { printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", oldentry->d_name, oldentry->d_maj); return EINVAL; } cdevsw[oldentry->d_maj] = NULL; if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW) bmaj2cmaj[oldentry->d_bmaj] = 254; return 0; } int devsw_module_handler(module_t mod, int what, void* arg) { struct devsw_module_data* data = (struct devsw_module_data*) arg; int error = 0; - switch (what) { - case MOD_LOAD: - error = cdevsw_add(data->cdevsw); - if (!error && data->chainevh) - error = data->chainevh(mod, what, data->chainarg); - return error; - - case MOD_UNLOAD: - if (data->chainevh) { - error = data->chainevh(mod, what, data->chainarg); - if (error) - return error; - } - cdevsw_remove(data->cdevsw); - return error; - } - if (data->chainevh) return data->chainevh(mod, what, data->chainarg); else return 0; } /* * dev_t and u_dev_t primitives */ int major(dev_t x) { if (x == NODEV) return NOUDEV; return((x->si_udev >> 8) & 0xff); } int minor(dev_t x) { if (x == NODEV) return NOUDEV; return(x->si_udev & 0xffff00ff); } int lminor(dev_t x) { int i; if (x == NODEV) return NOUDEV; i = minor(x); return ((i & 0xff) | (i >> 8)); } dev_t makebdev(int x, int y) { return (makedev(bmaj2cmaj[x], y)); } dev_t makedev(int x, int y) { struct specinfo *si; udev_t udev; int hash; static int stashed; udev = (x << 8) | y; hash = udev % DEVT_HASH; LIST_FOREACH(si, &dev_hash[hash], si_hash) { if (si->si_udev == udev) return (si); } if (stashed >= DEVT_STASH) { MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT, M_USE_RESERVE); bzero(si, sizeof(*si)); } else if (LIST_FIRST(&dev_free)) { si = LIST_FIRST(&dev_free); LIST_REMOVE(si, si_hash); } else { si = devt_stash + stashed++; si->si_flags |= SI_STASHED; } si->si_udev = udev; LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); return (si); } void freedev(dev_t dev) { int hash; if (!free_devt) return; if (SLIST_FIRST(&dev->si_hlist)) return; if (dev->si_devsw || dev->si_drv1 || dev->si_drv2) return; hash = dev->si_udev % DEVT_HASH; LIST_REMOVE(dev, si_hash); if (dev->si_flags & SI_STASHED) { bzero(dev, sizeof(*dev)); LIST_INSERT_HEAD(&dev_free, dev, si_hash); } else { FREE(dev, M_DEVT); } } udev_t dev2udev(dev_t x) { if (x == NODEV) return NOUDEV; return (x->si_udev); } udev_t dev2budev(dev_t x) { if (x == NODEV) return NOUDEV; else return makeudev(devsw(x)->d_bmaj, minor(x)); } dev_t udev2dev(udev_t x, int b) { switch (b) { case 0: return makedev(umajor(x), uminor(x)); case 1: return makebdev(umajor(x), uminor(x)); default: Debugger("udev2dev(...,X)"); return NODEV; } } int uminor(udev_t dev) { return(dev & 0xffff00ff); } int umajor(udev_t dev) { return((dev & 0xff00) >> 8); } udev_t makeudev(int x, int y) { return ((x << 8) | y); } dev_t make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...) { dev_t dev; va_list ap; int i; dev = makedev(devsw->d_maj, minor); va_start(ap, fmt); i = kvprintf(fmt, NULL, dev->si_name, 32, ap); dev->si_name[i] = '\0'; va_end(ap); dev->si_devsw = devsw; if (devfs_create_hook) devfs_create_hook(dev, uid, gid, perms); return (dev); } void remove_dev(dev_t dev) { if (devfs_remove_hook) devfs_remove_hook(dev); dev->si_drv1 = 0; dev->si_drv2 = 0; dev->si_devsw = 0; freedev(dev); } const char * devtoname(dev_t dev) { char *p; int mynor; if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { p = dev->si_name; if (devsw(dev)) sprintf(p, "#%s/", devsw(dev)->d_name); else sprintf(p, "#%d/", major(dev)); p += strlen(p); mynor = minor(dev); if (mynor < 0 || mynor > 255) sprintf(p, "%#x", (u_int)mynor); else sprintf(p, "%d", mynor); } return (dev->si_name); } diff --git a/sys/sys/conf.h b/sys/sys/conf.h index 7c1485e2b6b0..475f97b5a916 100644 --- a/sys/sys/conf.h +++ b/sys/sys/conf.h @@ -1,297 +1,296 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)conf.h 8.5 (Berkeley) 1/9/95 * $FreeBSD$ */ #ifndef _SYS_CONF_H_ #define _SYS_CONF_H_ #include #define SPECNAMELEN 15 struct tty; struct disk; struct vnode; struct specinfo { u_int si_flags; #define SI_STASHED 0x0001 /* created in stashed storage */ udev_t si_udev; LIST_ENTRY(specinfo) si_hash; SLIST_HEAD(, vnode) si_hlist; char si_name[SPECNAMELEN + 1]; void *si_drv1, *si_drv2; struct cdevsw *si_devsw; void *si_devfs; /* save cookie for devfs operations */ void *si_bdevfs; /* XXX block device (should go away) */ int si_iosize_max; /* maximum I/O size (for physio &al) */ union { struct { struct tty *__sit_tty; } __si_tty; struct { struct disk *__sid_disk; struct mount *__sid_mountpoint; int __sid_bsize_phys; /* min physical block size */ int __sid_bsize_best; /* optimal block size */ } __si_disk; } __si_u; }; #define si_tty __si_u.__si_tty.__sit_tty #define si_disk __si_u.__si_disk.__sid_disk #define si_mountpoint __si_u.__si_disk.__sid_mountpoint #define si_bsize_phys __si_u.__si_disk.__sid_bsize_phys #define si_bsize_best __si_u.__si_disk.__sid_bsize_best /* * Exported shorthand */ #define v_hashchain v_rdev->si_hlist #define v_specmountpoint v_rdev->si_mountpoint /* * Special device management */ #define SPECHSZ 64 #define SPECHASH(rdev) (((unsigned)(minor(rdev)))%SPECHSZ) /* * Definitions of device driver entry switches */ struct buf; struct proc; struct uio; typedef int d_open_t __P((dev_t dev, int oflags, int devtype, struct proc *p)); typedef int d_close_t __P((dev_t dev, int fflag, int devtype, struct proc *p)); typedef void d_strategy_t __P((struct buf *bp)); typedef int d_parms_t __P((dev_t dev, struct specinfo *sinfo, int ctl)); typedef int d_ioctl_t __P((dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)); typedef int d_dump_t __P((dev_t dev)); typedef int d_psize_t __P((dev_t dev)); typedef int d_read_t __P((dev_t dev, struct uio *uio, int ioflag)); typedef int d_write_t __P((dev_t dev, struct uio *uio, int ioflag)); typedef int d_poll_t __P((dev_t dev, int events, struct proc *p)); typedef int d_mmap_t __P((dev_t dev, vm_offset_t offset, int nprot)); typedef int l_open_t __P((dev_t dev, struct tty *tp)); typedef int l_close_t __P((struct tty *tp, int flag)); typedef int l_read_t __P((struct tty *tp, struct uio *uio, int flag)); typedef int l_write_t __P((struct tty *tp, struct uio *uio, int flag)); typedef int l_ioctl_t __P((struct tty *tp, u_long cmd, caddr_t data, int flag, struct proc *p)); typedef int l_rint_t __P((int c, struct tty *tp)); typedef int l_start_t __P((struct tty *tp)); typedef int l_modem_t __P((struct tty *tp, int flag)); /* This is type of the function DEVFS uses to hook into the kernel with */ typedef void devfs_create_t __P((dev_t dev, uid_t uid, gid_t gid, int perms)); typedef void devfs_remove_t __P((dev_t dev)); /* * XXX: The dummy argument can be used to do what strategy1() never * did anywhere: Create a per device flag to lock the device during * label/slice surgery, all calls with a dummy == 0 gets stalled on * a queue somewhere, whereas dummy == 1 are let through. Once out * of surgery, reset the flag and restart all the stuff on the stall * queue. */ #define BUF_STRATEGY(bp, dummy) (*devsw((bp)->b_dev)->d_strategy)(bp) /* * Types for d_flags. */ #define D_TAPE 0x0001 #define D_DISK 0x0002 #define D_TTY 0x0004 #define D_MEM 0x0008 #define D_TYPEMASK 0xffff /* * Flags for d_flags. */ #define D_NAGGED 0x20000 /* nagged about missing make_dev() */ #define D_CANFREE 0x40000 /* can free blocks */ #define D_TRACKCLOSE 0x80000 /* track all closes */ /* * Character device switch table */ struct cdevsw { d_open_t *d_open; d_close_t *d_close; d_read_t *d_read; d_write_t *d_write; d_ioctl_t *d_ioctl; d_poll_t *d_poll; d_mmap_t *d_mmap; d_strategy_t *d_strategy; const char *d_name; /* base device name, e.g. 'vn' */ int d_maj; d_dump_t *d_dump; d_psize_t *d_psize; u_int d_flags; int d_bmaj; }; /* * Line discipline switch table */ struct linesw { l_open_t *l_open; l_close_t *l_close; l_read_t *l_read; l_write_t *l_write; l_ioctl_t *l_ioctl; l_rint_t *l_rint; l_start_t *l_start; l_modem_t *l_modem; u_char l_hotchar; }; #ifdef KERNEL extern struct linesw linesw[]; extern int nlinesw; int ldisc_register __P((int , struct linesw *)); void ldisc_deregister __P((int)); #define LDISC_LOAD -1 /* Loadable line discipline */ #endif /* * Swap device table */ struct swdevt { udev_t sw_dev; /* For quasibogus swapdev reporting */ int sw_flags; int sw_nblks; struct vnode *sw_vp; dev_t sw_device; }; #define SW_FREED 0x01 #define SW_SEQUENTIAL 0x02 #define sw_freed sw_flags /* XXX compat */ #ifdef KERNEL d_open_t noopen; d_close_t noclose; d_read_t noread; d_write_t nowrite; d_ioctl_t noioctl; d_mmap_t nommap; #define nostrategy ((d_strategy_t *)NULL) #define nopoll seltrue d_dump_t nodump; #define NUMCDEVSW 256 /* * nopsize is little used, so not worth having dummy functions for. */ #define nopsize ((d_psize_t *)NULL) d_open_t nullopen; d_close_t nullclose; l_read_t l_noread; l_write_t l_nowrite; struct module; struct devsw_module_data { int (*chainevh)(struct module *, int, void *); /* next handler */ void *chainarg; /* arg for next event handler */ - struct cdevsw *cdevsw; /* device functions */ /* Do not initialize fields hereafter */ }; -#define DEV_MODULE(name, cmaj, bmaj, devsw, evh, arg) \ +#define DEV_MODULE(name, evh, arg) \ static struct devsw_module_data name##_devsw_mod = { \ - evh, arg, &devsw \ + evh, arg, \ }; \ \ static moduledata_t name##_mod = { \ #name, \ devsw_module_handler, \ &name##_devsw_mod \ }; \ -DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+cmaj*256+bmaj) +DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE) int cdevsw_add __P((struct cdevsw *new)); int cdevsw_remove __P((struct cdevsw *old)); dev_t chrtoblk __P((dev_t dev)); struct cdevsw *devsw __P((dev_t dev)); int devsw_module_handler __P((struct module *mod, int what, void *arg)); const char *devtoname __P((dev_t dev)); void freedev __P((dev_t dev)); int iskmemdev __P((dev_t dev)); int iszerodev __P((dev_t dev)); dev_t makebdev __P((int maj, int min)); dev_t make_dev __P((struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...)) __printflike(6, 7); int lminor __P((dev_t dev)); void remove_dev __P((dev_t dev)); void setconf __P((void)); extern devfs_create_t *devfs_create_hook; /* * XXX: This included for when DEVFS resurfaces */ #define UID_ROOT 0 #define UID_BIN 3 #define UID_UUCP 66 #define GID_WHEEL 0 #define GID_KMEM 2 #define GID_OPERATOR 5 #define GID_BIN 7 #define GID_GAMES 13 #define GID_DIALER 68 #endif /* KERNEL */ #endif /* !_SYS_CONF_H_ */ diff --git a/sys/sys/linedisc.h b/sys/sys/linedisc.h index 7c1485e2b6b0..475f97b5a916 100644 --- a/sys/sys/linedisc.h +++ b/sys/sys/linedisc.h @@ -1,297 +1,296 @@ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)conf.h 8.5 (Berkeley) 1/9/95 * $FreeBSD$ */ #ifndef _SYS_CONF_H_ #define _SYS_CONF_H_ #include #define SPECNAMELEN 15 struct tty; struct disk; struct vnode; struct specinfo { u_int si_flags; #define SI_STASHED 0x0001 /* created in stashed storage */ udev_t si_udev; LIST_ENTRY(specinfo) si_hash; SLIST_HEAD(, vnode) si_hlist; char si_name[SPECNAMELEN + 1]; void *si_drv1, *si_drv2; struct cdevsw *si_devsw; void *si_devfs; /* save cookie for devfs operations */ void *si_bdevfs; /* XXX block device (should go away) */ int si_iosize_max; /* maximum I/O size (for physio &al) */ union { struct { struct tty *__sit_tty; } __si_tty; struct { struct disk *__sid_disk; struct mount *__sid_mountpoint; int __sid_bsize_phys; /* min physical block size */ int __sid_bsize_best; /* optimal block size */ } __si_disk; } __si_u; }; #define si_tty __si_u.__si_tty.__sit_tty #define si_disk __si_u.__si_disk.__sid_disk #define si_mountpoint __si_u.__si_disk.__sid_mountpoint #define si_bsize_phys __si_u.__si_disk.__sid_bsize_phys #define si_bsize_best __si_u.__si_disk.__sid_bsize_best /* * Exported shorthand */ #define v_hashchain v_rdev->si_hlist #define v_specmountpoint v_rdev->si_mountpoint /* * Special device management */ #define SPECHSZ 64 #define SPECHASH(rdev) (((unsigned)(minor(rdev)))%SPECHSZ) /* * Definitions of device driver entry switches */ struct buf; struct proc; struct uio; typedef int d_open_t __P((dev_t dev, int oflags, int devtype, struct proc *p)); typedef int d_close_t __P((dev_t dev, int fflag, int devtype, struct proc *p)); typedef void d_strategy_t __P((struct buf *bp)); typedef int d_parms_t __P((dev_t dev, struct specinfo *sinfo, int ctl)); typedef int d_ioctl_t __P((dev_t dev, u_long cmd, caddr_t data, int fflag, struct proc *p)); typedef int d_dump_t __P((dev_t dev)); typedef int d_psize_t __P((dev_t dev)); typedef int d_read_t __P((dev_t dev, struct uio *uio, int ioflag)); typedef int d_write_t __P((dev_t dev, struct uio *uio, int ioflag)); typedef int d_poll_t __P((dev_t dev, int events, struct proc *p)); typedef int d_mmap_t __P((dev_t dev, vm_offset_t offset, int nprot)); typedef int l_open_t __P((dev_t dev, struct tty *tp)); typedef int l_close_t __P((struct tty *tp, int flag)); typedef int l_read_t __P((struct tty *tp, struct uio *uio, int flag)); typedef int l_write_t __P((struct tty *tp, struct uio *uio, int flag)); typedef int l_ioctl_t __P((struct tty *tp, u_long cmd, caddr_t data, int flag, struct proc *p)); typedef int l_rint_t __P((int c, struct tty *tp)); typedef int l_start_t __P((struct tty *tp)); typedef int l_modem_t __P((struct tty *tp, int flag)); /* This is type of the function DEVFS uses to hook into the kernel with */ typedef void devfs_create_t __P((dev_t dev, uid_t uid, gid_t gid, int perms)); typedef void devfs_remove_t __P((dev_t dev)); /* * XXX: The dummy argument can be used to do what strategy1() never * did anywhere: Create a per device flag to lock the device during * label/slice surgery, all calls with a dummy == 0 gets stalled on * a queue somewhere, whereas dummy == 1 are let through. Once out * of surgery, reset the flag and restart all the stuff on the stall * queue. */ #define BUF_STRATEGY(bp, dummy) (*devsw((bp)->b_dev)->d_strategy)(bp) /* * Types for d_flags. */ #define D_TAPE 0x0001 #define D_DISK 0x0002 #define D_TTY 0x0004 #define D_MEM 0x0008 #define D_TYPEMASK 0xffff /* * Flags for d_flags. */ #define D_NAGGED 0x20000 /* nagged about missing make_dev() */ #define D_CANFREE 0x40000 /* can free blocks */ #define D_TRACKCLOSE 0x80000 /* track all closes */ /* * Character device switch table */ struct cdevsw { d_open_t *d_open; d_close_t *d_close; d_read_t *d_read; d_write_t *d_write; d_ioctl_t *d_ioctl; d_poll_t *d_poll; d_mmap_t *d_mmap; d_strategy_t *d_strategy; const char *d_name; /* base device name, e.g. 'vn' */ int d_maj; d_dump_t *d_dump; d_psize_t *d_psize; u_int d_flags; int d_bmaj; }; /* * Line discipline switch table */ struct linesw { l_open_t *l_open; l_close_t *l_close; l_read_t *l_read; l_write_t *l_write; l_ioctl_t *l_ioctl; l_rint_t *l_rint; l_start_t *l_start; l_modem_t *l_modem; u_char l_hotchar; }; #ifdef KERNEL extern struct linesw linesw[]; extern int nlinesw; int ldisc_register __P((int , struct linesw *)); void ldisc_deregister __P((int)); #define LDISC_LOAD -1 /* Loadable line discipline */ #endif /* * Swap device table */ struct swdevt { udev_t sw_dev; /* For quasibogus swapdev reporting */ int sw_flags; int sw_nblks; struct vnode *sw_vp; dev_t sw_device; }; #define SW_FREED 0x01 #define SW_SEQUENTIAL 0x02 #define sw_freed sw_flags /* XXX compat */ #ifdef KERNEL d_open_t noopen; d_close_t noclose; d_read_t noread; d_write_t nowrite; d_ioctl_t noioctl; d_mmap_t nommap; #define nostrategy ((d_strategy_t *)NULL) #define nopoll seltrue d_dump_t nodump; #define NUMCDEVSW 256 /* * nopsize is little used, so not worth having dummy functions for. */ #define nopsize ((d_psize_t *)NULL) d_open_t nullopen; d_close_t nullclose; l_read_t l_noread; l_write_t l_nowrite; struct module; struct devsw_module_data { int (*chainevh)(struct module *, int, void *); /* next handler */ void *chainarg; /* arg for next event handler */ - struct cdevsw *cdevsw; /* device functions */ /* Do not initialize fields hereafter */ }; -#define DEV_MODULE(name, cmaj, bmaj, devsw, evh, arg) \ +#define DEV_MODULE(name, evh, arg) \ static struct devsw_module_data name##_devsw_mod = { \ - evh, arg, &devsw \ + evh, arg, \ }; \ \ static moduledata_t name##_mod = { \ #name, \ devsw_module_handler, \ &name##_devsw_mod \ }; \ -DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+cmaj*256+bmaj) +DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE) int cdevsw_add __P((struct cdevsw *new)); int cdevsw_remove __P((struct cdevsw *old)); dev_t chrtoblk __P((dev_t dev)); struct cdevsw *devsw __P((dev_t dev)); int devsw_module_handler __P((struct module *mod, int what, void *arg)); const char *devtoname __P((dev_t dev)); void freedev __P((dev_t dev)); int iskmemdev __P((dev_t dev)); int iszerodev __P((dev_t dev)); dev_t makebdev __P((int maj, int min)); dev_t make_dev __P((struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...)) __printflike(6, 7); int lminor __P((dev_t dev)); void remove_dev __P((dev_t dev)); void setconf __P((void)); extern devfs_create_t *devfs_create_hook; /* * XXX: This included for when DEVFS resurfaces */ #define UID_ROOT 0 #define UID_BIN 3 #define UID_UUCP 66 #define GID_WHEEL 0 #define GID_KMEM 2 #define GID_OPERATOR 5 #define GID_BIN 7 #define GID_GAMES 13 #define GID_DIALER 68 #endif /* KERNEL */ #endif /* !_SYS_CONF_H_ */