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/*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
25 * Copyright (c) 2013 Steven Hartland. All rights reserved.
26 */
27
28/*
29 * This file contains the functions which analyze the status of a pool.  This
30 * include both the status of an active pool, as well as the status exported
31 * pools.  Returns one of the ZPOOL_STATUS_* defines describing the status of
32 * the pool.  This status is independent (to a certain degree) from the state of
33 * the pool.  A pool's state describes only whether or not it is capable of
34 * providing the necessary fault tolerance for data.  The status describes the
35 * overall status of devices.  A pool that is online can still have a device
36 * that is experiencing errors.
37 *
38 * Only a subset of the possible faults can be detected using 'zpool status',
39 * and not all possible errors correspond to a FMA message ID.  The explanation
40 * is left up to the caller, depending on whether it is a live pool or an
41 * import.
42 */
43
44#include <libzfs.h>
45#include <string.h>
46#include <unistd.h>
47#include "libzfs_impl.h"
48#include "zfeature_common.h"
49
50/*
51 * Message ID table.  This must be kept in sync with the ZPOOL_STATUS_* defines
52 * in libzfs.h.  Note that there are some status results which go past the end
53 * of this table, and hence have no associated message ID.
54 */
55static char *zfs_msgid_table[] = {
56	"ZFS-8000-14",
57	"ZFS-8000-2Q",
58	"ZFS-8000-3C",
59	"ZFS-8000-4J",
60	"ZFS-8000-5E",
61	"ZFS-8000-6X",
62	"ZFS-8000-72",
63	"ZFS-8000-8A",
64	"ZFS-8000-9P",
65	"ZFS-8000-A5",
66	"ZFS-8000-EY",
67	"ZFS-8000-HC",
68	"ZFS-8000-JQ",
69	"ZFS-8000-K4",
70};
71
72#define	NMSGID	(sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
73
74/* ARGSUSED */
75static int
76vdev_missing(vdev_stat_t *vs, uint_t vsc)
77{
78	return (vs->vs_state == VDEV_STATE_CANT_OPEN &&
79	    vs->vs_aux == VDEV_AUX_OPEN_FAILED);
80}
81
82/* ARGSUSED */
83static int
84vdev_faulted(vdev_stat_t *vs, uint_t vsc)
85{
86	return (vs->vs_state == VDEV_STATE_FAULTED);
87}
88
89/* ARGSUSED */
90static int
91vdev_errors(vdev_stat_t *vs, uint_t vsc)
92{
93	return (vs->vs_state == VDEV_STATE_DEGRADED ||
94	    vs->vs_read_errors != 0 || vs->vs_write_errors != 0 ||
95	    vs->vs_checksum_errors != 0);
96}
97
98/* ARGSUSED */
99static int
100vdev_broken(vdev_stat_t *vs, uint_t vsc)
101{
102	return (vs->vs_state == VDEV_STATE_CANT_OPEN);
103}
104
105/* ARGSUSED */
106static int
107vdev_offlined(vdev_stat_t *vs, uint_t vsc)
108{
109	return (vs->vs_state == VDEV_STATE_OFFLINE);
110}
111
112/* ARGSUSED */
113static int
114vdev_removed(vdev_stat_t *vs, uint_t vsc)
115{
116	return (vs->vs_state == VDEV_STATE_REMOVED);
117}
118
119static int
120vdev_non_native_ashift(vdev_stat_t *vs, uint_t vsc)
121{
122	return (VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
123	    vs->vs_configured_ashift < vs->vs_physical_ashift);
124}
125
126/*
127 * Detect if any leaf devices that have seen errors or could not be opened.
128 */
129static boolean_t
130find_vdev_problem(nvlist_t *vdev, int (*func)(vdev_stat_t *, uint_t),
131    boolean_t ignore_replacing)
132{
133	nvlist_t **child;
134	vdev_stat_t *vs;
135	uint_t c, vsc, children;
136
137	/*
138	 * Ignore problems within a 'replacing' vdev, since we're presumably in
139	 * the process of repairing any such errors, and don't want to call them
140	 * out again.  We'll pick up the fact that a resilver is happening
141	 * later.
142	 */
143	if (ignore_replacing == B_TRUE) {
144		char *type;
145
146		verify(nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE,
147		    &type) == 0);
148		if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
149			return (B_FALSE);
150	}
151
152	if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child,
153	    &children) == 0) {
154		for (c = 0; c < children; c++)
155			if (find_vdev_problem(child[c], func, ignore_replacing))
156				return (B_TRUE);
157	} else {
158		verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_VDEV_STATS,
159		    (uint64_t **)&vs, &vsc) == 0);
160
161		if (func(vs, vsc) != 0)
162			return (B_TRUE);
163	}
164
165	/*
166	 * Check any L2 cache devs
167	 */
168	if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_L2CACHE, &child,
169	    &children) == 0) {
170		for (c = 0; c < children; c++)
171			if (find_vdev_problem(child[c], func, ignore_replacing))
172				return (B_TRUE);
173	}
174
175	return (B_FALSE);
176}
177
178/*
179 * Active pool health status.
180 *
181 * To determine the status for a pool, we make several passes over the config,
182 * picking the most egregious error we find.  In order of importance, we do the
183 * following:
184 *
185 *	- Check for a complete and valid configuration
186 *	- Look for any faulted or missing devices in a non-replicated config
187 *	- Check for any data errors
188 *	- Check for any faulted or missing devices in a replicated config
189 *	- Look for any devices showing errors
190 *	- Check for any resilvering devices
191 *
192 * There can obviously be multiple errors within a single pool, so this routine
193 * only picks the most damaging of all the current errors to report.
194 */
195static zpool_status_t
196check_status(nvlist_t *config, boolean_t isimport)
197{
198	nvlist_t *nvroot;
199	vdev_stat_t *vs;
200	pool_scan_stat_t *ps = NULL;
201	uint_t vsc, psc;
202	uint64_t nerr;
203	uint64_t version;
204	uint64_t stateval;
205	uint64_t suspended;
206	uint64_t hostid = 0;
207
208	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
209	    &version) == 0);
210	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
211	    &nvroot) == 0);
212	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
213	    (uint64_t **)&vs, &vsc) == 0);
214	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
215	    &stateval) == 0);
216
217	/*
218	 * Currently resilvering a vdev
219	 */
220	(void) nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
221	    (uint64_t **)&ps, &psc);
222	if (ps && ps->pss_func == POOL_SCAN_RESILVER &&
223	    ps->pss_state == DSS_SCANNING)
224		return (ZPOOL_STATUS_RESILVERING);
225
226	/*
227	 * Pool last accessed by another system.
228	 */
229	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
230	if (hostid != 0 && (unsigned long)hostid != gethostid() &&
231	    stateval == POOL_STATE_ACTIVE)
232		return (ZPOOL_STATUS_HOSTID_MISMATCH);
233
234	/*
235	 * Newer on-disk version.
236	 */
237	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
238	    vs->vs_aux == VDEV_AUX_VERSION_NEWER)
239		return (ZPOOL_STATUS_VERSION_NEWER);
240
241	/*
242	 * Unsupported feature(s).
243	 */
244	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
245	    vs->vs_aux == VDEV_AUX_UNSUP_FEAT) {
246		nvlist_t *nvinfo;
247
248		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
249		    &nvinfo) == 0);
250		if (nvlist_exists(nvinfo, ZPOOL_CONFIG_CAN_RDONLY))
251			return (ZPOOL_STATUS_UNSUP_FEAT_WRITE);
252		return (ZPOOL_STATUS_UNSUP_FEAT_READ);
253	}
254
255	/*
256	 * Check that the config is complete.
257	 */
258	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
259	    vs->vs_aux == VDEV_AUX_BAD_GUID_SUM)
260		return (ZPOOL_STATUS_BAD_GUID_SUM);
261
262	/*
263	 * Check whether the pool has suspended due to failed I/O.
264	 */
265	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED,
266	    &suspended) == 0) {
267		if (suspended == ZIO_FAILURE_MODE_CONTINUE)
268			return (ZPOOL_STATUS_IO_FAILURE_CONTINUE);
269		return (ZPOOL_STATUS_IO_FAILURE_WAIT);
270	}
271
272	/*
273	 * Could not read a log.
274	 */
275	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
276	    vs->vs_aux == VDEV_AUX_BAD_LOG) {
277		return (ZPOOL_STATUS_BAD_LOG);
278	}
279
280	/*
281	 * Bad devices in non-replicated config.
282	 */
283	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
284	    find_vdev_problem(nvroot, vdev_faulted, B_TRUE))
285		return (ZPOOL_STATUS_FAULTED_DEV_NR);
286
287	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
288	    find_vdev_problem(nvroot, vdev_missing, B_TRUE))
289		return (ZPOOL_STATUS_MISSING_DEV_NR);
290
291	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
292	    find_vdev_problem(nvroot, vdev_broken, B_TRUE))
293		return (ZPOOL_STATUS_CORRUPT_LABEL_NR);
294
295	/*
296	 * Corrupted pool metadata
297	 */
298	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
299	    vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
300		return (ZPOOL_STATUS_CORRUPT_POOL);
301
302	/*
303	 * Persistent data errors.
304	 */
305	if (!isimport) {
306		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
307		    &nerr) == 0 && nerr != 0)
308			return (ZPOOL_STATUS_CORRUPT_DATA);
309	}
310
311	/*
312	 * Missing devices in a replicated config.
313	 */
314	if (find_vdev_problem(nvroot, vdev_faulted, B_TRUE))
315		return (ZPOOL_STATUS_FAULTED_DEV_R);
316	if (find_vdev_problem(nvroot, vdev_missing, B_TRUE))
317		return (ZPOOL_STATUS_MISSING_DEV_R);
318	if (find_vdev_problem(nvroot, vdev_broken, B_TRUE))
319		return (ZPOOL_STATUS_CORRUPT_LABEL_R);
320
321	/*
322	 * Devices with errors
323	 */
324	if (!isimport && find_vdev_problem(nvroot, vdev_errors, B_TRUE))
325		return (ZPOOL_STATUS_FAILING_DEV);
326
327	/*
328	 * Offlined devices
329	 */
330	if (find_vdev_problem(nvroot, vdev_offlined, B_TRUE))
331		return (ZPOOL_STATUS_OFFLINE_DEV);
332
333	/*
334	 * Removed device
335	 */
336	if (find_vdev_problem(nvroot, vdev_removed, B_TRUE))
337		return (ZPOOL_STATUS_REMOVED_DEV);
338
339	/*
340	 * Suboptimal, but usable, ashift configuration.
341	 */
342	if (find_vdev_problem(nvroot, vdev_non_native_ashift, B_FALSE))
343		return (ZPOOL_STATUS_NON_NATIVE_ASHIFT);
344
345	/*
346	 * Outdated, but usable, version
347	 */
348	if (SPA_VERSION_IS_SUPPORTED(version) && version != SPA_VERSION)
349		return (ZPOOL_STATUS_VERSION_OLDER);
350
351	/*
352	 * Usable pool with disabled features
353	 */
354	if (version >= SPA_VERSION_FEATURES) {
355		int i;
356		nvlist_t *feat;
357
358		if (isimport) {
359			feat = fnvlist_lookup_nvlist(config,
360			    ZPOOL_CONFIG_LOAD_INFO);
361			feat = fnvlist_lookup_nvlist(feat,
362			    ZPOOL_CONFIG_ENABLED_FEAT);
363		} else {
364			feat = fnvlist_lookup_nvlist(config,
365			    ZPOOL_CONFIG_FEATURE_STATS);
366		}
367
368		for (i = 0; i < SPA_FEATURES; i++) {
369			zfeature_info_t *fi = &spa_feature_table[i];
370			if (!nvlist_exists(feat, fi->fi_guid))
371				return (ZPOOL_STATUS_FEAT_DISABLED);
372		}
373	}
374
375	return (ZPOOL_STATUS_OK);
376}
377
378zpool_status_t
379zpool_get_status(zpool_handle_t *zhp, char **msgid)
380{
381	zpool_status_t ret = check_status(zhp->zpool_config, B_FALSE);
382
383	if (ret >= NMSGID)
384		*msgid = NULL;
385	else
386		*msgid = zfs_msgid_table[ret];
387
388	return (ret);
389}
390
391zpool_status_t
392zpool_import_status(nvlist_t *config, char **msgid)
393{
394	zpool_status_t ret = check_status(config, B_TRUE);
395
396	if (ret >= NMSGID)
397		*msgid = NULL;
398	else
399		*msgid = zfs_msgid_table[ret];
400
401	return (ret);
402}
403
404static void
405dump_ddt_stat(const ddt_stat_t *dds, int h)
406{
407	char refcnt[6];
408	char blocks[6], lsize[6], psize[6], dsize[6];
409	char ref_blocks[6], ref_lsize[6], ref_psize[6], ref_dsize[6];
410
411	if (dds == NULL || dds->dds_blocks == 0)
412		return;
413
414	if (h == -1)
415		(void) strcpy(refcnt, "Total");
416	else
417		zfs_nicenum(1ULL << h, refcnt, sizeof (refcnt));
418
419	zfs_nicenum(dds->dds_blocks, blocks, sizeof (blocks));
420	zfs_nicenum(dds->dds_lsize, lsize, sizeof (lsize));
421	zfs_nicenum(dds->dds_psize, psize, sizeof (psize));
422	zfs_nicenum(dds->dds_dsize, dsize, sizeof (dsize));
423	zfs_nicenum(dds->dds_ref_blocks, ref_blocks, sizeof (ref_blocks));
424	zfs_nicenum(dds->dds_ref_lsize, ref_lsize, sizeof (ref_lsize));
425	zfs_nicenum(dds->dds_ref_psize, ref_psize, sizeof (ref_psize));
426	zfs_nicenum(dds->dds_ref_dsize, ref_dsize, sizeof (ref_dsize));
427
428	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
429	    refcnt,
430	    blocks, lsize, psize, dsize,
431	    ref_blocks, ref_lsize, ref_psize, ref_dsize);
432}
433
434/*
435 * Print the DDT histogram and the column totals.
436 */
437void
438zpool_dump_ddt(const ddt_stat_t *dds_total, const ddt_histogram_t *ddh)
439{
440	int h;
441
442	(void) printf("\n");
443
444	(void) printf("bucket   "
445	    "           allocated             "
446	    "          referenced          \n");
447	(void) printf("______   "
448	    "______________________________   "
449	    "______________________________\n");
450
451	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
452	    "refcnt",
453	    "blocks", "LSIZE", "PSIZE", "DSIZE",
454	    "blocks", "LSIZE", "PSIZE", "DSIZE");
455
456	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
457	    "------",
458	    "------", "-----", "-----", "-----",
459	    "------", "-----", "-----", "-----");
460
461	for (h = 0; h < 64; h++)
462		dump_ddt_stat(&ddh->ddh_stat[h], h);
463
464	dump_ddt_stat(dds_total, -1);
465
466	(void) printf("\n");
467}
468