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

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