e9b0cc4c23f709bb6a0924fce2e69a3465a8f811
[reactos.git] / reactos / drivers / filesystems / ntfs / fcb.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 *
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
24 */
25
26 /* INCLUDES *****************************************************************/
27
28 #include "ntfs.h"
29
30 #define NDEBUG
31 #include <debug.h>
32
33 /* MACROS *******************************************************************/
34
35 #define TAG_FCB 'BCFI'
36
37 /* FUNCTIONS ****************************************************************/
38
39 static
40 PWCHAR
41 NtfsGetNextPathElement(PWCHAR FileName)
42 {
43 if (*FileName == L'\0')
44 {
45 return NULL;
46 }
47
48 while (*FileName != L'\0' && *FileName != L'\\')
49 {
50 FileName++;
51 }
52
53 return FileName;
54 }
55
56
57 static
58 VOID
59 NtfsWSubString(PWCHAR pTarget,
60 const PWCHAR pSource,
61 size_t pLength)
62 {
63 wcsncpy(pTarget, pSource, pLength);
64 pTarget[pLength] = L'\0';
65 }
66
67
68 PNTFS_FCB
69 NtfsCreateFCB(PCWSTR FileName,
70 PNTFS_VCB Vcb)
71 {
72 PNTFS_FCB Fcb;
73
74 ASSERT(Vcb);
75 ASSERT(Vcb->Identifier.Type == NTFS_TYPE_VCB);
76
77 Fcb = ExAllocatePoolWithTag(NonPagedPool, sizeof(NTFS_FCB), TAG_FCB);
78 RtlZeroMemory(Fcb, sizeof(NTFS_FCB));
79
80 Fcb->Identifier.Type = NTFS_TYPE_FCB;
81 Fcb->Identifier.Size = sizeof(NTFS_TYPE_FCB);
82
83 Fcb->Vcb = Vcb;
84
85 if (FileName)
86 {
87 wcscpy(Fcb->PathName, FileName);
88 if (wcsrchr(Fcb->PathName, '\\') != 0)
89 {
90 Fcb->ObjectName = wcsrchr(Fcb->PathName, '\\');
91 }
92 else
93 {
94 Fcb->ObjectName = Fcb->PathName;
95 }
96 }
97
98 ExInitializeResourceLite(&Fcb->MainResource);
99
100 Fcb->RFCB.Resource = &(Fcb->MainResource);
101
102 return Fcb;
103 }
104
105
106 VOID
107 NtfsDestroyFCB(PNTFS_FCB Fcb)
108 {
109 ASSERT(Fcb);
110 ASSERT(Fcb->Identifier.Type == NTFS_TYPE_FCB);
111
112 ExDeleteResourceLite(&Fcb->MainResource);
113
114 ExFreePool(Fcb);
115 }
116
117
118 BOOLEAN
119 NtfsFCBIsDirectory(PNTFS_FCB Fcb)
120 {
121 UNREFERENCED_PARAMETER(Fcb);
122 // return(Fcb->entry.Attrib & FILE_ATTRIBUTE_DIRECTORY);
123 // return(Fcb->Entry.FileFlags & 0x02);
124 return TRUE;
125 }
126
127
128 BOOLEAN
129 NtfsFCBIsRoot(PNTFS_FCB Fcb)
130 {
131 return (wcscmp(Fcb->PathName, L"\\") == 0);
132 }
133
134
135 VOID
136 NtfsGrabFCB(PNTFS_VCB Vcb,
137 PNTFS_FCB Fcb)
138 {
139 KIRQL oldIrql;
140
141 DPRINT("grabbing FCB at %p: %S, refCount:%d\n",
142 Fcb,
143 Fcb->PathName,
144 Fcb->RefCount);
145
146 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
147 Fcb->RefCount++;
148 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
149 }
150
151
152 VOID
153 NtfsReleaseFCB(PNTFS_VCB Vcb,
154 PNTFS_FCB Fcb)
155 {
156 KIRQL oldIrql;
157
158 DPRINT("releasing FCB at %p: %S, refCount:%d\n",
159 Fcb,
160 Fcb->PathName,
161 Fcb->RefCount);
162
163 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
164 Fcb->RefCount--;
165 if (Fcb->RefCount <= 0 && !NtfsFCBIsDirectory(Fcb))
166 {
167 RemoveEntryList(&Fcb->FcbListEntry);
168 CcUninitializeCacheMap(Fcb->FileObject, NULL, NULL);
169 NtfsDestroyFCB(Fcb);
170 }
171
172 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
173 }
174
175
176 VOID
177 NtfsAddFCBToTable(PNTFS_VCB Vcb,
178 PNTFS_FCB Fcb)
179 {
180 KIRQL oldIrql;
181
182 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
183 Fcb->Vcb = Vcb;
184 InsertTailList(&Vcb->FcbListHead, &Fcb->FcbListEntry);
185 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
186 }
187
188
189 PNTFS_FCB
190 NtfsGrabFCBFromTable(PNTFS_VCB Vcb,
191 PCWSTR FileName)
192 {
193 KIRQL oldIrql;
194 PNTFS_FCB Fcb;
195 PLIST_ENTRY current_entry;
196
197 KeAcquireSpinLock(&Vcb->FcbListLock, &oldIrql);
198
199 if (FileName == NULL || *FileName == 0)
200 {
201 DPRINT("Return FCB for stream file object\n");
202 Fcb = Vcb->StreamFileObject->FsContext;
203 Fcb->RefCount++;
204 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
205 return Fcb;
206 }
207
208 current_entry = Vcb->FcbListHead.Flink;
209 while (current_entry != &Vcb->FcbListHead)
210 {
211 Fcb = CONTAINING_RECORD(current_entry, NTFS_FCB, FcbListEntry);
212
213 DPRINT("Comparing '%S' and '%S'\n", FileName, Fcb->PathName);
214 if (_wcsicmp(FileName, Fcb->PathName) == 0)
215 {
216 Fcb->RefCount++;
217 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
218 return Fcb;
219 }
220
221 //FIXME: need to compare against short name in FCB here
222
223 current_entry = current_entry->Flink;
224 }
225
226 KeReleaseSpinLock(&Vcb->FcbListLock, oldIrql);
227
228 return NULL;
229 }
230
231
232 NTSTATUS
233 NtfsFCBInitializeCache(PNTFS_VCB Vcb,
234 PNTFS_FCB Fcb)
235 {
236 PFILE_OBJECT FileObject;
237 NTSTATUS Status;
238 PNTFS_CCB newCCB;
239
240 FileObject = IoCreateStreamFileObject(NULL, Vcb->StorageDevice);
241
242 newCCB = ExAllocatePoolWithTag(NonPagedPool, sizeof(NTFS_CCB), TAG_CCB);
243 if (newCCB == NULL)
244 {
245 return STATUS_INSUFFICIENT_RESOURCES;
246 }
247
248 RtlZeroMemory(newCCB, sizeof(NTFS_CCB));
249
250 newCCB->Identifier.Type = NTFS_TYPE_CCB;
251 newCCB->Identifier.Size = sizeof(NTFS_TYPE_CCB);
252
253 FileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
254 FileObject->FsContext = Fcb;
255 FileObject->FsContext2 = newCCB;
256 newCCB->PtrFileObject = FileObject;
257 Fcb->FileObject = FileObject;
258 Fcb->Vcb = Vcb;
259
260 Status = STATUS_SUCCESS;
261 CcInitializeCacheMap(FileObject,
262 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
263 FALSE,
264 &(NtfsGlobalData->CacheMgrCallbacks),
265 Fcb);
266
267 ObDereferenceObject(FileObject);
268 Fcb->Flags |= FCB_CACHE_INITIALIZED;
269
270 return Status;
271 }
272
273
274 PNTFS_FCB
275 NtfsMakeRootFCB(PNTFS_VCB Vcb)
276 {
277 PNTFS_FCB Fcb;
278 PFILE_RECORD_HEADER MftRecord;
279 PFILENAME_ATTRIBUTE FileName;
280
281 MftRecord = ExAllocatePoolWithTag(NonPagedPool,
282 Vcb->NtfsInfo.BytesPerFileRecord,
283 TAG_NTFS);
284 if (MftRecord == NULL)
285 {
286 return NULL;
287 }
288
289 if (!NT_SUCCESS(ReadFileRecord(Vcb, NTFS_FILE_ROOT, MftRecord)))
290 {
291 return NULL;
292 }
293
294 FileName = GetFileNameFromRecord(MftRecord);
295 if (!FileName)
296 {
297 return NULL;
298 }
299
300 Fcb = NtfsCreateFCB(L"\\", Vcb);
301 if (!Fcb)
302 {
303 return NULL;
304 }
305
306 memcpy(&Fcb->Entry, FileName, FIELD_OFFSET(FILENAME_ATTRIBUTE, NameLength));
307 Fcb->Entry.NameType = FileName->NameType;
308 Fcb->Entry.NameLength = 0;
309 Fcb->Entry.Name[0] = UNICODE_NULL;
310 Fcb->RefCount = 1;
311 Fcb->DirIndex = 0;
312 Fcb->RFCB.FileSize.QuadPart = FileName->DataSize;
313 Fcb->RFCB.ValidDataLength.QuadPart = FileName->DataSize;
314 Fcb->RFCB.AllocationSize.QuadPart = FileName->AllocatedSize;
315 Fcb->MFTIndex = NTFS_FILE_ROOT;
316
317 NtfsFCBInitializeCache(Vcb, Fcb);
318 NtfsAddFCBToTable(Vcb, Fcb);
319 NtfsGrabFCB(Vcb, Fcb);
320
321 return Fcb;
322 }
323
324
325 PNTFS_FCB
326 NtfsOpenRootFCB(PNTFS_VCB Vcb)
327 {
328 PNTFS_FCB Fcb;
329
330 Fcb = NtfsGrabFCBFromTable(Vcb, L"\\");
331 if (Fcb == NULL)
332 {
333 Fcb = NtfsMakeRootFCB(Vcb);
334 }
335
336 return Fcb;
337 }
338
339
340 #if 0
341 static VOID
342 NtfsGetDirEntryName(PDEVICE_EXTENSION DeviceExt,
343 PDIR_RECORD Record,
344 PWSTR Name)
345 /*
346 * FUNCTION: Retrieves the file name, be it in short or long file name format
347 */
348 {
349 if (Record->FileIdLength == 1 && Record->FileId[0] == 0)
350 {
351 wcscpy(Name, L".");
352 }
353 else if (Record->FileIdLength == 1 && Record->FileId[0] == 1)
354 {
355 wcscpy(Name, L"..");
356 }
357 else
358 {
359 if (DeviceExt->CdInfo.JolietLevel == 0)
360 {
361 ULONG i;
362
363 for (i = 0; i < Record->FileIdLength && Record->FileId[i] != ';'; i++)
364 Name[i] = (WCHAR)Record->FileId[i];
365 Name[i] = 0;
366 }
367 else
368 {
369 NtfsSwapString(Name, Record->FileId, Record->FileIdLength);
370 }
371 }
372
373 DPRINT("Name '%S'\n", Name);
374 }
375 #endif
376
377
378 NTSTATUS
379 NtfsMakeFCBFromDirEntry(PNTFS_VCB Vcb,
380 PNTFS_FCB DirectoryFCB,
381 PUNICODE_STRING Name,
382 PFILE_RECORD_HEADER Record,
383 ULONGLONG MFTIndex,
384 PNTFS_FCB * fileFCB)
385 {
386 WCHAR pathName[MAX_PATH];
387 PFILENAME_ATTRIBUTE FileName;
388 PNTFS_FCB rcFCB;
389
390 DPRINT1("NtfsMakeFCBFromDirEntry(%p, %p, %wZ, %p, %p)\n", Vcb, DirectoryFCB, Name, Record, fileFCB);
391
392 FileName = GetFileNameFromRecord(Record);
393 if (!FileName)
394 {
395 return STATUS_OBJECT_NAME_NOT_FOUND; // Not sure that's the best here
396 }
397
398 if (Name->Buffer[0] != 0 && wcslen(DirectoryFCB->PathName) +
399 sizeof(WCHAR) + Name->Length / sizeof(WCHAR) > MAX_PATH)
400 {
401 return STATUS_OBJECT_NAME_INVALID;
402 }
403
404 wcscpy(pathName, DirectoryFCB->PathName);
405 if (!NtfsFCBIsRoot(DirectoryFCB))
406 {
407 wcscat(pathName, L"\\");
408 }
409 wcscat(pathName, Name->Buffer);
410
411 rcFCB = NtfsCreateFCB(pathName, Vcb);
412 if (!rcFCB)
413 {
414 return STATUS_INSUFFICIENT_RESOURCES;
415 }
416
417 memcpy(&rcFCB->Entry, FileName, FIELD_OFFSET(FILENAME_ATTRIBUTE, NameLength));
418 rcFCB->Entry.NameType = FileName->NameType;
419 rcFCB->RFCB.FileSize.QuadPart = FileName->DataSize;
420 rcFCB->RFCB.ValidDataLength.QuadPart = FileName->DataSize;
421 rcFCB->RFCB.AllocationSize.QuadPart = FileName->AllocatedSize;
422
423 NtfsFCBInitializeCache(Vcb, rcFCB);
424 rcFCB->RefCount = 1;
425 rcFCB->MFTIndex = MFTIndex;
426 NtfsAddFCBToTable(Vcb, rcFCB);
427 *fileFCB = rcFCB;
428
429 return STATUS_SUCCESS;
430 }
431
432
433 NTSTATUS
434 NtfsAttachFCBToFileObject(PNTFS_VCB Vcb,
435 PNTFS_FCB Fcb,
436 PFILE_OBJECT FileObject)
437 {
438 PNTFS_CCB newCCB;
439
440 newCCB = ExAllocatePoolWithTag(NonPagedPool, sizeof(NTFS_CCB), TAG_CCB);
441 if (newCCB == NULL)
442 {
443 return STATUS_INSUFFICIENT_RESOURCES;
444 }
445
446 RtlZeroMemory(newCCB, sizeof(NTFS_CCB));
447
448 newCCB->Identifier.Type = NTFS_TYPE_CCB;
449 newCCB->Identifier.Size = sizeof(NTFS_TYPE_CCB);
450
451 FileObject->SectionObjectPointer = &Fcb->SectionObjectPointers;
452 FileObject->FsContext = Fcb;
453 FileObject->FsContext2 = newCCB;
454 newCCB->PtrFileObject = FileObject;
455 Fcb->Vcb = Vcb;
456
457 if (!(Fcb->Flags & FCB_CACHE_INITIALIZED))
458 {
459 CcInitializeCacheMap(FileObject,
460 (PCC_FILE_SIZES)(&Fcb->RFCB.AllocationSize),
461 FALSE,
462 NULL,
463 NULL);
464
465 Fcb->Flags |= FCB_CACHE_INITIALIZED;
466 }
467
468 //DPRINT("file open: fcb:%x file size: %d\n", Fcb, Fcb->Entry.DataLengthL);
469
470 return STATUS_SUCCESS;
471 }
472
473
474 static NTSTATUS
475 NtfsDirFindFile(PNTFS_VCB Vcb,
476 PNTFS_FCB DirectoryFcb,
477 PWSTR FileToFind,
478 PNTFS_FCB *FoundFCB)
479 {
480 NTSTATUS Status;
481 ULONGLONG CurrentDir;
482 UNICODE_STRING File;
483 PFILE_RECORD_HEADER FileRecord;
484 PNTFS_ATTR_CONTEXT DataContext;
485 ULONGLONG MFTIndex;
486
487 DPRINT1("NtfsDirFindFile(%p, %p, %S, %p)\n", Vcb, DirectoryFcb, FileToFind, FoundFCB);
488
489 *FoundFCB = NULL;
490 RtlInitUnicodeString(&File, FileToFind);
491 CurrentDir = DirectoryFcb->MFTIndex;
492
493 Status = NtfsLookupFileAt(Vcb, &File, &FileRecord, &DataContext, &MFTIndex, CurrentDir);
494 if (!NT_SUCCESS(Status))
495 {
496 return Status;
497 }
498
499 Status = NtfsMakeFCBFromDirEntry(Vcb, DirectoryFcb, &File, FileRecord, MFTIndex, FoundFCB);
500 ExFreePoolWithTag(FileRecord, TAG_NTFS);
501
502 return Status;
503 }
504
505
506 NTSTATUS
507 NtfsGetFCBForFile(PNTFS_VCB Vcb,
508 PNTFS_FCB *pParentFCB,
509 PNTFS_FCB *pFCB,
510 const PWSTR pFileName)
511 {
512 NTSTATUS Status;
513 WCHAR pathName [MAX_PATH];
514 WCHAR elementName [MAX_PATH];
515 PWCHAR currentElement;
516 PNTFS_FCB FCB;
517 PNTFS_FCB parentFCB;
518
519 DPRINT("NtfsGetFCBForFile(%p, %p, %p, '%S')\n",
520 Vcb,
521 pParentFCB,
522 pFCB,
523 pFileName);
524
525 /* Dummy code */
526 // FCB = NtfsOpenRootFCB(Vcb);
527 // *pFCB = FCB;
528 // *pParentFCB = NULL;
529
530 #if 1
531 /* Trivial case, open of the root directory on volume */
532 if (pFileName [0] == L'\0' || wcscmp(pFileName, L"\\") == 0)
533 {
534 DPRINT("returning root FCB\n");
535
536 FCB = NtfsOpenRootFCB(Vcb);
537 *pFCB = FCB;
538 *pParentFCB = NULL;
539
540 return (FCB != NULL) ? STATUS_SUCCESS : STATUS_OBJECT_PATH_NOT_FOUND;
541 }
542 else
543 {
544 currentElement = pFileName + 1;
545 wcscpy (pathName, L"\\");
546 FCB = NtfsOpenRootFCB (Vcb);
547 }
548
549 parentFCB = NULL;
550
551 /* Parse filename and check each path element for existance and access */
552 while (NtfsGetNextPathElement(currentElement) != 0)
553 {
554 /* Skip blank directory levels */
555 if ((NtfsGetNextPathElement(currentElement) - currentElement) == 0)
556 {
557 currentElement++;
558 continue;
559 }
560
561 DPRINT("Parsing, currentElement:%S\n", currentElement);
562 DPRINT(" parentFCB:%p FCB:%p\n", parentFCB, FCB);
563
564 /* Descend to next directory level */
565 if (parentFCB)
566 {
567 NtfsReleaseFCB(Vcb, parentFCB);
568 parentFCB = NULL;
569 }
570
571 /* fail if element in FCB is not a directory */
572 if (!NtfsFCBIsDirectory(FCB))
573 {
574 DPRINT("Element in requested path is not a directory\n");
575
576 NtfsReleaseFCB(Vcb, FCB);
577 FCB = 0;
578 *pParentFCB = NULL;
579 *pFCB = NULL;
580
581 return STATUS_OBJECT_PATH_NOT_FOUND;
582 }
583
584 parentFCB = FCB;
585
586 /* Extract next directory level into dirName */
587 NtfsWSubString(pathName,
588 pFileName,
589 NtfsGetNextPathElement(currentElement) - pFileName);
590 DPRINT(" pathName:%S\n", pathName);
591
592 FCB = NtfsGrabFCBFromTable(Vcb, pathName);
593 if (FCB == NULL)
594 {
595 NtfsWSubString(elementName,
596 currentElement,
597 NtfsGetNextPathElement(currentElement) - currentElement);
598 DPRINT(" elementName:%S\n", elementName);
599
600 Status = NtfsDirFindFile(Vcb, parentFCB, elementName, &FCB);
601 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
602 {
603 *pParentFCB = parentFCB;
604 *pFCB = NULL;
605 currentElement = NtfsGetNextPathElement(currentElement);
606 if (*currentElement == L'\0' || NtfsGetNextPathElement(currentElement + 1) == 0)
607 {
608 return STATUS_OBJECT_NAME_NOT_FOUND;
609 }
610 else
611 {
612 return STATUS_OBJECT_PATH_NOT_FOUND;
613 }
614 }
615 else if (!NT_SUCCESS(Status))
616 {
617 NtfsReleaseFCB(Vcb, parentFCB);
618 *pParentFCB = NULL;
619 *pFCB = NULL;
620
621 return Status;
622 }
623 }
624
625 currentElement = NtfsGetNextPathElement(currentElement);
626 }
627
628 *pParentFCB = parentFCB;
629 *pFCB = FCB;
630 #endif
631
632 return STATUS_SUCCESS;
633 }
634
635 /* EOF */