Improve ZVOL queue behavior.
[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  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24
25 /*
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.
34  *
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
38  * import.
39  */
40
41 #include <libzfs.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include "libzfs_impl.h"
45
46 /*
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.
50  */
51 static char *zfs_msgid_table[] = {
52         "ZFS-8000-14",
53         "ZFS-8000-2Q",
54         "ZFS-8000-3C",
55         "ZFS-8000-4J",
56         "ZFS-8000-5E",
57         "ZFS-8000-6X",
58         "ZFS-8000-72",
59         "ZFS-8000-8A",
60         "ZFS-8000-9P",
61         "ZFS-8000-A5",
62         "ZFS-8000-EY",
63         "ZFS-8000-HC",
64         "ZFS-8000-JQ",
65         "ZFS-8000-K4",
66 };
67
68 #define NMSGID  (sizeof (zfs_msgid_table) / sizeof (zfs_msgid_table[0]))
69
70 /* ARGSUSED */
71 static int
72 vdev_missing(uint64_t state, uint64_t aux, uint64_t errs)
73 {
74         return (state == VDEV_STATE_CANT_OPEN &&
75             aux == VDEV_AUX_OPEN_FAILED);
76 }
77
78 /* ARGSUSED */
79 static int
80 vdev_faulted(uint64_t state, uint64_t aux, uint64_t errs)
81 {
82         return (state == VDEV_STATE_FAULTED);
83 }
84
85 /* ARGSUSED */
86 static int
87 vdev_errors(uint64_t state, uint64_t aux, uint64_t errs)
88 {
89         return (state == VDEV_STATE_DEGRADED || errs != 0);
90 }
91
92 /* ARGSUSED */
93 static int
94 vdev_broken(uint64_t state, uint64_t aux, uint64_t errs)
95 {
96         return (state == VDEV_STATE_CANT_OPEN);
97 }
98
99 /* ARGSUSED */
100 static int
101 vdev_offlined(uint64_t state, uint64_t aux, uint64_t errs)
102 {
103         return (state == VDEV_STATE_OFFLINE);
104 }
105
106 /* ARGSUSED */
107 static int
108 vdev_removed(uint64_t state, uint64_t aux, uint64_t errs)
109 {
110         return (state == VDEV_STATE_REMOVED);
111 }
112
113 /*
114  * Detect if any leaf devices that have seen errors or could not be opened.
115  */
116 static boolean_t
117 find_vdev_problem(nvlist_t *vdev, int (*func)(uint64_t, uint64_t, uint64_t))
118 {
119         nvlist_t **child;
120         vdev_stat_t *vs;
121         uint_t c, children;
122         char *type;
123
124         /*
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
128          * later.
129          */
130         verify(nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type) == 0);
131         if (strcmp(type, VDEV_TYPE_REPLACING) == 0)
132                 return (B_FALSE);
133
134         if (nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN, &child,
135             &children) == 0) {
136                 for (c = 0; c < children; c++)
137                         if (find_vdev_problem(child[c], func))
138                                 return (B_TRUE);
139         } else {
140                 verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_VDEV_STATS,
141                     (uint64_t **)&vs, &c) == 0);
142
143                 if (func(vs->vs_state, vs->vs_aux,
144                     vs->vs_read_errors +
145                     vs->vs_write_errors +
146                     vs->vs_checksum_errors))
147                         return (B_TRUE);
148         }
149
150         return (B_FALSE);
151 }
152
153 /*
154  * Active pool health status.
155  *
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
158  * following:
159  *
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
166  *
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.
169  */
170 static zpool_status_t
171 check_status(nvlist_t *config, boolean_t isimport)
172 {
173         nvlist_t *nvroot;
174         vdev_stat_t *vs;
175         pool_scan_stat_t *ps = NULL;
176         uint_t vsc, psc;
177         uint64_t nerr;
178         uint64_t version;
179         uint64_t stateval;
180         uint64_t suspended;
181         uint64_t hostid = 0;
182         unsigned long system_hostid = gethostid() & 0xffffffff;
183
184         verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
185             &version) == 0);
186         verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
187             &nvroot) == 0);
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,
191             &stateval) == 0);
192
193         /*
194          * Currently resilvering a vdev
195          */
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);
201
202         /*
203          * Pool last accessed by another system.
204          */
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);
209
210         /*
211          * Newer on-disk version.
212          */
213         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
214             vs->vs_aux == VDEV_AUX_VERSION_NEWER)
215                 return (ZPOOL_STATUS_VERSION_NEWER);
216
217         /*
218          * Check that the config is complete.
219          */
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);
223
224         /*
225          * Check whether the pool has suspended due to failed I/O.
226          */
227         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_SUSPENDED,
228             &suspended) == 0) {
229                 if (suspended == ZIO_FAILURE_MODE_CONTINUE)
230                         return (ZPOOL_STATUS_IO_FAILURE_CONTINUE);
231                 return (ZPOOL_STATUS_IO_FAILURE_WAIT);
232         }
233
234         /*
235          * Could not read a log.
236          */
237         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
238             vs->vs_aux == VDEV_AUX_BAD_LOG) {
239                 return (ZPOOL_STATUS_BAD_LOG);
240         }
241
242         /*
243          * Bad devices in non-replicated config.
244          */
245         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
246             find_vdev_problem(nvroot, vdev_faulted))
247                 return (ZPOOL_STATUS_FAULTED_DEV_NR);
248
249         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
250             find_vdev_problem(nvroot, vdev_missing))
251                 return (ZPOOL_STATUS_MISSING_DEV_NR);
252
253         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
254             find_vdev_problem(nvroot, vdev_broken))
255                 return (ZPOOL_STATUS_CORRUPT_LABEL_NR);
256
257         /*
258          * Corrupted pool metadata
259          */
260         if (vs->vs_state == VDEV_STATE_CANT_OPEN &&
261             vs->vs_aux == VDEV_AUX_CORRUPT_DATA)
262                 return (ZPOOL_STATUS_CORRUPT_POOL);
263
264         /*
265          * Persistent data errors.
266          */
267         if (!isimport) {
268                 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_ERRCOUNT,
269                     &nerr) == 0 && nerr != 0)
270                         return (ZPOOL_STATUS_CORRUPT_DATA);
271         }
272
273         /*
274          * Missing devices in a replicated config.
275          */
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);
282
283         /*
284          * Devices with errors
285          */
286         if (!isimport && find_vdev_problem(nvroot, vdev_errors))
287                 return (ZPOOL_STATUS_FAILING_DEV);
288
289         /*
290          * Offlined devices
291          */
292         if (find_vdev_problem(nvroot, vdev_offlined))
293                 return (ZPOOL_STATUS_OFFLINE_DEV);
294
295         /*
296          * Removed device
297          */
298         if (find_vdev_problem(nvroot, vdev_removed))
299                 return (ZPOOL_STATUS_REMOVED_DEV);
300
301         /*
302          * Outdated, but usable, version
303          */
304         if (version < SPA_VERSION)
305                 return (ZPOOL_STATUS_VERSION_OLDER);
306
307         return (ZPOOL_STATUS_OK);
308 }
309
310 zpool_status_t
311 zpool_get_status(zpool_handle_t *zhp, char **msgid)
312 {
313         zpool_status_t ret = check_status(zhp->zpool_config, B_FALSE);
314
315         if (ret >= NMSGID)
316                 *msgid = NULL;
317         else
318                 *msgid = zfs_msgid_table[ret];
319
320         return (ret);
321 }
322
323 zpool_status_t
324 zpool_import_status(nvlist_t *config, char **msgid)
325 {
326         zpool_status_t ret = check_status(config, B_TRUE);
327
328         if (ret >= NMSGID)
329                 *msgid = NULL;
330         else
331                 *msgid = zfs_msgid_table[ret];
332
333         return (ret);
334 }
335
336 static void
337 dump_ddt_stat(const ddt_stat_t *dds, int h)
338 {
339         char refcnt[6];
340         char blocks[6], lsize[6], psize[6], dsize[6];
341         char ref_blocks[6], ref_lsize[6], ref_psize[6], ref_dsize[6];
342
343         if (dds == NULL || dds->dds_blocks == 0)
344                 return;
345
346         if (h == -1)
347                 (void) strcpy(refcnt, "Total");
348         else
349                 zfs_nicenum(1ULL << h, refcnt, sizeof (refcnt));
350
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));
359
360         (void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
361             refcnt,
362             blocks, lsize, psize, dsize,
363             ref_blocks, ref_lsize, ref_psize, ref_dsize);
364 }
365
366 /*
367  * Print the DDT histogram and the column totals.
368  */
369 void
370 zpool_dump_ddt(const ddt_stat_t *dds_total, const ddt_histogram_t *ddh)
371 {
372         int h;
373
374         (void) printf("\n");
375
376         (void) printf("bucket   "
377             "           allocated             "
378             "          referenced          \n");
379         (void) printf("______   "
380             "______________________________   "
381             "______________________________\n");
382
383         (void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
384             "refcnt",
385             "blocks", "LSIZE", "PSIZE", "DSIZE",
386             "blocks", "LSIZE", "PSIZE", "DSIZE");
387
388         (void) printf("%6s   %6s   %5s   %5s   %5s   %6s   %5s   %5s   %5s\n",
389             "------",
390             "------", "-----", "-----", "-----",
391             "------", "-----", "-----", "-----");
392
393         for (h = 0; h < 64; h++)
394                 dump_ddt_stat(&ddh->ddh_stat[h], h);
395
396         dump_ddt_stat(dds_total, -1);
397
398         (void) printf("\n");
399 }