Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.73
      1 /*	$NetBSD: vfs_vnops.c,v 1.73 2003/08/07 16:32:04 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)vfs_vnops.c	8.14 (Berkeley) 6/15/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.73 2003/08/07 16:32:04 agc Exp $");
     41 
     42 #include "fs_union.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/file.h>
     48 #include <sys/stat.h>
     49 #include <sys/buf.h>
     50 #include <sys/proc.h>
     51 #include <sys/mount.h>
     52 #include <sys/namei.h>
     53 #include <sys/vnode.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/tty.h>
     56 #include <sys/poll.h>
     57 
     58 #include <uvm/uvm_extern.h>
     59 
     60 #ifdef UNION
     61 #include <fs/union/union.h>
     62 #endif
     63 
     64 #if defined(LKM) || defined(UNION)
     65 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct proc *);
     66 #endif
     67 
     68 #ifdef VERIFIED_EXEC
     69 #include <sys/verified_exec.h>
     70 
     71 extern LIST_HEAD(veriexec_devhead, veriexec_dev_list) veriexec_dev_head;
     72 extern struct veriexec_devhead veriexec_file_dev_head;
     73 #endif
     74 
     75 static int vn_read(struct file *fp, off_t *offset, struct uio *uio,
     76 	    struct ucred *cred, int flags);
     77 static int vn_write(struct file *fp, off_t *offset, struct uio *uio,
     78 	    struct ucred *cred, int flags);
     79 static int vn_closefile(struct file *fp, struct proc *p);
     80 static int vn_poll(struct file *fp, int events, struct proc *p);
     81 static int vn_fcntl(struct file *fp, u_int com, void *data, struct proc *p);
     82 static int vn_statfile(struct file *fp, struct stat *sb, struct proc *p);
     83 static int vn_ioctl(struct file *fp, u_long com, void *data, struct proc *p);
     84 
     85 struct 	fileops vnops = {
     86 	vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
     87 	vn_statfile, vn_closefile, vn_kqfilter
     88 };
     89 
     90 /*
     91  * Common code for vnode open operations.
     92  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
     93  */
     94 int
     95 vn_open(ndp, fmode, cmode)
     96 	struct nameidata *ndp;
     97 	int fmode, cmode;
     98 {
     99 	struct vnode *vp;
    100 	struct proc *p = ndp->ni_cnd.cn_proc;
    101 	struct ucred *cred = p->p_ucred;
    102 	struct vattr va;
    103 	int error;
    104 #ifdef VERIFIED_EXEC
    105 	char got_dev;
    106 	struct veriexec_inode_list *veriexec_node;
    107 	char fingerprint[MAXFINGERPRINTLEN];
    108 #endif
    109 
    110 	if (fmode & O_CREAT) {
    111 		ndp->ni_cnd.cn_nameiop = CREATE;
    112 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
    113 		if ((fmode & O_EXCL) == 0 &&
    114 		    ((fmode & O_NOFOLLOW) == 0))
    115 			ndp->ni_cnd.cn_flags |= FOLLOW;
    116 		if ((error = namei(ndp)) != 0)
    117 			return (error);
    118 		if (ndp->ni_vp == NULL) {
    119 			VATTR_NULL(&va);
    120 			va.va_type = VREG;
    121 			va.va_mode = cmode;
    122 			if (fmode & O_EXCL)
    123 				 va.va_vaflags |= VA_EXCLUSIVE;
    124 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
    125 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
    126 					   &ndp->ni_cnd, &va);
    127 			if (error)
    128 				return (error);
    129 			fmode &= ~O_TRUNC;
    130 			vp = ndp->ni_vp;
    131 		} else {
    132 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    133 			if (ndp->ni_dvp == ndp->ni_vp)
    134 				vrele(ndp->ni_dvp);
    135 			else
    136 				vput(ndp->ni_dvp);
    137 			ndp->ni_dvp = NULL;
    138 			vp = ndp->ni_vp;
    139 			if (fmode & O_EXCL) {
    140 				error = EEXIST;
    141 				goto bad;
    142 			}
    143 			if (ndp->ni_vp->v_type == VLNK) {
    144 				error = EFTYPE;
    145 				goto bad;
    146 			}
    147 			fmode &= ~O_CREAT;
    148 		}
    149 	} else {
    150 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    151 		ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
    152 		if ((error = namei(ndp)) != 0)
    153 			return (error);
    154 		vp = ndp->ni_vp;
    155 	}
    156 	if (vp->v_type == VSOCK) {
    157 		error = EOPNOTSUPP;
    158 		goto bad;
    159 	}
    160 
    161 #ifdef VERIFIED_EXEC
    162 	veriexec_node = NULL;
    163 
    164 	if ((error = VOP_GETATTR(vp, &va, cred, p)) != 0)
    165 		goto bad;
    166 #endif
    167 
    168 	if ((fmode & O_CREAT) == 0) {
    169 #ifdef VERIFIED_EXEC
    170 		  /*
    171 		   * Look for the file on the fingerprint lists iff
    172 		   * it has not been seen before.
    173 		   */
    174 		if ((vp->fp_status == FINGERPRINT_INVALID) ||
    175 		    (vp->fp_status == FINGERPRINT_NODEV)) {
    176 			  /* check the file list for the finger print */
    177 			veriexec_node = get_veriexec_inode(&veriexec_file_dev_head,
    178 						     va.va_fsid,
    179 						     va.va_fileid,
    180 						     &got_dev);
    181 			if (veriexec_node == NULL) {
    182 				/* failing that, check the exec list */
    183 				veriexec_node = get_veriexec_inode(
    184 					&veriexec_dev_head, va.va_fsid,
    185 					va.va_fileid, &got_dev);
    186 			}
    187 
    188 			if ((veriexec_node == NULL) && (got_dev == 1))
    189 				vp->fp_status = FINGERPRINT_NOENTRY;
    190 
    191 			if (veriexec_node != NULL) {
    192 				if ((error = evaluate_fingerprint(vp,
    193 						veriexec_node, p, va.va_size,
    194 						fingerprint)) != 0)
    195 					goto bad;
    196 
    197 				if (fingerprintcmp(veriexec_node,
    198 						   fingerprint) == 0) {
    199 					  /* fingerprint ok */
    200 					vp->fp_status =	FINGERPRINT_VALID;
    201 #ifdef VERIFIED_EXEC_DEBUG
    202 					printf(
    203 			"file fingerprint matches for dev %lu, file %lu\n",
    204 						va.va_fsid, va.va_fileid);
    205 #endif
    206 				} else {
    207 					vp->fp_status =	FINGERPRINT_NOMATCH;
    208 				}
    209 			}
    210 		}
    211 #endif
    212 
    213 		if (fmode & FREAD) {
    214 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
    215 				goto bad;
    216 
    217 #ifdef VERIFIED_EXEC
    218 				/* file is on finger print list */
    219 			if (vp->fp_status == FINGERPRINT_NOMATCH) {
    220 				  /* fingerprint bad */
    221 				printf(
    222 		"file fingerprint does not match on dev %lu, file %lu\n",
    223 					va.va_fsid, va.va_fileid);
    224 				if (securelevel > 2) {
    225 					error = EPERM;
    226 					goto bad;
    227 				}
    228 			}
    229 #endif
    230 		}
    231 		if (fmode & (FWRITE | O_TRUNC)) {
    232 			if (vp->v_type == VDIR) {
    233 				error = EISDIR;
    234 				goto bad;
    235 			}
    236 			if ((error = vn_writechk(vp)) != 0 ||
    237 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
    238 				goto bad;
    239 #ifdef VERIFIED_EXEC
    240 			  /*
    241 			   * If file has a fingerprint then
    242 			   * deny the write request, otherwise
    243 			   * invalidate the status so we don't
    244 			   * keep checking for the file having
    245 			   * a fingerprint.
    246 			   */
    247 			if (vp->fp_status == FINGERPRINT_VALID) {
    248 				printf(
    249 		      "writing to fingerprinted file for dev %lu, file %lu\n",
    250 		      va.va_fsid, va.va_fileid);
    251 				if (securelevel > 2) {
    252 					error = EPERM;
    253 					goto bad;
    254 				} else {
    255 					vp->fp_status =	FINGERPRINT_INVALID;
    256 				}
    257 			}
    258 #endif
    259 		}
    260 	}
    261 	if (fmode & O_TRUNC) {
    262 		VOP_UNLOCK(vp, 0);			/* XXX */
    263 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
    264 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
    265 		VATTR_NULL(&va);
    266 		va.va_size = 0;
    267 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
    268 			goto bad;
    269 	}
    270 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
    271 		goto bad;
    272 	if (vp->v_type == VREG &&
    273 	    uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
    274 		error = EIO;
    275 		goto bad;
    276 	}
    277 	if (fmode & FWRITE)
    278 		vp->v_writecount++;
    279 
    280 	return (0);
    281 bad:
    282 	vput(vp);
    283 	return (error);
    284 }
    285 
    286 /*
    287  * Check for write permissions on the specified vnode.
    288  * Prototype text segments cannot be written.
    289  */
    290 int
    291 vn_writechk(vp)
    292 	struct vnode *vp;
    293 {
    294 
    295 	/*
    296 	 * If the vnode is in use as a process's text,
    297 	 * we can't allow writing.
    298 	 */
    299 	if (vp->v_flag & VTEXT)
    300 		return (ETXTBSY);
    301 	return (0);
    302 }
    303 
    304 /*
    305  * Mark a vnode as having executable mappings.
    306  */
    307 void
    308 vn_markexec(vp)
    309 	struct vnode *vp;
    310 {
    311 	if ((vp->v_flag & VEXECMAP) == 0) {
    312 		uvmexp.filepages -= vp->v_uobj.uo_npages;
    313 		uvmexp.execpages += vp->v_uobj.uo_npages;
    314 	}
    315 	vp->v_flag |= VEXECMAP;
    316 }
    317 
    318 /*
    319  * Mark a vnode as being the text of a process.
    320  * Fail if the vnode is currently writable.
    321  */
    322 int
    323 vn_marktext(vp)
    324 	struct vnode *vp;
    325 {
    326 
    327 	if (vp->v_writecount != 0) {
    328 		KASSERT((vp->v_flag & VTEXT) == 0);
    329 		return (ETXTBSY);
    330 	}
    331 	vp->v_flag |= VTEXT;
    332 	vn_markexec(vp);
    333 	return (0);
    334 }
    335 
    336 /*
    337  * Vnode close call
    338  *
    339  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    340  */
    341 int
    342 vn_close(vp, flags, cred, p)
    343 	struct vnode *vp;
    344 	int flags;
    345 	struct ucred *cred;
    346 	struct proc *p;
    347 {
    348 	int error;
    349 
    350 	if (flags & FWRITE)
    351 		vp->v_writecount--;
    352 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    353 	error = VOP_CLOSE(vp, flags, cred, p);
    354 	vput(vp);
    355 	return (error);
    356 }
    357 
    358 /*
    359  * Package up an I/O request on a vnode into a uio and do it.
    360  */
    361 int
    362 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
    363 	enum uio_rw rw;
    364 	struct vnode *vp;
    365 	caddr_t base;
    366 	int len;
    367 	off_t offset;
    368 	enum uio_seg segflg;
    369 	int ioflg;
    370 	struct ucred *cred;
    371 	size_t *aresid;
    372 	struct proc *p;
    373 {
    374 	struct uio auio;
    375 	struct iovec aiov;
    376 	int error;
    377 
    378 	if ((ioflg & IO_NODELOCKED) == 0) {
    379 		if (rw == UIO_READ) {
    380 			vn_lock(vp, LK_SHARED | LK_RETRY);
    381 		} else {
    382 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    383 		}
    384 	}
    385 	auio.uio_iov = &aiov;
    386 	auio.uio_iovcnt = 1;
    387 	aiov.iov_base = base;
    388 	aiov.iov_len = len;
    389 	auio.uio_resid = len;
    390 	auio.uio_offset = offset;
    391 	auio.uio_segflg = segflg;
    392 	auio.uio_rw = rw;
    393 	auio.uio_procp = p;
    394 	if (rw == UIO_READ) {
    395 		error = VOP_READ(vp, &auio, ioflg, cred);
    396 	} else {
    397 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    398 	}
    399 	if (aresid)
    400 		*aresid = auio.uio_resid;
    401 	else
    402 		if (auio.uio_resid && error == 0)
    403 			error = EIO;
    404 	if ((ioflg & IO_NODELOCKED) == 0)
    405 		VOP_UNLOCK(vp, 0);
    406 	return (error);
    407 }
    408 
    409 int
    410 vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
    411 	struct file *fp;
    412 	char *buf;
    413 	int segflg, *done, *ncookies;
    414 	u_int count;
    415 	struct proc *p;
    416 	off_t **cookies;
    417 {
    418 	struct vnode *vp = (struct vnode *)fp->f_data;
    419 	struct iovec aiov;
    420 	struct uio auio;
    421 	int error, eofflag;
    422 
    423 unionread:
    424 	if (vp->v_type != VDIR)
    425 		return (EINVAL);
    426 	aiov.iov_base = buf;
    427 	aiov.iov_len = count;
    428 	auio.uio_iov = &aiov;
    429 	auio.uio_iovcnt = 1;
    430 	auio.uio_rw = UIO_READ;
    431 	auio.uio_segflg = segflg;
    432 	auio.uio_procp = p;
    433 	auio.uio_resid = count;
    434 	vn_lock(vp, LK_SHARED | LK_RETRY);
    435 	auio.uio_offset = fp->f_offset;
    436 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    437 		    ncookies);
    438 	fp->f_offset = auio.uio_offset;
    439 	VOP_UNLOCK(vp, 0);
    440 	if (error)
    441 		return (error);
    442 
    443 #if defined(UNION) || defined(LKM)
    444 	if (count == auio.uio_resid && vn_union_readdir_hook) {
    445 		struct vnode *ovp = vp;
    446 
    447 		error = (*vn_union_readdir_hook)(&vp, fp, p);
    448 		if (error)
    449 			return (error);
    450 		if (vp != ovp)
    451 			goto unionread;
    452 	}
    453 #endif /* UNION || LKM */
    454 
    455 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    456 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    457 		struct vnode *tvp = vp;
    458 		vp = vp->v_mount->mnt_vnodecovered;
    459 		VREF(vp);
    460 		fp->f_data = vp;
    461 		fp->f_offset = 0;
    462 		vrele(tvp);
    463 		goto unionread;
    464 	}
    465 	*done = count - auio.uio_resid;
    466 	return error;
    467 }
    468 
    469 /*
    470  * File table vnode read routine.
    471  */
    472 static int
    473 vn_read(fp, offset, uio, cred, flags)
    474 	struct file *fp;
    475 	off_t *offset;
    476 	struct uio *uio;
    477 	struct ucred *cred;
    478 	int flags;
    479 {
    480 	struct vnode *vp = (struct vnode *)fp->f_data;
    481 	int count, error, ioflag = 0;
    482 
    483 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    484 	if (fp->f_flag & FNONBLOCK)
    485 		ioflag |= IO_NDELAY;
    486 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    487 		ioflag |= IO_SYNC;
    488 	if (fp->f_flag & FALTIO)
    489 		ioflag |= IO_ALTSEMANTICS;
    490 	vn_lock(vp, LK_SHARED | LK_RETRY);
    491 	uio->uio_offset = *offset;
    492 	count = uio->uio_resid;
    493 	error = VOP_READ(vp, uio, ioflag, cred);
    494 	if (flags & FOF_UPDATE_OFFSET)
    495 		*offset += count - uio->uio_resid;
    496 	VOP_UNLOCK(vp, 0);
    497 	return (error);
    498 }
    499 
    500 /*
    501  * File table vnode write routine.
    502  */
    503 static int
    504 vn_write(fp, offset, uio, cred, flags)
    505 	struct file *fp;
    506 	off_t *offset;
    507 	struct uio *uio;
    508 	struct ucred *cred;
    509 	int flags;
    510 {
    511 	struct vnode *vp = (struct vnode *)fp->f_data;
    512 	int count, error, ioflag = IO_UNIT;
    513 
    514 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    515 		ioflag |= IO_APPEND;
    516 	if (fp->f_flag & FNONBLOCK)
    517 		ioflag |= IO_NDELAY;
    518 	if (fp->f_flag & FFSYNC ||
    519 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    520 		ioflag |= IO_SYNC;
    521 	else if (fp->f_flag & FDSYNC)
    522 		ioflag |= IO_DSYNC;
    523 	if (fp->f_flag & FALTIO)
    524 		ioflag |= IO_ALTSEMANTICS;
    525 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
    526 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    527 	uio->uio_offset = *offset;
    528 	count = uio->uio_resid;
    529 	error = VOP_WRITE(vp, uio, ioflag, cred);
    530 	if (flags & FOF_UPDATE_OFFSET) {
    531 		if (ioflag & IO_APPEND)
    532 			*offset = uio->uio_offset;
    533 		else
    534 			*offset += count - uio->uio_resid;
    535 	}
    536 	VOP_UNLOCK(vp, 0);
    537 	return (error);
    538 }
    539 
    540 /*
    541  * File table vnode stat routine.
    542  */
    543 static int
    544 vn_statfile(fp, sb, p)
    545 	struct file *fp;
    546 	struct stat *sb;
    547 	struct proc *p;
    548 {
    549 	struct vnode *vp = (struct vnode *)fp->f_data;
    550 
    551 	return vn_stat(vp, sb, p);
    552 }
    553 
    554 int
    555 vn_stat(vp, sb, p)
    556 	struct vnode *vp;
    557 	struct stat *sb;
    558 	struct proc *p;
    559 {
    560 	struct vattr va;
    561 	int error;
    562 	mode_t mode;
    563 
    564 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
    565 	if (error)
    566 		return (error);
    567 	/*
    568 	 * Copy from vattr table
    569 	 */
    570 	sb->st_dev = va.va_fsid;
    571 	sb->st_ino = va.va_fileid;
    572 	mode = va.va_mode;
    573 	switch (vp->v_type) {
    574 	case VREG:
    575 		mode |= S_IFREG;
    576 		break;
    577 	case VDIR:
    578 		mode |= S_IFDIR;
    579 		break;
    580 	case VBLK:
    581 		mode |= S_IFBLK;
    582 		break;
    583 	case VCHR:
    584 		mode |= S_IFCHR;
    585 		break;
    586 	case VLNK:
    587 		mode |= S_IFLNK;
    588 		break;
    589 	case VSOCK:
    590 		mode |= S_IFSOCK;
    591 		break;
    592 	case VFIFO:
    593 		mode |= S_IFIFO;
    594 		break;
    595 	default:
    596 		return (EBADF);
    597 	};
    598 	sb->st_mode = mode;
    599 	sb->st_nlink = va.va_nlink;
    600 	sb->st_uid = va.va_uid;
    601 	sb->st_gid = va.va_gid;
    602 	sb->st_rdev = va.va_rdev;
    603 	sb->st_size = va.va_size;
    604 	sb->st_atimespec = va.va_atime;
    605 	sb->st_mtimespec = va.va_mtime;
    606 	sb->st_ctimespec = va.va_ctime;
    607 	sb->st_birthtimespec = va.va_birthtime;
    608 	sb->st_blksize = va.va_blocksize;
    609 	sb->st_flags = va.va_flags;
    610 	sb->st_gen = 0;
    611 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    612 	return (0);
    613 }
    614 
    615 /*
    616  * File table vnode fcntl routine.
    617  */
    618 static int
    619 vn_fcntl(fp, com, data, p)
    620 	struct file *fp;
    621 	u_int com;
    622 	void *data;
    623 	struct proc *p;
    624 {
    625 	struct vnode *vp = ((struct vnode *)fp->f_data);
    626 	int error;
    627 
    628 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    629 	error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    630 	VOP_UNLOCK(vp, 0);
    631 	return (error);
    632 }
    633 
    634 /*
    635  * File table vnode ioctl routine.
    636  */
    637 static int
    638 vn_ioctl(fp, com, data, p)
    639 	struct file *fp;
    640 	u_long com;
    641 	void *data;
    642 	struct proc *p;
    643 {
    644 	struct vnode *vp = ((struct vnode *)fp->f_data);
    645 	struct vattr vattr;
    646 	int error;
    647 
    648 	switch (vp->v_type) {
    649 
    650 	case VREG:
    651 	case VDIR:
    652 		if (com == FIONREAD) {
    653 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    654 			if (error)
    655 				return (error);
    656 			*(int *)data = vattr.va_size - fp->f_offset;
    657 			return (0);
    658 		}
    659 		if (com == FIOGETBMAP) {
    660 			daddr_t *block;
    661 
    662 			if (*(daddr_t *)data < 0)
    663 				return (EINVAL);
    664 			block = (daddr_t *)data;
    665 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
    666 		}
    667 		if (com == OFIOGETBMAP) {
    668 			daddr_t ibn, obn;
    669 
    670 			if (*(int32_t *)data < 0)
    671 				return (EINVAL);
    672 			ibn = (daddr_t)*(int32_t *)data;
    673 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    674 			*(int32_t *)data = (int32_t)obn;
    675 			return error;
    676 		}
    677 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    678 			return (0);			/* XXX */
    679 		/* fall into ... */
    680 
    681 	case VFIFO:
    682 	case VCHR:
    683 	case VBLK:
    684 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    685 		if (error == 0 && com == TIOCSCTTY) {
    686 			if (p->p_session->s_ttyvp)
    687 				vrele(p->p_session->s_ttyvp);
    688 			p->p_session->s_ttyvp = vp;
    689 			VREF(vp);
    690 		}
    691 		return (error);
    692 
    693 	default:
    694 		return (EPASSTHROUGH);
    695 	}
    696 }
    697 
    698 /*
    699  * File table vnode poll routine.
    700  */
    701 static int
    702 vn_poll(fp, events, p)
    703 	struct file *fp;
    704 	int events;
    705 	struct proc *p;
    706 {
    707 
    708 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
    709 }
    710 
    711 /*
    712  * File table vnode kqfilter routine.
    713  */
    714 int
    715 vn_kqfilter(fp, kn)
    716 	struct file *fp;
    717 	struct knote *kn;
    718 {
    719 
    720 	return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
    721 }
    722 
    723 /*
    724  * Check that the vnode is still valid, and if so
    725  * acquire requested lock.
    726  */
    727 int
    728 vn_lock(vp, flags)
    729 	struct vnode *vp;
    730 	int flags;
    731 {
    732 	int error;
    733 
    734 	do {
    735 		if ((flags & LK_INTERLOCK) == 0)
    736 			simple_lock(&vp->v_interlock);
    737 		if (vp->v_flag & VXLOCK) {
    738 			if (flags & LK_NOWAIT) {
    739 				simple_unlock(&vp->v_interlock);
    740 				return EBUSY;
    741 			}
    742 			vp->v_flag |= VXWANT;
    743 			ltsleep(vp, PINOD | PNORELOCK,
    744 			    "vn_lock", 0, &vp->v_interlock);
    745 			error = ENOENT;
    746 		} else {
    747 			error = VOP_LOCK(vp, flags | LK_INTERLOCK);
    748 			if (error == 0 || error == EDEADLK || error == EBUSY)
    749 				return (error);
    750 		}
    751 		flags &= ~LK_INTERLOCK;
    752 	} while (flags & LK_RETRY);
    753 	return (error);
    754 }
    755 
    756 /*
    757  * File table vnode close routine.
    758  */
    759 static int
    760 vn_closefile(fp, p)
    761 	struct file *fp;
    762 	struct proc *p;
    763 {
    764 
    765 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    766 		fp->f_cred, p));
    767 }
    768 
    769 /*
    770  * Enable LK_CANRECURSE on lock. Return prior status.
    771  */
    772 u_int
    773 vn_setrecurse(vp)
    774 	struct vnode *vp;
    775 {
    776 	struct lock *lkp = &vp->v_lock;
    777 	u_int retval = lkp->lk_flags & LK_CANRECURSE;
    778 
    779 	lkp->lk_flags |= LK_CANRECURSE;
    780 	return retval;
    781 }
    782 
    783 /*
    784  * Called when done with locksetrecurse.
    785  */
    786 void
    787 vn_restorerecurse(vp, flags)
    788 	struct vnode *vp;
    789 	u_int flags;
    790 {
    791 	struct lock *lkp = &vp->v_lock;
    792 
    793 	lkp->lk_flags &= ~LK_CANRECURSE;
    794 	lkp->lk_flags |= flags;
    795 }
    796