sys_fs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. *
  15. * A destructive `format` runs as a kernel thread without the BKL and sets
  16. * format_busy while it rewrites a disk. Disk-backed mounts wait for it;
  17. * the RAM-backed mounts (/ramfs, /share) do not, so the WM's periodic
  18. * /share stats write and the terminal keep running during a format. */
  19. #include "syscall.h"
  20. #include "fatformat.h"
  21. #include "kmalloc.h"
  22. #include "paging.h"
  23. #include "process.h"
  24. #include "vfs.h"
  25. /* Wait for a destructive format that owns the disks while its FAT is
  26. * half-written. Sleeping drops the BKL so the formatter can finish. */
  27. static void fs_format_wait(void)
  28. {
  29. while (format_busy && current_proc()->pid != format_owner_pid) {
  30. if (!process_is_alive(format_owner_pid)) {
  31. format_busy = 0;
  32. break;
  33. }
  34. process_sleep_ms(10);
  35. }
  36. }
  37. /* A boot-time `storage` scan (or a concurrent storage command) is rebuilding
  38. * the mount table and reading through the FAT static buffers. Wait for it
  39. * instead of racing. */
  40. static void fs_scan_wait(void)
  41. {
  42. while (vfs_scan_busy && current_proc()->pid != vfs_scan_owner_pid) {
  43. if (vfs_scan_owner_pid && !process_is_alive(vfs_scan_owner_pid)) {
  44. vfs_scan_busy = 0;
  45. break;
  46. }
  47. process_sleep_ms(10);
  48. }
  49. }
  50. static int fs_resolve(const char *path, struct vfs_mount **m,
  51. char *rel, unsigned int rel_size);
  52. /* The disk may need its IRQ (floppy), and the FAT driver's static state must
  53. * not be preempted in the middle of an operation. */
  54. static void fs_preempt_enter(void)
  55. {
  56. preempt_disable();
  57. __asm__ volatile ("sti");
  58. }
  59. void sys_fs_enter(void)
  60. {
  61. /* Used by disk-touching services (the storage scan, ELF loads): wait
  62. * for a format and a mount-table rebuild, then take the non-
  63. * preemptible section. */
  64. fs_format_wait();
  65. fs_scan_wait();
  66. fs_preempt_enter();
  67. }
  68. /* sys_fs_enter() for an ELF load: a RAM-backed path (the usual
  69. * /ramfs/bin/<cmd>) neither waits for a rescan nor for a format, so the WM
  70. * and terminals can launch commands while `storage` probes the disks. */
  71. void sys_fs_enter_path(const char *path)
  72. {
  73. struct vfs_mount *m;
  74. char rel[VFS_PATH_MAX];
  75. if (!(path && fs_resolve(path, &m, rel, sizeof(rel)) == 0 &&
  76. vfs_mount_is_ram(m))) {
  77. fs_format_wait();
  78. fs_scan_wait();
  79. }
  80. fs_preempt_enter();
  81. }
  82. /* The `storage` rescan: wait for a format or another scan, but do NOT
  83. * disable preemption. The scan publishes vfs_scan_busy (disk file
  84. * operations wait for it) and its probes sleep through vfs_scan_relax(),
  85. * so the rest of the system keeps running while the disks are probed. */
  86. void sys_fs_scan(void)
  87. {
  88. fs_format_wait();
  89. fs_scan_wait();
  90. __asm__ volatile ("sti");
  91. vfs_mount_all();
  92. __asm__ volatile ("cli");
  93. }
  94. void sys_fs_exit(void)
  95. {
  96. __asm__ volatile ("cli");
  97. preempt_enable();
  98. }
  99. static int sys_err(int vfs_err)
  100. {
  101. switch (vfs_err) {
  102. case VFS_EIO: return SYS_EIO;
  103. case VFS_ENOENT: return SYS_ENOENT;
  104. case VFS_EROFS: return SYS_EROFS;
  105. case VFS_ENOSPC: return SYS_ENOSPC;
  106. default: return SYS_EINVAL;
  107. }
  108. }
  109. /* Resolve an absolute path to (mount, relative path); returns 0 or a SYS_E*
  110. * code (mirrors the old "no such device" vs "no filesystem" distinction). */
  111. static int fs_resolve(const char *path, struct vfs_mount **m,
  112. char *rel, unsigned int rel_size)
  113. {
  114. char abs[VFS_PATH_MAX * 2];
  115. if (!path)
  116. return SYS_ENODEV;
  117. /* Relative paths resolve against the calling process's cwd (inherited
  118. * from the terminal that spawned it); "" means the cwd itself. */
  119. if (path[0] != '/') {
  120. if (vfs_join_path(current_proc()->cwd, path, abs, sizeof(abs)) != 0)
  121. return SYS_EINVAL;
  122. path = abs;
  123. }
  124. if (path[0] == '/' && path[1] == '\0')
  125. path = "/"; /* the root listing goes through vfs_resolve below */
  126. if (vfs_resolve(path, m, rel, rel_size) != 0)
  127. return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
  128. return 0;
  129. }
  130. /* Resolve and enter the non-preemptible section for one path operation.
  131. * Disk-backed mounts wait for a running format; the RAM mounts do not. */
  132. static int sys_fs_begin(const char *path, struct vfs_mount **m,
  133. char *rel, unsigned int rel_size)
  134. {
  135. int r;
  136. /* /ramfs and /share are re-added first by a rescan and stay valid
  137. * while it sleeps: only disk paths (and paths that do not resolve
  138. * yet, which may be a disk being re-mounted) wait for the scan. */
  139. r = fs_resolve(path, m, rel, rel_size);
  140. if (vfs_scan_busy && (r != 0 || !vfs_mount_is_ram(*m))) {
  141. fs_scan_wait();
  142. r = fs_resolve(path, m, rel, rel_size);
  143. }
  144. if (r != 0)
  145. return r;
  146. if (vfs_mount_is_disk(*m)) {
  147. fs_format_wait();
  148. /* The mount table (or the mount slot) may have been rebuilt
  149. * while sleeping; resolve again before using it. */
  150. r = fs_resolve(path, m, rel, rel_size);
  151. if (r != 0)
  152. return r;
  153. }
  154. fs_preempt_enter();
  155. return 0;
  156. }
  157. /* sys_fs_begin() for a modifying operation: a read-only mount (the ramfs,
  158. * or a volume with the SYS_MOUNT_RO policy) refuses it up front. */
  159. static int sys_fs_begin_w(const char *path, struct vfs_mount **m,
  160. char *rel, unsigned int rel_size)
  161. {
  162. int r = sys_fs_begin(path, m, rel, rel_size);
  163. if (r == 0 && (*m)->ro) {
  164. sys_fs_exit();
  165. return SYS_EROFS;
  166. }
  167. return r;
  168. }
  169. int sys_read_file(const char *path, void *buf, unsigned int max)
  170. {
  171. struct vfs_mount *m;
  172. char rel[VFS_PATH_MAX];
  173. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  174. if (r != 0)
  175. return r;
  176. r = m->ops->read_file(m, rel, buf, max);
  177. if (r < 0)
  178. r = sys_err(r);
  179. sys_fs_exit();
  180. return r;
  181. }
  182. int sys_write_file(const char *path, const void *buf, unsigned int len)
  183. {
  184. struct vfs_mount *m;
  185. char rel[VFS_PATH_MAX];
  186. int r = sys_fs_begin_w(path, &m, rel, sizeof(rel));
  187. if (r != 0)
  188. return r;
  189. r = m->ops->write_file(m, rel, buf, len);
  190. if (r < 0)
  191. r = sys_err(r);
  192. sys_fs_exit();
  193. return r;
  194. }
  195. int sys_unlink(const char *path)
  196. {
  197. struct vfs_mount *m;
  198. char rel[VFS_PATH_MAX];
  199. int r = sys_fs_begin_w(path, &m, rel, sizeof(rel));
  200. if (r != 0)
  201. return r;
  202. r = m->ops->unlink(m, rel);
  203. if (r < 0)
  204. r = sys_err(r);
  205. sys_fs_exit();
  206. return r;
  207. }
  208. int sys_mkdir(const char *path)
  209. {
  210. struct vfs_mount *m;
  211. char rel[VFS_PATH_MAX];
  212. int r = sys_fs_begin_w(path, &m, rel, sizeof(rel));
  213. if (r != 0)
  214. return r;
  215. r = m->ops->mkdir(m, rel);
  216. if (r < 0)
  217. r = sys_err(r);
  218. sys_fs_exit();
  219. return r;
  220. }
  221. int sys_rmdir(const char *path)
  222. {
  223. struct vfs_mount *m;
  224. char rel[VFS_PATH_MAX];
  225. int r = sys_fs_begin_w(path, &m, rel, sizeof(rel));
  226. if (r != 0)
  227. return r;
  228. r = m->ops->rmdir(m, rel);
  229. if (r < 0)
  230. r = sys_err(r);
  231. sys_fs_exit();
  232. return r;
  233. }
  234. int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
  235. {
  236. struct vfs_mount *m;
  237. char rel[VFS_PATH_MAX];
  238. struct vfs_dirent d;
  239. unsigned int i;
  240. int r = sys_fs_begin(path, &m, rel, sizeof(rel));
  241. if (r != 0)
  242. return r;
  243. r = m->ops->list_dir(m, rel, index, &d);
  244. if (r == 1) {
  245. for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++)
  246. out->name[i] = d.name[i];
  247. out->name[i] = '\0';
  248. out->size = d.size;
  249. out->is_dir = d.is_dir;
  250. out->raw_83 = d.raw_83;
  251. r = 1;
  252. } else if (r < 0) {
  253. r = sys_err(r);
  254. } else {
  255. r = 0;
  256. }
  257. sys_fs_exit();
  258. return r;
  259. }
  260. static void copy_bounded(char *dst, const char *src, unsigned int size)
  261. {
  262. unsigned int i = 0;
  263. while (i + 1 < size && src[i]) {
  264. dst[i] = src[i];
  265. i++;
  266. }
  267. dst[i] = '\0';
  268. }
  269. int sys_list_mounts(unsigned int index, struct sys_mount *out)
  270. {
  271. if (index >= vfs_mount_count)
  272. return 0;
  273. copy_bounded(out->name, vfs_mounts[index].name, sizeof(out->name));
  274. copy_bounded(out->type, vfs_mounts[index].type, sizeof(out->type));
  275. out->total_sectors = vfs_mounts[index].total_sectors;
  276. copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label));
  277. out->ro = (unsigned int)vfs_mounts[index].ro;
  278. copy_bounded(out->dev, vfs_mounts[index].dev, sizeof(out->dev));
  279. copy_bounded(out->key, vfs_mounts[index].key, sizeof(out->key));
  280. return 1;
  281. }
  282. /* One raw sector of a block device for the `disks` partition view. Read
  283. * into a kernel buffer (some drivers DMA through their own bounce buffers,
  284. * others write the destination directly), then copy out. */
  285. int sys_blk_read(unsigned int index, unsigned int lba, void *buf)
  286. {
  287. unsigned char sec[512];
  288. blk_dev_t *d;
  289. unsigned int i;
  290. int r;
  291. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)buf,
  292. sizeof(sec), 1))
  293. return SYS_EINVAL;
  294. sys_fs_enter();
  295. if (index >= blk_dev_count) {
  296. sys_fs_exit();
  297. return SYS_ENODEV;
  298. }
  299. d = &blk_devs[index];
  300. r = SYS_EIO;
  301. if (d->present && lba < d->total_sectors && blk_read(d, lba, sec, 1) == 0)
  302. r = 0;
  303. sys_fs_exit();
  304. if (r == 0)
  305. for (i = 0; i < sizeof(sec); i++)
  306. ((unsigned char *)buf)[i] = sec[i];
  307. return r;
  308. }
  309. int sys_mount_ctl(int op, int a2, int a3)
  310. {
  311. struct sys_mount_policy p;
  312. int r;
  313. if (op == SYS_MOUNT_POLICY_GET) {
  314. if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a3,
  315. sizeof(p), 1))
  316. return SYS_EINVAL;
  317. return vfs_policy_get((unsigned int)a2,
  318. (struct sys_mount_policy *)a3);
  319. }
  320. if (op != SYS_MOUNT_POLICY_SET ||
  321. !paging_user_range_ok(current_proc()->cr3, (unsigned int)a2,
  322. sizeof(p), 0))
  323. return SYS_EINVAL;
  324. p = *(const struct sys_mount_policy *)a2;
  325. sys_fs_enter();
  326. r = vfs_policy_set(&p) == 0 ? 0 : SYS_EINVAL;
  327. sys_fs_exit();
  328. return r;
  329. }
  330. /* Boot: apply the policies saved by the `disks` app on /data (the boot
  331. * scan's kernel thread, after vfs_mount_all()). A missing file is normal. */
  332. struct mount_cfg {
  333. char magic[8];
  334. struct sys_mount_policy p[SYS_MOUNT_POLICY_MAX];
  335. unsigned char slack[512]; /* a FAT read may fill a whole last sector */
  336. };
  337. void sys_mount_load_config(void)
  338. {
  339. struct mount_cfg *cfg = (struct mount_cfg *)kmalloc(sizeof(*cfg));
  340. int n, i;
  341. if (!cfg)
  342. return;
  343. n = sys_read_file(SYS_MOUNT_CFG_PATH, cfg,
  344. sizeof(*cfg) - sizeof(cfg->slack));
  345. for (i = 0; n >= 8 && i < 8; i++)
  346. if (cfg->magic[i] != SYS_MOUNT_CFG_MAGIC[i])
  347. n = 0;
  348. n = n >= 8 ? (n - 8) / (int)sizeof(cfg->p[0]) : 0;
  349. for (i = 0; i < n; i++) {
  350. cfg->p[i].key[sizeof(cfg->p[i].key) - 1] = '\0';
  351. sys_fs_enter();
  352. vfs_policy_set(&cfg->p[i]);
  353. sys_fs_exit();
  354. }
  355. kfree(cfg);
  356. }
  357. /* Stat a path. Unlike read_file this never touches the disk beyond the FAT
  358. * directory walk, so the WebDAV server can probe a collection cheaply. */
  359. int sys_stat_path(const char *path, struct sys_stat *out)
  360. {
  361. struct vfs_mount *m;
  362. char rel[VFS_PATH_MAX];
  363. unsigned int size = 0, ro = 0;
  364. int is_dir = 0, r;
  365. if (!out)
  366. return SYS_EINVAL;
  367. r = sys_fs_begin(path, &m, rel, sizeof(rel));
  368. if (r != 0)
  369. return r;
  370. r = m->ops->stat(m, rel, &size, &is_dir);
  371. if (r < 0) {
  372. r = sys_err(r);
  373. } else {
  374. ro = (unsigned int)m->ro;
  375. out->size = size;
  376. out->is_dir = (unsigned int)is_dir;
  377. out->ro = ro;
  378. r = 0;
  379. }
  380. sys_fs_exit();
  381. return r;
  382. }