]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_fm.c
OpenZFS 9424 - ztest failure: "unprotected error in call to Lua API (Invalid value...
[mirror_zfs.git] / module / zfs / zfs_fm.c
CommitLineData
34dc7c2f
BB
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/*
9babb374 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
34dc7c2f
BB
23 * Use is subject to license terms.
24 */
25
5ffb9d1d
GW
26/*
27 * Copyright (c) 2012 by Delphix. All rights reserved.
28 */
29
34dc7c2f
BB
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>
428870ff 35#include <sys/zio_checksum.h>
34dc7c2f
BB
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
b128c09f 54 * SPA-wide ENA (Error Numeric Association).
34dc7c2f
BB
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
b128c09f
BB
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).
428870ff
BB
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.
34dc7c2f 103 */
428870ff 104#ifdef _KERNEL
12fa0466 105void
26685276
BB
106zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector)
107{
108 if (nvl)
109 fm_nvlist_destroy(nvl, FM_NVA_FREE);
110
111 if (detector)
112 fm_nvlist_destroy(detector, FM_NVA_FREE);
113}
114
6078881a
TH
115/*
116 * We want to rate limit ZIO delay and checksum events so as to not
117 * flood ZED when a disk is acting up.
118 *
119 * Returns 1 if we're ratelimiting, 0 if not.
120 */
121static int
122zfs_is_ratelimiting_event(const char *subclass, vdev_t *vd)
123{
124 int rc = 0;
125 /*
126 * __ratelimit() returns 1 if we're *not* ratelimiting and 0 if we
127 * are. Invert it to get our return value.
128 */
129 if (strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
130 rc = !zfs_ratelimit(&vd->vdev_delay_rl);
131 } else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
132 rc = !zfs_ratelimit(&vd->vdev_checksum_rl);
133 }
134
135 if (rc) {
136 /* We're rate limiting */
137 fm_erpt_dropped_increment();
138 }
139
140 return (rc);
141}
0426c168 142
428870ff
BB
143static void
144zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
a2c2ed1b 145 const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
b5256303 146 zio_t *zio, uint64_t stateoroffset, uint64_t size)
34dc7c2f 147{
34dc7c2f 148 nvlist_t *ereport, *detector;
428870ff 149
34dc7c2f
BB
150 uint64_t ena;
151 char class[64];
152
153 /*
428870ff
BB
154 * If we are doing a spa_tryimport() or in recovery mode,
155 * ignore errors.
34dc7c2f 156 */
428870ff
BB
157 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
158 spa_load_state(spa) == SPA_LOAD_RECOVER)
34dc7c2f
BB
159 return;
160
161 /*
162 * If we are in the middle of opening a pool, and the previous attempt
163 * failed, don't bother logging any new ereports - we're just going to
164 * get the same diagnosis anyway.
165 */
428870ff 166 if (spa_load_state(spa) != SPA_LOAD_NONE &&
34dc7c2f
BB
167 spa->spa_last_open_failed)
168 return;
169
b128c09f
BB
170 if (zio != NULL) {
171 /*
172 * If this is not a read or write zio, ignore the error. This
173 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
174 */
175 if (zio->io_type != ZIO_TYPE_READ &&
176 zio->io_type != ZIO_TYPE_WRITE)
177 return;
34dc7c2f 178
9babb374
BB
179 if (vd != NULL) {
180 /*
181 * If the vdev has already been marked as failing due
182 * to a failed probe, then ignore any subsequent I/O
183 * errors, as the DE will automatically fault the vdev
184 * on the first such failure. This also catches cases
185 * where vdev_remove_wanted is set and the device has
186 * not yet been asynchronously placed into the REMOVED
187 * state.
188 */
428870ff 189 if (zio->io_vd == vd && !vdev_accessible(vd, zio))
9babb374
BB
190 return;
191
192 /*
193 * Ignore checksum errors for reads from DTL regions of
194 * leaf vdevs.
195 */
196 if (zio->io_type == ZIO_TYPE_READ &&
197 zio->io_error == ECKSUM &&
198 vd->vdev_ops->vdev_op_leaf &&
199 vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
200 return;
201 }
b128c09f 202 }
34dc7c2f 203
428870ff
BB
204 /*
205 * For probe failure, we want to avoid posting ereports if we've
206 * already removed the device in the meantime.
207 */
208 if (vd != NULL &&
209 strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
210 (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
211 return;
212
a32df59e
TC
213 if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) &&
214 (zio != NULL) && (!zio->io_timestamp)) {
215 /* Ignore bogus delay events */
216 return;
217 }
218
34dc7c2f
BB
219 if ((ereport = fm_nvlist_create(NULL)) == NULL)
220 return;
221
222 if ((detector = fm_nvlist_create(NULL)) == NULL) {
223 fm_nvlist_destroy(ereport, FM_NVA_FREE);
224 return;
225 }
226
227 /*
228 * Serialize ereport generation
229 */
230 mutex_enter(&spa->spa_errlist_lock);
231
232 /*
233 * Determine the ENA to use for this event. If we are in a loading
234 * state, use a SPA-wide ENA. Otherwise, if we are in an I/O state, use
235 * a root zio-wide ENA. Otherwise, simply use a unique ENA.
236 */
428870ff 237 if (spa_load_state(spa) != SPA_LOAD_NONE) {
34dc7c2f
BB
238 if (spa->spa_ena == 0)
239 spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
240 ena = spa->spa_ena;
241 } else if (zio != NULL && zio->io_logical != NULL) {
242 if (zio->io_logical->io_ena == 0)
243 zio->io_logical->io_ena =
244 fm_ena_generate(0, FM_ENA_FMT1);
245 ena = zio->io_logical->io_ena;
246 } else {
247 ena = fm_ena_generate(0, FM_ENA_FMT1);
248 }
249
250 /*
251 * Construct the full class, detector, and other standard FMA fields.
252 */
253 (void) snprintf(class, sizeof (class), "%s.%s",
254 ZFS_ERROR_CLASS, subclass);
255
256 fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
257 vd != NULL ? vd->vdev_guid : 0);
258
259 fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
260
261 /*
262 * Construct the per-ereport payload, depending on which parameters are
263 * passed in.
264 */
265
266 /*
267 * Generic payload members common to all ereports.
34dc7c2f 268 */
bcdb96a3
C
269 fm_payload_set(ereport,
270 FM_EREPORT_PAYLOAD_ZFS_POOL, DATA_TYPE_STRING, spa_name(spa),
271 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, DATA_TYPE_UINT64, spa_guid(spa),
177c91d0
DB
272 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, DATA_TYPE_UINT64,
273 (uint64_t)spa_state(spa),
34dc7c2f 274 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
177c91d0 275 (int32_t)spa_load_state(spa), NULL);
b128c09f 276
a36cc8d2 277 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
278 DATA_TYPE_STRING,
279 spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
280 FM_EREPORT_FAILMODE_WAIT :
281 spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
282 FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
283 NULL);
34dc7c2f
BB
284
285 if (vd != NULL) {
286 vdev_t *pvd = vd->vdev_parent;
cc92e9d0 287 vdev_queue_t *vq = &vd->vdev_queue;
904ea276
BB
288 vdev_stat_t *vs = &vd->vdev_stat;
289 vdev_t *spare_vd;
290 uint64_t *spare_guids;
291 char **spare_paths;
292 int i, spare_count;
34dc7c2f
BB
293
294 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
295 DATA_TYPE_UINT64, vd->vdev_guid,
296 FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
297 DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
9babb374 298 if (vd->vdev_path != NULL)
34dc7c2f
BB
299 fm_payload_set(ereport,
300 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
301 DATA_TYPE_STRING, vd->vdev_path, NULL);
9babb374 302 if (vd->vdev_devid != NULL)
34dc7c2f
BB
303 fm_payload_set(ereport,
304 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
305 DATA_TYPE_STRING, vd->vdev_devid, NULL);
9babb374
BB
306 if (vd->vdev_fru != NULL)
307 fm_payload_set(ereport,
308 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
309 DATA_TYPE_STRING, vd->vdev_fru, NULL);
6568379e
TH
310 if (vd->vdev_enc_sysfs_path != NULL)
311 fm_payload_set(ereport,
312 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
313 DATA_TYPE_STRING, vd->vdev_enc_sysfs_path, NULL);
32a9872b
GW
314 if (vd->vdev_ashift)
315 fm_payload_set(ereport,
316 FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
317 DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
34dc7c2f 318
cc92e9d0
GW
319 if (vq != NULL) {
320 fm_payload_set(ereport,
321 FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
322 DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
323 fm_payload_set(ereport,
324 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
325 DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
326 }
327
904ea276
BB
328 if (vs != NULL) {
329 fm_payload_set(ereport,
330 FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS,
331 DATA_TYPE_UINT64, vs->vs_read_errors,
332 FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS,
333 DATA_TYPE_UINT64, vs->vs_write_errors,
334 FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS,
335 DATA_TYPE_UINT64, vs->vs_checksum_errors, NULL);
336 }
337
34dc7c2f
BB
338 if (pvd != NULL) {
339 fm_payload_set(ereport,
340 FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
341 DATA_TYPE_UINT64, pvd->vdev_guid,
342 FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
343 DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
344 NULL);
345 if (pvd->vdev_path)
346 fm_payload_set(ereport,
347 FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
348 DATA_TYPE_STRING, pvd->vdev_path, NULL);
349 if (pvd->vdev_devid)
350 fm_payload_set(ereport,
351 FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
352 DATA_TYPE_STRING, pvd->vdev_devid, NULL);
353 }
904ea276
BB
354
355 spare_count = spa->spa_spares.sav_count;
356 spare_paths = kmem_zalloc(sizeof (char *) * spare_count,
79c76d5b 357 KM_SLEEP);
904ea276 358 spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count,
79c76d5b 359 KM_SLEEP);
904ea276
BB
360
361 for (i = 0; i < spare_count; i++) {
362 spare_vd = spa->spa_spares.sav_vdevs[i];
363 if (spare_vd) {
364 spare_paths[i] = spare_vd->vdev_path;
365 spare_guids[i] = spare_vd->vdev_guid;
366 }
367 }
368
369 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS,
370 DATA_TYPE_STRING_ARRAY, spare_count, spare_paths,
371 FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS,
372 DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL);
373
374 kmem_free(spare_guids, sizeof (uint64_t) * spare_count);
375 kmem_free(spare_paths, sizeof (char *) * spare_count);
34dc7c2f
BB
376 }
377
378 if (zio != NULL) {
379 /*
380 * Payload common to all I/Os.
381 */
382 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
383 DATA_TYPE_INT32, zio->io_error, NULL);
312c07ed
BB
384 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
385 DATA_TYPE_INT32, zio->io_flags, NULL);
9dcb9719
BB
386 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
387 DATA_TYPE_UINT32, zio->io_stage, NULL);
388 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
389 DATA_TYPE_UINT32, zio->io_pipeline, NULL);
a69052be
BB
390 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
391 DATA_TYPE_UINT64, zio->io_delay, NULL);
cc92e9d0
GW
392 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
393 DATA_TYPE_UINT64, zio->io_timestamp, NULL);
cc92e9d0
GW
394 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
395 DATA_TYPE_UINT64, zio->io_delta, NULL);
34dc7c2f
BB
396
397 /*
398 * If the 'size' parameter is non-zero, it indicates this is a
399 * RAID-Z or other I/O where the physical offset and length are
400 * provided for us, instead of within the zio_t.
401 */
402 if (vd != NULL) {
403 if (size)
404 fm_payload_set(ereport,
405 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
406 DATA_TYPE_UINT64, stateoroffset,
407 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
408 DATA_TYPE_UINT64, size, NULL);
409 else
410 fm_payload_set(ereport,
411 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
412 DATA_TYPE_UINT64, zio->io_offset,
413 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
414 DATA_TYPE_UINT64, zio->io_size, NULL);
415 }
34dc7c2f
BB
416 } else if (vd != NULL) {
417 /*
418 * If we have a vdev but no zio, this is a device fault, and the
419 * 'stateoroffset' parameter indicates the previous state of the
420 * vdev.
421 */
422 fm_payload_set(ereport,
423 FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
424 DATA_TYPE_UINT64, stateoroffset, NULL);
425 }
428870ff 426
b5256303
TC
427 /*
428 * Payload for I/Os with corresponding logical information.
429 */
430 if (zb != NULL && (zio == NULL || zio->io_logical != NULL))
431 fm_payload_set(ereport,
432 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
433 DATA_TYPE_UINT64, zb->zb_objset,
434 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
435 DATA_TYPE_UINT64, zb->zb_object,
436 FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
437 DATA_TYPE_INT64, zb->zb_level,
438 FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
439 DATA_TYPE_UINT64, zb->zb_blkid, NULL);
440
34dc7c2f
BB
441 mutex_exit(&spa->spa_errlist_lock);
442
428870ff
BB
443 *ereport_out = ereport;
444 *detector_out = detector;
445}
446
447/* if it's <= 128 bytes, save the corruption directly */
448#define ZFM_MAX_INLINE (128 / sizeof (uint64_t))
449
450#define MAX_RANGES 16
451
452typedef struct zfs_ecksum_info {
453 /* histograms of set and cleared bits by bit number in a 64-bit word */
1b18c6d7
AG
454 uint32_t zei_histogram_set[sizeof (uint64_t) * NBBY];
455 uint32_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
428870ff
BB
456
457 /* inline arrays of bits set and cleared. */
458 uint64_t zei_bits_set[ZFM_MAX_INLINE];
459 uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
460
461 /*
462 * for each range, the number of bits set and cleared. The Hamming
463 * distance between the good and bad buffers is the sum of them all.
464 */
465 uint32_t zei_range_sets[MAX_RANGES];
466 uint32_t zei_range_clears[MAX_RANGES];
467
468 struct zei_ranges {
469 uint32_t zr_start;
470 uint32_t zr_end;
471 } zei_ranges[MAX_RANGES];
472
473 size_t zei_range_count;
474 uint32_t zei_mingap;
475 uint32_t zei_allowed_mingap;
476
477} zfs_ecksum_info_t;
478
479static void
1b18c6d7 480update_histogram(uint64_t value_arg, uint32_t *hist, uint32_t *count)
428870ff
BB
481{
482 size_t i;
483 size_t bits = 0;
484 uint64_t value = BE_64(value_arg);
485
486 /* We store the bits in big-endian (largest-first) order */
487 for (i = 0; i < 64; i++) {
488 if (value & (1ull << i)) {
cf232b53 489 hist[63 - i]++;
428870ff
BB
490 ++bits;
491 }
492 }
493 /* update the count of bits changed */
494 *count += bits;
495}
496
497/*
498 * We've now filled up the range array, and need to increase "mingap" and
499 * shrink the range list accordingly. zei_mingap is always the smallest
500 * distance between array entries, so we set the new_allowed_gap to be
501 * one greater than that. We then go through the list, joining together
502 * any ranges which are closer than the new_allowed_gap.
503 *
504 * By construction, there will be at least one. We also update zei_mingap
505 * to the new smallest gap, to prepare for our next invocation.
506 */
507static void
26685276 508zei_shrink_ranges(zfs_ecksum_info_t *eip)
428870ff
BB
509{
510 uint32_t mingap = UINT32_MAX;
511 uint32_t new_allowed_gap = eip->zei_mingap + 1;
512
513 size_t idx, output;
514 size_t max = eip->zei_range_count;
515
516 struct zei_ranges *r = eip->zei_ranges;
517
518 ASSERT3U(eip->zei_range_count, >, 0);
519 ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
520
521 output = idx = 0;
522 while (idx < max - 1) {
523 uint32_t start = r[idx].zr_start;
524 uint32_t end = r[idx].zr_end;
525
526 while (idx < max - 1) {
26685276 527 idx++;
428870ff 528
1c27024e
DB
529 uint32_t nstart = r[idx].zr_start;
530 uint32_t nend = r[idx].zr_end;
531
532 uint32_t gap = nstart - end;
428870ff
BB
533 if (gap < new_allowed_gap) {
534 end = nend;
535 continue;
536 }
537 if (gap < mingap)
538 mingap = gap;
539 break;
540 }
541 r[output].zr_start = start;
542 r[output].zr_end = end;
543 output++;
544 }
545 ASSERT3U(output, <, eip->zei_range_count);
546 eip->zei_range_count = output;
547 eip->zei_mingap = mingap;
548 eip->zei_allowed_mingap = new_allowed_gap;
549}
550
551static void
26685276 552zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
428870ff
BB
553{
554 struct zei_ranges *r = eip->zei_ranges;
555 size_t count = eip->zei_range_count;
556
557 if (count >= MAX_RANGES) {
26685276 558 zei_shrink_ranges(eip);
428870ff
BB
559 count = eip->zei_range_count;
560 }
561 if (count == 0) {
562 eip->zei_mingap = UINT32_MAX;
563 eip->zei_allowed_mingap = 1;
564 } else {
565 int gap = start - r[count - 1].zr_end;
566
567 if (gap < eip->zei_allowed_mingap) {
568 r[count - 1].zr_end = end;
569 return;
570 }
571 if (gap < eip->zei_mingap)
572 eip->zei_mingap = gap;
573 }
574 r[count].zr_start = start;
575 r[count].zr_end = end;
576 eip->zei_range_count++;
577}
578
579static size_t
26685276 580zei_range_total_size(zfs_ecksum_info_t *eip)
428870ff
BB
581{
582 struct zei_ranges *r = eip->zei_ranges;
583 size_t count = eip->zei_range_count;
584 size_t result = 0;
585 size_t idx;
586
587 for (idx = 0; idx < count; idx++)
588 result += (r[idx].zr_end - r[idx].zr_start);
589
590 return (result);
591}
592
593static zfs_ecksum_info_t *
594annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
84c07ada 595 const abd_t *goodabd, const abd_t *badabd, size_t size,
428870ff
BB
596 boolean_t drop_if_identical)
597{
84c07ada
GN
598 const uint64_t *good;
599 const uint64_t *bad;
428870ff
BB
600
601 uint64_t allset = 0;
602 uint64_t allcleared = 0;
603
604 size_t nui64s = size / sizeof (uint64_t);
605
606 size_t inline_size;
607 int no_inline = 0;
608 size_t idx;
609 size_t range;
610
611 size_t offset = 0;
612 ssize_t start = -1;
613
79c76d5b 614 zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
428870ff
BB
615
616 /* don't do any annotation for injected checksum errors */
617 if (info != NULL && info->zbc_injected)
618 return (eip);
619
620 if (info != NULL && info->zbc_has_cksum) {
621 fm_payload_set(ereport,
622 FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
623 DATA_TYPE_UINT64_ARRAY,
624 sizeof (info->zbc_expected) / sizeof (uint64_t),
625 (uint64_t *)&info->zbc_expected,
626 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
627 DATA_TYPE_UINT64_ARRAY,
628 sizeof (info->zbc_actual) / sizeof (uint64_t),
629 (uint64_t *)&info->zbc_actual,
630 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
631 DATA_TYPE_STRING,
632 info->zbc_checksum_name,
633 NULL);
634
635 if (info->zbc_byteswapped) {
636 fm_payload_set(ereport,
637 FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
638 DATA_TYPE_BOOLEAN, 1,
639 NULL);
640 }
641 }
642
84c07ada 643 if (badabd == NULL || goodabd == NULL)
428870ff
BB
644 return (eip);
645
1b18c6d7 646 ASSERT3U(nui64s, <=, UINT32_MAX);
428870ff
BB
647 ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
648 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
649 ASSERT3U(size, <=, UINT32_MAX);
650
84c07ada
GN
651 good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
652 bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
653
428870ff
BB
654 /* build up the range list by comparing the two buffers. */
655 for (idx = 0; idx < nui64s; idx++) {
656 if (good[idx] == bad[idx]) {
657 if (start == -1)
658 continue;
659
26685276 660 zei_add_range(eip, start, idx);
428870ff
BB
661 start = -1;
662 } else {
663 if (start != -1)
664 continue;
665
666 start = idx;
667 }
668 }
669 if (start != -1)
26685276 670 zei_add_range(eip, start, idx);
428870ff
BB
671
672 /* See if it will fit in our inline buffers */
26685276 673 inline_size = zei_range_total_size(eip);
428870ff
BB
674 if (inline_size > ZFM_MAX_INLINE)
675 no_inline = 1;
676
677 /*
678 * If there is no change and we want to drop if the buffers are
679 * identical, do so.
680 */
681 if (inline_size == 0 && drop_if_identical) {
682 kmem_free(eip, sizeof (*eip));
84c07ada
GN
683 abd_return_buf((abd_t *)goodabd, (void *)good, size);
684 abd_return_buf((abd_t *)badabd, (void *)bad, size);
428870ff
BB
685 return (NULL);
686 }
687
688 /*
689 * Now walk through the ranges, filling in the details of the
690 * differences. Also convert our uint64_t-array offsets to byte
691 * offsets.
692 */
693 for (range = 0; range < eip->zei_range_count; range++) {
694 size_t start = eip->zei_ranges[range].zr_start;
695 size_t end = eip->zei_ranges[range].zr_end;
696
697 for (idx = start; idx < end; idx++) {
698 uint64_t set, cleared;
699
700 // bits set in bad, but not in good
701 set = ((~good[idx]) & bad[idx]);
702 // bits set in good, but not in bad
703 cleared = (good[idx] & (~bad[idx]));
704
705 allset |= set;
706 allcleared |= cleared;
707
708 if (!no_inline) {
709 ASSERT3U(offset, <, inline_size);
710 eip->zei_bits_set[offset] = set;
711 eip->zei_bits_cleared[offset] = cleared;
712 offset++;
713 }
714
715 update_histogram(set, eip->zei_histogram_set,
716 &eip->zei_range_sets[range]);
717 update_histogram(cleared, eip->zei_histogram_cleared,
718 &eip->zei_range_clears[range]);
719 }
720
721 /* convert to byte offsets */
722 eip->zei_ranges[range].zr_start *= sizeof (uint64_t);
723 eip->zei_ranges[range].zr_end *= sizeof (uint64_t);
724 }
84c07ada
GN
725
726 abd_return_buf((abd_t *)goodabd, (void *)good, size);
727 abd_return_buf((abd_t *)badabd, (void *)bad, size);
728
428870ff
BB
729 eip->zei_allowed_mingap *= sizeof (uint64_t);
730 inline_size *= sizeof (uint64_t);
731
732 /* fill in ereport */
733 fm_payload_set(ereport,
734 FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
735 DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
736 (uint32_t *)eip->zei_ranges,
737 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
738 DATA_TYPE_UINT32, eip->zei_allowed_mingap,
739 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
740 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
741 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
742 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
743 NULL);
744
745 if (!no_inline) {
746 fm_payload_set(ereport,
747 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
748 DATA_TYPE_UINT8_ARRAY,
749 inline_size, (uint8_t *)eip->zei_bits_set,
750 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
751 DATA_TYPE_UINT8_ARRAY,
752 inline_size, (uint8_t *)eip->zei_bits_cleared,
753 NULL);
754 } else {
755 fm_payload_set(ereport,
756 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
1b18c6d7 757 DATA_TYPE_UINT32_ARRAY,
428870ff
BB
758 NBBY * sizeof (uint64_t), eip->zei_histogram_set,
759 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
1b18c6d7 760 DATA_TYPE_UINT32_ARRAY,
428870ff
BB
761 NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
762 NULL);
763 }
764 return (eip);
765}
766#endif
767
768void
b5256303 769zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd,
a2c2ed1b
TC
770 const zbookmark_phys_t *zb, zio_t *zio, uint64_t stateoroffset,
771 uint64_t size)
428870ff
BB
772{
773#ifdef _KERNEL
774 nvlist_t *ereport = NULL;
775 nvlist_t *detector = NULL;
776
17b43f96
GDN
777 if (zfs_is_ratelimiting_event(subclass, vd))
778 return;
779
b5256303
TC
780 zfs_ereport_start(&ereport, &detector, subclass, spa, vd,
781 zb, zio, stateoroffset, size);
428870ff
BB
782
783 if (ereport == NULL)
784 return;
785
26685276
BB
786 /* Cleanup is handled by the callback function */
787 zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
34dc7c2f
BB
788#endif
789}
790
428870ff 791void
a2c2ed1b 792zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
428870ff
BB
793 struct zio *zio, uint64_t offset, uint64_t length, void *arg,
794 zio_bad_cksum_t *info)
795{
6078881a
TH
796 zio_cksum_report_t *report;
797
798
799#ifdef _KERNEL
800 if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
801 return;
802#endif
803
804 report = kmem_zalloc(sizeof (*report), KM_SLEEP);
428870ff
BB
805
806 if (zio->io_vsd != NULL)
807 zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
808 else
809 zio_vsd_default_cksum_report(zio, report, arg);
810
811 /* copy the checksum failure information if it was provided */
812 if (info != NULL) {
79c76d5b 813 report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
428870ff
BB
814 bcopy(info, report->zcr_ckinfo, sizeof (*info));
815 }
816
817 report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
818 report->zcr_length = length;
819
820#ifdef _KERNEL
821 zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
b5256303 822 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length);
428870ff
BB
823
824 if (report->zcr_ereport == NULL) {
0426c168 825 zfs_ereport_free_checksum(report);
428870ff
BB
826 return;
827 }
828#endif
829
830 mutex_enter(&spa->spa_errlist_lock);
831 report->zcr_next = zio->io_logical->io_cksum_report;
832 zio->io_logical->io_cksum_report = report;
833 mutex_exit(&spa->spa_errlist_lock);
834}
835
836void
84c07ada
GN
837zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data,
838 const abd_t *bad_data, boolean_t drop_if_identical)
428870ff
BB
839{
840#ifdef _KERNEL
0426c168
IH
841 zfs_ecksum_info_t *info;
842
428870ff
BB
843 info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
844 good_data, bad_data, report->zcr_length, drop_if_identical);
428870ff 845 if (info != NULL)
26685276
BB
846 zfs_zevent_post(report->zcr_ereport,
847 report->zcr_detector, zfs_zevent_post_cb);
0426c168
IH
848 else
849 zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector);
428870ff 850
428870ff 851 report->zcr_ereport = report->zcr_detector = NULL;
428870ff
BB
852 if (info != NULL)
853 kmem_free(info, sizeof (*info));
854#endif
855}
856
857void
858zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
859{
860#ifdef _KERNEL
861 if (rpt->zcr_ereport != NULL) {
862 fm_nvlist_destroy(rpt->zcr_ereport,
863 FM_NVA_FREE);
864 fm_nvlist_destroy(rpt->zcr_detector,
865 FM_NVA_FREE);
866 }
867#endif
868 rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
869
870 if (rpt->zcr_ckinfo != NULL)
871 kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
872
873 kmem_free(rpt, sizeof (*rpt));
874}
875
428870ff
BB
876
877void
a2c2ed1b 878zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
428870ff 879 struct zio *zio, uint64_t offset, uint64_t length,
84c07ada 880 const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc)
428870ff
BB
881{
882#ifdef _KERNEL
883 nvlist_t *ereport = NULL;
884 nvlist_t *detector = NULL;
885 zfs_ecksum_info_t *info;
886
b5256303
TC
887 zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM,
888 spa, vd, zb, zio, offset, length);
428870ff
BB
889
890 if (ereport == NULL)
891 return;
892
893 info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
894 B_FALSE);
895
26685276
BB
896 if (info != NULL) {
897 zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
428870ff 898 kmem_free(info, sizeof (*info));
26685276 899 }
428870ff
BB
900#endif
901}
902
12fa0466
DE
903/*
904 * The 'sysevent.fs.zfs.*' events are signals posted to notify user space of
905 * change in the pool. All sysevents are listed in sys/sysevent/eventdefs.h
906 * and are designed to be consumed by the ZFS Event Daemon (ZED). For
907 * additional details refer to the zed(8) man page.
908 */
909nvlist_t *
910zfs_event_create(spa_t *spa, vdev_t *vd, const char *type, const char *name,
d02ca379 911 nvlist_t *aux)
34dc7c2f 912{
12fa0466 913 nvlist_t *resource = NULL;
34dc7c2f 914#ifdef _KERNEL
34dc7c2f
BB
915 char class[64];
916
428870ff 917 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
12fa0466 918 return (NULL);
428870ff 919
34dc7c2f 920 if ((resource = fm_nvlist_create(NULL)) == NULL)
12fa0466 921 return (NULL);
34dc7c2f 922
fb390aaf 923 (void) snprintf(class, sizeof (class), "%s.%s.%s", type,
34dc7c2f 924 ZFS_ERROR_CLASS, name);
904ea276
BB
925 VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION));
926 VERIFY0(nvlist_add_string(resource, FM_CLASS, class));
bcdb96a3
C
927 VERIFY0(nvlist_add_string(resource,
928 FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa)));
904ea276
BB
929 VERIFY0(nvlist_add_uint64(resource,
930 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)));
bcdb96a3
C
931 VERIFY0(nvlist_add_uint64(resource,
932 FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, spa_state(spa)));
904ea276
BB
933 VERIFY0(nvlist_add_int32(resource,
934 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa)));
935
26685276 936 if (vd) {
904ea276
BB
937 VERIFY0(nvlist_add_uint64(resource,
938 FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid));
939 VERIFY0(nvlist_add_uint64(resource,
940 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state));
fb390aaf
HR
941 if (vd->vdev_path != NULL)
942 VERIFY0(nvlist_add_string(resource,
943 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path));
944 if (vd->vdev_devid != NULL)
945 VERIFY0(nvlist_add_string(resource,
946 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid));
947 if (vd->vdev_fru != NULL)
948 VERIFY0(nvlist_add_string(resource,
949 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru));
6568379e
TH
950 if (vd->vdev_enc_sysfs_path != NULL)
951 VERIFY0(nvlist_add_string(resource,
952 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
953 vd->vdev_enc_sysfs_path));
12fa0466 954 }
d02ca379 955
12fa0466
DE
956 /* also copy any optional payload data */
957 if (aux) {
958 nvpair_t *elem = NULL;
959
960 while ((elem = nvlist_next_nvpair(aux, elem)) != NULL)
961 (void) nvlist_add_nvpair(resource, elem);
26685276 962 }
34dc7c2f 963
12fa0466
DE
964#endif
965 return (resource);
966}
967
968static void
969zfs_post_common(spa_t *spa, vdev_t *vd, const char *type, const char *name,
970 nvlist_t *aux)
971{
972#ifdef _KERNEL
973 nvlist_t *resource;
974
975 resource = zfs_event_create(spa, vd, type, name, aux);
976 if (resource)
977 zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
34dc7c2f
BB
978#endif
979}
980
34dc7c2f
BB
981/*
982 * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
983 * has been removed from the system. This will cause the DE to ignore any
984 * recent I/O errors, inferring that they are due to the asynchronous device
985 * removal.
986 */
987void
988zfs_post_remove(spa_t *spa, vdev_t *vd)
989{
d02ca379 990 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_REMOVED, NULL);
34dc7c2f
BB
991}
992
993/*
994 * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
995 * has the 'autoreplace' property set, and therefore any broken vdevs will be
996 * handled by higher level logic, and no vdev fault should be generated.
997 */
998void
999zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
1000{
d02ca379 1001 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_AUTOREPLACE, NULL);
34dc7c2f 1002}
428870ff
BB
1003
1004/*
1005 * The 'resource.fs.zfs.statechange' event is an internal signal that the
1006 * given vdev has transitioned its state to DEGRADED or HEALTHY. This will
1007 * cause the retire agent to repair any outstanding fault management cases
1008 * open because the device was not found (fault.fs.zfs.device).
1009 */
1010void
d02ca379 1011zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate)
428870ff 1012{
d02ca379
DB
1013#ifdef _KERNEL
1014 nvlist_t *aux;
1015
1016 /*
1017 * Add optional supplemental keys to payload
1018 */
1019 aux = fm_nvlist_create(NULL);
1020 if (vd && aux) {
1021 if (vd->vdev_physpath) {
1022 (void) nvlist_add_string(aux,
1023 FM_EREPORT_PAYLOAD_ZFS_VDEV_PHYSPATH,
1024 vd->vdev_physpath);
1025 }
1bbd8770
TH
1026 if (vd->vdev_enc_sysfs_path) {
1027 (void) nvlist_add_string(aux,
1028 FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1029 vd->vdev_enc_sysfs_path);
1030 }
1031
d02ca379
DB
1032 (void) nvlist_add_uint64(aux,
1033 FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE, laststate);
1034 }
1035
1036 zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_STATECHANGE,
1037 aux);
1038
1039 if (aux)
1040 fm_nvlist_destroy(aux, FM_NVA_FREE);
1041#endif
fb390aaf
HR
1042}
1043
93ce2b4c 1044#if defined(_KERNEL)
26685276
BB
1045EXPORT_SYMBOL(zfs_ereport_post);
1046EXPORT_SYMBOL(zfs_ereport_post_checksum);
1047EXPORT_SYMBOL(zfs_post_remove);
1048EXPORT_SYMBOL(zfs_post_autoreplace);
1049EXPORT_SYMBOL(zfs_post_state_change);
1050#endif /* _KERNEL */