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