10 #define DEFAULT_RING_SIZE 2048
14 typedef struct sPipe {
26 int FIFO_Install(char **Arguments);
27 int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data);
28 char *FIFO_ReadDir(tVFS_Node *Node, int Id);
29 tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename);
30 int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
31 void FIFO_Close(tVFS_Node *Node);
32 int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName);
33 Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
34 Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
35 tPipe *FIFO_Int_NewPipe(int Size, const char *Name);
38 MODULE_DEFINE(0, 0x0032, FIFO, FIFO_Install, NULL, NULL);
39 tDevFS_Driver gFIFO_DriverInfo = {
44 .ACLs = &gVFS_ACL_EveryoneRW,
45 .Flags = VFS_FFLAG_DIRECTORY,
46 .ReadDir = FIFO_ReadDir,
47 .FindDir = FIFO_FindDir,
49 .Relink = FIFO_Relink,
53 tVFS_Node gFIFO_AnonNode = {
55 .ACLs = &gVFS_ACL_EveryoneRW,
57 tPipe *gFIFO_NamedPipes = NULL;
61 * \fn int FIFO_Install(char **Options)
62 * \brief Installs the FIFO Driver
64 int FIFO_Install(char **Options)
66 DevFS_AddDevice( &gFIFO_DriverInfo );
71 * \fn int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data)
73 int FIFO_IOCtl(tVFS_Node *Node, int Id, void *Data)
79 * \fn char *FIFO_ReadDir(tVFS_Node *Node, int Id)
80 * \brief Reads from the FIFO root
82 char *FIFO_ReadDir(tVFS_Node *Node, int Id)
84 tPipe *tmp = gFIFO_NamedPipes;
86 // Entry 0 is Anon Pipes
87 if(Id == 0) return strdup("anon");
89 // Find the id'th node
90 while(--Id && tmp) tmp = tmp->Next;
91 // If node found, return it
92 if(tmp) return strdup(tmp->Name);
98 * \fn tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename)
99 * \brief Find a file in the FIFO root
100 * \note Creates an anon pipe if anon is requested
102 tVFS_Node *FIFO_FindDir(tVFS_Node *Node, const char *Filename)
105 if(!Filename) return NULL;
108 if(Filename[0] == '\0') return NULL;
111 if(Filename[0] == 'a' && Filename[1] == 'n'
112 && Filename[2] == 'o' && Filename[3] == 'n'
113 && Filename[4] == '\0') {
114 tmp = FIFO_Int_NewPipe(DEFAULT_RING_SIZE, "anon");
119 tmp = gFIFO_NamedPipes;
122 if(strcmp(tmp->Name, Filename) == 0)
130 * \fn int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
132 int FIFO_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
138 * \fn void FIFO_Close(tVFS_Node *Node)
139 * \brief Close a FIFO end
141 void FIFO_Close(tVFS_Node *Node)
144 if(!Node->ImplPtr) return ;
146 Node->ReferenceCount --;
147 if(Node->ReferenceCount) return ;
149 pipe = Node->ImplPtr;
151 if(strcmp(pipe->Name, "anon") == 0) {
160 * \fn int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName)
161 * \brief Relink a file (Deletes named pipes)
163 int FIFO_Relink(tVFS_Node *Node, const char *OldName, const char *NewName)
167 if(Node != &gFIFO_DriverInfo.RootNode) return 0;
170 if(strcmp(OldName, "anon")) return 0;
173 for(pipe = gFIFO_NamedPipes;
177 if(strcmp(pipe->Name, OldName) == 0)
182 // Relink a named pipe
185 for(tmp = gFIFO_NamedPipes;
189 if(strcmp(tmp->Name, NewName) == 0) return 0;
193 pipe->Name = malloc(strlen(NewName)+1);
194 strcpy(pipe->Name, NewName);
208 * \fn Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
209 * \brief Read from a fifo pipe
211 Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
213 tPipe *pipe = Node->ImplPtr;
215 Uint remaining = Length;
221 // Wait for buffer to fill
222 if(pipe->Flags & PF_BLOCKING)
225 len = Semaphore_Wait( &pipe->Semaphore, remaining );
227 VFS_SelectNode(Node, VFS_SELECT_READ, NULL, "FIFO_Read");
229 // TODO: Rethink this, it might not work on buffer overflow
230 if(pipe->WritePos - pipe->ReadPos < remaining)
231 len = pipe->WritePos - pipe->ReadPos;
238 if(pipe->ReadPos == pipe->WritePos)
241 if(pipe->WritePos - pipe->ReadPos < remaining)
242 len = pipe->WritePos - pipe->ReadPos;
247 // Check if read overflows buffer
248 if(len > pipe->BufSize - pipe->ReadPos)
250 int ofs = pipe->BufSize - pipe->ReadPos;
251 memcpy(Buffer, &pipe->Buffer[pipe->ReadPos], ofs);
252 memcpy((Uint8*)Buffer + ofs, &pipe->Buffer, len-ofs);
256 memcpy(Buffer, &pipe->Buffer[pipe->ReadPos], len);
259 // Increment read position
260 pipe->ReadPos += len;
261 pipe->ReadPos %= pipe->BufSize;
264 if( pipe->ReadPos == pipe->WritePos ) {
265 VFS_MarkAvaliable(Node, 0);
267 VFS_MarkFull(Node, 0); // Buffer can't still be full
269 // Decrement Remaining Bytes
271 // Increment Buffer address
272 Buffer = (Uint8*)Buffer + len;
280 * \fn Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
281 * \brief Write to a fifo pipe
283 Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
285 tPipe *pipe = Node->ImplPtr;
287 Uint remaining = Length;
293 // Wait for buffer to empty
294 if(pipe->Flags & PF_BLOCKING) {
296 len = Semaphore_Signal( &pipe->Semaphore, remaining );
298 VFS_SelectNode(Node, VFS_SELECT_WRITE, NULL, "FIFO_Write");
299 if(pipe->ReadPos - pipe->WritePos < remaining)
300 len = pipe->ReadPos - pipe->WritePos;
307 if(pipe->ReadPos == (pipe->WritePos+1)%pipe->BufSize)
310 if(pipe->ReadPos - pipe->WritePos < remaining)
311 len = pipe->ReadPos - pipe->WritePos;
316 // Check if write overflows buffer
317 if(len > pipe->BufSize - pipe->WritePos)
319 int ofs = pipe->BufSize - pipe->WritePos;
320 memcpy(&pipe->Buffer[pipe->WritePos], Buffer, ofs);
321 memcpy(&pipe->Buffer, (Uint8*)Buffer + ofs, len-ofs);
325 memcpy(&pipe->Buffer[pipe->WritePos], Buffer, len);
328 // Increment read position
329 pipe->WritePos += len;
330 pipe->WritePos %= pipe->BufSize;
333 if( pipe->ReadPos == pipe->WritePos ) {
334 VFS_MarkFull(Node, 1); // Buffer full
336 VFS_MarkAvaliable(Node, 1);
338 // Decrement Remaining Bytes
340 // Increment Buffer address
341 Buffer = (Uint8*)Buffer + len;
349 * \fn tPipe *FIFO_Int_NewPipe(int Size, const char *Name)
350 * \brief Create a new pipe
352 tPipe *FIFO_Int_NewPipe(int Size, const char *Name)
355 int namelen = strlen(Name) + 1;
356 int allocsize = sizeof(tPipe) + sizeof(tVFS_ACL) + Size + namelen;
358 ret = malloc(allocsize);
359 if(!ret) return NULL;
362 memset(ret, 0, allocsize);
363 ret->Flags = PF_BLOCKING;
367 ret->Buffer = (void*)( (Uint)ret + sizeof(tPipe) + sizeof(tVFS_ACL) );
369 // Set name (and FIFO name)
370 ret->Name = ret->Buffer + Size;
371 strcpy(ret->Name, Name);
372 // - Start empty, max of `Size`
373 //Semaphore_Init( &ret->Semaphore, 0, Size, "FIFO", ret->Name );
377 ret->Node.ImplPtr = ret;
378 ret->Node.UID = Threads_GetUID();
379 ret->Node.GID = Threads_GetGID();
380 ret->Node.NumACLs = 1;
381 ret->Node.ACLs = (void*)( (Uint)ret + sizeof(tPipe) );
382 ret->Node.ACLs->Group = 0;
383 ret->Node.ACLs->ID = ret->Node.UID;
384 ret->Node.ACLs->Inv = 0;
385 ret->Node.ACLs->Perms = -1;
388 = ret->Node.ATime = now();
389 ret->Node.Read = FIFO_Read;
390 ret->Node.Write = FIFO_Write;
391 ret->Node.Close = FIFO_Close;