root/miscfs/specfs/spec_subr.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. spec_open_clone
  2. spec_close_clone

    1 /*      $OpenBSD: spec_subr.c,v 1.2 2006/06/02 20:49:08 pedro Exp $     */
    2 
    3 /*
    4  * Copyright (c) 2006 Pedro Martelletto <pedro@openbsd.org>
    5  * Copyright (c) 2006 Thordur Bjornsson <thib@openbsd.org>
    6  *
    7  * Permission to use, copy, modify, and distribute this software for any
    8  * purpose with or without fee is hereby granted, provided that the above
    9  * copyright notice and this permission notice appear in all copies.
   10  *
   11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   18  */
   19 
   20 #include <sys/param.h>
   21 #include <sys/vnode.h>
   22 #include <sys/malloc.h>
   23 #include <sys/conf.h>
   24 #include <sys/stat.h>
   25 
   26 #include <miscfs/specfs/specdev.h>
   27 
   28 int
   29 spec_open_clone(struct vop_open_args *ap)
   30 {
   31         struct vnode *cvp, *vp = ap->a_vp;
   32         struct cloneinfo *cip;
   33         int error, i;
   34 
   35         for (i = 1; i < sizeof(vp->v_specbitmap) * NBBY; i++)
   36                 if (isclr(vp->v_specbitmap, i)) {
   37                         setbit(vp->v_specbitmap, i);
   38                         break;
   39                 }
   40 
   41         if (i == sizeof(vp->v_specbitmap) * NBBY)
   42                 return (EBUSY); /* too many open instances */
   43 
   44         printf("spec_open_clone(): cloning device (%d, %d) for pid %u\n",
   45             major(vp->v_rdev), minor(vp->v_rdev), curproc->p_pid);
   46 
   47         error = cdevvp(makedev(major(vp->v_rdev), i), &cvp);
   48         if (error)
   49                 return (error); /* out of vnodes */
   50 
   51         VOP_UNLOCK(vp, 0, ap->a_p);
   52 
   53         error = cdevsw[major(vp->v_rdev)].d_open(cvp->v_rdev, ap->a_mode,
   54             S_IFCHR, ap->a_p);
   55 
   56         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_p);
   57 
   58         if (error) {
   59                  clrbit(vp->v_specbitmap, i);
   60                  return (error); /* device open failed */
   61         }
   62 
   63         cip = malloc(sizeof(struct cloneinfo), M_TEMP, M_WAITOK);
   64         cip->ci_data = vp->v_data;
   65         cip->ci_vp = cvp;
   66 
   67         cvp->v_specparent = vp;
   68         vp->v_flag |= VCLONED;
   69         vp->v_data = cip;
   70 
   71         printf("spec_open_clone(): new minor for cloned device is %d\n",
   72             minor(cvp->v_rdev));
   73 
   74         return (0); /* device cloned */
   75 }
   76 
   77 int
   78 spec_close_clone(struct vop_close_args *ap)
   79 {
   80         struct vnode *pvp, *vp = ap->a_vp;
   81         int error;
   82 
   83         error = cdevsw[major(vp->v_rdev)].d_close(vp->v_rdev, ap->a_fflag,
   84             S_IFCHR, ap->a_p);
   85         if (error)
   86                 return (error); /* device close failed */
   87 
   88         pvp = vp->v_specparent; /* get parent device */
   89         clrbit(pvp->v_specbitmap, minor(vp->v_rdev));
   90 
   91         printf("spec_close_clone(): freeing minor %d of dev %d for"
   92             " pid %u\n", minor(vp->v_rdev), major(vp->v_rdev), curproc->p_pid);
   93 
   94         return (0); /* clone closed */
   95 }

/* [<][>][^][v][top][bottom][index][help] */