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