vfs.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Virtual filesystem: filesystems mounted under `/`. Two implementations:
  2. * FAT on block devices (probed by vfs_mount_all) and the read-only ramfs
  3. * backed by the command pack (ramfs.c, mounted at /ramfs). */
  4. #ifndef VFS_H
  5. #define VFS_H
  6. #include "blkdev.h"
  7. #include "fat.h"
  8. /* Largest file the shell will load from a filesystem into a kernel buffer.
  9. * The buffer is allocated from the kernel heap (not a static BSS array), so a
  10. * large ELF on a FAT mount can be spawned without moving the low-memory wall. */
  11. #define VFS_FILE_MAX (2u * 1024u * 1024u)
  12. struct vfs_mount;
  13. struct fs_ops;
  14. struct vfs_file {
  15. const void *data;
  16. unsigned int size;
  17. };
  18. /* One directory entry. `name` is the raw 8.3 field for FAT (space padded,
  19. * `raw_83` set) or the plain file name for the name-string filesystems. */
  20. struct vfs_dirent {
  21. char name[64];
  22. unsigned int size;
  23. unsigned int is_dir;
  24. unsigned int raw_83;
  25. };
  26. /* Per-filesystem error codes (0 = success; the read/list calls additionally
  27. * return positive byte/entry counts). Mapped to SYS_E* by sys_fs.c. */
  28. #define VFS_EIO (-1)
  29. #define VFS_ENOENT (-2)
  30. #define VFS_EINVAL (-3)
  31. #define VFS_EROFS (-4)
  32. #define VFS_ENOSPC (-5)
  33. /* Per-filesystem operations. Paths are relative to the mount (no leading
  34. * '/'); "" is the mount root. Implementations never print; list_dir returns
  35. * 1 (entry filled), 0 (end) or a negative VFS_E*. */
  36. struct fs_ops {
  37. int raw_83; /* dirent names are raw 8.3 (FAT) and need display fixing */
  38. int (*read_file)(struct vfs_mount *m, const char *path,
  39. void *buf, unsigned int max);
  40. int (*load_file)(struct vfs_mount *m, const char *path,
  41. struct vfs_file *out);
  42. void (*release_file)(struct vfs_mount *m, struct vfs_file *f);
  43. int (*stat)(struct vfs_mount *m, const char *path,
  44. unsigned int *size, int *is_dir);
  45. int (*write_file)(struct vfs_mount *m, const char *path,
  46. const void *buf, unsigned int len);
  47. int (*unlink)(struct vfs_mount *m, const char *path);
  48. int (*mkdir)(struct vfs_mount *m, const char *path);
  49. int (*rmdir)(struct vfs_mount *m, const char *path);
  50. int (*list_dir)(struct vfs_mount *m, const char *path,
  51. unsigned int index, struct vfs_dirent *out);
  52. };
  53. typedef struct vfs_mount {
  54. char name[16];
  55. char dev[16]; /* block device name for pN mounts */
  56. const struct fs_ops *ops;
  57. int mounted;
  58. int ro;
  59. fat_fs_t fs; /* FAT mounts only */
  60. unsigned int total_sectors; /* for the mount listing */
  61. char type[8]; /* "FAT12"/"FAT16"/"FAT32"/"ramfs" */
  62. char label[12];
  63. } vfs_mount_t;
  64. #define VFS_MAX_MOUNTS 24
  65. #define VFS_PATH_MAX 64
  66. extern vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
  67. extern unsigned int vfs_mount_count;
  68. /* Scan devices and mount all detected FAT filesystems. The ramfs/share
  69. * mounts are kept available throughout the scan; file syscalls that run on
  70. * another CPU wait until it completes (see vfs_scan_busy). */
  71. void vfs_mount_all(void);
  72. /* Re-mount only one block device after it was formatted. */
  73. void vfs_remount_device(blk_dev_t *d);
  74. /* Set while vfs_mount_all() owns the mount table and the FAT sector buffer.
  75. * sys_fs.c waits for it; the owner (the calling `storage` syscall) is exempt. */
  76. extern volatile int vfs_scan_busy;
  77. extern volatile unsigned int vfs_scan_owner_pid;
  78. /* 1 for the RAM-backed mounts (/ramfs, /share). They are re-added first and
  79. * stay valid while a rescan sleeps, so their file operations do not wait. */
  80. int vfs_mount_is_ram(const vfs_mount_t *m);
  81. /* Called from the wait loops of the storage probes (floppy, ATA, AHCI,
  82. * SDHCI). When the scan belongs to a sleepable process (the `storage`
  83. * command) it sleeps for `ms` instead of spinning, which drops the BKL so
  84. * the WM and other processes keep running; at boot (idle context) and in
  85. * normal I/O it is a no-op and the caller keeps busy-waiting. */
  86. int vfs_scan_relax(unsigned int ms);
  87. /* For a driver wait loop that started at tick t0: once it has waited 2 ms
  88. * or more, relax (sleep 1 ms) if a sleepable rescan owns the disks. Fast
  89. * handshakes and normal I/O never sleep. */
  90. void vfs_scan_poll(unsigned int t0);
  91. /* Mount the command pack (ramfs) at /ramfs; no-op when no pack is loaded. */
  92. void vfs_mount_ramfs(void);
  93. /* Mount the writable in-memory /share store (no-op if it cannot be set up). */
  94. void vfs_mount_share(void);
  95. /* Update the ramfs mount's reported size after ramfs_replace(). */
  96. void vfs_refresh_ramfs(void);
  97. /* Find a mount by name (NULL if not found). */
  98. vfs_mount_t *vfs_find(const char *name);
  99. /* Print the mount list (for `ls /`). */
  100. void vfs_list_root(void);
  101. /* Resolve an absolute path "/<mount>/<path>" into the mount and a normalized
  102. * path relative to it ("" for the mount root) in the caller's buffer. `.`
  103. * components are dropped, repeated slashes collapsed and a trailing slash
  104. * removed, so directory paths from Tab completion ("/ramfs/bin/") work.
  105. * Returns 0 or -1 (bad path / unknown mount). */
  106. int vfs_resolve(const char *path, struct vfs_mount **m,
  107. char *rel_path, unsigned int rel_size);
  108. /* Load a whole file (borrowed pointer for ramfs, the static buffer for FAT);
  109. * release it with vfs_release_file() after use. */
  110. int vfs_load_file(const char *path, struct vfs_file *out,
  111. struct vfs_mount **m_out);
  112. void vfs_release_file(struct vfs_mount *m, struct vfs_file *f);
  113. /* Convenience wrappers for the shell and completion. */
  114. int vfs_stat(const char *path, unsigned int *size, int *is_dir);
  115. int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out);
  116. /* Join `arg` onto `cwd` (arg may be absolute or relative) and normalize the
  117. * result into `out` as an absolute VFS path. "." and duplicate slashes are
  118. * dropped; ".." pops the previous component (popping the mount name yields
  119. * "/", and ".." at "/" stays "/"). Returns 0 on success, -1 if it doesn't fit. */
  120. int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size);
  121. /* After a failed vfs_resolve, returns 1 if the path's first component names a
  122. * known block device (i.e. it exists but has no mounted filesystem). */
  123. int vfs_device_known(const char *path);
  124. /* 1 when the mount is backed by a block device (the FAT driver), 0 for the
  125. * RAM-backed ramfs/share mounts. The file service uses this to block only
  126. * disk filesystems while a destructive format is running. */
  127. int vfs_mount_is_disk(const vfs_mount_t *m);
  128. #endif