vfs.h 4.5 KB

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