Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vnops.c revision 1.48
      1  1.48  wrstuden /*	$NetBSD: fdesc_vnops.c,v 1.48 1999/07/08 01:26:26 wrstuden Exp $	*/
      2  1.15       cgd 
      3   1.1       cgd /*
      4  1.14   mycroft  * Copyright (c) 1992, 1993
      5  1.14   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.8       cgd  * This code is derived from software donated to Berkeley by
      8   1.1       cgd  * Jan-Simon Pendry.
      9   1.1       cgd  *
     10   1.2       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.2       cgd  * modification, are permitted provided that the following conditions
     12   1.2       cgd  * are met:
     13   1.2       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.2       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.2       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.2       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.2       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.2       cgd  *    must display the following acknowledgement:
     20   1.8       cgd  *	This product includes software developed by the University of
     21   1.8       cgd  *	California, Berkeley and its contributors.
     22   1.2       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.2       cgd  *    may be used to endorse or promote products derived from this software
     24   1.2       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.2       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.2       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.2       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.2       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.2       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.2       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.2       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.2       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.2       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.2       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.2       cgd  * SUCH DAMAGE.
     37   1.1       cgd  *
     38  1.44      fvdl  *	@(#)fdesc_vnops.c	8.17 (Berkeley) 5/22/95
     39  1.22   mycroft  *
     40  1.22   mycroft  * #Id: fdesc_vnops.c,v 1.12 1993/04/06 16:17:17 jsp Exp #
     41   1.1       cgd  */
     42   1.1       cgd 
     43   1.1       cgd /*
     44   1.1       cgd  * /dev/fd Filesystem
     45   1.1       cgd  */
     46   1.1       cgd 
     47   1.8       cgd #include <sys/param.h>
     48   1.8       cgd #include <sys/systm.h>
     49   1.8       cgd #include <sys/types.h>
     50   1.8       cgd #include <sys/time.h>
     51   1.8       cgd #include <sys/proc.h>
     52   1.8       cgd #include <sys/kernel.h>	/* boottime */
     53   1.8       cgd #include <sys/resourcevar.h>
     54  1.30  christos #include <sys/socketvar.h>
     55   1.8       cgd #include <sys/filedesc.h>
     56   1.8       cgd #include <sys/vnode.h>
     57  1.14   mycroft #include <sys/malloc.h>
     58  1.32       mrg #include <sys/conf.h>
     59   1.8       cgd #include <sys/file.h>
     60   1.8       cgd #include <sys/stat.h>
     61   1.8       cgd #include <sys/mount.h>
     62   1.8       cgd #include <sys/namei.h>
     63   1.8       cgd #include <sys/buf.h>
     64  1.14   mycroft #include <sys/dirent.h>
     65  1.30  christos #include <sys/tty.h>
     66  1.34   mycroft 
     67   1.8       cgd #include <miscfs/fdesc/fdesc.h>
     68  1.34   mycroft #include <miscfs/genfs/genfs.h>
     69   1.8       cgd 
     70  1.14   mycroft #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
     71   1.8       cgd 
     72   1.8       cgd #define FDL_WANT	0x01
     73   1.8       cgd #define FDL_LOCKED	0x02
     74  1.14   mycroft static int fdcache_lock;
     75  1.14   mycroft 
     76  1.14   mycroft dev_t devctty;
     77   1.8       cgd 
     78   1.8       cgd #if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1)
     79   1.8       cgd FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2
     80   1.8       cgd #endif
     81   1.8       cgd 
     82  1.16   mycroft #define	NFDCACHE 4
     83  1.14   mycroft 
     84  1.17   mycroft #define FD_NHASH(ix) \
     85  1.17   mycroft 	(&fdhashtbl[(ix) & fdhash])
     86  1.17   mycroft LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
     87  1.17   mycroft u_long fdhash;
     88  1.14   mycroft 
     89  1.30  christos int	fdesc_lookup	__P((void *));
     90  1.48  wrstuden #define	fdesc_create	genfs_eopnotsupp_rele
     91  1.48  wrstuden #define	fdesc_mknod	genfs_eopnotsupp_rele
     92  1.30  christos int	fdesc_open	__P((void *));
     93  1.34   mycroft #define	fdesc_close	genfs_nullop
     94  1.34   mycroft #define	fdesc_access	genfs_nullop
     95  1.30  christos int	fdesc_getattr	__P((void *));
     96  1.30  christos int	fdesc_setattr	__P((void *));
     97  1.30  christos int	fdesc_read	__P((void *));
     98  1.30  christos int	fdesc_write	__P((void *));
     99  1.30  christos int	fdesc_ioctl	__P((void *));
    100  1.35   mycroft int	fdesc_poll	__P((void *));
    101  1.34   mycroft #define	fdesc_mmap	genfs_eopnotsupp
    102  1.34   mycroft #define	fdesc_fsync	genfs_nullop
    103  1.40      fvdl #define	fdesc_seek	genfs_seek
    104  1.48  wrstuden #define	fdesc_remove	genfs_eopnotsupp_rele
    105  1.31   mycroft int	fdesc_link	__P((void *));
    106  1.48  wrstuden #define	fdesc_rename	genfs_eopnotsupp_rele
    107  1.48  wrstuden #define	fdesc_mkdir	genfs_eopnotsupp_rele
    108  1.48  wrstuden #define	fdesc_rmdir	genfs_eopnotsupp_rele
    109  1.30  christos int	fdesc_symlink	__P((void *));
    110  1.30  christos int	fdesc_readdir	__P((void *));
    111  1.30  christos int	fdesc_readlink	__P((void *));
    112  1.34   mycroft #define	fdesc_abortop	genfs_abortop
    113  1.30  christos int	fdesc_inactive	__P((void *));
    114  1.30  christos int	fdesc_reclaim	__P((void *));
    115  1.48  wrstuden #define	fdesc_lock	genfs_lock
    116  1.48  wrstuden #define	fdesc_unlock	genfs_unlock
    117  1.34   mycroft #define	fdesc_bmap	genfs_badop
    118  1.34   mycroft #define	fdesc_strategy	genfs_badop
    119  1.30  christos int	fdesc_print	__P((void *));
    120  1.31   mycroft int	fdesc_pathconf	__P((void *));
    121  1.48  wrstuden #define	fdesc_islocked	genfs_islocked
    122  1.47    kleink #define	fdesc_advlock	genfs_einval
    123  1.34   mycroft #define	fdesc_blkatoff	genfs_eopnotsupp
    124  1.34   mycroft #define	fdesc_valloc	genfs_eopnotsupp
    125  1.34   mycroft #define	fdesc_vfree	genfs_nullop
    126  1.34   mycroft #define	fdesc_truncate	genfs_eopnotsupp
    127  1.34   mycroft #define	fdesc_update	genfs_nullop
    128  1.34   mycroft #define	fdesc_bwrite	genfs_eopnotsupp
    129  1.44      fvdl #define fdesc_revoke	genfs_revoke
    130  1.30  christos 
    131  1.30  christos static int fdesc_attr __P((int, struct vattr *, struct ucred *, struct proc *));
    132  1.30  christos 
    133  1.30  christos int (**fdesc_vnodeop_p) __P((void *));
    134  1.30  christos struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
    135  1.30  christos 	{ &vop_default_desc, vn_default_error },
    136  1.34   mycroft 	{ &vop_lookup_desc, fdesc_lookup },		/* lookup */
    137  1.34   mycroft 	{ &vop_create_desc, fdesc_create },		/* create */
    138  1.34   mycroft 	{ &vop_mknod_desc, fdesc_mknod },		/* mknod */
    139  1.34   mycroft 	{ &vop_open_desc, fdesc_open },			/* open */
    140  1.34   mycroft 	{ &vop_close_desc, fdesc_close },		/* close */
    141  1.34   mycroft 	{ &vop_access_desc, fdesc_access },		/* access */
    142  1.34   mycroft 	{ &vop_getattr_desc, fdesc_getattr },		/* getattr */
    143  1.34   mycroft 	{ &vop_setattr_desc, fdesc_setattr },		/* setattr */
    144  1.34   mycroft 	{ &vop_read_desc, fdesc_read },			/* read */
    145  1.34   mycroft 	{ &vop_write_desc, fdesc_write },		/* write */
    146  1.34   mycroft 	{ &vop_ioctl_desc, fdesc_ioctl },		/* ioctl */
    147  1.35   mycroft 	{ &vop_poll_desc, fdesc_poll },			/* poll */
    148  1.44      fvdl 	{ &vop_revoke_desc, fdesc_revoke },		/* revoke */
    149  1.34   mycroft 	{ &vop_mmap_desc, fdesc_mmap },			/* mmap */
    150  1.34   mycroft 	{ &vop_fsync_desc, fdesc_fsync },		/* fsync */
    151  1.34   mycroft 	{ &vop_seek_desc, fdesc_seek },			/* seek */
    152  1.34   mycroft 	{ &vop_remove_desc, fdesc_remove },		/* remove */
    153  1.34   mycroft 	{ &vop_link_desc, fdesc_link },			/* link */
    154  1.34   mycroft 	{ &vop_rename_desc, fdesc_rename },		/* rename */
    155  1.34   mycroft 	{ &vop_mkdir_desc, fdesc_mkdir },		/* mkdir */
    156  1.34   mycroft 	{ &vop_rmdir_desc, fdesc_rmdir },		/* rmdir */
    157  1.34   mycroft 	{ &vop_symlink_desc, fdesc_symlink },		/* symlink */
    158  1.34   mycroft 	{ &vop_readdir_desc, fdesc_readdir },		/* readdir */
    159  1.34   mycroft 	{ &vop_readlink_desc, fdesc_readlink },		/* readlink */
    160  1.34   mycroft 	{ &vop_abortop_desc, fdesc_abortop },		/* abortop */
    161  1.34   mycroft 	{ &vop_inactive_desc, fdesc_inactive },		/* inactive */
    162  1.34   mycroft 	{ &vop_reclaim_desc, fdesc_reclaim },		/* reclaim */
    163  1.34   mycroft 	{ &vop_lock_desc, fdesc_lock },			/* lock */
    164  1.34   mycroft 	{ &vop_unlock_desc, fdesc_unlock },		/* unlock */
    165  1.34   mycroft 	{ &vop_bmap_desc, fdesc_bmap },			/* bmap */
    166  1.34   mycroft 	{ &vop_strategy_desc, fdesc_strategy },		/* strategy */
    167  1.34   mycroft 	{ &vop_print_desc, fdesc_print },		/* print */
    168  1.34   mycroft 	{ &vop_islocked_desc, fdesc_islocked },		/* islocked */
    169  1.34   mycroft 	{ &vop_pathconf_desc, fdesc_pathconf },		/* pathconf */
    170  1.34   mycroft 	{ &vop_advlock_desc, fdesc_advlock },		/* advlock */
    171  1.34   mycroft 	{ &vop_blkatoff_desc, fdesc_blkatoff },		/* blkatoff */
    172  1.34   mycroft 	{ &vop_valloc_desc, fdesc_valloc },		/* valloc */
    173  1.34   mycroft 	{ &vop_vfree_desc, fdesc_vfree },		/* vfree */
    174  1.34   mycroft 	{ &vop_truncate_desc, fdesc_truncate },		/* truncate */
    175  1.34   mycroft 	{ &vop_update_desc, fdesc_update },		/* update */
    176  1.34   mycroft 	{ &vop_bwrite_desc, fdesc_bwrite },		/* bwrite */
    177  1.30  christos 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    178  1.30  christos };
    179  1.30  christos 
    180  1.30  christos struct vnodeopv_desc fdesc_vnodeop_opv_desc =
    181  1.30  christos 	{ &fdesc_vnodeop_p, fdesc_vnodeop_entries };
    182  1.30  christos 
    183  1.14   mycroft /*
    184  1.14   mycroft  * Initialise cache headers
    185  1.14   mycroft  */
    186  1.30  christos void
    187  1.14   mycroft fdesc_init()
    188  1.14   mycroft {
    189  1.32       mrg 	int cttymajor;
    190  1.14   mycroft 
    191  1.32       mrg 	/* locate the major number */
    192  1.32       mrg 	for (cttymajor = 0; cttymajor < nchrdev; cttymajor++)
    193  1.32       mrg 		if (cdevsw[cttymajor].d_open == cttyopen)
    194  1.32       mrg 			break;
    195  1.32       mrg 	devctty = makedev(cttymajor, 0);
    196  1.43       chs 	fdhashtbl = hashinit(NFDCACHE, M_CACHE, M_NOWAIT, &fdhash);
    197  1.14   mycroft }
    198  1.14   mycroft 
    199  1.48  wrstuden /*
    200  1.48  wrstuden  * Return a locked vnode of the correct type.
    201  1.48  wrstuden  */
    202  1.14   mycroft int
    203   1.8       cgd fdesc_allocvp(ftype, ix, mp, vpp)
    204   1.8       cgd 	fdntype ftype;
    205   1.8       cgd 	int ix;
    206   1.8       cgd 	struct mount *mp;
    207   1.8       cgd 	struct vnode **vpp;
    208   1.8       cgd {
    209  1.17   mycroft 	struct fdhashhead *fc;
    210  1.14   mycroft 	struct fdescnode *fd;
    211   1.8       cgd 	int error = 0;
    212   1.8       cgd 
    213  1.17   mycroft 	fc = FD_NHASH(ix);
    214   1.8       cgd loop:
    215  1.17   mycroft 	for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) {
    216  1.14   mycroft 		if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
    217  1.48  wrstuden 			if (vget(fd->fd_vnode, LK_EXCLUSIVE))
    218   1.8       cgd 				goto loop;
    219  1.14   mycroft 			*vpp = fd->fd_vnode;
    220   1.8       cgd 			return (error);
    221   1.8       cgd 		}
    222   1.8       cgd 	}
    223   1.8       cgd 
    224   1.8       cgd 	/*
    225   1.8       cgd 	 * otherwise lock the array while we call getnewvnode
    226   1.8       cgd 	 * since that can block.
    227   1.8       cgd 	 */
    228  1.14   mycroft 	if (fdcache_lock & FDL_LOCKED) {
    229  1.14   mycroft 		fdcache_lock |= FDL_WANT;
    230  1.14   mycroft 		sleep((caddr_t) &fdcache_lock, PINOD);
    231   1.8       cgd 		goto loop;
    232   1.8       cgd 	}
    233  1.14   mycroft 	fdcache_lock |= FDL_LOCKED;
    234   1.1       cgd 
    235  1.14   mycroft 	error = getnewvnode(VT_FDESC, mp, fdesc_vnodeop_p, vpp);
    236   1.8       cgd 	if (error)
    237   1.8       cgd 		goto out;
    238  1.14   mycroft 	MALLOC(fd, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
    239  1.14   mycroft 	(*vpp)->v_data = fd;
    240  1.14   mycroft 	fd->fd_vnode = *vpp;
    241  1.14   mycroft 	fd->fd_type = ftype;
    242  1.14   mycroft 	fd->fd_fd = -1;
    243  1.14   mycroft 	fd->fd_link = 0;
    244  1.14   mycroft 	fd->fd_ix = ix;
    245  1.48  wrstuden 	VOP_LOCK(*vpp, LK_EXCLUSIVE);
    246  1.17   mycroft 	LIST_INSERT_HEAD(fc, fd, fd_hash);
    247   1.8       cgd 
    248   1.8       cgd out:;
    249  1.14   mycroft 	fdcache_lock &= ~FDL_LOCKED;
    250   1.8       cgd 
    251  1.14   mycroft 	if (fdcache_lock & FDL_WANT) {
    252  1.14   mycroft 		fdcache_lock &= ~FDL_WANT;
    253  1.14   mycroft 		wakeup((caddr_t) &fdcache_lock);
    254   1.8       cgd 	}
    255   1.8       cgd 
    256   1.8       cgd 	return (error);
    257   1.8       cgd }
    258   1.1       cgd 
    259   1.1       cgd /*
    260   1.1       cgd  * vp is the current namei directory
    261   1.1       cgd  * ndp is the name to locate in that directory...
    262   1.1       cgd  */
    263  1.14   mycroft int
    264  1.30  christos fdesc_lookup(v)
    265  1.30  christos 	void *v;
    266  1.30  christos {
    267  1.14   mycroft 	struct vop_lookup_args /* {
    268  1.14   mycroft 		struct vnode * a_dvp;
    269  1.14   mycroft 		struct vnode ** a_vpp;
    270  1.14   mycroft 		struct componentname * a_cnp;
    271  1.30  christos 	} */ *ap = v;
    272  1.14   mycroft 	struct vnode **vpp = ap->a_vpp;
    273  1.14   mycroft 	struct vnode *dvp = ap->a_dvp;
    274  1.44      fvdl 	struct componentname *cnp = ap->a_cnp;
    275  1.44      fvdl 	struct proc *p = cnp->cn_proc;
    276  1.44      fvdl 	const char *pname = cnp->cn_nameptr;
    277  1.44      fvdl 	int nfiles = p->p_fd->fd_nfiles;
    278  1.30  christos 	unsigned fd = 0;
    279   1.1       cgd 	int error;
    280   1.1       cgd 	struct vnode *fvp;
    281   1.8       cgd 	char *ln;
    282   1.1       cgd 
    283  1.44      fvdl 	if (cnp->cn_namelen == 1 && *pname == '.') {
    284  1.14   mycroft 		*vpp = dvp;
    285  1.44      fvdl 		VREF(dvp);
    286   1.1       cgd 		return (0);
    287   1.1       cgd 	}
    288   1.1       cgd 
    289   1.8       cgd 	switch (VTOFDESC(dvp)->fd_type) {
    290   1.8       cgd 	default:
    291   1.8       cgd 	case Flink:
    292   1.8       cgd 	case Fdesc:
    293   1.8       cgd 	case Fctty:
    294   1.8       cgd 		error = ENOTDIR;
    295   1.8       cgd 		goto bad;
    296   1.8       cgd 
    297   1.8       cgd 	case Froot:
    298  1.46     perry 		if (cnp->cn_namelen == 2 && memcmp(pname, "fd", 2) == 0) {
    299   1.8       cgd 			error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp);
    300   1.8       cgd 			if (error)
    301   1.8       cgd 				goto bad;
    302  1.14   mycroft 			*vpp = fvp;
    303   1.8       cgd 			fvp->v_type = VDIR;
    304  1.48  wrstuden 			goto good;
    305   1.8       cgd 		}
    306   1.8       cgd 
    307  1.46     perry 		if (cnp->cn_namelen == 3 && memcmp(pname, "tty", 3) == 0) {
    308   1.8       cgd 			struct vnode *ttyvp = cttyvp(p);
    309   1.8       cgd 			if (ttyvp == NULL) {
    310   1.8       cgd 				error = ENXIO;
    311   1.8       cgd 				goto bad;
    312   1.8       cgd 			}
    313   1.8       cgd 			error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp);
    314   1.8       cgd 			if (error)
    315   1.8       cgd 				goto bad;
    316  1.14   mycroft 			*vpp = fvp;
    317  1.32       mrg 			fvp->v_type = VCHR;
    318  1.48  wrstuden 			goto good;
    319   1.8       cgd 		}
    320   1.8       cgd 
    321   1.8       cgd 		ln = 0;
    322  1.44      fvdl 		switch (cnp->cn_namelen) {
    323   1.8       cgd 		case 5:
    324  1.46     perry 			if (memcmp(pname, "stdin", 5) == 0) {
    325   1.8       cgd 				ln = "fd/0";
    326   1.8       cgd 				fd = FD_STDIN;
    327   1.8       cgd 			}
    328   1.1       cgd 			break;
    329   1.8       cgd 		case 6:
    330  1.46     perry 			if (memcmp(pname, "stdout", 6) == 0) {
    331   1.8       cgd 				ln = "fd/1";
    332   1.8       cgd 				fd = FD_STDOUT;
    333   1.8       cgd 			} else
    334  1.46     perry 			if (memcmp(pname, "stderr", 6) == 0) {
    335   1.8       cgd 				ln = "fd/2";
    336   1.8       cgd 				fd = FD_STDERR;
    337   1.8       cgd 			}
    338   1.8       cgd 			break;
    339   1.8       cgd 		}
    340   1.8       cgd 
    341   1.8       cgd 		if (ln) {
    342   1.8       cgd 			error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp);
    343   1.8       cgd 			if (error)
    344   1.8       cgd 				goto bad;
    345   1.8       cgd 			VTOFDESC(fvp)->fd_link = ln;
    346  1.14   mycroft 			*vpp = fvp;
    347   1.8       cgd 			fvp->v_type = VLNK;
    348  1.48  wrstuden 			goto good;
    349   1.8       cgd 		} else {
    350   1.8       cgd 			error = ENOENT;
    351   1.8       cgd 			goto bad;
    352   1.8       cgd 		}
    353   1.8       cgd 
    354  1.14   mycroft 		/* FALL THROUGH */
    355   1.8       cgd 
    356   1.8       cgd 	case Fdevfd:
    357  1.46     perry 		if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) {
    358  1.48  wrstuden 			VOP_UNLOCK(dvp, 0);
    359  1.48  wrstuden 			cnp->cn_flags |= PDIRUNLOCK;
    360  1.14   mycroft 			error = fdesc_root(dvp->v_mount, vpp);
    361  1.44      fvdl 			if (error)
    362  1.44      fvdl 				goto bad;
    363  1.48  wrstuden 			/*
    364  1.48  wrstuden 			 * If we're at the last component and need the
    365  1.48  wrstuden 			 * parent locked, undo the unlock above.
    366  1.48  wrstuden 			 */
    367  1.48  wrstuden 			if (((~cnp->cn_flags & (ISLASTCN | LOCKPARENT)) == 0) &&
    368  1.48  wrstuden 				   ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
    369  1.48  wrstuden 				cnp->cn_flags &= ~PDIRUNLOCK;
    370  1.48  wrstuden 			return (error);
    371   1.8       cgd 		}
    372   1.8       cgd 
    373   1.8       cgd 		fd = 0;
    374   1.8       cgd 		while (*pname >= '0' && *pname <= '9') {
    375   1.8       cgd 			fd = 10 * fd + *pname++ - '0';
    376   1.8       cgd 			if (fd >= nfiles)
    377   1.8       cgd 				break;
    378   1.8       cgd 		}
    379   1.1       cgd 
    380   1.8       cgd 		if (*pname != '\0') {
    381   1.8       cgd 			error = ENOENT;
    382   1.8       cgd 			goto bad;
    383   1.8       cgd 		}
    384   1.1       cgd 
    385   1.8       cgd 		if (fd >= nfiles || p->p_fd->fd_ofiles[fd] == NULL) {
    386   1.8       cgd 			error = EBADF;
    387   1.8       cgd 			goto bad;
    388   1.8       cgd 		}
    389   1.1       cgd 
    390   1.8       cgd 		error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
    391   1.8       cgd 		if (error)
    392   1.8       cgd 			goto bad;
    393   1.8       cgd 		VTOFDESC(fvp)->fd_fd = fd;
    394  1.14   mycroft 		*vpp = fvp;
    395  1.48  wrstuden 		goto good;
    396   1.8       cgd 	}
    397   1.1       cgd 
    398   1.1       cgd bad:;
    399  1.14   mycroft 	*vpp = NULL;
    400   1.1       cgd 	return (error);
    401  1.48  wrstuden 
    402  1.48  wrstuden good:;
    403  1.48  wrstuden 	/*
    404  1.48  wrstuden 	 * As "." was special cased above, we now unlock the parent if we're
    405  1.48  wrstuden 	 * suppoed to. We're only supposed to not unlock if this is the
    406  1.48  wrstuden 	 * last component, and the caller requested LOCKPARENT. So if either
    407  1.48  wrstuden 	 * condition is false, unlock.
    408  1.48  wrstuden 	 */
    409  1.48  wrstuden 	if (((~cnp->cn_flags) & (ISLASTCN | LOCKPARENT)) != 0) {
    410  1.48  wrstuden 		VOP_UNLOCK(dvp, 0);
    411  1.48  wrstuden 		cnp->cn_flags |= PDIRUNLOCK;
    412  1.48  wrstuden 	}
    413  1.48  wrstuden 	return (0);
    414   1.1       cgd }
    415   1.1       cgd 
    416  1.14   mycroft int
    417  1.30  christos fdesc_open(v)
    418  1.30  christos 	void *v;
    419  1.30  christos {
    420  1.14   mycroft 	struct vop_open_args /* {
    421  1.14   mycroft 		struct vnode *a_vp;
    422  1.14   mycroft 		int  a_mode;
    423  1.14   mycroft 		struct ucred *a_cred;
    424  1.14   mycroft 		struct proc *a_p;
    425  1.30  christos 	} */ *ap = v;
    426  1.14   mycroft 	struct vnode *vp = ap->a_vp;
    427   1.8       cgd 
    428   1.8       cgd 	switch (VTOFDESC(vp)->fd_type) {
    429   1.8       cgd 	case Fdesc:
    430  1.23   mycroft 		/*
    431  1.23   mycroft 		 * XXX Kludge: set p->p_dupfd to contain the value of the
    432  1.23   mycroft 		 * the file descriptor being sought for duplication. The error
    433  1.23   mycroft 		 * return ensures that the vnode for this device will be
    434  1.23   mycroft 		 * released by vn_open. Open will detect this special error and
    435  1.23   mycroft 		 * take the actions in dupfdopen.  Other callers of vn_open or
    436  1.23   mycroft 		 * VOP_OPEN will simply report the error.
    437  1.23   mycroft 		 */
    438  1.23   mycroft 		ap->a_p->p_dupfd = VTOFDESC(vp)->fd_fd;	/* XXX */
    439  1.23   mycroft 		return (ENODEV);
    440   1.1       cgd 
    441   1.8       cgd 	case Fctty:
    442  1.21   mycroft 		return (cttyopen(devctty, ap->a_mode, 0, ap->a_p));
    443  1.30  christos 	case Froot:
    444  1.30  christos 	case Fdevfd:
    445  1.30  christos 	case Flink:
    446  1.30  christos 		break;
    447   1.8       cgd 	}
    448   1.1       cgd 
    449  1.21   mycroft 	return (0);
    450   1.1       cgd }
    451   1.1       cgd 
    452   1.1       cgd static int
    453   1.1       cgd fdesc_attr(fd, vap, cred, p)
    454   1.1       cgd 	int fd;
    455   1.1       cgd 	struct vattr *vap;
    456   1.1       cgd 	struct ucred *cred;
    457   1.1       cgd 	struct proc *p;
    458   1.1       cgd {
    459   1.1       cgd 	struct filedesc *fdp = p->p_fd;
    460   1.1       cgd 	struct file *fp;
    461   1.8       cgd 	struct stat stb;
    462   1.1       cgd 	int error;
    463   1.1       cgd 
    464  1.14   mycroft 	if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
    465   1.1       cgd 		return (EBADF);
    466   1.1       cgd 
    467   1.1       cgd 	switch (fp->f_type) {
    468   1.1       cgd 	case DTYPE_VNODE:
    469   1.1       cgd 		error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, p);
    470   1.8       cgd 		if (error == 0 && vap->va_type == VDIR) {
    471   1.8       cgd 			/*
    472  1.22   mycroft 			 * directories can cause loops in the namespace,
    473  1.22   mycroft 			 * so turn off the 'x' bits to avoid trouble.
    474   1.8       cgd 			 */
    475  1.41   mycroft 			vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
    476   1.8       cgd 		}
    477   1.1       cgd 		break;
    478   1.1       cgd 
    479   1.1       cgd 	case DTYPE_SOCKET:
    480   1.8       cgd 		error = soo_stat((struct socket *)fp->f_data, &stb);
    481   1.8       cgd 		if (error == 0) {
    482   1.8       cgd 			vattr_null(vap);
    483   1.8       cgd 			vap->va_type = VSOCK;
    484   1.8       cgd 			vap->va_mode = stb.st_mode;
    485   1.8       cgd 			vap->va_nlink = stb.st_nlink;
    486   1.8       cgd 			vap->va_uid = stb.st_uid;
    487   1.8       cgd 			vap->va_gid = stb.st_gid;
    488   1.8       cgd 			vap->va_fsid = stb.st_dev;
    489   1.8       cgd 			vap->va_fileid = stb.st_ino;
    490   1.8       cgd 			vap->va_size = stb.st_size;
    491   1.8       cgd 			vap->va_blocksize = stb.st_blksize;
    492  1.12       cgd 			vap->va_atime = stb.st_atimespec;
    493  1.12       cgd 			vap->va_mtime = stb.st_mtimespec;
    494  1.12       cgd 			vap->va_ctime = stb.st_ctimespec;
    495   1.8       cgd 			vap->va_gen = stb.st_gen;
    496   1.8       cgd 			vap->va_flags = stb.st_flags;
    497   1.8       cgd 			vap->va_rdev = stb.st_rdev;
    498   1.8       cgd 			vap->va_bytes = stb.st_blocks * stb.st_blksize;
    499   1.8       cgd 		}
    500   1.1       cgd 		break;
    501   1.1       cgd 
    502   1.1       cgd 	default:
    503   1.1       cgd 		panic("fdesc attr");
    504   1.1       cgd 		break;
    505   1.1       cgd 	}
    506   1.1       cgd 
    507   1.1       cgd 	return (error);
    508   1.1       cgd }
    509   1.1       cgd 
    510  1.14   mycroft int
    511  1.30  christos fdesc_getattr(v)
    512  1.30  christos 	void *v;
    513  1.30  christos {
    514  1.14   mycroft 	struct vop_getattr_args /* {
    515  1.14   mycroft 		struct vnode *a_vp;
    516  1.14   mycroft 		struct vattr *a_vap;
    517  1.14   mycroft 		struct ucred *a_cred;
    518  1.14   mycroft 		struct proc *a_p;
    519  1.30  christos 	} */ *ap = v;
    520  1.14   mycroft 	struct vnode *vp = ap->a_vp;
    521  1.14   mycroft 	struct vattr *vap = ap->a_vap;
    522   1.1       cgd 	unsigned fd;
    523   1.8       cgd 	int error = 0;
    524   1.1       cgd 
    525   1.8       cgd 	switch (VTOFDESC(vp)->fd_type) {
    526   1.8       cgd 	case Froot:
    527   1.8       cgd 	case Fdevfd:
    528   1.8       cgd 	case Flink:
    529   1.8       cgd 	case Fctty:
    530  1.33       mrg 		VATTR_NULL(vap);
    531   1.8       cgd 		vap->va_fileid = VTOFDESC(vp)->fd_ix;
    532   1.8       cgd 
    533  1.30  christos #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH)
    534  1.30  christos #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH)
    535  1.30  christos #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH)
    536  1.30  christos 
    537   1.8       cgd 		switch (VTOFDESC(vp)->fd_type) {
    538   1.8       cgd 		case Flink:
    539  1.30  christos 			vap->va_mode = R_ALL|X_ALL;
    540   1.8       cgd 			vap->va_type = VLNK;
    541  1.32       mrg 			vap->va_rdev = 0;
    542   1.8       cgd 			vap->va_nlink = 1;
    543   1.8       cgd 			vap->va_size = strlen(VTOFDESC(vp)->fd_link);
    544   1.8       cgd 			break;
    545   1.8       cgd 
    546   1.8       cgd 		case Fctty:
    547  1.30  christos 			vap->va_mode = R_ALL|W_ALL;
    548  1.32       mrg 			vap->va_type = VCHR;
    549  1.32       mrg 			vap->va_rdev = devctty;
    550   1.8       cgd 			vap->va_nlink = 1;
    551   1.8       cgd 			vap->va_size = 0;
    552   1.8       cgd 			break;
    553   1.8       cgd 
    554   1.8       cgd 		default:
    555  1.30  christos 			vap->va_mode = R_ALL|X_ALL;
    556   1.8       cgd 			vap->va_type = VDIR;
    557  1.32       mrg 			vap->va_rdev = 0;
    558   1.8       cgd 			vap->va_nlink = 2;
    559   1.8       cgd 			vap->va_size = DEV_BSIZE;
    560   1.8       cgd 			break;
    561   1.8       cgd 		}
    562   1.1       cgd 		vap->va_uid = 0;
    563   1.1       cgd 		vap->va_gid = 0;
    564   1.1       cgd 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
    565   1.1       cgd 		vap->va_blocksize = DEV_BSIZE;
    566  1.28       jtc 		vap->va_atime.tv_sec = boottime.tv_sec;
    567  1.28       jtc 		vap->va_atime.tv_nsec = 0;
    568   1.1       cgd 		vap->va_mtime = vap->va_atime;
    569  1.14   mycroft 		vap->va_ctime = vap->va_mtime;
    570   1.1       cgd 		vap->va_gen = 0;
    571   1.1       cgd 		vap->va_flags = 0;
    572   1.1       cgd 		vap->va_bytes = 0;
    573   1.8       cgd 		break;
    574   1.8       cgd 
    575   1.8       cgd 	case Fdesc:
    576   1.8       cgd 		fd = VTOFDESC(vp)->fd_fd;
    577  1.14   mycroft 		error = fdesc_attr(fd, vap, ap->a_cred, ap->a_p);
    578   1.8       cgd 		break;
    579   1.8       cgd 
    580   1.8       cgd 	default:
    581   1.8       cgd 		panic("fdesc_getattr");
    582   1.8       cgd 		break;
    583   1.1       cgd 	}
    584   1.1       cgd 
    585   1.1       cgd 	if (error == 0)
    586   1.1       cgd 		vp->v_type = vap->va_type;
    587   1.8       cgd 
    588   1.1       cgd 	return (error);
    589   1.1       cgd }
    590   1.1       cgd 
    591  1.14   mycroft int
    592  1.30  christos fdesc_setattr(v)
    593  1.30  christos 	void *v;
    594  1.30  christos {
    595  1.14   mycroft 	struct vop_setattr_args /* {
    596  1.14   mycroft 		struct vnode *a_vp;
    597  1.14   mycroft 		struct vattr *a_vap;
    598  1.14   mycroft 		struct ucred *a_cred;
    599  1.14   mycroft 		struct proc *a_p;
    600  1.30  christos 	} */ *ap = v;
    601  1.14   mycroft 	struct filedesc *fdp = ap->a_p->p_fd;
    602   1.1       cgd 	struct file *fp;
    603   1.1       cgd 	unsigned fd;
    604   1.1       cgd 	int error;
    605   1.1       cgd 
    606   1.1       cgd 	/*
    607   1.1       cgd 	 * Can't mess with the root vnode
    608   1.1       cgd 	 */
    609  1.14   mycroft 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    610   1.8       cgd 	case Fdesc:
    611   1.8       cgd 		break;
    612   1.8       cgd 
    613   1.8       cgd 	case Fctty:
    614   1.8       cgd 		return (0);
    615   1.8       cgd 
    616   1.8       cgd 	default:
    617   1.1       cgd 		return (EACCES);
    618   1.8       cgd 	}
    619   1.1       cgd 
    620  1.14   mycroft 	fd = VTOFDESC(ap->a_vp)->fd_fd;
    621   1.1       cgd 	if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
    622   1.1       cgd 		return (EBADF);
    623   1.1       cgd 	}
    624   1.1       cgd 
    625   1.1       cgd 	/*
    626   1.1       cgd 	 * Can setattr the underlying vnode, but not sockets!
    627   1.1       cgd 	 */
    628   1.1       cgd 	switch (fp->f_type) {
    629   1.1       cgd 	case DTYPE_VNODE:
    630  1.14   mycroft 		error = VOP_SETATTR((struct vnode *) fp->f_data, ap->a_vap, ap->a_cred, ap->a_p);
    631   1.1       cgd 		break;
    632   1.1       cgd 
    633   1.1       cgd 	case DTYPE_SOCKET:
    634  1.14   mycroft 		error = 0;
    635   1.1       cgd 		break;
    636   1.1       cgd 
    637   1.1       cgd 	default:
    638   1.1       cgd 		panic("fdesc setattr");
    639   1.1       cgd 		break;
    640   1.1       cgd 	}
    641   1.1       cgd 
    642   1.1       cgd 	return (error);
    643   1.1       cgd }
    644   1.1       cgd 
    645  1.25   mycroft #define UIO_MX 32
    646   1.8       cgd 
    647  1.25   mycroft struct fdesc_target {
    648  1.25   mycroft 	ino_t ft_fileno;
    649  1.25   mycroft 	u_char ft_type;
    650  1.25   mycroft 	u_char ft_namlen;
    651  1.25   mycroft 	char *ft_name;
    652  1.25   mycroft } fdesc_targets[] = {
    653  1.25   mycroft /* NOTE: The name must be less than UIO_MX-16 chars in length */
    654  1.25   mycroft #define N(s) sizeof(s)-1, s
    655  1.25   mycroft 	{ FD_DEVFD,  DT_DIR,     N("fd")     },
    656  1.27   mycroft 	{ FD_STDIN,  DT_LNK,     N("stdin")  },
    657  1.27   mycroft 	{ FD_STDOUT, DT_LNK,     N("stdout") },
    658  1.27   mycroft 	{ FD_STDERR, DT_LNK,     N("stderr") },
    659  1.25   mycroft 	{ FD_CTTY,   DT_UNKNOWN, N("tty")    },
    660  1.25   mycroft #undef N
    661   1.8       cgd };
    662  1.25   mycroft static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
    663   1.8       cgd 
    664  1.14   mycroft int
    665  1.30  christos fdesc_readdir(v)
    666  1.30  christos 	void *v;
    667  1.30  christos {
    668  1.14   mycroft 	struct vop_readdir_args /* {
    669  1.14   mycroft 		struct vnode *a_vp;
    670  1.14   mycroft 		struct uio *a_uio;
    671  1.14   mycroft 		struct ucred *a_cred;
    672  1.22   mycroft 		int *a_eofflag;
    673  1.44      fvdl 		off_t **a_cookies;
    674  1.44      fvdl 		int *a_ncookies;
    675  1.30  christos 	} */ *ap = v;
    676  1.14   mycroft 	struct uio *uio = ap->a_uio;
    677  1.25   mycroft 	struct dirent d;
    678   1.1       cgd 	struct filedesc *fdp;
    679   1.8       cgd 	int i;
    680   1.1       cgd 	int error;
    681  1.44      fvdl 	off_t *cookies = NULL;
    682  1.44      fvdl 	int ncookies = 0;
    683  1.11        ws 
    684  1.14   mycroft 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    685   1.8       cgd 	case Fctty:
    686   1.8       cgd 		return (0);
    687   1.8       cgd 
    688   1.8       cgd 	case Fdesc:
    689   1.8       cgd 		return (ENOTDIR);
    690  1.30  christos 
    691  1.30  christos 	default:
    692  1.30  christos 		break;
    693   1.8       cgd 	}
    694   1.1       cgd 
    695   1.1       cgd 	fdp = uio->uio_procp->p_fd;
    696   1.8       cgd 
    697  1.25   mycroft 	if (uio->uio_resid < UIO_MX)
    698  1.25   mycroft 		return (EINVAL);
    699  1.26   mycroft 	if (uio->uio_offset < 0)
    700  1.25   mycroft 		return (EINVAL);
    701  1.25   mycroft 
    702  1.25   mycroft 	error = 0;
    703  1.26   mycroft 	i = uio->uio_offset;
    704  1.46     perry 	memset((caddr_t)&d, 0, UIO_MX);
    705  1.25   mycroft 	d.d_reclen = UIO_MX;
    706  1.44      fvdl 	if (ap->a_ncookies)
    707  1.44      fvdl 		ncookies = (uio->uio_resid / UIO_MX);
    708  1.25   mycroft 
    709  1.14   mycroft 	if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
    710  1.25   mycroft 		struct fdesc_target *ft;
    711   1.3       cgd 
    712  1.44      fvdl 		if (ap->a_ncookies) {
    713  1.44      fvdl 			ncookies = min(ncookies, (nfdesc_targets - i));
    714  1.44      fvdl 			MALLOC(cookies, off_t *, ncookies * sizeof(off_t),
    715  1.44      fvdl 			    M_TEMP, M_WAITOK);
    716  1.44      fvdl 			*ap->a_cookies = cookies;
    717  1.44      fvdl 			*ap->a_ncookies = ncookies;
    718  1.44      fvdl 		}
    719  1.44      fvdl 
    720  1.25   mycroft 		for (ft = &fdesc_targets[i];
    721  1.25   mycroft 		     uio->uio_resid >= UIO_MX && i < nfdesc_targets; ft++, i++) {
    722  1.25   mycroft 			switch (ft->ft_fileno) {
    723   1.8       cgd 			case FD_CTTY:
    724   1.8       cgd 				if (cttyvp(uio->uio_procp) == NULL)
    725   1.8       cgd 					continue;
    726   1.8       cgd 				break;
    727   1.8       cgd 
    728   1.8       cgd 			case FD_STDIN:
    729   1.8       cgd 			case FD_STDOUT:
    730   1.8       cgd 			case FD_STDERR:
    731  1.25   mycroft 				if ((ft->ft_fileno - FD_STDIN) >= fdp->fd_nfiles)
    732   1.8       cgd 					continue;
    733  1.25   mycroft 				if (fdp->fd_ofiles[ft->ft_fileno - FD_STDIN] == NULL)
    734   1.8       cgd 					continue;
    735   1.8       cgd 				break;
    736   1.8       cgd 			}
    737  1.25   mycroft 
    738  1.25   mycroft 			d.d_fileno = ft->ft_fileno;
    739  1.25   mycroft 			d.d_namlen = ft->ft_namlen;
    740  1.46     perry 			memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
    741  1.25   mycroft 			d.d_type = ft->ft_type;
    742  1.25   mycroft 
    743  1.30  christos 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    744   1.8       cgd 				break;
    745  1.44      fvdl 			if (cookies)
    746  1.26   mycroft 				*cookies++ = i + 1;
    747   1.3       cgd 		}
    748  1.25   mycroft 	} else {
    749  1.44      fvdl 		if (ap->a_ncookies) {
    750  1.44      fvdl 			ncookies = min(ncookies, (fdp->fd_nfiles + 2));
    751  1.44      fvdl 			MALLOC(cookies, off_t *, ncookies * sizeof(off_t),				    M_TEMP, M_WAITOK);
    752  1.44      fvdl 			*ap->a_cookies = cookies;
    753  1.44      fvdl 			*ap->a_ncookies = ncookies;
    754  1.44      fvdl 		}
    755  1.25   mycroft 		for (; i - 2 < fdp->fd_nfiles && uio->uio_resid >= UIO_MX;
    756  1.25   mycroft 		     i++) {
    757  1.25   mycroft 			switch (i) {
    758  1.25   mycroft 			case 0:
    759  1.25   mycroft 			case 1:
    760  1.25   mycroft 				d.d_fileno = FD_ROOT;		/* XXX */
    761  1.25   mycroft 				d.d_namlen = i + 1;
    762  1.46     perry 				memcpy(d.d_name, "..", d.d_namlen);
    763  1.25   mycroft 				d.d_name[i + 1] = '\0';
    764  1.25   mycroft 				d.d_type = DT_DIR;
    765  1.25   mycroft 				break;
    766  1.25   mycroft 
    767  1.25   mycroft 			default:
    768  1.25   mycroft 				if (fdp->fd_ofiles[i - 2] == NULL)
    769  1.25   mycroft 					continue;
    770  1.25   mycroft 				d.d_fileno = i - 2 + FD_STDIN;
    771  1.37  christos 				d.d_namlen = sprintf(d.d_name, "%d", i - 2);
    772  1.25   mycroft 				d.d_type = DT_UNKNOWN;
    773  1.25   mycroft 				break;
    774  1.25   mycroft 			}
    775   1.8       cgd 
    776  1.30  christos 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    777   1.1       cgd 				break;
    778  1.44      fvdl 			if (cookies)
    779  1.26   mycroft 				*cookies++ = i + 1;
    780   1.1       cgd 		}
    781   1.8       cgd 	}
    782   1.8       cgd 
    783  1.44      fvdl 	if (ap->a_ncookies && error) {
    784  1.44      fvdl 		FREE(*ap->a_cookies, M_TEMP);
    785  1.44      fvdl 		*ap->a_ncookies = 0;
    786  1.44      fvdl 		*ap->a_cookies = NULL;
    787  1.44      fvdl 	}
    788  1.44      fvdl 
    789  1.26   mycroft 	uio->uio_offset = i;
    790   1.8       cgd 	return (error);
    791   1.8       cgd }
    792   1.8       cgd 
    793  1.14   mycroft int
    794  1.30  christos fdesc_readlink(v)
    795  1.30  christos 	void *v;
    796  1.30  christos {
    797  1.14   mycroft 	struct vop_readlink_args /* {
    798  1.14   mycroft 		struct vnode *a_vp;
    799  1.14   mycroft 		struct uio *a_uio;
    800  1.14   mycroft 		struct ucred *a_cred;
    801  1.30  christos 	} */ *ap = v;
    802  1.14   mycroft 	struct vnode *vp = ap->a_vp;
    803   1.8       cgd 	int error;
    804   1.8       cgd 
    805  1.14   mycroft 	if (vp->v_type != VLNK)
    806  1.14   mycroft 		return (EPERM);
    807  1.14   mycroft 
    808   1.8       cgd 	if (VTOFDESC(vp)->fd_type == Flink) {
    809   1.8       cgd 		char *ln = VTOFDESC(vp)->fd_link;
    810  1.14   mycroft 		error = uiomove(ln, strlen(ln), ap->a_uio);
    811   1.8       cgd 	} else {
    812   1.8       cgd 		error = EOPNOTSUPP;
    813   1.8       cgd 	}
    814   1.8       cgd 
    815   1.8       cgd 	return (error);
    816   1.8       cgd }
    817   1.8       cgd 
    818  1.14   mycroft int
    819  1.30  christos fdesc_read(v)
    820  1.30  christos 	void *v;
    821  1.30  christos {
    822  1.14   mycroft 	struct vop_read_args /* {
    823  1.14   mycroft 		struct vnode *a_vp;
    824  1.14   mycroft 		struct uio *a_uio;
    825  1.14   mycroft 		int  a_ioflag;
    826  1.14   mycroft 		struct ucred *a_cred;
    827  1.30  christos 	} */ *ap = v;
    828   1.8       cgd 	int error = EOPNOTSUPP;
    829   1.8       cgd 
    830  1.14   mycroft 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    831   1.8       cgd 	case Fctty:
    832  1.14   mycroft 		error = cttyread(devctty, ap->a_uio, ap->a_ioflag);
    833   1.8       cgd 		break;
    834   1.8       cgd 
    835   1.8       cgd 	default:
    836   1.8       cgd 		error = EOPNOTSUPP;
    837   1.8       cgd 		break;
    838   1.8       cgd 	}
    839   1.8       cgd 
    840   1.8       cgd 	return (error);
    841   1.8       cgd }
    842   1.8       cgd 
    843  1.14   mycroft int
    844  1.30  christos fdesc_write(v)
    845  1.30  christos 	void *v;
    846  1.30  christos {
    847  1.14   mycroft 	struct vop_write_args /* {
    848  1.14   mycroft 		struct vnode *a_vp;
    849  1.14   mycroft 		struct uio *a_uio;
    850  1.14   mycroft 		int  a_ioflag;
    851  1.14   mycroft 		struct ucred *a_cred;
    852  1.30  christos 	} */ *ap = v;
    853   1.8       cgd 	int error = EOPNOTSUPP;
    854   1.8       cgd 
    855  1.14   mycroft 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    856   1.8       cgd 	case Fctty:
    857  1.14   mycroft 		error = cttywrite(devctty, ap->a_uio, ap->a_ioflag);
    858   1.8       cgd 		break;
    859   1.8       cgd 
    860   1.8       cgd 	default:
    861   1.8       cgd 		error = EOPNOTSUPP;
    862   1.8       cgd 		break;
    863   1.8       cgd 	}
    864   1.8       cgd 
    865   1.8       cgd 	return (error);
    866   1.8       cgd }
    867   1.8       cgd 
    868  1.14   mycroft int
    869  1.30  christos fdesc_ioctl(v)
    870  1.30  christos 	void *v;
    871  1.30  christos {
    872  1.14   mycroft 	struct vop_ioctl_args /* {
    873  1.14   mycroft 		struct vnode *a_vp;
    874  1.19       cgd 		u_long a_command;
    875  1.14   mycroft 		caddr_t  a_data;
    876  1.14   mycroft 		int  a_fflag;
    877  1.14   mycroft 		struct ucred *a_cred;
    878  1.14   mycroft 		struct proc *a_p;
    879  1.30  christos 	} */ *ap = v;
    880   1.8       cgd 	int error = EOPNOTSUPP;
    881   1.8       cgd 
    882  1.14   mycroft 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    883   1.8       cgd 	case Fctty:
    884  1.14   mycroft 		error = cttyioctl(devctty, ap->a_command, ap->a_data,
    885  1.30  christos 				  ap->a_fflag, ap->a_p);
    886   1.8       cgd 		break;
    887   1.8       cgd 
    888   1.8       cgd 	default:
    889   1.8       cgd 		error = EOPNOTSUPP;
    890   1.8       cgd 		break;
    891   1.1       cgd 	}
    892   1.8       cgd 
    893   1.8       cgd 	return (error);
    894   1.8       cgd }
    895   1.8       cgd 
    896  1.14   mycroft int
    897  1.35   mycroft fdesc_poll(v)
    898  1.30  christos 	void *v;
    899  1.30  christos {
    900  1.35   mycroft 	struct vop_poll_args /* {
    901  1.14   mycroft 		struct vnode *a_vp;
    902  1.35   mycroft 		int a_events;
    903  1.14   mycroft 		struct proc *a_p;
    904  1.30  christos 	} */ *ap = v;
    905  1.35   mycroft 	int revents;
    906   1.8       cgd 
    907  1.14   mycroft 	switch (VTOFDESC(ap->a_vp)->fd_type) {
    908   1.8       cgd 	case Fctty:
    909  1.35   mycroft 		revents = cttypoll(devctty, ap->a_events, ap->a_p);
    910   1.8       cgd 		break;
    911   1.1       cgd 
    912   1.8       cgd 	default:
    913  1.35   mycroft 		revents = genfs_poll(v);
    914   1.8       cgd 		break;
    915   1.8       cgd 	}
    916  1.34   mycroft 
    917  1.35   mycroft 	return (revents);
    918   1.1       cgd }
    919   1.1       cgd 
    920  1.14   mycroft int
    921  1.30  christos fdesc_inactive(v)
    922  1.30  christos 	void *v;
    923  1.30  christos {
    924  1.14   mycroft 	struct vop_inactive_args /* {
    925  1.14   mycroft 		struct vnode *a_vp;
    926  1.44      fvdl 		struct proc *a_p;
    927  1.30  christos 	} */ *ap = v;
    928  1.14   mycroft 	struct vnode *vp = ap->a_vp;
    929  1.14   mycroft 
    930   1.1       cgd 	/*
    931   1.1       cgd 	 * Clear out the v_type field to avoid
    932   1.1       cgd 	 * nasty things happening in vgone().
    933   1.1       cgd 	 */
    934  1.44      fvdl 	VOP_UNLOCK(vp, 0);
    935   1.1       cgd 	vp->v_type = VNON;
    936   1.1       cgd 	return (0);
    937   1.1       cgd }
    938   1.1       cgd 
    939  1.14   mycroft int
    940  1.30  christos fdesc_reclaim(v)
    941  1.30  christos 	void *v;
    942  1.30  christos {
    943  1.14   mycroft 	struct vop_reclaim_args /* {
    944  1.14   mycroft 		struct vnode *a_vp;
    945  1.30  christos 	} */ *ap = v;
    946  1.14   mycroft 	struct vnode *vp = ap->a_vp;
    947  1.17   mycroft 	struct fdescnode *fd = VTOFDESC(vp);
    948  1.14   mycroft 
    949  1.17   mycroft 	LIST_REMOVE(fd, fd_hash);
    950  1.14   mycroft 	FREE(vp->v_data, M_TEMP);
    951  1.14   mycroft 	vp->v_data = 0;
    952  1.14   mycroft 
    953  1.14   mycroft 	return (0);
    954  1.14   mycroft }
    955  1.14   mycroft 
    956  1.14   mycroft /*
    957  1.14   mycroft  * Return POSIX pathconf information applicable to special devices.
    958  1.14   mycroft  */
    959  1.30  christos int
    960  1.30  christos fdesc_pathconf(v)
    961  1.30  christos 	void *v;
    962  1.30  christos {
    963  1.14   mycroft 	struct vop_pathconf_args /* {
    964  1.14   mycroft 		struct vnode *a_vp;
    965  1.14   mycroft 		int a_name;
    966  1.18       cgd 		register_t *a_retval;
    967  1.30  christos 	} */ *ap = v;
    968   1.8       cgd 
    969  1.14   mycroft 	switch (ap->a_name) {
    970  1.14   mycroft 	case _PC_LINK_MAX:
    971  1.14   mycroft 		*ap->a_retval = LINK_MAX;
    972  1.14   mycroft 		return (0);
    973  1.14   mycroft 	case _PC_MAX_CANON:
    974  1.14   mycroft 		*ap->a_retval = MAX_CANON;
    975  1.14   mycroft 		return (0);
    976  1.14   mycroft 	case _PC_MAX_INPUT:
    977  1.14   mycroft 		*ap->a_retval = MAX_INPUT;
    978  1.14   mycroft 		return (0);
    979  1.14   mycroft 	case _PC_PIPE_BUF:
    980  1.14   mycroft 		*ap->a_retval = PIPE_BUF;
    981  1.14   mycroft 		return (0);
    982  1.14   mycroft 	case _PC_CHOWN_RESTRICTED:
    983  1.14   mycroft 		*ap->a_retval = 1;
    984  1.14   mycroft 		return (0);
    985  1.14   mycroft 	case _PC_VDISABLE:
    986  1.14   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
    987  1.45    kleink 		return (0);
    988  1.45    kleink 	case _PC_SYNC_IO:
    989  1.45    kleink 		*ap->a_retval = 1;
    990  1.14   mycroft 		return (0);
    991  1.14   mycroft 	default:
    992  1.14   mycroft 		return (EINVAL);
    993   1.8       cgd 	}
    994  1.14   mycroft 	/* NOTREACHED */
    995   1.8       cgd }
    996   1.8       cgd 
    997   1.1       cgd /*
    998   1.1       cgd  * Print out the contents of a /dev/fd vnode.
    999   1.1       cgd  */
   1000   1.1       cgd /* ARGSUSED */
   1001  1.14   mycroft int
   1002  1.30  christos fdesc_print(v)
   1003  1.30  christos 	void *v;
   1004   1.1       cgd {
   1005  1.37  christos 	printf("tag VT_NON, fdesc vnode\n");
   1006  1.14   mycroft 	return (0);
   1007  1.14   mycroft }
   1008  1.14   mycroft 
   1009  1.14   mycroft int
   1010  1.30  christos fdesc_link(v)
   1011  1.30  christos 	void *v;
   1012  1.30  christos {
   1013  1.29   mycroft 	struct vop_link_args /* {
   1014  1.29   mycroft 		struct vnode *a_dvp;
   1015  1.29   mycroft 		struct vnode *a_vp;
   1016  1.29   mycroft 		struct componentname *a_cnp;
   1017  1.30  christos 	} */ *ap = v;
   1018  1.29   mycroft 
   1019  1.29   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1020  1.29   mycroft 	vput(ap->a_dvp);
   1021  1.29   mycroft 	return (EROFS);
   1022  1.29   mycroft }
   1023  1.29   mycroft 
   1024  1.29   mycroft int
   1025  1.30  christos fdesc_symlink(v)
   1026  1.30  christos 	void *v;
   1027  1.30  christos {
   1028  1.29   mycroft 	struct vop_symlink_args /* {
   1029  1.29   mycroft 		struct vnode *a_dvp;
   1030  1.29   mycroft 		struct vnode **a_vpp;
   1031  1.29   mycroft 		struct componentname *a_cnp;
   1032  1.29   mycroft 		struct vattr *a_vap;
   1033  1.29   mycroft 		char *a_target;
   1034  1.30  christos 	} */ *ap = v;
   1035  1.29   mycroft 
   1036  1.29   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1037  1.29   mycroft 	vput(ap->a_dvp);
   1038  1.29   mycroft 	return (EROFS);
   1039   1.1       cgd }
   1040