CS350 COS
COS
Loading...
Searching...
No Matches
vfs.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013-2023 Ali Mashtizadeh
3 * All rights reserved.
4 */
5
6#include <stdbool.h>
7#include <stdint.h>
8#include <string.h>
9#include <errno.h>
10
11#include <sys/kassert.h>
12#include <sys/kdebug.h>
13#include <sys/spinlock.h>
14#include <sys/disk.h>
15#include <sys/vfs.h>
16#include <sys/handle.h>
17
18extern VFS *O2FS_Mount(Disk *root);
19
21static VFS *rootFS;
23
26
29
35int
37{
38 int status;
39
41
42 Slab_Init(&vfsSlab, "VFS Slab", sizeof(VFS), 16);
43 Slab_Init(&vnodeSlab, "VNode Slab", sizeof(VNode), 16);
44
45 rootFS = O2FS_Mount(rootDisk);
46 if (!rootFS)
47 return -1;
48
49 status = rootFS->op->getroot(rootFS, &rootNode);
50 if (status < 0)
51 Panic("Failed to get root VNode\n");
52
53 return 0;
54}
55
62VNode *
63VFS_Lookup(const char *path)
64{
65 int status;
66 const char *start = path + 1;
67 const char *end = path + 1;
69 VNode *curNode;
70 VNode *oldNode;
71 char curName[256];
72
73 if (path[0] != '/')
74 return NULL;
75
76 status = rootFS->op->getroot(rootFS, &curNode);
77 if (status < 0)
78 Panic("Failed to get root VNode\n");
79
80 while (1) {
81 while (*end != '\0' && *end != '/')
82 end++;
83
84 len = (size_t)(end - start);
85 if (len == 0) {
86 // Handle root and trailing slash
87 return curNode;
88 }
89 if (len > 256) {
90 // Release
91 return NULL;
92 }
93
94 memcpy(curName, start, len);
95 curName[len] = '\0';
96
97 oldNode = curNode;
98 curNode = NULL;
99 status = oldNode->op->lookup(oldNode, &curNode, curName);
100 if (status < 0) {
101 // Release
102 return NULL;
103 }
104
105 // Release oldNode
106
107 if (*end == '\0') {
108 Log(vfs, "%s %lx\n", path, curNode);
109 return curNode;
110 }
111
112 start = end + 1;
113 end = end + 1;
114 }
115}
116
123int
124VFS_Stat(const char *path, struct stat *sb)
125{
126 VNode *vn = VFS_Lookup(path);
127 if (vn == NULL)
128 return -ENOENT;
129
130 vn->op->stat(vn, sb);
131
132 // Release
133
134 return 0;
135}
136
146int
148{
149 return fn->op->open(fn);
150}
151
161int
163{
164 return fn->op->close(fn);
165}
166
179int
181{
182 return fn->op->read(fn, buf, off, len);
183}
184
197int
199{
200 return fn->op->write(fn, buf, off, len);
201}
202
215int
217{
218 return fn->op->readdir(fn, buf, len, off);
219}
220
#define ENOENT
Definition: errno.h:15
static char buf[4096]
Definition: ethdump.c:10
#define Log(_module, _format,...)
Definition: kassert.h:32
#define DEFINE_SLAB(_type, _pool)
Definition: kmem.h:69
void Slab_Init(Slab *slab, const char *name, uintptr_t objsz, uintptr_t align)
uint64_t len
Definition: multiboot.h:2
#define SPINLOCK_TYPE_NORMAL
Definition: spinlock.h:12
void Spinlock_Init(Spinlock *lock, const char *name, uint64_t type)
Definition: spinlock.c:43
Definition: stat.h:5
#define NULL
Definition: stddef.h:6
void * memcpy(void *dst, const void *src, size_t len)
Definition: string.c:177
Definition: disk.h:11
Definition: kmem.h:46
int(* close)(VNode *fn)
Definition: vfs.h:45
int(* open)(VNode *fn)
Definition: vfs.h:44
int(* stat)(VNode *fn, struct stat *sb)
Definition: vfs.h:46
int(* getroot)(VFS *fs, VNode **dn)
Definition: vfs.h:41
int(* write)(VNode *fn, void *buf, uint64_t off, uint64_t len)
Definition: vfs.h:48
int(* readdir)(VNode *fn, void *buf, uint64_t len, uint64_t *off)
Definition: vfs.h:49
int(* lookup)(VNode *dn, VNode **fn, const char *name)
Definition: vfs.h:43
int(* read)(VNode *fn, void *buf, uint64_t off, uint64_t len)
Definition: vfs.h:47
uint64_t size_t
Definition: types.h:17
unsigned long uint64_t
Definition: types.h:13
static VFS * rootFS
Definition: vfs.c:21
int VFS_Stat(const char *path, struct stat *sb)
Definition: vfs.c:124
static Slab vnodeSlab
Definition: vfs.c:25
static Slab vfsSlab
Definition: vfs.c:24
int VFS_Read(VNode *fn, void *buf, uint64_t off, uint64_t len)
Definition: vfs.c:180
VNode * VFS_Lookup(const char *path)
Definition: vfs.c:63
int VFS_Open(VNode *fn)
Definition: vfs.c:147
static Spinlock vfsLock
Definition: vfs.c:20
static VNode * rootNode
Definition: vfs.c:22
int VFS_ReadDir(VNode *fn, void *buf, uint64_t len, uint64_t *off)
Definition: vfs.c:216
int VFS_MountRoot(Disk *rootDisk)
Definition: vfs.c:36
int VFS_Write(VNode *fn, void *buf, uint64_t off, uint64_t len)
Definition: vfs.c:198
VFS * O2FS_Mount(Disk *root)
Definition: o2fs.c:42
int VFS_Close(VNode *fn)
Definition: vfs.c:162
VFSOp * op
Definition: vfs.h:25
VFSOp * op
Definition: vfs.h:12
Definition: vfs.h:11
Definition: vfs.h:24
void Panic(const char *str)
Definition: vgacons.c:164