3 * Copyright (C) 2002, 2014 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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 * COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS kernel
21 * FILE: drivers/filesystem/ntfs/create.c
22 * PURPOSE: NTFS filesystem driver
23 * PROGRAMMERS: Eric Kohl
24 * Pierre Schweitzer (pierre@reactos.org)
27 /* INCLUDES *****************************************************************/
34 static PCWSTR MftIdToName
[] = {
49 /* FUNCTIONS ****************************************************************/
53 NtfsMakeAbsoluteFilename(PFILE_OBJECT pFileObject
,
54 PWSTR pRelativeFileName
,
55 PWSTR
*pAbsoluteFilename
)
60 DPRINT("try related for %S\n", pRelativeFileName
);
61 Fcb
= pFileObject
->FsContext
;
64 if (Fcb
->Flags
& FCB_IS_VOLUME
)
66 /* This is likely to be an opening by ID, return ourselves */
67 if (pRelativeFileName
[0] == L
'\\')
69 *pAbsoluteFilename
= NULL
;
70 return STATUS_SUCCESS
;
73 return STATUS_INVALID_PARAMETER
;
76 /* verify related object is a directory and target name
77 don't start with \. */
78 if (NtfsFCBIsDirectory(Fcb
) == FALSE
||
79 pRelativeFileName
[0] == L
'\\')
81 return STATUS_INVALID_PARAMETER
;
84 /* construct absolute path name */
85 ASSERT(wcslen (Fcb
->PathName
) + 1 + wcslen (pRelativeFileName
) + 1 <= MAX_PATH
);
86 rcName
= ExAllocatePoolWithTag(NonPagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_NTFS
);
89 return STATUS_INSUFFICIENT_RESOURCES
;
92 wcscpy(rcName
, Fcb
->PathName
);
93 if (!NtfsFCBIsRoot(Fcb
))
94 wcscat (rcName
, L
"\\");
95 wcscat (rcName
, pRelativeFileName
);
96 *pAbsoluteFilename
= rcName
;
98 return STATUS_SUCCESS
;
104 NtfsMoonWalkID(PDEVICE_EXTENSION DeviceExt
,
106 PUNICODE_STRING OutPath
)
109 PFILE_RECORD_HEADER MftRecord
;
110 PFILENAME_ATTRIBUTE FileName
;
111 WCHAR FullPath
[MAX_PATH
];
112 ULONG WritePosition
= MAX_PATH
- 1;
114 DPRINT1("NtfsMoonWalkID(%p, %I64x, %p)\n", DeviceExt
, Id
, OutPath
);
116 RtlZeroMemory(FullPath
, sizeof(FullPath
));
117 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
118 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
120 if (MftRecord
== NULL
)
122 return STATUS_INSUFFICIENT_RESOURCES
;
127 Status
= ReadFileRecord(DeviceExt
, Id
, MftRecord
);
128 if (!NT_SUCCESS(Status
))
131 ASSERT(MftRecord
->Ntfs
.Type
== NRH_FILE_TYPE
);
132 if (!(MftRecord
->Flags
& FRH_IN_USE
))
134 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
138 FileName
= GetBestFileNameFromRecord(DeviceExt
, MftRecord
);
139 if (FileName
== NULL
)
141 DPRINT1("$FILE_NAME attribute not found for %I64x\n", Id
);
142 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
146 WritePosition
-= FileName
->NameLength
;
147 ASSERT(WritePosition
< MAX_PATH
);
148 RtlCopyMemory(FullPath
+ WritePosition
, FileName
->Name
, FileName
->NameLength
* sizeof(WCHAR
));
150 ASSERT(WritePosition
< MAX_PATH
);
151 FullPath
[WritePosition
] = L
'\\';
153 Id
= FileName
->DirectoryFileReferenceNumber
& NTFS_MFT_MASK
;
154 if (Id
== NTFS_FILE_ROOT
)
158 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
160 if (!NT_SUCCESS(Status
))
163 OutPath
->Length
= (MAX_PATH
- WritePosition
- 1) * sizeof(WCHAR
);
164 OutPath
->MaximumLength
= (MAX_PATH
- WritePosition
) * sizeof(WCHAR
);
165 OutPath
->Buffer
= ExAllocatePoolWithTag(NonPagedPool
, OutPath
->MaximumLength
, TAG_NTFS
);
166 if (OutPath
->Buffer
== NULL
)
168 return STATUS_INSUFFICIENT_RESOURCES
;
170 RtlCopyMemory(OutPath
->Buffer
, FullPath
+ WritePosition
, OutPath
->MaximumLength
);
177 NtfsOpenFileById(PDEVICE_EXTENSION DeviceExt
,
178 PFILE_OBJECT FileObject
,
180 PNTFS_FCB
* FoundFCB
)
184 PFILE_RECORD_HEADER MftRecord
;
186 DPRINT1("NtfsOpenFileById(%p, %p, %I64x, %p)\n", DeviceExt
, FileObject
, MftId
, FoundFCB
);
188 ASSERT(MftId
< 0x10);
189 if (MftId
> 0xb) /* No entries are used yet beyond this */
191 return STATUS_OBJECT_NAME_NOT_FOUND
;
194 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
195 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
197 if (MftRecord
== NULL
)
199 return STATUS_INSUFFICIENT_RESOURCES
;
202 Status
= ReadFileRecord(DeviceExt
, MftId
, MftRecord
);
203 if (!NT_SUCCESS(Status
))
205 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
209 if (!(MftRecord
->Flags
& FRH_IN_USE
))
211 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
212 return STATUS_OBJECT_PATH_NOT_FOUND
;
215 FCB
= NtfsGrabFCBFromTable(DeviceExt
, MftIdToName
[MftId
]);
220 RtlInitUnicodeString(&Name
, MftIdToName
[MftId
]);
221 Status
= NtfsMakeFCBFromDirEntry(DeviceExt
, NULL
, &Name
, NULL
, MftRecord
, MftId
, &FCB
);
222 if (!NT_SUCCESS(Status
))
224 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
231 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
233 Status
= NtfsAttachFCBToFileObject(DeviceExt
,
242 * FUNCTION: Opens a file
246 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt
,
247 PFILE_OBJECT FileObject
,
249 PNTFS_FCB
* FoundFCB
)
254 PWSTR AbsFileName
= NULL
;
256 DPRINT1("NtfsOpenFile(%p, %p, %S, %p)\n", DeviceExt
, FileObject
, FileName
, FoundFCB
);
260 if (FileObject
->RelatedFileObject
)
262 DPRINT("Converting relative filename to absolute filename\n");
264 Status
= NtfsMakeAbsoluteFilename(FileObject
->RelatedFileObject
,
267 if (AbsFileName
) FileName
= AbsFileName
;
268 if (!NT_SUCCESS(Status
))
274 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
276 DPRINT("PathName to open: %S\n", FileName
);
278 /* try first to find an existing FCB in memory */
279 DPRINT("Checking for existing FCB in memory\n");
280 Fcb
= NtfsGrabFCBFromTable(DeviceExt
,
284 DPRINT("No existing FCB found, making a new one if file exists.\n");
285 Status
= NtfsGetFCBForFile(DeviceExt
,
289 if (ParentFcb
!= NULL
)
291 NtfsReleaseFCB(DeviceExt
,
295 if (!NT_SUCCESS (Status
))
297 DPRINT("Could not make a new FCB, status: %x\n", Status
);
300 ExFreePool(AbsFileName
);
306 DPRINT("Attaching FCB to fileObject\n");
307 Status
= NtfsAttachFCBToFileObject(DeviceExt
,
312 ExFreePool(AbsFileName
);
321 * FUNCTION: Opens a file
325 NtfsCreateFile(PDEVICE_OBJECT DeviceObject
,
328 PDEVICE_EXTENSION DeviceExt
;
329 PIO_STACK_LOCATION Stack
;
330 PFILE_OBJECT FileObject
;
331 ULONG RequestedDisposition
;
332 ULONG RequestedOptions
;
333 PNTFS_FCB Fcb
= NULL
;
336 UNICODE_STRING FullPath
;
338 DPRINT1("NtfsCreateFile(%p, %p) called\n", DeviceObject
, Irp
);
340 DeviceExt
= DeviceObject
->DeviceExtension
;
342 Stack
= IoGetCurrentIrpStackLocation (Irp
);
345 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
346 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
347 // PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
348 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
349 RequestedDisposition
== FILE_SUPERSEDE
)
351 return STATUS_INVALID_PARAMETER
;
354 /* Deny create if the volume is locked */
355 if (DeviceExt
->Flags
& VCB_VOLUME_LOCKED
)
357 return STATUS_ACCESS_DENIED
;
360 FileObject
= Stack
->FileObject
;
362 if (RequestedDisposition
== FILE_CREATE
||
363 RequestedDisposition
== FILE_OVERWRITE_IF
||
364 RequestedDisposition
== FILE_SUPERSEDE
)
366 return STATUS_ACCESS_DENIED
;
369 if ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) == FILE_OPEN_BY_FILE_ID
)
373 if (FileObject
->FileName
.Length
!= sizeof(ULONGLONG
))
374 return STATUS_INVALID_PARAMETER
;
376 MFTId
= (*(PULONGLONG
)FileObject
->FileName
.Buffer
) & NTFS_MFT_MASK
;
379 Status
= NtfsOpenFileById(DeviceExt
, FileObject
, MFTId
, &Fcb
);
383 Status
= NtfsMoonWalkID(DeviceExt
, MFTId
, &FullPath
);
386 if (!NT_SUCCESS(Status
))
391 DPRINT1("Open by ID: %I64x -> %wZ\n", (*(PULONGLONG
)FileObject
->FileName
.Buffer
) & NTFS_MFT_MASK
, &FullPath
);
394 /* This a open operation for the volume itself */
395 if (FileObject
->FileName
.Length
== 0 &&
396 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
!= NULL
))
398 if (RequestedDisposition
!= FILE_OPEN
&&
399 RequestedDisposition
!= FILE_OPEN_IF
)
401 return STATUS_ACCESS_DENIED
;
404 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
406 return STATUS_NOT_A_DIRECTORY
;
409 NtfsAttachFCBToFileObject(DeviceExt
, DeviceExt
->VolumeFcb
, FileObject
);
410 DeviceExt
->VolumeFcb
->RefCount
++;
412 Irp
->IoStatus
.Information
= FILE_OPENED
;
413 return STATUS_SUCCESS
;
418 Status
= NtfsOpenFile(DeviceExt
,
420 ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) ? FullPath
.Buffer
: FileObject
->FileName
.Buffer
),
423 if (RequestedOptions
& FILE_OPEN_BY_FILE_ID
)
425 ExFreePoolWithTag(FullPath
.Buffer
, TAG_NTFS
);
429 if (NT_SUCCESS(Status
))
431 if (RequestedDisposition
== FILE_CREATE
)
433 Irp
->IoStatus
.Information
= FILE_EXISTS
;
434 NtfsCloseFile(DeviceExt
, FileObject
);
435 return STATUS_OBJECT_NAME_COLLISION
;
438 if (RequestedOptions
& FILE_NON_DIRECTORY_FILE
&&
439 NtfsFCBIsDirectory(Fcb
))
441 NtfsCloseFile(DeviceExt
, FileObject
);
442 return STATUS_FILE_IS_A_DIRECTORY
;
445 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
446 !NtfsFCBIsDirectory(Fcb
))
448 NtfsCloseFile(DeviceExt
, FileObject
);
449 return STATUS_NOT_A_DIRECTORY
;
453 * If it is a reparse point & FILE_OPEN_REPARSE_POINT, then allow opening it
455 * Otherwise, attempt to read reparse data and hand them to the Io manager
456 * with status reparse to force a reparse.
458 if (NtfsFCBIsReparsePoint(Fcb
) &&
459 ((RequestedOptions
& FILE_OPEN_REPARSE_POINT
) != FILE_OPEN_REPARSE_POINT
))
461 PREPARSE_DATA_BUFFER ReparseData
= NULL
;
463 Status
= NtfsReadFCBAttribute(DeviceExt
, Fcb
,
464 AttributeReparsePoint
, L
"", 0,
465 (PVOID
*)&Irp
->Tail
.Overlay
.AuxiliaryBuffer
);
466 if (NT_SUCCESS(Status
))
468 ReparseData
= (PREPARSE_DATA_BUFFER
)Irp
->Tail
.Overlay
.AuxiliaryBuffer
;
469 if (ReparseData
->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT
)
471 Status
= STATUS_REPARSE
;
475 Status
= STATUS_NOT_IMPLEMENTED
;
476 ExFreePoolWithTag(ReparseData
, TAG_NTFS
);
480 Irp
->IoStatus
.Information
= ((Status
== STATUS_REPARSE
) ? ReparseData
->ReparseTag
: 0);
482 NtfsCloseFile(DeviceExt
, FileObject
);
486 /* HUGLY HACK: remain RO so far... */
487 if (RequestedDisposition
== FILE_OVERWRITE
||
488 RequestedDisposition
== FILE_OVERWRITE_IF
||
489 RequestedDisposition
== FILE_SUPERSEDE
)
491 DPRINT1("Denying write request on NTFS volume\n");
492 NtfsCloseFile(DeviceExt
, FileObject
);
493 return STATUS_ACCESS_DENIED
;
498 /* HUGLY HACK: remain RO so far... */
499 if (RequestedDisposition
== FILE_CREATE
||
500 RequestedDisposition
== FILE_OPEN_IF
||
501 RequestedDisposition
== FILE_OVERWRITE_IF
||
502 RequestedDisposition
== FILE_SUPERSEDE
)
504 DPRINT1("Denying write request on NTFS volume\n");
505 return STATUS_ACCESS_DENIED
;
509 if (NT_SUCCESS(Status
))
511 Fcb
->OpenHandleCount
++;
512 DeviceExt
->OpenHandleCount
++;
516 * If the directory containing the file to open doesn't exist then
519 Irp
->IoStatus
.Information
= (NT_SUCCESS(Status
)) ? FILE_OPENED
: 0;
526 NtfsCreate(PNTFS_IRP_CONTEXT IrpContext
)
528 PDEVICE_EXTENSION DeviceExt
;
530 PDEVICE_OBJECT DeviceObject
;
532 DeviceObject
= IrpContext
->DeviceObject
;
533 if (DeviceObject
== NtfsGlobalData
->DeviceObject
)
535 /* DeviceObject represents FileSystem instead of logical volume */
536 DPRINT("Opening file system\n");
537 IrpContext
->Irp
->IoStatus
.Information
= FILE_OPENED
;
538 return STATUS_SUCCESS
;
541 DeviceExt
= DeviceObject
->DeviceExtension
;
543 if (!(IrpContext
->Flags
& IRPCONTEXT_CANWAIT
))
545 return NtfsMarkIrpContextForQueue(IrpContext
);
548 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
,
550 Status
= NtfsCreateFile(DeviceObject
,
552 ExReleaseResourceLite(&DeviceExt
->DirResource
);