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