1 /* $Id: create.c,v 1.51 2001/12/05 12:11:55 ekohl Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/io/create.c
6 * PURPOSE: Handling file create/open apis
7 * PROGRAMMER: David Welch (welch@cwcom.net)
12 /* INCLUDES ***************************************************************/
14 #include <ddk/ntddk.h>
15 #include <internal/ob.h>
16 #include <internal/io.h>
17 #include <internal/id.h>
18 #include <internal/pool.h>
21 #include <internal/debug.h>
23 /* GLOBALS *******************************************************************/
25 #define TAG_FILE_NAME TAG('F', 'N', 'A', 'M')
27 /* FUNCTIONS *************************************************************/
29 /**********************************************************************
45 NtDeleteFile(IN POBJECT_ATTRIBUTES ObjectAttributes
)
51 /**********************************************************************
65 IopCreateFile(PVOID ObjectBody
,
68 POBJECT_ATTRIBUTES ObjectAttributes
)
70 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
) Parent
;
71 PFILE_OBJECT FileObject
= (PFILE_OBJECT
) ObjectBody
;
74 DPRINT("IopCreateFile(ObjectBody %x, Parent %x, RemainingPath %S)\n",
79 if (NULL
== DeviceObject
)
81 /* This is probably an attempt to create a meta fileobject (eg. for FAT)
82 for the cache manager, so return STATUS_SUCCESS */
83 DPRINT("DeviceObject was NULL\n");
84 return (STATUS_SUCCESS
);
86 if (IoDeviceObjectType
!= BODY_TO_HEADER(Parent
)->ObjectType
)
88 CPRINT("Parent is a %S which is not a device type\n",
89 BODY_TO_HEADER(Parent
)->ObjectType
->TypeName
.Buffer
);
90 return (STATUS_UNSUCCESSFUL
);
92 Status
= ObReferenceObjectByPointer (DeviceObject
,
93 STANDARD_RIGHTS_REQUIRED
,
96 if (STATUS_SUCCESS
!= Status
)
102 DeviceObject
= IoGetAttachedDevice (DeviceObject
);
104 DPRINT("DeviceObject %x\n", DeviceObject
);
106 if (NULL
== RemainingPath
)
108 FileObject
->Flags
= FileObject
->Flags
| FO_DIRECT_DEVICE_OPEN
;
109 FileObject
->FileName
.Buffer
=
110 ExAllocatePoolWithTag(NonPagedPool
,
111 (ObjectAttributes
->ObjectName
->Length
+1)*sizeof(WCHAR
),
113 FileObject
->FileName
.Length
= ObjectAttributes
->ObjectName
->Length
;
114 FileObject
->FileName
.MaximumLength
=
115 ObjectAttributes
->ObjectName
->MaximumLength
;
116 RtlCopyUnicodeString(&(FileObject
->FileName
),
117 ObjectAttributes
->ObjectName
);
121 if ((DeviceObject
->DeviceType
!= FILE_DEVICE_FILE_SYSTEM
)
122 && (DeviceObject
->DeviceType
!= FILE_DEVICE_DISK
)
123 && (DeviceObject
->DeviceType
!= FILE_DEVICE_NETWORK
)
124 && (DeviceObject
->DeviceType
!= FILE_DEVICE_NAMED_PIPE
)
125 && (DeviceObject
->DeviceType
!= FILE_DEVICE_MAILSLOT
))
127 CPRINT ("Device was wrong type\n");
128 return (STATUS_UNSUCCESSFUL
);
131 if (DeviceObject
->DeviceType
!= FILE_DEVICE_NETWORK
132 && (DeviceObject
->DeviceType
!= FILE_DEVICE_NAMED_PIPE
)
133 && (DeviceObject
->DeviceType
!= FILE_DEVICE_MAILSLOT
))
135 if (!(DeviceObject
->Vpb
->Flags
& VPB_MOUNTED
))
137 DPRINT("Trying to mount storage device\n");
138 Status
= IoTryToMountStorageDevice (DeviceObject
);
139 DPRINT("Status %x\n", Status
);
140 if (!NT_SUCCESS(Status
))
142 CPRINT("Failed to mount storage device (statux %x)\n",
146 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
149 RtlCreateUnicodeString(&(FileObject
->FileName
),
152 DPRINT("FileObject->FileName.Buffer %S\n",
153 FileObject
->FileName
.Buffer
);
154 FileObject
->DeviceObject
= DeviceObject
;
155 DPRINT("FileObject %x DeviceObject %x\n",
158 FileObject
->Vpb
= DeviceObject
->Vpb
;
159 FileObject
->Type
= InternalFileType
;
161 return (STATUS_SUCCESS
);
165 /**********************************************************************
167 * IoCreateStreamFileObject@8
186 IoCreateStreamFileObject(PFILE_OBJECT FileObject
,
187 PDEVICE_OBJECT DeviceObject
)
190 PFILE_OBJECT CreatedFileObject
;
193 DPRINT("IoCreateStreamFileObject(FileObject %x, DeviceObject %x)\n",
194 FileObject
, DeviceObject
);
196 assert_irql (PASSIVE_LEVEL
);
198 Status
= ObCreateObject (&FileHandle
,
199 STANDARD_RIGHTS_REQUIRED
,
202 (PVOID
*)&CreatedFileObject
);
203 if (!NT_SUCCESS(Status
))
205 DPRINT("Could not create FileObject\n");
209 if (FileObject
!= NULL
)
211 DeviceObject
= FileObject
->DeviceObject
;
213 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
215 DPRINT("DeviceObject %x\n", DeviceObject
);
217 CreatedFileObject
->DeviceObject
= DeviceObject
;
218 CreatedFileObject
->Vpb
= DeviceObject
->Vpb
;
219 CreatedFileObject
->Type
= InternalFileType
;
220 CreatedFileObject
->Flags
|= FO_DIRECT_DEVICE_OPEN
;
222 ZwClose (FileHandle
);
224 return (CreatedFileObject
);
228 /**********************************************************************
233 * Either causes a new file or directory to be created, or it
234 * opens an existing file, device, directory or volume, giving
235 * the caller a handle for the file object. This handle can be
236 * used by subsequent calls to manipulate data within the file
237 * or the file object's state of attributes.
241 * Points to a variable which receives the file handle
245 * Desired access to the file;
248 * Structure describing the file;
250 * IoStatusBlock (OUT)
251 * Receives information about the operation on return;
253 * AllocationSize [OPTIONAL]
254 * Initial size of the file in bytes;
257 * Attributes to create the file with;
260 * Type of shared access the caller would like to the
264 * Specifies what to do, depending on whether the
265 * file already exists;
268 * Options for creating a new file;
270 * EaBuffer [OPTIONAL]
277 * Type of file (normal, named pipe, mailslot) to create;
279 * ExtraCreateParameters [OPTIONAL]
280 * Additional creation data for named pipe and mailsots;
289 * Prototype taken from Bo Branten's ntifs.h v15.
290 * Description taken from old NtCreateFile's which is
291 * now a wrapper of this call.
298 OUT PHANDLE FileHandle
,
299 IN ACCESS_MASK DesiredAccess
,
300 IN POBJECT_ATTRIBUTES ObjectAttributes
,
301 OUT PIO_STATUS_BLOCK IoStatusBlock
,
302 IN PLARGE_INTEGER AllocationSize OPTIONAL
,
303 IN ULONG FileAttributes
,
304 IN ULONG ShareAccess
,
305 IN ULONG CreateDisposition
,
306 IN ULONG CreateOptions
,
307 IN PVOID EaBuffer OPTIONAL
,
309 IN CREATE_FILE_TYPE CreateFileType
,
310 IN PVOID ExtraCreateParameters OPTIONAL
,
313 PFILE_OBJECT FileObject
;
317 PIO_STACK_LOCATION StackLoc
;
318 IO_STATUS_BLOCK IoSB
;
319 IO_SECURITY_CONTEXT SecurityContext
;
321 DPRINT("IoCreateFile(FileHandle %x, DesiredAccess %x, "
322 "ObjectAttributes %x ObjectAttributes->ObjectName->Buffer %S)\n",
323 FileHandle
,DesiredAccess
,ObjectAttributes
,
324 ObjectAttributes
->ObjectName
->Buffer
);
326 assert_irql(PASSIVE_LEVEL
);
330 Status
= ObCreateObject(FileHandle
,
334 (PVOID
*)&FileObject
);
335 if (!NT_SUCCESS(Status
))
337 DPRINT("ObCreateObject() failed!\n");
340 if (CreateOptions
& FILE_SYNCHRONOUS_IO_ALERT
)
342 FileObject
->Flags
|= (FO_ALERTABLE_IO
| FO_SYNCHRONOUS_IO
);
344 if (CreateOptions
& FILE_SYNCHRONOUS_IO_NONALERT
)
346 FileObject
->Flags
|= FO_SYNCHRONOUS_IO
;
349 SecurityContext
.SecurityQos
= NULL
; /* ?? */
350 SecurityContext
.AccessState
= NULL
; /* ?? */
351 SecurityContext
.DesiredAccess
= DesiredAccess
;
352 SecurityContext
.FullCreateOptions
= 0; /* ?? */
354 KeInitializeEvent(&FileObject
->Lock
, NotificationEvent
, TRUE
);
355 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
357 DPRINT("FileObject %x\n", FileObject
);
358 DPRINT("FileObject->DeviceObject %x\n", FileObject
->DeviceObject
);
360 * Create a new IRP to hand to
361 * the FS driver: this may fail
362 * due to resource shortage.
364 Irp
= IoAllocateIrp(FileObject
->DeviceObject
->StackSize
, FALSE
);
367 ZwClose(*FileHandle
);
368 return (STATUS_UNSUCCESSFUL
);
371 Irp
->UserIosb
= &IoSB
; //return iostatus
372 Irp
->AssociatedIrp
.SystemBuffer
= EaBuffer
;
373 Irp
->Tail
.Overlay
.AuxiliaryBuffer
= (PCHAR
)ExtraCreateParameters
;
374 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
375 Irp
->UserEvent
= &Event
;
378 * Get the stack location for the new
379 * IRP and prepare it.
381 StackLoc
= IoGetNextIrpStackLocation(Irp
);
382 switch (CreateFileType
)
385 case CreateFileTypeNone
:
386 StackLoc
->MajorFunction
= IRP_MJ_CREATE
;
389 case CreateFileTypeNamedPipe
:
390 StackLoc
->MajorFunction
= IRP_MJ_CREATE_NAMED_PIPE
;
393 case CreateFileTypeMailslot
:
394 StackLoc
->MajorFunction
= IRP_MJ_CREATE_MAILSLOT
;
397 StackLoc
->MinorFunction
= 0;
399 StackLoc
->Control
= 0;
400 StackLoc
->DeviceObject
= FileObject
->DeviceObject
;
401 StackLoc
->FileObject
= FileObject
;
402 StackLoc
->Parameters
.Create
.SecurityContext
= &SecurityContext
;
403 StackLoc
->Parameters
.Create
.Options
= (CreateOptions
& FILE_VALID_OPTION_FLAGS
);
404 StackLoc
->Parameters
.Create
.Options
|= (CreateDisposition
<< 24);
405 StackLoc
->Parameters
.Create
.FileAttributes
= FileAttributes
;
406 StackLoc
->Parameters
.Create
.ShareAccess
= ShareAccess
;
407 StackLoc
->Parameters
.Create
.EaLength
= EaLength
;
410 * Now call the driver and
411 * possibly wait if it can
412 * not complete the request
415 Status
= IofCallDriver(FileObject
->DeviceObject
, Irp
);
417 if (Status
== STATUS_PENDING
)
419 KeWaitForSingleObject(&Event
,
424 Status
= IoSB
.Status
;
426 if (!NT_SUCCESS(Status
))
428 DPRINT("Failing create request with status %x\n", Status
);
429 ZwClose(*FileHandle
);
433 *IoStatusBlock
= IoSB
;
435 assert_irql(PASSIVE_LEVEL
);
437 DPRINT("Finished IoCreateFile() (*FileHandle) %x\n", (*FileHandle
));
443 /**********************************************************************
448 * Entry point to call IoCreateFile with
449 * default parameters.
459 * Code originally in NtCreateFile moved in IoCreateFile.
462 NtCreateFile(PHANDLE FileHandle
,
463 ACCESS_MASK DesiredAccess
,
464 POBJECT_ATTRIBUTES ObjectAttributes
,
465 PIO_STATUS_BLOCK IoStatusBlock
,
466 PLARGE_INTEGER AllocateSize
,
467 ULONG FileAttributes
,
469 ULONG CreateDisposition
,
474 return IoCreateFile(FileHandle
,
491 /**********************************************************************
496 * Opens an existing file (simpler than NtCreateFile).
500 * Variable that receives the file handle on return;
503 * Access desired by the caller to the file;
506 * Structue describing the file to be opened;
508 * IoStatusBlock (OUT)
509 * Receives details about the result of the
513 * Type of shared access the caller requires;
516 * Options for the file open.
525 NtOpenFile(PHANDLE FileHandle
,
526 ACCESS_MASK DesiredAccess
,
527 POBJECT_ATTRIBUTES ObjectAttributes
,
528 PIO_STATUS_BLOCK IoStatusBlock
,
532 return IoCreateFile(FileHandle
,