Add linux events
[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 #include <sys/spa.h>
27 #include <sys/spa_impl.h>
28 #include <sys/vdev.h>
29 #include <sys/vdev_impl.h>
30 #include <sys/zio.h>
31 #include <sys/zio_checksum.h>
32
33 #include <sys/fm/fs/zfs.h>
34 #include <sys/fm/protocol.h>
35 #include <sys/fm/util.h>
36 #include <sys/sysevent.h>
37
38 /*
39  * This general routine is responsible for generating all the different ZFS
40  * ereports.  The payload is dependent on the class, and which arguments are
41  * supplied to the function:
42  *
43  *      EREPORT                 POOL    VDEV    IO
44  *      block                   X       X       X
45  *      data                    X               X
46  *      device                  X       X
47  *      pool                    X
48  *
49  * If we are in a loading state, all errors are chained together by the same
50  * SPA-wide ENA (Error Numeric Association).
51  *
52  * For isolated I/O requests, we get the ENA from the zio_t. The propagation
53  * gets very complicated due to RAID-Z, gang blocks, and vdev caching.  We want
54  * to chain together all ereports associated with a logical piece of data.  For
55  * read I/Os, there  are basically three 'types' of I/O, which form a roughly
56  * layered diagram:
57  *
58  *      +---------------+
59  *      | Aggregate I/O |       No associated logical data or device
60  *      +---------------+
61  *              |
62  *              V
63  *      +---------------+       Reads associated with a piece of logical data.
64  *      |   Read I/O    |       This includes reads on behalf of RAID-Z,
65  *      +---------------+       mirrors, gang blocks, retries, etc.
66  *              |
67  *              V
68  *      +---------------+       Reads associated with a particular device, but
69  *      | Physical I/O  |       no logical data.  Issued as part of vdev caching
70  *      +---------------+       and I/O aggregation.
71  *
72  * Note that 'physical I/O' here is not the same terminology as used in the rest
73  * of ZIO.  Typically, 'physical I/O' simply means that there is no attached
74  * blockpointer.  But I/O with no associated block pointer can still be related
75  * to a logical piece of data (i.e. RAID-Z requests).
76  *
77  * Purely physical I/O always have unique ENAs.  They are not related to a
78  * particular piece of logical data, and therefore cannot be chained together.
79  * We still generate an ereport, but the DE doesn't correlate it with any
80  * logical piece of data.  When such an I/O fails, the delegated I/O requests
81  * will issue a retry, which will trigger the 'real' ereport with the correct
82  * ENA.
83  *
84  * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
85  * When a new logical I/O is issued, we set this to point to itself.  Child I/Os
86  * then inherit this pointer, so that when it is first set subsequent failures
87  * will use the same ENA.  For vdev cache fill and queue aggregation I/O,
88  * this pointer is set to NULL, and no ereport will be generated (since it
89  * doesn't actually correspond to any particular device or piece of data,
90  * and the caller will always retry without caching or queueing anyway).
91  *
92  * For checksum errors, we want to include more information about the actual
93  * error which occurs.  Accordingly, we build an ereport when the error is
94  * noticed, but instead of sending it in immediately, we hang it off of the
95  * io_cksum_report field of the logical IO.  When the logical IO completes
96  * (successfully or not), zfs_ereport_finish_checksum() is called with the
97  * good and bad versions of the buffer (if available), and we annotate the
98  * ereport with information about the differences.
99  */
100 #ifdef _KERNEL
101 static void
102 zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector)
103 {
104         if (nvl)
105                 fm_nvlist_destroy(nvl, FM_NVA_FREE);
106
107         if (detector)
108                 fm_nvlist_destroy(detector, FM_NVA_FREE);
109 }
110
111 static void
112 zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
113     const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
114     uint64_t stateoroffset, uint64_t size)
115 {
116         nvlist_t *ereport, *detector;
117
118         uint64_t ena;
119         char class[64];
120
121         /*
122          * If we are doing a spa_tryimport() or in recovery mode,
123          * ignore errors.
124          */
125         if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
126             spa_load_state(spa) == SPA_LOAD_RECOVER)
127                 return;
128
129         /*
130          * If we are in the middle of opening a pool, and the previous attempt
131          * failed, don't bother logging any new ereports - we're just going to
132          * get the same diagnosis anyway.
133          */
134         if (spa_load_state(spa) != SPA_LOAD_NONE &&
135             spa->spa_last_open_failed)
136                 return;
137
138         if (zio != NULL) {
139                 /*
140                  * If this is not a read or write zio, ignore the error.  This
141                  * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
142                  */
143                 if (zio->io_type != ZIO_TYPE_READ &&
144                     zio->io_type != ZIO_TYPE_WRITE)
145                         return;
146
147                 /*
148                  * Ignore any errors from speculative I/Os, as failure is an
149                  * expected result.
150                  */
151                 if (zio->io_flags & ZIO_FLAG_SPECULATIVE)
152                         return;
153
154                 /*
155                  * If this I/O is not a retry I/O, don't post an ereport.
156                  * Otherwise, we risk making bad diagnoses based on B_FAILFAST
157                  * I/Os.
158                  */
159                 if (zio->io_error == EIO &&
160                     !(zio->io_flags & ZIO_FLAG_IO_RETRY))
161                         return;
162
163                 if (vd != NULL) {
164                         /*
165                          * If the vdev has already been marked as failing due
166                          * to a failed probe, then ignore any subsequent I/O
167                          * errors, as the DE will automatically fault the vdev
168                          * on the first such failure.  This also catches cases
169                          * where vdev_remove_wanted is set and the device has
170                          * not yet been asynchronously placed into the REMOVED
171                          * state.
172                          */
173                         if (zio->io_vd == vd && !vdev_accessible(vd, zio))
174                                 return;
175
176                         /*
177                          * Ignore checksum errors for reads from DTL regions of
178                          * leaf vdevs.
179                          */
180                         if (zio->io_type == ZIO_TYPE_READ &&
181                             zio->io_error == ECKSUM &&
182                             vd->vdev_ops->vdev_op_leaf &&
183                             vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
184                                 return;
185                 }
186         }
187
188         /*
189          * For probe failure, we want to avoid posting ereports if we've
190          * already removed the device in the meantime.
191          */
192         if (vd != NULL &&
193             strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
194             (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
195                 return;
196
197         if ((ereport = fm_nvlist_create(NULL)) == NULL)
198                 return;
199
200         if ((detector = fm_nvlist_create(NULL)) == NULL) {
201                 fm_nvlist_destroy(ereport, FM_NVA_FREE);
202                 return;
203         }
204
205         /*
206          * Serialize ereport generation
207          */
208         mutex_enter(&spa->spa_errlist_lock);
209
210         /*
211          * Determine the ENA to use for this event.  If we are in a loading
212          * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
213          * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
214          */
215         if (spa_load_state(spa) != SPA_LOAD_NONE) {
216                 if (spa->spa_ena == 0)
217                         spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
218                 ena = spa->spa_ena;
219         } else if (zio != NULL && zio->io_logical != NULL) {
220                 if (zio->io_logical->io_ena == 0)
221                         zio->io_logical->io_ena =
222                             fm_ena_generate(0, FM_ENA_FMT1);
223                 ena = zio->io_logical->io_ena;
224         } else {
225                 ena = fm_ena_generate(0, FM_ENA_FMT1);
226         }
227
228         /*
229          * Construct the full class, detector, and other standard FMA fields.
230          */
231         (void) snprintf(class, sizeof (class), "%s.%s",
232             ZFS_ERROR_CLASS, subclass);
233
234         fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
235             vd != NULL ? vd->vdev_guid : 0);
236
237         fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
238
239         /*
240          * Construct the per-ereport payload, depending on which parameters are
241          * passed in.
242          */
243
244         /*
245          * Generic payload members common to all ereports.
246          */
247         fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
248             DATA_TYPE_STRING, spa_name(spa), FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
249             DATA_TYPE_UINT64, spa_guid(spa),
250             FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
251             spa_load_state(spa), NULL);
252
253         if (spa != NULL) {
254                 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
255                     DATA_TYPE_STRING,
256                     spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
257                     FM_EREPORT_FAILMODE_WAIT :
258                     spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
259                     FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
260                     NULL);
261         }
262
263         if (vd != NULL) {
264                 vdev_t *pvd = vd->vdev_parent;
265
266                 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
267                     DATA_TYPE_UINT64, vd->vdev_guid,
268                     FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
269                     DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
270                 if (vd->vdev_path != NULL)
271                         fm_payload_set(ereport,
272                             FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
273                             DATA_TYPE_STRING, vd->vdev_path, NULL);
274                 if (vd->vdev_devid != NULL)
275                         fm_payload_set(ereport,
276                             FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
277                             DATA_TYPE_STRING, vd->vdev_devid, NULL);
278                 if (vd->vdev_fru != NULL)
279                         fm_payload_set(ereport,
280                             FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
281                             DATA_TYPE_STRING, vd->vdev_fru, NULL);
282
283                 if (pvd != NULL) {
284                         fm_payload_set(ereport,
285                             FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
286                             DATA_TYPE_UINT64, pvd->vdev_guid,
287                             FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
288                             DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
289                             NULL);
290                         if (pvd->vdev_path)
291                                 fm_payload_set(ereport,
292                                     FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
293                                     DATA_TYPE_STRING, pvd->vdev_path, NULL);
294                         if (pvd->vdev_devid)
295                                 fm_payload_set(ereport,
296                                     FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
297                                     DATA_TYPE_STRING, pvd->vdev_devid, NULL);
298                 }
299         }
300
301         if (zio != NULL) {
302                 /*
303                  * Payload common to all I/Os.
304                  */
305                 fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
306                     DATA_TYPE_INT32, zio->io_error, NULL);
307
308                 /*
309                  * If the 'size' parameter is non-zero, it indicates this is a
310                  * RAID-Z or other I/O where the physical offset and length are
311                  * provided for us, instead of within the zio_t.
312                  */
313                 if (vd != NULL) {
314                         if (size)
315                                 fm_payload_set(ereport,
316                                     FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
317                                     DATA_TYPE_UINT64, stateoroffset,
318                                     FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
319                                     DATA_TYPE_UINT64, size, NULL);
320                         else
321                                 fm_payload_set(ereport,
322                                     FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
323                                     DATA_TYPE_UINT64, zio->io_offset,
324                                     FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
325                                     DATA_TYPE_UINT64, zio->io_size, NULL);
326                 }
327
328                 /*
329                  * Payload for I/Os with corresponding logical information.
330                  */
331                 if (zio->io_logical != NULL)
332                         fm_payload_set(ereport,
333                             FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
334                             DATA_TYPE_UINT64,
335                             zio->io_logical->io_bookmark.zb_objset,
336                             FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
337                             DATA_TYPE_UINT64,
338                             zio->io_logical->io_bookmark.zb_object,
339                             FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
340                             DATA_TYPE_INT64,
341                             zio->io_logical->io_bookmark.zb_level,
342                             FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
343                             DATA_TYPE_UINT64,
344                             zio->io_logical->io_bookmark.zb_blkid, NULL);
345         } else if (vd != NULL) {
346                 /*
347                  * If we have a vdev but no zio, this is a device fault, and the
348                  * 'stateoroffset' parameter indicates the previous state of the
349                  * vdev.
350                  */
351                 fm_payload_set(ereport,
352                     FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
353                     DATA_TYPE_UINT64, stateoroffset, NULL);
354         }
355
356         mutex_exit(&spa->spa_errlist_lock);
357
358         *ereport_out = ereport;
359         *detector_out = detector;
360 }
361
362 /* if it's <= 128 bytes, save the corruption directly */
363 #define ZFM_MAX_INLINE          (128 / sizeof (uint64_t))
364
365 #define MAX_RANGES              16
366
367 typedef struct zfs_ecksum_info {
368         /* histograms of set and cleared bits by bit number in a 64-bit word */
369         uint16_t zei_histogram_set[sizeof (uint64_t) * NBBY];
370         uint16_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
371
372         /* inline arrays of bits set and cleared. */
373         uint64_t zei_bits_set[ZFM_MAX_INLINE];
374         uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
375
376         /*
377          * for each range, the number of bits set and cleared.  The Hamming
378          * distance between the good and bad buffers is the sum of them all.
379          */
380         uint32_t zei_range_sets[MAX_RANGES];
381         uint32_t zei_range_clears[MAX_RANGES];
382
383         struct zei_ranges {
384                 uint32_t        zr_start;
385                 uint32_t        zr_end;
386         } zei_ranges[MAX_RANGES];
387
388         size_t  zei_range_count;
389         uint32_t zei_mingap;
390         uint32_t zei_allowed_mingap;
391
392 } zfs_ecksum_info_t;
393
394 static void
395 update_histogram(uint64_t value_arg, uint16_t *hist, uint32_t *count)
396 {
397         size_t i;
398         size_t bits = 0;
399         uint64_t value = BE_64(value_arg);
400
401         /* We store the bits in big-endian (largest-first) order */
402         for (i = 0; i < 64; i++) {
403                 if (value & (1ull << i)) {
404                         hist[63 - i]++;
405                         ++bits;
406                 }
407         }
408         /* update the count of bits changed */
409         *count += bits;
410 }
411
412 /*
413  * We've now filled up the range array, and need to increase "mingap" and
414  * shrink the range list accordingly.  zei_mingap is always the smallest
415  * distance between array entries, so we set the new_allowed_gap to be
416  * one greater than that.  We then go through the list, joining together
417  * any ranges which are closer than the new_allowed_gap.
418  *
419  * By construction, there will be at least one.  We also update zei_mingap
420  * to the new smallest gap, to prepare for our next invocation.
421  */
422 static void
423 zei_shrink_ranges(zfs_ecksum_info_t *eip)
424 {
425         uint32_t mingap = UINT32_MAX;
426         uint32_t new_allowed_gap = eip->zei_mingap + 1;
427
428         size_t idx, output;
429         size_t max = eip->zei_range_count;
430
431         struct zei_ranges *r = eip->zei_ranges;
432
433         ASSERT3U(eip->zei_range_count, >, 0);
434         ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
435
436         output = idx = 0;
437         while (idx < max - 1) {
438                 uint32_t start = r[idx].zr_start;
439                 uint32_t end = r[idx].zr_end;
440
441                 while (idx < max - 1) {
442                         uint32_t nstart, nend, gap;
443
444                         idx++;
445                         nstart = r[idx].zr_start;
446                         nend = r[idx].zr_end;
447
448                         gap = nstart - end;
449                         if (gap < new_allowed_gap) {
450                                 end = nend;
451                                 continue;
452                         }
453                         if (gap < mingap)
454                                 mingap = gap;
455                         break;
456                 }
457                 r[output].zr_start = start;
458                 r[output].zr_end = end;
459                 output++;
460         }
461         ASSERT3U(output, <, eip->zei_range_count);
462         eip->zei_range_count = output;
463         eip->zei_mingap = mingap;
464         eip->zei_allowed_mingap = new_allowed_gap;
465 }
466
467 static void
468 zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
469 {
470         struct zei_ranges *r = eip->zei_ranges;
471         size_t count = eip->zei_range_count;
472
473         if (count >= MAX_RANGES) {
474                 zei_shrink_ranges(eip);
475                 count = eip->zei_range_count;
476         }
477         if (count == 0) {
478                 eip->zei_mingap = UINT32_MAX;
479                 eip->zei_allowed_mingap = 1;
480         } else {
481                 int gap = start - r[count - 1].zr_end;
482
483                 if (gap < eip->zei_allowed_mingap) {
484                         r[count - 1].zr_end = end;
485                         return;
486                 }
487                 if (gap < eip->zei_mingap)
488                         eip->zei_mingap = gap;
489         }
490         r[count].zr_start = start;
491         r[count].zr_end = end;
492         eip->zei_range_count++;
493 }
494
495 static size_t
496 zei_range_total_size(zfs_ecksum_info_t *eip)
497 {
498         struct zei_ranges *r = eip->zei_ranges;
499         size_t count = eip->zei_range_count;
500         size_t result = 0;
501         size_t idx;
502
503         for (idx = 0; idx < count; idx++)
504                 result += (r[idx].zr_end - r[idx].zr_start);
505
506         return (result);
507 }
508
509 static zfs_ecksum_info_t *
510 annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
511     const uint8_t *goodbuf, const uint8_t *badbuf, size_t size,
512     boolean_t drop_if_identical)
513 {
514         const uint64_t *good = (const uint64_t *)goodbuf;
515         const uint64_t *bad = (const uint64_t *)badbuf;
516
517         uint64_t allset = 0;
518         uint64_t allcleared = 0;
519
520         size_t nui64s = size / sizeof (uint64_t);
521
522         size_t inline_size;
523         int no_inline = 0;
524         size_t idx;
525         size_t range;
526
527         size_t offset = 0;
528         ssize_t start = -1;
529
530         zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
531
532         /* don't do any annotation for injected checksum errors */
533         if (info != NULL && info->zbc_injected)
534                 return (eip);
535
536         if (info != NULL && info->zbc_has_cksum) {
537                 fm_payload_set(ereport,
538                     FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
539                     DATA_TYPE_UINT64_ARRAY,
540                     sizeof (info->zbc_expected) / sizeof (uint64_t),
541                     (uint64_t *)&info->zbc_expected,
542                     FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
543                     DATA_TYPE_UINT64_ARRAY,
544                     sizeof (info->zbc_actual) / sizeof (uint64_t),
545                     (uint64_t *)&info->zbc_actual,
546                     FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
547                     DATA_TYPE_STRING,
548                     info->zbc_checksum_name,
549                     NULL);
550
551                 if (info->zbc_byteswapped) {
552                         fm_payload_set(ereport,
553                             FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
554                             DATA_TYPE_BOOLEAN, 1,
555                             NULL);
556                 }
557         }
558
559         if (badbuf == NULL || goodbuf == NULL)
560                 return (eip);
561
562         ASSERT3U(nui64s, <=, UINT16_MAX);
563         ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
564         ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
565         ASSERT3U(size, <=, UINT32_MAX);
566
567         /* build up the range list by comparing the two buffers. */
568         for (idx = 0; idx < nui64s; idx++) {
569                 if (good[idx] == bad[idx]) {
570                         if (start == -1)
571                                 continue;
572
573                         zei_add_range(eip, start, idx);
574                         start = -1;
575                 } else {
576                         if (start != -1)
577                                 continue;
578
579                         start = idx;
580                 }
581         }
582         if (start != -1)
583                 zei_add_range(eip, start, idx);
584
585         /* See if it will fit in our inline buffers */
586         inline_size = zei_range_total_size(eip);
587         if (inline_size > ZFM_MAX_INLINE)
588                 no_inline = 1;
589
590         /*
591          * If there is no change and we want to drop if the buffers are
592          * identical, do so.
593          */
594         if (inline_size == 0 && drop_if_identical) {
595                 kmem_free(eip, sizeof (*eip));
596                 return (NULL);
597         }
598
599         /*
600          * Now walk through the ranges, filling in the details of the
601          * differences.  Also convert our uint64_t-array offsets to byte
602          * offsets.
603          */
604         for (range = 0; range < eip->zei_range_count; range++) {
605                 size_t start = eip->zei_ranges[range].zr_start;
606                 size_t end = eip->zei_ranges[range].zr_end;
607
608                 for (idx = start; idx < end; idx++) {
609                         uint64_t set, cleared;
610
611                         // bits set in bad, but not in good
612                         set = ((~good[idx]) & bad[idx]);
613                         // bits set in good, but not in bad
614                         cleared = (good[idx] & (~bad[idx]));
615
616                         allset |= set;
617                         allcleared |= cleared;
618
619                         if (!no_inline) {
620                                 ASSERT3U(offset, <, inline_size);
621                                 eip->zei_bits_set[offset] = set;
622                                 eip->zei_bits_cleared[offset] = cleared;
623                                 offset++;
624                         }
625
626                         update_histogram(set, eip->zei_histogram_set,
627                             &eip->zei_range_sets[range]);
628                         update_histogram(cleared, eip->zei_histogram_cleared,
629                             &eip->zei_range_clears[range]);
630                 }
631
632                 /* convert to byte offsets */
633                 eip->zei_ranges[range].zr_start *= sizeof (uint64_t);
634                 eip->zei_ranges[range].zr_end   *= sizeof (uint64_t);
635         }
636         eip->zei_allowed_mingap *= sizeof (uint64_t);
637         inline_size             *= sizeof (uint64_t);
638
639         /* fill in ereport */
640         fm_payload_set(ereport,
641             FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
642             DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
643             (uint32_t *)eip->zei_ranges,
644             FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
645             DATA_TYPE_UINT32, eip->zei_allowed_mingap,
646             FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
647             DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
648             FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
649             DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
650             NULL);
651
652         if (!no_inline) {
653                 fm_payload_set(ereport,
654                     FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
655                     DATA_TYPE_UINT8_ARRAY,
656                     inline_size, (uint8_t *)eip->zei_bits_set,
657                     FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
658                     DATA_TYPE_UINT8_ARRAY,
659                     inline_size, (uint8_t *)eip->zei_bits_cleared,
660                     NULL);
661         } else {
662                 fm_payload_set(ereport,
663                     FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
664                     DATA_TYPE_UINT16_ARRAY,
665                     NBBY * sizeof (uint64_t), eip->zei_histogram_set,
666                     FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
667                     DATA_TYPE_UINT16_ARRAY,
668                     NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
669                     NULL);
670         }
671         return (eip);
672 }
673 #endif
674
675 void
676 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
677     uint64_t stateoroffset, uint64_t size)
678 {
679 #ifdef _KERNEL
680         nvlist_t *ereport = NULL;
681         nvlist_t *detector = NULL;
682
683         zfs_ereport_start(&ereport, &detector,
684             subclass, spa, vd, zio, stateoroffset, size);
685
686         if (ereport == NULL)
687                 return;
688
689         /* Cleanup is handled by the callback function */
690         zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
691 #endif
692 }
693
694 void
695 zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd,
696     struct zio *zio, uint64_t offset, uint64_t length, void *arg,
697     zio_bad_cksum_t *info)
698 {
699         zio_cksum_report_t *report = kmem_zalloc(sizeof (*report), KM_SLEEP);
700
701         if (zio->io_vsd != NULL)
702                 zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
703         else
704                 zio_vsd_default_cksum_report(zio, report, arg);
705
706         /* copy the checksum failure information if it was provided */
707         if (info != NULL) {
708                 report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
709                 bcopy(info, report->zcr_ckinfo, sizeof (*info));
710         }
711
712         report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
713         report->zcr_length = length;
714
715 #ifdef _KERNEL
716         zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
717             FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
718
719         if (report->zcr_ereport == NULL) {
720                 report->zcr_free(report->zcr_cbdata, report->zcr_cbinfo);
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 */