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