]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/zfs/zio_inject.c
dbuf_cons: deduplicate multilist_link_init()
[FreeBSD/FreeBSD.git] / module / zfs / zio_inject.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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24  * Copyright (c) 2017, Intel Corporation.
25  */
26
27 /*
28  * ZFS fault injection
29  *
30  * To handle fault injection, we keep track of a series of zinject_record_t
31  * structures which describe which logical block(s) should be injected with a
32  * fault.  These are kept in a global list.  Each record corresponds to a given
33  * spa_t and maintains a special hold on the spa_t so that it cannot be deleted
34  * or exported while the injection record exists.
35  *
36  * Device level injection is done using the 'zi_guid' field.  If this is set, it
37  * means that the error is destined for a particular device, not a piece of
38  * data.
39  *
40  * This is a rather poor data structure and algorithm, but we don't expect more
41  * than a few faults at any one time, so it should be sufficient for our needs.
42  */
43
44 #include <sys/arc.h>
45 #include <sys/zio.h>
46 #include <sys/zfs_ioctl.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/dmu_objset.h>
49 #include <sys/fs/zfs.h>
50
51 uint32_t zio_injection_enabled = 0;
52
53 /*
54  * Data describing each zinject handler registered on the system, and
55  * contains the list node linking the handler in the global zinject
56  * handler list.
57  */
58 typedef struct inject_handler {
59         int                     zi_id;
60         spa_t                   *zi_spa;
61         zinject_record_t        zi_record;
62         uint64_t                *zi_lanes;
63         int                     zi_next_lane;
64         list_node_t             zi_link;
65 } inject_handler_t;
66
67 /*
68  * List of all zinject handlers registered on the system, protected by
69  * the inject_lock defined below.
70  */
71 static list_t inject_handlers;
72
73 /*
74  * This protects insertion into, and traversal of, the inject handler
75  * list defined above; as well as the inject_delay_count. Any time a
76  * handler is inserted or removed from the list, this lock should be
77  * taken as a RW_WRITER; and any time traversal is done over the list
78  * (without modification to it) this lock should be taken as a RW_READER.
79  */
80 static krwlock_t inject_lock;
81
82 /*
83  * This holds the number of zinject delay handlers that have been
84  * registered on the system. It is protected by the inject_lock defined
85  * above. Thus modifications to this count must be a RW_WRITER of the
86  * inject_lock, and reads of this count must be (at least) a RW_READER
87  * of the lock.
88  */
89 static int inject_delay_count = 0;
90
91 /*
92  * This lock is used only in zio_handle_io_delay(), refer to the comment
93  * in that function for more details.
94  */
95 static kmutex_t inject_delay_mtx;
96
97 /*
98  * Used to assign unique identifying numbers to each new zinject handler.
99  */
100 static int inject_next_id = 1;
101
102 /*
103  * Test if the requested frequency was triggered
104  */
105 static boolean_t
106 freq_triggered(uint32_t frequency)
107 {
108         /*
109          * zero implies always (100%)
110          */
111         if (frequency == 0)
112                 return (B_TRUE);
113
114         /*
115          * Note: we still handle legacy (unscaled) frequecy values
116          */
117         uint32_t maximum = (frequency <= 100) ? 100 : ZI_PERCENTAGE_MAX;
118
119         return (spa_get_random(maximum) < frequency);
120 }
121
122 /*
123  * Returns true if the given record matches the I/O in progress.
124  */
125 static boolean_t
126 zio_match_handler(zbookmark_phys_t *zb, uint64_t type,
127     zinject_record_t *record, int error)
128 {
129         /*
130          * Check for a match against the MOS, which is based on type
131          */
132         if (zb->zb_objset == DMU_META_OBJSET &&
133             record->zi_objset == DMU_META_OBJSET &&
134             record->zi_object == DMU_META_DNODE_OBJECT) {
135                 if (record->zi_type == DMU_OT_NONE ||
136                     type == record->zi_type)
137                         return (freq_triggered(record->zi_freq));
138                 else
139                         return (B_FALSE);
140         }
141
142         /*
143          * Check for an exact match.
144          */
145         if (zb->zb_objset == record->zi_objset &&
146             zb->zb_object == record->zi_object &&
147             zb->zb_level == record->zi_level &&
148             zb->zb_blkid >= record->zi_start &&
149             zb->zb_blkid <= record->zi_end &&
150             error == record->zi_error)
151                 return (freq_triggered(record->zi_freq));
152
153         return (B_FALSE);
154 }
155
156 /*
157  * Panic the system when a config change happens in the function
158  * specified by tag.
159  */
160 void
161 zio_handle_panic_injection(spa_t *spa, char *tag, uint64_t type)
162 {
163         inject_handler_t *handler;
164
165         rw_enter(&inject_lock, RW_READER);
166
167         for (handler = list_head(&inject_handlers); handler != NULL;
168             handler = list_next(&inject_handlers, handler)) {
169
170                 if (spa != handler->zi_spa)
171                         continue;
172
173                 if (handler->zi_record.zi_type == type &&
174                     strcmp(tag, handler->zi_record.zi_func) == 0)
175                         panic("Panic requested in function %s\n", tag);
176         }
177
178         rw_exit(&inject_lock);
179 }
180
181 /*
182  * Determine if the I/O in question should return failure.  Returns the errno
183  * to be returned to the caller.
184  */
185 int
186 zio_handle_fault_injection(zio_t *zio, int error)
187 {
188         int ret = 0;
189         inject_handler_t *handler;
190
191         /*
192          * Ignore I/O not associated with any logical data.
193          */
194         if (zio->io_logical == NULL)
195                 return (0);
196
197         /*
198          * Currently, we only support fault injection on reads.
199          */
200         if (zio->io_type != ZIO_TYPE_READ)
201                 return (0);
202
203         rw_enter(&inject_lock, RW_READER);
204
205         for (handler = list_head(&inject_handlers); handler != NULL;
206             handler = list_next(&inject_handlers, handler)) {
207
208                 if (zio->io_spa != handler->zi_spa ||
209                     handler->zi_record.zi_cmd != ZINJECT_DATA_FAULT)
210                         continue;
211
212                 /* If this handler matches, return EIO */
213                 if (zio_match_handler(&zio->io_logical->io_bookmark,
214                     zio->io_bp ? BP_GET_TYPE(zio->io_bp) : DMU_OT_NONE,
215                     &handler->zi_record, error)) {
216                         ret = error;
217                         break;
218                 }
219         }
220
221         rw_exit(&inject_lock);
222
223         return (ret);
224 }
225
226 /*
227  * Determine if the zio is part of a label update and has an injection
228  * handler associated with that portion of the label. Currently, we
229  * allow error injection in either the nvlist or the uberblock region of
230  * of the vdev label.
231  */
232 int
233 zio_handle_label_injection(zio_t *zio, int error)
234 {
235         inject_handler_t *handler;
236         vdev_t *vd = zio->io_vd;
237         uint64_t offset = zio->io_offset;
238         int label;
239         int ret = 0;
240
241         if (offset >= VDEV_LABEL_START_SIZE &&
242             offset < vd->vdev_psize - VDEV_LABEL_END_SIZE)
243                 return (0);
244
245         rw_enter(&inject_lock, RW_READER);
246
247         for (handler = list_head(&inject_handlers); handler != NULL;
248             handler = list_next(&inject_handlers, handler)) {
249                 uint64_t start = handler->zi_record.zi_start;
250                 uint64_t end = handler->zi_record.zi_end;
251
252                 if (handler->zi_record.zi_cmd != ZINJECT_LABEL_FAULT)
253                         continue;
254
255                 /*
256                  * The injection region is the relative offsets within a
257                  * vdev label. We must determine the label which is being
258                  * updated and adjust our region accordingly.
259                  */
260                 label = vdev_label_number(vd->vdev_psize, offset);
261                 start = vdev_label_offset(vd->vdev_psize, label, start);
262                 end = vdev_label_offset(vd->vdev_psize, label, end);
263
264                 if (zio->io_vd->vdev_guid == handler->zi_record.zi_guid &&
265                     (offset >= start && offset <= end)) {
266                         ret = error;
267                         break;
268                 }
269         }
270         rw_exit(&inject_lock);
271         return (ret);
272 }
273
274 /*ARGSUSED*/
275 static int
276 zio_inject_bitflip_cb(void *data, size_t len, void *private)
277 {
278         ASSERTV(zio_t *zio = private);
279         uint8_t *buffer = data;
280         uint_t byte = spa_get_random(len);
281
282         ASSERT(zio->io_type == ZIO_TYPE_READ);
283
284         /* flip a single random bit in an abd data buffer */
285         buffer[byte] ^= 1 << spa_get_random(8);
286
287         return (1);     /* stop after first flip */
288 }
289
290 static int
291 zio_handle_device_injection_impl(vdev_t *vd, zio_t *zio, int err1, int err2)
292 {
293         inject_handler_t *handler;
294         int ret = 0;
295
296         /*
297          * We skip over faults in the labels unless it's during
298          * device open (i.e. zio == NULL).
299          */
300         if (zio != NULL) {
301                 uint64_t offset = zio->io_offset;
302
303                 if (offset < VDEV_LABEL_START_SIZE ||
304                     offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE)
305                         return (0);
306         }
307
308         rw_enter(&inject_lock, RW_READER);
309
310         for (handler = list_head(&inject_handlers); handler != NULL;
311             handler = list_next(&inject_handlers, handler)) {
312
313                 if (handler->zi_record.zi_cmd != ZINJECT_DEVICE_FAULT)
314                         continue;
315
316                 if (vd->vdev_guid == handler->zi_record.zi_guid) {
317                         if (handler->zi_record.zi_failfast &&
318                             (zio == NULL || (zio->io_flags &
319                             (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))) {
320                                 continue;
321                         }
322
323                         /* Handle type specific I/O failures */
324                         if (zio != NULL &&
325                             handler->zi_record.zi_iotype != ZIO_TYPES &&
326                             handler->zi_record.zi_iotype != zio->io_type)
327                                 continue;
328
329                         if (handler->zi_record.zi_error == err1 ||
330                             handler->zi_record.zi_error == err2) {
331                                 /*
332                                  * limit error injection if requested
333                                  */
334                                 if (!freq_triggered(handler->zi_record.zi_freq))
335                                         continue;
336
337                                 /*
338                                  * For a failed open, pretend like the device
339                                  * has gone away.
340                                  */
341                                 if (err1 == ENXIO)
342                                         vd->vdev_stat.vs_aux =
343                                             VDEV_AUX_OPEN_FAILED;
344
345                                 /*
346                                  * Treat these errors as if they had been
347                                  * retried so that all the appropriate stats
348                                  * and FMA events are generated.
349                                  */
350                                 if (!handler->zi_record.zi_failfast &&
351                                     zio != NULL)
352                                         zio->io_flags |= ZIO_FLAG_IO_RETRY;
353
354                                 /*
355                                  * EILSEQ means flip a bit after a read
356                                  */
357                                 if (handler->zi_record.zi_error == EILSEQ) {
358                                         if (zio == NULL)
359                                                 break;
360
361                                         /* locate buffer data and flip a bit */
362                                         (void) abd_iterate_func(zio->io_abd, 0,
363                                             zio->io_size, zio_inject_bitflip_cb,
364                                             zio);
365                                         break;
366                                 }
367
368                                 ret = handler->zi_record.zi_error;
369                                 break;
370                         }
371                         if (handler->zi_record.zi_error == ENXIO) {
372                                 ret = SET_ERROR(EIO);
373                                 break;
374                         }
375                 }
376         }
377
378         rw_exit(&inject_lock);
379
380         return (ret);
381 }
382
383 int
384 zio_handle_device_injection(vdev_t *vd, zio_t *zio, int error)
385 {
386         return (zio_handle_device_injection_impl(vd, zio, error, INT_MAX));
387 }
388
389 int
390 zio_handle_device_injections(vdev_t *vd, zio_t *zio, int err1, int err2)
391 {
392         return (zio_handle_device_injection_impl(vd, zio, err1, err2));
393 }
394
395 /*
396  * Simulate hardware that ignores cache flushes.  For requested number
397  * of seconds nix the actual writing to disk.
398  */
399 void
400 zio_handle_ignored_writes(zio_t *zio)
401 {
402         inject_handler_t *handler;
403
404         rw_enter(&inject_lock, RW_READER);
405
406         for (handler = list_head(&inject_handlers); handler != NULL;
407             handler = list_next(&inject_handlers, handler)) {
408
409                 /* Ignore errors not destined for this pool */
410                 if (zio->io_spa != handler->zi_spa ||
411                     handler->zi_record.zi_cmd != ZINJECT_IGNORED_WRITES)
412                         continue;
413
414                 /*
415                  * Positive duration implies # of seconds, negative
416                  * a number of txgs
417                  */
418                 if (handler->zi_record.zi_timer == 0) {
419                         if (handler->zi_record.zi_duration > 0)
420                                 handler->zi_record.zi_timer = ddi_get_lbolt64();
421                         else
422                                 handler->zi_record.zi_timer = zio->io_txg;
423                 }
424
425                 /* Have a "problem" writing 60% of the time */
426                 if (spa_get_random(100) < 60)
427                         zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
428                 break;
429         }
430
431         rw_exit(&inject_lock);
432 }
433
434 void
435 spa_handle_ignored_writes(spa_t *spa)
436 {
437         inject_handler_t *handler;
438
439         if (zio_injection_enabled == 0)
440                 return;
441
442         rw_enter(&inject_lock, RW_READER);
443
444         for (handler = list_head(&inject_handlers); handler != NULL;
445             handler = list_next(&inject_handlers, handler)) {
446
447                 if (spa != handler->zi_spa ||
448                     handler->zi_record.zi_cmd != ZINJECT_IGNORED_WRITES)
449                         continue;
450
451                 if (handler->zi_record.zi_duration > 0) {
452                         VERIFY(handler->zi_record.zi_timer == 0 ||
453                             ddi_time_after64(
454                             (int64_t)handler->zi_record.zi_timer +
455                             handler->zi_record.zi_duration * hz,
456                             ddi_get_lbolt64()));
457                 } else {
458                         /* duration is negative so the subtraction here adds */
459                         VERIFY(handler->zi_record.zi_timer == 0 ||
460                             handler->zi_record.zi_timer -
461                             handler->zi_record.zi_duration >=
462                             spa_syncing_txg(spa));
463                 }
464         }
465
466         rw_exit(&inject_lock);
467 }
468
469 hrtime_t
470 zio_handle_io_delay(zio_t *zio)
471 {
472         vdev_t *vd = zio->io_vd;
473         inject_handler_t *min_handler = NULL;
474         hrtime_t min_target = 0;
475         inject_handler_t *handler;
476         hrtime_t idle;
477         hrtime_t busy;
478         hrtime_t target;
479
480         rw_enter(&inject_lock, RW_READER);
481
482         /*
483          * inject_delay_count is a subset of zio_injection_enabled that
484          * is only incremented for delay handlers. These checks are
485          * mainly added to remind the reader why we're not explicitly
486          * checking zio_injection_enabled like the other functions.
487          */
488         IMPLY(inject_delay_count > 0, zio_injection_enabled > 0);
489         IMPLY(zio_injection_enabled == 0, inject_delay_count == 0);
490
491         /*
492          * If there aren't any inject delay handlers registered, then we
493          * can short circuit and simply return 0 here. A value of zero
494          * informs zio_delay_interrupt() that this request should not be
495          * delayed. This short circuit keeps us from acquiring the
496          * inject_delay_mutex unnecessarily.
497          */
498         if (inject_delay_count == 0) {
499                 rw_exit(&inject_lock);
500                 return (0);
501         }
502
503         /*
504          * Each inject handler has a number of "lanes" associated with
505          * it. Each lane is able to handle requests independently of one
506          * another, and at a latency defined by the inject handler
507          * record's zi_timer field. Thus if a handler in configured with
508          * a single lane with a 10ms latency, it will delay requests
509          * such that only a single request is completed every 10ms. So,
510          * if more than one request is attempted per each 10ms interval,
511          * the average latency of the requests will be greater than
512          * 10ms; but if only a single request is submitted each 10ms
513          * interval the average latency will be 10ms.
514          *
515          * We need to acquire this mutex to prevent multiple concurrent
516          * threads being assigned to the same lane of a given inject
517          * handler. The mutex allows us to perform the following two
518          * operations atomically:
519          *
520          *      1. determine the minimum handler and minimum target
521          *         value of all the possible handlers
522          *      2. update that minimum handler's lane array
523          *
524          * Without atomicity, two (or more) threads could pick the same
525          * lane in step (1), and then conflict with each other in step
526          * (2). This could allow a single lane handler to process
527          * multiple requests simultaneously, which shouldn't be possible.
528          */
529         mutex_enter(&inject_delay_mtx);
530
531         for (handler = list_head(&inject_handlers);
532             handler != NULL; handler = list_next(&inject_handlers, handler)) {
533                 if (handler->zi_record.zi_cmd != ZINJECT_DELAY_IO)
534                         continue;
535
536                 if (!freq_triggered(handler->zi_record.zi_freq))
537                         continue;
538
539                 if (vd->vdev_guid != handler->zi_record.zi_guid)
540                         continue;
541
542                 /*
543                  * Defensive; should never happen as the array allocation
544                  * occurs prior to inserting this handler on the list.
545                  */
546                 ASSERT3P(handler->zi_lanes, !=, NULL);
547
548                 /*
549                  * This should never happen, the zinject command should
550                  * prevent a user from setting an IO delay with zero lanes.
551                  */
552                 ASSERT3U(handler->zi_record.zi_nlanes, !=, 0);
553
554                 ASSERT3U(handler->zi_record.zi_nlanes, >,
555                     handler->zi_next_lane);
556
557                 /*
558                  * We want to issue this IO to the lane that will become
559                  * idle the soonest, so we compare the soonest this
560                  * specific handler can complete the IO with all other
561                  * handlers, to find the lowest value of all possible
562                  * lanes. We then use this lane to submit the request.
563                  *
564                  * Since each handler has a constant value for its
565                  * delay, we can just use the "next" lane for that
566                  * handler; as it will always be the lane with the
567                  * lowest value for that particular handler (i.e. the
568                  * lane that will become idle the soonest). This saves a
569                  * scan of each handler's lanes array.
570                  *
571                  * There's two cases to consider when determining when
572                  * this specific IO request should complete. If this
573                  * lane is idle, we want to "submit" the request now so
574                  * it will complete after zi_timer milliseconds. Thus,
575                  * we set the target to now + zi_timer.
576                  *
577                  * If the lane is busy, we want this request to complete
578                  * zi_timer milliseconds after the lane becomes idle.
579                  * Since the 'zi_lanes' array holds the time at which
580                  * each lane will become idle, we use that value to
581                  * determine when this request should complete.
582                  */
583                 idle = handler->zi_record.zi_timer + gethrtime();
584                 busy = handler->zi_record.zi_timer +
585                     handler->zi_lanes[handler->zi_next_lane];
586                 target = MAX(idle, busy);
587
588                 if (min_handler == NULL) {
589                         min_handler = handler;
590                         min_target = target;
591                         continue;
592                 }
593
594                 ASSERT3P(min_handler, !=, NULL);
595                 ASSERT3U(min_target, !=, 0);
596
597                 /*
598                  * We don't yet increment the "next lane" variable since
599                  * we still might find a lower value lane in another
600                  * handler during any remaining iterations. Once we're
601                  * sure we've selected the absolute minimum, we'll claim
602                  * the lane and increment the handler's "next lane"
603                  * field below.
604                  */
605
606                 if (target < min_target) {
607                         min_handler = handler;
608                         min_target = target;
609                 }
610         }
611
612         /*
613          * 'min_handler' will be NULL if no IO delays are registered for
614          * this vdev, otherwise it will point to the handler containing
615          * the lane that will become idle the soonest.
616          */
617         if (min_handler != NULL) {
618                 ASSERT3U(min_target, !=, 0);
619                 min_handler->zi_lanes[min_handler->zi_next_lane] = min_target;
620
621                 /*
622                  * If we've used all possible lanes for this handler,
623                  * loop back and start using the first lane again;
624                  * otherwise, just increment the lane index.
625                  */
626                 min_handler->zi_next_lane = (min_handler->zi_next_lane + 1) %
627                     min_handler->zi_record.zi_nlanes;
628         }
629
630         mutex_exit(&inject_delay_mtx);
631         rw_exit(&inject_lock);
632
633         return (min_target);
634 }
635
636 /*
637  * Create a new handler for the given record.  We add it to the list, adding
638  * a reference to the spa_t in the process.  We increment zio_injection_enabled,
639  * which is the switch to trigger all fault injection.
640  */
641 int
642 zio_inject_fault(char *name, int flags, int *id, zinject_record_t *record)
643 {
644         inject_handler_t *handler;
645         int error;
646         spa_t *spa;
647
648         /*
649          * If this is pool-wide metadata, make sure we unload the corresponding
650          * spa_t, so that the next attempt to load it will trigger the fault.
651          * We call spa_reset() to unload the pool appropriately.
652          */
653         if (flags & ZINJECT_UNLOAD_SPA)
654                 if ((error = spa_reset(name)) != 0)
655                         return (error);
656
657         if (record->zi_cmd == ZINJECT_DELAY_IO) {
658                 /*
659                  * A value of zero for the number of lanes or for the
660                  * delay time doesn't make sense.
661                  */
662                 if (record->zi_timer == 0 || record->zi_nlanes == 0)
663                         return (SET_ERROR(EINVAL));
664
665                 /*
666                  * The number of lanes is directly mapped to the size of
667                  * an array used by the handler. Thus, to ensure the
668                  * user doesn't trigger an allocation that's "too large"
669                  * we cap the number of lanes here.
670                  */
671                 if (record->zi_nlanes >= UINT16_MAX)
672                         return (SET_ERROR(EINVAL));
673         }
674
675         if (!(flags & ZINJECT_NULL)) {
676                 /*
677                  * spa_inject_ref() will add an injection reference, which will
678                  * prevent the pool from being removed from the namespace while
679                  * still allowing it to be unloaded.
680                  */
681                 if ((spa = spa_inject_addref(name)) == NULL)
682                         return (SET_ERROR(ENOENT));
683
684                 handler = kmem_alloc(sizeof (inject_handler_t), KM_SLEEP);
685
686                 handler->zi_spa = spa;
687                 handler->zi_record = *record;
688
689                 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
690                         handler->zi_lanes = kmem_zalloc(
691                             sizeof (*handler->zi_lanes) *
692                             handler->zi_record.zi_nlanes, KM_SLEEP);
693                         handler->zi_next_lane = 0;
694                 } else {
695                         handler->zi_lanes = NULL;
696                         handler->zi_next_lane = 0;
697                 }
698
699                 rw_enter(&inject_lock, RW_WRITER);
700
701                 /*
702                  * We can't move this increment into the conditional
703                  * above because we need to hold the RW_WRITER lock of
704                  * inject_lock, and we don't want to hold that while
705                  * allocating the handler's zi_lanes array.
706                  */
707                 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
708                         ASSERT3S(inject_delay_count, >=, 0);
709                         inject_delay_count++;
710                         ASSERT3S(inject_delay_count, >, 0);
711                 }
712
713                 *id = handler->zi_id = inject_next_id++;
714                 list_insert_tail(&inject_handlers, handler);
715                 atomic_inc_32(&zio_injection_enabled);
716
717                 rw_exit(&inject_lock);
718         }
719
720         /*
721          * Flush the ARC, so that any attempts to read this data will end up
722          * going to the ZIO layer.  Note that this is a little overkill, but
723          * we don't have the necessary ARC interfaces to do anything else, and
724          * fault injection isn't a performance critical path.
725          */
726         if (flags & ZINJECT_FLUSH_ARC)
727                 /*
728                  * We must use FALSE to ensure arc_flush returns, since
729                  * we're not preventing concurrent ARC insertions.
730                  */
731                 arc_flush(NULL, FALSE);
732
733         return (0);
734 }
735
736 /*
737  * Returns the next record with an ID greater than that supplied to the
738  * function.  Used to iterate over all handlers in the system.
739  */
740 int
741 zio_inject_list_next(int *id, char *name, size_t buflen,
742     zinject_record_t *record)
743 {
744         inject_handler_t *handler;
745         int ret;
746
747         mutex_enter(&spa_namespace_lock);
748         rw_enter(&inject_lock, RW_READER);
749
750         for (handler = list_head(&inject_handlers); handler != NULL;
751             handler = list_next(&inject_handlers, handler))
752                 if (handler->zi_id > *id)
753                         break;
754
755         if (handler) {
756                 *record = handler->zi_record;
757                 *id = handler->zi_id;
758                 (void) strncpy(name, spa_name(handler->zi_spa), buflen);
759                 ret = 0;
760         } else {
761                 ret = SET_ERROR(ENOENT);
762         }
763
764         rw_exit(&inject_lock);
765         mutex_exit(&spa_namespace_lock);
766
767         return (ret);
768 }
769
770 /*
771  * Clear the fault handler with the given identifier, or return ENOENT if none
772  * exists.
773  */
774 int
775 zio_clear_fault(int id)
776 {
777         inject_handler_t *handler;
778
779         rw_enter(&inject_lock, RW_WRITER);
780
781         for (handler = list_head(&inject_handlers); handler != NULL;
782             handler = list_next(&inject_handlers, handler))
783                 if (handler->zi_id == id)
784                         break;
785
786         if (handler == NULL) {
787                 rw_exit(&inject_lock);
788                 return (SET_ERROR(ENOENT));
789         }
790
791         if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
792                 ASSERT3S(inject_delay_count, >, 0);
793                 inject_delay_count--;
794                 ASSERT3S(inject_delay_count, >=, 0);
795         }
796
797         list_remove(&inject_handlers, handler);
798         rw_exit(&inject_lock);
799
800         if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
801                 ASSERT3P(handler->zi_lanes, !=, NULL);
802                 kmem_free(handler->zi_lanes, sizeof (*handler->zi_lanes) *
803                     handler->zi_record.zi_nlanes);
804         } else {
805                 ASSERT3P(handler->zi_lanes, ==, NULL);
806         }
807
808         spa_inject_delref(handler->zi_spa);
809         kmem_free(handler, sizeof (inject_handler_t));
810         atomic_dec_32(&zio_injection_enabled);
811
812         return (0);
813 }
814
815 void
816 zio_inject_init(void)
817 {
818         rw_init(&inject_lock, NULL, RW_DEFAULT, NULL);
819         mutex_init(&inject_delay_mtx, NULL, MUTEX_DEFAULT, NULL);
820         list_create(&inject_handlers, sizeof (inject_handler_t),
821             offsetof(inject_handler_t, zi_link));
822 }
823
824 void
825 zio_inject_fini(void)
826 {
827         list_destroy(&inject_handlers);
828         mutex_destroy(&inject_delay_mtx);
829         rw_destroy(&inject_lock);
830 }
831
832 #if defined(_KERNEL) && defined(HAVE_SPL)
833 EXPORT_SYMBOL(zio_injection_enabled);
834 EXPORT_SYMBOL(zio_inject_fault);
835 EXPORT_SYMBOL(zio_inject_list_next);
836 EXPORT_SYMBOL(zio_clear_fault);
837 EXPORT_SYMBOL(zio_handle_fault_injection);
838 EXPORT_SYMBOL(zio_handle_device_injection);
839 EXPORT_SYMBOL(zio_handle_label_injection);
840 #endif