3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: services/fs/ms/rw.c
6 * PURPOSE: Mailslot filesystem
7 * PROGRAMMER: Eric Kohl <ekohl@rz-online.de>
10 /* INCLUDES ******************************************************************/
18 /* FUNCTIONS *****************************************************************/
21 MsfsRead(PDEVICE_OBJECT DeviceObject
,
24 PIO_STACK_LOCATION IoStack
;
25 PFILE_OBJECT FileObject
;
26 PMSFS_MAILSLOT Mailslot
;
28 PMSFS_MESSAGE Message
;
35 DPRINT("MsfsRead(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
37 IoStack
= IoGetCurrentIrpStackLocation (Irp
);
38 FileObject
= IoStack
->FileObject
;
39 Fcb
= (PMSFS_FCB
)FileObject
->FsContext
;
40 Mailslot
= Fcb
->Mailslot
;
42 DPRINT("MailslotName: %wZ\n", &Mailslot
->Name
);
44 /* reading is not permitted on client side */
45 if (Fcb
->Mailslot
->ServerFcb
!= Fcb
)
47 Irp
->IoStatus
.Status
= STATUS_ACCESS_DENIED
;
48 Irp
->IoStatus
.Information
= 0;
50 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
52 return(STATUS_ACCESS_DENIED
);
55 Length
= IoStack
->Parameters
.Read
.Length
;
57 Buffer
= MmGetSystemAddressForMdl (Irp
->MdlAddress
);
59 Buffer
= Irp
->UserBuffer
;
61 Status
= KeWaitForSingleObject(&Mailslot
->MessageEvent
,
65 NULL
); /* FIXME: handle timeout */
66 if ((NT_SUCCESS(Status
)) && (Mailslot
->MessageCount
> 0))
68 /* copy current message into buffer */
69 Message
= CONTAINING_RECORD(Mailslot
->MessageListHead
.Flink
,
72 memcpy(Buffer
, &Message
->Buffer
, min(Message
->Size
,Length
));
73 LengthRead
= Message
->Size
;
75 KeAcquireSpinLock(&Mailslot
->MessageListLock
, &oldIrql
);
76 RemoveHeadList(&Mailslot
->MessageListHead
);
77 KeReleaseSpinLock(&Mailslot
->MessageListLock
, oldIrql
);
80 Mailslot
->MessageCount
--;
81 if (Mailslot
->MessageCount
== 0)
83 KeClearEvent(&Mailslot
->MessageEvent
);
87 Irp
->IoStatus
.Status
= Status
;
88 Irp
->IoStatus
.Information
= LengthRead
;
90 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
97 MsfsWrite(PDEVICE_OBJECT DeviceObject
,
100 PIO_STACK_LOCATION IoStack
;
101 PFILE_OBJECT FileObject
;
102 PMSFS_MAILSLOT Mailslot
;
104 PMSFS_MESSAGE Message
;
109 DPRINT("MsfsWrite(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
111 IoStack
= IoGetCurrentIrpStackLocation (Irp
);
112 FileObject
= IoStack
->FileObject
;
113 Fcb
= (PMSFS_FCB
)FileObject
->FsContext
;
114 Mailslot
= Fcb
->Mailslot
;
116 DPRINT("MailslotName: %wZ\n", &Mailslot
->Name
);
118 /* writing is not permitted on server side */
119 if (Fcb
->Mailslot
->ServerFcb
== Fcb
)
121 Irp
->IoStatus
.Status
= STATUS_ACCESS_DENIED
;
122 Irp
->IoStatus
.Information
= 0;
124 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
126 return(STATUS_ACCESS_DENIED
);
129 Length
= IoStack
->Parameters
.Write
.Length
;
131 Buffer
= MmGetSystemAddressForMdl (Irp
->MdlAddress
);
133 Buffer
= Irp
->UserBuffer
;
135 DPRINT("Length: %lu Message: %s\n", Length
, (PUCHAR
)Buffer
);
137 /* Allocate new message */
138 Message
= ExAllocatePool(NonPagedPool
,
139 sizeof(MSFS_MESSAGE
) + Length
);
142 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
143 Irp
->IoStatus
.Information
= 0;
145 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
147 return(STATUS_NO_MEMORY
);
150 Message
->Size
= Length
;
151 memcpy(&Message
->Buffer
, Buffer
, Length
);
153 KeAcquireSpinLock(&Mailslot
->MessageListLock
, &oldIrql
);
154 InsertTailList(&Mailslot
->MessageListHead
, &Message
->MessageListEntry
);
155 KeReleaseSpinLock(&Mailslot
->MessageListLock
, oldIrql
);
157 Mailslot
->MessageCount
++;
158 if (Mailslot
->MessageCount
== 1)
160 KeSetEvent(&Mailslot
->MessageEvent
,
165 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
166 Irp
->IoStatus
.Information
= Length
;
168 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
170 return(STATUS_SUCCESS
);