]> granicus.if.org Git - zfs/blob - lib/libzfs/libzfs_status.c
pyzfs: python3 support (library 2/2)
[zfs] / lib / libzfs / libzfs_status.c
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
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.
26  */
27
28 /*
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.
37  *
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
41  * import.
42  */
43
44 #include <libzfs.h>
45 #include <libzutil.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/systeminfo.h>
49 #include "libzfs_impl.h"
50 #include "zfeature_common.h"
51
52 /*
53  * Message ID table.  This must be kept in sync with the ZPOOL_STATUS_* defines
54  * in include/libzfs.h.  Note that there are some status results which go past
55  * the end of this table, and hence have no associated message ID.
56  */
57 static char *zfs_msgid_table[] = {
58         "ZFS-8000-14", /* ZPOOL_STATUS_CORRUPT_CACHE */
59         "ZFS-8000-2Q", /* ZPOOL_STATUS_MISSING_DEV_R */
60         "ZFS-8000-3C", /* ZPOOL_STATUS_MISSING_DEV_NR */
61         "ZFS-8000-4J", /* ZPOOL_STATUS_CORRUPT_LABEL_R */
62         "ZFS-8000-5E", /* ZPOOL_STATUS_CORRUPT_LABEL_NR */
63         "ZFS-8000-6X", /* ZPOOL_STATUS_BAD_GUID_SUM */
64         "ZFS-8000-72", /* ZPOOL_STATUS_CORRUPT_POOL */
65         "ZFS-8000-8A", /* ZPOOL_STATUS_CORRUPT_DATA */
66         "ZFS-8000-9P", /* ZPOOL_STATUS_FAILING_DEV */
67         "ZFS-8000-A5", /* ZPOOL_STATUS_VERSION_NEWER */
68         "ZFS-8000-EY", /* ZPOOL_STATUS_HOSTID_MISMATCH */
69         "ZFS-8000-EY", /* ZPOOL_STATUS_HOSTID_ACTIVE */
70         "ZFS-8000-EY", /* ZPOOL_STATUS_HOSTID_REQUIRED */
71         "ZFS-8000-HC", /* ZPOOL_STATUS_IO_FAILURE_WAIT */
72         "ZFS-8000-JQ", /* ZPOOL_STATUS_IO_FAILURE_CONTINUE */
73         "ZFS-8000-MM", /* ZPOOL_STATUS_IO_FAILURE_MMP */
74         "ZFS-8000-K4", /* ZPOOL_STATUS_BAD_LOG */
75         "ZFS-8000-ER", /* ZPOOL_STATUS_ERRATA */
76         /*
77          * The following results have no message ID.
78          *      ZPOOL_STATUS_UNSUP_FEAT_READ
79          *      ZPOOL_STATUS_UNSUP_FEAT_WRITE
80          *      ZPOOL_STATUS_FAULTED_DEV_R
81          *      ZPOOL_STATUS_FAULTED_DEV_NR
82          *      ZPOOL_STATUS_VERSION_OLDER
83          *      ZPOOL_STATUS_FEAT_DISABLED
84          *      ZPOOL_STATUS_RESILVERING
85          *      ZPOOL_STATUS_OFFLINE_DEV
86          *      ZPOOL_STATUS_REMOVED_DEV
87          *      ZPOOL_STATUS_OK
88          */
89 };
90
91 #define NMSGID  (sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
92
93 /* ARGSUSED */
94 static int
95 vdev_missing(uint64_t state, uint64_t aux, uint64_t errs)
96 {
97         return (state == VDEV_STATE_CANT_OPEN &&
98             aux == VDEV_AUX_OPEN_FAILED);
99 }
100
101 /* ARGSUSED */
102 static int
103 vdev_faulted(uint64_t state, uint64_t aux, uint64_t errs)
104 {
105         return (state == VDEV_STATE_FAULTED);
106 }
107
108 /* ARGSUSED */
109 static int
110 vdev_errors(uint64_t state, uint64_t aux, uint64_t errs)
111 {
112         return (state == VDEV_STATE_DEGRADED || errs != 0);
113 }
114
115 /* ARGSUSED */
116 static int
117 vdev_broken(uint64_t state, uint64_t aux, uint64_t errs)
118 {
119         return (state == VDEV_STATE_CANT_OPEN);
120 }
121
122 /* ARGSUSED */
123 static int
124 vdev_offlined(uint64_t state, uint64_t aux, uint64_t errs)
125 {
126         return (state == VDEV_STATE_OFFLINE);
127 }
128
129 /* ARGSUSED */
130 static int
131 vdev_removed(uint64_t state, uint64_t aux, uint64_t errs)
132 {
133         return (state == VDEV_STATE_REMOVED);
134 }
135
136 /*
137  * Detect if any leaf devices that have seen errors or could not be opened.
138  */
139 static boolean_t
140 find_vdev_problem(nvlist_t *vdev, int (*func)(uint64_t, uint64_t, uint64_t))
141 {
142         nvlist_t **child;
143         vdev_stat_t *vs;
144         uint_t c, children;
145         char *type;
146
147         /*
148          * Ignore problems within a 'replacing' vdev, since we're presumably in
149          * the process of repairing any such errors, and don't want to call them
150          * out again.  We'll pick up the fact that a resilver is happening
151          * later.
152          */
153         verify(nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type) == 0);
154         if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
155                 return (B_FALSE);
156
157         if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child,
158             &children) == 0) {
159                 for (c = 0; c < children; c++)
160                         if (find_vdev_problem(child[c], func))
161                                 return (B_TRUE);
162         } else {
163                 verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_VDEV_STATS,
164                     (uint64_t **)&vs, &c) == 0);
165
166                 if (func(vs->vs_state, vs->vs_aux,
167                     vs->vs_read_errors +
168                     vs->vs_write_errors +
169                     vs->vs_checksum_errors))
170                         return (B_TRUE);
171         }
172
173         /*
174          * Check any L2 cache devs
175          */
176         if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_L2CACHE, &child,
177             &children) == 0) {
178                 for (c = 0; c < children; c++)
179                         if (find_vdev_problem(child[c], func))
180                                 return (B_TRUE);
181         }
182
183         return (B_FALSE);
184 }
185
186 /*
187  * Active pool health status.
188  *
189  * To determine the status for a pool, we make several passes over the config,
190  * picking the most egregious error we find.  In order of importance, we do the
191  * following:
192  *
193  *      - Check for a complete and valid configuration
194  *      - Look for any faulted or missing devices in a non-replicated config
195  *      - Check for any data errors
196  *      - Check for any faulted or missing devices in a replicated config
197  *      - Look for any devices showing errors
198  *      - Check for any resilvering devices
199  *
200  * There can obviously be multiple errors within a single pool, so this routine
201  * only picks the most damaging of all the current errors to report.
202  */
203 static zpool_status_t
204 check_status(nvlist_t *config, boolean_t isimport, zpool_errata_t *erratap)
205 {
206         nvlist_t *nvroot;
207         vdev_stat_t *vs;
208         pool_scan_stat_t *ps = NULL;
209         uint_t vsc, psc;
210         uint64_t nerr;
211         uint64_t version;
212         uint64_t stateval;
213         uint64_t suspended;
214         uint64_t hostid = 0;
215         uint64_t errata = 0;
216         unsigned long system_hostid = get_system_hostid();
217
218         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
219             &version) == 0);
220         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
221             &nvroot) == 0);
222         verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
223             (uint64_t **)&vs, &vsc) == 0);
224         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
225             &stateval) == 0);
226
227         /*
228          * Currently resilvering a vdev
229          */
230         (void) nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_SCAN_STATS,
231             (uint64_t **)&ps, &psc);
232         if (ps != NULL && ps->pss_func == POOL_SCAN_RESILVER &&
233             ps->pss_state == DSS_SCANNING)
234                 return (ZPOOL_STATUS_RESILVERING);
235
236         /*
237          * The multihost property is set and the pool may be active.
238          */
239         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
240             vs->vs_aux == VDEV_AUX_ACTIVE) {
241                 mmp_state_t mmp_state;
242                 nvlist_t *nvinfo;
243
244                 nvinfo = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO);
245                 mmp_state = fnvlist_lookup_uint64(nvinfo,
246                     ZPOOL_CONFIG_MMP_STATE);
247
248                 if (mmp_state == MMP_STATE_ACTIVE)
249                         return (ZPOOL_STATUS_HOSTID_ACTIVE);
250                 else if (mmp_state == MMP_STATE_NO_HOSTID)
251                         return (ZPOOL_STATUS_HOSTID_REQUIRED);
252                 else
253                         return (ZPOOL_STATUS_HOSTID_MISMATCH);
254         }
255
256         /*
257          * Pool last accessed by another system.
258          */
259         (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_HOSTID, &hostid);
260         if (hostid != 0 && (unsigned long)hostid != system_hostid &&
261             stateval == POOL_STATE_ACTIVE)
262                 return (ZPOOL_STATUS_HOSTID_MISMATCH);
263
264         /*
265          * Newer on-disk version.
266          */
267         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
268             vs->vs_aux == VDEV_AUX_VERSION_NEWER)
269                 return (ZPOOL_STATUS_VERSION_NEWER);
270
271         /*
272          * Unsupported feature(s).
273          */
274         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
275             vs->vs_aux == VDEV_AUX_UNSUP_FEAT) {
276                 nvlist_t *nvinfo;
277
278                 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
279                     &nvinfo) == 0);
280                 if (nvlist_exists(nvinfo, ZPOOL_CONFIG_CAN_RDONLY))
281                         return (ZPOOL_STATUS_UNSUP_FEAT_WRITE);
282                 return (ZPOOL_STATUS_UNSUP_FEAT_READ);
283         }
284
285         /*
286          * Check that the config is complete.
287          */
288         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
289             vs->vs_aux == VDEV_AUX_BAD_GUID_SUM)
290                 return (ZPOOL_STATUS_BAD_GUID_SUM);
291
292         /*
293          * Check whether the pool has suspended.
294          */
295         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED,
296             &suspended) == 0) {
297                 uint64_t reason;
298
299                 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED_REASON,
300                     &reason) == 0 && reason == ZIO_SUSPEND_MMP)
301                         return (ZPOOL_STATUS_IO_FAILURE_MMP);
302
303                 if (suspended == ZIO_FAILURE_MODE_CONTINUE)
304                         return (ZPOOL_STATUS_IO_FAILURE_CONTINUE);
305                 return (ZPOOL_STATUS_IO_FAILURE_WAIT);
306         }
307
308         /*
309          * Could not read a log.
310          */
311         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
312             vs->vs_aux == VDEV_AUX_BAD_LOG) {
313                 return (ZPOOL_STATUS_BAD_LOG);
314         }
315
316         /*
317          * Bad devices in non-replicated config.
318          */
319         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
320             find_vdev_problem(nvroot, vdev_faulted))
321                 return (ZPOOL_STATUS_FAULTED_DEV_NR);
322
323         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
324             find_vdev_problem(nvroot, vdev_missing))
325                 return (ZPOOL_STATUS_MISSING_DEV_NR);
326
327         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
328             find_vdev_problem(nvroot, vdev_broken))
329                 return (ZPOOL_STATUS_CORRUPT_LABEL_NR);
330
331         /*
332          * Corrupted pool metadata
333          */
334         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
335             vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
336                 return (ZPOOL_STATUS_CORRUPT_POOL);
337
338         /*
339          * Persistent data errors.
340          */
341         if (!isimport) {
342                 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
343                     &nerr) == 0 && nerr != 0)
344                         return (ZPOOL_STATUS_CORRUPT_DATA);
345         }
346
347         /*
348          * Missing devices in a replicated config.
349          */
350         if (find_vdev_problem(nvroot, vdev_faulted))
351                 return (ZPOOL_STATUS_FAULTED_DEV_R);
352         if (find_vdev_problem(nvroot, vdev_missing))
353                 return (ZPOOL_STATUS_MISSING_DEV_R);
354         if (find_vdev_problem(nvroot, vdev_broken))
355                 return (ZPOOL_STATUS_CORRUPT_LABEL_R);
356
357         /*
358          * Devices with errors
359          */
360         if (!isimport && find_vdev_problem(nvroot, vdev_errors))
361                 return (ZPOOL_STATUS_FAILING_DEV);
362
363         /*
364          * Offlined devices
365          */
366         if (find_vdev_problem(nvroot, vdev_offlined))
367                 return (ZPOOL_STATUS_OFFLINE_DEV);
368
369         /*
370          * Removed device
371          */
372         if (find_vdev_problem(nvroot, vdev_removed))
373                 return (ZPOOL_STATUS_REMOVED_DEV);
374
375         /*
376          * Informational errata available.
377          */
378         (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRATA, &errata);
379         if (errata) {
380                 *erratap = errata;
381                 return (ZPOOL_STATUS_ERRATA);
382         }
383
384         /*
385          * Outdated, but usable, version
386          */
387         if (SPA_VERSION_IS_SUPPORTED(version) && version != SPA_VERSION)
388                 return (ZPOOL_STATUS_VERSION_OLDER);
389
390         /*
391          * Usable pool with disabled features
392          */
393         if (version >= SPA_VERSION_FEATURES) {
394                 int i;
395                 nvlist_t *feat;
396
397                 if (isimport) {
398                         feat = fnvlist_lookup_nvlist(config,
399                             ZPOOL_CONFIG_LOAD_INFO);
400                         if (nvlist_exists(feat, ZPOOL_CONFIG_ENABLED_FEAT))
401                                 feat = fnvlist_lookup_nvlist(feat,
402                                     ZPOOL_CONFIG_ENABLED_FEAT);
403                 } else {
404                         feat = fnvlist_lookup_nvlist(config,
405                             ZPOOL_CONFIG_FEATURE_STATS);
406                 }
407
408                 for (i = 0; i < SPA_FEATURES; i++) {
409                         zfeature_info_t *fi = &spa_feature_table[i];
410                         if (!nvlist_exists(feat, fi->fi_guid))
411                                 return (ZPOOL_STATUS_FEAT_DISABLED);
412                 }
413         }
414
415         return (ZPOOL_STATUS_OK);
416 }
417
418 zpool_status_t
419 zpool_get_status(zpool_handle_t *zhp, char **msgid, zpool_errata_t *errata)
420 {
421         zpool_status_t ret = check_status(zhp->zpool_config, B_FALSE, errata);
422         if (msgid != NULL) {
423                 if (ret >= NMSGID)
424                         *msgid = NULL;
425                 else
426                         *msgid = zfs_msgid_table[ret];
427         }
428         return (ret);
429 }
430
431 zpool_status_t
432 zpool_import_status(nvlist_t *config, char **msgid, zpool_errata_t *errata)
433 {
434         zpool_status_t ret = check_status(config, B_TRUE, errata);
435
436         if (ret >= NMSGID)
437                 *msgid = NULL;
438         else
439                 *msgid = zfs_msgid_table[ret];
440
441         return (ret);
442 }