rumpuser_bio.c revision 1.1 1 /* $NetBSD: rumpuser_bio.c,v 1.1 2013/04/29 12:56:04 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
131 if (!inited) {
132 pthread_mutex_lock(&biomtx);
133 if (!inited) {
134 char buf[16];
135 pthread_t pt;
136
137 /*
138 * duplicates policy of rump kernel. maybe a bit
139 * questionable, but since the setting is not
140 * used in normal circumstances, let's not care
141 */
142 if (getenv_r("RUMP_THREADS", buf, sizeof(buf)) == 0)
143 usethread = *buf != '0';
144
145 pthread_create(&pt, NULL, biothread, NULL);
146 inited = 1;
147 }
148 pthread_mutex_unlock(&biomtx);
149 assert(inited);
150 }
151
152 bio.bio_fd = fd;
153 bio.bio_op = op;
154 bio.bio_data = data;
155 bio.bio_dlen = dlen;
156 bio.bio_off = doff;
157 bio.bio_done = biodone;
158 bio.bio_donearg = bioarg;
159
160 if (!usethread) {
161 dobio(&bio);
162 } else {
163 pthread_mutex_lock(&biomtx);
164 while ((bio_head+1) % N_BIOS == bio_tail)
165 pthread_cond_wait(&biocv, &biomtx);
166
167 bios[bio_head] = bio;
168 bio_head = (bio_head+1) % N_BIOS;
169
170 pthread_cond_signal(&biocv);
171 pthread_mutex_unlock(&biomtx);
172 }
173 }
174