Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vfsops.c revision 1.42
      1 /*	$NetBSD: fdesc_vfsops.c,v 1.42 2003/06/29 18:43:31 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software donated to Berkeley by
      8  * Jan-Simon Pendry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)fdesc_vfsops.c	8.10 (Berkeley) 5/14/95
     39  *
     40  * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp #
     41  */
     42 
     43 /*
     44  * /dev/fd Filesystem
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.42 2003/06/29 18:43:31 thorpej Exp $");
     49 
     50 #if defined(_KERNEL_OPT)
     51 #include "opt_compat_netbsd.h"
     52 #endif
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/time.h>
     57 #include <sys/proc.h>
     58 #include <sys/resourcevar.h>
     59 #include <sys/filedesc.h>
     60 #include <sys/vnode.h>
     61 #include <sys/mount.h>
     62 #include <sys/namei.h>
     63 #include <sys/malloc.h>
     64 #include <miscfs/fdesc/fdesc.h>
     65 
     66 int	fdesc_mount __P((struct mount *, const char *, void *,
     67 			 struct nameidata *, struct lwp *));
     68 int	fdesc_start __P((struct mount *, int, struct lwp *));
     69 int	fdesc_unmount __P((struct mount *, int, struct lwp *));
     70 int	fdesc_quotactl __P((struct mount *, int, uid_t, caddr_t,
     71 			    struct lwp *));
     72 int	fdesc_statfs __P((struct mount *, struct statfs *, struct lwp *));
     73 int	fdesc_sync __P((struct mount *, int, struct ucred *, struct lwp *));
     74 int	fdesc_vget __P((struct mount *, ino_t, struct vnode **));
     75 int	fdesc_fhtovp __P((struct mount *, struct fid *, struct vnode **));
     76 int	fdesc_checkexp __P((struct mount *, struct mbuf *, int *,
     77 			    struct ucred **));
     78 int	fdesc_vptofh __P((struct vnode *, struct fid *));
     79 int	fdesc_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
     80 			  struct lwp *));
     81 
     82 /*
     83  * Mount the per-process file descriptors (/dev/fd)
     84  */
     85 int
     86 fdesc_mount(mp, path, data, ndp, l)
     87 	struct mount *mp;
     88 	const char *path;
     89 	void *data;
     90 	struct nameidata *ndp;
     91 	struct lwp *l;
     92 {
     93 	int error = 0;
     94 	struct fdescmount *fmp;
     95 	struct vnode *rvp;
     96 
     97 	if (mp->mnt_flag & MNT_GETARGS)
     98 		return 0;
     99 	/*
    100 	 * Update is a no-op
    101 	 */
    102 	if (mp->mnt_flag & MNT_UPDATE)
    103 		return (EOPNOTSUPP);
    104 
    105 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
    106 	if (error)
    107 		return (error);
    108 
    109 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
    110 				M_UFSMNT, M_WAITOK);	/* XXX */
    111 	rvp->v_type = VDIR;
    112 	rvp->v_flag |= VROOT;
    113 	fmp->f_root = rvp;
    114 	mp->mnt_flag |= MNT_LOCAL;
    115 	mp->mnt_data = fmp;
    116 	vfs_getnewfsid(mp);
    117 
    118 	error = set_statfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
    119 	    mp, l);
    120 	VOP_UNLOCK(rvp, 0);
    121 	return error;
    122 }
    123 
    124 int
    125 fdesc_start(mp, flags, l)
    126 	struct mount *mp;
    127 	int flags;
    128 	struct lwp *l;
    129 {
    130 	return (0);
    131 }
    132 
    133 int
    134 fdesc_unmount(mp, mntflags, l)
    135 	struct mount *mp;
    136 	int mntflags;
    137 	struct lwp *l;
    138 {
    139 	int error;
    140 	int flags = 0;
    141 	struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
    142 
    143 	if (mntflags & MNT_FORCE)
    144 		flags |= FORCECLOSE;
    145 
    146 	/*
    147 	 * Clear out buffer cache.  I don't think we
    148 	 * ever get anything cached at this level at the
    149 	 * moment, but who knows...
    150 	 */
    151 	if (rootvp->v_usecount > 1)
    152 		return (EBUSY);
    153 	if ((error = vflush(mp, rootvp, flags)) != 0)
    154 		return (error);
    155 
    156 	/*
    157 	 * Release reference on underlying root vnode
    158 	 */
    159 	vrele(rootvp);
    160 	/*
    161 	 * And blow it away for future re-use
    162 	 */
    163 	vgone(rootvp);
    164 	/*
    165 	 * Finally, throw away the fdescmount structure
    166 	 */
    167 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    168 	mp->mnt_data = 0;
    169 
    170 	return (0);
    171 }
    172 
    173 int
    174 fdesc_root(mp, vpp)
    175 	struct mount *mp;
    176 	struct vnode **vpp;
    177 {
    178 	struct vnode *vp;
    179 
    180 	/*
    181 	 * Return locked reference to root.
    182 	 */
    183 	vp = VFSTOFDESC(mp)->f_root;
    184 	VREF(vp);
    185 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    186 	*vpp = vp;
    187 	return (0);
    188 }
    189 
    190 int
    191 fdesc_quotactl(mp, cmd, uid, arg, l)
    192 	struct mount *mp;
    193 	int cmd;
    194 	uid_t uid;
    195 	caddr_t arg;
    196 	struct lwp *l;
    197 {
    198 
    199 	return (EOPNOTSUPP);
    200 }
    201 
    202 int
    203 fdesc_statfs(mp, sbp, l)
    204 	struct mount *mp;
    205 	struct statfs *sbp;
    206 	struct lwp *l;
    207 {
    208 	struct filedesc *fdp;
    209 	struct proc *p;
    210 	int lim;
    211 	int i;
    212 	int last;
    213 	int freefd;
    214 
    215 	/*
    216 	 * Compute number of free file descriptors.
    217 	 * [ Strange results will ensue if the open file
    218 	 * limit is ever reduced below the current number
    219 	 * of open files... ]
    220 	 */
    221 	p = l->l_proc;
    222 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
    223 	fdp = p->p_fd;
    224 	last = min(fdp->fd_nfiles, lim);
    225 	freefd = 0;
    226 	for (i = fdp->fd_freefile; i < last; i++)
    227 		if (fdp->fd_ofiles[i] == NULL)
    228 			freefd++;
    229 
    230 	/*
    231 	 * Adjust for the fact that the fdesc array may not
    232 	 * have been fully allocated yet.
    233 	 */
    234 	if (fdp->fd_nfiles < lim)
    235 		freefd += (lim - fdp->fd_nfiles);
    236 
    237 	sbp->f_bsize = DEV_BSIZE;
    238 	sbp->f_iosize = DEV_BSIZE;
    239 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    240 	sbp->f_bfree = 0;
    241 	sbp->f_bavail = 0;
    242 	sbp->f_files = lim + 1;		/* Allow for "." */
    243 	sbp->f_ffree = freefd;		/* See comments above */
    244 #ifdef COMPAT_09
    245 	sbp->f_type = 6;
    246 #else
    247 	sbp->f_type = 0;
    248 #endif
    249 	copy_statfs_info(sbp, mp);
    250 	return (0);
    251 }
    252 
    253 /*ARGSUSED*/
    254 int
    255 fdesc_sync(mp, waitfor, uc, l)
    256 	struct mount *mp;
    257 	int waitfor;
    258 	struct ucred *uc;
    259 	struct lwp *l;
    260 {
    261 
    262 	return (0);
    263 }
    264 
    265 /*
    266  * Fdesc flat namespace lookup.
    267  * Currently unsupported.
    268  */
    269 int
    270 fdesc_vget(mp, ino, vpp)
    271 	struct mount *mp;
    272 	ino_t ino;
    273 	struct vnode **vpp;
    274 {
    275 
    276 	return (EOPNOTSUPP);
    277 }
    278 
    279 
    280 /*ARGSUSED*/
    281 int
    282 fdesc_fhtovp(mp, fhp, vpp)
    283 	struct mount *mp;
    284 	struct fid *fhp;
    285 	struct vnode **vpp;
    286 {
    287 
    288 	return (EOPNOTSUPP);
    289 }
    290 
    291 /*ARGSUSED*/
    292 int
    293 fdesc_checkexp(mp, nam, exflagsp, credanonp)
    294 	struct mount *mp;
    295 	struct mbuf *nam;
    296 	int *exflagsp;
    297 	struct ucred **credanonp;
    298 {
    299 
    300 	return (EOPNOTSUPP);
    301 }
    302 
    303 /*ARGSUSED*/
    304 int
    305 fdesc_vptofh(vp, fhp)
    306 	struct vnode *vp;
    307 	struct fid *fhp;
    308 {
    309 	return (EOPNOTSUPP);
    310 }
    311 
    312 int
    313 fdesc_sysctl(name, namelen, oldp, oldlenp, newp, newlen, l)
    314 	int *name;
    315 	u_int namelen;
    316 	void *oldp;
    317 	size_t *oldlenp;
    318 	void *newp;
    319 	size_t newlen;
    320 	struct lwp *l;
    321 {
    322 	return (EOPNOTSUPP);
    323 }
    324 
    325 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
    326 
    327 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
    328 	&fdesc_vnodeop_opv_desc,
    329 	NULL,
    330 };
    331 
    332 struct vfsops fdesc_vfsops = {
    333 	MOUNT_FDESC,
    334 	fdesc_mount,
    335 	fdesc_start,
    336 	fdesc_unmount,
    337 	fdesc_root,
    338 	fdesc_quotactl,
    339 	fdesc_statfs,
    340 	fdesc_sync,
    341 	fdesc_vget,
    342 	fdesc_fhtovp,
    343 	fdesc_vptofh,
    344 	fdesc_init,
    345 	NULL,
    346 	fdesc_done,
    347 	fdesc_sysctl,
    348 	NULL,				/* vfs_mountroot */
    349 	fdesc_checkexp,
    350 	fdesc_vnodeopv_descs,
    351 };
    352