|
|
@@ -10,18 +10,22 @@
|
|
|
* 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. */
|
|
|
+ * 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"
|
|
|
|
|
|
-void sys_fs_enter(void)
|
|
|
+/* 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)
|
|
|
{
|
|
|
- /* A format owns the disks while its FAT is half-written. Do not let a
|
|
|
- * file syscall read that inode/FAT state; sleeping drops the BKL so the
|
|
|
- * formatter can finish. */
|
|
|
while (format_busy && current_proc()->pid != format_owner_pid) {
|
|
|
if (!process_is_alive(format_owner_pid)) {
|
|
|
format_busy = 0;
|
|
|
@@ -29,9 +33,13 @@ void sys_fs_enter(void)
|
|
|
}
|
|
|
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. */
|
|
|
+}
|
|
|
+
|
|
|
+/* 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;
|
|
|
@@ -39,8 +47,24 @@ void sys_fs_enter(void)
|
|
|
}
|
|
|
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"); /* the disk may need its IRQ */
|
|
|
+ __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)
|
|
|
@@ -84,19 +108,40 @@ static int fs_resolve(const char *path, struct vfs_mount **m,
|
|
|
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;
|
|
|
+ int r = sys_fs_begin(path, &m, rel, sizeof(rel));
|
|
|
|
|
|
- sys_fs_enter();
|
|
|
- r = fs_resolve(path, &m, rel, sizeof(rel));
|
|
|
- if (r == 0) {
|
|
|
- r = m->ops->read_file(m, rel, buf, max);
|
|
|
- if (r < 0)
|
|
|
- r = sys_err(r);
|
|
|
- }
|
|
|
+ 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;
|
|
|
}
|
|
|
@@ -105,15 +150,13 @@ int sys_write_file(const char *path, const void *buf, unsigned int len)
|
|
|
{
|
|
|
struct vfs_mount *m;
|
|
|
char rel[VFS_PATH_MAX];
|
|
|
- int r;
|
|
|
+ int r = sys_fs_begin(path, &m, rel, sizeof(rel));
|
|
|
|
|
|
- sys_fs_enter();
|
|
|
- r = fs_resolve(path, &m, rel, sizeof(rel));
|
|
|
- if (r == 0) {
|
|
|
- r = m->ops->write_file(m, rel, buf, len);
|
|
|
- if (r < 0)
|
|
|
- r = sys_err(r);
|
|
|
- }
|
|
|
+ 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;
|
|
|
}
|
|
|
@@ -122,15 +165,13 @@ int sys_unlink(const char *path)
|
|
|
{
|
|
|
struct vfs_mount *m;
|
|
|
char rel[VFS_PATH_MAX];
|
|
|
- int r;
|
|
|
+ int r = sys_fs_begin(path, &m, rel, sizeof(rel));
|
|
|
|
|
|
- sys_fs_enter();
|
|
|
- r = fs_resolve(path, &m, rel, sizeof(rel));
|
|
|
- if (r == 0) {
|
|
|
- r = m->ops->unlink(m, rel);
|
|
|
- if (r < 0)
|
|
|
- r = sys_err(r);
|
|
|
- }
|
|
|
+ if (r != 0)
|
|
|
+ return r;
|
|
|
+ r = m->ops->unlink(m, rel);
|
|
|
+ if (r < 0)
|
|
|
+ r = sys_err(r);
|
|
|
sys_fs_exit();
|
|
|
return r;
|
|
|
}
|
|
|
@@ -139,15 +180,13 @@ int sys_mkdir(const char *path)
|
|
|
{
|
|
|
struct vfs_mount *m;
|
|
|
char rel[VFS_PATH_MAX];
|
|
|
- int r;
|
|
|
+ int r = sys_fs_begin(path, &m, rel, sizeof(rel));
|
|
|
|
|
|
- sys_fs_enter();
|
|
|
- r = fs_resolve(path, &m, rel, sizeof(rel));
|
|
|
- if (r == 0) {
|
|
|
- r = m->ops->mkdir(m, rel);
|
|
|
- if (r < 0)
|
|
|
- r = sys_err(r);
|
|
|
- }
|
|
|
+ if (r != 0)
|
|
|
+ return r;
|
|
|
+ r = m->ops->mkdir(m, rel);
|
|
|
+ if (r < 0)
|
|
|
+ r = sys_err(r);
|
|
|
sys_fs_exit();
|
|
|
return r;
|
|
|
}
|
|
|
@@ -156,15 +195,13 @@ int sys_rmdir(const char *path)
|
|
|
{
|
|
|
struct vfs_mount *m;
|
|
|
char rel[VFS_PATH_MAX];
|
|
|
- int r;
|
|
|
+ int r = sys_fs_begin(path, &m, rel, sizeof(rel));
|
|
|
|
|
|
- sys_fs_enter();
|
|
|
- r = fs_resolve(path, &m, rel, sizeof(rel));
|
|
|
- if (r == 0) {
|
|
|
- r = m->ops->rmdir(m, rel);
|
|
|
- if (r < 0)
|
|
|
- r = sys_err(r);
|
|
|
- }
|
|
|
+ if (r != 0)
|
|
|
+ return r;
|
|
|
+ r = m->ops->rmdir(m, rel);
|
|
|
+ if (r < 0)
|
|
|
+ r = sys_err(r);
|
|
|
sys_fs_exit();
|
|
|
return r;
|
|
|
}
|
|
|
@@ -173,28 +210,25 @@ int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
|
|
|
{
|
|
|
struct vfs_mount *m;
|
|
|
char rel[VFS_PATH_MAX];
|
|
|
- int r;
|
|
|
-
|
|
|
- sys_fs_enter();
|
|
|
- r = fs_resolve(path, &m, rel, sizeof(rel));
|
|
|
- if (r == 0) {
|
|
|
- struct vfs_dirent d;
|
|
|
- unsigned int i;
|
|
|
+ struct vfs_dirent d;
|
|
|
+ unsigned int i;
|
|
|
+ int r = sys_fs_begin(path, &m, rel, sizeof(rel));
|
|
|
|
|
|
- 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;
|
|
|
- }
|
|
|
+ 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;
|
|
|
@@ -234,22 +268,18 @@ int sys_stat_path(const char *path, struct sys_stat *out)
|
|
|
|
|
|
if (!out)
|
|
|
return SYS_EINVAL;
|
|
|
- size = 0;
|
|
|
- is_dir = 0;
|
|
|
- ro = 0;
|
|
|
- sys_fs_enter();
|
|
|
- r = fs_resolve(path, &m, rel, sizeof(rel));
|
|
|
- if (r == 0) {
|
|
|
- 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;
|
|
|
- }
|
|
|
+ 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;
|