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.
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.
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]
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.
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.
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
47 #include "libzfs_impl.h"
48 #include "zfeature_common.h"
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.
55 static char *zfs_msgid_table[] = {
72 #define NMSGID (sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
76 vdev_missing(vdev_stat_t *vs, uint_t vsc)
78 return (vs->vs_state == VDEV_STATE_CANT_OPEN &&
79 vs->vs_aux == VDEV_AUX_OPEN_FAILED);
84 vdev_faulted(vdev_stat_t *vs, uint_t vsc)
86 return (vs->vs_state == VDEV_STATE_FAULTED);
91 vdev_errors(vdev_stat_t *vs, uint_t vsc)
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);
100 vdev_broken(vdev_stat_t *vs, uint_t vsc)
102 return (vs->vs_state == VDEV_STATE_CANT_OPEN);
107 vdev_offlined(vdev_stat_t *vs, uint_t vsc)
109 return (vs->vs_state == VDEV_STATE_OFFLINE);
114 vdev_removed(vdev_stat_t *vs, uint_t vsc)
116 return (vs->vs_state == VDEV_STATE_REMOVED);
120 vdev_non_native_ashift(vdev_stat_t *vs, uint_t vsc)
122 return (VDEV_STAT_VALID(vs_physical_ashift, vsc) &&
123 vs->vs_configured_ashift < vs->vs_physical_ashift);
127 * Detect if any leaf devices that have seen errors or could not be opened.
130 find_vdev_problem(nvlist_t *vdev, int (*func)(vdev_stat_t *, uint_t),
131 boolean_t ignore_replacing)
135 uint_t c, vsc, children;
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
143 if (ignore_replacing == B_TRUE) {
146 verify(nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE,
148 if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
152 if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child,
154 for (c = 0; c < children; c++)
155 if (find_vdev_problem(child[c], func, ignore_replacing))
158 verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_VDEV_STATS,
159 (uint64_t **)&vs, &vsc) == 0);
161 if (func(vs, vsc) != 0)
166 * Check any L2 cache devs
168 if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_L2CACHE, &child,
170 for (c = 0; c < children; c++)
171 if (find_vdev_problem(child[c], func, ignore_replacing))
179 * Active pool health status.
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
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
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.
195 static zpool_status_t
196 check_status(nvlist_t *config, boolean_t isimport)
200 pool_scan_stat_t *ps = NULL;
208 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
210 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
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,
218 * Currently resilvering a vdev
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);
227 * Pool last accessed by another system.
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);
235 * Newer on-disk version.
237 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
238 vs->vs_aux == VDEV_AUX_VERSION_NEWER)
239 return (ZPOOL_STATUS_VERSION_NEWER);
242 * Unsupported feature(s).
244 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
245 vs->vs_aux == VDEV_AUX_UNSUP_FEAT) {
248 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
250 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_CAN_RDONLY))
251 return (ZPOOL_STATUS_UNSUP_FEAT_WRITE);
252 return (ZPOOL_STATUS_UNSUP_FEAT_READ);
256 * Check that the config is complete.
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);
263 * Check whether the pool has suspended due to failed I/O.
265 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED,
267 if (suspended == ZIO_FAILURE_MODE_CONTINUE)
268 return (ZPOOL_STATUS_IO_FAILURE_CONTINUE);
269 return (ZPOOL_STATUS_IO_FAILURE_WAIT);
273 * Could not read a log.
275 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
276 vs->vs_aux == VDEV_AUX_BAD_LOG) {
277 return (ZPOOL_STATUS_BAD_LOG);
281 * Bad devices in non-replicated config.
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);
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);
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);
296 * Corrupted pool metadata
298 if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
299 vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
300 return (ZPOOL_STATUS_CORRUPT_POOL);
303 * Persistent data errors.
306 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
307 &nerr) == 0 && nerr != 0)
308 return (ZPOOL_STATUS_CORRUPT_DATA);
312 * Missing devices in a replicated config.
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);
322 * Devices with errors
324 if (!isimport && find_vdev_problem(nvroot, vdev_errors, B_TRUE))
325 return (ZPOOL_STATUS_FAILING_DEV);
330 if (find_vdev_problem(nvroot, vdev_offlined, B_TRUE))
331 return (ZPOOL_STATUS_OFFLINE_DEV);
336 if (find_vdev_problem(nvroot, vdev_removed, B_TRUE))
337 return (ZPOOL_STATUS_REMOVED_DEV);
340 * Suboptimal, but usable, ashift configuration.
342 if (find_vdev_problem(nvroot, vdev_non_native_ashift, B_FALSE))
343 return (ZPOOL_STATUS_NON_NATIVE_ASHIFT);
346 * Outdated, but usable, version
348 if (SPA_VERSION_IS_SUPPORTED(version) && version != SPA_VERSION)
349 return (ZPOOL_STATUS_VERSION_OLDER);
352 * Usable pool with disabled features
354 if (version >= SPA_VERSION_FEATURES) {
359 feat = fnvlist_lookup_nvlist(config,
360 ZPOOL_CONFIG_LOAD_INFO);
361 feat = fnvlist_lookup_nvlist(feat,
362 ZPOOL_CONFIG_ENABLED_FEAT);
364 feat = fnvlist_lookup_nvlist(config,
365 ZPOOL_CONFIG_FEATURE_STATS);
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);
375 return (ZPOOL_STATUS_OK);
379 zpool_get_status(zpool_handle_t *zhp, char **msgid)
381 zpool_status_t ret = check_status(zhp->zpool_config, B_FALSE);
386 *msgid = zfs_msgid_table[ret];
392 zpool_import_status(nvlist_t *config, char **msgid)
394 zpool_status_t ret = check_status(config, B_TRUE);
399 *msgid = zfs_msgid_table[ret];
405 dump_ddt_stat(const ddt_stat_t *dds, int h)
408 char blocks[6], lsize[6], psize[6], dsize[6];
409 char ref_blocks[6], ref_lsize[6], ref_psize[6], ref_dsize[6];
411 if (dds == NULL || dds->dds_blocks == 0)
415 (void) strcpy(refcnt, "Total");
417 zfs_nicenum(1ULL << h, refcnt, sizeof (refcnt));
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));
428 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
430 blocks, lsize, psize, dsize,
431 ref_blocks, ref_lsize, ref_psize, ref_dsize);
435 * Print the DDT histogram and the column totals.
438 zpool_dump_ddt(const ddt_stat_t *dds_total, const ddt_histogram_t *ddh)
444 (void) printf("bucket "
447 (void) printf("______ "
448 "______________________________ "
449 "______________________________\n");
451 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
453 "blocks", "LSIZE", "PSIZE", "DSIZE",
454 "blocks", "LSIZE", "PSIZE", "DSIZE");
456 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
458 "------", "-----", "-----", "-----",
459 "------", "-----", "-----", "-----");
461 for (h = 0; h < 64; h++)
462 dump_ddt_stat(&ddh->ddh_stat[h], h);
464 dump_ddt_stat(dds_total, -1);