Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/ereports.c
48529 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/zfs_ioctl.h>25#include <sys/nvpair.h>26#include <sys/fm/protocol.h>27#include <sys/fm/fs/zfs.h>2829/*30* Command to output io and checksum ereport values, one per line.31* Used by zpool_events_duplicates.ksh to check for duplicate events.32*33* example output line:34*35* checksum "error_pool" 0x856dd01ce52e336 0x000034 0x000400 0x000a402c0036* 0x000004 0x000000 0x000000 0x000000 0x00000137*/3839/*40* Our ereport duplicate criteria41*42* When the class and all of these values match, then an ereport is43* considered to be a duplicate.44*/45static const char *const criteria_name[] = {46FM_EREPORT_PAYLOAD_ZFS_POOL,47FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,48FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,49FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,50FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,51FM_EREPORT_PAYLOAD_ZFS_ZIO_TYPE,52FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY,5354/* logical zio criteriai (optional) */55FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,56FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,57FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,58FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,59};6061#define CRITERIA_NAMES_COUNT ARRAY_SIZE(criteria_name)6263static void64print_ereport_line(nvlist_t *nvl)65{66const char *class;67int last = CRITERIA_NAMES_COUNT - 1;6869/*70* For the test case context, we only want to see 'io' and71* 'checksum' subclass. We skip 'data' to minimize the output.72*/73if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||74strstr(class, "ereport.fs.zfs.") == NULL ||75strcmp(class, "ereport.fs.zfs.data") == 0) {76return;77}7879(void) printf("%s\t", class + strlen("ereport.fs.zfs."));8081for (int i = 0; i < CRITERIA_NAMES_COUNT; i++) {82nvpair_t *nvp;83uint32_t i32 = 0;84uint64_t i64 = 0;85const char *str = NULL;8687if (nvlist_lookup_nvpair(nvl, criteria_name[i], &nvp) != 0) {88/* print a proxy for optional criteria */89(void) printf("--------");90(void) printf("%c", i == last ? '\n' : '\t');91continue;92}9394switch (nvpair_type(nvp)) {95case DATA_TYPE_STRING:96(void) nvpair_value_string(nvp, &str);97(void) printf("\"%s\"", str ? str : "<NULL>");98break;99100case DATA_TYPE_INT32:101(void) nvpair_value_int32(nvp, (void *)&i32);102(void) printf("0x%06x", i32);103break;104105case DATA_TYPE_UINT32:106(void) nvpair_value_uint32(nvp, &i32);107(void) printf("0x%06x", i32);108break;109110case DATA_TYPE_INT64:111(void) nvpair_value_int64(nvp, (void *)&i64);112(void) printf("0x%06llx", (u_longlong_t)i64);113break;114115case DATA_TYPE_UINT64:116(void) nvpair_value_uint64(nvp, &i64);117if (strcmp(FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,118criteria_name[i]) == 0)119(void) printf("0x%010llx", (u_longlong_t)i64);120else121(void) printf("0x%06llx", (u_longlong_t)i64);122break;123default:124(void) printf("<unknown>");125break;126}127(void) printf("%c", i == last ? '\n' : '\t');128}129}130131static void132ereports_dump(libzfs_handle_t *zhdl, int zevent_fd)133{134nvlist_t *nvl;135int ret, dropped;136137while (1) {138ret = zpool_events_next(zhdl, &nvl, &dropped, ZEVENT_NONBLOCK,139zevent_fd);140if (ret || nvl == NULL)141break;142if (dropped > 0)143(void) fprintf(stdout, "dropped %d events\n", dropped);144print_ereport_line(nvl);145(void) fflush(stdout);146nvlist_free(nvl);147}148}149150int151main(void)152{153libzfs_handle_t *hdl;154int fd;155156hdl = libzfs_init();157if (hdl == NULL) {158(void) fprintf(stderr, "libzfs_init: %s\n", strerror(errno));159exit(2);160}161fd = open(ZFS_DEV, O_RDWR);162if (fd < 0) {163(void) fprintf(stderr, "open: %s\n", strerror(errno));164libzfs_fini(hdl);165exit(2);166}167168ereports_dump(hdl, fd);169170(void) close(fd);171libzfs_fini(hdl);172173return (0);174}175176177