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