2882 implement libzfs_core
[illumos-gate.git] / usr / src / uts / common / fs / zfs / zio_inject.c
blob9ae7d1f697fdb8d7708e11a270a602b2e995f982
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26 * ZFS fault injection
28 * To handle fault injection, we keep track of a series of zinject_record_t
29 * structures which describe which logical block(s) should be injected with a
30 * fault. These are kept in a global list. Each record corresponds to a given
31 * spa_t and maintains a special hold on the spa_t so that it cannot be deleted
32 * or exported while the injection record exists.
34 * Device level injection is done using the 'zi_guid' field. If this is set, it
35 * means that the error is destined for a particular device, not a piece of
36 * data.
38 * This is a rather poor data structure and algorithm, but we don't expect more
39 * than a few faults at any one time, so it should be sufficient for our needs.
42 #include <sys/arc.h>
43 #include <sys/zio_impl.h>
44 #include <sys/zfs_ioctl.h>
45 #include <sys/vdev_impl.h>
46 #include <sys/dmu_objset.h>
47 #include <sys/fs/zfs.h>
49 uint32_t zio_injection_enabled;
51 typedef struct inject_handler {
52 int zi_id;
53 spa_t *zi_spa;
54 zinject_record_t zi_record;
55 list_node_t zi_link;
56 } inject_handler_t;
58 static list_t inject_handlers;
59 static krwlock_t inject_lock;
60 static int inject_next_id = 1;
63 * Returns true if the given record matches the I/O in progress.
65 static boolean_t
66 zio_match_handler(zbookmark_t *zb, uint64_t type,
67 zinject_record_t *record, int error)
70 * Check for a match against the MOS, which is based on type
72 if (zb->zb_objset == DMU_META_OBJSET &&
73 record->zi_objset == DMU_META_OBJSET &&
74 record->zi_object == DMU_META_DNODE_OBJECT) {
75 if (record->zi_type == DMU_OT_NONE ||
76 type == record->zi_type)
77 return (record->zi_freq == 0 ||
78 spa_get_random(100) < record->zi_freq);
79 else
80 return (B_FALSE);
84 * Check for an exact match.
86 if (zb->zb_objset == record->zi_objset &&
87 zb->zb_object == record->zi_object &&
88 zb->zb_level == record->zi_level &&
89 zb->zb_blkid >= record->zi_start &&
90 zb->zb_blkid <= record->zi_end &&
91 error == record->zi_error)
92 return (record->zi_freq == 0 ||
93 spa_get_random(100) < record->zi_freq);
95 return (B_FALSE);
99 * Panic the system when a config change happens in the function
100 * specified by tag.
102 void
103 zio_handle_panic_injection(spa_t *spa, char *tag, uint64_t type)
105 inject_handler_t *handler;
107 rw_enter(&inject_lock, RW_READER);
109 for (handler = list_head(&inject_handlers); handler != NULL;
110 handler = list_next(&inject_handlers, handler)) {
112 if (spa != handler->zi_spa)
113 continue;
115 if (handler->zi_record.zi_type == type &&
116 strcmp(tag, handler->zi_record.zi_func) == 0)
117 panic("Panic requested in function %s\n", tag);
120 rw_exit(&inject_lock);
124 * Determine if the I/O in question should return failure. Returns the errno
125 * to be returned to the caller.
128 zio_handle_fault_injection(zio_t *zio, int error)
130 int ret = 0;
131 inject_handler_t *handler;
134 * Ignore I/O not associated with any logical data.
136 if (zio->io_logical == NULL)
137 return (0);
140 * Currently, we only support fault injection on reads.
142 if (zio->io_type != ZIO_TYPE_READ)
143 return (0);
145 rw_enter(&inject_lock, RW_READER);
147 for (handler = list_head(&inject_handlers); handler != NULL;
148 handler = list_next(&inject_handlers, handler)) {
150 /* Ignore errors not destined for this pool */
151 if (zio->io_spa != handler->zi_spa)
152 continue;
154 /* Ignore device errors and panic injection */
155 if (handler->zi_record.zi_guid != 0 ||
156 handler->zi_record.zi_func[0] != '\0' ||
157 handler->zi_record.zi_duration != 0)
158 continue;
160 /* If this handler matches, return EIO */
161 if (zio_match_handler(&zio->io_logical->io_bookmark,
162 zio->io_bp ? BP_GET_TYPE(zio->io_bp) : DMU_OT_NONE,
163 &handler->zi_record, error)) {
164 ret = error;
165 break;
169 rw_exit(&inject_lock);
171 return (ret);
175 * Determine if the zio is part of a label update and has an injection
176 * handler associated with that portion of the label. Currently, we
177 * allow error injection in either the nvlist or the uberblock region of
178 * of the vdev label.
181 zio_handle_label_injection(zio_t *zio, int error)
183 inject_handler_t *handler;
184 vdev_t *vd = zio->io_vd;
185 uint64_t offset = zio->io_offset;
186 int label;
187 int ret = 0;
189 if (offset >= VDEV_LABEL_START_SIZE &&
190 offset < vd->vdev_psize - VDEV_LABEL_END_SIZE)
191 return (0);
193 rw_enter(&inject_lock, RW_READER);
195 for (handler = list_head(&inject_handlers); handler != NULL;
196 handler = list_next(&inject_handlers, handler)) {
197 uint64_t start = handler->zi_record.zi_start;
198 uint64_t end = handler->zi_record.zi_end;
200 /* Ignore device only faults or panic injection */
201 if (handler->zi_record.zi_start == 0 ||
202 handler->zi_record.zi_func[0] != '\0' ||
203 handler->zi_record.zi_duration != 0)
204 continue;
207 * The injection region is the relative offsets within a
208 * vdev label. We must determine the label which is being
209 * updated and adjust our region accordingly.
211 label = vdev_label_number(vd->vdev_psize, offset);
212 start = vdev_label_offset(vd->vdev_psize, label, start);
213 end = vdev_label_offset(vd->vdev_psize, label, end);
215 if (zio->io_vd->vdev_guid == handler->zi_record.zi_guid &&
216 (offset >= start && offset <= end)) {
217 ret = error;
218 break;
221 rw_exit(&inject_lock);
222 return (ret);
227 zio_handle_device_injection(vdev_t *vd, zio_t *zio, int error)
229 inject_handler_t *handler;
230 int ret = 0;
233 * We skip over faults in the labels unless it's during
234 * device open (i.e. zio == NULL).
236 if (zio != NULL) {
237 uint64_t offset = zio->io_offset;
239 if (offset < VDEV_LABEL_START_SIZE ||
240 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE)
241 return (0);
244 rw_enter(&inject_lock, RW_READER);
246 for (handler = list_head(&inject_handlers); handler != NULL;
247 handler = list_next(&inject_handlers, handler)) {
250 * Ignore label specific faults, panic injection
251 * or fake writes
253 if (handler->zi_record.zi_start != 0 ||
254 handler->zi_record.zi_func[0] != '\0' ||
255 handler->zi_record.zi_duration != 0)
256 continue;
258 if (vd->vdev_guid == handler->zi_record.zi_guid) {
259 if (handler->zi_record.zi_failfast &&
260 (zio == NULL || (zio->io_flags &
261 (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))) {
262 continue;
265 /* Handle type specific I/O failures */
266 if (zio != NULL &&
267 handler->zi_record.zi_iotype != ZIO_TYPES &&
268 handler->zi_record.zi_iotype != zio->io_type)
269 continue;
271 if (handler->zi_record.zi_error == error) {
273 * For a failed open, pretend like the device
274 * has gone away.
276 if (error == ENXIO)
277 vd->vdev_stat.vs_aux =
278 VDEV_AUX_OPEN_FAILED;
281 * Treat these errors as if they had been
282 * retried so that all the appropriate stats
283 * and FMA events are generated.
285 if (!handler->zi_record.zi_failfast &&
286 zio != NULL)
287 zio->io_flags |= ZIO_FLAG_IO_RETRY;
289 ret = error;
290 break;
292 if (handler->zi_record.zi_error == ENXIO) {
293 ret = EIO;
294 break;
299 rw_exit(&inject_lock);
301 return (ret);
305 * Simulate hardware that ignores cache flushes. For requested number
306 * of seconds nix the actual writing to disk.
308 void
309 zio_handle_ignored_writes(zio_t *zio)
311 inject_handler_t *handler;
313 rw_enter(&inject_lock, RW_READER);
315 for (handler = list_head(&inject_handlers); handler != NULL;
316 handler = list_next(&inject_handlers, handler)) {
318 /* Ignore errors not destined for this pool */
319 if (zio->io_spa != handler->zi_spa)
320 continue;
322 if (handler->zi_record.zi_duration == 0)
323 continue;
326 * Positive duration implies # of seconds, negative
327 * a number of txgs
329 if (handler->zi_record.zi_timer == 0) {
330 if (handler->zi_record.zi_duration > 0)
331 handler->zi_record.zi_timer = ddi_get_lbolt64();
332 else
333 handler->zi_record.zi_timer = zio->io_txg;
336 /* Have a "problem" writing 60% of the time */
337 if (spa_get_random(100) < 60)
338 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
339 break;
342 rw_exit(&inject_lock);
345 void
346 spa_handle_ignored_writes(spa_t *spa)
348 inject_handler_t *handler;
350 if (zio_injection_enabled == 0)
351 return;
353 rw_enter(&inject_lock, RW_READER);
355 for (handler = list_head(&inject_handlers); handler != NULL;
356 handler = list_next(&inject_handlers, handler)) {
358 /* Ignore errors not destined for this pool */
359 if (spa != handler->zi_spa)
360 continue;
362 if (handler->zi_record.zi_duration == 0)
363 continue;
365 if (handler->zi_record.zi_duration > 0) {
366 VERIFY(handler->zi_record.zi_timer == 0 ||
367 handler->zi_record.zi_timer +
368 handler->zi_record.zi_duration * hz >
369 ddi_get_lbolt64());
370 } else {
371 /* duration is negative so the subtraction here adds */
372 VERIFY(handler->zi_record.zi_timer == 0 ||
373 handler->zi_record.zi_timer -
374 handler->zi_record.zi_duration >=
375 spa_syncing_txg(spa));
379 rw_exit(&inject_lock);
383 * Create a new handler for the given record. We add it to the list, adding
384 * a reference to the spa_t in the process. We increment zio_injection_enabled,
385 * which is the switch to trigger all fault injection.
388 zio_inject_fault(char *name, int flags, int *id, zinject_record_t *record)
390 inject_handler_t *handler;
391 int error;
392 spa_t *spa;
395 * If this is pool-wide metadata, make sure we unload the corresponding
396 * spa_t, so that the next attempt to load it will trigger the fault.
397 * We call spa_reset() to unload the pool appropriately.
399 if (flags & ZINJECT_UNLOAD_SPA)
400 if ((error = spa_reset(name)) != 0)
401 return (error);
403 if (!(flags & ZINJECT_NULL)) {
405 * spa_inject_ref() will add an injection reference, which will
406 * prevent the pool from being removed from the namespace while
407 * still allowing it to be unloaded.
409 if ((spa = spa_inject_addref(name)) == NULL)
410 return (ENOENT);
412 handler = kmem_alloc(sizeof (inject_handler_t), KM_SLEEP);
414 rw_enter(&inject_lock, RW_WRITER);
416 *id = handler->zi_id = inject_next_id++;
417 handler->zi_spa = spa;
418 handler->zi_record = *record;
419 list_insert_tail(&inject_handlers, handler);
420 atomic_add_32(&zio_injection_enabled, 1);
422 rw_exit(&inject_lock);
426 * Flush the ARC, so that any attempts to read this data will end up
427 * going to the ZIO layer. Note that this is a little overkill, but
428 * we don't have the necessary ARC interfaces to do anything else, and
429 * fault injection isn't a performance critical path.
431 if (flags & ZINJECT_FLUSH_ARC)
432 arc_flush(NULL);
434 return (0);
438 * Returns the next record with an ID greater than that supplied to the
439 * function. Used to iterate over all handlers in the system.
442 zio_inject_list_next(int *id, char *name, size_t buflen,
443 zinject_record_t *record)
445 inject_handler_t *handler;
446 int ret;
448 mutex_enter(&spa_namespace_lock);
449 rw_enter(&inject_lock, RW_READER);
451 for (handler = list_head(&inject_handlers); handler != NULL;
452 handler = list_next(&inject_handlers, handler))
453 if (handler->zi_id > *id)
454 break;
456 if (handler) {
457 *record = handler->zi_record;
458 *id = handler->zi_id;
459 (void) strncpy(name, spa_name(handler->zi_spa), buflen);
460 ret = 0;
461 } else {
462 ret = ENOENT;
465 rw_exit(&inject_lock);
466 mutex_exit(&spa_namespace_lock);
468 return (ret);
472 * Clear the fault handler with the given identifier, or return ENOENT if none
473 * exists.
476 zio_clear_fault(int id)
478 inject_handler_t *handler;
480 rw_enter(&inject_lock, RW_WRITER);
482 for (handler = list_head(&inject_handlers); handler != NULL;
483 handler = list_next(&inject_handlers, handler))
484 if (handler->zi_id == id)
485 break;
487 if (handler == NULL) {
488 rw_exit(&inject_lock);
489 return (ENOENT);
492 list_remove(&inject_handlers, handler);
493 rw_exit(&inject_lock);
495 spa_inject_delref(handler->zi_spa);
496 kmem_free(handler, sizeof (inject_handler_t));
497 atomic_add_32(&zio_injection_enabled, -1);
499 return (0);
502 void
503 zio_inject_init(void)
505 rw_init(&inject_lock, NULL, RW_DEFAULT, NULL);
506 list_create(&inject_handlers, sizeof (inject_handler_t),
507 offsetof(inject_handler_t, zi_link));
510 void
511 zio_inject_fini(void)
513 list_destroy(&inject_handlers);
514 rw_destroy(&inject_lock);