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)
10 /* INCLUDES *****************************************************************/
12 #include <ddk/ntddk.h>
13 #include <internal/string.h>
17 #include <internal/debug.h>
21 PDEVICE_OBJECT StorageDevice
;
22 } DEVICE_EXTENSION
, *PDEVICE_EXTENSION
;
24 /* GLOBALS *****************************************************************/
26 static PDRIVER_OBJECT DriverObject
;
28 /* FUNCTIONS ****************************************************************/
30 NTSTATUS
FsdCloseFile(PDEVICE_EXTENSION DeviceExt
, PFILE_OBJECT FileObject
)
32 * FUNCTION: Closes a file
37 NTSTATUS
FsdOpenFile(PDEVICE_EXTENSION DeviceExt
, PFILE_OBJECT FileObject
,
40 * FUNCTION: Opens a file
45 BOOLEAN
FsdHasFileSystem(PDEVICE_OBJECT DeviceToMount
)
47 * FUNCTION: Tests if the device contains a filesystem that can be mounted
53 NTSTATUS
FsdMountDevice(PDEVICE_EXTENSION DeviceExt
,
54 PDEVICE_OBJECT DeviceToMount
)
56 * FUNCTION: Mounts the device
62 NTSTATUS
FsdReadFile(PDEVICE_EXTENSION DeviceExt
, PFILE_OBJECT FileObject
,
63 PVOID Buffer
, ULONG Length
, ULONG Offset
)
65 * FUNCTION: Reads data from a file
71 NTSTATUS
FsdClose(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
73 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
74 PFILE_OBJECT FileObject
= Stack
->FileObject
;
75 PDEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
78 Status
= FsdCloseFile(DeviceExtension
,FileObject
);
80 Irp
->IoStatus
.Status
= Status
;
81 Irp
->IoStatus
.Information
= 0;
83 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
87 NTSTATUS
FsdCreate(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
89 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
90 PFILE_OBJECT FileObject
= Stack
->FileObject
;
92 PDEVICE_EXTENSION DeviceExt
;
94 DeviceExt
= DeviceObject
->DeviceExtension
;
95 Status
= FsdOpenFile(DeviceExt
,FileObject
,FileObject
->FileName
.Buffer
);
97 Irp
->IoStatus
.Status
= Status
;
98 Irp
->IoStatus
.Information
= 0;
100 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
105 NTSTATUS
FsdWrite(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
107 DPRINT("FsdWrite(DeviceObject %x Irp %x)\n",DeviceObject
,Irp
);
109 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
110 Irp
->IoStatus
.Information
= 0;
111 return(STATUS_UNSUCCESSFUL
);
114 NTSTATUS
FsdRead(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
119 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
120 PFILE_OBJECT FileObject
= Stack
->FileObject
;
121 PDEVICE_EXTENSION DeviceExt
= DeviceObject
->DeviceExtension
;
124 DPRINT("FsdRead(DeviceObject %x, Irp %x)\n",DeviceObject
,Irp
);
126 Length
= Stack
->Parameters
.Read
.Length
;
127 Buffer
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
128 Offset
= Stack
->Parameters
.Read
.ByteOffset
.LowPart
;
130 Status
= FsdReadFile(DeviceExt
,FileObject
,Buffer
,Length
,Offset
);
132 Irp
->IoStatus
.Status
= Status
;
133 Irp
->IoStatus
.Information
= Length
;
134 IoCompleteRequest(Irp
,IO_NO_INCREMENT
);
139 NTSTATUS
FsdMount(PDEVICE_OBJECT DeviceToMount
)
141 PDEVICE_OBJECT DeviceObject
;
142 PDEVICE_EXTENSION DeviceExt
;
144 IoCreateDevice(DriverObject
,
145 sizeof(DEVICE_EXTENSION
),
147 FILE_DEVICE_FILE_SYSTEM
,
151 DeviceObject
->Flags
= DeviceObject
->Flags
| DO_DIRECT_IO
;
152 DeviceExt
= (PVOID
)DeviceObject
->DeviceExtension
;
154 FsdMountDevice(DeviceExt
,DeviceToMount
);
156 DeviceExt
->StorageDevice
= IoAttachDeviceToDeviceStack(DeviceObject
,
158 return(STATUS_SUCCESS
);
161 NTSTATUS
FsdFileSystemControl(PDEVICE_OBJECT DeviceObject
, PIRP Irp
)
163 PIO_STACK_LOCATION Stack
= IoGetCurrentIrpStackLocation(Irp
);
164 PVPB vpb
= Stack
->Parameters
.Mount
.Vpb
;
165 PDEVICE_OBJECT DeviceToMount
= Stack
->Parameters
.Mount
.DeviceObject
;
168 if (FsdHasFileSystem(DeviceToMount
))
170 Status
= FsdMount(DeviceToMount
);
174 Status
= STATUS_UNRECOGNIZED_VOLUME
;
177 Irp
->IoStatus
.Status
= Status
;
178 Irp
->IoStatus
.Information
= 0;
180 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
184 NTSTATUS
DriverEntry(PDRIVER_OBJECT _DriverObject
,
185 PUNICODE_STRING RegistryPath
)
187 * FUNCTION: Called by the system to initalize the driver
189 * DriverObject = object describing this driver
190 * RegistryPath = path to our configuration entries
191 * RETURNS: Success or failure
194 PDEVICE_OBJECT DeviceObject
;
199 DbgPrint("Bare FSD Template 0.0.1\n");
201 DriverObject
= _DriverObject
;
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
)
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
;
221 IoRegisterFileSystem(DeviceObject
);
223 return(STATUS_SUCCESS
);