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