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)
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 	unsigned long system_hostid = get_system_hostid();
213 
214 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
215 	    &version) == 0);
216 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
217 	    &nvroot) == 0);
218 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
219 	    (uint64_t **)&vs, &vsc) == 0);
220 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
221 	    &stateval) == 0);
222 
223 	/*
224 	 * Currently resilvering a vdev
225 	 */
226 	(void) nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
227 	    (uint64_t **)&ps, &psc);
228 	if (ps && ps->pss_func == POOL_SCAN_RESILVER &&
229 	    ps->pss_state == DSS_SCANNING)
230 		return (ZPOOL_STATUS_RESILVERING);
231 
232 	/*
233 	 * The multihost property is set and the pool may be active.
234 	 */
235 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
236 	    vs->vs_aux == VDEV_AUX_ACTIVE) {
237 		mmp_state_t mmp_state;
238 		nvlist_t *nvinfo;
239 
240 		nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
241 		mmp_state = fnvlist_lookup_uint64(nvinfo,
242 		    ZPOOL_CONFIG_MMP_STATE);
243 
244 		if (mmp_state == MMP_STATE_ACTIVE)
245 			return (ZPOOL_STATUS_HOSTID_ACTIVE);
246 		else if (mmp_state == MMP_STATE_NO_HOSTID)
247 			return (ZPOOL_STATUS_HOSTID_REQUIRED);
248 		else
249 			return (ZPOOL_STATUS_HOSTID_MISMATCH);
250 	}
251 
252 	/*
253 	 * Pool last accessed by another system.
254 	 */
255 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
256 	if (hostid != 0 && (unsigned long)hostid != system_hostid &&
257 	    stateval == POOL_STATE_ACTIVE)
258 		return (ZPOOL_STATUS_HOSTID_MISMATCH);
259 
260 	/*
261 	 * Newer on-disk version.
262 	 */
263 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
264 	    vs->vs_aux == VDEV_AUX_VERSION_NEWER)
265 		return (ZPOOL_STATUS_VERSION_NEWER);
266 
267 	/*
268 	 * Unsupported feature(s).
269 	 */
270 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
271 	    vs->vs_aux == VDEV_AUX_UNSUP_FEAT) {
272 		nvlist_t *nvinfo;
273 
274 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
275 		    &nvinfo) == 0);
276 		if (nvlist_exists(nvinfo, ZPOOL_CONFIG_CAN_RDONLY))
277 			return (ZPOOL_STATUS_UNSUP_FEAT_WRITE);
278 		return (ZPOOL_STATUS_UNSUP_FEAT_READ);
279 	}
280 
281 	/*
282 	 * Check that the config is complete.
283 	 */
284 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
285 	    vs->vs_aux == VDEV_AUX_BAD_GUID_SUM)
286 		return (ZPOOL_STATUS_BAD_GUID_SUM);
287 
288 	/*
289 	 * Check whether the pool has suspended.
290 	 */
291 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED,
292 	    &suspended) == 0) {
293 		uint64_t reason;
294 
295 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED_REASON,
296 		    &reason) == 0 && reason == ZIO_SUSPEND_MMP)
297 			return (ZPOOL_STATUS_IO_FAILURE_MMP);
298 
299 		if (suspended == ZIO_FAILURE_MODE_CONTINUE)
300 			return (ZPOOL_STATUS_IO_FAILURE_CONTINUE);
301 		return (ZPOOL_STATUS_IO_FAILURE_WAIT);
302 	}
303 
304 	/*
305 	 * Could not read a log.
306 	 */
307 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
308 	    vs->vs_aux == VDEV_AUX_BAD_LOG) {
309 		return (ZPOOL_STATUS_BAD_LOG);
310 	}
311 
312 	/*
313 	 * Bad devices in non-replicated config.
314 	 */
315 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
316 	    find_vdev_problem(nvroot, vdev_faulted))
317 		return (ZPOOL_STATUS_FAULTED_DEV_NR);
318 
319 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
320 	    find_vdev_problem(nvroot, vdev_missing))
321 		return (ZPOOL_STATUS_MISSING_DEV_NR);
322 
323 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
324 	    find_vdev_problem(nvroot, vdev_broken))
325 		return (ZPOOL_STATUS_CORRUPT_LABEL_NR);
326 
327 	/*
328 	 * Corrupted pool metadata
329 	 */
330 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
331 	    vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
332 		return (ZPOOL_STATUS_CORRUPT_POOL);
333 
334 	/*
335 	 * Persistent data errors.
336 	 */
337 	if (!isimport) {
338 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
339 		    &nerr) == 0 && nerr != 0)
340 			return (ZPOOL_STATUS_CORRUPT_DATA);
341 	}
342 
343 	/*
344 	 * Missing devices in a replicated config.
345 	 */
346 	if (find_vdev_problem(nvroot, vdev_faulted))
347 		return (ZPOOL_STATUS_FAULTED_DEV_R);
348 	if (find_vdev_problem(nvroot, vdev_missing))
349 		return (ZPOOL_STATUS_MISSING_DEV_R);
350 	if (find_vdev_problem(nvroot, vdev_broken))
351 		return (ZPOOL_STATUS_CORRUPT_LABEL_R);
352 
353 	/*
354 	 * Devices with errors
355 	 */
356 	if (!isimport && find_vdev_problem(nvroot, vdev_errors))
357 		return (ZPOOL_STATUS_FAILING_DEV);
358 
359 	/*
360 	 * Offlined devices
361 	 */
362 	if (find_vdev_problem(nvroot, vdev_offlined))
363 		return (ZPOOL_STATUS_OFFLINE_DEV);
364 
365 	/*
366 	 * Removed device
367 	 */
368 	if (find_vdev_problem(nvroot, vdev_removed))
369 		return (ZPOOL_STATUS_REMOVED_DEV);
370 
371 	/*
372 	 * Outdated, but usable, version
373 	 */
374 	if (SPA_VERSION_IS_SUPPORTED(version) && version != SPA_VERSION)
375 		return (ZPOOL_STATUS_VERSION_OLDER);
376 
377 	/*
378 	 * Usable pool with disabled features
379 	 */
380 	if (version >= SPA_VERSION_FEATURES) {
381 		int i;
382 		nvlist_t *feat;
383 
384 		if (isimport) {
385 			feat = fnvlist_lookup_nvlist(config,
386 			    ZPOOL_CONFIG_LOAD_INFO);
387 			if (nvlist_exists(feat, ZPOOL_CONFIG_ENABLED_FEAT))
388 				feat = fnvlist_lookup_nvlist(feat,
389 				    ZPOOL_CONFIG_ENABLED_FEAT);
390 		} else {
391 			feat = fnvlist_lookup_nvlist(config,
392 			    ZPOOL_CONFIG_FEATURE_STATS);
393 		}
394 
395 		for (i = 0; i < SPA_FEATURES; i++) {
396 			zfeature_info_t *fi = &spa_feature_table[i];
397 			if (!nvlist_exists(feat, fi->fi_guid))
398 				return (ZPOOL_STATUS_FEAT_DISABLED);
399 		}
400 	}
401 
402 	return (ZPOOL_STATUS_OK);
403 }
404 
405 zpool_status_t
406 zpool_get_status(zpool_handle_t *zhp, char **msgid)
407 {
408 	zpool_status_t ret = check_status(zhp->zpool_config, B_FALSE);
409 
410 	if (ret >= NMSGID)
411 		*msgid = NULL;
412 	else
413 		*msgid = zfs_msgid_table[ret];
414 
415 	return (ret);
416 }
417 
418 zpool_status_t
419 zpool_import_status(nvlist_t *config, char **msgid)
420 {
421 	zpool_status_t ret = check_status(config, B_TRUE);
422 
423 	if (ret >= NMSGID)
424 		*msgid = NULL;
425 	else
426 		*msgid = zfs_msgid_table[ret];
427 
428 	return (ret);
429 }
430 
431 static void
432 dump_ddt_stat(const ddt_stat_t *dds, int h)
433 {
434 	char refcnt[6];
435 	char blocks[6], lsize[6], psize[6], dsize[6];
436 	char ref_blocks[6], ref_lsize[6], ref_psize[6], ref_dsize[6];
437 
438 	if (dds == NULL || dds->dds_blocks == 0)
439 		return;
440 
441 	if (h == -1)
442 		(void) strcpy(refcnt, "Total");
443 	else
444 		zfs_nicenum(1ULL << h, refcnt, sizeof (refcnt));
445 
446 	zfs_nicenum(dds->dds_blocks, blocks, sizeof (blocks));
447 	zfs_nicenum(dds->dds_lsize, lsize, sizeof (lsize));
448 	zfs_nicenum(dds->dds_psize, psize, sizeof (psize));
449 	zfs_nicenum(dds->dds_dsize, dsize, sizeof (dsize));
450 	zfs_nicenum(dds->dds_ref_blocks, ref_blocks, sizeof (ref_blocks));
451 	zfs_nicenum(dds->dds_ref_lsize, ref_lsize, sizeof (ref_lsize));
452 	zfs_nicenum(dds->dds_ref_psize, ref_psize, sizeof (ref_psize));
453 	zfs_nicenum(dds->dds_ref_dsize, ref_dsize, sizeof (ref_dsize));
454 
455 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
456 	    refcnt,
457 	    blocks, lsize, psize, dsize,
458 	    ref_blocks, ref_lsize, ref_psize, ref_dsize);
459 }
460 
461 /*
462  * Print the DDT histogram and the column totals.
463  */
464 void
465 zpool_dump_ddt(const ddt_stat_t *dds_total, const ddt_histogram_t *ddh)
466 {
467 	int h;
468 
469 	(void) printf("\n");
470 
471 	(void) printf("bucket   "
472 	    "           allocated             "
473 	    "          referenced          \n");
474 	(void) printf("______   "
475 	    "______________________________   "
476 	    "______________________________\n");
477 
478 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
479 	    "refcnt",
480 	    "blocks", "LSIZE", "PSIZE", "DSIZE",
481 	    "blocks", "LSIZE", "PSIZE", "DSIZE");
482 
483 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
484 	    "------",
485 	    "------", "-----", "-----", "-----",
486 	    "------", "-----", "-----", "-----");
487 
488 	for (h = 0; h < 64; h++)
489 		dump_ddt_stat(&ddh->ddh_stat[h], h);
490 
491 	dump_ddt_stat(dds_total, -1);
492 
493 	(void) printf("\n");
494 }
495