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