Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vfsops.c revision 1.76
      1 /*	$NetBSD: fdesc_vfsops.c,v 1.76 2008/05/13 08:31:13 simonb 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)fdesc_vfsops.c	8.10 (Berkeley) 5/14/95
     35  *
     36  * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp #
     37  */
     38 
     39 /*
     40  * /dev/fd Filesystem
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.76 2008/05/13 08:31:13 simonb Exp $");
     45 
     46 #if defined(_KERNEL_OPT)
     47 #include "opt_compat_netbsd.h"
     48 #endif
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/sysctl.h>
     53 #include <sys/time.h>
     54 #include <sys/proc.h>
     55 #include <sys/resourcevar.h>
     56 #include <sys/filedesc.h>
     57 #include <sys/vnode.h>
     58 #include <sys/mount.h>
     59 #include <sys/dirent.h>
     60 #include <sys/namei.h>
     61 #include <sys/malloc.h>
     62 #include <sys/kauth.h>
     63 #include <sys/module.h>
     64 
     65 #include <miscfs/genfs/genfs.h>
     66 #include <miscfs/fdesc/fdesc.h>
     67 
     68 MODULE(MODULE_CLASS_VFS, fdesc, NULL);
     69 
     70 VFS_PROTOS(fdesc);
     71 
     72 /*
     73  * Mount the per-process file descriptors (/dev/fd)
     74  */
     75 int
     76 fdesc_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     77 {
     78 	struct lwp *l = curlwp;
     79 	int error = 0;
     80 	struct fdescmount *fmp;
     81 	struct vnode *rvp;
     82 
     83 	if (mp->mnt_flag & MNT_GETARGS) {
     84 		*data_len = 0;
     85 		return 0;
     86 	}
     87 	/*
     88 	 * Update is a no-op
     89 	 */
     90 	if (mp->mnt_flag & MNT_UPDATE)
     91 		return (EOPNOTSUPP);
     92 
     93 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
     94 	if (error)
     95 		return (error);
     96 
     97 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
     98 				M_UFSMNT, M_WAITOK);	/* XXX */
     99 	rvp->v_type = VDIR;
    100 	rvp->v_vflag |= VV_ROOT;
    101 	fmp->f_root = rvp;
    102 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    103 	mp->mnt_flag |= MNT_LOCAL;
    104 	mp->mnt_data = fmp;
    105 	vfs_getnewfsid(mp);
    106 
    107 	error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
    108 	    mp->mnt_op->vfs_name, mp, l);
    109 	VOP_UNLOCK(rvp, 0);
    110 	return error;
    111 }
    112 
    113 int
    114 fdesc_start(struct mount *mp, int flags)
    115 {
    116 	return (0);
    117 }
    118 
    119 int
    120 fdesc_unmount(struct mount *mp, int mntflags)
    121 {
    122 	int error;
    123 	int flags = 0;
    124 	struct vnode *rtvp = VFSTOFDESC(mp)->f_root;
    125 
    126 	if (mntflags & MNT_FORCE)
    127 		flags |= FORCECLOSE;
    128 
    129 	if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
    130 		return (EBUSY);
    131 	if ((error = vflush(mp, rtvp, flags)) != 0)
    132 		return (error);
    133 
    134 	/*
    135 	 * Blow it away for future re-use
    136 	 */
    137 	vgone(rtvp);
    138 	/*
    139 	 * Finally, throw away the fdescmount structure
    140 	 */
    141 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    142 	mp->mnt_data = NULL;
    143 
    144 	return (0);
    145 }
    146 
    147 int
    148 fdesc_root(mp, vpp)
    149 	struct mount *mp;
    150 	struct vnode **vpp;
    151 {
    152 	struct vnode *vp;
    153 
    154 	/*
    155 	 * Return locked reference to root.
    156 	 */
    157 	vp = VFSTOFDESC(mp)->f_root;
    158 	VREF(vp);
    159 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    160 	*vpp = vp;
    161 	return (0);
    162 }
    163 
    164 int
    165 fdesc_statvfs(mp, sbp)
    166 	struct mount *mp;
    167 	struct statvfs *sbp;
    168 {
    169 	struct lwp *l = curlwp;
    170 	struct filedesc *fdp;
    171 	struct proc *p;
    172 	int lim;
    173 	int i;
    174 	int last;
    175 	int freefd;
    176 
    177 	/*
    178 	 * Compute number of free file descriptors.
    179 	 * [ Strange results will ensue if the open file
    180 	 * limit is ever reduced below the current number
    181 	 * of open files... ]
    182 	 */
    183 	p = l->l_proc;
    184 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
    185 	fdp = p->p_fd;
    186 	last = min(fdp->fd_nfiles, lim);
    187 	freefd = 0;
    188 	for (i = fdp->fd_freefile; i < last; i++)
    189 		if (fdp->fd_ofiles[i] == NULL)
    190 			freefd++;
    191 
    192 	/*
    193 	 * Adjust for the fact that the fdesc array may not
    194 	 * have been fully allocated yet.
    195 	 */
    196 	if (fdp->fd_nfiles < lim)
    197 		freefd += (lim - fdp->fd_nfiles);
    198 
    199 	sbp->f_bsize = DEV_BSIZE;
    200 	sbp->f_frsize = DEV_BSIZE;
    201 	sbp->f_iosize = DEV_BSIZE;
    202 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    203 	sbp->f_bfree = 0;
    204 	sbp->f_bavail = 0;
    205 	sbp->f_bresvd = 0;
    206 	sbp->f_files = lim + 1;		/* Allow for "." */
    207 	sbp->f_ffree = freefd;		/* See comments above */
    208 	sbp->f_favail = freefd;		/* See comments above */
    209 	sbp->f_fresvd = 0;
    210 	copy_statvfs_info(sbp, mp);
    211 	return (0);
    212 }
    213 
    214 /*ARGSUSED*/
    215 int
    216 fdesc_sync(struct mount *mp, int waitfor,
    217     kauth_cred_t uc)
    218 {
    219 
    220 	return (0);
    221 }
    222 
    223 /*
    224  * Fdesc flat namespace lookup.
    225  * Currently unsupported.
    226  */
    227 int
    228 fdesc_vget(struct mount *mp, ino_t ino,
    229     struct vnode **vpp)
    230 {
    231 
    232 	return (EOPNOTSUPP);
    233 }
    234 
    235 
    236 SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup")
    237 {
    238 
    239 	sysctl_createv(clog, 0, NULL, NULL,
    240 		       CTLFLAG_PERMANENT,
    241 		       CTLTYPE_NODE, "vfs", NULL,
    242 		       NULL, 0, NULL, 0,
    243 		       CTL_VFS, CTL_EOL);
    244 	sysctl_createv(clog, 0, NULL, NULL,
    245 		       CTLFLAG_PERMANENT,
    246 		       CTLTYPE_NODE, "fdesc",
    247 		       SYSCTL_DESCR("File-descriptor file system"),
    248 		       NULL, 0, NULL, 0,
    249 		       CTL_VFS, 7, CTL_EOL);
    250 	/*
    251 	 * XXX the "7" above could be dynamic, thereby eliminating one
    252 	 * more instance of the "number to vfs" mapping problem, but
    253 	 * "7" is the order as taken from sys/mount.h
    254 	 */
    255 }
    256 
    257 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
    258 
    259 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
    260 	&fdesc_vnodeop_opv_desc,
    261 	NULL,
    262 };
    263 
    264 struct vfsops fdesc_vfsops = {
    265 	MOUNT_FDESC,
    266 	0,
    267 	fdesc_mount,
    268 	fdesc_start,
    269 	fdesc_unmount,
    270 	fdesc_root,
    271 	(void *)eopnotsupp,		/* vfs_quotactl */
    272 	fdesc_statvfs,
    273 	fdesc_sync,
    274 	fdesc_vget,
    275 	(void *)eopnotsupp,		/* vfs_fhtovp */
    276 	(void *)eopnotsupp,		/* vfs_vptofh */
    277 	fdesc_init,
    278 	NULL,
    279 	fdesc_done,
    280 	NULL,				/* vfs_mountroot */
    281 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    282 	vfs_stdextattrctl,
    283 	(void *)eopnotsupp,		/* vfs_suspendctl */
    284 	genfs_renamelock_enter,
    285 	genfs_renamelock_exit,
    286 	(void *)eopnotsupp,
    287 	fdesc_vnodeopv_descs,
    288 	0,
    289 	{ NULL, NULL},
    290 };
    291 
    292 static int
    293 fdesc_modcmd(modcmd_t cmd, void *arg)
    294 {
    295 
    296 	switch (cmd) {
    297 	case MODULE_CMD_INIT:
    298 		return vfs_attach(&fdesc_vfsops);
    299 	case MODULE_CMD_FINI:
    300 		return vfs_detach(&fdesc_vfsops);
    301 	default:
    302 		return ENOTTY;
    303 	}
    304 }
    305