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