]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zio_inject.c
Project Quota on ZFS
[mirror_zfs.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
476 rw_enter(&inject_lock, RW_READER);
477
478 /*
479 * inject_delay_count is a subset of zio_injection_enabled that
480 * is only incremented for delay handlers. These checks are
481 * mainly added to remind the reader why we're not explicitly
482 * checking zio_injection_enabled like the other functions.
483 */
484 IMPLY(inject_delay_count > 0, zio_injection_enabled > 0);
485 IMPLY(zio_injection_enabled == 0, inject_delay_count == 0);
486
487 /*
488 * If there aren't any inject delay handlers registered, then we
489 * can short circuit and simply return 0 here. A value of zero
490 * informs zio_delay_interrupt() that this request should not be
491 * delayed. This short circuit keeps us from acquiring the
492 * inject_delay_mutex unnecessarily.
493 */
494 if (inject_delay_count == 0) {
495 rw_exit(&inject_lock);
496 return (0);
497 }
498
499 /*
500 * Each inject handler has a number of "lanes" associated with
501 * it. Each lane is able to handle requests independently of one
502 * another, and at a latency defined by the inject handler
503 * record's zi_timer field. Thus if a handler in configured with
504 * a single lane with a 10ms latency, it will delay requests
505 * such that only a single request is completed every 10ms. So,
506 * if more than one request is attempted per each 10ms interval,
507 * the average latency of the requests will be greater than
508 * 10ms; but if only a single request is submitted each 10ms
509 * interval the average latency will be 10ms.
510 *
511 * We need to acquire this mutex to prevent multiple concurrent
512 * threads being assigned to the same lane of a given inject
513 * handler. The mutex allows us to perform the following two
514 * operations atomically:
515 *
516 * 1. determine the minimum handler and minimum target
517 * value of all the possible handlers
518 * 2. update that minimum handler's lane array
519 *
520 * Without atomicity, two (or more) threads could pick the same
521 * lane in step (1), and then conflict with each other in step
522 * (2). This could allow a single lane handler to process
523 * multiple requests simultaneously, which shouldn't be possible.
524 */
525 mutex_enter(&inject_delay_mtx);
526
527 for (inject_handler_t *handler = list_head(&inject_handlers);
528 handler != NULL; handler = list_next(&inject_handlers, handler)) {
529 if (handler->zi_record.zi_cmd != ZINJECT_DELAY_IO)
530 continue;
531
532 if (!freq_triggered(handler->zi_record.zi_freq))
533 continue;
534
535 if (vd->vdev_guid != handler->zi_record.zi_guid)
536 continue;
537
538 /*
539 * Defensive; should never happen as the array allocation
540 * occurs prior to inserting this handler on the list.
541 */
542 ASSERT3P(handler->zi_lanes, !=, NULL);
543
544 /*
545 * This should never happen, the zinject command should
546 * prevent a user from setting an IO delay with zero lanes.
547 */
548 ASSERT3U(handler->zi_record.zi_nlanes, !=, 0);
549
550 ASSERT3U(handler->zi_record.zi_nlanes, >,
551 handler->zi_next_lane);
552
553 /*
554 * We want to issue this IO to the lane that will become
555 * idle the soonest, so we compare the soonest this
556 * specific handler can complete the IO with all other
557 * handlers, to find the lowest value of all possible
558 * lanes. We then use this lane to submit the request.
559 *
560 * Since each handler has a constant value for its
561 * delay, we can just use the "next" lane for that
562 * handler; as it will always be the lane with the
563 * lowest value for that particular handler (i.e. the
564 * lane that will become idle the soonest). This saves a
565 * scan of each handler's lanes array.
566 *
567 * There's two cases to consider when determining when
568 * this specific IO request should complete. If this
569 * lane is idle, we want to "submit" the request now so
570 * it will complete after zi_timer milliseconds. Thus,
571 * we set the target to now + zi_timer.
572 *
573 * If the lane is busy, we want this request to complete
574 * zi_timer milliseconds after the lane becomes idle.
575 * Since the 'zi_lanes' array holds the time at which
576 * each lane will become idle, we use that value to
577 * determine when this request should complete.
578 */
579 hrtime_t idle = handler->zi_record.zi_timer + gethrtime();
580 hrtime_t busy = handler->zi_record.zi_timer +
581 handler->zi_lanes[handler->zi_next_lane];
582 hrtime_t target = MAX(idle, busy);
583
584 if (min_handler == NULL) {
585 min_handler = handler;
586 min_target = target;
587 continue;
588 }
589
590 ASSERT3P(min_handler, !=, NULL);
591 ASSERT3U(min_target, !=, 0);
592
593 /*
594 * We don't yet increment the "next lane" variable since
595 * we still might find a lower value lane in another
596 * handler during any remaining iterations. Once we're
597 * sure we've selected the absolute minimum, we'll claim
598 * the lane and increment the handler's "next lane"
599 * field below.
600 */
601
602 if (target < min_target) {
603 min_handler = handler;
604 min_target = target;
605 }
606 }
607
608 /*
609 * 'min_handler' will be NULL if no IO delays are registered for
610 * this vdev, otherwise it will point to the handler containing
611 * the lane that will become idle the soonest.
612 */
613 if (min_handler != NULL) {
614 ASSERT3U(min_target, !=, 0);
615 min_handler->zi_lanes[min_handler->zi_next_lane] = min_target;
616
617 /*
618 * If we've used all possible lanes for this handler,
619 * loop back and start using the first lane again;
620 * otherwise, just increment the lane index.
621 */
622 min_handler->zi_next_lane = (min_handler->zi_next_lane + 1) %
623 min_handler->zi_record.zi_nlanes;
624 }
625
626 mutex_exit(&inject_delay_mtx);
627 rw_exit(&inject_lock);
628
629 return (min_target);
630 }
631
632 /*
633 * Create a new handler for the given record. We add it to the list, adding
634 * a reference to the spa_t in the process. We increment zio_injection_enabled,
635 * which is the switch to trigger all fault injection.
636 */
637 int
638 zio_inject_fault(char *name, int flags, int *id, zinject_record_t *record)
639 {
640 inject_handler_t *handler;
641 int error;
642 spa_t *spa;
643
644 /*
645 * If this is pool-wide metadata, make sure we unload the corresponding
646 * spa_t, so that the next attempt to load it will trigger the fault.
647 * We call spa_reset() to unload the pool appropriately.
648 */
649 if (flags & ZINJECT_UNLOAD_SPA)
650 if ((error = spa_reset(name)) != 0)
651 return (error);
652
653 if (record->zi_cmd == ZINJECT_DELAY_IO) {
654 /*
655 * A value of zero for the number of lanes or for the
656 * delay time doesn't make sense.
657 */
658 if (record->zi_timer == 0 || record->zi_nlanes == 0)
659 return (SET_ERROR(EINVAL));
660
661 /*
662 * The number of lanes is directly mapped to the size of
663 * an array used by the handler. Thus, to ensure the
664 * user doesn't trigger an allocation that's "too large"
665 * we cap the number of lanes here.
666 */
667 if (record->zi_nlanes >= UINT16_MAX)
668 return (SET_ERROR(EINVAL));
669 }
670
671 if (!(flags & ZINJECT_NULL)) {
672 /*
673 * spa_inject_ref() will add an injection reference, which will
674 * prevent the pool from being removed from the namespace while
675 * still allowing it to be unloaded.
676 */
677 if ((spa = spa_inject_addref(name)) == NULL)
678 return (SET_ERROR(ENOENT));
679
680 handler = kmem_alloc(sizeof (inject_handler_t), KM_SLEEP);
681
682 handler->zi_spa = spa;
683 handler->zi_record = *record;
684
685 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
686 handler->zi_lanes = kmem_zalloc(
687 sizeof (*handler->zi_lanes) *
688 handler->zi_record.zi_nlanes, KM_SLEEP);
689 handler->zi_next_lane = 0;
690 } else {
691 handler->zi_lanes = NULL;
692 handler->zi_next_lane = 0;
693 }
694
695 rw_enter(&inject_lock, RW_WRITER);
696
697 /*
698 * We can't move this increment into the conditional
699 * above because we need to hold the RW_WRITER lock of
700 * inject_lock, and we don't want to hold that while
701 * allocating the handler's zi_lanes array.
702 */
703 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
704 ASSERT3S(inject_delay_count, >=, 0);
705 inject_delay_count++;
706 ASSERT3S(inject_delay_count, >, 0);
707 }
708
709 *id = handler->zi_id = inject_next_id++;
710 list_insert_tail(&inject_handlers, handler);
711 atomic_inc_32(&zio_injection_enabled);
712
713 rw_exit(&inject_lock);
714 }
715
716 /*
717 * Flush the ARC, so that any attempts to read this data will end up
718 * going to the ZIO layer. Note that this is a little overkill, but
719 * we don't have the necessary ARC interfaces to do anything else, and
720 * fault injection isn't a performance critical path.
721 */
722 if (flags & ZINJECT_FLUSH_ARC)
723 /*
724 * We must use FALSE to ensure arc_flush returns, since
725 * we're not preventing concurrent ARC insertions.
726 */
727 arc_flush(NULL, FALSE);
728
729 return (0);
730 }
731
732 /*
733 * Returns the next record with an ID greater than that supplied to the
734 * function. Used to iterate over all handlers in the system.
735 */
736 int
737 zio_inject_list_next(int *id, char *name, size_t buflen,
738 zinject_record_t *record)
739 {
740 inject_handler_t *handler;
741 int ret;
742
743 mutex_enter(&spa_namespace_lock);
744 rw_enter(&inject_lock, RW_READER);
745
746 for (handler = list_head(&inject_handlers); handler != NULL;
747 handler = list_next(&inject_handlers, handler))
748 if (handler->zi_id > *id)
749 break;
750
751 if (handler) {
752 *record = handler->zi_record;
753 *id = handler->zi_id;
754 (void) strncpy(name, spa_name(handler->zi_spa), buflen);
755 ret = 0;
756 } else {
757 ret = SET_ERROR(ENOENT);
758 }
759
760 rw_exit(&inject_lock);
761 mutex_exit(&spa_namespace_lock);
762
763 return (ret);
764 }
765
766 /*
767 * Clear the fault handler with the given identifier, or return ENOENT if none
768 * exists.
769 */
770 int
771 zio_clear_fault(int id)
772 {
773 inject_handler_t *handler;
774
775 rw_enter(&inject_lock, RW_WRITER);
776
777 for (handler = list_head(&inject_handlers); handler != NULL;
778 handler = list_next(&inject_handlers, handler))
779 if (handler->zi_id == id)
780 break;
781
782 if (handler == NULL) {
783 rw_exit(&inject_lock);
784 return (SET_ERROR(ENOENT));
785 }
786
787 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
788 ASSERT3S(inject_delay_count, >, 0);
789 inject_delay_count--;
790 ASSERT3S(inject_delay_count, >=, 0);
791 }
792
793 list_remove(&inject_handlers, handler);
794 rw_exit(&inject_lock);
795
796 if (handler->zi_record.zi_cmd == ZINJECT_DELAY_IO) {
797 ASSERT3P(handler->zi_lanes, !=, NULL);
798 kmem_free(handler->zi_lanes, sizeof (*handler->zi_lanes) *
799 handler->zi_record.zi_nlanes);
800 } else {
801 ASSERT3P(handler->zi_lanes, ==, NULL);
802 }
803
804 spa_inject_delref(handler->zi_spa);
805 kmem_free(handler, sizeof (inject_handler_t));
806 atomic_dec_32(&zio_injection_enabled);
807
808 return (0);
809 }
810
811 void
812 zio_inject_init(void)
813 {
814 rw_init(&inject_lock, NULL, RW_DEFAULT, NULL);
815 mutex_init(&inject_delay_mtx, NULL, MUTEX_DEFAULT, NULL);
816 list_create(&inject_handlers, sizeof (inject_handler_t),
817 offsetof(inject_handler_t, zi_link));
818 }
819
820 void
821 zio_inject_fini(void)
822 {
823 list_destroy(&inject_handlers);
824 mutex_destroy(&inject_delay_mtx);
825 rw_destroy(&inject_lock);
826 }
827
828 #if defined(_KERNEL) && defined(HAVE_SPL)
829 EXPORT_SYMBOL(zio_injection_enabled);
830 EXPORT_SYMBOL(zio_inject_fault);
831 EXPORT_SYMBOL(zio_inject_list_next);
832 EXPORT_SYMBOL(zio_clear_fault);
833 EXPORT_SYMBOL(zio_handle_fault_injection);
834 EXPORT_SYMBOL(zio_handle_device_injection);
835 EXPORT_SYMBOL(zio_handle_label_injection);
836 #endif