summaryrefslogtreecommitdiff
path: root/src/kernel/vfs/backend.c
blob: 0f684a037bb5bb0bc168d555487346cb90854735 (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
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <kernel/vfs/backend.h>
#include <kernel/vfs/root.h>

// dispatches a VFS operation to the correct process
_Noreturn void vfs_backend_dispatch(struct vfs_backend *backend, struct vfs_op op) {
	struct vfs_op_request req = {
		.op = op,
		.caller = process_current,
		.backend = backend,
	};
	switch (backend->type) {
		case VFS_BACK_ROOT:
			int ret = vfs_root_handler(&req);
			vfs_backend_finish(&req, ret);
		case VFS_BACK_USER:
			process_current->state = PS_WAITS4FS;
			if (backend->handler == NULL) { // backend not ready yet
				if (backend->queue == NULL) {
					backend->queue = process_current;
					process_switch_any();
				} else {
					panic(); // TODO implement a proper queue
				}
			} else {
				if (backend->handler->state != PS_WAITS4REQUEST)
					panic(); // invalid state
				panic(); // TODO
			}
		default:
			panic();
	}
}

// returns from a VFS operation to the calling process
_Noreturn void vfs_backend_finish(struct vfs_op_request *req, int ret) {
	if (req->op.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;
	} 

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