summaryrefslogtreecommitdiff
path: root/src/kernel/vfs/request.c
blob: 03d785388b27d5d00795baa2c67cb93ba7ebb8f3 (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
105
106
107
108
109
110
111
112
#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>

int vfs_request_create(struct vfs_request req_) {
	struct vfs_request *req;
	process_transition(process_current, PS_WAITS4FS);
	process_current->waits4fs.queue_next = NULL;

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

	if (!req->backend || !req->backend->potential_handlers)
		return vfs_request_finish(req, -1);

	switch (req->backend->type) {
		case VFS_BACK_ROOT:
			return vfs_root_handler(req);
		case VFS_BACK_USER: {
			struct process **iter = &req->backend->queue;
			while (*iter != NULL) // find free spot in queue
				iter = &(*iter)->waits4fs.queue_next;
			*iter = req->caller;

			vfs_backend_accept(req->backend);
			return -1; // isn't passed to the caller process anyways
		}
		default:
			panic_invalid_state();
	}
}

int vfs_backend_accept(struct vfs_backend *backend) {
	struct vfs_request *req;
	struct process *handler = backend->handler;
	struct fs_wait_response res = {0};
	int len;

	if (!backend->handler) return -1;
	assert(handler->state == PS_WAITS4REQUEST);
	assert(!handler->handled_req);

	if (!backend->queue) return -1;
	// TODO wouldn't it be better to directly store vfs_requests in the queue?
	req = &backend->queue->waits4fs.req;
	backend->queue = backend->queue->waits4fs.queue_next;

	len = min(req->input.len, handler->awaited_req.max_len);
	if (!virt_cpy(handler->pages, handler->awaited_req.buf, 
				req->input.kern ? NULL : req->caller->pages, req->input.buf, len))
		goto fail; // can't copy buffer

	res.len      = len;
	res.capacity = req->output.len;
	res.id       = req->id;
	res.offset   = req->offset;
	res.op       = req->type;

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

	process_transition(handler, PS_RUNNING);
	handler->handled_req = req;
	req->backend->handler = NULL;
	regs_savereturn(&handler->regs, 0);
	return 0;
fail:
	panic_unimplemented(); // TODO
}

int vfs_request_finish(struct vfs_request *req, int ret) {
	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_invalid_state(); // we check for free handles before the open() call
		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);

	assert(req->caller->state == PS_WAITS4FS || req->caller->state == PS_WAITS4IRQ);
	process_transition(req->caller, PS_RUNNING);
	regs_savereturn(&req->caller->regs, ret);
	return ret;
}

void vfs_request_cancel(struct vfs_request *req, int ret) {
	if (req->input.kern)
		kfree(req->input.buf_kern);

	// ret must always be negative, so it won't be confused with a success
	if (ret > 0) ret = -ret;
	if (ret == 0) ret = -1;
	regs_savereturn(&req->caller->regs, ret);
	process_transition(req->caller, PS_RUNNING);
}