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
;
183 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
185 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
187 verify(nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_VDEV_STATS
,
188 (uint64_t **)&vs
, &vsc
) == 0);
189 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
193 * Currently resilvering a vdev
195 (void) nvlist_lookup_uint64_array(nvroot
, ZPOOL_CONFIG_SCAN_STATS
,
196 (uint64_t **)&ps
, &psc
);
197 if (ps
&& ps
->pss_func
== POOL_SCAN_RESILVER
&&
198 ps
->pss_state
== DSS_SCANNING
)
199 return (ZPOOL_STATUS_RESILVERING
);
202 * Pool last accessed by another system.
204 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_HOSTID
, &hostid
);
205 if (hostid
!= 0 && (unsigned long)hostid
!= gethostid() &&
206 stateval
== POOL_STATE_ACTIVE
)
207 return (ZPOOL_STATUS_HOSTID_MISMATCH
);
210 * Newer on-disk version.
212 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
213 vs
->vs_aux
== VDEV_AUX_VERSION_NEWER
)
214 return (ZPOOL_STATUS_VERSION_NEWER
);
217 * Check that the config is complete.
219 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
220 vs
->vs_aux
== VDEV_AUX_BAD_GUID_SUM
)
221 return (ZPOOL_STATUS_BAD_GUID_SUM
);
224 * Check whether the pool has suspended due to failed I/O.
226 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_SUSPENDED
,
228 if (suspended
== ZIO_FAILURE_MODE_CONTINUE
)
229 return (ZPOOL_STATUS_IO_FAILURE_CONTINUE
);
230 return (ZPOOL_STATUS_IO_FAILURE_WAIT
);
234 * Could not read a log.
236 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
237 vs
->vs_aux
== VDEV_AUX_BAD_LOG
) {
238 return (ZPOOL_STATUS_BAD_LOG
);
242 * Bad devices in non-replicated config.
244 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
245 find_vdev_problem(nvroot
, vdev_faulted
))
246 return (ZPOOL_STATUS_FAULTED_DEV_NR
);
248 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
249 find_vdev_problem(nvroot
, vdev_missing
))
250 return (ZPOOL_STATUS_MISSING_DEV_NR
);
252 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
253 find_vdev_problem(nvroot
, vdev_broken
))
254 return (ZPOOL_STATUS_CORRUPT_LABEL_NR
);
257 * Corrupted pool metadata
259 if (vs
->vs_state
== VDEV_STATE_CANT_OPEN
&&
260 vs
->vs_aux
== VDEV_AUX_CORRUPT_DATA
)
261 return (ZPOOL_STATUS_CORRUPT_POOL
);
264 * Persistent data errors.
267 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_ERRCOUNT
,
268 &nerr
) == 0 && nerr
!= 0)
269 return (ZPOOL_STATUS_CORRUPT_DATA
);
273 * Missing devices in a replicated config.
275 if (find_vdev_problem(nvroot
, vdev_faulted
))
276 return (ZPOOL_STATUS_FAULTED_DEV_R
);
277 if (find_vdev_problem(nvroot
, vdev_missing
))
278 return (ZPOOL_STATUS_MISSING_DEV_R
);
279 if (find_vdev_problem(nvroot
, vdev_broken
))
280 return (ZPOOL_STATUS_CORRUPT_LABEL_R
);
283 * Devices with errors
285 if (!isimport
&& find_vdev_problem(nvroot
, vdev_errors
))
286 return (ZPOOL_STATUS_FAILING_DEV
);
291 if (find_vdev_problem(nvroot
, vdev_offlined
))
292 return (ZPOOL_STATUS_OFFLINE_DEV
);
297 if (find_vdev_problem(nvroot
, vdev_removed
))
298 return (ZPOOL_STATUS_REMOVED_DEV
);
301 * Outdated, but usable, version
303 if (version
< SPA_VERSION
)
304 return (ZPOOL_STATUS_VERSION_OLDER
);
306 return (ZPOOL_STATUS_OK
);
310 zpool_get_status(zpool_handle_t
*zhp
, char **msgid
)
312 zpool_status_t ret
= check_status(zhp
->zpool_config
, B_FALSE
);
317 *msgid
= zfs_msgid_table
[ret
];
323 zpool_import_status(nvlist_t
*config
, char **msgid
)
325 zpool_status_t ret
= check_status(config
, B_TRUE
);
330 *msgid
= zfs_msgid_table
[ret
];
336 dump_ddt_stat(const ddt_stat_t
*dds
, int h
)
339 char blocks
[6], lsize
[6], psize
[6], dsize
[6];
340 char ref_blocks
[6], ref_lsize
[6], ref_psize
[6], ref_dsize
[6];
342 if (dds
== NULL
|| dds
->dds_blocks
== 0)
346 (void) strcpy(refcnt
, "Total");
348 zfs_nicenum(1ULL << h
, refcnt
, sizeof (refcnt
));
350 zfs_nicenum(dds
->dds_blocks
, blocks
, sizeof (blocks
));
351 zfs_nicenum(dds
->dds_lsize
, lsize
, sizeof (lsize
));
352 zfs_nicenum(dds
->dds_psize
, psize
, sizeof (psize
));
353 zfs_nicenum(dds
->dds_dsize
, dsize
, sizeof (dsize
));
354 zfs_nicenum(dds
->dds_ref_blocks
, ref_blocks
, sizeof (ref_blocks
));
355 zfs_nicenum(dds
->dds_ref_lsize
, ref_lsize
, sizeof (ref_lsize
));
356 zfs_nicenum(dds
->dds_ref_psize
, ref_psize
, sizeof (ref_psize
));
357 zfs_nicenum(dds
->dds_ref_dsize
, ref_dsize
, sizeof (ref_dsize
));
359 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
361 blocks
, lsize
, psize
, dsize
,
362 ref_blocks
, ref_lsize
, ref_psize
, ref_dsize
);
366 * Print the DDT histogram and the column totals.
369 zpool_dump_ddt(const ddt_stat_t
*dds_total
, const ddt_histogram_t
*ddh
)
375 (void) printf("bucket "
378 (void) printf("______ "
379 "______________________________ "
380 "______________________________\n");
382 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
384 "blocks", "LSIZE", "PSIZE", "DSIZE",
385 "blocks", "LSIZE", "PSIZE", "DSIZE");
387 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
389 "------", "-----", "-----", "-----",
390 "------", "-----", "-----", "-----");
392 for (h
= 0; h
< 64; h
++)
393 dump_ddt_stat(&ddh
->ddh_stat
[h
], h
);
395 dump_ddt_stat(dds_total
, -1);