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
->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
);
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;
121 DPRINT1("NtfsAllocateClusters(%p, %lu, %lu, %p, %p)\n", DeviceExt
, FirstDesiredCluster
, DesiredClusters
, FirstAssignedCluster
, AssignedClusters
);
123 BitmapRecord
= ExAllocatePoolWithTag(NonPagedPool
,
124 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
126 if (BitmapRecord
== NULL
)
131 Status
= ReadFileRecord(DeviceExt
, NTFS_FILE_BITMAP
, BitmapRecord
);
132 if (!NT_SUCCESS(Status
))
134 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
138 Status
= FindAttribute(DeviceExt
, BitmapRecord
, AttributeData
, L
"", 0, &DataContext
, NULL
);
139 if (!NT_SUCCESS(Status
))
141 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
145 BitmapDataSize
= AttributeDataLength(&DataContext
->Record
);
146 BitmapDataSize
= min(BitmapDataSize
, 0xffffffff);
147 ASSERT((BitmapDataSize
* 8) >= DeviceExt
->NtfsInfo
.ClusterCount
);
148 BitmapData
= ExAllocatePoolWithTag(NonPagedPool
, ROUND_UP(BitmapDataSize
, DeviceExt
->NtfsInfo
.BytesPerSector
), TAG_NTFS
);
149 if (BitmapData
== NULL
)
151 ReleaseAttributeContext(DataContext
);
152 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
156 DPRINT1("Total clusters: %I64x\n", DeviceExt
->NtfsInfo
.ClusterCount
);
157 DPRINT1("Total clusters in bitmap: %I64x\n", BitmapDataSize
* 8);
158 DPRINT1("Diff in size: %I64d B\n", ((BitmapDataSize
* 8) - DeviceExt
->NtfsInfo
.ClusterCount
) * DeviceExt
->NtfsInfo
.SectorsPerCluster
* DeviceExt
->NtfsInfo
.BytesPerSector
);
160 ReadAttribute(DeviceExt
, DataContext
, 0, (PCHAR
)BitmapData
, (ULONG
)BitmapDataSize
);
162 RtlInitializeBitMap(&Bitmap
, (PULONG
)BitmapData
, DeviceExt
->NtfsInfo
.ClusterCount
);
163 FreeClusters
= RtlNumberOfClearBits(&Bitmap
);
165 if( FreeClusters
< DesiredClusters
)
166 Status
= STATUS_DISK_FULL
;
168 // TODO: Observe MFT reservation zone
170 // Can we get one contiguous run?
171 ULONG AssignedRun
= RtlFindClearBitsAndSet(&Bitmap
, DesiredClusters
, FirstDesiredCluster
);
174 if (AssignedRun
!= 0xFFFFFFFF)
176 *FirstAssignedCluster
= AssignedRun
;
177 *AssignedClusters
= DesiredClusters
;
181 // we can't get one contiguous run
182 *AssignedClusters
= RtlFindNextForwardRunClear(&Bitmap
, FirstDesiredCluster
, FirstAssignedCluster
);
184 if (*AssignedClusters
== 0)
186 // we couldn't find any runs starting at DesiredFirstCluster
187 *AssignedClusters
= RtlFindLongestRunClear(&Bitmap
, FirstAssignedCluster
);
192 Status
= WriteAttribute(DeviceExt
, DataContext
, 0, BitmapData
, (ULONG
)BitmapDataSize
, &LengthWritten
);
194 ReleaseAttributeContext(DataContext
);
196 ExFreePoolWithTag(BitmapData
, TAG_NTFS
);
197 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
204 NtfsGetFsVolumeInformation(PDEVICE_OBJECT DeviceObject
,
205 PFILE_FS_VOLUME_INFORMATION FsVolumeInfo
,
208 DPRINT("NtfsGetFsVolumeInformation() called\n");
209 DPRINT("FsVolumeInfo = %p\n", FsVolumeInfo
);
210 DPRINT("BufferLength %lu\n", *BufferLength
);
212 DPRINT("Vpb %p\n", DeviceObject
->Vpb
);
214 DPRINT("Required length %lu\n",
215 sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
);
216 DPRINT("LabelLength %hu\n",
217 DeviceObject
->Vpb
->VolumeLabelLength
);
218 DPRINT("Label %.*S\n",
219 DeviceObject
->Vpb
->VolumeLabelLength
/ sizeof(WCHAR
),
220 DeviceObject
->Vpb
->VolumeLabel
);
222 if (*BufferLength
< sizeof(FILE_FS_VOLUME_INFORMATION
))
223 return STATUS_INFO_LENGTH_MISMATCH
;
225 if (*BufferLength
< (sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
))
226 return STATUS_BUFFER_OVERFLOW
;
229 FsVolumeInfo
->VolumeSerialNumber
= DeviceObject
->Vpb
->SerialNumber
;
230 FsVolumeInfo
->VolumeLabelLength
= DeviceObject
->Vpb
->VolumeLabelLength
;
231 memcpy(FsVolumeInfo
->VolumeLabel
,
232 DeviceObject
->Vpb
->VolumeLabel
,
233 DeviceObject
->Vpb
->VolumeLabelLength
);
236 FsVolumeInfo
->VolumeCreationTime
.QuadPart
= 0;
237 FsVolumeInfo
->SupportsObjects
= FALSE
;
239 *BufferLength
-= (sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
);
241 DPRINT("BufferLength %lu\n", *BufferLength
);
242 DPRINT("NtfsGetFsVolumeInformation() done\n");
244 return STATUS_SUCCESS
;
250 NtfsGetFsAttributeInformation(PDEVICE_EXTENSION DeviceExt
,
251 PFILE_FS_ATTRIBUTE_INFORMATION FsAttributeInfo
,
254 UNREFERENCED_PARAMETER(DeviceExt
);
256 DPRINT("NtfsGetFsAttributeInformation()\n");
257 DPRINT("FsAttributeInfo = %p\n", FsAttributeInfo
);
258 DPRINT("BufferLength %lu\n", *BufferLength
);
259 DPRINT("Required length %lu\n", (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8));
261 if (*BufferLength
< sizeof (FILE_FS_ATTRIBUTE_INFORMATION
))
262 return STATUS_INFO_LENGTH_MISMATCH
;
264 if (*BufferLength
< (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8))
265 return STATUS_BUFFER_OVERFLOW
;
267 FsAttributeInfo
->FileSystemAttributes
=
268 FILE_CASE_PRESERVED_NAMES
| FILE_UNICODE_ON_DISK
| FILE_READ_ONLY_VOLUME
;
269 FsAttributeInfo
->MaximumComponentNameLength
= 255;
270 FsAttributeInfo
->FileSystemNameLength
= 8;
272 memcpy(FsAttributeInfo
->FileSystemName
, L
"NTFS", 8);
274 DPRINT("Finished NtfsGetFsAttributeInformation()\n");
276 *BufferLength
-= (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8);
277 DPRINT("BufferLength %lu\n", *BufferLength
);
279 return STATUS_SUCCESS
;
285 NtfsGetFsSizeInformation(PDEVICE_OBJECT DeviceObject
,
286 PFILE_FS_SIZE_INFORMATION FsSizeInfo
,
289 PDEVICE_EXTENSION DeviceExt
;
290 NTSTATUS Status
= STATUS_SUCCESS
;
292 DPRINT("NtfsGetFsSizeInformation()\n");
293 DPRINT("FsSizeInfo = %p\n", FsSizeInfo
);
295 if (*BufferLength
< sizeof(FILE_FS_SIZE_INFORMATION
))
296 return STATUS_BUFFER_OVERFLOW
;
298 DeviceExt
= DeviceObject
->DeviceExtension
;
300 FsSizeInfo
->AvailableAllocationUnits
.QuadPart
= NtfsGetFreeClusters(DeviceExt
);
301 FsSizeInfo
->TotalAllocationUnits
.QuadPart
= DeviceExt
->NtfsInfo
.ClusterCount
;
302 FsSizeInfo
->SectorsPerAllocationUnit
= DeviceExt
->NtfsInfo
.SectorsPerCluster
;
303 FsSizeInfo
->BytesPerSector
= DeviceExt
->NtfsInfo
.BytesPerSector
;
305 DPRINT("Finished NtfsGetFsSizeInformation()\n");
306 if (NT_SUCCESS(Status
))
307 *BufferLength
-= sizeof(FILE_FS_SIZE_INFORMATION
);
315 NtfsGetFsDeviceInformation(PDEVICE_OBJECT DeviceObject
,
316 PFILE_FS_DEVICE_INFORMATION FsDeviceInfo
,
319 DPRINT("NtfsGetFsDeviceInformation()\n");
320 DPRINT("FsDeviceInfo = %p\n", FsDeviceInfo
);
321 DPRINT("BufferLength %lu\n", *BufferLength
);
322 DPRINT("Required length %lu\n", sizeof(FILE_FS_DEVICE_INFORMATION
));
324 if (*BufferLength
< sizeof(FILE_FS_DEVICE_INFORMATION
))
325 return STATUS_BUFFER_OVERFLOW
;
327 FsDeviceInfo
->DeviceType
= FILE_DEVICE_DISK
;
328 FsDeviceInfo
->Characteristics
= DeviceObject
->Characteristics
;
330 DPRINT("NtfsGetFsDeviceInformation() finished.\n");
332 *BufferLength
-= sizeof(FILE_FS_DEVICE_INFORMATION
);
333 DPRINT("BufferLength %lu\n", *BufferLength
);
335 return STATUS_SUCCESS
;
340 NtfsQueryVolumeInformation(PNTFS_IRP_CONTEXT IrpContext
)
343 PDEVICE_OBJECT DeviceObject
;
344 FS_INFORMATION_CLASS FsInformationClass
;
345 PIO_STACK_LOCATION Stack
;
346 NTSTATUS Status
= STATUS_SUCCESS
;
349 PDEVICE_EXTENSION DeviceExt
;
351 DPRINT("NtfsQueryVolumeInformation() called\n");
355 Irp
= IrpContext
->Irp
;
356 DeviceObject
= IrpContext
->DeviceObject
;
357 DeviceExt
= DeviceObject
->DeviceExtension
;
358 Stack
= IrpContext
->Stack
;
360 if (!ExAcquireResourceSharedLite(&DeviceExt
->DirResource
,
361 BooleanFlagOn(IrpContext
->Flags
, IRPCONTEXT_CANWAIT
)))
363 return NtfsMarkIrpContextForQueue(IrpContext
);
366 FsInformationClass
= Stack
->Parameters
.QueryVolume
.FsInformationClass
;
367 BufferLength
= Stack
->Parameters
.QueryVolume
.Length
;
368 SystemBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
369 RtlZeroMemory(SystemBuffer
, BufferLength
);
371 DPRINT("FsInformationClass %d\n", FsInformationClass
);
372 DPRINT("SystemBuffer %p\n", SystemBuffer
);
374 switch (FsInformationClass
)
376 case FileFsVolumeInformation
:
377 Status
= NtfsGetFsVolumeInformation(DeviceObject
,
382 case FileFsAttributeInformation
:
383 Status
= NtfsGetFsAttributeInformation(DeviceObject
->DeviceExtension
,
388 case FileFsSizeInformation
:
389 Status
= NtfsGetFsSizeInformation(DeviceObject
,
394 case FileFsDeviceInformation
:
395 Status
= NtfsGetFsDeviceInformation(DeviceObject
,
401 Status
= STATUS_NOT_SUPPORTED
;
404 ExReleaseResourceLite(&DeviceExt
->DirResource
);
406 if (NT_SUCCESS(Status
))
407 Irp
->IoStatus
.Information
=
408 Stack
->Parameters
.QueryVolume
.Length
- BufferLength
;
410 Irp
->IoStatus
.Information
= 0;
417 NtfsSetVolumeInformation(PNTFS_IRP_CONTEXT IrpContext
)
421 DPRINT("NtfsSetVolumeInformation() called\n");
425 Irp
= IrpContext
->Irp
;
426 Irp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
427 Irp
->IoStatus
.Information
= 0;
429 return STATUS_NOT_SUPPORTED
;