Usermode/ld-acess - Disabled PIC (not needed)
[tpg/acess2.git] / Kernel / include / vfs.h
1 /* 
2  * Acess2
3  * VFS Common Header
4  */
5 /**
6  * \file vfs.h
7  * \brief Acess VFS Layer
8  * 
9  * The Acess Virtual File System (VFS) provides abstraction of multiple
10  * physical filesystems, network storage and devices (both hardware and
11  * virtual) to the user.
12  * 
13  * The core of the VFS is the concept of a \ref tVFS_Node "VFS Node".
14  * A VFS Node represents a "file" in the VFS tree, this can be any sort
15  * of file (an ordinary file, a directory, a symbolic link or a device)
16  * depending on the bits set in the \ref tVFS_Node.Flags Flags field.
17  * - For more information see "VFS Node Flags"
18  */
19 #ifndef _VFS_H
20 #define _VFS_H
21
22 #include <acess.h>
23
24 /**
25  * \brief Thread list datatype for VFS_Select
26  */
27 typedef struct sVFS_SelectList  tVFS_SelectList;
28
29 /**
30  * \name tVFS_Node Flags
31  * \brief Flag values for tVFS_Node.Flags
32  * \{
33  */
34 //! \todo Is this still needed
35 #define VFS_FFLAG_READONLY      0x01    //!< Readonly File
36 /**
37  * \brief Directory Flag
38  * 
39  * This flag marks the tVFS_Node as describing a directory, and says
40  * that the tVFS_Node.FindDir, tVFS_Node.ReadDir, tVFS_Node.MkNod and
41  * tVFS_Node.Relink function pointers are valid.
42  * For a directory the tVFS_Node.Size field contains the number of files
43  * within the directory, or -1 for undetermined.
44  */
45 #define VFS_FFLAG_DIRECTORY     0x02
46 /**
47  * \brief Symbolic Link Flag
48  * 
49  * Marks a file as a symbolic link
50  */
51 #define VFS_FFLAG_SYMLINK       0x04
52 /**
53  * \brief Set User ID Flag
54  * 
55  * Allows an executable file to change it's executing user to the file's
56  * owner.
57  * In the case of a directory, it means that all immediate children will
58  * inherit the UID of the parent.
59  */
60 #define VFS_FFLAG_SETUID        0x08
61 /**
62  * \brief Set Group ID Flag
63  * 
64  * Allows an executable file to change it's executing group to the file's
65  * owning group.
66  * In the case of a directory, it means that all immediate children will
67  * inherit the GID of the parent.
68  */
69 #define VFS_FFLAG_SETGID        0x10
70
71 /**
72  * \brief "Don't do Write-Back" Flag
73  *
74  * Stops the VFS from calling tVFS_Node.Write on dirty pages when a region
75  * is unmapped. Nice for read-only files and memory-only files (or 
76  * pseudo-readonly filesystems)
77  */
78 #define VFS_FFLAG_NOWRITEBACK
79 /**
80  * \}
81  */
82
83 /**
84  * \brief Represents a node (file or directory) in the VFS tree
85  * 
86  * This structure provides the VFS with the functions required to read/write
87  * the file (or directory) that it represents.
88  */
89 typedef struct sVFS_Node
90 {
91         /**
92          * \name Identifiers
93          * \brief Fields used by the driver to identify what data this node
94          *        corresponds to.
95          * \{
96          */
97         Uint64  Inode;  //!< Inode ID - Must identify the node uniquely if tVFS_Driver.GetNodeFromINode is non-NULL
98         Uint    ImplInt;        //!< Implementation Usable Integer
99         void    *ImplPtr;       //!< Implementation Usable Pointer
100         /**
101          * \}
102          */
103         
104         /**
105          * \name Node State
106          * \brief Stores the misc information about the node
107          * \{
108          */
109          int    ReferenceCount; //!< Number of times the node is used
110         
111         Uint64  Size;   //!< File Size
112         
113         Uint32  Flags;  //!< File Flags
114         
115         /**
116          * \brief Pointer to cached data (FS Specific)
117          * \note The Inode_* functions will free when the node is uncached
118          *       this if needed
119          */
120         void    *Data;
121         
122         /**
123          * \brief Node mutex
124          * \note Provided for the Filesystem driver's use
125          */
126         tMutex  Lock;
127         
128         /**
129          * \}
130          */
131         
132         /**
133          * \name Times
134          * \{
135          */
136         Sint64  ATime;  //!< Last Accessed Time
137         Sint64  MTime;  //!< Last Modified Time
138         Sint64  CTime;  //!< Creation Time
139         /**
140          * \}
141          */
142         
143         /**
144          * \name Access control
145          * \{
146          */
147         tUID    UID;    //!< ID of Owning User
148         tGID    GID;    //!< ID of Owning Group
149         
150          int    NumACLs;        //!< Number of ACL entries
151         tVFS_ACL        *ACLs;  //!< Access Controll List pointer
152         /**
153          * \}
154          */
155         
156         /**
157          * \name VFS_Select() fields
158          * \note Used by the VFS internals, drivers should use VFS_Mark*
159          * \{
160          */
161          int    DataAvaliable;
162         tVFS_SelectList *ReadThreads;   //!< Threads waiting to read
163          int    BufferFull;
164         tVFS_SelectList *WriteThreads;  //!< Threads waiting to write
165          int    ErrorOccurred;
166         tVFS_SelectList *ErrorThreads;  //!< Threads waiting for an error
167         /**
168          * \}
169          */
170
171         /**
172          * \name VFS_MMap() fields
173          * \{
174          */
175         void    *MMapInfo;
176         /**
177          * \}
178          */
179         
180         /**
181          * \name Common Functions
182          * \brief Functions that are used no matter the value of .Flags
183          * \{
184          */
185         /**
186          * \brief Reference the node
187          * \param Node Pointer to this node
188          */
189         void    (*Reference)(struct sVFS_Node *Node);
190         /**
191          * \brief Close (dereference) the node
192          * \param Node  Pointer to this node
193          * 
194          * Usually .Close is used to write any changes to the node back to
195          * the persistent storage.
196          */
197         void    (*Close)(struct sVFS_Node *Node);
198         
199         /**
200          * \brief Send an IO Control
201          * \param Node  Pointer to this node
202          * \param Id    IOCtl call number
203          * \param Data  Pointer to data to pass to the driver
204          * \return Implementation defined
205          */
206          int    (*IOCtl)(struct sVFS_Node *Node, int Id, void *Data);
207         
208         /**
209          * \}
210          */
211         
212         /**
213          * \name Buffer Functions
214          * \brief Functions for accessing a buffer-type file (normal file or
215          *        symbolic link)
216          * \{
217          */
218         
219         /**
220          * \brief Read from the file
221          * \param Node  Pointer to this node
222          * \param Offset        Byte offset in the file
223          * \param Length        Number of bytes to read
224          * \param Buffer        Destination for read data
225          * \return Number of bytes read
226          */
227         Uint64  (*Read)(struct sVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
228         /**
229          * \brief Write to the file
230          * \param Node  Pointer to this node
231          * \param Offset        Byte offser in the file
232          * \param Length        Number of bytes to write
233          * \param Buffer        Source of written data
234          * \return Number of bytes read
235          */
236         Uint64  (*Write)(struct sVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
237
238         /**
239          * \brief Map a region of a file into memory
240          * \param Node  Pointer to this node
241          * \param Offset        Start of the region (page aligned)
242          * \param Length        Length of the region (page aligned)
243          * \param Dest  Location to which to map
244          * \return Boolean Failure
245          * \note If NULL, the VFS implements it using .Read
246          */
247          int    (*MMap)(struct sVFS_Node *Node, Uint64 Offset, int Length, void *Dest);
248         
249         /**
250          * \}
251          */
252         
253         /**
254          * \name Directory Functions
255          * \{
256          */
257         /**
258          * \brief Find an directory entry by name
259          * \param Node  Pointer to this node
260          * \param Name  Name of the file wanted
261          * \return Pointer to the requested node or NULL if it cannot be found
262          * \note The node returned must be accessable until ::tVFS_Node.Close
263          *       is called and ReferenceCount reaches zero.
264          */
265         struct sVFS_Node        *(*FindDir)(struct sVFS_Node *Node, const char *Name);
266         
267         /**
268          * \brief Read from a directory
269          * \param Node  Pointer to this node
270          * \param Pos   Offset in the directory
271          * \return Pointer to the name of the item on the heap (will be freed
272          *         by the caller). If the directory end has been reached, NULL
273          *         will be returned.
274          *         If an item is required to be skipped either &::NULLNode,
275          *         ::VFS_SKIP or ::VFS_SKIPN(0...1023) will be returned.
276          */
277         char    *(*ReadDir)(struct sVFS_Node *Node, int Pos);
278         
279         /**
280          * \brief Create a node in a directory
281          * \param Node  Pointer to this node
282          * \param Name  Name of the new child
283          * \param Flags Flags to apply to the new child (directory or symlink)
284          * \return Zero on Success, non-zero on error (see errno.h)
285          */
286          int    (*MkNod)(struct sVFS_Node *Node, const char *Name, Uint Flags);
287         
288         /**
289          * \brief Relink (Rename/Remove) a file/directory
290          * \param Node  Pointer to this node
291          * \param OldName       Name of the item to move/delete
292          * \param NewName       New name (or NULL if unlinking is wanted)
293          * \return Zero on Success, non-zero on error (see errno.h)
294          */
295          int    (*Relink)(struct sVFS_Node *Node, const char *OldName, const char *NewName);
296         
297         /**
298          * \brief Link a node to a name
299          * \param Node  Pointer to this node (directory)
300          * \param Child Node to create a new link to
301          * \param NewName       Name for the new link
302          * \return Zeron on success, non-zero on error (see errno.h)
303          */
304          int    (*Link)(struct sVFS_Node *Node, struct sVFS_Node *Child, const char *NewName);
305          
306          /**
307           * \}
308           */
309 } tVFS_Node;
310
311 /**
312  * \brief VFS Driver (Filesystem) Definition
313  */
314 typedef struct sVFS_Driver
315 {
316         /**
317          * \brief Unique Identifier for this filesystem type
318          */
319         const char      *Name;
320         /**
321          * \brief Flags applying to this driver
322          */
323         Uint    Flags;
324         
325         /**
326          * \brief Callback to mount a device
327          */
328         tVFS_Node       *(*InitDevice)(const char *Device, const char **Options);
329         /**
330          * \brief Callback to unmount a device
331          */
332         void    (*Unmount)(tVFS_Node *Node);
333         /**
334          * \brief Retrieve a VFS node from an inode
335          */
336         tVFS_Node       *(*GetNodeFromINode)(tVFS_Node *RootNode, Uint64 InodeValue);
337         /**
338          * \brief Used internally (next driver in the chain)
339          */
340         struct sVFS_Driver      *Next;
341 } tVFS_Driver;
342
343 // === GLOBALS ===
344 //! \brief Maximum number of elements that can be skipped in one return
345 #define VFS_MAXSKIP     ((void*)1024)
346 //! \brief Skip a single entry in readdir
347 #define VFS_SKIP        ((void*)1)
348 //! \brief Skip \a n entries in readdir
349 #define VFS_SKIPN(n)    ((void*)(n))
350
351 extern tVFS_Node        NULLNode;       //!< NULL VFS Node (Ignored/Skipped)
352 /**
353  * \name Static ACLs
354  * \brief Simple ACLs to aid writing drivers
355  * \{
356  */
357 extern tVFS_ACL gVFS_ACL_EveryoneRWX;   //!< Everyone Read/Write/Execute
358 extern tVFS_ACL gVFS_ACL_EveryoneRW;    //!< Everyone Read/Write
359 extern tVFS_ACL gVFS_ACL_EveryoneRX;    //!< Everyone Read/Execute
360 extern tVFS_ACL gVFS_ACL_EveryoneRO;    //!< Everyone Read only
361 /**
362  * \}
363  */
364
365 // === FUNCTIONS ===
366 /**
367  * \fn int VFS_AddDriver(tVFS_Driver *Info)
368  * \brief Registers the driver with the DevFS layer
369  * \param Info  Driver information structure
370  */
371 extern int      VFS_AddDriver(tVFS_Driver *Info);
372 /**
373  * \fn tVFS_Driver *VFS_GetFSByName(char *Name)
374  * \brief Get the information structure of a driver given its name
375  * \param Name  Name of filesystem driver to find
376  */
377 extern tVFS_Driver      *VFS_GetFSByName(const char *Name);
378
379
380 /**
381  * \brief Prepare a node for use
382  */
383 extern void     VFS_InitNode(tVFS_Node *Node);
384
385 /**
386  * \brief Clean up a node, ready for deletion
387  * \note This should ALWAYS be called before a node is freed, as it
388  *       cleans up VFS internal structures.
389  */
390 extern void     VFS_CleanupNode(tVFS_Node *Node);
391
392 /**
393  * \fn tVFS_ACL *VFS_UnixToAcessACL(Uint Mode, Uint Owner, Uint Group)
394  * \brief Transforms Unix Permssions into Acess ACLs
395  * \param Mode  Unix RWXrwxRWX mask
396  * \param Owner UID of the file's owner
397  * \param Group GID of the file's owning group
398  * \return An array of 3 Acess ACLs
399  */
400 extern tVFS_ACL *VFS_UnixToAcessACL(Uint Mode, Uint Owner, Uint Group);
401
402 /**
403  * \brief Flags fro \a TypeFlag of VFS_SelectNode
404  * \{
405  */
406 #define VFS_SELECT_READ 0x01
407 #define VFS_SELECT_WRITE        0x02
408 #define VFS_SELECT_ERROR        0x04
409 /**
410  * \}
411  */
412
413 /**
414  * \brief Wait for an event on a node
415  * \param Node  Node to wait on
416  * \param Type  Type of wait
417  * \param Timeout       Time to wait (NULL for infinite wait)
418  * \param Name  Name to show in debug output
419  * \return Number of nodes that actioned (0 or 1)
420  */
421 extern int      VFS_SelectNode(tVFS_Node *Node, int Type, tTime *Timeout, const char *Name);
422
423 /**
424  * \brief Change the full flag on a node
425  */
426 extern int      VFS_MarkFull(tVFS_Node *Node, BOOL IsBufferFull);
427 extern int      VFS_MarkAvaliable(tVFS_Node *Node, BOOL IsDataAvaliable);
428 extern int      VFS_MarkError(tVFS_Node *Node, BOOL IsErrorState);
429
430 // --- Node Cache --
431 /**
432  * \name Node Cache
433  * \brief Functions to allow a node to be cached in memory by the VFS
434  * 
435  * These functions store a node for the driver, to prevent it from having
436  * to re-generate the node on each call to FindDir. It also allows for
437  * fast cleanup when a filesystem is unmounted.
438  * \{
439  */
440 /**
441  * \fn int Inode_GetHandle(void)
442  * \brief Gets a unique handle to the Node Cache
443  * \return A unique handle for use for the rest of the Inode_* functions
444  */
445 extern int      Inode_GetHandle(void);
446 /**
447  * \fn tVFS_Node *Inode_GetCache(int Handle, Uint64 Inode)
448  * \brief Gets an inode from the node cache
449  * \param Handle        A handle returned by Inode_GetHandle()
450  * \param Inode Value of the Inode field of the ::tVFS_Node you want
451  * \return A pointer to the cached node
452  */
453 extern tVFS_Node        *Inode_GetCache(int Handle, Uint64 Inode);
454 /**
455  * \fn tVFS_Node *Inode_CacheNode(int Handle, tVFS_Node *Node)
456  * \brief Caches a node in the Node Cache
457  * \param Handle        A handle returned by Inode_GetHandle()
458  * \param Node  A pointer to the node to be cached (a copy is taken)
459  * \return A pointer to the node in the node cache
460  */
461 extern tVFS_Node        *Inode_CacheNode(int Handle, tVFS_Node *Node);
462 /**
463  * \fn int Inode_UncacheNode(int Handle, Uint64 Inode)
464  * \brief Dereferences (and removes if needed) a node from the cache
465  * \param Handle        A handle returned by Inode_GetHandle()
466  * \param Inode Value of the Inode field of the ::tVFS_Node you want to remove
467  */
468 extern void     Inode_UncacheNode(int Handle, Uint64 Inode);
469 /**
470  * \fn void Inode_ClearCache(int Handle)
471  * \brief Clears the cache for a handle
472  * \param Handle        A handle returned by Inode_GetHandle()
473  */
474 extern void     Inode_ClearCache(int Handle);
475
476 /**
477  * \}
478  */
479
480 #endif

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