1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14 * Copyright (c) 2016 by Delphix. All rights reserved.
15 * Copyright 2017 Jason King
16 * Copyright (c) 2017, Intel Corporation.
17 */
18
19 #include <assert.h>
20 #include <sys/zfs_context.h>
21 #include <sys/avl.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/spa.h>
26 #include <sys/fs/zfs.h>
27 #include <sys/zfs_refcount.h>
28 #include <sys/zfs_ioctl.h>
29 #include <sys/tunables.h>
30 #include <libzutil.h>
31 #include <libzpool.h>
32
33 /*
34 * Routines needed by more than one client of libzpool.
35 */
36
37 static void
show_vdev_stats(const char * desc,const char * ctype,nvlist_t * nv,int indent)38 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
39 {
40 vdev_stat_t *vs;
41 vdev_stat_t *v0 = { 0 };
42 uint64_t sec;
43 uint64_t is_log = 0;
44 nvlist_t **child;
45 uint_t c, children;
46 char used[6], avail[6];
47 char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
48
49 v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL);
50
51 if (indent == 0 && desc != NULL) {
52 (void) printf(" "
53 " capacity operations bandwidth ---- errors ----\n");
54 (void) printf("description "
55 "used avail read write read write read write cksum\n");
56 }
57
58 if (desc != NULL) {
59 const char *suffix = "";
60 const char *bias = NULL;
61 char bias_suffix[32];
62
63 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
64 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
65 &bias);
66 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
67 (uint64_t **)&vs, &c) != 0)
68 vs = v0;
69
70 if (bias != NULL) {
71 (void) snprintf(bias_suffix, sizeof (bias_suffix),
72 " (%s)", bias);
73 suffix = bias_suffix;
74 } else if (is_log) {
75 suffix = " (log)";
76 }
77
78 sec = MAX(1, vs->vs_timestamp / NANOSEC);
79
80 nicenum(vs->vs_alloc, used, sizeof (used));
81 nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
82 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
83 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
84 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
85 sizeof (rbytes));
86 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
87 sizeof (wbytes));
88 nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
89 nicenum(vs->vs_write_errors, werr, sizeof (werr));
90 nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
91
92 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
93 indent, "",
94 desc,
95 (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),
96 suffix,
97 vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
98 vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
99 rops, wops, rbytes, wbytes, rerr, werr, cerr);
100 }
101 umem_free(v0, sizeof (*v0));
102
103 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
104 return;
105
106 for (c = 0; c < children; c++) {
107 nvlist_t *cnv = child[c];
108 const char *cname = NULL;
109 char *tname;
110 uint64_t np;
111 int len;
112 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
113 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
114 cname = "<unknown>";
115 len = strlen(cname) + 2;
116 tname = umem_zalloc(len, UMEM_NOFAIL);
117 (void) strlcpy(tname, cname, len);
118 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
119 tname[strlen(tname)] = '0' + np;
120 show_vdev_stats(tname, ctype, cnv, indent + 2);
121 umem_free(tname, len);
122 }
123 }
124
125 void
show_pool_stats(spa_t * spa)126 show_pool_stats(spa_t *spa)
127 {
128 nvlist_t *config, *nvroot;
129 const char *name;
130
131 VERIFY0(spa_get_stats(spa_name(spa), &config, NULL, 0));
132
133 VERIFY0(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot));
134 VERIFY0(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name));
135
136 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
137 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
138 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
139
140 nvlist_free(config);
141 }
142
143 /*
144 * Common helper for working with libzpool tunables from the command line.
145 *
146 * Valid inputs:
147 *
148 * <name> show named tunable and value
149 * <name>=<value> set tunable value
150 *
151 * show show all tunables and values
152 * show=<name> show named tunable and value
153 * info show info about all tunables
154 * info=<name> show info about named tunable
155 */
156
157 typedef enum { SHOW, INFO, SET } tunable_mode_t;
158
159 static int
list_tunables_cb(const zfs_tunable_t * tunable,void * arg)160 list_tunables_cb(const zfs_tunable_t *tunable, void *arg)
161 {
162 const tunable_mode_t *mode = arg;
163
164 static const char *type[] = {
165 "int", "uint", "ulong", "u64", "str",
166 };
167 static const char *perm[] = {
168 "rw", "rd",
169 };
170
171 if (*mode == SHOW) {
172 char val[64];
173 int err = zfs_tunable_get(tunable, val, sizeof (val));
174 if (err == 0)
175 printf("%s: %s\n", tunable->zt_name, val);
176 else
177 printf("%s: [error getting tunable value: %s]\n",
178 tunable->zt_name, strerror(err));
179 } else {
180 printf("%s [%s %s]: %s\n", tunable->zt_name,
181 type[tunable->zt_type], perm[tunable->zt_perm],
182 tunable->zt_desc);
183 }
184
185 return (0);
186 }
187 int
handle_tunable_option(const char * _arg,boolean_t quiet)188 handle_tunable_option(const char *_arg, boolean_t quiet)
189 {
190 int err = 0;
191 char *arg = strdup(_arg);
192 char *k, *v;
193
194 v = arg;
195 k = strsep(&v, "=");
196
197 tunable_mode_t mode;
198
199 if (strcmp(k, "show") == 0) {
200 mode = SHOW;
201 k = v;
202 } else if (strcmp(k, "info") == 0) {
203 mode = INFO;
204 k = v;
205 } else if (v == NULL) {
206 mode = SHOW;
207 } else {
208 mode = SET;
209 }
210
211 if (quiet && mode != SET) {
212 err = EINVAL;
213 goto out;
214 }
215
216 if (mode == SET) {
217 const zfs_tunable_t *tunable = zfs_tunable_lookup(k);
218 if (tunable == NULL) {
219 err = ENOENT;
220 goto out;
221 }
222
223 char vold[256], vnew[256];
224 if (zfs_tunable_get(tunable, vold, sizeof (vold)) != 0)
225 strcpy(vold, "???");
226 err = zfs_tunable_set(tunable, v);
227 if (err != 0)
228 goto out;
229 if (zfs_tunable_get(tunable, vnew, sizeof (vnew)) != 0)
230 strcpy(vnew, "???");
231
232 if (!quiet)
233 printf("%s: %s -> %s\n", k, vold, vnew);
234 } else if (k != NULL) {
235 const zfs_tunable_t *tunable = zfs_tunable_lookup(k);
236 if (tunable == NULL) {
237 err = ENOENT;
238 goto out;
239 }
240 list_tunables_cb(tunable, &mode);
241 } else {
242 zfs_tunable_iter(list_tunables_cb, &mode);
243 }
244
245 out:
246 if (!quiet) {
247 if (err == ENOENT)
248 fprintf(stderr, "no such tunable: %s\n", k);
249 else if (err != 0)
250 fprintf(stderr, "couldn't set tunable '%s': %s\n",
251 k, strerror(err));
252 }
253
254 free(arg);
255 return (err);
256 }
257
258 static nvlist_t *
refresh_config(void * unused,nvlist_t * tryconfig)259 refresh_config(void *unused, nvlist_t *tryconfig)
260 {
261 (void) unused;
262 return (spa_tryimport(tryconfig));
263 }
264
265 #if defined(__FreeBSD__)
266
267 #include <sys/param.h>
268 #include <sys/sysctl.h>
269 #include <os/freebsd/zfs/sys/zfs_ioctl_compat.h>
270
271 static int
pool_active(void * unused,const char * name,uint64_t guid,boolean_t * isactive)272 pool_active(void *unused, const char *name, uint64_t guid, boolean_t *isactive)
273 {
274 (void) unused, (void) guid;
275 zfs_iocparm_t zp;
276 zfs_cmd_t *zc = NULL;
277 unsigned long request;
278 int ret;
279
280 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
281 if (fd < 0)
282 return (-1);
283
284 /*
285 * Use ZFS_IOC_POOL_STATS to check if the pool is active. We want to
286 * avoid adding a dependency on libzfs_core solely for this ioctl(),
287 * therefore we manually craft the stats command. Note that the command
288 * ID is identical between the openzfs and legacy ioctl() formats.
289 */
290 zc = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);
291
292 (void) strlcpy(zc->zc_name, name, sizeof (zc->zc_name));
293 zp.zfs_cmd = (uint64_t)(uintptr_t)zc;
294 zp.zfs_cmd_size = sizeof (zfs_cmd_t);
295 zp.zfs_ioctl_version = ZFS_IOCVER_OZFS;
296
297 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t);
298 ret = ioctl(fd, request, &zp);
299
300 free((void *)(uintptr_t)zc->zc_nvlist_dst);
301 umem_free(zc, sizeof (zfs_cmd_t));
302
303 (void) close(fd);
304
305 *isactive = (ret == 0);
306
307 return (0);
308 }
309 #else
310 static int
pool_active(void * unused,const char * name,uint64_t guid,boolean_t * isactive)311 pool_active(void *unused, const char *name, uint64_t guid,
312 boolean_t *isactive)
313 {
314 (void) unused, (void) guid;
315 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
316 if (fd < 0)
317 return (-1);
318
319 /*
320 * Use ZFS_IOC_POOL_STATS to check if a pool is active.
321 */
322 zfs_cmd_t *zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);
323 (void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name));
324
325 int ret = ioctl(fd, ZFS_IOC_POOL_STATS, zcp);
326
327 free((void *)(uintptr_t)zcp->zc_nvlist_dst);
328 umem_free(zcp, sizeof (zfs_cmd_t));
329
330 (void) close(fd);
331
332 *isactive = (ret == 0);
333
334 return (0);
335 }
336 #endif
337
338 pool_config_ops_t libzpool_config_ops = {
339 .pco_refresh_config = refresh_config,
340 .pco_pool_active = pool_active,
341 };
342