Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.141
      1 /*	$NetBSD: vfs_vnops.c,v 1.141 2007/10/07 13:39:04 hannken 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.141 2007/10/07 13:39:04 hannken Exp $");
     41 
     42 #include "fs_union.h"
     43 #include "veriexec.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/file.h>
     49 #include <sys/stat.h>
     50 #include <sys/buf.h>
     51 #include <sys/proc.h>
     52 #include <sys/malloc.h>
     53 #include <sys/mount.h>
     54 #include <sys/namei.h>
     55 #include <sys/vnode.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/tty.h>
     58 #include <sys/poll.h>
     59 #include <sys/kauth.h>
     60 #include <sys/syslog.h>
     61 #include <sys/fstrans.h>
     62 
     63 #include <miscfs/specfs/specdev.h>
     64 
     65 #include <uvm/uvm_extern.h>
     66 #include <uvm/uvm_readahead.h>
     67 
     68 #ifdef UNION
     69 #include <fs/union/union.h>
     70 #endif
     71 
     72 #if defined(LKM) || defined(UNION)
     73 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
     74 #endif
     75 
     76 #include <sys/verified_exec.h>
     77 
     78 static int vn_read(struct file *fp, off_t *offset, struct uio *uio,
     79 	    kauth_cred_t cred, int flags);
     80 static int vn_write(struct file *fp, off_t *offset, struct uio *uio,
     81 	    kauth_cred_t cred, int flags);
     82 static int vn_closefile(struct file *fp, struct lwp *l);
     83 static int vn_poll(struct file *fp, int events, struct lwp *l);
     84 static int vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l);
     85 static int vn_statfile(struct file *fp, struct stat *sb, struct lwp *l);
     86 static int vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l);
     87 
     88 const struct fileops vnops = {
     89 	vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
     90 	vn_statfile, vn_closefile, vn_kqfilter
     91 };
     92 
     93 /*
     94  * Common code for vnode open operations.
     95  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
     96  */
     97 int
     98 vn_open(struct nameidata *ndp, int fmode, int cmode)
     99 {
    100 	struct vnode *vp;
    101 	struct lwp *l = ndp->ni_cnd.cn_lwp;
    102 	kauth_cred_t cred = l->l_cred;
    103 	struct vattr va;
    104 	int error;
    105 	char *path;
    106 
    107 	ndp->ni_cnd.cn_flags &= TRYEMULROOT;
    108 
    109 	if (fmode & O_CREAT) {
    110 		ndp->ni_cnd.cn_nameiop = CREATE;
    111 		ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF;
    112 		if ((fmode & O_EXCL) == 0 &&
    113 		    ((fmode & O_NOFOLLOW) == 0))
    114 			ndp->ni_cnd.cn_flags |= FOLLOW;
    115 	} else {
    116 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    117 		ndp->ni_cnd.cn_flags |= LOCKLEAF;
    118 		if ((fmode & O_NOFOLLOW) == 0)
    119 			ndp->ni_cnd.cn_flags |= FOLLOW;
    120 	}
    121 
    122 	VERIEXEC_PATH_GET(ndp->ni_dirp, ndp->ni_segflg, ndp->ni_dirp, path);
    123 
    124 	error = namei(ndp);
    125 	if (error)
    126 		goto out;
    127 
    128 	vp = ndp->ni_vp;
    129 
    130 #if NVERIEXEC > 0
    131 	error = veriexec_openchk(l, ndp->ni_vp, ndp->ni_dirp, fmode);
    132 	if (error)
    133 		goto bad;
    134 #endif /* NVERIEXEC > 0 */
    135 
    136 	if (fmode & O_CREAT) {
    137 		if (ndp->ni_vp == NULL) {
    138 			VATTR_NULL(&va);
    139 			va.va_type = VREG;
    140 			va.va_mode = cmode;
    141 			if (fmode & O_EXCL)
    142 				 va.va_vaflags |= VA_EXCLUSIVE;
    143 			VOP_LEASE(ndp->ni_dvp, l, cred, LEASE_WRITE);
    144 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
    145 					   &ndp->ni_cnd, &va);
    146 			if (error)
    147 				goto out;
    148 			fmode &= ~O_TRUNC;
    149 			vp = ndp->ni_vp;
    150 		} else {
    151 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    152 			if (ndp->ni_dvp == ndp->ni_vp)
    153 				vrele(ndp->ni_dvp);
    154 			else
    155 				vput(ndp->ni_dvp);
    156 			ndp->ni_dvp = NULL;
    157 			vp = ndp->ni_vp;
    158 			if (fmode & O_EXCL) {
    159 				error = EEXIST;
    160 				goto bad;
    161 			}
    162 			fmode &= ~O_CREAT;
    163 		}
    164 	} else {
    165 		vp = ndp->ni_vp;
    166 	}
    167 	if (vp->v_type == VSOCK) {
    168 		error = EOPNOTSUPP;
    169 		goto bad;
    170 	}
    171 	if (ndp->ni_vp->v_type == VLNK) {
    172 		error = EFTYPE;
    173 		goto bad;
    174 	}
    175 
    176 	if ((fmode & O_CREAT) == 0) {
    177 		if (fmode & FREAD) {
    178 			if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
    179 				goto bad;
    180 		}
    181 
    182 		if (fmode & (FWRITE | O_TRUNC)) {
    183 			if (vp->v_type == VDIR) {
    184 				error = EISDIR;
    185 				goto bad;
    186 			}
    187 			if ((error = vn_writechk(vp)) != 0 ||
    188 			    (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
    189 				goto bad;
    190 		}
    191 	}
    192 
    193 	if (fmode & O_TRUNC) {
    194 		VOP_UNLOCK(vp, 0);			/* XXX */
    195 
    196 		VOP_LEASE(vp, l, cred, LEASE_WRITE);
    197 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
    198 		VATTR_NULL(&va);
    199 		va.va_size = 0;
    200 		error = VOP_SETATTR(vp, &va, cred, l);
    201 		if (error != 0)
    202 			goto bad;
    203 	}
    204 	if ((error = VOP_OPEN(vp, fmode, cred, l)) != 0)
    205 		goto bad;
    206 	if (fmode & FWRITE)
    207 		vp->v_writecount++;
    208 
    209 bad:
    210 	if (error)
    211 		vput(vp);
    212 out:
    213 	VERIEXEC_PATH_PUT(path);
    214 	return (error);
    215 }
    216 
    217 /*
    218  * Check for write permissions on the specified vnode.
    219  * Prototype text segments cannot be written.
    220  */
    221 int
    222 vn_writechk(struct vnode *vp)
    223 {
    224 
    225 	/*
    226 	 * If the vnode is in use as a process's text,
    227 	 * we can't allow writing.
    228 	 */
    229 	if (vp->v_flag & VTEXT)
    230 		return (ETXTBSY);
    231 	return (0);
    232 }
    233 
    234 /*
    235  * Mark a vnode as having executable mappings.
    236  */
    237 void
    238 vn_markexec(struct vnode *vp)
    239 {
    240 	if ((vp->v_flag & VEXECMAP) == 0) {
    241 		uvmexp.filepages -= vp->v_uobj.uo_npages;
    242 		uvmexp.execpages += vp->v_uobj.uo_npages;
    243 	}
    244 	vp->v_flag |= VEXECMAP;
    245 }
    246 
    247 /*
    248  * Mark a vnode as being the text of a process.
    249  * Fail if the vnode is currently writable.
    250  */
    251 int
    252 vn_marktext(struct vnode *vp)
    253 {
    254 
    255 	if (vp->v_writecount != 0) {
    256 		KASSERT((vp->v_flag & VTEXT) == 0);
    257 		return (ETXTBSY);
    258 	}
    259 	vp->v_flag |= VTEXT;
    260 	vn_markexec(vp);
    261 	return (0);
    262 }
    263 
    264 /*
    265  * Vnode close call
    266  *
    267  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    268  */
    269 int
    270 vn_close(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
    271 {
    272 	int error;
    273 
    274 	if (flags & FWRITE)
    275 		vp->v_writecount--;
    276 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    277 	error = VOP_CLOSE(vp, flags, cred, l);
    278 	vput(vp);
    279 	return (error);
    280 }
    281 
    282 /*
    283  * Package up an I/O request on a vnode into a uio and do it.
    284  */
    285 int
    286 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
    287     enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
    288     struct lwp *l)
    289 {
    290 	struct uio auio;
    291 	struct iovec aiov;
    292 	int error;
    293 
    294 	if ((ioflg & IO_NODELOCKED) == 0) {
    295 		if (rw == UIO_READ) {
    296 			vn_lock(vp, LK_SHARED | LK_RETRY);
    297 		} else /* UIO_WRITE */ {
    298 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    299 		}
    300 	}
    301 	auio.uio_iov = &aiov;
    302 	auio.uio_iovcnt = 1;
    303 	aiov.iov_base = base;
    304 	aiov.iov_len = len;
    305 	auio.uio_resid = len;
    306 	auio.uio_offset = offset;
    307 	auio.uio_rw = rw;
    308 	if (segflg == UIO_SYSSPACE) {
    309 		UIO_SETUP_SYSSPACE(&auio);
    310 	} else {
    311 		auio.uio_vmspace = l->l_proc->p_vmspace;
    312 	}
    313 	if (rw == UIO_READ) {
    314 		error = VOP_READ(vp, &auio, ioflg, cred);
    315 	} else {
    316 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    317 	}
    318 	if (aresid)
    319 		*aresid = auio.uio_resid;
    320 	else
    321 		if (auio.uio_resid && error == 0)
    322 			error = EIO;
    323 	if ((ioflg & IO_NODELOCKED) == 0) {
    324 		VOP_UNLOCK(vp, 0);
    325 	}
    326 	return (error);
    327 }
    328 
    329 int
    330 vn_readdir(struct file *fp, char *bf, int segflg, u_int count, int *done,
    331     struct lwp *l, off_t **cookies, int *ncookies)
    332 {
    333 	struct vnode *vp = (struct vnode *)fp->f_data;
    334 	struct iovec aiov;
    335 	struct uio auio;
    336 	int error, eofflag;
    337 
    338 	/* Limit the size on any kernel buffers used by VOP_READDIR */
    339 	count = min(MAXBSIZE, count);
    340 
    341 unionread:
    342 	if (vp->v_type != VDIR)
    343 		return (EINVAL);
    344 	aiov.iov_base = bf;
    345 	aiov.iov_len = count;
    346 	auio.uio_iov = &aiov;
    347 	auio.uio_iovcnt = 1;
    348 	auio.uio_rw = UIO_READ;
    349 	if (segflg == UIO_SYSSPACE) {
    350 		UIO_SETUP_SYSSPACE(&auio);
    351 	} else {
    352 		KASSERT(l == curlwp);
    353 		auio.uio_vmspace = l->l_proc->p_vmspace;
    354 	}
    355 	auio.uio_resid = count;
    356 	vn_lock(vp, LK_SHARED | LK_RETRY);
    357 	auio.uio_offset = fp->f_offset;
    358 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    359 		    ncookies);
    360 	fp->f_offset = auio.uio_offset;
    361 	VOP_UNLOCK(vp, 0);
    362 	if (error)
    363 		return (error);
    364 
    365 #if defined(UNION) || defined(LKM)
    366 	if (count == auio.uio_resid && vn_union_readdir_hook) {
    367 		struct vnode *ovp = vp;
    368 
    369 		error = (*vn_union_readdir_hook)(&vp, fp, l);
    370 		if (error)
    371 			return (error);
    372 		if (vp != ovp)
    373 			goto unionread;
    374 	}
    375 #endif /* UNION || LKM */
    376 
    377 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    378 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    379 		struct vnode *tvp = vp;
    380 		vp = vp->v_mount->mnt_vnodecovered;
    381 		VREF(vp);
    382 		fp->f_data = vp;
    383 		fp->f_offset = 0;
    384 		vrele(tvp);
    385 		goto unionread;
    386 	}
    387 	*done = count - auio.uio_resid;
    388 	return error;
    389 }
    390 
    391 /*
    392  * File table vnode read routine.
    393  */
    394 static int
    395 vn_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    396     int flags)
    397 {
    398 	struct vnode *vp = (struct vnode *)fp->f_data;
    399 	int count, error, ioflag;
    400 	struct lwp *l = curlwp;
    401 
    402 	VOP_LEASE(vp, l, cred, LEASE_READ);
    403 	ioflag = IO_ADV_ENCODE(fp->f_advice);
    404 	if (fp->f_flag & FNONBLOCK)
    405 		ioflag |= IO_NDELAY;
    406 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    407 		ioflag |= IO_SYNC;
    408 	if (fp->f_flag & FALTIO)
    409 		ioflag |= IO_ALTSEMANTICS;
    410 	if (fp->f_flag & FDIRECT)
    411 		ioflag |= IO_DIRECT;
    412 	vn_lock(vp, LK_SHARED | LK_RETRY);
    413 	uio->uio_offset = *offset;
    414 	count = uio->uio_resid;
    415 	error = VOP_READ(vp, uio, ioflag, cred);
    416 	if (flags & FOF_UPDATE_OFFSET)
    417 		*offset += count - uio->uio_resid;
    418 	VOP_UNLOCK(vp, 0);
    419 	return (error);
    420 }
    421 
    422 /*
    423  * File table vnode write routine.
    424  */
    425 static int
    426 vn_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    427     int flags)
    428 {
    429 	struct vnode *vp = (struct vnode *)fp->f_data;
    430 	int count, error, ioflag = IO_UNIT;
    431 	struct lwp *l = curlwp;
    432 
    433 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    434 		ioflag |= IO_APPEND;
    435 	if (fp->f_flag & FNONBLOCK)
    436 		ioflag |= IO_NDELAY;
    437 	if (fp->f_flag & FFSYNC ||
    438 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    439 		ioflag |= IO_SYNC;
    440 	else if (fp->f_flag & FDSYNC)
    441 		ioflag |= IO_DSYNC;
    442 	if (fp->f_flag & FALTIO)
    443 		ioflag |= IO_ALTSEMANTICS;
    444 	if (fp->f_flag & FDIRECT)
    445 		ioflag |= IO_DIRECT;
    446 	VOP_LEASE(vp, l, cred, LEASE_WRITE);
    447 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    448 	uio->uio_offset = *offset;
    449 	count = uio->uio_resid;
    450 	error = VOP_WRITE(vp, uio, ioflag, cred);
    451 	if (flags & FOF_UPDATE_OFFSET) {
    452 		if (ioflag & IO_APPEND)
    453 			*offset = uio->uio_offset;
    454 		else
    455 			*offset += count - uio->uio_resid;
    456 	}
    457 	VOP_UNLOCK(vp, 0);
    458 	return (error);
    459 }
    460 
    461 /*
    462  * File table vnode stat routine.
    463  */
    464 static int
    465 vn_statfile(struct file *fp, struct stat *sb, struct lwp *l)
    466 {
    467 	struct vnode *vp = (struct vnode *)fp->f_data;
    468 
    469 	return vn_stat(vp, sb, l);
    470 }
    471 
    472 int
    473 vn_stat(struct vnode *vp, struct stat *sb, struct lwp *l)
    474 {
    475 	struct vattr va;
    476 	int error;
    477 	mode_t mode;
    478 
    479 	error = VOP_GETATTR(vp, &va, l->l_cred, l);
    480 	if (error)
    481 		return (error);
    482 	/*
    483 	 * Copy from vattr table
    484 	 */
    485 	sb->st_dev = va.va_fsid;
    486 	sb->st_ino = va.va_fileid;
    487 	mode = va.va_mode;
    488 	switch (vp->v_type) {
    489 	case VREG:
    490 		mode |= S_IFREG;
    491 		break;
    492 	case VDIR:
    493 		mode |= S_IFDIR;
    494 		break;
    495 	case VBLK:
    496 		mode |= S_IFBLK;
    497 		break;
    498 	case VCHR:
    499 		mode |= S_IFCHR;
    500 		break;
    501 	case VLNK:
    502 		mode |= S_IFLNK;
    503 		break;
    504 	case VSOCK:
    505 		mode |= S_IFSOCK;
    506 		break;
    507 	case VFIFO:
    508 		mode |= S_IFIFO;
    509 		break;
    510 	default:
    511 		return (EBADF);
    512 	};
    513 	sb->st_mode = mode;
    514 	sb->st_nlink = va.va_nlink;
    515 	sb->st_uid = va.va_uid;
    516 	sb->st_gid = va.va_gid;
    517 	sb->st_rdev = va.va_rdev;
    518 	sb->st_size = va.va_size;
    519 	sb->st_atimespec = va.va_atime;
    520 	sb->st_mtimespec = va.va_mtime;
    521 	sb->st_ctimespec = va.va_ctime;
    522 	sb->st_birthtimespec = va.va_birthtime;
    523 	sb->st_blksize = va.va_blocksize;
    524 	sb->st_flags = va.va_flags;
    525 	sb->st_gen = 0;
    526 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    527 	return (0);
    528 }
    529 
    530 /*
    531  * File table vnode fcntl routine.
    532  */
    533 static int
    534 vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l)
    535 {
    536 	struct vnode *vp = ((struct vnode *)fp->f_data);
    537 	int error;
    538 
    539 	error = VOP_FCNTL(vp, com, data, fp->f_flag, l->l_cred, l);
    540 	return (error);
    541 }
    542 
    543 /*
    544  * File table vnode ioctl routine.
    545  */
    546 static int
    547 vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l)
    548 {
    549 	struct vnode *vp = ((struct vnode *)fp->f_data), *ovp;
    550 	struct proc *p = l->l_proc;
    551 	struct vattr vattr;
    552 	int error;
    553 
    554 	switch (vp->v_type) {
    555 
    556 	case VREG:
    557 	case VDIR:
    558 		if (com == FIONREAD) {
    559 			error = VOP_GETATTR(vp, &vattr, l->l_cred, l);
    560 			if (error)
    561 				return (error);
    562 			*(int *)data = vattr.va_size - fp->f_offset;
    563 			return (0);
    564 		}
    565 		if ((com == FIONWRITE) || (com == FIONSPACE)) {
    566 			/*
    567 			 * Files don't have send queues, so there never
    568 			 * are any bytes in them, nor is there any
    569 			 * open space in them.
    570 			 */
    571 			*(int *)data = 0;
    572 			return (0);
    573 		}
    574 		if (com == FIOGETBMAP) {
    575 			daddr_t *block;
    576 
    577 			if (*(daddr_t *)data < 0)
    578 				return (EINVAL);
    579 			block = (daddr_t *)data;
    580 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
    581 		}
    582 		if (com == OFIOGETBMAP) {
    583 			daddr_t ibn, obn;
    584 
    585 			if (*(int32_t *)data < 0)
    586 				return (EINVAL);
    587 			ibn = (daddr_t)*(int32_t *)data;
    588 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    589 			*(int32_t *)data = (int32_t)obn;
    590 			return error;
    591 		}
    592 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    593 			return (0);			/* XXX */
    594 		/* fall into ... */
    595 	case VFIFO:
    596 	case VCHR:
    597 	case VBLK:
    598 		error = VOP_IOCTL(vp, com, data, fp->f_flag,
    599 		    l->l_cred, l);
    600 		if (error == 0 && com == TIOCSCTTY) {
    601 			VREF(vp);
    602 			mutex_enter(&proclist_lock);
    603 			ovp = p->p_session->s_ttyvp;
    604 			p->p_session->s_ttyvp = vp;
    605 			mutex_exit(&proclist_lock);
    606 			if (ovp != NULL)
    607 				vrele(ovp);
    608 		}
    609 		return (error);
    610 
    611 	default:
    612 		return (EPASSTHROUGH);
    613 	}
    614 }
    615 
    616 /*
    617  * File table vnode poll routine.
    618  */
    619 static int
    620 vn_poll(struct file *fp, int events, struct lwp *l)
    621 {
    622 
    623 	return (VOP_POLL(((struct vnode *)fp->f_data), events, l));
    624 }
    625 
    626 /*
    627  * File table vnode kqfilter routine.
    628  */
    629 int
    630 vn_kqfilter(struct file *fp, struct knote *kn)
    631 {
    632 
    633 	return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
    634 }
    635 
    636 /*
    637  * Check that the vnode is still valid, and if so
    638  * acquire requested lock.
    639  */
    640 int
    641 vn_lock(struct vnode *vp, int flags)
    642 {
    643 	int error;
    644 
    645 #if 0
    646 	KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
    647 	    || (vp->v_flag & VONWORKLST) != 0);
    648 #endif
    649 	KASSERT((flags &
    650 	    ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_DRAIN|LK_NOWAIT|LK_RETRY|
    651 	    LK_SETRECURSE|LK_CANRECURSE))
    652 	    == 0);
    653 
    654 	do {
    655 		if ((flags & LK_INTERLOCK) == 0)
    656 			simple_lock(&vp->v_interlock);
    657 		if (vp->v_flag & VXLOCK) {
    658 			if (flags & LK_NOWAIT) {
    659 				simple_unlock(&vp->v_interlock);
    660 				return EBUSY;
    661 			}
    662 			vp->v_flag |= VXWANT;
    663 			ltsleep(vp, PINOD | PNORELOCK,
    664 			    "vn_lock", 0, &vp->v_interlock);
    665 			error = ENOENT;
    666 		} else {
    667 			error = VOP_LOCK(vp,
    668 			    (flags & ~LK_RETRY) | LK_INTERLOCK);
    669 			if (error == 0 || error == EDEADLK || error == EBUSY)
    670 				return (error);
    671 		}
    672 		flags &= ~LK_INTERLOCK;
    673 	} while (flags & LK_RETRY);
    674 	return (error);
    675 }
    676 
    677 /*
    678  * File table vnode close routine.
    679  */
    680 static int
    681 vn_closefile(struct file *fp, struct lwp *l)
    682 {
    683 
    684 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    685 		fp->f_cred, l));
    686 }
    687 
    688 /*
    689  * Enable LK_CANRECURSE on lock. Return prior status.
    690  */
    691 u_int
    692 vn_setrecurse(struct vnode *vp)
    693 {
    694 	struct lock *lkp = &vp->v_lock;
    695 	u_int retval = lkp->lk_flags & LK_CANRECURSE;
    696 
    697 	lkp->lk_flags |= LK_CANRECURSE;
    698 	return retval;
    699 }
    700 
    701 /*
    702  * Called when done with locksetrecurse.
    703  */
    704 void
    705 vn_restorerecurse(struct vnode *vp, u_int flags)
    706 {
    707 	struct lock *lkp = &vp->v_lock;
    708 
    709 	lkp->lk_flags &= ~LK_CANRECURSE;
    710 	lkp->lk_flags |= flags;
    711 }
    712 
    713 /*
    714  * Obsolete: this function will be removed from 6.0
    715  * Please use fscow_establish() instead.
    716  */
    717 int
    718 vn_cow_establish(struct vnode *vp,
    719     int (*func)(void *, struct buf *), void *cookie)
    720 {
    721 	static int firstrun = 1;
    722 
    723 	if (firstrun) {
    724 		printf("%s: this function is obsolete.\n", __FUNCTION__);
    725 		firstrun = 0;
    726 	}
    727 	if (vp->v_type == VBLK)
    728 		return fscow_establish(vp->v_specmountpoint, func, cookie);
    729 	else
    730 		return fscow_establish(vp->v_mount, func, cookie);
    731 }
    732 
    733 /*
    734  * Obsolete: this function will be removed from 6.0
    735  * Please use fscow_disestablish() instead.
    736  */
    737 int
    738 vn_cow_disestablish(struct vnode *vp,
    739     int (*func)(void *, struct buf *), void *cookie)
    740 {
    741 	if (vp->v_type == VBLK)
    742 		return fscow_disestablish(vp->v_specmountpoint, func, cookie);
    743 	else
    744 		return fscow_disestablish(vp->v_mount, func, cookie);
    745 }
    746 
    747 /*
    748  * Simplified in-kernel wrapper calls for extended attribute access.
    749  * Both calls pass in a NULL credential, authorizing a "kernel" access.
    750  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
    751  */
    752 int
    753 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
    754     const char *attrname, size_t *buflen, void *bf, struct lwp *l)
    755 {
    756 	struct uio auio;
    757 	struct iovec aiov;
    758 	int error;
    759 
    760 	aiov.iov_len = *buflen;
    761 	aiov.iov_base = bf;
    762 
    763 	auio.uio_iov = &aiov;
    764 	auio.uio_iovcnt = 1;
    765 	auio.uio_rw = UIO_READ;
    766 	auio.uio_offset = 0;
    767 	auio.uio_resid = *buflen;
    768 	UIO_SETUP_SYSSPACE(&auio);
    769 
    770 	if ((ioflg & IO_NODELOCKED) == 0)
    771 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    772 
    773 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
    774 	    l);
    775 
    776 	if ((ioflg & IO_NODELOCKED) == 0)
    777 		VOP_UNLOCK(vp, 0);
    778 
    779 	if (error == 0)
    780 		*buflen = *buflen - auio.uio_resid;
    781 
    782 	return (error);
    783 }
    784 
    785 /*
    786  * XXX Failure mode if partially written?
    787  */
    788 int
    789 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
    790     const char *attrname, size_t buflen, const void *bf, struct lwp *l)
    791 {
    792 	struct uio auio;
    793 	struct iovec aiov;
    794 	int error;
    795 
    796 	aiov.iov_len = buflen;
    797 	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
    798 
    799 	auio.uio_iov = &aiov;
    800 	auio.uio_iovcnt = 1;
    801 	auio.uio_rw = UIO_WRITE;
    802 	auio.uio_offset = 0;
    803 	auio.uio_resid = buflen;
    804 	UIO_SETUP_SYSSPACE(&auio);
    805 
    806 	if ((ioflg & IO_NODELOCKED) == 0) {
    807 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    808 	}
    809 
    810 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, l);
    811 
    812 	if ((ioflg & IO_NODELOCKED) == 0) {
    813 		VOP_UNLOCK(vp, 0);
    814 	}
    815 
    816 	return (error);
    817 }
    818 
    819 int
    820 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
    821     const char *attrname, struct lwp *l)
    822 {
    823 	int error;
    824 
    825 	if ((ioflg & IO_NODELOCKED) == 0) {
    826 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    827 	}
    828 
    829 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, l);
    830 	if (error == EOPNOTSUPP)
    831 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
    832 		    NULL, l);
    833 
    834 	if ((ioflg & IO_NODELOCKED) == 0) {
    835 		VOP_UNLOCK(vp, 0);
    836 	}
    837 
    838 	return (error);
    839 }
    840 
    841 void
    842 vn_ra_allocctx(struct vnode *vp)
    843 {
    844 	struct uvm_ractx *ra = NULL;
    845 
    846 	if (vp->v_type != VREG) {
    847 		return;
    848 	}
    849 	if (vp->v_ractx != NULL) {
    850 		return;
    851 	}
    852 	simple_lock(&vp->v_interlock);
    853 	if (vp->v_ractx == NULL) {
    854 		simple_unlock(&vp->v_interlock);
    855 		ra = uvm_ra_allocctx();
    856 		simple_lock(&vp->v_interlock);
    857 		if (ra != NULL && vp->v_ractx == NULL) {
    858 			vp->v_ractx = ra;
    859 			ra = NULL;
    860 		}
    861 	}
    862 	simple_unlock(&vp->v_interlock);
    863 	if (ra != NULL) {
    864 		uvm_ra_freectx(ra);
    865 	}
    866 }
    867