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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
#include <camellia/errno.h>
#include <camellia/flags.h>
#include <kernel/malloc.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <kernel/queue.h>
#include <kernel/vfs/request.h>
#include <shared/mem.h>
/** Checks if the request flags don't contradict each other.
* While this could be done by caller of vfsreq_dispatchcopy, as currently
* there's only one of them per operation, doing this here means that any
* potential future callers can't forget those checks.
* Doing this so late is kinda inefficient but I don't really care. */
static bool
vfsreq_isvalid(VfsReq *r)
{
/* Notable omissions:
* - Simplifying the path for open(). Already done when resolving the mount.
* - Checking the handle permissions. We don't have access to it.
* */
if (r->type == VFSOP_OPEN && !(r->flags & OPEN_WRITE) && (r->flags & OPEN_CREATE)) {
return false;
}
// XXX if i add a handle field to vfs_request, check ->readable ->writeable here
return true;
}
void
vfsreq_dispatchcopy(VfsReq tmpl)
{
VfsReq *req;
VfsBackend *backend;
/* allocate memory for the request and move it there */
req = kmalloc(sizeof *req, TagVfsReq);
memcpy(req, &tmpl, sizeof *req);
backend = req->backend;
if (backend) {
assert(backend->usehcnt > 0);
backend->usehcnt++;
}
if (req->caller) {
proc_setstate(req->caller, PS_WAITS4FS);
req->caller->reqslot = req;
}
/* Check request validity. Doing this after the memcpy means that it
* doesn't need to be special cased in vfsreq_finish. */
if (!vfsreq_isvalid(req)) {
vfsreq_finish_short(req, -EINVAL);
return;
}
assert(req->reqqueue_next == NULL);
if (backend == NULL) {
/* null mount - probably never had a real backend */
vfsreq_finish_short(req, -ENOENT);
} else if (backend->is_user) {
vfsback_useraccept(req);
} else { /* kernel backend */
assert(backend->kern.accept);
backend->kern.accept(req);
}
}
void
vfsreq_finish(VfsReq *req, char __user *stored, long ret, int flags, Proc *handler)
{
if (req->type == VFSOP_OPEN && ret >= 0) {
Handle *h;
if (!(flags & FSR_DELEGATE)) {
/* default behavior - create a new handle for the file, wrap the id */
h = handle_init(HANDLE_FILE);
h->backend = req->backend;
req->backend->usehcnt++;
h->file_id = stored;
h->readable = OPEN_READABLE(req->flags);
h->writeable = OPEN_WRITEABLE(req->flags);
} else {
/* delegating - moving a handle to the caller */
assert(handler);
h = hs_take(handler->hs, ret);
if (h) {
h->readable = h->readable && OPEN_READABLE(req->flags);
h->writeable = h->writeable && OPEN_WRITEABLE(req->flags);
}
}
if (h) {
// TODO write tests for caller getting killed while opening a file
if (!req->caller) panic_unimplemented();
ret = hs_put(req->caller->hs, h);
if (ret < 0) ret = -EMFILE;
} else {
ret = -1;
}
}
if (req->type == VFSOP_READ && ret >= 0) {
assert((size_t)ret <= req->output.len);
}
if (req->input.kern) {
kfree(req->input.buf_kern);
}
if (req->backend) {
vfsback_userdown(req->backend);
}
assert(req->reqqueue_next == NULL);
if (req->caller) {
assert(req->caller->state == PS_WAITS4FS);
proc_savereturn(req->caller, ret);
proc_setstate(req->caller, PS_RUNNING);
}
kfree(req);
}
void
vfsback_useraccept(VfsReq *req)
{
VfsBackend *backend;
Proc *handler;
struct ufs_request res = {0};
int len;
assert(req != NULL);
backend = req->backend;
assert(backend);
assert(backend->is_user);
if (backend->user.provhcnt == 0) {
vfsreq_finish_short(req, -EPIPE);
return;
} else if (backend->user.handler == NULL) {
reqqueue_join(&backend->user.queue, req);
return;
}
handler = backend->user.handler;
assert(handler->state == PS_WAITS4REQUEST);
// the pcpy calls aren't present in all kernel backends
// it's a way to tell apart kernel and user backends apart
// TODO check validity of memory regions somewhere else
if (req->input.buf) {
__user void *buf = handler->awaited_req.buf;
len = min(req->input.len, handler->awaited_req.max_len);
if (req->input.kern) {
pcpy_to(handler, buf, req->input.buf_kern, len);
} else {
len = pcpy_bi(
handler, buf,
req->caller, req->input.buf, len
);
}
} else {
len = req->output.len;
}
res.len = len;
res.capacity = req->output.len;
res.id = req->id;
res.id2 = req->id2;
res.offset = req->offset;
res.flags = req->flags;
res.op = req->type;
if (pcpy_to(handler, handler->awaited_req.res, &res, sizeof res) < sizeof(res))
{
panic_unimplemented();
}
Handle *h;
hid_t hid = hs_hinit(handler->hs, HANDLE_FS_REQ, &h);
if (hid < 0) panic_unimplemented();
h->req = req;
proc_setstate(handler, PS_RUNNING);
proc_savereturn(handler, hid);
req->backend->user.handler = NULL;
}
static void
vfsback_checkfree(VfsBackend *b)
{
if (b->is_user && b->usehcnt == 0 && b->user.provhcnt == 0) {
assert(b->user.queue.head == NULL);
kfree(b);
}
}
void
vfsback_userdown(VfsBackend *b)
{
assert(b);
assert(0 < b->usehcnt);
b->usehcnt--;
if (b->usehcnt == 0) {
if (!b->is_user && b->kern.cleanup) {
b->kern.cleanup(b);
}
if (b->is_user && b->user.handler) {
/* tell the process that the filesystem won't receive any more
* requests */
Proc *p = b->user.handler;
b->user.handler = NULL;
assert(p->state == PS_WAITS4REQUEST);
proc_savereturn(p, -EPIPE);
proc_setstate(p, PS_RUNNING);
}
}
vfsback_checkfree(b);
}
void
vfsback_provdown(VfsBackend *b)
{
assert(b);
assert(b->is_user);
assert(0 < b->user.provhcnt);
b->user.provhcnt--;
if (b->user.provhcnt == 0) {
assert(b->is_user);
/* discard everything in the queue */
VfsReq *q;
while ((q = reqqueue_pop(&b->user.queue))) {
vfsreq_finish_short(q, -EPIPE);
}
}
vfsback_checkfree(b);
}
void
reqqueue_init(ReqQueue *q)
{
QUEUE_INIT(q);
}
void
reqqueue_join(ReqQueue *q, VfsReq *req)
{
QUEUE_APPEND(q, reqqueue, req);
}
VfsReq *
reqqueue_pop(ReqQueue *q)
{
return QUEUE_POP(q, reqqueue);
}
void
reqqueue_ringreadall(ReqQueue *q, ring_t *r)
{
VfsReq *req;
char tmp[64];
size_t mlen = 0;
if (ring_used(r) == 0) return;
/* read as much as the biggest request wants */
for (req = q->head; req; req = req->reqqueue_next) {
mlen = max(mlen, req->output.len);
}
mlen = min(mlen, sizeof tmp);
mlen = ring_get(r, tmp, mlen);
while ((req = reqqueue_pop(q))) {
size_t ret = min(mlen, req->output.len);
assert(req->type == VFSOP_READ);
if (req->caller) {
pcpy_to(req->caller, req->output.buf, tmp, ret);
}
vfsreq_finish_short(req, ret);
}
}
|