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