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 <stddef.h> 40 #include <zone.h> 41 #include <fcntl.h> 42 #include <sys/mntent.h> 43 #include <sys/mnttab.h> 44 #include <sys/mount.h> 45 #include <sys/avl.h> 46 #include <priv.h> 47 #include <pwd.h> 48 #include <grp.h> 49 #include <stddef.h> 50 #include <ucred.h> 51 52 #include <sys/spa.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 zvol_create_link_common(libzfs_handle_t *, const char *, int); 62 63 /* 64 * Given a single type (not a mask of types), return the type in a human 65 * readable form. 66 */ 67 const char * 68 zfs_type_to_name(zfs_type_t type) 69 { 70 switch (type) { 71 case ZFS_TYPE_FILESYSTEM: 72 return (dgettext(TEXT_DOMAIN, "filesystem")); 73 case ZFS_TYPE_SNAPSHOT: 74 return (dgettext(TEXT_DOMAIN, "snapshot")); 75 case ZFS_TYPE_VOLUME: 76 return (dgettext(TEXT_DOMAIN, "volume")); 77 } 78 79 return (NULL); 80 } 81 82 /* 83 * Given a path and mask of ZFS types, return a string describing this dataset. 84 * This is used when we fail to open a dataset and we cannot get an exact type. 85 * We guess what the type would have been based on the path and the mask of 86 * acceptable types. 87 */ 88 static const char * 89 path_to_str(const char *path, int types) 90 { 91 /* 92 * When given a single type, always report the exact type. 93 */ 94 if (types == ZFS_TYPE_SNAPSHOT) 95 return (dgettext(TEXT_DOMAIN, "snapshot")); 96 if (types == ZFS_TYPE_FILESYSTEM) 97 return (dgettext(TEXT_DOMAIN, "filesystem")); 98 if (types == ZFS_TYPE_VOLUME) 99 return (dgettext(TEXT_DOMAIN, "volume")); 100 101 /* 102 * The user is requesting more than one type of dataset. If this is the 103 * case, consult the path itself. If we're looking for a snapshot, and 104 * a '@' is found, then report it as "snapshot". Otherwise, remove the 105 * snapshot attribute and try again. 106 */ 107 if (types & ZFS_TYPE_SNAPSHOT) { 108 if (strchr(path, '@') != NULL) 109 return (dgettext(TEXT_DOMAIN, "snapshot")); 110 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 111 } 112 113 114 /* 115 * The user has requested either filesystems or volumes. 116 * We have no way of knowing a priori what type this would be, so always 117 * report it as "filesystem" or "volume", our two primitive types. 118 */ 119 if (types & ZFS_TYPE_FILESYSTEM) 120 return (dgettext(TEXT_DOMAIN, "filesystem")); 121 122 assert(types & ZFS_TYPE_VOLUME); 123 return (dgettext(TEXT_DOMAIN, "volume")); 124 } 125 126 /* 127 * Validate a ZFS path. This is used even before trying to open the dataset, to 128 * provide a more meaningful error message. We place a more useful message in 129 * 'buf' detailing exactly why the name was not valid. 130 */ 131 static int 132 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 133 boolean_t modifying) 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 if (modifying && strchr(path, '%') != NULL) { 207 if (hdl != NULL) 208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 209 "invalid character %c in name"), '%'); 210 return (0); 211 } 212 213 return (-1); 214 } 215 216 int 217 zfs_name_valid(const char *name, zfs_type_t type) 218 { 219 return (zfs_validate_name(NULL, name, type, B_FALSE)); 220 } 221 222 /* 223 * This function takes the raw DSL properties, and filters out the user-defined 224 * properties into a separate nvlist. 225 */ 226 static nvlist_t * 227 process_user_props(zfs_handle_t *zhp, nvlist_t *props) 228 { 229 libzfs_handle_t *hdl = zhp->zfs_hdl; 230 nvpair_t *elem; 231 nvlist_t *propval; 232 nvlist_t *nvl; 233 234 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 235 (void) no_memory(hdl); 236 return (NULL); 237 } 238 239 elem = NULL; 240 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 241 if (!zfs_prop_user(nvpair_name(elem))) 242 continue; 243 244 verify(nvpair_value_nvlist(elem, &propval) == 0); 245 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 246 nvlist_free(nvl); 247 (void) no_memory(hdl); 248 return (NULL); 249 } 250 } 251 252 return (nvl); 253 } 254 255 /* 256 * Utility function to gather stats (objset and zpl) for the given object. 257 */ 258 static int 259 get_stats(zfs_handle_t *zhp) 260 { 261 zfs_cmd_t zc = { 0 }; 262 libzfs_handle_t *hdl = zhp->zfs_hdl; 263 nvlist_t *allprops, *userprops; 264 265 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 266 267 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 268 return (-1); 269 270 while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 271 if (errno == ENOMEM) { 272 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 273 zcmd_free_nvlists(&zc); 274 return (-1); 275 } 276 } else { 277 zcmd_free_nvlists(&zc); 278 return (-1); 279 } 280 } 281 282 zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 283 284 (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 285 286 if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 287 zcmd_free_nvlists(&zc); 288 return (-1); 289 } 290 291 zcmd_free_nvlists(&zc); 292 293 if ((userprops = process_user_props(zhp, allprops)) == NULL) { 294 nvlist_free(allprops); 295 return (-1); 296 } 297 298 nvlist_free(zhp->zfs_props); 299 nvlist_free(zhp->zfs_user_props); 300 301 zhp->zfs_props = allprops; 302 zhp->zfs_user_props = userprops; 303 304 return (0); 305 } 306 307 /* 308 * Refresh the properties currently stored in the handle. 309 */ 310 void 311 zfs_refresh_properties(zfs_handle_t *zhp) 312 { 313 (void) get_stats(zhp); 314 } 315 316 /* 317 * Makes a handle from the given dataset name. Used by zfs_open() and 318 * zfs_iter_* to create child handles on the fly. 319 */ 320 zfs_handle_t * 321 make_dataset_handle(libzfs_handle_t *hdl, const char *path) 322 { 323 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 324 char *logstr; 325 326 if (zhp == NULL) 327 return (NULL); 328 329 zhp->zfs_hdl = hdl; 330 331 /* 332 * Preserve history log string. 333 * any changes performed here will be 334 * logged as an internal event. 335 */ 336 logstr = zhp->zfs_hdl->libzfs_log_str; 337 zhp->zfs_hdl->libzfs_log_str = NULL; 338 top: 339 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 340 341 if (get_stats(zhp) != 0) { 342 zhp->zfs_hdl->libzfs_log_str = logstr; 343 free(zhp); 344 return (NULL); 345 } 346 347 if (zhp->zfs_dmustats.dds_inconsistent) { 348 zfs_cmd_t zc = { 0 }; 349 350 /* 351 * If it is dds_inconsistent, then we've caught it in 352 * the middle of a 'zfs receive' or 'zfs destroy', and 353 * it is inconsistent from the ZPL's point of view, so 354 * can't be mounted. However, it could also be that we 355 * have crashed in the middle of one of those 356 * operations, in which case we need to get rid of the 357 * inconsistent state. We do that by either rolling 358 * back to the previous snapshot (which will fail if 359 * there is none), or destroying the filesystem. Note 360 * that if we are still in the middle of an active 361 * 'receive' or 'destroy', then the rollback and destroy 362 * will fail with EBUSY and we will drive on as usual. 363 */ 364 365 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 366 367 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 368 (void) zvol_remove_link(hdl, zhp->zfs_name); 369 zc.zc_objset_type = DMU_OST_ZVOL; 370 } else { 371 zc.zc_objset_type = DMU_OST_ZFS; 372 } 373 374 /* 375 * If we can successfully destroy it, pretend that it 376 * never existed. 377 */ 378 if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 379 zhp->zfs_hdl->libzfs_log_str = logstr; 380 free(zhp); 381 errno = ENOENT; 382 return (NULL); 383 } 384 /* If we can successfully roll it back, reget the stats */ 385 if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 386 goto top; 387 } 388 389 /* 390 * We've managed to open the dataset and gather statistics. Determine 391 * the high-level type. 392 */ 393 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 394 zhp->zfs_head_type = ZFS_TYPE_VOLUME; 395 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 396 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 397 else 398 abort(); 399 400 if (zhp->zfs_dmustats.dds_is_snapshot) 401 zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 402 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 403 zhp->zfs_type = ZFS_TYPE_VOLUME; 404 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 405 zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 406 else 407 abort(); /* we should never see any other types */ 408 409 zhp->zfs_hdl->libzfs_log_str = logstr; 410 return (zhp); 411 } 412 413 /* 414 * Opens the given snapshot, filesystem, or volume. The 'types' 415 * argument is a mask of acceptable types. The function will print an 416 * appropriate error message and return NULL if it can't be opened. 417 */ 418 zfs_handle_t * 419 zfs_open(libzfs_handle_t *hdl, const char *path, int types) 420 { 421 zfs_handle_t *zhp; 422 char errbuf[1024]; 423 424 (void) snprintf(errbuf, sizeof (errbuf), 425 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 426 427 /* 428 * Validate the name before we even try to open it. 429 */ 430 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 432 "invalid dataset name")); 433 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 434 return (NULL); 435 } 436 437 /* 438 * Try to get stats for the dataset, which will tell us if it exists. 439 */ 440 errno = 0; 441 if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 442 (void) zfs_standard_error(hdl, errno, errbuf); 443 return (NULL); 444 } 445 446 if (!(types & zhp->zfs_type)) { 447 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 448 zfs_close(zhp); 449 return (NULL); 450 } 451 452 return (zhp); 453 } 454 455 /* 456 * Release a ZFS handle. Nothing to do but free the associated memory. 457 */ 458 void 459 zfs_close(zfs_handle_t *zhp) 460 { 461 if (zhp->zfs_mntopts) 462 free(zhp->zfs_mntopts); 463 nvlist_free(zhp->zfs_props); 464 nvlist_free(zhp->zfs_user_props); 465 free(zhp); 466 } 467 468 int 469 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 470 { 471 char *pool_name; 472 zpool_handle_t *zpool_handle; 473 char *p; 474 475 pool_name = zfs_alloc(zhp->zfs_hdl, MAXPATHLEN); 476 if (zfs_prop_get(zhp, ZFS_PROP_NAME, pool_name, 477 MAXPATHLEN, NULL, NULL, 0, B_FALSE) != 0) { 478 free(pool_name); 479 return (-1); 480 } 481 482 if (p = strchr(pool_name, '/')) 483 *p = '\0'; 484 zpool_handle = zpool_open(zhp->zfs_hdl, pool_name); 485 free(pool_name); 486 if (zpool_handle == NULL) 487 return (-1); 488 489 *spa_version = zpool_get_prop_int(zpool_handle, 490 ZPOOL_PROP_VERSION, NULL); 491 zpool_close(zpool_handle); 492 return (0); 493 } 494 495 /* 496 * The choice of reservation property depends on the SPA version. 497 */ 498 static int 499 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 500 { 501 int spa_version; 502 503 if (zfs_spa_version(zhp, &spa_version) < 0) 504 return (-1); 505 506 if (spa_version >= SPA_VERSION_REFRESERVATION) 507 *resv_prop = ZFS_PROP_REFRESERVATION; 508 else 509 *resv_prop = ZFS_PROP_RESERVATION; 510 511 return (0); 512 } 513 514 /* 515 * Given an nvlist of properties to set, validates that they are correct, and 516 * parses any numeric properties (index, boolean, etc) if they are specified as 517 * strings. 518 */ 519 static nvlist_t * 520 zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 521 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 522 { 523 nvpair_t *elem; 524 uint64_t intval; 525 char *strval; 526 zfs_prop_t prop; 527 nvlist_t *ret; 528 int chosen_normal = -1; 529 int chosen_utf = -1; 530 531 if (type == ZFS_TYPE_SNAPSHOT) { 532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 533 "snapshot properties cannot be modified")); 534 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 535 return (NULL); 536 } 537 538 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 539 (void) no_memory(hdl); 540 return (NULL); 541 } 542 543 elem = NULL; 544 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 545 const char *propname = nvpair_name(elem); 546 547 /* 548 * Make sure this property is valid and applies to this type. 549 */ 550 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 551 if (!zfs_prop_user(propname)) { 552 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 553 "invalid property '%s'"), propname); 554 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 555 goto error; 556 } 557 558 /* 559 * If this is a user property, make sure it's a 560 * string, and that it's less than ZAP_MAXNAMELEN. 561 */ 562 if (nvpair_type(elem) != DATA_TYPE_STRING) { 563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 564 "'%s' must be a string"), propname); 565 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 566 goto error; 567 } 568 569 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 571 "property name '%s' is too long"), 572 propname); 573 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 574 goto error; 575 } 576 577 (void) nvpair_value_string(elem, &strval); 578 if (nvlist_add_string(ret, propname, strval) != 0) { 579 (void) no_memory(hdl); 580 goto error; 581 } 582 continue; 583 } 584 585 if (!zfs_prop_valid_for_type(prop, type)) { 586 zfs_error_aux(hdl, 587 dgettext(TEXT_DOMAIN, "'%s' does not " 588 "apply to datasets of this type"), propname); 589 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 590 goto error; 591 } 592 593 if (zfs_prop_readonly(prop) && 594 (!zfs_prop_setonce(prop) || zhp != NULL)) { 595 zfs_error_aux(hdl, 596 dgettext(TEXT_DOMAIN, "'%s' is readonly"), 597 propname); 598 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 599 goto error; 600 } 601 602 if (zprop_parse_value(hdl, elem, prop, type, ret, 603 &strval, &intval, errbuf) != 0) 604 goto error; 605 606 /* 607 * Perform some additional checks for specific properties. 608 */ 609 switch (prop) { 610 case ZFS_PROP_VERSION: 611 { 612 int version; 613 614 if (zhp == NULL) 615 break; 616 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 617 if (intval < version) { 618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 619 "Can not downgrade; already at version %u"), 620 version); 621 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 622 goto error; 623 } 624 break; 625 } 626 627 case ZFS_PROP_RECORDSIZE: 628 case ZFS_PROP_VOLBLOCKSIZE: 629 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 630 if (intval < SPA_MINBLOCKSIZE || 631 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 633 "'%s' must be power of 2 from %u " 634 "to %uk"), propname, 635 (uint_t)SPA_MINBLOCKSIZE, 636 (uint_t)SPA_MAXBLOCKSIZE >> 10); 637 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 638 goto error; 639 } 640 break; 641 642 case ZFS_PROP_SHAREISCSI: 643 if (strcmp(strval, "off") != 0 && 644 strcmp(strval, "on") != 0 && 645 strcmp(strval, "type=disk") != 0) { 646 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 647 "'%s' must be 'on', 'off', or 'type=disk'"), 648 propname); 649 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 650 goto error; 651 } 652 653 break; 654 655 case ZFS_PROP_MOUNTPOINT: 656 { 657 namecheck_err_t why; 658 659 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 660 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 661 break; 662 663 if (mountpoint_namecheck(strval, &why)) { 664 switch (why) { 665 case NAME_ERR_LEADING_SLASH: 666 zfs_error_aux(hdl, 667 dgettext(TEXT_DOMAIN, 668 "'%s' must be an absolute path, " 669 "'none', or 'legacy'"), propname); 670 break; 671 case NAME_ERR_TOOLONG: 672 zfs_error_aux(hdl, 673 dgettext(TEXT_DOMAIN, 674 "component of '%s' is too long"), 675 propname); 676 break; 677 } 678 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 679 goto error; 680 } 681 } 682 683 /*FALLTHRU*/ 684 685 case ZFS_PROP_SHARESMB: 686 case ZFS_PROP_SHARENFS: 687 /* 688 * For the mountpoint and sharenfs or sharesmb 689 * properties, check if it can be set in a 690 * global/non-global zone based on 691 * the zoned property value: 692 * 693 * global zone non-global zone 694 * -------------------------------------------------- 695 * zoned=on mountpoint (no) mountpoint (yes) 696 * sharenfs (no) sharenfs (no) 697 * sharesmb (no) sharesmb (no) 698 * 699 * zoned=off mountpoint (yes) N/A 700 * sharenfs (yes) 701 * sharesmb (yes) 702 */ 703 if (zoned) { 704 if (getzoneid() == GLOBAL_ZONEID) { 705 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 706 "'%s' cannot be set on " 707 "dataset in a non-global zone"), 708 propname); 709 (void) zfs_error(hdl, EZFS_ZONED, 710 errbuf); 711 goto error; 712 } else if (prop == ZFS_PROP_SHARENFS || 713 prop == ZFS_PROP_SHARESMB) { 714 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 715 "'%s' cannot be set in " 716 "a non-global zone"), propname); 717 (void) zfs_error(hdl, EZFS_ZONED, 718 errbuf); 719 goto error; 720 } 721 } else if (getzoneid() != GLOBAL_ZONEID) { 722 /* 723 * If zoned property is 'off', this must be in 724 * a globle zone. If not, something is wrong. 725 */ 726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 727 "'%s' cannot be set while dataset " 728 "'zoned' property is set"), propname); 729 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 730 goto error; 731 } 732 733 /* 734 * At this point, it is legitimate to set the 735 * property. Now we want to make sure that the 736 * property value is valid if it is sharenfs. 737 */ 738 if ((prop == ZFS_PROP_SHARENFS || 739 prop == ZFS_PROP_SHARESMB) && 740 strcmp(strval, "on") != 0 && 741 strcmp(strval, "off") != 0) { 742 zfs_share_proto_t proto; 743 744 if (prop == ZFS_PROP_SHARESMB) 745 proto = PROTO_SMB; 746 else 747 proto = PROTO_NFS; 748 749 /* 750 * Must be an valid sharing protocol 751 * option string so init the libshare 752 * in order to enable the parser and 753 * then parse the options. We use the 754 * control API since we don't care about 755 * the current configuration and don't 756 * want the overhead of loading it 757 * until we actually do something. 758 */ 759 760 if (zfs_init_libshare(hdl, 761 SA_INIT_CONTROL_API) != SA_OK) { 762 /* 763 * An error occurred so we can't do 764 * anything 765 */ 766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 767 "'%s' cannot be set: problem " 768 "in share initialization"), 769 propname); 770 (void) zfs_error(hdl, EZFS_BADPROP, 771 errbuf); 772 goto error; 773 } 774 775 if (zfs_parse_options(strval, proto) != SA_OK) { 776 /* 777 * There was an error in parsing so 778 * deal with it by issuing an error 779 * message and leaving after 780 * uninitializing the the libshare 781 * interface. 782 */ 783 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 784 "'%s' cannot be set to invalid " 785 "options"), propname); 786 (void) zfs_error(hdl, EZFS_BADPROP, 787 errbuf); 788 zfs_uninit_libshare(hdl); 789 goto error; 790 } 791 zfs_uninit_libshare(hdl); 792 } 793 794 break; 795 case ZFS_PROP_UTF8ONLY: 796 chosen_utf = (int)intval; 797 break; 798 case ZFS_PROP_NORMALIZE: 799 chosen_normal = (int)intval; 800 break; 801 } 802 803 /* 804 * For changes to existing volumes, we have some additional 805 * checks to enforce. 806 */ 807 if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 808 uint64_t volsize = zfs_prop_get_int(zhp, 809 ZFS_PROP_VOLSIZE); 810 uint64_t blocksize = zfs_prop_get_int(zhp, 811 ZFS_PROP_VOLBLOCKSIZE); 812 char buf[64]; 813 814 switch (prop) { 815 case ZFS_PROP_RESERVATION: 816 case ZFS_PROP_REFRESERVATION: 817 if (intval > volsize) { 818 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 819 "'%s' is greater than current " 820 "volume size"), propname); 821 (void) zfs_error(hdl, EZFS_BADPROP, 822 errbuf); 823 goto error; 824 } 825 break; 826 827 case ZFS_PROP_VOLSIZE: 828 if (intval % blocksize != 0) { 829 zfs_nicenum(blocksize, buf, 830 sizeof (buf)); 831 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 832 "'%s' must be a multiple of " 833 "volume block size (%s)"), 834 propname, buf); 835 (void) zfs_error(hdl, EZFS_BADPROP, 836 errbuf); 837 goto error; 838 } 839 840 if (intval == 0) { 841 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 842 "'%s' cannot be zero"), 843 propname); 844 (void) zfs_error(hdl, EZFS_BADPROP, 845 errbuf); 846 goto error; 847 } 848 break; 849 } 850 } 851 } 852 853 /* 854 * If normalization was chosen, but no UTF8 choice was made, 855 * enforce rejection of non-UTF8 names. 856 * 857 * If normalization was chosen, but rejecting non-UTF8 names 858 * was explicitly not chosen, it is an error. 859 */ 860 if (chosen_normal > 0 && chosen_utf < 0) { 861 if (nvlist_add_uint64(ret, 862 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 863 (void) no_memory(hdl); 864 goto error; 865 } 866 } else if (chosen_normal > 0 && chosen_utf == 0) { 867 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 868 "'%s' must be set 'on' if normalization chosen"), 869 zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 870 (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 871 goto error; 872 } 873 874 /* 875 * If this is an existing volume, and someone is setting the volsize, 876 * make sure that it matches the reservation, or add it if necessary. 877 */ 878 if (zhp != NULL && type == ZFS_TYPE_VOLUME && 879 nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 880 &intval) == 0) { 881 uint64_t old_volsize = zfs_prop_get_int(zhp, 882 ZFS_PROP_VOLSIZE); 883 uint64_t old_reservation; 884 uint64_t new_reservation; 885 zfs_prop_t resv_prop; 886 887 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 888 goto error; 889 old_reservation = zfs_prop_get_int(zhp, resv_prop); 890 891 if (old_volsize == old_reservation && 892 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop), 893 &new_reservation) != 0) { 894 if (nvlist_add_uint64(ret, 895 zfs_prop_to_name(resv_prop), intval) != 0) { 896 (void) no_memory(hdl); 897 goto error; 898 } 899 } 900 } 901 return (ret); 902 903 error: 904 nvlist_free(ret); 905 return (NULL); 906 } 907 908 static int 909 zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 910 uint64_t *ret_who) 911 { 912 struct passwd *pwd; 913 struct group *grp; 914 uid_t id; 915 916 if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 917 *who_type == ZFS_DELEG_NAMED_SET) { 918 *ret_who = -1; 919 return (0); 920 } 921 if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 922 return (EZFS_BADWHO); 923 924 if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 925 strcmp(who, "everyone") == 0) { 926 *ret_who = -1; 927 *who_type = ZFS_DELEG_EVERYONE; 928 return (0); 929 } 930 931 pwd = getpwnam(who); 932 grp = getgrnam(who); 933 934 if ((*who_type == ZFS_DELEG_USER) && pwd) { 935 *ret_who = pwd->pw_uid; 936 } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 937 *ret_who = grp->gr_gid; 938 } else if (pwd) { 939 *ret_who = pwd->pw_uid; 940 *who_type = ZFS_DELEG_USER; 941 } else if (grp) { 942 *ret_who = grp->gr_gid; 943 *who_type = ZFS_DELEG_GROUP; 944 } else { 945 char *end; 946 947 id = strtol(who, &end, 10); 948 if (errno != 0 || *end != '\0') { 949 return (EZFS_BADWHO); 950 } else { 951 *ret_who = id; 952 if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 953 *who_type = ZFS_DELEG_USER; 954 } 955 } 956 957 return (0); 958 } 959 960 static void 961 zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 962 { 963 if (perms_nvp != NULL) { 964 verify(nvlist_add_nvlist(who_nvp, 965 name, perms_nvp) == 0); 966 } else { 967 verify(nvlist_add_boolean(who_nvp, name) == 0); 968 } 969 } 970 971 static void 972 helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 973 zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 974 nvlist_t *sets_nvp) 975 { 976 boolean_t do_perms, do_sets; 977 char name[ZFS_MAX_DELEG_NAME]; 978 979 do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 980 do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 981 982 if (!do_perms && !do_sets) 983 do_perms = do_sets = B_TRUE; 984 985 if (do_perms) { 986 zfs_deleg_whokey(name, who_type, inherit, 987 (who_type == ZFS_DELEG_NAMED_SET) ? 988 whostr : (void *)&whoid); 989 zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 990 } 991 if (do_sets) { 992 zfs_deleg_whokey(name, toupper(who_type), inherit, 993 (who_type == ZFS_DELEG_NAMED_SET) ? 994 whostr : (void *)&whoid); 995 zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 996 } 997 } 998 999 static void 1000 zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 1001 nvlist_t *perms_nvp, nvlist_t *sets_nvp, 1002 zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 1003 { 1004 if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 1005 helper(who_type, whoid, whostr, 0, 1006 who_nvp, perms_nvp, sets_nvp); 1007 } else { 1008 if (inherit & ZFS_DELEG_PERM_LOCAL) { 1009 helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 1010 who_nvp, perms_nvp, sets_nvp); 1011 } 1012 if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 1013 helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 1014 who_nvp, perms_nvp, sets_nvp); 1015 } 1016 } 1017 } 1018 1019 /* 1020 * Construct nvlist to pass down to kernel for setting/removing permissions. 1021 * 1022 * The nvlist is constructed as a series of nvpairs with an optional embedded 1023 * nvlist of permissions to remove or set. The topmost nvpairs are the actual 1024 * base attribute named stored in the dsl. 1025 * Arguments: 1026 * 1027 * whostr: is a comma separated list of users, groups, or a single set name. 1028 * whostr may be null for everyone or create perms. 1029 * who_type: is the type of entry in whostr. Typically this will be 1030 * ZFS_DELEG_WHO_UNKNOWN. 1031 * perms: common separated list of permissions. May be null if user 1032 * is requested to remove permissions by who. 1033 * inherit: Specifies the inheritance of the permissions. Will be either 1034 * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 1035 * nvp The constructed nvlist to pass to zfs_perm_set(). 1036 * The output nvp will look something like this. 1037 * ul$1234 -> {create ; destroy } 1038 * Ul$1234 -> { @myset } 1039 * s-$@myset - { snapshot; checksum; compression } 1040 */ 1041 int 1042 zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 1043 zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 1044 { 1045 nvlist_t *who_nvp; 1046 nvlist_t *perms_nvp = NULL; 1047 nvlist_t *sets_nvp = NULL; 1048 char errbuf[1024]; 1049 char *who_tok, *perm; 1050 int error; 1051 1052 *nvp = NULL; 1053 1054 if (perms) { 1055 if ((error = nvlist_alloc(&perms_nvp, 1056 NV_UNIQUE_NAME, 0)) != 0) { 1057 return (1); 1058 } 1059 if ((error = nvlist_alloc(&sets_nvp, 1060 NV_UNIQUE_NAME, 0)) != 0) { 1061 nvlist_free(perms_nvp); 1062 return (1); 1063 } 1064 } 1065 1066 if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 1067 if (perms_nvp) 1068 nvlist_free(perms_nvp); 1069 if (sets_nvp) 1070 nvlist_free(sets_nvp); 1071 return (1); 1072 } 1073 1074 if (who_type == ZFS_DELEG_NAMED_SET) { 1075 namecheck_err_t why; 1076 char what; 1077 1078 if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 1079 nvlist_free(who_nvp); 1080 if (perms_nvp) 1081 nvlist_free(perms_nvp); 1082 if (sets_nvp) 1083 nvlist_free(sets_nvp); 1084 1085 switch (why) { 1086 case NAME_ERR_NO_AT: 1087 zfs_error_aux(zhp->zfs_hdl, 1088 dgettext(TEXT_DOMAIN, 1089 "set definition must begin with an '@' " 1090 "character")); 1091 } 1092 return (zfs_error(zhp->zfs_hdl, 1093 EZFS_BADPERMSET, whostr)); 1094 } 1095 } 1096 1097 /* 1098 * Build up nvlist(s) of permissions. Two nvlists are maintained. 1099 * The first nvlist perms_nvp will have normal permissions and the 1100 * other sets_nvp will have only permssion set names in it. 1101 */ 1102 for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 1103 const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 1104 1105 if (perm_canonical) { 1106 verify(nvlist_add_boolean(perms_nvp, 1107 perm_canonical) == 0); 1108 } else if (perm[0] == '@') { 1109 verify(nvlist_add_boolean(sets_nvp, perm) == 0); 1110 } else { 1111 nvlist_free(who_nvp); 1112 nvlist_free(perms_nvp); 1113 nvlist_free(sets_nvp); 1114 return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 1115 } 1116 } 1117 1118 if (whostr && who_type != ZFS_DELEG_CREATE) { 1119 who_tok = strtok(whostr, ","); 1120 if (who_tok == NULL) { 1121 nvlist_free(who_nvp); 1122 if (perms_nvp) 1123 nvlist_free(perms_nvp); 1124 if (sets_nvp) 1125 nvlist_free(sets_nvp); 1126 (void) snprintf(errbuf, sizeof (errbuf), 1127 dgettext(TEXT_DOMAIN, "Who string is NULL"), 1128 whostr); 1129 return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 1130 } 1131 } 1132 1133 /* 1134 * Now create the nvlist(s) 1135 */ 1136 do { 1137 uint64_t who_id; 1138 1139 error = zfs_get_perm_who(who_tok, &who_type, 1140 &who_id); 1141 if (error) { 1142 nvlist_free(who_nvp); 1143 if (perms_nvp) 1144 nvlist_free(perms_nvp); 1145 if (sets_nvp) 1146 nvlist_free(sets_nvp); 1147 (void) snprintf(errbuf, sizeof (errbuf), 1148 dgettext(TEXT_DOMAIN, 1149 "Unable to determine uid/gid for " 1150 "%s "), who_tok); 1151 return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 1152 } 1153 1154 /* 1155 * add entries for both local and descendent when required 1156 */ 1157 zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 1158 perms_nvp, sets_nvp, who_type, inherit); 1159 1160 } while (who_tok = strtok(NULL, ",")); 1161 *nvp = who_nvp; 1162 return (0); 1163 } 1164 1165 static int 1166 zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 1167 { 1168 zfs_cmd_t zc = { 0 }; 1169 int error; 1170 char errbuf[1024]; 1171 1172 (void) snprintf(errbuf, sizeof (errbuf), 1173 dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 1174 zhp->zfs_name); 1175 1176 if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 1177 return (-1); 1178 1179 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1180 zc.zc_perm_action = unset; 1181 1182 error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 1183 if (error && errno == ENOTSUP) { 1184 (void) snprintf(errbuf, sizeof (errbuf), 1185 gettext("Pool must be upgraded to use 'allow/unallow'")); 1186 zcmd_free_nvlists(&zc); 1187 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 1188 } else if (error) { 1189 return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 1190 } 1191 zcmd_free_nvlists(&zc); 1192 1193 return (error); 1194 } 1195 1196 int 1197 zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 1198 { 1199 return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 1200 } 1201 1202 int 1203 zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 1204 { 1205 return (zfs_perm_set_common(zhp, perms, B_TRUE)); 1206 } 1207 1208 static int 1209 perm_compare(const void *arg1, const void *arg2) 1210 { 1211 const zfs_perm_node_t *node1 = arg1; 1212 const zfs_perm_node_t *node2 = arg2; 1213 int ret; 1214 1215 ret = strcmp(node1->z_pname, node2->z_pname); 1216 1217 if (ret > 0) 1218 return (1); 1219 if (ret < 0) 1220 return (-1); 1221 else 1222 return (0); 1223 } 1224 1225 static void 1226 zfs_destroy_perm_tree(avl_tree_t *tree) 1227 { 1228 zfs_perm_node_t *permnode; 1229 void *cookie = NULL; 1230 1231 while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 1232 free(permnode); 1233 avl_destroy(tree); 1234 } 1235 1236 static void 1237 zfs_destroy_tree(avl_tree_t *tree) 1238 { 1239 zfs_allow_node_t *allownode; 1240 void *cookie = NULL; 1241 1242 while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 1243 zfs_destroy_perm_tree(&allownode->z_localdescend); 1244 zfs_destroy_perm_tree(&allownode->z_local); 1245 zfs_destroy_perm_tree(&allownode->z_descend); 1246 free(allownode); 1247 } 1248 avl_destroy(tree); 1249 } 1250 1251 void 1252 zfs_free_allows(zfs_allow_t *allow) 1253 { 1254 zfs_allow_t *allownext; 1255 zfs_allow_t *freeallow; 1256 1257 allownext = allow; 1258 while (allownext) { 1259 zfs_destroy_tree(&allownext->z_sets); 1260 zfs_destroy_tree(&allownext->z_crperms); 1261 zfs_destroy_tree(&allownext->z_user); 1262 zfs_destroy_tree(&allownext->z_group); 1263 zfs_destroy_tree(&allownext->z_everyone); 1264 freeallow = allownext; 1265 allownext = allownext->z_next; 1266 free(freeallow); 1267 } 1268 } 1269 1270 static zfs_allow_t * 1271 zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 1272 { 1273 zfs_allow_t *ptree; 1274 1275 if ((ptree = zfs_alloc(zhp->zfs_hdl, 1276 sizeof (zfs_allow_t))) == NULL) { 1277 return (NULL); 1278 } 1279 1280 (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 1281 avl_create(&ptree->z_sets, 1282 perm_compare, sizeof (zfs_allow_node_t), 1283 offsetof(zfs_allow_node_t, z_node)); 1284 avl_create(&ptree->z_crperms, 1285 perm_compare, sizeof (zfs_allow_node_t), 1286 offsetof(zfs_allow_node_t, z_node)); 1287 avl_create(&ptree->z_user, 1288 perm_compare, sizeof (zfs_allow_node_t), 1289 offsetof(zfs_allow_node_t, z_node)); 1290 avl_create(&ptree->z_group, 1291 perm_compare, sizeof (zfs_allow_node_t), 1292 offsetof(zfs_allow_node_t, z_node)); 1293 avl_create(&ptree->z_everyone, 1294 perm_compare, sizeof (zfs_allow_node_t), 1295 offsetof(zfs_allow_node_t, z_node)); 1296 1297 if (prev) 1298 prev->z_next = ptree; 1299 ptree->z_next = NULL; 1300 return (ptree); 1301 } 1302 1303 /* 1304 * Add permissions to the appropriate AVL permission tree. 1305 * The appropriate tree may not be the requested tree. 1306 * For example if ld indicates a local permission, but 1307 * same permission also exists as a descendent permission 1308 * then the permission will be removed from the descendent 1309 * tree and add the the local+descendent tree. 1310 */ 1311 static int 1312 zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 1313 char *perm, char ld) 1314 { 1315 zfs_perm_node_t pnode, *permnode, *permnode2; 1316 zfs_perm_node_t *newnode; 1317 avl_index_t where, where2; 1318 avl_tree_t *tree, *altree; 1319 1320 (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 1321 1322 if (ld == ZFS_DELEG_NA) { 1323 tree = &allownode->z_localdescend; 1324 altree = &allownode->z_descend; 1325 } else if (ld == ZFS_DELEG_LOCAL) { 1326 tree = &allownode->z_local; 1327 altree = &allownode->z_descend; 1328 } else { 1329 tree = &allownode->z_descend; 1330 altree = &allownode->z_local; 1331 } 1332 permnode = avl_find(tree, &pnode, &where); 1333 permnode2 = avl_find(altree, &pnode, &where2); 1334 1335 if (permnode2) { 1336 avl_remove(altree, permnode2); 1337 free(permnode2); 1338 if (permnode == NULL) { 1339 tree = &allownode->z_localdescend; 1340 } 1341 } 1342 1343 /* 1344 * Now insert new permission in either requested location 1345 * local/descendent or into ld when perm will exist in both. 1346 */ 1347 if (permnode == NULL) { 1348 if ((newnode = zfs_alloc(zhp->zfs_hdl, 1349 sizeof (zfs_perm_node_t))) == NULL) { 1350 return (-1); 1351 } 1352 *newnode = pnode; 1353 avl_add(tree, newnode); 1354 } 1355 return (0); 1356 } 1357 1358 /* 1359 * Uggh, this is going to be a bit complicated. 1360 * we have an nvlist coming out of the kernel that 1361 * will indicate where the permission is set and then 1362 * it will contain allow of the various "who's", and what 1363 * their permissions are. To further complicate this 1364 * we will then have to coalesce the local,descendent 1365 * and local+descendent permissions where appropriate. 1366 * The kernel only knows about a permission as being local 1367 * or descendent, but not both. 1368 * 1369 * In order to make this easier for zfs_main to deal with 1370 * a series of AVL trees will be used to maintain 1371 * all of this, primarily for sorting purposes as well 1372 * as the ability to quickly locate a specific entry. 1373 * 1374 * What we end up with are tree's for sets, create perms, 1375 * user, groups and everyone. With each of those trees 1376 * we have subtrees for local, descendent and local+descendent 1377 * permissions. 1378 */ 1379 int 1380 zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 1381 { 1382 zfs_cmd_t zc = { 0 }; 1383 int error; 1384 nvlist_t *nvlist; 1385 nvlist_t *permnv, *sourcenv; 1386 nvpair_t *who_pair, *source_pair; 1387 nvpair_t *perm_pair; 1388 char errbuf[1024]; 1389 zfs_allow_t *zallowp, *newallowp; 1390 char ld; 1391 char *nvpname; 1392 uid_t uid; 1393 gid_t gid; 1394 avl_tree_t *tree; 1395 avl_index_t where; 1396 1397 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1398 1399 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 1400 return (-1); 1401 1402 while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 1403 if (errno == ENOMEM) { 1404 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 1405 zcmd_free_nvlists(&zc); 1406 return (-1); 1407 } 1408 } else if (errno == ENOTSUP) { 1409 zcmd_free_nvlists(&zc); 1410 (void) snprintf(errbuf, sizeof (errbuf), 1411 gettext("Pool must be upgraded to use 'allow'")); 1412 return (zfs_error(zhp->zfs_hdl, 1413 EZFS_BADVERSION, errbuf)); 1414 } else { 1415 zcmd_free_nvlists(&zc); 1416 return (-1); 1417 } 1418 } 1419 1420 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 1421 zcmd_free_nvlists(&zc); 1422 return (-1); 1423 } 1424 1425 zcmd_free_nvlists(&zc); 1426 1427 source_pair = nvlist_next_nvpair(nvlist, NULL); 1428 1429 if (source_pair == NULL) { 1430 *zfs_perms = NULL; 1431 return (0); 1432 } 1433 1434 *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 1435 if (*zfs_perms == NULL) { 1436 return (0); 1437 } 1438 1439 zallowp = *zfs_perms; 1440 1441 for (;;) { 1442 struct passwd *pwd; 1443 struct group *grp; 1444 zfs_allow_node_t *allownode; 1445 zfs_allow_node_t findallownode; 1446 zfs_allow_node_t *newallownode; 1447 1448 (void) strlcpy(zallowp->z_setpoint, 1449 nvpair_name(source_pair), 1450 sizeof (zallowp->z_setpoint)); 1451 1452 if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 1453 goto abort; 1454 1455 /* 1456 * Make sure nvlist is composed correctly 1457 */ 1458 if (zfs_deleg_verify_nvlist(sourcenv)) { 1459 goto abort; 1460 } 1461 1462 who_pair = nvlist_next_nvpair(sourcenv, NULL); 1463 if (who_pair == NULL) { 1464 goto abort; 1465 } 1466 1467 do { 1468 error = nvpair_value_nvlist(who_pair, &permnv); 1469 if (error) { 1470 goto abort; 1471 } 1472 1473 /* 1474 * First build up the key to use 1475 * for looking up in the various 1476 * who trees. 1477 */ 1478 ld = nvpair_name(who_pair)[1]; 1479 nvpname = nvpair_name(who_pair); 1480 switch (nvpair_name(who_pair)[0]) { 1481 case ZFS_DELEG_USER: 1482 case ZFS_DELEG_USER_SETS: 1483 tree = &zallowp->z_user; 1484 uid = atol(&nvpname[3]); 1485 pwd = getpwuid(uid); 1486 (void) snprintf(findallownode.z_key, 1487 sizeof (findallownode.z_key), "user %s", 1488 (pwd) ? pwd->pw_name : 1489 &nvpair_name(who_pair)[3]); 1490 break; 1491 case ZFS_DELEG_GROUP: 1492 case ZFS_DELEG_GROUP_SETS: 1493 tree = &zallowp->z_group; 1494 gid = atol(&nvpname[3]); 1495 grp = getgrgid(gid); 1496 (void) snprintf(findallownode.z_key, 1497 sizeof (findallownode.z_key), "group %s", 1498 (grp) ? grp->gr_name : 1499 &nvpair_name(who_pair)[3]); 1500 break; 1501 case ZFS_DELEG_CREATE: 1502 case ZFS_DELEG_CREATE_SETS: 1503 tree = &zallowp->z_crperms; 1504 (void) strlcpy(findallownode.z_key, "", 1505 sizeof (findallownode.z_key)); 1506 break; 1507 case ZFS_DELEG_EVERYONE: 1508 case ZFS_DELEG_EVERYONE_SETS: 1509 (void) snprintf(findallownode.z_key, 1510 sizeof (findallownode.z_key), "everyone"); 1511 tree = &zallowp->z_everyone; 1512 break; 1513 case ZFS_DELEG_NAMED_SET: 1514 case ZFS_DELEG_NAMED_SET_SETS: 1515 (void) snprintf(findallownode.z_key, 1516 sizeof (findallownode.z_key), "%s", 1517 &nvpair_name(who_pair)[3]); 1518 tree = &zallowp->z_sets; 1519 break; 1520 } 1521 1522 /* 1523 * Place who in tree 1524 */ 1525 allownode = avl_find(tree, &findallownode, &where); 1526 if (allownode == NULL) { 1527 if ((newallownode = zfs_alloc(zhp->zfs_hdl, 1528 sizeof (zfs_allow_node_t))) == NULL) { 1529 goto abort; 1530 } 1531 avl_create(&newallownode->z_localdescend, 1532 perm_compare, 1533 sizeof (zfs_perm_node_t), 1534 offsetof(zfs_perm_node_t, z_node)); 1535 avl_create(&newallownode->z_local, 1536 perm_compare, 1537 sizeof (zfs_perm_node_t), 1538 offsetof(zfs_perm_node_t, z_node)); 1539 avl_create(&newallownode->z_descend, 1540 perm_compare, 1541 sizeof (zfs_perm_node_t), 1542 offsetof(zfs_perm_node_t, z_node)); 1543 (void) strlcpy(newallownode->z_key, 1544 findallownode.z_key, 1545 sizeof (findallownode.z_key)); 1546 avl_insert(tree, newallownode, where); 1547 allownode = newallownode; 1548 } 1549 1550 /* 1551 * Now iterate over the permissions and 1552 * place them in the appropriate local, 1553 * descendent or local+descendent tree. 1554 * 1555 * The permissions are added to the tree 1556 * via zfs_coalesce_perm(). 1557 */ 1558 perm_pair = nvlist_next_nvpair(permnv, NULL); 1559 if (perm_pair == NULL) 1560 goto abort; 1561 do { 1562 if (zfs_coalesce_perm(zhp, allownode, 1563 nvpair_name(perm_pair), ld) != 0) 1564 goto abort; 1565 } while (perm_pair = nvlist_next_nvpair(permnv, 1566 perm_pair)); 1567 } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 1568 1569 source_pair = nvlist_next_nvpair(nvlist, source_pair); 1570 if (source_pair == NULL) 1571 break; 1572 1573 /* 1574 * allocate another node from the link list of 1575 * zfs_allow_t structures 1576 */ 1577 newallowp = zfs_alloc_perm_tree(zhp, zallowp, 1578 nvpair_name(source_pair)); 1579 if (newallowp == NULL) { 1580 goto abort; 1581 } 1582 zallowp = newallowp; 1583 } 1584 nvlist_free(nvlist); 1585 return (0); 1586 abort: 1587 zfs_free_allows(*zfs_perms); 1588 nvlist_free(nvlist); 1589 return (-1); 1590 } 1591 1592 /* 1593 * Given a property name and value, set the property for the given dataset. 1594 */ 1595 int 1596 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1597 { 1598 zfs_cmd_t zc = { 0 }; 1599 int ret = -1; 1600 prop_changelist_t *cl = NULL; 1601 char errbuf[1024]; 1602 libzfs_handle_t *hdl = zhp->zfs_hdl; 1603 nvlist_t *nvl = NULL, *realprops; 1604 zfs_prop_t prop; 1605 1606 (void) snprintf(errbuf, sizeof (errbuf), 1607 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1608 zhp->zfs_name); 1609 1610 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1611 nvlist_add_string(nvl, propname, propval) != 0) { 1612 (void) no_memory(hdl); 1613 goto error; 1614 } 1615 1616 if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 1617 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 1618 goto error; 1619 1620 nvlist_free(nvl); 1621 nvl = realprops; 1622 1623 prop = zfs_name_to_prop(propname); 1624 1625 if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1626 goto error; 1627 1628 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 1629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1630 "child dataset with inherited mountpoint is used " 1631 "in a non-global zone")); 1632 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1633 goto error; 1634 } 1635 1636 if ((ret = changelist_prefix(cl)) != 0) 1637 goto error; 1638 1639 /* 1640 * Execute the corresponding ioctl() to set this property. 1641 */ 1642 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1643 1644 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 1645 goto error; 1646 1647 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1648 if (ret != 0) { 1649 switch (errno) { 1650 1651 case ENOSPC: 1652 /* 1653 * For quotas and reservations, ENOSPC indicates 1654 * something different; setting a quota or reservation 1655 * doesn't use any disk space. 1656 */ 1657 switch (prop) { 1658 case ZFS_PROP_QUOTA: 1659 case ZFS_PROP_REFQUOTA: 1660 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1661 "size is less than current used or " 1662 "reserved space")); 1663 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1664 break; 1665 1666 case ZFS_PROP_RESERVATION: 1667 case ZFS_PROP_REFRESERVATION: 1668 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1669 "size is greater than available space")); 1670 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1671 break; 1672 1673 default: 1674 (void) zfs_standard_error(hdl, errno, errbuf); 1675 break; 1676 } 1677 break; 1678 1679 case EBUSY: 1680 if (prop == ZFS_PROP_VOLBLOCKSIZE) 1681 (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 1682 else 1683 (void) zfs_standard_error(hdl, EBUSY, errbuf); 1684 break; 1685 1686 case EROFS: 1687 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 1688 break; 1689 1690 case ENOTSUP: 1691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1692 "pool must be upgraded to set this " 1693 "property or value")); 1694 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 1695 break; 1696 1697 case EOVERFLOW: 1698 /* 1699 * This platform can't address a volume this big. 1700 */ 1701 #ifdef _ILP32 1702 if (prop == ZFS_PROP_VOLSIZE) { 1703 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1704 break; 1705 } 1706 #endif 1707 /* FALLTHROUGH */ 1708 default: 1709 (void) zfs_standard_error(hdl, errno, errbuf); 1710 } 1711 } else { 1712 /* 1713 * Refresh the statistics so the new property value 1714 * is reflected. 1715 */ 1716 if ((ret = changelist_postfix(cl)) == 0) 1717 (void) get_stats(zhp); 1718 } 1719 1720 error: 1721 nvlist_free(nvl); 1722 zcmd_free_nvlists(&zc); 1723 if (cl) 1724 changelist_free(cl); 1725 return (ret); 1726 } 1727 1728 /* 1729 * Given a property, inherit the value from the parent dataset. 1730 */ 1731 int 1732 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1733 { 1734 zfs_cmd_t zc = { 0 }; 1735 int ret; 1736 prop_changelist_t *cl; 1737 libzfs_handle_t *hdl = zhp->zfs_hdl; 1738 char errbuf[1024]; 1739 zfs_prop_t prop; 1740 1741 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 1742 "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1743 1744 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 1745 /* 1746 * For user properties, the amount of work we have to do is very 1747 * small, so just do it here. 1748 */ 1749 if (!zfs_prop_user(propname)) { 1750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1751 "invalid property")); 1752 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1753 } 1754 1755 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1756 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1757 1758 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1759 return (zfs_standard_error(hdl, errno, errbuf)); 1760 1761 return (0); 1762 } 1763 1764 /* 1765 * Verify that this property is inheritable. 1766 */ 1767 if (zfs_prop_readonly(prop)) 1768 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1769 1770 if (!zfs_prop_inheritable(prop)) 1771 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1772 1773 /* 1774 * Check to see if the value applies to this type 1775 */ 1776 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1777 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1778 1779 /* 1780 * Normalize the name, to get rid of shorthand abbrevations. 1781 */ 1782 propname = zfs_prop_to_name(prop); 1783 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1784 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1785 1786 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1787 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 1788 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1789 "dataset is used in a non-global zone")); 1790 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1791 } 1792 1793 /* 1794 * Determine datasets which will be affected by this change, if any. 1795 */ 1796 if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1797 return (-1); 1798 1799 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 1800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1801 "child dataset with inherited mountpoint is used " 1802 "in a non-global zone")); 1803 ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1804 goto error; 1805 } 1806 1807 if ((ret = changelist_prefix(cl)) != 0) 1808 goto error; 1809 1810 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 1811 return (zfs_standard_error(hdl, errno, errbuf)); 1812 } else { 1813 1814 if ((ret = changelist_postfix(cl)) != 0) 1815 goto error; 1816 1817 /* 1818 * Refresh the statistics so the new property is reflected. 1819 */ 1820 (void) get_stats(zhp); 1821 } 1822 1823 error: 1824 changelist_free(cl); 1825 return (ret); 1826 } 1827 1828 /* 1829 * True DSL properties are stored in an nvlist. The following two functions 1830 * extract them appropriately. 1831 */ 1832 static uint64_t 1833 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 1834 { 1835 nvlist_t *nv; 1836 uint64_t value; 1837 1838 *source = NULL; 1839 if (nvlist_lookup_nvlist(zhp->zfs_props, 1840 zfs_prop_to_name(prop), &nv) == 0) { 1841 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1842 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 1843 } else { 1844 value = zfs_prop_default_numeric(prop); 1845 *source = ""; 1846 } 1847 1848 return (value); 1849 } 1850 1851 static char * 1852 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 1853 { 1854 nvlist_t *nv; 1855 char *value; 1856 1857 *source = NULL; 1858 if (nvlist_lookup_nvlist(zhp->zfs_props, 1859 zfs_prop_to_name(prop), &nv) == 0) { 1860 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1861 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 1862 } else { 1863 if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 1864 value = ""; 1865 *source = ""; 1866 } 1867 1868 return (value); 1869 } 1870 1871 /* 1872 * Internal function for getting a numeric property. Both zfs_prop_get() and 1873 * zfs_prop_get_int() are built using this interface. 1874 * 1875 * Certain properties can be overridden using 'mount -o'. In this case, scan 1876 * the contents of the /etc/mnttab entry, searching for the appropriate options. 1877 * If they differ from the on-disk values, report the current values and mark 1878 * the source "temporary". 1879 */ 1880 static int 1881 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 1882 char **source, uint64_t *val) 1883 { 1884 zfs_cmd_t zc = { 0 }; 1885 nvlist_t *zplprops = NULL; 1886 struct mnttab mnt; 1887 char *mntopt_on = NULL; 1888 char *mntopt_off = NULL; 1889 1890 *source = NULL; 1891 1892 switch (prop) { 1893 case ZFS_PROP_ATIME: 1894 mntopt_on = MNTOPT_ATIME; 1895 mntopt_off = MNTOPT_NOATIME; 1896 break; 1897 1898 case ZFS_PROP_DEVICES: 1899 mntopt_on = MNTOPT_DEVICES; 1900 mntopt_off = MNTOPT_NODEVICES; 1901 break; 1902 1903 case ZFS_PROP_EXEC: 1904 mntopt_on = MNTOPT_EXEC; 1905 mntopt_off = MNTOPT_NOEXEC; 1906 break; 1907 1908 case ZFS_PROP_READONLY: 1909 mntopt_on = MNTOPT_RO; 1910 mntopt_off = MNTOPT_RW; 1911 break; 1912 1913 case ZFS_PROP_SETUID: 1914 mntopt_on = MNTOPT_SETUID; 1915 mntopt_off = MNTOPT_NOSETUID; 1916 break; 1917 1918 case ZFS_PROP_XATTR: 1919 mntopt_on = MNTOPT_XATTR; 1920 mntopt_off = MNTOPT_NOXATTR; 1921 break; 1922 1923 case ZFS_PROP_NBMAND: 1924 mntopt_on = MNTOPT_NBMAND; 1925 mntopt_off = MNTOPT_NONBMAND; 1926 break; 1927 } 1928 1929 /* 1930 * Because looking up the mount options is potentially expensive 1931 * (iterating over all of /etc/mnttab), we defer its calculation until 1932 * we're looking up a property which requires its presence. 1933 */ 1934 if (!zhp->zfs_mntcheck && 1935 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 1936 struct mnttab entry, search = { 0 }; 1937 FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 1938 1939 search.mnt_special = (char *)zhp->zfs_name; 1940 search.mnt_fstype = MNTTYPE_ZFS; 1941 rewind(mnttab); 1942 1943 if (getmntany(mnttab, &entry, &search) == 0) { 1944 zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 1945 entry.mnt_mntopts); 1946 if (zhp->zfs_mntopts == NULL) 1947 return (-1); 1948 } 1949 1950 zhp->zfs_mntcheck = B_TRUE; 1951 } 1952 1953 if (zhp->zfs_mntopts == NULL) 1954 mnt.mnt_mntopts = ""; 1955 else 1956 mnt.mnt_mntopts = zhp->zfs_mntopts; 1957 1958 switch (prop) { 1959 case ZFS_PROP_ATIME: 1960 case ZFS_PROP_DEVICES: 1961 case ZFS_PROP_EXEC: 1962 case ZFS_PROP_READONLY: 1963 case ZFS_PROP_SETUID: 1964 case ZFS_PROP_XATTR: 1965 case ZFS_PROP_NBMAND: 1966 *val = getprop_uint64(zhp, prop, source); 1967 1968 if (hasmntopt(&mnt, mntopt_on) && !*val) { 1969 *val = B_TRUE; 1970 if (src) 1971 *src = ZPROP_SRC_TEMPORARY; 1972 } else if (hasmntopt(&mnt, mntopt_off) && *val) { 1973 *val = B_FALSE; 1974 if (src) 1975 *src = ZPROP_SRC_TEMPORARY; 1976 } 1977 break; 1978 1979 case ZFS_PROP_CANMOUNT: 1980 *val = getprop_uint64(zhp, prop, source); 1981 if (*val == 0) 1982 *source = zhp->zfs_name; 1983 else 1984 *source = ""; /* default */ 1985 break; 1986 1987 case ZFS_PROP_QUOTA: 1988 case ZFS_PROP_REFQUOTA: 1989 case ZFS_PROP_RESERVATION: 1990 case ZFS_PROP_REFRESERVATION: 1991 *val = getprop_uint64(zhp, prop, source); 1992 if (*val == 0) 1993 *source = ""; /* default */ 1994 else 1995 *source = zhp->zfs_name; 1996 break; 1997 1998 case ZFS_PROP_MOUNTED: 1999 *val = (zhp->zfs_mntopts != NULL); 2000 break; 2001 2002 case ZFS_PROP_NUMCLONES: 2003 *val = zhp->zfs_dmustats.dds_num_clones; 2004 break; 2005 2006 case ZFS_PROP_VERSION: 2007 case ZFS_PROP_NORMALIZE: 2008 case ZFS_PROP_UTF8ONLY: 2009 case ZFS_PROP_CASE: 2010 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 2011 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 2012 return (-1); 2013 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2014 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 2015 zcmd_free_nvlists(&zc); 2016 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2017 "unable to get %s property"), 2018 zfs_prop_to_name(prop)); 2019 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 2020 dgettext(TEXT_DOMAIN, "internal error"))); 2021 } 2022 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 2023 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 2024 val) != 0) { 2025 zcmd_free_nvlists(&zc); 2026 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2027 "unable to get %s property"), 2028 zfs_prop_to_name(prop)); 2029 return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM, 2030 dgettext(TEXT_DOMAIN, "internal error"))); 2031 } 2032 if (zplprops) 2033 nvlist_free(zplprops); 2034 zcmd_free_nvlists(&zc); 2035 break; 2036 2037 default: 2038 switch (zfs_prop_get_type(prop)) { 2039 case PROP_TYPE_NUMBER: 2040 case PROP_TYPE_INDEX: 2041 *val = getprop_uint64(zhp, prop, source); 2042 break; 2043 2044 case PROP_TYPE_STRING: 2045 default: 2046 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2047 "cannot get non-numeric property")); 2048 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 2049 dgettext(TEXT_DOMAIN, "internal error"))); 2050 } 2051 } 2052 2053 return (0); 2054 } 2055 2056 /* 2057 * Calculate the source type, given the raw source string. 2058 */ 2059 static void 2060 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2061 char *statbuf, size_t statlen) 2062 { 2063 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2064 return; 2065 2066 if (source == NULL) { 2067 *srctype = ZPROP_SRC_NONE; 2068 } else if (source[0] == '\0') { 2069 *srctype = ZPROP_SRC_DEFAULT; 2070 } else { 2071 if (strcmp(source, zhp->zfs_name) == 0) { 2072 *srctype = ZPROP_SRC_LOCAL; 2073 } else { 2074 (void) strlcpy(statbuf, source, statlen); 2075 *srctype = ZPROP_SRC_INHERITED; 2076 } 2077 } 2078 2079 } 2080 2081 /* 2082 * Retrieve a property from the given object. If 'literal' is specified, then 2083 * numbers are left as exact values. Otherwise, numbers are converted to a 2084 * human-readable form. 2085 * 2086 * Returns 0 on success, or -1 on error. 2087 */ 2088 int 2089 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2090 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2091 { 2092 char *source = NULL; 2093 uint64_t val; 2094 char *str; 2095 const char *root; 2096 const char *strval; 2097 2098 /* 2099 * Check to see if this property applies to our object 2100 */ 2101 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2102 return (-1); 2103 2104 if (src) 2105 *src = ZPROP_SRC_NONE; 2106 2107 switch (prop) { 2108 case ZFS_PROP_CREATION: 2109 /* 2110 * 'creation' is a time_t stored in the statistics. We convert 2111 * this into a string unless 'literal' is specified. 2112 */ 2113 { 2114 val = getprop_uint64(zhp, prop, &source); 2115 time_t time = (time_t)val; 2116 struct tm t; 2117 2118 if (literal || 2119 localtime_r(&time, &t) == NULL || 2120 strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2121 &t) == 0) 2122 (void) snprintf(propbuf, proplen, "%llu", val); 2123 } 2124 break; 2125 2126 case ZFS_PROP_MOUNTPOINT: 2127 /* 2128 * Getting the precise mountpoint can be tricky. 2129 * 2130 * - for 'none' or 'legacy', return those values. 2131 * - for default mountpoints, construct it as /zfs/<dataset> 2132 * - for inherited mountpoints, we want to take everything 2133 * after our ancestor and append it to the inherited value. 2134 * 2135 * If the pool has an alternate root, we want to prepend that 2136 * root to any values we return. 2137 */ 2138 root = zhp->zfs_root; 2139 str = getprop_string(zhp, prop, &source); 2140 2141 if (str[0] == '\0') { 2142 (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2143 root, zhp->zfs_name); 2144 } else if (str[0] == '/') { 2145 const char *relpath = zhp->zfs_name + strlen(source); 2146 2147 if (relpath[0] == '/') 2148 relpath++; 2149 if (str[1] == '\0') 2150 str++; 2151 2152 if (relpath[0] == '\0') 2153 (void) snprintf(propbuf, proplen, "%s%s", 2154 root, str); 2155 else 2156 (void) snprintf(propbuf, proplen, "%s%s%s%s", 2157 root, str, relpath[0] == '@' ? "" : "/", 2158 relpath); 2159 } else { 2160 /* 'legacy' or 'none' */ 2161 (void) strlcpy(propbuf, str, proplen); 2162 } 2163 2164 break; 2165 2166 case ZFS_PROP_ORIGIN: 2167 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2168 proplen); 2169 /* 2170 * If there is no parent at all, return failure to indicate that 2171 * it doesn't apply to this dataset. 2172 */ 2173 if (propbuf[0] == '\0') 2174 return (-1); 2175 break; 2176 2177 case ZFS_PROP_QUOTA: 2178 case ZFS_PROP_REFQUOTA: 2179 case ZFS_PROP_RESERVATION: 2180 case ZFS_PROP_REFRESERVATION: 2181 2182 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2183 return (-1); 2184 2185 /* 2186 * If quota or reservation is 0, we translate this into 'none' 2187 * (unless literal is set), and indicate that it's the default 2188 * value. Otherwise, we print the number nicely and indicate 2189 * that its set locally. 2190 */ 2191 if (val == 0) { 2192 if (literal) 2193 (void) strlcpy(propbuf, "0", proplen); 2194 else 2195 (void) strlcpy(propbuf, "none", proplen); 2196 } else { 2197 if (literal) 2198 (void) snprintf(propbuf, proplen, "%llu", 2199 (u_longlong_t)val); 2200 else 2201 zfs_nicenum(val, propbuf, proplen); 2202 } 2203 break; 2204 2205 case ZFS_PROP_COMPRESSRATIO: 2206 if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2207 return (-1); 2208 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 2209 val / 100, (longlong_t)val % 100); 2210 break; 2211 2212 case ZFS_PROP_TYPE: 2213 switch (zhp->zfs_type) { 2214 case ZFS_TYPE_FILESYSTEM: 2215 str = "filesystem"; 2216 break; 2217 case ZFS_TYPE_VOLUME: 2218 str = "volume"; 2219 break; 2220 case ZFS_TYPE_SNAPSHOT: 2221 str = "snapshot"; 2222 break; 2223 default: 2224 abort(); 2225 } 2226 (void) snprintf(propbuf, proplen, "%s", str); 2227 break; 2228 2229 case ZFS_PROP_MOUNTED: 2230 /* 2231 * The 'mounted' property is a pseudo-property that described 2232 * whether the filesystem is currently mounted. Even though 2233 * it's a boolean value, the typical values of "on" and "off" 2234 * don't make sense, so we translate to "yes" and "no". 2235 */ 2236 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 2237 src, &source, &val) != 0) 2238 return (-1); 2239 if (val) 2240 (void) strlcpy(propbuf, "yes", proplen); 2241 else 2242 (void) strlcpy(propbuf, "no", proplen); 2243 break; 2244 2245 case ZFS_PROP_NAME: 2246 /* 2247 * The 'name' property is a pseudo-property derived from the 2248 * dataset name. It is presented as a real property to simplify 2249 * consumers. 2250 */ 2251 (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2252 break; 2253 2254 default: 2255 switch (zfs_prop_get_type(prop)) { 2256 case PROP_TYPE_NUMBER: 2257 if (get_numeric_property(zhp, prop, src, 2258 &source, &val) != 0) 2259 return (-1); 2260 if (literal) 2261 (void) snprintf(propbuf, proplen, "%llu", 2262 (u_longlong_t)val); 2263 else 2264 zfs_nicenum(val, propbuf, proplen); 2265 break; 2266 2267 case PROP_TYPE_STRING: 2268 (void) strlcpy(propbuf, 2269 getprop_string(zhp, prop, &source), proplen); 2270 break; 2271 2272 case PROP_TYPE_INDEX: 2273 if (get_numeric_property(zhp, prop, src, 2274 &source, &val) != 0) 2275 return (-1); 2276 if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2277 return (-1); 2278 (void) strlcpy(propbuf, strval, proplen); 2279 break; 2280 2281 default: 2282 abort(); 2283 } 2284 } 2285 2286 get_source(zhp, src, source, statbuf, statlen); 2287 2288 return (0); 2289 } 2290 2291 /* 2292 * Utility function to get the given numeric property. Does no validation that 2293 * the given property is the appropriate type; should only be used with 2294 * hard-coded property types. 2295 */ 2296 uint64_t 2297 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2298 { 2299 char *source; 2300 uint64_t val; 2301 2302 (void) get_numeric_property(zhp, prop, NULL, &source, &val); 2303 2304 return (val); 2305 } 2306 2307 int 2308 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 2309 { 2310 char buf[64]; 2311 2312 zfs_nicenum(val, buf, sizeof (buf)); 2313 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 2314 } 2315 2316 /* 2317 * Similar to zfs_prop_get(), but returns the value as an integer. 2318 */ 2319 int 2320 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2321 zprop_source_t *src, char *statbuf, size_t statlen) 2322 { 2323 char *source; 2324 2325 /* 2326 * Check to see if this property applies to our object 2327 */ 2328 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 2329 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 2330 dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 2331 zfs_prop_to_name(prop))); 2332 } 2333 2334 if (src) 2335 *src = ZPROP_SRC_NONE; 2336 2337 if (get_numeric_property(zhp, prop, src, &source, value) != 0) 2338 return (-1); 2339 2340 get_source(zhp, src, source, statbuf, statlen); 2341 2342 return (0); 2343 } 2344 2345 /* 2346 * Returns the name of the given zfs handle. 2347 */ 2348 const char * 2349 zfs_get_name(const zfs_handle_t *zhp) 2350 { 2351 return (zhp->zfs_name); 2352 } 2353 2354 /* 2355 * Returns the type of the given zfs handle. 2356 */ 2357 zfs_type_t 2358 zfs_get_type(const zfs_handle_t *zhp) 2359 { 2360 return (zhp->zfs_type); 2361 } 2362 2363 /* 2364 * Iterate over all child filesystems 2365 */ 2366 int 2367 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2368 { 2369 zfs_cmd_t zc = { 0 }; 2370 zfs_handle_t *nzhp; 2371 int ret; 2372 2373 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 2374 return (0); 2375 2376 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2377 ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2378 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2379 /* 2380 * Ignore private dataset names. 2381 */ 2382 if (dataset_name_hidden(zc.zc_name)) 2383 continue; 2384 2385 /* 2386 * Silently ignore errors, as the only plausible explanation is 2387 * that the pool has since been removed. 2388 */ 2389 if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 2390 zc.zc_name)) == NULL) 2391 continue; 2392 2393 if ((ret = func(nzhp, data)) != 0) 2394 return (ret); 2395 } 2396 2397 /* 2398 * An errno value of ESRCH indicates normal completion. If ENOENT is 2399 * returned, then the underlying dataset has been removed since we 2400 * obtained the handle. 2401 */ 2402 if (errno != ESRCH && errno != ENOENT) 2403 return (zfs_standard_error(zhp->zfs_hdl, errno, 2404 dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2405 2406 return (0); 2407 } 2408 2409 /* 2410 * Iterate over all snapshots 2411 */ 2412 int 2413 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2414 { 2415 zfs_cmd_t zc = { 0 }; 2416 zfs_handle_t *nzhp; 2417 int ret; 2418 2419 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 2420 return (0); 2421 2422 for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2423 ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 2424 &zc) == 0; 2425 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2426 2427 if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 2428 zc.zc_name)) == NULL) 2429 continue; 2430 2431 if ((ret = func(nzhp, data)) != 0) 2432 return (ret); 2433 } 2434 2435 /* 2436 * An errno value of ESRCH indicates normal completion. If ENOENT is 2437 * returned, then the underlying dataset has been removed since we 2438 * obtained the handle. Silently ignore this case, and return success. 2439 */ 2440 if (errno != ESRCH && errno != ENOENT) 2441 return (zfs_standard_error(zhp->zfs_hdl, errno, 2442 dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2443 2444 return (0); 2445 } 2446 2447 /* 2448 * Iterate over all children, snapshots and filesystems 2449 */ 2450 int 2451 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2452 { 2453 int ret; 2454 2455 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 2456 return (ret); 2457 2458 return (zfs_iter_snapshots(zhp, func, data)); 2459 } 2460 2461 /* 2462 * Given a complete name, return just the portion that refers to the parent. 2463 * Can return NULL if this is a pool. 2464 */ 2465 static int 2466 parent_name(const char *path, char *buf, size_t buflen) 2467 { 2468 char *loc; 2469 2470 if ((loc = strrchr(path, '/')) == NULL) 2471 return (-1); 2472 2473 (void) strncpy(buf, path, MIN(buflen, loc - path)); 2474 buf[loc - path] = '\0'; 2475 2476 return (0); 2477 } 2478 2479 /* 2480 * If accept_ancestor is false, then check to make sure that the given path has 2481 * a parent, and that it exists. If accept_ancestor is true, then find the 2482 * closest existing ancestor for the given path. In prefixlen return the 2483 * length of already existing prefix of the given path. We also fetch the 2484 * 'zoned' property, which is used to validate property settings when creating 2485 * new datasets. 2486 */ 2487 static int 2488 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 2489 boolean_t accept_ancestor, int *prefixlen) 2490 { 2491 zfs_cmd_t zc = { 0 }; 2492 char parent[ZFS_MAXNAMELEN]; 2493 char *slash; 2494 zfs_handle_t *zhp; 2495 char errbuf[1024]; 2496 2497 (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 2498 path); 2499 2500 /* get parent, and check to see if this is just a pool */ 2501 if (parent_name(path, parent, sizeof (parent)) != 0) { 2502 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2503 "missing dataset name")); 2504 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2505 } 2506 2507 /* check to see if the pool exists */ 2508 if ((slash = strchr(parent, '/')) == NULL) 2509 slash = parent + strlen(parent); 2510 (void) strncpy(zc.zc_name, parent, slash - parent); 2511 zc.zc_name[slash - parent] = '\0'; 2512 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2513 errno == ENOENT) { 2514 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2515 "no such pool '%s'"), zc.zc_name); 2516 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2517 } 2518 2519 /* check to see if the parent dataset exists */ 2520 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 2521 if (errno == ENOENT && accept_ancestor) { 2522 /* 2523 * Go deeper to find an ancestor, give up on top level. 2524 */ 2525 if (parent_name(parent, parent, sizeof (parent)) != 0) { 2526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2527 "no such pool '%s'"), zc.zc_name); 2528 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2529 } 2530 } else if (errno == ENOENT) { 2531 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2532 "parent does not exist")); 2533 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2534 } else 2535 return (zfs_standard_error(hdl, errno, errbuf)); 2536 } 2537 2538 *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2539 /* we are in a non-global zone, but parent is in the global zone */ 2540 if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 2541 (void) zfs_standard_error(hdl, EPERM, errbuf); 2542 zfs_close(zhp); 2543 return (-1); 2544 } 2545 2546 /* make sure parent is a filesystem */ 2547 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 2548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2549 "parent is not a filesystem")); 2550 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 2551 zfs_close(zhp); 2552 return (-1); 2553 } 2554 2555 zfs_close(zhp); 2556 if (prefixlen != NULL) 2557 *prefixlen = strlen(parent); 2558 return (0); 2559 } 2560 2561 /* 2562 * Finds whether the dataset of the given type(s) exists. 2563 */ 2564 boolean_t 2565 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 2566 { 2567 zfs_handle_t *zhp; 2568 2569 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 2570 return (B_FALSE); 2571 2572 /* 2573 * Try to get stats for the dataset, which will tell us if it exists. 2574 */ 2575 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 2576 int ds_type = zhp->zfs_type; 2577 2578 zfs_close(zhp); 2579 if (types & ds_type) 2580 return (B_TRUE); 2581 } 2582 return (B_FALSE); 2583 } 2584 2585 /* 2586 * Given a path to 'target', create all the ancestors between 2587 * the prefixlen portion of the path, and the target itself. 2588 * Fail if the initial prefixlen-ancestor does not already exist. 2589 */ 2590 int 2591 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 2592 { 2593 zfs_handle_t *h; 2594 char *cp; 2595 const char *opname; 2596 2597 /* make sure prefix exists */ 2598 cp = target + prefixlen; 2599 if (*cp != '/') { 2600 assert(strchr(cp, '/') == NULL); 2601 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2602 } else { 2603 *cp = '\0'; 2604 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2605 *cp = '/'; 2606 } 2607 if (h == NULL) 2608 return (-1); 2609 zfs_close(h); 2610 2611 /* 2612 * Attempt to create, mount, and share any ancestor filesystems, 2613 * up to the prefixlen-long one. 2614 */ 2615 for (cp = target + prefixlen + 1; 2616 cp = strchr(cp, '/'); *cp = '/', cp++) { 2617 char *logstr; 2618 2619 *cp = '\0'; 2620 2621 h = make_dataset_handle(hdl, target); 2622 if (h) { 2623 /* it already exists, nothing to do here */ 2624 zfs_close(h); 2625 continue; 2626 } 2627 2628 logstr = hdl->libzfs_log_str; 2629 hdl->libzfs_log_str = NULL; 2630 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 2631 NULL) != 0) { 2632 hdl->libzfs_log_str = logstr; 2633 opname = dgettext(TEXT_DOMAIN, "create"); 2634 goto ancestorerr; 2635 } 2636 2637 hdl->libzfs_log_str = logstr; 2638 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2639 if (h == NULL) { 2640 opname = dgettext(TEXT_DOMAIN, "open"); 2641 goto ancestorerr; 2642 } 2643 2644 if (zfs_mount(h, NULL, 0) != 0) { 2645 opname = dgettext(TEXT_DOMAIN, "mount"); 2646 goto ancestorerr; 2647 } 2648 2649 if (zfs_share(h) != 0) { 2650 opname = dgettext(TEXT_DOMAIN, "share"); 2651 goto ancestorerr; 2652 } 2653 2654 zfs_close(h); 2655 } 2656 2657 return (0); 2658 2659 ancestorerr: 2660 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2661 "failed to %s ancestor '%s'"), opname, target); 2662 return (-1); 2663 } 2664 2665 /* 2666 * Creates non-existing ancestors of the given path. 2667 */ 2668 int 2669 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 2670 { 2671 int prefix; 2672 uint64_t zoned; 2673 char *path_copy; 2674 int rc; 2675 2676 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 2677 return (-1); 2678 2679 if ((path_copy = strdup(path)) != NULL) { 2680 rc = create_parents(hdl, path_copy, prefix); 2681 free(path_copy); 2682 } 2683 if (path_copy == NULL || rc != 0) 2684 return (-1); 2685 2686 return (0); 2687 } 2688 2689 /* 2690 * Create a new filesystem or volume. 2691 */ 2692 int 2693 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 2694 nvlist_t *props) 2695 { 2696 zfs_cmd_t zc = { 0 }; 2697 int ret; 2698 uint64_t size = 0; 2699 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 2700 char errbuf[1024]; 2701 uint64_t zoned; 2702 2703 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2704 "cannot create '%s'"), path); 2705 2706 /* validate the path, taking care to note the extended error message */ 2707 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 2708 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2709 2710 /* validate parents exist */ 2711 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2712 return (-1); 2713 2714 /* 2715 * The failure modes when creating a dataset of a different type over 2716 * one that already exists is a little strange. In particular, if you 2717 * try to create a dataset on top of an existing dataset, the ioctl() 2718 * will return ENOENT, not EEXIST. To prevent this from happening, we 2719 * first try to see if the dataset exists. 2720 */ 2721 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 2722 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 2723 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2724 "dataset already exists")); 2725 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2726 } 2727 2728 if (type == ZFS_TYPE_VOLUME) 2729 zc.zc_objset_type = DMU_OST_ZVOL; 2730 else 2731 zc.zc_objset_type = DMU_OST_ZFS; 2732 2733 if (props && (props = zfs_validate_properties(hdl, type, props, 2734 zoned, NULL, errbuf)) == 0) 2735 return (-1); 2736 2737 if (type == ZFS_TYPE_VOLUME) { 2738 /* 2739 * If we are creating a volume, the size and block size must 2740 * satisfy a few restraints. First, the blocksize must be a 2741 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 2742 * volsize must be a multiple of the block size, and cannot be 2743 * zero. 2744 */ 2745 if (props == NULL || nvlist_lookup_uint64(props, 2746 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 2747 nvlist_free(props); 2748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2749 "missing volume size")); 2750 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2751 } 2752 2753 if ((ret = nvlist_lookup_uint64(props, 2754 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 2755 &blocksize)) != 0) { 2756 if (ret == ENOENT) { 2757 blocksize = zfs_prop_default_numeric( 2758 ZFS_PROP_VOLBLOCKSIZE); 2759 } else { 2760 nvlist_free(props); 2761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2762 "missing volume block size")); 2763 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2764 } 2765 } 2766 2767 if (size == 0) { 2768 nvlist_free(props); 2769 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2770 "volume size cannot be zero")); 2771 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2772 } 2773 2774 if (size % blocksize != 0) { 2775 nvlist_free(props); 2776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2777 "volume size must be a multiple of volume block " 2778 "size")); 2779 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2780 } 2781 } 2782 2783 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 2784 return (-1); 2785 nvlist_free(props); 2786 2787 /* create the dataset */ 2788 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2789 2790 if (ret == 0 && type == ZFS_TYPE_VOLUME) { 2791 ret = zvol_create_link(hdl, path); 2792 if (ret) { 2793 (void) zfs_standard_error(hdl, errno, 2794 dgettext(TEXT_DOMAIN, 2795 "Volume successfully created, but device links " 2796 "were not created")); 2797 zcmd_free_nvlists(&zc); 2798 return (-1); 2799 } 2800 } 2801 2802 zcmd_free_nvlists(&zc); 2803 2804 /* check for failure */ 2805 if (ret != 0) { 2806 char parent[ZFS_MAXNAMELEN]; 2807 (void) parent_name(path, parent, sizeof (parent)); 2808 2809 switch (errno) { 2810 case ENOENT: 2811 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2812 "no such parent '%s'"), parent); 2813 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2814 2815 case EINVAL: 2816 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2817 "parent '%s' is not a filesystem"), parent); 2818 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2819 2820 case EDOM: 2821 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2822 "volume block size must be power of 2 from " 2823 "%u to %uk"), 2824 (uint_t)SPA_MINBLOCKSIZE, 2825 (uint_t)SPA_MAXBLOCKSIZE >> 10); 2826 2827 return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2828 2829 case ENOTSUP: 2830 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2831 "pool must be upgraded to set this " 2832 "property or value")); 2833 return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 2834 2835 #ifdef _ILP32 2836 case EOVERFLOW: 2837 /* 2838 * This platform can't address a volume this big. 2839 */ 2840 if (type == ZFS_TYPE_VOLUME) 2841 return (zfs_error(hdl, EZFS_VOLTOOBIG, 2842 errbuf)); 2843 #endif 2844 /* FALLTHROUGH */ 2845 default: 2846 return (zfs_standard_error(hdl, errno, errbuf)); 2847 } 2848 } 2849 2850 return (0); 2851 } 2852 2853 /* 2854 * Destroys the given dataset. The caller must make sure that the filesystem 2855 * isn't mounted, and that there are no active dependents. 2856 */ 2857 int 2858 zfs_destroy(zfs_handle_t *zhp) 2859 { 2860 zfs_cmd_t zc = { 0 }; 2861 2862 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2863 2864 if (ZFS_IS_VOLUME(zhp)) { 2865 /* 2866 * If user doesn't have permissions to unshare volume, then 2867 * abort the request. This would only happen for a 2868 * non-privileged user. 2869 */ 2870 if (zfs_unshare_iscsi(zhp) != 0) { 2871 return (-1); 2872 } 2873 2874 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2875 return (-1); 2876 2877 zc.zc_objset_type = DMU_OST_ZVOL; 2878 } else { 2879 zc.zc_objset_type = DMU_OST_ZFS; 2880 } 2881 2882 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 2883 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 2884 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 2885 zhp->zfs_name)); 2886 } 2887 2888 remove_mountpoint(zhp); 2889 2890 return (0); 2891 } 2892 2893 struct destroydata { 2894 char *snapname; 2895 boolean_t gotone; 2896 boolean_t closezhp; 2897 }; 2898 2899 static int 2900 zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 2901 { 2902 struct destroydata *dd = arg; 2903 zfs_handle_t *szhp; 2904 char name[ZFS_MAXNAMELEN]; 2905 boolean_t closezhp = dd->closezhp; 2906 int rv; 2907 2908 (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 2909 (void) strlcat(name, "@", sizeof (name)); 2910 (void) strlcat(name, dd->snapname, sizeof (name)); 2911 2912 szhp = make_dataset_handle(zhp->zfs_hdl, name); 2913 if (szhp) { 2914 dd->gotone = B_TRUE; 2915 zfs_close(szhp); 2916 } 2917 2918 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 2919 (void) zvol_remove_link(zhp->zfs_hdl, name); 2920 /* 2921 * NB: this is simply a best-effort. We don't want to 2922 * return an error, because then we wouldn't visit all 2923 * the volumes. 2924 */ 2925 } 2926 2927 dd->closezhp = B_TRUE; 2928 rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 2929 if (closezhp) 2930 zfs_close(zhp); 2931 return (rv); 2932 } 2933 2934 /* 2935 * Destroys all snapshots with the given name in zhp & descendants. 2936 */ 2937 int 2938 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 2939 { 2940 zfs_cmd_t zc = { 0 }; 2941 int ret; 2942 struct destroydata dd = { 0 }; 2943 2944 dd.snapname = snapname; 2945 (void) zfs_remove_link_cb(zhp, &dd); 2946 2947 if (!dd.gotone) { 2948 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 2949 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 2950 zhp->zfs_name, snapname)); 2951 } 2952 2953 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2954 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 2955 2956 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 2957 if (ret != 0) { 2958 char errbuf[1024]; 2959 2960 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2961 "cannot destroy '%s@%s'"), zc.zc_name, snapname); 2962 2963 switch (errno) { 2964 case EEXIST: 2965 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2966 "snapshot is cloned")); 2967 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 2968 2969 default: 2970 return (zfs_standard_error(zhp->zfs_hdl, errno, 2971 errbuf)); 2972 } 2973 } 2974 2975 return (0); 2976 } 2977 2978 /* 2979 * Clones the given dataset. The target must be of the same type as the source. 2980 */ 2981 int 2982 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2983 { 2984 zfs_cmd_t zc = { 0 }; 2985 char parent[ZFS_MAXNAMELEN]; 2986 int ret; 2987 char errbuf[1024]; 2988 libzfs_handle_t *hdl = zhp->zfs_hdl; 2989 zfs_type_t type; 2990 uint64_t zoned; 2991 2992 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2993 2994 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 2995 "cannot create '%s'"), target); 2996 2997 /* validate the target name */ 2998 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 2999 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3000 3001 /* validate parents exist */ 3002 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3003 return (-1); 3004 3005 (void) parent_name(target, parent, sizeof (parent)); 3006 3007 /* do the clone */ 3008 if (ZFS_IS_VOLUME(zhp)) { 3009 zc.zc_objset_type = DMU_OST_ZVOL; 3010 type = ZFS_TYPE_VOLUME; 3011 } else { 3012 zc.zc_objset_type = DMU_OST_ZFS; 3013 type = ZFS_TYPE_FILESYSTEM; 3014 } 3015 3016 if (props) { 3017 if ((props = zfs_validate_properties(hdl, type, props, 3018 zoned, zhp, errbuf)) == NULL) 3019 return (-1); 3020 3021 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 3022 nvlist_free(props); 3023 return (-1); 3024 } 3025 3026 nvlist_free(props); 3027 } 3028 3029 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 3030 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 3031 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3032 3033 zcmd_free_nvlists(&zc); 3034 3035 if (ret != 0) { 3036 switch (errno) { 3037 3038 case ENOENT: 3039 /* 3040 * The parent doesn't exist. We should have caught this 3041 * above, but there may a race condition that has since 3042 * destroyed the parent. 3043 * 3044 * At this point, we don't know whether it's the source 3045 * that doesn't exist anymore, or whether the target 3046 * dataset doesn't exist. 3047 */ 3048 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3049 "no such parent '%s'"), parent); 3050 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3051 3052 case EXDEV: 3053 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3054 "source and target pools differ")); 3055 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 3056 errbuf)); 3057 3058 default: 3059 return (zfs_standard_error(zhp->zfs_hdl, errno, 3060 errbuf)); 3061 } 3062 } else if (ZFS_IS_VOLUME(zhp)) { 3063 ret = zvol_create_link(zhp->zfs_hdl, target); 3064 } 3065 3066 return (ret); 3067 } 3068 3069 typedef struct promote_data { 3070 char cb_mountpoint[MAXPATHLEN]; 3071 const char *cb_target; 3072 const char *cb_errbuf; 3073 uint64_t cb_pivot_txg; 3074 } promote_data_t; 3075 3076 static int 3077 promote_snap_cb(zfs_handle_t *zhp, void *data) 3078 { 3079 promote_data_t *pd = data; 3080 zfs_handle_t *szhp; 3081 char snapname[MAXPATHLEN]; 3082 int rv = 0; 3083 3084 /* We don't care about snapshots after the pivot point */ 3085 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 3086 zfs_close(zhp); 3087 return (0); 3088 } 3089 3090 /* Remove the device link if it's a zvol. */ 3091 if (ZFS_IS_VOLUME(zhp)) 3092 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 3093 3094 /* Check for conflicting names */ 3095 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 3096 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 3097 szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 3098 if (szhp != NULL) { 3099 zfs_close(szhp); 3100 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 3101 "snapshot name '%s' from origin \n" 3102 "conflicts with '%s' from target"), 3103 zhp->zfs_name, snapname); 3104 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 3105 } 3106 zfs_close(zhp); 3107 return (rv); 3108 } 3109 3110 static int 3111 promote_snap_done_cb(zfs_handle_t *zhp, void *data) 3112 { 3113 promote_data_t *pd = data; 3114 3115 /* We don't care about snapshots after the pivot point */ 3116 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 3117 /* Create the device link if it's a zvol. */ 3118 if (ZFS_IS_VOLUME(zhp)) 3119 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3120 } 3121 3122 zfs_close(zhp); 3123 return (0); 3124 } 3125 3126 /* 3127 * Promotes the given clone fs to be the clone parent. 3128 */ 3129 int 3130 zfs_promote(zfs_handle_t *zhp) 3131 { 3132 libzfs_handle_t *hdl = zhp->zfs_hdl; 3133 zfs_cmd_t zc = { 0 }; 3134 char parent[MAXPATHLEN]; 3135 char *cp; 3136 int ret; 3137 zfs_handle_t *pzhp; 3138 promote_data_t pd; 3139 char errbuf[1024]; 3140 3141 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3142 "cannot promote '%s'"), zhp->zfs_name); 3143 3144 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3146 "snapshots can not be promoted")); 3147 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3148 } 3149 3150 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 3151 if (parent[0] == '\0') { 3152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3153 "not a cloned filesystem")); 3154 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3155 } 3156 cp = strchr(parent, '@'); 3157 *cp = '\0'; 3158 3159 /* Walk the snapshots we will be moving */ 3160 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 3161 if (pzhp == NULL) 3162 return (-1); 3163 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 3164 zfs_close(pzhp); 3165 pd.cb_target = zhp->zfs_name; 3166 pd.cb_errbuf = errbuf; 3167 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 3168 if (pzhp == NULL) 3169 return (-1); 3170 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 3171 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 3172 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 3173 if (ret != 0) { 3174 zfs_close(pzhp); 3175 return (-1); 3176 } 3177 3178 /* issue the ioctl */ 3179 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 3180 sizeof (zc.zc_value)); 3181 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3182 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 3183 3184 if (ret != 0) { 3185 int save_errno = errno; 3186 3187 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 3188 zfs_close(pzhp); 3189 3190 switch (save_errno) { 3191 case EEXIST: 3192 /* 3193 * There is a conflicting snapshot name. We 3194 * should have caught this above, but they could 3195 * have renamed something in the mean time. 3196 */ 3197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3198 "conflicting snapshot name from parent '%s'"), 3199 parent); 3200 return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3201 3202 default: 3203 return (zfs_standard_error(hdl, save_errno, errbuf)); 3204 } 3205 } else { 3206 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3207 } 3208 3209 zfs_close(pzhp); 3210 return (ret); 3211 } 3212 3213 struct createdata { 3214 const char *cd_snapname; 3215 int cd_ifexists; 3216 }; 3217 3218 static int 3219 zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 3220 { 3221 struct createdata *cd = arg; 3222 int ret; 3223 3224 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3225 char name[MAXPATHLEN]; 3226 3227 (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 3228 (void) strlcat(name, "@", sizeof (name)); 3229 (void) strlcat(name, cd->cd_snapname, sizeof (name)); 3230 (void) zvol_create_link_common(zhp->zfs_hdl, name, 3231 cd->cd_ifexists); 3232 /* 3233 * NB: this is simply a best-effort. We don't want to 3234 * return an error, because then we wouldn't visit all 3235 * the volumes. 3236 */ 3237 } 3238 3239 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 3240 3241 zfs_close(zhp); 3242 3243 return (ret); 3244 } 3245 3246 /* 3247 * Takes a snapshot of the given dataset. 3248 */ 3249 int 3250 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3251 { 3252 const char *delim; 3253 char *parent; 3254 zfs_handle_t *zhp; 3255 zfs_cmd_t zc = { 0 }; 3256 int ret; 3257 char errbuf[1024]; 3258 3259 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3260 "cannot snapshot '%s'"), path); 3261 3262 /* validate the target name */ 3263 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 3264 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3265 3266 /* make sure the parent exists and is of the appropriate type */ 3267 delim = strchr(path, '@'); 3268 if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 3269 return (-1); 3270 (void) strncpy(parent, path, delim - path); 3271 parent[delim - path] = '\0'; 3272 3273 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3274 ZFS_TYPE_VOLUME)) == NULL) { 3275 free(parent); 3276 return (-1); 3277 } 3278 3279 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3280 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 3281 if (ZFS_IS_VOLUME(zhp)) 3282 zc.zc_objset_type = DMU_OST_ZVOL; 3283 else 3284 zc.zc_objset_type = DMU_OST_ZFS; 3285 zc.zc_cookie = recursive; 3286 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 3287 3288 /* 3289 * if it was recursive, the one that actually failed will be in 3290 * zc.zc_name. 3291 */ 3292 if (ret != 0) 3293 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3294 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 3295 3296 if (ret == 0 && recursive) { 3297 struct createdata cd; 3298 3299 cd.cd_snapname = delim + 1; 3300 cd.cd_ifexists = B_FALSE; 3301 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 3302 } 3303 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 3304 ret = zvol_create_link(zhp->zfs_hdl, path); 3305 if (ret != 0) { 3306 (void) zfs_standard_error(hdl, errno, 3307 dgettext(TEXT_DOMAIN, 3308 "Volume successfully snapshotted, but device links " 3309 "were not created")); 3310 free(parent); 3311 zfs_close(zhp); 3312 return (-1); 3313 } 3314 } 3315 3316 if (ret != 0) 3317 (void) zfs_standard_error(hdl, errno, errbuf); 3318 3319 free(parent); 3320 zfs_close(zhp); 3321 3322 return (ret); 3323 } 3324 3325 /* 3326 * Destroy any more recent snapshots. We invoke this callback on any dependents 3327 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3328 * is a dependent and we should just destroy it without checking the transaction 3329 * group. 3330 */ 3331 typedef struct rollback_data { 3332 const char *cb_target; /* the snapshot */ 3333 uint64_t cb_create; /* creation time reference */ 3334 int cb_error; 3335 boolean_t cb_dependent; 3336 } rollback_data_t; 3337 3338 static int 3339 rollback_destroy(zfs_handle_t *zhp, void *data) 3340 { 3341 rollback_data_t *cbp = data; 3342 3343 if (!cbp->cb_dependent) { 3344 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 3345 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 3346 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 3347 cbp->cb_create) { 3348 char *logstr; 3349 3350 cbp->cb_dependent = B_TRUE; 3351 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 3352 rollback_destroy, cbp); 3353 cbp->cb_dependent = B_FALSE; 3354 3355 logstr = zhp->zfs_hdl->libzfs_log_str; 3356 zhp->zfs_hdl->libzfs_log_str = NULL; 3357 cbp->cb_error |= zfs_destroy(zhp); 3358 zhp->zfs_hdl->libzfs_log_str = logstr; 3359 } 3360 } else { 3361 cbp->cb_error |= zfs_destroy(zhp); 3362 } 3363 3364 zfs_close(zhp); 3365 return (0); 3366 } 3367 3368 /* 3369 * Given a dataset, rollback to a specific snapshot, discarding any 3370 * data changes since then and making it the active dataset. 3371 * 3372 * Any snapshots more recent than the target are destroyed, along with 3373 * their dependents. 3374 */ 3375 int 3376 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap) 3377 { 3378 rollback_data_t cb = { 0 }; 3379 int err; 3380 zfs_cmd_t zc = { 0 }; 3381 boolean_t restore_resv = 0; 3382 uint64_t old_volsize, new_volsize; 3383 zfs_prop_t resv_prop; 3384 3385 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3386 zhp->zfs_type == ZFS_TYPE_VOLUME); 3387 3388 /* 3389 * Destroy all recent snapshots and its dependends. 3390 */ 3391 cb.cb_target = snap->zfs_name; 3392 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 3393 (void) zfs_iter_children(zhp, rollback_destroy, &cb); 3394 3395 if (cb.cb_error != 0) 3396 return (cb.cb_error); 3397 3398 /* 3399 * Now that we have verified that the snapshot is the latest, 3400 * rollback to the given snapshot. 3401 */ 3402 3403 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3404 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3405 return (-1); 3406 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 3407 return (-1); 3408 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 3409 restore_resv = 3410 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 3411 } 3412 3413 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3414 3415 if (ZFS_IS_VOLUME(zhp)) 3416 zc.zc_objset_type = DMU_OST_ZVOL; 3417 else 3418 zc.zc_objset_type = DMU_OST_ZFS; 3419 3420 /* 3421 * We rely on zfs_iter_children() to verify that there are no 3422 * newer snapshots for the given dataset. Therefore, we can 3423 * simply pass the name on to the ioctl() call. There is still 3424 * an unlikely race condition where the user has taken a 3425 * snapshot since we verified that this was the most recent. 3426 * 3427 */ 3428 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 3429 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 3430 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 3431 zhp->zfs_name); 3432 } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3433 3434 /* 3435 * For volumes, if the pre-rollback volsize matched the pre- 3436 * rollback reservation and the volsize has changed then set 3437 * the reservation property to the post-rollback volsize. 3438 * Make a new handle since the rollback closed the dataset. 3439 */ 3440 zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name); 3441 if (!zhp) 3442 return (err); 3443 3444 if (restore_resv) { 3445 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 3446 if (old_volsize != new_volsize) 3447 zfs_prop_set_int(zhp, resv_prop, new_volsize); 3448 } 3449 err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3450 zfs_close(zhp); 3451 } 3452 return (err); 3453 } 3454 3455 /* 3456 * Iterate over all dependents for a given dataset. This includes both 3457 * hierarchical dependents (children) and data dependents (snapshots and 3458 * clones). The bulk of the processing occurs in get_dependents() in 3459 * libzfs_graph.c. 3460 */ 3461 int 3462 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 3463 zfs_iter_f func, void *data) 3464 { 3465 char **dependents; 3466 size_t count; 3467 int i; 3468 zfs_handle_t *child; 3469 int ret = 0; 3470 3471 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 3472 &dependents, &count) != 0) 3473 return (-1); 3474 3475 for (i = 0; i < count; i++) { 3476 if ((child = make_dataset_handle(zhp->zfs_hdl, 3477 dependents[i])) == NULL) 3478 continue; 3479 3480 if ((ret = func(child, data)) != 0) 3481 break; 3482 } 3483 3484 for (i = 0; i < count; i++) 3485 free(dependents[i]); 3486 free(dependents); 3487 3488 return (ret); 3489 } 3490 3491 /* 3492 * Renames the given dataset. 3493 */ 3494 int 3495 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3496 { 3497 int ret; 3498 zfs_cmd_t zc = { 0 }; 3499 char *delim; 3500 prop_changelist_t *cl = NULL; 3501 zfs_handle_t *zhrp = NULL; 3502 char *parentname = NULL; 3503 char parent[ZFS_MAXNAMELEN]; 3504 libzfs_handle_t *hdl = zhp->zfs_hdl; 3505 char errbuf[1024]; 3506 3507 /* if we have the same exact name, just return success */ 3508 if (strcmp(zhp->zfs_name, target) == 0) 3509 return (0); 3510 3511 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3512 "cannot rename to '%s'"), target); 3513 3514 /* 3515 * Make sure the target name is valid 3516 */ 3517 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 3518 if ((strchr(target, '@') == NULL) || 3519 *target == '@') { 3520 /* 3521 * Snapshot target name is abbreviated, 3522 * reconstruct full dataset name 3523 */ 3524 (void) strlcpy(parent, zhp->zfs_name, 3525 sizeof (parent)); 3526 delim = strchr(parent, '@'); 3527 if (strchr(target, '@') == NULL) 3528 *(++delim) = '\0'; 3529 else 3530 *delim = '\0'; 3531 (void) strlcat(parent, target, sizeof (parent)); 3532 target = parent; 3533 } else { 3534 /* 3535 * Make sure we're renaming within the same dataset. 3536 */ 3537 delim = strchr(target, '@'); 3538 if (strncmp(zhp->zfs_name, target, delim - target) 3539 != 0 || zhp->zfs_name[delim - target] != '@') { 3540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3541 "snapshots must be part of same " 3542 "dataset")); 3543 return (zfs_error(hdl, EZFS_CROSSTARGET, 3544 errbuf)); 3545 } 3546 } 3547 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 3548 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3549 } else { 3550 if (recursive) { 3551 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3552 "recursive rename must be a snapshot")); 3553 return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3554 } 3555 3556 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 3557 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3558 uint64_t unused; 3559 3560 /* validate parents */ 3561 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3562 return (-1); 3563 3564 (void) parent_name(target, parent, sizeof (parent)); 3565 3566 /* make sure we're in the same pool */ 3567 verify((delim = strchr(target, '/')) != NULL); 3568 if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3569 zhp->zfs_name[delim - target] != '/') { 3570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3571 "datasets must be within same pool")); 3572 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3573 } 3574 3575 /* new name cannot be a child of the current dataset name */ 3576 if (strncmp(parent, zhp->zfs_name, 3577 strlen(zhp->zfs_name)) == 0) { 3578 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3579 "New dataset name cannot be a descendent of " 3580 "current dataset name")); 3581 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3582 } 3583 } 3584 3585 (void) snprintf(errbuf, sizeof (errbuf), 3586 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 3587 3588 if (getzoneid() == GLOBAL_ZONEID && 3589 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 3590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3591 "dataset is used in a non-global zone")); 3592 return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3593 } 3594 3595 if (recursive) { 3596 struct destroydata dd; 3597 3598 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 3599 if (parentname == NULL) { 3600 ret = -1; 3601 goto error; 3602 } 3603 delim = strchr(parentname, '@'); 3604 *delim = '\0'; 3605 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 3606 if (zhrp == NULL) { 3607 ret = -1; 3608 goto error; 3609 } 3610 3611 dd.snapname = delim + 1; 3612 dd.gotone = B_FALSE; 3613 dd.closezhp = B_TRUE; 3614 3615 /* We remove any zvol links prior to renaming them */ 3616 ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 3617 if (ret) { 3618 goto error; 3619 } 3620 } else { 3621 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 3622 return (-1); 3623 3624 if (changelist_haszonedchild(cl)) { 3625 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3626 "child dataset with inherited mountpoint is used " 3627 "in a non-global zone")); 3628 (void) zfs_error(hdl, EZFS_ZONED, errbuf); 3629 goto error; 3630 } 3631 3632 if ((ret = changelist_prefix(cl)) != 0) 3633 goto error; 3634 } 3635 3636 if (ZFS_IS_VOLUME(zhp)) 3637 zc.zc_objset_type = DMU_OST_ZVOL; 3638 else 3639 zc.zc_objset_type = DMU_OST_ZFS; 3640 3641 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3642 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 3643 3644 zc.zc_cookie = recursive; 3645 3646 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 3647 /* 3648 * if it was recursive, the one that actually failed will 3649 * be in zc.zc_name 3650 */ 3651 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3652 "cannot rename '%s'"), zc.zc_name); 3653 3654 if (recursive && errno == EEXIST) { 3655 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3656 "a child dataset already has a snapshot " 3657 "with the new name")); 3658 (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 3659 } else { 3660 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 3661 } 3662 3663 /* 3664 * On failure, we still want to remount any filesystems that 3665 * were previously mounted, so we don't alter the system state. 3666 */ 3667 if (recursive) { 3668 struct createdata cd; 3669 3670 /* only create links for datasets that had existed */ 3671 cd.cd_snapname = delim + 1; 3672 cd.cd_ifexists = B_TRUE; 3673 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 3674 &cd); 3675 } else { 3676 (void) changelist_postfix(cl); 3677 } 3678 } else { 3679 if (recursive) { 3680 struct createdata cd; 3681 3682 /* only create links for datasets that had existed */ 3683 cd.cd_snapname = strchr(target, '@') + 1; 3684 cd.cd_ifexists = B_TRUE; 3685 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 3686 &cd); 3687 } else { 3688 changelist_rename(cl, zfs_get_name(zhp), target); 3689 ret = changelist_postfix(cl); 3690 } 3691 } 3692 3693 error: 3694 if (parentname) { 3695 free(parentname); 3696 } 3697 if (zhrp) { 3698 zfs_close(zhrp); 3699 } 3700 if (cl) { 3701 changelist_free(cl); 3702 } 3703 return (ret); 3704 } 3705 3706 /* 3707 * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3708 * poke devfsadm to create the /dev link, and then wait for the link to appear. 3709 */ 3710 int 3711 zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3712 { 3713 return (zvol_create_link_common(hdl, dataset, B_FALSE)); 3714 } 3715 3716 static int 3717 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 3718 { 3719 zfs_cmd_t zc = { 0 }; 3720 di_devlink_handle_t dhdl; 3721 priv_set_t *priv_effective; 3722 int privileged; 3723 3724 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3725 3726 /* 3727 * Issue the appropriate ioctl. 3728 */ 3729 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3730 switch (errno) { 3731 case EEXIST: 3732 /* 3733 * Silently ignore the case where the link already 3734 * exists. This allows 'zfs volinit' to be run multiple 3735 * times without errors. 3736 */ 3737 return (0); 3738 3739 case ENOENT: 3740 /* 3741 * Dataset does not exist in the kernel. If we 3742 * don't care (see zfs_rename), then ignore the 3743 * error quietly. 3744 */ 3745 if (ifexists) { 3746 return (0); 3747 } 3748 3749 /* FALLTHROUGH */ 3750 3751 default: 3752 return (zfs_standard_error_fmt(hdl, errno, 3753 dgettext(TEXT_DOMAIN, "cannot create device links " 3754 "for '%s'"), dataset)); 3755 } 3756 } 3757 3758 /* 3759 * If privileged call devfsadm and wait for the links to 3760 * magically appear. 3761 * Otherwise, print out an informational message. 3762 */ 3763 3764 priv_effective = priv_allocset(); 3765 (void) getppriv(PRIV_EFFECTIVE, priv_effective); 3766 privileged = (priv_isfullset(priv_effective) == B_TRUE); 3767 priv_freeset(priv_effective); 3768 3769 if (privileged) { 3770 if ((dhdl = di_devlink_init(ZFS_DRIVER, 3771 DI_MAKE_LINK)) == NULL) { 3772 zfs_error_aux(hdl, strerror(errno)); 3773 (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 3774 dgettext(TEXT_DOMAIN, "cannot create device links " 3775 "for '%s'"), dataset); 3776 (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 3777 return (-1); 3778 } else { 3779 (void) di_devlink_fini(&dhdl); 3780 } 3781 } else { 3782 char pathname[MAXPATHLEN]; 3783 struct stat64 statbuf; 3784 int i; 3785 3786 #define MAX_WAIT 10 3787 3788 /* 3789 * This is the poor mans way of waiting for the link 3790 * to show up. If after 10 seconds we still don't 3791 * have it, then print out a message. 3792 */ 3793 (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 3794 dataset); 3795 3796 for (i = 0; i != MAX_WAIT; i++) { 3797 if (stat64(pathname, &statbuf) == 0) 3798 break; 3799 (void) sleep(1); 3800 } 3801 if (i == MAX_WAIT) 3802 (void) printf(gettext("%s may not be immediately " 3803 "available\n"), pathname); 3804 } 3805 3806 return (0); 3807 } 3808 3809 /* 3810 * Remove a minor node for the given zvol and the associated /dev links. 3811 */ 3812 int 3813 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3814 { 3815 zfs_cmd_t zc = { 0 }; 3816 3817 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3818 3819 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3820 switch (errno) { 3821 case ENXIO: 3822 /* 3823 * Silently ignore the case where the link no longer 3824 * exists, so that 'zfs volfini' can be run multiple 3825 * times without errors. 3826 */ 3827 return (0); 3828 3829 default: 3830 return (zfs_standard_error_fmt(hdl, errno, 3831 dgettext(TEXT_DOMAIN, "cannot remove device " 3832 "links for '%s'"), dataset)); 3833 } 3834 } 3835 3836 return (0); 3837 } 3838 3839 nvlist_t * 3840 zfs_get_user_props(zfs_handle_t *zhp) 3841 { 3842 return (zhp->zfs_user_props); 3843 } 3844 3845 /* 3846 * This function is used by 'zfs list' to determine the exact set of columns to 3847 * display, and their maximum widths. This does two main things: 3848 * 3849 * - If this is a list of all properties, then expand the list to include 3850 * all native properties, and set a flag so that for each dataset we look 3851 * for new unique user properties and add them to the list. 3852 * 3853 * - For non fixed-width properties, keep track of the maximum width seen 3854 * so that we can size the column appropriately. 3855 */ 3856 int 3857 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 3858 { 3859 libzfs_handle_t *hdl = zhp->zfs_hdl; 3860 zprop_list_t *entry; 3861 zprop_list_t **last, **start; 3862 nvlist_t *userprops, *propval; 3863 nvpair_t *elem; 3864 char *strval; 3865 char buf[ZFS_MAXPROPLEN]; 3866 3867 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 3868 return (-1); 3869 3870 userprops = zfs_get_user_props(zhp); 3871 3872 entry = *plp; 3873 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 3874 /* 3875 * Go through and add any user properties as necessary. We 3876 * start by incrementing our list pointer to the first 3877 * non-native property. 3878 */ 3879 start = plp; 3880 while (*start != NULL) { 3881 if ((*start)->pl_prop == ZPROP_INVAL) 3882 break; 3883 start = &(*start)->pl_next; 3884 } 3885 3886 elem = NULL; 3887 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 3888 /* 3889 * See if we've already found this property in our list. 3890 */ 3891 for (last = start; *last != NULL; 3892 last = &(*last)->pl_next) { 3893 if (strcmp((*last)->pl_user_prop, 3894 nvpair_name(elem)) == 0) 3895 break; 3896 } 3897 3898 if (*last == NULL) { 3899 if ((entry = zfs_alloc(hdl, 3900 sizeof (zprop_list_t))) == NULL || 3901 ((entry->pl_user_prop = zfs_strdup(hdl, 3902 nvpair_name(elem)))) == NULL) { 3903 free(entry); 3904 return (-1); 3905 } 3906 3907 entry->pl_prop = ZPROP_INVAL; 3908 entry->pl_width = strlen(nvpair_name(elem)); 3909 entry->pl_all = B_TRUE; 3910 *last = entry; 3911 } 3912 } 3913 } 3914 3915 /* 3916 * Now go through and check the width of any non-fixed columns 3917 */ 3918 for (entry = *plp; entry != NULL; entry = entry->pl_next) { 3919 if (entry->pl_fixed) 3920 continue; 3921 3922 if (entry->pl_prop != ZPROP_INVAL) { 3923 if (zfs_prop_get(zhp, entry->pl_prop, 3924 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 3925 if (strlen(buf) > entry->pl_width) 3926 entry->pl_width = strlen(buf); 3927 } 3928 } else if (nvlist_lookup_nvlist(userprops, 3929 entry->pl_user_prop, &propval) == 0) { 3930 verify(nvlist_lookup_string(propval, 3931 ZPROP_VALUE, &strval) == 0); 3932 if (strlen(strval) > entry->pl_width) 3933 entry->pl_width = strlen(strval); 3934 } 3935 } 3936 3937 return (0); 3938 } 3939 3940 int 3941 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 3942 { 3943 zfs_cmd_t zc = { 0 }; 3944 nvlist_t *nvp; 3945 gid_t gid; 3946 uid_t uid; 3947 const gid_t *groups; 3948 int group_cnt; 3949 int error; 3950 3951 if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 3952 return (no_memory(hdl)); 3953 3954 uid = ucred_geteuid(cred); 3955 gid = ucred_getegid(cred); 3956 group_cnt = ucred_getgroups(cred, &groups); 3957 3958 if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 3959 return (1); 3960 3961 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 3962 nvlist_free(nvp); 3963 return (1); 3964 } 3965 3966 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 3967 nvlist_free(nvp); 3968 return (1); 3969 } 3970 3971 if (nvlist_add_uint32_array(nvp, 3972 ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 3973 nvlist_free(nvp); 3974 return (1); 3975 } 3976 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3977 3978 if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 3979 return (-1); 3980 3981 error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 3982 nvlist_free(nvp); 3983 return (error); 3984 } 3985 3986 int 3987 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 3988 void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 3989 { 3990 zfs_cmd_t zc = { 0 }; 3991 int error; 3992 3993 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3994 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 3995 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 3996 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 3997 zc.zc_share.z_sharetype = operation; 3998 zc.zc_share.z_sharemax = sharemax; 3999 4000 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4001 return (error); 4002 } 4003