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