|
miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
|
#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>
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_t * | vfs_get_mount (int idx) |
| static int | vfs_abs_path (const char *path, char *buf) |
| static vfs_mount_t * | vfs_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 |
| #define VFS_ELOOP 40 |
| #define VFS_MAX_SYMLINK_DEPTH 8 |
|
static |


| int register_filesystem | ( | const char * | name, |
| int(* | mount )(const char *source, const char *target, const void *data) ) |


|
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.


|
inlinestatic |

| 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).


| 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).


| int vfs_fd_devno | ( | int | fd | ) |


|
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).


| 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.


| int vfs_fstatfs | ( | int | fd, |
| vfs_statfs_t * | out ) |


| const vfs_mount_t * vfs_get_mount | ( | int | idx | ) |

| 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.


| 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).


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.


| 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.

| int vfs_mount_count | ( | void | ) |

| 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).


| 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.


| int vfs_path_devno | ( | const char * | path | ) |


| 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.


| 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.


| 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.


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.


| 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).


|
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).


| 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).


| 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.


| int vfs_statfs | ( | const char * | path, |
| vfs_statfs_t * | out ) |


| 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).


| 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.


| 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.


|
static |
|
static |
|
static |
|
static |