Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vfsops.c revision 1.88
      1 /*	$NetBSD: fdesc_vfsops.c,v 1.88 2014/03/23 15:21:16 hannken 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.88 2014/03/23 15:21:16 hannken 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 vnode *rvp;
     83 
     84 	if (mp->mnt_flag & MNT_GETARGS) {
     85 		*data_len = 0;
     86 		return 0;
     87 	}
     88 	/*
     89 	 * Update is a no-op
     90 	 */
     91 	if (mp->mnt_flag & MNT_UPDATE)
     92 		return (EOPNOTSUPP);
     93 
     94 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
     95 	if (error)
     96 		return (error);
     97 
     98 	rvp->v_type = VDIR;
     99 	rvp->v_vflag |= VV_ROOT;
    100 	mp->mnt_stat.f_namemax = FDESC_MAXNAMLEN;
    101 	mp->mnt_flag |= MNT_LOCAL;
    102 	mp->mnt_data = rvp;
    103 	vfs_getnewfsid(mp);
    104 
    105 	error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
    106 	    mp->mnt_op->vfs_name, mp, l);
    107 	VOP_UNLOCK(rvp);
    108 	return error;
    109 }
    110 
    111 int
    112 fdesc_start(struct mount *mp, int flags)
    113 {
    114 	return (0);
    115 }
    116 
    117 int
    118 fdesc_unmount(struct mount *mp, int mntflags)
    119 {
    120 	int error;
    121 	int flags = 0;
    122 	struct vnode *rtvp = mp->mnt_data;
    123 
    124 	if (mntflags & MNT_FORCE)
    125 		flags |= FORCECLOSE;
    126 
    127 	if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
    128 		return (EBUSY);
    129 	if ((error = vflush(mp, rtvp, flags)) != 0)
    130 		return (error);
    131 
    132 	/*
    133 	 * Blow it away for future re-use
    134 	 */
    135 	vgone(rtvp);
    136 	mp->mnt_data = NULL;
    137 
    138 	return (0);
    139 }
    140 
    141 int
    142 fdesc_root(struct mount *mp, struct vnode **vpp)
    143 {
    144 	struct vnode *vp;
    145 
    146 	/*
    147 	 * Return locked reference to root.
    148 	 */
    149 	vp = mp->mnt_data;
    150 	vref(vp);
    151 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    152 	*vpp = vp;
    153 	return (0);
    154 }
    155 
    156 /*ARGSUSED*/
    157 int
    158 fdesc_sync(struct mount *mp, int waitfor,
    159     kauth_cred_t uc)
    160 {
    161 
    162 	return (0);
    163 }
    164 
    165 /*
    166  * Fdesc flat namespace lookup.
    167  * Currently unsupported.
    168  */
    169 int
    170 fdesc_vget(struct mount *mp, ino_t ino,
    171     struct vnode **vpp)
    172 {
    173 
    174 	return (EOPNOTSUPP);
    175 }
    176 
    177 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
    178 
    179 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
    180 	&fdesc_vnodeop_opv_desc,
    181 	NULL,
    182 };
    183 
    184 struct vfsops fdesc_vfsops = {
    185 	.vfs_name = MOUNT_FDESC,
    186 	.vfs_min_mount_data = 0,
    187 	.vfs_mount = fdesc_mount,
    188 	.vfs_start = fdesc_start,
    189 	.vfs_unmount = fdesc_unmount,
    190 	.vfs_root = fdesc_root,
    191 	.vfs_quotactl = (void *)eopnotsupp,
    192 	.vfs_statvfs = genfs_statvfs,
    193 	.vfs_sync = fdesc_sync,
    194 	.vfs_vget = fdesc_vget,
    195 	.vfs_fhtovp = (void *)eopnotsupp,
    196 	.vfs_vptofh = (void *)eopnotsupp,
    197 	.vfs_init = fdesc_init,
    198 	.vfs_done = fdesc_done,
    199 	.vfs_snapshot = (void *)eopnotsupp,
    200 	.vfs_extattrctl = vfs_stdextattrctl,
    201 	.vfs_suspendctl = (void *)eopnotsupp,
    202 	.vfs_renamelock_enter = genfs_renamelock_enter,
    203 	.vfs_renamelock_exit = genfs_renamelock_exit,
    204 	.vfs_fsync = (void *)eopnotsupp,
    205 	.vfs_opv_descs = fdesc_vnodeopv_descs
    206 };
    207 
    208 static int
    209 fdesc_modcmd(modcmd_t cmd, void *arg)
    210 {
    211 	int error;
    212 
    213 	switch (cmd) {
    214 	case MODULE_CMD_INIT:
    215 		error = vfs_attach(&fdesc_vfsops);
    216 		if (error != 0)
    217 			break;
    218 		sysctl_createv(&fdesc_sysctl_log, 0, NULL, NULL,
    219 			       CTLFLAG_PERMANENT,
    220 			       CTLTYPE_NODE, "fdesc",
    221 			       SYSCTL_DESCR("File-descriptor file system"),
    222 			       NULL, 0, NULL, 0,
    223 			       CTL_VFS, 7, CTL_EOL);
    224 		/*
    225 		 * XXX the "7" above could be dynamic, thereby eliminating one
    226 		 * more instance of the "number to vfs" mapping problem, but
    227 		 * "7" is the order as taken from sys/mount.h
    228 		 */
    229 		break;
    230 	case MODULE_CMD_FINI:
    231 		error = vfs_detach(&fdesc_vfsops);
    232 		if (error != 0)
    233 			break;
    234 		sysctl_teardown(&fdesc_sysctl_log);
    235 		break;
    236 	default:
    237 		error = ENOTTY;
    238 		break;
    239 	}
    240 
    241 	return (error);
    242 }
    243