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