More work on the FAT driver, more messy, but more complete
[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  * \name tVFS_Node Flags
26  * \brief Flag values for tVFS_Node.Flags
27  * \{
28  */
29 //! \todo Is this still needed
30 #define VFS_FFLAG_READONLY      0x01    //!< Readonly File
31 /**
32  * \brief Directory Flag
33  * 
34  * This flag marks the tVFS_Node as describing a directory, and says
35  * that the tVFS_Node.FindDir, tVFS_Node.ReadDir, tVFS_Node.MkNod and
36  * tVFS_Node.Relink function pointers are valid.
37  * For a directory the tVFS_Node.Size field contains the number of files
38  * within the directory, or -1 for undetermined.
39  */
40 #define VFS_FFLAG_DIRECTORY     0x02
41 /**
42  * \brief Symbolic Link Flag
43  * 
44  * Marks a file as a symbolic link
45  */
46 #define VFS_FFLAG_SYMLINK       0x04
47 /**
48  * \brief Set User ID Flag
49  * 
50  * Allows an executable file to change it's executing user to the file's
51  * owner.
52  * In the case of a directory, it means that all immediate children will
53  * inherit the UID of the parent.
54  */
55 #define VFS_FFLAG_SETUID        0x08
56 /**
57  * \brief Set Group ID Flag
58  * 
59  * Allows an executable file to change it's executing group to the file's
60  * owning group.
61  * In the case of a directory, it means that all immediate children will
62  * inherit the GID of the parent.
63  */
64 #define VFS_FFLAG_SETGID        0x10
65 /**
66  * \}
67  */
68
69 /**
70  * \brief VFS Node
71  * 
72  * This structure provides the VFS with the functions required to read/write
73  * the file (or directory) that it represents.
74  */
75 typedef struct sVFS_Node
76 {
77         /**
78          * \name Identifiers
79          * \brief Fields used by the driver to identify what data this node
80          *        corresponds to.
81          * \{
82          */
83         Uint64  Inode;  //!< Inode ID (Essentially another ImplInt)
84         Uint    ImplInt;        //!< Implementation Usable Integer
85         void    *ImplPtr;       //!< Implementation Usable Pointer
86         /**
87          * \}
88          */
89         
90         /**
91          * \name Node State
92          * \brief Stores the misc information about the node
93          * \{
94          */
95          int    ReferenceCount; //!< Number of times the node is used
96         
97         Uint64  Size;   //!< File Size
98         
99         Uint32  Flags;  //!< File Flags
100         /**
101          * \}
102          */
103         
104         /**
105          * \name Times
106          * \{
107          */
108         Sint64  ATime;  //!< Last Accessed Time
109         Sint64  MTime;  //!< Last Modified Time
110         Sint64  CTime;  //!< Creation Time
111         /**
112          * \}
113          */
114         
115         /**
116          * \name Access controll
117          * \{
118          */
119         tUID    UID;    //!< ID of Owning User
120         tGID    GID;    //!< ID of Owning Group
121         
122          int    NumACLs;        //!< Number of ACL entries
123         tVFS_ACL        *ACLs;  //!< Access Controll List pointer
124         /**
125          * \}
126          */
127         
128         /**
129          * \name Common Functions
130          * \brief Functions that are used no matter the value of .Flags
131          * \{
132          */
133         /**
134          * \brief Reference the node
135          * \param Node Pointer to this node
136          */
137         void    (*Reference)(struct sVFS_Node *Node);
138         /**
139          * \brief Close (dereference) the node
140          * \param Node  Pointer to this node
141          * 
142          * Usually .Close is used to write any changes to the node back to
143          * the persistent storage.
144          */
145         void    (*Close)(struct sVFS_Node *Node);
146         
147         /**
148          * \brief Send an IO Control
149          * \param Node  Pointer to this node
150          * \param Id    IOCtl call number
151          * \param Data  Pointer to data to pass to the driver
152          * \return Implementation defined
153          */
154          int    (*IOCtl)(struct sVFS_Node *Node, int Id, void *Data);
155         
156         /**
157          * }
158          */
159         
160         /**
161          * \name Buffer Functions
162          * \brief Functions for accessing a buffer-type file (normal file or
163          *        symbolic link)
164          * \{
165          */
166         
167         /**
168          * \brief Read from the file
169          * \param Node  Pointer to this node
170          * \param Offset        Byte offset in the file
171          * \param Length        Number of bytes to read
172          * \param Buffer        Destination for read data
173          * \return Number of bytes read
174          */
175         Uint64  (*Read)(struct sVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
176         /**
177          * \brief Write to the file
178          * \param Node  Pointer to this node
179          * \param Offset        Byte offser in the file
180          * \param Length        Number of bytes to write
181          * \param Buffer        Source of written data
182          * \return Number of bytes read
183          */
184         Uint64  (*Write)(struct sVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
185         
186         /**
187          * }
188          */
189         
190         /**
191          * \name Directory Functions
192          * \{
193          */
194         /**
195          * \brief Find an directory entry by name
196          * \param Node  Pointer to this node
197          * \param Name  Name of the file wanted
198          * \return Pointer to the requested node or NULL if it cannot be found
199          * \note The node returned must be accessable until ::tVFS_Node.Close
200          *       is called and ReferenceCount reaches zero.
201          */
202         struct sVFS_Node        *(*FindDir)(struct sVFS_Node *Node, char *Name);
203         
204         /**
205          * \brief Read from a directory
206          * \param Node  Pointer to this node
207          * \param Pos   Offset in the directory
208          * \return Pointer to the name of the item on the heap (will be freed
209          *         by the caller). If the directory end has been reached, NULL
210          *         will be returned.
211          *         If an item is required to be skipped either &::NULLNode,
212          *         ::VFS_SKIP or ::VFS_SKIPN(0...1023) will be returned.
213          */
214         char    *(*ReadDir)(struct sVFS_Node *Node, int Pos);
215         
216         /**
217          * \brief Create a node in a directory
218          * \param Node  Pointer to this node
219          * \param Name  Name of the new child
220          * \param Flags Flags to apply to the new child (directory or symlink)
221          * \return Boolean success
222          */
223          int    (*MkNod)(struct sVFS_Node *Node, char *Name, Uint Flags);
224         
225         /**
226          * \brief Relink (Rename/Remove) a file/directory
227          * \param Node  Pointer to this node
228          * \param OldName       Name of the item to move/delete
229          * \param NewName       New name (or NULL if unlinking is wanted)
230          * \return Zero on Success, non-zero on error (see errno.h)
231          */
232          int    (*Relink)(struct sVFS_Node *Node, char *OldName, char *NewName);
233          
234          /**
235           * }
236           */
237 } tVFS_Node;
238
239 /**
240  * \brief VFS Driver (Filesystem) Definition
241  */
242 typedef struct sVFS_Driver
243 {
244         //! \brief Unique Identifier for this filesystem type
245         char    *Name;
246         //! \brief Flags applying to this driver
247         Uint    Flags;
248         
249         //! \brief Callback to mount a device
250         tVFS_Node       *(*InitDevice)(char *Device, char **Options);
251         //! \brief Callback to unmount a device
252         void    (*Unmount)(tVFS_Node *Node);
253         //! \brief Used internally (next driver in the chain)
254         struct sVFS_Driver      *Next;
255 } tVFS_Driver;
256
257 // === GLOBALS ===
258 //! \brief Maximum number of elements that can be skipped in one return
259 #define VFS_MAXSKIP     ((void*)1024)
260 //! \brief Skip a single entry in readdir
261 #define VFS_SKIP        ((void*)1)
262 //! \brief Skip \a n entries in readdir
263 #define VFS_SKIPN(n)    ((void*)(n))
264
265 extern tVFS_Node        NULLNode;       //!< NULL VFS Node (Ignored/Skipped)
266 /**
267  * \name Static ACLs
268  * \brief Simple ACLs to aid writing drivers
269  * \{
270  */
271 extern tVFS_ACL gVFS_ACL_EveryoneRWX;   //!< Everyone Read/Write/Execute
272 extern tVFS_ACL gVFS_ACL_EveryoneRW;    //!< Everyone Read/Write
273 extern tVFS_ACL gVFS_ACL_EveryoneRX;    //!< Everyone Read/Execute
274 extern tVFS_ACL gVFS_ACL_EveryoneRO;    //!< Everyone Read only
275 /**
276  * \}
277  */
278
279 // === FUNCTIONS ===
280 /**
281  * \fn int VFS_AddDriver(tVFS_Driver *Info)
282  * \brief Registers the driver with the DevFS layer
283  * \param Info  Driver information structure
284  */
285 extern int      VFS_AddDriver(tVFS_Driver *Info);
286 /**
287  * \fn tVFS_Driver *VFS_GetFSByName(char *Name)
288  * \brief Get the information structure of a driver given its name
289  * \param Name  Name of filesystem driver to find
290  */
291 extern tVFS_Driver      *VFS_GetFSByName(char *Name);
292 /**
293  * \fn tVFS_ACL *VFS_UnixToAcessACL(Uint Mode, Uint Owner, Uint Group)
294  * \brief Transforms Unix Permssions into Acess ACLs
295  * \param Mode  Unix RWXrwxRWX mask
296  * \param Owner UID of the file's owner
297  * \param Group GID of the file's owning group
298  * \return An array of 3 Acess ACLs
299  */
300 extern tVFS_ACL *VFS_UnixToAcessACL(Uint Mode, Uint Owner, Uint Group);
301
302 // --- Node Cache --
303 /**
304  * \name Node Cache
305  * \brief Functions to allow a node to be cached in memory by the VFS
306  * 
307  * These functions store a node for the driver, to prevent it from having
308  * to re-generate the node on each call to FindDir. It also allows for
309  * fast cleanup when a filesystem is unmounted.
310  * \{
311  */
312 /**
313  * \fn int Inode_GetHandle()
314  * \brief Gets a unique handle to the Node Cache
315  * \return A unique handle for use for the rest of the Inode_* functions
316  */
317 extern int      Inode_GetHandle();
318 /**
319  * \fn tVFS_Node *Inode_GetCache(int Handle, Uint64 Inode)
320  * \brief Gets an inode from the node cache
321  * \param Handle        A handle returned by Inode_GetHandle()
322  * \param Inode Value of the Inode field of the ::tVFS_Node you want
323  * \return A pointer to the cached node
324  */
325 extern tVFS_Node        *Inode_GetCache(int Handle, Uint64 Inode);
326 /**
327  * \fn tVFS_Node *Inode_CacheNode(int Handle, tVFS_Node *Node)
328  * \brief Caches a node in the Node Cache
329  * \param Handle        A handle returned by Inode_GetHandle()
330  * \param Node  A pointer to the node to be cached (a copy is taken)
331  * \return A pointer to the node in the node cache
332  */
333 extern tVFS_Node        *Inode_CacheNode(int Handle, tVFS_Node *Node);
334 /**
335  * \fn int Inode_UncacheNode(int Handle, Uint64 Inode)
336  * \brief Dereferences (and removes if needed) a node from the cache
337  * \param Handle        A handle returned by Inode_GetHandle()
338  * \param Inode Value of the Inode field of the ::tVFS_Node you want to remove
339  */
340 extern void     Inode_UncacheNode(int Handle, Uint64 Inode);
341 /**
342  * \fn void Inode_ClearCache(int Handle)
343  * \brief Clears the cache for a handle
344  * \param Handle        A handle returned by Inode_GetHandle()
345  */
346 extern void     Inode_ClearCache(int Handle);
347
348 /**
349  * \}
350  */
351
352 #endif

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