Initial work on error logging.
[reactos.git] / reactos / ntoskrnl / io / errlog.c
1 /* $Id: errlog.c,v 1.9 2003/06/20 22:43:27 ekohl Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/io/errlog.c
6 * PURPOSE: Error logging
7 * PROGRAMMER: David Welch (welch@cwcom.net)
8 * UPDATE HISTORY:
9 * Created 22/05/98
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15
16 #include <internal/port.h>
17
18 //#define NDEBUG
19 #include <internal/debug.h>
20
21 /* TYPES *********************************************************************/
22
23
24 typedef struct _IO_ERROR_LOG_PACKET
25 {
26 UCHAR MajorFunctionCode;
27 UCHAR RetryCount;
28 USHORT DumpDataSize;
29 USHORT NumberOfStrings;
30 USHORT StringOffset;
31 USHORT EventCategory;
32 NTSTATUS ErrorCode;
33 ULONG UniqueErrorValue;
34 NTSTATUS FinalStatus;
35 ULONG SequenceNumber;
36 ULONG IoControlCode;
37 LARGE_INTEGER DeviceOffset;
38 ULONG DumpData[1];
39 } IO_ERROR_LOG_PACKET, *PIO_ERROR_LOG_PACKET;
40
41 typedef struct _ERROR_LOG_ENTRY
42 {
43 ULONG EntrySize;
44 } ERROR_LOG_ENTRY, *PERROR_LOG_ENTRY;
45
46
47 /* GLOBALS *******************************************************************/
48
49 static KSPIN_LOCK IopAllocationLock;
50 static ULONG IopTotalLogSize;
51
52
53 /* FUNCTIONS *****************************************************************/
54
55 NTSTATUS
56 IopInitErrorLog (VOID)
57 {
58 IopTotalLogSize = 0;
59 KeInitializeSpinLock (&IopAllocationLock);
60
61 return STATUS_SUCCESS;
62 }
63
64
65 PVOID STDCALL
66 IoAllocateErrorLogEntry (IN PVOID IoObject,
67 IN UCHAR EntrySize)
68 {
69 PERROR_LOG_ENTRY LogEntry;
70 ULONG LogEntrySize;
71 KIRQL Irql;
72
73 DPRINT1 ("IoAllocateErrorLogEntry() called\n");
74
75 if (IoObject == NULL)
76 return NULL;
77
78 KeAcquireSpinLock (&IopAllocationLock,
79 &Irql);
80
81 if (IopTotalLogSize > PAGE_SIZE)
82 {
83 KeReleaseSpinLock (&IopAllocationLock,
84 Irql);
85 return NULL;
86 }
87
88 LogEntrySize = sizeof(ERROR_LOG_ENTRY) + EntrySize;
89 LogEntry = ExAllocatePool (NonPagedPool,
90 LogEntrySize);
91 if (LogEntry == NULL)
92 {
93 KeReleaseSpinLock (&IopAllocationLock,
94 Irql);
95 return NULL;
96 }
97
98 IopTotalLogSize += EntrySize;
99
100 LogEntry->EntrySize = LogEntrySize;
101
102 KeReleaseSpinLock (&IopAllocationLock,
103 Irql);
104
105 return (PVOID)((ULONG_PTR)LogEntry + sizeof(ERROR_LOG_ENTRY));
106 }
107
108
109 VOID STDCALL
110 IoWriteErrorLogEntry (IN PVOID ElEntry)
111 {
112 PERROR_LOG_ENTRY LogEntry;
113 KIRQL Irql;
114
115 DPRINT1 ("IoWriteErrorLogEntry() called\n");
116
117 LogEntry = (PERROR_LOG_ENTRY)((ULONG_PTR)ElEntry - sizeof(ERROR_LOG_ENTRY));
118
119
120 /* FIXME: Write log entry to the error log port or keep it in a list */
121
122
123 /* Release error log entry */
124 KeAcquireSpinLock (&IopAllocationLock,
125 &Irql);
126
127 IopTotalLogSize -= LogEntry->EntrySize;
128 ExFreePool (LogEntry);
129
130 KeReleaseSpinLock (&IopAllocationLock,
131 Irql);
132 }
133
134 /* EOF */