Home | History | Annotate | Line # | Download | only in dmover
dmover_io.c revision 1.40.18.2
      1  1.40.18.2     rmind /*	$NetBSD: dmover_io.c,v 1.40.18.2 2014/05/18 17:45:36 rmind 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.40.18.2     rmind __KERNEL_RCSID(0, "$NetBSD: dmover_io.c,v 1.40.18.2 2014/05/18 17:45:36 rmind 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.1   thorpej #include <sys/file.h>
     68        1.1   thorpej #include <sys/filedesc.h>
     69        1.1   thorpej #include <sys/filio.h>
     70        1.1   thorpej #include <sys/select.h>
     71        1.1   thorpej #include <sys/systm.h>
     72       1.21      yamt #include <sys/workqueue.h>
     73       1.21      yamt #include <sys/once.h>
     74       1.35    nonaka #include <sys/stat.h>
     75       1.35    nonaka #include <sys/kauth.h>
     76       1.40  jakllsch #include <sys/mutex.h>
     77       1.40  jakllsch #include <sys/condvar.h>
     78       1.21      yamt 
     79       1.39  uebayasi #include <uvm/uvm_extern.h>
     80       1.39  uebayasi 
     81        1.1   thorpej #include <dev/dmover/dmovervar.h>
     82        1.1   thorpej #include <dev/dmover/dmover_io.h>
     83        1.1   thorpej 
     84        1.1   thorpej struct dmio_usrreq_state {
     85       1.21      yamt 	union {
     86       1.21      yamt 		struct work u_work;
     87       1.21      yamt 		TAILQ_ENTRY(dmio_usrreq_state) u_q;
     88       1.21      yamt 	} dus_u;
     89       1.21      yamt #define	dus_q		dus_u.u_q
     90       1.21      yamt #define	dus_work	dus_u.u_work
     91        1.1   thorpej 	struct uio dus_uio_out;
     92        1.1   thorpej 	struct uio *dus_uio_in;
     93        1.1   thorpej 	struct dmover_request *dus_req;
     94        1.1   thorpej 	uint32_t dus_id;
     95       1.21      yamt 	struct vmspace *dus_vmspace;
     96        1.1   thorpej };
     97        1.1   thorpej 
     98        1.1   thorpej struct dmio_state {
     99        1.1   thorpej 	struct dmover_session *ds_session;
    100        1.1   thorpej 	TAILQ_HEAD(, dmio_usrreq_state) ds_pending;
    101        1.1   thorpej 	TAILQ_HEAD(, dmio_usrreq_state) ds_complete;
    102        1.1   thorpej 	struct selinfo ds_selq;
    103       1.19     perry 	volatile int ds_flags;
    104        1.1   thorpej 	u_int ds_nreqs;
    105       1.40  jakllsch 	kmutex_t ds_lock;
    106       1.40  jakllsch 	kcondvar_t ds_complete_cv;
    107       1.40  jakllsch 	kcondvar_t ds_nreqs_cv;
    108       1.33  christos 	struct timespec ds_atime;
    109       1.33  christos 	struct timespec ds_mtime;
    110       1.33  christos 	struct timespec ds_btime;
    111        1.1   thorpej };
    112        1.1   thorpej 
    113       1.21      yamt static ONCE_DECL(dmio_cleaner_control);
    114       1.21      yamt static struct workqueue *dmio_cleaner;
    115       1.21      yamt static int dmio_cleaner_init(void);
    116       1.40  jakllsch static struct dmio_state *dmio_state_get(void);
    117       1.40  jakllsch static void dmio_state_put(struct dmio_state *);
    118       1.21      yamt static void dmio_usrreq_fini1(struct work *wk, void *);
    119       1.21      yamt 
    120        1.1   thorpej #define	DMIO_STATE_SEL		0x0001
    121        1.1   thorpej #define	DMIO_STATE_DEAD		0x0002
    122        1.1   thorpej #define	DMIO_STATE_LARVAL	0x0004
    123        1.1   thorpej #define	DMIO_STATE_READ_WAIT	0x0008
    124        1.1   thorpej #define	DMIO_STATE_WRITE_WAIT	0x0010
    125        1.1   thorpej 
    126        1.1   thorpej #define	DMIO_NREQS_MAX		64	/* XXX pulled out of a hat */
    127        1.1   thorpej 
    128        1.1   thorpej struct pool dmio_state_pool;
    129        1.1   thorpej struct pool dmio_usrreq_state_pool;
    130        1.1   thorpej 
    131        1.1   thorpej void	dmoverioattach(int);
    132        1.5   gehenna 
    133        1.5   gehenna dev_type_open(dmoverioopen);
    134        1.5   gehenna 
    135        1.5   gehenna const struct cdevsw dmoverio_cdevsw = {
    136  1.40.18.2     rmind 	.d_open = dmoverioopen,
    137  1.40.18.2     rmind 	.d_close = noclose,
    138  1.40.18.2     rmind 	.d_read = noread,
    139  1.40.18.2     rmind 	.d_write = nowrite,
    140  1.40.18.2     rmind 	.d_ioctl = noioctl,
    141  1.40.18.2     rmind 	.d_stop = nostop,
    142  1.40.18.2     rmind 	.d_tty = notty,
    143  1.40.18.2     rmind 	.d_poll = nopoll,
    144  1.40.18.2     rmind 	.d_mmap = nommap,
    145  1.40.18.2     rmind 	.d_kqfilter = nokqfilter,
    146  1.40.18.2     rmind 	.d_flag = D_OTHER
    147        1.5   gehenna };
    148        1.1   thorpej 
    149        1.1   thorpej /*
    150        1.1   thorpej  * dmoverioattach:
    151        1.1   thorpej  *
    152        1.1   thorpej  *	Pseudo-device attach routine.
    153        1.1   thorpej  */
    154        1.1   thorpej void
    155        1.1   thorpej dmoverioattach(int count)
    156        1.1   thorpej {
    157        1.1   thorpej 
    158        1.1   thorpej 	pool_init(&dmio_state_pool, sizeof(struct dmio_state),
    159       1.26        ad 	    0, 0, 0, "dmiostate", NULL, IPL_SOFTCLOCK);
    160        1.1   thorpej 	pool_init(&dmio_usrreq_state_pool, sizeof(struct dmio_usrreq_state),
    161       1.26        ad 	    0, 0, 0, "dmiourstate", NULL, IPL_SOFTCLOCK);
    162        1.1   thorpej }
    163        1.1   thorpej 
    164        1.1   thorpej /*
    165       1.21      yamt  * dmio_cleaner_init:
    166       1.21      yamt  *
    167       1.21      yamt  *	Create cleaner thread.
    168       1.21      yamt  */
    169       1.21      yamt static int
    170       1.21      yamt dmio_cleaner_init(void)
    171       1.21      yamt {
    172       1.21      yamt 
    173       1.21      yamt 	return workqueue_create(&dmio_cleaner, "dmioclean", dmio_usrreq_fini1,
    174       1.24      yamt 	    NULL, PWAIT, IPL_SOFTCLOCK, 0);
    175       1.21      yamt }
    176       1.21      yamt 
    177       1.40  jakllsch static struct dmio_state *
    178       1.40  jakllsch dmio_state_get(void)
    179       1.40  jakllsch {
    180       1.40  jakllsch 	struct dmio_state *ds;
    181       1.40  jakllsch 
    182       1.40  jakllsch 	ds = pool_get(&dmio_state_pool, PR_WAITOK);
    183       1.40  jakllsch 
    184       1.40  jakllsch 	memset(ds, 0, sizeof(*ds));
    185       1.40  jakllsch 
    186       1.40  jakllsch 	getnanotime(&ds->ds_btime);
    187       1.40  jakllsch 	ds->ds_atime = ds->ds_mtime = ds->ds_btime;
    188       1.40  jakllsch 
    189       1.40  jakllsch 	mutex_init(&ds->ds_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
    190       1.40  jakllsch 	cv_init(&ds->ds_complete_cv, "dmvrrd");
    191       1.40  jakllsch 	cv_init(&ds->ds_nreqs_cv, "dmiowr");
    192       1.40  jakllsch 	TAILQ_INIT(&ds->ds_pending);
    193       1.40  jakllsch 	TAILQ_INIT(&ds->ds_complete);
    194       1.40  jakllsch 	selinit(&ds->ds_selq);
    195       1.40  jakllsch 
    196       1.40  jakllsch 	return ds;
    197       1.40  jakllsch }
    198       1.40  jakllsch 
    199       1.40  jakllsch static void
    200       1.40  jakllsch dmio_state_put(struct dmio_state *ds)
    201       1.40  jakllsch {
    202       1.40  jakllsch 
    203       1.40  jakllsch 	seldestroy(&ds->ds_selq);
    204       1.40  jakllsch 	cv_destroy(&ds->ds_nreqs_cv);
    205       1.40  jakllsch 	cv_destroy(&ds->ds_complete_cv);
    206       1.40  jakllsch 	mutex_destroy(&ds->ds_lock);
    207       1.40  jakllsch 
    208       1.40  jakllsch 	pool_put(&dmio_state_pool, ds);
    209       1.40  jakllsch }
    210       1.40  jakllsch 
    211       1.21      yamt /*
    212        1.1   thorpej  * dmio_usrreq_init:
    213        1.1   thorpej  *
    214        1.1   thorpej  *	Build a request structure.
    215        1.1   thorpej  */
    216        1.1   thorpej static int
    217        1.1   thorpej dmio_usrreq_init(struct file *fp, struct dmio_usrreq_state *dus,
    218        1.1   thorpej     struct dmio_usrreq *req, struct dmover_request *dreq)
    219        1.1   thorpej {
    220        1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    221        1.1   thorpej 	struct dmover_session *dses = ds->ds_session;
    222        1.1   thorpej 	struct uio *uio_out = &dus->dus_uio_out;
    223        1.1   thorpej 	struct uio *uio_in;
    224        1.1   thorpej 	dmio_buffer inbuf;
    225        1.1   thorpej 	size_t len;
    226        1.7   thorpej 	int i, error;
    227        1.7   thorpej 	u_int j;
    228        1.1   thorpej 
    229        1.1   thorpej 	/* XXX How should malloc interact w/ FNONBLOCK? */
    230        1.1   thorpej 
    231       1.21      yamt 	error = RUN_ONCE(&dmio_cleaner_control, dmio_cleaner_init);
    232       1.21      yamt 	if (error) {
    233       1.21      yamt 		return error;
    234       1.21      yamt 	}
    235       1.21      yamt 
    236       1.21      yamt 	error = proc_vmspace_getref(curproc, &dus->dus_vmspace);
    237       1.21      yamt 	if (error) {
    238       1.21      yamt 		return error;
    239       1.21      yamt 	}
    240       1.21      yamt 
    241       1.12   thorpej 	if (req->req_outbuf.dmbuf_iovcnt != 0) {
    242       1.12   thorpej 		if (req->req_outbuf.dmbuf_iovcnt > IOV_MAX)
    243       1.12   thorpej 			return (EINVAL);
    244       1.12   thorpej 		len = sizeof(struct iovec) * req->req_outbuf.dmbuf_iovcnt;
    245       1.12   thorpej 		uio_out->uio_iov = malloc(len, M_TEMP, M_WAITOK);
    246       1.12   thorpej 		error = copyin(req->req_outbuf.dmbuf_iov, uio_out->uio_iov,
    247       1.12   thorpej 		    len);
    248       1.12   thorpej 		if (error) {
    249       1.12   thorpej 			free(uio_out->uio_iov, M_TEMP);
    250       1.12   thorpej 			return (error);
    251       1.12   thorpej 		}
    252        1.1   thorpej 
    253       1.12   thorpej 		for (j = 0, len = 0; j < req->req_outbuf.dmbuf_iovcnt; j++) {
    254       1.12   thorpej 			len += uio_out->uio_iov[j].iov_len;
    255       1.12   thorpej 			if (len > SSIZE_MAX) {
    256       1.12   thorpej 				free(uio_out->uio_iov, M_TEMP);
    257       1.12   thorpej 				return (EINVAL);
    258       1.12   thorpej 			}
    259       1.12   thorpej 		}
    260        1.1   thorpej 
    261       1.12   thorpej 		uio_out->uio_iovcnt = req->req_outbuf.dmbuf_iovcnt;
    262       1.12   thorpej 		uio_out->uio_resid = len;
    263       1.12   thorpej 		uio_out->uio_rw = UIO_READ;
    264       1.21      yamt 		uio_out->uio_vmspace = dus->dus_vmspace;
    265       1.21      yamt 
    266       1.12   thorpej 		dreq->dreq_outbuf_type = DMOVER_BUF_UIO;
    267       1.12   thorpej 		dreq->dreq_outbuf.dmbuf_uio = uio_out;
    268       1.12   thorpej 	} else {
    269       1.12   thorpej 		uio_out->uio_iov = NULL;
    270       1.12   thorpej 		uio_out = NULL;
    271       1.12   thorpej 		dreq->dreq_outbuf_type = DMOVER_BUF_NONE;
    272        1.1   thorpej 	}
    273        1.1   thorpej 
    274       1.12   thorpej 	memcpy(dreq->dreq_immediate, req->req_immediate,
    275       1.12   thorpej 	    sizeof(dreq->dreq_immediate));
    276        1.1   thorpej 
    277        1.1   thorpej 	if (dses->dses_ninputs == 0) {
    278       1.12   thorpej 		/* No inputs; all done. */
    279        1.1   thorpej 		return (0);
    280        1.1   thorpej 	}
    281        1.1   thorpej 
    282        1.1   thorpej 	dreq->dreq_inbuf_type = DMOVER_BUF_UIO;
    283        1.1   thorpej 
    284        1.1   thorpej 	dus->dus_uio_in = malloc(sizeof(struct uio) * dses->dses_ninputs,
    285        1.1   thorpej 	    M_TEMP, M_WAITOK);
    286        1.1   thorpej 	memset(dus->dus_uio_in, 0, sizeof(struct uio) * dses->dses_ninputs);
    287        1.1   thorpej 
    288        1.1   thorpej 	for (i = 0; i < dses->dses_ninputs; i++) {
    289        1.1   thorpej 		uio_in = &dus->dus_uio_in[i];
    290        1.1   thorpej 
    291        1.1   thorpej 		error = copyin(&req->req_inbuf[i], &inbuf, sizeof(inbuf));
    292        1.1   thorpej 		if (error)
    293        1.1   thorpej 			goto bad;
    294        1.1   thorpej 
    295        1.1   thorpej 		if (inbuf.dmbuf_iovcnt > IOV_MAX) {
    296        1.1   thorpej 			error = EINVAL;
    297        1.1   thorpej 			goto bad;
    298        1.1   thorpej 		}
    299        1.1   thorpej 		len = sizeof(struct iovec) * inbuf.dmbuf_iovcnt;
    300        1.1   thorpej 		if (len == 0) {
    301        1.1   thorpej 			error = EINVAL;
    302        1.1   thorpej 			goto bad;
    303        1.1   thorpej 		}
    304        1.1   thorpej 		uio_in->uio_iov = malloc(len, M_TEMP, M_WAITOK);
    305        1.1   thorpej 
    306        1.1   thorpej 		error = copyin(inbuf.dmbuf_iov, uio_in->uio_iov, len);
    307        1.1   thorpej 		if (error) {
    308        1.1   thorpej 			free(uio_in->uio_iov, M_TEMP);
    309        1.1   thorpej 			goto bad;
    310        1.1   thorpej 		}
    311        1.1   thorpej 
    312       1.12   thorpej 		for (j = 0, len = 0; j < inbuf.dmbuf_iovcnt; j++) {
    313        1.1   thorpej 			len += uio_in->uio_iov[j].iov_len;
    314        1.1   thorpej 			if (len > SSIZE_MAX) {
    315        1.1   thorpej 				free(uio_in->uio_iov, M_TEMP);
    316        1.1   thorpej 				error = EINVAL;
    317        1.1   thorpej 				goto bad;
    318        1.1   thorpej 			}
    319        1.1   thorpej 		}
    320        1.1   thorpej 
    321       1.12   thorpej 		if (uio_out != NULL && len != uio_out->uio_resid) {
    322        1.1   thorpej 			free(uio_in->uio_iov, M_TEMP);
    323        1.1   thorpej 			error = EINVAL;
    324        1.1   thorpej 			goto bad;
    325        1.1   thorpej 		}
    326        1.1   thorpej 
    327        1.1   thorpej 		uio_in->uio_iovcnt = inbuf.dmbuf_iovcnt;
    328        1.1   thorpej 		uio_in->uio_resid = len;
    329        1.1   thorpej 		uio_in->uio_rw = UIO_WRITE;
    330       1.21      yamt 		uio_in->uio_vmspace = dus->dus_vmspace;
    331        1.1   thorpej 
    332        1.1   thorpej 		dreq->dreq_inbuf[i].dmbuf_uio = uio_in;
    333        1.1   thorpej 	}
    334        1.1   thorpej 
    335        1.1   thorpej 	return (0);
    336        1.1   thorpej 
    337        1.1   thorpej  bad:
    338        1.1   thorpej 	if (i > 0) {
    339        1.1   thorpej 		for (--i; i >= 0; i--) {
    340        1.1   thorpej 			uio_in = &dus->dus_uio_in[i];
    341        1.1   thorpej 			free(uio_in->uio_iov, M_TEMP);
    342        1.1   thorpej 		}
    343        1.1   thorpej 	}
    344        1.1   thorpej 	free(dus->dus_uio_in, M_TEMP);
    345       1.12   thorpej 	if (uio_out != NULL)
    346       1.12   thorpej 		free(uio_out->uio_iov, M_TEMP);
    347       1.21      yamt 	uvmspace_free(dus->dus_vmspace);
    348        1.1   thorpej 	return (error);
    349        1.1   thorpej }
    350        1.1   thorpej 
    351        1.1   thorpej /*
    352        1.1   thorpej  * dmio_usrreq_fini:
    353        1.1   thorpej  *
    354        1.1   thorpej  *	Tear down a request.  Must be called at splsoftclock().
    355        1.1   thorpej  */
    356        1.1   thorpej static void
    357        1.1   thorpej dmio_usrreq_fini(struct dmio_state *ds, struct dmio_usrreq_state *dus)
    358        1.1   thorpej {
    359        1.1   thorpej 	struct dmover_session *dses = ds->ds_session;
    360        1.1   thorpej 	struct uio *uio_out = &dus->dus_uio_out;
    361        1.1   thorpej 	struct uio *uio_in;
    362        1.1   thorpej 	int i;
    363        1.1   thorpej 
    364       1.12   thorpej 	if (uio_out->uio_iov != NULL)
    365       1.12   thorpej 		free(uio_out->uio_iov, M_TEMP);
    366        1.1   thorpej 
    367       1.21      yamt 	if (dses->dses_ninputs) {
    368       1.21      yamt 		for (i = 0; i < dses->dses_ninputs; i++) {
    369       1.21      yamt 			uio_in = &dus->dus_uio_in[i];
    370       1.21      yamt 			free(uio_in->uio_iov, M_TEMP);
    371       1.21      yamt 		}
    372       1.21      yamt 		free(dus->dus_uio_in, M_TEMP);
    373        1.1   thorpej 	}
    374        1.1   thorpej 
    375       1.27     rmind 	workqueue_enqueue(dmio_cleaner, &dus->dus_work, NULL);
    376       1.21      yamt }
    377       1.21      yamt 
    378       1.21      yamt static void
    379       1.21      yamt dmio_usrreq_fini1(struct work *wk, void *dummy)
    380       1.21      yamt {
    381       1.21      yamt 	struct dmio_usrreq_state *dus = (void *)wk;
    382        1.1   thorpej 
    383       1.21      yamt 	KASSERT(wk == &dus->dus_work);
    384        1.1   thorpej 
    385       1.21      yamt 	uvmspace_free(dus->dus_vmspace);
    386        1.1   thorpej 	pool_put(&dmio_usrreq_state_pool, dus);
    387        1.1   thorpej }
    388        1.1   thorpej 
    389        1.1   thorpej /*
    390        1.1   thorpej  * dmio_read:
    391        1.1   thorpej  *
    392        1.1   thorpej  *	Read file op.
    393        1.1   thorpej  */
    394        1.1   thorpej static int
    395        1.1   thorpej dmio_read(struct file *fp, off_t *offp, struct uio *uio,
    396       1.22      elad     kauth_cred_t cred, int flags)
    397        1.1   thorpej {
    398        1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    399        1.1   thorpej 	struct dmio_usrreq_state *dus;
    400        1.1   thorpej 	struct dmover_request *dreq;
    401        1.1   thorpej 	struct dmio_usrresp resp;
    402       1.40  jakllsch 	int error = 0, progress = 0;
    403        1.1   thorpej 
    404        1.1   thorpej 	if ((uio->uio_resid % sizeof(resp)) != 0)
    405        1.1   thorpej 		return (EINVAL);
    406        1.1   thorpej 
    407        1.1   thorpej 	if (ds->ds_session == NULL)
    408        1.1   thorpej 		return (ENXIO);
    409        1.1   thorpej 
    410       1.33  christos 	getnanotime(&ds->ds_atime);
    411       1.40  jakllsch 	mutex_enter(&ds->ds_lock);
    412        1.1   thorpej 
    413        1.1   thorpej 	while (uio->uio_resid != 0) {
    414        1.1   thorpej 
    415        1.1   thorpej 		for (;;) {
    416        1.1   thorpej 			dus = TAILQ_FIRST(&ds->ds_complete);
    417        1.1   thorpej 			if (dus == NULL) {
    418        1.1   thorpej 				if (fp->f_flag & FNONBLOCK) {
    419        1.1   thorpej 					error = progress ? 0 : EWOULDBLOCK;
    420        1.1   thorpej 					goto out;
    421        1.1   thorpej 				}
    422        1.9       scw 				ds->ds_flags |= DMIO_STATE_READ_WAIT;
    423       1.40  jakllsch 				error = cv_wait_sig(&ds->ds_complete_cv, &ds->ds_lock);
    424        1.1   thorpej 				if (error)
    425        1.1   thorpej 					goto out;
    426        1.1   thorpej 				continue;
    427        1.1   thorpej 			}
    428        1.1   thorpej 			/* Have a completed request. */
    429        1.1   thorpej 			TAILQ_REMOVE(&ds->ds_complete, dus, dus_q);
    430        1.1   thorpej 			ds->ds_nreqs--;
    431        1.1   thorpej 			if (ds->ds_flags & DMIO_STATE_WRITE_WAIT) {
    432        1.1   thorpej 				ds->ds_flags &= ~DMIO_STATE_WRITE_WAIT;
    433       1.40  jakllsch 				cv_broadcast(&ds->ds_nreqs_cv);
    434        1.1   thorpej 			}
    435        1.1   thorpej 			if (ds->ds_flags & DMIO_STATE_SEL) {
    436        1.1   thorpej 				ds->ds_flags &= ~DMIO_STATE_SEL;
    437       1.30     rmind 				selnotify(&ds->ds_selq, POLLIN | POLLRDNORM, 0);
    438        1.1   thorpej 			}
    439        1.1   thorpej 			break;
    440        1.1   thorpej 		}
    441        1.1   thorpej 
    442        1.1   thorpej 		dreq = dus->dus_req;
    443        1.1   thorpej 		resp.resp_id = dus->dus_id;
    444       1.12   thorpej 		if (dreq->dreq_flags & DMOVER_REQ_ERROR)
    445       1.12   thorpej 			resp.resp_error = dreq->dreq_error;
    446       1.12   thorpej 		else {
    447       1.12   thorpej 			resp.resp_error = 0;
    448       1.12   thorpej 			memcpy(resp.resp_immediate, dreq->dreq_immediate,
    449       1.12   thorpej 			    sizeof(resp.resp_immediate));
    450       1.12   thorpej 		}
    451        1.1   thorpej 
    452        1.1   thorpej 		dmio_usrreq_fini(ds, dus);
    453        1.1   thorpej 
    454       1.40  jakllsch 		mutex_exit(&ds->ds_lock);
    455        1.1   thorpej 
    456        1.1   thorpej 		progress = 1;
    457        1.1   thorpej 
    458        1.1   thorpej 		dmover_request_free(dreq);
    459        1.1   thorpej 
    460        1.1   thorpej 		error = uiomove(&resp, sizeof(resp), uio);
    461        1.1   thorpej 		if (error)
    462        1.1   thorpej 			return (error);
    463        1.1   thorpej 
    464       1.40  jakllsch 		mutex_enter(&ds->ds_lock);
    465        1.1   thorpej 	}
    466        1.1   thorpej 
    467        1.1   thorpej  out:
    468       1.40  jakllsch 	mutex_exit(&ds->ds_lock);
    469        1.1   thorpej 
    470        1.1   thorpej 	return (error);
    471        1.1   thorpej }
    472        1.1   thorpej 
    473        1.1   thorpej /*
    474        1.1   thorpej  * dmio_usrreq_done:
    475        1.1   thorpej  *
    476        1.1   thorpej  *	Dmover completion callback.
    477        1.1   thorpej  */
    478        1.1   thorpej static void
    479        1.1   thorpej dmio_usrreq_done(struct dmover_request *dreq)
    480        1.1   thorpej {
    481        1.1   thorpej 	struct dmio_usrreq_state *dus = dreq->dreq_cookie;
    482        1.1   thorpej 	struct dmio_state *ds = dreq->dreq_session->dses_cookie;
    483        1.1   thorpej 
    484        1.1   thorpej 	/* We're already at splsoftclock(). */
    485        1.1   thorpej 
    486       1.40  jakllsch 	mutex_enter(&ds->ds_lock);
    487        1.1   thorpej 	TAILQ_REMOVE(&ds->ds_pending, dus, dus_q);
    488        1.1   thorpej 	if (ds->ds_flags & DMIO_STATE_DEAD) {
    489       1.40  jakllsch 		int nreqs = --ds->ds_nreqs;
    490       1.40  jakllsch 		mutex_exit(&ds->ds_lock);
    491        1.1   thorpej 		dmio_usrreq_fini(ds, dus);
    492        1.1   thorpej 		dmover_request_free(dreq);
    493       1.40  jakllsch 		if (nreqs == 0) {
    494       1.40  jakllsch 			dmio_state_put(ds);
    495        1.1   thorpej 		}
    496       1.40  jakllsch 		return;
    497       1.40  jakllsch 	}
    498       1.40  jakllsch 
    499       1.40  jakllsch 	TAILQ_INSERT_TAIL(&ds->ds_complete, dus, dus_q);
    500       1.40  jakllsch 	if (ds->ds_flags & DMIO_STATE_READ_WAIT) {
    501       1.40  jakllsch 		ds->ds_flags &= ~DMIO_STATE_READ_WAIT;
    502       1.40  jakllsch 		cv_broadcast(&ds->ds_complete_cv);
    503       1.40  jakllsch 	}
    504       1.40  jakllsch 	if (ds->ds_flags & DMIO_STATE_SEL) {
    505       1.40  jakllsch 		ds->ds_flags &= ~DMIO_STATE_SEL;
    506       1.40  jakllsch 		selnotify(&ds->ds_selq, POLLOUT | POLLWRNORM, 0);
    507        1.1   thorpej 	}
    508       1.40  jakllsch 	mutex_exit(&ds->ds_lock);
    509        1.1   thorpej }
    510        1.1   thorpej 
    511        1.1   thorpej /*
    512        1.1   thorpej  * dmio_write:
    513        1.1   thorpej  *
    514        1.1   thorpej  *	Write file op.
    515        1.1   thorpej  */
    516        1.1   thorpej static int
    517        1.1   thorpej dmio_write(struct file *fp, off_t *offp, struct uio *uio,
    518       1.22      elad     kauth_cred_t cred, int flags)
    519        1.1   thorpej {
    520        1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    521        1.1   thorpej 	struct dmio_usrreq_state *dus;
    522        1.1   thorpej 	struct dmover_request *dreq;
    523        1.1   thorpej 	struct dmio_usrreq req;
    524       1.40  jakllsch 	int error = 0, progress = 0;
    525        1.1   thorpej 
    526        1.1   thorpej 	if ((uio->uio_resid % sizeof(req)) != 0)
    527        1.1   thorpej 		return (EINVAL);
    528        1.1   thorpej 
    529        1.1   thorpej 	if (ds->ds_session == NULL)
    530        1.1   thorpej 		return (ENXIO);
    531        1.1   thorpej 
    532       1.33  christos 	getnanotime(&ds->ds_mtime);
    533       1.40  jakllsch 	mutex_enter(&ds->ds_lock);
    534        1.1   thorpej 
    535        1.1   thorpej 	while (uio->uio_resid != 0) {
    536        1.1   thorpej 
    537        1.1   thorpej 		if (ds->ds_nreqs == DMIO_NREQS_MAX) {
    538        1.1   thorpej 			if (fp->f_flag & FNONBLOCK) {
    539        1.1   thorpej 				error = progress ? 0 : EWOULDBLOCK;
    540        1.1   thorpej 				break;
    541        1.1   thorpej 			}
    542        1.1   thorpej 			ds->ds_flags |= DMIO_STATE_WRITE_WAIT;
    543       1.40  jakllsch 			error = cv_wait_sig(&ds->ds_complete_cv, &ds->ds_lock);
    544        1.1   thorpej 			if (error)
    545        1.1   thorpej 				break;
    546        1.1   thorpej 			continue;
    547        1.1   thorpej 		}
    548        1.1   thorpej 
    549        1.1   thorpej 		ds->ds_nreqs++;
    550        1.1   thorpej 
    551       1.40  jakllsch 		mutex_exit(&ds->ds_lock);
    552        1.1   thorpej 
    553        1.1   thorpej 		progress = 1;
    554        1.1   thorpej 
    555        1.1   thorpej 		error = uiomove(&req, sizeof(req), uio);
    556        1.1   thorpej 		if (error) {
    557       1.40  jakllsch 			mutex_enter(&ds->ds_lock);
    558        1.1   thorpej 			ds->ds_nreqs--;
    559        1.1   thorpej 			break;
    560        1.1   thorpej 		}
    561        1.1   thorpej 
    562        1.1   thorpej 		/* XXX How should this interact with FNONBLOCK? */
    563        1.1   thorpej 		dreq = dmover_request_alloc(ds->ds_session, NULL);
    564        1.1   thorpej 		if (dreq == NULL) {
    565        1.1   thorpej 			/* XXX */
    566        1.1   thorpej 			ds->ds_nreqs--;
    567        1.1   thorpej 			error = ENOMEM;
    568       1.40  jakllsch 			return error;
    569        1.1   thorpej 		}
    570        1.1   thorpej 		dus = pool_get(&dmio_usrreq_state_pool, PR_WAITOK);
    571        1.1   thorpej 
    572        1.1   thorpej 		error = dmio_usrreq_init(fp, dus, &req, dreq);
    573        1.1   thorpej 		if (error) {
    574        1.1   thorpej 			dmover_request_free(dreq);
    575        1.1   thorpej 			pool_put(&dmio_usrreq_state_pool, dus);
    576       1.40  jakllsch 			return error;
    577        1.1   thorpej 		}
    578        1.1   thorpej 
    579        1.1   thorpej 		dreq->dreq_callback = dmio_usrreq_done;
    580        1.1   thorpej 		dreq->dreq_cookie = dus;
    581        1.1   thorpej 
    582        1.1   thorpej 		dus->dus_req = dreq;
    583        1.1   thorpej 		dus->dus_id = req.req_id;
    584        1.1   thorpej 
    585       1.40  jakllsch 		mutex_enter(&ds->ds_lock);
    586        1.1   thorpej 
    587        1.1   thorpej 		TAILQ_INSERT_TAIL(&ds->ds_pending, dus, dus_q);
    588        1.1   thorpej 
    589       1.40  jakllsch 		mutex_exit(&ds->ds_lock);
    590        1.1   thorpej 
    591        1.1   thorpej 		dmover_process(dreq);
    592        1.1   thorpej 
    593       1.40  jakllsch 		mutex_enter(&ds->ds_lock);
    594        1.1   thorpej 	}
    595        1.1   thorpej 
    596       1.40  jakllsch 	mutex_exit(&ds->ds_lock);
    597        1.1   thorpej 
    598        1.1   thorpej 	return (error);
    599        1.1   thorpej }
    600        1.1   thorpej 
    601       1.33  christos static int
    602       1.33  christos dmio_stat(struct file *fp, struct stat *st)
    603       1.33  christos {
    604       1.33  christos 	struct dmio_state *ds = fp->f_data;
    605       1.33  christos 
    606  1.40.18.1     rmind 	(void)memset(st, 0, sizeof(*st));
    607       1.33  christos 	KERNEL_LOCK(1, NULL);
    608       1.33  christos 	st->st_dev = makedev(cdevsw_lookup_major(&dmoverio_cdevsw), 0);
    609       1.35    nonaka 	st->st_atimespec = ds->ds_atime;
    610       1.35    nonaka 	st->st_mtimespec = ds->ds_mtime;
    611       1.35    nonaka 	st->st_ctimespec = st->st_birthtimespec = ds->ds_btime;
    612       1.34  christos 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
    613       1.34  christos 	st->st_gid = kauth_cred_getegid(fp->f_cred);
    614       1.35    nonaka 	KERNEL_UNLOCK_ONE(NULL);
    615       1.33  christos 	return 0;
    616       1.33  christos }
    617       1.33  christos 
    618        1.1   thorpej /*
    619        1.1   thorpej  * dmio_ioctl:
    620        1.1   thorpej  *
    621        1.1   thorpej  *	Ioctl file op.
    622        1.1   thorpej  */
    623        1.1   thorpej static int
    624       1.31        ad dmio_ioctl(struct file *fp, u_long cmd, void *data)
    625        1.1   thorpej {
    626        1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    627       1.40  jakllsch 	int error;
    628        1.1   thorpej 
    629        1.1   thorpej 	switch (cmd) {
    630        1.1   thorpej 	case FIONBIO:
    631        1.1   thorpej 	case FIOASYNC:
    632        1.1   thorpej 		return (0);
    633        1.1   thorpej 
    634        1.1   thorpej 	case DMIO_SETFUNC:
    635        1.1   thorpej 	    {
    636        1.8       dsl 		struct dmio_setfunc *dsf = data;
    637        1.1   thorpej 		struct dmover_session *dses;
    638        1.1   thorpej 
    639       1.40  jakllsch 		mutex_enter(&ds->ds_lock);
    640        1.1   thorpej 
    641        1.1   thorpej 		if (ds->ds_session != NULL ||
    642        1.1   thorpej 		    (ds->ds_flags & DMIO_STATE_LARVAL) != 0) {
    643       1.40  jakllsch 			mutex_exit(&ds->ds_lock);
    644        1.1   thorpej 			return (EBUSY);
    645        1.1   thorpej 		}
    646        1.1   thorpej 
    647        1.1   thorpej 		ds->ds_flags |= DMIO_STATE_LARVAL;
    648        1.1   thorpej 
    649       1.40  jakllsch 		mutex_exit(&ds->ds_lock);
    650        1.1   thorpej 
    651        1.1   thorpej 		dsf->dsf_name[DMIO_MAX_FUNCNAME - 1] = '\0';
    652        1.1   thorpej 		error = dmover_session_create(dsf->dsf_name, &dses);
    653        1.1   thorpej 
    654       1.40  jakllsch 		mutex_enter(&ds->ds_lock);
    655        1.1   thorpej 
    656        1.1   thorpej 		if (error == 0) {
    657        1.1   thorpej 			dses->dses_cookie = ds;
    658        1.1   thorpej 			ds->ds_session = dses;
    659        1.1   thorpej 		}
    660        1.1   thorpej 		ds->ds_flags &= ~DMIO_STATE_LARVAL;
    661        1.1   thorpej 
    662       1.40  jakllsch 		mutex_exit(&ds->ds_lock);
    663        1.1   thorpej 		break;
    664        1.1   thorpej 	    }
    665        1.1   thorpej 
    666        1.1   thorpej 	default:
    667        1.1   thorpej 		error = ENOTTY;
    668        1.1   thorpej 	}
    669        1.1   thorpej 
    670        1.1   thorpej 	return (error);
    671        1.1   thorpej }
    672        1.1   thorpej 
    673        1.1   thorpej /*
    674        1.1   thorpej  * dmio_poll:
    675        1.1   thorpej  *
    676        1.1   thorpej  *	Poll file op.
    677        1.1   thorpej  */
    678        1.1   thorpej static int
    679       1.31        ad dmio_poll(struct file *fp, int events)
    680        1.1   thorpej {
    681        1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    682       1.40  jakllsch 	int revents = 0;
    683        1.1   thorpej 
    684        1.1   thorpej 	if ((events & (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)) == 0)
    685        1.1   thorpej 		return (revents);
    686        1.1   thorpej 
    687       1.40  jakllsch 	mutex_enter(&ds->ds_lock);
    688        1.1   thorpej 
    689        1.1   thorpej 	if (ds->ds_flags & DMIO_STATE_DEAD) {
    690        1.1   thorpej 		/* EOF */
    691        1.1   thorpej 		revents |= events & (POLLIN | POLLRDNORM |
    692        1.1   thorpej 		    POLLOUT | POLLWRNORM);
    693        1.1   thorpej 		goto out;
    694        1.1   thorpej 	}
    695        1.1   thorpej 
    696        1.1   thorpej 	/* We can read if there are completed requests. */
    697        1.1   thorpej 	if (events & (POLLIN | POLLRDNORM))
    698        1.1   thorpej 		if (TAILQ_EMPTY(&ds->ds_complete) == 0)
    699        1.1   thorpej 			revents |= events & (POLLIN | POLLRDNORM);
    700        1.1   thorpej 
    701        1.1   thorpej 	/*
    702        1.1   thorpej 	 * We can write if there is there are fewer then DMIO_NREQS_MAX
    703        1.1   thorpej 	 * are already in the queue.
    704        1.1   thorpej 	 */
    705        1.1   thorpej 	if (events & (POLLOUT | POLLWRNORM))
    706        1.1   thorpej 		if (ds->ds_nreqs < DMIO_NREQS_MAX)
    707        1.1   thorpej 			revents |= events & (POLLOUT | POLLWRNORM);
    708        1.1   thorpej 
    709        1.1   thorpej 	if (revents == 0) {
    710       1.31        ad 		selrecord(curlwp, &ds->ds_selq);
    711        1.1   thorpej 		ds->ds_flags |= DMIO_STATE_SEL;
    712        1.1   thorpej 	}
    713        1.1   thorpej 
    714        1.1   thorpej  out:
    715       1.40  jakllsch 	mutex_exit(&ds->ds_lock);
    716        1.1   thorpej 
    717        1.1   thorpej 	return (revents);
    718        1.1   thorpej }
    719        1.1   thorpej 
    720        1.1   thorpej /*
    721        1.1   thorpej  * dmio_close:
    722        1.1   thorpej  *
    723        1.1   thorpej  *	Close file op.
    724        1.1   thorpej  */
    725        1.1   thorpej static int
    726       1.31        ad dmio_close(struct file *fp)
    727        1.1   thorpej {
    728        1.1   thorpej 	struct dmio_state *ds = (struct dmio_state *) fp->f_data;
    729        1.1   thorpej 	struct dmio_usrreq_state *dus;
    730        1.1   thorpej 	struct dmover_session *dses;
    731        1.1   thorpej 
    732       1.40  jakllsch 	mutex_enter(&ds->ds_lock);
    733        1.1   thorpej 
    734        1.1   thorpej 	ds->ds_flags |= DMIO_STATE_DEAD;
    735        1.1   thorpej 
    736        1.1   thorpej 	/* Garbage-collect all the responses on the queue. */
    737        1.1   thorpej 	while ((dus = TAILQ_FIRST(&ds->ds_complete)) != NULL) {
    738        1.1   thorpej 		TAILQ_REMOVE(&ds->ds_complete, dus, dus_q);
    739        1.1   thorpej 		ds->ds_nreqs--;
    740       1.40  jakllsch 		mutex_exit(&ds->ds_lock);
    741        1.1   thorpej 		dmover_request_free(dus->dus_req);
    742        1.1   thorpej 		dmio_usrreq_fini(ds, dus);
    743       1.40  jakllsch 		mutex_enter(&ds->ds_lock);
    744        1.1   thorpej 	}
    745        1.1   thorpej 
    746        1.1   thorpej 	/*
    747        1.1   thorpej 	 * If there are any requests pending, we have to wait for
    748        1.1   thorpej 	 * them.  Don't free the dmio_state in this case.
    749        1.1   thorpej 	 */
    750        1.1   thorpej 	if (ds->ds_nreqs == 0) {
    751        1.1   thorpej 		dses = ds->ds_session;
    752       1.40  jakllsch 		mutex_exit(&ds->ds_lock);
    753       1.40  jakllsch 		dmio_state_put(ds);
    754        1.1   thorpej 	} else {
    755        1.1   thorpej 		dses = NULL;
    756       1.40  jakllsch 		mutex_exit(&ds->ds_lock);
    757        1.1   thorpej 	}
    758        1.1   thorpej 
    759        1.1   thorpej 	fp->f_data = NULL;
    760        1.1   thorpej 
    761        1.1   thorpej 	if (dses != NULL)
    762        1.1   thorpej 		dmover_session_destroy(dses);
    763        1.1   thorpej 
    764        1.1   thorpej 	return (0);
    765        1.1   thorpej }
    766        1.1   thorpej 
    767       1.15  christos static const struct fileops dmio_fileops = {
    768       1.32        ad 	.fo_read = dmio_read,
    769       1.32        ad 	.fo_write = dmio_write,
    770       1.32        ad 	.fo_ioctl = dmio_ioctl,
    771       1.32        ad 	.fo_fcntl = fnullop_fcntl,
    772       1.32        ad 	.fo_poll = dmio_poll,
    773       1.33  christos 	.fo_stat = dmio_stat,
    774       1.32        ad 	.fo_close = dmio_close,
    775       1.32        ad 	.fo_kqfilter = fnullop_kqfilter,
    776       1.37       dsl 	.fo_restart = fnullop_restart,
    777        1.1   thorpej };
    778        1.1   thorpej 
    779        1.1   thorpej /*
    780        1.1   thorpej  * dmoverioopen:
    781        1.1   thorpej  *
    782        1.1   thorpej  *	Device switch open routine.
    783        1.1   thorpej  */
    784        1.1   thorpej int
    785       1.18  christos dmoverioopen(dev_t dev, int flag, int mode, struct lwp *l)
    786        1.1   thorpej {
    787        1.1   thorpej 	struct dmio_state *ds;
    788        1.1   thorpej 	struct file *fp;
    789       1.40  jakllsch 	int error, fd;
    790        1.1   thorpej 
    791       1.31        ad 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    792        1.1   thorpej 		return (error);
    793        1.1   thorpej 
    794       1.40  jakllsch 	ds = dmio_state_get();
    795        1.1   thorpej 
    796       1.31        ad 	return fd_clone(fp, fd, flag, &dmio_fileops, ds);
    797        1.1   thorpej }
    798