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 allow gzip compression")); 1994 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 1995 break; 1996 1997 case EOVERFLOW: 1998 /* 1999 * This platform can't address a volume this big. 2000 */ 2001 #ifdef _ILP32 2002 if (prop == ZFS_PROP_VOLSIZE) { 2003 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 2004 break; 2005 } 2006 #endif 2007 /* FALLTHROUGH */ 2008 default: 2009 (void) zfs_standard_error(hdl, errno, errbuf); 2010 } 2011 } else { 2012 /* 2013 * Refresh the statistics so the new property value 2014 * is reflected. 2015 */ 2016 if ((ret = changelist_postfix(cl)) == 0) 2017 (void) get_stats(zhp); 2018 } 2019 2020 error: 2021 nvlist_free(nvl); 2022 zcmd_free_nvlists(&zc); 2023 if (cl) 2024 changelist_free(cl); 2025 return (ret); 2026 } 2027 2028 /* 2029 * Given a property, inherit the value from the parent dataset. 2030 */ 2031 int 2032 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 2033 { 2034 zfs_cmd_t zc = { 0 }; 2035 int ret; 2036 prop_changelist_t *cl; 2037 libzfs_handle_t *hdl = zhp->zfs_hdl; 2038 char errbuf[1024]; 2039 zfs_prop_t prop; 2040 2041 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2042 "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 2043 2044 if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) { 2045 /* 2046 * For user properties, the amount of work we have to do is very 2047 * small, so just do it here. 2048 */ 2049 if (!zfs_prop_user(propname)) { 2050 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2051 "invalid property")); 2052 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2053 } 2054 2055 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2056 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2057 2058 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc) != 0) 2059 return (zfs_standard_error(hdl, errno, errbuf)); 2060 2061 return (0); 2062 } 2063 2064 /* 2065 * Verify that this property is inheritable. 2066 */ 2067 if (zfs_prop_readonly(prop)) 2068 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 2069 2070 if (!zfs_prop_inheritable(prop)) 2071 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2072 2073 /* 2074 * Check to see if the value applies to this type 2075 */ 2076 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2077 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2078 2079 /* 2080 * Normalize the name, to get rid of shorthand abbrevations. 2081 */ 2082 propname = zfs_prop_to_name(prop); 2083 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2084 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2085 2086 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2087 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 2088 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2089 "dataset is used in a non-global zone")); 2090 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2091 } 2092 2093 /* 2094 * Determine datasets which will be affected by this change, if any. 2095 */ 2096 if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 2097 return (-1); 2098 2099 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 2100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2101 "child dataset with inherited mountpoint is used " 2102 "in a non-global zone")); 2103 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2104 goto error; 2105 } 2106 2107 if ((ret = changelist_prefix(cl)) != 0) 2108 goto error; 2109 2110 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc)) != 0) { 2111 return (zfs_standard_error(hdl, errno, errbuf)); 2112 } else { 2113 2114 if ((ret = changelist_postfix(cl)) != 0) 2115 goto error; 2116 2117 /* 2118 * Refresh the statistics so the new property is reflected. 2119 */ 2120 (void) get_stats(zhp); 2121 } 2122 2123 error: 2124 changelist_free(cl); 2125 return (ret); 2126 } 2127 2128 void 2129 nicebool(int value, char *buf, size_t buflen) 2130 { 2131 if (value) 2132 (void) strlcpy(buf, "on", buflen); 2133 else 2134 (void) strlcpy(buf, "off", buflen); 2135 } 2136 2137 /* 2138 * True DSL properties are stored in an nvlist. The following two functions 2139 * extract them appropriately. 2140 */ 2141 static uint64_t 2142 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 2143 { 2144 nvlist_t *nv; 2145 uint64_t value; 2146 2147 *source = NULL; 2148 if (nvlist_lookup_nvlist(zhp->zfs_props, 2149 zfs_prop_to_name(prop), &nv) == 0) { 2150 verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0); 2151 (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 2152 } else { 2153 value = zfs_prop_default_numeric(prop); 2154 *source = ""; 2155 } 2156 2157 return (value); 2158 } 2159 2160 static char * 2161 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 2162 { 2163 nvlist_t *nv; 2164 char *value; 2165 2166 *source = NULL; 2167 if (nvlist_lookup_nvlist(zhp->zfs_props, 2168 zfs_prop_to_name(prop), &nv) == 0) { 2169 verify(nvlist_lookup_string(nv, ZFS_PROP_VALUE, &value) == 0); 2170 (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 2171 } else { 2172 if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 2173 value = ""; 2174 *source = ""; 2175 } 2176 2177 return (value); 2178 } 2179 2180 /* 2181 * Internal function for getting a numeric property. Both zfs_prop_get() and 2182 * zfs_prop_get_int() are built using this interface. 2183 * 2184 * Certain properties can be overridden using 'mount -o'. In this case, scan 2185 * the contents of the /etc/mnttab entry, searching for the appropriate options. 2186 * If they differ from the on-disk values, report the current values and mark 2187 * the source "temporary". 2188 */ 2189 static int 2190 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src, 2191 char **source, uint64_t *val) 2192 { 2193 struct mnttab mnt; 2194 char *mntopt_on = NULL; 2195 char *mntopt_off = NULL; 2196 2197 *source = NULL; 2198 2199 switch (prop) { 2200 case ZFS_PROP_ATIME: 2201 mntopt_on = MNTOPT_ATIME; 2202 mntopt_off = MNTOPT_NOATIME; 2203 break; 2204 2205 case ZFS_PROP_DEVICES: 2206 mntopt_on = MNTOPT_DEVICES; 2207 mntopt_off = MNTOPT_NODEVICES; 2208 break; 2209 2210 case ZFS_PROP_EXEC: 2211 mntopt_on = MNTOPT_EXEC; 2212 mntopt_off = MNTOPT_NOEXEC; 2213 break; 2214 2215 case ZFS_PROP_READONLY: 2216 mntopt_on = MNTOPT_RO; 2217 mntopt_off = MNTOPT_RW; 2218 break; 2219 2220 case ZFS_PROP_SETUID: 2221 mntopt_on = MNTOPT_SETUID; 2222 mntopt_off = MNTOPT_NOSETUID; 2223 break; 2224 2225 case ZFS_PROP_XATTR: 2226 mntopt_on = MNTOPT_XATTR; 2227 mntopt_off = MNTOPT_NOXATTR; 2228 break; 2229 } 2230 2231 /* 2232 * Because looking up the mount options is potentially expensive 2233 * (iterating over all of /etc/mnttab), we defer its calculation until 2234 * we're looking up a property which requires its presence. 2235 */ 2236 if (!zhp->zfs_mntcheck && 2237 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 2238 struct mnttab entry, search = { 0 }; 2239 FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 2240 2241 search.mnt_special = (char *)zhp->zfs_name; 2242 search.mnt_fstype = MNTTYPE_ZFS; 2243 rewind(mnttab); 2244 2245 if (getmntany(mnttab, &entry, &search) == 0) { 2246 zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 2247 entry.mnt_mntopts); 2248 if (zhp->zfs_mntopts == NULL) 2249 return (-1); 2250 } 2251 2252 zhp->zfs_mntcheck = B_TRUE; 2253 } 2254 2255 if (zhp->zfs_mntopts == NULL) 2256 mnt.mnt_mntopts = ""; 2257 else 2258 mnt.mnt_mntopts = zhp->zfs_mntopts; 2259 2260 switch (prop) { 2261 case ZFS_PROP_ATIME: 2262 case ZFS_PROP_DEVICES: 2263 case ZFS_PROP_EXEC: 2264 case ZFS_PROP_READONLY: 2265 case ZFS_PROP_SETUID: 2266 case ZFS_PROP_XATTR: 2267 *val = getprop_uint64(zhp, prop, source); 2268 2269 if (hasmntopt(&mnt, mntopt_on) && !*val) { 2270 *val = B_TRUE; 2271 if (src) 2272 *src = ZFS_SRC_TEMPORARY; 2273 } else if (hasmntopt(&mnt, mntopt_off) && *val) { 2274 *val = B_FALSE; 2275 if (src) 2276 *src = ZFS_SRC_TEMPORARY; 2277 } 2278 break; 2279 2280 case ZFS_PROP_CANMOUNT: 2281 *val = getprop_uint64(zhp, prop, source); 2282 if (*val == 0) 2283 *source = zhp->zfs_name; 2284 else 2285 *source = ""; /* default */ 2286 break; 2287 2288 case ZFS_PROP_QUOTA: 2289 case ZFS_PROP_RESERVATION: 2290 *val = getprop_uint64(zhp, prop, source); 2291 if (*val == 0) 2292 *source = ""; /* default */ 2293 else 2294 *source = zhp->zfs_name; 2295 break; 2296 2297 case ZFS_PROP_MOUNTED: 2298 *val = (zhp->zfs_mntopts != NULL); 2299 break; 2300 2301 case ZFS_PROP_NUMCLONES: 2302 *val = zhp->zfs_dmustats.dds_num_clones; 2303 break; 2304 2305 default: 2306 switch (zfs_prop_get_type(prop)) { 2307 case prop_type_number: 2308 case prop_type_boolean: 2309 case prop_type_index: 2310 *val = getprop_uint64(zhp, prop, source); 2311 break; 2312 2313 case prop_type_string: 2314 default: 2315 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2316 "cannot get non-numeric property")); 2317 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 2318 dgettext(TEXT_DOMAIN, "internal error"))); 2319 } 2320 } 2321 2322 return (0); 2323 } 2324 2325 /* 2326 * Calculate the source type, given the raw source string. 2327 */ 2328 static void 2329 get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source, 2330 char *statbuf, size_t statlen) 2331 { 2332 if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY) 2333 return; 2334 2335 if (source == NULL) { 2336 *srctype = ZFS_SRC_NONE; 2337 } else if (source[0] == '\0') { 2338 *srctype = ZFS_SRC_DEFAULT; 2339 } else { 2340 if (strcmp(source, zhp->zfs_name) == 0) { 2341 *srctype = ZFS_SRC_LOCAL; 2342 } else { 2343 (void) strlcpy(statbuf, source, statlen); 2344 *srctype = ZFS_SRC_INHERITED; 2345 } 2346 } 2347 2348 } 2349 2350 /* 2351 * Retrieve a property from the given object. If 'literal' is specified, then 2352 * numbers are left as exact values. Otherwise, numbers are converted to a 2353 * human-readable form. 2354 * 2355 * Returns 0 on success, or -1 on error. 2356 */ 2357 int 2358 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2359 zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2360 { 2361 char *source = NULL; 2362 uint64_t val; 2363 char *str; 2364 const char *root; 2365 const char *strval; 2366 2367 /* 2368 * Check to see if this property applies to our object 2369 */ 2370 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2371 return (-1); 2372 2373 if (src) 2374 *src = ZFS_SRC_NONE; 2375 2376 switch (prop) { 2377 case ZFS_PROP_CREATION: 2378 /* 2379 * 'creation' is a time_t stored in the statistics. We convert 2380 * this into a string unless 'literal' is specified. 2381 */ 2382 { 2383 val = getprop_uint64(zhp, prop, &source); 2384 time_t time = (time_t)val; 2385 struct tm t; 2386 2387 if (literal || 2388 localtime_r(&time, &t) == NULL || 2389 strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2390 &t) == 0) 2391 (void) snprintf(propbuf, proplen, "%llu", val); 2392 } 2393 break; 2394 2395 case ZFS_PROP_MOUNTPOINT: 2396 /* 2397 * Getting the precise mountpoint can be tricky. 2398 * 2399 * - for 'none' or 'legacy', return those values. 2400 * - for default mountpoints, construct it as /zfs/<dataset> 2401 * - for inherited mountpoints, we want to take everything 2402 * after our ancestor and append it to the inherited value. 2403 * 2404 * If the pool has an alternate root, we want to prepend that 2405 * root to any values we return. 2406 */ 2407 root = zhp->zfs_root; 2408 str = getprop_string(zhp, prop, &source); 2409 2410 if (str[0] == '\0') { 2411 (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2412 root, zhp->zfs_name); 2413 } else if (str[0] == '/') { 2414 const char *relpath = zhp->zfs_name + strlen(source); 2415 2416 if (relpath[0] == '/') 2417 relpath++; 2418 if (str[1] == '\0') 2419 str++; 2420 2421 if (relpath[0] == '\0') 2422 (void) snprintf(propbuf, proplen, "%s%s", 2423 root, str); 2424 else 2425 (void) snprintf(propbuf, proplen, "%s%s%s%s", 2426 root, str, relpath[0] == '@' ? "" : "/", 2427 relpath); 2428 } else { 2429 /* 'legacy' or 'none' */ 2430 (void) strlcpy(propbuf, str, proplen); 2431 } 2432 2433 break; 2434 2435 case ZFS_PROP_ORIGIN: 2436 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2437 proplen); 2438 /* 2439 * If there is no parent at all, return failure to indicate that 2440 * it doesn't apply to this dataset. 2441 */ 2442 if (propbuf[0] == '\0') 2443 return (-1); 2444 break; 2445 2446 case ZFS_PROP_QUOTA: 2447 case ZFS_PROP_RESERVATION: 2448 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2449 return (-1); 2450 2451 /* 2452 * If quota or reservation is 0, we translate this into 'none' 2453 * (unless literal is set), and indicate that it's the default 2454 * value. Otherwise, we print the number nicely and indicate 2455 * that its set locally. 2456 */ 2457 if (val == 0) { 2458 if (literal) 2459 (void) strlcpy(propbuf, "0", proplen); 2460 else 2461 (void) strlcpy(propbuf, "none", proplen); 2462 } else { 2463 if (literal) 2464 (void) snprintf(propbuf, proplen, "%llu", 2465 (u_longlong_t)val); 2466 else 2467 zfs_nicenum(val, propbuf, proplen); 2468 } 2469 break; 2470 2471 case ZFS_PROP_COMPRESSRATIO: 2472 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2473 return (-1); 2474 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 2475 val / 100, (longlong_t)val % 100); 2476 break; 2477 2478 case ZFS_PROP_TYPE: 2479 switch (zhp->zfs_type) { 2480 case ZFS_TYPE_FILESYSTEM: 2481 str = "filesystem"; 2482 break; 2483 case ZFS_TYPE_VOLUME: 2484 str = "volume"; 2485 break; 2486 case ZFS_TYPE_SNAPSHOT: 2487 str = "snapshot"; 2488 break; 2489 default: 2490 abort(); 2491 } 2492 (void) snprintf(propbuf, proplen, "%s", str); 2493 break; 2494 2495 case ZFS_PROP_MOUNTED: 2496 /* 2497 * The 'mounted' property is a pseudo-property that described 2498 * whether the filesystem is currently mounted. Even though 2499 * it's a boolean value, the typical values of "on" and "off" 2500 * don't make sense, so we translate to "yes" and "no". 2501 */ 2502 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 2503 src, &source, &val) != 0) 2504 return (-1); 2505 if (val) 2506 (void) strlcpy(propbuf, "yes", proplen); 2507 else 2508 (void) strlcpy(propbuf, "no", proplen); 2509 break; 2510 2511 case ZFS_PROP_NAME: 2512 /* 2513 * The 'name' property is a pseudo-property derived from the 2514 * dataset name. It is presented as a real property to simplify 2515 * consumers. 2516 */ 2517 (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2518 break; 2519 2520 default: 2521 switch (zfs_prop_get_type(prop)) { 2522 case prop_type_number: 2523 if (get_numeric_property(zhp, prop, src, 2524 &source, &val) != 0) 2525 return (-1); 2526 if (literal) 2527 (void) snprintf(propbuf, proplen, "%llu", 2528 (u_longlong_t)val); 2529 else 2530 zfs_nicenum(val, propbuf, proplen); 2531 break; 2532 2533 case prop_type_string: 2534 (void) strlcpy(propbuf, 2535 getprop_string(zhp, prop, &source), proplen); 2536 break; 2537 2538 case prop_type_boolean: 2539 if (get_numeric_property(zhp, prop, src, 2540 &source, &val) != 0) 2541 return (-1); 2542 nicebool(val, propbuf, proplen); 2543 2544 break; 2545 2546 case prop_type_index: 2547 val = getprop_uint64(zhp, prop, &source); 2548 if (zfs_prop_index_to_string(prop, val, 2549 &strval) != 0) 2550 return (-1); 2551 (void) strlcpy(propbuf, strval, proplen); 2552 break; 2553 2554 default: 2555 abort(); 2556 } 2557 } 2558 2559 get_source(zhp, src, source, statbuf, statlen); 2560 2561 return (0); 2562 } 2563 2564 /* 2565 * Utility function to get the given numeric property. Does no validation that 2566 * the given property is the appropriate type; should only be used with 2567 * hard-coded property types. 2568 */ 2569 uint64_t 2570 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2571 { 2572 char *source; 2573 zfs_source_t sourcetype = ZFS_SRC_NONE; 2574 uint64_t val; 2575 2576 (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 2577 2578 return (val); 2579 } 2580 2581 /* 2582 * Similar to zfs_prop_get(), but returns the value as an integer. 2583 */ 2584 int 2585 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2586 zfs_source_t *src, char *statbuf, size_t statlen) 2587 { 2588 char *source; 2589 2590 /* 2591 * Check to see if this property applies to our object 2592 */ 2593 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2594 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 2595 dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 2596 zfs_prop_to_name(prop))); 2597 2598 if (src) 2599 *src = ZFS_SRC_NONE; 2600 2601 if (get_numeric_property(zhp, prop, src, &source, value) != 0) 2602 return (-1); 2603 2604 get_source(zhp, src, source, statbuf, statlen); 2605 2606 return (0); 2607 } 2608 2609 /* 2610 * Returns the name of the given zfs handle. 2611 */ 2612 const char * 2613 zfs_get_name(const zfs_handle_t *zhp) 2614 { 2615 return (zhp->zfs_name); 2616 } 2617 2618 /* 2619 * Returns the type of the given zfs handle. 2620 */ 2621 zfs_type_t 2622 zfs_get_type(const zfs_handle_t *zhp) 2623 { 2624 return (zhp->zfs_type); 2625 } 2626 2627 /* 2628 * Iterate over all child filesystems 2629 */ 2630 int 2631 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2632 { 2633 zfs_cmd_t zc = { 0 }; 2634 zfs_handle_t *nzhp; 2635 int ret; 2636 2637 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2638 ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2639 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2640 /* 2641 * Ignore private dataset names. 2642 */ 2643 if (dataset_name_hidden(zc.zc_name)) 2644 continue; 2645 2646 /* 2647 * Silently ignore errors, as the only plausible explanation is 2648 * that the pool has since been removed. 2649 */ 2650 if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 2651 zc.zc_name)) == NULL) 2652 continue; 2653 2654 if ((ret = func(nzhp, data)) != 0) 2655 return (ret); 2656 } 2657 2658 /* 2659 * An errno value of ESRCH indicates normal completion. If ENOENT is 2660 * returned, then the underlying dataset has been removed since we 2661 * obtained the handle. 2662 */ 2663 if (errno != ESRCH && errno != ENOENT) 2664 return (zfs_standard_error(zhp->zfs_hdl, errno, 2665 dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2666 2667 return (0); 2668 } 2669 2670 /* 2671 * Iterate over all snapshots 2672 */ 2673 int 2674 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2675 { 2676 zfs_cmd_t zc = { 0 }; 2677 zfs_handle_t *nzhp; 2678 int ret; 2679 2680 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2681 ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 2682 &zc) == 0; 2683 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2684 2685 if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 2686 zc.zc_name)) == NULL) 2687 continue; 2688 2689 if ((ret = func(nzhp, data)) != 0) 2690 return (ret); 2691 } 2692 2693 /* 2694 * An errno value of ESRCH indicates normal completion. If ENOENT is 2695 * returned, then the underlying dataset has been removed since we 2696 * obtained the handle. Silently ignore this case, and return success. 2697 */ 2698 if (errno != ESRCH && errno != ENOENT) 2699 return (zfs_standard_error(zhp->zfs_hdl, errno, 2700 dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2701 2702 return (0); 2703 } 2704 2705 /* 2706 * Iterate over all children, snapshots and filesystems 2707 */ 2708 int 2709 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2710 { 2711 int ret; 2712 2713 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 2714 return (ret); 2715 2716 return (zfs_iter_snapshots(zhp, func, data)); 2717 } 2718 2719 /* 2720 * Given a complete name, return just the portion that refers to the parent. 2721 * Can return NULL if this is a pool. 2722 */ 2723 static int 2724 parent_name(const char *path, char *buf, size_t buflen) 2725 { 2726 char *loc; 2727 2728 if ((loc = strrchr(path, '/')) == NULL) 2729 return (-1); 2730 2731 (void) strncpy(buf, path, MIN(buflen, loc - path)); 2732 buf[loc - path] = '\0'; 2733 2734 return (0); 2735 } 2736 2737 /* 2738 * If accept_ancestor is false, then check to make sure that the given path has 2739 * a parent, and that it exists. If accept_ancestor is true, then find the 2740 * closest existing ancestor for the given path. In prefixlen return the 2741 * length of already existing prefix of the given path. We also fetch the 2742 * 'zoned' property, which is used to validate property settings when creating 2743 * new datasets. 2744 */ 2745 static int 2746 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 2747 boolean_t accept_ancestor, int *prefixlen) 2748 { 2749 zfs_cmd_t zc = { 0 }; 2750 char parent[ZFS_MAXNAMELEN]; 2751 char *slash; 2752 zfs_handle_t *zhp; 2753 char errbuf[1024]; 2754 2755 (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 2756 path); 2757 2758 /* get parent, and check to see if this is just a pool */ 2759 if (parent_name(path, parent, sizeof (parent)) != 0) { 2760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2761 "missing dataset name")); 2762 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2763 } 2764 2765 /* check to see if the pool exists */ 2766 if ((slash = strchr(parent, '/')) == NULL) 2767 slash = parent + strlen(parent); 2768 (void) strncpy(zc.zc_name, parent, slash - parent); 2769 zc.zc_name[slash - parent] = '\0'; 2770 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2771 errno == ENOENT) { 2772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2773 "no such pool '%s'"), zc.zc_name); 2774 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2775 } 2776 2777 /* check to see if the parent dataset exists */ 2778 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 2779 if (errno == ENOENT && accept_ancestor) { 2780 /* 2781 * Go deeper to find an ancestor, give up on top level. 2782 */ 2783 if (parent_name(parent, parent, sizeof (parent)) != 0) { 2784 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2785 "no such pool '%s'"), zc.zc_name); 2786 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2787 } 2788 } else if (errno == ENOENT) { 2789 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2790 "parent does not exist")); 2791 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2792 } else 2793 return (zfs_standard_error(hdl, errno, errbuf)); 2794 } 2795 2796 *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2797 /* we are in a non-global zone, but parent is in the global zone */ 2798 if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 2799 (void) zfs_standard_error(hdl, EPERM, errbuf); 2800 zfs_close(zhp); 2801 return (-1); 2802 } 2803 2804 /* make sure parent is a filesystem */ 2805 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 2806 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2807 "parent is not a filesystem")); 2808 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 2809 zfs_close(zhp); 2810 return (-1); 2811 } 2812 2813 zfs_close(zhp); 2814 if (prefixlen != NULL) 2815 *prefixlen = strlen(parent); 2816 return (0); 2817 } 2818 2819 /* 2820 * Finds whether the dataset of the given type(s) exists. 2821 */ 2822 boolean_t 2823 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 2824 { 2825 zfs_handle_t *zhp; 2826 2827 if (!zfs_validate_name(hdl, path, types)) 2828 return (B_FALSE); 2829 2830 /* 2831 * Try to get stats for the dataset, which will tell us if it exists. 2832 */ 2833 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 2834 int ds_type = zhp->zfs_type; 2835 2836 zfs_close(zhp); 2837 if (types & ds_type) 2838 return (B_TRUE); 2839 } 2840 return (B_FALSE); 2841 } 2842 2843 /* 2844 * Creates non-existing ancestors of the given path. 2845 */ 2846 int 2847 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 2848 { 2849 int prefix; 2850 uint64_t zoned; 2851 char *path_copy; 2852 int rc; 2853 2854 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 2855 return (-1); 2856 2857 if ((path_copy = strdup(path)) != NULL) { 2858 rc = create_parents(hdl, path_copy, prefix); 2859 free(path_copy); 2860 } 2861 if (path_copy == NULL || rc != 0) 2862 return (-1); 2863 2864 return (0); 2865 } 2866 2867 /* 2868 * Create a new filesystem or volume. 2869 */ 2870 int 2871 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 2872 nvlist_t *props) 2873 { 2874 zfs_cmd_t zc = { 0 }; 2875 int ret; 2876 uint64_t size = 0; 2877 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 2878 char errbuf[1024]; 2879 uint64_t zoned; 2880 2881 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2882 "cannot create '%s'"), path); 2883 2884 /* validate the path, taking care to note the extended error message */ 2885 if (!zfs_validate_name(hdl, path, type)) 2886 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2887 2888 /* validate parents exist */ 2889 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2890 return (-1); 2891 2892 /* 2893 * The failure modes when creating a dataset of a different type over 2894 * one that already exists is a little strange. In particular, if you 2895 * try to create a dataset on top of an existing dataset, the ioctl() 2896 * will return ENOENT, not EEXIST. To prevent this from happening, we 2897 * first try to see if the dataset exists. 2898 */ 2899 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 2900 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 2901 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2902 "dataset already exists")); 2903 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2904 } 2905 2906 if (type == ZFS_TYPE_VOLUME) 2907 zc.zc_objset_type = DMU_OST_ZVOL; 2908 else 2909 zc.zc_objset_type = DMU_OST_ZFS; 2910 2911 if (props && (props = zfs_validate_properties(hdl, type, NULL, props, 2912 zoned, NULL, errbuf)) == 0) 2913 return (-1); 2914 2915 if (type == ZFS_TYPE_VOLUME) { 2916 /* 2917 * If we are creating a volume, the size and block size must 2918 * satisfy a few restraints. First, the blocksize must be a 2919 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 2920 * volsize must be a multiple of the block size, and cannot be 2921 * zero. 2922 */ 2923 if (props == NULL || nvlist_lookup_uint64(props, 2924 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 2925 nvlist_free(props); 2926 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2927 "missing volume size")); 2928 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2929 } 2930 2931 if ((ret = nvlist_lookup_uint64(props, 2932 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 2933 &blocksize)) != 0) { 2934 if (ret == ENOENT) { 2935 blocksize = zfs_prop_default_numeric( 2936 ZFS_PROP_VOLBLOCKSIZE); 2937 } else { 2938 nvlist_free(props); 2939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2940 "missing volume block size")); 2941 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2942 } 2943 } 2944 2945 if (size == 0) { 2946 nvlist_free(props); 2947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2948 "volume size cannot be zero")); 2949 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2950 } 2951 2952 if (size % blocksize != 0) { 2953 nvlist_free(props); 2954 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2955 "volume size must be a multiple of volume block " 2956 "size")); 2957 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2958 } 2959 } 2960 2961 if (props && 2962 zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) 2963 return (-1); 2964 nvlist_free(props); 2965 2966 /* create the dataset */ 2967 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2968 2969 if (ret == 0 && type == ZFS_TYPE_VOLUME) { 2970 ret = zvol_create_link(hdl, path); 2971 if (ret) { 2972 (void) zfs_standard_error(hdl, errno, 2973 dgettext(TEXT_DOMAIN, 2974 "Volume successfully created, but device links " 2975 "were not created")); 2976 zcmd_free_nvlists(&zc); 2977 return (-1); 2978 } 2979 } 2980 2981 zcmd_free_nvlists(&zc); 2982 2983 /* check for failure */ 2984 if (ret != 0) { 2985 char parent[ZFS_MAXNAMELEN]; 2986 (void) parent_name(path, parent, sizeof (parent)); 2987 2988 switch (errno) { 2989 case ENOENT: 2990 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2991 "no such parent '%s'"), parent); 2992 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2993 2994 case EINVAL: 2995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2996 "parent '%s' is not a filesystem"), parent); 2997 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2998 2999 case EDOM: 3000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3001 "volume block size must be power of 2 from " 3002 "%u to %uk"), 3003 (uint_t)SPA_MINBLOCKSIZE, 3004 (uint_t)SPA_MAXBLOCKSIZE >> 10); 3005 3006 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3007 3008 #ifdef _ILP32 3009 case EOVERFLOW: 3010 /* 3011 * This platform can't address a volume this big. 3012 */ 3013 if (type == ZFS_TYPE_VOLUME) 3014 return (zfs_error(hdl, EZFS_VOLTOOBIG, 3015 errbuf)); 3016 #endif 3017 /* FALLTHROUGH */ 3018 default: 3019 return (zfs_standard_error(hdl, errno, errbuf)); 3020 } 3021 } 3022 3023 return (0); 3024 } 3025 3026 /* 3027 * Destroys the given dataset. The caller must make sure that the filesystem 3028 * isn't mounted, and that there are no active dependents. 3029 */ 3030 int 3031 zfs_destroy(zfs_handle_t *zhp) 3032 { 3033 zfs_cmd_t zc = { 0 }; 3034 3035 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3036 3037 if (ZFS_IS_VOLUME(zhp)) { 3038 /* 3039 * If user doesn't have permissions to unshare volume, then 3040 * abort the request. This would only happen for a 3041 * non-privileged user. 3042 */ 3043 if (zfs_unshare_iscsi(zhp) != 0) { 3044 return (-1); 3045 } 3046 3047 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3048 return (-1); 3049 3050 zc.zc_objset_type = DMU_OST_ZVOL; 3051 } else { 3052 zc.zc_objset_type = DMU_OST_ZFS; 3053 } 3054 3055 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 3056 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 3057 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 3058 zhp->zfs_name)); 3059 } 3060 3061 remove_mountpoint(zhp); 3062 3063 return (0); 3064 } 3065 3066 struct destroydata { 3067 char *snapname; 3068 boolean_t gotone; 3069 boolean_t closezhp; 3070 }; 3071 3072 static int 3073 zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 3074 { 3075 struct destroydata *dd = arg; 3076 zfs_handle_t *szhp; 3077 char name[ZFS_MAXNAMELEN]; 3078 boolean_t closezhp = dd->closezhp; 3079 int rv; 3080 3081 (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 3082 (void) strlcat(name, "@", sizeof (name)); 3083 (void) strlcat(name, dd->snapname, sizeof (name)); 3084 3085 szhp = make_dataset_handle(zhp->zfs_hdl, name); 3086 if (szhp) { 3087 dd->gotone = B_TRUE; 3088 zfs_close(szhp); 3089 } 3090 3091 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3092 (void) zvol_remove_link(zhp->zfs_hdl, name); 3093 /* 3094 * NB: this is simply a best-effort. We don't want to 3095 * return an error, because then we wouldn't visit all 3096 * the volumes. 3097 */ 3098 } 3099 3100 dd->closezhp = B_TRUE; 3101 rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 3102 if (closezhp) 3103 zfs_close(zhp); 3104 return (rv); 3105 } 3106 3107 /* 3108 * Destroys all snapshots with the given name in zhp & descendants. 3109 */ 3110 int 3111 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 3112 { 3113 zfs_cmd_t zc = { 0 }; 3114 int ret; 3115 struct destroydata dd = { 0 }; 3116 3117 dd.snapname = snapname; 3118 (void) zfs_remove_link_cb(zhp, &dd); 3119 3120 if (!dd.gotone) { 3121 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 3122 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 3123 zhp->zfs_name, snapname)); 3124 } 3125 3126 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3127 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 3128 3129 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 3130 if (ret != 0) { 3131 char errbuf[1024]; 3132 3133 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3134 "cannot destroy '%s@%s'"), zc.zc_name, snapname); 3135 3136 switch (errno) { 3137 case EEXIST: 3138 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3139 "snapshot is cloned")); 3140 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 3141 3142 default: 3143 return (zfs_standard_error(zhp->zfs_hdl, errno, 3144 errbuf)); 3145 } 3146 } 3147 3148 return (0); 3149 } 3150 3151 /* 3152 * Clones the given dataset. The target must be of the same type as the source. 3153 */ 3154 int 3155 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3156 { 3157 zfs_cmd_t zc = { 0 }; 3158 char parent[ZFS_MAXNAMELEN]; 3159 int ret; 3160 char errbuf[1024]; 3161 libzfs_handle_t *hdl = zhp->zfs_hdl; 3162 zfs_type_t type; 3163 uint64_t zoned; 3164 3165 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3166 3167 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3168 "cannot create '%s'"), target); 3169 3170 /* validate the target name */ 3171 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM)) 3172 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3173 3174 /* validate parents exist */ 3175 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3176 return (-1); 3177 3178 (void) parent_name(target, parent, sizeof (parent)); 3179 3180 /* do the clone */ 3181 if (ZFS_IS_VOLUME(zhp)) { 3182 zc.zc_objset_type = DMU_OST_ZVOL; 3183 type = ZFS_TYPE_VOLUME; 3184 } else { 3185 zc.zc_objset_type = DMU_OST_ZFS; 3186 type = ZFS_TYPE_FILESYSTEM; 3187 } 3188 3189 if (props) { 3190 if ((props = zfs_validate_properties(hdl, type, NULL, props, 3191 zoned, zhp, errbuf)) == NULL) 3192 return (-1); 3193 3194 if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) { 3195 nvlist_free(props); 3196 return (-1); 3197 } 3198 3199 nvlist_free(props); 3200 } 3201 3202 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 3203 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 3204 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3205 3206 zcmd_free_nvlists(&zc); 3207 3208 if (ret != 0) { 3209 switch (errno) { 3210 3211 case ENOENT: 3212 /* 3213 * The parent doesn't exist. We should have caught this 3214 * above, but there may a race condition that has since 3215 * destroyed the parent. 3216 * 3217 * At this point, we don't know whether it's the source 3218 * that doesn't exist anymore, or whether the target 3219 * dataset doesn't exist. 3220 */ 3221 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3222 "no such parent '%s'"), parent); 3223 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3224 3225 case EXDEV: 3226 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3227 "source and target pools differ")); 3228 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 3229 errbuf)); 3230 3231 default: 3232 return (zfs_standard_error(zhp->zfs_hdl, errno, 3233 errbuf)); 3234 } 3235 } else if (ZFS_IS_VOLUME(zhp)) { 3236 ret = zvol_create_link(zhp->zfs_hdl, target); 3237 } 3238 3239 return (ret); 3240 } 3241 3242 typedef struct promote_data { 3243 char cb_mountpoint[MAXPATHLEN]; 3244 const char *cb_target; 3245 const char *cb_errbuf; 3246 uint64_t cb_pivot_txg; 3247 } promote_data_t; 3248 3249 static int 3250 promote_snap_cb(zfs_handle_t *zhp, void *data) 3251 { 3252 promote_data_t *pd = data; 3253 zfs_handle_t *szhp; 3254 char snapname[MAXPATHLEN]; 3255 int rv = 0; 3256 3257 /* We don't care about snapshots after the pivot point */ 3258 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 3259 zfs_close(zhp); 3260 return (0); 3261 } 3262 3263 /* Remove the device link if it's a zvol. */ 3264 if (ZFS_IS_VOLUME(zhp)) 3265 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 3266 3267 /* Check for conflicting names */ 3268 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 3269 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 3270 szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 3271 if (szhp != NULL) { 3272 zfs_close(szhp); 3273 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3274 "snapshot name '%s' from origin \n" 3275 "conflicts with '%s' from target"), 3276 zhp->zfs_name, snapname); 3277 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 3278 } 3279 zfs_close(zhp); 3280 return (rv); 3281 } 3282 3283 static int 3284 promote_snap_done_cb(zfs_handle_t *zhp, void *data) 3285 { 3286 promote_data_t *pd = data; 3287 3288 /* We don't care about snapshots after the pivot point */ 3289 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 3290 /* Create the device link if it's a zvol. */ 3291 if (ZFS_IS_VOLUME(zhp)) 3292 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3293 } 3294 3295 zfs_close(zhp); 3296 return (0); 3297 } 3298 3299 /* 3300 * Promotes the given clone fs to be the clone parent. 3301 */ 3302 int 3303 zfs_promote(zfs_handle_t *zhp) 3304 { 3305 libzfs_handle_t *hdl = zhp->zfs_hdl; 3306 zfs_cmd_t zc = { 0 }; 3307 char parent[MAXPATHLEN]; 3308 char *cp; 3309 int ret; 3310 zfs_handle_t *pzhp; 3311 promote_data_t pd; 3312 char errbuf[1024]; 3313 3314 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3315 "cannot promote '%s'"), zhp->zfs_name); 3316 3317 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3318 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3319 "snapshots can not be promoted")); 3320 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3321 } 3322 3323 (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 3324 if (parent[0] == '\0') { 3325 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3326 "not a cloned filesystem")); 3327 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3328 } 3329 cp = strchr(parent, '@'); 3330 *cp = '\0'; 3331 3332 /* Walk the snapshots we will be moving */ 3333 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 3334 if (pzhp == NULL) 3335 return (-1); 3336 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 3337 zfs_close(pzhp); 3338 pd.cb_target = zhp->zfs_name; 3339 pd.cb_errbuf = errbuf; 3340 pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY); 3341 if (pzhp == NULL) 3342 return (-1); 3343 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 3344 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 3345 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 3346 if (ret != 0) { 3347 zfs_close(pzhp); 3348 return (-1); 3349 } 3350 3351 /* issue the ioctl */ 3352 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 3353 sizeof (zc.zc_value)); 3354 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3355 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 3356 3357 if (ret != 0) { 3358 int save_errno = errno; 3359 3360 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 3361 zfs_close(pzhp); 3362 3363 switch (save_errno) { 3364 case EEXIST: 3365 /* 3366 * There is a conflicting snapshot name. We 3367 * should have caught this above, but they could 3368 * have renamed something in the mean time. 3369 */ 3370 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3371 "conflicting snapshot name from parent '%s'"), 3372 parent); 3373 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3374 3375 default: 3376 return (zfs_standard_error(hdl, save_errno, errbuf)); 3377 } 3378 } else { 3379 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3380 } 3381 3382 zfs_close(pzhp); 3383 return (ret); 3384 } 3385 3386 struct createdata { 3387 const char *cd_snapname; 3388 int cd_ifexists; 3389 }; 3390 3391 static int 3392 zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 3393 { 3394 struct createdata *cd = arg; 3395 int ret; 3396 3397 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3398 char name[MAXPATHLEN]; 3399 3400 (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 3401 (void) strlcat(name, "@", sizeof (name)); 3402 (void) strlcat(name, cd->cd_snapname, sizeof (name)); 3403 (void) zvol_create_link_common(zhp->zfs_hdl, name, 3404 cd->cd_ifexists); 3405 /* 3406 * NB: this is simply a best-effort. We don't want to 3407 * return an error, because then we wouldn't visit all 3408 * the volumes. 3409 */ 3410 } 3411 3412 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 3413 3414 zfs_close(zhp); 3415 3416 return (ret); 3417 } 3418 3419 /* 3420 * Takes a snapshot of the given dataset. 3421 */ 3422 int 3423 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3424 { 3425 const char *delim; 3426 char *parent; 3427 zfs_handle_t *zhp; 3428 zfs_cmd_t zc = { 0 }; 3429 int ret; 3430 char errbuf[1024]; 3431 3432 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3433 "cannot snapshot '%s'"), path); 3434 3435 /* validate the target name */ 3436 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT)) 3437 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3438 3439 /* make sure the parent exists and is of the appropriate type */ 3440 delim = strchr(path, '@'); 3441 if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 3442 return (-1); 3443 (void) strncpy(parent, path, delim - path); 3444 parent[delim - path] = '\0'; 3445 3446 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3447 ZFS_TYPE_VOLUME)) == NULL) { 3448 free(parent); 3449 return (-1); 3450 } 3451 3452 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3453 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 3454 if (ZFS_IS_VOLUME(zhp)) 3455 zc.zc_objset_type = DMU_OST_ZVOL; 3456 else 3457 zc.zc_objset_type = DMU_OST_ZFS; 3458 zc.zc_cookie = recursive; 3459 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 3460 3461 /* 3462 * if it was recursive, the one that actually failed will be in 3463 * zc.zc_name. 3464 */ 3465 if (ret != 0) 3466 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3467 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 3468 3469 if (ret == 0 && recursive) { 3470 struct createdata cd; 3471 3472 cd.cd_snapname = delim + 1; 3473 cd.cd_ifexists = B_FALSE; 3474 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 3475 } 3476 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 3477 ret = zvol_create_link(zhp->zfs_hdl, path); 3478 if (ret != 0) { 3479 (void) zfs_standard_error(hdl, errno, 3480 dgettext(TEXT_DOMAIN, 3481 "Volume successfully snapshotted, but device links " 3482 "were not created")); 3483 free(parent); 3484 zfs_close(zhp); 3485 return (-1); 3486 } 3487 } 3488 3489 if (ret != 0) 3490 (void) zfs_standard_error(hdl, errno, errbuf); 3491 3492 free(parent); 3493 zfs_close(zhp); 3494 3495 return (ret); 3496 } 3497 3498 /* 3499 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 3500 * NULL) to the file descriptor specified by outfd. 3501 */ 3502 int 3503 zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3504 { 3505 zfs_cmd_t zc = { 0 }; 3506 char errbuf[1024]; 3507 libzfs_handle_t *hdl = zhp->zfs_hdl; 3508 3509 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3510 3511 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3512 if (fromsnap) 3513 (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 3514 zc.zc_cookie = outfd; 3515 3516 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 3517 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3518 "cannot send '%s'"), zhp->zfs_name); 3519 3520 switch (errno) { 3521 3522 case EXDEV: 3523 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3524 "not an earlier snapshot from the same fs")); 3525 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3526 3527 case EDQUOT: 3528 case EFBIG: 3529 case EIO: 3530 case ENOLINK: 3531 case ENOSPC: 3532 case ENOSTR: 3533 case ENXIO: 3534 case EPIPE: 3535 case ERANGE: 3536 case EFAULT: 3537 case EROFS: 3538 zfs_error_aux(hdl, strerror(errno)); 3539 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3540 3541 default: 3542 return (zfs_standard_error(hdl, errno, errbuf)); 3543 } 3544 } 3545 3546 return (0); 3547 } 3548 3549 /* 3550 * Create ancestors of 'target', but not target itself, and not 3551 * ancestors whose names are shorter than prefixlen. Die if 3552 * prefixlen-ancestor does not exist. 3553 */ 3554 static int 3555 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 3556 { 3557 zfs_handle_t *h; 3558 char *cp; 3559 3560 /* make sure prefix exists */ 3561 cp = strchr(target + prefixlen, '/'); 3562 *cp = '\0'; 3563 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3564 *cp = '/'; 3565 if (h == NULL) 3566 return (-1); 3567 zfs_close(h); 3568 3569 /* 3570 * Attempt to create, mount, and share any ancestor filesystems, 3571 * up to the prefixlen-long one. 3572 */ 3573 for (cp = target + prefixlen + 1; 3574 cp = strchr(cp, '/'); *cp = '/', cp++) { 3575 const char *opname; 3576 char *logstr; 3577 3578 *cp = '\0'; 3579 3580 h = make_dataset_handle(hdl, target); 3581 if (h) { 3582 /* it already exists, nothing to do here */ 3583 zfs_close(h); 3584 continue; 3585 } 3586 3587 opname = dgettext(TEXT_DOMAIN, "create"); 3588 logstr = hdl->libzfs_log_str; 3589 hdl->libzfs_log_str = NULL; 3590 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 3591 NULL) != 0) { 3592 hdl->libzfs_log_str = logstr; 3593 goto ancestorerr; 3594 } 3595 3596 hdl->libzfs_log_str = logstr; 3597 opname = dgettext(TEXT_DOMAIN, "open"); 3598 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3599 if (h == NULL) 3600 goto ancestorerr; 3601 3602 opname = dgettext(TEXT_DOMAIN, "mount"); 3603 if (zfs_mount(h, NULL, 0) != 0) 3604 goto ancestorerr; 3605 3606 opname = dgettext(TEXT_DOMAIN, "share"); 3607 if (zfs_share(h) != 0) 3608 goto ancestorerr; 3609 3610 zfs_close(h); 3611 3612 continue; 3613 ancestorerr: 3614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3615 "failed to %s ancestor '%s'"), opname, target); 3616 return (-1); 3617 } 3618 3619 return (0); 3620 } 3621 3622 /* 3623 * Restores a backup of tosnap from the file descriptor specified by infd. 3624 */ 3625 int 3626 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 3627 int verbose, int dryrun, boolean_t force, int infd) 3628 { 3629 zfs_cmd_t zc = { 0 }; 3630 time_t begin_time; 3631 int ioctl_err, err, bytes, size, choplen; 3632 char *cp; 3633 dmu_replay_record_t drr; 3634 struct drr_begin *drrb = &zc.zc_begin_record; 3635 char errbuf[1024]; 3636 prop_changelist_t *clp; 3637 char chopprefix[ZFS_MAXNAMELEN]; 3638 3639 begin_time = time(NULL); 3640 3641 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3642 "cannot receive")); 3643 3644 /* read in the BEGIN record */ 3645 cp = (char *)&drr; 3646 bytes = 0; 3647 do { 3648 size = read(infd, cp, sizeof (drr) - bytes); 3649 cp += size; 3650 bytes += size; 3651 } while (size > 0); 3652 3653 if (size < 0 || bytes != sizeof (drr)) { 3654 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3655 "stream (failed to read first record)")); 3656 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3657 } 3658 3659 zc.zc_begin_record = drr.drr_u.drr_begin; 3660 3661 if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3662 drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 3663 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3664 "stream (bad magic number)")); 3665 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3666 } 3667 3668 if (drrb->drr_version != DMU_BACKUP_VERSION && 3669 drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 3670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 3671 "0x%llx is supported (stream is version 0x%llx)"), 3672 DMU_BACKUP_VERSION, drrb->drr_version); 3673 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3674 } 3675 3676 if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 3677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3678 "stream (bad snapshot name)")); 3679 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3680 } 3681 /* 3682 * Determine how much of the snapshot name stored in the stream 3683 * we are going to tack on to the name they specified on the 3684 * command line, and how much we are going to chop off. 3685 * 3686 * If they specified a snapshot, chop the entire name stored in 3687 * the stream. 3688 */ 3689 (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3690 if (isprefix) { 3691 /* 3692 * They specified a fs with -d, we want to tack on 3693 * everything but the pool name stored in the stream 3694 */ 3695 if (strchr(tosnap, '@')) { 3696 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 3697 "argument - snapshot not allowed with -d")); 3698 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3699 } 3700 cp = strchr(chopprefix, '/'); 3701 if (cp == NULL) 3702 cp = strchr(chopprefix, '@'); 3703 *cp = '\0'; 3704 } else if (strchr(tosnap, '@') == NULL) { 3705 /* 3706 * If they specified a filesystem without -d, we want to 3707 * tack on everything after the fs specified in the 3708 * first name from the stream. 3709 */ 3710 cp = strchr(chopprefix, '@'); 3711 *cp = '\0'; 3712 } 3713 choplen = strlen(chopprefix); 3714 3715 /* 3716 * Determine name of destination snapshot, store in zc_value. 3717 */ 3718 (void) strcpy(zc.zc_value, tosnap); 3719 (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 3720 sizeof (zc.zc_value)); 3721 if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT)) 3722 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3723 3724 (void) strcpy(zc.zc_name, zc.zc_value); 3725 if (drrb->drr_fromguid) { 3726 /* incremental backup stream */ 3727 zfs_handle_t *h; 3728 3729 /* do the recvbackup ioctl to the containing fs */ 3730 *strchr(zc.zc_name, '@') = '\0'; 3731 3732 /* make sure destination fs exists */ 3733 h = zfs_open(hdl, zc.zc_name, 3734 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3735 if (h == NULL) 3736 return (-1); 3737 if (!dryrun) { 3738 /* 3739 * We need to unmount all the dependents of the dataset 3740 * and the dataset itself. If it's a volume 3741 * then remove device link. 3742 */ 3743 if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 3744 clp = changelist_gather(h, ZFS_PROP_NAME, 0); 3745 if (clp == NULL) 3746 return (-1); 3747 if (changelist_prefix(clp) != 0) { 3748 changelist_free(clp); 3749 return (-1); 3750 } 3751 } else { 3752 if (zvol_remove_link(hdl, h->zfs_name) != 0) { 3753 zfs_close(h); 3754 return (-1); 3755 } 3756 3757 } 3758 } 3759 zfs_close(h); 3760 } else { 3761 /* full backup stream */ 3762 3763 /* Make sure destination fs does not exist */ 3764 *strchr(zc.zc_name, '@') = '\0'; 3765 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 3766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3767 "destination '%s' exists"), zc.zc_name); 3768 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3769 } 3770 3771 if (strchr(zc.zc_name, '/') == NULL) { 3772 /* 3773 * they're trying to do a recv into a 3774 * nonexistant topmost filesystem. 3775 */ 3776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3777 "destination does not exist"), zc.zc_name); 3778 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3779 } 3780 3781 /* Do the recvbackup ioctl to the fs's parent. */ 3782 *strrchr(zc.zc_name, '/') = '\0'; 3783 3784 if (isprefix && (err = create_parents(hdl, 3785 zc.zc_value, strlen(tosnap))) != 0) { 3786 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 3787 } 3788 3789 } 3790 3791 zc.zc_cookie = infd; 3792 zc.zc_guid = force; 3793 if (verbose) { 3794 (void) printf("%s %s stream of %s into %s\n", 3795 dryrun ? "would receive" : "receiving", 3796 drrb->drr_fromguid ? "incremental" : "full", 3797 drr.drr_u.drr_begin.drr_toname, 3798 zc.zc_value); 3799 (void) fflush(stdout); 3800 } 3801 if (dryrun) 3802 return (0); 3803 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3804 if (ioctl_err != 0) { 3805 switch (errno) { 3806 case ENODEV: 3807 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3808 "most recent snapshot does not match incremental " 3809 "source")); 3810 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3811 break; 3812 case ETXTBSY: 3813 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3814 "destination has been modified since most recent " 3815 "snapshot")); 3816 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3817 break; 3818 case EEXIST: 3819 if (drrb->drr_fromguid == 0) { 3820 /* it's the containing fs that exists */ 3821 cp = strchr(zc.zc_value, '@'); 3822 *cp = '\0'; 3823 } 3824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3825 "destination already exists")); 3826 (void) zfs_error_fmt(hdl, EZFS_EXISTS, 3827 dgettext(TEXT_DOMAIN, "cannot restore to %s"), 3828 zc.zc_value); 3829 break; 3830 case EINVAL: 3831 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3832 break; 3833 case ECKSUM: 3834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3835 "invalid stream (checksum mismatch)")); 3836 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3837 break; 3838 default: 3839 (void) zfs_standard_error(hdl, errno, errbuf); 3840 } 3841 } 3842 3843 /* 3844 * Mount or recreate the /dev links for the target filesystem 3845 * (if created, or if we tore them down to do an incremental 3846 * restore), and the /dev links for the new snapshot (if 3847 * created). Also mount any children of the target filesystem 3848 * if we did an incremental receive. 3849 */ 3850 cp = strchr(zc.zc_value, '@'); 3851 if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3852 zfs_handle_t *h; 3853 3854 *cp = '\0'; 3855 h = zfs_open(hdl, zc.zc_value, 3856 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3857 *cp = '@'; 3858 if (h) { 3859 if (h->zfs_type == ZFS_TYPE_VOLUME) { 3860 err = zvol_create_link(hdl, h->zfs_name); 3861 if (err == 0 && ioctl_err == 0) 3862 err = zvol_create_link(hdl, 3863 zc.zc_value); 3864 } else { 3865 if (drrb->drr_fromguid) { 3866 err = changelist_postfix(clp); 3867 changelist_free(clp); 3868 } else { 3869 err = zfs_mount(h, NULL, 0); 3870 } 3871 } 3872 zfs_close(h); 3873 } 3874 } 3875 3876 if (err || ioctl_err) 3877 return (-1); 3878 3879 if (verbose) { 3880 char buf1[64]; 3881 char buf2[64]; 3882 uint64_t bytes = zc.zc_cookie; 3883 time_t delta = time(NULL) - begin_time; 3884 if (delta == 0) 3885 delta = 1; 3886 zfs_nicenum(bytes, buf1, sizeof (buf1)); 3887 zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3888 3889 (void) printf("received %sb stream in %lu seconds (%sb/sec)\n", 3890 buf1, delta, buf2); 3891 } 3892 3893 return (0); 3894 } 3895 3896 /* 3897 * Destroy any more recent snapshots. We invoke this callback on any dependents 3898 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3899 * is a dependent and we should just destroy it without checking the transaction 3900 * group. 3901 */ 3902 typedef struct rollback_data { 3903 const char *cb_target; /* the snapshot */ 3904 uint64_t cb_create; /* creation time reference */ 3905 prop_changelist_t *cb_clp; /* changelist pointer */ 3906 int cb_error; 3907 boolean_t cb_dependent; 3908 } rollback_data_t; 3909 3910 static int 3911 rollback_destroy(zfs_handle_t *zhp, void *data) 3912 { 3913 rollback_data_t *cbp = data; 3914 3915 if (!cbp->cb_dependent) { 3916 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 3917 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 3918 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 3919 cbp->cb_create) { 3920 char *logstr; 3921 3922 cbp->cb_dependent = B_TRUE; 3923 if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 3924 cbp) != 0) 3925 cbp->cb_error = 1; 3926 cbp->cb_dependent = B_FALSE; 3927 3928 logstr = zhp->zfs_hdl->libzfs_log_str; 3929 zhp->zfs_hdl->libzfs_log_str = NULL; 3930 if (zfs_destroy(zhp) != 0) 3931 cbp->cb_error = 1; 3932 else 3933 changelist_remove(zhp, cbp->cb_clp); 3934 zhp->zfs_hdl->libzfs_log_str = logstr; 3935 } 3936 } else { 3937 if (zfs_destroy(zhp) != 0) 3938 cbp->cb_error = 1; 3939 else 3940 changelist_remove(zhp, cbp->cb_clp); 3941 } 3942 3943 zfs_close(zhp); 3944 return (0); 3945 } 3946 3947 /* 3948 * Rollback the dataset to its latest snapshot. 3949 */ 3950 static int 3951 do_rollback(zfs_handle_t *zhp) 3952 { 3953 int ret; 3954 zfs_cmd_t zc = { 0 }; 3955 3956 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3957 zhp->zfs_type == ZFS_TYPE_VOLUME); 3958 3959 if (zhp->zfs_type == ZFS_TYPE_VOLUME && 3960 zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3961 return (-1); 3962 3963 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3964 3965 if (ZFS_IS_VOLUME(zhp)) 3966 zc.zc_objset_type = DMU_OST_ZVOL; 3967 else 3968 zc.zc_objset_type = DMU_OST_ZFS; 3969 3970 /* 3971 * We rely on the consumer to verify that there are no newer snapshots 3972 * for the given dataset. Given these constraints, we can simply pass 3973 * the name on to the ioctl() call. There is still an unlikely race 3974 * condition where the user has taken a snapshot since we verified that 3975 * this was the most recent. 3976 */ 3977 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 3978 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 3979 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 3980 zhp->zfs_name); 3981 } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3982 ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3983 } 3984 3985 return (ret); 3986 } 3987 3988 /* 3989 * Given a dataset, rollback to a specific snapshot, discarding any 3990 * data changes since then and making it the active dataset. 3991 * 3992 * Any snapshots more recent than the target are destroyed, along with 3993 * their dependents. 3994 */ 3995 int 3996 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 3997 { 3998 int ret; 3999 rollback_data_t cb = { 0 }; 4000 prop_changelist_t *clp; 4001 4002 /* 4003 * Unmount all dependendents of the dataset and the dataset itself. 4004 * The list we need to gather is the same as for doing rename 4005 */ 4006 clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 4007 if (clp == NULL) 4008 return (-1); 4009 4010 if ((ret = changelist_prefix(clp)) != 0) 4011 goto out; 4012 4013 /* 4014 * Destroy all recent snapshots and its dependends. 4015 */ 4016 cb.cb_target = snap->zfs_name; 4017 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 4018 cb.cb_clp = clp; 4019 (void) zfs_iter_children(zhp, rollback_destroy, &cb); 4020 4021 if ((ret = cb.cb_error) != 0) { 4022 (void) changelist_postfix(clp); 4023 goto out; 4024 } 4025 4026 /* 4027 * Now that we have verified that the snapshot is the latest, 4028 * rollback to the given snapshot. 4029 */ 4030 ret = do_rollback(zhp); 4031 4032 if (ret != 0) { 4033 (void) changelist_postfix(clp); 4034 goto out; 4035 } 4036 4037 /* 4038 * We only want to re-mount the filesystem if it was mounted in the 4039 * first place. 4040 */ 4041 ret = changelist_postfix(clp); 4042 4043 out: 4044 changelist_free(clp); 4045 return (ret); 4046 } 4047 4048 /* 4049 * Iterate over all dependents for a given dataset. This includes both 4050 * hierarchical dependents (children) and data dependents (snapshots and 4051 * clones). The bulk of the processing occurs in get_dependents() in 4052 * libzfs_graph.c. 4053 */ 4054 int 4055 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 4056 zfs_iter_f func, void *data) 4057 { 4058 char **dependents; 4059 size_t count; 4060 int i; 4061 zfs_handle_t *child; 4062 int ret = 0; 4063 4064 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 4065 &dependents, &count) != 0) 4066 return (-1); 4067 4068 for (i = 0; i < count; i++) { 4069 if ((child = make_dataset_handle(zhp->zfs_hdl, 4070 dependents[i])) == NULL) 4071 continue; 4072 4073 if ((ret = func(child, data)) != 0) 4074 break; 4075 } 4076 4077 for (i = 0; i < count; i++) 4078 free(dependents[i]); 4079 free(dependents); 4080 4081 return (ret); 4082 } 4083 4084 /* 4085 * Renames the given dataset. 4086 */ 4087 int 4088 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 4089 { 4090 int ret; 4091 zfs_cmd_t zc = { 0 }; 4092 char *delim; 4093 prop_changelist_t *cl = NULL; 4094 zfs_handle_t *zhrp = NULL; 4095 char *parentname = NULL; 4096 char parent[ZFS_MAXNAMELEN]; 4097 libzfs_handle_t *hdl = zhp->zfs_hdl; 4098 char errbuf[1024]; 4099 4100 /* if we have the same exact name, just return success */ 4101 if (strcmp(zhp->zfs_name, target) == 0) 4102 return (0); 4103 4104 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4105 "cannot rename to '%s'"), target); 4106 4107 /* 4108 * Make sure the target name is valid 4109 */ 4110 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 4111 if ((strchr(target, '@') == NULL) || 4112 *target == '@') { 4113 /* 4114 * Snapshot target name is abbreviated, 4115 * reconstruct full dataset name 4116 */ 4117 (void) strlcpy(parent, zhp->zfs_name, 4118 sizeof (parent)); 4119 delim = strchr(parent, '@'); 4120 if (strchr(target, '@') == NULL) 4121 *(++delim) = '\0'; 4122 else 4123 *delim = '\0'; 4124 (void) strlcat(parent, target, sizeof (parent)); 4125 target = parent; 4126 } else { 4127 /* 4128 * Make sure we're renaming within the same dataset. 4129 */ 4130 delim = strchr(target, '@'); 4131 if (strncmp(zhp->zfs_name, target, delim - target) 4132 != 0 || zhp->zfs_name[delim - target] != '@') { 4133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4134 "snapshots must be part of same " 4135 "dataset")); 4136 return (zfs_error(hdl, EZFS_CROSSTARGET, 4137 errbuf)); 4138 } 4139 } 4140 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 4141 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4142 } else { 4143 if (recursive) { 4144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4145 "recursive rename must be a snapshot")); 4146 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4147 } 4148 4149 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 4150 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4151 uint64_t unused; 4152 4153 /* validate parents */ 4154 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4155 return (-1); 4156 4157 (void) parent_name(target, parent, sizeof (parent)); 4158 4159 /* make sure we're in the same pool */ 4160 verify((delim = strchr(target, '/')) != NULL); 4161 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4162 zhp->zfs_name[delim - target] != '/') { 4163 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4164 "datasets must be within same pool")); 4165 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4166 } 4167 4168 /* new name cannot be a child of the current dataset name */ 4169 if (strncmp(parent, zhp->zfs_name, 4170 strlen(zhp->zfs_name)) == 0) { 4171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4172 "New dataset name cannot be a descendent of " 4173 "current dataset name")); 4174 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4175 } 4176 } 4177 4178 (void) snprintf(errbuf, sizeof (errbuf), 4179 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 4180 4181 if (getzoneid() == GLOBAL_ZONEID && 4182 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 4183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4184 "dataset is used in a non-global zone")); 4185 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4186 } 4187 4188 if (recursive) { 4189 struct destroydata dd; 4190 4191 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 4192 if (parentname == NULL) { 4193 ret = -1; 4194 goto error; 4195 } 4196 delim = strchr(parentname, '@'); 4197 *delim = '\0'; 4198 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY); 4199 if (zhrp == NULL) { 4200 ret = -1; 4201 goto error; 4202 } 4203 4204 dd.snapname = delim + 1; 4205 dd.gotone = B_FALSE; 4206 dd.closezhp = B_TRUE; 4207 4208 /* We remove any zvol links prior to renaming them */ 4209 ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 4210 if (ret) { 4211 goto error; 4212 } 4213 } else { 4214 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 4215 return (-1); 4216 4217 if (changelist_haszonedchild(cl)) { 4218 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4219 "child dataset with inherited mountpoint is used " 4220 "in a non-global zone")); 4221 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 4222 goto error; 4223 } 4224 4225 if ((ret = changelist_prefix(cl)) != 0) 4226 goto error; 4227 } 4228 4229 if (ZFS_IS_VOLUME(zhp)) 4230 zc.zc_objset_type = DMU_OST_ZVOL; 4231 else 4232 zc.zc_objset_type = DMU_OST_ZFS; 4233 4234 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4235 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 4236 4237 zc.zc_cookie = recursive; 4238 4239 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 4240 /* 4241 * if it was recursive, the one that actually failed will 4242 * be in zc.zc_name 4243 */ 4244 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4245 "cannot rename to '%s'"), zc.zc_name); 4246 4247 if (recursive && errno == EEXIST) { 4248 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4249 "a child dataset already has a snapshot " 4250 "with the new name")); 4251 (void) zfs_error(hdl, EZFS_CROSSTARGET, errbuf); 4252 } else { 4253 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 4254 } 4255 4256 /* 4257 * On failure, we still want to remount any filesystems that 4258 * were previously mounted, so we don't alter the system state. 4259 */ 4260 if (recursive) { 4261 struct createdata cd; 4262 4263 /* only create links for datasets that had existed */ 4264 cd.cd_snapname = delim + 1; 4265 cd.cd_ifexists = B_TRUE; 4266 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 4267 &cd); 4268 } else { 4269 (void) changelist_postfix(cl); 4270 } 4271 } else { 4272 if (recursive) { 4273 struct createdata cd; 4274 4275 /* only create links for datasets that had existed */ 4276 cd.cd_snapname = strchr(target, '@') + 1; 4277 cd.cd_ifexists = B_TRUE; 4278 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 4279 &cd); 4280 } else { 4281 changelist_rename(cl, zfs_get_name(zhp), target); 4282 ret = changelist_postfix(cl); 4283 } 4284 } 4285 4286 error: 4287 if (parentname) { 4288 free(parentname); 4289 } 4290 if (zhrp) { 4291 zfs_close(zhrp); 4292 } 4293 if (cl) { 4294 changelist_free(cl); 4295 } 4296 return (ret); 4297 } 4298 4299 /* 4300 * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4301 * poke devfsadm to create the /dev link, and then wait for the link to appear. 4302 */ 4303 int 4304 zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4305 { 4306 return (zvol_create_link_common(hdl, dataset, B_FALSE)); 4307 } 4308 4309 static int 4310 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 4311 { 4312 zfs_cmd_t zc = { 0 }; 4313 di_devlink_handle_t dhdl; 4314 priv_set_t *priv_effective; 4315 int privileged; 4316 4317 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4318 4319 /* 4320 * Issue the appropriate ioctl. 4321 */ 4322 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4323 switch (errno) { 4324 case EEXIST: 4325 /* 4326 * Silently ignore the case where the link already 4327 * exists. This allows 'zfs volinit' to be run multiple 4328 * times without errors. 4329 */ 4330 return (0); 4331 4332 case ENOENT: 4333 /* 4334 * Dataset does not exist in the kernel. If we 4335 * don't care (see zfs_rename), then ignore the 4336 * error quietly. 4337 */ 4338 if (ifexists) { 4339 return (0); 4340 } 4341 4342 /* FALLTHROUGH */ 4343 4344 default: 4345 return (zfs_standard_error_fmt(hdl, errno, 4346 dgettext(TEXT_DOMAIN, "cannot create device links " 4347 "for '%s'"), dataset)); 4348 } 4349 } 4350 4351 /* 4352 * If privileged call devfsadm and wait for the links to 4353 * magically appear. 4354 * Otherwise, print out an informational message. 4355 */ 4356 4357 priv_effective = priv_allocset(); 4358 (void) getppriv(PRIV_EFFECTIVE, priv_effective); 4359 privileged = (priv_isfullset(priv_effective) == B_TRUE); 4360 priv_freeset(priv_effective); 4361 4362 if (privileged) { 4363 if ((dhdl = di_devlink_init(ZFS_DRIVER, 4364 DI_MAKE_LINK)) == NULL) { 4365 zfs_error_aux(hdl, strerror(errno)); 4366 (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 4367 dgettext(TEXT_DOMAIN, "cannot create device links " 4368 "for '%s'"), dataset); 4369 (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 4370 return (-1); 4371 } else { 4372 (void) di_devlink_fini(&dhdl); 4373 } 4374 } else { 4375 char pathname[MAXPATHLEN]; 4376 struct stat64 statbuf; 4377 int i; 4378 4379 #define MAX_WAIT 10 4380 4381 /* 4382 * This is the poor mans way of waiting for the link 4383 * to show up. If after 10 seconds we still don't 4384 * have it, then print out a message. 4385 */ 4386 (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 4387 dataset); 4388 4389 for (i = 0; i != MAX_WAIT; i++) { 4390 if (stat64(pathname, &statbuf) == 0) 4391 break; 4392 (void) sleep(1); 4393 } 4394 if (i == MAX_WAIT) 4395 (void) printf(gettext("%s may not be immediately " 4396 "available\n"), pathname); 4397 } 4398 4399 return (0); 4400 } 4401 4402 /* 4403 * Remove a minor node for the given zvol and the associated /dev links. 4404 */ 4405 int 4406 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4407 { 4408 zfs_cmd_t zc = { 0 }; 4409 4410 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4411 4412 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4413 switch (errno) { 4414 case ENXIO: 4415 /* 4416 * Silently ignore the case where the link no longer 4417 * exists, so that 'zfs volfini' can be run multiple 4418 * times without errors. 4419 */ 4420 return (0); 4421 4422 default: 4423 return (zfs_standard_error_fmt(hdl, errno, 4424 dgettext(TEXT_DOMAIN, "cannot remove device " 4425 "links for '%s'"), dataset)); 4426 } 4427 } 4428 4429 return (0); 4430 } 4431 4432 nvlist_t * 4433 zfs_get_user_props(zfs_handle_t *zhp) 4434 { 4435 return (zhp->zfs_user_props); 4436 } 4437 4438 /* 4439 * Given a comma-separated list of properties, construct a property list 4440 * containing both user-defined and native properties. This function will 4441 * return a NULL list if 'all' is specified, which can later be expanded on a 4442 * per-dataset basis by zfs_expand_proplist(). 4443 */ 4444 int 4445 zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields, 4446 zfs_proplist_t **listp, zfs_type_t type) 4447 { 4448 size_t len; 4449 char *s, *p; 4450 char c; 4451 zfs_prop_t prop; 4452 zfs_proplist_t *entry; 4453 zfs_proplist_t **last; 4454 4455 *listp = NULL; 4456 last = listp; 4457 4458 /* 4459 * If 'all' is specified, return a NULL list. 4460 */ 4461 if (strcmp(fields, "all") == 0) 4462 return (0); 4463 4464 /* 4465 * If no fields were specified, return an error. 4466 */ 4467 if (fields[0] == '\0') { 4468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4469 "no properties specified")); 4470 return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 4471 "bad property list"))); 4472 } 4473 4474 /* 4475 * It would be nice to use getsubopt() here, but the inclusion of column 4476 * aliases makes this more effort than it's worth. 4477 */ 4478 s = fields; 4479 while (*s != '\0') { 4480 if ((p = strchr(s, ',')) == NULL) { 4481 len = strlen(s); 4482 p = s + len; 4483 } else { 4484 len = p - s; 4485 } 4486 4487 /* 4488 * Check for empty options. 4489 */ 4490 if (len == 0) { 4491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4492 "empty property name")); 4493 return (zfs_error(hdl, EZFS_BADPROP, 4494 dgettext(TEXT_DOMAIN, "bad property list"))); 4495 } 4496 4497 /* 4498 * Check all regular property names. 4499 */ 4500 c = s[len]; 4501 s[len] = '\0'; 4502 prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) : 4503 zfs_name_to_prop(s); 4504 4505 if (prop != ZFS_PROP_INVAL && 4506 !zfs_prop_valid_for_type(prop, type)) 4507 prop = ZFS_PROP_INVAL; 4508 4509 /* 4510 * When no property table entry can be found, return failure if 4511 * this is a pool property or if this isn't a user-defined 4512 * dataset property, 4513 */ 4514 if (prop == ZFS_PROP_INVAL && 4515 (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) { 4516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4517 "invalid property '%s'"), s); 4518 return (zfs_error(hdl, EZFS_BADPROP, 4519 dgettext(TEXT_DOMAIN, "bad property list"))); 4520 } 4521 4522 if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL) 4523 return (-1); 4524 4525 entry->pl_prop = prop; 4526 if (prop == ZFS_PROP_INVAL) { 4527 if ((entry->pl_user_prop = 4528 zfs_strdup(hdl, s)) == NULL) { 4529 free(entry); 4530 return (-1); 4531 } 4532 entry->pl_width = strlen(s); 4533 } else { 4534 entry->pl_width = zfs_prop_width(prop, 4535 &entry->pl_fixed); 4536 } 4537 4538 *last = entry; 4539 last = &entry->pl_next; 4540 4541 s = p; 4542 if (c == ',') 4543 s++; 4544 } 4545 4546 return (0); 4547 } 4548 4549 int 4550 zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp) 4551 { 4552 return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY)); 4553 } 4554 4555 void 4556 zfs_free_proplist(zfs_proplist_t *pl) 4557 { 4558 zfs_proplist_t *next; 4559 4560 while (pl != NULL) { 4561 next = pl->pl_next; 4562 free(pl->pl_user_prop); 4563 free(pl); 4564 pl = next; 4565 } 4566 } 4567 4568 typedef struct expand_data { 4569 zfs_proplist_t **last; 4570 libzfs_handle_t *hdl; 4571 } expand_data_t; 4572 4573 static zfs_prop_t 4574 zfs_expand_proplist_cb(zfs_prop_t prop, void *cb) 4575 { 4576 zfs_proplist_t *entry; 4577 expand_data_t *edp = cb; 4578 4579 if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL) 4580 return (ZFS_PROP_INVAL); 4581 4582 entry->pl_prop = prop; 4583 entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed); 4584 entry->pl_all = B_TRUE; 4585 4586 *(edp->last) = entry; 4587 edp->last = &entry->pl_next; 4588 4589 return (ZFS_PROP_CONT); 4590 } 4591 4592 int 4593 zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp, 4594 zfs_type_t type) 4595 { 4596 zfs_proplist_t *entry; 4597 zfs_proplist_t **last; 4598 expand_data_t exp; 4599 4600 if (*plp == NULL) { 4601 /* 4602 * If this is the very first time we've been called for an 'all' 4603 * specification, expand the list to include all native 4604 * properties. 4605 */ 4606 last = plp; 4607 4608 exp.last = last; 4609 exp.hdl = hdl; 4610 4611 if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type, 4612 B_FALSE) == ZFS_PROP_INVAL) 4613 return (-1); 4614 4615 /* 4616 * Add 'name' to the beginning of the list, which is handled 4617 * specially. 4618 */ 4619 if ((entry = zfs_alloc(hdl, 4620 sizeof (zfs_proplist_t))) == NULL) 4621 return (-1); 4622 4623 entry->pl_prop = ZFS_PROP_NAME; 4624 entry->pl_width = zfs_prop_width(ZFS_PROP_NAME, 4625 &entry->pl_fixed); 4626 entry->pl_all = B_TRUE; 4627 entry->pl_next = *plp; 4628 *plp = entry; 4629 } 4630 return (0); 4631 } 4632 4633 /* 4634 * This function is used by 'zfs list' to determine the exact set of columns to 4635 * display, and their maximum widths. This does two main things: 4636 * 4637 * - If this is a list of all properties, then expand the list to include 4638 * all native properties, and set a flag so that for each dataset we look 4639 * for new unique user properties and add them to the list. 4640 * 4641 * - For non fixed-width properties, keep track of the maximum width seen 4642 * so that we can size the column appropriately. 4643 */ 4644 int 4645 zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp) 4646 { 4647 libzfs_handle_t *hdl = zhp->zfs_hdl; 4648 zfs_proplist_t *entry; 4649 zfs_proplist_t **last, **start; 4650 nvlist_t *userprops, *propval; 4651 nvpair_t *elem; 4652 char *strval; 4653 char buf[ZFS_MAXPROPLEN]; 4654 4655 if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0) 4656 return (-1); 4657 4658 userprops = zfs_get_user_props(zhp); 4659 4660 entry = *plp; 4661 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 4662 /* 4663 * Go through and add any user properties as necessary. We 4664 * start by incrementing our list pointer to the first 4665 * non-native property. 4666 */ 4667 start = plp; 4668 while (*start != NULL) { 4669 if ((*start)->pl_prop == ZFS_PROP_INVAL) 4670 break; 4671 start = &(*start)->pl_next; 4672 } 4673 4674 elem = NULL; 4675 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 4676 /* 4677 * See if we've already found this property in our list. 4678 */ 4679 for (last = start; *last != NULL; 4680 last = &(*last)->pl_next) { 4681 if (strcmp((*last)->pl_user_prop, 4682 nvpair_name(elem)) == 0) 4683 break; 4684 } 4685 4686 if (*last == NULL) { 4687 if ((entry = zfs_alloc(hdl, 4688 sizeof (zfs_proplist_t))) == NULL || 4689 ((entry->pl_user_prop = zfs_strdup(hdl, 4690 nvpair_name(elem)))) == NULL) { 4691 free(entry); 4692 return (-1); 4693 } 4694 4695 entry->pl_prop = ZFS_PROP_INVAL; 4696 entry->pl_width = strlen(nvpair_name(elem)); 4697 entry->pl_all = B_TRUE; 4698 *last = entry; 4699 } 4700 } 4701 } 4702 4703 /* 4704 * Now go through and check the width of any non-fixed columns 4705 */ 4706 for (entry = *plp; entry != NULL; entry = entry->pl_next) { 4707 if (entry->pl_fixed) 4708 continue; 4709 4710 if (entry->pl_prop != ZFS_PROP_INVAL) { 4711 if (zfs_prop_get(zhp, entry->pl_prop, 4712 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 4713 if (strlen(buf) > entry->pl_width) 4714 entry->pl_width = strlen(buf); 4715 } 4716 } else if (nvlist_lookup_nvlist(userprops, 4717 entry->pl_user_prop, &propval) == 0) { 4718 verify(nvlist_lookup_string(propval, 4719 ZFS_PROP_VALUE, &strval) == 0); 4720 if (strlen(strval) > entry->pl_width) 4721 entry->pl_width = strlen(strval); 4722 } 4723 } 4724 4725 return (0); 4726 } 4727 4728 int 4729 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 4730 { 4731 zfs_cmd_t zc = { 0 }; 4732 nvlist_t *nvp; 4733 size_t sz; 4734 gid_t gid; 4735 uid_t uid; 4736 const gid_t *groups; 4737 int group_cnt; 4738 int error; 4739 4740 if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 4741 return (no_memory(hdl)); 4742 4743 uid = ucred_geteuid(cred); 4744 gid = ucred_getegid(cred); 4745 group_cnt = ucred_getgroups(cred, &groups); 4746 4747 if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 4748 return (1); 4749 4750 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 4751 nvlist_free(nvp); 4752 return (1); 4753 } 4754 4755 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 4756 nvlist_free(nvp); 4757 return (1); 4758 } 4759 4760 if (nvlist_add_uint32_array(nvp, 4761 ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 4762 nvlist_free(nvp); 4763 return (1); 4764 } 4765 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4766 4767 if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz)) 4768 return (-1); 4769 4770 error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 4771 nvlist_free(nvp); 4772 return (error); 4773 } 4774 4775 int 4776 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4777 void *export, void *sharetab, int sharemax, boolean_t share_on) 4778 { 4779 zfs_cmd_t zc = { 0 }; 4780 int error; 4781 4782 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4783 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4784 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 4785 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4786 zc.zc_share.z_sharetype = share_on; 4787 zc.zc_share.z_sharemax = sharemax; 4788 4789 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4790 return (error); 4791 } 4792