Home | History | Annotate | Line # | Download | only in dmover
dmover_io.c revision 1.32
      1  1.32        ad /*	$NetBSD: dmover_io.c,v 1.32 2009/04/04 10:12:51 ad Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4  1.12   thorpej  * Copyright (c) 2002, 2003 Wasabi Systems, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8   1.1   thorpej  *
      9   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10   1.1   thorpej  * modification, are permitted provided that the following conditions
     11   1.1   thorpej  * are met:
     12   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18   1.1   thorpej  *    must display the following acknowledgement:
     19   1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     20   1.1   thorpej  *	Wasabi Systems, Inc.
     21   1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1   thorpej  *    or promote products derived from this software without specific prior
     23   1.1   thorpej  *    written permission.
     24   1.1   thorpej  *
     25   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1   thorpej  */
     37   1.1   thorpej 
     38   1.1   thorpej /*
     39   1.1   thorpej  * dmover_io.c: Support for user-space access to dmover-api
     40   1.1   thorpej  *
     41   1.1   thorpej  * This interface is quite simple:
     42   1.1   thorpej  *
     43   1.1   thorpej  *	1.  The user opens /dev/dmover, which is a cloning device.  This
     44   1.1   thorpej  *	    allocates internal state for the session.
     45   1.1   thorpej  *
     46   1.1   thorpej  *	2.  The user does a DMIO_SETFUNC to select the data movement
     47   1.1   thorpej  *	    function.  This actually creates the dmover session.
     48   1.1   thorpej  *
     49   1.1   thorpej  *	3.  The user writes request messages to its dmover handle.
     50   1.1   thorpej  *
     51   1.1   thorpej  *	4.  The user reads request responses from its dmover handle.
     52   1.1   thorpej  *
     53   1.1   thorpej  *	5.  The user closes the file descriptor and the session is
     54   1.1   thorpej  *	    torn down.
     55   1.1   thorpej  */
     56   1.1   thorpej 
     57   1.1   thorpej #include <sys/cdefs.h>
     58  1.32        ad __KERNEL_RCSID(0, "$NetBSD: dmover_io.c,v 1.32 2009/04/04 10:12:51 ad Exp $");
     59   1.1   thorpej 
     60   1.1   thorpej #include <sys/param.h>
     61   1.1   thorpej #include <sys/queue.h>
     62   1.1   thorpej #include <sys/conf.h>
     63   1.1   thorpej #include <sys/pool.h>
     64   1.1   thorpej #include <sys/proc.h>
     65   1.1   thorpej #include <sys/poll.h>
     66   1.1   thorpej #include <sys/malloc.h>
     67  1.29        ad #include <sys/simplelock.h>
     68   1.1   thorpej #include <sys/file.h>
     69   1.1   thorpej #include <sys/filedesc.h>
     70   1.1   thorpej #include <sys/filio.h>
     71   1.1   thorpej #include <sys/select.h>
     72   1.1   thorpej #include <sys/systm.h>
     73  1.21      yamt #include <sys/workqueue.h>
     74  1.21      yamt #include <sys/once.h>
     75  1.21      yamt 
     76  1.21      yamt #include <uvm/uvm_extern.h>
     77   1.1   thorpej 
     78   1.1   thorpej #include <dev/dmover/dmovervar.h>
     79   1.1   thorpej #include <dev/dmover/dmover_io.h>
     80   1.1   thorpej 
     81   1.1   thorpej struct dmio_usrreq_state {
     82  1.21      yamt 	union {
     83  1.21      yamt 		struct work u_work;
     84  1.21      yamt 		TAILQ_ENTRY(dmio_usrreq_state) u_q;
     85  1.21      yamt 	} dus_u;
     86  1.21      yamt #define	dus_q		dus_u.u_q
     87  1.21      yamt #define	dus_work	dus_u.u_work
     88   1.1   thorpej 	struct uio dus_uio_out;
     89   1.1   thorpej 	struct uio *dus_uio_in;
     90   1.1   thorpej 	struct dmover_request *dus_req;
     91   1.1   thorpej 	uint32_t dus_id;
     92  1.21      yamt 	struct vmspace *dus_vmspace;
     93   1.1   thorpej };
     94   1.1   thorpej 
     95   1.1   thorpej struct dmio_state {
     96   1.1   thorpej 	struct dmover_session *ds_session;
     97   1.1   thorpej 	TAILQ_HEAD(, dmio_usrreq_state) ds_pending;
     98   1.1   thorpej 	TAILQ_HEAD(, dmio_usrreq_state) ds_complete;
     99   1.1   thorpej 	struct selinfo ds_selq;
    100  1.19     perry 	volatile int ds_flags;
    101   1.1   thorpej 	u_int ds_nreqs;
    102   1.1   thorpej 	struct simplelock ds_slock;
    103   1.1   thorpej };
    104   1.1   thorpej 
    105  1.21      yamt static ONCE_DECL(dmio_cleaner_control);
    106  1.21      yamt static struct workqueue *dmio_cleaner;
    107  1.21      yamt static int dmio_cleaner_init(void);
    108  1.21      yamt static void dmio_usrreq_fini1(struct work *wk, void *);
    109  1.21      yamt 
    110   1.1   thorpej #define	DMIO_STATE_SEL		0x0001
    111   1.1   thorpej #define	DMIO_STATE_DEAD		0x0002
    112   1.1   thorpej #define	DMIO_STATE_LARVAL	0x0004
    113   1.1   thorpej #define	DMIO_STATE_READ_WAIT	0x0008
    114   1.1   thorpej #define	DMIO_STATE_WRITE_WAIT	0x0010
    115   1.1   thorpej 
    116   1.1   thorpej #define	DMIO_NREQS_MAX		64	/* XXX pulled out of a hat */
    117   1.1   thorpej 
    118   1.1   thorpej struct pool dmio_state_pool;
    119   1.1   thorpej struct pool dmio_usrreq_state_pool;
    120   1.1   thorpej 
    121   1.1   thorpej void	dmoverioattach(int);
    122   1.5   gehenna 
    123   1.5   gehenna dev_type_open(dmoverioopen);
    124   1.5   gehenna 
    125   1.5   gehenna const struct cdevsw dmoverio_cdevsw = {
    126   1.5   gehenna 	dmoverioopen, noclose, noread, nowrite, noioctl,
    127   1.6  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
    128  1.25      cube 	D_OTHER
    129   1.5   gehenna };
    130   1.1   thorpej 
    131   1.1   thorpej /*
    132   1.1   thorpej  * dmoverioattach:
    133   1.1   thorpej  *
    134   1.1   thorpej  *	Pseudo-device attach routine.
    135   1.1   thorpej  */
    136   1.1   thorpej void
    137   1.1   thorpej dmoverioattach(int count)
    138   1.1   thorpej {
    139   1.1   thorpej 
    140   1.1   thorpej 	pool_init(&dmio_state_pool, sizeof(struct dmio_state),
    141  1.26        ad 	    0, 0, 0, "dmiostate", NULL, IPL_SOFTCLOCK);
    142   1.1   thorpej 	pool_init(&dmio_usrreq_state_pool, sizeof(struct dmio_usrreq_state),
    143  1.26        ad 	    0, 0, 0, "dmiourstate", NULL, IPL_SOFTCLOCK);
    144   1.1   thorpej }
    145   1.1   thorpej 
    146   1.1   thorpej /*
    147  1.21      yamt  * dmio_cleaner_init:
    148  1.21      yamt  *
    149  1.21      yamt  *	Create cleaner thread.
    150  1.21      yamt  */
    151  1.21      yamt static int
    152  1.21      yamt dmio_cleaner_init(void)
    153  1.21      yamt {
    154  1.21      yamt 
    155  1.21      yamt 	return workqueue_create(&dmio_cleaner, "dmioclean", dmio_usrreq_fini1,
    156  1.24      yamt 	    NULL, PWAIT, IPL_SOFTCLOCK, 0);
    157  1.21      yamt }
    158  1.21      yamt 
    159  1.21      yamt /*
    160   1.1   thorpej  * dmio_usrreq_init:
    161   1.1   thorpej  *
    162   1.1   thorpej  *	Build a request structure.
    163   1.1   thorpej  */
    164   1.1   thorpej static int
    165   1.1   thorpej dmio_usrreq_init(struct file *fp, struct dmio_usrreq_state *dus,
    166   1.1   thorpej     struct dmio_usrreq *req, struct dmover_request *dreq)
    167   1.1   thorpej {
    168   1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    169   1.1   thorpej 	struct dmover_session *dses = ds->ds_session;
    170   1.1   thorpej 	struct uio *uio_out = &dus->dus_uio_out;
    171   1.1   thorpej 	struct uio *uio_in;
    172   1.1   thorpej 	dmio_buffer inbuf;
    173   1.1   thorpej 	size_t len;
    174   1.7   thorpej 	int i, error;
    175   1.7   thorpej 	u_int j;
    176   1.1   thorpej 
    177   1.1   thorpej 	/* XXX How should malloc interact w/ FNONBLOCK? */
    178   1.1   thorpej 
    179  1.21      yamt 	error = RUN_ONCE(&dmio_cleaner_control, dmio_cleaner_init);
    180  1.21      yamt 	if (error) {
    181  1.21      yamt 		return error;
    182  1.21      yamt 	}
    183  1.21      yamt 
    184  1.21      yamt 	error = proc_vmspace_getref(curproc, &dus->dus_vmspace);
    185  1.21      yamt 	if (error) {
    186  1.21      yamt 		return error;
    187  1.21      yamt 	}
    188  1.21      yamt 
    189  1.12   thorpej 	if (req->req_outbuf.dmbuf_iovcnt != 0) {
    190  1.12   thorpej 		if (req->req_outbuf.dmbuf_iovcnt > IOV_MAX)
    191  1.12   thorpej 			return (EINVAL);
    192  1.12   thorpej 		len = sizeof(struct iovec) * req->req_outbuf.dmbuf_iovcnt;
    193  1.12   thorpej 		uio_out->uio_iov = malloc(len, M_TEMP, M_WAITOK);
    194  1.12   thorpej 		error = copyin(req->req_outbuf.dmbuf_iov, uio_out->uio_iov,
    195  1.12   thorpej 		    len);
    196  1.12   thorpej 		if (error) {
    197  1.12   thorpej 			free(uio_out->uio_iov, M_TEMP);
    198  1.12   thorpej 			return (error);
    199  1.12   thorpej 		}
    200   1.1   thorpej 
    201  1.12   thorpej 		for (j = 0, len = 0; j < req->req_outbuf.dmbuf_iovcnt; j++) {
    202  1.12   thorpej 			len += uio_out->uio_iov[j].iov_len;
    203  1.12   thorpej 			if (len > SSIZE_MAX) {
    204  1.12   thorpej 				free(uio_out->uio_iov, M_TEMP);
    205  1.12   thorpej 				return (EINVAL);
    206  1.12   thorpej 			}
    207  1.12   thorpej 		}
    208   1.1   thorpej 
    209  1.12   thorpej 		uio_out->uio_iovcnt = req->req_outbuf.dmbuf_iovcnt;
    210  1.12   thorpej 		uio_out->uio_resid = len;
    211  1.12   thorpej 		uio_out->uio_rw = UIO_READ;
    212  1.21      yamt 		uio_out->uio_vmspace = dus->dus_vmspace;
    213  1.21      yamt 
    214  1.12   thorpej 		dreq->dreq_outbuf_type = DMOVER_BUF_UIO;
    215  1.12   thorpej 		dreq->dreq_outbuf.dmbuf_uio = uio_out;
    216  1.12   thorpej 	} else {
    217  1.12   thorpej 		uio_out->uio_iov = NULL;
    218  1.12   thorpej 		uio_out = NULL;
    219  1.12   thorpej 		dreq->dreq_outbuf_type = DMOVER_BUF_NONE;
    220   1.1   thorpej 	}
    221   1.1   thorpej 
    222  1.12   thorpej 	memcpy(dreq->dreq_immediate, req->req_immediate,
    223  1.12   thorpej 	    sizeof(dreq->dreq_immediate));
    224   1.1   thorpej 
    225   1.1   thorpej 	if (dses->dses_ninputs == 0) {
    226  1.12   thorpej 		/* No inputs; all done. */
    227   1.1   thorpej 		return (0);
    228   1.1   thorpej 	}
    229   1.1   thorpej 
    230   1.1   thorpej 	dreq->dreq_inbuf_type = DMOVER_BUF_UIO;
    231   1.1   thorpej 
    232   1.1   thorpej 	dus->dus_uio_in = malloc(sizeof(struct uio) * dses->dses_ninputs,
    233   1.1   thorpej 	    M_TEMP, M_WAITOK);
    234   1.1   thorpej 	memset(dus->dus_uio_in, 0, sizeof(struct uio) * dses->dses_ninputs);
    235   1.1   thorpej 
    236   1.1   thorpej 	for (i = 0; i < dses->dses_ninputs; i++) {
    237   1.1   thorpej 		uio_in = &dus->dus_uio_in[i];
    238   1.1   thorpej 
    239   1.1   thorpej 		error = copyin(&req->req_inbuf[i], &inbuf, sizeof(inbuf));
    240   1.1   thorpej 		if (error)
    241   1.1   thorpej 			goto bad;
    242   1.1   thorpej 
    243   1.1   thorpej 		if (inbuf.dmbuf_iovcnt > IOV_MAX) {
    244   1.1   thorpej 			error = EINVAL;
    245   1.1   thorpej 			goto bad;
    246   1.1   thorpej 		}
    247   1.1   thorpej 		len = sizeof(struct iovec) * inbuf.dmbuf_iovcnt;
    248   1.1   thorpej 		if (len == 0) {
    249   1.1   thorpej 			error = EINVAL;
    250   1.1   thorpej 			goto bad;
    251   1.1   thorpej 		}
    252   1.1   thorpej 		uio_in->uio_iov = malloc(len, M_TEMP, M_WAITOK);
    253   1.1   thorpej 
    254   1.1   thorpej 		error = copyin(inbuf.dmbuf_iov, uio_in->uio_iov, len);
    255   1.1   thorpej 		if (error) {
    256   1.1   thorpej 			free(uio_in->uio_iov, M_TEMP);
    257   1.1   thorpej 			goto bad;
    258   1.1   thorpej 		}
    259   1.1   thorpej 
    260  1.12   thorpej 		for (j = 0, len = 0; j < inbuf.dmbuf_iovcnt; j++) {
    261   1.1   thorpej 			len += uio_in->uio_iov[j].iov_len;
    262   1.1   thorpej 			if (len > SSIZE_MAX) {
    263   1.1   thorpej 				free(uio_in->uio_iov, M_TEMP);
    264   1.1   thorpej 				error = EINVAL;
    265   1.1   thorpej 				goto bad;
    266   1.1   thorpej 			}
    267   1.1   thorpej 		}
    268   1.1   thorpej 
    269  1.12   thorpej 		if (uio_out != NULL && len != uio_out->uio_resid) {
    270   1.1   thorpej 			free(uio_in->uio_iov, M_TEMP);
    271   1.1   thorpej 			error = EINVAL;
    272   1.1   thorpej 			goto bad;
    273   1.1   thorpej 		}
    274   1.1   thorpej 
    275   1.1   thorpej 		uio_in->uio_iovcnt = inbuf.dmbuf_iovcnt;
    276   1.1   thorpej 		uio_in->uio_resid = len;
    277   1.1   thorpej 		uio_in->uio_rw = UIO_WRITE;
    278  1.21      yamt 		uio_in->uio_vmspace = dus->dus_vmspace;
    279   1.1   thorpej 
    280   1.1   thorpej 		dreq->dreq_inbuf[i].dmbuf_uio = uio_in;
    281   1.1   thorpej 	}
    282   1.1   thorpej 
    283   1.1   thorpej 	return (0);
    284   1.1   thorpej 
    285   1.1   thorpej  bad:
    286   1.1   thorpej 	if (i > 0) {
    287   1.1   thorpej 		for (--i; i >= 0; i--) {
    288   1.1   thorpej 			uio_in = &dus->dus_uio_in[i];
    289   1.1   thorpej 			free(uio_in->uio_iov, M_TEMP);
    290   1.1   thorpej 		}
    291   1.1   thorpej 	}
    292   1.1   thorpej 	free(dus->dus_uio_in, M_TEMP);
    293  1.12   thorpej 	if (uio_out != NULL)
    294  1.12   thorpej 		free(uio_out->uio_iov, M_TEMP);
    295  1.21      yamt 	uvmspace_free(dus->dus_vmspace);
    296   1.1   thorpej 	return (error);
    297   1.1   thorpej }
    298   1.1   thorpej 
    299   1.1   thorpej /*
    300   1.1   thorpej  * dmio_usrreq_fini:
    301   1.1   thorpej  *
    302   1.1   thorpej  *	Tear down a request.  Must be called at splsoftclock().
    303   1.1   thorpej  */
    304   1.1   thorpej static void
    305   1.1   thorpej dmio_usrreq_fini(struct dmio_state *ds, struct dmio_usrreq_state *dus)
    306   1.1   thorpej {
    307   1.1   thorpej 	struct dmover_session *dses = ds->ds_session;
    308   1.1   thorpej 	struct uio *uio_out = &dus->dus_uio_out;
    309   1.1   thorpej 	struct uio *uio_in;
    310   1.1   thorpej 	int i;
    311   1.1   thorpej 
    312  1.12   thorpej 	if (uio_out->uio_iov != NULL)
    313  1.12   thorpej 		free(uio_out->uio_iov, M_TEMP);
    314   1.1   thorpej 
    315  1.21      yamt 	if (dses->dses_ninputs) {
    316  1.21      yamt 		for (i = 0; i < dses->dses_ninputs; i++) {
    317  1.21      yamt 			uio_in = &dus->dus_uio_in[i];
    318  1.21      yamt 			free(uio_in->uio_iov, M_TEMP);
    319  1.21      yamt 		}
    320  1.21      yamt 		free(dus->dus_uio_in, M_TEMP);
    321   1.1   thorpej 	}
    322   1.1   thorpej 
    323  1.27     rmind 	workqueue_enqueue(dmio_cleaner, &dus->dus_work, NULL);
    324  1.21      yamt }
    325  1.21      yamt 
    326  1.21      yamt static void
    327  1.21      yamt dmio_usrreq_fini1(struct work *wk, void *dummy)
    328  1.21      yamt {
    329  1.21      yamt 	struct dmio_usrreq_state *dus = (void *)wk;
    330  1.21      yamt 	int s;
    331   1.1   thorpej 
    332  1.21      yamt 	KASSERT(wk == &dus->dus_work);
    333   1.1   thorpej 
    334  1.21      yamt 	uvmspace_free(dus->dus_vmspace);
    335  1.21      yamt 	s = splsoftclock();
    336   1.1   thorpej 	pool_put(&dmio_usrreq_state_pool, dus);
    337  1.21      yamt 	splx(s);
    338   1.1   thorpej }
    339   1.1   thorpej 
    340   1.1   thorpej /*
    341   1.1   thorpej  * dmio_read:
    342   1.1   thorpej  *
    343   1.1   thorpej  *	Read file op.
    344   1.1   thorpej  */
    345   1.1   thorpej static int
    346   1.1   thorpej dmio_read(struct file *fp, off_t *offp, struct uio *uio,
    347  1.22      elad     kauth_cred_t cred, int flags)
    348   1.1   thorpej {
    349   1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    350   1.1   thorpej 	struct dmio_usrreq_state *dus;
    351   1.1   thorpej 	struct dmover_request *dreq;
    352   1.1   thorpej 	struct dmio_usrresp resp;
    353   1.1   thorpej 	int s, error = 0, progress = 0;
    354   1.1   thorpej 
    355   1.1   thorpej 	if ((uio->uio_resid % sizeof(resp)) != 0)
    356   1.1   thorpej 		return (EINVAL);
    357   1.1   thorpej 
    358   1.1   thorpej 	if (ds->ds_session == NULL)
    359   1.1   thorpej 		return (ENXIO);
    360   1.1   thorpej 
    361   1.1   thorpej 	s = splsoftclock();
    362   1.1   thorpej 	simple_lock(&ds->ds_slock);
    363   1.1   thorpej 
    364   1.1   thorpej 	while (uio->uio_resid != 0) {
    365   1.1   thorpej 
    366   1.1   thorpej 		for (;;) {
    367   1.1   thorpej 			dus = TAILQ_FIRST(&ds->ds_complete);
    368   1.1   thorpej 			if (dus == NULL) {
    369   1.1   thorpej 				if (fp->f_flag & FNONBLOCK) {
    370   1.1   thorpej 					error = progress ? 0 : EWOULDBLOCK;
    371   1.1   thorpej 					goto out;
    372   1.1   thorpej 				}
    373   1.9       scw 				ds->ds_flags |= DMIO_STATE_READ_WAIT;
    374   1.1   thorpej 				error = ltsleep(&ds->ds_complete,
    375   1.1   thorpej 				    PRIBIO | PCATCH, "dmvrrd", 0,
    376   1.1   thorpej 				    &ds->ds_slock);
    377   1.1   thorpej 				if (error)
    378   1.1   thorpej 					goto out;
    379   1.1   thorpej 				continue;
    380   1.1   thorpej 			}
    381   1.1   thorpej 			/* Have a completed request. */
    382   1.1   thorpej 			TAILQ_REMOVE(&ds->ds_complete, dus, dus_q);
    383   1.1   thorpej 			ds->ds_nreqs--;
    384   1.1   thorpej 			if (ds->ds_flags & DMIO_STATE_WRITE_WAIT) {
    385   1.1   thorpej 				ds->ds_flags &= ~DMIO_STATE_WRITE_WAIT;
    386   1.1   thorpej 				wakeup(&ds->ds_nreqs);
    387   1.1   thorpej 			}
    388   1.1   thorpej 			if (ds->ds_flags & DMIO_STATE_SEL) {
    389   1.1   thorpej 				ds->ds_flags &= ~DMIO_STATE_SEL;
    390  1.30     rmind 				selnotify(&ds->ds_selq, POLLIN | POLLRDNORM, 0);
    391   1.1   thorpej 			}
    392   1.1   thorpej 			break;
    393   1.1   thorpej 		}
    394   1.1   thorpej 
    395   1.1   thorpej 		simple_unlock(&ds->ds_slock);
    396   1.1   thorpej 
    397   1.1   thorpej 		dreq = dus->dus_req;
    398   1.1   thorpej 		resp.resp_id = dus->dus_id;
    399  1.12   thorpej 		if (dreq->dreq_flags & DMOVER_REQ_ERROR)
    400  1.12   thorpej 			resp.resp_error = dreq->dreq_error;
    401  1.12   thorpej 		else {
    402  1.12   thorpej 			resp.resp_error = 0;
    403  1.12   thorpej 			memcpy(resp.resp_immediate, dreq->dreq_immediate,
    404  1.12   thorpej 			    sizeof(resp.resp_immediate));
    405  1.12   thorpej 		}
    406   1.1   thorpej 
    407   1.1   thorpej 		dmio_usrreq_fini(ds, dus);
    408   1.1   thorpej 
    409   1.1   thorpej 		splx(s);
    410   1.1   thorpej 
    411   1.1   thorpej 		progress = 1;
    412   1.1   thorpej 
    413   1.1   thorpej 		dmover_request_free(dreq);
    414   1.1   thorpej 
    415   1.1   thorpej 		error = uiomove(&resp, sizeof(resp), uio);
    416   1.1   thorpej 		if (error)
    417   1.1   thorpej 			return (error);
    418   1.1   thorpej 
    419   1.1   thorpej 		s = splsoftclock();
    420   1.1   thorpej 		simple_lock(&ds->ds_slock);
    421   1.1   thorpej 	}
    422   1.1   thorpej 
    423   1.1   thorpej  out:
    424   1.1   thorpej 	simple_unlock(&ds->ds_slock);
    425   1.1   thorpej 	splx(s);
    426   1.1   thorpej 
    427   1.1   thorpej 	return (error);
    428   1.1   thorpej }
    429   1.1   thorpej 
    430   1.1   thorpej /*
    431   1.1   thorpej  * dmio_usrreq_done:
    432   1.1   thorpej  *
    433   1.1   thorpej  *	Dmover completion callback.
    434   1.1   thorpej  */
    435   1.1   thorpej static void
    436   1.1   thorpej dmio_usrreq_done(struct dmover_request *dreq)
    437   1.1   thorpej {
    438   1.1   thorpej 	struct dmio_usrreq_state *dus = dreq->dreq_cookie;
    439   1.1   thorpej 	struct dmio_state *ds = dreq->dreq_session->dses_cookie;
    440   1.1   thorpej 
    441   1.1   thorpej 	/* We're already at splsoftclock(). */
    442   1.1   thorpej 
    443   1.1   thorpej 	simple_lock(&ds->ds_slock);
    444   1.1   thorpej 	TAILQ_REMOVE(&ds->ds_pending, dus, dus_q);
    445   1.1   thorpej 	if (ds->ds_flags & DMIO_STATE_DEAD) {
    446   1.1   thorpej 		ds->ds_nreqs--;
    447   1.1   thorpej 		dmio_usrreq_fini(ds, dus);
    448   1.1   thorpej 		dmover_request_free(dreq);
    449   1.1   thorpej 		if (ds->ds_nreqs == 0) {
    450   1.1   thorpej 			simple_unlock(&ds->ds_slock);
    451  1.28        ad 			seldestroy(&ds->ds_selq);
    452   1.1   thorpej 			pool_put(&dmio_state_pool, ds);
    453   1.1   thorpej 			return;
    454   1.1   thorpej 		}
    455   1.1   thorpej 	} else {
    456   1.1   thorpej 		TAILQ_INSERT_TAIL(&ds->ds_complete, dus, dus_q);
    457   1.1   thorpej 		if (ds->ds_flags & DMIO_STATE_READ_WAIT) {
    458   1.1   thorpej 			ds->ds_flags &= ~DMIO_STATE_READ_WAIT;
    459   1.1   thorpej 			wakeup(&ds->ds_complete);
    460   1.1   thorpej 		}
    461   1.1   thorpej 		if (ds->ds_flags & DMIO_STATE_SEL) {
    462   1.1   thorpej 			ds->ds_flags &= ~DMIO_STATE_SEL;
    463  1.30     rmind 			selnotify(&ds->ds_selq, POLLOUT | POLLWRNORM, 0);
    464   1.1   thorpej 		}
    465   1.1   thorpej 	}
    466   1.1   thorpej 	simple_unlock(&ds->ds_slock);
    467   1.1   thorpej }
    468   1.1   thorpej 
    469   1.1   thorpej /*
    470   1.1   thorpej  * dmio_write:
    471   1.1   thorpej  *
    472   1.1   thorpej  *	Write file op.
    473   1.1   thorpej  */
    474   1.1   thorpej static int
    475   1.1   thorpej dmio_write(struct file *fp, off_t *offp, struct uio *uio,
    476  1.22      elad     kauth_cred_t cred, int flags)
    477   1.1   thorpej {
    478   1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    479   1.1   thorpej 	struct dmio_usrreq_state *dus;
    480   1.1   thorpej 	struct dmover_request *dreq;
    481   1.1   thorpej 	struct dmio_usrreq req;
    482   1.1   thorpej 	int error = 0, s, progress = 0;
    483   1.1   thorpej 
    484   1.1   thorpej 	if ((uio->uio_resid % sizeof(req)) != 0)
    485   1.1   thorpej 		return (EINVAL);
    486   1.1   thorpej 
    487   1.1   thorpej 	if (ds->ds_session == NULL)
    488   1.1   thorpej 		return (ENXIO);
    489   1.1   thorpej 
    490   1.1   thorpej 	s = splsoftclock();
    491   1.1   thorpej 	simple_lock(&ds->ds_slock);
    492   1.1   thorpej 
    493   1.1   thorpej 	while (uio->uio_resid != 0) {
    494   1.1   thorpej 
    495   1.1   thorpej 		if (ds->ds_nreqs == DMIO_NREQS_MAX) {
    496   1.1   thorpej 			if (fp->f_flag & FNONBLOCK) {
    497   1.1   thorpej 				error = progress ? 0 : EWOULDBLOCK;
    498   1.1   thorpej 				break;
    499   1.1   thorpej 			}
    500   1.1   thorpej 			ds->ds_flags |= DMIO_STATE_WRITE_WAIT;
    501   1.1   thorpej 			error = ltsleep(&ds->ds_nreqs, PRIBIO | PCATCH,
    502   1.1   thorpej 			    "dmiowr", 0, &ds->ds_slock);
    503   1.1   thorpej 			if (error)
    504   1.1   thorpej 				break;
    505   1.1   thorpej 			continue;
    506   1.1   thorpej 		}
    507   1.1   thorpej 
    508   1.1   thorpej 		ds->ds_nreqs++;
    509   1.1   thorpej 
    510   1.1   thorpej 		simple_unlock(&ds->ds_slock);
    511   1.1   thorpej 		splx(s);
    512   1.1   thorpej 
    513   1.1   thorpej 		progress = 1;
    514   1.1   thorpej 
    515   1.1   thorpej 		error = uiomove(&req, sizeof(req), uio);
    516   1.1   thorpej 		if (error) {
    517   1.1   thorpej 			s = splsoftclock();
    518   1.1   thorpej 			simple_lock(&ds->ds_slock);
    519   1.1   thorpej 			ds->ds_nreqs--;
    520   1.1   thorpej 			break;
    521   1.1   thorpej 		}
    522   1.1   thorpej 
    523   1.1   thorpej 		/* XXX How should this interact with FNONBLOCK? */
    524   1.1   thorpej 		dreq = dmover_request_alloc(ds->ds_session, NULL);
    525   1.1   thorpej 		if (dreq == NULL) {
    526   1.1   thorpej 			/* XXX */
    527   1.1   thorpej 			s = splsoftclock();
    528   1.1   thorpej 			simple_lock(&ds->ds_slock);
    529   1.1   thorpej 			ds->ds_nreqs--;
    530   1.1   thorpej 			error = ENOMEM;
    531   1.1   thorpej 			break;
    532   1.1   thorpej 		}
    533   1.1   thorpej 		s = splsoftclock();
    534   1.1   thorpej 		dus = pool_get(&dmio_usrreq_state_pool, PR_WAITOK);
    535   1.1   thorpej 		splx(s);
    536   1.1   thorpej 
    537   1.1   thorpej 		error = dmio_usrreq_init(fp, dus, &req, dreq);
    538   1.1   thorpej 		if (error) {
    539   1.1   thorpej 			dmover_request_free(dreq);
    540   1.1   thorpej 			s = splsoftclock();
    541   1.1   thorpej 			pool_put(&dmio_usrreq_state_pool, dus);
    542   1.1   thorpej 			simple_lock(&ds->ds_slock);
    543   1.1   thorpej 			break;
    544   1.1   thorpej 		}
    545   1.1   thorpej 
    546   1.1   thorpej 		dreq->dreq_callback = dmio_usrreq_done;
    547   1.1   thorpej 		dreq->dreq_cookie = dus;
    548   1.1   thorpej 
    549   1.1   thorpej 		dus->dus_req = dreq;
    550   1.1   thorpej 		dus->dus_id = req.req_id;
    551   1.1   thorpej 
    552   1.1   thorpej 		s = splsoftclock();
    553   1.1   thorpej 		simple_lock(&ds->ds_slock);
    554   1.1   thorpej 
    555   1.1   thorpej 		TAILQ_INSERT_TAIL(&ds->ds_pending, dus, dus_q);
    556   1.1   thorpej 
    557   1.1   thorpej 		simple_unlock(&ds->ds_slock);
    558   1.1   thorpej 		splx(s);
    559   1.1   thorpej 
    560   1.1   thorpej 		dmover_process(dreq);
    561   1.1   thorpej 
    562   1.1   thorpej 		s = splsoftclock();
    563   1.1   thorpej 		simple_lock(&ds->ds_slock);
    564   1.1   thorpej 	}
    565   1.1   thorpej 
    566   1.1   thorpej 	simple_unlock(&ds->ds_slock);
    567   1.1   thorpej 	splx(s);
    568   1.1   thorpej 
    569   1.1   thorpej 	return (error);
    570   1.1   thorpej }
    571   1.1   thorpej 
    572   1.1   thorpej /*
    573   1.1   thorpej  * dmio_ioctl:
    574   1.1   thorpej  *
    575   1.1   thorpej  *	Ioctl file op.
    576   1.1   thorpej  */
    577   1.1   thorpej static int
    578  1.31        ad dmio_ioctl(struct file *fp, u_long cmd, void *data)
    579   1.1   thorpej {
    580   1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    581   1.1   thorpej 	int error, s;
    582   1.1   thorpej 
    583   1.1   thorpej 	switch (cmd) {
    584   1.1   thorpej 	case FIONBIO:
    585   1.1   thorpej 	case FIOASYNC:
    586   1.1   thorpej 		return (0);
    587   1.1   thorpej 
    588   1.1   thorpej 	case DMIO_SETFUNC:
    589   1.1   thorpej 	    {
    590   1.8       dsl 		struct dmio_setfunc *dsf = data;
    591   1.1   thorpej 		struct dmover_session *dses;
    592   1.1   thorpej 
    593   1.1   thorpej 		s = splsoftclock();
    594   1.1   thorpej 		simple_lock(&ds->ds_slock);
    595   1.1   thorpej 
    596   1.1   thorpej 		if (ds->ds_session != NULL ||
    597   1.1   thorpej 		    (ds->ds_flags & DMIO_STATE_LARVAL) != 0) {
    598   1.1   thorpej 			simple_unlock(&ds->ds_slock);
    599   1.1   thorpej 			splx(s);
    600   1.1   thorpej 			return (EBUSY);
    601   1.1   thorpej 		}
    602   1.1   thorpej 
    603   1.1   thorpej 		ds->ds_flags |= DMIO_STATE_LARVAL;
    604   1.1   thorpej 
    605   1.1   thorpej 		simple_unlock(&ds->ds_slock);
    606   1.1   thorpej 		splx(s);
    607   1.1   thorpej 
    608   1.1   thorpej 		dsf->dsf_name[DMIO_MAX_FUNCNAME - 1] = '\0';
    609   1.1   thorpej 		error = dmover_session_create(dsf->dsf_name, &dses);
    610   1.1   thorpej 
    611   1.1   thorpej 		s = splsoftclock();
    612   1.1   thorpej 		simple_lock(&ds->ds_slock);
    613   1.1   thorpej 
    614   1.1   thorpej 		if (error == 0) {
    615   1.1   thorpej 			dses->dses_cookie = ds;
    616   1.1   thorpej 			ds->ds_session = dses;
    617   1.1   thorpej 		}
    618   1.1   thorpej 		ds->ds_flags &= ~DMIO_STATE_LARVAL;
    619   1.1   thorpej 
    620   1.1   thorpej 		simple_unlock(&ds->ds_slock);
    621   1.1   thorpej 		splx(s);
    622   1.1   thorpej 		break;
    623   1.1   thorpej 	    }
    624   1.1   thorpej 
    625   1.1   thorpej 	default:
    626   1.1   thorpej 		error = ENOTTY;
    627   1.1   thorpej 	}
    628   1.1   thorpej 
    629   1.1   thorpej 	return (error);
    630   1.1   thorpej }
    631   1.1   thorpej 
    632   1.1   thorpej /*
    633   1.1   thorpej  * dmio_poll:
    634   1.1   thorpej  *
    635   1.1   thorpej  *	Poll file op.
    636   1.1   thorpej  */
    637   1.1   thorpej static int
    638  1.31        ad dmio_poll(struct file *fp, int events)
    639   1.1   thorpej {
    640   1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    641   1.1   thorpej 	int s, revents = 0;
    642   1.1   thorpej 
    643   1.1   thorpej 	if ((events & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)) == 0)
    644   1.1   thorpej 		return (revents);
    645   1.1   thorpej 
    646   1.1   thorpej 	s = splsoftclock();
    647   1.1   thorpej 	simple_lock(&ds->ds_slock);
    648   1.1   thorpej 
    649   1.1   thorpej 	if (ds->ds_flags & DMIO_STATE_DEAD) {
    650   1.1   thorpej 		/* EOF */
    651   1.1   thorpej 		revents |= events & (POLLIN | POLLRDNORM |
    652   1.1   thorpej 		    POLLOUT | POLLWRNORM);
    653   1.1   thorpej 		goto out;
    654   1.1   thorpej 	}
    655   1.1   thorpej 
    656   1.1   thorpej 	/* We can read if there are completed requests. */
    657   1.1   thorpej 	if (events & (POLLIN | POLLRDNORM))
    658   1.1   thorpej 		if (TAILQ_EMPTY(&ds->ds_complete) == 0)
    659   1.1   thorpej 			revents |= events & (POLLIN | POLLRDNORM);
    660   1.1   thorpej 
    661   1.1   thorpej 	/*
    662   1.1   thorpej 	 * We can write if there is there are fewer then DMIO_NREQS_MAX
    663   1.1   thorpej 	 * are already in the queue.
    664   1.1   thorpej 	 */
    665   1.1   thorpej 	if (events & (POLLOUT | POLLWRNORM))
    666   1.1   thorpej 		if (ds->ds_nreqs < DMIO_NREQS_MAX)
    667   1.1   thorpej 			revents |= events & (POLLOUT | POLLWRNORM);
    668   1.1   thorpej 
    669   1.1   thorpej 	if (revents == 0) {
    670  1.31        ad 		selrecord(curlwp, &ds->ds_selq);
    671   1.1   thorpej 		ds->ds_flags |= DMIO_STATE_SEL;
    672   1.1   thorpej 	}
    673   1.1   thorpej 
    674   1.1   thorpej  out:
    675   1.1   thorpej 	simple_unlock(&ds->ds_slock);
    676   1.1   thorpej 	splx(s);
    677   1.1   thorpej 
    678   1.1   thorpej 	return (revents);
    679   1.1   thorpej }
    680   1.1   thorpej 
    681   1.1   thorpej /*
    682   1.1   thorpej  * dmio_close:
    683   1.1   thorpej  *
    684   1.1   thorpej  *	Close file op.
    685   1.1   thorpej  */
    686   1.1   thorpej static int
    687  1.31        ad dmio_close(struct file *fp)
    688   1.1   thorpej {
    689   1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    690   1.1   thorpej 	struct dmio_usrreq_state *dus;
    691   1.1   thorpej 	struct dmover_session *dses;
    692   1.1   thorpej 	int s;
    693   1.1   thorpej 
    694   1.1   thorpej 	s = splsoftclock();
    695   1.1   thorpej 	simple_lock(&ds->ds_slock);
    696   1.1   thorpej 
    697   1.1   thorpej 	ds->ds_flags |= DMIO_STATE_DEAD;
    698   1.1   thorpej 
    699   1.1   thorpej 	/* Garbage-collect all the responses on the queue. */
    700   1.1   thorpej 	while ((dus = TAILQ_FIRST(&ds->ds_complete)) != NULL) {
    701   1.1   thorpej 		TAILQ_REMOVE(&ds->ds_complete, dus, dus_q);
    702   1.1   thorpej 		ds->ds_nreqs--;
    703   1.1   thorpej 		dmover_request_free(dus->dus_req);
    704   1.1   thorpej 		dmio_usrreq_fini(ds, dus);
    705   1.1   thorpej 	}
    706   1.1   thorpej 
    707   1.1   thorpej 	/*
    708   1.1   thorpej 	 * If there are any requests pending, we have to wait for
    709   1.1   thorpej 	 * them.  Don't free the dmio_state in this case.
    710   1.1   thorpej 	 */
    711   1.1   thorpej 	if (ds->ds_nreqs == 0) {
    712   1.1   thorpej 		dses = ds->ds_session;
    713   1.1   thorpej 		simple_unlock(&ds->ds_slock);
    714  1.28        ad 		seldestroy(&ds->ds_selq);
    715   1.1   thorpej 		pool_put(&dmio_state_pool, ds);
    716   1.1   thorpej 	} else {
    717   1.1   thorpej 		dses = NULL;
    718   1.1   thorpej 		simple_unlock(&ds->ds_slock);
    719   1.1   thorpej 	}
    720   1.1   thorpej 
    721   1.1   thorpej 	splx(s);
    722   1.1   thorpej 
    723   1.1   thorpej 	fp->f_data = NULL;
    724   1.1   thorpej 
    725   1.1   thorpej 	if (dses != NULL)
    726   1.1   thorpej 		dmover_session_destroy(dses);
    727   1.1   thorpej 
    728   1.1   thorpej 	return (0);
    729   1.1   thorpej }
    730   1.1   thorpej 
    731  1.15  christos static const struct fileops dmio_fileops = {
    732  1.32        ad 	.fo_read = dmio_read,
    733  1.32        ad 	.fo_write = dmio_write,
    734  1.32        ad 	.fo_ioctl = dmio_ioctl,
    735  1.32        ad 	.fo_fcntl = fnullop_fcntl,
    736  1.32        ad 	.fo_poll = dmio_poll,
    737  1.32        ad 	.fo_stat = fbadop_stat,
    738  1.32        ad 	.fo_close = dmio_close,
    739  1.32        ad 	.fo_kqfilter = fnullop_kqfilter,
    740  1.32        ad 	.fo_drain = fnullop_drain,
    741   1.1   thorpej };
    742   1.1   thorpej 
    743   1.1   thorpej /*
    744   1.1   thorpej  * dmoverioopen:
    745   1.1   thorpej  *
    746   1.1   thorpej  *	Device switch open routine.
    747   1.1   thorpej  */
    748   1.1   thorpej int
    749  1.18  christos dmoverioopen(dev_t dev, int flag, int mode, struct lwp *l)
    750   1.1   thorpej {
    751   1.1   thorpej 	struct dmio_state *ds;
    752   1.1   thorpej 	struct file *fp;
    753   1.1   thorpej 	int error, fd, s;
    754   1.1   thorpej 
    755   1.1   thorpej 	/* falloc() will use the descriptor for us. */
    756  1.31        ad 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    757   1.1   thorpej 		return (error);
    758   1.1   thorpej 
    759   1.1   thorpej 	s = splsoftclock();
    760   1.1   thorpej 	ds = pool_get(&dmio_state_pool, PR_WAITOK);
    761   1.1   thorpej 	splx(s);
    762   1.1   thorpej 
    763   1.1   thorpej 	memset(ds, 0, sizeof(*ds));
    764  1.20      yamt 	simple_lock_init(&ds->ds_slock);
    765   1.1   thorpej 	TAILQ_INIT(&ds->ds_pending);
    766   1.1   thorpej 	TAILQ_INIT(&ds->ds_complete);
    767  1.28        ad 	selinit(&ds->ds_selq);
    768   1.1   thorpej 
    769  1.31        ad 	return fd_clone(fp, fd, flag, &dmio_fileops, ds);
    770   1.1   thorpej }
    771