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