Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syscalls: create sys_statvfs syscalls #631

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ enum {
/* Directory operations */
mtLookup, mtLink, mtUnlink, mtReaddir,

mtCount
mtCount,

mtStat = 0xf53
};

/* clang-format on */
Expand Down
38 changes: 38 additions & 0 deletions include/posix-statvfs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Phoenix-RTOS
*
* Operating system kernel
*
* POSIX-compatibility definitions - statvfs
*
* Copyright 2025 Phoenix Systems
* Author: Hubert Badocha
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/


#ifndef _PHOENIX_POSIX_STATVFS_H_
#define _PHOENIX_POSIX_STATVFS_H_

#include "posix-types.h"


struct statvfs {
unsigned long f_bsize; /* Filesystem block size */
unsigned long f_frsize; /* Fundamental filesystem block size */
fsblkcnt_t f_blocks; /* Number of blocks on filesystem (in f_frsize units) */
fsblkcnt_t f_bfree; /* Number of free blocks */
fsblkcnt_t f_bavail; /* Number of free blocks available to non-privileged process */
fsfilcnt_t f_files; /* Number of inodes */
fsfilcnt_t f_ffree; /* Number of free inodes */
fsfilcnt_t f_favail; /* Number of free inodes available to non-privileged process */
unsigned long f_fsid; /* Filesystem ID */
unsigned long f_flag; /* Filesystem flags */
unsigned long f_namemax; /* Maximum filename length */
};


#endif
2 changes: 2 additions & 0 deletions include/posix-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion include/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,7 @@
ID(sbi_getchar) \
ID(sigreturn) \
\
ID(mprotect)
ID(mprotect) \
\
ID(sys_statvfs)
/* clang-format on */
71 changes: 71 additions & 0 deletions posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,77 @@ 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;
devp = NULL;
}
else {
if (proc_lookup(path, &oid, &dev) < 0) {
return -ENOENT;
}
oidp = &oid;
devp = &dev;
}

/* Detect mountpoint */
if ((devp != NULL) && (oidp->port != devp->port)) {
hal_memset(&msg, 0, sizeof(msg));
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)) {
return -EIO;
}

if (S_ISDIR(msg.o.attr.val)) {
oidp = devp;
}
}

hal_memset(stat, 0, sizeof(*stat));

hal_memset(&msg, 0, sizeof(msg));
msg.type = mtStat;
msg.o.data = stat;
msg.o.size = sizeof(*stat);

if (proc_send(oidp->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)
Expand Down
4 changes: 4 additions & 0 deletions posix/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "include/posix-poll.h"
#include "include/posix-socket.h"
#include "include/posix-stat.h"
#include "include/posix-statvfs.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this file included in the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad

#include "include/posix-stdio.h"
#include "include/posix-timespec.h"
#include "include/posix-uio.h"
Expand Down Expand Up @@ -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 *stat);


extern int posix_fsync(int fd);


Expand Down
19 changes: 19 additions & 0 deletions syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading