Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.72.2.3
      1 /*	$NetBSD: vfs_vnops.c,v 1.72.2.3 2004/08/03 10:53:02 skrll 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.72.2.3 2004/08/03 10:53:02 skrll 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 lwp *);
     69 #endif
     70 
     71 #ifdef VERIFIED_EXEC
     72 #include <sys/verified_exec.h>
     73 
     74 extern LIST_HEAD(veriexec_devhead, veriexec_dev_list) veriexec_dev_head;
     75 extern struct veriexec_devhead veriexec_file_dev_head;
     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 lwp *l);
     83 static int vn_poll(struct file *fp, int events, struct lwp *l);
     84 static int vn_fcntl(struct file *fp, u_int com, void *data, struct lwp *l);
     85 static int vn_statfile(struct file *fp, struct stat *sb, struct lwp *l);
     86 static int vn_ioctl(struct file *fp, u_long com, void *data, struct lwp *l);
     87 
     88 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(ndp, fmode, cmode)
     99 	struct nameidata *ndp;
    100 	int fmode, cmode;
    101 {
    102 	struct vnode *vp;
    103 	struct mount *mp;
    104 	struct lwp *l = ndp->ni_cnd.cn_lwp;
    105 	struct ucred *cred = l->l_proc->p_ucred;
    106 	struct vattr va;
    107 	int error;
    108 #ifdef VERIFIED_EXEC
    109 	char got_dev;
    110 	struct veriexec_inode_list *veriexec_node;
    111 	char fingerprint[MAXFINGERPRINTLEN];
    112 #endif
    113 
    114 restart:
    115 	if (fmode & O_CREAT) {
    116 		ndp->ni_cnd.cn_nameiop = CREATE;
    117 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
    118 		if ((fmode & O_EXCL) == 0 &&
    119 		    ((fmode & O_NOFOLLOW) == 0))
    120 			ndp->ni_cnd.cn_flags |= FOLLOW;
    121 		if ((error = namei(ndp)) != 0)
    122 			return (error);
    123 		if (ndp->ni_vp == NULL) {
    124 			VATTR_NULL(&va);
    125 			va.va_type = VREG;
    126 			va.va_mode = cmode;
    127 			if (fmode & O_EXCL)
    128 				 va.va_vaflags |= VA_EXCLUSIVE;
    129 			if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
    130 				VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    131 				vput(ndp->ni_dvp);
    132 				if ((error = vn_start_write(NULL, &mp,
    133 				    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
    134 					return (error);
    135 				goto restart;
    136 			}
    137 			VOP_LEASE(ndp->ni_dvp, l, cred, LEASE_WRITE);
    138 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
    139 					   &ndp->ni_cnd, &va);
    140 			vn_finished_write(mp, 0);
    141 			if (error)
    142 				return (error);
    143 			fmode &= ~O_TRUNC;
    144 			vp = ndp->ni_vp;
    145 		} else {
    146 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    147 			if (ndp->ni_dvp == ndp->ni_vp)
    148 				vrele(ndp->ni_dvp);
    149 			else
    150 				vput(ndp->ni_dvp);
    151 			ndp->ni_dvp = NULL;
    152 			vp = ndp->ni_vp;
    153 			if (fmode & O_EXCL) {
    154 				error = EEXIST;
    155 				goto bad;
    156 			}
    157 			fmode &= ~O_CREAT;
    158 		}
    159 	} else {
    160 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    161 		ndp->ni_cnd.cn_flags = LOCKLEAF;
    162 		if ((fmode & O_NOFOLLOW) == 0)
    163 			ndp->ni_cnd.cn_flags |= FOLLOW;
    164 		if ((error = namei(ndp)) != 0)
    165 			return (error);
    166 		vp = ndp->ni_vp;
    167 	}
    168 	if (vp->v_type == VSOCK) {
    169 		error = EOPNOTSUPP;
    170 		goto bad;
    171 	}
    172 	if (ndp->ni_vp->v_type == VLNK) {
    173 		error = EFTYPE;
    174 		goto bad;
    175 	}
    176 
    177 #ifdef VERIFIED_EXEC
    178 	veriexec_node = NULL;
    179 
    180 	if ((error = VOP_GETATTR(vp, &va, cred, l)) != 0)
    181 		goto bad;
    182 #endif
    183 
    184 	if ((fmode & O_CREAT) == 0) {
    185 #ifdef VERIFIED_EXEC
    186 		  /*
    187 		   * Look for the file on the fingerprint lists iff
    188 		   * it has not been seen before.
    189 		   */
    190 		if ((vp->fp_status == FINGERPRINT_INVALID) ||
    191 		    (vp->fp_status == FINGERPRINT_NODEV)) {
    192 			  /* check the file list for the finger print */
    193 			veriexec_node = get_veriexec_inode(&veriexec_file_dev_head,
    194 						     va.va_fsid,
    195 						     va.va_fileid,
    196 						     &got_dev);
    197 			if (veriexec_node == NULL) {
    198 				/* failing that, check the exec list */
    199 				veriexec_node = get_veriexec_inode(
    200 					&veriexec_dev_head, va.va_fsid,
    201 					va.va_fileid, &got_dev);
    202 			}
    203 
    204 			if ((veriexec_node == NULL) && (got_dev == 1))
    205 				vp->fp_status = FINGERPRINT_NOENTRY;
    206 
    207 			if (veriexec_node != NULL) {
    208 				if ((error = evaluate_fingerprint(vp,
    209 						veriexec_node, l, va.va_size,
    210 						fingerprint)) != 0)
    211 					goto bad;
    212 
    213 				if (fingerprintcmp(veriexec_node,
    214 						   fingerprint) == 0) {
    215 					  /* fingerprint ok */
    216 					vp->fp_status =	FINGERPRINT_VALID;
    217 #ifdef VERIFIED_EXEC_DEBUG
    218 					printf(
    219 			"file fingerprint matches for dev %lu, file %lu\n",
    220 						va.va_fsid, va.va_fileid);
    221 #endif
    222 				} else {
    223 					vp->fp_status =	FINGERPRINT_NOMATCH;
    224 				}
    225 			}
    226 		}
    227 #endif
    228 
    229 		if (fmode & FREAD) {
    230 			if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
    231 				goto bad;
    232 
    233 #ifdef VERIFIED_EXEC
    234 				/* file is on finger print list */
    235 			if (vp->fp_status == FINGERPRINT_NOMATCH) {
    236 				  /* fingerprint bad */
    237 				printf(
    238 		"file fingerprint does not match on dev %lu, file %lu\n",
    239 					va.va_fsid, va.va_fileid);
    240 				if (securelevel > 2) {
    241 					error = EPERM;
    242 					goto bad;
    243 				}
    244 			}
    245 #endif
    246 		}
    247 		if (fmode & (FWRITE | O_TRUNC)) {
    248 			if (vp->v_type == VDIR) {
    249 				error = EISDIR;
    250 				goto bad;
    251 			}
    252 			if ((error = vn_writechk(vp)) != 0 ||
    253 			    (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
    254 				goto bad;
    255 #ifdef VERIFIED_EXEC
    256 			  /*
    257 			   * If file has a fingerprint then
    258 			   * deny the write request, otherwise
    259 			   * invalidate the status so we don't
    260 			   * keep checking for the file having
    261 			   * a fingerprint.
    262 			   */
    263 			if (vp->fp_status == FINGERPRINT_VALID) {
    264 				printf(
    265 		      "writing to fingerprinted file for dev %lu, file %lu\n",
    266 		      va.va_fsid, va.va_fileid);
    267 				if (securelevel > 2) {
    268 					error = EPERM;
    269 					goto bad;
    270 				} else {
    271 					vp->fp_status =	FINGERPRINT_INVALID;
    272 				}
    273 			}
    274 #endif
    275 		}
    276 	}
    277 	if (fmode & O_TRUNC) {
    278 		VOP_UNLOCK(vp, 0);			/* XXX */
    279 		if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
    280 			vput(vp);
    281 			return (error);
    282 		}
    283 		VOP_LEASE(vp, l, cred, LEASE_WRITE);
    284 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
    285 		VATTR_NULL(&va);
    286 		va.va_size = 0;
    287 		error = VOP_SETATTR(vp, &va, cred, l);
    288 		vn_finished_write(mp, 0);
    289 		if (error != 0)
    290 			goto bad;
    291 	}
    292 	if ((error = VOP_OPEN(vp, fmode, cred, l)) != 0)
    293 		goto bad;
    294 	if (vp->v_type == VREG &&
    295 	    uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
    296 		error = EIO;
    297 		goto bad;
    298 	}
    299 	if (fmode & FWRITE)
    300 		vp->v_writecount++;
    301 
    302 	return (0);
    303 bad:
    304 	vput(vp);
    305 	return (error);
    306 }
    307 
    308 /*
    309  * Check for write permissions on the specified vnode.
    310  * Prototype text segments cannot be written.
    311  */
    312 int
    313 vn_writechk(vp)
    314 	struct vnode *vp;
    315 {
    316 
    317 	/*
    318 	 * If the vnode is in use as a process's text,
    319 	 * we can't allow writing.
    320 	 */
    321 	if (vp->v_flag & VTEXT)
    322 		return (ETXTBSY);
    323 	return (0);
    324 }
    325 
    326 /*
    327  * Mark a vnode as having executable mappings.
    328  */
    329 void
    330 vn_markexec(vp)
    331 	struct vnode *vp;
    332 {
    333 	if ((vp->v_flag & VEXECMAP) == 0) {
    334 		uvmexp.filepages -= vp->v_uobj.uo_npages;
    335 		uvmexp.execpages += vp->v_uobj.uo_npages;
    336 	}
    337 	vp->v_flag |= VEXECMAP;
    338 }
    339 
    340 /*
    341  * Mark a vnode as being the text of a process.
    342  * Fail if the vnode is currently writable.
    343  */
    344 int
    345 vn_marktext(vp)
    346 	struct vnode *vp;
    347 {
    348 
    349 	if (vp->v_writecount != 0) {
    350 		KASSERT((vp->v_flag & VTEXT) == 0);
    351 		return (ETXTBSY);
    352 	}
    353 	vp->v_flag |= VTEXT;
    354 	vn_markexec(vp);
    355 	return (0);
    356 }
    357 
    358 /*
    359  * Vnode close call
    360  *
    361  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    362  */
    363 int
    364 vn_close(vp, flags, cred, l)
    365 	struct vnode *vp;
    366 	int flags;
    367 	struct ucred *cred;
    368 	struct lwp *l;
    369 {
    370 	int error;
    371 
    372 	if (flags & FWRITE)
    373 		vp->v_writecount--;
    374 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    375 	error = VOP_CLOSE(vp, flags, cred, l);
    376 	vput(vp);
    377 	return (error);
    378 }
    379 
    380 /*
    381  * Package up an I/O request on a vnode into a uio and do it.
    382  */
    383 int
    384 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, l)
    385 	enum uio_rw rw;
    386 	struct vnode *vp;
    387 	caddr_t base;
    388 	int len;
    389 	off_t offset;
    390 	enum uio_seg segflg;
    391 	int ioflg;
    392 	struct ucred *cred;
    393 	size_t *aresid;
    394 	struct lwp *l;
    395 {
    396 	struct uio auio;
    397 	struct iovec aiov;
    398 	struct mount *mp;
    399 	int error;
    400 
    401 	if ((ioflg & IO_NODELOCKED) == 0) {
    402 		if (rw == UIO_READ) {
    403 			vn_lock(vp, LK_SHARED | LK_RETRY);
    404 		} else /* UIO_WRITE */ {
    405 			if (vp->v_type != VCHR &&
    406 			    (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH))
    407 			    != 0)
    408 				return (error);
    409 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    410 		}
    411 	}
    412 	auio.uio_iov = &aiov;
    413 	auio.uio_iovcnt = 1;
    414 	aiov.iov_base = base;
    415 	aiov.iov_len = len;
    416 	auio.uio_resid = len;
    417 	auio.uio_offset = offset;
    418 	auio.uio_segflg = segflg;
    419 	auio.uio_rw = rw;
    420 	auio.uio_lwp = l;
    421 	if (rw == UIO_READ) {
    422 		error = VOP_READ(vp, &auio, ioflg, cred);
    423 	} else {
    424 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    425 	}
    426 	if (aresid)
    427 		*aresid = auio.uio_resid;
    428 	else
    429 		if (auio.uio_resid && error == 0)
    430 			error = EIO;
    431 	if ((ioflg & IO_NODELOCKED) == 0) {
    432 		if (rw == UIO_WRITE)
    433 			vn_finished_write(mp, 0);
    434 		VOP_UNLOCK(vp, 0);
    435 	}
    436 	return (error);
    437 }
    438 
    439 int
    440 vn_readdir(fp, buf, segflg, count, done, l, cookies, ncookies)
    441 	struct file *fp;
    442 	char *buf;
    443 	int segflg, *done, *ncookies;
    444 	u_int count;
    445 	struct lwp *l;
    446 	off_t **cookies;
    447 {
    448 	struct vnode *vp = (struct vnode *)fp->f_data;
    449 	struct iovec aiov;
    450 	struct uio auio;
    451 	int error, eofflag;
    452 
    453 unionread:
    454 	if (vp->v_type != VDIR)
    455 		return (EINVAL);
    456 	aiov.iov_base = buf;
    457 	aiov.iov_len = count;
    458 	auio.uio_iov = &aiov;
    459 	auio.uio_iovcnt = 1;
    460 	auio.uio_rw = UIO_READ;
    461 	auio.uio_segflg = segflg;
    462 	auio.uio_lwp = l;
    463 	auio.uio_resid = count;
    464 	vn_lock(vp, LK_SHARED | LK_RETRY);
    465 	auio.uio_offset = fp->f_offset;
    466 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    467 		    ncookies);
    468 	fp->f_offset = auio.uio_offset;
    469 	VOP_UNLOCK(vp, 0);
    470 	if (error)
    471 		return (error);
    472 
    473 #if defined(UNION) || defined(LKM)
    474 	if (count == auio.uio_resid && vn_union_readdir_hook) {
    475 		struct vnode *ovp = vp;
    476 
    477 		error = (*vn_union_readdir_hook)(&vp, fp, l);
    478 		if (error)
    479 			return (error);
    480 		if (vp != ovp)
    481 			goto unionread;
    482 	}
    483 #endif /* UNION || LKM */
    484 
    485 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    486 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    487 		struct vnode *tvp = vp;
    488 		vp = vp->v_mount->mnt_vnodecovered;
    489 		VREF(vp);
    490 		fp->f_data = vp;
    491 		fp->f_offset = 0;
    492 		vrele(tvp);
    493 		goto unionread;
    494 	}
    495 	*done = count - auio.uio_resid;
    496 	return error;
    497 }
    498 
    499 /*
    500  * File table vnode read routine.
    501  */
    502 static int
    503 vn_read(fp, offset, uio, cred, flags)
    504 	struct file *fp;
    505 	off_t *offset;
    506 	struct uio *uio;
    507 	struct ucred *cred;
    508 	int flags;
    509 {
    510 	struct vnode *vp = (struct vnode *)fp->f_data;
    511 	int count, error, ioflag = 0;
    512 
    513 	VOP_LEASE(vp, uio->uio_lwp, cred, LEASE_READ);
    514 	if (fp->f_flag & FNONBLOCK)
    515 		ioflag |= IO_NDELAY;
    516 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    517 		ioflag |= IO_SYNC;
    518 	if (fp->f_flag & FALTIO)
    519 		ioflag |= IO_ALTSEMANTICS;
    520 	vn_lock(vp, LK_SHARED | LK_RETRY);
    521 	uio->uio_offset = *offset;
    522 	count = uio->uio_resid;
    523 	error = VOP_READ(vp, uio, ioflag, cred);
    524 	if (flags & FOF_UPDATE_OFFSET)
    525 		*offset += count - uio->uio_resid;
    526 	VOP_UNLOCK(vp, 0);
    527 	return (error);
    528 }
    529 
    530 /*
    531  * File table vnode write routine.
    532  */
    533 static int
    534 vn_write(fp, offset, uio, cred, flags)
    535 	struct file *fp;
    536 	off_t *offset;
    537 	struct uio *uio;
    538 	struct ucred *cred;
    539 	int flags;
    540 {
    541 	struct vnode *vp = (struct vnode *)fp->f_data;
    542 	struct mount *mp;
    543 	int count, error, ioflag = IO_UNIT;
    544 
    545 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    546 		ioflag |= IO_APPEND;
    547 	if (fp->f_flag & FNONBLOCK)
    548 		ioflag |= IO_NDELAY;
    549 	if (fp->f_flag & FFSYNC ||
    550 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    551 		ioflag |= IO_SYNC;
    552 	else if (fp->f_flag & FDSYNC)
    553 		ioflag |= IO_DSYNC;
    554 	if (fp->f_flag & FALTIO)
    555 		ioflag |= IO_ALTSEMANTICS;
    556 	mp = NULL;
    557 	if (vp->v_type != VCHR &&
    558 	    (error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
    559 		return (error);
    560 	VOP_LEASE(vp, uio->uio_lwp, cred, LEASE_WRITE);
    561 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    562 	uio->uio_offset = *offset;
    563 	count = uio->uio_resid;
    564 	error = VOP_WRITE(vp, uio, ioflag, cred);
    565 	if (flags & FOF_UPDATE_OFFSET) {
    566 		if (ioflag & IO_APPEND)
    567 			*offset = uio->uio_offset;
    568 		else
    569 			*offset += count - uio->uio_resid;
    570 	}
    571 	VOP_UNLOCK(vp, 0);
    572 	vn_finished_write(mp, 0);
    573 	return (error);
    574 }
    575 
    576 /*
    577  * File table vnode stat routine.
    578  */
    579 static int
    580 vn_statfile(fp, sb, l)
    581 	struct file *fp;
    582 	struct stat *sb;
    583 	struct lwp *l;
    584 {
    585 	struct vnode *vp = (struct vnode *)fp->f_data;
    586 
    587 	return vn_stat(vp, sb, l);
    588 }
    589 
    590 int
    591 vn_stat(vp, sb, l)
    592 	struct vnode *vp;
    593 	struct stat *sb;
    594 	struct lwp *l;
    595 {
    596 	struct vattr va;
    597 	int error;
    598 	mode_t mode;
    599 
    600 	error = VOP_GETATTR(vp, &va, l->l_proc->p_ucred, l);
    601 	if (error)
    602 		return (error);
    603 	/*
    604 	 * Copy from vattr table
    605 	 */
    606 	sb->st_dev = va.va_fsid;
    607 	sb->st_ino = va.va_fileid;
    608 	mode = va.va_mode;
    609 	switch (vp->v_type) {
    610 	case VREG:
    611 		mode |= S_IFREG;
    612 		break;
    613 	case VDIR:
    614 		mode |= S_IFDIR;
    615 		break;
    616 	case VBLK:
    617 		mode |= S_IFBLK;
    618 		break;
    619 	case VCHR:
    620 		mode |= S_IFCHR;
    621 		break;
    622 	case VLNK:
    623 		mode |= S_IFLNK;
    624 		break;
    625 	case VSOCK:
    626 		mode |= S_IFSOCK;
    627 		break;
    628 	case VFIFO:
    629 		mode |= S_IFIFO;
    630 		break;
    631 	default:
    632 		return (EBADF);
    633 	};
    634 	sb->st_mode = mode;
    635 	sb->st_nlink = va.va_nlink;
    636 	sb->st_uid = va.va_uid;
    637 	sb->st_gid = va.va_gid;
    638 	sb->st_rdev = va.va_rdev;
    639 	sb->st_size = va.va_size;
    640 	sb->st_atimespec = va.va_atime;
    641 	sb->st_mtimespec = va.va_mtime;
    642 	sb->st_ctimespec = va.va_ctime;
    643 	sb->st_birthtimespec = va.va_birthtime;
    644 	sb->st_blksize = va.va_blocksize;
    645 	sb->st_flags = va.va_flags;
    646 	sb->st_gen = 0;
    647 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    648 	return (0);
    649 }
    650 
    651 /*
    652  * File table vnode fcntl routine.
    653  */
    654 static int
    655 vn_fcntl(fp, com, data, l)
    656 	struct file *fp;
    657 	u_int com;
    658 	void *data;
    659 	struct lwp *l;
    660 {
    661 	struct vnode *vp = ((struct vnode *)fp->f_data);
    662 	int error;
    663 
    664 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    665 	error = VOP_FCNTL(vp, com, data, fp->f_flag, l->l_proc->p_ucred, l);
    666 	VOP_UNLOCK(vp, 0);
    667 	return (error);
    668 }
    669 
    670 /*
    671  * File table vnode ioctl routine.
    672  */
    673 static int
    674 vn_ioctl(fp, com, data, l)
    675 	struct file *fp;
    676 	u_long com;
    677 	void *data;
    678 	struct lwp *l;
    679 {
    680 	struct vnode *vp = ((struct vnode *)fp->f_data);
    681 	struct proc *p = l->l_proc;
    682 	struct vattr vattr;
    683 	int error;
    684 
    685 	switch (vp->v_type) {
    686 
    687 	case VREG:
    688 	case VDIR:
    689 		if (com == FIONREAD) {
    690 			error = VOP_GETATTR(vp, &vattr, l->l_proc->p_ucred, l);
    691 			if (error)
    692 				return (error);
    693 			*(int *)data = vattr.va_size - fp->f_offset;
    694 			return (0);
    695 		}
    696 		if (com == FIOGETBMAP) {
    697 			daddr_t *block;
    698 
    699 			if (*(daddr_t *)data < 0)
    700 				return (EINVAL);
    701 			block = (daddr_t *)data;
    702 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
    703 		}
    704 		if (com == OFIOGETBMAP) {
    705 			daddr_t ibn, obn;
    706 
    707 			if (*(int32_t *)data < 0)
    708 				return (EINVAL);
    709 			ibn = (daddr_t)*(int32_t *)data;
    710 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
    711 			*(int32_t *)data = (int32_t)obn;
    712 			return error;
    713 		}
    714 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    715 			return (0);			/* XXX */
    716 		/* fall into ... */
    717 	case VFIFO:
    718 	case VCHR:
    719 	case VBLK:
    720 		error = VOP_IOCTL(vp, com, data, fp->f_flag,
    721 		    l->l_proc->p_ucred, l);
    722 		if (error == 0 && com == TIOCSCTTY) {
    723 			if (p->p_session->s_ttyvp)
    724 				vrele(p->p_session->s_ttyvp);
    725 			p->p_session->s_ttyvp = vp;
    726 			VREF(vp);
    727 		}
    728 		return (error);
    729 
    730 	default:
    731 		return (EPASSTHROUGH);
    732 	}
    733 }
    734 
    735 /*
    736  * File table vnode poll routine.
    737  */
    738 static int
    739 vn_poll(fp, events, l)
    740 	struct file *fp;
    741 	int events;
    742 	struct lwp *l;
    743 {
    744 
    745 	return (VOP_POLL(((struct vnode *)fp->f_data), events, l));
    746 }
    747 
    748 /*
    749  * File table vnode kqfilter routine.
    750  */
    751 int
    752 vn_kqfilter(fp, kn)
    753 	struct file *fp;
    754 	struct knote *kn;
    755 {
    756 
    757 	return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
    758 }
    759 
    760 /*
    761  * Check that the vnode is still valid, and if so
    762  * acquire requested lock.
    763  */
    764 int
    765 vn_lock(vp, flags)
    766 	struct vnode *vp;
    767 	int flags;
    768 {
    769 	int error;
    770 
    771 	KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
    772 	    || (vp->v_flag & VONWORKLST) != 0);
    773 
    774 	do {
    775 		if ((flags & LK_INTERLOCK) == 0)
    776 			simple_lock(&vp->v_interlock);
    777 		if (vp->v_flag & VXLOCK) {
    778 			if (flags & LK_NOWAIT) {
    779 				simple_unlock(&vp->v_interlock);
    780 				return EBUSY;
    781 			}
    782 			vp->v_flag |= VXWANT;
    783 			ltsleep(vp, PINOD | PNORELOCK,
    784 			    "vn_lock", 0, &vp->v_interlock);
    785 			error = ENOENT;
    786 		} else {
    787 			error = VOP_LOCK(vp,
    788 			    (flags & ~LK_RETRY) | LK_INTERLOCK);
    789 			if (error == 0 || error == EDEADLK || error == EBUSY)
    790 				return (error);
    791 		}
    792 		flags &= ~LK_INTERLOCK;
    793 	} while (flags & LK_RETRY);
    794 	return (error);
    795 }
    796 
    797 /*
    798  * File table vnode close routine.
    799  */
    800 static int
    801 vn_closefile(fp, l)
    802 	struct file *fp;
    803 	struct lwp *l;
    804 {
    805 
    806 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    807 		fp->f_cred, l));
    808 }
    809 
    810 /*
    811  * Enable LK_CANRECURSE on lock. Return prior status.
    812  */
    813 u_int
    814 vn_setrecurse(vp)
    815 	struct vnode *vp;
    816 {
    817 	struct lock *lkp = &vp->v_lock;
    818 	u_int retval = lkp->lk_flags & LK_CANRECURSE;
    819 
    820 	lkp->lk_flags |= LK_CANRECURSE;
    821 	return retval;
    822 }
    823 
    824 /*
    825  * Called when done with locksetrecurse.
    826  */
    827 void
    828 vn_restorerecurse(vp, flags)
    829 	struct vnode *vp;
    830 	u_int flags;
    831 {
    832 	struct lock *lkp = &vp->v_lock;
    833 
    834 	lkp->lk_flags &= ~LK_CANRECURSE;
    835 	lkp->lk_flags |= flags;
    836 }
    837 
    838 int
    839 vn_cow_establish(struct vnode *vp,
    840     int (*func)(void *, struct buf *), void *cookie)
    841 {
    842 	int s;
    843 	struct spec_cow_entry *e;
    844 
    845 	MALLOC(e, struct spec_cow_entry *, sizeof(struct spec_cow_entry),
    846 	    M_DEVBUF, M_WAITOK);
    847 	e->ce_func = func;
    848 	e->ce_cookie = cookie;
    849 
    850 	SPEC_COW_LOCK(vp->v_specinfo, s);
    851 	vp->v_spec_cow_req++;
    852 	while (vp->v_spec_cow_count > 0)
    853 		ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
    854 		    &vp->v_spec_cow_slock);
    855 
    856 	SLIST_INSERT_HEAD(&vp->v_spec_cow_head, e, ce_list);
    857 
    858 	vp->v_spec_cow_req--;
    859 	if (vp->v_spec_cow_req == 0)
    860 		wakeup(&vp->v_spec_cow_req);
    861 	SPEC_COW_UNLOCK(vp->v_specinfo, s);
    862 
    863 	return 0;
    864 }
    865 
    866 int
    867 vn_cow_disestablish(struct vnode *vp,
    868     int (*func)(void *, struct buf *), void *cookie)
    869 {
    870 	int s;
    871 	struct spec_cow_entry *e;
    872 
    873 	SPEC_COW_LOCK(vp->v_specinfo, s);
    874 	vp->v_spec_cow_req++;
    875 	while (vp->v_spec_cow_count > 0)
    876 		ltsleep(&vp->v_spec_cow_req, PRIBIO, "cowlist", 0,
    877 		    &vp->v_spec_cow_slock);
    878 
    879 	SLIST_FOREACH(e, &vp->v_spec_cow_head, ce_list)
    880 		if (e->ce_func == func && e->ce_cookie == cookie) {
    881 			SLIST_REMOVE(&vp->v_spec_cow_head, e,
    882 			    spec_cow_entry, ce_list);
    883 			FREE(e, M_DEVBUF);
    884 			break;
    885 		}
    886 
    887 	vp->v_spec_cow_req--;
    888 	if (vp->v_spec_cow_req == 0)
    889 		wakeup(&vp->v_spec_cow_req);
    890 	SPEC_COW_UNLOCK(vp->v_specinfo, s);
    891 
    892 	return e ? 0 : EINVAL;
    893 }
    894