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/fcb.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMER: Eric Kohl
26 /* INCLUDES *****************************************************************/
33 /* MACROS *******************************************************************/
35 #define TAG_FCB 'BCFI'
37 /* FUNCTIONS ****************************************************************/
41 NtfsGetNextPathElement(PWCHAR FileName
)
43 if (*FileName
== L
'\0')
48 while (*FileName
!= L
'\0' && *FileName
!= L
'\\')
59 NtfsWSubString(PWCHAR pTarget
,
63 wcsncpy(pTarget
, pSource
, pLength
);
64 pTarget
[pLength
] = L
'\0';
69 NtfsCreateFCB(PCWSTR FileName
,
75 ASSERT(Vcb
->Identifier
.Type
== NTFS_TYPE_VCB
);
77 Fcb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(NTFS_FCB
), TAG_FCB
);
78 RtlZeroMemory(Fcb
, sizeof(NTFS_FCB
));
80 Fcb
->Identifier
.Type
= NTFS_TYPE_FCB
;
81 Fcb
->Identifier
.Size
= sizeof(NTFS_TYPE_FCB
);
87 wcscpy(Fcb
->PathName
, FileName
);
88 if (wcsrchr(Fcb
->PathName
, '\\') != 0)
90 Fcb
->ObjectName
= wcsrchr(Fcb
->PathName
, '\\');
94 Fcb
->ObjectName
= Fcb
->PathName
;
98 ExInitializeResourceLite(&Fcb
->MainResource
);
100 Fcb
->RFCB
.Resource
= &(Fcb
->MainResource
);
107 NtfsDestroyFCB(PNTFS_FCB Fcb
)
110 ASSERT(Fcb
->Identifier
.Type
== NTFS_TYPE_FCB
);
112 ExDeleteResourceLite(&Fcb
->MainResource
);
119 NtfsFCBIsDirectory(PNTFS_FCB Fcb
)
121 UNREFERENCED_PARAMETER(Fcb
);
122 // return(Fcb->entry.Attrib & FILE_ATTRIBUTE_DIRECTORY);
123 // return(Fcb->Entry.FileFlags & 0x02);
129 NtfsFCBIsRoot(PNTFS_FCB Fcb
)
131 return (wcscmp(Fcb
->PathName
, L
"\\") == 0);
136 NtfsGrabFCB(PNTFS_VCB Vcb
,
141 DPRINT("grabbing FCB at %p: %S, refCount:%d\n",
146 KeAcquireSpinLock(&Vcb
->FcbListLock
, &oldIrql
);
148 KeReleaseSpinLock(&Vcb
->FcbListLock
, oldIrql
);
153 NtfsReleaseFCB(PNTFS_VCB Vcb
,
158 DPRINT("releasing FCB at %p: %S, refCount:%d\n",
163 KeAcquireSpinLock(&Vcb
->FcbListLock
, &oldIrql
);
165 if (Fcb
->RefCount
<= 0 && !NtfsFCBIsDirectory(Fcb
))
167 RemoveEntryList(&Fcb
->FcbListEntry
);
168 CcUninitializeCacheMap(Fcb
->FileObject
, NULL
, NULL
);
172 KeReleaseSpinLock(&Vcb
->FcbListLock
, oldIrql
);
177 NtfsAddFCBToTable(PNTFS_VCB Vcb
,
182 KeAcquireSpinLock(&Vcb
->FcbListLock
, &oldIrql
);
184 InsertTailList(&Vcb
->FcbListHead
, &Fcb
->FcbListEntry
);
185 KeReleaseSpinLock(&Vcb
->FcbListLock
, oldIrql
);
190 NtfsGrabFCBFromTable(PNTFS_VCB Vcb
,
195 PLIST_ENTRY current_entry
;
197 KeAcquireSpinLock(&Vcb
->FcbListLock
, &oldIrql
);
199 if (FileName
== NULL
|| *FileName
== 0)
201 DPRINT("Return FCB for stream file object\n");
202 Fcb
= Vcb
->StreamFileObject
->FsContext
;
204 KeReleaseSpinLock(&Vcb
->FcbListLock
, oldIrql
);
208 current_entry
= Vcb
->FcbListHead
.Flink
;
209 while (current_entry
!= &Vcb
->FcbListHead
)
211 Fcb
= CONTAINING_RECORD(current_entry
, NTFS_FCB
, FcbListEntry
);
213 DPRINT("Comparing '%S' and '%S'\n", FileName
, Fcb
->PathName
);
214 if (_wcsicmp(FileName
, Fcb
->PathName
) == 0)
217 KeReleaseSpinLock(&Vcb
->FcbListLock
, oldIrql
);
221 //FIXME: need to compare against short name in FCB here
223 current_entry
= current_entry
->Flink
;
226 KeReleaseSpinLock(&Vcb
->FcbListLock
, oldIrql
);
233 NtfsFCBInitializeCache(PNTFS_VCB Vcb
,
236 PFILE_OBJECT FileObject
;
240 FileObject
= IoCreateStreamFileObject(NULL
, Vcb
->StorageDevice
);
242 newCCB
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(NTFS_CCB
), TAG_CCB
);
245 return STATUS_INSUFFICIENT_RESOURCES
;
248 RtlZeroMemory(newCCB
, sizeof(NTFS_CCB
));
250 newCCB
->Identifier
.Type
= NTFS_TYPE_CCB
;
251 newCCB
->Identifier
.Size
= sizeof(NTFS_TYPE_CCB
);
253 FileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
254 FileObject
->FsContext
= Fcb
;
255 FileObject
->FsContext2
= newCCB
;
256 newCCB
->PtrFileObject
= FileObject
;
257 Fcb
->FileObject
= FileObject
;
260 Status
= STATUS_SUCCESS
;
261 CcInitializeCacheMap(FileObject
,
262 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
264 &(NtfsGlobalData
->CacheMgrCallbacks
),
267 ObDereferenceObject(FileObject
);
268 Fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
275 NtfsMakeRootFCB(PNTFS_VCB Vcb
)
279 Fcb
= NtfsCreateFCB(L
"\\", Vcb
);
281 // memset(Fcb->entry.Filename, ' ', 11);
283 // Fcb->Entry.DataLengthL = Vcb->CdInfo.RootSize;
284 // Fcb->Entry.ExtentLocationL = Vcb->CdInfo.RootStart;
285 // Fcb->Entry.FileFlags = 0x02; // FILE_ATTRIBUTE_DIRECTORY;
288 Fcb
->RFCB
.FileSize
.QuadPart
= PAGE_SIZE
;//Vcb->CdInfo.RootSize;
289 Fcb
->RFCB
.ValidDataLength
.QuadPart
= PAGE_SIZE
;//Vcb->CdInfo.RootSize;
290 Fcb
->RFCB
.AllocationSize
.QuadPart
= PAGE_SIZE
;//Vcb->CdInfo.RootSize;
291 Fcb
->MFTIndex
= NTFS_FILE_ROOT
;
293 NtfsFCBInitializeCache(Vcb
, Fcb
);
294 NtfsAddFCBToTable(Vcb
, Fcb
);
295 NtfsGrabFCB(Vcb
, Fcb
);
302 NtfsOpenRootFCB(PNTFS_VCB Vcb
)
306 Fcb
= NtfsGrabFCBFromTable(Vcb
, L
"\\");
309 Fcb
= NtfsMakeRootFCB(Vcb
);
318 NtfsGetDirEntryName(PDEVICE_EXTENSION DeviceExt
,
322 * FUNCTION: Retrieves the file name, be it in short or long file name format
325 if (Record
->FileIdLength
== 1 && Record
->FileId
[0] == 0)
329 else if (Record
->FileIdLength
== 1 && Record
->FileId
[0] == 1)
335 if (DeviceExt
->CdInfo
.JolietLevel
== 0)
339 for (i
= 0; i
< Record
->FileIdLength
&& Record
->FileId
[i
] != ';'; i
++)
340 Name
[i
] = (WCHAR
)Record
->FileId
[i
];
345 NtfsSwapString(Name
, Record
->FileId
, Record
->FileIdLength
);
349 DPRINT("Name '%S'\n", Name
);
355 NtfsMakeFCBFromDirEntry(PNTFS_VCB Vcb
,
356 PNTFS_FCB DirectoryFCB
,
357 PUNICODE_STRING Name
,
358 PFILE_RECORD_HEADER Record
,
362 WCHAR pathName
[MAX_PATH
];
363 PFILENAME_ATTRIBUTE FileName
;
366 DPRINT1("NtfsMakeFCBFromDirEntry(%p, %p, %wZ, %p, %p)\n", Vcb
, DirectoryFCB
, Name
, Record
, fileFCB
);
368 FileName
= GetFileNameFromRecord(Record
);
371 return STATUS_OBJECT_NAME_NOT_FOUND
; // Not sure that's the best here
374 if (Name
->Buffer
[0] != 0 && wcslen(DirectoryFCB
->PathName
) +
375 sizeof(WCHAR
) + Name
->Length
/ sizeof(WCHAR
) > MAX_PATH
)
377 return STATUS_OBJECT_NAME_INVALID
;
380 wcscpy(pathName
, DirectoryFCB
->PathName
);
381 if (!NtfsFCBIsRoot(DirectoryFCB
))
383 wcscat(pathName
, L
"\\");
385 wcscat(pathName
, Name
->Buffer
);
387 rcFCB
= NtfsCreateFCB(pathName
, Vcb
);
390 return STATUS_INSUFFICIENT_RESOURCES
;
393 rcFCB
->RFCB
.FileSize
.QuadPart
= FileName
->DataSize
;
394 rcFCB
->RFCB
.ValidDataLength
.QuadPart
= FileName
->DataSize
;
395 rcFCB
->RFCB
.AllocationSize
.QuadPart
= FileName
->AllocatedSize
;
397 NtfsFCBInitializeCache(Vcb
, rcFCB
);
399 rcFCB
->MFTIndex
= MFTIndex
;
400 NtfsAddFCBToTable(Vcb
, rcFCB
);
403 return STATUS_SUCCESS
;
408 NtfsAttachFCBToFileObject(PNTFS_VCB Vcb
,
410 PFILE_OBJECT FileObject
)
414 newCCB
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(NTFS_CCB
), TAG_CCB
);
417 return STATUS_INSUFFICIENT_RESOURCES
;
420 RtlZeroMemory(newCCB
, sizeof(NTFS_CCB
));
422 newCCB
->Identifier
.Type
= NTFS_TYPE_CCB
;
423 newCCB
->Identifier
.Size
= sizeof(NTFS_TYPE_CCB
);
425 FileObject
->SectionObjectPointer
= &Fcb
->SectionObjectPointers
;
426 FileObject
->FsContext
= Fcb
;
427 FileObject
->FsContext2
= newCCB
;
428 newCCB
->PtrFileObject
= FileObject
;
431 if (!(Fcb
->Flags
& FCB_CACHE_INITIALIZED
))
433 CcInitializeCacheMap(FileObject
,
434 (PCC_FILE_SIZES
)(&Fcb
->RFCB
.AllocationSize
),
439 Fcb
->Flags
|= FCB_CACHE_INITIALIZED
;
442 //DPRINT("file open: fcb:%x file size: %d\n", Fcb, Fcb->Entry.DataLengthL);
444 return STATUS_SUCCESS
;
449 NtfsDirFindFile(PNTFS_VCB Vcb
,
450 PNTFS_FCB DirectoryFcb
,
455 ULONGLONG CurrentDir
;
457 PFILE_RECORD_HEADER FileRecord
;
458 PNTFS_ATTR_CONTEXT DataContext
;
461 DPRINT1("NtfsDirFindFile(%p, %p, %S, %p)\n", Vcb
, DirectoryFcb
, FileToFind
, FoundFCB
);
464 RtlInitUnicodeString(&File
, FileToFind
);
465 CurrentDir
= DirectoryFcb
->MFTIndex
;
467 Status
= NtfsLookupFileAt(Vcb
, &File
, &FileRecord
, &DataContext
, &MFTIndex
, CurrentDir
);
468 if (!NT_SUCCESS(Status
))
473 Status
= NtfsMakeFCBFromDirEntry(Vcb
, DirectoryFcb
, &File
, FileRecord
, MFTIndex
, FoundFCB
);
474 ExFreePoolWithTag(FileRecord
, TAG_NTFS
);
481 NtfsGetFCBForFile(PNTFS_VCB Vcb
,
482 PNTFS_FCB
*pParentFCB
,
484 const PWSTR pFileName
)
487 WCHAR pathName
[MAX_PATH
];
488 WCHAR elementName
[MAX_PATH
];
489 PWCHAR currentElement
;
493 DPRINT("NtfsGetFCBForFile(%p, %p, %p, '%S')\n",
500 // FCB = NtfsOpenRootFCB(Vcb);
502 // *pParentFCB = NULL;
505 /* Trivial case, open of the root directory on volume */
506 if (pFileName
[0] == L
'\0' || wcscmp(pFileName
, L
"\\") == 0)
508 DPRINT("returning root FCB\n");
510 FCB
= NtfsOpenRootFCB(Vcb
);
514 return (FCB
!= NULL
) ? STATUS_SUCCESS
: STATUS_OBJECT_PATH_NOT_FOUND
;
518 currentElement
= pFileName
+ 1;
519 wcscpy (pathName
, L
"\\");
520 FCB
= NtfsOpenRootFCB (Vcb
);
525 /* Parse filename and check each path element for existance and access */
526 while (NtfsGetNextPathElement(currentElement
) != 0)
528 /* Skip blank directory levels */
529 if ((NtfsGetNextPathElement(currentElement
) - currentElement
) == 0)
535 DPRINT("Parsing, currentElement:%S\n", currentElement
);
536 DPRINT(" parentFCB:%p FCB:%p\n", parentFCB
, FCB
);
538 /* Descend to next directory level */
541 NtfsReleaseFCB(Vcb
, parentFCB
);
545 /* fail if element in FCB is not a directory */
546 if (!NtfsFCBIsDirectory(FCB
))
548 DPRINT("Element in requested path is not a directory\n");
550 NtfsReleaseFCB(Vcb
, FCB
);
555 return STATUS_OBJECT_PATH_NOT_FOUND
;
560 /* Extract next directory level into dirName */
561 NtfsWSubString(pathName
,
563 NtfsGetNextPathElement(currentElement
) - pFileName
);
564 DPRINT(" pathName:%S\n", pathName
);
566 FCB
= NtfsGrabFCBFromTable(Vcb
, pathName
);
569 NtfsWSubString(elementName
,
571 NtfsGetNextPathElement(currentElement
) - currentElement
);
572 DPRINT(" elementName:%S\n", elementName
);
574 Status
= NtfsDirFindFile(Vcb
, parentFCB
, elementName
, &FCB
);
575 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
577 *pParentFCB
= parentFCB
;
579 currentElement
= NtfsGetNextPathElement(currentElement
);
580 if (*currentElement
== L
'\0' || NtfsGetNextPathElement(currentElement
+ 1) == 0)
582 return STATUS_OBJECT_NAME_NOT_FOUND
;
586 return STATUS_OBJECT_PATH_NOT_FOUND
;
589 else if (!NT_SUCCESS(Status
))
591 NtfsReleaseFCB(Vcb
, parentFCB
);
599 currentElement
= NtfsGetNextPathElement(currentElement
);
602 *pParentFCB
= parentFCB
;
606 return STATUS_SUCCESS
;