forked from winfsp/winfsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwq.c
176 lines (149 loc) · 5.45 KB
/
wq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @file sys/wq.c
*
* @copyright 2015-2019 Bill Zissimopoulos
*/
/*
* This file is part of WinFsp.
*
* You can redistribute it and/or modify it under the terms of the GNU
* General Public License version 3 as published by the Free Software
* Foundation.
*
* Licensees holding a valid commercial license may use this software
* in accordance with the commercial license agreement provided in
* conjunction with the software. The terms and conditions of any such
* commercial license agreement shall govern, supersede, and render
* ineffective any application of the GPLv3 license to this software,
* notwithstanding of any reference thereto in the software or
* associated repository.
*/
#include <sys/driver.h>
NTSTATUS FspWqCreateAndPostIrpWorkItem(PIRP Irp,
FSP_IOP_REQUEST_WORK *WorkRoutine, FSP_IOP_REQUEST_FINI *RequestFini,
BOOLEAN CreateAndPost);
VOID FspWqPostIrpWorkItem(PIRP Irp);
static VOID FspWqWorkRoutine(PVOID Context);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, FspWqCreateAndPostIrpWorkItem)
#pragma alloc_text(PAGE, FspWqPostIrpWorkItem)
#pragma alloc_text(PAGE, FspWqWorkRoutine)
#endif
static inline
NTSTATUS FspWqPrepareIrpWorkItem(PIRP Irp)
{
NTSTATUS Result = STATUS_SUCCESS;
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
/* lock/buffer the user buffer */
if ((IRP_MJ_READ == IrpSp->MajorFunction || IRP_MJ_WRITE == IrpSp->MajorFunction) &&
!FlagOn(IrpSp->MinorFunction, IRP_MN_MDL))
{
if (IRP_MJ_READ == IrpSp->MajorFunction)
Result = FspLockUserBuffer(Irp, IrpSp->Parameters.Read.Length, IoWriteAccess);
else
Result = FspLockUserBuffer(Irp, IrpSp->Parameters.Write.Length, IoReadAccess);
}
else
if (IRP_MJ_DIRECTORY_CONTROL == IrpSp->MajorFunction)
Result = FspLockUserBuffer(Irp, IrpSp->Parameters.QueryDirectory.Length, IoWriteAccess);
return Result;
}
NTSTATUS FspWqCreateAndPostIrpWorkItem(PIRP Irp,
FSP_IOP_REQUEST_WORK *WorkRoutine, FSP_IOP_REQUEST_FINI *RequestFini,
BOOLEAN CreateAndPost)
{
PAGED_CODE();
FSP_FSCTL_TRANSACT_REQ *Request = FspIrpRequest(Irp);
FSP_FSCTL_TRANSACT_REQ_WORK_ITEM *RequestWorkItem;
NTSTATUS Result;
if (0 == Request)
{
Result = FspWqPrepareIrpWorkItem(Irp);
if (!NT_SUCCESS(Result))
return Result;
Result = FspIopCreateRequestAndWorkItem(Irp, 0, RequestFini, &Request);
if (!NT_SUCCESS(Result))
return Result;
RequestWorkItem = FspIopRequestWorkItem(Request);
RequestWorkItem->WorkRoutine = WorkRoutine;
ExInitializeWorkItem(&RequestWorkItem->WorkQueueItem, FspWqWorkRoutine, Irp);
}
else if (0 == FspIopRequestWorkItem(Request))
{
Result = FspWqPrepareIrpWorkItem(Irp);
if (!NT_SUCCESS(Result))
return Result;
Result = FspIopCreateRequestWorkItem(Request);
if (!NT_SUCCESS(Result))
return Result;
RequestWorkItem = FspIopRequestWorkItem(Request);
RequestWorkItem->WorkRoutine = WorkRoutine;
ExInitializeWorkItem(&RequestWorkItem->WorkQueueItem, FspWqWorkRoutine, Irp);
}
ASSERT(RequestFini == ((FSP_FSCTL_TRANSACT_REQ_HEADER *)
((PUINT8)Request - sizeof(FSP_FSCTL_TRANSACT_REQ_HEADER)))->RequestFini);
if (!CreateAndPost)
return STATUS_SUCCESS;
FspWqPostIrpWorkItem(Irp);
return STATUS_PENDING;
}
VOID FspWqPostIrpWorkItem(PIRP Irp)
{
PAGED_CODE();
FSP_FSCTL_TRANSACT_REQ *Request = FspIrpRequest(Irp);
FSP_FSCTL_TRANSACT_REQ_WORK_ITEM *RequestWorkItem = FspIopRequestWorkItem(Request);
#if DBG
switch (IoGetCurrentIrpStackLocation(Irp)->MajorFunction)
{
case IRP_MJ_CREATE:
ASSERT(0 != FspIopIrpResponse(Irp));
break;
case IRP_MJ_READ:
case IRP_MJ_WRITE:
case IRP_MJ_DIRECTORY_CONTROL:
case IRP_MJ_LOCK_CONTROL:
break;
default:
ASSERT(0);
break;
}
#endif
ASSERT(Request->Hint == (UINT_PTR)Irp);
ASSERT(0 != RequestWorkItem);
ASSERT(0 != RequestWorkItem->WorkRoutine);
IoMarkIrpPending(Irp);
ExQueueWorkItem(&RequestWorkItem->WorkQueueItem, CriticalWorkQueue);
}
static VOID FspWqWorkRoutine(PVOID Context)
{
PAGED_CODE();
PIRP Irp = Context;
PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
PDEVICE_OBJECT DeviceObject = IrpSp->DeviceObject;
FSP_FSCTL_TRANSACT_REQ *Request = FspIrpRequest(Irp);
FSP_FSCTL_TRANSACT_REQ_WORK_ITEM *RequestWorkItem = FspIopRequestWorkItem(Request);
FSP_IOP_REQUEST_WORK *WorkRoutine = RequestWorkItem->WorkRoutine;
NTSTATUS Result;
IoSetTopLevelIrp(Irp);
Result = WorkRoutine(DeviceObject, Irp, IrpSp, TRUE);
if (STATUS_PENDING != Result && !(FSP_STATUS_IGNORE_BIT & Result))
{
ASSERT(0 == (FSP_STATUS_PRIVATE_BIT & Result) ||
FSP_STATUS_IOQ_POST == Result || FSP_STATUS_IOQ_POST_BEST_EFFORT == Result);
DEBUGLOGIRP(Irp, Result);
if (FSP_STATUS_PRIVATE_BIT & Result)
{
FSP_FSVOL_DEVICE_EXTENSION *FsvolDeviceExtension =
FspFsvolDeviceExtension(DeviceObject);
if (!FspIoqPostIrpEx(FsvolDeviceExtension->Ioq, Irp,
FSP_STATUS_IOQ_POST_BEST_EFFORT == Result, &Result))
{
DEBUGLOG("FspIoqPostIrpEx = %s", NtStatusSym(Result));
FspIopCompleteIrp(Irp, Result);
}
}
else
FspIopCompleteIrp(Irp, Result);
}
IoSetTopLevelIrp(0);
}