Home | History | Annotate | Line # | Download | only in specfs
spec_vnops.c revision 1.143
      1  1.143   hannken /*	$NetBSD: spec_vnops.c,v 1.143 2014/03/24 13:42:40 hannken Exp $	*/
      2  1.112        ad 
      3  1.112        ad /*-
      4  1.112        ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.112        ad  * All rights reserved.
      6  1.112        ad  *
      7  1.112        ad  * Redistribution and use in source and binary forms, with or without
      8  1.112        ad  * modification, are permitted provided that the following conditions
      9  1.112        ad  * are met:
     10  1.112        ad  * 1. Redistributions of source code must retain the above copyright
     11  1.112        ad  *    notice, this list of conditions and the following disclaimer.
     12  1.112        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.112        ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.112        ad  *    documentation and/or other materials provided with the distribution.
     15  1.112        ad  *
     16  1.112        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.112        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.112        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.112        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.112        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.112        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.112        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.112        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.112        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.112        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.112        ad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.112        ad  */
     28   1.16       cgd 
     29    1.1       cgd /*
     30   1.15   mycroft  * Copyright (c) 1989, 1993
     31   1.15   mycroft  *	The Regents of the University of California.  All rights reserved.
     32    1.1       cgd  *
     33    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     34    1.1       cgd  * modification, are permitted provided that the following conditions
     35    1.1       cgd  * are met:
     36    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     37    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     38    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     39    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     40    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     41   1.69       agc  * 3. Neither the name of the University nor the names of its contributors
     42    1.1       cgd  *    may be used to endorse or promote products derived from this software
     43    1.1       cgd  *    without specific prior written permission.
     44    1.1       cgd  *
     45    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     46    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     49    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55    1.1       cgd  * SUCH DAMAGE.
     56    1.1       cgd  *
     57   1.39      fvdl  *	@(#)spec_vnops.c	8.15 (Berkeley) 7/14/95
     58    1.1       cgd  */
     59   1.60     lukem 
     60   1.60     lukem #include <sys/cdefs.h>
     61  1.143   hannken __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.143 2014/03/24 13:42:40 hannken Exp $");
     62    1.1       cgd 
     63    1.9   mycroft #include <sys/param.h>
     64    1.9   mycroft #include <sys/proc.h>
     65    1.9   mycroft #include <sys/systm.h>
     66    1.9   mycroft #include <sys/kernel.h>
     67    1.9   mycroft #include <sys/conf.h>
     68    1.9   mycroft #include <sys/buf.h>
     69    1.9   mycroft #include <sys/mount.h>
     70    1.9   mycroft #include <sys/namei.h>
     71    1.9   mycroft #include <sys/vnode.h>
     72    1.9   mycroft #include <sys/stat.h>
     73    1.9   mycroft #include <sys/errno.h>
     74    1.9   mycroft #include <sys/ioctl.h>
     75   1.81        ws #include <sys/poll.h>
     76    1.9   mycroft #include <sys/file.h>
     77    1.9   mycroft #include <sys/disklabel.h>
     78   1.35    kleink #include <sys/lockf.h>
     79   1.71       dsl #include <sys/tty.h>
     80   1.87      elad #include <sys/kauth.h>
     81  1.106   hannken #include <sys/fstrans.h>
     82  1.122      haad #include <sys/module.h>
     83   1.28  christos 
     84   1.30   mycroft #include <miscfs/genfs/genfs.h>
     85   1.15   mycroft #include <miscfs/specfs/specdev.h>
     86    1.1       cgd 
     87    1.1       cgd /* symbolic sleep message strings for devices */
     88   1.37   mycroft const char	devopn[] = "devopn";
     89   1.37   mycroft const char	devio[] = "devio";
     90   1.37   mycroft const char	devwait[] = "devwait";
     91   1.37   mycroft const char	devin[] = "devin";
     92   1.37   mycroft const char	devout[] = "devout";
     93   1.37   mycroft const char	devioc[] = "devioc";
     94   1.37   mycroft const char	devcls[] = "devcls";
     95   1.61      matt 
     96  1.137   hannken #define	SPECHSZ	64
     97  1.137   hannken #if	((SPECHSZ&(SPECHSZ-1)) == 0)
     98  1.137   hannken #define	SPECHASH(rdev)	(((rdev>>5)+(rdev))&(SPECHSZ-1))
     99  1.137   hannken #else
    100  1.137   hannken #define	SPECHASH(rdev)	(((unsigned)((rdev>>5)+(rdev)))%SPECHSZ)
    101  1.137   hannken #endif
    102  1.137   hannken 
    103  1.137   hannken static vnode_t	*specfs_hash[SPECHSZ];
    104   1.46  sommerfe 
    105   1.46  sommerfe /*
    106  1.112        ad  * This vnode operations vector is used for special device nodes
    107  1.112        ad  * created from whole cloth by the kernel.  For the ops vector for
    108  1.112        ad  * vnodes built from special devices found in a filesystem, see (e.g)
    109  1.112        ad  * ffs_specop_entries[] in ffs_vnops.c or the equivalent for other
    110  1.112        ad  * filesystems.
    111   1.46  sommerfe  */
    112    1.1       cgd 
    113   1.82   xtraeme int (**spec_vnodeop_p)(void *);
    114   1.53  jdolecek const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
    115   1.15   mycroft 	{ &vop_default_desc, vn_default_error },
    116   1.15   mycroft 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    117   1.15   mycroft 	{ &vop_create_desc, spec_create },		/* create */
    118   1.15   mycroft 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    119   1.15   mycroft 	{ &vop_open_desc, spec_open },			/* open */
    120   1.15   mycroft 	{ &vop_close_desc, spec_close },		/* close */
    121   1.15   mycroft 	{ &vop_access_desc, spec_access },		/* access */
    122   1.15   mycroft 	{ &vop_getattr_desc, spec_getattr },		/* getattr */
    123   1.15   mycroft 	{ &vop_setattr_desc, spec_setattr },		/* setattr */
    124   1.15   mycroft 	{ &vop_read_desc, spec_read },			/* read */
    125   1.15   mycroft 	{ &vop_write_desc, spec_write },		/* write */
    126   1.47  sommerfe 	{ &vop_fcntl_desc, spec_fcntl },		/* fcntl */
    127   1.15   mycroft 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    128   1.32   mycroft 	{ &vop_poll_desc, spec_poll },			/* poll */
    129   1.65  jdolecek 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    130   1.39      fvdl 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    131   1.15   mycroft 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    132   1.15   mycroft 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    133   1.15   mycroft 	{ &vop_seek_desc, spec_seek },			/* seek */
    134   1.15   mycroft 	{ &vop_remove_desc, spec_remove },		/* remove */
    135   1.15   mycroft 	{ &vop_link_desc, spec_link },			/* link */
    136   1.15   mycroft 	{ &vop_rename_desc, spec_rename },		/* rename */
    137   1.15   mycroft 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    138   1.15   mycroft 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    139   1.15   mycroft 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    140   1.15   mycroft 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    141   1.15   mycroft 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    142   1.15   mycroft 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    143   1.15   mycroft 	{ &vop_inactive_desc, spec_inactive },		/* inactive */
    144   1.15   mycroft 	{ &vop_reclaim_desc, spec_reclaim },		/* reclaim */
    145   1.15   mycroft 	{ &vop_lock_desc, spec_lock },			/* lock */
    146   1.15   mycroft 	{ &vop_unlock_desc, spec_unlock },		/* unlock */
    147   1.15   mycroft 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    148   1.15   mycroft 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    149   1.15   mycroft 	{ &vop_print_desc, spec_print },		/* print */
    150   1.15   mycroft 	{ &vop_islocked_desc, spec_islocked },		/* islocked */
    151   1.15   mycroft 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    152   1.15   mycroft 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    153   1.28  christos 	{ &vop_bwrite_desc, spec_bwrite },		/* bwrite */
    154   1.55       chs 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    155   1.55       chs 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    156   1.55       chs 	{ NULL, NULL }
    157    1.1       cgd };
    158   1.53  jdolecek const struct vnodeopv_desc spec_vnodeop_opv_desc =
    159   1.15   mycroft 	{ &spec_vnodeop_p, spec_vnodeop_entries };
    160    1.1       cgd 
    161  1.127      elad static kauth_listener_t rawio_listener;
    162  1.127      elad 
    163  1.126      elad /* Returns true if vnode is /dev/mem or /dev/kmem. */
    164  1.126      elad bool
    165  1.126      elad iskmemvp(struct vnode *vp)
    166  1.126      elad {
    167  1.126      elad 	return ((vp->v_type == VCHR) && iskmemdev(vp->v_rdev));
    168  1.126      elad }
    169  1.126      elad 
    170    1.1       cgd /*
    171  1.112        ad  * Returns true if dev is /dev/mem or /dev/kmem.
    172  1.112        ad  */
    173  1.112        ad int
    174  1.112        ad iskmemdev(dev_t dev)
    175  1.112        ad {
    176  1.112        ad 	/* mem_no is emitted by config(8) to generated devsw.c */
    177  1.112        ad 	extern const int mem_no;
    178  1.112        ad 
    179  1.112        ad 	/* minor 14 is /dev/io on i386 with COMPAT_10 */
    180  1.112        ad 	return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
    181  1.112        ad }
    182  1.112        ad 
    183  1.127      elad static int
    184  1.127      elad rawio_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    185  1.127      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    186  1.127      elad {
    187  1.127      elad 	int result;
    188  1.127      elad 
    189  1.127      elad 	result = KAUTH_RESULT_DEFER;
    190  1.127      elad 
    191  1.127      elad 	if ((action != KAUTH_DEVICE_RAWIO_SPEC) &&
    192  1.127      elad 	    (action != KAUTH_DEVICE_RAWIO_PASSTHRU))
    193  1.127      elad 		return result;
    194  1.127      elad 
    195  1.127      elad 	/* Access is mandated by permissions. */
    196  1.127      elad 	result = KAUTH_RESULT_ALLOW;
    197  1.127      elad 
    198  1.127      elad 	return result;
    199  1.127      elad }
    200  1.127      elad 
    201  1.127      elad void
    202  1.127      elad spec_init(void)
    203  1.127      elad {
    204  1.127      elad 
    205  1.127      elad 	rawio_listener = kauth_listen_scope(KAUTH_SCOPE_DEVICE,
    206  1.127      elad 	    rawio_listener_cb, NULL);
    207  1.127      elad }
    208  1.127      elad 
    209  1.112        ad /*
    210  1.112        ad  * Initialize a vnode that represents a device.
    211  1.112        ad  */
    212  1.112        ad void
    213  1.112        ad spec_node_init(vnode_t *vp, dev_t rdev)
    214  1.112        ad {
    215  1.112        ad 	specnode_t *sn;
    216  1.112        ad 	specdev_t *sd;
    217  1.112        ad 	vnode_t *vp2;
    218  1.112        ad 	vnode_t **vpp;
    219  1.112        ad 
    220  1.112        ad 	KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
    221  1.112        ad 	KASSERT(vp->v_specnode == NULL);
    222  1.112        ad 
    223  1.112        ad 	/*
    224  1.112        ad 	 * Search the hash table for this device.  If known, add a
    225  1.112        ad 	 * reference to the device structure.  If not known, create
    226  1.112        ad 	 * a new entry to represent the device.  In all cases add
    227  1.112        ad 	 * the vnode to the hash table.
    228  1.112        ad 	 */
    229  1.112        ad 	sn = kmem_alloc(sizeof(*sn), KM_SLEEP);
    230  1.112        ad 	if (sn == NULL) {
    231  1.112        ad 		/* XXX */
    232  1.112        ad 		panic("spec_node_init: unable to allocate memory");
    233  1.112        ad 	}
    234  1.112        ad 	sd = kmem_alloc(sizeof(*sd), KM_SLEEP);
    235  1.112        ad 	if (sd == NULL) {
    236  1.112        ad 		/* XXX */
    237  1.112        ad 		panic("spec_node_init: unable to allocate memory");
    238  1.112        ad 	}
    239  1.120     pooka 	mutex_enter(&device_lock);
    240  1.112        ad 	vpp = &specfs_hash[SPECHASH(rdev)];
    241  1.112        ad 	for (vp2 = *vpp; vp2 != NULL; vp2 = vp2->v_specnext) {
    242  1.112        ad 		KASSERT(vp2->v_specnode != NULL);
    243  1.112        ad 		if (rdev == vp2->v_rdev && vp->v_type == vp2->v_type) {
    244  1.112        ad 			break;
    245  1.112        ad 		}
    246  1.112        ad 	}
    247  1.112        ad 	if (vp2 == NULL) {
    248  1.112        ad 		/* No existing record, create a new one. */
    249  1.112        ad 		sd->sd_rdev = rdev;
    250  1.112        ad 		sd->sd_mountpoint = NULL;
    251  1.112        ad 		sd->sd_lockf = NULL;
    252  1.112        ad 		sd->sd_refcnt = 1;
    253  1.112        ad 		sd->sd_opencnt = 0;
    254  1.112        ad 		sd->sd_bdevvp = NULL;
    255  1.112        ad 		sn->sn_dev = sd;
    256  1.112        ad 		sd = NULL;
    257  1.112        ad 	} else {
    258  1.112        ad 		/* Use the existing record. */
    259  1.112        ad 		sn->sn_dev = vp2->v_specnode->sn_dev;
    260  1.112        ad 		sn->sn_dev->sd_refcnt++;
    261  1.112        ad 	}
    262  1.112        ad 	/* Insert vnode into the hash chain. */
    263  1.112        ad 	sn->sn_opencnt = 0;
    264  1.112        ad 	sn->sn_rdev = rdev;
    265  1.112        ad 	sn->sn_gone = false;
    266  1.112        ad 	vp->v_specnode = sn;
    267  1.112        ad 	vp->v_specnext = *vpp;
    268  1.112        ad 	*vpp = vp;
    269  1.120     pooka 	mutex_exit(&device_lock);
    270  1.112        ad 
    271  1.112        ad 	/* Free the record we allocated if unused. */
    272  1.112        ad 	if (sd != NULL) {
    273  1.112        ad 		kmem_free(sd, sizeof(*sd));
    274  1.112        ad 	}
    275  1.112        ad }
    276  1.112        ad 
    277  1.112        ad /*
    278  1.137   hannken  * Lookup a vnode by device number and return it referenced.
    279  1.137   hannken  */
    280  1.137   hannken int
    281  1.137   hannken spec_node_lookup_by_dev(enum vtype type, dev_t dev, vnode_t **vpp)
    282  1.137   hannken {
    283  1.137   hannken 	int error;
    284  1.137   hannken 	vnode_t *vp;
    285  1.137   hannken 
    286  1.137   hannken 	mutex_enter(&device_lock);
    287  1.137   hannken 	for (vp = specfs_hash[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
    288  1.137   hannken 		if (type == vp->v_type && dev == vp->v_rdev) {
    289  1.137   hannken 			mutex_enter(vp->v_interlock);
    290  1.137   hannken 			/* If clean or being cleaned, then ignore it. */
    291  1.143   hannken 			if (vdead_check(vp, VDEAD_NOWAIT) == 0)
    292  1.137   hannken 				break;
    293  1.137   hannken 			mutex_exit(vp->v_interlock);
    294  1.137   hannken 		}
    295  1.137   hannken 	}
    296  1.137   hannken 	KASSERT(vp == NULL || mutex_owned(vp->v_interlock));
    297  1.137   hannken 	if (vp == NULL) {
    298  1.137   hannken 		mutex_exit(&device_lock);
    299  1.137   hannken 		return ENOENT;
    300  1.137   hannken 	}
    301  1.137   hannken 	/*
    302  1.137   hannken 	 * If it is an opened block device return the opened vnode.
    303  1.137   hannken 	 */
    304  1.137   hannken 	if (type == VBLK && vp->v_specnode->sn_dev->sd_bdevvp != NULL) {
    305  1.137   hannken 		mutex_exit(vp->v_interlock);
    306  1.137   hannken 		vp = vp->v_specnode->sn_dev->sd_bdevvp;
    307  1.137   hannken 		mutex_enter(vp->v_interlock);
    308  1.137   hannken 	}
    309  1.137   hannken 	mutex_exit(&device_lock);
    310  1.137   hannken 	error = vget(vp, 0);
    311  1.137   hannken 	if (error != 0)
    312  1.137   hannken 		return error;
    313  1.137   hannken 	*vpp = vp;
    314  1.137   hannken 
    315  1.137   hannken 	return 0;
    316  1.137   hannken }
    317  1.137   hannken 
    318  1.137   hannken /*
    319  1.137   hannken  * Lookup a vnode by file system mounted on and return it referenced.
    320  1.137   hannken  */
    321  1.137   hannken int
    322  1.137   hannken spec_node_lookup_by_mount(struct mount *mp, vnode_t **vpp)
    323  1.137   hannken {
    324  1.137   hannken 	int i, error;
    325  1.137   hannken 	vnode_t *vp, *vq;
    326  1.137   hannken 
    327  1.137   hannken 	mutex_enter(&device_lock);
    328  1.137   hannken 	for (i = 0, vq = NULL; i < SPECHSZ && vq == NULL; i++) {
    329  1.137   hannken 		for (vp = specfs_hash[i]; vp; vp = vp->v_specnext) {
    330  1.137   hannken 			if (vp->v_type != VBLK)
    331  1.137   hannken 				continue;
    332  1.137   hannken 			vq = vp->v_specnode->sn_dev->sd_bdevvp;
    333  1.141   hannken 			if (vq != NULL &&
    334  1.141   hannken 			    vq->v_specnode->sn_dev->sd_mountpoint == mp)
    335  1.137   hannken 				break;
    336  1.137   hannken 			vq = NULL;
    337  1.137   hannken 		}
    338  1.137   hannken 	}
    339  1.137   hannken 	if (vq == NULL) {
    340  1.137   hannken 		mutex_exit(&device_lock);
    341  1.137   hannken 		return ENOENT;
    342  1.137   hannken 	}
    343  1.137   hannken 	mutex_enter(vq->v_interlock);
    344  1.137   hannken 	mutex_exit(&device_lock);
    345  1.137   hannken 	error = vget(vq, 0);
    346  1.137   hannken 	if (error != 0)
    347  1.137   hannken 		return error;
    348  1.137   hannken 	*vpp = vq;
    349  1.137   hannken 
    350  1.137   hannken 	return 0;
    351  1.137   hannken 
    352  1.137   hannken }
    353  1.137   hannken 
    354  1.137   hannken /*
    355  1.141   hannken  * Get the file system mounted on this block device.
    356  1.141   hannken  */
    357  1.141   hannken struct mount *
    358  1.141   hannken spec_node_getmountedfs(vnode_t *devvp)
    359  1.141   hannken {
    360  1.141   hannken 	struct mount *mp;
    361  1.141   hannken 
    362  1.141   hannken 	KASSERT(devvp->v_type == VBLK);
    363  1.141   hannken 	mp = devvp->v_specnode->sn_dev->sd_mountpoint;
    364  1.141   hannken 
    365  1.141   hannken 	return mp;
    366  1.141   hannken }
    367  1.141   hannken 
    368  1.141   hannken /*
    369  1.141   hannken  * Set the file system mounted on this block device.
    370  1.141   hannken  */
    371  1.141   hannken void
    372  1.141   hannken spec_node_setmountedfs(vnode_t *devvp, struct mount *mp)
    373  1.141   hannken {
    374  1.141   hannken 
    375  1.141   hannken 	KASSERT(devvp->v_type == VBLK);
    376  1.141   hannken 	KASSERT(devvp->v_specnode->sn_dev->sd_mountpoint == NULL || mp == NULL);
    377  1.141   hannken 	devvp->v_specnode->sn_dev->sd_mountpoint = mp;
    378  1.141   hannken }
    379  1.141   hannken 
    380  1.141   hannken /*
    381  1.112        ad  * A vnode representing a special device is going away.  Close
    382  1.112        ad  * the device if the vnode holds it open.
    383  1.112        ad  */
    384  1.112        ad void
    385  1.112        ad spec_node_revoke(vnode_t *vp)
    386  1.112        ad {
    387  1.112        ad 	specnode_t *sn;
    388  1.112        ad 	specdev_t *sd;
    389  1.112        ad 
    390  1.112        ad 	sn = vp->v_specnode;
    391  1.112        ad 	sd = sn->sn_dev;
    392  1.112        ad 
    393  1.112        ad 	KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
    394  1.112        ad 	KASSERT(vp->v_specnode != NULL);
    395  1.112        ad 	KASSERT(sn->sn_gone == false);
    396  1.112        ad 
    397  1.120     pooka 	mutex_enter(&device_lock);
    398  1.112        ad 	KASSERT(sn->sn_opencnt <= sd->sd_opencnt);
    399  1.112        ad 	if (sn->sn_opencnt != 0) {
    400  1.112        ad 		sd->sd_opencnt -= (sn->sn_opencnt - 1);
    401  1.112        ad 		sn->sn_opencnt = 1;
    402  1.112        ad 		sn->sn_gone = true;
    403  1.120     pooka 		mutex_exit(&device_lock);
    404  1.112        ad 
    405  1.112        ad 		VOP_CLOSE(vp, FNONBLOCK, NOCRED);
    406  1.112        ad 
    407  1.120     pooka 		mutex_enter(&device_lock);
    408  1.112        ad 		KASSERT(sn->sn_opencnt == 0);
    409  1.112        ad 	}
    410  1.120     pooka 	mutex_exit(&device_lock);
    411  1.112        ad }
    412  1.112        ad 
    413  1.112        ad /*
    414  1.112        ad  * A vnode representing a special device is being recycled.
    415  1.112        ad  * Destroy the specfs component.
    416  1.112        ad  */
    417  1.112        ad void
    418  1.112        ad spec_node_destroy(vnode_t *vp)
    419  1.112        ad {
    420  1.112        ad 	specnode_t *sn;
    421  1.112        ad 	specdev_t *sd;
    422  1.112        ad 	vnode_t **vpp, *vp2;
    423  1.112        ad 	int refcnt;
    424  1.112        ad 
    425  1.112        ad 	sn = vp->v_specnode;
    426  1.112        ad 	sd = sn->sn_dev;
    427  1.112        ad 
    428  1.112        ad 	KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
    429  1.112        ad 	KASSERT(vp->v_specnode != NULL);
    430  1.112        ad 	KASSERT(sn->sn_opencnt == 0);
    431  1.112        ad 
    432  1.120     pooka 	mutex_enter(&device_lock);
    433  1.112        ad 	/* Remove from the hash and destroy the node. */
    434  1.112        ad 	vpp = &specfs_hash[SPECHASH(vp->v_rdev)];
    435  1.112        ad 	for (vp2 = *vpp;; vp2 = vp2->v_specnext) {
    436  1.112        ad 		if (vp2 == NULL) {
    437  1.112        ad 			panic("spec_node_destroy: corrupt hash");
    438  1.112        ad 		}
    439  1.112        ad 		if (vp2 == vp) {
    440  1.112        ad 			KASSERT(vp == *vpp);
    441  1.112        ad 			*vpp = vp->v_specnext;
    442  1.112        ad 			break;
    443  1.112        ad 		}
    444  1.112        ad 		if (vp2->v_specnext == vp) {
    445  1.112        ad 			vp2->v_specnext = vp->v_specnext;
    446  1.112        ad 			break;
    447  1.112        ad 		}
    448  1.112        ad 	}
    449  1.112        ad 	sn = vp->v_specnode;
    450  1.112        ad 	vp->v_specnode = NULL;
    451  1.112        ad 	refcnt = sd->sd_refcnt--;
    452  1.112        ad 	KASSERT(refcnt > 0);
    453  1.120     pooka 	mutex_exit(&device_lock);
    454  1.112        ad 
    455  1.112        ad 	/* If the device is no longer in use, destroy our record. */
    456  1.112        ad 	if (refcnt == 1) {
    457  1.112        ad 		KASSERT(sd->sd_opencnt == 0);
    458  1.112        ad 		KASSERT(sd->sd_bdevvp == NULL);
    459  1.112        ad 		kmem_free(sd, sizeof(*sd));
    460  1.112        ad 	}
    461  1.112        ad 	kmem_free(sn, sizeof(*sn));
    462  1.112        ad }
    463  1.112        ad 
    464  1.112        ad /*
    465    1.1       cgd  * Trivial lookup routine that always fails.
    466    1.1       cgd  */
    467    1.4    andrew int
    468  1.104     pooka spec_lookup(void *v)
    469   1.28  christos {
    470  1.142   hannken 	struct vop_lookup_v2_args /* {
    471   1.15   mycroft 		struct vnode *a_dvp;
    472   1.15   mycroft 		struct vnode **a_vpp;
    473   1.15   mycroft 		struct componentname *a_cnp;
    474   1.28  christos 	} */ *ap = v;
    475    1.1       cgd 
    476   1.15   mycroft 	*ap->a_vpp = NULL;
    477    1.1       cgd 	return (ENOTDIR);
    478   1.66  jdolecek }
    479   1.66  jdolecek 
    480   1.66  jdolecek /*
    481   1.15   mycroft  * Open a special file.
    482    1.1       cgd  */
    483    1.1       cgd /* ARGSUSED */
    484   1.28  christos int
    485  1.104     pooka spec_open(void *v)
    486   1.28  christos {
    487   1.15   mycroft 	struct vop_open_args /* {
    488   1.15   mycroft 		struct vnode *a_vp;
    489   1.15   mycroft 		int  a_mode;
    490   1.87      elad 		kauth_cred_t a_cred;
    491   1.28  christos 	} */ *ap = v;
    492  1.112        ad 	struct lwp *l;
    493  1.112        ad 	struct vnode *vp;
    494  1.112        ad 	dev_t dev;
    495    1.1       cgd 	int error;
    496   1.55       chs 	struct partinfo pi;
    497   1.96      elad 	enum kauth_device_req req;
    498  1.112        ad 	specnode_t *sn;
    499  1.112        ad 	specdev_t *sd;
    500  1.112        ad 
    501  1.122      haad 	u_int gen;
    502  1.122      haad 	const char *name;
    503  1.122      haad 
    504  1.112        ad 	l = curlwp;
    505  1.112        ad 	vp = ap->a_vp;
    506  1.112        ad 	dev = vp->v_rdev;
    507  1.112        ad 	sn = vp->v_specnode;
    508  1.112        ad 	sd = sn->sn_dev;
    509  1.122      haad 	name = NULL;
    510  1.122      haad 	gen = 0;
    511  1.122      haad 
    512   1.15   mycroft 	/*
    513   1.15   mycroft 	 * Don't allow open if fs is mounted -nodev.
    514   1.15   mycroft 	 */
    515    1.1       cgd 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
    516    1.1       cgd 		return (ENXIO);
    517    1.1       cgd 
    518  1.112        ad 	switch (ap->a_mode & (FREAD | FWRITE)) {
    519  1.112        ad 	case FREAD | FWRITE:
    520  1.112        ad 		req = KAUTH_REQ_DEVICE_RAWIO_SPEC_RW;
    521  1.112        ad 		break;
    522  1.112        ad 	case FWRITE:
    523  1.112        ad 		req = KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE;
    524  1.112        ad 		break;
    525  1.112        ad 	default:
    526  1.112        ad 		req = KAUTH_REQ_DEVICE_RAWIO_SPEC_READ;
    527  1.112        ad 		break;
    528  1.112        ad 	}
    529   1.89      elad 
    530    1.1       cgd 	switch (vp->v_type) {
    531    1.1       cgd 	case VCHR:
    532   1.96      elad 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    533  1.112        ad 		if (error != 0)
    534   1.96      elad 			return (error);
    535   1.89      elad 
    536  1.112        ad 		/*
    537  1.112        ad 		 * Character devices can accept opens from multiple
    538  1.112        ad 		 * vnodes.
    539  1.112        ad 		 */
    540  1.120     pooka 		mutex_enter(&device_lock);
    541  1.112        ad 		if (sn->sn_gone) {
    542  1.120     pooka 			mutex_exit(&device_lock);
    543  1.112        ad 			return (EBADF);
    544  1.112        ad 		}
    545  1.112        ad 		sd->sd_opencnt++;
    546  1.112        ad 		sn->sn_opencnt++;
    547  1.120     pooka 		mutex_exit(&device_lock);
    548  1.100        ad 		if (cdev_type(dev) == D_TTY)
    549  1.108        ad 			vp->v_vflag |= VV_ISTTY;
    550  1.130   hannken 		VOP_UNLOCK(vp);
    551  1.122      haad 		do {
    552  1.125   tsutsui 			const struct cdevsw *cdev;
    553  1.125   tsutsui 
    554  1.122      haad 			gen = module_gen;
    555  1.122      haad 			error = cdev_open(dev, ap->a_mode, S_IFCHR, l);
    556  1.122      haad 			if (error != ENXIO)
    557  1.122      haad 				break;
    558  1.122      haad 
    559  1.125   tsutsui 			/* Check if we already have a valid driver */
    560  1.125   tsutsui 			mutex_enter(&device_lock);
    561  1.125   tsutsui 			cdev = cdevsw_lookup(dev);
    562  1.125   tsutsui 			mutex_exit(&device_lock);
    563  1.125   tsutsui 			if (cdev != NULL)
    564  1.125   tsutsui 				break;
    565  1.125   tsutsui 
    566  1.122      haad 			/* Get device name from devsw_conv array */
    567  1.122      haad 			if ((name = cdevsw_getname(major(dev))) == NULL)
    568  1.122      haad 				break;
    569  1.122      haad 
    570  1.122      haad 			/* Try to autoload device module */
    571  1.129     ahoka 			(void) module_autoload(name, MODULE_CLASS_DRIVER);
    572  1.122      haad 		} while (gen != module_gen);
    573  1.122      haad 
    574   1.39      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    575   1.70       dsl 		break;
    576    1.1       cgd 
    577    1.1       cgd 	case VBLK:
    578   1.96      elad 		error = kauth_authorize_device_spec(ap->a_cred, req, vp);
    579  1.112        ad 		if (error != 0)
    580   1.96      elad 			return (error);
    581  1.112        ad 
    582  1.112        ad 		/*
    583  1.112        ad 		 * For block devices, permit only one open.  The buffer
    584  1.112        ad 		 * cache cannot remain self-consistent with multiple
    585  1.112        ad 		 * vnodes holding a block device open.
    586  1.112        ad 		 */
    587  1.120     pooka 		mutex_enter(&device_lock);
    588  1.112        ad 		if (sn->sn_gone) {
    589  1.120     pooka 			mutex_exit(&device_lock);
    590  1.112        ad 			return (EBADF);
    591  1.112        ad 		}
    592  1.112        ad 		if (sd->sd_opencnt != 0) {
    593  1.120     pooka 			mutex_exit(&device_lock);
    594  1.112        ad 			return EBUSY;
    595  1.112        ad 		}
    596  1.112        ad 		sn->sn_opencnt = 1;
    597  1.112        ad 		sd->sd_opencnt = 1;
    598  1.112        ad 		sd->sd_bdevvp = vp;
    599  1.120     pooka 		mutex_exit(&device_lock);
    600  1.122      haad 		do {
    601  1.125   tsutsui 			const struct bdevsw *bdev;
    602  1.125   tsutsui 
    603  1.122      haad 			gen = module_gen;
    604  1.122      haad 			error = bdev_open(dev, ap->a_mode, S_IFBLK, l);
    605  1.122      haad 			if (error != ENXIO)
    606  1.122      haad 				break;
    607  1.122      haad 
    608  1.125   tsutsui 			/* Check if we already have a valid driver */
    609  1.125   tsutsui 			mutex_enter(&device_lock);
    610  1.125   tsutsui 			bdev = bdevsw_lookup(dev);
    611  1.125   tsutsui 			mutex_exit(&device_lock);
    612  1.125   tsutsui 			if (bdev != NULL)
    613  1.125   tsutsui 				break;
    614  1.125   tsutsui 
    615  1.122      haad 			/* Get device name from devsw_conv array */
    616  1.122      haad 			if ((name = bdevsw_getname(major(dev))) == NULL)
    617  1.122      haad 				break;
    618  1.122      haad 
    619  1.130   hannken 			VOP_UNLOCK(vp);
    620  1.122      haad 
    621  1.122      haad                         /* Try to autoload device module */
    622  1.122      haad 			(void) module_autoload(name, MODULE_CLASS_DRIVER);
    623  1.122      haad 
    624  1.122      haad 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    625  1.122      haad 		} while (gen != module_gen);
    626  1.112        ad 
    627   1.70       dsl 		break;
    628   1.55       chs 
    629   1.28  christos 	case VNON:
    630   1.28  christos 	case VLNK:
    631   1.28  christos 	case VDIR:
    632   1.28  christos 	case VREG:
    633   1.28  christos 	case VBAD:
    634   1.28  christos 	case VFIFO:
    635   1.28  christos 	case VSOCK:
    636   1.70       dsl 	default:
    637   1.70       dsl 		return 0;
    638    1.1       cgd 	}
    639   1.70       dsl 
    640  1.120     pooka 	mutex_enter(&device_lock);
    641  1.112        ad 	if (sn->sn_gone) {
    642  1.112        ad 		if (error == 0)
    643  1.112        ad 			error = EBADF;
    644  1.112        ad 	} else if (error != 0) {
    645  1.112        ad 		sd->sd_opencnt--;
    646  1.112        ad 		sn->sn_opencnt--;
    647  1.115   hannken 		if (vp->v_type == VBLK)
    648  1.115   hannken 			sd->sd_bdevvp = NULL;
    649  1.115   hannken 
    650  1.112        ad 	}
    651  1.120     pooka 	mutex_exit(&device_lock);
    652   1.89      elad 
    653  1.112        ad 	if (cdev_type(dev) != D_DISK || error != 0)
    654   1.70       dsl 		return error;
    655  1.112        ad 
    656  1.100        ad 	if (vp->v_type == VCHR)
    657  1.100        ad 		error = cdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
    658  1.100        ad 	else
    659  1.100        ad 		error = bdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
    660  1.100        ad 	if (error == 0)
    661   1.99      yamt 		uvm_vnp_setsize(vp,
    662   1.99      yamt 		    (voff_t)pi.disklab->d_secsize * pi.part->p_size);
    663   1.70       dsl 	return 0;
    664    1.1       cgd }
    665    1.1       cgd 
    666    1.1       cgd /*
    667    1.1       cgd  * Vnode op for read
    668    1.1       cgd  */
    669    1.1       cgd /* ARGSUSED */
    670   1.28  christos int
    671  1.104     pooka spec_read(void *v)
    672   1.28  christos {
    673   1.15   mycroft 	struct vop_read_args /* {
    674   1.15   mycroft 		struct vnode *a_vp;
    675   1.15   mycroft 		struct uio *a_uio;
    676   1.15   mycroft 		int  a_ioflag;
    677   1.87      elad 		kauth_cred_t a_cred;
    678   1.28  christos 	} */ *ap = v;
    679   1.48  augustss 	struct vnode *vp = ap->a_vp;
    680   1.48  augustss 	struct uio *uio = ap->a_uio;
    681   1.86      yamt  	struct lwp *l = curlwp;
    682   1.56       chs 	struct buf *bp;
    683   1.57       chs 	daddr_t bn;
    684   1.59       chs 	int bsize, bscale;
    685   1.56       chs 	struct partinfo dpart;
    686   1.64   gehenna 	int n, on;
    687    1.1       cgd 	int error = 0;
    688    1.1       cgd 
    689    1.1       cgd #ifdef DIAGNOSTIC
    690    1.1       cgd 	if (uio->uio_rw != UIO_READ)
    691    1.1       cgd 		panic("spec_read mode");
    692   1.86      yamt 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    693   1.86      yamt 	    uio->uio_vmspace != curproc->p_vmspace)
    694    1.1       cgd 		panic("spec_read proc");
    695    1.1       cgd #endif
    696    1.1       cgd 	if (uio->uio_resid == 0)
    697    1.1       cgd 		return (0);
    698    1.1       cgd 
    699   1.56       chs 	switch (vp->v_type) {
    700   1.56       chs 
    701   1.56       chs 	case VCHR:
    702  1.130   hannken 		VOP_UNLOCK(vp);
    703  1.100        ad 		error = cdev_read(vp->v_rdev, uio, ap->a_ioflag);
    704   1.58       chs 		vn_lock(vp, LK_SHARED | LK_RETRY);
    705    1.1       cgd 		return (error);
    706    1.1       cgd 
    707   1.56       chs 	case VBLK:
    708  1.112        ad 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
    709   1.56       chs 		if (uio->uio_offset < 0)
    710   1.56       chs 			return (EINVAL);
    711   1.56       chs 		bsize = BLKDEV_IOSIZE;
    712  1.138  dholland 
    713  1.138  dholland 		/*
    714  1.138  dholland 		 * dholland 20130616: XXX this logic should not be
    715  1.138  dholland 		 * here. It is here because the old buffer cache
    716  1.138  dholland 		 * demands that all accesses to the same blocks need
    717  1.138  dholland 		 * to be the same size; but it only works for FFS and
    718  1.138  dholland 		 * nowadays I think it'll fail silently if the size
    719  1.138  dholland 		 * info in the disklabel is wrong. (Or missing.) The
    720  1.138  dholland 		 * buffer cache needs to be smarter; or failing that
    721  1.138  dholland 		 * we need a reliable way here to get the right block
    722  1.138  dholland 		 * size; or a reliable way to guarantee that (a) the
    723  1.138  dholland 		 * fs is not mounted when we get here and (b) any
    724  1.138  dholland 		 * buffers generated here will get purged when the fs
    725  1.138  dholland 		 * does get mounted.
    726  1.138  dholland 		 */
    727  1.100        ad 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
    728   1.56       chs 			if (dpart.part->p_fstype == FS_BSDFFS &&
    729   1.56       chs 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    730   1.56       chs 				bsize = dpart.part->p_frag *
    731   1.56       chs 				    dpart.part->p_fsize;
    732   1.56       chs 		}
    733  1.138  dholland 
    734   1.59       chs 		bscale = bsize >> DEV_BSHIFT;
    735   1.56       chs 		do {
    736   1.59       chs 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    737   1.56       chs 			on = uio->uio_offset % bsize;
    738   1.56       chs 			n = min((unsigned)(bsize - on), uio->uio_resid);
    739  1.119   hannken 			error = bread(vp, bn, bsize, NOCRED, 0, &bp);
    740   1.56       chs 			if (error) {
    741   1.56       chs 				return (error);
    742   1.56       chs 			}
    743  1.136   hannken 			n = min(n, bsize - bp->b_resid);
    744   1.56       chs 			error = uiomove((char *)bp->b_data + on, n, uio);
    745  1.107        ad 			brelse(bp, 0);
    746   1.56       chs 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    747   1.56       chs 		return (error);
    748   1.56       chs 
    749   1.56       chs 	default:
    750   1.56       chs 		panic("spec_read type");
    751    1.1       cgd 	}
    752   1.56       chs 	/* NOTREACHED */
    753    1.1       cgd }
    754    1.1       cgd 
    755    1.1       cgd /*
    756    1.1       cgd  * Vnode op for write
    757    1.1       cgd  */
    758    1.1       cgd /* ARGSUSED */
    759   1.28  christos int
    760  1.104     pooka spec_write(void *v)
    761   1.28  christos {
    762   1.15   mycroft 	struct vop_write_args /* {
    763   1.15   mycroft 		struct vnode *a_vp;
    764   1.15   mycroft 		struct uio *a_uio;
    765   1.15   mycroft 		int  a_ioflag;
    766   1.87      elad 		kauth_cred_t a_cred;
    767   1.28  christos 	} */ *ap = v;
    768   1.48  augustss 	struct vnode *vp = ap->a_vp;
    769   1.48  augustss 	struct uio *uio = ap->a_uio;
    770   1.86      yamt 	struct lwp *l = curlwp;
    771   1.56       chs 	struct buf *bp;
    772   1.56       chs 	daddr_t bn;
    773   1.59       chs 	int bsize, bscale;
    774   1.56       chs 	struct partinfo dpart;
    775   1.64   gehenna 	int n, on;
    776    1.1       cgd 	int error = 0;
    777    1.1       cgd 
    778    1.1       cgd #ifdef DIAGNOSTIC
    779    1.1       cgd 	if (uio->uio_rw != UIO_WRITE)
    780    1.1       cgd 		panic("spec_write mode");
    781   1.86      yamt 	if (&uio->uio_vmspace->vm_map != kernel_map &&
    782   1.86      yamt 	    uio->uio_vmspace != curproc->p_vmspace)
    783    1.1       cgd 		panic("spec_write proc");
    784    1.1       cgd #endif
    785    1.1       cgd 
    786   1.56       chs 	switch (vp->v_type) {
    787   1.56       chs 
    788   1.56       chs 	case VCHR:
    789  1.130   hannken 		VOP_UNLOCK(vp);
    790  1.100        ad 		error = cdev_write(vp->v_rdev, uio, ap->a_ioflag);
    791   1.39      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    792    1.1       cgd 		return (error);
    793   1.56       chs 
    794   1.56       chs 	case VBLK:
    795  1.112        ad 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
    796   1.56       chs 		if (uio->uio_resid == 0)
    797   1.56       chs 			return (0);
    798   1.56       chs 		if (uio->uio_offset < 0)
    799   1.56       chs 			return (EINVAL);
    800   1.56       chs 		bsize = BLKDEV_IOSIZE;
    801  1.100        ad 		if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
    802   1.56       chs 			if (dpart.part->p_fstype == FS_BSDFFS &&
    803   1.56       chs 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
    804   1.56       chs 				bsize = dpart.part->p_frag *
    805   1.56       chs 				    dpart.part->p_fsize;
    806   1.56       chs 		}
    807   1.59       chs 		bscale = bsize >> DEV_BSHIFT;
    808   1.56       chs 		do {
    809   1.59       chs 			bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
    810   1.56       chs 			on = uio->uio_offset % bsize;
    811   1.56       chs 			n = min((unsigned)(bsize - on), uio->uio_resid);
    812   1.56       chs 			if (n == bsize)
    813   1.56       chs 				bp = getblk(vp, bn, bsize, 0, 0);
    814   1.56       chs 			else
    815  1.119   hannken 				error = bread(vp, bn, bsize, NOCRED,
    816  1.119   hannken 				    B_MODIFY, &bp);
    817   1.56       chs 			if (error) {
    818   1.56       chs 				return (error);
    819   1.56       chs 			}
    820   1.56       chs 			n = min(n, bsize - bp->b_resid);
    821   1.56       chs 			error = uiomove((char *)bp->b_data + on, n, uio);
    822   1.56       chs 			if (error)
    823  1.107        ad 				brelse(bp, 0);
    824   1.56       chs 			else {
    825   1.56       chs 				if (n + on == bsize)
    826   1.56       chs 					bawrite(bp);
    827   1.56       chs 				else
    828   1.56       chs 					bdwrite(bp);
    829  1.107        ad 				error = bp->b_error;
    830   1.56       chs 			}
    831   1.56       chs 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
    832   1.56       chs 		return (error);
    833   1.56       chs 
    834   1.56       chs 	default:
    835   1.56       chs 		panic("spec_write type");
    836   1.55       chs 	}
    837   1.56       chs 	/* NOTREACHED */
    838    1.1       cgd }
    839    1.1       cgd 
    840    1.1       cgd /*
    841    1.1       cgd  * Device ioctl operation.
    842    1.1       cgd  */
    843    1.1       cgd /* ARGSUSED */
    844   1.28  christos int
    845  1.104     pooka spec_ioctl(void *v)
    846   1.28  christos {
    847   1.15   mycroft 	struct vop_ioctl_args /* {
    848   1.15   mycroft 		struct vnode *a_vp;
    849   1.19       cgd 		u_long a_command;
    850   1.78       jrf 		void  *a_data;
    851   1.15   mycroft 		int  a_fflag;
    852   1.87      elad 		kauth_cred_t a_cred;
    853   1.28  christos 	} */ *ap = v;
    854   1.83       chs 	struct vnode *vp;
    855   1.83       chs 	dev_t dev;
    856    1.1       cgd 
    857   1.83       chs 	/*
    858   1.83       chs 	 * Extract all the info we need from the vnode, taking care to
    859   1.83       chs 	 * avoid a race with VOP_REVOKE().
    860   1.83       chs 	 */
    861   1.83       chs 
    862   1.83       chs 	vp = ap->a_vp;
    863   1.83       chs 	dev = NODEV;
    864  1.134     rmind 	mutex_enter(vp->v_interlock);
    865  1.143   hannken 	if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) {
    866   1.83       chs 		dev = vp->v_rdev;
    867   1.83       chs 	}
    868  1.134     rmind 	mutex_exit(vp->v_interlock);
    869   1.83       chs 	if (dev == NODEV) {
    870   1.83       chs 		return ENXIO;
    871   1.83       chs 	}
    872   1.83       chs 
    873   1.83       chs 	switch (vp->v_type) {
    874    1.1       cgd 
    875    1.1       cgd 	case VCHR:
    876  1.100        ad 		return cdev_ioctl(dev, ap->a_command, ap->a_data,
    877  1.109     pooka 		    ap->a_fflag, curlwp);
    878    1.1       cgd 
    879    1.1       cgd 	case VBLK:
    880  1.112        ad 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
    881  1.100        ad 		return bdev_ioctl(dev, ap->a_command, ap->a_data,
    882  1.109     pooka 		   ap->a_fflag, curlwp);
    883    1.1       cgd 
    884    1.1       cgd 	default:
    885    1.1       cgd 		panic("spec_ioctl");
    886    1.1       cgd 		/* NOTREACHED */
    887    1.1       cgd 	}
    888    1.1       cgd }
    889    1.1       cgd 
    890    1.1       cgd /* ARGSUSED */
    891   1.28  christos int
    892  1.104     pooka spec_poll(void *v)
    893   1.28  christos {
    894   1.32   mycroft 	struct vop_poll_args /* {
    895   1.15   mycroft 		struct vnode *a_vp;
    896   1.32   mycroft 		int a_events;
    897   1.28  christos 	} */ *ap = v;
    898   1.91       jld 	struct vnode *vp;
    899   1.48  augustss 	dev_t dev;
    900    1.1       cgd 
    901   1.91       jld 	/*
    902   1.91       jld 	 * Extract all the info we need from the vnode, taking care to
    903   1.91       jld 	 * avoid a race with VOP_REVOKE().
    904   1.91       jld 	 */
    905   1.91       jld 
    906   1.91       jld 	vp = ap->a_vp;
    907   1.91       jld 	dev = NODEV;
    908  1.134     rmind 	mutex_enter(vp->v_interlock);
    909  1.143   hannken 	if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) {
    910   1.91       jld 		dev = vp->v_rdev;
    911   1.91       jld 	}
    912  1.134     rmind 	mutex_exit(vp->v_interlock);
    913   1.91       jld 	if (dev == NODEV) {
    914   1.92       jld 		return POLLERR;
    915   1.91       jld 	}
    916   1.91       jld 
    917   1.91       jld 	switch (vp->v_type) {
    918    1.1       cgd 
    919    1.1       cgd 	case VCHR:
    920  1.109     pooka 		return cdev_poll(dev, ap->a_events, curlwp);
    921   1.30   mycroft 
    922   1.30   mycroft 	default:
    923   1.32   mycroft 		return (genfs_poll(v));
    924   1.15   mycroft 	}
    925   1.15   mycroft }
    926   1.65  jdolecek 
    927   1.65  jdolecek /* ARGSUSED */
    928   1.65  jdolecek int
    929  1.104     pooka spec_kqfilter(void *v)
    930   1.65  jdolecek {
    931   1.65  jdolecek 	struct vop_kqfilter_args /* {
    932   1.65  jdolecek 		struct vnode	*a_vp;
    933   1.65  jdolecek 		struct proc	*a_kn;
    934   1.65  jdolecek 	} */ *ap = v;
    935   1.65  jdolecek 	dev_t dev;
    936   1.65  jdolecek 
    937   1.65  jdolecek 	switch (ap->a_vp->v_type) {
    938   1.65  jdolecek 
    939   1.65  jdolecek 	case VCHR:
    940   1.65  jdolecek 		dev = ap->a_vp->v_rdev;
    941  1.100        ad 		return cdev_kqfilter(dev, ap->a_kn);
    942   1.65  jdolecek 	default:
    943   1.65  jdolecek 		/*
    944   1.65  jdolecek 		 * Block devices don't support kqfilter, and refuse it
    945   1.65  jdolecek 		 * for any other files (like those vflush()ed) too.
    946   1.65  jdolecek 		 */
    947   1.65  jdolecek 		return (EOPNOTSUPP);
    948   1.65  jdolecek 	}
    949   1.65  jdolecek }
    950   1.65  jdolecek 
    951   1.15   mycroft /*
    952  1.101     pooka  * Allow mapping of only D_DISK.  This is called only for VBLK.
    953  1.101     pooka  */
    954  1.101     pooka int
    955  1.104     pooka spec_mmap(void *v)
    956  1.101     pooka {
    957  1.101     pooka 	struct vop_mmap_args /* {
    958  1.101     pooka 		struct vnode *a_vp;
    959  1.102     pooka 		vm_prot_t a_prot;
    960  1.101     pooka 		kauth_cred_t a_cred;
    961  1.101     pooka 	} */ *ap = v;
    962  1.101     pooka 	struct vnode *vp = ap->a_vp;
    963  1.101     pooka 
    964  1.101     pooka 	KASSERT(vp->v_type == VBLK);
    965  1.101     pooka 	if (bdev_type(vp->v_rdev) != D_DISK)
    966  1.101     pooka 		return EINVAL;
    967  1.101     pooka 
    968  1.101     pooka 	return 0;
    969  1.101     pooka }
    970  1.101     pooka 
    971  1.101     pooka /*
    972   1.15   mycroft  * Synch buffers associated with a block device
    973   1.15   mycroft  */
    974   1.15   mycroft /* ARGSUSED */
    975   1.15   mycroft int
    976  1.104     pooka spec_fsync(void *v)
    977   1.28  christos {
    978   1.15   mycroft 	struct vop_fsync_args /* {
    979   1.15   mycroft 		struct vnode *a_vp;
    980   1.87      elad 		kauth_cred_t a_cred;
    981   1.40    kleink 		int  a_flags;
    982   1.50      fvdl 		off_t offlo;
    983   1.50      fvdl 		off_t offhi;
    984   1.28  christos 	} */ *ap = v;
    985   1.48  augustss 	struct vnode *vp = ap->a_vp;
    986  1.118        ad 	struct mount *mp;
    987  1.118        ad 	int error;
    988   1.15   mycroft 
    989  1.112        ad 	if (vp->v_type == VBLK) {
    990  1.141   hannken 		if ((mp = spec_node_getmountedfs(vp)) != NULL) {
    991  1.133   hannken 			error = VFS_FSYNC(mp, vp, ap->a_flags);
    992  1.118        ad 			if (error != EOPNOTSUPP)
    993  1.118        ad 				return error;
    994  1.118        ad 		}
    995  1.135       chs 		return vflushbuf(vp, ap->a_flags);
    996  1.112        ad 	}
    997   1.15   mycroft 	return (0);
    998    1.1       cgd }
    999    1.1       cgd 
   1000    1.1       cgd /*
   1001    1.1       cgd  * Just call the device strategy routine
   1002    1.1       cgd  */
   1003   1.28  christos int
   1004  1.104     pooka spec_strategy(void *v)
   1005   1.28  christos {
   1006   1.15   mycroft 	struct vop_strategy_args /* {
   1007   1.76   hannken 		struct vnode *a_vp;
   1008   1.15   mycroft 		struct buf *a_bp;
   1009   1.28  christos 	} */ *ap = v;
   1010   1.76   hannken 	struct vnode *vp = ap->a_vp;
   1011   1.76   hannken 	struct buf *bp = ap->a_bp;
   1012  1.106   hannken 	int error;
   1013    1.1       cgd 
   1014  1.112        ad 	KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
   1015  1.112        ad 
   1016   1.79   hannken 	error = 0;
   1017   1.76   hannken 	bp->b_dev = vp->v_rdev;
   1018   1.77   hannken 
   1019  1.106   hannken 	if (!(bp->b_flags & B_READ))
   1020  1.110   hannken 		error = fscow_run(bp, false);
   1021   1.77   hannken 
   1022   1.79   hannken 	if (error) {
   1023   1.79   hannken 		bp->b_error = error;
   1024  1.140  dholland 		bp->b_resid = bp->b_bcount;
   1025   1.79   hannken 		biodone(bp);
   1026   1.79   hannken 		return (error);
   1027   1.79   hannken 	}
   1028   1.79   hannken 
   1029  1.100        ad 	bdev_strategy(bp);
   1030   1.76   hannken 
   1031    1.1       cgd 	return (0);
   1032    1.1       cgd }
   1033    1.1       cgd 
   1034   1.39      fvdl int
   1035  1.104     pooka spec_inactive(void *v)
   1036   1.39      fvdl {
   1037   1.39      fvdl 	struct vop_inactive_args /* {
   1038   1.39      fvdl 		struct vnode *a_vp;
   1039   1.85  christos 		struct proc *a_l;
   1040   1.39      fvdl 	} */ *ap = v;
   1041   1.39      fvdl 
   1042  1.130   hannken 	VOP_UNLOCK(ap->a_vp);
   1043   1.39      fvdl 	return (0);
   1044   1.39      fvdl }
   1045   1.39      fvdl 
   1046    1.1       cgd /*
   1047    1.1       cgd  * This is a noop, simply returning what one has been given.
   1048    1.1       cgd  */
   1049   1.28  christos int
   1050  1.104     pooka spec_bmap(void *v)
   1051   1.28  christos {
   1052   1.15   mycroft 	struct vop_bmap_args /* {
   1053   1.15   mycroft 		struct vnode *a_vp;
   1054   1.15   mycroft 		daddr_t  a_bn;
   1055   1.15   mycroft 		struct vnode **a_vpp;
   1056   1.15   mycroft 		daddr_t *a_bnp;
   1057   1.39      fvdl 		int *a_runp;
   1058   1.28  christos 	} */ *ap = v;
   1059    1.1       cgd 
   1060   1.15   mycroft 	if (ap->a_vpp != NULL)
   1061   1.15   mycroft 		*ap->a_vpp = ap->a_vp;
   1062   1.15   mycroft 	if (ap->a_bnp != NULL)
   1063   1.15   mycroft 		*ap->a_bnp = ap->a_bn;
   1064   1.39      fvdl 	if (ap->a_runp != NULL)
   1065   1.55       chs 		*ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
   1066    1.1       cgd 	return (0);
   1067    1.1       cgd }
   1068    1.1       cgd 
   1069    1.1       cgd /*
   1070    1.1       cgd  * Device close routine
   1071    1.1       cgd  */
   1072    1.1       cgd /* ARGSUSED */
   1073   1.28  christos int
   1074  1.104     pooka spec_close(void *v)
   1075   1.28  christos {
   1076   1.15   mycroft 	struct vop_close_args /* {
   1077   1.15   mycroft 		struct vnode *a_vp;
   1078   1.15   mycroft 		int  a_fflag;
   1079   1.87      elad 		kauth_cred_t a_cred;
   1080   1.28  christos 	} */ *ap = v;
   1081   1.48  augustss 	struct vnode *vp = ap->a_vp;
   1082   1.71       dsl 	struct session *sess;
   1083    1.1       cgd 	dev_t dev = vp->v_rdev;
   1084  1.143   hannken 	int flags = ap->a_fflag;
   1085  1.143   hannken 	int mode, error, count;
   1086  1.112        ad 	specnode_t *sn;
   1087  1.112        ad 	specdev_t *sd;
   1088   1.44  wrstuden 
   1089  1.143   hannken 	mutex_enter(vp->v_interlock);
   1090  1.112        ad 	sn = vp->v_specnode;
   1091  1.112        ad 	sd = sn->sn_dev;
   1092  1.143   hannken 	/*
   1093  1.143   hannken 	 * If we're going away soon, make this non-blocking.
   1094  1.143   hannken 	 * Also ensures that we won't wedge in vn_lock below.
   1095  1.143   hannken 	 */
   1096  1.143   hannken 	if (vdead_check(vp, VDEAD_NOWAIT) != 0)
   1097  1.143   hannken 		flags |= FNONBLOCK;
   1098  1.143   hannken 	mutex_exit(vp->v_interlock);
   1099    1.1       cgd 
   1100    1.1       cgd 	switch (vp->v_type) {
   1101    1.1       cgd 
   1102    1.1       cgd 	case VCHR:
   1103   1.11       cgd 		/*
   1104   1.11       cgd 		 * Hack: a tty device that is a controlling terminal
   1105  1.112        ad 		 * has a reference from the session structure.  We
   1106  1.112        ad 		 * cannot easily tell that a character device is a
   1107  1.112        ad 		 * controlling terminal, unless it is the closing
   1108  1.112        ad 		 * process' controlling terminal.  In that case, if the
   1109  1.112        ad 		 * open count is 1 release the reference from the
   1110  1.112        ad 		 * session.  Also, remove the link from the tty back to
   1111  1.112        ad 		 * the session and pgrp.
   1112  1.112        ad 		 *
   1113  1.112        ad 		 * XXX V. fishy.
   1114   1.11       cgd 		 */
   1115  1.116        ad 		mutex_enter(proc_lock);
   1116  1.112        ad 		sess = curlwp->l_proc->p_session;
   1117  1.112        ad 		if (sn->sn_opencnt == 1 && vp == sess->s_ttyvp) {
   1118  1.112        ad 			mutex_spin_enter(&tty_lock);
   1119   1.71       dsl 			sess->s_ttyvp = NULL;
   1120   1.72        pk 			if (sess->s_ttyp->t_session != NULL) {
   1121   1.72        pk 				sess->s_ttyp->t_pgrp = NULL;
   1122   1.72        pk 				sess->s_ttyp->t_session = NULL;
   1123  1.112        ad 				mutex_spin_exit(&tty_lock);
   1124  1.124     rmind 				/* Releases proc_lock. */
   1125  1.124     rmind 				proc_sessrele(sess);
   1126  1.100        ad 			} else {
   1127  1.112        ad 				mutex_spin_exit(&tty_lock);
   1128  1.100        ad 				if (sess->s_ttyp->t_pgrp != NULL)
   1129  1.100        ad 					panic("spec_close: spurious pgrp ref");
   1130  1.116        ad 				mutex_exit(proc_lock);
   1131  1.100        ad 			}
   1132   1.11       cgd 			vrele(vp);
   1133  1.100        ad 		} else
   1134  1.116        ad 			mutex_exit(proc_lock);
   1135  1.100        ad 
   1136    1.1       cgd 		/*
   1137    1.1       cgd 		 * If the vnode is locked, then we are in the midst
   1138    1.1       cgd 		 * of forcably closing the device, otherwise we only
   1139    1.1       cgd 		 * close on last reference.
   1140    1.1       cgd 		 */
   1141    1.1       cgd 		mode = S_IFCHR;
   1142    1.1       cgd 		break;
   1143    1.1       cgd 
   1144    1.1       cgd 	case VBLK:
   1145  1.112        ad 		KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
   1146    1.1       cgd 		/*
   1147    1.1       cgd 		 * On last close of a block device (that isn't mounted)
   1148    1.1       cgd 		 * we must invalidate any in core blocks, so that
   1149    1.1       cgd 		 * we can, for instance, change floppy disks.
   1150    1.1       cgd 		 */
   1151  1.109     pooka 		error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0);
   1152   1.28  christos 		if (error)
   1153   1.15   mycroft 			return (error);
   1154    1.1       cgd 		/*
   1155    1.1       cgd 		 * We do not want to really close the device if it
   1156    1.1       cgd 		 * is still in use unless we are trying to close it
   1157    1.1       cgd 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
   1158    1.1       cgd 		 * holds a reference to the vnode, and because we mark
   1159    1.1       cgd 		 * any other vnodes that alias this device, when the
   1160    1.1       cgd 		 * sum of the reference counts on all the aliased
   1161    1.1       cgd 		 * vnodes descends to one, we are on last close.
   1162    1.1       cgd 		 */
   1163    1.1       cgd 		mode = S_IFBLK;
   1164    1.1       cgd 		break;
   1165    1.5       cgd 
   1166    1.1       cgd 	default:
   1167    1.1       cgd 		panic("spec_close: not special");
   1168    1.1       cgd 	}
   1169    1.1       cgd 
   1170  1.120     pooka 	mutex_enter(&device_lock);
   1171  1.112        ad 	sn->sn_opencnt--;
   1172  1.112        ad 	count = --sd->sd_opencnt;
   1173  1.112        ad 	if (vp->v_type == VBLK)
   1174  1.112        ad 		sd->sd_bdevvp = NULL;
   1175  1.120     pooka 	mutex_exit(&device_lock);
   1176  1.112        ad 
   1177  1.112        ad 	if (count != 0)
   1178  1.112        ad 		return 0;
   1179  1.112        ad 
   1180   1.44  wrstuden 	/*
   1181   1.62       wiz 	 * If we're able to block, release the vnode lock & reacquire. We
   1182   1.72        pk 	 * might end up sleeping for someone else who wants our queues. They
   1183  1.143   hannken 	 * won't get them if we hold the vnode locked.
   1184   1.44  wrstuden 	 */
   1185  1.143   hannken 	if (!(flags & FNONBLOCK))
   1186  1.130   hannken 		VOP_UNLOCK(vp);
   1187   1.44  wrstuden 
   1188  1.100        ad 	if (vp->v_type == VBLK)
   1189  1.143   hannken 		error = bdev_close(dev, flags, mode, curlwp);
   1190   1.64   gehenna 	else
   1191  1.143   hannken 		error = cdev_close(dev, flags, mode, curlwp);
   1192   1.44  wrstuden 
   1193  1.143   hannken 	if (!(flags & FNONBLOCK))
   1194   1.44  wrstuden 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1195   1.44  wrstuden 
   1196   1.44  wrstuden 	return (error);
   1197    1.1       cgd }
   1198    1.1       cgd 
   1199    1.1       cgd /*
   1200    1.1       cgd  * Print out the contents of a special device vnode.
   1201    1.1       cgd  */
   1202   1.28  christos int
   1203  1.104     pooka spec_print(void *v)
   1204   1.28  christos {
   1205   1.15   mycroft 	struct vop_print_args /* {
   1206   1.15   mycroft 		struct vnode *a_vp;
   1207   1.28  christos 	} */ *ap = v;
   1208   1.15   mycroft 
   1209  1.121  christos 	printf("dev %llu, %llu\n", (unsigned long long)major(ap->a_vp->v_rdev),
   1210  1.121  christos 	    (unsigned long long)minor(ap->a_vp->v_rdev));
   1211   1.28  christos 	return 0;
   1212   1.15   mycroft }
   1213   1.15   mycroft 
   1214   1.15   mycroft /*
   1215   1.15   mycroft  * Return POSIX pathconf information applicable to special devices.
   1216   1.15   mycroft  */
   1217   1.28  christos int
   1218  1.104     pooka spec_pathconf(void *v)
   1219   1.28  christos {
   1220   1.15   mycroft 	struct vop_pathconf_args /* {
   1221   1.15   mycroft 		struct vnode *a_vp;
   1222   1.15   mycroft 		int a_name;
   1223   1.18       cgd 		register_t *a_retval;
   1224   1.28  christos 	} */ *ap = v;
   1225    1.1       cgd 
   1226   1.15   mycroft 	switch (ap->a_name) {
   1227   1.15   mycroft 	case _PC_LINK_MAX:
   1228   1.15   mycroft 		*ap->a_retval = LINK_MAX;
   1229   1.15   mycroft 		return (0);
   1230   1.15   mycroft 	case _PC_MAX_CANON:
   1231   1.15   mycroft 		*ap->a_retval = MAX_CANON;
   1232   1.15   mycroft 		return (0);
   1233   1.15   mycroft 	case _PC_MAX_INPUT:
   1234   1.15   mycroft 		*ap->a_retval = MAX_INPUT;
   1235   1.15   mycroft 		return (0);
   1236   1.15   mycroft 	case _PC_PIPE_BUF:
   1237   1.15   mycroft 		*ap->a_retval = PIPE_BUF;
   1238   1.15   mycroft 		return (0);
   1239   1.15   mycroft 	case _PC_CHOWN_RESTRICTED:
   1240   1.15   mycroft 		*ap->a_retval = 1;
   1241   1.15   mycroft 		return (0);
   1242   1.15   mycroft 	case _PC_VDISABLE:
   1243   1.15   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
   1244   1.41    kleink 		return (0);
   1245   1.41    kleink 	case _PC_SYNC_IO:
   1246   1.41    kleink 		*ap->a_retval = 1;
   1247   1.15   mycroft 		return (0);
   1248   1.15   mycroft 	default:
   1249   1.15   mycroft 		return (EINVAL);
   1250   1.15   mycroft 	}
   1251    1.1       cgd 	/* NOTREACHED */
   1252   1.35    kleink }
   1253   1.35    kleink 
   1254   1.80     perry /*
   1255   1.35    kleink  * Advisory record locking support.
   1256   1.35    kleink  */
   1257   1.35    kleink int
   1258  1.104     pooka spec_advlock(void *v)
   1259   1.35    kleink {
   1260   1.35    kleink 	struct vop_advlock_args /* {
   1261   1.35    kleink 		struct vnode *a_vp;
   1262   1.78       jrf 		void *a_id;
   1263   1.35    kleink 		int a_op;
   1264   1.35    kleink 		struct flock *a_fl;
   1265   1.35    kleink 		int a_flags;
   1266   1.35    kleink 	} */ *ap = v;
   1267   1.48  augustss 	struct vnode *vp = ap->a_vp;
   1268   1.35    kleink 
   1269   1.49  jdolecek 	return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
   1270    1.1       cgd }
   1271