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