Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vfsops.c revision 1.77
      1 /*	$NetBSD: fdesc_vfsops.c,v 1.77 2008/06/28 01:34:06 rumble 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.77 2008/06/28 01:34:06 rumble 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 static struct sysctllog *fdesc_sysctl_log;
     73 
     74 /*
     75  * Mount the per-process file descriptors (/dev/fd)
     76  */
     77 int
     78 fdesc_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     79 {
     80 	struct lwp *l = curlwp;
     81 	int error = 0;
     82 	struct fdescmount *fmp;
     83 	struct vnode *rvp;
     84 
     85 	if (mp->mnt_flag & MNT_GETARGS) {
     86 		*data_len = 0;
     87 		return 0;
     88 	}
     89 	/*
     90 	 * Update is a no-op
     91 	 */
     92 	if (mp->mnt_flag & MNT_UPDATE)
     93 		return (EOPNOTSUPP);
     94 
     95 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
     96 	if (error)
     97 		return (error);
     98 
     99 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
    100 				M_UFSMNT, M_WAITOK);	/* XXX */
    101 	rvp->v_type = VDIR;
    102 	rvp->v_vflag |= VV_ROOT;
    103 	fmp->f_root = rvp;
    104 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    105 	mp->mnt_flag |= MNT_LOCAL;
    106 	mp->mnt_data = fmp;
    107 	vfs_getnewfsid(mp);
    108 
    109 	error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
    110 	    mp->mnt_op->vfs_name, mp, l);
    111 	VOP_UNLOCK(rvp, 0);
    112 	return error;
    113 }
    114 
    115 int
    116 fdesc_start(struct mount *mp, int flags)
    117 {
    118 	return (0);
    119 }
    120 
    121 int
    122 fdesc_unmount(struct mount *mp, int mntflags)
    123 {
    124 	int error;
    125 	int flags = 0;
    126 	struct vnode *rtvp = VFSTOFDESC(mp)->f_root;
    127 
    128 	if (mntflags & MNT_FORCE)
    129 		flags |= FORCECLOSE;
    130 
    131 	if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
    132 		return (EBUSY);
    133 	if ((error = vflush(mp, rtvp, flags)) != 0)
    134 		return (error);
    135 
    136 	/*
    137 	 * Blow it away for future re-use
    138 	 */
    139 	vgone(rtvp);
    140 	/*
    141 	 * Finally, throw away the fdescmount structure
    142 	 */
    143 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    144 	mp->mnt_data = NULL;
    145 
    146 	return (0);
    147 }
    148 
    149 int
    150 fdesc_root(mp, vpp)
    151 	struct mount *mp;
    152 	struct vnode **vpp;
    153 {
    154 	struct vnode *vp;
    155 
    156 	/*
    157 	 * Return locked reference to root.
    158 	 */
    159 	vp = VFSTOFDESC(mp)->f_root;
    160 	VREF(vp);
    161 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    162 	*vpp = vp;
    163 	return (0);
    164 }
    165 
    166 int
    167 fdesc_statvfs(mp, sbp)
    168 	struct mount *mp;
    169 	struct statvfs *sbp;
    170 {
    171 	struct lwp *l = curlwp;
    172 	struct filedesc *fdp;
    173 	struct proc *p;
    174 	int lim;
    175 	int i;
    176 	int last;
    177 	int freefd;
    178 
    179 	/*
    180 	 * Compute number of free file descriptors.
    181 	 * [ Strange results will ensue if the open file
    182 	 * limit is ever reduced below the current number
    183 	 * of open files... ]
    184 	 */
    185 	p = l->l_proc;
    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_bsize = DEV_BSIZE;
    202 	sbp->f_frsize = 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_bresvd = 0;
    208 	sbp->f_files = lim + 1;		/* Allow for "." */
    209 	sbp->f_ffree = freefd;		/* See comments above */
    210 	sbp->f_favail = freefd;		/* See comments above */
    211 	sbp->f_fresvd = 0;
    212 	copy_statvfs_info(sbp, mp);
    213 	return (0);
    214 }
    215 
    216 /*ARGSUSED*/
    217 int
    218 fdesc_sync(struct mount *mp, int waitfor,
    219     kauth_cred_t uc)
    220 {
    221 
    222 	return (0);
    223 }
    224 
    225 /*
    226  * Fdesc flat namespace lookup.
    227  * Currently unsupported.
    228  */
    229 int
    230 fdesc_vget(struct mount *mp, ino_t ino,
    231     struct vnode **vpp)
    232 {
    233 
    234 	return (EOPNOTSUPP);
    235 }
    236 
    237 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
    238 
    239 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
    240 	&fdesc_vnodeop_opv_desc,
    241 	NULL,
    242 };
    243 
    244 struct vfsops fdesc_vfsops = {
    245 	MOUNT_FDESC,
    246 	0,
    247 	fdesc_mount,
    248 	fdesc_start,
    249 	fdesc_unmount,
    250 	fdesc_root,
    251 	(void *)eopnotsupp,		/* vfs_quotactl */
    252 	fdesc_statvfs,
    253 	fdesc_sync,
    254 	fdesc_vget,
    255 	(void *)eopnotsupp,		/* vfs_fhtovp */
    256 	(void *)eopnotsupp,		/* vfs_vptofh */
    257 	fdesc_init,
    258 	NULL,
    259 	fdesc_done,
    260 	NULL,				/* vfs_mountroot */
    261 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    262 	vfs_stdextattrctl,
    263 	(void *)eopnotsupp,		/* vfs_suspendctl */
    264 	genfs_renamelock_enter,
    265 	genfs_renamelock_exit,
    266 	(void *)eopnotsupp,
    267 	fdesc_vnodeopv_descs,
    268 	0,
    269 	{ NULL, NULL},
    270 };
    271 
    272 static int
    273 fdesc_modcmd(modcmd_t cmd, void *arg)
    274 {
    275 	int error;
    276 
    277 	switch (cmd) {
    278 	case MODULE_CMD_INIT:
    279 		error = vfs_attach(&fdesc_vfsops);
    280 		if (error != 0)
    281 			break;
    282 		sysctl_createv(&fdesc_sysctl_log, 0, NULL, NULL,
    283 			       CTLFLAG_PERMANENT,
    284 			       CTLTYPE_NODE, "vfs", NULL,
    285 			       NULL, 0, NULL, 0,
    286 			       CTL_VFS, CTL_EOL);
    287 		sysctl_createv(&fdesc_sysctl_log, 0, NULL, NULL,
    288 			       CTLFLAG_PERMANENT,
    289 			       CTLTYPE_NODE, "fdesc",
    290 			       SYSCTL_DESCR("File-descriptor file system"),
    291 			       NULL, 0, NULL, 0,
    292 			       CTL_VFS, 7, CTL_EOL);
    293 		/*
    294 		 * XXX the "7" above could be dynamic, thereby eliminating one
    295 		 * more instance of the "number to vfs" mapping problem, but
    296 		 * "7" is the order as taken from sys/mount.h
    297 		 */
    298 		break;
    299 	case MODULE_CMD_FINI:
    300 		error = vfs_detach(&fdesc_vfsops);
    301 		if (error != 0)
    302 			break;
    303 		sysctl_teardown(&fdesc_sysctl_log);
    304 		break;
    305 	default:
    306 		error = ENOTTY;
    307 		break;
    308 	}
    309 
    310 	return (error);
    311 }
    312