/* File service for ring-3 commands (see syscall.h for the ABI). * * Filesystem-agnostic: paths resolve through the VFS (vfs.c), so FAT volumes * and the ramfs behave the same. Two properties shape how these run: * - the FAT driver keeps state in static buffers, so no other process may * run its code concurrently; * - the floppy driver waits for IRQ6, so interrupts must stay enabled. * So instead of a lock we disable preemption for the duration: the scheduler * will not switch away from the running process while the count is set. * That keeps each operation atomic (and immune to Ctrl+C killing the process * mid-write) at the cost of a brief stall during disk I/O. Buffers point * into the caller's address space, which is also the active one during the * syscall, so no copy in/out is needed. * * A destructive `format` runs as a kernel thread without the BKL and sets * format_busy while it rewrites a disk. Disk-backed mounts wait for it; * the RAM-backed mounts (/ramfs, /share) do not, so the WM's periodic * /share stats write and the terminal keep running during a format. */ #include "syscall.h" #include "fatformat.h" #include "process.h" #include "vfs.h" /* Wait for a destructive format that owns the disks while its FAT is * half-written. Sleeping drops the BKL so the formatter can finish. */ static void fs_format_wait(void) { while (format_busy && current_proc()->pid != format_owner_pid) { if (!process_is_alive(format_owner_pid)) { format_busy = 0; break; } process_sleep_ms(10); } } /* A boot-time `storage` scan (or a concurrent storage command) is rebuilding * the mount table and reading through the FAT static buffers. Wait for it * instead of racing. */ static void fs_scan_wait(void) { while (vfs_scan_busy && current_proc()->pid != vfs_scan_owner_pid) { if (vfs_scan_owner_pid && !process_is_alive(vfs_scan_owner_pid)) { vfs_scan_busy = 0; break; } process_sleep_ms(10); } } static int fs_resolve(const char *path, struct vfs_mount **m, char *rel, unsigned int rel_size); /* The disk may need its IRQ (floppy), and the FAT driver's static state must * not be preempted in the middle of an operation. */ static void fs_preempt_enter(void) { preempt_disable(); __asm__ volatile ("sti"); } void sys_fs_enter(void) { /* Used by disk-touching services (the storage scan, ELF loads): wait * for a format and a mount-table rebuild, then take the non- * preemptible section. */ fs_format_wait(); fs_scan_wait(); fs_preempt_enter(); } /* sys_fs_enter() for an ELF load: a RAM-backed path (the usual * /ramfs/bin/) neither waits for a rescan nor for a format, so the WM * and terminals can launch commands while `storage` probes the disks. */ void sys_fs_enter_path(const char *path) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; if (!(path && fs_resolve(path, &m, rel, sizeof(rel)) == 0 && vfs_mount_is_ram(m))) { fs_format_wait(); fs_scan_wait(); } fs_preempt_enter(); } /* The `storage` rescan: wait for a format or another scan, but do NOT * disable preemption. The scan publishes vfs_scan_busy (disk file * operations wait for it) and its probes sleep through vfs_scan_relax(), * so the rest of the system keeps running while the disks are probed. */ void sys_fs_scan(void) { fs_format_wait(); fs_scan_wait(); __asm__ volatile ("sti"); vfs_mount_all(); __asm__ volatile ("cli"); } void sys_fs_exit(void) { __asm__ volatile ("cli"); preempt_enable(); } static int sys_err(int vfs_err) { switch (vfs_err) { case VFS_EIO: return SYS_EIO; case VFS_ENOENT: return SYS_ENOENT; case VFS_EROFS: return SYS_EROFS; case VFS_ENOSPC: return SYS_ENOSPC; default: return SYS_EINVAL; } } /* Resolve an absolute path to (mount, relative path); returns 0 or a SYS_E* * code (mirrors the old "no such device" vs "no filesystem" distinction). */ static int fs_resolve(const char *path, struct vfs_mount **m, char *rel, unsigned int rel_size) { char abs[VFS_PATH_MAX * 2]; if (!path) return SYS_ENODEV; /* Relative paths resolve against the calling process's cwd (inherited * from the terminal that spawned it); "" means the cwd itself. */ if (path[0] != '/') { if (vfs_join_path(current_proc()->cwd, path, abs, sizeof(abs)) != 0) return SYS_EINVAL; path = abs; } if (path[0] == '/' && path[1] == '\0') path = "/"; /* the root listing goes through vfs_resolve below */ if (vfs_resolve(path, m, rel, rel_size) != 0) return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV; return 0; } /* Resolve and enter the non-preemptible section for one path operation. * Disk-backed mounts wait for a running format; the RAM mounts do not. */ static int sys_fs_begin(const char *path, struct vfs_mount **m, char *rel, unsigned int rel_size) { int r; /* /ramfs and /share are re-added first by a rescan and stay valid * while it sleeps: only disk paths (and paths that do not resolve * yet, which may be a disk being re-mounted) wait for the scan. */ r = fs_resolve(path, m, rel, rel_size); if (vfs_scan_busy && (r != 0 || !vfs_mount_is_ram(*m))) { fs_scan_wait(); r = fs_resolve(path, m, rel, rel_size); } if (r != 0) return r; if (vfs_mount_is_disk(*m)) { fs_format_wait(); /* The mount table (or the mount slot) may have been rebuilt * while sleeping; resolve again before using it. */ r = fs_resolve(path, m, rel, rel_size); if (r != 0) return r; } fs_preempt_enter(); return 0; } int sys_read_file(const char *path, void *buf, unsigned int max) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; int r = sys_fs_begin(path, &m, rel, sizeof(rel)); if (r != 0) return r; r = m->ops->read_file(m, rel, buf, max); if (r < 0) r = sys_err(r); sys_fs_exit(); return r; } int sys_write_file(const char *path, const void *buf, unsigned int len) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; int r = sys_fs_begin(path, &m, rel, sizeof(rel)); if (r != 0) return r; r = m->ops->write_file(m, rel, buf, len); if (r < 0) r = sys_err(r); sys_fs_exit(); return r; } int sys_unlink(const char *path) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; int r = sys_fs_begin(path, &m, rel, sizeof(rel)); if (r != 0) return r; r = m->ops->unlink(m, rel); if (r < 0) r = sys_err(r); sys_fs_exit(); return r; } int sys_mkdir(const char *path) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; int r = sys_fs_begin(path, &m, rel, sizeof(rel)); if (r != 0) return r; r = m->ops->mkdir(m, rel); if (r < 0) r = sys_err(r); sys_fs_exit(); return r; } int sys_rmdir(const char *path) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; int r = sys_fs_begin(path, &m, rel, sizeof(rel)); if (r != 0) return r; r = m->ops->rmdir(m, rel); if (r < 0) r = sys_err(r); sys_fs_exit(); return r; } int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; struct vfs_dirent d; unsigned int i; int r = sys_fs_begin(path, &m, rel, sizeof(rel)); if (r != 0) return r; r = m->ops->list_dir(m, rel, index, &d); if (r == 1) { for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++) out->name[i] = d.name[i]; out->name[i] = '\0'; out->size = d.size; out->is_dir = d.is_dir; out->raw_83 = d.raw_83; r = 1; } else if (r < 0) { r = sys_err(r); } else { r = 0; } sys_fs_exit(); return r; } static void copy_bounded(char *dst, const char *src, unsigned int size) { unsigned int i = 0; while (i + 1 < size && src[i]) { dst[i] = src[i]; i++; } dst[i] = '\0'; } int sys_list_mounts(unsigned int index, struct sys_mount *out) { if (index >= vfs_mount_count) return 0; copy_bounded(out->name, vfs_mounts[index].name, sizeof(out->name)); copy_bounded(out->type, vfs_mounts[index].type, sizeof(out->type)); out->total_sectors = vfs_mounts[index].total_sectors; copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label)); out->ro = (unsigned int)vfs_mounts[index].ro; return 1; } /* Stat a path. Unlike read_file this never touches the disk beyond the FAT * directory walk, so the WebDAV server can probe a collection cheaply. */ int sys_stat_path(const char *path, struct sys_stat *out) { struct vfs_mount *m; char rel[VFS_PATH_MAX]; unsigned int size = 0, ro = 0; int is_dir = 0, r; if (!out) return SYS_EINVAL; r = sys_fs_begin(path, &m, rel, sizeof(rel)); if (r != 0) return r; r = m->ops->stat(m, rel, &size, &is_dir); if (r < 0) { r = sys_err(r); } else { ro = (unsigned int)m->ro; out->size = size; out->is_dir = (unsigned int)is_dir; out->ro = ro; r = 0; } sys_fs_exit(); return r; }