Nucleus
Barry Read-only Ext2 file system driver 648c56b (3 years, 2 months ago)
diff --git a/vfs/ext2fs/super.c b/vfs/ext2fs/super.c
new file mode 100644
index 0000000..f7949f0
--- /dev/null
+++ b/vfs/ext2fs/super.c
@@ -0,0 +1,67 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <nucleus/task.h>
+#include <nucleus/vfs.h>
+#include "ext2fs.h"
+
+Inode *ext2fs_alloc_inode(SuperBlock *sb);
+
+const uint16_t EXT2_SIGNATURE = 0xEF53;
+
+SuperOps ext2fsSuperOps = {
+ .alloc_inode = ext2fs_alloc_inode,
+};
+
+/* Mount an ext2fs instance */
+Inode *
+ext2fs_mount(FSType *type, int flags, const char *dev, void *data)
+{
+ /* Open backing file */
+ int fd = open(dev, O_RDONLY);
+ if (fd < 0)
+ return (void *) fd;
+ File *back = get(get_file_by_fd(fd));
+ close(fd);
+ if (!S_ISBLK(back->inode->mode)) {
+ put(back);
+ return (void *) -ENOTBLK;
+ }
+
+ /* Read super block */
+ struct Ext2Super *rsuper = kmalloc(sizeof(struct Ext2Super));
+ back->pos = 1024;
+ file_read(back, (char *) rsuper, sizeof(struct Ext2Super));
+ if (rsuper->signature != EXT2_SIGNATURE)
+ goto error;
+ if (rsuper->requiredFeatures & 0x01)
+ /* Compression required */
+ goto error;
+ if ((rsuper->writableFeatures & 0x02) && !(flags & MS_RDONLY))
+ /* 64-bit file system */
+ goto error;
+
+ SuperBlock *super = new(&superBlockType);
+ super->type = get(type);
+ super->ops = &ext2fsSuperOps;
+ super->back = back;
+ super->data = rsuper;
+ super->root = new(&inodeType);
+ ext2fs_read_vnode(super, 2, super->root);
+
+ return super->root;
+
+error:
+ put(back);
+ kfree(rsuper);
+ return (void *) -EINVAL;
+}
+
+/* Allocate an inode */
+Inode *
+ext2fs_alloc_inode(SuperBlock *sb)
+{
+ return NULL;
+}