Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vfsops.c revision 1.26
      1  1.26     perry /*	$NetBSD: fdesc_vfsops.c,v 1.26 1998/08/09 20:51:08 perry Exp $	*/
      2  1.13       cgd 
      3   1.1       cgd /*
      4  1.24      fvdl  * Copyright (c) 1992, 1993, 1995
      5  1.12   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.6       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.6       cgd  *	This product includes software developed by the University of
     21   1.6       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.24      fvdl  *	@(#)fdesc_vfsops.c	8.10 (Berkeley) 5/14/95
     39  1.15   mycroft  *
     40  1.15   mycroft  * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 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.25  jonathan 
     47  1.25  jonathan #if defined(_KERNEL) && !defined(_LKM)
     48  1.25  jonathan #include "opt_compat_netbsd.h"
     49  1.25  jonathan #endif
     50   1.1       cgd 
     51   1.6       cgd #include <sys/param.h>
     52   1.6       cgd #include <sys/systm.h>
     53   1.6       cgd #include <sys/time.h>
     54   1.6       cgd #include <sys/types.h>
     55   1.6       cgd #include <sys/proc.h>
     56   1.6       cgd #include <sys/resourcevar.h>
     57   1.6       cgd #include <sys/filedesc.h>
     58   1.6       cgd #include <sys/vnode.h>
     59   1.6       cgd #include <sys/mount.h>
     60   1.6       cgd #include <sys/namei.h>
     61   1.6       cgd #include <sys/malloc.h>
     62   1.6       cgd #include <miscfs/fdesc/fdesc.h>
     63   1.1       cgd 
     64  1.22       cgd int	fdesc_mount __P((struct mount *, const char *, void *,
     65  1.21  christos 			 struct nameidata *, struct proc *));
     66  1.21  christos int	fdesc_start __P((struct mount *, int, struct proc *));
     67  1.21  christos int	fdesc_unmount __P((struct mount *, int, struct proc *));
     68  1.21  christos int	fdesc_root __P((struct mount *, struct vnode **));
     69  1.21  christos int	fdesc_quotactl __P((struct mount *, int, uid_t, caddr_t,
     70  1.21  christos 			    struct proc *));
     71  1.21  christos int	fdesc_statfs __P((struct mount *, struct statfs *, struct proc *));
     72  1.21  christos int	fdesc_sync __P((struct mount *, int, struct ucred *, struct proc *));
     73  1.21  christos int	fdesc_vget __P((struct mount *, ino_t, struct vnode **));
     74  1.21  christos int	fdesc_fhtovp __P((struct mount *, struct fid *, struct mbuf *,
     75  1.21  christos 			  struct vnode **, int *, struct ucred **));
     76  1.21  christos int	fdesc_vptofh __P((struct vnode *, struct fid *));
     77  1.24      fvdl int	fdesc_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
     78  1.24      fvdl 			  struct proc *));
     79  1.21  christos 
     80   1.1       cgd /*
     81   1.1       cgd  * Mount the per-process file descriptors (/dev/fd)
     82   1.1       cgd  */
     83  1.12   mycroft int
     84   1.1       cgd fdesc_mount(mp, path, data, ndp, p)
     85   1.1       cgd 	struct mount *mp;
     86  1.22       cgd 	const char *path;
     87  1.22       cgd 	void *data;
     88   1.1       cgd 	struct nameidata *ndp;
     89   1.1       cgd 	struct proc *p;
     90   1.1       cgd {
     91   1.1       cgd 	int error = 0;
     92  1.19   mycroft 	size_t size;
     93   1.1       cgd 	struct fdescmount *fmp;
     94   1.1       cgd 	struct vnode *rvp;
     95   1.1       cgd 
     96   1.1       cgd 	/*
     97   1.1       cgd 	 * Update is a no-op
     98   1.1       cgd 	 */
     99   1.1       cgd 	if (mp->mnt_flag & MNT_UPDATE)
    100   1.1       cgd 		return (EOPNOTSUPP);
    101   1.1       cgd 
    102   1.6       cgd 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
    103   1.1       cgd 	if (error)
    104   1.1       cgd 		return (error);
    105   1.1       cgd 
    106   1.6       cgd 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
    107  1.12   mycroft 				M_UFSMNT, M_WAITOK);	/* XXX */
    108   1.1       cgd 	rvp->v_type = VDIR;
    109   1.1       cgd 	rvp->v_flag |= VROOT;
    110   1.1       cgd 	fmp->f_root = rvp;
    111  1.14   mycroft 	mp->mnt_flag |= MNT_LOCAL;
    112  1.14   mycroft 	mp->mnt_data = (qaddr_t)fmp;
    113  1.24      fvdl 	vfs_getnewfsid(mp, MOUNT_FDESC);
    114   1.1       cgd 
    115   1.1       cgd 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
    116  1.26     perry 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
    117  1.26     perry 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
    118  1.26     perry 	memcpy(mp->mnt_stat.f_mntfromname, "fdesc", sizeof("fdesc"));
    119   1.1       cgd 	return (0);
    120   1.1       cgd }
    121   1.1       cgd 
    122  1.12   mycroft int
    123   1.1       cgd fdesc_start(mp, flags, p)
    124   1.1       cgd 	struct mount *mp;
    125   1.1       cgd 	int flags;
    126   1.1       cgd 	struct proc *p;
    127   1.1       cgd {
    128   1.1       cgd 	return (0);
    129   1.1       cgd }
    130   1.1       cgd 
    131  1.12   mycroft int
    132   1.1       cgd fdesc_unmount(mp, mntflags, p)
    133   1.1       cgd 	struct mount *mp;
    134   1.1       cgd 	int mntflags;
    135   1.1       cgd 	struct proc *p;
    136   1.1       cgd {
    137   1.1       cgd 	int error;
    138   1.1       cgd 	int flags = 0;
    139   1.1       cgd 	struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
    140   1.1       cgd 
    141  1.24      fvdl 	if (mntflags & MNT_FORCE)
    142   1.1       cgd 		flags |= FORCECLOSE;
    143   1.1       cgd 
    144   1.1       cgd 	/*
    145   1.1       cgd 	 * Clear out buffer cache.  I don't think we
    146   1.1       cgd 	 * ever get anything cached at this level at the
    147   1.1       cgd 	 * moment, but who knows...
    148   1.1       cgd 	 */
    149   1.1       cgd 	if (rootvp->v_usecount > 1)
    150   1.1       cgd 		return (EBUSY);
    151  1.21  christos 	if ((error = vflush(mp, rootvp, flags)) != 0)
    152   1.1       cgd 		return (error);
    153   1.1       cgd 
    154   1.1       cgd 	/*
    155   1.1       cgd 	 * Release reference on underlying root vnode
    156   1.1       cgd 	 */
    157   1.1       cgd 	vrele(rootvp);
    158   1.1       cgd 	/*
    159   1.1       cgd 	 * And blow it away for future re-use
    160   1.1       cgd 	 */
    161   1.1       cgd 	vgone(rootvp);
    162   1.1       cgd 	/*
    163   1.1       cgd 	 * Finally, throw away the fdescmount structure
    164   1.1       cgd 	 */
    165  1.12   mycroft 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    166   1.1       cgd 	mp->mnt_data = 0;
    167  1.12   mycroft 
    168  1.12   mycroft 	return (0);
    169   1.1       cgd }
    170   1.1       cgd 
    171  1.12   mycroft int
    172   1.1       cgd fdesc_root(mp, vpp)
    173   1.1       cgd 	struct mount *mp;
    174   1.1       cgd 	struct vnode **vpp;
    175   1.1       cgd {
    176   1.1       cgd 	struct vnode *vp;
    177   1.1       cgd 
    178   1.1       cgd 	/*
    179   1.1       cgd 	 * Return locked reference to root.
    180   1.1       cgd 	 */
    181   1.1       cgd 	vp = VFSTOFDESC(mp)->f_root;
    182   1.1       cgd 	VREF(vp);
    183  1.24      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    184   1.1       cgd 	*vpp = vp;
    185   1.1       cgd 	return (0);
    186   1.1       cgd }
    187   1.1       cgd 
    188  1.12   mycroft int
    189   1.1       cgd fdesc_quotactl(mp, cmd, uid, arg, p)
    190   1.1       cgd 	struct mount *mp;
    191   1.1       cgd 	int cmd;
    192   1.1       cgd 	uid_t uid;
    193   1.1       cgd 	caddr_t arg;
    194   1.1       cgd 	struct proc *p;
    195   1.1       cgd {
    196  1.12   mycroft 
    197   1.1       cgd 	return (EOPNOTSUPP);
    198   1.1       cgd }
    199   1.1       cgd 
    200  1.12   mycroft int
    201   1.1       cgd fdesc_statfs(mp, sbp, p)
    202   1.1       cgd 	struct mount *mp;
    203   1.1       cgd 	struct statfs *sbp;
    204   1.1       cgd 	struct proc *p;
    205   1.1       cgd {
    206   1.1       cgd 	struct filedesc *fdp;
    207   1.1       cgd 	int lim;
    208   1.1       cgd 	int i;
    209   1.1       cgd 	int last;
    210   1.1       cgd 	int freefd;
    211   1.1       cgd 
    212   1.1       cgd 	/*
    213   1.1       cgd 	 * Compute number of free file descriptors.
    214   1.1       cgd 	 * [ Strange results will ensue if the open file
    215   1.1       cgd 	 * limit is ever reduced below the current number
    216   1.1       cgd 	 * of open files... ]
    217   1.1       cgd 	 */
    218   1.5   mycroft 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
    219   1.1       cgd 	fdp = p->p_fd;
    220   1.1       cgd 	last = min(fdp->fd_nfiles, lim);
    221   1.1       cgd 	freefd = 0;
    222   1.1       cgd 	for (i = fdp->fd_freefile; i < last; i++)
    223   1.1       cgd 		if (fdp->fd_ofiles[i] == NULL)
    224   1.1       cgd 			freefd++;
    225   1.1       cgd 
    226   1.1       cgd 	/*
    227   1.1       cgd 	 * Adjust for the fact that the fdesc array may not
    228   1.1       cgd 	 * have been fully allocated yet.
    229   1.1       cgd 	 */
    230   1.1       cgd 	if (fdp->fd_nfiles < lim)
    231   1.1       cgd 		freefd += (lim - fdp->fd_nfiles);
    232   1.1       cgd 
    233   1.1       cgd 	sbp->f_bsize = DEV_BSIZE;
    234  1.10       cgd 	sbp->f_iosize = DEV_BSIZE;
    235   1.1       cgd 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    236   1.1       cgd 	sbp->f_bfree = 0;
    237   1.1       cgd 	sbp->f_bavail = 0;
    238   1.1       cgd 	sbp->f_files = lim + 1;		/* Allow for "." */
    239   1.1       cgd 	sbp->f_ffree = freefd;		/* See comments above */
    240  1.24      fvdl #ifdef COMPAT_09
    241  1.24      fvdl 	sbp->f_type = 6;
    242  1.24      fvdl #else
    243  1.24      fvdl 	sbp->f_type = 0;
    244  1.24      fvdl #endif
    245   1.1       cgd 	if (sbp != &mp->mnt_stat) {
    246  1.26     perry 		memcpy(&sbp->f_fsid, &mp->mnt_stat.f_fsid, sizeof(sbp->f_fsid));
    247  1.26     perry 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    248  1.26     perry 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    249   1.1       cgd 	}
    250  1.17   mycroft 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    251   1.1       cgd 	return (0);
    252   1.1       cgd }
    253   1.1       cgd 
    254  1.21  christos /*ARGSUSED*/
    255  1.12   mycroft int
    256  1.21  christos fdesc_sync(mp, waitfor, uc, p)
    257   1.1       cgd 	struct mount *mp;
    258   1.1       cgd 	int waitfor;
    259  1.21  christos 	struct ucred *uc;
    260  1.21  christos 	struct proc *p;
    261   1.1       cgd {
    262  1.12   mycroft 
    263   1.1       cgd 	return (0);
    264   1.1       cgd }
    265   1.1       cgd 
    266  1.12   mycroft /*
    267  1.12   mycroft  * Fdesc flat namespace lookup.
    268  1.12   mycroft  * Currently unsupported.
    269  1.12   mycroft  */
    270  1.12   mycroft int
    271  1.12   mycroft fdesc_vget(mp, ino, vpp)
    272  1.12   mycroft 	struct mount *mp;
    273  1.12   mycroft 	ino_t ino;
    274  1.12   mycroft 	struct vnode **vpp;
    275  1.12   mycroft {
    276  1.12   mycroft 
    277  1.12   mycroft 	return (EOPNOTSUPP);
    278  1.12   mycroft }
    279  1.12   mycroft 
    280  1.21  christos 
    281  1.21  christos /*ARGSUSED*/
    282  1.12   mycroft int
    283  1.21  christos fdesc_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
    284   1.1       cgd 	struct mount *mp;
    285   1.1       cgd 	struct fid *fhp;
    286  1.21  christos 	struct mbuf *nam;
    287   1.1       cgd 	struct vnode **vpp;
    288  1.21  christos 	int *exflagsp;
    289  1.21  christos 	struct ucred **credanonp;
    290   1.1       cgd {
    291  1.21  christos 
    292   1.1       cgd 	return (EOPNOTSUPP);
    293   1.1       cgd }
    294   1.1       cgd 
    295  1.21  christos /*ARGSUSED*/
    296  1.12   mycroft int
    297   1.1       cgd fdesc_vptofh(vp, fhp)
    298   1.1       cgd 	struct vnode *vp;
    299   1.1       cgd 	struct fid *fhp;
    300   1.1       cgd {
    301   1.1       cgd 	return (EOPNOTSUPP);
    302   1.1       cgd }
    303   1.1       cgd 
    304  1.24      fvdl int
    305  1.24      fvdl fdesc_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    306  1.24      fvdl 	int *name;
    307  1.24      fvdl 	u_int namelen;
    308  1.24      fvdl 	void *oldp;
    309  1.24      fvdl 	size_t *oldlenp;
    310  1.24      fvdl 	void *newp;
    311  1.24      fvdl 	size_t newlen;
    312  1.24      fvdl 	struct proc *p;
    313  1.24      fvdl {
    314  1.24      fvdl 	return (EOPNOTSUPP);
    315  1.24      fvdl }
    316  1.24      fvdl 
    317  1.23   thorpej extern struct vnodeopv_desc fdesc_vnodeop_opv_desc;
    318  1.23   thorpej 
    319  1.23   thorpej struct vnodeopv_desc *fdesc_vnodeopv_descs[] = {
    320  1.23   thorpej 	&fdesc_vnodeop_opv_desc,
    321  1.23   thorpej 	NULL,
    322  1.23   thorpej };
    323  1.23   thorpej 
    324   1.1       cgd struct vfsops fdesc_vfsops = {
    325   1.9       cgd 	MOUNT_FDESC,
    326   1.1       cgd 	fdesc_mount,
    327   1.1       cgd 	fdesc_start,
    328   1.1       cgd 	fdesc_unmount,
    329   1.1       cgd 	fdesc_root,
    330   1.1       cgd 	fdesc_quotactl,
    331   1.1       cgd 	fdesc_statfs,
    332   1.1       cgd 	fdesc_sync,
    333  1.12   mycroft 	fdesc_vget,
    334   1.1       cgd 	fdesc_fhtovp,
    335   1.1       cgd 	fdesc_vptofh,
    336   1.1       cgd 	fdesc_init,
    337  1.24      fvdl 	fdesc_sysctl,
    338  1.23   thorpej 	NULL,				/* vfs_mountroot */
    339  1.23   thorpej 	fdesc_vnodeopv_descs,
    340   1.1       cgd };
    341