sys_fs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 set `sys_fs_busy` for the duration: the scheduler
  9. * (process.c) will not switch away from the running process while it 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 "process.h"
  16. #include "vfs.h"
  17. volatile int sys_fs_busy;
  18. void sys_fs_enter(void)
  19. {
  20. sys_fs_busy = 1;
  21. __asm__ volatile ("sti"); /* the disk may need its IRQ */
  22. }
  23. void sys_fs_exit(void)
  24. {
  25. __asm__ volatile ("cli");
  26. sys_fs_busy = 0;
  27. }
  28. static int sys_err(int vfs_err)
  29. {
  30. switch (vfs_err) {
  31. case VFS_EIO: return SYS_EIO;
  32. case VFS_ENOENT: return SYS_ENOENT;
  33. case VFS_EROFS: return SYS_EROFS;
  34. default: return SYS_EINVAL;
  35. }
  36. }
  37. /* Resolve an absolute path to (mount, relative path); returns 0 or a SYS_E*
  38. * code (mirrors the old "no such device" vs "no filesystem" distinction). */
  39. static int fs_resolve(const char *path, struct vfs_mount **m,
  40. char *rel, unsigned int rel_size)
  41. {
  42. char abs[VFS_PATH_MAX * 2];
  43. if (!path)
  44. return SYS_ENODEV;
  45. /* Relative paths resolve against the calling process's cwd (inherited
  46. * from the terminal that spawned it); "" means the cwd itself. */
  47. if (path[0] != '/') {
  48. if (vfs_join_path(procs[current_pid].cwd, path, abs, sizeof(abs)) != 0)
  49. return SYS_EINVAL;
  50. path = abs;
  51. }
  52. if (path[0] == '/' && path[1] == '\0')
  53. path = "/"; /* the root listing goes through vfs_resolve below */
  54. if (vfs_resolve(path, m, rel, rel_size) != 0)
  55. return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
  56. return 0;
  57. }
  58. int sys_read_file(const char *path, void *buf, unsigned int max)
  59. {
  60. struct vfs_mount *m;
  61. char rel[VFS_PATH_MAX];
  62. int r;
  63. sys_fs_enter();
  64. r = fs_resolve(path, &m, rel, sizeof(rel));
  65. if (r == 0) {
  66. r = m->ops->read_file(m, rel, buf, max);
  67. if (r < 0)
  68. r = sys_err(r);
  69. }
  70. sys_fs_exit();
  71. return r;
  72. }
  73. int sys_write_file(const char *path, const void *buf, unsigned int len)
  74. {
  75. struct vfs_mount *m;
  76. char rel[VFS_PATH_MAX];
  77. int r;
  78. sys_fs_enter();
  79. r = fs_resolve(path, &m, rel, sizeof(rel));
  80. if (r == 0) {
  81. r = m->ops->write_file(m, rel, buf, len);
  82. if (r < 0)
  83. r = sys_err(r);
  84. }
  85. sys_fs_exit();
  86. return r;
  87. }
  88. int sys_unlink(const char *path)
  89. {
  90. struct vfs_mount *m;
  91. char rel[VFS_PATH_MAX];
  92. int r;
  93. sys_fs_enter();
  94. r = fs_resolve(path, &m, rel, sizeof(rel));
  95. if (r == 0) {
  96. r = m->ops->unlink(m, rel);
  97. if (r < 0)
  98. r = sys_err(r);
  99. }
  100. sys_fs_exit();
  101. return r;
  102. }
  103. int sys_mkdir(const char *path)
  104. {
  105. struct vfs_mount *m;
  106. char rel[VFS_PATH_MAX];
  107. int r;
  108. sys_fs_enter();
  109. r = fs_resolve(path, &m, rel, sizeof(rel));
  110. if (r == 0) {
  111. r = m->ops->mkdir(m, rel);
  112. if (r < 0)
  113. r = sys_err(r);
  114. }
  115. sys_fs_exit();
  116. return r;
  117. }
  118. int sys_rmdir(const char *path)
  119. {
  120. struct vfs_mount *m;
  121. char rel[VFS_PATH_MAX];
  122. int r;
  123. sys_fs_enter();
  124. r = fs_resolve(path, &m, rel, sizeof(rel));
  125. if (r == 0) {
  126. r = m->ops->rmdir(m, rel);
  127. if (r < 0)
  128. r = sys_err(r);
  129. }
  130. sys_fs_exit();
  131. return r;
  132. }
  133. int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
  134. {
  135. struct vfs_mount *m;
  136. char rel[VFS_PATH_MAX];
  137. int r;
  138. sys_fs_enter();
  139. r = fs_resolve(path, &m, rel, sizeof(rel));
  140. if (r == 0) {
  141. struct vfs_dirent d;
  142. unsigned int i;
  143. r = m->ops->list_dir(m, rel, index, &d);
  144. if (r == 1) {
  145. for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++)
  146. out->name[i] = d.name[i];
  147. out->name[i] = '\0';
  148. out->size = d.size;
  149. out->is_dir = d.is_dir;
  150. r = 1;
  151. } else if (r < 0) {
  152. r = sys_err(r);
  153. } else {
  154. r = 0;
  155. }
  156. }
  157. sys_fs_exit();
  158. return r;
  159. }
  160. static void copy_bounded(char *dst, const char *src, unsigned int size)
  161. {
  162. unsigned int i = 0;
  163. while (i + 1 < size && src[i]) {
  164. dst[i] = src[i];
  165. i++;
  166. }
  167. dst[i] = '\0';
  168. }
  169. int sys_list_mounts(unsigned int index, struct sys_mount *out)
  170. {
  171. if (index >= vfs_mount_count)
  172. return 0;
  173. copy_bounded(out->name, vfs_mounts[index].name, sizeof(out->name));
  174. copy_bounded(out->type, vfs_mounts[index].type, sizeof(out->type));
  175. out->total_sectors = vfs_mounts[index].total_sectors;
  176. copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label));
  177. return 1;
  178. }