a9bac494387204c14578da7677f6cae2358513c3
[reactos.git] / reactos / drivers / fs / ext2 / super.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: services/fs/ext2/super.c
5 * PURPOSE: ext2 filesystem
6 * PROGRAMMER: David Welch (welch@mcmail.com)
7 * UPDATE HISTORY:
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ddk/ntddk.h>
13 #include <wchar.h>
14 #include <internal/string.h>
15
16 #define NDEBUG
17 #include <internal/debug.h>
18
19 #include "ext2fs.h"
20
21 /* GLOBALS *****************************************************************/
22
23 static PDRIVER_OBJECT DriverObject;
24
25 /* FUNCTIONS ****************************************************************/
26
27 NTSTATUS Ext2Close(PDEVICE_OBJECT DeviceObject, PIRP Irp)
28 {
29 PIO_STACK_LOCATION Stack;
30 PFILE_OBJECT FileObject;
31 PDEVICE_EXTENSION DeviceExtension;
32 NTSTATUS Status;
33
34 DPRINT("Ext2Close(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
35
36 Stack = IoGetCurrentIrpStackLocation(Irp);
37 FileObject = Stack->FileObject;
38 DeviceExtension = DeviceObject->DeviceExtension;
39
40 Irp->IoStatus.Status = Status;
41 Irp->IoStatus.Information = 0;
42
43 IoCompleteRequest(Irp, IO_NO_INCREMENT);
44 return(Status);
45 }
46
47 NTSTATUS Ext2Write(PDEVICE_OBJECT DeviceObject, PIRP Irp)
48 {
49 DPRINT("FsdWrite(DeviceObject %x Irp %x)\n",DeviceObject,Irp);
50
51 Irp->IoStatus.Status = STATUS_UNSUCCESSFUL;
52 Irp->IoStatus.Information = 0;
53 return(STATUS_UNSUCCESSFUL);
54 }
55
56 NTSTATUS Ext2Read(PDEVICE_OBJECT DeviceObject, PIRP Irp)
57 {
58 ULONG Length;
59 PVOID Buffer;
60 LARGE_INTEGER Offset;
61 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
62 PFILE_OBJECT FileObject = Stack->FileObject;
63 PDEVICE_EXTENSION DeviceExt = DeviceObject->DeviceExtension;
64 NTSTATUS Status;
65
66 DPRINT("FsdRead(DeviceObject %x, Irp %x)\n",DeviceObject,Irp);
67
68 Length = Stack->Parameters.Read.Length;
69 Buffer = MmGetSystemAddressForMdl(Irp->MdlAddress);
70 Offset = Stack->Parameters.Read.ByteOffset;
71
72 Status = Ext2ReadFile(DeviceExt,FileObject,Buffer,Length,Offset);
73
74 Irp->IoStatus.Status = Status;
75 Irp->IoStatus.Information = Length;
76 IoCompleteRequest(Irp,IO_NO_INCREMENT);
77
78 return(Status);
79 }
80
81
82 NTSTATUS Ext2Mount(PDEVICE_OBJECT DeviceToMount)
83 {
84 PDEVICE_OBJECT DeviceObject;
85 PDEVICE_EXTENSION DeviceExt;
86 PVOID BlockBuffer;
87 struct ext2_super_block* superblock;
88
89 DPRINT("Ext2Mount(DeviceToMount %x)\n",DeviceToMount);
90
91 BlockBuffer = ExAllocatePool(NonPagedPool,BLOCKSIZE);
92 Ext2ReadSectors(DeviceToMount,
93 1,
94 1,
95 BlockBuffer);
96 superblock = BlockBuffer;
97
98 if (superblock->s_magic != EXT2_SUPER_MAGIC)
99 {
100 ExFreePool(BlockBuffer);
101 return(STATUS_UNRECOGNIZED_VOLUME);
102 }
103 DPRINT("Volume recognized\n");
104 DPRINT("s_inodes_count %d\n",superblock->s_inodes_count);
105 DPRINT("s_blocks_count %d\n",superblock->s_blocks_count);
106
107 IoCreateDevice(DriverObject,
108 sizeof(DEVICE_EXTENSION),
109 NULL,
110 FILE_DEVICE_FILE_SYSTEM,
111 0,
112 FALSE,
113 &DeviceObject);
114 DeviceObject->Flags = DeviceObject->Flags | DO_DIRECT_IO;
115 DeviceExt = (PVOID)DeviceObject->DeviceExtension;
116
117 DeviceExt->StorageDevice = IoAttachDeviceToDeviceStack(DeviceObject,
118 DeviceToMount);
119 DeviceExt->superblock = superblock;
120
121 return(STATUS_SUCCESS);
122 }
123
124 NTSTATUS Ext2FileSystemControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
125 {
126 PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
127 PVPB vpb = Stack->Parameters.Mount.Vpb;
128 PDEVICE_OBJECT DeviceToMount = Stack->Parameters.Mount.DeviceObject;
129 NTSTATUS Status;
130
131 Status = Ext2Mount(DeviceToMount);
132
133 Irp->IoStatus.Status = Status;
134 Irp->IoStatus.Information = 0;
135
136 IoCompleteRequest(Irp, IO_NO_INCREMENT);
137 return(Status);
138 }
139
140 NTSTATUS DriverEntry(PDRIVER_OBJECT _DriverObject,
141 PUNICODE_STRING RegistryPath)
142 /*
143 * FUNCTION: Called by the system to initalize the driver
144 * ARGUMENTS:
145 * DriverObject = object describing this driver
146 * RegistryPath = path to our configuration entries
147 * RETURNS: Success or failure
148 */
149 {
150 PDEVICE_OBJECT DeviceObject;
151 NTSTATUS ret;
152 UNICODE_STRING DeviceNameU;
153 ANSI_STRING DeviceNameA;
154
155 DbgPrint("Ext2 FSD 0.0.1\n");
156
157 DriverObject = _DriverObject;
158
159 RtlInitAnsiString(&DeviceNameA,"\\Device\\Ext2Fsd");
160 RtlAnsiStringToUnicodeString(&DeviceNameU,&DeviceNameA,TRUE);
161 ret = IoCreateDevice(DriverObject,
162 0,
163 &DeviceNameU,
164 FILE_DEVICE_FILE_SYSTEM,
165 0,
166 FALSE,
167 &DeviceObject);
168 if (ret!=STATUS_SUCCESS)
169 {
170 return(ret);
171 }
172
173 DeviceObject->Flags=0;
174 DriverObject->MajorFunction[IRP_MJ_CLOSE] = Ext2Close;
175 DriverObject->MajorFunction[IRP_MJ_CREATE] = Ext2Create;
176 DriverObject->MajorFunction[IRP_MJ_READ] = Ext2Read;
177 DriverObject->MajorFunction[IRP_MJ_WRITE] = Ext2Write;
178 DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =
179 Ext2FileSystemControl;
180 DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL]=
181 Ext2DirectoryControl;
182 DriverObject->DriverUnload = NULL;
183
184 DPRINT("DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] %x\n",
185 DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL]);
186 DPRINT("IRP_MJ_DIRECTORY_CONTROL %d\n",IRP_MJ_DIRECTORY_CONTROL);
187
188 IoRegisterFileSystem(DeviceObject);
189
190 return(STATUS_SUCCESS);
191 }
192