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