]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_fm.c
module/*.ko: prune .data, global .rodata
[mirror_zfs.git] / module / zfs / zfs_fm.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Copyright (c) 2012,2021 by Delphix. All rights reserved.
28 */
29
30 #include <sys/spa.h>
31 #include <sys/spa_impl.h>
32 #include <sys/vdev.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/zio.h>
35 #include <sys/zio_checksum.h>
36
37 #include <sys/fm/fs/zfs.h>
38 #include <sys/fm/protocol.h>
39 #include <sys/fm/util.h>
40 #include <sys/sysevent.h>
41
42 /*
43 * This general routine is responsible for generating all the different ZFS
44 * ereports. The payload is dependent on the class, and which arguments are
45 * supplied to the function:
46 *
47 * EREPORT POOL VDEV IO
48 * block X X X
49 * data X X
50 * device X X
51 * pool X
52 *
53 * If we are in a loading state, all errors are chained together by the same
54 * SPA-wide ENA (Error Numeric Association).
55 *
56 * For isolated I/O requests, we get the ENA from the zio_t. The propagation
57 * gets very complicated due to RAID-Z, gang blocks, and vdev caching. We want
58 * to chain together all ereports associated with a logical piece of data. For
59 * read I/Os, there are basically three 'types' of I/O, which form a roughly
60 * layered diagram:
61 *
62 * +---------------+
63 * | Aggregate I/O | No associated logical data or device
64 * +---------------+
65 * |
66 * V
67 * +---------------+ Reads associated with a piece of logical data.
68 * | Read I/O | This includes reads on behalf of RAID-Z,
69 * +---------------+ mirrors, gang blocks, retries, etc.
70 * |
71 * V
72 * +---------------+ Reads associated with a particular device, but
73 * | Physical I/O | no logical data. Issued as part of vdev caching
74 * +---------------+ and I/O aggregation.
75 *
76 * Note that 'physical I/O' here is not the same terminology as used in the rest
77 * of ZIO. Typically, 'physical I/O' simply means that there is no attached
78 * blockpointer. But I/O with no associated block pointer can still be related
79 * to a logical piece of data (i.e. RAID-Z requests).
80 *
81 * Purely physical I/O always have unique ENAs. They are not related to a
82 * particular piece of logical data, and therefore cannot be chained together.
83 * We still generate an ereport, but the DE doesn't correlate it with any
84 * logical piece of data. When such an I/O fails, the delegated I/O requests
85 * will issue a retry, which will trigger the 'real' ereport with the correct
86 * ENA.
87 *
88 * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
89 * When a new logical I/O is issued, we set this to point to itself. Child I/Os
90 * then inherit this pointer, so that when it is first set subsequent failures
91 * will use the same ENA. For vdev cache fill and queue aggregation I/O,
92 * this pointer is set to NULL, and no ereport will be generated (since it
93 * doesn't actually correspond to any particular device or piece of data,
94 * and the caller will always retry without caching or queueing anyway).
95 *
96 * For checksum errors, we want to include more information about the actual
97 * error which occurs. Accordingly, we build an ereport when the error is
98 * noticed, but instead of sending it in immediately, we hang it off of the
99 * io_cksum_report field of the logical IO. When the logical IO completes
100 * (successfully or not), zfs_ereport_finish_checksum() is called with the
101 * good and bad versions of the buffer (if available), and we annotate the
102 * ereport with information about the differences.
103 */
104
105 #ifdef _KERNEL
106 /*
107 * Duplicate ereport Detection
108 *
109 * Some ereports are retained momentarily for detecting duplicates. These
110 * are kept in a recent_events_node_t in both a time-ordered list and an AVL
111 * tree of recent unique ereports.
112 *
113 * The lifespan of these recent ereports is bounded (15 mins) and a cleaner
114 * task is used to purge stale entries.
115 */
116 static list_t recent_events_list;
117 static avl_tree_t recent_events_tree;
118 static kmutex_t recent_events_lock;
119 static taskqid_t recent_events_cleaner_tqid;
120
121 /*
122 * Each node is about 128 bytes so 2,000 would consume 1/4 MiB.
123 *
124 * This setting can be changed dynamically and setting it to zero
125 * disables duplicate detection.
126 */
127 static unsigned int zfs_zevent_retain_max = 2000;
128
129 /*
130 * The lifespan for a recent ereport entry. The default of 15 minutes is
131 * intended to outlive the zfs diagnosis engine's threshold of 10 errors
132 * over a period of 10 minutes.
133 */
134 static unsigned int zfs_zevent_retain_expire_secs = 900;
135
136 typedef enum zfs_subclass {
137 ZSC_IO,
138 ZSC_DATA,
139 ZSC_CHECKSUM
140 } zfs_subclass_t;
141
142 typedef struct {
143 /* common criteria */
144 uint64_t re_pool_guid;
145 uint64_t re_vdev_guid;
146 int re_io_error;
147 uint64_t re_io_size;
148 uint64_t re_io_offset;
149 zfs_subclass_t re_subclass;
150 zio_priority_t re_io_priority;
151
152 /* logical zio criteria (optional) */
153 zbookmark_phys_t re_io_bookmark;
154
155 /* internal state */
156 avl_node_t re_tree_link;
157 list_node_t re_list_link;
158 uint64_t re_timestamp;
159 } recent_events_node_t;
160
161 static int
162 recent_events_compare(const void *a, const void *b)
163 {
164 const recent_events_node_t *node1 = a;
165 const recent_events_node_t *node2 = b;
166 int cmp;
167
168 /*
169 * The comparison order here is somewhat arbitrary.
170 * What's important is that if every criteria matches, then it
171 * is a duplicate (i.e. compare returns 0)
172 */
173 if ((cmp = TREE_CMP(node1->re_subclass, node2->re_subclass)) != 0)
174 return (cmp);
175 if ((cmp = TREE_CMP(node1->re_pool_guid, node2->re_pool_guid)) != 0)
176 return (cmp);
177 if ((cmp = TREE_CMP(node1->re_vdev_guid, node2->re_vdev_guid)) != 0)
178 return (cmp);
179 if ((cmp = TREE_CMP(node1->re_io_error, node2->re_io_error)) != 0)
180 return (cmp);
181 if ((cmp = TREE_CMP(node1->re_io_priority, node2->re_io_priority)) != 0)
182 return (cmp);
183 if ((cmp = TREE_CMP(node1->re_io_size, node2->re_io_size)) != 0)
184 return (cmp);
185 if ((cmp = TREE_CMP(node1->re_io_offset, node2->re_io_offset)) != 0)
186 return (cmp);
187
188 const zbookmark_phys_t *zb1 = &node1->re_io_bookmark;
189 const zbookmark_phys_t *zb2 = &node2->re_io_bookmark;
190
191 if ((cmp = TREE_CMP(zb1->zb_objset, zb2->zb_objset)) != 0)
192 return (cmp);
193 if ((cmp = TREE_CMP(zb1->zb_object, zb2->zb_object)) != 0)
194 return (cmp);
195 if ((cmp = TREE_CMP(zb1->zb_level, zb2->zb_level)) != 0)
196 return (cmp);
197 if ((cmp = TREE_CMP(zb1->zb_blkid, zb2->zb_blkid)) != 0)
198 return (cmp);
199
200 return (0);
201 }
202
203 static void zfs_ereport_schedule_cleaner(void);
204
205 /*
206 * background task to clean stale recent event nodes.
207 */
208 static void
209 zfs_ereport_cleaner(void *arg)
210 {
211 recent_events_node_t *entry;
212 uint64_t now = gethrtime();
213
214 /*
215 * purge expired entries
216 */
217 mutex_enter(&recent_events_lock);
218 while ((entry = list_tail(&recent_events_list)) != NULL) {
219 uint64_t age = NSEC2SEC(now - entry->re_timestamp);
220 if (age <= zfs_zevent_retain_expire_secs)
221 break;
222
223 /* remove expired node */
224 avl_remove(&recent_events_tree, entry);
225 list_remove(&recent_events_list, entry);
226 kmem_free(entry, sizeof (*entry));
227 }
228
229 /* Restart the cleaner if more entries remain */
230 recent_events_cleaner_tqid = 0;
231 if (!list_is_empty(&recent_events_list))
232 zfs_ereport_schedule_cleaner();
233
234 mutex_exit(&recent_events_lock);
235 }
236
237 static void
238 zfs_ereport_schedule_cleaner(void)
239 {
240 ASSERT(MUTEX_HELD(&recent_events_lock));
241
242 uint64_t timeout = SEC2NSEC(zfs_zevent_retain_expire_secs + 1);
243
244 recent_events_cleaner_tqid = taskq_dispatch_delay(
245 system_delay_taskq, zfs_ereport_cleaner, NULL, TQ_SLEEP,
246 ddi_get_lbolt() + NSEC_TO_TICK(timeout));
247 }
248
249 /*
250 * Clear entries for a given vdev or all vdevs in a pool when vdev == NULL
251 */
252 void
253 zfs_ereport_clear(spa_t *spa, vdev_t *vd)
254 {
255 uint64_t vdev_guid, pool_guid;
256 int cnt = 0;
257
258 ASSERT(vd != NULL || spa != NULL);
259 if (vd == NULL) {
260 vdev_guid = 0;
261 pool_guid = spa_guid(spa);
262 } else {
263 vdev_guid = vd->vdev_guid;
264 pool_guid = 0;
265 }
266
267 mutex_enter(&recent_events_lock);
268
269 recent_events_node_t *next = list_head(&recent_events_list);
270 while (next != NULL) {
271 recent_events_node_t *entry = next;
272
273 next = list_next(&recent_events_list, next);
274
275 if (entry->re_vdev_guid == vdev_guid ||
276 entry->re_pool_guid == pool_guid) {
277 avl_remove(&recent_events_tree, entry);
278 list_remove(&recent_events_list, entry);
279 kmem_free(entry, sizeof (*entry));
280 cnt++;
281 }
282 }
283
284 mutex_exit(&recent_events_lock);
285 }
286
287 /*
288 * Check if an ereport would be a duplicate of one recently posted.
289 *
290 * An ereport is considered a duplicate if the set of criteria in
291 * recent_events_node_t all match.
292 *
293 * Only FM_EREPORT_ZFS_IO, FM_EREPORT_ZFS_DATA, and FM_EREPORT_ZFS_CHECKSUM
294 * are candidates for duplicate checking.
295 */
296 static boolean_t
297 zfs_ereport_is_duplicate(const char *subclass, spa_t *spa, vdev_t *vd,
298 const zbookmark_phys_t *zb, zio_t *zio, uint64_t offset, uint64_t size)
299 {
300 recent_events_node_t search = {0}, *entry;
301
302 if (vd == NULL || zio == NULL)
303 return (B_FALSE);
304
305 if (zfs_zevent_retain_max == 0)
306 return (B_FALSE);
307
308 if (strcmp(subclass, FM_EREPORT_ZFS_IO) == 0)
309 search.re_subclass = ZSC_IO;
310 else if (strcmp(subclass, FM_EREPORT_ZFS_DATA) == 0)
311 search.re_subclass = ZSC_DATA;
312 else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0)
313 search.re_subclass = ZSC_CHECKSUM;
314 else
315 return (B_FALSE);
316
317 search.re_pool_guid = spa_guid(spa);
318 search.re_vdev_guid = vd->vdev_guid;
319 search.re_io_error = zio->io_error;
320 search.re_io_priority = zio->io_priority;
321 /* if size is supplied use it over what's in zio */
322 if (size) {
323 search.re_io_size = size;
324 search.re_io_offset = offset;
325 } else {
326 search.re_io_size = zio->io_size;
327 search.re_io_offset = zio->io_offset;
328 }
329
330 /* grab optional logical zio criteria */
331 if (zb != NULL) {
332 search.re_io_bookmark.zb_objset = zb->zb_objset;
333 search.re_io_bookmark.zb_object = zb->zb_object;
334 search.re_io_bookmark.zb_level = zb->zb_level;
335 search.re_io_bookmark.zb_blkid = zb->zb_blkid;
336 }
337
338 uint64_t now = gethrtime();
339
340 mutex_enter(&recent_events_lock);
341
342 /* check if we have seen this one recently */
343 entry = avl_find(&recent_events_tree, &search, NULL);
344 if (entry != NULL) {
345 uint64_t age = NSEC2SEC(now - entry->re_timestamp);
346
347 /*
348 * There is still an active cleaner (since we're here).
349 * Reset the last seen time for this duplicate entry
350 * so that its lifespand gets extended.
351 */
352 list_remove(&recent_events_list, entry);
353 list_insert_head(&recent_events_list, entry);
354 entry->re_timestamp = now;
355
356 zfs_zevent_track_duplicate();
357 mutex_exit(&recent_events_lock);
358
359 return (age <= zfs_zevent_retain_expire_secs);
360 }
361
362 if (avl_numnodes(&recent_events_tree) >= zfs_zevent_retain_max) {
363 /* recycle oldest node */
364 entry = list_tail(&recent_events_list);
365 ASSERT(entry != NULL);
366 list_remove(&recent_events_list, entry);
367 avl_remove(&recent_events_tree, entry);
368 } else {
369 entry = kmem_alloc(sizeof (recent_events_node_t), KM_SLEEP);
370 }
371
372 /* record this as a recent ereport */
373 *entry = search;
374 avl_add(&recent_events_tree, entry);
375 list_insert_head(&recent_events_list, entry);
376 entry->re_timestamp = now;
377
378 /* Start a cleaner if not already scheduled */
379 if (recent_events_cleaner_tqid == 0)
380 zfs_ereport_schedule_cleaner();
381
382 mutex_exit(&recent_events_lock);
383 return (B_FALSE);
384 }
385
386 void
387 zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector)
388 {
389 if (nvl)
390 fm_nvlist_destroy(nvl, FM_NVA_FREE);
391
392 if (detector)
393 fm_nvlist_destroy(detector, FM_NVA_FREE);
394 }
395
396 /*
397 * We want to rate limit ZIO delay, deadman, and checksum events so as to not
398 * flood zevent consumers when a disk is acting up.
399 *
400 * Returns 1 if we're ratelimiting, 0 if not.
401 */
402 static int
403 zfs_is_ratelimiting_event(const char *subclass, vdev_t *vd)
404 {
405 int rc = 0;
406 /*
407 * zfs_ratelimit() returns 1 if we're *not* ratelimiting and 0 if we
408 * are. Invert it to get our return value.
409 */
410 if (strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
411 rc = !zfs_ratelimit(&vd->vdev_delay_rl);
412 } else if (strcmp(subclass, FM_EREPORT_ZFS_DEADMAN) == 0) {
413 rc = !zfs_ratelimit(&vd->vdev_deadman_rl);
414 } else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
415 rc = !zfs_ratelimit(&vd->vdev_checksum_rl);
416 }
417
418 if (rc) {
419 /* We're rate limiting */
420 fm_erpt_dropped_increment();
421 }
422
423 return (rc);
424 }
425
426 /*
427 * Return B_TRUE if the event actually posted, B_FALSE if not.
428 */
429 static boolean_t
430 zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
431 const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
432 zio_t *zio, uint64_t stateoroffset, uint64_t size)
433 {
434 nvlist_t *ereport, *detector;
435
436 uint64_t ena;
437 char class[64];
438
439 if ((ereport = fm_nvlist_create(NULL)) == NULL)
440 return (B_FALSE);
441
442 if ((detector = fm_nvlist_create(NULL)) == NULL) {
443 fm_nvlist_destroy(ereport, FM_NVA_FREE);
444 return (B_FALSE);
445 }
446
447 /*
448 * Serialize ereport generation
449 */
450 mutex_enter(&spa->spa_errlist_lock);
451
452 /*
453 * Determine the ENA to use for this event. If we are in a loading
454 * state, use a SPA-wide ENA. Otherwise, if we are in an I/O state, use
455 * a root zio-wide ENA. Otherwise, simply use a unique ENA.
456 */
457 if (spa_load_state(spa) != SPA_LOAD_NONE) {
458 if (spa->spa_ena == 0)
459 spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
460 ena = spa->spa_ena;
461 } else if (zio != NULL && zio->io_logical != NULL) {
462 if (zio->io_logical->io_ena == 0)
463 zio->io_logical->io_ena =
464 fm_ena_generate(0, FM_ENA_FMT1);
465 ena = zio->io_logical->io_ena;
466 } else {
467 ena = fm_ena_generate(0, FM_ENA_FMT1);
468 }
469
470 /*
471 * Construct the full class, detector, and other standard FMA fields.
472 */
473 (void) snprintf(class, sizeof (class), "%s.%s",
474 ZFS_ERROR_CLASS, subclass);
475
476 fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
477 vd != NULL ? vd->vdev_guid : 0);
478
479 fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
480
481 /*
482 * Construct the per-ereport payload, depending on which parameters are
483 * passed in.
484 */
485
486 /*
487 * Generic payload members common to all ereports.
488 */
489 fm_payload_set(ereport,
490 FM_EREPORT_PAYLOAD_ZFS_POOL, DATA_TYPE_STRING, spa_name(spa),
491 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, DATA_TYPE_UINT64, spa_guid(spa),
492 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, DATA_TYPE_UINT64,
493 (uint64_t)spa_state(spa),
494 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
495 (int32_t)spa_load_state(spa), NULL);
496
497 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
498 DATA_TYPE_STRING,
499 spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
500 FM_EREPORT_FAILMODE_WAIT :
501 spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
502 FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
503 NULL);
504
505 if (vd != NULL) {
506 vdev_t *pvd = vd->vdev_parent;
507 vdev_queue_t *vq = &vd->vdev_queue;
508 vdev_stat_t *vs = &vd->vdev_stat;
509 vdev_t *spare_vd;
510 uint64_t *spare_guids;
511 char **spare_paths;
512 int i, spare_count;
513
514 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
515 DATA_TYPE_UINT64, vd->vdev_guid,
516 FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
517 DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
518 if (vd->vdev_path != NULL)
519 fm_payload_set(ereport,
520 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
521 DATA_TYPE_STRING, vd->vdev_path, NULL);
522 if (vd->vdev_devid != NULL)
523 fm_payload_set(ereport,
524 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
525 DATA_TYPE_STRING, vd->vdev_devid, NULL);
526 if (vd->vdev_fru != NULL)
527 fm_payload_set(ereport,
528 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
529 DATA_TYPE_STRING, vd->vdev_fru, NULL);
530 if (vd->vdev_enc_sysfs_path != NULL)
531 fm_payload_set(ereport,
532 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
533 DATA_TYPE_STRING, vd->vdev_enc_sysfs_path, NULL);
534 if (vd->vdev_ashift)
535 fm_payload_set(ereport,
536 FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
537 DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
538
539 if (vq != NULL) {
540 fm_payload_set(ereport,
541 FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
542 DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
543 fm_payload_set(ereport,
544 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
545 DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
546 }
547
548 if (vs != NULL) {
549 fm_payload_set(ereport,
550 FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS,
551 DATA_TYPE_UINT64, vs->vs_read_errors,
552 FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS,
553 DATA_TYPE_UINT64, vs->vs_write_errors,
554 FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS,
555 DATA_TYPE_UINT64, vs->vs_checksum_errors,
556 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELAYS,
557 DATA_TYPE_UINT64, vs->vs_slow_ios,
558 NULL);
559 }
560
561 if (pvd != NULL) {
562 fm_payload_set(ereport,
563 FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
564 DATA_TYPE_UINT64, pvd->vdev_guid,
565 FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
566 DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
567 NULL);
568 if (pvd->vdev_path)
569 fm_payload_set(ereport,
570 FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
571 DATA_TYPE_STRING, pvd->vdev_path, NULL);
572 if (pvd->vdev_devid)
573 fm_payload_set(ereport,
574 FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
575 DATA_TYPE_STRING, pvd->vdev_devid, NULL);
576 }
577
578 spare_count = spa->spa_spares.sav_count;
579 spare_paths = kmem_zalloc(sizeof (char *) * spare_count,
580 KM_SLEEP);
581 spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count,
582 KM_SLEEP);
583
584 for (i = 0; i < spare_count; i++) {
585 spare_vd = spa->spa_spares.sav_vdevs[i];
586 if (spare_vd) {
587 spare_paths[i] = spare_vd->vdev_path;
588 spare_guids[i] = spare_vd->vdev_guid;
589 }
590 }
591
592 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS,
593 DATA_TYPE_STRING_ARRAY, spare_count, spare_paths,
594 FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS,
595 DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL);
596
597 kmem_free(spare_guids, sizeof (uint64_t) * spare_count);
598 kmem_free(spare_paths, sizeof (char *) * spare_count);
599 }
600
601 if (zio != NULL) {
602 /*
603 * Payload common to all I/Os.
604 */
605 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
606 DATA_TYPE_INT32, zio->io_error, NULL);
607 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
608 DATA_TYPE_INT32, zio->io_flags, NULL);
609 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
610 DATA_TYPE_UINT32, zio->io_stage, NULL);
611 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
612 DATA_TYPE_UINT32, zio->io_pipeline, NULL);
613 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
614 DATA_TYPE_UINT64, zio->io_delay, NULL);
615 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
616 DATA_TYPE_UINT64, zio->io_timestamp, NULL);
617 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
618 DATA_TYPE_UINT64, zio->io_delta, NULL);
619 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY,
620 DATA_TYPE_UINT32, zio->io_priority, NULL);
621
622 /*
623 * If the 'size' parameter is non-zero, it indicates this is a
624 * RAID-Z or other I/O where the physical offset and length are
625 * provided for us, instead of within the zio_t.
626 */
627 if (vd != NULL) {
628 if (size)
629 fm_payload_set(ereport,
630 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
631 DATA_TYPE_UINT64, stateoroffset,
632 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
633 DATA_TYPE_UINT64, size, NULL);
634 else
635 fm_payload_set(ereport,
636 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
637 DATA_TYPE_UINT64, zio->io_offset,
638 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
639 DATA_TYPE_UINT64, zio->io_size, NULL);
640 }
641 } else if (vd != NULL) {
642 /*
643 * If we have a vdev but no zio, this is a device fault, and the
644 * 'stateoroffset' parameter indicates the previous state of the
645 * vdev.
646 */
647 fm_payload_set(ereport,
648 FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
649 DATA_TYPE_UINT64, stateoroffset, NULL);
650 }
651
652 /*
653 * Payload for I/Os with corresponding logical information.
654 */
655 if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) {
656 fm_payload_set(ereport,
657 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
658 DATA_TYPE_UINT64, zb->zb_objset,
659 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
660 DATA_TYPE_UINT64, zb->zb_object,
661 FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
662 DATA_TYPE_INT64, zb->zb_level,
663 FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
664 DATA_TYPE_UINT64, zb->zb_blkid, NULL);
665 }
666
667 mutex_exit(&spa->spa_errlist_lock);
668
669 *ereport_out = ereport;
670 *detector_out = detector;
671 return (B_TRUE);
672 }
673
674 /* if it's <= 128 bytes, save the corruption directly */
675 #define ZFM_MAX_INLINE (128 / sizeof (uint64_t))
676
677 #define MAX_RANGES 16
678
679 typedef struct zfs_ecksum_info {
680 /* histograms of set and cleared bits by bit number in a 64-bit word */
681 uint32_t zei_histogram_set[sizeof (uint64_t) * NBBY];
682 uint32_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
683
684 /* inline arrays of bits set and cleared. */
685 uint64_t zei_bits_set[ZFM_MAX_INLINE];
686 uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
687
688 /*
689 * for each range, the number of bits set and cleared. The Hamming
690 * distance between the good and bad buffers is the sum of them all.
691 */
692 uint32_t zei_range_sets[MAX_RANGES];
693 uint32_t zei_range_clears[MAX_RANGES];
694
695 struct zei_ranges {
696 uint32_t zr_start;
697 uint32_t zr_end;
698 } zei_ranges[MAX_RANGES];
699
700 size_t zei_range_count;
701 uint32_t zei_mingap;
702 uint32_t zei_allowed_mingap;
703
704 } zfs_ecksum_info_t;
705
706 static void
707 update_histogram(uint64_t value_arg, uint32_t *hist, uint32_t *count)
708 {
709 size_t i;
710 size_t bits = 0;
711 uint64_t value = BE_64(value_arg);
712
713 /* We store the bits in big-endian (largest-first) order */
714 for (i = 0; i < 64; i++) {
715 if (value & (1ull << i)) {
716 hist[63 - i]++;
717 ++bits;
718 }
719 }
720 /* update the count of bits changed */
721 *count += bits;
722 }
723
724 /*
725 * We've now filled up the range array, and need to increase "mingap" and
726 * shrink the range list accordingly. zei_mingap is always the smallest
727 * distance between array entries, so we set the new_allowed_gap to be
728 * one greater than that. We then go through the list, joining together
729 * any ranges which are closer than the new_allowed_gap.
730 *
731 * By construction, there will be at least one. We also update zei_mingap
732 * to the new smallest gap, to prepare for our next invocation.
733 */
734 static void
735 zei_shrink_ranges(zfs_ecksum_info_t *eip)
736 {
737 uint32_t mingap = UINT32_MAX;
738 uint32_t new_allowed_gap = eip->zei_mingap + 1;
739
740 size_t idx, output;
741 size_t max = eip->zei_range_count;
742
743 struct zei_ranges *r = eip->zei_ranges;
744
745 ASSERT3U(eip->zei_range_count, >, 0);
746 ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
747
748 output = idx = 0;
749 while (idx < max - 1) {
750 uint32_t start = r[idx].zr_start;
751 uint32_t end = r[idx].zr_end;
752
753 while (idx < max - 1) {
754 idx++;
755
756 uint32_t nstart = r[idx].zr_start;
757 uint32_t nend = r[idx].zr_end;
758
759 uint32_t gap = nstart - end;
760 if (gap < new_allowed_gap) {
761 end = nend;
762 continue;
763 }
764 if (gap < mingap)
765 mingap = gap;
766 break;
767 }
768 r[output].zr_start = start;
769 r[output].zr_end = end;
770 output++;
771 }
772 ASSERT3U(output, <, eip->zei_range_count);
773 eip->zei_range_count = output;
774 eip->zei_mingap = mingap;
775 eip->zei_allowed_mingap = new_allowed_gap;
776 }
777
778 static void
779 zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
780 {
781 struct zei_ranges *r = eip->zei_ranges;
782 size_t count = eip->zei_range_count;
783
784 if (count >= MAX_RANGES) {
785 zei_shrink_ranges(eip);
786 count = eip->zei_range_count;
787 }
788 if (count == 0) {
789 eip->zei_mingap = UINT32_MAX;
790 eip->zei_allowed_mingap = 1;
791 } else {
792 int gap = start - r[count - 1].zr_end;
793
794 if (gap < eip->zei_allowed_mingap) {
795 r[count - 1].zr_end = end;
796 return;
797 }
798 if (gap < eip->zei_mingap)
799 eip->zei_mingap = gap;
800 }
801 r[count].zr_start = start;
802 r[count].zr_end = end;
803 eip->zei_range_count++;
804 }
805
806 static size_t
807 zei_range_total_size(zfs_ecksum_info_t *eip)
808 {
809 struct zei_ranges *r = eip->zei_ranges;
810 size_t count = eip->zei_range_count;
811 size_t result = 0;
812 size_t idx;
813
814 for (idx = 0; idx < count; idx++)
815 result += (r[idx].zr_end - r[idx].zr_start);
816
817 return (result);
818 }
819
820 static zfs_ecksum_info_t *
821 annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
822 const abd_t *goodabd, const abd_t *badabd, size_t size,
823 boolean_t drop_if_identical)
824 {
825 const uint64_t *good;
826 const uint64_t *bad;
827
828 size_t nui64s = size / sizeof (uint64_t);
829
830 size_t inline_size;
831 int no_inline = 0;
832 size_t idx;
833 size_t range;
834
835 size_t offset = 0;
836 ssize_t start = -1;
837
838 zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
839
840 /* don't do any annotation for injected checksum errors */
841 if (info != NULL && info->zbc_injected)
842 return (eip);
843
844 if (info != NULL && info->zbc_has_cksum) {
845 fm_payload_set(ereport,
846 FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
847 DATA_TYPE_UINT64_ARRAY,
848 sizeof (info->zbc_expected) / sizeof (uint64_t),
849 (uint64_t *)&info->zbc_expected,
850 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
851 DATA_TYPE_UINT64_ARRAY,
852 sizeof (info->zbc_actual) / sizeof (uint64_t),
853 (uint64_t *)&info->zbc_actual,
854 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
855 DATA_TYPE_STRING,
856 info->zbc_checksum_name,
857 NULL);
858
859 if (info->zbc_byteswapped) {
860 fm_payload_set(ereport,
861 FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
862 DATA_TYPE_BOOLEAN, 1,
863 NULL);
864 }
865 }
866
867 if (badabd == NULL || goodabd == NULL)
868 return (eip);
869
870 ASSERT3U(nui64s, <=, UINT32_MAX);
871 ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
872 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
873 ASSERT3U(size, <=, UINT32_MAX);
874
875 good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
876 bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
877
878 /* build up the range list by comparing the two buffers. */
879 for (idx = 0; idx < nui64s; idx++) {
880 if (good[idx] == bad[idx]) {
881 if (start == -1)
882 continue;
883
884 zei_add_range(eip, start, idx);
885 start = -1;
886 } else {
887 if (start != -1)
888 continue;
889
890 start = idx;
891 }
892 }
893 if (start != -1)
894 zei_add_range(eip, start, idx);
895
896 /* See if it will fit in our inline buffers */
897 inline_size = zei_range_total_size(eip);
898 if (inline_size > ZFM_MAX_INLINE)
899 no_inline = 1;
900
901 /*
902 * If there is no change and we want to drop if the buffers are
903 * identical, do so.
904 */
905 if (inline_size == 0 && drop_if_identical) {
906 kmem_free(eip, sizeof (*eip));
907 abd_return_buf((abd_t *)goodabd, (void *)good, size);
908 abd_return_buf((abd_t *)badabd, (void *)bad, size);
909 return (NULL);
910 }
911
912 /*
913 * Now walk through the ranges, filling in the details of the
914 * differences. Also convert our uint64_t-array offsets to byte
915 * offsets.
916 */
917 for (range = 0; range < eip->zei_range_count; range++) {
918 size_t start = eip->zei_ranges[range].zr_start;
919 size_t end = eip->zei_ranges[range].zr_end;
920
921 for (idx = start; idx < end; idx++) {
922 uint64_t set, cleared;
923
924 // bits set in bad, but not in good
925 set = ((~good[idx]) & bad[idx]);
926 // bits set in good, but not in bad
927 cleared = (good[idx] & (~bad[idx]));
928
929 if (!no_inline) {
930 ASSERT3U(offset, <, inline_size);
931 eip->zei_bits_set[offset] = set;
932 eip->zei_bits_cleared[offset] = cleared;
933 offset++;
934 }
935
936 update_histogram(set, eip->zei_histogram_set,
937 &eip->zei_range_sets[range]);
938 update_histogram(cleared, eip->zei_histogram_cleared,
939 &eip->zei_range_clears[range]);
940 }
941
942 /* convert to byte offsets */
943 eip->zei_ranges[range].zr_start *= sizeof (uint64_t);
944 eip->zei_ranges[range].zr_end *= sizeof (uint64_t);
945 }
946
947 abd_return_buf((abd_t *)goodabd, (void *)good, size);
948 abd_return_buf((abd_t *)badabd, (void *)bad, size);
949
950 eip->zei_allowed_mingap *= sizeof (uint64_t);
951 inline_size *= sizeof (uint64_t);
952
953 /* fill in ereport */
954 fm_payload_set(ereport,
955 FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
956 DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
957 (uint32_t *)eip->zei_ranges,
958 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
959 DATA_TYPE_UINT32, eip->zei_allowed_mingap,
960 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
961 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
962 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
963 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
964 NULL);
965
966 if (!no_inline) {
967 fm_payload_set(ereport,
968 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
969 DATA_TYPE_UINT8_ARRAY,
970 inline_size, (uint8_t *)eip->zei_bits_set,
971 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
972 DATA_TYPE_UINT8_ARRAY,
973 inline_size, (uint8_t *)eip->zei_bits_cleared,
974 NULL);
975 } else {
976 fm_payload_set(ereport,
977 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
978 DATA_TYPE_UINT32_ARRAY,
979 NBBY * sizeof (uint64_t), eip->zei_histogram_set,
980 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
981 DATA_TYPE_UINT32_ARRAY,
982 NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
983 NULL);
984 }
985 return (eip);
986 }
987 #else
988 void
989 zfs_ereport_clear(spa_t *spa, vdev_t *vd)
990 {
991 (void) spa, (void) vd;
992 }
993 #endif
994
995 /*
996 * Make sure our event is still valid for the given zio/vdev/pool. For example,
997 * we don't want to keep logging events for a faulted or missing vdev.
998 */
999 boolean_t
1000 zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio)
1001 {
1002 #ifdef _KERNEL
1003 /*
1004 * If we are doing a spa_tryimport() or in recovery mode,
1005 * ignore errors.
1006 */
1007 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
1008 spa_load_state(spa) == SPA_LOAD_RECOVER)
1009 return (B_FALSE);
1010
1011 /*
1012 * If we are in the middle of opening a pool, and the previous attempt
1013 * failed, don't bother logging any new ereports - we're just going to
1014 * get the same diagnosis anyway.
1015 */
1016 if (spa_load_state(spa) != SPA_LOAD_NONE &&
1017 spa->spa_last_open_failed)
1018 return (B_FALSE);
1019
1020 if (zio != NULL) {
1021 /*
1022 * If this is not a read or write zio, ignore the error. This
1023 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
1024 */
1025 if (zio->io_type != ZIO_TYPE_READ &&
1026 zio->io_type != ZIO_TYPE_WRITE)
1027 return (B_FALSE);
1028
1029 if (vd != NULL) {
1030 /*
1031 * If the vdev has already been marked as failing due
1032 * to a failed probe, then ignore any subsequent I/O
1033 * errors, as the DE will automatically fault the vdev
1034 * on the first such failure. This also catches cases
1035 * where vdev_remove_wanted is set and the device has
1036 * not yet been asynchronously placed into the REMOVED
1037 * state.
1038 */
1039 if (zio->io_vd == vd && !vdev_accessible(vd, zio))
1040 return (B_FALSE);
1041
1042 /*
1043 * Ignore checksum errors for reads from DTL regions of
1044 * leaf vdevs.
1045 */
1046 if (zio->io_type == ZIO_TYPE_READ &&
1047 zio->io_error == ECKSUM &&
1048 vd->vdev_ops->vdev_op_leaf &&
1049 vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
1050 return (B_FALSE);
1051 }
1052 }
1053
1054 /*
1055 * For probe failure, we want to avoid posting ereports if we've
1056 * already removed the device in the meantime.
1057 */
1058 if (vd != NULL &&
1059 strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
1060 (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
1061 return (B_FALSE);
1062
1063 /* Ignore bogus delay events (like from ioctls or unqueued IOs) */
1064 if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) &&
1065 (zio != NULL) && (!zio->io_timestamp)) {
1066 return (B_FALSE);
1067 }
1068 #else
1069 (void) subclass, (void) spa, (void) vd, (void) zio;
1070 #endif
1071 return (B_TRUE);
1072 }
1073
1074 /*
1075 * Post an ereport for the given subclass
1076 *
1077 * Returns
1078 * - 0 if an event was posted
1079 * - EINVAL if there was a problem posting event
1080 * - EBUSY if the event was rate limited
1081 * - EALREADY if the event was already posted (duplicate)
1082 */
1083 int
1084 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd,
1085 const zbookmark_phys_t *zb, zio_t *zio, uint64_t state)
1086 {
1087 int rc = 0;
1088 #ifdef _KERNEL
1089 nvlist_t *ereport = NULL;
1090 nvlist_t *detector = NULL;
1091
1092 if (!zfs_ereport_is_valid(subclass, spa, vd, zio))
1093 return (EINVAL);
1094
1095 if (zfs_ereport_is_duplicate(subclass, spa, vd, zb, zio, 0, 0))
1096 return (SET_ERROR(EALREADY));
1097
1098 if (zfs_is_ratelimiting_event(subclass, vd))
1099 return (SET_ERROR(EBUSY));
1100
1101 if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd,
1102 zb, zio, state, 0))
1103 return (SET_ERROR(EINVAL)); /* couldn't post event */
1104
1105 if (ereport == NULL)
1106 return (SET_ERROR(EINVAL));
1107
1108 /* Cleanup is handled by the callback function */
1109 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1110 #else
1111 (void) subclass, (void) spa, (void) vd, (void) zb, (void) zio,
1112 (void) state;
1113 #endif
1114 return (rc);
1115 }
1116
1117 /*
1118 * Prepare a checksum ereport
1119 *
1120 * Returns
1121 * - 0 if an event was posted
1122 * - EINVAL if there was a problem posting event
1123 * - EBUSY if the event was rate limited
1124 * - EALREADY if the event was already posted (duplicate)
1125 */
1126 int
1127 zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1128 struct zio *zio, uint64_t offset, uint64_t length, zio_bad_cksum_t *info)
1129 {
1130 zio_cksum_report_t *report;
1131
1132 #ifdef _KERNEL
1133 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
1134 return (SET_ERROR(EINVAL));
1135
1136 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
1137 offset, length))
1138 return (SET_ERROR(EALREADY));
1139
1140 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
1141 return (SET_ERROR(EBUSY));
1142 #else
1143 (void) zb, (void) offset;
1144 #endif
1145
1146 report = kmem_zalloc(sizeof (*report), KM_SLEEP);
1147
1148 zio_vsd_default_cksum_report(zio, report);
1149
1150 /* copy the checksum failure information if it was provided */
1151 if (info != NULL) {
1152 report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
1153 bcopy(info, report->zcr_ckinfo, sizeof (*info));
1154 }
1155
1156 report->zcr_sector = 1ULL << vd->vdev_top->vdev_ashift;
1157 report->zcr_align =
1158 vdev_psize_to_asize(vd->vdev_top, report->zcr_sector);
1159 report->zcr_length = length;
1160
1161 #ifdef _KERNEL
1162 (void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
1163 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length);
1164
1165 if (report->zcr_ereport == NULL) {
1166 zfs_ereport_free_checksum(report);
1167 return (0);
1168 }
1169 #endif
1170
1171 mutex_enter(&spa->spa_errlist_lock);
1172 report->zcr_next = zio->io_logical->io_cksum_report;
1173 zio->io_logical->io_cksum_report = report;
1174 mutex_exit(&spa->spa_errlist_lock);
1175 return (0);
1176 }
1177
1178 void
1179 zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data,
1180 const abd_t *bad_data, boolean_t drop_if_identical)
1181 {
1182 #ifdef _KERNEL
1183 zfs_ecksum_info_t *info;
1184
1185 info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
1186 good_data, bad_data, report->zcr_length, drop_if_identical);
1187 if (info != NULL)
1188 zfs_zevent_post(report->zcr_ereport,
1189 report->zcr_detector, zfs_zevent_post_cb);
1190 else
1191 zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector);
1192
1193 report->zcr_ereport = report->zcr_detector = NULL;
1194 if (info != NULL)
1195 kmem_free(info, sizeof (*info));
1196 #else
1197 (void) report, (void) good_data, (void) bad_data,
1198 (void) drop_if_identical;
1199 #endif
1200 }
1201
1202 void
1203 zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
1204 {
1205 #ifdef _KERNEL
1206 if (rpt->zcr_ereport != NULL) {
1207 fm_nvlist_destroy(rpt->zcr_ereport,
1208 FM_NVA_FREE);
1209 fm_nvlist_destroy(rpt->zcr_detector,
1210 FM_NVA_FREE);
1211 }
1212 #endif
1213 rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
1214
1215 if (rpt->zcr_ckinfo != NULL)
1216 kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
1217
1218 kmem_free(rpt, sizeof (*rpt));
1219 }
1220
1221 /*
1222 * Post a checksum ereport
1223 *
1224 * Returns
1225 * - 0 if an event was posted
1226 * - EINVAL if there was a problem posting event
1227 * - EBUSY if the event was rate limited
1228 * - EALREADY if the event was already posted (duplicate)
1229 */
1230 int
1231 zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1232 struct zio *zio, uint64_t offset, uint64_t length,
1233 const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc)
1234 {
1235 int rc = 0;
1236 #ifdef _KERNEL
1237 nvlist_t *ereport = NULL;
1238 nvlist_t *detector = NULL;
1239 zfs_ecksum_info_t *info;
1240
1241 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
1242 return (SET_ERROR(EINVAL));
1243
1244 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
1245 offset, length))
1246 return (SET_ERROR(EALREADY));
1247
1248 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
1249 return (SET_ERROR(EBUSY));
1250
1251 if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM,
1252 spa, vd, zb, zio, offset, length) || (ereport == NULL)) {
1253 return (SET_ERROR(EINVAL));
1254 }
1255
1256 info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
1257 B_FALSE);
1258
1259 if (info != NULL) {
1260 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1261 kmem_free(info, sizeof (*info));
1262 }
1263 #else
1264 (void) spa, (void) vd, (void) zb, (void) zio, (void) offset,
1265 (void) length, (void) good_data, (void) bad_data, (void) zbc;
1266 #endif
1267 return (rc);
1268 }
1269
1270 /*
1271 * The 'sysevent.fs.zfs.*' events are signals posted to notify user space of
1272 * change in the pool. All sysevents are listed in sys/sysevent/eventdefs.h
1273 * and are designed to be consumed by the ZFS Event Daemon (ZED). For
1274 * additional details refer to the zed(8) man page.
1275 */
1276 nvlist_t *
1277 zfs_event_create(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1278 nvlist_t *aux)
1279 {
1280 nvlist_t *resource = NULL;
1281 #ifdef _KERNEL
1282 char class[64];
1283
1284 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
1285 return (NULL);
1286
1287 if ((resource = fm_nvlist_create(NULL)) == NULL)
1288 return (NULL);
1289
1290 (void) snprintf(class, sizeof (class), "%s.%s.%s", type,
1291 ZFS_ERROR_CLASS, name);
1292 VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION));
1293 VERIFY0(nvlist_add_string(resource, FM_CLASS, class));
1294 VERIFY0(nvlist_add_string(resource,
1295 FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa)));
1296 VERIFY0(nvlist_add_uint64(resource,
1297 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)));
1298 VERIFY0(nvlist_add_uint64(resource,
1299 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, spa_state(spa)));
1300 VERIFY0(nvlist_add_int32(resource,
1301 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa)));
1302
1303 if (vd) {
1304 VERIFY0(nvlist_add_uint64(resource,
1305 FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid));
1306 VERIFY0(nvlist_add_uint64(resource,
1307 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state));
1308 if (vd->vdev_path != NULL)
1309 VERIFY0(nvlist_add_string(resource,
1310 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path));
1311 if (vd->vdev_devid != NULL)
1312 VERIFY0(nvlist_add_string(resource,
1313 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid));
1314 if (vd->vdev_fru != NULL)
1315 VERIFY0(nvlist_add_string(resource,
1316 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru));
1317 if (vd->vdev_enc_sysfs_path != NULL)
1318 VERIFY0(nvlist_add_string(resource,
1319 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1320 vd->vdev_enc_sysfs_path));
1321 }
1322
1323 /* also copy any optional payload data */
1324 if (aux) {
1325 nvpair_t *elem = NULL;
1326
1327 while ((elem = nvlist_next_nvpair(aux, elem)) != NULL)
1328 (void) nvlist_add_nvpair(resource, elem);
1329 }
1330 #else
1331 (void) spa, (void) vd, (void) type, (void) name, (void) aux;
1332 #endif
1333 return (resource);
1334 }
1335
1336 static void
1337 zfs_post_common(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1338 nvlist_t *aux)
1339 {
1340 #ifdef _KERNEL
1341 nvlist_t *resource;
1342
1343 resource = zfs_event_create(spa, vd, type, name, aux);
1344 if (resource)
1345 zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
1346 #else
1347 (void) spa, (void) vd, (void) type, (void) name, (void) aux;
1348 #endif
1349 }
1350
1351 /*
1352 * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
1353 * has been removed from the system. This will cause the DE to ignore any
1354 * recent I/O errors, inferring that they are due to the asynchronous device
1355 * removal.
1356 */
1357 void
1358 zfs_post_remove(spa_t *spa, vdev_t *vd)
1359 {
1360 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_REMOVED, NULL);
1361 }
1362
1363 /*
1364 * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
1365 * has the 'autoreplace' property set, and therefore any broken vdevs will be
1366 * handled by higher level logic, and no vdev fault should be generated.
1367 */
1368 void
1369 zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
1370 {
1371 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_AUTOREPLACE, NULL);
1372 }
1373
1374 /*
1375 * The 'resource.fs.zfs.statechange' event is an internal signal that the
1376 * given vdev has transitioned its state to DEGRADED or HEALTHY. This will
1377 * cause the retire agent to repair any outstanding fault management cases
1378 * open because the device was not found (fault.fs.zfs.device).
1379 */
1380 void
1381 zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate)
1382 {
1383 #ifdef _KERNEL
1384 nvlist_t *aux;
1385
1386 /*
1387 * Add optional supplemental keys to payload
1388 */
1389 aux = fm_nvlist_create(NULL);
1390 if (vd && aux) {
1391 if (vd->vdev_physpath) {
1392 (void) nvlist_add_string(aux,
1393 FM_EREPORT_PAYLOAD_ZFS_VDEV_PHYSPATH,
1394 vd->vdev_physpath);
1395 }
1396 if (vd->vdev_enc_sysfs_path) {
1397 (void) nvlist_add_string(aux,
1398 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1399 vd->vdev_enc_sysfs_path);
1400 }
1401
1402 (void) nvlist_add_uint64(aux,
1403 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE, laststate);
1404 }
1405
1406 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_STATECHANGE,
1407 aux);
1408
1409 if (aux)
1410 fm_nvlist_destroy(aux, FM_NVA_FREE);
1411 #else
1412 (void) spa, (void) vd, (void) laststate;
1413 #endif
1414 }
1415
1416 #ifdef _KERNEL
1417 void
1418 zfs_ereport_init(void)
1419 {
1420 mutex_init(&recent_events_lock, NULL, MUTEX_DEFAULT, NULL);
1421 list_create(&recent_events_list, sizeof (recent_events_node_t),
1422 offsetof(recent_events_node_t, re_list_link));
1423 avl_create(&recent_events_tree, recent_events_compare,
1424 sizeof (recent_events_node_t), offsetof(recent_events_node_t,
1425 re_tree_link));
1426 }
1427
1428 /*
1429 * This 'early' fini needs to run before zfs_fini() which on Linux waits
1430 * for the system_delay_taskq to drain.
1431 */
1432 void
1433 zfs_ereport_taskq_fini(void)
1434 {
1435 mutex_enter(&recent_events_lock);
1436 if (recent_events_cleaner_tqid != 0) {
1437 taskq_cancel_id(system_delay_taskq, recent_events_cleaner_tqid);
1438 recent_events_cleaner_tqid = 0;
1439 }
1440 mutex_exit(&recent_events_lock);
1441 }
1442
1443 void
1444 zfs_ereport_fini(void)
1445 {
1446 recent_events_node_t *entry;
1447
1448 while ((entry = list_head(&recent_events_list)) != NULL) {
1449 avl_remove(&recent_events_tree, entry);
1450 list_remove(&recent_events_list, entry);
1451 kmem_free(entry, sizeof (*entry));
1452 }
1453 avl_destroy(&recent_events_tree);
1454 list_destroy(&recent_events_list);
1455 mutex_destroy(&recent_events_lock);
1456 }
1457
1458 void
1459 zfs_ereport_snapshot_post(const char *subclass, spa_t *spa, const char *name)
1460 {
1461 nvlist_t *aux;
1462
1463 aux = fm_nvlist_create(NULL);
1464 nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_SNAPSHOT_NAME, name);
1465
1466 zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
1467 fm_nvlist_destroy(aux, FM_NVA_FREE);
1468 }
1469
1470 /*
1471 * Post when a event when a zvol is created or removed
1472 *
1473 * This is currently only used by macOS, since it uses the event to create
1474 * symlinks between the volume name (mypool/myvol) and the actual /dev
1475 * device (/dev/disk3). For example:
1476 *
1477 * /var/run/zfs/dsk/mypool/myvol -> /dev/disk3
1478 *
1479 * name: The full name of the zvol ("mypool/myvol")
1480 * dev_name: The full /dev name for the zvol ("/dev/disk3")
1481 * raw_name: The raw /dev name for the zvol ("/dev/rdisk3")
1482 */
1483 void
1484 zfs_ereport_zvol_post(const char *subclass, const char *name,
1485 const char *dev_name, const char *raw_name)
1486 {
1487 nvlist_t *aux;
1488 char *r;
1489
1490 boolean_t locked = mutex_owned(&spa_namespace_lock);
1491 if (!locked) mutex_enter(&spa_namespace_lock);
1492 spa_t *spa = spa_lookup(name);
1493 if (!locked) mutex_exit(&spa_namespace_lock);
1494
1495 if (spa == NULL)
1496 return;
1497
1498 aux = fm_nvlist_create(NULL);
1499 nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_DEVICE_NAME, dev_name);
1500 nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_RAW_DEVICE_NAME,
1501 raw_name);
1502 r = strchr(name, '/');
1503 if (r && r[1])
1504 nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_VOLUME, &r[1]);
1505
1506 zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
1507 fm_nvlist_destroy(aux, FM_NVA_FREE);
1508 }
1509
1510 EXPORT_SYMBOL(zfs_ereport_post);
1511 EXPORT_SYMBOL(zfs_ereport_is_valid);
1512 EXPORT_SYMBOL(zfs_ereport_post_checksum);
1513 EXPORT_SYMBOL(zfs_post_remove);
1514 EXPORT_SYMBOL(zfs_post_autoreplace);
1515 EXPORT_SYMBOL(zfs_post_state_change);
1516
1517 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_max, UINT, ZMOD_RW,
1518 "Maximum recent zevents records to retain for duplicate checking");
1519 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_expire_secs, UINT, ZMOD_RW,
1520 "Expiration time for recent zevents records");
1521 #endif /* _KERNEL */