]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/zfs/zio_inject.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.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, 2014 by Delphix. All rights reserved.
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/vdev_impl.h>
47 #include <sys/dmu_objset.h>
48 #include <sys/fs/zfs.h>
49
50 uint32_t zio_injection_enabled = 0;
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_phys_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 == DMU_META_OBJSET &&
74 record->zi_objset == DMU_META_OBJSET &&
75 record->zi_object == DMU_META_DNODE_OBJECT) {
76 if (record->zi_type == DMU_OT_NONE ||
77 type == record->zi_type)
78 return (record->zi_freq == 0 ||
79 spa_get_random(100) < record->zi_freq);
80 else
81 return (B_FALSE);
82 }
83
84 /*
85 * Check for an exact match.
86 */
87 if (zb->zb_objset == record->zi_objset &&
88 zb->zb_object == record->zi_object &&
89 zb->zb_level == record->zi_level &&
90 zb->zb_blkid >= record->zi_start &&
91 zb->zb_blkid <= record->zi_end &&
92 error == record->zi_error)
93 return (record->zi_freq == 0 ||
94 spa_get_random(100) < record->zi_freq);
95
96 return (B_FALSE);
97 }
98
99 /*
100 * Panic the system when a config change happens in the function
101 * specified by tag.
102 */
103 void
104 zio_handle_panic_injection(spa_t *spa, char *tag, uint64_t type)
105 {
106 inject_handler_t *handler;
107
108 rw_enter(&inject_lock, RW_READER);
109
110 for (handler = list_head(&inject_handlers); handler != NULL;
111 handler = list_next(&inject_handlers, handler)) {
112
113 if (spa != handler->zi_spa)
114 continue;
115
116 if (handler->zi_record.zi_type == type &&
117 strcmp(tag, handler->zi_record.zi_func) == 0)
118 panic("Panic requested in function %s\n", tag);
119 }
120
121 rw_exit(&inject_lock);
122 }
123
124 /*
125 * Determine if the I/O in question should return failure. Returns the errno
126 * to be returned to the caller.
127 */
128 int
129 zio_handle_fault_injection(zio_t *zio, int error)
130 {
131 int ret = 0;
132 inject_handler_t *handler;
133
134 /*
135 * Ignore I/O not associated with any logical data.
136 */
137 if (zio->io_logical == NULL)
138 return (0);
139
140 /*
141 * Currently, we only support fault injection on reads.
142 */
143 if (zio->io_type != ZIO_TYPE_READ)
144 return (0);
145
146 rw_enter(&inject_lock, RW_READER);
147
148 for (handler = list_head(&inject_handlers); handler != NULL;
149 handler = list_next(&inject_handlers, handler)) {
150
151 if (zio->io_spa != handler->zi_spa ||
152 handler->zi_record.zi_cmd != ZINJECT_DATA_FAULT)
153 continue;
154
155 /* If this handler matches, return EIO */
156 if (zio_match_handler(&zio->io_logical->io_bookmark,
157 zio->io_bp ? BP_GET_TYPE(zio->io_bp) : DMU_OT_NONE,
158 &handler->zi_record, error)) {
159 ret = error;
160 break;
161 }
162 }
163
164 rw_exit(&inject_lock);
165
166 return (ret);
167 }
168
169 /*
170 * Determine if the zio is part of a label update and has an injection
171 * handler associated with that portion of the label. Currently, we
172 * allow error injection in either the nvlist or the uberblock region of
173 * of the vdev label.
174 */
175 int
176 zio_handle_label_injection(zio_t *zio, int error)
177 {
178 inject_handler_t *handler;
179 vdev_t *vd = zio->io_vd;
180 uint64_t offset = zio->io_offset;
181 int label;
182 int ret = 0;
183
184 if (offset >= VDEV_LABEL_START_SIZE &&
185 offset < vd->vdev_psize - VDEV_LABEL_END_SIZE)
186 return (0);
187
188 rw_enter(&inject_lock, RW_READER);
189
190 for (handler = list_head(&inject_handlers); handler != NULL;
191 handler = list_next(&inject_handlers, handler)) {
192 uint64_t start = handler->zi_record.zi_start;
193 uint64_t end = handler->zi_record.zi_end;
194
195 if (handler->zi_record.zi_cmd != ZINJECT_LABEL_FAULT)
196 continue;
197
198 /*
199 * The injection region is the relative offsets within a
200 * vdev label. We must determine the label which is being
201 * updated and adjust our region accordingly.
202 */
203 label = vdev_label_number(vd->vdev_psize, offset);
204 start = vdev_label_offset(vd->vdev_psize, label, start);
205 end = vdev_label_offset(vd->vdev_psize, label, end);
206
207 if (zio->io_vd->vdev_guid == handler->zi_record.zi_guid &&
208 (offset >= start && offset <= end)) {
209 ret = error;
210 break;
211 }
212 }
213 rw_exit(&inject_lock);
214 return (ret);
215 }
216
217
218 int
219 zio_handle_device_injection(vdev_t *vd, zio_t *zio, int error)
220 {
221 inject_handler_t *handler;
222 int ret = 0;
223
224 /*
225 * We skip over faults in the labels unless it's during
226 * device open (i.e. zio == NULL).
227 */
228 if (zio != NULL) {
229 uint64_t offset = zio->io_offset;
230
231 if (offset < VDEV_LABEL_START_SIZE ||
232 offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE)
233 return (0);
234 }
235
236 rw_enter(&inject_lock, RW_READER);
237
238 for (handler = list_head(&inject_handlers); handler != NULL;
239 handler = list_next(&inject_handlers, handler)) {
240
241 if (handler->zi_record.zi_cmd != ZINJECT_DEVICE_FAULT)
242 continue;
243
244 if (vd->vdev_guid == handler->zi_record.zi_guid) {
245 if (handler->zi_record.zi_failfast &&
246 (zio == NULL || (zio->io_flags &
247 (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))) {
248 continue;
249 }
250
251 /* Handle type specific I/O failures */
252 if (zio != NULL &&
253 handler->zi_record.zi_iotype != ZIO_TYPES &&
254 handler->zi_record.zi_iotype != zio->io_type)
255 continue;
256
257 if (handler->zi_record.zi_error == error) {
258 /*
259 * For a failed open, pretend like the device
260 * has gone away.
261 */
262 if (error == ENXIO)
263 vd->vdev_stat.vs_aux =
264 VDEV_AUX_OPEN_FAILED;
265
266 /*
267 * Treat these errors as if they had been
268 * retried so that all the appropriate stats
269 * and FMA events are generated.
270 */
271 if (!handler->zi_record.zi_failfast &&
272 zio != NULL)
273 zio->io_flags |= ZIO_FLAG_IO_RETRY;
274
275 ret = error;
276 break;
277 }
278 if (handler->zi_record.zi_error == ENXIO) {
279 ret = SET_ERROR(EIO);
280 break;
281 }
282 }
283 }
284
285 rw_exit(&inject_lock);
286
287 return (ret);
288 }
289
290 /*
291 * Simulate hardware that ignores cache flushes. For requested number
292 * of seconds nix the actual writing to disk.
293 */
294 void
295 zio_handle_ignored_writes(zio_t *zio)
296 {
297 inject_handler_t *handler;
298
299 rw_enter(&inject_lock, RW_READER);
300
301 for (handler = list_head(&inject_handlers); handler != NULL;
302 handler = list_next(&inject_handlers, handler)) {
303
304 /* Ignore errors not destined for this pool */
305 if (zio->io_spa != handler->zi_spa ||
306 handler->zi_record.zi_cmd != ZINJECT_IGNORED_WRITES)
307 continue;
308
309 /*
310 * Positive duration implies # of seconds, negative
311 * a number of txgs
312 */
313 if (handler->zi_record.zi_timer == 0) {
314 if (handler->zi_record.zi_duration > 0)
315 handler->zi_record.zi_timer = ddi_get_lbolt64();
316 else
317 handler->zi_record.zi_timer = zio->io_txg;
318 }
319
320 /* Have a "problem" writing 60% of the time */
321 if (spa_get_random(100) < 60)
322 zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
323 break;
324 }
325
326 rw_exit(&inject_lock);
327 }
328
329 void
330 spa_handle_ignored_writes(spa_t *spa)
331 {
332 inject_handler_t *handler;
333
334 if (zio_injection_enabled == 0)
335 return;
336
337 rw_enter(&inject_lock, RW_READER);
338
339 for (handler = list_head(&inject_handlers); handler != NULL;
340 handler = list_next(&inject_handlers, handler)) {
341
342 if (spa != handler->zi_spa ||
343 handler->zi_record.zi_cmd != ZINJECT_IGNORED_WRITES)
344 continue;
345
346 if (handler->zi_record.zi_duration > 0) {
347 VERIFY(handler->zi_record.zi_timer == 0 ||
348 ddi_time_after64(
349 (int64_t)handler->zi_record.zi_timer +
350 handler->zi_record.zi_duration * hz,
351 ddi_get_lbolt64()));
352 } else {
353 /* duration is negative so the subtraction here adds */
354 VERIFY(handler->zi_record.zi_timer == 0 ||
355 handler->zi_record.zi_timer -
356 handler->zi_record.zi_duration >=
357 spa_syncing_txg(spa));
358 }
359 }
360
361 rw_exit(&inject_lock);
362 }
363
364 uint64_t
365 zio_handle_io_delay(zio_t *zio)
366 {
367 vdev_t *vd = zio->io_vd;
368 inject_handler_t *handler;
369 uint64_t seconds = 0;
370
371 if (zio_injection_enabled == 0)
372 return (0);
373
374 rw_enter(&inject_lock, RW_READER);
375
376 for (handler = list_head(&inject_handlers); handler != NULL;
377 handler = list_next(&inject_handlers, handler)) {
378
379 if (handler->zi_record.zi_cmd != ZINJECT_DELAY_IO)
380 continue;
381
382 if (vd->vdev_guid == handler->zi_record.zi_guid) {
383 seconds = handler->zi_record.zi_timer;
384 break;
385 }
386
387 }
388 rw_exit(&inject_lock);
389 return (seconds);
390 }
391
392 /*
393 * Create a new handler for the given record. We add it to the list, adding
394 * a reference to the spa_t in the process. We increment zio_injection_enabled,
395 * which is the switch to trigger all fault injection.
396 */
397 int
398 zio_inject_fault(char *name, int flags, int *id, zinject_record_t *record)
399 {
400 inject_handler_t *handler;
401 int error;
402 spa_t *spa;
403
404 /*
405 * If this is pool-wide metadata, make sure we unload the corresponding
406 * spa_t, so that the next attempt to load it will trigger the fault.
407 * We call spa_reset() to unload the pool appropriately.
408 */
409 if (flags & ZINJECT_UNLOAD_SPA)
410 if ((error = spa_reset(name)) != 0)
411 return (error);
412
413 if (!(flags & ZINJECT_NULL)) {
414 /*
415 * spa_inject_ref() will add an injection reference, which will
416 * prevent the pool from being removed from the namespace while
417 * still allowing it to be unloaded.
418 */
419 if ((spa = spa_inject_addref(name)) == NULL)
420 return (SET_ERROR(ENOENT));
421
422 handler = kmem_alloc(sizeof (inject_handler_t), KM_SLEEP);
423
424 rw_enter(&inject_lock, RW_WRITER);
425
426 *id = handler->zi_id = inject_next_id++;
427 handler->zi_spa = spa;
428 handler->zi_record = *record;
429 list_insert_tail(&inject_handlers, handler);
430 atomic_add_32(&zio_injection_enabled, 1);
431
432 rw_exit(&inject_lock);
433 }
434
435 /*
436 * Flush the ARC, so that any attempts to read this data will end up
437 * going to the ZIO layer. Note that this is a little overkill, but
438 * we don't have the necessary ARC interfaces to do anything else, and
439 * fault injection isn't a performance critical path.
440 */
441 if (flags & ZINJECT_FLUSH_ARC)
442 arc_flush(NULL);
443
444 return (0);
445 }
446
447 /*
448 * Returns the next record with an ID greater than that supplied to the
449 * function. Used to iterate over all handlers in the system.
450 */
451 int
452 zio_inject_list_next(int *id, char *name, size_t buflen,
453 zinject_record_t *record)
454 {
455 inject_handler_t *handler;
456 int ret;
457
458 mutex_enter(&spa_namespace_lock);
459 rw_enter(&inject_lock, RW_READER);
460
461 for (handler = list_head(&inject_handlers); handler != NULL;
462 handler = list_next(&inject_handlers, handler))
463 if (handler->zi_id > *id)
464 break;
465
466 if (handler) {
467 *record = handler->zi_record;
468 *id = handler->zi_id;
469 (void) strncpy(name, spa_name(handler->zi_spa), buflen);
470 ret = 0;
471 } else {
472 ret = SET_ERROR(ENOENT);
473 }
474
475 rw_exit(&inject_lock);
476 mutex_exit(&spa_namespace_lock);
477
478 return (ret);
479 }
480
481 /*
482 * Clear the fault handler with the given identifier, or return ENOENT if none
483 * exists.
484 */
485 int
486 zio_clear_fault(int id)
487 {
488 inject_handler_t *handler;
489
490 rw_enter(&inject_lock, RW_WRITER);
491
492 for (handler = list_head(&inject_handlers); handler != NULL;
493 handler = list_next(&inject_handlers, handler))
494 if (handler->zi_id == id)
495 break;
496
497 if (handler == NULL) {
498 rw_exit(&inject_lock);
499 return (SET_ERROR(ENOENT));
500 }
501
502 list_remove(&inject_handlers, handler);
503 rw_exit(&inject_lock);
504
505 spa_inject_delref(handler->zi_spa);
506 kmem_free(handler, sizeof (inject_handler_t));
507 atomic_add_32(&zio_injection_enabled, -1);
508
509 return (0);
510 }
511
512 void
513 zio_inject_init(void)
514 {
515 rw_init(&inject_lock, NULL, RW_DEFAULT, NULL);
516 list_create(&inject_handlers, sizeof (inject_handler_t),
517 offsetof(inject_handler_t, zi_link));
518 }
519
520 void
521 zio_inject_fini(void)
522 {
523 list_destroy(&inject_handlers);
524 rw_destroy(&inject_lock);
525 }
526
527 #if defined(_KERNEL) && defined(HAVE_SPL)
528 EXPORT_SYMBOL(zio_injection_enabled);
529 EXPORT_SYMBOL(zio_inject_fault);
530 EXPORT_SYMBOL(zio_inject_list_next);
531 EXPORT_SYMBOL(zio_clear_fault);
532 EXPORT_SYMBOL(zio_handle_fault_injection);
533 EXPORT_SYMBOL(zio_handle_device_injection);
534 EXPORT_SYMBOL(zio_handle_label_injection);
535 #endif