Orion
Barry Importing existing Orion kernel d41a53c (3 years, 1 month ago)
diff --git a/vfs/tmpfs/file.c b/vfs/tmpfs/file.c
new file mode 100644
index 0000000..252c88f
--- /dev/null
+++ b/vfs/tmpfs/file.c
@@ -0,0 +1,126 @@
+/*
+ * This file controls access to TmpFS Files. It contains the functions called
+ * by the VFS for any operation on a File struct belonging to TmpFS.
+ */
+
+#include <stddef.h>
+#include <string.h>
+#include <errno.h>
+#include "fs.h"
+#include "../vfs.h"
+#include "../cache.h"
+#include "../../mem/paging.h"
+#include "../../mem/frame.h"
+
+size_t tmpfs_read(File *file, char *buf, size_t size, off_t offset);
+size_t tmpfs_write(File *file, char *buf, size_t size, off_t offset);
+int tmpfs_readdir(File *file, DirEnt *dent, off_t index);
+int tmpfs_open(File *file);
+
+FileOps tmpfsFileOps = {
+ .read = tmpfs_read,
+ .write = tmpfs_write,
+ .readdir = tmpfs_readdir,
+ .open = tmpfs_open,
+};
+
+/* Read a file */
+size_t
+tmpfs_read(File *file, char *buf, size_t size, off_t offset)
+{
+ if (offset > file->inode->size)
+ return 0;
+ if (size + offset > file->inode->size)
+ size = file->inode->size - offset;
+
+ uint16_t min;
+ size_t count = 0;
+ Page *page;
+ page_t oldPage;
+ while (size) {
+ min = (size > 0x1000) ? 0x1000 : size;
+ page = page_find(file->inode, offset);
+ if (!page)
+ break;
+ acquire(&quickPageLock);
+ oldPage = quick_page(page->frame);
+ memcpy(buf + count, QUICK_PAGE, min);
+ quick_page(oldPage);
+ release(&quickPageLock);
+ size -= min;
+ count += min;
+ }
+ return count;
+}
+
+/* Write a file */
+size_t
+tmpfs_write(File *file, char *buf, size_t size, off_t offset)
+{
+ if (size + offset > file->inode->size)
+ file->inode->size = size + offset;
+
+ uint16_t min;
+ size_t count = 0;
+ Page *page;
+ page_t oldPage;
+ while (size) {
+ min = (size > 0x1000) ? 0x1000 : size;
+ page = page_find(file->inode, offset);
+ if (!page)
+ page = page_create(file->inode, alloc_frames(1),
+ offset);
+ acquire(&quickPageLock);
+ oldPage = quick_page(page->frame);
+ memcpy(QUICK_PAGE, buf + count, min);
+ quick_page(oldPage);
+ release(&quickPageLock);
+ size -= min;
+ count += min;
+ }
+ return count;
+}
+
+/* Read a directory entry */
+int
+tmpfs_readdir(File *file, DirEnt *dent, off_t index)
+{
+ DirEntry *de;
+
+ if (!index--) {
+ dent->ino = file->inode->ino;
+ dent->type = DT_DIR;
+ dent->namelen = 2;
+ strncpy(dent->name, ".", dent->namelen);
+ return 0;
+ }
+
+ for (de = file->inode->dirEntries; de && index; de = de->next, index--);
+ if (!de)
+ return -ENOENT;
+ dent->ino = de->inode->ino;
+ if (S_ISBLK(de->inode->mode))
+ dent->type = DT_BLK;
+ if (S_ISCHR(de->inode->mode))
+ dent->type = DT_CHR;
+ if (S_ISDIR(de->inode->mode))
+ dent->type = DT_DIR;
+ if (S_ISFIFO(de->inode->mode))
+ dent->type = DT_FIFO;
+ if (S_ISLNK(de->inode->mode))
+ dent->type = DT_LNK;
+ if (S_ISREG(de->inode->mode))
+ dent->type = DT_REG;
+ if (S_ISSOCK(de->inode->mode))
+ dent->type = DT_SOCK;
+ dent->namelen = strnlen(de->name, NAME_MAX) + 1;
+ strncpy(dent->name, de->name, NAME_MAX);
+ return 0;
+}
+
+/* Open a file */
+int
+tmpfs_open(File *file)
+{
+ return 0;
+}
diff --git a/vfs/tmpfs/fs.h b/vfs/tmpfs/fs.h
new file mode 100644
index 0000000..104f64d
--- /dev/null
+++ b/vfs/tmpfs/fs.h
@@ -0,0 +1,13 @@
+#ifndef KERNEL_VFS_TMPFS_H
+#define KERNEL_VFS_TMPFS_H
+
+#include "../vfs.h"
+
+/* Operations */
+extern SuperOps tmpfsSuperOps;
+extern InodeOps tmpfsInodeOps;
+extern FileOps tmpfsFileOps;
+
+extern FileSystemType tmpfsType;
+
+#endif
diff --git a/vfs/tmpfs/inode.c b/vfs/tmpfs/inode.c
new file mode 100644
index 0000000..5da5408
--- /dev/null
+++ b/vfs/tmpfs/inode.c
@@ -0,0 +1,69 @@
+/*
+ * This file contains the functions dealing with TmpFS inodes. The VFS will
+ * call these when it performs operations on TmpFS Inodes, or is dealing with
+ * the TmpFS hierarchy.
+ */
+
+#include <string.h>
+#include "fs.h"
+#include "../vfs.h"
+#include "../../mem/heap.h"
+
+int tmpfs_create(Inode *inode, DirEntry *entry, mode_t mode);
+Inode *tmpfs_lookup(Inode *inode, const char *name);
+int tmpfs_mkdir(Inode *inode, DirEntry *entry, mode_t mode);
+int tmpfs_rmdir(Inode *inode, DirEntry *entry);
+int tmpfs_mknod(Inode *inode, DirEntry *entry, mode_t mode, dev_t dev);
+int tmpfs_rename(Inode *si, DirEntry *sde, Inode *di, DirEntry *dde);
+
+InodeOps tmpfsInodeOps = {
+ .create = tmpfs_create,
+ .lookup = tmpfs_lookup,
+ .mkdir = tmpfs_mkdir,
+ .rmdir = tmpfs_rmdir,
+ .mknod = tmpfs_mknod,
+ .rename = tmpfs_rename,
+};
+
+/* Create a file */
+int
+tmpfs_create(Inode *inode, DirEntry *entry, mode_t mode)
+{
+ return 0;
+}
+
+
+/* Look up a file */
+Inode *
+tmpfs_lookup(Inode *inode, const char *name)
+{
+ return NULL;
+}
+
+/* Make a directory */
+int
+tmpfs_mkdir(Inode *inode, DirEntry *entry, mode_t mode)
+{
+ return 0;
+}
+
+/* Remove a directory */
+int
+tmpfs_rmdir(Inode *inode, DirEntry *entry)
+{
+ return 0;
+}
+
+/* Make a node */
+int
+tmpfs_mknod(Inode *inode, DirEntry *entry, mode_t mode, dev_t dev)
+{
+ return 0;
+}
+
+/* Rename/mode a directory entry */
+int
+tmpfs_rename(Inode *si, DirEntry *sde, Inode *di, DirEntry *dde)
+{
+ return 0;
+}
diff --git a/vfs/tmpfs/super.c b/vfs/tmpfs/super.c
new file mode 100644
index 0000000..ede1961
--- /dev/null
+++ b/vfs/tmpfs/super.c
@@ -0,0 +1,65 @@
+/*
+ * This file controls the superblock for TmpFS. It supports mounting new TmpFS
+ * filesystems. The VFS will use the calls here when dealing directly with the
+ * filesystem structure.
+ */
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include "fs.h"
+#include "../vfs.h"
+#include "../super.h"
+#include "../inode.h"
+#include "../../mem/heap.h"
+
+Inode *tmpfs_mount(FileSystemType *type, int flags,
+ const char *dev, void *data);
+Inode *tmpfs_alloc_inode(SuperBlock *sb);
+
+FileSystemType tmpfsType = {
+ .name = "TmpFS",
+ .mount = tmpfs_mount,
+// .kill_sb = tmpfs_kill_sb,
+};
+
+SuperOps tmpfsSuperOps = {
+ .alloc_inode = tmpfs_alloc_inode,
+// .free_inode = tmpfs_free_inode,
+// .write_inode = tmpfs_write_inode,
+// .delete_inode = tmpfs_delete_inode,
+};
+
+/* Mount a TmpFS instance */
+Inode *
+tmpfs_mount(FileSystemType *type, int flags, const char *dev, void *data)
+{
+ if (type != &tmpfsType)
+ return NULL;
+
+ SuperBlock *super = kmalloc(sizeof(SuperBlock));
+
+ super->type = type;
+ super->ops = &tmpfsSuperOps;
+ init_lock(&super->lock);
+
+ Inode *inode = super_alloc_inode(super);
+ inode->mode = S_IFDIR | 0755;
+ super->root = inode;
+
+ return inode;
+}
+
+/* Allocate an inode */
+Inode *
+tmpfs_alloc_inode(SuperBlock *sb)
+{
+ Inode *inode = kmalloc(sizeof(Inode));
+ init_lock(&inode->lock);
+ inode->ops = &tmpfsInodeOps;
+ inode->fileOps = &tmpfsFileOps;
+ inode->super = sb;
+ inode->nlink = 1;
+ return inode_get(inode); /* This ensures that the inode is never free */
+}