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

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