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 /* FUNCTIONS ****************************************************************/
38 NtfsMakeAbsoluteFilename(PFILE_OBJECT pFileObject
,
39 PWSTR pRelativeFileName
,
40 PWSTR
*pAbsoluteFilename
)
45 DPRINT("try related for %S\n", pRelativeFileName
);
46 Fcb
= pFileObject
->FsContext
;
49 if (Fcb
->Flags
& FCB_IS_VOLUME
)
51 /* This is likely to be an opening by ID, return ourselves */
52 if (pRelativeFileName
[0] == L
'\\')
54 *pAbsoluteFilename
= NULL
;
55 return STATUS_SUCCESS
;
58 return STATUS_INVALID_PARAMETER
;
61 /* verify related object is a directory and target name
62 don't start with \. */
63 if (NtfsFCBIsDirectory(Fcb
) == FALSE
||
64 pRelativeFileName
[0] == L
'\\')
66 return STATUS_INVALID_PARAMETER
;
69 /* construct absolute path name */
70 ASSERT(wcslen (Fcb
->PathName
) + 1 + wcslen (pRelativeFileName
) + 1 <= MAX_PATH
);
71 rcName
= ExAllocatePoolWithTag(NonPagedPool
, MAX_PATH
* sizeof(WCHAR
), TAG_NTFS
);
74 return STATUS_INSUFFICIENT_RESOURCES
;
77 wcscpy(rcName
, Fcb
->PathName
);
78 if (!NtfsFCBIsRoot(Fcb
))
79 wcscat (rcName
, L
"\\");
80 wcscat (rcName
, pRelativeFileName
);
81 *pAbsoluteFilename
= rcName
;
83 return STATUS_SUCCESS
;
89 NtfsMoonWalkID(PDEVICE_EXTENSION DeviceExt
,
91 PUNICODE_STRING OutPath
)
94 PFILE_RECORD_HEADER MftRecord
;
95 PFILENAME_ATTRIBUTE FileName
;
96 WCHAR FullPath
[MAX_PATH
];
97 ULONG WritePosition
= MAX_PATH
- 1;
99 DPRINT1("NtfsMoonWalkID(%p, %I64x, %p)\n", DeviceExt
, Id
, OutPath
);
101 RtlZeroMemory(FullPath
, sizeof(FullPath
));
102 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
103 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
105 if (MftRecord
== NULL
)
107 return STATUS_INSUFFICIENT_RESOURCES
;
112 Status
= ReadFileRecord(DeviceExt
, Id
, MftRecord
);
113 if (!NT_SUCCESS(Status
))
116 ASSERT(MftRecord
->Ntfs
.Type
== NRH_FILE_TYPE
);
117 if (!(MftRecord
->Flags
& FRH_IN_USE
))
119 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
123 FileName
= GetBestFileNameFromRecord(MftRecord
);
124 if (FileName
== NULL
)
126 DPRINT1("$FILE_NAME attribute not found for %I64x\n", Id
);
127 Status
= STATUS_OBJECT_PATH_NOT_FOUND
;
131 WritePosition
-= FileName
->NameLength
;
132 ASSERT(WritePosition
< MAX_PATH
);
133 RtlCopyMemory(FullPath
+ WritePosition
, FileName
->Name
, FileName
->NameLength
* sizeof(WCHAR
));
135 ASSERT(WritePosition
< MAX_PATH
);
136 FullPath
[WritePosition
] = L
'\\';
138 Id
= FileName
->DirectoryFileReferenceNumber
& NTFS_MFT_MASK
;
139 if (Id
== NTFS_FILE_ROOT
)
143 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
145 if (!NT_SUCCESS(Status
))
148 OutPath
->Length
= (MAX_PATH
- WritePosition
- 1) * sizeof(WCHAR
);
149 OutPath
->MaximumLength
= (MAX_PATH
- WritePosition
) * sizeof(WCHAR
);
150 OutPath
->Buffer
= ExAllocatePoolWithTag(NonPagedPool
, OutPath
->MaximumLength
, TAG_NTFS
);
151 if (OutPath
->Buffer
== NULL
)
153 return STATUS_INSUFFICIENT_RESOURCES
;
155 RtlCopyMemory(OutPath
->Buffer
, FullPath
+ WritePosition
, OutPath
->MaximumLength
);
161 * FUNCTION: Opens a file
165 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt
,
166 PFILE_OBJECT FileObject
,
168 PNTFS_FCB
* FoundFCB
)
173 PWSTR AbsFileName
= NULL
;
175 DPRINT1("NtfsOpenFile(%p, %p, %S, %p)\n", DeviceExt
, FileObject
, FileName
, FoundFCB
);
179 if (FileObject
->RelatedFileObject
)
181 DPRINT("Converting relative filename to absolute filename\n");
183 Status
= NtfsMakeAbsoluteFilename(FileObject
->RelatedFileObject
,
186 if (AbsFileName
) FileName
= AbsFileName
;
187 if (!NT_SUCCESS(Status
))
193 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
195 DPRINT("PathName to open: %S\n", FileName
);
197 /* try first to find an existing FCB in memory */
198 DPRINT("Checking for existing FCB in memory\n");
199 Fcb
= NtfsGrabFCBFromTable(DeviceExt
,
203 DPRINT("No existing FCB found, making a new one if file exists.\n");
204 Status
= NtfsGetFCBForFile(DeviceExt
,
208 if (ParentFcb
!= NULL
)
210 NtfsReleaseFCB(DeviceExt
,
214 if (!NT_SUCCESS (Status
))
216 DPRINT("Could not make a new FCB, status: %x\n", Status
);
219 ExFreePool(AbsFileName
);
225 DPRINT("Attaching FCB to fileObject\n");
226 Status
= NtfsAttachFCBToFileObject(DeviceExt
,
231 ExFreePool(AbsFileName
);
240 * FUNCTION: Opens a file
244 NtfsCreateFile(PDEVICE_OBJECT DeviceObject
,
247 PDEVICE_EXTENSION DeviceExt
;
248 PIO_STACK_LOCATION Stack
;
249 PFILE_OBJECT FileObject
;
250 ULONG RequestedDisposition
;
251 ULONG RequestedOptions
;
255 UNICODE_STRING FullPath
;
257 DPRINT1("NtfsCreateFile(%p, %p) called\n", DeviceObject
, Irp
);
259 DeviceExt
= DeviceObject
->DeviceExtension
;
261 Stack
= IoGetCurrentIrpStackLocation (Irp
);
264 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
265 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
266 // PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
267 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
268 RequestedDisposition
== FILE_SUPERSEDE
)
270 return STATUS_INVALID_PARAMETER
;
273 FileObject
= Stack
->FileObject
;
275 if (RequestedDisposition
== FILE_CREATE
||
276 RequestedDisposition
== FILE_OVERWRITE_IF
||
277 RequestedDisposition
== FILE_SUPERSEDE
)
279 return STATUS_ACCESS_DENIED
;
282 if ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) == FILE_OPEN_BY_FILE_ID
)
286 if (FileObject
->FileName
.Length
!= sizeof(ULONGLONG
))
287 return STATUS_INVALID_PARAMETER
;
289 MFTId
= (*(PULONGLONG
)FileObject
->FileName
.Buffer
) & NTFS_MFT_MASK
;
293 return STATUS_NOT_IMPLEMENTED
;
296 Status
= NtfsMoonWalkID(DeviceExt
, MFTId
, &FullPath
);
297 if (!NT_SUCCESS(Status
))
302 DPRINT1("Open by ID: %I64x -> %wZ\n", (*(PULONGLONG
)FileObject
->FileName
.Buffer
) & NTFS_MFT_MASK
, &FullPath
);
305 /* This a open operation for the volume itself */
306 if (FileObject
->FileName
.Length
== 0 &&
307 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
!= NULL
))
309 if (RequestedDisposition
!= FILE_OPEN
&&
310 RequestedDisposition
!= FILE_OPEN_IF
)
312 return STATUS_ACCESS_DENIED
;
315 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
317 return STATUS_NOT_A_DIRECTORY
;
320 NtfsAttachFCBToFileObject(DeviceExt
, DeviceExt
->VolumeFcb
, FileObject
);
321 DeviceExt
->VolumeFcb
->RefCount
++;
323 Irp
->IoStatus
.Information
= FILE_OPENED
;
324 return STATUS_SUCCESS
;
327 Status
= NtfsOpenFile(DeviceExt
,
329 ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) ? FullPath
.Buffer
: FileObject
->FileName
.Buffer
),
332 if (RequestedOptions
& FILE_OPEN_BY_FILE_ID
)
334 ExFreePoolWithTag(FullPath
.Buffer
, TAG_NTFS
);
337 if (NT_SUCCESS(Status
))
339 if (RequestedDisposition
== FILE_CREATE
)
341 Irp
->IoStatus
.Information
= FILE_EXISTS
;
342 NtfsCloseFile(DeviceExt
, FileObject
);
343 return STATUS_OBJECT_NAME_COLLISION
;
346 if (RequestedOptions
& FILE_NON_DIRECTORY_FILE
&&
347 NtfsFCBIsDirectory(Fcb
))
349 NtfsCloseFile(DeviceExt
, FileObject
);
350 return STATUS_FILE_IS_A_DIRECTORY
;
353 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
354 !NtfsFCBIsDirectory(Fcb
))
356 NtfsCloseFile(DeviceExt
, FileObject
);
357 return STATUS_NOT_A_DIRECTORY
;
361 * If it is a reparse point & FILE_OPEN_REPARSE_POINT, then allow opening it
363 * Otherwise, attempt to read reparse data and hand them to the Io manager
364 * with status reparse to force a reparse.
366 if (NtfsFCBIsReparsePoint(Fcb
) &&
367 ((RequestedOptions
& FILE_OPEN_REPARSE_POINT
) != FILE_OPEN_REPARSE_POINT
))
369 PREPARSE_DATA_BUFFER ReparseData
= NULL
;
371 Status
= NtfsReadFCBAttribute(DeviceExt
, Fcb
,
372 AttributeReparsePoint
, L
"", 0,
373 (PVOID
*)&Irp
->Tail
.Overlay
.AuxiliaryBuffer
);
374 if (NT_SUCCESS(Status
))
376 ReparseData
= (PREPARSE_DATA_BUFFER
)Irp
->Tail
.Overlay
.AuxiliaryBuffer
;
377 if (ReparseData
->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT
)
379 Status
= STATUS_REPARSE
;
383 Status
= STATUS_NOT_IMPLEMENTED
;
384 ExFreePoolWithTag(ReparseData
, TAG_NTFS
);
388 Irp
->IoStatus
.Information
= ((Status
== STATUS_REPARSE
) ? ReparseData
->ReparseTag
: 0);
390 NtfsCloseFile(DeviceExt
, FileObject
);
394 /* HUGLY HACK: remain RO so far... */
395 if (RequestedDisposition
== FILE_OVERWRITE
||
396 RequestedDisposition
== FILE_OVERWRITE_IF
||
397 RequestedDisposition
== FILE_SUPERSEDE
)
399 DPRINT1("Denying write request on NTFS volume\n");
400 NtfsCloseFile(DeviceExt
, FileObject
);
401 return STATUS_ACCESS_DENIED
;
406 /* HUGLY HACK: remain RO so far... */
407 if (RequestedDisposition
== FILE_CREATE
||
408 RequestedDisposition
== FILE_OPEN_IF
||
409 RequestedDisposition
== FILE_OVERWRITE_IF
||
410 RequestedDisposition
== FILE_SUPERSEDE
)
412 DPRINT1("Denying write request on NTFS volume\n");
413 return STATUS_ACCESS_DENIED
;
418 * If the directory containing the file to open doesn't exist then
421 Irp
->IoStatus
.Information
= (NT_SUCCESS(Status
)) ? FILE_OPENED
: 0;
428 NtfsCreate(PNTFS_IRP_CONTEXT IrpContext
)
430 PDEVICE_EXTENSION DeviceExt
;
432 PDEVICE_OBJECT DeviceObject
;
434 DeviceObject
= IrpContext
->DeviceObject
;
435 if (DeviceObject
== NtfsGlobalData
->DeviceObject
)
437 /* DeviceObject represents FileSystem instead of logical volume */
438 DPRINT("Opening file system\n");
439 IrpContext
->Irp
->IoStatus
.Information
= FILE_OPENED
;
440 return STATUS_SUCCESS
;
443 DeviceExt
= DeviceObject
->DeviceExtension
;
445 if (!(IrpContext
->Flags
& IRPCONTEXT_CANWAIT
))
447 return NtfsMarkIrpContextForQueue(IrpContext
);
450 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
,
452 Status
= NtfsCreateFile(DeviceObject
,
454 ExReleaseResourceLite(&DeviceExt
->DirResource
);