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 Id
= Id
& NTFS_MFT_MASK
;
103 RtlZeroMemory(FullPath
, sizeof(FullPath
));
104 MftRecord
= ExAllocatePoolWithTag(NonPagedPool
,
105 DeviceExt
->NtfsInfo
.BytesPerFileRecord
,
107 if (MftRecord
== NULL
)
109 return STATUS_INSUFFICIENT_RESOURCES
;
114 Status
= ReadFileRecord(DeviceExt
, Id
, MftRecord
);
115 if (!NT_SUCCESS(Status
))
118 ASSERT(MftRecord
->Ntfs
.Type
== NRH_FILE_TYPE
);
120 FileName
= GetBestFileNameFromRecord(MftRecord
);
121 WritePosition
-= FileName
->NameLength
;
122 ASSERT(WritePosition
< MAX_PATH
);
123 RtlCopyMemory(FullPath
+ WritePosition
, FileName
->Name
, FileName
->NameLength
* sizeof(WCHAR
));
125 ASSERT(WritePosition
< MAX_PATH
);
126 FullPath
[WritePosition
] = L
'\\';
128 Id
= FileName
->DirectoryFileReferenceNumber
& NTFS_MFT_MASK
;
129 if (Id
== NTFS_FILE_ROOT
)
133 ExFreePoolWithTag(MftRecord
, TAG_NTFS
);
135 OutPath
->Length
= (MAX_PATH
- WritePosition
- 1) * sizeof(WCHAR
);
136 OutPath
->MaximumLength
= (MAX_PATH
- WritePosition
) * sizeof(WCHAR
);
137 OutPath
->Buffer
= ExAllocatePoolWithTag(NonPagedPool
, OutPath
->MaximumLength
, TAG_NTFS
);
138 if (OutPath
->Buffer
== NULL
)
140 return STATUS_INSUFFICIENT_RESOURCES
;
142 RtlCopyMemory(OutPath
->Buffer
, FullPath
+ WritePosition
, OutPath
->MaximumLength
);
148 * FUNCTION: Opens a file
152 NtfsOpenFile(PDEVICE_EXTENSION DeviceExt
,
153 PFILE_OBJECT FileObject
,
155 PNTFS_FCB
* FoundFCB
)
160 PWSTR AbsFileName
= NULL
;
162 DPRINT1("NtfsOpenFile(%p, %p, %S, %p)\n", DeviceExt
, FileObject
, FileName
, FoundFCB
);
166 if (FileObject
->RelatedFileObject
)
168 DPRINT("Converting relative filename to absolute filename\n");
170 Status
= NtfsMakeAbsoluteFilename(FileObject
->RelatedFileObject
,
173 if (AbsFileName
) FileName
= AbsFileName
;
174 if (!NT_SUCCESS(Status
))
180 //FIXME: Get cannonical path name (remove .'s, ..'s and extra separators)
182 DPRINT("PathName to open: %S\n", FileName
);
184 /* try first to find an existing FCB in memory */
185 DPRINT("Checking for existing FCB in memory\n");
186 Fcb
= NtfsGrabFCBFromTable(DeviceExt
,
190 DPRINT("No existing FCB found, making a new one if file exists.\n");
191 Status
= NtfsGetFCBForFile(DeviceExt
,
195 if (ParentFcb
!= NULL
)
197 NtfsReleaseFCB(DeviceExt
,
201 if (!NT_SUCCESS (Status
))
203 DPRINT("Could not make a new FCB, status: %x\n", Status
);
206 ExFreePool(AbsFileName
);
212 DPRINT("Attaching FCB to fileObject\n");
213 Status
= NtfsAttachFCBToFileObject(DeviceExt
,
218 ExFreePool(AbsFileName
);
227 * FUNCTION: Opens a file
231 NtfsCreateFile(PDEVICE_OBJECT DeviceObject
,
234 PDEVICE_EXTENSION DeviceExt
;
235 PIO_STACK_LOCATION Stack
;
236 PFILE_OBJECT FileObject
;
237 ULONG RequestedDisposition
;
238 ULONG RequestedOptions
;
242 UNICODE_STRING FullPath
;
244 DPRINT1("NtfsCreateFile(%p, %p) called\n", DeviceObject
, Irp
);
246 DeviceExt
= DeviceObject
->DeviceExtension
;
248 Stack
= IoGetCurrentIrpStackLocation (Irp
);
251 RequestedDisposition
= ((Stack
->Parameters
.Create
.Options
>> 24) & 0xff);
252 RequestedOptions
= Stack
->Parameters
.Create
.Options
& FILE_VALID_OPTION_FLAGS
;
253 // PagingFileCreate = (Stack->Flags & SL_OPEN_PAGING_FILE) ? TRUE : FALSE;
254 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
255 RequestedDisposition
== FILE_SUPERSEDE
)
257 return STATUS_INVALID_PARAMETER
;
260 FileObject
= Stack
->FileObject
;
262 if (RequestedDisposition
== FILE_CREATE
||
263 RequestedDisposition
== FILE_OVERWRITE_IF
||
264 RequestedDisposition
== FILE_SUPERSEDE
)
266 return STATUS_ACCESS_DENIED
;
269 if ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) == FILE_OPEN_BY_FILE_ID
)
271 if (FileObject
->FileName
.Length
!= sizeof(ULONGLONG
))
272 return STATUS_INVALID_PARAMETER
;
274 Status
= NtfsMoonWalkID(DeviceExt
, (*(PULONGLONG
)FileObject
->FileName
.Buffer
), &FullPath
);
275 if (!NT_SUCCESS(Status
))
280 DPRINT1("Open by ID: %I64x -> %wZ\n", (*(PULONGLONG
)FileObject
->FileName
.Buffer
) & NTFS_MFT_MASK
, &FullPath
);
283 /* This a open operation for the volume itself */
284 if (FileObject
->FileName
.Length
== 0 &&
285 (FileObject
->RelatedFileObject
== NULL
|| FileObject
->RelatedFileObject
->FsContext2
!= NULL
))
287 if (RequestedDisposition
!= FILE_OPEN
&&
288 RequestedDisposition
!= FILE_OPEN_IF
)
290 return STATUS_ACCESS_DENIED
;
293 if (RequestedOptions
& FILE_DIRECTORY_FILE
)
295 return STATUS_NOT_A_DIRECTORY
;
298 NtfsAttachFCBToFileObject(DeviceExt
, DeviceExt
->VolumeFcb
, FileObject
);
299 DeviceExt
->VolumeFcb
->RefCount
++;
301 Irp
->IoStatus
.Information
= FILE_OPENED
;
302 return STATUS_SUCCESS
;
305 Status
= NtfsOpenFile(DeviceExt
,
307 ((RequestedOptions
& FILE_OPEN_BY_FILE_ID
) ? FullPath
.Buffer
: FileObject
->FileName
.Buffer
),
310 if (RequestedOptions
& FILE_OPEN_BY_FILE_ID
)
312 ExFreePoolWithTag(FullPath
.Buffer
, TAG_NTFS
);
315 if (NT_SUCCESS(Status
))
317 if (RequestedDisposition
== FILE_CREATE
)
319 Irp
->IoStatus
.Information
= FILE_EXISTS
;
320 NtfsCloseFile(DeviceExt
, FileObject
);
321 return STATUS_OBJECT_NAME_COLLISION
;
324 if (RequestedOptions
& FILE_NON_DIRECTORY_FILE
&&
325 NtfsFCBIsDirectory(Fcb
))
327 NtfsCloseFile(DeviceExt
, FileObject
);
328 return STATUS_FILE_IS_A_DIRECTORY
;
331 if (RequestedOptions
& FILE_DIRECTORY_FILE
&&
332 !NtfsFCBIsDirectory(Fcb
))
334 NtfsCloseFile(DeviceExt
, FileObject
);
335 return STATUS_NOT_A_DIRECTORY
;
339 * If it is a reparse point & FILE_OPEN_REPARSE_POINT, then allow opening it
341 * Otherwise, attempt to read reparse data and hand them to the Io manager
342 * with status reparse to force a reparse.
344 if (NtfsFCBIsReparsePoint(Fcb
) &&
345 ((RequestedOptions
& FILE_OPEN_REPARSE_POINT
) != FILE_OPEN_REPARSE_POINT
))
347 PREPARSE_DATA_BUFFER ReparseData
= NULL
;
349 Status
= NtfsReadFCBAttribute(DeviceExt
, Fcb
,
350 AttributeReparsePoint
, L
"", 0,
351 (PVOID
*)&Irp
->Tail
.Overlay
.AuxiliaryBuffer
);
352 if (NT_SUCCESS(Status
))
354 ReparseData
= (PREPARSE_DATA_BUFFER
)Irp
->Tail
.Overlay
.AuxiliaryBuffer
;
355 if (ReparseData
->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT
)
357 Status
= STATUS_REPARSE
;
361 Status
= STATUS_NOT_IMPLEMENTED
;
362 ExFreePoolWithTag(ReparseData
, TAG_NTFS
);
366 Irp
->IoStatus
.Information
= ((Status
== STATUS_REPARSE
) ? ReparseData
->ReparseTag
: 0);
368 NtfsCloseFile(DeviceExt
, FileObject
);
372 /* HUGLY HACK: remain RO so far... */
373 if (RequestedDisposition
== FILE_OVERWRITE
||
374 RequestedDisposition
== FILE_OVERWRITE_IF
||
375 RequestedDisposition
== FILE_SUPERSEDE
)
377 DPRINT1("Denying write request on NTFS volume\n");
378 NtfsCloseFile(DeviceExt
, FileObject
);
379 return STATUS_ACCESS_DENIED
;
384 /* HUGLY HACK: remain RO so far... */
385 if (RequestedDisposition
== FILE_CREATE
||
386 RequestedDisposition
== FILE_OPEN_IF
||
387 RequestedDisposition
== FILE_OVERWRITE_IF
||
388 RequestedDisposition
== FILE_SUPERSEDE
)
390 DPRINT1("Denying write request on NTFS volume\n");
391 return STATUS_ACCESS_DENIED
;
396 * If the directory containing the file to open doesn't exist then
399 Irp
->IoStatus
.Information
= (NT_SUCCESS(Status
)) ? FILE_OPENED
: 0;
400 Irp
->IoStatus
.Status
= Status
;
408 NtfsFsdCreate(PDEVICE_OBJECT DeviceObject
,
411 PDEVICE_EXTENSION DeviceExt
;
414 if (DeviceObject
== NtfsGlobalData
->DeviceObject
)
416 /* DeviceObject represents FileSystem instead of logical volume */
417 DPRINT("Opening file system\n");
418 Irp
->IoStatus
.Information
= FILE_OPENED
;
419 Status
= STATUS_SUCCESS
;
423 DeviceExt
= DeviceObject
->DeviceExtension
;
425 FsRtlEnterFileSystem();
426 ExAcquireResourceExclusiveLite(&DeviceExt
->DirResource
,
428 Status
= NtfsCreateFile(DeviceObject
,
430 ExReleaseResourceLite(&DeviceExt
->DirResource
);
431 FsRtlExitFileSystem();
434 Irp
->IoStatus
.Status
= Status
;
435 IoCompleteRequest(Irp
,
436 NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
);