| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /* 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);
- }
- }
- /* 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();
- }
- 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;
- 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;
- }
|