ReactOS  0.4.15-dev-1636-gf634010
scratch.h
Go to the documentation of this file.
1 /*++
2 
3 Copyright (C) Microsoft Corporation. All rights reserved.
4 
5 Module Name:
6 
7  mmc.h
8 
9 Abstract:
10 
11  Functions/macros for using the scratch buffer and scratch request/SRB
12  in the serial I/O queue context
13 
14 Author:
15 
16 Environment:
17 
18  kernel mode only
19 
20 Notes:
21 
22 
23 Revision History:
24 
25 --*/
26 
27 #ifndef __SCRATCH_H__
28 #define __SCRATCH_H__
29 
30 
32 VOID
33 ScratchBuffer_Deallocate(
34  _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
35  );
36 
38 BOOLEAN
39 ScratchBuffer_Allocate(
40  _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
41  );
42 
43 VOID
45  _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
46  _In_ BOOLEAN ResetRequestHistory
47  );
48 
50 VOID
51 ScratchBuffer_SetupSrb(
52  _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
56  );
57 
58 VOID
60  _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
61  _In_ WDFREQUEST OriginalRequest,
65  _In_ BOOLEAN IsReadRequest,
66  _In_ BOOLEAN UsePartialMdl
67  );
68 
71  _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
72  _In_ BOOLEAN SynchronousSrb,
73  _When_(SynchronousSrb, _Pre_null_)
74  _When_(!SynchronousSrb, _In_opt_)
75  PSRB_HISTORY_ITEM *SrbHistoryItem
76  );
77 
80  _In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
82  );
83 
84 #if DBG
85  #define ScratchBuffer_BeginUse(context) ScratchBuffer_BeginUseX((context), __FILE__, __LINE__)
86 #else
87  #define ScratchBuffer_BeginUse(context) ScratchBuffer_BeginUseX((context), NULL, (ULONG)-1)
88 #endif
89 
91 {
92  // NOTE: these are not "real" locks. They are simply to help
93  // avoid multiple uses of the scratch buffer. Thus, it
94  // is not critical to have atomic operations here.
95  PVOID tmp = InterlockedCompareExchangePointer((PVOID)&(DeviceExtension->ScratchContext.ScratchInUse), (PVOID)-1, NULL);
96  NT_ASSERT(tmp == NULL);
97  UNREFERENCED_PARAMETER(tmp); //defensive coding, avoid PREFAST warning.
98  DeviceExtension->ScratchContext.ScratchInUseFileName = File;
99  DeviceExtension->ScratchContext.ScratchInUseLineNumber = Line;
100  ScratchBuffer_ResetItems(DeviceExtension, TRUE);
101  RequestClearSendTime(DeviceExtension->ScratchContext.ScratchRequest);
102  return;
103 }
105 {
106  // NOTE: these are not "real" locks. They are simply to help
107  // avoid multiple uses of the scratch buffer. Thus, it
108  // is not critical to have atomic operations here.
109 
110  // On lock release, we erase ScratchInUseFileName and ScratchInUseLineNumber _before_ releasing ScratchInUse,
111  // because otherwise we may erase these after the lock has been acquired again by another thread. We store the
112  // old values of ScratchInUseFileName and ScratchInUseLineNumber in local variables to facilitate debugging,
113  // if the ASSERT at the end of the function is hit.
114  PCSTR scratchInUseFileName;
115  ULONG scratchInUseLineNumber;
116  PVOID tmp;
117 
118  scratchInUseFileName = DeviceExtension->ScratchContext.ScratchInUseFileName;
119  scratchInUseLineNumber = DeviceExtension->ScratchContext.ScratchInUseLineNumber;
120  UNREFERENCED_PARAMETER(scratchInUseFileName);
121  UNREFERENCED_PARAMETER(scratchInUseLineNumber);
122  DeviceExtension->ScratchContext.ScratchInUseFileName = NULL;
123  DeviceExtension->ScratchContext.ScratchInUseLineNumber = 0;
124 
125  //
126  // If we have used the PartialMdl in the scratch context we should notify MM that we will be reusing it
127  // otherwise it may leak System VA if some one below us has mapped the same.
128  //
129 
130  if (DeviceExtension->ScratchContext.PartialMdlIsBuilt != FALSE)
131  {
132  MmPrepareMdlForReuse(DeviceExtension->ScratchContext.PartialMdl);
133  DeviceExtension->ScratchContext.PartialMdlIsBuilt = FALSE;
134  }
135 
136  tmp = InterlockedCompareExchangePointer((PVOID)&(DeviceExtension->ScratchContext.ScratchInUse), NULL, (PVOID)-1);
137  NT_ASSERT(tmp == ((PVOID)-1));
138  UNREFERENCED_PARAMETER(tmp); //defensive coding, avoid PREFAST warning.
139  return;
140 }
141 
142 VOID
145  );
146 
147 VOID
150  );
151 
153 NTSTATUS
154 ScratchBuffer_ExecuteCdbEx(
155  _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
159  _In_ PCDB Cdb,
162  );
163 
165 FORCEINLINE // __REACTOS__
166 NTSTATUS
167 ScratchBuffer_ExecuteCdb(
168  _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
169  _In_opt_ WDFREQUEST OriginalRequest,
172  _In_ PCDB Cdb,
174  )
175 {
176  return ScratchBuffer_ExecuteCdbEx(DeviceExtension,
178  TransferSize,
180  Cdb,
182  0);
183 }
184 
186 
187 #endif //__SCRATCH_H__
_IRQL_requires_max_(APC_LEVEL) VOID ScratchBuffer_Deallocate(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension)
Definition: cddata.c:197
#define _Pre_null_
Definition: no_sal2.h:512
FORCEINLINE VOID ScratchBuffer_EndUse(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension)
Definition: scratch.h:104
VOID CompressSrbHistoryData(_Inout_ PSRB_HISTORY RequestHistory)
Definition: scratch.c:1033
_In_ PFCB _In_ LONGLONG StartingOffset
Definition: cdprocs.h:290
_In_opt_ WDFREQUEST _In_ ULONG _In_ BOOLEAN GetDataFromDevice
Definition: scratch.h:53
#define TRUE
Definition: types.h:120
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
Definition: cdrw_hw.h:28
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS ScratchBuffer_SendSrb(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension, _In_ BOOLEAN SynchronousSrb, _When_(SynchronousSrb, _Pre_null_) _When_(!SynchronousSrb, _In_opt_) PSRB_HISTORY_ITEM *SrbHistoryItem)
Definition: scratch.c:906
NTSTATUS ScratchBuffer_PerformNextReadWrite(_In_ PCDROM_DEVICE_EXTENSION DeviceExtension, _In_ BOOLEAN FirstTry)
Definition: scratch.c:429
_In_opt_ WDFREQUEST _In_ ULONG MaximumTransferLength
Definition: scratch.h:53
#define _In_opt_
Definition: no_sal2.h:212
struct Line Line
Definition: wdfdevice.h:4081
_In_opt_ PIRP _In_ PSCSI_REQUEST_BLOCK _In_ UCHAR _In_ ULONG _In_ ULONG _In_opt_ SRB_HISTORY * RequestHistory
Definition: classpnp.h:482
#define FALSE
Definition: types.h:117
_In_opt_ WDFREQUEST OriginalRequest
Definition: scratch.h:53
#define InterlockedCompareExchangePointer
Definition: interlocked.h:129
unsigned char BOOLEAN
VOID RequestClearSendTime(_In_ WDFREQUEST Request)
Definition: common.c:111
const char * LPCSTR
Definition: xmlstorage.h:183
VOID ScratchBuffer_SetupReadWriteSrb(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension, _In_ WDFREQUEST OriginalRequest, _In_ LARGE_INTEGER StartingOffset, _In_ ULONG RequiredLength, _Inout_updates_bytes_(RequiredLength) UCHAR *DataBuffer, _In_ BOOLEAN IsReadRequest, _In_ BOOLEAN UsePartialMdl)
Definition: scratch.c:1212
_In_opt_ WDFREQUEST _In_ ULONG _In_ BOOLEAN _In_ PCDB _In_ UCHAR OprationLength
Definition: scratch.h:156
#define _Inout_updates_bytes_(s)
Definition: no_sal2.h:184
VOID ValidateSrbHistoryDataPresumptions(_In_ SRB_HISTORY const *RequestHistory)
Definition: scratch.c:1135
_In_opt_ WDFREQUEST _In_ ULONG _In_ BOOLEAN _In_ PCDB Cdb
Definition: scratch.h:156
#define _Inout_
Definition: no_sal2.h:162
Definition: ncftp.h:79
unsigned char UCHAR
Definition: xmlstorage.h:181
_In_opt_ WDFREQUEST _In_ ULONG _In_ BOOLEAN _In_ PCDB _In_ UCHAR _In_ ULONG TimeoutValue
Definition: scratch.h:156
FORCEINLINE VOID ScratchBuffer_BeginUseX(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension, _In_opt_ LPCSTR File, ULONG Line)
Definition: scratch.h:90
Definition: copy.c:32
#define MmPrepareMdlForReuse(_Mdl)
#define _In_
Definition: no_sal2.h:158
#define FORCEINLINE
Definition: wdftypes.h:67
_In_ ULONG _Out_opt_ PULONG RequiredLength
Definition: wmifuncs.h:29
#define NULL
Definition: types.h:112
KDEFERRED_ROUTINE ScratchBuffer_ReadWriteTimerRoutine
Definition: scratch.h:185
#define _When_(c, a)
Definition: no_sal2.h:38
unsigned int ULONG
Definition: retypes.h:1
const char * PCSTR
Definition: typedefs.h:52
Definition: File.h:15
_In_opt_ WDFREQUEST _In_ ULONG TransferSize
Definition: scratch.h:156
_Must_inspect_result_ _In_ WDFDEVICE _In_ BOOLEAN _In_opt_ PVOID _In_ LONG _In_z_ PCHAR File
Definition: wdfdevice.h:4061
#define APC_LEVEL
Definition: env_spec_w32.h:695
VOID ScratchBuffer_ResetItems(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension, _In_ BOOLEAN ResetRequestHistory)
Definition: scratch.c:343
#define NT_ASSERT
Definition: rtlfuncs.h:3312