1 /* $Id: create.c,v 1.44 2001/06/14 21:05:07 jfilby 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 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)*2,
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");
129 return (STATUS_UNSUCCESSFUL
);
132 if (DeviceObject
->DeviceType
!= FILE_DEVICE_NETWORK
133 && (DeviceObject
->DeviceType
!= FILE_DEVICE_NAMED_PIPE
)
134 && (DeviceObject
->DeviceType
!= FILE_DEVICE_MAILSLOT
))
136 if (!(DeviceObject
->Vpb
->Flags
& VPB_MOUNTED
))
138 DPRINT("Trying to mount storage device\n");
139 Status
= IoTryToMountStorageDevice (DeviceObject
);
140 DPRINT("Status %x\n", Status
);
141 if (!NT_SUCCESS(Status
))
143 CPRINT("Failed to mount storage device (statux %x)\n",
147 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
150 RtlCreateUnicodeString(&(FileObject
->FileName
),
153 DPRINT("FileObject->FileName.Buffer %S\n",
154 FileObject
->FileName
.Buffer
);
155 FileObject
->DeviceObject
= DeviceObject
;
156 DPRINT("FileObject %x DeviceObject %x\n",
159 FileObject
->Vpb
= DeviceObject
->Vpb
;
160 FileObject
->Type
= InternalFileType
;
162 return (STATUS_SUCCESS
);
166 /**********************************************************************
168 * IoCreateStreamFileObject@8
187 IoCreateStreamFileObject(PFILE_OBJECT FileObject
,
188 PDEVICE_OBJECT DeviceObject
)
191 PFILE_OBJECT CreatedFileObject
;
193 DPRINT("IoCreateStreamFileObject(FileObject %x, DeviceObject %x)\n",
194 FileObject
, DeviceObject
);
196 assert_irql (PASSIVE_LEVEL
);
198 CreatedFileObject
= ObCreateObject (&FileHandle
,
199 STANDARD_RIGHTS_REQUIRED
,
202 if (NULL
== CreatedFileObject
)
204 DPRINT("Could not create FileObject\n");
208 if (FileObject
!= NULL
)
210 DeviceObject
= FileObject
->DeviceObject
;
212 DeviceObject
= IoGetAttachedDevice(DeviceObject
);
214 DPRINT("DeviceObject %x\n", DeviceObject
);
216 CreatedFileObject
->DeviceObject
= DeviceObject
;
217 CreatedFileObject
->Vpb
= DeviceObject
->Vpb
;
218 CreatedFileObject
->Type
= InternalFileType
;
219 CreatedFileObject
->Flags
|= FO_DIRECT_DEVICE_OPEN
;
221 ZwClose (FileHandle
);
223 return (CreatedFileObject
);
227 /**********************************************************************
232 * Either causes a new file or directory to be created, or it
233 * opens an existing file, device, directory or volume, giving
234 * the caller a handle for the file object. This handle can be
235 * used by subsequent calls to manipulate data within the file
236 * or the file object's state of attributes.
240 * Points to a variable which receives the file handle
244 * Desired access to the file;
247 * Structure describing the file;
249 * IoStatusBlock (OUT)
250 * Receives information about the operation on return;
252 * AllocationSize [OPTIONAL]
253 * Initial size of the file in bytes;
256 * Attributes to create the file with;
259 * Type of shared access the caller would like to the
263 * Specifies what to do, depending on whether the
264 * file already exists;
267 * Options for creating a new file;
269 * EaBuffer [OPTIONAL]
276 * Type of file (normal, named pipe, mailslot) to create;
278 * ExtraCreateParameters [OPTIONAL]
279 * Additional creation data for named pipe and mailsots;
288 * Prototype taken from Bo Branten's ntifs.h v15.
289 * Description taken from old NtCreateFile's which is
290 * now a wrapper of this call.
297 OUT PHANDLE FileHandle
,
298 IN ACCESS_MASK DesiredAccess
,
299 IN POBJECT_ATTRIBUTES ObjectAttributes
,
300 OUT PIO_STATUS_BLOCK IoStatusBlock
,
301 IN PLARGE_INTEGER AllocationSize OPTIONAL
,
302 IN ULONG FileAttributes
,
303 IN ULONG ShareAccess
,
304 IN ULONG CreateDisposition
,
305 IN ULONG CreateOptions
,
306 IN PVOID EaBuffer OPTIONAL
,
308 IN CREATE_FILE_TYPE CreateFileType
,
309 IN PVOID ExtraCreateParameters OPTIONAL
,
312 PFILE_OBJECT FileObject
;
316 PIO_STACK_LOCATION StackLoc
;
318 DPRINT("IoCreateFile(FileHandle %x, DesiredAccess %x, "
319 "ObjectAttributes %x ObjectAttributes->ObjectName->Buffer %S)\n",
320 FileHandle
,DesiredAccess
,ObjectAttributes
,
321 ObjectAttributes
->ObjectName
->Buffer
);
323 assert_irql(PASSIVE_LEVEL
);
327 FileObject
= ObCreateObject(FileHandle
,
331 if (FileObject
== NULL
)
333 DPRINT1("ObCreateObject() failed!\n");
334 return (STATUS_UNSUCCESSFUL
);
336 if (CreateOptions
& FILE_SYNCHRONOUS_IO_ALERT
)
338 FileObject
->Flags
|= (FO_ALERTABLE_IO
| FO_SYNCHRONOUS_IO
);
340 if (CreateOptions
& FILE_SYNCHRONOUS_IO_NONALERT
)
342 FileObject
->Flags
|= FO_SYNCHRONOUS_IO
;
344 KeInitializeEvent(&FileObject
->Lock
, NotificationEvent
, TRUE
);
345 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
347 DPRINT("FileObject %x\n", FileObject
);
348 DPRINT("FileObject->DeviceObject %x\n", FileObject
->DeviceObject
);
350 * Create a new IRP to hand to
351 * the FS driver: this may fail
352 * due to resource shortage.
354 Irp
= IoAllocateIrp(FileObject
->DeviceObject
->StackSize
, FALSE
);
357 return (STATUS_UNSUCCESSFUL
);
360 Irp
->UserIosb
= IoStatusBlock
; //return iostatus
361 Irp
->AssociatedIrp
.SystemBuffer
= EaBuffer
;
362 Irp
->Tail
.Overlay
.AuxiliaryBuffer
= (PCHAR
)ExtraCreateParameters
;
365 * Get the stack location for the new
366 * IRP and prepare it.
368 StackLoc
= IoGetNextIrpStackLocation(Irp
);
369 switch (CreateFileType
)
372 case CreateFileTypeNone
:
373 StackLoc
->MajorFunction
= IRP_MJ_CREATE
;
376 case CreateFileTypeNamedPipe
:
377 StackLoc
->MajorFunction
= IRP_MJ_CREATE_NAMED_PIPE
;
380 case CreateFileTypeMailslot
:
381 StackLoc
->MajorFunction
= IRP_MJ_CREATE_MAILSLOT
;
384 StackLoc
->MinorFunction
= 0;
386 StackLoc
->Control
= 0;
387 StackLoc
->DeviceObject
= FileObject
->DeviceObject
;
388 StackLoc
->FileObject
= FileObject
;
389 StackLoc
->Parameters
.Create
.Options
= (CreateOptions
& FILE_VALID_OPTION_FLAGS
);
390 StackLoc
->Parameters
.Create
.Options
|= (CreateDisposition
<< 24);
393 * Now call the driver and
394 * possibly wait if it can
395 * not complete the request
398 Status
= IofCallDriver(FileObject
->DeviceObject
, Irp
);
400 if (Status
== STATUS_PENDING
)
402 KeWaitForSingleObject(&Event
,
407 Status
= IoStatusBlock
->Status
;
409 if (!NT_SUCCESS(Status
))
411 DPRINT("Failing create request with status %x\n", Status
);
412 ZwClose(*FileHandle
);
416 assert_irql(PASSIVE_LEVEL
);
418 DPRINT("Finished IoCreateFile() (*FileHandle) %x\n", (*FileHandle
));
424 /**********************************************************************
429 * Entry point to call IoCreateFile with
430 * default parameters.
440 * Code originally in NtCreateFile moved in IoCreateFile.
443 NtCreateFile(PHANDLE FileHandle
,
444 ACCESS_MASK DesiredAccess
,
445 POBJECT_ATTRIBUTES ObjectAttributes
,
446 PIO_STATUS_BLOCK IoStatusBlock
,
447 PLARGE_INTEGER AllocateSize
,
448 ULONG FileAttributes
,
450 ULONG CreateDisposition
,
455 return IoCreateFile(FileHandle
,
472 /**********************************************************************
477 * Opens an existing file (simpler than NtCreateFile).
481 * Variable that receives the file handle on return;
484 * Access desired by the caller to the file;
487 * Structue describing the file to be opened;
489 * IoStatusBlock (OUT)
490 * Receives details about the result of the
494 * Type of shared access the caller requires;
497 * Options for the file open.
506 NtOpenFile(PHANDLE FileHandle
,
507 ACCESS_MASK DesiredAccess
,
508 POBJECT_ATTRIBUTES ObjectAttributes
,
509 PIO_STATUS_BLOCK IoStatusBlock
,
513 return IoCreateFile(FileHandle
,