Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.106
      1  1.106   hannken /*	$NetBSD: spec_vnops.c,v 1.106 2007/10/07 13:39:04 hannken 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.69       agc  * 3. Neither the name of the University nor the names of its contributors
     16    1.1       cgd  *    may be used to endorse or promote products derived from this software
     17    1.1       cgd  *    without specific prior written permission.
     18    1.1       cgd  *
     19    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29    1.1       cgd  * SUCH DAMAGE.
     30    1.1       cgd  *
     31   1.39      fvdl  *	@(#)spec_vnops.c	8.15 (Berkeley) 7/14/95
     32    1.1       cgd  */
     33   1.60     lukem 
     34   1.60     lukem #include <sys/cdefs.h>
     35  1.106   hannken __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.106 2007/10/07 13:39:04 hannken Exp $");
     36    1.1       cgd 
     37    1.9   mycroft #include <sys/param.h>
     38    1.9   mycroft #include <sys/proc.h>
     39    1.9   mycroft #include <sys/systm.h>
     40    1.9   mycroft #include <sys/kernel.h>
     41    1.9   mycroft #include <sys/conf.h>
     42    1.9   mycroft #include <sys/buf.h>
     43    1.9   mycroft #include <sys/mount.h>
     44    1.9   mycroft #include <sys/namei.h>
     45    1.9   mycroft #include <sys/vnode.h>
     46    1.9   mycroft #include <sys/stat.h>
     47    1.9   mycroft #include <sys/errno.h>
     48    1.9   mycroft #include <sys/ioctl.h>
     49   1.81        ws #include <sys/poll.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.71       dsl #include <sys/tty.h>
     54   1.87      elad #include <sys/kauth.h>
     55  1.106   hannken #include <sys/fstrans.h>
     56   1.28  christos 
     57   1.30   mycroft #include <miscfs/genfs/genfs.h>
     58   1.15   mycroft #include <miscfs/specfs/specdev.h>
     59    1.1       cgd 
     60    1.1       cgd /* symbolic sleep message strings for devices */
     61   1.37   mycroft const char	devopn[] = "devopn";
     62   1.37   mycroft const char	devio[] = "devio";
     63   1.37   mycroft const char	devwait[] = "devwait";
     64   1.37   mycroft const char	devin[] = "devin";
     65   1.37   mycroft const char	devout[] = "devout";
     66   1.37   mycroft const char	devioc[] = "devioc";
     67   1.37   mycroft const char	devcls[] = "devcls";
     68   1.61      matt 
     69   1.61      matt struct vnode	*speclisth[SPECHSZ];
     70   1.46  sommerfe 
     71   1.46  sommerfe /*
     72   1.46  sommerfe  * This vnode operations vector is used for two things only:
     73   1.46  sommerfe  * - special device nodes created from whole cloth by the kernel.
     74   1.46  sommerfe  * - as a temporary vnodeops replacement for vnodes which were found to
     75   1.46  sommerfe  *	be aliased by callers of checkalias().
     76   1.46  sommerfe  * For the ops vector for vnodes built from special devices found in a
     77   1.46  sommerfe  * filesystem, see (e.g) ffs_specop_entries[] in ffs_vnops.c or the
     78   1.46  sommerfe  * equivalent for other filesystems.
     79   1.46  sommerfe  */
     80    1.1       cgd 
     81   1.82   xtraeme int (**spec_vnodeop_p)(void *);
     82   1.53  jdolecek const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
     83   1.15   mycroft 	{ &vop_default_desc, vn_default_error },
     84   1.15   mycroft 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
     85   1.15   mycroft 	{ &vop_create_desc, spec_create },		/* create */
     86   1.15   mycroft 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
     87   1.15   mycroft 	{ &vop_open_desc, spec_open },			/* open */
     88   1.15   mycroft 	{ &vop_close_desc, spec_close },		/* close */
     89   1.15   mycroft 	{ &vop_access_desc, spec_access },		/* access */
     90   1.15   mycroft 	{ &vop_getattr_desc, spec_getattr },		/* getattr */
     91   1.15   mycroft 	{ &vop_setattr_desc, spec_setattr },		/* setattr */
     92   1.15   mycroft 	{ &vop_read_desc, spec_read },			/* read */
     93   1.15   mycroft 	{ &vop_write_desc, spec_write },		/* write */
     94   1.21   mycroft 	{ &vop_lease_desc, spec_lease_check },		/* lease */
     95   1.47  sommerfe 	{ &vop_fcntl_desc, spec_fcntl },		/* fcntl */
     96   1.15   mycroft 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
     97   1.32   mycroft 	{ &vop_poll_desc, spec_poll },			/* poll */
     98   1.65  jdolecek 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
     99   1.39      fvdl 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    100   1.15   mycroft 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    101   1.15   mycroft 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    102   1.15   mycroft 	{ &vop_seek_desc, spec_seek },			/* seek */
    103   1.15   mycroft 	{ &vop_remove_desc, spec_remove },		/* remove */
    104   1.15   mycroft 	{ &vop_link_desc, spec_link },			/* link */
    105   1.15   mycroft 	{ &vop_rename_desc, spec_rename },		/* rename */
    106   1.15   mycroft 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    107   1.15   mycroft 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    108   1.15   mycroft 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    109   1.15   mycroft 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    110   1.15   mycroft 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    111   1.15   mycroft 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    112   1.15   mycroft 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
    113   1.15   mycroft 	{ &vop_reclaim_desc, spec_reclaim },		/* reclaim */
    114   1.15   mycroft 	{ &vop_lock_desc, spec_lock },			/* lock */
    115   1.15   mycroft 	{ &vop_unlock_desc, spec_unlock },		/* unlock */
    116   1.15   mycroft 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    117   1.15   mycroft 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    118   1.15   mycroft 	{ &vop_print_desc, spec_print },		/* print */
    119   1.15   mycroft 	{ &vop_islocked_desc, spec_islocked },		/* islocked */
    120   1.15   mycroft 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    121   1.15   mycroft 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    122   1.28  christos 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
    123   1.55       chs 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    124   1.55       chs 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    125   1.55       chs 	{ NULL, NULL }
    126    1.1       cgd };
    127   1.53  jdolecek const struct vnodeopv_desc spec_vnodeop_opv_desc =
    128   1.15   mycroft 	{ &spec_vnodeop_p, spec_vnodeop_entries };
    129    1.1       cgd 
    130    1.1       cgd /*
    131    1.1       cgd  * Trivial lookup routine that always fails.
    132    1.1       cgd  */
    133    1.4    andrew int
    134  1.104     pooka spec_lookup(void *v)
    135   1.28  christos {
    136   1.15   mycroft 	struct vop_lookup_args /* {
    137   1.15   mycroft 		struct vnode *a_dvp;
    138   1.15   mycroft 		struct vnode **a_vpp;
    139   1.15   mycroft 		struct componentname *a_cnp;
    140   1.28  christos 	} */ *ap = v;
    141    1.1       cgd 
    142   1.15   mycroft 	*ap->a_vpp = NULL;
    143    1.1       cgd 	return (ENOTDIR);
    144   1.66  jdolecek }
    145   1.66  jdolecek 
    146   1.66  jdolecek /*
    147   1.66  jdolecek  * Returns true if dev is /dev/mem or /dev/kmem.
    148   1.66  jdolecek  */
    149   1.96      elad int
    150   1.66  jdolecek iskmemdev(dev_t dev)
    151   1.66  jdolecek {
    152   1.66  jdolecek 	/* mem_no is emitted by config(8) to generated devsw.c */
    153   1.66  jdolecek 	extern const int mem_no;
    154   1.66  jdolecek 
    155   1.66  jdolecek 	/* minor 14 is /dev/io on i386 with COMPAT_10 */
    156   1.66  jdolecek 	return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
    157    1.1       cgd }
    158    1.1       cgd 
    159    1.1       cgd /*
    160   1.15   mycroft  * Open a special file.
    161    1.1       cgd  */
    162    1.1       cgd /* ARGSUSED */
    163   1.28  christos int
    164  1.104     pooka spec_open(void *v)
    165   1.28  christos {
    166   1.15   mycroft 	struct vop_open_args /* {
    167   1.15   mycroft 		struct vnode *a_vp;
    168   1.15   mycroft 		int  a_mode;
    169   1.87      elad 		kauth_cred_t a_cred;
    170   1.85  christos 		struct lwp *a_l;
    171   1.28  christos 	} */ *ap = v;
    172   1.85  christos 	struct lwp *l = ap->a_l;
    173   1.94      elad 	struct vnode *vp = ap->a_vp;
    174   1.94      elad 	dev_t dev = (dev_t)vp->v_rdev;
    175    1.1       cgd 	int error;
    176   1.55       chs 	struct partinfo pi;
    177   1.96      elad 	enum kauth_device_req req;
    178    1.1       cgd 
    179   1.15   mycroft 	/*
    180   1.15   mycroft 	 * Don't allow open if fs is mounted -nodev.
    181   1.15   mycroft 	 */
    182    1.1       cgd 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    183    1.1       cgd 		return (ENXIO);
    184    1.1       cgd 
    185   1.96      elad #define M2K(m)	(((m) & FREAD) && ((m) & FWRITE) ? \
    186   1.96      elad 		 KAUTH_REQ_DEVICE_RAWIO_SPEC_RW : \
    187   1.96      elad 		 (m) & FWRITE ? KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE : \
    188   1.96      elad 		 KAUTH_REQ_DEVICE_RAWIO_SPEC_READ)
    189   1.89      elad 
    190    1.1       cgd 	switch (vp->v_type) {
    191    1.1       cgd 
    192    1.1       cgd 	case VCHR:
    193   1.96      elad 		req = M2K(ap->a_mode);
    194   1.96      elad 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    195   1.96      elad 		if (error)
    196   1.96      elad 			return (error);
    197   1.89      elad 
    198  1.100        ad 		if (cdev_type(dev) == D_TTY)
    199   1.23   mycroft 			vp->v_flag |= VISTTY;
    200   1.39      fvdl 		VOP_UNLOCK(vp, 0);
    201  1.100        ad 		error = cdev_open(dev, ap->a_mode, S_IFCHR, l);
    202   1.39      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    203  1.100        ad 		if (cdev_type(dev) != D_DISK)
    204   1.70       dsl 			return error;
    205   1.70       dsl 		break;
    206    1.1       cgd 
    207    1.1       cgd 	case VBLK:
    208   1.96      elad 		req = M2K(ap->a_mode);
    209   1.96      elad 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    210   1.96      elad 		if (error)
    211   1.96      elad 			return (error);
    212  1.100        ad 		error = bdev_open(dev, ap->a_mode, S_IFBLK, l);
    213   1.70       dsl 		break;
    214   1.55       chs 
    215   1.28  christos 	case VNON:
    216   1.28  christos 	case VLNK:
    217   1.28  christos 	case VDIR:
    218   1.28  christos 	case VREG:
    219   1.28  christos 	case VBAD:
    220   1.28  christos 	case VFIFO:
    221   1.28  christos 	case VSOCK:
    222   1.70       dsl 	default:
    223   1.70       dsl 		return 0;
    224    1.1       cgd 	}
    225   1.70       dsl 
    226   1.89      elad #undef M2K
    227   1.89      elad 
    228   1.70       dsl 	if (error)
    229   1.70       dsl 		return error;
    230  1.100        ad 	if (vp->v_type == VCHR)
    231  1.100        ad 		error = cdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
    232  1.100        ad 	else
    233  1.100        ad 		error = bdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
    234  1.100        ad 	if (error == 0)
    235   1.99      yamt 		uvm_vnp_setsize(vp,
    236   1.99      yamt 		    (voff_t)pi.disklab->d_secsize * pi.part->p_size);
    237   1.70       dsl 	return 0;
    238    1.1       cgd }
    239    1.1       cgd 
    240    1.1       cgd /*
    241    1.1       cgd  * Vnode op for read
    242    1.1       cgd  */
    243    1.1       cgd /* ARGSUSED */
    244   1.28  christos int
    245  1.104     pooka spec_read(void *v)
    246   1.28  christos {
    247   1.15   mycroft 	struct vop_read_args /* {
    248   1.15   mycroft 		struct vnode *a_vp;
    249   1.15   mycroft 		struct uio *a_uio;
    250   1.15   mycroft 		int  a_ioflag;
    251   1.87      elad 		kauth_cred_t a_cred;
    252   1.28  christos 	} */ *ap = v;
    253   1.48  augustss 	struct vnode *vp = ap->a_vp;
    254   1.48  augustss 	struct uio *uio = ap->a_uio;
    255   1.86      yamt  	struct lwp *l = curlwp;
    256   1.56       chs 	struct buf *bp;
    257   1.57       chs 	daddr_t bn;
    258   1.59       chs 	int bsize, bscale;
    259   1.56       chs 	struct partinfo dpart;
    260   1.64   gehenna 	int n, on;
    261    1.1       cgd 	int error = 0;
    262    1.1       cgd 
    263    1.1       cgd #ifdef DIAGNOSTIC
    264    1.1       cgd 	if (uio->uio_rw != UIO_READ)
    265    1.1       cgd 		panic("spec_read mode");
    266   1.86      yamt 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    267   1.86      yamt 	    uio->uio_vmspace != curproc->p_vmspace)
    268    1.1       cgd 		panic("spec_read proc");
    269    1.1       cgd #endif
    270    1.1       cgd 	if (uio->uio_resid == 0)
    271    1.1       cgd 		return (0);
    272    1.1       cgd 
    273   1.56       chs 	switch (vp->v_type) {
    274   1.56       chs 
    275   1.56       chs 	case VCHR:
    276   1.39      fvdl 		VOP_UNLOCK(vp, 0);
    277  1.100        ad 		error = cdev_read(vp->v_rdev, uio, ap->a_ioflag);
    278   1.58       chs 		vn_lock(vp, LK_SHARED | LK_RETRY);
    279    1.1       cgd 		return (error);
    280    1.1       cgd 
    281   1.56       chs 	case VBLK:
    282   1.56       chs 		if (uio->uio_offset < 0)
    283   1.56       chs 			return (EINVAL);
    284   1.56       chs 		bsize = BLKDEV_IOSIZE;
    285  1.100        ad 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
    286   1.56       chs 			if (dpart.part->p_fstype == FS_BSDFFS &&
    287   1.56       chs 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    288   1.56       chs 				bsize = dpart.part->p_frag *
    289   1.56       chs 				    dpart.part->p_fsize;
    290   1.56       chs 		}
    291   1.59       chs 		bscale = bsize >> DEV_BSHIFT;
    292   1.56       chs 		do {
    293   1.59       chs 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    294   1.56       chs 			on = uio->uio_offset % bsize;
    295   1.56       chs 			n = min((unsigned)(bsize - on), uio->uio_resid);
    296   1.57       chs 			error = bread(vp, bn, bsize, NOCRED, &bp);
    297   1.56       chs 			n = min(n, bsize - bp->b_resid);
    298   1.56       chs 			if (error) {
    299   1.56       chs 				brelse(bp);
    300   1.56       chs 				return (error);
    301   1.56       chs 			}
    302   1.56       chs 			error = uiomove((char *)bp->b_data + on, n, uio);
    303   1.56       chs 			brelse(bp);
    304   1.56       chs 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    305   1.56       chs 		return (error);
    306   1.56       chs 
    307   1.56       chs 	default:
    308   1.56       chs 		panic("spec_read type");
    309    1.1       cgd 	}
    310   1.56       chs 	/* NOTREACHED */
    311    1.1       cgd }
    312    1.1       cgd 
    313    1.1       cgd /*
    314    1.1       cgd  * Vnode op for write
    315    1.1       cgd  */
    316    1.1       cgd /* ARGSUSED */
    317   1.28  christos int
    318  1.104     pooka spec_write(void *v)
    319   1.28  christos {
    320   1.15   mycroft 	struct vop_write_args /* {
    321   1.15   mycroft 		struct vnode *a_vp;
    322   1.15   mycroft 		struct uio *a_uio;
    323   1.15   mycroft 		int  a_ioflag;
    324   1.87      elad 		kauth_cred_t a_cred;
    325   1.28  christos 	} */ *ap = v;
    326   1.48  augustss 	struct vnode *vp = ap->a_vp;
    327   1.48  augustss 	struct uio *uio = ap->a_uio;
    328   1.86      yamt 	struct lwp *l = curlwp;
    329   1.56       chs 	struct buf *bp;
    330   1.56       chs 	daddr_t bn;
    331   1.59       chs 	int bsize, bscale;
    332   1.56       chs 	struct partinfo dpart;
    333   1.64   gehenna 	int n, on;
    334    1.1       cgd 	int error = 0;
    335    1.1       cgd 
    336    1.1       cgd #ifdef DIAGNOSTIC
    337    1.1       cgd 	if (uio->uio_rw != UIO_WRITE)
    338    1.1       cgd 		panic("spec_write mode");
    339   1.86      yamt 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    340   1.86      yamt 	    uio->uio_vmspace != curproc->p_vmspace)
    341    1.1       cgd 		panic("spec_write proc");
    342    1.1       cgd #endif
    343    1.1       cgd 
    344   1.56       chs 	switch (vp->v_type) {
    345   1.56       chs 
    346   1.56       chs 	case VCHR:
    347   1.39      fvdl 		VOP_UNLOCK(vp, 0);
    348  1.100        ad 		error = cdev_write(vp->v_rdev, uio, ap->a_ioflag);
    349   1.39      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    350    1.1       cgd 		return (error);
    351   1.56       chs 
    352   1.56       chs 	case VBLK:
    353   1.56       chs 		if (uio->uio_resid == 0)
    354   1.56       chs 			return (0);
    355   1.56       chs 		if (uio->uio_offset < 0)
    356   1.56       chs 			return (EINVAL);
    357   1.56       chs 		bsize = BLKDEV_IOSIZE;
    358  1.100        ad 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
    359   1.56       chs 			if (dpart.part->p_fstype == FS_BSDFFS &&
    360   1.56       chs 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    361   1.56       chs 				bsize = dpart.part->p_frag *
    362   1.56       chs 				    dpart.part->p_fsize;
    363   1.56       chs 		}
    364   1.59       chs 		bscale = bsize >> DEV_BSHIFT;
    365   1.56       chs 		do {
    366   1.59       chs 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    367   1.56       chs 			on = uio->uio_offset % bsize;
    368   1.56       chs 			n = min((unsigned)(bsize - on), uio->uio_resid);
    369   1.56       chs 			if (n == bsize)
    370   1.56       chs 				bp = getblk(vp, bn, bsize, 0, 0);
    371   1.56       chs 			else
    372   1.56       chs 				error = bread(vp, bn, bsize, NOCRED, &bp);
    373   1.56       chs 			if (error) {
    374   1.56       chs 				brelse(bp);
    375   1.56       chs 				return (error);
    376   1.56       chs 			}
    377   1.56       chs 			n = min(n, bsize - bp->b_resid);
    378   1.56       chs 			error = uiomove((char *)bp->b_data + on, n, uio);
    379   1.56       chs 			if (error)
    380   1.56       chs 				brelse(bp);
    381   1.56       chs 			else {
    382   1.56       chs 				if (n + on == bsize)
    383   1.56       chs 					bawrite(bp);
    384   1.56       chs 				else
    385   1.56       chs 					bdwrite(bp);
    386  1.103        ad 				if (bp->b_error != 0)
    387   1.56       chs 					error = bp->b_error;
    388   1.56       chs 			}
    389   1.56       chs 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    390   1.56       chs 		return (error);
    391   1.56       chs 
    392   1.56       chs 	default:
    393   1.56       chs 		panic("spec_write type");
    394   1.55       chs 	}
    395   1.56       chs 	/* NOTREACHED */
    396    1.1       cgd }
    397    1.1       cgd 
    398    1.1       cgd /*
    399    1.1       cgd  * Device ioctl operation.
    400    1.1       cgd  */
    401    1.1       cgd /* ARGSUSED */
    402   1.28  christos int
    403  1.104     pooka spec_ioctl(void *v)
    404   1.28  christos {
    405   1.15   mycroft 	struct vop_ioctl_args /* {
    406   1.15   mycroft 		struct vnode *a_vp;
    407   1.19       cgd 		u_long a_command;
    408   1.78       jrf 		void  *a_data;
    409   1.15   mycroft 		int  a_fflag;
    410   1.87      elad 		kauth_cred_t a_cred;
    411   1.85  christos 		struct lwp *a_l;
    412   1.28  christos 	} */ *ap = v;
    413   1.83       chs 	struct vnode *vp;
    414   1.83       chs 	dev_t dev;
    415    1.1       cgd 
    416   1.83       chs 	/*
    417   1.83       chs 	 * Extract all the info we need from the vnode, taking care to
    418   1.83       chs 	 * avoid a race with VOP_REVOKE().
    419   1.83       chs 	 */
    420   1.83       chs 
    421   1.83       chs 	vp = ap->a_vp;
    422   1.83       chs 	dev = NODEV;
    423   1.83       chs 	simple_lock(&vp->v_interlock);
    424   1.83       chs 	if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
    425   1.83       chs 		dev = vp->v_rdev;
    426   1.83       chs 	}
    427   1.83       chs 	simple_unlock(&vp->v_interlock);
    428   1.83       chs 	if (dev == NODEV) {
    429   1.83       chs 		return ENXIO;
    430   1.83       chs 	}
    431   1.83       chs 
    432   1.83       chs 	switch (vp->v_type) {
    433    1.1       cgd 
    434    1.1       cgd 	case VCHR:
    435  1.100        ad 		return cdev_ioctl(dev, ap->a_command, ap->a_data,
    436  1.100        ad 		    ap->a_fflag, ap->a_l);
    437    1.1       cgd 
    438    1.1       cgd 	case VBLK:
    439   1.42   thorpej 		if (ap->a_command == 0 && (long)ap->a_data == B_TAPE) {
    440  1.100        ad 			if (bdev_type(dev) == D_TAPE)
    441    1.1       cgd 				return (0);
    442    1.1       cgd 			else
    443    1.1       cgd 				return (1);
    444   1.42   thorpej 		}
    445  1.100        ad 		return bdev_ioctl(dev, ap->a_command, ap->a_data,
    446  1.100        ad 		   ap->a_fflag, ap->a_l);
    447    1.1       cgd 
    448    1.1       cgd 	default:
    449    1.1       cgd 		panic("spec_ioctl");
    450    1.1       cgd 		/* NOTREACHED */
    451    1.1       cgd 	}
    452    1.1       cgd }
    453    1.1       cgd 
    454    1.1       cgd /* ARGSUSED */
    455   1.28  christos int
    456  1.104     pooka spec_poll(void *v)
    457   1.28  christos {
    458   1.32   mycroft 	struct vop_poll_args /* {
    459   1.15   mycroft 		struct vnode *a_vp;
    460   1.32   mycroft 		int a_events;
    461   1.85  christos 		struct lwp *a_l;
    462   1.28  christos 	} */ *ap = v;
    463   1.91       jld 	struct vnode *vp;
    464   1.48  augustss 	dev_t dev;
    465    1.1       cgd 
    466   1.91       jld 	/*
    467   1.91       jld 	 * Extract all the info we need from the vnode, taking care to
    468   1.91       jld 	 * avoid a race with VOP_REVOKE().
    469   1.91       jld 	 */
    470   1.91       jld 
    471   1.91       jld 	vp = ap->a_vp;
    472   1.91       jld 	dev = NODEV;
    473   1.91       jld 	simple_lock(&vp->v_interlock);
    474   1.91       jld 	if ((vp->v_flag & VXLOCK) == 0 && vp->v_specinfo) {
    475   1.91       jld 		dev = vp->v_rdev;
    476   1.91       jld 	}
    477   1.91       jld 	simple_unlock(&vp->v_interlock);
    478   1.91       jld 	if (dev == NODEV) {
    479   1.92       jld 		return POLLERR;
    480   1.91       jld 	}
    481   1.91       jld 
    482   1.91       jld 	switch (vp->v_type) {
    483    1.1       cgd 
    484    1.1       cgd 	case VCHR:
    485  1.100        ad 		return cdev_poll(dev, ap->a_events, ap->a_l);
    486   1.30   mycroft 
    487   1.30   mycroft 	default:
    488   1.32   mycroft 		return (genfs_poll(v));
    489   1.15   mycroft 	}
    490   1.15   mycroft }
    491   1.65  jdolecek 
    492   1.65  jdolecek /* ARGSUSED */
    493   1.65  jdolecek int
    494  1.104     pooka spec_kqfilter(void *v)
    495   1.65  jdolecek {
    496   1.65  jdolecek 	struct vop_kqfilter_args /* {
    497   1.65  jdolecek 		struct vnode	*a_vp;
    498   1.65  jdolecek 		struct proc	*a_kn;
    499   1.65  jdolecek 	} */ *ap = v;
    500   1.65  jdolecek 	dev_t dev;
    501   1.65  jdolecek 
    502   1.65  jdolecek 	switch (ap->a_vp->v_type) {
    503   1.65  jdolecek 
    504   1.65  jdolecek 	case VCHR:
    505   1.65  jdolecek 		dev = ap->a_vp->v_rdev;
    506  1.100        ad 		return cdev_kqfilter(dev, ap->a_kn);
    507   1.65  jdolecek 	default:
    508   1.65  jdolecek 		/*
    509   1.65  jdolecek 		 * Block devices don't support kqfilter, and refuse it
    510   1.65  jdolecek 		 * for any other files (like those vflush()ed) too.
    511   1.65  jdolecek 		 */
    512   1.65  jdolecek 		return (EOPNOTSUPP);
    513   1.65  jdolecek 	}
    514   1.65  jdolecek }
    515   1.65  jdolecek 
    516   1.15   mycroft /*
    517  1.101     pooka  * Allow mapping of only D_DISK.  This is called only for VBLK.
    518  1.101     pooka  */
    519  1.101     pooka int
    520  1.104     pooka spec_mmap(void *v)
    521  1.101     pooka {
    522  1.101     pooka 	struct vop_mmap_args /* {
    523  1.101     pooka 		struct vnode *a_vp;
    524  1.102     pooka 		vm_prot_t a_prot;
    525  1.101     pooka 		kauth_cred_t a_cred;
    526  1.101     pooka 		struct lwp *a_l;
    527  1.101     pooka 	} */ *ap = v;
    528  1.101     pooka 	struct vnode *vp = ap->a_vp;
    529  1.101     pooka 
    530  1.101     pooka 	KASSERT(vp->v_type == VBLK);
    531  1.101     pooka 	if (bdev_type(vp->v_rdev) != D_DISK)
    532  1.101     pooka 		return EINVAL;
    533  1.101     pooka 
    534  1.101     pooka 	return 0;
    535  1.101     pooka }
    536  1.101     pooka 
    537  1.101     pooka /*
    538   1.15   mycroft  * Synch buffers associated with a block device
    539   1.15   mycroft  */
    540   1.15   mycroft /* ARGSUSED */
    541   1.15   mycroft int
    542  1.104     pooka spec_fsync(void *v)
    543   1.28  christos {
    544   1.15   mycroft 	struct vop_fsync_args /* {
    545   1.15   mycroft 		struct vnode *a_vp;
    546   1.87      elad 		kauth_cred_t a_cred;
    547   1.40    kleink 		int  a_flags;
    548   1.50      fvdl 		off_t offlo;
    549   1.50      fvdl 		off_t offhi;
    550   1.85  christos 		struct lwp *a_l;
    551   1.28  christos 	} */ *ap = v;
    552   1.48  augustss 	struct vnode *vp = ap->a_vp;
    553   1.15   mycroft 
    554   1.30   mycroft 	if (vp->v_type == VBLK)
    555   1.40    kleink 		vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
    556   1.15   mycroft 	return (0);
    557    1.1       cgd }
    558    1.1       cgd 
    559    1.1       cgd /*
    560    1.1       cgd  * Just call the device strategy routine
    561    1.1       cgd  */
    562   1.28  christos int
    563  1.104     pooka spec_strategy(void *v)
    564   1.28  christos {
    565   1.15   mycroft 	struct vop_strategy_args /* {
    566   1.76   hannken 		struct vnode *a_vp;
    567   1.15   mycroft 		struct buf *a_bp;
    568   1.28  christos 	} */ *ap = v;
    569   1.76   hannken 	struct vnode *vp = ap->a_vp;
    570   1.76   hannken 	struct buf *bp = ap->a_bp;
    571  1.106   hannken 	int error;
    572    1.1       cgd 
    573   1.79   hannken 	error = 0;
    574   1.76   hannken 	bp->b_dev = vp->v_rdev;
    575   1.45      fvdl 	if (!(bp->b_flags & B_READ) &&
    576  1.105     pooka 	    (LIST_FIRST(&bp->b_dep)) != NULL && bioopsp)
    577  1.105     pooka 		bioopsp->io_start(bp);
    578   1.77   hannken 
    579  1.106   hannken 	if (!(bp->b_flags & B_READ))
    580  1.106   hannken 		error = fscow_run(bp);
    581   1.77   hannken 
    582   1.79   hannken 	if (error) {
    583   1.79   hannken 		bp->b_error = error;
    584   1.79   hannken 		biodone(bp);
    585   1.79   hannken 		return (error);
    586   1.79   hannken 	}
    587   1.79   hannken 
    588  1.100        ad 	bdev_strategy(bp);
    589   1.76   hannken 
    590    1.1       cgd 	return (0);
    591    1.1       cgd }
    592    1.1       cgd 
    593   1.39      fvdl int
    594  1.104     pooka spec_inactive(void *v)
    595   1.39      fvdl {
    596   1.39      fvdl 	struct vop_inactive_args /* {
    597   1.39      fvdl 		struct vnode *a_vp;
    598   1.85  christos 		struct proc *a_l;
    599   1.39      fvdl 	} */ *ap = v;
    600   1.39      fvdl 
    601   1.39      fvdl 	VOP_UNLOCK(ap->a_vp, 0);
    602   1.39      fvdl 	return (0);
    603   1.39      fvdl }
    604   1.39      fvdl 
    605    1.1       cgd /*
    606    1.1       cgd  * This is a noop, simply returning what one has been given.
    607    1.1       cgd  */
    608   1.28  christos int
    609  1.104     pooka spec_bmap(void *v)
    610   1.28  christos {
    611   1.15   mycroft 	struct vop_bmap_args /* {
    612   1.15   mycroft 		struct vnode *a_vp;
    613   1.15   mycroft 		daddr_t  a_bn;
    614   1.15   mycroft 		struct vnode **a_vpp;
    615   1.15   mycroft 		daddr_t *a_bnp;
    616   1.39      fvdl 		int *a_runp;
    617   1.28  christos 	} */ *ap = v;
    618    1.1       cgd 
    619   1.15   mycroft 	if (ap->a_vpp != NULL)
    620   1.15   mycroft 		*ap->a_vpp = ap->a_vp;
    621   1.15   mycroft 	if (ap->a_bnp != NULL)
    622   1.15   mycroft 		*ap->a_bnp = ap->a_bn;
    623   1.39      fvdl 	if (ap->a_runp != NULL)
    624   1.55       chs 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
    625    1.1       cgd 	return (0);
    626    1.1       cgd }
    627    1.1       cgd 
    628    1.1       cgd /*
    629    1.1       cgd  * Device close routine
    630    1.1       cgd  */
    631    1.1       cgd /* ARGSUSED */
    632   1.28  christos int
    633  1.104     pooka spec_close(void *v)
    634   1.28  christos {
    635   1.15   mycroft 	struct vop_close_args /* {
    636   1.15   mycroft 		struct vnode *a_vp;
    637   1.15   mycroft 		int  a_fflag;
    638   1.87      elad 		kauth_cred_t a_cred;
    639   1.85  christos 		struct lwp *a_l;
    640   1.28  christos 	} */ *ap = v;
    641   1.48  augustss 	struct vnode *vp = ap->a_vp;
    642   1.71       dsl 	struct session *sess;
    643    1.1       cgd 	dev_t dev = vp->v_rdev;
    644   1.44  wrstuden 	int mode, error, count, flags, flags1;
    645   1.44  wrstuden 
    646   1.54   thorpej 	count = vcount(vp);
    647   1.44  wrstuden 	flags = vp->v_flag;
    648    1.1       cgd 
    649    1.1       cgd 	switch (vp->v_type) {
    650    1.1       cgd 
    651    1.1       cgd 	case VCHR:
    652   1.11       cgd 		/*
    653   1.11       cgd 		 * Hack: a tty device that is a controlling terminal
    654   1.11       cgd 		 * has a reference from the session structure.
    655   1.11       cgd 		 * We cannot easily tell that a character device is
    656   1.11       cgd 		 * a controlling terminal, unless it is the closing
    657   1.11       cgd 		 * process' controlling terminal.  In that case,
    658   1.11       cgd 		 * if the reference count is 2 (this last descriptor
    659   1.11       cgd 		 * plus the session), release the reference from the session.
    660   1.71       dsl 		 * Also remove the link from the tty back to the session
    661   1.71       dsl 		 * and pgrp - due to the way consoles are handled we cannot
    662   1.71       dsl 		 * guarantee that the vrele() will do the final close on the
    663   1.71       dsl 		 * actual tty device.
    664   1.11       cgd 		 */
    665  1.100        ad 		mutex_enter(&proclist_lock);
    666   1.85  christos 		if (count == 2 && ap->a_l &&
    667   1.85  christos 		    vp == (sess = ap->a_l->l_proc->p_session)->s_ttyvp) {
    668   1.71       dsl 			sess->s_ttyvp = NULL;
    669   1.72        pk 			if (sess->s_ttyp->t_session != NULL) {
    670   1.72        pk 				sess->s_ttyp->t_pgrp = NULL;
    671   1.72        pk 				sess->s_ttyp->t_session = NULL;
    672  1.100        ad 				mutex_exit(&proclist_lock);
    673   1.72        pk 				SESSRELE(sess);
    674  1.100        ad 			} else {
    675  1.100        ad 				if (sess->s_ttyp->t_pgrp != NULL)
    676  1.100        ad 					panic("spec_close: spurious pgrp ref");
    677  1.100        ad 				mutex_exit(&proclist_lock);
    678  1.100        ad 			}
    679   1.11       cgd 			vrele(vp);
    680   1.44  wrstuden 			count--;
    681  1.100        ad 		} else
    682  1.100        ad 			mutex_exit(&proclist_lock);
    683  1.100        ad 
    684    1.1       cgd 		/*
    685    1.1       cgd 		 * If the vnode is locked, then we are in the midst
    686    1.1       cgd 		 * of forcably closing the device, otherwise we only
    687    1.1       cgd 		 * close on last reference.
    688    1.1       cgd 		 */
    689   1.44  wrstuden 		if (count > 1 && (flags & VXLOCK) == 0)
    690    1.1       cgd 			return (0);
    691    1.1       cgd 		mode = S_IFCHR;
    692    1.1       cgd 		break;
    693    1.1       cgd 
    694    1.1       cgd 	case VBLK:
    695    1.1       cgd 		/*
    696    1.1       cgd 		 * On last close of a block device (that isn't mounted)
    697    1.1       cgd 		 * we must invalidate any in core blocks, so that
    698    1.1       cgd 		 * we can, for instance, change floppy disks.
    699    1.1       cgd 		 */
    700   1.85  christos 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_l, 0, 0);
    701   1.28  christos 		if (error)
    702   1.15   mycroft 			return (error);
    703    1.1       cgd 		/*
    704    1.1       cgd 		 * We do not want to really close the device if it
    705    1.1       cgd 		 * is still in use unless we are trying to close it
    706    1.1       cgd 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
    707    1.1       cgd 		 * holds a reference to the vnode, and because we mark
    708    1.1       cgd 		 * any other vnodes that alias this device, when the
    709    1.1       cgd 		 * sum of the reference counts on all the aliased
    710    1.1       cgd 		 * vnodes descends to one, we are on last close.
    711    1.1       cgd 		 */
    712   1.44  wrstuden 		if (count > 1 && (flags & VXLOCK) == 0)
    713    1.1       cgd 			return (0);
    714    1.1       cgd 		mode = S_IFBLK;
    715    1.1       cgd 		break;
    716    1.5       cgd 
    717    1.1       cgd 	default:
    718    1.1       cgd 		panic("spec_close: not special");
    719    1.1       cgd 	}
    720    1.1       cgd 
    721   1.44  wrstuden 	flags1 = ap->a_fflag;
    722   1.44  wrstuden 
    723   1.44  wrstuden 	/*
    724   1.44  wrstuden 	 * if VXLOCK is set, then we're going away soon, so make this
    725   1.44  wrstuden 	 * non-blocking. Also ensures that we won't wedge in vn_lock below.
    726   1.44  wrstuden 	 */
    727   1.44  wrstuden 	if (flags & VXLOCK)
    728   1.44  wrstuden 		flags1 |= FNONBLOCK;
    729   1.44  wrstuden 
    730   1.44  wrstuden 	/*
    731   1.62       wiz 	 * If we're able to block, release the vnode lock & reacquire. We
    732   1.72        pk 	 * might end up sleeping for someone else who wants our queues. They
    733  1.100        ad 	 * won't get them if we hold the vnode locked. Also, if VXLOCK is
    734  1.100        ad 	 * set, don't release the lock as we won't be able to regain it.
    735   1.44  wrstuden 	 */
    736   1.44  wrstuden 	if (!(flags1 & FNONBLOCK))
    737   1.44  wrstuden 		VOP_UNLOCK(vp, 0);
    738   1.44  wrstuden 
    739  1.100        ad 	if (vp->v_type == VBLK)
    740  1.100        ad 		error = bdev_close(dev, flags1, mode, ap->a_l);
    741   1.64   gehenna 	else
    742  1.100        ad 		error = cdev_close(dev, flags1, mode, ap->a_l);
    743   1.44  wrstuden 
    744   1.44  wrstuden 	if (!(flags1 & FNONBLOCK))
    745   1.44  wrstuden 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    746   1.44  wrstuden 
    747   1.44  wrstuden 	return (error);
    748    1.1       cgd }
    749    1.1       cgd 
    750    1.1       cgd /*
    751    1.1       cgd  * Print out the contents of a special device vnode.
    752    1.1       cgd  */
    753   1.28  christos int
    754  1.104     pooka spec_print(void *v)
    755   1.28  christos {
    756   1.15   mycroft 	struct vop_print_args /* {
    757   1.15   mycroft 		struct vnode *a_vp;
    758   1.28  christos 	} */ *ap = v;
    759   1.15   mycroft 
    760   1.34  christos 	printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
    761   1.33  christos 	    minor(ap->a_vp->v_rdev));
    762   1.28  christos 	return 0;
    763   1.15   mycroft }
    764   1.15   mycroft 
    765   1.15   mycroft /*
    766   1.15   mycroft  * Return POSIX pathconf information applicable to special devices.
    767   1.15   mycroft  */
    768   1.28  christos int
    769  1.104     pooka spec_pathconf(void *v)
    770   1.28  christos {
    771   1.15   mycroft 	struct vop_pathconf_args /* {
    772   1.15   mycroft 		struct vnode *a_vp;
    773   1.15   mycroft 		int a_name;
    774   1.18       cgd 		register_t *a_retval;
    775   1.28  christos 	} */ *ap = v;
    776    1.1       cgd 
    777   1.15   mycroft 	switch (ap->a_name) {
    778   1.15   mycroft 	case _PC_LINK_MAX:
    779   1.15   mycroft 		*ap->a_retval = LINK_MAX;
    780   1.15   mycroft 		return (0);
    781   1.15   mycroft 	case _PC_MAX_CANON:
    782   1.15   mycroft 		*ap->a_retval = MAX_CANON;
    783   1.15   mycroft 		return (0);
    784   1.15   mycroft 	case _PC_MAX_INPUT:
    785   1.15   mycroft 		*ap->a_retval = MAX_INPUT;
    786   1.15   mycroft 		return (0);
    787   1.15   mycroft 	case _PC_PIPE_BUF:
    788   1.15   mycroft 		*ap->a_retval = PIPE_BUF;
    789   1.15   mycroft 		return (0);
    790   1.15   mycroft 	case _PC_CHOWN_RESTRICTED:
    791   1.15   mycroft 		*ap->a_retval = 1;
    792   1.15   mycroft 		return (0);
    793   1.15   mycroft 	case _PC_VDISABLE:
    794   1.15   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
    795   1.41    kleink 		return (0);
    796   1.41    kleink 	case _PC_SYNC_IO:
    797   1.41    kleink 		*ap->a_retval = 1;
    798   1.15   mycroft 		return (0);
    799   1.15   mycroft 	default:
    800   1.15   mycroft 		return (EINVAL);
    801   1.15   mycroft 	}
    802    1.1       cgd 	/* NOTREACHED */
    803   1.35    kleink }
    804   1.35    kleink 
    805   1.80     perry /*
    806   1.35    kleink  * Advisory record locking support.
    807   1.35    kleink  */
    808   1.35    kleink int
    809  1.104     pooka spec_advlock(void *v)
    810   1.35    kleink {
    811   1.35    kleink 	struct vop_advlock_args /* {
    812   1.35    kleink 		struct vnode *a_vp;
    813   1.78       jrf 		void *a_id;
    814   1.35    kleink 		int a_op;
    815   1.35    kleink 		struct flock *a_fl;
    816   1.35    kleink 		int a_flags;
    817   1.35    kleink 	} */ *ap = v;
    818   1.48  augustss 	struct vnode *vp = ap->a_vp;
    819   1.35    kleink 
    820   1.49  jdolecek 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
    821    1.1       cgd }
    822