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