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