3 * Copyright (C) 2002,2003 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/attrib.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Eric Kohl
25 * Hervé Poussineau (hpoussin@reactos.org)
26 * Pierre Schweitzer (pierre@reactos.org)
29 /* INCLUDES *****************************************************************/
36 /* FUNCTIONS ****************************************************************/
42 * Adds a run of allocated clusters to a non-resident attribute.
45 * Pointer to an NTFS_VCB for the destination volume.
48 * Pointer to an NTFS_ATTR_CONTEXT describing the destination attribute.
51 * Byte offset of the destination attribute relative to its file record.
54 * Pointer to a complete copy of the file record containing the destination attribute. Must be at least
55 * Vcb->NtfsInfo.BytesPerFileRecord bytes long.
57 * @param NextAssignedCluster
58 * Logical cluster number of the start of the data run being added.
61 * How many clusters are in the data run being added. Can't be 0.
64 * STATUS_SUCCESS on success. STATUS_INVALID_PARAMETER if AttrContext describes a resident attribute.
65 * STATUS_INSUFFICIENT_RESOURCES if ConvertDataRunsToLargeMCB() fails.
66 * STATUS_BUFFER_TOO_SMALL if ConvertLargeMCBToDataRuns() fails.
67 * STATUS_NOT_IMPLEMENTED if we need to migrate the attribute to an attribute list (TODO).
70 * Clusters should have been allocated previously with NtfsAllocateClusters().
76 PNTFS_ATTR_CONTEXT AttrContext
,
78 PFILE_RECORD_HEADER FileRecord
,
79 ULONGLONG NextAssignedCluster
,
83 PUCHAR DataRun
= (PUCHAR
)&AttrContext
->Record
+ AttrContext
->Record
.NonResident
.MappingPairsOffset
;
85 PNTFS_ATTR_RECORD DestinationAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ AttrOffset
);
86 LARGE_MCB DataRunsMCB
;
87 ULONG NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.Length
;
88 ULONGLONG NextVBN
= AttrContext
->Record
.NonResident
.LowestVCN
;
90 // Allocate some memory for the RunBuffer
92 int RunBufferOffset
= 0;
94 if (!AttrContext
->Record
.IsNonResident
)
95 return STATUS_INVALID_PARAMETER
;
97 RunBuffer
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
99 // Convert the data runs to a map control block
100 Status
= ConvertDataRunsToLargeMCB(DataRun
, &DataRunsMCB
, &NextVBN
);
101 if (!NT_SUCCESS(Status
))
103 DPRINT1("Unable to convert data runs to MCB (probably ran out of memory)!\n");
104 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
108 // Add newly-assigned clusters to mcb
110 if (!FsRtlAddLargeMcbEntry(&DataRunsMCB
,
115 FsRtlUninitializeLargeMcb(&DataRunsMCB
);
116 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
117 return STATUS_INSUFFICIENT_RESOURCES
;
119 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
) {
120 FsRtlUninitializeLargeMcb(&DataRunsMCB
);
121 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
122 _SEH2_YIELD(return STATUS_INSUFFICIENT_RESOURCES
);
126 // Convert the map control block back to encoded data runs
127 ConvertLargeMCBToDataRuns(&DataRunsMCB
, RunBuffer
, Vcb
->NtfsInfo
.BytesPerCluster
, &RunBufferOffset
);
129 // Get the amount of free space between the start of the of the first data run and the attribute end
130 DataRunMaxLength
= AttrContext
->Record
.Length
- AttrContext
->Record
.NonResident
.MappingPairsOffset
;
132 // Do we need to extend the attribute (or convert to attribute list)?
133 if (DataRunMaxLength
< RunBufferOffset
)
135 PNTFS_ATTR_RECORD NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ NextAttributeOffset
);
136 DataRunMaxLength
+= Vcb
->NtfsInfo
.BytesPerFileRecord
- NextAttributeOffset
- (sizeof(ULONG
) * 2);
138 // Can we move the end of the attribute?
139 if (NextAttribute
->Type
!= AttributeEnd
|| DataRunMaxLength
< RunBufferOffset
- 1)
141 DPRINT1("FIXME: Need to create attribute list! Max Data Run Length available: %d\n", DataRunMaxLength
);
142 if (NextAttribute
->Type
!= AttributeEnd
)
143 DPRINT1("There's another attribute after this one with type %0xlx\n", NextAttribute
->Type
);
144 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
145 FsRtlUninitializeLargeMcb(&DataRunsMCB
);
146 return STATUS_NOT_IMPLEMENTED
;
149 // calculate position of end markers
150 NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.NonResident
.MappingPairsOffset
+ RunBufferOffset
;
151 NextAttributeOffset
= ALIGN_UP_BY(NextAttributeOffset
, 8);
153 // Write the end markers
154 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ NextAttributeOffset
);
155 NextAttribute
->Type
= AttributeEnd
;
156 NextAttribute
->Length
= FILE_RECORD_END
;
159 DestinationAttribute
->Length
= NextAttributeOffset
- AttrOffset
;
160 AttrContext
->Record
.Length
= DestinationAttribute
->Length
;
162 // We need to increase the FileRecord size
163 FileRecord
->BytesInUse
= NextAttributeOffset
+ (sizeof(ULONG
) * 2);
166 // NOTE: from this point on the original attribute record will contain invalid data in it's runbuffer
167 // TODO: Elegant fix? Could we free the old Record and allocate a new one without issue?
170 DestinationAttribute
->NonResident
.HighestVCN
=
171 AttrContext
->Record
.NonResident
.HighestVCN
= max(NextVBN
- 1 + RunLength
,
172 AttrContext
->Record
.NonResident
.HighestVCN
);
174 // Write data runs to destination attribute
175 RtlCopyMemory((PVOID
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
),
179 // Update the file record
180 Status
= UpdateFileRecord(Vcb
, AttrContext
->FileMFTIndex
, FileRecord
);
182 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
183 FsRtlUninitializeLargeMcb(&DataRunsMCB
);
185 NtfsDumpDataRuns((PUCHAR
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
), 0);
191 * @name ConvertDataRunsToLargeMCB
194 * Converts binary data runs to a map control block.
197 * Pointer to the run data
200 * Pointer to an unitialized LARGE_MCB structure.
203 * STATUS_SUCCESS on success, STATUS_INSUFFICIENT_RESOURCES if we fail to
204 * initialize the mcb or add an entry.
207 * Initializes the LARGE_MCB pointed to by DataRunsMCB. If this function succeeds, you
208 * need to call FsRtlUninitializeLargeMcb() when you're done with DataRunsMCB. This
209 * function will ensure the LargeMCB has been unitialized in case of failure.
213 ConvertDataRunsToLargeMCB(PUCHAR DataRun
,
214 PLARGE_MCB DataRunsMCB
,
217 LONGLONG DataRunOffset
;
218 ULONGLONG DataRunLength
;
219 LONGLONG DataRunStartLCN
;
220 ULONGLONG NextCluster
;
222 ULONGLONG LastLCN
= 0;
224 // Initialize the MCB, potentially catch an exception
226 FsRtlInitializeLargeMcb(DataRunsMCB
, NonPagedPool
);
227 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
) {
228 _SEH2_YIELD(return STATUS_INSUFFICIENT_RESOURCES
);
231 while (*DataRun
!= 0)
233 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
235 if (DataRunOffset
!= -1)
238 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
239 LastLCN
= DataRunStartLCN
;
240 NextCluster
= LastLCN
+ DataRunLength
;
244 if (!FsRtlAddLargeMcbEntry(DataRunsMCB
,
249 FsRtlUninitializeLargeMcb(DataRunsMCB
);
250 return STATUS_INSUFFICIENT_RESOURCES
;
252 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
) {
253 FsRtlUninitializeLargeMcb(DataRunsMCB
);
254 _SEH2_YIELD(return STATUS_INSUFFICIENT_RESOURCES
);
259 *pNextVBN
+= DataRunLength
;
262 return STATUS_SUCCESS
;
266 * @name ConvertLargeMCBToDataRuns
269 * Converts a map control block to a series of encoded data runs (used by non-resident attributes).
272 * Pointer to a LARGE_MCB structure describing the data runs.
275 * Pointer to the buffer that will receive the encoded data runs.
277 * @param MaxBufferSize
278 * Size of RunBuffer, in bytes.
280 * @param UsedBufferSize
281 * Pointer to a ULONG that will receive the size of the data runs in bytes. Can't be NULL.
284 * STATUS_SUCCESS on success, STATUS_BUFFER_TOO_SMALL if RunBuffer is too small to contain the
289 ConvertLargeMCBToDataRuns(PLARGE_MCB DataRunsMCB
,
292 PULONG UsedBufferSize
)
294 NTSTATUS Status
= STATUS_SUCCESS
;
295 ULONG RunBufferOffset
= 0;
296 LONGLONG DataRunOffset
;
297 ULONGLONG LastLCN
= 0;
299 LONGLONG Vbn
, Lbn
, Count
;
302 DPRINT("\t[Vbn, Lbn, Count]\n");
304 // convert each mcb entry to a data run
305 for (int i
= 0; FsRtlGetNextLargeMcbEntry(DataRunsMCB
, i
, &Vbn
, &Lbn
, &Count
); i
++)
307 UCHAR DataRunOffsetSize
= 0;
308 UCHAR DataRunLengthSize
= 0;
309 UCHAR ControlByte
= 0;
312 DPRINT("\t[%I64d, %I64d,%I64d]\n", Vbn
, Lbn
, Count
);
314 // TODO: check for holes and convert to sparse runs
315 DataRunOffset
= Lbn
- LastLCN
;
318 // now we need to determine how to represent DataRunOffset with the minimum number of bytes
319 DPRINT("Determining how many bytes needed to represent %I64x\n", DataRunOffset
);
320 DataRunOffsetSize
= GetPackedByteCount(DataRunOffset
, TRUE
);
321 DPRINT("%d bytes needed.\n", DataRunOffsetSize
);
323 // determine how to represent DataRunLengthSize with the minimum number of bytes
324 DPRINT("Determining how many bytes needed to represent %I64x\n", Count
);
325 DataRunLengthSize
= GetPackedByteCount(Count
, TRUE
);
326 DPRINT("%d bytes needed.\n", DataRunLengthSize
);
328 // ensure the next data run + end marker would be > Max buffer size
329 if (RunBufferOffset
+ 2 + DataRunLengthSize
+ DataRunOffsetSize
> MaxBufferSize
)
331 Status
= STATUS_BUFFER_TOO_SMALL
;
332 DPRINT1("FIXME: Ran out of room in buffer for data runs!\n");
336 // pack and copy the control byte
337 ControlByte
= (DataRunOffsetSize
<< 4) + DataRunLengthSize
;
338 RunBuffer
[RunBufferOffset
++] = ControlByte
;
340 // copy DataRunLength
341 RtlCopyMemory(RunBuffer
+ RunBufferOffset
, &Count
, DataRunLengthSize
);
342 RunBufferOffset
+= DataRunLengthSize
;
344 // copy DataRunOffset
345 RtlCopyMemory(RunBuffer
+ RunBufferOffset
, &DataRunOffset
, DataRunOffsetSize
);
346 RunBufferOffset
+= DataRunOffsetSize
;
350 RunBuffer
[RunBufferOffset
++] = 0;
352 *UsedBufferSize
= RunBufferOffset
;
353 DPRINT("New Size of DataRuns: %ld\n", *UsedBufferSize
);
359 DecodeRun(PUCHAR DataRun
,
360 LONGLONG
*DataRunOffset
,
361 ULONGLONG
*DataRunLength
)
363 UCHAR DataRunOffsetSize
;
364 UCHAR DataRunLengthSize
;
367 DataRunOffsetSize
= (*DataRun
>> 4) & 0xF;
368 DataRunLengthSize
= *DataRun
& 0xF;
372 for (i
= 0; i
< DataRunLengthSize
; i
++)
374 *DataRunLength
+= ((ULONG64
)*DataRun
) << (i
* 8);
378 /* NTFS 3+ sparse files */
379 if (DataRunOffsetSize
== 0)
385 for (i
= 0; i
< DataRunOffsetSize
- 1; i
++)
387 *DataRunOffset
+= ((ULONG64
)*DataRun
) << (i
* 8);
390 /* The last byte contains sign so we must process it different way. */
391 *DataRunOffset
= ((LONG64
)(CHAR
)(*(DataRun
++)) << (i
* 8)) + *DataRunOffset
;
394 DPRINT("DataRunOffsetSize: %x\n", DataRunOffsetSize
);
395 DPRINT("DataRunLengthSize: %x\n", DataRunLengthSize
);
396 DPRINT("DataRunOffset: %x\n", *DataRunOffset
);
397 DPRINT("DataRunLength: %x\n", *DataRunLength
);
403 FindRun(PNTFS_ATTR_RECORD NresAttr
,
408 if (vcn
< NresAttr
->NonResident
.LowestVCN
|| vcn
> NresAttr
->NonResident
.HighestVCN
)
411 DecodeRun((PUCHAR
)((ULONG_PTR
)NresAttr
+ NresAttr
->NonResident
.MappingPairsOffset
), (PLONGLONG
)lcn
, count
);
418 InternalReadNonResidentAttributes(PFIND_ATTR_CONTXT Context
)
421 PNTFS_ATTR_RECORD Attribute
;
422 PNTFS_ATTR_CONTEXT ListContext
;
424 DPRINT("InternalReadNonResidentAttributes(%p)\n", Context
);
426 Attribute
= Context
->CurrAttr
;
427 ASSERT(Attribute
->Type
== AttributeAttributeList
);
429 if (Context
->OnlyResident
)
431 Context
->NonResidentStart
= NULL
;
432 Context
->NonResidentEnd
= NULL
;
433 return STATUS_SUCCESS
;
436 if (Context
->NonResidentStart
!= NULL
)
438 return STATUS_FILE_CORRUPT_ERROR
;
441 ListContext
= PrepareAttributeContext(Attribute
);
442 ListSize
= AttributeDataLength(&ListContext
->Record
);
443 if (ListSize
> 0xFFFFFFFF)
445 ReleaseAttributeContext(ListContext
);
446 return STATUS_BUFFER_OVERFLOW
;
449 Context
->NonResidentStart
= ExAllocatePoolWithTag(NonPagedPool
, (ULONG
)ListSize
, TAG_NTFS
);
450 if (Context
->NonResidentStart
== NULL
)
452 ReleaseAttributeContext(ListContext
);
453 return STATUS_INSUFFICIENT_RESOURCES
;
456 if (ReadAttribute(Context
->Vcb
, ListContext
, 0, (PCHAR
)Context
->NonResidentStart
, (ULONG
)ListSize
) != ListSize
)
458 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
459 Context
->NonResidentStart
= NULL
;
460 ReleaseAttributeContext(ListContext
);
461 return STATUS_FILE_CORRUPT_ERROR
;
464 ReleaseAttributeContext(ListContext
);
465 Context
->NonResidentEnd
= (PNTFS_ATTR_RECORD
)((PCHAR
)Context
->NonResidentStart
+ ListSize
);
466 return STATUS_SUCCESS
;
471 InternalGetNextAttribute(PFIND_ATTR_CONTXT Context
)
473 PNTFS_ATTR_RECORD NextAttribute
;
475 if (Context
->CurrAttr
== (PVOID
)-1)
480 if (Context
->CurrAttr
>= Context
->FirstAttr
&&
481 Context
->CurrAttr
< Context
->LastAttr
)
483 if (Context
->CurrAttr
->Length
== 0)
485 DPRINT1("Broken length!\n");
486 Context
->CurrAttr
= (PVOID
)-1;
490 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
492 if (NextAttribute
> Context
->LastAttr
|| NextAttribute
< Context
->FirstAttr
)
494 DPRINT1("Broken length: 0x%lx!\n", Context
->CurrAttr
->Length
);
495 Context
->CurrAttr
= (PVOID
)-1;
499 Context
->Offset
+= ((ULONG_PTR
)NextAttribute
- (ULONG_PTR
)Context
->CurrAttr
);
500 Context
->CurrAttr
= NextAttribute
;
502 if (Context
->CurrAttr
< Context
->LastAttr
&&
503 Context
->CurrAttr
->Type
!= AttributeEnd
)
505 return Context
->CurrAttr
;
509 if (Context
->NonResidentStart
== NULL
)
511 Context
->CurrAttr
= (PVOID
)-1;
515 if (Context
->CurrAttr
< Context
->NonResidentStart
||
516 Context
->CurrAttr
>= Context
->NonResidentEnd
)
518 Context
->CurrAttr
= Context
->NonResidentStart
;
520 else if (Context
->CurrAttr
->Length
!= 0)
522 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
523 Context
->Offset
+= ((ULONG_PTR
)NextAttribute
- (ULONG_PTR
)Context
->CurrAttr
);
524 Context
->CurrAttr
= NextAttribute
;
528 DPRINT1("Broken length!\n");
529 Context
->CurrAttr
= (PVOID
)-1;
533 if (Context
->CurrAttr
< Context
->NonResidentEnd
&&
534 Context
->CurrAttr
->Type
!= AttributeEnd
)
536 return Context
->CurrAttr
;
539 Context
->CurrAttr
= (PVOID
)-1;
544 FindFirstAttribute(PFIND_ATTR_CONTXT Context
,
545 PDEVICE_EXTENSION Vcb
,
546 PFILE_RECORD_HEADER FileRecord
,
547 BOOLEAN OnlyResident
,
548 PNTFS_ATTR_RECORD
* Attribute
)
552 DPRINT("FindFistAttribute(%p, %p, %p, %p, %u, %p)\n", Context
, Vcb
, FileRecord
, OnlyResident
, Attribute
);
555 Context
->OnlyResident
= OnlyResident
;
556 Context
->FirstAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
557 Context
->CurrAttr
= Context
->FirstAttr
;
558 Context
->LastAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->BytesInUse
);
559 Context
->NonResidentStart
= NULL
;
560 Context
->NonResidentEnd
= NULL
;
561 Context
->Offset
= FileRecord
->AttributeOffset
;
563 if (Context
->FirstAttr
->Type
== AttributeEnd
)
565 Context
->CurrAttr
= (PVOID
)-1;
566 return STATUS_END_OF_FILE
;
568 else if (Context
->FirstAttr
->Type
== AttributeAttributeList
)
570 Status
= InternalReadNonResidentAttributes(Context
);
571 if (!NT_SUCCESS(Status
))
576 *Attribute
= InternalGetNextAttribute(Context
);
577 if (*Attribute
== NULL
)
579 return STATUS_END_OF_FILE
;
584 *Attribute
= Context
->CurrAttr
;
585 Context
->Offset
= (UCHAR
*)Context
->CurrAttr
- (UCHAR
*)FileRecord
;
588 return STATUS_SUCCESS
;
592 FindNextAttribute(PFIND_ATTR_CONTXT Context
,
593 PNTFS_ATTR_RECORD
* Attribute
)
597 DPRINT("FindNextAttribute(%p, %p)\n", Context
, Attribute
);
599 *Attribute
= InternalGetNextAttribute(Context
);
600 if (*Attribute
== NULL
)
602 return STATUS_END_OF_FILE
;
605 if (Context
->CurrAttr
->Type
!= AttributeAttributeList
)
607 return STATUS_SUCCESS
;
610 Status
= InternalReadNonResidentAttributes(Context
);
611 if (!NT_SUCCESS(Status
))
616 *Attribute
= InternalGetNextAttribute(Context
);
617 if (*Attribute
== NULL
)
619 return STATUS_END_OF_FILE
;
622 return STATUS_SUCCESS
;
626 FindCloseAttribute(PFIND_ATTR_CONTXT Context
)
628 if (Context
->NonResidentStart
!= NULL
)
630 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
631 Context
->NonResidentStart
= NULL
;
637 NtfsDumpFileNameAttribute(PNTFS_ATTR_RECORD Attribute
)
639 PFILENAME_ATTRIBUTE FileNameAttr
;
641 DbgPrint(" $FILE_NAME ");
643 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
645 FileNameAttr
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
646 DbgPrint(" (%x) '%.*S' ", FileNameAttr
->NameType
, FileNameAttr
->NameLength
, FileNameAttr
->Name
);
647 DbgPrint(" '%x' \n", FileNameAttr
->FileAttributes
);
648 DbgPrint(" AllocatedSize: %I64u\nDataSize: %I64u\n", FileNameAttr
->AllocatedSize
, FileNameAttr
->DataSize
);
654 NtfsDumpStandardInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
656 PSTANDARD_INFORMATION StandardInfoAttr
;
658 DbgPrint(" $STANDARD_INFORMATION ");
660 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
662 StandardInfoAttr
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
663 DbgPrint(" '%x' ", StandardInfoAttr
->FileAttribute
);
669 NtfsDumpVolumeNameAttribute(PNTFS_ATTR_RECORD Attribute
)
673 DbgPrint(" $VOLUME_NAME ");
675 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
677 VolumeName
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
678 DbgPrint(" '%.*S' ", Attribute
->Resident
.ValueLength
/ sizeof(WCHAR
), VolumeName
);
684 NtfsDumpVolumeInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
686 PVOLINFO_ATTRIBUTE VolInfoAttr
;
688 DbgPrint(" $VOLUME_INFORMATION ");
690 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
692 VolInfoAttr
= (PVOLINFO_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
693 DbgPrint(" NTFS Version %u.%u Flags 0x%04hx ",
694 VolInfoAttr
->MajorVersion
,
695 VolInfoAttr
->MinorVersion
,
702 NtfsDumpIndexRootAttribute(PNTFS_ATTR_RECORD Attribute
)
704 PINDEX_ROOT_ATTRIBUTE IndexRootAttr
;
706 IndexRootAttr
= (PINDEX_ROOT_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
708 if (IndexRootAttr
->AttributeType
== AttributeFileName
)
709 ASSERT(IndexRootAttr
->CollationRule
== COLLATION_FILE_NAME
);
711 DbgPrint(" $INDEX_ROOT (%uB, %u) ", IndexRootAttr
->SizeOfEntry
, IndexRootAttr
->ClustersPerIndexRecord
);
713 if (IndexRootAttr
->Header
.Flags
== INDEX_ROOT_SMALL
)
715 DbgPrint(" (small) ");
719 ASSERT(IndexRootAttr
->Header
.Flags
== INDEX_ROOT_LARGE
);
720 DbgPrint(" (large) ");
727 NtfsDumpAttribute(PDEVICE_EXTENSION Vcb
,
728 PNTFS_ATTR_RECORD Attribute
)
733 ULONGLONG runcount
= 0;
735 switch (Attribute
->Type
)
737 case AttributeFileName
:
738 NtfsDumpFileNameAttribute(Attribute
);
741 case AttributeStandardInformation
:
742 NtfsDumpStandardInformationAttribute(Attribute
);
745 case AttributeObjectId
:
746 DbgPrint(" $OBJECT_ID ");
749 case AttributeSecurityDescriptor
:
750 DbgPrint(" $SECURITY_DESCRIPTOR ");
753 case AttributeVolumeName
:
754 NtfsDumpVolumeNameAttribute(Attribute
);
757 case AttributeVolumeInformation
:
758 NtfsDumpVolumeInformationAttribute(Attribute
);
763 //DataBuf = ExAllocatePool(NonPagedPool,AttributeLengthAllocated(Attribute));
766 case AttributeIndexRoot
:
767 NtfsDumpIndexRootAttribute(Attribute
);
770 case AttributeIndexAllocation
:
771 DbgPrint(" $INDEX_ALLOCATION ");
774 case AttributeBitmap
:
775 DbgPrint(" $BITMAP ");
778 case AttributeReparsePoint
:
779 DbgPrint(" $REPARSE_POINT ");
782 case AttributeEAInformation
:
783 DbgPrint(" $EA_INFORMATION ");
790 case AttributePropertySet
:
791 DbgPrint(" $PROPERTY_SET ");
794 case AttributeLoggedUtilityStream
:
795 DbgPrint(" $LOGGED_UTILITY_STREAM ");
799 DbgPrint(" Attribute %lx ",
804 if (Attribute
->Type
!= AttributeAttributeList
)
806 if (Attribute
->NameLength
!= 0)
808 Name
.Length
= Attribute
->NameLength
* sizeof(WCHAR
);
809 Name
.MaximumLength
= Name
.Length
;
810 Name
.Buffer
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->NameOffset
);
812 DbgPrint("'%wZ' ", &Name
);
816 Attribute
->IsNonResident
? "non-resident" : "resident");
818 if (Attribute
->IsNonResident
)
820 FindRun(Attribute
,0,&lcn
, &runcount
);
822 DbgPrint(" AllocatedSize %I64u DataSize %I64u InitilizedSize %I64u\n",
823 Attribute
->NonResident
.AllocatedSize
, Attribute
->NonResident
.DataSize
, Attribute
->NonResident
.InitializedSize
);
824 DbgPrint(" logical clusters: %I64u - %I64u\n",
825 lcn
, lcn
+ runcount
- 1);
828 DbgPrint(" %u bytes of data\n", Attribute
->Resident
.ValueLength
);
833 VOID
NtfsDumpDataRunData(PUCHAR DataRun
)
835 UCHAR DataRunOffsetSize
;
836 UCHAR DataRunLengthSize
;
839 DbgPrint("%02x ", *DataRun
);
844 DataRunOffsetSize
= (*DataRun
>> 4) & 0xF;
845 DataRunLengthSize
= *DataRun
& 0xF;
848 for (i
= 0; i
< DataRunLengthSize
; i
++)
850 DbgPrint("%02x ", *DataRun
);
854 for (i
= 0; i
< DataRunOffsetSize
; i
++)
856 DbgPrint("%02x ", *DataRun
);
860 NtfsDumpDataRunData(DataRun
);
865 NtfsDumpDataRuns(PVOID StartOfRun
,
866 ULONGLONG CurrentLCN
)
868 PUCHAR DataRun
= StartOfRun
;
869 LONGLONG DataRunOffset
;
870 ULONGLONG DataRunLength
;
874 DPRINT1("Dumping data runs.\n\tData:\n\t\t");
875 NtfsDumpDataRunData(StartOfRun
);
876 DbgPrint("\n\tRuns:\n\t\tOff\t\tLCN\t\tLength\n");
879 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
881 if (DataRunOffset
!= -1)
882 CurrentLCN
+= DataRunOffset
;
884 DbgPrint("\t\t%I64d\t", DataRunOffset
);
885 if (DataRunOffset
< 99999)
887 DbgPrint("%I64u\t", CurrentLCN
);
888 if (CurrentLCN
< 99999)
890 DbgPrint("%I64u\n", DataRunLength
);
893 DbgPrint("\t\t00\n");
895 NtfsDumpDataRuns(DataRun
, CurrentLCN
);
900 NtfsDumpFileAttributes(PDEVICE_EXTENSION Vcb
,
901 PFILE_RECORD_HEADER FileRecord
)
904 FIND_ATTR_CONTXT Context
;
905 PNTFS_ATTR_RECORD Attribute
;
907 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
908 while (NT_SUCCESS(Status
))
910 NtfsDumpAttribute(Vcb
, Attribute
);
912 Status
= FindNextAttribute(&Context
, &Attribute
);
915 FindCloseAttribute(&Context
);
919 GetFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
920 PFILE_RECORD_HEADER FileRecord
,
923 FIND_ATTR_CONTXT Context
;
924 PNTFS_ATTR_RECORD Attribute
;
925 PFILENAME_ATTRIBUTE Name
;
928 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
929 while (NT_SUCCESS(Status
))
931 if (Attribute
->Type
== AttributeFileName
)
933 Name
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
934 if (Name
->NameType
== NameType
||
935 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_WIN32
) ||
936 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_DOS
))
938 FindCloseAttribute(&Context
);
943 Status
= FindNextAttribute(&Context
, &Attribute
);
946 FindCloseAttribute(&Context
);
952 * Returns the minimum number of bytes needed to represent the value of a
953 * 64-bit number. Used to encode data runs.
956 GetPackedByteCount(LONGLONG NumberToPack
,
962 if (NumberToPack
>= 0x0100000000000000)
964 if (NumberToPack
>= 0x0001000000000000)
966 if (NumberToPack
>= 0x0000010000000000)
968 if (NumberToPack
>= 0x0000000100000000)
970 if (NumberToPack
>= 0x0000000001000000)
972 if (NumberToPack
>= 0x0000000000010000)
974 if (NumberToPack
>= 0x0000000000000100)
979 if (NumberToPack
> 0)
981 // we have to make sure the number that gets encoded won't be interpreted as negative
982 if (NumberToPack
>= 0x0080000000000000)
984 if (NumberToPack
>= 0x0000800000000000)
986 if (NumberToPack
>= 0x0000008000000000)
988 if (NumberToPack
>= 0x0000000080000000)
990 if (NumberToPack
>= 0x0000000000800000)
992 if (NumberToPack
>= 0x0000000000008000)
994 if (NumberToPack
>= 0x0000000000000080)
1001 if (NumberToPack
<= 0xff80000000000000)
1003 if (NumberToPack
<= 0xffff800000000000)
1005 if (NumberToPack
<= 0xffffff8000000000)
1007 if (NumberToPack
<= 0xffffffff80000000)
1009 if (NumberToPack
<= 0xffffffffff800000)
1011 if (NumberToPack
<= 0xffffffffffff8000)
1013 if (NumberToPack
<= 0xffffffffffffff80)
1021 GetLastClusterInDataRun(PDEVICE_EXTENSION Vcb
, PNTFS_ATTR_RECORD Attribute
, PULONGLONG LastCluster
)
1023 LONGLONG DataRunOffset
;
1024 ULONGLONG DataRunLength
;
1025 LONGLONG DataRunStartLCN
;
1027 ULONGLONG LastLCN
= 0;
1028 PUCHAR DataRun
= (PUCHAR
)Attribute
+ Attribute
->NonResident
.MappingPairsOffset
;
1030 if (!Attribute
->IsNonResident
)
1031 return STATUS_INVALID_PARAMETER
;
1035 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1037 if (DataRunOffset
!= -1)
1040 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
1041 LastLCN
= DataRunStartLCN
;
1042 *LastCluster
= LastLCN
+ DataRunLength
- 1;
1049 return STATUS_SUCCESS
;
1052 PSTANDARD_INFORMATION
1053 GetStandardInformationFromRecord(PDEVICE_EXTENSION Vcb
,
1054 PFILE_RECORD_HEADER FileRecord
)
1057 FIND_ATTR_CONTXT Context
;
1058 PNTFS_ATTR_RECORD Attribute
;
1059 PSTANDARD_INFORMATION StdInfo
;
1061 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
1062 while (NT_SUCCESS(Status
))
1064 if (Attribute
->Type
== AttributeStandardInformation
)
1066 StdInfo
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1067 FindCloseAttribute(&Context
);
1071 Status
= FindNextAttribute(&Context
, &Attribute
);
1074 FindCloseAttribute(&Context
);
1079 GetBestFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
1080 PFILE_RECORD_HEADER FileRecord
)
1082 PFILENAME_ATTRIBUTE FileName
;
1084 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_POSIX
);
1085 if (FileName
== NULL
)
1087 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_WIN32
);
1088 if (FileName
== NULL
)
1090 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_DOS
);