Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.64
      1 /*	$NetBSD: vfs_vnops.c,v 1.64 2003/03/03 21:25:09 jdolecek 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)vfs_vnops.c	8.14 (Berkeley) 6/15/95
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.64 2003/03/03 21:25:09 jdolecek Exp $");
     45 
     46 #include "fs_union.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/kernel.h>
     51 #include <sys/file.h>
     52 #include <sys/stat.h>
     53 #include <sys/buf.h>
     54 #include <sys/proc.h>
     55 #include <sys/mount.h>
     56 #include <sys/namei.h>
     57 #include <sys/vnode.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/tty.h>
     60 #include <sys/poll.h>
     61 
     62 #include <uvm/uvm_extern.h>
     63 
     64 #ifdef UNION
     65 #include <miscfs/union/union.h>
     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, caddr_t 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, caddr_t 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 #ifdef UNION
    444 {
    445 	extern struct vnode *union_dircache __P((struct vnode *));
    446 
    447 	if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
    448 		struct vnode *lvp;
    449 
    450 		lvp = union_dircache(vp);
    451 		if (lvp != NULLVP) {
    452 			struct vattr va;
    453 
    454 			/*
    455 			 * If the directory is opaque,
    456 			 * then don't show lower entries
    457 			 */
    458 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
    459 			if (va.va_flags & OPAQUE) {
    460 				vput(lvp);
    461 				lvp = NULL;
    462 			}
    463 		}
    464 
    465 		if (lvp != NULLVP) {
    466 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
    467 			if (error) {
    468 				vput(lvp);
    469 				return (error);
    470 			}
    471 			VOP_UNLOCK(lvp, 0);
    472 			fp->f_data = (caddr_t) lvp;
    473 			fp->f_offset = 0;
    474 			error = vn_close(vp, FREAD, fp->f_cred, p);
    475 			if (error)
    476 				return (error);
    477 			vp = lvp;
    478 			goto unionread;
    479 		}
    480 	}
    481 }
    482 #endif /* UNION */
    483 
    484 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    485 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    486 		struct vnode *tvp = vp;
    487 		vp = vp->v_mount->mnt_vnodecovered;
    488 		VREF(vp);
    489 		fp->f_data = (caddr_t) vp;
    490 		fp->f_offset = 0;
    491 		vrele(tvp);
    492 		goto unionread;
    493 	}
    494 	*done = count - auio.uio_resid;
    495 	return error;
    496 }
    497 
    498 /*
    499  * File table vnode read routine.
    500  */
    501 static int
    502 vn_read(fp, offset, uio, cred, flags)
    503 	struct file *fp;
    504 	off_t *offset;
    505 	struct uio *uio;
    506 	struct ucred *cred;
    507 	int flags;
    508 {
    509 	struct vnode *vp = (struct vnode *)fp->f_data;
    510 	int count, error, ioflag = 0;
    511 
    512 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    513 	if (fp->f_flag & FNONBLOCK)
    514 		ioflag |= IO_NDELAY;
    515 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    516 		ioflag |= IO_SYNC;
    517 	if (fp->f_flag & FALTIO)
    518 		ioflag |= IO_ALTSEMANTICS;
    519 	vn_lock(vp, LK_SHARED | LK_RETRY);
    520 	uio->uio_offset = *offset;
    521 	count = uio->uio_resid;
    522 	error = VOP_READ(vp, uio, ioflag, cred);
    523 	if (flags & FOF_UPDATE_OFFSET)
    524 		*offset += count - uio->uio_resid;
    525 	VOP_UNLOCK(vp, 0);
    526 	return (error);
    527 }
    528 
    529 /*
    530  * File table vnode write routine.
    531  */
    532 static int
    533 vn_write(fp, offset, uio, cred, flags)
    534 	struct file *fp;
    535 	off_t *offset;
    536 	struct uio *uio;
    537 	struct ucred *cred;
    538 	int flags;
    539 {
    540 	struct vnode *vp = (struct vnode *)fp->f_data;
    541 	int count, error, ioflag = IO_UNIT;
    542 
    543 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    544 		ioflag |= IO_APPEND;
    545 	if (fp->f_flag & FNONBLOCK)
    546 		ioflag |= IO_NDELAY;
    547 	if (fp->f_flag & FFSYNC ||
    548 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    549 		ioflag |= IO_SYNC;
    550 	else if (fp->f_flag & FDSYNC)
    551 		ioflag |= IO_DSYNC;
    552 	if (fp->f_flag & FALTIO)
    553 		ioflag |= IO_ALTSEMANTICS;
    554 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
    555 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    556 	uio->uio_offset = *offset;
    557 	count = uio->uio_resid;
    558 	error = VOP_WRITE(vp, uio, ioflag, cred);
    559 	if (flags & FOF_UPDATE_OFFSET) {
    560 		if (ioflag & IO_APPEND)
    561 			*offset = uio->uio_offset;
    562 		else
    563 			*offset += count - uio->uio_resid;
    564 	}
    565 	VOP_UNLOCK(vp, 0);
    566 	return (error);
    567 }
    568 
    569 /*
    570  * File table vnode stat routine.
    571  */
    572 static int
    573 vn_statfile(fp, sb, p)
    574 	struct file *fp;
    575 	struct stat *sb;
    576 	struct proc *p;
    577 {
    578 	struct vnode *vp = (struct vnode *)fp->f_data;
    579 
    580 	return vn_stat(vp, sb, p);
    581 }
    582 
    583 int
    584 vn_stat(vp, sb, p)
    585 	struct vnode *vp;
    586 	struct stat *sb;
    587 	struct proc *p;
    588 {
    589 	struct vattr va;
    590 	int error;
    591 	mode_t mode;
    592 
    593 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
    594 	if (error)
    595 		return (error);
    596 	/*
    597 	 * Copy from vattr table
    598 	 */
    599 	sb->st_dev = va.va_fsid;
    600 	sb->st_ino = va.va_fileid;
    601 	mode = va.va_mode;
    602 	switch (vp->v_type) {
    603 	case VREG:
    604 		mode |= S_IFREG;
    605 		break;
    606 	case VDIR:
    607 		mode |= S_IFDIR;
    608 		break;
    609 	case VBLK:
    610 		mode |= S_IFBLK;
    611 		break;
    612 	case VCHR:
    613 		mode |= S_IFCHR;
    614 		break;
    615 	case VLNK:
    616 		mode |= S_IFLNK;
    617 		break;
    618 	case VSOCK:
    619 		mode |= S_IFSOCK;
    620 		break;
    621 	case VFIFO:
    622 		mode |= S_IFIFO;
    623 		break;
    624 	default:
    625 		return (EBADF);
    626 	};
    627 	sb->st_mode = mode;
    628 	sb->st_nlink = va.va_nlink;
    629 	sb->st_uid = va.va_uid;
    630 	sb->st_gid = va.va_gid;
    631 	sb->st_rdev = va.va_rdev;
    632 	sb->st_size = va.va_size;
    633 	sb->st_atimespec = va.va_atime;
    634 	sb->st_mtimespec = va.va_mtime;
    635 	sb->st_ctimespec = va.va_ctime;
    636 	sb->st_blksize = va.va_blocksize;
    637 	sb->st_flags = va.va_flags;
    638 	sb->st_gen = 0;
    639 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    640 	return (0);
    641 }
    642 
    643 /*
    644  * File table vnode fcntl routine.
    645  */
    646 static int
    647 vn_fcntl(fp, com, data, p)
    648 	struct file *fp;
    649 	u_int com;
    650 	caddr_t data;
    651 	struct proc *p;
    652 {
    653 	struct vnode *vp = ((struct vnode *)fp->f_data);
    654 	int error;
    655 
    656 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    657 	error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    658 	VOP_UNLOCK(vp, 0);
    659 	return (error);
    660 }
    661 
    662 /*
    663  * File table vnode ioctl routine.
    664  */
    665 static int
    666 vn_ioctl(fp, com, data, p)
    667 	struct file *fp;
    668 	u_long com;
    669 	caddr_t data;
    670 	struct proc *p;
    671 {
    672 	struct vnode *vp = ((struct vnode *)fp->f_data);
    673 	struct vattr vattr;
    674 	int error;
    675 
    676 	switch (vp->v_type) {
    677 
    678 	case VREG:
    679 	case VDIR:
    680 		if (com == FIONREAD) {
    681 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    682 			if (error)
    683 				return (error);
    684 			*(int *)data = vattr.va_size - fp->f_offset;
    685 			return (0);
    686 		}
    687 		if (com == FIOGETBMAP) {
    688 			daddr_t *block;
    689 
    690 			if (*(daddr_t *)data < 0)
    691 				return (EINVAL);
    692 			block = (daddr_t *)data;
    693 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
    694 		}
    695 		if (com == OFIOGETBMAP) {
    696 			daddr_t ibn, obn;
    697 
    698 			if (*(int32_t *)data < 0)
    699 				return (EINVAL);
    700 			ibn = (daddr_t)*(int32_t *)data;
    701 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    702 			*(int32_t *)data = (int32_t)obn;
    703 			return error;
    704 		}
    705 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    706 			return (0);			/* XXX */
    707 		/* fall into ... */
    708 
    709 	case VFIFO:
    710 	case VCHR:
    711 	case VBLK:
    712 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    713 		if (error == 0 && com == TIOCSCTTY) {
    714 			if (p->p_session->s_ttyvp)
    715 				vrele(p->p_session->s_ttyvp);
    716 			p->p_session->s_ttyvp = vp;
    717 			VREF(vp);
    718 		}
    719 		return (error);
    720 
    721 	default:
    722 		return (EPASSTHROUGH);
    723 	}
    724 }
    725 
    726 /*
    727  * File table vnode poll routine.
    728  */
    729 static int
    730 vn_poll(fp, events, p)
    731 	struct file *fp;
    732 	int events;
    733 	struct proc *p;
    734 {
    735 
    736 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
    737 }
    738 
    739 /*
    740  * File table vnode kqfilter routine.
    741  */
    742 int
    743 vn_kqfilter(fp, kn)
    744 	struct file *fp;
    745 	struct knote *kn;
    746 {
    747 
    748 	return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
    749 }
    750 
    751 /*
    752  * Check that the vnode is still valid, and if so
    753  * acquire requested lock.
    754  */
    755 int
    756 vn_lock(vp, flags)
    757 	struct vnode *vp;
    758 	int flags;
    759 {
    760 	int error;
    761 
    762 	do {
    763 		if ((flags & LK_INTERLOCK) == 0)
    764 			simple_lock(&vp->v_interlock);
    765 		if (vp->v_flag & VXLOCK) {
    766 			if (flags & LK_NOWAIT) {
    767 				simple_unlock(&vp->v_interlock);
    768 				return EBUSY;
    769 			}
    770 			vp->v_flag |= VXWANT;
    771 			ltsleep(vp, PINOD | PNORELOCK,
    772 			    "vn_lock", 0, &vp->v_interlock);
    773 			error = ENOENT;
    774 		} else {
    775 			error = VOP_LOCK(vp, flags | LK_INTERLOCK);
    776 			if (error == 0 || error == EDEADLK || error == EBUSY)
    777 				return (error);
    778 		}
    779 		flags &= ~LK_INTERLOCK;
    780 	} while (flags & LK_RETRY);
    781 	return (error);
    782 }
    783 
    784 /*
    785  * File table vnode close routine.
    786  */
    787 static int
    788 vn_closefile(fp, p)
    789 	struct file *fp;
    790 	struct proc *p;
    791 {
    792 
    793 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    794 		fp->f_cred, p));
    795 }
    796 
    797 /*
    798  * Enable LK_CANRECURSE on lock. Return prior status.
    799  */
    800 u_int
    801 vn_setrecurse(vp)
    802 	struct vnode *vp;
    803 {
    804 	struct lock *lkp = &vp->v_lock;
    805 	u_int retval = lkp->lk_flags & LK_CANRECURSE;
    806 
    807 	lkp->lk_flags |= LK_CANRECURSE;
    808 	return retval;
    809 }
    810 
    811 /*
    812  * Called when done with locksetrecurse.
    813  */
    814 void
    815 vn_restorerecurse(vp, flags)
    816 	struct vnode *vp;
    817 	u_int flags;
    818 {
    819 	struct lock *lkp = &vp->v_lock;
    820 
    821 	lkp->lk_flags &= ~LK_CANRECURSE;
    822 	lkp->lk_flags |= flags;
    823 }
    824