]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzpool/util.c
Fix zdb crash
[mirror_zfs.git] / lib / libzpool / util.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
ed828c0c 23 * Copyright (c) 2016 by Delphix. All rights reserved.
f3c8c9e6 24 * Copyright 2017 Jason King
cc99f275 25 * Copyright (c) 2017, Intel Corporation.
34dc7c2f
BB
26 */
27
34dc7c2f
BB
28#include <assert.h>
29#include <sys/zfs_context.h>
30#include <sys/avl.h>
31#include <string.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <sys/spa.h>
35#include <sys/fs/zfs.h>
36#include <sys/refcount.h>
e89f1295 37#include <sys/zfs_ioctl.h>
ed828c0c 38#include <dlfcn.h>
e89f1295 39#include <libzutil.h>
34dc7c2f
BB
40
41/*
42 * Routines needed by more than one client of libzpool.
43 */
44
34dc7c2f 45static void
b128c09f 46show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
34dc7c2f 47{
34dc7c2f 48 vdev_stat_t *vs;
193a37cb 49 vdev_stat_t *v0 = { 0 };
34dc7c2f
BB
50 uint64_t sec;
51 uint64_t is_log = 0;
b128c09f
BB
52 nvlist_t **child;
53 uint_t c, children;
34dc7c2f
BB
54 char used[6], avail[6];
55 char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
34dc7c2f 56
193a37cb
TH
57 v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL);
58
b128c09f
BB
59 if (indent == 0 && desc != NULL) {
60 (void) printf(" "
34dc7c2f 61 " capacity operations bandwidth ---- errors ----\n");
b128c09f 62 (void) printf("description "
34dc7c2f
BB
63 "used avail read write read write read write cksum\n");
64 }
65
b128c09f 66 if (desc != NULL) {
cc99f275
DB
67 char *suffix = "", *bias = NULL;
68 char bias_suffix[32];
b128c09f 69
cc99f275
DB
70 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
71 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
72 &bias);
428870ff 73 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
b128c09f 74 (uint64_t **)&vs, &c) != 0)
193a37cb 75 vs = v0;
b128c09f 76
cc99f275
DB
77 if (bias != NULL) {
78 (void) snprintf(bias_suffix, sizeof (bias_suffix),
79 " (%s)", bias);
80 suffix = bias_suffix;
81 } else if (is_log) {
82 suffix = " (log)";
83 }
84
b128c09f
BB
85 sec = MAX(1, vs->vs_timestamp / NANOSEC);
86
f3c8c9e6
JK
87 nicenum(vs->vs_alloc, used, sizeof (used));
88 nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
89 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
90 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
91 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
92 sizeof (rbytes));
93 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
94 sizeof (wbytes));
95 nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
96 nicenum(vs->vs_write_errors, werr, sizeof (werr));
97 nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
b128c09f
BB
98
99 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
100 indent, "",
b128c09f 101 desc,
cc99f275
DB
102 (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),
103 suffix,
b128c09f
BB
104 vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
105 vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
106 rops, wops, rbytes, wbytes, rerr, werr, cerr);
107 }
790c880e 108 umem_free(v0, sizeof (*v0));
b128c09f
BB
109
110 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
34dc7c2f
BB
111 return;
112
113 for (c = 0; c < children; c++) {
114 nvlist_t *cnv = child[c];
ccc92611 115 char *cname = NULL, *tname;
34dc7c2f 116 uint64_t np;
ccc92611 117 int len;
34dc7c2f
BB
118 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
119 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
120 cname = "<unknown>";
ccc92611 121 len = strlen(cname) + 2;
122 tname = umem_zalloc(len, UMEM_NOFAIL);
123 (void) strlcpy(tname, cname, len);
34dc7c2f
BB
124 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
125 tname[strlen(tname)] = '0' + np;
b128c09f 126 show_vdev_stats(tname, ctype, cnv, indent + 2);
790c880e 127 umem_free(tname, len);
34dc7c2f
BB
128 }
129}
130
131void
132show_pool_stats(spa_t *spa)
133{
134 nvlist_t *config, *nvroot;
135 char *name;
136
b128c09f 137 VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
34dc7c2f
BB
138
139 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
140 &nvroot) == 0);
141 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
142 &name) == 0);
143
b128c09f
BB
144 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
145 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
146 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
147
148 nvlist_free(config);
34dc7c2f 149}
ed828c0c
GM
150
151/*
152 * Sets given global variable in libzpool to given unsigned 32-bit value.
153 * arg: "<variable>=<value>"
154 */
155int
156set_global_var(char *arg)
157{
158 void *zpoolhdl;
159 char *varname = arg, *varval;
160 u_longlong_t val;
161
162#ifndef _LITTLE_ENDIAN
163 /*
164 * On big endian systems changing a 64-bit variable would set the high
165 * 32 bits instead of the low 32 bits, which could cause unexpected
166 * results.
167 */
168 fprintf(stderr, "Setting global variables is only supported on "
169 "little-endian systems\n");
170 return (ENOTSUP);
171#endif
7b0dc2a3 172 if (arg != NULL && (varval = strchr(arg, '=')) != NULL) {
ed828c0c
GM
173 *varval = '\0';
174 varval++;
175 val = strtoull(varval, NULL, 0);
176 if (val > UINT32_MAX) {
177 fprintf(stderr, "Value for global variable '%s' must "
178 "be a 32-bit unsigned integer\n", varname);
179 return (EOVERFLOW);
180 }
181 } else {
182 return (EINVAL);
183 }
184
185 zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
186 if (zpoolhdl != NULL) {
187 uint32_t *var;
188 var = dlsym(zpoolhdl, varname);
189 if (var == NULL) {
190 fprintf(stderr, "Global variable '%s' does not exist "
191 "in libzpool.so\n", varname);
192 return (EINVAL);
193 }
194 *var = (uint32_t)val;
195
196 dlclose(zpoolhdl);
197 } else {
198 fprintf(stderr, "Failed to open libzpool.so to set global "
199 "variable\n");
200 return (EIO);
201 }
202
203 return (0);
204}
e89f1295
DB
205
206static nvlist_t *
207refresh_config(void *unused, nvlist_t *tryconfig)
208{
209 return (spa_tryimport(tryconfig));
210}
211
212static int
213pool_active(void *unused, const char *name, uint64_t guid,
214 boolean_t *isactive)
215{
216 zfs_cmd_t *zcp;
217 nvlist_t *innvl;
218 char *packed = NULL;
219 size_t size = 0;
220 int fd, ret;
221
222 /*
223 * Use ZFS_IOC_POOL_SYNC to confirm if a pool is active
224 */
225
226 fd = open("/dev/zfs", O_RDWR);
227 if (fd < 0)
228 return (-1);
229
230 zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);
231
232 innvl = fnvlist_alloc();
233 fnvlist_add_boolean_value(innvl, "force", B_FALSE);
234
235 (void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name));
236 packed = fnvlist_pack(innvl, &size);
237 zcp->zc_nvlist_src = (uint64_t)(uintptr_t)packed;
238 zcp->zc_nvlist_src_size = size;
239
240 ret = ioctl(fd, ZFS_IOC_POOL_SYNC, zcp);
241
242 fnvlist_pack_free(packed, size);
243 free((void *)(uintptr_t)zcp->zc_nvlist_dst);
244 nvlist_free(innvl);
245 umem_free(zcp, sizeof (zfs_cmd_t));
246
247 (void) close(fd);
248
249 *isactive = (ret == 0);
250
251 return (0);
252}
253
254const pool_config_ops_t libzpool_config_ops = {
255 .pco_refresh_config = refresh_config,
256 .pco_pool_active = pool_active,
257};