1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*6523Sek110237  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens /*
29789Sahrens  * This file contains the functions which analyze the status of a pool.  This
30789Sahrens  * include both the status of an active pool, as well as the status exported
31789Sahrens  * pools.  Returns one of the ZPOOL_STATUS_* defines describing the status of
32789Sahrens  * the pool.  This status is independent (to a certain degree) from the state of
334451Seschrock  * the pool.  A pool's state describes only whether or not it is capable of
34789Sahrens  * providing the necessary fault tolerance for data.  The status describes the
35789Sahrens  * overall status of devices.  A pool that is online can still have a device
36789Sahrens  * that is experiencing errors.
37789Sahrens  *
38789Sahrens  * Only a subset of the possible faults can be detected using 'zpool status',
39789Sahrens  * and not all possible errors correspond to a FMA message ID.  The explanation
40789Sahrens  * is left up to the caller, depending on whether it is a live pool or an
41789Sahrens  * import.
42789Sahrens  */
43789Sahrens 
44789Sahrens #include <libzfs.h>
45789Sahrens #include <string.h>
463975Sek110237 #include <unistd.h>
47789Sahrens #include "libzfs_impl.h"
48789Sahrens 
49789Sahrens /*
504451Seschrock  * Message ID table.  This must be kept in sync with the ZPOOL_STATUS_* defines
51789Sahrens  * in libzfs.h.  Note that there are some status results which go past the end
52789Sahrens  * of this table, and hence have no associated message ID.
53789Sahrens  */
543975Sek110237 static char *zfs_msgid_table[] = {
55789Sahrens 	"ZFS-8000-14",
56789Sahrens 	"ZFS-8000-2Q",
57789Sahrens 	"ZFS-8000-3C",
58789Sahrens 	"ZFS-8000-4J",
59789Sahrens 	"ZFS-8000-5E",
60789Sahrens 	"ZFS-8000-6X",
61789Sahrens 	"ZFS-8000-72",
62789Sahrens 	"ZFS-8000-8A",
63789Sahrens 	"ZFS-8000-9P",
643975Sek110237 	"ZFS-8000-A5",
65*6523Sek110237 	"ZFS-8000-EY",
66*6523Sek110237 	"ZFS-8000-HC",
67*6523Sek110237 	"ZFS-8000-JQ"
68789Sahrens };
69789Sahrens 
703975Sek110237 #define	NMSGID	(sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
71789Sahrens 
72789Sahrens /* ARGSUSED */
73789Sahrens static int
74789Sahrens vdev_missing(uint64_t state, uint64_t aux, uint64_t errs)
75789Sahrens {
76789Sahrens 	return (state == VDEV_STATE_CANT_OPEN &&
77789Sahrens 	    aux == VDEV_AUX_OPEN_FAILED);
78789Sahrens }
79789Sahrens 
80789Sahrens /* ARGSUSED */
81789Sahrens static int
824451Seschrock vdev_faulted(uint64_t state, uint64_t aux, uint64_t errs)
834451Seschrock {
844451Seschrock 	return (state == VDEV_STATE_FAULTED);
854451Seschrock }
864451Seschrock 
874451Seschrock /* ARGSUSED */
884451Seschrock static int
89789Sahrens vdev_errors(uint64_t state, uint64_t aux, uint64_t errs)
90789Sahrens {
914451Seschrock 	return (state == VDEV_STATE_DEGRADED || errs != 0);
92789Sahrens }
93789Sahrens 
94789Sahrens /* ARGSUSED */
95789Sahrens static int
96789Sahrens vdev_broken(uint64_t state, uint64_t aux, uint64_t errs)
97789Sahrens {
98789Sahrens 	return (state == VDEV_STATE_CANT_OPEN);
99789Sahrens }
100789Sahrens 
101789Sahrens /* ARGSUSED */
102789Sahrens static int
103789Sahrens vdev_offlined(uint64_t state, uint64_t aux, uint64_t errs)
104789Sahrens {
105789Sahrens 	return (state == VDEV_STATE_OFFLINE);
106789Sahrens }
107789Sahrens 
108789Sahrens /*
109789Sahrens  * Detect if any leaf devices that have seen errors or could not be opened.
110789Sahrens  */
1112082Seschrock static boolean_t
112789Sahrens find_vdev_problem(nvlist_t *vdev, int (*func)(uint64_t, uint64_t, uint64_t))
113789Sahrens {
114789Sahrens 	nvlist_t **child;
115789Sahrens 	vdev_stat_t *vs;
116789Sahrens 	uint_t c, children;
117789Sahrens 	char *type;
118789Sahrens 
119789Sahrens 	/*
120789Sahrens 	 * Ignore problems within a 'replacing' vdev, since we're presumably in
121789Sahrens 	 * the process of repairing any such errors, and don't want to call them
122789Sahrens 	 * out again.  We'll pick up the fact that a resilver is happening
123789Sahrens 	 * later.
124789Sahrens 	 */
125789Sahrens 	verify(nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type) == 0);
126789Sahrens 	if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
1272082Seschrock 		return (B_FALSE);
128789Sahrens 
129789Sahrens 	if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child,
130789Sahrens 	    &children) == 0) {
131789Sahrens 		for (c = 0; c < children; c++)
132789Sahrens 			if (find_vdev_problem(child[c], func))
1332082Seschrock 				return (B_TRUE);
134789Sahrens 	} else {
135789Sahrens 		verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_STATS,
136789Sahrens 		    (uint64_t **)&vs, &c) == 0);
137789Sahrens 
138789Sahrens 		if (func(vs->vs_state, vs->vs_aux,
139789Sahrens 		    vs->vs_read_errors +
140789Sahrens 		    vs->vs_write_errors +
141789Sahrens 		    vs->vs_checksum_errors))
1422082Seschrock 			return (B_TRUE);
143789Sahrens 	}
144789Sahrens 
1452082Seschrock 	return (B_FALSE);
146789Sahrens }
147789Sahrens 
148789Sahrens /*
149789Sahrens  * Active pool health status.
150789Sahrens  *
151789Sahrens  * To determine the status for a pool, we make several passes over the config,
152789Sahrens  * picking the most egregious error we find.  In order of importance, we do the
153789Sahrens  * following:
154789Sahrens  *
155789Sahrens  *	- Check for a complete and valid configuration
1564451Seschrock  *	- Look for any faulted or missing devices in a non-replicated config
1571544Seschrock  *	- Check for any data errors
1584451Seschrock  *	- Check for any faulted or missing devices in a replicated config
159789Sahrens  *	- Look for any devices showing errors
160789Sahrens  *	- Check for any resilvering devices
161789Sahrens  *
162789Sahrens  * There can obviously be multiple errors within a single pool, so this routine
163789Sahrens  * only picks the most damaging of all the current errors to report.
164789Sahrens  */
165789Sahrens static zpool_status_t
166*6523Sek110237 check_status(zpool_handle_t *zhp, nvlist_t *config, boolean_t isimport)
167789Sahrens {
168789Sahrens 	nvlist_t *nvroot;
169789Sahrens 	vdev_stat_t *vs;
170789Sahrens 	uint_t vsc;
1711544Seschrock 	uint64_t nerr;
1721760Seschrock 	uint64_t version;
1733975Sek110237 	uint64_t stateval;
1743975Sek110237 	uint64_t hostid = 0;
175789Sahrens 
1761760Seschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1771760Seschrock 	    &version) == 0);
178789Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
179789Sahrens 	    &nvroot) == 0);
180789Sahrens 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
181789Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
1823975Sek110237 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1833975Sek110237 	    &stateval) == 0);
1843975Sek110237 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
1853975Sek110237 
1863975Sek110237 	/*
1873975Sek110237 	 * Pool last accessed by another system.
1883975Sek110237 	 */
1893975Sek110237 	if (hostid != 0 && (unsigned long)hostid != gethostid() &&
1903975Sek110237 	    stateval == POOL_STATE_ACTIVE)
1913975Sek110237 		return (ZPOOL_STATUS_HOSTID_MISMATCH);
192789Sahrens 
193789Sahrens 	/*
1941760Seschrock 	 * Newer on-disk version.
1951760Seschrock 	 */
1961760Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
1971760Seschrock 	    vs->vs_aux == VDEV_AUX_VERSION_NEWER)
1981760Seschrock 		return (ZPOOL_STATUS_VERSION_NEWER);
1991760Seschrock 
2001760Seschrock 	/*
201789Sahrens 	 * Check that the config is complete.
202789Sahrens 	 */
203789Sahrens 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
2041544Seschrock 	    vs->vs_aux == VDEV_AUX_BAD_GUID_SUM)
205789Sahrens 		return (ZPOOL_STATUS_BAD_GUID_SUM);
2061544Seschrock 
2071544Seschrock 	/*
208*6523Sek110237 	 * Pool has experienced failed I/O.
209*6523Sek110237 	 */
210*6523Sek110237 	if (stateval == POOL_STATE_IO_FAILURE) {
211*6523Sek110237 		zpool_handle_t *tmp_zhp = NULL;
212*6523Sek110237 		libzfs_handle_t *hdl = NULL;
213*6523Sek110237 		char property[ZPOOL_MAXPROPLEN];
214*6523Sek110237 		char *failmode = NULL;
215*6523Sek110237 
216*6523Sek110237 		if (zhp == NULL) {
217*6523Sek110237 			char *poolname;
218*6523Sek110237 
219*6523Sek110237 			verify(nvlist_lookup_string(config,
220*6523Sek110237 			    ZPOOL_CONFIG_POOL_NAME, &poolname) == 0);
221*6523Sek110237 			if ((hdl = libzfs_init()) == NULL)
222*6523Sek110237 				return (ZPOOL_STATUS_IO_FAILURE_WAIT);
223*6523Sek110237 			tmp_zhp = zpool_open_canfail(hdl, poolname);
224*6523Sek110237 			if (tmp_zhp == NULL) {
225*6523Sek110237 				libzfs_fini(hdl);
226*6523Sek110237 				return (ZPOOL_STATUS_IO_FAILURE_WAIT);
227*6523Sek110237 			}
228*6523Sek110237 		}
229*6523Sek110237 		if (zpool_get_prop(zhp ? zhp : tmp_zhp, ZPOOL_PROP_FAILUREMODE,
230*6523Sek110237 		    property, sizeof (property), NULL) == 0)
231*6523Sek110237 			failmode = property;
232*6523Sek110237 		if (tmp_zhp != NULL)
233*6523Sek110237 			zpool_close(tmp_zhp);
234*6523Sek110237 		if (hdl != NULL)
235*6523Sek110237 			libzfs_fini(hdl);
236*6523Sek110237 		if (failmode == NULL)
237*6523Sek110237 			return (ZPOOL_STATUS_IO_FAILURE_WAIT);
238*6523Sek110237 
239*6523Sek110237 		if (strncmp(failmode, "continue", strlen("continue")) == 0)
240*6523Sek110237 			return (ZPOOL_STATUS_IO_FAILURE_CONTINUE);
241*6523Sek110237 		else
242*6523Sek110237 			return (ZPOOL_STATUS_IO_FAILURE_WAIT);
243*6523Sek110237 	}
244*6523Sek110237 
245*6523Sek110237 	/*
2464451Seschrock 	 * Bad devices in non-replicated config.
2471544Seschrock 	 */
2481544Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
2494451Seschrock 	    find_vdev_problem(nvroot, vdev_faulted))
2504451Seschrock 		return (ZPOOL_STATUS_FAULTED_DEV_NR);
2514451Seschrock 
2524451Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
2531544Seschrock 	    find_vdev_problem(nvroot, vdev_missing))
2541544Seschrock 		return (ZPOOL_STATUS_MISSING_DEV_NR);
2551544Seschrock 
2561544Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
2571544Seschrock 	    find_vdev_problem(nvroot, vdev_broken))
2581544Seschrock 		return (ZPOOL_STATUS_CORRUPT_LABEL_NR);
2591544Seschrock 
2601544Seschrock 	/*
2611544Seschrock 	 * Corrupted pool metadata
2621544Seschrock 	 */
2631544Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
2641544Seschrock 	    vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
2651544Seschrock 		return (ZPOOL_STATUS_CORRUPT_POOL);
2661544Seschrock 
2671544Seschrock 	/*
2681544Seschrock 	 * Persistent data errors.
2691544Seschrock 	 */
2701544Seschrock 	if (!isimport) {
2711544Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
2721544Seschrock 		    &nerr) == 0 && nerr != 0)
2731544Seschrock 			return (ZPOOL_STATUS_CORRUPT_DATA);
274789Sahrens 	}
275789Sahrens 
276789Sahrens 	/*
2771544Seschrock 	 * Missing devices in a replicated config.
278789Sahrens 	 */
2794451Seschrock 	if (find_vdev_problem(nvroot, vdev_faulted))
2804451Seschrock 		return (ZPOOL_STATUS_FAULTED_DEV_R);
2811544Seschrock 	if (find_vdev_problem(nvroot, vdev_missing))
2821544Seschrock 		return (ZPOOL_STATUS_MISSING_DEV_R);
2831544Seschrock 	if (find_vdev_problem(nvroot, vdev_broken))
2841544Seschrock 		return (ZPOOL_STATUS_CORRUPT_LABEL_R);
285789Sahrens 
286789Sahrens 	/*
287789Sahrens 	 * Devices with errors
288789Sahrens 	 */
289789Sahrens 	if (!isimport && find_vdev_problem(nvroot, vdev_errors))
290789Sahrens 		return (ZPOOL_STATUS_FAILING_DEV);
291789Sahrens 
292789Sahrens 	/*
293789Sahrens 	 * Offlined devices
294789Sahrens 	 */
295789Sahrens 	if (find_vdev_problem(nvroot, vdev_offlined))
296789Sahrens 		return (ZPOOL_STATUS_OFFLINE_DEV);
297789Sahrens 
298789Sahrens 	/*
299789Sahrens 	 * Currently resilvering
300789Sahrens 	 */
301789Sahrens 	if (!vs->vs_scrub_complete && vs->vs_scrub_type == POOL_SCRUB_RESILVER)
302789Sahrens 		return (ZPOOL_STATUS_RESILVERING);
303789Sahrens 
304789Sahrens 	/*
3051760Seschrock 	 * Outdated, but usable, version
306789Sahrens 	 */
3074577Sahrens 	if (version < SPA_VERSION)
3081760Seschrock 		return (ZPOOL_STATUS_VERSION_OLDER);
309789Sahrens 
310789Sahrens 	return (ZPOOL_STATUS_OK);
311789Sahrens }
312789Sahrens 
313789Sahrens zpool_status_t
314789Sahrens zpool_get_status(zpool_handle_t *zhp, char **msgid)
315789Sahrens {
316*6523Sek110237 	zpool_status_t ret = check_status(zhp, zhp->zpool_config, B_FALSE);
317789Sahrens 
318789Sahrens 	if (ret >= NMSGID)
319789Sahrens 		*msgid = NULL;
320789Sahrens 	else
3214451Seschrock 		*msgid = zfs_msgid_table[ret];
322789Sahrens 
323789Sahrens 	return (ret);
324789Sahrens }
325789Sahrens 
326789Sahrens zpool_status_t
327789Sahrens zpool_import_status(nvlist_t *config, char **msgid)
328789Sahrens {
329*6523Sek110237 	zpool_status_t ret = check_status(NULL, config, B_TRUE);
330789Sahrens 
331789Sahrens 	if (ret >= NMSGID)
332789Sahrens 		*msgid = NULL;
333789Sahrens 	else
3343975Sek110237 		*msgid = zfs_msgid_table[ret];
335789Sahrens 
336789Sahrens 	return (ret);
337789Sahrens }
338