The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/ptyfs/ptyfs_subr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: ptyfs_subr.c,v 1.7 2006/05/14 21:31:52 elad Exp $      */
    2 
    3 /*
    4  * Copyright (c) 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * This code is derived from software contributed 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  *      @(#)ptyfs_subr.c        8.6 (Berkeley) 5/14/95
   35  */
   36 
   37 /*
   38  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
   39  * Copyright (c) 1993 Jan-Simon Pendry
   40  *
   41  * This code is derived from software contributed to Berkeley by
   42  * Jan-Simon Pendry.
   43  *
   44  * Redistribution and use in source and binary forms, with or without
   45  * modification, are permitted provided that the following conditions
   46  * are met:
   47  * 1. Redistributions of source code must retain the above copyright
   48  *    notice, this list of conditions and the following disclaimer.
   49  * 2. Redistributions in binary form must reproduce the above copyright
   50  *    notice, this list of conditions and the following disclaimer in the
   51  *    documentation and/or other materials provided with the distribution.
   52  * 3. All advertising materials mentioning features or use of this software
   53  *    must display the following acknowledgement:
   54  *      This product includes software developed by the University of
   55  *      California, Berkeley and its contributors.
   56  * 4. Neither the name of the University nor the names of its contributors
   57  *    may be used to endorse or promote products derived from this software
   58  *    without specific prior written permission.
   59  *
   60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   70  * SUCH DAMAGE.
   71  *
   72  *      @(#)procfs_subr.c       8.6 (Berkeley) 5/14/95
   73  */
   74 
   75 #include <sys/cdefs.h>
   76 __KERNEL_RCSID(0, "$NetBSD: ptyfs_subr.c,v 1.7 2006/05/14 21:31:52 elad Exp $");
   77 
   78 #include <sys/param.h>
   79 #include <sys/systm.h>
   80 #include <sys/time.h>
   81 #include <sys/kernel.h>
   82 #include <sys/vnode.h>
   83 #include <sys/malloc.h>
   84 #include <sys/stat.h>
   85 #include <sys/file.h>
   86 #include <sys/namei.h>
   87 #include <sys/filedesc.h>
   88 #include <sys/select.h>
   89 #include <sys/tty.h>
   90 #include <sys/pty.h>
   91 #include <sys/kauth.h>
   92 
   93 #include <fs/ptyfs/ptyfs.h>
   94 #include <miscfs/specfs/specdev.h>
   95 
   96 static struct lock ptyfs_hashlock;
   97 
   98 static LIST_HEAD(ptyfs_hashhead, ptyfsnode) *ptyfs_used_tbl, *ptyfs_free_tbl;
   99 static u_long ptyfs_used_mask, ptyfs_free_mask; /* size of hash table - 1 */
  100 static struct simplelock ptyfs_used_slock, ptyfs_free_slock;
  101 
  102 static void ptyfs_getinfo(struct ptyfsnode *, struct lwp *);
  103 
  104 static void ptyfs_hashins(struct ptyfsnode *);
  105 static void ptyfs_hashrem(struct ptyfsnode *);
  106 
  107 static struct vnode *ptyfs_used_get(ptyfstype, int, struct mount *);
  108 static struct ptyfsnode *ptyfs_free_get(ptyfstype, int, struct lwp *);
  109 
  110 static void ptyfs_rehash(struct simplelock *, struct ptyfs_hashhead **,
  111     u_long *);
  112 
  113 #define PTYHASH(type, pty, mask) (PTYFS_FILENO(type, pty) % (mask + 1))
  114 
  115 
  116 static void
  117 ptyfs_getinfo(struct ptyfsnode *ptyfs, struct lwp *l)
  118 {
  119         extern struct ptm_pty *ptyfs_save_ptm, ptm_ptyfspty;
  120 
  121         if (ptyfs->ptyfs_type == PTYFSroot) {
  122                 ptyfs->ptyfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
  123                     S_IROTH|S_IXOTH;
  124                 goto out;
  125         } else
  126                 ptyfs->ptyfs_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|
  127                     S_IROTH|S_IWOTH;
  128 
  129         if (ptyfs_save_ptm != NULL && ptyfs_save_ptm != &ptm_ptyfspty) {
  130                 int error;
  131                 struct nameidata nd;
  132                 char ttyname[64];
  133                 kauth_cred_t cred;
  134                 struct vattr va;
  135                 /*
  136                  * We support traditional ptys, so we copy the info
  137                  * from the inode
  138                  */
  139                 if ((error = (*ptyfs_save_ptm->makename)(
  140                         ptyfs_save_ptm, l, ttyname, sizeof(ttyname),
  141                         ptyfs->ptyfs_pty, ptyfs->ptyfs_type == PTYFSpts ? 't'
  142                         : 'p')) != 0)
  143                                 goto out;
  144                 NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, ttyname,
  145                      l);
  146                 if ((error = namei(&nd)) != 0)
  147                         goto out;
  148                 cred = kauth_cred_alloc();
  149                 error = VOP_GETATTR(nd.ni_vp, &va, cred, l);
  150                 kauth_cred_free(cred);
  151                 VOP_UNLOCK(nd.ni_vp, 0);
  152                 vrele(nd.ni_vp);
  153                 if (error)
  154                         goto out;
  155                 ptyfs->ptyfs_uid = va.va_uid;
  156                 ptyfs->ptyfs_gid = va.va_gid;
  157                 ptyfs->ptyfs_mode = va.va_mode;
  158                 ptyfs->ptyfs_flags = va.va_flags;
  159                 ptyfs->ptyfs_birthtime = va.va_birthtime;
  160                 ptyfs->ptyfs_ctime = va.va_ctime;
  161                 ptyfs->ptyfs_mtime = va.va_mtime;
  162                 ptyfs->ptyfs_atime = va.va_atime;
  163                 return;
  164         }
  165 out:
  166         ptyfs->ptyfs_uid = ptyfs->ptyfs_gid = 0;
  167         ptyfs->ptyfs_flags |= PTYFS_CHANGE;
  168         PTYFS_ITIMES(ptyfs, NULL, NULL, NULL);
  169         ptyfs->ptyfs_birthtime = ptyfs->ptyfs_mtime =
  170             ptyfs->ptyfs_atime = ptyfs->ptyfs_ctime;
  171         ptyfs->ptyfs_flags = 0;
  172 }
  173 
  174 
  175 /*
  176  * allocate a ptyfsnode/vnode pair.  the vnode is
  177  * referenced, and locked.
  178  *
  179  * the pid, ptyfs_type, and mount point uniquely
  180  * identify a ptyfsnode.  the mount point is needed
  181  * because someone might mount this filesystem
  182  * twice.
  183  *
  184  * all ptyfsnodes are maintained on a singly-linked
  185  * list.  new nodes are only allocated when they cannot
  186  * be found on this list.  entries on the list are
  187  * removed when the vfs reclaim entry is called.
  188  *
  189  * a single lock is kept for the entire list.  this is
  190  * needed because the getnewvnode() function can block
  191  * waiting for a vnode to become free, in which case there
  192  * may be more than one ptyess trying to get the same
  193  * vnode.  this lock is only taken if we are going to
  194  * call getnewvnode, since the kernel itself is single-threaded.
  195  *
  196  * if an entry is found on the list, then call vget() to
  197  * take a reference.  this is done because there may be
  198  * zero references to it and so it needs to removed from
  199  * the vnode free list.
  200  */
  201 int
  202 ptyfs_allocvp(struct mount *mp, struct vnode **vpp, ptyfstype type, int pty,
  203     struct lwp *l)
  204 {
  205         struct ptyfsnode *ptyfs;
  206         struct vnode *vp, *nvp;
  207         int error;
  208 
  209         do {
  210                 if ((*vpp = ptyfs_used_get(type, pty, mp)) != NULL)
  211                         return 0;
  212         } while (lockmgr(&ptyfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
  213 
  214         if ((error = getnewvnode(VT_PTYFS, mp, ptyfs_vnodeop_p, &vp)) != 0) {
  215                 *vpp = NULL;
  216                 lockmgr(&ptyfs_hashlock, LK_RELEASE, NULL);
  217                 return error;
  218         }
  219 
  220         vp->v_data = ptyfs = ptyfs_free_get(type, pty, l);
  221         ptyfs->ptyfs_vnode = vp;
  222 
  223         switch (type) {
  224         case PTYFSroot: /* /pts = dr-xr-xr-x */
  225                 vp->v_type = VDIR;
  226                 vp->v_flag = VROOT;
  227                 break;
  228 
  229         case PTYFSpts:  /* /pts/N = cxxxxxxxxx */
  230         case PTYFSptc:  /* controlling side = cxxxxxxxxx */
  231                 vp->v_type = VCHR;
  232                 if ((nvp = checkalias(vp, PTYFS_MAKEDEV(ptyfs), mp)) != NULL) {
  233                         /*
  234                          * Discard unneeded vnode, but save its inode.
  235                          */
  236                         nvp->v_data = vp->v_data;
  237                         vp->v_data = NULL;
  238                         /* XXX spec_vnodeops has no locking, do it explicitly */
  239                         VOP_UNLOCK(vp, 0);
  240                         vp->v_op = spec_vnodeop_p;
  241                         vp->v_flag &= ~VLOCKSWORK;
  242                         vrele(vp);
  243                         vgone(vp);
  244                         lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
  245                         /*
  246                          * Reinitialize aliased inode.
  247                          */
  248                         vp = nvp;
  249                         ptyfs->ptyfs_vnode = vp;
  250                 }
  251                 break;
  252         default:
  253                 panic("ptyfs_allocvp");
  254         }
  255 
  256         ptyfs_hashins(ptyfs);
  257         uvm_vnp_setsize(vp, 0);
  258         lockmgr(&ptyfs_hashlock, LK_RELEASE, NULL);
  259 
  260         *vpp = vp;
  261         return 0;
  262 }
  263 
  264 int
  265 ptyfs_freevp(struct vnode *vp)
  266 {
  267         struct ptyfsnode *ptyfs = VTOPTYFS(vp);
  268 
  269         ptyfs_hashrem(ptyfs);
  270         vp->v_data = NULL;
  271         return 0;
  272 }
  273 
  274 /*
  275  * Initialize ptyfsnode hash table.
  276  */
  277 void
  278 ptyfs_hashinit(void)
  279 {
  280         lockinit(&ptyfs_hashlock, PINOD, "ptyfs_hashlock", 0, 0);
  281         ptyfs_used_tbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
  282             M_WAITOK, &ptyfs_used_mask);
  283         ptyfs_free_tbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
  284             M_WAITOK, &ptyfs_free_mask);
  285         simple_lock_init(&ptyfs_used_slock);
  286         simple_lock_init(&ptyfs_free_slock);
  287 }
  288 
  289 void
  290 ptyfs_hashreinit(void)
  291 {
  292     ptyfs_rehash(&ptyfs_used_slock, &ptyfs_used_tbl, &ptyfs_used_mask);
  293     ptyfs_rehash(&ptyfs_free_slock, &ptyfs_free_tbl, &ptyfs_free_mask);
  294 }
  295 
  296 static void
  297 ptyfs_rehash(struct simplelock *hlock, struct ptyfs_hashhead **hhead,
  298     u_long *hmask)
  299 {
  300         struct ptyfsnode *pp;
  301         struct ptyfs_hashhead *oldhash, *hash;
  302         u_long i, oldmask, mask, val;
  303 
  304         hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
  305             &mask);
  306 
  307         simple_lock(hlock);
  308         oldhash = *hhead;
  309         oldmask = *hmask;
  310         *hhead = hash;
  311         *hmask = mask;
  312         for (i = 0; i <= oldmask; i++) {
  313                 while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
  314                         LIST_REMOVE(pp, ptyfs_hash);
  315                         val = PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
  316                             ptyfs_used_mask);
  317                         LIST_INSERT_HEAD(&hash[val], pp, ptyfs_hash);
  318                 }
  319         }
  320         simple_unlock(hlock);
  321         hashdone(oldhash, M_UFSMNT);
  322 }
  323 
  324 /*
  325  * Free ptyfsnode hash table.
  326  */
  327 void
  328 ptyfs_hashdone(void)
  329 {
  330         hashdone(ptyfs_used_tbl, M_UFSMNT);
  331         hashdone(ptyfs_free_tbl, M_UFSMNT);
  332 }
  333 
  334 /*
  335  * Get a ptyfsnode from the free table, or allocate one.
  336  * Removes the node from the free table.
  337  */
  338 struct ptyfsnode *
  339 ptyfs_free_get(ptyfstype type, int pty, struct lwp *l)
  340 {
  341         struct ptyfs_hashhead *ppp;
  342         struct ptyfsnode *pp;
  343 
  344         simple_lock(&ptyfs_free_slock);
  345         ppp = &ptyfs_free_tbl[PTYHASH(type, pty, ptyfs_free_mask)];
  346         LIST_FOREACH(pp, ppp, ptyfs_hash) {
  347                 if (pty == pp->ptyfs_pty && pp->ptyfs_type == type) {
  348                         LIST_REMOVE(pp, ptyfs_hash);
  349                         simple_unlock(&ptyfs_free_slock);
  350                         return pp;
  351                 }
  352         }
  353         simple_unlock(&ptyfs_free_slock);
  354 
  355         MALLOC(pp, void *, sizeof(struct ptyfsnode), M_TEMP, M_WAITOK);
  356         pp->ptyfs_pty = pty;
  357         pp->ptyfs_type = type;
  358         pp->ptyfs_fileno = PTYFS_FILENO(pty, type);
  359         ptyfs_getinfo(pp, l);
  360         return pp;
  361 }
  362 
  363 struct vnode *
  364 ptyfs_used_get(ptyfstype type, int pty, struct mount *mp)
  365 {
  366         struct ptyfs_hashhead *ppp;
  367         struct ptyfsnode *pp;
  368         struct vnode *vp;
  369 
  370 loop:
  371         simple_lock(&ptyfs_used_slock);
  372         ppp = &ptyfs_used_tbl[PTYHASH(type, pty, ptyfs_used_mask)];
  373         LIST_FOREACH(pp, ppp, ptyfs_hash) {
  374                 vp = PTYFSTOV(pp);
  375                 if (pty == pp->ptyfs_pty && pp->ptyfs_type == type &&
  376                     vp->v_mount == mp) {
  377                         simple_lock(&vp->v_interlock);
  378                         simple_unlock(&ptyfs_used_slock);
  379                         if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
  380                                 goto loop;
  381                         return vp;
  382                 }
  383         }
  384         simple_unlock(&ptyfs_used_slock);
  385         return NULL;
  386 }
  387 
  388 /*
  389  * Insert the ptyfsnode into the used table and lock it.
  390  */
  391 static void
  392 ptyfs_hashins(struct ptyfsnode *pp)
  393 {
  394         struct ptyfs_hashhead *ppp;
  395 
  396         /* lock the ptyfsnode, then put it on the appropriate hash list */
  397         lockmgr(&pp->ptyfs_vnode->v_lock, LK_EXCLUSIVE, NULL);
  398 
  399         simple_lock(&ptyfs_used_slock);
  400         ppp = &ptyfs_used_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
  401             ptyfs_used_mask)];
  402         LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
  403         simple_unlock(&ptyfs_used_slock);
  404 }
  405 
  406 /*
  407  * Remove the ptyfsnode from the used table, and add it to the free table
  408  */
  409 static void
  410 ptyfs_hashrem(struct ptyfsnode *pp)
  411 {
  412         struct ptyfs_hashhead *ppp;
  413 
  414         simple_lock(&ptyfs_used_slock);
  415         LIST_REMOVE(pp, ptyfs_hash);
  416         simple_unlock(&ptyfs_used_slock);
  417 
  418         simple_lock(&ptyfs_free_slock);
  419         ppp = &ptyfs_free_tbl[PTYHASH(pp->ptyfs_type, pp->ptyfs_pty,
  420             ptyfs_free_mask)];
  421         LIST_INSERT_HEAD(ppp, pp, ptyfs_hash);
  422         simple_unlock(&ptyfs_free_slock);
  423 }

Cache object: d055a76b00d90951fc7b9245a28baed4


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.