Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
vfs.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <sys/kassert.h>
#include <sys/kdebug.h>
#include <sys/spinlock.h>
#include <sys/disk.h>
#include <sys/vfs.h>
#include <sys/handle.h>
Include dependency graph for vfs.c:

Go to the source code of this file.

Functions

VFSO2FS_Mount (Disk *root)
 
 DEFINE_SLAB (VFS, &vfsSlab)
 
 DEFINE_SLAB (VNode, &vnodeSlab)
 
int VFS_MountRoot (Disk *rootDisk)
 
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)
 

Variables

static Spinlock vfsLock
 
static VFSrootFS
 
static VNoderootNode
 
static Slab vfsSlab
 
static Slab vnodeSlab
 

Function Documentation

◆ DEFINE_SLAB() [1/2]

DEFINE_SLAB ( VFS  ,
vfsSlab 
)

◆ DEFINE_SLAB() [2/2]

DEFINE_SLAB ( VNode  ,
vnodeSlab 
)

◆ O2FS_Mount()

VFS * O2FS_Mount ( Disk root)

Definition at line 42 of file o2fs.c.

43{
44 int status;
45 VFS *fs = VFS_Alloc();
46 BufCacheEntry *entry;
47 SuperBlock *sb;
48
49 ASSERT(sizeof(BDirEntry) == 512);
50
51 if (!fs)
52 return NULL;
53
54 status = BufCache_Read(disk, 0, &entry);
55 if (status < 0) {
56 Alert(o2fs, "Disk cache read failed\n");
57 return NULL;
58 }
59
60 // Read superblock
61 sb = entry->buffer;
62 if (memcmp(sb->magic, SUPERBLOCK_MAGIC, 8) != 0) {
63 Alert(o2fs, "Invalid file system\n");
64 BufCache_Release(entry);
65 return NULL;
66 }
69 Alert(o2fs, "Unsupported file system version\n");
70 BufCache_Release(entry);
71 return NULL;
72 }
73
74 // Read bitmap
75 for (int i = 0; i < sb->bitmapSize; i++) {
76 ASSERT(i < 16);
77
78 BufCacheEntry *bentry;
79 uint64_t offset = sb->bitmapOffset + i * sb->blockSize;
80
81 if (BufCache_Read(disk, offset, &bentry) < 0) {
82 Alert(o2fs, "Bitmap read failed\n");
83 for (i = 0; i < 16; i++)
85 BufCache_Release(entry);
86 return NULL;
87 }
88
89 fs->bitmap[i] = bentry;
90 }
91
92 DLOG(o2fs, "File system mounted\n");
93 DLOG(o2fs, "Root @ 0x%llx\n", sb->root.offset);
94
95 fs->fsptr = entry;
96 fs->fsval = sb->root.offset;
97 fs->blksize = sb->blockSize;
98
99 // Setup VFS structure
100 fs->op = &O2FSOperations;
101 fs->disk = disk;
102 Spinlock_Init(&fs->lock, "O2FS Lock", SPINLOCK_TYPE_NORMAL);
103 fs->refCount = 1;
104 fs->root = NULL;
105
106 status = O2FS_GetRoot(fs, &fs->root);
107 if (status < 0) {
108 Alert(o2fs, "Mount failed");
109 BufCache_Release(entry);
110 return NULL;
111 }
112
113 return fs;
114}
int BufCache_Read(Disk *disk, uint64_t diskOffset, BufCacheEntry **entry)
Definition: bufcache.c:229
void BufCache_Release(BufCacheEntry *entry)
Definition: bufcache.c:205
#define DLOG(_module, _format,...)
Definition: kassert.h:37
#define Alert(_module, _format,...)
Definition: kassert.h:28
#define ASSERT(_x)
Definition: kassert.h:8
static VFSOp O2FSOperations
Definition: o2fs.c:29
int O2FS_GetRoot(VFS *fs, VNode **dn)
Definition: o2fs.c:353
#define SUPERBLOCK_MAGIC
Definition: o2fs.h:63
uint8_t magic[8]
Definition: o2fs.h:47
ObjID root
Definition: o2fs.h:58
uint16_t versionMinor
Definition: o2fs.h:49
uint16_t versionMajor
Definition: o2fs.h:48
uint64_t offset
Definition: o2fs.h:39
uint64_t bitmapSize
Definition: o2fs.h:54
uint64_t blockSize
Definition: o2fs.h:53
#define O2FS_VERSION_MINOR
Definition: o2fs.h:31
uint64_t bitmapOffset
Definition: o2fs.h:55
#define O2FS_VERSION_MAJOR
Definition: o2fs.h:30
Definition: o2fs.h:99
#define SPINLOCK_TYPE_NORMAL
Definition: spinlock.h:12
void Spinlock_Init(Spinlock *lock, const char *name, uint64_t type)
Definition: spinlock.c:43
#define NULL
Definition: stddef.h:6
int memcmp(const void *b1, const void *b2, size_t len)
Definition: string.c:192
void * buffer
Definition: bufcache.h:11
unsigned long uint64_t
Definition: types.h:13
void * bitmap[16]
Definition: vfs.h:21
uint64_t blksize
Definition: vfs.h:19
Disk * disk
Definition: vfs.h:13
Spinlock lock
Definition: vfs.h:14
uint64_t fsval
Definition: vfs.h:18
VFSOp * op
Definition: vfs.h:12
VNode * root
Definition: vfs.h:20
void * fsptr
Definition: vfs.h:17
uint64_t refCount
Definition: vfs.h:15
Definition: vfs.h:11
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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
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
static VFS * rootFS
Definition: vfs.c:21
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)
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
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:

Variable Documentation

◆ rootFS

VFS* rootFS
static

Definition at line 21 of file vfs.c.

◆ rootNode

VNode* rootNode
static

Definition at line 22 of file vfs.c.

◆ vfsLock

Spinlock vfsLock
static

Definition at line 20 of file vfs.c.

◆ vfsSlab

Slab vfsSlab
static

Definition at line 24 of file vfs.c.

◆ vnodeSlab

Slab vnodeSlab
static

Definition at line 25 of file vfs.c.