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