Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.28
      1  1.28  christos /*	$NetBSD: spec_vnops.c,v 1.28 1996/02/09 22:40:57 christos 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.21   mycroft  *	@(#)spec_vnops.c	8.8 (Berkeley) 11/21/94
     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.28  christos #include <sys/cpu.h>
     53  1.28  christos 
     54  1.15   mycroft #include <miscfs/specfs/specdev.h>
     55   1.1       cgd 
     56   1.1       cgd /* symbolic sleep message strings for devices */
     57   1.1       cgd char	devopn[] = "devopn";
     58   1.1       cgd char	devio[] = "devio";
     59   1.1       cgd char	devwait[] = "devwait";
     60   1.1       cgd char	devin[] = "devin";
     61   1.1       cgd char	devout[] = "devout";
     62   1.1       cgd char	devioc[] = "devioc";
     63   1.1       cgd char	devcls[] = "devcls";
     64   1.1       cgd 
     65  1.28  christos int (**spec_vnodeop_p) __P((void *));
     66  1.15   mycroft struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
     67  1.15   mycroft 	{ &vop_default_desc, vn_default_error },
     68  1.15   mycroft 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
     69  1.15   mycroft 	{ &vop_create_desc, spec_create },		/* create */
     70  1.15   mycroft 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
     71  1.15   mycroft 	{ &vop_open_desc, spec_open },			/* open */
     72  1.15   mycroft 	{ &vop_close_desc, spec_close },		/* close */
     73  1.15   mycroft 	{ &vop_access_desc, spec_access },		/* access */
     74  1.15   mycroft 	{ &vop_getattr_desc, spec_getattr },		/* getattr */
     75  1.15   mycroft 	{ &vop_setattr_desc, spec_setattr },		/* setattr */
     76  1.15   mycroft 	{ &vop_read_desc, spec_read },			/* read */
     77  1.15   mycroft 	{ &vop_write_desc, spec_write },		/* write */
     78  1.21   mycroft 	{ &vop_lease_desc, spec_lease_check },		/* lease */
     79  1.15   mycroft 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
     80  1.15   mycroft 	{ &vop_select_desc, spec_select },		/* select */
     81  1.15   mycroft 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
     82  1.15   mycroft 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
     83  1.15   mycroft 	{ &vop_seek_desc, spec_seek },			/* seek */
     84  1.15   mycroft 	{ &vop_remove_desc, spec_remove },		/* remove */
     85  1.15   mycroft 	{ &vop_link_desc, spec_link },			/* link */
     86  1.15   mycroft 	{ &vop_rename_desc, spec_rename },		/* rename */
     87  1.15   mycroft 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
     88  1.15   mycroft 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
     89  1.15   mycroft 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
     90  1.15   mycroft 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
     91  1.15   mycroft 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
     92  1.15   mycroft 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
     93  1.15   mycroft 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
     94  1.15   mycroft 	{ &vop_reclaim_desc, spec_reclaim },		/* reclaim */
     95  1.15   mycroft 	{ &vop_lock_desc, spec_lock },			/* lock */
     96  1.15   mycroft 	{ &vop_unlock_desc, spec_unlock },		/* unlock */
     97  1.15   mycroft 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
     98  1.15   mycroft 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
     99  1.15   mycroft 	{ &vop_print_desc, spec_print },		/* print */
    100  1.15   mycroft 	{ &vop_islocked_desc, spec_islocked },		/* islocked */
    101  1.15   mycroft 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    102  1.15   mycroft 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    103  1.15   mycroft 	{ &vop_blkatoff_desc, spec_blkatoff },		/* blkatoff */
    104  1.15   mycroft 	{ &vop_valloc_desc, spec_valloc },		/* valloc */
    105  1.15   mycroft 	{ &vop_vfree_desc, spec_vfree },		/* vfree */
    106  1.15   mycroft 	{ &vop_truncate_desc, spec_truncate },		/* truncate */
    107  1.15   mycroft 	{ &vop_update_desc, spec_update },		/* update */
    108  1.28  christos 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
    109  1.28  christos 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    110   1.1       cgd };
    111  1.15   mycroft struct vnodeopv_desc spec_vnodeop_opv_desc =
    112  1.15   mycroft 	{ &spec_vnodeop_p, spec_vnodeop_entries };
    113   1.1       cgd 
    114   1.1       cgd /*
    115   1.1       cgd  * Trivial lookup routine that always fails.
    116   1.1       cgd  */
    117   1.4    andrew int
    118  1.28  christos spec_lookup(v)
    119  1.28  christos 	void *v;
    120  1.28  christos {
    121  1.15   mycroft 	struct vop_lookup_args /* {
    122  1.15   mycroft 		struct vnode *a_dvp;
    123  1.15   mycroft 		struct vnode **a_vpp;
    124  1.15   mycroft 		struct componentname *a_cnp;
    125  1.28  christos 	} */ *ap = v;
    126   1.1       cgd 
    127  1.15   mycroft 	*ap->a_vpp = NULL;
    128   1.1       cgd 	return (ENOTDIR);
    129   1.1       cgd }
    130   1.1       cgd 
    131   1.1       cgd /*
    132  1.15   mycroft  * Open a special file.
    133   1.1       cgd  */
    134   1.1       cgd /* ARGSUSED */
    135  1.28  christos int
    136  1.28  christos spec_open(v)
    137  1.28  christos 	void *v;
    138  1.28  christos {
    139  1.15   mycroft 	struct vop_open_args /* {
    140  1.15   mycroft 		struct vnode *a_vp;
    141  1.15   mycroft 		int  a_mode;
    142  1.15   mycroft 		struct ucred *a_cred;
    143  1.15   mycroft 		struct proc *a_p;
    144  1.28  christos 	} */ *ap = v;
    145  1.15   mycroft 	struct vnode *bvp, *vp = ap->a_vp;
    146  1.15   mycroft 	dev_t bdev, dev = (dev_t)vp->v_rdev;
    147   1.1       cgd 	register int maj = major(dev);
    148   1.1       cgd 	int error;
    149   1.1       cgd 
    150  1.15   mycroft 	/*
    151  1.15   mycroft 	 * Don't allow open if fs is mounted -nodev.
    152  1.15   mycroft 	 */
    153   1.1       cgd 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    154   1.1       cgd 		return (ENXIO);
    155   1.1       cgd 
    156   1.1       cgd 	switch (vp->v_type) {
    157   1.1       cgd 
    158   1.1       cgd 	case VCHR:
    159   1.1       cgd 		if ((u_int)maj >= nchrdev)
    160   1.1       cgd 			return (ENXIO);
    161  1.15   mycroft 		if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
    162  1.15   mycroft 			/*
    163  1.15   mycroft 			 * When running in very secure mode, do not allow
    164  1.15   mycroft 			 * opens for writing of any disk character devices.
    165  1.15   mycroft 			 */
    166  1.23   mycroft 			if (securelevel >= 2 && cdevsw[maj].d_type == D_DISK)
    167  1.15   mycroft 				return (EPERM);
    168  1.15   mycroft 			/*
    169  1.15   mycroft 			 * When running in secure mode, do not allow opens
    170  1.15   mycroft 			 * for writing of /dev/mem, /dev/kmem, or character
    171  1.15   mycroft 			 * devices whose corresponding block devices are
    172  1.15   mycroft 			 * currently mounted.
    173  1.15   mycroft 			 */
    174  1.15   mycroft 			if (securelevel >= 1) {
    175  1.15   mycroft 				if ((bdev = chrtoblk(dev)) != NODEV &&
    176  1.15   mycroft 				    vfinddev(bdev, VBLK, &bvp) &&
    177  1.15   mycroft 				    bvp->v_usecount > 0 &&
    178  1.15   mycroft 				    (error = vfs_mountedon(bvp)))
    179  1.15   mycroft 					return (error);
    180  1.15   mycroft 				if (iskmemdev(dev))
    181  1.15   mycroft 					return (EPERM);
    182  1.15   mycroft 			}
    183  1.15   mycroft 		}
    184  1.23   mycroft 		if (cdevsw[maj].d_type == D_TTY)
    185  1.23   mycroft 			vp->v_flag |= VISTTY;
    186   1.1       cgd 		VOP_UNLOCK(vp);
    187  1.22   mycroft 		error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, ap->a_p);
    188   1.1       cgd 		VOP_LOCK(vp);
    189   1.1       cgd 		return (error);
    190   1.1       cgd 
    191   1.1       cgd 	case VBLK:
    192   1.1       cgd 		if ((u_int)maj >= nblkdev)
    193   1.1       cgd 			return (ENXIO);
    194  1.15   mycroft 		/*
    195  1.15   mycroft 		 * When running in very secure mode, do not allow
    196  1.15   mycroft 		 * opens for writing of any disk block devices.
    197  1.15   mycroft 		 */
    198  1.15   mycroft 		if (securelevel >= 2 && ap->a_cred != FSCRED &&
    199  1.23   mycroft 		    (ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK)
    200  1.15   mycroft 			return (EPERM);
    201  1.15   mycroft 		/*
    202  1.15   mycroft 		 * Do not allow opens of block devices that are
    203  1.15   mycroft 		 * currently mounted.
    204  1.15   mycroft 		 */
    205  1.28  christos 		if ((error = vfs_mountedon(vp)) != 0)
    206   1.1       cgd 			return (error);
    207  1.22   mycroft 		return ((*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, ap->a_p));
    208  1.28  christos 	case VNON:
    209  1.28  christos 	case VLNK:
    210  1.28  christos 	case VDIR:
    211  1.28  christos 	case VREG:
    212  1.28  christos 	case VBAD:
    213  1.28  christos 	case VFIFO:
    214  1.28  christos 	case VSOCK:
    215  1.28  christos 		break;
    216   1.1       cgd 	}
    217  1.15   mycroft 	return (0);
    218   1.1       cgd }
    219   1.1       cgd 
    220   1.1       cgd /*
    221   1.1       cgd  * Vnode op for read
    222   1.1       cgd  */
    223   1.1       cgd /* ARGSUSED */
    224  1.28  christos int
    225  1.28  christos spec_read(v)
    226  1.28  christos 	void *v;
    227  1.28  christos {
    228  1.15   mycroft 	struct vop_read_args /* {
    229  1.15   mycroft 		struct vnode *a_vp;
    230  1.15   mycroft 		struct uio *a_uio;
    231  1.15   mycroft 		int  a_ioflag;
    232  1.15   mycroft 		struct ucred *a_cred;
    233  1.28  christos 	} */ *ap = v;
    234  1.15   mycroft 	register struct vnode *vp = ap->a_vp;
    235  1.15   mycroft 	register struct uio *uio = ap->a_uio;
    236  1.15   mycroft  	struct proc *p = uio->uio_procp;
    237   1.1       cgd 	struct buf *bp;
    238  1.15   mycroft 	daddr_t bn, nextbn;
    239  1.17    paulus 	long bsize, bscale, ssize;
    240   1.1       cgd 	struct partinfo dpart;
    241  1.28  christos 	int n, on, majordev;
    242  1.28  christos 	int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
    243   1.1       cgd 	int error = 0;
    244   1.1       cgd 
    245   1.1       cgd #ifdef DIAGNOSTIC
    246   1.1       cgd 	if (uio->uio_rw != UIO_READ)
    247   1.1       cgd 		panic("spec_read mode");
    248   1.1       cgd 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    249   1.1       cgd 		panic("spec_read proc");
    250   1.1       cgd #endif
    251   1.1       cgd 	if (uio->uio_resid == 0)
    252   1.1       cgd 		return (0);
    253   1.1       cgd 
    254   1.1       cgd 	switch (vp->v_type) {
    255   1.1       cgd 
    256   1.1       cgd 	case VCHR:
    257   1.1       cgd 		VOP_UNLOCK(vp);
    258   1.1       cgd 		error = (*cdevsw[major(vp->v_rdev)].d_read)
    259  1.15   mycroft 			(vp->v_rdev, uio, ap->a_ioflag);
    260   1.1       cgd 		VOP_LOCK(vp);
    261   1.1       cgd 		return (error);
    262   1.1       cgd 
    263   1.1       cgd 	case VBLK:
    264  1.24   mycroft 		if (uio->uio_resid == 0)
    265  1.24   mycroft 			return (0);
    266   1.1       cgd 		if (uio->uio_offset < 0)
    267   1.1       cgd 			return (EINVAL);
    268   1.1       cgd 		bsize = BLKDEV_IOSIZE;
    269  1.17    paulus 		ssize = DEV_BSIZE;
    270  1.24   mycroft 		if ((majordev = major(vp->v_rdev)) < nblkdev &&
    271  1.15   mycroft 		    (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
    272  1.24   mycroft 		    (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
    273  1.17    paulus 			if (dpart.part->p_fstype == FS_BSDFFS &&
    274  1.17    paulus 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    275  1.17    paulus 				bsize = dpart.part->p_frag *
    276  1.17    paulus 				    dpart.part->p_fsize;
    277  1.17    paulus 			if (dpart.disklab->d_secsize != 0)
    278  1.17    paulus 				ssize = dpart.disklab->d_secsize;
    279  1.17    paulus 		}
    280  1.17    paulus 		bscale = bsize / ssize;
    281   1.1       cgd 		do {
    282  1.17    paulus 			bn = (uio->uio_offset / ssize) &~ (bscale - 1);
    283   1.1       cgd 			on = uio->uio_offset % bsize;
    284  1.14       cgd 			n = min((unsigned)(bsize - on), uio->uio_resid);
    285  1.15   mycroft 			if (vp->v_lastr + bscale == bn) {
    286  1.15   mycroft 				nextbn = bn + bscale;
    287  1.15   mycroft 				error = breadn(vp, bn, (int)bsize, &nextbn,
    288  1.15   mycroft 					(int *)&bsize, 1, NOCRED, &bp);
    289  1.15   mycroft 			} else
    290   1.1       cgd 				error = bread(vp, bn, (int)bsize, NOCRED, &bp);
    291   1.1       cgd 			vp->v_lastr = bn;
    292  1.14       cgd 			n = min(n, bsize - bp->b_resid);
    293   1.1       cgd 			if (error) {
    294   1.1       cgd 				brelse(bp);
    295   1.1       cgd 				return (error);
    296   1.1       cgd 			}
    297  1.15   mycroft 			error = uiomove((char *)bp->b_data + on, n, uio);
    298   1.1       cgd 			brelse(bp);
    299   1.1       cgd 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    300   1.1       cgd 		return (error);
    301   1.1       cgd 
    302   1.1       cgd 	default:
    303   1.1       cgd 		panic("spec_read type");
    304   1.1       cgd 	}
    305   1.1       cgd 	/* NOTREACHED */
    306   1.1       cgd }
    307   1.1       cgd 
    308   1.1       cgd /*
    309   1.1       cgd  * Vnode op for write
    310   1.1       cgd  */
    311   1.1       cgd /* ARGSUSED */
    312  1.28  christos int
    313  1.28  christos spec_write(v)
    314  1.28  christos 	void *v;
    315  1.28  christos {
    316  1.15   mycroft 	struct vop_write_args /* {
    317  1.15   mycroft 		struct vnode *a_vp;
    318  1.15   mycroft 		struct uio *a_uio;
    319  1.15   mycroft 		int  a_ioflag;
    320  1.15   mycroft 		struct ucred *a_cred;
    321  1.28  christos 	} */ *ap = v;
    322  1.15   mycroft 	register struct vnode *vp = ap->a_vp;
    323  1.15   mycroft 	register struct uio *uio = ap->a_uio;
    324   1.1       cgd 	struct proc *p = uio->uio_procp;
    325   1.1       cgd 	struct buf *bp;
    326   1.1       cgd 	daddr_t bn;
    327  1.24   mycroft 	long bsize, bscale, ssize;
    328   1.1       cgd 	struct partinfo dpart;
    329  1.28  christos 	int n, on, majordev;
    330  1.28  christos 	int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
    331   1.1       cgd 	int error = 0;
    332   1.1       cgd 
    333   1.1       cgd #ifdef DIAGNOSTIC
    334   1.1       cgd 	if (uio->uio_rw != UIO_WRITE)
    335   1.1       cgd 		panic("spec_write mode");
    336   1.1       cgd 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    337   1.1       cgd 		panic("spec_write proc");
    338   1.1       cgd #endif
    339   1.1       cgd 
    340   1.1       cgd 	switch (vp->v_type) {
    341   1.1       cgd 
    342   1.1       cgd 	case VCHR:
    343   1.1       cgd 		VOP_UNLOCK(vp);
    344   1.1       cgd 		error = (*cdevsw[major(vp->v_rdev)].d_write)
    345  1.15   mycroft 			(vp->v_rdev, uio, ap->a_ioflag);
    346   1.1       cgd 		VOP_LOCK(vp);
    347   1.1       cgd 		return (error);
    348   1.1       cgd 
    349   1.1       cgd 	case VBLK:
    350   1.1       cgd 		if (uio->uio_resid == 0)
    351   1.1       cgd 			return (0);
    352   1.1       cgd 		if (uio->uio_offset < 0)
    353   1.1       cgd 			return (EINVAL);
    354   1.1       cgd 		bsize = BLKDEV_IOSIZE;
    355  1.17    paulus 		ssize = DEV_BSIZE;
    356  1.24   mycroft 		if ((majordev = major(vp->v_rdev)) < nblkdev &&
    357  1.24   mycroft 		    (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
    358  1.24   mycroft 		    (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
    359   1.1       cgd 			if (dpart.part->p_fstype == FS_BSDFFS &&
    360   1.1       cgd 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    361   1.1       cgd 				bsize = dpart.part->p_frag *
    362   1.1       cgd 				    dpart.part->p_fsize;
    363  1.17    paulus 			if (dpart.disklab->d_secsize != 0)
    364  1.17    paulus 				ssize = dpart.disklab->d_secsize;
    365   1.1       cgd 		}
    366  1.24   mycroft 		bscale = bsize / ssize;
    367   1.1       cgd 		do {
    368  1.24   mycroft 			bn = (uio->uio_offset / ssize) &~ (bscale - 1);
    369   1.1       cgd 			on = uio->uio_offset % bsize;
    370  1.14       cgd 			n = min((unsigned)(bsize - on), uio->uio_resid);
    371   1.1       cgd 			if (n == bsize)
    372  1.13       cgd 				bp = getblk(vp, bn, bsize, 0, 0);
    373   1.1       cgd 			else
    374   1.1       cgd 				error = bread(vp, bn, bsize, NOCRED, &bp);
    375  1.14       cgd 			n = min(n, bsize - bp->b_resid);
    376   1.1       cgd 			if (error) {
    377   1.1       cgd 				brelse(bp);
    378   1.1       cgd 				return (error);
    379   1.1       cgd 			}
    380  1.15   mycroft 			error = uiomove((char *)bp->b_data + on, n, uio);
    381  1.26       cgd 			if (n + on == bsize)
    382   1.1       cgd 				bawrite(bp);
    383  1.26       cgd 			else
    384   1.1       cgd 				bdwrite(bp);
    385   1.1       cgd 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    386   1.1       cgd 		return (error);
    387   1.1       cgd 
    388   1.1       cgd 	default:
    389   1.1       cgd 		panic("spec_write type");
    390   1.1       cgd 	}
    391   1.1       cgd 	/* NOTREACHED */
    392   1.1       cgd }
    393   1.1       cgd 
    394   1.1       cgd /*
    395   1.1       cgd  * Device ioctl operation.
    396   1.1       cgd  */
    397   1.1       cgd /* ARGSUSED */
    398  1.28  christos int
    399  1.28  christos spec_ioctl(v)
    400  1.28  christos 	void *v;
    401  1.28  christos {
    402  1.15   mycroft 	struct vop_ioctl_args /* {
    403  1.15   mycroft 		struct vnode *a_vp;
    404  1.19       cgd 		u_long a_command;
    405  1.15   mycroft 		caddr_t  a_data;
    406  1.15   mycroft 		int  a_fflag;
    407  1.15   mycroft 		struct ucred *a_cred;
    408  1.15   mycroft 		struct proc *a_p;
    409  1.28  christos 	} */ *ap = v;
    410  1.15   mycroft 	dev_t dev = ap->a_vp->v_rdev;
    411  1.23   mycroft 	int maj = major(dev);
    412   1.1       cgd 
    413  1.15   mycroft 	switch (ap->a_vp->v_type) {
    414   1.1       cgd 
    415   1.1       cgd 	case VCHR:
    416  1.23   mycroft 		return ((*cdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
    417  1.15   mycroft 		    ap->a_fflag, ap->a_p));
    418   1.1       cgd 
    419   1.1       cgd 	case VBLK:
    420  1.19       cgd 		if (ap->a_command == 0 && (long)ap->a_data == B_TAPE)
    421  1.23   mycroft 			if (bdevsw[maj].d_type == D_TAPE)
    422   1.1       cgd 				return (0);
    423   1.1       cgd 			else
    424   1.1       cgd 				return (1);
    425  1.23   mycroft 		return ((*bdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
    426  1.15   mycroft 		   ap->a_fflag, ap->a_p));
    427   1.1       cgd 
    428   1.1       cgd 	default:
    429   1.1       cgd 		panic("spec_ioctl");
    430   1.1       cgd 		/* NOTREACHED */
    431   1.1       cgd 	}
    432   1.1       cgd }
    433   1.1       cgd 
    434   1.1       cgd /* ARGSUSED */
    435  1.28  christos int
    436  1.28  christos spec_select(v)
    437  1.28  christos 	void *v;
    438  1.28  christos {
    439  1.15   mycroft 	struct vop_select_args /* {
    440  1.15   mycroft 		struct vnode *a_vp;
    441  1.15   mycroft 		int  a_which;
    442  1.15   mycroft 		int  a_fflags;
    443  1.15   mycroft 		struct ucred *a_cred;
    444  1.15   mycroft 		struct proc *a_p;
    445  1.28  christos 	} */ *ap = v;
    446   1.1       cgd 	register dev_t dev;
    447   1.1       cgd 
    448  1.15   mycroft 	switch (ap->a_vp->v_type) {
    449   1.1       cgd 
    450   1.1       cgd 	default:
    451   1.1       cgd 		return (1);		/* XXX */
    452   1.1       cgd 
    453   1.1       cgd 	case VCHR:
    454  1.15   mycroft 		dev = ap->a_vp->v_rdev;
    455  1.15   mycroft 		return (*cdevsw[major(dev)].d_select)(dev, ap->a_which, ap->a_p);
    456  1.15   mycroft 	}
    457  1.15   mycroft }
    458  1.15   mycroft /*
    459  1.15   mycroft  * Synch buffers associated with a block device
    460  1.15   mycroft  */
    461  1.15   mycroft /* ARGSUSED */
    462  1.15   mycroft int
    463  1.28  christos spec_fsync(v)
    464  1.28  christos 	void *v;
    465  1.28  christos {
    466  1.15   mycroft 	struct vop_fsync_args /* {
    467  1.15   mycroft 		struct vnode *a_vp;
    468  1.15   mycroft 		struct ucred *a_cred;
    469  1.15   mycroft 		int  a_waitfor;
    470  1.15   mycroft 		struct proc *a_p;
    471  1.28  christos 	} */ *ap = v;
    472  1.15   mycroft 	register struct vnode *vp = ap->a_vp;
    473  1.15   mycroft 	register struct buf *bp;
    474  1.15   mycroft 	struct buf *nbp;
    475  1.15   mycroft 	int s;
    476  1.15   mycroft 
    477  1.15   mycroft 	if (vp->v_type == VCHR)
    478  1.15   mycroft 		return (0);
    479  1.15   mycroft 	/*
    480  1.15   mycroft 	 * Flush all dirty buffers associated with a block device.
    481  1.15   mycroft 	 */
    482  1.15   mycroft loop:
    483  1.15   mycroft 	s = splbio();
    484  1.15   mycroft 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
    485  1.15   mycroft 		nbp = bp->b_vnbufs.le_next;
    486  1.15   mycroft 		if ((bp->b_flags & B_BUSY))
    487  1.15   mycroft 			continue;
    488  1.15   mycroft 		if ((bp->b_flags & B_DELWRI) == 0)
    489  1.15   mycroft 			panic("spec_fsync: not dirty");
    490  1.15   mycroft 		bremfree(bp);
    491  1.15   mycroft 		bp->b_flags |= B_BUSY;
    492  1.15   mycroft 		splx(s);
    493  1.15   mycroft 		bawrite(bp);
    494  1.15   mycroft 		goto loop;
    495  1.15   mycroft 	}
    496  1.15   mycroft 	if (ap->a_waitfor == MNT_WAIT) {
    497  1.15   mycroft 		while (vp->v_numoutput) {
    498  1.15   mycroft 			vp->v_flag |= VBWAIT;
    499  1.15   mycroft 			sleep((caddr_t)&vp->v_numoutput, PRIBIO + 1);
    500  1.15   mycroft 		}
    501  1.15   mycroft #ifdef DIAGNOSTIC
    502  1.15   mycroft 		if (vp->v_dirtyblkhd.lh_first) {
    503  1.25       cgd 			splx(s);
    504  1.15   mycroft 			vprint("spec_fsync: dirty", vp);
    505  1.15   mycroft 			goto loop;
    506  1.15   mycroft 		}
    507  1.15   mycroft #endif
    508   1.1       cgd 	}
    509  1.15   mycroft 	splx(s);
    510  1.15   mycroft 	return (0);
    511   1.1       cgd }
    512   1.1       cgd 
    513   1.1       cgd /*
    514   1.1       cgd  * Just call the device strategy routine
    515   1.1       cgd  */
    516  1.28  christos int
    517  1.28  christos spec_strategy(v)
    518  1.28  christos 	void *v;
    519  1.28  christos {
    520  1.15   mycroft 	struct vop_strategy_args /* {
    521  1.15   mycroft 		struct buf *a_bp;
    522  1.28  christos 	} */ *ap = v;
    523   1.1       cgd 
    524  1.15   mycroft 	(*bdevsw[major(ap->a_bp->b_dev)].d_strategy)(ap->a_bp);
    525   1.1       cgd 	return (0);
    526   1.1       cgd }
    527   1.1       cgd 
    528   1.1       cgd /*
    529   1.1       cgd  * This is a noop, simply returning what one has been given.
    530   1.1       cgd  */
    531  1.28  christos int
    532  1.28  christos spec_bmap(v)
    533  1.28  christos 	void *v;
    534  1.28  christos {
    535  1.15   mycroft 	struct vop_bmap_args /* {
    536  1.15   mycroft 		struct vnode *a_vp;
    537  1.15   mycroft 		daddr_t  a_bn;
    538  1.15   mycroft 		struct vnode **a_vpp;
    539  1.15   mycroft 		daddr_t *a_bnp;
    540  1.28  christos 	} */ *ap = v;
    541   1.1       cgd 
    542  1.15   mycroft 	if (ap->a_vpp != NULL)
    543  1.15   mycroft 		*ap->a_vpp = ap->a_vp;
    544  1.15   mycroft 	if (ap->a_bnp != NULL)
    545  1.15   mycroft 		*ap->a_bnp = ap->a_bn;
    546   1.1       cgd 	return (0);
    547   1.1       cgd }
    548   1.1       cgd 
    549   1.1       cgd /*
    550   1.1       cgd  * At the moment we do not do any locking.
    551   1.1       cgd  */
    552   1.1       cgd /* ARGSUSED */
    553  1.28  christos int
    554  1.28  christos spec_lock(v)
    555  1.28  christos 	void *v;
    556   1.1       cgd {
    557   1.1       cgd 
    558   1.1       cgd 	return (0);
    559   1.1       cgd }
    560   1.1       cgd 
    561   1.1       cgd /* ARGSUSED */
    562  1.28  christos int
    563  1.28  christos spec_unlock(v)
    564  1.28  christos 	void *v;
    565   1.1       cgd {
    566   1.1       cgd 
    567   1.1       cgd 	return (0);
    568   1.1       cgd }
    569   1.1       cgd 
    570   1.1       cgd /*
    571   1.1       cgd  * Device close routine
    572   1.1       cgd  */
    573   1.1       cgd /* ARGSUSED */
    574  1.28  christos int
    575  1.28  christos spec_close(v)
    576  1.28  christos 	void *v;
    577  1.28  christos {
    578  1.15   mycroft 	struct vop_close_args /* {
    579  1.15   mycroft 		struct vnode *a_vp;
    580  1.15   mycroft 		int  a_fflag;
    581  1.15   mycroft 		struct ucred *a_cred;
    582  1.15   mycroft 		struct proc *a_p;
    583  1.28  christos 	} */ *ap = v;
    584  1.15   mycroft 	register struct vnode *vp = ap->a_vp;
    585   1.1       cgd 	dev_t dev = vp->v_rdev;
    586   1.1       cgd 	int (*devclose) __P((dev_t, int, int, struct proc *));
    587  1.15   mycroft 	int mode, error;
    588   1.1       cgd 
    589   1.1       cgd 	switch (vp->v_type) {
    590   1.1       cgd 
    591   1.1       cgd 	case VCHR:
    592  1.11       cgd 		/*
    593  1.11       cgd 		 * Hack: a tty device that is a controlling terminal
    594  1.11       cgd 		 * has a reference from the session structure.
    595  1.11       cgd 		 * We cannot easily tell that a character device is
    596  1.11       cgd 		 * a controlling terminal, unless it is the closing
    597  1.11       cgd 		 * process' controlling terminal.  In that case,
    598  1.11       cgd 		 * if the reference count is 2 (this last descriptor
    599  1.11       cgd 		 * plus the session), release the reference from the session.
    600  1.11       cgd 		 */
    601  1.15   mycroft 		if (vcount(vp) == 2 && ap->a_p &&
    602  1.15   mycroft 		    vp == ap->a_p->p_session->s_ttyvp) {
    603  1.11       cgd 			vrele(vp);
    604  1.15   mycroft 			ap->a_p->p_session->s_ttyvp = NULL;
    605  1.11       cgd 		}
    606   1.1       cgd 		/*
    607   1.1       cgd 		 * If the vnode is locked, then we are in the midst
    608   1.1       cgd 		 * of forcably closing the device, otherwise we only
    609   1.1       cgd 		 * close on last reference.
    610   1.1       cgd 		 */
    611   1.1       cgd 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
    612   1.1       cgd 			return (0);
    613   1.1       cgd 		devclose = cdevsw[major(dev)].d_close;
    614   1.1       cgd 		mode = S_IFCHR;
    615   1.1       cgd 		break;
    616   1.1       cgd 
    617   1.1       cgd 	case VBLK:
    618   1.1       cgd 		/*
    619   1.1       cgd 		 * On last close of a block device (that isn't mounted)
    620   1.1       cgd 		 * we must invalidate any in core blocks, so that
    621   1.1       cgd 		 * we can, for instance, change floppy disks.
    622   1.1       cgd 		 */
    623  1.28  christos 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
    624  1.28  christos 		if (error)
    625  1.15   mycroft 			return (error);
    626   1.1       cgd 		/*
    627   1.1       cgd 		 * We do not want to really close the device if it
    628   1.1       cgd 		 * is still in use unless we are trying to close it
    629   1.1       cgd 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    630   1.1       cgd 		 * holds a reference to the vnode, and because we mark
    631   1.1       cgd 		 * any other vnodes that alias this device, when the
    632   1.1       cgd 		 * sum of the reference counts on all the aliased
    633   1.1       cgd 		 * vnodes descends to one, we are on last close.
    634   1.1       cgd 		 */
    635   1.1       cgd 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
    636   1.1       cgd 			return (0);
    637   1.1       cgd 		devclose = bdevsw[major(dev)].d_close;
    638   1.1       cgd 		mode = S_IFBLK;
    639   1.1       cgd 		break;
    640   1.5       cgd 
    641   1.1       cgd 	default:
    642   1.1       cgd 		panic("spec_close: not special");
    643   1.1       cgd 	}
    644   1.1       cgd 
    645  1.15   mycroft 	return ((*devclose)(dev, ap->a_fflag, mode, ap->a_p));
    646   1.1       cgd }
    647   1.1       cgd 
    648   1.1       cgd /*
    649   1.1       cgd  * Print out the contents of a special device vnode.
    650   1.1       cgd  */
    651  1.28  christos int
    652  1.28  christos spec_print(v)
    653  1.28  christos 	void *v;
    654  1.28  christos {
    655  1.15   mycroft 	struct vop_print_args /* {
    656  1.15   mycroft 		struct vnode *a_vp;
    657  1.28  christos 	} */ *ap = v;
    658  1.15   mycroft 
    659  1.15   mycroft 	printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
    660  1.15   mycroft 		minor(ap->a_vp->v_rdev));
    661  1.28  christos 	return 0;
    662  1.15   mycroft }
    663  1.15   mycroft 
    664  1.15   mycroft /*
    665  1.15   mycroft  * Return POSIX pathconf information applicable to special devices.
    666  1.15   mycroft  */
    667  1.28  christos int
    668  1.28  christos spec_pathconf(v)
    669  1.28  christos 	void *v;
    670  1.28  christos {
    671  1.15   mycroft 	struct vop_pathconf_args /* {
    672  1.15   mycroft 		struct vnode *a_vp;
    673  1.15   mycroft 		int a_name;
    674  1.18       cgd 		register_t *a_retval;
    675  1.28  christos 	} */ *ap = v;
    676   1.1       cgd 
    677  1.15   mycroft 	switch (ap->a_name) {
    678  1.15   mycroft 	case _PC_LINK_MAX:
    679  1.15   mycroft 		*ap->a_retval = LINK_MAX;
    680  1.15   mycroft 		return (0);
    681  1.15   mycroft 	case _PC_MAX_CANON:
    682  1.15   mycroft 		*ap->a_retval = MAX_CANON;
    683  1.15   mycroft 		return (0);
    684  1.15   mycroft 	case _PC_MAX_INPUT:
    685  1.15   mycroft 		*ap->a_retval = MAX_INPUT;
    686  1.15   mycroft 		return (0);
    687  1.15   mycroft 	case _PC_PIPE_BUF:
    688  1.15   mycroft 		*ap->a_retval = PIPE_BUF;
    689  1.15   mycroft 		return (0);
    690  1.15   mycroft 	case _PC_CHOWN_RESTRICTED:
    691  1.15   mycroft 		*ap->a_retval = 1;
    692  1.15   mycroft 		return (0);
    693  1.15   mycroft 	case _PC_VDISABLE:
    694  1.15   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
    695  1.15   mycroft 		return (0);
    696  1.15   mycroft 	default:
    697  1.15   mycroft 		return (EINVAL);
    698  1.15   mycroft 	}
    699  1.15   mycroft 	/* NOTREACHED */
    700   1.1       cgd }
    701   1.1       cgd 
    702   1.1       cgd /*
    703   1.1       cgd  * Special device advisory byte-level locks.
    704   1.1       cgd  */
    705   1.1       cgd /* ARGSUSED */
    706  1.28  christos int
    707  1.28  christos spec_advlock(v)
    708  1.28  christos 	void *v;
    709   1.1       cgd {
    710   1.1       cgd 
    711   1.1       cgd 	return (EOPNOTSUPP);
    712   1.1       cgd }
    713   1.1       cgd 
    714   1.1       cgd /*
    715   1.1       cgd  * Special device failed operation
    716   1.1       cgd  */
    717  1.28  christos /*ARGSUSED*/
    718  1.28  christos int
    719  1.28  christos spec_ebadf(v)
    720  1.28  christos 	void *v;
    721   1.1       cgd {
    722   1.1       cgd 
    723   1.1       cgd 	return (EBADF);
    724   1.1       cgd }
    725   1.1       cgd 
    726   1.1       cgd /*
    727   1.1       cgd  * Special device bad operation
    728   1.1       cgd  */
    729  1.28  christos /*ARGSUSED*/
    730  1.28  christos int
    731  1.28  christos spec_badop(v)
    732  1.28  christos 	void *v;
    733   1.1       cgd {
    734   1.1       cgd 
    735   1.1       cgd 	panic("spec_badop called");
    736   1.1       cgd 	/* NOTREACHED */
    737   1.1       cgd }
    738