f13f1b693888b865ae522249227b40e78f672f94
[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  */
73
74 /**
75  * \brief Represents a node (file or directory) in the VFS tree
76  * 
77  * This structure provides the VFS with the functions required to read/write
78  * the file (or directory) that it represents.
79  */
80 typedef struct sVFS_Node
81 {
82         /**
83          * \name Identifiers
84          * \brief Fields used by the driver to identify what data this node
85          *        corresponds to.
86          * \{
87          */
88         Uint64  Inode;  //!< Inode ID (Essentially another ImplInt)
89         Uint    ImplInt;        //!< Implementation Usable Integer
90         void    *ImplPtr;       //!< Implementation Usable Pointer
91         /**
92          * \}
93          */
94         
95         /**
96          * \name Node State
97          * \brief Stores the misc information about the node
98          * \{
99          */
100          int    ReferenceCount; //!< Number of times the node is used
101         
102         Uint64  Size;   //!< File Size
103         
104         Uint32  Flags;  //!< File Flags
105         
106         /**
107          * \brief Pointer to cached data (FS Specific)
108          * \note The Inode_* functions will free when the node is uncached
109          *       this if needed
110          */
111         void    *Data;
112         
113         /**
114          * \brief Node mutex
115          * \note Provided for the Filesystem driver's use
116          */
117         tMutex  Lock;
118         
119         /**
120          * \}
121          */
122         
123         /**
124          * \name Times
125          * \{
126          */
127         Sint64  ATime;  //!< Last Accessed Time
128         Sint64  MTime;  //!< Last Modified Time
129         Sint64  CTime;  //!< Creation Time
130         /**
131          * \}
132          */
133         
134         /**
135          * \name Access control
136          * \{
137          */
138         tUID    UID;    //!< ID of Owning User
139         tGID    GID;    //!< ID of Owning Group
140         
141          int    NumACLs;        //!< Number of ACL entries
142         tVFS_ACL        *ACLs;  //!< Access Controll List pointer
143         /**
144          * \}
145          */
146         
147         /**
148          * \name VFS_Select() fields
149          * \{
150          */
151          int    DataAvaliable;
152         tVFS_SelectList *ReadThreads;   //!< Threads waiting to read
153          int    BufferFull;
154         tVFS_SelectList *WriteThreads;  //!< Threads waiting to write
155          int    ErrorOccurred;
156         tVFS_SelectList *ErrorThreads;  //!< Threads waiting for an error
157         /**
158          * \}
159          */
160         
161         /**
162          * \name Common Functions
163          * \brief Functions that are used no matter the value of .Flags
164          * \{
165          */
166         /**
167          * \brief Reference the node
168          * \param Node Pointer to this node
169          */
170         void    (*Reference)(struct sVFS_Node *Node);
171         /**
172          * \brief Close (dereference) the node
173          * \param Node  Pointer to this node
174          * 
175          * Usually .Close is used to write any changes to the node back to
176          * the persistent storage.
177          */
178         void    (*Close)(struct sVFS_Node *Node);
179         
180         /**
181          * \brief Send an IO Control
182          * \param Node  Pointer to this node
183          * \param Id    IOCtl call number
184          * \param Data  Pointer to data to pass to the driver
185          * \return Implementation defined
186          */
187          int    (*IOCtl)(struct sVFS_Node *Node, int Id, void *Data);
188         
189         /**
190          * \}
191          */
192         
193         /**
194          * \name Buffer Functions
195          * \brief Functions for accessing a buffer-type file (normal file or
196          *        symbolic link)
197          * \{
198          */
199         
200         /**
201          * \brief Read from the file
202          * \param Node  Pointer to this node
203          * \param Offset        Byte offset in the file
204          * \param Length        Number of bytes to read
205          * \param Buffer        Destination for read data
206          * \return Number of bytes read
207          */
208         Uint64  (*Read)(struct sVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
209         /**
210          * \brief Write to the file
211          * \param Node  Pointer to this node
212          * \param Offset        Byte offser in the file
213          * \param Length        Number of bytes to write
214          * \param Buffer        Source of written data
215          * \return Number of bytes read
216          */
217         Uint64  (*Write)(struct sVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
218         
219         /**
220          * \}
221          */
222         
223         /**
224          * \name Directory Functions
225          * \{
226          */
227         /**
228          * \brief Find an directory entry by name
229          * \param Node  Pointer to this node
230          * \param Name  Name of the file wanted
231          * \return Pointer to the requested node or NULL if it cannot be found
232          * \note The node returned must be accessable until ::tVFS_Node.Close
233          *       is called and ReferenceCount reaches zero.
234          */
235         struct sVFS_Node        *(*FindDir)(struct sVFS_Node *Node, const char *Name);
236         
237         /**
238          * \brief Read from a directory
239          * \param Node  Pointer to this node
240          * \param Pos   Offset in the directory
241          * \return Pointer to the name of the item on the heap (will be freed
242          *         by the caller). If the directory end has been reached, NULL
243          *         will be returned.
244          *         If an item is required to be skipped either &::NULLNode,
245          *         ::VFS_SKIP or ::VFS_SKIPN(0...1023) will be returned.
246          */
247         char    *(*ReadDir)(struct sVFS_Node *Node, int Pos);
248         
249         /**
250          * \brief Create a node in a directory
251          * \param Node  Pointer to this node
252          * \param Name  Name of the new child
253          * \param Flags Flags to apply to the new child (directory or symlink)
254          * \return Zero on Success, non-zero on error (see errno.h)
255          */
256          int    (*MkNod)(struct sVFS_Node *Node, const char *Name, Uint Flags);
257         
258         /**
259          * \brief Relink (Rename/Remove) a file/directory
260          * \param Node  Pointer to this node
261          * \param OldName       Name of the item to move/delete
262          * \param NewName       New name (or NULL if unlinking is wanted)
263          * \return Zero on Success, non-zero on error (see errno.h)
264          */
265          int    (*Relink)(struct sVFS_Node *Node, const char *OldName, const char *NewName);
266         
267         /**
268          * \brief Link a node to a name
269          * \param Node  Pointer to this node (directory)
270          * \param Child Node to create a new link to
271          * \param NewName       Name for the new link
272          * \return Zeron on success, non-zero on error (see errno.h)
273          */
274          int    (*Link)(struct sVFS_Node *Node, struct sVFS_Node *Child, const char *NewName);
275          
276          /**
277           * \}
278           */
279 } tVFS_Node;
280
281 /**
282  * \brief VFS Driver (Filesystem) Definition
283  */
284 typedef struct sVFS_Driver
285 {
286         //! \brief Unique Identifier for this filesystem type
287         const char      *Name;
288         //! \brief Flags applying to this driver
289         Uint    Flags;
290         
291         //! \brief Callback to mount a device
292         tVFS_Node       *(*InitDevice)(const char *Device, const char **Options);
293         //! \brief Callback to unmount a device
294         void    (*Unmount)(tVFS_Node *Node);
295         //! \brief Used internally (next driver in the chain)
296         struct sVFS_Driver      *Next;
297 } tVFS_Driver;
298
299 // === GLOBALS ===
300 //! \brief Maximum number of elements that can be skipped in one return
301 #define VFS_MAXSKIP     ((void*)1024)
302 //! \brief Skip a single entry in readdir
303 #define VFS_SKIP        ((void*)1)
304 //! \brief Skip \a n entries in readdir
305 #define VFS_SKIPN(n)    ((void*)(n))
306
307 extern tVFS_Node        NULLNode;       //!< NULL VFS Node (Ignored/Skipped)
308 /**
309  * \name Static ACLs
310  * \brief Simple ACLs to aid writing drivers
311  * \{
312  */
313 extern tVFS_ACL gVFS_ACL_EveryoneRWX;   //!< Everyone Read/Write/Execute
314 extern tVFS_ACL gVFS_ACL_EveryoneRW;    //!< Everyone Read/Write
315 extern tVFS_ACL gVFS_ACL_EveryoneRX;    //!< Everyone Read/Execute
316 extern tVFS_ACL gVFS_ACL_EveryoneRO;    //!< Everyone Read only
317 /**
318  * \}
319  */
320
321 // === FUNCTIONS ===
322 /**
323  * \fn int VFS_AddDriver(tVFS_Driver *Info)
324  * \brief Registers the driver with the DevFS layer
325  * \param Info  Driver information structure
326  */
327 extern int      VFS_AddDriver(tVFS_Driver *Info);
328 /**
329  * \fn tVFS_Driver *VFS_GetFSByName(char *Name)
330  * \brief Get the information structure of a driver given its name
331  * \param Name  Name of filesystem driver to find
332  */
333 extern tVFS_Driver      *VFS_GetFSByName(const char *Name);
334
335
336 /**
337  * \brief Prepare a node for use
338  */
339 extern void     VFS_InitNode(tVFS_Node *Node);
340
341 /**
342  * \brief Clean up a node, ready for deletion
343  * \note This should ALWAYS be called before a node is freed, as it
344  *       cleans up VFS internal structures.
345  */
346 extern void     VFS_CleanupNode(tVFS_Node *Node);
347
348 /**
349  * \fn tVFS_ACL *VFS_UnixToAcessACL(Uint Mode, Uint Owner, Uint Group)
350  * \brief Transforms Unix Permssions into Acess ACLs
351  * \param Mode  Unix RWXrwxRWX mask
352  * \param Owner UID of the file's owner
353  * \param Group GID of the file's owning group
354  * \return An array of 3 Acess ACLs
355  */
356 extern tVFS_ACL *VFS_UnixToAcessACL(Uint Mode, Uint Owner, Uint Group);
357
358 /**
359  * \brief Flags fro \a TypeFlag of VFS_SelectNode
360  * \{
361  */
362 #define VFS_SELECT_READ 0x01
363 #define VFS_SELECT_WRITE        0x02
364 #define VFS_SELECT_ERROR        0x04
365 /**
366  * \}
367  */
368
369 /**
370  * \brief Wait for an event on a node
371  * \param Node  Node to wait on
372  * \param Type  Type of wait
373  * \param Timeout       Time to wait (NULL for infinite wait)
374  * \param Name  Name to show in debug output
375  * \return Number of nodes that actioned (0 or 1)
376  */
377 extern int      VFS_SelectNode(tVFS_Node *Node, int Type, tTime *Timeout, const char *Name);
378
379 /**
380  * \brief Change the full flag on a node
381  */
382 extern int      VFS_MarkFull(tVFS_Node *Node, BOOL IsBufferFull);
383 extern int      VFS_MarkAvaliable(tVFS_Node *Node, BOOL IsDataAvaliable);
384 extern int      VFS_MarkError(tVFS_Node *Node, BOOL IsErrorState);
385
386 // --- Node Cache --
387 /**
388  * \name Node Cache
389  * \brief Functions to allow a node to be cached in memory by the VFS
390  * 
391  * These functions store a node for the driver, to prevent it from having
392  * to re-generate the node on each call to FindDir. It also allows for
393  * fast cleanup when a filesystem is unmounted.
394  * \{
395  */
396 /**
397  * \fn int Inode_GetHandle(void)
398  * \brief Gets a unique handle to the Node Cache
399  * \return A unique handle for use for the rest of the Inode_* functions
400  */
401 extern int      Inode_GetHandle(void);
402 /**
403  * \fn tVFS_Node *Inode_GetCache(int Handle, Uint64 Inode)
404  * \brief Gets an inode from the node cache
405  * \param Handle        A handle returned by Inode_GetHandle()
406  * \param Inode Value of the Inode field of the ::tVFS_Node you want
407  * \return A pointer to the cached node
408  */
409 extern tVFS_Node        *Inode_GetCache(int Handle, Uint64 Inode);
410 /**
411  * \fn tVFS_Node *Inode_CacheNode(int Handle, tVFS_Node *Node)
412  * \brief Caches a node in the Node Cache
413  * \param Handle        A handle returned by Inode_GetHandle()
414  * \param Node  A pointer to the node to be cached (a copy is taken)
415  * \return A pointer to the node in the node cache
416  */
417 extern tVFS_Node        *Inode_CacheNode(int Handle, tVFS_Node *Node);
418 /**
419  * \fn int Inode_UncacheNode(int Handle, Uint64 Inode)
420  * \brief Dereferences (and removes if needed) a node from the cache
421  * \param Handle        A handle returned by Inode_GetHandle()
422  * \param Inode Value of the Inode field of the ::tVFS_Node you want to remove
423  */
424 extern void     Inode_UncacheNode(int Handle, Uint64 Inode);
425 /**
426  * \fn void Inode_ClearCache(int Handle)
427  * \brief Clears the cache for a handle
428  * \param Handle        A handle returned by Inode_GetHandle()
429  */
430 extern void     Inode_ClearCache(int Handle);
431
432 /**
433  * \}
434  */
435
436 #endif

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