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

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