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 */ 21fa9e4066Sahrens /* 2295173954Sek * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27fa9e4066Sahrens 28fa9e4066Sahrens /* 29fa9e4066Sahrens * This file contains the functions which analyze the status of a pool. This 30fa9e4066Sahrens * include both the status of an active pool, as well as the status exported 31fa9e4066Sahrens * pools. Returns one of the ZPOOL_STATUS_* defines describing the status of 32fa9e4066Sahrens * the pool. This status is independent (to a certain degree) from the state of 33*3d7072f8Seschrock * the pool. A pool's state describes only whether or not it is capable of 34fa9e4066Sahrens * providing the necessary fault tolerance for data. The status describes the 35fa9e4066Sahrens * overall status of devices. A pool that is online can still have a device 36fa9e4066Sahrens * that is experiencing errors. 37fa9e4066Sahrens * 38fa9e4066Sahrens * Only a subset of the possible faults can be detected using 'zpool status', 39fa9e4066Sahrens * and not all possible errors correspond to a FMA message ID. The explanation 40fa9e4066Sahrens * is left up to the caller, depending on whether it is a live pool or an 41fa9e4066Sahrens * import. 42fa9e4066Sahrens */ 43fa9e4066Sahrens 44fa9e4066Sahrens #include <libzfs.h> 45fa9e4066Sahrens #include <string.h> 4695173954Sek #include <unistd.h> 47fa9e4066Sahrens #include "libzfs_impl.h" 48fa9e4066Sahrens 49fa9e4066Sahrens /* 50*3d7072f8Seschrock * 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", 6595173954Sek "ZFS-8000-EY" 66fa9e4066Sahrens }; 67fa9e4066Sahrens 6895173954Sek #define NMSGID (sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0])) 69fa9e4066Sahrens 70fa9e4066Sahrens /* ARGSUSED */ 71fa9e4066Sahrens static int 72fa9e4066Sahrens vdev_missing(uint64_t state, uint64_t aux, uint64_t errs) 73fa9e4066Sahrens { 74fa9e4066Sahrens return (state == VDEV_STATE_CANT_OPEN && 75fa9e4066Sahrens aux == VDEV_AUX_OPEN_FAILED); 76fa9e4066Sahrens } 77fa9e4066Sahrens 78*3d7072f8Seschrock /* ARGSUSED */ 79*3d7072f8Seschrock static int 80*3d7072f8Seschrock vdev_faulted(uint64_t state, uint64_t aux, uint64_t errs) 81*3d7072f8Seschrock { 82*3d7072f8Seschrock return (state == VDEV_STATE_FAULTED); 83*3d7072f8Seschrock } 84*3d7072f8Seschrock 85fa9e4066Sahrens /* ARGSUSED */ 86fa9e4066Sahrens static int 87fa9e4066Sahrens vdev_errors(uint64_t state, uint64_t aux, uint64_t errs) 88fa9e4066Sahrens { 89*3d7072f8Seschrock return (state == VDEV_STATE_DEGRADED || errs != 0); 90fa9e4066Sahrens } 91fa9e4066Sahrens 92fa9e4066Sahrens /* ARGSUSED */ 93fa9e4066Sahrens static int 94fa9e4066Sahrens vdev_broken(uint64_t state, uint64_t aux, uint64_t errs) 95fa9e4066Sahrens { 96fa9e4066Sahrens return (state == VDEV_STATE_CANT_OPEN); 97fa9e4066Sahrens } 98fa9e4066Sahrens 99fa9e4066Sahrens /* ARGSUSED */ 100fa9e4066Sahrens static int 101fa9e4066Sahrens vdev_offlined(uint64_t state, uint64_t aux, uint64_t errs) 102fa9e4066Sahrens { 103fa9e4066Sahrens return (state == VDEV_STATE_OFFLINE); 104fa9e4066Sahrens } 105fa9e4066Sahrens 106fa9e4066Sahrens /* 107fa9e4066Sahrens * Detect if any leaf devices that have seen errors or could not be opened. 108fa9e4066Sahrens */ 10999653d4eSeschrock static boolean_t 110fa9e4066Sahrens find_vdev_problem(nvlist_t *vdev, int (*func)(uint64_t, uint64_t, uint64_t)) 111fa9e4066Sahrens { 112fa9e4066Sahrens nvlist_t **child; 113fa9e4066Sahrens vdev_stat_t *vs; 114fa9e4066Sahrens uint_t c, children; 115fa9e4066Sahrens char *type; 116fa9e4066Sahrens 117fa9e4066Sahrens /* 118fa9e4066Sahrens * Ignore problems within a 'replacing' vdev, since we're presumably in 119fa9e4066Sahrens * the process of repairing any such errors, and don't want to call them 120fa9e4066Sahrens * out again. We'll pick up the fact that a resilver is happening 121fa9e4066Sahrens * later. 122fa9e4066Sahrens */ 123fa9e4066Sahrens verify(nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type) == 0); 124fa9e4066Sahrens if (strcmp(type, VDEV_TYPE_REPLACING) == 0) 12599653d4eSeschrock return (B_FALSE); 126fa9e4066Sahrens 127fa9e4066Sahrens if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child, 128fa9e4066Sahrens &children) == 0) { 129fa9e4066Sahrens for (c = 0; c < children; c++) 130fa9e4066Sahrens if (find_vdev_problem(child[c], func)) 13199653d4eSeschrock return (B_TRUE); 132fa9e4066Sahrens } else { 133fa9e4066Sahrens verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_STATS, 134fa9e4066Sahrens (uint64_t **)&vs, &c) == 0); 135fa9e4066Sahrens 136fa9e4066Sahrens if (func(vs->vs_state, vs->vs_aux, 137fa9e4066Sahrens vs->vs_read_errors + 138fa9e4066Sahrens vs->vs_write_errors + 139fa9e4066Sahrens vs->vs_checksum_errors)) 14099653d4eSeschrock return (B_TRUE); 141fa9e4066Sahrens } 142fa9e4066Sahrens 14399653d4eSeschrock return (B_FALSE); 144fa9e4066Sahrens } 145fa9e4066Sahrens 146fa9e4066Sahrens /* 147fa9e4066Sahrens * Active pool health status. 148fa9e4066Sahrens * 149fa9e4066Sahrens * To determine the status for a pool, we make several passes over the config, 150fa9e4066Sahrens * picking the most egregious error we find. In order of importance, we do the 151fa9e4066Sahrens * following: 152fa9e4066Sahrens * 153fa9e4066Sahrens * - Check for a complete and valid configuration 154*3d7072f8Seschrock * - Look for any faulted or missing devices in a non-replicated config 155fa9e4066Sahrens * - Check for any data errors 156*3d7072f8Seschrock * - Check for any faulted or missing devices in a replicated config 157ea8dc4b6Seschrock * - Look for any devices showing errors 158fa9e4066Sahrens * - Check for any resilvering devices 159fa9e4066Sahrens * 160fa9e4066Sahrens * There can obviously be multiple errors within a single pool, so this routine 161fa9e4066Sahrens * only picks the most damaging of all the current errors to report. 162fa9e4066Sahrens */ 163fa9e4066Sahrens static zpool_status_t 16499653d4eSeschrock check_status(nvlist_t *config, boolean_t isimport) 165fa9e4066Sahrens { 166fa9e4066Sahrens nvlist_t *nvroot; 167fa9e4066Sahrens vdev_stat_t *vs; 168fa9e4066Sahrens uint_t vsc; 169ea8dc4b6Seschrock uint64_t nerr; 170eaca9bbdSeschrock uint64_t version; 17195173954Sek uint64_t stateval; 17295173954Sek uint64_t hostid = 0; 173fa9e4066Sahrens 174eaca9bbdSeschrock verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 175eaca9bbdSeschrock &version) == 0); 176fa9e4066Sahrens verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 177fa9e4066Sahrens &nvroot) == 0); 178fa9e4066Sahrens verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_STATS, 179fa9e4066Sahrens (uint64_t **)&vs, &vsc) == 0); 18095173954Sek verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, 18195173954Sek &stateval) == 0); 18295173954Sek (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid); 18395173954Sek 18495173954Sek /* 18595173954Sek * Pool last accessed by another system. 18695173954Sek */ 18795173954Sek if (hostid != 0 && (unsigned long)hostid != gethostid() && 18895173954Sek stateval == POOL_STATE_ACTIVE) 18995173954Sek return (ZPOOL_STATUS_HOSTID_MISMATCH); 190fa9e4066Sahrens 191eaca9bbdSeschrock /* 192eaca9bbdSeschrock * Newer on-disk version. 193eaca9bbdSeschrock */ 194eaca9bbdSeschrock if (vs->vs_state == VDEV_STATE_CANT_OPEN && 195eaca9bbdSeschrock vs->vs_aux == VDEV_AUX_VERSION_NEWER) 196eaca9bbdSeschrock return (ZPOOL_STATUS_VERSION_NEWER); 197eaca9bbdSeschrock 198fa9e4066Sahrens /* 199fa9e4066Sahrens * Check that the config is complete. 200fa9e4066Sahrens */ 201fa9e4066Sahrens if (vs->vs_state == VDEV_STATE_CANT_OPEN && 202ea8dc4b6Seschrock vs->vs_aux == VDEV_AUX_BAD_GUID_SUM) 203fa9e4066Sahrens return (ZPOOL_STATUS_BAD_GUID_SUM); 204fa9e4066Sahrens 205fa9e4066Sahrens /* 206*3d7072f8Seschrock * Bad devices in non-replicated config. 207fa9e4066Sahrens */ 208*3d7072f8Seschrock if (vs->vs_state == VDEV_STATE_CANT_OPEN && 209*3d7072f8Seschrock find_vdev_problem(nvroot, vdev_faulted)) 210*3d7072f8Seschrock return (ZPOOL_STATUS_FAULTED_DEV_NR); 211*3d7072f8Seschrock 212ea8dc4b6Seschrock if (vs->vs_state == VDEV_STATE_CANT_OPEN && 213ea8dc4b6Seschrock find_vdev_problem(nvroot, vdev_missing)) 214ea8dc4b6Seschrock return (ZPOOL_STATUS_MISSING_DEV_NR); 215ea8dc4b6Seschrock 216ea8dc4b6Seschrock if (vs->vs_state == VDEV_STATE_CANT_OPEN && 217ea8dc4b6Seschrock find_vdev_problem(nvroot, vdev_broken)) 218ea8dc4b6Seschrock return (ZPOOL_STATUS_CORRUPT_LABEL_NR); 219ea8dc4b6Seschrock 220ea8dc4b6Seschrock /* 221ea8dc4b6Seschrock * Corrupted pool metadata 222ea8dc4b6Seschrock */ 223ea8dc4b6Seschrock if (vs->vs_state == VDEV_STATE_CANT_OPEN && 224ea8dc4b6Seschrock vs->vs_aux == VDEV_AUX_CORRUPT_DATA) 225ea8dc4b6Seschrock return (ZPOOL_STATUS_CORRUPT_POOL); 226fa9e4066Sahrens 227fa9e4066Sahrens /* 228ea8dc4b6Seschrock * Persistent data errors. 229fa9e4066Sahrens */ 230ea8dc4b6Seschrock if (!isimport) { 231ea8dc4b6Seschrock if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT, 232ea8dc4b6Seschrock &nerr) == 0 && nerr != 0) 233ea8dc4b6Seschrock return (ZPOOL_STATUS_CORRUPT_DATA); 234fa9e4066Sahrens } 235fa9e4066Sahrens 236ea8dc4b6Seschrock /* 237ea8dc4b6Seschrock * Missing devices in a replicated config. 238ea8dc4b6Seschrock */ 239*3d7072f8Seschrock if (find_vdev_problem(nvroot, vdev_faulted)) 240*3d7072f8Seschrock return (ZPOOL_STATUS_FAULTED_DEV_R); 241ea8dc4b6Seschrock if (find_vdev_problem(nvroot, vdev_missing)) 242ea8dc4b6Seschrock return (ZPOOL_STATUS_MISSING_DEV_R); 243ea8dc4b6Seschrock if (find_vdev_problem(nvroot, vdev_broken)) 244ea8dc4b6Seschrock return (ZPOOL_STATUS_CORRUPT_LABEL_R); 245ea8dc4b6Seschrock 246fa9e4066Sahrens /* 247fa9e4066Sahrens * Devices with errors 248fa9e4066Sahrens */ 249fa9e4066Sahrens if (!isimport && find_vdev_problem(nvroot, vdev_errors)) 250fa9e4066Sahrens return (ZPOOL_STATUS_FAILING_DEV); 251fa9e4066Sahrens 252fa9e4066Sahrens /* 253fa9e4066Sahrens * Offlined devices 254fa9e4066Sahrens */ 255fa9e4066Sahrens if (find_vdev_problem(nvroot, vdev_offlined)) 256fa9e4066Sahrens return (ZPOOL_STATUS_OFFLINE_DEV); 257fa9e4066Sahrens 258fa9e4066Sahrens /* 259fa9e4066Sahrens * Currently resilvering 260fa9e4066Sahrens */ 261fa9e4066Sahrens if (!vs->vs_scrub_complete && vs->vs_scrub_type == POOL_SCRUB_RESILVER) 262fa9e4066Sahrens return (ZPOOL_STATUS_RESILVERING); 263fa9e4066Sahrens 264fa9e4066Sahrens /* 265eaca9bbdSeschrock * Outdated, but usable, version 266fa9e4066Sahrens */ 267eaca9bbdSeschrock if (version < ZFS_VERSION) 268eaca9bbdSeschrock return (ZPOOL_STATUS_VERSION_OLDER); 269fa9e4066Sahrens 270fa9e4066Sahrens return (ZPOOL_STATUS_OK); 271fa9e4066Sahrens } 272fa9e4066Sahrens 273fa9e4066Sahrens zpool_status_t 274fa9e4066Sahrens zpool_get_status(zpool_handle_t *zhp, char **msgid) 275fa9e4066Sahrens { 27699653d4eSeschrock zpool_status_t ret = check_status(zhp->zpool_config, B_FALSE); 277fa9e4066Sahrens 278fa9e4066Sahrens if (ret >= NMSGID) 279fa9e4066Sahrens *msgid = NULL; 280fa9e4066Sahrens else 281*3d7072f8Seschrock *msgid = zfs_msgid_table[ret]; 282fa9e4066Sahrens 283fa9e4066Sahrens return (ret); 284fa9e4066Sahrens } 285fa9e4066Sahrens 286fa9e4066Sahrens zpool_status_t 287fa9e4066Sahrens zpool_import_status(nvlist_t *config, char **msgid) 288fa9e4066Sahrens { 28999653d4eSeschrock zpool_status_t ret = check_status(config, B_TRUE); 290fa9e4066Sahrens 291fa9e4066Sahrens if (ret >= NMSGID) 292fa9e4066Sahrens *msgid = NULL; 293fa9e4066Sahrens else 29495173954Sek *msgid = zfs_msgid_table[ret]; 295fa9e4066Sahrens 296fa9e4066Sahrens return (ret); 297fa9e4066Sahrens } 298