Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.4
      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.4  andrew  *	$Id: spec_vnops.c,v 1.4 1993/06/27 06:01:50 andrew Exp $
     35  1.1     cgd  */
     36  1.1     cgd 
     37  1.1     cgd #include "param.h"
     38  1.1     cgd #include "proc.h"
     39  1.1     cgd #include "systm.h"
     40  1.1     cgd #include "kernel.h"
     41  1.1     cgd #include "conf.h"
     42  1.1     cgd #include "buf.h"
     43  1.1     cgd #include "mount.h"
     44  1.1     cgd #include "namei.h"
     45  1.1     cgd #include "vnode.h"
     46  1.1     cgd #include "specdev.h"
     47  1.1     cgd #include "stat.h"
     48  1.1     cgd #include "errno.h"
     49  1.1     cgd #include "ioctl.h"
     50  1.1     cgd #include "file.h"
     51  1.1     cgd #include "dkbad.h"	/* XXX */
     52  1.1     cgd #include "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.1     cgd 		bscale = bsize / DEV_BSIZE;
    211  1.1     cgd 		do {
    212  1.1     cgd 			bn = (uio->uio_offset / DEV_BSIZE) &~ (bscale - 1);
    213  1.1     cgd 			on = uio->uio_offset % bsize;
    214  1.1     cgd 			n = MIN((unsigned)(bsize - on), uio->uio_resid);
    215  1.1     cgd 			if (vp->v_lastr + bscale == bn)
    216  1.1     cgd 				error = breada(vp, bn, (int)bsize, bn + bscale,
    217  1.1     cgd 					(int)bsize, NOCRED, &bp);
    218  1.1     cgd 			else
    219  1.1     cgd 				error = bread(vp, bn, (int)bsize, NOCRED, &bp);
    220  1.1     cgd 			vp->v_lastr = bn;
    221  1.1     cgd 			n = MIN(n, bsize - bp->b_resid);
    222  1.1     cgd 			if (error) {
    223  1.1     cgd 				brelse(bp);
    224  1.1     cgd 				return (error);
    225  1.1     cgd 			}
    226  1.1     cgd 			error = uiomove(bp->b_un.b_addr + on, n, uio);
    227  1.2     cgd #ifdef OMIT	/* 20 Aug 92*/
    228  1.1     cgd 			if (n + on == bsize)
    229  1.1     cgd 				bp->b_flags |= B_AGE;
    230  1.2     cgd #endif	/* OMIT*/
    231  1.1     cgd 			brelse(bp);
    232  1.1     cgd 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    233  1.1     cgd 		return (error);
    234  1.1     cgd 
    235  1.1     cgd 	default:
    236  1.1     cgd 		panic("spec_read type");
    237  1.1     cgd 	}
    238  1.1     cgd 	/* NOTREACHED */
    239  1.1     cgd }
    240  1.1     cgd 
    241  1.1     cgd /*
    242  1.1     cgd  * Vnode op for write
    243  1.1     cgd  */
    244  1.1     cgd /* ARGSUSED */
    245  1.4  andrew int
    246  1.1     cgd spec_write(vp, uio, ioflag, cred)
    247  1.1     cgd 	register struct vnode *vp;
    248  1.1     cgd 	register struct uio *uio;
    249  1.1     cgd 	int ioflag;
    250  1.1     cgd 	struct ucred *cred;
    251  1.1     cgd {
    252  1.1     cgd 	struct proc *p = uio->uio_procp;
    253  1.1     cgd 	struct buf *bp;
    254  1.1     cgd 	daddr_t bn;
    255  1.1     cgd 	int bsize, blkmask;
    256  1.1     cgd 	struct partinfo dpart;
    257  1.1     cgd 	register int n, on;
    258  1.1     cgd 	int error = 0;
    259  1.1     cgd 	extern int mem_no;
    260  1.1     cgd 
    261  1.1     cgd #ifdef DIAGNOSTIC
    262  1.1     cgd 	if (uio->uio_rw != UIO_WRITE)
    263  1.1     cgd 		panic("spec_write mode");
    264  1.1     cgd 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    265  1.1     cgd 		panic("spec_write proc");
    266  1.1     cgd #endif
    267  1.1     cgd 
    268  1.1     cgd 	switch (vp->v_type) {
    269  1.1     cgd 
    270  1.1     cgd 	case VCHR:
    271  1.1     cgd 		/*
    272  1.1     cgd 		 * Negative offsets allowed only for /dev/kmem
    273  1.1     cgd 		 */
    274  1.1     cgd 		if (uio->uio_offset < 0 && major(vp->v_rdev) != mem_no)
    275  1.1     cgd 			return (EINVAL);
    276  1.1     cgd 		VOP_UNLOCK(vp);
    277  1.1     cgd 		error = (*cdevsw[major(vp->v_rdev)].d_write)
    278  1.1     cgd 			(vp->v_rdev, uio, ioflag);
    279  1.1     cgd 		VOP_LOCK(vp);
    280  1.1     cgd 		return (error);
    281  1.1     cgd 
    282  1.1     cgd 	case VBLK:
    283  1.1     cgd 		if (uio->uio_resid == 0)
    284  1.1     cgd 			return (0);
    285  1.1     cgd 		if (uio->uio_offset < 0)
    286  1.1     cgd 			return (EINVAL);
    287  1.1     cgd 		bsize = BLKDEV_IOSIZE;
    288  1.1     cgd 		if ((*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, DIOCGPART,
    289  1.1     cgd 		    (caddr_t)&dpart, FREAD, p) == 0) {
    290  1.1     cgd 			if (dpart.part->p_fstype == FS_BSDFFS &&
    291  1.1     cgd 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    292  1.1     cgd 				bsize = dpart.part->p_frag *
    293  1.1     cgd 				    dpart.part->p_fsize;
    294  1.1     cgd 		}
    295  1.1     cgd 		blkmask = (bsize / DEV_BSIZE) - 1;
    296  1.1     cgd 		do {
    297  1.1     cgd 			bn = (uio->uio_offset / DEV_BSIZE) &~ blkmask;
    298  1.1     cgd 			on = uio->uio_offset % bsize;
    299  1.1     cgd 			n = MIN((unsigned)(bsize - on), uio->uio_resid);
    300  1.1     cgd 			if (n == bsize)
    301  1.1     cgd 				bp = getblk(vp, bn, bsize);
    302  1.1     cgd 			else
    303  1.1     cgd 				error = bread(vp, bn, bsize, NOCRED, &bp);
    304  1.1     cgd 			n = MIN(n, bsize - bp->b_resid);
    305  1.1     cgd 			if (error) {
    306  1.1     cgd 				brelse(bp);
    307  1.1     cgd 				return (error);
    308  1.1     cgd 			}
    309  1.1     cgd 			error = uiomove(bp->b_un.b_addr + on, n, uio);
    310  1.1     cgd 			if (n + on == bsize) {
    311  1.1     cgd 				bp->b_flags |= B_AGE;
    312  1.1     cgd 				bawrite(bp);
    313  1.1     cgd 			} else
    314  1.1     cgd 				bdwrite(bp);
    315  1.1     cgd 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    316  1.1     cgd 		return (error);
    317  1.1     cgd 
    318  1.1     cgd 	default:
    319  1.1     cgd 		panic("spec_write type");
    320  1.1     cgd 	}
    321  1.1     cgd 	/* NOTREACHED */
    322  1.1     cgd }
    323  1.1     cgd 
    324  1.1     cgd /*
    325  1.1     cgd  * Device ioctl operation.
    326  1.1     cgd  */
    327  1.1     cgd /* ARGSUSED */
    328  1.4  andrew int
    329  1.1     cgd spec_ioctl(vp, com, data, fflag, cred, p)
    330  1.1     cgd 	struct vnode *vp;
    331  1.1     cgd 	int com;
    332  1.1     cgd 	caddr_t data;
    333  1.1     cgd 	int fflag;
    334  1.1     cgd 	struct ucred *cred;
    335  1.1     cgd 	struct proc *p;
    336  1.1     cgd {
    337  1.1     cgd 	dev_t dev = vp->v_rdev;
    338  1.1     cgd 
    339  1.1     cgd 	switch (vp->v_type) {
    340  1.1     cgd 
    341  1.1     cgd 	case VCHR:
    342  1.1     cgd 		return ((*cdevsw[major(dev)].d_ioctl)(dev, com, data,
    343  1.1     cgd 		    fflag, p));
    344  1.1     cgd 
    345  1.1     cgd 	case VBLK:
    346  1.1     cgd 		if (com == 0 && (int)data == B_TAPE)
    347  1.1     cgd 			if (bdevsw[major(dev)].d_flags & B_TAPE)
    348  1.1     cgd 				return (0);
    349  1.1     cgd 			else
    350  1.1     cgd 				return (1);
    351  1.1     cgd 		return ((*bdevsw[major(dev)].d_ioctl)(dev, com, data,
    352  1.1     cgd 		   fflag, p));
    353  1.1     cgd 
    354  1.1     cgd 	default:
    355  1.1     cgd 		panic("spec_ioctl");
    356  1.1     cgd 		/* NOTREACHED */
    357  1.1     cgd 	}
    358  1.1     cgd }
    359  1.1     cgd 
    360  1.1     cgd /* ARGSUSED */
    361  1.4  andrew int
    362  1.1     cgd spec_select(vp, which, fflags, cred, p)
    363  1.1     cgd 	struct vnode *vp;
    364  1.1     cgd 	int which, fflags;
    365  1.1     cgd 	struct ucred *cred;
    366  1.1     cgd 	struct proc *p;
    367  1.1     cgd {
    368  1.1     cgd 	register dev_t dev;
    369  1.1     cgd 
    370  1.1     cgd 	switch (vp->v_type) {
    371  1.1     cgd 
    372  1.1     cgd 	default:
    373  1.1     cgd 		return (1);		/* XXX */
    374  1.1     cgd 
    375  1.1     cgd 	case VCHR:
    376  1.1     cgd 		dev = vp->v_rdev;
    377  1.1     cgd 		return (*cdevsw[major(dev)].d_select)(dev, which, p);
    378  1.1     cgd 	}
    379  1.1     cgd }
    380  1.1     cgd 
    381  1.1     cgd /*
    382  1.1     cgd  * Just call the device strategy routine
    383  1.1     cgd  */
    384  1.4  andrew int
    385  1.1     cgd spec_strategy(bp)
    386  1.1     cgd 	register struct buf *bp;
    387  1.1     cgd {
    388  1.1     cgd 
    389  1.1     cgd 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
    390  1.1     cgd 	return (0);
    391  1.1     cgd }
    392  1.1     cgd 
    393  1.1     cgd /*
    394  1.1     cgd  * This is a noop, simply returning what one has been given.
    395  1.1     cgd  */
    396  1.4  andrew int
    397  1.1     cgd spec_bmap(vp, bn, vpp, bnp)
    398  1.1     cgd 	struct vnode *vp;
    399  1.1     cgd 	daddr_t bn;
    400  1.1     cgd 	struct vnode **vpp;
    401  1.1     cgd 	daddr_t *bnp;
    402  1.1     cgd {
    403  1.1     cgd 
    404  1.1     cgd 	if (vpp != NULL)
    405  1.1     cgd 		*vpp = vp;
    406  1.1     cgd 	if (bnp != NULL)
    407  1.1     cgd 		*bnp = bn;
    408  1.1     cgd 	return (0);
    409  1.1     cgd }
    410  1.1     cgd 
    411  1.1     cgd /*
    412  1.1     cgd  * At the moment we do not do any locking.
    413  1.1     cgd  */
    414  1.1     cgd /* ARGSUSED */
    415  1.4  andrew int
    416  1.1     cgd spec_lock(vp)
    417  1.1     cgd 	struct vnode *vp;
    418  1.1     cgd {
    419  1.1     cgd 
    420  1.1     cgd 	return (0);
    421  1.1     cgd }
    422  1.1     cgd 
    423  1.1     cgd /* ARGSUSED */
    424  1.4  andrew int
    425  1.1     cgd spec_unlock(vp)
    426  1.1     cgd 	struct vnode *vp;
    427  1.1     cgd {
    428  1.1     cgd 
    429  1.1     cgd 	return (0);
    430  1.1     cgd }
    431  1.1     cgd 
    432  1.1     cgd /*
    433  1.1     cgd  * Device close routine
    434  1.1     cgd  */
    435  1.1     cgd /* ARGSUSED */
    436  1.4  andrew int
    437  1.1     cgd spec_close(vp, flag, cred, p)
    438  1.1     cgd 	register struct vnode *vp;
    439  1.1     cgd 	int flag;
    440  1.1     cgd 	struct ucred *cred;
    441  1.1     cgd 	struct proc *p;
    442  1.1     cgd {
    443  1.1     cgd 	dev_t dev = vp->v_rdev;
    444  1.1     cgd 	int (*devclose) __P((dev_t, int, int, struct proc *));
    445  1.1     cgd 	int mode;
    446  1.1     cgd 
    447  1.1     cgd 	switch (vp->v_type) {
    448  1.1     cgd 
    449  1.1     cgd 	case VCHR:
    450  1.1     cgd 		/*
    451  1.1     cgd 		 * If the vnode is locked, then we are in the midst
    452  1.1     cgd 		 * of forcably closing the device, otherwise we only
    453  1.1     cgd 		 * close on last reference.
    454  1.1     cgd 		 */
    455  1.1     cgd 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
    456  1.1     cgd 			return (0);
    457  1.1     cgd 		devclose = cdevsw[major(dev)].d_close;
    458  1.1     cgd 		mode = S_IFCHR;
    459  1.1     cgd 		break;
    460  1.1     cgd 
    461  1.1     cgd 	case VBLK:
    462  1.1     cgd 		/*
    463  1.1     cgd 		 * On last close of a block device (that isn't mounted)
    464  1.1     cgd 		 * we must invalidate any in core blocks, so that
    465  1.1     cgd 		 * we can, for instance, change floppy disks.
    466  1.1     cgd 		 */
    467  1.1     cgd 		vflushbuf(vp, 0);
    468  1.1     cgd 		if (vinvalbuf(vp, 1))
    469  1.1     cgd 			return (0);
    470  1.1     cgd 		/*
    471  1.1     cgd 		 * We do not want to really close the device if it
    472  1.1     cgd 		 * is still in use unless we are trying to close it
    473  1.1     cgd 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    474  1.1     cgd 		 * holds a reference to the vnode, and because we mark
    475  1.1     cgd 		 * any other vnodes that alias this device, when the
    476  1.1     cgd 		 * sum of the reference counts on all the aliased
    477  1.1     cgd 		 * vnodes descends to one, we are on last close.
    478  1.1     cgd 		 */
    479  1.1     cgd 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
    480  1.1     cgd 			return (0);
    481  1.1     cgd 		devclose = bdevsw[major(dev)].d_close;
    482  1.1     cgd 		mode = S_IFBLK;
    483  1.1     cgd 		break;
    484  1.1     cgd 
    485  1.1     cgd 	default:
    486  1.1     cgd 		panic("spec_close: not special");
    487  1.1     cgd 	}
    488  1.1     cgd 
    489  1.1     cgd 	return ((*devclose)(dev, flag, mode, p));
    490  1.1     cgd }
    491  1.1     cgd 
    492  1.1     cgd /*
    493  1.1     cgd  * Print out the contents of a special device vnode.
    494  1.1     cgd  */
    495  1.4  andrew void
    496  1.1     cgd spec_print(vp)
    497  1.1     cgd 	struct vnode *vp;
    498  1.1     cgd {
    499  1.1     cgd 
    500  1.1     cgd 	printf("tag VT_NON, dev %d, %d\n", major(vp->v_rdev),
    501  1.1     cgd 		minor(vp->v_rdev));
    502  1.1     cgd }
    503  1.1     cgd 
    504  1.1     cgd /*
    505  1.1     cgd  * Special device advisory byte-level locks.
    506  1.1     cgd  */
    507  1.1     cgd /* ARGSUSED */
    508  1.4  andrew int
    509  1.1     cgd spec_advlock(vp, id, op, fl, flags)
    510  1.1     cgd 	struct vnode *vp;
    511  1.1     cgd 	caddr_t id;
    512  1.1     cgd 	int op;
    513  1.1     cgd 	struct flock *fl;
    514  1.1     cgd 	int flags;
    515  1.1     cgd {
    516  1.1     cgd 
    517  1.1     cgd 	return (EOPNOTSUPP);
    518  1.1     cgd }
    519  1.1     cgd 
    520  1.1     cgd /*
    521  1.1     cgd  * Special device failed operation
    522  1.1     cgd  */
    523  1.4  andrew int
    524  1.1     cgd spec_ebadf()
    525  1.1     cgd {
    526  1.1     cgd 
    527  1.1     cgd 	return (EBADF);
    528  1.1     cgd }
    529  1.1     cgd 
    530  1.1     cgd /*
    531  1.1     cgd  * Special device bad operation
    532  1.1     cgd  */
    533  1.4  andrew int
    534  1.1     cgd spec_badop()
    535  1.1     cgd {
    536  1.1     cgd 
    537  1.1     cgd 	panic("spec_badop called");
    538  1.1     cgd 	/* NOTREACHED */
    539  1.1     cgd }
    540