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;
122 DPRINT1("NtfsAllocateClusters(%p, %lu, %lu, %p)\n", DeviceExt
, DesiredClusters
, FirstDesiredCluster
, FirstAssignedCluster
, AssignedClusters
);
124 BitmapRecord
= ExAllocatePoolWithTag(NonPagedPool
,
125 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
127 if (BitmapRecord
== NULL
)
132 Status
= ReadFileRecord(DeviceExt
, NTFS_FILE_BITMAP
, BitmapRecord
);
133 if (!NT_SUCCESS(Status
))
135 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
139 Status
= FindAttribute(DeviceExt
, BitmapRecord
, AttributeData
, L
"", 0, &DataContext
, NULL
);
140 if (!NT_SUCCESS(Status
))
142 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
146 BitmapDataSize
= AttributeDataLength(&DataContext
->Record
);
147 BitmapDataSize
= min(BitmapDataSize
, 0xffffffff);
148 ASSERT((BitmapDataSize
* 8) >= DeviceExt
->NtfsInfo
.ClusterCount
);
149 BitmapData
= ExAllocatePoolWithTag(NonPagedPool
, ROUND_UP(BitmapDataSize
, DeviceExt
->NtfsInfo
.BytesPerSector
), TAG_NTFS
);
150 if (BitmapData
== NULL
)
152 ReleaseAttributeContext(DataContext
);
153 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
157 DPRINT1("Total clusters: %I64x\n", DeviceExt
->NtfsInfo
.ClusterCount
);
158 DPRINT1("Total clusters in bitmap: %I64x\n", BitmapDataSize
* 8);
159 DPRINT1("Diff in size: %I64d B\n", ((BitmapDataSize
* 8) - DeviceExt
->NtfsInfo
.ClusterCount
) * DeviceExt
->NtfsInfo
.SectorsPerCluster
* DeviceExt
->NtfsInfo
.BytesPerSector
);
161 ReadAttribute(DeviceExt
, DataContext
, Read
, (PCHAR
)((ULONG_PTR
)BitmapData
+ Read
), (ULONG
)BitmapDataSize
);
163 RtlInitializeBitMap(&Bitmap
, (PULONG
)BitmapData
, DeviceExt
->NtfsInfo
.ClusterCount
);
164 FreeClusters
= RtlNumberOfClearBits(&Bitmap
);
166 if (FreeClusters
>= DesiredClusters
)
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
);
195 Status
= STATUS_DISK_FULL
;
198 ReleaseAttributeContext(DataContext
);
200 ExFreePoolWithTag(BitmapData
, TAG_NTFS
);
201 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
208 NtfsGetFsVolumeInformation(PDEVICE_OBJECT DeviceObject
,
209 PFILE_FS_VOLUME_INFORMATION FsVolumeInfo
,
212 DPRINT("NtfsGetFsVolumeInformation() called\n");
213 DPRINT("FsVolumeInfo = %p\n", FsVolumeInfo
);
214 DPRINT("BufferLength %lu\n", *BufferLength
);
216 DPRINT("Vpb %p\n", DeviceObject
->Vpb
);
218 DPRINT("Required length %lu\n",
219 sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
);
220 DPRINT("LabelLength %hu\n",
221 DeviceObject
->Vpb
->VolumeLabelLength
);
222 DPRINT("Label %.*S\n",
223 DeviceObject
->Vpb
->VolumeLabelLength
/ sizeof(WCHAR
),
224 DeviceObject
->Vpb
->VolumeLabel
);
226 if (*BufferLength
< sizeof(FILE_FS_VOLUME_INFORMATION
))
227 return STATUS_INFO_LENGTH_MISMATCH
;
229 if (*BufferLength
< (sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
))
230 return STATUS_BUFFER_OVERFLOW
;
233 FsVolumeInfo
->VolumeSerialNumber
= DeviceObject
->Vpb
->SerialNumber
;
234 FsVolumeInfo
->VolumeLabelLength
= DeviceObject
->Vpb
->VolumeLabelLength
;
235 memcpy(FsVolumeInfo
->VolumeLabel
,
236 DeviceObject
->Vpb
->VolumeLabel
,
237 DeviceObject
->Vpb
->VolumeLabelLength
);
240 FsVolumeInfo
->VolumeCreationTime
.QuadPart
= 0;
241 FsVolumeInfo
->SupportsObjects
= FALSE
;
243 *BufferLength
-= (sizeof(FILE_FS_VOLUME_INFORMATION
) + DeviceObject
->Vpb
->VolumeLabelLength
);
245 DPRINT("BufferLength %lu\n", *BufferLength
);
246 DPRINT("NtfsGetFsVolumeInformation() done\n");
248 return STATUS_SUCCESS
;
254 NtfsGetFsAttributeInformation(PDEVICE_EXTENSION DeviceExt
,
255 PFILE_FS_ATTRIBUTE_INFORMATION FsAttributeInfo
,
258 UNREFERENCED_PARAMETER(DeviceExt
);
260 DPRINT("NtfsGetFsAttributeInformation()\n");
261 DPRINT("FsAttributeInfo = %p\n", FsAttributeInfo
);
262 DPRINT("BufferLength %lu\n", *BufferLength
);
263 DPRINT("Required length %lu\n", (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8));
265 if (*BufferLength
< sizeof (FILE_FS_ATTRIBUTE_INFORMATION
))
266 return STATUS_INFO_LENGTH_MISMATCH
;
268 if (*BufferLength
< (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8))
269 return STATUS_BUFFER_OVERFLOW
;
271 FsAttributeInfo
->FileSystemAttributes
=
272 FILE_CASE_PRESERVED_NAMES
| FILE_UNICODE_ON_DISK
| FILE_READ_ONLY_VOLUME
;
273 FsAttributeInfo
->MaximumComponentNameLength
= 255;
274 FsAttributeInfo
->FileSystemNameLength
= 8;
276 memcpy(FsAttributeInfo
->FileSystemName
, L
"NTFS", 8);
278 DPRINT("Finished NtfsGetFsAttributeInformation()\n");
280 *BufferLength
-= (sizeof(FILE_FS_ATTRIBUTE_INFORMATION
) + 8);
281 DPRINT("BufferLength %lu\n", *BufferLength
);
283 return STATUS_SUCCESS
;
289 NtfsGetFsSizeInformation(PDEVICE_OBJECT DeviceObject
,
290 PFILE_FS_SIZE_INFORMATION FsSizeInfo
,
293 PDEVICE_EXTENSION DeviceExt
;
294 NTSTATUS Status
= STATUS_SUCCESS
;
296 DPRINT("NtfsGetFsSizeInformation()\n");
297 DPRINT("FsSizeInfo = %p\n", FsSizeInfo
);
299 if (*BufferLength
< sizeof(FILE_FS_SIZE_INFORMATION
))
300 return STATUS_BUFFER_OVERFLOW
;
302 DeviceExt
= DeviceObject
->DeviceExtension
;
304 FsSizeInfo
->AvailableAllocationUnits
.QuadPart
= NtfsGetFreeClusters(DeviceExt
);
305 FsSizeInfo
->TotalAllocationUnits
.QuadPart
= DeviceExt
->NtfsInfo
.ClusterCount
;
306 FsSizeInfo
->SectorsPerAllocationUnit
= DeviceExt
->NtfsInfo
.SectorsPerCluster
;
307 FsSizeInfo
->BytesPerSector
= DeviceExt
->NtfsInfo
.BytesPerSector
;
309 DPRINT("Finished NtfsGetFsSizeInformation()\n");
310 if (NT_SUCCESS(Status
))
311 *BufferLength
-= sizeof(FILE_FS_SIZE_INFORMATION
);
319 NtfsGetFsDeviceInformation(PDEVICE_OBJECT DeviceObject
,
320 PFILE_FS_DEVICE_INFORMATION FsDeviceInfo
,
323 DPRINT("NtfsGetFsDeviceInformation()\n");
324 DPRINT("FsDeviceInfo = %p\n", FsDeviceInfo
);
325 DPRINT("BufferLength %lu\n", *BufferLength
);
326 DPRINT("Required length %lu\n", sizeof(FILE_FS_DEVICE_INFORMATION
));
328 if (*BufferLength
< sizeof(FILE_FS_DEVICE_INFORMATION
))
329 return STATUS_BUFFER_OVERFLOW
;
331 FsDeviceInfo
->DeviceType
= FILE_DEVICE_DISK
;
332 FsDeviceInfo
->Characteristics
= DeviceObject
->Characteristics
;
334 DPRINT("NtfsGetFsDeviceInformation() finished.\n");
336 *BufferLength
-= sizeof(FILE_FS_DEVICE_INFORMATION
);
337 DPRINT("BufferLength %lu\n", *BufferLength
);
339 return STATUS_SUCCESS
;
344 NtfsQueryVolumeInformation(PNTFS_IRP_CONTEXT IrpContext
)
347 PDEVICE_OBJECT DeviceObject
;
348 FS_INFORMATION_CLASS FsInformationClass
;
349 PIO_STACK_LOCATION Stack
;
350 NTSTATUS Status
= STATUS_SUCCESS
;
353 PDEVICE_EXTENSION DeviceExt
;
355 DPRINT("NtfsQueryVolumeInformation() called\n");
359 Irp
= IrpContext
->Irp
;
360 DeviceObject
= IrpContext
->DeviceObject
;
361 DeviceExt
= DeviceObject
->DeviceExtension
;
362 Stack
= IrpContext
->Stack
;
364 if (!ExAcquireResourceSharedLite(&DeviceExt
->DirResource
,
365 BooleanFlagOn(IrpContext
->Flags
, IRPCONTEXT_CANWAIT
)))
367 return NtfsMarkIrpContextForQueue(IrpContext
);
370 FsInformationClass
= Stack
->Parameters
.QueryVolume
.FsInformationClass
;
371 BufferLength
= Stack
->Parameters
.QueryVolume
.Length
;
372 SystemBuffer
= Irp
->AssociatedIrp
.SystemBuffer
;
373 RtlZeroMemory(SystemBuffer
, BufferLength
);
375 DPRINT("FsInformationClass %d\n", FsInformationClass
);
376 DPRINT("SystemBuffer %p\n", SystemBuffer
);
378 switch (FsInformationClass
)
380 case FileFsVolumeInformation
:
381 Status
= NtfsGetFsVolumeInformation(DeviceObject
,
386 case FileFsAttributeInformation
:
387 Status
= NtfsGetFsAttributeInformation(DeviceObject
->DeviceExtension
,
392 case FileFsSizeInformation
:
393 Status
= NtfsGetFsSizeInformation(DeviceObject
,
398 case FileFsDeviceInformation
:
399 Status
= NtfsGetFsDeviceInformation(DeviceObject
,
405 Status
= STATUS_NOT_SUPPORTED
;
408 ExReleaseResourceLite(&DeviceExt
->DirResource
);
410 if (NT_SUCCESS(Status
))
411 Irp
->IoStatus
.Information
=
412 Stack
->Parameters
.QueryVolume
.Length
- BufferLength
;
414 Irp
->IoStatus
.Information
= 0;
421 NtfsSetVolumeInformation(PNTFS_IRP_CONTEXT IrpContext
)
425 DPRINT("NtfsSetVolumeInformation() called\n");
429 Irp
= IrpContext
->Irp
;
430 Irp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
431 Irp
->IoStatus
.Information
= 0;
433 return STATUS_NOT_SUPPORTED
;