sys_fs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* File service for ring-3 commands (see syscall.h for the ABI).
  2. *
  3. * Filesystem-agnostic: paths resolve through the VFS (vfs.c), so FAT volumes
  4. * and the ramfs behave the same. Two properties shape how these run:
  5. * - the FAT driver keeps state in static buffers, so no other process may
  6. * run its code concurrently;
  7. * - the floppy driver waits for IRQ6, so interrupts must stay enabled.
  8. * So instead of a lock we disable preemption for the duration: the scheduler
  9. * will not switch away from the running process while the count is set.
  10. * That keeps each operation atomic (and immune to Ctrl+C killing the process
  11. * mid-write) at the cost of a brief stall during disk I/O. Buffers point
  12. * into the caller's address space, which is also the active one during the
  13. * syscall, so no copy in/out is needed.
  14. *
  15. * A destructive `format` runs as a kernel thread without the BKL and sets
  16. * format_busy while it rewrites a disk. Disk-backed mounts wait for it;
  17. * the RAM-backed mounts (/ramfs, /share) do not, so the WM's periodic
  18. * /share stats write and the terminal keep running during a format. */
  19. #include "syscall.h"
  20. #include "fatformat.h"
  21. #include "process.h"
  22. #include "vfs.h"
  23. /* Wait for a destructive format that owns the disks while its FAT is
  24. * half-written. Sleeping drops the BKL so the formatter can finish. */
  25. static void fs_format_wait(void)
  26. {
  27. while (format_busy && current_proc()->pid != format_owner_pid) {
  28. if (!process_is_alive(format_owner_pid)) {
  29. format_busy = 0;
  30. break;
  31. }
  32. process_sleep_ms(10);
  33. }
  34. }
  35. /* A boot-time `storage` scan (or a concurrent storage command) is rebuilding
  36. * the mount table and reading through the FAT static buffers. Wait for it
  37. * instead of racing. */
  38. static void fs_scan_wait(void)
  39. {
  40. while (vfs_scan_busy && current_proc()->pid != vfs_scan_owner_pid) {
  41. if (vfs_scan_owner_pid && !process_is_alive(vfs_scan_owner_pid)) {
  42. vfs_scan_busy = 0;
  43. break;
  44. }
  45. process_sleep_ms(10);
  46. }
  47. }
  48. static int fs_resolve(const char *path, struct vfs_mount **m,
  49. char *rel, unsigned int rel_size);
  50. /* The disk may need its IRQ (floppy), and the FAT driver's static state must
  51. * not be preempted in the middle of an operation. */
  52. static void fs_preempt_enter(void)
  53. {
  54. preempt_disable();
  55. __asm__ volatile ("sti");
  56. }
  57. void sys_fs_enter(void)
  58. {
  59. /* Used by disk-touching services (the storage scan, ELF loads): wait
  60. * for a format and a mount-table rebuild, then take the non-
  61. * preemptible section. */
  62. fs_format_wait();
  63. fs_scan_wait();
  64. fs_preempt_enter();
  65. }
  66. /* sys_fs_enter() for an ELF load: a RAM-backed path (the usual
  67. * /ramfs/bin/<cmd>) neither waits for a rescan nor for a format, so the WM
  68. * and terminals can launch commands while `storage` probes the disks. */
  69. void sys_fs_enter_path(const char *path)
  70. {
  71. struct vfs_mount *m;
  72. char rel[VFS_PATH_MAX];
  73. if (!(path && fs_resolve(path, &m, rel, sizeof(rel)) == 0 &&
  74. vfs_mount_is_ram(m))) {
  75. fs_format_wait();
  76. fs_scan_wait();
  77. }
  78. fs_preempt_enter();
  79. }
  80. /* The `storage` rescan: wait for a format or another scan, but do NOT
  81. * disable preemption. The scan publishes vfs_scan_busy (disk file
  82. * operations wait for it) and its probes sleep through vfs_scan_relax(),
  83. * so the rest of the system keeps running while the disks are probed. */
  84. void sys_fs_scan(void)
  85. {
  86. fs_format_wait();
  87. fs_scan_wait();
  88. __asm__ volatile ("sti");
  89. vfs_mount_all();
  90. __asm__ volatile ("cli");
  91. }
  92. void sys_fs_exit(void)
  93. {
  94. __asm__ volatile ("cli");
  95. preempt_enable();
  96. }
  97. static int sys_err(int vfs_err)
  98. {
  99. switch (vfs_err) {
  100. case VFS_EIO: return SYS_EIO;
  101. case VFS_ENOENT: return SYS_ENOENT;
  102. case VFS_EROFS: return SYS_EROFS;
  103. case VFS_ENOSPC: return SYS_ENOSPC;
  104. default: return SYS_EINVAL;
  105. }
  106. }
  107. /* Resolve an absolute path to (mount, relative path); returns 0 or a SYS_E*
  108. * code (mirrors the old "no such device" vs "no filesystem" distinction). */
  109. static int fs_resolve(const char *path, struct vfs_mount **m,
  110. char *rel, unsigned int rel_size)
  111. {
  112. char abs[VFS_PATH_MAX * 2];
  113. if (!path)
  114. return SYS_ENODEV;
  115. /* Relative paths resolve against the calling process's cwd (inherited
  116. * from the terminal that spawned it); "" means the cwd itself. */
  117. if (path[0] != '/') {
  118. if (vfs_join_path(current_proc()->cwd, path, abs, sizeof(abs)) != 0)
  119. return SYS_EINVAL;
  120. path = abs;
  121. }
  122. if (path[0] == '/' && path[1] == '\0')
  123. path = "/"; /* the root listing goes through vfs_resolve below */
  124. if (vfs_resolve(path, m, rel, rel_size) != 0)
  125. return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
  126. return 0;
  127. }
  128. /* Resolve and enter the non-preemptible section for one path operation.
  129. * Disk-backed mounts wait for a running format; the RAM mounts do not. */
  130. static int sys_fs_begin(const char *path, struct vfs_mount **m,
  131. char *rel, unsigned int rel_size)
  132. {
  133. int r;
  134. /* /ramfs and /share are re-added first by a rescan and stay valid
  135. * while it sleeps: only disk paths (and paths that do not resolve
  136. * yet, which may be a disk being re-mounted) wait for the scan. */
  137. r = fs_resolve(path, m, rel, rel_size);
  138. if (vfs_scan_busy && (r != 0 || !vfs_mount_is_ram(*m))) {
  139. fs_scan_wait();
  140. r = fs_resolve(path, m, rel, rel_size);
  141. }
  142. if (r != 0)
  143. return r;
  144. if (vfs_mount_is_disk(*m)) {
  145. fs_format_wait();
  146. /* The mount table (or the mount slot) may have been rebuilt
  147. * while sleeping; resolve again before using it. */
  148. r = fs_resolve(path, m, rel, rel_size);
  149. if (r != 0)
  150. return r;
  151. }
  152. fs_preempt_enter();
  153. return 0;
  154. }
  155. int sys_read_file(const char *path, void *buf, unsigned int max)
  156. {
  157. struct vfs_mount *m;
  158. char rel[VFS_PATH_MAX];
  159. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  160. if (r != 0)
  161. return r;
  162. r = m->ops->read_file(m, rel, buf, max);
  163. if (r < 0)
  164. r = sys_err(r);
  165. sys_fs_exit();
  166. return r;
  167. }
  168. int sys_write_file(const char *path, const void *buf, unsigned int len)
  169. {
  170. struct vfs_mount *m;
  171. char rel[VFS_PATH_MAX];
  172. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  173. if (r != 0)
  174. return r;
  175. r = m->ops->write_file(m, rel, buf, len);
  176. if (r < 0)
  177. r = sys_err(r);
  178. sys_fs_exit();
  179. return r;
  180. }
  181. int sys_unlink(const char *path)
  182. {
  183. struct vfs_mount *m;
  184. char rel[VFS_PATH_MAX];
  185. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  186. if (r != 0)
  187. return r;
  188. r = m->ops->unlink(m, rel);
  189. if (r < 0)
  190. r = sys_err(r);
  191. sys_fs_exit();
  192. return r;
  193. }
  194. int sys_mkdir(const char *path)
  195. {
  196. struct vfs_mount *m;
  197. char rel[VFS_PATH_MAX];
  198. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  199. if (r != 0)
  200. return r;
  201. r = m->ops->mkdir(m, rel);
  202. if (r < 0)
  203. r = sys_err(r);
  204. sys_fs_exit();
  205. return r;
  206. }
  207. int sys_rmdir(const char *path)
  208. {
  209. struct vfs_mount *m;
  210. char rel[VFS_PATH_MAX];
  211. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  212. if (r != 0)
  213. return r;
  214. r = m->ops->rmdir(m, rel);
  215. if (r < 0)
  216. r = sys_err(r);
  217. sys_fs_exit();
  218. return r;
  219. }
  220. int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
  221. {
  222. struct vfs_mount *m;
  223. char rel[VFS_PATH_MAX];
  224. struct vfs_dirent d;
  225. unsigned int i;
  226. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  227. if (r != 0)
  228. return r;
  229. r = m->ops->list_dir(m, rel, index, &d);
  230. if (r == 1) {
  231. for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++)
  232. out->name[i] = d.name[i];
  233. out->name[i] = '\0';
  234. out->size = d.size;
  235. out->is_dir = d.is_dir;
  236. out->raw_83 = d.raw_83;
  237. r = 1;
  238. } else if (r < 0) {
  239. r = sys_err(r);
  240. } else {
  241. r = 0;
  242. }
  243. sys_fs_exit();
  244. return r;
  245. }
  246. static void copy_bounded(char *dst, const char *src, unsigned int size)
  247. {
  248. unsigned int i = 0;
  249. while (i + 1 < size && src[i]) {
  250. dst[i] = src[i];
  251. i++;
  252. }
  253. dst[i] = '\0';
  254. }
  255. int sys_list_mounts(unsigned int index, struct sys_mount *out)
  256. {
  257. if (index >= vfs_mount_count)
  258. return 0;
  259. copy_bounded(out->name, vfs_mounts[index].name, sizeof(out->name));
  260. copy_bounded(out->type, vfs_mounts[index].type, sizeof(out->type));
  261. out->total_sectors = vfs_mounts[index].total_sectors;
  262. copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label));
  263. out->ro = (unsigned int)vfs_mounts[index].ro;
  264. return 1;
  265. }
  266. /* Stat a path. Unlike read_file this never touches the disk beyond the FAT
  267. * directory walk, so the WebDAV server can probe a collection cheaply. */
  268. int sys_stat_path(const char *path, struct sys_stat *out)
  269. {
  270. struct vfs_mount *m;
  271. char rel[VFS_PATH_MAX];
  272. unsigned int size = 0, ro = 0;
  273. int is_dir = 0, r;
  274. if (!out)
  275. return SYS_EINVAL;
  276. r = sys_fs_begin(path, &m, rel, sizeof(rel));
  277. if (r != 0)
  278. return r;
  279. r = m->ops->stat(m, rel, &size, &is_dir);
  280. if (r < 0) {
  281. r = sys_err(r);
  282. } else {
  283. ro = (unsigned int)m->ro;
  284. out->size = size;
  285. out->is_dir = (unsigned int)is_dir;
  286. out->ro = ro;
  287. r = 0;
  288. }
  289. sys_fs_exit();
  290. return r;
  291. }