summaryrefslogtreecommitdiff
path: root/src/kernel/arch/amd64/driver/fsroot.c
blob: 0eef12f9c07f642f96491b330e37e1c9f94a1629 (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
#include <camellia/errno.h>
#include <camellia/fsutil.h>
#include <kernel/arch/amd64/driver/driver.h>
#include <kernel/arch/amd64/driver/util.h>
#include <kernel/malloc.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <kernel/util.h>
#include <kernel/vfs/mount.h>
#include <kernel/vfs/request.h>
#include <shared/mem.h>

enum {
	Hbase,
	Hkdev,
};

static int
get_kdev(char *lst)
{
	int len = 0;
	for (VfsMount *m = vfs_mount_seed(); m; m = m->prev) {
		if (m->prefix_len == 0) {
			continue; /* that's us */
		}
		assert(m->prefix_len > 6);
		assert(memcmp(m->prefix, "/kdev/", 6) == 0);
		len += m->prefix_len - 6 + 1;
		if (lst) {
			memcpy(lst, m->prefix + 6, m->prefix_len - 6);
			lst += m->prefix_len - 6;
			*lst++ = '\0';
		}
	}
	return len;
}

static long
handle(VfsReq *req)
{
	static char *kdev = NULL;
	static int kdev_len = 0;
	const char *lst = NULL;
	int len = 0;

	if (!req->caller) return -1;
	if (req->type != VFSOP_OPEN) {
		switch ((uintptr_t __force)req->id) {
		case Hbase:
			lst = "kdev/";
			len = strlen(lst) + 1;
			break;
		case Hkdev:
			if (!kdev) {
				kdev_len = get_kdev(NULL);
				kdev = kmalloc(kdev_len);
				get_kdev(kdev);
			}
			lst = kdev;
			len = kdev_len;
			break;
		default:
			assert(false);
		}
	}

	switch (req->type) {
	case VFSOP_OPEN:
		if (reqpathcmp(req, "/")) return Hbase;
		if (reqpathcmp(req, "/kdev/")) return Hkdev;
		return -ENOENT;
	case VFSOP_READ:
		return req_readcopy(req, lst, len);
	case VFSOP_GETSIZE:
		return len;
	default:
		return -ENOSYS;
	}
}

static void
accept(VfsReq *req)
{
	vfsreq_finish_short(req, handle(req));
}

void
vfs_root_init(void)
{
	vfs_root_register("", accept);
}