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.
19 /* $Id: mft.c,v 1.4 2003/11/13 15:26:34 ekohl Exp $
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 *****************************************************************/
31 #include <ddk/ntddk.h>
32 #include <ntos/minmax.h>
40 //#define __min(a,b) (((a) < (b)) ? (a) : (b))
43 /* FUNCTIONS ****************************************************************/
47 NtfsOpenMft (PDEVICE_EXTENSION Vcb
)
50 PFILE_RECORD_HEADER MftRecord
;
51 PFILE_RECORD_HEADER FileRecord
;
52 // PATTRIBUTE Attribute;
53 // PATTRIBUTE AttrData;
54 // PRESIDENT_ATTRIBUTE ResAttr;
57 ULONG BytesPerFileRecord
;
61 DPRINT1("NtfsOpenMft() called\n");
64 Vcb
->NtfsInfo
.ClustersPerFileRecord
* Vcb
->NtfsInfo
.BytesPerCluster
;
66 MftRecord
= ExAllocatePool(NonPagedPool
,
68 if (MftRecord
== NULL
)
70 return STATUS_INSUFFICIENT_RESOURCES
;
73 Status
= NtfsReadSectors(Vcb
->StorageDevice
,
74 Vcb
->NtfsInfo
.MftStart
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
75 BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerSector
,
76 Vcb
->NtfsInfo
.BytesPerSector
,
79 if (!NT_SUCCESS(Status
))
81 ExFreePool(MftRecord
);
86 FixupUpdateSequenceArray(MftRecord
);
88 // Attribute = FindAttribute(MftRecord, AttributeBitmap, 0);
90 /* Get number of file records*/
91 n
= AttributeDataLength (FindAttribute (MftRecord
, AttributeData
, 0))
94 FileRecord
= ExAllocatePool(NonPagedPool
, BytesPerFileRecord
);
95 if (FileRecord
== NULL
)
97 ExFreePool(MftRecord
);
98 return(STATUS_INSUFFICIENT_RESOURCES
);
101 /* Enumerate MFT Records */
102 DPRINT("Enumerate MFT records\n");
103 for ( i
=0; i
< n
; i
++)
105 ReadFileRecord(Vcb
, i
, FileRecord
, MftRecord
);
107 if (FileRecord
->Ntfs
.Type
== NRH_FILE_TYPE
&& (FileRecord
->Flags
& FRH_IN_USE
))
109 DPRINT("\nFile %lu\n\n", i
);
111 /* Enumerate attributtes */
112 NtfsDumpFileAttributes (FileRecord
);
117 ExFreePool(FileRecord
);
118 ExFreePool(MftRecord
);
125 FindAttribute (PFILE_RECORD_HEADER FileRecord
,
129 PATTRIBUTE Attribute
;
131 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)FileRecord
+ FileRecord
->AttributeOffset
);
132 while (Attribute
->AttributeType
!= -1)
134 if (Attribute
->AttributeType
== Type
)
139 Attribute
= (PATTRIBUTE
)((ULONG_PTR
)Attribute
+ Attribute
->Length
);
147 AttributeAllocatedLength (PATTRIBUTE Attribute
)
149 if (Attribute
->Nonresident
)
151 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->AllocatedSize
;
154 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
159 AttributeDataLength (PATTRIBUTE Attribute
)
161 if (Attribute
->Nonresident
)
163 return ((PNONRESIDENT_ATTRIBUTE
)Attribute
)->DataSize
;
166 return ((PRESIDENT_ATTRIBUTE
)Attribute
)->ValueLength
;
171 ReadAttribute (PATTRIBUTE attr
,
173 PDEVICE_EXTENSION Vcb
,
174 PDEVICE_OBJECT DeviceObject
)
176 if (attr
->Nonresident
== FALSE
)
179 (PVOID
)((ULONG_PTR
)attr
+ ((PRESIDENT_ATTRIBUTE
)attr
)->ValueOffset
),
180 ((PRESIDENT_ATTRIBUTE
)attr
)->ValueLength
);
183 PNONRESIDENT_ATTRIBUTE NresAttr
= (PNONRESIDENT_ATTRIBUTE
)attr
;
184 ReadExternalAttribute(Vcb
, NresAttr
, 0, (ULONG
)(NresAttr
->LastVcn
) + 1,
193 ReadFileRecord (PDEVICE_EXTENSION Vcb
,
195 PFILE_RECORD_HEADER file
,
196 PFILE_RECORD_HEADER Mft
)
199 ULONG clusters
= Vcb
->NtfsInfo
.ClustersPerFileRecord
;
200 ULONG BytesPerFileRecord
= clusters
* Vcb
->NtfsInfo
.BytesPerCluster
;
203 p
= ExAllocatePool(NonPagedPool
, BytesPerFileRecord
);
205 ULONGLONG vcn
= index
* BytesPerFileRecord
/ Vcb
->NtfsInfo
.BytesPerCluster
;
207 ReadVCN (Vcb
, Mft
, AttributeData
, vcn
, clusters
, p
);
209 LONG m
= (Vcb
->NtfsInfo
.BytesPerCluster
/ BytesPerFileRecord
) - 1;
211 ULONG n
= m
> 0 ? (index
& m
) : 0;
213 memcpy(file
, p
+ n
* BytesPerFileRecord
, BytesPerFileRecord
);
217 FixupUpdateSequenceArray(file
);
223 ReadExternalAttribute (PDEVICE_EXTENSION Vcb
,
224 PNONRESIDENT_ATTRIBUTE NresAttr
,
234 PUCHAR bytes
= (PUCHAR
)buffer
;
236 for (left
= count
; left
>0; left
-=readcount
)
238 FindRun(NresAttr
, vcn
, &lcn
, &runcount
);
240 // readcount = (ULONG)(__min(runcount, left));
241 readcount
= (ULONG
)min (runcount
, left
);
244 ULONG n
= readcount
* Vcb
->NtfsInfo
.BytesPerCluster
;
249 ReadLCN(Vcb
, lcn
, readcount
, bytes
);
260 ReadVCN (PDEVICE_EXTENSION Vcb
,
261 PFILE_RECORD_HEADER file
,
267 PNONRESIDENT_ATTRIBUTE NresAttr
;
270 attr
= FindAttribute(file
, type
, 0);
272 NresAttr
= (PNONRESIDENT_ATTRIBUTE
) attr
;
274 if (NresAttr
== 0 || (vcn
< NresAttr
->StartVcn
||vcn
> NresAttr
->LastVcn
))
276 // PATTRIBUTE attrList = FindAttribute(file,AttributeAttributeList,0);
277 DbgPrint("Exeption \n");
281 ReadExternalAttribute(Vcb
, NresAttr
, vcn
, count
, buffer
);
285 BOOL
bitset(PUCHAR bitmap
, ULONG i
)
287 return (bitmap
[i
>>3] & (1 << (i
& 7))) !=0;
291 VOID
FixupUpdateSequenceArray(PFILE_RECORD_HEADER file
)
293 PUSHORT usa
= (PUSHORT
)((PVOID
)file
+ file
->Ntfs
.UsaOffset
);
294 PUSHORT sector
= (PUSHORT
)file
;
297 for( i
=1; i
< file
->Ntfs
.UsaCount
; i
++)
299 sector
[255] = usa
[i
];
308 ReadLCN (PDEVICE_EXTENSION Vcb
,
313 LARGE_INTEGER DiskSector
;
315 DiskSector
.QuadPart
= lcn
;
317 return NtfsReadSectors (Vcb
->StorageDevice
,
318 DiskSector
.u
.LowPart
* Vcb
->NtfsInfo
.SectorsPerCluster
,
319 count
* Vcb
->NtfsInfo
.SectorsPerCluster
,
320 Vcb
->NtfsInfo
.BytesPerSector
,