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