3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
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.
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.
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.
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystems/fastfat/create.c
22 * PURPOSE: VFAT Filesystem
23 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
24 * Pierre Schweitzer (pierre@reactos.org)
27 /* INCLUDES *****************************************************************/
34 /* FUNCTIONS *****************************************************************/
38 PFAT_DIR_ENTRY pEntry
,
39 PUNICODE_STRING NameU
)
45 RtlCopyMemory(cString
, pEntry
->ShortName
, 11);
47 if (cString
[0] == 0x05)
52 StringA
.Buffer
= cString
;
53 for (StringA
.Length
= 0;
54 StringA
.Length
< 8 && StringA
.Buffer
[StringA
.Length
] != ' ';
56 StringA
.MaximumLength
= StringA
.Length
;
58 RtlOemStringToUnicodeString(NameU
, &StringA
, FALSE
);
60 if (BooleanFlagOn(pEntry
->lCase
, VFAT_CASE_LOWER_BASE
))
62 RtlDowncaseUnicodeString(NameU
, NameU
, FALSE
);
65 if (cString
[8] != ' ')
67 Length
= NameU
->Length
;
68 NameU
->Buffer
+= Length
/ sizeof(WCHAR
);
69 if (!FAT_ENTRY_VOLUME(pEntry
))
71 Length
+= sizeof(WCHAR
);
72 NameU
->Buffer
[0] = L
'.';
76 NameU
->MaximumLength
-= Length
;
78 StringA
.Buffer
= &cString
[8];
79 for (StringA
.Length
= 0;
80 StringA
.Length
< 3 && StringA
.Buffer
[StringA
.Length
] != ' ';
82 StringA
.MaximumLength
= StringA
.Length
;
83 RtlOemStringToUnicodeString(NameU
, &StringA
, FALSE
);
84 if (BooleanFlagOn(pEntry
->lCase
, VFAT_CASE_LOWER_EXT
))
86 RtlDowncaseUnicodeString(NameU
, NameU
, FALSE
);
88 NameU
->Buffer
-= Length
/ sizeof(WCHAR
);
89 NameU
->Length
+= Length
;
90 NameU
->MaximumLength
+= Length
;
93 NameU
->Buffer
[NameU
->Length
/ sizeof(WCHAR
)] = 0;
94 DPRINT("'%wZ'\n", NameU
);
98 * FUNCTION: Find a file
102 PDEVICE_EXTENSION DeviceExt
,
104 PUNICODE_STRING FileToFindU
,
105 PVFAT_DIRENTRY_CONTEXT DirContext
,
108 PWCHAR PathNameBuffer
;
109 USHORT PathNameBufferLength
;
111 PVOID Context
= NULL
;
115 UNICODE_STRING PathNameU
;
116 UNICODE_STRING FileToFindUpcase
;
118 BOOLEAN IsFatX
= vfatVolumeIsFatX(DeviceExt
);
120 DPRINT("FindFile(Parent %p, FileToFind '%wZ', DirIndex: %u)\n",
121 Parent
, FileToFindU
, DirContext
->DirIndex
);
122 DPRINT("FindFile: Path %wZ\n",&Parent
->PathNameU
);
124 PathNameBufferLength
= LONGNAME_MAX_LENGTH
* sizeof(WCHAR
);
125 PathNameBuffer
= ExAllocatePoolWithTag(NonPagedPool
, PathNameBufferLength
+ sizeof(WCHAR
), TAG_NAME
);
128 return STATUS_INSUFFICIENT_RESOURCES
;
131 PathNameU
.Buffer
= PathNameBuffer
;
132 PathNameU
.Length
= 0;
133 PathNameU
.MaximumLength
= PathNameBufferLength
;
135 DirContext
->LongNameU
.Length
= 0;
136 DirContext
->ShortNameU
.Length
= 0;
138 WildCard
= FsRtlDoesNameContainWildCards(FileToFindU
);
140 if (WildCard
== FALSE
)
142 /* if there is no '*?' in the search name, than look first for an existing fcb */
143 RtlCopyUnicodeString(&PathNameU
, &Parent
->PathNameU
);
144 if (!vfatFCBIsRoot(Parent
))
146 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = L
'\\';
147 PathNameU
.Length
+= sizeof(WCHAR
);
149 RtlAppendUnicodeStringToString(&PathNameU
, FileToFindU
);
150 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = 0;
151 rcFcb
= vfatGrabFCBFromTable(DeviceExt
, &PathNameU
);
154 ULONG startIndex
= rcFcb
->startIndex
;
155 if (IsFatX
&& !vfatFCBIsRoot(Parent
))
159 if(startIndex
>= DirContext
->DirIndex
)
161 RtlCopyUnicodeString(&DirContext
->LongNameU
, &rcFcb
->LongNameU
);
162 RtlCopyUnicodeString(&DirContext
->ShortNameU
, &rcFcb
->ShortNameU
);
163 RtlCopyMemory(&DirContext
->DirEntry
, &rcFcb
->entry
, sizeof(DIR_ENTRY
));
164 DirContext
->StartIndex
= rcFcb
->startIndex
;
165 DirContext
->DirIndex
= rcFcb
->dirIndex
;
166 DPRINT("FindFile: new Name %wZ, DirIndex %u (%u)\n",
167 &DirContext
->LongNameU
, DirContext
->DirIndex
, DirContext
->StartIndex
);
168 Status
= STATUS_SUCCESS
;
172 DPRINT("FCB not found for %wZ\n", &PathNameU
);
173 Status
= STATUS_UNSUCCESSFUL
;
175 vfatReleaseFCB(DeviceExt
, rcFcb
);
176 ExFreePoolWithTag(PathNameBuffer
, TAG_NAME
);
181 /* FsRtlIsNameInExpression need the searched string to be upcase,
182 * even if IgnoreCase is specified */
183 Status
= RtlUpcaseUnicodeString(&FileToFindUpcase
, FileToFindU
, TRUE
);
184 if (!NT_SUCCESS(Status
))
186 ExFreePoolWithTag(PathNameBuffer
, TAG_NAME
);
192 Status
= VfatGetNextDirEntry(DeviceExt
, &Context
, &Page
, Parent
, DirContext
, First
);
194 if (Status
== STATUS_NO_MORE_ENTRIES
)
198 if (ENTRY_VOLUME(IsFatX
, &DirContext
->DirEntry
))
200 DirContext
->DirIndex
++;
203 if (DirContext
->LongNameU
.Length
== 0 ||
204 DirContext
->ShortNameU
.Length
== 0)
206 DPRINT1("WARNING: File system corruption detected. You may need to run a disk repair utility.\n");
207 if (VfatGlobalData
->Flags
& VFAT_BREAK_ON_CORRUPTION
)
209 ASSERT(DirContext
->LongNameU
.Length
!= 0 &&
210 DirContext
->ShortNameU
.Length
!= 0);
212 DirContext
->DirIndex
++;
217 Found
= FsRtlIsNameInExpression(&FileToFindUpcase
, &DirContext
->LongNameU
, TRUE
, NULL
) ||
218 FsRtlIsNameInExpression(&FileToFindUpcase
, &DirContext
->ShortNameU
, TRUE
, NULL
);
222 Found
= FsRtlAreNamesEqual(&DirContext
->LongNameU
, FileToFindU
, TRUE
, NULL
) ||
223 FsRtlAreNamesEqual(&DirContext
->ShortNameU
, FileToFindU
, TRUE
, NULL
);
230 RtlCopyUnicodeString(&PathNameU
, &Parent
->PathNameU
);
231 if (!vfatFCBIsRoot(Parent
))
233 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = L
'\\';
234 PathNameU
.Length
+= sizeof(WCHAR
);
236 RtlAppendUnicodeStringToString(&PathNameU
, &DirContext
->LongNameU
);
237 PathNameU
.Buffer
[PathNameU
.Length
/ sizeof(WCHAR
)] = 0;
238 rcFcb
= vfatGrabFCBFromTable(DeviceExt
, &PathNameU
);
241 RtlCopyMemory(&DirContext
->DirEntry
, &rcFcb
->entry
, sizeof(DIR_ENTRY
));
242 vfatReleaseFCB(DeviceExt
, rcFcb
);
245 DPRINT("%u\n", DirContext
->LongNameU
.Length
);
246 DPRINT("FindFile: new Name %wZ, DirIndex %u\n",
247 &DirContext
->LongNameU
, DirContext
->DirIndex
);
251 CcUnpinData(Context
);
253 RtlFreeUnicodeString(&FileToFindUpcase
);
254 ExFreePoolWithTag(PathNameBuffer
, TAG_NAME
);
255 return STATUS_SUCCESS
;
257 DirContext
->DirIndex
++;
262 CcUnpinData(Context
);
265 RtlFreeUnicodeString(&FileToFindUpcase
);
266 ExFreePoolWithTag(PathNameBuffer
, TAG_NAME
);
271 * FUNCTION: Opens a file
276 PDEVICE_EXTENSION DeviceExt
,
277 PUNICODE_STRING PathNameU
,
278 PFILE_OBJECT FileObject
,
279 ULONG RequestedDisposition
,
280 ULONG RequestedOptions
,
286 DPRINT("VfatOpenFile(%p, '%wZ', %p, %p)\n", DeviceExt
, PathNameU
, FileObject
, ParentFcb
);
288 if (FileObject
->RelatedFileObject
)
290 DPRINT("'%wZ'\n", &FileObject
->RelatedFileObject
->FileName
);
292 *ParentFcb
= FileObject
->RelatedFileObject
->FsContext
;
299 if (!DeviceExt
->FatInfo
.FixedMedia
)
301 Status
= VfatBlockDeviceIoControl(DeviceExt
->StorageDevice
,
302 IOCTL_DISK_CHECK_VERIFY
,
308 if (!NT_SUCCESS(Status
))
310 DPRINT("Status %lx\n", Status
);
318 vfatGrabFCB(DeviceExt
, *ParentFcb
);
321 /* try first to find an existing FCB in memory */
322 DPRINT("Checking for existing FCB in memory\n");
324 Status
= vfatGetFCBForFile(DeviceExt
, ParentFcb
, &Fcb
, PathNameU
);
325 if (!NT_SUCCESS(Status
))
327 DPRINT ("Could not make a new FCB, status: %x\n", Status
);
331 /* Fail, if we try to overwrite an existing directory */
332 if ((!BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) && vfatFCBIsDirectory(Fcb
)) &&
333 (RequestedDisposition
== FILE_OVERWRITE
||
334 RequestedDisposition
== FILE_OVERWRITE_IF
||
335 RequestedDisposition
== FILE_SUPERSEDE
))
337 vfatReleaseFCB(DeviceExt
, Fcb
);
338 return STATUS_OBJECT_NAME_COLLISION
;
341 if (BooleanFlagOn(Fcb
->Flags
, FCB_DELETE_PENDING
))
343 vfatReleaseFCB(DeviceExt
, Fcb
);
344 return STATUS_DELETE_PENDING
;
347 /* Fail, if we try to overwrite a read-only file */
348 if (vfatFCBIsReadOnly(Fcb
) &&
349 (RequestedDisposition
== FILE_OVERWRITE
||
350 RequestedDisposition
== FILE_OVERWRITE_IF
))
352 vfatReleaseFCB(DeviceExt
, Fcb
);
353 return STATUS_ACCESS_DENIED
;
356 if (vfatFCBIsReadOnly(Fcb
) &&
357 (RequestedOptions
& FILE_DELETE_ON_CLOSE
))
359 vfatReleaseFCB(DeviceExt
, Fcb
);
360 return STATUS_CANNOT_DELETE
;
363 if ((vfatFCBIsRoot(Fcb
) || IsDotOrDotDot(&Fcb
->LongNameU
)) &&
364 BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
366 // we cannot delete a '.', '..' or the root directory
367 vfatReleaseFCB(DeviceExt
, Fcb
);
368 return STATUS_CANNOT_DELETE
;
371 /* If that one was marked for closing, remove it */
372 if (BooleanFlagOn(Fcb
->Flags
, FCB_DELAYED_CLOSE
))
374 BOOLEAN ConcurrentDeletion
;
375 PVFAT_CLOSE_CONTEXT CloseContext
;
377 /* Get the context */
378 CloseContext
= Fcb
->CloseContext
;
379 /* Is someone already taking over? */
380 if (CloseContext
!= NULL
)
382 ConcurrentDeletion
= FALSE
;
384 ExAcquireFastMutex(&VfatGlobalData
->CloseMutex
);
385 /* Check whether it was already removed, if not, do it */
386 if (!IsListEmpty(&CloseContext
->CloseListEntry
))
388 RemoveEntryList(&CloseContext
->CloseListEntry
);
389 --VfatGlobalData
->CloseCount
;
390 ConcurrentDeletion
= TRUE
;
392 ExReleaseFastMutex(&VfatGlobalData
->CloseMutex
);
394 /* It's not delayed anymore! */
395 ClearFlag(Fcb
->Flags
, FCB_DELAYED_CLOSE
);
396 /* Release the extra reference (would have been removed by IRP_MJ_CLOSE) */
397 vfatReleaseFCB(DeviceExt
, Fcb
);
398 Fcb
->CloseContext
= NULL
;
399 /* If no concurrent deletion, free work item */
400 if (!ConcurrentDeletion
)
402 ExFreeToPagedLookasideList(&VfatGlobalData
->CloseContextLookasideList
, CloseContext
);
406 DPRINT("Reusing delayed close FCB for %wZ\n", &Fcb
->PathNameU
);
409 DPRINT("Attaching FCB to fileObject\n");
410 Status
= vfatAttachFCBToFileObject(DeviceExt
, Fcb
, FileObject
);
411 if (!NT_SUCCESS(Status
))
413 vfatReleaseFCB(DeviceExt
, Fcb
);
419 * FUNCTION: Create or open a file
423 PDEVICE_OBJECT DeviceObject
,
426 PIO_STACK_LOCATION Stack
;
427 PFILE_OBJECT FileObject
;
428 NTSTATUS Status
= STATUS_SUCCESS
;
429 PDEVICE_EXTENSION DeviceExt
;
430 ULONG RequestedDisposition
, RequestedOptions
;
431 PVFATFCB pFcb
= NULL
;
432 PVFATFCB ParentFcb
= NULL
;
433 PVFATCCB pCcb
= NULL
;
435 BOOLEAN PagingFileCreate
;
437 BOOLEAN OpenTargetDir
;
438 BOOLEAN TrailingBackslash
;
439 UNICODE_STRING FileNameU
;
440 UNICODE_STRING PathNameU
;
443 /* Unpack the various parameters. */
444 Stack
= IoGetCurrentIrpStackLocation(Irp
);
445 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
446 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
447 PagingFileCreate
= BooleanFlagOn(Stack
->Flags
, SL_OPEN_PAGING_FILE
);
448 OpenTargetDir
= BooleanFlagOn(Stack
->Flags
, SL_OPEN_TARGET_DIRECTORY
);
450 FileObject
= Stack
->FileObject
;
451 DeviceExt
= DeviceObject
->DeviceExtension
;
453 if (BooleanFlagOn(Stack
->Parameters
.Create
.Options
, FILE_OPEN_BY_FILE_ID
))
455 return STATUS_NOT_IMPLEMENTED
;
458 /* Check their validity. */
459 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
460 RequestedDisposition
== FILE_SUPERSEDE
)
462 return STATUS_INVALID_PARAMETER
;
465 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
466 BooleanFlagOn(RequestedOptions
, FILE_NON_DIRECTORY_FILE
))
468 return STATUS_INVALID_PARAMETER
;
471 /* Deny create if the volume is locked */
472 if (BooleanFlagOn(DeviceExt
->Flags
, VCB_VOLUME_LOCKED
))
474 return STATUS_ACCESS_DENIED
;
477 /* This a open operation for the volume itself */
478 if (FileObject
->FileName
.Length
== 0 &&
479 (FileObject
->RelatedFileObject
== NULL
||
480 FileObject
->RelatedFileObject
->FsContext2
!= NULL
||
481 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
))
483 DPRINT("Volume opening\n");
485 if (RequestedDisposition
!= FILE_OPEN
&&
486 RequestedDisposition
!= FILE_OPEN_IF
)
488 return STATUS_ACCESS_DENIED
;
491 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
492 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
== NULL
|| FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
))
494 return STATUS_NOT_A_DIRECTORY
;
499 return STATUS_INVALID_PARAMETER
;
502 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
504 return STATUS_CANNOT_DELETE
;
507 vfatAddToStat(DeviceExt
, Fat
.CreateHits
, 1);
509 pFcb
= DeviceExt
->VolumeFcb
;
511 if (pFcb
->OpenHandleCount
== 0)
513 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
514 Stack
->Parameters
.Create
.ShareAccess
,
516 &pFcb
->FCBShareAccess
);
520 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
521 Stack
->Parameters
.Create
.ShareAccess
,
523 &pFcb
->FCBShareAccess
,
525 if (!NT_SUCCESS(Status
))
527 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
532 vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
533 DeviceExt
->OpenHandleCount
++;
534 pFcb
->OpenHandleCount
++;
535 vfatAddToStat(DeviceExt
, Fat
.SuccessfulCreates
, 1);
537 Irp
->IoStatus
.Information
= FILE_OPENED
;
538 return STATUS_SUCCESS
;
541 if (FileObject
->RelatedFileObject
!= NULL
&&
542 FileObject
->RelatedFileObject
->FsContext
== DeviceExt
->VolumeFcb
)
544 ASSERT(FileObject
->FileName
.Length
!= 0);
545 return STATUS_OBJECT_PATH_NOT_FOUND
;
548 /* Check for illegal characters and illegal dot sequences in the file name */
549 PathNameU
= FileObject
->FileName
;
550 c
= PathNameU
.Buffer
+ PathNameU
.Length
/ sizeof(WCHAR
);
554 while (c
-- > PathNameU
.Buffer
)
556 if (*c
== L
'\\' || c
== PathNameU
.Buffer
)
558 if (Dots
&& last
> c
)
560 return STATUS_OBJECT_NAME_INVALID
;
562 if (*c
== L
'\\' && (c
- 1) > PathNameU
.Buffer
&&
565 return STATUS_OBJECT_NAME_INVALID
;
576 if (*c
!= '\\' && vfatIsLongIllegal(*c
))
578 return STATUS_OBJECT_NAME_INVALID
;
582 /* Check if we try to open target directory of root dir */
583 if (OpenTargetDir
&& FileObject
->RelatedFileObject
== NULL
&& PathNameU
.Length
== sizeof(WCHAR
) &&
584 PathNameU
.Buffer
[0] == L
'\\')
586 return STATUS_INVALID_PARAMETER
;
589 if (FileObject
->RelatedFileObject
&& PathNameU
.Length
>= sizeof(WCHAR
) && PathNameU
.Buffer
[0] == L
'\\')
591 return STATUS_OBJECT_NAME_INVALID
;
594 TrailingBackslash
= FALSE
;
595 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
597 PathNameU
.Length
-= sizeof(WCHAR
);
598 TrailingBackslash
= TRUE
;
601 if (PathNameU
.Length
> sizeof(WCHAR
) && PathNameU
.Buffer
[PathNameU
.Length
/sizeof(WCHAR
)-1] == L
'\\')
603 return STATUS_OBJECT_NAME_INVALID
;
606 /* Try opening the file. */
609 vfatAddToStat(DeviceExt
, Fat
.CreateHits
, 1);
611 Status
= VfatOpenFile(DeviceExt
, &PathNameU
, FileObject
, RequestedDisposition
, RequestedOptions
, &ParentFcb
);
616 LONG idx
, FileNameLen
;
618 vfatAddToStat(DeviceExt
, Fat
.CreateHits
, 1);
620 ParentFcb
= (FileObject
->RelatedFileObject
!= NULL
) ? FileObject
->RelatedFileObject
->FsContext
: NULL
;
623 vfatGrabFCB(DeviceExt
, ParentFcb
);
626 Status
= vfatGetFCBForFile(DeviceExt
, &ParentFcb
, &TargetFcb
, &PathNameU
);
627 if (NT_SUCCESS(Status
))
629 vfatReleaseFCB(DeviceExt
, TargetFcb
);
630 Irp
->IoStatus
.Information
= FILE_EXISTS
;
634 Irp
->IoStatus
.Information
= FILE_DOES_NOT_EXIST
;
637 idx
= FileObject
->FileName
.Length
/ sizeof(WCHAR
) - 1;
639 /* Skip trailing \ - if any */
640 if (PathNameU
.Buffer
[idx
] == L
'\\')
643 PathNameU
.Length
-= sizeof(WCHAR
);
647 while (idx
>= 0 && PathNameU
.Buffer
[idx
] != L
'\\')
652 if (idx
> 0 || PathNameU
.Buffer
[0] == L
'\\')
654 /* We don't want to include / in the name */
655 FileNameLen
= PathNameU
.Length
- ((idx
+ 1) * sizeof(WCHAR
));
657 /* Update FO just to keep file name */
658 /* Skip first slash */
660 FileObject
->FileName
.Length
= FileNameLen
;
661 RtlMoveMemory(&PathNameU
.Buffer
[0], &PathNameU
.Buffer
[idx
], FileObject
->FileName
.Length
);
663 /* Terminate the string at the last backslash */
664 PathNameU
.Buffer
[idx
+ 1] = UNICODE_NULL
;
665 PathNameU
.Length
= (idx
+ 1) * sizeof(WCHAR
);
666 PathNameU
.MaximumLength
= PathNameU
.Length
+ sizeof(WCHAR
);
668 /* Update the file object as well */
669 FileObject
->FileName
.Length
= PathNameU
.Length
;
670 FileObject
->FileName
.MaximumLength
= PathNameU
.MaximumLength
;
675 /* This is a relative open and we have only the filename, so open the parent directory
676 * It is in RelatedFileObject
678 ASSERT(FileObject
->RelatedFileObject
!= NULL
);
680 /* No need to modify the FO, it already has the name */
683 /* We're done with opening! */
684 if (ParentFcb
!= NULL
)
686 Status
= vfatAttachFCBToFileObject(DeviceExt
, ParentFcb
, FileObject
);
689 if (NT_SUCCESS(Status
))
691 pFcb
= FileObject
->FsContext
;
692 ASSERT(pFcb
== ParentFcb
);
694 if (pFcb
->OpenHandleCount
== 0)
696 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
697 Stack
->Parameters
.Create
.ShareAccess
,
699 &pFcb
->FCBShareAccess
);
703 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
704 Stack
->Parameters
.Create
.ShareAccess
,
706 &pFcb
->FCBShareAccess
,
708 if (!NT_SUCCESS(Status
))
710 VfatCloseFile(DeviceExt
, FileObject
);
711 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
716 pCcb
= FileObject
->FsContext2
;
717 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
719 pCcb
->Flags
|= CCB_DELETE_ON_CLOSE
;
722 pFcb
->OpenHandleCount
++;
723 DeviceExt
->OpenHandleCount
++;
725 else if (ParentFcb
!= NULL
)
727 vfatReleaseFCB(DeviceExt
, ParentFcb
);
730 if (NT_SUCCESS(Status
))
732 vfatAddToStat(DeviceExt
, Fat
.SuccessfulCreates
, 1);
736 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
743 * If the directory containing the file to open doesn't exist then
746 if (Status
== STATUS_OBJECT_PATH_NOT_FOUND
||
747 Status
== STATUS_INVALID_PARAMETER
||
748 Status
== STATUS_DELETE_PENDING
||
749 Status
== STATUS_ACCESS_DENIED
||
750 Status
== STATUS_OBJECT_NAME_COLLISION
)
754 vfatReleaseFCB(DeviceExt
, ParentFcb
);
756 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
760 if (!NT_SUCCESS(Status
) && ParentFcb
== NULL
)
762 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU
, Status
);
763 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
767 Attributes
= (Stack
->Parameters
.Create
.FileAttributes
& (FILE_ATTRIBUTE_ARCHIVE
|
768 FILE_ATTRIBUTE_SYSTEM
|
769 FILE_ATTRIBUTE_HIDDEN
|
770 FILE_ATTRIBUTE_DIRECTORY
|
771 FILE_ATTRIBUTE_READONLY
));
773 /* If the file open failed then create the required file */
774 if (!NT_SUCCESS (Status
))
776 if (RequestedDisposition
== FILE_CREATE
||
777 RequestedDisposition
== FILE_OPEN_IF
||
778 RequestedDisposition
== FILE_OVERWRITE_IF
||
779 RequestedDisposition
== FILE_SUPERSEDE
)
781 if (!BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
))
783 if (TrailingBackslash
)
785 vfatReleaseFCB(DeviceExt
, ParentFcb
);
786 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
787 return STATUS_OBJECT_NAME_INVALID
;
789 Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
791 vfatSplitPathName(&PathNameU
, NULL
, &FileNameU
);
793 if (IsDotOrDotDot(&FileNameU
))
795 vfatReleaseFCB(DeviceExt
, ParentFcb
);
796 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
797 return STATUS_OBJECT_NAME_INVALID
;
799 Status
= VfatAddEntry(DeviceExt
, &FileNameU
, &pFcb
, ParentFcb
, RequestedOptions
,
801 vfatReleaseFCB(DeviceExt
, ParentFcb
);
802 if (NT_SUCCESS(Status
))
804 Status
= vfatAttachFCBToFileObject(DeviceExt
, pFcb
, FileObject
);
805 if (!NT_SUCCESS(Status
))
807 vfatReleaseFCB(DeviceExt
, pFcb
);
808 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
812 Irp
->IoStatus
.Information
= FILE_CREATED
;
813 VfatSetAllocationSizeInformation(FileObject
,
816 &Irp
->Overlay
.AllocationSize
);
817 VfatSetExtendedAttributes(FileObject
,
818 Irp
->AssociatedIrp
.SystemBuffer
,
819 Stack
->Parameters
.Create
.EaLength
);
821 if (PagingFileCreate
)
823 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
824 SetFlag(DeviceExt
->Flags
, VCB_IS_SYS_OR_HAS_PAGE
);
829 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
835 vfatReleaseFCB(DeviceExt
, ParentFcb
);
836 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
844 vfatReleaseFCB(DeviceExt
, ParentFcb
);
847 pFcb
= FileObject
->FsContext
;
849 /* Otherwise fail if the caller wanted to create a new file */
850 if (RequestedDisposition
== FILE_CREATE
)
852 VfatCloseFile(DeviceExt
, FileObject
);
853 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
855 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
856 return STATUS_OBJECT_NAME_INVALID
;
858 Irp
->IoStatus
.Information
= FILE_EXISTS
;
859 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
860 return STATUS_OBJECT_NAME_COLLISION
;
863 if (pFcb
->OpenHandleCount
!= 0)
865 Status
= IoCheckShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
866 Stack
->Parameters
.Create
.ShareAccess
,
868 &pFcb
->FCBShareAccess
,
870 if (!NT_SUCCESS(Status
))
872 VfatCloseFile(DeviceExt
, FileObject
);
873 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
879 * Check the file has the requested attributes
881 if (BooleanFlagOn(RequestedOptions
, FILE_NON_DIRECTORY_FILE
) &&
882 vfatFCBIsDirectory(pFcb
))
884 VfatCloseFile (DeviceExt
, FileObject
);
885 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
886 return STATUS_FILE_IS_A_DIRECTORY
;
888 if (BooleanFlagOn(RequestedOptions
, FILE_DIRECTORY_FILE
) &&
889 !vfatFCBIsDirectory(pFcb
))
891 VfatCloseFile (DeviceExt
, FileObject
);
892 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
893 return STATUS_NOT_A_DIRECTORY
;
895 if (TrailingBackslash
&& !vfatFCBIsDirectory(pFcb
))
897 VfatCloseFile (DeviceExt
, FileObject
);
898 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
899 return STATUS_OBJECT_NAME_INVALID
;
901 #ifndef USE_ROS_CC_AND_FS
902 if (!vfatFCBIsDirectory(pFcb
))
904 if (BooleanFlagOn(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
, FILE_WRITE_DATA
) ||
905 RequestedDisposition
== FILE_OVERWRITE
||
906 RequestedDisposition
== FILE_OVERWRITE_IF
||
907 (RequestedOptions
& FILE_DELETE_ON_CLOSE
))
909 if (!MmFlushImageSection(&pFcb
->SectionObjectPointers
, MmFlushForWrite
))
911 DPRINT1("%wZ\n", &pFcb
->PathNameU
);
912 DPRINT1("%d %d %d\n", Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
& FILE_WRITE_DATA
,
913 RequestedDisposition
== FILE_OVERWRITE
, RequestedDisposition
== FILE_OVERWRITE_IF
);
914 VfatCloseFile (DeviceExt
, FileObject
);
915 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
916 return (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
)) ? STATUS_CANNOT_DELETE
917 : STATUS_SHARING_VIOLATION
;
922 if (PagingFileCreate
)
925 * Do more checking for page files. It is possible,
926 * that the file was opened and closed previously
927 * as a normal cached file. In this case, the cache
928 * manager has referenced the fileobject and the fcb
929 * is held in memory. Try to remove the fileobject
930 * from cache manager and use the fcb.
932 if (pFcb
->RefCount
> 1)
934 if(!BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
936 VfatCloseFile(DeviceExt
, FileObject
);
937 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
938 return STATUS_INVALID_PARAMETER
;
943 pFcb
->Flags
|= FCB_IS_PAGE_FILE
;
944 SetFlag(DeviceExt
->Flags
, VCB_IS_SYS_OR_HAS_PAGE
);
949 if (BooleanFlagOn(pFcb
->Flags
, FCB_IS_PAGE_FILE
))
951 VfatCloseFile(DeviceExt
, FileObject
);
952 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
953 return STATUS_INVALID_PARAMETER
;
957 if (RequestedDisposition
== FILE_OVERWRITE
||
958 RequestedDisposition
== FILE_OVERWRITE_IF
||
959 RequestedDisposition
== FILE_SUPERSEDE
)
961 if ((BooleanFlagOn(*pFcb
->Attributes
, FILE_ATTRIBUTE_HIDDEN
) && !BooleanFlagOn(Attributes
, FILE_ATTRIBUTE_HIDDEN
)) ||
962 (BooleanFlagOn(*pFcb
->Attributes
, FILE_ATTRIBUTE_SYSTEM
) && !BooleanFlagOn(Attributes
, FILE_ATTRIBUTE_SYSTEM
)))
964 VfatCloseFile(DeviceExt
, FileObject
);
965 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
966 return STATUS_ACCESS_DENIED
;
969 if (!vfatFCBIsDirectory(pFcb
))
971 LARGE_INTEGER SystemTime
;
973 if (RequestedDisposition
== FILE_SUPERSEDE
)
975 *pFcb
->Attributes
= Attributes
;
979 *pFcb
->Attributes
|= Attributes
;
981 *pFcb
->Attributes
|= FILE_ATTRIBUTE_ARCHIVE
;
983 KeQuerySystemTime(&SystemTime
);
984 if (vfatVolumeIsFatX(DeviceExt
))
986 FsdSystemTimeToDosDateTime(DeviceExt
,
987 &SystemTime
, &pFcb
->entry
.FatX
.UpdateDate
,
988 &pFcb
->entry
.FatX
.UpdateTime
);
992 FsdSystemTimeToDosDateTime(DeviceExt
,
993 &SystemTime
, &pFcb
->entry
.Fat
.UpdateDate
,
994 &pFcb
->entry
.Fat
.UpdateTime
);
997 VfatUpdateEntry(DeviceExt
, pFcb
);
1000 ExAcquireResourceExclusiveLite(&(pFcb
->MainResource
), TRUE
);
1001 Status
= VfatSetAllocationSizeInformation(FileObject
,
1004 &Irp
->Overlay
.AllocationSize
);
1005 ExReleaseResourceLite(&(pFcb
->MainResource
));
1006 if (!NT_SUCCESS (Status
))
1008 VfatCloseFile(DeviceExt
, FileObject
);
1009 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
1014 if (RequestedDisposition
== FILE_SUPERSEDE
)
1016 Irp
->IoStatus
.Information
= FILE_SUPERSEDED
;
1018 else if (RequestedDisposition
== FILE_OVERWRITE
||
1019 RequestedDisposition
== FILE_OVERWRITE_IF
)
1021 Irp
->IoStatus
.Information
= FILE_OVERWRITTEN
;
1025 Irp
->IoStatus
.Information
= FILE_OPENED
;
1029 if (pFcb
->OpenHandleCount
== 0)
1031 IoSetShareAccess(Stack
->Parameters
.Create
.SecurityContext
->DesiredAccess
,
1032 Stack
->Parameters
.Create
.ShareAccess
,
1034 &pFcb
->FCBShareAccess
);
1038 IoUpdateShareAccess(FileObject
,
1039 &pFcb
->FCBShareAccess
);
1042 pCcb
= FileObject
->FsContext2
;
1043 if (BooleanFlagOn(RequestedOptions
, FILE_DELETE_ON_CLOSE
))
1045 pCcb
->Flags
|= CCB_DELETE_ON_CLOSE
;
1048 if (Irp
->IoStatus
.Information
== FILE_CREATED
)
1050 vfatReportChange(DeviceExt
,
1052 (vfatFCBIsDirectory(pFcb
) ?
1053 FILE_NOTIFY_CHANGE_DIR_NAME
: FILE_NOTIFY_CHANGE_FILE_NAME
),
1056 else if (Irp
->IoStatus
.Information
== FILE_OVERWRITTEN
||
1057 Irp
->IoStatus
.Information
== FILE_SUPERSEDED
)
1059 vfatReportChange(DeviceExt
,
1061 FILE_NOTIFY_CHANGE_LAST_WRITE
| FILE_NOTIFY_CHANGE_ATTRIBUTES
| FILE_NOTIFY_CHANGE_SIZE
,
1062 FILE_ACTION_MODIFIED
);
1065 pFcb
->OpenHandleCount
++;
1066 DeviceExt
->OpenHandleCount
++;
1068 /* FIXME : test write access if requested */
1070 /* FIXME: That is broken, we cannot reach this code path with failure */
1071 ASSERT(NT_SUCCESS(Status
));
1072 if (NT_SUCCESS(Status
))
1074 vfatAddToStat(DeviceExt
, Fat
.SuccessfulCreates
, 1);
1078 vfatAddToStat(DeviceExt
, Fat
.FailedCreates
, 1);
1085 * FUNCTION: Create or open a file
1089 PVFAT_IRP_CONTEXT IrpContext
)
1095 if (IrpContext
->DeviceObject
== VfatGlobalData
->DeviceObject
)
1097 /* DeviceObject represents FileSystem instead of logical volume */
1098 DPRINT ("FsdCreate called with file system\n");
1099 IrpContext
->Irp
->IoStatus
.Information
= FILE_OPENED
;
1100 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;
1102 return STATUS_SUCCESS
;
1105 IrpContext
->Irp
->IoStatus
.Information
= 0;
1106 ExAcquireResourceExclusiveLite(&IrpContext
->DeviceExt
->DirResource
, TRUE
);
1107 Status
= VfatCreateFile(IrpContext
->DeviceObject
, IrpContext
->Irp
);
1108 ExReleaseResourceLite(&IrpContext
->DeviceExt
->DirResource
);
1110 if (NT_SUCCESS(Status
))
1111 IrpContext
->PriorityBoost
= IO_DISK_INCREMENT
;