Initial revision
[reactos.git] / reactos / drivers / fs / template / template.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: services/fs/template/template.c
5 * PURPOSE: Bare filesystem template
6 * PROGRAMMER: David Welch (welch@mcmail.com)
7 * UPDATE HISTORY:
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ddk/ntddk.h>
13 #include <internal/string.h>
14 #include <wstring.h>
15
16 #define NDEBUG
17 #include <internal/debug.h>
18
19 typedef struct
20 {
21 PDEVICE_OBJECT StorageDevice;
22 } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
23
24 /* GLOBALS *****************************************************************/
25
26 static PDRIVER_OBJECT DriverObject;
27
28 /* FUNCTIONS ****************************************************************/
29
30 NTSTATUS FsdCloseFile(PDEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject)
31 /*
32 * FUNCTION: Closes a file
33 */
34 {
35 }
36
37 NTSTATUS FsdOpenFile(PDEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject,
38 PWSTR FileName)
39 /*
40 * FUNCTION: Opens a file
41 */
42 {
43 }
44
45 BOOLEAN FsdHasFileSystem(PDEVICE_OBJECT DeviceToMount)
46 /*
47 * FUNCTION: Tests if the device contains a filesystem that can be mounted
48 * by this fsd
49 */
50 {
51 }
52
53 NTSTATUS FsdMountDevice(PDEVICE_EXTENSION DeviceExt,
54 PDEVICE_OBJECT DeviceToMount)
55 /*
56 * FUNCTION: Mounts the device
57 */
58 {
59 }
60
61
62 NTSTATUS FsdReadFile(PDEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject,
63 PVOID Buffer, ULONG Length, ULONG Offset)
64 /*
65 * FUNCTION: Reads data from a file
66 */
67 {
68 }
69
70
71 NTSTATUS FsdClose(PDEVICE_OBJECT DeviceObject, PIRP Irp)
72 {
73 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
74 PFILE_OBJECT FileObject = Stack->FileObject;
75 PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
76 NTSTATUS Status;
77
78 Status = FsdCloseFile(DeviceExtension,FileObject);
79
80 Irp->IoStatus.Status = Status;
81 Irp->IoStatus.Information = 0;
82
83 IoCompleteRequest(Irp, IO_NO_INCREMENT);
84 return(Status);
85 }
86
87 NTSTATUS FsdCreate(PDEVICE_OBJECT DeviceObject, PIRP Irp)
88 {
89 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
90 PFILE_OBJECT FileObject = Stack->FileObject;
91 NTSTATUS Status;
92 PDEVICE_EXTENSION DeviceExt;
93
94 DeviceExt = DeviceObject->DeviceExtension;
95 Status = FsdOpenFile(DeviceExt,FileObject,FileObject->FileName.Buffer);
96
97 Irp->IoStatus.Status = Status;
98 Irp->IoStatus.Information = 0;
99
100 IoCompleteRequest(Irp, IO_NO_INCREMENT);
101 return(Status);
102 }
103
104
105 NTSTATUS FsdWrite(PDEVICE_OBJECT DeviceObject, PIRP Irp)
106 {
107 DPRINT("FsdWrite(DeviceObject %x Irp %x)\n",DeviceObject,Irp);
108
109 Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
110 Irp->IoStatus.Information = 0;
111 return(STATUS_UNSUCCESSFUL);
112 }
113
114 NTSTATUS FsdRead(PDEVICE_OBJECT DeviceObject, PIRP Irp)
115 {
116 ULONG Length;
117 PVOID Buffer;
118 ULONG Offset;
119 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
120 PFILE_OBJECT FileObject = Stack->FileObject;
121 PDEVICE_EXTENSION DeviceExt = DeviceObject->DeviceExtension;
122 NTSTATUS Status;
123
124 DPRINT("FsdRead(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
125
126 Length = Stack->Parameters.Read.Length;
127 Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
128 Offset = Stack->Parameters.Read.ByteOffset.LowPart;
129
130 Status = FsdReadFile(DeviceExt,FileObject,Buffer,Length,Offset);
131
132 Irp->IoStatus.Status = Status;
133 Irp->IoStatus.Information = Length;
134 IoCompleteRequest(Irp,IO_NO_INCREMENT);
135 return(Status);
136 }
137
138
139 NTSTATUS FsdMount(PDEVICE_OBJECT DeviceToMount)
140 {
141 PDEVICE_OBJECT DeviceObject;
142 PDEVICE_EXTENSION DeviceExt;
143
144 IoCreateDevice(DriverObject,
145 sizeof(DEVICE_EXTENSION),
146 NULL,
147 FILE_DEVICE_FILE_SYSTEM,
148 0,
149 FALSE,
150 &DeviceObject);
151 DeviceObject->Flags = DeviceObject->Flags | DO_DIRECT_IO;
152 DeviceExt = (PVOID)DeviceObject->DeviceExtension;
153
154 FsdMountDevice(DeviceExt,DeviceToMount);
155
156 DeviceExt->StorageDevice = IoAttachDeviceToDeviceStack(DeviceObject,
157 DeviceToMount);
158 return(STATUS_SUCCESS);
159 }
160
161 NTSTATUS FsdFileSystemControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
162 {
163 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
164 PVPB vpb = Stack->Parameters.Mount.Vpb;
165 PDEVICE_OBJECT DeviceToMount = Stack->Parameters.Mount.DeviceObject;
166 NTSTATUS Status;
167
168 if (FsdHasFileSystem(DeviceToMount))
169 {
170 Status = FsdMount(DeviceToMount);
171 }
172 else
173 {
174 Status = STATUS_UNRECOGNIZED_VOLUME;
175 }
176
177 Irp->IoStatus.Status = Status;
178 Irp->IoStatus.Information = 0;
179
180 IoCompleteRequest(Irp, IO_NO_INCREMENT);
181 return(Status);
182 }
183
184 NTSTATUS DriverEntry(PDRIVER_OBJECT _DriverObject,
185 PUNICODE_STRING RegistryPath)
186 /*
187 * FUNCTION: Called by the system to initalize the driver
188 * ARGUMENTS:
189 * DriverObject = object describing this driver
190 * RegistryPath = path to our configuration entries
191 * RETURNS: Success or failure
192 */
193 {
194 PDEVICE_OBJECT DeviceObject;
195 NTSTATUS ret;
196 UNICODE_STRING ustr;
197 ANSI_STRING astr;
198
199 DbgPrint("Bare FSD Template 0.0.1\n");
200
201 DriverObject = _DriverObject;
202
203 RtlInitAnsiString(&astr,"\\Device\\BareFsd");
204 RtlAnsiStringToUnicodeString(&ustr,&astr,TRUE);
205 ret = IoCreateDevice(DriverObject,0,&ustr,
206 FILE_DEVICE_FILE_SYSTEM,0,FALSE,&DeviceObject);
207 if (ret!=STATUS_SUCCESS)
208 {
209 return(ret);
210 }
211
212 DeviceObject->Flags=0;
213 DriverObject->MajorFunction[IRP_MJ_CLOSE] = FsdClose;
214 DriverObject->MajorFunction[IRP_MJ_CREATE] = FsdCreate;
215 DriverObject->MajorFunction[IRP_MJ_READ] = FsdRead;
216 DriverObject->MajorFunction[IRP_MJ_WRITE] = FsdWrite;
217 DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =
218 FsdFileSystemControl;
219 DriverObject->DriverUnload = NULL;
220
221 IoRegisterFileSystem(DeviceObject);
222
223 return(STATUS_SUCCESS);
224 }
225