1 /* $Id: create.c,v 1.28 2004/12/30 16:15:10 ekohl Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: services/fs/np/create.c
6 * PURPOSE: Named pipe filesystem
7 * PROGRAMMER: David Welch <welch@cwcom.net>
10 /* INCLUDES ******************************************************************/
12 #include <ddk/ntddk.h>
19 /* FUNCTIONS *****************************************************************/
22 NpfsFindPipe(PNPFS_DEVICE_EXTENSION DeviceExt
,
23 PUNICODE_STRING PipeName
)
25 PLIST_ENTRY CurrentEntry
;
28 CurrentEntry
= DeviceExt
->PipeListHead
.Flink
;
29 while (CurrentEntry
!= &DeviceExt
->PipeListHead
)
31 Pipe
= CONTAINING_RECORD(CurrentEntry
, NPFS_PIPE
, PipeListEntry
);
32 if (RtlCompareUnicodeString(PipeName
,
36 DPRINT("<%wZ> = <%wZ>\n", PipeName
, &Pipe
->PipeName
);
40 CurrentEntry
= CurrentEntry
->Flink
;
48 NpfsFindListeningServerInstance(PNPFS_PIPE Pipe
)
50 PLIST_ENTRY CurrentEntry
;
53 CurrentEntry
= Pipe
->ServerFcbListHead
.Flink
;
54 while (CurrentEntry
!= &Pipe
->ServerFcbListHead
)
56 ServerFcb
= CONTAINING_RECORD(CurrentEntry
, NPFS_FCB
, FcbListEntry
);
57 if (ServerFcb
->PipeState
== FILE_PIPE_LISTENING_STATE
)
59 DPRINT("Server found! Fcb %p\n", ServerFcb
);
62 CurrentEntry
= CurrentEntry
->Flink
;
70 NpfsCreate(PDEVICE_OBJECT DeviceObject
,
73 PIO_STACK_LOCATION IoStack
;
74 PFILE_OBJECT FileObject
;
77 PNPFS_FCB ServerFcb
= NULL
;
78 PNPFS_DEVICE_EXTENSION DeviceExt
;
79 BOOLEAN SpecialAccess
;
81 DPRINT("NpfsCreate(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
83 DeviceExt
= (PNPFS_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
84 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
85 FileObject
= IoStack
->FileObject
;
86 DPRINT("FileObject %p\n", FileObject
);
87 DPRINT("FileName %wZ\n", &FileObject
->FileName
);
89 Irp
->IoStatus
.Information
= 0;
91 SpecialAccess
= ((IoStack
->Parameters
.Create
.ShareAccess
& 3) == 3);
94 DPRINT("NpfsCreate() open client end for special use!\n");
98 * Step 1. Find the pipe we're trying to open.
100 KeLockMutex(&DeviceExt
->PipeListLock
);
101 Pipe
= NpfsFindPipe(DeviceExt
,
102 &FileObject
->FileName
);
105 /* Not found, bail out with error. */
106 DPRINT("No pipe found!\n");
107 KeUnlockMutex(&DeviceExt
->PipeListLock
);
108 Irp
->IoStatus
.Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
109 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
110 return STATUS_OBJECT_NAME_NOT_FOUND
;
113 KeUnlockMutex(&DeviceExt
->PipeListLock
);
116 * Step 2. Search for listening server FCB.
120 * Acquire the lock for FCB lists. From now on no modifications to the
121 * FCB lists are allowed, because it can cause various misconsistencies.
123 KeLockMutex(&Pipe
->FcbListLock
);
127 ServerFcb
= NpfsFindListeningServerInstance(Pipe
);
128 if (ServerFcb
== NULL
)
130 /* Not found, bail out with error for FILE_OPEN requests. */
131 DPRINT("No listening server fcb found!\n");
132 KeUnlockMutex(&Pipe
->FcbListLock
);
133 Irp
->IoStatus
.Status
= STATUS_PIPE_BUSY
;
134 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
135 return STATUS_PIPE_BUSY
;
138 else if (IsListEmpty(&Pipe
->ServerFcbListHead
))
140 DPRINT("No server fcb found!\n");
141 KeUnlockMutex(&Pipe
->FcbListLock
);
142 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
143 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
144 return STATUS_UNSUCCESSFUL
;
148 * Step 3. Create the client FCB.
150 ClientFcb
= ExAllocatePool(NonPagedPool
, sizeof(NPFS_FCB
));
151 if (ClientFcb
== NULL
)
153 DPRINT("No memory!\n");
154 KeUnlockMutex(&Pipe
->FcbListLock
);
155 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
156 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
157 return STATUS_NO_MEMORY
;
160 ClientFcb
->Pipe
= Pipe
;
161 ClientFcb
->PipeEnd
= FILE_PIPE_CLIENT_END
;
162 ClientFcb
->OtherSide
= NULL
;
163 ClientFcb
->PipeState
= SpecialAccess
? 0 : FILE_PIPE_DISCONNECTED_STATE
;
165 /* Initialize data list. */
166 if (Pipe
->InboundQuota
)
168 ClientFcb
->Data
= ExAllocatePool(NonPagedPool
, Pipe
->InboundQuota
);
169 if (ClientFcb
->Data
== NULL
)
171 DPRINT("No memory!\n");
172 ExFreePool(ClientFcb
);
173 KeUnlockMutex(&Pipe
->FcbListLock
);
174 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
175 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
176 return STATUS_NO_MEMORY
;
181 ClientFcb
->Data
= NULL
;
184 ClientFcb
->ReadPtr
= ClientFcb
->Data
;
185 ClientFcb
->WritePtr
= ClientFcb
->Data
;
186 ClientFcb
->ReadDataAvailable
= 0;
187 ClientFcb
->WriteQuotaAvailable
= Pipe
->InboundQuota
;
188 ClientFcb
->MaxDataLength
= Pipe
->InboundQuota
;
189 KeInitializeSpinLock(&ClientFcb
->DataListLock
);
190 KeInitializeEvent(&ClientFcb
->ConnectEvent
, SynchronizationEvent
, FALSE
);
191 KeInitializeEvent(&ClientFcb
->Event
, SynchronizationEvent
, FALSE
);
194 * Step 4. Add the client FCB to a list and connect it if necessary.
197 /* Add the client FCB to the pipe FCB list. */
198 InsertTailList(&Pipe
->ClientFcbListHead
, &ClientFcb
->FcbListEntry
);
202 ClientFcb
->OtherSide
= ServerFcb
;
203 ServerFcb
->OtherSide
= ClientFcb
;
204 ClientFcb
->PipeState
= FILE_PIPE_CONNECTED_STATE
;
205 ServerFcb
->PipeState
= FILE_PIPE_CONNECTED_STATE
;
207 /* Wake server thread */
208 DPRINT("Setting the ConnectEvent for %x\n", ServerFcb
);
209 KeSetEvent(&ServerFcb
->ConnectEvent
, 0, FALSE
);
212 KeUnlockMutex(&Pipe
->FcbListLock
);
214 FileObject
->FsContext
= ClientFcb
;
216 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
217 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
219 DPRINT("Success!\n");
221 return STATUS_SUCCESS
;
226 NpfsCreateNamedPipe(PDEVICE_OBJECT DeviceObject
,
229 PIO_STACK_LOCATION IoStack
;
230 PFILE_OBJECT FileObject
;
231 PNPFS_DEVICE_EXTENSION DeviceExt
;
234 PNAMED_PIPE_CREATE_PARAMETERS Buffer
;
235 BOOLEAN NewPipe
= FALSE
;
237 DPRINT("NpfsCreateNamedPipe(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
239 DeviceExt
= (PNPFS_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
240 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
241 FileObject
= IoStack
->FileObject
;
242 DPRINT("FileObject %p\n", FileObject
);
243 DPRINT("Pipe name %wZ\n", &FileObject
->FileName
);
245 Buffer
= IoStack
->Parameters
.CreatePipe
.Parameters
;
247 Irp
->IoStatus
.Information
= 0;
249 Fcb
= ExAllocatePool(NonPagedPool
, sizeof(NPFS_FCB
));
252 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
253 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
254 return STATUS_NO_MEMORY
;
257 KeLockMutex(&DeviceExt
->PipeListLock
);
260 * First search for existing Pipe with the same name.
262 Pipe
= NpfsFindPipe(DeviceExt
,
263 &FileObject
->FileName
);
267 * Found Pipe with the same name. Check if we are
270 KeUnlockMutex(&DeviceExt
->PipeListLock
);
272 if (Pipe
->CurrentInstances
>= Pipe
->MaximumInstances
)
274 DPRINT("Out of instances.\n");
276 Irp
->IoStatus
.Status
= STATUS_PIPE_BUSY
;
277 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
278 return STATUS_PIPE_BUSY
;
281 /* FIXME: Check pipe modes also! */
282 if (Pipe
->MaximumInstances
!= Buffer
->MaximumInstances
||
283 Pipe
->TimeOut
.QuadPart
!= Buffer
->DefaultTimeout
.QuadPart
)
285 DPRINT("Asked for invalid pipe mode.\n");
287 Irp
->IoStatus
.Status
= STATUS_ACCESS_DENIED
;
288 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
289 return STATUS_ACCESS_DENIED
;
295 Pipe
= ExAllocatePool(NonPagedPool
, sizeof(NPFS_PIPE
));
298 KeUnlockMutex(&DeviceExt
->PipeListLock
);
299 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
300 Irp
->IoStatus
.Information
= 0;
301 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
302 return STATUS_NO_MEMORY
;
305 if (RtlCreateUnicodeString(&Pipe
->PipeName
, FileObject
->FileName
.Buffer
) == FALSE
)
307 KeUnlockMutex(&DeviceExt
->PipeListLock
);
310 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
311 Irp
->IoStatus
.Information
= 0;
312 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
313 return STATUS_NO_MEMORY
;
316 InitializeListHead(&Pipe
->ServerFcbListHead
);
317 InitializeListHead(&Pipe
->ClientFcbListHead
);
318 KeInitializeMutex(&Pipe
->FcbListLock
, 0);
320 Pipe
->PipeType
= Buffer
->NamedPipeType
? FILE_PIPE_MESSAGE_TYPE
: FILE_PIPE_BYTE_STREAM_TYPE
;
321 Pipe
->PipeWriteMode
= Buffer
->NamedPipeType
? FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
322 Pipe
->PipeReadMode
= Buffer
->ReadMode
? FILE_PIPE_MESSAGE_MODE
: FILE_PIPE_BYTE_STREAM_MODE
;
323 Pipe
->PipeBlockMode
= Buffer
->CompletionMode
;
324 Pipe
->PipeConfiguration
= IoStack
->Parameters
.Create
.Options
& 0x3;
325 Pipe
->MaximumInstances
= Buffer
->MaximumInstances
;
326 Pipe
->CurrentInstances
= 0;
327 Pipe
->TimeOut
= Buffer
->DefaultTimeout
;
328 if (!(IoStack
->Parameters
.Create
.Options
& FILE_PIPE_OUTBOUND
) ||
329 IoStack
->Parameters
.Create
.Options
& FILE_PIPE_FULL_DUPLEX
)
331 if (Buffer
->InboundQuota
== 0)
333 Pipe
->InboundQuota
= DeviceExt
->DefaultQuota
;
337 Pipe
->InboundQuota
= PAGE_ROUND_UP(Buffer
->InboundQuota
);
338 if (Pipe
->InboundQuota
< DeviceExt
->MinQuota
)
340 Pipe
->InboundQuota
= DeviceExt
->MinQuota
;
342 else if (Pipe
->InboundQuota
> DeviceExt
->MaxQuota
)
344 Pipe
->InboundQuota
= DeviceExt
->MaxQuota
;
350 Pipe
->InboundQuota
= 0;
353 if (IoStack
->Parameters
.Create
.Options
& (FILE_PIPE_FULL_DUPLEX
|FILE_PIPE_OUTBOUND
))
355 if (Buffer
->OutboundQuota
== 0)
357 Pipe
->OutboundQuota
= DeviceExt
->DefaultQuota
;
361 Pipe
->OutboundQuota
= PAGE_ROUND_UP(Buffer
->OutboundQuota
);
362 if (Pipe
->OutboundQuota
< DeviceExt
->MinQuota
)
364 Pipe
->OutboundQuota
= DeviceExt
->MinQuota
;
366 else if (Pipe
->OutboundQuota
> DeviceExt
->MaxQuota
)
368 Pipe
->OutboundQuota
= DeviceExt
->MaxQuota
;
374 Pipe
->OutboundQuota
= 0;
377 InsertTailList(&DeviceExt
->PipeListHead
, &Pipe
->PipeListEntry
);
378 KeUnlockMutex(&DeviceExt
->PipeListLock
);
381 if (Pipe
->OutboundQuota
)
383 Fcb
->Data
= ExAllocatePool(NonPagedPool
, Pipe
->OutboundQuota
);
384 if (Fcb
->Data
== NULL
)
390 RtlFreeUnicodeString(&Pipe
->PipeName
);
394 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
395 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
396 return STATUS_NO_MEMORY
;
404 Fcb
->ReadPtr
= Fcb
->Data
;
405 Fcb
->WritePtr
= Fcb
->Data
;
406 Fcb
->ReadDataAvailable
= 0;
407 Fcb
->WriteQuotaAvailable
= Pipe
->OutboundQuota
;
408 Fcb
->MaxDataLength
= Pipe
->OutboundQuota
;
409 KeInitializeSpinLock(&Fcb
->DataListLock
);
411 Pipe
->CurrentInstances
++;
413 KeLockMutex(&Pipe
->FcbListLock
);
414 InsertTailList(&Pipe
->ServerFcbListHead
, &Fcb
->FcbListEntry
);
415 KeUnlockMutex(&Pipe
->FcbListLock
);
418 Fcb
->PipeEnd
= FILE_PIPE_SERVER_END
;
419 Fcb
->PipeState
= FILE_PIPE_LISTENING_STATE
;
420 Fcb
->OtherSide
= NULL
;
422 KeInitializeEvent(&Fcb
->ConnectEvent
,
423 SynchronizationEvent
,
426 KeInitializeEvent(&Fcb
->Event
,
427 SynchronizationEvent
,
430 FileObject
->FsContext
= Fcb
;
432 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
433 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
435 return STATUS_SUCCESS
;
441 PDEVICE_OBJECT DeviceObject
,
444 PNPFS_DEVICE_EXTENSION DeviceExt
;
445 PIO_STACK_LOCATION IoStack
;
446 PFILE_OBJECT FileObject
;
451 DPRINT("NpfsClose(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
453 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
454 DeviceExt
= (PNPFS_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
455 FileObject
= IoStack
->FileObject
;
456 Fcb
= FileObject
->FsContext
;
460 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
461 Irp
->IoStatus
.Information
= 0;
462 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
463 return STATUS_SUCCESS
;
466 DPRINT("Fcb %x\n", Fcb
);
469 DPRINT("Closing pipe %wZ\n", &Pipe
->PipeName
);
471 KeLockMutex(&Pipe
->FcbListLock
);
473 Server
= (Fcb
->PipeEnd
== FILE_PIPE_SERVER_END
);
477 /* FIXME: Clean up existing connections here ?? */
479 Pipe
->CurrentInstances
--;
486 if (Fcb
->PipeState
== FILE_PIPE_CONNECTED_STATE
)
490 #ifndef FIN_WORKAROUND_READCLOSE
491 Fcb
->OtherSide
->PipeState
= FILE_PIPE_CLOSING_STATE
;
492 Fcb
->OtherSide
->OtherSide
= NULL
;
495 * Signaling the write event. If is possible that an other
496 * thread waits for an empty buffer.
498 KeSetEvent(&Fcb
->OtherSide
->Event
, IO_NO_INCREMENT
, FALSE
);
501 #ifndef FIN_WORKAROUND_READCLOSE
506 FileObject
->FsContext
= NULL
;
508 #ifndef FIN_WORKAROUND_READCLOSE
509 RemoveEntryList(&Fcb
->FcbListEntry
);
511 ExFreePool(Fcb
->Data
);
514 Fcb
->PipeState
= FILE_PIPE_CLOSING_STATE
;
515 if (Fcb
->OtherSide
== NULL
||
516 Fcb
->OtherSide
->PipeState
== FILE_PIPE_CLOSING_STATE
)
518 if (Server
&& Fcb
->OtherSide
!= NULL
&&
519 Fcb
->OtherSide
->PipeState
== FILE_PIPE_CLOSING_STATE
)
521 RemoveEntryList(&Fcb
->OtherSide
->FcbListEntry
);
522 if (Fcb
->OtherSide
->Data
)
523 ExFreePool(Fcb
->OtherSide
->Data
);
524 ExFreePool(Fcb
->OtherSide
);
527 RemoveEntryList(&Fcb
->FcbListEntry
);
529 ExFreePool(Fcb
->Data
);
535 KeUnlockMutex(&Pipe
->FcbListLock
);
537 if (Server
&& Pipe
->CurrentInstances
== 0)
539 RtlFreeUnicodeString(&Pipe
->PipeName
);
540 KeLockMutex(&DeviceExt
->PipeListLock
);
541 RemoveEntryList(&Pipe
->PipeListEntry
);
542 KeUnlockMutex(&DeviceExt
->PipeListLock
);
546 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
547 Irp
->IoStatus
.Information
= 0;
548 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
550 return STATUS_SUCCESS
;