Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.41
      1  1.41    kleink /*	$NetBSD: spec_vnops.c,v 1.41 1998/08/03 14:20:00 kleink Exp $	*/
      2  1.16       cgd 
      3   1.1       cgd /*
      4  1.15   mycroft  * Copyright (c) 1989, 1993
      5  1.15   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35  1.39      fvdl  *	@(#)spec_vnops.c	8.15 (Berkeley) 7/14/95
     36   1.1       cgd  */
     37   1.1       cgd 
     38   1.9   mycroft #include <sys/param.h>
     39   1.9   mycroft #include <sys/proc.h>
     40   1.9   mycroft #include <sys/systm.h>
     41   1.9   mycroft #include <sys/kernel.h>
     42   1.9   mycroft #include <sys/conf.h>
     43   1.9   mycroft #include <sys/buf.h>
     44   1.9   mycroft #include <sys/mount.h>
     45   1.9   mycroft #include <sys/namei.h>
     46   1.9   mycroft #include <sys/vnode.h>
     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/disklabel.h>
     52  1.35    kleink #include <sys/lockf.h>
     53  1.28  christos 
     54  1.30   mycroft #include <miscfs/genfs/genfs.h>
     55  1.15   mycroft #include <miscfs/specfs/specdev.h>
     56   1.1       cgd 
     57   1.1       cgd /* symbolic sleep message strings for devices */
     58  1.37   mycroft const char	devopn[] = "devopn";
     59  1.37   mycroft const char	devio[] = "devio";
     60  1.37   mycroft const char	devwait[] = "devwait";
     61  1.37   mycroft const char	devin[] = "devin";
     62  1.37   mycroft const char	devout[] = "devout";
     63  1.37   mycroft const char	devioc[] = "devioc";
     64  1.37   mycroft const char	devcls[] = "devcls";
     65   1.1       cgd 
     66  1.28  christos int (**spec_vnodeop_p) __P((void *));
     67  1.15   mycroft struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
     68  1.15   mycroft 	{ &vop_default_desc, vn_default_error },
     69  1.15   mycroft 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
     70  1.15   mycroft 	{ &vop_create_desc, spec_create },		/* create */
     71  1.15   mycroft 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
     72  1.15   mycroft 	{ &vop_open_desc, spec_open },			/* open */
     73  1.15   mycroft 	{ &vop_close_desc, spec_close },		/* close */
     74  1.15   mycroft 	{ &vop_access_desc, spec_access },		/* access */
     75  1.15   mycroft 	{ &vop_getattr_desc, spec_getattr },		/* getattr */
     76  1.15   mycroft 	{ &vop_setattr_desc, spec_setattr },		/* setattr */
     77  1.15   mycroft 	{ &vop_read_desc, spec_read },			/* read */
     78  1.15   mycroft 	{ &vop_write_desc, spec_write },		/* write */
     79  1.21   mycroft 	{ &vop_lease_desc, spec_lease_check },		/* lease */
     80  1.15   mycroft 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
     81  1.32   mycroft 	{ &vop_poll_desc, spec_poll },			/* poll */
     82  1.39      fvdl 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
     83  1.15   mycroft 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
     84  1.15   mycroft 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
     85  1.15   mycroft 	{ &vop_seek_desc, spec_seek },			/* seek */
     86  1.15   mycroft 	{ &vop_remove_desc, spec_remove },		/* remove */
     87  1.15   mycroft 	{ &vop_link_desc, spec_link },			/* link */
     88  1.15   mycroft 	{ &vop_rename_desc, spec_rename },		/* rename */
     89  1.15   mycroft 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
     90  1.15   mycroft 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
     91  1.15   mycroft 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
     92  1.15   mycroft 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
     93  1.15   mycroft 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
     94  1.15   mycroft 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
     95  1.15   mycroft 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
     96  1.15   mycroft 	{ &vop_reclaim_desc, spec_reclaim },		/* reclaim */
     97  1.15   mycroft 	{ &vop_lock_desc, spec_lock },			/* lock */
     98  1.15   mycroft 	{ &vop_unlock_desc, spec_unlock },		/* unlock */
     99  1.15   mycroft 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    100  1.15   mycroft 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    101  1.15   mycroft 	{ &vop_print_desc, spec_print },		/* print */
    102  1.15   mycroft 	{ &vop_islocked_desc, spec_islocked },		/* islocked */
    103  1.15   mycroft 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    104  1.15   mycroft 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    105  1.15   mycroft 	{ &vop_blkatoff_desc, spec_blkatoff },		/* blkatoff */
    106  1.15   mycroft 	{ &vop_valloc_desc, spec_valloc },		/* valloc */
    107  1.15   mycroft 	{ &vop_vfree_desc, spec_vfree },		/* vfree */
    108  1.15   mycroft 	{ &vop_truncate_desc, spec_truncate },		/* truncate */
    109  1.15   mycroft 	{ &vop_update_desc, spec_update },		/* update */
    110  1.28  christos 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
    111  1.28  christos 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    112   1.1       cgd };
    113  1.15   mycroft struct vnodeopv_desc spec_vnodeop_opv_desc =
    114  1.15   mycroft 	{ &spec_vnodeop_p, spec_vnodeop_entries };
    115   1.1       cgd 
    116   1.1       cgd /*
    117   1.1       cgd  * Trivial lookup routine that always fails.
    118   1.1       cgd  */
    119   1.4    andrew int
    120  1.28  christos spec_lookup(v)
    121  1.28  christos 	void *v;
    122  1.28  christos {
    123  1.15   mycroft 	struct vop_lookup_args /* {
    124  1.15   mycroft 		struct vnode *a_dvp;
    125  1.15   mycroft 		struct vnode **a_vpp;
    126  1.15   mycroft 		struct componentname *a_cnp;
    127  1.28  christos 	} */ *ap = v;
    128   1.1       cgd 
    129  1.15   mycroft 	*ap->a_vpp = NULL;
    130   1.1       cgd 	return (ENOTDIR);
    131   1.1       cgd }
    132   1.1       cgd 
    133   1.1       cgd /*
    134  1.15   mycroft  * Open a special file.
    135   1.1       cgd  */
    136   1.1       cgd /* ARGSUSED */
    137  1.28  christos int
    138  1.28  christos spec_open(v)
    139  1.28  christos 	void *v;
    140  1.28  christos {
    141  1.15   mycroft 	struct vop_open_args /* {
    142  1.15   mycroft 		struct vnode *a_vp;
    143  1.15   mycroft 		int  a_mode;
    144  1.15   mycroft 		struct ucred *a_cred;
    145  1.15   mycroft 		struct proc *a_p;
    146  1.28  christos 	} */ *ap = v;
    147  1.39      fvdl 	struct proc *p = ap->a_p;
    148  1.15   mycroft 	struct vnode *bvp, *vp = ap->a_vp;
    149  1.15   mycroft 	dev_t bdev, dev = (dev_t)vp->v_rdev;
    150   1.1       cgd 	register int maj = major(dev);
    151   1.1       cgd 	int error;
    152   1.1       cgd 
    153  1.15   mycroft 	/*
    154  1.15   mycroft 	 * Don't allow open if fs is mounted -nodev.
    155  1.15   mycroft 	 */
    156   1.1       cgd 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    157   1.1       cgd 		return (ENXIO);
    158   1.1       cgd 
    159   1.1       cgd 	switch (vp->v_type) {
    160   1.1       cgd 
    161   1.1       cgd 	case VCHR:
    162   1.1       cgd 		if ((u_int)maj >= nchrdev)
    163   1.1       cgd 			return (ENXIO);
    164  1.15   mycroft 		if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
    165  1.15   mycroft 			/*
    166  1.15   mycroft 			 * When running in very secure mode, do not allow
    167  1.15   mycroft 			 * opens for writing of any disk character devices.
    168  1.15   mycroft 			 */
    169  1.23   mycroft 			if (securelevel >= 2 && cdevsw[maj].d_type == D_DISK)
    170  1.15   mycroft 				return (EPERM);
    171  1.15   mycroft 			/*
    172  1.15   mycroft 			 * When running in secure mode, do not allow opens
    173  1.15   mycroft 			 * for writing of /dev/mem, /dev/kmem, or character
    174  1.15   mycroft 			 * devices whose corresponding block devices are
    175  1.15   mycroft 			 * currently mounted.
    176  1.15   mycroft 			 */
    177  1.15   mycroft 			if (securelevel >= 1) {
    178  1.38  christos 				if ((bdev = chrtoblk(dev)) != (dev_t)NODEV &&
    179  1.15   mycroft 				    vfinddev(bdev, VBLK, &bvp) &&
    180  1.15   mycroft 				    bvp->v_usecount > 0 &&
    181  1.15   mycroft 				    (error = vfs_mountedon(bvp)))
    182  1.15   mycroft 					return (error);
    183  1.15   mycroft 				if (iskmemdev(dev))
    184  1.15   mycroft 					return (EPERM);
    185  1.15   mycroft 			}
    186  1.15   mycroft 		}
    187  1.23   mycroft 		if (cdevsw[maj].d_type == D_TTY)
    188  1.23   mycroft 			vp->v_flag |= VISTTY;
    189  1.39      fvdl 		VOP_UNLOCK(vp, 0);
    190  1.39      fvdl 		error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, p);
    191  1.39      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    192   1.1       cgd 		return (error);
    193   1.1       cgd 
    194   1.1       cgd 	case VBLK:
    195   1.1       cgd 		if ((u_int)maj >= nblkdev)
    196   1.1       cgd 			return (ENXIO);
    197  1.15   mycroft 		/*
    198  1.15   mycroft 		 * When running in very secure mode, do not allow
    199  1.15   mycroft 		 * opens for writing of any disk block devices.
    200  1.15   mycroft 		 */
    201  1.15   mycroft 		if (securelevel >= 2 && ap->a_cred != FSCRED &&
    202  1.23   mycroft 		    (ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK)
    203  1.15   mycroft 			return (EPERM);
    204  1.15   mycroft 		/*
    205  1.15   mycroft 		 * Do not allow opens of block devices that are
    206  1.15   mycroft 		 * currently mounted.
    207  1.15   mycroft 		 */
    208  1.28  christos 		if ((error = vfs_mountedon(vp)) != 0)
    209   1.1       cgd 			return (error);
    210  1.39      fvdl 		return ((*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, p));
    211  1.28  christos 	case VNON:
    212  1.28  christos 	case VLNK:
    213  1.28  christos 	case VDIR:
    214  1.28  christos 	case VREG:
    215  1.28  christos 	case VBAD:
    216  1.28  christos 	case VFIFO:
    217  1.28  christos 	case VSOCK:
    218  1.28  christos 		break;
    219   1.1       cgd 	}
    220  1.15   mycroft 	return (0);
    221   1.1       cgd }
    222   1.1       cgd 
    223   1.1       cgd /*
    224   1.1       cgd  * Vnode op for read
    225   1.1       cgd  */
    226   1.1       cgd /* ARGSUSED */
    227  1.28  christos int
    228  1.28  christos spec_read(v)
    229  1.28  christos 	void *v;
    230  1.28  christos {
    231  1.15   mycroft 	struct vop_read_args /* {
    232  1.15   mycroft 		struct vnode *a_vp;
    233  1.15   mycroft 		struct uio *a_uio;
    234  1.15   mycroft 		int  a_ioflag;
    235  1.15   mycroft 		struct ucred *a_cred;
    236  1.28  christos 	} */ *ap = v;
    237  1.15   mycroft 	register struct vnode *vp = ap->a_vp;
    238  1.15   mycroft 	register struct uio *uio = ap->a_uio;
    239  1.15   mycroft  	struct proc *p = uio->uio_procp;
    240   1.1       cgd 	struct buf *bp;
    241  1.15   mycroft 	daddr_t bn, nextbn;
    242  1.17    paulus 	long bsize, bscale, ssize;
    243   1.1       cgd 	struct partinfo dpart;
    244  1.28  christos 	int n, on, majordev;
    245  1.28  christos 	int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
    246   1.1       cgd 	int error = 0;
    247   1.1       cgd 
    248   1.1       cgd #ifdef DIAGNOSTIC
    249   1.1       cgd 	if (uio->uio_rw != UIO_READ)
    250   1.1       cgd 		panic("spec_read mode");
    251   1.1       cgd 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    252   1.1       cgd 		panic("spec_read proc");
    253   1.1       cgd #endif
    254   1.1       cgd 	if (uio->uio_resid == 0)
    255   1.1       cgd 		return (0);
    256   1.1       cgd 
    257   1.1       cgd 	switch (vp->v_type) {
    258   1.1       cgd 
    259   1.1       cgd 	case VCHR:
    260  1.39      fvdl 		VOP_UNLOCK(vp, 0);
    261   1.1       cgd 		error = (*cdevsw[major(vp->v_rdev)].d_read)
    262  1.15   mycroft 			(vp->v_rdev, uio, ap->a_ioflag);
    263  1.39      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    264   1.1       cgd 		return (error);
    265   1.1       cgd 
    266   1.1       cgd 	case VBLK:
    267   1.1       cgd 		if (uio->uio_offset < 0)
    268   1.1       cgd 			return (EINVAL);
    269   1.1       cgd 		bsize = BLKDEV_IOSIZE;
    270  1.17    paulus 		ssize = DEV_BSIZE;
    271  1.24   mycroft 		if ((majordev = major(vp->v_rdev)) < nblkdev &&
    272  1.15   mycroft 		    (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
    273  1.24   mycroft 		    (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
    274  1.17    paulus 			if (dpart.part->p_fstype == FS_BSDFFS &&
    275  1.17    paulus 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    276  1.17    paulus 				bsize = dpart.part->p_frag *
    277  1.17    paulus 				    dpart.part->p_fsize;
    278  1.17    paulus 			if (dpart.disklab->d_secsize != 0)
    279  1.17    paulus 				ssize = dpart.disklab->d_secsize;
    280  1.17    paulus 		}
    281  1.17    paulus 		bscale = bsize / ssize;
    282   1.1       cgd 		do {
    283  1.17    paulus 			bn = (uio->uio_offset / ssize) &~ (bscale - 1);
    284   1.1       cgd 			on = uio->uio_offset % bsize;
    285  1.14       cgd 			n = min((unsigned)(bsize - on), uio->uio_resid);
    286  1.15   mycroft 			if (vp->v_lastr + bscale == bn) {
    287  1.15   mycroft 				nextbn = bn + bscale;
    288  1.15   mycroft 				error = breadn(vp, bn, (int)bsize, &nextbn,
    289  1.15   mycroft 					(int *)&bsize, 1, NOCRED, &bp);
    290  1.15   mycroft 			} else
    291   1.1       cgd 				error = bread(vp, bn, (int)bsize, NOCRED, &bp);
    292   1.1       cgd 			vp->v_lastr = bn;
    293  1.14       cgd 			n = min(n, bsize - bp->b_resid);
    294   1.1       cgd 			if (error) {
    295   1.1       cgd 				brelse(bp);
    296   1.1       cgd 				return (error);
    297   1.1       cgd 			}
    298  1.15   mycroft 			error = uiomove((char *)bp->b_data + on, n, uio);
    299   1.1       cgd 			brelse(bp);
    300   1.1       cgd 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    301   1.1       cgd 		return (error);
    302   1.1       cgd 
    303   1.1       cgd 	default:
    304   1.1       cgd 		panic("spec_read type");
    305   1.1       cgd 	}
    306   1.1       cgd 	/* NOTREACHED */
    307   1.1       cgd }
    308   1.1       cgd 
    309   1.1       cgd /*
    310   1.1       cgd  * Vnode op for write
    311   1.1       cgd  */
    312   1.1       cgd /* ARGSUSED */
    313  1.28  christos int
    314  1.28  christos spec_write(v)
    315  1.28  christos 	void *v;
    316  1.28  christos {
    317  1.15   mycroft 	struct vop_write_args /* {
    318  1.15   mycroft 		struct vnode *a_vp;
    319  1.15   mycroft 		struct uio *a_uio;
    320  1.15   mycroft 		int  a_ioflag;
    321  1.15   mycroft 		struct ucred *a_cred;
    322  1.28  christos 	} */ *ap = v;
    323  1.15   mycroft 	register struct vnode *vp = ap->a_vp;
    324  1.15   mycroft 	register struct uio *uio = ap->a_uio;
    325   1.1       cgd 	struct proc *p = uio->uio_procp;
    326   1.1       cgd 	struct buf *bp;
    327   1.1       cgd 	daddr_t bn;
    328  1.24   mycroft 	long bsize, bscale, ssize;
    329   1.1       cgd 	struct partinfo dpart;
    330  1.28  christos 	int n, on, majordev;
    331  1.28  christos 	int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
    332   1.1       cgd 	int error = 0;
    333   1.1       cgd 
    334   1.1       cgd #ifdef DIAGNOSTIC
    335   1.1       cgd 	if (uio->uio_rw != UIO_WRITE)
    336   1.1       cgd 		panic("spec_write mode");
    337   1.1       cgd 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    338   1.1       cgd 		panic("spec_write proc");
    339   1.1       cgd #endif
    340   1.1       cgd 
    341   1.1       cgd 	switch (vp->v_type) {
    342   1.1       cgd 
    343   1.1       cgd 	case VCHR:
    344  1.39      fvdl 		VOP_UNLOCK(vp, 0);
    345   1.1       cgd 		error = (*cdevsw[major(vp->v_rdev)].d_write)
    346  1.15   mycroft 			(vp->v_rdev, uio, ap->a_ioflag);
    347  1.39      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    348   1.1       cgd 		return (error);
    349   1.1       cgd 
    350   1.1       cgd 	case VBLK:
    351   1.1       cgd 		if (uio->uio_resid == 0)
    352   1.1       cgd 			return (0);
    353   1.1       cgd 		if (uio->uio_offset < 0)
    354   1.1       cgd 			return (EINVAL);
    355   1.1       cgd 		bsize = BLKDEV_IOSIZE;
    356  1.17    paulus 		ssize = DEV_BSIZE;
    357  1.24   mycroft 		if ((majordev = major(vp->v_rdev)) < nblkdev &&
    358  1.24   mycroft 		    (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
    359  1.24   mycroft 		    (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
    360   1.1       cgd 			if (dpart.part->p_fstype == FS_BSDFFS &&
    361   1.1       cgd 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    362   1.1       cgd 				bsize = dpart.part->p_frag *
    363   1.1       cgd 				    dpart.part->p_fsize;
    364  1.17    paulus 			if (dpart.disklab->d_secsize != 0)
    365  1.17    paulus 				ssize = dpart.disklab->d_secsize;
    366   1.1       cgd 		}
    367  1.24   mycroft 		bscale = bsize / ssize;
    368   1.1       cgd 		do {
    369  1.24   mycroft 			bn = (uio->uio_offset / ssize) &~ (bscale - 1);
    370   1.1       cgd 			on = uio->uio_offset % bsize;
    371  1.14       cgd 			n = min((unsigned)(bsize - on), uio->uio_resid);
    372   1.1       cgd 			if (n == bsize)
    373  1.13       cgd 				bp = getblk(vp, bn, bsize, 0, 0);
    374   1.1       cgd 			else
    375   1.1       cgd 				error = bread(vp, bn, bsize, NOCRED, &bp);
    376  1.14       cgd 			n = min(n, bsize - bp->b_resid);
    377   1.1       cgd 			if (error) {
    378   1.1       cgd 				brelse(bp);
    379   1.1       cgd 				return (error);
    380   1.1       cgd 			}
    381  1.15   mycroft 			error = uiomove((char *)bp->b_data + on, n, uio);
    382  1.26       cgd 			if (n + on == bsize)
    383   1.1       cgd 				bawrite(bp);
    384  1.26       cgd 			else
    385   1.1       cgd 				bdwrite(bp);
    386   1.1       cgd 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    387   1.1       cgd 		return (error);
    388   1.1       cgd 
    389   1.1       cgd 	default:
    390   1.1       cgd 		panic("spec_write type");
    391   1.1       cgd 	}
    392   1.1       cgd 	/* NOTREACHED */
    393   1.1       cgd }
    394   1.1       cgd 
    395   1.1       cgd /*
    396   1.1       cgd  * Device ioctl operation.
    397   1.1       cgd  */
    398   1.1       cgd /* ARGSUSED */
    399  1.28  christos int
    400  1.28  christos spec_ioctl(v)
    401  1.28  christos 	void *v;
    402  1.28  christos {
    403  1.15   mycroft 	struct vop_ioctl_args /* {
    404  1.15   mycroft 		struct vnode *a_vp;
    405  1.19       cgd 		u_long a_command;
    406  1.15   mycroft 		caddr_t  a_data;
    407  1.15   mycroft 		int  a_fflag;
    408  1.15   mycroft 		struct ucred *a_cred;
    409  1.15   mycroft 		struct proc *a_p;
    410  1.28  christos 	} */ *ap = v;
    411  1.15   mycroft 	dev_t dev = ap->a_vp->v_rdev;
    412  1.23   mycroft 	int maj = major(dev);
    413   1.1       cgd 
    414  1.15   mycroft 	switch (ap->a_vp->v_type) {
    415   1.1       cgd 
    416   1.1       cgd 	case VCHR:
    417  1.23   mycroft 		return ((*cdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
    418  1.15   mycroft 		    ap->a_fflag, ap->a_p));
    419   1.1       cgd 
    420   1.1       cgd 	case VBLK:
    421  1.19       cgd 		if (ap->a_command == 0 && (long)ap->a_data == B_TAPE)
    422  1.23   mycroft 			if (bdevsw[maj].d_type == D_TAPE)
    423   1.1       cgd 				return (0);
    424   1.1       cgd 			else
    425   1.1       cgd 				return (1);
    426  1.23   mycroft 		return ((*bdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
    427  1.15   mycroft 		   ap->a_fflag, ap->a_p));
    428   1.1       cgd 
    429   1.1       cgd 	default:
    430   1.1       cgd 		panic("spec_ioctl");
    431   1.1       cgd 		/* NOTREACHED */
    432   1.1       cgd 	}
    433   1.1       cgd }
    434   1.1       cgd 
    435   1.1       cgd /* ARGSUSED */
    436  1.28  christos int
    437  1.32   mycroft spec_poll(v)
    438  1.28  christos 	void *v;
    439  1.28  christos {
    440  1.32   mycroft 	struct vop_poll_args /* {
    441  1.15   mycroft 		struct vnode *a_vp;
    442  1.32   mycroft 		int a_events;
    443  1.15   mycroft 		struct proc *a_p;
    444  1.28  christos 	} */ *ap = v;
    445   1.1       cgd 	register dev_t dev;
    446   1.1       cgd 
    447  1.15   mycroft 	switch (ap->a_vp->v_type) {
    448   1.1       cgd 
    449   1.1       cgd 	case VCHR:
    450  1.15   mycroft 		dev = ap->a_vp->v_rdev;
    451  1.32   mycroft 		return (*cdevsw[major(dev)].d_poll)(dev, ap->a_events, ap->a_p);
    452  1.30   mycroft 
    453  1.30   mycroft 	default:
    454  1.32   mycroft 		return (genfs_poll(v));
    455  1.15   mycroft 	}
    456  1.15   mycroft }
    457  1.15   mycroft /*
    458  1.15   mycroft  * Synch buffers associated with a block device
    459  1.15   mycroft  */
    460  1.15   mycroft /* ARGSUSED */
    461  1.15   mycroft int
    462  1.28  christos spec_fsync(v)
    463  1.28  christos 	void *v;
    464  1.28  christos {
    465  1.15   mycroft 	struct vop_fsync_args /* {
    466  1.15   mycroft 		struct vnode *a_vp;
    467  1.15   mycroft 		struct ucred *a_cred;
    468  1.40    kleink 		int  a_flags;
    469  1.15   mycroft 		struct proc *a_p;
    470  1.28  christos 	} */ *ap = v;
    471  1.15   mycroft 	register struct vnode *vp = ap->a_vp;
    472  1.15   mycroft 
    473  1.30   mycroft 	if (vp->v_type == VBLK)
    474  1.40    kleink 		vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
    475  1.15   mycroft 	return (0);
    476   1.1       cgd }
    477   1.1       cgd 
    478   1.1       cgd /*
    479   1.1       cgd  * Just call the device strategy routine
    480   1.1       cgd  */
    481  1.28  christos int
    482  1.28  christos spec_strategy(v)
    483  1.28  christos 	void *v;
    484  1.28  christos {
    485  1.15   mycroft 	struct vop_strategy_args /* {
    486  1.15   mycroft 		struct buf *a_bp;
    487  1.28  christos 	} */ *ap = v;
    488   1.1       cgd 
    489  1.15   mycroft 	(*bdevsw[major(ap->a_bp->b_dev)].d_strategy)(ap->a_bp);
    490   1.1       cgd 	return (0);
    491   1.1       cgd }
    492   1.1       cgd 
    493  1.39      fvdl int
    494  1.39      fvdl spec_inactive(v)
    495  1.39      fvdl 	void *v;
    496  1.39      fvdl {
    497  1.39      fvdl 	struct vop_inactive_args /* {
    498  1.39      fvdl 		struct vnode *a_vp;
    499  1.39      fvdl 		struct proc *a_p;
    500  1.39      fvdl 	} */ *ap = v;
    501  1.39      fvdl 
    502  1.39      fvdl 	VOP_UNLOCK(ap->a_vp, 0);
    503  1.39      fvdl 	return (0);
    504  1.39      fvdl }
    505  1.39      fvdl 
    506   1.1       cgd /*
    507   1.1       cgd  * This is a noop, simply returning what one has been given.
    508   1.1       cgd  */
    509  1.28  christos int
    510  1.28  christos spec_bmap(v)
    511  1.28  christos 	void *v;
    512  1.28  christos {
    513  1.15   mycroft 	struct vop_bmap_args /* {
    514  1.15   mycroft 		struct vnode *a_vp;
    515  1.15   mycroft 		daddr_t  a_bn;
    516  1.15   mycroft 		struct vnode **a_vpp;
    517  1.15   mycroft 		daddr_t *a_bnp;
    518  1.39      fvdl 		int *a_runp;
    519  1.28  christos 	} */ *ap = v;
    520   1.1       cgd 
    521  1.15   mycroft 	if (ap->a_vpp != NULL)
    522  1.15   mycroft 		*ap->a_vpp = ap->a_vp;
    523  1.15   mycroft 	if (ap->a_bnp != NULL)
    524  1.15   mycroft 		*ap->a_bnp = ap->a_bn;
    525  1.39      fvdl 	if (ap->a_runp != NULL)
    526  1.39      fvdl 		*ap->a_runp = 0;
    527   1.1       cgd 	return (0);
    528   1.1       cgd }
    529   1.1       cgd 
    530   1.1       cgd /*
    531   1.1       cgd  * Device close routine
    532   1.1       cgd  */
    533   1.1       cgd /* ARGSUSED */
    534  1.28  christos int
    535  1.28  christos spec_close(v)
    536  1.28  christos 	void *v;
    537  1.28  christos {
    538  1.15   mycroft 	struct vop_close_args /* {
    539  1.15   mycroft 		struct vnode *a_vp;
    540  1.15   mycroft 		int  a_fflag;
    541  1.15   mycroft 		struct ucred *a_cred;
    542  1.15   mycroft 		struct proc *a_p;
    543  1.28  christos 	} */ *ap = v;
    544  1.15   mycroft 	register struct vnode *vp = ap->a_vp;
    545   1.1       cgd 	dev_t dev = vp->v_rdev;
    546   1.1       cgd 	int (*devclose) __P((dev_t, int, int, struct proc *));
    547  1.15   mycroft 	int mode, error;
    548   1.1       cgd 
    549   1.1       cgd 	switch (vp->v_type) {
    550   1.1       cgd 
    551   1.1       cgd 	case VCHR:
    552  1.11       cgd 		/*
    553  1.11       cgd 		 * Hack: a tty device that is a controlling terminal
    554  1.11       cgd 		 * has a reference from the session structure.
    555  1.11       cgd 		 * We cannot easily tell that a character device is
    556  1.11       cgd 		 * a controlling terminal, unless it is the closing
    557  1.11       cgd 		 * process' controlling terminal.  In that case,
    558  1.11       cgd 		 * if the reference count is 2 (this last descriptor
    559  1.11       cgd 		 * plus the session), release the reference from the session.
    560  1.11       cgd 		 */
    561  1.15   mycroft 		if (vcount(vp) == 2 && ap->a_p &&
    562  1.15   mycroft 		    vp == ap->a_p->p_session->s_ttyvp) {
    563  1.11       cgd 			vrele(vp);
    564  1.15   mycroft 			ap->a_p->p_session->s_ttyvp = NULL;
    565  1.11       cgd 		}
    566   1.1       cgd 		/*
    567   1.1       cgd 		 * If the vnode is locked, then we are in the midst
    568   1.1       cgd 		 * of forcably closing the device, otherwise we only
    569   1.1       cgd 		 * close on last reference.
    570   1.1       cgd 		 */
    571   1.1       cgd 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
    572   1.1       cgd 			return (0);
    573   1.1       cgd 		devclose = cdevsw[major(dev)].d_close;
    574   1.1       cgd 		mode = S_IFCHR;
    575   1.1       cgd 		break;
    576   1.1       cgd 
    577   1.1       cgd 	case VBLK:
    578   1.1       cgd 		/*
    579   1.1       cgd 		 * On last close of a block device (that isn't mounted)
    580   1.1       cgd 		 * we must invalidate any in core blocks, so that
    581   1.1       cgd 		 * we can, for instance, change floppy disks.
    582   1.1       cgd 		 */
    583  1.28  christos 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
    584  1.28  christos 		if (error)
    585  1.15   mycroft 			return (error);
    586   1.1       cgd 		/*
    587   1.1       cgd 		 * We do not want to really close the device if it
    588   1.1       cgd 		 * is still in use unless we are trying to close it
    589   1.1       cgd 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    590   1.1       cgd 		 * holds a reference to the vnode, and because we mark
    591   1.1       cgd 		 * any other vnodes that alias this device, when the
    592   1.1       cgd 		 * sum of the reference counts on all the aliased
    593   1.1       cgd 		 * vnodes descends to one, we are on last close.
    594   1.1       cgd 		 */
    595   1.1       cgd 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
    596   1.1       cgd 			return (0);
    597   1.1       cgd 		devclose = bdevsw[major(dev)].d_close;
    598   1.1       cgd 		mode = S_IFBLK;
    599   1.1       cgd 		break;
    600   1.5       cgd 
    601   1.1       cgd 	default:
    602   1.1       cgd 		panic("spec_close: not special");
    603   1.1       cgd 	}
    604   1.1       cgd 
    605  1.15   mycroft 	return ((*devclose)(dev, ap->a_fflag, mode, ap->a_p));
    606   1.1       cgd }
    607   1.1       cgd 
    608   1.1       cgd /*
    609   1.1       cgd  * Print out the contents of a special device vnode.
    610   1.1       cgd  */
    611  1.28  christos int
    612  1.28  christos spec_print(v)
    613  1.28  christos 	void *v;
    614  1.28  christos {
    615  1.15   mycroft 	struct vop_print_args /* {
    616  1.15   mycroft 		struct vnode *a_vp;
    617  1.28  christos 	} */ *ap = v;
    618  1.15   mycroft 
    619  1.34  christos 	printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
    620  1.33  christos 	    minor(ap->a_vp->v_rdev));
    621  1.28  christos 	return 0;
    622  1.15   mycroft }
    623  1.15   mycroft 
    624  1.15   mycroft /*
    625  1.15   mycroft  * Return POSIX pathconf information applicable to special devices.
    626  1.15   mycroft  */
    627  1.28  christos int
    628  1.28  christos spec_pathconf(v)
    629  1.28  christos 	void *v;
    630  1.28  christos {
    631  1.15   mycroft 	struct vop_pathconf_args /* {
    632  1.15   mycroft 		struct vnode *a_vp;
    633  1.15   mycroft 		int a_name;
    634  1.18       cgd 		register_t *a_retval;
    635  1.28  christos 	} */ *ap = v;
    636   1.1       cgd 
    637  1.15   mycroft 	switch (ap->a_name) {
    638  1.15   mycroft 	case _PC_LINK_MAX:
    639  1.15   mycroft 		*ap->a_retval = LINK_MAX;
    640  1.15   mycroft 		return (0);
    641  1.15   mycroft 	case _PC_MAX_CANON:
    642  1.15   mycroft 		*ap->a_retval = MAX_CANON;
    643  1.15   mycroft 		return (0);
    644  1.15   mycroft 	case _PC_MAX_INPUT:
    645  1.15   mycroft 		*ap->a_retval = MAX_INPUT;
    646  1.15   mycroft 		return (0);
    647  1.15   mycroft 	case _PC_PIPE_BUF:
    648  1.15   mycroft 		*ap->a_retval = PIPE_BUF;
    649  1.15   mycroft 		return (0);
    650  1.15   mycroft 	case _PC_CHOWN_RESTRICTED:
    651  1.15   mycroft 		*ap->a_retval = 1;
    652  1.15   mycroft 		return (0);
    653  1.15   mycroft 	case _PC_VDISABLE:
    654  1.15   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
    655  1.41    kleink 		return (0);
    656  1.41    kleink 	case _PC_SYNC_IO:
    657  1.41    kleink 		*ap->a_retval = 1;
    658  1.15   mycroft 		return (0);
    659  1.15   mycroft 	default:
    660  1.15   mycroft 		return (EINVAL);
    661  1.15   mycroft 	}
    662   1.1       cgd 	/* NOTREACHED */
    663  1.35    kleink }
    664  1.35    kleink 
    665  1.35    kleink /*
    666  1.35    kleink  * Advisory record locking support.
    667  1.35    kleink  */
    668  1.35    kleink int
    669  1.35    kleink spec_advlock(v)
    670  1.35    kleink 	void *v;
    671  1.35    kleink {
    672  1.35    kleink 	struct vop_advlock_args /* {
    673  1.35    kleink 		struct vnode *a_vp;
    674  1.35    kleink 		caddr_t a_id;
    675  1.35    kleink 		int a_op;
    676  1.35    kleink 		struct flock *a_fl;
    677  1.35    kleink 		int a_flags;
    678  1.35    kleink 	} */ *ap = v;
    679  1.35    kleink 	register struct vnode *vp = ap->a_vp;
    680  1.35    kleink 
    681  1.35    kleink 	return (lf_advlock(&vp->v_speclockf, (off_t)0, ap->a_id, ap->a_op,
    682  1.35    kleink 	                   ap->a_fl, ap->a_flags));
    683   1.1       cgd }
    684