sys_fs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* File service for ring-3 commands (see syscall.h for the ABI).
  2. *
  3. * The VFS/FAT layer and the disk drivers stay in the kernel; these calls
  4. * expose whole-file operations on absolute paths. Two properties shape how
  5. * they run:
  6. * - the FAT driver keeps state in static buffers, so no other process may
  7. * run its FAT code concurrently;
  8. * - the floppy driver waits for IRQ6, so interrupts must stay enabled.
  9. * So instead of a lock we set `sys_fs_busy` for the duration: the scheduler
  10. * (schedule() in process.c) will not switch away from the running process
  11. * while it is set. That keeps each operation atomic (and immune to Ctrl+C
  12. * killing the process mid-write) at the cost of a brief system stall during
  13. * disk I/O. Buffers point into the caller's address space, which is also the
  14. * active one during the syscall, so no copy in/out is needed. */
  15. #include "syscall.h"
  16. #include "fat.h"
  17. #include "vfs.h"
  18. volatile int sys_fs_busy;
  19. void sys_fs_enter(void)
  20. {
  21. sys_fs_busy = 1;
  22. __asm__ volatile ("sti"); /* the disk may need its IRQ */
  23. }
  24. void sys_fs_exit(void)
  25. {
  26. __asm__ volatile ("cli");
  27. sys_fs_busy = 0;
  28. }
  29. static void copy_bounded(char *dst, const char *src, unsigned int size)
  30. {
  31. unsigned int i = 0;
  32. while (i + 1 < size && src[i]) {
  33. dst[i] = src[i];
  34. i++;
  35. }
  36. dst[i] = '\0';
  37. }
  38. static const char *fat_type_name(fat_type_t t)
  39. {
  40. switch (t) {
  41. case FAT12: return "FAT12";
  42. case FAT16: return "FAT16";
  43. case FAT32: return "FAT32";
  44. default: return "FAT";
  45. }
  46. }
  47. /* Resolve `path` to (filesystem, parent directory cluster, final 8.3 name).
  48. * Returns 0 or a SYS_E* code. */
  49. static int fs_parent(const char *path, fat_fs_t **fs_out,
  50. unsigned int *dir_cluster, char name_8_3[12])
  51. {
  52. fat_fs_t *fs;
  53. const char *fs_path;
  54. int r;
  55. if (!path || !path[0])
  56. return SYS_ENODEV;
  57. if (vfs_resolve(path, &fs, &fs_path) != 0)
  58. return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
  59. r = fat_resolve_path(fs, fs_path, dir_cluster, name_8_3);
  60. if (r == -2)
  61. return SYS_EIO;
  62. if (r != 0)
  63. return SYS_ENOENT;
  64. *fs_out = fs;
  65. return 0;
  66. }
  67. /* Resolve `path` to (filesystem, directory cluster). */
  68. static int fs_dir(const char *path, fat_fs_t **fs_out, unsigned int *dir_cluster)
  69. {
  70. fat_fs_t *fs;
  71. const char *fs_path;
  72. int r;
  73. if (!path || !path[0])
  74. return SYS_ENODEV;
  75. if (vfs_resolve(path, &fs, &fs_path) != 0)
  76. return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
  77. r = fat_resolve_dir(fs, fs_path, dir_cluster);
  78. if (r == -2)
  79. return SYS_EIO;
  80. if (r != 0)
  81. return SYS_EINVAL; /* exists, but not a directory */
  82. *fs_out = fs;
  83. return 0;
  84. }
  85. int sys_read_file(const char *path, void *buf, unsigned int max)
  86. {
  87. fat_fs_t *fs;
  88. unsigned int dir;
  89. char name[12];
  90. int r;
  91. sys_fs_enter();
  92. r = fs_parent(path, &fs, &dir, name);
  93. if (r == 0) {
  94. r = fat_read_file_in(fs, dir, name, buf, max);
  95. if (r == -2)
  96. r = SYS_EIO;
  97. else if (r < 0)
  98. r = SYS_ENOENT;
  99. }
  100. sys_fs_exit();
  101. return r;
  102. }
  103. int sys_write_file(const char *path, const void *buf, unsigned int len)
  104. {
  105. fat_fs_t *fs;
  106. unsigned int dir;
  107. char name[12];
  108. int r;
  109. sys_fs_enter();
  110. r = fs_parent(path, &fs, &dir, name);
  111. if (r == 0) {
  112. r = fat_write_file_in(fs, dir, name, buf, len);
  113. if (r == -2)
  114. r = SYS_EIO;
  115. else if (r != 0)
  116. r = SYS_EWRITE;
  117. }
  118. sys_fs_exit();
  119. return r;
  120. }
  121. int sys_unlink(const char *path)
  122. {
  123. fat_fs_t *fs;
  124. unsigned int dir;
  125. char name[12];
  126. int r;
  127. sys_fs_enter();
  128. r = fs_parent(path, &fs, &dir, name);
  129. if (r == 0) {
  130. r = fat_delete_file_in(fs, dir, name);
  131. if (r == -2)
  132. r = SYS_EIO;
  133. else if (r != 0)
  134. r = SYS_ENOENT;
  135. }
  136. sys_fs_exit();
  137. return r;
  138. }
  139. int sys_mkdir(const char *path)
  140. {
  141. fat_fs_t *fs;
  142. unsigned int dir;
  143. char name[12];
  144. int r;
  145. sys_fs_enter();
  146. r = fs_parent(path, &fs, &dir, name);
  147. if (r == 0) {
  148. r = fat_mkdir(fs, dir, name);
  149. if (r == -2)
  150. r = SYS_EIO;
  151. else if (r != 0)
  152. r = SYS_EWRITE;
  153. }
  154. sys_fs_exit();
  155. return r;
  156. }
  157. int sys_rmdir(const char *path)
  158. {
  159. fat_fs_t *fs;
  160. unsigned int dir;
  161. char name[12];
  162. int r;
  163. sys_fs_enter();
  164. r = fs_parent(path, &fs, &dir, name);
  165. if (r == 0) {
  166. r = fat_rmdir(fs, dir, name);
  167. if (r == -2)
  168. r = SYS_EIO;
  169. else if (r != 0)
  170. r = SYS_EWRITE;
  171. }
  172. sys_fs_exit();
  173. return r;
  174. }
  175. /* Directory listing: index-based, so the kernel keeps no per-process state.
  176. * `fat_list_dir` walks from the start each call; directories are small. */
  177. static unsigned int list_index;
  178. static unsigned int list_seen;
  179. static struct sys_dirent *list_out;
  180. static int list_done;
  181. static void dirent_cb(const char *name, int is_dir, unsigned int size)
  182. {
  183. unsigned int i;
  184. if (list_done || list_seen < list_index) {
  185. list_seen++;
  186. return;
  187. }
  188. for (i = 0; i < 11; i++)
  189. list_out->name[i] = name[i] ? name[i] : ' ';
  190. list_out->name[11] = '\0';
  191. list_out->size = size;
  192. list_out->is_dir = (unsigned int)is_dir;
  193. list_done = 1;
  194. list_seen++;
  195. }
  196. int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
  197. {
  198. fat_fs_t *fs;
  199. unsigned int dir;
  200. int r;
  201. sys_fs_enter();
  202. r = fs_dir(path, &fs, &dir);
  203. if (r == 0) {
  204. list_index = index;
  205. list_seen = 0;
  206. list_out = out;
  207. list_done = 0;
  208. r = fat_list_dir(fs, dir, dirent_cb);
  209. if (r == -2)
  210. r = SYS_EIO;
  211. else
  212. r = list_done ? 1 : 0; /* 1 = entry returned, 0 = end */
  213. }
  214. sys_fs_exit();
  215. return r;
  216. }
  217. int sys_list_mounts(unsigned int index, struct sys_mount *out)
  218. {
  219. if (index >= vfs_mount_count)
  220. return 0;
  221. copy_bounded(out->name, vfs_mounts[index].name, sizeof(out->name));
  222. copy_bounded(out->type, fat_type_name(vfs_mounts[index].fs.type),
  223. sizeof(out->type));
  224. out->total_sectors = vfs_mounts[index].fs.total_sectors;
  225. copy_bounded(out->label, vfs_mounts[index].fs.label, sizeof(out->label));
  226. return 1;
  227. }