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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * This file contains the functions which analyze the status of a pool. This
28 * include both the status of an active pool, as well as the status exported
29 * pools. Returns one of the ZPOOL_STATUS_* defines describing the status of
30 * the pool. This status is independent (to a certain degree) from the state of
31 * the pool. A pool's state describes only whether or not it is capable of
32 * providing the necessary fault tolerance for data. The status describes the
33 * overall status of devices. A pool that is online can still have a device
34 * that is experiencing errors.
36 * Only a subset of the possible faults can be detected using 'zpool status',
37 * and not all possible errors correspond to a FMA message ID. The explanation
38 * is left up to the caller, depending on whether it is a live pool or an
45 #include "libzfs_impl.h"
48 * Message ID table. This must be kept in sync with the ZPOOL_STATUS_* defines
49 * in libzfs.h. Note that there are some status results which go past the end
50 * of this table, and hence have no associated message ID.
52 static char *zfs_msgid_table[] = {
69 #define NMSGID (sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
73 vdev_missing(uint64_t state, uint64_t aux, uint64_t errs)
75 return (state == VDEV_STATE_CANT_OPEN &&
76 aux == VDEV_AUX_OPEN_FAILED);
81 vdev_faulted(uint64_t state, uint64_t aux, uint64_t errs)
83 return (state == VDEV_STATE_FAULTED);
88 vdev_errors(uint64_t state, uint64_t aux, uint64_t errs)
90 return (state == VDEV_STATE_DEGRADED || errs != 0);
95 vdev_broken(uint64_t state, uint64_t aux, uint64_t errs)
97 return (state == VDEV_STATE_CANT_OPEN);
102 vdev_offlined(uint64_t state, uint64_t aux, uint64_t errs)
104 return (state == VDEV_STATE_OFFLINE);
108 * Detect if any leaf devices that have seen errors or could not be opened.
111 find_vdev_problem(nvlist_t *vdev, int (*func)(uint64_t, uint64_t, uint64_t))
119 * Ignore problems within a 'replacing' vdev, since we're presumably in
120 * the process of repairing any such errors, and don't want to call them
121 * out again. We'll pick up the fact that a resilver is happening
124 verify(nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type) == 0);
125 if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
128 if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child,
130 for (c = 0; c < children; c++)
131 if (find_vdev_problem(child[c], func))
134 verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_STATS,
135 (uint64_t **)&vs, &c) == 0);
137 if (func(vs->vs_state, vs->vs_aux,
139 vs->vs_write_errors +
140 vs->vs_checksum_errors))
148 * Active pool health status.
150 * To determine the status for a pool, we make several passes over the config,
151 * picking the most egregious error we find. In order of importance, we do the
154 * - Check for a complete and valid configuration
155 * - Look for any faulted or missing devices in a non-replicated config
156 * - Check for any data errors
157 * - Check for any faulted or missing devices in a replicated config
158 * - Look for any devices showing errors
159 * - Check for any resilvering devices
161 * There can obviously be multiple errors within a single pool, so this routine
162 * only picks the most damaging of all the current errors to report.
164 static zpool_status_t
165 check_status(nvlist_t *config, boolean_t isimport)
176 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
178 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
180 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
181 (uint64_t **)&vs, &vsc) == 0);
182 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
184 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
187 * Pool last accessed by another system.
189 if (hostid != 0 && (unsigned long)hostid != gethostid() &&
190 stateval == POOL_STATE_ACTIVE)
191 return (ZPOOL_STATUS_HOSTID_MISMATCH);
194 * Newer on-disk version.
196 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
197 vs->vs_aux == VDEV_AUX_VERSION_NEWER)
198 return (ZPOOL_STATUS_VERSION_NEWER);
201 * Check that the config is complete.
203 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
204 vs->vs_aux == VDEV_AUX_BAD_GUID_SUM)
205 return (ZPOOL_STATUS_BAD_GUID_SUM);
208 * Check whether the pool has suspended due to failed I/O.
210 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED,
212 if (suspended == ZIO_FAILURE_MODE_CONTINUE)
213 return (ZPOOL_STATUS_IO_FAILURE_CONTINUE);
214 return (ZPOOL_STATUS_IO_FAILURE_WAIT);
218 * Could not read a log.
220 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
221 vs->vs_aux == VDEV_AUX_BAD_LOG) {
222 return (ZPOOL_STATUS_BAD_LOG);
226 * Bad devices in non-replicated config.
228 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
229 find_vdev_problem(nvroot, vdev_faulted))
230 return (ZPOOL_STATUS_FAULTED_DEV_NR);
232 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
233 find_vdev_problem(nvroot, vdev_missing))
234 return (ZPOOL_STATUS_MISSING_DEV_NR);
236 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
237 find_vdev_problem(nvroot, vdev_broken))
238 return (ZPOOL_STATUS_CORRUPT_LABEL_NR);
241 * Corrupted pool metadata
243 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
244 vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
245 return (ZPOOL_STATUS_CORRUPT_POOL);
248 * Persistent data errors.
251 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
252 &nerr) == 0 && nerr != 0)
253 return (ZPOOL_STATUS_CORRUPT_DATA);
257 * Missing devices in a replicated config.
259 if (find_vdev_problem(nvroot, vdev_faulted))
260 return (ZPOOL_STATUS_FAULTED_DEV_R);
261 if (find_vdev_problem(nvroot, vdev_missing))
262 return (ZPOOL_STATUS_MISSING_DEV_R);
263 if (find_vdev_problem(nvroot, vdev_broken))
264 return (ZPOOL_STATUS_CORRUPT_LABEL_R);
267 * Devices with errors
269 if (!isimport && find_vdev_problem(nvroot, vdev_errors))
270 return (ZPOOL_STATUS_FAILING_DEV);
275 if (find_vdev_problem(nvroot, vdev_offlined))
276 return (ZPOOL_STATUS_OFFLINE_DEV);
279 * Currently resilvering
281 if (!vs->vs_scrub_complete && vs->vs_scrub_type == POOL_SCRUB_RESILVER)
282 return (ZPOOL_STATUS_RESILVERING);
285 * Outdated, but usable, version
287 if (version < SPA_VERSION)
288 return (ZPOOL_STATUS_VERSION_OLDER);
290 return (ZPOOL_STATUS_OK);
294 zpool_get_status(zpool_handle_t *zhp, char **msgid)
296 zpool_status_t ret = check_status(zhp->zpool_config, B_FALSE);
301 *msgid = zfs_msgid_table[ret];
307 zpool_import_status(nvlist_t *config, char **msgid)
309 zpool_status_t ret = check_status(config, B_TRUE);
314 *msgid = zfs_msgid_table[ret];