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