1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22ea8dc4b6Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens /*
29fa9e4066Sahrens  * This file contains the functions which analyze the status of a pool.  This
30fa9e4066Sahrens  * include both the status of an active pool, as well as the status exported
31fa9e4066Sahrens  * pools.  Returns one of the ZPOOL_STATUS_* defines describing the status of
32fa9e4066Sahrens  * the pool.  This status is independent (to a certain degree) from the state of
33fa9e4066Sahrens  * the pool.  A pool's state descsribes only whether or not it is capable of
34fa9e4066Sahrens  * providing the necessary fault tolerance for data.  The status describes the
35fa9e4066Sahrens  * overall status of devices.  A pool that is online can still have a device
36fa9e4066Sahrens  * that is experiencing errors.
37fa9e4066Sahrens  *
38fa9e4066Sahrens  * Only a subset of the possible faults can be detected using 'zpool status',
39fa9e4066Sahrens  * and not all possible errors correspond to a FMA message ID.  The explanation
40fa9e4066Sahrens  * is left up to the caller, depending on whether it is a live pool or an
41fa9e4066Sahrens  * import.
42fa9e4066Sahrens  */
43fa9e4066Sahrens 
44fa9e4066Sahrens #include <libzfs.h>
45fa9e4066Sahrens #include <string.h>
46fa9e4066Sahrens #include "libzfs_impl.h"
47fa9e4066Sahrens 
48fa9e4066Sahrens /*
49fa9e4066Sahrens  * Message ID table.  This must be kep in sync with the ZPOOL_STATUS_* defines
50fa9e4066Sahrens  * in libzfs.h.  Note that there are some status results which go past the end
51fa9e4066Sahrens  * of this table, and hence have no associated message ID.
52fa9e4066Sahrens  */
53fa9e4066Sahrens static char *msgid_table[] = {
54fa9e4066Sahrens 	"ZFS-8000-14",
55fa9e4066Sahrens 	"ZFS-8000-2Q",
56fa9e4066Sahrens 	"ZFS-8000-3C",
57fa9e4066Sahrens 	"ZFS-8000-4J",
58fa9e4066Sahrens 	"ZFS-8000-5E",
59fa9e4066Sahrens 	"ZFS-8000-6X",
60fa9e4066Sahrens 	"ZFS-8000-72",
61fa9e4066Sahrens 	"ZFS-8000-8A",
62fa9e4066Sahrens 	"ZFS-8000-9P",
63fa9e4066Sahrens 	"ZFS-8000-A5"
64fa9e4066Sahrens };
65fa9e4066Sahrens 
66ea8dc4b6Seschrock /*
67ea8dc4b6Seschrock  * If the pool is active, a certain class of static errors is overridden by the
68ea8dc4b6Seschrock  * faults as analayzed by FMA.  These faults have separate knowledge articles,
69ea8dc4b6Seschrock  * and the article referred to by 'zpool status' must match that indicated by
70ea8dc4b6Seschrock  * the syslog error message.  We override missing data as well as corrupt pool.
71ea8dc4b6Seschrock  */
72ea8dc4b6Seschrock static char *msgid_table_active[] = {
73ea8dc4b6Seschrock 	"ZFS-8000-14",
74ea8dc4b6Seschrock 	"ZFS-8000-D3",		/* overridden */
75ea8dc4b6Seschrock 	"ZFS-8000-D3",		/* overridden */
76ea8dc4b6Seschrock 	"ZFS-8000-4J",
77ea8dc4b6Seschrock 	"ZFS-8000-5E",
78ea8dc4b6Seschrock 	"ZFS-8000-6X",
79ea8dc4b6Seschrock 	"ZFS-8000-CS",		/* overridden */
80ea8dc4b6Seschrock 	"ZFS-8000-8A",
81ea8dc4b6Seschrock 	"ZFS-8000-9P",
82ea8dc4b6Seschrock 	"ZFS-8000-CS",		/* overridden */
83ea8dc4b6Seschrock };
84ea8dc4b6Seschrock 
85fa9e4066Sahrens #define	NMSGID	(sizeof (msgid_table) / sizeof (msgid_table[0]))
86fa9e4066Sahrens 
87fa9e4066Sahrens /* ARGSUSED */
88fa9e4066Sahrens static int
89fa9e4066Sahrens vdev_missing(uint64_t state, uint64_t aux, uint64_t errs)
90fa9e4066Sahrens {
91fa9e4066Sahrens 	return (state == VDEV_STATE_CANT_OPEN &&
92fa9e4066Sahrens 	    aux == VDEV_AUX_OPEN_FAILED);
93fa9e4066Sahrens }
94fa9e4066Sahrens 
95fa9e4066Sahrens /* ARGSUSED */
96fa9e4066Sahrens static int
97fa9e4066Sahrens vdev_errors(uint64_t state, uint64_t aux, uint64_t errs)
98fa9e4066Sahrens {
99fa9e4066Sahrens 	return (errs != 0);
100fa9e4066Sahrens }
101fa9e4066Sahrens 
102fa9e4066Sahrens /* ARGSUSED */
103fa9e4066Sahrens static int
104fa9e4066Sahrens vdev_broken(uint64_t state, uint64_t aux, uint64_t errs)
105fa9e4066Sahrens {
106fa9e4066Sahrens 	return (state == VDEV_STATE_CANT_OPEN);
107fa9e4066Sahrens }
108fa9e4066Sahrens 
109fa9e4066Sahrens /* ARGSUSED */
110fa9e4066Sahrens static int
111fa9e4066Sahrens vdev_offlined(uint64_t state, uint64_t aux, uint64_t errs)
112fa9e4066Sahrens {
113fa9e4066Sahrens 	return (state == VDEV_STATE_OFFLINE);
114fa9e4066Sahrens }
115fa9e4066Sahrens 
116fa9e4066Sahrens /*
117fa9e4066Sahrens  * Detect if any leaf devices that have seen errors or could not be opened.
118fa9e4066Sahrens  */
119fa9e4066Sahrens static int
120fa9e4066Sahrens find_vdev_problem(nvlist_t *vdev, int (*func)(uint64_t, uint64_t, uint64_t))
121fa9e4066Sahrens {
122fa9e4066Sahrens 	nvlist_t **child;
123fa9e4066Sahrens 	vdev_stat_t *vs;
124fa9e4066Sahrens 	uint_t c, children;
125fa9e4066Sahrens 	char *type;
126fa9e4066Sahrens 
127fa9e4066Sahrens 	/*
128fa9e4066Sahrens 	 * Ignore problems within a 'replacing' vdev, since we're presumably in
129fa9e4066Sahrens 	 * the process of repairing any such errors, and don't want to call them
130fa9e4066Sahrens 	 * out again.  We'll pick up the fact that a resilver is happening
131fa9e4066Sahrens 	 * later.
132fa9e4066Sahrens 	 */
133fa9e4066Sahrens 	verify(nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type) == 0);
134fa9e4066Sahrens 	if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
135fa9e4066Sahrens 		return (FALSE);
136fa9e4066Sahrens 
137fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child,
138fa9e4066Sahrens 	    &children) == 0) {
139fa9e4066Sahrens 		for (c = 0; c < children; c++)
140fa9e4066Sahrens 			if (find_vdev_problem(child[c], func))
141fa9e4066Sahrens 				return (TRUE);
142fa9e4066Sahrens 	} else {
143fa9e4066Sahrens 		verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_STATS,
144fa9e4066Sahrens 		    (uint64_t **)&vs, &c) == 0);
145fa9e4066Sahrens 
146fa9e4066Sahrens 		if (func(vs->vs_state, vs->vs_aux,
147fa9e4066Sahrens 		    vs->vs_read_errors +
148fa9e4066Sahrens 		    vs->vs_write_errors +
149fa9e4066Sahrens 		    vs->vs_checksum_errors))
150fa9e4066Sahrens 			return (TRUE);
151fa9e4066Sahrens 	}
152fa9e4066Sahrens 
153fa9e4066Sahrens 	return (FALSE);
154fa9e4066Sahrens }
155fa9e4066Sahrens 
156fa9e4066Sahrens /*
157fa9e4066Sahrens  * Active pool health status.
158fa9e4066Sahrens  *
159fa9e4066Sahrens  * To determine the status for a pool, we make several passes over the config,
160fa9e4066Sahrens  * picking the most egregious error we find.  In order of importance, we do the
161fa9e4066Sahrens  * following:
162fa9e4066Sahrens  *
163fa9e4066Sahrens  *	- Check for a complete and valid configuration
164ea8dc4b6Seschrock  *	- Look for any missing devices in a non-replicated config
165fa9e4066Sahrens  *	- Check for any data errors
166ea8dc4b6Seschrock  *	- Check for any missing devices in a replicated config
167ea8dc4b6Seschrock  *	- Look for any devices showing errors
168fa9e4066Sahrens  *	- Check for any resilvering devices
169fa9e4066Sahrens  *
170fa9e4066Sahrens  * There can obviously be multiple errors within a single pool, so this routine
171fa9e4066Sahrens  * only picks the most damaging of all the current errors to report.
172fa9e4066Sahrens  */
173fa9e4066Sahrens static zpool_status_t
174fa9e4066Sahrens check_status(nvlist_t *config, int isimport)
175fa9e4066Sahrens {
176fa9e4066Sahrens 	nvlist_t *nvroot;
177fa9e4066Sahrens 	vdev_stat_t *vs;
178fa9e4066Sahrens 	uint_t vsc;
179ea8dc4b6Seschrock 	uint64_t nerr;
180*eaca9bbdSeschrock 	uint64_t version;
181fa9e4066Sahrens 
182*eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
183*eaca9bbdSeschrock 	    &version) == 0);
184fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
185fa9e4066Sahrens 	    &nvroot) == 0);
186fa9e4066Sahrens 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS,
187fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
188fa9e4066Sahrens 
189*eaca9bbdSeschrock 	/*
190*eaca9bbdSeschrock 	 * Newer on-disk version.
191*eaca9bbdSeschrock 	 */
192*eaca9bbdSeschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
193*eaca9bbdSeschrock 	    vs->vs_aux == VDEV_AUX_VERSION_NEWER)
194*eaca9bbdSeschrock 		return (ZPOOL_STATUS_VERSION_NEWER);
195*eaca9bbdSeschrock 
196fa9e4066Sahrens 	/*
197fa9e4066Sahrens 	 * Check that the config is complete.
198fa9e4066Sahrens 	 */
199fa9e4066Sahrens 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
200ea8dc4b6Seschrock 	    vs->vs_aux == VDEV_AUX_BAD_GUID_SUM)
201fa9e4066Sahrens 		return (ZPOOL_STATUS_BAD_GUID_SUM);
202fa9e4066Sahrens 
203fa9e4066Sahrens 	/*
204ea8dc4b6Seschrock 	 * Missing devices in non-replicated config.
205fa9e4066Sahrens 	 */
206ea8dc4b6Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
207ea8dc4b6Seschrock 	    find_vdev_problem(nvroot, vdev_missing))
208ea8dc4b6Seschrock 		return (ZPOOL_STATUS_MISSING_DEV_NR);
209ea8dc4b6Seschrock 
210ea8dc4b6Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
211ea8dc4b6Seschrock 	    find_vdev_problem(nvroot, vdev_broken))
212ea8dc4b6Seschrock 		return (ZPOOL_STATUS_CORRUPT_LABEL_NR);
213ea8dc4b6Seschrock 
214ea8dc4b6Seschrock 	/*
215ea8dc4b6Seschrock 	 * Corrupted pool metadata
216ea8dc4b6Seschrock 	 */
217ea8dc4b6Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
218ea8dc4b6Seschrock 	    vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
219ea8dc4b6Seschrock 		return (ZPOOL_STATUS_CORRUPT_POOL);
220fa9e4066Sahrens 
221fa9e4066Sahrens 	/*
222ea8dc4b6Seschrock 	 * Persistent data errors.
223fa9e4066Sahrens 	 */
224ea8dc4b6Seschrock 	if (!isimport) {
225ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
226ea8dc4b6Seschrock 		    &nerr) == 0 && nerr != 0)
227ea8dc4b6Seschrock 			return (ZPOOL_STATUS_CORRUPT_DATA);
228fa9e4066Sahrens 	}
229fa9e4066Sahrens 
230ea8dc4b6Seschrock 	/*
231ea8dc4b6Seschrock 	 * Missing devices in a replicated config.
232ea8dc4b6Seschrock 	 */
233ea8dc4b6Seschrock 	if (find_vdev_problem(nvroot, vdev_missing))
234ea8dc4b6Seschrock 		return (ZPOOL_STATUS_MISSING_DEV_R);
235ea8dc4b6Seschrock 	if (find_vdev_problem(nvroot, vdev_broken))
236ea8dc4b6Seschrock 		return (ZPOOL_STATUS_CORRUPT_LABEL_R);
237ea8dc4b6Seschrock 
238fa9e4066Sahrens 	/*
239fa9e4066Sahrens 	 * Devices with errors
240fa9e4066Sahrens 	 */
241fa9e4066Sahrens 	if (!isimport && find_vdev_problem(nvroot, vdev_errors))
242fa9e4066Sahrens 		return (ZPOOL_STATUS_FAILING_DEV);
243fa9e4066Sahrens 
244fa9e4066Sahrens 	/*
245fa9e4066Sahrens 	 * Offlined devices
246fa9e4066Sahrens 	 */
247fa9e4066Sahrens 	if (find_vdev_problem(nvroot, vdev_offlined))
248fa9e4066Sahrens 		return (ZPOOL_STATUS_OFFLINE_DEV);
249fa9e4066Sahrens 
250fa9e4066Sahrens 	/*
251fa9e4066Sahrens 	 * Currently resilvering
252fa9e4066Sahrens 	 */
253fa9e4066Sahrens 	if (!vs->vs_scrub_complete && vs->vs_scrub_type == POOL_SCRUB_RESILVER)
254fa9e4066Sahrens 		return (ZPOOL_STATUS_RESILVERING);
255fa9e4066Sahrens 
256fa9e4066Sahrens 	/*
257*eaca9bbdSeschrock 	 * Outdated, but usable, version
258fa9e4066Sahrens 	 */
259*eaca9bbdSeschrock 	if (version < ZFS_VERSION)
260*eaca9bbdSeschrock 		return (ZPOOL_STATUS_VERSION_OLDER);
261fa9e4066Sahrens 
262fa9e4066Sahrens 	return (ZPOOL_STATUS_OK);
263fa9e4066Sahrens }
264fa9e4066Sahrens 
265fa9e4066Sahrens zpool_status_t
266fa9e4066Sahrens zpool_get_status(zpool_handle_t *zhp, char **msgid)
267fa9e4066Sahrens {
268fa9e4066Sahrens 	zpool_status_t ret = check_status(zhp->zpool_config, FALSE);
269fa9e4066Sahrens 
270fa9e4066Sahrens 	if (ret >= NMSGID)
271fa9e4066Sahrens 		*msgid = NULL;
272fa9e4066Sahrens 	else
273ea8dc4b6Seschrock 		*msgid = msgid_table_active[ret];
274fa9e4066Sahrens 
275fa9e4066Sahrens 	return (ret);
276fa9e4066Sahrens }
277fa9e4066Sahrens 
278fa9e4066Sahrens zpool_status_t
279fa9e4066Sahrens zpool_import_status(nvlist_t *config, char **msgid)
280fa9e4066Sahrens {
281fa9e4066Sahrens 	zpool_status_t ret = check_status(config, TRUE);
282fa9e4066Sahrens 
283fa9e4066Sahrens 	if (ret >= NMSGID)
284fa9e4066Sahrens 		*msgid = NULL;
285fa9e4066Sahrens 	else
286fa9e4066Sahrens 		*msgid = msgid_table[ret];
287fa9e4066Sahrens 
288fa9e4066Sahrens 	return (ret);
289fa9e4066Sahrens }
290