[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/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_VFAT);
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 ExFreePool(PathNameBuffer);
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 ExFreePool(PathNameBuffer);
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 ExFreePool(PathNameBuffer);
255 return STATUS_SUCCESS;
256 }
257 DirContext->DirIndex++;
258 }
259
260 if (Context)
261 {
262 CcUnpinData(Context);
263 }
264
265 RtlFreeUnicodeString(&FileToFindUpcase);
266 ExFreePool(PathNameBuffer);
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 DPRINT("Attaching FCB to fileObject\n");
374 Status = vfatAttachFCBToFileObject(DeviceExt, Fcb, FileObject);
375 if (!NT_SUCCESS(Status))
376 {
377 vfatReleaseFCB(DeviceExt, Fcb);
378 }
379 return Status;
380 }
381
382 /*
383 * FUNCTION: Create or open a file
384 */
385 static NTSTATUS
386 VfatCreateFile(
387 PDEVICE_OBJECT DeviceObject,
388 PIRP Irp)
389 {
390 PIO_STACK_LOCATION Stack;
391 PFILE_OBJECT FileObject;
392 NTSTATUS Status = STATUS_SUCCESS;
393 PDEVICE_EXTENSION DeviceExt;
394 ULONG RequestedDisposition, RequestedOptions;
395 PVFATFCB pFcb = NULL;
396 PVFATFCB ParentFcb = NULL;
397 PVFATCCB pCcb = NULL;
398 PWCHAR c, last;
399 BOOLEAN PagingFileCreate;
400 BOOLEAN Dots;
401 BOOLEAN OpenTargetDir;
402 BOOLEAN TrailingBackslash;
403 UNICODE_STRING FileNameU;
404 UNICODE_STRING PathNameU;
405 ULONG Attributes;
406
407 /* Unpack the various parameters. */
408 Stack = IoGetCurrentIrpStackLocation(Irp);
409 RequestedDisposition = ((Stack->Parameters.Create.Options >> 24) & 0xff);
410 RequestedOptions = Stack->Parameters.Create.Options & FILE_VALID_OPTION_FLAGS;
411 PagingFileCreate = BooleanFlagOn(Stack->Flags, SL_OPEN_PAGING_FILE);
412 OpenTargetDir = BooleanFlagOn(Stack->Flags, SL_OPEN_TARGET_DIRECTORY);
413
414 FileObject = Stack->FileObject;
415 DeviceExt = DeviceObject->DeviceExtension;
416
417 if (BooleanFlagOn(Stack->Parameters.Create.Options, FILE_OPEN_BY_FILE_ID))
418 {
419 return STATUS_NOT_IMPLEMENTED;
420 }
421
422 /* Check their validity. */
423 if (BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE) &&
424 RequestedDisposition == FILE_SUPERSEDE)
425 {
426 return STATUS_INVALID_PARAMETER;
427 }
428
429 if (BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE) &&
430 BooleanFlagOn(RequestedOptions, FILE_NON_DIRECTORY_FILE))
431 {
432 return STATUS_INVALID_PARAMETER;
433 }
434
435 /* Deny create if the volume is locked */
436 if (BooleanFlagOn(DeviceExt->Flags, VCB_VOLUME_LOCKED))
437 {
438 return STATUS_ACCESS_DENIED;
439 }
440
441 /* This a open operation for the volume itself */
442 if (FileObject->FileName.Length == 0 &&
443 (FileObject->RelatedFileObject == NULL ||
444 FileObject->RelatedFileObject->FsContext2 != NULL ||
445 FileObject->RelatedFileObject->FsContext == DeviceExt->VolumeFcb))
446 {
447 DPRINT("Volume opening\n");
448
449 if (RequestedDisposition != FILE_OPEN &&
450 RequestedDisposition != FILE_OPEN_IF)
451 {
452 return STATUS_ACCESS_DENIED;
453 }
454
455 if (BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE) &&
456 (FileObject->RelatedFileObject == NULL || FileObject->RelatedFileObject->FsContext2 == NULL || FileObject->RelatedFileObject->FsContext == DeviceExt->VolumeFcb))
457 {
458 return STATUS_NOT_A_DIRECTORY;
459 }
460
461 if (OpenTargetDir)
462 {
463 return STATUS_INVALID_PARAMETER;
464 }
465
466 if (BooleanFlagOn(RequestedOptions, FILE_DELETE_ON_CLOSE))
467 {
468 return STATUS_CANNOT_DELETE;
469 }
470
471 pFcb = DeviceExt->VolumeFcb;
472
473 if (pFcb->OpenHandleCount == 0)
474 {
475 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
476 Stack->Parameters.Create.ShareAccess,
477 FileObject,
478 &pFcb->FCBShareAccess);
479 }
480 else
481 {
482 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
483 Stack->Parameters.Create.ShareAccess,
484 FileObject,
485 &pFcb->FCBShareAccess,
486 FALSE);
487 if (!NT_SUCCESS(Status))
488 {
489 return Status;
490 }
491 }
492
493 vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
494 DeviceExt->OpenHandleCount++;
495 pFcb->OpenHandleCount++;
496
497 Irp->IoStatus.Information = FILE_OPENED;
498 return STATUS_SUCCESS;
499 }
500
501 if (FileObject->RelatedFileObject != NULL &&
502 FileObject->RelatedFileObject->FsContext == DeviceExt->VolumeFcb)
503 {
504 ASSERT(FileObject->FileName.Length != 0);
505 return STATUS_OBJECT_PATH_NOT_FOUND;
506 }
507
508 /* Check for illegal characters and illegal dot sequences in the file name */
509 PathNameU = FileObject->FileName;
510 c = PathNameU.Buffer + PathNameU.Length / sizeof(WCHAR);
511 last = c - 1;
512
513 Dots = TRUE;
514 while (c-- > PathNameU.Buffer)
515 {
516 if (*c == L'\\' || c == PathNameU.Buffer)
517 {
518 if (Dots && last > c)
519 {
520 return STATUS_OBJECT_NAME_INVALID;
521 }
522 if (*c == L'\\' && (c - 1) > PathNameU.Buffer &&
523 *(c - 1) == L'\\')
524 {
525 return STATUS_OBJECT_NAME_INVALID;
526 }
527
528 last = c - 1;
529 Dots = TRUE;
530 }
531 else if (*c != L'.')
532 {
533 Dots = FALSE;
534 }
535
536 if (*c != '\\' && vfatIsLongIllegal(*c))
537 {
538 return STATUS_OBJECT_NAME_INVALID;
539 }
540 }
541
542 /* Check if we try to open target directory of root dir */
543 if (OpenTargetDir && FileObject->RelatedFileObject == NULL && PathNameU.Length == sizeof(WCHAR) &&
544 PathNameU.Buffer[0] == L'\\')
545 {
546 return STATUS_INVALID_PARAMETER;
547 }
548
549 if (FileObject->RelatedFileObject && PathNameU.Length >= sizeof(WCHAR) && PathNameU.Buffer[0] == L'\\')
550 {
551 return STATUS_OBJECT_NAME_INVALID;
552 }
553
554 TrailingBackslash = FALSE;
555 if (PathNameU.Length > sizeof(WCHAR) && PathNameU.Buffer[PathNameU.Length/sizeof(WCHAR)-1] == L'\\')
556 {
557 PathNameU.Length -= sizeof(WCHAR);
558 TrailingBackslash = TRUE;
559 }
560
561 if (PathNameU.Length > sizeof(WCHAR) && PathNameU.Buffer[PathNameU.Length/sizeof(WCHAR)-1] == L'\\')
562 {
563 return STATUS_OBJECT_NAME_INVALID;
564 }
565
566 /* Try opening the file. */
567 if (!OpenTargetDir)
568 {
569 Status = VfatOpenFile(DeviceExt, &PathNameU, FileObject, RequestedDisposition, RequestedOptions, &ParentFcb);
570 }
571 else
572 {
573 PVFATFCB TargetFcb;
574 LONG idx, FileNameLen;
575
576 ParentFcb = (FileObject->RelatedFileObject != NULL) ? FileObject->RelatedFileObject->FsContext : NULL;
577 if (ParentFcb)
578 {
579 vfatGrabFCB(DeviceExt, ParentFcb);
580 }
581 Status = vfatGetFCBForFile(DeviceExt, &ParentFcb, &TargetFcb, &PathNameU);
582
583 if (NT_SUCCESS(Status))
584 {
585 vfatReleaseFCB(DeviceExt, TargetFcb);
586 Irp->IoStatus.Information = FILE_EXISTS;
587 }
588 else
589 {
590 Irp->IoStatus.Information = FILE_DOES_NOT_EXIST;
591 }
592
593 idx = FileObject->FileName.Length / sizeof(WCHAR) - 1;
594
595 /* Skip trailing \ - if any */
596 if (PathNameU.Buffer[idx] == L'\\')
597 {
598 --idx;
599 PathNameU.Length -= sizeof(WCHAR);
600 }
601
602 /* Get file name */
603 while (idx >= 0 && PathNameU.Buffer[idx] != L'\\')
604 {
605 --idx;
606 }
607
608 if (idx > 0 || PathNameU.Buffer[0] == L'\\')
609 {
610 /* We don't want to include / in the name */
611 FileNameLen = PathNameU.Length - ((idx + 1) * sizeof(WCHAR));
612
613 /* Update FO just to keep file name */
614 /* Skip first slash */
615 ++idx;
616 FileObject->FileName.Length = FileNameLen;
617 RtlMoveMemory(&PathNameU.Buffer[0], &PathNameU.Buffer[idx], FileObject->FileName.Length);
618 }
619 else
620 {
621 /* This is a relative open and we have only the filename, so open the parent directory
622 * It is in RelatedFileObject
623 */
624 ASSERT(FileObject->RelatedFileObject != NULL);
625
626 /* No need to modify the FO, it already has the name */
627 }
628
629 /* We're done with opening! */
630 if (ParentFcb != NULL)
631 {
632 Status = vfatAttachFCBToFileObject(DeviceExt, ParentFcb, FileObject);
633 }
634
635 if (NT_SUCCESS(Status))
636 {
637 pFcb = FileObject->FsContext;
638 ASSERT(pFcb == ParentFcb);
639
640 if (pFcb->OpenHandleCount == 0)
641 {
642 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
643 Stack->Parameters.Create.ShareAccess,
644 FileObject,
645 &pFcb->FCBShareAccess);
646 }
647 else
648 {
649 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
650 Stack->Parameters.Create.ShareAccess,
651 FileObject,
652 &pFcb->FCBShareAccess,
653 FALSE);
654 if (!NT_SUCCESS(Status))
655 {
656 VfatCloseFile(DeviceExt, FileObject);
657 return Status;
658 }
659 }
660
661 pCcb = FileObject->FsContext2;
662 if (BooleanFlagOn(RequestedOptions, FILE_DELETE_ON_CLOSE))
663 {
664 pCcb->Flags |= CCB_DELETE_ON_CLOSE;
665 }
666
667 pFcb->OpenHandleCount++;
668 DeviceExt->OpenHandleCount++;
669 }
670 else if (ParentFcb != NULL)
671 {
672 vfatReleaseFCB(DeviceExt, ParentFcb);
673 }
674
675 return Status;
676 }
677
678 /*
679 * If the directory containing the file to open doesn't exist then
680 * fail immediately
681 */
682 if (Status == STATUS_OBJECT_PATH_NOT_FOUND ||
683 Status == STATUS_INVALID_PARAMETER ||
684 Status == STATUS_DELETE_PENDING ||
685 Status == STATUS_ACCESS_DENIED ||
686 Status == STATUS_OBJECT_NAME_COLLISION)
687 {
688 if (ParentFcb)
689 {
690 vfatReleaseFCB(DeviceExt, ParentFcb);
691 }
692 return Status;
693 }
694
695 if (!NT_SUCCESS(Status) && ParentFcb == NULL)
696 {
697 DPRINT1("VfatOpenFile failed for '%wZ', status %x\n", &PathNameU, Status);
698 return Status;
699 }
700
701 /* If the file open failed then create the required file */
702 if (!NT_SUCCESS (Status))
703 {
704 if (RequestedDisposition == FILE_CREATE ||
705 RequestedDisposition == FILE_OPEN_IF ||
706 RequestedDisposition == FILE_OVERWRITE_IF ||
707 RequestedDisposition == FILE_SUPERSEDE)
708 {
709 Attributes = Stack->Parameters.Create.FileAttributes & ~FILE_ATTRIBUTE_NORMAL;
710 if (!BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE))
711 {
712 if (TrailingBackslash)
713 {
714 vfatReleaseFCB(DeviceExt, ParentFcb);
715 return STATUS_OBJECT_NAME_INVALID;
716 }
717 Attributes |= FILE_ATTRIBUTE_ARCHIVE;
718 }
719 vfatSplitPathName(&PathNameU, NULL, &FileNameU);
720 Status = VfatAddEntry(DeviceExt, &FileNameU, &pFcb, ParentFcb, RequestedOptions,
721 (UCHAR)FlagOn(Attributes, FILE_ATTRIBUTE_VALID_FLAGS), NULL);
722 vfatReleaseFCB(DeviceExt, ParentFcb);
723 if (NT_SUCCESS(Status))
724 {
725 Status = vfatAttachFCBToFileObject(DeviceExt, pFcb, FileObject);
726 if (!NT_SUCCESS(Status))
727 {
728 vfatReleaseFCB(DeviceExt, pFcb);
729 return Status;
730 }
731
732 Irp->IoStatus.Information = FILE_CREATED;
733 VfatSetAllocationSizeInformation(FileObject,
734 pFcb,
735 DeviceExt,
736 &Irp->Overlay.AllocationSize);
737 VfatSetExtendedAttributes(FileObject,
738 Irp->AssociatedIrp.SystemBuffer,
739 Stack->Parameters.Create.EaLength);
740
741 if (PagingFileCreate)
742 {
743 pFcb->Flags |= FCB_IS_PAGE_FILE;
744 }
745 }
746 else
747 {
748 return Status;
749 }
750 }
751 else
752 {
753 vfatReleaseFCB(DeviceExt, ParentFcb);
754 return Status;
755 }
756 }
757 else
758 {
759 if (ParentFcb)
760 {
761 vfatReleaseFCB(DeviceExt, ParentFcb);
762 }
763
764 pFcb = FileObject->FsContext;
765
766 /* Otherwise fail if the caller wanted to create a new file */
767 if (RequestedDisposition == FILE_CREATE)
768 {
769 VfatCloseFile(DeviceExt, FileObject);
770 if (TrailingBackslash && !vfatFCBIsDirectory(pFcb))
771 {
772 return STATUS_OBJECT_NAME_INVALID;
773 }
774 Irp->IoStatus.Information = FILE_EXISTS;
775 return STATUS_OBJECT_NAME_COLLISION;
776 }
777
778 if (pFcb->OpenHandleCount != 0)
779 {
780 Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
781 Stack->Parameters.Create.ShareAccess,
782 FileObject,
783 &pFcb->FCBShareAccess,
784 FALSE);
785 if (!NT_SUCCESS(Status))
786 {
787 VfatCloseFile(DeviceExt, FileObject);
788 return Status;
789 }
790 }
791
792 /*
793 * Check the file has the requested attributes
794 */
795 if (BooleanFlagOn(RequestedOptions, FILE_NON_DIRECTORY_FILE) &&
796 vfatFCBIsDirectory(pFcb))
797 {
798 VfatCloseFile (DeviceExt, FileObject);
799 return STATUS_FILE_IS_A_DIRECTORY;
800 }
801 if (BooleanFlagOn(RequestedOptions, FILE_DIRECTORY_FILE) &&
802 !vfatFCBIsDirectory(pFcb))
803 {
804 VfatCloseFile (DeviceExt, FileObject);
805 return STATUS_NOT_A_DIRECTORY;
806 }
807 if (TrailingBackslash && !vfatFCBIsDirectory(pFcb))
808 {
809 VfatCloseFile (DeviceExt, FileObject);
810 return STATUS_OBJECT_NAME_INVALID;
811 }
812 #ifndef USE_ROS_CC_AND_FS
813 if (!vfatFCBIsDirectory(pFcb))
814 {
815 if (BooleanFlagOn(Stack->Parameters.Create.SecurityContext->DesiredAccess, FILE_WRITE_DATA) ||
816 RequestedDisposition == FILE_OVERWRITE ||
817 RequestedDisposition == FILE_OVERWRITE_IF ||
818 (RequestedOptions & FILE_DELETE_ON_CLOSE))
819 {
820 if (!MmFlushImageSection(&pFcb->SectionObjectPointers, MmFlushForWrite))
821 {
822 DPRINT1("%wZ\n", &pFcb->PathNameU);
823 DPRINT1("%d %d %d\n", Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA,
824 RequestedDisposition == FILE_OVERWRITE, RequestedDisposition == FILE_OVERWRITE_IF);
825 VfatCloseFile (DeviceExt, FileObject);
826 return (BooleanFlagOn(RequestedOptions, FILE_DELETE_ON_CLOSE)) ? STATUS_CANNOT_DELETE
827 : STATUS_SHARING_VIOLATION;
828 }
829 }
830 }
831 #endif
832 if (PagingFileCreate)
833 {
834 /* FIXME:
835 * Do more checking for page files. It is possible,
836 * that the file was opened and closed previously
837 * as a normal cached file. In this case, the cache
838 * manager has referenced the fileobject and the fcb
839 * is held in memory. Try to remove the fileobject
840 * from cache manager and use the fcb.
841 */
842 if (pFcb->RefCount > 1)
843 {
844 if(!BooleanFlagOn(pFcb->Flags, FCB_IS_PAGE_FILE))
845 {
846 VfatCloseFile(DeviceExt, FileObject);
847 return STATUS_INVALID_PARAMETER;
848 }
849 }
850 else
851 {
852 pFcb->Flags |= FCB_IS_PAGE_FILE;
853 }
854 }
855 else
856 {
857 if (BooleanFlagOn(pFcb->Flags, FCB_IS_PAGE_FILE))
858 {
859 VfatCloseFile(DeviceExt, FileObject);
860 return STATUS_INVALID_PARAMETER;
861 }
862 }
863
864 if (RequestedDisposition == FILE_OVERWRITE ||
865 RequestedDisposition == FILE_OVERWRITE_IF ||
866 RequestedDisposition == FILE_SUPERSEDE)
867 {
868 if (!vfatFCBIsDirectory(pFcb))
869 {
870 *pFcb->Attributes = Stack->Parameters.Create.FileAttributes & ~FILE_ATTRIBUTE_NORMAL;
871 *pFcb->Attributes |= FILE_ATTRIBUTE_ARCHIVE;
872 VfatUpdateEntry(pFcb, vfatVolumeIsFatX(DeviceExt));
873 }
874
875 ExAcquireResourceExclusiveLite(&(pFcb->MainResource), TRUE);
876 Status = VfatSetAllocationSizeInformation(FileObject,
877 pFcb,
878 DeviceExt,
879 &Irp->Overlay.AllocationSize);
880 ExReleaseResourceLite(&(pFcb->MainResource));
881 if (!NT_SUCCESS (Status))
882 {
883 VfatCloseFile(DeviceExt, FileObject);
884 return Status;
885 }
886 }
887
888 if (RequestedDisposition == FILE_SUPERSEDE)
889 {
890 Irp->IoStatus.Information = FILE_SUPERSEDED;
891 }
892 else if (RequestedDisposition == FILE_OVERWRITE ||
893 RequestedDisposition == FILE_OVERWRITE_IF)
894 {
895 Irp->IoStatus.Information = FILE_OVERWRITTEN;
896 }
897 else
898 {
899 Irp->IoStatus.Information = FILE_OPENED;
900 }
901 }
902
903 if (pFcb->OpenHandleCount == 0)
904 {
905 IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
906 Stack->Parameters.Create.ShareAccess,
907 FileObject,
908 &pFcb->FCBShareAccess);
909 }
910 else
911 {
912 IoUpdateShareAccess(FileObject,
913 &pFcb->FCBShareAccess);
914 }
915
916 pCcb = FileObject->FsContext2;
917 if (BooleanFlagOn(RequestedOptions, FILE_DELETE_ON_CLOSE))
918 {
919 pCcb->Flags |= CCB_DELETE_ON_CLOSE;
920 }
921
922 if (Irp->IoStatus.Information == FILE_CREATED)
923 {
924 FsRtlNotifyFullReportChange(DeviceExt->NotifySync,
925 &(DeviceExt->NotifyList),
926 (PSTRING)&pFcb->PathNameU,
927 pFcb->PathNameU.Length - pFcb->LongNameU.Length,
928 NULL,
929 NULL,
930 (vfatFCBIsDirectory(pFcb) ?
931 FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME),
932 FILE_ACTION_ADDED,
933 NULL);
934 }
935
936 pFcb->OpenHandleCount++;
937 DeviceExt->OpenHandleCount++;
938
939 /* FIXME : test write access if requested */
940
941 return Status;
942 }
943
944 /*
945 * FUNCTION: Create or open a file
946 */
947 NTSTATUS
948 VfatCreate(
949 PVFAT_IRP_CONTEXT IrpContext)
950 {
951 NTSTATUS Status;
952
953 ASSERT(IrpContext);
954
955 if (IrpContext->DeviceObject == VfatGlobalData->DeviceObject)
956 {
957 /* DeviceObject represents FileSystem instead of logical volume */
958 DPRINT ("FsdCreate called with file system\n");
959 IrpContext->Irp->IoStatus.Information = FILE_OPENED;
960 IrpContext->PriorityBoost = IO_DISK_INCREMENT;
961
962 return STATUS_SUCCESS;
963 }
964
965 if (!BooleanFlagOn(IrpContext->Flags, IRPCONTEXT_CANWAIT))
966 {
967 return VfatMarkIrpContextForQueue(IrpContext);
968 }
969
970 IrpContext->Irp->IoStatus.Information = 0;
971 ExAcquireResourceExclusiveLite(&IrpContext->DeviceExt->DirResource, TRUE);
972 Status = VfatCreateFile(IrpContext->DeviceObject, IrpContext->Irp);
973 ExReleaseResourceLite(&IrpContext->DeviceExt->DirResource);
974
975 if (NT_SUCCESS(Status))
976 IrpContext->PriorityBoost = IO_DISK_INCREMENT;
977
978 return Status;
979 }
980
981 /* EOF */