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