]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/zfs/zfs_fm.c
Imported Upstream version 0.6.5.3
[mirror_zfs-debian.git] / module / zfs / zfs_fm.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Copyright (c) 2012 by Delphix. All rights reserved.
28 */
29
30 #include <sys/spa.h>
31 #include <sys/spa_impl.h>
32 #include <sys/vdev.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/zio.h>
35 #include <sys/zio_checksum.h>
36
37 #include <sys/fm/fs/zfs.h>
38 #include <sys/fm/protocol.h>
39 #include <sys/fm/util.h>
40 #include <sys/sysevent.h>
41
42 /*
43 * This general routine is responsible for generating all the different ZFS
44 * ereports. The payload is dependent on the class, and which arguments are
45 * supplied to the function:
46 *
47 * EREPORT POOL VDEV IO
48 * block X X X
49 * data X X
50 * device X X
51 * pool X
52 *
53 * If we are in a loading state, all errors are chained together by the same
54 * SPA-wide ENA (Error Numeric Association).
55 *
56 * For isolated I/O requests, we get the ENA from the zio_t. The propagation
57 * gets very complicated due to RAID-Z, gang blocks, and vdev caching. We want
58 * to chain together all ereports associated with a logical piece of data. For
59 * read I/Os, there are basically three 'types' of I/O, which form a roughly
60 * layered diagram:
61 *
62 * +---------------+
63 * | Aggregate I/O | No associated logical data or device
64 * +---------------+
65 * |
66 * V
67 * +---------------+ Reads associated with a piece of logical data.
68 * | Read I/O | This includes reads on behalf of RAID-Z,
69 * +---------------+ mirrors, gang blocks, retries, etc.
70 * |
71 * V
72 * +---------------+ Reads associated with a particular device, but
73 * | Physical I/O | no logical data. Issued as part of vdev caching
74 * +---------------+ and I/O aggregation.
75 *
76 * Note that 'physical I/O' here is not the same terminology as used in the rest
77 * of ZIO. Typically, 'physical I/O' simply means that there is no attached
78 * blockpointer. But I/O with no associated block pointer can still be related
79 * to a logical piece of data (i.e. RAID-Z requests).
80 *
81 * Purely physical I/O always have unique ENAs. They are not related to a
82 * particular piece of logical data, and therefore cannot be chained together.
83 * We still generate an ereport, but the DE doesn't correlate it with any
84 * logical piece of data. When such an I/O fails, the delegated I/O requests
85 * will issue a retry, which will trigger the 'real' ereport with the correct
86 * ENA.
87 *
88 * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
89 * When a new logical I/O is issued, we set this to point to itself. Child I/Os
90 * then inherit this pointer, so that when it is first set subsequent failures
91 * will use the same ENA. For vdev cache fill and queue aggregation I/O,
92 * this pointer is set to NULL, and no ereport will be generated (since it
93 * doesn't actually correspond to any particular device or piece of data,
94 * and the caller will always retry without caching or queueing anyway).
95 *
96 * For checksum errors, we want to include more information about the actual
97 * error which occurs. Accordingly, we build an ereport when the error is
98 * noticed, but instead of sending it in immediately, we hang it off of the
99 * io_cksum_report field of the logical IO. When the logical IO completes
100 * (successfully or not), zfs_ereport_finish_checksum() is called with the
101 * good and bad versions of the buffer (if available), and we annotate the
102 * ereport with information about the differences.
103 */
104 #ifdef _KERNEL
105 static void
106 zfs_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
115 static void
116 zfs_zevent_post_cb_noop(nvlist_t *nvl, nvlist_t *detector)
117 {
118 }
119
120 static void
121 zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
122 const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
123 uint64_t stateoroffset, uint64_t size)
124 {
125 nvlist_t *ereport, *detector;
126
127 uint64_t ena;
128 char class[64];
129
130 /*
131 * If we are doing a spa_tryimport() or in recovery mode,
132 * ignore errors.
133 */
134 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
135 spa_load_state(spa) == SPA_LOAD_RECOVER)
136 return;
137
138 /*
139 * If we are in the middle of opening a pool, and the previous attempt
140 * failed, don't bother logging any new ereports - we're just going to
141 * get the same diagnosis anyway.
142 */
143 if (spa_load_state(spa) != SPA_LOAD_NONE &&
144 spa->spa_last_open_failed)
145 return;
146
147 if (zio != NULL) {
148 /*
149 * If this is not a read or write zio, ignore the error. This
150 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
151 */
152 if (zio->io_type != ZIO_TYPE_READ &&
153 zio->io_type != ZIO_TYPE_WRITE)
154 return;
155
156 if (vd != NULL) {
157 /*
158 * If the vdev has already been marked as failing due
159 * to a failed probe, then ignore any subsequent I/O
160 * errors, as the DE will automatically fault the vdev
161 * on the first such failure. This also catches cases
162 * where vdev_remove_wanted is set and the device has
163 * not yet been asynchronously placed into the REMOVED
164 * state.
165 */
166 if (zio->io_vd == vd && !vdev_accessible(vd, zio))
167 return;
168
169 /*
170 * Ignore checksum errors for reads from DTL regions of
171 * leaf vdevs.
172 */
173 if (zio->io_type == ZIO_TYPE_READ &&
174 zio->io_error == ECKSUM &&
175 vd->vdev_ops->vdev_op_leaf &&
176 vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
177 return;
178 }
179 }
180
181 /*
182 * For probe failure, we want to avoid posting ereports if we've
183 * already removed the device in the meantime.
184 */
185 if (vd != NULL &&
186 strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
187 (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
188 return;
189
190 if ((ereport = fm_nvlist_create(NULL)) == NULL)
191 return;
192
193 if ((detector = fm_nvlist_create(NULL)) == NULL) {
194 fm_nvlist_destroy(ereport, FM_NVA_FREE);
195 return;
196 }
197
198 /*
199 * Serialize ereport generation
200 */
201 mutex_enter(&spa->spa_errlist_lock);
202
203 /*
204 * Determine the ENA to use for this event. If we are in a loading
205 * state, use a SPA-wide ENA. Otherwise, if we are in an I/O state, use
206 * a root zio-wide ENA. Otherwise, simply use a unique ENA.
207 */
208 if (spa_load_state(spa) != SPA_LOAD_NONE) {
209 if (spa->spa_ena == 0)
210 spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
211 ena = spa->spa_ena;
212 } else if (zio != NULL && zio->io_logical != NULL) {
213 if (zio->io_logical->io_ena == 0)
214 zio->io_logical->io_ena =
215 fm_ena_generate(0, FM_ENA_FMT1);
216 ena = zio->io_logical->io_ena;
217 } else {
218 ena = fm_ena_generate(0, FM_ENA_FMT1);
219 }
220
221 /*
222 * Construct the full class, detector, and other standard FMA fields.
223 */
224 (void) snprintf(class, sizeof (class), "%s.%s",
225 ZFS_ERROR_CLASS, subclass);
226
227 fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
228 vd != NULL ? vd->vdev_guid : 0);
229
230 fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
231
232 /*
233 * Construct the per-ereport payload, depending on which parameters are
234 * passed in.
235 */
236
237 /*
238 * Generic payload members common to all ereports.
239 */
240 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
241 DATA_TYPE_STRING, spa_name(spa), FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
242 DATA_TYPE_UINT64, spa_guid(spa),
243 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
244 spa_load_state(spa), NULL);
245
246 if (spa != NULL) {
247 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
248 DATA_TYPE_STRING,
249 spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
250 FM_EREPORT_FAILMODE_WAIT :
251 spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
252 FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
253 NULL);
254 }
255
256 if (vd != NULL) {
257 vdev_t *pvd = vd->vdev_parent;
258 vdev_queue_t *vq = &vd->vdev_queue;
259 vdev_stat_t *vs = &vd->vdev_stat;
260 vdev_t *spare_vd;
261 uint64_t *spare_guids;
262 char **spare_paths;
263 int i, spare_count;
264
265 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
266 DATA_TYPE_UINT64, vd->vdev_guid,
267 FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
268 DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
269 if (vd->vdev_path != NULL)
270 fm_payload_set(ereport,
271 FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
272 DATA_TYPE_STRING, vd->vdev_path, NULL);
273 if (vd->vdev_devid != NULL)
274 fm_payload_set(ereport,
275 FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
276 DATA_TYPE_STRING, vd->vdev_devid, NULL);
277 if (vd->vdev_fru != NULL)
278 fm_payload_set(ereport,
279 FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
280 DATA_TYPE_STRING, vd->vdev_fru, NULL);
281 if (vd->vdev_ashift)
282 fm_payload_set(ereport,
283 FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
284 DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
285
286 if (vq != NULL) {
287 fm_payload_set(ereport,
288 FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
289 DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
290 fm_payload_set(ereport,
291 FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
292 DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
293 }
294
295 if (vs != NULL) {
296 fm_payload_set(ereport,
297 FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS,
298 DATA_TYPE_UINT64, vs->vs_read_errors,
299 FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS,
300 DATA_TYPE_UINT64, vs->vs_write_errors,
301 FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS,
302 DATA_TYPE_UINT64, vs->vs_checksum_errors, NULL);
303 }
304
305 if (pvd != NULL) {
306 fm_payload_set(ereport,
307 FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
308 DATA_TYPE_UINT64, pvd->vdev_guid,
309 FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
310 DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
311 NULL);
312 if (pvd->vdev_path)
313 fm_payload_set(ereport,
314 FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
315 DATA_TYPE_STRING, pvd->vdev_path, NULL);
316 if (pvd->vdev_devid)
317 fm_payload_set(ereport,
318 FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
319 DATA_TYPE_STRING, pvd->vdev_devid, NULL);
320 }
321
322 spare_count = spa->spa_spares.sav_count;
323 spare_paths = kmem_zalloc(sizeof (char *) * spare_count,
324 KM_SLEEP);
325 spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count,
326 KM_SLEEP);
327
328 for (i = 0; i < spare_count; i++) {
329 spare_vd = spa->spa_spares.sav_vdevs[i];
330 if (spare_vd) {
331 spare_paths[i] = spare_vd->vdev_path;
332 spare_guids[i] = spare_vd->vdev_guid;
333 }
334 }
335
336 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS,
337 DATA_TYPE_STRING_ARRAY, spare_count, spare_paths,
338 FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS,
339 DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL);
340
341 kmem_free(spare_guids, sizeof (uint64_t) * spare_count);
342 kmem_free(spare_paths, sizeof (char *) * spare_count);
343 }
344
345 if (zio != NULL) {
346 /*
347 * Payload common to all I/Os.
348 */
349 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
350 DATA_TYPE_INT32, zio->io_error, NULL);
351 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
352 DATA_TYPE_INT32, zio->io_flags, NULL);
353 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
354 DATA_TYPE_UINT32, zio->io_stage, NULL);
355 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
356 DATA_TYPE_UINT32, zio->io_pipeline, NULL);
357 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
358 DATA_TYPE_UINT64, zio->io_delay, NULL);
359 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
360 DATA_TYPE_UINT64, zio->io_timestamp, NULL);
361 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
362 DATA_TYPE_UINT64, zio->io_delta, NULL);
363
364 /*
365 * If the 'size' parameter is non-zero, it indicates this is a
366 * RAID-Z or other I/O where the physical offset and length are
367 * provided for us, instead of within the zio_t.
368 */
369 if (vd != NULL) {
370 if (size)
371 fm_payload_set(ereport,
372 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
373 DATA_TYPE_UINT64, stateoroffset,
374 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
375 DATA_TYPE_UINT64, size, NULL);
376 else
377 fm_payload_set(ereport,
378 FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
379 DATA_TYPE_UINT64, zio->io_offset,
380 FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
381 DATA_TYPE_UINT64, zio->io_size, NULL);
382 }
383
384 /*
385 * Payload for I/Os with corresponding logical information.
386 */
387 if (zio->io_logical != NULL)
388 fm_payload_set(ereport,
389 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
390 DATA_TYPE_UINT64,
391 zio->io_logical->io_bookmark.zb_objset,
392 FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
393 DATA_TYPE_UINT64,
394 zio->io_logical->io_bookmark.zb_object,
395 FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
396 DATA_TYPE_INT64,
397 zio->io_logical->io_bookmark.zb_level,
398 FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
399 DATA_TYPE_UINT64,
400 zio->io_logical->io_bookmark.zb_blkid, NULL);
401 } else if (vd != NULL) {
402 /*
403 * If we have a vdev but no zio, this is a device fault, and the
404 * 'stateoroffset' parameter indicates the previous state of the
405 * vdev.
406 */
407 fm_payload_set(ereport,
408 FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
409 DATA_TYPE_UINT64, stateoroffset, NULL);
410 }
411
412 mutex_exit(&spa->spa_errlist_lock);
413
414 *ereport_out = ereport;
415 *detector_out = detector;
416 }
417
418 /* if it's <= 128 bytes, save the corruption directly */
419 #define ZFM_MAX_INLINE (128 / sizeof (uint64_t))
420
421 #define MAX_RANGES 16
422
423 typedef struct zfs_ecksum_info {
424 /* histograms of set and cleared bits by bit number in a 64-bit word */
425 uint16_t zei_histogram_set[sizeof (uint64_t) * NBBY];
426 uint16_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
427
428 /* inline arrays of bits set and cleared. */
429 uint64_t zei_bits_set[ZFM_MAX_INLINE];
430 uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
431
432 /*
433 * for each range, the number of bits set and cleared. The Hamming
434 * distance between the good and bad buffers is the sum of them all.
435 */
436 uint32_t zei_range_sets[MAX_RANGES];
437 uint32_t zei_range_clears[MAX_RANGES];
438
439 struct zei_ranges {
440 uint32_t zr_start;
441 uint32_t zr_end;
442 } zei_ranges[MAX_RANGES];
443
444 size_t zei_range_count;
445 uint32_t zei_mingap;
446 uint32_t zei_allowed_mingap;
447
448 } zfs_ecksum_info_t;
449
450 static void
451 update_histogram(uint64_t value_arg, uint16_t *hist, uint32_t *count)
452 {
453 size_t i;
454 size_t bits = 0;
455 uint64_t value = BE_64(value_arg);
456
457 /* We store the bits in big-endian (largest-first) order */
458 for (i = 0; i < 64; i++) {
459 if (value & (1ull << i)) {
460 hist[63 - i]++;
461 ++bits;
462 }
463 }
464 /* update the count of bits changed */
465 *count += bits;
466 }
467
468 /*
469 * We've now filled up the range array, and need to increase "mingap" and
470 * shrink the range list accordingly. zei_mingap is always the smallest
471 * distance between array entries, so we set the new_allowed_gap to be
472 * one greater than that. We then go through the list, joining together
473 * any ranges which are closer than the new_allowed_gap.
474 *
475 * By construction, there will be at least one. We also update zei_mingap
476 * to the new smallest gap, to prepare for our next invocation.
477 */
478 static void
479 zei_shrink_ranges(zfs_ecksum_info_t *eip)
480 {
481 uint32_t mingap = UINT32_MAX;
482 uint32_t new_allowed_gap = eip->zei_mingap + 1;
483
484 size_t idx, output;
485 size_t max = eip->zei_range_count;
486
487 struct zei_ranges *r = eip->zei_ranges;
488
489 ASSERT3U(eip->zei_range_count, >, 0);
490 ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
491
492 output = idx = 0;
493 while (idx < max - 1) {
494 uint32_t start = r[idx].zr_start;
495 uint32_t end = r[idx].zr_end;
496
497 while (idx < max - 1) {
498 uint32_t nstart, nend, gap;
499
500 idx++;
501 nstart = r[idx].zr_start;
502 nend = r[idx].zr_end;
503
504 gap = nstart - end;
505 if (gap < new_allowed_gap) {
506 end = nend;
507 continue;
508 }
509 if (gap < mingap)
510 mingap = gap;
511 break;
512 }
513 r[output].zr_start = start;
514 r[output].zr_end = end;
515 output++;
516 }
517 ASSERT3U(output, <, eip->zei_range_count);
518 eip->zei_range_count = output;
519 eip->zei_mingap = mingap;
520 eip->zei_allowed_mingap = new_allowed_gap;
521 }
522
523 static void
524 zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
525 {
526 struct zei_ranges *r = eip->zei_ranges;
527 size_t count = eip->zei_range_count;
528
529 if (count >= MAX_RANGES) {
530 zei_shrink_ranges(eip);
531 count = eip->zei_range_count;
532 }
533 if (count == 0) {
534 eip->zei_mingap = UINT32_MAX;
535 eip->zei_allowed_mingap = 1;
536 } else {
537 int gap = start - r[count - 1].zr_end;
538
539 if (gap < eip->zei_allowed_mingap) {
540 r[count - 1].zr_end = end;
541 return;
542 }
543 if (gap < eip->zei_mingap)
544 eip->zei_mingap = gap;
545 }
546 r[count].zr_start = start;
547 r[count].zr_end = end;
548 eip->zei_range_count++;
549 }
550
551 static size_t
552 zei_range_total_size(zfs_ecksum_info_t *eip)
553 {
554 struct zei_ranges *r = eip->zei_ranges;
555 size_t count = eip->zei_range_count;
556 size_t result = 0;
557 size_t idx;
558
559 for (idx = 0; idx < count; idx++)
560 result += (r[idx].zr_end - r[idx].zr_start);
561
562 return (result);
563 }
564
565 static zfs_ecksum_info_t *
566 annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
567 const uint8_t *goodbuf, const uint8_t *badbuf, size_t size,
568 boolean_t drop_if_identical)
569 {
570 const uint64_t *good = (const uint64_t *)goodbuf;
571 const uint64_t *bad = (const uint64_t *)badbuf;
572
573 uint64_t allset = 0;
574 uint64_t allcleared = 0;
575
576 size_t nui64s = size / sizeof (uint64_t);
577
578 size_t inline_size;
579 int no_inline = 0;
580 size_t idx;
581 size_t range;
582
583 size_t offset = 0;
584 ssize_t start = -1;
585
586 zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
587
588 /* don't do any annotation for injected checksum errors */
589 if (info != NULL && info->zbc_injected)
590 return (eip);
591
592 if (info != NULL && info->zbc_has_cksum) {
593 fm_payload_set(ereport,
594 FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
595 DATA_TYPE_UINT64_ARRAY,
596 sizeof (info->zbc_expected) / sizeof (uint64_t),
597 (uint64_t *)&info->zbc_expected,
598 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
599 DATA_TYPE_UINT64_ARRAY,
600 sizeof (info->zbc_actual) / sizeof (uint64_t),
601 (uint64_t *)&info->zbc_actual,
602 FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
603 DATA_TYPE_STRING,
604 info->zbc_checksum_name,
605 NULL);
606
607 if (info->zbc_byteswapped) {
608 fm_payload_set(ereport,
609 FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
610 DATA_TYPE_BOOLEAN, 1,
611 NULL);
612 }
613 }
614
615 if (badbuf == NULL || goodbuf == NULL)
616 return (eip);
617
618 ASSERT3U(nui64s, <=, UINT16_MAX);
619 ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
620 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
621 ASSERT3U(size, <=, UINT32_MAX);
622
623 /* build up the range list by comparing the two buffers. */
624 for (idx = 0; idx < nui64s; idx++) {
625 if (good[idx] == bad[idx]) {
626 if (start == -1)
627 continue;
628
629 zei_add_range(eip, start, idx);
630 start = -1;
631 } else {
632 if (start != -1)
633 continue;
634
635 start = idx;
636 }
637 }
638 if (start != -1)
639 zei_add_range(eip, start, idx);
640
641 /* See if it will fit in our inline buffers */
642 inline_size = zei_range_total_size(eip);
643 if (inline_size > ZFM_MAX_INLINE)
644 no_inline = 1;
645
646 /*
647 * If there is no change and we want to drop if the buffers are
648 * identical, do so.
649 */
650 if (inline_size == 0 && drop_if_identical) {
651 kmem_free(eip, sizeof (*eip));
652 return (NULL);
653 }
654
655 /*
656 * Now walk through the ranges, filling in the details of the
657 * differences. Also convert our uint64_t-array offsets to byte
658 * offsets.
659 */
660 for (range = 0; range < eip->zei_range_count; range++) {
661 size_t start = eip->zei_ranges[range].zr_start;
662 size_t end = eip->zei_ranges[range].zr_end;
663
664 for (idx = start; idx < end; idx++) {
665 uint64_t set, cleared;
666
667 // bits set in bad, but not in good
668 set = ((~good[idx]) & bad[idx]);
669 // bits set in good, but not in bad
670 cleared = (good[idx] & (~bad[idx]));
671
672 allset |= set;
673 allcleared |= cleared;
674
675 if (!no_inline) {
676 ASSERT3U(offset, <, inline_size);
677 eip->zei_bits_set[offset] = set;
678 eip->zei_bits_cleared[offset] = cleared;
679 offset++;
680 }
681
682 update_histogram(set, eip->zei_histogram_set,
683 &eip->zei_range_sets[range]);
684 update_histogram(cleared, eip->zei_histogram_cleared,
685 &eip->zei_range_clears[range]);
686 }
687
688 /* convert to byte offsets */
689 eip->zei_ranges[range].zr_start *= sizeof (uint64_t);
690 eip->zei_ranges[range].zr_end *= sizeof (uint64_t);
691 }
692 eip->zei_allowed_mingap *= sizeof (uint64_t);
693 inline_size *= sizeof (uint64_t);
694
695 /* fill in ereport */
696 fm_payload_set(ereport,
697 FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
698 DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
699 (uint32_t *)eip->zei_ranges,
700 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
701 DATA_TYPE_UINT32, eip->zei_allowed_mingap,
702 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
703 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
704 FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
705 DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
706 NULL);
707
708 if (!no_inline) {
709 fm_payload_set(ereport,
710 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
711 DATA_TYPE_UINT8_ARRAY,
712 inline_size, (uint8_t *)eip->zei_bits_set,
713 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
714 DATA_TYPE_UINT8_ARRAY,
715 inline_size, (uint8_t *)eip->zei_bits_cleared,
716 NULL);
717 } else {
718 fm_payload_set(ereport,
719 FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
720 DATA_TYPE_UINT16_ARRAY,
721 NBBY * sizeof (uint64_t), eip->zei_histogram_set,
722 FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
723 DATA_TYPE_UINT16_ARRAY,
724 NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
725 NULL);
726 }
727 return (eip);
728 }
729 #endif
730
731 void
732 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
733 uint64_t stateoroffset, uint64_t size)
734 {
735 #ifdef _KERNEL
736 nvlist_t *ereport = NULL;
737 nvlist_t *detector = NULL;
738
739 zfs_ereport_start(&ereport, &detector,
740 subclass, spa, vd, zio, stateoroffset, size);
741
742 if (ereport == NULL)
743 return;
744
745 /* Cleanup is handled by the callback function */
746 zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
747 #endif
748 }
749
750 void
751 zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd,
752 struct zio *zio, uint64_t offset, uint64_t length, void *arg,
753 zio_bad_cksum_t *info)
754 {
755 zio_cksum_report_t *report = kmem_zalloc(sizeof (*report), KM_SLEEP);
756
757 if (zio->io_vsd != NULL)
758 zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
759 else
760 zio_vsd_default_cksum_report(zio, report, arg);
761
762 /* copy the checksum failure information if it was provided */
763 if (info != NULL) {
764 report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
765 bcopy(info, report->zcr_ckinfo, sizeof (*info));
766 }
767
768 report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
769 report->zcr_length = length;
770
771 #ifdef _KERNEL
772 zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
773 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
774
775 if (report->zcr_ereport == NULL) {
776 zfs_ereport_free_checksum(report);
777 return;
778 }
779 #endif
780
781 mutex_enter(&spa->spa_errlist_lock);
782 report->zcr_next = zio->io_logical->io_cksum_report;
783 zio->io_logical->io_cksum_report = report;
784 mutex_exit(&spa->spa_errlist_lock);
785 }
786
787 void
788 zfs_ereport_finish_checksum(zio_cksum_report_t *report,
789 const void *good_data, const void *bad_data, boolean_t drop_if_identical)
790 {
791 #ifdef _KERNEL
792 zfs_ecksum_info_t *info;
793
794 info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
795 good_data, bad_data, report->zcr_length, drop_if_identical);
796 if (info != NULL)
797 zfs_zevent_post(report->zcr_ereport,
798 report->zcr_detector, zfs_zevent_post_cb);
799 else
800 zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector);
801
802 report->zcr_ereport = report->zcr_detector = NULL;
803 if (info != NULL)
804 kmem_free(info, sizeof (*info));
805 #endif
806 }
807
808 void
809 zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
810 {
811 #ifdef _KERNEL
812 if (rpt->zcr_ereport != NULL) {
813 fm_nvlist_destroy(rpt->zcr_ereport,
814 FM_NVA_FREE);
815 fm_nvlist_destroy(rpt->zcr_detector,
816 FM_NVA_FREE);
817 }
818 #endif
819 rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
820
821 if (rpt->zcr_ckinfo != NULL)
822 kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
823
824 kmem_free(rpt, sizeof (*rpt));
825 }
826
827 void
828 zfs_ereport_send_interim_checksum(zio_cksum_report_t *report)
829 {
830 #ifdef _KERNEL
831 zfs_zevent_post(report->zcr_ereport, report->zcr_detector,
832 zfs_zevent_post_cb_noop);
833 #endif
834 }
835
836 void
837 zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd,
838 struct zio *zio, uint64_t offset, uint64_t length,
839 const void *good_data, const void *bad_data, zio_bad_cksum_t *zbc)
840 {
841 #ifdef _KERNEL
842 nvlist_t *ereport = NULL;
843 nvlist_t *detector = NULL;
844 zfs_ecksum_info_t *info;
845
846 zfs_ereport_start(&ereport, &detector,
847 FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
848
849 if (ereport == NULL)
850 return;
851
852 info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
853 B_FALSE);
854
855 if (info != NULL) {
856 zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
857 kmem_free(info, sizeof (*info));
858 }
859 #endif
860 }
861
862 static void
863 zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
864 {
865 #ifdef _KERNEL
866 nvlist_t *resource;
867 char class[64];
868
869 if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
870 return;
871
872 if ((resource = fm_nvlist_create(NULL)) == NULL)
873 return;
874
875 (void) snprintf(class, sizeof (class), "%s.%s.%s", FM_RSRC_RESOURCE,
876 ZFS_ERROR_CLASS, name);
877 VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION));
878 VERIFY0(nvlist_add_string(resource, FM_CLASS, class));
879 VERIFY0(nvlist_add_uint64(resource,
880 FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)));
881 VERIFY0(nvlist_add_int32(resource,
882 FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa)));
883
884 if (vd) {
885 VERIFY0(nvlist_add_uint64(resource,
886 FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid));
887 VERIFY0(nvlist_add_uint64(resource,
888 FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state));
889 }
890
891 zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
892 #endif
893 }
894
895 /*
896 * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
897 * has been removed from the system. This will cause the DE to ignore any
898 * recent I/O errors, inferring that they are due to the asynchronous device
899 * removal.
900 */
901 void
902 zfs_post_remove(spa_t *spa, vdev_t *vd)
903 {
904 zfs_post_common(spa, vd, FM_EREPORT_RESOURCE_REMOVED);
905 }
906
907 /*
908 * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
909 * has the 'autoreplace' property set, and therefore any broken vdevs will be
910 * handled by higher level logic, and no vdev fault should be generated.
911 */
912 void
913 zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
914 {
915 zfs_post_common(spa, vd, FM_EREPORT_RESOURCE_AUTOREPLACE);
916 }
917
918 /*
919 * The 'resource.fs.zfs.statechange' event is an internal signal that the
920 * given vdev has transitioned its state to DEGRADED or HEALTHY. This will
921 * cause the retire agent to repair any outstanding fault management cases
922 * open because the device was not found (fault.fs.zfs.device).
923 */
924 void
925 zfs_post_state_change(spa_t *spa, vdev_t *vd)
926 {
927 zfs_post_common(spa, vd, FM_EREPORT_RESOURCE_STATECHANGE);
928 }
929
930 #if defined(_KERNEL) && defined(HAVE_SPL)
931 EXPORT_SYMBOL(zfs_ereport_post);
932 EXPORT_SYMBOL(zfs_ereport_post_checksum);
933 EXPORT_SYMBOL(zfs_post_remove);
934 EXPORT_SYMBOL(zfs_post_autoreplace);
935 EXPORT_SYMBOL(zfs_post_state_change);
936 #endif /* _KERNEL */