rumpuser_bio.c revision 1.2 1 /* $NetBSD: rumpuser_bio.c,v 1.2 2013/04/29 13:53:46 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 rumpuser__reschedule(0, NULL);
92 biop->bio_done(biop->bio_donearg, (size_t)rv, error);
93 rumpuser__unschedule(0, &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
104 NOFAIL_ERRNO(pthread_mutex_lock(&biomtx));
105 for (;;) {
106 while (bio_head == bio_tail)
107 NOFAIL_ERRNO(pthread_cond_wait(&biocv, &biomtx));
108
109 biop = &bios[bio_tail];
110 pthread_mutex_unlock(&biomtx);
111
112 dobio(biop);
113
114 NOFAIL_ERRNO(pthread_mutex_lock(&biomtx));
115 bio_tail = (bio_tail+1) % N_BIOS;
116 pthread_cond_signal(&biocv);
117 }
118
119 /* unreachable */
120 abort();
121 }
122
123 void
124 rumpuser_bio(int fd, int op, void *data, size_t dlen, off_t doff,
125 rump_biodone_fn biodone, void *bioarg)
126 {
127 struct rumpuser_bio bio;
128 static int inited = 0;
129 static int usethread = 0;
130 int nlocks;
131
132 rumpuser__unschedule(0, &nlocks, NULL);
133
134 if (!inited) {
135 pthread_mutex_lock(&biomtx);
136 if (!inited) {
137 char buf[16];
138 pthread_t pt;
139
140 /*
141 * duplicates policy of rump kernel. maybe a bit
142 * questionable, but since the setting is not
143 * used in normal circumstances, let's not care
144 */
145 if (getenv_r("RUMP_THREADS", buf, sizeof(buf)) == 0)
146 usethread = *buf != '0';
147
148 pthread_create(&pt, NULL, biothread, NULL);
149 inited = 1;
150 }
151 pthread_mutex_unlock(&biomtx);
152 assert(inited);
153 }
154
155 bio.bio_fd = fd;
156 bio.bio_op = op;
157 bio.bio_data = data;
158 bio.bio_dlen = dlen;
159 bio.bio_off = doff;
160 bio.bio_done = biodone;
161 bio.bio_donearg = bioarg;
162
163 if (!usethread) {
164 dobio(&bio);
165 } else {
166 pthread_mutex_lock(&biomtx);
167 while ((bio_head+1) % N_BIOS == bio_tail)
168 pthread_cond_wait(&biocv, &biomtx);
169
170 bios[bio_head] = bio;
171 bio_head = (bio_head+1) % N_BIOS;
172
173 pthread_cond_signal(&biocv);
174 pthread_mutex_unlock(&biomtx);
175 }
176
177 rumpuser__reschedule(nlocks, NULL);
178 }
179