1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <assert.h> 30 #include <ctype.h> 31 #include <errno.h> 32 #include <libdevinfo.h> 33 #include <libintl.h> 34 #include <math.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <strings.h> 38 #include <unistd.h> 39 #include <zone.h> 40 #include <fcntl.h> 41 #include <sys/mntent.h> 42 #include <sys/mnttab.h> 43 #include <sys/mount.h> 44 #include <sys/avl.h> 45 #include <priv.h> 46 #include <pwd.h> 47 #include <grp.h> 48 #include <stddef.h> 49 #include <ucred.h> 50 51 #include <sys/spa.h> 52 #include <sys/zio.h> 53 #include <sys/zap.h> 54 #include <libzfs.h> 55 56 #include "zfs_namecheck.h" 57 #include "zfs_prop.h" 58 #include "libzfs_impl.h" 59 #include "zfs_deleg.h" 60 61 static int create_parents(libzfs_handle_t *, char *, int); 62 static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 63 64 /* 65 * Given a single type (not a mask of types), return the type in a human 66 * readable form. 67 */ 68 const char * 69 zfs_type_to_name(zfs_type_t type) 70 { 71 switch (type) { 72 case ZFS_TYPE_FILESYSTEM: 73 return (dgettext(TEXT_DOMAIN, "filesystem")); 74 case ZFS_TYPE_SNAPSHOT: 75 return (dgettext(TEXT_DOMAIN, "snapshot")); 76 case ZFS_TYPE_VOLUME: 77 return (dgettext(TEXT_DOMAIN, "volume")); 78 } 79 80 return (NULL); 81 } 82 83 /* 84 * Given a path and mask of ZFS types, return a string describing this dataset. 85 * This is used when we fail to open a dataset and we cannot get an exact type. 86 * We guess what the type would have been based on the path and the mask of 87 * acceptable types. 88 */ 89 static const char * 90 path_to_str(const char *path, int types) 91 { 92 /* 93 * When given a single type, always report the exact type. 94 */ 95 if (types == ZFS_TYPE_SNAPSHOT) 96 return (dgettext(TEXT_DOMAIN, "snapshot")); 97 if (types == ZFS_TYPE_FILESYSTEM) 98 return (dgettext(TEXT_DOMAIN, "filesystem")); 99 if (types == ZFS_TYPE_VOLUME) 100 return (dgettext(TEXT_DOMAIN, "volume")); 101 102 /* 103 * The user is requesting more than one type of dataset. If this is the 104 * case, consult the path itself. If we're looking for a snapshot, and 105 * a '@' is found, then report it as "snapshot". Otherwise, remove the 106 * snapshot attribute and try again. 107 */ 108 if (types & ZFS_TYPE_SNAPSHOT) { 109 if (strchr(path, '@') != NULL) 110 return (dgettext(TEXT_DOMAIN, "snapshot")); 111 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 112 } 113 114 115 /* 116 * The user has requested either filesystems or volumes. 117 * We have no way of knowing a priori what type this would be, so always 118 * report it as "filesystem" or "volume", our two primitive types. 119 */ 120 if (types & ZFS_TYPE_FILESYSTEM) 121 return (dgettext(TEXT_DOMAIN, "filesystem")); 122 123 assert(types & ZFS_TYPE_VOLUME); 124 return (dgettext(TEXT_DOMAIN, "volume")); 125 } 126 127 /* 128 * Validate a ZFS path. This is used even before trying to open the dataset, to 129 * provide a more meaningful error message. We place a more useful message in 130 * 'buf' detailing exactly why the name was not valid. 131 */ 132 static int 133 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type) 134 { 135 namecheck_err_t why; 136 char what; 137 138 if (dataset_namecheck(path, &why, &what) != 0) { 139 if (hdl != NULL) { 140 switch (why) { 141 case NAME_ERR_TOOLONG: 142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 143 "name is too long")); 144 break; 145 146 case NAME_ERR_LEADING_SLASH: 147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 148 "leading slash in name")); 149 break; 150 151 case NAME_ERR_EMPTY_COMPONENT: 152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 153 "empty component in name")); 154 break; 155 156 case NAME_ERR_TRAILING_SLASH: 157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 158 "trailing slash in name")); 159 break; 160 161 case NAME_ERR_INVALCHAR: 162 zfs_error_aux(hdl, 163 dgettext(TEXT_DOMAIN, "invalid character " 164 "'%c' in name"), what); 165 break; 166 167 case NAME_ERR_MULTIPLE_AT: 168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 169 "multiple '@' delimiters in name")); 170 break; 171 172 case NAME_ERR_NOLETTER: 173 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 174 "pool doesn't begin with a letter")); 175 break; 176 177 case NAME_ERR_RESERVED: 178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 179 "name is reserved")); 180 break; 181 182 case NAME_ERR_DISKLIKE: 183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 184 "reserved disk name")); 185 break; 186 } 187 } 188 189 return (0); 190 } 191 192 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 193 if (hdl != NULL) 194 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 195 "snapshot delimiter '@' in filesystem name")); 196 return (0); 197 } 198 199 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 200 if (hdl != NULL) 201 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 202 "missing '@' delimiter in snapshot name")); 203 return (0); 204 } 205 206 return (-1); 207 } 208 209 int 210 zfs_name_valid(const char *name, zfs_type_t type) 211 { 212 return (zfs_validate_name(NULL, name, type)); 213 } 214 215 /* 216 * This function takes the raw DSL properties, and filters out the user-defined 217 * properties into a separate nvlist. 218 */ 219 static nvlist_t * 220 process_user_props(zfs_handle_t *zhp, nvlist_t *props) 221 { 222 libzfs_handle_t *hdl = zhp->zfs_hdl; 223 nvpair_t *elem; 224 nvlist_t *propval; 225 nvlist_t *nvl; 226 227 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 228 (void) no_memory(hdl); 229 return (NULL); 230 } 231 232 elem = NULL; 233 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 234 if (!zfs_prop_user(nvpair_name(elem))) 235 continue; 236 237 verify(nvpair_value_nvlist(elem, &propval) == 0); 238 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 239 nvlist_free(nvl); 240 (void) no_memory(hdl); 241 return (NULL); 242 } 243 } 244 245 return (nvl); 246 } 247 248 /* 249 * Utility function to gather stats (objset and zpl) for the given object. 250 */ 251 static int 252 get_stats(zfs_handle_t *zhp) 253 { 254 zfs_cmd_t zc = { 0 }; 255 libzfs_handle_t *hdl = zhp->zfs_hdl; 256 nvlist_t *allprops, *userprops; 257 258 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 259 260 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 261 return (-1); 262 263 while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 264 if (errno == ENOMEM) { 265 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 266 zcmd_free_nvlists(&zc); 267 return (-1); 268 } 269 } else { 270 zcmd_free_nvlists(&zc); 271 return (-1); 272 } 273 } 274 275 zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 276 277 (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 278 279 if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 280 zcmd_free_nvlists(&zc); 281 return (-1); 282 } 283 284 zcmd_free_nvlists(&zc); 285 286 if ((userprops = process_user_props(zhp, allprops)) == NULL) { 287 nvlist_free(allprops); 288 return (-1); 289 } 290 291 nvlist_free(zhp->zfs_props); 292 nvlist_free(zhp->zfs_user_props); 293 294 zhp->zfs_props = allprops; 295 zhp->zfs_user_props = userprops; 296 297 return (0); 298 } 299 300 /* 301 * Refresh the properties currently stored in the handle. 302 */ 303 void 304 zfs_refresh_properties(zfs_handle_t *zhp) 305 { 306 (void) get_stats(zhp); 307 } 308 309 /* 310 * Makes a handle from the given dataset name. Used by zfs_open() and 311 * zfs_iter_* to create child handles on the fly. 312 */ 313 zfs_handle_t * 314 make_dataset_handle(libzfs_handle_t *hdl, const char *path) 315 { 316 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 317 char *logstr; 318 319 if (zhp == NULL) 320 return (NULL); 321 322 zhp->zfs_hdl = hdl; 323 324 /* 325 * Preserve history log string. 326 * any changes performed here will be 327 * logged as an internal event. 328 */ 329 logstr = zhp->zfs_hdl->libzfs_log_str; 330 zhp->zfs_hdl->libzfs_log_str = NULL; 331 top: 332 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 333 334 if (get_stats(zhp) != 0) { 335 zhp->zfs_hdl->libzfs_log_str = logstr; 336 free(zhp); 337 return (NULL); 338 } 339 340 if (zhp->zfs_dmustats.dds_inconsistent) { 341 zfs_cmd_t zc = { 0 }; 342 343 /* 344 * If it is dds_inconsistent, then we've caught it in 345 * the middle of a 'zfs receive' or 'zfs destroy', and 346 * it is inconsistent from the ZPL's point of view, so 347 * can't be mounted. However, it could also be that we 348 * have crashed in the middle of one of those 349 * operations, in which case we need to get rid of the 350 * inconsistent state. We do that by either rolling 351 * back to the previous snapshot (which will fail if 352 * there is none), or destroying the filesystem. Note 353 * that if we are still in the middle of an active 354 * 'receive' or 'destroy', then the rollback and destroy 355 * will fail with EBUSY and we will drive on as usual. 356 */ 357 358 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 359 360 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 361 (void) zvol_remove_link(hdl, zhp->zfs_name); 362 zc.zc_objset_type = DMU_OST_ZVOL; 363 } else { 364 zc.zc_objset_type = DMU_OST_ZFS; 365 } 366 367 /* If we can successfully roll it back, reget the stats */ 368 if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 369 goto top; 370 /* 371 * If we can sucessfully destroy it, pretend that it 372 * never existed. 373 */ 374 if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 375 zhp->zfs_hdl->libzfs_log_str = logstr; 376 free(zhp); 377 errno = ENOENT; 378 return (NULL); 379 } 380 } 381 382 /* 383 * We've managed to open the dataset and gather statistics. Determine 384 * the high-level type. 385 */ 386 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 387 zhp->zfs_head_type = ZFS_TYPE_VOLUME; 388 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 389 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 390 else 391 abort(); 392 393 if (zhp->zfs_dmustats.dds_is_snapshot) 394 zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 395 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 396 zhp->zfs_type = ZFS_TYPE_VOLUME; 397 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 398 zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 399 else 400 abort(); /* we should never see any other types */ 401 402 zhp->zfs_hdl->libzfs_log_str = logstr; 403 return (zhp); 404 } 405 406 /* 407 * Opens the given snapshot, filesystem, or volume. The 'types' 408 * argument is a mask of acceptable types. The function will print an 409 * appropriate error message and return NULL if it can't be opened. 410 */ 411 zfs_handle_t * 412 zfs_open(libzfs_handle_t *hdl, const char *path, int types) 413 { 414 zfs_handle_t *zhp; 415 char errbuf[1024]; 416 417 (void) snprintf(errbuf, sizeof (errbuf), 418 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 419 420 /* 421 * Validate the name before we even try to open it. 422 */ 423 if (!zfs_validate_name(hdl, path, ZFS_TYPE_ANY)) { 424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 425 "invalid dataset name")); 426 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 427 return (NULL); 428 } 429 430 /* 431 * Try to get stats for the dataset, which will tell us if it exists. 432 */ 433 errno = 0; 434 if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 435 (void) zfs_standard_error(hdl, errno, errbuf); 436 return (NULL); 437 } 438 439 if (!(types & zhp->zfs_type)) { 440 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 441 zfs_close(zhp); 442 return (NULL); 443 } 444 445 return (zhp); 446 } 447 448 /* 449 * Release a ZFS handle. Nothing to do but free the associated memory. 450 */ 451 void 452 zfs_close(zfs_handle_t *zhp) 453 { 454 if (zhp->zfs_mntopts) 455 free(zhp->zfs_mntopts); 456 nvlist_free(zhp->zfs_props); 457 nvlist_free(zhp->zfs_user_props); 458 free(zhp); 459 } 460 461 /* 462 * Given a numeric suffix, convert the value into a number of bits that the 463 * resulting value must be shifted. 464 */ 465 static int 466 str2shift(libzfs_handle_t *hdl, const char *buf) 467 { 468 const char *ends = "BKMGTPEZ"; 469 int i; 470 471 if (buf[0] == '\0') 472 return (0); 473 for (i = 0; i < strlen(ends); i++) { 474 if (toupper(buf[0]) == ends[i]) 475 break; 476 } 477 if (i == strlen(ends)) { 478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 479 "invalid numeric suffix '%s'"), buf); 480 return (-1); 481 } 482 483 /* 484 * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 485 * allow 'BB' - that's just weird. 486 */ 487 if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 488 toupper(buf[0]) != 'B')) 489 return (10*i); 490 491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 492 "invalid numeric suffix '%s'"), buf); 493 return (-1); 494 } 495 496 /* 497 * Convert a string of the form '100G' into a real number. Used when setting 498 * properties or creating a volume. 'buf' is used to place an extended error 499 * message for the caller to use. 500 */ 501 static int 502 nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 503 { 504 char *end; 505 int shift; 506 507 *num = 0; 508 509 /* Check to see if this looks like a number. */ 510 if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 511 if (hdl) 512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 513 "bad numeric value '%s'"), value); 514 return (-1); 515 } 516 517 /* Rely on stroll() to process the numeric portion. */ 518 errno = 0; 519 *num = strtoll(value, &end, 10); 520 521 /* 522 * Check for ERANGE, which indicates that the value is too large to fit 523 * in a 64-bit value. 524 */ 525 if (errno == ERANGE) { 526 if (hdl) 527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 528 "numeric value is too large")); 529 return (-1); 530 } 531 532 /* 533 * If we have a decimal value, then do the computation with floating 534 * point arithmetic. Otherwise, use standard arithmetic. 535 */ 536 if (*end == '.') { 537 double fval = strtod(value, &end); 538 539 if ((shift = str2shift(hdl, end)) == -1) 540 return (-1); 541 542 fval *= pow(2, shift); 543 544 if (fval > UINT64_MAX) { 545 if (hdl) 546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 547 "numeric value is too large")); 548 return (-1); 549 } 550 551 *num = (uint64_t)fval; 552 } else { 553 if ((shift = str2shift(hdl, end)) == -1) 554 return (-1); 555 556 /* Check for overflow */ 557 if (shift >= 64 || (*num << shift) >> shift != *num) { 558 if (hdl) 559 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 560 "numeric value is too large")); 561 return (-1); 562 } 563 564 *num <<= shift; 565 } 566 567 return (0); 568 } 569 570 int 571 zfs_nicestrtonum(libzfs_handle_t *hdl, const char *str, uint64_t *val) 572 { 573 return (nicestrtonum(hdl, str, val)); 574 } 575 576 /* 577 * The prop_parse_*() functions are designed to allow flexibility in callers 578 * when setting properties. At the DSL layer, all properties are either 64-bit 579 * numbers or strings. We want the user to be able to ignore this fact and 580 * specify properties as native values (boolean, for example) or as strings (to 581 * simplify command line utilities). This also handles converting index types 582 * (compression, checksum, etc) from strings to their on-disk index. 583 */ 584 585 static int 586 prop_parse_boolean(libzfs_handle_t *hdl, nvpair_t *elem, uint64_t *val) 587 { 588 uint64_t ret; 589 590 switch (nvpair_type(elem)) { 591 case DATA_TYPE_STRING: 592 { 593 char *value; 594 verify(nvpair_value_string(elem, &value) == 0); 595 596 if (strcmp(value, "on") == 0) { 597 ret = 1; 598 } else if (strcmp(value, "off") == 0) { 599 ret = 0; 600 } else { 601 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 602 "property '%s' must be 'on' or 'off'"), 603 nvpair_name(elem)); 604 return (-1); 605 } 606 break; 607 } 608 609 case DATA_TYPE_UINT64: 610 { 611 verify(nvpair_value_uint64(elem, &ret) == 0); 612 if (ret > 1) { 613 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 614 "'%s' must be a boolean value"), 615 nvpair_name(elem)); 616 return (-1); 617 } 618 break; 619 } 620 621 case DATA_TYPE_BOOLEAN_VALUE: 622 { 623 boolean_t value; 624 verify(nvpair_value_boolean_value(elem, &value) == 0); 625 ret = value; 626 break; 627 } 628 629 default: 630 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 631 "'%s' must be a boolean value"), 632 nvpair_name(elem)); 633 return (-1); 634 } 635 636 *val = ret; 637 return (0); 638 } 639 640 static int 641 prop_parse_number(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop, 642 uint64_t *val) 643 { 644 uint64_t ret; 645 boolean_t isnone = B_FALSE; 646 647 switch (nvpair_type(elem)) { 648 case DATA_TYPE_STRING: 649 { 650 char *value; 651 (void) nvpair_value_string(elem, &value); 652 if (strcmp(value, "none") == 0) { 653 isnone = B_TRUE; 654 ret = 0; 655 } else if (nicestrtonum(hdl, value, &ret) != 0) { 656 return (-1); 657 } 658 break; 659 } 660 661 case DATA_TYPE_UINT64: 662 (void) nvpair_value_uint64(elem, &ret); 663 break; 664 665 default: 666 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 667 "'%s' must be a number"), 668 nvpair_name(elem)); 669 return (-1); 670 } 671 672 /* 673 * Quota special: force 'none' and don't allow 0. 674 */ 675 if (ret == 0 && !isnone && prop == ZFS_PROP_QUOTA) { 676 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 677 "use 'none' to disable quota")); 678 return (-1); 679 } 680 681 *val = ret; 682 return (0); 683 } 684 685 static int 686 prop_parse_index(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop, 687 uint64_t *val) 688 { 689 char *propname = nvpair_name(elem); 690 char *value; 691 692 if (nvpair_type(elem) != DATA_TYPE_STRING) { 693 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 694 "'%s' must be a string"), propname); 695 return (-1); 696 } 697 698 (void) nvpair_value_string(elem, &value); 699 700 if (zfs_prop_string_to_index(prop, value, val) != 0) { 701 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 702 "'%s' must be one of '%s'"), propname, 703 zfs_prop_values(prop)); 704 return (-1); 705 } 706 707 return (0); 708 } 709 710 /* 711 * Check if the bootfs name has the same pool name as it is set to. 712 * Assuming bootfs is a valid dataset name. 713 */ 714 static boolean_t 715 bootfs_poolname_valid(char *pool, char *bootfs) 716 { 717 char ch, *pname; 718 719 /* get the pool name from the bootfs name */ 720 pname = bootfs; 721 while (*bootfs && !isspace(*bootfs) && *bootfs != '/') 722 bootfs++; 723 724 ch = *bootfs; 725 *bootfs = 0; 726 727 if (strcmp(pool, pname) == 0) { 728 *bootfs = ch; 729 return (B_TRUE); 730 } 731 732 *bootfs = ch; 733 return (B_FALSE); 734 } 735 736 /* 737 * Given an nvlist of properties to set, validates that they are correct, and 738 * parses any numeric properties (index, boolean, etc) if they are specified as 739 * strings. 740 */ 741 nvlist_t * 742 zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, char *pool_name, 743 nvlist_t *nvl, uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 744 { 745 nvpair_t *elem; 746 const char *propname; 747 zfs_prop_t prop; 748 uint64_t intval; 749 char *strval; 750 nvlist_t *ret; 751 int isuser; 752 753 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 754 (void) no_memory(hdl); 755 return (NULL); 756 } 757 758 if (type == ZFS_TYPE_SNAPSHOT) { 759 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 760 "snapshot properties cannot be modified")); 761 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 762 goto error; 763 } 764 765 elem = NULL; 766 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 767 propname = nvpair_name(elem); 768 769 /* 770 * Make sure this property is valid and applies to this type. 771 */ 772 if ((prop = zfs_name_to_prop_common(propname, type)) 773 == ZFS_PROP_INVAL) { 774 isuser = zfs_prop_user(propname); 775 if (!isuser || (isuser && (type & ZFS_TYPE_POOL))) { 776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 777 "invalid property '%s'"), 778 propname); 779 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 780 goto error; 781 } else { 782 /* 783 * If this is a user property, make sure it's a 784 * string, and that it's less than 785 * ZAP_MAXNAMELEN. 786 */ 787 if (nvpair_type(elem) != DATA_TYPE_STRING) { 788 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 789 "'%s' must be a string"), 790 propname); 791 (void) zfs_error(hdl, EZFS_BADPROP, 792 errbuf); 793 goto error; 794 } 795 796 if (strlen(nvpair_name(elem)) >= 797 ZAP_MAXNAMELEN) { 798 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 799 "property name '%s' is too long"), 800 propname); 801 (void) zfs_error(hdl, EZFS_BADPROP, 802 errbuf); 803 goto error; 804 } 805 } 806 807 (void) nvpair_value_string(elem, &strval); 808 if (nvlist_add_string(ret, propname, strval) != 0) { 809 (void) no_memory(hdl); 810 goto error; 811 } 812 continue; 813 } 814 815 /* 816 * Normalize the name, to get rid of shorthand abbrevations. 817 */ 818 propname = zfs_prop_to_name(prop); 819 820 if (!zfs_prop_valid_for_type(prop, type)) { 821 zfs_error_aux(hdl, 822 dgettext(TEXT_DOMAIN, "'%s' does not " 823 "apply to datasets of this type"), propname); 824 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 825 goto error; 826 } 827 828 if (zfs_prop_readonly(prop) && 829 (prop != ZFS_PROP_VOLBLOCKSIZE || zhp != NULL)) { 830 zfs_error_aux(hdl, 831 dgettext(TEXT_DOMAIN, "'%s' is readonly"), 832 propname); 833 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 834 goto error; 835 } 836 837 /* 838 * Convert any properties to the internal DSL value types. 839 */ 840 strval = NULL; 841 switch (zfs_prop_get_type(prop)) { 842 case prop_type_boolean: 843 if (prop_parse_boolean(hdl, elem, &intval) != 0) { 844 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 845 goto error; 846 } 847 break; 848 849 case prop_type_string: 850 if (nvpair_type(elem) != DATA_TYPE_STRING) { 851 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 852 "'%s' must be a string"), 853 propname); 854 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 855 goto error; 856 } 857 (void) nvpair_value_string(elem, &strval); 858 if (strlen(strval) >= ZFS_MAXPROPLEN) { 859 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 860 "'%s' is too long"), propname); 861 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 862 goto error; 863 } 864 break; 865 866 case prop_type_number: 867 if (prop_parse_number(hdl, elem, prop, &intval) != 0) { 868 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 869 goto error; 870 } 871 break; 872 873 case prop_type_index: 874 if (prop_parse_index(hdl, elem, prop, &intval) != 0) { 875 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 876 goto error; 877 } 878 break; 879 880 default: 881 abort(); 882 } 883 884 /* 885 * Add the result to our return set of properties. 886 */ 887 if (strval) { 888 if (nvlist_add_string(ret, propname, strval) != 0) { 889 (void) no_memory(hdl); 890 goto error; 891 } 892 } else if (nvlist_add_uint64(ret, propname, intval) != 0) { 893 (void) no_memory(hdl); 894 goto error; 895 } 896 897 /* 898 * Perform some additional checks for specific properties. 899 */ 900 switch (prop) { 901 case ZFS_PROP_VERSION: 902 { 903 int version; 904 905 if (zhp == NULL) 906 break; 907 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 908 if (intval < version) { 909 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 910 "Can not downgrade; already at version %u"), 911 version); 912 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 913 goto error; 914 } 915 break; 916 } 917 918 case ZFS_PROP_RECORDSIZE: 919 case ZFS_PROP_VOLBLOCKSIZE: 920 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 921 if (intval < SPA_MINBLOCKSIZE || 922 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 923 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 924 "'%s' must be power of 2 from %u " 925 "to %uk"), propname, 926 (uint_t)SPA_MINBLOCKSIZE, 927 (uint_t)SPA_MAXBLOCKSIZE >> 10); 928 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 929 goto error; 930 } 931 break; 932 933 case ZFS_PROP_SHAREISCSI: 934 if (strcmp(strval, "off") != 0 && 935 strcmp(strval, "on") != 0 && 936 strcmp(strval, "type=disk") != 0) { 937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 938 "'%s' must be 'on', 'off', or 'type=disk'"), 939 propname); 940 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 941 goto error; 942 } 943 944 break; 945 946 case ZFS_PROP_MOUNTPOINT: 947 { 948 namecheck_err_t why; 949 950 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 951 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 952 break; 953 954 if (mountpoint_namecheck(strval, &why)) { 955 switch (why) { 956 case NAME_ERR_LEADING_SLASH: 957 zfs_error_aux(hdl, 958 dgettext(TEXT_DOMAIN, 959 "'%s' must be an absolute path, " 960 "'none', or 'legacy'"), propname); 961 break; 962 case NAME_ERR_TOOLONG: 963 zfs_error_aux(hdl, 964 dgettext(TEXT_DOMAIN, 965 "component of '%s' is too long"), 966 propname); 967 break; 968 } 969 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 970 goto error; 971 } 972 } 973 974 /*FALLTHRU*/ 975 976 case ZFS_PROP_SHARENFS: 977 /* 978 * For the mountpoint and sharenfs properties, check if 979 * it can be set in a global/non-global zone based on 980 * the zoned property value: 981 * 982 * global zone non-global zone 983 * -------------------------------------------------- 984 * zoned=on mountpoint (no) mountpoint (yes) 985 * sharenfs (no) sharenfs (no) 986 * 987 * zoned=off mountpoint (yes) N/A 988 * sharenfs (yes) 989 */ 990 if (zoned) { 991 if (getzoneid() == GLOBAL_ZONEID) { 992 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 993 "'%s' cannot be set on " 994 "dataset in a non-global zone"), 995 propname); 996 (void) zfs_error(hdl, EZFS_ZONED, 997 errbuf); 998 goto error; 999 } else if (prop == ZFS_PROP_SHARENFS) { 1000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1001 "'%s' cannot be set in " 1002 "a non-global zone"), propname); 1003 (void) zfs_error(hdl, EZFS_ZONED, 1004 errbuf); 1005 goto error; 1006 } 1007 } else if (getzoneid() != GLOBAL_ZONEID) { 1008 /* 1009 * If zoned property is 'off', this must be in 1010 * a globle zone. If not, something is wrong. 1011 */ 1012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1013 "'%s' cannot be set while dataset " 1014 "'zoned' property is set"), propname); 1015 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 1016 goto error; 1017 } 1018 1019 /* 1020 * At this point, it is legitimate to set the 1021 * property. Now we want to make sure that the 1022 * property value is valid if it is sharenfs. 1023 */ 1024 if (prop == ZFS_PROP_SHARENFS && 1025 strcmp(strval, "on") != 0 && 1026 strcmp(strval, "off") != 0) { 1027 1028 /* 1029 * Must be an NFS option string so 1030 * init the libshare in order to 1031 * enable the parser and then parse 1032 * the options. We use the control API 1033 * since we don't care about the 1034 * current configuration and don't 1035 * want the overhead of loading it 1036 * until we actually do something. 1037 */ 1038 1039 if (zfs_init_libshare(hdl, 1040 SA_INIT_CONTROL_API) != SA_OK) { 1041 /* 1042 * An error occurred so we can't do 1043 * anything 1044 */ 1045 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1046 "'%s' cannot be set: problem " 1047 "in share initialization"), 1048 propname); 1049 (void) zfs_error(hdl, EZFS_BADPROP, 1050 errbuf); 1051 goto error; 1052 } 1053 1054 if (zfs_parse_options(strval, "nfs") != SA_OK) { 1055 /* 1056 * There was an error in parsing so 1057 * deal with it by issuing an error 1058 * message and leaving after 1059 * uninitializing the the libshare 1060 * interface. 1061 */ 1062 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1063 "'%s' cannot be set to invalid " 1064 "options"), propname); 1065 (void) zfs_error(hdl, EZFS_BADPROP, 1066 errbuf); 1067 zfs_uninit_libshare(hdl); 1068 goto error; 1069 } 1070 zfs_uninit_libshare(hdl); 1071 } 1072 1073 break; 1074 1075 case ZPOOL_PROP_BOOTFS: 1076 /* 1077 * bootfs property value has to be a dataset name and 1078 * the dataset has to be in the same pool as it sets to. 1079 */ 1080 if (strval[0] != '\0' && (!zfs_name_valid(strval, 1081 ZFS_TYPE_FILESYSTEM) || !bootfs_poolname_valid( 1082 pool_name, strval))) { 1083 1084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 1085 "is an invalid name"), strval); 1086 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 1087 goto error; 1088 } 1089 break; 1090 } 1091 1092 /* 1093 * For changes to existing volumes, we have some additional 1094 * checks to enforce. 1095 */ 1096 if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1097 uint64_t volsize = zfs_prop_get_int(zhp, 1098 ZFS_PROP_VOLSIZE); 1099 uint64_t blocksize = zfs_prop_get_int(zhp, 1100 ZFS_PROP_VOLBLOCKSIZE); 1101 char buf[64]; 1102 1103 switch (prop) { 1104 case ZFS_PROP_RESERVATION: 1105 if (intval > volsize) { 1106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1107 "'%s' is greater than current " 1108 "volume size"), propname); 1109 (void) zfs_error(hdl, EZFS_BADPROP, 1110 errbuf); 1111 goto error; 1112 } 1113 break; 1114 1115 case ZFS_PROP_VOLSIZE: 1116 if (intval % blocksize != 0) { 1117 zfs_nicenum(blocksize, buf, 1118 sizeof (buf)); 1119 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1120 "'%s' must be a multiple of " 1121 "volume block size (%s)"), 1122 propname, buf); 1123 (void) zfs_error(hdl, EZFS_BADPROP, 1124 errbuf); 1125 goto error; 1126 } 1127 1128 if (intval == 0) { 1129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1130 "'%s' cannot be zero"), 1131 propname); 1132 (void) zfs_error(hdl, EZFS_BADPROP, 1133 errbuf); 1134 goto error; 1135 } 1136 break; 1137 } 1138 } 1139 } 1140 1141 /* 1142 * If this is an existing volume, and someone is setting the volsize, 1143 * make sure that it matches the reservation, or add it if necessary. 1144 */ 1145 if (zhp != NULL && type == ZFS_TYPE_VOLUME && 1146 nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1147 &intval) == 0) { 1148 uint64_t old_volsize = zfs_prop_get_int(zhp, 1149 ZFS_PROP_VOLSIZE); 1150 uint64_t old_reservation = zfs_prop_get_int(zhp, 1151 ZFS_PROP_RESERVATION); 1152 uint64_t new_reservation; 1153 1154 if (old_volsize == old_reservation && 1155 nvlist_lookup_uint64(ret, 1156 zfs_prop_to_name(ZFS_PROP_RESERVATION), 1157 &new_reservation) != 0) { 1158 if (nvlist_add_uint64(ret, 1159 zfs_prop_to_name(ZFS_PROP_RESERVATION), 1160 intval) != 0) { 1161 (void) no_memory(hdl); 1162 goto error; 1163 } 1164 } 1165 } 1166 1167 return (ret); 1168 1169 error: 1170 nvlist_free(ret); 1171 return (NULL); 1172 } 1173 1174 static int 1175 zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 1176 uint64_t *ret_who) 1177 { 1178 struct passwd *pwd; 1179 struct group *grp; 1180 uid_t id; 1181 1182 if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 1183 *who_type == ZFS_DELEG_NAMED_SET) { 1184 *ret_who = -1; 1185 return (0); 1186 } 1187 if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 1188 return (EZFS_BADWHO); 1189 1190 if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 1191 strcmp(who, "everyone") == 0) { 1192 *ret_who = -1; 1193 *who_type = ZFS_DELEG_EVERYONE; 1194 return (0); 1195 } 1196 1197 pwd = getpwnam(who); 1198 grp = getgrnam(who); 1199 1200 if ((*who_type == ZFS_DELEG_USER) && pwd) { 1201 *ret_who = pwd->pw_uid; 1202 } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 1203 *ret_who = grp->gr_gid; 1204 } else if (pwd) { 1205 *ret_who = pwd->pw_uid; 1206 *who_type = ZFS_DELEG_USER; 1207 } else if (grp) { 1208 *ret_who = grp->gr_gid; 1209 *who_type = ZFS_DELEG_GROUP; 1210 } else { 1211 char *end; 1212 1213 id = strtol(who, &end, 10); 1214 if (errno != 0 || *end != '\0') { 1215 return (EZFS_BADWHO); 1216 } else { 1217 *ret_who = id; 1218 if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 1219 *who_type = ZFS_DELEG_USER; 1220 } 1221 } 1222 1223 return (0); 1224 } 1225 1226 static void 1227 zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 1228 { 1229 if (perms_nvp != NULL) { 1230 verify(nvlist_add_nvlist(who_nvp, 1231 name, perms_nvp) == 0); 1232 } else { 1233 verify(nvlist_add_boolean(who_nvp, name) == 0); 1234 } 1235 } 1236 1237 static void 1238 helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 1239 zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 1240 nvlist_t *sets_nvp) 1241 { 1242 boolean_t do_perms, do_sets; 1243 char name[ZFS_MAX_DELEG_NAME]; 1244 1245 do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 1246 do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 1247 1248 if (!do_perms && !do_sets) 1249 do_perms = do_sets = B_TRUE; 1250 1251 if (do_perms) { 1252 zfs_deleg_whokey(name, who_type, inherit, 1253 (who_type == ZFS_DELEG_NAMED_SET) ? 1254 whostr : (void *)&whoid); 1255 zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 1256 } 1257 if (do_sets) { 1258 zfs_deleg_whokey(name, toupper(who_type), inherit, 1259 (who_type == ZFS_DELEG_NAMED_SET) ? 1260 whostr : (void *)&whoid); 1261 zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 1262 } 1263 } 1264 1265 static void 1266 zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 1267 nvlist_t *perms_nvp, nvlist_t *sets_nvp, 1268 zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 1269 { 1270 if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 1271 helper(who_type, whoid, whostr, 0, 1272 who_nvp, perms_nvp, sets_nvp); 1273 } else { 1274 if (inherit & ZFS_DELEG_PERM_LOCAL) { 1275 helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 1276 who_nvp, perms_nvp, sets_nvp); 1277 } 1278 if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 1279 helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 1280 who_nvp, perms_nvp, sets_nvp); 1281 } 1282 } 1283 } 1284 1285 /* 1286 * Construct nvlist to pass down to kernel for setting/removing permissions. 1287 * 1288 * The nvlist is constructed as a series of nvpairs with an optional embedded 1289 * nvlist of permissions to remove or set. The topmost nvpairs are the actual 1290 * base attribute named stored in the dsl. 1291 * Arguments: 1292 * 1293 * whostr: is a comma separated list of users, groups, or a single set name. 1294 * whostr may be null for everyone or create perms. 1295 * who_type: is the type of entry in whostr. Typically this will be 1296 * ZFS_DELEG_WHO_UNKNOWN. 1297 * perms: comman separated list of permissions. May be null if user 1298 * is requested to remove permissions by who. 1299 * inherit: Specifies the inheritance of the permissions. Will be either 1300 * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 1301 * nvp The constructed nvlist to pass to zfs_perm_set(). 1302 * The output nvp will look something like this. 1303 * ul$1234 -> {create ; destroy } 1304 * Ul$1234 -> { @myset } 1305 * s-$@myset - { snapshot; checksum; compression } 1306 */ 1307 int 1308 zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 1309 zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 1310 { 1311 nvlist_t *who_nvp; 1312 nvlist_t *perms_nvp = NULL; 1313 nvlist_t *sets_nvp = NULL; 1314 char errbuf[1024]; 1315 char *who_tok; 1316 int error; 1317 1318 *nvp = NULL; 1319 1320 if (perms) { 1321 /* Make sure permission string doesn't have an '=' sign in it */ 1322 if (strchr(perms, '=') != NULL) { 1323 (void) snprintf(errbuf, sizeof (errbuf), 1324 dgettext(TEXT_DOMAIN, 1325 "permissions can't contain equal sign : '%s'"), 1326 perms); 1327 return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, errbuf)); 1328 } 1329 1330 if ((error = nvlist_alloc(&perms_nvp, 1331 NV_UNIQUE_NAME, 0)) != 0) { 1332 return (1); 1333 } 1334 if ((error = nvlist_alloc(&sets_nvp, 1335 NV_UNIQUE_NAME, 0)) != 0) { 1336 nvlist_free(perms_nvp); 1337 return (1); 1338 } 1339 } 1340 1341 if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 1342 if (perms_nvp) 1343 nvlist_free(perms_nvp); 1344 if (sets_nvp) 1345 nvlist_free(sets_nvp); 1346 return (1); 1347 } 1348 1349 if (who_type == ZFS_DELEG_NAMED_SET) { 1350 namecheck_err_t why; 1351 char what; 1352 1353 if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 1354 switch (why) { 1355 case NAME_ERR_NO_AT: 1356 zfs_error_aux(zhp->zfs_hdl, 1357 dgettext(TEXT_DOMAIN, 1358 "set definition must begin with an '@' " 1359 "character")); 1360 } 1361 return (zfs_error(zhp->zfs_hdl, 1362 EZFS_BADPERMSET, whostr)); 1363 } 1364 } 1365 1366 /* 1367 * Build up nvlist(s) of permissions. Two nvlists are maintained. 1368 * The first nvlist perms_nvp will have normal permissions and the 1369 * other sets_nvp will have only permssion set names in it. 1370 */ 1371 1372 1373 while (perms && *perms != '\0') { 1374 char *value; 1375 char *perm_name; 1376 nvlist_t *update_nvp; 1377 int perm_num; 1378 char canonical_name[64]; 1379 char *canonicalp = canonical_name; 1380 1381 1382 update_nvp = perms_nvp; 1383 1384 perm_num = getsubopt(&perms, zfs_deleg_perm_tab, &value); 1385 if (perm_num == -1) { 1386 zfs_prop_t prop; 1387 1388 prop = zfs_name_to_prop(value); 1389 if (prop != ZFS_PROP_INVAL) { 1390 (void) snprintf(canonical_name, 1391 sizeof (canonical_name), "%s", 1392 zfs_prop_to_name(prop)); 1393 perm_num = getsubopt(&canonicalp, 1394 zfs_deleg_perm_tab, &value); 1395 } 1396 } 1397 if (perm_num != -1) { 1398 perm_name = zfs_deleg_perm_tab[perm_num]; 1399 } else { /* check and see if permission is a named set */ 1400 if (value[0] == '@') { 1401 1402 /* 1403 * make sure permssion set isn't defined 1404 * in terms of itself. ie. 1405 * @set1 = create,destroy,@set1 1406 */ 1407 if (who_type == ZFS_DELEG_NAMED_SET && 1408 strcmp(value, whostr) == 0) { 1409 nvlist_free(who_nvp); 1410 nvlist_free(perms_nvp); 1411 if (sets_nvp) 1412 nvlist_free(sets_nvp); 1413 (void) snprintf(errbuf, 1414 sizeof (errbuf), 1415 dgettext(TEXT_DOMAIN, 1416 "Invalid permission %s"), value); 1417 return (zfs_error(zhp->zfs_hdl, 1418 EZFS_PERMSET_CIRCULAR, errbuf)); 1419 } 1420 update_nvp = sets_nvp; 1421 perm_name = value; 1422 } else { 1423 nvlist_free(who_nvp); 1424 nvlist_free(perms_nvp); 1425 if (sets_nvp) 1426 nvlist_free(sets_nvp); 1427 return (zfs_error(zhp->zfs_hdl, 1428 EZFS_BADPERM, value)); 1429 } 1430 } 1431 verify(nvlist_add_boolean(update_nvp, perm_name) == 0); 1432 } 1433 1434 if (whostr && who_type != ZFS_DELEG_CREATE) { 1435 who_tok = strtok(whostr, ","); 1436 if (who_tok == NULL) { 1437 nvlist_free(who_nvp); 1438 nvlist_free(perms_nvp); 1439 if (sets_nvp) 1440 nvlist_free(sets_nvp); 1441 (void) snprintf(errbuf, sizeof (errbuf), 1442 dgettext(TEXT_DOMAIN, "Who string is NULL"), 1443 whostr); 1444 return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 1445 } 1446 } 1447 1448 /* 1449 * Now create the nvlist(s) 1450 */ 1451 do { 1452 uint64_t who_id; 1453 1454 error = zfs_get_perm_who(who_tok, &who_type, 1455 &who_id); 1456 if (error) { 1457 nvlist_free(who_nvp); 1458 nvlist_free(perms_nvp); 1459 if (sets_nvp) 1460 nvlist_free(sets_nvp); 1461 (void) snprintf(errbuf, sizeof (errbuf), 1462 dgettext(TEXT_DOMAIN, 1463 "Unable to determine uid/gid for " 1464 "%s "), who_tok); 1465 return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 1466 } 1467 1468 /* 1469 * add entries for both local and descendent when required 1470 */ 1471 1472 zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 1473 perms_nvp, sets_nvp, who_type, inherit); 1474 1475 } while (who_tok = strtok(NULL, ",")); 1476 *nvp = who_nvp; 1477 return (0); 1478 } 1479 1480 static int 1481 zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 1482 { 1483 zfs_cmd_t zc = { 0 }; 1484 int error; 1485 size_t sz; 1486 char errbuf[1024]; 1487 1488 (void) snprintf(errbuf, sizeof (errbuf), 1489 dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 1490 zhp->zfs_name); 1491 1492 if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp, &sz)) 1493 return (-1); 1494 1495 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1496 zc.zc_perm_action = unset; 1497 1498 error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 1499 if (error && errno == ENOTSUP) { 1500 (void) snprintf(errbuf, sizeof (errbuf), 1501 gettext("Pool must be upgraded to use 'allow/unallow'")); 1502 zcmd_free_nvlists(&zc); 1503 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 1504 } else if (error) { 1505 return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 1506 } 1507 zcmd_free_nvlists(&zc); 1508 1509 return (error); 1510 } 1511 1512 int 1513 zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 1514 { 1515 return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 1516 } 1517 1518 int 1519 zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 1520 { 1521 return (zfs_perm_set_common(zhp, perms, B_TRUE)); 1522 } 1523 1524 static int 1525 perm_compare(const void *arg1, const void *arg2) 1526 { 1527 const zfs_perm_node_t *node1 = arg1; 1528 const zfs_perm_node_t *node2 = arg2; 1529 int ret; 1530 1531 ret = strcmp(node1->z_pname, node2->z_pname); 1532 1533 if (ret > 0) 1534 return (1); 1535 if (ret < 0) 1536 return (-1); 1537 else 1538 return (0); 1539 } 1540 1541 static void 1542 zfs_destroy_perm_tree(avl_tree_t *tree) 1543 { 1544 zfs_perm_node_t *permnode; 1545 void *cookie; 1546 1547 cookie = NULL; 1548 while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) { 1549 avl_remove(tree, permnode); 1550 free(permnode); 1551 } 1552 } 1553 1554 static void 1555 zfs_destroy_tree(avl_tree_t *tree) 1556 { 1557 zfs_allow_node_t *allownode; 1558 void *cookie; 1559 1560 cookie = NULL; 1561 while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 1562 zfs_destroy_perm_tree(&allownode->z_localdescend); 1563 zfs_destroy_perm_tree(&allownode->z_local); 1564 zfs_destroy_perm_tree(&allownode->z_descend); 1565 avl_remove(tree, allownode); 1566 free(allownode); 1567 } 1568 } 1569 1570 void 1571 zfs_free_allows(zfs_allow_t *allow) 1572 { 1573 zfs_allow_t *allownext; 1574 zfs_allow_t *freeallow; 1575 1576 allownext = allow; 1577 while (allownext) { 1578 zfs_destroy_tree(&allownext->z_sets); 1579 zfs_destroy_tree(&allownext->z_crperms); 1580 zfs_destroy_tree(&allownext->z_user); 1581 zfs_destroy_tree(&allownext->z_group); 1582 zfs_destroy_tree(&allownext->z_everyone); 1583 freeallow = allownext; 1584 allownext = allownext->z_next; 1585 free(freeallow); 1586 } 1587 } 1588 1589 static zfs_allow_t * 1590 zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 1591 { 1592 zfs_allow_t *ptree; 1593 1594 if ((ptree = zfs_alloc(zhp->zfs_hdl, 1595 sizeof (zfs_allow_t))) == NULL) { 1596 return (NULL); 1597 } 1598 1599 (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 1600 avl_create(&ptree->z_sets, 1601 perm_compare, sizeof (zfs_allow_node_t), 1602 offsetof(zfs_allow_node_t, z_node)); 1603 avl_create(&ptree->z_crperms, 1604 perm_compare, sizeof (zfs_allow_node_t), 1605 offsetof(zfs_allow_node_t, z_node)); 1606 avl_create(&ptree->z_user, 1607 perm_compare, sizeof (zfs_allow_node_t), 1608 offsetof(zfs_allow_node_t, z_node)); 1609 avl_create(&ptree->z_group, 1610 perm_compare, sizeof (zfs_allow_node_t), 1611 offsetof(zfs_allow_node_t, z_node)); 1612 avl_create(&ptree->z_everyone, 1613 perm_compare, sizeof (zfs_allow_node_t), 1614 offsetof(zfs_allow_node_t, z_node)); 1615 1616 if (prev) 1617 prev->z_next = ptree; 1618 ptree->z_next = NULL; 1619 return (ptree); 1620 } 1621 1622 /* 1623 * Add permissions to the appropriate AVL permission tree. 1624 * The appropriate tree may not be the requested tree. 1625 * For example if ld indicates a local permission, but 1626 * same permission also exists as a descendent permission 1627 * then the permission will be removed from the descendent 1628 * tree and add the the local+descendent tree. 1629 */ 1630 static int 1631 zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 1632 char *perm, char ld) 1633 { 1634 zfs_perm_node_t pnode, *permnode, *permnode2; 1635 zfs_perm_node_t *newnode; 1636 avl_index_t where, where2; 1637 avl_tree_t *tree, *altree; 1638 1639 (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 1640 1641 if (ld == ZFS_DELEG_NA) { 1642 tree = &allownode->z_localdescend; 1643 altree = &allownode->z_descend; 1644 } else if (ld == ZFS_DELEG_LOCAL) { 1645 tree = &allownode->z_local; 1646 altree = &allownode->z_descend; 1647 } else { 1648 tree = &allownode->z_descend; 1649 altree = &allownode->z_local; 1650 } 1651 permnode = avl_find(tree, &pnode, &where); 1652 permnode2 = avl_find(altree, &pnode, &where2); 1653 1654 if (permnode2) { 1655 avl_remove(altree, permnode2); 1656 free(permnode2); 1657 if (permnode == NULL) { 1658 tree = &allownode->z_localdescend; 1659 } 1660 } 1661 1662 /* 1663 * Now insert new permission in either requested location 1664 * local/descendent or into ld when perm will exist in both. 1665 */ 1666 if (permnode == NULL) { 1667 if ((newnode = zfs_alloc(zhp->zfs_hdl, 1668 sizeof (zfs_perm_node_t))) == NULL) { 1669 return (-1); 1670 } 1671 *newnode = pnode; 1672 avl_add(tree, newnode); 1673 } 1674 return (0); 1675 } 1676 1677 /* 1678 * Uggh, this is going to be a bit complicated. 1679 * we have an nvlist coming out of the kernel that 1680 * will indicate where the permission is set and then 1681 * it will contain allow of the various "who's", and what 1682 * their permissions are. To further complicate this 1683 * we will then have to coalesce the local,descendent 1684 * and local+descendent permissions where appropriate. 1685 * The kernel only knows about a permission as being local 1686 * or descendent, but not both. 1687 * 1688 * In order to make this easier for zfs_main to deal with 1689 * a series of AVL trees will be used to maintain 1690 * all of this, primarily for sorting purposes as well 1691 * as the ability to quickly locate a specific entry. 1692 * 1693 * What we end up with are tree's for sets, create perms, 1694 * user, groups and everyone. With each of those trees 1695 * we have subtrees for local, descendent and local+descendent 1696 * permissions. 1697 */ 1698 int 1699 zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 1700 { 1701 zfs_cmd_t zc = { 0 }; 1702 int error; 1703 nvlist_t *nvlist; 1704 nvlist_t *permnv, *sourcenv; 1705 nvpair_t *who_pair, *source_pair; 1706 nvpair_t *perm_pair; 1707 char errbuf[1024]; 1708 zfs_allow_t *zallowp, *newallowp; 1709 char ld; 1710 char *nvpname; 1711 uid_t uid; 1712 gid_t gid; 1713 avl_tree_t *tree; 1714 avl_index_t where; 1715 1716 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1717 1718 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 1719 return (-1); 1720 1721 while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 1722 if (errno == ENOMEM) { 1723 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 1724 zcmd_free_nvlists(&zc); 1725 return (-1); 1726 } 1727 } else if (errno == ENOTSUP) { 1728 zcmd_free_nvlists(&zc); 1729 (void) snprintf(errbuf, sizeof (errbuf), 1730 gettext("Pool must be upgraded to use 'allow'")); 1731 return (zfs_error(zhp->zfs_hdl, 1732 EZFS_BADVERSION, errbuf)); 1733 } else { 1734 zcmd_free_nvlists(&zc); 1735 return (-1); 1736 } 1737 } 1738 1739 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 1740 zcmd_free_nvlists(&zc); 1741 return (-1); 1742 } 1743 1744 zcmd_free_nvlists(&zc); 1745 1746 source_pair = nvlist_next_nvpair(nvlist, NULL); 1747 1748 if (source_pair == NULL) { 1749 *zfs_perms = NULL; 1750 return (0); 1751 } 1752 1753 *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 1754 if (*zfs_perms == NULL) { 1755 return (0); 1756 } 1757 1758 zallowp = *zfs_perms; 1759 1760 for (;;) { 1761 struct passwd *pwd; 1762 struct group *grp; 1763 zfs_allow_node_t *allownode; 1764 zfs_allow_node_t findallownode; 1765 zfs_allow_node_t *newallownode; 1766 1767 (void) strlcpy(zallowp->z_setpoint, 1768 nvpair_name(source_pair), 1769 sizeof (zallowp->z_setpoint)); 1770 1771 if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 1772 goto abort; 1773 1774 /* 1775 * Make sure nvlist is composed correctly 1776 */ 1777 if (zfs_deleg_verify_nvlist(sourcenv)) { 1778 goto abort; 1779 } 1780 1781 who_pair = nvlist_next_nvpair(sourcenv, NULL); 1782 if (who_pair == NULL) { 1783 goto abort; 1784 } 1785 1786 do { 1787 error = nvpair_value_nvlist(who_pair, &permnv); 1788 if (error) { 1789 goto abort; 1790 } 1791 1792 /* 1793 * First build up the key to use 1794 * for looking up in the various 1795 * who trees. 1796 */ 1797 ld = nvpair_name(who_pair)[1]; 1798 nvpname = nvpair_name(who_pair); 1799 switch (nvpair_name(who_pair)[0]) { 1800 case ZFS_DELEG_USER: 1801 case ZFS_DELEG_USER_SETS: 1802 tree = &zallowp->z_user; 1803 uid = atol(&nvpname[3]); 1804 pwd = getpwuid(uid); 1805 (void) snprintf(findallownode.z_key, 1806 sizeof (findallownode.z_key), "user %s", 1807 (pwd) ? pwd->pw_name : 1808 &nvpair_name(who_pair)[3]); 1809 break; 1810 case ZFS_DELEG_GROUP: 1811 case ZFS_DELEG_GROUP_SETS: 1812 tree = &zallowp->z_group; 1813 gid = atol(&nvpname[3]); 1814 grp = getgrgid(gid); 1815 (void) snprintf(findallownode.z_key, 1816 sizeof (findallownode.z_key), "group %s", 1817 (grp) ? grp->gr_name : 1818 &nvpair_name(who_pair)[3]); 1819 break; 1820 case ZFS_DELEG_CREATE: 1821 case ZFS_DELEG_CREATE_SETS: 1822 tree = &zallowp->z_crperms; 1823 (void) strlcpy(findallownode.z_key, "", 1824 sizeof (findallownode.z_key)); 1825 break; 1826 case ZFS_DELEG_EVERYONE: 1827 case ZFS_DELEG_EVERYONE_SETS: 1828 (void) snprintf(findallownode.z_key, 1829 sizeof (findallownode.z_key), "everyone"); 1830 tree = &zallowp->z_everyone; 1831 break; 1832 case ZFS_DELEG_NAMED_SET: 1833 case ZFS_DELEG_NAMED_SET_SETS: 1834 (void) snprintf(findallownode.z_key, 1835 sizeof (findallownode.z_key), "%s", 1836 &nvpair_name(who_pair)[3]); 1837 tree = &zallowp->z_sets; 1838 break; 1839 } 1840 1841 /* 1842 * Place who in tree 1843 */ 1844 allownode = avl_find(tree, &findallownode, &where); 1845 if (allownode == NULL) { 1846 if ((newallownode = zfs_alloc(zhp->zfs_hdl, 1847 sizeof (zfs_allow_node_t))) == NULL) { 1848 goto abort; 1849 } 1850 avl_create(&newallownode->z_localdescend, 1851 perm_compare, 1852 sizeof (zfs_perm_node_t), 1853 offsetof(zfs_perm_node_t, z_node)); 1854 avl_create(&newallownode->z_local, 1855 perm_compare, 1856 sizeof (zfs_perm_node_t), 1857 offsetof(zfs_perm_node_t, z_node)); 1858 avl_create(&newallownode->z_descend, 1859 perm_compare, 1860 sizeof (zfs_perm_node_t), 1861 offsetof(zfs_perm_node_t, z_node)); 1862 (void) strlcpy(newallownode->z_key, 1863 findallownode.z_key, 1864 sizeof (findallownode.z_key)); 1865 avl_insert(tree, newallownode, where); 1866 allownode = newallownode; 1867 } 1868 1869 /* 1870 * Now iterate over the permissions and 1871 * place them in the appropriate local, 1872 * descendent or local+descendent tree. 1873 * 1874 * The permissions are added to the tree 1875 * via zfs_coalesce_perm(). 1876 */ 1877 perm_pair = nvlist_next_nvpair(permnv, NULL); 1878 if (perm_pair == NULL) 1879 goto abort; 1880 do { 1881 if (zfs_coalesce_perm(zhp, allownode, 1882 nvpair_name(perm_pair), ld) != 0) 1883 goto abort; 1884 } while (perm_pair = nvlist_next_nvpair(permnv, 1885 perm_pair)); 1886 } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 1887 1888 source_pair = nvlist_next_nvpair(nvlist, source_pair); 1889 if (source_pair == NULL) 1890 break; 1891 1892 /* 1893 * allocate another node from the link list of 1894 * zfs_allow_t structures 1895 */ 1896 newallowp = zfs_alloc_perm_tree(zhp, zallowp, 1897 nvpair_name(source_pair)); 1898 if (newallowp == NULL) { 1899 goto abort; 1900 } 1901 zallowp = newallowp; 1902 } 1903 nvlist_free(nvlist); 1904 return (0); 1905 abort: 1906 zfs_free_allows(*zfs_perms); 1907 nvlist_free(nvlist); 1908 return (-1); 1909 } 1910 1911 /* 1912 * Given a property name and value, set the property for the given dataset. 1913 */ 1914 int 1915 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1916 { 1917 zfs_cmd_t zc = { 0 }; 1918 int ret = -1; 1919 prop_changelist_t *cl = NULL; 1920 char errbuf[1024]; 1921 libzfs_handle_t *hdl = zhp->zfs_hdl; 1922 nvlist_t *nvl = NULL, *realprops; 1923 zfs_prop_t prop; 1924 1925 (void) snprintf(errbuf, sizeof (errbuf), 1926 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1927 zhp->zfs_name); 1928 1929 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1930 nvlist_add_string(nvl, propname, propval) != 0) { 1931 (void) no_memory(hdl); 1932 goto error; 1933 } 1934 1935 if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, NULL, nvl, 1936 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 1937 goto error; 1938 nvlist_free(nvl); 1939 nvl = realprops; 1940 1941 prop = zfs_name_to_prop(propname); 1942 1943 if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1944 goto error; 1945 1946 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 1947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1948 "child dataset with inherited mountpoint is used " 1949 "in a non-global zone")); 1950 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1951 goto error; 1952 } 1953 1954 if ((ret = changelist_prefix(cl)) != 0) 1955 goto error; 1956 1957 /* 1958 * Execute the corresponding ioctl() to set this property. 1959 */ 1960 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1961 1962 if (zcmd_write_src_nvlist(hdl, &zc, nvl, NULL) != 0) 1963 goto error; 1964 1965 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1966 1967 if (ret != 0) { 1968 switch (errno) { 1969 1970 case ENOSPC: 1971 /* 1972 * For quotas and reservations, ENOSPC indicates 1973 * something different; setting a quota or reservation 1974 * doesn't use any disk space. 1975 */ 1976 switch (prop) { 1977 case ZFS_PROP_QUOTA: 1978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1979 "size is less than current used or " 1980 "reserved space")); 1981 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1982 break; 1983 1984 case ZFS_PROP_RESERVATION: 1985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1986 "size is greater than available space")); 1987 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1988 break; 1989 1990 default: 1991 (void) zfs_standard_error(hdl, errno, errbuf); 1992 break; 1993 } 1994 break; 1995 1996 case EBUSY: 1997 if (prop == ZFS_PROP_VOLBLOCKSIZE) 1998 (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 1999 else 2000 (void) zfs_standard_error(hdl, EBUSY, errbuf); 2001 break; 2002 2003 case EROFS: 2004 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 2005 break; 2006 2007 case ENOTSUP: 2008 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2009 "pool must be upgraded to set this " 2010 "property or value")); 2011 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 2012 break; 2013 2014 case EOVERFLOW: 2015 /* 2016 * This platform can't address a volume this big. 2017 */ 2018 #ifdef _ILP32 2019 if (prop == ZFS_PROP_VOLSIZE) { 2020 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 2021 break; 2022 } 2023 #endif 2024 /* FALLTHROUGH */ 2025 default: 2026 (void) zfs_standard_error(hdl, errno, errbuf); 2027 } 2028 } else { 2029 /* 2030 * Refresh the statistics so the new property value 2031 * is reflected. 2032 */ 2033 if ((ret = changelist_postfix(cl)) == 0) 2034 (void) get_stats(zhp); 2035 } 2036 2037 error: 2038 nvlist_free(nvl); 2039 zcmd_free_nvlists(&zc); 2040 if (cl) 2041 changelist_free(cl); 2042 return (ret); 2043 } 2044 2045 /* 2046 * Given a property, inherit the value from the parent dataset. 2047 */ 2048 int 2049 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 2050 { 2051 zfs_cmd_t zc = { 0 }; 2052 int ret; 2053 prop_changelist_t *cl; 2054 libzfs_handle_t *hdl = zhp->zfs_hdl; 2055 char errbuf[1024]; 2056 zfs_prop_t prop; 2057 2058 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2059 "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 2060 2061 if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) { 2062 /* 2063 * For user properties, the amount of work we have to do is very 2064 * small, so just do it here. 2065 */ 2066 if (!zfs_prop_user(propname)) { 2067 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2068 "invalid property")); 2069 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2070 } 2071 2072 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2073 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2074 2075 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc) != 0) 2076 return (zfs_standard_error(hdl, errno, errbuf)); 2077 2078 return (0); 2079 } 2080 2081 /* 2082 * Verify that this property is inheritable. 2083 */ 2084 if (zfs_prop_readonly(prop)) 2085 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 2086 2087 if (!zfs_prop_inheritable(prop)) 2088 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2089 2090 /* 2091 * Check to see if the value applies to this type 2092 */ 2093 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2094 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2095 2096 /* 2097 * Normalize the name, to get rid of shorthand abbrevations. 2098 */ 2099 propname = zfs_prop_to_name(prop); 2100 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2101 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2102 2103 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2104 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 2105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2106 "dataset is used in a non-global zone")); 2107 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2108 } 2109 2110 /* 2111 * Determine datasets which will be affected by this change, if any. 2112 */ 2113 if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 2114 return (-1); 2115 2116 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 2117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2118 "child dataset with inherited mountpoint is used " 2119 "in a non-global zone")); 2120 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2121 goto error; 2122 } 2123 2124 if ((ret = changelist_prefix(cl)) != 0) 2125 goto error; 2126 2127 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc)) != 0) { 2128 return (zfs_standard_error(hdl, errno, errbuf)); 2129 } else { 2130 2131 if ((ret = changelist_postfix(cl)) != 0) 2132 goto error; 2133 2134 /* 2135 * Refresh the statistics so the new property is reflected. 2136 */ 2137 (void) get_stats(zhp); 2138 } 2139 2140 error: 2141 changelist_free(cl); 2142 return (ret); 2143 } 2144 2145 void 2146 nicebool(int value, char *buf, size_t buflen) 2147 { 2148 if (value) 2149 (void) strlcpy(buf, "on", buflen); 2150 else 2151 (void) strlcpy(buf, "off", buflen); 2152 } 2153 2154 /* 2155 * True DSL properties are stored in an nvlist. The following two functions 2156 * extract them appropriately. 2157 */ 2158 static uint64_t 2159 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 2160 { 2161 nvlist_t *nv; 2162 uint64_t value; 2163 2164 *source = NULL; 2165 if (nvlist_lookup_nvlist(zhp->zfs_props, 2166 zfs_prop_to_name(prop), &nv) == 0) { 2167 verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0); 2168 (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 2169 } else { 2170 value = zfs_prop_default_numeric(prop); 2171 *source = ""; 2172 } 2173 2174 return (value); 2175 } 2176 2177 static char * 2178 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 2179 { 2180 nvlist_t *nv; 2181 char *value; 2182 2183 *source = NULL; 2184 if (nvlist_lookup_nvlist(zhp->zfs_props, 2185 zfs_prop_to_name(prop), &nv) == 0) { 2186 verify(nvlist_lookup_string(nv, ZFS_PROP_VALUE, &value) == 0); 2187 (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 2188 } else { 2189 if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 2190 value = ""; 2191 *source = ""; 2192 } 2193 2194 return (value); 2195 } 2196 2197 /* 2198 * Internal function for getting a numeric property. Both zfs_prop_get() and 2199 * zfs_prop_get_int() are built using this interface. 2200 * 2201 * Certain properties can be overridden using 'mount -o'. In this case, scan 2202 * the contents of the /etc/mnttab entry, searching for the appropriate options. 2203 * If they differ from the on-disk values, report the current values and mark 2204 * the source "temporary". 2205 */ 2206 static int 2207 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src, 2208 char **source, uint64_t *val) 2209 { 2210 struct mnttab mnt; 2211 char *mntopt_on = NULL; 2212 char *mntopt_off = NULL; 2213 2214 *source = NULL; 2215 2216 switch (prop) { 2217 case ZFS_PROP_ATIME: 2218 mntopt_on = MNTOPT_ATIME; 2219 mntopt_off = MNTOPT_NOATIME; 2220 break; 2221 2222 case ZFS_PROP_DEVICES: 2223 mntopt_on = MNTOPT_DEVICES; 2224 mntopt_off = MNTOPT_NODEVICES; 2225 break; 2226 2227 case ZFS_PROP_EXEC: 2228 mntopt_on = MNTOPT_EXEC; 2229 mntopt_off = MNTOPT_NOEXEC; 2230 break; 2231 2232 case ZFS_PROP_READONLY: 2233 mntopt_on = MNTOPT_RO; 2234 mntopt_off = MNTOPT_RW; 2235 break; 2236 2237 case ZFS_PROP_SETUID: 2238 mntopt_on = MNTOPT_SETUID; 2239 mntopt_off = MNTOPT_NOSETUID; 2240 break; 2241 2242 case ZFS_PROP_XATTR: 2243 mntopt_on = MNTOPT_XATTR; 2244 mntopt_off = MNTOPT_NOXATTR; 2245 break; 2246 } 2247 2248 /* 2249 * Because looking up the mount options is potentially expensive 2250 * (iterating over all of /etc/mnttab), we defer its calculation until 2251 * we're looking up a property which requires its presence. 2252 */ 2253 if (!zhp->zfs_mntcheck && 2254 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 2255 struct mnttab entry, search = { 0 }; 2256 FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 2257 2258 search.mnt_special = (char *)zhp->zfs_name; 2259 search.mnt_fstype = MNTTYPE_ZFS; 2260 rewind(mnttab); 2261 2262 if (getmntany(mnttab, &entry, &search) == 0) { 2263 zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 2264 entry.mnt_mntopts); 2265 if (zhp->zfs_mntopts == NULL) 2266 return (-1); 2267 } 2268 2269 zhp->zfs_mntcheck = B_TRUE; 2270 } 2271 2272 if (zhp->zfs_mntopts == NULL) 2273 mnt.mnt_mntopts = ""; 2274 else 2275 mnt.mnt_mntopts = zhp->zfs_mntopts; 2276 2277 switch (prop) { 2278 case ZFS_PROP_ATIME: 2279 case ZFS_PROP_DEVICES: 2280 case ZFS_PROP_EXEC: 2281 case ZFS_PROP_READONLY: 2282 case ZFS_PROP_SETUID: 2283 case ZFS_PROP_XATTR: 2284 *val = getprop_uint64(zhp, prop, source); 2285 2286 if (hasmntopt(&mnt, mntopt_on) && !*val) { 2287 *val = B_TRUE; 2288 if (src) 2289 *src = ZFS_SRC_TEMPORARY; 2290 } else if (hasmntopt(&mnt, mntopt_off) && *val) { 2291 *val = B_FALSE; 2292 if (src) 2293 *src = ZFS_SRC_TEMPORARY; 2294 } 2295 break; 2296 2297 case ZFS_PROP_CANMOUNT: 2298 *val = getprop_uint64(zhp, prop, source); 2299 if (*val == 0) 2300 *source = zhp->zfs_name; 2301 else 2302 *source = ""; /* default */ 2303 break; 2304 2305 case ZFS_PROP_QUOTA: 2306 case ZFS_PROP_RESERVATION: 2307 *val = getprop_uint64(zhp, prop, source); 2308 if (*val == 0) 2309 *source = ""; /* default */ 2310 else 2311 *source = zhp->zfs_name; 2312 break; 2313 2314 case ZFS_PROP_MOUNTED: 2315 *val = (zhp->zfs_mntopts != NULL); 2316 break; 2317 2318 case ZFS_PROP_NUMCLONES: 2319 *val = zhp->zfs_dmustats.dds_num_clones; 2320 break; 2321 2322 default: 2323 switch (zfs_prop_get_type(prop)) { 2324 case prop_type_number: 2325 case prop_type_boolean: 2326 case prop_type_index: 2327 *val = getprop_uint64(zhp, prop, source); 2328 break; 2329 2330 case prop_type_string: 2331 default: 2332 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2333 "cannot get non-numeric property")); 2334 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 2335 dgettext(TEXT_DOMAIN, "internal error"))); 2336 } 2337 } 2338 2339 return (0); 2340 } 2341 2342 /* 2343 * Calculate the source type, given the raw source string. 2344 */ 2345 static void 2346 get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source, 2347 char *statbuf, size_t statlen) 2348 { 2349 if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY) 2350 return; 2351 2352 if (source == NULL) { 2353 *srctype = ZFS_SRC_NONE; 2354 } else if (source[0] == '\0') { 2355 *srctype = ZFS_SRC_DEFAULT; 2356 } else { 2357 if (strcmp(source, zhp->zfs_name) == 0) { 2358 *srctype = ZFS_SRC_LOCAL; 2359 } else { 2360 (void) strlcpy(statbuf, source, statlen); 2361 *srctype = ZFS_SRC_INHERITED; 2362 } 2363 } 2364 2365 } 2366 2367 /* 2368 * Retrieve a property from the given object. If 'literal' is specified, then 2369 * numbers are left as exact values. Otherwise, numbers are converted to a 2370 * human-readable form. 2371 * 2372 * Returns 0 on success, or -1 on error. 2373 */ 2374 int 2375 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2376 zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2377 { 2378 char *source = NULL; 2379 uint64_t val; 2380 char *str; 2381 const char *root; 2382 const char *strval; 2383 2384 /* 2385 * Check to see if this property applies to our object 2386 */ 2387 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2388 return (-1); 2389 2390 if (src) 2391 *src = ZFS_SRC_NONE; 2392 2393 switch (prop) { 2394 case ZFS_PROP_CREATION: 2395 /* 2396 * 'creation' is a time_t stored in the statistics. We convert 2397 * this into a string unless 'literal' is specified. 2398 */ 2399 { 2400 val = getprop_uint64(zhp, prop, &source); 2401 time_t time = (time_t)val; 2402 struct tm t; 2403 2404 if (literal || 2405 localtime_r(&time, &t) == NULL || 2406 strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2407 &t) == 0) 2408 (void) snprintf(propbuf, proplen, "%llu", val); 2409 } 2410 break; 2411 2412 case ZFS_PROP_MOUNTPOINT: 2413 /* 2414 * Getting the precise mountpoint can be tricky. 2415 * 2416 * - for 'none' or 'legacy', return those values. 2417 * - for default mountpoints, construct it as /zfs/<dataset> 2418 * - for inherited mountpoints, we want to take everything 2419 * after our ancestor and append it to the inherited value. 2420 * 2421 * If the pool has an alternate root, we want to prepend that 2422 * root to any values we return. 2423 */ 2424 root = zhp->zfs_root; 2425 str = getprop_string(zhp, prop, &source); 2426 2427 if (str[0] == '\0') { 2428 (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2429 root, zhp->zfs_name); 2430 } else if (str[0] == '/') { 2431 const char *relpath = zhp->zfs_name + strlen(source); 2432 2433 if (relpath[0] == '/') 2434 relpath++; 2435 if (str[1] == '\0') 2436 str++; 2437 2438 if (relpath[0] == '\0') 2439 (void) snprintf(propbuf, proplen, "%s%s", 2440 root, str); 2441 else 2442 (void) snprintf(propbuf, proplen, "%s%s%s%s", 2443 root, str, relpath[0] == '@' ? "" : "/", 2444 relpath); 2445 } else { 2446 /* 'legacy' or 'none' */ 2447 (void) strlcpy(propbuf, str, proplen); 2448 } 2449 2450 break; 2451 2452 case ZFS_PROP_ORIGIN: 2453 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2454 proplen); 2455 /* 2456 * If there is no parent at all, return failure to indicate that 2457 * it doesn't apply to this dataset. 2458 */ 2459 if (propbuf[0] == '\0') 2460 return (-1); 2461 break; 2462 2463 case ZFS_PROP_QUOTA: 2464 case ZFS_PROP_RESERVATION: 2465 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2466 return (-1); 2467 2468 /* 2469 * If quota or reservation is 0, we translate this into 'none' 2470 * (unless literal is set), and indicate that it's the default 2471 * value. Otherwise, we print the number nicely and indicate 2472 * that its set locally. 2473 */ 2474 if (val == 0) { 2475 if (literal) 2476 (void) strlcpy(propbuf, "0", proplen); 2477 else 2478 (void) strlcpy(propbuf, "none", proplen); 2479 } else { 2480 if (literal) 2481 (void) snprintf(propbuf, proplen, "%llu", 2482 (u_longlong_t)val); 2483 else 2484 zfs_nicenum(val, propbuf, proplen); 2485 } 2486 break; 2487 2488 case ZFS_PROP_COMPRESSRATIO: 2489 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2490 return (-1); 2491 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 2492 val / 100, (longlong_t)val % 100); 2493 break; 2494 2495 case ZFS_PROP_TYPE: 2496 switch (zhp->zfs_type) { 2497 case ZFS_TYPE_FILESYSTEM: 2498 str = "filesystem"; 2499 break; 2500 case ZFS_TYPE_VOLUME: 2501 str = "volume"; 2502 break; 2503 case ZFS_TYPE_SNAPSHOT: 2504 str = "snapshot"; 2505 break; 2506 default: 2507 abort(); 2508 } 2509 (void) snprintf(propbuf, proplen, "%s", str); 2510 break; 2511 2512 case ZFS_PROP_MOUNTED: 2513 /* 2514 * The 'mounted' property is a pseudo-property that described 2515 * whether the filesystem is currently mounted. Even though 2516 * it's a boolean value, the typical values of "on" and "off" 2517 * don't make sense, so we translate to "yes" and "no". 2518 */ 2519 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 2520 src, &source, &val) != 0) 2521 return (-1); 2522 if (val) 2523 (void) strlcpy(propbuf, "yes", proplen); 2524 else 2525 (void) strlcpy(propbuf, "no", proplen); 2526 break; 2527 2528 case ZFS_PROP_NAME: 2529 /* 2530 * The 'name' property is a pseudo-property derived from the 2531 * dataset name. It is presented as a real property to simplify 2532 * consumers. 2533 */ 2534 (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2535 break; 2536 2537 default: 2538 switch (zfs_prop_get_type(prop)) { 2539 case prop_type_number: 2540 if (get_numeric_property(zhp, prop, src, 2541 &source, &val) != 0) 2542 return (-1); 2543 if (literal) 2544 (void) snprintf(propbuf, proplen, "%llu", 2545 (u_longlong_t)val); 2546 else 2547 zfs_nicenum(val, propbuf, proplen); 2548 break; 2549 2550 case prop_type_string: 2551 (void) strlcpy(propbuf, 2552 getprop_string(zhp, prop, &source), proplen); 2553 break; 2554 2555 case prop_type_boolean: 2556 if (get_numeric_property(zhp, prop, src, 2557 &source, &val) != 0) 2558 return (-1); 2559 nicebool(val, propbuf, proplen); 2560 2561 break; 2562 2563 case prop_type_index: 2564 val = getprop_uint64(zhp, prop, &source); 2565 if (zfs_prop_index_to_string(prop, val, 2566 &strval) != 0) 2567 return (-1); 2568 (void) strlcpy(propbuf, strval, proplen); 2569 break; 2570 2571 default: 2572 abort(); 2573 } 2574 } 2575 2576 get_source(zhp, src, source, statbuf, statlen); 2577 2578 return (0); 2579 } 2580 2581 /* 2582 * Utility function to get the given numeric property. Does no validation that 2583 * the given property is the appropriate type; should only be used with 2584 * hard-coded property types. 2585 */ 2586 uint64_t 2587 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2588 { 2589 char *source; 2590 zfs_source_t sourcetype = ZFS_SRC_NONE; 2591 uint64_t val; 2592 2593 (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 2594 2595 return (val); 2596 } 2597 2598 /* 2599 * Similar to zfs_prop_get(), but returns the value as an integer. 2600 */ 2601 int 2602 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2603 zfs_source_t *src, char *statbuf, size_t statlen) 2604 { 2605 char *source; 2606 2607 /* 2608 * Check to see if this property applies to our object 2609 */ 2610 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2611 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 2612 dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 2613 zfs_prop_to_name(prop))); 2614 2615 if (src) 2616 *src = ZFS_SRC_NONE; 2617 2618 if (get_numeric_property(zhp, prop, src, &source, value) != 0) 2619 return (-1); 2620 2621 get_source(zhp, src, source, statbuf, statlen); 2622 2623 return (0); 2624 } 2625 2626 /* 2627 * Returns the name of the given zfs handle. 2628 */ 2629 const char * 2630 zfs_get_name(const zfs_handle_t *zhp) 2631 { 2632 return (zhp->zfs_name); 2633 } 2634 2635 /* 2636 * Returns the type of the given zfs handle. 2637 */ 2638 zfs_type_t 2639 zfs_get_type(const zfs_handle_t *zhp) 2640 { 2641 return (zhp->zfs_type); 2642 } 2643 2644 /* 2645 * Iterate over all child filesystems 2646 */ 2647 int 2648 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2649 { 2650 zfs_cmd_t zc = { 0 }; 2651 zfs_handle_t *nzhp; 2652 int ret; 2653 2654 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2655 ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2656 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2657 /* 2658 * Ignore private dataset names. 2659 */ 2660 if (dataset_name_hidden(zc.zc_name)) 2661 continue; 2662 2663 /* 2664 * Silently ignore errors, as the only plausible explanation is 2665 * that the pool has since been removed. 2666 */ 2667 if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 2668 zc.zc_name)) == NULL) 2669 continue; 2670 2671 if ((ret = func(nzhp, data)) != 0) 2672 return (ret); 2673 } 2674 2675 /* 2676 * An errno value of ESRCH indicates normal completion. If ENOENT is 2677 * returned, then the underlying dataset has been removed since we 2678 * obtained the handle. 2679 */ 2680 if (errno != ESRCH && errno != ENOENT) 2681 return (zfs_standard_error(zhp->zfs_hdl, errno, 2682 dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2683 2684 return (0); 2685 } 2686 2687 /* 2688 * Iterate over all snapshots 2689 */ 2690 int 2691 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2692 { 2693 zfs_cmd_t zc = { 0 }; 2694 zfs_handle_t *nzhp; 2695 int ret; 2696 2697 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2698 ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 2699 &zc) == 0; 2700 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2701 2702 if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 2703 zc.zc_name)) == NULL) 2704 continue; 2705 2706 if ((ret = func(nzhp, data)) != 0) 2707 return (ret); 2708 } 2709 2710 /* 2711 * An errno value of ESRCH indicates normal completion. If ENOENT is 2712 * returned, then the underlying dataset has been removed since we 2713 * obtained the handle. Silently ignore this case, and return success. 2714 */ 2715 if (errno != ESRCH && errno != ENOENT) 2716 return (zfs_standard_error(zhp->zfs_hdl, errno, 2717 dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2718 2719 return (0); 2720 } 2721 2722 /* 2723 * Iterate over all children, snapshots and filesystems 2724 */ 2725 int 2726 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2727 { 2728 int ret; 2729 2730 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 2731 return (ret); 2732 2733 return (zfs_iter_snapshots(zhp, func, data)); 2734 } 2735 2736 /* 2737 * Given a complete name, return just the portion that refers to the parent. 2738 * Can return NULL if this is a pool. 2739 */ 2740 static int 2741 parent_name(const char *path, char *buf, size_t buflen) 2742 { 2743 char *loc; 2744 2745 if ((loc = strrchr(path, '/')) == NULL) 2746 return (-1); 2747 2748 (void) strncpy(buf, path, MIN(buflen, loc - path)); 2749 buf[loc - path] = '\0'; 2750 2751 return (0); 2752 } 2753 2754 /* 2755 * If accept_ancestor is false, then check to make sure that the given path has 2756 * a parent, and that it exists. If accept_ancestor is true, then find the 2757 * closest existing ancestor for the given path. In prefixlen return the 2758 * length of already existing prefix of the given path. We also fetch the 2759 * 'zoned' property, which is used to validate property settings when creating 2760 * new datasets. 2761 */ 2762 static int 2763 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 2764 boolean_t accept_ancestor, int *prefixlen) 2765 { 2766 zfs_cmd_t zc = { 0 }; 2767 char parent[ZFS_MAXNAMELEN]; 2768 char *slash; 2769 zfs_handle_t *zhp; 2770 char errbuf[1024]; 2771 2772 (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 2773 path); 2774 2775 /* get parent, and check to see if this is just a pool */ 2776 if (parent_name(path, parent, sizeof (parent)) != 0) { 2777 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2778 "missing dataset name")); 2779 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2780 } 2781 2782 /* check to see if the pool exists */ 2783 if ((slash = strchr(parent, '/')) == NULL) 2784 slash = parent + strlen(parent); 2785 (void) strncpy(zc.zc_name, parent, slash - parent); 2786 zc.zc_name[slash - parent] = '\0'; 2787 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2788 errno == ENOENT) { 2789 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2790 "no such pool '%s'"), zc.zc_name); 2791 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2792 } 2793 2794 /* check to see if the parent dataset exists */ 2795 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 2796 if (errno == ENOENT && accept_ancestor) { 2797 /* 2798 * Go deeper to find an ancestor, give up on top level. 2799 */ 2800 if (parent_name(parent, parent, sizeof (parent)) != 0) { 2801 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2802 "no such pool '%s'"), zc.zc_name); 2803 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2804 } 2805 } else if (errno == ENOENT) { 2806 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2807 "parent does not exist")); 2808 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2809 } else 2810 return (zfs_standard_error(hdl, errno, errbuf)); 2811 } 2812 2813 *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2814 /* we are in a non-global zone, but parent is in the global zone */ 2815 if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 2816 (void) zfs_standard_error(hdl, EPERM, errbuf); 2817 zfs_close(zhp); 2818 return (-1); 2819 } 2820 2821 /* make sure parent is a filesystem */ 2822 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 2823 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2824 "parent is not a filesystem")); 2825 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 2826 zfs_close(zhp); 2827 return (-1); 2828 } 2829 2830 zfs_close(zhp); 2831 if (prefixlen != NULL) 2832 *prefixlen = strlen(parent); 2833 return (0); 2834 } 2835 2836 /* 2837 * Finds whether the dataset of the given type(s) exists. 2838 */ 2839 boolean_t 2840 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 2841 { 2842 zfs_handle_t *zhp; 2843 2844 if (!zfs_validate_name(hdl, path, types)) 2845 return (B_FALSE); 2846 2847 /* 2848 * Try to get stats for the dataset, which will tell us if it exists. 2849 */ 2850 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 2851 int ds_type = zhp->zfs_type; 2852 2853 zfs_close(zhp); 2854 if (types & ds_type) 2855 return (B_TRUE); 2856 } 2857 return (B_FALSE); 2858 } 2859 2860 /* 2861 * Creates non-existing ancestors of the given path. 2862 */ 2863 int 2864 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 2865 { 2866 int prefix; 2867 uint64_t zoned; 2868 char *path_copy; 2869 int rc; 2870 2871 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 2872 return (-1); 2873 2874 if ((path_copy = strdup(path)) != NULL) { 2875 rc = create_parents(hdl, path_copy, prefix); 2876 free(path_copy); 2877 } 2878 if (path_copy == NULL || rc != 0) 2879 return (-1); 2880 2881 return (0); 2882 } 2883 2884 /* 2885 * Create a new filesystem or volume. 2886 */ 2887 int 2888 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 2889 nvlist_t *props) 2890 { 2891 zfs_cmd_t zc = { 0 }; 2892 int ret; 2893 uint64_t size = 0; 2894 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 2895 char errbuf[1024]; 2896 uint64_t zoned; 2897 2898 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2899 "cannot create '%s'"), path); 2900 2901 /* validate the path, taking care to note the extended error message */ 2902 if (!zfs_validate_name(hdl, path, type)) 2903 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2904 2905 /* validate parents exist */ 2906 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2907 return (-1); 2908 2909 /* 2910 * The failure modes when creating a dataset of a different type over 2911 * one that already exists is a little strange. In particular, if you 2912 * try to create a dataset on top of an existing dataset, the ioctl() 2913 * will return ENOENT, not EEXIST. To prevent this from happening, we 2914 * first try to see if the dataset exists. 2915 */ 2916 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 2917 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 2918 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2919 "dataset already exists")); 2920 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2921 } 2922 2923 if (type == ZFS_TYPE_VOLUME) 2924 zc.zc_objset_type = DMU_OST_ZVOL; 2925 else 2926 zc.zc_objset_type = DMU_OST_ZFS; 2927 2928 if (props && (props = zfs_validate_properties(hdl, type, NULL, props, 2929 zoned, NULL, errbuf)) == 0) 2930 return (-1); 2931 2932 if (type == ZFS_TYPE_VOLUME) { 2933 /* 2934 * If we are creating a volume, the size and block size must 2935 * satisfy a few restraints. First, the blocksize must be a 2936 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 2937 * volsize must be a multiple of the block size, and cannot be 2938 * zero. 2939 */ 2940 if (props == NULL || nvlist_lookup_uint64(props, 2941 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 2942 nvlist_free(props); 2943 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2944 "missing volume size")); 2945 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2946 } 2947 2948 if ((ret = nvlist_lookup_uint64(props, 2949 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 2950 &blocksize)) != 0) { 2951 if (ret == ENOENT) { 2952 blocksize = zfs_prop_default_numeric( 2953 ZFS_PROP_VOLBLOCKSIZE); 2954 } else { 2955 nvlist_free(props); 2956 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2957 "missing volume block size")); 2958 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2959 } 2960 } 2961 2962 if (size == 0) { 2963 nvlist_free(props); 2964 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2965 "volume size cannot be zero")); 2966 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2967 } 2968 2969 if (size % blocksize != 0) { 2970 nvlist_free(props); 2971 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2972 "volume size must be a multiple of volume block " 2973 "size")); 2974 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2975 } 2976 } 2977 2978 if (props && 2979 zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) 2980 return (-1); 2981 nvlist_free(props); 2982 2983 /* create the dataset */ 2984 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2985 2986 if (ret == 0 && type == ZFS_TYPE_VOLUME) { 2987 ret = zvol_create_link(hdl, path); 2988 if (ret) { 2989 (void) zfs_standard_error(hdl, errno, 2990 dgettext(TEXT_DOMAIN, 2991 "Volume successfully created, but device links " 2992 "were not created")); 2993 zcmd_free_nvlists(&zc); 2994 return (-1); 2995 } 2996 } 2997 2998 zcmd_free_nvlists(&zc); 2999 3000 /* check for failure */ 3001 if (ret != 0) { 3002 char parent[ZFS_MAXNAMELEN]; 3003 (void) parent_name(path, parent, sizeof (parent)); 3004 3005 switch (errno) { 3006 case ENOENT: 3007 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3008 "no such parent '%s'"), parent); 3009 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3010 3011 case EINVAL: 3012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3013 "parent '%s' is not a filesystem"), parent); 3014 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3015 3016 case EDOM: 3017 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3018 "volume block size must be power of 2 from " 3019 "%u to %uk"), 3020 (uint_t)SPA_MINBLOCKSIZE, 3021 (uint_t)SPA_MAXBLOCKSIZE >> 10); 3022 3023 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3024 3025 case ENOTSUP: 3026 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3027 "pool must be upgraded to set this " 3028 "property or value")); 3029 return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3030 3031 #ifdef _ILP32 3032 case EOVERFLOW: 3033 /* 3034 * This platform can't address a volume this big. 3035 */ 3036 if (type == ZFS_TYPE_VOLUME) 3037 return (zfs_error(hdl, EZFS_VOLTOOBIG, 3038 errbuf)); 3039 #endif 3040 /* FALLTHROUGH */ 3041 default: 3042 return (zfs_standard_error(hdl, errno, errbuf)); 3043 } 3044 } 3045 3046 return (0); 3047 } 3048 3049 /* 3050 * Destroys the given dataset. The caller must make sure that the filesystem 3051 * isn't mounted, and that there are no active dependents. 3052 */ 3053 int 3054 zfs_destroy(zfs_handle_t *zhp) 3055 { 3056 zfs_cmd_t zc = { 0 }; 3057 3058 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3059 3060 if (ZFS_IS_VOLUME(zhp)) { 3061 /* 3062 * If user doesn't have permissions to unshare volume, then 3063 * abort the request. This would only happen for a 3064 * non-privileged user. 3065 */ 3066 if (zfs_unshare_iscsi(zhp) != 0) { 3067 return (-1); 3068 } 3069 3070 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3071 return (-1); 3072 3073 zc.zc_objset_type = DMU_OST_ZVOL; 3074 } else { 3075 zc.zc_objset_type = DMU_OST_ZFS; 3076 } 3077 3078 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 3079 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 3080 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 3081 zhp->zfs_name)); 3082 } 3083 3084 remove_mountpoint(zhp); 3085 3086 return (0); 3087 } 3088 3089 struct destroydata { 3090 char *snapname; 3091 boolean_t gotone; 3092 boolean_t closezhp; 3093 }; 3094 3095 static int 3096 zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 3097 { 3098 struct destroydata *dd = arg; 3099 zfs_handle_t *szhp; 3100 char name[ZFS_MAXNAMELEN]; 3101 boolean_t closezhp = dd->closezhp; 3102 int rv; 3103 3104 (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 3105 (void) strlcat(name, "@", sizeof (name)); 3106 (void) strlcat(name, dd->snapname, sizeof (name)); 3107 3108 szhp = make_dataset_handle(zhp->zfs_hdl, name); 3109 if (szhp) { 3110 dd->gotone = B_TRUE; 3111 zfs_close(szhp); 3112 } 3113 3114 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3115 (void) zvol_remove_link(zhp->zfs_hdl, name); 3116 /* 3117 * NB: this is simply a best-effort. We don't want to 3118 * return an error, because then we wouldn't visit all 3119 * the volumes. 3120 */ 3121 } 3122 3123 dd->closezhp = B_TRUE; 3124 rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 3125 if (closezhp) 3126 zfs_close(zhp); 3127 return (rv); 3128 } 3129 3130 /* 3131 * Destroys all snapshots with the given name in zhp & descendants. 3132 */ 3133 int 3134 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 3135 { 3136 zfs_cmd_t zc = { 0 }; 3137 int ret; 3138 struct destroydata dd = { 0 }; 3139 3140 dd.snapname = snapname; 3141 (void) zfs_remove_link_cb(zhp, &dd); 3142 3143 if (!dd.gotone) { 3144 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 3145 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 3146 zhp->zfs_name, snapname)); 3147 } 3148 3149 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3150 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 3151 3152 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 3153 if (ret != 0) { 3154 char errbuf[1024]; 3155 3156 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3157 "cannot destroy '%s@%s'"), zc.zc_name, snapname); 3158 3159 switch (errno) { 3160 case EEXIST: 3161 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3162 "snapshot is cloned")); 3163 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 3164 3165 default: 3166 return (zfs_standard_error(zhp->zfs_hdl, errno, 3167 errbuf)); 3168 } 3169 } 3170 3171 return (0); 3172 } 3173 3174 /* 3175 * Clones the given dataset. The target must be of the same type as the source. 3176 */ 3177 int 3178 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3179 { 3180 zfs_cmd_t zc = { 0 }; 3181 char parent[ZFS_MAXNAMELEN]; 3182 int ret; 3183 char errbuf[1024]; 3184 libzfs_handle_t *hdl = zhp->zfs_hdl; 3185 zfs_type_t type; 3186 uint64_t zoned; 3187 3188 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3189 3190 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3191 "cannot create '%s'"), target); 3192 3193 /* validate the target name */ 3194 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM)) 3195 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3196 3197 /* validate parents exist */ 3198 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3199 return (-1); 3200 3201 (void) parent_name(target, parent, sizeof (parent)); 3202 3203 /* do the clone */ 3204 if (ZFS_IS_VOLUME(zhp)) { 3205 zc.zc_objset_type = DMU_OST_ZVOL; 3206 type = ZFS_TYPE_VOLUME; 3207 } else { 3208 zc.zc_objset_type = DMU_OST_ZFS; 3209 type = ZFS_TYPE_FILESYSTEM; 3210 } 3211 3212 if (props) { 3213 if ((props = zfs_validate_properties(hdl, type, NULL, props, 3214 zoned, zhp, errbuf)) == NULL) 3215 return (-1); 3216 3217 if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) { 3218 nvlist_free(props); 3219 return (-1); 3220 } 3221 3222 nvlist_free(props); 3223 } 3224 3225 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 3226 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 3227 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3228 3229 zcmd_free_nvlists(&zc); 3230 3231 if (ret != 0) { 3232 switch (errno) { 3233 3234 case ENOENT: 3235 /* 3236 * The parent doesn't exist. We should have caught this 3237 * above, but there may a race condition that has since 3238 * destroyed the parent. 3239 * 3240 * At this point, we don't know whether it's the source 3241 * that doesn't exist anymore, or whether the target 3242 * dataset doesn't exist. 3243 */ 3244 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3245 "no such parent '%s'"), parent); 3246 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3247 3248 case EXDEV: 3249 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3250 "source and target pools differ")); 3251 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 3252 errbuf)); 3253 3254 default: 3255 return (zfs_standard_error(zhp->zfs_hdl, errno, 3256 errbuf)); 3257 } 3258 } else if (ZFS_IS_VOLUME(zhp)) { 3259 ret = zvol_create_link(zhp->zfs_hdl, target); 3260 } 3261 3262 return (ret); 3263 } 3264 3265 typedef struct promote_data { 3266 char cb_mountpoint[MAXPATHLEN]; 3267 const char *cb_target; 3268 const char *cb_errbuf; 3269 uint64_t cb_pivot_txg; 3270 } promote_data_t; 3271 3272 static int 3273 promote_snap_cb(zfs_handle_t *zhp, void *data) 3274 { 3275 promote_data_t *pd = data; 3276 zfs_handle_t *szhp; 3277 char snapname[MAXPATHLEN]; 3278 int rv = 0; 3279 3280 /* We don't care about snapshots after the pivot point */ 3281 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 3282 zfs_close(zhp); 3283 return (0); 3284 } 3285 3286 /* Remove the device link if it's a zvol. */ 3287 if (ZFS_IS_VOLUME(zhp)) 3288 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 3289 3290 /* Check for conflicting names */ 3291 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 3292 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 3293 szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 3294 if (szhp != NULL) { 3295 zfs_close(szhp); 3296 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3297 "snapshot name '%s' from origin \n" 3298 "conflicts with '%s' from target"), 3299 zhp->zfs_name, snapname); 3300 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 3301 } 3302 zfs_close(zhp); 3303 return (rv); 3304 } 3305 3306 static int 3307 promote_snap_done_cb(zfs_handle_t *zhp, void *data) 3308 { 3309 promote_data_t *pd = data; 3310 3311 /* We don't care about snapshots after the pivot point */ 3312 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 3313 /* Create the device link if it's a zvol. */ 3314 if (ZFS_IS_VOLUME(zhp)) 3315 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3316 } 3317 3318 zfs_close(zhp); 3319 return (0); 3320 } 3321 3322 /* 3323 * Promotes the given clone fs to be the clone parent. 3324 */ 3325 int 3326 zfs_promote(zfs_handle_t *zhp) 3327 { 3328 libzfs_handle_t *hdl = zhp->zfs_hdl; 3329 zfs_cmd_t zc = { 0 }; 3330 char parent[MAXPATHLEN]; 3331 char *cp; 3332 int ret; 3333 zfs_handle_t *pzhp; 3334 promote_data_t pd; 3335 char errbuf[1024]; 3336 3337 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3338 "cannot promote '%s'"), zhp->zfs_name); 3339 3340 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3341 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3342 "snapshots can not be promoted")); 3343 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3344 } 3345 3346 (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 3347 if (parent[0] == '\0') { 3348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3349 "not a cloned filesystem")); 3350 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3351 } 3352 cp = strchr(parent, '@'); 3353 *cp = '\0'; 3354 3355 /* Walk the snapshots we will be moving */ 3356 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 3357 if (pzhp == NULL) 3358 return (-1); 3359 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 3360 zfs_close(pzhp); 3361 pd.cb_target = zhp->zfs_name; 3362 pd.cb_errbuf = errbuf; 3363 pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY); 3364 if (pzhp == NULL) 3365 return (-1); 3366 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 3367 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 3368 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 3369 if (ret != 0) { 3370 zfs_close(pzhp); 3371 return (-1); 3372 } 3373 3374 /* issue the ioctl */ 3375 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 3376 sizeof (zc.zc_value)); 3377 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3378 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 3379 3380 if (ret != 0) { 3381 int save_errno = errno; 3382 3383 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 3384 zfs_close(pzhp); 3385 3386 switch (save_errno) { 3387 case EEXIST: 3388 /* 3389 * There is a conflicting snapshot name. We 3390 * should have caught this above, but they could 3391 * have renamed something in the mean time. 3392 */ 3393 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3394 "conflicting snapshot name from parent '%s'"), 3395 parent); 3396 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3397 3398 default: 3399 return (zfs_standard_error(hdl, save_errno, errbuf)); 3400 } 3401 } else { 3402 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3403 } 3404 3405 zfs_close(pzhp); 3406 return (ret); 3407 } 3408 3409 struct createdata { 3410 const char *cd_snapname; 3411 int cd_ifexists; 3412 }; 3413 3414 static int 3415 zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 3416 { 3417 struct createdata *cd = arg; 3418 int ret; 3419 3420 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3421 char name[MAXPATHLEN]; 3422 3423 (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 3424 (void) strlcat(name, "@", sizeof (name)); 3425 (void) strlcat(name, cd->cd_snapname, sizeof (name)); 3426 (void) zvol_create_link_common(zhp->zfs_hdl, name, 3427 cd->cd_ifexists); 3428 /* 3429 * NB: this is simply a best-effort. We don't want to 3430 * return an error, because then we wouldn't visit all 3431 * the volumes. 3432 */ 3433 } 3434 3435 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 3436 3437 zfs_close(zhp); 3438 3439 return (ret); 3440 } 3441 3442 /* 3443 * Takes a snapshot of the given dataset. 3444 */ 3445 int 3446 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3447 { 3448 const char *delim; 3449 char *parent; 3450 zfs_handle_t *zhp; 3451 zfs_cmd_t zc = { 0 }; 3452 int ret; 3453 char errbuf[1024]; 3454 3455 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3456 "cannot snapshot '%s'"), path); 3457 3458 /* validate the target name */ 3459 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT)) 3460 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3461 3462 /* make sure the parent exists and is of the appropriate type */ 3463 delim = strchr(path, '@'); 3464 if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 3465 return (-1); 3466 (void) strncpy(parent, path, delim - path); 3467 parent[delim - path] = '\0'; 3468 3469 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3470 ZFS_TYPE_VOLUME)) == NULL) { 3471 free(parent); 3472 return (-1); 3473 } 3474 3475 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3476 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 3477 if (ZFS_IS_VOLUME(zhp)) 3478 zc.zc_objset_type = DMU_OST_ZVOL; 3479 else 3480 zc.zc_objset_type = DMU_OST_ZFS; 3481 zc.zc_cookie = recursive; 3482 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 3483 3484 /* 3485 * if it was recursive, the one that actually failed will be in 3486 * zc.zc_name. 3487 */ 3488 if (ret != 0) 3489 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3490 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 3491 3492 if (ret == 0 && recursive) { 3493 struct createdata cd; 3494 3495 cd.cd_snapname = delim + 1; 3496 cd.cd_ifexists = B_FALSE; 3497 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 3498 } 3499 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 3500 ret = zvol_create_link(zhp->zfs_hdl, path); 3501 if (ret != 0) { 3502 (void) zfs_standard_error(hdl, errno, 3503 dgettext(TEXT_DOMAIN, 3504 "Volume successfully snapshotted, but device links " 3505 "were not created")); 3506 free(parent); 3507 zfs_close(zhp); 3508 return (-1); 3509 } 3510 } 3511 3512 if (ret != 0) 3513 (void) zfs_standard_error(hdl, errno, errbuf); 3514 3515 free(parent); 3516 zfs_close(zhp); 3517 3518 return (ret); 3519 } 3520 3521 /* 3522 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 3523 * NULL) to the file descriptor specified by outfd. 3524 */ 3525 int 3526 zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3527 { 3528 zfs_cmd_t zc = { 0 }; 3529 char errbuf[1024]; 3530 libzfs_handle_t *hdl = zhp->zfs_hdl; 3531 3532 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3533 3534 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3535 if (fromsnap) 3536 (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 3537 zc.zc_cookie = outfd; 3538 3539 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 3540 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3541 "cannot send '%s'"), zhp->zfs_name); 3542 3543 switch (errno) { 3544 3545 case EXDEV: 3546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3547 "not an earlier snapshot from the same fs")); 3548 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3549 3550 case EDQUOT: 3551 case EFBIG: 3552 case EIO: 3553 case ENOLINK: 3554 case ENOSPC: 3555 case ENOSTR: 3556 case ENXIO: 3557 case EPIPE: 3558 case ERANGE: 3559 case EFAULT: 3560 case EROFS: 3561 zfs_error_aux(hdl, strerror(errno)); 3562 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3563 3564 default: 3565 return (zfs_standard_error(hdl, errno, errbuf)); 3566 } 3567 } 3568 3569 return (0); 3570 } 3571 3572 /* 3573 * Create ancestors of 'target', but not target itself, and not 3574 * ancestors whose names are shorter than prefixlen. Die if 3575 * prefixlen-ancestor does not exist. 3576 */ 3577 static int 3578 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 3579 { 3580 zfs_handle_t *h; 3581 char *cp; 3582 3583 /* make sure prefix exists */ 3584 cp = strchr(target + prefixlen, '/'); 3585 if (cp == NULL) { 3586 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3587 } else { 3588 *cp = '\0'; 3589 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3590 *cp = '/'; 3591 } 3592 if (h == NULL) 3593 return (-1); 3594 zfs_close(h); 3595 3596 /* 3597 * Attempt to create, mount, and share any ancestor filesystems, 3598 * up to the prefixlen-long one. 3599 */ 3600 for (cp = target + prefixlen + 1; 3601 cp = strchr(cp, '/'); *cp = '/', cp++) { 3602 const char *opname; 3603 char *logstr; 3604 3605 *cp = '\0'; 3606 3607 h = make_dataset_handle(hdl, target); 3608 if (h) { 3609 /* it already exists, nothing to do here */ 3610 zfs_close(h); 3611 continue; 3612 } 3613 3614 opname = dgettext(TEXT_DOMAIN, "create"); 3615 logstr = hdl->libzfs_log_str; 3616 hdl->libzfs_log_str = NULL; 3617 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 3618 NULL) != 0) { 3619 hdl->libzfs_log_str = logstr; 3620 goto ancestorerr; 3621 } 3622 3623 hdl->libzfs_log_str = logstr; 3624 opname = dgettext(TEXT_DOMAIN, "open"); 3625 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3626 if (h == NULL) 3627 goto ancestorerr; 3628 3629 opname = dgettext(TEXT_DOMAIN, "mount"); 3630 if (zfs_mount(h, NULL, 0) != 0) 3631 goto ancestorerr; 3632 3633 opname = dgettext(TEXT_DOMAIN, "share"); 3634 if (zfs_share(h) != 0) 3635 goto ancestorerr; 3636 3637 zfs_close(h); 3638 3639 continue; 3640 ancestorerr: 3641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3642 "failed to %s ancestor '%s'"), opname, target); 3643 return (-1); 3644 } 3645 3646 return (0); 3647 } 3648 3649 /* 3650 * Restores a backup of tosnap from the file descriptor specified by infd. 3651 */ 3652 int 3653 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 3654 int verbose, int dryrun, boolean_t force, int infd) 3655 { 3656 zfs_cmd_t zc = { 0 }; 3657 time_t begin_time; 3658 int ioctl_err, err, bytes, size, choplen; 3659 char *cp; 3660 dmu_replay_record_t drr; 3661 struct drr_begin *drrb = &zc.zc_begin_record; 3662 char errbuf[1024]; 3663 prop_changelist_t *clp; 3664 char chopprefix[ZFS_MAXNAMELEN]; 3665 3666 begin_time = time(NULL); 3667 3668 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3669 "cannot receive")); 3670 3671 /* read in the BEGIN record */ 3672 cp = (char *)&drr; 3673 bytes = 0; 3674 do { 3675 size = read(infd, cp, sizeof (drr) - bytes); 3676 cp += size; 3677 bytes += size; 3678 } while (size > 0); 3679 3680 if (size < 0 || bytes != sizeof (drr)) { 3681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3682 "stream (failed to read first record)")); 3683 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3684 } 3685 3686 zc.zc_begin_record = drr.drr_u.drr_begin; 3687 3688 if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3689 drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 3690 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3691 "stream (bad magic number)")); 3692 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3693 } 3694 3695 if (drrb->drr_version != DMU_BACKUP_VERSION && 3696 drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 3697 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 3698 "0x%llx is supported (stream is version 0x%llx)"), 3699 DMU_BACKUP_VERSION, drrb->drr_version); 3700 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3701 } 3702 3703 if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 3704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3705 "stream (bad snapshot name)")); 3706 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3707 } 3708 /* 3709 * Determine how much of the snapshot name stored in the stream 3710 * we are going to tack on to the name they specified on the 3711 * command line, and how much we are going to chop off. 3712 * 3713 * If they specified a snapshot, chop the entire name stored in 3714 * the stream. 3715 */ 3716 (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3717 if (isprefix) { 3718 /* 3719 * They specified a fs with -d, we want to tack on 3720 * everything but the pool name stored in the stream 3721 */ 3722 if (strchr(tosnap, '@')) { 3723 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3724 "argument - snapshot not allowed with -d")); 3725 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3726 } 3727 cp = strchr(chopprefix, '/'); 3728 if (cp == NULL) 3729 cp = strchr(chopprefix, '@'); 3730 *cp = '\0'; 3731 } else if (strchr(tosnap, '@') == NULL) { 3732 /* 3733 * If they specified a filesystem without -d, we want to 3734 * tack on everything after the fs specified in the 3735 * first name from the stream. 3736 */ 3737 cp = strchr(chopprefix, '@'); 3738 *cp = '\0'; 3739 } 3740 choplen = strlen(chopprefix); 3741 3742 /* 3743 * Determine name of destination snapshot, store in zc_value. 3744 */ 3745 (void) strcpy(zc.zc_value, tosnap); 3746 (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 3747 sizeof (zc.zc_value)); 3748 if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT)) 3749 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3750 3751 (void) strcpy(zc.zc_name, zc.zc_value); 3752 if (drrb->drr_fromguid) { 3753 /* incremental backup stream */ 3754 zfs_handle_t *h; 3755 3756 /* do the recvbackup ioctl to the containing fs */ 3757 *strchr(zc.zc_name, '@') = '\0'; 3758 3759 /* make sure destination fs exists */ 3760 h = zfs_open(hdl, zc.zc_name, 3761 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3762 if (h == NULL) 3763 return (-1); 3764 if (!dryrun) { 3765 /* 3766 * We need to unmount all the dependents of the dataset 3767 * and the dataset itself. If it's a volume 3768 * then remove device link. 3769 */ 3770 if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 3771 clp = changelist_gather(h, ZFS_PROP_NAME, 0); 3772 if (clp == NULL) 3773 return (-1); 3774 if (changelist_prefix(clp) != 0) { 3775 changelist_free(clp); 3776 return (-1); 3777 } 3778 } else { 3779 if (zvol_remove_link(hdl, h->zfs_name) != 0) { 3780 zfs_close(h); 3781 return (-1); 3782 } 3783 3784 } 3785 } 3786 zfs_close(h); 3787 } else { 3788 /* full backup stream */ 3789 3790 /* Make sure destination fs does not exist */ 3791 *strchr(zc.zc_name, '@') = '\0'; 3792 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 3793 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3794 "destination '%s' exists"), zc.zc_name); 3795 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3796 } 3797 3798 if (strchr(zc.zc_name, '/') == NULL) { 3799 /* 3800 * they're trying to do a recv into a 3801 * nonexistant topmost filesystem. 3802 */ 3803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3804 "destination does not exist"), zc.zc_name); 3805 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3806 } 3807 3808 /* Do the recvbackup ioctl to the fs's parent. */ 3809 *strrchr(zc.zc_name, '/') = '\0'; 3810 3811 if (isprefix && (err = create_parents(hdl, 3812 zc.zc_value, strlen(tosnap))) != 0) { 3813 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 3814 } 3815 3816 } 3817 3818 zc.zc_cookie = infd; 3819 zc.zc_guid = force; 3820 if (verbose) { 3821 (void) printf("%s %s stream of %s into %s\n", 3822 dryrun ? "would receive" : "receiving", 3823 drrb->drr_fromguid ? "incremental" : "full", 3824 drr.drr_u.drr_begin.drr_toname, 3825 zc.zc_value); 3826 (void) fflush(stdout); 3827 } 3828 if (dryrun) 3829 return (0); 3830 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3831 if (ioctl_err != 0) { 3832 switch (errno) { 3833 case ENODEV: 3834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3835 "most recent snapshot does not match incremental " 3836 "source")); 3837 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3838 break; 3839 case ETXTBSY: 3840 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3841 "destination has been modified since most recent " 3842 "snapshot")); 3843 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3844 break; 3845 case EEXIST: 3846 if (drrb->drr_fromguid == 0) { 3847 /* it's the containing fs that exists */ 3848 cp = strchr(zc.zc_value, '@'); 3849 *cp = '\0'; 3850 } 3851 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3852 "destination already exists")); 3853 (void) zfs_error_fmt(hdl, EZFS_EXISTS, 3854 dgettext(TEXT_DOMAIN, "cannot restore to %s"), 3855 zc.zc_value); 3856 break; 3857 case EINVAL: 3858 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3859 break; 3860 case ECKSUM: 3861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3862 "invalid stream (checksum mismatch)")); 3863 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3864 break; 3865 default: 3866 (void) zfs_standard_error(hdl, errno, errbuf); 3867 } 3868 } 3869 3870 /* 3871 * Mount or recreate the /dev links for the target filesystem 3872 * (if created, or if we tore them down to do an incremental 3873 * restore), and the /dev links for the new snapshot (if 3874 * created). Also mount any children of the target filesystem 3875 * if we did an incremental receive. 3876 */ 3877 cp = strchr(zc.zc_value, '@'); 3878 if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3879 zfs_handle_t *h; 3880 3881 *cp = '\0'; 3882 h = zfs_open(hdl, zc.zc_value, 3883 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3884 *cp = '@'; 3885 if (h) { 3886 if (h->zfs_type == ZFS_TYPE_VOLUME) { 3887 err = zvol_create_link(hdl, h->zfs_name); 3888 if (err == 0 && ioctl_err == 0) 3889 err = zvol_create_link(hdl, 3890 zc.zc_value); 3891 } else { 3892 if (drrb->drr_fromguid) { 3893 err = changelist_postfix(clp); 3894 changelist_free(clp); 3895 } else { 3896 err = zfs_mount(h, NULL, 0); 3897 } 3898 } 3899 zfs_close(h); 3900 } 3901 } 3902 3903 if (err || ioctl_err) 3904 return (-1); 3905 3906 if (verbose) { 3907 char buf1[64]; 3908 char buf2[64]; 3909 uint64_t bytes = zc.zc_cookie; 3910 time_t delta = time(NULL) - begin_time; 3911 if (delta == 0) 3912 delta = 1; 3913 zfs_nicenum(bytes, buf1, sizeof (buf1)); 3914 zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3915 3916 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 3917 buf1, delta, buf2); 3918 } 3919 3920 return (0); 3921 } 3922 3923 /* 3924 * Destroy any more recent snapshots. We invoke this callback on any dependents 3925 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3926 * is a dependent and we should just destroy it without checking the transaction 3927 * group. 3928 */ 3929 typedef struct rollback_data { 3930 const char *cb_target; /* the snapshot */ 3931 uint64_t cb_create; /* creation time reference */ 3932 prop_changelist_t *cb_clp; /* changelist pointer */ 3933 int cb_error; 3934 boolean_t cb_dependent; 3935 } rollback_data_t; 3936 3937 static int 3938 rollback_destroy(zfs_handle_t *zhp, void *data) 3939 { 3940 rollback_data_t *cbp = data; 3941 3942 if (!cbp->cb_dependent) { 3943 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 3944 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 3945 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 3946 cbp->cb_create) { 3947 char *logstr; 3948 3949 cbp->cb_dependent = B_TRUE; 3950 if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 3951 cbp) != 0) 3952 cbp->cb_error = 1; 3953 cbp->cb_dependent = B_FALSE; 3954 3955 logstr = zhp->zfs_hdl->libzfs_log_str; 3956 zhp->zfs_hdl->libzfs_log_str = NULL; 3957 if (zfs_destroy(zhp) != 0) 3958 cbp->cb_error = 1; 3959 else 3960 changelist_remove(zhp, cbp->cb_clp); 3961 zhp->zfs_hdl->libzfs_log_str = logstr; 3962 } 3963 } else { 3964 if (zfs_destroy(zhp) != 0) 3965 cbp->cb_error = 1; 3966 else 3967 changelist_remove(zhp, cbp->cb_clp); 3968 } 3969 3970 zfs_close(zhp); 3971 return (0); 3972 } 3973 3974 /* 3975 * Rollback the dataset to its latest snapshot. 3976 */ 3977 static int 3978 do_rollback(zfs_handle_t *zhp) 3979 { 3980 int ret; 3981 zfs_cmd_t zc = { 0 }; 3982 3983 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3984 zhp->zfs_type == ZFS_TYPE_VOLUME); 3985 3986 if (zhp->zfs_type == ZFS_TYPE_VOLUME && 3987 zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3988 return (-1); 3989 3990 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3991 3992 if (ZFS_IS_VOLUME(zhp)) 3993 zc.zc_objset_type = DMU_OST_ZVOL; 3994 else 3995 zc.zc_objset_type = DMU_OST_ZFS; 3996 3997 /* 3998 * We rely on the consumer to verify that there are no newer snapshots 3999 * for the given dataset. Given these constraints, we can simply pass 4000 * the name on to the ioctl() call. There is still an unlikely race 4001 * condition where the user has taken a snapshot since we verified that 4002 * this was the most recent. 4003 */ 4004 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 4005 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 4006 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 4007 zhp->zfs_name); 4008 } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 4009 ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 4010 } 4011 4012 return (ret); 4013 } 4014 4015 /* 4016 * Given a dataset, rollback to a specific snapshot, discarding any 4017 * data changes since then and making it the active dataset. 4018 * 4019 * Any snapshots more recent than the target are destroyed, along with 4020 * their dependents. 4021 */ 4022 int 4023 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 4024 { 4025 int ret; 4026 rollback_data_t cb = { 0 }; 4027 prop_changelist_t *clp; 4028 4029 /* 4030 * Unmount all dependendents of the dataset and the dataset itself. 4031 * The list we need to gather is the same as for doing rename 4032 */ 4033 clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 4034 if (clp == NULL) 4035 return (-1); 4036 4037 if ((ret = changelist_prefix(clp)) != 0) 4038 goto out; 4039 4040 /* 4041 * Destroy all recent snapshots and its dependends. 4042 */ 4043 cb.cb_target = snap->zfs_name; 4044 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 4045 cb.cb_clp = clp; 4046 (void) zfs_iter_children(zhp, rollback_destroy, &cb); 4047 4048 if ((ret = cb.cb_error) != 0) { 4049 (void) changelist_postfix(clp); 4050 goto out; 4051 } 4052 4053 /* 4054 * Now that we have verified that the snapshot is the latest, 4055 * rollback to the given snapshot. 4056 */ 4057 ret = do_rollback(zhp); 4058 4059 if (ret != 0) { 4060 (void) changelist_postfix(clp); 4061 goto out; 4062 } 4063 4064 /* 4065 * We only want to re-mount the filesystem if it was mounted in the 4066 * first place. 4067 */ 4068 ret = changelist_postfix(clp); 4069 4070 out: 4071 changelist_free(clp); 4072 return (ret); 4073 } 4074 4075 /* 4076 * Iterate over all dependents for a given dataset. This includes both 4077 * hierarchical dependents (children) and data dependents (snapshots and 4078 * clones). The bulk of the processing occurs in get_dependents() in 4079 * libzfs_graph.c. 4080 */ 4081 int 4082 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 4083 zfs_iter_f func, void *data) 4084 { 4085 char **dependents; 4086 size_t count; 4087 int i; 4088 zfs_handle_t *child; 4089 int ret = 0; 4090 4091 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 4092 &dependents, &count) != 0) 4093 return (-1); 4094 4095 for (i = 0; i < count; i++) { 4096 if ((child = make_dataset_handle(zhp->zfs_hdl, 4097 dependents[i])) == NULL) 4098 continue; 4099 4100 if ((ret = func(child, data)) != 0) 4101 break; 4102 } 4103 4104 for (i = 0; i < count; i++) 4105 free(dependents[i]); 4106 free(dependents); 4107 4108 return (ret); 4109 } 4110 4111 /* 4112 * Renames the given dataset. 4113 */ 4114 int 4115 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 4116 { 4117 int ret; 4118 zfs_cmd_t zc = { 0 }; 4119 char *delim; 4120 prop_changelist_t *cl = NULL; 4121 zfs_handle_t *zhrp = NULL; 4122 char *parentname = NULL; 4123 char parent[ZFS_MAXNAMELEN]; 4124 libzfs_handle_t *hdl = zhp->zfs_hdl; 4125 char errbuf[1024]; 4126 4127 /* if we have the same exact name, just return success */ 4128 if (strcmp(zhp->zfs_name, target) == 0) 4129 return (0); 4130 4131 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4132 "cannot rename to '%s'"), target); 4133 4134 /* 4135 * Make sure the target name is valid 4136 */ 4137 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 4138 if ((strchr(target, '@') == NULL) || 4139 *target == '@') { 4140 /* 4141 * Snapshot target name is abbreviated, 4142 * reconstruct full dataset name 4143 */ 4144 (void) strlcpy(parent, zhp->zfs_name, 4145 sizeof (parent)); 4146 delim = strchr(parent, '@'); 4147 if (strchr(target, '@') == NULL) 4148 *(++delim) = '\0'; 4149 else 4150 *delim = '\0'; 4151 (void) strlcat(parent, target, sizeof (parent)); 4152 target = parent; 4153 } else { 4154 /* 4155 * Make sure we're renaming within the same dataset. 4156 */ 4157 delim = strchr(target, '@'); 4158 if (strncmp(zhp->zfs_name, target, delim - target) 4159 != 0 || zhp->zfs_name[delim - target] != '@') { 4160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4161 "snapshots must be part of same " 4162 "dataset")); 4163 return (zfs_error(hdl, EZFS_CROSSTARGET, 4164 errbuf)); 4165 } 4166 } 4167 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 4168 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4169 } else { 4170 if (recursive) { 4171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4172 "recursive rename must be a snapshot")); 4173 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4174 } 4175 4176 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 4177 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4178 uint64_t unused; 4179 4180 /* validate parents */ 4181 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4182 return (-1); 4183 4184 (void) parent_name(target, parent, sizeof (parent)); 4185 4186 /* make sure we're in the same pool */ 4187 verify((delim = strchr(target, '/')) != NULL); 4188 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4189 zhp->zfs_name[delim - target] != '/') { 4190 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4191 "datasets must be within same pool")); 4192 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4193 } 4194 4195 /* new name cannot be a child of the current dataset name */ 4196 if (strncmp(parent, zhp->zfs_name, 4197 strlen(zhp->zfs_name)) == 0) { 4198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4199 "New dataset name cannot be a descendent of " 4200 "current dataset name")); 4201 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4202 } 4203 } 4204 4205 (void) snprintf(errbuf, sizeof (errbuf), 4206 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 4207 4208 if (getzoneid() == GLOBAL_ZONEID && 4209 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 4210 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4211 "dataset is used in a non-global zone")); 4212 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4213 } 4214 4215 if (recursive) { 4216 struct destroydata dd; 4217 4218 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 4219 if (parentname == NULL) { 4220 ret = -1; 4221 goto error; 4222 } 4223 delim = strchr(parentname, '@'); 4224 *delim = '\0'; 4225 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY); 4226 if (zhrp == NULL) { 4227 ret = -1; 4228 goto error; 4229 } 4230 4231 dd.snapname = delim + 1; 4232 dd.gotone = B_FALSE; 4233 dd.closezhp = B_TRUE; 4234 4235 /* We remove any zvol links prior to renaming them */ 4236 ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 4237 if (ret) { 4238 goto error; 4239 } 4240 } else { 4241 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 4242 return (-1); 4243 4244 if (changelist_haszonedchild(cl)) { 4245 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4246 "child dataset with inherited mountpoint is used " 4247 "in a non-global zone")); 4248 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 4249 goto error; 4250 } 4251 4252 if ((ret = changelist_prefix(cl)) != 0) 4253 goto error; 4254 } 4255 4256 if (ZFS_IS_VOLUME(zhp)) 4257 zc.zc_objset_type = DMU_OST_ZVOL; 4258 else 4259 zc.zc_objset_type = DMU_OST_ZFS; 4260 4261 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4262 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 4263 4264 zc.zc_cookie = recursive; 4265 4266 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 4267 /* 4268 * if it was recursive, the one that actually failed will 4269 * be in zc.zc_name 4270 */ 4271 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4272 "cannot rename to '%s'"), zc.zc_name); 4273 4274 if (recursive && errno == EEXIST) { 4275 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4276 "a child dataset already has a snapshot " 4277 "with the new name")); 4278 (void) zfs_error(hdl, EZFS_CROSSTARGET, errbuf); 4279 } else { 4280 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 4281 } 4282 4283 /* 4284 * On failure, we still want to remount any filesystems that 4285 * were previously mounted, so we don't alter the system state. 4286 */ 4287 if (recursive) { 4288 struct createdata cd; 4289 4290 /* only create links for datasets that had existed */ 4291 cd.cd_snapname = delim + 1; 4292 cd.cd_ifexists = B_TRUE; 4293 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 4294 &cd); 4295 } else { 4296 (void) changelist_postfix(cl); 4297 } 4298 } else { 4299 if (recursive) { 4300 struct createdata cd; 4301 4302 /* only create links for datasets that had existed */ 4303 cd.cd_snapname = strchr(target, '@') + 1; 4304 cd.cd_ifexists = B_TRUE; 4305 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 4306 &cd); 4307 } else { 4308 changelist_rename(cl, zfs_get_name(zhp), target); 4309 ret = changelist_postfix(cl); 4310 } 4311 } 4312 4313 error: 4314 if (parentname) { 4315 free(parentname); 4316 } 4317 if (zhrp) { 4318 zfs_close(zhrp); 4319 } 4320 if (cl) { 4321 changelist_free(cl); 4322 } 4323 return (ret); 4324 } 4325 4326 /* 4327 * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4328 * poke devfsadm to create the /dev link, and then wait for the link to appear. 4329 */ 4330 int 4331 zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4332 { 4333 return (zvol_create_link_common(hdl, dataset, B_FALSE)); 4334 } 4335 4336 static int 4337 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 4338 { 4339 zfs_cmd_t zc = { 0 }; 4340 di_devlink_handle_t dhdl; 4341 priv_set_t *priv_effective; 4342 int privileged; 4343 4344 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4345 4346 /* 4347 * Issue the appropriate ioctl. 4348 */ 4349 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4350 switch (errno) { 4351 case EEXIST: 4352 /* 4353 * Silently ignore the case where the link already 4354 * exists. This allows 'zfs volinit' to be run multiple 4355 * times without errors. 4356 */ 4357 return (0); 4358 4359 case ENOENT: 4360 /* 4361 * Dataset does not exist in the kernel. If we 4362 * don't care (see zfs_rename), then ignore the 4363 * error quietly. 4364 */ 4365 if (ifexists) { 4366 return (0); 4367 } 4368 4369 /* FALLTHROUGH */ 4370 4371 default: 4372 return (zfs_standard_error_fmt(hdl, errno, 4373 dgettext(TEXT_DOMAIN, "cannot create device links " 4374 "for '%s'"), dataset)); 4375 } 4376 } 4377 4378 /* 4379 * If privileged call devfsadm and wait for the links to 4380 * magically appear. 4381 * Otherwise, print out an informational message. 4382 */ 4383 4384 priv_effective = priv_allocset(); 4385 (void) getppriv(PRIV_EFFECTIVE, priv_effective); 4386 privileged = (priv_isfullset(priv_effective) == B_TRUE); 4387 priv_freeset(priv_effective); 4388 4389 if (privileged) { 4390 if ((dhdl = di_devlink_init(ZFS_DRIVER, 4391 DI_MAKE_LINK)) == NULL) { 4392 zfs_error_aux(hdl, strerror(errno)); 4393 (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 4394 dgettext(TEXT_DOMAIN, "cannot create device links " 4395 "for '%s'"), dataset); 4396 (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 4397 return (-1); 4398 } else { 4399 (void) di_devlink_fini(&dhdl); 4400 } 4401 } else { 4402 char pathname[MAXPATHLEN]; 4403 struct stat64 statbuf; 4404 int i; 4405 4406 #define MAX_WAIT 10 4407 4408 /* 4409 * This is the poor mans way of waiting for the link 4410 * to show up. If after 10 seconds we still don't 4411 * have it, then print out a message. 4412 */ 4413 (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 4414 dataset); 4415 4416 for (i = 0; i != MAX_WAIT; i++) { 4417 if (stat64(pathname, &statbuf) == 0) 4418 break; 4419 (void) sleep(1); 4420 } 4421 if (i == MAX_WAIT) 4422 (void) printf(gettext("%s may not be immediately " 4423 "available\n"), pathname); 4424 } 4425 4426 return (0); 4427 } 4428 4429 /* 4430 * Remove a minor node for the given zvol and the associated /dev links. 4431 */ 4432 int 4433 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4434 { 4435 zfs_cmd_t zc = { 0 }; 4436 4437 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4438 4439 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4440 switch (errno) { 4441 case ENXIO: 4442 /* 4443 * Silently ignore the case where the link no longer 4444 * exists, so that 'zfs volfini' can be run multiple 4445 * times without errors. 4446 */ 4447 return (0); 4448 4449 default: 4450 return (zfs_standard_error_fmt(hdl, errno, 4451 dgettext(TEXT_DOMAIN, "cannot remove device " 4452 "links for '%s'"), dataset)); 4453 } 4454 } 4455 4456 return (0); 4457 } 4458 4459 nvlist_t * 4460 zfs_get_user_props(zfs_handle_t *zhp) 4461 { 4462 return (zhp->zfs_user_props); 4463 } 4464 4465 /* 4466 * Given a comma-separated list of properties, construct a property list 4467 * containing both user-defined and native properties. This function will 4468 * return a NULL list if 'all' is specified, which can later be expanded on a 4469 * per-dataset basis by zfs_expand_proplist(). 4470 */ 4471 int 4472 zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields, 4473 zfs_proplist_t **listp, zfs_type_t type) 4474 { 4475 size_t len; 4476 char *s, *p; 4477 char c; 4478 zfs_prop_t prop; 4479 zfs_proplist_t *entry; 4480 zfs_proplist_t **last; 4481 4482 *listp = NULL; 4483 last = listp; 4484 4485 /* 4486 * If 'all' is specified, return a NULL list. 4487 */ 4488 if (strcmp(fields, "all") == 0) 4489 return (0); 4490 4491 /* 4492 * If no fields were specified, return an error. 4493 */ 4494 if (fields[0] == '\0') { 4495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4496 "no properties specified")); 4497 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 4498 "bad property list"))); 4499 } 4500 4501 /* 4502 * It would be nice to use getsubopt() here, but the inclusion of column 4503 * aliases makes this more effort than it's worth. 4504 */ 4505 s = fields; 4506 while (*s != '\0') { 4507 if ((p = strchr(s, ',')) == NULL) { 4508 len = strlen(s); 4509 p = s + len; 4510 } else { 4511 len = p - s; 4512 } 4513 4514 /* 4515 * Check for empty options. 4516 */ 4517 if (len == 0) { 4518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4519 "empty property name")); 4520 return (zfs_error(hdl, EZFS_BADPROP, 4521 dgettext(TEXT_DOMAIN, "bad property list"))); 4522 } 4523 4524 /* 4525 * Check all regular property names. 4526 */ 4527 c = s[len]; 4528 s[len] = '\0'; 4529 prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) : 4530 zfs_name_to_prop(s); 4531 4532 if (prop != ZFS_PROP_INVAL && 4533 !zfs_prop_valid_for_type(prop, type)) 4534 prop = ZFS_PROP_INVAL; 4535 4536 /* 4537 * When no property table entry can be found, return failure if 4538 * this is a pool property or if this isn't a user-defined 4539 * dataset property, 4540 */ 4541 if (prop == ZFS_PROP_INVAL && 4542 (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) { 4543 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4544 "invalid property '%s'"), s); 4545 return (zfs_error(hdl, EZFS_BADPROP, 4546 dgettext(TEXT_DOMAIN, "bad property list"))); 4547 } 4548 4549 if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL) 4550 return (-1); 4551 4552 entry->pl_prop = prop; 4553 if (prop == ZFS_PROP_INVAL) { 4554 if ((entry->pl_user_prop = 4555 zfs_strdup(hdl, s)) == NULL) { 4556 free(entry); 4557 return (-1); 4558 } 4559 entry->pl_width = strlen(s); 4560 } else { 4561 entry->pl_width = zfs_prop_width(prop, 4562 &entry->pl_fixed); 4563 } 4564 4565 *last = entry; 4566 last = &entry->pl_next; 4567 4568 s = p; 4569 if (c == ',') 4570 s++; 4571 } 4572 4573 return (0); 4574 } 4575 4576 int 4577 zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp) 4578 { 4579 return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY)); 4580 } 4581 4582 void 4583 zfs_free_proplist(zfs_proplist_t *pl) 4584 { 4585 zfs_proplist_t *next; 4586 4587 while (pl != NULL) { 4588 next = pl->pl_next; 4589 free(pl->pl_user_prop); 4590 free(pl); 4591 pl = next; 4592 } 4593 } 4594 4595 typedef struct expand_data { 4596 zfs_proplist_t **last; 4597 libzfs_handle_t *hdl; 4598 } expand_data_t; 4599 4600 static zfs_prop_t 4601 zfs_expand_proplist_cb(zfs_prop_t prop, void *cb) 4602 { 4603 zfs_proplist_t *entry; 4604 expand_data_t *edp = cb; 4605 4606 if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL) 4607 return (ZFS_PROP_INVAL); 4608 4609 entry->pl_prop = prop; 4610 entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed); 4611 entry->pl_all = B_TRUE; 4612 4613 *(edp->last) = entry; 4614 edp->last = &entry->pl_next; 4615 4616 return (ZFS_PROP_CONT); 4617 } 4618 4619 int 4620 zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp, 4621 zfs_type_t type) 4622 { 4623 zfs_proplist_t *entry; 4624 zfs_proplist_t **last; 4625 expand_data_t exp; 4626 4627 if (*plp == NULL) { 4628 /* 4629 * If this is the very first time we've been called for an 'all' 4630 * specification, expand the list to include all native 4631 * properties. 4632 */ 4633 last = plp; 4634 4635 exp.last = last; 4636 exp.hdl = hdl; 4637 4638 if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type, 4639 B_FALSE, B_FALSE) == ZFS_PROP_INVAL) 4640 return (-1); 4641 4642 /* 4643 * Add 'name' to the beginning of the list, which is handled 4644 * specially. 4645 */ 4646 if ((entry = zfs_alloc(hdl, 4647 sizeof (zfs_proplist_t))) == NULL) 4648 return (-1); 4649 4650 entry->pl_prop = ZFS_PROP_NAME; 4651 entry->pl_width = zfs_prop_width(ZFS_PROP_NAME, 4652 &entry->pl_fixed); 4653 entry->pl_all = B_TRUE; 4654 entry->pl_next = *plp; 4655 *plp = entry; 4656 } 4657 return (0); 4658 } 4659 4660 /* 4661 * This function is used by 'zfs list' to determine the exact set of columns to 4662 * display, and their maximum widths. This does two main things: 4663 * 4664 * - If this is a list of all properties, then expand the list to include 4665 * all native properties, and set a flag so that for each dataset we look 4666 * for new unique user properties and add them to the list. 4667 * 4668 * - For non fixed-width properties, keep track of the maximum width seen 4669 * so that we can size the column appropriately. 4670 */ 4671 int 4672 zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp) 4673 { 4674 libzfs_handle_t *hdl = zhp->zfs_hdl; 4675 zfs_proplist_t *entry; 4676 zfs_proplist_t **last, **start; 4677 nvlist_t *userprops, *propval; 4678 nvpair_t *elem; 4679 char *strval; 4680 char buf[ZFS_MAXPROPLEN]; 4681 4682 if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0) 4683 return (-1); 4684 4685 userprops = zfs_get_user_props(zhp); 4686 4687 entry = *plp; 4688 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 4689 /* 4690 * Go through and add any user properties as necessary. We 4691 * start by incrementing our list pointer to the first 4692 * non-native property. 4693 */ 4694 start = plp; 4695 while (*start != NULL) { 4696 if ((*start)->pl_prop == ZFS_PROP_INVAL) 4697 break; 4698 start = &(*start)->pl_next; 4699 } 4700 4701 elem = NULL; 4702 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 4703 /* 4704 * See if we've already found this property in our list. 4705 */ 4706 for (last = start; *last != NULL; 4707 last = &(*last)->pl_next) { 4708 if (strcmp((*last)->pl_user_prop, 4709 nvpair_name(elem)) == 0) 4710 break; 4711 } 4712 4713 if (*last == NULL) { 4714 if ((entry = zfs_alloc(hdl, 4715 sizeof (zfs_proplist_t))) == NULL || 4716 ((entry->pl_user_prop = zfs_strdup(hdl, 4717 nvpair_name(elem)))) == NULL) { 4718 free(entry); 4719 return (-1); 4720 } 4721 4722 entry->pl_prop = ZFS_PROP_INVAL; 4723 entry->pl_width = strlen(nvpair_name(elem)); 4724 entry->pl_all = B_TRUE; 4725 *last = entry; 4726 } 4727 } 4728 } 4729 4730 /* 4731 * Now go through and check the width of any non-fixed columns 4732 */ 4733 for (entry = *plp; entry != NULL; entry = entry->pl_next) { 4734 if (entry->pl_fixed) 4735 continue; 4736 4737 if (entry->pl_prop != ZFS_PROP_INVAL) { 4738 if (zfs_prop_get(zhp, entry->pl_prop, 4739 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 4740 if (strlen(buf) > entry->pl_width) 4741 entry->pl_width = strlen(buf); 4742 } 4743 } else if (nvlist_lookup_nvlist(userprops, 4744 entry->pl_user_prop, &propval) == 0) { 4745 verify(nvlist_lookup_string(propval, 4746 ZFS_PROP_VALUE, &strval) == 0); 4747 if (strlen(strval) > entry->pl_width) 4748 entry->pl_width = strlen(strval); 4749 } 4750 } 4751 4752 return (0); 4753 } 4754 4755 int 4756 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 4757 { 4758 zfs_cmd_t zc = { 0 }; 4759 nvlist_t *nvp; 4760 size_t sz; 4761 gid_t gid; 4762 uid_t uid; 4763 const gid_t *groups; 4764 int group_cnt; 4765 int error; 4766 4767 if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 4768 return (no_memory(hdl)); 4769 4770 uid = ucred_geteuid(cred); 4771 gid = ucred_getegid(cred); 4772 group_cnt = ucred_getgroups(cred, &groups); 4773 4774 if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 4775 return (1); 4776 4777 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 4778 nvlist_free(nvp); 4779 return (1); 4780 } 4781 4782 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 4783 nvlist_free(nvp); 4784 return (1); 4785 } 4786 4787 if (nvlist_add_uint32_array(nvp, 4788 ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 4789 nvlist_free(nvp); 4790 return (1); 4791 } 4792 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4793 4794 if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz)) 4795 return (-1); 4796 4797 error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 4798 nvlist_free(nvp); 4799 return (error); 4800 } 4801 4802 int 4803 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4804 void *export, void *sharetab, int sharemax, boolean_t share_on) 4805 { 4806 zfs_cmd_t zc = { 0 }; 4807 int error; 4808 4809 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4810 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4811 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 4812 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4813 zc.zc_share.z_sharetype = share_on; 4814 zc.zc_share.z_sharemax = sharemax; 4815 4816 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4817 return (error); 4818 } 4819