~swisschili/bluejay

a664e72155847451ca4433dd09ff15eb562fd3de — swissChili 9 months ago 480f176
Mount EXT2 to VFS
M .dir-locals.el => .dir-locals.el +4 -3
@@ 1,5 1,6 @@
((nil . ((eval
		  . (setq-local flycheck-clang-include-path
						(jmk-includes-for (expand-file-name buffer-file-name))
		  . (let ((path (expand-file-name (or buffer-file-name "."))))
			  (setq-local flycheck-clang-include-path
						(jmk-includes-for path)
						flycheck-clang-args
						(jmk-other-flags-for (expand-file-name buffer-file-name)))))))
						(jmk-other-flags-for path)))))))

M share/jmk/dir-locals.el => share/jmk/dir-locals.el +4 -3
@@ 1,5 1,6 @@
((nil . ((eval
		  . (setq-local flycheck-clang-include-path
						(jmk-includes-for (expand-file-name buffer-file-name))
		  . (let ((path (expand-file-name (or buffer-file-name "."))))
			  (setq-local flycheck-clang-include-path
						(jmk-includes-for path)
						flycheck-clang-args
						(jmk-other-flags-for (expand-file-name buffer-file-name)))))))
						(jmk-other-flags-for path)))))))

M src/kernel/dri/fs/ext2/ext2.c => src/kernel/dri/fs/ext2/ext2.c +8 -27
@@ 1,6 1,8 @@
#include <alloc.h>
#include <dri/ata_pio/ata_pio.h>
#include <dri/fs/ext2/ext2.h>
#include <dri/fs/ext2/ext2_vfs.h>
#include <vfs.h>
#include <io.h>
#include <kint.h>
#include <log.h>


@@ 145,38 147,17 @@ static bool print_entry(uint inode, const char *name, uint l, void *sb)

void ext2_mount(struct fs_node *where)
{
	UNUSED(where)

	struct ext2_superblock sb = ext2_read_superblock();

	kprintf(DEBUG "EXT2 magic = 0x%x\n", sb.signature);
	struct ext2_superblock *sb = malloc(sizeof(struct ext2_superblock));
	*sb = ext2_read_superblock();

	// Read the root inode 2
	struct ext2_inode root;
	struct fs_node *ext2_root = ext2_inode2vfs(sb, 2, "/", 1);
	uint err;

	if (ext2_find_inode(&sb, 2, &root))
	{
		kprintf(OKAY "Found root inode 2 (size=0x%x, num_blocks=0x%x)\n",
				root.size, root.num_blocks);
		// kprintf(DEBUG "Root.mode = 0x%x\n", root.mode & 0xf000);
		kassert((root.mode & 0xf000) == EXT2_S_IFDIR,
				"Root (inode 2) is not a directory.");

		char *name = "hello-hl.txt";
		kprintf(INFO "Creating hard link %s -> hello.txt\n", name);
		ext2_hard_link(&sb, &root, name, strlen(name), 12);

		kprintf("ls /\n");
		kprintf("inode\t name\n");
		kprintf("--------------------\n");
		ext2_dir_ls(&sb, &root, print_entry, &sb);
	}
	else
	if ((err = fs_mount(where, ext2_root)))
	{
		kprintf(WARN "Failed to find root inode 2\n");
		kprintf(WARN "Failed to mount EXT2: error %d\n", err);
	}

	kprintf(INFO "First free inode is %d\n", ext2_first_free_inode(&sb));
}

bool ext2_valid_filesystem()

M src/kernel/dri/fs/ext2/ext2_vfs.c => src/kernel/dri/fs/ext2/ext2_vfs.c +7 -0
@@ 196,6 196,12 @@ static bool ext2_dirent_to_fs_node_cb(uint inode, const char *name,
	struct ext2_fs_dirent *dent =
		malloc(sizeof(struct ext2_fs_dirent));

	if (strncmp(".", name, name_len) == 0 ||
		strncmp("..", name, name_len) == 0)
	{
		return true;
	}

	dent->node = ext2_inode2vfs(d->sb, inode, (char *)name, name_len);
	dent->name_len = name_len;
	memcpy(dent->name, name, MIN(name_len, 256));


@@ 263,6 269,7 @@ struct fs_node *ext2_inode2vfs(struct ext2_superblock *sb, uint inode,
		struct ext2_fs_dirent_to_fs_data data;
		data.d = d;
		data.last = NULL;
		data.sb = sb;

		ext2_dir_ls(sb, &in, ext2_dirent_to_fs_node_cb, &data);


M src/kernel/main.c => src/kernel/main.c +8 -13
@@ 59,20 59,7 @@ int kmain(struct multiboot_info *mboot)
	kprintf(DEBUG "initrd is at 0x%x to 0x%x\n", initrd_loc);

	init_initrd_vfs(initrd_loc);
#endif

	kprintf(OKAY "VFS initialized\n");

	kprintf(OKAY "Initial setup complete!\n");

#ifdef TEST_VFS_INITRD
	kprintf(INFO "fs_readdir(\"/dev/initrd\")\n");

	struct fs_dirent dirent;
	for (int i = 0; fs_readdir(&root, i, &dirent); i++)
	{
		kprintf(INFO "name: %s, inode: %d\n", dirent.name, dirent.inode);
	}
#endif

	asm("sti");


@@ 116,6 103,14 @@ int kmain(struct multiboot_info *mboot)
			"Filesystem is not a valid EXT2 format, only EXT2 is supported\n");
	}

	kprintf(INFO "fs_readdir(\"/\") mnt=%p\n", root.mount);

	struct fs_dirent dirent;
	for (int i = 0; fs_readdir(&root, i, &dirent); i++)
	{
		kprintf(INFO "name: %s, inode: %d\n", dirent.name, dirent.inode);
	}

	while (true)
		asm("hlt");


M src/kernel/vfs.c => src/kernel/vfs.c +29 -15
@@ 6,6 6,9 @@ struct fs_vtable root_v, dev_v;

uint fs_read(struct fs_node *node, size_t offset, size_t size, uchar *buffer)
{
	if (node->mount)
		node = node->mount;

	if (!node || !node->vtable || !node->vtable->read)
		return 0;



@@ 14,6 17,9 @@ uint fs_read(struct fs_node *node, size_t offset, size_t size, uchar *buffer)

uint fs_write(struct fs_node *node, size_t offset, size_t size, uchar *buffer)
{
	if (node->mount)
		node = node->mount;

	if (!node || !node->vtable || !node->vtable->write)
		return 0;



@@ 22,6 28,9 @@ uint fs_write(struct fs_node *node, size_t offset, size_t size, uchar *buffer)

void fs_open(struct fs_node *node)
{
	if (node->mount)
		node = node->mount;

	if (!node || !node->vtable || !node->vtable->open)
		return;



@@ 30,6 39,9 @@ void fs_open(struct fs_node *node)

void fs_close(struct fs_node *node)
{
	if (node->mount)
		node = node->mount;

	if (!node || !node->vtable || !node->vtable->close)
		return;



@@ 38,6 50,9 @@ void fs_close(struct fs_node *node)

bool fs_readdir(struct fs_node *node, uint index, struct fs_dirent *out)
{
	if (node->mount)
		node = node->mount;

	if (!node || !node->vtable || !node->vtable->readdir ||
		(node->flags & 7) != FS_DIRECTORY)
		return false;


@@ 47,6 62,9 @@ bool fs_readdir(struct fs_node *node, uint index, struct fs_dirent *out)

struct fs_node *fs_finddir(struct fs_node *node, char *name, uint name_len)
{
	if (node->mount)
		node = node->mount;

	if (!node || !node->vtable || !node->vtable->finddir ||
		(node->flags & 7) != FS_DIRECTORY)
		return NULL;


@@ 103,24 121,20 @@ struct fs_node *dev_finddir(struct fs_node *node, char *name, uint name_len)
	return NULL;
}

void init_vfs()
static struct fs_vtable root_vt =
{
	memset(&root_v, 0, sizeof(root_v));
	memset(&dev_v, 0, sizeof(root_v));
	memset(&root, 0, sizeof(root));
	memset(&dev, 0, sizeof(dev));
	NULL,
};

void init_vfs()
{
	root.mount = NULL;
	root.inode = 0; // fake inode
	root.flags = FS_DIRECTORY;
	root.vtable = &root_v;
	dev.flags = FS_DIRECTORY;
	dev.vtable = &dev_v;
	initrd.flags = FS_DIRECTORY;

	root_v.readdir = root_readdir;
	root_v.finddir = root_finddir;

	dev_v.readdir = dev_readdir;
	dev_v.finddir = dev_finddir;
	root.mask = ~0;
	root.gid = root.uid = 0;
	root.size = 0;
	root.vtable = &root_vt;
}

uint fs_mount(struct fs_node *target, struct fs_node *source)