sys_fs.c 5.7 KB

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