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

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