[FASTFAT] Finally drop the TAG_VFAT allocation tag
[reactos.git] / 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/filesystems/fastfat/create.c
22 * PURPOSE: VFAT Filesystem
23 * PROGRAMMER: Jason Filby (jasonfilby@yahoo.com)
24 * Pierre Schweitzer (pierre@reactos.org)
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "vfat.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 /* FUNCTIONS *****************************************************************/
35
36 VOID
37 vfat8Dot3ToString(
38 PFAT_DIR_ENTRY pEntry,
39 PUNICODE_STRING NameU)
40 {
41 OEM_STRING StringA;
42 USHORT Length;
43 CHAR cString[12];
44
45 RtlCopyMemory(cString, pEntry->ShortName, 11);
46 cString[11] = 0;
47 if (cString[0] == 0x05)
48 {
49 cString[0] = 0xe5;
50 }
51
52 StringA.Buffer = cString;
53 for (StringA.Length = 0;
54 StringA.Length < 8 && StringA.Buffer[StringA.Length] != ' ';
55 StringA.Length++);
56 StringA.MaximumLength = StringA.Length;
57
58 RtlOemStringToUnicodeString(NameU, &StringA, FALSE);
59
60 if (BooleanFlagOn(pEntry->lCase, VFAT_CASE_LOWER_BASE))
61 {
62 RtlDowncaseUnicodeString(NameU, NameU, FALSE);
63 }
64
65 if (cString[8] != ' ')
66 {
67 Length = NameU->Length;
68 NameU->Buffer += Length / sizeof(WCHAR);
69 if (!FAT_ENTRY_VOLUME(pEntry))
70 {
71 Length += sizeof(WCHAR);
72 NameU->Buffer[0] = L'.';
73 NameU->Buffer++;
74 }
75 NameU->Length = 0;
76 NameU->MaximumLength -= Length;
77
78 StringA.Buffer = &cString[8];
79 for (StringA.Length = 0;
80 StringA.Length < 3 && StringA.Buffer[StringA.Length] != ' ';
81 StringA.Length++);
82 StringA.MaximumLength = StringA.Length;
83 RtlOemStringToUnicodeString(NameU, &StringA, FALSE);
84 if (BooleanFlagOn(pEntry->lCase, VFAT_CASE_LOWER_EXT))
85 {
86 RtlDowncaseUnicodeString(NameU, NameU, FALSE);
87 }
88 NameU->Buffer -= Length / sizeof(WCHAR);
89 NameU->Length += Length;
90 NameU->MaximumLength += Length;
91 }
92
93 NameU->Buffer[NameU->Length / sizeof(WCHAR)] = 0;
94 DPRINT("'%wZ'\n", NameU);
95 }
96
97 /*
98 * FUNCTION: Find a file
99 */
100 NTSTATUS
101 FindFile(
102 PDEVICE_EXTENSION DeviceExt,
103 PVFATFCB Parent,
104 PUNICODE_STRING FileToFindU,
105 PVFAT_DIRENTRY_CONTEXT DirContext,
106 BOOLEAN First)
107 {
108 PWCHAR PathNameBuffer;
109 USHORT PathNameBufferLength;
110 NTSTATUS Status;
111 PVOID Context = NULL;
112 PVOID Page;
113 PVFATFCB rcFcb;
114 BOOLEAN Found;
115 UNICODE_STRING PathNameU;
116 UNICODE_STRING FileToFindUpcase;
117 BOOLEAN WildCard;
118 BOOLEAN IsFatX = vfatVolumeIsFatX(DeviceExt);
119
120 DPRINT("FindFile(Parent %p, FileToFind '%wZ', DirIndex: %u)\n",
121 Parent, FileToFindU, DirContext->DirIndex);
122 DPRINT("FindFile: Path %wZ\n",&Parent->PathNameU);
123
124 PathNameBufferLength = LONGNAME_MAX_LENGTH * sizeof(WCHAR);
125 PathNameBuffer = ExAllocatePoolWithTag(NonPagedPool, PathNameBufferLength + sizeof(WCHAR), TAG_NAME);
126 if (!PathNameBuffer)
127 {
128 return STATUS_INSUFFICIENT_RESOURCES;
129 }
130
131 PathNameU.Buffer = PathNameBuffer;
132 PathNameU.Length = 0;
133 PathNameU.MaximumLength = PathNameBufferLength;
134
135 DirContext->LongNameU.Length = 0;
136 DirContext->ShortNameU.Length = 0;
137
138 WildCard = FsRtlDoesNameContainWildCards(FileToFindU);
139
140 if (WildCard == FALSE)
141 {
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))
145 {
146 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = L'\\';
147 PathNameU.Length += sizeof(WCHAR);
148 }
149 RtlAppendUnicodeStringToString(&PathNameU, FileToFindU);
150 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = 0;
151 rcFcb = vfatGrabFCBFromTable(DeviceExt, &PathNameU);
152 if (rcFcb)
153 {
154 ULONG startIndex = rcFcb->startIndex;
155 if (IsFatX && !vfatFCBIsRoot(Parent))
156 {
157 startIndex += 2;
158 }
159 if(startIndex >= DirContext->DirIndex)
160 {
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;
169 }
170 else
171 {
172 DPRINT("FCB not found for %wZ\n", &PathNameU);
173 Status = STATUS_UNSUCCESSFUL;
174 }
175 vfatReleaseFCB(DeviceExt, rcFcb);
176 ExFreePoolWithTag(PathNameBuffer, TAG_NAME);
177 return Status;
178 }
179 }
180
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))
185 {
186 ExFreePoolWithTag(PathNameBuffer, TAG_NAME);
187 return Status;
188 }
189
190 while (TRUE)
191 {
192 Status = VfatGetNextDirEntry(DeviceExt, &Context, &Page, Parent, DirContext, First);
193 First = FALSE;
194 if (Status == STATUS_NO_MORE_ENTRIES)
195 {
196 break;
197 }
198 if (ENTRY_VOLUME(IsFatX, &DirContext->DirEntry))
199 {
200 DirContext->DirIndex++;
201 continue;
202 }
203 if (DirContext->LongNameU.Length == 0 ||
204 DirContext->ShortNameU.Length == 0)
205 {
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)
208 {
209 ASSERT(DirContext->LongNameU.Length != 0 &&
210 DirContext->ShortNameU.Length != 0);
211 }
212 DirContext->DirIndex++;
213 continue;
214 }
215 if (WildCard)
216 {
217 Found = FsRtlIsNameInExpression(&FileToFindUpcase, &DirContext->LongNameU, TRUE, NULL) ||
218 FsRtlIsNameInExpression(&FileToFindUpcase, &DirContext->ShortNameU, TRUE, NULL);
219 }
220 else
221 {
222 Found = FsRtlAreNamesEqual(&DirContext->LongNameU, FileToFindU, TRUE, NULL) ||
223 FsRtlAreNamesEqual(&DirContext->ShortNameU, FileToFindU, TRUE, NULL);
224 }
225
226 if (Found)
227 {
228 if (WildCard)
229 {
230 RtlCopyUnicodeString(&PathNameU, &Parent->PathNameU);
231 if (!vfatFCBIsRoot(Parent))
232 {
233 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = L'\\';
234 PathNameU.Length += sizeof(WCHAR);
235 }
236 RtlAppendUnicodeStringToString(&PathNameU, &DirContext->LongNameU);
237 PathNameU.Buffer[PathNameU.Length / sizeof(WCHAR)] = 0;
238 rcFcb = vfatGrabFCBFromTable(DeviceExt, &PathNameU);
239 if (rcFcb != NULL)
240 {
241 RtlCopyMemory(&DirContext->DirEntry, &rcFcb->entry, sizeof(DIR_ENTRY));
242 vfatReleaseFCB(DeviceExt, rcFcb);
243 }
244 }
245 DPRINT("%u\n", DirContext->LongNameU.Length);
246 DPRINT("FindFile: new Name %wZ, DirIndex %u\n",
247 &DirContext->LongNameU, DirContext->DirIndex);
248
249 if (Context)
250 {
251 CcUnpinData(Context);
252 }
253 RtlFreeUnicodeString(&FileToFindUpcase);
254 ExFreePoolWithTag(PathNameBuffer, TAG_NAME);
255 return STATUS_SUCCESS;
256 }
257 DirContext->DirIndex++;
258 }
259
260 if (Context)
261 {
262 CcUnpinData(Context);
263 }
264
265 RtlFreeUnicodeString(&FileToFindUpcase);
266 ExFreePoolWithTag(PathNameBuffer, TAG_NAME);
267 return Status;
268 }
269
270 /*
271 * FUNCTION: Opens a file
272 */
273 static
274 NTSTATUS
275 VfatOpenFile(
276 PDEVICE_EXTENSION DeviceExt,
277 PUNICODE_STRING PathNameU,
278 PFILE_OBJECT FileObject,
279 ULONG RequestedDisposition,
280 ULONG RequestedOptions,
281 PVFATFCB *ParentFcb)
282 {
283 PVFATFCB Fcb;
284 NTSTATUS Status;
285
286 DPRINT("VfatOpenFile(%p, '%wZ', %p, %p)\n", DeviceExt, PathNameU, FileObject, ParentFcb);
287
288 if (FileObject->RelatedFileObject)
289 {
290 DPRINT("'%wZ'\n", &FileObject->RelatedFileObject->FileName);
291
292 *ParentFcb = FileObject->RelatedFileObject->FsContext;
293 }
294 else
295 {
296 *ParentFcb = NULL;
297 }
298
299 if (!DeviceExt->FatInfo.FixedMedia)
300 {
301 Status = VfatBlockDeviceIoControl(DeviceExt->StorageDevice,
302 IOCTL_DISK_CHECK_VERIFY,
303 NULL,
304 0,
305 NULL,
306 0,
307 FALSE);
308 if (!NT_SUCCESS(Status))
309 {
310 DPRINT("Status %lx\n", Status);
311 *ParentFcb = NULL;
312 return Status;
313 }
314 }
315
316 if (*ParentFcb)
317 {
318 vfatGrabFCB(DeviceExt, *ParentFcb);
319 }
320
321 /* try first to find an existing FCB in memory */
322 DPRINT("Checking for existing FCB in memory\n");
323
324 Status = vfatGetFCBForFile(DeviceExt, ParentFcb, &Fcb, PathNameU);
325 if (!NT_SUCCESS(Status))
326 {
327 DPRINT ("Could not make a new FCB, status: %x\n", Status);
328 return Status;
329 }
330
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))
336 {
337 vfatReleaseFCB(DeviceExt, Fcb);
338 return STATUS_OBJECT_NAME_COLLISION;
339 }
340
341 if (BooleanFlagOn(Fcb->Flags, FCB_DELETE_PENDING))
342 {
343 vfatReleaseFCB(DeviceExt, Fcb);
344 return STATUS_DELETE_PENDING;
345 }
346
347 /* Fail, if we try to overwrite a read-only file */
348 if (vfatFCBIsReadOnly(Fcb) &&
349 (RequestedDisposition == FILE_OVERWRITE ||
350 RequestedDisposition == FILE_OVERWRITE_IF))
351 {
352 vfatReleaseFCB(DeviceExt, Fcb);
353 return STATUS_ACCESS_DENIED;
354 }
355
356 if (vfatFCBIsReadOnly(Fcb) &&
357 (RequestedOptions & FILE_DELETE_ON_CLOSE))
358 {
359 vfatReleaseFCB(DeviceExt, Fcb);
360 return STATUS_CANNOT_DELETE;
361 }
362
363 if ((vfatFCBIsRoot(Fcb) ||
364 (Fcb->LongNameU.Length == sizeof(WCHAR) && Fcb->LongNameU.Buffer[0] == L'.') ||
365 (Fcb->LongNameU.Length == 2 * sizeof(WCHAR) && Fcb->LongNameU.Buffer[0] == L'.' && Fcb->LongNameU.Buffer[1] == L'.')) &&
366 BooleanFlagOn(RequestedOptions, FILE_DELETE_ON_CLOSE))
367 {
368 // we cannot delete a '.', '..' or the root directory
369 vfatReleaseFCB(DeviceExt, Fcb);
370 return STATUS_CANNOT_DELETE;
371 }
372
373 /* If that one was marked for closing, remove it */
374 if (BooleanFlagOn(Fcb->Flags, FCB_DELAYED_CLOSE))
375 {
376 BOOLEAN ConcurrentDeletion;
377 PVFAT_CLOSE_CONTEXT CloseContext;
378
379 /* Get the context */
380 CloseContext = Fcb->CloseContext;
381 /* Is someone already taking over? */
382 if (CloseContext != NULL)
383 {
384 ConcurrentDeletion = FALSE;
385 /* Lock list */
386 ExAcquireFastMutex(&VfatGlobalData->CloseMutex);
387 /* Check whether it was already removed, if not, do it */
388 if (!IsListEmpty(&CloseContext->CloseListEntry))
389 {
390 RemoveEntryList(&CloseContext->CloseListEntry);
391 --VfatGlobalData->CloseCount;
392 ConcurrentDeletion = TRUE;
393 }
394 ExReleaseFastMutex(&VfatGlobalData->CloseMutex);
395
396 /* It's not delayed anymore! */
397 ClearFlag(Fcb->Flags, FCB_DELAYED_CLOSE);
398 /* Release the extra reference (would have been removed by IRP_MJ_CLOSE) */
399 vfatReleaseFCB(DeviceExt, Fcb);
400 Fcb->CloseContext = NULL;
401 /* If no concurrent deletion, free work item */
402 if (!ConcurrentDeletion)
403 {
404 ExFreeToPagedLookasideList(&VfatGlobalData->CloseContextLookasideList, CloseContext);
405 }
406 }
407
408 DPRINT("Reusing delayed close FCB for %wZ\n", &Fcb->PathNameU);
409 }
410
411 DPRINT("Attaching FCB to fileObject\n");
412 Status = vfatAttachFCBToFileObject(DeviceExt, Fcb, FileObject);
413 if (!NT_SUCCESS(Status))
414 {
415 vfatReleaseFCB(DeviceExt, Fcb);
416 }
417 return Status;
418 }
419
420 /*
421 * FUNCTION: Create or open a file
422 */
423 static NTSTATUS
424 VfatCreateFile(
425 PDEVICE_OBJECT DeviceObject,
426 PIRP Irp)
427 {
428 PIO_STACK_LOCATION Stack;
429 PFILE_OBJECT FileObject;
430 NTSTATUS Status = STATUS_SUCCESS;
431 PDEVICE_EXTENSION DeviceExt;
432 ULONG RequestedDisposition, RequestedOptions;
433 PVFATFCB pFcb = NULL;
434 PVFATFCB ParentFcb = NULL;
435 PVFATCCB pCcb = NULL;
436 PWCHAR c, last;
437 BOOLEAN PagingFileCreate;
438 BOOLEAN Dots;
439 BOOLEAN OpenTargetDir;
440 BOOLEAN TrailingBackslash;
441 UNICODE_STRING FileNameU;
442 UNICODE_STRING PathNameU;
443 ULONG Attributes;
444
445 /* Unpack the various parameters. */
446 Stack = IoGetCurrentIrpStackLocation(Irp);
447 RequestedDisposition = ((Stack->Parameters.Create.Options >> 24) & 0xff);
448 RequestedOptions = Stack->Parameters.Create.Options & FILE_VALID_OPTION_FLAGS;
449 PagingFileCreate = BooleanFlagOn(Stack->Flags, SL_OPEN_PAGING_FILE);
450 OpenTargetDir = BooleanFlagOn(Stack->Flags, SL_OPEN_TARGET_DIRECTORY);
451
452 FileObject = Stack->FileObject;
453 DeviceExt = DeviceObject->DeviceExtension;
454
455 if (BooleanFlagOn(Stack->Parameters.Create.Options, FILE_OPEN_BY_FILE_ID))
456 {
457 return STATUS_NOT_IMPLEMENTED;
458 }
459
460 /* Check their validity. */
461 if (BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE) &&
462 RequestedDisposition == FILE_SUPERSEDE)
463 {
464 return STATUS_INVALID_PARAMETER;
465 }
466
467 if (BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE) &&
468 BooleanFlagOn(RequestedOptions, FILE_NON_DIRECTORY_FILE))
469 {
470 return STATUS_INVALID_PARAMETER;
471 }
472
473 /* Deny create if the volume is locked */
474 if (BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED))
475 {
476 return STATUS_ACCESS_DENIED;
477 }
478
479 /* This a open operation for the volume itself */
480 if (FileObject->FileName.Length == 0 &&
481 (FileObject->RelatedFileObject == NULL ||
482 FileObject->RelatedFileObject->FsContext2 != NULL ||
483 FileObject->RelatedFileObject->FsContext == DeviceExt->VolumeFcb))
484 {
485 DPRINT("Volume opening\n");
486
487 if (RequestedDisposition != FILE_OPEN &&
488 RequestedDisposition != FILE_OPEN_IF)
489 {
490 return STATUS_ACCESS_DENIED;
491 }
492
493 if (BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE) &&
494 (FileObject->RelatedFileObject == NULL || FileObject->RelatedFileObject->FsContext2 == NULL || FileObject->RelatedFileObject->FsContext == DeviceExt->VolumeFcb))
495 {
496 return STATUS_NOT_A_DIRECTORY;
497 }
498
499 if (OpenTargetDir)
500 {
501 return STATUS_INVALID_PARAMETER;
502 }
503
504 if (BooleanFlagOn(RequestedOptions, FILE_DELETE_ON_CLOSE))
505 {
506 return STATUS_CANNOT_DELETE;
507 }
508
509 vfatAddToStat(DeviceExt, Fat.CreateHits, 1);
510
511 pFcb = DeviceExt->VolumeFcb;
512
513 if (pFcb->OpenHandleCount == 0)
514 {
515 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
516 Stack->Parameters.Create.ShareAccess,
517 FileObject,
518 &pFcb->FCBShareAccess);
519 }
520 else
521 {
522 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
523 Stack->Parameters.Create.ShareAccess,
524 FileObject,
525 &pFcb->FCBShareAccess,
526 FALSE);
527 if (!NT_SUCCESS(Status))
528 {
529 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
530 return Status;
531 }
532 }
533
534 vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
535 DeviceExt->OpenHandleCount++;
536 pFcb->OpenHandleCount++;
537 vfatAddToStat(DeviceExt, Fat.SuccessfulCreates, 1);
538
539 Irp->IoStatus.Information = FILE_OPENED;
540 return STATUS_SUCCESS;
541 }
542
543 if (FileObject->RelatedFileObject != NULL &&
544 FileObject->RelatedFileObject->FsContext == DeviceExt->VolumeFcb)
545 {
546 ASSERT(FileObject->FileName.Length != 0);
547 return STATUS_OBJECT_PATH_NOT_FOUND;
548 }
549
550 /* Check for illegal characters and illegal dot sequences in the file name */
551 PathNameU = FileObject->FileName;
552 c = PathNameU.Buffer + PathNameU.Length / sizeof(WCHAR);
553 last = c - 1;
554
555 Dots = TRUE;
556 while (c-- > PathNameU.Buffer)
557 {
558 if (*c == L'\\' || c == PathNameU.Buffer)
559 {
560 if (Dots && last > c)
561 {
562 return STATUS_OBJECT_NAME_INVALID;
563 }
564 if (*c == L'\\' && (c - 1) > PathNameU.Buffer &&
565 *(c - 1) == L'\\')
566 {
567 return STATUS_OBJECT_NAME_INVALID;
568 }
569
570 last = c - 1;
571 Dots = TRUE;
572 }
573 else if (*c != L'.')
574 {
575 Dots = FALSE;
576 }
577
578 if (*c != '\\' && vfatIsLongIllegal(*c))
579 {
580 return STATUS_OBJECT_NAME_INVALID;
581 }
582 }
583
584 /* Check if we try to open target directory of root dir */
585 if (OpenTargetDir && FileObject->RelatedFileObject == NULL && PathNameU.Length == sizeof(WCHAR) &&
586 PathNameU.Buffer[0] == L'\\')
587 {
588 return STATUS_INVALID_PARAMETER;
589 }
590
591 if (FileObject->RelatedFileObject && PathNameU.Length >= sizeof(WCHAR) && PathNameU.Buffer[0] == L'\\')
592 {
593 return STATUS_OBJECT_NAME_INVALID;
594 }
595
596 TrailingBackslash = FALSE;
597 if (PathNameU.Length > sizeof(WCHAR) && PathNameU.Buffer[PathNameU.Length/sizeof(WCHAR)-1] == L'\\')
598 {
599 PathNameU.Length -= sizeof(WCHAR);
600 TrailingBackslash = TRUE;
601 }
602
603 if (PathNameU.Length > sizeof(WCHAR) && PathNameU.Buffer[PathNameU.Length/sizeof(WCHAR)-1] == L'\\')
604 {
605 return STATUS_OBJECT_NAME_INVALID;
606 }
607
608 /* Try opening the file. */
609 if (!OpenTargetDir)
610 {
611 vfatAddToStat(DeviceExt, Fat.CreateHits, 1);
612
613 Status = VfatOpenFile(DeviceExt, &PathNameU, FileObject, RequestedDisposition, RequestedOptions, &ParentFcb);
614 }
615 else
616 {
617 PVFATFCB TargetFcb;
618 LONG idx, FileNameLen;
619
620 vfatAddToStat(DeviceExt, Fat.CreateHits, 1);
621
622 ParentFcb = (FileObject->RelatedFileObject != NULL) ? FileObject->RelatedFileObject->FsContext : NULL;
623 if (ParentFcb)
624 {
625 vfatGrabFCB(DeviceExt, ParentFcb);
626 }
627
628 Status = vfatGetFCBForFile(DeviceExt, &ParentFcb, &TargetFcb, &PathNameU);
629 if (NT_SUCCESS(Status))
630 {
631 vfatReleaseFCB(DeviceExt, TargetFcb);
632 Irp->IoStatus.Information = FILE_EXISTS;
633 }
634 else
635 {
636 Irp->IoStatus.Information = FILE_DOES_NOT_EXIST;
637 }
638
639 idx = FileObject->FileName.Length / sizeof(WCHAR) - 1;
640
641 /* Skip trailing \ - if any */
642 if (PathNameU.Buffer[idx] == L'\\')
643 {
644 --idx;
645 PathNameU.Length -= sizeof(WCHAR);
646 }
647
648 /* Get file name */
649 while (idx >= 0 && PathNameU.Buffer[idx] != L'\\')
650 {
651 --idx;
652 }
653
654 if (idx > 0 || PathNameU.Buffer[0] == L'\\')
655 {
656 /* We don't want to include / in the name */
657 FileNameLen = PathNameU.Length - ((idx + 1) * sizeof(WCHAR));
658
659 /* Update FO just to keep file name */
660 /* Skip first slash */
661 ++idx;
662 FileObject->FileName.Length = FileNameLen;
663 RtlMoveMemory(&PathNameU.Buffer[0], &PathNameU.Buffer[idx], FileObject->FileName.Length);
664 #if 0
665 /* Terminate the string at the last backslash */
666 PathNameU.Buffer[idx + 1] = UNICODE_NULL;
667 PathNameU.Length = (idx + 1) * sizeof(WCHAR);
668 PathNameU.MaximumLength = PathNameU.Length + sizeof(WCHAR);
669
670 /* Update the file object as well */
671 FileObject->FileName.Length = PathNameU.Length;
672 FileObject->FileName.MaximumLength = PathNameU.MaximumLength;
673 #endif
674 }
675 else
676 {
677 /* This is a relative open and we have only the filename, so open the parent directory
678 * It is in RelatedFileObject
679 */
680 ASSERT(FileObject->RelatedFileObject != NULL);
681
682 /* No need to modify the FO, it already has the name */
683 }
684
685 /* We're done with opening! */
686 if (ParentFcb != NULL)
687 {
688 Status = vfatAttachFCBToFileObject(DeviceExt, ParentFcb, FileObject);
689 }
690
691 if (NT_SUCCESS(Status))
692 {
693 pFcb = FileObject->FsContext;
694 ASSERT(pFcb == ParentFcb);
695
696 if (pFcb->OpenHandleCount == 0)
697 {
698 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
699 Stack->Parameters.Create.ShareAccess,
700 FileObject,
701 &pFcb->FCBShareAccess);
702 }
703 else
704 {
705 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
706 Stack->Parameters.Create.ShareAccess,
707 FileObject,
708 &pFcb->FCBShareAccess,
709 FALSE);
710 if (!NT_SUCCESS(Status))
711 {
712 VfatCloseFile(DeviceExt, FileObject);
713 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
714 return Status;
715 }
716 }
717
718 pCcb = FileObject->FsContext2;
719 if (BooleanFlagOn(RequestedOptions, FILE_DELETE_ON_CLOSE))
720 {
721 pCcb->Flags |= CCB_DELETE_ON_CLOSE;
722 }
723
724 pFcb->OpenHandleCount++;
725 DeviceExt->OpenHandleCount++;
726 }
727 else if (ParentFcb != NULL)
728 {
729 vfatReleaseFCB(DeviceExt, ParentFcb);
730 }
731
732 if (NT_SUCCESS(Status))
733 {
734 vfatAddToStat(DeviceExt, Fat.SuccessfulCreates, 1);
735 }
736 else
737 {
738 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
739 }
740
741 return Status;
742 }
743
744 /*
745 * If the directory containing the file to open doesn't exist then
746 * fail immediately
747 */
748 if (Status == STATUS_OBJECT_PATH_NOT_FOUND ||
749 Status == STATUS_INVALID_PARAMETER ||
750 Status == STATUS_DELETE_PENDING ||
751 Status == STATUS_ACCESS_DENIED ||
752 Status == STATUS_OBJECT_NAME_COLLISION)
753 {
754 if (ParentFcb)
755 {
756 vfatReleaseFCB(DeviceExt, ParentFcb);
757 }
758 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
759 return Status;
760 }
761
762 if (!NT_SUCCESS(Status) && ParentFcb == NULL)
763 {
764 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU, Status);
765 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
766 return Status;
767 }
768
769 Attributes = (Stack->Parameters.Create.FileAttributes & (FILE_ATTRIBUTE_ARCHIVE |
770 FILE_ATTRIBUTE_SYSTEM |
771 FILE_ATTRIBUTE_HIDDEN |
772 FILE_ATTRIBUTE_DIRECTORY |
773 FILE_ATTRIBUTE_READONLY));
774
775 /* If the file open failed then create the required file */
776 if (!NT_SUCCESS (Status))
777 {
778 if (RequestedDisposition == FILE_CREATE ||
779 RequestedDisposition == FILE_OPEN_IF ||
780 RequestedDisposition == FILE_OVERWRITE_IF ||
781 RequestedDisposition == FILE_SUPERSEDE)
782 {
783 if (!BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE))
784 {
785 if (TrailingBackslash)
786 {
787 vfatReleaseFCB(DeviceExt, ParentFcb);
788 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
789 return STATUS_OBJECT_NAME_INVALID;
790 }
791 Attributes |= FILE_ATTRIBUTE_ARCHIVE;
792 }
793 vfatSplitPathName(&PathNameU, NULL, &FileNameU);
794 Status = VfatAddEntry(DeviceExt, &FileNameU, &pFcb, ParentFcb, RequestedOptions,
795 Attributes, NULL);
796 vfatReleaseFCB(DeviceExt, ParentFcb);
797 if (NT_SUCCESS(Status))
798 {
799 Status = vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
800 if (!NT_SUCCESS(Status))
801 {
802 vfatReleaseFCB(DeviceExt, pFcb);
803 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
804 return Status;
805 }
806
807 Irp->IoStatus.Information = FILE_CREATED;
808 VfatSetAllocationSizeInformation(FileObject,
809 pFcb,
810 DeviceExt,
811 &Irp->Overlay.AllocationSize);
812 VfatSetExtendedAttributes(FileObject,
813 Irp->AssociatedIrp.SystemBuffer,
814 Stack->Parameters.Create.EaLength);
815
816 if (PagingFileCreate)
817 {
818 pFcb->Flags |= FCB_IS_PAGE_FILE;
819 SetFlag(DeviceExt->Flags, VCB_IS_SYS_OR_HAS_PAGE);
820 }
821 }
822 else
823 {
824 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
825 return Status;
826 }
827 }
828 else
829 {
830 vfatReleaseFCB(DeviceExt, ParentFcb);
831 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
832 return Status;
833 }
834 }
835 else
836 {
837 if (ParentFcb)
838 {
839 vfatReleaseFCB(DeviceExt, ParentFcb);
840 }
841
842 pFcb = FileObject->FsContext;
843
844 /* Otherwise fail if the caller wanted to create a new file */
845 if (RequestedDisposition == FILE_CREATE)
846 {
847 VfatCloseFile(DeviceExt, FileObject);
848 if (TrailingBackslash && !vfatFCBIsDirectory(pFcb))
849 {
850 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
851 return STATUS_OBJECT_NAME_INVALID;
852 }
853 Irp->IoStatus.Information = FILE_EXISTS;
854 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
855 return STATUS_OBJECT_NAME_COLLISION;
856 }
857
858 if (pFcb->OpenHandleCount != 0)
859 {
860 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
861 Stack->Parameters.Create.ShareAccess,
862 FileObject,
863 &pFcb->FCBShareAccess,
864 FALSE);
865 if (!NT_SUCCESS(Status))
866 {
867 VfatCloseFile(DeviceExt, FileObject);
868 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
869 return Status;
870 }
871 }
872
873 /*
874 * Check the file has the requested attributes
875 */
876 if (BooleanFlagOn(RequestedOptions, FILE_NON_DIRECTORY_FILE) &&
877 vfatFCBIsDirectory(pFcb))
878 {
879 VfatCloseFile (DeviceExt, FileObject);
880 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
881 return STATUS_FILE_IS_A_DIRECTORY;
882 }
883 if (BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE) &&
884 !vfatFCBIsDirectory(pFcb))
885 {
886 VfatCloseFile (DeviceExt, FileObject);
887 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
888 return STATUS_NOT_A_DIRECTORY;
889 }
890 if (TrailingBackslash && !vfatFCBIsDirectory(pFcb))
891 {
892 VfatCloseFile (DeviceExt, FileObject);
893 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
894 return STATUS_OBJECT_NAME_INVALID;
895 }
896 #ifndef USE_ROS_CC_AND_FS
897 if (!vfatFCBIsDirectory(pFcb))
898 {
899 if (BooleanFlagOn(Stack->Parameters.Create.SecurityContext->DesiredAccess, FILE_WRITE_DATA) ||
900 RequestedDisposition == FILE_OVERWRITE ||
901 RequestedDisposition == FILE_OVERWRITE_IF ||
902 (RequestedOptions & FILE_DELETE_ON_CLOSE))
903 {
904 if (!MmFlushImageSection(&pFcb->SectionObjectPointers, MmFlushForWrite))
905 {
906 DPRINT1("%wZ\n", &pFcb->PathNameU);
907 DPRINT1("%d %d %d\n", Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA,
908 RequestedDisposition == FILE_OVERWRITE, RequestedDisposition == FILE_OVERWRITE_IF);
909 VfatCloseFile (DeviceExt, FileObject);
910 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
911 return (BooleanFlagOn(RequestedOptions, FILE_DELETE_ON_CLOSE)) ? STATUS_CANNOT_DELETE
912 : STATUS_SHARING_VIOLATION;
913 }
914 }
915 }
916 #endif
917 if (PagingFileCreate)
918 {
919 /* FIXME:
920 * Do more checking for page files. It is possible,
921 * that the file was opened and closed previously
922 * as a normal cached file. In this case, the cache
923 * manager has referenced the fileobject and the fcb
924 * is held in memory. Try to remove the fileobject
925 * from cache manager and use the fcb.
926 */
927 if (pFcb->RefCount > 1)
928 {
929 if(!BooleanFlagOn(pFcb->Flags, FCB_IS_PAGE_FILE))
930 {
931 VfatCloseFile(DeviceExt, FileObject);
932 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
933 return STATUS_INVALID_PARAMETER;
934 }
935 }
936 else
937 {
938 pFcb->Flags |= FCB_IS_PAGE_FILE;
939 SetFlag(DeviceExt->Flags, VCB_IS_SYS_OR_HAS_PAGE);
940 }
941 }
942 else
943 {
944 if (BooleanFlagOn(pFcb->Flags, FCB_IS_PAGE_FILE))
945 {
946 VfatCloseFile(DeviceExt, FileObject);
947 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
948 return STATUS_INVALID_PARAMETER;
949 }
950 }
951
952 if (RequestedDisposition == FILE_OVERWRITE ||
953 RequestedDisposition == FILE_OVERWRITE_IF ||
954 RequestedDisposition == FILE_SUPERSEDE)
955 {
956 if ((BooleanFlagOn(*pFcb->Attributes, FILE_ATTRIBUTE_HIDDEN) && !BooleanFlagOn(Attributes, FILE_ATTRIBUTE_HIDDEN)) ||
957 (BooleanFlagOn(*pFcb->Attributes, FILE_ATTRIBUTE_SYSTEM) && !BooleanFlagOn(Attributes, FILE_ATTRIBUTE_SYSTEM)))
958 {
959 VfatCloseFile(DeviceExt, FileObject);
960 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
961 return STATUS_ACCESS_DENIED;
962 }
963
964 if (!vfatFCBIsDirectory(pFcb))
965 {
966 LARGE_INTEGER SystemTime;
967
968 if (RequestedDisposition == FILE_SUPERSEDE)
969 {
970 *pFcb->Attributes = Attributes;
971 }
972 else
973 {
974 *pFcb->Attributes |= Attributes;
975 }
976 *pFcb->Attributes |= FILE_ATTRIBUTE_ARCHIVE;
977
978 KeQuerySystemTime(&SystemTime);
979 if (vfatVolumeIsFatX(DeviceExt))
980 {
981 FsdSystemTimeToDosDateTime(DeviceExt,
982 &SystemTime, &pFcb->entry.FatX.UpdateDate,
983 &pFcb->entry.FatX.UpdateTime);
984 }
985 else
986 {
987 FsdSystemTimeToDosDateTime(DeviceExt,
988 &SystemTime, &pFcb->entry.Fat.UpdateDate,
989 &pFcb->entry.Fat.UpdateTime);
990 }
991
992 VfatUpdateEntry(DeviceExt, pFcb);
993 }
994
995 ExAcquireResourceExclusiveLite(&(pFcb->MainResource), TRUE);
996 Status = VfatSetAllocationSizeInformation(FileObject,
997 pFcb,
998 DeviceExt,
999 &Irp->Overlay.AllocationSize);
1000 ExReleaseResourceLite(&(pFcb->MainResource));
1001 if (!NT_SUCCESS (Status))
1002 {
1003 VfatCloseFile(DeviceExt, FileObject);
1004 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
1005 return Status;
1006 }
1007 }
1008
1009 if (RequestedDisposition == FILE_SUPERSEDE)
1010 {
1011 Irp->IoStatus.Information = FILE_SUPERSEDED;
1012 }
1013 else if (RequestedDisposition == FILE_OVERWRITE ||
1014 RequestedDisposition == FILE_OVERWRITE_IF)
1015 {
1016 Irp->IoStatus.Information = FILE_OVERWRITTEN;
1017 }
1018 else
1019 {
1020 Irp->IoStatus.Information = FILE_OPENED;
1021 }
1022 }
1023
1024 if (pFcb->OpenHandleCount == 0)
1025 {
1026 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
1027 Stack->Parameters.Create.ShareAccess,
1028 FileObject,
1029 &pFcb->FCBShareAccess);
1030 }
1031 else
1032 {
1033 IoUpdateShareAccess(FileObject,
1034 &pFcb->FCBShareAccess);
1035 }
1036
1037 pCcb = FileObject->FsContext2;
1038 if (BooleanFlagOn(RequestedOptions, FILE_DELETE_ON_CLOSE))
1039 {
1040 pCcb->Flags |= CCB_DELETE_ON_CLOSE;
1041 }
1042
1043 if (Irp->IoStatus.Information == FILE_CREATED)
1044 {
1045 vfatReportChange(DeviceExt,
1046 pFcb,
1047 (vfatFCBIsDirectory(pFcb) ?
1048 FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME),
1049 FILE_ACTION_ADDED);
1050 }
1051 else if (Irp->IoStatus.Information == FILE_OVERWRITTEN ||
1052 Irp->IoStatus.Information == FILE_SUPERSEDED)
1053 {
1054 vfatReportChange(DeviceExt,
1055 pFcb,
1056 FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE,
1057 FILE_ACTION_MODIFIED);
1058 }
1059
1060 pFcb->OpenHandleCount++;
1061 DeviceExt->OpenHandleCount++;
1062
1063 /* FIXME : test write access if requested */
1064
1065 /* FIXME: That is broken, we cannot reach this code path with failure */
1066 ASSERT(NT_SUCCESS(Status));
1067 if (NT_SUCCESS(Status))
1068 {
1069 vfatAddToStat(DeviceExt, Fat.SuccessfulCreates, 1);
1070 }
1071 else
1072 {
1073 vfatAddToStat(DeviceExt, Fat.FailedCreates, 1);
1074 }
1075
1076 return Status;
1077 }
1078
1079 /*
1080 * FUNCTION: Create or open a file
1081 */
1082 NTSTATUS
1083 VfatCreate(
1084 PVFAT_IRP_CONTEXT IrpContext)
1085 {
1086 NTSTATUS Status;
1087
1088 ASSERT(IrpContext);
1089
1090 if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
1091 {
1092 /* DeviceObject represents FileSystem instead of logical volume */
1093 DPRINT ("FsdCreate called with file system\n");
1094 IrpContext->Irp->IoStatus.Information = FILE_OPENED;
1095 IrpContext->PriorityBoost = IO_DISK_INCREMENT;
1096
1097 return STATUS_SUCCESS;
1098 }
1099
1100 IrpContext->Irp->IoStatus.Information = 0;
1101 ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource, TRUE);
1102 Status = VfatCreateFile(IrpContext->DeviceObject, IrpContext->Irp);
1103 ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
1104
1105 if (NT_SUCCESS(Status))
1106 IrpContext->PriorityBoost = IO_DISK_INCREMENT;
1107
1108 return Status;
1109 }
1110
1111 /* EOF */