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 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 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
recent_events_compare(const void * a,const void * b)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
zfs_ereport_cleaner(void * arg)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
zfs_ereport_schedule_cleaner(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
zfs_ereport_clear(spa_t * spa,vdev_t * vd)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
zfs_ereport_is_duplicate(const char * subclass,spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,zio_t * zio,uint64_t offset,uint64_t size)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
zfs_zevent_post_cb(nvlist_t * nvl,nvlist_t * detector)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
zfs_is_ratelimiting_event(const char * subclass,vdev_t * vd)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
zfs_ereport_start(nvlist_t ** ereport_out,nvlist_t ** detector_out,const char * subclass,spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,zio_t * zio,uint64_t stateoroffset,uint64_t size)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
update_histogram(uint64_t value_arg,uint32_t * hist,uint32_t * count)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
zei_shrink_ranges(zfs_ecksum_info_t * eip)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
zei_add_range(zfs_ecksum_info_t * eip,int start,int end)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
zei_range_total_size(zfs_ecksum_info_t * eip)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 *
annotate_ecksum(nvlist_t * ereport,zio_bad_cksum_t * info,const abd_t * goodabd,const abd_t * badabd,size_t size,boolean_t drop_if_identical)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 uint64_t allset = 0;
829 uint64_t allcleared = 0;
830
831 size_t nui64s = size / sizeof (uint64_t);
832
833 size_t inline_size;
834 int no_inline = 0;
835 size_t idx;
836 size_t range;
837
838 size_t offset = 0;
839 ssize_t start = -1;
840
841 zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
842
843 /* don't do any annotation for injected checksum errors */
844 if (info != NULL && info->zbc_injected)
845 return (eip);
846
847 if (info != NULL && info->zbc_has_cksum) {
848 fm_payload_set(ereport,
849 FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
850 DATA_TYPE_UINT64_ARRAY,
851 sizeof (info->zbc_expected) / sizeof (uint64_t),
852 (uint64_t *)&info->zbc_expected,
853 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
854 DATA_TYPE_UINT64_ARRAY,
855 sizeof (info->zbc_actual) / sizeof (uint64_t),
856 (uint64_t *)&info->zbc_actual,
857 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
858 DATA_TYPE_STRING,
859 info->zbc_checksum_name,
860 NULL);
861
862 if (info->zbc_byteswapped) {
863 fm_payload_set(ereport,
864 FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
865 DATA_TYPE_BOOLEAN, 1,
866 NULL);
867 }
868 }
869
870 if (badabd == NULL || goodabd == NULL)
871 return (eip);
872
873 ASSERT3U(nui64s, <=, UINT32_MAX);
874 ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
875 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
876 ASSERT3U(size, <=, UINT32_MAX);
877
878 good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
879 bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
880
881 /* build up the range list by comparing the two buffers. */
882 for (idx = 0; idx < nui64s; idx++) {
883 if (good[idx] == bad[idx]) {
884 if (start == -1)
885 continue;
886
887 zei_add_range(eip, start, idx);
888 start = -1;
889 } else {
890 if (start != -1)
891 continue;
892
893 start = idx;
894 }
895 }
896 if (start != -1)
897 zei_add_range(eip, start, idx);
898
899 /* See if it will fit in our inline buffers */
900 inline_size = zei_range_total_size(eip);
901 if (inline_size > ZFM_MAX_INLINE)
902 no_inline = 1;
903
904 /*
905 * If there is no change and we want to drop if the buffers are
906 * identical, do so.
907 */
908 if (inline_size == 0 && drop_if_identical) {
909 kmem_free(eip, sizeof (*eip));
910 abd_return_buf((abd_t *)goodabd, (void *)good, size);
911 abd_return_buf((abd_t *)badabd, (void *)bad, size);
912 return (NULL);
913 }
914
915 /*
916 * Now walk through the ranges, filling in the details of the
917 * differences. Also convert our uint64_t-array offsets to byte
918 * offsets.
919 */
920 for (range = 0; range < eip->zei_range_count; range++) {
921 size_t start = eip->zei_ranges[range].zr_start;
922 size_t end = eip->zei_ranges[range].zr_end;
923
924 for (idx = start; idx < end; idx++) {
925 uint64_t set, cleared;
926
927 // bits set in bad, but not in good
928 set = ((~good[idx]) & bad[idx]);
929 // bits set in good, but not in bad
930 cleared = (good[idx] & (~bad[idx]));
931
932 allset |= set;
933 allcleared |= cleared;
934
935 if (!no_inline) {
936 ASSERT3U(offset, <, inline_size);
937 eip->zei_bits_set[offset] = set;
938 eip->zei_bits_cleared[offset] = cleared;
939 offset++;
940 }
941
942 update_histogram(set, eip->zei_histogram_set,
943 &eip->zei_range_sets[range]);
944 update_histogram(cleared, eip->zei_histogram_cleared,
945 &eip->zei_range_clears[range]);
946 }
947
948 /* convert to byte offsets */
949 eip->zei_ranges[range].zr_start *= sizeof (uint64_t);
950 eip->zei_ranges[range].zr_end *= sizeof (uint64_t);
951 }
952
953 abd_return_buf((abd_t *)goodabd, (void *)good, size);
954 abd_return_buf((abd_t *)badabd, (void *)bad, size);
955
956 eip->zei_allowed_mingap *= sizeof (uint64_t);
957 inline_size *= sizeof (uint64_t);
958
959 /* fill in ereport */
960 fm_payload_set(ereport,
961 FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
962 DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
963 (uint32_t *)eip->zei_ranges,
964 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
965 DATA_TYPE_UINT32, eip->zei_allowed_mingap,
966 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
967 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
968 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
969 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
970 NULL);
971
972 if (!no_inline) {
973 fm_payload_set(ereport,
974 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
975 DATA_TYPE_UINT8_ARRAY,
976 inline_size, (uint8_t *)eip->zei_bits_set,
977 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
978 DATA_TYPE_UINT8_ARRAY,
979 inline_size, (uint8_t *)eip->zei_bits_cleared,
980 NULL);
981 } else {
982 fm_payload_set(ereport,
983 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
984 DATA_TYPE_UINT32_ARRAY,
985 NBBY * sizeof (uint64_t), eip->zei_histogram_set,
986 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
987 DATA_TYPE_UINT32_ARRAY,
988 NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
989 NULL);
990 }
991 return (eip);
992 }
993 #else
994 void
zfs_ereport_clear(spa_t * spa,vdev_t * vd)995 zfs_ereport_clear(spa_t *spa, vdev_t *vd)
996 {
997 (void) spa, (void) vd;
998 }
999 #endif
1000
1001 /*
1002 * Make sure our event is still valid for the given zio/vdev/pool. For example,
1003 * we don't want to keep logging events for a faulted or missing vdev.
1004 */
1005 boolean_t
zfs_ereport_is_valid(const char * subclass,spa_t * spa,vdev_t * vd,zio_t * zio)1006 zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio)
1007 {
1008 #ifdef _KERNEL
1009 /*
1010 * If we are doing a spa_tryimport() or in recovery mode,
1011 * ignore errors.
1012 */
1013 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
1014 spa_load_state(spa) == SPA_LOAD_RECOVER)
1015 return (B_FALSE);
1016
1017 /*
1018 * If we are in the middle of opening a pool, and the previous attempt
1019 * failed, don't bother logging any new ereports - we're just going to
1020 * get the same diagnosis anyway.
1021 */
1022 if (spa_load_state(spa) != SPA_LOAD_NONE &&
1023 spa->spa_last_open_failed)
1024 return (B_FALSE);
1025
1026 if (zio != NULL) {
1027 /*
1028 * If this is not a read or write zio, ignore the error. This
1029 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
1030 */
1031 if (zio->io_type != ZIO_TYPE_READ &&
1032 zio->io_type != ZIO_TYPE_WRITE)
1033 return (B_FALSE);
1034
1035 if (vd != NULL) {
1036 /*
1037 * If the vdev has already been marked as failing due
1038 * to a failed probe, then ignore any subsequent I/O
1039 * errors, as the DE will automatically fault the vdev
1040 * on the first such failure. This also catches cases
1041 * where vdev_remove_wanted is set and the device has
1042 * not yet been asynchronously placed into the REMOVED
1043 * state.
1044 */
1045 if (zio->io_vd == vd && !vdev_accessible(vd, zio))
1046 return (B_FALSE);
1047
1048 /*
1049 * Ignore checksum errors for reads from DTL regions of
1050 * leaf vdevs.
1051 */
1052 if (zio->io_type == ZIO_TYPE_READ &&
1053 zio->io_error == ECKSUM &&
1054 vd->vdev_ops->vdev_op_leaf &&
1055 vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
1056 return (B_FALSE);
1057 }
1058 }
1059
1060 /*
1061 * For probe failure, we want to avoid posting ereports if we've
1062 * already removed the device in the meantime.
1063 */
1064 if (vd != NULL &&
1065 strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
1066 (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
1067 return (B_FALSE);
1068
1069 /* Ignore bogus delay events (like from ioctls or unqueued IOs) */
1070 if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) &&
1071 (zio != NULL) && (!zio->io_timestamp)) {
1072 return (B_FALSE);
1073 }
1074 #else
1075 (void) subclass, (void) spa, (void) vd, (void) zio;
1076 #endif
1077 return (B_TRUE);
1078 }
1079
1080 /*
1081 * Post an ereport for the given subclass
1082 *
1083 * Returns
1084 * - 0 if an event was posted
1085 * - EINVAL if there was a problem posting event
1086 * - EBUSY if the event was rate limited
1087 * - EALREADY if the event was already posted (duplicate)
1088 */
1089 int
zfs_ereport_post(const char * subclass,spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,zio_t * zio,uint64_t state)1090 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd,
1091 const zbookmark_phys_t *zb, zio_t *zio, uint64_t state)
1092 {
1093 int rc = 0;
1094 #ifdef _KERNEL
1095 nvlist_t *ereport = NULL;
1096 nvlist_t *detector = NULL;
1097
1098 if (!zfs_ereport_is_valid(subclass, spa, vd, zio))
1099 return (EINVAL);
1100
1101 if (zfs_ereport_is_duplicate(subclass, spa, vd, zb, zio, 0, 0))
1102 return (SET_ERROR(EALREADY));
1103
1104 if (zfs_is_ratelimiting_event(subclass, vd))
1105 return (SET_ERROR(EBUSY));
1106
1107 if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd,
1108 zb, zio, state, 0))
1109 return (SET_ERROR(EINVAL)); /* couldn't post event */
1110
1111 if (ereport == NULL)
1112 return (SET_ERROR(EINVAL));
1113
1114 /* Cleanup is handled by the callback function */
1115 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1116 #else
1117 (void) subclass, (void) spa, (void) vd, (void) zb, (void) zio,
1118 (void) state;
1119 #endif
1120 return (rc);
1121 }
1122
1123 /*
1124 * Prepare a checksum ereport
1125 *
1126 * Returns
1127 * - 0 if an event was posted
1128 * - EINVAL if there was a problem posting event
1129 * - EBUSY if the event was rate limited
1130 * - EALREADY if the event was already posted (duplicate)
1131 */
1132 int
zfs_ereport_start_checksum(spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,struct zio * zio,uint64_t offset,uint64_t length,zio_bad_cksum_t * info)1133 zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1134 struct zio *zio, uint64_t offset, uint64_t length, zio_bad_cksum_t *info)
1135 {
1136 zio_cksum_report_t *report;
1137
1138 #ifdef _KERNEL
1139 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
1140 return (SET_ERROR(EINVAL));
1141
1142 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
1143 offset, length))
1144 return (SET_ERROR(EALREADY));
1145
1146 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
1147 return (SET_ERROR(EBUSY));
1148 #else
1149 (void) zb, (void) offset;
1150 #endif
1151
1152 report = kmem_zalloc(sizeof (*report), KM_SLEEP);
1153
1154 zio_vsd_default_cksum_report(zio, report);
1155
1156 /* copy the checksum failure information if it was provided */
1157 if (info != NULL) {
1158 report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
1159 bcopy(info, report->zcr_ckinfo, sizeof (*info));
1160 }
1161
1162 report->zcr_sector = 1ULL << vd->vdev_top->vdev_ashift;
1163 report->zcr_align =
1164 vdev_psize_to_asize(vd->vdev_top, report->zcr_sector);
1165 report->zcr_length = length;
1166
1167 #ifdef _KERNEL
1168 (void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
1169 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length);
1170
1171 if (report->zcr_ereport == NULL) {
1172 zfs_ereport_free_checksum(report);
1173 return (0);
1174 }
1175 #endif
1176
1177 mutex_enter(&spa->spa_errlist_lock);
1178 report->zcr_next = zio->io_logical->io_cksum_report;
1179 zio->io_logical->io_cksum_report = report;
1180 mutex_exit(&spa->spa_errlist_lock);
1181 return (0);
1182 }
1183
1184 void
zfs_ereport_finish_checksum(zio_cksum_report_t * report,const abd_t * good_data,const abd_t * bad_data,boolean_t drop_if_identical)1185 zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data,
1186 const abd_t *bad_data, boolean_t drop_if_identical)
1187 {
1188 #ifdef _KERNEL
1189 zfs_ecksum_info_t *info;
1190
1191 info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
1192 good_data, bad_data, report->zcr_length, drop_if_identical);
1193 if (info != NULL)
1194 zfs_zevent_post(report->zcr_ereport,
1195 report->zcr_detector, zfs_zevent_post_cb);
1196 else
1197 zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector);
1198
1199 report->zcr_ereport = report->zcr_detector = NULL;
1200 if (info != NULL)
1201 kmem_free(info, sizeof (*info));
1202 #else
1203 (void) report, (void) good_data, (void) bad_data,
1204 (void) drop_if_identical;
1205 #endif
1206 }
1207
1208 void
zfs_ereport_free_checksum(zio_cksum_report_t * rpt)1209 zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
1210 {
1211 #ifdef _KERNEL
1212 if (rpt->zcr_ereport != NULL) {
1213 fm_nvlist_destroy(rpt->zcr_ereport,
1214 FM_NVA_FREE);
1215 fm_nvlist_destroy(rpt->zcr_detector,
1216 FM_NVA_FREE);
1217 }
1218 #endif
1219 rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
1220
1221 if (rpt->zcr_ckinfo != NULL)
1222 kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
1223
1224 kmem_free(rpt, sizeof (*rpt));
1225 }
1226
1227 /*
1228 * Post a checksum ereport
1229 *
1230 * Returns
1231 * - 0 if an event was posted
1232 * - EINVAL if there was a problem posting event
1233 * - EBUSY if the event was rate limited
1234 * - EALREADY if the event was already posted (duplicate)
1235 */
1236 int
zfs_ereport_post_checksum(spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,struct zio * zio,uint64_t offset,uint64_t length,const abd_t * good_data,const abd_t * bad_data,zio_bad_cksum_t * zbc)1237 zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1238 struct zio *zio, uint64_t offset, uint64_t length,
1239 const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc)
1240 {
1241 int rc = 0;
1242 #ifdef _KERNEL
1243 nvlist_t *ereport = NULL;
1244 nvlist_t *detector = NULL;
1245 zfs_ecksum_info_t *info;
1246
1247 if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
1248 return (SET_ERROR(EINVAL));
1249
1250 if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
1251 offset, length))
1252 return (SET_ERROR(EALREADY));
1253
1254 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
1255 return (SET_ERROR(EBUSY));
1256
1257 if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM,
1258 spa, vd, zb, zio, offset, length) || (ereport == NULL)) {
1259 return (SET_ERROR(EINVAL));
1260 }
1261
1262 info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
1263 B_FALSE);
1264
1265 if (info != NULL) {
1266 rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1267 kmem_free(info, sizeof (*info));
1268 }
1269 #else
1270 (void) spa, (void) vd, (void) zb, (void) zio, (void) offset,
1271 (void) length, (void) good_data, (void) bad_data, (void) zbc;
1272 #endif
1273 return (rc);
1274 }
1275
1276 /*
1277 * The 'sysevent.fs.zfs.*' events are signals posted to notify user space of
1278 * change in the pool. All sysevents are listed in sys/sysevent/eventdefs.h
1279 * and are designed to be consumed by the ZFS Event Daemon (ZED). For
1280 * additional details refer to the zed(8) man page.
1281 */
1282 nvlist_t *
zfs_event_create(spa_t * spa,vdev_t * vd,const char * type,const char * name,nvlist_t * aux)1283 zfs_event_create(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1284 nvlist_t *aux)
1285 {
1286 nvlist_t *resource = NULL;
1287 #ifdef _KERNEL
1288 char class[64];
1289
1290 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
1291 return (NULL);
1292
1293 if ((resource = fm_nvlist_create(NULL)) == NULL)
1294 return (NULL);
1295
1296 (void) snprintf(class, sizeof (class), "%s.%s.%s", type,
1297 ZFS_ERROR_CLASS, name);
1298 VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION));
1299 VERIFY0(nvlist_add_string(resource, FM_CLASS, class));
1300 VERIFY0(nvlist_add_string(resource,
1301 FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa)));
1302 VERIFY0(nvlist_add_uint64(resource,
1303 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)));
1304 VERIFY0(nvlist_add_uint64(resource,
1305 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, spa_state(spa)));
1306 VERIFY0(nvlist_add_int32(resource,
1307 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa)));
1308
1309 if (vd) {
1310 VERIFY0(nvlist_add_uint64(resource,
1311 FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid));
1312 VERIFY0(nvlist_add_uint64(resource,
1313 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state));
1314 if (vd->vdev_path != NULL)
1315 VERIFY0(nvlist_add_string(resource,
1316 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path));
1317 if (vd->vdev_devid != NULL)
1318 VERIFY0(nvlist_add_string(resource,
1319 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid));
1320 if (vd->vdev_fru != NULL)
1321 VERIFY0(nvlist_add_string(resource,
1322 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru));
1323 if (vd->vdev_enc_sysfs_path != NULL)
1324 VERIFY0(nvlist_add_string(resource,
1325 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1326 vd->vdev_enc_sysfs_path));
1327 }
1328
1329 /* also copy any optional payload data */
1330 if (aux) {
1331 nvpair_t *elem = NULL;
1332
1333 while ((elem = nvlist_next_nvpair(aux, elem)) != NULL)
1334 (void) nvlist_add_nvpair(resource, elem);
1335 }
1336 #else
1337 (void) spa, (void) vd, (void) type, (void) name, (void) aux;
1338 #endif
1339 return (resource);
1340 }
1341
1342 static void
zfs_post_common(spa_t * spa,vdev_t * vd,const char * type,const char * name,nvlist_t * aux)1343 zfs_post_common(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1344 nvlist_t *aux)
1345 {
1346 #ifdef _KERNEL
1347 nvlist_t *resource;
1348
1349 resource = zfs_event_create(spa, vd, type, name, aux);
1350 if (resource)
1351 zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
1352 #else
1353 (void) spa, (void) vd, (void) type, (void) name, (void) aux;
1354 #endif
1355 }
1356
1357 /*
1358 * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
1359 * has been removed from the system. This will cause the DE to ignore any
1360 * recent I/O errors, inferring that they are due to the asynchronous device
1361 * removal.
1362 */
1363 void
zfs_post_remove(spa_t * spa,vdev_t * vd)1364 zfs_post_remove(spa_t *spa, vdev_t *vd)
1365 {
1366 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_REMOVED, NULL);
1367 }
1368
1369 /*
1370 * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
1371 * has the 'autoreplace' property set, and therefore any broken vdevs will be
1372 * handled by higher level logic, and no vdev fault should be generated.
1373 */
1374 void
zfs_post_autoreplace(spa_t * spa,vdev_t * vd)1375 zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
1376 {
1377 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_AUTOREPLACE, NULL);
1378 }
1379
1380 /*
1381 * The 'resource.fs.zfs.statechange' event is an internal signal that the
1382 * given vdev has transitioned its state to DEGRADED or HEALTHY. This will
1383 * cause the retire agent to repair any outstanding fault management cases
1384 * open because the device was not found (fault.fs.zfs.device).
1385 */
1386 void
zfs_post_state_change(spa_t * spa,vdev_t * vd,uint64_t laststate)1387 zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate)
1388 {
1389 #ifdef _KERNEL
1390 nvlist_t *aux;
1391
1392 /*
1393 * Add optional supplemental keys to payload
1394 */
1395 aux = fm_nvlist_create(NULL);
1396 if (vd && aux) {
1397 if (vd->vdev_physpath) {
1398 (void) nvlist_add_string(aux,
1399 FM_EREPORT_PAYLOAD_ZFS_VDEV_PHYSPATH,
1400 vd->vdev_physpath);
1401 }
1402 if (vd->vdev_enc_sysfs_path) {
1403 (void) nvlist_add_string(aux,
1404 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1405 vd->vdev_enc_sysfs_path);
1406 }
1407
1408 (void) nvlist_add_uint64(aux,
1409 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE, laststate);
1410 }
1411
1412 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_STATECHANGE,
1413 aux);
1414
1415 if (aux)
1416 fm_nvlist_destroy(aux, FM_NVA_FREE);
1417 #else
1418 (void) spa, (void) vd, (void) laststate;
1419 #endif
1420 }
1421
1422 #ifdef _KERNEL
1423 void
zfs_ereport_init(void)1424 zfs_ereport_init(void)
1425 {
1426 mutex_init(&recent_events_lock, NULL, MUTEX_DEFAULT, NULL);
1427 list_create(&recent_events_list, sizeof (recent_events_node_t),
1428 offsetof(recent_events_node_t, re_list_link));
1429 avl_create(&recent_events_tree, recent_events_compare,
1430 sizeof (recent_events_node_t), offsetof(recent_events_node_t,
1431 re_tree_link));
1432 }
1433
1434 /*
1435 * This 'early' fini needs to run before zfs_fini() which on Linux waits
1436 * for the system_delay_taskq to drain.
1437 */
1438 void
zfs_ereport_taskq_fini(void)1439 zfs_ereport_taskq_fini(void)
1440 {
1441 mutex_enter(&recent_events_lock);
1442 if (recent_events_cleaner_tqid != 0) {
1443 taskq_cancel_id(system_delay_taskq, recent_events_cleaner_tqid);
1444 recent_events_cleaner_tqid = 0;
1445 }
1446 mutex_exit(&recent_events_lock);
1447 }
1448
1449 void
zfs_ereport_fini(void)1450 zfs_ereport_fini(void)
1451 {
1452 recent_events_node_t *entry;
1453
1454 while ((entry = list_head(&recent_events_list)) != NULL) {
1455 avl_remove(&recent_events_tree, entry);
1456 list_remove(&recent_events_list, entry);
1457 kmem_free(entry, sizeof (*entry));
1458 }
1459 avl_destroy(&recent_events_tree);
1460 list_destroy(&recent_events_list);
1461 mutex_destroy(&recent_events_lock);
1462 }
1463
1464 void
zfs_ereport_snapshot_post(const char * subclass,spa_t * spa,const char * name)1465 zfs_ereport_snapshot_post(const char *subclass, spa_t *spa, const char *name)
1466 {
1467 nvlist_t *aux;
1468
1469 aux = fm_nvlist_create(NULL);
1470 nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_SNAPSHOT_NAME, name);
1471
1472 zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
1473 fm_nvlist_destroy(aux, FM_NVA_FREE);
1474 }
1475
1476 /*
1477 * Post when a event when a zvol is created or removed
1478 *
1479 * This is currently only used by macOS, since it uses the event to create
1480 * symlinks between the volume name (mypool/myvol) and the actual /dev
1481 * device (/dev/disk3). For example:
1482 *
1483 * /var/run/zfs/dsk/mypool/myvol -> /dev/disk3
1484 *
1485 * name: The full name of the zvol ("mypool/myvol")
1486 * dev_name: The full /dev name for the zvol ("/dev/disk3")
1487 * raw_name: The raw /dev name for the zvol ("/dev/rdisk3")
1488 */
1489 void
zfs_ereport_zvol_post(const char * subclass,const char * name,const char * dev_name,const char * raw_name)1490 zfs_ereport_zvol_post(const char *subclass, const char *name,
1491 const char *dev_name, const char *raw_name)
1492 {
1493 nvlist_t *aux;
1494 char *r;
1495
1496 boolean_t locked = mutex_owned(&spa_namespace_lock);
1497 if (!locked) mutex_enter(&spa_namespace_lock);
1498 spa_t *spa = spa_lookup(name);
1499 if (!locked) mutex_exit(&spa_namespace_lock);
1500
1501 if (spa == NULL)
1502 return;
1503
1504 aux = fm_nvlist_create(NULL);
1505 nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_DEVICE_NAME, dev_name);
1506 nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_RAW_DEVICE_NAME,
1507 raw_name);
1508 r = strchr(name, '/');
1509 if (r && r[1])
1510 nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_VOLUME, &r[1]);
1511
1512 zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
1513 fm_nvlist_destroy(aux, FM_NVA_FREE);
1514 }
1515
1516 EXPORT_SYMBOL(zfs_ereport_post);
1517 EXPORT_SYMBOL(zfs_ereport_is_valid);
1518 EXPORT_SYMBOL(zfs_ereport_post_checksum);
1519 EXPORT_SYMBOL(zfs_post_remove);
1520 EXPORT_SYMBOL(zfs_post_autoreplace);
1521 EXPORT_SYMBOL(zfs_post_state_change);
1522
1523 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_max, UINT, ZMOD_RW,
1524 "Maximum recent zevents records to retain for duplicate checking");
1525 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_expire_secs, UINT, ZMOD_RW,
1526 "Expiration time for recent zevents records");
1527 #endif /* _KERNEL */
1528