vfs.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "fat.h"
  7. /* Largest file the shell will load from a filesystem into a kernel buffer. */
  8. #define VFS_FILE_MAX (16 * 1024)
  9. struct vfs_mount;
  10. struct fs_ops;
  11. struct vfs_file {
  12. const void *data;
  13. unsigned int size;
  14. };
  15. /* One directory entry: `name` is the raw 8.3 field for FAT (space padded) or
  16. * the plain file name for ramfs. */
  17. struct vfs_dirent {
  18. char name[12];
  19. unsigned int size;
  20. unsigned int is_dir;
  21. };
  22. /* Per-filesystem error codes (0 = success; the read/list calls additionally
  23. * return positive byte/entry counts). Mapped to SYS_E* by sys_fs.c. */
  24. #define VFS_EIO (-1)
  25. #define VFS_ENOENT (-2)
  26. #define VFS_EINVAL (-3)
  27. #define VFS_EROFS (-4)
  28. /* Per-filesystem operations. Paths are relative to the mount (no leading
  29. * '/'); "" is the mount root. Implementations never print; list_dir returns
  30. * 1 (entry filled), 0 (end) or a negative VFS_E*. */
  31. struct fs_ops {
  32. int raw_83; /* dirent names are raw 8.3 (FAT) and need display fixing */
  33. int (*read_file)(struct vfs_mount *m, const char *path,
  34. void *buf, unsigned int max);
  35. int (*load_file)(struct vfs_mount *m, const char *path,
  36. struct vfs_file *out);
  37. void (*release_file)(struct vfs_mount *m, struct vfs_file *f);
  38. int (*stat)(struct vfs_mount *m, const char *path,
  39. unsigned int *size, int *is_dir);
  40. int (*write_file)(struct vfs_mount *m, const char *path,
  41. const void *buf, unsigned int len);
  42. int (*unlink)(struct vfs_mount *m, const char *path);
  43. int (*mkdir)(struct vfs_mount *m, const char *path);
  44. int (*rmdir)(struct vfs_mount *m, const char *path);
  45. int (*list_dir)(struct vfs_mount *m, const char *path,
  46. unsigned int index, struct vfs_dirent *out);
  47. };
  48. typedef struct vfs_mount {
  49. char name[16];
  50. const struct fs_ops *ops;
  51. int mounted;
  52. int ro;
  53. fat_fs_t fs; /* FAT mounts only */
  54. unsigned int total_sectors; /* for the mount listing */
  55. char type[8]; /* "FAT12"/"FAT16"/"FAT32"/"ramfs" */
  56. char label[12];
  57. } vfs_mount_t;
  58. #define VFS_MAX_MOUNTS 24
  59. #define VFS_PATH_MAX 64
  60. extern vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
  61. extern unsigned int vfs_mount_count;
  62. /* Scan devices and mount all detected FAT filesystems. */
  63. void vfs_mount_all(void);
  64. /* Mount the command pack (ramfs) at /ramfs; no-op when no pack is loaded. */
  65. void vfs_mount_ramfs(void);
  66. /* Update the ramfs mount's reported size after ramfs_replace(). */
  67. void vfs_refresh_ramfs(void);
  68. /* Find a mount by name (NULL if not found). */
  69. vfs_mount_t *vfs_find(const char *name);
  70. /* Print the mount list (for `ls /`). */
  71. void vfs_list_root(void);
  72. /* Resolve an absolute path "/<mount>/<path>" into the mount and a normalized
  73. * path relative to it ("" for the mount root) in the caller's buffer. `.`
  74. * components are dropped, repeated slashes collapsed and a trailing slash
  75. * removed, so directory paths from Tab completion ("/ramfs/bin/") work.
  76. * Returns 0 or -1 (bad path / unknown mount). */
  77. int vfs_resolve(const char *path, struct vfs_mount **m,
  78. char *rel_path, unsigned int rel_size);
  79. /* Load a whole file (borrowed pointer for ramfs, the static buffer for FAT);
  80. * release it with vfs_release_file() after use. */
  81. int vfs_load_file(const char *path, struct vfs_file *out,
  82. struct vfs_mount **m_out);
  83. void vfs_release_file(struct vfs_mount *m, struct vfs_file *f);
  84. /* Convenience wrappers for the shell and completion. */
  85. int vfs_stat(const char *path, unsigned int *size, int *is_dir);
  86. int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out);
  87. /* Join `arg` onto `cwd` (arg may be absolute or relative) and normalize the
  88. * result into `out` as an absolute VFS path. "." and duplicate slashes are
  89. * dropped; ".." pops the previous component (popping the mount name yields
  90. * "/", and ".." at "/" stays "/"). Returns 0 on success, -1 if it doesn't fit. */
  91. int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size);
  92. /* After a failed vfs_resolve, returns 1 if the path's first component names a
  93. * known block device (i.e. it exists but has no mounted filesystem). */
  94. int vfs_device_known(const char *path);
  95. #endif