Add ptyfs. This is experimental.

This commit is contained in:
christos 2004-11-11 01:40:32 +00:00
parent f1134ebb91
commit 7fc8278a34
7 changed files with 1972 additions and 2 deletions

View File

@ -1,7 +1,7 @@
# $NetBSD: Makefile,v 1.5 2003/03/16 08:26:47 jdolecek Exp $
# $NetBSD: Makefile,v 1.6 2004/11/11 01:40:32 christos Exp $
SUBDIR= fdesc fifofs genfs kernfs nullfs overlay portal
SUBDIR+= procfs specfs syncfs umapfs
SUBDIR+= procfs ptyfs specfs syncfs umapfs
INCSDIR= /usr/include/miscfs

View File

@ -0,0 +1,7 @@
# $NetBSD: Makefile,v 1.1 2004/11/11 01:40:32 christos Exp $
INCSDIR= /usr/include/miscfs/ptyfs
INCS= ptyfs.h
.include <bsd.kinc.mk>

View File

@ -0,0 +1,7 @@
# $NetBSD: files.ptyfs,v 1.1 2004/11/11 01:40:32 christos Exp $
deffs fs_ptyfs.h PTYFS # XXX
file miscfs/ptyfs/ptyfs_subr.c ptyfs
file miscfs/ptyfs/ptyfs_vfsops.c ptyfs
file miscfs/ptyfs/ptyfs_vnops.c ptyfs

151
sys/miscfs/ptyfs/ptyfs.h Normal file
View File

@ -0,0 +1,151 @@
/* $NetBSD: ptyfs.h,v 1.1 2004/11/11 01:40:32 christos Exp $ */
/*
* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ptyfs.h 8.9 (Berkeley) 5/14/95
*/
/*
* Copyright (c) 1993 Jan-Simon Pendry
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ptyfs.h 8.9 (Berkeley) 5/14/95
*/
#ifdef _KERNEL
/*
* The different types of node in a ptyfs filesystem
*/
typedef enum {
PTYFSpts, /* The slave side of a pty */
PTYFSptc, /* The controlling side of a pty */
PTYFSroot, /* the filesystem root */
} ptyfstype;
/*
* control data for the proc file system.
*/
struct ptyfsnode {
LIST_ENTRY(ptyfsnode) ptyfs_hash; /* hash chain */
struct vnode *ptyfs_vnode; /* vnode associated with this ptyfsnode */
ptyfstype ptyfs_type; /* type of ptyfs node */
int ptyfs_pty; /* the pty index */
u_long ptyfs_fileno; /* unique file id */
int ptyfs_flag; /* status flag for times */
#define PTYFS_ACCESS 1
#define PTYFS_MODIFY 2
#define PTYFS_CHANGE 4
/* Attribute information */
uid_t ptyfs_uid;
gid_t ptyfs_gid;
mode_t ptyfs_mode;
int ptyfs_flags;
struct timespec ptyfs_ctime, ptyfs_mtime, ptyfs_atime, ptyfs_birthtime;
};
#endif /* _KERNEL */
/*
* Kernel stuff follows
*/
#ifdef _KERNEL
#define CNEQ(cnp, s, len) \
((cnp)->cn_namelen == (len) && \
(memcmp((s), (cnp)->cn_nameptr, (len)) == 0))
#define UIO_MX 32
#define PTYFS_FILENO(pty, type) \
((((uint32_t)type) << 30) | (pty + 1))
#define PTYFS_MAKEDEV(ptyfs) \
pty_makedev((ptyfs)->ptyfs_type == PTYFSpts ? 't' : 'p', (ptyfs)->ptyfs_pty)
/*
* Convert between ptyfsnode vnode
*/
#define VTOPTYFS(vp) ((struct ptyfsnode *)(vp)->v_data)
#define PTYFSTOV(ptyfs) ((ptyfs)->ptyfs_vnode)
int ptyfs_freevp(struct vnode *);
int ptyfs_allocvp(struct mount *, struct vnode **, ptyfstype, int,
struct proc *);
void ptyfs_hashinit(void);
void ptyfs_hashreinit(void);
void ptyfs_hashdone(void);
int ptyfs_getfp(struct ptyfsnode *, struct proc **, struct file **);
/* functions to check whether or not files should be displayed */
int ptyfs_validfile(struct proc *, struct mount *);
int ptyfs_validfpregs(struct proc *, struct mount *);
int ptyfs_validregs(struct proc *, struct mount *);
int ptyfs_validmap(struct proc *, struct mount *);
extern int (**ptyfs_vnodeop_p)(void *);
extern struct vfsops ptyfs_vfsops;
int ptyfs_root(struct mount *, struct vnode **);
#ifdef SYSCTL_SETUP_PROTO
SYSCTL_SETUP_PROTO(sysctl_vfs_ptyfs_setup);
#endif /* SYSCTL_SETUP_PROTO */
#endif /* _KERNEL */

View File

@ -0,0 +1,421 @@
/* $NetBSD: ptyfs_subr.c,v 1.1 2004/11/11 01:40:32 christos Exp $ */
/*
* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ptyfs_subr.c 8.6 (Berkeley) 5/14/95
*/
/*
* Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
* Copyright (c) 1993 Jan-Simon Pendry
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ptyfs_subr.c,v 1.1 2004/11/11 01:40:32 christos Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <sys/malloc.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/namei.h>
#include <sys/filedesc.h>
#include <sys/select.h>
#include <sys/tty.h>
#include <sys/pty.h>
#include <miscfs/ptyfs/ptyfs.h>
#include <miscfs/specfs/specdev.h>
static struct lock ptyfs_hashlock;
static LIST_HEAD(ptyfs_hashhead, ptyfsnode) *ptyfs_used_tbl, *ptyfs_free_tbl;
static u_long ptyfs_used_mask, ptyfs_free_mask; /* size of hash table - 1 */
static struct simplelock ptyfs_used_slock, ptyfs_free_slock;
static void ptyfs_getinfo(struct ptyfsnode *, struct proc *);
static void ptyfs_hashins(struct ptyfsnode *);
static void ptyfs_hashrem(struct ptyfsnode *);
static struct vnode *ptyfs_used_get(ptyfstype, int, struct mount *);
static struct ptyfsnode *ptyfs_free_get(ptyfstype, int, struct proc *);
static void ptyfs_rehash(struct simplelock *, struct ptyfs_hashhead **,
u_long *);
#define PTYHASH(type, pty, mask) (PTYFS_FILENO(type, pty) % (mask + 1))
static void
ptyfs_getinfo(struct ptyfsnode *ptyfs, struct proc *p)
{
extern struct ptm_pty *ptyfs_save_ptm, ptm_ptyfspty;
if (ptyfs->ptyfs_type == PTYFSroot) {
ptyfs->ptyfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
S_IROTH|S_IXOTH;
goto out;
} else
ptyfs->ptyfs_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|
S_IROTH|S_IWOTH;
if (ptyfs_save_ptm != NULL && ptyfs_save_ptm != &ptm_ptyfspty) {
int error;
struct nameidata nd;
char ttyname[64];
struct ucred *cred;
struct vattr va;
/*
* We support traditional ptys, so we copy the info
* from the inode
*/
if ((error = (*ptyfs_save_ptm->makename)(
ptyfs_save_ptm, ttyname, sizeof(ttyname),
ptyfs->ptyfs_pty, ptyfs->ptyfs_type == PTYFSpts ? 't'
: 'p')) != 0)
goto out;
NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, ttyname,
p);
if ((error = namei(&nd)) != 0)
goto out;
cred = crget();
error = VOP_GETATTR(nd.ni_vp, &va, cred, p);
crfree(cred);
VOP_UNLOCK(nd.ni_vp, 0);
vrele(nd.ni_vp);
if (error)
goto out;
ptyfs->ptyfs_uid = va.va_uid;
ptyfs->ptyfs_gid = va.va_gid;
ptyfs->ptyfs_mode = va.va_mode;
ptyfs->ptyfs_flags = va.va_flags;
ptyfs->ptyfs_birthtime = va.va_birthtime;
ptyfs->ptyfs_ctime = va.va_ctime;
ptyfs->ptyfs_mtime = va.va_mtime;
ptyfs->ptyfs_atime = va.va_atime;
return;
}
out:
ptyfs->ptyfs_uid = ptyfs->ptyfs_gid = 0;
TIMEVAL_TO_TIMESPEC(&time, &ptyfs->ptyfs_ctime);
ptyfs->ptyfs_birthtime = ptyfs->ptyfs_mtime =
ptyfs->ptyfs_atime = ptyfs->ptyfs_ctime;
ptyfs->ptyfs_flags = 0;
}
/*
* allocate a ptyfsnode/vnode pair. the vnode is
* referenced, and locked.
*
* the pid, ptyfs_type, and mount point uniquely
* identify a ptyfsnode. the mount point is needed
* because someone might mount this filesystem
* twice.
*
* all ptyfsnodes are maintained on a singly-linked
* list. new nodes are only allocated when they cannot
* be found on this list. entries on the list are
* removed when the vfs reclaim entry is called.
*
* a single lock is kept for the entire list. this is
* needed because the getnewvnode() function can block
* waiting for a vnode to become free, in which case there
* may be more than one ptyess trying to get the same
* vnode. this lock is only taken if we are going to
* call getnewvnode, since the kernel itself is single-threaded.
*
* if an entry is found on the list, then call vget() to
* take a reference. this is done because there may be
* zero references to it and so it needs to removed from
* the vnode free list.
*/
int
ptyfs_allocvp(struct mount *mp, struct vnode **vpp, ptyfstype type, int pty,
struct proc *p)
{
struct ptyfsnode *ptyfs;
struct vnode *vp, *nvp;
int error;
do {
if ((*vpp = ptyfs_used_get(type, pty, mp)) != NULL)
return 0;
} while (lockmgr(&ptyfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
if ((error = getnewvnode(VT_PTYFS, mp, ptyfs_vnodeop_p, &vp)) != 0) {
*vpp = NULL;
lockmgr(&ptyfs_hashlock, LK_RELEASE, NULL);
return error;
}
vp->v_data = ptyfs = ptyfs_free_get(type, pty, p);
ptyfs->ptyfs_vnode = vp;
switch (type) {
case PTYFSroot: /* /pts = dr-xr-xr-x */
vp->v_type = VDIR;
vp->v_flag = VROOT;
break;
case PTYFSpts: /* /pts/N = cxxxxxxxxx */
case PTYFSptc: /* controlling side = cxxxxxxxxx */
vp->v_type = VCHR;
if ((nvp = checkalias(vp, PTYFS_MAKEDEV(ptyfs), mp)) != NULL) {
/*
* Discard unneeded vnode, but save its inode.
*/
nvp->v_data = vp->v_data;
vp->v_data = NULL;
/* XXX spec_vnodeops has no locking, do it explicitly */
VOP_UNLOCK(vp, 0);
vp->v_op = spec_vnodeop_p;
vp->v_flag &= ~VLOCKSWORK;
vrele(vp);
vgone(vp);
lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
/*
* Reinitialize aliased inode.
*/
vp = nvp;
ptyfs->ptyfs_vnode = vp;
}
break;
default:
panic("ptyfs_allocvp");
}
ptyfs_hashins(ptyfs);
uvm_vnp_setsize(vp, 0);
lockmgr(&ptyfs_hashlock, LK_RELEASE, NULL);
*vpp = vp;
return 0;
}
int
ptyfs_freevp(struct vnode *vp)
{
struct ptyfsnode *ptyfs = VTOPTYFS(vp);
ptyfs_hashrem(ptyfs);
vp->v_data = NULL;
return 0;
}
/*
* Initialize ptyfsnode hash table.
*/
void
ptyfs_hashinit(void)
{
lockinit(&ptyfs_hashlock, PINOD, "ptyfs_hashlock", 0, 0);
ptyfs_used_tbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
M_WAITOK, &ptyfs_used_mask);
ptyfs_free_tbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
M_WAITOK, &ptyfs_free_mask);
simple_lock_init(&ptyfs_used_slock);
simple_lock_init(&ptyfs_free_slock);
}
void
ptyfs_hashreinit(void)
{
ptyfs_rehash(&ptyfs_used_slock, &ptyfs_used_tbl, &ptyfs_used_mask);
ptyfs_rehash(&ptyfs_free_slock, &ptyfs_free_tbl, &ptyfs_free_mask);
}
static void
ptyfs_rehash(struct simplelock *hlock, struct ptyfs_hashhead **hhead,
u_long *hmask)
{
struct ptyfsnode *pp;
struct ptyfs_hashhead *oldhash, *hash;
u_long i, oldmask, mask, val;
hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
&mask);
simple_lock(hlock);
oldhash = *hhead;
oldmask = *hmask;
*hhead = hash;
*hmask = mask;
for (i = 0; i <= oldmask; i++) {
while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
LIST_REMOVE(pp, ptyfs_hash);
val = PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
ptyfs_used_mask);
LIST_INSERT_HEAD(&hash[val], pp, ptyfs_hash);
}
}
simple_unlock(hlock);
hashdone(oldhash, M_UFSMNT);
}
/*
* Free ptyfsnode hash table.
*/
void
ptyfs_hashdone(void)
{
hashdone(ptyfs_used_tbl, M_UFSMNT);
hashdone(ptyfs_free_tbl, M_UFSMNT);
}
/*
* Get a ptyfsnode from the free table, or allocate one.
* Removes the node from the free table.
*/
struct ptyfsnode *
ptyfs_free_get(ptyfstype type, int pty, struct proc *p)
{
struct ptyfs_hashhead *ppp;
struct ptyfsnode *pp;
simple_lock(&ptyfs_free_slock);
ppp = &ptyfs_free_tbl[PTYHASH(type, pty, ptyfs_free_mask)];
LIST_FOREACH(pp, ppp, ptyfs_hash) {
if (pty == pp->ptyfs_pty && pp->ptyfs_type == type) {
LIST_REMOVE(pp, ptyfs_hash);
simple_unlock(&ptyfs_free_slock);
return pp;
}
}
simple_unlock(&ptyfs_free_slock);
MALLOC(pp, void *, sizeof(struct ptyfsnode), M_TEMP, M_WAITOK);
pp->ptyfs_pty = pty;
pp->ptyfs_type = type;
pp->ptyfs_fileno = PTYFS_FILENO(pty, type);
ptyfs_getinfo(pp, p);
return pp;
}
struct vnode *
ptyfs_used_get(ptyfstype type, int pty, struct mount *mp)
{
struct ptyfs_hashhead *ppp;
struct ptyfsnode *pp;
struct vnode *vp;
loop:
simple_lock(&ptyfs_used_slock);
ppp = &ptyfs_used_tbl[PTYHASH(type, pty, ptyfs_used_mask)];
LIST_FOREACH(pp, ppp, ptyfs_hash) {
vp = PTYFSTOV(pp);
if (pty == pp->ptyfs_pty && pp->ptyfs_type == type &&
vp->v_mount == mp) {
simple_lock(&vp->v_interlock);
simple_unlock(&ptyfs_used_slock);
if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
goto loop;
return vp;
}
}
simple_unlock(&ptyfs_used_slock);
return NULL;
}
/*
* Insert the ptyfsnode into the used table and lock it.
*/
static void
ptyfs_hashins(struct ptyfsnode *pp)
{
struct ptyfs_hashhead *ppp;
/* lock the ptyfsnode, then put it on the appropriate hash list */
lockmgr(&pp->ptyfs_vnode->v_lock, LK_EXCLUSIVE, NULL);
simple_lock(&ptyfs_used_slock);
ppp = &ptyfs_used_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
ptyfs_used_mask)];
LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
simple_unlock(&ptyfs_used_slock);
}
/*
* Remove the ptyfsnode from the used table, and add it to the free table
*/
static void
ptyfs_hashrem(struct ptyfsnode *pp)
{
struct ptyfs_hashhead *ppp;
simple_lock(&ptyfs_used_slock);
LIST_REMOVE(pp, ptyfs_hash);
simple_unlock(&ptyfs_used_slock);
simple_lock(&ptyfs_free_slock);
ppp = &ptyfs_free_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
ptyfs_free_mask)];
LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
simple_unlock(&ptyfs_free_slock);
}

View File

@ -0,0 +1,354 @@
/* $NetBSD: ptyfs_vfsops.c,v 1.1 2004/11/11 01:40:32 christos Exp $ */
/*
* Copyright (c) 1992, 1993, 1995
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software donated to Berkeley by
* Jan-Simon Pendry.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* Kernel params Filesystem
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ptyfs_vfsops.c,v 1.1 2004/11/11 01:40:32 christos Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/conf.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/dirent.h>
#include <sys/malloc.h>
#include <sys/syslog.h>
#include <sys/select.h>
#include <sys/tty.h>
#include <sys/pty.h>
#include <miscfs/specfs/specdev.h>
#include <miscfs/ptyfs/ptyfs.h>
MALLOC_DEFINE(M_PTYFSMNT, "ptyfs mount", "ptyfs mount structures");
void ptyfs_init(void);
void ptyfs_reinit(void);
void ptyfs_done(void);
int ptyfs_mount(struct mount *, const char *, void *, struct nameidata *,
struct proc *);
int ptyfs_start(struct mount *, int, struct proc *);
int ptyfs_unmount(struct mount *, int, struct proc *);
int ptyfs_statvfs(struct mount *, struct statvfs *, struct proc *);
int ptyfs_quotactl(struct mount *, int, uid_t, void *, struct proc *);
int ptyfs_sync(struct mount *, int, struct ucred *, struct proc *);
int ptyfs_vget(struct mount *, ino_t, struct vnode **);
int ptyfs_fhtovp(struct mount *, struct fid *, struct vnode **);
int ptyfs_checkexp(struct mount *, struct mbuf *, int *, struct ucred **);
int ptyfs_vptofh(struct vnode *, struct fid *);
static int ptyfs__allocvp(struct ptm_pty *, struct proc *, struct vnode **,
dev_t, char);
static int ptyfs__makename(struct ptm_pty *, char *, size_t, dev_t, char);
/*
* ptm glue: When we mount, we make ptm point to us.
*/
struct ptm_pty *ptyfs_save_ptm;
struct ptm_pty ptm_ptyfspty = {
ptyfs__allocvp,
ptyfs__makename,
NULL
};
static int
ptyfs__makename(struct ptm_pty *pt, char *buf, size_t bufsiz, dev_t dev,
char ms)
{
struct mount *mp = pt->arg;
size_t len;
switch (ms) {
case 'p':
/* We don't provide access to the master, should we? */
len = snprintf(buf, bufsiz, "/dev/null");
break;
case 't':
len = snprintf(buf, bufsiz, "%s/%d", mp->mnt_stat.f_mntonname,
minor(dev));
break;
default:
return EINVAL;
}
return len >= bufsiz ? ENOSPC : 0;
}
static int
/*ARGSUSED*/
ptyfs__allocvp(struct ptm_pty *pt, struct proc *p, struct vnode **vpp,
dev_t dev, char ms)
{
struct mount *mp = pt->arg;
ptyfstype type;
switch (ms) {
case 'p':
type = PTYFSptc;
break;
case 't':
type = PTYFSpts;
break;
default:
return EINVAL;
}
return ptyfs_allocvp(mp, vpp, type, minor(dev), p);
}
void
ptyfs_init(void)
{
#ifdef _LKM
malloc_type_attach(M_PTYFSMNT);
#endif
ptyfs_hashinit();
}
void
ptyfs_reinit(void)
{
ptyfs_hashreinit();
}
void
ptyfs_done(void)
{
#ifdef _LKM
malloc_type_detach(M_PTYFSMNT);
#endif
ptyfs_hashdone();
}
/*
* Mount the Pseudo tty params filesystem
*/
int
ptyfs_mount(struct mount *mp, const char *path, void *data,
struct nameidata *ndp, struct proc *p)
{
int error = 0;
if (UIO_MX & (UIO_MX - 1)) {
log(LOG_ERR, "ptyfs: invalid directory entry size");
return EINVAL;
}
if (mp->mnt_flag & MNT_GETARGS)
return 0;
/*
* Update is a no-op
*/
if (mp->mnt_flag & MNT_UPDATE)
return EOPNOTSUPP;
mp->mnt_data = NULL;
mp->mnt_flag |= MNT_LOCAL;
vfs_getnewfsid(mp);
if ((error = set_statvfs_info(path, UIO_USERSPACE, "ptyfs",
UIO_SYSSPACE, mp, p)) != 0) {
return error;
}
/* Point pty access to us */
if (ptyfs_save_ptm != NULL)
return EBUSY;
ptm_ptyfspty.arg = mp;
ptyfs_save_ptm = pty_sethandler(&ptm_ptyfspty);
return 0;
}
/*ARGSUSED*/
int
ptyfs_start(struct mount *mp, int flags, struct proc *p)
{
return 0;
}
/*ARGSUSED*/
int
ptyfs_unmount(struct mount *mp, int mntflags, struct proc *p)
{
int error;
int flags = 0;
if (mntflags & MNT_FORCE)
flags |= FORCECLOSE;
if ((error = vflush(mp, 0, flags)) != 0)
return (error);
/* Restore where pty access was pointing */
ptm_ptyfspty.arg = NULL;
(void)pty_sethandler(ptyfs_save_ptm);
ptyfs_save_ptm = NULL;
/*
* Finally, throw away the ptyfs_mount structure
*/
return 0;
}
int
ptyfs_root(struct mount *mp, struct vnode **vpp)
{
/* setup "." */
return ptyfs_allocvp(mp, vpp, PTYFSroot, 0, NULL);
}
/*ARGSUSED*/
int
ptyfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct proc *p)
{
return EOPNOTSUPP;
}
/*ARGSUSED*/
int
ptyfs_statvfs(struct mount *mp, struct statvfs *sbp, struct proc *p)
{
sbp->f_bsize = DEV_BSIZE;
sbp->f_frsize = DEV_BSIZE;
sbp->f_iosize = DEV_BSIZE;
sbp->f_blocks = 2; /* 1K to keep df happy */
sbp->f_bfree = 0;
sbp->f_bavail = 0;
sbp->f_bresvd = 0;
sbp->f_files = 1024; /* XXX lie */
sbp->f_ffree = 128; /* XXX lie */
sbp->f_favail = 128; /* XXX lie */
sbp->f_fresvd = 0;
sbp->f_namemax = MAXNAMLEN;
copy_statvfs_info(sbp, mp);
return 0;
}
/*ARGSUSED*/
int
ptyfs_sync(struct mount *mp, int waitfor, struct ucred *uc, struct proc *p)
{
return 0;
}
/*
* Kernfs flat namespace lookup.
* Currently unsupported.
*/
/*ARGSUSED*/
int
ptyfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
{
return EOPNOTSUPP;
}
/*ARGSUSED*/
int
ptyfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
{
return EOPNOTSUPP;
}
/*ARGSUSED*/
int
ptyfs_checkexp(struct mount *mp, struct mbuf *mb, int *wh, struct ucred **anon)
{
return EOPNOTSUPP;
}
SYSCTL_SETUP(sysctl_vfs_ptyfs_setup, "sysctl vfs.ptyfs subtree setup")
{
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "vfs", NULL,
NULL, 0, NULL, 0,
CTL_VFS, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "ptyfs",
SYSCTL_DESCR("Pty file system"),
NULL, 0, NULL, 0,
CTL_VFS, 23, CTL_EOL);
/*
* XXX the "23" above could be dynamic, thereby eliminating
* one more instance of the "number to vfs" mapping problem,
* but "23" is the order as taken from sys/mount.h
*/
}
/*ARGSUSED*/
int
ptyfs_vptofh(struct vnode *vp, struct fid *fhp)
{
return EOPNOTSUPP;
}
extern const struct vnodeopv_desc ptyfs_vnodeop_opv_desc;
const struct vnodeopv_desc * const ptyfs_vnodeopv_descs[] = {
&ptyfs_vnodeop_opv_desc,
NULL,
};
struct vfsops ptyfs_vfsops = {
MOUNT_PTYFS,
ptyfs_mount,
ptyfs_start,
ptyfs_unmount,
ptyfs_root,
ptyfs_quotactl,
ptyfs_statvfs,
ptyfs_sync,
ptyfs_vget,
ptyfs_fhtovp,
ptyfs_vptofh,
ptyfs_init,
ptyfs_reinit,
ptyfs_done,
NULL,
NULL, /* vfs_mountroot */
ptyfs_checkexp,
(int (*)(struct mount *, struct vnode *, struct timespec *))eopnotsupp,
ptyfs_vnodeopv_descs,
};

File diff suppressed because it is too large Load Diff