sys_fs.c 4.6 KB

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