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