Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.45.2.8
      1  1.45.2.6   nathanw /*	$NetBSD: vfs_vnops.c,v 1.45.2.8 2002/11/11 22:14:15 nathanw Exp $	*/
      2      1.12       cgd 
      3      1.10       cgd /*
      4      1.11   mycroft  * Copyright (c) 1982, 1986, 1989, 1993
      5      1.11   mycroft  *	The Regents of the University of California.  All rights reserved.
      6      1.10       cgd  * (c) UNIX System Laboratories, Inc.
      7      1.10       cgd  * All or some portions of this file are derived from material licensed
      8      1.10       cgd  * to the University of California by American Telephone and Telegraph
      9      1.10       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10      1.10       cgd  * the permission of UNIX System Laboratories, Inc.
     11      1.10       cgd  *
     12      1.10       cgd  * Redistribution and use in source and binary forms, with or without
     13      1.10       cgd  * modification, are permitted provided that the following conditions
     14      1.10       cgd  * are met:
     15      1.10       cgd  * 1. Redistributions of source code must retain the above copyright
     16      1.10       cgd  *    notice, this list of conditions and the following disclaimer.
     17      1.10       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18      1.10       cgd  *    notice, this list of conditions and the following disclaimer in the
     19      1.10       cgd  *    documentation and/or other materials provided with the distribution.
     20      1.10       cgd  * 3. All advertising materials mentioning features or use of this software
     21      1.10       cgd  *    must display the following acknowledgement:
     22      1.10       cgd  *	This product includes software developed by the University of
     23      1.10       cgd  *	California, Berkeley and its contributors.
     24      1.10       cgd  * 4. Neither the name of the University nor the names of its contributors
     25      1.10       cgd  *    may be used to endorse or promote products derived from this software
     26      1.10       cgd  *    without specific prior written permission.
     27      1.10       cgd  *
     28      1.10       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29      1.10       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30      1.10       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31      1.10       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32      1.10       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33      1.10       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34      1.10       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35      1.10       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36      1.10       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37      1.10       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38      1.10       cgd  * SUCH DAMAGE.
     39      1.10       cgd  *
     40      1.28      fvdl  *	@(#)vfs_vnops.c	8.14 (Berkeley) 6/15/95
     41      1.10       cgd  */
     42      1.26       mrg 
     43  1.45.2.4   nathanw #include <sys/cdefs.h>
     44  1.45.2.6   nathanw __KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.45.2.8 2002/11/11 22:14:15 nathanw Exp $");
     45  1.45.2.4   nathanw 
     46      1.27   thorpej #include "fs_union.h"
     47      1.10       cgd 
     48      1.10       cgd #include <sys/param.h>
     49      1.10       cgd #include <sys/systm.h>
     50      1.10       cgd #include <sys/kernel.h>
     51      1.10       cgd #include <sys/file.h>
     52      1.10       cgd #include <sys/stat.h>
     53      1.10       cgd #include <sys/buf.h>
     54      1.10       cgd #include <sys/proc.h>
     55      1.10       cgd #include <sys/mount.h>
     56      1.10       cgd #include <sys/namei.h>
     57      1.10       cgd #include <sys/vnode.h>
     58      1.10       cgd #include <sys/ioctl.h>
     59      1.10       cgd #include <sys/tty.h>
     60      1.21   mycroft #include <sys/poll.h>
     61      1.11   mycroft 
     62      1.25       mrg #include <uvm/uvm_extern.h>
     63      1.25       mrg 
     64      1.28      fvdl #ifdef UNION
     65      1.28      fvdl #include <miscfs/union/union.h>
     66      1.28      fvdl #endif
     67  1.45.2.8   nathanw #include <sys/verified_exec.h>
     68  1.45.2.8   nathanw 
     69  1.45.2.8   nathanw extern LIST_HEAD(veriexec_devhead, veriexec_dev_list) veriexec_dev_head;
     70  1.45.2.8   nathanw extern struct veriexec_devhead veriexec_file_dev_head;
     71      1.28      fvdl 
     72  1.45.2.7   nathanw static int vn_read(struct file *fp, off_t *offset, struct uio *uio,
     73  1.45.2.7   nathanw 	    struct ucred *cred, int flags);
     74  1.45.2.7   nathanw static int vn_write(struct file *fp, off_t *offset, struct uio *uio,
     75  1.45.2.7   nathanw 	    struct ucred *cred, int flags);
     76  1.45.2.7   nathanw static int vn_closefile(struct file *fp, struct proc *p);
     77  1.45.2.7   nathanw static int vn_poll(struct file *fp, int events, struct proc *p);
     78  1.45.2.7   nathanw static int vn_fcntl(struct file *fp, u_int com, caddr_t data, struct proc *p);
     79  1.45.2.7   nathanw static int vn_statfile(struct file *fp, struct stat *sb, struct proc *p);
     80  1.45.2.7   nathanw static int vn_ioctl(struct file *fp, u_long com, caddr_t data, struct proc *p);
     81  1.45.2.2   nathanw 
     82  1.45.2.2   nathanw struct 	fileops vnops = {
     83  1.45.2.2   nathanw 	vn_read, vn_write, vn_ioctl, vn_fcntl, vn_poll,
     84  1.45.2.8   nathanw 	vn_statfile, vn_closefile, vn_kqfilter
     85  1.45.2.2   nathanw };
     86      1.10       cgd 
     87      1.10       cgd /*
     88      1.10       cgd  * Common code for vnode open operations.
     89      1.10       cgd  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
     90      1.10       cgd  */
     91      1.20  christos int
     92      1.18   mycroft vn_open(ndp, fmode, cmode)
     93      1.41  augustss 	struct nameidata *ndp;
     94      1.10       cgd 	int fmode, cmode;
     95      1.10       cgd {
     96      1.41  augustss 	struct vnode *vp;
     97      1.41  augustss 	struct proc *p = ndp->ni_cnd.cn_proc;
     98      1.41  augustss 	struct ucred *cred = p->p_ucred;
     99      1.19   mycroft 	struct vattr va;
    100      1.10       cgd 	int error;
    101  1.45.2.8   nathanw #ifdef VERIFIED_EXEC
    102  1.45.2.8   nathanw 	char got_dev;
    103  1.45.2.8   nathanw 	struct veriexec_inode_list *veriexec_node;
    104  1.45.2.8   nathanw 	char fingerprint[MAXFINGERPRINTLEN];
    105  1.45.2.8   nathanw #endif
    106  1.45.2.8   nathanw 
    107      1.10       cgd 	if (fmode & O_CREAT) {
    108      1.11   mycroft 		ndp->ni_cnd.cn_nameiop = CREATE;
    109      1.11   mycroft 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
    110      1.38    bouyer 		if ((fmode & O_EXCL) == 0 &&
    111      1.38    bouyer 		    ((fmode & FNOSYMLINK) == 0))
    112      1.11   mycroft 			ndp->ni_cnd.cn_flags |= FOLLOW;
    113      1.20  christos 		if ((error = namei(ndp)) != 0)
    114      1.10       cgd 			return (error);
    115      1.10       cgd 		if (ndp->ni_vp == NULL) {
    116      1.19   mycroft 			VATTR_NULL(&va);
    117      1.19   mycroft 			va.va_type = VREG;
    118      1.19   mycroft 			va.va_mode = cmode;
    119      1.28      fvdl 			if (fmode & O_EXCL)
    120      1.28      fvdl 				 va.va_vaflags |= VA_EXCLUSIVE;
    121      1.17   mycroft 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
    122      1.20  christos 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
    123      1.20  christos 					   &ndp->ni_cnd, &va);
    124      1.20  christos 			if (error)
    125      1.10       cgd 				return (error);
    126      1.10       cgd 			fmode &= ~O_TRUNC;
    127      1.10       cgd 			vp = ndp->ni_vp;
    128      1.10       cgd 		} else {
    129      1.11   mycroft 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
    130      1.10       cgd 			if (ndp->ni_dvp == ndp->ni_vp)
    131      1.10       cgd 				vrele(ndp->ni_dvp);
    132      1.10       cgd 			else
    133      1.10       cgd 				vput(ndp->ni_dvp);
    134      1.10       cgd 			ndp->ni_dvp = NULL;
    135      1.10       cgd 			vp = ndp->ni_vp;
    136      1.10       cgd 			if (fmode & O_EXCL) {
    137      1.10       cgd 				error = EEXIST;
    138      1.38    bouyer 				goto bad;
    139      1.38    bouyer 			}
    140      1.38    bouyer 			if (ndp->ni_vp->v_type == VLNK) {
    141      1.38    bouyer 				error = EFTYPE;
    142      1.10       cgd 				goto bad;
    143      1.10       cgd 			}
    144      1.10       cgd 			fmode &= ~O_CREAT;
    145      1.10       cgd 		}
    146      1.10       cgd 	} else {
    147      1.11   mycroft 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    148      1.11   mycroft 		ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
    149      1.20  christos 		if ((error = namei(ndp)) != 0)
    150      1.10       cgd 			return (error);
    151      1.10       cgd 		vp = ndp->ni_vp;
    152      1.10       cgd 	}
    153      1.10       cgd 	if (vp->v_type == VSOCK) {
    154      1.10       cgd 		error = EOPNOTSUPP;
    155      1.10       cgd 		goto bad;
    156      1.10       cgd 	}
    157  1.45.2.8   nathanw 
    158  1.45.2.8   nathanw #ifdef VERIFIED_EXEC
    159  1.45.2.8   nathanw 	veriexec_node = NULL;
    160  1.45.2.8   nathanw 
    161  1.45.2.8   nathanw 	if ((error = VOP_GETATTR(vp, &va, cred, p)) != 0)
    162  1.45.2.8   nathanw 		goto bad;
    163  1.45.2.8   nathanw #endif
    164  1.45.2.8   nathanw 
    165      1.10       cgd 	if ((fmode & O_CREAT) == 0) {
    166  1.45.2.8   nathanw #ifdef VERIFIED_EXEC
    167  1.45.2.8   nathanw 		  /*
    168  1.45.2.8   nathanw 		   * Look for the file on the fingerprint lists iff
    169  1.45.2.8   nathanw 		   * it has not been seen before.
    170  1.45.2.8   nathanw 		   */
    171  1.45.2.8   nathanw 		if ((vp->fp_status == FINGERPRINT_INVALID) ||
    172  1.45.2.8   nathanw 		    (vp->fp_status == FINGERPRINT_NODEV)) {
    173  1.45.2.8   nathanw 			  /* check the file list for the finger print */
    174  1.45.2.8   nathanw 			veriexec_node = get_veriexec_inode(&veriexec_file_dev_head,
    175  1.45.2.8   nathanw 						     va.va_fsid,
    176  1.45.2.8   nathanw 						     va.va_fileid,
    177  1.45.2.8   nathanw 						     &got_dev);
    178  1.45.2.8   nathanw 			if (veriexec_node == NULL) {
    179  1.45.2.8   nathanw 				/* failing that, check the exec list */
    180  1.45.2.8   nathanw 				veriexec_node = get_veriexec_inode(
    181  1.45.2.8   nathanw 					&veriexec_dev_head, va.va_fsid,
    182  1.45.2.8   nathanw 					va.va_fileid, &got_dev);
    183  1.45.2.8   nathanw 			}
    184  1.45.2.8   nathanw 
    185  1.45.2.8   nathanw 			if ((veriexec_node == NULL) && (got_dev == 1))
    186  1.45.2.8   nathanw 				vp->fp_status = FINGERPRINT_NOENTRY;
    187  1.45.2.8   nathanw 
    188  1.45.2.8   nathanw 			if (veriexec_node != NULL) {
    189  1.45.2.8   nathanw 				if ((error = evaluate_fingerprint(vp,
    190  1.45.2.8   nathanw 						veriexec_node, p, va.va_size,
    191  1.45.2.8   nathanw 						fingerprint)) != 0)
    192  1.45.2.8   nathanw 					goto bad;
    193  1.45.2.8   nathanw 
    194  1.45.2.8   nathanw 				if (fingerprintcmp(veriexec_node,
    195  1.45.2.8   nathanw 						   fingerprint) == 0) {
    196  1.45.2.8   nathanw 					  /* fingerprint ok */
    197  1.45.2.8   nathanw 					vp->fp_status =	FINGERPRINT_VALID;
    198  1.45.2.8   nathanw #ifdef VERIFIED_EXEC_DEBUG
    199  1.45.2.8   nathanw 					printf(
    200  1.45.2.8   nathanw 			"file fingerprint matches for dev %lu, file %lu\n",
    201  1.45.2.8   nathanw 						va.va_fsid, va.va_fileid);
    202  1.45.2.8   nathanw #endif
    203  1.45.2.8   nathanw 				} else {
    204  1.45.2.8   nathanw 					vp->fp_status =	FINGERPRINT_NOMATCH;
    205  1.45.2.8   nathanw 				}
    206  1.45.2.8   nathanw 			}
    207  1.45.2.8   nathanw 		}
    208  1.45.2.8   nathanw #endif
    209  1.45.2.8   nathanw 
    210      1.10       cgd 		if (fmode & FREAD) {
    211      1.20  christos 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
    212      1.10       cgd 				goto bad;
    213  1.45.2.8   nathanw 
    214  1.45.2.8   nathanw #ifdef VERIFIED_EXEC
    215  1.45.2.8   nathanw 				/* file is on finger print list */
    216  1.45.2.8   nathanw 			if (vp->fp_status == FINGERPRINT_NOMATCH) {
    217  1.45.2.8   nathanw 				  /* fingerprint bad */
    218  1.45.2.8   nathanw 				printf(
    219  1.45.2.8   nathanw 		"file fingerprint does not match on dev %lu, file %lu\n",
    220  1.45.2.8   nathanw 					va.va_fsid, va.va_fileid);
    221  1.45.2.8   nathanw 				if (securelevel > 2) {
    222  1.45.2.8   nathanw 					error = EPERM;
    223  1.45.2.8   nathanw 					goto bad;
    224  1.45.2.8   nathanw 				}
    225  1.45.2.8   nathanw 			}
    226  1.45.2.8   nathanw #endif
    227      1.10       cgd 		}
    228      1.10       cgd 		if (fmode & (FWRITE | O_TRUNC)) {
    229      1.10       cgd 			if (vp->v_type == VDIR) {
    230      1.10       cgd 				error = EISDIR;
    231      1.10       cgd 				goto bad;
    232      1.10       cgd 			}
    233      1.20  christos 			if ((error = vn_writechk(vp)) != 0 ||
    234      1.20  christos 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
    235      1.10       cgd 				goto bad;
    236  1.45.2.8   nathanw #ifdef VERIFIED_EXEC
    237  1.45.2.8   nathanw 			  /*
    238  1.45.2.8   nathanw 			   * If file has a fingerprint then
    239  1.45.2.8   nathanw 			   * deny the write request, otherwise
    240  1.45.2.8   nathanw 			   * invalidate the status so we don't
    241  1.45.2.8   nathanw 			   * keep checking for the file having
    242  1.45.2.8   nathanw 			   * a fingerprint.
    243  1.45.2.8   nathanw 			   */
    244  1.45.2.8   nathanw 			if (vp->fp_status == FINGERPRINT_VALID) {
    245  1.45.2.8   nathanw 				printf(
    246  1.45.2.8   nathanw 		      "writing to fingerprinted file for dev %lu, file %lu\n",
    247  1.45.2.8   nathanw 		      va.va_fsid, va.va_fileid);
    248  1.45.2.8   nathanw 				if (securelevel > 2) {
    249  1.45.2.8   nathanw 					error = EPERM;
    250  1.45.2.8   nathanw 					goto bad;
    251  1.45.2.8   nathanw 				} else {
    252  1.45.2.8   nathanw 					vp->fp_status =	FINGERPRINT_INVALID;
    253  1.45.2.8   nathanw 				}
    254  1.45.2.8   nathanw 			}
    255  1.45.2.8   nathanw #endif
    256      1.10       cgd 		}
    257      1.10       cgd 	}
    258      1.10       cgd 	if (fmode & O_TRUNC) {
    259      1.28      fvdl 		VOP_UNLOCK(vp, 0);			/* XXX */
    260      1.17   mycroft 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
    261      1.28      fvdl 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
    262      1.19   mycroft 		VATTR_NULL(&va);
    263      1.19   mycroft 		va.va_size = 0;
    264      1.20  christos 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
    265      1.10       cgd 			goto bad;
    266      1.10       cgd 	}
    267      1.20  christos 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
    268      1.10       cgd 		goto bad;
    269      1.45       chs 	if (vp->v_type == VREG &&
    270      1.45       chs 	    uvn_attach(vp, fmode & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
    271      1.45       chs 		error = EIO;
    272      1.45       chs 		goto bad;
    273      1.45       chs 	}
    274      1.10       cgd 	if (fmode & FWRITE)
    275      1.10       cgd 		vp->v_writecount++;
    276      1.45       chs 
    277      1.10       cgd 	return (0);
    278      1.10       cgd bad:
    279      1.10       cgd 	vput(vp);
    280      1.10       cgd 	return (error);
    281      1.10       cgd }
    282      1.10       cgd 
    283      1.10       cgd /*
    284      1.10       cgd  * Check for write permissions on the specified vnode.
    285      1.28      fvdl  * Prototype text segments cannot be written.
    286      1.10       cgd  */
    287      1.20  christos int
    288      1.10       cgd vn_writechk(vp)
    289      1.41  augustss 	struct vnode *vp;
    290      1.10       cgd {
    291      1.10       cgd 
    292      1.10       cgd 	/*
    293      1.45       chs 	 * If the vnode is in use as a process's text,
    294      1.45       chs 	 * we can't allow writing.
    295      1.10       cgd 	 */
    296      1.45       chs 	if (vp->v_flag & VTEXT)
    297      1.25       mrg 		return (ETXTBSY);
    298      1.10       cgd 	return (0);
    299      1.42       chs }
    300      1.42       chs 
    301      1.42       chs /*
    302  1.45.2.4   nathanw  * Mark a vnode as having executable mappings.
    303      1.42       chs  */
    304      1.42       chs void
    305  1.45.2.4   nathanw vn_markexec(vp)
    306      1.42       chs 	struct vnode *vp;
    307      1.42       chs {
    308  1.45.2.4   nathanw 	if ((vp->v_flag & VEXECMAP) == 0) {
    309  1.45.2.5   nathanw 		uvmexp.filepages -= vp->v_uobj.uo_npages;
    310  1.45.2.5   nathanw 		uvmexp.execpages += vp->v_uobj.uo_npages;
    311  1.45.2.1   nathanw 	}
    312  1.45.2.4   nathanw 	vp->v_flag |= VEXECMAP;
    313      1.10       cgd }
    314      1.10       cgd 
    315      1.10       cgd /*
    316  1.45.2.7   nathanw  * Mark a vnode as being the text of a process.
    317  1.45.2.7   nathanw  * Fail if the vnode is currently writable.
    318  1.45.2.7   nathanw  */
    319  1.45.2.7   nathanw int
    320  1.45.2.7   nathanw vn_marktext(vp)
    321  1.45.2.7   nathanw 	struct vnode *vp;
    322  1.45.2.7   nathanw {
    323  1.45.2.7   nathanw 
    324  1.45.2.7   nathanw 	if (vp->v_writecount != 0) {
    325  1.45.2.7   nathanw 		KASSERT((vp->v_flag & VTEXT) == 0);
    326  1.45.2.7   nathanw 		return (ETXTBSY);
    327  1.45.2.7   nathanw 	}
    328  1.45.2.7   nathanw 	vp->v_flag |= VTEXT;
    329  1.45.2.7   nathanw 	vn_markexec(vp);
    330  1.45.2.7   nathanw 	return (0);
    331  1.45.2.7   nathanw }
    332  1.45.2.7   nathanw 
    333  1.45.2.7   nathanw /*
    334      1.10       cgd  * Vnode close call
    335      1.32  wrstuden  *
    336      1.32  wrstuden  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
    337      1.10       cgd  */
    338      1.20  christos int
    339      1.10       cgd vn_close(vp, flags, cred, p)
    340      1.41  augustss 	struct vnode *vp;
    341      1.10       cgd 	int flags;
    342      1.10       cgd 	struct ucred *cred;
    343      1.10       cgd 	struct proc *p;
    344      1.10       cgd {
    345      1.10       cgd 	int error;
    346      1.10       cgd 
    347      1.10       cgd 	if (flags & FWRITE)
    348      1.10       cgd 		vp->v_writecount--;
    349      1.32  wrstuden 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    350      1.10       cgd 	error = VOP_CLOSE(vp, flags, cred, p);
    351      1.32  wrstuden 	vput(vp);
    352      1.10       cgd 	return (error);
    353      1.10       cgd }
    354      1.10       cgd 
    355      1.10       cgd /*
    356      1.10       cgd  * Package up an I/O request on a vnode into a uio and do it.
    357      1.10       cgd  */
    358      1.20  christos int
    359      1.10       cgd vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
    360      1.10       cgd 	enum uio_rw rw;
    361      1.10       cgd 	struct vnode *vp;
    362      1.10       cgd 	caddr_t base;
    363      1.10       cgd 	int len;
    364      1.10       cgd 	off_t offset;
    365      1.10       cgd 	enum uio_seg segflg;
    366      1.10       cgd 	int ioflg;
    367      1.10       cgd 	struct ucred *cred;
    368      1.30   thorpej 	size_t *aresid;
    369      1.10       cgd 	struct proc *p;
    370      1.10       cgd {
    371      1.10       cgd 	struct uio auio;
    372      1.10       cgd 	struct iovec aiov;
    373      1.10       cgd 	int error;
    374      1.10       cgd 
    375  1.45.2.3   nathanw 	if ((ioflg & IO_NODELOCKED) == 0) {
    376  1.45.2.3   nathanw 		if (rw == UIO_READ) {
    377  1.45.2.3   nathanw 			vn_lock(vp, LK_SHARED | LK_RETRY);
    378  1.45.2.3   nathanw 		} else {
    379  1.45.2.3   nathanw 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    380  1.45.2.3   nathanw 		}
    381  1.45.2.3   nathanw 	}
    382      1.10       cgd 	auio.uio_iov = &aiov;
    383      1.10       cgd 	auio.uio_iovcnt = 1;
    384      1.10       cgd 	aiov.iov_base = base;
    385      1.10       cgd 	aiov.iov_len = len;
    386      1.10       cgd 	auio.uio_resid = len;
    387      1.10       cgd 	auio.uio_offset = offset;
    388      1.10       cgd 	auio.uio_segflg = segflg;
    389      1.10       cgd 	auio.uio_rw = rw;
    390      1.10       cgd 	auio.uio_procp = p;
    391      1.11   mycroft 	if (rw == UIO_READ) {
    392      1.10       cgd 		error = VOP_READ(vp, &auio, ioflg, cred);
    393      1.11   mycroft 	} else {
    394      1.10       cgd 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    395      1.11   mycroft 	}
    396      1.10       cgd 	if (aresid)
    397      1.10       cgd 		*aresid = auio.uio_resid;
    398      1.10       cgd 	else
    399      1.10       cgd 		if (auio.uio_resid && error == 0)
    400      1.10       cgd 			error = EIO;
    401      1.10       cgd 	if ((ioflg & IO_NODELOCKED) == 0)
    402      1.28      fvdl 		VOP_UNLOCK(vp, 0);
    403      1.10       cgd 	return (error);
    404      1.23      fvdl }
    405      1.23      fvdl 
    406      1.23      fvdl int
    407      1.23      fvdl vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
    408      1.23      fvdl 	struct file *fp;
    409      1.23      fvdl 	char *buf;
    410      1.28      fvdl 	int segflg, *done, *ncookies;
    411      1.23      fvdl 	u_int count;
    412      1.23      fvdl 	struct proc *p;
    413      1.28      fvdl 	off_t **cookies;
    414      1.23      fvdl {
    415      1.23      fvdl 	struct vnode *vp = (struct vnode *)fp->f_data;
    416      1.23      fvdl 	struct iovec aiov;
    417      1.23      fvdl 	struct uio auio;
    418      1.23      fvdl 	int error, eofflag;
    419      1.23      fvdl 
    420      1.23      fvdl unionread:
    421      1.23      fvdl 	if (vp->v_type != VDIR)
    422      1.23      fvdl 		return (EINVAL);
    423      1.23      fvdl 	aiov.iov_base = buf;
    424      1.23      fvdl 	aiov.iov_len = count;
    425      1.23      fvdl 	auio.uio_iov = &aiov;
    426      1.23      fvdl 	auio.uio_iovcnt = 1;
    427      1.23      fvdl 	auio.uio_rw = UIO_READ;
    428      1.23      fvdl 	auio.uio_segflg = segflg;
    429      1.23      fvdl 	auio.uio_procp = p;
    430      1.23      fvdl 	auio.uio_resid = count;
    431  1.45.2.3   nathanw 	vn_lock(vp, LK_SHARED | LK_RETRY);
    432      1.23      fvdl 	auio.uio_offset = fp->f_offset;
    433      1.28      fvdl 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
    434      1.23      fvdl 		    ncookies);
    435      1.23      fvdl 	fp->f_offset = auio.uio_offset;
    436      1.28      fvdl 	VOP_UNLOCK(vp, 0);
    437      1.23      fvdl 	if (error)
    438      1.23      fvdl 		return (error);
    439      1.23      fvdl 
    440      1.23      fvdl #ifdef UNION
    441      1.23      fvdl {
    442      1.23      fvdl 	extern struct vnode *union_dircache __P((struct vnode *));
    443      1.23      fvdl 
    444      1.23      fvdl 	if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
    445      1.23      fvdl 		struct vnode *lvp;
    446      1.23      fvdl 
    447      1.23      fvdl 		lvp = union_dircache(vp);
    448      1.23      fvdl 		if (lvp != NULLVP) {
    449      1.23      fvdl 			struct vattr va;
    450      1.23      fvdl 
    451      1.23      fvdl 			/*
    452      1.23      fvdl 			 * If the directory is opaque,
    453      1.23      fvdl 			 * then don't show lower entries
    454      1.23      fvdl 			 */
    455      1.23      fvdl 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
    456      1.23      fvdl 			if (va.va_flags & OPAQUE) {
    457      1.23      fvdl 				vput(lvp);
    458      1.23      fvdl 				lvp = NULL;
    459      1.23      fvdl 			}
    460      1.23      fvdl 		}
    461      1.23      fvdl 
    462      1.23      fvdl 		if (lvp != NULLVP) {
    463      1.23      fvdl 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
    464      1.23      fvdl 			if (error) {
    465      1.28      fvdl 				vput(lvp);
    466      1.23      fvdl 				return (error);
    467      1.23      fvdl 			}
    468      1.28      fvdl 			VOP_UNLOCK(lvp, 0);
    469      1.23      fvdl 			fp->f_data = (caddr_t) lvp;
    470      1.23      fvdl 			fp->f_offset = 0;
    471      1.23      fvdl 			error = vn_close(vp, FREAD, fp->f_cred, p);
    472      1.23      fvdl 			if (error)
    473      1.23      fvdl 				return (error);
    474      1.23      fvdl 			vp = lvp;
    475      1.23      fvdl 			goto unionread;
    476      1.23      fvdl 		}
    477      1.23      fvdl 	}
    478      1.23      fvdl }
    479      1.23      fvdl #endif /* UNION */
    480      1.23      fvdl 
    481      1.23      fvdl 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    482      1.23      fvdl 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    483      1.23      fvdl 		struct vnode *tvp = vp;
    484      1.23      fvdl 		vp = vp->v_mount->mnt_vnodecovered;
    485      1.23      fvdl 		VREF(vp);
    486      1.23      fvdl 		fp->f_data = (caddr_t) vp;
    487      1.23      fvdl 		fp->f_offset = 0;
    488      1.23      fvdl 		vrele(tvp);
    489      1.23      fvdl 		goto unionread;
    490      1.23      fvdl 	}
    491      1.23      fvdl 	*done = count - auio.uio_resid;
    492      1.23      fvdl 	return error;
    493      1.10       cgd }
    494      1.10       cgd 
    495      1.10       cgd /*
    496      1.10       cgd  * File table vnode read routine.
    497      1.10       cgd  */
    498  1.45.2.7   nathanw static int
    499      1.29   thorpej vn_read(fp, offset, uio, cred, flags)
    500      1.10       cgd 	struct file *fp;
    501      1.29   thorpej 	off_t *offset;
    502      1.10       cgd 	struct uio *uio;
    503      1.10       cgd 	struct ucred *cred;
    504      1.29   thorpej 	int flags;
    505      1.10       cgd {
    506      1.28      fvdl 	struct vnode *vp = (struct vnode *)fp->f_data;
    507      1.31    kleink 	int count, error, ioflag = 0;
    508      1.10       cgd 
    509      1.17   mycroft 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    510      1.31    kleink 	if (fp->f_flag & FNONBLOCK)
    511      1.31    kleink 		ioflag |= IO_NDELAY;
    512      1.31    kleink 	if ((fp->f_flag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
    513      1.31    kleink 		ioflag |= IO_SYNC;
    514      1.37  wrstuden 	if (fp->f_flag & FALTIO)
    515      1.37  wrstuden 		ioflag |= IO_ALTSEMANTICS;
    516  1.45.2.3   nathanw 	vn_lock(vp, LK_SHARED | LK_RETRY);
    517      1.29   thorpej 	uio->uio_offset = *offset;
    518      1.10       cgd 	count = uio->uio_resid;
    519      1.31    kleink 	error = VOP_READ(vp, uio, ioflag, cred);
    520      1.29   thorpej 	if (flags & FOF_UPDATE_OFFSET)
    521      1.29   thorpej 		*offset += count - uio->uio_resid;
    522      1.28      fvdl 	VOP_UNLOCK(vp, 0);
    523      1.10       cgd 	return (error);
    524      1.10       cgd }
    525      1.10       cgd 
    526      1.10       cgd /*
    527      1.10       cgd  * File table vnode write routine.
    528      1.10       cgd  */
    529  1.45.2.7   nathanw static int
    530      1.29   thorpej vn_write(fp, offset, uio, cred, flags)
    531      1.10       cgd 	struct file *fp;
    532      1.29   thorpej 	off_t *offset;
    533      1.10       cgd 	struct uio *uio;
    534      1.10       cgd 	struct ucred *cred;
    535      1.29   thorpej 	int flags;
    536      1.10       cgd {
    537      1.28      fvdl 	struct vnode *vp = (struct vnode *)fp->f_data;
    538      1.17   mycroft 	int count, error, ioflag = IO_UNIT;
    539      1.10       cgd 
    540      1.10       cgd 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    541      1.10       cgd 		ioflag |= IO_APPEND;
    542      1.10       cgd 	if (fp->f_flag & FNONBLOCK)
    543      1.10       cgd 		ioflag |= IO_NDELAY;
    544      1.24   thorpej 	if (fp->f_flag & FFSYNC ||
    545      1.24   thorpej 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
    546      1.24   thorpej 		ioflag |= IO_SYNC;
    547      1.31    kleink 	else if (fp->f_flag & FDSYNC)
    548      1.31    kleink 		ioflag |= IO_DSYNC;
    549      1.37  wrstuden 	if (fp->f_flag & FALTIO)
    550      1.37  wrstuden 		ioflag |= IO_ALTSEMANTICS;
    551      1.17   mycroft 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
    552      1.28      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    553      1.29   thorpej 	uio->uio_offset = *offset;
    554      1.10       cgd 	count = uio->uio_resid;
    555      1.10       cgd 	error = VOP_WRITE(vp, uio, ioflag, cred);
    556      1.29   thorpej 	if (flags & FOF_UPDATE_OFFSET) {
    557      1.29   thorpej 		if (ioflag & IO_APPEND)
    558      1.29   thorpej 			*offset = uio->uio_offset;
    559      1.29   thorpej 		else
    560      1.29   thorpej 			*offset += count - uio->uio_resid;
    561      1.29   thorpej 	}
    562      1.28      fvdl 	VOP_UNLOCK(vp, 0);
    563      1.10       cgd 	return (error);
    564      1.10       cgd }
    565      1.10       cgd 
    566      1.10       cgd /*
    567      1.10       cgd  * File table vnode stat routine.
    568      1.10       cgd  */
    569  1.45.2.2   nathanw static int
    570  1.45.2.2   nathanw vn_statfile(fp, sb, p)
    571  1.45.2.2   nathanw 	struct file *fp;
    572  1.45.2.2   nathanw 	struct stat *sb;
    573  1.45.2.2   nathanw 	struct proc *p;
    574  1.45.2.2   nathanw {
    575  1.45.2.2   nathanw 	struct vnode *vp = (struct vnode *)fp->f_data;
    576  1.45.2.2   nathanw 
    577  1.45.2.2   nathanw 	return vn_stat(vp, sb, p);
    578  1.45.2.2   nathanw }
    579  1.45.2.2   nathanw 
    580      1.20  christos int
    581  1.45.2.7   nathanw vn_stat(vp, sb, p)
    582  1.45.2.7   nathanw 	struct vnode *vp;
    583      1.41  augustss 	struct stat *sb;
    584      1.10       cgd 	struct proc *p;
    585      1.10       cgd {
    586      1.19   mycroft 	struct vattr va;
    587      1.10       cgd 	int error;
    588      1.35  wrstuden 	mode_t mode;
    589      1.10       cgd 
    590      1.19   mycroft 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
    591      1.10       cgd 	if (error)
    592      1.10       cgd 		return (error);
    593      1.10       cgd 	/*
    594      1.10       cgd 	 * Copy from vattr table
    595      1.10       cgd 	 */
    596      1.19   mycroft 	sb->st_dev = va.va_fsid;
    597      1.19   mycroft 	sb->st_ino = va.va_fileid;
    598      1.19   mycroft 	mode = va.va_mode;
    599      1.10       cgd 	switch (vp->v_type) {
    600      1.10       cgd 	case VREG:
    601      1.10       cgd 		mode |= S_IFREG;
    602      1.10       cgd 		break;
    603      1.10       cgd 	case VDIR:
    604      1.10       cgd 		mode |= S_IFDIR;
    605      1.10       cgd 		break;
    606      1.10       cgd 	case VBLK:
    607      1.10       cgd 		mode |= S_IFBLK;
    608      1.10       cgd 		break;
    609      1.10       cgd 	case VCHR:
    610      1.10       cgd 		mode |= S_IFCHR;
    611      1.10       cgd 		break;
    612      1.10       cgd 	case VLNK:
    613      1.10       cgd 		mode |= S_IFLNK;
    614      1.10       cgd 		break;
    615      1.10       cgd 	case VSOCK:
    616      1.10       cgd 		mode |= S_IFSOCK;
    617      1.10       cgd 		break;
    618      1.10       cgd 	case VFIFO:
    619      1.10       cgd 		mode |= S_IFIFO;
    620      1.10       cgd 		break;
    621      1.10       cgd 	default:
    622      1.10       cgd 		return (EBADF);
    623      1.10       cgd 	};
    624      1.10       cgd 	sb->st_mode = mode;
    625      1.19   mycroft 	sb->st_nlink = va.va_nlink;
    626      1.19   mycroft 	sb->st_uid = va.va_uid;
    627      1.19   mycroft 	sb->st_gid = va.va_gid;
    628      1.19   mycroft 	sb->st_rdev = va.va_rdev;
    629      1.19   mycroft 	sb->st_size = va.va_size;
    630      1.19   mycroft 	sb->st_atimespec = va.va_atime;
    631      1.19   mycroft 	sb->st_mtimespec = va.va_mtime;
    632      1.19   mycroft 	sb->st_ctimespec = va.va_ctime;
    633      1.19   mycroft 	sb->st_blksize = va.va_blocksize;
    634      1.19   mycroft 	sb->st_flags = va.va_flags;
    635      1.22   mycroft 	sb->st_gen = 0;
    636      1.19   mycroft 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    637      1.10       cgd 	return (0);
    638      1.37  wrstuden }
    639      1.37  wrstuden 
    640      1.37  wrstuden /*
    641      1.37  wrstuden  * File table vnode fcntl routine.
    642      1.37  wrstuden  */
    643  1.45.2.7   nathanw static int
    644      1.37  wrstuden vn_fcntl(fp, com, data, p)
    645      1.37  wrstuden 	struct file *fp;
    646      1.37  wrstuden 	u_int com;
    647      1.37  wrstuden 	caddr_t data;
    648      1.37  wrstuden 	struct proc *p;
    649      1.37  wrstuden {
    650      1.41  augustss 	struct vnode *vp = ((struct vnode *)fp->f_data);
    651      1.37  wrstuden 	int error;
    652      1.37  wrstuden 
    653      1.37  wrstuden 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    654      1.37  wrstuden 	error = VOP_FCNTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    655      1.37  wrstuden 	VOP_UNLOCK(vp, 0);
    656      1.37  wrstuden 	return (error);
    657      1.10       cgd }
    658      1.10       cgd 
    659      1.10       cgd /*
    660      1.10       cgd  * File table vnode ioctl routine.
    661      1.10       cgd  */
    662  1.45.2.7   nathanw static int
    663      1.10       cgd vn_ioctl(fp, com, data, p)
    664      1.10       cgd 	struct file *fp;
    665      1.15       cgd 	u_long com;
    666      1.10       cgd 	caddr_t data;
    667      1.10       cgd 	struct proc *p;
    668      1.10       cgd {
    669      1.41  augustss 	struct vnode *vp = ((struct vnode *)fp->f_data);
    670      1.10       cgd 	struct vattr vattr;
    671      1.10       cgd 	int error;
    672      1.10       cgd 
    673      1.10       cgd 	switch (vp->v_type) {
    674      1.10       cgd 
    675      1.10       cgd 	case VREG:
    676      1.10       cgd 	case VDIR:
    677      1.10       cgd 		if (com == FIONREAD) {
    678      1.20  christos 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    679      1.20  christos 			if (error)
    680      1.10       cgd 				return (error);
    681      1.10       cgd 			*(int *)data = vattr.va_size - fp->f_offset;
    682      1.10       cgd 			return (0);
    683      1.10       cgd 		}
    684      1.10       cgd 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    685      1.10       cgd 			return (0);			/* XXX */
    686      1.10       cgd 		/* fall into ... */
    687      1.10       cgd 
    688      1.10       cgd 	default:
    689  1.45.2.6   nathanw 		return (EPASSTHROUGH);
    690      1.10       cgd 
    691      1.10       cgd 	case VFIFO:
    692      1.10       cgd 	case VCHR:
    693      1.10       cgd 	case VBLK:
    694      1.10       cgd 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    695      1.10       cgd 		if (error == 0 && com == TIOCSCTTY) {
    696      1.13       cgd 			if (p->p_session->s_ttyvp)
    697      1.13       cgd 				vrele(p->p_session->s_ttyvp);
    698      1.10       cgd 			p->p_session->s_ttyvp = vp;
    699      1.10       cgd 			VREF(vp);
    700      1.10       cgd 		}
    701      1.10       cgd 		return (error);
    702      1.10       cgd 	}
    703      1.10       cgd }
    704      1.10       cgd 
    705      1.10       cgd /*
    706      1.21   mycroft  * File table vnode poll routine.
    707      1.10       cgd  */
    708  1.45.2.7   nathanw static int
    709      1.21   mycroft vn_poll(fp, events, p)
    710      1.10       cgd 	struct file *fp;
    711      1.21   mycroft 	int events;
    712      1.10       cgd 	struct proc *p;
    713      1.10       cgd {
    714      1.10       cgd 
    715      1.21   mycroft 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
    716  1.45.2.8   nathanw }
    717  1.45.2.8   nathanw 
    718  1.45.2.8   nathanw /*
    719  1.45.2.8   nathanw  * File table vnode kqfilter routine.
    720  1.45.2.8   nathanw  */
    721  1.45.2.8   nathanw int
    722  1.45.2.8   nathanw vn_kqfilter(fp, kn)
    723  1.45.2.8   nathanw 	struct file *fp;
    724  1.45.2.8   nathanw 	struct knote *kn;
    725  1.45.2.8   nathanw {
    726  1.45.2.8   nathanw 
    727  1.45.2.8   nathanw 	return (VOP_KQFILTER((struct vnode *)fp->f_data, kn));
    728      1.28      fvdl }
    729      1.28      fvdl 
    730      1.28      fvdl /*
    731      1.28      fvdl  * Check that the vnode is still valid, and if so
    732      1.28      fvdl  * acquire requested lock.
    733      1.28      fvdl  */
    734      1.28      fvdl int
    735      1.28      fvdl vn_lock(vp, flags)
    736      1.28      fvdl 	struct vnode *vp;
    737      1.28      fvdl 	int flags;
    738      1.28      fvdl {
    739      1.28      fvdl 	int error;
    740      1.34  sommerfe 
    741      1.28      fvdl 	do {
    742      1.36   mycroft 		if ((flags & LK_INTERLOCK) == 0)
    743      1.28      fvdl 			simple_lock(&vp->v_interlock);
    744      1.28      fvdl 		if (vp->v_flag & VXLOCK) {
    745  1.45.2.3   nathanw 			if (flags & LK_NOWAIT) {
    746  1.45.2.3   nathanw 				simple_unlock(&vp->v_interlock);
    747  1.45.2.3   nathanw 				return EBUSY;
    748  1.45.2.3   nathanw 			}
    749      1.28      fvdl 			vp->v_flag |= VXWANT;
    750  1.45.2.3   nathanw 			ltsleep(vp, PINOD | PNORELOCK,
    751      1.44  sommerfe 			    "vn_lock", 0, &vp->v_interlock);
    752      1.28      fvdl 			error = ENOENT;
    753      1.28      fvdl 		} else {
    754      1.28      fvdl 			error = VOP_LOCK(vp, flags | LK_INTERLOCK);
    755  1.45.2.3   nathanw 			if (error == 0 || error == EDEADLK || error == EBUSY)
    756      1.28      fvdl 				return (error);
    757      1.28      fvdl 		}
    758      1.28      fvdl 		flags &= ~LK_INTERLOCK;
    759      1.28      fvdl 	} while (flags & LK_RETRY);
    760      1.28      fvdl 	return (error);
    761      1.10       cgd }
    762      1.10       cgd 
    763      1.10       cgd /*
    764      1.10       cgd  * File table vnode close routine.
    765      1.10       cgd  */
    766  1.45.2.7   nathanw static int
    767      1.10       cgd vn_closefile(fp, p)
    768      1.10       cgd 	struct file *fp;
    769      1.10       cgd 	struct proc *p;
    770      1.10       cgd {
    771      1.10       cgd 
    772      1.10       cgd 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    773      1.10       cgd 		fp->f_cred, p));
    774      1.39      fvdl }
    775      1.39      fvdl 
    776      1.39      fvdl /*
    777      1.39      fvdl  * Enable LK_CANRECURSE on lock. Return prior status.
    778      1.39      fvdl  */
    779      1.39      fvdl u_int
    780      1.39      fvdl vn_setrecurse(vp)
    781      1.39      fvdl 	struct vnode *vp;
    782      1.39      fvdl {
    783      1.39      fvdl 	struct lock *lkp = &vp->v_lock;
    784      1.39      fvdl 	u_int retval = lkp->lk_flags & LK_CANRECURSE;
    785      1.39      fvdl 
    786      1.39      fvdl 	lkp->lk_flags |= LK_CANRECURSE;
    787      1.39      fvdl 	return retval;
    788      1.39      fvdl }
    789      1.39      fvdl 
    790      1.39      fvdl /*
    791      1.39      fvdl  * Called when done with locksetrecurse.
    792      1.39      fvdl  */
    793      1.39      fvdl void
    794      1.39      fvdl vn_restorerecurse(vp, flags)
    795      1.39      fvdl 	struct vnode *vp;
    796      1.39      fvdl 	u_int flags;
    797      1.39      fvdl {
    798      1.39      fvdl 	struct lock *lkp = &vp->v_lock;
    799      1.39      fvdl 
    800      1.39      fvdl 	lkp->lk_flags &= ~LK_CANRECURSE;
    801      1.39      fvdl 	lkp->lk_flags |= flags;
    802      1.10       cgd }
    803