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