Networking - Reworked route table management
[tpg/acess2.git] / Kernel / include / vfs_ext.h
1 /**
2  * \file vfs_ext.h
3  * \brief Exported VFS Definitions
4  * \author John Hodge (thePowersGang)
5  */
6 #ifndef _VFS_EXT_H
7 #define _VFS_EXT_H
8
9 //! Inode number type
10 typedef Uint64  tInode;
11
12 //! Mountpoint identifier type
13 typedef Uint32  tMount;
14
15 // === CONSTANTS ===
16 //! Maximum size of a Memory Path generated by VFS_GetMemPath
17 #define VFS_MEMPATH_SIZE        (3 + (BITS/4)*2)
18 /**
19  * \name Flags for VFS_Open
20  * \{
21  */
22 //! Open for execution
23 #define VFS_OPENFLAG_EXEC       0x01
24 //! Open for reading
25 #define VFS_OPENFLAG_READ       0x02
26 //! Open for writing
27 #define VFS_OPENFLAG_WRITE      0x04
28 //! Do not resolve the final symbolic link
29 #define VFS_OPENFLAG_NOLINK     0x40
30 //! Create the file if it doesn't exist
31 #define VFS_OPENFLAG_CREATE     0x80
32 //! Open as a user
33 #define VFS_OPENFLAG_USER       0x8000
34 /**
35  * \}
36  */
37 //! Marks a VFS handle as belonging to the kernel
38 #define VFS_KERNEL_FLAG 0x40000000
39
40 //! Architectual maximum number of file descriptors
41 #define MAX_FILE_DESCS  128
42
43 /**
44  * \brief VFS_Seek directions
45  */
46 enum eVFS_SeekDirs
47 {
48         SEEK_SET = 1,   //!< Set the current file offset
49         SEEK_CUR = 0,   //!< Seek relative to the current position
50         SEEK_END = -1   //!< Seek from the end of the file backwards
51 };
52
53 /**
54  * \name ACL Permissions
55  * \{
56  */
57 /**
58  * \brief Readable
59  */
60 #define VFS_PERM_READ   0x00000001
61 /**
62  * \brief Writeable
63  */
64 #define VFS_PERM_WRITE  0x00000002
65 /**
66  * \brief Append allowed
67  */
68 #define VFS_PERM_APPEND 0x00000004
69 /**
70  * \brief Executable
71  */
72 #define VFS_PERM_EXECUTE        0x00000008
73 /**
74  * \brief All permissions granted
75  */
76 #define VFS_PERM_ALL    0x7FFFFFFF      // Mask for permissions
77 /**
78  * \brief Denies instead of granting permissions
79  * \note Denials take precedence
80  */
81 #define VFS_PERM_DENY   0x80000000      // Inverts permissions
82 /**
83  * \}
84  */
85
86 /**
87  * \brief MMap protection flags
88  * \{
89  */
90 #define MMAP_PROT_READ  0x001   //!< Readable memory
91 #define MMAP_PROT_WRITE 0x002   //!< Writable memory
92 #define MMAP_PROT_EXEC  0x004   //!< Executable memory
93 /**
94  * \}
95  */
96
97 /**
98  * \brief MMap mapping flags
99  * \{
100  */
101 #define MMAP_MAP_SHARED         0x001   //!< Shared with all other users of the FD
102 #define MMAP_MAP_PRIVATE        0x002   //!< Local (COW) copy
103 #define MMAP_MAP_FIXED          0x004   //!< Load to a fixed address
104 #define MMAP_MAP_ANONYMOUS      0x008   //!< Not associated with a FD
105 /**
106  * \}
107  */
108
109 // -- System Call Structures ---
110 /**
111  * \brief ACL Defintion Structure
112  */
113 typedef struct sVFS_ACL
114 {
115         struct {
116                 unsigned Group: 1;      //!< Group (as opposed to user) flag
117                 unsigned ID:    31;     //!< ID of Group/User (-1 for nobody/world)
118         };
119         struct {
120                 unsigned Inv:   1;      //!< Invert Permissions
121                 unsigned Perms: 31;     //!< Permission Flags
122         };
123 } tVFS_ACL;
124
125 /**
126  * \brief SYS_FINFO structure
127  */
128 typedef struct sFInfo
129 {
130         tMount  mount;  //!< Mountpoint ID
131         tInode  inode;  //!< Inode
132         Uint32  uid;    //!< Owning User ID
133         Uint32  gid;    //!< Owning Group ID
134         Uint32  flags;  //!< File flags
135         Uint64  size;   //!< File Size
136         Sint64  atime;  //!< Last Accessed time
137         Sint64  mtime;  //!< Last modified time
138         Sint64  ctime;  //!< Creation time
139         Sint32  numacls;        //!< Total number of ACL entries
140         tVFS_ACL        acls[]; //!< ACL buffer (size is passed in the \a MaxACLs argument to VFS_FInfo)
141 } PACKED tFInfo;
142
143 /**
144  * \brief fd_set for select()
145  */
146 typedef struct
147 {
148         //! Bitmap of set file descriptors
149         Uint16  flags[MAX_FILE_DESCS/16];
150 }       fd_set;
151
152 /**
153  * \brief Clear a descriptor flag in a fd_set
154  * \param fd    File descriptor to clear
155  * \param fdsetp        Set to modify
156  */
157 #define FD_CLR(fd, fdsetp) ((fdsetp)->flags[(fd)/16]&=~(1<<((fd)%16)))
158 /**
159  * \brief Set a descriptor flag in a fd_set
160  * \param fd    File descriptor to set
161  * \param fdsetp        Set to modify
162  */
163 #define FD_SET(fd, fdsetp) ((fdsetp)->flags[(fd)/16]|=~(1<<((fd)%16)))
164 /**
165  * \brief Test a descriptor flag in a fd_set
166  * \param fd    File descriptor to test
167  * \param fdsetp        Set to modify
168  */
169 #define FD_ISSET(fd, fdsetp) ((fdsetp)->flags[(fd)/16]&(1<<((fd)%16)))
170
171 // === FUNCTIONS ===
172 /**
173  * \brief Initialise the VFS (called by system.c)
174  * \return Boolean Success
175  */
176 extern int      VFS_Init(void);
177
178 /**
179  * \brief Open a file
180  * \param Path  Absolute or relative path to the file
181  * \param Flags Flags defining how to open the file
182  * \return VFS Handle (an integer) or -1 if an error occured
183  * \note Calls VFS_OpenEx(Path, Flags, 0)
184  */
185 extern int      VFS_Open(const char *Path, Uint Flags);
186 /**
187  * \brief Open a file
188  * \param Path  Absolute or relative path to the file
189  * \param Flags Flags defining how to open the file
190  * \param Mode  Mode for newly created file (POSIX compatability)
191  * \return VFS Handle (an integer) or -1 if an error occured
192  */
193 extern int      VFS_OpenEx(const char *Path, Uint Flags, Uint Mode);
194 /**
195  * \brief Opens a file via an open directory
196  * \note The file to open must be a direct child of the parent
197  * \param FD    Parent Directory
198  * \param Name  Child name
199  * \param Mode  Open mode
200  * \return File handle (same as returned from VFS_Open)
201  */
202 extern int      VFS_OpenChild(int FD, const char *Name, Uint Mode);
203 /**
204  * \brief Opens a file given a mount ID and an inode number
205  * \param Mount Mount ID returned by FInfo
206  * \param Inode Inode number from FInfo
207  * \param Mode  Open mode (see VFS_Open)
208  * \return File handle (same as VFS_Open)
209  */
210 extern int      VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode);
211
212 /**
213  * \brief Close a currently open file
214  * \param FD    Handle returned by ::VFS_Open
215  */
216 extern void     VFS_Close(int FD);
217
218 /**
219  * \brief Get file information from an open file
220  * \param FD    File handle returned by ::VFS_Open
221  * \param Dest  Destination for the read information
222  * \param MaxACLs       Number of ACL slots allocated in the \a Dest structure
223  * \return Boolean Success
224  * 
225  * If the \a NumACLs is smaller than the number of ACLs the node has, only
226  * \a NumACLs will be copied into \a Dest, but the tFInfo.numacls field
227  * will be set to the true ammount of ACLs. It is up to the user to do with
228  * this information how they like.
229  */
230 extern int      VFS_FInfo(int FD, tFInfo *Dest, int MaxACLs);
231 /**
232  * \brief Gets the permissions appling to a user/group.
233  * \param FD    File handle returned by ::VFS_Open
234  * \param Dest  ACL information structure to edit
235  * \return Boolean success
236  * 
237  * This function sets the tVFS_ACL.Inv and tVFS_ACL.Perms fields to what
238  * permissions the user/group specied in tVFS_ACL.ID has on the file.
239  */
240 extern int      VFS_GetACL(int FD, tVFS_ACL *Dest);
241 /**
242  * \brief Changes the user's current working directory
243  * \param Dest  New working directory (either absolute or relative to the current)
244  * \return Boolean Success
245  */
246 extern int      VFS_ChDir(const char *Dest);
247 /**
248  * \brief Change the current virtual root for the user
249  * \param New New virtual root (same as ::VFS_ChDir but cannot go
250  *            above the current virtual root)
251  * \return Boolean success
252  */
253 extern int      VFS_ChRoot(const char *New);
254
255 /**
256  * \brief Change the location of the current file pointer
257  * \param FD    File handle returned by ::VFS_Open
258  * \param Offset        Offset within the file to go to
259  * \param Whence        A direction from ::eVFS_SeekDirs
260  * \return Boolean success
261  */
262 extern int      VFS_Seek(int FD, Sint64 Offset, int Whence);
263 /**
264  * \brief Returns the current file pointer
265  * \param FD    File handle returned by ::VFS_Open
266  * \return Current file position
267  */
268 extern Uint64   VFS_Tell(int FD);
269
270 /**
271  * \brief Reads data from a file
272  * \param FD    File handle returned by ::VFS_Open
273  * \param Length        Number of bytes to read from the file
274  * \param Buffer        Destination of read data
275  * \return Number of read bytes
276  */
277 extern Uint64   VFS_Read(int FD, Uint64 Length, void *Buffer);
278 /**
279  * \brief Writes data to a file
280  * \param FD    File handle returned by ::VFS_Open
281  * \param Length        Number of bytes to write to the file
282  * \param Buffer        Source of written data
283  * \return Number of bytes written
284  */
285 extern Uint64   VFS_Write(int FD, Uint64 Length, const void *Buffer);
286
287 /**
288  * \brief Reads from a specific offset in the file
289  * \param FD    File handle returned by ::VFS_Open
290  * \param Offset        Byte offset in the file
291  * \param Length        Number of bytes to read from the file
292  * \param Buffer        Source of read data
293  * \return Number of bytes read
294  */
295 extern Uint64   VFS_ReadAt(int FD, Uint64 Offset, Uint64 Length, void *Buffer);
296 /**
297  * \brief Writes to a specific offset in the file
298  * \param FD    File handle returned by ::VFS_Open
299  * \param Offset        Byte offset in the file
300  * \param Length        Number of bytes to write to the file
301  * \param Buffer        Source of written data
302  * \return Number of bytes written
303  */
304 extern Uint64   VFS_WriteAt(int FD, Uint64 Offset, Uint64 Length, const void *Buffer);
305
306 /**
307  * \brief Sends an IOCtl request to the driver
308  * \param FD    File handle returned by ::VFS_Open
309  * \param ID    IOCtl call ID (driver specific)
310  * \param Buffer        Data pointer to send to the driver
311  * \return Driver specific response
312  */
313 extern int      VFS_IOCtl(int FD, int ID, void *Buffer);
314
315 /**
316  * \brief Creates a VFS Memory path from a pointer and size
317  * \param Dest  Destination for the created path
318  * \param Base  Base of the memory file
319  * \param Length        Length of the memory buffer
320  * \note A maximum of VFS_MEMPATH_SIZE bytes will be required in \a Dest
321  */
322 extern void     VFS_GetMemPath(char *Dest, void *Base, Uint Length);
323 /**
324  * \brief Gets the canoical (true) path of a file
325  * \param Path  File path (may contain symlinks, relative elements etc.)
326  * \return Absolute path with no symlinks
327  */
328 extern char     *VFS_GetTruePath(const char *Path);
329
330 /**
331  * \brief Mounts a filesystem
332  * \param Device        Device to mount
333  * \param MountPoint    Location to mount
334  * \param Filesystem    Filesystem to use
335  * \param Options       Options string to pass the driver
336  * \return 1 on succes, -1 on error
337  */
338 extern int      VFS_Mount(const char *Device, const char *MountPoint, const char *Filesystem, const char *Options);
339 /**
340  * \brief Create a new directory
341  * \param Path  Path to new directory (absolute or relative)
342  * \return Boolean success
343  * \note The parent of the directory must exist
344  */
345 extern int      VFS_MkDir(const char *Path);
346 /**
347  * \brief Create a symbolic link
348  * \param Name  Name of the symbolic link
349  * \param Link  File the symlink points to
350  * \return Boolean success (\a Link is not tested for existence)
351  */
352 extern int      VFS_Symlink(const char *Name, const char *Link);
353 /**
354  * \brief Read from a directory
355  * \param FD    File handle returned by ::VFS_Open
356  * \param Dest  Destination array for the file name (max 255 bytes)
357  * \return Boolean Success
358  */
359 extern int      VFS_ReadDir(int FD, char *Dest);
360 /**
361  * \brief Wait for an aciton on a file descriptor
362  * \param MaxHandle     Maximum set handle in \a *Handles
363  * \param ReadHandles   Handles to wait for data on
364  * \param WriteHandles  Handles to wait to write to
365  * \param ErrHandles    Handle to wait for errors on
366  * \param Timeout       Timeout for select() (if null, there is no timeout), if zero select() is non blocking
367  * \param ExtraEvents   Extra event set to wait on
368  * \param IsKernel      Use kernel handles as opposed to user handles
369  * \return Number of handles that actioned
370  */
371 extern int VFS_Select(int MaxHandle, fd_set *ReadHandles, fd_set *WriteHandles, fd_set *ErrHandles, tTime *Timeout, Uint32 ExtraEvents, BOOL IsKernel);
372
373 /**
374  * \brief Map a file into memory
375  * \param DestHint      Suggested place for read data
376  * \param Length        Size of area to map
377  * \param Protection    Protection type (see `man mmap`)
378  * \param Flags Mapping flags
379  * \param FD    File descriptor to load from
380  * \param Offset        Start of region
381  */
382 extern void     *VFS_MMap(void *DestHint, size_t Length, int Protection, int Flags, int FD, Uint64 Offset);
383
384 /**
385  * \brief Unmap memory allocated by VFS_MMap
386  * \param Addr  Address of data to unmap
387  * \param Length        Length of data
388  */
389 extern int      VFS_MUnmap(void *Addr, size_t Length);
390 #endif

UCC git Repository :: git.ucc.asn.au