Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vfsops.c revision 1.67
      1 /*	$NetBSD: fdesc_vfsops.c,v 1.67 2007/07/17 11:19:34 pooka 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.67 2007/07/17 11:19:34 pooka 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 
     64 #include <miscfs/fdesc/fdesc.h>
     65 
     66 int	fdesc_mount(struct mount *, const char *, void *, size_t *,
     67 			 struct nameidata *, struct lwp *);
     68 int	fdesc_start(struct mount *, int, struct lwp *);
     69 int	fdesc_unmount(struct mount *, int, struct lwp *);
     70 int	fdesc_quotactl(struct mount *, int, uid_t, void *,
     71 			    struct lwp *);
     72 int	fdesc_statvfs(struct mount *, struct statvfs *, struct lwp *);
     73 int	fdesc_sync(struct mount *, int, kauth_cred_t, struct lwp *);
     74 int	fdesc_vget(struct mount *, ino_t, struct vnode **);
     75 
     76 /*
     77  * Mount the per-process file descriptors (/dev/fd)
     78  */
     79 int
     80 fdesc_mount(struct mount *mp, const char *path, void *data, size_t *data_len,
     81     struct nameidata *ndp, struct lwp *l)
     82 {
     83 	int error = 0;
     84 	struct fdescmount *fmp;
     85 	struct vnode *rvp;
     86 
     87 	if (mp->mnt_flag & MNT_GETARGS) {
     88 		*data_len = 0;
     89 		return 0;
     90 	}
     91 	/*
     92 	 * Update is a no-op
     93 	 */
     94 	if (mp->mnt_flag & MNT_UPDATE)
     95 		return (EOPNOTSUPP);
     96 
     97 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
     98 	if (error)
     99 		return (error);
    100 
    101 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
    102 				M_UFSMNT, M_WAITOK);	/* XXX */
    103 	rvp->v_type = VDIR;
    104 	rvp->v_flag |= VROOT;
    105 	fmp->f_root = rvp;
    106 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    107 	mp->mnt_flag |= MNT_LOCAL;
    108 	mp->mnt_data = fmp;
    109 	vfs_getnewfsid(mp);
    110 
    111 	error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
    112 	    mp->mnt_op->vfs_name, mp, l);
    113 	VOP_UNLOCK(rvp, 0);
    114 	return error;
    115 }
    116 
    117 int
    118 fdesc_start(struct mount *mp, int flags,
    119     struct lwp *l)
    120 {
    121 	return (0);
    122 }
    123 
    124 int
    125 fdesc_unmount(struct mount *mp, int mntflags, struct lwp *l)
    126 {
    127 	int error;
    128 	int flags = 0;
    129 	struct vnode *rtvp = VFSTOFDESC(mp)->f_root;
    130 
    131 	if (mntflags & MNT_FORCE)
    132 		flags |= FORCECLOSE;
    133 
    134 	if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
    135 		return (EBUSY);
    136 	if ((error = vflush(mp, rtvp, flags)) != 0)
    137 		return (error);
    138 
    139 	/*
    140 	 * Release reference on underlying root vnode
    141 	 */
    142 	vrele(rtvp);
    143 	/*
    144 	 * And blow it away for future re-use
    145 	 */
    146 	vgone(rtvp);
    147 	/*
    148 	 * Finally, throw away the fdescmount structure
    149 	 */
    150 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    151 	mp->mnt_data = 0;
    152 
    153 	return (0);
    154 }
    155 
    156 int
    157 fdesc_root(mp, vpp)
    158 	struct mount *mp;
    159 	struct vnode **vpp;
    160 {
    161 	struct vnode *vp;
    162 
    163 	/*
    164 	 * Return locked reference to root.
    165 	 */
    166 	vp = VFSTOFDESC(mp)->f_root;
    167 	VREF(vp);
    168 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    169 	*vpp = vp;
    170 	return (0);
    171 }
    172 
    173 int
    174 fdesc_quotactl(struct mount *mp, int cmd, uid_t uid,
    175     void *arg, struct lwp *l)
    176 {
    177 
    178 	return (EOPNOTSUPP);
    179 }
    180 
    181 int
    182 fdesc_statvfs(mp, sbp, l)
    183 	struct mount *mp;
    184 	struct statvfs *sbp;
    185 	struct lwp *l;
    186 {
    187 	struct filedesc *fdp;
    188 	struct proc *p;
    189 	int lim;
    190 	int i;
    191 	int last;
    192 	int freefd;
    193 
    194 	/*
    195 	 * Compute number of free file descriptors.
    196 	 * [ Strange results will ensue if the open file
    197 	 * limit is ever reduced below the current number
    198 	 * of open files... ]
    199 	 */
    200 	p = l->l_proc;
    201 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
    202 	fdp = p->p_fd;
    203 	last = min(fdp->fd_nfiles, lim);
    204 	freefd = 0;
    205 	for (i = fdp->fd_freefile; i < last; i++)
    206 		if (fdp->fd_ofiles[i] == NULL)
    207 			freefd++;
    208 
    209 	/*
    210 	 * Adjust for the fact that the fdesc array may not
    211 	 * have been fully allocated yet.
    212 	 */
    213 	if (fdp->fd_nfiles < lim)
    214 		freefd += (lim - fdp->fd_nfiles);
    215 
    216 	sbp->f_bsize = DEV_BSIZE;
    217 	sbp->f_frsize = DEV_BSIZE;
    218 	sbp->f_iosize = DEV_BSIZE;
    219 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    220 	sbp->f_bfree = 0;
    221 	sbp->f_bavail = 0;
    222 	sbp->f_bresvd = 0;
    223 	sbp->f_files = lim + 1;		/* Allow for "." */
    224 	sbp->f_ffree = freefd;		/* See comments above */
    225 	sbp->f_favail = freefd;		/* See comments above */
    226 	sbp->f_fresvd = 0;
    227 	copy_statvfs_info(sbp, mp);
    228 	return (0);
    229 }
    230 
    231 /*ARGSUSED*/
    232 int
    233 fdesc_sync(struct mount *mp, int waitfor,
    234     kauth_cred_t uc, struct lwp *l)
    235 {
    236 
    237 	return (0);
    238 }
    239 
    240 /*
    241  * Fdesc flat namespace lookup.
    242  * Currently unsupported.
    243  */
    244 int
    245 fdesc_vget(struct mount *mp, ino_t ino,
    246     struct vnode **vpp)
    247 {
    248 
    249 	return (EOPNOTSUPP);
    250 }
    251 
    252 
    253 SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup")
    254 {
    255 
    256 	sysctl_createv(clog, 0, NULL, NULL,
    257 		       CTLFLAG_PERMANENT,
    258 		       CTLTYPE_NODE, "vfs", NULL,
    259 		       NULL, 0, NULL, 0,
    260 		       CTL_VFS, CTL_EOL);
    261 	sysctl_createv(clog, 0, NULL, NULL,
    262 		       CTLFLAG_PERMANENT,
    263 		       CTLTYPE_NODE, "fdesc",
    264 		       SYSCTL_DESCR("File-descriptor file system"),
    265 		       NULL, 0, NULL, 0,
    266 		       CTL_VFS, 7, CTL_EOL);
    267 	/*
    268 	 * XXX the "7" above could be dynamic, thereby eliminating one
    269 	 * more instance of the "number to vfs" mapping problem, but
    270 	 * "7" is the order as taken from sys/mount.h
    271 	 */
    272 }
    273 
    274 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
    275 
    276 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
    277 	&fdesc_vnodeop_opv_desc,
    278 	NULL,
    279 };
    280 
    281 struct vfsops fdesc_vfsops = {
    282 	MOUNT_FDESC,
    283 	0,
    284 	fdesc_mount,
    285 	fdesc_start,
    286 	fdesc_unmount,
    287 	fdesc_root,
    288 	fdesc_quotactl,
    289 	fdesc_statvfs,
    290 	fdesc_sync,
    291 	fdesc_vget,
    292 	(void *)eopnotsupp,		/* vfs_fhtovp */
    293 	(void *)eopnotsupp,		/* vfs_vptofh */
    294 	fdesc_init,
    295 	NULL,
    296 	fdesc_done,
    297 	NULL,				/* vfs_mountroot */
    298 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    299 	vfs_stdextattrctl,
    300 	vfs_stdsuspendctl,
    301 	fdesc_vnodeopv_descs,
    302 	0,
    303 	{ NULL, NULL},
    304 };
    305 VFS_ATTACH(fdesc_vfsops);
    306