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
, NULL
);
66 if (!NT_SUCCESS(Status
))
68 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
72 BitmapDataSize
= AttributeDataLength(DataContext
->pRecord
);
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
);
103 * NtfsAllocateClusters
104 * Allocates a run of clusters. The run allocated might be smaller than DesiredClusters.
107 NtfsAllocateClusters(PDEVICE_EXTENSION DeviceExt
,
108 ULONG FirstDesiredCluster
,
109 ULONG DesiredClusters
,
110 PULONG FirstAssignedCluster
,
111 PULONG AssignedClusters
)
114 PFILE_RECORD_HEADER BitmapRecord
;
115 PNTFS_ATTR_CONTEXT DataContext
;
116 ULONGLONG BitmapDataSize
;
118 ULONGLONG FreeClusters
= 0;
123 DPRINT1("NtfsAllocateClusters(%p, %lu, %lu, %p, %p)\n", DeviceExt
, FirstDesiredCluster
, DesiredClusters
, FirstAssignedCluster
, AssignedClusters
);
125 BitmapRecord
= ExAllocatePoolWithTag(NonPagedPool
,
126 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
128 if (BitmapRecord
== NULL
)
130 return STATUS_INSUFFICIENT_RESOURCES
;
133 Status
= ReadFileRecord(DeviceExt
, NTFS_FILE_BITMAP
, BitmapRecord
);
134 if (!NT_SUCCESS(Status
))
136 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
140 Status
= FindAttribute(DeviceExt
, BitmapRecord
, AttributeData
, L
"", 0, &DataContext
, NULL
);
141 if (!NT_SUCCESS(Status
))
143 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
147 BitmapDataSize
= AttributeDataLength(DataContext
->pRecord
);
148 BitmapDataSize
= min(BitmapDataSize
, 0xffffffff);
149 ASSERT((BitmapDataSize
* 8) >= DeviceExt
->NtfsInfo
.ClusterCount
);
150 BitmapData
= ExAllocatePoolWithTag(NonPagedPool
, ROUND_UP(BitmapDataSize
, DeviceExt
->NtfsInfo
.BytesPerSector
), TAG_NTFS
);
151 if (BitmapData
== NULL
)
153 ReleaseAttributeContext(DataContext
);
154 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
155 return STATUS_INSUFFICIENT_RESOURCES
;
158 DPRINT1("Total clusters: %I64x\n", DeviceExt
->NtfsInfo
.ClusterCount
);
159 DPRINT1("Total clusters in bitmap: %I64x\n", BitmapDataSize
* 8);
160 DPRINT1("Diff in size: %I64d B\n", ((BitmapDataSize
* 8) - DeviceExt
->NtfsInfo
.ClusterCount
) * DeviceExt
->NtfsInfo
.SectorsPerCluster
* DeviceExt
->NtfsInfo
.BytesPerSector
);
162 ReadAttribute(DeviceExt
, DataContext
, 0, (PCHAR
)BitmapData
, (ULONG
)BitmapDataSize
);
164 RtlInitializeBitMap(&Bitmap
, (PULONG
)BitmapData
, DeviceExt
->NtfsInfo
.ClusterCount
);
165 FreeClusters
= RtlNumberOfClearBits(&Bitmap
);
167 if (FreeClusters
< DesiredClusters
)
169 ReleaseAttributeContext(DataContext
);
171 ExFreePoolWithTag(BitmapData
, TAG_NTFS
);
172 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
173 return STATUS_DISK_FULL
;
176 // TODO: Observe MFT reservation zone
178 // Can we get one contiguous run?
179 AssignedRun
= RtlFindClearBitsAndSet(&Bitmap
, DesiredClusters
, FirstDesiredCluster
);
181 if (AssignedRun
!= 0xFFFFFFFF)
183 *FirstAssignedCluster
= AssignedRun
;
184 *AssignedClusters
= DesiredClusters
;
188 // we can't get one contiguous run
189 *AssignedClusters
= RtlFindNextForwardRunClear(&Bitmap
, FirstDesiredCluster
, FirstAssignedCluster
);
191 if (*AssignedClusters
== 0)
193 // we couldn't find any runs starting at DesiredFirstCluster
194 *AssignedClusters
= RtlFindLongestRunClear(&Bitmap
, FirstAssignedCluster
);
199 Status
= WriteAttribute(DeviceExt
, DataContext
, 0, BitmapData
, (ULONG
)BitmapDataSize
, &LengthWritten
);
201 ReleaseAttributeContext(DataContext
);
203 ExFreePoolWithTag(BitmapData
, TAG_NTFS
);
204 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
211 NtfsGetFsVolumeInformation(PDEVICE_OBJECT DeviceObject
,
212 PFILE_FS_VOLUME_INFORMATION FsVolumeInfo
,
215 DPRINT("NtfsGetFsVolumeInformation() called\n");
216 DPRINT("FsVolumeInfo = %p\n", FsVolumeInfo
);
217 DPRINT("BufferLength %lu\n", *BufferLength
);
219 DPRINT("Vpb %p\n", DeviceObject
->Vpb
);
221 DPRINT("Required length %lu\n",
222 sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
);
223 DPRINT("LabelLength %hu\n",
224 DeviceObject
->Vpb
->VolumeLabelLength
);
225 DPRINT("Label %.*S\n",
226 DeviceObject
->Vpb
->VolumeLabelLength
/ sizeof(WCHAR
),
227 DeviceObject
->Vpb
->VolumeLabel
);
229 if (*BufferLength
< sizeof(FILE_FS_VOLUME_INFORMATION
))
230 return STATUS_INFO_LENGTH_MISMATCH
;
232 if (*BufferLength
< (sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
))
233 return STATUS_BUFFER_OVERFLOW
;
236 FsVolumeInfo
->VolumeSerialNumber
= DeviceObject
->Vpb
->SerialNumber
;
237 FsVolumeInfo
->VolumeLabelLength
= DeviceObject
->Vpb
->VolumeLabelLength
;
238 memcpy(FsVolumeInfo
->VolumeLabel
,
239 DeviceObject
->Vpb
->VolumeLabel
,
240 DeviceObject
->Vpb
->VolumeLabelLength
);
243 FsVolumeInfo
->VolumeCreationTime
.QuadPart
= 0;
244 FsVolumeInfo
->SupportsObjects
= FALSE
;
246 *BufferLength
-= (sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
);
248 DPRINT("BufferLength %lu\n", *BufferLength
);
249 DPRINT("NtfsGetFsVolumeInformation() done\n");
251 return STATUS_SUCCESS
;
257 NtfsGetFsAttributeInformation(PDEVICE_EXTENSION DeviceExt
,
258 PFILE_FS_ATTRIBUTE_INFORMATION FsAttributeInfo
,
261 UNREFERENCED_PARAMETER(DeviceExt
);
263 DPRINT("NtfsGetFsAttributeInformation()\n");
264 DPRINT("FsAttributeInfo = %p\n", FsAttributeInfo
);
265 DPRINT("BufferLength %lu\n", *BufferLength
);
266 DPRINT("Required length %lu\n", (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8));
268 if (*BufferLength
< sizeof (FILE_FS_ATTRIBUTE_INFORMATION
))
269 return STATUS_INFO_LENGTH_MISMATCH
;
271 if (*BufferLength
< (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8))
272 return STATUS_BUFFER_OVERFLOW
;
274 FsAttributeInfo
->FileSystemAttributes
=
275 FILE_CASE_PRESERVED_NAMES
| FILE_UNICODE_ON_DISK
| FILE_READ_ONLY_VOLUME
;
276 FsAttributeInfo
->MaximumComponentNameLength
= 255;
277 FsAttributeInfo
->FileSystemNameLength
= 8;
279 memcpy(FsAttributeInfo
->FileSystemName
, L
"NTFS", 8);
281 DPRINT("Finished NtfsGetFsAttributeInformation()\n");
283 *BufferLength
-= (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8);
284 DPRINT("BufferLength %lu\n", *BufferLength
);
286 return STATUS_SUCCESS
;
292 NtfsGetFsSizeInformation(PDEVICE_OBJECT DeviceObject
,
293 PFILE_FS_SIZE_INFORMATION FsSizeInfo
,
296 PDEVICE_EXTENSION DeviceExt
;
297 NTSTATUS Status
= STATUS_SUCCESS
;
299 DPRINT("NtfsGetFsSizeInformation()\n");
300 DPRINT("FsSizeInfo = %p\n", FsSizeInfo
);
302 if (*BufferLength
< sizeof(FILE_FS_SIZE_INFORMATION
))
303 return STATUS_BUFFER_OVERFLOW
;
305 DeviceExt
= DeviceObject
->DeviceExtension
;
307 FsSizeInfo
->AvailableAllocationUnits
.QuadPart
= NtfsGetFreeClusters(DeviceExt
);
308 FsSizeInfo
->TotalAllocationUnits
.QuadPart
= DeviceExt
->NtfsInfo
.ClusterCount
;
309 FsSizeInfo
->SectorsPerAllocationUnit
= DeviceExt
->NtfsInfo
.SectorsPerCluster
;
310 FsSizeInfo
->BytesPerSector
= DeviceExt
->NtfsInfo
.BytesPerSector
;
312 DPRINT("Finished NtfsGetFsSizeInformation()\n");
313 if (NT_SUCCESS(Status
))
314 *BufferLength
-= sizeof(FILE_FS_SIZE_INFORMATION
);
322 NtfsGetFsDeviceInformation(PDEVICE_OBJECT DeviceObject
,
323 PFILE_FS_DEVICE_INFORMATION FsDeviceInfo
,
326 DPRINT("NtfsGetFsDeviceInformation()\n");
327 DPRINT("FsDeviceInfo = %p\n", FsDeviceInfo
);
328 DPRINT("BufferLength %lu\n", *BufferLength
);
329 DPRINT("Required length %lu\n", sizeof(FILE_FS_DEVICE_INFORMATION
));
331 if (*BufferLength
< sizeof(FILE_FS_DEVICE_INFORMATION
))
332 return STATUS_BUFFER_OVERFLOW
;
334 FsDeviceInfo
->DeviceType
= FILE_DEVICE_DISK
;
335 FsDeviceInfo
->Characteristics
= DeviceObject
->Characteristics
;
337 DPRINT("NtfsGetFsDeviceInformation() finished.\n");
339 *BufferLength
-= sizeof(FILE_FS_DEVICE_INFORMATION
);
340 DPRINT("BufferLength %lu\n", *BufferLength
);
342 return STATUS_SUCCESS
;
347 NtfsQueryVolumeInformation(PNTFS_IRP_CONTEXT IrpContext
)
350 PDEVICE_OBJECT DeviceObject
;
351 FS_INFORMATION_CLASS FsInformationClass
;
352 PIO_STACK_LOCATION Stack
;
353 NTSTATUS Status
= STATUS_SUCCESS
;
356 PDEVICE_EXTENSION DeviceExt
;
358 DPRINT("NtfsQueryVolumeInformation() called\n");
362 Irp
= IrpContext
->Irp
;
363 DeviceObject
= IrpContext
->DeviceObject
;
364 DeviceExt
= DeviceObject
->DeviceExtension
;
365 Stack
= IrpContext
->Stack
;
367 if (!ExAcquireResourceSharedLite(&DeviceExt
->DirResource
,
368 BooleanFlagOn(IrpContext
->Flags
, IRPCONTEXT_CANWAIT
)))
370 return NtfsMarkIrpContextForQueue(IrpContext
);
373 FsInformationClass
= Stack
->Parameters
.QueryVolume
.FsInformationClass
;
374 BufferLength
= Stack
->Parameters
.QueryVolume
.Length
;
375 SystemBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
376 RtlZeroMemory(SystemBuffer
, BufferLength
);
378 DPRINT("FsInformationClass %d\n", FsInformationClass
);
379 DPRINT("SystemBuffer %p\n", SystemBuffer
);
381 switch (FsInformationClass
)
383 case FileFsVolumeInformation
:
384 Status
= NtfsGetFsVolumeInformation(DeviceObject
,
389 case FileFsAttributeInformation
:
390 Status
= NtfsGetFsAttributeInformation(DeviceObject
->DeviceExtension
,
395 case FileFsSizeInformation
:
396 Status
= NtfsGetFsSizeInformation(DeviceObject
,
401 case FileFsDeviceInformation
:
402 Status
= NtfsGetFsDeviceInformation(DeviceObject
,
408 Status
= STATUS_NOT_SUPPORTED
;
411 ExReleaseResourceLite(&DeviceExt
->DirResource
);
413 if (NT_SUCCESS(Status
))
414 Irp
->IoStatus
.Information
=
415 Stack
->Parameters
.QueryVolume
.Length
- BufferLength
;
417 Irp
->IoStatus
.Information
= 0;
424 NtfsSetVolumeInformation(PNTFS_IRP_CONTEXT IrpContext
)
428 DPRINT("NtfsSetVolumeInformation() called\n");
432 Irp
= IrpContext
->Irp
;
433 Irp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
434 Irp
->IoStatus
.Information
= 0;
436 return STATUS_NOT_SUPPORTED
;