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