[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
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 vfatReleaseFCB(
292 PDEVICE_EXTENSION pVCB,
293 PVFATFCB pFCB)
294 {
295 PVFATFCB tmpFcb;
296
297 DPRINT("releasing FCB at %p: %wZ, refCount:%d\n",
298 pFCB, &pFCB->PathNameU, pFCB->RefCount);
299
300 while (pFCB)
301 {
302 pFCB->RefCount--;
303 if (pFCB->RefCount == 0)
304 {
305 ASSERT(pFCB->OpenHandleCount == 0);
306 tmpFcb = pFCB->parentFcb;
307 vfatDelFCBFromTable(pVCB, pFCB);
308 vfatDestroyFCB(pFCB);
309 }
310 else
311 {
312 tmpFcb = NULL;
313 }
314 pFCB = tmpFcb;
315 }
316 }
317
318 static
319 VOID
320 vfatAddFCBToTable(
321 PDEVICE_EXTENSION pVCB,
322 PVFATFCB pFCB)
323 {
324 ULONG Index;
325 ULONG ShortIndex;
326
327 Index = pFCB->Hash.Hash % pVCB->HashTableSize;
328 ShortIndex = pFCB->ShortHash.Hash % pVCB->HashTableSize;
329
330 InsertTailList(&pVCB->FcbListHead, &pFCB->FcbListEntry);
331
332 pFCB->Hash.next = pVCB->FcbHashTable[Index];
333 pVCB->FcbHashTable[Index] = &pFCB->Hash;
334 if (pFCB->Hash.Hash != pFCB->ShortHash.Hash)
335 {
336 pFCB->ShortHash.next = pVCB->FcbHashTable[ShortIndex];
337 pVCB->FcbHashTable[ShortIndex] = &pFCB->ShortHash;
338 }
339 if (pFCB->parentFcb)
340 {
341 pFCB->parentFcb->RefCount++;
342 }
343 }
344
345 NTSTATUS
346 vfatUpdateFCB(
347 PDEVICE_EXTENSION pVCB,
348 PVFATFCB Fcb,
349 PUNICODE_STRING LongName,
350 PUNICODE_STRING ShortName,
351 PVFATFCB ParentFcb)
352 {
353 NTSTATUS Status;
354 PVFATFCB OldParent;
355
356 DPRINT("vfatUpdateFCB(%p, %p, %wZ, %wZ, %p)\n", pVCB, Fcb, LongName, ShortName, ParentFcb);
357
358 /* Delete old name */
359 if (Fcb->PathNameBuffer)
360 {
361 ExFreePoolWithTag(Fcb->PathNameBuffer, TAG_FCB);
362 }
363
364 /* Delete from table */
365 vfatDelFCBFromTable(pVCB, Fcb);
366
367 /* Get full path name */
368 Status = vfatMakeFullName(ParentFcb, LongName, ShortName, &Fcb->PathNameU);
369 if (!NT_SUCCESS(Status))
370 {
371 return Status;
372 }
373
374 /* Split it properly */
375 Fcb->PathNameBuffer = Fcb->PathNameU.Buffer;
376 Fcb->DirNameU.Buffer = Fcb->PathNameU.Buffer;
377 vfatSplitPathName(&Fcb->PathNameU, &Fcb->DirNameU, &Fcb->LongNameU);
378
379 /* Copy short name */
380 RtlCopyUnicodeString(&Fcb->ShortNameU, ShortName);
381
382 /* Recompute hashes */
383 Fcb->Hash.Hash = vfatNameHash(0, &Fcb->PathNameU);
384 if (pVCB->Flags & VCB_IS_FATX)
385 {
386 Fcb->ShortHash.Hash = Fcb->Hash.Hash;
387 }
388 else
389 {
390 Fcb->ShortHash.Hash = vfatNameHash(0, &Fcb->DirNameU);
391 Fcb->ShortHash.Hash = vfatNameHash(Fcb->ShortHash.Hash, &Fcb->ShortNameU);
392 }
393
394 /* Set parent */
395 OldParent = Fcb->parentFcb;
396 Fcb->parentFcb = ParentFcb;
397
398 /* Add to the table */
399 vfatAddFCBToTable(pVCB, Fcb);
400
401 /* If we moved accross directories, dereferenced our old parent
402 * We also derefence in case we're just renaming since AddFCBToTable references it
403 */
404 vfatReleaseFCB(pVCB, OldParent);
405
406 /* In case we were moving accross directories, reset caching on old parent */
407 //if (OldParent != ParentFcb)
408 //{
409 // CcUninitializeCacheMap(OldParent->FileObject, NULL, NULL);
410 //}
411
412 return STATUS_SUCCESS;
413 }
414
415 PVFATFCB
416 vfatGrabFCBFromTable(
417 PDEVICE_EXTENSION pVCB,
418 PUNICODE_STRING PathNameU)
419 {
420 PVFATFCB rcFCB;
421 ULONG Hash;
422 UNICODE_STRING DirNameU;
423 UNICODE_STRING FileNameU;
424 PUNICODE_STRING FcbNameU;
425
426 HASHENTRY* entry;
427
428 DPRINT("'%wZ'\n", PathNameU);
429
430 Hash = vfatNameHash(0, PathNameU);
431
432 entry = pVCB->FcbHashTable[Hash % pVCB->HashTableSize];
433 if (entry)
434 {
435 vfatSplitPathName(PathNameU, &DirNameU, &FileNameU);
436 }
437
438 while (entry)
439 {
440 if (entry->Hash == Hash)
441 {
442 rcFCB = entry->self;
443 DPRINT("'%wZ' '%wZ'\n", &DirNameU, &rcFCB->DirNameU);
444 if (RtlEqualUnicodeString(&DirNameU, &rcFCB->DirNameU, TRUE))
445 {
446 if (rcFCB->Hash.Hash == Hash)
447 {
448 FcbNameU = &rcFCB->LongNameU;
449 }
450 else
451 {
452 FcbNameU = &rcFCB->ShortNameU;
453 }
454 /* compare the file name */
455 DPRINT("'%wZ' '%wZ'\n", &FileNameU, FcbNameU);
456 if (RtlEqualUnicodeString(&FileNameU, FcbNameU, TRUE))
457 {
458 rcFCB->RefCount++;
459 return rcFCB;
460 }
461 }
462 }
463 entry = entry->next;
464 }
465 return NULL;
466 }
467
468 static
469 NTSTATUS
470 vfatFCBInitializeCacheFromVolume(
471 PVCB vcb,
472 PVFATFCB fcb)
473 {
474 PFILE_OBJECT fileObject;
475 PVFATCCB newCCB;
476 NTSTATUS status;
477
478 fileObject = IoCreateStreamFileObject (NULL, vcb->StorageDevice);
479
480 newCCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
481 if (newCCB == NULL)
482 {
483 ObDereferenceObject(fileObject);
484 return STATUS_INSUFFICIENT_RESOURCES;
485 }
486 RtlZeroMemory(newCCB, sizeof (VFATCCB));
487
488 fileObject->SectionObjectPointer = &fcb->SectionObjectPointers;
489 fileObject->FsContext = fcb;
490 fileObject->FsContext2 = newCCB;
491 fcb->FileObject = fileObject;
492 fcb->RefCount++;
493
494 _SEH2_TRY
495 {
496 CcInitializeCacheMap(fileObject,
497 (PCC_FILE_SIZES)(&fcb->RFCB.AllocationSize),
498 TRUE,
499 &VfatGlobalData->CacheMgrCallbacks,
500 fcb);
501 }
502 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
503 {
504 status = _SEH2_GetExceptionCode();
505 fcb->RefCount--;
506 fcb->FileObject = NULL;
507 ExFreeToNPagedLookasideList(&VfatGlobalData->CcbLookasideList, newCCB);
508 ObDereferenceObject(fileObject);
509 return status;
510 }
511 _SEH2_END;
512
513 fcb->Flags |= FCB_CACHE_INITIALIZED;
514 return STATUS_SUCCESS;
515 }
516
517 PVFATFCB
518 vfatMakeRootFCB(
519 PDEVICE_EXTENSION pVCB)
520 {
521 PVFATFCB FCB;
522 ULONG FirstCluster, CurrentCluster, Size = 0;
523 NTSTATUS Status = STATUS_SUCCESS;
524 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\");
525
526 FCB = vfatNewFCB(pVCB, &NameU);
527 if (FCB->Flags & FCB_IS_FATX_ENTRY)
528 {
529 memset(FCB->entry.FatX.Filename, ' ', 42);
530 FCB->entry.FatX.FileSize = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
531 FCB->entry.FatX.Attrib = FILE_ATTRIBUTE_DIRECTORY;
532 FCB->entry.FatX.FirstCluster = 1;
533 Size = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
534 }
535 else
536 {
537 memset(FCB->entry.Fat.ShortName, ' ', 11);
538 FCB->entry.Fat.FileSize = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
539 FCB->entry.Fat.Attrib = FILE_ATTRIBUTE_DIRECTORY;
540 if (pVCB->FatInfo.FatType == FAT32)
541 {
542 CurrentCluster = FirstCluster = pVCB->FatInfo.RootCluster;
543 FCB->entry.Fat.FirstCluster = (unsigned short)(FirstCluster & 0xffff);
544 FCB->entry.Fat.FirstClusterHigh = (unsigned short)(FirstCluster >> 16);
545
546 while (CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
547 {
548 Size += pVCB->FatInfo.BytesPerCluster;
549 Status = NextCluster (pVCB, FirstCluster, &CurrentCluster, FALSE);
550 }
551 }
552 else
553 {
554 FCB->entry.Fat.FirstCluster = 1;
555 Size = pVCB->FatInfo.rootDirectorySectors * pVCB->FatInfo.BytesPerSector;
556 }
557 }
558 FCB->ShortHash.Hash = FCB->Hash.Hash;
559 FCB->RefCount = 2;
560 FCB->dirIndex = 0;
561 FCB->RFCB.FileSize.QuadPart = Size;
562 FCB->RFCB.ValidDataLength.QuadPart = Size;
563 FCB->RFCB.AllocationSize.QuadPart = Size;
564 FCB->RFCB.IsFastIoPossible = FastIoIsNotPossible;
565
566 vfatFCBInitializeCacheFromVolume(pVCB, FCB);
567 vfatAddFCBToTable(pVCB, FCB);
568
569 return FCB;
570 }
571
572 PVFATFCB
573 vfatOpenRootFCB(
574 PDEVICE_EXTENSION pVCB)
575 {
576 PVFATFCB FCB;
577 UNICODE_STRING NameU = RTL_CONSTANT_STRING(L"\\");
578
579 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
580 if (FCB == NULL)
581 {
582 FCB = vfatMakeRootFCB(pVCB);
583 }
584
585 return FCB;
586 }
587
588 NTSTATUS
589 vfatMakeFCBFromDirEntry(
590 PVCB vcb,
591 PVFATFCB directoryFCB,
592 PVFAT_DIRENTRY_CONTEXT DirContext,
593 PVFATFCB *fileFCB)
594 {
595 PVFATFCB rcFCB;
596 ULONG Size;
597 UNICODE_STRING NameU;
598 NTSTATUS Status;
599
600 Status = vfatMakeFullName(directoryFCB, &DirContext->LongNameU, &DirContext->ShortNameU, &NameU);
601 if (!NT_SUCCESS(Status))
602 {
603 return Status;
604 }
605
606 rcFCB = vfatNewFCB(vcb, &NameU);
607 RtlCopyMemory(&rcFCB->entry, &DirContext->DirEntry, sizeof (DIR_ENTRY));
608 RtlCopyUnicodeString(&rcFCB->ShortNameU, &DirContext->ShortNameU);
609 if (vcb->Flags & VCB_IS_FATX)
610 {
611 rcFCB->ShortHash.Hash = rcFCB->Hash.Hash;
612 }
613 else
614 {
615 rcFCB->ShortHash.Hash = vfatNameHash(0, &rcFCB->DirNameU);
616 rcFCB->ShortHash.Hash = vfatNameHash(rcFCB->ShortHash.Hash, &rcFCB->ShortNameU);
617 }
618
619 if (vfatFCBIsDirectory(rcFCB))
620 {
621 ULONG FirstCluster, CurrentCluster;
622 NTSTATUS Status = STATUS_SUCCESS;
623 Size = 0;
624 FirstCluster = vfatDirEntryGetFirstCluster(vcb, &rcFCB->entry);
625 if (FirstCluster == 1)
626 {
627 Size = vcb->FatInfo.rootDirectorySectors * vcb->FatInfo.BytesPerSector;
628 }
629 else if (FirstCluster != 0)
630 {
631 CurrentCluster = FirstCluster;
632 while (CurrentCluster != 0xffffffff && NT_SUCCESS(Status))
633 {
634 Size += vcb->FatInfo.BytesPerCluster;
635 Status = NextCluster(vcb, FirstCluster, &CurrentCluster, FALSE);
636 }
637 }
638 }
639 else if (rcFCB->Flags & FCB_IS_FATX_ENTRY)
640 {
641 Size = rcFCB->entry.FatX.FileSize;
642 }
643 else
644 {
645 Size = rcFCB->entry.Fat.FileSize;
646 }
647 rcFCB->dirIndex = DirContext->DirIndex;
648 rcFCB->startIndex = DirContext->StartIndex;
649 if ((rcFCB->Flags & FCB_IS_FATX_ENTRY) && !vfatFCBIsRoot(directoryFCB))
650 {
651 ASSERT(DirContext->DirIndex >= 2 && DirContext->StartIndex >= 2);
652 rcFCB->dirIndex = DirContext->DirIndex-2;
653 rcFCB->startIndex = DirContext->StartIndex-2;
654 }
655 rcFCB->RFCB.FileSize.QuadPart = Size;
656 rcFCB->RFCB.ValidDataLength.QuadPart = Size;
657 rcFCB->RFCB.AllocationSize.QuadPart = ROUND_UP(Size, vcb->FatInfo.BytesPerCluster);
658 rcFCB->RefCount++;
659 if (vfatFCBIsDirectory(rcFCB))
660 {
661 vfatFCBInitializeCacheFromVolume(vcb, rcFCB);
662 }
663 rcFCB->parentFcb = directoryFCB;
664 vfatAddFCBToTable(vcb, rcFCB);
665 *fileFCB = rcFCB;
666
667 ExFreePool(NameU.Buffer);
668 return STATUS_SUCCESS;
669 }
670
671 NTSTATUS
672 vfatAttachFCBToFileObject(
673 PDEVICE_EXTENSION vcb,
674 PVFATFCB fcb,
675 PFILE_OBJECT fileObject)
676 {
677 PVFATCCB newCCB;
678
679 UNREFERENCED_PARAMETER(vcb);
680
681 newCCB = ExAllocateFromNPagedLookasideList(&VfatGlobalData->CcbLookasideList);
682 if (newCCB == NULL)
683 {
684 return STATUS_INSUFFICIENT_RESOURCES;
685 }
686 RtlZeroMemory(newCCB, sizeof (VFATCCB));
687
688 fileObject->SectionObjectPointer = &fcb->SectionObjectPointers;
689 fileObject->FsContext = fcb;
690 fileObject->FsContext2 = newCCB;
691 DPRINT("file open: fcb:%p PathName:%wZ\n", fcb, &fcb->PathNameU);
692
693 return STATUS_SUCCESS;
694 }
695
696 NTSTATUS
697 vfatDirFindFile(
698 PDEVICE_EXTENSION pDeviceExt,
699 PVFATFCB pDirectoryFCB,
700 PUNICODE_STRING FileToFindU,
701 PVFATFCB *pFoundFCB)
702 {
703 NTSTATUS status;
704 PVOID Context = NULL;
705 PVOID Page = NULL;
706 BOOLEAN First = TRUE;
707 VFAT_DIRENTRY_CONTEXT DirContext;
708 /* This buffer must have a size of 260 characters, because
709 vfatMakeFCBFromDirEntry can copy 20 name entries with 13 characters. */
710 WCHAR LongNameBuffer[260];
711 WCHAR ShortNameBuffer[13];
712 BOOLEAN FoundLong = FALSE;
713 BOOLEAN FoundShort = FALSE;
714
715 ASSERT(pDeviceExt);
716 ASSERT(pDirectoryFCB);
717 ASSERT(FileToFindU);
718
719 DPRINT("vfatDirFindFile(VCB:%p, dirFCB:%p, File:%wZ)\n",
720 pDeviceExt, pDirectoryFCB, FileToFindU);
721 DPRINT("Dir Path:%wZ\n", &pDirectoryFCB->PathNameU);
722
723 DirContext.DirIndex = 0;
724 DirContext.LongNameU.Buffer = LongNameBuffer;
725 DirContext.LongNameU.Length = 0;
726 DirContext.LongNameU.MaximumLength = sizeof(LongNameBuffer);
727 DirContext.ShortNameU.Buffer = ShortNameBuffer;
728 DirContext.ShortNameU.Length = 0;
729 DirContext.ShortNameU.MaximumLength = sizeof(ShortNameBuffer);
730
731 while (TRUE)
732 {
733 status = pDeviceExt->GetNextDirEntry(&Context,
734 &Page,
735 pDirectoryFCB,
736 &DirContext,
737 First);
738 First = FALSE;
739 if (status == STATUS_NO_MORE_ENTRIES)
740 {
741 return STATUS_OBJECT_NAME_NOT_FOUND;
742 }
743 if (!NT_SUCCESS(status))
744 {
745 return status;
746 }
747
748 DPRINT(" Index:%u longName:%wZ\n",
749 DirContext.DirIndex, &DirContext.LongNameU);
750
751 if (!ENTRY_VOLUME(pDeviceExt, &DirContext.DirEntry))
752 {
753 FoundLong = RtlEqualUnicodeString(FileToFindU, &DirContext.LongNameU, TRUE);
754 if (FoundLong == FALSE)
755 {
756 FoundShort = RtlEqualUnicodeString(FileToFindU, &DirContext.ShortNameU, TRUE);
757 }
758 if (FoundLong || FoundShort)
759 {
760 status = vfatMakeFCBFromDirEntry(pDeviceExt,
761 pDirectoryFCB,
762 &DirContext,
763 pFoundFCB);
764 CcUnpinData(Context);
765 return status;
766 }
767 }
768 DirContext.DirIndex++;
769 }
770
771 return STATUS_OBJECT_NAME_NOT_FOUND;
772 }
773
774 NTSTATUS
775 vfatGetFCBForFile(
776 PDEVICE_EXTENSION pVCB,
777 PVFATFCB *pParentFCB,
778 PVFATFCB *pFCB,
779 PUNICODE_STRING pFileNameU)
780 {
781 NTSTATUS status;
782 PVFATFCB FCB = NULL;
783 PVFATFCB parentFCB;
784 UNICODE_STRING NameU;
785 UNICODE_STRING RootNameU = RTL_CONSTANT_STRING(L"\\");
786 UNICODE_STRING FileNameU;
787 WCHAR NameBuffer[260];
788 PWCHAR curr, prev, last;
789 ULONG Length;
790
791 DPRINT("vfatGetFCBForFile (%p,%p,%p,%wZ)\n",
792 pVCB, pParentFCB, pFCB, pFileNameU);
793
794 FileNameU.Buffer = NameBuffer;
795 FileNameU.MaximumLength = sizeof(NameBuffer);
796 RtlCopyUnicodeString(&FileNameU, pFileNameU);
797
798 parentFCB = *pParentFCB;
799
800 if (parentFCB == NULL)
801 {
802 // Trivial case, open of the root directory on volume
803 if (RtlEqualUnicodeString(&FileNameU, &RootNameU, FALSE))
804 {
805 DPRINT("returning root FCB\n");
806
807 FCB = vfatOpenRootFCB(pVCB);
808 *pFCB = FCB;
809 *pParentFCB = NULL;
810
811 return (FCB != NULL) ? STATUS_SUCCESS : STATUS_OBJECT_PATH_NOT_FOUND;
812 }
813
814 /* Check for an existing FCB */
815 FCB = vfatGrabFCBFromTable(pVCB, &FileNameU);
816 if (FCB)
817 {
818 *pFCB = FCB;
819 *pParentFCB = FCB->parentFcb;
820 (*pParentFCB)->RefCount++;
821 return STATUS_SUCCESS;
822 }
823
824 last = curr = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
825 while (*curr != L'\\' && curr > FileNameU.Buffer)
826 {
827 curr--;
828 }
829
830 if (curr > FileNameU.Buffer)
831 {
832 NameU.Buffer = FileNameU.Buffer;
833 NameU.MaximumLength = NameU.Length = (curr - FileNameU.Buffer) * sizeof(WCHAR);
834 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
835 if (FCB)
836 {
837 Length = (curr - FileNameU.Buffer) * sizeof(WCHAR);
838 if (Length != FCB->PathNameU.Length)
839 {
840 if (FileNameU.Length + FCB->PathNameU.Length - Length > FileNameU.MaximumLength)
841 {
842 vfatReleaseFCB(pVCB, FCB);
843 return STATUS_OBJECT_NAME_INVALID;
844 }
845 RtlMoveMemory(FileNameU.Buffer + FCB->PathNameU.Length / sizeof(WCHAR),
846 curr, FileNameU.Length - Length);
847 FileNameU.Length += (USHORT)(FCB->PathNameU.Length - Length);
848 curr = FileNameU.Buffer + FCB->PathNameU.Length / sizeof(WCHAR);
849 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
850 }
851 RtlCopyMemory(FileNameU.Buffer, FCB->PathNameU.Buffer, FCB->PathNameU.Length);
852 }
853 }
854 else
855 {
856 FCB = NULL;
857 }
858
859 if (FCB == NULL)
860 {
861 FCB = vfatOpenRootFCB(pVCB);
862 curr = FileNameU.Buffer;
863 }
864
865 parentFCB = NULL;
866 prev = curr;
867 }
868 else
869 {
870 FCB = parentFCB;
871 parentFCB = NULL;
872 prev = curr = FileNameU.Buffer - 1;
873 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
874 }
875
876 while (curr <= last)
877 {
878 if (parentFCB)
879 {
880 vfatReleaseFCB(pVCB, parentFCB);
881 parentFCB = 0;
882 }
883 // fail if element in FCB is not a directory
884 if (!vfatFCBIsDirectory(FCB))
885 {
886 DPRINT ("Element in requested path is not a directory\n");
887
888 vfatReleaseFCB(pVCB, FCB);
889 FCB = NULL;
890 *pParentFCB = NULL;
891 *pFCB = NULL;
892
893 return STATUS_OBJECT_PATH_NOT_FOUND;
894 }
895 parentFCB = FCB;
896 if (prev < curr)
897 {
898 Length = (curr - prev) * sizeof(WCHAR);
899 if (Length != parentFCB->LongNameU.Length)
900 {
901 if (FileNameU.Length + parentFCB->LongNameU.Length - Length > FileNameU.MaximumLength)
902 {
903 vfatReleaseFCB(pVCB, parentFCB);
904 return STATUS_OBJECT_NAME_INVALID;
905 }
906 RtlMoveMemory(prev + parentFCB->LongNameU.Length / sizeof(WCHAR), curr,
907 FileNameU.Length - (curr - FileNameU.Buffer) * sizeof(WCHAR));
908 FileNameU.Length += (USHORT)(parentFCB->LongNameU.Length - Length);
909 curr = prev + parentFCB->LongNameU.Length / sizeof(WCHAR);
910 last = FileNameU.Buffer + FileNameU.Length / sizeof(WCHAR) - 1;
911 }
912 RtlCopyMemory(prev, parentFCB->LongNameU.Buffer, parentFCB->LongNameU.Length);
913 }
914 curr++;
915 prev = curr;
916 while (*curr != L'\\' && curr <= last)
917 {
918 curr++;
919 }
920 NameU.Buffer = FileNameU.Buffer;
921 NameU.Length = (curr - NameU.Buffer) * sizeof(WCHAR);
922 NameU.MaximumLength = FileNameU.MaximumLength;
923 DPRINT("%wZ\n", &NameU);
924 FCB = vfatGrabFCBFromTable(pVCB, &NameU);
925 if (FCB == NULL)
926 {
927 NameU.Buffer = prev;
928 NameU.MaximumLength = NameU.Length = (curr - prev) * sizeof(WCHAR);
929 status = vfatDirFindFile(pVCB, parentFCB, &NameU, &FCB);
930 if (status == STATUS_OBJECT_NAME_NOT_FOUND)
931 {
932 *pFCB = NULL;
933 if (curr > last)
934 {
935 *pParentFCB = parentFCB;
936 return STATUS_OBJECT_NAME_NOT_FOUND;
937 }
938 else
939 {
940 vfatReleaseFCB(pVCB, parentFCB);
941 *pParentFCB = NULL;
942 return STATUS_OBJECT_PATH_NOT_FOUND;
943 }
944 }
945 else if (!NT_SUCCESS(status))
946 {
947 vfatReleaseFCB(pVCB, parentFCB);
948 *pParentFCB = NULL;
949 *pFCB = NULL;
950
951 return status;
952 }
953 }
954 }
955
956 *pParentFCB = parentFCB;
957 *pFCB = FCB;
958
959 return STATUS_SUCCESS;
960 }