1 /* $Id: rw.c,v 1.4 2002/09/08 10:22:10 chorns Exp $
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 ******************************************************************/
12 #include <ddk/ntddk.h>
13 #include <ntos/minmax.h>
20 /* FUNCTIONS *****************************************************************/
23 MsfsRead(PDEVICE_OBJECT DeviceObject
,
26 PIO_STACK_LOCATION IoStack
;
27 PFILE_OBJECT FileObject
;
28 PMSFS_MAILSLOT Mailslot
;
30 PMSFS_MESSAGE Message
;
37 DPRINT("MsfsRead(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
39 IoStack
= IoGetCurrentIrpStackLocation (Irp
);
40 FileObject
= IoStack
->FileObject
;
41 Fcb
= (PMSFS_FCB
)FileObject
->FsContext
;
42 Mailslot
= Fcb
->Mailslot
;
44 DPRINT("MailslotName: %wZ\n", &Mailslot
->Name
);
46 /* reading is not permitted on client side */
47 if (Fcb
->Mailslot
->ServerFcb
!= Fcb
)
49 Irp
->IoStatus
.Status
= STATUS_ACCESS_DENIED
;
50 Irp
->IoStatus
.Information
= 0;
52 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
54 return(STATUS_ACCESS_DENIED
);
57 Length
= IoStack
->Parameters
.Read
.Length
;
59 Buffer
= MmGetSystemAddressForMdl (Irp
->MdlAddress
);
61 Buffer
= Irp
->UserBuffer
;
63 Status
= KeWaitForSingleObject(&Mailslot
->MessageEvent
,
67 NULL
); /* FIXME: handle timeout */
68 if ((NT_SUCCESS(Status
)) && (Mailslot
->MessageCount
> 0))
70 /* copy current message into buffer */
71 Message
= CONTAINING_RECORD(Mailslot
->MessageListHead
.Flink
,
74 memcpy(Buffer
, &Message
->Buffer
, min(Message
->Size
,Length
));
75 LengthRead
= Message
->Size
;
77 KeAcquireSpinLock(&Mailslot
->MessageListLock
, &oldIrql
);
78 RemoveHeadList(&Mailslot
->MessageListHead
);
79 KeReleaseSpinLock(&Mailslot
->MessageListLock
, oldIrql
);
82 Mailslot
->MessageCount
--;
83 if (Mailslot
->MessageCount
== 0)
85 KeClearEvent(&Mailslot
->MessageEvent
);
89 Irp
->IoStatus
.Status
= Status
;
90 Irp
->IoStatus
.Information
= LengthRead
;
92 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
99 MsfsWrite(PDEVICE_OBJECT DeviceObject
,
102 PIO_STACK_LOCATION IoStack
;
103 PFILE_OBJECT FileObject
;
104 PMSFS_MAILSLOT Mailslot
;
106 PMSFS_MESSAGE Message
;
111 DPRINT("MsfsWrite(DeviceObject %p Irp %p)\n", DeviceObject
, Irp
);
113 IoStack
= IoGetCurrentIrpStackLocation (Irp
);
114 FileObject
= IoStack
->FileObject
;
115 Fcb
= (PMSFS_FCB
)FileObject
->FsContext
;
116 Mailslot
= Fcb
->Mailslot
;
118 DPRINT("MailslotName: %wZ\n", &Mailslot
->Name
);
120 /* writing is not permitted on server side */
121 if (Fcb
->Mailslot
->ServerFcb
== Fcb
)
123 Irp
->IoStatus
.Status
= STATUS_ACCESS_DENIED
;
124 Irp
->IoStatus
.Information
= 0;
126 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
128 return(STATUS_ACCESS_DENIED
);
131 Length
= IoStack
->Parameters
.Write
.Length
;
133 Buffer
= MmGetSystemAddressForMdl (Irp
->MdlAddress
);
135 Buffer
= Irp
->UserBuffer
;
137 DPRINT("Length: %lu Message: %s\n", Length
, (PUCHAR
)Buffer
);
139 /* Allocate new message */
140 Message
= ExAllocatePool(NonPagedPool
,
141 sizeof(MSFS_MESSAGE
) + Length
);
144 Irp
->IoStatus
.Status
= STATUS_NO_MEMORY
;
145 Irp
->IoStatus
.Information
= 0;
147 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
149 return(STATUS_NO_MEMORY
);
152 Message
->Size
= Length
;
153 memcpy(&Message
->Buffer
, Buffer
, Length
);
155 KeAcquireSpinLock(&Mailslot
->MessageListLock
, &oldIrql
);
156 InsertTailList(&Mailslot
->MessageListHead
, &Message
->MessageListEntry
);
157 KeReleaseSpinLock(&Mailslot
->MessageListLock
, oldIrql
);
159 Mailslot
->MessageCount
++;
160 if (Mailslot
->MessageCount
== 1)
162 KeSetEvent(&Mailslot
->MessageEvent
,
167 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
168 Irp
->IoStatus
.Information
= Length
;
170 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
172 return(STATUS_SUCCESS
);