From 4f18d62840a6a467e79d591efa8e167146e283cc Mon Sep 17 00:00:00 2001 From: Hubert Badocha Date: Fri, 17 Jan 2025 13:57:47 +0100 Subject: [PATCH] syscalls: create sys_statvfs syscalls JIRA: RTOS-965 --- include/msg.h | 4 ++- include/posix-types.h | 2 ++ include/syscalls.h | 4 ++- posix/posix.c | 66 +++++++++++++++++++++++++++++++++++++++++++ posix/posix.h | 4 +++ syscalls.c | 19 +++++++++++++ 6 files changed, 97 insertions(+), 2 deletions(-) diff --git a/include/msg.h b/include/msg.h index a364c09f0..941c91cb0 100644 --- a/include/msg.h +++ b/include/msg.h @@ -35,7 +35,9 @@ enum { /* Directory operations */ mtLookup, mtLink, mtUnlink, mtReaddir, - mtCount + mtCount, + + mtStat = 0xf53 }; /* clang-format on */ diff --git a/include/posix-types.h b/include/posix-types.h index b6aed5e1f..badac33d1 100644 --- a/include/posix-types.h +++ b/include/posix-types.h @@ -27,6 +27,8 @@ typedef int ino_t; /* FIXME: should be unsigned long long to encode id_t? */ typedef int nlink_t; typedef int blksize_t; typedef long long blkcnt_t; +typedef unsigned long long fsblkcnt_t; +typedef unsigned long long fsfilcnt_t; #endif diff --git a/include/syscalls.h b/include/syscalls.h index da0103f05..0099210c7 100644 --- a/include/syscalls.h +++ b/include/syscalls.h @@ -121,5 +121,7 @@ ID(sbi_getchar) \ ID(sigreturn) \ \ - ID(mprotect) + ID(mprotect) \ + \ + ID(sys_statvfs) /* clang-format on */ diff --git a/posix/posix.c b/posix/posix.c index 8e39128d8..d95d90348 100644 --- a/posix/posix.c +++ b/posix/posix.c @@ -477,6 +477,72 @@ static int posix_create(const char *filename, int type, mode_t mode, oid_t dev, return err; } +int posix_statvfs(const char *path, int fildes, struct statvfs *stat) +{ + oid_t oid, dev; + oid_t *oidp, *devp; + open_file_t *f; + msg_t msg; + int err = EOK; + + if (((path == NULL) && (fildes < 0)) || + ((path != NULL) && (fildes != -1))) { + return -EINVAL; + } + + if (path == NULL) { + err = posix_getOpenFile(fildes, &f); + if (err < 0) { + return err; + } + oidp = &f->oid; + } else { + if (proc_lookup(path, &oid, &dev) < 0) { + return -ENOENT; + } + oidp = &oid; + devp = &dev; + } + + /* Detect mountpoint */ + if ((devp != NULL) && (oidp->port != devp->port)) { + msg.type = mtGetAttr; + hal_memcpy(&msg.oid, oidp, sizeof(*oidp)); + msg.i.attr.type = atMode; + + if ((proc_send(oidp->port, &msg) < 0) || (msg.o.err < 0)) { + (void)posix_fileDeref(f); + return -EIO; + } + + if (S_ISDIR(msg.o.attr.val)) { + oidp = devp; + } + } + + hal_memset(stat, 0, sizeof(*stat)); + + msg.type = mtStat; + msg.o.data = stat; + msg.o.size = sizeof(*stat); + + if (proc_send(oid.port, &msg) < 0) { + err = -EIO; + } else { + err = msg.o.err; + } + + if (path == NULL) { + if (err == EOK) { + err = posix_fileDeref(f); + } else { + (void)posix_fileDeref(f); + } + } + + return err; +} + /* TODO: handle O_CREAT and O_EXCL */ int posix_open(const char *filename, int oflag, char *ustack) diff --git a/posix/posix.h b/posix/posix.h index 83d8042e0..40db295de 100644 --- a/posix/posix.h +++ b/posix/posix.h @@ -23,6 +23,7 @@ #include "include/posix-poll.h" #include "include/posix-socket.h" #include "include/posix-stat.h" +#include "include/posix-statvfs.h" #include "include/posix-stdio.h" #include "include/posix-timespec.h" #include "include/posix-uio.h" @@ -77,6 +78,9 @@ extern int posix_chmod(const char *path, mode_t mode); extern int posix_fstat(int fd, struct stat *buf); +extern int posix_statvfs(const char *path, int fd, struct statvfs *buf); + + extern int posix_fsync(int fd); diff --git a/syscalls.c b/syscalls.c index 6f2b11a32..5e077b94b 100644 --- a/syscalls.c +++ b/syscalls.c @@ -1273,6 +1273,25 @@ int syscalls_sys_fstat(char *ustack) } +int syscalls_sys_statvfs(char *ustack) +{ + process_t *proc = proc_current()->process; + int fd; + const char *path; + struct statvfs *stat; + + GETFROMSTACK(ustack, const char *, path, 0); + GETFROMSTACK(ustack, int, fd, 1); + GETFROMSTACK(ustack, struct statvfs *, stat, 2); + + if (vm_mapBelongs(proc, stat, sizeof(*stat)) < 0) { + return -EFAULT; + } + + return posix_statvfs(path, fd, stat); +} + + int syscalls_sys_fsync(char *ustack) { int fd;