miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
vfs.h File Reference
#include <miniOS/types.h>
#include <miniOS/fs/ext2.h>
#include <miniOS/fs/pipe.h>
Include dependency graph for vfs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  vfs_inode_info
struct  vfs_statfs
struct  vfs_ops
struct  filesystem_type
struct  vfs_mount
struct  vfs_file

Macros

#define VFS_MAX_FDS   64 /* max open files per task */
#define VFS_PATH_MAX   256 /* max path length */
#define VFS_FD_STDIN   0
#define VFS_FD_STDOUT   1
#define VFS_FD_STDERR   2
#define VFS_FIRST_OPEN_FD   3
#define VFS_FILE_TYPE_REG   1
#define VFS_FILE_TYPE_DIR   2
#define VFS_FILE_TYPE_PIPE   3 /* anonymous pipe backed by ring buffer */
#define VFS_FILE_TYPE_SYMLINK   4 /* symbolic link; used by lstat to detect symlink targets */
#define VFS_FILE_TYPE_CHAR   5 /* character device node */
#define VFS_FILE_TYPE_BLK   6 /* block device node */
#define VFS_FILE_TYPE_SOCKET   7 /* BSD-style socket backed by lwIP PCB */
#define VFS_FILE_TYPE_UNIX_SOCKET   8 /* AF_UNIX socket backed by unix_sock_t */
#define VFS_DEVICE_NONE   0
#define VFS_DEVICE_TTY   1
#define VFS_DEVICE_NULL   2
#define VFS_MAX_FS_TYPES   16
#define VFS_MAX_MOUNTS   16

Typedefs

typedef struct vfs_inode_info vfs_inode_info_t
typedef struct vfs_statfs vfs_statfs_t
typedef struct vfs_ops vfs_ops_t
typedef struct filesystem_type filesystem_type_t
typedef struct vfs_mount vfs_mount_t
typedef struct vfs_file vfs_file_t

Functions

int register_filesystem (const char *name, int(*mount)(const char *source, const char *target, const void *data))
int vfs_mount_fstype (const char *fstype, const char *source, const char *target, const void *data)
int vfs_register_mount (const char *point, vfs_ops_t *ops, uint32_t root_ino)
int vfs_mount_count (void)
const vfs_mount_tvfs_get_mount (int idx)
int vfs_path_devno (const char *path)
int vfs_fd_devno (int fd)
void vfs_mount (vfs_ops_t *ops)
int vfs_open (const char *path, int flags, int mode)
int vfs_read (int fd, void *buf, uint32_t len)
int vfs_readdir (int fd, vfs_dirent_cb_t cb, void *ud)
int vfs_write (int fd, const void *buf, uint32_t len)
int vfs_close (int fd)
int vfs_unlink (const char *path)
int vfs_rename (const char *oldpath, const char *newpath)
int vfs_rmdir (const char *path)
int vfs_mkdir (const char *path, int mode)
int vfs_stat (const char *path, vfs_inode_info_t *out)
int vfs_statfs (const char *path, vfs_statfs_t *out)
int vfs_fstatfs (int fd, vfs_statfs_t *out)
int vfs_fstat (int fd, vfs_inode_info_t *out)
int vfs_lstat (const char *path, vfs_inode_info_t *out)
int vfs_symlink (const char *path, const char *target)
int vfs_chmod (const char *path, uint16_t mode)
int vfs_mknod (const char *path, uint16_t mode, uint32_t dev)
int vfs_readlink (const char *path, char *buf, uint32_t bufsiz)

Macro Definition Documentation

◆ VFS_DEVICE_NONE

#define VFS_DEVICE_NONE   0

◆ VFS_DEVICE_NULL

#define VFS_DEVICE_NULL   2

◆ VFS_DEVICE_TTY

#define VFS_DEVICE_TTY   1

◆ VFS_FD_STDERR

#define VFS_FD_STDERR   2

◆ VFS_FD_STDIN

#define VFS_FD_STDIN   0

◆ VFS_FD_STDOUT

#define VFS_FD_STDOUT   1

◆ VFS_FILE_TYPE_BLK

#define VFS_FILE_TYPE_BLK   6 /* block device node */

◆ VFS_FILE_TYPE_CHAR

#define VFS_FILE_TYPE_CHAR   5 /* character device node */

◆ VFS_FILE_TYPE_DIR

#define VFS_FILE_TYPE_DIR   2

◆ VFS_FILE_TYPE_PIPE

#define VFS_FILE_TYPE_PIPE   3 /* anonymous pipe backed by ring buffer */

◆ VFS_FILE_TYPE_REG

#define VFS_FILE_TYPE_REG   1

◆ VFS_FILE_TYPE_SOCKET

#define VFS_FILE_TYPE_SOCKET   7 /* BSD-style socket backed by lwIP PCB */

◆ VFS_FILE_TYPE_SYMLINK

#define VFS_FILE_TYPE_SYMLINK   4 /* symbolic link; used by lstat to detect symlink targets */

◆ VFS_FILE_TYPE_UNIX_SOCKET

#define VFS_FILE_TYPE_UNIX_SOCKET   8 /* AF_UNIX socket backed by unix_sock_t */

◆ VFS_FIRST_OPEN_FD

#define VFS_FIRST_OPEN_FD   3

◆ VFS_MAX_FDS

#define VFS_MAX_FDS   64 /* max open files per task */

◆ VFS_MAX_FS_TYPES

#define VFS_MAX_FS_TYPES   16

◆ VFS_MAX_MOUNTS

#define VFS_MAX_MOUNTS   16

◆ VFS_PATH_MAX

#define VFS_PATH_MAX   256 /* max path length */

Typedef Documentation

◆ filesystem_type_t

◆ vfs_file_t

typedef struct vfs_file vfs_file_t

◆ vfs_inode_info_t

◆ vfs_mount_t

typedef struct vfs_mount vfs_mount_t

◆ vfs_ops_t

typedef struct vfs_ops vfs_ops_t

◆ vfs_statfs_t

typedef struct vfs_statfs vfs_statfs_t

Function Documentation

◆ register_filesystem()

int register_filesystem ( const char * name,
int(* mount )(const char *source, const char *target, const void *data) )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_chmod()

int vfs_chmod ( const char * path,
uint16_t mode )

vfs_chmod() - Change the permission bits of a file by absolute path. @path: Absolute path to the file. @mode: New permission bits (low 12 bits only; type bits are preserved).

Returns
: 0 on success, -1 on error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_close()

int vfs_close ( int fd)

vfs_close() - Release an open file descriptor, flushing write data to disk. @fd: File descriptor to close (0 to VFS_MAX_FDS-1).

If the file was opened with write flags and ops->flush is set, calls ops->flush(inode) to write back the final inode state, then calls ata_flush() to push the ATA write-back cache to stable storage. Clears the in_use flag in the calling task's fd_table entry.

Returns
: 0 on success, -1 if @fd is out of range or not open.

vfs_close() - Release an open file descriptor, flushing write data to disk. @fd: File descriptor to close.

If the file was opened with write flags and ops->flush is set, calls ops->flush(inode) to write back the final inode state, then calls ata_flush() to push the ATA write-back cache to stable storage (EXT2W-07).

Returns
: 0 on success, -1 on invalid or already-closed fd.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_fd_devno()

int vfs_fd_devno ( int fd)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_fstat()

int vfs_fstat ( int fd,
vfs_inode_info_t * out )

vfs_fstat() - Get metadata from an open file descriptor. @fd: Open file descriptor. @out: Output struct populated on success.

Returns
: 0 on success, -1 on bad fd.

vfs_fstat() - Get file metadata from an open file descriptor. @fd: Open fd (must be VFS_FIRST_OPEN_FD <= fd < VFS_MAX_FDS, in_use). @out: Populated with inode, ftype, size on success.

Returns
: 0 on success, -1 on bad fd.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_fstatfs()

int vfs_fstatfs ( int fd,
vfs_statfs_t * out )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_get_mount()

const vfs_mount_t * vfs_get_mount ( int idx)
Here is the caller graph for this function:

◆ vfs_lstat()

int vfs_lstat ( const char * path,
vfs_inode_info_t * out )

vfs_lstat() - Look up file metadata by absolute path WITHOUT following symlinks. @path: Absolute path. @out: Output struct populated on success.

Returns
: 0 on success, -1 on error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_mkdir()

int vfs_mkdir ( const char * path,
int mode )

vfs_mkdir() - Create a directory by absolute path. @path: Absolute path of the new directory (e.g., "/tmp/newdir"). @mode: Permission bits (accepted, passed through to ops->mkdir).

Returns
: 0 on success, -1 if parent not found, ops->mkdir is NULL, or creation fails.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_mknod()

int vfs_mknod ( const char * path,
uint16_t mode,
uint32_t dev )

vfs_mknod() - Create a device special file by absolute path. @path: Absolute path of the new device node. @mode: EXT2_S_IFCHR|perms or EXT2_S_IFBLK|perms. @dev: Encoded device number (major<<8)|minor.

Returns
: 0 on success, -1 on error, -22 on bad mode.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_mount()

void vfs_mount ( vfs_ops_t * ops)

vfs_mount() - Register a filesystem's operations table (backwards-compat shim). @ops: Pointer to a vfs_ops_t struct with lookup, read, and readdir function pointers. Must point to static storage valid for the kernel lifetime.

Calls vfs_register_mount("/", ops) to mount the filesystem at root. Kept for backwards compatibility — prefer vfs_register_mount() for new code.

vfs_mount() - Store the filesystem ops table at root "/" (backwards-compat shim). @ops: Pointer to filesystem ops (must have lookup, read, readdir set).

Calls vfs_register_mount("/", ops). Kept for backwards compatibility.

Here is the call graph for this function:

◆ vfs_mount_count()

int vfs_mount_count ( void )
Here is the caller graph for this function:

◆ vfs_mount_fstype()

int vfs_mount_fstype ( const char * fstype,
const char * source,
const char * target,
const void * data )

vfs_mount_fstype() - Look up a registered filesystem type and mount it. @fstype: Filesystem type name (e.g. "ext2", "sysfs"). @source: Device/source string forwarded to the mount callback (may be NULL). @target: Absolute mount point path (e.g. "/", "/sys"). @data: Filesystem-specific options pointer forwarded to the mount callback (may be NULL for pseudo-filesystems that need no extra parameters).

Returns
: 0 on success, -22 (EINVAL) if @fstype is not registered, or the negative errno returned by the filesystem's mount callback.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_open()

int vfs_open ( const char * path,
int flags,
int mode )

vfs_open() - Open or create a file by absolute path. @path: Absolute path to the file or directory (e.g., "/etc/passwd"). @flags: O_RDONLY (0), O_WRONLY (1), O_CREAT (0x0200). O_CREAT creates file if missing. @mode: File permission bits (accepted but ignored; all files created with 0644).

Calls g_mounted_ops->lookup() to resolve the path to typed inode metadata. If O_CREAT is set and file not found, creates a new file via ext2_create(). Allocates a file descriptor in the calling task's fd_table (per-thread; up to VFS_MAX_FDS=16 concurrent open files). Initialises the vfs_file_t entry with offset=0 and the resolved inode metadata.

Returns
: File descriptor (VFS_FIRST_OPEN_FD to VFS_MAX_FDS-1) on success, -1 if the path is not found, creation fails, or all fds are in use.

vfs_open() - Open or create a file by absolute path. @path: Absolute path. @flags: O_RDONLY (0), O_WRONLY (1), O_CREAT (0x0200). O_CREAT creates file if missing. O_TRUNC (0x0400): Truncate existing regular file to zero on open. @mode: File permission bits for O_CREAT (lower 9 bits used; 0 → 0644 default). Write permission on the parent directory is enforced before creation.

Resolves path through the mount table using longest-prefix match. If O_CREAT is set and lookup fails: parses the parent path, looks up parent inode, and calls the FS create op to allocate a new inode and directory entry.

Returns
: fd index (VFS_FIRST_OPEN_FD to VFS_MAX_FDS-1), or -1 on error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_path_devno()

int vfs_path_devno ( const char * path)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_read()

int vfs_read ( int fd,
void * buf,
uint32_t len )

vfs_read() - Read bytes from an open file descriptor. @fd: File descriptor returned by vfs_open. @buf: Destination buffer. @len: Maximum number of bytes to read.

Dispatches to g_mounted_ops->read() with the fd's inode number and current offset. Advances the offset by the number of bytes read.

Returns
: Number of bytes read (may be less than @len at EOF), 0 at EOF, or -1 on error (invalid fd or read failure).

vfs_read() - Dispatch a read to the mounted filesystem. @fd: Open file descriptor. @buf: Destination buffer. @len: Byte count requested.

Validates fd in range and in_use. Calls fd_table[fd].ops->read() with the stored inode and current offset. Advances offset by bytes_read.

Returns
: Bytes read, 0 at EOF, -1 on bad fd or read error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_readdir()

int vfs_readdir ( int fd,
vfs_dirent_cb_t cb,
void * ud )

vfs_readdir() - Iterate directory entries from an open directory fd. @fd: File descriptor of an open directory (must have ftype == VFS_FILE_TYPE_DIR). @cb: Callback invoked per entry (name, name_len, inode, file_type, ud). @ud: Opaque user data forwarded to @cb.

Dispatches to g_mounted_ops->readdir() with the fd's inode number and current directory offset.

Returns
: 0 when all entries visited, non-zero value from @cb if stopped early, or -1 on invalid fd or if fd is not a directory.

vfs_readdir() - Dispatch directory iteration to the mounted filesystem. @fd: Open file descriptor (must be a directory; ftype == VFS_FILE_TYPE_DIR). @cb: Entry callback. @ud: Opaque user data for @cb.

Validates fd and ftype. Calls fd_table[fd].ops->readdir() with the stored inode, forwarding @cb and @ud.

Returns
: 0 when all entries visited, non-zero from @cb, or -1 on bad fd.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_readlink()

int vfs_readlink ( const char * path,
char * buf,
uint32_t bufsiz )

vfs_readlink() - Read symlink target into buf without following. @path: Absolute path to a symlink inode. @buf: Output buffer (NOT null-terminated per POSIX). @bufsiz: Size of buf.

Returns
: Number of bytes written, or -1 if not a symlink or error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_register_mount()

int vfs_register_mount ( const char * point,
vfs_ops_t * ops,
uint32_t root_ino )

vfs_register_mount() - Register a filesystem at a mount point. @point: Absolute mount point path (e.g. "/", "/tmp"). Copied into mount table. @ops: Pointer to vfs_ops_t with at least lookup, read, readdir set.

Stores entry in g_mounts[]. Up to VFS_MAX_MOUNTS=8 mounts supported. @root_ino: Starting inode for path resolution (pass 0 for fs-default).

Returns
: 0 on success, -1 if mount table is full.

vfs_register_mount() - Register a filesystem at a mount point. @point: Absolute mount point path (e.g. "/", "/tmp"). Copied into mount table. @ops: Pointer to vfs_ops_t with at least lookup, read, readdir set.

Stores entry in g_mounts[]. Up to VFS_MAX_MOUNTS=8 mounts supported.

Returns
: 0 on success, -1 if mount table is full.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_rename()

int vfs_rename ( const char * oldpath,
const char * newpath )

vfs_rename() - Rename or move a file or directory. @oldpath: Absolute path of the source. @newpath: Absolute path of the destination.

Both paths must be on the same mounted filesystem (EXDEV otherwise). If @newpath already exists as a file it is atomically replaced. Directories cannot be overwritten (returns ENOTEMPTY).

Returns
: 0 on success, or a negative errno-style value on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_rmdir()

int vfs_rmdir ( const char * path)

vfs_rmdir() - Delete an empty directory by absolute path. @path: Absolute path of the directory to remove.

Resolves the mount point for @path, looks up the parent directory inode, then calls ops->rmdir(parent_ino, dirname).

Returns
: 0 on success or a negative errno-style value on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_stat()

int vfs_stat ( const char * path,
vfs_inode_info_t * out )

vfs_stat() - Look up file metadata by absolute path. @path: Absolute path. @out: Output struct populated on success.

Returns
: 0 on success, -1 on error.

vfs_stat() - Look up file metadata by absolute path. @path: Absolute path to query. @out: Populated with inode, file_type, size on success.

Returns
: 0 on success, -1 if path not found or no mount.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_statfs()

int vfs_statfs ( const char * path,
vfs_statfs_t * out )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_symlink()

int vfs_symlink ( const char * path,
const char * target )

vfs_symlink() - Create a symbolic link at path pointing to target. @path: Absolute path of the new symlink. @target: Symlink target string (stored as-is; may be relative or absolute).

Returns
: 0 on success, -1 on error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_unlink()

int vfs_unlink ( const char * path)

vfs_unlink() - Delete a file by absolute path. @path: Absolute path of the file to remove (e.g., "/tmp/foo.txt").

Resolves the mount point for @path, looks up the parent directory inode, then calls ops->unlink(parent_ino, filename). Immediate deletion.

Returns
: 0 on success, -1 if not found, ops->unlink is NULL, or delete fails.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_write()

int vfs_write ( int fd,
const void * buf,
uint32_t len )

vfs_write() - Write bytes to an open file descriptor. @fd: File descriptor returned by vfs_open with O_WRONLY. @buf: Source buffer. @len: Number of bytes to write.

Dispatches to g_mounted_ops->write() with the fd's inode and current offset. Advances offset by the number of bytes written.

Returns
: Number of bytes written, or -1 on error (invalid fd, write failure).

vfs_write() - Dispatch a write to the mounted filesystem. @fd: Open file descriptor with O_WRONLY flag. @buf: Source buffer. @len: Byte count to write.

Validates fd. Calls fd_table[fd].ops->write() with the stored inode and current offset. Advances offset and updates size if write extends file.

Returns
: Bytes written, or -1 on bad fd or write error.
Here is the call graph for this function:
Here is the caller graph for this function: