Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 lines
12 KiB

  1. /*++
  2. Copyright (c) 1990-2000 Microsoft Corporation
  3. Module Name:
  4. remlock.c
  5. Abstract:
  6. This code implements remove locks.
  7. Authors:
  8. Peter Wieland
  9. Kenneth Ray
  10. Environment:
  11. kernel mode only
  12. Notes:
  13. Revision History:
  14. --*/
  15. #include "pnpmgrp.h"
  16. #pragma hdrstop
  17. #include <remlock.h>
  18. #pragma alloc_text(PAGE, IoInitializeRemoveLockEx)
  19. #pragma alloc_text(PAGE, IoReleaseRemoveLockAndWaitEx)
  20. #define MinutesToTicks(x) \
  21. (ULONGLONG) KeQueryTimeIncrement() * \
  22. 10 * \
  23. 1000 * \
  24. 1000 * \
  25. 60 * \
  26. x
  27. // 10 -> microseconds, 1000 -> miliseconds, 1000 -> seconds, 60 -> minutes
  28. typedef struct _IO_PRIVATE_REMOVE_LOCK {
  29. IO_REMOVE_LOCK_COMMON_BLOCK Common;
  30. IO_REMOVE_LOCK_DBG_BLOCK Dbg;
  31. } IO_PRIVATE_REMOVE_LOCK, *PIO_PRIVATE_REMOVE_LOCK;
  32. #define FREESIZE sizeof (IO_REMOVE_LOCK_COMMON_BLOCK)
  33. #define CHECKEDSIZE sizeof (IO_PRIVATE_REMOVE_LOCK)
  34. NTSYSAPI
  35. VOID
  36. NTAPI
  37. IoInitializeRemoveLockEx(
  38. IN PIO_REMOVE_LOCK PublicLock,
  39. IN ULONG AllocateTag, // Used only on checked kernels
  40. IN ULONG MaxLockedMinutes, // Used only on checked kernels
  41. IN ULONG HighWatermark, // Used only on checked kernels
  42. IN ULONG RemlockSize // are we checked or free
  43. )
  44. /*++
  45. Routine Description:
  46. This routine is called to initialize the remove lock for a device object.
  47. --*/
  48. {
  49. PIO_PRIVATE_REMOVE_LOCK Lock = (PIO_PRIVATE_REMOVE_LOCK) PublicLock;
  50. PAGED_CODE ();
  51. if (Lock) {
  52. switch (RemlockSize) {
  53. case CHECKEDSIZE:
  54. Lock->Dbg.Signature = IO_REMOVE_LOCK_SIG;
  55. Lock->Dbg.HighWatermark = HighWatermark;
  56. Lock->Dbg.MaxLockedTicks = MinutesToTicks (MaxLockedMinutes);
  57. Lock->Dbg.AllocateTag = AllocateTag;
  58. KeInitializeSpinLock (&Lock->Dbg.Spin);
  59. Lock->Dbg.LowMemoryCount = 0;
  60. Lock->Dbg.Blocks = NULL;
  61. //
  62. // fall through
  63. //
  64. case FREESIZE:
  65. Lock->Common.Removed = FALSE;
  66. Lock->Common.IoCount = 1;
  67. KeInitializeEvent(&Lock->Common.RemoveEvent,
  68. SynchronizationEvent,
  69. FALSE);
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. }
  76. NTSYSAPI
  77. NTSTATUS
  78. NTAPI
  79. IoAcquireRemoveLockEx(
  80. IN PIO_REMOVE_LOCK PublicLock,
  81. IN OPTIONAL PVOID Tag,
  82. IN PCSTR File,
  83. IN ULONG Line,
  84. IN ULONG RemlockSize // are we checked or free
  85. )
  86. /*++
  87. Routine Description:
  88. This routine is called to acquire the remove lock for a device object.
  89. While the lock is held, the caller can assume that no pending pnp REMOVE
  90. requests will be completed.
  91. The lock should be acquired immediately upon entering a dispatch routine.
  92. It should also be acquired before creating any new reference to the
  93. device object if there's a chance of releasing the reference before the
  94. new one is done.
  95. Arguments:
  96. RemoveLock - A pointer to an initialized REMOVE_LOCK structure.
  97. Tag - Used for tracking lock allocation and release. If an irp is
  98. specified when acquiring the lock then the same Tag must be
  99. used to release the lock before the Tag is completed.
  100. File - set to __FILE__ as the location in the code where the lock was taken.
  101. Line - set to __LINE__.
  102. Return Value:
  103. Returns whether or not the remove lock was obtained.
  104. If successful the caller should continue with work calling
  105. IoReleaseRemoveLock when finished.
  106. If not successful the lock was not obtained. The caller should abort the
  107. work but not call IoReleaseRemoveLock.
  108. --*/
  109. {
  110. PIO_PRIVATE_REMOVE_LOCK Lock = (PIO_PRIVATE_REMOVE_LOCK) PublicLock;
  111. LONG lockValue;
  112. NTSTATUS status;
  113. PIO_REMOVE_LOCK_TRACKING_BLOCK trackingBlock;
  114. //
  115. // Grab the remove lock
  116. //
  117. lockValue = InterlockedIncrement(&Lock->Common.IoCount);
  118. ASSERTMSG("IoAcquireRemoveLock - lock value was negative : ",
  119. (lockValue > 0));
  120. if (! Lock->Common.Removed) {
  121. switch (RemlockSize) {
  122. case CHECKEDSIZE:
  123. ASSERTMSG("RemoveLock increased to meet LockHighWatermark",
  124. ((0 == Lock->Dbg.HighWatermark) ||
  125. (lockValue <= Lock->Dbg.HighWatermark)));
  126. trackingBlock = ExAllocatePoolWithTag(
  127. NonPagedPool,
  128. sizeof(IO_REMOVE_LOCK_TRACKING_BLOCK),
  129. Lock->Dbg.AllocateTag);
  130. if (NULL == trackingBlock) {
  131. // ASSERTMSG ("insufficient resources", FALSE);
  132. InterlockedIncrement (& Lock->Dbg.LowMemoryCount);
  133. //
  134. // Let the acquire go through but without adding the
  135. // tracking block.
  136. // When we are later releasing the lock, but the tracking
  137. // block does not exist, deduct from this value to see if the
  138. // release was still valuable.
  139. //
  140. } else {
  141. KIRQL oldIrql;
  142. RtlZeroMemory (trackingBlock,
  143. sizeof (IO_REMOVE_LOCK_TRACKING_BLOCK));
  144. trackingBlock->Tag = Tag;
  145. trackingBlock->File = File;
  146. trackingBlock->Line = Line;
  147. KeQueryTickCount(&trackingBlock->TimeLocked);
  148. ExAcquireSpinLock (&Lock->Dbg.Spin, &oldIrql);
  149. trackingBlock->Link = Lock->Dbg.Blocks;
  150. Lock->Dbg.Blocks = trackingBlock;
  151. ExReleaseSpinLock(&Lock->Dbg.Spin, oldIrql);
  152. }
  153. break;
  154. case FREESIZE:
  155. break;
  156. default:
  157. break;
  158. }
  159. status = STATUS_SUCCESS;
  160. } else {
  161. if (0 == InterlockedDecrement (&Lock->Common.IoCount)) {
  162. KeSetEvent (&Lock->Common.RemoveEvent, 0, FALSE);
  163. }
  164. status = STATUS_DELETE_PENDING;
  165. }
  166. return status;
  167. }
  168. NTSYSAPI
  169. VOID
  170. NTAPI
  171. IoReleaseRemoveLockEx(
  172. IN PIO_REMOVE_LOCK PublicLock,
  173. IN PVOID Tag,
  174. IN ULONG RemlockSize // are we checked or free
  175. )
  176. /*++
  177. Routine Description:
  178. This routine is called to release the remove lock on the device object. It
  179. must be called when finished using a previously locked reference to the
  180. device object. If an Tag was specified when acquiring the lock then the
  181. same Tag must be specified when releasing the lock.
  182. When the lock count reduces to zero, this routine will signal the waiting
  183. event to release the waiting thread deleting the device object protected
  184. by this lock.
  185. Arguments:
  186. DeviceObject - the device object to lock
  187. Tag - The tag (if any) specified when acquiring the lock. This is used
  188. for lock tracking purposes
  189. Return Value:
  190. none
  191. --*/
  192. {
  193. PIO_PRIVATE_REMOVE_LOCK Lock = (PIO_PRIVATE_REMOVE_LOCK) PublicLock;
  194. LONG lockValue;
  195. KIRQL oldIrql;
  196. LARGE_INTEGER ticks;
  197. LONGLONG difference;
  198. BOOLEAN found;
  199. PIO_REMOVE_LOCK_TRACKING_BLOCK last;
  200. PIO_REMOVE_LOCK_TRACKING_BLOCK current;
  201. switch (RemlockSize) {
  202. case CHECKEDSIZE:
  203. //
  204. // Check the tick count and make sure this thing hasn't been locked
  205. // for more than MaxLockedMinutes.
  206. //
  207. found = FALSE;
  208. ExAcquireSpinLock(&Lock->Dbg.Spin, &oldIrql);
  209. last = (Lock->Dbg.Blocks);
  210. current = last;
  211. KeQueryTickCount((&ticks));
  212. while (NULL != current) {
  213. if (Lock->Dbg.MaxLockedTicks) {
  214. difference = ticks.QuadPart - current->TimeLocked.QuadPart;
  215. if (Lock->Dbg.MaxLockedTicks < difference) {
  216. IopDbgPrint(( IOP_ERROR_LEVEL,
  217. "IoReleaseRemoveLock: Lock %#08lx (tag %#08lx) "
  218. "locked for %I64d ticks - TOO LONG\n",
  219. Lock,
  220. current->Tag,
  221. difference));
  222. IopDbgPrint(( IOP_ERROR_LEVEL,
  223. "IoReleaseRemoveLock: Lock acquired in file "
  224. "%s on line %d\n",
  225. current->File,
  226. current->Line));
  227. ASSERT(FALSE);
  228. }
  229. }
  230. if ((!found) && (current->Tag == Tag)) {
  231. found = TRUE;
  232. if (current == Lock->Dbg.Blocks) {
  233. Lock->Dbg.Blocks = current->Link;
  234. ExFreePool (current);
  235. current = Lock->Dbg.Blocks;
  236. } else {
  237. last->Link = current->Link;
  238. ExFreePool (current);
  239. current = last->Link;
  240. }
  241. continue;
  242. }
  243. last = current;
  244. current = current->Link;
  245. }
  246. ExReleaseSpinLock(&Lock->Dbg.Spin, oldIrql);
  247. if (!found) {
  248. //
  249. // Check to see if we have any credits in our Low Memory Count.
  250. // In this fassion we can tell if we have acquired any locks without
  251. // the memory for adding tracking blocks.
  252. //
  253. if (InterlockedDecrement (& Lock->Dbg.LowMemoryCount) < 0) {
  254. //
  255. // We have just released a lock that neither had a corresponding
  256. // tracking block, nor a credit in LowMemoryCount.
  257. //
  258. InterlockedIncrement (& Lock->Dbg.LowMemoryCount);
  259. IopDbgPrint (( IOP_ERROR_LEVEL,
  260. "IoReleaseRemoveLock: Couldn't find Tag %#08lx "
  261. "in the lock tracking list\n",
  262. Tag));
  263. ASSERT(FALSE);
  264. }
  265. }
  266. break;
  267. case FREESIZE:
  268. break;
  269. default:
  270. break;
  271. }
  272. lockValue = InterlockedDecrement(&Lock->Common.IoCount);
  273. ASSERT(0 <= lockValue);
  274. if (0 == lockValue) {
  275. ASSERT (Lock->Common.Removed);
  276. //
  277. // The device needs to be removed. Signal the remove event
  278. // that it's safe to go ahead.
  279. //
  280. KeSetEvent(&Lock->Common.RemoveEvent,
  281. IO_NO_INCREMENT,
  282. FALSE);
  283. }
  284. return;
  285. }
  286. NTSYSAPI
  287. VOID
  288. NTAPI
  289. IoReleaseRemoveLockAndWaitEx (
  290. IN PIO_REMOVE_LOCK PublicLock,
  291. IN PVOID Tag,
  292. IN ULONG RemlockSize // are we checked or free
  293. )
  294. /*++
  295. Routine Description:
  296. This routine is called when the client would like to delete the remove-
  297. locked resource.
  298. This routine will block until all the remove locks have completed.
  299. This routine MUST be called after acquiring once more the lock.
  300. Arguments:
  301. RemoveLock -
  302. Return Value:
  303. none
  304. --*/
  305. {
  306. PIO_PRIVATE_REMOVE_LOCK Lock = (PIO_PRIVATE_REMOVE_LOCK) PublicLock;
  307. LONG ioCount;
  308. PAGED_CODE ();
  309. Lock->Common.Removed = TRUE;
  310. ioCount = InterlockedDecrement (&Lock->Common.IoCount);
  311. ASSERT (0 < ioCount);
  312. if (0 < InterlockedDecrement (&Lock->Common.IoCount)) {
  313. KeWaitForSingleObject (&Lock->Common.RemoveEvent,
  314. Executive,
  315. KernelMode,
  316. FALSE,
  317. NULL);
  318. }
  319. switch (RemlockSize) {
  320. case CHECKEDSIZE:
  321. ASSERT (Lock->Dbg.Blocks);
  322. if (Tag != Lock->Dbg.Blocks->Tag) {
  323. IopDbgPrint (( IOP_ERROR_LEVEL,
  324. "IoRelaseRemoveLockAndWait last tag invalid %x %x\n",
  325. Tag,
  326. Lock->Dbg.Blocks->Tag));
  327. ASSERT (Tag != Lock->Dbg.Blocks->Tag);
  328. }
  329. ExFreePool (Lock->Dbg.Blocks);
  330. break;
  331. case FREESIZE:
  332. break;
  333. default:
  334. break;
  335. }
  336. }