aboutsummaryrefslogtreecommitdiffstats
path: root/src/kernel/vfs.c
blob: b3af7fce3cd0736c8a9df4b2681b3ed41b91b12d (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
 * @file vfs.c
 * An implementation of filesystem abstraction
 *
 * Copyright (C) 2018 Clyne Sullivan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "vfs.h"

#include <kernel/task.h>
#include <fs/stdio.h>

// +1 vol for stdio, +3 fd's for stdout, in, err
#define VFS_MAX_VOLS  (1 + 8)
#define VFS_MAX_FILES (3 + 10)

static vfs_volume vfs_volumes[VFS_MAX_VOLS];
static vfs_file vfs_files[VFS_MAX_FILES];

#define VFS_PID_CHECK(fpid) { \
	uint32_t pid = task_getpid(); \
	if (pid != fpid && fpid != 0) \
		return 0; }

void vfs_svc(uint32_t n, uint32_t *ret, uint32_t *args)
{
	switch (n) {
	case 0:
		*((int *)ret) = vfs_mount((vfs_volume_funcs *)args[0],
			args[1]);
		break;
	case 1:
		*((int *)ret) = vfs_open((const char *)args[0], args[1]);
		break;
	case 2:
		*((int *)ret) = vfs_close(args[0]);
		break;
	case 3:
		*((int *)ret) = vfs_read(args[0], args[1], (uint8_t *)args[2]);
		break;
	case 4:
		*((int *)ret) = vfs_write(args[0], args[1], (const uint8_t *)args[2]);
		break;
	default:
		break;
	}
}

void vfs_init(void)
{
    // Mark all volumes and files as empty
	for (int i = 0; i < VFS_MAX_VOLS; i++)
		vfs_volumes[i].flags = 0;
	for (int i = 0; i < VFS_MAX_FILES; i++)
		vfs_files[i].flags = 0;

	vfs_mount(&stdio_funcs, 0);
    // Order of opening here may be important TODO confirm
	vfs_open("   in", VFS_FILE_READ);
	vfs_open("   out", VFS_FILE_WRITE);
	vfs_open("   err", VFS_FILE_WRITE);
}

int vfs_mount(const vfs_volume_funcs *funcs, uint32_t flags)
{
	for (int i = 0; i < VFS_MAX_VOLS; i++) {
		if (!(vfs_volumes[i].flags && VFS_MOUNTED)) {
			vfs_volumes[i].flags = VFS_MOUNTED | flags;
			vfs_volumes[i].funcs = funcs;
			return i;
		}
	}

	return -1;
}

int vfs_get_drive(const char *path)
{
	// Validate parameters
	if (path[0] == '\0')
		return -1;

	// Default to 'A' if no drive specified (A gives stdio)
	if (path[1] == '\0' || path[1] != ':' ||
		path[2] == '\0' || path[2] != '/')
		return 0;

	// Find chosen drive
	int drive = -1;
	for (int i = 0; i < VFS_MAX_VOLS; i++) {
		if (path[0] == ('a' + i) || path[0] == ('A' + i)) {
			drive = i;
			break;
		}
	}

	if (drive == -1 || !(vfs_volumes[drive].flags && VFS_MOUNTED))
		return -1;

	return drive;
}

int vfs_open(const char *path, uint32_t flags)
{
	int drive = vfs_get_drive(path);
	if (drive == -1)
		return -1;
	if (vfs_volumes[drive].funcs->open == 0)
		return -1;

	// Find available file handle
	int file = -1;
	for (int i = 0; i < VFS_MAX_FILES; i++) {
		if (!(vfs_files[i].flags & VFS_FILE_OPEN)) {
			file = i;
			break;
		}
	}

	if (file == -1)
		return -1;

	void *fsinfo = vfs_volumes[drive].funcs->open(path + 3);
	if (fsinfo == 0)
		return -1;

	vfs_files[file].flags = VFS_FILE_OPEN | flags;
	vfs_files[file].vol = drive;
	vfs_files[file].pid = task_getpid();
	vfs_files[file].info.pos = 0;
	vfs_files[file].info.fsinfo = fsinfo;

	return file;
}

int vfs_close(int fd)
{
	if (fd < 0 || fd > VFS_MAX_FILES)
		return -1;
	if (vfs_volumes[vfs_files[fd].vol].funcs->close == 0)
		return -1;
	VFS_PID_CHECK(vfs_files[fd].pid);
	if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
		return 0;

	// TODO care
	/*int ret =*/ vfs_volumes[vfs_files[fd].vol].funcs->close(
		&vfs_files[fd].info);

	vfs_files[fd].flags = 0;
	vfs_files[fd].pid = 0;
	return 0;
}

uint32_t vfs_read(int fd, uint32_t count, uint8_t *buffer)
{
	if (fd < 0 || fd > VFS_MAX_FILES || count == 0 || buffer == 0)
		return 0;
	if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
		return -1;
	if (vfs_volumes[vfs_files[fd].vol].funcs->read == 0)
		return -1;
	VFS_PID_CHECK(vfs_files[fd].pid);
	if ((!(vfs_files[fd].flags & VFS_FILE_READ)) || (vfs_files[fd].flags &
		VFS_EOF))
		return 0;

	uint32_t ret = vfs_volumes[vfs_files[fd].vol].funcs->read(
		&vfs_files[fd].info, count, buffer);

	if (ret < count)
		vfs_files[fd].flags |= VFS_EOF;

	return ret;
}

uint32_t vfs_write(int fd, uint32_t count, const uint8_t *buffer)
{
	if (fd < 0 || fd > VFS_MAX_FILES || count == 0 || buffer == 0)
		return 0;
	if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
		return -1;
	if (vfs_volumes[vfs_files[fd].vol].funcs->write == 0)
		return -1;
	VFS_PID_CHECK(vfs_files[fd].pid);
	// TODO append?
	if ((!(vfs_files[fd].flags & VFS_FILE_WRITE)) || (vfs_files[fd].flags &
		VFS_EOF))
		return 0;

	uint32_t ret = vfs_volumes[vfs_files[fd].vol].funcs->write(
		&vfs_files[fd].info, count, buffer);

	if (ret < count)
		vfs_files[fd].flags |= VFS_EOF;

	return ret;
}

int vfs_seek(int fd, int32_t offset, int whence)
{
	if (fd < 0 || fd > VFS_MAX_FILES)
		return -1;
	if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
		return -1;
	if (vfs_volumes[vfs_files[fd].vol].funcs->seek == 0)
		return -1;
	VFS_PID_CHECK(vfs_files[fd].pid);

	return vfs_volumes[vfs_files[fd].vol].funcs->seek(&vfs_files[fd].info,
		offset, whence);
}

int32_t vfs_tell(int fd)
{
	if (fd < 0 || fd > VFS_MAX_FILES)
		return -1;
	if (!(vfs_files[fd].flags & VFS_FILE_OPEN))
		return -1;
	VFS_PID_CHECK(vfs_files[fd].pid);

	return (int32_t)vfs_files[fd].info.pos;
}