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]
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
28 #if !defined(_KERNEL) || !defined(__linux__)
31 kmutex_t zfs_dbgmsgs_lock
;
32 int zfs_dbgmsg_maxsize
= 4<<20; /* 4MB */
36 * Enable various debugging features.
41 * zfs_recover can be set to nonzero to attempt to recover from
42 * otherwise-fatal errors, typically caused by on-disk corruption. When
43 * set, calls to zfs_panic_recover() will turn into warning messages.
44 * This should only be used as a last resort, as it typically results
45 * in leaked space, or worse.
47 int zfs_recover
= B_FALSE
;
50 * If destroy encounters an EIO while reading metadata (e.g. indirect
51 * blocks), space referenced by the missing metadata can not be freed.
52 * Normally this causes the background destroy to become "stalled", as
53 * it is unable to make forward progress. While in this stalled state,
54 * all remaining space to free from the error-encountering filesystem is
55 * "temporarily leaked". Set this flag to cause it to ignore the EIO,
56 * permanently leak the space from indirect blocks that can not be read,
57 * and continue to free everything else that it can.
59 * The default, "stalling" behavior is useful if the storage partially
60 * fails (i.e. some but not all i/os fail), and then later recovers. In
61 * this case, we will be able to continue pool operations while it is
62 * partially failed, and when it recovers, we can continue to free the
63 * space, with no leaks. However, note that this case is actually
66 * Typically pools either (a) fail completely (but perhaps temporarily,
67 * e.g. a top-level vdev going offline), or (b) have localized,
68 * permanent errors (e.g. disk returns the wrong data due to bit flip or
69 * firmware bug). In case (a), this setting does not matter because the
70 * pool will be suspended and the sync thread will not be able to make
71 * forward progress regardless. In case (b), because the error is
72 * permanent, the best we can do is leak the minimum amount of space,
73 * which is what setting this flag will do. Therefore, it is reasonable
74 * for this flag to normally be set, but we chose the more conservative
75 * approach of not setting it, so that there is no possibility of
76 * leaking space in the "partial temporary" failure case.
78 int zfs_free_leak_on_eio
= B_FALSE
;
82 zfs_panic_recover(const char *fmt
, ...)
87 vcmn_err(zfs_recover
? CE_WARN
: CE_PANIC
, fmt
, adx
);
92 * Debug logging is enabled by default for production kernel builds.
93 * The overhead for this is negligible and the logs can be valuable when
94 * debugging. For non-production user space builds all debugging except
95 * logging is enabled since performance is no longer a concern.
100 #if !defined(_KERNEL) || !defined(__linux__)
101 list_create(&zfs_dbgmsgs
, sizeof (zfs_dbgmsg_t
),
102 offsetof(zfs_dbgmsg_t
, zdm_node
));
103 mutex_init(&zfs_dbgmsgs_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
106 if (zfs_flags
== 0) {
108 zfs_flags
= ZFS_DEBUG_DPRINTF
;
109 spl_debug_set_mask(spl_debug_get_mask() | SD_DPRINTF
);
110 spl_debug_set_subsys(spl_debug_get_subsys() | SS_USER1
);
112 zfs_flags
= ~ZFS_DEBUG_DPRINTF
;
118 zfs_dbgmsg_fini(void)
120 #if !defined(_KERNEL) || !defined(__linux__)
123 while ((zdm
= list_remove_head(&zfs_dbgmsgs
)) != NULL
) {
124 int size
= sizeof (zfs_dbgmsg_t
) + strlen(zdm
->zdm_msg
);
125 kmem_free(zdm
, size
);
126 zfs_dbgmsg_size
-= size
;
128 mutex_destroy(&zfs_dbgmsgs_lock
);
129 ASSERT0(zfs_dbgmsg_size
);
133 #if !defined(_KERNEL) || !defined(__linux__)
135 * Print these messages by running:
136 * echo ::zfs_dbgmsg | mdb -k
138 * Monitor these messages by running:
139 * dtrace -qn 'zfs-dbgmsg{printf("%s\n", stringof(arg0))}'
141 * When used with libzpool, monitor with:
142 * dtrace -qn 'zfs$pid::zfs_dbgmsg:probe1{printf("%s\n", copyinstr(arg1))}'
145 zfs_dbgmsg(const char *fmt
, ...)
152 size
= vsnprintf(NULL
, 0, fmt
, adx
);
156 * There is one byte of string in sizeof (zfs_dbgmsg_t), used
157 * for the terminating null.
159 zdm
= kmem_alloc(sizeof (zfs_dbgmsg_t
) + size
, KM_SLEEP
);
160 zdm
->zdm_timestamp
= gethrestime_sec();
163 (void) vsnprintf(zdm
->zdm_msg
, size
+ 1, fmt
, adx
);
166 DTRACE_PROBE1(zfs__dbgmsg
, char *, zdm
->zdm_msg
);
168 mutex_enter(&zfs_dbgmsgs_lock
);
169 list_insert_tail(&zfs_dbgmsgs
, zdm
);
170 zfs_dbgmsg_size
+= sizeof (zfs_dbgmsg_t
) + size
;
171 while (zfs_dbgmsg_size
> zfs_dbgmsg_maxsize
) {
172 zdm
= list_remove_head(&zfs_dbgmsgs
);
173 size
= sizeof (zfs_dbgmsg_t
) + strlen(zdm
->zdm_msg
);
174 kmem_free(zdm
, size
);
175 zfs_dbgmsg_size
-= size
;
177 mutex_exit(&zfs_dbgmsgs_lock
);
181 zfs_dbgmsg_print(const char *tag
)
185 (void) printf("ZFS_DBGMSG(%s):\n", tag
);
186 mutex_enter(&zfs_dbgmsgs_lock
);
187 for (zdm
= list_head(&zfs_dbgmsgs
); zdm
;
188 zdm
= list_next(&zfs_dbgmsgs
, zdm
))
189 (void) printf("%s\n", zdm
->zdm_msg
);
190 mutex_exit(&zfs_dbgmsgs_lock
);
195 module_param(zfs_flags
, int, 0644);
196 MODULE_PARM_DESC(zfs_flags
, "Set additional debugging flags");
198 module_param(zfs_recover
, int, 0644);
199 MODULE_PARM_DESC(zfs_recover
, "Set to attempt to recover from fatal errors");
201 module_param(zfs_free_leak_on_eio
, int, 0644);
202 MODULE_PARM_DESC(zfs_free_leak_on_eio
,
203 "Set to ignore IO errors during free and permanently leak the space");