Home | History | Annotate | Line # | Download | only in fdesc
fdesc_vfsops.c revision 1.55.2.2
      1  1.55.2.2      yamt /*	$NetBSD: fdesc_vfsops.c,v 1.55.2.2 2006/12/30 20:50:17 yamt Exp $	*/
      2      1.13       cgd 
      3       1.1       cgd /*
      4      1.24      fvdl  * Copyright (c) 1992, 1993, 1995
      5      1.12   mycroft  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.6       cgd  * This code is derived from software donated to Berkeley by
      8       1.1       cgd  * Jan-Simon Pendry.
      9       1.1       cgd  *
     10       1.2       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.2       cgd  * modification, are permitted provided that the following conditions
     12       1.2       cgd  * are met:
     13       1.2       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.2       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.2       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.2       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.2       cgd  *    documentation and/or other materials provided with the distribution.
     18      1.44       agc  * 3. Neither the name of the University nor the names of its contributors
     19       1.2       cgd  *    may be used to endorse or promote products derived from this software
     20       1.2       cgd  *    without specific prior written permission.
     21       1.1       cgd  *
     22       1.2       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.2       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.2       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.2       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.2       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.2       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.2       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.2       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.2       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.2       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.2       cgd  * SUCH DAMAGE.
     33       1.1       cgd  *
     34      1.24      fvdl  *	@(#)fdesc_vfsops.c	8.10 (Berkeley) 5/14/95
     35      1.15   mycroft  *
     36      1.15   mycroft  * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp #
     37       1.1       cgd  */
     38       1.1       cgd 
     39       1.1       cgd /*
     40       1.1       cgd  * /dev/fd Filesystem
     41       1.1       cgd  */
     42      1.35     lukem 
     43      1.35     lukem #include <sys/cdefs.h>
     44  1.55.2.2      yamt __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.55.2.2 2006/12/30 20:50:17 yamt Exp $");
     45      1.25  jonathan 
     46      1.33       mrg #if defined(_KERNEL_OPT)
     47      1.25  jonathan #include "opt_compat_netbsd.h"
     48      1.25  jonathan #endif
     49       1.1       cgd 
     50       1.6       cgd #include <sys/param.h>
     51       1.6       cgd #include <sys/systm.h>
     52      1.45    atatat #include <sys/sysctl.h>
     53       1.6       cgd #include <sys/time.h>
     54       1.6       cgd #include <sys/proc.h>
     55       1.6       cgd #include <sys/resourcevar.h>
     56       1.6       cgd #include <sys/filedesc.h>
     57       1.6       cgd #include <sys/vnode.h>
     58       1.6       cgd #include <sys/mount.h>
     59      1.48  christos #include <sys/dirent.h>
     60       1.6       cgd #include <sys/namei.h>
     61       1.6       cgd #include <sys/malloc.h>
     62  1.55.2.1      yamt #include <sys/kauth.h>
     63  1.55.2.1      yamt 
     64       1.6       cgd #include <miscfs/fdesc/fdesc.h>
     65       1.1       cgd 
     66  1.55.2.1      yamt int	fdesc_mount(struct mount *, const char *, void *,
     67  1.55.2.1      yamt 			 struct nameidata *, struct lwp *);
     68  1.55.2.1      yamt int	fdesc_start(struct mount *, int, struct lwp *);
     69  1.55.2.1      yamt int	fdesc_unmount(struct mount *, int, struct lwp *);
     70  1.55.2.1      yamt int	fdesc_quotactl(struct mount *, int, uid_t, void *,
     71  1.55.2.1      yamt 			    struct lwp *);
     72  1.55.2.1      yamt int	fdesc_statvfs(struct mount *, struct statvfs *, struct lwp *);
     73  1.55.2.1      yamt int	fdesc_sync(struct mount *, int, kauth_cred_t, struct lwp *);
     74  1.55.2.1      yamt int	fdesc_vget(struct mount *, ino_t, struct vnode **);
     75      1.21  christos 
     76       1.1       cgd /*
     77       1.1       cgd  * Mount the per-process file descriptors (/dev/fd)
     78       1.1       cgd  */
     79      1.12   mycroft int
     80  1.55.2.2      yamt fdesc_mount(struct mount *mp, const char *path, void *data,
     81  1.55.2.2      yamt     struct nameidata *ndp, struct lwp *l)
     82       1.1       cgd {
     83       1.1       cgd 	int error = 0;
     84       1.1       cgd 	struct fdescmount *fmp;
     85       1.1       cgd 	struct vnode *rvp;
     86       1.1       cgd 
     87      1.38  christos 	if (mp->mnt_flag & MNT_GETARGS)
     88      1.38  christos 		return 0;
     89       1.1       cgd 	/*
     90       1.1       cgd 	 * Update is a no-op
     91       1.1       cgd 	 */
     92       1.1       cgd 	if (mp->mnt_flag & MNT_UPDATE)
     93       1.1       cgd 		return (EOPNOTSUPP);
     94       1.1       cgd 
     95       1.6       cgd 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
     96       1.1       cgd 	if (error)
     97       1.1       cgd 		return (error);
     98       1.1       cgd 
     99       1.6       cgd 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
    100      1.12   mycroft 				M_UFSMNT, M_WAITOK);	/* XXX */
    101       1.1       cgd 	rvp->v_type = VDIR;
    102       1.1       cgd 	rvp->v_flag |= VROOT;
    103       1.1       cgd 	fmp->f_root = rvp;
    104      1.52  jdolecek 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    105      1.14   mycroft 	mp->mnt_flag |= MNT_LOCAL;
    106      1.37     soren 	mp->mnt_data = fmp;
    107      1.31     assar 	vfs_getnewfsid(mp);
    108       1.1       cgd 
    109      1.47  christos 	error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
    110  1.55.2.1      yamt 	    mp, l);
    111      1.28  wrstuden 	VOP_UNLOCK(rvp, 0);
    112      1.39  christos 	return error;
    113       1.1       cgd }
    114       1.1       cgd 
    115      1.12   mycroft int
    116  1.55.2.2      yamt fdesc_start(struct mount *mp, int flags,
    117  1.55.2.2      yamt     struct lwp *l)
    118       1.1       cgd {
    119       1.1       cgd 	return (0);
    120       1.1       cgd }
    121       1.1       cgd 
    122      1.12   mycroft int
    123  1.55.2.2      yamt fdesc_unmount(struct mount *mp, int mntflags, struct lwp *l)
    124       1.1       cgd {
    125       1.1       cgd 	int error;
    126       1.1       cgd 	int flags = 0;
    127      1.55  christos 	struct vnode *rtvp = VFSTOFDESC(mp)->f_root;
    128       1.1       cgd 
    129      1.24      fvdl 	if (mntflags & MNT_FORCE)
    130       1.1       cgd 		flags |= FORCECLOSE;
    131       1.1       cgd 
    132       1.1       cgd 	/*
    133       1.1       cgd 	 * Clear out buffer cache.  I don't think we
    134       1.1       cgd 	 * ever get anything cached at this level at the
    135       1.1       cgd 	 * moment, but who knows...
    136       1.1       cgd 	 */
    137      1.55  christos 	if (rtvp->v_usecount > 1)
    138       1.1       cgd 		return (EBUSY);
    139      1.55  christos 	if ((error = vflush(mp, rtvp, flags)) != 0)
    140       1.1       cgd 		return (error);
    141       1.1       cgd 
    142       1.1       cgd 	/*
    143       1.1       cgd 	 * Release reference on underlying root vnode
    144       1.1       cgd 	 */
    145      1.55  christos 	vrele(rtvp);
    146       1.1       cgd 	/*
    147       1.1       cgd 	 * And blow it away for future re-use
    148       1.1       cgd 	 */
    149      1.55  christos 	vgone(rtvp);
    150       1.1       cgd 	/*
    151       1.1       cgd 	 * Finally, throw away the fdescmount structure
    152       1.1       cgd 	 */
    153      1.12   mycroft 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    154       1.1       cgd 	mp->mnt_data = 0;
    155      1.12   mycroft 
    156      1.12   mycroft 	return (0);
    157       1.1       cgd }
    158       1.1       cgd 
    159      1.12   mycroft int
    160      1.42   thorpej fdesc_root(mp, vpp)
    161       1.1       cgd 	struct mount *mp;
    162       1.1       cgd 	struct vnode **vpp;
    163       1.1       cgd {
    164       1.1       cgd 	struct vnode *vp;
    165       1.1       cgd 
    166       1.1       cgd 	/*
    167       1.1       cgd 	 * Return locked reference to root.
    168       1.1       cgd 	 */
    169       1.1       cgd 	vp = VFSTOFDESC(mp)->f_root;
    170       1.1       cgd 	VREF(vp);
    171      1.24      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    172       1.1       cgd 	*vpp = vp;
    173       1.1       cgd 	return (0);
    174       1.1       cgd }
    175       1.1       cgd 
    176      1.12   mycroft int
    177  1.55.2.2      yamt fdesc_quotactl(struct mount *mp, int cmd, uid_t uid,
    178  1.55.2.2      yamt     void *arg, struct lwp *l)
    179       1.1       cgd {
    180      1.12   mycroft 
    181       1.1       cgd 	return (EOPNOTSUPP);
    182       1.1       cgd }
    183       1.1       cgd 
    184      1.12   mycroft int
    185  1.55.2.1      yamt fdesc_statvfs(mp, sbp, l)
    186       1.1       cgd 	struct mount *mp;
    187      1.47  christos 	struct statvfs *sbp;
    188  1.55.2.1      yamt 	struct lwp *l;
    189       1.1       cgd {
    190       1.1       cgd 	struct filedesc *fdp;
    191  1.55.2.1      yamt 	struct proc *p;
    192       1.1       cgd 	int lim;
    193       1.1       cgd 	int i;
    194       1.1       cgd 	int last;
    195       1.1       cgd 	int freefd;
    196       1.1       cgd 
    197       1.1       cgd 	/*
    198       1.1       cgd 	 * Compute number of free file descriptors.
    199       1.1       cgd 	 * [ Strange results will ensue if the open file
    200       1.1       cgd 	 * limit is ever reduced below the current number
    201       1.1       cgd 	 * of open files... ]
    202       1.1       cgd 	 */
    203  1.55.2.1      yamt 	p = l->l_proc;
    204       1.5   mycroft 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
    205       1.1       cgd 	fdp = p->p_fd;
    206       1.1       cgd 	last = min(fdp->fd_nfiles, lim);
    207       1.1       cgd 	freefd = 0;
    208       1.1       cgd 	for (i = fdp->fd_freefile; i < last; i++)
    209       1.1       cgd 		if (fdp->fd_ofiles[i] == NULL)
    210       1.1       cgd 			freefd++;
    211       1.1       cgd 
    212       1.1       cgd 	/*
    213       1.1       cgd 	 * Adjust for the fact that the fdesc array may not
    214       1.1       cgd 	 * have been fully allocated yet.
    215       1.1       cgd 	 */
    216       1.1       cgd 	if (fdp->fd_nfiles < lim)
    217       1.1       cgd 		freefd += (lim - fdp->fd_nfiles);
    218       1.1       cgd 
    219       1.1       cgd 	sbp->f_bsize = DEV_BSIZE;
    220      1.47  christos 	sbp->f_frsize = DEV_BSIZE;
    221      1.10       cgd 	sbp->f_iosize = DEV_BSIZE;
    222       1.1       cgd 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    223       1.1       cgd 	sbp->f_bfree = 0;
    224       1.1       cgd 	sbp->f_bavail = 0;
    225      1.47  christos 	sbp->f_bresvd = 0;
    226       1.1       cgd 	sbp->f_files = lim + 1;		/* Allow for "." */
    227       1.1       cgd 	sbp->f_ffree = freefd;		/* See comments above */
    228      1.47  christos 	sbp->f_favail = freefd;		/* See comments above */
    229      1.47  christos 	sbp->f_fresvd = 0;
    230      1.47  christos 	copy_statvfs_info(sbp, mp);
    231       1.1       cgd 	return (0);
    232       1.1       cgd }
    233       1.1       cgd 
    234      1.21  christos /*ARGSUSED*/
    235      1.12   mycroft int
    236  1.55.2.2      yamt fdesc_sync(struct mount *mp, int waitfor,
    237  1.55.2.2      yamt     kauth_cred_t uc, struct lwp *l)
    238       1.1       cgd {
    239      1.12   mycroft 
    240       1.1       cgd 	return (0);
    241       1.1       cgd }
    242       1.1       cgd 
    243      1.12   mycroft /*
    244      1.12   mycroft  * Fdesc flat namespace lookup.
    245      1.12   mycroft  * Currently unsupported.
    246      1.12   mycroft  */
    247      1.12   mycroft int
    248  1.55.2.2      yamt fdesc_vget(struct mount *mp, ino_t ino,
    249  1.55.2.2      yamt     struct vnode **vpp)
    250      1.12   mycroft {
    251      1.12   mycroft 
    252      1.12   mycroft 	return (EOPNOTSUPP);
    253      1.12   mycroft }
    254      1.12   mycroft 
    255      1.21  christos 
    256      1.45    atatat SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup")
    257      1.24      fvdl {
    258      1.45    atatat 
    259      1.46    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    260      1.46    atatat 		       CTLFLAG_PERMANENT,
    261      1.45    atatat 		       CTLTYPE_NODE, "vfs", NULL,
    262      1.45    atatat 		       NULL, 0, NULL, 0,
    263      1.45    atatat 		       CTL_VFS, CTL_EOL);
    264      1.46    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    265      1.46    atatat 		       CTLFLAG_PERMANENT,
    266      1.50    atatat 		       CTLTYPE_NODE, "fdesc",
    267      1.50    atatat 		       SYSCTL_DESCR("File-descriptor file system"),
    268      1.45    atatat 		       NULL, 0, NULL, 0,
    269      1.45    atatat 		       CTL_VFS, 7, CTL_EOL);
    270      1.45    atatat 	/*
    271      1.45    atatat 	 * XXX the "7" above could be dynamic, thereby eliminating one
    272      1.45    atatat 	 * more instance of the "number to vfs" mapping problem, but
    273      1.45    atatat 	 * "7" is the order as taken from sys/mount.h
    274      1.45    atatat 	 */
    275      1.24      fvdl }
    276      1.24      fvdl 
    277      1.32  jdolecek extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
    278      1.23   thorpej 
    279      1.32  jdolecek const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
    280      1.23   thorpej 	&fdesc_vnodeop_opv_desc,
    281      1.23   thorpej 	NULL,
    282      1.23   thorpej };
    283      1.23   thorpej 
    284       1.1       cgd struct vfsops fdesc_vfsops = {
    285       1.9       cgd 	MOUNT_FDESC,
    286       1.1       cgd 	fdesc_mount,
    287       1.1       cgd 	fdesc_start,
    288       1.1       cgd 	fdesc_unmount,
    289       1.1       cgd 	fdesc_root,
    290       1.1       cgd 	fdesc_quotactl,
    291      1.47  christos 	fdesc_statvfs,
    292       1.1       cgd 	fdesc_sync,
    293      1.12   mycroft 	fdesc_vget,
    294  1.55.2.2      yamt 	(void *)eopnotsupp,		/* vfs_fhtovp */
    295  1.55.2.2      yamt 	(void *)eopnotsupp,		/* vfs_vptofh */
    296       1.1       cgd 	fdesc_init,
    297      1.34       chs 	NULL,
    298      1.29  jdolecek 	fdesc_done,
    299      1.23   thorpej 	NULL,				/* vfs_mountroot */
    300      1.51   hannken 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    301      1.53   thorpej 	vfs_stdextattrctl,
    302      1.23   thorpej 	fdesc_vnodeopv_descs,
    303  1.55.2.2      yamt 	0,
    304  1.55.2.2      yamt 	{ NULL, NULL},
    305       1.1       cgd };
    306      1.54   thorpej VFS_ATTACH(fdesc_vfsops);
    307