3 * Copyright (C) 2002 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/mft.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMER: Eric Kohl
24 * Updated by Valentin Verkhovsky 2003/09/12
27 /* INCLUDES *****************************************************************/
34 /* GLOBALS *****************************************************************/
37 /* FUNCTIONS ****************************************************************/
41 NtfsOpenMft (PDEVICE_EXTENSION Vcb
)
44 PFILE_RECORD_HEADER MftRecord
;
45 PFILE_RECORD_HEADER FileRecord
;
46 // PATTRIBUTE Attribute;
47 // PATTRIBUTE AttrData;
48 // PRESIDENT_ATTRIBUTE ResAttr;
51 ULONG BytesPerFileRecord
;
55 DPRINT1("NtfsOpenMft() called\n");
57 BytesPerFileRecord
= Vcb
->NtfsInfo
.BytesPerFileRecord
;
59 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
60 BytesPerFileRecord
, TAG_NTFS
);
61 if (MftRecord
== NULL
)
63 return STATUS_INSUFFICIENT_RESOURCES
;
66 Status
= NtfsReadSectors(Vcb
->StorageDevice
,
67 Vcb
->NtfsInfo
.MftStart
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
68 BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerSector
,
69 Vcb
->NtfsInfo
.BytesPerSector
,
72 if (!NT_SUCCESS(Status
))
74 ExFreePool(MftRecord
);
79 FixupUpdateSequenceArray(MftRecord
);
81 // Attribute = FindAttribute(MftRecord, AttributeBitmap, 0);
83 /* Get number of file records*/
84 n
= AttributeDataLength (FindAttribute (MftRecord
, AttributeData
, 0))
87 FileRecord
= ExAllocatePoolWithTag(NonPagedPool
, BytesPerFileRecord
, TAG_NTFS
);
88 if (FileRecord
== NULL
)
90 ExFreePool(MftRecord
);
91 return(STATUS_INSUFFICIENT_RESOURCES
);
94 /* Enumerate MFT Records */
95 DPRINT("Enumerate MFT records\n");
96 for ( i
=0; i
< n
; i
++)
98 ReadFileRecord(Vcb
, i
, FileRecord
, MftRecord
);
100 if (FileRecord
->Ntfs
.Type
== NRH_FILE_TYPE
&& (FileRecord
->Flags
& FRH_IN_USE
))
102 DPRINT("\nFile %lu\n\n", i
);
104 /* Enumerate attributtes */
105 NtfsDumpFileAttributes (FileRecord
);
110 ExFreePool(FileRecord
);
111 ExFreePool(MftRecord
);
118 FindAttribute (PFILE_RECORD_HEADER FileRecord
,
122 PATTRIBUTE Attribute
;
124 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
125 while (Attribute
< (PATTRIBUTE
)((ULONG_PTR
)FileRecord
+ FileRecord
->BytesInUse
) &&
126 Attribute
->AttributeType
!= (ATTRIBUTE_TYPE
)-1)
128 if (Attribute
->AttributeType
== Type
)
133 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Length
);
141 AttributeAllocatedLength (PATTRIBUTE Attribute
)
143 if (Attribute
->Nonresident
)
145 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->AllocatedSize
;
148 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
153 AttributeDataLength (PATTRIBUTE Attribute
)
155 if (Attribute
->Nonresident
)
157 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->DataSize
;
160 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
165 ReadAttribute (PATTRIBUTE attr
,
167 PDEVICE_EXTENSION Vcb
,
168 PDEVICE_OBJECT DeviceObject
)
170 PNONRESIDENT_ATTRIBUTE NresAttr
= (PNONRESIDENT_ATTRIBUTE
)attr
;
171 if (attr
->Nonresident
== FALSE
)
174 (PVOID
)((ULONG_PTR
)attr
+ ((PRESIDENT_ATTRIBUTE
)attr
)->ValueOffset
),
175 ((PRESIDENT_ATTRIBUTE
)attr
)->ValueLength
);
178 ReadExternalAttribute(Vcb
, NresAttr
, 0, (ULONG
)(NresAttr
->LastVcn
) + 1,
187 ReadFileRecord (PDEVICE_EXTENSION Vcb
,
189 PFILE_RECORD_HEADER file
,
190 PFILE_RECORD_HEADER Mft
)
193 ULONG BytesPerFileRecord
= Vcb
->NtfsInfo
.BytesPerFileRecord
;
194 ULONG clusters
= max(BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerCluster
, 1);
195 ULONGLONG vcn
= index
* BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerCluster
;
196 LONG m
= (Vcb
->NtfsInfo
.BytesPerCluster
/ BytesPerFileRecord
) - 1;
197 ULONG n
= m
> 0 ? (index
& m
) : 0;
199 p
= ExAllocatePoolWithTag(NonPagedPool
, clusters
* Vcb
->NtfsInfo
.BytesPerCluster
, TAG_NTFS
);
201 ReadVCN (Vcb
, Mft
, AttributeData
, vcn
, clusters
, p
);
203 memcpy(file
, (PVOID
)((ULONG_PTR
)p
+ n
* BytesPerFileRecord
), BytesPerFileRecord
);
207 FixupUpdateSequenceArray(file
);
209 return STATUS_SUCCESS
;
215 ReadExternalAttribute (PDEVICE_EXTENSION Vcb
,
216 PNONRESIDENT_ATTRIBUTE NresAttr
,
227 PUCHAR bytes
= (PUCHAR
)buffer
;
229 for (left
= count
; left
>0; left
-=readcount
)
231 FindRun(NresAttr
, vcn
, &lcn
, &runcount
);
233 // readcount = (ULONG)(__min(runcount, left));
234 readcount
= (ULONG
)min (runcount
, left
);
237 n
= readcount
* Vcb
->NtfsInfo
.BytesPerCluster
;
242 ReadLCN(Vcb
, lcn
, readcount
, bytes
);
253 ReadVCN (PDEVICE_EXTENSION Vcb
,
254 PFILE_RECORD_HEADER file
,
260 PNONRESIDENT_ATTRIBUTE NresAttr
;
263 attr
= FindAttribute(file
, type
, 0);
265 NresAttr
= (PNONRESIDENT_ATTRIBUTE
) attr
;
267 if (NresAttr
== 0 || (vcn
< NresAttr
->StartVcn
||vcn
> NresAttr
->LastVcn
))
269 // PATTRIBUTE attrList = FindAttribute(file,AttributeAttributeList,0);
270 DbgPrint("Exeption \n");
274 ReadExternalAttribute(Vcb
, NresAttr
, vcn
, count
, buffer
);
279 BOOL
bitset(PUCHAR bitmap
, ULONG i
)
281 return (bitmap
[i
>>3] & (1 << (i
& 7))) !=0;
286 VOID
FixupUpdateSequenceArray(PFILE_RECORD_HEADER file
)
288 PUSHORT usa
= (PUSHORT
)((ULONG_PTR
)file
+ file
->Ntfs
.UsaOffset
);
289 PUSHORT sector
= (PUSHORT
)file
;
292 for( i
=1; i
< file
->Ntfs
.UsaCount
; i
++)
294 sector
[255] = usa
[i
];
303 ReadLCN (PDEVICE_EXTENSION Vcb
,
308 LARGE_INTEGER DiskSector
;
310 DiskSector
.QuadPart
= lcn
;
312 return NtfsReadSectors (Vcb
->StorageDevice
,
313 DiskSector
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
314 count
* Vcb
->NtfsInfo
.SectorsPerCluster
,
315 Vcb
->NtfsInfo
.BytesPerSector
,