9 #define DEFAULT_RING_SIZE 2048
13 typedef struct sPipe {
25 int FIFO_Install(char **Arguments);
26 int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data);
27 char *FIFO_ReadDir(tVFS_Node *Node, int Id);
28 tVFS_Node *FIFO_FindDir(tVFS_Node *Node, char *Filename);
29 int FIFO_MkNod(tVFS_Node *Node, char *Name, Uint Flags);
30 void FIFO_Close(tVFS_Node *Node);
31 int FIFO_Relink(tVFS_Node *Node, char *OldName, char *NewName);
32 Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
33 Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
34 tPipe *FIFO_Int_NewPipe(int Size, char *Name);
37 MODULE_DEFINE(0, 0x0032, FIFO, FIFO_Install, NULL, NULL);
38 tDevFS_Driver gFIFO_DriverInfo = {
42 .ACLs = &gVFS_ACL_EveryoneRW,
43 .Flags = VFS_FFLAG_DIRECTORY,
44 .ReadDir = FIFO_ReadDir,
45 .FindDir = FIFO_FindDir,
47 .Relink = FIFO_Relink,
51 tVFS_Node gFIFO_AnonNode = {
53 .ACLs = &gVFS_ACL_EveryoneRW,
55 tPipe *gFIFO_NamedPipes = NULL;
59 * \fn int FIFO_Install(char **Options)
60 * \brief Installs the FIFO Driver
62 int FIFO_Install(char **Options)
64 DevFS_AddDevice( &gFIFO_DriverInfo );
65 return MODULE_INIT_SUCCESS;
69 * \fn int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data)
71 int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data)
77 * \fn char *FIFO_ReadDir(tVFS_Node *Node, int Id)
78 * \brief Reads from the FIFO root
80 char *FIFO_ReadDir(tVFS_Node *Node, int Id)
82 tPipe *tmp = gFIFO_NamedPipes;
83 // Entry 0 is Anon Pipes
84 if(Id == 0) return strdup("anon");
86 // Find the id'th node
87 while(--Id && tmp) tmp = tmp->Next;
88 // If node found, return it
89 if(tmp) return strdup(tmp->Name);
95 * \fn tVFS_Node *FIFO_FindDir(tVFS_Node *Node, char *Filename)
96 * \brief Find a file in the FIFO root
97 * \note Creates an anon pipe if anon is requested
99 tVFS_Node *FIFO_FindDir(tVFS_Node *Node, char *Filename)
102 if(!Filename) return NULL;
105 if(Filename[0] == '\0') return NULL;
108 if(Filename[0] == 'a' && Filename[1] == 'n'
109 && Filename[2] == 'o' && Filename[3] == 'n'
110 && Filename[4] == '\0') {
111 tmp = FIFO_Int_NewPipe(DEFAULT_RING_SIZE, "anon");
116 tmp = gFIFO_NamedPipes;
119 if(strcmp(tmp->Name, Filename) == 0)
127 * \fn int FIFO_MkNod(tVFS_Node *Node, char *Name, Uint Flags)
129 int FIFO_MkNod(tVFS_Node *Node, char *Name, Uint Flags)
135 * \fn void FIFO_Close(vfs_node *Node)
137 void FIFO_Close(tVFS_Node *Node)
140 if(!Node->ImplPtr) return ;
142 Node->ReferenceCount --;
143 if(Node->ReferenceCount) return ;
145 pipe = Node->ImplPtr;
147 if(strcmp(pipe->Name, "anon") == 0) {
156 * \fn int FIFO_Relink(tVFS_Node *Node, char *OldName, char *NewName)
157 * \brief Relink a file (Deletes named pipes)
159 int FIFO_Relink(tVFS_Node *Node, char *OldName, char *NewName)
163 if(Node != &gFIFO_DriverInfo.RootNode) return 0;
166 if(strcmp(OldName, "anon")) return 0;
169 for(pipe = gFIFO_NamedPipes;
173 if(strcmp(pipe->Name, OldName) == 0)
178 // Relink a named pipe
181 for(tmp = gFIFO_NamedPipes;
185 if(strcmp(tmp->Name, NewName) == 0) return 0;
189 pipe->Name = malloc(strlen(NewName)+1);
190 strcpy(pipe->Name, NewName);
204 * \fn Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
205 * \brief Read from a fifo pipe
207 Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
209 tPipe *pipe = Node->ImplPtr;
211 Uint remaining = Length;
217 // Wait for buffer to fill
218 if(pipe->Flags & PF_BLOCKING)
219 while(pipe->ReadPos == pipe->WritePos)
222 if(pipe->ReadPos == pipe->WritePos)
226 if(pipe->WritePos - pipe->ReadPos < remaining)
227 len = pipe->WritePos - pipe->ReadPos;
231 // Check if read overflows buffer
232 if(len > pipe->BufSize - pipe->ReadPos)
234 int ofs = pipe->BufSize - pipe->ReadPos;
235 memcpy(Buffer, &pipe->Buffer[pipe->ReadPos], ofs);
236 memcpy(Buffer + ofs, &pipe->Buffer, len-ofs);
240 memcpy(Buffer, &pipe->Buffer[pipe->ReadPos], len);
243 // Increment read position
244 pipe->ReadPos += len;
245 pipe->ReadPos %= pipe->BufSize;
247 // Decrement Remaining Bytes
249 // Increment Buffer address
258 * \fn Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
259 * \brief Write to a fifo pipe
261 Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
263 tPipe *pipe = Node->ImplPtr;
265 Uint remaining = Length;
271 // Wait for buffer to empty
272 if(pipe->Flags & PF_BLOCKING)
273 while(pipe->ReadPos == (pipe->WritePos+1)%pipe->BufSize)
276 if(pipe->ReadPos == (pipe->WritePos+1)%pipe->BufSize)
280 if(pipe->ReadPos - pipe->WritePos < remaining)
281 len = pipe->ReadPos - pipe->WritePos;
285 // Check if write overflows buffer
286 if(len > pipe->BufSize - pipe->WritePos)
288 int ofs = pipe->BufSize - pipe->WritePos;
289 memcpy(&pipe->Buffer[pipe->WritePos], Buffer, ofs);
290 memcpy(&pipe->Buffer, Buffer + ofs, len-ofs);
294 memcpy(&pipe->Buffer[pipe->WritePos], Buffer, len);
297 // Increment read position
298 pipe->WritePos += len;
299 pipe->WritePos %= pipe->BufSize;
301 // Decrement Remaining Bytes
303 // Increment Buffer address
312 * \fn tPipe *FIFO_Int_NewPipe(int Size, char *Name)
313 * \brief Create a new pipe
315 tPipe *FIFO_Int_NewPipe(int Size, char *Name)
318 int allocsize = sizeof(tPipe) + sizeof(tVFS_ACL) + Size;
320 ret = malloc(allocsize);
321 if(!ret) return NULL;
324 memset(ret, 0, allocsize);
330 ret->Buffer = (void*)( (Uint)ret + sizeof(tPipe) + sizeof(tVFS_ACL) );
338 ret->Node.ImplPtr = ret;
339 ret->Node.UID = Threads_GetUID();
340 ret->Node.GID = Threads_GetGID();
341 ret->Node.NumACLs = 1;
342 ret->Node.ACLs = (void*)( (Uint)ret + sizeof(tPipe) );
343 ret->Node.ACLs->Group = 0;
344 ret->Node.ACLs->ID = ret->Node.UID;
345 ret->Node.ACLs->Inv = 0;
346 ret->Node.ACLs->Perms = -1;
349 = ret->Node.ATime = now();
350 ret->Node.Read = FIFO_Read;
351 ret->Node.Write = FIFO_Write;
352 ret->Node.Close = FIFO_Close;