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