[NPFS]
[reactos.git] / reactos / drivers / filesystems / npfs / npfs.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/filesystems/npfs/npfs.c
5 * PURPOSE: Named pipe filesystem
6 * PROGRAMMER: David Welch <welch@cwcom.net>
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include "npfs.h"
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* FUNCTIONS *****************************************************************/
17
18 NTSTATUS NTAPI
19 DriverEntry(PDRIVER_OBJECT DriverObject,
20 PUNICODE_STRING RegistryPath)
21 {
22 PDEVICE_OBJECT DeviceObject;
23 UNICODE_STRING DeviceName;
24 PNPFS_VCB Vcb;
25 PNPFS_FCB Fcb;
26 NTSTATUS Status;
27
28 UNREFERENCED_PARAMETER(RegistryPath);
29
30 DPRINT("Named Pipe FSD 0.0.2\n");
31
32 ASSERT (sizeof(NPFS_CONTEXT) <= FIELD_OFFSET(IRP, Tail.Overlay.DriverContext));
33 ASSERT (sizeof(NPFS_WAITER_ENTRY) <= FIELD_OFFSET(IRP, Tail.Overlay.DriverContext));
34
35 DriverObject->MajorFunction[IRP_MJ_CREATE] = NpfsCreate;
36 DriverObject->MajorFunction[IRP_MJ_CREATE_NAMED_PIPE] =
37 NpfsCreateNamedPipe;
38 DriverObject->MajorFunction[IRP_MJ_CLOSE] = NpfsClose;
39 DriverObject->MajorFunction[IRP_MJ_READ] = NpfsRead;
40 DriverObject->MajorFunction[IRP_MJ_WRITE] = NpfsWrite;
41 DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] =
42 NpfsQueryInformation;
43 DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] =
44 NpfsSetInformation;
45 DriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] =
46 NpfsQueryVolumeInformation;
47 DriverObject->MajorFunction[IRP_MJ_CLEANUP] = NpfsCleanup;
48 DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS] = NpfsFlushBuffers;
49 DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] =
50 NpfsDirectoryControl;
51 DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =
52 NpfsFileSystemControl;
53 // DriverObject->MajorFunction[IRP_MJ_QUERY_SECURITY] =
54 // NpfsQuerySecurity;
55 // DriverObject->MajorFunction[IRP_MJ_SET_SECURITY] =
56 // NpfsSetSecurity;
57
58 DriverObject->DriverUnload = NULL;
59
60 RtlInitUnicodeString(&DeviceName, L"\\Device\\NamedPipe");
61 Status = IoCreateDevice(DriverObject,
62 sizeof(NPFS_VCB),
63 &DeviceName,
64 FILE_DEVICE_NAMED_PIPE,
65 0,
66 FALSE,
67 &DeviceObject);
68 if (!NT_SUCCESS(Status))
69 {
70 DPRINT("Failed to create named pipe device! (Status %x)\n", Status);
71 return Status;
72 }
73
74 /* Initialize the device object */
75 DeviceObject->Flags |= DO_DIRECT_IO;
76 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
77
78 /* Initialize the Volume Control Block (VCB) */
79 Vcb = (PNPFS_VCB)DeviceObject->DeviceExtension;
80 InitializeListHead(&Vcb->PipeListHead);
81 InitializeListHead(&Vcb->ThreadListHead);
82 KeInitializeMutex(&Vcb->PipeListLock, 0);
83 Vcb->EmptyWaiterCount = 0;
84
85 /* set the size quotas */
86 Vcb->MinQuota = PAGE_SIZE;
87 Vcb->DefaultQuota = 8 * PAGE_SIZE;
88 Vcb->MaxQuota = 64 * PAGE_SIZE;
89
90 /* Create the device FCB */
91 Fcb = ExAllocatePoolWithTag(NonPagedPool, sizeof(NPFS_FCB), TAG_NPFS_FCB);
92 if (!Fcb)
93 {
94 DPRINT1("Out of memory for device FCB!\n");
95 IoDeleteDevice(DeviceObject);
96 return STATUS_INSUFFICIENT_RESOURCES;
97 }
98 Fcb->Type = FCB_DEVICE;
99 Fcb->Vcb = Vcb;
100 Fcb->RefCount = 1;
101 Vcb->DeviceFcb = Fcb;
102
103 /* Create the root directory FCB */
104 Fcb = ExAllocatePoolWithTag(NonPagedPool, sizeof(NPFS_FCB), TAG_NPFS_FCB);
105 if (!Fcb)
106 {
107 DPRINT1("Out of memory for root FCB!\n");
108 IoDeleteDevice(DeviceObject);
109 ExFreePoolWithTag(Vcb->DeviceFcb, TAG_NPFS_FCB);
110 return STATUS_INSUFFICIENT_RESOURCES;
111 }
112 Fcb->Type = FCB_DIRECTORY;
113 Fcb->Vcb = Vcb;
114 Fcb->RefCount = 1;
115 Vcb->RootFcb = Fcb;
116
117 return STATUS_SUCCESS;
118 }
119
120
121 FCB_TYPE
122 NpfsGetFcb(PFILE_OBJECT FileObject,
123 PNPFS_FCB *Fcb)
124 {
125 PNPFS_FCB LocalFcb = NULL;
126 FCB_TYPE FcbType = FCB_INVALID;
127
128 _SEH2_TRY
129 {
130 LocalFcb = (PNPFS_FCB)FileObject->FsContext;
131 FcbType = LocalFcb->Type;
132 }
133 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
134 {
135 LocalFcb = NULL;
136 FcbType = FCB_INVALID;
137 }
138 _SEH2_END;
139
140 *Fcb = LocalFcb;
141
142 return FcbType;
143 }
144
145
146 CCB_TYPE
147 NpfsGetCcb(PFILE_OBJECT FileObject,
148 PNPFS_CCB *Ccb)
149 {
150 PNPFS_CCB LocalCcb = NULL;
151 CCB_TYPE CcbType = CCB_INVALID;
152
153 _SEH2_TRY
154 {
155 LocalCcb = (PNPFS_CCB)FileObject->FsContext2;
156 CcbType = LocalCcb->Type;
157 }
158 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
159 {
160 LocalCcb = NULL;
161 CcbType = CCB_INVALID;
162 }
163 _SEH2_END;
164
165 *Ccb = LocalCcb;
166
167 return CcbType;
168 }
169
170 /* EOF */