/ kernel / include / vfs.h
vfs.h
  1  #ifndef VFS_H
  2  #define VFS_H
  3  
  4  #include "types.h"
  5  #include "fs.h"
  6  
  7  // VFS limits
  8  #define VFS_NAME_MAX     255
  9  #define VFS_PATH_MAX     4096
 10  #define VFS_MOUNT_MAX    32
 11  #define VFS_OPEN_MAX     1024
 12  
 13  // File types
 14  #define VFS_FILE         1
 15  #define VFS_DIRECTORY    2
 16  #define VFS_CHARDEV      3
 17  #define VFS_BLOCKDEV     4
 18  #define VFS_PIPE         5
 19  #define VFS_SYMLINK      6
 20  #define VFS_MOUNTPOINT   8
 21  
 22  // File modes (permissions)
 23  #define VFS_IRUSR    0400
 24  #define VFS_IWUSR    0200
 25  #define VFS_IXUSR    0100
 26  #define VFS_IRGRP    0040
 27  #define VFS_IWGRP    0020
 28  #define VFS_IXGRP    0010
 29  #define VFS_IROTH    0004
 30  #define VFS_IWOTH    0002
 31  #define VFS_IXOTH    0001
 32  
 33  // Open flags
 34  #define O_RDONLY     0x0000
 35  #define O_WRONLY     0x0001
 36  #define O_RDWR       0x0002
 37  #define O_CREAT      0x0040
 38  #define O_EXCL       0x0080
 39  #define O_TRUNC      0x0200
 40  #define O_APPEND     0x0400
 41  #define O_DIRECTORY  0x10000
 42  
 43  // Seek whence
 44  #define SEEK_SET     0
 45  #define SEEK_CUR     1
 46  #define SEEK_END     2
 47  
 48  // Forward declarations
 49  struct vfs_node;
 50  struct vfs_dirent;
 51  struct stat;
 52  
 53  // VFS operations
 54  typedef struct vfs_operations {
 55      // File operations
 56      uint32_t (*read)(struct vfs_node*, uint32_t offset, uint32_t size, uint8_t* buffer);
 57      uint32_t (*write)(struct vfs_node*, uint32_t offset, uint32_t size, uint8_t* buffer);
 58      void (*open)(struct vfs_node*, uint32_t flags);
 59      void (*close)(struct vfs_node*);
 60      
 61      // Directory operations
 62      struct vfs_dirent* (*readdir)(struct vfs_node*, uint32_t index);
 63      struct vfs_node* (*finddir)(struct vfs_node*, const char* name);
 64      struct vfs_node* (*create)(struct vfs_node*, const char* name, uint16_t mode);
 65      int (*unlink)(struct vfs_node*, const char* name);
 66      
 67      // Metadata operations
 68      int (*stat)(struct vfs_node*, struct stat* st);
 69      int (*chmod)(struct vfs_node*, uint16_t mode);
 70      int (*chown)(struct vfs_node*, uint32_t uid, uint32_t gid);
 71  } vfs_operations_t;
 72  
 73  // VFS node (inode)
 74  typedef struct vfs_node {
 75      char name[VFS_NAME_MAX];    // Filename
 76      uint32_t inode;             // Inode number
 77      uint32_t flags;             // Type and permissions
 78      uint32_t uid;               // Owner user ID
 79      uint32_t gid;               // Owner group ID
 80      uint32_t size;              // File size
 81      uint32_t atime;             // Access time
 82      uint32_t mtime;             // Modification time
 83      uint32_t ctime;             // Creation time
 84      
 85      vfs_operations_t* ops;      // Operations table
 86      struct vfs_node* ptr;       // Used by mountpoints and symlinks
 87      
 88      uint32_t refcount;          // Reference count
 89      void* private_data;         // Filesystem-specific data
 90  } vfs_node_t;
 91  
 92  // Directory entry
 93  typedef struct vfs_dirent {
 94      char name[VFS_NAME_MAX];
 95      uint32_t inode;
 96  } vfs_dirent_t;
 97  
 98  // Stat structure
 99  struct stat {
100      uint32_t st_dev;      // Device ID
101      uint32_t st_ino;      // Inode number
102      uint16_t st_mode;     // File mode
103      uint32_t st_nlink;    // Number of hard links
104      uint32_t st_uid;      // User ID
105      uint32_t st_gid;      // Group ID
106      uint32_t st_size;     // File size
107      uint32_t st_atime;    // Access time
108      uint32_t st_mtime;    // Modification time
109      uint32_t st_ctime;    // Creation time
110  };
111  
112  // Mount point
113  typedef struct vfs_mount {
114      char path[VFS_PATH_MAX];    // Mount path
115      vfs_node_t* root;           // Root node of mounted filesystem
116      struct vfs_mount* next;     // Next mount point
117  } vfs_mount_t;
118  
119  // VFS functions
120  void vfs_init(void);
121  vfs_node_t* vfs_open(const char* path, uint32_t flags);
122  void vfs_close(vfs_node_t* node);
123  uint32_t vfs_read(vfs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer);
124  uint32_t vfs_write(vfs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer);
125  vfs_dirent_t* vfs_readdir(vfs_node_t* node, uint32_t index);
126  vfs_node_t* vfs_finddir(vfs_node_t* node, const char* name);
127  int vfs_stat(const char* path, struct stat* st);
128  int vfs_mount(const char* path, vfs_node_t* root);
129  int vfs_unmount(const char* path);
130  
131  // Path resolution
132  vfs_node_t* vfs_resolve_path(const char* path);
133  char* vfs_canonicalize_path(const char* path);
134  
135  // Global VFS root
136  extern vfs_node_t* vfs_root;
137  
138  #endif // VFS_H