Home | History | Annotate | Line # | Download | only in kern
vfs_vnops.c revision 1.22.4.1
      1  1.22.4.1   thorpej /*	$NetBSD: vfs_vnops.c,v 1.22.4.1 1997/10/14 10:26:30 thorpej 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.17   mycroft  *	@(#)vfs_vnops.c	8.5 (Berkeley) 12/8/94
     41      1.10       cgd  */
     42      1.10       cgd 
     43      1.10       cgd #include <sys/param.h>
     44      1.10       cgd #include <sys/systm.h>
     45      1.10       cgd #include <sys/kernel.h>
     46      1.10       cgd #include <sys/file.h>
     47      1.10       cgd #include <sys/stat.h>
     48      1.10       cgd #include <sys/buf.h>
     49      1.10       cgd #include <sys/proc.h>
     50      1.10       cgd #include <sys/mount.h>
     51      1.10       cgd #include <sys/namei.h>
     52      1.10       cgd #include <sys/vnode.h>
     53      1.10       cgd #include <sys/ioctl.h>
     54      1.10       cgd #include <sys/tty.h>
     55      1.21   mycroft #include <sys/poll.h>
     56      1.10       cgd 
     57      1.11   mycroft #include <vm/vm.h>
     58      1.11   mycroft 
     59      1.10       cgd struct 	fileops vnops =
     60      1.21   mycroft 	{ vn_read, vn_write, vn_ioctl, vn_poll, vn_closefile };
     61      1.10       cgd 
     62      1.10       cgd /*
     63      1.10       cgd  * Common code for vnode open operations.
     64      1.10       cgd  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
     65      1.10       cgd  */
     66      1.20  christos int
     67      1.18   mycroft vn_open(ndp, fmode, cmode)
     68      1.10       cgd 	register struct nameidata *ndp;
     69      1.10       cgd 	int fmode, cmode;
     70      1.10       cgd {
     71      1.10       cgd 	register struct vnode *vp;
     72      1.11   mycroft 	register struct proc *p = ndp->ni_cnd.cn_proc;
     73      1.10       cgd 	register struct ucred *cred = p->p_ucred;
     74      1.19   mycroft 	struct vattr va;
     75      1.10       cgd 	int error;
     76      1.10       cgd 
     77      1.10       cgd 	if (fmode & O_CREAT) {
     78      1.11   mycroft 		ndp->ni_cnd.cn_nameiop = CREATE;
     79      1.11   mycroft 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
     80      1.10       cgd 		if ((fmode & O_EXCL) == 0)
     81      1.11   mycroft 			ndp->ni_cnd.cn_flags |= FOLLOW;
     82      1.20  christos 		if ((error = namei(ndp)) != 0)
     83      1.10       cgd 			return (error);
     84      1.10       cgd 		if (ndp->ni_vp == NULL) {
     85      1.19   mycroft 			VATTR_NULL(&va);
     86      1.19   mycroft 			va.va_type = VREG;
     87      1.19   mycroft 			va.va_mode = cmode;
     88      1.17   mycroft 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
     89      1.20  christos 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
     90      1.20  christos 					   &ndp->ni_cnd, &va);
     91      1.20  christos 			if (error)
     92      1.10       cgd 				return (error);
     93      1.10       cgd 			fmode &= ~O_TRUNC;
     94      1.10       cgd 			vp = ndp->ni_vp;
     95      1.10       cgd 		} else {
     96      1.11   mycroft 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
     97      1.10       cgd 			if (ndp->ni_dvp == ndp->ni_vp)
     98      1.10       cgd 				vrele(ndp->ni_dvp);
     99      1.10       cgd 			else
    100      1.10       cgd 				vput(ndp->ni_dvp);
    101      1.10       cgd 			ndp->ni_dvp = NULL;
    102      1.10       cgd 			vp = ndp->ni_vp;
    103      1.10       cgd 			if (fmode & O_EXCL) {
    104      1.10       cgd 				error = EEXIST;
    105      1.10       cgd 				goto bad;
    106      1.10       cgd 			}
    107      1.10       cgd 			fmode &= ~O_CREAT;
    108      1.10       cgd 		}
    109      1.10       cgd 	} else {
    110      1.11   mycroft 		ndp->ni_cnd.cn_nameiop = LOOKUP;
    111      1.11   mycroft 		ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
    112      1.20  christos 		if ((error = namei(ndp)) != 0)
    113      1.10       cgd 			return (error);
    114      1.10       cgd 		vp = ndp->ni_vp;
    115      1.10       cgd 	}
    116      1.10       cgd 	if (vp->v_type == VSOCK) {
    117      1.10       cgd 		error = EOPNOTSUPP;
    118      1.10       cgd 		goto bad;
    119      1.10       cgd 	}
    120      1.10       cgd 	if ((fmode & O_CREAT) == 0) {
    121      1.10       cgd 		if (fmode & FREAD) {
    122      1.20  christos 			if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
    123      1.10       cgd 				goto bad;
    124      1.10       cgd 		}
    125      1.10       cgd 		if (fmode & (FWRITE | O_TRUNC)) {
    126      1.10       cgd 			if (vp->v_type == VDIR) {
    127      1.10       cgd 				error = EISDIR;
    128      1.10       cgd 				goto bad;
    129      1.10       cgd 			}
    130      1.20  christos 			if ((error = vn_writechk(vp)) != 0 ||
    131      1.20  christos 			    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
    132      1.10       cgd 				goto bad;
    133      1.10       cgd 		}
    134      1.10       cgd 	}
    135      1.10       cgd 	if (fmode & O_TRUNC) {
    136      1.11   mycroft 		VOP_UNLOCK(vp);				/* XXX */
    137      1.17   mycroft 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
    138      1.11   mycroft 		VOP_LOCK(vp);				/* XXX */
    139      1.19   mycroft 		VATTR_NULL(&va);
    140      1.19   mycroft 		va.va_size = 0;
    141      1.20  christos 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
    142      1.10       cgd 			goto bad;
    143      1.10       cgd 	}
    144      1.20  christos 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
    145      1.10       cgd 		goto bad;
    146      1.10       cgd 	if (fmode & FWRITE)
    147      1.10       cgd 		vp->v_writecount++;
    148      1.10       cgd 	return (0);
    149      1.10       cgd bad:
    150      1.10       cgd 	vput(vp);
    151      1.10       cgd 	return (error);
    152      1.10       cgd }
    153      1.10       cgd 
    154      1.10       cgd /*
    155      1.10       cgd  * Check for write permissions on the specified vnode.
    156      1.10       cgd  * The read-only status of the file system is checked.
    157      1.10       cgd  * Also, prototype text segments cannot be written.
    158      1.10       cgd  */
    159      1.20  christos int
    160      1.10       cgd vn_writechk(vp)
    161      1.10       cgd 	register struct vnode *vp;
    162      1.10       cgd {
    163      1.10       cgd 
    164      1.10       cgd 	/*
    165      1.10       cgd 	 * Disallow write attempts on read-only file systems;
    166      1.10       cgd 	 * unless the file is a socket or a block or character
    167      1.10       cgd 	 * device resident on the file system.
    168      1.10       cgd 	 */
    169      1.10       cgd 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
    170      1.10       cgd 		switch (vp->v_type) {
    171      1.10       cgd 		case VREG: case VDIR: case VLNK:
    172      1.10       cgd 			return (EROFS);
    173      1.20  christos 		case VNON: case VCHR: case VSOCK:
    174      1.20  christos 		case VFIFO: case VBAD: case VBLK:
    175      1.20  christos 			break;
    176      1.10       cgd 		}
    177      1.10       cgd 	}
    178      1.10       cgd 	/*
    179      1.10       cgd 	 * If there's shared text associated with
    180      1.10       cgd 	 * the vnode, try to free it up once.  If
    181      1.10       cgd 	 * we fail, we can't allow writing.
    182      1.10       cgd 	 */
    183      1.10       cgd 	if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
    184      1.10       cgd 		return (ETXTBSY);
    185      1.10       cgd 	return (0);
    186      1.10       cgd }
    187      1.10       cgd 
    188      1.10       cgd /*
    189      1.10       cgd  * Vnode close call
    190      1.10       cgd  */
    191      1.20  christos int
    192      1.10       cgd vn_close(vp, flags, cred, p)
    193      1.10       cgd 	register struct vnode *vp;
    194      1.10       cgd 	int flags;
    195      1.10       cgd 	struct ucred *cred;
    196      1.10       cgd 	struct proc *p;
    197      1.10       cgd {
    198      1.10       cgd 	int error;
    199      1.10       cgd 
    200      1.10       cgd 	if (flags & FWRITE)
    201      1.10       cgd 		vp->v_writecount--;
    202      1.10       cgd 	error = VOP_CLOSE(vp, flags, cred, p);
    203      1.10       cgd 	vrele(vp);
    204      1.10       cgd 	return (error);
    205      1.10       cgd }
    206      1.10       cgd 
    207      1.10       cgd /*
    208      1.10       cgd  * Package up an I/O request on a vnode into a uio and do it.
    209      1.10       cgd  */
    210      1.20  christos int
    211      1.10       cgd vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
    212      1.10       cgd 	enum uio_rw rw;
    213      1.10       cgd 	struct vnode *vp;
    214      1.10       cgd 	caddr_t base;
    215      1.10       cgd 	int len;
    216      1.10       cgd 	off_t offset;
    217      1.10       cgd 	enum uio_seg segflg;
    218      1.10       cgd 	int ioflg;
    219      1.10       cgd 	struct ucred *cred;
    220      1.10       cgd 	int *aresid;
    221      1.10       cgd 	struct proc *p;
    222      1.10       cgd {
    223      1.10       cgd 	struct uio auio;
    224      1.10       cgd 	struct iovec aiov;
    225      1.10       cgd 	int error;
    226      1.10       cgd 
    227      1.10       cgd 	if ((ioflg & IO_NODELOCKED) == 0)
    228      1.10       cgd 		VOP_LOCK(vp);
    229      1.10       cgd 	auio.uio_iov = &aiov;
    230      1.10       cgd 	auio.uio_iovcnt = 1;
    231      1.10       cgd 	aiov.iov_base = base;
    232      1.10       cgd 	aiov.iov_len = len;
    233      1.10       cgd 	auio.uio_resid = len;
    234      1.10       cgd 	auio.uio_offset = offset;
    235      1.10       cgd 	auio.uio_segflg = segflg;
    236      1.10       cgd 	auio.uio_rw = rw;
    237      1.10       cgd 	auio.uio_procp = p;
    238      1.11   mycroft 	if (rw == UIO_READ) {
    239      1.10       cgd 		error = VOP_READ(vp, &auio, ioflg, cred);
    240      1.11   mycroft 	} else {
    241      1.10       cgd 		error = VOP_WRITE(vp, &auio, ioflg, cred);
    242      1.11   mycroft 	}
    243      1.10       cgd 	if (aresid)
    244      1.10       cgd 		*aresid = auio.uio_resid;
    245      1.10       cgd 	else
    246      1.10       cgd 		if (auio.uio_resid && error == 0)
    247      1.10       cgd 			error = EIO;
    248      1.10       cgd 	if ((ioflg & IO_NODELOCKED) == 0)
    249      1.10       cgd 		VOP_UNLOCK(vp);
    250      1.10       cgd 	return (error);
    251  1.22.4.1   thorpej }
    252  1.22.4.1   thorpej 
    253  1.22.4.1   thorpej int
    254  1.22.4.1   thorpej vn_readdir(fp, buf, segflg, count, done, p, cookies, ncookies)
    255  1.22.4.1   thorpej 	struct file *fp;
    256  1.22.4.1   thorpej 	char *buf;
    257  1.22.4.1   thorpej 	int segflg, *done, ncookies;
    258  1.22.4.1   thorpej 	u_int count;
    259  1.22.4.1   thorpej 	struct proc *p;
    260  1.22.4.1   thorpej 	off_t *cookies;
    261  1.22.4.1   thorpej {
    262  1.22.4.1   thorpej 	struct vnode *vp = (struct vnode *)fp->f_data;
    263  1.22.4.1   thorpej 	struct iovec aiov;
    264  1.22.4.1   thorpej 	struct uio auio;
    265  1.22.4.1   thorpej 	int error, eofflag;
    266  1.22.4.1   thorpej 
    267  1.22.4.1   thorpej unionread:
    268  1.22.4.1   thorpej 	if (vp->v_type != VDIR)
    269  1.22.4.1   thorpej 		return (EINVAL);
    270  1.22.4.1   thorpej 	aiov.iov_base = buf;
    271  1.22.4.1   thorpej 	aiov.iov_len = count;
    272  1.22.4.1   thorpej 	auio.uio_iov = &aiov;
    273  1.22.4.1   thorpej 	auio.uio_iovcnt = 1;
    274  1.22.4.1   thorpej 	auio.uio_rw = UIO_READ;
    275  1.22.4.1   thorpej 	auio.uio_segflg = segflg;
    276  1.22.4.1   thorpej 	auio.uio_procp = p;
    277  1.22.4.1   thorpej 	auio.uio_resid = count;
    278  1.22.4.1   thorpej 	VOP_LOCK(vp);
    279  1.22.4.1   thorpej 	auio.uio_offset = fp->f_offset;
    280  1.22.4.1   thorpej 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (off_t *)cookies,
    281  1.22.4.1   thorpej 		    ncookies);
    282  1.22.4.1   thorpej 	fp->f_offset = auio.uio_offset;
    283  1.22.4.1   thorpej 	VOP_UNLOCK(vp);
    284  1.22.4.1   thorpej 	if (error)
    285  1.22.4.1   thorpej 		return (error);
    286  1.22.4.1   thorpej 
    287  1.22.4.1   thorpej #ifdef UNION
    288  1.22.4.1   thorpej {
    289  1.22.4.1   thorpej 	extern int (**union_vnodeop_p) __P((void *));
    290  1.22.4.1   thorpej 	extern struct vnode *union_dircache __P((struct vnode *));
    291  1.22.4.1   thorpej 
    292  1.22.4.1   thorpej 	if (count == auio.uio_resid && (vp->v_op == union_vnodeop_p)) {
    293  1.22.4.1   thorpej 		struct vnode *lvp;
    294  1.22.4.1   thorpej 
    295  1.22.4.1   thorpej 		lvp = union_dircache(vp);
    296  1.22.4.1   thorpej 		if (lvp != NULLVP) {
    297  1.22.4.1   thorpej 			struct vattr va;
    298  1.22.4.1   thorpej 
    299  1.22.4.1   thorpej 			/*
    300  1.22.4.1   thorpej 			 * If the directory is opaque,
    301  1.22.4.1   thorpej 			 * then don't show lower entries
    302  1.22.4.1   thorpej 			 */
    303  1.22.4.1   thorpej 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
    304  1.22.4.1   thorpej 			if (va.va_flags & OPAQUE) {
    305  1.22.4.1   thorpej 				vput(lvp);
    306  1.22.4.1   thorpej 				lvp = NULL;
    307  1.22.4.1   thorpej 			}
    308  1.22.4.1   thorpej 		}
    309  1.22.4.1   thorpej 
    310  1.22.4.1   thorpej 		if (lvp != NULLVP) {
    311  1.22.4.1   thorpej 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
    312  1.22.4.1   thorpej 			VOP_UNLOCK(lvp);
    313  1.22.4.1   thorpej 
    314  1.22.4.1   thorpej 			if (error) {
    315  1.22.4.1   thorpej 				vrele(lvp);
    316  1.22.4.1   thorpej 				return (error);
    317  1.22.4.1   thorpej 			}
    318  1.22.4.1   thorpej 			fp->f_data = (caddr_t) lvp;
    319  1.22.4.1   thorpej 			fp->f_offset = 0;
    320  1.22.4.1   thorpej 			error = vn_close(vp, FREAD, fp->f_cred, p);
    321  1.22.4.1   thorpej 			if (error)
    322  1.22.4.1   thorpej 				return (error);
    323  1.22.4.1   thorpej 			vp = lvp;
    324  1.22.4.1   thorpej 			goto unionread;
    325  1.22.4.1   thorpej 		}
    326  1.22.4.1   thorpej 	}
    327  1.22.4.1   thorpej }
    328  1.22.4.1   thorpej #endif /* UNION */
    329  1.22.4.1   thorpej 
    330  1.22.4.1   thorpej 	if (count == auio.uio_resid && (vp->v_flag & VROOT) &&
    331  1.22.4.1   thorpej 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    332  1.22.4.1   thorpej 		struct vnode *tvp = vp;
    333  1.22.4.1   thorpej 		vp = vp->v_mount->mnt_vnodecovered;
    334  1.22.4.1   thorpej 		VREF(vp);
    335  1.22.4.1   thorpej 		fp->f_data = (caddr_t) vp;
    336  1.22.4.1   thorpej 		fp->f_offset = 0;
    337  1.22.4.1   thorpej 		vrele(tvp);
    338  1.22.4.1   thorpej 		goto unionread;
    339  1.22.4.1   thorpej 	}
    340  1.22.4.1   thorpej 	*done = count - auio.uio_resid;
    341  1.22.4.1   thorpej 	return error;
    342      1.10       cgd }
    343      1.10       cgd 
    344      1.10       cgd /*
    345      1.10       cgd  * File table vnode read routine.
    346      1.10       cgd  */
    347      1.20  christos int
    348      1.10       cgd vn_read(fp, uio, cred)
    349      1.10       cgd 	struct file *fp;
    350      1.10       cgd 	struct uio *uio;
    351      1.10       cgd 	struct ucred *cred;
    352      1.10       cgd {
    353      1.10       cgd 	register struct vnode *vp = (struct vnode *)fp->f_data;
    354      1.10       cgd 	int count, error;
    355      1.10       cgd 
    356      1.17   mycroft 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_READ);
    357      1.10       cgd 	VOP_LOCK(vp);
    358      1.10       cgd 	uio->uio_offset = fp->f_offset;
    359      1.10       cgd 	count = uio->uio_resid;
    360      1.10       cgd 	error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
    361      1.10       cgd 		cred);
    362      1.10       cgd 	fp->f_offset += count - uio->uio_resid;
    363      1.10       cgd 	VOP_UNLOCK(vp);
    364      1.10       cgd 	return (error);
    365      1.10       cgd }
    366      1.10       cgd 
    367      1.10       cgd /*
    368      1.10       cgd  * File table vnode write routine.
    369      1.10       cgd  */
    370      1.20  christos int
    371      1.10       cgd vn_write(fp, uio, cred)
    372      1.10       cgd 	struct file *fp;
    373      1.10       cgd 	struct uio *uio;
    374      1.10       cgd 	struct ucred *cred;
    375      1.10       cgd {
    376      1.10       cgd 	register struct vnode *vp = (struct vnode *)fp->f_data;
    377      1.17   mycroft 	int count, error, ioflag = IO_UNIT;
    378      1.10       cgd 
    379      1.10       cgd 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
    380      1.10       cgd 		ioflag |= IO_APPEND;
    381      1.10       cgd 	if (fp->f_flag & FNONBLOCK)
    382      1.10       cgd 		ioflag |= IO_NDELAY;
    383      1.17   mycroft 	VOP_LEASE(vp, uio->uio_procp, cred, LEASE_WRITE);
    384      1.10       cgd 	VOP_LOCK(vp);
    385      1.10       cgd 	uio->uio_offset = fp->f_offset;
    386      1.10       cgd 	count = uio->uio_resid;
    387      1.10       cgd 	error = VOP_WRITE(vp, uio, ioflag, cred);
    388      1.10       cgd 	if (ioflag & IO_APPEND)
    389      1.10       cgd 		fp->f_offset = uio->uio_offset;
    390      1.10       cgd 	else
    391      1.10       cgd 		fp->f_offset += count - uio->uio_resid;
    392      1.10       cgd 	VOP_UNLOCK(vp);
    393      1.10       cgd 	return (error);
    394      1.10       cgd }
    395      1.10       cgd 
    396      1.10       cgd /*
    397      1.10       cgd  * File table vnode stat routine.
    398      1.10       cgd  */
    399      1.20  christos int
    400      1.10       cgd vn_stat(vp, sb, p)
    401      1.10       cgd 	struct vnode *vp;
    402      1.10       cgd 	register struct stat *sb;
    403      1.10       cgd 	struct proc *p;
    404      1.10       cgd {
    405      1.19   mycroft 	struct vattr va;
    406      1.10       cgd 	int error;
    407      1.10       cgd 	u_short mode;
    408      1.10       cgd 
    409      1.19   mycroft 	error = VOP_GETATTR(vp, &va, p->p_ucred, p);
    410      1.10       cgd 	if (error)
    411      1.10       cgd 		return (error);
    412      1.10       cgd 	/*
    413      1.10       cgd 	 * Copy from vattr table
    414      1.10       cgd 	 */
    415      1.19   mycroft 	sb->st_dev = va.va_fsid;
    416      1.19   mycroft 	sb->st_ino = va.va_fileid;
    417      1.19   mycroft 	mode = va.va_mode;
    418      1.10       cgd 	switch (vp->v_type) {
    419      1.10       cgd 	case VREG:
    420      1.10       cgd 		mode |= S_IFREG;
    421      1.10       cgd 		break;
    422      1.10       cgd 	case VDIR:
    423      1.10       cgd 		mode |= S_IFDIR;
    424      1.10       cgd 		break;
    425      1.10       cgd 	case VBLK:
    426      1.10       cgd 		mode |= S_IFBLK;
    427      1.10       cgd 		break;
    428      1.10       cgd 	case VCHR:
    429      1.10       cgd 		mode |= S_IFCHR;
    430      1.10       cgd 		break;
    431      1.10       cgd 	case VLNK:
    432      1.10       cgd 		mode |= S_IFLNK;
    433      1.10       cgd 		break;
    434      1.10       cgd 	case VSOCK:
    435      1.10       cgd 		mode |= S_IFSOCK;
    436      1.10       cgd 		break;
    437      1.10       cgd 	case VFIFO:
    438      1.10       cgd 		mode |= S_IFIFO;
    439      1.10       cgd 		break;
    440      1.10       cgd 	default:
    441      1.10       cgd 		return (EBADF);
    442      1.10       cgd 	};
    443      1.10       cgd 	sb->st_mode = mode;
    444      1.19   mycroft 	sb->st_nlink = va.va_nlink;
    445      1.19   mycroft 	sb->st_uid = va.va_uid;
    446      1.19   mycroft 	sb->st_gid = va.va_gid;
    447      1.19   mycroft 	sb->st_rdev = va.va_rdev;
    448      1.19   mycroft 	sb->st_size = va.va_size;
    449      1.19   mycroft 	sb->st_atimespec = va.va_atime;
    450      1.19   mycroft 	sb->st_mtimespec = va.va_mtime;
    451      1.19   mycroft 	sb->st_ctimespec = va.va_ctime;
    452      1.19   mycroft 	sb->st_blksize = va.va_blocksize;
    453      1.19   mycroft 	sb->st_flags = va.va_flags;
    454      1.22   mycroft 	sb->st_gen = 0;
    455      1.19   mycroft 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
    456      1.10       cgd 	return (0);
    457      1.10       cgd }
    458      1.10       cgd 
    459      1.10       cgd /*
    460      1.10       cgd  * File table vnode ioctl routine.
    461      1.10       cgd  */
    462      1.20  christos int
    463      1.10       cgd vn_ioctl(fp, com, data, p)
    464      1.10       cgd 	struct file *fp;
    465      1.15       cgd 	u_long com;
    466      1.10       cgd 	caddr_t data;
    467      1.10       cgd 	struct proc *p;
    468      1.10       cgd {
    469      1.10       cgd 	register struct vnode *vp = ((struct vnode *)fp->f_data);
    470      1.10       cgd 	struct vattr vattr;
    471      1.10       cgd 	int error;
    472      1.10       cgd 
    473      1.10       cgd 	switch (vp->v_type) {
    474      1.10       cgd 
    475      1.10       cgd 	case VREG:
    476      1.10       cgd 	case VDIR:
    477      1.10       cgd 		if (com == FIONREAD) {
    478      1.20  christos 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
    479      1.20  christos 			if (error)
    480      1.10       cgd 				return (error);
    481      1.10       cgd 			*(int *)data = vattr.va_size - fp->f_offset;
    482      1.10       cgd 			return (0);
    483      1.10       cgd 		}
    484      1.10       cgd 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
    485      1.10       cgd 			return (0);			/* XXX */
    486      1.10       cgd 		/* fall into ... */
    487      1.10       cgd 
    488      1.10       cgd 	default:
    489      1.10       cgd 		return (ENOTTY);
    490      1.10       cgd 
    491      1.10       cgd 	case VFIFO:
    492      1.10       cgd 	case VCHR:
    493      1.10       cgd 	case VBLK:
    494      1.10       cgd 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
    495      1.10       cgd 		if (error == 0 && com == TIOCSCTTY) {
    496      1.13       cgd 			if (p->p_session->s_ttyvp)
    497      1.13       cgd 				vrele(p->p_session->s_ttyvp);
    498      1.10       cgd 			p->p_session->s_ttyvp = vp;
    499      1.10       cgd 			VREF(vp);
    500      1.10       cgd 		}
    501      1.10       cgd 		return (error);
    502      1.10       cgd 	}
    503      1.10       cgd }
    504      1.10       cgd 
    505      1.10       cgd /*
    506      1.21   mycroft  * File table vnode poll routine.
    507      1.10       cgd  */
    508      1.20  christos int
    509      1.21   mycroft vn_poll(fp, events, p)
    510      1.10       cgd 	struct file *fp;
    511      1.21   mycroft 	int events;
    512      1.10       cgd 	struct proc *p;
    513      1.10       cgd {
    514      1.10       cgd 
    515      1.21   mycroft 	return (VOP_POLL(((struct vnode *)fp->f_data), events, p));
    516      1.10       cgd }
    517      1.10       cgd 
    518      1.10       cgd /*
    519      1.10       cgd  * File table vnode close routine.
    520      1.10       cgd  */
    521      1.20  christos int
    522      1.10       cgd vn_closefile(fp, p)
    523      1.10       cgd 	struct file *fp;
    524      1.10       cgd 	struct proc *p;
    525      1.10       cgd {
    526      1.10       cgd 
    527      1.10       cgd 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
    528      1.10       cgd 		fp->f_cred, p));
    529      1.10       cgd }
    530