3 * Copyright (C) 2002, 2014 ReactOS Team
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/volume.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Eric Kohl
24 * Pierre Schweitzer (pierre@reactos.org)
27 /* INCLUDES *****************************************************************/
34 /* FUNCTIONS ****************************************************************/
37 NtfsGetFreeClusters(PDEVICE_EXTENSION DeviceExt
)
40 PFILE_RECORD_HEADER BitmapRecord
;
41 PNTFS_ATTR_CONTEXT DataContext
;
42 ULONGLONG BitmapDataSize
;
44 ULONGLONG FreeClusters
= 0;
48 DPRINT1("NtfsGetFreeClusters(%p)\n", DeviceExt
);
50 BitmapRecord
= ExAllocatePoolWithTag(NonPagedPool
,
51 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
53 if (BitmapRecord
== NULL
)
58 Status
= ReadFileRecord(DeviceExt
, NTFS_FILE_BITMAP
, BitmapRecord
);
59 if (!NT_SUCCESS(Status
))
61 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
65 Status
= FindAttribute(DeviceExt
, BitmapRecord
, AttributeData
, L
"", 0, &DataContext
);
66 if (!NT_SUCCESS(Status
))
68 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
72 BitmapDataSize
= AttributeDataLength(&DataContext
->Record
);
73 ASSERT((BitmapDataSize
* 8) >= DeviceExt
->NtfsInfo
.ClusterCount
);
74 BitmapData
= ExAllocatePoolWithTag(NonPagedPool
, ROUND_UP(BitmapDataSize
, DeviceExt
->NtfsInfo
.BytesPerSector
), TAG_NTFS
);
75 if (BitmapData
== NULL
)
77 ReleaseAttributeContext(DataContext
);
78 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
82 /* FIXME: Totally underoptimized! */
83 for (; Read
< BitmapDataSize
; Read
+= DeviceExt
->NtfsInfo
.BytesPerSector
)
85 ReadAttribute(DeviceExt
, DataContext
, Read
, (PCHAR
)((ULONG_PTR
)BitmapData
+ Read
), DeviceExt
->NtfsInfo
.BytesPerSector
);
87 ReleaseAttributeContext(DataContext
);
89 DPRINT1("Total clusters: %I64x\n", DeviceExt
->NtfsInfo
.ClusterCount
);
90 DPRINT1("Total clusters in bitmap: %I64x\n", BitmapDataSize
* 8);
91 DPRINT1("Diff in size: %I64d B\n", ((BitmapDataSize
* 8) - DeviceExt
->NtfsInfo
.ClusterCount
) * DeviceExt
->NtfsInfo
.SectorsPerCluster
* DeviceExt
->NtfsInfo
.BytesPerSector
);
93 RtlInitializeBitMap(&Bitmap
, (PULONG
)BitmapData
, DeviceExt
->NtfsInfo
.ClusterCount
);
94 FreeClusters
= RtlNumberOfClearBits(&Bitmap
);
96 ExFreePoolWithTag(BitmapData
, TAG_NTFS
);
97 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
104 NtfsGetFsVolumeInformation(PDEVICE_OBJECT DeviceObject
,
105 PFILE_FS_VOLUME_INFORMATION FsVolumeInfo
,
108 DPRINT("NtfsGetFsVolumeInformation() called\n");
109 DPRINT("FsVolumeInfo = %p\n", FsVolumeInfo
);
110 DPRINT("BufferLength %lu\n", *BufferLength
);
112 DPRINT("Vpb %p\n", DeviceObject
->Vpb
);
114 DPRINT("Required length %lu\n",
115 sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
);
116 DPRINT("LabelLength %hu\n",
117 DeviceObject
->Vpb
->VolumeLabelLength
);
118 DPRINT("Label %.*S\n",
119 DeviceObject
->Vpb
->VolumeLabelLength
/ sizeof(WCHAR
),
120 DeviceObject
->Vpb
->VolumeLabel
);
122 if (*BufferLength
< sizeof(FILE_FS_VOLUME_INFORMATION
))
123 return STATUS_INFO_LENGTH_MISMATCH
;
125 if (*BufferLength
< (sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
))
126 return STATUS_BUFFER_OVERFLOW
;
129 FsVolumeInfo
->VolumeSerialNumber
= DeviceObject
->Vpb
->SerialNumber
;
130 FsVolumeInfo
->VolumeLabelLength
= DeviceObject
->Vpb
->VolumeLabelLength
;
131 memcpy(FsVolumeInfo
->VolumeLabel
,
132 DeviceObject
->Vpb
->VolumeLabel
,
133 DeviceObject
->Vpb
->VolumeLabelLength
);
136 FsVolumeInfo
->VolumeCreationTime
.QuadPart
= 0;
137 FsVolumeInfo
->SupportsObjects
= FALSE
;
139 *BufferLength
-= (sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
);
141 DPRINT("BufferLength %lu\n", *BufferLength
);
142 DPRINT("NtfsGetFsVolumeInformation() done\n");
144 return STATUS_SUCCESS
;
150 NtfsGetFsAttributeInformation(PDEVICE_EXTENSION DeviceExt
,
151 PFILE_FS_ATTRIBUTE_INFORMATION FsAttributeInfo
,
154 UNREFERENCED_PARAMETER(DeviceExt
);
156 DPRINT("NtfsGetFsAttributeInformation()\n");
157 DPRINT("FsAttributeInfo = %p\n", FsAttributeInfo
);
158 DPRINT("BufferLength %lu\n", *BufferLength
);
159 DPRINT("Required length %lu\n", (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8));
161 if (*BufferLength
< sizeof (FILE_FS_ATTRIBUTE_INFORMATION
))
162 return STATUS_INFO_LENGTH_MISMATCH
;
164 if (*BufferLength
< (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8))
165 return STATUS_BUFFER_OVERFLOW
;
167 FsAttributeInfo
->FileSystemAttributes
=
168 FILE_CASE_PRESERVED_NAMES
| FILE_UNICODE_ON_DISK
| FILE_READ_ONLY_VOLUME
;
169 FsAttributeInfo
->MaximumComponentNameLength
= 255;
170 FsAttributeInfo
->FileSystemNameLength
= 8;
172 memcpy(FsAttributeInfo
->FileSystemName
, L
"NTFS", 8);
174 DPRINT("Finished NtfsGetFsAttributeInformation()\n");
176 *BufferLength
-= (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8);
177 DPRINT("BufferLength %lu\n", *BufferLength
);
179 return STATUS_SUCCESS
;
185 NtfsGetFsSizeInformation(PDEVICE_OBJECT DeviceObject
,
186 PFILE_FS_SIZE_INFORMATION FsSizeInfo
,
189 PDEVICE_EXTENSION DeviceExt
;
190 NTSTATUS Status
= STATUS_SUCCESS
;
192 DPRINT("NtfsGetFsSizeInformation()\n");
193 DPRINT("FsSizeInfo = %p\n", FsSizeInfo
);
195 if (*BufferLength
< sizeof(FILE_FS_SIZE_INFORMATION
))
196 return STATUS_BUFFER_OVERFLOW
;
198 DeviceExt
= DeviceObject
->DeviceExtension
;
200 FsSizeInfo
->AvailableAllocationUnits
.QuadPart
= NtfsGetFreeClusters(DeviceExt
);
201 FsSizeInfo
->TotalAllocationUnits
.QuadPart
= DeviceExt
->NtfsInfo
.ClusterCount
;
202 FsSizeInfo
->SectorsPerAllocationUnit
= DeviceExt
->NtfsInfo
.SectorsPerCluster
;
203 FsSizeInfo
->BytesPerSector
= DeviceExt
->NtfsInfo
.BytesPerSector
;
205 DPRINT("Finished NtfsGetFsSizeInformation()\n");
206 if (NT_SUCCESS(Status
))
207 *BufferLength
-= sizeof(FILE_FS_SIZE_INFORMATION
);
215 NtfsGetFsDeviceInformation(PDEVICE_OBJECT DeviceObject
,
216 PFILE_FS_DEVICE_INFORMATION FsDeviceInfo
,
219 DPRINT("NtfsGetFsDeviceInformation()\n");
220 DPRINT("FsDeviceInfo = %p\n", FsDeviceInfo
);
221 DPRINT("BufferLength %lu\n", *BufferLength
);
222 DPRINT("Required length %lu\n", sizeof(FILE_FS_DEVICE_INFORMATION
));
224 if (*BufferLength
< sizeof(FILE_FS_DEVICE_INFORMATION
))
225 return STATUS_BUFFER_OVERFLOW
;
227 FsDeviceInfo
->DeviceType
= FILE_DEVICE_DISK
;
228 FsDeviceInfo
->Characteristics
= DeviceObject
->Characteristics
;
230 DPRINT("NtfsGetFsDeviceInformation() finished.\n");
232 *BufferLength
-= sizeof(FILE_FS_DEVICE_INFORMATION
);
233 DPRINT("BufferLength %lu\n", *BufferLength
);
235 return STATUS_SUCCESS
;
240 NtfsQueryVolumeInformation(PNTFS_IRP_CONTEXT IrpContext
)
243 PDEVICE_OBJECT DeviceObject
;
244 FS_INFORMATION_CLASS FsInformationClass
;
245 PIO_STACK_LOCATION Stack
;
246 NTSTATUS Status
= STATUS_SUCCESS
;
249 PDEVICE_EXTENSION DeviceExt
;
251 DPRINT("NtfsQueryVolumeInformation() called\n");
255 Irp
= IrpContext
->Irp
;
256 DeviceObject
= IrpContext
->DeviceObject
;
257 DeviceExt
= DeviceObject
->DeviceExtension
;
258 Stack
= IrpContext
->Stack
;
260 if (!ExAcquireResourceSharedLite(&DeviceExt
->DirResource
,
261 BooleanFlagOn(IrpContext
->Flags
, IRPCONTEXT_CANWAIT
)))
263 return NtfsMarkIrpContextForQueue(IrpContext
);
266 FsInformationClass
= Stack
->Parameters
.QueryVolume
.FsInformationClass
;
267 BufferLength
= Stack
->Parameters
.QueryVolume
.Length
;
268 SystemBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
269 RtlZeroMemory(SystemBuffer
, BufferLength
);
271 DPRINT("FsInformationClass %d\n", FsInformationClass
);
272 DPRINT("SystemBuffer %p\n", SystemBuffer
);
274 switch (FsInformationClass
)
276 case FileFsVolumeInformation
:
277 Status
= NtfsGetFsVolumeInformation(DeviceObject
,
282 case FileFsAttributeInformation
:
283 Status
= NtfsGetFsAttributeInformation(DeviceObject
->DeviceExtension
,
288 case FileFsSizeInformation
:
289 Status
= NtfsGetFsSizeInformation(DeviceObject
,
294 case FileFsDeviceInformation
:
295 Status
= NtfsGetFsDeviceInformation(DeviceObject
,
301 Status
= STATUS_NOT_SUPPORTED
;
304 ExReleaseResourceLite(&DeviceExt
->DirResource
);
306 if (NT_SUCCESS(Status
))
307 Irp
->IoStatus
.Information
=
308 Stack
->Parameters
.QueryVolume
.Length
- BufferLength
;
310 Irp
->IoStatus
.Information
= 0;
317 NtfsSetVolumeInformation(PNTFS_IRP_CONTEXT IrpContext
)
321 DPRINT("NtfsSetVolumeInformation() called\n");
325 Irp
= IrpContext
->Irp
;
326 Irp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
327 Irp
->IoStatus
.Information
= 0;
329 return STATUS_NOT_SUPPORTED
;