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

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