sys_fs.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. default: return SYS_EINVAL;
  45. }
  46. }
  47. /* Resolve an absolute path to (mount, relative path); returns 0 or a SYS_E*
  48. * code (mirrors the old "no such device" vs "no filesystem" distinction). */
  49. static int fs_resolve(const char *path, struct vfs_mount **m,
  50. char *rel, unsigned int rel_size)
  51. {
  52. char abs[VFS_PATH_MAX * 2];
  53. if (!path)
  54. return SYS_ENODEV;
  55. /* Relative paths resolve against the calling process's cwd (inherited
  56. * from the terminal that spawned it); "" means the cwd itself. */
  57. if (path[0] != '/') {
  58. if (vfs_join_path(current_proc()->cwd, path, abs, sizeof(abs)) != 0)
  59. return SYS_EINVAL;
  60. path = abs;
  61. }
  62. if (path[0] == '/' && path[1] == '\0')
  63. path = "/"; /* the root listing goes through vfs_resolve below */
  64. if (vfs_resolve(path, m, rel, rel_size) != 0)
  65. return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
  66. return 0;
  67. }
  68. int sys_read_file(const char *path, void *buf, unsigned int max)
  69. {
  70. struct vfs_mount *m;
  71. char rel[VFS_PATH_MAX];
  72. int r;
  73. sys_fs_enter();
  74. r = fs_resolve(path, &m, rel, sizeof(rel));
  75. if (r == 0) {
  76. r = m->ops->read_file(m, rel, buf, max);
  77. if (r < 0)
  78. r = sys_err(r);
  79. }
  80. sys_fs_exit();
  81. return r;
  82. }
  83. int sys_write_file(const char *path, const void *buf, unsigned int len)
  84. {
  85. struct vfs_mount *m;
  86. char rel[VFS_PATH_MAX];
  87. int r;
  88. sys_fs_enter();
  89. r = fs_resolve(path, &m, rel, sizeof(rel));
  90. if (r == 0) {
  91. r = m->ops->write_file(m, rel, buf, len);
  92. if (r < 0)
  93. r = sys_err(r);
  94. }
  95. sys_fs_exit();
  96. return r;
  97. }
  98. int sys_unlink(const char *path)
  99. {
  100. struct vfs_mount *m;
  101. char rel[VFS_PATH_MAX];
  102. int r;
  103. sys_fs_enter();
  104. r = fs_resolve(path, &m, rel, sizeof(rel));
  105. if (r == 0) {
  106. r = m->ops->unlink(m, rel);
  107. if (r < 0)
  108. r = sys_err(r);
  109. }
  110. sys_fs_exit();
  111. return r;
  112. }
  113. int sys_mkdir(const char *path)
  114. {
  115. struct vfs_mount *m;
  116. char rel[VFS_PATH_MAX];
  117. int r;
  118. sys_fs_enter();
  119. r = fs_resolve(path, &m, rel, sizeof(rel));
  120. if (r == 0) {
  121. r = m->ops->mkdir(m, rel);
  122. if (r < 0)
  123. r = sys_err(r);
  124. }
  125. sys_fs_exit();
  126. return r;
  127. }
  128. int sys_rmdir(const char *path)
  129. {
  130. struct vfs_mount *m;
  131. char rel[VFS_PATH_MAX];
  132. int r;
  133. sys_fs_enter();
  134. r = fs_resolve(path, &m, rel, sizeof(rel));
  135. if (r == 0) {
  136. r = m->ops->rmdir(m, rel);
  137. if (r < 0)
  138. r = sys_err(r);
  139. }
  140. sys_fs_exit();
  141. return r;
  142. }
  143. int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
  144. {
  145. struct vfs_mount *m;
  146. char rel[VFS_PATH_MAX];
  147. int r;
  148. sys_fs_enter();
  149. r = fs_resolve(path, &m, rel, sizeof(rel));
  150. if (r == 0) {
  151. struct vfs_dirent d;
  152. unsigned int i;
  153. r = m->ops->list_dir(m, rel, index, &d);
  154. if (r == 1) {
  155. for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++)
  156. out->name[i] = d.name[i];
  157. out->name[i] = '\0';
  158. out->size = d.size;
  159. out->is_dir = d.is_dir;
  160. r = 1;
  161. } else if (r < 0) {
  162. r = sys_err(r);
  163. } else {
  164. r = 0;
  165. }
  166. }
  167. sys_fs_exit();
  168. return r;
  169. }
  170. static void copy_bounded(char *dst, const char *src, unsigned int size)
  171. {
  172. unsigned int i = 0;
  173. while (i + 1 < size && src[i]) {
  174. dst[i] = src[i];
  175. i++;
  176. }
  177. dst[i] = '\0';
  178. }
  179. int sys_list_mounts(unsigned int index, struct sys_mount *out)
  180. {
  181. if (index >= vfs_mount_count)
  182. return 0;
  183. copy_bounded(out->name, vfs_mounts[index].name, sizeof(out->name));
  184. copy_bounded(out->type, vfs_mounts[index].type, sizeof(out->type));
  185. out->total_sectors = vfs_mounts[index].total_sectors;
  186. copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label));
  187. return 1;
  188. }