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