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