Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.140.4.1
      1 /*	$NetBSD: vfs_vnops.c,v 1.140.4.1 2007/10/26 15:48:48 joerg 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.140.4.1 2007/10/26 15:48:48 joerg 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 		simple_lock(&vp->v_interlock);
    208 		vp->v_writecount++;
    209 		simple_unlock(&vp->v_interlock);
    210 	}
    211 
    212 bad:
    213 	if (error)
    214 		vput(vp);
    215 out:
    216 	VERIEXEC_PATH_PUT(path);
    217 	return (error);
    218 }
    219 
    220 /*
    221  * Check for write permissions on the specified vnode.
    222  * Prototype text segments cannot be written.
    223  */
    224 int
    225 vn_writechk(struct vnode *vp)
    226 {
    227 
    228 	/*
    229 	 * If the vnode is in use as a process's text,
    230 	 * we can't allow writing.
    231 	 */
    232 	if (vp->v_iflag & VI_TEXT)
    233 		return (ETXTBSY);
    234 	return (0);
    235 }
    236 
    237 /*
    238  * Mark a vnode as having executable mappings.
    239  */
    240 void
    241 vn_markexec(struct vnode *vp)
    242 {
    243 
    244 	LOCK_ASSERT(simple_lock_held(&vp->v_interlock));
    245 
    246 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
    247 		/* XXXSMP should be atomic */
    248 		uvmexp.filepages -= vp->v_uobj.uo_npages;
    249 		uvmexp.execpages += vp->v_uobj.uo_npages;
    250 	}
    251 	vp->v_iflag |= VI_EXECMAP;
    252 }
    253 
    254 /*
    255  * Mark a vnode as being the text of a process.
    256  * Fail if the vnode is currently writable.
    257  */
    258 int
    259 vn_marktext(struct vnode *vp)
    260 {
    261 
    262 	simple_lock(&vp->v_interlock);
    263 	if (vp->v_writecount != 0) {
    264 		KASSERT((vp->v_iflag & VI_TEXT) == 0);
    265 		simple_unlock(&vp->v_interlock);
    266 		return (ETXTBSY);
    267 	}
    268 	vp->v_iflag |= VI_TEXT;
    269 	vn_markexec(vp);
    270 	simple_unlock(&vp->v_interlock);
    271 	return (0);
    272 }
    273 
    274 /*
    275  * Vnode close call
    276  *
    277  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    278  */
    279 int
    280 vn_close(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
    281 {
    282 	int error;
    283 
    284 	simple_lock(&vp->v_interlock);
    285 	if (flags & FWRITE)
    286 		vp->v_writecount--;
    287 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK);
    288 	error = VOP_CLOSE(vp, flags, cred, l);
    289 	vput(vp);
    290 	return (error);
    291 }
    292 
    293 /*
    294  * Package up an I/O request on a vnode into a uio and do it.
    295  */
    296 int
    297 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
    298     enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
    299     struct lwp *l)
    300 {
    301 	struct uio auio;
    302 	struct iovec aiov;
    303 	int error;
    304 
    305 	if ((ioflg & IO_NODELOCKED) == 0) {
    306 		if (rw == UIO_READ) {
    307 			vn_lock(vp, LK_SHARED | LK_RETRY);
    308 		} else /* UIO_WRITE */ {
    309 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    310 		}
    311 	}
    312 	auio.uio_iov = &aiov;
    313 	auio.uio_iovcnt = 1;
    314 	aiov.iov_base = base;
    315 	aiov.iov_len = len;
    316 	auio.uio_resid = len;
    317 	auio.uio_offset = offset;
    318 	auio.uio_rw = rw;
    319 	if (segflg == UIO_SYSSPACE) {
    320 		UIO_SETUP_SYSSPACE(&auio);
    321 	} else {
    322 		auio.uio_vmspace = l->l_proc->p_vmspace;
    323 	}
    324 	if (rw == UIO_READ) {
    325 		error = VOP_READ(vp, &auio, ioflg, cred);
    326 	} else {
    327 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    328 	}
    329 	if (aresid)
    330 		*aresid = auio.uio_resid;
    331 	else
    332 		if (auio.uio_resid && error == 0)
    333 			error = EIO;
    334 	if ((ioflg & IO_NODELOCKED) == 0) {
    335 		VOP_UNLOCK(vp, 0);
    336 	}
    337 	return (error);
    338 }
    339 
    340 int
    341 vn_readdir(struct file *fp, char *bf, int segflg, u_int count, int *done,
    342     struct lwp *l, off_t **cookies, int *ncookies)
    343 {
    344 	struct vnode *vp = (struct vnode *)fp->f_data;
    345 	struct iovec aiov;
    346 	struct uio auio;
    347 	int error, eofflag;
    348 
    349 	/* Limit the size on any kernel buffers used by VOP_READDIR */
    350 	count = min(MAXBSIZE, count);
    351 
    352 unionread:
    353 	if (vp->v_type != VDIR)
    354 		return (EINVAL);
    355 	aiov.iov_base = bf;
    356 	aiov.iov_len = count;
    357 	auio.uio_iov = &aiov;
    358 	auio.uio_iovcnt = 1;
    359 	auio.uio_rw = UIO_READ;
    360 	if (segflg == UIO_SYSSPACE) {
    361 		UIO_SETUP_SYSSPACE(&auio);
    362 	} else {
    363 		KASSERT(l == curlwp);
    364 		auio.uio_vmspace = l->l_proc->p_vmspace;
    365 	}
    366 	auio.uio_resid = count;
    367 	vn_lock(vp, LK_SHARED | LK_RETRY);
    368 	auio.uio_offset = fp->f_offset;
    369 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    370 		    ncookies);
    371 	mutex_enter(&fp->f_lock);
    372 	fp->f_offset = auio.uio_offset;
    373 	mutex_exit(&fp->f_lock);
    374 	VOP_UNLOCK(vp, 0);
    375 	if (error)
    376 		return (error);
    377 
    378 #if defined(UNION) || defined(LKM)
    379 	if (count == auio.uio_resid && vn_union_readdir_hook) {
    380 		struct vnode *ovp = vp;
    381 
    382 		error = (*vn_union_readdir_hook)(&vp, fp, l);
    383 		if (error)
    384 			return (error);
    385 		if (vp != ovp)
    386 			goto unionread;
    387 	}
    388 #endif /* UNION || LKM */
    389 
    390 	if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
    391 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    392 		struct vnode *tvp = vp;
    393 		vp = vp->v_mount->mnt_vnodecovered;
    394 		VREF(vp);
    395 		mutex_enter(&fp->f_lock);
    396 		fp->f_data = vp;
    397 		fp->f_offset = 0;
    398 		mutex_exit(&fp->f_lock);
    399 		vrele(tvp);
    400 		goto unionread;
    401 	}
    402 	*done = count - auio.uio_resid;
    403 	return error;
    404 }
    405 
    406 /*
    407  * File table vnode read routine.
    408  */
    409 static int
    410 vn_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    411     int flags)
    412 {
    413 	struct vnode *vp = (struct vnode *)fp->f_data;
    414 	int count, error, ioflag;
    415 	struct lwp *l = curlwp;
    416 
    417 	VOP_LEASE(vp, l, cred, LEASE_READ);
    418 	mutex_enter(&fp->f_lock);
    419 	ioflag = IO_ADV_ENCODE(fp->f_advice);
    420 	if (fp->f_flag & FNONBLOCK)
    421 		ioflag |= IO_NDELAY;
    422 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    423 		ioflag |= IO_SYNC;
    424 	if (fp->f_flag & FALTIO)
    425 		ioflag |= IO_ALTSEMANTICS;
    426 	if (fp->f_flag & FDIRECT)
    427 		ioflag |= IO_DIRECT;
    428 	mutex_exit(&fp->f_lock);
    429 	vn_lock(vp, LK_SHARED | LK_RETRY);
    430 	uio->uio_offset = *offset;
    431 	count = uio->uio_resid;
    432 	error = VOP_READ(vp, uio, ioflag, cred);
    433 	if (flags & FOF_UPDATE_OFFSET)
    434 		*offset += count - uio->uio_resid;
    435 	VOP_UNLOCK(vp, 0);
    436 	return (error);
    437 }
    438 
    439 /*
    440  * File table vnode write routine.
    441  */
    442 static int
    443 vn_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    444     int flags)
    445 {
    446 	struct vnode *vp = (struct vnode *)fp->f_data;
    447 	int count, error, ioflag = IO_UNIT;
    448 	struct lwp *l = curlwp;
    449 
    450 	mutex_enter(&fp->f_lock);
    451 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    452 		ioflag |= IO_APPEND;
    453 	if (fp->f_flag & FNONBLOCK)
    454 		ioflag |= IO_NDELAY;
    455 	if (fp->f_flag & FFSYNC ||
    456 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    457 		ioflag |= IO_SYNC;
    458 	else if (fp->f_flag & FDSYNC)
    459 		ioflag |= IO_DSYNC;
    460 	if (fp->f_flag & FALTIO)
    461 		ioflag |= IO_ALTSEMANTICS;
    462 	if (fp->f_flag & FDIRECT)
    463 		ioflag |= IO_DIRECT;
    464 	mutex_exit(&fp->f_lock);
    465 	VOP_LEASE(vp, l, cred, LEASE_WRITE);
    466 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    467 	uio->uio_offset = *offset;
    468 	count = uio->uio_resid;
    469 	error = VOP_WRITE(vp, uio, ioflag, cred);
    470 	if (flags & FOF_UPDATE_OFFSET) {
    471 		if (ioflag & IO_APPEND)
    472 			*offset = uio->uio_offset;
    473 		else
    474 			*offset += count - uio->uio_resid;
    475 	}
    476 	VOP_UNLOCK(vp, 0);
    477 	return (error);
    478 }
    479 
    480 /*
    481  * File table vnode stat routine.
    482  */
    483 static int
    484 vn_statfile(struct file *fp, struct stat *sb, struct lwp *l)
    485 {
    486 	struct vnode *vp = (struct vnode *)fp->f_data;
    487 
    488 	return vn_stat(vp, sb, l);
    489 }
    490 
    491 int
    492 vn_stat(struct vnode *vp, struct stat *sb, struct lwp *l)
    493 {
    494 	struct vattr va;
    495 	int error;
    496 	mode_t mode;
    497 
    498 	error = VOP_GETATTR(vp, &va, l->l_cred, l);
    499 	if (error)
    500 		return (error);
    501 	/*
    502 	 * Copy from vattr table
    503 	 */
    504 	sb->st_dev = va.va_fsid;
    505 	sb->st_ino = va.va_fileid;
    506 	mode = va.va_mode;
    507 	switch (vp->v_type) {
    508 	case VREG:
    509 		mode |= S_IFREG;
    510 		break;
    511 	case VDIR:
    512 		mode |= S_IFDIR;
    513 		break;
    514 	case VBLK:
    515 		mode |= S_IFBLK;
    516 		break;
    517 	case VCHR:
    518 		mode |= S_IFCHR;
    519 		break;
    520 	case VLNK:
    521 		mode |= S_IFLNK;
    522 		break;
    523 	case VSOCK:
    524 		mode |= S_IFSOCK;
    525 		break;
    526 	case VFIFO:
    527 		mode |= S_IFIFO;
    528 		break;
    529 	default:
    530 		return (EBADF);
    531 	};
    532 	sb->st_mode = mode;
    533 	sb->st_nlink = va.va_nlink;
    534 	sb->st_uid = va.va_uid;
    535 	sb->st_gid = va.va_gid;
    536 	sb->st_rdev = va.va_rdev;
    537 	sb->st_size = va.va_size;
    538 	sb->st_atimespec = va.va_atime;
    539 	sb->st_mtimespec = va.va_mtime;
    540 	sb->st_ctimespec = va.va_ctime;
    541 	sb->st_birthtimespec = va.va_birthtime;
    542 	sb->st_blksize = va.va_blocksize;
    543 	sb->st_flags = va.va_flags;
    544 	sb->st_gen = 0;
    545 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    546 	return (0);
    547 }
    548 
    549 /*
    550  * File table vnode fcntl routine.
    551  */
    552 static int
    553 vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l)
    554 {
    555 	struct vnode *vp = ((struct vnode *)fp->f_data);
    556 	int error;
    557 
    558 	error = VOP_FCNTL(vp, com, data, fp->f_flag, l->l_cred, l);
    559 	return (error);
    560 }
    561 
    562 /*
    563  * File table vnode ioctl routine.
    564  */
    565 static int
    566 vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l)
    567 {
    568 	struct vnode *vp = ((struct vnode *)fp->f_data), *ovp;
    569 	struct proc *p = l->l_proc;
    570 	struct vattr vattr;
    571 	int error;
    572 
    573 	switch (vp->v_type) {
    574 
    575 	case VREG:
    576 	case VDIR:
    577 		if (com == FIONREAD) {
    578 			error = VOP_GETATTR(vp, &vattr, l->l_cred, l);
    579 			if (error)
    580 				return (error);
    581 			*(int *)data = vattr.va_size - fp->f_offset;
    582 			return (0);
    583 		}
    584 		if ((com == FIONWRITE) || (com == FIONSPACE)) {
    585 			/*
    586 			 * Files don't have send queues, so there never
    587 			 * are any bytes in them, nor is there any
    588 			 * open space in them.
    589 			 */
    590 			*(int *)data = 0;
    591 			return (0);
    592 		}
    593 		if (com == FIOGETBMAP) {
    594 			daddr_t *block;
    595 
    596 			if (*(daddr_t *)data < 0)
    597 				return (EINVAL);
    598 			block = (daddr_t *)data;
    599 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
    600 		}
    601 		if (com == OFIOGETBMAP) {
    602 			daddr_t ibn, obn;
    603 
    604 			if (*(int32_t *)data < 0)
    605 				return (EINVAL);
    606 			ibn = (daddr_t)*(int32_t *)data;
    607 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    608 			*(int32_t *)data = (int32_t)obn;
    609 			return error;
    610 		}
    611 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    612 			return (0);			/* XXX */
    613 		/* fall into ... */
    614 	case VFIFO:
    615 	case VCHR:
    616 	case VBLK:
    617 		error = VOP_IOCTL(vp, com, data, fp->f_flag,
    618 		    l->l_cred, l);
    619 		if (error == 0 && com == TIOCSCTTY) {
    620 			VREF(vp);
    621 			mutex_enter(&proclist_lock);
    622 			ovp = p->p_session->s_ttyvp;
    623 			p->p_session->s_ttyvp = vp;
    624 			mutex_exit(&proclist_lock);
    625 			if (ovp != NULL)
    626 				vrele(ovp);
    627 		}
    628 		return (error);
    629 
    630 	default:
    631 		return (EPASSTHROUGH);
    632 	}
    633 }
    634 
    635 /*
    636  * File table vnode poll routine.
    637  */
    638 static int
    639 vn_poll(struct file *fp, int events, struct lwp *l)
    640 {
    641 
    642 	return (VOP_POLL(((struct vnode *)fp->f_data), events, l));
    643 }
    644 
    645 /*
    646  * File table vnode kqfilter routine.
    647  */
    648 int
    649 vn_kqfilter(struct file *fp, struct knote *kn)
    650 {
    651 
    652 	return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
    653 }
    654 
    655 /*
    656  * Check that the vnode is still valid, and if so
    657  * acquire requested lock.
    658  */
    659 int
    660 vn_lock(struct vnode *vp, int flags)
    661 {
    662 	int error;
    663 
    664 #if 0
    665 	KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
    666 	    || (vp->v_iflag & VI_ONWORKLST) != 0);
    667 #endif
    668 	KASSERT((flags &
    669 	    ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_DRAIN|LK_NOWAIT|LK_RETRY|
    670 	    LK_SETRECURSE|LK_CANRECURSE))
    671 	    == 0);
    672 
    673 	do {
    674 		if ((flags & LK_INTERLOCK) == 0)
    675 			simple_lock(&vp->v_interlock);
    676 		if (vp->v_iflag & VI_XLOCK) {
    677 			if (flags & LK_NOWAIT) {
    678 				simple_unlock(&vp->v_interlock);
    679 				return EBUSY;
    680 			}
    681 			vp->v_iflag |= VI_XWANT;
    682 			ltsleep(vp, PINOD | PNORELOCK,
    683 			    "vn_lock", 0, &vp->v_interlock);
    684 			error = ENOENT;
    685 		} else {
    686 			error = VOP_LOCK(vp,
    687 			    (flags & ~LK_RETRY) | LK_INTERLOCK);
    688 			if (error == 0 || error == EDEADLK || error == EBUSY)
    689 				return (error);
    690 		}
    691 		flags &= ~LK_INTERLOCK;
    692 	} while (flags & LK_RETRY);
    693 	return (error);
    694 }
    695 
    696 /*
    697  * File table vnode close routine.
    698  */
    699 static int
    700 vn_closefile(struct file *fp, struct lwp *l)
    701 {
    702 
    703 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    704 		fp->f_cred, l));
    705 }
    706 
    707 /*
    708  * Enable LK_CANRECURSE on lock. Return prior status.
    709  */
    710 u_int
    711 vn_setrecurse(struct vnode *vp)
    712 {
    713 	struct lock *lkp = &vp->v_lock;
    714 	u_int retval;
    715 
    716 	simple_lock(&lkp->lk_interlock);
    717 	retval = lkp->lk_flags & LK_CANRECURSE;
    718 	lkp->lk_flags |= LK_CANRECURSE;
    719 	simple_unlock(&lkp->lk_interlock);
    720 
    721 	return retval;
    722 }
    723 
    724 /*
    725  * Called when done with locksetrecurse.
    726  */
    727 void
    728 vn_restorerecurse(struct vnode *vp, u_int flags)
    729 {
    730 	struct lock *lkp = &vp->v_lock;
    731 
    732 	simple_lock(&lkp->lk_interlock);
    733 	lkp->lk_flags &= ~LK_CANRECURSE;
    734 	lkp->lk_flags |= flags;
    735 	simple_unlock(&lkp->lk_interlock);
    736 }
    737 
    738 /*
    739  * Obsolete: this function will be removed from 6.0
    740  * Please use fscow_establish() instead.
    741  */
    742 int
    743 vn_cow_establish(struct vnode *vp,
    744     int (*func)(void *, struct buf *), void *cookie)
    745 {
    746 	static int firstrun = 1;
    747 
    748 	if (firstrun) {
    749 		printf("%s: this function is obsolete.\n", __FUNCTION__);
    750 		firstrun = 0;
    751 	}
    752 	if (vp->v_type == VBLK)
    753 		return fscow_establish(vp->v_specmountpoint, func, cookie);
    754 	else
    755 		return fscow_establish(vp->v_mount, func, cookie);
    756 }
    757 
    758 /*
    759  * Obsolete: this function will be removed from 6.0
    760  * Please use fscow_disestablish() instead.
    761  */
    762 int
    763 vn_cow_disestablish(struct vnode *vp,
    764     int (*func)(void *, struct buf *), void *cookie)
    765 {
    766 	if (vp->v_type == VBLK)
    767 		return fscow_disestablish(vp->v_specmountpoint, func, cookie);
    768 	else
    769 		return fscow_disestablish(vp->v_mount, func, cookie);
    770 }
    771 
    772 /*
    773  * Simplified in-kernel wrapper calls for extended attribute access.
    774  * Both calls pass in a NULL credential, authorizing a "kernel" access.
    775  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
    776  */
    777 int
    778 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
    779     const char *attrname, size_t *buflen, void *bf, struct lwp *l)
    780 {
    781 	struct uio auio;
    782 	struct iovec aiov;
    783 	int error;
    784 
    785 	aiov.iov_len = *buflen;
    786 	aiov.iov_base = bf;
    787 
    788 	auio.uio_iov = &aiov;
    789 	auio.uio_iovcnt = 1;
    790 	auio.uio_rw = UIO_READ;
    791 	auio.uio_offset = 0;
    792 	auio.uio_resid = *buflen;
    793 	UIO_SETUP_SYSSPACE(&auio);
    794 
    795 	if ((ioflg & IO_NODELOCKED) == 0)
    796 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    797 
    798 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
    799 	    l);
    800 
    801 	if ((ioflg & IO_NODELOCKED) == 0)
    802 		VOP_UNLOCK(vp, 0);
    803 
    804 	if (error == 0)
    805 		*buflen = *buflen - auio.uio_resid;
    806 
    807 	return (error);
    808 }
    809 
    810 /*
    811  * XXX Failure mode if partially written?
    812  */
    813 int
    814 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
    815     const char *attrname, size_t buflen, const void *bf, struct lwp *l)
    816 {
    817 	struct uio auio;
    818 	struct iovec aiov;
    819 	int error;
    820 
    821 	aiov.iov_len = buflen;
    822 	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
    823 
    824 	auio.uio_iov = &aiov;
    825 	auio.uio_iovcnt = 1;
    826 	auio.uio_rw = UIO_WRITE;
    827 	auio.uio_offset = 0;
    828 	auio.uio_resid = buflen;
    829 	UIO_SETUP_SYSSPACE(&auio);
    830 
    831 	if ((ioflg & IO_NODELOCKED) == 0) {
    832 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    833 	}
    834 
    835 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, l);
    836 
    837 	if ((ioflg & IO_NODELOCKED) == 0) {
    838 		VOP_UNLOCK(vp, 0);
    839 	}
    840 
    841 	return (error);
    842 }
    843 
    844 int
    845 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
    846     const char *attrname, struct lwp *l)
    847 {
    848 	int error;
    849 
    850 	if ((ioflg & IO_NODELOCKED) == 0) {
    851 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    852 	}
    853 
    854 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, l);
    855 	if (error == EOPNOTSUPP)
    856 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
    857 		    NULL, l);
    858 
    859 	if ((ioflg & IO_NODELOCKED) == 0) {
    860 		VOP_UNLOCK(vp, 0);
    861 	}
    862 
    863 	return (error);
    864 }
    865 
    866 void
    867 vn_ra_allocctx(struct vnode *vp)
    868 {
    869 	struct uvm_ractx *ra = NULL;
    870 
    871 	if (vp->v_type != VREG) {
    872 		return;
    873 	}
    874 	if (vp->v_ractx != NULL) {
    875 		return;
    876 	}
    877 	simple_lock(&vp->v_interlock);
    878 	if (vp->v_ractx == NULL) {
    879 		simple_unlock(&vp->v_interlock);
    880 		ra = uvm_ra_allocctx();
    881 		simple_lock(&vp->v_interlock);
    882 		if (ra != NULL && vp->v_ractx == NULL) {
    883 			vp->v_ractx = ra;
    884 			ra = NULL;
    885 		}
    886 	}
    887 	simple_unlock(&vp->v_interlock);
    888 	if (ra != NULL) {
    889 		uvm_ra_freectx(ra);
    890 	}
    891 }
    892