68a1050620764178f5e38174a43ab85570574518
[reactos.git] / drivers / filesystems / ntfs / volinfo.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002, 2014 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 *
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)
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "ntfs.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 /* FUNCTIONS ****************************************************************/
35
36 ULONGLONG
37 NtfsGetFreeClusters(PDEVICE_EXTENSION DeviceExt)
38 {
39 NTSTATUS Status;
40 PFILE_RECORD_HEADER BitmapRecord;
41 PNTFS_ATTR_CONTEXT DataContext;
42 ULONGLONG BitmapDataSize;
43 PCHAR BitmapData;
44 ULONGLONG FreeClusters = 0;
45 ULONG Read = 0;
46 RTL_BITMAP Bitmap;
47
48 DPRINT1("NtfsGetFreeClusters(%p)\n", DeviceExt);
49
50 BitmapRecord = ExAllocatePoolWithTag(NonPagedPool,
51 DeviceExt->NtfsInfo.BytesPerFileRecord,
52 TAG_NTFS);
53 if (BitmapRecord == NULL)
54 {
55 return 0;
56 }
57
58 Status = ReadFileRecord(DeviceExt, NTFS_FILE_BITMAP, BitmapRecord);
59 if (!NT_SUCCESS(Status))
60 {
61 ExFreePoolWithTag(BitmapRecord, TAG_NTFS);
62 return 0;
63 }
64
65 Status = FindAttribute(DeviceExt, BitmapRecord, AttributeData, L"", 0, &DataContext, NULL);
66 if (!NT_SUCCESS(Status))
67 {
68 ExFreePoolWithTag(BitmapRecord, TAG_NTFS);
69 return 0;
70 }
71
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)
76 {
77 ReleaseAttributeContext(DataContext);
78 ExFreePoolWithTag(BitmapRecord, TAG_NTFS);
79 return 0;
80 }
81
82 /* FIXME: Totally underoptimized! */
83 for (; Read < BitmapDataSize; Read += DeviceExt->NtfsInfo.BytesPerSector)
84 {
85 ReadAttribute(DeviceExt, DataContext, Read, (PCHAR)((ULONG_PTR)BitmapData + Read), DeviceExt->NtfsInfo.BytesPerSector);
86 }
87 ReleaseAttributeContext(DataContext);
88
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);
92
93 RtlInitializeBitMap(&Bitmap, (PULONG)BitmapData, DeviceExt->NtfsInfo.ClusterCount);
94 FreeClusters = RtlNumberOfClearBits(&Bitmap);
95
96 ExFreePoolWithTag(BitmapData, TAG_NTFS);
97 ExFreePoolWithTag(BitmapRecord, TAG_NTFS);
98
99 return FreeClusters;
100 }
101
102 /**
103 * NtfsAllocateClusters
104 * Allocates a run of clusters. The run allocated might be smaller than DesiredClusters.
105 */
106 NTSTATUS
107 NtfsAllocateClusters(PDEVICE_EXTENSION DeviceExt,
108 ULONG FirstDesiredCluster,
109 ULONG DesiredClusters,
110 PULONG FirstAssignedCluster,
111 PULONG AssignedClusters)
112 {
113 NTSTATUS Status;
114 PFILE_RECORD_HEADER BitmapRecord;
115 PNTFS_ATTR_CONTEXT DataContext;
116 ULONGLONG BitmapDataSize;
117 PCHAR BitmapData;
118 ULONGLONG FreeClusters = 0;
119 RTL_BITMAP Bitmap;
120
121 DPRINT1("NtfsAllocateClusters(%p, %lu, %lu, %p, %p)\n", DeviceExt, FirstDesiredCluster, DesiredClusters, FirstAssignedCluster, AssignedClusters);
122
123 BitmapRecord = ExAllocatePoolWithTag(NonPagedPool,
124 DeviceExt->NtfsInfo.BytesPerFileRecord,
125 TAG_NTFS);
126 if (BitmapRecord == NULL)
127 {
128 return 0;
129 }
130
131 Status = ReadFileRecord(DeviceExt, NTFS_FILE_BITMAP, BitmapRecord);
132 if (!NT_SUCCESS(Status))
133 {
134 ExFreePoolWithTag(BitmapRecord, TAG_NTFS);
135 return 0;
136 }
137
138 Status = FindAttribute(DeviceExt, BitmapRecord, AttributeData, L"", 0, &DataContext, NULL);
139 if (!NT_SUCCESS(Status))
140 {
141 ExFreePoolWithTag(BitmapRecord, TAG_NTFS);
142 return 0;
143 }
144
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)
150 {
151 ReleaseAttributeContext(DataContext);
152 ExFreePoolWithTag(BitmapRecord, TAG_NTFS);
153 return 0;
154 }
155
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);
159
160 ReadAttribute(DeviceExt, DataContext, 0, (PCHAR)BitmapData, (ULONG)BitmapDataSize);
161
162 RtlInitializeBitMap(&Bitmap, (PULONG)BitmapData, DeviceExt->NtfsInfo.ClusterCount);
163 FreeClusters = RtlNumberOfClearBits(&Bitmap);
164
165 if( FreeClusters < DesiredClusters )
166 Status = STATUS_DISK_FULL;
167
168 // TODO: Observe MFT reservation zone
169
170 // Can we get one contiguous run?
171 ULONG AssignedRun = RtlFindClearBitsAndSet(&Bitmap, DesiredClusters, FirstDesiredCluster);
172 ULONG LengthWritten;
173
174 if (AssignedRun != 0xFFFFFFFF)
175 {
176 *FirstAssignedCluster = AssignedRun;
177 *AssignedClusters = DesiredClusters;
178 }
179 else
180 {
181 // we can't get one contiguous run
182 *AssignedClusters = RtlFindNextForwardRunClear(&Bitmap, FirstDesiredCluster, FirstAssignedCluster);
183
184 if (*AssignedClusters == 0)
185 {
186 // we couldn't find any runs starting at DesiredFirstCluster
187 *AssignedClusters = RtlFindLongestRunClear(&Bitmap, FirstAssignedCluster);
188 }
189
190 }
191
192 Status = WriteAttribute(DeviceExt, DataContext, 0, BitmapData, (ULONG)BitmapDataSize, &LengthWritten);
193
194 ReleaseAttributeContext(DataContext);
195
196 ExFreePoolWithTag(BitmapData, TAG_NTFS);
197 ExFreePoolWithTag(BitmapRecord, TAG_NTFS);
198
199 return Status;
200 }
201
202 static
203 NTSTATUS
204 NtfsGetFsVolumeInformation(PDEVICE_OBJECT DeviceObject,
205 PFILE_FS_VOLUME_INFORMATION FsVolumeInfo,
206 PULONG BufferLength)
207 {
208 DPRINT("NtfsGetFsVolumeInformation() called\n");
209 DPRINT("FsVolumeInfo = %p\n", FsVolumeInfo);
210 DPRINT("BufferLength %lu\n", *BufferLength);
211
212 DPRINT("Vpb %p\n", DeviceObject->Vpb);
213
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);
221
222 if (*BufferLength < sizeof(FILE_FS_VOLUME_INFORMATION))
223 return STATUS_INFO_LENGTH_MISMATCH;
224
225 if (*BufferLength < (sizeof(FILE_FS_VOLUME_INFORMATION) + DeviceObject->Vpb->VolumeLabelLength))
226 return STATUS_BUFFER_OVERFLOW;
227
228 /* valid entries */
229 FsVolumeInfo->VolumeSerialNumber = DeviceObject->Vpb->SerialNumber;
230 FsVolumeInfo->VolumeLabelLength = DeviceObject->Vpb->VolumeLabelLength;
231 memcpy(FsVolumeInfo->VolumeLabel,
232 DeviceObject->Vpb->VolumeLabel,
233 DeviceObject->Vpb->VolumeLabelLength);
234
235 /* dummy entries */
236 FsVolumeInfo->VolumeCreationTime.QuadPart = 0;
237 FsVolumeInfo->SupportsObjects = FALSE;
238
239 *BufferLength -= (sizeof(FILE_FS_VOLUME_INFORMATION) + DeviceObject->Vpb->VolumeLabelLength);
240
241 DPRINT("BufferLength %lu\n", *BufferLength);
242 DPRINT("NtfsGetFsVolumeInformation() done\n");
243
244 return STATUS_SUCCESS;
245 }
246
247
248 static
249 NTSTATUS
250 NtfsGetFsAttributeInformation(PDEVICE_EXTENSION DeviceExt,
251 PFILE_FS_ATTRIBUTE_INFORMATION FsAttributeInfo,
252 PULONG BufferLength)
253 {
254 UNREFERENCED_PARAMETER(DeviceExt);
255
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));
260
261 if (*BufferLength < sizeof (FILE_FS_ATTRIBUTE_INFORMATION))
262 return STATUS_INFO_LENGTH_MISMATCH;
263
264 if (*BufferLength < (sizeof(FILE_FS_ATTRIBUTE_INFORMATION) + 8))
265 return STATUS_BUFFER_OVERFLOW;
266
267 FsAttributeInfo->FileSystemAttributes =
268 FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_READ_ONLY_VOLUME;
269 FsAttributeInfo->MaximumComponentNameLength = 255;
270 FsAttributeInfo->FileSystemNameLength = 8;
271
272 memcpy(FsAttributeInfo->FileSystemName, L"NTFS", 8);
273
274 DPRINT("Finished NtfsGetFsAttributeInformation()\n");
275
276 *BufferLength -= (sizeof(FILE_FS_ATTRIBUTE_INFORMATION) + 8);
277 DPRINT("BufferLength %lu\n", *BufferLength);
278
279 return STATUS_SUCCESS;
280 }
281
282
283 static
284 NTSTATUS
285 NtfsGetFsSizeInformation(PDEVICE_OBJECT DeviceObject,
286 PFILE_FS_SIZE_INFORMATION FsSizeInfo,
287 PULONG BufferLength)
288 {
289 PDEVICE_EXTENSION DeviceExt;
290 NTSTATUS Status = STATUS_SUCCESS;
291
292 DPRINT("NtfsGetFsSizeInformation()\n");
293 DPRINT("FsSizeInfo = %p\n", FsSizeInfo);
294
295 if (*BufferLength < sizeof(FILE_FS_SIZE_INFORMATION))
296 return STATUS_BUFFER_OVERFLOW;
297
298 DeviceExt = DeviceObject->DeviceExtension;
299
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;
304
305 DPRINT("Finished NtfsGetFsSizeInformation()\n");
306 if (NT_SUCCESS(Status))
307 *BufferLength -= sizeof(FILE_FS_SIZE_INFORMATION);
308
309 return Status;
310 }
311
312
313 static
314 NTSTATUS
315 NtfsGetFsDeviceInformation(PDEVICE_OBJECT DeviceObject,
316 PFILE_FS_DEVICE_INFORMATION FsDeviceInfo,
317 PULONG BufferLength)
318 {
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));
323
324 if (*BufferLength < sizeof(FILE_FS_DEVICE_INFORMATION))
325 return STATUS_BUFFER_OVERFLOW;
326
327 FsDeviceInfo->DeviceType = FILE_DEVICE_DISK;
328 FsDeviceInfo->Characteristics = DeviceObject->Characteristics;
329
330 DPRINT("NtfsGetFsDeviceInformation() finished.\n");
331
332 *BufferLength -= sizeof(FILE_FS_DEVICE_INFORMATION);
333 DPRINT("BufferLength %lu\n", *BufferLength);
334
335 return STATUS_SUCCESS;
336 }
337
338
339 NTSTATUS
340 NtfsQueryVolumeInformation(PNTFS_IRP_CONTEXT IrpContext)
341 {
342 PIRP Irp;
343 PDEVICE_OBJECT DeviceObject;
344 FS_INFORMATION_CLASS FsInformationClass;
345 PIO_STACK_LOCATION Stack;
346 NTSTATUS Status = STATUS_SUCCESS;
347 PVOID SystemBuffer;
348 ULONG BufferLength;
349 PDEVICE_EXTENSION DeviceExt;
350
351 DPRINT("NtfsQueryVolumeInformation() called\n");
352
353 ASSERT(IrpContext);
354
355 Irp = IrpContext->Irp;
356 DeviceObject = IrpContext->DeviceObject;
357 DeviceExt = DeviceObject->DeviceExtension;
358 Stack = IrpContext->Stack;
359
360 if (!ExAcquireResourceSharedLite(&DeviceExt->DirResource,
361 BooleanFlagOn(IrpContext->Flags, IRPCONTEXT_CANWAIT)))
362 {
363 return NtfsMarkIrpContextForQueue(IrpContext);
364 }
365
366 FsInformationClass = Stack->Parameters.QueryVolume.FsInformationClass;
367 BufferLength = Stack->Parameters.QueryVolume.Length;
368 SystemBuffer = Irp->AssociatedIrp.SystemBuffer;
369 RtlZeroMemory(SystemBuffer, BufferLength);
370
371 DPRINT("FsInformationClass %d\n", FsInformationClass);
372 DPRINT("SystemBuffer %p\n", SystemBuffer);
373
374 switch (FsInformationClass)
375 {
376 case FileFsVolumeInformation:
377 Status = NtfsGetFsVolumeInformation(DeviceObject,
378 SystemBuffer,
379 &BufferLength);
380 break;
381
382 case FileFsAttributeInformation:
383 Status = NtfsGetFsAttributeInformation(DeviceObject->DeviceExtension,
384 SystemBuffer,
385 &BufferLength);
386 break;
387
388 case FileFsSizeInformation:
389 Status = NtfsGetFsSizeInformation(DeviceObject,
390 SystemBuffer,
391 &BufferLength);
392 break;
393
394 case FileFsDeviceInformation:
395 Status = NtfsGetFsDeviceInformation(DeviceObject,
396 SystemBuffer,
397 &BufferLength);
398 break;
399
400 default:
401 Status = STATUS_NOT_SUPPORTED;
402 }
403
404 ExReleaseResourceLite(&DeviceExt->DirResource);
405
406 if (NT_SUCCESS(Status))
407 Irp->IoStatus.Information =
408 Stack->Parameters.QueryVolume.Length - BufferLength;
409 else
410 Irp->IoStatus.Information = 0;
411
412 return Status;
413 }
414
415
416 NTSTATUS
417 NtfsSetVolumeInformation(PNTFS_IRP_CONTEXT IrpContext)
418 {
419 PIRP Irp;
420
421 DPRINT("NtfsSetVolumeInformation() called\n");
422
423 ASSERT(IrpContext);
424
425 Irp = IrpContext->Irp;
426 Irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
427 Irp->IoStatus.Information = 0;
428
429 return STATUS_NOT_SUPPORTED;
430 }
431
432 /* EOF */