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