2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/filesystems/npfs/create.c
5 * PURPOSE: Named pipe filesystem
6 * PROGRAMMER: David Welch <welch@cwcom.net>
9 /* INCLUDES ******************************************************************/
16 //#define USING_PROPER_NPFS_WAIT_SEMANTICS
18 /* FUNCTIONS *****************************************************************/
21 NpfsDereferenceFcb(PNPFS_FCB Fcb
)
23 PNPFS_VCB Vcb
= Fcb
->Vcb
;
25 KeLockMutex(&Vcb
->PipeListLock
);
26 if (InterlockedDecrement(&Fcb
->RefCount
) == 0)
28 DPRINT("NpfsDereferenceFcb. Deleting %p\n", Fcb
);
29 RemoveEntryList(&Fcb
->PipeListEntry
);
30 RtlFreeUnicodeString(&Fcb
->PipeName
);
31 ExFreePoolWithTag(Fcb
, TAG_NPFS_FCB
);
33 KeUnlockMutex(&Vcb
->PipeListLock
);
38 NpfsAllocateCcb(CCB_TYPE Type
, PNPFS_FCB Fcb
)
42 Ccb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(NPFS_CCB
), TAG_NPFS_CCB
);
48 RtlZeroMemory(Ccb
, sizeof(NPFS_CCB
));
52 // FIXME: why does this function not reference Fcb?
54 Ccb
->OtherSide
= NULL
;
61 NpfsReferenceCcb(PNPFS_CCB Ccb
)
63 ASSERT(Ccb
->RefCount
> 0);
64 InterlockedIncrement(&Ccb
->RefCount
);
69 NpfsDereferenceCcb(PNPFS_CCB Ccb
)
71 /* Decrement reference count */
72 ASSERT(Ccb
->RefCount
> 0);
73 if (InterlockedDecrement(&Ccb
->RefCount
) == 0)
75 /* Its zero, delete CCB */
76 ExFreePoolWithTag(Ccb
, TAG_NPFS_CCB
);
82 NpfsCcbSetOtherSide(PNPFS_CCB Ccb
, PNPFS_CCB OtherSide
)
84 /* Dereference old other side */
85 if (Ccb
->OtherSide
) NpfsDereferenceCcb(Ccb
->OtherSide
);
87 /* Reference the new other side */
88 if (OtherSide
) NpfsReferenceCcb(OtherSide
);
91 Ccb
->OtherSide
= OtherSide
;
95 NpfsFindPipe(PNPFS_VCB Vcb
,
96 PUNICODE_STRING PipeName
)
98 PLIST_ENTRY CurrentEntry
;
101 CurrentEntry
= Vcb
->PipeListHead
.Flink
;
102 while (CurrentEntry
!= &Vcb
->PipeListHead
)
104 Fcb
= CONTAINING_RECORD(CurrentEntry
, NPFS_FCB
, PipeListEntry
);
105 if (RtlCompareUnicodeString(PipeName
,
109 DPRINT("<%wZ> = <%wZ>\n", PipeName
, &Fcb
->PipeName
);
110 (VOID
)InterlockedIncrement(&Fcb
->RefCount
);
114 CurrentEntry
= CurrentEntry
->Flink
;
122 NpfsFindListeningServerInstance(PNPFS_FCB Fcb
)
124 PLIST_ENTRY CurrentEntry
;
125 PNPFS_WAITER_ENTRY Waiter
;
129 CurrentEntry
= Fcb
->WaiterListHead
.Flink
;
130 while (CurrentEntry
!= &Fcb
->WaiterListHead
)
132 Waiter
= CONTAINING_RECORD(CurrentEntry
, NPFS_WAITER_ENTRY
, Entry
);
133 Irp
= CONTAINING_RECORD(Waiter
, IRP
, Tail
.Overlay
.DriverContext
);
134 if (Waiter
->Ccb
->PipeState
== FILE_PIPE_LISTENING_STATE
)
136 DPRINT("Server found! CCB %p\n", Waiter
->Ccb
);
138 IoAcquireCancelSpinLock(&oldIrql
);
141 if (IoSetCancelRoutine(Irp
, NULL
) != NULL
)
143 IoReleaseCancelSpinLock(oldIrql
);
147 IoReleaseCancelSpinLock(oldIrql
);
150 CurrentEntry
= CurrentEntry
->Flink
;
158 NpfsSignalAndRemoveListeningServerInstance(PNPFS_FCB Fcb
,
161 PLIST_ENTRY CurrentEntry
;
162 PNPFS_WAITER_ENTRY Waiter
;
165 CurrentEntry
= Fcb
->WaiterListHead
.Flink
;
166 while (CurrentEntry
!= &Fcb
->WaiterListHead
)
168 Waiter
= CONTAINING_RECORD(CurrentEntry
, NPFS_WAITER_ENTRY
, Entry
);
169 if (Waiter
->Ccb
== Ccb
)
171 DPRINT("Server found! CCB %p\n", Waiter
->Ccb
);
173 RemoveEntryList(&Waiter
->Entry
);
174 Irp
= CONTAINING_RECORD(Waiter
, IRP
, Tail
.Overlay
.DriverContext
);
175 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
176 Irp
->IoStatus
.Information
= 0;
177 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
180 CurrentEntry
= CurrentEntry
->Flink
;
186 NpfsOpenFileSystem(PNPFS_FCB Fcb
,
187 PFILE_OBJECT FileObject
,
188 PIO_STATUS_BLOCK IoStatus
)
192 DPRINT("NpfsOpenFileSystem()\n");
194 Ccb
= NpfsAllocateCcb(CCB_DEVICE
, Fcb
);
197 IoStatus
->Status
= STATUS_NO_MEMORY
;
201 Ccb
->FileObject
= FileObject
;
203 FileObject
->FsContext
= Fcb
;
204 FileObject
->FsContext2
= Ccb
;
206 IoStatus
->Information
= FILE_OPENED
;
207 IoStatus
->Status
= STATUS_SUCCESS
;
214 NpfsOpenRootDirectory(PNPFS_FCB Fcb
,
215 PFILE_OBJECT FileObject
,
216 PIO_STATUS_BLOCK IoStatus
)
220 DPRINT("NpfsOpenRootDirectory()\n");
222 Ccb
= NpfsAllocateCcb(CCB_DIRECTORY
, Fcb
);
225 IoStatus
->Status
= STATUS_NO_MEMORY
;
229 Ccb
->FileObject
= FileObject
;
231 FileObject
->FsContext
= Fcb
;
232 FileObject
->FsContext2
= Ccb
;
234 IoStatus
->Information
= FILE_OPENED
;
235 IoStatus
->Status
= STATUS_SUCCESS
;
242 NpfsCreate(PDEVICE_OBJECT DeviceObject
,
245 PEXTENDED_IO_STACK_LOCATION IoStack
;
246 PUNICODE_STRING FileName
;
247 PFILE_OBJECT FileObject
;
248 PFILE_OBJECT RelatedFileObject
;
251 PNPFS_CCB ServerCcb
= NULL
;
254 #ifndef USING_PROPER_NPFS_WAIT_SEMANTICS
255 ACCESS_MASK DesiredAccess
;
256 BOOLEAN SpecialAccess
;
259 DPRINT("NpfsCreate(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
261 Vcb
= (PNPFS_VCB
)DeviceObject
->DeviceExtension
;
262 IoStack
= (PEXTENDED_IO_STACK_LOCATION
)IoGetCurrentIrpStackLocation(Irp
);
263 FileObject
= IoStack
->FileObject
;
264 RelatedFileObject
= FileObject
->RelatedFileObject
;
265 FileName
= &FileObject
->FileName
;
266 #ifndef USING_PROPER_NPFS_WAIT_SEMANTICS
267 DesiredAccess
= IoStack
->Parameters
.CreatePipe
.SecurityContext
->DesiredAccess
;
270 DPRINT("FileObject %p\n", FileObject
);
271 DPRINT("FileName %wZ\n", &FileObject
->FileName
);
273 Irp
->IoStatus
.Information
= 0;
275 #ifndef USING_PROPER_NPFS_WAIT_SEMANTICS
276 SpecialAccess
= ((DesiredAccess
& SPECIFIC_RIGHTS_ALL
) == FILE_READ_ATTRIBUTES
);
279 DPRINT("NpfsCreate() open client end for special use!\n");
283 DPRINT("FileName->Length: %hu RelatedFileObject: %p\n", FileName
->Length
, RelatedFileObject
);
285 /* Open the file system */
286 if (FileName
->Length
== 0 &&
287 (RelatedFileObject
== NULL
|| ((PNPFS_CCB
)RelatedFileObject
->FsContext2
)->Type
== CCB_DEVICE
))
289 DPRINT("Open the file system\n");
291 NpfsOpenFileSystem(Vcb
->DeviceFcb
,
295 Status
= Irp
->IoStatus
.Status
;
296 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
300 /* Open the root directory */
301 if ((FileName
->Length
== 2 && FileName
->Buffer
[0] == L
'\\' && RelatedFileObject
== NULL
) ||
302 (FileName
->Length
== 0 && ((PNPFS_CCB
)RelatedFileObject
->FsContext2
)->Type
== CCB_DIRECTORY
))
304 DPRINT("Open the root directory\n");
306 NpfsOpenRootDirectory(Vcb
->RootFcb
,
310 Status
= Irp
->IoStatus
.Status
;
311 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
317 * Step 1. Find the pipe we're trying to open.
319 KeLockMutex(&Vcb
->PipeListLock
);
320 Fcb
= NpfsFindPipe(Vcb
, &FileObject
->FileName
);
323 /* Not found, bail out with error. */
324 DPRINT("No pipe found!\n");
325 KeUnlockMutex(&Vcb
->PipeListLock
);
326 Irp
->IoStatus
.Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
327 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
328 return STATUS_OBJECT_NAME_NOT_FOUND
;
331 KeUnlockMutex(&Vcb
->PipeListLock
);
334 * Acquire the lock for CCB lists. From now on no modifications to the
335 * CCB lists are allowed, because it can cause various misconsistencies.
337 KeLockMutex(&Fcb
->CcbListLock
);
340 * Step 2. Create the client CCB.
342 ClientCcb
= NpfsAllocateCcb(CCB_PIPE
, Fcb
);
343 if (ClientCcb
== NULL
)
345 DPRINT("No memory!\n");
346 KeUnlockMutex(&Fcb
->CcbListLock
);
347 NpfsDereferenceFcb(Fcb
);
348 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
349 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
350 return STATUS_NO_MEMORY
;
353 ClientCcb
->FileObject
= FileObject
;
354 ClientCcb
->Thread
= (struct ETHREAD
*)Irp
->Tail
.Overlay
.Thread
;
355 ClientCcb
->PipeEnd
= FILE_PIPE_CLIENT_END
;
356 #ifndef USING_PROPER_NPFS_WAIT_SEMANTICS
357 ClientCcb
->PipeState
= SpecialAccess
? 0 : FILE_PIPE_DISCONNECTED_STATE
;
359 ClientCcb
->PipeState
= FILE_PIPE_DISCONNECTED_STATE
;
361 InitializeListHead(&ClientCcb
->ReadRequestListHead
);
363 DPRINT("CCB: %p\n", ClientCcb
);
365 /* Initialize data list. */
366 if (Fcb
->OutboundQuota
)
368 ClientCcb
->Data
= ExAllocatePoolWithTag(PagedPool
,
371 if (ClientCcb
->Data
== NULL
)
373 DPRINT("No memory!\n");
374 NpfsDereferenceCcb(ClientCcb
);
375 KeUnlockMutex(&Fcb
->CcbListLock
);
376 NpfsDereferenceFcb(Fcb
);
377 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
378 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
379 return STATUS_NO_MEMORY
;
384 ClientCcb
->Data
= NULL
;
387 ClientCcb
->ReadPtr
= ClientCcb
->Data
;
388 ClientCcb
->WritePtr
= ClientCcb
->Data
;
389 ClientCcb
->ReadDataAvailable
= 0;
390 ClientCcb
->WriteQuotaAvailable
= Fcb
->OutboundQuota
;
391 ClientCcb
->MaxDataLength
= Fcb
->OutboundQuota
;
392 ExInitializeFastMutex(&ClientCcb
->DataListLock
);
393 KeInitializeEvent(&ClientCcb
->ConnectEvent
, SynchronizationEvent
, FALSE
);
394 KeInitializeEvent(&ClientCcb
->ReadEvent
, NotificationEvent
, FALSE
);
395 KeInitializeEvent(&ClientCcb
->WriteEvent
, NotificationEvent
, FALSE
);
399 * Step 3. Search for listening server CCB.
401 #ifndef USING_PROPER_NPFS_WAIT_SEMANTICS
406 * WARNING: Point of no return! Once we get the server CCB it's
407 * possible that we completed a wait request and so we have to
408 * complete even this request.
411 ServerCcb
= NpfsFindListeningServerInstance(Fcb
);
412 if (ServerCcb
== NULL
)
414 PLIST_ENTRY CurrentEntry
;
418 * If no waiting server CCB was found then try to pick
419 * one of the listing server CCB on the pipe.
422 CurrentEntry
= Fcb
->ServerCcbListHead
.Flink
;
423 while (CurrentEntry
!= &Fcb
->ServerCcbListHead
)
425 Ccb
= CONTAINING_RECORD(CurrentEntry
, NPFS_CCB
, CcbListEntry
);
426 if (Ccb
->PipeState
== FILE_PIPE_LISTENING_STATE
)
431 CurrentEntry
= CurrentEntry
->Flink
;
435 * No one is listening to me?! I'm so lonely... :(
438 if (ServerCcb
== NULL
)
440 /* Not found, bail out with error for FILE_OPEN requests. */
441 DPRINT("No listening server CCB found!\n");
444 ExFreePoolWithTag(ClientCcb
->Data
, TAG_NPFS_CCB_DATA
);
447 NpfsDereferenceCcb(ClientCcb
);
448 KeUnlockMutex(&Fcb
->CcbListLock
);
449 NpfsDereferenceFcb(Fcb
);
450 Irp
->IoStatus
.Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
451 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
452 return STATUS_OBJECT_NAME_NOT_FOUND
;
457 /* Signal the server thread and remove it from the waiter list */
458 /* FIXME: Merge this with the NpfsFindListeningServerInstance routine. */
459 NpfsSignalAndRemoveListeningServerInstance(Fcb
, ServerCcb
);
461 #ifndef USING_PROPER_NPFS_WAIT_SEMANTICS
463 else if (IsListEmpty(&Fcb
->ServerCcbListHead
))
465 DPRINT("No server fcb found!\n");
469 ExFreePoolWithTag(ClientCcb
->Data
, TAG_NPFS_CCB_DATA
);
472 NpfsDereferenceCcb(ClientCcb
);
473 KeUnlockMutex(&Fcb
->CcbListLock
);
474 NpfsDereferenceFcb(Fcb
);
475 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
476 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
477 return STATUS_UNSUCCESSFUL
;
482 * Step 4. Add the client CCB to a list and connect it if possible.
485 /* Add the client CCB to the pipe CCB list. */
486 InsertTailList(&Fcb
->ClientCcbListHead
, &ClientCcb
->CcbListEntry
);
488 /* Connect to listening server side */
491 NpfsCcbSetOtherSide(ClientCcb
, ServerCcb
);
492 NpfsCcbSetOtherSide(ServerCcb
, ClientCcb
);
493 ClientCcb
->PipeState
= FILE_PIPE_CONNECTED_STATE
;
494 ServerCcb
->PipeState
= FILE_PIPE_CONNECTED_STATE
;
495 KeSetEvent(&ServerCcb
->ConnectEvent
, IO_NO_INCREMENT
, FALSE
);
498 KeUnlockMutex(&Fcb
->CcbListLock
);
500 FileObject
->FsContext
= Fcb
;
501 FileObject
->FsContext2
= ClientCcb
;
502 FileObject
->Flags
|= FO_NAMED_PIPE
;
504 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
505 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
507 DPRINT("Success!\n");
509 return STATUS_SUCCESS
;
514 NpfsCreateNamedPipe(PDEVICE_OBJECT DeviceObject
,
517 PEXTENDED_IO_STACK_LOCATION IoStack
;
518 PFILE_OBJECT FileObject
;
522 PNAMED_PIPE_CREATE_PARAMETERS Buffer
;
524 DPRINT("NpfsCreateNamedPipe(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
526 Vcb
= (PNPFS_VCB
)DeviceObject
->DeviceExtension
;
527 IoStack
= (PEXTENDED_IO_STACK_LOCATION
)IoGetCurrentIrpStackLocation(Irp
);
528 FileObject
= IoStack
->FileObject
;
529 DPRINT("FileObject %p\n", FileObject
);
530 DPRINT("Pipe name %wZ\n", &FileObject
->FileName
);
532 Buffer
= IoStack
->Parameters
.CreatePipe
.Parameters
;
534 Irp
->IoStatus
.Information
= 0;
536 if (!(IoStack
->Parameters
.CreatePipe
.ShareAccess
& (FILE_SHARE_READ
|FILE_SHARE_WRITE
)) ||
537 (IoStack
->Parameters
.CreatePipe
.ShareAccess
& ~(FILE_SHARE_READ
|FILE_SHARE_WRITE
)))
539 Irp
->IoStatus
.Status
= STATUS_INVALID_PARAMETER
;
540 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
541 return STATUS_INVALID_PARAMETER
;
544 KeLockMutex(&Vcb
->PipeListLock
);
547 * First search for existing Pipe with the same name.
549 Fcb
= NpfsFindPipe(Vcb
, &FileObject
->FileName
);
553 * Found Pipe with the same name. Check if we are
556 KeUnlockMutex(&Vcb
->PipeListLock
);
558 if (Fcb
->CurrentInstances
>= Fcb
->MaximumInstances
)
560 DPRINT("Out of instances.\n");
561 NpfsDereferenceFcb(Fcb
);
562 Irp
->IoStatus
.Status
= STATUS_INSTANCE_NOT_AVAILABLE
;
563 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
564 return STATUS_INSTANCE_NOT_AVAILABLE
;
567 if (Fcb
->MaximumInstances
!= Buffer
->MaximumInstances
||
568 Fcb
->TimeOut
.QuadPart
!= Buffer
->DefaultTimeout
.QuadPart
||
569 Fcb
->PipeType
!= Buffer
->NamedPipeType
)
571 DPRINT("Asked for invalid pipe mode.\n");
572 NpfsDereferenceFcb(Fcb
);
573 Irp
->IoStatus
.Status
= STATUS_ACCESS_DENIED
;
574 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
575 return STATUS_ACCESS_DENIED
;
580 Fcb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(NPFS_FCB
), TAG_NPFS_FCB
);
583 KeUnlockMutex(&Vcb
->PipeListLock
);
584 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
585 Irp
->IoStatus
.Information
= 0;
586 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
587 return STATUS_NO_MEMORY
;
590 Fcb
->Type
= FCB_PIPE
;
593 Fcb
->PipeName
.Length
= FileObject
->FileName
.Length
;
594 Fcb
->PipeName
.MaximumLength
= Fcb
->PipeName
.Length
+ sizeof(UNICODE_NULL
);
595 Fcb
->PipeName
.Buffer
= ExAllocatePoolWithTag(NonPagedPool
,
596 Fcb
->PipeName
.MaximumLength
,
598 if (Fcb
->PipeName
.Buffer
== NULL
)
600 KeUnlockMutex(&Vcb
->PipeListLock
);
601 ExFreePoolWithTag(Fcb
, TAG_NPFS_FCB
);
602 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
603 Irp
->IoStatus
.Information
= 0;
604 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
605 return STATUS_NO_MEMORY
;
608 RtlCopyUnicodeString(&Fcb
->PipeName
, &FileObject
->FileName
);
610 InitializeListHead(&Fcb
->ServerCcbListHead
);
611 InitializeListHead(&Fcb
->ClientCcbListHead
);
612 InitializeListHead(&Fcb
->WaiterListHead
);
613 KeInitializeMutex(&Fcb
->CcbListLock
, 0);
615 Fcb
->PipeType
= Buffer
->NamedPipeType
;
616 Fcb
->ServerReadMode
= Buffer
->ReadMode
;
617 /* MSDN documentation reads that clients always start off in byte mode */
618 Fcb
->ClientReadMode
= FILE_PIPE_BYTE_STREAM_MODE
;
620 Fcb
->CompletionMode
= Buffer
->CompletionMode
;
621 switch (IoStack
->Parameters
.CreatePipe
.ShareAccess
& (FILE_SHARE_READ
|FILE_SHARE_WRITE
))
623 case FILE_SHARE_READ
:
624 Fcb
->PipeConfiguration
= FILE_PIPE_OUTBOUND
;
626 case FILE_SHARE_WRITE
:
627 Fcb
->PipeConfiguration
= FILE_PIPE_INBOUND
;
629 case FILE_SHARE_READ
|FILE_SHARE_WRITE
:
630 Fcb
->PipeConfiguration
= FILE_PIPE_FULL_DUPLEX
;
633 Fcb
->MaximumInstances
= Buffer
->MaximumInstances
;
634 Fcb
->CurrentInstances
= 0;
635 Fcb
->TimeOut
= Buffer
->DefaultTimeout
;
636 if (!(Fcb
->PipeConfiguration
& FILE_PIPE_OUTBOUND
) ||
637 Fcb
->PipeConfiguration
& FILE_PIPE_FULL_DUPLEX
)
639 if (Buffer
->InboundQuota
== 0)
641 Fcb
->InboundQuota
= Vcb
->DefaultQuota
;
645 Fcb
->InboundQuota
= PAGE_ROUND_UP(Buffer
->InboundQuota
);
646 if (Fcb
->InboundQuota
< Vcb
->MinQuota
)
648 Fcb
->InboundQuota
= Vcb
->MinQuota
;
650 else if (Fcb
->InboundQuota
> Vcb
->MaxQuota
)
652 Fcb
->InboundQuota
= Vcb
->MaxQuota
;
658 Fcb
->InboundQuota
= 0;
661 if (Fcb
->PipeConfiguration
& (FILE_PIPE_FULL_DUPLEX
|FILE_PIPE_OUTBOUND
))
663 if (Buffer
->OutboundQuota
== 0)
665 Fcb
->OutboundQuota
= Vcb
->DefaultQuota
;
669 Fcb
->OutboundQuota
= PAGE_ROUND_UP(Buffer
->OutboundQuota
);
670 if (Fcb
->OutboundQuota
< Vcb
->MinQuota
)
672 Fcb
->OutboundQuota
= Vcb
->MinQuota
;
674 else if (Fcb
->OutboundQuota
> Vcb
->MaxQuota
)
676 Fcb
->OutboundQuota
= Vcb
->MaxQuota
;
682 Fcb
->OutboundQuota
= 0;
685 InsertTailList(&Vcb
->PipeListHead
, &Fcb
->PipeListEntry
);
686 KeUnlockMutex(&Vcb
->PipeListLock
);
689 Ccb
= NpfsAllocateCcb(CCB_PIPE
, Fcb
);
692 NpfsDereferenceFcb(Fcb
);
693 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
694 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
695 return STATUS_NO_MEMORY
;
698 Ccb
->Thread
= (struct ETHREAD
*)Irp
->Tail
.Overlay
.Thread
;
700 if (Fcb
->InboundQuota
)
702 Ccb
->Data
= ExAllocatePoolWithTag(PagedPool
,
705 if (Ccb
->Data
== NULL
)
707 NpfsDereferenceCcb(Ccb
);
708 NpfsDereferenceFcb(Fcb
);
710 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
711 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
712 return STATUS_NO_MEMORY
;
720 Ccb
->ReadPtr
= Ccb
->Data
;
721 Ccb
->WritePtr
= Ccb
->Data
;
722 Ccb
->ReadDataAvailable
= 0;
723 Ccb
->WriteQuotaAvailable
= Fcb
->InboundQuota
;
724 Ccb
->MaxDataLength
= Fcb
->InboundQuota
;
725 InitializeListHead(&Ccb
->ReadRequestListHead
);
726 ExInitializeFastMutex(&Ccb
->DataListLock
);
728 Fcb
->CurrentInstances
++;
731 Ccb
->FileObject
= FileObject
;
732 Ccb
->PipeEnd
= FILE_PIPE_SERVER_END
;
733 Ccb
->PipeState
= FILE_PIPE_LISTENING_STATE
;
735 DPRINT("CCB: %p\n", Ccb
);
737 KeInitializeEvent(&Ccb
->ConnectEvent
, SynchronizationEvent
, FALSE
);
738 KeInitializeEvent(&Ccb
->ReadEvent
, NotificationEvent
, FALSE
);
739 KeInitializeEvent(&Ccb
->WriteEvent
, NotificationEvent
, FALSE
);
741 KeLockMutex(&Fcb
->CcbListLock
);
742 InsertTailList(&Fcb
->ServerCcbListHead
, &Ccb
->CcbListEntry
);
743 KeUnlockMutex(&Fcb
->CcbListLock
);
745 FileObject
->FsContext
= Fcb
;
746 FileObject
->FsContext2
= Ccb
;
747 FileObject
->Flags
|= FO_NAMED_PIPE
;
749 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
750 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
752 DPRINT("Success!\n");
754 return STATUS_SUCCESS
;
759 NpfsCleanup(PDEVICE_OBJECT DeviceObject
,
763 PIO_STACK_LOCATION IoStack
;
764 PFILE_OBJECT FileObject
;
765 PNPFS_CCB Ccb
, OtherSide
;
769 DPRINT("NpfsCleanup(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
771 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
772 //Vcb = (PNPFS_VCB)DeviceObject->DeviceExtension;
773 FileObject
= IoStack
->FileObject
;
774 Ccb
= FileObject
->FsContext2
;
778 DPRINT("Success!\n");
779 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
780 Irp
->IoStatus
.Information
= 0;
781 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
782 return STATUS_SUCCESS
;
785 if (Ccb
->Type
== CCB_DEVICE
)
787 DPRINT("Cleanup the file system!\n");
788 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
789 Irp
->IoStatus
.Information
= 0;
790 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
791 return STATUS_SUCCESS
;
794 if (Ccb
->Type
== CCB_DIRECTORY
)
796 DPRINT("Cleanup the root directory!\n");
797 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
798 Irp
->IoStatus
.Information
= 0;
799 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
800 return STATUS_SUCCESS
;
803 DPRINT("CCB %p\n", Ccb
);
806 DPRINT("Cleaning pipe %wZ\n", &Fcb
->PipeName
);
808 KeLockMutex(&Fcb
->CcbListLock
);
810 Server
= (Ccb
->PipeEnd
== FILE_PIPE_SERVER_END
);
814 /* FIXME: Clean up existing connections here ?? */
821 if ((Ccb
->PipeState
== FILE_PIPE_CONNECTED_STATE
) && (Ccb
->OtherSide
))
823 OtherSide
= Ccb
->OtherSide
;
824 ASSERT(OtherSide
->OtherSide
== Ccb
);
826 /* Lock the server first */
829 ExAcquireFastMutex(&Ccb
->DataListLock
);
830 ExAcquireFastMutex(&OtherSide
->DataListLock
);
834 ExAcquireFastMutex(&OtherSide
->DataListLock
);
835 ExAcquireFastMutex(&Ccb
->DataListLock
);
839 NpfsCcbSetOtherSide(OtherSide
, NULL
);
840 NpfsCcbSetOtherSide(Ccb
, NULL
);
843 * Signaling the write event. If is possible that an other
844 * thread waits for an empty buffer.
846 KeSetEvent(&OtherSide
->ReadEvent
, IO_NO_INCREMENT
, FALSE
);
847 KeSetEvent(&OtherSide
->WriteEvent
, IO_NO_INCREMENT
, FALSE
);
850 ExReleaseFastMutex(&OtherSide
->DataListLock
);
851 ExReleaseFastMutex(&Ccb
->DataListLock
);
855 ExReleaseFastMutex(&Ccb
->DataListLock
);
856 ExReleaseFastMutex(&OtherSide
->DataListLock
);
859 else if (Ccb
->PipeState
== FILE_PIPE_LISTENING_STATE
)
862 PNPFS_WAITER_ENTRY WaitEntry
= NULL
;
863 BOOLEAN Complete
= FALSE
;
867 Entry
= Ccb
->Fcb
->WaiterListHead
.Flink
;
868 while (Entry
!= &Ccb
->Fcb
->WaiterListHead
)
870 WaitEntry
= CONTAINING_RECORD(Entry
, NPFS_WAITER_ENTRY
, Entry
);
871 if (WaitEntry
->Ccb
== Ccb
)
873 RemoveEntryList(Entry
);
874 tmpIrp
= CONTAINING_RECORD(WaitEntry
, IRP
, Tail
.Overlay
.DriverContext
);
875 IoAcquireCancelSpinLock(&oldIrql
);
876 Complete
= (NULL
!= IoSetCancelRoutine(tmpIrp
, NULL
));
877 IoReleaseCancelSpinLock(oldIrql
);
880 tmpIrp
->IoStatus
.Status
= STATUS_PIPE_BROKEN
;
881 tmpIrp
->IoStatus
.Information
= 0;
882 IoCompleteRequest(tmpIrp
, IO_NO_INCREMENT
);
886 Entry
= Entry
->Flink
;
890 Ccb
->PipeState
= FILE_PIPE_CLOSING_STATE
;
892 KeUnlockMutex(&Fcb
->CcbListLock
);
894 ExAcquireFastMutex(&Ccb
->DataListLock
);
897 ExFreePoolWithTag(Ccb
->Data
, TAG_NPFS_CCB_DATA
);
900 Ccb
->WritePtr
= NULL
;
902 ExReleaseFastMutex(&Ccb
->DataListLock
);
904 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
905 Irp
->IoStatus
.Information
= 0;
906 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
908 DPRINT("Success!\n");
910 return STATUS_SUCCESS
;
914 NpfsClose(PDEVICE_OBJECT DeviceObject
,
917 PIO_STACK_LOCATION IoStack
;
918 PFILE_OBJECT FileObject
;
924 DPRINT("NpfsClose(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
926 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
927 //Vcb = (PNPFS_VCB)DeviceObject->DeviceExtension;
928 FileObject
= IoStack
->FileObject
;
929 Ccb
= FileObject
->FsContext2
;
933 DPRINT("Success!\n");
934 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
935 Irp
->IoStatus
.Information
= 0;
936 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
937 return STATUS_SUCCESS
;
940 if (Ccb
->Type
== CCB_DEVICE
)
942 DPRINT("Closing the file system!\n");
944 NpfsDereferenceCcb(Ccb
);
945 FileObject
->FsContext
= NULL
;
946 FileObject
->FsContext2
= NULL
;
948 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
949 Irp
->IoStatus
.Information
= 0;
950 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
951 return STATUS_SUCCESS
;
954 if (Ccb
->Type
== CCB_DIRECTORY
)
956 DPRINT("Closing the root directory!\n");
958 if (Ccb
->u
.Directory
.SearchPattern
.Buffer
!= NULL
)
959 ExFreePoolWithTag(Ccb
->u
.Directory
.SearchPattern
.Buffer
,
962 NpfsDereferenceCcb(Ccb
);
963 FileObject
->FsContext
= NULL
;
964 FileObject
->FsContext2
= NULL
;
966 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
967 Irp
->IoStatus
.Information
= 0;
968 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
969 return STATUS_SUCCESS
;
972 DPRINT("CCB %p\n", Ccb
);
975 DPRINT("Closing pipe %wZ\n", &Fcb
->PipeName
);
977 KeLockMutex(&Fcb
->CcbListLock
);
979 Server
= (Ccb
->PipeEnd
== FILE_PIPE_SERVER_END
);
984 Fcb
->CurrentInstances
--;
991 /* Disconnect the pipes */
994 /* FIXME: Timo wants it rewritten */
995 /*ASSERT(Ccb->OtherSide->OtherSide == Ccb);*/
996 NpfsCcbSetOtherSide(Ccb
->OtherSide
, NULL
);
997 NpfsCcbSetOtherSide(Ccb
, NULL
);
1000 ASSERT(Ccb
->PipeState
== FILE_PIPE_CLOSING_STATE
);
1002 FileObject
->FsContext2
= NULL
;
1004 RemoveEntryList(&Ccb
->CcbListEntry
);
1006 NpfsDereferenceCcb(Ccb
);
1008 KeUnlockMutex(&Fcb
->CcbListLock
);
1010 NpfsDereferenceFcb(Fcb
);
1011 FileObject
->FsContext
= NULL
;
1013 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1014 Irp
->IoStatus
.Information
= 0;
1015 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1017 DPRINT("Success!\n");
1019 return STATUS_SUCCESS
;