summaryrefslogtreecommitdiff
path: root/src/kernel/vfs/mount.c
blob: fa5d65b26da9c013926f95b20fb42b202537abb6 (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
#include <kernel/malloc.h>
#include <kernel/panic.h>
#include <kernel/vfs/mount.h>
#include <shared/mem.h>

static VfsMount *mount_root = NULL;

VfsMount *vfs_mount_seed(void) {
	return mount_root;
}

void vfs_root_register(const char *path, void (*accept)(VfsReq *)) {
	VfsBackend *backend = kmalloc(sizeof *backend, "root bck");
	VfsMount *mount = kmalloc(sizeof *mount, "root mnt");
	*backend = (VfsBackend) {
		.is_user = false,
		.usehcnt = 1,
		.kern.accept = accept,
	};
	*mount = (VfsMount){
		.prev = mount_root,
		.prefix = path,
		.prefix_len = strlen(path),
		.backend = backend,
		.refs = 1,
	};
	mount_root = mount;
}


VfsMount *vfs_mount_resolve(
		VfsMount *top, const char *path, size_t path_len)
{
	for (; top; top = top->prev) {
		if (top->prefix_len > path_len)
			continue;
		if (memcmp(top->prefix, path, top->prefix_len) != 0)
			continue;

		/* ensure that there's no garbage after the match
		 * e.g. don't recognize /thisasdf as mounted under /this */

		if (top->prefix_len == path_len) // can't happen if prefix == path
			break;
		if (path[top->prefix_len] == '/')
			break;

		/* Also valid if prefix ends with '/'. Can only happen with kernel-
		 * provided mounts. */
		if (top->prefix_len != 0 && path[top->prefix_len-1] == '/')
			break;
	}
	return top;
}

void vfs_mount_remref(VfsMount *mnt) {
	assert(mnt);
	assert(mnt->refs > 0);
	if (--(mnt->refs) > 0) return;

	VfsMount *prev = mnt->prev;
	if (mnt->backend) {
		vfsback_userdown(mnt->backend);
	}
	if (mnt->prefix_owned) {
		kfree((void*)mnt->prefix);
	}
	kfree(mnt);

	if (prev) {
		vfs_mount_remref(prev);
	}
}