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.
116 * STATUS_SUCCESS on success. STATUS_NOT_IMPLEMENTED if target address isn't at the end
117 * of the given file record.
120 * Only adding the attribute to the end of the file record is supported; AttributeAddress must
121 * be of type AttributeEnd.
122 * As it's implemented, this function is only intended to assist in creating new file records. It
123 * could be made more general-purpose by considering file records with an $ATTRIBUTE_LIST.
124 * It's the caller's responsibility to ensure the given file record has enough memory allocated
128 AddFileName(PFILE_RECORD_HEADER FileRecord
,
129 PNTFS_ATTR_RECORD AttributeAddress
,
130 PDEVICE_EXTENSION DeviceExt
,
131 PFILE_OBJECT FileObject
)
133 ULONG ResidentHeaderLength
= FIELD_OFFSET(NTFS_ATTR_RECORD
, Resident
.Reserved
) + sizeof(UCHAR
);
134 PFILENAME_ATTRIBUTE FileNameAttribute
;
135 LARGE_INTEGER SystemTime
;
136 ULONG FileRecordEnd
= AttributeAddress
->Length
;
137 ULONGLONG CurrentMFTIndex
= NTFS_FILE_ROOT
;
138 UNICODE_STRING Current
, Remaining
;
139 NTSTATUS Status
= STATUS_SUCCESS
;
140 ULONG FirstEntry
= 0;
142 if (AttributeAddress
->Type
!= AttributeEnd
)
144 DPRINT1("FIXME: Can only add $FILE_NAME attribute to the end of a file record.\n");
145 return STATUS_NOT_IMPLEMENTED
;
148 AttributeAddress
->Type
= AttributeFileName
;
149 AttributeAddress
->Instance
= FileRecord
->NextAttributeNumber
++;
151 FileNameAttribute
= (PFILENAME_ATTRIBUTE
)((LONG_PTR
)AttributeAddress
+ ResidentHeaderLength
);
154 KeQuerySystemTime(&SystemTime
);
155 FileNameAttribute
->CreationTime
= SystemTime
.QuadPart
;
156 FileNameAttribute
->ChangeTime
= SystemTime
.QuadPart
;
157 FileNameAttribute
->LastWriteTime
= SystemTime
.QuadPart
;
158 FileNameAttribute
->LastAccessTime
= SystemTime
.QuadPart
;
160 FileNameAttribute
->FileAttributes
= NTFS_FILE_TYPE_ARCHIVE
;
162 // we need to extract the filename from the path
163 DPRINT1("Pathname: %wZ\n", &FileObject
->FileName
);
165 FsRtlDissectName(FileObject
->FileName
, &Current
, &Remaining
);
167 while (Current
.Length
!= 0)
169 DPRINT1("Current: %wZ\n", &Current
);
171 Status
= NtfsFindMftRecord(DeviceExt
, CurrentMFTIndex
, &Current
, &FirstEntry
, FALSE
, &CurrentMFTIndex
);
172 if (!NT_SUCCESS(Status
))
177 if (Remaining
.Length
== 0)
180 FsRtlDissectName(Current
, &Current
, &Remaining
);
183 DPRINT1("MFT Index of parent: %I64u\n", CurrentMFTIndex
);
185 // set reference to parent directory
186 FileNameAttribute
->DirectoryFileReferenceNumber
= CurrentMFTIndex
;
188 // The highest 2 bytes should be the sequence number, unless the parent happens to be root
189 if (CurrentMFTIndex
== NTFS_FILE_ROOT
)
190 FileNameAttribute
->DirectoryFileReferenceNumber
|= (ULONGLONG
)NTFS_FILE_ROOT
<< 48;
192 FileNameAttribute
->DirectoryFileReferenceNumber
|= (ULONGLONG
)FileRecord
->SequenceNumber
<< 48;
194 DPRINT1("FileNameAttribute->DirectoryFileReferenceNumber: 0x%I64x\n", FileNameAttribute
->DirectoryFileReferenceNumber
);
196 FileNameAttribute
->NameLength
= Current
.Length
/ 2;
197 // TODO: Get proper nametype, add DOS links as needed
198 FileNameAttribute
->NameType
= NTFS_FILE_NAME_WIN32_AND_DOS
;
199 RtlCopyMemory(FileNameAttribute
->Name
, Current
.Buffer
, Current
.Length
);
200 FileRecord
->LinkCount
++;
202 AttributeAddress
->Length
= ResidentHeaderLength
+
203 FIELD_OFFSET(FILENAME_ATTRIBUTE
, Name
) + Current
.Length
;
204 AttributeAddress
->Length
= ALIGN_UP_BY(AttributeAddress
->Length
, 8);
206 AttributeAddress
->Resident
.ValueLength
= FIELD_OFFSET(FILENAME_ATTRIBUTE
, Name
) + Current
.Length
;
207 AttributeAddress
->Resident
.ValueOffset
= ResidentHeaderLength
;
208 AttributeAddress
->Resident
.Flags
= 1; // indexed
210 // move the attribute-end and file-record-end markers to the end of the file record
211 AttributeAddress
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)AttributeAddress
+ AttributeAddress
->Length
);
212 SetFileRecordEnd(FileRecord
, AttributeAddress
, FileRecordEnd
);
221 * Adds a run of allocated clusters to a non-resident attribute.
224 * Pointer to an NTFS_VCB for the destination volume.
227 * Pointer to an NTFS_ATTR_CONTEXT describing the destination attribute.
230 * Byte offset of the destination attribute relative to its file record.
233 * Pointer to a complete copy of the file record containing the destination attribute. Must be at least
234 * Vcb->NtfsInfo.BytesPerFileRecord bytes long.
236 * @param NextAssignedCluster
237 * Logical cluster number of the start of the data run being added.
240 * How many clusters are in the data run being added. Can't be 0.
243 * STATUS_SUCCESS on success. STATUS_INVALID_PARAMETER if AttrContext describes a resident attribute.
244 * STATUS_INSUFFICIENT_RESOURCES if ConvertDataRunsToLargeMCB() fails or if we fail to allocate a
245 * buffer for the new data runs.
246 * STATUS_INSUFFICIENT_RESOURCES or STATUS_UNSUCCESSFUL if FsRtlAddLargeMcbEntry() fails.
247 * STATUS_BUFFER_TOO_SMALL if ConvertLargeMCBToDataRuns() fails.
248 * STATUS_NOT_IMPLEMENTED if we need to migrate the attribute to an attribute list (TODO).
251 * Clusters should have been allocated previously with NtfsAllocateClusters().
256 AddRun(PNTFS_VCB Vcb
,
257 PNTFS_ATTR_CONTEXT AttrContext
,
259 PFILE_RECORD_HEADER FileRecord
,
260 ULONGLONG NextAssignedCluster
,
264 int DataRunMaxLength
;
265 PNTFS_ATTR_RECORD DestinationAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ AttrOffset
);
266 ULONG NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.Length
;
267 ULONGLONG NextVBN
= 0;
272 if (!AttrContext
->Record
.IsNonResident
)
273 return STATUS_INVALID_PARAMETER
;
275 if (AttrContext
->Record
.NonResident
.AllocatedSize
!= 0)
276 NextVBN
= AttrContext
->Record
.NonResident
.HighestVCN
+ 1;
278 // Add newly-assigned clusters to mcb
280 if (!FsRtlAddLargeMcbEntry(&AttrContext
->DataRunsMCB
,
285 ExRaiseStatus(STATUS_UNSUCCESSFUL
);
287 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
) {
288 _SEH2_YIELD(_SEH2_GetExceptionCode());
291 RunBuffer
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
294 DPRINT1("ERROR: Couldn't allocate memory for data runs!\n");
295 return STATUS_INSUFFICIENT_RESOURCES
;
298 // Convert the map control block back to encoded data runs
299 ConvertLargeMCBToDataRuns(&AttrContext
->DataRunsMCB
, RunBuffer
, Vcb
->NtfsInfo
.BytesPerCluster
, &RunBufferSize
);
301 // Get the amount of free space between the start of the of the first data run and the attribute end
302 DataRunMaxLength
= AttrContext
->Record
.Length
- AttrContext
->Record
.NonResident
.MappingPairsOffset
;
304 // Do we need to extend the attribute (or convert to attribute list)?
305 if (DataRunMaxLength
< RunBufferSize
)
307 PNTFS_ATTR_RECORD NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ NextAttributeOffset
);
308 DataRunMaxLength
+= Vcb
->NtfsInfo
.BytesPerFileRecord
- NextAttributeOffset
- (sizeof(ULONG
) * 2);
310 // Can we move the end of the attribute?
311 if (NextAttribute
->Type
!= AttributeEnd
|| DataRunMaxLength
< RunBufferSize
- 1)
313 DPRINT1("FIXME: Need to create attribute list! Max Data Run Length available: %d\n", DataRunMaxLength
);
314 if (NextAttribute
->Type
!= AttributeEnd
)
315 DPRINT1("There's another attribute after this one with type %0xlx\n", NextAttribute
->Type
);
316 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
317 return STATUS_NOT_IMPLEMENTED
;
320 // calculate position of end markers
321 NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.NonResident
.MappingPairsOffset
+ RunBufferSize
;
322 NextAttributeOffset
= ALIGN_UP_BY(NextAttributeOffset
, 8);
325 DestinationAttribute
->Length
= NextAttributeOffset
- AttrOffset
;
326 AttrContext
->Record
.Length
= DestinationAttribute
->Length
;
328 // End the file record
329 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ NextAttributeOffset
);
330 SetFileRecordEnd(FileRecord
, NextAttribute
, FILE_RECORD_END
);
334 DestinationAttribute
->NonResident
.HighestVCN
=
335 AttrContext
->Record
.NonResident
.HighestVCN
= max(NextVBN
- 1 + RunLength
,
336 AttrContext
->Record
.NonResident
.HighestVCN
);
338 // Write data runs to destination attribute
339 RtlCopyMemory((PVOID
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
),
343 // Update the file record
344 Status
= UpdateFileRecord(Vcb
, AttrContext
->FileMFTIndex
, FileRecord
);
346 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
348 NtfsDumpDataRuns((PUCHAR
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
), 0);
354 * @name AddStandardInformation
357 * Adds a $STANDARD_INFORMATION attribute to a given FileRecord.
360 * Pointer to a complete file record to add the attribute to. Caller is responsible for
361 * ensuring FileRecord is large enough to contain $STANDARD_INFORMATION.
363 * @param AttributeAddress
364 * Pointer to the region of memory that will receive the $STANDARD_INFORMATION attribute.
365 * This address must reside within FileRecord. Must be aligned to an 8-byte boundary (relative to FileRecord).
368 * STATUS_SUCCESS on success. STATUS_NOT_IMPLEMENTED if target address isn't at the end
369 * of the given file record.
372 * Only adding the attribute to the end of the file record is supported; AttributeAddress must
373 * be of type AttributeEnd.
374 * As it's implemented, this function is only intended to assist in creating new file records. It
375 * could be made more general-purpose by considering file records with an $ATTRIBUTE_LIST.
376 * It's the caller's responsibility to ensure the given file record has enough memory allocated
380 AddStandardInformation(PFILE_RECORD_HEADER FileRecord
,
381 PNTFS_ATTR_RECORD AttributeAddress
)
383 ULONG ResidentHeaderLength
= FIELD_OFFSET(NTFS_ATTR_RECORD
, Resident
.Reserved
) + sizeof(UCHAR
);
384 PSTANDARD_INFORMATION StandardInfo
= (PSTANDARD_INFORMATION
)((LONG_PTR
)AttributeAddress
+ ResidentHeaderLength
);
385 LARGE_INTEGER SystemTime
;
386 ULONG FileRecordEnd
= AttributeAddress
->Length
;
388 if (AttributeAddress
->Type
!= AttributeEnd
)
390 DPRINT1("FIXME: Can only add $STANDARD_INFORMATION attribute to the end of a file record.\n");
391 return STATUS_NOT_IMPLEMENTED
;
394 AttributeAddress
->Type
= AttributeStandardInformation
;
395 AttributeAddress
->Length
= sizeof(STANDARD_INFORMATION
) + ResidentHeaderLength
;
396 AttributeAddress
->Length
= ALIGN_UP_BY(AttributeAddress
->Length
, 8);
397 AttributeAddress
->Resident
.ValueLength
= sizeof(STANDARD_INFORMATION
);
398 AttributeAddress
->Resident
.ValueOffset
= ResidentHeaderLength
;
399 AttributeAddress
->Instance
= FileRecord
->NextAttributeNumber
++;
401 // set dates and times
402 KeQuerySystemTime(&SystemTime
);
403 StandardInfo
->CreationTime
= SystemTime
.QuadPart
;
404 StandardInfo
->ChangeTime
= SystemTime
.QuadPart
;
405 StandardInfo
->LastWriteTime
= SystemTime
.QuadPart
;
406 StandardInfo
->LastAccessTime
= SystemTime
.QuadPart
;
407 StandardInfo
->FileAttribute
= NTFS_FILE_TYPE_ARCHIVE
;
409 // move the attribute-end and file-record-end markers to the end of the file record
410 AttributeAddress
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)AttributeAddress
+ AttributeAddress
->Length
);
411 SetFileRecordEnd(FileRecord
, AttributeAddress
, FileRecordEnd
);
413 return STATUS_SUCCESS
;
417 * @name ConvertDataRunsToLargeMCB
420 * Converts binary data runs to a map control block.
423 * Pointer to the run data
426 * Pointer to an unitialized LARGE_MCB structure.
429 * STATUS_SUCCESS on success, STATUS_INSUFFICIENT_RESOURCES or STATUS_UNSUCCESSFUL if we fail to
430 * initialize the mcb or add an entry.
433 * Initializes the LARGE_MCB pointed to by DataRunsMCB. If this function succeeds, you
434 * need to call FsRtlUninitializeLargeMcb() when you're done with DataRunsMCB. This
435 * function will ensure the LargeMCB has been unitialized in case of failure.
439 ConvertDataRunsToLargeMCB(PUCHAR DataRun
,
440 PLARGE_MCB DataRunsMCB
,
443 LONGLONG DataRunOffset
;
444 ULONGLONG DataRunLength
;
445 LONGLONG DataRunStartLCN
;
446 ULONGLONG LastLCN
= 0;
448 // Initialize the MCB, potentially catch an exception
450 FsRtlInitializeLargeMcb(DataRunsMCB
, NonPagedPool
);
451 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
) {
452 _SEH2_YIELD(return _SEH2_GetExceptionCode());
455 while (*DataRun
!= 0)
457 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
459 if (DataRunOffset
!= -1)
462 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
463 LastLCN
= DataRunStartLCN
;
466 if (!FsRtlAddLargeMcbEntry(DataRunsMCB
,
471 ExRaiseStatus(STATUS_UNSUCCESSFUL
);
473 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
) {
474 FsRtlUninitializeLargeMcb(DataRunsMCB
);
475 _SEH2_YIELD(return _SEH2_GetExceptionCode());
480 *pNextVBN
+= DataRunLength
;
483 return STATUS_SUCCESS
;
487 * @name ConvertLargeMCBToDataRuns
490 * Converts a map control block to a series of encoded data runs (used by non-resident attributes).
493 * Pointer to a LARGE_MCB structure describing the data runs.
496 * Pointer to the buffer that will receive the encoded data runs.
498 * @param MaxBufferSize
499 * Size of RunBuffer, in bytes.
501 * @param UsedBufferSize
502 * Pointer to a ULONG that will receive the size of the data runs in bytes. Can't be NULL.
505 * STATUS_SUCCESS on success, STATUS_BUFFER_TOO_SMALL if RunBuffer is too small to contain the
510 ConvertLargeMCBToDataRuns(PLARGE_MCB DataRunsMCB
,
513 PULONG UsedBufferSize
)
515 NTSTATUS Status
= STATUS_SUCCESS
;
516 ULONG RunBufferOffset
= 0;
517 LONGLONG DataRunOffset
;
518 ULONGLONG LastLCN
= 0;
519 LONGLONG Vbn
, Lbn
, Count
;
523 DPRINT("\t[Vbn, Lbn, Count]\n");
525 // convert each mcb entry to a data run
526 for (i
= 0; FsRtlGetNextLargeMcbEntry(DataRunsMCB
, i
, &Vbn
, &Lbn
, &Count
); i
++)
528 UCHAR DataRunOffsetSize
= 0;
529 UCHAR DataRunLengthSize
= 0;
530 UCHAR ControlByte
= 0;
533 DPRINT("\t[%I64d, %I64d,%I64d]\n", Vbn
, Lbn
, Count
);
535 // TODO: check for holes and convert to sparse runs
536 DataRunOffset
= Lbn
- LastLCN
;
539 // now we need to determine how to represent DataRunOffset with the minimum number of bytes
540 DPRINT("Determining how many bytes needed to represent %I64x\n", DataRunOffset
);
541 DataRunOffsetSize
= GetPackedByteCount(DataRunOffset
, TRUE
);
542 DPRINT("%d bytes needed.\n", DataRunOffsetSize
);
544 // determine how to represent DataRunLengthSize with the minimum number of bytes
545 DPRINT("Determining how many bytes needed to represent %I64x\n", Count
);
546 DataRunLengthSize
= GetPackedByteCount(Count
, TRUE
);
547 DPRINT("%d bytes needed.\n", DataRunLengthSize
);
549 // ensure the next data run + end marker would be <= Max buffer size
550 if (RunBufferOffset
+ 2 + DataRunLengthSize
+ DataRunOffsetSize
> MaxBufferSize
)
552 Status
= STATUS_BUFFER_TOO_SMALL
;
553 DPRINT1("FIXME: Ran out of room in buffer for data runs!\n");
557 // pack and copy the control byte
558 ControlByte
= (DataRunOffsetSize
<< 4) + DataRunLengthSize
;
559 RunBuffer
[RunBufferOffset
++] = ControlByte
;
561 // copy DataRunLength
562 RtlCopyMemory(RunBuffer
+ RunBufferOffset
, &Count
, DataRunLengthSize
);
563 RunBufferOffset
+= DataRunLengthSize
;
565 // copy DataRunOffset
566 RtlCopyMemory(RunBuffer
+ RunBufferOffset
, &DataRunOffset
, DataRunOffsetSize
);
567 RunBufferOffset
+= DataRunOffsetSize
;
571 RunBuffer
[RunBufferOffset
++] = 0;
573 *UsedBufferSize
= RunBufferOffset
;
574 DPRINT("New Size of DataRuns: %ld\n", *UsedBufferSize
);
580 DecodeRun(PUCHAR DataRun
,
581 LONGLONG
*DataRunOffset
,
582 ULONGLONG
*DataRunLength
)
584 UCHAR DataRunOffsetSize
;
585 UCHAR DataRunLengthSize
;
588 DataRunOffsetSize
= (*DataRun
>> 4) & 0xF;
589 DataRunLengthSize
= *DataRun
& 0xF;
593 for (i
= 0; i
< DataRunLengthSize
; i
++)
595 *DataRunLength
+= ((ULONG64
)*DataRun
) << (i
* 8);
599 /* NTFS 3+ sparse files */
600 if (DataRunOffsetSize
== 0)
606 for (i
= 0; i
< DataRunOffsetSize
- 1; i
++)
608 *DataRunOffset
+= ((ULONG64
)*DataRun
) << (i
* 8);
611 /* The last byte contains sign so we must process it different way. */
612 *DataRunOffset
= ((LONG64
)(CHAR
)(*(DataRun
++)) << (i
* 8)) + *DataRunOffset
;
615 DPRINT("DataRunOffsetSize: %x\n", DataRunOffsetSize
);
616 DPRINT("DataRunLengthSize: %x\n", DataRunLengthSize
);
617 DPRINT("DataRunOffset: %x\n", *DataRunOffset
);
618 DPRINT("DataRunLength: %x\n", *DataRunLength
);
624 FindRun(PNTFS_ATTR_RECORD NresAttr
,
629 if (vcn
< NresAttr
->NonResident
.LowestVCN
|| vcn
> NresAttr
->NonResident
.HighestVCN
)
632 DecodeRun((PUCHAR
)((ULONG_PTR
)NresAttr
+ NresAttr
->NonResident
.MappingPairsOffset
), (PLONGLONG
)lcn
, count
);
641 * Shrinks the allocation size of a non-resident attribute by a given number of clusters.
642 * Frees the clusters from the volume's $BITMAP file as well as the attribute's data runs.
645 * Pointer to an NTFS_VCB for the destination volume.
648 * Pointer to an NTFS_ATTR_CONTEXT describing the attribute from which the clusters will be freed.
651 * Byte offset of the destination attribute relative to its file record.
654 * Pointer to a complete copy of the file record containing the attribute. Must be at least
655 * Vcb->NtfsInfo.BytesPerFileRecord bytes long.
657 * @param ClustersToFree
658 * Number of clusters that should be freed from the end of the data stream. Must be no more
659 * Than the number of clusters assigned to the attribute (HighestVCN + 1).
662 * STATUS_SUCCESS on success. STATUS_INVALID_PARAMETER if AttrContext describes a resident attribute,
663 * or if the caller requested more clusters be freed than the attribute has been allocated.
664 * STATUS_INSUFFICIENT_RESOURCES if allocating a buffer for the data runs fails or
665 * if ConvertDataRunsToLargeMCB() fails.
666 * STATUS_BUFFER_TOO_SMALL if ConvertLargeMCBToDataRuns() fails.
671 FreeClusters(PNTFS_VCB Vcb
,
672 PNTFS_ATTR_CONTEXT AttrContext
,
674 PFILE_RECORD_HEADER FileRecord
,
675 ULONG ClustersToFree
)
677 NTSTATUS Status
= STATUS_SUCCESS
;
678 ULONG ClustersLeftToFree
= ClustersToFree
;
680 PNTFS_ATTR_RECORD DestinationAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ AttrOffset
);
681 ULONG NextAttributeOffset
= AttrOffset
+ AttrContext
->Record
.Length
;
682 PNTFS_ATTR_RECORD NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ NextAttributeOffset
);
685 ULONG RunBufferSize
= 0;
687 PFILE_RECORD_HEADER BitmapRecord
;
688 PNTFS_ATTR_CONTEXT DataContext
;
689 ULONGLONG BitmapDataSize
;
694 if (!AttrContext
->Record
.IsNonResident
)
696 return STATUS_INVALID_PARAMETER
;
699 // Read the $Bitmap file
700 BitmapRecord
= ExAllocatePoolWithTag(NonPagedPool
,
701 Vcb
->NtfsInfo
.BytesPerFileRecord
,
703 if (BitmapRecord
== NULL
)
705 DPRINT1("Error: Unable to allocate memory for bitmap file record!\n");
706 return STATUS_NO_MEMORY
;
709 Status
= ReadFileRecord(Vcb
, NTFS_FILE_BITMAP
, BitmapRecord
);
710 if (!NT_SUCCESS(Status
))
712 DPRINT1("Error: Unable to read file record for bitmap!\n");
713 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
717 Status
= FindAttribute(Vcb
, BitmapRecord
, AttributeData
, L
"", 0, &DataContext
, NULL
);
718 if (!NT_SUCCESS(Status
))
720 DPRINT1("Error: Unable to find data attribute for bitmap file!\n");
721 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
725 BitmapDataSize
= AttributeDataLength(&DataContext
->Record
);
726 BitmapDataSize
= min(BitmapDataSize
, 0xffffffff);
727 ASSERT((BitmapDataSize
* 8) >= Vcb
->NtfsInfo
.ClusterCount
);
728 BitmapData
= ExAllocatePoolWithTag(NonPagedPool
, ROUND_UP(BitmapDataSize
, Vcb
->NtfsInfo
.BytesPerSector
), TAG_NTFS
);
729 if (BitmapData
== NULL
)
731 DPRINT1("Error: Unable to allocate memory for bitmap file data!\n");
732 ReleaseAttributeContext(DataContext
);
733 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
737 ReadAttribute(Vcb
, DataContext
, 0, (PCHAR
)BitmapData
, (ULONG
)BitmapDataSize
);
739 RtlInitializeBitMap(&Bitmap
, (PULONG
)BitmapData
, Vcb
->NtfsInfo
.ClusterCount
);
741 // free clusters in $BITMAP file
742 while (ClustersLeftToFree
> 0)
744 LONGLONG LargeVbn
, LargeLbn
;
746 if (!FsRtlLookupLastLargeMcbEntry(&AttrContext
->DataRunsMCB
, &LargeVbn
, &LargeLbn
))
748 Status
= STATUS_INVALID_PARAMETER
;
749 DPRINT1("DRIVER ERROR: FreeClusters called to free %lu clusters, which is %lu more clusters than are assigned to attribute!",
757 // deallocate this cluster
758 RtlClearBits(&Bitmap
, LargeLbn
, 1);
760 FsRtlTruncateLargeMcb(&AttrContext
->DataRunsMCB
, AttrContext
->Record
.NonResident
.HighestVCN
);
762 // decrement HighestVCN, but don't let it go below 0
763 AttrContext
->Record
.NonResident
.HighestVCN
= min(AttrContext
->Record
.NonResident
.HighestVCN
, AttrContext
->Record
.NonResident
.HighestVCN
- 1);
764 ClustersLeftToFree
--;
767 // update $BITMAP file on disk
768 Status
= WriteAttribute(Vcb
, DataContext
, 0, BitmapData
, (ULONG
)BitmapDataSize
, &LengthWritten
);
769 if (!NT_SUCCESS(Status
))
771 ReleaseAttributeContext(DataContext
);
772 ExFreePoolWithTag(BitmapData
, TAG_NTFS
);
773 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
777 ReleaseAttributeContext(DataContext
);
778 ExFreePoolWithTag(BitmapData
, TAG_NTFS
);
779 ExFreePoolWithTag(BitmapRecord
, TAG_NTFS
);
781 // Save updated data runs to file record
783 // Allocate some memory for a new RunBuffer
784 RunBuffer
= ExAllocatePoolWithTag(NonPagedPool
, Vcb
->NtfsInfo
.BytesPerFileRecord
, TAG_NTFS
);
787 DPRINT1("ERROR: Couldn't allocate memory for data runs!\n");
788 return STATUS_INSUFFICIENT_RESOURCES
;
791 // Convert the map control block back to encoded data runs
792 ConvertLargeMCBToDataRuns(&AttrContext
->DataRunsMCB
, RunBuffer
, Vcb
->NtfsInfo
.BytesPerCluster
, &RunBufferSize
);
795 DestinationAttribute
->NonResident
.HighestVCN
= AttrContext
->Record
.NonResident
.HighestVCN
;
797 // Write data runs to destination attribute
798 RtlCopyMemory((PVOID
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
),
802 // Is DestinationAttribute the last attribute in the file record?
803 if (NextAttribute
->Type
== AttributeEnd
)
805 // update attribute length
806 AttrContext
->Record
.Length
= ALIGN_UP_BY(AttrContext
->Record
.NonResident
.MappingPairsOffset
+ RunBufferSize
, 8);
807 DestinationAttribute
->Length
= AttrContext
->Record
.Length
;
810 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->Length
);
811 SetFileRecordEnd(FileRecord
, NextAttribute
, FILE_RECORD_END
);
814 // Update the file record
815 Status
= UpdateFileRecord(Vcb
, AttrContext
->FileMFTIndex
, FileRecord
);
817 ExFreePoolWithTag(RunBuffer
, TAG_NTFS
);
819 NtfsDumpDataRuns((PUCHAR
)((ULONG_PTR
)DestinationAttribute
+ DestinationAttribute
->NonResident
.MappingPairsOffset
), 0);
826 InternalReadNonResidentAttributes(PFIND_ATTR_CONTXT Context
)
829 PNTFS_ATTR_RECORD Attribute
;
830 PNTFS_ATTR_CONTEXT ListContext
;
832 DPRINT("InternalReadNonResidentAttributes(%p)\n", Context
);
834 Attribute
= Context
->CurrAttr
;
835 ASSERT(Attribute
->Type
== AttributeAttributeList
);
837 if (Context
->OnlyResident
)
839 Context
->NonResidentStart
= NULL
;
840 Context
->NonResidentEnd
= NULL
;
841 return STATUS_SUCCESS
;
844 if (Context
->NonResidentStart
!= NULL
)
846 return STATUS_FILE_CORRUPT_ERROR
;
849 ListContext
= PrepareAttributeContext(Attribute
);
850 ListSize
= AttributeDataLength(&ListContext
->Record
);
851 if (ListSize
> 0xFFFFFFFF)
853 ReleaseAttributeContext(ListContext
);
854 return STATUS_BUFFER_OVERFLOW
;
857 Context
->NonResidentStart
= ExAllocatePoolWithTag(NonPagedPool
, (ULONG
)ListSize
, TAG_NTFS
);
858 if (Context
->NonResidentStart
== NULL
)
860 ReleaseAttributeContext(ListContext
);
861 return STATUS_INSUFFICIENT_RESOURCES
;
864 if (ReadAttribute(Context
->Vcb
, ListContext
, 0, (PCHAR
)Context
->NonResidentStart
, (ULONG
)ListSize
) != ListSize
)
866 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
867 Context
->NonResidentStart
= NULL
;
868 ReleaseAttributeContext(ListContext
);
869 return STATUS_FILE_CORRUPT_ERROR
;
872 ReleaseAttributeContext(ListContext
);
873 Context
->NonResidentEnd
= (PNTFS_ATTR_RECORD
)((PCHAR
)Context
->NonResidentStart
+ ListSize
);
874 return STATUS_SUCCESS
;
879 InternalGetNextAttribute(PFIND_ATTR_CONTXT Context
)
881 PNTFS_ATTR_RECORD NextAttribute
;
883 if (Context
->CurrAttr
== (PVOID
)-1)
888 if (Context
->CurrAttr
>= Context
->FirstAttr
&&
889 Context
->CurrAttr
< Context
->LastAttr
)
891 if (Context
->CurrAttr
->Length
== 0)
893 DPRINT1("Broken length!\n");
894 Context
->CurrAttr
= (PVOID
)-1;
898 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
900 if (NextAttribute
> Context
->LastAttr
|| NextAttribute
< Context
->FirstAttr
)
902 DPRINT1("Broken length: 0x%lx!\n", Context
->CurrAttr
->Length
);
903 Context
->CurrAttr
= (PVOID
)-1;
907 Context
->Offset
+= ((ULONG_PTR
)NextAttribute
- (ULONG_PTR
)Context
->CurrAttr
);
908 Context
->CurrAttr
= NextAttribute
;
910 if (Context
->CurrAttr
< Context
->LastAttr
&&
911 Context
->CurrAttr
->Type
!= AttributeEnd
)
913 return Context
->CurrAttr
;
917 if (Context
->NonResidentStart
== NULL
)
919 Context
->CurrAttr
= (PVOID
)-1;
923 if (Context
->CurrAttr
< Context
->NonResidentStart
||
924 Context
->CurrAttr
>= Context
->NonResidentEnd
)
926 Context
->CurrAttr
= Context
->NonResidentStart
;
928 else if (Context
->CurrAttr
->Length
!= 0)
930 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
931 Context
->Offset
+= ((ULONG_PTR
)NextAttribute
- (ULONG_PTR
)Context
->CurrAttr
);
932 Context
->CurrAttr
= NextAttribute
;
936 DPRINT1("Broken length!\n");
937 Context
->CurrAttr
= (PVOID
)-1;
941 if (Context
->CurrAttr
< Context
->NonResidentEnd
&&
942 Context
->CurrAttr
->Type
!= AttributeEnd
)
944 return Context
->CurrAttr
;
947 Context
->CurrAttr
= (PVOID
)-1;
952 FindFirstAttribute(PFIND_ATTR_CONTXT Context
,
953 PDEVICE_EXTENSION Vcb
,
954 PFILE_RECORD_HEADER FileRecord
,
955 BOOLEAN OnlyResident
,
956 PNTFS_ATTR_RECORD
* Attribute
)
960 DPRINT("FindFistAttribute(%p, %p, %p, %p, %u, %p)\n", Context
, Vcb
, FileRecord
, OnlyResident
, Attribute
);
963 Context
->OnlyResident
= OnlyResident
;
964 Context
->FirstAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
965 Context
->CurrAttr
= Context
->FirstAttr
;
966 Context
->LastAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->BytesInUse
);
967 Context
->NonResidentStart
= NULL
;
968 Context
->NonResidentEnd
= NULL
;
969 Context
->Offset
= FileRecord
->AttributeOffset
;
971 if (Context
->FirstAttr
->Type
== AttributeEnd
)
973 Context
->CurrAttr
= (PVOID
)-1;
974 return STATUS_END_OF_FILE
;
976 else if (Context
->FirstAttr
->Type
== AttributeAttributeList
)
978 Status
= InternalReadNonResidentAttributes(Context
);
979 if (!NT_SUCCESS(Status
))
984 *Attribute
= InternalGetNextAttribute(Context
);
985 if (*Attribute
== NULL
)
987 return STATUS_END_OF_FILE
;
992 *Attribute
= Context
->CurrAttr
;
993 Context
->Offset
= (UCHAR
*)Context
->CurrAttr
- (UCHAR
*)FileRecord
;
996 return STATUS_SUCCESS
;
1000 FindNextAttribute(PFIND_ATTR_CONTXT Context
,
1001 PNTFS_ATTR_RECORD
* Attribute
)
1005 DPRINT("FindNextAttribute(%p, %p)\n", Context
, Attribute
);
1007 *Attribute
= InternalGetNextAttribute(Context
);
1008 if (*Attribute
== NULL
)
1010 return STATUS_END_OF_FILE
;
1013 if (Context
->CurrAttr
->Type
!= AttributeAttributeList
)
1015 return STATUS_SUCCESS
;
1018 Status
= InternalReadNonResidentAttributes(Context
);
1019 if (!NT_SUCCESS(Status
))
1024 *Attribute
= InternalGetNextAttribute(Context
);
1025 if (*Attribute
== NULL
)
1027 return STATUS_END_OF_FILE
;
1030 return STATUS_SUCCESS
;
1034 FindCloseAttribute(PFIND_ATTR_CONTXT Context
)
1036 if (Context
->NonResidentStart
!= NULL
)
1038 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
1039 Context
->NonResidentStart
= NULL
;
1045 NtfsDumpFileNameAttribute(PNTFS_ATTR_RECORD Attribute
)
1047 PFILENAME_ATTRIBUTE FileNameAttr
;
1049 DbgPrint(" $FILE_NAME ");
1051 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
1053 FileNameAttr
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1054 DbgPrint(" (%x) '%.*S' ", FileNameAttr
->NameType
, FileNameAttr
->NameLength
, FileNameAttr
->Name
);
1055 DbgPrint(" '%x' \n", FileNameAttr
->FileAttributes
);
1056 DbgPrint(" AllocatedSize: %I64u\nDataSize: %I64u\n", FileNameAttr
->AllocatedSize
, FileNameAttr
->DataSize
);
1062 NtfsDumpStandardInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
1064 PSTANDARD_INFORMATION StandardInfoAttr
;
1066 DbgPrint(" $STANDARD_INFORMATION ");
1068 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
1070 StandardInfoAttr
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1071 DbgPrint(" '%x' ", StandardInfoAttr
->FileAttribute
);
1077 NtfsDumpVolumeNameAttribute(PNTFS_ATTR_RECORD Attribute
)
1081 DbgPrint(" $VOLUME_NAME ");
1083 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
1085 VolumeName
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1086 DbgPrint(" '%.*S' ", Attribute
->Resident
.ValueLength
/ sizeof(WCHAR
), VolumeName
);
1092 NtfsDumpVolumeInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
1094 PVOLINFO_ATTRIBUTE VolInfoAttr
;
1096 DbgPrint(" $VOLUME_INFORMATION ");
1098 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
1100 VolInfoAttr
= (PVOLINFO_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1101 DbgPrint(" NTFS Version %u.%u Flags 0x%04hx ",
1102 VolInfoAttr
->MajorVersion
,
1103 VolInfoAttr
->MinorVersion
,
1104 VolInfoAttr
->Flags
);
1110 NtfsDumpIndexRootAttribute(PNTFS_ATTR_RECORD Attribute
)
1112 PINDEX_ROOT_ATTRIBUTE IndexRootAttr
;
1114 IndexRootAttr
= (PINDEX_ROOT_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1116 if (IndexRootAttr
->AttributeType
== AttributeFileName
)
1117 ASSERT(IndexRootAttr
->CollationRule
== COLLATION_FILE_NAME
);
1119 DbgPrint(" $INDEX_ROOT (%uB, %u) ", IndexRootAttr
->SizeOfEntry
, IndexRootAttr
->ClustersPerIndexRecord
);
1121 if (IndexRootAttr
->Header
.Flags
== INDEX_ROOT_SMALL
)
1123 DbgPrint(" (small) ");
1127 ASSERT(IndexRootAttr
->Header
.Flags
== INDEX_ROOT_LARGE
);
1128 DbgPrint(" (large) ");
1135 NtfsDumpAttribute(PDEVICE_EXTENSION Vcb
,
1136 PNTFS_ATTR_RECORD Attribute
)
1138 UNICODE_STRING Name
;
1141 ULONGLONG runcount
= 0;
1143 switch (Attribute
->Type
)
1145 case AttributeFileName
:
1146 NtfsDumpFileNameAttribute(Attribute
);
1149 case AttributeStandardInformation
:
1150 NtfsDumpStandardInformationAttribute(Attribute
);
1153 case AttributeObjectId
:
1154 DbgPrint(" $OBJECT_ID ");
1157 case AttributeSecurityDescriptor
:
1158 DbgPrint(" $SECURITY_DESCRIPTOR ");
1161 case AttributeVolumeName
:
1162 NtfsDumpVolumeNameAttribute(Attribute
);
1165 case AttributeVolumeInformation
:
1166 NtfsDumpVolumeInformationAttribute(Attribute
);
1170 DbgPrint(" $DATA ");
1171 //DataBuf = ExAllocatePool(NonPagedPool,AttributeLengthAllocated(Attribute));
1174 case AttributeIndexRoot
:
1175 NtfsDumpIndexRootAttribute(Attribute
);
1178 case AttributeIndexAllocation
:
1179 DbgPrint(" $INDEX_ALLOCATION ");
1182 case AttributeBitmap
:
1183 DbgPrint(" $BITMAP ");
1186 case AttributeReparsePoint
:
1187 DbgPrint(" $REPARSE_POINT ");
1190 case AttributeEAInformation
:
1191 DbgPrint(" $EA_INFORMATION ");
1198 case AttributePropertySet
:
1199 DbgPrint(" $PROPERTY_SET ");
1202 case AttributeLoggedUtilityStream
:
1203 DbgPrint(" $LOGGED_UTILITY_STREAM ");
1207 DbgPrint(" Attribute %lx ",
1212 if (Attribute
->Type
!= AttributeAttributeList
)
1214 if (Attribute
->NameLength
!= 0)
1216 Name
.Length
= Attribute
->NameLength
* sizeof(WCHAR
);
1217 Name
.MaximumLength
= Name
.Length
;
1218 Name
.Buffer
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->NameOffset
);
1220 DbgPrint("'%wZ' ", &Name
);
1224 Attribute
->IsNonResident
? "non-resident" : "resident");
1226 if (Attribute
->IsNonResident
)
1228 FindRun(Attribute
,0,&lcn
, &runcount
);
1230 DbgPrint(" AllocatedSize %I64u DataSize %I64u InitilizedSize %I64u\n",
1231 Attribute
->NonResident
.AllocatedSize
, Attribute
->NonResident
.DataSize
, Attribute
->NonResident
.InitializedSize
);
1232 DbgPrint(" logical clusters: %I64u - %I64u\n",
1233 lcn
, lcn
+ runcount
- 1);
1236 DbgPrint(" %u bytes of data\n", Attribute
->Resident
.ValueLength
);
1241 VOID
NtfsDumpDataRunData(PUCHAR DataRun
)
1243 UCHAR DataRunOffsetSize
;
1244 UCHAR DataRunLengthSize
;
1247 DbgPrint("%02x ", *DataRun
);
1252 DataRunOffsetSize
= (*DataRun
>> 4) & 0xF;
1253 DataRunLengthSize
= *DataRun
& 0xF;
1256 for (i
= 0; i
< DataRunLengthSize
; i
++)
1258 DbgPrint("%02x ", *DataRun
);
1262 for (i
= 0; i
< DataRunOffsetSize
; i
++)
1264 DbgPrint("%02x ", *DataRun
);
1268 NtfsDumpDataRunData(DataRun
);
1273 NtfsDumpDataRuns(PVOID StartOfRun
,
1274 ULONGLONG CurrentLCN
)
1276 PUCHAR DataRun
= StartOfRun
;
1277 LONGLONG DataRunOffset
;
1278 ULONGLONG DataRunLength
;
1280 if (CurrentLCN
== 0)
1282 DPRINT1("Dumping data runs.\n\tData:\n\t\t");
1283 NtfsDumpDataRunData(StartOfRun
);
1284 DbgPrint("\n\tRuns:\n\t\tOff\t\tLCN\t\tLength\n");
1287 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1289 if (DataRunOffset
!= -1)
1290 CurrentLCN
+= DataRunOffset
;
1292 DbgPrint("\t\t%I64d\t", DataRunOffset
);
1293 if (DataRunOffset
< 99999)
1295 DbgPrint("%I64u\t", CurrentLCN
);
1296 if (CurrentLCN
< 99999)
1298 DbgPrint("%I64u\n", DataRunLength
);
1301 DbgPrint("\t\t00\n");
1303 NtfsDumpDataRuns(DataRun
, CurrentLCN
);
1308 NtfsDumpFileAttributes(PDEVICE_EXTENSION Vcb
,
1309 PFILE_RECORD_HEADER FileRecord
)
1312 FIND_ATTR_CONTXT Context
;
1313 PNTFS_ATTR_RECORD Attribute
;
1315 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
1316 while (NT_SUCCESS(Status
))
1318 NtfsDumpAttribute(Vcb
, Attribute
);
1320 Status
= FindNextAttribute(&Context
, &Attribute
);
1323 FindCloseAttribute(&Context
);
1327 GetFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
1328 PFILE_RECORD_HEADER FileRecord
,
1331 FIND_ATTR_CONTXT Context
;
1332 PNTFS_ATTR_RECORD Attribute
;
1333 PFILENAME_ATTRIBUTE Name
;
1336 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
1337 while (NT_SUCCESS(Status
))
1339 if (Attribute
->Type
== AttributeFileName
)
1341 Name
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1342 if (Name
->NameType
== NameType
||
1343 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_WIN32
) ||
1344 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_DOS
))
1346 FindCloseAttribute(&Context
);
1351 Status
= FindNextAttribute(&Context
, &Attribute
);
1354 FindCloseAttribute(&Context
);
1359 * GetPackedByteCount
1360 * Returns the minimum number of bytes needed to represent the value of a
1361 * 64-bit number. Used to encode data runs.
1364 GetPackedByteCount(LONGLONG NumberToPack
,
1370 if (NumberToPack
>= 0x0100000000000000)
1372 if (NumberToPack
>= 0x0001000000000000)
1374 if (NumberToPack
>= 0x0000010000000000)
1376 if (NumberToPack
>= 0x0000000100000000)
1378 if (NumberToPack
>= 0x0000000001000000)
1380 if (NumberToPack
>= 0x0000000000010000)
1382 if (NumberToPack
>= 0x0000000000000100)
1387 if (NumberToPack
> 0)
1389 // we have to make sure the number that gets encoded won't be interpreted as negative
1390 if (NumberToPack
>= 0x0080000000000000)
1392 if (NumberToPack
>= 0x0000800000000000)
1394 if (NumberToPack
>= 0x0000008000000000)
1396 if (NumberToPack
>= 0x0000000080000000)
1398 if (NumberToPack
>= 0x0000000000800000)
1400 if (NumberToPack
>= 0x0000000000008000)
1402 if (NumberToPack
>= 0x0000000000000080)
1409 if (NumberToPack
<= 0xff80000000000000)
1411 if (NumberToPack
<= 0xffff800000000000)
1413 if (NumberToPack
<= 0xffffff8000000000)
1415 if (NumberToPack
<= 0xffffffff80000000)
1417 if (NumberToPack
<= 0xffffffffff800000)
1419 if (NumberToPack
<= 0xffffffffffff8000)
1421 if (NumberToPack
<= 0xffffffffffffff80)
1429 GetLastClusterInDataRun(PDEVICE_EXTENSION Vcb
, PNTFS_ATTR_RECORD Attribute
, PULONGLONG LastCluster
)
1431 LONGLONG DataRunOffset
;
1432 ULONGLONG DataRunLength
;
1433 LONGLONG DataRunStartLCN
;
1435 ULONGLONG LastLCN
= 0;
1436 PUCHAR DataRun
= (PUCHAR
)Attribute
+ Attribute
->NonResident
.MappingPairsOffset
;
1438 if (!Attribute
->IsNonResident
)
1439 return STATUS_INVALID_PARAMETER
;
1443 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
1445 if (DataRunOffset
!= -1)
1448 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
1449 LastLCN
= DataRunStartLCN
;
1450 *LastCluster
= LastLCN
+ DataRunLength
- 1;
1457 return STATUS_SUCCESS
;
1460 PSTANDARD_INFORMATION
1461 GetStandardInformationFromRecord(PDEVICE_EXTENSION Vcb
,
1462 PFILE_RECORD_HEADER FileRecord
)
1465 FIND_ATTR_CONTXT Context
;
1466 PNTFS_ATTR_RECORD Attribute
;
1467 PSTANDARD_INFORMATION StdInfo
;
1469 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
1470 while (NT_SUCCESS(Status
))
1472 if (Attribute
->Type
== AttributeStandardInformation
)
1474 StdInfo
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
1475 FindCloseAttribute(&Context
);
1479 Status
= FindNextAttribute(&Context
, &Attribute
);
1482 FindCloseAttribute(&Context
);
1487 GetBestFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
1488 PFILE_RECORD_HEADER FileRecord
)
1490 PFILENAME_ATTRIBUTE FileName
;
1492 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_POSIX
);
1493 if (FileName
== NULL
)
1495 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_WIN32
);
1496 if (FileName
== NULL
)
1498 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_DOS
);