rumpuser_bio.c revision 1.7.2.2 1 1.7.2.2 tls /* $NetBSD: rumpuser_bio.c,v 1.7.2.2 2013/06/23 06:21:08 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.2 tls #include <sys/types.h>
31 1.7.2.2 tls
32 1.7.2.2 tls #include <assert.h>
33 1.7.2.2 tls #include <errno.h>
34 1.7.2.2 tls #include <pthread.h>
35 1.7.2.2 tls #include <stdint.h>
36 1.7.2.2 tls #include <stdio.h>
37 1.7.2.2 tls #include <string.h>
38 1.7.2.2 tls #include <unistd.h>
39 1.7.2.2 tls
40 1.7.2.2 tls #include <rump/rumpuser.h>
41 1.7.2.2 tls
42 1.7.2.2 tls #include "rumpuser_int.h"
43 1.7.2.2 tls
44 1.7.2.2 tls struct rumpuser_bio {
45 1.7.2.2 tls int bio_fd;
46 1.7.2.2 tls int bio_op;
47 1.7.2.2 tls void *bio_data;
48 1.7.2.2 tls size_t bio_dlen;
49 1.7.2.2 tls off_t bio_off;
50 1.7.2.2 tls
51 1.7.2.2 tls rump_biodone_fn bio_done;
52 1.7.2.2 tls void *bio_donearg;
53 1.7.2.2 tls };
54 1.7.2.2 tls
55 1.7.2.2 tls #define N_BIOS 128
56 1.7.2.2 tls static pthread_mutex_t biomtx = PTHREAD_MUTEX_INITIALIZER;
57 1.7.2.2 tls static pthread_cond_t biocv = PTHREAD_COND_INITIALIZER;
58 1.7.2.2 tls static int bio_head, bio_tail;
59 1.7.2.2 tls static struct rumpuser_bio bios[N_BIOS];
60 1.7.2.2 tls
61 1.7.2.2 tls static void
62 1.7.2.2 tls dobio(struct rumpuser_bio *biop)
63 1.7.2.2 tls {
64 1.7.2.2 tls ssize_t rv;
65 1.7.2.2 tls int error, dummy;
66 1.7.2.2 tls
67 1.7.2.2 tls assert(biop->bio_donearg != NULL);
68 1.7.2.2 tls if (biop->bio_op & RUMPUSER_BIO_READ) {
69 1.7.2.2 tls error = 0;
70 1.7.2.2 tls rv = pread(biop->bio_fd, biop->bio_data,
71 1.7.2.2 tls biop->bio_dlen, biop->bio_off);
72 1.7.2.2 tls if (rv < 0) {
73 1.7.2.2 tls rv = 0;
74 1.7.2.2 tls error = errno;
75 1.7.2.2 tls }
76 1.7.2.2 tls } else {
77 1.7.2.2 tls error = 0;
78 1.7.2.2 tls rv = pwrite(biop->bio_fd, biop->bio_data,
79 1.7.2.2 tls biop->bio_dlen, biop->bio_off);
80 1.7.2.2 tls if (rv < 0) {
81 1.7.2.2 tls rv = 0;
82 1.7.2.2 tls error = errno;
83 1.7.2.2 tls } else if (biop->bio_op & RUMPUSER_BIO_SYNC) {
84 1.7.2.2 tls #ifdef __NetBSD__
85 1.7.2.2 tls fsync_range(biop->bio_fd, FDATASYNC,
86 1.7.2.2 tls biop->bio_off, biop->bio_dlen);
87 1.7.2.2 tls #else
88 1.7.2.2 tls fsync(biop->bio_fd);
89 1.7.2.2 tls #endif
90 1.7.2.2 tls }
91 1.7.2.2 tls }
92 1.7.2.2 tls rumpkern_sched(0, NULL);
93 1.7.2.2 tls biop->bio_done(biop->bio_donearg, (size_t)rv, error);
94 1.7.2.2 tls rumpkern_unsched(&dummy, NULL);
95 1.7.2.2 tls
96 1.7.2.2 tls /* paranoia */
97 1.7.2.2 tls biop->bio_donearg = NULL;
98 1.7.2.2 tls }
99 1.7.2.2 tls
100 1.7.2.2 tls static void *
101 1.7.2.2 tls biothread(void *arg)
102 1.7.2.2 tls {
103 1.7.2.2 tls struct rumpuser_bio *biop;
104 1.7.2.2 tls int rv;
105 1.7.2.2 tls
106 1.7.2.2 tls rumpuser__hyp.hyp_schedule();
107 1.7.2.2 tls rv = rumpuser__hyp.hyp_lwproc_newlwp(0);
108 1.7.2.2 tls assert(rv == 0);
109 1.7.2.2 tls rumpuser__hyp.hyp_unschedule();
110 1.7.2.2 tls NOFAIL_ERRNO(pthread_mutex_lock(&biomtx));
111 1.7.2.2 tls for (;;) {
112 1.7.2.2 tls while (bio_head == bio_tail)
113 1.7.2.2 tls NOFAIL_ERRNO(pthread_cond_wait(&biocv, &biomtx));
114 1.7.2.2 tls
115 1.7.2.2 tls biop = &bios[bio_tail];
116 1.7.2.2 tls pthread_mutex_unlock(&biomtx);
117 1.7.2.2 tls
118 1.7.2.2 tls dobio(biop);
119 1.7.2.2 tls
120 1.7.2.2 tls NOFAIL_ERRNO(pthread_mutex_lock(&biomtx));
121 1.7.2.2 tls bio_tail = (bio_tail+1) % N_BIOS;
122 1.7.2.2 tls pthread_cond_signal(&biocv);
123 1.7.2.2 tls }
124 1.7.2.2 tls
125 1.7.2.2 tls /* unreachable */
126 1.7.2.2 tls abort();
127 1.7.2.2 tls }
128 1.7.2.2 tls
129 1.7.2.2 tls void
130 1.7.2.2 tls rumpuser_bio(int fd, int op, void *data, size_t dlen, int64_t doff,
131 1.7.2.2 tls rump_biodone_fn biodone, void *bioarg)
132 1.7.2.2 tls {
133 1.7.2.2 tls struct rumpuser_bio bio;
134 1.7.2.2 tls static int inited = 0;
135 1.7.2.2 tls static int usethread = 1;
136 1.7.2.2 tls int nlocks;
137 1.7.2.2 tls
138 1.7.2.2 tls rumpkern_unsched(&nlocks, NULL);
139 1.7.2.2 tls
140 1.7.2.2 tls if (!inited) {
141 1.7.2.2 tls pthread_mutex_lock(&biomtx);
142 1.7.2.2 tls if (!inited) {
143 1.7.2.2 tls char buf[16];
144 1.7.2.2 tls pthread_t pt;
145 1.7.2.2 tls
146 1.7.2.2 tls /*
147 1.7.2.2 tls * duplicates policy of rump kernel. maybe a bit
148 1.7.2.2 tls * questionable, but since the setting is not
149 1.7.2.2 tls * used in normal circumstances, let's not care
150 1.7.2.2 tls */
151 1.7.2.2 tls if (getenv_r("RUMP_THREADS", buf, sizeof(buf)) == 0)
152 1.7.2.2 tls usethread = *buf != '0';
153 1.7.2.2 tls
154 1.7.2.2 tls if (usethread)
155 1.7.2.2 tls pthread_create(&pt, NULL, biothread, NULL);
156 1.7.2.2 tls inited = 1;
157 1.7.2.2 tls }
158 1.7.2.2 tls pthread_mutex_unlock(&biomtx);
159 1.7.2.2 tls assert(inited);
160 1.7.2.2 tls }
161 1.7.2.2 tls
162 1.7.2.2 tls bio.bio_fd = fd;
163 1.7.2.2 tls bio.bio_op = op;
164 1.7.2.2 tls bio.bio_data = data;
165 1.7.2.2 tls bio.bio_dlen = dlen;
166 1.7.2.2 tls bio.bio_off = (off_t)doff;
167 1.7.2.2 tls bio.bio_done = biodone;
168 1.7.2.2 tls bio.bio_donearg = bioarg;
169 1.7.2.2 tls
170 1.7.2.2 tls if (!usethread) {
171 1.7.2.2 tls dobio(&bio);
172 1.7.2.2 tls } else {
173 1.7.2.2 tls pthread_mutex_lock(&biomtx);
174 1.7.2.2 tls while ((bio_head+1) % N_BIOS == bio_tail)
175 1.7.2.2 tls pthread_cond_wait(&biocv, &biomtx);
176 1.7.2.2 tls
177 1.7.2.2 tls bios[bio_head] = bio;
178 1.7.2.2 tls bio_head = (bio_head+1) % N_BIOS;
179 1.7.2.2 tls
180 1.7.2.2 tls pthread_cond_signal(&biocv);
181 1.7.2.2 tls pthread_mutex_unlock(&biomtx);
182 1.7.2.2 tls }
183 1.7.2.2 tls
184 1.7.2.2 tls rumpkern_sched(nlocks, NULL);
185 1.7.2.2 tls }
186