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