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