From e05d72ba8f09866b768f3da7776b807072ed7b9b Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Mon, 23 Oct 2023 22:13:19 +0200 Subject: VFS/libc: Create a syscall for mkdir and add the function to libc --- Makefile | 2 +- cpu/syscall.c | 2 ++ fs/ext2.c | 4 +++- fs/shm.c | 2 +- fs/tmpfs.c | 5 +++-- fs/vfs.c | 29 +++++++++++++++++++++++++++++ fs/vfs.h | 7 +++++-- scalls/mkdir.c | 5 +++++ scalls/mkdir.h | 2 ++ socket.c | 2 +- userland/libc/include/syscall.h | 1 + userland/libc/sys/stat/mkdir.c | 7 ++----- 12 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 scalls/mkdir.c create mode 100644 scalls/mkdir.h diff --git a/Makefile b/Makefile index 74f52b4..32b521f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC="./sysroot/bin/i686-sb-gcc" AS="./sysroot/bin/i686-sb-as" -OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o scalls/ppoll.o kubsan.o scalls/mmap.o drivers/serial.o scalls/accept.o scalls/bind.o scalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o scalls/shm.o elf.o ksbrk.o sched/scheduler.o scalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o scalls/msleep.o scalls/uptime.o +OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o cpu/int_syscall.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o scalls/ppoll.o kubsan.o scalls/mmap.o drivers/serial.o scalls/accept.o scalls/bind.o scalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o scalls/shm.o elf.o ksbrk.o sched/scheduler.o scalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o scalls/msleep.o scalls/uptime.o scalls/mkdir.o CFLAGS = -O2 -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. INCLUDE=-I./includes/ -I./libc/include/ diff --git a/cpu/syscall.c b/cpu/syscall.c index 19c195f..4682c53 100644 --- a/cpu/syscall.c +++ b/cpu/syscall.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -159,6 +160,7 @@ void (*syscall_functions[])() = { (void(*))syscall_socket, (void(*))syscall_shm_open, (void(*))syscall_ftruncate, (void(*))syscall_stat, (void(*))syscall_msleep, (void(*))syscall_uptime, + (void(*))syscall_mkdir, }; void syscall_function_handler(uint32_t eax, uint32_t arg1, uint32_t arg2, diff --git a/fs/ext2.c b/fs/ext2.c index a0caaa5..51bb13a 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -504,7 +504,8 @@ vfs_inode_t *ext2_open(const char *path) { return vfs_create_inode(inode_num, type, 1 /*has_data*/, 1 /*can_write*/, 1 /*is_open*/, NULL /*internal_object*/, file_size, ext2_open, ext2_create_file, ext2_read, ext2_write, - ext2_close, NULL /*get_vm_object*/); + ext2_close, ext2_create_directory, + NULL /*get_vm_object*/); } uint64_t end_of_last_entry_position(int dir_inode, uint64_t *entry_offset, @@ -697,6 +698,7 @@ vfs_inode_t *ext2_mount(void) { root->write = ext2_write; root->close = ext2_close; root->create_file = ext2_create_file; + root->create_directory = ext2_create_directory; parse_superblock(); return root; } diff --git a/fs/shm.c b/fs/shm.c index 00f8dfb..8668c9a 100644 --- a/fs/shm.c +++ b/fs/shm.c @@ -64,7 +64,7 @@ int shm_open(const char *name, int oflag, mode_t mode) { vfs_create_inode(0 /*inode_num*/, 0 /*type*/, 1 /*has_data*/, 1 /*can_write*/, 1 /*is_open*/, internal_object, 0 /*file_size*/, NULL /*open*/, NULL /*create_file*/, - shm_read, shm_write, NULL /*close*/, shm_get_vm_object); + shm_read, shm_write, NULL /*close*/, NULL/*create_directory*/, shm_get_vm_object); vfs_fd_t *fd_ptr; int fd = vfs_create_fd(oflag, mode, inode, &fd_ptr); diff --git a/fs/tmpfs.c b/fs/tmpfs.c index 2c5974b..575ab0c 100644 --- a/fs/tmpfs.c +++ b/fs/tmpfs.c @@ -47,7 +47,8 @@ void dual_pipe(int fd[2]) { vfs_inode_t *inode = vfs_create_inode( 0 /*inode_num*/, 0 /*type*/, has_data, can_write, is_open, internal_object, 0 /*file_size*/, NULL /*open*/, NULL /*create_file*/, - tmp_read, tmp_write, tmp_close, NULL /*get_vm_object*/); + tmp_read, tmp_write, tmp_close, NULL /*create_directory*/, + NULL /*get_vm_object*/); assert(inode); vfs_fd_t *fd_ptr; @@ -76,7 +77,7 @@ void pipe(int fd[2]) { vfs_inode_t *inode = vfs_create_inode( 0 /*inode_num*/, 0 /*type*/, has_data, can_write, is_open, internal_object, 0 /*file_size*/, NULL /*open*/, NULL /*create_file*/, - tmp_read, tmp_write, tmp_close, NULL /*get_vm_object*/); + tmp_read, tmp_write, tmp_close, NULL/*create_directory*/, NULL /*get_vm_object*/); assert(inode); vfs_fd_t *fd_ptr; diff --git a/fs/vfs.c b/fs/vfs.c index 9842e11..5f61d89 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -30,6 +30,7 @@ vfs_inode_t *vfs_create_inode( int (*read)(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd), int (*write)(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd), void (*close)(vfs_fd_t *fd), + int (*create_directory)(const char *path, int mode), vfs_vm_object_t *(*get_vm_object)(uint64_t length, uint64_t offset, vfs_fd_t *fd)) { vfs_inode_t *r = kmalloc(sizeof(inode_t)); @@ -45,6 +46,7 @@ vfs_inode_t *vfs_create_inode( r->read = read; r->write = write; r->close = close; + r->create_directory = create_directory; r->get_vm_object = get_vm_object; return r; } @@ -155,6 +157,33 @@ char *vfs_resolve_path(const char *file, char *resolved_path) { return final; } +int vfs_mkdir(const char *path, int mode) { + vfs_mounts_t *file_mount = 0; + int length = 0; + for (int i = 0; i < num_mounts; i++) { + int path_len = strlen(mounts[i].path); + if (path_len <= length) + continue; + + if (isequal_n(mounts[i].path, path, path_len)) { + length = path_len; + file_mount = &mounts[i]; + } + } + if (1 != length) + path += length; + + if (!file_mount) { + kprintf("vfs_internal_open could not find mounted path for file : %s\n", + path); + return 0; + } + assert(file_mount->local_root->create_directory); + // TODO: Error checking, don't just assume it is fine + file_mount->local_root->create_directory(path, mode); + return 0; +} + int vfs_open(const char *file, int flags, int mode) { char resolved_path[256] = {0}; vfs_resolve_path(file, resolved_path); diff --git a/fs/vfs.h b/fs/vfs.h index b3ef3e1..b8a9984 100644 --- a/fs/vfs.h +++ b/fs/vfs.h @@ -47,8 +47,8 @@ struct vfs_fd { int flags; int mode; int reference_count; // Number of usages of this file descriptor, - // once it reaches zero then the contents can - // be freed. + // once it reaches zero then the contents can + // be freed. vfs_inode_t *inode; }; @@ -65,6 +65,7 @@ struct vfs_inode { int (*read)(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd); int (*write)(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd); void (*close)(vfs_fd_t *fd); + int (*create_directory)(const char *path, int mode); vfs_vm_object_t *(*get_vm_object)(uint64_t length, uint64_t offset, vfs_fd_t *fd); }; @@ -80,6 +81,7 @@ int vfs_pread(int fd, void *buf, uint64_t count, uint64_t offset); vfs_vm_object_t *vfs_get_vm_object(int fd, uint64_t length, uint64_t offset); int vfs_dup2(int org_fd, int new_fd); vfs_inode_t *vfs_internal_open(const char *file); +int vfs_mkdir(const char *path, int mode); int vfs_create_fd(int flags, int mode, vfs_inode_t *inode, vfs_fd_t **fd); vfs_inode_t *vfs_create_inode( int inode_num, int type, uint8_t has_data, uint8_t can_write, @@ -89,6 +91,7 @@ vfs_inode_t *vfs_create_inode( int (*read)(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd), int (*write)(uint8_t *buffer, uint64_t offset, uint64_t len, vfs_fd_t *fd), void (*close)(vfs_fd_t *fd), + int (*create_directory)(const char *path, int mode), vfs_vm_object_t *(*get_vm_object)(uint64_t length, uint64_t offset, vfs_fd_t *fd)); #endif diff --git a/scalls/mkdir.c b/scalls/mkdir.c new file mode 100644 index 0000000..43f2424 --- /dev/null +++ b/scalls/mkdir.c @@ -0,0 +1,5 @@ +#include + +int syscall_mkdir(const char *path, int mode) { + return vfs_mkdir(path, mode); +} diff --git a/scalls/mkdir.h b/scalls/mkdir.h new file mode 100644 index 0000000..0bf0043 --- /dev/null +++ b/scalls/mkdir.h @@ -0,0 +1,2 @@ +#include +int syscall_mkdir(const char *path, int mode); diff --git a/socket.c b/socket.c index 0e7437c..8328b38 100644 --- a/socket.c +++ b/socket.c @@ -128,7 +128,7 @@ int socket(int domain, int type, int protocol) { 0 /*inode_num*/, FS_TYPE_UNIX_SOCKET, 0 /*has_data*/, 1 /*can_write*/, 1 /*is_open*/, new_socket /*internal_object*/, 0 /*file_size*/, NULL /*open*/, NULL /*create_file*/, socket_read, socket_write, - socket_close, NULL /*get_vm_object*/); + socket_close, NULL/*create_directory*/, NULL /*get_vm_object*/); vfs_fd_t *fd; int n = vfs_create_fd(O_RDWR | O_NONBLOCK, 0, inode, &fd); diff --git a/userland/libc/include/syscall.h b/userland/libc/include/syscall.h index caa7779..b3863d2 100644 --- a/userland/libc/include/syscall.h +++ b/userland/libc/include/syscall.h @@ -31,6 +31,7 @@ #define SYS_STAT 23 #define SYS_MSLEEP 24 #define SYS_UPTIME 25 +#define SYS_MKDIR 26 int syscall(uint32_t eax, uint32_t ebx, uint32_t ecx, uint32_t edx, uint32_t esi, uint32_t edi); diff --git a/userland/libc/sys/stat/mkdir.c b/userland/libc/sys/stat/mkdir.c index c057fa7..927afd8 100644 --- a/userland/libc/sys/stat/mkdir.c +++ b/userland/libc/sys/stat/mkdir.c @@ -1,9 +1,6 @@ #include -#include +#include int mkdir(const char *path, mode_t mode) { - (void)path; - (void)mode; - assert(0); // TODO: Implement - return 0; + return syscall(SYS_MKDIR, (void *)path, (void *)mode, 0, 0, 0); } -- cgit v1.2.3