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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26 * This file contains the functions which analyze the status of a pool. This
27 * include both the status of an active pool, as well as the status exported
28 * pools. Returns one of the ZPOOL_STATUS_* defines describing the status of
29 * the pool. This status is independent (to a certain degree) from the state of
30 * the pool. A pool's state describes only whether or not it is capable of
31 * providing the necessary fault tolerance for data. The status describes the
32 * overall status of devices. A pool that is online can still have a device
33 * that is experiencing errors.
35 * Only a subset of the possible faults can be detected using 'zpool status',
36 * and not all possible errors correspond to a FMA message ID. The explanation
37 * is left up to the caller, depending on whether it is a live pool or an
44 #include "libzfs_impl.h"
47 * Message ID table. This must be kept in sync with the ZPOOL_STATUS_* defines
48 * in libzfs.h. Note that there are some status results which go past the end
49 * of this table, and hence have no associated message ID.
51 static char *zfs_msgid_table
[] = {
68 #define NMSGID (sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
72 vdev_missing(uint64_t state
, uint64_t aux
, uint64_t errs
)
74 return (state
== VDEV_STATE_CANT_OPEN
&&
75 aux
== VDEV_AUX_OPEN_FAILED
);
80 vdev_faulted(uint64_t state
, uint64_t aux
, uint64_t errs
)
82 return (state
== VDEV_STATE_FAULTED
);
87 vdev_errors(uint64_t state
, uint64_t aux
, uint64_t errs
)
89 return (state
== VDEV_STATE_DEGRADED
|| errs
!= 0);
94 vdev_broken(uint64_t state
, uint64_t aux
, uint64_t errs
)
96 return (state
== VDEV_STATE_CANT_OPEN
);
101 vdev_offlined(uint64_t state
, uint64_t aux
, uint64_t errs
)
103 return (state
== VDEV_STATE_OFFLINE
);
108 vdev_removed(uint64_t state
, uint64_t aux
, uint64_t errs
)
110 return (state
== VDEV_STATE_REMOVED
);
114 * Detect if any leaf devices that have seen errors or could not be opened.
117 find_vdev_problem(nvlist_t
*vdev
, int (*func
)(uint64_t, uint64_t, uint64_t))
125 * Ignore problems within a 'replacing' vdev, since we're presumably in
126 * the process of repairing any such errors, and don't want to call them
127 * out again. We'll pick up the fact that a resilver is happening
130 verify(nvlist_lookup_string(vdev
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
131 if (strcmp(type
, VDEV_TYPE_REPLACING
) == 0)
134 if (nvlist_lookup_nvlist_array(vdev
, ZPOOL_CONFIG_CHILDREN
, &child
,
136 for (c
= 0; c
< children
; c
++)
137 if (find_vdev_problem(child
[c
], func
))
140 verify(nvlist_lookup_uint64_array(vdev
, ZPOOL_CONFIG_VDEV_STATS
,
141 (uint64_t **)&vs
, &c
) == 0);
143 if (func(vs
->vs_state
, vs
->vs_aux
,
145 vs
->vs_write_errors
+
146 vs
->vs_checksum_errors
))
154 * Active pool health status.
156 * To determine the status for a pool, we make several passes over the config,
157 * picking the most egregious error we find. In order of importance, we do the
160 * - Check for a complete and valid configuration
161 * - Look for any faulted or missing devices in a non-replicated config
162 * - Check for any data errors
163 * - Check for any faulted or missing devices in a replicated config
164 * - Look for any devices showing errors
165 * - Check for any resilvering devices
167 * There can obviously be multiple errors within a single pool, so this routine
168 * only picks the most damaging of all the current errors to report.
170 static zpool_status_t
171 check_status(nvlist_t
*config
, boolean_t isimport
)
175 pool_scan_stat_t
*ps
= NULL
;
182 unsigned long system_hostid
= gethostid() & 0xffffffff;
184 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
186 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
188 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
189 (uint64_t **)&vs
, &vsc
) == 0);
190 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
194 * Currently resilvering a vdev
196 (void) nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
197 (uint64_t **)&ps
, &psc
);
198 if (ps
&& ps
->pss_func
== POOL_SCAN_RESILVER
&&
199 ps
->pss_state
== DSS_SCANNING
)
200 return (ZPOOL_STATUS_RESILVERING
);
203 * Pool last accessed by another system.
205 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
206 if (hostid
!= 0 && (unsigned long)hostid
!= system_hostid
&&
207 stateval
== POOL_STATE_ACTIVE
)
208 return (ZPOOL_STATUS_HOSTID_MISMATCH
);
211 * Newer on-disk version.
213 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
214 vs
->vs_aux
== VDEV_AUX_VERSION_NEWER
)
215 return (ZPOOL_STATUS_VERSION_NEWER
);
218 * Check that the config is complete.
220 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
221 vs
->vs_aux
== VDEV_AUX_BAD_GUID_SUM
)
222 return (ZPOOL_STATUS_BAD_GUID_SUM
);
225 * Check whether the pool has suspended due to failed I/O.
227 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_SUSPENDED
,
229 if (suspended
== ZIO_FAILURE_MODE_CONTINUE
)
230 return (ZPOOL_STATUS_IO_FAILURE_CONTINUE
);
231 return (ZPOOL_STATUS_IO_FAILURE_WAIT
);
235 * Could not read a log.
237 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
238 vs
->vs_aux
== VDEV_AUX_BAD_LOG
) {
239 return (ZPOOL_STATUS_BAD_LOG
);
243 * Bad devices in non-replicated config.
245 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
246 find_vdev_problem(nvroot
, vdev_faulted
))
247 return (ZPOOL_STATUS_FAULTED_DEV_NR
);
249 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
250 find_vdev_problem(nvroot
, vdev_missing
))
251 return (ZPOOL_STATUS_MISSING_DEV_NR
);
253 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
254 find_vdev_problem(nvroot
, vdev_broken
))
255 return (ZPOOL_STATUS_CORRUPT_LABEL_NR
);
258 * Corrupted pool metadata
260 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
261 vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)
262 return (ZPOOL_STATUS_CORRUPT_POOL
);
265 * Persistent data errors.
268 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
269 &nerr
) == 0 && nerr
!= 0)
270 return (ZPOOL_STATUS_CORRUPT_DATA
);
274 * Missing devices in a replicated config.
276 if (find_vdev_problem(nvroot
, vdev_faulted
))
277 return (ZPOOL_STATUS_FAULTED_DEV_R
);
278 if (find_vdev_problem(nvroot
, vdev_missing
))
279 return (ZPOOL_STATUS_MISSING_DEV_R
);
280 if (find_vdev_problem(nvroot
, vdev_broken
))
281 return (ZPOOL_STATUS_CORRUPT_LABEL_R
);
284 * Devices with errors
286 if (!isimport
&& find_vdev_problem(nvroot
, vdev_errors
))
287 return (ZPOOL_STATUS_FAILING_DEV
);
292 if (find_vdev_problem(nvroot
, vdev_offlined
))
293 return (ZPOOL_STATUS_OFFLINE_DEV
);
298 if (find_vdev_problem(nvroot
, vdev_removed
))
299 return (ZPOOL_STATUS_REMOVED_DEV
);
302 * Outdated, but usable, version
304 if (version
< SPA_VERSION
)
305 return (ZPOOL_STATUS_VERSION_OLDER
);
307 return (ZPOOL_STATUS_OK
);
311 zpool_get_status(zpool_handle_t
*zhp
, char **msgid
)
313 zpool_status_t ret
= check_status(zhp
->zpool_config
, B_FALSE
);
318 *msgid
= zfs_msgid_table
[ret
];
324 zpool_import_status(nvlist_t
*config
, char **msgid
)
326 zpool_status_t ret
= check_status(config
, B_TRUE
);
331 *msgid
= zfs_msgid_table
[ret
];
337 dump_ddt_stat(const ddt_stat_t
*dds
, int h
)
340 char blocks
[6], lsize
[6], psize
[6], dsize
[6];
341 char ref_blocks
[6], ref_lsize
[6], ref_psize
[6], ref_dsize
[6];
343 if (dds
== NULL
|| dds
->dds_blocks
== 0)
347 (void) strcpy(refcnt
, "Total");
349 zfs_nicenum(1ULL << h
, refcnt
, sizeof (refcnt
));
351 zfs_nicenum(dds
->dds_blocks
, blocks
, sizeof (blocks
));
352 zfs_nicenum(dds
->dds_lsize
, lsize
, sizeof (lsize
));
353 zfs_nicenum(dds
->dds_psize
, psize
, sizeof (psize
));
354 zfs_nicenum(dds
->dds_dsize
, dsize
, sizeof (dsize
));
355 zfs_nicenum(dds
->dds_ref_blocks
, ref_blocks
, sizeof (ref_blocks
));
356 zfs_nicenum(dds
->dds_ref_lsize
, ref_lsize
, sizeof (ref_lsize
));
357 zfs_nicenum(dds
->dds_ref_psize
, ref_psize
, sizeof (ref_psize
));
358 zfs_nicenum(dds
->dds_ref_dsize
, ref_dsize
, sizeof (ref_dsize
));
360 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
362 blocks
, lsize
, psize
, dsize
,
363 ref_blocks
, ref_lsize
, ref_psize
, ref_dsize
);
367 * Print the DDT histogram and the column totals.
370 zpool_dump_ddt(const ddt_stat_t
*dds_total
, const ddt_histogram_t
*ddh
)
376 (void) printf("bucket "
379 (void) printf("______ "
380 "______________________________ "
381 "______________________________\n");
383 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
385 "blocks", "LSIZE", "PSIZE", "DSIZE",
386 "blocks", "LSIZE", "PSIZE", "DSIZE");
388 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
390 "------", "-----", "-----", "-----",
391 "------", "-----", "-----", "-----");
393 for (h
= 0; h
< 64; h
++)
394 dump_ddt_stat(&ddh
->ddh_stat
[h
], h
);
396 dump_ddt_stat(dds_total
, -1);