Home | History | Annotate | Line # | Download | only in putter
putter.c revision 1.1
      1  1.1  pooka /*	$NetBSD: putter.c,v 1.1 2007/11/12 14:30:56 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2006, 2007  Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Development of this software was supported by the
      7  1.1  pooka  * Ulla Tuominen Foundation and the Finnish Cultural Foundation and the
      8  1.1  pooka  * Research Foundation of Helsinki University of Technology
      9  1.1  pooka  *
     10  1.1  pooka  * Redistribution and use in source and binary forms, with or without
     11  1.1  pooka  * modification, are permitted provided that the following conditions
     12  1.1  pooka  * are met:
     13  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     14  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     15  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     18  1.1  pooka  *
     19  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  pooka  * SUCH DAMAGE.
     30  1.1  pooka  */
     31  1.1  pooka 
     32  1.1  pooka /*
     33  1.1  pooka  * Pass-to-Userspace TransporTER: generic kernel-user request-response
     34  1.1  pooka  * transport interface.
     35  1.1  pooka  */
     36  1.1  pooka 
     37  1.1  pooka #include <sys/cdefs.h>
     38  1.1  pooka __KERNEL_RCSID(0, "$NetBSD: putter.c,v 1.1 2007/11/12 14:30:56 pooka Exp $");
     39  1.1  pooka 
     40  1.1  pooka #include <sys/param.h>
     41  1.1  pooka #include <sys/conf.h>
     42  1.1  pooka #include <sys/file.h>
     43  1.1  pooka #include <sys/filedesc.h>
     44  1.1  pooka #include <sys/kmem.h>
     45  1.1  pooka #include <sys/poll.h>
     46  1.1  pooka #include <sys/socketvar.h>
     47  1.1  pooka 
     48  1.1  pooka #include <dev/putter/puttervar.h>
     49  1.1  pooka 
     50  1.1  pooka #include <fs/puffs/puffs_msgif.h> /* XXX: for frame headers, goes away soon */
     51  1.1  pooka 
     52  1.1  pooka /*
     53  1.1  pooka  * putter instance structures.  these are always allocated and freed
     54  1.1  pooka  * from the context of the transport user.
     55  1.1  pooka  */
     56  1.1  pooka struct putter_instance {
     57  1.1  pooka 	pid_t			pi_pid;
     58  1.1  pooka 	int			pi_idx;
     59  1.1  pooka 	int			pi_fd;
     60  1.1  pooka 	struct selinfo		pi_sel;
     61  1.1  pooka 
     62  1.1  pooka 	void			*pi_private;
     63  1.1  pooka 	struct putter_ops	*pi_pop;
     64  1.1  pooka 
     65  1.1  pooka 	uint8_t			*pi_curput;
     66  1.1  pooka 	size_t			pi_curres;
     67  1.1  pooka 	void			*pi_curopaq;
     68  1.1  pooka 
     69  1.1  pooka 	TAILQ_ENTRY(putter_instance) pi_entries;
     70  1.1  pooka };
     71  1.1  pooka #define PUTTER_EMBRYO ((void *)-1)	/* before attach	*/
     72  1.1  pooka #define PUTTER_DEAD ((void *)-2)	/* after detach		*/
     73  1.1  pooka 
     74  1.1  pooka static TAILQ_HEAD(, putter_instance) putter_ilist
     75  1.1  pooka     = TAILQ_HEAD_INITIALIZER(putter_ilist);
     76  1.1  pooka 
     77  1.1  pooka static int get_pi_idx(struct putter_instance *);
     78  1.1  pooka 
     79  1.1  pooka #ifdef DEBUG
     80  1.1  pooka #ifndef PUTTERDEBUG
     81  1.1  pooka #define PUTTERDEBUG
     82  1.1  pooka #endif
     83  1.1  pooka #endif
     84  1.1  pooka 
     85  1.1  pooka #ifdef PUTTERDEBUG
     86  1.1  pooka static int putterdebug = 0;
     87  1.1  pooka #define DPRINTF(x) if (putterdebug > 0) printf x
     88  1.1  pooka #define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x
     89  1.1  pooka #else
     90  1.1  pooka #define DPRINTF(x)
     91  1.1  pooka #define DPRINTF_VERBOSE(x)
     92  1.1  pooka #endif
     93  1.1  pooka 
     94  1.1  pooka /*
     95  1.1  pooka  * public init / deinit
     96  1.1  pooka  */
     97  1.1  pooka 
     98  1.1  pooka /* protects both the list and the contents of the list elements */
     99  1.1  pooka static kmutex_t pi_mtx;
    100  1.1  pooka 
    101  1.1  pooka void putterattach(void);
    102  1.1  pooka 
    103  1.1  pooka void
    104  1.1  pooka putterattach()
    105  1.1  pooka {
    106  1.1  pooka 
    107  1.1  pooka 	mutex_init(&pi_mtx, MUTEX_DEFAULT, IPL_NONE);
    108  1.1  pooka }
    109  1.1  pooka 
    110  1.1  pooka #if 0
    111  1.1  pooka void
    112  1.1  pooka putter_destroy()
    113  1.1  pooka {
    114  1.1  pooka 
    115  1.1  pooka 	mutex_destroy(&pi_mtx);
    116  1.1  pooka }
    117  1.1  pooka #endif
    118  1.1  pooka 
    119  1.1  pooka /*
    120  1.1  pooka  * fd routines, for cloner
    121  1.1  pooka  */
    122  1.1  pooka static int putter_fop_read(struct file *, off_t *, struct uio *,
    123  1.1  pooka 			   kauth_cred_t, int);
    124  1.1  pooka static int putter_fop_write(struct file *, off_t *, struct uio *,
    125  1.1  pooka 			    kauth_cred_t, int);
    126  1.1  pooka static int putter_fop_ioctl(struct file*, u_long, void *, struct lwp *);
    127  1.1  pooka static int putter_fop_poll(struct file *, int, struct lwp *);
    128  1.1  pooka static int putter_fop_close(struct file *, struct lwp *);
    129  1.1  pooka static int putter_fop_kqfilter(struct file *, struct knote *);
    130  1.1  pooka 
    131  1.1  pooka 
    132  1.1  pooka static const struct fileops putter_fileops = {
    133  1.1  pooka 	putter_fop_read,
    134  1.1  pooka 	putter_fop_write,
    135  1.1  pooka 	putter_fop_ioctl,
    136  1.1  pooka 	fnullop_fcntl,
    137  1.1  pooka 	putter_fop_poll,
    138  1.1  pooka 	fbadop_stat,
    139  1.1  pooka 	putter_fop_close,
    140  1.1  pooka 	putter_fop_kqfilter
    141  1.1  pooka };
    142  1.1  pooka 
    143  1.1  pooka static int
    144  1.1  pooka putter_fop_read(struct file *fp, off_t *off, struct uio *uio,
    145  1.1  pooka 	kauth_cred_t cred, int flags)
    146  1.1  pooka {
    147  1.1  pooka 	struct putter_instance *pi = fp->f_data;
    148  1.1  pooka 	size_t origres, moved;
    149  1.1  pooka 	int error;
    150  1.1  pooka 
    151  1.1  pooka 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
    152  1.1  pooka 		printf("putter_fop_read: private %d not inited\n", pi->pi_idx);
    153  1.1  pooka 		return ENOENT;
    154  1.1  pooka 	}
    155  1.1  pooka 
    156  1.1  pooka 	if (pi->pi_curput == NULL) {
    157  1.1  pooka 		error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid,
    158  1.1  pooka 		    fp->f_flag & O_NONBLOCK, &pi->pi_curput,
    159  1.1  pooka 		    &pi->pi_curres, &pi->pi_curopaq);
    160  1.1  pooka 		if (error)
    161  1.1  pooka 			return error;
    162  1.1  pooka 	}
    163  1.1  pooka 
    164  1.1  pooka 	origres = uio->uio_resid;
    165  1.1  pooka 	error = uiomove(pi->pi_curput, pi->pi_curres, uio);
    166  1.1  pooka 	moved = origres - uio->uio_resid;
    167  1.1  pooka 	DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n",
    168  1.1  pooka 	    pi, moved, pi->pi_curput, error));
    169  1.1  pooka 
    170  1.1  pooka 	KASSERT(pi->pi_curres >= moved);
    171  1.1  pooka 	pi->pi_curres -= moved;
    172  1.1  pooka 	pi->pi_curput += moved;
    173  1.1  pooka 
    174  1.1  pooka 	if (pi->pi_curres == 0) {
    175  1.1  pooka 		pi->pi_pop->pop_releaseout(pi->pi_private,
    176  1.1  pooka 		    pi->pi_curopaq, error);
    177  1.1  pooka 		pi->pi_curput = NULL;
    178  1.1  pooka 	}
    179  1.1  pooka 
    180  1.1  pooka 	return error;
    181  1.1  pooka }
    182  1.1  pooka 
    183  1.1  pooka static int
    184  1.1  pooka putter_fop_write(struct file *fp, off_t *off, struct uio *uio,
    185  1.1  pooka 	kauth_cred_t cred, int flags)
    186  1.1  pooka {
    187  1.1  pooka 	struct putter_instance *pi = fp->f_data;
    188  1.1  pooka 	struct puffs_frame pfr;
    189  1.1  pooka 	uint8_t *buf;
    190  1.1  pooka 	size_t frsize;
    191  1.1  pooka 	int error;
    192  1.1  pooka 
    193  1.1  pooka 	DPRINTF(("puffs_fop_write (%p): writing response, resid %zu\n",
    194  1.1  pooka 	    pi->pi_private, uio->uio_resid));
    195  1.1  pooka 
    196  1.1  pooka 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
    197  1.1  pooka 		printf("putter_fop_write: putter %d not inited\n", pi->pi_idx);
    198  1.1  pooka 		return ENOENT;
    199  1.1  pooka 	}
    200  1.1  pooka 
    201  1.1  pooka 	error = uiomove(&pfr, sizeof(struct puffs_frame), uio);
    202  1.1  pooka 	if (error)
    203  1.1  pooka 		return error;
    204  1.1  pooka 
    205  1.1  pooka 	/* Sorry mate, the kernel doesn't buffer. */
    206  1.1  pooka 	frsize = pfr.pfr_len - sizeof(struct puffs_frame);
    207  1.1  pooka 	if (uio->uio_resid < frsize)
    208  1.1  pooka 		return EINVAL;
    209  1.1  pooka 
    210  1.1  pooka 	buf = kmem_alloc(frsize + sizeof(struct puffs_frame), KM_SLEEP);
    211  1.1  pooka 	memcpy(buf, &pfr, sizeof(pfr));
    212  1.1  pooka 	error = uiomove(buf+sizeof(struct puffs_frame), frsize, uio);
    213  1.1  pooka 	if (error == 0) {
    214  1.1  pooka 		pi->pi_pop->pop_dispatch(pi->pi_private, buf);
    215  1.1  pooka 	}
    216  1.1  pooka 	kmem_free(buf, frsize + sizeof(struct puffs_frame));
    217  1.1  pooka 
    218  1.1  pooka 	return error;
    219  1.1  pooka }
    220  1.1  pooka 
    221  1.1  pooka /*
    222  1.1  pooka  * Poll query interface.  The question is only if an event
    223  1.1  pooka  * can be read from us (and by read I mean ioctl... ugh).
    224  1.1  pooka  */
    225  1.1  pooka #define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)
    226  1.1  pooka static int
    227  1.1  pooka putter_fop_poll(struct file *fp, int events, struct lwp *l)
    228  1.1  pooka {
    229  1.1  pooka 	struct putter_instance *pi = fp->f_data;
    230  1.1  pooka 	int revents;
    231  1.1  pooka 
    232  1.1  pooka 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
    233  1.1  pooka 		printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx);
    234  1.1  pooka 		return ENOENT;
    235  1.1  pooka 	}
    236  1.1  pooka 
    237  1.1  pooka 	revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
    238  1.1  pooka 	if ((events & PUTTERPOLL_EVSET) == 0)
    239  1.1  pooka 		return revents;
    240  1.1  pooka 
    241  1.1  pooka 	/* check queue */
    242  1.1  pooka 	if (pi->pi_pop->pop_waitcount(pi->pi_private))
    243  1.1  pooka 		revents |= PUTTERPOLL_EVSET;
    244  1.1  pooka 	else
    245  1.1  pooka 		selrecord(l, &pi->pi_sel);
    246  1.1  pooka 
    247  1.1  pooka 	return revents;
    248  1.1  pooka }
    249  1.1  pooka 
    250  1.1  pooka /*
    251  1.1  pooka  * device close = forced unmount.
    252  1.1  pooka  *
    253  1.1  pooka  * unmounting is a frightfully complex operation to avoid races
    254  1.1  pooka  */
    255  1.1  pooka static int
    256  1.1  pooka putter_fop_close(struct file *fp, struct lwp *l)
    257  1.1  pooka {
    258  1.1  pooka 	struct putter_instance *pi = fp->f_data;
    259  1.1  pooka 	int rv;
    260  1.1  pooka 
    261  1.1  pooka 	DPRINTF(("putter_fop_close: device closed\n"));
    262  1.1  pooka 
    263  1.1  pooka  restart:
    264  1.1  pooka 	mutex_enter(&pi_mtx);
    265  1.1  pooka 	/*
    266  1.1  pooka 	 * First check if the fs was never mounted.  In that case
    267  1.1  pooka 	 * remove the instance from the list.  If mount is attempted later,
    268  1.1  pooka 	 * it will simply fail.
    269  1.1  pooka 	 */
    270  1.1  pooka 	if (pi->pi_private == PUTTER_EMBRYO) {
    271  1.1  pooka 		TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
    272  1.1  pooka 		mutex_exit(&pi_mtx);
    273  1.1  pooka 
    274  1.1  pooka 		DPRINTF(("putter_fop_close: data associated with fp %p was "
    275  1.1  pooka 		    "embryonic\n", fp));
    276  1.1  pooka 
    277  1.1  pooka 		goto out;
    278  1.1  pooka 	}
    279  1.1  pooka 
    280  1.1  pooka 	/*
    281  1.1  pooka 	 * Next, analyze if unmount was called and the instance is dead.
    282  1.1  pooka 	 * In this case we can just free the structure and go home, it
    283  1.1  pooka 	 * was removed from the list by putter_rmprivate().
    284  1.1  pooka 	 */
    285  1.1  pooka 	if (pi->pi_private == PUTTER_DEAD) {
    286  1.1  pooka 		mutex_exit(&pi_mtx);
    287  1.1  pooka 
    288  1.1  pooka 		DPRINTF(("putter_fop_close: putter associated with fp %p (%d) "
    289  1.1  pooka 		    "dead, freeing\n", fp, pi->pi_idx));
    290  1.1  pooka 
    291  1.1  pooka 		goto out;
    292  1.1  pooka 	}
    293  1.1  pooka 
    294  1.1  pooka 	/*
    295  1.1  pooka 	 * So we have a reference.  Proceed to unwrap the file system.
    296  1.1  pooka 	 */
    297  1.1  pooka 	mutex_exit(&pi_mtx);
    298  1.1  pooka 
    299  1.1  pooka 	/* hmm?  suspicious locking? */
    300  1.1  pooka 	while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
    301  1.1  pooka 		goto restart;
    302  1.1  pooka 
    303  1.1  pooka  out:
    304  1.1  pooka 	/*
    305  1.1  pooka 	 * Finally, release the instance information.  It was already
    306  1.1  pooka 	 * removed from the list by putter_rmprivate() and we know it's
    307  1.1  pooka 	 * dead, so no need to lock.
    308  1.1  pooka 	 */
    309  1.1  pooka 	kmem_free(pi, sizeof(struct putter_instance));
    310  1.1  pooka 
    311  1.1  pooka 	return 0;
    312  1.1  pooka }
    313  1.1  pooka 
    314  1.1  pooka static int
    315  1.1  pooka putter_fop_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
    316  1.1  pooka {
    317  1.1  pooka 
    318  1.1  pooka 	/*
    319  1.1  pooka 	 * work already done in sys_ioctl().  skip sanity checks to enable
    320  1.1  pooka 	 * setting non-blocking fd without yet having mounted the fs
    321  1.1  pooka 	 */
    322  1.1  pooka 	if (cmd == FIONBIO)
    323  1.1  pooka 		return 0;
    324  1.1  pooka 
    325  1.1  pooka 	return EINVAL;
    326  1.1  pooka }
    327  1.1  pooka 
    328  1.1  pooka /* kqueue stuff */
    329  1.1  pooka 
    330  1.1  pooka static void
    331  1.1  pooka filt_putterdetach(struct knote *kn)
    332  1.1  pooka {
    333  1.1  pooka 	struct putter_instance *pi = kn->kn_hook;
    334  1.1  pooka 
    335  1.1  pooka 	mutex_enter(&pi_mtx);
    336  1.1  pooka 	SLIST_REMOVE(&pi->pi_sel.sel_klist, kn, knote, kn_selnext);
    337  1.1  pooka 	mutex_exit(&pi_mtx);
    338  1.1  pooka }
    339  1.1  pooka 
    340  1.1  pooka static int
    341  1.1  pooka filt_putterioctl(struct knote *kn, long hint)
    342  1.1  pooka {
    343  1.1  pooka 	struct putter_instance *pi = kn->kn_hook;
    344  1.1  pooka 	int error;
    345  1.1  pooka 
    346  1.1  pooka 	error = 0;
    347  1.1  pooka 	mutex_enter(&pi_mtx);
    348  1.1  pooka 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
    349  1.1  pooka 		error = 1;
    350  1.1  pooka 	mutex_exit(&pi_mtx);
    351  1.1  pooka 	if (error)
    352  1.1  pooka 		return 0;
    353  1.1  pooka 
    354  1.1  pooka 	kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
    355  1.1  pooka 
    356  1.1  pooka 	return kn->kn_data != 0;
    357  1.1  pooka }
    358  1.1  pooka 
    359  1.1  pooka static const struct filterops putterioctl_filtops =
    360  1.1  pooka 	{ 1, NULL, filt_putterdetach, filt_putterioctl };
    361  1.1  pooka 
    362  1.1  pooka static int
    363  1.1  pooka putter_fop_kqfilter(struct file *fp, struct knote *kn)
    364  1.1  pooka {
    365  1.1  pooka 	struct putter_instance *pi = fp->f_data;
    366  1.1  pooka 	struct klist *klist;
    367  1.1  pooka 
    368  1.1  pooka 	if (kn->kn_filter != EVFILT_READ)
    369  1.1  pooka 		return 1;
    370  1.1  pooka 
    371  1.1  pooka 	klist = &pi->pi_sel.sel_klist;
    372  1.1  pooka 	kn->kn_fop = &putterioctl_filtops;
    373  1.1  pooka 	kn->kn_hook = pi;
    374  1.1  pooka 
    375  1.1  pooka 	mutex_enter(&pi_mtx);
    376  1.1  pooka 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    377  1.1  pooka 	mutex_exit(&pi_mtx);
    378  1.1  pooka 
    379  1.1  pooka 	return 0;
    380  1.1  pooka }
    381  1.1  pooka 
    382  1.1  pooka 
    383  1.1  pooka /*
    384  1.1  pooka  * Device routines.  These are for when /dev/puffs is initially
    385  1.1  pooka  * opened before it has been cloned.
    386  1.1  pooka  */
    387  1.1  pooka 
    388  1.1  pooka dev_type_open(puttercdopen);
    389  1.1  pooka dev_type_close(puttercdclose);
    390  1.1  pooka dev_type_ioctl(puttercdioctl);
    391  1.1  pooka 
    392  1.1  pooka /* dev */
    393  1.1  pooka const struct cdevsw putter_cdevsw = {
    394  1.1  pooka 	puttercdopen,	puttercdclose,	noread,		nowrite,
    395  1.1  pooka 	noioctl,	nostop,		notty,		nopoll,
    396  1.1  pooka 	nommap,		nokqfilter,	D_OTHER
    397  1.1  pooka };
    398  1.1  pooka int
    399  1.1  pooka puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l)
    400  1.1  pooka {
    401  1.1  pooka 	struct putter_instance *pi;
    402  1.1  pooka 	struct file *fp;
    403  1.1  pooka 	int error, fd, idx;
    404  1.1  pooka 
    405  1.1  pooka 	/*
    406  1.1  pooka 	 * XXX: decide on some security model and check permissions
    407  1.1  pooka 	 */
    408  1.1  pooka 
    409  1.1  pooka 	if (minor(dev) != PUFFS_CLONER)
    410  1.1  pooka 		return ENXIO;
    411  1.1  pooka 
    412  1.1  pooka 	if ((error = falloc(l, &fp, &fd)) != 0)
    413  1.1  pooka 		return error;
    414  1.1  pooka 
    415  1.1  pooka 	pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP);
    416  1.1  pooka 
    417  1.1  pooka 	mutex_enter(&pi_mtx);
    418  1.1  pooka 	idx = get_pi_idx(pi);
    419  1.1  pooka 	if (idx == PUFFS_CLONER) {
    420  1.1  pooka 		mutex_exit(&pi_mtx);
    421  1.1  pooka 		kmem_free(pi, sizeof(struct putter_instance));
    422  1.1  pooka 		FILE_UNUSE(fp, l);
    423  1.1  pooka 		ffree(fp);
    424  1.1  pooka 		return EBUSY;
    425  1.1  pooka 	}
    426  1.1  pooka 
    427  1.1  pooka 	pi->pi_pid = l->l_proc->p_pid;
    428  1.1  pooka 	pi->pi_idx = idx;
    429  1.1  pooka 	selinit(&pi->pi_sel);
    430  1.1  pooka 	pi->pi_curput = NULL;
    431  1.1  pooka 	pi->pi_curres = 0;
    432  1.1  pooka 	pi->pi_curopaq = NULL;
    433  1.1  pooka 	mutex_exit(&pi_mtx);
    434  1.1  pooka 
    435  1.1  pooka 	DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
    436  1.1  pooka 	    pi->pi_pid));
    437  1.1  pooka 
    438  1.1  pooka 	return fdclone(l, fp, fd, FREAD|FWRITE, &putter_fileops, pi);
    439  1.1  pooka }
    440  1.1  pooka 
    441  1.1  pooka int
    442  1.1  pooka puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l)
    443  1.1  pooka {
    444  1.1  pooka 
    445  1.1  pooka 	panic("puttercdclose impossible\n");
    446  1.1  pooka 
    447  1.1  pooka 	return 0;
    448  1.1  pooka }
    449  1.1  pooka 
    450  1.1  pooka 
    451  1.1  pooka /*
    452  1.1  pooka  * Set the private structure for the file descriptor.  This is
    453  1.1  pooka  * typically done immediately when the counterpart has knowledge
    454  1.1  pooka  * about the private structure's address and the file descriptor
    455  1.1  pooka  * (e.g. vfs mount routine).
    456  1.1  pooka  *
    457  1.1  pooka  * We only want to make sure that the caller had the right to open the
    458  1.1  pooka  * device, we don't so much care about which context it gets in case
    459  1.1  pooka  * the same process opened multiple (since they are equal at this point).
    460  1.1  pooka  */
    461  1.1  pooka struct putter_instance *
    462  1.1  pooka putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop)
    463  1.1  pooka {
    464  1.1  pooka 	struct putter_instance *pi = NULL;
    465  1.1  pooka 
    466  1.1  pooka 	mutex_enter(&pi_mtx);
    467  1.1  pooka 	TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
    468  1.1  pooka 		if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) {
    469  1.1  pooka 			pi->pi_private = ppriv;
    470  1.1  pooka 			pi->pi_fd = fd;
    471  1.1  pooka 			pi->pi_pop = pop;
    472  1.1  pooka 			break;
    473  1.1  pooka 		    }
    474  1.1  pooka 	}
    475  1.1  pooka 	mutex_exit(&pi_mtx);
    476  1.1  pooka 
    477  1.1  pooka 	DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
    478  1.1  pooka 	    pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0));
    479  1.1  pooka 
    480  1.1  pooka 	return pi;
    481  1.1  pooka }
    482  1.1  pooka 
    483  1.1  pooka /*
    484  1.1  pooka  * Remove fp <-> private mapping.
    485  1.1  pooka  */
    486  1.1  pooka void
    487  1.1  pooka putter_detach(struct putter_instance *pi)
    488  1.1  pooka {
    489  1.1  pooka 
    490  1.1  pooka 	mutex_enter(&pi_mtx);
    491  1.1  pooka 	TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
    492  1.1  pooka 	pi->pi_private = PUTTER_DEAD;
    493  1.1  pooka 	mutex_exit(&pi_mtx);
    494  1.1  pooka 
    495  1.1  pooka 	DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
    496  1.1  pooka }
    497  1.1  pooka 
    498  1.1  pooka void
    499  1.1  pooka putter_notify(struct putter_instance *pi)
    500  1.1  pooka {
    501  1.1  pooka 
    502  1.1  pooka 	selnotify(&pi->pi_sel, 0);
    503  1.1  pooka }
    504  1.1  pooka 
    505  1.1  pooka /* search sorted list of instances for free minor, sorted insert arg */
    506  1.1  pooka static int
    507  1.1  pooka get_pi_idx(struct putter_instance *pi_i)
    508  1.1  pooka {
    509  1.1  pooka 	struct putter_instance *pi;
    510  1.1  pooka 	int i;
    511  1.1  pooka 
    512  1.1  pooka 	i = 0;
    513  1.1  pooka 	TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
    514  1.1  pooka 		if (i == PUFFS_CLONER)
    515  1.1  pooka 			return PUFFS_CLONER;
    516  1.1  pooka 		if (i != pi->pi_idx)
    517  1.1  pooka 			break;
    518  1.1  pooka 		i++;
    519  1.1  pooka 	}
    520  1.1  pooka 
    521  1.1  pooka 	pi_i->pi_private = PUTTER_EMBRYO;
    522  1.1  pooka 
    523  1.1  pooka 	if (pi == NULL)
    524  1.1  pooka 		TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries);
    525  1.1  pooka 	else
    526  1.1  pooka 		TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries);
    527  1.1  pooka 
    528  1.1  pooka 	return i;
    529  1.1  pooka }
    530