738bc6a5c1c98257d6cf692e5362e151d9484049
[reactos.git] / reactos / drivers / filesystems / fastfat / create.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /*
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/fs/vfat/create.c
22 * PURPOSE: VFAT Filesystem
23 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
24 */
25
26 /* INCLUDES *****************************************************************/
27
28 #define NDEBUG
29 #include "vfat.h"
30
31 /* FUNCTIONS *****************************************************************/
32
33 VOID
34 vfat8Dot3ToString(
35 PFAT_DIR_ENTRY pEntry,
36 PUNICODE_STRING NameU)
37 {
38 OEM_STRING StringA;
39 USHORT Length;
40 CHAR cString[12];
41
42 RtlCopyMemory(cString, pEntry->ShortName, 11);
43 cString[11] = 0;
44 if (cString[0] == 0x05)
45 {
46 cString[0] = 0xe5;
47 }
48
49 StringA.Buffer = cString;
50 for (StringA.Length = 0;
51 StringA.Length < 8 && StringA.Buffer[StringA.Length] != ' ';
52 StringA.Length++);
53 StringA.MaximumLength = StringA.Length;
54
55 RtlOemStringToUnicodeString(NameU, &StringA, FALSE);
56
57 if (pEntry->lCase & VFAT_CASE_LOWER_BASE)
58 {
59 RtlDowncaseUnicodeString(NameU, NameU, FALSE);
60 }
61
62 if (cString[8] != ' ')
63 {
64 Length = NameU->Length;
65 NameU->Buffer += Length / sizeof(WCHAR);
66 if (!FAT_ENTRY_VOLUME(pEntry))
67 {
68 Length += sizeof(WCHAR);
69 NameU->Buffer[0] = L'.';
70 NameU->Buffer++;
71 }
72 NameU->Length = 0;
73 NameU->MaximumLength -= Length;
74
75 StringA.Buffer = &cString[8];
76 for (StringA.Length = 0;
77 StringA.Length < 3 && StringA.Buffer[StringA.Length] != ' ';
78 StringA.Length++);
79 StringA.MaximumLength = StringA.Length;
80 RtlOemStringToUnicodeString(NameU, &StringA, FALSE);
81 if (pEntry->lCase & VFAT_CASE_LOWER_EXT)
82 {
83 RtlDowncaseUnicodeString(NameU, NameU, FALSE);
84 }
85 NameU->Buffer -= Length / sizeof(WCHAR);
86 NameU->Length += Length;
87 NameU->MaximumLength += Length;
88 }
89
90 NameU->Buffer[NameU->Length / sizeof(WCHAR)] = 0;
91 DPRINT("'%wZ'\n", NameU);
92 }
93
94 /*
95 * FUNCTION: Read the volume label
96 */
97 NTSTATUS
98 ReadVolumeLabel(
99 PDEVICE_EXTENSION DeviceExt,
100 PVPB Vpb)
101 {
102 PVOID Context = NULL;
103 ULONG DirIndex = 0;
104 PDIR_ENTRY Entry;
105 PVFATFCB pFcb;
106 LARGE_INTEGER FileOffset;
107 UNICODE_STRING NameU;
108 ULONG SizeDirEntry;
109 ULONG EntriesPerPage;
110 OEM_STRING StringO;
111
112 NameU.Buffer = Vpb->VolumeLabel;
113 NameU.Length = 0;
114 NameU.MaximumLength = sizeof(Vpb->VolumeLabel);
115 *(Vpb->VolumeLabel) = 0;
116 Vpb->VolumeLabelLength = 0;
117
118 if (DeviceExt->Flags & VCB_IS_FATX)
119 {
120 SizeDirEntry = sizeof(FATX_DIR_ENTRY);
121 EntriesPerPage = FATX_ENTRIES_PER_PAGE;
122 }
123 else
124 {
125 SizeDirEntry = sizeof(FAT_DIR_ENTRY);
126 EntriesPerPage = FAT_ENTRIES_PER_PAGE;
127 }
128
129 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
130 pFcb = vfatOpenRootFCB(DeviceExt);
131 ExReleaseResourceLite(&DeviceExt->DirResource);
132
133 FileOffset.QuadPart = 0;
134 if (CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, TRUE, &Context, (PVOID*)&Entry))
135 {
136 while (TRUE)
137 {
138 if (ENTRY_VOLUME(DeviceExt, Entry))
139 {
140 /* copy volume label */
141 if (DeviceExt->Flags & VCB_IS_FATX)
142 {
143 StringO.Buffer = (PCHAR)Entry->FatX.Filename;
144 StringO.MaximumLength = StringO.Length = Entry->FatX.FilenameLength;
145 RtlOemStringToUnicodeString(&NameU, &StringO, FALSE);
146 }
147 else
148 {
149 vfat8Dot3ToString(&Entry->Fat, &NameU);
150 }
151 Vpb->VolumeLabelLength = NameU.Length;
152 break;
153 }
154 if (ENTRY_END(DeviceExt, Entry))
155 {
156 break;
157 }
158 DirIndex++;
159 Entry = (PDIR_ENTRY)((ULONG_PTR)Entry + SizeDirEntry);
160 if ((DirIndex % EntriesPerPage) == 0)
161 {
162 CcUnpinData(Context);
163 FileOffset.u.LowPart += PAGE_SIZE;
164 if (!CcMapData(pFcb->FileObject, &FileOffset, SizeDirEntry, TRUE, &Context, (PVOID*)&Entry))
165 {
166 Context = NULL;
167 break;
168 }
169 }
170 }
171 if (Context)
172 {
173 CcUnpinData(Context);
174 }
175 }
176 ExAcquireResourceExclusiveLite(&DeviceExt->DirResource, TRUE);
177 vfatReleaseFCB(DeviceExt, pFcb);
178 ExReleaseResourceLite(&DeviceExt->DirResource);
179
180 return STATUS_SUCCESS;
181 }
182
183 /*
184 * FUNCTION: Find a file
185 */
186 NTSTATUS
187 FindFile(
188 PDEVICE_EXTENSION DeviceExt,
189 PVFATFCB Parent,
190 PUNICODE_STRING FileToFindU,
191 PVFAT_DIRENTRY_CONTEXT DirContext,
192 BOOLEAN First)
193 {
194 PWCHAR PathNameBuffer;
195 USHORT PathNameBufferLength;
196 NTSTATUS Status;
197 PVOID Context = NULL;
198 PVOID Page;
199 PVFATFCB rcFcb;
200 BOOLEAN Found;
201 UNICODE_STRING PathNameU;
202 UNICODE_STRING FileToFindUpcase;
203 BOOLEAN WildCard;
204
205 DPRINT("FindFile(Parent %p, FileToFind '%wZ', DirIndex: %u)\n",
206 Parent, FileToFindU, DirContext->DirIndex);
207 DPRINT("FindFile: Path %wZ\n",&Parent->PathNameU);
208
209 PathNameBufferLength = LONGNAME_MAX_LENGTH * sizeof(WCHAR);
210 PathNameBuffer = ExAllocatePoolWithTag(NonPagedPool, PathNameBufferLength + sizeof(WCHAR), TAG_VFAT);
211 if (!PathNameBuffer)
212 {
213 return STATUS_INSUFFICIENT_RESOURCES;
214 }
215
216 PathNameU.Buffer = PathNameBuffer;
217 PathNameU.Length = 0;
218 PathNameU.MaximumLength = PathNameBufferLength;
219
220 DirContext->LongNameU.Length = 0;
221 DirContext->ShortNameU.Length = 0;
222
223 WildCard = FsRtlDoesNameContainWildCards(FileToFindU);
224
225 if (WildCard == FALSE)
226 {
227 /* if there is no '*?' in the search name, than look first for an existing fcb */
228 RtlCopyUnicodeString(&PathNameU, &Parent->PathNameU);
229 if (!vfatFCBIsRoot(Parent))
230 {
231 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = L'\\';
232 PathNameU.Length += sizeof(WCHAR);
233 }
234 RtlAppendUnicodeStringToString(&PathNameU, FileToFindU);
235 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = 0;
236 rcFcb = vfatGrabFCBFromTable(DeviceExt, &PathNameU);
237 if (rcFcb)
238 {
239 ULONG startIndex = rcFcb->startIndex;
240 if ((rcFcb->Flags & FCB_IS_FATX_ENTRY) && !vfatFCBIsRoot(Parent))
241 {
242 startIndex += 2;
243 }
244 if(startIndex >= DirContext->DirIndex)
245 {
246 RtlCopyUnicodeString(&DirContext->LongNameU, &rcFcb->LongNameU);
247 RtlCopyUnicodeString(&DirContext->ShortNameU, &rcFcb->ShortNameU);
248 RtlCopyMemory(&DirContext->DirEntry, &rcFcb->entry, sizeof(DIR_ENTRY));
249 DirContext->StartIndex = rcFcb->startIndex;
250 DirContext->DirIndex = rcFcb->dirIndex;
251 DPRINT("FindFile: new Name %wZ, DirIndex %u (%u)\n",
252 &DirContext->LongNameU, DirContext->DirIndex, DirContext->StartIndex);
253 Status = STATUS_SUCCESS;
254 }
255 else
256 {
257 DPRINT("FCB not found for %wZ\n", &PathNameU);
258 Status = STATUS_UNSUCCESSFUL;
259 }
260 vfatReleaseFCB(DeviceExt, rcFcb);
261 ExFreePool(PathNameBuffer);
262 return Status;
263 }
264 }
265
266 /* FsRtlIsNameInExpression need the searched string to be upcase,
267 * even if IgnoreCase is specified */
268 Status = RtlUpcaseUnicodeString(&FileToFindUpcase, FileToFindU, TRUE);
269 if (!NT_SUCCESS(Status))
270 {
271 ExFreePool(PathNameBuffer);
272 return Status;
273 }
274
275 while(TRUE)
276 {
277 Status = DeviceExt->GetNextDirEntry(&Context, &Page, Parent, DirContext, First);
278 First = FALSE;
279 if (Status == STATUS_NO_MORE_ENTRIES)
280 {
281 break;
282 }
283 if (ENTRY_VOLUME(DeviceExt, &DirContext->DirEntry))
284 {
285 DirContext->DirIndex++;
286 continue;
287 }
288 if (WildCard)
289 {
290 Found = FsRtlIsNameInExpression(&FileToFindUpcase, &DirContext->LongNameU, TRUE, NULL) ||
291 FsRtlIsNameInExpression(&FileToFindUpcase, &DirContext->ShortNameU, TRUE, NULL);
292 }
293 else
294 {
295 Found = FsRtlAreNamesEqual(&DirContext->LongNameU, FileToFindU, TRUE, NULL) ||
296 FsRtlAreNamesEqual(&DirContext->ShortNameU, FileToFindU, TRUE, NULL);
297 }
298
299 if (Found)
300 {
301 if (WildCard)
302 {
303 RtlCopyUnicodeString(&PathNameU, &Parent->PathNameU);
304 if (!vfatFCBIsRoot(Parent))
305 {
306 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = L'\\';
307 PathNameU.Length += sizeof(WCHAR);
308 }
309 RtlAppendUnicodeStringToString(&PathNameU, &DirContext->LongNameU);
310 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = 0;
311 rcFcb = vfatGrabFCBFromTable(DeviceExt, &PathNameU);
312 if (rcFcb != NULL)
313 {
314 RtlCopyMemory(&DirContext->DirEntry, &rcFcb->entry, sizeof(DIR_ENTRY));
315 vfatReleaseFCB(DeviceExt, rcFcb);
316 }
317 }
318 DPRINT("%u\n", DirContext->LongNameU.Length);
319 DPRINT("FindFile: new Name %wZ, DirIndex %u\n",
320 &DirContext->LongNameU, DirContext->DirIndex);
321
322 if (Context)
323 {
324 CcUnpinData(Context);
325 }
326 RtlFreeUnicodeString(&FileToFindUpcase);
327 ExFreePool(PathNameBuffer);
328 return STATUS_SUCCESS;
329 }
330 DirContext->DirIndex++;
331 }
332
333 if (Context)
334 {
335 CcUnpinData(Context);
336 }
337
338 RtlFreeUnicodeString(&FileToFindUpcase);
339 ExFreePool(PathNameBuffer);
340 return Status;
341 }
342
343 /*
344 * FUNCTION: Opens a file
345 */
346 static
347 NTSTATUS
348 VfatOpenFile(
349 PDEVICE_EXTENSION DeviceExt,
350 PUNICODE_STRING PathNameU,
351 PFILE_OBJECT FileObject,
352 PVFATFCB *ParentFcb)
353 {
354 PVFATFCB Fcb;
355 NTSTATUS Status;
356
357 DPRINT("VfatOpenFile(%p, '%wZ', %p, %p)\n", DeviceExt, PathNameU, FileObject, ParentFcb);
358
359 if (FileObject->RelatedFileObject)
360 {
361 DPRINT("'%wZ'\n", &FileObject->RelatedFileObject->FileName);
362
363 *ParentFcb = FileObject->RelatedFileObject->FsContext;
364 (*ParentFcb)->RefCount++;
365 }
366 else
367 {
368 *ParentFcb = NULL;
369 }
370
371 if (!DeviceExt->FatInfo.FixedMedia)
372 {
373 Status = VfatBlockDeviceIoControl(DeviceExt->StorageDevice,
374 IOCTL_DISK_CHECK_VERIFY,
375 NULL,
376 0,
377 NULL,
378 0,
379 FALSE);
380 if (!NT_SUCCESS(Status))
381 {
382 DPRINT ("Status %lx\n", Status);
383 *ParentFcb = NULL;
384 return Status;
385 }
386 }
387
388 if (*ParentFcb)
389 {
390 (*ParentFcb)->RefCount++;
391 }
392
393 /* try first to find an existing FCB in memory */
394 DPRINT("Checking for existing FCB in memory\n");
395
396 Status = vfatGetFCBForFile(DeviceExt, ParentFcb, &Fcb, PathNameU);
397 if (!NT_SUCCESS (Status))
398 {
399 DPRINT ("Could not make a new FCB, status: %x\n", Status);
400 return Status;
401 }
402 if (Fcb->Flags & FCB_DELETE_PENDING)
403 {
404 vfatReleaseFCB (DeviceExt, Fcb);
405 return STATUS_DELETE_PENDING;
406 }
407 DPRINT("Attaching FCB to fileObject\n");
408 Status = vfatAttachFCBToFileObject(DeviceExt, Fcb, FileObject);
409 if (!NT_SUCCESS(Status))
410 {
411 vfatReleaseFCB (DeviceExt, Fcb);
412 }
413 return Status;
414 }
415
416 /*
417 * FUNCTION: Create or open a file
418 */
419 static NTSTATUS
420 VfatCreateFile(
421 PDEVICE_OBJECT DeviceObject,
422 PIRP Irp)
423 {
424 PIO_STACK_LOCATION Stack;
425 PFILE_OBJECT FileObject;
426 NTSTATUS Status = STATUS_SUCCESS;
427 PDEVICE_EXTENSION DeviceExt;
428 ULONG RequestedDisposition, RequestedOptions;
429 PVFATFCB pFcb = NULL;
430 PVFATFCB ParentFcb = NULL;
431 PWCHAR c, last;
432 BOOLEAN PagingFileCreate = FALSE;
433 BOOLEAN Dots;
434 UNICODE_STRING FileNameU;
435 UNICODE_STRING PathNameU;
436 ULONG Attributes;
437
438 /* Unpack the various parameters. */
439 Stack = IoGetCurrentIrpStackLocation (Irp);
440 RequestedDisposition = ((Stack->Parameters.Create.Options >> 24) & 0xff);
441 RequestedOptions = Stack->Parameters.Create.Options & FILE_VALID_OPTION_FLAGS;
442 PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
443 FileObject = Stack->FileObject;
444 DeviceExt = DeviceObject->DeviceExtension;
445
446 /* Check their validity. */
447 if (RequestedOptions & FILE_DIRECTORY_FILE &&
448 RequestedDisposition == FILE_SUPERSEDE)
449 {
450 return STATUS_INVALID_PARAMETER;
451 }
452
453 if (RequestedOptions & FILE_DIRECTORY_FILE &&
454 RequestedOptions & FILE_NON_DIRECTORY_FILE)
455 {
456 return STATUS_INVALID_PARAMETER;
457 }
458
459 /* This a open operation for the volume itself */
460 if (FileObject->FileName.Length == 0 &&
461 (FileObject->RelatedFileObject == NULL || FileObject->RelatedFileObject->FsContext2 != NULL))
462 {
463 if (RequestedDisposition != FILE_OPEN &&
464 RequestedDisposition != FILE_OPEN_IF)
465 {
466 return STATUS_ACCESS_DENIED;
467 }
468 #if 0
469 /* In spite of what is shown in WDK, it seems that Windows FAT driver doesn't perform that test */
470 if (RequestedOptions & FILE_DIRECTORY_FILE)
471 {
472 return STATUS_NOT_A_DIRECTORY;
473 }
474 #endif
475
476 pFcb = DeviceExt->VolumeFcb;
477 vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
478 pFcb->RefCount++;
479
480 Irp->IoStatus.Information = FILE_OPENED;
481 return STATUS_SUCCESS;
482 }
483
484 /* Check for illegal characters and illegale dot sequences in the file name */
485 PathNameU = FileObject->FileName;
486 c = PathNameU.Buffer + PathNameU.Length / sizeof(WCHAR);
487 last = c - 1;
488 Dots = TRUE;
489 while (c-- > PathNameU.Buffer)
490 {
491 if (*c == L'\\' || c == PathNameU.Buffer)
492 {
493 if (Dots && last > c)
494 {
495 return STATUS_OBJECT_NAME_INVALID;
496 }
497 last = c - 1;
498 Dots = TRUE;
499 }
500 else if (*c != L'.')
501 {
502 Dots = FALSE;
503 }
504
505 if (*c != '\\' && vfatIsLongIllegal(*c))
506 {
507 return STATUS_OBJECT_NAME_INVALID;
508 }
509 }
510
511 if (FileObject->RelatedFileObject && PathNameU.Length >= sizeof(WCHAR) && PathNameU.Buffer[0] == L'\\')
512 {
513 return STATUS_OBJECT_NAME_INVALID;
514 }
515
516 if (PathNameU.Length > sizeof(WCHAR) && PathNameU.Buffer[PathNameU.Length/sizeof(WCHAR)-1] == L'\\')
517 {
518 PathNameU.Length -= sizeof(WCHAR);
519 }
520
521 /* Try opening the file. */
522 Status = VfatOpenFile(DeviceExt, &PathNameU, FileObject, &ParentFcb);
523
524 /*
525 * If the directory containing the file to open doesn't exist then
526 * fail immediately
527 */
528 if (Status == STATUS_OBJECT_PATH_NOT_FOUND ||
529 Status == STATUS_INVALID_PARAMETER ||
530 Status == STATUS_DELETE_PENDING)
531 {
532 if (ParentFcb)
533 {
534 vfatReleaseFCB(DeviceExt, ParentFcb);
535 }
536 return Status;
537 }
538
539 if (!NT_SUCCESS(Status) && ParentFcb == NULL)
540 {
541 DPRINT1("VfatOpenFile faild for '%wZ', status %x\n", &PathNameU, Status);
542 return Status;
543 }
544
545 /* If the file open failed then create the required file */
546 if (!NT_SUCCESS (Status))
547 {
548 if (RequestedDisposition == FILE_CREATE ||
549 RequestedDisposition == FILE_OPEN_IF ||
550 RequestedDisposition == FILE_OVERWRITE_IF ||
551 RequestedDisposition == FILE_SUPERSEDE)
552 {
553 Attributes = Stack->Parameters.Create.FileAttributes & ~FILE_ATTRIBUTE_NORMAL;
554 vfatSplitPathName(&PathNameU, NULL, &FileNameU);
555 Status = VfatAddEntry(DeviceExt, &FileNameU, &pFcb, ParentFcb, RequestedOptions,
556 (UCHAR)(Attributes & FILE_ATTRIBUTE_VALID_FLAGS));
557 vfatReleaseFCB(DeviceExt, ParentFcb);
558 if (NT_SUCCESS(Status))
559 {
560 Status = vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
561 if (!NT_SUCCESS(Status))
562 {
563 vfatReleaseFCB(DeviceExt, pFcb);
564 return Status;
565 }
566
567 Irp->IoStatus.Information = FILE_CREATED;
568 VfatSetAllocationSizeInformation(FileObject,
569 pFcb,
570 DeviceExt,
571 &Irp->Overlay.AllocationSize);
572 VfatSetExtendedAttributes(FileObject,
573 Irp->AssociatedIrp.SystemBuffer,
574 Stack->Parameters.Create.EaLength);
575
576 if (PagingFileCreate)
577 {
578 pFcb->Flags |= FCB_IS_PAGE_FILE;
579 }
580 }
581 else
582 {
583 return Status;
584 }
585 }
586 else
587 {
588 if (ParentFcb)
589 {
590 vfatReleaseFCB(DeviceExt, ParentFcb);
591 }
592 return Status;
593 }
594 }
595 else
596 {
597 if (ParentFcb)
598 {
599 vfatReleaseFCB(DeviceExt, ParentFcb);
600 }
601
602 /* Otherwise fail if the caller wanted to create a new file */
603 if (RequestedDisposition == FILE_CREATE)
604 {
605 Irp->IoStatus.Information = FILE_EXISTS;
606 VfatCloseFile(DeviceExt, FileObject);
607 return STATUS_OBJECT_NAME_COLLISION;
608 }
609
610 pFcb = FileObject->FsContext;
611
612 if (pFcb->OpenHandleCount != 0)
613 {
614 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
615 Stack->Parameters.Create.ShareAccess,
616 FileObject,
617 &pFcb->FCBShareAccess,
618 FALSE);
619 if (!NT_SUCCESS(Status))
620 {
621 VfatCloseFile(DeviceExt, FileObject);
622 return Status;
623 }
624 }
625
626 /*
627 * Check the file has the requested attributes
628 */
629 if (RequestedOptions & FILE_NON_DIRECTORY_FILE &&
630 *pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY)
631 {
632 VfatCloseFile (DeviceExt, FileObject);
633 return STATUS_FILE_IS_A_DIRECTORY;
634 }
635 if (RequestedOptions & FILE_DIRECTORY_FILE &&
636 !(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
637 {
638 VfatCloseFile (DeviceExt, FileObject);
639 return STATUS_NOT_A_DIRECTORY;
640 }
641 #ifndef USE_ROS_CC_AND_FS
642 if (!(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
643 {
644 if (Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA ||
645 RequestedDisposition == FILE_OVERWRITE ||
646 RequestedDisposition == FILE_OVERWRITE_IF)
647 {
648 if (!MmFlushImageSection(&pFcb->SectionObjectPointers, MmFlushForWrite))
649 {
650 DPRINT1("%wZ\n", &pFcb->PathNameU);
651 DPRINT1("%d %d %d\n", Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA,
652 RequestedDisposition == FILE_OVERWRITE, RequestedDisposition == FILE_OVERWRITE_IF);
653 VfatCloseFile (DeviceExt, FileObject);
654 return STATUS_SHARING_VIOLATION;
655 }
656 }
657 }
658 #endif
659 if (PagingFileCreate)
660 {
661 /* FIXME:
662 * Do more checking for page files. It is possible,
663 * that the file was opened and closed previously
664 * as a normal cached file. In this case, the cache
665 * manager has referenced the fileobject and the fcb
666 * is held in memory. Try to remove the fileobject
667 * from cache manager and use the fcb.
668 */
669 if (pFcb->RefCount > 1)
670 {
671 if(!(pFcb->Flags & FCB_IS_PAGE_FILE))
672 {
673 VfatCloseFile(DeviceExt, FileObject);
674 return STATUS_INVALID_PARAMETER;
675 }
676 }
677 else
678 {
679 pFcb->Flags |= FCB_IS_PAGE_FILE;
680 }
681 }
682 else
683 {
684 if (pFcb->Flags & FCB_IS_PAGE_FILE)
685 {
686 VfatCloseFile(DeviceExt, FileObject);
687 return STATUS_INVALID_PARAMETER;
688 }
689 }
690
691 if (RequestedDisposition == FILE_OVERWRITE ||
692 RequestedDisposition == FILE_OVERWRITE_IF ||
693 RequestedDisposition == FILE_SUPERSEDE)
694 {
695 ExAcquireResourceExclusiveLite(&(pFcb->MainResource), TRUE);
696 Status = VfatSetAllocationSizeInformation(FileObject,
697 pFcb,
698 DeviceExt,
699 &Irp->Overlay.AllocationSize);
700 ExReleaseResourceLite(&(pFcb->MainResource));
701 if (!NT_SUCCESS (Status))
702 {
703 VfatCloseFile(DeviceExt, FileObject);
704 return Status;
705 }
706 }
707
708 if (RequestedDisposition == FILE_SUPERSEDE)
709 {
710 Irp->IoStatus.Information = FILE_SUPERSEDED;
711 }
712 else if (RequestedDisposition == FILE_OVERWRITE ||
713 RequestedDisposition == FILE_OVERWRITE_IF)
714 {
715 Irp->IoStatus.Information = FILE_OVERWRITTEN;
716 }
717 else
718 {
719 Irp->IoStatus.Information = FILE_OPENED;
720 }
721 }
722
723 if (pFcb->OpenHandleCount == 0)
724 {
725 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
726 Stack->Parameters.Create.ShareAccess,
727 FileObject,
728 &pFcb->FCBShareAccess);
729 }
730 else
731 {
732 IoUpdateShareAccess(FileObject,
733 &pFcb->FCBShareAccess);
734 }
735
736 pFcb->OpenHandleCount++;
737
738 /* FIXME : test write access if requested */
739
740 return Status;
741 }
742
743 /*
744 * FUNCTION: Create or open a file
745 */
746 NTSTATUS
747 VfatCreate(
748 PVFAT_IRP_CONTEXT IrpContext)
749 {
750 NTSTATUS Status;
751
752 ASSERT(IrpContext);
753
754 if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
755 {
756 /* DeviceObject represents FileSystem instead of logical volume */
757 DPRINT ("FsdCreate called with file system\n");
758 IrpContext->Irp->IoStatus.Information = FILE_OPENED;
759 IrpContext->Irp->IoStatus.Status = STATUS_SUCCESS;
760 IoCompleteRequest(IrpContext->Irp, IO_DISK_INCREMENT);
761 VfatFreeIrpContext(IrpContext);
762 return STATUS_SUCCESS;
763 }
764
765 if (!(IrpContext->Flags & IRPCONTEXT_CANWAIT))
766 {
767 return(VfatQueueRequest(IrpContext));
768 }
769
770 IrpContext->Irp->IoStatus.Information = 0;
771 ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource, TRUE);
772 Status = VfatCreateFile(IrpContext->DeviceObject, IrpContext->Irp);
773 ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
774
775 IrpContext->Irp->IoStatus.Status = Status;
776 IoCompleteRequest(IrpContext->Irp,
777 (CCHAR)(NT_SUCCESS(Status) ? IO_DISK_INCREMENT : IO_NO_INCREMENT));
778 VfatFreeIrpContext(IrpContext);
779 return Status;
780 }
781
782 /* EOF */