[FASTFAT]
[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 if (!(RequestedOptions & FILE_DIRECTORY_FILE))
555 Attributes |= FILE_ATTRIBUTE_ARCHIVE;
556 vfatSplitPathName(&PathNameU, NULL, &FileNameU);
557 Status = VfatAddEntry(DeviceExt, &FileNameU, &pFcb, ParentFcb, RequestedOptions,
558 (UCHAR)(Attributes & FILE_ATTRIBUTE_VALID_FLAGS));
559 vfatReleaseFCB(DeviceExt, ParentFcb);
560 if (NT_SUCCESS(Status))
561 {
562 Status = vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
563 if (!NT_SUCCESS(Status))
564 {
565 vfatReleaseFCB(DeviceExt, pFcb);
566 return Status;
567 }
568
569 Irp->IoStatus.Information = FILE_CREATED;
570 VfatSetAllocationSizeInformation(FileObject,
571 pFcb,
572 DeviceExt,
573 &Irp->Overlay.AllocationSize);
574 VfatSetExtendedAttributes(FileObject,
575 Irp->AssociatedIrp.SystemBuffer,
576 Stack->Parameters.Create.EaLength);
577
578 if (PagingFileCreate)
579 {
580 pFcb->Flags |= FCB_IS_PAGE_FILE;
581 }
582 }
583 else
584 {
585 return Status;
586 }
587 }
588 else
589 {
590 if (ParentFcb)
591 {
592 vfatReleaseFCB(DeviceExt, ParentFcb);
593 }
594 return Status;
595 }
596 }
597 else
598 {
599 if (ParentFcb)
600 {
601 vfatReleaseFCB(DeviceExt, ParentFcb);
602 }
603
604 /* Otherwise fail if the caller wanted to create a new file */
605 if (RequestedDisposition == FILE_CREATE)
606 {
607 Irp->IoStatus.Information = FILE_EXISTS;
608 VfatCloseFile(DeviceExt, FileObject);
609 return STATUS_OBJECT_NAME_COLLISION;
610 }
611
612 pFcb = FileObject->FsContext;
613
614 if (pFcb->OpenHandleCount != 0)
615 {
616 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
617 Stack->Parameters.Create.ShareAccess,
618 FileObject,
619 &pFcb->FCBShareAccess,
620 FALSE);
621 if (!NT_SUCCESS(Status))
622 {
623 VfatCloseFile(DeviceExt, FileObject);
624 return Status;
625 }
626 }
627
628 /*
629 * Check the file has the requested attributes
630 */
631 if (RequestedOptions & FILE_NON_DIRECTORY_FILE &&
632 *pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY)
633 {
634 VfatCloseFile (DeviceExt, FileObject);
635 return STATUS_FILE_IS_A_DIRECTORY;
636 }
637 if (RequestedOptions & FILE_DIRECTORY_FILE &&
638 !(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
639 {
640 VfatCloseFile (DeviceExt, FileObject);
641 return STATUS_NOT_A_DIRECTORY;
642 }
643 #ifndef USE_ROS_CC_AND_FS
644 if (!(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
645 {
646 if (Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA ||
647 RequestedDisposition == FILE_OVERWRITE ||
648 RequestedDisposition == FILE_OVERWRITE_IF)
649 {
650 if (!MmFlushImageSection(&pFcb->SectionObjectPointers, MmFlushForWrite))
651 {
652 DPRINT1("%wZ\n", &pFcb->PathNameU);
653 DPRINT1("%d %d %d\n", Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA,
654 RequestedDisposition == FILE_OVERWRITE, RequestedDisposition == FILE_OVERWRITE_IF);
655 VfatCloseFile (DeviceExt, FileObject);
656 return STATUS_SHARING_VIOLATION;
657 }
658 }
659 }
660 #endif
661 if (PagingFileCreate)
662 {
663 /* FIXME:
664 * Do more checking for page files. It is possible,
665 * that the file was opened and closed previously
666 * as a normal cached file. In this case, the cache
667 * manager has referenced the fileobject and the fcb
668 * is held in memory. Try to remove the fileobject
669 * from cache manager and use the fcb.
670 */
671 if (pFcb->RefCount > 1)
672 {
673 if(!(pFcb->Flags & FCB_IS_PAGE_FILE))
674 {
675 VfatCloseFile(DeviceExt, FileObject);
676 return STATUS_INVALID_PARAMETER;
677 }
678 }
679 else
680 {
681 pFcb->Flags |= FCB_IS_PAGE_FILE;
682 }
683 }
684 else
685 {
686 if (pFcb->Flags & FCB_IS_PAGE_FILE)
687 {
688 VfatCloseFile(DeviceExt, FileObject);
689 return STATUS_INVALID_PARAMETER;
690 }
691 }
692
693 if (RequestedDisposition == FILE_OVERWRITE ||
694 RequestedDisposition == FILE_OVERWRITE_IF ||
695 RequestedDisposition == FILE_SUPERSEDE)
696 {
697 if (!(*pFcb->Attributes & FILE_ATTRIBUTE_DIRECTORY))
698 {
699 *pFcb->Attributes = Stack->Parameters.Create.FileAttributes & ~FILE_ATTRIBUTE_NORMAL;
700 *pFcb->Attributes |= FILE_ATTRIBUTE_ARCHIVE;
701 VfatUpdateEntry(pFcb);
702 }
703
704 ExAcquireResourceExclusiveLite(&(pFcb->MainResource), TRUE);
705 Status = VfatSetAllocationSizeInformation(FileObject,
706 pFcb,
707 DeviceExt,
708 &Irp->Overlay.AllocationSize);
709 ExReleaseResourceLite(&(pFcb->MainResource));
710 if (!NT_SUCCESS (Status))
711 {
712 VfatCloseFile(DeviceExt, FileObject);
713 return Status;
714 }
715 }
716
717 if (RequestedDisposition == FILE_SUPERSEDE)
718 {
719 Irp->IoStatus.Information = FILE_SUPERSEDED;
720 }
721 else if (RequestedDisposition == FILE_OVERWRITE ||
722 RequestedDisposition == FILE_OVERWRITE_IF)
723 {
724 Irp->IoStatus.Information = FILE_OVERWRITTEN;
725 }
726 else
727 {
728 Irp->IoStatus.Information = FILE_OPENED;
729 }
730 }
731
732 if (pFcb->OpenHandleCount == 0)
733 {
734 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
735 Stack->Parameters.Create.ShareAccess,
736 FileObject,
737 &pFcb->FCBShareAccess);
738 }
739 else
740 {
741 IoUpdateShareAccess(FileObject,
742 &pFcb->FCBShareAccess);
743 }
744
745 pFcb->OpenHandleCount++;
746
747 /* FIXME : test write access if requested */
748
749 return Status;
750 }
751
752 /*
753 * FUNCTION: Create or open a file
754 */
755 NTSTATUS
756 VfatCreate(
757 PVFAT_IRP_CONTEXT IrpContext)
758 {
759 NTSTATUS Status;
760
761 ASSERT(IrpContext);
762
763 if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
764 {
765 /* DeviceObject represents FileSystem instead of logical volume */
766 DPRINT ("FsdCreate called with file system\n");
767 IrpContext->Irp->IoStatus.Information = FILE_OPENED;
768 IrpContext->Irp->IoStatus.Status = STATUS_SUCCESS;
769 IoCompleteRequest(IrpContext->Irp, IO_DISK_INCREMENT);
770 VfatFreeIrpContext(IrpContext);
771 return STATUS_SUCCESS;
772 }
773
774 if (!(IrpContext->Flags & IRPCONTEXT_CANWAIT))
775 {
776 return(VfatQueueRequest(IrpContext));
777 }
778
779 IrpContext->Irp->IoStatus.Information = 0;
780 ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource, TRUE);
781 Status = VfatCreateFile(IrpContext->DeviceObject, IrpContext->Irp);
782 ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
783
784 IrpContext->Irp->IoStatus.Status = Status;
785 IoCompleteRequest(IrpContext->Irp,
786 (CCHAR)(NT_SUCCESS(Status) ? IO_DISK_INCREMENT : IO_NO_INCREMENT));
787 VfatFreeIrpContext(IrpContext);
788 return Status;
789 }
790
791 /* EOF */