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

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