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
[] = {
73 #define NMSGID (sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
77 vdev_missing(uint64_t state
, uint64_t aux
, uint64_t errs
)
79 return (state
== VDEV_STATE_CANT_OPEN
&&
80 aux
== VDEV_AUX_OPEN_FAILED
);
85 vdev_faulted(uint64_t state
, uint64_t aux
, uint64_t errs
)
87 return (state
== VDEV_STATE_FAULTED
);
92 vdev_errors(uint64_t state
, uint64_t aux
, uint64_t errs
)
94 return (state
== VDEV_STATE_DEGRADED
|| errs
!= 0);
99 vdev_broken(uint64_t state
, uint64_t aux
, uint64_t errs
)
101 return (state
== VDEV_STATE_CANT_OPEN
);
106 vdev_offlined(uint64_t state
, uint64_t aux
, uint64_t errs
)
108 return (state
== VDEV_STATE_OFFLINE
);
113 vdev_removed(uint64_t state
, uint64_t aux
, uint64_t errs
)
115 return (state
== VDEV_STATE_REMOVED
);
119 * Detect if any leaf devices that have seen errors or could not be opened.
122 find_vdev_problem(nvlist_t
*vdev
, int (*func
)(uint64_t, uint64_t, uint64_t))
130 * Ignore problems within a 'replacing' vdev, since we're presumably in
131 * the process of repairing any such errors, and don't want to call them
132 * out again. We'll pick up the fact that a resilver is happening
135 verify(nvlist_lookup_string(vdev
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
136 if (strcmp(type
, VDEV_TYPE_REPLACING
) == 0)
139 if (nvlist_lookup_nvlist_array(vdev
, ZPOOL_CONFIG_CHILDREN
, &child
,
141 for (c
= 0; c
< children
; c
++)
142 if (find_vdev_problem(child
[c
], func
))
145 verify(nvlist_lookup_uint64_array(vdev
, ZPOOL_CONFIG_VDEV_STATS
,
146 (uint64_t **)&vs
, &c
) == 0);
148 if (func(vs
->vs_state
, vs
->vs_aux
,
150 vs
->vs_write_errors
+
151 vs
->vs_checksum_errors
))
156 * Check any L2 cache devs
158 if (nvlist_lookup_nvlist_array(vdev
, ZPOOL_CONFIG_L2CACHE
, &child
,
160 for (c
= 0; c
< children
; c
++)
161 if (find_vdev_problem(child
[c
], func
))
169 * Active pool health status.
171 * To determine the status for a pool, we make several passes over the config,
172 * picking the most egregious error we find. In order of importance, we do the
175 * - Check for a complete and valid configuration
176 * - Look for any faulted or missing devices in a non-replicated config
177 * - Check for any data errors
178 * - Check for any faulted or missing devices in a replicated config
179 * - Look for any devices showing errors
180 * - Check for any resilvering devices
182 * There can obviously be multiple errors within a single pool, so this routine
183 * only picks the most damaging of all the current errors to report.
185 static zpool_status_t
186 check_status(nvlist_t
*config
, boolean_t isimport
, zpool_errata_t
*erratap
)
190 pool_scan_stat_t
*ps
= NULL
;
198 unsigned long system_hostid
= get_system_hostid();
200 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
202 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
204 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
205 (uint64_t **)&vs
, &vsc
) == 0);
206 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
210 * Currently resilvering a vdev
212 (void) nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
213 (uint64_t **)&ps
, &psc
);
214 if (ps
&& ps
->pss_func
== POOL_SCAN_RESILVER
&&
215 ps
->pss_state
== DSS_SCANNING
)
216 return (ZPOOL_STATUS_RESILVERING
);
219 * Pool last accessed by another system.
221 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
222 if (hostid
!= 0 && (unsigned long)hostid
!= system_hostid
&&
223 stateval
== POOL_STATE_ACTIVE
)
224 return (ZPOOL_STATUS_HOSTID_MISMATCH
);
227 * Newer on-disk version.
229 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
230 vs
->vs_aux
== VDEV_AUX_VERSION_NEWER
)
231 return (ZPOOL_STATUS_VERSION_NEWER
);
234 * Unsupported feature(s).
236 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
237 vs
->vs_aux
== VDEV_AUX_UNSUP_FEAT
) {
240 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
242 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_CAN_RDONLY
))
243 return (ZPOOL_STATUS_UNSUP_FEAT_WRITE
);
244 return (ZPOOL_STATUS_UNSUP_FEAT_READ
);
248 * Check that the config is complete.
250 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
251 vs
->vs_aux
== VDEV_AUX_BAD_GUID_SUM
)
252 return (ZPOOL_STATUS_BAD_GUID_SUM
);
255 * Check whether the pool has suspended due to failed I/O.
257 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_SUSPENDED
,
259 if (suspended
== ZIO_FAILURE_MODE_CONTINUE
)
260 return (ZPOOL_STATUS_IO_FAILURE_CONTINUE
);
261 return (ZPOOL_STATUS_IO_FAILURE_WAIT
);
265 * Could not read a log.
267 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
268 vs
->vs_aux
== VDEV_AUX_BAD_LOG
) {
269 return (ZPOOL_STATUS_BAD_LOG
);
273 * Bad devices in non-replicated config.
275 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
276 find_vdev_problem(nvroot
, vdev_faulted
))
277 return (ZPOOL_STATUS_FAULTED_DEV_NR
);
279 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
280 find_vdev_problem(nvroot
, vdev_missing
))
281 return (ZPOOL_STATUS_MISSING_DEV_NR
);
283 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
284 find_vdev_problem(nvroot
, vdev_broken
))
285 return (ZPOOL_STATUS_CORRUPT_LABEL_NR
);
288 * Corrupted pool metadata
290 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
291 vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)
292 return (ZPOOL_STATUS_CORRUPT_POOL
);
295 * Persistent data errors.
298 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
299 &nerr
) == 0 && nerr
!= 0)
300 return (ZPOOL_STATUS_CORRUPT_DATA
);
304 * Missing devices in a replicated config.
306 if (find_vdev_problem(nvroot
, vdev_faulted
))
307 return (ZPOOL_STATUS_FAULTED_DEV_R
);
308 if (find_vdev_problem(nvroot
, vdev_missing
))
309 return (ZPOOL_STATUS_MISSING_DEV_R
);
310 if (find_vdev_problem(nvroot
, vdev_broken
))
311 return (ZPOOL_STATUS_CORRUPT_LABEL_R
);
314 * Devices with errors
316 if (!isimport
&& find_vdev_problem(nvroot
, vdev_errors
))
317 return (ZPOOL_STATUS_FAILING_DEV
);
322 if (find_vdev_problem(nvroot
, vdev_offlined
))
323 return (ZPOOL_STATUS_OFFLINE_DEV
);
328 if (find_vdev_problem(nvroot
, vdev_removed
))
329 return (ZPOOL_STATUS_REMOVED_DEV
);
332 * Outdated, but usable, version
334 if (SPA_VERSION_IS_SUPPORTED(version
) && version
!= SPA_VERSION
)
335 return (ZPOOL_STATUS_VERSION_OLDER
);
338 * Usable pool with disabled features
340 if (version
>= SPA_VERSION_FEATURES
) {
345 feat
= fnvlist_lookup_nvlist(config
,
346 ZPOOL_CONFIG_LOAD_INFO
);
347 feat
= fnvlist_lookup_nvlist(feat
,
348 ZPOOL_CONFIG_ENABLED_FEAT
);
350 feat
= fnvlist_lookup_nvlist(config
,
351 ZPOOL_CONFIG_FEATURE_STATS
);
354 for (i
= 0; i
< SPA_FEATURES
; i
++) {
355 zfeature_info_t
*fi
= &spa_feature_table
[i
];
356 if (!nvlist_exists(feat
, fi
->fi_guid
))
357 return (ZPOOL_STATUS_FEAT_DISABLED
);
362 * Informational errata available.
364 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRATA
, &errata
);
367 return (ZPOOL_STATUS_ERRATA
);
370 return (ZPOOL_STATUS_OK
);
374 zpool_get_status(zpool_handle_t
*zhp
, char **msgid
, zpool_errata_t
*errata
)
376 zpool_status_t ret
= check_status(zhp
->zpool_config
, B_FALSE
, errata
);
381 *msgid
= zfs_msgid_table
[ret
];
387 zpool_import_status(nvlist_t
*config
, char **msgid
, zpool_errata_t
*errata
)
389 zpool_status_t ret
= check_status(config
, B_TRUE
, errata
);
394 *msgid
= zfs_msgid_table
[ret
];
400 dump_ddt_stat(const ddt_stat_t
*dds
, int h
)
403 char blocks
[6], lsize
[6], psize
[6], dsize
[6];
404 char ref_blocks
[6], ref_lsize
[6], ref_psize
[6], ref_dsize
[6];
406 if (dds
== NULL
|| dds
->dds_blocks
== 0)
410 (void) strcpy(refcnt
, "Total");
412 zfs_nicenum(1ULL << h
, refcnt
, sizeof (refcnt
));
414 zfs_nicenum(dds
->dds_blocks
, blocks
, sizeof (blocks
));
415 zfs_nicebytes(dds
->dds_lsize
, lsize
, sizeof (lsize
));
416 zfs_nicebytes(dds
->dds_psize
, psize
, sizeof (psize
));
417 zfs_nicebytes(dds
->dds_dsize
, dsize
, sizeof (dsize
));
418 zfs_nicenum(dds
->dds_ref_blocks
, ref_blocks
, sizeof (ref_blocks
));
419 zfs_nicebytes(dds
->dds_ref_lsize
, ref_lsize
, sizeof (ref_lsize
));
420 zfs_nicebytes(dds
->dds_ref_psize
, ref_psize
, sizeof (ref_psize
));
421 zfs_nicebytes(dds
->dds_ref_dsize
, ref_dsize
, sizeof (ref_dsize
));
423 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
425 blocks
, lsize
, psize
, dsize
,
426 ref_blocks
, ref_lsize
, ref_psize
, ref_dsize
);
430 * Print the DDT histogram and the column totals.
433 zpool_dump_ddt(const ddt_stat_t
*dds_total
, const ddt_histogram_t
*ddh
)
439 (void) printf("bucket "
442 (void) printf("______ "
443 "______________________________ "
444 "______________________________\n");
446 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
448 "blocks", "LSIZE", "PSIZE", "DSIZE",
449 "blocks", "LSIZE", "PSIZE", "DSIZE");
451 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
453 "------", "-----", "-----", "-----",
454 "------", "-----", "-----", "-----");
456 for (h
= 0; h
< 64; h
++)
457 dump_ddt_stat(&ddh
->ddh_stat
[h
], h
);
459 dump_ddt_stat(dds_total
, -1);