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