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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: services/fs/ntfs/mft.c
24 * PURPOSE: NTFS filesystem driver
25 * PROGRAMMER: Eric Kohl
26 * Updated by Valentin Verkhovsky 2003/09/12
29 /* INCLUDES *****************************************************************/
36 /* GLOBALS *****************************************************************/
39 /* FUNCTIONS ****************************************************************/
43 NtfsOpenMft (PDEVICE_EXTENSION Vcb
)
46 PFILE_RECORD_HEADER MftRecord
;
47 PFILE_RECORD_HEADER FileRecord
;
48 // PATTRIBUTE Attribute;
49 // PATTRIBUTE AttrData;
50 // PRESIDENT_ATTRIBUTE ResAttr;
53 ULONG BytesPerFileRecord
;
57 DPRINT1("NtfsOpenMft() called\n");
59 BytesPerFileRecord
= Vcb
->NtfsInfo
.BytesPerFileRecord
;
61 MftRecord
= ExAllocatePool(NonPagedPool
,
63 if (MftRecord
== NULL
)
65 return STATUS_INSUFFICIENT_RESOURCES
;
68 Status
= NtfsReadSectors(Vcb
->StorageDevice
,
69 Vcb
->NtfsInfo
.MftStart
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
70 BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerSector
,
71 Vcb
->NtfsInfo
.BytesPerSector
,
74 if (!NT_SUCCESS(Status
))
76 ExFreePool(MftRecord
);
81 FixupUpdateSequenceArray(MftRecord
);
83 // Attribute = FindAttribute(MftRecord, AttributeBitmap, 0);
85 /* Get number of file records*/
86 n
= AttributeDataLength (FindAttribute (MftRecord
, AttributeData
, 0))
89 FileRecord
= ExAllocatePool(NonPagedPool
, BytesPerFileRecord
);
90 if (FileRecord
== NULL
)
92 ExFreePool(MftRecord
);
93 return(STATUS_INSUFFICIENT_RESOURCES
);
96 /* Enumerate MFT Records */
97 DPRINT("Enumerate MFT records\n");
98 for ( i
=0; i
< n
; i
++)
100 ReadFileRecord(Vcb
, i
, FileRecord
, MftRecord
);
102 if (FileRecord
->Ntfs
.Type
== NRH_FILE_TYPE
&& (FileRecord
->Flags
& FRH_IN_USE
))
104 DPRINT("\nFile %lu\n\n", i
);
106 /* Enumerate attributtes */
107 NtfsDumpFileAttributes (FileRecord
);
112 ExFreePool(FileRecord
);
113 ExFreePool(MftRecord
);
120 FindAttribute (PFILE_RECORD_HEADER FileRecord
,
124 PATTRIBUTE Attribute
;
126 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
127 while (Attribute
< (PATTRIBUTE
)((ULONG_PTR
)FileRecord
+ FileRecord
->BytesInUse
) &&
128 Attribute
->AttributeType
!= (ATTRIBUTE_TYPE
)-1)
130 if (Attribute
->AttributeType
== Type
)
135 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Length
);
143 AttributeAllocatedLength (PATTRIBUTE Attribute
)
145 if (Attribute
->Nonresident
)
147 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->AllocatedSize
;
150 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
155 AttributeDataLength (PATTRIBUTE Attribute
)
157 if (Attribute
->Nonresident
)
159 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->DataSize
;
162 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
167 ReadAttribute (PATTRIBUTE attr
,
169 PDEVICE_EXTENSION Vcb
,
170 PDEVICE_OBJECT DeviceObject
)
172 if (attr
->Nonresident
== FALSE
)
175 (PVOID
)((ULONG_PTR
)attr
+ ((PRESIDENT_ATTRIBUTE
)attr
)->ValueOffset
),
176 ((PRESIDENT_ATTRIBUTE
)attr
)->ValueLength
);
179 PNONRESIDENT_ATTRIBUTE NresAttr
= (PNONRESIDENT_ATTRIBUTE
)attr
;
180 ReadExternalAttribute(Vcb
, NresAttr
, 0, (ULONG
)(NresAttr
->LastVcn
) + 1,
189 ReadFileRecord (PDEVICE_EXTENSION Vcb
,
191 PFILE_RECORD_HEADER file
,
192 PFILE_RECORD_HEADER Mft
)
195 ULONG BytesPerFileRecord
= Vcb
->NtfsInfo
.BytesPerFileRecord
;
196 ULONG clusters
= max(BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerCluster
, 1);
198 p
= ExAllocatePool(NonPagedPool
, clusters
* Vcb
->NtfsInfo
.BytesPerCluster
);
200 ULONGLONG vcn
= index
* BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerCluster
;
202 ReadVCN (Vcb
, Mft
, AttributeData
, vcn
, clusters
, p
);
204 LONG m
= (Vcb
->NtfsInfo
.BytesPerCluster
/ BytesPerFileRecord
) - 1;
206 ULONG n
= m
> 0 ? (index
& m
) : 0;
208 memcpy(file
, (PVOID
)((ULONG_PTR
)p
+ n
* BytesPerFileRecord
), BytesPerFileRecord
);
212 FixupUpdateSequenceArray(file
);
214 return STATUS_SUCCESS
;
220 ReadExternalAttribute (PDEVICE_EXTENSION Vcb
,
221 PNONRESIDENT_ATTRIBUTE NresAttr
,
231 PUCHAR bytes
= (PUCHAR
)buffer
;
233 for (left
= count
; left
>0; left
-=readcount
)
235 FindRun(NresAttr
, vcn
, &lcn
, &runcount
);
237 // readcount = (ULONG)(__min(runcount, left));
238 readcount
= (ULONG
)min (runcount
, left
);
241 ULONG n
= readcount
* Vcb
->NtfsInfo
.BytesPerCluster
;
246 ReadLCN(Vcb
, lcn
, readcount
, bytes
);
257 ReadVCN (PDEVICE_EXTENSION Vcb
,
258 PFILE_RECORD_HEADER file
,
264 PNONRESIDENT_ATTRIBUTE NresAttr
;
267 attr
= FindAttribute(file
, type
, 0);
269 NresAttr
= (PNONRESIDENT_ATTRIBUTE
) attr
;
271 if (NresAttr
== 0 || (vcn
< NresAttr
->StartVcn
||vcn
> NresAttr
->LastVcn
))
273 // PATTRIBUTE attrList = FindAttribute(file,AttributeAttributeList,0);
274 DbgPrint("Exeption \n");
278 ReadExternalAttribute(Vcb
, NresAttr
, vcn
, count
, buffer
);
283 BOOL
bitset(PUCHAR bitmap
, ULONG i
)
285 return (bitmap
[i
>>3] & (1 << (i
& 7))) !=0;
290 VOID
FixupUpdateSequenceArray(PFILE_RECORD_HEADER file
)
292 PUSHORT usa
= (PUSHORT
)((ULONG_PTR
)file
+ file
->Ntfs
.UsaOffset
);
293 PUSHORT sector
= (PUSHORT
)file
;
296 for( i
=1; i
< file
->Ntfs
.UsaCount
; i
++)
298 sector
[255] = usa
[i
];
307 ReadLCN (PDEVICE_EXTENSION Vcb
,
312 LARGE_INTEGER DiskSector
;
314 DiskSector
.QuadPart
= lcn
;
316 return NtfsReadSectors (Vcb
->StorageDevice
,
317 DiskSector
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
318 count
* Vcb
->NtfsInfo
.SectorsPerCluster
,
319 Vcb
->NtfsInfo
.BytesPerSector
,