From 171ea4905076a85f2f729ebe3df328e243b61b34 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 19 Jul 2026 20:33:52 -0400 Subject: [PATCH 1/3] fs: make buf on write const Signed-off-by: Michael Shaw --- fs/fat32/inode.c | 2 +- fs/vfs/devfs.c | 4 ++-- fs/vfs/ramfs.c | 2 +- fs/vfs/uapi.c | 48 ++++++++++++++++++++++++++++++++++++++++++++---- fs/vfs/vfs.c | 2 +- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/fs/fat32/inode.c b/fs/fat32/inode.c index 7240848..e5efa37 100644 --- a/fs/fat32/inode.c +++ b/fs/fat32/inode.c @@ -16,7 +16,7 @@ static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset) return -ENOSYS; } -static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset) +static ssize_t write(inode_t *inode, const char *buf, size_t size, size_t offset) { return -ENOSYS; } diff --git a/fs/vfs/devfs.c b/fs/vfs/devfs.c index 6b1aad2..b21833f 100644 --- a/fs/vfs/devfs.c +++ b/fs/vfs/devfs.c @@ -18,7 +18,7 @@ static ssize_t i_read_chr(inode_t *inode, char *buf, size_t size, size_t offset) return 1; } -static ssize_t i_write_chr(inode_t *inode, char *buf, size_t size, size_t offset) +static ssize_t i_write_chr(inode_t *inode, const char *buf, size_t size, size_t offset) { for (size_t i = offset; i < size + offset; i++) tty_putchar(inode->data, buf[offset + i]); @@ -31,7 +31,7 @@ static ssize_t i_read_blk(inode_t *inode, char *buf, size_t n_blocks, size_t off return block_read(inode->data, buf, offset, n_blocks); } -static ssize_t i_write_blk(inode_t *inode, char *buf, size_t n_blocks, size_t offset) +static ssize_t i_write_blk(inode_t *inode, const char *buf, size_t n_blocks, size_t offset) { return block_write(inode->data, buf, offset, n_blocks); } diff --git a/fs/vfs/ramfs.c b/fs/vfs/ramfs.c index b7276c9..eaed320 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -105,7 +105,7 @@ static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset) return size; } -static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset) +static ssize_t write(inode_t *inode, const char *buf, size_t size, size_t offset) { size_t new_size = size + offset; diff --git a/fs/vfs/uapi.c b/fs/vfs/uapi.c index 8b400c1..f3031d8 100644 --- a/fs/vfs/uapi.c +++ b/fs/vfs/uapi.c @@ -6,24 +6,64 @@ #include #include +static struct file *check_fd(int fd) +{ + struct proc *proc = get_cur_proc(); + + if (fd >= proc->fd_array_size) + return NULL; + + struct file *f = proc->fdt + fd; + if (!f->dirent) + return NULL; + + return f; +} + +static ssize_t do_read(dirent_t *dirent, void *buf, size_t nbyte, off_t offset) +{ + return vfs_read(dirent->inode, buf, nbyte, offset); +} + +static ssize_t do_write(dirent_t *dirent, const void *buf, size_t nbyte, off_t offset) +{ + return vfs_write(dirent->inode, buf, nbyte, offset); +} + static ssize_t sys_pread(int fd, void *buf, size_t nbyte, off_t offset) { - return -ENOSYS; + struct file *f = check_fd(fd); + if (!f) + return -EBADF; + + return do_read(f->dirent, buf, nbyte, offset); } static ssize_t sys_read(int fd, void *buf, size_t nbyte) { - return -ENOSYS; + struct file *f = check_fd(fd); + if (!f) + return -EBADF; + + return do_read(f->dirent, buf, nbyte, f->offset); } static ssize_t sys_pwrite(int fd, const void *buf, size_t nbyte, off_t offset) { - return -ENOSYS; + struct file *f = check_fd(fd); + if (!f) + return -EBADF; + + return do_write(f->dirent, buf, nbyte, offset); } static ssize_t sys_write(int fd, const void *buf, size_t nbyte) { - return -ENOSYS; + struct file *f = check_fd(fd); + if (!f) + return -EBADF; + + return do_write(f->dirent, buf, nbyte, f->offset); } static int sys_open(const char *path, int flags) diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index 4bc46b6..43762f9 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -199,7 +199,7 @@ int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out) return 0; } -ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset) +ssize_t vfs_write(inode_t *ino, const char *buf, size_t size, size_t offset) { if (!ino) return -EINVAL; From be701fdd0b35163818df26bb610a938129a7c6ec Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 19 Jul 2026 20:38:25 -0400 Subject: [PATCH 2/3] block: make write buf const Signed-off-by: Michael Shaw --- include/kernel/block.h | 8 ++++---- include/kernel/cache.h | 2 +- kernel/block/block.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/kernel/block.h b/include/kernel/block.h index 11e8924..c1064ab 100644 --- a/include/kernel/block.h +++ b/include/kernel/block.h @@ -28,17 +28,17 @@ struct block_device { size_t lba_start; struct block_device *part_of; /* if it is a partition */ - void *priv; + void *priv; - size_t sg_max_len; - size_t sg_max_segments; + size_t sg_max_len; + size_t sg_max_segments; struct block_ops ops; struct disk disk; }; ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks); -ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n_blocks); +ssize_t block_write(struct block_device *dev, const char *buf, size_t offset, size_t n_blocks); int block_check_partitions(struct block_device *bdev); diff --git a/include/kernel/cache.h b/include/kernel/cache.h index 3ecea0e..5131237 100644 --- a/include/kernel/cache.h +++ b/include/kernel/cache.h @@ -4,6 +4,6 @@ #include -void cache_flush_range(void *addr, size_t len); +void cache_flush_range(const void *addr, size_t len); #endif /* _CACHE_H */ diff --git a/kernel/block/block.c b/kernel/block/block.c index 77875db..2f0576f 100644 --- a/kernel/block/block.c +++ b/kernel/block/block.c @@ -80,7 +80,7 @@ ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_ return ret; } -ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n_blocks) +ssize_t block_write(struct block_device *dev, const char *buf, size_t offset, size_t n_blocks) { while (dev->part_of) { offset += dev->lba_start; From fdc7d5ce5ce30f166621704be46c05e331550f5b Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 19 Jul 2026 20:39:02 -0400 Subject: [PATCH 3/3] vfs: make write buf const Signed-off-by: Michael Shaw --- include/fs/inode.h | 2 +- include/fs/vfs.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/fs/inode.h b/include/fs/inode.h index abf220d..87a8b18 100644 --- a/include/fs/inode.h +++ b/include/fs/inode.h @@ -13,7 +13,7 @@ typedef struct _fs fs_t; struct inode_ops { ssize_t (*read)(inode_t *inode, char *buf, size_t size, size_t offset); - ssize_t (*write)(inode_t *inode, char *buf, size_t size, size_t offset); + ssize_t (*write)(inode_t *inode, const char *buf, size_t size, size_t offset); int (*link)(inode_t *parent_dir, inode_t *child, const char *name, dirent_t **out); int (*unlink)(inode_t *parent_dir, const char *name); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 60955f0..5672042 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -118,7 +118,7 @@ fs_t *vfs_get_root(void); struct mount *vfs_get_root_mount(void); struct mount *mount_find_child(struct mount *parent, dirent_t *dent); -ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset); +ssize_t vfs_write(inode_t *ino, const char *buf, size_t size, size_t offset); ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset); #endif /* _VFS_H_ */