ad17fa6aac2199776c87520294863da4b24d13a7
[reactos.git] / reactos / drivers / filesystems / fastfat / fcb.c
1 /*
2 * FILE: drivers/filesystems/fastfat/fcb.c
3 * PURPOSE: Routines to manipulate FCBs.
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS kernel
6 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
7 * Rex Jolliff (rex@lvcablemodem.com)
8 * Herve Poussineau (reactos@poussine.freesurf.fr)
9 * Pierre Schweitzer (pierre@reactos.org)
10 */
11
12 /* ------------------------------------------------------- INCLUDES */
13
14 #include "vfat.h"
15
16 #define NDEBUG
17 #include <debug.h>
18
19 #ifdef __GNUC__
20 #include <wctype.h> /* towlower prototype */
21 #endif
22
23 /* -------------------------------------------------------- DEFINES */
24
25 #define TAG_FCB 'BCFV'
26
27 /* -------------------------------------------------------- PUBLICS */
28
29 static
30 ULONG
31 vfatNameHash(
32 ULONG hash,
33 PUNICODE_STRING NameU)
34 {
35 PWCHAR last;
36 PWCHAR curr;
37 register WCHAR c;
38
39 // LFN could start from "."
40 //ASSERT(NameU->Buffer[0] != L'.');
41 curr = NameU->Buffer;
42 last = NameU->Buffer + NameU->Length / sizeof(WCHAR);
43
44 while(curr < last)
45 {
46 c = towlower(*curr++);
47 hash = (hash + (c << 4) + (c >> 4)) * 11;
48 }
49 return hash;
50 }
51
52 VOID
53 vfatSplitPathName(
54 PUNICODE_STRING PathNameU,
55 PUNICODE_STRING DirNameU,
56 PUNICODE_STRING FileNameU)
57 {
58 PWCHAR pName;
59 USHORT Length = 0;
60 pName = PathNameU->Buffer + PathNameU->Length / sizeof(WCHAR) - 1;
61 while (*pName != L'\\' && pName >= PathNameU->Buffer)
62 {
63 pName--;
64 Length++;
65 }
66 ASSERT(*pName == L'\\' || pName < PathNameU->Buffer);
67 if (FileNameU)
68 {
69 FileNameU->Buffer = pName + 1;
70 FileNameU->Length = FileNameU->MaximumLength = Length * sizeof(WCHAR);
71 }
72 if (DirNameU)
73 {
74 DirNameU->Buffer = PathNameU->Buffer;
75 DirNameU->Length = (pName + 1 - PathNameU->Buffer) * sizeof(WCHAR);
76 DirNameU->MaximumLength = DirNameU->Length;
77 }
78 }
79
80 static
81 VOID
82 vfatInitFcb(
83 PVFATFCB Fcb,
84 PUNICODE_STRING NameU)
85 {
86 USHORT PathNameBufferLength;
87
88 if (NameU)
89 PathNameBufferLength = NameU->Length + sizeof(WCHAR);
90 else
91 PathNameBufferLength = 0;
92
93 Fcb->PathNameBuffer = ExAllocatePoolWithTag(NonPagedPool, PathNameBufferLength, TAG_FCB);
94 if (!Fcb->PathNameBuffer)
95 {
96 /* FIXME: what to do if no more memory? */
97 DPRINT1("Unable to initialize FCB for filename '%wZ'\n", NameU);
98 KeBugCheckEx(FAT_FILE_SYSTEM, (ULONG_PTR)Fcb, (ULONG_PTR)NameU, 0, 0);
99 }
100
101 Fcb->PathNameU.Length = 0;
102 Fcb->PathNameU.Buffer = Fcb->PathNameBuffer;
103 Fcb->PathNameU.MaximumLength = PathNameBufferLength;
104 Fcb->ShortNameU.Length = 0;
105 Fcb->ShortNameU.Buffer = Fcb->ShortNameBuffer;
106 Fcb->ShortNameU.MaximumLength = sizeof(Fcb->ShortNameBuffer);
107 Fcb->DirNameU.Buffer = Fcb->PathNameU.Buffer;
108 if (NameU && NameU->Length)
109 {
110 RtlCopyUnicodeString(&Fcb->PathNameU, NameU);
111 vfatSplitPathName(&Fcb->PathNameU, &Fcb->DirNameU, &Fcb->LongNameU);
112 }
113 else
114 {
115 Fcb->DirNameU.Buffer = Fcb->LongNameU.Buffer = NULL;
116 Fcb->DirNameU.MaximumLength = Fcb->DirNameU.Length = 0;
117 Fcb->LongNameU.MaximumLength = Fcb->LongNameU.Length = 0;
118 }
119 RtlZeroMemory(&Fcb->FCBShareAccess, sizeof(SHARE_ACCESS));
120 Fcb->OpenHandleCount = 0;
121 }
122
123 PVFATFCB
124 vfatNewFCB(
125 PDEVICE_EXTENSION pVCB,
126 PUNICODE_STRING pFileNameU)
127 {
128 PVFATFCB rcFCB;
129
130 DPRINT("'%wZ'\n", pFileNameU);
131
132 rcFCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->FcbLookasideList);
133 if (rcFCB == NULL)
134 {
135 return NULL;
136 }
137 RtlZeroMemory(rcFCB, sizeof(VFATFCB));
138 vfatInitFcb(rcFCB, pFileNameU);
139 if (pVCB->Flags & VCB_IS_FATX)
140 {
141 rcFCB->Flags |= FCB_IS_FATX_ENTRY;
142 rcFCB->Attributes = &rcFCB->entry.FatX.Attrib;
143 }
144 else
145 rcFCB->Attributes = &rcFCB->entry.Fat.Attrib;
146 rcFCB->Hash.Hash = vfatNameHash(0, &rcFCB->PathNameU);
147 rcFCB->Hash.self = rcFCB;
148 rcFCB->ShortHash.self = rcFCB;
149 ExInitializeResourceLite(&rcFCB->PagingIoResource);
150 ExInitializeResourceLite(&rcFCB->MainResource);
151 FsRtlInitializeFileLock(&rcFCB->FileLock, NULL, NULL);
152 ExInitializeFastMutex(&rcFCB->LastMutex);
153 rcFCB->RFCB.PagingIoResource = &rcFCB->PagingIoResource;
154 rcFCB->RFCB.Resource = &rcFCB->MainResource;
155 rcFCB->RFCB.IsFastIoPossible = FastIoIsNotPossible;
156
157 return rcFCB;
158 }
159
160 static
161 VOID
162 vfatDelFCBFromTable(
163 PDEVICE_EXTENSION pVCB,
164 PVFATFCB pFCB)
165 {
166 ULONG Index;
167 ULONG ShortIndex;
168 HASHENTRY* entry;
169
170 Index = pFCB->Hash.Hash % pVCB->HashTableSize;
171 ShortIndex = pFCB->ShortHash.Hash % pVCB->HashTableSize;
172
173 if (pFCB->Hash.Hash != pFCB->ShortHash.Hash)
174 {
175 entry = pVCB->FcbHashTable[ShortIndex];
176 if (entry->self == pFCB)
177 {
178 pVCB->FcbHashTable[ShortIndex] = entry->next;
179 }
180 else
181 {
182 while (entry->next->self != pFCB)
183 {
184 entry = entry->next;
185 }
186 entry->next = pFCB->ShortHash.next;
187 }
188 }
189 entry = pVCB->FcbHashTable[Index];
190 if (entry->self == pFCB)
191 {
192 pVCB->FcbHashTable[Index] = entry->next;
193 }
194 else
195 {
196 while (entry->next->self != pFCB)
197 {
198 entry = entry->next;
199 }
200 entry->next = pFCB->Hash.next;
201 }
202
203 RemoveEntryList(&pFCB->FcbListEntry);
204 }
205
206 static
207 NTSTATUS
208 vfatMakeFullName(
209 PVFATFCB directoryFCB,
210 PUNICODE_STRING LongNameU,
211 PUNICODE_STRING ShortNameU,
212 PUNICODE_STRING NameU)
213 {
214 PWCHAR PathNameBuffer;
215 USHORT PathNameLength;
216
217 PathNameLength = directoryFCB->PathNameU.Length + max(LongNameU->Length, ShortNameU->Length);
218 if (!vfatFCBIsRoot(directoryFCB))
219 {
220 PathNameLength += sizeof(WCHAR);
221 }
222
223 if (PathNameLength > LONGNAME_MAX_LENGTH * sizeof(WCHAR))
224 {
225 return STATUS_OBJECT_NAME_INVALID;
226 }
227 PathNameBuffer = ExAllocatePoolWithTag(NonPagedPool, PathNameLength + sizeof(WCHAR), TAG_FCB);
228 if (!PathNameBuffer)
229 {
230 return STATUS_INSUFFICIENT_RESOURCES;
231 }
232 NameU->Buffer = PathNameBuffer;
233 NameU->Length = 0;
234 NameU->MaximumLength = PathNameLength;
235
236 RtlCopyUnicodeString(NameU, &directoryFCB->PathNameU);
237 if (!vfatFCBIsRoot(directoryFCB))
238 {
239 RtlAppendUnicodeToString(NameU, L"\\");
240 }
241 if (LongNameU->Length > 0)
242 {
243 RtlAppendUnicodeStringToString(NameU, LongNameU);
244 }
245 else
246 {
247 RtlAppendUnicodeStringToString(NameU, ShortNameU);
248 }
249 NameU->Buffer[NameU->Length / sizeof(WCHAR)] = 0;
250
251 return STATUS_SUCCESS;
252 }
253
254 VOID
255 vfatDestroyCCB(
256 PVFATCCB pCcb)
257 {
258 if (pCcb->SearchPattern.Buffer)
259 {
260 ExFreePoolWithTag(pCcb->SearchPattern.Buffer, TAG_VFAT);
261 }
262 ExFreeToNPagedLookasideList(&VfatGlobalData->CcbLookasideList, pCcb);
263 }
264
265 VOID
266 vfatDestroyFCB(
267 PVFATFCB pFCB)
268 {
269 FsRtlUninitializeFileLock(&pFCB->FileLock);
270 ExFreePool(pFCB->PathNameBuffer);
271 ExDeleteResourceLite(&pFCB->PagingIoResource);
272 ExDeleteResourceLite(&pFCB->MainResource);
273 ExFreeToNPagedLookasideList(&VfatGlobalData->FcbLookasideList, pFCB);
274 }
275
276 BOOLEAN
277 vfatFCBIsDirectory(
278 PVFATFCB FCB)
279 {
280 return ((*FCB->Attributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
281 }
282
283 BOOLEAN
284 vfatFCBIsRoot(
285 PVFATFCB FCB)
286 {
287 return FCB->PathNameU.Length == sizeof(WCHAR) && FCB->PathNameU.Buffer[0] == L'\\' ? TRUE : FALSE;
288 }
289
290 VOID
291 vfatGrabFCB(
292 PDEVICE_EXTENSION pVCB,
293 PVFATFCB pFCB)
294 {
295 ASSERT(ExIsResourceAcquiredExclusive(&pVCB->DirResource));
296
297 ASSERT(pFCB != pVCB->VolumeFcb);
298 ASSERT(pFCB->RefCount > 0);
299 ++pFCB->RefCount;
300 }
301
302 VOID
303 vfatReleaseFCB(
304 PDEVICE_EXTENSION pVCB,
305 PVFATFCB pFCB)
306 {
307 PVFATFCB tmpFcb;
308
309 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
310 pFCB, &pFCB->PathNameU, pFCB->RefCount);
311
312 ASSERT(ExIsResourceAcquiredExclusive(&pVCB->DirResource));
313
314 while (pFCB)
315 {
316 ASSERT(pFCB != pVCB->VolumeFcb);
317 ASSERT(pFCB->RefCount > 0);
318 pFCB->RefCount--;
319 if (pFCB->RefCount == 0)
320 {
321 ASSERT(pFCB->OpenHandleCount == 0);
322 tmpFcb = pFCB->parentFcb;
323 vfatDelFCBFromTable(pVCB, pFCB);
324 vfatDestroyFCB(pFCB);
325 }
326 else
327 {
328 tmpFcb = NULL;
329 }
330 pFCB = tmpFcb;
331 }
332 }
333
334 static
335 VOID
336 vfatAddFCBToTable(
337 PDEVICE_EXTENSION pVCB,
338 PVFATFCB pFCB)
339 {
340 ULONG Index;
341 ULONG ShortIndex;
342
343 Index = pFCB->Hash.Hash % pVCB->HashTableSize;
344 ShortIndex = pFCB->ShortHash.Hash % pVCB->HashTableSize;
345
346 InsertTailList(&pVCB->FcbListHead, &pFCB->FcbListEntry);
347
348 pFCB->Hash.next = pVCB->FcbHashTable[Index];
349 pVCB->FcbHashTable[Index] = &pFCB->Hash;
350 if (pFCB->Hash.Hash != pFCB->ShortHash.Hash)
351 {
352 pFCB->ShortHash.next = pVCB->FcbHashTable[ShortIndex];
353 pVCB->FcbHashTable[ShortIndex] = &pFCB->ShortHash;
354 }
355 if (pFCB->parentFcb)
356 {
357 vfatGrabFCB(pVCB, pFCB->parentFcb);
358 }
359 }
360
361 static
362 VOID
363 vfatInitFCBFromDirEntry(
364 PDEVICE_EXTENSION Vcb,
365 PVFATFCB Fcb,
366 PVFAT_DIRENTRY_CONTEXT DirContext)
367 {
368 ULONG Size;
369
370 RtlCopyMemory(&Fcb->entry, &DirContext->DirEntry, sizeof (DIR_ENTRY));
371 RtlCopyUnicodeString(&Fcb->ShortNameU, &DirContext->ShortNameU);
372 if (Vcb->Flags & VCB_IS_FATX)
373 {
374 Fcb->ShortHash.Hash = Fcb->Hash.Hash;
375 }
376 else
377 {
378 Fcb->ShortHash.Hash = vfatNameHash(0, &Fcb->DirNameU);
379 Fcb->ShortHash.Hash = vfatNameHash(Fcb->ShortHash.Hash, &Fcb->ShortNameU);
380 }
381
382 if (vfatFCBIsDirectory(Fcb))
383 {
384 ULONG FirstCluster, CurrentCluster;
385 NTSTATUS Status = STATUS_SUCCESS;
386 Size = 0;
387 FirstCluster = vfatDirEntryGetFirstCluster(Vcb, &Fcb->entry);
388 if (FirstCluster == 1)
389 {
390 Size = Vcb->FatInfo.rootDirectorySectors * Vcb->FatInfo.BytesPerSector;
391 }
392 else if (FirstCluster != 0)
393 {
394 CurrentCluster = FirstCluster;
395 while (CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
396 {
397 Size += Vcb->FatInfo.BytesPerCluster;
398 Status = NextCluster(Vcb, FirstCluster, &CurrentCluster, FALSE);
399 }
400 }
401 }
402 else if (Fcb->Flags & FCB_IS_FATX_ENTRY)
403 {
404 Size = Fcb->entry.FatX.FileSize;
405 }
406 else
407 {
408 Size = Fcb->entry.Fat.FileSize;
409 }
410 Fcb->dirIndex = DirContext->DirIndex;
411 Fcb->startIndex = DirContext->StartIndex;
412 if ((Fcb->Flags & FCB_IS_FATX_ENTRY) && !vfatFCBIsRoot(Fcb))
413 {
414 ASSERT(DirContext->DirIndex >= 2 && DirContext->StartIndex >= 2);
415 Fcb->dirIndex = DirContext->DirIndex-2;
416 Fcb->startIndex = DirContext->StartIndex-2;
417 }
418 Fcb->RFCB.FileSize.QuadPart = Size;
419 Fcb->RFCB.ValidDataLength.QuadPart = Size;
420 Fcb->RFCB.AllocationSize.QuadPart = ROUND_UP_64(Size, Vcb->FatInfo.BytesPerCluster);
421 }
422
423 NTSTATUS
424 vfatUpdateFCB(
425 PDEVICE_EXTENSION pVCB,
426 PVFATFCB Fcb,
427 PVFAT_DIRENTRY_CONTEXT DirContext,
428 PVFATFCB ParentFcb)
429 {
430 NTSTATUS Status;
431 PVFATFCB OldParent;
432
433 DPRINT("vfatUpdateFCB(%p, %p, %p, %p)\n", pVCB, Fcb, DirContext, ParentFcb);
434
435 /* Get full path name */
436 Status = vfatMakeFullName(ParentFcb, &DirContext->LongNameU, &DirContext->ShortNameU, &Fcb->PathNameU);
437 if (!NT_SUCCESS(Status))
438 {
439 return Status;
440 }
441
442 /* Delete old name */
443 if (Fcb->PathNameBuffer)
444 {
445 ExFreePoolWithTag(Fcb->PathNameBuffer, TAG_FCB);
446 }
447
448 /* Delete from table */
449 vfatDelFCBFromTable(pVCB, Fcb);
450
451 /* Split it properly */
452 Fcb->PathNameBuffer = Fcb->PathNameU.Buffer;
453 Fcb->DirNameU.Buffer = Fcb->PathNameU.Buffer;
454 vfatSplitPathName(&Fcb->PathNameU, &Fcb->DirNameU, &Fcb->LongNameU);
455
456 /* Save old parent */
457 OldParent = Fcb->parentFcb;
458
459 /* Reinit FCB */
460 vfatInitFCBFromDirEntry(pVCB, Fcb, DirContext);
461
462 if (vfatFCBIsDirectory(Fcb))
463 {
464 CcFlushCache(&Fcb->SectionObjectPointers, NULL, 0, NULL);
465 }
466 Fcb->parentFcb = ParentFcb;
467 vfatAddFCBToTable(pVCB, Fcb);
468
469 /* If we moved across directories, dereference our old parent
470 * We also dereference in case we're just renaming since AddFCBToTable references it
471 */
472 vfatReleaseFCB(pVCB, OldParent);
473
474 return STATUS_SUCCESS;
475 }
476
477 PVFATFCB
478 vfatGrabFCBFromTable(
479 PDEVICE_EXTENSION pVCB,
480 PUNICODE_STRING PathNameU)
481 {
482 PVFATFCB rcFCB;
483 ULONG Hash;
484 UNICODE_STRING DirNameU;
485 UNICODE_STRING FileNameU;
486 PUNICODE_STRING FcbNameU;
487
488 HASHENTRY* entry;
489
490 DPRINT("'%wZ'\n", PathNameU);
491
492 ASSERT(PathNameU->Length >= sizeof(WCHAR) && PathNameU->Buffer[0] == L'\\');
493 Hash = vfatNameHash(0, PathNameU);
494
495 entry = pVCB->FcbHashTable[Hash % pVCB->HashTableSize];
496 if (entry)
497 {
498 vfatSplitPathName(PathNameU, &DirNameU, &FileNameU);
499 }
500
501 while (entry)
502 {
503 if (entry->Hash == Hash)
504 {
505 rcFCB = entry->self;
506 DPRINT("'%wZ' '%wZ'\n", &DirNameU, &rcFCB->DirNameU);
507 if (RtlEqualUnicodeString(&DirNameU, &rcFCB->DirNameU, TRUE))
508 {
509 if (rcFCB->Hash.Hash == Hash)
510 {
511 FcbNameU = &rcFCB->LongNameU;
512 }
513 else
514 {
515 FcbNameU = &rcFCB->ShortNameU;
516 }
517 /* compare the file name */
518 DPRINT("'%wZ' '%wZ'\n", &FileNameU, FcbNameU);
519 if (RtlEqualUnicodeString(&FileNameU, FcbNameU, TRUE))
520 {
521 vfatGrabFCB(pVCB, rcFCB);
522 return rcFCB;
523 }
524 }
525 }
526 entry = entry->next;
527 }
528 return NULL;
529 }
530
531 static
532 NTSTATUS
533 vfatFCBInitializeCacheFromVolume(
534 PVCB vcb,
535 PVFATFCB fcb)
536 {
537 PFILE_OBJECT fileObject;
538 PVFATCCB newCCB;
539 NTSTATUS status;
540
541 fileObject = IoCreateStreamFileObject (NULL, vcb->StorageDevice);
542
543 newCCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
544 if (newCCB == NULL)
545 {
546 ObDereferenceObject(fileObject);
547 return STATUS_INSUFFICIENT_RESOURCES;
548 }
549 RtlZeroMemory(newCCB, sizeof (VFATCCB));
550
551 fileObject->SectionObjectPointer = &fcb->SectionObjectPointers;
552 fileObject->FsContext = fcb;
553 fileObject->FsContext2 = newCCB;
554 fcb->FileObject = fileObject;
555
556 _SEH2_TRY
557 {
558 CcInitializeCacheMap(fileObject,
559 (PCC_FILE_SIZES)(&fcb->RFCB.AllocationSize),
560 TRUE,
561 &VfatGlobalData->CacheMgrCallbacks,
562 fcb);
563 }
564 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
565 {
566 status = _SEH2_GetExceptionCode();
567 fcb->FileObject = NULL;
568 ExFreeToNPagedLookasideList(&VfatGlobalData->CcbLookasideList, newCCB);
569 ObDereferenceObject(fileObject);
570 return status;
571 }
572 _SEH2_END;
573
574 vfatGrabFCB(vcb, fcb);
575 fcb->Flags |= FCB_CACHE_INITIALIZED;
576 return STATUS_SUCCESS;
577 }
578
579 PVFATFCB
580 vfatMakeRootFCB(
581 PDEVICE_EXTENSION pVCB)
582 {
583 PVFATFCB FCB;
584 ULONG FirstCluster, CurrentCluster, Size = 0;
585 NTSTATUS Status = STATUS_SUCCESS;
586 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\");
587
588 FCB = vfatNewFCB(pVCB, &NameU);
589 if (FCB->Flags & FCB_IS_FATX_ENTRY)
590 {
591 memset(FCB->entry.FatX.Filename, ' ', 42);
592 FCB->entry.FatX.FileSize = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
593 FCB->entry.FatX.Attrib = FILE_ATTRIBUTE_DIRECTORY;
594 FCB->entry.FatX.FirstCluster = 1;
595 Size = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
596 }
597 else
598 {
599 memset(FCB->entry.Fat.ShortName, ' ', 11);
600 FCB->entry.Fat.FileSize = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
601 FCB->entry.Fat.Attrib = FILE_ATTRIBUTE_DIRECTORY;
602 if (pVCB->FatInfo.FatType == FAT32)
603 {
604 CurrentCluster = FirstCluster = pVCB->FatInfo.RootCluster;
605 FCB->entry.Fat.FirstCluster = (unsigned short)(FirstCluster & 0xffff);
606 FCB->entry.Fat.FirstClusterHigh = (unsigned short)(FirstCluster >> 16);
607
608 while (CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
609 {
610 Size += pVCB->FatInfo.BytesPerCluster;
611 Status = NextCluster (pVCB, FirstCluster, &CurrentCluster, FALSE);
612 }
613 }
614 else
615 {
616 FCB->entry.Fat.FirstCluster = 1;
617 Size = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
618 }
619 }
620 FCB->ShortHash.Hash = FCB->Hash.Hash;
621 FCB->RefCount = 2;
622 FCB->dirIndex = 0;
623 FCB->RFCB.FileSize.QuadPart = Size;
624 FCB->RFCB.ValidDataLength.QuadPart = Size;
625 FCB->RFCB.AllocationSize.QuadPart = Size;
626 FCB->RFCB.IsFastIoPossible = FastIoIsNotPossible;
627
628 vfatFCBInitializeCacheFromVolume(pVCB, FCB);
629 vfatAddFCBToTable(pVCB, FCB);
630
631 return FCB;
632 }
633
634 PVFATFCB
635 vfatOpenRootFCB(
636 PDEVICE_EXTENSION pVCB)
637 {
638 PVFATFCB FCB;
639 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\");
640
641 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
642 if (FCB == NULL)
643 {
644 FCB = vfatMakeRootFCB(pVCB);
645 }
646
647 return FCB;
648 }
649
650 NTSTATUS
651 vfatMakeFCBFromDirEntry(
652 PVCB vcb,
653 PVFATFCB directoryFCB,
654 PVFAT_DIRENTRY_CONTEXT DirContext,
655 PVFATFCB *fileFCB)
656 {
657 PVFATFCB rcFCB;
658 UNICODE_STRING NameU;
659 NTSTATUS Status;
660
661 Status = vfatMakeFullName(directoryFCB, &DirContext->LongNameU, &DirContext->ShortNameU, &NameU);
662 if (!NT_SUCCESS(Status))
663 {
664 return Status;
665 }
666
667 rcFCB = vfatNewFCB(vcb, &NameU);
668 vfatInitFCBFromDirEntry(vcb, rcFCB, DirContext);
669
670 rcFCB->RefCount = 1;
671 if (vfatFCBIsDirectory(rcFCB))
672 {
673 vfatFCBInitializeCacheFromVolume(vcb, rcFCB);
674 }
675 rcFCB->parentFcb = directoryFCB;
676 vfatAddFCBToTable(vcb, rcFCB);
677 *fileFCB = rcFCB;
678
679 ExFreePool(NameU.Buffer);
680 return STATUS_SUCCESS;
681 }
682
683 NTSTATUS
684 vfatAttachFCBToFileObject(
685 PDEVICE_EXTENSION vcb,
686 PVFATFCB fcb,
687 PFILE_OBJECT fileObject)
688 {
689 PVFATCCB newCCB;
690
691 UNREFERENCED_PARAMETER(vcb);
692
693 newCCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
694 if (newCCB == NULL)
695 {
696 return STATUS_INSUFFICIENT_RESOURCES;
697 }
698 RtlZeroMemory(newCCB, sizeof (VFATCCB));
699
700 fileObject->SectionObjectPointer = &fcb->SectionObjectPointers;
701 fileObject->FsContext = fcb;
702 fileObject->FsContext2 = newCCB;
703 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb, &fcb->PathNameU);
704
705 return STATUS_SUCCESS;
706 }
707
708 NTSTATUS
709 vfatDirFindFile(
710 PDEVICE_EXTENSION pDeviceExt,
711 PVFATFCB pDirectoryFCB,
712 PUNICODE_STRING FileToFindU,
713 PVFATFCB *pFoundFCB)
714 {
715 NTSTATUS status;
716 PVOID Context = NULL;
717 PVOID Page = NULL;
718 BOOLEAN First = TRUE;
719 VFAT_DIRENTRY_CONTEXT DirContext;
720 /* This buffer must have a size of 260 characters, because
721 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
722 WCHAR LongNameBuffer[260];
723 WCHAR ShortNameBuffer[13];
724 BOOLEAN FoundLong = FALSE;
725 BOOLEAN FoundShort = FALSE;
726
727 ASSERT(pDeviceExt);
728 ASSERT(pDirectoryFCB);
729 ASSERT(FileToFindU);
730
731 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
732 pDeviceExt, pDirectoryFCB, FileToFindU);
733 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB->PathNameU);
734
735 DirContext.DirIndex = 0;
736 DirContext.LongNameU.Buffer = LongNameBuffer;
737 DirContext.LongNameU.Length = 0;
738 DirContext.LongNameU.MaximumLength = sizeof(LongNameBuffer);
739 DirContext.ShortNameU.Buffer = ShortNameBuffer;
740 DirContext.ShortNameU.Length = 0;
741 DirContext.ShortNameU.MaximumLength = sizeof(ShortNameBuffer);
742
743 while (TRUE)
744 {
745 status = pDeviceExt->GetNextDirEntry(&Context,
746 &Page,
747 pDirectoryFCB,
748 &DirContext,
749 First);
750 First = FALSE;
751 if (status == STATUS_NO_MORE_ENTRIES)
752 {
753 return STATUS_OBJECT_NAME_NOT_FOUND;
754 }
755 if (!NT_SUCCESS(status))
756 {
757 return status;
758 }
759
760 DPRINT(" Index:%u longName:%wZ\n",
761 DirContext.DirIndex, &DirContext.LongNameU);
762
763 if (!ENTRY_VOLUME(pDeviceExt, &DirContext.DirEntry))
764 {
765 if (DirContext.LongNameU.Length == 0 ||
766 DirContext.ShortNameU.Length == 0)
767 {
768 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
769 if (VfatGlobalData->Flags & VFAT_BREAK_ON_CORRUPTION)
770 {
771 ASSERT(DirContext.LongNameU.Length != 0 &&
772 DirContext.ShortNameU.Length != 0);
773 }
774 DirContext.DirIndex++;
775 continue;
776 }
777 FoundLong = RtlEqualUnicodeString(FileToFindU, &DirContext.LongNameU, TRUE);
778 if (FoundLong == FALSE)
779 {
780 FoundShort = RtlEqualUnicodeString(FileToFindU, &DirContext.ShortNameU, TRUE);
781 }
782 if (FoundLong || FoundShort)
783 {
784 status = vfatMakeFCBFromDirEntry(pDeviceExt,
785 pDirectoryFCB,
786 &DirContext,
787 pFoundFCB);
788 CcUnpinData(Context);
789 return status;
790 }
791 }
792 DirContext.DirIndex++;
793 }
794
795 return STATUS_OBJECT_NAME_NOT_FOUND;
796 }
797
798 NTSTATUS
799 vfatGetFCBForFile(
800 PDEVICE_EXTENSION pVCB,
801 PVFATFCB *pParentFCB,
802 PVFATFCB *pFCB,
803 PUNICODE_STRING pFileNameU)
804 {
805 NTSTATUS status;
806 PVFATFCB FCB = NULL;
807 PVFATFCB parentFCB;
808 UNICODE_STRING NameU;
809 UNICODE_STRING RootNameU = RTL_CONSTANT_STRING(L"\\");
810 UNICODE_STRING FileNameU;
811 WCHAR NameBuffer[260];
812 PWCHAR curr, prev, last;
813 ULONG Length;
814
815 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
816 pVCB, pParentFCB, pFCB, pFileNameU);
817
818 RtlInitEmptyUnicodeString(&FileNameU, NameBuffer, sizeof(NameBuffer));
819
820 parentFCB = *pParentFCB;
821
822 if (parentFCB == NULL)
823 {
824 /* Passed-in name is the full name */
825 RtlCopyUnicodeString(&FileNameU, pFileNameU);
826
827 // Trivial case, open of the root directory on volume
828 if (RtlEqualUnicodeString(&FileNameU, &RootNameU, FALSE))
829 {
830 DPRINT("returning root FCB\n");
831
832 FCB = vfatOpenRootFCB(pVCB);
833 *pFCB = FCB;
834 *pParentFCB = NULL;
835
836 return (FCB != NULL) ? STATUS_SUCCESS : STATUS_OBJECT_PATH_NOT_FOUND;
837 }
838
839 /* Check for an existing FCB */
840 FCB = vfatGrabFCBFromTable(pVCB, &FileNameU);
841 if (FCB)
842 {
843 *pFCB = FCB;
844 *pParentFCB = FCB->parentFcb;
845 vfatGrabFCB(pVCB, *pParentFCB);
846 return STATUS_SUCCESS;
847 }
848
849 last = curr = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
850 while (*curr != L'\\' && curr > FileNameU.Buffer)
851 {
852 curr--;
853 }
854
855 if (curr > FileNameU.Buffer)
856 {
857 NameU.Buffer = FileNameU.Buffer;
858 NameU.MaximumLength = NameU.Length = (curr - FileNameU.Buffer) * sizeof(WCHAR);
859 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
860 if (FCB)
861 {
862 Length = (curr - FileNameU.Buffer) * sizeof(WCHAR);
863 if (Length != FCB->PathNameU.Length)
864 {
865 if (FileNameU.Length + FCB->PathNameU.Length - Length > FileNameU.MaximumLength)
866 {
867 vfatReleaseFCB(pVCB, FCB);
868 return STATUS_OBJECT_NAME_INVALID;
869 }
870 RtlMoveMemory(FileNameU.Buffer + FCB->PathNameU.Length / sizeof(WCHAR),
871 curr, FileNameU.Length - Length);
872 FileNameU.Length += (USHORT)(FCB->PathNameU.Length - Length);
873 curr = FileNameU.Buffer + FCB->PathNameU.Length / sizeof(WCHAR);
874 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
875 }
876 RtlCopyMemory(FileNameU.Buffer, FCB->PathNameU.Buffer, FCB->PathNameU.Length);
877 }
878 }
879 else
880 {
881 FCB = NULL;
882 }
883
884 if (FCB == NULL)
885 {
886 FCB = vfatOpenRootFCB(pVCB);
887 curr = FileNameU.Buffer;
888 }
889
890 parentFCB = NULL;
891 prev = curr;
892 }
893 else
894 {
895 /* Make absolute path */
896 RtlCopyUnicodeString(&FileNameU, &parentFCB->PathNameU);
897 curr = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
898 if (*curr != L'\\')
899 {
900 RtlAppendUnicodeToString(&FileNameU, L"\\");
901 curr++;
902 }
903 ASSERT(*curr == L'\\');
904 RtlAppendUnicodeStringToString(&FileNameU, pFileNameU);
905
906 FCB = parentFCB;
907 parentFCB = NULL;
908 prev = curr;
909 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
910 }
911
912 while (curr <= last)
913 {
914 if (parentFCB)
915 {
916 vfatReleaseFCB(pVCB, parentFCB);
917 parentFCB = NULL;
918 }
919 // fail if element in FCB is not a directory
920 if (!vfatFCBIsDirectory(FCB))
921 {
922 DPRINT ("Element in requested path is not a directory\n");
923
924 vfatReleaseFCB(pVCB, FCB);
925 FCB = NULL;
926 *pParentFCB = NULL;
927 *pFCB = NULL;
928
929 return STATUS_OBJECT_PATH_NOT_FOUND;
930 }
931 parentFCB = FCB;
932 if (prev < curr)
933 {
934 Length = (curr - prev) * sizeof(WCHAR);
935 if (Length != parentFCB->LongNameU.Length)
936 {
937 if (FileNameU.Length + parentFCB->LongNameU.Length - Length > FileNameU.MaximumLength)
938 {
939 vfatReleaseFCB(pVCB, parentFCB);
940 *pParentFCB = NULL;
941 *pFCB = NULL;
942 return STATUS_OBJECT_NAME_INVALID;
943 }
944 RtlMoveMemory(prev + parentFCB->LongNameU.Length / sizeof(WCHAR), curr,
945 FileNameU.Length - (curr - FileNameU.Buffer) * sizeof(WCHAR));
946 FileNameU.Length += (USHORT)(parentFCB->LongNameU.Length - Length);
947 curr = prev + parentFCB->LongNameU.Length / sizeof(WCHAR);
948 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
949 }
950 RtlCopyMemory(prev, parentFCB->LongNameU.Buffer, parentFCB->LongNameU.Length);
951 }
952 curr++;
953 prev = curr;
954 while (*curr != L'\\' && curr <= last)
955 {
956 curr++;
957 }
958 NameU.Buffer = FileNameU.Buffer;
959 NameU.Length = (curr - NameU.Buffer) * sizeof(WCHAR);
960 NameU.MaximumLength = FileNameU.MaximumLength;
961 DPRINT("%wZ\n", &NameU);
962 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
963 if (FCB == NULL)
964 {
965 NameU.Buffer = prev;
966 NameU.MaximumLength = NameU.Length = (curr - prev) * sizeof(WCHAR);
967 status = vfatDirFindFile(pVCB, parentFCB, &NameU, &FCB);
968 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
969 {
970 *pFCB = NULL;
971 if (curr > last)
972 {
973 *pParentFCB = parentFCB;
974 return STATUS_OBJECT_NAME_NOT_FOUND;
975 }
976 else
977 {
978 vfatReleaseFCB(pVCB, parentFCB);
979 *pParentFCB = NULL;
980 return STATUS_OBJECT_PATH_NOT_FOUND;
981 }
982 }
983 else if (!NT_SUCCESS(status))
984 {
985 vfatReleaseFCB(pVCB, parentFCB);
986 *pParentFCB = NULL;
987 *pFCB = NULL;
988
989 return status;
990 }
991 }
992 }
993
994 *pParentFCB = parentFCB;
995 *pFCB = FCB;
996
997 return STATUS_SUCCESS;
998 }