]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzpool/util.c
FreeBSD: Add zfs_link_create() error handling
[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
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
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>
27d96d22 36#include <sys/zfs_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) {
a926aab9 67 const char *suffix = "";
d1807f16 68 const char *bias = NULL;
cc99f275 69 char bias_suffix[32];
b128c09f 70
cc99f275
DB
71 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
72 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
73 &bias);
428870ff 74 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
b128c09f 75 (uint64_t **)&vs, &c) != 0)
193a37cb 76 vs = v0;
b128c09f 77
cc99f275
DB
78 if (bias != NULL) {
79 (void) snprintf(bias_suffix, sizeof (bias_suffix),
80 " (%s)", bias);
81 suffix = bias_suffix;
82 } else if (is_log) {
83 suffix = " (log)";
84 }
85
b128c09f
BB
86 sec = MAX(1, vs->vs_timestamp / NANOSEC);
87
f3c8c9e6
JK
88 nicenum(vs->vs_alloc, used, sizeof (used));
89 nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
90 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
91 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
92 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
93 sizeof (rbytes));
94 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
95 sizeof (wbytes));
96 nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
97 nicenum(vs->vs_write_errors, werr, sizeof (werr));
98 nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
b128c09f
BB
99
100 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
101 indent, "",
b128c09f 102 desc,
cc99f275
DB
103 (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),
104 suffix,
b128c09f
BB
105 vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
106 vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
107 rops, wops, rbytes, wbytes, rerr, werr, cerr);
108 }
790c880e 109 umem_free(v0, sizeof (*v0));
b128c09f
BB
110
111 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
34dc7c2f
BB
112 return;
113
114 for (c = 0; c < children; c++) {
115 nvlist_t *cnv = child[c];
d1807f16
RY
116 const char *cname = NULL;
117 char *tname;
34dc7c2f 118 uint64_t np;
ccc92611 119 int len;
34dc7c2f
BB
120 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
121 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
d1807f16 122 cname = "<unknown>";
ccc92611 123 len = strlen(cname) + 2;
124 tname = umem_zalloc(len, UMEM_NOFAIL);
125 (void) strlcpy(tname, cname, len);
34dc7c2f
BB
126 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
127 tname[strlen(tname)] = '0' + np;
b128c09f 128 show_vdev_stats(tname, ctype, cnv, indent + 2);
790c880e 129 umem_free(tname, len);
34dc7c2f
BB
130 }
131}
132
133void
134show_pool_stats(spa_t *spa)
135{
136 nvlist_t *config, *nvroot;
d1807f16 137 const char *name;
34dc7c2f 138
b128c09f 139 VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
34dc7c2f
BB
140
141 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
142 &nvroot) == 0);
143 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
144 &name) == 0);
145
b128c09f
BB
146 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
147 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
148 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
149
150 nvlist_free(config);
34dc7c2f 151}
ed828c0c 152
edc508ac
CS
153/* *k_out must be freed by the caller */
154static int
155set_global_var_parse_kv(const char *arg, char **k_out, u_longlong_t *v_out)
156{
157 int err;
158 VERIFY(arg);
159 char *d = strdup(arg);
160
161 char *save = NULL;
162 char *k = strtok_r(d, "=", &save);
163 char *v_str = strtok_r(NULL, "=", &save);
164 char *follow = strtok_r(NULL, "=", &save);
165 if (k == NULL || v_str == NULL || follow != NULL) {
166 err = EINVAL;
167 goto err_free;
168 }
169
170 u_longlong_t val = strtoull(v_str, NULL, 0);
171 if (val > UINT32_MAX) {
172 fprintf(stderr, "Value for global variable '%s' must "
173 "be a 32-bit unsigned integer, got '%s'\n", k, v_str);
174 err = EOVERFLOW;
175 goto err_free;
176 }
177
d25153d5 178 *k_out = strdup(k);
edc508ac 179 *v_out = val;
d25153d5 180 free(d);
edc508ac
CS
181 return (0);
182
183err_free:
d25153d5 184 free(d);
edc508ac
CS
185
186 return (err);
187}
188
ed828c0c
GM
189/*
190 * Sets given global variable in libzpool to given unsigned 32-bit value.
191 * arg: "<variable>=<value>"
192 */
193int
edc508ac 194set_global_var(char const *arg)
ed828c0c
GM
195{
196 void *zpoolhdl;
edc508ac 197 char *varname;
ed828c0c 198 u_longlong_t val;
edc508ac 199 int ret;
ed828c0c 200
b5fffa1d 201#ifndef _ZFS_LITTLE_ENDIAN
ed828c0c
GM
202 /*
203 * On big endian systems changing a 64-bit variable would set the high
204 * 32 bits instead of the low 32 bits, which could cause unexpected
205 * results.
206 */
207 fprintf(stderr, "Setting global variables is only supported on "
208 "little-endian systems\n");
edc508ac
CS
209 ret = ENOTSUP;
210 goto out_ret;
ed828c0c 211#endif
edc508ac
CS
212
213 if ((ret = set_global_var_parse_kv(arg, &varname, &val)) != 0) {
214 goto out_ret;
ed828c0c
GM
215 }
216
217 zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
218 if (zpoolhdl != NULL) {
219 uint32_t *var;
220 var = dlsym(zpoolhdl, varname);
221 if (var == NULL) {
222 fprintf(stderr, "Global variable '%s' does not exist "
223 "in libzpool.so\n", varname);
edc508ac
CS
224 ret = EINVAL;
225 goto out_dlclose;
ed828c0c
GM
226 }
227 *var = (uint32_t)val;
228
ed828c0c
GM
229 } else {
230 fprintf(stderr, "Failed to open libzpool.so to set global "
231 "variable\n");
edc508ac 232 ret = EIO;
c6b161e3 233 goto out_free;
ed828c0c
GM
234 }
235
edc508ac
CS
236 ret = 0;
237
238out_dlclose:
239 dlclose(zpoolhdl);
c6b161e3 240out_free:
edc508ac
CS
241 free(varname);
242out_ret:
243 return (ret);
ed828c0c 244}
e89f1295
DB
245
246static nvlist_t *
247refresh_config(void *unused, nvlist_t *tryconfig)
248{
56050599 249 (void) unused;
e89f1295
DB
250 return (spa_tryimport(tryconfig));
251}
252
514498fe
BB
253#if defined(__FreeBSD__)
254
255#include <sys/param.h>
256#include <sys/sysctl.h>
257#include <os/freebsd/zfs/sys/zfs_ioctl_compat.h>
258
e89f1295 259static int
514498fe 260pool_active(void *unused, const char *name, uint64_t guid, boolean_t *isactive)
e89f1295 261{
56050599 262 (void) unused, (void) guid;
514498fe
BB
263 zfs_iocparm_t zp;
264 zfs_cmd_t *zc = NULL;
20b867f5 265#ifdef ZFS_LEGACY_SUPPORT
514498fe 266 zfs_cmd_legacy_t *zcl = NULL;
20b867f5 267#endif
514498fe
BB
268 unsigned long request;
269 int ret;
270
271 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
272 if (fd < 0)
273 return (-1);
e89f1295
DB
274
275 /*
514498fe
BB
276 * Use ZFS_IOC_POOL_STATS to check if the pool is active. We want to
277 * avoid adding a dependency on libzfs_core solely for this ioctl(),
278 * therefore we manually craft the stats command. Note that the command
279 * ID is identical between the openzfs and legacy ioctl() formats.
e89f1295 280 */
514498fe
BB
281 int ver = ZFS_IOCVER_NONE;
282 size_t ver_size = sizeof (ver);
e89f1295 283
514498fe
BB
284 sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0);
285
286 switch (ver) {
287 case ZFS_IOCVER_OZFS:
288 zc = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);
289
290 (void) strlcpy(zc->zc_name, name, sizeof (zc->zc_name));
291 zp.zfs_cmd = (uint64_t)(uintptr_t)zc;
292 zp.zfs_cmd_size = sizeof (zfs_cmd_t);
293 zp.zfs_ioctl_version = ZFS_IOCVER_OZFS;
294
295 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t);
296 ret = ioctl(fd, request, &zp);
297
298 free((void *)(uintptr_t)zc->zc_nvlist_dst);
299 umem_free(zc, sizeof (zfs_cmd_t));
300
301 break;
20b867f5 302#ifdef ZFS_LEGACY_SUPPORT
514498fe
BB
303 case ZFS_IOCVER_LEGACY:
304 zcl = umem_zalloc(sizeof (zfs_cmd_legacy_t), UMEM_NOFAIL);
305
306 (void) strlcpy(zcl->zc_name, name, sizeof (zcl->zc_name));
307 zp.zfs_cmd = (uint64_t)(uintptr_t)zcl;
308 zp.zfs_cmd_size = sizeof (zfs_cmd_legacy_t);
309 zp.zfs_ioctl_version = ZFS_IOCVER_LEGACY;
310
311 request = _IOWR('Z', ZFS_IOC_POOL_STATS, zfs_iocparm_t);
312 ret = ioctl(fd, request, &zp);
e89f1295 313
514498fe
BB
314 free((void *)(uintptr_t)zcl->zc_nvlist_dst);
315 umem_free(zcl, sizeof (zfs_cmd_legacy_t));
e89f1295 316
514498fe 317 break;
20b867f5 318#endif
514498fe
BB
319 default:
320 fprintf(stderr, "unrecognized zfs ioctl version %d", ver);
321 exit(1);
322 }
323
324 (void) close(fd);
325
326 *isactive = (ret == 0);
e89f1295 327
514498fe
BB
328 return (0);
329}
330#else
331static int
332pool_active(void *unused, const char *name, uint64_t guid,
333 boolean_t *isactive)
334{
56050599 335 (void) unused, (void) guid;
514498fe
BB
336 int fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
337 if (fd < 0)
338 return (-1);
339
340 /*
341 * Use ZFS_IOC_POOL_STATS to check if a pool is active.
342 */
343 zfs_cmd_t *zcp = umem_zalloc(sizeof (zfs_cmd_t), UMEM_NOFAIL);
e89f1295 344 (void) strlcpy(zcp->zc_name, name, sizeof (zcp->zc_name));
e89f1295 345
514498fe 346 int ret = ioctl(fd, ZFS_IOC_POOL_STATS, zcp);
e89f1295 347
e89f1295 348 free((void *)(uintptr_t)zcp->zc_nvlist_dst);
e89f1295
DB
349 umem_free(zcp, sizeof (zfs_cmd_t));
350
351 (void) close(fd);
352
353 *isactive = (ret == 0);
354
355 return (0);
356}
514498fe 357#endif
e89f1295 358
a2d5643f 359pool_config_ops_t libzpool_config_ops = {
e89f1295
DB
360 .pco_refresh_config = refresh_config,
361 .pco_pool_active = pool_active,
362};