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 ****************************************************************/
39 AddRun(PNTFS_ATTR_CONTEXT AttrContext
,
40 ULONGLONG NextAssignedCluster
,
45 if (!AttrContext
->Record
.IsNonResident
)
46 return STATUS_INVALID_PARAMETER
;
48 return STATUS_NOT_IMPLEMENTED
;
52 DecodeRun(PUCHAR DataRun
,
53 LONGLONG
*DataRunOffset
,
54 ULONGLONG
*DataRunLength
)
56 UCHAR DataRunOffsetSize
;
57 UCHAR DataRunLengthSize
;
60 DataRunOffsetSize
= (*DataRun
>> 4) & 0xF;
61 DataRunLengthSize
= *DataRun
& 0xF;
65 for (i
= 0; i
< DataRunLengthSize
; i
++)
67 *DataRunLength
+= ((ULONG64
)*DataRun
) << (i
* 8);
71 /* NTFS 3+ sparse files */
72 if (DataRunOffsetSize
== 0)
78 for (i
= 0; i
< DataRunOffsetSize
- 1; i
++)
80 *DataRunOffset
+= ((ULONG64
)*DataRun
) << (i
* 8);
83 /* The last byte contains sign so we must process it different way. */
84 *DataRunOffset
= ((LONG64
)(CHAR
)(*(DataRun
++)) << (i
* 8)) + *DataRunOffset
;
87 DPRINT("DataRunOffsetSize: %x\n", DataRunOffsetSize
);
88 DPRINT("DataRunLengthSize: %x\n", DataRunLengthSize
);
89 DPRINT("DataRunOffset: %x\n", *DataRunOffset
);
90 DPRINT("DataRunLength: %x\n", *DataRunLength
);
96 FindRun(PNTFS_ATTR_RECORD NresAttr
,
101 if (vcn
< NresAttr
->NonResident
.LowestVCN
|| vcn
> NresAttr
->NonResident
.HighestVCN
)
104 DecodeRun((PUCHAR
)((ULONG_PTR
)NresAttr
+ NresAttr
->NonResident
.MappingPairsOffset
), (PLONGLONG
)lcn
, count
);
111 InternalReadNonResidentAttributes(PFIND_ATTR_CONTXT Context
)
114 PNTFS_ATTR_RECORD Attribute
;
115 PNTFS_ATTR_CONTEXT ListContext
;
117 DPRINT("InternalReadNonResidentAttributes(%p)\n", Context
);
119 Attribute
= Context
->CurrAttr
;
120 ASSERT(Attribute
->Type
== AttributeAttributeList
);
122 if (Context
->OnlyResident
)
124 Context
->NonResidentStart
= NULL
;
125 Context
->NonResidentEnd
= NULL
;
126 return STATUS_SUCCESS
;
129 if (Context
->NonResidentStart
!= NULL
)
131 return STATUS_FILE_CORRUPT_ERROR
;
134 ListContext
= PrepareAttributeContext(Attribute
);
135 ListSize
= AttributeDataLength(&ListContext
->Record
);
136 if (ListSize
> 0xFFFFFFFF)
138 ReleaseAttributeContext(ListContext
);
139 return STATUS_BUFFER_OVERFLOW
;
142 Context
->NonResidentStart
= ExAllocatePoolWithTag(NonPagedPool
, (ULONG
)ListSize
, TAG_NTFS
);
143 if (Context
->NonResidentStart
== NULL
)
145 ReleaseAttributeContext(ListContext
);
146 return STATUS_INSUFFICIENT_RESOURCES
;
149 if (ReadAttribute(Context
->Vcb
, ListContext
, 0, (PCHAR
)Context
->NonResidentStart
, (ULONG
)ListSize
) != ListSize
)
151 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
152 Context
->NonResidentStart
= NULL
;
153 ReleaseAttributeContext(ListContext
);
154 return STATUS_FILE_CORRUPT_ERROR
;
157 ReleaseAttributeContext(ListContext
);
158 Context
->NonResidentEnd
= (PNTFS_ATTR_RECORD
)((PCHAR
)Context
->NonResidentStart
+ ListSize
);
159 return STATUS_SUCCESS
;
164 InternalGetNextAttribute(PFIND_ATTR_CONTXT Context
)
166 PNTFS_ATTR_RECORD NextAttribute
;
168 if (Context
->CurrAttr
== (PVOID
)-1)
173 if (Context
->CurrAttr
>= Context
->FirstAttr
&&
174 Context
->CurrAttr
< Context
->LastAttr
)
176 if (Context
->CurrAttr
->Length
== 0)
178 DPRINT1("Broken length!\n");
179 Context
->CurrAttr
= (PVOID
)-1;
183 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
184 Context
->Offset
+= ((ULONG_PTR
)NextAttribute
- (ULONG_PTR
)Context
->CurrAttr
);
185 Context
->CurrAttr
= NextAttribute
;
187 if (Context
->CurrAttr
< Context
->LastAttr
&&
188 Context
->CurrAttr
->Type
!= AttributeEnd
)
190 return Context
->CurrAttr
;
194 if (Context
->NonResidentStart
== NULL
)
196 Context
->CurrAttr
= (PVOID
)-1;
200 if (Context
->CurrAttr
< Context
->NonResidentStart
||
201 Context
->CurrAttr
>= Context
->NonResidentEnd
)
203 Context
->CurrAttr
= Context
->NonResidentStart
;
205 else if (Context
->CurrAttr
->Length
!= 0)
207 NextAttribute
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)Context
->CurrAttr
+ Context
->CurrAttr
->Length
);
208 Context
->Offset
+= ((ULONG_PTR
)NextAttribute
- (ULONG_PTR
)Context
->CurrAttr
);
209 Context
->CurrAttr
= NextAttribute
;
213 DPRINT1("Broken length!\n");
214 Context
->CurrAttr
= (PVOID
)-1;
218 if (Context
->CurrAttr
< Context
->NonResidentEnd
&&
219 Context
->CurrAttr
->Type
!= AttributeEnd
)
221 return Context
->CurrAttr
;
224 Context
->CurrAttr
= (PVOID
)-1;
229 FindFirstAttribute(PFIND_ATTR_CONTXT Context
,
230 PDEVICE_EXTENSION Vcb
,
231 PFILE_RECORD_HEADER FileRecord
,
232 BOOLEAN OnlyResident
,
233 PNTFS_ATTR_RECORD
* Attribute
)
237 DPRINT("FindFistAttribute(%p, %p, %p, %p, %u, %p)\n", Context
, Vcb
, FileRecord
, OnlyResident
, Attribute
);
240 Context
->OnlyResident
= OnlyResident
;
241 Context
->FirstAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
242 Context
->CurrAttr
= Context
->FirstAttr
;
243 Context
->LastAttr
= (PNTFS_ATTR_RECORD
)((ULONG_PTR
)FileRecord
+ FileRecord
->BytesInUse
);
244 Context
->NonResidentStart
= NULL
;
245 Context
->NonResidentEnd
= NULL
;
246 Context
->Offset
= FileRecord
->AttributeOffset
;
248 if (Context
->FirstAttr
->Type
== AttributeEnd
)
250 Context
->CurrAttr
= (PVOID
)-1;
251 return STATUS_END_OF_FILE
;
253 else if (Context
->FirstAttr
->Type
== AttributeAttributeList
)
255 Status
= InternalReadNonResidentAttributes(Context
);
256 if (!NT_SUCCESS(Status
))
261 *Attribute
= InternalGetNextAttribute(Context
);
262 if (*Attribute
== NULL
)
264 return STATUS_END_OF_FILE
;
269 *Attribute
= Context
->CurrAttr
;
270 Context
->Offset
= (UCHAR
*)Context
->CurrAttr
- (UCHAR
*)FileRecord
;
273 return STATUS_SUCCESS
;
277 FindNextAttribute(PFIND_ATTR_CONTXT Context
,
278 PNTFS_ATTR_RECORD
* Attribute
)
282 DPRINT("FindNextAttribute(%p, %p)\n", Context
, Attribute
);
284 *Attribute
= InternalGetNextAttribute(Context
);
285 if (*Attribute
== NULL
)
287 return STATUS_END_OF_FILE
;
290 if (Context
->CurrAttr
->Type
!= AttributeAttributeList
)
292 return STATUS_SUCCESS
;
295 Status
= InternalReadNonResidentAttributes(Context
);
296 if (!NT_SUCCESS(Status
))
301 *Attribute
= InternalGetNextAttribute(Context
);
302 if (*Attribute
== NULL
)
304 return STATUS_END_OF_FILE
;
307 return STATUS_SUCCESS
;
311 FindCloseAttribute(PFIND_ATTR_CONTXT Context
)
313 if (Context
->NonResidentStart
!= NULL
)
315 ExFreePoolWithTag(Context
->NonResidentStart
, TAG_NTFS
);
316 Context
->NonResidentStart
= NULL
;
322 NtfsDumpFileNameAttribute(PNTFS_ATTR_RECORD Attribute
)
324 PFILENAME_ATTRIBUTE FileNameAttr
;
326 DbgPrint(" $FILE_NAME ");
328 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
330 FileNameAttr
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
331 DbgPrint(" (%x) '%.*S' ", FileNameAttr
->NameType
, FileNameAttr
->NameLength
, FileNameAttr
->Name
);
332 DbgPrint(" '%x' \n", FileNameAttr
->FileAttributes
);
333 DbgPrint(" AllocatedSize: %I64u\nDataSize: %I64u\n", FileNameAttr
->AllocatedSize
, FileNameAttr
->DataSize
);
339 NtfsDumpStandardInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
341 PSTANDARD_INFORMATION StandardInfoAttr
;
343 DbgPrint(" $STANDARD_INFORMATION ");
345 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
347 StandardInfoAttr
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
348 DbgPrint(" '%x' ", StandardInfoAttr
->FileAttribute
);
354 NtfsDumpVolumeNameAttribute(PNTFS_ATTR_RECORD Attribute
)
358 DbgPrint(" $VOLUME_NAME ");
360 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
362 VolumeName
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
363 DbgPrint(" '%.*S' ", Attribute
->Resident
.ValueLength
/ sizeof(WCHAR
), VolumeName
);
369 NtfsDumpVolumeInformationAttribute(PNTFS_ATTR_RECORD Attribute
)
371 PVOLINFO_ATTRIBUTE VolInfoAttr
;
373 DbgPrint(" $VOLUME_INFORMATION ");
375 // DbgPrint(" Length %lu Offset %hu ", Attribute->Resident.ValueLength, Attribute->Resident.ValueOffset);
377 VolInfoAttr
= (PVOLINFO_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
378 DbgPrint(" NTFS Version %u.%u Flags 0x%04hx ",
379 VolInfoAttr
->MajorVersion
,
380 VolInfoAttr
->MinorVersion
,
387 NtfsDumpIndexRootAttribute(PNTFS_ATTR_RECORD Attribute
)
389 PINDEX_ROOT_ATTRIBUTE IndexRootAttr
;
391 IndexRootAttr
= (PINDEX_ROOT_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
393 if (IndexRootAttr
->AttributeType
== AttributeFileName
)
394 ASSERT(IndexRootAttr
->CollationRule
== COLLATION_FILE_NAME
);
396 DbgPrint(" $INDEX_ROOT (%uB, %u) ", IndexRootAttr
->SizeOfEntry
, IndexRootAttr
->ClustersPerIndexRecord
);
398 if (IndexRootAttr
->Header
.Flags
== INDEX_ROOT_SMALL
)
400 DbgPrint(" (small) ");
404 ASSERT(IndexRootAttr
->Header
.Flags
== INDEX_ROOT_LARGE
);
405 DbgPrint(" (large) ");
412 NtfsDumpAttribute(PDEVICE_EXTENSION Vcb
,
413 PNTFS_ATTR_RECORD Attribute
)
418 ULONGLONG runcount
= 0;
420 switch (Attribute
->Type
)
422 case AttributeFileName
:
423 NtfsDumpFileNameAttribute(Attribute
);
426 case AttributeStandardInformation
:
427 NtfsDumpStandardInformationAttribute(Attribute
);
430 case AttributeObjectId
:
431 DbgPrint(" $OBJECT_ID ");
434 case AttributeSecurityDescriptor
:
435 DbgPrint(" $SECURITY_DESCRIPTOR ");
438 case AttributeVolumeName
:
439 NtfsDumpVolumeNameAttribute(Attribute
);
442 case AttributeVolumeInformation
:
443 NtfsDumpVolumeInformationAttribute(Attribute
);
448 //DataBuf = ExAllocatePool(NonPagedPool,AttributeLengthAllocated(Attribute));
451 case AttributeIndexRoot
:
452 NtfsDumpIndexRootAttribute(Attribute
);
455 case AttributeIndexAllocation
:
456 DbgPrint(" $INDEX_ALLOCATION ");
459 case AttributeBitmap
:
460 DbgPrint(" $BITMAP ");
463 case AttributeReparsePoint
:
464 DbgPrint(" $REPARSE_POINT ");
467 case AttributeEAInformation
:
468 DbgPrint(" $EA_INFORMATION ");
475 case AttributePropertySet
:
476 DbgPrint(" $PROPERTY_SET ");
479 case AttributeLoggedUtilityStream
:
480 DbgPrint(" $LOGGED_UTILITY_STREAM ");
484 DbgPrint(" Attribute %lx ",
489 if (Attribute
->Type
!= AttributeAttributeList
)
491 if (Attribute
->NameLength
!= 0)
493 Name
.Length
= Attribute
->NameLength
* sizeof(WCHAR
);
494 Name
.MaximumLength
= Name
.Length
;
495 Name
.Buffer
= (PWCHAR
)((ULONG_PTR
)Attribute
+ Attribute
->NameOffset
);
497 DbgPrint("'%wZ' ", &Name
);
501 Attribute
->IsNonResident
? "non-resident" : "resident");
503 if (Attribute
->IsNonResident
)
505 FindRun(Attribute
,0,&lcn
, &runcount
);
507 DbgPrint(" AllocatedSize %I64u DataSize %I64u InitilizedSize %I64u\n",
508 Attribute
->NonResident
.AllocatedSize
, Attribute
->NonResident
.DataSize
, Attribute
->NonResident
.InitializedSize
);
509 DbgPrint(" logical clusters: %I64u - %I64u\n",
510 lcn
, lcn
+ runcount
- 1);
517 NtfsDumpFileAttributes(PDEVICE_EXTENSION Vcb
,
518 PFILE_RECORD_HEADER FileRecord
)
521 FIND_ATTR_CONTXT Context
;
522 PNTFS_ATTR_RECORD Attribute
;
524 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
525 while (NT_SUCCESS(Status
))
527 NtfsDumpAttribute(Vcb
, Attribute
);
529 Status
= FindNextAttribute(&Context
, &Attribute
);
532 FindCloseAttribute(&Context
);
536 GetFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
537 PFILE_RECORD_HEADER FileRecord
,
540 FIND_ATTR_CONTXT Context
;
541 PNTFS_ATTR_RECORD Attribute
;
542 PFILENAME_ATTRIBUTE Name
;
545 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
546 while (NT_SUCCESS(Status
))
548 if (Attribute
->Type
== AttributeFileName
)
550 Name
= (PFILENAME_ATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
551 if (Name
->NameType
== NameType
||
552 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_WIN32
) ||
553 (Name
->NameType
== NTFS_FILE_NAME_WIN32_AND_DOS
&& NameType
== NTFS_FILE_NAME_DOS
))
555 FindCloseAttribute(&Context
);
560 Status
= FindNextAttribute(&Context
, &Attribute
);
563 FindCloseAttribute(&Context
);
568 GetLastClusterInDataRun(PDEVICE_EXTENSION Vcb
, PNTFS_ATTR_RECORD Attribute
, PULONGLONG LastCluster
)
570 LONGLONG DataRunOffset
;
571 ULONGLONG DataRunLength
;
572 LONGLONG DataRunStartLCN
;
574 ULONGLONG LastLCN
= 0;
575 PUCHAR DataRun
= (PUCHAR
)Attribute
+ Attribute
->NonResident
.MappingPairsOffset
;
577 if (!Attribute
->IsNonResident
)
578 return STATUS_INVALID_PARAMETER
;
582 DataRun
= DecodeRun(DataRun
, &DataRunOffset
, &DataRunLength
);
584 if (DataRunOffset
== -1)
589 // if it's the last run, return the last cluster of the last run
590 *LastCluster
= LastLCN
+ DataRunLength
- 1;
597 DataRunStartLCN
= LastLCN
+ DataRunOffset
;
598 LastLCN
= DataRunStartLCN
;
603 *LastCluster
= LastLCN
+ DataRunLength
- 1;
608 return STATUS_SUCCESS
;
611 PSTANDARD_INFORMATION
612 GetStandardInformationFromRecord(PDEVICE_EXTENSION Vcb
,
613 PFILE_RECORD_HEADER FileRecord
)
616 FIND_ATTR_CONTXT Context
;
617 PNTFS_ATTR_RECORD Attribute
;
618 PSTANDARD_INFORMATION StdInfo
;
620 Status
= FindFirstAttribute(&Context
, Vcb
, FileRecord
, FALSE
, &Attribute
);
621 while (NT_SUCCESS(Status
))
623 if (Attribute
->Type
== AttributeStandardInformation
)
625 StdInfo
= (PSTANDARD_INFORMATION
)((ULONG_PTR
)Attribute
+ Attribute
->Resident
.ValueOffset
);
626 FindCloseAttribute(&Context
);
630 Status
= FindNextAttribute(&Context
, &Attribute
);
633 FindCloseAttribute(&Context
);
638 GetBestFileNameFromRecord(PDEVICE_EXTENSION Vcb
,
639 PFILE_RECORD_HEADER FileRecord
)
641 PFILENAME_ATTRIBUTE FileName
;
643 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_POSIX
);
644 if (FileName
== NULL
)
646 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_WIN32
);
647 if (FileName
== NULL
)
649 FileName
= GetFileNameFromRecord(Vcb
, FileRecord
, NTFS_FILE_NAME_DOS
);