File: ereports.c

package info (click to toggle)
zfs-linux 2.3.4~git20250812.3b64a96-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 70,688 kB
  • sloc: ansic: 393,668; sh: 68,068; asm: 47,734; python: 8,160; makefile: 5,125; perl: 859; sed: 41
file content (174 lines) | stat: -rw-r--r-- 4,165 bytes parent folder | download | duplicates (4)
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
// SPDX-License-Identifier: CDDL-1.0
/*
 * CDDL HEADER START
 *
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2020 by Delphix. All rights reserved.
 */

#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <libzfs.h>
#include <sys/zfs_ioctl.h>
#include <sys/nvpair.h>
#include <sys/fm/protocol.h>
#include <sys/fm/fs/zfs.h>

/*
 * Command to output io and checksum ereport values, one per line.
 * Used by zpool_events_duplicates.ksh to check for duplicate events.
 *
 * example output line:
 *
 * checksum "error_pool" 0x856dd01ce52e336 0x000034 0x000400 0x000a402c00
 *  0x000004	0x000000	0x000000	0x000000	0x000001
 */

/*
 * Our ereport duplicate criteria
 *
 * When the class and all of these values match, then an ereport is
 * considered to be a duplicate.
 */
static const char *const criteria_name[] = {
	FM_EREPORT_PAYLOAD_ZFS_POOL,
	FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
	FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
	FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
	FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
	FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY,

	/* logical zio criteriai (optional) */
	FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
	FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
	FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
	FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
};

#define	CRITERIA_NAMES_COUNT	ARRAY_SIZE(criteria_name)

static void
print_ereport_line(nvlist_t *nvl)
{
	const char *class;
	int last = CRITERIA_NAMES_COUNT - 1;

	/*
	 * For the test case context, we only want to see 'io' and
	 * 'checksum' subclass.  We skip 'data' to minimize the output.
	 */
	if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||
	    strstr(class, "ereport.fs.zfs.") == NULL ||
	    strcmp(class, "ereport.fs.zfs.data") == 0) {
		return;
	}

	(void) printf("%s\t", class + strlen("ereport.fs.zfs."));

	for (int i = 0; i < CRITERIA_NAMES_COUNT; i++) {
		nvpair_t *nvp;
		uint32_t i32 = 0;
		uint64_t i64 = 0;
		const char *str = NULL;

		if (nvlist_lookup_nvpair(nvl, criteria_name[i], &nvp) != 0) {
			/* print a proxy for optional criteria */
			(void) printf("--------");
			(void) printf("%c", i == last ? '\n' : '\t');
			continue;
		}

		switch (nvpair_type(nvp)) {
		case DATA_TYPE_STRING:
			(void) nvpair_value_string(nvp, &str);
			(void) printf("\"%s\"", str ? str : "<NULL>");
			break;

		case DATA_TYPE_INT32:
			(void) nvpair_value_int32(nvp, (void *)&i32);
			(void) printf("0x%06x", i32);
			break;

		case DATA_TYPE_UINT32:
			(void) nvpair_value_uint32(nvp, &i32);
			(void) printf("0x%06x", i32);
			break;

		case DATA_TYPE_INT64:
			(void) nvpair_value_int64(nvp, (void *)&i64);
			(void) printf("0x%06llx", (u_longlong_t)i64);
			break;

		case DATA_TYPE_UINT64:
			(void) nvpair_value_uint64(nvp, &i64);
			if (strcmp(FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
			    criteria_name[i]) == 0)
				(void) printf("0x%010llx", (u_longlong_t)i64);
			else
				(void) printf("0x%06llx", (u_longlong_t)i64);
			break;
		default:
			(void) printf("<unknown>");
			break;
		}
		(void) printf("%c", i == last ? '\n' : '\t');
	}
}

static void
ereports_dump(libzfs_handle_t *zhdl, int zevent_fd)
{
	nvlist_t *nvl;
	int ret, dropped;

	while (1) {
		ret = zpool_events_next(zhdl, &nvl, &dropped, ZEVENT_NONBLOCK,
		    zevent_fd);
		if (ret || nvl == NULL)
			break;
		if (dropped > 0)
			(void) fprintf(stdout, "dropped %d events\n", dropped);
		print_ereport_line(nvl);
		(void) fflush(stdout);
		nvlist_free(nvl);
	}
}

int
main(void)
{
	libzfs_handle_t *hdl;
	int fd;

	hdl = libzfs_init();
	if (hdl == NULL) {
		(void) fprintf(stderr, "libzfs_init: %s\n", strerror(errno));
		exit(2);
	}
	fd = open(ZFS_DEV, O_RDWR);
	if (fd < 0) {
		(void) fprintf(stderr, "open: %s\n", strerror(errno));
		libzfs_fini(hdl);
		exit(2);
	}

	ereports_dump(hdl, fd);

	(void) close(fd);
	libzfs_fini(hdl);

	return (0);
}