1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 25 * Copyright 2020 Joyent, Inc. All rights reserved. 26 * Copyright (c) 2011, 2024 by Delphix. All rights reserved. 27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 28 * Copyright (c) 2017 Datto Inc. 29 * Copyright (c) 2020 The FreeBSD Foundation 30 * 31 * Portions of this software were developed by Allan Jude 32 * under sponsorship from the FreeBSD Foundation. 33 */ 34 35 /* 36 * Internal utility routines for the ZFS library. 37 */ 38 39 #include <errno.h> 40 #include <fcntl.h> 41 #include <libintl.h> 42 #include <stdarg.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <strings.h> 46 #include <unistd.h> 47 #include <math.h> 48 #if LIBFETCH_DYNAMIC 49 #include <dlfcn.h> 50 #endif 51 #include <sys/stat.h> 52 #include <sys/mnttab.h> 53 #include <sys/mntent.h> 54 #include <sys/types.h> 55 #include <sys/wait.h> 56 57 #include <libzfs.h> 58 #include <libzfs_core.h> 59 60 #include "libzfs_impl.h" 61 #include "zfs_prop.h" 62 #include "zfeature_common.h" 63 #include <zfs_fletcher.h> 64 #include <libzutil.h> 65 66 /* 67 * We only care about the scheme in order to match the scheme 68 * with the handler. Each handler should validate the full URI 69 * as necessary. 70 */ 71 #define URI_REGEX "^\\([A-Za-z][A-Za-z0-9+.\\-]*\\):" 72 #define STR_NUMS "0123456789" 73 74 int 75 libzfs_errno(libzfs_handle_t *hdl) 76 { 77 return (hdl->libzfs_error); 78 } 79 80 const char * 81 libzfs_error_action(libzfs_handle_t *hdl) 82 { 83 return (hdl->libzfs_action); 84 } 85 86 const char * 87 libzfs_error_description(libzfs_handle_t *hdl) 88 { 89 if (hdl->libzfs_desc[0] != '\0') 90 return (hdl->libzfs_desc); 91 92 switch (hdl->libzfs_error) { 93 case EZFS_NOMEM: 94 return (dgettext(TEXT_DOMAIN, "out of memory")); 95 case EZFS_BADPROP: 96 return (dgettext(TEXT_DOMAIN, "invalid property value")); 97 case EZFS_PROPREADONLY: 98 return (dgettext(TEXT_DOMAIN, "read-only property")); 99 case EZFS_PROPTYPE: 100 return (dgettext(TEXT_DOMAIN, "property doesn't apply to " 101 "datasets of this type")); 102 case EZFS_PROPNONINHERIT: 103 return (dgettext(TEXT_DOMAIN, "property cannot be inherited")); 104 case EZFS_PROPSPACE: 105 return (dgettext(TEXT_DOMAIN, "invalid quota or reservation")); 106 case EZFS_BADTYPE: 107 return (dgettext(TEXT_DOMAIN, "operation not applicable to " 108 "datasets of this type")); 109 case EZFS_BUSY: 110 return (dgettext(TEXT_DOMAIN, "pool or dataset is busy")); 111 case EZFS_EXISTS: 112 return (dgettext(TEXT_DOMAIN, "pool or dataset exists")); 113 case EZFS_NOENT: 114 return (dgettext(TEXT_DOMAIN, "no such pool or dataset")); 115 case EZFS_BADSTREAM: 116 return (dgettext(TEXT_DOMAIN, "invalid backup stream")); 117 case EZFS_DSREADONLY: 118 return (dgettext(TEXT_DOMAIN, "dataset is read-only")); 119 case EZFS_VOLTOOBIG: 120 return (dgettext(TEXT_DOMAIN, "volume size exceeds limit for " 121 "this system")); 122 case EZFS_INVALIDNAME: 123 return (dgettext(TEXT_DOMAIN, "invalid name")); 124 case EZFS_BADRESTORE: 125 return (dgettext(TEXT_DOMAIN, "unable to restore to " 126 "destination")); 127 case EZFS_BADBACKUP: 128 return (dgettext(TEXT_DOMAIN, "backup failed")); 129 case EZFS_BADTARGET: 130 return (dgettext(TEXT_DOMAIN, "invalid target vdev")); 131 case EZFS_NODEVICE: 132 return (dgettext(TEXT_DOMAIN, "no such device in pool")); 133 case EZFS_BADDEV: 134 return (dgettext(TEXT_DOMAIN, "invalid device")); 135 case EZFS_NOREPLICAS: 136 return (dgettext(TEXT_DOMAIN, "no valid replicas")); 137 case EZFS_RESILVERING: 138 return (dgettext(TEXT_DOMAIN, "currently resilvering")); 139 case EZFS_BADVERSION: 140 return (dgettext(TEXT_DOMAIN, "unsupported version or " 141 "feature")); 142 case EZFS_POOLUNAVAIL: 143 return (dgettext(TEXT_DOMAIN, "pool is unavailable")); 144 case EZFS_DEVOVERFLOW: 145 return (dgettext(TEXT_DOMAIN, "too many devices in one vdev")); 146 case EZFS_BADPATH: 147 return (dgettext(TEXT_DOMAIN, "must be an absolute path")); 148 case EZFS_CROSSTARGET: 149 return (dgettext(TEXT_DOMAIN, "operation crosses datasets or " 150 "pools")); 151 case EZFS_ZONED: 152 return (dgettext(TEXT_DOMAIN, "dataset in use by local zone")); 153 case EZFS_MOUNTFAILED: 154 return (dgettext(TEXT_DOMAIN, "mount failed")); 155 case EZFS_UMOUNTFAILED: 156 return (dgettext(TEXT_DOMAIN, "unmount failed")); 157 case EZFS_UNSHARENFSFAILED: 158 return (dgettext(TEXT_DOMAIN, "NFS share removal failed")); 159 case EZFS_SHARENFSFAILED: 160 return (dgettext(TEXT_DOMAIN, "NFS share creation failed")); 161 case EZFS_UNSHARESMBFAILED: 162 return (dgettext(TEXT_DOMAIN, "SMB share removal failed")); 163 case EZFS_SHARESMBFAILED: 164 return (dgettext(TEXT_DOMAIN, "SMB share creation failed")); 165 case EZFS_PERM: 166 return (dgettext(TEXT_DOMAIN, "permission denied")); 167 case EZFS_NOSPC: 168 return (dgettext(TEXT_DOMAIN, "out of space")); 169 case EZFS_FAULT: 170 return (dgettext(TEXT_DOMAIN, "bad address")); 171 case EZFS_IO: 172 return (dgettext(TEXT_DOMAIN, "I/O error")); 173 case EZFS_INTR: 174 return (dgettext(TEXT_DOMAIN, "signal received")); 175 case EZFS_CKSUM: 176 return (dgettext(TEXT_DOMAIN, "insufficient replicas")); 177 case EZFS_ISSPARE: 178 return (dgettext(TEXT_DOMAIN, "device is reserved as a hot " 179 "spare")); 180 case EZFS_INVALCONFIG: 181 return (dgettext(TEXT_DOMAIN, "invalid vdev configuration")); 182 case EZFS_RECURSIVE: 183 return (dgettext(TEXT_DOMAIN, "recursive dataset dependency")); 184 case EZFS_NOHISTORY: 185 return (dgettext(TEXT_DOMAIN, "no history available")); 186 case EZFS_POOLPROPS: 187 return (dgettext(TEXT_DOMAIN, "failed to retrieve " 188 "pool properties")); 189 case EZFS_POOL_NOTSUP: 190 return (dgettext(TEXT_DOMAIN, "operation not supported " 191 "on this type of pool")); 192 case EZFS_POOL_INVALARG: 193 return (dgettext(TEXT_DOMAIN, "invalid argument for " 194 "this pool operation")); 195 case EZFS_NAMETOOLONG: 196 return (dgettext(TEXT_DOMAIN, "dataset name is too long")); 197 case EZFS_OPENFAILED: 198 return (dgettext(TEXT_DOMAIN, "open failed")); 199 case EZFS_NOCAP: 200 return (dgettext(TEXT_DOMAIN, 201 "disk capacity information could not be retrieved")); 202 case EZFS_LABELFAILED: 203 return (dgettext(TEXT_DOMAIN, "write of label failed")); 204 case EZFS_BADWHO: 205 return (dgettext(TEXT_DOMAIN, "invalid user/group")); 206 case EZFS_BADPERM: 207 return (dgettext(TEXT_DOMAIN, "invalid permission")); 208 case EZFS_BADPERMSET: 209 return (dgettext(TEXT_DOMAIN, "invalid permission set name")); 210 case EZFS_NODELEGATION: 211 return (dgettext(TEXT_DOMAIN, "delegated administration is " 212 "disabled on pool")); 213 case EZFS_BADCACHE: 214 return (dgettext(TEXT_DOMAIN, "invalid or missing cache file")); 215 case EZFS_ISL2CACHE: 216 return (dgettext(TEXT_DOMAIN, "device is in use as a cache")); 217 case EZFS_VDEVNOTSUP: 218 return (dgettext(TEXT_DOMAIN, "vdev specification is not " 219 "supported")); 220 case EZFS_NOTSUP: 221 return (dgettext(TEXT_DOMAIN, "operation not supported " 222 "on this dataset")); 223 case EZFS_IOC_NOTSUPPORTED: 224 return (dgettext(TEXT_DOMAIN, "operation not supported by " 225 "zfs kernel module")); 226 case EZFS_ACTIVE_SPARE: 227 return (dgettext(TEXT_DOMAIN, "pool has active shared spare " 228 "device")); 229 case EZFS_UNPLAYED_LOGS: 230 return (dgettext(TEXT_DOMAIN, "log device has unplayed intent " 231 "logs")); 232 case EZFS_REFTAG_RELE: 233 return (dgettext(TEXT_DOMAIN, "no such tag on this dataset")); 234 case EZFS_REFTAG_HOLD: 235 return (dgettext(TEXT_DOMAIN, "tag already exists on this " 236 "dataset")); 237 case EZFS_TAGTOOLONG: 238 return (dgettext(TEXT_DOMAIN, "tag too long")); 239 case EZFS_PIPEFAILED: 240 return (dgettext(TEXT_DOMAIN, "pipe create failed")); 241 case EZFS_THREADCREATEFAILED: 242 return (dgettext(TEXT_DOMAIN, "thread create failed")); 243 case EZFS_POSTSPLIT_ONLINE: 244 return (dgettext(TEXT_DOMAIN, "disk was split from this pool " 245 "into a new one")); 246 case EZFS_SCRUB_PAUSED: 247 return (dgettext(TEXT_DOMAIN, "scrub is paused; " 248 "use 'zpool scrub' to resume scrub")); 249 case EZFS_SCRUB_PAUSED_TO_CANCEL: 250 return (dgettext(TEXT_DOMAIN, "scrub is paused; " 251 "use 'zpool scrub' to resume or 'zpool scrub -s' to " 252 "cancel scrub")); 253 case EZFS_SCRUBBING: 254 return (dgettext(TEXT_DOMAIN, "currently scrubbing; " 255 "use 'zpool scrub -s' to cancel scrub")); 256 case EZFS_ERRORSCRUBBING: 257 return (dgettext(TEXT_DOMAIN, "currently error scrubbing; " 258 "use 'zpool scrub -s' to cancel error scrub")); 259 case EZFS_ERRORSCRUB_PAUSED: 260 return (dgettext(TEXT_DOMAIN, "error scrub is paused; " 261 "use 'zpool scrub -e' to resume error scrub")); 262 case EZFS_NO_SCRUB: 263 return (dgettext(TEXT_DOMAIN, "there is no active scrub")); 264 case EZFS_DIFF: 265 return (dgettext(TEXT_DOMAIN, "unable to generate diffs")); 266 case EZFS_DIFFDATA: 267 return (dgettext(TEXT_DOMAIN, "invalid diff data")); 268 case EZFS_POOLREADONLY: 269 return (dgettext(TEXT_DOMAIN, "pool is read-only")); 270 case EZFS_NO_PENDING: 271 return (dgettext(TEXT_DOMAIN, "operation is not " 272 "in progress")); 273 case EZFS_CHECKPOINT_EXISTS: 274 return (dgettext(TEXT_DOMAIN, "checkpoint exists")); 275 case EZFS_DISCARDING_CHECKPOINT: 276 return (dgettext(TEXT_DOMAIN, "currently discarding " 277 "checkpoint")); 278 case EZFS_NO_CHECKPOINT: 279 return (dgettext(TEXT_DOMAIN, "checkpoint does not exist")); 280 case EZFS_DEVRM_IN_PROGRESS: 281 return (dgettext(TEXT_DOMAIN, "device removal in progress")); 282 case EZFS_VDEV_TOO_BIG: 283 return (dgettext(TEXT_DOMAIN, "device exceeds supported size")); 284 case EZFS_ACTIVE_POOL: 285 return (dgettext(TEXT_DOMAIN, "pool is imported on a " 286 "different host")); 287 case EZFS_CRYPTOFAILED: 288 return (dgettext(TEXT_DOMAIN, "encryption failure")); 289 case EZFS_TOOMANY: 290 return (dgettext(TEXT_DOMAIN, "argument list too long")); 291 case EZFS_INITIALIZING: 292 return (dgettext(TEXT_DOMAIN, "currently initializing")); 293 case EZFS_NO_INITIALIZE: 294 return (dgettext(TEXT_DOMAIN, "there is no active " 295 "initialization")); 296 case EZFS_WRONG_PARENT: 297 return (dgettext(TEXT_DOMAIN, "invalid parent dataset")); 298 case EZFS_TRIMMING: 299 return (dgettext(TEXT_DOMAIN, "currently trimming")); 300 case EZFS_NO_TRIM: 301 return (dgettext(TEXT_DOMAIN, "there is no active trim")); 302 case EZFS_TRIM_NOTSUP: 303 return (dgettext(TEXT_DOMAIN, "trim operations are not " 304 "supported by this device")); 305 case EZFS_NO_RESILVER_DEFER: 306 return (dgettext(TEXT_DOMAIN, "this action requires the " 307 "resilver_defer feature")); 308 case EZFS_EXPORT_IN_PROGRESS: 309 return (dgettext(TEXT_DOMAIN, "pool export in progress")); 310 case EZFS_REBUILDING: 311 return (dgettext(TEXT_DOMAIN, "currently sequentially " 312 "resilvering")); 313 case EZFS_VDEV_NOTSUP: 314 return (dgettext(TEXT_DOMAIN, "operation not supported " 315 "on this type of vdev")); 316 case EZFS_NOT_USER_NAMESPACE: 317 return (dgettext(TEXT_DOMAIN, "the provided file " 318 "was not a user namespace file")); 319 case EZFS_RESUME_EXISTS: 320 return (dgettext(TEXT_DOMAIN, "Resuming recv on existing " 321 "dataset without force")); 322 case EZFS_RAIDZ_EXPAND_IN_PROGRESS: 323 return (dgettext(TEXT_DOMAIN, "raidz expansion in progress")); 324 case EZFS_ASHIFT_MISMATCH: 325 return (dgettext(TEXT_DOMAIN, "adding devices with " 326 "different physical sector sizes is not allowed")); 327 case EZFS_UNKNOWN: 328 return (dgettext(TEXT_DOMAIN, "unknown error")); 329 default: 330 assert(hdl->libzfs_error == 0); 331 return (dgettext(TEXT_DOMAIN, "no error")); 332 } 333 } 334 335 void 336 zfs_error_aux(libzfs_handle_t *hdl, const char *fmt, ...) 337 { 338 va_list ap; 339 340 va_start(ap, fmt); 341 342 (void) vsnprintf(hdl->libzfs_desc, sizeof (hdl->libzfs_desc), 343 fmt, ap); 344 hdl->libzfs_desc_active = 1; 345 346 va_end(ap); 347 } 348 349 static void 350 zfs_verror(libzfs_handle_t *hdl, int error, const char *fmt, va_list ap) 351 { 352 (void) vsnprintf(hdl->libzfs_action, sizeof (hdl->libzfs_action), 353 fmt, ap); 354 hdl->libzfs_error = error; 355 356 if (hdl->libzfs_desc_active) 357 hdl->libzfs_desc_active = 0; 358 else 359 hdl->libzfs_desc[0] = '\0'; 360 361 if (hdl->libzfs_printerr) { 362 if (error == EZFS_UNKNOWN) { 363 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "internal " 364 "error: %s: %s\n"), hdl->libzfs_action, 365 libzfs_error_description(hdl)); 366 abort(); 367 } 368 369 (void) fprintf(stderr, "%s: %s\n", hdl->libzfs_action, 370 libzfs_error_description(hdl)); 371 if (error == EZFS_NOMEM) 372 exit(1); 373 } 374 } 375 376 int 377 zfs_error(libzfs_handle_t *hdl, int error, const char *msg) 378 { 379 return (zfs_error_fmt(hdl, error, "%s", msg)); 380 } 381 382 int 383 zfs_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 384 { 385 va_list ap; 386 387 va_start(ap, fmt); 388 389 zfs_verror(hdl, error, fmt, ap); 390 391 va_end(ap); 392 393 return (-1); 394 } 395 396 static int 397 zfs_common_error(libzfs_handle_t *hdl, int error, const char *fmt, 398 va_list ap) 399 { 400 switch (error) { 401 case EPERM: 402 case EACCES: 403 zfs_verror(hdl, EZFS_PERM, fmt, ap); 404 return (-1); 405 406 case ECANCELED: 407 zfs_verror(hdl, EZFS_NODELEGATION, fmt, ap); 408 return (-1); 409 410 case EIO: 411 zfs_verror(hdl, EZFS_IO, fmt, ap); 412 return (-1); 413 414 case EFAULT: 415 zfs_verror(hdl, EZFS_FAULT, fmt, ap); 416 return (-1); 417 418 case EINTR: 419 zfs_verror(hdl, EZFS_INTR, fmt, ap); 420 return (-1); 421 422 case ECKSUM: 423 zfs_verror(hdl, EZFS_CKSUM, fmt, ap); 424 return (-1); 425 } 426 427 return (0); 428 } 429 430 int 431 zfs_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 432 { 433 return (zfs_standard_error_fmt(hdl, error, "%s", msg)); 434 } 435 436 int 437 zfs_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 438 { 439 va_list ap; 440 441 va_start(ap, fmt); 442 443 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 444 va_end(ap); 445 return (-1); 446 } 447 448 switch (error) { 449 case ENXIO: 450 case ENODEV: 451 case EPIPE: 452 zfs_verror(hdl, EZFS_IO, fmt, ap); 453 break; 454 455 case ENOENT: 456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 457 "dataset does not exist")); 458 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 459 break; 460 461 case ENOSPC: 462 case EDQUOT: 463 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 464 break; 465 466 case EEXIST: 467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 468 "dataset already exists")); 469 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 470 break; 471 472 case EBUSY: 473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 474 "dataset is busy")); 475 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 476 break; 477 case EROFS: 478 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 479 break; 480 case ENAMETOOLONG: 481 zfs_verror(hdl, EZFS_NAMETOOLONG, fmt, ap); 482 break; 483 case ENOTSUP: 484 zfs_verror(hdl, EZFS_BADVERSION, fmt, ap); 485 break; 486 case EAGAIN: 487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 488 "pool I/O is currently suspended")); 489 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 490 break; 491 case EREMOTEIO: 492 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); 493 break; 494 case ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE: 495 case ZFS_ERR_IOC_CMD_UNAVAIL: 496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 497 "module does not support this operation. A reboot may " 498 "be required to enable this operation.")); 499 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 500 break; 501 case ZFS_ERR_IOC_ARG_UNAVAIL: 502 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 503 "module does not support an option for this operation. " 504 "A reboot may be required to enable this option.")); 505 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 506 break; 507 case ZFS_ERR_IOC_ARG_REQUIRED: 508 case ZFS_ERR_IOC_ARG_BADTYPE: 509 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 510 break; 511 case ZFS_ERR_WRONG_PARENT: 512 zfs_verror(hdl, EZFS_WRONG_PARENT, fmt, ap); 513 break; 514 case ZFS_ERR_BADPROP: 515 zfs_verror(hdl, EZFS_BADPROP, fmt, ap); 516 break; 517 case ZFS_ERR_NOT_USER_NAMESPACE: 518 zfs_verror(hdl, EZFS_NOT_USER_NAMESPACE, fmt, ap); 519 break; 520 default: 521 zfs_error_aux(hdl, "%s", zfs_strerror(error)); 522 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 523 break; 524 } 525 526 va_end(ap); 527 return (-1); 528 } 529 530 void 531 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 532 char *errbuf) 533 { 534 switch (err) { 535 536 case ENOSPC: 537 /* 538 * For quotas and reservations, ENOSPC indicates 539 * something different; setting a quota or reservation 540 * doesn't use any disk space. 541 */ 542 switch (prop) { 543 case ZFS_PROP_QUOTA: 544 case ZFS_PROP_REFQUOTA: 545 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 546 "size is less than current used or " 547 "reserved space")); 548 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 549 break; 550 551 case ZFS_PROP_RESERVATION: 552 case ZFS_PROP_REFRESERVATION: 553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 554 "size is greater than available space")); 555 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 556 break; 557 558 default: 559 (void) zfs_standard_error(hdl, err, errbuf); 560 break; 561 } 562 break; 563 564 case EBUSY: 565 (void) zfs_standard_error(hdl, EBUSY, errbuf); 566 break; 567 568 case EROFS: 569 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 570 break; 571 572 case E2BIG: 573 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 574 "property value too long")); 575 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 576 break; 577 578 case ENOTSUP: 579 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 580 "pool and or dataset must be upgraded to set this " 581 "property or value")); 582 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 583 break; 584 585 case ERANGE: 586 if (prop == ZFS_PROP_COMPRESSION || 587 prop == ZFS_PROP_DNODESIZE || 588 prop == ZFS_PROP_RECORDSIZE) { 589 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 590 "property setting is not allowed on " 591 "bootable datasets")); 592 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 593 } else if (prop == ZFS_PROP_CHECKSUM || 594 prop == ZFS_PROP_DEDUP) { 595 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 596 "property setting is not allowed on " 597 "root pools")); 598 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 599 } else { 600 (void) zfs_standard_error(hdl, err, errbuf); 601 } 602 break; 603 604 case EINVAL: 605 if (prop == ZPROP_INVAL) { 606 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 607 } else { 608 (void) zfs_standard_error(hdl, err, errbuf); 609 } 610 break; 611 612 case ZFS_ERR_BADPROP: 613 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 614 break; 615 616 case EACCES: 617 if (prop == ZFS_PROP_KEYLOCATION) { 618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 619 "keylocation may only be set on encryption roots")); 620 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 621 } else { 622 (void) zfs_standard_error(hdl, err, errbuf); 623 } 624 break; 625 626 case EOVERFLOW: 627 /* 628 * This platform can't address a volume this big. 629 */ 630 #ifdef _ILP32 631 if (prop == ZFS_PROP_VOLSIZE) { 632 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 633 break; 634 } 635 zfs_fallthrough; 636 #endif 637 default: 638 (void) zfs_standard_error(hdl, err, errbuf); 639 } 640 } 641 642 int 643 zpool_standard_error(libzfs_handle_t *hdl, int error, const char *msg) 644 { 645 return (zpool_standard_error_fmt(hdl, error, "%s", msg)); 646 } 647 648 int 649 zpool_standard_error_fmt(libzfs_handle_t *hdl, int error, const char *fmt, ...) 650 { 651 va_list ap; 652 653 va_start(ap, fmt); 654 655 if (zfs_common_error(hdl, error, fmt, ap) != 0) { 656 va_end(ap); 657 return (-1); 658 } 659 660 switch (error) { 661 case ENODEV: 662 zfs_verror(hdl, EZFS_NODEVICE, fmt, ap); 663 break; 664 665 case ENOENT: 666 zfs_error_aux(hdl, 667 dgettext(TEXT_DOMAIN, "no such pool or dataset")); 668 zfs_verror(hdl, EZFS_NOENT, fmt, ap); 669 break; 670 671 case EEXIST: 672 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 673 "pool already exists")); 674 zfs_verror(hdl, EZFS_EXISTS, fmt, ap); 675 break; 676 677 case EBUSY: 678 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool is busy")); 679 zfs_verror(hdl, EZFS_BUSY, fmt, ap); 680 break; 681 682 /* There is no pending operation to cancel */ 683 case ENOTACTIVE: 684 zfs_verror(hdl, EZFS_NO_PENDING, fmt, ap); 685 break; 686 687 case ENXIO: 688 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 689 "one or more devices is currently unavailable")); 690 zfs_verror(hdl, EZFS_BADDEV, fmt, ap); 691 break; 692 693 case ENAMETOOLONG: 694 zfs_verror(hdl, EZFS_DEVOVERFLOW, fmt, ap); 695 break; 696 697 case ENOTSUP: 698 zfs_verror(hdl, EZFS_POOL_NOTSUP, fmt, ap); 699 break; 700 701 case EINVAL: 702 zfs_verror(hdl, EZFS_POOL_INVALARG, fmt, ap); 703 break; 704 705 case ENOSPC: 706 case EDQUOT: 707 zfs_verror(hdl, EZFS_NOSPC, fmt, ap); 708 break; 709 710 case EAGAIN: 711 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 712 "pool I/O is currently suspended")); 713 zfs_verror(hdl, EZFS_POOLUNAVAIL, fmt, ap); 714 break; 715 716 case EROFS: 717 zfs_verror(hdl, EZFS_POOLREADONLY, fmt, ap); 718 break; 719 case EDOM: 720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 721 "block size out of range or does not match")); 722 zfs_verror(hdl, EZFS_BADPROP, fmt, ap); 723 break; 724 case EREMOTEIO: 725 zfs_verror(hdl, EZFS_ACTIVE_POOL, fmt, ap); 726 break; 727 case ZFS_ERR_CHECKPOINT_EXISTS: 728 zfs_verror(hdl, EZFS_CHECKPOINT_EXISTS, fmt, ap); 729 break; 730 case ZFS_ERR_DISCARDING_CHECKPOINT: 731 zfs_verror(hdl, EZFS_DISCARDING_CHECKPOINT, fmt, ap); 732 break; 733 case ZFS_ERR_NO_CHECKPOINT: 734 zfs_verror(hdl, EZFS_NO_CHECKPOINT, fmt, ap); 735 break; 736 case ZFS_ERR_DEVRM_IN_PROGRESS: 737 zfs_verror(hdl, EZFS_DEVRM_IN_PROGRESS, fmt, ap); 738 break; 739 case ZFS_ERR_VDEV_TOO_BIG: 740 zfs_verror(hdl, EZFS_VDEV_TOO_BIG, fmt, ap); 741 break; 742 case ZFS_ERR_EXPORT_IN_PROGRESS: 743 zfs_verror(hdl, EZFS_EXPORT_IN_PROGRESS, fmt, ap); 744 break; 745 case ZFS_ERR_RESILVER_IN_PROGRESS: 746 zfs_verror(hdl, EZFS_RESILVERING, fmt, ap); 747 break; 748 case ZFS_ERR_REBUILD_IN_PROGRESS: 749 zfs_verror(hdl, EZFS_REBUILDING, fmt, ap); 750 break; 751 case ZFS_ERR_BADPROP: 752 zfs_verror(hdl, EZFS_BADPROP, fmt, ap); 753 break; 754 case ZFS_ERR_VDEV_NOTSUP: 755 zfs_verror(hdl, EZFS_VDEV_NOTSUP, fmt, ap); 756 break; 757 case ZFS_ERR_IOC_CMD_UNAVAIL: 758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 759 "module does not support this operation. A reboot may " 760 "be required to enable this operation.")); 761 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 762 break; 763 case ZFS_ERR_IOC_ARG_UNAVAIL: 764 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "the loaded zfs " 765 "module does not support an option for this operation. " 766 "A reboot may be required to enable this option.")); 767 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 768 break; 769 case ZFS_ERR_IOC_ARG_REQUIRED: 770 case ZFS_ERR_IOC_ARG_BADTYPE: 771 zfs_verror(hdl, EZFS_IOC_NOTSUPPORTED, fmt, ap); 772 break; 773 case ZFS_ERR_RAIDZ_EXPAND_IN_PROGRESS: 774 zfs_verror(hdl, EZFS_RAIDZ_EXPAND_IN_PROGRESS, fmt, ap); 775 break; 776 case ZFS_ERR_ASHIFT_MISMATCH: 777 zfs_verror(hdl, EZFS_ASHIFT_MISMATCH, fmt, ap); 778 break; 779 default: 780 zfs_error_aux(hdl, "%s", zfs_strerror(error)); 781 zfs_verror(hdl, EZFS_UNKNOWN, fmt, ap); 782 } 783 784 va_end(ap); 785 return (-1); 786 } 787 788 /* 789 * Display an out of memory error message and abort the current program. 790 */ 791 int 792 no_memory(libzfs_handle_t *hdl) 793 { 794 return (zfs_error(hdl, EZFS_NOMEM, "internal error")); 795 } 796 797 /* 798 * A safe form of malloc() which will die if the allocation fails. 799 */ 800 void * 801 zfs_alloc(libzfs_handle_t *hdl, size_t size) 802 { 803 void *data; 804 805 if ((data = calloc(1, size)) == NULL) 806 (void) no_memory(hdl); 807 808 return (data); 809 } 810 811 /* 812 * A safe form of asprintf() which will die if the allocation fails. 813 */ 814 char * 815 zfs_asprintf(libzfs_handle_t *hdl, const char *fmt, ...) 816 { 817 va_list ap; 818 char *ret; 819 int err; 820 821 va_start(ap, fmt); 822 823 err = vasprintf(&ret, fmt, ap); 824 825 va_end(ap); 826 827 if (err < 0) { 828 (void) no_memory(hdl); 829 ret = NULL; 830 } 831 832 return (ret); 833 } 834 835 /* 836 * A safe form of realloc(), which also zeroes newly allocated space. 837 */ 838 void * 839 zfs_realloc(libzfs_handle_t *hdl, void *ptr, size_t oldsize, size_t newsize) 840 { 841 void *ret; 842 843 if ((ret = realloc(ptr, newsize)) == NULL) { 844 (void) no_memory(hdl); 845 return (NULL); 846 } 847 848 memset((char *)ret + oldsize, 0, newsize - oldsize); 849 return (ret); 850 } 851 852 /* 853 * A safe form of strdup() which will die if the allocation fails. 854 */ 855 char * 856 zfs_strdup(libzfs_handle_t *hdl, const char *str) 857 { 858 char *ret; 859 860 if ((ret = strdup(str)) == NULL) 861 (void) no_memory(hdl); 862 863 return (ret); 864 } 865 866 void 867 libzfs_print_on_error(libzfs_handle_t *hdl, boolean_t printerr) 868 { 869 hdl->libzfs_printerr = printerr; 870 } 871 872 /* 873 * Read lines from an open file descriptor and store them in an array of 874 * strings until EOF. lines[] will be allocated and populated with all the 875 * lines read. All newlines are replaced with NULL terminators for 876 * convenience. lines[] must be freed after use with libzfs_free_str_array(). 877 * 878 * Returns the number of lines read. 879 */ 880 static int 881 libzfs_read_stdout_from_fd(int fd, char **lines[]) 882 { 883 884 FILE *fp; 885 int lines_cnt = 0; 886 size_t len = 0; 887 char *line = NULL; 888 char **tmp_lines = NULL, **tmp; 889 890 fp = fdopen(fd, "r"); 891 if (fp == NULL) { 892 close(fd); 893 return (0); 894 } 895 while (getline(&line, &len, fp) != -1) { 896 tmp = realloc(tmp_lines, sizeof (*tmp_lines) * (lines_cnt + 1)); 897 if (tmp == NULL) { 898 /* Return the lines we were able to process */ 899 break; 900 } 901 tmp_lines = tmp; 902 903 /* Remove newline if not EOF */ 904 if (line[strlen(line) - 1] == '\n') 905 line[strlen(line) - 1] = '\0'; 906 907 tmp_lines[lines_cnt] = strdup(line); 908 if (tmp_lines[lines_cnt] == NULL) 909 break; 910 ++lines_cnt; 911 } 912 free(line); 913 fclose(fp); 914 *lines = tmp_lines; 915 return (lines_cnt); 916 } 917 918 static int 919 libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags, 920 char **lines[], int *lines_cnt) 921 { 922 pid_t pid; 923 int error, devnull_fd; 924 int link[2]; 925 926 /* 927 * Setup a pipe between our child and parent process if we're 928 * reading stdout. 929 */ 930 if (lines != NULL && pipe2(link, O_NONBLOCK | O_CLOEXEC) == -1) 931 return (-EPIPE); 932 933 pid = fork(); 934 if (pid == 0) { 935 /* Child process */ 936 setpgid(0, 0); 937 devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC); 938 939 if (devnull_fd < 0) 940 _exit(-1); 941 942 if (!(flags & STDOUT_VERBOSE) && (lines == NULL)) 943 (void) dup2(devnull_fd, STDOUT_FILENO); 944 else if (lines != NULL) { 945 /* Save the output to lines[] */ 946 dup2(link[1], STDOUT_FILENO); 947 } 948 949 if (!(flags & STDERR_VERBOSE)) 950 (void) dup2(devnull_fd, STDERR_FILENO); 951 952 if (flags & NO_DEFAULT_PATH) { 953 if (env == NULL) 954 execv(path, argv); 955 else 956 execve(path, argv, env); 957 } else { 958 if (env == NULL) 959 execvp(path, argv); 960 else 961 execvpe(path, argv, env); 962 } 963 964 _exit(-1); 965 } else if (pid > 0) { 966 /* Parent process */ 967 int status; 968 969 while ((error = waitpid(pid, &status, 0)) == -1 && 970 errno == EINTR) 971 ; 972 if (error < 0 || !WIFEXITED(status)) 973 return (-1); 974 975 if (lines != NULL) { 976 close(link[1]); 977 *lines_cnt = libzfs_read_stdout_from_fd(link[0], lines); 978 } 979 return (WEXITSTATUS(status)); 980 } 981 982 return (-1); 983 } 984 985 int 986 libzfs_run_process(const char *path, char *argv[], int flags) 987 { 988 return (libzfs_run_process_impl(path, argv, NULL, flags, NULL, NULL)); 989 } 990 991 /* 992 * Run a command and store its stdout lines in an array of strings (lines[]). 993 * lines[] is allocated and populated for you, and the number of lines is set in 994 * lines_cnt. lines[] must be freed after use with libzfs_free_str_array(). 995 * All newlines (\n) in lines[] are terminated for convenience. 996 */ 997 int 998 libzfs_run_process_get_stdout(const char *path, char *argv[], char *env[], 999 char **lines[], int *lines_cnt) 1000 { 1001 return (libzfs_run_process_impl(path, argv, env, 0, lines, lines_cnt)); 1002 } 1003 1004 /* 1005 * Same as libzfs_run_process_get_stdout(), but run without $PATH set. This 1006 * means that *path needs to be the full path to the executable. 1007 */ 1008 int 1009 libzfs_run_process_get_stdout_nopath(const char *path, char *argv[], 1010 char *env[], char **lines[], int *lines_cnt) 1011 { 1012 return (libzfs_run_process_impl(path, argv, env, NO_DEFAULT_PATH, 1013 lines, lines_cnt)); 1014 } 1015 1016 /* 1017 * Free an array of strings. Free both the strings contained in the array and 1018 * the array itself. 1019 */ 1020 void 1021 libzfs_free_str_array(char **strs, int count) 1022 { 1023 while (--count >= 0) 1024 free(strs[count]); 1025 1026 free(strs); 1027 } 1028 1029 /* 1030 * Returns 1 if environment variable is set to "YES", "yes", "ON", "on", or 1031 * a non-zero number. 1032 * 1033 * Returns 0 otherwise. 1034 */ 1035 boolean_t 1036 libzfs_envvar_is_set(const char *envvar) 1037 { 1038 char *env = getenv(envvar); 1039 return (env && (strtoul(env, NULL, 0) > 0 || 1040 (!strncasecmp(env, "YES", 3) && strnlen(env, 4) == 3) || 1041 (!strncasecmp(env, "ON", 2) && strnlen(env, 3) == 2))); 1042 } 1043 1044 libzfs_handle_t * 1045 libzfs_init(void) 1046 { 1047 libzfs_handle_t *hdl; 1048 int error; 1049 char *env; 1050 1051 if ((error = libzfs_load_module()) != 0) { 1052 errno = error; 1053 return (NULL); 1054 } 1055 1056 if ((hdl = calloc(1, sizeof (libzfs_handle_t))) == NULL) { 1057 return (NULL); 1058 } 1059 1060 if (regcomp(&hdl->libzfs_urire, URI_REGEX, 0) != 0) { 1061 free(hdl); 1062 return (NULL); 1063 } 1064 1065 if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL|O_CLOEXEC)) < 0) { 1066 free(hdl); 1067 return (NULL); 1068 } 1069 1070 if (libzfs_core_init() != 0) { 1071 (void) close(hdl->libzfs_fd); 1072 free(hdl); 1073 return (NULL); 1074 } 1075 1076 zfs_prop_init(); 1077 zpool_prop_init(); 1078 zpool_feature_init(); 1079 vdev_prop_init(); 1080 libzfs_mnttab_init(hdl); 1081 fletcher_4_init(); 1082 1083 if (getenv("ZFS_PROP_DEBUG") != NULL) { 1084 hdl->libzfs_prop_debug = B_TRUE; 1085 } 1086 if ((env = getenv("ZFS_SENDRECV_MAX_NVLIST")) != NULL) { 1087 if ((error = zfs_nicestrtonum(hdl, env, 1088 &hdl->libzfs_max_nvlist))) { 1089 errno = error; 1090 (void) close(hdl->libzfs_fd); 1091 free(hdl); 1092 return (NULL); 1093 } 1094 } else { 1095 hdl->libzfs_max_nvlist = (SPA_MAXBLOCKSIZE * 4); 1096 } 1097 1098 /* 1099 * For testing, remove some settable properties and features 1100 */ 1101 if (libzfs_envvar_is_set("ZFS_SYSFS_PROP_SUPPORT_TEST")) { 1102 zprop_desc_t *proptbl; 1103 1104 proptbl = zpool_prop_get_table(); 1105 proptbl[ZPOOL_PROP_COMMENT].pd_zfs_mod_supported = B_FALSE; 1106 1107 proptbl = zfs_prop_get_table(); 1108 proptbl[ZFS_PROP_DNODESIZE].pd_zfs_mod_supported = B_FALSE; 1109 1110 zfeature_info_t *ftbl = spa_feature_table; 1111 ftbl[SPA_FEATURE_LARGE_BLOCKS].fi_zfs_mod_supported = B_FALSE; 1112 } 1113 1114 return (hdl); 1115 } 1116 1117 void 1118 libzfs_fini(libzfs_handle_t *hdl) 1119 { 1120 (void) close(hdl->libzfs_fd); 1121 zpool_free_handles(hdl); 1122 namespace_clear(hdl); 1123 libzfs_mnttab_fini(hdl); 1124 libzfs_core_fini(); 1125 regfree(&hdl->libzfs_urire); 1126 fletcher_4_fini(); 1127 #if LIBFETCH_DYNAMIC 1128 if (hdl->libfetch != (void *)-1 && hdl->libfetch != NULL) 1129 (void) dlclose(hdl->libfetch); 1130 free(hdl->libfetch_load_error); 1131 #endif 1132 free(hdl); 1133 } 1134 1135 libzfs_handle_t * 1136 zpool_get_handle(zpool_handle_t *zhp) 1137 { 1138 return (zhp->zpool_hdl); 1139 } 1140 1141 libzfs_handle_t * 1142 zfs_get_handle(zfs_handle_t *zhp) 1143 { 1144 return (zhp->zfs_hdl); 1145 } 1146 1147 zpool_handle_t * 1148 zfs_get_pool_handle(const zfs_handle_t *zhp) 1149 { 1150 return (zhp->zpool_hdl); 1151 } 1152 1153 /* 1154 * Given a name, determine whether or not it's a valid path 1155 * (starts with '/' or "./"). If so, walk the mnttab trying 1156 * to match the device number. If not, treat the path as an 1157 * fs/vol/snap/bkmark name. 1158 */ 1159 zfs_handle_t * 1160 zfs_path_to_zhandle(libzfs_handle_t *hdl, const char *path, zfs_type_t argtype) 1161 { 1162 struct stat64 statbuf; 1163 struct extmnttab entry; 1164 1165 if (path[0] != '/' && strncmp(path, "./", strlen("./")) != 0) { 1166 /* 1167 * It's not a valid path, assume it's a name of type 'argtype'. 1168 */ 1169 return (zfs_open(hdl, path, argtype)); 1170 } 1171 1172 if (getextmntent(path, &entry, &statbuf) != 0) 1173 return (NULL); 1174 1175 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 1176 (void) fprintf(stderr, gettext("'%s': not a ZFS filesystem\n"), 1177 path); 1178 return (NULL); 1179 } 1180 1181 return (zfs_open(hdl, entry.mnt_special, ZFS_TYPE_FILESYSTEM)); 1182 } 1183 1184 /* 1185 * Initialize the zc_nvlist_dst member to prepare for receiving an nvlist from 1186 * an ioctl(). 1187 */ 1188 void 1189 zcmd_alloc_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, size_t len) 1190 { 1191 if (len == 0) 1192 len = 256 * 1024; 1193 zc->zc_nvlist_dst_size = len; 1194 zc->zc_nvlist_dst = 1195 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 1196 } 1197 1198 /* 1199 * Called when an ioctl() which returns an nvlist fails with ENOMEM. This will 1200 * expand the nvlist to the size specified in 'zc_nvlist_dst_size', which was 1201 * filled in by the kernel to indicate the actual required size. 1202 */ 1203 void 1204 zcmd_expand_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc) 1205 { 1206 free((void *)(uintptr_t)zc->zc_nvlist_dst); 1207 zc->zc_nvlist_dst = 1208 (uint64_t)(uintptr_t)zfs_alloc(hdl, zc->zc_nvlist_dst_size); 1209 } 1210 1211 /* 1212 * Called to free the src and dst nvlists stored in the command structure. 1213 */ 1214 void 1215 zcmd_free_nvlists(zfs_cmd_t *zc) 1216 { 1217 free((void *)(uintptr_t)zc->zc_nvlist_conf); 1218 free((void *)(uintptr_t)zc->zc_nvlist_src); 1219 free((void *)(uintptr_t)zc->zc_nvlist_dst); 1220 zc->zc_nvlist_conf = 0; 1221 zc->zc_nvlist_src = 0; 1222 zc->zc_nvlist_dst = 0; 1223 } 1224 1225 static void 1226 zcmd_write_nvlist_com(libzfs_handle_t *hdl, uint64_t *outnv, uint64_t *outlen, 1227 nvlist_t *nvl) 1228 { 1229 char *packed; 1230 1231 size_t len = fnvlist_size(nvl); 1232 packed = zfs_alloc(hdl, len); 1233 1234 verify(nvlist_pack(nvl, &packed, &len, NV_ENCODE_NATIVE, 0) == 0); 1235 1236 *outnv = (uint64_t)(uintptr_t)packed; 1237 *outlen = len; 1238 } 1239 1240 void 1241 zcmd_write_conf_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 1242 { 1243 zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_conf, 1244 &zc->zc_nvlist_conf_size, nvl); 1245 } 1246 1247 void 1248 zcmd_write_src_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t *nvl) 1249 { 1250 zcmd_write_nvlist_com(hdl, &zc->zc_nvlist_src, 1251 &zc->zc_nvlist_src_size, nvl); 1252 } 1253 1254 /* 1255 * Unpacks an nvlist from the ZFS ioctl command structure. 1256 */ 1257 int 1258 zcmd_read_dst_nvlist(libzfs_handle_t *hdl, zfs_cmd_t *zc, nvlist_t **nvlp) 1259 { 1260 if (nvlist_unpack((void *)(uintptr_t)zc->zc_nvlist_dst, 1261 zc->zc_nvlist_dst_size, nvlp, 0) != 0) 1262 return (no_memory(hdl)); 1263 1264 return (0); 1265 } 1266 1267 /* 1268 * ================================================================ 1269 * API shared by zfs and zpool property management 1270 * ================================================================ 1271 */ 1272 1273 void 1274 zcmd_print_json(nvlist_t *nvl) 1275 { 1276 nvlist_print_json(stdout, nvl); 1277 (void) putchar('\n'); 1278 nvlist_free(nvl); 1279 } 1280 1281 static void 1282 zprop_print_headers(zprop_get_cbdata_t *cbp, zfs_type_t type) 1283 { 1284 zprop_list_t *pl; 1285 int i; 1286 char *title; 1287 size_t len; 1288 1289 cbp->cb_first = B_FALSE; 1290 if (cbp->cb_scripted) 1291 return; 1292 1293 /* 1294 * Start with the length of the column headers. 1295 */ 1296 cbp->cb_colwidths[GET_COL_NAME] = strlen(dgettext(TEXT_DOMAIN, "NAME")); 1297 cbp->cb_colwidths[GET_COL_PROPERTY] = strlen(dgettext(TEXT_DOMAIN, 1298 "PROPERTY")); 1299 cbp->cb_colwidths[GET_COL_VALUE] = strlen(dgettext(TEXT_DOMAIN, 1300 "VALUE")); 1301 cbp->cb_colwidths[GET_COL_RECVD] = strlen(dgettext(TEXT_DOMAIN, 1302 "RECEIVED")); 1303 cbp->cb_colwidths[GET_COL_SOURCE] = strlen(dgettext(TEXT_DOMAIN, 1304 "SOURCE")); 1305 1306 /* first property is always NAME */ 1307 assert(cbp->cb_proplist->pl_prop == 1308 ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1309 ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME))); 1310 1311 /* 1312 * Go through and calculate the widths for each column. For the 1313 * 'source' column, we kludge it up by taking the worst-case scenario of 1314 * inheriting from the longest name. This is acceptable because in the 1315 * majority of cases 'SOURCE' is the last column displayed, and we don't 1316 * use the width anyway. Note that the 'VALUE' column can be oversized, 1317 * if the name of the property is much longer than any values we find. 1318 */ 1319 for (pl = cbp->cb_proplist; pl != NULL; pl = pl->pl_next) { 1320 /* 1321 * 'PROPERTY' column 1322 */ 1323 if (pl->pl_prop != ZPROP_USERPROP) { 1324 const char *propname = (type == ZFS_TYPE_POOL) ? 1325 zpool_prop_to_name(pl->pl_prop) : 1326 ((type == ZFS_TYPE_VDEV) ? 1327 vdev_prop_to_name(pl->pl_prop) : 1328 zfs_prop_to_name(pl->pl_prop)); 1329 1330 assert(propname != NULL); 1331 len = strlen(propname); 1332 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 1333 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 1334 } else { 1335 assert(pl->pl_user_prop != NULL); 1336 len = strlen(pl->pl_user_prop); 1337 if (len > cbp->cb_colwidths[GET_COL_PROPERTY]) 1338 cbp->cb_colwidths[GET_COL_PROPERTY] = len; 1339 } 1340 1341 /* 1342 * 'VALUE' column. The first property is always the 'name' 1343 * property that was tacked on either by /sbin/zfs's 1344 * zfs_do_get() or when calling zprop_expand_list(), so we 1345 * ignore its width. If the user specified the name property 1346 * to display, then it will be later in the list in any case. 1347 */ 1348 if (pl != cbp->cb_proplist && 1349 pl->pl_width > cbp->cb_colwidths[GET_COL_VALUE]) 1350 cbp->cb_colwidths[GET_COL_VALUE] = pl->pl_width; 1351 1352 /* 'RECEIVED' column. */ 1353 if (pl != cbp->cb_proplist && 1354 pl->pl_recvd_width > cbp->cb_colwidths[GET_COL_RECVD]) 1355 cbp->cb_colwidths[GET_COL_RECVD] = pl->pl_recvd_width; 1356 1357 /* 1358 * 'NAME' and 'SOURCE' columns 1359 */ 1360 if (pl->pl_prop == ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 1361 ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : 1362 ZFS_PROP_NAME)) && pl->pl_width > 1363 cbp->cb_colwidths[GET_COL_NAME]) { 1364 cbp->cb_colwidths[GET_COL_NAME] = pl->pl_width; 1365 cbp->cb_colwidths[GET_COL_SOURCE] = pl->pl_width + 1366 strlen(dgettext(TEXT_DOMAIN, "inherited from")); 1367 } 1368 } 1369 1370 /* 1371 * Now go through and print the headers. 1372 */ 1373 for (i = 0; i < ZFS_GET_NCOLS; i++) { 1374 switch (cbp->cb_columns[i]) { 1375 case GET_COL_NAME: 1376 title = dgettext(TEXT_DOMAIN, "NAME"); 1377 break; 1378 case GET_COL_PROPERTY: 1379 title = dgettext(TEXT_DOMAIN, "PROPERTY"); 1380 break; 1381 case GET_COL_VALUE: 1382 title = dgettext(TEXT_DOMAIN, "VALUE"); 1383 break; 1384 case GET_COL_RECVD: 1385 title = dgettext(TEXT_DOMAIN, "RECEIVED"); 1386 break; 1387 case GET_COL_SOURCE: 1388 title = dgettext(TEXT_DOMAIN, "SOURCE"); 1389 break; 1390 default: 1391 title = NULL; 1392 } 1393 1394 if (title != NULL) { 1395 if (i == (ZFS_GET_NCOLS - 1) || 1396 cbp->cb_columns[i + 1] == GET_COL_NONE) 1397 (void) printf("%s", title); 1398 else 1399 (void) printf("%-*s ", 1400 cbp->cb_colwidths[cbp->cb_columns[i]], 1401 title); 1402 } 1403 } 1404 (void) printf("\n"); 1405 } 1406 1407 /* 1408 * Add property value and source to provided nvlist, according to 1409 * settings in cb structure. Later to be printed in JSON format. 1410 */ 1411 int 1412 zprop_nvlist_one_property(const char *propname, 1413 const char *value, zprop_source_t sourcetype, const char *source, 1414 const char *recvd_value, nvlist_t *nvl, boolean_t as_int) 1415 { 1416 int ret = 0; 1417 nvlist_t *src_nv, *prop; 1418 boolean_t all_numeric = strspn(value, STR_NUMS) == strlen(value); 1419 src_nv = prop = NULL; 1420 1421 if ((nvlist_alloc(&prop, NV_UNIQUE_NAME, 0) != 0) || 1422 (nvlist_alloc(&src_nv, NV_UNIQUE_NAME, 0) != 0)) { 1423 ret = -1; 1424 goto err; 1425 } 1426 1427 if (as_int && all_numeric) { 1428 uint64_t val; 1429 sscanf(value, "%lld", (u_longlong_t *)&val); 1430 if (nvlist_add_uint64(prop, "value", val) != 0) { 1431 ret = -1; 1432 goto err; 1433 } 1434 } else { 1435 if (nvlist_add_string(prop, "value", value) != 0) { 1436 ret = -1; 1437 goto err; 1438 } 1439 } 1440 1441 switch (sourcetype) { 1442 case ZPROP_SRC_NONE: 1443 if (nvlist_add_string(src_nv, "type", "NONE") != 0 || 1444 (nvlist_add_string(src_nv, "data", "-") != 0)) { 1445 ret = -1; 1446 goto err; 1447 } 1448 break; 1449 case ZPROP_SRC_DEFAULT: 1450 if (nvlist_add_string(src_nv, "type", "DEFAULT") != 0 || 1451 (nvlist_add_string(src_nv, "data", "-") != 0)) { 1452 ret = -1; 1453 goto err; 1454 } 1455 break; 1456 case ZPROP_SRC_LOCAL: 1457 if (nvlist_add_string(src_nv, "type", "LOCAL") != 0 || 1458 (nvlist_add_string(src_nv, "data", "-") != 0)) { 1459 ret = -1; 1460 goto err; 1461 } 1462 break; 1463 case ZPROP_SRC_TEMPORARY: 1464 if (nvlist_add_string(src_nv, "type", "TEMPORARY") != 0 || 1465 (nvlist_add_string(src_nv, "data", "-") != 0)) { 1466 ret = -1; 1467 goto err; 1468 } 1469 break; 1470 case ZPROP_SRC_INHERITED: 1471 if (nvlist_add_string(src_nv, "type", "INHERITED") != 0 || 1472 (nvlist_add_string(src_nv, "data", source) != 0)) { 1473 ret = -1; 1474 goto err; 1475 } 1476 break; 1477 case ZPROP_SRC_RECEIVED: 1478 if (nvlist_add_string(src_nv, "type", "RECEIVED") != 0 || 1479 (nvlist_add_string(src_nv, "data", 1480 (recvd_value == NULL ? "-" : recvd_value)) != 0)) { 1481 ret = -1; 1482 goto err; 1483 } 1484 break; 1485 default: 1486 assert(!"unhandled zprop_source_t"); 1487 if (nvlist_add_string(src_nv, "type", 1488 "unhandled zprop_source_t") != 0) { 1489 ret = -1; 1490 goto err; 1491 } 1492 } 1493 if ((nvlist_add_nvlist(prop, "source", src_nv) != 0) || 1494 (nvlist_add_nvlist(nvl, propname, prop)) != 0) { 1495 ret = -1; 1496 goto err; 1497 } 1498 err: 1499 nvlist_free(src_nv); 1500 nvlist_free(prop); 1501 return (ret); 1502 } 1503 1504 /* 1505 * Display a single line of output, according to the settings in the callback 1506 * structure. 1507 */ 1508 void 1509 zprop_print_one_property(const char *name, zprop_get_cbdata_t *cbp, 1510 const char *propname, const char *value, zprop_source_t sourcetype, 1511 const char *source, const char *recvd_value) 1512 { 1513 int i; 1514 const char *str = NULL; 1515 char buf[128]; 1516 1517 /* 1518 * Ignore those source types that the user has chosen to ignore. 1519 */ 1520 if ((sourcetype & cbp->cb_sources) == 0) 1521 return; 1522 1523 if (cbp->cb_first) 1524 zprop_print_headers(cbp, cbp->cb_type); 1525 1526 for (i = 0; i < ZFS_GET_NCOLS; i++) { 1527 switch (cbp->cb_columns[i]) { 1528 case GET_COL_NAME: 1529 str = name; 1530 break; 1531 1532 case GET_COL_PROPERTY: 1533 str = propname; 1534 break; 1535 1536 case GET_COL_VALUE: 1537 str = value; 1538 break; 1539 1540 case GET_COL_SOURCE: 1541 switch (sourcetype) { 1542 case ZPROP_SRC_NONE: 1543 str = "-"; 1544 break; 1545 1546 case ZPROP_SRC_DEFAULT: 1547 str = "default"; 1548 break; 1549 1550 case ZPROP_SRC_LOCAL: 1551 str = "local"; 1552 break; 1553 1554 case ZPROP_SRC_TEMPORARY: 1555 str = "temporary"; 1556 break; 1557 1558 case ZPROP_SRC_INHERITED: 1559 (void) snprintf(buf, sizeof (buf), 1560 "inherited from %s", source); 1561 str = buf; 1562 break; 1563 case ZPROP_SRC_RECEIVED: 1564 str = "received"; 1565 break; 1566 1567 default: 1568 str = NULL; 1569 assert(!"unhandled zprop_source_t"); 1570 } 1571 break; 1572 1573 case GET_COL_RECVD: 1574 str = (recvd_value == NULL ? "-" : recvd_value); 1575 break; 1576 1577 default: 1578 continue; 1579 } 1580 1581 if (i == (ZFS_GET_NCOLS - 1) || 1582 cbp->cb_columns[i + 1] == GET_COL_NONE) 1583 (void) printf("%s", str); 1584 else if (cbp->cb_scripted) 1585 (void) printf("%s\t", str); 1586 else 1587 (void) printf("%-*s ", 1588 cbp->cb_colwidths[cbp->cb_columns[i]], 1589 str); 1590 } 1591 1592 (void) printf("\n"); 1593 } 1594 1595 int 1596 zprop_collect_property(const char *name, zprop_get_cbdata_t *cbp, 1597 const char *propname, const char *value, zprop_source_t sourcetype, 1598 const char *source, const char *recvd_value, nvlist_t *nvl) 1599 { 1600 if (cbp->cb_json) { 1601 if ((sourcetype & cbp->cb_sources) == 0) 1602 return (0); 1603 else { 1604 return (zprop_nvlist_one_property(propname, value, 1605 sourcetype, source, recvd_value, nvl, 1606 cbp->cb_json_as_int)); 1607 } 1608 } else { 1609 zprop_print_one_property(name, cbp, 1610 propname, value, sourcetype, source, recvd_value); 1611 return (0); 1612 } 1613 } 1614 1615 /* 1616 * Given a numeric suffix, convert the value into a number of bits that the 1617 * resulting value must be shifted. 1618 */ 1619 static int 1620 str2shift(libzfs_handle_t *hdl, const char *buf) 1621 { 1622 const char *ends = "BKMGTPEZ"; 1623 int i, len; 1624 1625 if (buf[0] == '\0') 1626 return (0); 1627 1628 len = strlen(ends); 1629 for (i = 0; i < len; i++) { 1630 if (toupper(buf[0]) == ends[i]) 1631 break; 1632 } 1633 if (i == len) { 1634 if (hdl) 1635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1636 "invalid numeric suffix '%s'"), buf); 1637 return (-1); 1638 } 1639 1640 /* 1641 * Allow 'G' = 'GB' = 'GiB', case-insensitively. 1642 * However, 'BB' and 'BiB' are disallowed. 1643 */ 1644 if (buf[1] == '\0' || 1645 (toupper(buf[0]) != 'B' && 1646 ((toupper(buf[1]) == 'B' && buf[2] == '\0') || 1647 (toupper(buf[1]) == 'I' && toupper(buf[2]) == 'B' && 1648 buf[3] == '\0')))) 1649 return (10 * i); 1650 1651 if (hdl) 1652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1653 "invalid numeric suffix '%s'"), buf); 1654 return (-1); 1655 } 1656 1657 /* 1658 * Convert a string of the form '100G' into a real number. Used when setting 1659 * properties or creating a volume. 'buf' is used to place an extended error 1660 * message for the caller to use. 1661 */ 1662 int 1663 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 1664 { 1665 char *end; 1666 int shift; 1667 1668 *num = 0; 1669 1670 /* Check to see if this looks like a number. */ 1671 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 1672 if (hdl) 1673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1674 "bad numeric value '%s'"), value); 1675 return (-1); 1676 } 1677 1678 /* Rely on strtoull() to process the numeric portion. */ 1679 errno = 0; 1680 *num = strtoull(value, &end, 10); 1681 1682 /* 1683 * Check for ERANGE, which indicates that the value is too large to fit 1684 * in a 64-bit value. 1685 */ 1686 if (errno == ERANGE) { 1687 if (hdl) 1688 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1689 "numeric value is too large")); 1690 return (-1); 1691 } 1692 1693 /* 1694 * If we have a decimal value, then do the computation with floating 1695 * point arithmetic. Otherwise, use standard arithmetic. 1696 */ 1697 if (*end == '.') { 1698 double fval = strtod(value, &end); 1699 1700 if ((shift = str2shift(hdl, end)) == -1) 1701 return (-1); 1702 1703 fval *= pow(2, shift); 1704 1705 /* 1706 * UINT64_MAX is not exactly representable as a double. 1707 * The closest representation is UINT64_MAX + 1, so we 1708 * use a >= comparison instead of > for the bounds check. 1709 */ 1710 if (fval >= (double)UINT64_MAX) { 1711 if (hdl) 1712 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1713 "numeric value is too large")); 1714 return (-1); 1715 } 1716 1717 *num = (uint64_t)fval; 1718 } else { 1719 if ((shift = str2shift(hdl, end)) == -1) 1720 return (-1); 1721 1722 /* Check for overflow */ 1723 if (shift >= 64 || (*num << shift) >> shift != *num) { 1724 if (hdl) 1725 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1726 "numeric value is too large")); 1727 return (-1); 1728 } 1729 1730 *num <<= shift; 1731 } 1732 1733 return (0); 1734 } 1735 1736 /* 1737 * Given a propname=value nvpair to set, parse any numeric properties 1738 * (index, boolean, etc) if they are specified as strings and add the 1739 * resulting nvpair to the returned nvlist. 1740 * 1741 * At the DSL layer, all properties are either 64-bit numbers or strings. 1742 * We want the user to be able to ignore this fact and specify properties 1743 * as native values (numbers, for example) or as strings (to simplify 1744 * command line utilities). This also handles converting index types 1745 * (compression, checksum, etc) from strings to their on-disk index. 1746 */ 1747 int 1748 zprop_parse_value(libzfs_handle_t *hdl, nvpair_t *elem, int prop, 1749 zfs_type_t type, nvlist_t *ret, const char **svalp, uint64_t *ivalp, 1750 const char *errbuf) 1751 { 1752 data_type_t datatype = nvpair_type(elem); 1753 zprop_type_t proptype; 1754 const char *propname; 1755 const char *value; 1756 boolean_t isnone = B_FALSE; 1757 boolean_t isauto = B_FALSE; 1758 int err = 0; 1759 1760 if (type == ZFS_TYPE_POOL) { 1761 proptype = zpool_prop_get_type(prop); 1762 propname = zpool_prop_to_name(prop); 1763 } else if (type == ZFS_TYPE_VDEV) { 1764 proptype = vdev_prop_get_type(prop); 1765 propname = vdev_prop_to_name(prop); 1766 } else { 1767 proptype = zfs_prop_get_type(prop); 1768 propname = zfs_prop_to_name(prop); 1769 } 1770 1771 /* 1772 * Convert any properties to the internal DSL value types. 1773 */ 1774 *svalp = NULL; 1775 *ivalp = 0; 1776 1777 switch (proptype) { 1778 case PROP_TYPE_STRING: 1779 if (datatype != DATA_TYPE_STRING) { 1780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1781 "'%s' must be a string"), nvpair_name(elem)); 1782 goto error; 1783 } 1784 err = nvpair_value_string(elem, svalp); 1785 if (err != 0) { 1786 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1787 "'%s' is invalid"), nvpair_name(elem)); 1788 goto error; 1789 } 1790 if (strlen(*svalp) >= ZFS_MAXPROPLEN) { 1791 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1792 "'%s' is too long"), nvpair_name(elem)); 1793 goto error; 1794 } 1795 break; 1796 1797 case PROP_TYPE_NUMBER: 1798 if (datatype == DATA_TYPE_STRING) { 1799 (void) nvpair_value_string(elem, &value); 1800 if (strcmp(value, "none") == 0) { 1801 isnone = B_TRUE; 1802 } else if (strcmp(value, "auto") == 0) { 1803 isauto = B_TRUE; 1804 } else if (zfs_nicestrtonum(hdl, value, ivalp) != 0) { 1805 goto error; 1806 } 1807 } else if (datatype == DATA_TYPE_UINT64) { 1808 (void) nvpair_value_uint64(elem, ivalp); 1809 } else { 1810 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1811 "'%s' must be a number"), nvpair_name(elem)); 1812 goto error; 1813 } 1814 1815 /* 1816 * Quota special: force 'none' and don't allow 0. 1817 */ 1818 if ((type & ZFS_TYPE_DATASET) && *ivalp == 0 && !isnone && 1819 (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_REFQUOTA)) { 1820 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1821 "use 'none' to disable quota/refquota")); 1822 goto error; 1823 } 1824 /* 1825 * Pool dedup table quota; force use of 'none' instead of 0 1826 */ 1827 if ((type & ZFS_TYPE_POOL) && *ivalp == 0 && 1828 (!isnone && !isauto) && 1829 prop == ZPOOL_PROP_DEDUP_TABLE_QUOTA) { 1830 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1831 "use 'none' to disable ddt table quota")); 1832 goto error; 1833 } 1834 1835 /* 1836 * Special handling for "*_limit=none". In this case it's not 1837 * 0 but UINT64_MAX. 1838 */ 1839 if ((type & ZFS_TYPE_DATASET) && isnone && 1840 (prop == ZFS_PROP_FILESYSTEM_LIMIT || 1841 prop == ZFS_PROP_SNAPSHOT_LIMIT)) { 1842 *ivalp = UINT64_MAX; 1843 } 1844 1845 /* 1846 * Special handling for "checksum_*=none". In this case it's not 1847 * 0 but UINT64_MAX. 1848 */ 1849 if ((type & ZFS_TYPE_VDEV) && isnone && 1850 (prop == VDEV_PROP_CHECKSUM_N || 1851 prop == VDEV_PROP_CHECKSUM_T || 1852 prop == VDEV_PROP_IO_N || 1853 prop == VDEV_PROP_IO_T || 1854 prop == VDEV_PROP_SLOW_IO_N || 1855 prop == VDEV_PROP_SLOW_IO_T)) { 1856 *ivalp = UINT64_MAX; 1857 } 1858 1859 /* 1860 * Special handling for setting 'refreservation' to 'auto'. Use 1861 * UINT64_MAX to tell the caller to use zfs_fix_auto_resv(). 1862 * 'auto' is only allowed on volumes. 1863 */ 1864 if (isauto) { 1865 switch (prop) { 1866 case ZFS_PROP_REFRESERVATION: 1867 if ((type & ZFS_TYPE_VOLUME) == 0) { 1868 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1869 "'%s=auto' only allowed on " 1870 "volumes"), nvpair_name(elem)); 1871 goto error; 1872 } 1873 *ivalp = UINT64_MAX; 1874 break; 1875 case ZPOOL_PROP_DEDUP_TABLE_QUOTA: 1876 ASSERT(type & ZFS_TYPE_POOL); 1877 *ivalp = UINT64_MAX; 1878 break; 1879 default: 1880 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1881 "'auto' is invalid value for '%s'"), 1882 nvpair_name(elem)); 1883 goto error; 1884 } 1885 } 1886 1887 break; 1888 1889 case PROP_TYPE_INDEX: 1890 if (datatype != DATA_TYPE_STRING) { 1891 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1892 "'%s' must be a string"), nvpair_name(elem)); 1893 goto error; 1894 } 1895 1896 (void) nvpair_value_string(elem, &value); 1897 1898 if (zprop_string_to_index(prop, value, ivalp, type) != 0) { 1899 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1900 "'%s' must be one of '%s'"), propname, 1901 zprop_values(prop, type)); 1902 goto error; 1903 } 1904 break; 1905 1906 default: 1907 abort(); 1908 } 1909 1910 /* 1911 * Add the result to our return set of properties. 1912 */ 1913 if (*svalp != NULL) { 1914 if (nvlist_add_string(ret, propname, *svalp) != 0) { 1915 (void) no_memory(hdl); 1916 return (-1); 1917 } 1918 } else { 1919 if (nvlist_add_uint64(ret, propname, *ivalp) != 0) { 1920 (void) no_memory(hdl); 1921 return (-1); 1922 } 1923 } 1924 1925 return (0); 1926 error: 1927 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1928 return (-1); 1929 } 1930 1931 static int 1932 addlist(libzfs_handle_t *hdl, const char *propname, zprop_list_t **listp, 1933 zfs_type_t type) 1934 { 1935 int prop = zprop_name_to_prop(propname, type); 1936 if (prop != ZPROP_INVAL && !zprop_valid_for_type(prop, type, B_FALSE)) 1937 prop = ZPROP_INVAL; 1938 1939 /* 1940 * Return failure if no property table entry was found and this isn't 1941 * a user-defined property. 1942 */ 1943 if (prop == ZPROP_USERPROP && ((type == ZFS_TYPE_POOL && 1944 !zfs_prop_user(propname) && 1945 !zpool_prop_feature(propname) && 1946 !zpool_prop_unsupported(propname)) || 1947 ((type == ZFS_TYPE_DATASET) && !zfs_prop_user(propname) && 1948 !zfs_prop_userquota(propname) && !zfs_prop_written(propname)) || 1949 ((type == ZFS_TYPE_VDEV) && !vdev_prop_user(propname)))) { 1950 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1951 "invalid property '%s'"), propname); 1952 return (zfs_error(hdl, EZFS_BADPROP, 1953 dgettext(TEXT_DOMAIN, "bad property list"))); 1954 } 1955 1956 zprop_list_t *entry = zfs_alloc(hdl, sizeof (*entry)); 1957 1958 entry->pl_prop = prop; 1959 if (prop == ZPROP_USERPROP) { 1960 entry->pl_user_prop = zfs_strdup(hdl, propname); 1961 entry->pl_width = strlen(propname); 1962 } else { 1963 entry->pl_width = zprop_width(prop, &entry->pl_fixed, 1964 type); 1965 } 1966 1967 *listp = entry; 1968 1969 return (0); 1970 } 1971 1972 /* 1973 * Given a comma-separated list of properties, construct a property list 1974 * containing both user-defined and native properties. This function will 1975 * return a NULL list if 'all' is specified, which can later be expanded 1976 * by zprop_expand_list(). 1977 */ 1978 int 1979 zprop_get_list(libzfs_handle_t *hdl, char *props, zprop_list_t **listp, 1980 zfs_type_t type) 1981 { 1982 *listp = NULL; 1983 1984 /* 1985 * If 'all' is specified, return a NULL list. 1986 */ 1987 if (strcmp(props, "all") == 0) 1988 return (0); 1989 1990 /* 1991 * If no props were specified, return an error. 1992 */ 1993 if (props[0] == '\0') { 1994 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1995 "no properties specified")); 1996 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 1997 "bad property list"))); 1998 } 1999 2000 for (char *p; (p = strsep(&props, ",")); ) 2001 if (strcmp(p, "space") == 0) { 2002 static const char *const spaceprops[] = { 2003 "name", "avail", "used", "usedbysnapshots", 2004 "usedbydataset", "usedbyrefreservation", 2005 "usedbychildren" 2006 }; 2007 2008 for (int i = 0; i < ARRAY_SIZE(spaceprops); i++) { 2009 if (addlist(hdl, spaceprops[i], listp, type)) 2010 return (-1); 2011 listp = &(*listp)->pl_next; 2012 } 2013 } else { 2014 if (addlist(hdl, p, listp, type)) 2015 return (-1); 2016 listp = &(*listp)->pl_next; 2017 } 2018 2019 return (0); 2020 } 2021 2022 void 2023 zprop_free_list(zprop_list_t *pl) 2024 { 2025 zprop_list_t *next; 2026 2027 while (pl != NULL) { 2028 next = pl->pl_next; 2029 free(pl->pl_user_prop); 2030 free(pl); 2031 pl = next; 2032 } 2033 } 2034 2035 typedef struct expand_data { 2036 zprop_list_t **last; 2037 libzfs_handle_t *hdl; 2038 zfs_type_t type; 2039 } expand_data_t; 2040 2041 static int 2042 zprop_expand_list_cb(int prop, void *cb) 2043 { 2044 zprop_list_t *entry; 2045 expand_data_t *edp = cb; 2046 2047 entry = zfs_alloc(edp->hdl, sizeof (zprop_list_t)); 2048 2049 entry->pl_prop = prop; 2050 entry->pl_width = zprop_width(prop, &entry->pl_fixed, edp->type); 2051 entry->pl_all = B_TRUE; 2052 2053 *(edp->last) = entry; 2054 edp->last = &entry->pl_next; 2055 2056 return (ZPROP_CONT); 2057 } 2058 2059 int 2060 zprop_expand_list(libzfs_handle_t *hdl, zprop_list_t **plp, zfs_type_t type) 2061 { 2062 zprop_list_t *entry; 2063 zprop_list_t **last; 2064 expand_data_t exp; 2065 2066 if (*plp == NULL) { 2067 /* 2068 * If this is the very first time we've been called for an 'all' 2069 * specification, expand the list to include all native 2070 * properties. 2071 */ 2072 last = plp; 2073 2074 exp.last = last; 2075 exp.hdl = hdl; 2076 exp.type = type; 2077 2078 if (zprop_iter_common(zprop_expand_list_cb, &exp, B_FALSE, 2079 B_FALSE, type) == ZPROP_INVAL) 2080 return (-1); 2081 2082 /* 2083 * Add 'name' to the beginning of the list, which is handled 2084 * specially. 2085 */ 2086 entry = zfs_alloc(hdl, sizeof (zprop_list_t)); 2087 entry->pl_prop = ((type == ZFS_TYPE_POOL) ? ZPOOL_PROP_NAME : 2088 ((type == ZFS_TYPE_VDEV) ? VDEV_PROP_NAME : ZFS_PROP_NAME)); 2089 entry->pl_width = zprop_width(entry->pl_prop, 2090 &entry->pl_fixed, type); 2091 entry->pl_all = B_TRUE; 2092 entry->pl_next = *plp; 2093 *plp = entry; 2094 } 2095 return (0); 2096 } 2097 2098 int 2099 zprop_iter(zprop_func func, void *cb, boolean_t show_all, boolean_t ordered, 2100 zfs_type_t type) 2101 { 2102 return (zprop_iter_common(func, cb, show_all, ordered, type)); 2103 } 2104 2105 const char * 2106 zfs_version_userland(void) 2107 { 2108 return (ZFS_META_ALIAS); 2109 } 2110 2111 /* 2112 * Prints both zfs userland and kernel versions 2113 * Returns 0 on success, and -1 on error 2114 */ 2115 int 2116 zfs_version_print(void) 2117 { 2118 (void) puts(ZFS_META_ALIAS); 2119 2120 char *kver = zfs_version_kernel(); 2121 if (kver == NULL) { 2122 fprintf(stderr, "zfs_version_kernel() failed: %s\n", 2123 zfs_strerror(errno)); 2124 return (-1); 2125 } 2126 2127 (void) printf("zfs-kmod-%s\n", kver); 2128 free(kver); 2129 return (0); 2130 } 2131 2132 /* 2133 * Returns an nvlist with both zfs userland and kernel versions. 2134 * Returns NULL on error. 2135 */ 2136 nvlist_t * 2137 zfs_version_nvlist(void) 2138 { 2139 nvlist_t *nvl; 2140 char kmod_ver[64]; 2141 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 2142 return (NULL); 2143 if (nvlist_add_string(nvl, "userland", ZFS_META_ALIAS) != 0) 2144 goto err; 2145 char *kver = zfs_version_kernel(); 2146 if (kver == NULL) { 2147 fprintf(stderr, "zfs_version_kernel() failed: %s\n", 2148 zfs_strerror(errno)); 2149 goto err; 2150 } 2151 (void) snprintf(kmod_ver, 64, "zfs-kmod-%s", kver); 2152 if (nvlist_add_string(nvl, "kernel", kmod_ver) != 0) 2153 goto err; 2154 return (nvl); 2155 err: 2156 nvlist_free(nvl); 2157 return (NULL); 2158 } 2159 2160 /* 2161 * Return 1 if the user requested ANSI color output, and our terminal supports 2162 * it. Return 0 for no color. 2163 */ 2164 int 2165 use_color(void) 2166 { 2167 static int use_color = -1; 2168 char *term; 2169 2170 /* 2171 * Optimization: 2172 * 2173 * For each zpool invocation, we do a single check to see if we should 2174 * be using color or not, and cache that value for the lifetime of the 2175 * the zpool command. That makes it cheap to call use_color() when 2176 * we're printing with color. We assume that the settings are not going 2177 * to change during the invocation of a zpool command (the user isn't 2178 * going to change the ZFS_COLOR value while zpool is running, for 2179 * example). 2180 */ 2181 if (use_color != -1) { 2182 /* 2183 * We've already figured out if we should be using color or 2184 * not. Return the cached value. 2185 */ 2186 return (use_color); 2187 } 2188 2189 term = getenv("TERM"); 2190 /* 2191 * The user sets the ZFS_COLOR env var set to enable zpool ANSI color 2192 * output. However if NO_COLOR is set (https://no-color.org/) then 2193 * don't use it. Also, don't use color if terminal doesn't support 2194 * it. 2195 */ 2196 if (libzfs_envvar_is_set("ZFS_COLOR") && 2197 !libzfs_envvar_is_set("NO_COLOR") && 2198 isatty(STDOUT_FILENO) && term && strcmp("dumb", term) != 0 && 2199 strcmp("unknown", term) != 0) { 2200 /* Color supported */ 2201 use_color = 1; 2202 } else { 2203 use_color = 0; 2204 } 2205 2206 return (use_color); 2207 } 2208 2209 /* 2210 * The functions color_start() and color_end() are used for when you want 2211 * to colorize a block of text. 2212 * 2213 * For example: 2214 * color_start(ANSI_RED) 2215 * printf("hello"); 2216 * printf("world"); 2217 * color_end(); 2218 */ 2219 void 2220 color_start(const char *color) 2221 { 2222 if (color && use_color()) { 2223 fputs(color, stdout); 2224 fflush(stdout); 2225 } 2226 } 2227 2228 void 2229 color_end(void) 2230 { 2231 if (use_color()) { 2232 fputs(ANSI_RESET, stdout); 2233 fflush(stdout); 2234 } 2235 2236 } 2237 2238 /* 2239 * printf() with a color. If color is NULL, then do a normal printf. 2240 */ 2241 int 2242 printf_color(const char *color, const char *format, ...) 2243 { 2244 va_list aptr; 2245 int rc; 2246 2247 if (color) 2248 color_start(color); 2249 2250 va_start(aptr, format); 2251 rc = vprintf(format, aptr); 2252 va_end(aptr); 2253 2254 if (color) 2255 color_end(); 2256 2257 return (rc); 2258 } 2259 2260 /* PATH + 5 env vars + a NULL entry = 7 */ 2261 #define ZPOOL_VDEV_SCRIPT_ENV_COUNT 7 2262 2263 /* 2264 * There's a few places where ZFS will call external scripts (like the script 2265 * in zpool.d/ and `zfs_prepare_disk`). These scripts are called with a 2266 * reduced $PATH, and some vdev specific environment vars set. This function 2267 * will allocate an populate the environment variable array that is passed to 2268 * these scripts. The user must free the arrays with zpool_vdev_free_env() when 2269 * they are done. 2270 * 2271 * The following env vars will be set (but value could be blank): 2272 * 2273 * POOL_NAME 2274 * VDEV_PATH 2275 * VDEV_UPATH 2276 * VDEV_ENC_SYSFS_PATH 2277 * 2278 * In addition, you can set an optional environment variable named 'opt_key' 2279 * to 'opt_val' if you want. 2280 * 2281 * Returns allocated env[] array on success, NULL otherwise. 2282 */ 2283 char ** 2284 zpool_vdev_script_alloc_env(const char *pool_name, 2285 const char *vdev_path, const char *vdev_upath, 2286 const char *vdev_enc_sysfs_path, const char *opt_key, const char *opt_val) 2287 { 2288 char **env = NULL; 2289 int rc; 2290 2291 env = calloc(ZPOOL_VDEV_SCRIPT_ENV_COUNT, sizeof (*env)); 2292 if (!env) 2293 return (NULL); 2294 2295 env[0] = strdup("PATH=/bin:/sbin:/usr/bin:/usr/sbin"); 2296 if (!env[0]) 2297 goto error; 2298 2299 /* Setup our custom environment variables */ 2300 rc = asprintf(&env[1], "POOL_NAME=%s", pool_name ? pool_name : ""); 2301 if (rc == -1) { 2302 env[1] = NULL; 2303 goto error; 2304 } 2305 2306 rc = asprintf(&env[2], "VDEV_PATH=%s", vdev_path ? vdev_path : ""); 2307 if (rc == -1) { 2308 env[2] = NULL; 2309 goto error; 2310 } 2311 2312 rc = asprintf(&env[3], "VDEV_UPATH=%s", vdev_upath ? vdev_upath : ""); 2313 if (rc == -1) { 2314 env[3] = NULL; 2315 goto error; 2316 } 2317 2318 rc = asprintf(&env[4], "VDEV_ENC_SYSFS_PATH=%s", 2319 vdev_enc_sysfs_path ? vdev_enc_sysfs_path : ""); 2320 if (rc == -1) { 2321 env[4] = NULL; 2322 goto error; 2323 } 2324 2325 if (opt_key != NULL) { 2326 rc = asprintf(&env[5], "%s=%s", opt_key, 2327 opt_val ? opt_val : ""); 2328 if (rc == -1) { 2329 env[5] = NULL; 2330 goto error; 2331 } 2332 } 2333 2334 return (env); 2335 2336 error: 2337 for (int i = 0; i < ZPOOL_VDEV_SCRIPT_ENV_COUNT; i++) 2338 free(env[i]); 2339 2340 free(env); 2341 2342 return (NULL); 2343 } 2344 2345 /* 2346 * Free the env[] array that was allocated by zpool_vdev_script_alloc_env(). 2347 */ 2348 void 2349 zpool_vdev_script_free_env(char **env) 2350 { 2351 for (int i = 0; i < ZPOOL_VDEV_SCRIPT_ENV_COUNT; i++) 2352 free(env[i]); 2353 2354 free(env); 2355 } 2356 2357 /* 2358 * Prepare a disk by (optionally) running a program before labeling the disk. 2359 * This can be useful for installing disk firmware or doing some pre-flight 2360 * checks on the disk before it becomes part of the pool. The program run is 2361 * located at ZFSEXECDIR/zfs_prepare_disk 2362 * (E.x: /usr/local/libexec/zfs/zfs_prepare_disk). 2363 * 2364 * Return 0 on success, non-zero on failure. 2365 */ 2366 int 2367 zpool_prepare_disk(zpool_handle_t *zhp, nvlist_t *vdev_nv, 2368 const char *prepare_str, char **lines[], int *lines_cnt) 2369 { 2370 const char *script_path = ZFSEXECDIR "/zfs_prepare_disk"; 2371 const char *pool_name; 2372 int rc = 0; 2373 2374 /* Path to script and a NULL entry */ 2375 char *argv[2] = {(char *)script_path}; 2376 char **env = NULL; 2377 const char *path = NULL, *enc_sysfs_path = NULL; 2378 char *upath; 2379 *lines_cnt = 0; 2380 2381 if (access(script_path, X_OK) != 0) { 2382 /* No script, nothing to do */ 2383 return (0); 2384 } 2385 2386 (void) nvlist_lookup_string(vdev_nv, ZPOOL_CONFIG_PATH, &path); 2387 (void) nvlist_lookup_string(vdev_nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH, 2388 &enc_sysfs_path); 2389 2390 upath = zfs_get_underlying_path(path); 2391 pool_name = zhp ? zpool_get_name(zhp) : NULL; 2392 2393 env = zpool_vdev_script_alloc_env(pool_name, path, upath, 2394 enc_sysfs_path, "VDEV_PREPARE", prepare_str); 2395 2396 free(upath); 2397 2398 if (env == NULL) { 2399 return (ENOMEM); 2400 } 2401 2402 rc = libzfs_run_process_get_stdout(script_path, argv, env, lines, 2403 lines_cnt); 2404 2405 zpool_vdev_script_free_env(env); 2406 2407 return (rc); 2408 } 2409 2410 /* 2411 * Optionally run a script and then label a disk. The script can be used to 2412 * prepare a disk for inclusion into the pool. For example, it might update 2413 * the disk's firmware or check its health. 2414 * 2415 * The 'name' provided is the short name, stripped of any leading 2416 * /dev path, and is passed to zpool_label_disk. vdev_nv is the nvlist for 2417 * the vdev. prepare_str is a string that gets passed as the VDEV_PREPARE 2418 * env variable to the script. 2419 * 2420 * The following env vars are passed to the script: 2421 * 2422 * POOL_NAME: The pool name (blank during zpool create) 2423 * VDEV_PREPARE: Reason why the disk is being prepared for inclusion: 2424 * "create", "add", "replace", or "autoreplace" 2425 * VDEV_PATH: Path to the disk 2426 * VDEV_UPATH: One of the 'underlying paths' to the disk. This is 2427 * useful for DM devices. 2428 * VDEV_ENC_SYSFS_PATH: Path to the disk's enclosure sysfs path, if available. 2429 * 2430 * Note, some of these values can be blank. 2431 * 2432 * Return 0 on success, non-zero otherwise. 2433 */ 2434 int 2435 zpool_prepare_and_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, 2436 const char *name, nvlist_t *vdev_nv, const char *prepare_str, 2437 char **lines[], int *lines_cnt) 2438 { 2439 int rc; 2440 char vdev_path[MAXPATHLEN]; 2441 (void) snprintf(vdev_path, sizeof (vdev_path), "%s/%s", DISK_ROOT, 2442 name); 2443 2444 /* zhp will be NULL when creating a pool */ 2445 rc = zpool_prepare_disk(zhp, vdev_nv, prepare_str, lines, lines_cnt); 2446 if (rc != 0) 2447 return (rc); 2448 2449 rc = zpool_label_disk(hdl, zhp, name); 2450 return (rc); 2451 } 2452