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