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

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