xref: /titanic_41/usr/src/uts/common/fs/zfs/zio_inject.c (revision c6d6228cbba828ab5b2b6db6c280a44b2d841653)
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  * ZFS fault injection
28  *
29  * To handle fault injection, we keep track of a series of zinject_record_t
30  * structures which describe which logical block(s) should be injected with a
31  * fault.  These are kept in a global list.  Each record corresponds to a given
32  * spa_t and maintains a special hold on the spa_t so that it cannot be deleted
33  * or exported while the injection record exists.
34  *
35  * Device level injection is done using the 'zi_guid' field.  If this is set, it
36  * means that the error is destined for a particular device, not a piece of
37  * data.
38  *
39  * This is a rather poor data structure and algorithm, but we don't expect more
40  * than a few faults at any one time, so it should be sufficient for our needs.
41  */
42 
43 #include <sys/arc.h>
44 #include <sys/zio_impl.h>
45 #include <sys/zfs_ioctl.h>
46 #include <sys/spa_impl.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/fs/zfs.h>
49 
50 uint32_t zio_injection_enabled;
51 
52 typedef struct inject_handler {
53 	int			zi_id;
54 	spa_t			*zi_spa;
55 	zinject_record_t	zi_record;
56 	list_node_t		zi_link;
57 } inject_handler_t;
58 
59 static list_t inject_handlers;
60 static krwlock_t inject_lock;
61 static int inject_next_id = 1;
62 
63 /*
64  * Returns true if the given record matches the I/O in progress.
65  */
66 static boolean_t
67 zio_match_handler(zbookmark_t *zb, uint64_t type,
68     zinject_record_t *record, int error)
69 {
70 	/*
71 	 * Check for a match against the MOS, which is based on type
72 	 */
73 	if (zb->zb_objset == 0 && record->zi_objset == 0 &&
74 	    record->zi_object == 0) {
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);
81 	}
82 
83 	/*
84 	 * Check for an exact match.
85 	 */
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);
94 
95 	return (B_FALSE);
96 }
97 
98 /*
99  * Determine if the I/O in question should return failure.  Returns the errno
100  * to be returned to the caller.
101  */
102 int
103 zio_handle_fault_injection(zio_t *zio, int error)
104 {
105 	int ret = 0;
106 	inject_handler_t *handler;
107 
108 	/*
109 	 * Ignore I/O not associated with any logical data.
110 	 */
111 	if (zio->io_logical == NULL)
112 		return (0);
113 
114 	/*
115 	 * Currently, we only support fault injection on reads.
116 	 */
117 	if (zio->io_type != ZIO_TYPE_READ)
118 		return (0);
119 
120 	rw_enter(&inject_lock, RW_READER);
121 
122 	for (handler = list_head(&inject_handlers); handler != NULL;
123 	    handler = list_next(&inject_handlers, handler)) {
124 
125 		/* Ignore errors not destined for this pool */
126 		if (zio->io_spa != handler->zi_spa)
127 			continue;
128 
129 		/* Ignore device errors */
130 		if (handler->zi_record.zi_guid != 0)
131 			continue;
132 
133 		/* If this handler matches, return EIO */
134 		if (zio_match_handler(&zio->io_logical->io_bookmark,
135 		    zio->io_bp ? BP_GET_TYPE(zio->io_bp) : DMU_OT_NONE,
136 		    &handler->zi_record, error)) {
137 			ret = error;
138 			break;
139 		}
140 	}
141 
142 	rw_exit(&inject_lock);
143 
144 	return (ret);
145 }
146 
147 /*
148  * Determine if the zio is part of a label update and has an injection
149  * handler associated with that portion of the label. Currently, we
150  * allow error injection in either the nvlist or the uberblock region of
151  * of the vdev label.
152  */
153 int
154 zio_handle_label_injection(zio_t *zio, int error)
155 {
156 	inject_handler_t *handler;
157 	vdev_t *vd = zio->io_vd;
158 	uint64_t offset = zio->io_offset;
159 	int label;
160 	int ret = 0;
161 
162 	if (offset + zio->io_size > VDEV_LABEL_START_SIZE &&
163 	    offset < vd->vdev_psize - VDEV_LABEL_END_SIZE)
164 		return (0);
165 
166 	rw_enter(&inject_lock, RW_READER);
167 
168 	for (handler = list_head(&inject_handlers); handler != NULL;
169 	    handler = list_next(&inject_handlers, handler)) {
170 		uint64_t start = handler->zi_record.zi_start;
171 		uint64_t end = handler->zi_record.zi_end;
172 
173 		/* Ignore device only faults */
174 		if (handler->zi_record.zi_start == 0)
175 			continue;
176 
177 		/*
178 		 * The injection region is the relative offsets within a
179 		 * vdev label. We must determine the label which is being
180 		 * updated and adjust our region accordingly.
181 		 */
182 		label = vdev_label_number(vd->vdev_psize, offset);
183 		start = vdev_label_offset(vd->vdev_psize, label, start);
184 		end = vdev_label_offset(vd->vdev_psize, label, end);
185 
186 		if (zio->io_vd->vdev_guid == handler->zi_record.zi_guid &&
187 		    (offset >= start && offset <= end)) {
188 			ret = error;
189 			break;
190 		}
191 	}
192 	rw_exit(&inject_lock);
193 	return (ret);
194 }
195 
196 
197 int
198 zio_handle_device_injection(vdev_t *vd, zio_t *zio, int error)
199 {
200 	inject_handler_t *handler;
201 	int ret = 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 		/* Ignore label specific faults */
209 		if (handler->zi_record.zi_start != 0)
210 			continue;
211 
212 		if (vd->vdev_guid == handler->zi_record.zi_guid) {
213 			if (handler->zi_record.zi_failfast &&
214 			    (zio == NULL || (zio->io_flags &
215 			    (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))) {
216 				continue;
217 			}
218 
219 			if (handler->zi_record.zi_error == error) {
220 				/*
221 				 * For a failed open, pretend like the device
222 				 * has gone away.
223 				 */
224 				if (error == ENXIO)
225 					vd->vdev_stat.vs_aux =
226 					    VDEV_AUX_OPEN_FAILED;
227 				ret = error;
228 				break;
229 			}
230 			if (handler->zi_record.zi_error == ENXIO) {
231 				ret = EIO;
232 				break;
233 			}
234 		}
235 	}
236 
237 	rw_exit(&inject_lock);
238 
239 	return (ret);
240 }
241 
242 /*
243  * Create a new handler for the given record.  We add it to the list, adding
244  * a reference to the spa_t in the process.  We increment zio_injection_enabled,
245  * which is the switch to trigger all fault injection.
246  */
247 int
248 zio_inject_fault(char *name, int flags, int *id, zinject_record_t *record)
249 {
250 	inject_handler_t *handler;
251 	int error;
252 	spa_t *spa;
253 
254 	/*
255 	 * If this is pool-wide metadata, make sure we unload the corresponding
256 	 * spa_t, so that the next attempt to load it will trigger the fault.
257 	 * We call spa_reset() to unload the pool appropriately.
258 	 */
259 	if (flags & ZINJECT_UNLOAD_SPA)
260 		if ((error = spa_reset(name)) != 0)
261 			return (error);
262 
263 	if (!(flags & ZINJECT_NULL)) {
264 		/*
265 		 * spa_inject_ref() will add an injection reference, which will
266 		 * prevent the pool from being removed from the namespace while
267 		 * still allowing it to be unloaded.
268 		 */
269 		if ((spa = spa_inject_addref(name)) == NULL)
270 			return (ENOENT);
271 
272 		handler = kmem_alloc(sizeof (inject_handler_t), KM_SLEEP);
273 
274 		rw_enter(&inject_lock, RW_WRITER);
275 
276 		*id = handler->zi_id = inject_next_id++;
277 		handler->zi_spa = spa;
278 		handler->zi_record = *record;
279 		list_insert_tail(&inject_handlers, handler);
280 		atomic_add_32(&zio_injection_enabled, 1);
281 
282 		rw_exit(&inject_lock);
283 	}
284 
285 	/*
286 	 * Flush the ARC, so that any attempts to read this data will end up
287 	 * going to the ZIO layer.  Note that this is a little overkill, but
288 	 * we don't have the necessary ARC interfaces to do anything else, and
289 	 * fault injection isn't a performance critical path.
290 	 */
291 	if (flags & ZINJECT_FLUSH_ARC)
292 		arc_flush(NULL);
293 
294 	return (0);
295 }
296 
297 /*
298  * Returns the next record with an ID greater than that supplied to the
299  * function.  Used to iterate over all handlers in the system.
300  */
301 int
302 zio_inject_list_next(int *id, char *name, size_t buflen,
303     zinject_record_t *record)
304 {
305 	inject_handler_t *handler;
306 	int ret;
307 
308 	mutex_enter(&spa_namespace_lock);
309 	rw_enter(&inject_lock, RW_READER);
310 
311 	for (handler = list_head(&inject_handlers); handler != NULL;
312 	    handler = list_next(&inject_handlers, handler))
313 		if (handler->zi_id > *id)
314 			break;
315 
316 	if (handler) {
317 		*record = handler->zi_record;
318 		*id = handler->zi_id;
319 		(void) strncpy(name, spa_name(handler->zi_spa), buflen);
320 		ret = 0;
321 	} else {
322 		ret = ENOENT;
323 	}
324 
325 	rw_exit(&inject_lock);
326 	mutex_exit(&spa_namespace_lock);
327 
328 	return (ret);
329 }
330 
331 /*
332  * Clear the fault handler with the given identifier, or return ENOENT if none
333  * exists.
334  */
335 int
336 zio_clear_fault(int id)
337 {
338 	inject_handler_t *handler;
339 	int ret;
340 
341 	rw_enter(&inject_lock, RW_WRITER);
342 
343 	for (handler = list_head(&inject_handlers); handler != NULL;
344 	    handler = list_next(&inject_handlers, handler))
345 		if (handler->zi_id == id)
346 			break;
347 
348 	if (handler == NULL) {
349 		ret = ENOENT;
350 	} else {
351 		list_remove(&inject_handlers, handler);
352 		spa_inject_delref(handler->zi_spa);
353 		kmem_free(handler, sizeof (inject_handler_t));
354 		atomic_add_32(&zio_injection_enabled, -1);
355 		ret = 0;
356 	}
357 
358 	rw_exit(&inject_lock);
359 
360 	return (ret);
361 }
362 
363 void
364 zio_inject_init(void)
365 {
366 	rw_init(&inject_lock, NULL, RW_DEFAULT, NULL);
367 	list_create(&inject_handlers, sizeof (inject_handler_t),
368 	    offsetof(inject_handler_t, zi_link));
369 }
370 
371 void
372 zio_inject_fini(void)
373 {
374 	list_destroy(&inject_handlers);
375 	rw_destroy(&inject_lock);
376 }
377