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  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
233f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24ad135b5dSChristopher Siden  * Copyright (c) 2012 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens /*
28fa9e4066Sahrens  * This file contains the functions which analyze the status of a pool.  This
29fa9e4066Sahrens  * include both the status of an active pool, as well as the status exported
30fa9e4066Sahrens  * pools.  Returns one of the ZPOOL_STATUS_* defines describing the status of
31fa9e4066Sahrens  * the pool.  This status is independent (to a certain degree) from the state of
323d7072f8Seschrock  * the pool.  A pool's state describes only whether or not it is capable of
33fa9e4066Sahrens  * providing the necessary fault tolerance for data.  The status describes the
34fa9e4066Sahrens  * overall status of devices.  A pool that is online can still have a device
35fa9e4066Sahrens  * that is experiencing errors.
36fa9e4066Sahrens  *
37fa9e4066Sahrens  * Only a subset of the possible faults can be detected using 'zpool status',
38fa9e4066Sahrens  * and not all possible errors correspond to a FMA message ID.  The explanation
39fa9e4066Sahrens  * is left up to the caller, depending on whether it is a live pool or an
40fa9e4066Sahrens  * import.
41fa9e4066Sahrens  */
42fa9e4066Sahrens 
43fa9e4066Sahrens #include <libzfs.h>
44fa9e4066Sahrens #include <string.h>
4595173954Sek #include <unistd.h>
46fa9e4066Sahrens #include "libzfs_impl.h"
47*57221772SChristopher Siden #include "zfeature_common.h"
48fa9e4066Sahrens 
49fa9e4066Sahrens /*
503d7072f8Seschrock  * Message ID table.  This must be kept in sync with the ZPOOL_STATUS_* defines
51fa9e4066Sahrens  * in libzfs.h.  Note that there are some status results which go past the end
52fa9e4066Sahrens  * of this table, and hence have no associated message ID.
53fa9e4066Sahrens  */
5495173954Sek static char *zfs_msgid_table[] = {
55fa9e4066Sahrens 	"ZFS-8000-14",
56fa9e4066Sahrens 	"ZFS-8000-2Q",
57fa9e4066Sahrens 	"ZFS-8000-3C",
58fa9e4066Sahrens 	"ZFS-8000-4J",
59fa9e4066Sahrens 	"ZFS-8000-5E",
60fa9e4066Sahrens 	"ZFS-8000-6X",
61fa9e4066Sahrens 	"ZFS-8000-72",
62fa9e4066Sahrens 	"ZFS-8000-8A",
63fa9e4066Sahrens 	"ZFS-8000-9P",
6495173954Sek 	"ZFS-8000-A5",
6532b87932Sek 	"ZFS-8000-EY",
6632b87932Sek 	"ZFS-8000-HC",
67b87f3af3Sperrin 	"ZFS-8000-JQ",
68b87f3af3Sperrin 	"ZFS-8000-K4",
69fa9e4066Sahrens };
70fa9e4066Sahrens 
7195173954Sek #define	NMSGID	(sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
72fa9e4066Sahrens 
73fa9e4066Sahrens /* ARGSUSED */
74fa9e4066Sahrens static int
75fa9e4066Sahrens vdev_missing(uint64_t state, uint64_t aux, uint64_t errs)
76fa9e4066Sahrens {
77fa9e4066Sahrens 	return (state == VDEV_STATE_CANT_OPEN &&
78fa9e4066Sahrens 	    aux == VDEV_AUX_OPEN_FAILED);
79fa9e4066Sahrens }
80fa9e4066Sahrens 
813d7072f8Seschrock /* ARGSUSED */
823d7072f8Seschrock static int
833d7072f8Seschrock vdev_faulted(uint64_t state, uint64_t aux, uint64_t errs)
843d7072f8Seschrock {
853d7072f8Seschrock 	return (state == VDEV_STATE_FAULTED);
863d7072f8Seschrock }
873d7072f8Seschrock 
88fa9e4066Sahrens /* ARGSUSED */
89fa9e4066Sahrens static int
90fa9e4066Sahrens vdev_errors(uint64_t state, uint64_t aux, uint64_t errs)
91fa9e4066Sahrens {
923d7072f8Seschrock 	return (state == VDEV_STATE_DEGRADED || errs != 0);
93fa9e4066Sahrens }
94fa9e4066Sahrens 
95fa9e4066Sahrens /* ARGSUSED */
96fa9e4066Sahrens static int
97fa9e4066Sahrens vdev_broken(uint64_t state, uint64_t aux, uint64_t errs)
98fa9e4066Sahrens {
99fa9e4066Sahrens 	return (state == VDEV_STATE_CANT_OPEN);
100fa9e4066Sahrens }
101fa9e4066Sahrens 
102fa9e4066Sahrens /* ARGSUSED */
103fa9e4066Sahrens static int
104fa9e4066Sahrens vdev_offlined(uint64_t state, uint64_t aux, uint64_t errs)
105fa9e4066Sahrens {
106fa9e4066Sahrens 	return (state == VDEV_STATE_OFFLINE);
107fa9e4066Sahrens }
108fa9e4066Sahrens 
109c25309d4SGeorge Wilson /* ARGSUSED */
110c25309d4SGeorge Wilson static int
111c25309d4SGeorge Wilson vdev_removed(uint64_t state, uint64_t aux, uint64_t errs)
112c25309d4SGeorge Wilson {
113c25309d4SGeorge Wilson 	return (state == VDEV_STATE_REMOVED);
114c25309d4SGeorge Wilson }
115c25309d4SGeorge Wilson 
116fa9e4066Sahrens /*
117fa9e4066Sahrens  * Detect if any leaf devices that have seen errors or could not be opened.
118fa9e4066Sahrens  */
11999653d4eSeschrock static boolean_t
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)
13599653d4eSeschrock 		return (B_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))
14199653d4eSeschrock 				return (B_TRUE);
142fa9e4066Sahrens 	} else {
1433f9d6ad7SLin Ling 		verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_VDEV_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))
15099653d4eSeschrock 			return (B_TRUE);
151fa9e4066Sahrens 	}
152fa9e4066Sahrens 
15399653d4eSeschrock 	return (B_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
1643d7072f8Seschrock  *	- Look for any faulted or missing devices in a non-replicated config
165fa9e4066Sahrens  *	- Check for any data errors
1663d7072f8Seschrock  *	- Check for any faulted or 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
174e14bb325SJeff Bonwick check_status(nvlist_t *config, boolean_t isimport)
175fa9e4066Sahrens {
176fa9e4066Sahrens 	nvlist_t *nvroot;
177fa9e4066Sahrens 	vdev_stat_t *vs;
1783f9d6ad7SLin Ling 	pool_scan_stat_t *ps = NULL;
1793f9d6ad7SLin Ling 	uint_t vsc, psc;
180ea8dc4b6Seschrock 	uint64_t nerr;
181eaca9bbdSeschrock 	uint64_t version;
18295173954Sek 	uint64_t stateval;
183e14bb325SJeff Bonwick 	uint64_t suspended;
18495173954Sek 	uint64_t hostid = 0;
185fa9e4066Sahrens 
186eaca9bbdSeschrock 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
187eaca9bbdSeschrock 	    &version) == 0);
188fa9e4066Sahrens 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
189fa9e4066Sahrens 	    &nvroot) == 0);
1903f9d6ad7SLin Ling 	verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
191fa9e4066Sahrens 	    (uint64_t **)&vs, &vsc) == 0);
19295173954Sek 	verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
19395173954Sek 	    &stateval) == 0);
1943f9d6ad7SLin Ling 
1953f9d6ad7SLin Ling 	/*
1963f9d6ad7SLin Ling 	 * Currently resilvering a vdev
1973f9d6ad7SLin Ling 	 */
1983f9d6ad7SLin Ling 	(void) nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
1993f9d6ad7SLin Ling 	    (uint64_t **)&ps, &psc);
2003f9d6ad7SLin Ling 	if (ps && ps->pss_func == POOL_SCAN_RESILVER &&
2013f9d6ad7SLin Ling 	    ps->pss_state == DSS_SCANNING)
2023f9d6ad7SLin Ling 		return (ZPOOL_STATUS_RESILVERING);
20395173954Sek 
20495173954Sek 	/*
20595173954Sek 	 * Pool last accessed by another system.
20695173954Sek 	 */
2073f9d6ad7SLin Ling 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
20895173954Sek 	if (hostid != 0 && (unsigned long)hostid != gethostid() &&
20995173954Sek 	    stateval == POOL_STATE_ACTIVE)
21095173954Sek 		return (ZPOOL_STATUS_HOSTID_MISMATCH);
211fa9e4066Sahrens 
212eaca9bbdSeschrock 	/*
213eaca9bbdSeschrock 	 * Newer on-disk version.
214eaca9bbdSeschrock 	 */
215eaca9bbdSeschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
216eaca9bbdSeschrock 	    vs->vs_aux == VDEV_AUX_VERSION_NEWER)
217eaca9bbdSeschrock 		return (ZPOOL_STATUS_VERSION_NEWER);
218eaca9bbdSeschrock 
219ad135b5dSChristopher Siden 	/*
220ad135b5dSChristopher Siden 	 * Unsupported feature(s).
221ad135b5dSChristopher Siden 	 */
222ad135b5dSChristopher Siden 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
223ad135b5dSChristopher Siden 	    vs->vs_aux == VDEV_AUX_UNSUP_FEAT) {
224ad135b5dSChristopher Siden 		nvlist_t *nvinfo;
225ad135b5dSChristopher Siden 
226ad135b5dSChristopher Siden 		verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
227ad135b5dSChristopher Siden 		    &nvinfo) == 0);
228ad135b5dSChristopher Siden 		if (nvlist_exists(nvinfo, ZPOOL_CONFIG_CAN_RDONLY))
229ad135b5dSChristopher Siden 			return (ZPOOL_STATUS_UNSUP_FEAT_WRITE);
230ad135b5dSChristopher Siden 		return (ZPOOL_STATUS_UNSUP_FEAT_READ);
231ad135b5dSChristopher Siden 	}
232ad135b5dSChristopher Siden 
233fa9e4066Sahrens 	/*
234fa9e4066Sahrens 	 * Check that the config is complete.
235fa9e4066Sahrens 	 */
236fa9e4066Sahrens 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
237ea8dc4b6Seschrock 	    vs->vs_aux == VDEV_AUX_BAD_GUID_SUM)
238fa9e4066Sahrens 		return (ZPOOL_STATUS_BAD_GUID_SUM);
239fa9e4066Sahrens 
24032b87932Sek 	/*
241e14bb325SJeff Bonwick 	 * Check whether the pool has suspended due to failed I/O.
24232b87932Sek 	 */
243e14bb325SJeff Bonwick 	if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED,
244e14bb325SJeff Bonwick 	    &suspended) == 0) {
245e14bb325SJeff Bonwick 		if (suspended == ZIO_FAILURE_MODE_CONTINUE)
24632b87932Sek 			return (ZPOOL_STATUS_IO_FAILURE_CONTINUE);
247e14bb325SJeff Bonwick 		return (ZPOOL_STATUS_IO_FAILURE_WAIT);
24832b87932Sek 	}
24932b87932Sek 
250b87f3af3Sperrin 	/*
251b87f3af3Sperrin 	 * Could not read a log.
252b87f3af3Sperrin 	 */
253b87f3af3Sperrin 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
254b87f3af3Sperrin 	    vs->vs_aux == VDEV_AUX_BAD_LOG) {
255b87f3af3Sperrin 		return (ZPOOL_STATUS_BAD_LOG);
256b87f3af3Sperrin 	}
257b87f3af3Sperrin 
258fa9e4066Sahrens 	/*
2593d7072f8Seschrock 	 * Bad devices in non-replicated config.
260fa9e4066Sahrens 	 */
2613d7072f8Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
2623d7072f8Seschrock 	    find_vdev_problem(nvroot, vdev_faulted))
2633d7072f8Seschrock 		return (ZPOOL_STATUS_FAULTED_DEV_NR);
2643d7072f8Seschrock 
265ea8dc4b6Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
266ea8dc4b6Seschrock 	    find_vdev_problem(nvroot, vdev_missing))
267ea8dc4b6Seschrock 		return (ZPOOL_STATUS_MISSING_DEV_NR);
268ea8dc4b6Seschrock 
269ea8dc4b6Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
270ea8dc4b6Seschrock 	    find_vdev_problem(nvroot, vdev_broken))
271ea8dc4b6Seschrock 		return (ZPOOL_STATUS_CORRUPT_LABEL_NR);
272ea8dc4b6Seschrock 
273ea8dc4b6Seschrock 	/*
274ea8dc4b6Seschrock 	 * Corrupted pool metadata
275ea8dc4b6Seschrock 	 */
276ea8dc4b6Seschrock 	if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
277ea8dc4b6Seschrock 	    vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
278ea8dc4b6Seschrock 		return (ZPOOL_STATUS_CORRUPT_POOL);
279fa9e4066Sahrens 
280fa9e4066Sahrens 	/*
281ea8dc4b6Seschrock 	 * Persistent data errors.
282fa9e4066Sahrens 	 */
283ea8dc4b6Seschrock 	if (!isimport) {
284ea8dc4b6Seschrock 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
285ea8dc4b6Seschrock 		    &nerr) == 0 && nerr != 0)
286ea8dc4b6Seschrock 			return (ZPOOL_STATUS_CORRUPT_DATA);
287fa9e4066Sahrens 	}
288fa9e4066Sahrens 
289ea8dc4b6Seschrock 	/*
290ea8dc4b6Seschrock 	 * Missing devices in a replicated config.
291ea8dc4b6Seschrock 	 */
2923d7072f8Seschrock 	if (find_vdev_problem(nvroot, vdev_faulted))
2933d7072f8Seschrock 		return (ZPOOL_STATUS_FAULTED_DEV_R);
294ea8dc4b6Seschrock 	if (find_vdev_problem(nvroot, vdev_missing))
295ea8dc4b6Seschrock 		return (ZPOOL_STATUS_MISSING_DEV_R);
296ea8dc4b6Seschrock 	if (find_vdev_problem(nvroot, vdev_broken))
297ea8dc4b6Seschrock 		return (ZPOOL_STATUS_CORRUPT_LABEL_R);
298ea8dc4b6Seschrock 
299fa9e4066Sahrens 	/*
300fa9e4066Sahrens 	 * Devices with errors
301fa9e4066Sahrens 	 */
302fa9e4066Sahrens 	if (!isimport && find_vdev_problem(nvroot, vdev_errors))
303fa9e4066Sahrens 		return (ZPOOL_STATUS_FAILING_DEV);
304fa9e4066Sahrens 
305fa9e4066Sahrens 	/*
306fa9e4066Sahrens 	 * Offlined devices
307fa9e4066Sahrens 	 */
308fa9e4066Sahrens 	if (find_vdev_problem(nvroot, vdev_offlined))
309fa9e4066Sahrens 		return (ZPOOL_STATUS_OFFLINE_DEV);
310fa9e4066Sahrens 
311c25309d4SGeorge Wilson 	/*
312c25309d4SGeorge Wilson 	 * Removed device
313c25309d4SGeorge Wilson 	 */
314c25309d4SGeorge Wilson 	if (find_vdev_problem(nvroot, vdev_removed))
315c25309d4SGeorge Wilson 		return (ZPOOL_STATUS_REMOVED_DEV);
316c25309d4SGeorge Wilson 
317fa9e4066Sahrens 	/*
318eaca9bbdSeschrock 	 * Outdated, but usable, version
319fa9e4066Sahrens 	 */
320ad135b5dSChristopher Siden 	if (SPA_VERSION_IS_SUPPORTED(version) && version != SPA_VERSION)
321eaca9bbdSeschrock 		return (ZPOOL_STATUS_VERSION_OLDER);
322fa9e4066Sahrens 
323*57221772SChristopher Siden 	/*
324*57221772SChristopher Siden 	 * Usable pool with disabled features
325*57221772SChristopher Siden 	 */
326*57221772SChristopher Siden 	if (version >= SPA_VERSION_FEATURES) {
327*57221772SChristopher Siden 		int i;
328*57221772SChristopher Siden 		nvlist_t *feat;
329*57221772SChristopher Siden 
330*57221772SChristopher Siden 		if (isimport) {
331*57221772SChristopher Siden 			feat = fnvlist_lookup_nvlist(config,
332*57221772SChristopher Siden 			    ZPOOL_CONFIG_LOAD_INFO);
333*57221772SChristopher Siden 			feat = fnvlist_lookup_nvlist(feat,
334*57221772SChristopher Siden 			    ZPOOL_CONFIG_ENABLED_FEAT);
335*57221772SChristopher Siden 		} else {
336*57221772SChristopher Siden 			feat = fnvlist_lookup_nvlist(config,
337*57221772SChristopher Siden 			    ZPOOL_CONFIG_FEATURE_STATS);
338*57221772SChristopher Siden 		}
339*57221772SChristopher Siden 
340*57221772SChristopher Siden 		for (i = 0; i < SPA_FEATURES; i++) {
341*57221772SChristopher Siden 			zfeature_info_t *fi = &spa_feature_table[i];
342*57221772SChristopher Siden 			if (!nvlist_exists(feat, fi->fi_guid))
343*57221772SChristopher Siden 				return (ZPOOL_STATUS_FEAT_DISABLED);
344*57221772SChristopher Siden 		}
345*57221772SChristopher Siden 	}
346*57221772SChristopher Siden 
347fa9e4066Sahrens 	return (ZPOOL_STATUS_OK);
348fa9e4066Sahrens }
349fa9e4066Sahrens 
350fa9e4066Sahrens zpool_status_t
351fa9e4066Sahrens zpool_get_status(zpool_handle_t *zhp, char **msgid)
352fa9e4066Sahrens {
353e14bb325SJeff Bonwick 	zpool_status_t ret = check_status(zhp->zpool_config, B_FALSE);
354fa9e4066Sahrens 
355fa9e4066Sahrens 	if (ret >= NMSGID)
356fa9e4066Sahrens 		*msgid = NULL;
357fa9e4066Sahrens 	else
3583d7072f8Seschrock 		*msgid = zfs_msgid_table[ret];
359fa9e4066Sahrens 
360fa9e4066Sahrens 	return (ret);
361fa9e4066Sahrens }
362fa9e4066Sahrens 
363fa9e4066Sahrens zpool_status_t
364fa9e4066Sahrens zpool_import_status(nvlist_t *config, char **msgid)
365fa9e4066Sahrens {
366e14bb325SJeff Bonwick 	zpool_status_t ret = check_status(config, B_TRUE);
367fa9e4066Sahrens 
368fa9e4066Sahrens 	if (ret >= NMSGID)
369fa9e4066Sahrens 		*msgid = NULL;
370fa9e4066Sahrens 	else
37195173954Sek 		*msgid = zfs_msgid_table[ret];
372fa9e4066Sahrens 
373fa9e4066Sahrens 	return (ret);
374fa9e4066Sahrens }
3759eb19f4dSGeorge Wilson 
3769eb19f4dSGeorge Wilson static void
3779eb19f4dSGeorge Wilson dump_ddt_stat(const ddt_stat_t *dds, int h)
3789eb19f4dSGeorge Wilson {
3799eb19f4dSGeorge Wilson 	char refcnt[6];
3809eb19f4dSGeorge Wilson 	char blocks[6], lsize[6], psize[6], dsize[6];
3819eb19f4dSGeorge Wilson 	char ref_blocks[6], ref_lsize[6], ref_psize[6], ref_dsize[6];
3829eb19f4dSGeorge Wilson 
3839eb19f4dSGeorge Wilson 	if (dds == NULL || dds->dds_blocks == 0)
3849eb19f4dSGeorge Wilson 		return;
3859eb19f4dSGeorge Wilson 
3869eb19f4dSGeorge Wilson 	if (h == -1)
3879eb19f4dSGeorge Wilson 		(void) strcpy(refcnt, "Total");
3889eb19f4dSGeorge Wilson 	else
3899eb19f4dSGeorge Wilson 		zfs_nicenum(1ULL << h, refcnt, sizeof (refcnt));
3909eb19f4dSGeorge Wilson 
3919eb19f4dSGeorge Wilson 	zfs_nicenum(dds->dds_blocks, blocks, sizeof (blocks));
3929eb19f4dSGeorge Wilson 	zfs_nicenum(dds->dds_lsize, lsize, sizeof (lsize));
3939eb19f4dSGeorge Wilson 	zfs_nicenum(dds->dds_psize, psize, sizeof (psize));
3949eb19f4dSGeorge Wilson 	zfs_nicenum(dds->dds_dsize, dsize, sizeof (dsize));
3959eb19f4dSGeorge Wilson 	zfs_nicenum(dds->dds_ref_blocks, ref_blocks, sizeof (ref_blocks));
3969eb19f4dSGeorge Wilson 	zfs_nicenum(dds->dds_ref_lsize, ref_lsize, sizeof (ref_lsize));
3979eb19f4dSGeorge Wilson 	zfs_nicenum(dds->dds_ref_psize, ref_psize, sizeof (ref_psize));
3989eb19f4dSGeorge Wilson 	zfs_nicenum(dds->dds_ref_dsize, ref_dsize, sizeof (ref_dsize));
3999eb19f4dSGeorge Wilson 
4009eb19f4dSGeorge Wilson 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
4019eb19f4dSGeorge Wilson 	    refcnt,
4029eb19f4dSGeorge Wilson 	    blocks, lsize, psize, dsize,
4039eb19f4dSGeorge Wilson 	    ref_blocks, ref_lsize, ref_psize, ref_dsize);
4049eb19f4dSGeorge Wilson }
4059eb19f4dSGeorge Wilson 
4069eb19f4dSGeorge Wilson /*
4079eb19f4dSGeorge Wilson  * Print the DDT histogram and the column totals.
4089eb19f4dSGeorge Wilson  */
4099eb19f4dSGeorge Wilson void
4109eb19f4dSGeorge Wilson zpool_dump_ddt(const ddt_stat_t *dds_total, const ddt_histogram_t *ddh)
4119eb19f4dSGeorge Wilson {
4129eb19f4dSGeorge Wilson 	int h;
4139eb19f4dSGeorge Wilson 
4149eb19f4dSGeorge Wilson 	(void) printf("\n");
4159eb19f4dSGeorge Wilson 
4169eb19f4dSGeorge Wilson 	(void) printf("bucket   "
4179eb19f4dSGeorge Wilson 	    "           allocated             "
4189eb19f4dSGeorge Wilson 	    "          referenced          \n");
4199eb19f4dSGeorge Wilson 	(void) printf("______   "
4209eb19f4dSGeorge Wilson 	    "______________________________   "
4219eb19f4dSGeorge Wilson 	    "______________________________\n");
4229eb19f4dSGeorge Wilson 
4239eb19f4dSGeorge Wilson 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
4249eb19f4dSGeorge Wilson 	    "refcnt",
4259eb19f4dSGeorge Wilson 	    "blocks", "LSIZE", "PSIZE", "DSIZE",
4269eb19f4dSGeorge Wilson 	    "blocks", "LSIZE", "PSIZE", "DSIZE");
4279eb19f4dSGeorge Wilson 
4289eb19f4dSGeorge Wilson 	(void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
4299eb19f4dSGeorge Wilson 	    "------",
4309eb19f4dSGeorge Wilson 	    "------", "-----", "-----", "-----",
4319eb19f4dSGeorge Wilson 	    "------", "-----", "-----", "-----");
4329eb19f4dSGeorge Wilson 
4339eb19f4dSGeorge Wilson 	for (h = 0; h < 64; h++)
4349eb19f4dSGeorge Wilson 		dump_ddt_stat(&ddh->ddh_stat[h], h);
4359eb19f4dSGeorge Wilson 
4369eb19f4dSGeorge Wilson 	dump_ddt_stat(dds_total, -1);
4379eb19f4dSGeorge Wilson 
4389eb19f4dSGeorge Wilson 	(void) printf("\n");
4399eb19f4dSGeorge Wilson }
440