miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
vfs.c File Reference
#include <miniOS/fs/vfs.h>
#include <miniOS/fs/ext2.h>
#include <miniOS/fs/pipe.h>
#include <miniOS/drivers/tty.h>
#include <miniOS/drivers/ata.h>
#include <miniOS/sched/sched.h>
#include <miniOS/io.h>
#include <string.h>
Include dependency graph for vfs.c:

Macros

#define VFS_ELOOP   40
#define VFS_MAX_SYMLINK_DEPTH   8

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_mount_count (void)
const vfs_mount_tvfs_get_mount (int idx)
static int vfs_abs_path (const char *path, char *buf)
static vfs_mount_tvfs_find_mount (const char *path, const char **relative_out)
int vfs_path_devno (const char *path)
int vfs_fd_devno (int fd)
int vfs_statfs (const char *path, vfs_statfs_t *out)
int vfs_fstatfs (int fd, vfs_statfs_t *out)
int vfs_register_mount (const char *point, vfs_ops_t *ops, uint32_t root_ino)
void vfs_mount (vfs_ops_t *ops)
static void vfs_activate_root (const vfs_mount_t *mnt)
static int vfs_resolve_path (const char *path, char *out_path, vfs_inode_info_t *out_info, int depth)
static int alloc_fd (void)
int vfs_open (const char *path, int flags, int mode)
int vfs_read (int fd, void *buf, uint32_t len)
int vfs_write (int fd, const void *buf, uint32_t len)
int vfs_readdir (int fd, vfs_dirent_cb_t cb, void *ud)
int vfs_close (int fd)
int vfs_stat (const char *path, vfs_inode_info_t *out)
int vfs_lstat (const char *path, vfs_inode_info_t *out)
int vfs_readlink (const char *path, char *buf, uint32_t bufsiz)
int vfs_symlink (const char *path, const char *target)
int vfs_fstat (int fd, vfs_inode_info_t *out)
int vfs_unlink (const char *path)
int vfs_rmdir (const char *path)
int vfs_mkdir (const char *path, int mode)
int vfs_chmod (const char *path, uint16_t mode)
int vfs_mknod (const char *path, uint16_t mode, uint32_t dev)
int vfs_rename (const char *oldpath, const char *newpath)

Variables

static vfs_mount_t g_mounts [VFS_MAX_MOUNTS]
static int g_num_mounts = 0
static filesystem_type_t g_fs_types [VFS_MAX_FS_TYPES]
static int g_num_fs_types = 0

Macro Definition Documentation

◆ VFS_ELOOP

#define VFS_ELOOP   40

◆ VFS_MAX_SYMLINK_DEPTH

#define VFS_MAX_SYMLINK_DEPTH   8

Function Documentation

◆ alloc_fd()

int alloc_fd ( void )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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_abs_path()

int vfs_abs_path ( const char * path,
char * buf )
static

vfs_abs_path() - Expand a potentially-relative path to absolute using thread CWD. If @path starts with '/', copies it verbatim to @buf. Otherwise, prepends the calling thread's cwd (with '/' separator as needed). @path: Input path (absolute or relative). @buf: Output buffer of VFS_PATH_MAX bytes.

Returns
: 0 on success, -36 (ENAMETOOLONG) if result exceeds VFS_PATH_MAX-1.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vfs_activate_root()

void vfs_activate_root ( const vfs_mount_t * mnt)
inlinestatic
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.

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_find_mount()

vfs_mount_t * vfs_find_mount ( const char * path,
const char ** relative_out )
static

vfs_find_mount() - Find the deepest mount point that is a prefix of path. Uses longest-prefix-match. Boundary check: next char after mount point must be '/' or '\0' to prevent "/tmp" matching "/tmpfoo". @path: Absolute path to resolve. @relative_out: Set to the subpath after the mount point (leading '/' stripped).

Returns
: Pointer to matching vfs_mount_t, or NULL if no match.
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 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() - 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. @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() - 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() - 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.

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_resolve_path()

int vfs_resolve_path ( const char * path,
char * out_path,
vfs_inode_info_t * out_info,
int depth )
static

vfs_resolve_path() - Resolve a path, following symlinks up to VFS_MAX_SYMLINK_DEPTH hops. @path: Absolute input path to resolve. @out_path: Output buffer (VFS_PATH_MAX bytes) filled with the final resolved path. @out_info: Populated with final inode metadata on success. @depth: Current hop depth (caller passes 0).

Returns
: 0 on success, -2 (ENOENT) if path not found, -40 (ELOOP) on cycle.
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 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() - 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:

Variable Documentation

◆ g_fs_types

filesystem_type_t g_fs_types[VFS_MAX_FS_TYPES]
static

◆ g_mounts

vfs_mount_t g_mounts[VFS_MAX_MOUNTS]
static

◆ g_num_fs_types

int g_num_fs_types = 0
static

◆ g_num_mounts

int g_num_mounts = 0
static