| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #ifndef VFS_H |
|---|
| 22 | #define VFS_H |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | #include <fs/exports.h> |
|---|
| 26 | |
|---|
| 27 | #include <llist.h> |
|---|
| 28 | #include <sdevice.h> |
|---|
| 29 | #include <typedefs.h> |
|---|
| 30 | #include <types.h> |
|---|
| 31 | #include <time.h> |
|---|
| 32 | #include <wait.h> |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | #define VFS_ATTR_FILE 1 |
|---|
| 37 | #define VFS_ATTR_DIR 2 |
|---|
| 38 | #define VFS_ATTR_READ 4 |
|---|
| 39 | #define VFS_ATTR_WRITE 8 |
|---|
| 40 | #define VFS_ATTR_BLOCK 16 |
|---|
| 41 | #define VFS_ATTR_CHAR 32 |
|---|
| 42 | #define VFS_ATTR_SOCKET 64 |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | #define FILE_READ 1 |
|---|
| 47 | #define FILE_WRITE 2 |
|---|
| 48 | #define FILE_CREATE_OPEN 4 |
|---|
| 49 | #define FILE_FORCE_OPEN 8 |
|---|
| 50 | #define FILE_FORCE_CREATE 16 |
|---|
| 51 | #define FILE_DIRECTORY 32 |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | #define MAX_FILES 10 |
|---|
| 55 | #define PATH_MAX 2048 |
|---|
| 56 | |
|---|
| 57 | struct VNode; |
|---|
| 58 | struct File; |
|---|
| 59 | struct FileSystem; |
|---|
| 60 | struct VfsSuperBlock; |
|---|
| 61 | struct PollItem; |
|---|
| 62 | struct PollQueue; |
|---|
| 63 | |
|---|
| 64 | int VfsInit(); |
|---|
| 65 | int VfsMountRoot(struct StorageDevice* storageDev); |
|---|
| 66 | int VNodeInitCache(); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | int VfsMount(char* mountPoint,char* deviceName,char* fsName,void* mData); |
|---|
| 70 | struct VfsSuperBlock* VfsAllocSuper(struct StorageDevice* sDev,int flags); |
|---|
| 71 | void VfsFreeSuper(struct VfsSuperBlock* superBlock); |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | int NameToVNode(struct VNode** retVal,char* path,int flags); |
|---|
| 75 | struct VNode* DirNameVNode(char* path,char** retPath,int* retLen); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | struct VNode* VNodeGet(struct VfsSuperBlock* superBlock,vid_t id); |
|---|
| 79 | struct VNode* VNodeGetEmpty(); |
|---|
| 80 | void VNodeRelease(struct VNode* vNode); |
|---|
| 81 | void VNodeLock(struct VNode* vNode); |
|---|
| 82 | void VNodeUnlock(struct VNode* vNode); |
|---|
| 83 | void VNodeWrite(struct VNode* vNode); |
|---|
| 84 | int VNodeRead(struct VNode* vNode); |
|---|
| 85 | void VNodeWaitOn(struct VNode* vNode); |
|---|
| 86 | |
|---|
| 87 | int VfsChangeDir(struct Process* process,char* dirName); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | void VfsFileAccessed(struct VNode* vNode); |
|---|
| 91 | void VfsFileModified(struct VNode* vNode,int create); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | int FileGenericRead(struct File* file,BYTE* buffer,size_t size); |
|---|
| 95 | int VfsGetFreeFd(struct Process* process); |
|---|
| 96 | struct File* FileGet(int fd); |
|---|
| 97 | int DoOpenFile(struct File* file,char* path,int flags,int perms); |
|---|
| 98 | int DoReadFile(struct File* file,BYTE* buffer,size_t size); |
|---|
| 99 | int DoWriteFile(struct File* file,BYTE* buffer,size_t size); |
|---|
| 100 | int DoCloseFile(struct File* file); |
|---|
| 101 | int DoSeekFile(struct File* file,int distance,int whence); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | int FileGenericReadPage(addr_t pageAddr,struct VNode* vNode,int offset); |
|---|
| 105 | |
|---|
| 106 | char* GetName(char* path); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | typedef struct |
|---|
| 111 | { |
|---|
| 112 | WORD major; |
|---|
| 113 | WORD minor; |
|---|
| 114 | } DevId; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | struct DirEntry |
|---|
| 119 | { |
|---|
| 120 | DWORD vNodeNum; |
|---|
| 121 | DWORD offset; |
|---|
| 122 | WORD length; |
|---|
| 123 | char name[1]; |
|---|
| 124 | }; |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | #define VNODE_DIRTY 1 |
|---|
| 133 | |
|---|
| 134 | struct VNode |
|---|
| 135 | { |
|---|
| 136 | struct VNode* mountNode; |
|---|
| 137 | DWORD flags,refs,size,mode; |
|---|
| 138 | vid_t id; |
|---|
| 139 | struct VfsSuperBlock* superBlock; |
|---|
| 140 | struct VNodeOps* vNodeOps; |
|---|
| 141 | struct FileOps* fileOps; |
|---|
| 142 | struct ListHead next; |
|---|
| 143 | DevId devId; |
|---|
| 144 | WaitQueue waitQueue; |
|---|
| 145 | int lock; |
|---|
| 146 | void* extraInfo; |
|---|
| 147 | struct Time aTime,cTime,mTime; |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | struct ListHead sharedList; |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | #define SetVNodeDirty(vNode) (((vNode)->flags) |= VNODE_DIRTY) |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | #define FILE_NONBLOCK 0x1 |
|---|
| 163 | |
|---|
| 164 | struct File |
|---|
| 165 | { |
|---|
| 166 | struct VNode* vNode; |
|---|
| 167 | int position; |
|---|
| 168 | int flags; |
|---|
| 169 | struct FileOps* fileOps; |
|---|
| 170 | }; |
|---|
| 171 | |
|---|
| 172 | struct FileOps |
|---|
| 173 | { |
|---|
| 174 | int (*open)(struct File* file); |
|---|
| 175 | int (*close)(struct File* file); |
|---|
| 176 | int (*read)(struct File* file,BYTE* buffer,DWORD size); |
|---|
| 177 | int (*write)(struct File* file,BYTE* buffer,DWORD size); |
|---|
| 178 | int (*seek)(struct File* file,int pos,int whence); |
|---|
| 179 | int (*readDir)(struct File* file,void* dirEntries); |
|---|
| 180 | int (*ioctl)(struct File* file,unsigned long code,char* data); |
|---|
| 181 | int (*poll)(struct File* file, struct PollItem* item, struct PollQueue* pollQueue); |
|---|
| 182 | int (*mMap)(struct VNode* vNode, DWORD address, DWORD offset); |
|---|
| 183 | }; |
|---|
| 184 | |
|---|
| 185 | int FillDir(void* dirEntry,char* name,int nameLen,DWORD vNodeNum); |
|---|
| 186 | |
|---|
| 187 | struct VNodeOps |
|---|
| 188 | { |
|---|
| 189 | int (*create)(struct VNode** retVal,struct VNode* dir,char* name,int nameLength); |
|---|
| 190 | int (*remove)(struct VNode* dir,char* name,int nameLen); |
|---|
| 191 | int (*lookup)(struct VNode** retVal,struct VNode* dir,char* name,int nameLength); |
|---|
| 192 | int (*mkDir)(struct VNode** retVal,struct VNode* dir,char* name,int nameLength); |
|---|
| 193 | int (*rmDir)(struct VNode* dir,char* name,int nameLength); |
|---|
| 194 | int (*permission)(struct VNode* vNode,int access); |
|---|
| 195 | int (*blockMap)(struct VNode* vNode,int offset); |
|---|
| 196 | int (*truncate)(struct VNode* vNode,int size); |
|---|
| 197 | }; |
|---|
| 198 | |
|---|
| 199 | struct SuperBlockOps |
|---|
| 200 | { |
|---|
| 201 | |
|---|
| 202 | int (*allocVNode)(struct VNode* vNode); |
|---|
| 203 | int (*freeVNode)(struct VNode* vNode); |
|---|
| 204 | int (*readVNode)(struct VNode* vNode); |
|---|
| 205 | int (*writeVNode)(struct VNode* vNode); |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | int (*writeSuper)(struct VfsSuperBlock* superBlock); |
|---|
| 209 | }; |
|---|
| 210 | |
|---|
| 211 | #define SB_DIRTY 0x1 |
|---|
| 212 | #define SB_RDONLY 0x2 |
|---|
| 213 | |
|---|
| 214 | struct VfsSuperBlock |
|---|
| 215 | { |
|---|
| 216 | struct SuperBlockOps* sbOps; |
|---|
| 217 | void* privData; |
|---|
| 218 | DWORD coveredId; |
|---|
| 219 | struct VfsSuperBlock* parent; |
|---|
| 220 | struct StorageDevice* sDevice; |
|---|
| 221 | struct VNode* mount; |
|---|
| 222 | int flags; |
|---|
| 223 | struct ListHead vNodeList, sbList; |
|---|
| 224 | }; |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | #define BYTES_PER_SECTOR(s) ((s)->sDevice->softBlockSize) |
|---|
| 229 | |
|---|
| 230 | struct Stat |
|---|
| 231 | { |
|---|
| 232 | DWORD size,vNum,mode; |
|---|
| 233 | unsigned long aTime,cTime,mTime; |
|---|
| 234 | }PACKED; |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | typedef struct VfsSuperBlock* (*VfsReadSuperFunc)(struct StorageDevice* dev,int flags,char* data); |
|---|
| 238 | |
|---|
| 239 | struct FileSystem |
|---|
| 240 | { |
|---|
| 241 | char* name; |
|---|
| 242 | VfsReadSuperFunc readSuper; |
|---|
| 243 | struct ListHead list; |
|---|
| 244 | }; |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | int VfsRegisterFileSystem(struct FileSystem* fs); |
|---|
| 248 | int VfsDeregisterFileSystem(struct FileSystem* fs); |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | #define BUFFER_DIRTY 0x1 |
|---|
| 255 | #define BUFFER_LOCKED 0x2 |
|---|
| 256 | |
|---|
| 257 | struct Buffer |
|---|
| 258 | { |
|---|
| 259 | struct StorageDevice* device; |
|---|
| 260 | struct ListHead list; |
|---|
| 261 | DWORD blockNum; |
|---|
| 262 | int flags,refs; |
|---|
| 263 | BYTE* data; |
|---|
| 264 | WaitQueue waitQueue; |
|---|
| 265 | }; |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | int BlockInit(); |
|---|
| 270 | struct Buffer* BlockRead(struct StorageDevice* device,DWORD blockNum); |
|---|
| 271 | int BlockWrite(struct StorageDevice* device,struct Buffer* buffer); |
|---|
| 272 | int BlockSyncAll(); |
|---|
| 273 | int BlockSyncDevice(struct StorageDevice* device); |
|---|
| 274 | int BlockFree(struct Buffer* buffer); |
|---|
| 275 | int BlockSetSize(struct StorageDevice* device,int blockSize); |
|---|
| 276 | void WaitForBuffer(struct Buffer* buffer); |
|---|
| 277 | void BufferUnlock(struct Buffer* buffer); |
|---|
| 278 | void BufferLock(struct Buffer* buffer); |
|---|
| 279 | struct Buffer* BlockBufferAlloc(struct StorageDevice* device,DWORD blockNum); |
|---|
| 280 | int BlockSendRequest(struct StorageDevice* device,struct Buffer* buff,int type); |
|---|
| 281 | int BlockSendRequestRaw(struct StorageDevice* device,struct Request* request); |
|---|
| 282 | struct Buffer* BlockFindBuffer(struct StorageDevice* sDevice,DWORD blockNum); |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | #define POLL_IN 1 |
|---|
| 286 | #define POLL_OUT 2 |
|---|
| 287 | |
|---|
| 288 | struct PollQueue |
|---|
| 289 | { |
|---|
| 290 | int i; |
|---|
| 291 | int numFds; |
|---|
| 292 | WaitQueue** waitQueues; |
|---|
| 293 | struct WaitQueueEntry* waitEntries; |
|---|
| 294 | }; |
|---|
| 295 | |
|---|
| 296 | struct PollItem |
|---|
| 297 | { |
|---|
| 298 | int fd; |
|---|
| 299 | short events; |
|---|
| 300 | short revents; |
|---|
| 301 | }; |
|---|
| 302 | |
|---|
| 303 | int PollAddWait(struct PollQueue* pollQueue, WaitQueue* waitQueue); |
|---|
| 304 | |
|---|
| 305 | #endif |
|---|