summaryrefslogtreecommitdiff
path: root/src/kernel/vfs/request.c
blob: 0164dfd0a6d4cad4134b2a7c5e12e591b4ede5f1 (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
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
#include <camellia/errno.h>
#include <kernel/arch/amd64/driver/fsroot.h>
#include <kernel/mem/alloc.h>
#include <kernel/mem/virt.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <kernel/vfs/request.h>
#include <shared/mem.h>

void vfsreq_create(struct vfs_request req_) {
	struct vfs_request *req = kmalloc(sizeof *req); // freed in vfsreq_finish
	memcpy(req, &req_, sizeof *req);

	if (req->backend)
		req->backend->refcount++;

	if (req->caller) {
		// TODO after running `run_tests` from vga, this is entered with state == PS_WAITS4FS
		process_transition(req->caller, PS_WAITS4FS);
		req->caller->waits4fs.req = req;
	}

	if (!req->backend || !req->backend->potential_handlers) {
		vfsreq_finish_short(req, -1);
		return;
	}

	struct vfs_request **iter = &req->backend->queue;
	while (*iter != NULL) // find free spot in queue
		iter = &(*iter)->queue_next;
	*iter = req;

	vfs_backend_tryaccept(req->backend);
}

void vfsreq_finish(struct vfs_request *req, char __user *stored, long ret,
		int flags, struct process *handler)
{
	if (req->type == VFSOP_OPEN && ret >= 0) {
		// TODO write tests for caller getting killed while opening a file
		if (!req->caller) panic_unimplemented();

		handle_t handle = process_find_free_handle(req->caller, 0);
		if (handle < 0)
			panic_invalid_state(); // we check for free handles before the open() call

		if (!(flags & FSR_DELEGATE)) {
			/* default behavior - create a new handle for the file, wrap the id */
			struct handle *backing = handle_init(HANDLE_FILE);
			backing->backend = req->backend;
			req->backend->refcount++;
			backing->file_id = stored;
			req->caller->handles[handle] = backing;
		} else {
			/* delegating - moving a handle to the caller */
			assert(handler);

			struct handle *h = handler->handles[ret];
			if (!h) {
				kprintf("tried delegating an invalid handle\n");
				handle = -1; // return error
			} else {
				req->caller->handles[handle] = h;
				handler->handles[ret] = NULL;
			}
		}
		ret = handle;
	}

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

	if (req->caller) {
		assert(req->caller->state == PS_WAITS4FS);
		regs_savereturn(&req->caller->regs, ret);
		process_transition(req->caller, PS_RUNNING);
	}

	if (req->backend)
		vfs_backend_refdown(req->backend);

	kfree(req);
	return;
}

void vfs_backend_tryaccept(struct vfs_backend *backend) {
	struct vfs_request *req = backend->queue;
	if (!req) return;

	/* ensure backend is ready to accept request */
	if (backend->is_user) {
		if (!backend->user.handler) return;
	} else {
		assert(backend->kern.ready);
		if (!backend->kern.ready(backend)) return;
	}

	backend->queue = req->queue_next;

	if (backend->is_user) {
		vfs_backend_user_accept(req);
	} else {
		assert(backend->kern.accept);
		backend->kern.accept(req);
	}
}

void vfs_backend_user_accept(struct vfs_request *req) {
	struct process *handler;
	struct fs_wait_response res = {0};
	struct virt_cpy_error cpyerr;
	int len;

	assert(req && req->backend && req->backend->user.handler);
	handler = req->backend->user.handler;
	assert(handler->state == PS_WAITS4REQUEST);
	assert(handler->handled_req == NULL);

	// the virt_cpy 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) {
		len = min(req->input.len, handler->awaited_req.max_len);
		virt_cpy(handler->pages, handler->awaited_req.buf,
				 req->input.kern ? NULL : req->caller->pages, req->input.buf,
				 len, &cpyerr);
		if (cpyerr.write_fail)
			panic_unimplemented();
		if (cpyerr.read_fail) {
			vfsreq_finish_short(req, -EFAULT);
			return;
		}
	} else {
		len = req->output.len;
	}

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

	if (!virt_cpy_to(handler->pages,
				handler->awaited_req.res, &res, sizeof res))
	{
		panic_unimplemented();
	}

	process_transition(handler, PS_RUNNING);
	handler->handled_req = req;
	req->backend->user.handler = NULL;
	regs_savereturn(&handler->regs, 0);
	return;
}

void vfs_backend_refdown(struct vfs_backend *b) {
	assert(b);
	assert(b->refcount > 0);
	if (--(b->refcount) > 0) return;

	assert(!b->queue);
	if (b->heap) kfree(b);
}