-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvfs.c
More file actions
423 lines (326 loc) · 10.9 KB
/
vfs.c
File metadata and controls
423 lines (326 loc) · 10.9 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "vfs.h"
#include "heap.h"
#include "console.h"
#include "kstring.h"
static struct VfsNode *vfs_root = NULL;
static struct Filesystem *filesystems = NULL;
static struct MountPoint *mount_points = NULL;
struct RamfsEntry {
struct VfsNode node;
uint8_t *data;
size_t capacity;
struct RamfsEntry *children;
struct RamfsEntry *next;
};
static ino_t ramfs_next_inode = 1;
static struct VfsNode *ramfs_open(struct VfsNode *dir, const char *name, int flags);
static void ramfs_close(struct VfsNode *node);
static ssize_t ramfs_read(struct VfsNode *node, void *buf, size_t size, off_t offset);
static ssize_t ramfs_write(struct VfsNode *node, const void *buf, size_t size, off_t offset);
static int ramfs_readdir(struct VfsNode *node, struct VfsDirent *entry, uint32_t index);
static struct VfsNode *ramfs_finddir(struct VfsNode *node, const char *name);
static int ramfs_create(struct VfsNode *dir, const char *name, mode_t mode);
static int ramfs_mkdir(struct VfsNode *dir, const char *name, mode_t mode);
static struct RamfsEntry *ramfs_create_entry(const char *name, uint32_t flags, mode_t mode) {
struct RamfsEntry *entry = kzalloc(sizeof(struct RamfsEntry));
if (!entry) return NULL;
kstrncpy(entry->node.name, name, VFS_MAX_NAME);
entry->node.flags = flags;
entry->node.mode = mode;
entry->node.inode = ramfs_next_inode++;
entry->node.uid = 0;
entry->node.gid = 0;
entry->node.ref_count = 1;
entry->node.open = ramfs_open;
entry->node.close = ramfs_close;
entry->node.read = ramfs_read;
entry->node.write = ramfs_write;
entry->node.readdir = ramfs_readdir;
entry->node.finddir = ramfs_finddir;
entry->node.create = ramfs_create;
entry->node.mkdir = ramfs_mkdir;
entry->node.fs_data = entry;
return entry;
}
static struct VfsNode *ramfs_mount(const char *device, void *options) {
(void)device;
(void)options;
struct RamfsEntry *root = ramfs_create_entry("", VFS_DIRECTORY, 0755);
if (!root) return NULL;
return &root->node;
}
static int ramfs_unmount(struct VfsNode *root) {
(void)root;
return 0;
}
static struct VfsNode *ramfs_open(struct VfsNode *dir, const char *name, int flags) {
struct VfsNode *node = ramfs_finddir(dir, name);
if (!node && (flags & O_CREAT)) {
if (ramfs_create(dir, name, 0644) == 0) {
node = ramfs_finddir(dir, name);
}
}
if (node) {
node->ref_count++;
}
return node;
}
static void ramfs_close(struct VfsNode *node) {
if (node && node->ref_count > 0) {
node->ref_count--;
}
}
static ssize_t ramfs_read(struct VfsNode *node, void *buf, size_t size, off_t offset) {
if (!node || !(node->flags & VFS_FILE)) return -EINVAL;
struct RamfsEntry *entry = (struct RamfsEntry *)node->fs_data;
if (!entry->data) return 0;
if ((size_t)offset >= node->size) return 0;
size_t to_read = size;
if (offset + to_read > node->size) {
to_read = node->size - offset;
}
uint8_t *src = entry->data + offset;
uint8_t *dst = buf;
for (size_t i = 0; i < to_read; i++) {
dst[i] = src[i];
}
return to_read;
}
static ssize_t ramfs_write(struct VfsNode *node, const void *buf, size_t size, off_t offset) {
if (!node || !(node->flags & VFS_FILE)) return -EINVAL;
struct RamfsEntry *entry = (struct RamfsEntry *)node->fs_data;
size_t needed = offset + size;
if (needed > entry->capacity) {
size_t new_cap = needed * 2;
if (new_cap < 4096) new_cap = 4096;
uint8_t *new_data = kmalloc(new_cap);
if (!new_data) return -ENOMEM;
if (entry->data) {
uint8_t *src = entry->data;
for (size_t i = 0; i < node->size; i++) {
new_data[i] = src[i];
}
kfree(entry->data);
}
entry->data = new_data;
entry->capacity = new_cap;
}
const uint8_t *src = buf;
uint8_t *dst = entry->data + offset;
for (size_t i = 0; i < size; i++) {
dst[i] = src[i];
}
if (offset + size > node->size) {
node->size = offset + size;
}
return size;
}
static int ramfs_readdir(struct VfsNode *node, struct VfsDirent *entry, uint32_t index) {
if (!node || !(node->flags & VFS_DIRECTORY)) return -ENOTDIR;
struct RamfsEntry *dir = (struct RamfsEntry *)node->fs_data;
struct RamfsEntry *child = dir->children;
uint32_t i = 0;
while (child && i < index) {
child = child->next;
i++;
}
if (!child) return -1;
entry->inode = child->node.inode;
kstrncpy(entry->name, child->node.name, VFS_MAX_NAME);
return 0;
}
static struct VfsNode *ramfs_finddir(struct VfsNode *node, const char *name) {
if (!node || !(node->flags & VFS_DIRECTORY)) return NULL;
struct RamfsEntry *dir = (struct RamfsEntry *)node->fs_data;
struct RamfsEntry *child = dir->children;
while (child) {
if (kstrcmp(child->node.name, name) == 0) {
return &child->node;
}
child = child->next;
}
return NULL;
}
static int ramfs_create(struct VfsNode *dir, const char *name, mode_t mode) {
if (!dir || !(dir->flags & VFS_DIRECTORY)) return -ENOTDIR;
if (ramfs_finddir(dir, name)) return -EEXIST;
struct RamfsEntry *entry = ramfs_create_entry(name, VFS_FILE, mode);
if (!entry) return -ENOMEM;
struct RamfsEntry *parent = (struct RamfsEntry *)dir->fs_data;
entry->next = parent->children;
parent->children = entry;
return 0;
}
static int ramfs_mkdir(struct VfsNode *dir, const char *name, mode_t mode) {
if (!dir || !(dir->flags & VFS_DIRECTORY)) return -ENOTDIR;
if (ramfs_finddir(dir, name)) return -EEXIST;
struct RamfsEntry *entry = ramfs_create_entry(name, VFS_DIRECTORY, mode);
if (!entry) return -ENOMEM;
struct RamfsEntry *parent = (struct RamfsEntry *)dir->fs_data;
entry->next = parent->children;
parent->children = entry;
return 0;
}
static struct Filesystem ramfs = {
.name = "ramfs",
.mount = ramfs_mount,
.unmount = ramfs_unmount,
.next = NULL
};
void vfs_init(void) {
vfs_register_fs(&ramfs);
}
int vfs_register_fs(struct Filesystem *fs) {
if (!fs) return -EINVAL;
fs->next = filesystems;
filesystems = fs;
return 0;
}
int vfs_mount(const char *path, const char *fs_name, const char *device) {
struct Filesystem *fs = filesystems;
while (fs) {
if (kstrcmp(fs->name, fs_name) == 0) break;
fs = fs->next;
}
if (!fs) return -ENODEV;
struct VfsNode *root = fs->mount(device, NULL);
if (!root) return -EIO;
struct MountPoint *mp = kzalloc(sizeof(struct MountPoint));
if (!mp) {
fs->unmount(root);
return -ENOMEM;
}
kstrncpy(mp->path, path, VFS_MAX_PATH);
mp->root = root;
mp->fs = fs;
mp->next = mount_points;
mount_points = mp;
if (kstrcmp(path, "/") == 0) {
vfs_root = root;
}
return 0;
}
int vfs_unmount(const char *path) {
struct MountPoint *prev = NULL;
struct MountPoint *mp = mount_points;
while (mp) {
if (kstrcmp(mp->path, path) == 0) {
if (prev) {
prev->next = mp->next;
} else {
mount_points = mp->next;
}
mp->fs->unmount(mp->root);
kfree(mp);
return 0;
}
prev = mp;
mp = mp->next;
}
return -EINVAL;
}
struct VfsNode *vfs_resolve_path(const char *path) {
if (!path || path[0] != '/') return NULL;
if (!vfs_root) return NULL;
if (kstrcmp(path, "/") == 0) return vfs_root;
struct VfsNode *node = vfs_root;
char component[VFS_MAX_NAME];
const char *p = path + 1;
while (*p) {
size_t len = 0;
while (*p && *p != '/' && len < VFS_MAX_NAME - 1) {
component[len++] = *p++;
}
component[len] = '\0';
if (len == 0) {
if (*p == '/') p++;
continue;
}
node = vfs_finddir(node, component);
if (!node) return NULL;
if (*p == '/') p++;
}
return node;
}
struct VfsNode *vfs_open(const char *path, int flags) {
struct VfsNode *parent = NULL;
struct VfsNode *node = vfs_resolve_path(path);
if (!node && (flags & O_CREAT)) {
char parent_path[VFS_MAX_PATH];
kstrncpy(parent_path, path, VFS_MAX_PATH);
char *last_slash = parent_path;
for (char *p = parent_path; *p; p++) {
if (*p == '/') last_slash = p;
}
if (last_slash == parent_path) {
parent = vfs_root;
} else {
*last_slash = '\0';
parent = vfs_resolve_path(parent_path);
}
if (parent && parent->create) {
const char *name = last_slash + 1;
if (parent->create(parent, name, 0644) == 0) {
node = vfs_resolve_path(path);
}
}
}
if (node && node->open) {
node = node->open(node, "", flags);
}
return node;
}
void vfs_close(struct VfsNode *node) {
if (node && node->close) {
node->close(node);
}
}
ssize_t vfs_read(struct VfsNode *node, void *buf, size_t size, off_t offset) {
if (!node || !node->read) return -ENODEV;
return node->read(node, buf, size, offset);
}
ssize_t vfs_write(struct VfsNode *node, const void *buf, size_t size, off_t offset) {
if (!node || !node->write) return -ENODEV;
return node->write(node, buf, size, offset);
}
int vfs_readdir(struct VfsNode *node, struct VfsDirent *entry, uint32_t index) {
if (!node || !node->readdir) return -ENODEV;
return node->readdir(node, entry, index);
}
struct VfsNode *vfs_finddir(struct VfsNode *node, const char *name) {
if (!node || !node->finddir) return NULL;
return node->finddir(node, name);
}
int vfs_mkdir(const char *path, mode_t mode) {
char parent_path[VFS_MAX_PATH];
kstrncpy(parent_path, path, VFS_MAX_PATH);
char *last_slash = parent_path;
for (char *p = parent_path; *p; p++) {
if (*p == '/') last_slash = p;
}
const char *name;
struct VfsNode *parent;
if (last_slash == parent_path) {
parent = vfs_root;
name = path + 1;
} else {
*last_slash = '\0';
parent = vfs_resolve_path(parent_path);
name = last_slash + 1;
}
if (!parent || !parent->mkdir) return -ENODEV;
return parent->mkdir(parent, name, mode);
}
int vfs_create(const char *path, mode_t mode) {
struct VfsNode *node = vfs_open(path, O_CREAT | O_EXCL);
if (node) {
node->mode = mode;
vfs_close(node);
return 0;
}
return -EEXIST;
}
int vfs_unlink(const char *path) {
(void)path;
return -ENOSYS;
}