Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.14
      1   1.1      cgd /*
      2   1.1      cgd  * Copyright (c) 1989 The Regents of the University of California.
      3   1.1      cgd  * All rights reserved.
      4   1.1      cgd  *
      5   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6   1.1      cgd  * modification, are permitted provided that the following conditions
      7   1.1      cgd  * are met:
      8   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14   1.1      cgd  *    must display the following acknowledgement:
     15   1.1      cgd  *	This product includes software developed by the University of
     16   1.1      cgd  *	California, Berkeley and its contributors.
     17   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18   1.1      cgd  *    may be used to endorse or promote products derived from this software
     19   1.1      cgd  *    without specific prior written permission.
     20   1.1      cgd  *
     21   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1      cgd  * SUCH DAMAGE.
     32   1.1      cgd  *
     33   1.3      cgd  *	from: @(#)spec_vnops.c	7.37 (Berkeley) 5/30/91
     34  1.14      cgd  *	$Id: spec_vnops.c,v 1.14 1994/05/24 02:36:33 cgd Exp $
     35   1.1      cgd  */
     36   1.1      cgd 
     37   1.9  mycroft #include <sys/param.h>
     38   1.9  mycroft #include <sys/proc.h>
     39   1.9  mycroft #include <sys/systm.h>
     40   1.9  mycroft #include <sys/kernel.h>
     41   1.9  mycroft #include <sys/conf.h>
     42   1.9  mycroft #include <sys/buf.h>
     43   1.9  mycroft #include <sys/mount.h>
     44   1.9  mycroft #include <sys/namei.h>
     45   1.9  mycroft #include <sys/vnode.h>
     46   1.9  mycroft #include <miscfs/specfs/specdev.h> /* XXX */
     47   1.9  mycroft #include <sys/stat.h>
     48   1.9  mycroft #include <sys/errno.h>
     49   1.9  mycroft #include <sys/ioctl.h>
     50   1.9  mycroft #include <sys/file.h>
     51   1.9  mycroft #include <sys/dkbad.h>	/* XXX */
     52   1.9  mycroft #include <sys/disklabel.h>
     53   1.1      cgd 
     54   1.1      cgd /* symbolic sleep message strings for devices */
     55   1.1      cgd char	devopn[] = "devopn";
     56   1.1      cgd char	devio[] = "devio";
     57   1.1      cgd char	devwait[] = "devwait";
     58   1.1      cgd char	devin[] = "devin";
     59   1.1      cgd char	devout[] = "devout";
     60   1.1      cgd char	devioc[] = "devioc";
     61   1.1      cgd char	devcls[] = "devcls";
     62   1.1      cgd 
     63   1.1      cgd struct vnodeops spec_vnodeops = {
     64   1.1      cgd 	spec_lookup,		/* lookup */
     65   1.1      cgd 	spec_create,		/* create */
     66   1.1      cgd 	spec_mknod,		/* mknod */
     67   1.1      cgd 	spec_open,		/* open */
     68   1.1      cgd 	spec_close,		/* close */
     69   1.1      cgd 	spec_access,		/* access */
     70   1.1      cgd 	spec_getattr,		/* getattr */
     71   1.1      cgd 	spec_setattr,		/* setattr */
     72   1.1      cgd 	spec_read,		/* read */
     73   1.1      cgd 	spec_write,		/* write */
     74   1.1      cgd 	spec_ioctl,		/* ioctl */
     75   1.1      cgd 	spec_select,		/* select */
     76   1.1      cgd 	spec_mmap,		/* mmap */
     77   1.1      cgd 	spec_fsync,		/* fsync */
     78   1.1      cgd 	spec_seek,		/* seek */
     79   1.1      cgd 	spec_remove,		/* remove */
     80   1.1      cgd 	spec_link,		/* link */
     81   1.1      cgd 	spec_rename,		/* rename */
     82   1.1      cgd 	spec_mkdir,		/* mkdir */
     83   1.1      cgd 	spec_rmdir,		/* rmdir */
     84   1.1      cgd 	spec_symlink,		/* symlink */
     85   1.1      cgd 	spec_readdir,		/* readdir */
     86   1.1      cgd 	spec_readlink,		/* readlink */
     87   1.1      cgd 	spec_abortop,		/* abortop */
     88   1.1      cgd 	spec_inactive,		/* inactive */
     89   1.1      cgd 	spec_reclaim,		/* reclaim */
     90   1.1      cgd 	spec_lock,		/* lock */
     91   1.1      cgd 	spec_unlock,		/* unlock */
     92   1.1      cgd 	spec_bmap,		/* bmap */
     93   1.1      cgd 	spec_strategy,		/* strategy */
     94   1.1      cgd 	spec_print,		/* print */
     95   1.1      cgd 	spec_islocked,		/* islocked */
     96   1.1      cgd 	spec_advlock,		/* advlock */
     97   1.1      cgd };
     98   1.1      cgd 
     99   1.1      cgd /*
    100   1.1      cgd  * Trivial lookup routine that always fails.
    101   1.1      cgd  */
    102   1.4   andrew int
    103   1.1      cgd spec_lookup(vp, ndp, p)
    104   1.1      cgd 	struct vnode *vp;
    105   1.1      cgd 	struct nameidata *ndp;
    106   1.1      cgd 	struct proc *p;
    107   1.1      cgd {
    108   1.1      cgd 
    109   1.1      cgd 	ndp->ni_dvp = vp;
    110   1.1      cgd 	ndp->ni_vp = NULL;
    111   1.1      cgd 	return (ENOTDIR);
    112   1.1      cgd }
    113   1.1      cgd 
    114   1.1      cgd /*
    115   1.1      cgd  * Open a special file: Don't allow open if fs is mounted -nodev,
    116   1.1      cgd  * and don't allow opens of block devices that are currently mounted.
    117   1.1      cgd  * Otherwise, call device driver open function.
    118   1.1      cgd  */
    119   1.1      cgd /* ARGSUSED */
    120   1.4   andrew int
    121   1.1      cgd spec_open(vp, mode, cred, p)
    122   1.1      cgd 	register struct vnode *vp;
    123   1.1      cgd 	int mode;
    124   1.1      cgd 	struct ucred *cred;
    125   1.1      cgd 	struct proc *p;
    126   1.1      cgd {
    127   1.1      cgd 	dev_t dev = (dev_t)vp->v_rdev;
    128   1.1      cgd 	register int maj = major(dev);
    129   1.1      cgd 	int error;
    130   1.1      cgd 
    131   1.1      cgd 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    132   1.1      cgd 		return (ENXIO);
    133   1.1      cgd 
    134   1.1      cgd 	switch (vp->v_type) {
    135   1.1      cgd 
    136   1.1      cgd 	case VCHR:
    137   1.1      cgd 		if ((u_int)maj >= nchrdev)
    138   1.1      cgd 			return (ENXIO);
    139   1.1      cgd 		VOP_UNLOCK(vp);
    140   1.1      cgd 		error = (*cdevsw[maj].d_open)(dev, mode, S_IFCHR, p);
    141   1.1      cgd 		VOP_LOCK(vp);
    142   1.1      cgd 		return (error);
    143   1.1      cgd 
    144   1.1      cgd 	case VBLK:
    145   1.1      cgd 		if ((u_int)maj >= nblkdev)
    146   1.1      cgd 			return (ENXIO);
    147   1.1      cgd 		if (error = mountedon(vp))
    148   1.1      cgd 			return (error);
    149   1.1      cgd 		return ((*bdevsw[maj].d_open)(dev, mode, S_IFBLK, p));
    150   1.4   andrew 
    151   1.4   andrew 	default:
    152   1.4   andrew 		return (0);
    153   1.1      cgd 	}
    154   1.1      cgd }
    155   1.1      cgd 
    156   1.1      cgd /*
    157   1.1      cgd  * Vnode op for read
    158   1.1      cgd  */
    159   1.1      cgd /* ARGSUSED */
    160   1.4   andrew int
    161   1.1      cgd spec_read(vp, uio, ioflag, cred)
    162   1.1      cgd 	register struct vnode *vp;
    163   1.1      cgd 	register struct uio *uio;
    164   1.1      cgd 	int ioflag;
    165   1.1      cgd 	struct ucred *cred;
    166   1.1      cgd {
    167   1.1      cgd 	struct proc *p = uio->uio_procp;
    168   1.1      cgd 	struct buf *bp;
    169   1.1      cgd 	daddr_t bn;
    170   1.1      cgd 	long bsize, bscale;
    171   1.1      cgd 	struct partinfo dpart;
    172   1.1      cgd 	register int n, on;
    173   1.1      cgd 	int error = 0;
    174   1.1      cgd 	extern int mem_no;
    175   1.1      cgd 
    176   1.1      cgd #ifdef DIAGNOSTIC
    177   1.1      cgd 	if (uio->uio_rw != UIO_READ)
    178   1.1      cgd 		panic("spec_read mode");
    179   1.1      cgd 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    180   1.1      cgd 		panic("spec_read proc");
    181   1.1      cgd #endif
    182   1.1      cgd 	if (uio->uio_resid == 0)
    183   1.1      cgd 		return (0);
    184   1.1      cgd 
    185   1.1      cgd 	switch (vp->v_type) {
    186   1.1      cgd 
    187   1.1      cgd 	case VCHR:
    188   1.1      cgd 		/*
    189   1.1      cgd 		 * Negative offsets allowed only for /dev/kmem
    190   1.1      cgd 		 */
    191   1.1      cgd 		if (uio->uio_offset < 0 && major(vp->v_rdev) != mem_no)
    192   1.1      cgd 			return (EINVAL);
    193   1.1      cgd 		VOP_UNLOCK(vp);
    194   1.1      cgd 		error = (*cdevsw[major(vp->v_rdev)].d_read)
    195   1.1      cgd 			(vp->v_rdev, uio, ioflag);
    196   1.1      cgd 		VOP_LOCK(vp);
    197   1.1      cgd 		return (error);
    198   1.1      cgd 
    199   1.1      cgd 	case VBLK:
    200   1.1      cgd 		if (uio->uio_offset < 0)
    201   1.1      cgd 			return (EINVAL);
    202   1.1      cgd 		bsize = BLKDEV_IOSIZE;
    203   1.1      cgd 		if ((*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, DIOCGPART,
    204   1.1      cgd 		    (caddr_t)&dpart, FREAD, p) == 0) {
    205   1.1      cgd 			if (dpart.part->p_fstype == FS_BSDFFS &&
    206   1.1      cgd 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    207   1.1      cgd 				bsize = dpart.part->p_frag *
    208   1.1      cgd 				    dpart.part->p_fsize;
    209   1.1      cgd 		}
    210   1.6      cgd 		if (bsize > MAXBSIZE)
    211   1.7    glass 			bsize = MAXBSIZE;
    212   1.1      cgd 		bscale = bsize / DEV_BSIZE;
    213   1.1      cgd 		do {
    214   1.1      cgd 			bn = (uio->uio_offset / DEV_BSIZE) &~ (bscale - 1);
    215   1.1      cgd 			on = uio->uio_offset % bsize;
    216  1.14      cgd 			n = min((unsigned)(bsize - on), uio->uio_resid);
    217   1.1      cgd 			if (vp->v_lastr + bscale == bn)
    218   1.1      cgd 				error = breada(vp, bn, (int)bsize, bn + bscale,
    219   1.1      cgd 					(int)bsize, NOCRED, &bp);
    220   1.1      cgd 			else
    221   1.1      cgd 				error = bread(vp, bn, (int)bsize, NOCRED, &bp);
    222   1.1      cgd 			vp->v_lastr = bn;
    223  1.14      cgd 			n = min(n, bsize - bp->b_resid);
    224   1.1      cgd 			if (error) {
    225   1.1      cgd 				brelse(bp);
    226   1.1      cgd 				return (error);
    227   1.1      cgd 			}
    228   1.1      cgd 			error = uiomove(bp->b_un.b_addr + on, n, uio);
    229   1.2      cgd #ifdef OMIT	/* 20 Aug 92*/
    230   1.1      cgd 			if (n + on == bsize)
    231   1.1      cgd 				bp->b_flags |= B_AGE;
    232   1.2      cgd #endif	/* OMIT*/
    233   1.1      cgd 			brelse(bp);
    234   1.1      cgd 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    235   1.1      cgd 		return (error);
    236   1.1      cgd 
    237   1.1      cgd 	default:
    238   1.1      cgd 		panic("spec_read type");
    239   1.1      cgd 	}
    240   1.1      cgd 	/* NOTREACHED */
    241   1.1      cgd }
    242   1.1      cgd 
    243   1.1      cgd /*
    244   1.1      cgd  * Vnode op for write
    245   1.1      cgd  */
    246   1.1      cgd /* ARGSUSED */
    247   1.4   andrew int
    248   1.1      cgd spec_write(vp, uio, ioflag, cred)
    249   1.1      cgd 	register struct vnode *vp;
    250   1.1      cgd 	register struct uio *uio;
    251   1.1      cgd 	int ioflag;
    252   1.1      cgd 	struct ucred *cred;
    253   1.1      cgd {
    254   1.1      cgd 	struct proc *p = uio->uio_procp;
    255   1.1      cgd 	struct buf *bp;
    256   1.1      cgd 	daddr_t bn;
    257   1.1      cgd 	int bsize, blkmask;
    258   1.1      cgd 	struct partinfo dpart;
    259   1.1      cgd 	register int n, on;
    260   1.1      cgd 	int error = 0;
    261   1.1      cgd 	extern int mem_no;
    262   1.1      cgd 
    263   1.1      cgd #ifdef DIAGNOSTIC
    264   1.1      cgd 	if (uio->uio_rw != UIO_WRITE)
    265   1.1      cgd 		panic("spec_write mode");
    266   1.1      cgd 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    267   1.1      cgd 		panic("spec_write proc");
    268   1.1      cgd #endif
    269   1.1      cgd 
    270   1.1      cgd 	switch (vp->v_type) {
    271   1.1      cgd 
    272   1.1      cgd 	case VCHR:
    273   1.1      cgd 		/*
    274   1.1      cgd 		 * Negative offsets allowed only for /dev/kmem
    275   1.1      cgd 		 */
    276   1.1      cgd 		if (uio->uio_offset < 0 && major(vp->v_rdev) != mem_no)
    277   1.1      cgd 			return (EINVAL);
    278   1.1      cgd 		VOP_UNLOCK(vp);
    279   1.1      cgd 		error = (*cdevsw[major(vp->v_rdev)].d_write)
    280   1.1      cgd 			(vp->v_rdev, uio, ioflag);
    281   1.1      cgd 		VOP_LOCK(vp);
    282   1.1      cgd 		return (error);
    283   1.1      cgd 
    284   1.1      cgd 	case VBLK:
    285   1.1      cgd 		if (uio->uio_resid == 0)
    286   1.1      cgd 			return (0);
    287   1.1      cgd 		if (uio->uio_offset < 0)
    288   1.1      cgd 			return (EINVAL);
    289   1.1      cgd 		bsize = BLKDEV_IOSIZE;
    290   1.1      cgd 		if ((*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, DIOCGPART,
    291   1.1      cgd 		    (caddr_t)&dpart, FREAD, p) == 0) {
    292   1.1      cgd 			if (dpart.part->p_fstype == FS_BSDFFS &&
    293   1.1      cgd 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    294   1.1      cgd 				bsize = dpart.part->p_frag *
    295   1.1      cgd 				    dpart.part->p_fsize;
    296   1.1      cgd 		}
    297   1.6      cgd 		if (bsize > MAXBSIZE)
    298   1.6      cgd 			bsize = MAXBSIZE;
    299   1.1      cgd 		blkmask = (bsize / DEV_BSIZE) - 1;
    300   1.1      cgd 		do {
    301   1.1      cgd 			bn = (uio->uio_offset / DEV_BSIZE) &~ blkmask;
    302   1.1      cgd 			on = uio->uio_offset % bsize;
    303  1.14      cgd 			n = min((unsigned)(bsize - on), uio->uio_resid);
    304   1.1      cgd 			if (n == bsize)
    305  1.13      cgd 				bp = getblk(vp, bn, bsize, 0, 0);
    306   1.1      cgd 			else
    307   1.1      cgd 				error = bread(vp, bn, bsize, NOCRED, &bp);
    308  1.14      cgd 			n = min(n, bsize - bp->b_resid);
    309   1.1      cgd 			if (error) {
    310   1.1      cgd 				brelse(bp);
    311   1.1      cgd 				return (error);
    312   1.1      cgd 			}
    313   1.1      cgd 			error = uiomove(bp->b_un.b_addr + on, n, uio);
    314   1.1      cgd 			if (n + on == bsize) {
    315   1.1      cgd 				bp->b_flags |= B_AGE;
    316   1.1      cgd 				bawrite(bp);
    317   1.1      cgd 			} else
    318   1.1      cgd 				bdwrite(bp);
    319   1.1      cgd 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    320   1.1      cgd 		return (error);
    321   1.1      cgd 
    322   1.1      cgd 	default:
    323   1.1      cgd 		panic("spec_write type");
    324   1.1      cgd 	}
    325   1.1      cgd 	/* NOTREACHED */
    326   1.1      cgd }
    327   1.1      cgd 
    328   1.1      cgd /*
    329   1.1      cgd  * Device ioctl operation.
    330   1.1      cgd  */
    331   1.1      cgd /* ARGSUSED */
    332   1.4   andrew int
    333   1.1      cgd spec_ioctl(vp, com, data, fflag, cred, p)
    334   1.1      cgd 	struct vnode *vp;
    335   1.1      cgd 	int com;
    336   1.1      cgd 	caddr_t data;
    337   1.1      cgd 	int fflag;
    338   1.1      cgd 	struct ucred *cred;
    339   1.1      cgd 	struct proc *p;
    340   1.1      cgd {
    341   1.1      cgd 	dev_t dev = vp->v_rdev;
    342   1.1      cgd 
    343   1.1      cgd 	switch (vp->v_type) {
    344   1.1      cgd 
    345   1.1      cgd 	case VCHR:
    346   1.1      cgd 		return ((*cdevsw[major(dev)].d_ioctl)(dev, com, data,
    347   1.1      cgd 		    fflag, p));
    348   1.1      cgd 
    349   1.1      cgd 	case VBLK:
    350   1.1      cgd 		if (com == 0 && (int)data == B_TAPE)
    351   1.1      cgd 			if (bdevsw[major(dev)].d_flags & B_TAPE)
    352   1.1      cgd 				return (0);
    353   1.1      cgd 			else
    354   1.1      cgd 				return (1);
    355   1.1      cgd 		return ((*bdevsw[major(dev)].d_ioctl)(dev, com, data,
    356   1.1      cgd 		   fflag, p));
    357   1.1      cgd 
    358   1.1      cgd 	default:
    359   1.1      cgd 		panic("spec_ioctl");
    360   1.1      cgd 		/* NOTREACHED */
    361   1.1      cgd 	}
    362   1.1      cgd }
    363   1.1      cgd 
    364   1.1      cgd /* ARGSUSED */
    365   1.4   andrew int
    366   1.1      cgd spec_select(vp, which, fflags, cred, p)
    367   1.1      cgd 	struct vnode *vp;
    368   1.1      cgd 	int which, fflags;
    369   1.1      cgd 	struct ucred *cred;
    370   1.1      cgd 	struct proc *p;
    371   1.1      cgd {
    372   1.1      cgd 	register dev_t dev;
    373   1.1      cgd 
    374   1.1      cgd 	switch (vp->v_type) {
    375   1.1      cgd 
    376   1.1      cgd 	default:
    377   1.1      cgd 		return (1);		/* XXX */
    378   1.1      cgd 
    379   1.1      cgd 	case VCHR:
    380   1.1      cgd 		dev = vp->v_rdev;
    381   1.1      cgd 		return (*cdevsw[major(dev)].d_select)(dev, which, p);
    382   1.1      cgd 	}
    383   1.1      cgd }
    384   1.1      cgd 
    385   1.1      cgd /*
    386   1.1      cgd  * Just call the device strategy routine
    387   1.1      cgd  */
    388   1.4   andrew int
    389   1.1      cgd spec_strategy(bp)
    390   1.1      cgd 	register struct buf *bp;
    391   1.1      cgd {
    392   1.1      cgd 
    393   1.1      cgd 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
    394   1.1      cgd 	return (0);
    395   1.1      cgd }
    396   1.1      cgd 
    397   1.1      cgd /*
    398   1.1      cgd  * This is a noop, simply returning what one has been given.
    399   1.1      cgd  */
    400   1.4   andrew int
    401   1.1      cgd spec_bmap(vp, bn, vpp, bnp)
    402   1.1      cgd 	struct vnode *vp;
    403   1.1      cgd 	daddr_t bn;
    404   1.1      cgd 	struct vnode **vpp;
    405   1.1      cgd 	daddr_t *bnp;
    406   1.1      cgd {
    407   1.1      cgd 
    408   1.1      cgd 	if (vpp != NULL)
    409   1.1      cgd 		*vpp = vp;
    410   1.1      cgd 	if (bnp != NULL)
    411   1.1      cgd 		*bnp = bn;
    412   1.1      cgd 	return (0);
    413   1.1      cgd }
    414   1.1      cgd 
    415   1.1      cgd /*
    416   1.1      cgd  * At the moment we do not do any locking.
    417   1.1      cgd  */
    418   1.1      cgd /* ARGSUSED */
    419   1.4   andrew int
    420   1.1      cgd spec_lock(vp)
    421   1.1      cgd 	struct vnode *vp;
    422   1.1      cgd {
    423   1.1      cgd 
    424   1.1      cgd 	return (0);
    425   1.1      cgd }
    426   1.1      cgd 
    427   1.1      cgd /* ARGSUSED */
    428   1.4   andrew int
    429   1.1      cgd spec_unlock(vp)
    430   1.1      cgd 	struct vnode *vp;
    431   1.1      cgd {
    432   1.1      cgd 
    433   1.1      cgd 	return (0);
    434   1.1      cgd }
    435   1.1      cgd 
    436   1.1      cgd /*
    437   1.1      cgd  * Device close routine
    438   1.1      cgd  */
    439   1.1      cgd /* ARGSUSED */
    440   1.4   andrew int
    441   1.1      cgd spec_close(vp, flag, cred, p)
    442   1.1      cgd 	register struct vnode *vp;
    443   1.1      cgd 	int flag;
    444   1.1      cgd 	struct ucred *cred;
    445   1.1      cgd 	struct proc *p;
    446   1.1      cgd {
    447   1.1      cgd 	dev_t dev = vp->v_rdev;
    448   1.1      cgd 	int (*devclose) __P((dev_t, int, int, struct proc *));
    449   1.1      cgd 	int mode;
    450   1.1      cgd 
    451   1.1      cgd 	switch (vp->v_type) {
    452   1.1      cgd 
    453   1.1      cgd 	case VCHR:
    454  1.11      cgd 		/*
    455  1.11      cgd 		 * Hack: a tty device that is a controlling terminal
    456  1.11      cgd 		 * has a reference from the session structure.
    457  1.11      cgd 		 * We cannot easily tell that a character device is
    458  1.11      cgd 		 * a controlling terminal, unless it is the closing
    459  1.11      cgd 		 * process' controlling terminal.  In that case,
    460  1.11      cgd 		 * if the reference count is 2 (this last descriptor
    461  1.11      cgd 		 * plus the session), release the reference from the session.
    462  1.11      cgd 		 */
    463  1.12      cgd 		if (vcount(vp) == 2 && p && vp == p->p_session->s_ttyvp) {
    464  1.11      cgd 			vrele(vp);
    465  1.12      cgd 			p->p_session->s_ttyvp = NULL;
    466  1.11      cgd 		}
    467   1.1      cgd 		/*
    468   1.1      cgd 		 * If the vnode is locked, then we are in the midst
    469   1.1      cgd 		 * of forcably closing the device, otherwise we only
    470   1.1      cgd 		 * close on last reference.
    471   1.1      cgd 		 */
    472   1.1      cgd 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
    473   1.1      cgd 			return (0);
    474   1.1      cgd 		devclose = cdevsw[major(dev)].d_close;
    475   1.1      cgd 		mode = S_IFCHR;
    476   1.1      cgd 		break;
    477   1.1      cgd 
    478   1.1      cgd 	case VBLK:
    479   1.1      cgd 		/*
    480   1.1      cgd 		 * On last close of a block device (that isn't mounted)
    481   1.1      cgd 		 * we must invalidate any in core blocks, so that
    482   1.1      cgd 		 * we can, for instance, change floppy disks.
    483   1.1      cgd 		 */
    484   1.1      cgd 		vflushbuf(vp, 0);
    485   1.1      cgd 		if (vinvalbuf(vp, 1))
    486   1.1      cgd 			return (0);
    487   1.1      cgd 		/*
    488   1.1      cgd 		 * We do not want to really close the device if it
    489   1.1      cgd 		 * is still in use unless we are trying to close it
    490   1.1      cgd 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    491   1.1      cgd 		 * holds a reference to the vnode, and because we mark
    492   1.1      cgd 		 * any other vnodes that alias this device, when the
    493   1.1      cgd 		 * sum of the reference counts on all the aliased
    494   1.1      cgd 		 * vnodes descends to one, we are on last close.
    495   1.1      cgd 		 */
    496   1.1      cgd 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
    497   1.1      cgd 			return (0);
    498   1.1      cgd 		devclose = bdevsw[major(dev)].d_close;
    499   1.1      cgd 		mode = S_IFBLK;
    500   1.1      cgd 		break;
    501   1.5      cgd 
    502   1.5      cgd #ifdef DEBUG
    503   1.5      cgd 	case VBAD:
    504   1.5      cgd 		vprint("spec_close: close VBAD vnode", vp);
    505   1.5      cgd 		break;
    506   1.5      cgd #endif
    507   1.1      cgd 
    508   1.1      cgd 	default:
    509   1.1      cgd 		panic("spec_close: not special");
    510   1.1      cgd 	}
    511   1.1      cgd 
    512   1.1      cgd 	return ((*devclose)(dev, flag, mode, p));
    513   1.1      cgd }
    514   1.1      cgd 
    515   1.1      cgd /*
    516   1.1      cgd  * Print out the contents of a special device vnode.
    517   1.1      cgd  */
    518  1.10      cgd int
    519   1.1      cgd spec_print(vp)
    520   1.1      cgd 	struct vnode *vp;
    521   1.1      cgd {
    522   1.1      cgd 
    523   1.1      cgd 	printf("tag VT_NON, dev %d, %d\n", major(vp->v_rdev),
    524   1.1      cgd 		minor(vp->v_rdev));
    525  1.10      cgd 	return (0);
    526   1.1      cgd }
    527   1.1      cgd 
    528   1.1      cgd /*
    529   1.1      cgd  * Special device advisory byte-level locks.
    530   1.1      cgd  */
    531   1.1      cgd /* ARGSUSED */
    532   1.4   andrew int
    533   1.1      cgd spec_advlock(vp, id, op, fl, flags)
    534   1.1      cgd 	struct vnode *vp;
    535   1.1      cgd 	caddr_t id;
    536   1.1      cgd 	int op;
    537   1.1      cgd 	struct flock *fl;
    538   1.1      cgd 	int flags;
    539   1.1      cgd {
    540   1.1      cgd 
    541   1.1      cgd 	return (EOPNOTSUPP);
    542   1.1      cgd }
    543   1.1      cgd 
    544   1.1      cgd /*
    545   1.1      cgd  * Special device failed operation
    546   1.1      cgd  */
    547   1.4   andrew int
    548   1.1      cgd spec_ebadf()
    549   1.1      cgd {
    550   1.1      cgd 
    551   1.1      cgd 	return (EBADF);
    552   1.1      cgd }
    553   1.1      cgd 
    554   1.1      cgd /*
    555   1.1      cgd  * Special device bad operation
    556   1.1      cgd  */
    557   1.4   andrew int
    558   1.1      cgd spec_badop()
    559   1.1      cgd {
    560   1.1      cgd 
    561   1.1      cgd 	panic("spec_badop called");
    562   1.1      cgd 	/* NOTREACHED */
    563   1.1      cgd }
    564