sys_fs.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include "syscall.h"
  15. #include "fatformat.h"
  16. #include "process.h"
  17. #include "vfs.h"
  18. void sys_fs_enter(void)
  19. {
  20. /* A format owns the disks while its FAT is half-written. Do not let a
  21. * file syscall read that inode/FAT state; sleeping drops the BKL so the
  22. * formatter can finish. */
  23. while (format_busy && current_proc()->pid != format_owner_pid) {
  24. if (!process_is_alive(format_owner_pid)) {
  25. format_busy = 0;
  26. break;
  27. }
  28. process_sleep_ms(10);
  29. }
  30. /* A boot-time `storage` scan (or a concurrent storage command) is
  31. * rebuilding the mount table and reading through the FAT static
  32. * buffers. Wait for it instead of racing. */
  33. while (vfs_scan_busy && current_proc()->pid != vfs_scan_owner_pid) {
  34. if (vfs_scan_owner_pid && !process_is_alive(vfs_scan_owner_pid)) {
  35. vfs_scan_busy = 0;
  36. break;
  37. }
  38. process_sleep_ms(10);
  39. }
  40. preempt_disable();
  41. __asm__ volatile ("sti"); /* the disk may need its IRQ */
  42. }
  43. void sys_fs_exit(void)
  44. {
  45. __asm__ volatile ("cli");
  46. preempt_enable();
  47. }
  48. static int sys_err(int vfs_err)
  49. {
  50. switch (vfs_err) {
  51. case VFS_EIO: return SYS_EIO;
  52. case VFS_ENOENT: return SYS_ENOENT;
  53. case VFS_EROFS: return SYS_EROFS;
  54. case VFS_ENOSPC: return SYS_ENOSPC;
  55. default: return SYS_EINVAL;
  56. }
  57. }
  58. /* Resolve an absolute path to (mount, relative path); returns 0 or a SYS_E*
  59. * code (mirrors the old "no such device" vs "no filesystem" distinction). */
  60. static int fs_resolve(const char *path, struct vfs_mount **m,
  61. char *rel, unsigned int rel_size)
  62. {
  63. char abs[VFS_PATH_MAX * 2];
  64. if (!path)
  65. return SYS_ENODEV;
  66. /* Relative paths resolve against the calling process's cwd (inherited
  67. * from the terminal that spawned it); "" means the cwd itself. */
  68. if (path[0] != '/') {
  69. if (vfs_join_path(current_proc()->cwd, path, abs, sizeof(abs)) != 0)
  70. return SYS_EINVAL;
  71. path = abs;
  72. }
  73. if (path[0] == '/' && path[1] == '\0')
  74. path = "/"; /* the root listing goes through vfs_resolve below */
  75. if (vfs_resolve(path, m, rel, rel_size) != 0)
  76. return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
  77. return 0;
  78. }
  79. int sys_read_file(const char *path, void *buf, unsigned int max)
  80. {
  81. struct vfs_mount *m;
  82. char rel[VFS_PATH_MAX];
  83. int r;
  84. sys_fs_enter();
  85. r = fs_resolve(path, &m, rel, sizeof(rel));
  86. if (r == 0) {
  87. r = m->ops->read_file(m, rel, buf, max);
  88. if (r < 0)
  89. r = sys_err(r);
  90. }
  91. sys_fs_exit();
  92. return r;
  93. }
  94. int sys_write_file(const char *path, const void *buf, unsigned int len)
  95. {
  96. struct vfs_mount *m;
  97. char rel[VFS_PATH_MAX];
  98. int r;
  99. sys_fs_enter();
  100. r = fs_resolve(path, &m, rel, sizeof(rel));
  101. if (r == 0) {
  102. r = m->ops->write_file(m, rel, buf, len);
  103. if (r < 0)
  104. r = sys_err(r);
  105. }
  106. sys_fs_exit();
  107. return r;
  108. }
  109. int sys_unlink(const char *path)
  110. {
  111. struct vfs_mount *m;
  112. char rel[VFS_PATH_MAX];
  113. int r;
  114. sys_fs_enter();
  115. r = fs_resolve(path, &m, rel, sizeof(rel));
  116. if (r == 0) {
  117. r = m->ops->unlink(m, rel);
  118. if (r < 0)
  119. r = sys_err(r);
  120. }
  121. sys_fs_exit();
  122. return r;
  123. }
  124. int sys_mkdir(const char *path)
  125. {
  126. struct vfs_mount *m;
  127. char rel[VFS_PATH_MAX];
  128. int r;
  129. sys_fs_enter();
  130. r = fs_resolve(path, &m, rel, sizeof(rel));
  131. if (r == 0) {
  132. r = m->ops->mkdir(m, rel);
  133. if (r < 0)
  134. r = sys_err(r);
  135. }
  136. sys_fs_exit();
  137. return r;
  138. }
  139. int sys_rmdir(const char *path)
  140. {
  141. struct vfs_mount *m;
  142. char rel[VFS_PATH_MAX];
  143. int r;
  144. sys_fs_enter();
  145. r = fs_resolve(path, &m, rel, sizeof(rel));
  146. if (r == 0) {
  147. r = m->ops->rmdir(m, rel);
  148. if (r < 0)
  149. r = sys_err(r);
  150. }
  151. sys_fs_exit();
  152. return r;
  153. }
  154. int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
  155. {
  156. struct vfs_mount *m;
  157. char rel[VFS_PATH_MAX];
  158. int r;
  159. sys_fs_enter();
  160. r = fs_resolve(path, &m, rel, sizeof(rel));
  161. if (r == 0) {
  162. struct vfs_dirent d;
  163. unsigned int i;
  164. r = m->ops->list_dir(m, rel, index, &d);
  165. if (r == 1) {
  166. for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++)
  167. out->name[i] = d.name[i];
  168. out->name[i] = '\0';
  169. out->size = d.size;
  170. out->is_dir = d.is_dir;
  171. out->raw_83 = d.raw_83;
  172. r = 1;
  173. } else if (r < 0) {
  174. r = sys_err(r);
  175. } else {
  176. r = 0;
  177. }
  178. }
  179. sys_fs_exit();
  180. return r;
  181. }
  182. static void copy_bounded(char *dst, const char *src, unsigned int size)
  183. {
  184. unsigned int i = 0;
  185. while (i + 1 < size && src[i]) {
  186. dst[i] = src[i];
  187. i++;
  188. }
  189. dst[i] = '\0';
  190. }
  191. int sys_list_mounts(unsigned int index, struct sys_mount *out)
  192. {
  193. if (index >= vfs_mount_count)
  194. return 0;
  195. copy_bounded(out->name, vfs_mounts[index].name, sizeof(out->name));
  196. copy_bounded(out->type, vfs_mounts[index].type, sizeof(out->type));
  197. out->total_sectors = vfs_mounts[index].total_sectors;
  198. copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label));
  199. out->ro = (unsigned int)vfs_mounts[index].ro;
  200. return 1;
  201. }
  202. /* Stat a path. Unlike read_file this never touches the disk beyond the FAT
  203. * directory walk, so the WebDAV server can probe a collection cheaply. */
  204. int sys_stat_path(const char *path, struct sys_stat *out)
  205. {
  206. struct vfs_mount *m;
  207. char rel[VFS_PATH_MAX];
  208. unsigned int size = 0, ro = 0;
  209. int is_dir = 0, r;
  210. if (!out)
  211. return SYS_EINVAL;
  212. size = 0;
  213. is_dir = 0;
  214. ro = 0;
  215. sys_fs_enter();
  216. r = fs_resolve(path, &m, rel, sizeof(rel));
  217. if (r == 0) {
  218. r = m->ops->stat(m, rel, &size, &is_dir);
  219. if (r < 0) {
  220. r = sys_err(r);
  221. } else {
  222. ro = (unsigned int)m->ro;
  223. out->size = size;
  224. out->is_dir = (unsigned int)is_dir;
  225. out->ro = ro;
  226. r = 0;
  227. }
  228. }
  229. sys_fs_exit();
  230. return r;
  231. }