]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_fm.c
Add missing DATA_TYPE_STRING_ARRAY output
[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
26685276
BB
105static void
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
428870ff
BB
115static void
116zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
117 const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
34dc7c2f
BB
118 uint64_t stateoroffset, uint64_t size)
119{
34dc7c2f 120 nvlist_t *ereport, *detector;
428870ff 121
34dc7c2f
BB
122 uint64_t ena;
123 char class[64];
124
125 /*
428870ff
BB
126 * If we are doing a spa_tryimport() or in recovery mode,
127 * ignore errors.
34dc7c2f 128 */
428870ff
BB
129 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
130 spa_load_state(spa) == SPA_LOAD_RECOVER)
34dc7c2f
BB
131 return;
132
133 /*
134 * If we are in the middle of opening a pool, and the previous attempt
135 * failed, don't bother logging any new ereports - we're just going to
136 * get the same diagnosis anyway.
137 */
428870ff 138 if (spa_load_state(spa) != SPA_LOAD_NONE &&
34dc7c2f
BB
139 spa->spa_last_open_failed)
140 return;
141
b128c09f
BB
142 if (zio != NULL) {
143 /*
144 * If this is not a read or write zio, ignore the error. This
145 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
146 */
147 if (zio->io_type != ZIO_TYPE_READ &&
148 zio->io_type != ZIO_TYPE_WRITE)
149 return;
34dc7c2f 150
9babb374
BB
151 if (vd != NULL) {
152 /*
153 * If the vdev has already been marked as failing due
154 * to a failed probe, then ignore any subsequent I/O
155 * errors, as the DE will automatically fault the vdev
156 * on the first such failure. This also catches cases
157 * where vdev_remove_wanted is set and the device has
158 * not yet been asynchronously placed into the REMOVED
159 * state.
160 */
428870ff 161 if (zio->io_vd == vd && !vdev_accessible(vd, zio))
9babb374
BB
162 return;
163
164 /*
165 * Ignore checksum errors for reads from DTL regions of
166 * leaf vdevs.
167 */
168 if (zio->io_type == ZIO_TYPE_READ &&
169 zio->io_error == ECKSUM &&
170 vd->vdev_ops->vdev_op_leaf &&
171 vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
172 return;
173 }
b128c09f 174 }
34dc7c2f 175
428870ff
BB
176 /*
177 * For probe failure, we want to avoid posting ereports if we've
178 * already removed the device in the meantime.
179 */
180 if (vd != NULL &&
181 strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
182 (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
183 return;
184
34dc7c2f
BB
185 if ((ereport = fm_nvlist_create(NULL)) == NULL)
186 return;
187
188 if ((detector = fm_nvlist_create(NULL)) == NULL) {
189 fm_nvlist_destroy(ereport, FM_NVA_FREE);
190 return;
191 }
192
193 /*
194 * Serialize ereport generation
195 */
196 mutex_enter(&spa->spa_errlist_lock);
197
198 /*
199 * Determine the ENA to use for this event. If we are in a loading
200 * state, use a SPA-wide ENA. Otherwise, if we are in an I/O state, use
201 * a root zio-wide ENA. Otherwise, simply use a unique ENA.
202 */
428870ff 203 if (spa_load_state(spa) != SPA_LOAD_NONE) {
34dc7c2f
BB
204 if (spa->spa_ena == 0)
205 spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
206 ena = spa->spa_ena;
207 } else if (zio != NULL && zio->io_logical != NULL) {
208 if (zio->io_logical->io_ena == 0)
209 zio->io_logical->io_ena =
210 fm_ena_generate(0, FM_ENA_FMT1);
211 ena = zio->io_logical->io_ena;
212 } else {
213 ena = fm_ena_generate(0, FM_ENA_FMT1);
214 }
215
216 /*
217 * Construct the full class, detector, and other standard FMA fields.
218 */
219 (void) snprintf(class, sizeof (class), "%s.%s",
220 ZFS_ERROR_CLASS, subclass);
221
222 fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
223 vd != NULL ? vd->vdev_guid : 0);
224
225 fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
226
227 /*
228 * Construct the per-ereport payload, depending on which parameters are
229 * passed in.
230 */
231
232 /*
233 * Generic payload members common to all ereports.
34dc7c2f
BB
234 */
235 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
b128c09f 236 DATA_TYPE_STRING, spa_name(spa), FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
34dc7c2f
BB
237 DATA_TYPE_UINT64, spa_guid(spa),
238 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
428870ff 239 spa_load_state(spa), NULL);
b128c09f
BB
240
241 if (spa != NULL) {
242 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
243 DATA_TYPE_STRING,
244 spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
245 FM_EREPORT_FAILMODE_WAIT :
246 spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
247 FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
248 NULL);
249 }
34dc7c2f
BB
250
251 if (vd != NULL) {
252 vdev_t *pvd = vd->vdev_parent;
cc92e9d0 253 vdev_queue_t *vq = &vd->vdev_queue;
34dc7c2f
BB
254
255 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
256 DATA_TYPE_UINT64, vd->vdev_guid,
257 FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
258 DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
9babb374 259 if (vd->vdev_path != NULL)
34dc7c2f
BB
260 fm_payload_set(ereport,
261 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
262 DATA_TYPE_STRING, vd->vdev_path, NULL);
9babb374 263 if (vd->vdev_devid != NULL)
34dc7c2f
BB
264 fm_payload_set(ereport,
265 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
266 DATA_TYPE_STRING, vd->vdev_devid, NULL);
9babb374
BB
267 if (vd->vdev_fru != NULL)
268 fm_payload_set(ereport,
269 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
270 DATA_TYPE_STRING, vd->vdev_fru, NULL);
32a9872b
GW
271 if (vd->vdev_ashift)
272 fm_payload_set(ereport,
273 FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
274 DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
34dc7c2f 275
cc92e9d0
GW
276 if (vq != NULL) {
277 fm_payload_set(ereport,
278 FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
279 DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
280 fm_payload_set(ereport,
281 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
282 DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
283 }
284
34dc7c2f
BB
285 if (pvd != NULL) {
286 fm_payload_set(ereport,
287 FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
288 DATA_TYPE_UINT64, pvd->vdev_guid,
289 FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
290 DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
291 NULL);
292 if (pvd->vdev_path)
293 fm_payload_set(ereport,
294 FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
295 DATA_TYPE_STRING, pvd->vdev_path, NULL);
296 if (pvd->vdev_devid)
297 fm_payload_set(ereport,
298 FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
299 DATA_TYPE_STRING, pvd->vdev_devid, NULL);
300 }
301 }
302
303 if (zio != NULL) {
304 /*
305 * Payload common to all I/Os.
306 */
307 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
308 DATA_TYPE_INT32, zio->io_error, NULL);
312c07ed
BB
309 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
310 DATA_TYPE_INT32, zio->io_flags, NULL);
9dcb9719
BB
311 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
312 DATA_TYPE_UINT32, zio->io_stage, NULL);
313 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
314 DATA_TYPE_UINT32, zio->io_pipeline, NULL);
a69052be
BB
315 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
316 DATA_TYPE_UINT64, zio->io_delay, NULL);
cc92e9d0
GW
317 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
318 DATA_TYPE_UINT64, zio->io_timestamp, NULL);
cc92e9d0
GW
319 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
320 DATA_TYPE_UINT64, zio->io_delta, NULL);
34dc7c2f
BB
321
322 /*
323 * If the 'size' parameter is non-zero, it indicates this is a
324 * RAID-Z or other I/O where the physical offset and length are
325 * provided for us, instead of within the zio_t.
326 */
327 if (vd != NULL) {
328 if (size)
329 fm_payload_set(ereport,
330 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
331 DATA_TYPE_UINT64, stateoroffset,
332 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
333 DATA_TYPE_UINT64, size, NULL);
334 else
335 fm_payload_set(ereport,
336 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
337 DATA_TYPE_UINT64, zio->io_offset,
338 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
339 DATA_TYPE_UINT64, zio->io_size, NULL);
340 }
341
342 /*
343 * Payload for I/Os with corresponding logical information.
344 */
345 if (zio->io_logical != NULL)
346 fm_payload_set(ereport,
347 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
348 DATA_TYPE_UINT64,
349 zio->io_logical->io_bookmark.zb_objset,
350 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
351 DATA_TYPE_UINT64,
352 zio->io_logical->io_bookmark.zb_object,
353 FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
354 DATA_TYPE_INT64,
355 zio->io_logical->io_bookmark.zb_level,
356 FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
357 DATA_TYPE_UINT64,
358 zio->io_logical->io_bookmark.zb_blkid, NULL);
359 } else if (vd != NULL) {
360 /*
361 * If we have a vdev but no zio, this is a device fault, and the
362 * 'stateoroffset' parameter indicates the previous state of the
363 * vdev.
364 */
365 fm_payload_set(ereport,
366 FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
367 DATA_TYPE_UINT64, stateoroffset, NULL);
368 }
428870ff 369
34dc7c2f
BB
370 mutex_exit(&spa->spa_errlist_lock);
371
428870ff
BB
372 *ereport_out = ereport;
373 *detector_out = detector;
374}
375
376/* if it's <= 128 bytes, save the corruption directly */
377#define ZFM_MAX_INLINE (128 / sizeof (uint64_t))
378
379#define MAX_RANGES 16
380
381typedef struct zfs_ecksum_info {
382 /* histograms of set and cleared bits by bit number in a 64-bit word */
383 uint16_t zei_histogram_set[sizeof (uint64_t) * NBBY];
384 uint16_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
385
386 /* inline arrays of bits set and cleared. */
387 uint64_t zei_bits_set[ZFM_MAX_INLINE];
388 uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
389
390 /*
391 * for each range, the number of bits set and cleared. The Hamming
392 * distance between the good and bad buffers is the sum of them all.
393 */
394 uint32_t zei_range_sets[MAX_RANGES];
395 uint32_t zei_range_clears[MAX_RANGES];
396
397 struct zei_ranges {
398 uint32_t zr_start;
399 uint32_t zr_end;
400 } zei_ranges[MAX_RANGES];
401
402 size_t zei_range_count;
403 uint32_t zei_mingap;
404 uint32_t zei_allowed_mingap;
405
406} zfs_ecksum_info_t;
407
408static void
409update_histogram(uint64_t value_arg, uint16_t *hist, uint32_t *count)
410{
411 size_t i;
412 size_t bits = 0;
413 uint64_t value = BE_64(value_arg);
414
415 /* We store the bits in big-endian (largest-first) order */
416 for (i = 0; i < 64; i++) {
417 if (value & (1ull << i)) {
418 hist[63 - i]++;
419 ++bits;
420 }
421 }
422 /* update the count of bits changed */
423 *count += bits;
424}
425
426/*
427 * We've now filled up the range array, and need to increase "mingap" and
428 * shrink the range list accordingly. zei_mingap is always the smallest
429 * distance between array entries, so we set the new_allowed_gap to be
430 * one greater than that. We then go through the list, joining together
431 * any ranges which are closer than the new_allowed_gap.
432 *
433 * By construction, there will be at least one. We also update zei_mingap
434 * to the new smallest gap, to prepare for our next invocation.
435 */
436static void
26685276 437zei_shrink_ranges(zfs_ecksum_info_t *eip)
428870ff
BB
438{
439 uint32_t mingap = UINT32_MAX;
440 uint32_t new_allowed_gap = eip->zei_mingap + 1;
441
442 size_t idx, output;
443 size_t max = eip->zei_range_count;
444
445 struct zei_ranges *r = eip->zei_ranges;
446
447 ASSERT3U(eip->zei_range_count, >, 0);
448 ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
449
450 output = idx = 0;
451 while (idx < max - 1) {
452 uint32_t start = r[idx].zr_start;
453 uint32_t end = r[idx].zr_end;
454
455 while (idx < max - 1) {
26685276 456 uint32_t nstart, nend, gap;
428870ff 457
26685276
BB
458 idx++;
459 nstart = r[idx].zr_start;
460 nend = r[idx].zr_end;
428870ff 461
26685276 462 gap = nstart - end;
428870ff
BB
463 if (gap < new_allowed_gap) {
464 end = nend;
465 continue;
466 }
467 if (gap < mingap)
468 mingap = gap;
469 break;
470 }
471 r[output].zr_start = start;
472 r[output].zr_end = end;
473 output++;
474 }
475 ASSERT3U(output, <, eip->zei_range_count);
476 eip->zei_range_count = output;
477 eip->zei_mingap = mingap;
478 eip->zei_allowed_mingap = new_allowed_gap;
479}
480
481static void
26685276 482zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
428870ff
BB
483{
484 struct zei_ranges *r = eip->zei_ranges;
485 size_t count = eip->zei_range_count;
486
487 if (count >= MAX_RANGES) {
26685276 488 zei_shrink_ranges(eip);
428870ff
BB
489 count = eip->zei_range_count;
490 }
491 if (count == 0) {
492 eip->zei_mingap = UINT32_MAX;
493 eip->zei_allowed_mingap = 1;
494 } else {
495 int gap = start - r[count - 1].zr_end;
496
497 if (gap < eip->zei_allowed_mingap) {
498 r[count - 1].zr_end = end;
499 return;
500 }
501 if (gap < eip->zei_mingap)
502 eip->zei_mingap = gap;
503 }
504 r[count].zr_start = start;
505 r[count].zr_end = end;
506 eip->zei_range_count++;
507}
508
509static size_t
26685276 510zei_range_total_size(zfs_ecksum_info_t *eip)
428870ff
BB
511{
512 struct zei_ranges *r = eip->zei_ranges;
513 size_t count = eip->zei_range_count;
514 size_t result = 0;
515 size_t idx;
516
517 for (idx = 0; idx < count; idx++)
518 result += (r[idx].zr_end - r[idx].zr_start);
519
520 return (result);
521}
522
523static zfs_ecksum_info_t *
524annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
525 const uint8_t *goodbuf, const uint8_t *badbuf, size_t size,
526 boolean_t drop_if_identical)
527{
528 const uint64_t *good = (const uint64_t *)goodbuf;
529 const uint64_t *bad = (const uint64_t *)badbuf;
530
531 uint64_t allset = 0;
532 uint64_t allcleared = 0;
533
534 size_t nui64s = size / sizeof (uint64_t);
535
536 size_t inline_size;
537 int no_inline = 0;
538 size_t idx;
539 size_t range;
540
541 size_t offset = 0;
542 ssize_t start = -1;
543
b8d06fca 544 zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_PUSHPAGE);
428870ff
BB
545
546 /* don't do any annotation for injected checksum errors */
547 if (info != NULL && info->zbc_injected)
548 return (eip);
549
550 if (info != NULL && info->zbc_has_cksum) {
551 fm_payload_set(ereport,
552 FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
553 DATA_TYPE_UINT64_ARRAY,
554 sizeof (info->zbc_expected) / sizeof (uint64_t),
555 (uint64_t *)&info->zbc_expected,
556 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
557 DATA_TYPE_UINT64_ARRAY,
558 sizeof (info->zbc_actual) / sizeof (uint64_t),
559 (uint64_t *)&info->zbc_actual,
560 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
561 DATA_TYPE_STRING,
562 info->zbc_checksum_name,
563 NULL);
564
565 if (info->zbc_byteswapped) {
566 fm_payload_set(ereport,
567 FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
568 DATA_TYPE_BOOLEAN, 1,
569 NULL);
570 }
571 }
572
573 if (badbuf == NULL || goodbuf == NULL)
574 return (eip);
575
576 ASSERT3U(nui64s, <=, UINT16_MAX);
577 ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
578 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
579 ASSERT3U(size, <=, UINT32_MAX);
580
581 /* build up the range list by comparing the two buffers. */
582 for (idx = 0; idx < nui64s; idx++) {
583 if (good[idx] == bad[idx]) {
584 if (start == -1)
585 continue;
586
26685276 587 zei_add_range(eip, start, idx);
428870ff
BB
588 start = -1;
589 } else {
590 if (start != -1)
591 continue;
592
593 start = idx;
594 }
595 }
596 if (start != -1)
26685276 597 zei_add_range(eip, start, idx);
428870ff
BB
598
599 /* See if it will fit in our inline buffers */
26685276 600 inline_size = zei_range_total_size(eip);
428870ff
BB
601 if (inline_size > ZFM_MAX_INLINE)
602 no_inline = 1;
603
604 /*
605 * If there is no change and we want to drop if the buffers are
606 * identical, do so.
607 */
608 if (inline_size == 0 && drop_if_identical) {
609 kmem_free(eip, sizeof (*eip));
610 return (NULL);
611 }
612
613 /*
614 * Now walk through the ranges, filling in the details of the
615 * differences. Also convert our uint64_t-array offsets to byte
616 * offsets.
617 */
618 for (range = 0; range < eip->zei_range_count; range++) {
619 size_t start = eip->zei_ranges[range].zr_start;
620 size_t end = eip->zei_ranges[range].zr_end;
621
622 for (idx = start; idx < end; idx++) {
623 uint64_t set, cleared;
624
625 // bits set in bad, but not in good
626 set = ((~good[idx]) & bad[idx]);
627 // bits set in good, but not in bad
628 cleared = (good[idx] & (~bad[idx]));
629
630 allset |= set;
631 allcleared |= cleared;
632
633 if (!no_inline) {
634 ASSERT3U(offset, <, inline_size);
635 eip->zei_bits_set[offset] = set;
636 eip->zei_bits_cleared[offset] = cleared;
637 offset++;
638 }
639
640 update_histogram(set, eip->zei_histogram_set,
641 &eip->zei_range_sets[range]);
642 update_histogram(cleared, eip->zei_histogram_cleared,
643 &eip->zei_range_clears[range]);
644 }
645
646 /* convert to byte offsets */
647 eip->zei_ranges[range].zr_start *= sizeof (uint64_t);
648 eip->zei_ranges[range].zr_end *= sizeof (uint64_t);
649 }
650 eip->zei_allowed_mingap *= sizeof (uint64_t);
651 inline_size *= sizeof (uint64_t);
652
653 /* fill in ereport */
654 fm_payload_set(ereport,
655 FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
656 DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
657 (uint32_t *)eip->zei_ranges,
658 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
659 DATA_TYPE_UINT32, eip->zei_allowed_mingap,
660 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
661 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
662 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
663 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
664 NULL);
665
666 if (!no_inline) {
667 fm_payload_set(ereport,
668 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
669 DATA_TYPE_UINT8_ARRAY,
670 inline_size, (uint8_t *)eip->zei_bits_set,
671 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
672 DATA_TYPE_UINT8_ARRAY,
673 inline_size, (uint8_t *)eip->zei_bits_cleared,
674 NULL);
675 } else {
676 fm_payload_set(ereport,
677 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
678 DATA_TYPE_UINT16_ARRAY,
679 NBBY * sizeof (uint64_t), eip->zei_histogram_set,
680 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
681 DATA_TYPE_UINT16_ARRAY,
682 NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
683 NULL);
684 }
685 return (eip);
686}
687#endif
688
689void
690zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
691 uint64_t stateoroffset, uint64_t size)
692{
693#ifdef _KERNEL
694 nvlist_t *ereport = NULL;
695 nvlist_t *detector = NULL;
696
697 zfs_ereport_start(&ereport, &detector,
698 subclass, spa, vd, zio, stateoroffset, size);
699
700 if (ereport == NULL)
701 return;
702
26685276
BB
703 /* Cleanup is handled by the callback function */
704 zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
34dc7c2f
BB
705#endif
706}
707
428870ff
BB
708void
709zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd,
710 struct zio *zio, uint64_t offset, uint64_t length, void *arg,
711 zio_bad_cksum_t *info)
712{
b8d06fca 713 zio_cksum_report_t *report = kmem_zalloc(sizeof (*report), KM_PUSHPAGE);
428870ff
BB
714
715 if (zio->io_vsd != NULL)
716 zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
717 else
718 zio_vsd_default_cksum_report(zio, report, arg);
719
720 /* copy the checksum failure information if it was provided */
721 if (info != NULL) {
b8d06fca 722 report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_PUSHPAGE);
428870ff
BB
723 bcopy(info, report->zcr_ckinfo, sizeof (*info));
724 }
725
726 report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
727 report->zcr_length = length;
728
729#ifdef _KERNEL
730 zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
731 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
732
733 if (report->zcr_ereport == NULL) {
734 report->zcr_free(report->zcr_cbdata, report->zcr_cbinfo);
5ffb9d1d
GW
735 if (report->zcr_ckinfo != NULL) {
736 kmem_free(report->zcr_ckinfo,
737 sizeof (*report->zcr_ckinfo));
738 }
428870ff
BB
739 kmem_free(report, sizeof (*report));
740 return;
741 }
742#endif
743
744 mutex_enter(&spa->spa_errlist_lock);
745 report->zcr_next = zio->io_logical->io_cksum_report;
746 zio->io_logical->io_cksum_report = report;
747 mutex_exit(&spa->spa_errlist_lock);
748}
749
750void
751zfs_ereport_finish_checksum(zio_cksum_report_t *report,
752 const void *good_data, const void *bad_data, boolean_t drop_if_identical)
753{
754#ifdef _KERNEL
755 zfs_ecksum_info_t *info = NULL;
756 info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
757 good_data, bad_data, report->zcr_length, drop_if_identical);
758
759 if (info != NULL)
26685276
BB
760 zfs_zevent_post(report->zcr_ereport,
761 report->zcr_detector, zfs_zevent_post_cb);
428870ff 762
428870ff 763 report->zcr_ereport = report->zcr_detector = NULL;
428870ff
BB
764 if (info != NULL)
765 kmem_free(info, sizeof (*info));
766#endif
767}
768
769void
770zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
771{
772#ifdef _KERNEL
773 if (rpt->zcr_ereport != NULL) {
774 fm_nvlist_destroy(rpt->zcr_ereport,
775 FM_NVA_FREE);
776 fm_nvlist_destroy(rpt->zcr_detector,
777 FM_NVA_FREE);
778 }
779#endif
780 rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
781
782 if (rpt->zcr_ckinfo != NULL)
783 kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
784
785 kmem_free(rpt, sizeof (*rpt));
786}
787
788void
789zfs_ereport_send_interim_checksum(zio_cksum_report_t *report)
790{
791#ifdef _KERNEL
26685276 792 zfs_zevent_post(report->zcr_ereport, report->zcr_detector, NULL);
428870ff
BB
793#endif
794}
795
796void
797zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd,
798 struct zio *zio, uint64_t offset, uint64_t length,
799 const void *good_data, const void *bad_data, zio_bad_cksum_t *zbc)
800{
801#ifdef _KERNEL
802 nvlist_t *ereport = NULL;
803 nvlist_t *detector = NULL;
804 zfs_ecksum_info_t *info;
805
806 zfs_ereport_start(&ereport, &detector,
807 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
808
809 if (ereport == NULL)
810 return;
811
812 info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
813 B_FALSE);
814
26685276
BB
815 if (info != NULL) {
816 zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
428870ff 817 kmem_free(info, sizeof (*info));
26685276 818 }
428870ff
BB
819#endif
820}
821
34dc7c2f
BB
822static void
823zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
824{
825#ifdef _KERNEL
826 nvlist_t *resource;
827 char class[64];
828
428870ff
BB
829 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
830 return;
831
34dc7c2f
BB
832 if ((resource = fm_nvlist_create(NULL)) == NULL)
833 return;
834
835 (void) snprintf(class, sizeof (class), "%s.%s.%s", FM_RSRC_RESOURCE,
836 ZFS_ERROR_CLASS, name);
837 VERIFY(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION) == 0);
838 VERIFY(nvlist_add_string(resource, FM_CLASS, class) == 0);
839 VERIFY(nvlist_add_uint64(resource,
840 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)) == 0);
26685276 841 if (vd) {
34dc7c2f
BB
842 VERIFY(nvlist_add_uint64(resource,
843 FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid) == 0);
26685276
BB
844 VERIFY(nvlist_add_uint64(resource,
845 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state) == 0);
846 }
34dc7c2f 847
26685276 848 zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
34dc7c2f
BB
849#endif
850}
851
34dc7c2f
BB
852/*
853 * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
854 * has been removed from the system. This will cause the DE to ignore any
855 * recent I/O errors, inferring that they are due to the asynchronous device
856 * removal.
857 */
858void
859zfs_post_remove(spa_t *spa, vdev_t *vd)
860{
26685276 861 zfs_post_common(spa, vd, FM_EREPORT_RESOURCE_REMOVED);
34dc7c2f
BB
862}
863
864/*
865 * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
866 * has the 'autoreplace' property set, and therefore any broken vdevs will be
867 * handled by higher level logic, and no vdev fault should be generated.
868 */
869void
870zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
871{
26685276 872 zfs_post_common(spa, vd, FM_EREPORT_RESOURCE_AUTOREPLACE);
34dc7c2f 873}
428870ff
BB
874
875/*
876 * The 'resource.fs.zfs.statechange' event is an internal signal that the
877 * given vdev has transitioned its state to DEGRADED or HEALTHY. This will
878 * cause the retire agent to repair any outstanding fault management cases
879 * open because the device was not found (fault.fs.zfs.device).
880 */
881void
882zfs_post_state_change(spa_t *spa, vdev_t *vd)
883{
26685276 884 zfs_post_common(spa, vd, FM_EREPORT_RESOURCE_STATECHANGE);
428870ff 885}
26685276
BB
886
887#if defined(_KERNEL) && defined(HAVE_SPL)
888EXPORT_SYMBOL(zfs_ereport_post);
889EXPORT_SYMBOL(zfs_ereport_post_checksum);
890EXPORT_SYMBOL(zfs_post_remove);
891EXPORT_SYMBOL(zfs_post_autoreplace);
892EXPORT_SYMBOL(zfs_post_state_change);
893#endif /* _KERNEL */