sys_fs.c 12 KB

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