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