Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_bio.c revision 1.7.2.3
      1  1.7.2.2  tls /*	$NetBSD: rumpuser_bio.c,v 1.7.2.3 2014/08/20 00:02:21 tls Exp $	*/
      2  1.7.2.2  tls 
      3  1.7.2.2  tls /*-
      4  1.7.2.2  tls  * Copyright (c) 2013 Antti Kantee.  All Rights Reserved.
      5  1.7.2.2  tls  *
      6  1.7.2.2  tls  * Redistribution and use in source and binary forms, with or without
      7  1.7.2.2  tls  * modification, are permitted provided that the following conditions
      8  1.7.2.2  tls  * are met:
      9  1.7.2.2  tls  * 1. Redistributions of source code must retain the above copyright
     10  1.7.2.2  tls  *    notice, this list of conditions and the following disclaimer.
     11  1.7.2.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.7.2.2  tls  *    notice, this list of conditions and the following disclaimer in the
     13  1.7.2.2  tls  *    documentation and/or other materials provided with the distribution.
     14  1.7.2.2  tls  *
     15  1.7.2.2  tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.7.2.2  tls  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.7.2.2  tls  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.7.2.2  tls  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.7.2.2  tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.7.2.2  tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.7.2.2  tls  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.7.2.2  tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.7.2.2  tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.7.2.2  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.7.2.2  tls  * SUCH DAMAGE.
     26  1.7.2.2  tls  */
     27  1.7.2.2  tls 
     28  1.7.2.2  tls #include "rumpuser_port.h"
     29  1.7.2.2  tls 
     30  1.7.2.3  tls #if !defined(lint)
     31  1.7.2.3  tls __RCSID("$NetBSD: rumpuser_bio.c,v 1.7.2.3 2014/08/20 00:02:21 tls Exp $");
     32  1.7.2.3  tls #endif /* !lint */
     33  1.7.2.3  tls 
     34  1.7.2.2  tls #include <sys/types.h>
     35  1.7.2.2  tls 
     36  1.7.2.2  tls #include <assert.h>
     37  1.7.2.2  tls #include <errno.h>
     38  1.7.2.2  tls #include <pthread.h>
     39  1.7.2.2  tls #include <stdint.h>
     40  1.7.2.2  tls #include <stdio.h>
     41  1.7.2.2  tls #include <string.h>
     42  1.7.2.2  tls #include <unistd.h>
     43  1.7.2.2  tls 
     44  1.7.2.2  tls #include <rump/rumpuser.h>
     45  1.7.2.2  tls 
     46  1.7.2.2  tls #include "rumpuser_int.h"
     47  1.7.2.2  tls 
     48  1.7.2.2  tls struct rumpuser_bio {
     49  1.7.2.2  tls 	int bio_fd;
     50  1.7.2.2  tls 	int bio_op;
     51  1.7.2.2  tls 	void *bio_data;
     52  1.7.2.2  tls 	size_t bio_dlen;
     53  1.7.2.2  tls 	off_t bio_off;
     54  1.7.2.2  tls 
     55  1.7.2.2  tls 	rump_biodone_fn bio_done;
     56  1.7.2.2  tls 	void *bio_donearg;
     57  1.7.2.2  tls };
     58  1.7.2.2  tls 
     59  1.7.2.2  tls #define N_BIOS 128
     60  1.7.2.2  tls static pthread_mutex_t biomtx = PTHREAD_MUTEX_INITIALIZER;
     61  1.7.2.2  tls static pthread_cond_t biocv = PTHREAD_COND_INITIALIZER;
     62  1.7.2.2  tls static int bio_head, bio_tail;
     63  1.7.2.2  tls static struct rumpuser_bio bios[N_BIOS];
     64  1.7.2.2  tls 
     65  1.7.2.2  tls static void
     66  1.7.2.2  tls dobio(struct rumpuser_bio *biop)
     67  1.7.2.2  tls {
     68  1.7.2.2  tls 	ssize_t rv;
     69  1.7.2.2  tls 	int error, dummy;
     70  1.7.2.2  tls 
     71  1.7.2.2  tls 	assert(biop->bio_donearg != NULL);
     72  1.7.2.2  tls 	if (biop->bio_op & RUMPUSER_BIO_READ) {
     73  1.7.2.2  tls 		error = 0;
     74  1.7.2.2  tls 		rv = pread(biop->bio_fd, biop->bio_data,
     75  1.7.2.2  tls 		    biop->bio_dlen, biop->bio_off);
     76  1.7.2.2  tls 		if (rv < 0) {
     77  1.7.2.2  tls 			rv = 0;
     78  1.7.2.2  tls 			error = errno;
     79  1.7.2.2  tls 		}
     80  1.7.2.2  tls 	} else {
     81  1.7.2.2  tls 		error = 0;
     82  1.7.2.2  tls 		rv = pwrite(biop->bio_fd, biop->bio_data,
     83  1.7.2.2  tls 		    biop->bio_dlen, biop->bio_off);
     84  1.7.2.2  tls 		if (rv < 0) {
     85  1.7.2.2  tls 			rv = 0;
     86  1.7.2.2  tls 			error = errno;
     87  1.7.2.2  tls 		} else if (biop->bio_op & RUMPUSER_BIO_SYNC) {
     88  1.7.2.2  tls #ifdef __NetBSD__
     89  1.7.2.2  tls 			fsync_range(biop->bio_fd, FDATASYNC,
     90  1.7.2.2  tls 			    biop->bio_off, biop->bio_dlen);
     91  1.7.2.2  tls #else
     92  1.7.2.2  tls 			fsync(biop->bio_fd);
     93  1.7.2.2  tls #endif
     94  1.7.2.2  tls 		}
     95  1.7.2.2  tls 	}
     96  1.7.2.2  tls 	rumpkern_sched(0, NULL);
     97  1.7.2.2  tls 	biop->bio_done(biop->bio_donearg, (size_t)rv, error);
     98  1.7.2.2  tls 	rumpkern_unsched(&dummy, NULL);
     99  1.7.2.2  tls 
    100  1.7.2.2  tls 	/* paranoia */
    101  1.7.2.2  tls 	biop->bio_donearg = NULL;
    102  1.7.2.2  tls }
    103  1.7.2.2  tls 
    104  1.7.2.2  tls static void *
    105  1.7.2.2  tls biothread(void *arg)
    106  1.7.2.2  tls {
    107  1.7.2.2  tls 	struct rumpuser_bio *biop;
    108  1.7.2.2  tls 	int rv;
    109  1.7.2.2  tls 
    110  1.7.2.2  tls 	rumpuser__hyp.hyp_schedule();
    111  1.7.2.2  tls 	rv = rumpuser__hyp.hyp_lwproc_newlwp(0);
    112  1.7.2.2  tls 	assert(rv == 0);
    113  1.7.2.2  tls 	rumpuser__hyp.hyp_unschedule();
    114  1.7.2.2  tls 	NOFAIL_ERRNO(pthread_mutex_lock(&biomtx));
    115  1.7.2.2  tls 	for (;;) {
    116  1.7.2.2  tls 		while (bio_head == bio_tail)
    117  1.7.2.2  tls 			NOFAIL_ERRNO(pthread_cond_wait(&biocv, &biomtx));
    118  1.7.2.2  tls 
    119  1.7.2.2  tls 		biop = &bios[bio_tail];
    120  1.7.2.2  tls 		pthread_mutex_unlock(&biomtx);
    121  1.7.2.2  tls 
    122  1.7.2.2  tls 		dobio(biop);
    123  1.7.2.2  tls 
    124  1.7.2.2  tls 		NOFAIL_ERRNO(pthread_mutex_lock(&biomtx));
    125  1.7.2.2  tls 		bio_tail = (bio_tail+1) % N_BIOS;
    126  1.7.2.2  tls 		pthread_cond_signal(&biocv);
    127  1.7.2.2  tls 	}
    128  1.7.2.2  tls 
    129  1.7.2.2  tls 	/* unreachable */
    130  1.7.2.2  tls 	abort();
    131  1.7.2.2  tls }
    132  1.7.2.2  tls 
    133  1.7.2.2  tls void
    134  1.7.2.2  tls rumpuser_bio(int fd, int op, void *data, size_t dlen, int64_t doff,
    135  1.7.2.2  tls 	rump_biodone_fn biodone, void *bioarg)
    136  1.7.2.2  tls {
    137  1.7.2.2  tls 	struct rumpuser_bio bio;
    138  1.7.2.2  tls 	static int inited = 0;
    139  1.7.2.2  tls 	static int usethread = 1;
    140  1.7.2.2  tls 	int nlocks;
    141  1.7.2.2  tls 
    142  1.7.2.2  tls 	rumpkern_unsched(&nlocks, NULL);
    143  1.7.2.2  tls 
    144  1.7.2.2  tls 	if (!inited) {
    145  1.7.2.2  tls 		pthread_mutex_lock(&biomtx);
    146  1.7.2.2  tls 		if (!inited) {
    147  1.7.2.2  tls 			char buf[16];
    148  1.7.2.2  tls 			pthread_t pt;
    149  1.7.2.2  tls 
    150  1.7.2.2  tls 			/*
    151  1.7.2.2  tls 			 * duplicates policy of rump kernel.  maybe a bit
    152  1.7.2.2  tls 			 * questionable, but since the setting is not
    153  1.7.2.2  tls 			 * used in normal circumstances, let's not care
    154  1.7.2.2  tls 			 */
    155  1.7.2.2  tls 			if (getenv_r("RUMP_THREADS", buf, sizeof(buf)) == 0)
    156  1.7.2.2  tls 				usethread = *buf != '0';
    157  1.7.2.2  tls 
    158  1.7.2.2  tls 			if (usethread)
    159  1.7.2.2  tls 				pthread_create(&pt, NULL, biothread, NULL);
    160  1.7.2.2  tls 			inited = 1;
    161  1.7.2.2  tls 		}
    162  1.7.2.2  tls 		pthread_mutex_unlock(&biomtx);
    163  1.7.2.2  tls 		assert(inited);
    164  1.7.2.2  tls 	}
    165  1.7.2.2  tls 
    166  1.7.2.2  tls 	bio.bio_fd = fd;
    167  1.7.2.2  tls 	bio.bio_op = op;
    168  1.7.2.2  tls 	bio.bio_data = data;
    169  1.7.2.2  tls 	bio.bio_dlen = dlen;
    170  1.7.2.2  tls 	bio.bio_off = (off_t)doff;
    171  1.7.2.2  tls 	bio.bio_done = biodone;
    172  1.7.2.2  tls 	bio.bio_donearg = bioarg;
    173  1.7.2.2  tls 
    174  1.7.2.2  tls 	if (!usethread) {
    175  1.7.2.2  tls 		dobio(&bio);
    176  1.7.2.2  tls 	} else {
    177  1.7.2.2  tls 		pthread_mutex_lock(&biomtx);
    178  1.7.2.2  tls 		while ((bio_head+1) % N_BIOS == bio_tail)
    179  1.7.2.2  tls 			pthread_cond_wait(&biocv, &biomtx);
    180  1.7.2.2  tls 
    181  1.7.2.2  tls 		bios[bio_head] = bio;
    182  1.7.2.2  tls 		bio_head = (bio_head+1) % N_BIOS;
    183  1.7.2.2  tls 
    184  1.7.2.2  tls 		pthread_cond_signal(&biocv);
    185  1.7.2.2  tls 		pthread_mutex_unlock(&biomtx);
    186  1.7.2.2  tls 	}
    187  1.7.2.2  tls 
    188  1.7.2.2  tls 	rumpkern_sched(nlocks, NULL);
    189  1.7.2.2  tls }
    190