CS350 COS
COS
Loading...
Searching...
No Matches
vfs.h File Reference
#include <sys/kmem.h>
#include <sys/stat.h>
Include dependency graph for vfs.h:

Go to the source code of this file.

Data Structures

struct  VFS
 
struct  VNode
 
struct  VFSOp
 

Typedefs

typedef struct VFSOp VFSOp
 
typedef struct VNode VNode
 
typedef struct VFS VFS
 

Functions

 DECLARE_SLAB (VFS)
 
 DECLARE_SLAB (VNode)
 
int VFS_MountRoot (Disk *root)
 
VNodeVFS_Lookup (const char *path)
 
int VFS_Stat (const char *path, struct stat *sb)
 
int VFS_Open (VNode *fn)
 
int VFS_Close (VNode *fn)
 
int VFS_Read (VNode *fn, void *buf, uint64_t off, uint64_t len)
 
int VFS_Write (VNode *fn, void *buf, uint64_t off, uint64_t len)
 
int VFS_ReadDir (VNode *fn, void *buf, uint64_t len, uint64_t *off)
 

Data Structure Documentation

◆ VFS

struct VFS

Definition at line 11 of file vfs.h.

Collaboration diagram for VFS:
[legend]
Data Fields
void * bitmap[16]
uint64_t blksize
Disk * disk
void * fsptr
uint64_t fsval
Spinlock lock
VFSOp * op
uint64_t refCount
VNode * root

◆ VNode

struct VNode

Definition at line 24 of file vfs.h.

Collaboration diagram for VNode:
[legend]
Data Fields
Disk * disk
void * fsptr
uint64_t fsval
Spinlock lock
VFSOp * op
uint64_t refCount
VFS * vfs

Typedef Documentation

◆ VFS

typedef struct VFS VFS

◆ VFSOp

typedef struct VFSOp VFSOp

Definition at line 8 of file vfs.h.

◆ VNode

typedef struct VNode VNode

Definition at line 9 of file vfs.h.

Function Documentation

◆ DECLARE_SLAB() [1/2]

DECLARE_SLAB ( VFS  )

◆ DECLARE_SLAB() [2/2]

DECLARE_SLAB ( VNode  )

◆ VFS_Close()

int VFS_Close ( VNode fn)

VFS_Close –

Close a vnode.

Parameters
[in]fnVNode to close.
Returns
Return status

Definition at line 162 of file vfs.c.

163{
164 return fn->op->close(fn);
165}
int(* close)(VNode *fn)
Definition: vfs.h:45
VFSOp * op
Definition: vfs.h:25
Here is the caller graph for this function:

◆ VFS_Lookup()

VNode * VFS_Lookup ( const char *  path)

VFS_Lookup –

Lookup a VNode by a path. This function recursively searches the directory heirarchy until the given path is found otherwise returns NULL if not found.

Definition at line 63 of file vfs.c.

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}
#define Log(_module, _format,...)
Definition: kassert.h:32
uint64_t len
Definition: multiboot.h:2
#define NULL
Definition: stddef.h:6
void * memcpy(void *dst, const void *src, size_t len)
Definition: string.c:177
int(* getroot)(VFS *fs, VNode **dn)
Definition: vfs.h:41
int(* lookup)(VNode *dn, VNode **fn, const char *name)
Definition: vfs.h:43
uint64_t size_t
Definition: types.h:17
unsigned long uint64_t
Definition: types.h:13
static VFS * rootFS
Definition: vfs.c:21
VFSOp * op
Definition: vfs.h:12
Definition: vfs.h:24
void Panic(const char *str)
Definition: vgacons.c:164
Here is the call graph for this function:
Here is the caller graph for this function:

◆ VFS_MountRoot()

int VFS_MountRoot ( Disk rootDisk)

VFS_MountRoot –

Mount the root file system from a specific disk.

Definition at line 36 of file vfs.c.

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}
void Slab_Init(Slab *slab, const char *name, uintptr_t objsz, uintptr_t align)
#define SPINLOCK_TYPE_NORMAL
Definition: spinlock.h:12
void Spinlock_Init(Spinlock *lock, const char *name, uint64_t type)
Definition: spinlock.c:43
static Slab vnodeSlab
Definition: vfs.c:25
static Slab vfsSlab
Definition: vfs.c:24
static Spinlock vfsLock
Definition: vfs.c:20
static VNode * rootNode
Definition: vfs.c:22
VFS * O2FS_Mount(Disk *root)
Definition: o2fs.c:42
Definition: vfs.h:11
Here is the call graph for this function:
Here is the caller graph for this function:

◆ VFS_Open()

int VFS_Open ( VNode fn)

VFS_Open –

Open a vnode for reading and writing.

Parameters
[in]fnVNode to open.
Returns
Return status

Definition at line 147 of file vfs.c.

148{
149 return fn->op->open(fn);
150}
int(* open)(VNode *fn)
Definition: vfs.h:44
Here is the caller graph for this function:

◆ VFS_Read()

int VFS_Read ( VNode fn,
void *  buf,
uint64_t  off,
uint64_t  len 
)

VFS_Read –

Read from a vnode.

Parameters
[in]fnVNode to read from.
[in]bufBuffer to write the data to.
[in]offFile offset in bytes.
[in]lenLength to read in bytes.
Returns
Return status

Definition at line 180 of file vfs.c.

181{
182 return fn->op->read(fn, buf, off, len);
183}
static char buf[4096]
Definition: ethdump.c:10
int(* read)(VNode *fn, void *buf, uint64_t off, uint64_t len)
Definition: vfs.h:47
Here is the caller graph for this function:

◆ VFS_ReadDir()

int VFS_ReadDir ( VNode fn,
void *  buf,
uint64_t  len,
uint64_t off 
)

VFS_ReadDir –

Read a directory entry from a vnode.

Parameters
[in]fnVNode to read from.
[in]bufBuffer to write the data to.
[in]offDirectory offset in bytes.
[in]lenLength to read in bytes.
Returns
Return status

Definition at line 216 of file vfs.c.

217{
218 return fn->op->readdir(fn, buf, len, off);
219}
int(* readdir)(VNode *fn, void *buf, uint64_t len, uint64_t *off)
Definition: vfs.h:49
Here is the caller graph for this function:

◆ VFS_Stat()

int VFS_Stat ( const char *  path,
struct stat sb 
)

VFS_Stat –

Return the struct stat that contains the file and directory information for a given VNode.

Definition at line 124 of file vfs.c.

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}
#define ENOENT
Definition: errno.h:15
int(* stat)(VNode *fn, struct stat *sb)
Definition: vfs.h:46
VNode * VFS_Lookup(const char *path)
Definition: vfs.c:63
Here is the call graph for this function:
Here is the caller graph for this function:

◆ VFS_Write()

int VFS_Write ( VNode fn,
void *  buf,
uint64_t  off,
uint64_t  len 
)

VFS_Write –

Write from a vnode.

Parameters
[in]fnVNode to write to.
[in]bufBuffer to read the data from.
[in]offFile offset in bytes.
[in]lenLength to read in bytes.
Returns
Return status

Definition at line 198 of file vfs.c.

199{
200 return fn->op->write(fn, buf, off, len);
201}
int(* write)(VNode *fn, void *buf, uint64_t off, uint64_t len)
Definition: vfs.h:48
Here is the caller graph for this function: