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

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