forked from TheNewNormal/libxhyve
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmirage_block_c.c
240 lines (222 loc) · 6.18 KB
/
mirage_block_c.c
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// +build qcow2
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <caml/alloc.h>
#include <caml/threads.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/callback.h>
#include <caml/bigarray.h>
#include "mirage_block_c.h"
void
mirage_block_register_thread(){
caml_c_thread_register();
}
void
mirage_block_unregister_thread(){
caml_c_thread_unregister();
}
/* Convenience macro to cache the OCaml callback and immediately abort()
if it can't be found -- this would indicate a fundamental linking error. */
#define OCAML_NAMED_FUNCTION(name) \
static value *fn = NULL; \
if (fn == NULL) { \
fn = caml_named_value(name); \
} \
if (fn == NULL) { \
fprintf(stderr, "Callback.register for " name " not called: are all objects linked?\n"); \
abort(); \
}
/* Every call has 2 C functions:
1. static void ocaml_FOO: using the CAMLparam/CAMLreturn macros. This assumes
the runtime system lock is held. Errors are propagated back by out
parameters, since we must use CAMLreturn and yet we cannot use CAMLreturn
for regular C values.
2. plain C FOO: this acquires the runtime lock and calls the ocaml_FOO
function.
An alternative design would be to use Begin_roots and End_roots after
acquiring the runtime lock. */
static void
ocaml_mirage_block_open(const char *config, int *out, int *err) {
CAMLparam0();
CAMLlocal2(ocaml_config, handle);
ocaml_config = caml_copy_string(config);
OCAML_NAMED_FUNCTION("mirage_block_open")
handle = caml_callback_exn(*fn, ocaml_config);
if (Is_exception_result(handle)){
*err = 1;
} else {
*err = 0;
*out = Int_val(handle);
}
CAMLreturn0;
}
mirage_block_handle
mirage_block_open(const char *config) {
int result;
int err = 1;
caml_acquire_runtime_system();
ocaml_mirage_block_open(config, &result, &err);
caml_release_runtime_system();
if (err){
errno = EINVAL;
return (-1);
} else {
return result;
}
}
static void
ocaml_mirage_block_stat(mirage_block_handle h, struct stat *out, int *err) {
CAMLparam0();
CAMLlocal2(ocaml_handle, result);
ocaml_handle = Val_int(h);
OCAML_NAMED_FUNCTION("mirage_block_stat")
result = caml_callback_exn(*fn, ocaml_handle);
int read_write = Int_val(Field(result, 0)) != 0;
unsigned int sector_size = (unsigned int)Int_val(Field(result, 1));
uint64_t size_sectors = (uint64_t)Int64_val(Field(result, 2));
if (Is_exception_result(result)){
*err = 1;
} else {
*err = 0;
bzero(out, sizeof(struct stat));
out->st_dev = 0;
out->st_ino = 0;
out->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR | (read_write?(S_IWOTH | S_IWGRP | S_IWUSR): 0);
out->st_nlink = 1;
out->st_uid = 0;
out->st_gid = 0;
out->st_rdev = 0;
out->st_size = (off_t)(sector_size * size_sectors);
out->st_blocks = (blkcnt_t)size_sectors;
out->st_blksize = (blksize_t)sector_size;
out->st_flags = 0;
out->st_gen = 0;
}
CAMLreturn0;
}
int
mirage_block_stat(mirage_block_handle h, struct stat *buf) {
int err = 1;
caml_acquire_runtime_system();
ocaml_mirage_block_stat(h, buf, &err);
caml_release_runtime_system();
if (err){
errno = EINVAL;
return (-1);
} else {
return 0;
}
}
static void
ocaml_mirage_block_close(int handle, int *err) {
CAMLparam0();
CAMLlocal2(ocaml_handle, result);
ocaml_handle = Val_int(handle);
OCAML_NAMED_FUNCTION("mirage_block_close")
result = caml_callback_exn(*fn, ocaml_handle);
*err = 0;
if (Is_exception_result(result)){
*err = 1;
}
CAMLreturn0;
}
int mirage_block_close(int handle){
int err = 1;
caml_acquire_runtime_system();
ocaml_mirage_block_close(handle, &err);
caml_release_runtime_system();
return err;
}
static void
ocaml_mirage_block_preadv(const int handle, const struct iovec *iov, int iovcnt, off_t ofs, ssize_t *out, int *err) {
CAMLparam0();
CAMLlocal4(ocaml_handle, ocaml_bufs, ocaml_ofs, ocaml_result);
ocaml_handle = Val_int(handle);
ocaml_bufs = caml_alloc_tuple((mlsize_t)iovcnt);
ocaml_ofs = Val_int(ofs);
for (int i = 0; i < iovcnt; i++ ){
Store_field(ocaml_bufs, (mlsize_t)i, caml_ba_alloc_dims(CAML_BA_CHAR | CAML_BA_C_LAYOUT,
1, (*(iov+i)).iov_base, (*(iov+i)).iov_len));
}
OCAML_NAMED_FUNCTION("mirage_block_preadv")
ocaml_result = caml_callback3_exn(*fn, ocaml_handle, ocaml_bufs, ocaml_ofs);
if (Is_exception_result(ocaml_result)) {
*err = 1;
} else {
*err = 0;
*out = Int_val(ocaml_result);
}
CAMLreturn0;
}
ssize_t
mirage_block_preadv(mirage_block_handle h, const struct iovec *iov, int iovcnt, off_t offset) {
ssize_t len;
int err = 1;
caml_acquire_runtime_system();
ocaml_mirage_block_preadv(h, iov, iovcnt, offset, &len, &err);
caml_release_runtime_system();
if (err){
errno = EINVAL;
return (-1);
}
return len;
}
static void
ocaml_mirage_block_pwritev(const int handle, const struct iovec *iov, int iovcnt, off_t ofs, ssize_t *out, int *err) {
CAMLparam0();
CAMLlocal4(ocaml_handle, ocaml_bufs, ocaml_ofs, ocaml_result);
ocaml_handle = Val_int(handle);
ocaml_bufs = caml_alloc_tuple((mlsize_t)iovcnt);
ocaml_ofs = Val_int(ofs);
for (int i = 0; i < iovcnt; i++ ){
Store_field(ocaml_bufs, (mlsize_t)i, caml_ba_alloc_dims(CAML_BA_CHAR | CAML_BA_C_LAYOUT,
1, (*(iov+i)).iov_base, (*(iov+i)).iov_len));
}
OCAML_NAMED_FUNCTION("mirage_block_pwritev")
ocaml_result = caml_callback3_exn(*fn, ocaml_handle, ocaml_bufs, ocaml_ofs);
if (Is_exception_result(ocaml_result)) {
*err = 1;
} else {
*err = 0;
*out = Int_val(ocaml_result);
}
CAMLreturn0;
}
ssize_t
mirage_block_pwritev(mirage_block_handle h, const struct iovec *iov, int iovcnt, off_t offset) {
ssize_t len;
int err = 1;
caml_acquire_runtime_system();
ocaml_mirage_block_pwritev(h, iov, iovcnt, offset, &len, &err);
caml_release_runtime_system();
if (err){
errno = EINVAL;
return (-1);
}
return len;
}
static void
ocaml_mirage_block_flush(int handle, int *err) {
CAMLparam0();
CAMLlocal2(ocaml_handle, result);
ocaml_handle = Val_int(handle);
OCAML_NAMED_FUNCTION("mirage_block_flush")
result = caml_callback_exn(*fn, ocaml_handle);
*err = 0;
if (Is_exception_result(result)){
errno = EINVAL;
*err = 1;
}
CAMLreturn0;
}
int mirage_block_flush(int handle){
int err = 1;
caml_acquire_runtime_system();
ocaml_mirage_block_flush(handle, &err);
caml_release_runtime_system();
return err;
}