Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/ereports.c
110107 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* This file and its contents are supplied under the terms of the5* Common Development and Distribution License ("CDDL"), version 1.0.6* You may only use this file in accordance with the terms of version7* 1.0 of the CDDL.8*9* A full copy of the text of the CDDL should have accompanied this10* source. A copy of the CDDL is also available via the Internet at11* http://www.illumos.org/license/CDDL.12*13* CDDL HEADER END14*/1516/*17* Copyright (c) 2020 by Delphix. All rights reserved.18*/1920#include <assert.h>21#include <fcntl.h>22#include <stdio.h>23#include <libzfs.h>24#include <sys/nvpair.h>25#include <sys/fm/protocol.h>26#include <sys/fm/fs/zfs.h>2728#define ZEVENT_NONBLOCK 0x12930/*31* Command to output io and checksum ereport values, one per line.32* Used by zpool_events_duplicates.ksh to check for duplicate events.33*34* example output line:35*36* checksum "error_pool" 0x856dd01ce52e336 0x000034 0x000400 0x000a402c0037* 0x000004 0x000000 0x000000 0x000000 0x00000138*/3940/*41* Our ereport duplicate criteria42*43* When the class and all of these values match, then an ereport is44* considered to be a duplicate.45*/46static const char *const criteria_name[] = {47FM_EREPORT_PAYLOAD_ZFS_POOL,48FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,49FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,50FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,51FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,52FM_EREPORT_PAYLOAD_ZFS_ZIO_TYPE,53FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY,5455/* logical zio criteriai (optional) */56FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,57FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,58FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,59FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,60};6162#define CRITERIA_NAMES_COUNT ARRAY_SIZE(criteria_name)6364static void65print_ereport_line(nvlist_t *nvl)66{67const char *class;68int last = CRITERIA_NAMES_COUNT - 1;6970/*71* For the test case context, we only want to see 'io' and72* 'checksum' subclass. We skip 'data' to minimize the output.73*/74if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||75strstr(class, "ereport.fs.zfs.") == NULL ||76strcmp(class, "ereport.fs.zfs.data") == 0) {77return;78}7980(void) printf("%s\t", class + strlen("ereport.fs.zfs."));8182for (int i = 0; i < CRITERIA_NAMES_COUNT; i++) {83nvpair_t *nvp;84uint32_t i32 = 0;85uint64_t i64 = 0;86const char *str = NULL;8788if (nvlist_lookup_nvpair(nvl, criteria_name[i], &nvp) != 0) {89/* print a proxy for optional criteria */90(void) printf("--------");91(void) printf("%c", i == last ? '\n' : '\t');92continue;93}9495switch (nvpair_type(nvp)) {96case DATA_TYPE_STRING:97(void) nvpair_value_string(nvp, &str);98(void) printf("\"%s\"", str ? str : "<NULL>");99break;100101case DATA_TYPE_INT32:102(void) nvpair_value_int32(nvp, (void *)&i32);103(void) printf("0x%06x", i32);104break;105106case DATA_TYPE_UINT32:107(void) nvpair_value_uint32(nvp, &i32);108(void) printf("0x%06x", i32);109break;110111case DATA_TYPE_INT64:112(void) nvpair_value_int64(nvp, (void *)&i64);113(void) printf("0x%06llx", (u_longlong_t)i64);114break;115116case DATA_TYPE_UINT64:117(void) nvpair_value_uint64(nvp, &i64);118if (strcmp(FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,119criteria_name[i]) == 0)120(void) printf("0x%010llx", (u_longlong_t)i64);121else122(void) printf("0x%06llx", (u_longlong_t)i64);123break;124default:125(void) printf("<unknown>");126break;127}128(void) printf("%c", i == last ? '\n' : '\t');129}130}131132static void133ereports_dump(libzfs_handle_t *zhdl, int zevent_fd)134{135nvlist_t *nvl;136int ret, dropped;137138while (1) {139ret = zpool_events_next(zhdl, &nvl, &dropped, ZEVENT_NONBLOCK,140zevent_fd);141if (ret || nvl == NULL)142break;143if (dropped > 0)144(void) fprintf(stdout, "dropped %d events\n", dropped);145print_ereport_line(nvl);146(void) fflush(stdout);147nvlist_free(nvl);148}149}150151int152main(void)153{154libzfs_handle_t *hdl;155int fd;156157hdl = libzfs_init();158if (hdl == NULL) {159(void) fprintf(stderr, "libzfs_init: %s\n", strerror(errno));160exit(2);161}162fd = open(ZFS_DEV, O_RDWR);163if (fd < 0) {164(void) fprintf(stderr, "open: %s\n", strerror(errno));165libzfs_fini(hdl);166exit(2);167}168169ereports_dump(hdl, fd);170171(void) close(fd);172libzfs_fini(hdl);173174return (0);175}176177178