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