xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_status.c (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
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  */
55 static char *zfs_msgid_table[] = {
56 	"ZFS-8000-14", /* ZPOOL_STATUS_CORRUPT_CACHE */
57 	"ZFS-8000-2Q", /* ZPOOL_STATUS_MISSING_DEV_R */
58 	"ZFS-8000-3C", /* ZPOOL_STATUS_MISSING_DEV_NR */
59 	"ZFS-8000-4J", /* ZPOOL_STATUS_CORRUPT_LABEL_R */
60 	"ZFS-8000-5E", /* ZPOOL_STATUS_CORRUPT_LABEL_NR */
61 	"ZFS-8000-6X", /* ZPOOL_STATUS_BAD_GUID_SUM */
62 	"ZFS-8000-72", /* ZPOOL_STATUS_CORRUPT_POOL */
63 	"ZFS-8000-8A", /* ZPOOL_STATUS_CORRUPT_DATA */
64 	"ZFS-8000-9P", /* ZPOOL_STATUS_FAILING_DEV */
65 	"ZFS-8000-A5", /* ZPOOL_STATUS_VERSION_NEWER */
66 	"ZFS-8000-EY", /* ZPOOL_STATUS_HOSTID_MISMATCH */
67 	"ZFS-8000-EY", /* ZPOOL_STATUS_HOSTID_ACTIVE */
68 	"ZFS-8000-EY", /* ZPOOL_STATUS_HOSTID_REQUIRED */
69 	"ZFS-8000-HC", /* ZPOOL_STATUS_IO_FAILURE_WAIT */
70 	"ZFS-8000-JQ", /* ZPOOL_STATUS_IO_FAILURE_CONTINUE */
71 	"ZFS-8000-MM", /* ZPOOL_STATUS_IO_FAILURE_MMP */
72 	"ZFS-8000-K4", /* ZPOOL_STATUS_BAD_LOG */
73 	/*
74 	 * The following results have no message ID.
75 	 *	ZPOOL_STATUS_UNSUP_FEAT_READ
76 	 *	ZPOOL_STATUS_UNSUP_FEAT_WRITE
77 	 *	ZPOOL_STATUS_FAULTED_DEV_R
78 	 *	ZPOOL_STATUS_FAULTED_DEV_NR
79 	 *	ZPOOL_STATUS_VERSION_OLDER
80 	 *	ZPOOL_STATUS_FEAT_DISABLED
81 	 *	ZPOOL_STATUS_RESILVERING
82 	 *	ZPOOL_STATUS_OFFLINE_DEV
83 	 *	ZPOOL_STATUS_REMOVED_DEV
84 	 *	ZPOOL_STATUS_OK
85 	 */
86 };
87 
88 #define	NMSGID	(sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
89 
90 /* ARGSUSED */
91 static int
92 vdev_missing(uint64_t state, uint64_t aux, uint64_t errs)
93 {
94 	return (state == VDEV_STATE_CANT_OPEN &&
95 	    aux == VDEV_AUX_OPEN_FAILED);
96 }
97 
98 /* ARGSUSED */
99 static int
100 vdev_faulted(uint64_t state, uint64_t aux, uint64_t errs)
101 {
102 	return (state == VDEV_STATE_FAULTED);
103 }
104 
105 /* ARGSUSED */
106 static int
107 vdev_errors(uint64_t state, uint64_t aux, uint64_t errs)
108 {
109 	return (state == VDEV_STATE_DEGRADED || errs != 0);
110 }
111 
112 /* ARGSUSED */
113 static int
114 vdev_broken(uint64_t state, uint64_t aux, uint64_t errs)
115 {
116 	return (state == VDEV_STATE_CANT_OPEN);
117 }
118 
119 /* ARGSUSED */
120 static int
121 vdev_offlined(uint64_t state, uint64_t aux, uint64_t errs)
122 {
123 	return (state == VDEV_STATE_OFFLINE);
124 }
125 
126 /* ARGSUSED */
127 static int
128 vdev_removed(uint64_t state, uint64_t aux, uint64_t errs)
129 {
130 	return (state == VDEV_STATE_REMOVED);
131 }
132 
133 /*
134  * Detect if any leaf devices that have seen errors or could not be opened.
135  */
136 static boolean_t
137 find_vdev_problem(nvlist_t *vdev, int (*func)(uint64_t, uint64_t, uint64_t))
138 {
139 	nvlist_t **child;
140 	vdev_stat_t *vs;
141 	uint_t c, children;
142 	char *type;
143 
144 	/*
145 	 * Ignore problems within a 'replacing' vdev, since we're presumably in
146 	 * the process of repairing any such errors, and don't want to call them
147 	 * out again.  We'll pick up the fact that a resilver is happening
148 	 * later.
149 	 */
150 	verify(nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type) == 0);
151 	if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
152 		return (B_FALSE);
153 
154 	if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child,
155 	    &children) == 0) {
156 		for (c = 0; c < children; c++)
157 			if (find_vdev_problem(child[c], func))
158 				return (B_TRUE);
159 	} else {
160 		verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_VDEV_STATS,
161 		    (uint64_t **)&vs, &c) == 0);
162 
163 		if (func(vs->vs_state, vs->vs_aux,
164 		    vs->vs_read_errors +
165 		    vs->vs_write_errors +
166 		    vs->vs_checksum_errors))
167 			return (B_TRUE);
168 	}
169 
170 	/*
171 	 * Check any L2 cache devs
172 	 */
173 	if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_L2CACHE, &child,
174 	    &children) == 0) {
175 		for (c = 0; c < children; c++)
176 			if (find_vdev_problem(child[c], func))
177 				return (B_TRUE);
178 	}
179 
180 	return (B_FALSE);
181 }
182 
183 /*
184  * Active pool health status.
185  *
186  * To determine the status for a pool, we make several passes over the config,
187  * picking the most egregious error we find.  In order of importance, we do the
188  * following:
189  *
190  *	- Check for a complete and valid configuration
191  *	- Look for any faulted or missing devices in a non-replicated config
192  *	- Check for any data errors
193  *	- Check for any faulted or missing devices in a replicated config
194  *	- Look for any devices showing errors
195  *	- Check for any resilvering devices
196  *
197  * There can obviously be multiple errors within a single pool, so this routine
198  * only picks the most damaging of all the current errors to report.
199  */
200 static zpool_status_t
201 check_status(nvlist_t *config, boolean_t isimport, zpool_errata_t *erratap)
202 {
203 	nvlist_t *nvroot;
204 	vdev_stat_t *vs;
205 	pool_scan_stat_t *ps = NULL;
206 	uint_t vsc, psc;
207 	uint64_t nerr;
208 	uint64_t version;
209 	uint64_t stateval;
210 	uint64_t suspended;
211 	uint64_t hostid = 0;
212 	uint64_t errata = 0;
213 	unsigned long system_hostid = get_system_hostid();
214 
215 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
216 	    &version) == 0);
217 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
218 	    &nvroot) == 0);
219 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
220 	    (uint64_t **)&vs, &vsc) == 0);
221 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
222 	    &stateval) == 0);
223 
224 	/*
225 	 * Currently resilvering a vdev
226 	 */
227 	(void) nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
228 	    (uint64_t **)&ps, &psc);
229 	if (ps != NULL && ps->pss_func == POOL_SCAN_RESILVER &&
230 	    ps->pss_state == DSS_SCANNING)
231 		return (ZPOOL_STATUS_RESILVERING);
232 
233 	/*
234 	 * The multihost property is set and the pool may be active.
235 	 */
236 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
237 	    vs->vs_aux == VDEV_AUX_ACTIVE) {
238 		mmp_state_t mmp_state;
239 		nvlist_t *nvinfo;
240 
241 		nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
242 		mmp_state = fnvlist_lookup_uint64(nvinfo,
243 		    ZPOOL_CONFIG_MMP_STATE);
244 
245 		if (mmp_state == MMP_STATE_ACTIVE)
246 			return (ZPOOL_STATUS_HOSTID_ACTIVE);
247 		else if (mmp_state == MMP_STATE_NO_HOSTID)
248 			return (ZPOOL_STATUS_HOSTID_REQUIRED);
249 		else
250 			return (ZPOOL_STATUS_HOSTID_MISMATCH);
251 	}
252 
253 	/*
254 	 * Pool last accessed by another system.
255 	 */
256 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
257 	if (hostid != 0 && (unsigned long)hostid != system_hostid &&
258 	    stateval == POOL_STATE_ACTIVE)
259 		return (ZPOOL_STATUS_HOSTID_MISMATCH);
260 
261 	/*
262 	 * Newer on-disk version.
263 	 */
264 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
265 	    vs->vs_aux == VDEV_AUX_VERSION_NEWER)
266 		return (ZPOOL_STATUS_VERSION_NEWER);
267 
268 	/*
269 	 * Unsupported feature(s).
270 	 */
271 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
272 	    vs->vs_aux == VDEV_AUX_UNSUP_FEAT) {
273 		nvlist_t *nvinfo;
274 
275 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
276 		    &nvinfo) == 0);
277 		if (nvlist_exists(nvinfo, ZPOOL_CONFIG_CAN_RDONLY))
278 			return (ZPOOL_STATUS_UNSUP_FEAT_WRITE);
279 		return (ZPOOL_STATUS_UNSUP_FEAT_READ);
280 	}
281 
282 	/*
283 	 * Check that the config is complete.
284 	 */
285 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
286 	    vs->vs_aux == VDEV_AUX_BAD_GUID_SUM)
287 		return (ZPOOL_STATUS_BAD_GUID_SUM);
288 
289 	/*
290 	 * Check whether the pool has suspended.
291 	 */
292 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED,
293 	    &suspended) == 0) {
294 		uint64_t reason;
295 
296 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED_REASON,
297 		    &reason) == 0 && reason == ZIO_SUSPEND_MMP)
298 			return (ZPOOL_STATUS_IO_FAILURE_MMP);
299 
300 		if (suspended == ZIO_FAILURE_MODE_CONTINUE)
301 			return (ZPOOL_STATUS_IO_FAILURE_CONTINUE);
302 		return (ZPOOL_STATUS_IO_FAILURE_WAIT);
303 	}
304 
305 	/*
306 	 * Could not read a log.
307 	 */
308 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
309 	    vs->vs_aux == VDEV_AUX_BAD_LOG) {
310 		return (ZPOOL_STATUS_BAD_LOG);
311 	}
312 
313 	/*
314 	 * Bad devices in non-replicated config.
315 	 */
316 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
317 	    find_vdev_problem(nvroot, vdev_faulted))
318 		return (ZPOOL_STATUS_FAULTED_DEV_NR);
319 
320 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
321 	    find_vdev_problem(nvroot, vdev_missing))
322 		return (ZPOOL_STATUS_MISSING_DEV_NR);
323 
324 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
325 	    find_vdev_problem(nvroot, vdev_broken))
326 		return (ZPOOL_STATUS_CORRUPT_LABEL_NR);
327 
328 	/*
329 	 * Corrupted pool metadata
330 	 */
331 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
332 	    vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
333 		return (ZPOOL_STATUS_CORRUPT_POOL);
334 
335 	/*
336 	 * Persistent data errors.
337 	 */
338 	if (!isimport) {
339 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
340 		    &nerr) == 0 && nerr != 0)
341 			return (ZPOOL_STATUS_CORRUPT_DATA);
342 	}
343 
344 	/*
345 	 * Missing devices in a replicated config.
346 	 */
347 	if (find_vdev_problem(nvroot, vdev_faulted))
348 		return (ZPOOL_STATUS_FAULTED_DEV_R);
349 	if (find_vdev_problem(nvroot, vdev_missing))
350 		return (ZPOOL_STATUS_MISSING_DEV_R);
351 	if (find_vdev_problem(nvroot, vdev_broken))
352 		return (ZPOOL_STATUS_CORRUPT_LABEL_R);
353 
354 	/*
355 	 * Devices with errors
356 	 */
357 	if (!isimport && find_vdev_problem(nvroot, vdev_errors))
358 		return (ZPOOL_STATUS_FAILING_DEV);
359 
360 	/*
361 	 * Offlined devices
362 	 */
363 	if (find_vdev_problem(nvroot, vdev_offlined))
364 		return (ZPOOL_STATUS_OFFLINE_DEV);
365 
366 	/*
367 	 * Removed device
368 	 */
369 	if (find_vdev_problem(nvroot, vdev_removed))
370 		return (ZPOOL_STATUS_REMOVED_DEV);
371 
372 	/*
373 	 * Informational errata available.
374 	 */
375 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRATA, &errata);
376 	if (errata) {
377 		*erratap = errata;
378 		return (ZPOOL_STATUS_ERRATA);
379 	}
380 
381 	/*
382 	 * Outdated, but usable, version
383 	 */
384 	if (SPA_VERSION_IS_SUPPORTED(version) && version != SPA_VERSION)
385 		return (ZPOOL_STATUS_VERSION_OLDER);
386 
387 	/*
388 	 * Usable pool with disabled features
389 	 */
390 	if (version >= SPA_VERSION_FEATURES) {
391 		int i;
392 		nvlist_t *feat;
393 
394 		if (isimport) {
395 			feat = fnvlist_lookup_nvlist(config,
396 			    ZPOOL_CONFIG_LOAD_INFO);
397 			if (nvlist_exists(feat, ZPOOL_CONFIG_ENABLED_FEAT))
398 				feat = fnvlist_lookup_nvlist(feat,
399 				    ZPOOL_CONFIG_ENABLED_FEAT);
400 		} else {
401 			feat = fnvlist_lookup_nvlist(config,
402 			    ZPOOL_CONFIG_FEATURE_STATS);
403 		}
404 
405 		for (i = 0; i < SPA_FEATURES; i++) {
406 			zfeature_info_t *fi = &spa_feature_table[i];
407 			if (!nvlist_exists(feat, fi->fi_guid))
408 				return (ZPOOL_STATUS_FEAT_DISABLED);
409 		}
410 	}
411 
412 	return (ZPOOL_STATUS_OK);
413 }
414 
415 zpool_status_t
416 zpool_get_status(zpool_handle_t *zhp, char **msgid, zpool_errata_t *errata)
417 {
418 	zpool_status_t ret = check_status(zhp->zpool_config, B_FALSE, errata);
419 
420 	if (ret >= NMSGID)
421 		*msgid = NULL;
422 	else
423 		*msgid = zfs_msgid_table[ret];
424 
425 	return (ret);
426 }
427 
428 zpool_status_t
429 zpool_import_status(nvlist_t *config, char **msgid, zpool_errata_t *errata)
430 {
431 	zpool_status_t ret = check_status(config, B_TRUE, errata);
432 
433 	if (ret >= NMSGID)
434 		*msgid = NULL;
435 	else
436 		*msgid = zfs_msgid_table[ret];
437 
438 	return (ret);
439 }
440 
441 static void
442 dump_ddt_stat(const ddt_stat_t *dds, int h)
443 {
444 	char refcnt[6];
445 	char blocks[6], lsize[6], psize[6], dsize[6];
446 	char ref_blocks[6], ref_lsize[6], ref_psize[6], ref_dsize[6];
447 
448 	if (dds == NULL || dds->dds_blocks == 0)
449 		return;
450 
451 	if (h == -1)
452 		(void) strcpy(refcnt, "Total");
453 	else
454 		zfs_nicenum(1ULL << h, refcnt, sizeof (refcnt));
455 
456 	zfs_nicenum(dds->dds_blocks, blocks, sizeof (blocks));
457 	zfs_nicenum(dds->dds_lsize, lsize, sizeof (lsize));
458 	zfs_nicenum(dds->dds_psize, psize, sizeof (psize));
459 	zfs_nicenum(dds->dds_dsize, dsize, sizeof (dsize));
460 	zfs_nicenum(dds->dds_ref_blocks, ref_blocks, sizeof (ref_blocks));
461 	zfs_nicenum(dds->dds_ref_lsize, ref_lsize, sizeof (ref_lsize));
462 	zfs_nicenum(dds->dds_ref_psize, ref_psize, sizeof (ref_psize));
463 	zfs_nicenum(dds->dds_ref_dsize, ref_dsize, sizeof (ref_dsize));
464 
465 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
466 	    refcnt,
467 	    blocks, lsize, psize, dsize,
468 	    ref_blocks, ref_lsize, ref_psize, ref_dsize);
469 }
470 
471 /*
472  * Print the DDT histogram and the column totals.
473  */
474 void
475 zpool_dump_ddt(const ddt_stat_t *dds_total, const ddt_histogram_t *ddh)
476 {
477 	int h;
478 
479 	(void) printf("\n");
480 
481 	(void) printf("bucket   "
482 	    "           allocated             "
483 	    "          referenced          \n");
484 	(void) printf("______   "
485 	    "______________________________   "
486 	    "______________________________\n");
487 
488 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
489 	    "refcnt",
490 	    "blocks", "LSIZE", "PSIZE", "DSIZE",
491 	    "blocks", "LSIZE", "PSIZE", "DSIZE");
492 
493 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
494 	    "------",
495 	    "------", "-----", "-----", "-----",
496 	    "------", "-----", "-----", "-----");
497 
498 	for (h = 0; h < 64; h++)
499 		dump_ddt_stat(&ddh->ddh_stat[h], h);
500 
501 	dump_ddt_stat(dds_total, -1);
502 
503 	(void) printf("\n");
504 }
505