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