Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vfsops.c revision 1.1.1.2
      1 /*
      2  * Copyright (c) 1992, 1993, 1995
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software donated to Berkeley by
      6  * Jan-Simon Pendry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)fdesc_vfsops.c	8.10 (Berkeley) 5/14/95
     37  *
     38  * $Id: fdesc_vfsops.c,v 1.1.1.2 1998/03/01 02:13:12 fvdl Exp $
     39  */
     40 
     41 /*
     42  * /dev/fd Filesystem
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/time.h>
     48 #include <sys/types.h>
     49 #include <sys/proc.h>
     50 #include <sys/resourcevar.h>
     51 #include <sys/filedesc.h>
     52 #include <sys/vnode.h>
     53 #include <sys/mount.h>
     54 #include <sys/namei.h>
     55 #include <sys/malloc.h>
     56 #include <miscfs/fdesc/fdesc.h>
     57 
     58 /*
     59  * Mount the per-process file descriptors (/dev/fd)
     60  */
     61 int
     62 fdesc_mount(mp, path, data, ndp, p)
     63 	struct mount *mp;
     64 	char *path;
     65 	caddr_t data;
     66 	struct nameidata *ndp;
     67 	struct proc *p;
     68 {
     69 	int error = 0;
     70 	u_int size;
     71 	struct fdescmount *fmp;
     72 	struct vnode *rvp;
     73 
     74 	/*
     75 	 * Update is a no-op
     76 	 */
     77 	if (mp->mnt_flag & MNT_UPDATE)
     78 		return (EOPNOTSUPP);
     79 
     80 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
     81 	if (error)
     82 		return (error);
     83 
     84 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
     85 				M_UFSMNT, M_WAITOK);	/* XXX */
     86 	rvp->v_type = VDIR;
     87 	rvp->v_flag |= VROOT;
     88 	fmp->f_root = rvp;
     89 	/* XXX -- don't mark as local to work around fts() problems */
     90 	/*mp->mnt_flag |= MNT_LOCAL;*/
     91 	mp->mnt_data = (qaddr_t) fmp;
     92 	vfs_getnewfsid(mp);
     93 
     94 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
     95 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
     96 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
     97 	bcopy("fdesc", mp->mnt_stat.f_mntfromname, sizeof("fdesc"));
     98 	return (0);
     99 }
    100 
    101 int
    102 fdesc_start(mp, flags, p)
    103 	struct mount *mp;
    104 	int flags;
    105 	struct proc *p;
    106 {
    107 	return (0);
    108 }
    109 
    110 int
    111 fdesc_unmount(mp, mntflags, p)
    112 	struct mount *mp;
    113 	int mntflags;
    114 	struct proc *p;
    115 {
    116 	int error;
    117 	int flags = 0;
    118 	struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
    119 
    120 	if (mntflags & MNT_FORCE)
    121 		flags |= FORCECLOSE;
    122 
    123 	/*
    124 	 * Clear out buffer cache.  I don't think we
    125 	 * ever get anything cached at this level at the
    126 	 * moment, but who knows...
    127 	 */
    128 	if (rootvp->v_usecount > 1)
    129 		return (EBUSY);
    130 	if (error = vflush(mp, rootvp, flags))
    131 		return (error);
    132 
    133 	/*
    134 	 * Release reference on underlying root vnode
    135 	 */
    136 	vrele(rootvp);
    137 	/*
    138 	 * And blow it away for future re-use
    139 	 */
    140 	vgone(rootvp);
    141 	/*
    142 	 * Finally, throw away the fdescmount structure
    143 	 */
    144 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    145 	mp->mnt_data = 0;
    146 
    147 	return (0);
    148 }
    149 
    150 int
    151 fdesc_root(mp, vpp)
    152 	struct mount *mp;
    153 	struct vnode **vpp;
    154 {
    155 	struct proc *p = curproc;	/* XXX */
    156 	struct vnode *vp;
    157 
    158 	/*
    159 	 * Return locked reference to root.
    160 	 */
    161 	vp = VFSTOFDESC(mp)->f_root;
    162 	VREF(vp);
    163 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
    164 	*vpp = vp;
    165 	return (0);
    166 }
    167 
    168 int
    169 fdesc_statfs(mp, sbp, p)
    170 	struct mount *mp;
    171 	struct statfs *sbp;
    172 	struct proc *p;
    173 {
    174 	struct filedesc *fdp;
    175 	int lim;
    176 	int i;
    177 	int last;
    178 	int freefd;
    179 
    180 	/*
    181 	 * Compute number of free file descriptors.
    182 	 * [ Strange results will ensue if the open file
    183 	 * limit is ever reduced below the current number
    184 	 * of open files... ]
    185 	 */
    186 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
    187 	fdp = p->p_fd;
    188 	last = min(fdp->fd_nfiles, lim);
    189 	freefd = 0;
    190 	for (i = fdp->fd_freefile; i < last; i++)
    191 		if (fdp->fd_ofiles[i] == NULL)
    192 			freefd++;
    193 
    194 	/*
    195 	 * Adjust for the fact that the fdesc array may not
    196 	 * have been fully allocated yet.
    197 	 */
    198 	if (fdp->fd_nfiles < lim)
    199 		freefd += (lim - fdp->fd_nfiles);
    200 
    201 	sbp->f_flags = 0;
    202 	sbp->f_bsize = DEV_BSIZE;
    203 	sbp->f_iosize = DEV_BSIZE;
    204 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    205 	sbp->f_bfree = 0;
    206 	sbp->f_bavail = 0;
    207 	sbp->f_files = lim + 1;		/* Allow for "." */
    208 	sbp->f_ffree = freefd;		/* See comments above */
    209 	if (sbp != &mp->mnt_stat) {
    210 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
    211 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
    212 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
    213 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    214 	}
    215 	return (0);
    216 }
    217 
    218 int
    219 fdesc_sync(mp, waitfor)
    220 	struct mount *mp;
    221 	int waitfor;
    222 {
    223 
    224 	return (0);
    225 }
    226 
    227 #define fdesc_fhtovp ((int (*) __P((struct mount *, struct fid *, \
    228 	    struct mbuf *, struct vnode **, int *, struct ucred **)))eopnotsupp)
    229 #define fdesc_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
    230 	    struct proc *)))eopnotsupp)
    231 #define fdesc_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
    232 	    size_t, struct proc *)))eopnotsupp)
    233 #define fdesc_vget ((int (*) __P((struct mount *, ino_t, struct vnode **))) \
    234 	    eopnotsupp)
    235 #define fdesc_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
    236 
    237 struct vfsops fdesc_vfsops = {
    238 	fdesc_mount,
    239 	fdesc_start,
    240 	fdesc_unmount,
    241 	fdesc_root,
    242 	fdesc_quotactl,
    243 	fdesc_statfs,
    244 	fdesc_sync,
    245 	fdesc_vget,
    246 	fdesc_fhtovp,
    247 	fdesc_vptofh,
    248 	fdesc_init,
    249 	fdesc_sysctl,
    250 };
    251