Home | History | Annotate | Line # | Download | only in dmover
dmover_io.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: dmover_io.c,v 1.1 2002/08/02 00:30:38 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 2002 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.1  thorpej __KERNEL_RCSID(0, "$NetBSD");
     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.1  thorpej #include <sys/lock.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.1  thorpej 
     74  1.1  thorpej #include <dev/dmover/dmovervar.h>
     75  1.1  thorpej #include <dev/dmover/dmover_io.h>
     76  1.1  thorpej 
     77  1.1  thorpej struct dmio_usrreq_state {
     78  1.1  thorpej 	TAILQ_ENTRY(dmio_usrreq_state) dus_q;
     79  1.1  thorpej 	struct uio dus_uio_out;
     80  1.1  thorpej 	struct uio *dus_uio_in;
     81  1.1  thorpej 	struct dmover_request *dus_req;
     82  1.1  thorpej 	uint32_t dus_id;
     83  1.1  thorpej };
     84  1.1  thorpej 
     85  1.1  thorpej struct dmio_state {
     86  1.1  thorpej 	struct dmover_session *ds_session;
     87  1.1  thorpej 	TAILQ_HEAD(, dmio_usrreq_state) ds_pending;
     88  1.1  thorpej 	TAILQ_HEAD(, dmio_usrreq_state) ds_complete;
     89  1.1  thorpej 	struct selinfo ds_selq;
     90  1.1  thorpej 	__volatile int ds_flags;
     91  1.1  thorpej 	u_int ds_nreqs;
     92  1.1  thorpej 	struct simplelock ds_slock;
     93  1.1  thorpej };
     94  1.1  thorpej 
     95  1.1  thorpej #define	DMIO_STATE_SEL		0x0001
     96  1.1  thorpej #define	DMIO_STATE_DEAD		0x0002
     97  1.1  thorpej #define	DMIO_STATE_LARVAL	0x0004
     98  1.1  thorpej #define	DMIO_STATE_READ_WAIT	0x0008
     99  1.1  thorpej #define	DMIO_STATE_WRITE_WAIT	0x0010
    100  1.1  thorpej 
    101  1.1  thorpej #define	DMIO_NREQS_MAX		64	/* XXX pulled out of a hat */
    102  1.1  thorpej 
    103  1.1  thorpej struct pool dmio_state_pool;
    104  1.1  thorpej struct pool dmio_usrreq_state_pool;
    105  1.1  thorpej 
    106  1.1  thorpej void	dmoverioattach(int);
    107  1.1  thorpej cdev_decl(dmoverio);
    108  1.1  thorpej 
    109  1.1  thorpej /*
    110  1.1  thorpej  * dmoverioattach:
    111  1.1  thorpej  *
    112  1.1  thorpej  *	Pseudo-device attach routine.
    113  1.1  thorpej  */
    114  1.1  thorpej void
    115  1.1  thorpej dmoverioattach(int count)
    116  1.1  thorpej {
    117  1.1  thorpej 
    118  1.1  thorpej 	pool_init(&dmio_state_pool, sizeof(struct dmio_state),
    119  1.1  thorpej 	    0, 0, 0, "dmiostate", NULL);
    120  1.1  thorpej 	pool_init(&dmio_usrreq_state_pool, sizeof(struct dmio_usrreq_state),
    121  1.1  thorpej 	    0, 0, 0, "dmiourstate", NULL);
    122  1.1  thorpej }
    123  1.1  thorpej 
    124  1.1  thorpej /*
    125  1.1  thorpej  * dmio_usrreq_init:
    126  1.1  thorpej  *
    127  1.1  thorpej  *	Build a request structure.
    128  1.1  thorpej  */
    129  1.1  thorpej static int
    130  1.1  thorpej dmio_usrreq_init(struct file *fp, struct dmio_usrreq_state *dus,
    131  1.1  thorpej     struct dmio_usrreq *req, struct dmover_request *dreq)
    132  1.1  thorpej {
    133  1.1  thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    134  1.1  thorpej 	struct dmover_session *dses = ds->ds_session;
    135  1.1  thorpej 	struct uio *uio_out = &dus->dus_uio_out;
    136  1.1  thorpej 	struct uio *uio_in;
    137  1.1  thorpej 	dmio_buffer inbuf;
    138  1.1  thorpej 	size_t len;
    139  1.1  thorpej 	u_int i, j;
    140  1.1  thorpej 	int error;
    141  1.1  thorpej 
    142  1.1  thorpej 	/* XXX How should malloc interact w/ FNONBLOCK? */
    143  1.1  thorpej 
    144  1.1  thorpej 	if (req->req_outbuf.dmbuf_iovcnt > IOV_MAX)
    145  1.1  thorpej 		return (EINVAL);
    146  1.1  thorpej 	len = sizeof(struct iovec) * req->req_outbuf.dmbuf_iovcnt;
    147  1.1  thorpej 	if (len == 0)
    148  1.1  thorpej 		return (EINVAL);
    149  1.1  thorpej 	uio_out->uio_iov = malloc(len, M_TEMP, M_WAITOK);
    150  1.1  thorpej 
    151  1.1  thorpej 	error = copyin(req->req_outbuf.dmbuf_iov, uio_out->uio_iov, len);
    152  1.1  thorpej 	if (error) {
    153  1.1  thorpej 		free(uio_out->uio_iov, M_TEMP);
    154  1.1  thorpej 		return (error);
    155  1.1  thorpej 	}
    156  1.1  thorpej 
    157  1.1  thorpej 	for (j = 0, len = 0; j < req->req_outbuf.dmbuf_iovcnt; j++) {
    158  1.1  thorpej 		len += uio_out->uio_iov[j].iov_len;
    159  1.1  thorpej 		if (len > SSIZE_MAX) {
    160  1.1  thorpej 			free(uio_out->uio_iov, M_TEMP);
    161  1.1  thorpej 			return (error);
    162  1.1  thorpej 		}
    163  1.1  thorpej 	}
    164  1.1  thorpej 
    165  1.1  thorpej 	uio_out->uio_iovcnt = req->req_outbuf.dmbuf_iovcnt;
    166  1.1  thorpej 	uio_out->uio_resid = len;
    167  1.1  thorpej 	uio_out->uio_rw = UIO_READ;
    168  1.1  thorpej 	uio_out->uio_segflg = UIO_USERSPACE;
    169  1.1  thorpej 	uio_out->uio_procp = curproc;
    170  1.1  thorpej 
    171  1.1  thorpej 	dreq->dreq_outbuf_type = DMOVER_BUF_UIO;
    172  1.1  thorpej 	dreq->dreq_outbuf.dmbuf_uio = uio_out;
    173  1.1  thorpej 
    174  1.1  thorpej 	if (dses->dses_ninputs == 0) {
    175  1.1  thorpej 		/* No inputs; copy the immediate. */
    176  1.1  thorpej 		memcpy(dreq->dreq_immediate, req->req_immediate,
    177  1.1  thorpej 		    sizeof(dreq->dreq_immediate));
    178  1.1  thorpej 		return (0);
    179  1.1  thorpej 	}
    180  1.1  thorpej 
    181  1.1  thorpej 	dreq->dreq_inbuf_type = DMOVER_BUF_UIO;
    182  1.1  thorpej 
    183  1.1  thorpej 	dus->dus_uio_in = malloc(sizeof(struct uio) * dses->dses_ninputs,
    184  1.1  thorpej 	    M_TEMP, M_WAITOK);
    185  1.1  thorpej 	memset(dus->dus_uio_in, 0, sizeof(struct uio) * dses->dses_ninputs);
    186  1.1  thorpej 
    187  1.1  thorpej 	for (i = 0; i < dses->dses_ninputs; i++) {
    188  1.1  thorpej 		uio_in = &dus->dus_uio_in[i];
    189  1.1  thorpej 
    190  1.1  thorpej 		error = copyin(&req->req_inbuf[i], &inbuf, sizeof(inbuf));
    191  1.1  thorpej 		if (error)
    192  1.1  thorpej 			goto bad;
    193  1.1  thorpej 
    194  1.1  thorpej 		if (inbuf.dmbuf_iovcnt > IOV_MAX) {
    195  1.1  thorpej 			error = EINVAL;
    196  1.1  thorpej 			goto bad;
    197  1.1  thorpej 		}
    198  1.1  thorpej 		len = sizeof(struct iovec) * inbuf.dmbuf_iovcnt;
    199  1.1  thorpej 		if (len == 0) {
    200  1.1  thorpej 			error = EINVAL;
    201  1.1  thorpej 			goto bad;
    202  1.1  thorpej 		}
    203  1.1  thorpej 		uio_in->uio_iov = malloc(len, M_TEMP, M_WAITOK);
    204  1.1  thorpej 
    205  1.1  thorpej 		error = copyin(inbuf.dmbuf_iov, uio_in->uio_iov, len);
    206  1.1  thorpej 		if (error) {
    207  1.1  thorpej 			free(uio_in->uio_iov, M_TEMP);
    208  1.1  thorpej 			goto bad;
    209  1.1  thorpej 		}
    210  1.1  thorpej 
    211  1.1  thorpej 		for (j = 0, len = 0; j < req->req_outbuf.dmbuf_iovcnt; j++) {
    212  1.1  thorpej 			len += uio_in->uio_iov[j].iov_len;
    213  1.1  thorpej 			if (len > SSIZE_MAX) {
    214  1.1  thorpej 				free(uio_in->uio_iov, M_TEMP);
    215  1.1  thorpej 				error = EINVAL;
    216  1.1  thorpej 				goto bad;
    217  1.1  thorpej 			}
    218  1.1  thorpej 		}
    219  1.1  thorpej 
    220  1.1  thorpej 		if (len != uio_out->uio_resid) {
    221  1.1  thorpej 			free(uio_in->uio_iov, M_TEMP);
    222  1.1  thorpej 			error = EINVAL;
    223  1.1  thorpej 			goto bad;
    224  1.1  thorpej 		}
    225  1.1  thorpej 
    226  1.1  thorpej 		uio_in->uio_iovcnt = inbuf.dmbuf_iovcnt;
    227  1.1  thorpej 		uio_in->uio_resid = len;
    228  1.1  thorpej 		uio_in->uio_rw = UIO_WRITE;
    229  1.1  thorpej 		uio_in->uio_segflg = UIO_USERSPACE;
    230  1.1  thorpej 		uio_in->uio_procp = curproc;
    231  1.1  thorpej 
    232  1.1  thorpej 		dreq->dreq_inbuf[i].dmbuf_uio = uio_in;
    233  1.1  thorpej 	}
    234  1.1  thorpej 
    235  1.1  thorpej 	return (0);
    236  1.1  thorpej 
    237  1.1  thorpej  bad:
    238  1.1  thorpej 	if (i > 0) {
    239  1.1  thorpej 		for (--i; i >= 0; i--) {
    240  1.1  thorpej 			uio_in = &dus->dus_uio_in[i];
    241  1.1  thorpej 			free(uio_in->uio_iov, M_TEMP);
    242  1.1  thorpej 		}
    243  1.1  thorpej 	}
    244  1.1  thorpej 	free(dus->dus_uio_in, M_TEMP);
    245  1.1  thorpej 	free(uio_out->uio_iov, M_TEMP);
    246  1.1  thorpej 	return (error);
    247  1.1  thorpej }
    248  1.1  thorpej 
    249  1.1  thorpej /*
    250  1.1  thorpej  * dmio_usrreq_fini:
    251  1.1  thorpej  *
    252  1.1  thorpej  *	Tear down a request.  Must be called at splsoftclock().
    253  1.1  thorpej  */
    254  1.1  thorpej static void
    255  1.1  thorpej dmio_usrreq_fini(struct dmio_state *ds, struct dmio_usrreq_state *dus)
    256  1.1  thorpej {
    257  1.1  thorpej 	struct dmover_session *dses = ds->ds_session;
    258  1.1  thorpej 	struct uio *uio_out = &dus->dus_uio_out;
    259  1.1  thorpej 	struct uio *uio_in;
    260  1.1  thorpej 	int i;
    261  1.1  thorpej 
    262  1.1  thorpej 	free(uio_out->uio_iov, M_TEMP);
    263  1.1  thorpej 
    264  1.1  thorpej 	if (dses->dses_ninputs == 0) {
    265  1.1  thorpej 		pool_put(&dmio_usrreq_state_pool, dus);
    266  1.1  thorpej 		return;
    267  1.1  thorpej 	}
    268  1.1  thorpej 
    269  1.1  thorpej 	for (i = 0; i < dses->dses_ninputs; i++) {
    270  1.1  thorpej 		uio_in = &dus->dus_uio_in[i];
    271  1.1  thorpej 		free(uio_in->uio_iov, M_TEMP);
    272  1.1  thorpej 	}
    273  1.1  thorpej 
    274  1.1  thorpej 	free(dus->dus_uio_in, M_TEMP);
    275  1.1  thorpej 
    276  1.1  thorpej 	pool_put(&dmio_usrreq_state_pool, dus);
    277  1.1  thorpej }
    278  1.1  thorpej 
    279  1.1  thorpej /*
    280  1.1  thorpej  * dmio_read:
    281  1.1  thorpej  *
    282  1.1  thorpej  *	Read file op.
    283  1.1  thorpej  */
    284  1.1  thorpej static int
    285  1.1  thorpej dmio_read(struct file *fp, off_t *offp, struct uio *uio,
    286  1.1  thorpej     struct ucred *cred, int flags)
    287  1.1  thorpej {
    288  1.1  thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    289  1.1  thorpej 	struct dmio_usrreq_state *dus;
    290  1.1  thorpej 	struct dmover_request *dreq;
    291  1.1  thorpej 	struct dmio_usrresp resp;
    292  1.1  thorpej 	int s, error = 0, progress = 0;
    293  1.1  thorpej 
    294  1.1  thorpej 	if ((uio->uio_resid % sizeof(resp)) != 0)
    295  1.1  thorpej 		return (EINVAL);
    296  1.1  thorpej 
    297  1.1  thorpej 	if (ds->ds_session == NULL)
    298  1.1  thorpej 		return (ENXIO);
    299  1.1  thorpej 
    300  1.1  thorpej 	s = splsoftclock();
    301  1.1  thorpej 	simple_lock(&ds->ds_slock);
    302  1.1  thorpej 
    303  1.1  thorpej 	while (uio->uio_resid != 0) {
    304  1.1  thorpej 
    305  1.1  thorpej 		for (;;) {
    306  1.1  thorpej 			dus = TAILQ_FIRST(&ds->ds_complete);
    307  1.1  thorpej 			if (dus == NULL) {
    308  1.1  thorpej 				if (fp->f_flag & FNONBLOCK) {
    309  1.1  thorpej 					error = progress ? 0 : EWOULDBLOCK;
    310  1.1  thorpej 					goto out;
    311  1.1  thorpej 				}
    312  1.1  thorpej 				error = ltsleep(&ds->ds_complete,
    313  1.1  thorpej 				    PRIBIO | PCATCH, "dmvrrd", 0,
    314  1.1  thorpej 				    &ds->ds_slock);
    315  1.1  thorpej 				if (error)
    316  1.1  thorpej 					goto out;
    317  1.1  thorpej 				continue;
    318  1.1  thorpej 			}
    319  1.1  thorpej 			/* Have a completed request. */
    320  1.1  thorpej 			TAILQ_REMOVE(&ds->ds_complete, dus, dus_q);
    321  1.1  thorpej 			ds->ds_nreqs--;
    322  1.1  thorpej 			if (ds->ds_flags & DMIO_STATE_WRITE_WAIT) {
    323  1.1  thorpej 				ds->ds_flags &= ~DMIO_STATE_WRITE_WAIT;
    324  1.1  thorpej 				wakeup(&ds->ds_nreqs);
    325  1.1  thorpej 			}
    326  1.1  thorpej 			if (ds->ds_flags & DMIO_STATE_SEL) {
    327  1.1  thorpej 				ds->ds_flags &= ~DMIO_STATE_SEL;
    328  1.1  thorpej 				selwakeup(&ds->ds_selq);
    329  1.1  thorpej 			}
    330  1.1  thorpej 			break;
    331  1.1  thorpej 		}
    332  1.1  thorpej 
    333  1.1  thorpej 		simple_unlock(&ds->ds_slock);
    334  1.1  thorpej 
    335  1.1  thorpej 		dreq = dus->dus_req;
    336  1.1  thorpej 		resp.resp_id = dus->dus_id;
    337  1.1  thorpej 		resp.resp_error = (dreq->dreq_flags & DMOVER_REQ_ERROR) ?
    338  1.1  thorpej 		    dreq->dreq_error : 0;
    339  1.1  thorpej 
    340  1.1  thorpej 		dmio_usrreq_fini(ds, dus);
    341  1.1  thorpej 
    342  1.1  thorpej 		splx(s);
    343  1.1  thorpej 
    344  1.1  thorpej 		progress = 1;
    345  1.1  thorpej 
    346  1.1  thorpej 		dmover_request_free(dreq);
    347  1.1  thorpej 
    348  1.1  thorpej 		error = uiomove(&resp, sizeof(resp), uio);
    349  1.1  thorpej 		if (error)
    350  1.1  thorpej 			return (error);
    351  1.1  thorpej 
    352  1.1  thorpej 		s = splsoftclock();
    353  1.1  thorpej 		simple_lock(&ds->ds_slock);
    354  1.1  thorpej 	}
    355  1.1  thorpej 
    356  1.1  thorpej  out:
    357  1.1  thorpej 	simple_unlock(&ds->ds_slock);
    358  1.1  thorpej 	splx(s);
    359  1.1  thorpej 
    360  1.1  thorpej 	return (error);
    361  1.1  thorpej }
    362  1.1  thorpej 
    363  1.1  thorpej /*
    364  1.1  thorpej  * dmio_usrreq_done:
    365  1.1  thorpej  *
    366  1.1  thorpej  *	Dmover completion callback.
    367  1.1  thorpej  */
    368  1.1  thorpej static void
    369  1.1  thorpej dmio_usrreq_done(struct dmover_request *dreq)
    370  1.1  thorpej {
    371  1.1  thorpej 	struct dmio_usrreq_state *dus = dreq->dreq_cookie;
    372  1.1  thorpej 	struct dmio_state *ds = dreq->dreq_session->dses_cookie;
    373  1.1  thorpej 
    374  1.1  thorpej 	/* We're already at splsoftclock(). */
    375  1.1  thorpej 
    376  1.1  thorpej 	simple_lock(&ds->ds_slock);
    377  1.1  thorpej 	TAILQ_REMOVE(&ds->ds_pending, dus, dus_q);
    378  1.1  thorpej 	if (ds->ds_flags & DMIO_STATE_DEAD) {
    379  1.1  thorpej 		ds->ds_nreqs--;
    380  1.1  thorpej 		dmio_usrreq_fini(ds, dus);
    381  1.1  thorpej 		dmover_request_free(dreq);
    382  1.1  thorpej 		if (ds->ds_nreqs == 0) {
    383  1.1  thorpej 			simple_unlock(&ds->ds_slock);
    384  1.1  thorpej 			pool_put(&dmio_state_pool, ds);
    385  1.1  thorpej 			return;
    386  1.1  thorpej 		}
    387  1.1  thorpej 	} else {
    388  1.1  thorpej 		TAILQ_INSERT_TAIL(&ds->ds_complete, dus, dus_q);
    389  1.1  thorpej 		if (ds->ds_flags & DMIO_STATE_READ_WAIT) {
    390  1.1  thorpej 			ds->ds_flags &= ~DMIO_STATE_READ_WAIT;
    391  1.1  thorpej 			wakeup(&ds->ds_complete);
    392  1.1  thorpej 		}
    393  1.1  thorpej 		if (ds->ds_flags & DMIO_STATE_SEL) {
    394  1.1  thorpej 			ds->ds_flags &= ~DMIO_STATE_SEL;
    395  1.1  thorpej 			selwakeup(&ds->ds_selq);
    396  1.1  thorpej 		}
    397  1.1  thorpej 	}
    398  1.1  thorpej 	simple_unlock(&ds->ds_slock);
    399  1.1  thorpej }
    400  1.1  thorpej 
    401  1.1  thorpej /*
    402  1.1  thorpej  * dmio_write:
    403  1.1  thorpej  *
    404  1.1  thorpej  *	Write file op.
    405  1.1  thorpej  */
    406  1.1  thorpej static int
    407  1.1  thorpej dmio_write(struct file *fp, off_t *offp, struct uio *uio,
    408  1.1  thorpej     struct ucred *cred, int flags)
    409  1.1  thorpej {
    410  1.1  thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    411  1.1  thorpej 	struct dmio_usrreq_state *dus;
    412  1.1  thorpej 	struct dmover_request *dreq;
    413  1.1  thorpej 	struct dmio_usrreq req;
    414  1.1  thorpej 	int error = 0, s, progress = 0;
    415  1.1  thorpej 
    416  1.1  thorpej 	if ((uio->uio_resid % sizeof(req)) != 0)
    417  1.1  thorpej 		return (EINVAL);
    418  1.1  thorpej 
    419  1.1  thorpej 	if (ds->ds_session == NULL)
    420  1.1  thorpej 		return (ENXIO);
    421  1.1  thorpej 
    422  1.1  thorpej 	s = splsoftclock();
    423  1.1  thorpej 	simple_lock(&ds->ds_slock);
    424  1.1  thorpej 
    425  1.1  thorpej 	while (uio->uio_resid != 0) {
    426  1.1  thorpej 
    427  1.1  thorpej 		if (ds->ds_nreqs == DMIO_NREQS_MAX) {
    428  1.1  thorpej 			if (fp->f_flag & FNONBLOCK) {
    429  1.1  thorpej 				error = progress ? 0 : EWOULDBLOCK;
    430  1.1  thorpej 				break;
    431  1.1  thorpej 			}
    432  1.1  thorpej 			ds->ds_flags |= DMIO_STATE_WRITE_WAIT;
    433  1.1  thorpej 			error = ltsleep(&ds->ds_nreqs, PRIBIO | PCATCH,
    434  1.1  thorpej 			    "dmiowr", 0, &ds->ds_slock);
    435  1.1  thorpej 			if (error)
    436  1.1  thorpej 				break;
    437  1.1  thorpej 			continue;
    438  1.1  thorpej 		}
    439  1.1  thorpej 
    440  1.1  thorpej 		ds->ds_nreqs++;
    441  1.1  thorpej 
    442  1.1  thorpej 		simple_unlock(&ds->ds_slock);
    443  1.1  thorpej 		splx(s);
    444  1.1  thorpej 
    445  1.1  thorpej 		progress = 1;
    446  1.1  thorpej 
    447  1.1  thorpej 		error = uiomove(&req, sizeof(req), uio);
    448  1.1  thorpej 		if (error) {
    449  1.1  thorpej 			s = splsoftclock();
    450  1.1  thorpej 			simple_lock(&ds->ds_slock);
    451  1.1  thorpej 			ds->ds_nreqs--;
    452  1.1  thorpej 			break;
    453  1.1  thorpej 		}
    454  1.1  thorpej 
    455  1.1  thorpej 		/* XXX How should this interact with FNONBLOCK? */
    456  1.1  thorpej 		dreq = dmover_request_alloc(ds->ds_session, NULL);
    457  1.1  thorpej 		if (dreq == NULL) {
    458  1.1  thorpej 			/* XXX */
    459  1.1  thorpej 			s = splsoftclock();
    460  1.1  thorpej 			simple_lock(&ds->ds_slock);
    461  1.1  thorpej 			ds->ds_nreqs--;
    462  1.1  thorpej 			error = ENOMEM;
    463  1.1  thorpej 			break;
    464  1.1  thorpej 		}
    465  1.1  thorpej 		s = splsoftclock();
    466  1.1  thorpej 		dus = pool_get(&dmio_usrreq_state_pool, PR_WAITOK);
    467  1.1  thorpej 		splx(s);
    468  1.1  thorpej 
    469  1.1  thorpej 		error = dmio_usrreq_init(fp, dus, &req, dreq);
    470  1.1  thorpej 		if (error) {
    471  1.1  thorpej 			dmover_request_free(dreq);
    472  1.1  thorpej 			s = splsoftclock();
    473  1.1  thorpej 			pool_put(&dmio_usrreq_state_pool, dus);
    474  1.1  thorpej 			simple_lock(&ds->ds_slock);
    475  1.1  thorpej 			break;
    476  1.1  thorpej 		}
    477  1.1  thorpej 
    478  1.1  thorpej 		dreq->dreq_callback = dmio_usrreq_done;
    479  1.1  thorpej 		dreq->dreq_cookie = dus;
    480  1.1  thorpej 
    481  1.1  thorpej 		dus->dus_req = dreq;
    482  1.1  thorpej 		dus->dus_id = req.req_id;
    483  1.1  thorpej 
    484  1.1  thorpej 		s = splsoftclock();
    485  1.1  thorpej 		simple_lock(&ds->ds_slock);
    486  1.1  thorpej 
    487  1.1  thorpej 		TAILQ_INSERT_TAIL(&ds->ds_pending, dus, dus_q);
    488  1.1  thorpej 
    489  1.1  thorpej 		simple_unlock(&ds->ds_slock);
    490  1.1  thorpej 		splx(s);
    491  1.1  thorpej 
    492  1.1  thorpej 		dmover_process(dreq);
    493  1.1  thorpej 
    494  1.1  thorpej 		s = splsoftclock();
    495  1.1  thorpej 		simple_lock(&ds->ds_slock);
    496  1.1  thorpej 	}
    497  1.1  thorpej 
    498  1.1  thorpej 	simple_unlock(&ds->ds_slock);
    499  1.1  thorpej 	splx(s);
    500  1.1  thorpej 
    501  1.1  thorpej 	return (error);
    502  1.1  thorpej }
    503  1.1  thorpej 
    504  1.1  thorpej /*
    505  1.1  thorpej  * dmio_ioctl:
    506  1.1  thorpej  *
    507  1.1  thorpej  *	Ioctl file op.
    508  1.1  thorpej  */
    509  1.1  thorpej static int
    510  1.1  thorpej dmio_ioctl(struct file *fp, u_long cmd, caddr_t data, struct proc *p)
    511  1.1  thorpej {
    512  1.1  thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    513  1.1  thorpej 	int error, s;
    514  1.1  thorpej 
    515  1.1  thorpej 	switch (cmd) {
    516  1.1  thorpej 	case FIONBIO:
    517  1.1  thorpej 	case FIOASYNC:
    518  1.1  thorpej 		return (0);
    519  1.1  thorpej 
    520  1.1  thorpej 	case DMIO_SETFUNC:
    521  1.1  thorpej 	    {
    522  1.1  thorpej 		struct dmio_setfunc *dsf = (void *) data;
    523  1.1  thorpej 		struct dmover_session *dses;
    524  1.1  thorpej 
    525  1.1  thorpej 		s = splsoftclock();
    526  1.1  thorpej 		simple_lock(&ds->ds_slock);
    527  1.1  thorpej 
    528  1.1  thorpej 		if (ds->ds_session != NULL ||
    529  1.1  thorpej 		    (ds->ds_flags & DMIO_STATE_LARVAL) != 0) {
    530  1.1  thorpej 			simple_unlock(&ds->ds_slock);
    531  1.1  thorpej 			splx(s);
    532  1.1  thorpej 			return (EBUSY);
    533  1.1  thorpej 		}
    534  1.1  thorpej 
    535  1.1  thorpej 		ds->ds_flags |= DMIO_STATE_LARVAL;
    536  1.1  thorpej 
    537  1.1  thorpej 		simple_unlock(&ds->ds_slock);
    538  1.1  thorpej 		splx(s);
    539  1.1  thorpej 
    540  1.1  thorpej 		dsf->dsf_name[DMIO_MAX_FUNCNAME - 1] = '\0';
    541  1.1  thorpej 		error = dmover_session_create(dsf->dsf_name, &dses);
    542  1.1  thorpej 
    543  1.1  thorpej 		s = splsoftclock();
    544  1.1  thorpej 		simple_lock(&ds->ds_slock);
    545  1.1  thorpej 
    546  1.1  thorpej 		if (error == 0) {
    547  1.1  thorpej 			dses->dses_cookie = ds;
    548  1.1  thorpej 			ds->ds_session = dses;
    549  1.1  thorpej 		}
    550  1.1  thorpej 		ds->ds_flags &= ~DMIO_STATE_LARVAL;
    551  1.1  thorpej 
    552  1.1  thorpej 		simple_unlock(&ds->ds_slock);
    553  1.1  thorpej 		splx(s);
    554  1.1  thorpej 		break;
    555  1.1  thorpej 	    }
    556  1.1  thorpej 
    557  1.1  thorpej 	default:
    558  1.1  thorpej 		error = ENOTTY;
    559  1.1  thorpej 	}
    560  1.1  thorpej 
    561  1.1  thorpej 	return (error);
    562  1.1  thorpej }
    563  1.1  thorpej 
    564  1.1  thorpej /*
    565  1.1  thorpej  * dmio_fcntl:
    566  1.1  thorpej  *
    567  1.1  thorpej  *	Fcntl file op.
    568  1.1  thorpej  */
    569  1.1  thorpej static int
    570  1.1  thorpej dmio_fcntl(struct file *fp, u_int cmd, caddr_t data, struct proc *p)
    571  1.1  thorpej {
    572  1.1  thorpej 
    573  1.1  thorpej 	if (cmd == FNONBLOCK || cmd == FASYNC)
    574  1.1  thorpej 		return (0);
    575  1.1  thorpej 
    576  1.1  thorpej 	return (EOPNOTSUPP);
    577  1.1  thorpej }
    578  1.1  thorpej 
    579  1.1  thorpej /*
    580  1.1  thorpej  * dmio_poll:
    581  1.1  thorpej  *
    582  1.1  thorpej  *	Poll file op.
    583  1.1  thorpej  */
    584  1.1  thorpej static int
    585  1.1  thorpej dmio_poll(struct file *fp, int events, struct proc *p)
    586  1.1  thorpej {
    587  1.1  thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    588  1.1  thorpej 	int s, revents = 0;
    589  1.1  thorpej 
    590  1.1  thorpej 	if ((events & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)) == 0)
    591  1.1  thorpej 		return (revents);
    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_flags & DMIO_STATE_DEAD) {
    597  1.1  thorpej 		/* EOF */
    598  1.1  thorpej 		revents |= events & (POLLIN | POLLRDNORM |
    599  1.1  thorpej 		    POLLOUT | POLLWRNORM);
    600  1.1  thorpej 		goto out;
    601  1.1  thorpej 	}
    602  1.1  thorpej 
    603  1.1  thorpej 	/* We can read if there are completed requests. */
    604  1.1  thorpej 	if (events & (POLLIN | POLLRDNORM))
    605  1.1  thorpej 		if (TAILQ_EMPTY(&ds->ds_complete) == 0)
    606  1.1  thorpej 			revents |= events & (POLLIN | POLLRDNORM);
    607  1.1  thorpej 
    608  1.1  thorpej 	/*
    609  1.1  thorpej 	 * We can write if there is there are fewer then DMIO_NREQS_MAX
    610  1.1  thorpej 	 * are already in the queue.
    611  1.1  thorpej 	 */
    612  1.1  thorpej 	if (events & (POLLOUT | POLLWRNORM))
    613  1.1  thorpej 		if (ds->ds_nreqs < DMIO_NREQS_MAX)
    614  1.1  thorpej 			revents |= events & (POLLOUT | POLLWRNORM);
    615  1.1  thorpej 
    616  1.1  thorpej 	if (revents == 0) {
    617  1.1  thorpej 		selrecord(p, &ds->ds_selq);
    618  1.1  thorpej 		ds->ds_flags |= DMIO_STATE_SEL;
    619  1.1  thorpej 	}
    620  1.1  thorpej 
    621  1.1  thorpej  out:
    622  1.1  thorpej 	simple_unlock(&ds->ds_slock);
    623  1.1  thorpej 	splx(s);
    624  1.1  thorpej 
    625  1.1  thorpej 	return (revents);
    626  1.1  thorpej }
    627  1.1  thorpej 
    628  1.1  thorpej /*
    629  1.1  thorpej  * dmio_stat:
    630  1.1  thorpej  *
    631  1.1  thorpej  *	Stat file op.
    632  1.1  thorpej  */
    633  1.1  thorpej static int
    634  1.1  thorpej dmio_stat(struct file *fp, struct stat *sb, struct proc *p)
    635  1.1  thorpej {
    636  1.1  thorpej 
    637  1.1  thorpej 	return (EOPNOTSUPP);
    638  1.1  thorpej }
    639  1.1  thorpej 
    640  1.1  thorpej /*
    641  1.1  thorpej  * dmio_close:
    642  1.1  thorpej  *
    643  1.1  thorpej  *	Close file op.
    644  1.1  thorpej  */
    645  1.1  thorpej static int
    646  1.1  thorpej dmio_close(struct file *fp, struct proc *p)
    647  1.1  thorpej {
    648  1.1  thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    649  1.1  thorpej 	struct dmio_usrreq_state *dus;
    650  1.1  thorpej 	struct dmover_session *dses;
    651  1.1  thorpej 	int s;
    652  1.1  thorpej 
    653  1.1  thorpej 	s = splsoftclock();
    654  1.1  thorpej 	simple_lock(&ds->ds_slock);
    655  1.1  thorpej 
    656  1.1  thorpej 	ds->ds_flags |= DMIO_STATE_DEAD;
    657  1.1  thorpej 
    658  1.1  thorpej 	/* Garbage-collect all the responses on the queue. */
    659  1.1  thorpej 	while ((dus = TAILQ_FIRST(&ds->ds_complete)) != NULL) {
    660  1.1  thorpej 		TAILQ_REMOVE(&ds->ds_complete, dus, dus_q);
    661  1.1  thorpej 		ds->ds_nreqs--;
    662  1.1  thorpej 		dmover_request_free(dus->dus_req);
    663  1.1  thorpej 		dmio_usrreq_fini(ds, dus);
    664  1.1  thorpej 	}
    665  1.1  thorpej 
    666  1.1  thorpej 	/*
    667  1.1  thorpej 	 * If there are any requests pending, we have to wait for
    668  1.1  thorpej 	 * them.  Don't free the dmio_state in this case.
    669  1.1  thorpej 	 */
    670  1.1  thorpej 	if (ds->ds_nreqs == 0) {
    671  1.1  thorpej 		dses = ds->ds_session;
    672  1.1  thorpej 		simple_unlock(&ds->ds_slock);
    673  1.1  thorpej 		pool_put(&dmio_state_pool, ds);
    674  1.1  thorpej 	} else {
    675  1.1  thorpej 		dses = NULL;
    676  1.1  thorpej 		simple_unlock(&ds->ds_slock);
    677  1.1  thorpej 	}
    678  1.1  thorpej 
    679  1.1  thorpej 	splx(s);
    680  1.1  thorpej 
    681  1.1  thorpej 	fp->f_data = NULL;
    682  1.1  thorpej 
    683  1.1  thorpej 	if (dses != NULL)
    684  1.1  thorpej 		dmover_session_destroy(dses);
    685  1.1  thorpej 
    686  1.1  thorpej 	return (0);
    687  1.1  thorpej }
    688  1.1  thorpej 
    689  1.1  thorpej static struct fileops dmio_fileops = {
    690  1.1  thorpej 	dmio_read,
    691  1.1  thorpej 	dmio_write,
    692  1.1  thorpej 	dmio_ioctl,
    693  1.1  thorpej 	dmio_fcntl,
    694  1.1  thorpej 	dmio_poll,
    695  1.1  thorpej 	dmio_stat,
    696  1.1  thorpej 	dmio_close,
    697  1.1  thorpej };
    698  1.1  thorpej 
    699  1.1  thorpej /*
    700  1.1  thorpej  * dmoverioopen:
    701  1.1  thorpej  *
    702  1.1  thorpej  *	Device switch open routine.
    703  1.1  thorpej  */
    704  1.1  thorpej int
    705  1.1  thorpej dmoverioopen(dev_t dev, int flag, int mode, struct proc *p)
    706  1.1  thorpej {
    707  1.1  thorpej 	struct dmio_state *ds;
    708  1.1  thorpej 	struct file *fp;
    709  1.1  thorpej 	int error, fd, s;
    710  1.1  thorpej 
    711  1.1  thorpej 	/* falloc() will use the descriptor for us. */
    712  1.1  thorpej 	if ((error = falloc(p, &fp, &fd)) != 0)
    713  1.1  thorpej 		return (error);
    714  1.1  thorpej 
    715  1.1  thorpej 	s = splsoftclock();
    716  1.1  thorpej 	ds = pool_get(&dmio_state_pool, PR_WAITOK);
    717  1.1  thorpej 	splx(s);
    718  1.1  thorpej 
    719  1.1  thorpej 	memset(ds, 0, sizeof(*ds));
    720  1.1  thorpej 	TAILQ_INIT(&ds->ds_pending);
    721  1.1  thorpej 	TAILQ_INIT(&ds->ds_complete);
    722  1.1  thorpej 
    723  1.1  thorpej 	fp->f_flag = FREAD | FWRITE;
    724  1.1  thorpej 	fp->f_type = DTYPE_MISC;
    725  1.1  thorpej 	fp->f_ops = &dmio_fileops;
    726  1.1  thorpej 	fp->f_data = (caddr_t) ds;
    727  1.1  thorpej 
    728  1.1  thorpej 	p->p_dupfd = fd;
    729  1.1  thorpej 	FILE_SET_MATURE(fp);
    730  1.1  thorpej 	FILE_UNUSE(fp, p);
    731  1.1  thorpej 
    732  1.1  thorpej 	return (ENXIO);
    733  1.1  thorpej }
    734