2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/filesystems/msfs/rw.c
5 * PURPOSE: Mailslot filesystem
6 * PROGRAMMER: Eric Kohl
9 /* INCLUDES ******************************************************************/
16 /* FUNCTIONS *****************************************************************/
19 MsfsRead(PDEVICE_OBJECT DeviceObject
,
22 PIO_STACK_LOCATION IoStack
;
23 PFILE_OBJECT FileObject
;
26 PMSFS_MESSAGE Message
;
32 PLARGE_INTEGER Timeout
;
34 DPRINT("MsfsRead(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
36 IoStack
= IoGetCurrentIrpStackLocation (Irp
);
37 FileObject
= IoStack
->FileObject
;
38 Fcb
= (PMSFS_FCB
)FileObject
->FsContext
;
39 Ccb
= (PMSFS_CCB
)FileObject
->FsContext2
;
41 DPRINT("MailslotName: %wZ\n", &Fcb
->Name
);
43 /* reading is not permitted on client side */
44 if (Fcb
->ServerCcb
!= Ccb
)
46 Irp
->IoStatus
.Status
= STATUS_ACCESS_DENIED
;
47 Irp
->IoStatus
.Information
= 0;
49 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
51 return STATUS_ACCESS_DENIED
;
54 Length
= IoStack
->Parameters
.Read
.Length
;
56 Buffer
= MmGetSystemAddressForMdlSafe(Irp
->MdlAddress
, NormalPagePriority
);
58 Buffer
= Irp
->UserBuffer
;
60 if (Fcb
->TimeOut
.QuadPart
== -1LL)
63 Timeout
= &Fcb
->TimeOut
;
65 Status
= KeWaitForSingleObject(&Fcb
->MessageEvent
,
70 if (Status
!= STATUS_USER_APC
)
72 if (Fcb
->MessageCount
> 0)
74 /* copy current message into buffer */
75 Message
= CONTAINING_RECORD(Fcb
->MessageListHead
.Flink
,
79 memcpy(Buffer
, &Message
->Buffer
, min(Message
->Size
,Length
));
80 LengthRead
= Message
->Size
;
82 KeAcquireSpinLock(&Fcb
->MessageListLock
, &oldIrql
);
83 RemoveHeadList(&Fcb
->MessageListHead
);
84 KeReleaseSpinLock(&Fcb
->MessageListLock
, oldIrql
);
86 ExFreePoolWithTag(Message
, 'rFsM');
88 if (Fcb
->MessageCount
== 0)
90 KeClearEvent(&Fcb
->MessageEvent
);
95 /* No message found after waiting */
96 Status
= STATUS_IO_TIMEOUT
;
100 Irp
->IoStatus
.Status
= Status
;
101 Irp
->IoStatus
.Information
= LengthRead
;
103 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
110 MsfsWrite(PDEVICE_OBJECT DeviceObject
,
113 PIO_STACK_LOCATION IoStack
;
114 PFILE_OBJECT FileObject
;
117 PMSFS_MESSAGE Message
;
122 DPRINT("MsfsWrite(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
124 IoStack
= IoGetCurrentIrpStackLocation (Irp
);
125 FileObject
= IoStack
->FileObject
;
126 Fcb
= (PMSFS_FCB
)FileObject
->FsContext
;
127 Ccb
= (PMSFS_CCB
)FileObject
->FsContext2
;
129 DPRINT("MailslotName: %wZ\n", &Fcb
->Name
);
131 /* writing is not permitted on server side */
132 if (Fcb
->ServerCcb
== Ccb
)
134 Irp
->IoStatus
.Status
= STATUS_ACCESS_DENIED
;
135 Irp
->IoStatus
.Information
= 0;
137 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
139 return STATUS_ACCESS_DENIED
;
142 Length
= IoStack
->Parameters
.Write
.Length
;
144 Buffer
= MmGetSystemAddressForMdlSafe(Irp
->MdlAddress
, NormalPagePriority
);
146 Buffer
= Irp
->UserBuffer
;
148 DPRINT("Length: %lu Message: %s\n", Length
, (PUCHAR
)Buffer
);
150 /* Allocate new message */
151 Message
= ExAllocatePoolWithTag(NonPagedPool
,
152 sizeof(MSFS_MESSAGE
) + Length
,
156 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
157 Irp
->IoStatus
.Information
= 0;
159 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
161 return STATUS_NO_MEMORY
;
164 Message
->Size
= Length
;
165 memcpy(&Message
->Buffer
, Buffer
, Length
);
167 KeAcquireSpinLock(&Fcb
->MessageListLock
, &oldIrql
);
168 InsertTailList(&Fcb
->MessageListHead
, &Message
->MessageListEntry
);
169 KeReleaseSpinLock(&Fcb
->MessageListLock
, oldIrql
);
172 if (Fcb
->MessageCount
== 1)
174 KeSetEvent(&Fcb
->MessageEvent
,
179 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
180 Irp
->IoStatus
.Information
= Length
;
182 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
184 return STATUS_SUCCESS
;