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 $DATA attribute to a given FileRecord.
45 * Pointer to a complete file record to add the attribute to. Caller is responsible for
46 * ensuring FileRecord is large enough to contain $DATA.
48 * @param AttributeAddress
49 * Pointer to the region of memory that will receive the $DATA attribute.
50 * This address must reside within FileRecord. Must be aligned to an 8-byte boundary (relative to FileRecord).
53 * STATUS_SUCCESS on success. STATUS_NOT_IMPLEMENTED if target address isn't at the end
54 * of the given file record.
57 * Only adding the attribute to the end of the file record is supported; AttributeAddress must
58 * be of type AttributeEnd.
59 * As it's implemented, this function is only intended to assist in creating new file records. It
60 * could be made more general-purpose by considering file records with an $ATTRIBUTE_LIST.
61 * It's the caller's responsibility to ensure the given file record has enough memory allocated
65 AddData(PFILE_RECORD_HEADER FileRecord
,
66 PNTFS_ATTR_RECORD AttributeAddress
)
68 ULONG ResidentHeaderLength
= FIELD_OFFSET(NTFS_ATTR_RECORD
, Resident
.Reserved
) + sizeof(UCHAR
);
69 ULONG FileRecordEnd
= AttributeAddress
->Length
;
71 if (AttributeAddress
->Type
!= AttributeEnd
)
73 DPRINT1("FIXME: Can only add $DATA attribute to the end of a file record.\n");
74 return STATUS_NOT_IMPLEMENTED
;
77 AttributeAddress
->Type
= AttributeData
;
78 AttributeAddress
->Length
= ResidentHeaderLength
;
79 AttributeAddress
->Length
= ALIGN_UP_BY(AttributeAddress
->Length
, 8);
80 AttributeAddress
->Resident
.ValueLength
= 0;
81 AttributeAddress
->Resident
.ValueOffset
= ResidentHeaderLength
;
83 // for unnamed $DATA attributes, NameOffset equals header length
84 AttributeAddress
->NameOffset
= ResidentHeaderLength
;
85 AttributeAddress
->Instance
= FileRecord
->NextAttributeNumber
++;
87 // move the attribute-end and file-record-end markers to the end of the file record
88 AttributeAddress
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)AttributeAddress
+ AttributeAddress
->Length
);
89 SetFileRecordEnd(FileRecord
, AttributeAddress
, FileRecordEnd
);
91 return STATUS_SUCCESS
;
98 * Adds a $FILE_NAME attribute to a given FileRecord.
101 * Pointer to a complete file record to add the attribute to. Caller is responsible for
102 * ensuring FileRecord is large enough to contain $FILE_NAME.
104 * @param AttributeAddress
105 * Pointer to the region of memory that will receive the $FILE_NAME attribute.
106 * This address must reside within FileRecord. Must be aligned to an 8-byte boundary (relative to FileRecord).
109 * Points to the target disk's DEVICE_EXTENSION.
112 * Pointer to the FILE_OBJECT which represents the new name.
113 * This parameter is used to determine the filename and parent directory.
115 * @param CaseSensitive
116 * Boolean indicating if the function should operate in case-sensitive mode. This will be TRUE
117 * if an application opened the file with the FILE_FLAG_POSIX_SEMANTICS flag.
119 * @param ParentMftIndex
120 * Pointer to a ULONGLONG which will receive the index of the parent directory.
123 * STATUS_SUCCESS on success. STATUS_NOT_IMPLEMENTED if target address isn't at the end
124 * of the given file record.
127 * Only adding the attribute to the end of the file record is supported; AttributeAddress must
128 * be of type AttributeEnd.
129 * As it's implemented, this function is only intended to assist in creating new file records. It
130 * could be made more general-purpose by considering file records with an $ATTRIBUTE_LIST.
131 * It's the caller's responsibility to ensure the given file record has enough memory allocated
135 AddFileName(PFILE_RECORD_HEADER FileRecord
,
136 PNTFS_ATTR_RECORD AttributeAddress
,
137 PDEVICE_EXTENSION DeviceExt
,
138 PFILE_OBJECT FileObject
,
139 BOOLEAN CaseSensitive
,
140 PULONGLONG ParentMftIndex
)
142 ULONG ResidentHeaderLength
= FIELD_OFFSET(NTFS_ATTR_RECORD
, Resident
.Reserved
) + sizeof(UCHAR
);
143 PFILENAME_ATTRIBUTE FileNameAttribute
;
144 LARGE_INTEGER SystemTime
;
145 ULONG FileRecordEnd
= AttributeAddress
->Length
;
146 ULONGLONG CurrentMFTIndex
= NTFS_FILE_ROOT
;
147 UNICODE_STRING Current
, Remaining
, FilenameNoPath
;
148 NTSTATUS Status
= STATUS_SUCCESS
;
150 WCHAR Buffer
[MAX_PATH
];
152 if (AttributeAddress
->Type
!= AttributeEnd
)
154 DPRINT1("FIXME: Can only add $FILE_NAME attribute to the end of a file record.\n");
155 return STATUS_NOT_IMPLEMENTED
;
158 AttributeAddress
->Type
= AttributeFileName
;
159 AttributeAddress
->Instance
= FileRecord
->NextAttributeNumber
++;
161 FileNameAttribute
= (PFILENAME_ATTRIBUTE
)((LONG_PTR
)AttributeAddress
+ ResidentHeaderLength
);
164 KeQuerySystemTime(&SystemTime
);
165 FileNameAttribute
->CreationTime
= SystemTime
.QuadPart
;
166 FileNameAttribute
->ChangeTime
= SystemTime
.QuadPart
;
167 FileNameAttribute
->LastWriteTime
= SystemTime
.QuadPart
;
168 FileNameAttribute
->LastAccessTime
= SystemTime
.QuadPart
;
170 FileNameAttribute
->FileAttributes
= NTFS_FILE_TYPE_ARCHIVE
;
172 // we need to extract the filename from the path
173 DPRINT1("Pathname: %wZ\n", &FileObject
->FileName
);
175 RtlInitEmptyUnicodeString(&FilenameNoPath
, Buffer
, MAX_PATH
);
177 FsRtlDissectName(FileObject
->FileName
, &Current
, &Remaining
);
179 while (Current
.Length
!= 0)
181 DPRINT1("Current: %wZ\n", &Current
);
183 if(Remaining
.Length
!= 0)
184 RtlCopyUnicodeString(&FilenameNoPath
, &Remaining
);
187 Status
= NtfsFindMftRecord(DeviceExt
,
194 if (!NT_SUCCESS(Status
))
197 if (Remaining
.Length
== 0 )
199 if(Current
.Length
!= 0)
200 RtlCopyUnicodeString(&FilenameNoPath
, &Current
);
204 FsRtlDissectName(Remaining
, &Current
, &Remaining
);
207 DPRINT1("MFT Index of parent: %I64u\n", CurrentMFTIndex
);
209 // set reference to parent directory
210 FileNameAttribute
->DirectoryFileReferenceNumber
= CurrentMFTIndex
;
211 *ParentMftIndex
= CurrentMFTIndex
;
213 DPRINT1("SequenceNumber: 0x%02x\n", FileRecord
->SequenceNumber
);
215 // The highest 2 bytes should be the sequence number, unless the parent happens to be root
216 if (CurrentMFTIndex
== NTFS_FILE_ROOT
)
217 FileNameAttribute
->DirectoryFileReferenceNumber
|= (ULONGLONG
)NTFS_FILE_ROOT
<< 48;
219 FileNameAttribute
->DirectoryFileReferenceNumber
|= (ULONGLONG
)FileRecord
->SequenceNumber
<< 48;
221 DPRINT1("FileNameAttribute->DirectoryFileReferenceNumber: 0x%016I64x\n", FileNameAttribute
->DirectoryFileReferenceNumber
);
223 FileNameAttribute
->NameLength
= FilenameNoPath
.Length
/ sizeof(WCHAR
);
224 RtlCopyMemory(FileNameAttribute
->Name
, FilenameNoPath
.Buffer
, FilenameNoPath
.Length
);
226 // For now, we're emulating the way Windows behaves when 8.3 name generation is disabled
227 // TODO: add DOS Filename as needed
228 if (RtlIsNameLegalDOS8Dot3(&FilenameNoPath
, NULL
, NULL
))
229 FileNameAttribute
->NameType
= NTFS_FILE_NAME_WIN32_AND_DOS
;
231 FileNameAttribute
->NameType
= NTFS_FILE_NAME_POSIX
;
233 FileRecord
->LinkCount
++;
235 AttributeAddress
->Length
= ResidentHeaderLength
+
236 FIELD_OFFSET(FILENAME_ATTRIBUTE
, Name
) + FilenameNoPath
.Length
;
237 AttributeAddress
->Length
= ALIGN_UP_BY(AttributeAddress
->Length
, 8);
239 AttributeAddress
->Resident
.ValueLength
= FIELD_OFFSET(FILENAME_ATTRIBUTE
, Name
) + FilenameNoPath
.Length
;
240 AttributeAddress
->Resident
.ValueOffset
= ResidentHeaderLength
;
241 AttributeAddress
->Resident
.Flags
= 1; // indexed
243 // move the attribute-end and file-record-end markers to the end of the file record
244 AttributeAddress
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)AttributeAddress
+ AttributeAddress
->Length
);
245 SetFileRecordEnd(FileRecord
, AttributeAddress
, FileRecordEnd
);
254 * Adds a run of allocated clusters to a non-resident attribute.
257 * Pointer to an NTFS_VCB for the destination volume.
260 * Pointer to an NTFS_ATTR_CONTEXT describing the destination attribute.
263 * Byte offset of the destination attribute relative to its file record.
266 * Pointer to a complete copy of the file record containing the destination attribute. Must be at least
267 * Vcb->NtfsInfo.BytesPerFileRecord bytes long.
269 * @param NextAssignedCluster
270 * Logical cluster number of the start of the data run being added.
273 * How many clusters are in the data run being added. Can't be 0.
276 * STATUS_SUCCESS on success. STATUS_INVALID_PARAMETER if AttrContext describes a resident attribute.
277 * STATUS_INSUFFICIENT_RESOURCES if ConvertDataRunsToLargeMCB() fails or if we fail to allocate a
278 * buffer for the new data runs.
279 * STATUS_INSUFFICIENT_RESOURCES or STATUS_UNSUCCESSFUL if FsRtlAddLargeMcbEntry() fails.
280 * STATUS_BUFFER_TOO_SMALL if ConvertLargeMCBToDataRuns() fails.
281 * STATUS_NOT_IMPLEMENTED if we need to migrate the attribute to an attribute list (TODO).
284 * Clusters should have been allocated previously with NtfsAllocateClusters().
289 AddRun(PNTFS_VCB Vcb
,
290 PNTFS_ATTR_CONTEXT AttrContext
,
292 PFILE_RECORD_HEADER FileRecord
,
293 ULONGLONG NextAssignedCluster
,
297 int DataRunMaxLength
;
298 PNTFS_ATTR_RECORD DestinationAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ AttrOffset
);
299 ULONG NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.Length
;
300 ULONGLONG NextVBN
= 0;
305 if (!AttrContext
->Record
.IsNonResident
)
306 return STATUS_INVALID_PARAMETER
;
308 if (AttrContext
->Record
.NonResident
.AllocatedSize
!= 0)
309 NextVBN
= AttrContext
->Record
.NonResident
.HighestVCN
+ 1;
311 // Add newly-assigned clusters to mcb
313 if (!FsRtlAddLargeMcbEntry(&AttrContext
->DataRunsMCB
,
318 ExRaiseStatus(STATUS_UNSUCCESSFUL
);
320 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
) {
321 _SEH2_YIELD(_SEH2_GetExceptionCode());
324 RunBuffer
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
327 DPRINT1("ERROR: Couldn't allocate memory for data runs!\n");
328 return STATUS_INSUFFICIENT_RESOURCES
;
331 // Convert the map control block back to encoded data runs
332 ConvertLargeMCBToDataRuns(&AttrContext
->DataRunsMCB
, RunBuffer
, Vcb
->NtfsInfo
.BytesPerCluster
, &RunBufferSize
);
334 // Get the amount of free space between the start of the of the first data run and the attribute end
335 DataRunMaxLength
= AttrContext
->Record
.Length
- AttrContext
->Record
.NonResident
.MappingPairsOffset
;
337 // Do we need to extend the attribute (or convert to attribute list)?
338 if (DataRunMaxLength
< RunBufferSize
)
340 PNTFS_ATTR_RECORD NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ NextAttributeOffset
);
341 DataRunMaxLength
+= Vcb
->NtfsInfo
.BytesPerFileRecord
- NextAttributeOffset
- (sizeof(ULONG
) * 2);
343 // Can we move the end of the attribute?
344 if (NextAttribute
->Type
!= AttributeEnd
|| DataRunMaxLength
< RunBufferSize
- 1)
346 DPRINT1("FIXME: Need to create attribute list! Max Data Run Length available: %d\n", DataRunMaxLength
);
347 if (NextAttribute
->Type
!= AttributeEnd
)
348 DPRINT1("There's another attribute after this one with type %0xlx\n", NextAttribute
->Type
);
349 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
350 return STATUS_NOT_IMPLEMENTED
;
353 // calculate position of end markers
354 NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.NonResident
.MappingPairsOffset
+ RunBufferSize
;
355 NextAttributeOffset
= ALIGN_UP_BY(NextAttributeOffset
, 8);
358 DestinationAttribute
->Length
= NextAttributeOffset
- AttrOffset
;
359 AttrContext
->Record
.Length
= DestinationAttribute
->Length
;
361 // End the file record
362 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ NextAttributeOffset
);
363 SetFileRecordEnd(FileRecord
, NextAttribute
, FILE_RECORD_END
);
367 DestinationAttribute
->NonResident
.HighestVCN
=
368 AttrContext
->Record
.NonResident
.HighestVCN
= max(NextVBN
- 1 + RunLength
,
369 AttrContext
->Record
.NonResident
.HighestVCN
);
371 // Write data runs to destination attribute
372 RtlCopyMemory((PVOID
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
),
376 // Update the file record
377 Status
= UpdateFileRecord(Vcb
, AttrContext
->FileMFTIndex
, FileRecord
);
379 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
381 NtfsDumpDataRuns((PUCHAR
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
), 0);
387 * @name AddStandardInformation
390 * Adds a $STANDARD_INFORMATION attribute to a given FileRecord.
393 * Pointer to a complete file record to add the attribute to. Caller is responsible for
394 * ensuring FileRecord is large enough to contain $STANDARD_INFORMATION.
396 * @param AttributeAddress
397 * Pointer to the region of memory that will receive the $STANDARD_INFORMATION attribute.
398 * This address must reside within FileRecord. Must be aligned to an 8-byte boundary (relative to FileRecord).
401 * STATUS_SUCCESS on success. STATUS_NOT_IMPLEMENTED if target address isn't at the end
402 * of the given file record.
405 * Only adding the attribute to the end of the file record is supported; AttributeAddress must
406 * be of type AttributeEnd.
407 * As it's implemented, this function is only intended to assist in creating new file records. It
408 * could be made more general-purpose by considering file records with an $ATTRIBUTE_LIST.
409 * It's the caller's responsibility to ensure the given file record has enough memory allocated
413 AddStandardInformation(PFILE_RECORD_HEADER FileRecord
,
414 PNTFS_ATTR_RECORD AttributeAddress
)
416 ULONG ResidentHeaderLength
= FIELD_OFFSET(NTFS_ATTR_RECORD
, Resident
.Reserved
) + sizeof(UCHAR
);
417 PSTANDARD_INFORMATION StandardInfo
= (PSTANDARD_INFORMATION
)((LONG_PTR
)AttributeAddress
+ ResidentHeaderLength
);
418 LARGE_INTEGER SystemTime
;
419 ULONG FileRecordEnd
= AttributeAddress
->Length
;
421 if (AttributeAddress
->Type
!= AttributeEnd
)
423 DPRINT1("FIXME: Can only add $STANDARD_INFORMATION attribute to the end of a file record.\n");
424 return STATUS_NOT_IMPLEMENTED
;
427 AttributeAddress
->Type
= AttributeStandardInformation
;
428 AttributeAddress
->Length
= sizeof(STANDARD_INFORMATION
) + ResidentHeaderLength
;
429 AttributeAddress
->Length
= ALIGN_UP_BY(AttributeAddress
->Length
, 8);
430 AttributeAddress
->Resident
.ValueLength
= sizeof(STANDARD_INFORMATION
);
431 AttributeAddress
->Resident
.ValueOffset
= ResidentHeaderLength
;
432 AttributeAddress
->Instance
= FileRecord
->NextAttributeNumber
++;
434 // set dates and times
435 KeQuerySystemTime(&SystemTime
);
436 StandardInfo
->CreationTime
= SystemTime
.QuadPart
;
437 StandardInfo
->ChangeTime
= SystemTime
.QuadPart
;
438 StandardInfo
->LastWriteTime
= SystemTime
.QuadPart
;
439 StandardInfo
->LastAccessTime
= SystemTime
.QuadPart
;
440 StandardInfo
->FileAttribute
= NTFS_FILE_TYPE_ARCHIVE
;
442 // move the attribute-end and file-record-end markers to the end of the file record
443 AttributeAddress
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)AttributeAddress
+ AttributeAddress
->Length
);
444 SetFileRecordEnd(FileRecord
, AttributeAddress
, FileRecordEnd
);
446 return STATUS_SUCCESS
;
450 * @name ConvertDataRunsToLargeMCB
453 * Converts binary data runs to a map control block.
456 * Pointer to the run data
459 * Pointer to an unitialized LARGE_MCB structure.
462 * STATUS_SUCCESS on success, STATUS_INSUFFICIENT_RESOURCES or STATUS_UNSUCCESSFUL if we fail to
463 * initialize the mcb or add an entry.
466 * Initializes the LARGE_MCB pointed to by DataRunsMCB. If this function succeeds, you
467 * need to call FsRtlUninitializeLargeMcb() when you're done with DataRunsMCB. This
468 * function will ensure the LargeMCB has been unitialized in case of failure.
472 ConvertDataRunsToLargeMCB(PUCHAR DataRun
,
473 PLARGE_MCB DataRunsMCB
,
476 LONGLONG DataRunOffset
;
477 ULONGLONG DataRunLength
;
478 LONGLONG DataRunStartLCN
;
479 ULONGLONG LastLCN
= 0;
481 // Initialize the MCB, potentially catch an exception
483 FsRtlInitializeLargeMcb(DataRunsMCB
, NonPagedPool
);
484 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
) {
485 _SEH2_YIELD(return _SEH2_GetExceptionCode());
488 while (*DataRun
!= 0)
490 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
492 if (DataRunOffset
!= -1)
495 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
496 LastLCN
= DataRunStartLCN
;
499 if (!FsRtlAddLargeMcbEntry(DataRunsMCB
,
504 ExRaiseStatus(STATUS_UNSUCCESSFUL
);
506 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
) {
507 FsRtlUninitializeLargeMcb(DataRunsMCB
);
508 _SEH2_YIELD(return _SEH2_GetExceptionCode());
513 *pNextVBN
+= DataRunLength
;
516 return STATUS_SUCCESS
;
520 * @name ConvertLargeMCBToDataRuns
523 * Converts a map control block to a series of encoded data runs (used by non-resident attributes).
526 * Pointer to a LARGE_MCB structure describing the data runs.
529 * Pointer to the buffer that will receive the encoded data runs.
531 * @param MaxBufferSize
532 * Size of RunBuffer, in bytes.
534 * @param UsedBufferSize
535 * Pointer to a ULONG that will receive the size of the data runs in bytes. Can't be NULL.
538 * STATUS_SUCCESS on success, STATUS_BUFFER_TOO_SMALL if RunBuffer is too small to contain the
543 ConvertLargeMCBToDataRuns(PLARGE_MCB DataRunsMCB
,
546 PULONG UsedBufferSize
)
548 NTSTATUS Status
= STATUS_SUCCESS
;
549 ULONG RunBufferOffset
= 0;
550 LONGLONG DataRunOffset
;
551 ULONGLONG LastLCN
= 0;
552 LONGLONG Vbn
, Lbn
, Count
;
556 DPRINT("\t[Vbn, Lbn, Count]\n");
558 // convert each mcb entry to a data run
559 for (i
= 0; FsRtlGetNextLargeMcbEntry(DataRunsMCB
, i
, &Vbn
, &Lbn
, &Count
); i
++)
561 UCHAR DataRunOffsetSize
= 0;
562 UCHAR DataRunLengthSize
= 0;
563 UCHAR ControlByte
= 0;
566 DPRINT("\t[%I64d, %I64d,%I64d]\n", Vbn
, Lbn
, Count
);
568 // TODO: check for holes and convert to sparse runs
569 DataRunOffset
= Lbn
- LastLCN
;
572 // now we need to determine how to represent DataRunOffset with the minimum number of bytes
573 DPRINT("Determining how many bytes needed to represent %I64x\n", DataRunOffset
);
574 DataRunOffsetSize
= GetPackedByteCount(DataRunOffset
, TRUE
);
575 DPRINT("%d bytes needed.\n", DataRunOffsetSize
);
577 // determine how to represent DataRunLengthSize with the minimum number of bytes
578 DPRINT("Determining how many bytes needed to represent %I64x\n", Count
);
579 DataRunLengthSize
= GetPackedByteCount(Count
, TRUE
);
580 DPRINT("%d bytes needed.\n", DataRunLengthSize
);
582 // ensure the next data run + end marker would be <= Max buffer size
583 if (RunBufferOffset
+ 2 + DataRunLengthSize
+ DataRunOffsetSize
> MaxBufferSize
)
585 Status
= STATUS_BUFFER_TOO_SMALL
;
586 DPRINT1("FIXME: Ran out of room in buffer for data runs!\n");
590 // pack and copy the control byte
591 ControlByte
= (DataRunOffsetSize
<< 4) + DataRunLengthSize
;
592 RunBuffer
[RunBufferOffset
++] = ControlByte
;
594 // copy DataRunLength
595 RtlCopyMemory(RunBuffer
+ RunBufferOffset
, &Count
, DataRunLengthSize
);
596 RunBufferOffset
+= DataRunLengthSize
;
598 // copy DataRunOffset
599 RtlCopyMemory(RunBuffer
+ RunBufferOffset
, &DataRunOffset
, DataRunOffsetSize
);
600 RunBufferOffset
+= DataRunOffsetSize
;
604 RunBuffer
[RunBufferOffset
++] = 0;
606 *UsedBufferSize
= RunBufferOffset
;
607 DPRINT("New Size of DataRuns: %ld\n", *UsedBufferSize
);
613 DecodeRun(PUCHAR DataRun
,
614 LONGLONG
*DataRunOffset
,
615 ULONGLONG
*DataRunLength
)
617 UCHAR DataRunOffsetSize
;
618 UCHAR DataRunLengthSize
;
621 DataRunOffsetSize
= (*DataRun
>> 4) & 0xF;
622 DataRunLengthSize
= *DataRun
& 0xF;
626 for (i
= 0; i
< DataRunLengthSize
; i
++)
628 *DataRunLength
+= ((ULONG64
)*DataRun
) << (i
* 8);
632 /* NTFS 3+ sparse files */
633 if (DataRunOffsetSize
== 0)
639 for (i
= 0; i
< DataRunOffsetSize
- 1; i
++)
641 *DataRunOffset
+= ((ULONG64
)*DataRun
) << (i
* 8);
644 /* The last byte contains sign so we must process it different way. */
645 *DataRunOffset
= ((LONG64
)(CHAR
)(*(DataRun
++)) << (i
* 8)) + *DataRunOffset
;
648 DPRINT("DataRunOffsetSize: %x\n", DataRunOffsetSize
);
649 DPRINT("DataRunLengthSize: %x\n", DataRunLengthSize
);
650 DPRINT("DataRunOffset: %x\n", *DataRunOffset
);
651 DPRINT("DataRunLength: %x\n", *DataRunLength
);
657 FindRun(PNTFS_ATTR_RECORD NresAttr
,
662 if (vcn
< NresAttr
->NonResident
.LowestVCN
|| vcn
> NresAttr
->NonResident
.HighestVCN
)
665 DecodeRun((PUCHAR
)((ULONG_PTR
)NresAttr
+ NresAttr
->NonResident
.MappingPairsOffset
), (PLONGLONG
)lcn
, count
);
674 * Shrinks the allocation size of a non-resident attribute by a given number of clusters.
675 * Frees the clusters from the volume's $BITMAP file as well as the attribute's data runs.
678 * Pointer to an NTFS_VCB for the destination volume.
681 * Pointer to an NTFS_ATTR_CONTEXT describing the attribute from which the clusters will be freed.
684 * Byte offset of the destination attribute relative to its file record.
687 * Pointer to a complete copy of the file record containing the attribute. Must be at least
688 * Vcb->NtfsInfo.BytesPerFileRecord bytes long.
690 * @param ClustersToFree
691 * Number of clusters that should be freed from the end of the data stream. Must be no more
692 * Than the number of clusters assigned to the attribute (HighestVCN + 1).
695 * STATUS_SUCCESS on success. STATUS_INVALID_PARAMETER if AttrContext describes a resident attribute,
696 * or if the caller requested more clusters be freed than the attribute has been allocated.
697 * STATUS_INSUFFICIENT_RESOURCES if allocating a buffer for the data runs fails or
698 * if ConvertDataRunsToLargeMCB() fails.
699 * STATUS_BUFFER_TOO_SMALL if ConvertLargeMCBToDataRuns() fails.
704 FreeClusters(PNTFS_VCB Vcb
,
705 PNTFS_ATTR_CONTEXT AttrContext
,
707 PFILE_RECORD_HEADER FileRecord
,
708 ULONG ClustersToFree
)
710 NTSTATUS Status
= STATUS_SUCCESS
;
711 ULONG ClustersLeftToFree
= ClustersToFree
;
713 PNTFS_ATTR_RECORD DestinationAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ AttrOffset
);
714 ULONG NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.Length
;
715 PNTFS_ATTR_RECORD NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ NextAttributeOffset
);
718 ULONG RunBufferSize
= 0;
720 PFILE_RECORD_HEADER BitmapRecord
;
721 PNTFS_ATTR_CONTEXT DataContext
;
722 ULONGLONG BitmapDataSize
;
727 if (!AttrContext
->Record
.IsNonResident
)
729 return STATUS_INVALID_PARAMETER
;
732 // Read the $Bitmap file
733 BitmapRecord
= ExAllocatePoolWithTag(NonPagedPool
,
734 Vcb
->NtfsInfo
.BytesPerFileRecord
,
736 if (BitmapRecord
== NULL
)
738 DPRINT1("Error: Unable to allocate memory for bitmap file record!\n");
739 return STATUS_NO_MEMORY
;
742 Status
= ReadFileRecord(Vcb
, NTFS_FILE_BITMAP
, BitmapRecord
);
743 if (!NT_SUCCESS(Status
))
745 DPRINT1("Error: Unable to read file record for bitmap!\n");
746 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
750 Status
= FindAttribute(Vcb
, BitmapRecord
, AttributeData
, L
"", 0, &DataContext
, NULL
);
751 if (!NT_SUCCESS(Status
))
753 DPRINT1("Error: Unable to find data attribute for bitmap file!\n");
754 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
758 BitmapDataSize
= AttributeDataLength(&DataContext
->Record
);
759 BitmapDataSize
= min(BitmapDataSize
, 0xffffffff);
760 ASSERT((BitmapDataSize
* 8) >= Vcb
->NtfsInfo
.ClusterCount
);
761 BitmapData
= ExAllocatePoolWithTag(NonPagedPool
, ROUND_UP(BitmapDataSize
, Vcb
->NtfsInfo
.BytesPerSector
), TAG_NTFS
);
762 if (BitmapData
== NULL
)
764 DPRINT1("Error: Unable to allocate memory for bitmap file data!\n");
765 ReleaseAttributeContext(DataContext
);
766 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
770 ReadAttribute(Vcb
, DataContext
, 0, (PCHAR
)BitmapData
, (ULONG
)BitmapDataSize
);
772 RtlInitializeBitMap(&Bitmap
, (PULONG
)BitmapData
, Vcb
->NtfsInfo
.ClusterCount
);
774 // free clusters in $BITMAP file
775 while (ClustersLeftToFree
> 0)
777 LONGLONG LargeVbn
, LargeLbn
;
779 if (!FsRtlLookupLastLargeMcbEntry(&AttrContext
->DataRunsMCB
, &LargeVbn
, &LargeLbn
))
781 Status
= STATUS_INVALID_PARAMETER
;
782 DPRINT1("DRIVER ERROR: FreeClusters called to free %lu clusters, which is %lu more clusters than are assigned to attribute!",
790 // deallocate this cluster
791 RtlClearBits(&Bitmap
, LargeLbn
, 1);
793 FsRtlTruncateLargeMcb(&AttrContext
->DataRunsMCB
, AttrContext
->Record
.NonResident
.HighestVCN
);
795 // decrement HighestVCN, but don't let it go below 0
796 AttrContext
->Record
.NonResident
.HighestVCN
= min(AttrContext
->Record
.NonResident
.HighestVCN
, AttrContext
->Record
.NonResident
.HighestVCN
- 1);
797 ClustersLeftToFree
--;
800 // update $BITMAP file on disk
801 Status
= WriteAttribute(Vcb
, DataContext
, 0, BitmapData
, (ULONG
)BitmapDataSize
, &LengthWritten
);
802 if (!NT_SUCCESS(Status
))
804 ReleaseAttributeContext(DataContext
);
805 ExFreePoolWithTag(BitmapData
, TAG_NTFS
);
806 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
810 ReleaseAttributeContext(DataContext
);
811 ExFreePoolWithTag(BitmapData
, TAG_NTFS
);
812 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
814 // Save updated data runs to file record
816 // Allocate some memory for a new RunBuffer
817 RunBuffer
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
820 DPRINT1("ERROR: Couldn't allocate memory for data runs!\n");
821 return STATUS_INSUFFICIENT_RESOURCES
;
824 // Convert the map control block back to encoded data runs
825 ConvertLargeMCBToDataRuns(&AttrContext
->DataRunsMCB
, RunBuffer
, Vcb
->NtfsInfo
.BytesPerCluster
, &RunBufferSize
);
828 DestinationAttribute
->NonResident
.HighestVCN
= AttrContext
->Record
.NonResident
.HighestVCN
;
830 // Write data runs to destination attribute
831 RtlCopyMemory((PVOID
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
),
835 // Is DestinationAttribute the last attribute in the file record?
836 if (NextAttribute
->Type
== AttributeEnd
)
838 // update attribute length
839 AttrContext
->Record
.Length
= ALIGN_UP_BY(AttrContext
->Record
.NonResident
.MappingPairsOffset
+ RunBufferSize
, 8);
840 DestinationAttribute
->Length
= AttrContext
->Record
.Length
;
843 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->Length
);
844 SetFileRecordEnd(FileRecord
, NextAttribute
, FILE_RECORD_END
);
847 // Update the file record
848 Status
= UpdateFileRecord(Vcb
, AttrContext
->FileMFTIndex
, FileRecord
);
850 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
852 NtfsDumpDataRuns((PUCHAR
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
), 0);
859 InternalReadNonResidentAttributes(PFIND_ATTR_CONTXT Context
)
862 PNTFS_ATTR_RECORD Attribute
;
863 PNTFS_ATTR_CONTEXT ListContext
;
865 DPRINT("InternalReadNonResidentAttributes(%p)\n", Context
);
867 Attribute
= Context
->CurrAttr
;
868 ASSERT(Attribute
->Type
== AttributeAttributeList
);
870 if (Context
->OnlyResident
)
872 Context
->NonResidentStart
= NULL
;
873 Context
->NonResidentEnd
= NULL
;
874 return STATUS_SUCCESS
;
877 if (Context
->NonResidentStart
!= NULL
)
879 return STATUS_FILE_CORRUPT_ERROR
;
882 ListContext
= PrepareAttributeContext(Attribute
);
883 ListSize
= AttributeDataLength(&ListContext
->Record
);
884 if (ListSize
> 0xFFFFFFFF)
886 ReleaseAttributeContext(ListContext
);
887 return STATUS_BUFFER_OVERFLOW
;
890 Context
->NonResidentStart
= ExAllocatePoolWithTag(NonPagedPool
, (ULONG
)ListSize
, TAG_NTFS
);
891 if (Context
->NonResidentStart
== NULL
)
893 ReleaseAttributeContext(ListContext
);
894 return STATUS_INSUFFICIENT_RESOURCES
;
897 if (ReadAttribute(Context
->Vcb
, ListContext
, 0, (PCHAR
)Context
->NonResidentStart
, (ULONG
)ListSize
) != ListSize
)
899 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
900 Context
->NonResidentStart
= NULL
;
901 ReleaseAttributeContext(ListContext
);
902 return STATUS_FILE_CORRUPT_ERROR
;
905 ReleaseAttributeContext(ListContext
);
906 Context
->NonResidentEnd
= (PNTFS_ATTR_RECORD
)((PCHAR
)Context
->NonResidentStart
+ ListSize
);
907 return STATUS_SUCCESS
;
912 InternalGetNextAttribute(PFIND_ATTR_CONTXT Context
)
914 PNTFS_ATTR_RECORD NextAttribute
;
916 if (Context
->CurrAttr
== (PVOID
)-1)
921 if (Context
->CurrAttr
>= Context
->FirstAttr
&&
922 Context
->CurrAttr
< Context
->LastAttr
)
924 if (Context
->CurrAttr
->Length
== 0)
926 DPRINT1("Broken length!\n");
927 Context
->CurrAttr
= (PVOID
)-1;
931 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
933 if (NextAttribute
> Context
->LastAttr
|| NextAttribute
< Context
->FirstAttr
)
935 DPRINT1("Broken length: 0x%lx!\n", Context
->CurrAttr
->Length
);
936 Context
->CurrAttr
= (PVOID
)-1;
940 Context
->Offset
+= ((ULONG_PTR
)NextAttribute
- (ULONG_PTR
)Context
->CurrAttr
);
941 Context
->CurrAttr
= NextAttribute
;
943 if (Context
->CurrAttr
< Context
->LastAttr
&&
944 Context
->CurrAttr
->Type
!= AttributeEnd
)
946 return Context
->CurrAttr
;
950 if (Context
->NonResidentStart
== NULL
)
952 Context
->CurrAttr
= (PVOID
)-1;
956 if (Context
->CurrAttr
< Context
->NonResidentStart
||
957 Context
->CurrAttr
>= Context
->NonResidentEnd
)
959 Context
->CurrAttr
= Context
->NonResidentStart
;
961 else if (Context
->CurrAttr
->Length
!= 0)
963 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
964 Context
->Offset
+= ((ULONG_PTR
)NextAttribute
- (ULONG_PTR
)Context
->CurrAttr
);
965 Context
->CurrAttr
= NextAttribute
;
969 DPRINT1("Broken length!\n");
970 Context
->CurrAttr
= (PVOID
)-1;
974 if (Context
->CurrAttr
< Context
->NonResidentEnd
&&
975 Context
->CurrAttr
->Type
!= AttributeEnd
)
977 return Context
->CurrAttr
;
980 Context
->CurrAttr
= (PVOID
)-1;
985 FindFirstAttribute(PFIND_ATTR_CONTXT Context
,
986 PDEVICE_EXTENSION Vcb
,
987 PFILE_RECORD_HEADER FileRecord
,
988 BOOLEAN OnlyResident
,
989 PNTFS_ATTR_RECORD
* Attribute
)
993 DPRINT("FindFistAttribute(%p, %p, %p, %p, %u, %p)\n", Context
, Vcb
, FileRecord
, OnlyResident
, Attribute
);
996 Context
->OnlyResident
= OnlyResident
;
997 Context
->FirstAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
998 Context
->CurrAttr
= Context
->FirstAttr
;
999 Context
->LastAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->BytesInUse
);
1000 Context
->NonResidentStart
= NULL
;
1001 Context
->NonResidentEnd
= NULL
;
1002 Context
->Offset
= FileRecord
->AttributeOffset
;
1004 if (Context
->FirstAttr
->Type
== AttributeEnd
)
1006 Context
->CurrAttr
= (PVOID
)-1;
1007 return STATUS_END_OF_FILE
;
1009 else if (Context
->FirstAttr
->Type
== AttributeAttributeList
)
1011 Status
= InternalReadNonResidentAttributes(Context
);
1012 if (!NT_SUCCESS(Status
))
1017 *Attribute
= InternalGetNextAttribute(Context
);
1018 if (*Attribute
== NULL
)
1020 return STATUS_END_OF_FILE
;
1025 *Attribute
= Context
->CurrAttr
;
1026 Context
->Offset
= (UCHAR
*)Context
->CurrAttr
- (UCHAR
*)FileRecord
;
1029 return STATUS_SUCCESS
;
1033 FindNextAttribute(PFIND_ATTR_CONTXT Context
,
1034 PNTFS_ATTR_RECORD
* Attribute
)
1038 DPRINT("FindNextAttribute(%p, %p)\n", Context
, Attribute
);
1040 *Attribute
= InternalGetNextAttribute(Context
);
1041 if (*Attribute
== NULL
)
1043 return STATUS_END_OF_FILE
;
1046 if (Context
->CurrAttr
->Type
!= AttributeAttributeList
)
1048 return STATUS_SUCCESS
;
1051 Status
= InternalReadNonResidentAttributes(Context
);
1052 if (!NT_SUCCESS(Status
))
1057 *Attribute
= InternalGetNextAttribute(Context
);
1058 if (*Attribute
== NULL
)
1060 return STATUS_END_OF_FILE
;
1063 return STATUS_SUCCESS
;
1067 FindCloseAttribute(PFIND_ATTR_CONTXT Context
)
1069 if (Context
->NonResidentStart
!= NULL
)
1071 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
1072 Context
->NonResidentStart
= NULL
;
1078 NtfsDumpFileNameAttribute(PNTFS_ATTR_RECORD Attribute
)
1080 PFILENAME_ATTRIBUTE FileNameAttr
;
1082 DbgPrint(" $FILE_NAME ");
1084 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
1086 FileNameAttr
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1087 DbgPrint(" (%x) '%.*S' ", FileNameAttr
->NameType
, FileNameAttr
->NameLength
, FileNameAttr
->Name
);
1088 DbgPrint(" '%x' \n", FileNameAttr
->FileAttributes
);
1089 DbgPrint(" AllocatedSize: %I64u\nDataSize: %I64u\n", FileNameAttr
->AllocatedSize
, FileNameAttr
->DataSize
);
1090 DbgPrint(" File reference: 0x%016I64x\n", FileNameAttr
->DirectoryFileReferenceNumber
);
1096 NtfsDumpStandardInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
1098 PSTANDARD_INFORMATION StandardInfoAttr
;
1100 DbgPrint(" $STANDARD_INFORMATION ");
1102 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
1104 StandardInfoAttr
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1105 DbgPrint(" '%x' ", StandardInfoAttr
->FileAttribute
);
1111 NtfsDumpVolumeNameAttribute(PNTFS_ATTR_RECORD Attribute
)
1115 DbgPrint(" $VOLUME_NAME ");
1117 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
1119 VolumeName
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1120 DbgPrint(" '%.*S' ", Attribute
->Resident
.ValueLength
/ sizeof(WCHAR
), VolumeName
);
1126 NtfsDumpVolumeInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
1128 PVOLINFO_ATTRIBUTE VolInfoAttr
;
1130 DbgPrint(" $VOLUME_INFORMATION ");
1132 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
1134 VolInfoAttr
= (PVOLINFO_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1135 DbgPrint(" NTFS Version %u.%u Flags 0x%04hx ",
1136 VolInfoAttr
->MajorVersion
,
1137 VolInfoAttr
->MinorVersion
,
1138 VolInfoAttr
->Flags
);
1144 NtfsDumpIndexRootAttribute(PNTFS_ATTR_RECORD Attribute
)
1146 PINDEX_ROOT_ATTRIBUTE IndexRootAttr
;
1147 ULONG currentOffset
;
1150 IndexRootAttr
= (PINDEX_ROOT_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1152 if (IndexRootAttr
->AttributeType
== AttributeFileName
)
1153 ASSERT(IndexRootAttr
->CollationRule
== COLLATION_FILE_NAME
);
1155 DbgPrint(" $INDEX_ROOT (%u bytes per index record, %u clusters) ", IndexRootAttr
->SizeOfEntry
, IndexRootAttr
->ClustersPerIndexRecord
);
1157 if (IndexRootAttr
->Header
.Flags
== INDEX_ROOT_SMALL
)
1159 DbgPrint(" (small)\n");
1163 ASSERT(IndexRootAttr
->Header
.Flags
== INDEX_ROOT_LARGE
);
1164 DbgPrint(" (large)\n");
1167 DbgPrint(" Offset to first index: 0x%lx\n Total size of index entries: 0x%lx\n Allocated size of node: 0x%lx\n",
1168 IndexRootAttr
->Header
.FirstEntryOffset
,
1169 IndexRootAttr
->Header
.TotalSizeOfEntries
,
1170 IndexRootAttr
->Header
.AllocatedSize
);
1171 currentOffset
= IndexRootAttr
->Header
.FirstEntryOffset
;
1173 // print details of every node in the index
1174 while (currentOffset
< IndexRootAttr
->Header
.TotalSizeOfEntries
)
1176 PINDEX_ENTRY_ATTRIBUTE currentIndexExtry
= (PINDEX_ENTRY_ATTRIBUTE
)((ULONG_PTR
)IndexRootAttr
+ 0x10 + currentOffset
);
1177 DbgPrint(" Index Node Entry %lu", currentNode
++);
1178 if (currentIndexExtry
->Flags
& NTFS_INDEX_ENTRY_NODE
)
1179 DbgPrint(" (Branch)");
1181 DbgPrint(" (Leaf)");
1182 if((currentIndexExtry
->Flags
& NTFS_INDEX_ENTRY_END
))
1184 DbgPrint(" (Dummy Key)");
1186 DbgPrint("\n File Reference: 0x%016I64x\n", currentIndexExtry
->Data
.Directory
.IndexedFile
);
1187 DbgPrint(" Index Entry Length: 0x%x\n", currentIndexExtry
->Length
);
1188 DbgPrint(" Index Key Length: 0x%x\n", currentIndexExtry
->KeyLength
);
1190 // if this isn't the final (dummy) node, print info about the key (Filename attribute)
1191 if (!(currentIndexExtry
->Flags
& NTFS_INDEX_ENTRY_END
))
1193 UNICODE_STRING Name
;
1194 DbgPrint(" Parent File Reference: 0x%016I64x\n", currentIndexExtry
->FileName
.DirectoryFileReferenceNumber
);
1195 DbgPrint(" $FILENAME indexed: ");
1196 Name
.Length
= currentIndexExtry
->FileName
.NameLength
* sizeof(WCHAR
);
1197 Name
.MaximumLength
= Name
.Length
;
1198 Name
.Buffer
= currentIndexExtry
->FileName
.Name
;
1199 DbgPrint("'%wZ'\n", &Name
);
1202 // if this node has a sub-node beneath it
1203 if (currentIndexExtry
->Flags
& NTFS_INDEX_ENTRY_NODE
)
1205 // Print the VCN of the sub-node
1206 PULONGLONG SubNodeVCN
= (PULONGLONG
)((ULONG_PTR
)currentIndexExtry
+ currentIndexExtry
->Length
- 8);
1207 DbgPrint(" VCN of sub-node: 0x%llx\n", *SubNodeVCN
);
1210 currentOffset
+= currentIndexExtry
->Length
;
1211 ASSERT(currentIndexExtry
->Length
);
1219 NtfsDumpAttribute(PDEVICE_EXTENSION Vcb
,
1220 PNTFS_ATTR_RECORD Attribute
)
1222 UNICODE_STRING Name
;
1225 ULONGLONG runcount
= 0;
1227 switch (Attribute
->Type
)
1229 case AttributeFileName
:
1230 NtfsDumpFileNameAttribute(Attribute
);
1233 case AttributeStandardInformation
:
1234 NtfsDumpStandardInformationAttribute(Attribute
);
1237 case AttributeObjectId
:
1238 DbgPrint(" $OBJECT_ID ");
1241 case AttributeSecurityDescriptor
:
1242 DbgPrint(" $SECURITY_DESCRIPTOR ");
1245 case AttributeVolumeName
:
1246 NtfsDumpVolumeNameAttribute(Attribute
);
1249 case AttributeVolumeInformation
:
1250 NtfsDumpVolumeInformationAttribute(Attribute
);
1254 DbgPrint(" $DATA ");
1255 //DataBuf = ExAllocatePool(NonPagedPool,AttributeLengthAllocated(Attribute));
1258 case AttributeIndexRoot
:
1259 NtfsDumpIndexRootAttribute(Attribute
);
1262 case AttributeIndexAllocation
:
1263 DbgPrint(" $INDEX_ALLOCATION ");
1266 case AttributeBitmap
:
1267 DbgPrint(" $BITMAP ");
1270 case AttributeReparsePoint
:
1271 DbgPrint(" $REPARSE_POINT ");
1274 case AttributeEAInformation
:
1275 DbgPrint(" $EA_INFORMATION ");
1282 case AttributePropertySet
:
1283 DbgPrint(" $PROPERTY_SET ");
1286 case AttributeLoggedUtilityStream
:
1287 DbgPrint(" $LOGGED_UTILITY_STREAM ");
1291 DbgPrint(" Attribute %lx ",
1296 if (Attribute
->Type
!= AttributeAttributeList
)
1298 if (Attribute
->NameLength
!= 0)
1300 Name
.Length
= Attribute
->NameLength
* sizeof(WCHAR
);
1301 Name
.MaximumLength
= Name
.Length
;
1302 Name
.Buffer
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->NameOffset
);
1304 DbgPrint("'%wZ' ", &Name
);
1308 Attribute
->IsNonResident
? "non-resident" : "resident");
1310 if (Attribute
->IsNonResident
)
1312 FindRun(Attribute
,0,&lcn
, &runcount
);
1314 DbgPrint(" AllocatedSize %I64u DataSize %I64u InitilizedSize %I64u\n",
1315 Attribute
->NonResident
.AllocatedSize
, Attribute
->NonResident
.DataSize
, Attribute
->NonResident
.InitializedSize
);
1316 DbgPrint(" logical clusters: %I64u - %I64u\n",
1317 lcn
, lcn
+ runcount
- 1);
1320 DbgPrint(" %u bytes of data\n", Attribute
->Resident
.ValueLength
);
1325 VOID
NtfsDumpDataRunData(PUCHAR DataRun
)
1327 UCHAR DataRunOffsetSize
;
1328 UCHAR DataRunLengthSize
;
1331 DbgPrint("%02x ", *DataRun
);
1336 DataRunOffsetSize
= (*DataRun
>> 4) & 0xF;
1337 DataRunLengthSize
= *DataRun
& 0xF;
1340 for (i
= 0; i
< DataRunLengthSize
; i
++)
1342 DbgPrint("%02x ", *DataRun
);
1346 for (i
= 0; i
< DataRunOffsetSize
; i
++)
1348 DbgPrint("%02x ", *DataRun
);
1352 NtfsDumpDataRunData(DataRun
);
1357 NtfsDumpDataRuns(PVOID StartOfRun
,
1358 ULONGLONG CurrentLCN
)
1360 PUCHAR DataRun
= StartOfRun
;
1361 LONGLONG DataRunOffset
;
1362 ULONGLONG DataRunLength
;
1364 if (CurrentLCN
== 0)
1366 DPRINT1("Dumping data runs.\n\tData:\n\t\t");
1367 NtfsDumpDataRunData(StartOfRun
);
1368 DbgPrint("\n\tRuns:\n\t\tOff\t\tLCN\t\tLength\n");
1371 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1373 if (DataRunOffset
!= -1)
1374 CurrentLCN
+= DataRunOffset
;
1376 DbgPrint("\t\t%I64d\t", DataRunOffset
);
1377 if (DataRunOffset
< 99999)
1379 DbgPrint("%I64u\t", CurrentLCN
);
1380 if (CurrentLCN
< 99999)
1382 DbgPrint("%I64u\n", DataRunLength
);
1385 DbgPrint("\t\t00\n");
1387 NtfsDumpDataRuns(DataRun
, CurrentLCN
);
1392 NtfsDumpFileAttributes(PDEVICE_EXTENSION Vcb
,
1393 PFILE_RECORD_HEADER FileRecord
)
1396 FIND_ATTR_CONTXT Context
;
1397 PNTFS_ATTR_RECORD Attribute
;
1399 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
1400 while (NT_SUCCESS(Status
))
1402 NtfsDumpAttribute(Vcb
, Attribute
);
1404 Status
= FindNextAttribute(&Context
, &Attribute
);
1407 FindCloseAttribute(&Context
);
1411 GetFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
1412 PFILE_RECORD_HEADER FileRecord
,
1415 FIND_ATTR_CONTXT Context
;
1416 PNTFS_ATTR_RECORD Attribute
;
1417 PFILENAME_ATTRIBUTE Name
;
1420 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
1421 while (NT_SUCCESS(Status
))
1423 if (Attribute
->Type
== AttributeFileName
)
1425 Name
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1426 if (Name
->NameType
== NameType
||
1427 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_WIN32
) ||
1428 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_DOS
))
1430 FindCloseAttribute(&Context
);
1435 Status
= FindNextAttribute(&Context
, &Attribute
);
1438 FindCloseAttribute(&Context
);
1443 * GetPackedByteCount
1444 * Returns the minimum number of bytes needed to represent the value of a
1445 * 64-bit number. Used to encode data runs.
1448 GetPackedByteCount(LONGLONG NumberToPack
,
1454 if (NumberToPack
>= 0x0100000000000000)
1456 if (NumberToPack
>= 0x0001000000000000)
1458 if (NumberToPack
>= 0x0000010000000000)
1460 if (NumberToPack
>= 0x0000000100000000)
1462 if (NumberToPack
>= 0x0000000001000000)
1464 if (NumberToPack
>= 0x0000000000010000)
1466 if (NumberToPack
>= 0x0000000000000100)
1471 if (NumberToPack
> 0)
1473 // we have to make sure the number that gets encoded won't be interpreted as negative
1474 if (NumberToPack
>= 0x0080000000000000)
1476 if (NumberToPack
>= 0x0000800000000000)
1478 if (NumberToPack
>= 0x0000008000000000)
1480 if (NumberToPack
>= 0x0000000080000000)
1482 if (NumberToPack
>= 0x0000000000800000)
1484 if (NumberToPack
>= 0x0000000000008000)
1486 if (NumberToPack
>= 0x0000000000000080)
1493 if (NumberToPack
<= 0xff80000000000000)
1495 if (NumberToPack
<= 0xffff800000000000)
1497 if (NumberToPack
<= 0xffffff8000000000)
1499 if (NumberToPack
<= 0xffffffff80000000)
1501 if (NumberToPack
<= 0xffffffffff800000)
1503 if (NumberToPack
<= 0xffffffffffff8000)
1505 if (NumberToPack
<= 0xffffffffffffff80)
1513 GetLastClusterInDataRun(PDEVICE_EXTENSION Vcb
, PNTFS_ATTR_RECORD Attribute
, PULONGLONG LastCluster
)
1515 LONGLONG DataRunOffset
;
1516 ULONGLONG DataRunLength
;
1517 LONGLONG DataRunStartLCN
;
1519 ULONGLONG LastLCN
= 0;
1520 PUCHAR DataRun
= (PUCHAR
)Attribute
+ Attribute
->NonResident
.MappingPairsOffset
;
1522 if (!Attribute
->IsNonResident
)
1523 return STATUS_INVALID_PARAMETER
;
1527 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1529 if (DataRunOffset
!= -1)
1532 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
1533 LastLCN
= DataRunStartLCN
;
1534 *LastCluster
= LastLCN
+ DataRunLength
- 1;
1541 return STATUS_SUCCESS
;
1544 PSTANDARD_INFORMATION
1545 GetStandardInformationFromRecord(PDEVICE_EXTENSION Vcb
,
1546 PFILE_RECORD_HEADER FileRecord
)
1549 FIND_ATTR_CONTXT Context
;
1550 PNTFS_ATTR_RECORD Attribute
;
1551 PSTANDARD_INFORMATION StdInfo
;
1553 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
1554 while (NT_SUCCESS(Status
))
1556 if (Attribute
->Type
== AttributeStandardInformation
)
1558 StdInfo
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1559 FindCloseAttribute(&Context
);
1563 Status
= FindNextAttribute(&Context
, &Attribute
);
1566 FindCloseAttribute(&Context
);
1570 ULONG
GetFileNameAttributeLength(PFILENAME_ATTRIBUTE FileNameAttribute
)
1572 ULONG Length
= FIELD_OFFSET(FILENAME_ATTRIBUTE
, Name
) + (FileNameAttribute
->NameLength
* sizeof(WCHAR
));
1577 GetBestFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
1578 PFILE_RECORD_HEADER FileRecord
)
1580 PFILENAME_ATTRIBUTE FileName
;
1582 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_POSIX
);
1583 if (FileName
== NULL
)
1585 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_WIN32
);
1586 if (FileName
== NULL
)
1588 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_DOS
);