[ROSAPPS]
[reactos.git] / rosapps / templates / template / template.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: services/fs/template/template.c
23 * PURPOSE: Bare filesystem template
24 * PROGRAMMER: David Welch (welch@mcmail.com)
25 * UPDATE HISTORY:
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #include <ntddk.h>
31
32 #define NDEBUG
33 #include <debug.h>
34
35 typedef struct
36 {
37 PDEVICE_OBJECT StorageDevice;
38 } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
39
40 /* GLOBALS ******************************************************************/
41
42 static PDRIVER_OBJECT DriverObject;
43
44 /* FUNCTIONS ****************************************************************/
45
46 NTSTATUS NTAPI
47 FsdCloseFile(PDEVICE_EXTENSION DeviceExt,
48 PFILE_OBJECT FileObject)
49 /*
50 * FUNCTION: Closes a file
51 */
52 {
53 return(STATUS_SUCCESS);
54 }
55
56
57 NTSTATUS NTAPI
58 FsdOpenFile(PDEVICE_EXTENSION DeviceExt,
59 PFILE_OBJECT FileObject,
60 PWSTR FileName)
61 /*
62 * FUNCTION: Opens a file
63 */
64 {
65 return(STATUS_SUCCESS);
66 }
67
68
69 BOOLEAN NTAPI
70 FsdHasFileSystem(PDEVICE_OBJECT DeviceToMount)
71 /*
72 * FUNCTION: Tests if the device contains a filesystem that can be mounted
73 * by this fsd
74 */
75 {
76 return(TRUE);
77 }
78
79
80 NTSTATUS NTAPI
81 FsdMountDevice(PDEVICE_EXTENSION DeviceExt,
82 PDEVICE_OBJECT DeviceToMount)
83 /*
84 * FUNCTION: Mounts the device
85 */
86 {
87 return(STATUS_SUCCESS);
88 }
89
90
91 NTSTATUS NTAPI
92 FsdReadFile(PDEVICE_EXTENSION DeviceExt,
93 PFILE_OBJECT FileObject,
94 PVOID Buffer,
95 ULONG Length,
96 ULONG Offset)
97 /*
98 * FUNCTION: Reads data from a file
99 */
100 {
101 return(STATUS_SUCCESS);
102 }
103
104
105 NTSTATUS NTAPI
106 FsdClose(PDEVICE_OBJECT DeviceObject,
107 PIRP Irp)
108 {
109 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
110 PFILE_OBJECT FileObject = Stack->FileObject;
111 PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
112 NTSTATUS Status;
113
114 Status = FsdCloseFile(DeviceExtension,FileObject);
115
116 Irp->IoStatus.Status = Status;
117 Irp->IoStatus.Information = 0;
118
119 IoCompleteRequest(Irp, IO_NO_INCREMENT);
120 return(Status);
121 }
122
123
124 NTSTATUS NTAPI
125 FsdCreate(PDEVICE_OBJECT DeviceObject,
126 PIRP Irp)
127 {
128 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
129 PFILE_OBJECT FileObject = Stack->FileObject;
130 NTSTATUS Status;
131 PDEVICE_EXTENSION DeviceExt;
132
133 DeviceExt = DeviceObject->DeviceExtension;
134 Status = FsdOpenFile(DeviceExt,FileObject,FileObject->FileName.Buffer);
135
136 Irp->IoStatus.Status = Status;
137 Irp->IoStatus.Information = 0;
138
139 IoCompleteRequest(Irp, IO_NO_INCREMENT);
140 return(Status);
141 }
142
143
144 NTSTATUS NTAPI
145 FsdWrite(PDEVICE_OBJECT DeviceObject,
146 PIRP Irp)
147 {
148 DPRINT("FsdWrite(DeviceObject %x Irp %x)\n",DeviceObject,Irp);
149
150 Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
151 Irp->IoStatus.Information = 0;
152 return(STATUS_UNSUCCESSFUL);
153 }
154
155 NTSTATUS NTAPI
156 FsdRead(PDEVICE_OBJECT DeviceObject,
157 PIRP Irp)
158 {
159 ULONG Length;
160 PVOID Buffer;
161 ULONG Offset;
162 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
163 PFILE_OBJECT FileObject = Stack->FileObject;
164 PDEVICE_EXTENSION DeviceExt = DeviceObject->DeviceExtension;
165 NTSTATUS Status;
166
167 DPRINT("FsdRead(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
168
169 Length = Stack->Parameters.Read.Length;
170 Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
171 Offset = Stack->Parameters.Read.ByteOffset.LowPart;
172
173 Status = FsdReadFile(DeviceExt,FileObject,Buffer,Length,Offset);
174
175 Irp->IoStatus.Status = Status;
176 Irp->IoStatus.Information = Length;
177 IoCompleteRequest(Irp,IO_NO_INCREMENT);
178 return(Status);
179 }
180
181
182 NTSTATUS
183 FsdMount(PDEVICE_OBJECT DeviceToMount)
184 {
185 PDEVICE_OBJECT DeviceObject;
186 PDEVICE_EXTENSION DeviceExt;
187
188 IoCreateDevice(DriverObject,
189 sizeof(DEVICE_EXTENSION),
190 NULL,
191 FILE_DEVICE_FILE_SYSTEM,
192 0,
193 FALSE,
194 &DeviceObject);
195 DeviceObject->Flags = DeviceObject->Flags | DO_DIRECT_IO;
196 DeviceExt = (PVOID)DeviceObject->DeviceExtension;
197
198 FsdMountDevice(DeviceExt,
199 DeviceToMount);
200
201 DeviceExt->StorageDevice = DeviceToMount;
202 DeviceExt->StorageDevice->Vpb->DeviceObject = DeviceObject;
203 DeviceExt->StorageDevice->Vpb->RealDevice = DeviceExt->StorageDevice;
204 DeviceExt->StorageDevice->Vpb->Flags |= VPB_MOUNTED;
205 DeviceObject->StackSize = DeviceExt->StorageDevice->StackSize + 1;
206 DeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
207
208 return(STATUS_SUCCESS);
209 }
210
211
212 NTSTATUS NTAPI
213 FsdFileSystemControl(PDEVICE_OBJECT DeviceObject,
214 PIRP Irp)
215 {
216 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
217 PVPB vpb = Stack->Parameters.Mount.Vpb;
218 PDEVICE_OBJECT DeviceToMount = Stack->Parameters.Mount.DeviceObject;
219 NTSTATUS Status;
220
221 if (FsdHasFileSystem(DeviceToMount))
222 {
223 Status = FsdMount(DeviceToMount);
224 }
225 else
226 {
227 Status = STATUS_UNRECOGNIZED_VOLUME;
228 }
229
230 Irp->IoStatus.Status = Status;
231 Irp->IoStatus.Information = 0;
232
233 IoCompleteRequest(Irp, IO_NO_INCREMENT);
234 return(Status);
235 }
236
237
238 NTSTATUS NTAPI
239 DriverEntry(PDRIVER_OBJECT _DriverObject,
240 PUNICODE_STRING RegistryPath)
241 /*
242 * FUNCTION: Called by the system to initalize the driver
243 * ARGUMENTS:
244 * DriverObject = object describing this driver
245 * RegistryPath = path to our configuration entries
246 * RETURNS: Success or failure
247 */
248 {
249 PDEVICE_OBJECT DeviceObject;
250 NTSTATUS Status;
251 UNICODE_STRING DeviceName = RTL_CONSTANT_STRING(L"\\Device\\BareFsd");
252
253 DbgPrint("Bare FSD Template 0.0.1\n");
254
255 DriverObject = _DriverObject;
256
257 Status = IoCreateDevice(DriverObject,
258 0,
259 &DeviceName,
260 FILE_DEVICE_FILE_SYSTEM,
261 0,
262 FALSE,
263 &DeviceObject);
264 if (!NT_SUCCESS(Status))
265 {
266 return(Status);
267 }
268
269 DeviceObject->Flags=0;
270 DriverObject->MajorFunction[IRP_MJ_CLOSE] = FsdClose;
271 DriverObject->MajorFunction[IRP_MJ_CREATE] = FsdCreate;
272 DriverObject->MajorFunction[IRP_MJ_READ] = FsdRead;
273 DriverObject->MajorFunction[IRP_MJ_WRITE] = FsdWrite;
274 DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =
275 FsdFileSystemControl;
276 DriverObject->DriverUnload = NULL;
277
278 IoRegisterFileSystem(DeviceObject);
279
280 return(STATUS_SUCCESS);
281 }
282
283 /* EOF */