summaryrefslogtreecommitdiff
path: root/src/kernel/vfs/request.c
blob: 6159bbf84432eedbfe43ee6f004f23c1707770c6 (plain)
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
#include <kernel/mem/alloc.h>
#include <kernel/mem/virt.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <kernel/vfs/request.h>
#include <kernel/vfs/root.h>

// dispatches a VFS operation to the correct process
int vfs_request_create(struct vfs_request req_) {
	struct vfs_request *req;
	int ret;
	process_current->state = PS_WAITS4FS;

	// the request is owned by the caller
	process_current->pending_req = req_;
	req = &process_current->pending_req;

	switch (req->backend->type) {
		case VFS_BACK_ROOT:
			ret = vfs_root_handler(req);
			ret = vfs_request_finish(req, ret);
			return ret;
		case VFS_BACK_USER:
			if (req->backend->handler == NULL) {
				// backend isn't ready yet, join the queue
				assert(req->backend->queue == NULL); // TODO implement a proper queue
				req->backend->queue = process_current;
				process_switch_any();
			} else {
				vfs_request_pass2handler(req);
			}
		default:
			panic();
	}
}

_Noreturn void vfs_request_pass2handler(struct vfs_request *req) {
	struct process *handler = req->backend->handler;
	int len;
	assert(handler);
	assert(handler->state == PS_WAITS4REQUEST);
	assert(!handler->handled_req);
	handler->state = PS_RUNNING;
	handler->handled_req = req;

	if (!virt_cpy_from(handler->pages,
				&len, handler->awaited_req.len, sizeof len))
		goto fail; // can't read buffer length
	if (len > req->input.len) {
		// input bigger than buffer
		// TODO what should be done during e.g. open() calls? truncating doesn't seem right
		len = req->input.len;
	}

	if (req->input.kern) {
		if (!virt_cpy_to(handler->pages,
					handler->awaited_req.buf, req->input.buf_kern, len))
			goto fail; // can't copy buffer
	} else {
		if (!virt_cpy(handler->pages, handler->awaited_req.buf, 
					req->caller->pages, req->input.buf, len))
			goto fail; // can't copy buffer
	}

	if (!virt_cpy_to(handler->pages,
				handler->awaited_req.len, &len, sizeof len))
		goto fail; // can't copy new length

	if (!virt_cpy_to(handler->pages,
				handler->awaited_req.id, &req->id, sizeof req->id))
		goto fail; // can't copy id

	regs_savereturn(&handler->regs, req->type);
	process_switch(handler);
fail:
	panic(); // TODO
}

int vfs_request_finish(struct vfs_request *req, int ret) {
	struct process *caller = req->caller;

	if (req->type == VFSOP_OPEN && ret >= 0) {
		// open() calls need special handling
		// we need to wrap the id returned by the VFS in a handle passed to
		// the client
		handle_t handle = process_find_handle(req->caller);
		if (handle < 0) panic();
		req->caller->handles[handle] = (struct handle){
			.type = HANDLE_FILE,
			.file = {
				.backend = req->backend,
				.id = ret,
			},
		};
		ret = handle;
	}

	if (req->input.kern)  kfree(req->input.buf_kern);

	req->caller->state = PS_RUNNING;
	regs_savereturn(&req->caller->regs, ret);
	kfree(req);
	return ret;
}