zio_inject.c revision 874395d5f8cae2b9cd2d1fcbfcfe963a0c23966d
312N/A/*
312N/A * CDDL HEADER START
312N/A *
312N/A * The contents of this file are subject to the terms of the
312N/A * Common Development and Distribution License (the "License").
312N/A * You may not use this file except in compliance with the License.
312N/A *
312N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
312N/A * or http://www.opensolaris.org/os/licensing.
312N/A * See the License for the specific language governing permissions
312N/A * and limitations under the License.
312N/A *
312N/A * When distributing Covered Code, include this CDDL HEADER in each
312N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
312N/A * If applicable, add the following below this CDDL HEADER, with the
312N/A * fields enclosed by brackets "[]" replaced with your own identifying
312N/A * information: Portions Copyright [yyyy] [name of copyright owner]
312N/A *
312N/A * CDDL HEADER END
312N/A */
312N/A/*
1639N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
312N/A * Use is subject to license terms.
312N/A */
814N/A
814N/A#pragma ident "%Z%%M% %I% %E% SMI"
1780N/A
814N/A/*
312N/A * ZFS fault injection
312N/A *
312N/A * To handle fault injection, we keep track of a series of zinject_record_t
623N/A * structures which describe which logical block(s) should be injected with a
312N/A * fault. These are kept in a global list. Each record corresponds to a given
312N/A * spa_t and maintains a special hold on the spa_t so that it cannot be deleted
844N/A * or exported while the injection record exists.
844N/A *
623N/A * Device level injection is done using the 'zi_guid' field. If this is set, it
312N/A * means that the error is destined for a particular device, not a piece of
1258N/A * data.
312N/A *
312N/A * This is a rather poor data structure and algorithm, but we don't expect more
312N/A * than a few faults at any one time, so it should be sufficient for our needs.
312N/A */
312N/A
312N/A#include <sys/arc.h>
312N/A#include <sys/zio_impl.h>
312N/A#include <sys/zfs_ioctl.h>
312N/A#include <sys/spa_impl.h>
312N/A#include <sys/vdev_impl.h>
312N/A
312N/Auint32_t zio_injection_enabled;
312N/A
312N/Atypedef struct inject_handler {
312N/A int zi_id;
312N/A spa_t *zi_spa;
814N/A zinject_record_t zi_record;
814N/A list_node_t zi_link;
814N/A} inject_handler_t;
312N/A
312N/Astatic list_t inject_handlers;
312N/Astatic krwlock_t inject_lock;
623N/Astatic int inject_next_id = 1;
623N/A
623N/A/*
623N/A * Returns true if the given record matches the I/O in progress.
623N/A */
623N/Astatic boolean_t
623N/Azio_match_handler(zbookmark_t *zb, uint64_t type,
312N/A zinject_record_t *record, int error)
312N/A{
312N/A /*
312N/A * Check for a match against the MOS, which is based on type
312N/A */
312N/A if (zb->zb_objset == 0 && record->zi_objset == 0 &&
312N/A record->zi_object == 0) {
1639N/A if (record->zi_type == DMU_OT_NONE ||
1639N/A type == record->zi_type)
623N/A return (record->zi_freq == 0 ||
623N/A spa_get_random(100) < record->zi_freq);
623N/A else
623N/A return (B_FALSE);
312N/A }
312N/A
312N/A /*
312N/A * Check for an exact match.
312N/A */
312N/A if (zb->zb_objset == record->zi_objset &&
312N/A zb->zb_object == record->zi_object &&
312N/A zb->zb_level == record->zi_level &&
623N/A zb->zb_blkid >= record->zi_start &&
312N/A zb->zb_blkid <= record->zi_end &&
312N/A error == record->zi_error)
312N/A return (record->zi_freq == 0 ||
312N/A spa_get_random(100) < record->zi_freq);
312N/A
return (B_FALSE);
}
/*
* Determine if the I/O in question should return failure. Returns the errno
* to be returned to the caller.
*/
int
zio_handle_fault_injection(zio_t *zio, int error)
{
int ret = 0;
inject_handler_t *handler;
/*
* Ignore I/O not associated with any logical data.
*/
if (zio->io_logical == NULL)
return (0);
/*
* Currently, we only support fault injection on reads.
*/
if (zio->io_type != ZIO_TYPE_READ)
return (0);
rw_enter(&inject_lock, RW_READER);
for (handler = list_head(&inject_handlers); handler != NULL;
handler = list_next(&inject_handlers, handler)) {
/* Ignore errors not destined for this pool */
if (zio->io_spa != handler->zi_spa)
continue;
/* Ignore device errors */
if (handler->zi_record.zi_guid != 0)
continue;
/* If this handler matches, return EIO */
if (zio_match_handler(&zio->io_logical->io_bookmark,
zio->io_bp ? BP_GET_TYPE(zio->io_bp) : DMU_OT_NONE,
&handler->zi_record, error)) {
ret = error;
break;
}
}
rw_exit(&inject_lock);
return (ret);
}
int
zio_handle_device_injection(vdev_t *vd, int error)
{
inject_handler_t *handler;
int ret = 0;
rw_enter(&inject_lock, RW_READER);
for (handler = list_head(&inject_handlers); handler != NULL;
handler = list_next(&inject_handlers, handler)) {
if (vd->vdev_guid == handler->zi_record.zi_guid) {
if (handler->zi_record.zi_error == error) {
/*
* For a failed open, pretend like the device
* has gone away.
*/
if (error == ENXIO)
vd->vdev_stat.vs_aux =
VDEV_AUX_OPEN_FAILED;
ret = error;
break;
}
if (handler->zi_record.zi_error == ENXIO) {
ret = EIO;
break;
}
}
}
rw_exit(&inject_lock);
return (ret);
}
/*
* Create a new handler for the given record. We add it to the list, adding
* a reference to the spa_t in the process. We increment zio_injection_enabled,
* which is the switch to trigger all fault injection.
*/
int
zio_inject_fault(char *name, int flags, int *id, zinject_record_t *record)
{
inject_handler_t *handler;
int error;
spa_t *spa;
/*
* If this is pool-wide metadata, make sure we unload the corresponding
* spa_t, so that the next attempt to load it will trigger the fault.
* We call spa_reset() to unload the pool appropriately.
*/
if (flags & ZINJECT_UNLOAD_SPA)
if ((error = spa_reset(name)) != 0)
return (error);
if (!(flags & ZINJECT_NULL)) {
/*
* spa_inject_ref() will add an injection reference, which will
* prevent the pool from being removed from the namespace while
* still allowing it to be unloaded.
*/
if ((spa = spa_inject_addref(name)) == NULL)
return (ENOENT);
handler = kmem_alloc(sizeof (inject_handler_t), KM_SLEEP);
rw_enter(&inject_lock, RW_WRITER);
*id = handler->zi_id = inject_next_id++;
handler->zi_spa = spa;
handler->zi_record = *record;
list_insert_tail(&inject_handlers, handler);
atomic_add_32(&zio_injection_enabled, 1);
rw_exit(&inject_lock);
}
/*
* Flush the ARC, so that any attempts to read this data will end up
* going to the ZIO layer. Note that this is a little overkill, but
* we don't have the necessary ARC interfaces to do anything else, and
* fault injection isn't a performance critical path.
*/
if (flags & ZINJECT_FLUSH_ARC)
arc_flush(NULL);
return (0);
}
/*
* Returns the next record with an ID greater than that supplied to the
* function. Used to iterate over all handlers in the system.
*/
int
zio_inject_list_next(int *id, char *name, size_t buflen,
zinject_record_t *record)
{
inject_handler_t *handler;
int ret;
mutex_enter(&spa_namespace_lock);
rw_enter(&inject_lock, RW_READER);
for (handler = list_head(&inject_handlers); handler != NULL;
handler = list_next(&inject_handlers, handler))
if (handler->zi_id > *id)
break;
if (handler) {
*record = handler->zi_record;
*id = handler->zi_id;
(void) strncpy(name, spa_name(handler->zi_spa), buflen);
ret = 0;
} else {
ret = ENOENT;
}
rw_exit(&inject_lock);
mutex_exit(&spa_namespace_lock);
return (ret);
}
/*
* Clear the fault handler with the given identifier, or return ENOENT if none
* exists.
*/
int
zio_clear_fault(int id)
{
inject_handler_t *handler;
int ret;
rw_enter(&inject_lock, RW_WRITER);
for (handler = list_head(&inject_handlers); handler != NULL;
handler = list_next(&inject_handlers, handler))
if (handler->zi_id == id)
break;
if (handler == NULL) {
ret = ENOENT;
} else {
list_remove(&inject_handlers, handler);
spa_inject_delref(handler->zi_spa);
kmem_free(handler, sizeof (inject_handler_t));
atomic_add_32(&zio_injection_enabled, -1);
ret = 0;
}
rw_exit(&inject_lock);
return (ret);
}
void
zio_inject_init(void)
{
list_create(&inject_handlers, sizeof (inject_handler_t),
offsetof(inject_handler_t, zi_link));
}
void
zio_inject_fini(void)
{
list_destroy(&inject_handlers);
}