sys_fs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /* The disk may need its IRQ (floppy), and the FAT driver's static state must
  49. * not be preempted in the middle of an operation. */
  50. static void fs_preempt_enter(void)
  51. {
  52. preempt_disable();
  53. __asm__ volatile ("sti");
  54. }
  55. void sys_fs_enter(void)
  56. {
  57. /* Used by disk-touching services (the storage scan, ELF loads): wait
  58. * for a format and a mount-table rebuild, then take the non-
  59. * preemptible section. */
  60. fs_format_wait();
  61. fs_scan_wait();
  62. fs_preempt_enter();
  63. }
  64. void sys_fs_exit(void)
  65. {
  66. __asm__ volatile ("cli");
  67. preempt_enable();
  68. }
  69. static int sys_err(int vfs_err)
  70. {
  71. switch (vfs_err) {
  72. case VFS_EIO: return SYS_EIO;
  73. case VFS_ENOENT: return SYS_ENOENT;
  74. case VFS_EROFS: return SYS_EROFS;
  75. case VFS_ENOSPC: return SYS_ENOSPC;
  76. default: return SYS_EINVAL;
  77. }
  78. }
  79. /* Resolve an absolute path to (mount, relative path); returns 0 or a SYS_E*
  80. * code (mirrors the old "no such device" vs "no filesystem" distinction). */
  81. static int fs_resolve(const char *path, struct vfs_mount **m,
  82. char *rel, unsigned int rel_size)
  83. {
  84. char abs[VFS_PATH_MAX * 2];
  85. if (!path)
  86. return SYS_ENODEV;
  87. /* Relative paths resolve against the calling process's cwd (inherited
  88. * from the terminal that spawned it); "" means the cwd itself. */
  89. if (path[0] != '/') {
  90. if (vfs_join_path(current_proc()->cwd, path, abs, sizeof(abs)) != 0)
  91. return SYS_EINVAL;
  92. path = abs;
  93. }
  94. if (path[0] == '/' && path[1] == '\0')
  95. path = "/"; /* the root listing goes through vfs_resolve below */
  96. if (vfs_resolve(path, m, rel, rel_size) != 0)
  97. return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
  98. return 0;
  99. }
  100. /* Resolve and enter the non-preemptible section for one path operation.
  101. * Disk-backed mounts wait for a running format; the RAM mounts do not. */
  102. static int sys_fs_begin(const char *path, struct vfs_mount **m,
  103. char *rel, unsigned int rel_size)
  104. {
  105. int r;
  106. fs_scan_wait();
  107. r = fs_resolve(path, m, rel, rel_size);
  108. if (r != 0)
  109. return r;
  110. if (vfs_mount_is_disk(*m)) {
  111. fs_format_wait();
  112. /* The mount table (or the mount slot) may have been rebuilt
  113. * while sleeping; resolve again before using it. */
  114. r = fs_resolve(path, m, rel, rel_size);
  115. if (r != 0)
  116. return r;
  117. }
  118. fs_preempt_enter();
  119. return 0;
  120. }
  121. int sys_read_file(const char *path, void *buf, unsigned int max)
  122. {
  123. struct vfs_mount *m;
  124. char rel[VFS_PATH_MAX];
  125. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  126. if (r != 0)
  127. return r;
  128. r = m->ops->read_file(m, rel, buf, max);
  129. if (r < 0)
  130. r = sys_err(r);
  131. sys_fs_exit();
  132. return r;
  133. }
  134. int sys_write_file(const char *path, const void *buf, unsigned int len)
  135. {
  136. struct vfs_mount *m;
  137. char rel[VFS_PATH_MAX];
  138. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  139. if (r != 0)
  140. return r;
  141. r = m->ops->write_file(m, rel, buf, len);
  142. if (r < 0)
  143. r = sys_err(r);
  144. sys_fs_exit();
  145. return r;
  146. }
  147. int sys_unlink(const char *path)
  148. {
  149. struct vfs_mount *m;
  150. char rel[VFS_PATH_MAX];
  151. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  152. if (r != 0)
  153. return r;
  154. r = m->ops->unlink(m, rel);
  155. if (r < 0)
  156. r = sys_err(r);
  157. sys_fs_exit();
  158. return r;
  159. }
  160. int sys_mkdir(const char *path)
  161. {
  162. struct vfs_mount *m;
  163. char rel[VFS_PATH_MAX];
  164. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  165. if (r != 0)
  166. return r;
  167. r = m->ops->mkdir(m, rel);
  168. if (r < 0)
  169. r = sys_err(r);
  170. sys_fs_exit();
  171. return r;
  172. }
  173. int sys_rmdir(const char *path)
  174. {
  175. struct vfs_mount *m;
  176. char rel[VFS_PATH_MAX];
  177. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  178. if (r != 0)
  179. return r;
  180. r = m->ops->rmdir(m, rel);
  181. if (r < 0)
  182. r = sys_err(r);
  183. sys_fs_exit();
  184. return r;
  185. }
  186. int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
  187. {
  188. struct vfs_mount *m;
  189. char rel[VFS_PATH_MAX];
  190. struct vfs_dirent d;
  191. unsigned int i;
  192. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  193. if (r != 0)
  194. return r;
  195. r = m->ops->list_dir(m, rel, index, &d);
  196. if (r == 1) {
  197. for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++)
  198. out->name[i] = d.name[i];
  199. out->name[i] = '\0';
  200. out->size = d.size;
  201. out->is_dir = d.is_dir;
  202. out->raw_83 = d.raw_83;
  203. r = 1;
  204. } else if (r < 0) {
  205. r = sys_err(r);
  206. } else {
  207. r = 0;
  208. }
  209. sys_fs_exit();
  210. return r;
  211. }
  212. static void copy_bounded(char *dst, const char *src, unsigned int size)
  213. {
  214. unsigned int i = 0;
  215. while (i + 1 < size && src[i]) {
  216. dst[i] = src[i];
  217. i++;
  218. }
  219. dst[i] = '\0';
  220. }
  221. int sys_list_mounts(unsigned int index, struct sys_mount *out)
  222. {
  223. if (index >= vfs_mount_count)
  224. return 0;
  225. copy_bounded(out->name, vfs_mounts[index].name, sizeof(out->name));
  226. copy_bounded(out->type, vfs_mounts[index].type, sizeof(out->type));
  227. out->total_sectors = vfs_mounts[index].total_sectors;
  228. copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label));
  229. out->ro = (unsigned int)vfs_mounts[index].ro;
  230. return 1;
  231. }
  232. /* Stat a path. Unlike read_file this never touches the disk beyond the FAT
  233. * directory walk, so the WebDAV server can probe a collection cheaply. */
  234. int sys_stat_path(const char *path, struct sys_stat *out)
  235. {
  236. struct vfs_mount *m;
  237. char rel[VFS_PATH_MAX];
  238. unsigned int size = 0, ro = 0;
  239. int is_dir = 0, r;
  240. if (!out)
  241. return SYS_EINVAL;
  242. r = sys_fs_begin(path, &m, rel, sizeof(rel));
  243. if (r != 0)
  244. return r;
  245. r = m->ops->stat(m, rel, &size, &is_dir);
  246. if (r < 0) {
  247. r = sys_err(r);
  248. } else {
  249. ro = (unsigned int)m->ro;
  250. out->size = size;
  251. out->is_dir = (unsigned int)is_dir;
  252. out->ro = ro;
  253. r = 0;
  254. }
  255. sys_fs_exit();
  256. return r;
  257. }