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