1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 213126Sahl 22789Sahrens /* 233363Sgw25295 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 29789Sahrens #include <assert.h> 30789Sahrens #include <ctype.h> 31789Sahrens #include <errno.h> 32789Sahrens #include <libdevinfo.h> 33789Sahrens #include <libintl.h> 34789Sahrens #include <math.h> 35789Sahrens #include <stdio.h> 36789Sahrens #include <stdlib.h> 37789Sahrens #include <strings.h> 38789Sahrens #include <unistd.h> 39789Sahrens #include <zone.h> 402082Seschrock #include <fcntl.h> 41789Sahrens #include <sys/mntent.h> 42789Sahrens #include <sys/mnttab.h> 431294Slling #include <sys/mount.h> 444543Smarks #include <sys/avl.h> 454543Smarks #include <priv.h> 464543Smarks #include <pwd.h> 474543Smarks #include <grp.h> 484543Smarks #include <stddef.h> 494543Smarks #include <ucred.h> 50789Sahrens 51789Sahrens #include <sys/spa.h> 52789Sahrens #include <sys/zio.h> 532676Seschrock #include <sys/zap.h> 54789Sahrens #include <libzfs.h> 55789Sahrens 56789Sahrens #include "zfs_namecheck.h" 57789Sahrens #include "zfs_prop.h" 58789Sahrens #include "libzfs_impl.h" 594543Smarks #include "zfs_deleg.h" 60789Sahrens 614490Svb160487 static int create_parents(libzfs_handle_t *, char *, int); 624007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 634007Smmusante 64789Sahrens /* 65789Sahrens * Given a single type (not a mask of types), return the type in a human 66789Sahrens * readable form. 67789Sahrens */ 68789Sahrens const char * 69789Sahrens zfs_type_to_name(zfs_type_t type) 70789Sahrens { 71789Sahrens switch (type) { 72789Sahrens case ZFS_TYPE_FILESYSTEM: 73789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 74789Sahrens case ZFS_TYPE_SNAPSHOT: 75789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 76789Sahrens case ZFS_TYPE_VOLUME: 77789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 78789Sahrens } 79789Sahrens 80789Sahrens return (NULL); 81789Sahrens } 82789Sahrens 83789Sahrens /* 84789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 85789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 86789Sahrens * We guess what the type would have been based on the path and the mask of 87789Sahrens * acceptable types. 88789Sahrens */ 89789Sahrens static const char * 90789Sahrens path_to_str(const char *path, int types) 91789Sahrens { 92789Sahrens /* 93789Sahrens * When given a single type, always report the exact type. 94789Sahrens */ 95789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 96789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 97789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 98789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 99789Sahrens if (types == ZFS_TYPE_VOLUME) 100789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 101789Sahrens 102789Sahrens /* 103789Sahrens * The user is requesting more than one type of dataset. If this is the 104789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 105789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 106789Sahrens * snapshot attribute and try again. 107789Sahrens */ 108789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 109789Sahrens if (strchr(path, '@') != NULL) 110789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 111789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 112789Sahrens } 113789Sahrens 114789Sahrens 115789Sahrens /* 116789Sahrens * The user has requested either filesystems or volumes. 117789Sahrens * We have no way of knowing a priori what type this would be, so always 118789Sahrens * report it as "filesystem" or "volume", our two primitive types. 119789Sahrens */ 120789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 121789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 122789Sahrens 123789Sahrens assert(types & ZFS_TYPE_VOLUME); 124789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 125789Sahrens } 126789Sahrens 127789Sahrens /* 128789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 129789Sahrens * provide a more meaningful error message. We place a more useful message in 130789Sahrens * 'buf' detailing exactly why the name was not valid. 131789Sahrens */ 132789Sahrens static int 1332082Seschrock zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type) 134789Sahrens { 135789Sahrens namecheck_err_t why; 136789Sahrens char what; 137789Sahrens 138789Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 1392082Seschrock if (hdl != NULL) { 140789Sahrens switch (why) { 1411003Slling case NAME_ERR_TOOLONG: 1422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1432082Seschrock "name is too long")); 1441003Slling break; 1451003Slling 146789Sahrens case NAME_ERR_LEADING_SLASH: 1472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1482082Seschrock "leading slash in name")); 149789Sahrens break; 150789Sahrens 151789Sahrens case NAME_ERR_EMPTY_COMPONENT: 1522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1532082Seschrock "empty component in name")); 154789Sahrens break; 155789Sahrens 156789Sahrens case NAME_ERR_TRAILING_SLASH: 1572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1582082Seschrock "trailing slash in name")); 159789Sahrens break; 160789Sahrens 161789Sahrens case NAME_ERR_INVALCHAR: 1622082Seschrock zfs_error_aux(hdl, 163789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 1642082Seschrock "'%c' in name"), what); 165789Sahrens break; 166789Sahrens 167789Sahrens case NAME_ERR_MULTIPLE_AT: 1682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1692082Seschrock "multiple '@' delimiters in name")); 170789Sahrens break; 1712856Snd150628 1722856Snd150628 case NAME_ERR_NOLETTER: 1732856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1742856Snd150628 "pool doesn't begin with a letter")); 1752856Snd150628 break; 1762856Snd150628 1772856Snd150628 case NAME_ERR_RESERVED: 1782856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1792856Snd150628 "name is reserved")); 1802856Snd150628 break; 1812856Snd150628 1822856Snd150628 case NAME_ERR_DISKLIKE: 1832856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1842856Snd150628 "reserved disk name")); 1852856Snd150628 break; 186789Sahrens } 187789Sahrens } 188789Sahrens 189789Sahrens return (0); 190789Sahrens } 191789Sahrens 192789Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 1932082Seschrock if (hdl != NULL) 1942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1952082Seschrock "snapshot delimiter '@' in filesystem name")); 196789Sahrens return (0); 197789Sahrens } 198789Sahrens 1992199Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 2002199Sahrens if (hdl != NULL) 2012199Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2023413Smmusante "missing '@' delimiter in snapshot name")); 2032199Sahrens return (0); 2042199Sahrens } 2052199Sahrens 2062082Seschrock return (-1); 207789Sahrens } 208789Sahrens 209789Sahrens int 210789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 211789Sahrens { 2122082Seschrock return (zfs_validate_name(NULL, name, type)); 213789Sahrens } 214789Sahrens 215789Sahrens /* 2162676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2172676Seschrock * properties into a separate nvlist. 2182676Seschrock */ 2194217Seschrock static nvlist_t * 2204217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2212676Seschrock { 2222676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2232676Seschrock nvpair_t *elem; 2242676Seschrock nvlist_t *propval; 2254217Seschrock nvlist_t *nvl; 2264217Seschrock 2274217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2284217Seschrock (void) no_memory(hdl); 2294217Seschrock return (NULL); 2304217Seschrock } 2312676Seschrock 2322676Seschrock elem = NULL; 2334217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2342676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2352676Seschrock continue; 2362676Seschrock 2372676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2384217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2394217Seschrock nvlist_free(nvl); 2404217Seschrock (void) no_memory(hdl); 2414217Seschrock return (NULL); 2424217Seschrock } 2432676Seschrock } 2442676Seschrock 2454217Seschrock return (nvl); 2462676Seschrock } 2472676Seschrock 2482676Seschrock /* 249789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 250789Sahrens */ 251789Sahrens static int 252789Sahrens get_stats(zfs_handle_t *zhp) 253789Sahrens { 254789Sahrens zfs_cmd_t zc = { 0 }; 2552676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2564217Seschrock nvlist_t *allprops, *userprops; 257789Sahrens 258789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 259789Sahrens 2602676Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 2612082Seschrock return (-1); 2621356Seschrock 2632082Seschrock while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 2641356Seschrock if (errno == ENOMEM) { 2652676Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2662676Seschrock zcmd_free_nvlists(&zc); 2672082Seschrock return (-1); 2682676Seschrock } 2691356Seschrock } else { 2702676Seschrock zcmd_free_nvlists(&zc); 2711356Seschrock return (-1); 2721356Seschrock } 2731356Seschrock } 274789Sahrens 2752885Sahrens zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 276789Sahrens 2772676Seschrock (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 2781544Seschrock 2794217Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 2802676Seschrock zcmd_free_nvlists(&zc); 2812082Seschrock return (-1); 2822082Seschrock } 283789Sahrens 2842676Seschrock zcmd_free_nvlists(&zc); 2852676Seschrock 2864217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 2874217Seschrock nvlist_free(allprops); 2882676Seschrock return (-1); 2894217Seschrock } 2904217Seschrock 2914217Seschrock nvlist_free(zhp->zfs_props); 2924217Seschrock nvlist_free(zhp->zfs_user_props); 2934217Seschrock 2944217Seschrock zhp->zfs_props = allprops; 2954217Seschrock zhp->zfs_user_props = userprops; 2962082Seschrock 297789Sahrens return (0); 298789Sahrens } 299789Sahrens 300789Sahrens /* 301789Sahrens * Refresh the properties currently stored in the handle. 302789Sahrens */ 303789Sahrens void 304789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 305789Sahrens { 306789Sahrens (void) get_stats(zhp); 307789Sahrens } 308789Sahrens 309789Sahrens /* 310789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 311789Sahrens * zfs_iter_* to create child handles on the fly. 312789Sahrens */ 313789Sahrens zfs_handle_t * 3142082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path) 315789Sahrens { 3162082Seschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 3174543Smarks char *logstr; 3182082Seschrock 3192082Seschrock if (zhp == NULL) 3202082Seschrock return (NULL); 3212082Seschrock 3222082Seschrock zhp->zfs_hdl = hdl; 323789Sahrens 3244543Smarks /* 3254543Smarks * Preserve history log string. 3264543Smarks * any changes performed here will be 3274543Smarks * logged as an internal event. 3284543Smarks */ 3294543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 3304543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 3311758Sahrens top: 332789Sahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 333789Sahrens 334789Sahrens if (get_stats(zhp) != 0) { 3354543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 336789Sahrens free(zhp); 337789Sahrens return (NULL); 338789Sahrens } 339789Sahrens 3401758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 3411758Sahrens zfs_cmd_t zc = { 0 }; 3421758Sahrens 3431758Sahrens /* 3441758Sahrens * If it is dds_inconsistent, then we've caught it in 3451758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 3461758Sahrens * it is inconsistent from the ZPL's point of view, so 3471758Sahrens * can't be mounted. However, it could also be that we 3481758Sahrens * have crashed in the middle of one of those 3491758Sahrens * operations, in which case we need to get rid of the 3501758Sahrens * inconsistent state. We do that by either rolling 3511758Sahrens * back to the previous snapshot (which will fail if 3521758Sahrens * there is none), or destroying the filesystem. Note 3531758Sahrens * that if we are still in the middle of an active 3541758Sahrens * 'receive' or 'destroy', then the rollback and destroy 3551758Sahrens * will fail with EBUSY and we will drive on as usual. 3561758Sahrens */ 3571758Sahrens 3581758Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3591758Sahrens 3602885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 3612082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 3621758Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3631758Sahrens } else { 3641758Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3651758Sahrens } 3661758Sahrens 3671758Sahrens /* If we can successfully roll it back, reget the stats */ 3682082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 3691758Sahrens goto top; 3701758Sahrens /* 3711758Sahrens * If we can sucessfully destroy it, pretend that it 3721758Sahrens * never existed. 3731758Sahrens */ 3742082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 3754543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 3761758Sahrens free(zhp); 3771758Sahrens errno = ENOENT; 3781758Sahrens return (NULL); 3791758Sahrens } 3801758Sahrens } 3811758Sahrens 382789Sahrens /* 383789Sahrens * We've managed to open the dataset and gather statistics. Determine 384789Sahrens * the high-level type. 385789Sahrens */ 3862885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 3872885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 3882885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 3892885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 3902885Sahrens else 3912885Sahrens abort(); 3922885Sahrens 393789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 394789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 395789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 396789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 397789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 398789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 399789Sahrens else 4002082Seschrock abort(); /* we should never see any other types */ 401789Sahrens 4024543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 403789Sahrens return (zhp); 404789Sahrens } 405789Sahrens 406789Sahrens /* 407789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 408789Sahrens * argument is a mask of acceptable types. The function will print an 409789Sahrens * appropriate error message and return NULL if it can't be opened. 410789Sahrens */ 411789Sahrens zfs_handle_t * 4122082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 413789Sahrens { 414789Sahrens zfs_handle_t *zhp; 4152082Seschrock char errbuf[1024]; 4162082Seschrock 4172082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 4182082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 419789Sahrens 420789Sahrens /* 4212082Seschrock * Validate the name before we even try to open it. 422789Sahrens */ 4232082Seschrock if (!zfs_validate_name(hdl, path, ZFS_TYPE_ANY)) { 4242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4252082Seschrock "invalid dataset name")); 4262082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 427789Sahrens return (NULL); 428789Sahrens } 429789Sahrens 430789Sahrens /* 431789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 432789Sahrens */ 433789Sahrens errno = 0; 4342082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 4353237Slling (void) zfs_standard_error(hdl, errno, errbuf); 436789Sahrens return (NULL); 437789Sahrens } 438789Sahrens 439789Sahrens if (!(types & zhp->zfs_type)) { 4402082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 4412142Seschrock zfs_close(zhp); 442789Sahrens return (NULL); 443789Sahrens } 444789Sahrens 445789Sahrens return (zhp); 446789Sahrens } 447789Sahrens 448789Sahrens /* 449789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 450789Sahrens */ 451789Sahrens void 452789Sahrens zfs_close(zfs_handle_t *zhp) 453789Sahrens { 454789Sahrens if (zhp->zfs_mntopts) 455789Sahrens free(zhp->zfs_mntopts); 4562676Seschrock nvlist_free(zhp->zfs_props); 4572676Seschrock nvlist_free(zhp->zfs_user_props); 458789Sahrens free(zhp); 459789Sahrens } 460789Sahrens 461789Sahrens /* 462789Sahrens * Given a numeric suffix, convert the value into a number of bits that the 463789Sahrens * resulting value must be shifted. 464789Sahrens */ 465789Sahrens static int 4662082Seschrock str2shift(libzfs_handle_t *hdl, const char *buf) 467789Sahrens { 468789Sahrens const char *ends = "BKMGTPEZ"; 469789Sahrens int i; 470789Sahrens 471789Sahrens if (buf[0] == '\0') 472789Sahrens return (0); 473789Sahrens for (i = 0; i < strlen(ends); i++) { 474789Sahrens if (toupper(buf[0]) == ends[i]) 475789Sahrens break; 476789Sahrens } 477789Sahrens if (i == strlen(ends)) { 4782082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4792082Seschrock "invalid numeric suffix '%s'"), buf); 480789Sahrens return (-1); 481789Sahrens } 482789Sahrens 483789Sahrens /* 484789Sahrens * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 485789Sahrens * allow 'BB' - that's just weird. 486789Sahrens */ 487789Sahrens if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 4882082Seschrock toupper(buf[0]) != 'B')) 489789Sahrens return (10*i); 4902082Seschrock 4912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4922082Seschrock "invalid numeric suffix '%s'"), buf); 493789Sahrens return (-1); 494789Sahrens } 495789Sahrens 496789Sahrens /* 497789Sahrens * Convert a string of the form '100G' into a real number. Used when setting 498789Sahrens * properties or creating a volume. 'buf' is used to place an extended error 499789Sahrens * message for the caller to use. 500789Sahrens */ 501789Sahrens static int 5022082Seschrock nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) 503789Sahrens { 504789Sahrens char *end; 505789Sahrens int shift; 506789Sahrens 507789Sahrens *num = 0; 508789Sahrens 509789Sahrens /* Check to see if this looks like a number. */ 510789Sahrens if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { 5112082Seschrock if (hdl) 5122082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5132082Seschrock "bad numeric value '%s'"), value); 514789Sahrens return (-1); 515789Sahrens } 516789Sahrens 517789Sahrens /* Rely on stroll() to process the numeric portion. */ 518789Sahrens errno = 0; 519789Sahrens *num = strtoll(value, &end, 10); 520789Sahrens 521789Sahrens /* 522789Sahrens * Check for ERANGE, which indicates that the value is too large to fit 523789Sahrens * in a 64-bit value. 524789Sahrens */ 525789Sahrens if (errno == ERANGE) { 5262082Seschrock if (hdl) 5272082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5282082Seschrock "numeric value is too large")); 529789Sahrens return (-1); 530789Sahrens } 531789Sahrens 532789Sahrens /* 533789Sahrens * If we have a decimal value, then do the computation with floating 534789Sahrens * point arithmetic. Otherwise, use standard arithmetic. 535789Sahrens */ 536789Sahrens if (*end == '.') { 537789Sahrens double fval = strtod(value, &end); 538789Sahrens 5392082Seschrock if ((shift = str2shift(hdl, end)) == -1) 540789Sahrens return (-1); 541789Sahrens 542789Sahrens fval *= pow(2, shift); 543789Sahrens 544789Sahrens if (fval > UINT64_MAX) { 5452082Seschrock if (hdl) 5462082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5472082Seschrock "numeric value is too large")); 548789Sahrens return (-1); 549789Sahrens } 550789Sahrens 551789Sahrens *num = (uint64_t)fval; 552789Sahrens } else { 5532082Seschrock if ((shift = str2shift(hdl, end)) == -1) 554789Sahrens return (-1); 555789Sahrens 556789Sahrens /* Check for overflow */ 557789Sahrens if (shift >= 64 || (*num << shift) >> shift != *num) { 5582082Seschrock if (hdl) 5592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5602082Seschrock "numeric value is too large")); 561789Sahrens return (-1); 562789Sahrens } 563789Sahrens 564789Sahrens *num <<= shift; 565789Sahrens } 566789Sahrens 567789Sahrens return (0); 568789Sahrens } 569789Sahrens 570789Sahrens int 5712676Seschrock zfs_nicestrtonum(libzfs_handle_t *hdl, const char *str, uint64_t *val) 5722676Seschrock { 5732676Seschrock return (nicestrtonum(hdl, str, val)); 5742676Seschrock } 5752676Seschrock 5762676Seschrock /* 5772676Seschrock * The prop_parse_*() functions are designed to allow flexibility in callers 5782676Seschrock * when setting properties. At the DSL layer, all properties are either 64-bit 5792676Seschrock * numbers or strings. We want the user to be able to ignore this fact and 5802676Seschrock * specify properties as native values (boolean, for example) or as strings (to 5812676Seschrock * simplify command line utilities). This also handles converting index types 5822676Seschrock * (compression, checksum, etc) from strings to their on-disk index. 5832676Seschrock */ 5842676Seschrock 5852676Seschrock static int 5862676Seschrock prop_parse_boolean(libzfs_handle_t *hdl, nvpair_t *elem, uint64_t *val) 587789Sahrens { 5882676Seschrock uint64_t ret; 5892676Seschrock 5902676Seschrock switch (nvpair_type(elem)) { 5912676Seschrock case DATA_TYPE_STRING: 5922676Seschrock { 5932676Seschrock char *value; 5943363Sgw25295 verify(nvpair_value_string(elem, &value) == 0); 5952676Seschrock 5962676Seschrock if (strcmp(value, "on") == 0) { 5972676Seschrock ret = 1; 5982676Seschrock } else if (strcmp(value, "off") == 0) { 5992676Seschrock ret = 0; 6002676Seschrock } else { 6012676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6022676Seschrock "property '%s' must be 'on' or 'off'"), 6032676Seschrock nvpair_name(elem)); 6042676Seschrock return (-1); 6052676Seschrock } 6062676Seschrock break; 6072676Seschrock } 6082676Seschrock 6092676Seschrock case DATA_TYPE_UINT64: 6102676Seschrock { 6113363Sgw25295 verify(nvpair_value_uint64(elem, &ret) == 0); 6122676Seschrock if (ret > 1) { 6132676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6142676Seschrock "'%s' must be a boolean value"), 6152676Seschrock nvpair_name(elem)); 6162676Seschrock return (-1); 6172676Seschrock } 6182676Seschrock break; 6192676Seschrock } 6202676Seschrock 6212676Seschrock case DATA_TYPE_BOOLEAN_VALUE: 6222676Seschrock { 6232676Seschrock boolean_t value; 6243363Sgw25295 verify(nvpair_value_boolean_value(elem, &value) == 0); 6252676Seschrock ret = value; 6262676Seschrock break; 6272676Seschrock } 6282676Seschrock 6292676Seschrock default: 6302676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6312676Seschrock "'%s' must be a boolean value"), 6322676Seschrock nvpair_name(elem)); 6332676Seschrock return (-1); 6342676Seschrock } 6352676Seschrock 6362676Seschrock *val = ret; 6372676Seschrock return (0); 6382676Seschrock } 6392676Seschrock 6402676Seschrock static int 6412676Seschrock prop_parse_number(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop, 6422676Seschrock uint64_t *val) 6432676Seschrock { 6442676Seschrock uint64_t ret; 6452676Seschrock boolean_t isnone = B_FALSE; 6462676Seschrock 6472676Seschrock switch (nvpair_type(elem)) { 6482676Seschrock case DATA_TYPE_STRING: 6492676Seschrock { 6502676Seschrock char *value; 6512676Seschrock (void) nvpair_value_string(elem, &value); 6522676Seschrock if (strcmp(value, "none") == 0) { 6532676Seschrock isnone = B_TRUE; 6542676Seschrock ret = 0; 6552676Seschrock } else if (nicestrtonum(hdl, value, &ret) != 0) { 6562676Seschrock return (-1); 6572676Seschrock } 6582676Seschrock break; 6592676Seschrock } 6602676Seschrock 6612676Seschrock case DATA_TYPE_UINT64: 6622676Seschrock (void) nvpair_value_uint64(elem, &ret); 6632676Seschrock break; 6642676Seschrock 6652676Seschrock default: 6662676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6672676Seschrock "'%s' must be a number"), 6682676Seschrock nvpair_name(elem)); 6692676Seschrock return (-1); 6702676Seschrock } 6712676Seschrock 6722676Seschrock /* 6732676Seschrock * Quota special: force 'none' and don't allow 0. 6742676Seschrock */ 6752676Seschrock if (ret == 0 && !isnone && prop == ZFS_PROP_QUOTA) { 6762676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6772676Seschrock "use 'none' to disable quota")); 6782676Seschrock return (-1); 6792676Seschrock } 6802676Seschrock 6812676Seschrock *val = ret; 6822676Seschrock return (0); 6832676Seschrock } 6842676Seschrock 6852676Seschrock static int 6862676Seschrock prop_parse_index(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop, 6872676Seschrock uint64_t *val) 6882676Seschrock { 6892676Seschrock char *propname = nvpair_name(elem); 6902676Seschrock char *value; 6912676Seschrock 6922676Seschrock if (nvpair_type(elem) != DATA_TYPE_STRING) { 6932676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6942676Seschrock "'%s' must be a string"), propname); 6952676Seschrock return (-1); 6962676Seschrock } 6972676Seschrock 6982676Seschrock (void) nvpair_value_string(elem, &value); 6992676Seschrock 7002676Seschrock if (zfs_prop_string_to_index(prop, value, val) != 0) { 7012676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7022676Seschrock "'%s' must be one of '%s'"), propname, 7032676Seschrock zfs_prop_values(prop)); 7042676Seschrock return (-1); 7052676Seschrock } 7062676Seschrock 7072676Seschrock return (0); 708789Sahrens } 709789Sahrens 710789Sahrens /* 7113912Slling * Check if the bootfs name has the same pool name as it is set to. 7123912Slling * Assuming bootfs is a valid dataset name. 7133912Slling */ 7143912Slling static boolean_t 7153912Slling bootfs_poolname_valid(char *pool, char *bootfs) 7163912Slling { 7173912Slling char ch, *pname; 7183912Slling 7193912Slling /* get the pool name from the bootfs name */ 7203912Slling pname = bootfs; 7213912Slling while (*bootfs && !isspace(*bootfs) && *bootfs != '/') 7223912Slling bootfs++; 7233912Slling 7243912Slling ch = *bootfs; 7253912Slling *bootfs = 0; 7263912Slling 7273912Slling if (strcmp(pool, pname) == 0) { 7283912Slling *bootfs = ch; 7293912Slling return (B_TRUE); 7303912Slling } 7313912Slling 7323912Slling *bootfs = ch; 7333912Slling return (B_FALSE); 7343912Slling } 7353912Slling 7363912Slling /* 7372676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 7382676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 7392676Seschrock * strings. 740789Sahrens */ 7413912Slling nvlist_t * 7423912Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, char *pool_name, 7433912Slling nvlist_t *nvl, uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 744789Sahrens { 7452676Seschrock nvpair_t *elem; 7462676Seschrock const char *propname; 7472676Seschrock zfs_prop_t prop; 7482676Seschrock uint64_t intval; 7492676Seschrock char *strval; 7502676Seschrock nvlist_t *ret; 7513912Slling int isuser; 7522676Seschrock 7532676Seschrock if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 7542676Seschrock (void) no_memory(hdl); 7552676Seschrock return (NULL); 7562676Seschrock } 7572676Seschrock 7582676Seschrock if (type == ZFS_TYPE_SNAPSHOT) { 7592676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7603413Smmusante "snapshot properties cannot be modified")); 7612676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 7622676Seschrock goto error; 763789Sahrens } 764789Sahrens 7652676Seschrock elem = NULL; 7662676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 7672676Seschrock propname = nvpair_name(elem); 7682676Seschrock 7692676Seschrock /* 7702676Seschrock * Make sure this property is valid and applies to this type. 7712676Seschrock */ 7723912Slling if ((prop = zfs_name_to_prop_common(propname, type)) 7733912Slling == ZFS_PROP_INVAL) { 7743912Slling isuser = zfs_prop_user(propname); 7753912Slling if (!isuser || (isuser && (type & ZFS_TYPE_POOL))) { 7762676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7772676Seschrock "invalid property '%s'"), 7782676Seschrock propname); 7792676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7802676Seschrock goto error; 7812676Seschrock } else { 7822676Seschrock /* 7832676Seschrock * If this is a user property, make sure it's a 7842676Seschrock * string, and that it's less than 7852676Seschrock * ZAP_MAXNAMELEN. 7862676Seschrock */ 7872676Seschrock if (nvpair_type(elem) != DATA_TYPE_STRING) { 7882676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7892676Seschrock "'%s' must be a string"), 7902676Seschrock propname); 7912676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7922676Seschrock errbuf); 7932676Seschrock goto error; 7942676Seschrock } 7952676Seschrock 7962676Seschrock if (strlen(nvpair_name(elem)) >= 7972676Seschrock ZAP_MAXNAMELEN) { 7982676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7992676Seschrock "property name '%s' is too long"), 8002676Seschrock propname); 8012676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8022676Seschrock errbuf); 8032676Seschrock goto error; 8042676Seschrock } 8052676Seschrock } 8062676Seschrock 8072676Seschrock (void) nvpair_value_string(elem, &strval); 8082676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 8092676Seschrock (void) no_memory(hdl); 8102676Seschrock goto error; 8112676Seschrock } 8122676Seschrock continue; 813789Sahrens } 8142676Seschrock 8152676Seschrock /* 8162676Seschrock * Normalize the name, to get rid of shorthand abbrevations. 8172676Seschrock */ 8182676Seschrock propname = zfs_prop_to_name(prop); 8192676Seschrock 8202676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 8212676Seschrock zfs_error_aux(hdl, 8222676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 8232676Seschrock "apply to datasets of this type"), propname); 8242676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 8252676Seschrock goto error; 8262676Seschrock } 8272676Seschrock 8282676Seschrock if (zfs_prop_readonly(prop) && 8292676Seschrock (prop != ZFS_PROP_VOLBLOCKSIZE || zhp != NULL)) { 8302676Seschrock zfs_error_aux(hdl, 8312676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 8322676Seschrock propname); 8332676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 8342676Seschrock goto error; 8352676Seschrock } 8362676Seschrock 8372676Seschrock /* 8382676Seschrock * Convert any properties to the internal DSL value types. 8392676Seschrock */ 8402676Seschrock strval = NULL; 8412676Seschrock switch (zfs_prop_get_type(prop)) { 8422676Seschrock case prop_type_boolean: 8432676Seschrock if (prop_parse_boolean(hdl, elem, &intval) != 0) { 8442676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8452676Seschrock goto error; 8462676Seschrock } 847789Sahrens break; 8482676Seschrock 8492676Seschrock case prop_type_string: 8502676Seschrock if (nvpair_type(elem) != DATA_TYPE_STRING) { 8512082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8522676Seschrock "'%s' must be a string"), 8532676Seschrock propname); 8542676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8552676Seschrock goto error; 856789Sahrens } 8572676Seschrock (void) nvpair_value_string(elem, &strval); 8582676Seschrock if (strlen(strval) >= ZFS_MAXPROPLEN) { 8592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8602676Seschrock "'%s' is too long"), propname); 8612676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8622676Seschrock goto error; 8632676Seschrock } 8642676Seschrock break; 8652676Seschrock 8662676Seschrock case prop_type_number: 8672676Seschrock if (prop_parse_number(hdl, elem, prop, &intval) != 0) { 8682676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8692676Seschrock goto error; 8702676Seschrock } 8712676Seschrock break; 8722676Seschrock 8732676Seschrock case prop_type_index: 8742676Seschrock if (prop_parse_index(hdl, elem, prop, &intval) != 0) { 8752676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8762676Seschrock goto error; 877789Sahrens } 878789Sahrens break; 879789Sahrens 8802676Seschrock default: 8812676Seschrock abort(); 8822676Seschrock } 8832676Seschrock 8842676Seschrock /* 8852676Seschrock * Add the result to our return set of properties. 8862676Seschrock */ 8872676Seschrock if (strval) { 8882676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 8892676Seschrock (void) no_memory(hdl); 8902676Seschrock goto error; 891789Sahrens } 8922676Seschrock } else if (nvlist_add_uint64(ret, propname, intval) != 0) { 8932676Seschrock (void) no_memory(hdl); 8942676Seschrock goto error; 8952676Seschrock } 8962676Seschrock 8972676Seschrock /* 8982676Seschrock * Perform some additional checks for specific properties. 8992676Seschrock */ 9002676Seschrock switch (prop) { 901*4577Sahrens case ZFS_PROP_VERSION: 902*4577Sahrens { 903*4577Sahrens int version; 904*4577Sahrens 905*4577Sahrens if (zhp == NULL) 906*4577Sahrens break; 907*4577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 908*4577Sahrens if (intval < version) { 909*4577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 910*4577Sahrens "Can not downgrade; already at version %u"), 911*4577Sahrens version); 912*4577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 913*4577Sahrens goto error; 914*4577Sahrens } 915*4577Sahrens break; 916*4577Sahrens } 917*4577Sahrens 9182676Seschrock case ZFS_PROP_RECORDSIZE: 9192676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 9202676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 9212676Seschrock if (intval < SPA_MINBLOCKSIZE || 9222676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 9232082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9242676Seschrock "'%s' must be power of 2 from %u " 9252676Seschrock "to %uk"), propname, 9262676Seschrock (uint_t)SPA_MINBLOCKSIZE, 9272676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 9282676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9292676Seschrock goto error; 930789Sahrens } 931789Sahrens break; 932789Sahrens 9333126Sahl case ZFS_PROP_SHAREISCSI: 9343126Sahl if (strcmp(strval, "off") != 0 && 9353126Sahl strcmp(strval, "on") != 0 && 9363126Sahl strcmp(strval, "type=disk") != 0) { 9373126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9383126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 9393126Sahl propname); 9403126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9413126Sahl goto error; 9423126Sahl } 9433126Sahl 9443126Sahl break; 9453126Sahl 9462676Seschrock case ZFS_PROP_MOUNTPOINT: 9472676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 9482676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 9492676Seschrock break; 9502676Seschrock 9512676Seschrock if (strval[0] != '/') { 9522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9532676Seschrock "'%s' must be an absolute path, " 9542676Seschrock "'none', or 'legacy'"), propname); 9552676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9562676Seschrock goto error; 957789Sahrens } 9583126Sahl /*FALLTHRU*/ 9593126Sahl 9603126Sahl case ZFS_PROP_SHARENFS: 9613126Sahl /* 9623126Sahl * For the mountpoint and sharenfs properties, check if 9633126Sahl * it can be set in a global/non-global zone based on 9643126Sahl * the zoned property value: 9653126Sahl * 9663126Sahl * global zone non-global zone 9673126Sahl * -------------------------------------------------- 9683126Sahl * zoned=on mountpoint (no) mountpoint (yes) 9693126Sahl * sharenfs (no) sharenfs (no) 9703126Sahl * 9713126Sahl * zoned=off mountpoint (yes) N/A 9723126Sahl * sharenfs (yes) 9733126Sahl */ 9742676Seschrock if (zoned) { 9752676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 9762676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9772676Seschrock "'%s' cannot be set on " 9782676Seschrock "dataset in a non-global zone"), 9792676Seschrock propname); 9802676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9812676Seschrock errbuf); 9822676Seschrock goto error; 9832676Seschrock } else if (prop == ZFS_PROP_SHARENFS) { 9842676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9852676Seschrock "'%s' cannot be set in " 9862676Seschrock "a non-global zone"), propname); 9872676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9882676Seschrock errbuf); 9892676Seschrock goto error; 9902676Seschrock } 9912676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 9922676Seschrock /* 9932676Seschrock * If zoned property is 'off', this must be in 9942676Seschrock * a globle zone. If not, something is wrong. 9952676Seschrock */ 9962676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9972676Seschrock "'%s' cannot be set while dataset " 9982676Seschrock "'zoned' property is set"), propname); 9992676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 10002676Seschrock goto error; 10012676Seschrock } 10023126Sahl 10034180Sdougm /* 10044180Sdougm * At this point, it is legitimate to set the 10054180Sdougm * property. Now we want to make sure that the 10064180Sdougm * property value is valid if it is sharenfs. 10074180Sdougm */ 10084180Sdougm if (prop == ZFS_PROP_SHARENFS && 10094217Seschrock strcmp(strval, "on") != 0 && 10104217Seschrock strcmp(strval, "off") != 0) { 10114180Sdougm 10124180Sdougm /* 10134180Sdougm * Must be an NFS option string so 10144180Sdougm * init the libshare in order to 10154180Sdougm * enable the parser and then parse 10164180Sdougm * the options. We use the control API 10174180Sdougm * since we don't care about the 10184180Sdougm * current configuration and don't 10194180Sdougm * want the overhead of loading it 10204180Sdougm * until we actually do something. 10214180Sdougm */ 10224180Sdougm 10234217Seschrock if (zfs_init_libshare(hdl, 10244217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 10254217Seschrock /* 10264217Seschrock * An error occurred so we can't do 10274217Seschrock * anything 10284217Seschrock */ 10294217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10304217Seschrock "'%s' cannot be set: problem " 10314217Seschrock "in share initialization"), 10324217Seschrock propname); 10334217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10344217Seschrock errbuf); 10354217Seschrock goto error; 10364217Seschrock } 10374217Seschrock 10384217Seschrock if (zfs_parse_options(strval, "nfs") != SA_OK) { 10394217Seschrock /* 10404217Seschrock * There was an error in parsing so 10414217Seschrock * deal with it by issuing an error 10424217Seschrock * message and leaving after 10434217Seschrock * uninitializing the the libshare 10444217Seschrock * interface. 10454217Seschrock */ 10464217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10474217Seschrock "'%s' cannot be set to invalid " 10484217Seschrock "options"), propname); 10494217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10504217Seschrock errbuf); 10514217Seschrock zfs_uninit_libshare(hdl); 10524217Seschrock goto error; 10534217Seschrock } 10544180Sdougm zfs_uninit_libshare(hdl); 10554180Sdougm } 10564180Sdougm 10573126Sahl break; 10583912Slling 10594451Seschrock case ZPOOL_PROP_BOOTFS: 10603912Slling /* 10613912Slling * bootfs property value has to be a dataset name and 10623912Slling * the dataset has to be in the same pool as it sets to. 10633912Slling */ 10643912Slling if (strval[0] != '\0' && (!zfs_name_valid(strval, 10653912Slling ZFS_TYPE_FILESYSTEM) || !bootfs_poolname_valid( 10663912Slling pool_name, strval))) { 10673912Slling 10683912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 10693912Slling "is an invalid name"), strval); 10703912Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 10713912Slling goto error; 10723912Slling } 10733912Slling break; 10742676Seschrock } 10752676Seschrock 10762676Seschrock /* 10772676Seschrock * For changes to existing volumes, we have some additional 10782676Seschrock * checks to enforce. 10792676Seschrock */ 10802676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 10812676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 10822676Seschrock ZFS_PROP_VOLSIZE); 10832676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 10842676Seschrock ZFS_PROP_VOLBLOCKSIZE); 10852676Seschrock char buf[64]; 10862676Seschrock 10872676Seschrock switch (prop) { 10882676Seschrock case ZFS_PROP_RESERVATION: 10892676Seschrock if (intval > volsize) { 10902676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10912676Seschrock "'%s' is greater than current " 10922676Seschrock "volume size"), propname); 10932676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10942676Seschrock errbuf); 10952676Seschrock goto error; 10962676Seschrock } 10972676Seschrock break; 10982676Seschrock 10992676Seschrock case ZFS_PROP_VOLSIZE: 11002676Seschrock if (intval % blocksize != 0) { 11012676Seschrock zfs_nicenum(blocksize, buf, 11022676Seschrock sizeof (buf)); 11032676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11042676Seschrock "'%s' must be a multiple of " 11052676Seschrock "volume block size (%s)"), 11062676Seschrock propname, buf); 11072676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 11082676Seschrock errbuf); 11092676Seschrock goto error; 11102676Seschrock } 11112676Seschrock 11122676Seschrock if (intval == 0) { 11132676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11142676Seschrock "'%s' cannot be zero"), 11152676Seschrock propname); 11162676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 11172676Seschrock errbuf); 11182676Seschrock goto error; 1119789Sahrens } 11203126Sahl break; 1121789Sahrens } 1122789Sahrens } 1123789Sahrens } 1124789Sahrens 11252676Seschrock /* 11262676Seschrock * If this is an existing volume, and someone is setting the volsize, 11272676Seschrock * make sure that it matches the reservation, or add it if necessary. 11282676Seschrock */ 11292676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 11302676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 11312676Seschrock &intval) == 0) { 11322676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 11332676Seschrock ZFS_PROP_VOLSIZE); 11342676Seschrock uint64_t old_reservation = zfs_prop_get_int(zhp, 11352676Seschrock ZFS_PROP_RESERVATION); 11362676Seschrock uint64_t new_reservation; 11372676Seschrock 11382676Seschrock if (old_volsize == old_reservation && 11392676Seschrock nvlist_lookup_uint64(ret, 11402676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 11412676Seschrock &new_reservation) != 0) { 11422676Seschrock if (nvlist_add_uint64(ret, 11432676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 11442676Seschrock intval) != 0) { 11452676Seschrock (void) no_memory(hdl); 11462676Seschrock goto error; 11472676Seschrock } 11482676Seschrock } 11492676Seschrock } 11502676Seschrock 11512676Seschrock return (ret); 11522676Seschrock 11532676Seschrock error: 11542676Seschrock nvlist_free(ret); 11552676Seschrock return (NULL); 1156789Sahrens } 1157789Sahrens 11584543Smarks static int 11594543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 11604543Smarks uint64_t *ret_who) 11614543Smarks { 11624543Smarks struct passwd *pwd; 11634543Smarks struct group *grp; 11644543Smarks uid_t id; 11654543Smarks 11664543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 11674543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 11684543Smarks *ret_who = -1; 11694543Smarks return (0); 11704543Smarks } 11714543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 11724543Smarks return (EZFS_BADWHO); 11734543Smarks 11744543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 11754543Smarks strcmp(who, "everyone") == 0) { 11764543Smarks *ret_who = -1; 11774543Smarks *who_type = ZFS_DELEG_EVERYONE; 11784543Smarks return (0); 11794543Smarks } 11804543Smarks 11814543Smarks pwd = getpwnam(who); 11824543Smarks grp = getgrnam(who); 11834543Smarks 11844543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 11854543Smarks *ret_who = pwd->pw_uid; 11864543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 11874543Smarks *ret_who = grp->gr_gid; 11884543Smarks } else if (pwd) { 11894543Smarks *ret_who = pwd->pw_uid; 11904543Smarks *who_type = ZFS_DELEG_USER; 11914543Smarks } else if (grp) { 11924543Smarks *ret_who = grp->gr_gid; 11934543Smarks *who_type = ZFS_DELEG_GROUP; 11944543Smarks } else { 11954543Smarks char *end; 11964543Smarks 11974543Smarks id = strtol(who, &end, 10); 11984543Smarks if (errno != 0 || *end != '\0') { 11994543Smarks return (EZFS_BADWHO); 12004543Smarks } else { 12014543Smarks *ret_who = id; 12024543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 12034543Smarks *who_type = ZFS_DELEG_USER; 12044543Smarks } 12054543Smarks } 12064543Smarks 12074543Smarks return (0); 12084543Smarks } 12094543Smarks 12104543Smarks static void 12114543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 12124543Smarks { 12134543Smarks if (perms_nvp != NULL) { 12144543Smarks verify(nvlist_add_nvlist(who_nvp, 12154543Smarks name, perms_nvp) == 0); 12164543Smarks } else { 12174543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 12184543Smarks } 12194543Smarks } 12204543Smarks 12214543Smarks static void 12224543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 12234543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 12244543Smarks nvlist_t *sets_nvp) 12254543Smarks { 12264543Smarks boolean_t do_perms, do_sets; 12274543Smarks char name[ZFS_MAX_DELEG_NAME]; 12284543Smarks 12294543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 12304543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 12314543Smarks 12324543Smarks if (!do_perms && !do_sets) 12334543Smarks do_perms = do_sets = B_TRUE; 12344543Smarks 12354543Smarks if (do_perms) { 12364543Smarks zfs_deleg_whokey(name, who_type, inherit, 12374543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 12384543Smarks whostr : (void *)&whoid); 12394543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 12404543Smarks } 12414543Smarks if (do_sets) { 12424543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 12434543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 12444543Smarks whostr : (void *)&whoid); 12454543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 12464543Smarks } 12474543Smarks } 12484543Smarks 12494543Smarks static void 12504543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 12514543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 12524543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 12534543Smarks { 12544543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 12554543Smarks helper(who_type, whoid, whostr, 0, 12564543Smarks who_nvp, perms_nvp, sets_nvp); 12574543Smarks } else { 12584543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 12594543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 12604543Smarks who_nvp, perms_nvp, sets_nvp); 12614543Smarks } 12624543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 12634543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 12644543Smarks who_nvp, perms_nvp, sets_nvp); 12654543Smarks } 12664543Smarks } 12674543Smarks } 12684543Smarks 12694543Smarks /* 12704543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 12714543Smarks * 12724543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 12734543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 12744543Smarks * base attribute named stored in the dsl. 12754543Smarks * Arguments: 12764543Smarks * 12774543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 12784543Smarks * whostr may be null for everyone or create perms. 12794543Smarks * who_type: is the type of entry in whostr. Typically this will be 12804543Smarks * ZFS_DELEG_WHO_UNKNOWN. 12814543Smarks * perms: comman separated list of permissions. May be null if user 12824543Smarks * is requested to remove permissions by who. 12834543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 12844543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 12854543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 12864543Smarks * The output nvp will look something like this. 12874543Smarks * ul$1234 -> {create ; destroy } 12884543Smarks * Ul$1234 -> { @myset } 12894543Smarks * s-$@myset - { snapshot; checksum; compression } 12904543Smarks */ 12914543Smarks int 12924543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 12934543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 12944543Smarks { 12954543Smarks nvlist_t *who_nvp; 12964543Smarks nvlist_t *perms_nvp = NULL; 12974543Smarks nvlist_t *sets_nvp = NULL; 12984543Smarks char errbuf[1024]; 12994543Smarks char *who_tok; 13004543Smarks int error; 13014543Smarks 13024543Smarks *nvp = NULL; 13034543Smarks 13044543Smarks if (perms) { 13054543Smarks /* Make sure permission string doesn't have an '=' sign in it */ 13064543Smarks if (strchr(perms, '=') != NULL) { 13074543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13084543Smarks dgettext(TEXT_DOMAIN, 13094543Smarks "permissions can't contain equal sign : '%s'"), 13104543Smarks perms); 13114543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, errbuf)); 13124543Smarks } 13134543Smarks 13144543Smarks if ((error = nvlist_alloc(&perms_nvp, 13154543Smarks NV_UNIQUE_NAME, 0)) != 0) { 13164543Smarks return (1); 13174543Smarks } 13184543Smarks if ((error = nvlist_alloc(&sets_nvp, 13194543Smarks NV_UNIQUE_NAME, 0)) != 0) { 13204543Smarks nvlist_free(perms_nvp); 13214543Smarks return (1); 13224543Smarks } 13234543Smarks } 13244543Smarks 13254543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 13264543Smarks if (perms_nvp) 13274543Smarks nvlist_free(perms_nvp); 13284543Smarks if (sets_nvp) 13294543Smarks nvlist_free(sets_nvp); 13304543Smarks return (1); 13314543Smarks } 13324543Smarks 13334543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 13344543Smarks namecheck_err_t why; 13354543Smarks char what; 13364543Smarks 13374543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 13384543Smarks switch (why) { 13394543Smarks case NAME_ERR_NO_AT: 13404543Smarks zfs_error_aux(zhp->zfs_hdl, 13414543Smarks dgettext(TEXT_DOMAIN, 13424543Smarks "set definition must begin with an '@' " 13434543Smarks "character")); 13444543Smarks } 13454543Smarks return (zfs_error(zhp->zfs_hdl, 13464543Smarks EZFS_BADPERMSET, whostr)); 13474543Smarks } 13484543Smarks } 13494543Smarks 13504543Smarks /* 13514543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 13524543Smarks * The first nvlist perms_nvp will have normal permissions and the 13534543Smarks * other sets_nvp will have only permssion set names in it. 13544543Smarks */ 13554543Smarks 13564543Smarks 13574543Smarks while (perms && *perms != '\0') { 13584543Smarks char *value; 13594543Smarks char *perm_name; 13604543Smarks nvlist_t *update_nvp; 13614543Smarks int perm_num; 13624543Smarks char canonical_name[64]; 13634543Smarks char *canonicalp = canonical_name; 13644543Smarks 13654543Smarks 13664543Smarks update_nvp = perms_nvp; 13674543Smarks 13684543Smarks perm_num = getsubopt(&perms, zfs_deleg_perm_tab, &value); 13694543Smarks if (perm_num == -1) { 13704543Smarks zfs_prop_t prop; 13714543Smarks 13724543Smarks prop = zfs_name_to_prop(value); 13734543Smarks if (prop != ZFS_PROP_INVAL) { 13744543Smarks (void) snprintf(canonical_name, 13754543Smarks sizeof (canonical_name), "%s", 13764543Smarks zfs_prop_to_name(prop)); 13774543Smarks perm_num = getsubopt(&canonicalp, 13784543Smarks zfs_deleg_perm_tab, &value); 13794543Smarks } 13804543Smarks } 13814543Smarks if (perm_num != -1) { 13824543Smarks perm_name = zfs_deleg_perm_tab[perm_num]; 13834543Smarks } else { /* check and see if permission is a named set */ 13844543Smarks if (value[0] == '@') { 13854543Smarks 13864543Smarks /* 13874543Smarks * make sure permssion set isn't defined 13884543Smarks * in terms of itself. ie. 13894543Smarks * @set1 = create,destroy,@set1 13904543Smarks */ 13914543Smarks if (who_type == ZFS_DELEG_NAMED_SET && 13924543Smarks strcmp(value, whostr) == 0) { 13934543Smarks nvlist_free(who_nvp); 13944543Smarks nvlist_free(perms_nvp); 13954543Smarks if (sets_nvp) 13964543Smarks nvlist_free(sets_nvp); 13974543Smarks (void) snprintf(errbuf, 13984543Smarks sizeof (errbuf), 13994543Smarks dgettext(TEXT_DOMAIN, 14004543Smarks "Invalid permission %s"), value); 14014543Smarks return (zfs_error(zhp->zfs_hdl, 14024543Smarks EZFS_PERMSET_CIRCULAR, errbuf)); 14034543Smarks } 14044543Smarks update_nvp = sets_nvp; 14054543Smarks perm_name = value; 14064543Smarks } else { 14074543Smarks nvlist_free(who_nvp); 14084543Smarks nvlist_free(perms_nvp); 14094543Smarks if (sets_nvp) 14104543Smarks nvlist_free(sets_nvp); 14114543Smarks return (zfs_error(zhp->zfs_hdl, 14124543Smarks EZFS_BADPERM, value)); 14134543Smarks } 14144543Smarks } 14154543Smarks verify(nvlist_add_boolean(update_nvp, perm_name) == 0); 14164543Smarks } 14174543Smarks 14184543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 14194543Smarks who_tok = strtok(whostr, ","); 14204543Smarks if (who_tok == NULL) { 14214543Smarks nvlist_free(who_nvp); 14224543Smarks nvlist_free(perms_nvp); 14234543Smarks if (sets_nvp) 14244543Smarks nvlist_free(sets_nvp); 14254543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14264543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 14274543Smarks whostr); 14284543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 14294543Smarks } 14304543Smarks } 14314543Smarks 14324543Smarks /* 14334543Smarks * Now create the nvlist(s) 14344543Smarks */ 14354543Smarks do { 14364543Smarks uint64_t who_id; 14374543Smarks 14384543Smarks error = zfs_get_perm_who(who_tok, &who_type, 14394543Smarks &who_id); 14404543Smarks if (error) { 14414543Smarks nvlist_free(who_nvp); 14424543Smarks nvlist_free(perms_nvp); 14434543Smarks if (sets_nvp) 14444543Smarks nvlist_free(sets_nvp); 14454543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14464543Smarks dgettext(TEXT_DOMAIN, 14474543Smarks "Unable to determine uid/gid for " 14484543Smarks "%s "), who_tok); 14494543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 14504543Smarks } 14514543Smarks 14524543Smarks /* 14534543Smarks * add entries for both local and descendent when required 14544543Smarks */ 14554543Smarks 14564543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 14574543Smarks perms_nvp, sets_nvp, who_type, inherit); 14584543Smarks 14594543Smarks } while (who_tok = strtok(NULL, ",")); 14604543Smarks *nvp = who_nvp; 14614543Smarks return (0); 14624543Smarks } 14634543Smarks 14644543Smarks static int 14654543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 14664543Smarks { 14674543Smarks zfs_cmd_t zc = { 0 }; 14684543Smarks int error; 14694543Smarks size_t sz; 14704543Smarks char errbuf[1024]; 14714543Smarks 14724543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14734543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 14744543Smarks zhp->zfs_name); 14754543Smarks 14764543Smarks if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp, &sz)) 14774543Smarks return (-1); 14784543Smarks 14794543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 14804543Smarks zc.zc_perm_action = unset; 14814543Smarks 14824543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 14834543Smarks if (error && errno == ENOTSUP) { 14844543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14854543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 14864543Smarks zcmd_free_nvlists(&zc); 14874543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 14884543Smarks } else if (error) { 14894543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 14904543Smarks } 14914543Smarks zcmd_free_nvlists(&zc); 14924543Smarks 14934543Smarks return (error); 14944543Smarks } 14954543Smarks 14964543Smarks int 14974543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 14984543Smarks { 14994543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 15004543Smarks } 15014543Smarks 15024543Smarks int 15034543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 15044543Smarks { 15054543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 15064543Smarks } 15074543Smarks 15084543Smarks static int 15094543Smarks perm_compare(const void *arg1, const void *arg2) 15104543Smarks { 15114543Smarks const zfs_perm_node_t *node1 = arg1; 15124543Smarks const zfs_perm_node_t *node2 = arg2; 15134543Smarks int ret; 15144543Smarks 15154543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 15164543Smarks 15174543Smarks if (ret > 0) 15184543Smarks return (1); 15194543Smarks if (ret < 0) 15204543Smarks return (-1); 15214543Smarks else 15224543Smarks return (0); 15234543Smarks } 15244543Smarks 15254543Smarks static void 15264543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 15274543Smarks { 15284543Smarks zfs_perm_node_t *permnode; 15294543Smarks void *cookie; 15304543Smarks 15314543Smarks cookie = NULL; 15324543Smarks while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) { 15334543Smarks avl_remove(tree, permnode); 15344543Smarks free(permnode); 15354543Smarks } 15364543Smarks } 15374543Smarks 15384543Smarks static void 15394543Smarks zfs_destroy_tree(avl_tree_t *tree) 15404543Smarks { 15414543Smarks zfs_allow_node_t *allownode; 15424543Smarks void *cookie; 15434543Smarks 15444543Smarks cookie = NULL; 15454543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 15464543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 15474543Smarks zfs_destroy_perm_tree(&allownode->z_local); 15484543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 15494543Smarks avl_remove(tree, allownode); 15504543Smarks free(allownode); 15514543Smarks } 15524543Smarks } 15534543Smarks 15544543Smarks void 15554543Smarks zfs_free_allows(zfs_allow_t *allow) 15564543Smarks { 15574543Smarks zfs_allow_t *allownext; 15584543Smarks zfs_allow_t *freeallow; 15594543Smarks 15604543Smarks allownext = allow; 15614543Smarks while (allownext) { 15624543Smarks zfs_destroy_tree(&allownext->z_sets); 15634543Smarks zfs_destroy_tree(&allownext->z_crperms); 15644543Smarks zfs_destroy_tree(&allownext->z_user); 15654543Smarks zfs_destroy_tree(&allownext->z_group); 15664543Smarks zfs_destroy_tree(&allownext->z_everyone); 15674543Smarks freeallow = allownext; 15684543Smarks allownext = allownext->z_next; 15694543Smarks free(freeallow); 15704543Smarks } 15714543Smarks } 15724543Smarks 15734543Smarks static zfs_allow_t * 15744543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 15754543Smarks { 15764543Smarks zfs_allow_t *ptree; 15774543Smarks 15784543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 15794543Smarks sizeof (zfs_allow_t))) == NULL) { 15804543Smarks return (NULL); 15814543Smarks } 15824543Smarks 15834543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 15844543Smarks avl_create(&ptree->z_sets, 15854543Smarks perm_compare, sizeof (zfs_allow_node_t), 15864543Smarks offsetof(zfs_allow_node_t, z_node)); 15874543Smarks avl_create(&ptree->z_crperms, 15884543Smarks perm_compare, sizeof (zfs_allow_node_t), 15894543Smarks offsetof(zfs_allow_node_t, z_node)); 15904543Smarks avl_create(&ptree->z_user, 15914543Smarks perm_compare, sizeof (zfs_allow_node_t), 15924543Smarks offsetof(zfs_allow_node_t, z_node)); 15934543Smarks avl_create(&ptree->z_group, 15944543Smarks perm_compare, sizeof (zfs_allow_node_t), 15954543Smarks offsetof(zfs_allow_node_t, z_node)); 15964543Smarks avl_create(&ptree->z_everyone, 15974543Smarks perm_compare, sizeof (zfs_allow_node_t), 15984543Smarks offsetof(zfs_allow_node_t, z_node)); 15994543Smarks 16004543Smarks if (prev) 16014543Smarks prev->z_next = ptree; 16024543Smarks ptree->z_next = NULL; 16034543Smarks return (ptree); 16044543Smarks } 16054543Smarks 16064543Smarks /* 16074543Smarks * Add permissions to the appropriate AVL permission tree. 16084543Smarks * The appropriate tree may not be the requested tree. 16094543Smarks * For example if ld indicates a local permission, but 16104543Smarks * same permission also exists as a descendent permission 16114543Smarks * then the permission will be removed from the descendent 16124543Smarks * tree and add the the local+descendent tree. 16134543Smarks */ 16144543Smarks static int 16154543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 16164543Smarks char *perm, char ld) 16174543Smarks { 16184543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 16194543Smarks zfs_perm_node_t *newnode; 16204543Smarks avl_index_t where, where2; 16214543Smarks avl_tree_t *tree, *altree; 16224543Smarks 16234543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 16244543Smarks 16254543Smarks if (ld == ZFS_DELEG_NA) { 16264543Smarks tree = &allownode->z_localdescend; 16274543Smarks altree = &allownode->z_descend; 16284543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 16294543Smarks tree = &allownode->z_local; 16304543Smarks altree = &allownode->z_descend; 16314543Smarks } else { 16324543Smarks tree = &allownode->z_descend; 16334543Smarks altree = &allownode->z_local; 16344543Smarks } 16354543Smarks permnode = avl_find(tree, &pnode, &where); 16364543Smarks permnode2 = avl_find(altree, &pnode, &where2); 16374543Smarks 16384543Smarks if (permnode2) { 16394543Smarks avl_remove(altree, permnode2); 16404543Smarks free(permnode2); 16414543Smarks if (permnode == NULL) { 16424543Smarks tree = &allownode->z_localdescend; 16434543Smarks } 16444543Smarks } 16454543Smarks 16464543Smarks /* 16474543Smarks * Now insert new permission in either requested location 16484543Smarks * local/descendent or into ld when perm will exist in both. 16494543Smarks */ 16504543Smarks if (permnode == NULL) { 16514543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 16524543Smarks sizeof (zfs_perm_node_t))) == NULL) { 16534543Smarks return (-1); 16544543Smarks } 16554543Smarks *newnode = pnode; 16564543Smarks avl_add(tree, newnode); 16574543Smarks } 16584543Smarks return (0); 16594543Smarks } 1660*4577Sahrens 16614543Smarks /* 16624543Smarks * Uggh, this is going to be a bit complicated. 16634543Smarks * we have an nvlist coming out of the kernel that 16644543Smarks * will indicate where the permission is set and then 16654543Smarks * it will contain allow of the various "who's", and what 16664543Smarks * their permissions are. To further complicate this 16674543Smarks * we will then have to coalesce the local,descendent 16684543Smarks * and local+descendent permissions where appropriate. 16694543Smarks * The kernel only knows about a permission as being local 16704543Smarks * or descendent, but not both. 16714543Smarks * 16724543Smarks * In order to make this easier for zfs_main to deal with 16734543Smarks * a series of AVL trees will be used to maintain 16744543Smarks * all of this, primarily for sorting purposes as well 16754543Smarks * as the ability to quickly locate a specific entry. 16764543Smarks * 16774543Smarks * What we end up with are tree's for sets, create perms, 16784543Smarks * user, groups and everyone. With each of those trees 16794543Smarks * we have subtrees for local, descendent and local+descendent 16804543Smarks * permissions. 16814543Smarks */ 16824543Smarks int 16834543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 16844543Smarks { 16854543Smarks zfs_cmd_t zc = { 0 }; 16864543Smarks int error; 16874543Smarks nvlist_t *nvlist; 16884543Smarks nvlist_t *permnv, *sourcenv; 16894543Smarks nvpair_t *who_pair, *source_pair; 16904543Smarks nvpair_t *perm_pair; 16914543Smarks char errbuf[1024]; 16924543Smarks zfs_allow_t *zallowp, *newallowp; 16934543Smarks char ld; 16944543Smarks char *nvpname; 16954543Smarks uid_t uid; 16964543Smarks gid_t gid; 16974543Smarks avl_tree_t *tree; 16984543Smarks avl_index_t where; 16994543Smarks 17004543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17014543Smarks 17024543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 17034543Smarks return (-1); 17044543Smarks 17054543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 17064543Smarks if (errno == ENOMEM) { 17074543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 17084543Smarks zcmd_free_nvlists(&zc); 17094543Smarks return (-1); 17104543Smarks } 17114543Smarks } else if (errno == ENOTSUP) { 17124543Smarks zcmd_free_nvlists(&zc); 17134543Smarks (void) snprintf(errbuf, sizeof (errbuf), 17144543Smarks gettext("Pool must be upgraded to use 'allow'")); 17154543Smarks return (zfs_error(zhp->zfs_hdl, 17164543Smarks EZFS_BADVERSION, errbuf)); 17174543Smarks } else { 17184543Smarks zcmd_free_nvlists(&zc); 17194543Smarks return (-1); 17204543Smarks } 17214543Smarks } 17224543Smarks 17234543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 17244543Smarks zcmd_free_nvlists(&zc); 17254543Smarks return (-1); 17264543Smarks } 17274543Smarks 17284543Smarks zcmd_free_nvlists(&zc); 17294543Smarks 17304543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 17314543Smarks 17324543Smarks if (source_pair == NULL) { 17334543Smarks *zfs_perms = NULL; 17344543Smarks return (0); 17354543Smarks } 17364543Smarks 17374543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 17384543Smarks if (*zfs_perms == NULL) { 17394543Smarks return (0); 17404543Smarks } 17414543Smarks 17424543Smarks zallowp = *zfs_perms; 17434543Smarks 17444543Smarks for (;;) { 17454543Smarks struct passwd *pwd; 17464543Smarks struct group *grp; 17474543Smarks zfs_allow_node_t *allownode; 17484543Smarks zfs_allow_node_t findallownode; 17494543Smarks zfs_allow_node_t *newallownode; 17504543Smarks 17514543Smarks (void) strlcpy(zallowp->z_setpoint, 17524543Smarks nvpair_name(source_pair), 17534543Smarks sizeof (zallowp->z_setpoint)); 17544543Smarks 17554543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 17564543Smarks goto abort; 17574543Smarks 17584543Smarks /* 17594543Smarks * Make sure nvlist is composed correctly 17604543Smarks */ 17614543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 17624543Smarks goto abort; 17634543Smarks } 17644543Smarks 17654543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 17664543Smarks if (who_pair == NULL) { 17674543Smarks goto abort; 17684543Smarks } 17694543Smarks 17704543Smarks do { 17714543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 17724543Smarks if (error) { 17734543Smarks goto abort; 17744543Smarks } 17754543Smarks 17764543Smarks /* 17774543Smarks * First build up the key to use 17784543Smarks * for looking up in the various 17794543Smarks * who trees. 17804543Smarks */ 17814543Smarks ld = nvpair_name(who_pair)[1]; 17824543Smarks nvpname = nvpair_name(who_pair); 17834543Smarks switch (nvpair_name(who_pair)[0]) { 17844543Smarks case ZFS_DELEG_USER: 17854543Smarks case ZFS_DELEG_USER_SETS: 17864543Smarks tree = &zallowp->z_user; 17874543Smarks uid = atol(&nvpname[3]); 17884543Smarks pwd = getpwuid(uid); 17894543Smarks (void) snprintf(findallownode.z_key, 17904543Smarks sizeof (findallownode.z_key), "user %s", 17914543Smarks (pwd) ? pwd->pw_name : 17924543Smarks &nvpair_name(who_pair)[3]); 17934543Smarks break; 17944543Smarks case ZFS_DELEG_GROUP: 17954543Smarks case ZFS_DELEG_GROUP_SETS: 17964543Smarks tree = &zallowp->z_group; 17974543Smarks gid = atol(&nvpname[3]); 17984543Smarks grp = getgrgid(gid); 17994543Smarks (void) snprintf(findallownode.z_key, 18004543Smarks sizeof (findallownode.z_key), "group %s", 18014543Smarks (grp) ? grp->gr_name : 18024543Smarks &nvpair_name(who_pair)[3]); 18034543Smarks break; 18044543Smarks case ZFS_DELEG_CREATE: 18054543Smarks case ZFS_DELEG_CREATE_SETS: 18064543Smarks tree = &zallowp->z_crperms; 18074543Smarks (void) strlcpy(findallownode.z_key, "", 18084543Smarks sizeof (findallownode.z_key)); 18094543Smarks break; 18104543Smarks case ZFS_DELEG_EVERYONE: 18114543Smarks case ZFS_DELEG_EVERYONE_SETS: 18124543Smarks (void) snprintf(findallownode.z_key, 18134543Smarks sizeof (findallownode.z_key), "everyone"); 18144543Smarks tree = &zallowp->z_everyone; 18154543Smarks break; 18164543Smarks case ZFS_DELEG_NAMED_SET: 18174543Smarks case ZFS_DELEG_NAMED_SET_SETS: 18184543Smarks (void) snprintf(findallownode.z_key, 18194543Smarks sizeof (findallownode.z_key), "%s", 18204543Smarks &nvpair_name(who_pair)[3]); 18214543Smarks tree = &zallowp->z_sets; 18224543Smarks break; 18234543Smarks } 18244543Smarks 18254543Smarks /* 18264543Smarks * Place who in tree 18274543Smarks */ 18284543Smarks allownode = avl_find(tree, &findallownode, &where); 18294543Smarks if (allownode == NULL) { 18304543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 18314543Smarks sizeof (zfs_allow_node_t))) == NULL) { 18324543Smarks goto abort; 18334543Smarks } 18344543Smarks avl_create(&newallownode->z_localdescend, 18354543Smarks perm_compare, 18364543Smarks sizeof (zfs_perm_node_t), 18374543Smarks offsetof(zfs_perm_node_t, z_node)); 18384543Smarks avl_create(&newallownode->z_local, 18394543Smarks perm_compare, 18404543Smarks sizeof (zfs_perm_node_t), 18414543Smarks offsetof(zfs_perm_node_t, z_node)); 18424543Smarks avl_create(&newallownode->z_descend, 18434543Smarks perm_compare, 18444543Smarks sizeof (zfs_perm_node_t), 18454543Smarks offsetof(zfs_perm_node_t, z_node)); 18464543Smarks (void) strlcpy(newallownode->z_key, 18474543Smarks findallownode.z_key, 18484543Smarks sizeof (findallownode.z_key)); 18494543Smarks avl_insert(tree, newallownode, where); 18504543Smarks allownode = newallownode; 18514543Smarks } 18524543Smarks 18534543Smarks /* 18544543Smarks * Now iterate over the permissions and 18554543Smarks * place them in the appropriate local, 18564543Smarks * descendent or local+descendent tree. 18574543Smarks * 18584543Smarks * The permissions are added to the tree 18594543Smarks * via zfs_coalesce_perm(). 18604543Smarks */ 18614543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 18624543Smarks if (perm_pair == NULL) 18634543Smarks goto abort; 18644543Smarks do { 18654543Smarks if (zfs_coalesce_perm(zhp, allownode, 18664543Smarks nvpair_name(perm_pair), ld) != 0) 18674543Smarks goto abort; 18684543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 18694543Smarks perm_pair)); 18704543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 18714543Smarks 18724543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 18734543Smarks if (source_pair == NULL) 18744543Smarks break; 18754543Smarks 18764543Smarks /* 18774543Smarks * allocate another node from the link list of 18784543Smarks * zfs_allow_t structures 18794543Smarks */ 18804543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 18814543Smarks nvpair_name(source_pair)); 18824543Smarks if (newallowp == NULL) { 18834543Smarks goto abort; 18844543Smarks } 18854543Smarks zallowp = newallowp; 18864543Smarks } 18874543Smarks nvlist_free(nvlist); 18884543Smarks return (0); 18894543Smarks abort: 18904543Smarks zfs_free_allows(*zfs_perms); 18914543Smarks nvlist_free(nvlist); 18924543Smarks return (-1); 18934543Smarks } 18944543Smarks 1895789Sahrens /* 1896789Sahrens * Given a property name and value, set the property for the given dataset. 1897789Sahrens */ 1898789Sahrens int 18992676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1900789Sahrens { 1901789Sahrens zfs_cmd_t zc = { 0 }; 19022676Seschrock int ret = -1; 19032676Seschrock prop_changelist_t *cl = NULL; 19042082Seschrock char errbuf[1024]; 19052082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 19062676Seschrock nvlist_t *nvl = NULL, *realprops; 19072676Seschrock zfs_prop_t prop; 19082082Seschrock 19092082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 19102676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 19112082Seschrock zhp->zfs_name); 19122082Seschrock 19132676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 19142676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 19152676Seschrock (void) no_memory(hdl); 19162676Seschrock goto error; 1917789Sahrens } 1918789Sahrens 19193912Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, NULL, nvl, 19202676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 19212676Seschrock goto error; 19222676Seschrock nvlist_free(nvl); 19232676Seschrock nvl = realprops; 19242676Seschrock 19252676Seschrock prop = zfs_name_to_prop(propname); 19262676Seschrock 1927789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 19282676Seschrock goto error; 1929789Sahrens 1930789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 19312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19322082Seschrock "child dataset with inherited mountpoint is used " 19332082Seschrock "in a non-global zone")); 19342082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1935789Sahrens goto error; 1936789Sahrens } 1937789Sahrens 1938789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1939789Sahrens goto error; 1940789Sahrens 1941789Sahrens /* 1942789Sahrens * Execute the corresponding ioctl() to set this property. 1943789Sahrens */ 1944789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1945789Sahrens 19462676Seschrock if (zcmd_write_src_nvlist(hdl, &zc, nvl, NULL) != 0) 19472676Seschrock goto error; 19482676Seschrock 19494543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1950789Sahrens 1951789Sahrens if (ret != 0) { 1952789Sahrens switch (errno) { 1953789Sahrens 1954789Sahrens case ENOSPC: 1955789Sahrens /* 1956789Sahrens * For quotas and reservations, ENOSPC indicates 1957789Sahrens * something different; setting a quota or reservation 1958789Sahrens * doesn't use any disk space. 1959789Sahrens */ 1960789Sahrens switch (prop) { 1961789Sahrens case ZFS_PROP_QUOTA: 19622082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19632082Seschrock "size is less than current used or " 19642082Seschrock "reserved space")); 19652082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1966789Sahrens break; 1967789Sahrens 1968789Sahrens case ZFS_PROP_RESERVATION: 19692082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19702082Seschrock "size is greater than available space")); 19712082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1972789Sahrens break; 1973789Sahrens 1974789Sahrens default: 19752082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1976789Sahrens break; 1977789Sahrens } 1978789Sahrens break; 1979789Sahrens 1980789Sahrens case EBUSY: 19812082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 19822082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 19832082Seschrock else 19842676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1985789Sahrens break; 1986789Sahrens 19871175Slling case EROFS: 19882082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 19891175Slling break; 19901175Slling 19913886Sahl case ENOTSUP: 19923886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19933886Sahl "pool must be upgraded to allow gzip compression")); 19943886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 19953886Sahl break; 19963886Sahl 1997789Sahrens case EOVERFLOW: 1998789Sahrens /* 1999789Sahrens * This platform can't address a volume this big. 2000789Sahrens */ 2001789Sahrens #ifdef _ILP32 2002789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 20032082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 2004789Sahrens break; 2005789Sahrens } 2006789Sahrens #endif 20072082Seschrock /* FALLTHROUGH */ 2008789Sahrens default: 20092082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 2010789Sahrens } 2011789Sahrens } else { 2012789Sahrens /* 2013789Sahrens * Refresh the statistics so the new property value 2014789Sahrens * is reflected. 2015789Sahrens */ 20162676Seschrock if ((ret = changelist_postfix(cl)) == 0) 20172676Seschrock (void) get_stats(zhp); 2018789Sahrens } 2019789Sahrens 2020789Sahrens error: 20212676Seschrock nvlist_free(nvl); 20222676Seschrock zcmd_free_nvlists(&zc); 20232676Seschrock if (cl) 20242676Seschrock changelist_free(cl); 2025789Sahrens return (ret); 2026789Sahrens } 2027789Sahrens 2028789Sahrens /* 2029789Sahrens * Given a property, inherit the value from the parent dataset. 2030789Sahrens */ 2031789Sahrens int 20322676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 2033789Sahrens { 2034789Sahrens zfs_cmd_t zc = { 0 }; 2035789Sahrens int ret; 2036789Sahrens prop_changelist_t *cl; 20372082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 20382082Seschrock char errbuf[1024]; 20392676Seschrock zfs_prop_t prop; 20402082Seschrock 20412082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 20422082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 2043789Sahrens 20442676Seschrock if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) { 20452676Seschrock /* 20462676Seschrock * For user properties, the amount of work we have to do is very 20472676Seschrock * small, so just do it here. 20482676Seschrock */ 20492676Seschrock if (!zfs_prop_user(propname)) { 20502676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20512676Seschrock "invalid property")); 20522676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 20532676Seschrock } 20542676Seschrock 20552676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 20562676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 20572676Seschrock 20584543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc) != 0) 20592676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 20602676Seschrock 20612676Seschrock return (0); 20622676Seschrock } 20632676Seschrock 2064789Sahrens /* 2065789Sahrens * Verify that this property is inheritable. 2066789Sahrens */ 20672082Seschrock if (zfs_prop_readonly(prop)) 20682082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 20692082Seschrock 20702082Seschrock if (!zfs_prop_inheritable(prop)) 20712082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2072789Sahrens 2073789Sahrens /* 2074789Sahrens * Check to see if the value applies to this type 2075789Sahrens */ 20762082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 20772082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2078789Sahrens 20793443Srm160521 /* 20803443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 20813443Srm160521 */ 20823443Srm160521 propname = zfs_prop_to_name(prop); 2083789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 20842676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2085789Sahrens 2086789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2087789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 20882082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20892082Seschrock "dataset is used in a non-global zone")); 20902082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2091789Sahrens } 2092789Sahrens 2093789Sahrens /* 2094789Sahrens * Determine datasets which will be affected by this change, if any. 2095789Sahrens */ 2096789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 2097789Sahrens return (-1); 2098789Sahrens 2099789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 21002082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21012082Seschrock "child dataset with inherited mountpoint is used " 21022082Seschrock "in a non-global zone")); 21032082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2104789Sahrens goto error; 2105789Sahrens } 2106789Sahrens 2107789Sahrens if ((ret = changelist_prefix(cl)) != 0) 2108789Sahrens goto error; 2109789Sahrens 21104543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc)) != 0) { 21112082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2112789Sahrens } else { 2113789Sahrens 21142169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 2115789Sahrens goto error; 2116789Sahrens 2117789Sahrens /* 2118789Sahrens * Refresh the statistics so the new property is reflected. 2119789Sahrens */ 2120789Sahrens (void) get_stats(zhp); 2121789Sahrens } 2122789Sahrens 2123789Sahrens error: 2124789Sahrens changelist_free(cl); 2125789Sahrens return (ret); 2126789Sahrens } 2127789Sahrens 21283912Slling void 2129789Sahrens nicebool(int value, char *buf, size_t buflen) 2130789Sahrens { 2131789Sahrens if (value) 2132789Sahrens (void) strlcpy(buf, "on", buflen); 2133789Sahrens else 2134789Sahrens (void) strlcpy(buf, "off", buflen); 2135789Sahrens } 2136789Sahrens 2137789Sahrens /* 21381356Seschrock * True DSL properties are stored in an nvlist. The following two functions 21391356Seschrock * extract them appropriately. 21401356Seschrock */ 21411356Seschrock static uint64_t 21421356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 21431356Seschrock { 21441356Seschrock nvlist_t *nv; 21451356Seschrock uint64_t value; 21461356Seschrock 21472885Sahrens *source = NULL; 21481356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 21491356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 21501356Seschrock verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0); 21512885Sahrens (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 21521356Seschrock } else { 21531356Seschrock value = zfs_prop_default_numeric(prop); 21541356Seschrock *source = ""; 21551356Seschrock } 21561356Seschrock 21571356Seschrock return (value); 21581356Seschrock } 21591356Seschrock 21601356Seschrock static char * 21611356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 21621356Seschrock { 21631356Seschrock nvlist_t *nv; 21641356Seschrock char *value; 21651356Seschrock 21662885Sahrens *source = NULL; 21671356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 21681356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 21691356Seschrock verify(nvlist_lookup_string(nv, ZFS_PROP_VALUE, &value) == 0); 21702885Sahrens (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 21711356Seschrock } else { 21721356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 21731356Seschrock value = ""; 21741356Seschrock *source = ""; 21751356Seschrock } 21761356Seschrock 21771356Seschrock return (value); 21781356Seschrock } 21791356Seschrock 21801356Seschrock /* 2181789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2182789Sahrens * zfs_prop_get_int() are built using this interface. 2183789Sahrens * 2184789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2185789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2186789Sahrens * If they differ from the on-disk values, report the current values and mark 2187789Sahrens * the source "temporary". 2188789Sahrens */ 21892082Seschrock static int 2190789Sahrens get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src, 21912082Seschrock char **source, uint64_t *val) 2192789Sahrens { 2193789Sahrens struct mnttab mnt; 21943265Sahrens char *mntopt_on = NULL; 21953265Sahrens char *mntopt_off = NULL; 2196789Sahrens 2197789Sahrens *source = NULL; 2198789Sahrens 21993265Sahrens switch (prop) { 22003265Sahrens case ZFS_PROP_ATIME: 22013265Sahrens mntopt_on = MNTOPT_ATIME; 22023265Sahrens mntopt_off = MNTOPT_NOATIME; 22033265Sahrens break; 22043265Sahrens 22053265Sahrens case ZFS_PROP_DEVICES: 22063265Sahrens mntopt_on = MNTOPT_DEVICES; 22073265Sahrens mntopt_off = MNTOPT_NODEVICES; 22083265Sahrens break; 22093265Sahrens 22103265Sahrens case ZFS_PROP_EXEC: 22113265Sahrens mntopt_on = MNTOPT_EXEC; 22123265Sahrens mntopt_off = MNTOPT_NOEXEC; 22133265Sahrens break; 22143265Sahrens 22153265Sahrens case ZFS_PROP_READONLY: 22163265Sahrens mntopt_on = MNTOPT_RO; 22173265Sahrens mntopt_off = MNTOPT_RW; 22183265Sahrens break; 22193265Sahrens 22203265Sahrens case ZFS_PROP_SETUID: 22213265Sahrens mntopt_on = MNTOPT_SETUID; 22223265Sahrens mntopt_off = MNTOPT_NOSETUID; 22233265Sahrens break; 22243265Sahrens 22253265Sahrens case ZFS_PROP_XATTR: 22263265Sahrens mntopt_on = MNTOPT_XATTR; 22273265Sahrens mntopt_off = MNTOPT_NOXATTR; 22283265Sahrens break; 22293265Sahrens } 22303265Sahrens 22312474Seschrock /* 22322474Seschrock * Because looking up the mount options is potentially expensive 22332474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 22342474Seschrock * we're looking up a property which requires its presence. 22352474Seschrock */ 22362474Seschrock if (!zhp->zfs_mntcheck && 22373265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 22383265Sahrens struct mnttab entry, search = { 0 }; 22393265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 22402474Seschrock 22412474Seschrock search.mnt_special = (char *)zhp->zfs_name; 22422474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 22433265Sahrens rewind(mnttab); 22443265Sahrens 22453265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 22463265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 22473265Sahrens entry.mnt_mntopts); 22483265Sahrens if (zhp->zfs_mntopts == NULL) 22493265Sahrens return (-1); 22503265Sahrens } 22512474Seschrock 22522474Seschrock zhp->zfs_mntcheck = B_TRUE; 22532474Seschrock } 22542474Seschrock 2255789Sahrens if (zhp->zfs_mntopts == NULL) 2256789Sahrens mnt.mnt_mntopts = ""; 2257789Sahrens else 2258789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2259789Sahrens 2260789Sahrens switch (prop) { 2261789Sahrens case ZFS_PROP_ATIME: 22623265Sahrens case ZFS_PROP_DEVICES: 22633265Sahrens case ZFS_PROP_EXEC: 22643265Sahrens case ZFS_PROP_READONLY: 22653265Sahrens case ZFS_PROP_SETUID: 22663265Sahrens case ZFS_PROP_XATTR: 22672082Seschrock *val = getprop_uint64(zhp, prop, source); 22682082Seschrock 22693265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 22702082Seschrock *val = B_TRUE; 2271789Sahrens if (src) 2272789Sahrens *src = ZFS_SRC_TEMPORARY; 22733265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 22742082Seschrock *val = B_FALSE; 2275789Sahrens if (src) 2276789Sahrens *src = ZFS_SRC_TEMPORARY; 2277789Sahrens } 22782082Seschrock break; 2279789Sahrens 22803265Sahrens case ZFS_PROP_CANMOUNT: 22812082Seschrock *val = getprop_uint64(zhp, prop, source); 22823417Srm160521 if (*val == 0) 22833417Srm160521 *source = zhp->zfs_name; 22843417Srm160521 else 22853417Srm160521 *source = ""; /* default */ 22862082Seschrock break; 2287789Sahrens 2288789Sahrens case ZFS_PROP_QUOTA: 2289789Sahrens case ZFS_PROP_RESERVATION: 22902885Sahrens *val = getprop_uint64(zhp, prop, source); 22912885Sahrens if (*val == 0) 2292789Sahrens *source = ""; /* default */ 2293789Sahrens else 2294789Sahrens *source = zhp->zfs_name; 22952082Seschrock break; 2296789Sahrens 2297789Sahrens case ZFS_PROP_MOUNTED: 22982082Seschrock *val = (zhp->zfs_mntopts != NULL); 22992082Seschrock break; 2300789Sahrens 23013377Seschrock case ZFS_PROP_NUMCLONES: 23023377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 23033377Seschrock break; 23043377Seschrock 2305789Sahrens default: 2306*4577Sahrens switch (zfs_prop_get_type(prop)) { 2307*4577Sahrens case prop_type_number: 2308*4577Sahrens case prop_type_boolean: 2309*4577Sahrens case prop_type_index: 2310*4577Sahrens *val = getprop_uint64(zhp, prop, source); 2311*4577Sahrens break; 2312*4577Sahrens 2313*4577Sahrens case prop_type_string: 2314*4577Sahrens default: 2315*4577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2316*4577Sahrens "cannot get non-numeric property")); 2317*4577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 2318*4577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 2319*4577Sahrens } 2320789Sahrens } 2321789Sahrens 2322789Sahrens return (0); 2323789Sahrens } 2324789Sahrens 2325789Sahrens /* 2326789Sahrens * Calculate the source type, given the raw source string. 2327789Sahrens */ 2328789Sahrens static void 2329789Sahrens get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source, 2330789Sahrens char *statbuf, size_t statlen) 2331789Sahrens { 2332789Sahrens if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY) 2333789Sahrens return; 2334789Sahrens 2335789Sahrens if (source == NULL) { 2336789Sahrens *srctype = ZFS_SRC_NONE; 2337789Sahrens } else if (source[0] == '\0') { 2338789Sahrens *srctype = ZFS_SRC_DEFAULT; 2339789Sahrens } else { 2340789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 2341789Sahrens *srctype = ZFS_SRC_LOCAL; 2342789Sahrens } else { 2343789Sahrens (void) strlcpy(statbuf, source, statlen); 2344789Sahrens *srctype = ZFS_SRC_INHERITED; 2345789Sahrens } 2346789Sahrens } 2347789Sahrens 2348789Sahrens } 2349789Sahrens 2350789Sahrens /* 2351789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2352789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2353789Sahrens * human-readable form. 2354789Sahrens * 2355789Sahrens * Returns 0 on success, or -1 on error. 2356789Sahrens */ 2357789Sahrens int 2358789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 23592082Seschrock zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2360789Sahrens { 2361789Sahrens char *source = NULL; 2362789Sahrens uint64_t val; 2363789Sahrens char *str; 2364789Sahrens const char *root; 23652676Seschrock const char *strval; 2366789Sahrens 2367789Sahrens /* 2368789Sahrens * Check to see if this property applies to our object 2369789Sahrens */ 2370789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2371789Sahrens return (-1); 2372789Sahrens 2373789Sahrens if (src) 2374789Sahrens *src = ZFS_SRC_NONE; 2375789Sahrens 2376789Sahrens switch (prop) { 2377789Sahrens case ZFS_PROP_CREATION: 2378789Sahrens /* 2379789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2380789Sahrens * this into a string unless 'literal' is specified. 2381789Sahrens */ 2382789Sahrens { 23832885Sahrens val = getprop_uint64(zhp, prop, &source); 23842885Sahrens time_t time = (time_t)val; 2385789Sahrens struct tm t; 2386789Sahrens 2387789Sahrens if (literal || 2388789Sahrens localtime_r(&time, &t) == NULL || 2389789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2390789Sahrens &t) == 0) 23912885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2392789Sahrens } 2393789Sahrens break; 2394789Sahrens 2395789Sahrens case ZFS_PROP_MOUNTPOINT: 2396789Sahrens /* 2397789Sahrens * Getting the precise mountpoint can be tricky. 2398789Sahrens * 2399789Sahrens * - for 'none' or 'legacy', return those values. 2400789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2401789Sahrens * - for inherited mountpoints, we want to take everything 2402789Sahrens * after our ancestor and append it to the inherited value. 2403789Sahrens * 2404789Sahrens * If the pool has an alternate root, we want to prepend that 2405789Sahrens * root to any values we return. 2406789Sahrens */ 24071544Seschrock root = zhp->zfs_root; 24081356Seschrock str = getprop_string(zhp, prop, &source); 24091356Seschrock 24101356Seschrock if (str[0] == '\0') { 2411789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2412789Sahrens root, zhp->zfs_name); 24131356Seschrock } else if (str[0] == '/') { 24141356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2415789Sahrens 2416789Sahrens if (relpath[0] == '/') 2417789Sahrens relpath++; 24181356Seschrock if (str[1] == '\0') 24191356Seschrock str++; 2420789Sahrens 2421789Sahrens if (relpath[0] == '\0') 2422789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 24231356Seschrock root, str); 2424789Sahrens else 2425789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 24261356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2427789Sahrens relpath); 2428789Sahrens } else { 2429789Sahrens /* 'legacy' or 'none' */ 24301356Seschrock (void) strlcpy(propbuf, str, proplen); 2431789Sahrens } 2432789Sahrens 2433789Sahrens break; 2434789Sahrens 2435789Sahrens case ZFS_PROP_ORIGIN: 24362885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2437789Sahrens proplen); 2438789Sahrens /* 2439789Sahrens * If there is no parent at all, return failure to indicate that 2440789Sahrens * it doesn't apply to this dataset. 2441789Sahrens */ 2442789Sahrens if (propbuf[0] == '\0') 2443789Sahrens return (-1); 2444789Sahrens break; 2445789Sahrens 2446789Sahrens case ZFS_PROP_QUOTA: 2447789Sahrens case ZFS_PROP_RESERVATION: 24482082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 24492082Seschrock return (-1); 2450789Sahrens 2451789Sahrens /* 2452789Sahrens * If quota or reservation is 0, we translate this into 'none' 2453789Sahrens * (unless literal is set), and indicate that it's the default 2454789Sahrens * value. Otherwise, we print the number nicely and indicate 2455789Sahrens * that its set locally. 2456789Sahrens */ 2457789Sahrens if (val == 0) { 2458789Sahrens if (literal) 2459789Sahrens (void) strlcpy(propbuf, "0", proplen); 2460789Sahrens else 2461789Sahrens (void) strlcpy(propbuf, "none", proplen); 2462789Sahrens } else { 2463789Sahrens if (literal) 24642856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 24653912Slling (u_longlong_t)val); 2466789Sahrens else 2467789Sahrens zfs_nicenum(val, propbuf, proplen); 2468789Sahrens } 2469789Sahrens break; 2470789Sahrens 2471789Sahrens case ZFS_PROP_COMPRESSRATIO: 24722082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 24732082Seschrock return (-1); 24742856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 24752856Snd150628 val / 100, (longlong_t)val % 100); 2476789Sahrens break; 2477789Sahrens 2478789Sahrens case ZFS_PROP_TYPE: 2479789Sahrens switch (zhp->zfs_type) { 2480789Sahrens case ZFS_TYPE_FILESYSTEM: 2481789Sahrens str = "filesystem"; 2482789Sahrens break; 2483789Sahrens case ZFS_TYPE_VOLUME: 2484789Sahrens str = "volume"; 2485789Sahrens break; 2486789Sahrens case ZFS_TYPE_SNAPSHOT: 2487789Sahrens str = "snapshot"; 2488789Sahrens break; 2489789Sahrens default: 24902082Seschrock abort(); 2491789Sahrens } 2492789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2493789Sahrens break; 2494789Sahrens 2495789Sahrens case ZFS_PROP_MOUNTED: 2496789Sahrens /* 2497789Sahrens * The 'mounted' property is a pseudo-property that described 2498789Sahrens * whether the filesystem is currently mounted. Even though 2499789Sahrens * it's a boolean value, the typical values of "on" and "off" 2500789Sahrens * don't make sense, so we translate to "yes" and "no". 2501789Sahrens */ 25022082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 25032082Seschrock src, &source, &val) != 0) 25042082Seschrock return (-1); 25052082Seschrock if (val) 2506789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2507789Sahrens else 2508789Sahrens (void) strlcpy(propbuf, "no", proplen); 2509789Sahrens break; 2510789Sahrens 2511789Sahrens case ZFS_PROP_NAME: 2512789Sahrens /* 2513789Sahrens * The 'name' property is a pseudo-property derived from the 2514789Sahrens * dataset name. It is presented as a real property to simplify 2515789Sahrens * consumers. 2516789Sahrens */ 2517789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2518789Sahrens break; 2519789Sahrens 2520789Sahrens default: 2521*4577Sahrens switch (zfs_prop_get_type(prop)) { 2522*4577Sahrens case prop_type_number: 2523*4577Sahrens if (get_numeric_property(zhp, prop, src, 2524*4577Sahrens &source, &val) != 0) 2525*4577Sahrens return (-1); 2526*4577Sahrens if (literal) 2527*4577Sahrens (void) snprintf(propbuf, proplen, "%llu", 2528*4577Sahrens (u_longlong_t)val); 2529*4577Sahrens else 2530*4577Sahrens zfs_nicenum(val, propbuf, proplen); 2531*4577Sahrens break; 2532*4577Sahrens 2533*4577Sahrens case prop_type_string: 2534*4577Sahrens (void) strlcpy(propbuf, 2535*4577Sahrens getprop_string(zhp, prop, &source), proplen); 2536*4577Sahrens break; 2537*4577Sahrens 2538*4577Sahrens case prop_type_boolean: 2539*4577Sahrens if (get_numeric_property(zhp, prop, src, 2540*4577Sahrens &source, &val) != 0) 2541*4577Sahrens return (-1); 2542*4577Sahrens nicebool(val, propbuf, proplen); 2543*4577Sahrens 2544*4577Sahrens break; 2545*4577Sahrens 2546*4577Sahrens case prop_type_index: 2547*4577Sahrens val = getprop_uint64(zhp, prop, &source); 2548*4577Sahrens if (zfs_prop_index_to_string(prop, val, 2549*4577Sahrens &strval) != 0) 2550*4577Sahrens return (-1); 2551*4577Sahrens (void) strlcpy(propbuf, strval, proplen); 2552*4577Sahrens break; 2553*4577Sahrens 2554*4577Sahrens default: 2555*4577Sahrens abort(); 2556*4577Sahrens } 2557789Sahrens } 2558789Sahrens 2559789Sahrens get_source(zhp, src, source, statbuf, statlen); 2560789Sahrens 2561789Sahrens return (0); 2562789Sahrens } 2563789Sahrens 2564789Sahrens /* 2565789Sahrens * Utility function to get the given numeric property. Does no validation that 2566789Sahrens * the given property is the appropriate type; should only be used with 2567789Sahrens * hard-coded property types. 2568789Sahrens */ 2569789Sahrens uint64_t 2570789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2571789Sahrens { 2572789Sahrens char *source; 2573789Sahrens zfs_source_t sourcetype = ZFS_SRC_NONE; 25742082Seschrock uint64_t val; 25752082Seschrock 25762082Seschrock (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 25772082Seschrock 25782082Seschrock return (val); 2579789Sahrens } 2580789Sahrens 2581789Sahrens /* 2582789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2583789Sahrens */ 2584789Sahrens int 2585789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2586789Sahrens zfs_source_t *src, char *statbuf, size_t statlen) 2587789Sahrens { 2588789Sahrens char *source; 2589789Sahrens 2590789Sahrens /* 2591789Sahrens * Check to see if this property applies to our object 2592789Sahrens */ 2593789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 25943237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 25952082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 25962082Seschrock zfs_prop_to_name(prop))); 2597789Sahrens 2598789Sahrens if (src) 2599789Sahrens *src = ZFS_SRC_NONE; 2600789Sahrens 26012082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 26022082Seschrock return (-1); 2603789Sahrens 2604789Sahrens get_source(zhp, src, source, statbuf, statlen); 2605789Sahrens 2606789Sahrens return (0); 2607789Sahrens } 2608789Sahrens 2609789Sahrens /* 2610789Sahrens * Returns the name of the given zfs handle. 2611789Sahrens */ 2612789Sahrens const char * 2613789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2614789Sahrens { 2615789Sahrens return (zhp->zfs_name); 2616789Sahrens } 2617789Sahrens 2618789Sahrens /* 2619789Sahrens * Returns the type of the given zfs handle. 2620789Sahrens */ 2621789Sahrens zfs_type_t 2622789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2623789Sahrens { 2624789Sahrens return (zhp->zfs_type); 2625789Sahrens } 2626789Sahrens 2627789Sahrens /* 26281356Seschrock * Iterate over all child filesystems 2629789Sahrens */ 2630789Sahrens int 26311356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2632789Sahrens { 2633789Sahrens zfs_cmd_t zc = { 0 }; 2634789Sahrens zfs_handle_t *nzhp; 2635789Sahrens int ret; 2636789Sahrens 2637789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 26382082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2639789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2640789Sahrens /* 2641789Sahrens * Ignore private dataset names. 2642789Sahrens */ 2643789Sahrens if (dataset_name_hidden(zc.zc_name)) 2644789Sahrens continue; 2645789Sahrens 2646789Sahrens /* 2647789Sahrens * Silently ignore errors, as the only plausible explanation is 2648789Sahrens * that the pool has since been removed. 2649789Sahrens */ 26502082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 26512082Seschrock zc.zc_name)) == NULL) 2652789Sahrens continue; 2653789Sahrens 2654789Sahrens if ((ret = func(nzhp, data)) != 0) 2655789Sahrens return (ret); 2656789Sahrens } 2657789Sahrens 2658789Sahrens /* 2659789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2660789Sahrens * returned, then the underlying dataset has been removed since we 2661789Sahrens * obtained the handle. 2662789Sahrens */ 2663789Sahrens if (errno != ESRCH && errno != ENOENT) 26642082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 26652082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2666789Sahrens 26671356Seschrock return (0); 26681356Seschrock } 26691356Seschrock 26701356Seschrock /* 26711356Seschrock * Iterate over all snapshots 26721356Seschrock */ 26731356Seschrock int 26741356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 26751356Seschrock { 26761356Seschrock zfs_cmd_t zc = { 0 }; 26771356Seschrock zfs_handle_t *nzhp; 26781356Seschrock int ret; 2679789Sahrens 2680789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 26812082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 26822082Seschrock &zc) == 0; 2683789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2684789Sahrens 26852082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 26862082Seschrock zc.zc_name)) == NULL) 2687789Sahrens continue; 2688789Sahrens 2689789Sahrens if ((ret = func(nzhp, data)) != 0) 2690789Sahrens return (ret); 2691789Sahrens } 2692789Sahrens 2693789Sahrens /* 2694789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2695789Sahrens * returned, then the underlying dataset has been removed since we 2696789Sahrens * obtained the handle. Silently ignore this case, and return success. 2697789Sahrens */ 2698789Sahrens if (errno != ESRCH && errno != ENOENT) 26992082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 27002082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2701789Sahrens 2702789Sahrens return (0); 2703789Sahrens } 2704789Sahrens 2705789Sahrens /* 27061356Seschrock * Iterate over all children, snapshots and filesystems 27071356Seschrock */ 27081356Seschrock int 27091356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 27101356Seschrock { 27111356Seschrock int ret; 27121356Seschrock 27131356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 27141356Seschrock return (ret); 27151356Seschrock 27161356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 27171356Seschrock } 27181356Seschrock 27191356Seschrock /* 2720789Sahrens * Given a complete name, return just the portion that refers to the parent. 2721789Sahrens * Can return NULL if this is a pool. 2722789Sahrens */ 2723789Sahrens static int 2724789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2725789Sahrens { 2726789Sahrens char *loc; 2727789Sahrens 2728789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2729789Sahrens return (-1); 2730789Sahrens 2731789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2732789Sahrens buf[loc - path] = '\0'; 2733789Sahrens 2734789Sahrens return (0); 2735789Sahrens } 2736789Sahrens 2737789Sahrens /* 27384490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 27394490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 27404490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 27414490Svb160487 * length of already existing prefix of the given path. We also fetch the 27424490Svb160487 * 'zoned' property, which is used to validate property settings when creating 27434490Svb160487 * new datasets. 2744789Sahrens */ 2745789Sahrens static int 27464490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 27474490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2748789Sahrens { 2749789Sahrens zfs_cmd_t zc = { 0 }; 2750789Sahrens char parent[ZFS_MAXNAMELEN]; 2751789Sahrens char *slash; 27521356Seschrock zfs_handle_t *zhp; 27532082Seschrock char errbuf[1024]; 27542082Seschrock 27552082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 27562082Seschrock path); 2757789Sahrens 2758789Sahrens /* get parent, and check to see if this is just a pool */ 2759789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 27602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27612082Seschrock "missing dataset name")); 27622082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2763789Sahrens } 2764789Sahrens 2765789Sahrens /* check to see if the pool exists */ 2766789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2767789Sahrens slash = parent + strlen(parent); 2768789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2769789Sahrens zc.zc_name[slash - parent] = '\0'; 27702082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2771789Sahrens errno == ENOENT) { 27722082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27732082Seschrock "no such pool '%s'"), zc.zc_name); 27742082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2775789Sahrens } 2776789Sahrens 2777789Sahrens /* check to see if the parent dataset exists */ 27784490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 27794490Svb160487 if (errno == ENOENT && accept_ancestor) { 27804490Svb160487 /* 27814490Svb160487 * Go deeper to find an ancestor, give up on top level. 27824490Svb160487 */ 27834490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 27844490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27854490Svb160487 "no such pool '%s'"), zc.zc_name); 27864490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 27874490Svb160487 } 27884490Svb160487 } else if (errno == ENOENT) { 27892082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27902082Seschrock "parent does not exist")); 27912082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 27924490Svb160487 } else 27932082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2794789Sahrens } 2795789Sahrens 27962676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2797789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 27982676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 27992082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 28001356Seschrock zfs_close(zhp); 2801789Sahrens return (-1); 2802789Sahrens } 2803789Sahrens 2804789Sahrens /* make sure parent is a filesystem */ 28051356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 28062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28072082Seschrock "parent is not a filesystem")); 28082082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 28091356Seschrock zfs_close(zhp); 2810789Sahrens return (-1); 2811789Sahrens } 2812789Sahrens 28131356Seschrock zfs_close(zhp); 28144490Svb160487 if (prefixlen != NULL) 28154490Svb160487 *prefixlen = strlen(parent); 28164490Svb160487 return (0); 28174490Svb160487 } 28184490Svb160487 28194490Svb160487 /* 28204490Svb160487 * Finds whether the dataset of the given type(s) exists. 28214490Svb160487 */ 28224490Svb160487 boolean_t 28234490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 28244490Svb160487 { 28254490Svb160487 zfs_handle_t *zhp; 28264490Svb160487 28274490Svb160487 if (!zfs_validate_name(hdl, path, types)) 28284490Svb160487 return (B_FALSE); 28294490Svb160487 28304490Svb160487 /* 28314490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 28324490Svb160487 */ 28334490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 28344490Svb160487 int ds_type = zhp->zfs_type; 28354490Svb160487 28364490Svb160487 zfs_close(zhp); 28374490Svb160487 if (types & ds_type) 28384490Svb160487 return (B_TRUE); 28394490Svb160487 } 28404490Svb160487 return (B_FALSE); 28414490Svb160487 } 28424490Svb160487 28434490Svb160487 /* 28444490Svb160487 * Creates non-existing ancestors of the given path. 28454490Svb160487 */ 28464490Svb160487 int 28474490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 28484490Svb160487 { 28494490Svb160487 int prefix; 28504490Svb160487 uint64_t zoned; 28514490Svb160487 char *path_copy; 28524490Svb160487 int rc; 28534490Svb160487 28544490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 28554490Svb160487 return (-1); 28564490Svb160487 28574490Svb160487 if ((path_copy = strdup(path)) != NULL) { 28584490Svb160487 rc = create_parents(hdl, path_copy, prefix); 28594490Svb160487 free(path_copy); 28604490Svb160487 } 28614490Svb160487 if (path_copy == NULL || rc != 0) 28624490Svb160487 return (-1); 28634490Svb160487 2864789Sahrens return (0); 2865789Sahrens } 2866789Sahrens 2867789Sahrens /* 28682676Seschrock * Create a new filesystem or volume. 2869789Sahrens */ 2870789Sahrens int 28712082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 28722676Seschrock nvlist_t *props) 2873789Sahrens { 2874789Sahrens zfs_cmd_t zc = { 0 }; 2875789Sahrens int ret; 2876789Sahrens uint64_t size = 0; 2877789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 28782082Seschrock char errbuf[1024]; 28792676Seschrock uint64_t zoned; 28802082Seschrock 28812082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28822082Seschrock "cannot create '%s'"), path); 2883789Sahrens 2884789Sahrens /* validate the path, taking care to note the extended error message */ 28852082Seschrock if (!zfs_validate_name(hdl, path, type)) 28862082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2887789Sahrens 2888789Sahrens /* validate parents exist */ 28894490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2890789Sahrens return (-1); 2891789Sahrens 2892789Sahrens /* 2893789Sahrens * The failure modes when creating a dataset of a different type over 2894789Sahrens * one that already exists is a little strange. In particular, if you 2895789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2896789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2897789Sahrens * first try to see if the dataset exists. 2898789Sahrens */ 2899789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 29004490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 29012082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29022082Seschrock "dataset already exists")); 29032082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2904789Sahrens } 2905789Sahrens 2906789Sahrens if (type == ZFS_TYPE_VOLUME) 2907789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2908789Sahrens else 2909789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2910789Sahrens 29113912Slling if (props && (props = zfs_validate_properties(hdl, type, NULL, props, 29123912Slling zoned, NULL, errbuf)) == 0) 29132676Seschrock return (-1); 29142676Seschrock 2915789Sahrens if (type == ZFS_TYPE_VOLUME) { 29161133Seschrock /* 29171133Seschrock * If we are creating a volume, the size and block size must 29181133Seschrock * satisfy a few restraints. First, the blocksize must be a 29191133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 29201133Seschrock * volsize must be a multiple of the block size, and cannot be 29211133Seschrock * zero. 29221133Seschrock */ 29232676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 29242676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 29252676Seschrock nvlist_free(props); 29262082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29272676Seschrock "missing volume size")); 29282676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2929789Sahrens } 2930789Sahrens 29312676Seschrock if ((ret = nvlist_lookup_uint64(props, 29322676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 29332676Seschrock &blocksize)) != 0) { 29342676Seschrock if (ret == ENOENT) { 29352676Seschrock blocksize = zfs_prop_default_numeric( 29362676Seschrock ZFS_PROP_VOLBLOCKSIZE); 29372676Seschrock } else { 29382676Seschrock nvlist_free(props); 29392676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29402676Seschrock "missing volume block size")); 29412676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29422676Seschrock } 29432676Seschrock } 29442676Seschrock 29452676Seschrock if (size == 0) { 29462676Seschrock nvlist_free(props); 29472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29482676Seschrock "volume size cannot be zero")); 29492676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29501133Seschrock } 29511133Seschrock 29521133Seschrock if (size % blocksize != 0) { 29532676Seschrock nvlist_free(props); 29542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29552676Seschrock "volume size must be a multiple of volume block " 29562676Seschrock "size")); 29572676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29581133Seschrock } 2959789Sahrens } 2960789Sahrens 29612676Seschrock if (props && 29622676Seschrock zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) 29632676Seschrock return (-1); 29642676Seschrock nvlist_free(props); 29652676Seschrock 2966789Sahrens /* create the dataset */ 29674543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2968789Sahrens 29693912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 29702082Seschrock ret = zvol_create_link(hdl, path); 29713912Slling if (ret) { 29723912Slling (void) zfs_standard_error(hdl, errno, 29733912Slling dgettext(TEXT_DOMAIN, 29743912Slling "Volume successfully created, but device links " 29753912Slling "were not created")); 29763912Slling zcmd_free_nvlists(&zc); 29773912Slling return (-1); 29783912Slling } 29793912Slling } 2980789Sahrens 29812676Seschrock zcmd_free_nvlists(&zc); 29822676Seschrock 2983789Sahrens /* check for failure */ 2984789Sahrens if (ret != 0) { 2985789Sahrens char parent[ZFS_MAXNAMELEN]; 2986789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2987789Sahrens 2988789Sahrens switch (errno) { 2989789Sahrens case ENOENT: 29902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29912082Seschrock "no such parent '%s'"), parent); 29922082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2993789Sahrens 2994789Sahrens case EINVAL: 29952082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29963413Smmusante "parent '%s' is not a filesystem"), parent); 29972082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2998789Sahrens 2999789Sahrens case EDOM: 30002082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30012676Seschrock "volume block size must be power of 2 from " 30022676Seschrock "%u to %uk"), 3003789Sahrens (uint_t)SPA_MINBLOCKSIZE, 3004789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 30052082Seschrock 30062676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 30072082Seschrock 3008789Sahrens #ifdef _ILP32 3009789Sahrens case EOVERFLOW: 3010789Sahrens /* 3011789Sahrens * This platform can't address a volume this big. 3012789Sahrens */ 30132082Seschrock if (type == ZFS_TYPE_VOLUME) 30142082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 30152082Seschrock errbuf)); 3016789Sahrens #endif 30172082Seschrock /* FALLTHROUGH */ 3018789Sahrens default: 30192082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3020789Sahrens } 3021789Sahrens } 3022789Sahrens 3023789Sahrens return (0); 3024789Sahrens } 3025789Sahrens 3026789Sahrens /* 3027789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 3028789Sahrens * isn't mounted, and that there are no active dependents. 3029789Sahrens */ 3030789Sahrens int 3031789Sahrens zfs_destroy(zfs_handle_t *zhp) 3032789Sahrens { 3033789Sahrens zfs_cmd_t zc = { 0 }; 3034789Sahrens 3035789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3036789Sahrens 30372676Seschrock if (ZFS_IS_VOLUME(zhp)) { 30383126Sahl /* 30394543Smarks * If user doesn't have permissions to unshare volume, then 30404543Smarks * abort the request. This would only happen for a 30414543Smarks * non-privileged user. 30423126Sahl */ 30434543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 30444543Smarks return (-1); 30454543Smarks } 30463126Sahl 30472082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3048789Sahrens return (-1); 3049789Sahrens 3050789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3051789Sahrens } else { 3052789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3053789Sahrens } 3054789Sahrens 30554543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 30563237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 30572082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 30582082Seschrock zhp->zfs_name)); 30592199Sahrens } 3060789Sahrens 3061789Sahrens remove_mountpoint(zhp); 3062789Sahrens 3063789Sahrens return (0); 3064789Sahrens } 3065789Sahrens 30662199Sahrens struct destroydata { 30672199Sahrens char *snapname; 30682199Sahrens boolean_t gotone; 30693265Sahrens boolean_t closezhp; 30702199Sahrens }; 30712199Sahrens 30722199Sahrens static int 30732199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 30742199Sahrens { 30752199Sahrens struct destroydata *dd = arg; 30762199Sahrens zfs_handle_t *szhp; 30772199Sahrens char name[ZFS_MAXNAMELEN]; 30783265Sahrens boolean_t closezhp = dd->closezhp; 30793265Sahrens int rv; 30802199Sahrens 30812676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 30822676Seschrock (void) strlcat(name, "@", sizeof (name)); 30832676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 30842199Sahrens 30852199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 30862199Sahrens if (szhp) { 30872199Sahrens dd->gotone = B_TRUE; 30882199Sahrens zfs_close(szhp); 30892199Sahrens } 30902199Sahrens 30912199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 30922199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 30932199Sahrens /* 30942199Sahrens * NB: this is simply a best-effort. We don't want to 30952199Sahrens * return an error, because then we wouldn't visit all 30962199Sahrens * the volumes. 30972199Sahrens */ 30982199Sahrens } 30992199Sahrens 31003265Sahrens dd->closezhp = B_TRUE; 31013265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 31023265Sahrens if (closezhp) 31033265Sahrens zfs_close(zhp); 31043265Sahrens return (rv); 31052199Sahrens } 31062199Sahrens 31072199Sahrens /* 31082199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 31092199Sahrens */ 31102199Sahrens int 31112199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 31122199Sahrens { 31132199Sahrens zfs_cmd_t zc = { 0 }; 31142199Sahrens int ret; 31152199Sahrens struct destroydata dd = { 0 }; 31162199Sahrens 31172199Sahrens dd.snapname = snapname; 31182199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 31192199Sahrens 31202199Sahrens if (!dd.gotone) { 31213237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 31222199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 31232199Sahrens zhp->zfs_name, snapname)); 31242199Sahrens } 31252199Sahrens 31262199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31272676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 31282199Sahrens 31294543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 31302199Sahrens if (ret != 0) { 31312199Sahrens char errbuf[1024]; 31322199Sahrens 31332199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31342199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 31352199Sahrens 31362199Sahrens switch (errno) { 31372199Sahrens case EEXIST: 31382199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31392199Sahrens "snapshot is cloned")); 31402199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 31412199Sahrens 31422199Sahrens default: 31432199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 31442199Sahrens errbuf)); 31452199Sahrens } 31462199Sahrens } 31472199Sahrens 31482199Sahrens return (0); 31492199Sahrens } 31502199Sahrens 3151789Sahrens /* 3152789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3153789Sahrens */ 3154789Sahrens int 31552676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3156789Sahrens { 3157789Sahrens zfs_cmd_t zc = { 0 }; 3158789Sahrens char parent[ZFS_MAXNAMELEN]; 3159789Sahrens int ret; 31602082Seschrock char errbuf[1024]; 31612082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 31622676Seschrock zfs_type_t type; 31632676Seschrock uint64_t zoned; 3164789Sahrens 3165789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3166789Sahrens 31672082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31682082Seschrock "cannot create '%s'"), target); 31692082Seschrock 3170789Sahrens /* validate the target name */ 31712082Seschrock if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM)) 31722082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3173789Sahrens 3174789Sahrens /* validate parents exist */ 31754490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3176789Sahrens return (-1); 3177789Sahrens 3178789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3179789Sahrens 3180789Sahrens /* do the clone */ 31812676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3182789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 31832744Snn35248 type = ZFS_TYPE_VOLUME; 31842676Seschrock } else { 3185789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 31862744Snn35248 type = ZFS_TYPE_FILESYSTEM; 31872676Seschrock } 31882676Seschrock 31892676Seschrock if (props) { 31903912Slling if ((props = zfs_validate_properties(hdl, type, NULL, props, 31913912Slling zoned, zhp, errbuf)) == NULL) 31922676Seschrock return (-1); 31932676Seschrock 31942676Seschrock if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) { 31952676Seschrock nvlist_free(props); 31962676Seschrock return (-1); 31972676Seschrock } 31982676Seschrock 31992676Seschrock nvlist_free(props); 32002676Seschrock } 3201789Sahrens 3202789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 32032676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 32044543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3205789Sahrens 32062676Seschrock zcmd_free_nvlists(&zc); 32072676Seschrock 3208789Sahrens if (ret != 0) { 3209789Sahrens switch (errno) { 3210789Sahrens 3211789Sahrens case ENOENT: 3212789Sahrens /* 3213789Sahrens * The parent doesn't exist. We should have caught this 3214789Sahrens * above, but there may a race condition that has since 3215789Sahrens * destroyed the parent. 3216789Sahrens * 3217789Sahrens * At this point, we don't know whether it's the source 3218789Sahrens * that doesn't exist anymore, or whether the target 3219789Sahrens * dataset doesn't exist. 3220789Sahrens */ 32212082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32222082Seschrock "no such parent '%s'"), parent); 32232082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 32242082Seschrock 32252082Seschrock case EXDEV: 32262082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32272082Seschrock "source and target pools differ")); 32282082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 32292082Seschrock errbuf)); 32302082Seschrock 32312082Seschrock default: 32322082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 32332082Seschrock errbuf)); 32342082Seschrock } 32352676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 32362082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 32372082Seschrock } 32382082Seschrock 32392082Seschrock return (ret); 32402082Seschrock } 32412082Seschrock 32422082Seschrock typedef struct promote_data { 32432082Seschrock char cb_mountpoint[MAXPATHLEN]; 32442082Seschrock const char *cb_target; 32452082Seschrock const char *cb_errbuf; 32462082Seschrock uint64_t cb_pivot_txg; 32472082Seschrock } promote_data_t; 32482082Seschrock 32492082Seschrock static int 32502082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 32512082Seschrock { 32522082Seschrock promote_data_t *pd = data; 32532082Seschrock zfs_handle_t *szhp; 32542082Seschrock char snapname[MAXPATHLEN]; 32553265Sahrens int rv = 0; 32562082Seschrock 32572082Seschrock /* We don't care about snapshots after the pivot point */ 32583265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 32593265Sahrens zfs_close(zhp); 32602082Seschrock return (0); 32613265Sahrens } 32622082Seschrock 32632417Sahrens /* Remove the device link if it's a zvol. */ 32642676Seschrock if (ZFS_IS_VOLUME(zhp)) 32652417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 32662082Seschrock 32672082Seschrock /* Check for conflicting names */ 32682676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 32692676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 32702082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 32712082Seschrock if (szhp != NULL) { 32722082Seschrock zfs_close(szhp); 32732082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32742082Seschrock "snapshot name '%s' from origin \n" 32752082Seschrock "conflicts with '%s' from target"), 32762082Seschrock zhp->zfs_name, snapname); 32773265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 32782082Seschrock } 32793265Sahrens zfs_close(zhp); 32803265Sahrens return (rv); 32812082Seschrock } 32822082Seschrock 32832417Sahrens static int 32842417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 32852417Sahrens { 32862417Sahrens promote_data_t *pd = data; 32872417Sahrens 32882417Sahrens /* We don't care about snapshots after the pivot point */ 32893265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 32903265Sahrens /* Create the device link if it's a zvol. */ 32913265Sahrens if (ZFS_IS_VOLUME(zhp)) 32923265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 32933265Sahrens } 32943265Sahrens 32953265Sahrens zfs_close(zhp); 32962417Sahrens return (0); 32972417Sahrens } 32982417Sahrens 32992082Seschrock /* 33002082Seschrock * Promotes the given clone fs to be the clone parent. 33012082Seschrock */ 33022082Seschrock int 33032082Seschrock zfs_promote(zfs_handle_t *zhp) 33042082Seschrock { 33052082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 33062082Seschrock zfs_cmd_t zc = { 0 }; 33072082Seschrock char parent[MAXPATHLEN]; 33082082Seschrock char *cp; 33092082Seschrock int ret; 33102082Seschrock zfs_handle_t *pzhp; 33112082Seschrock promote_data_t pd; 33122082Seschrock char errbuf[1024]; 33132082Seschrock 33142082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33152082Seschrock "cannot promote '%s'"), zhp->zfs_name); 33162082Seschrock 33172082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 33182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33192082Seschrock "snapshots can not be promoted")); 33202082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 33212082Seschrock } 33222082Seschrock 33232676Seschrock (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 33242082Seschrock if (parent[0] == '\0') { 33252082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33262082Seschrock "not a cloned filesystem")); 33272082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 33282082Seschrock } 33292082Seschrock cp = strchr(parent, '@'); 33302082Seschrock *cp = '\0'; 33312082Seschrock 33322082Seschrock /* Walk the snapshots we will be moving */ 33332082Seschrock pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 33342082Seschrock if (pzhp == NULL) 33352082Seschrock return (-1); 33362082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 33372082Seschrock zfs_close(pzhp); 33382082Seschrock pd.cb_target = zhp->zfs_name; 33392082Seschrock pd.cb_errbuf = errbuf; 33402082Seschrock pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY); 33412082Seschrock if (pzhp == NULL) 33422082Seschrock return (-1); 33432082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 33442082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 33452082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 33462417Sahrens if (ret != 0) { 33472417Sahrens zfs_close(pzhp); 33482082Seschrock return (-1); 33492417Sahrens } 33502082Seschrock 33512082Seschrock /* issue the ioctl */ 33522676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 33532676Seschrock sizeof (zc.zc_value)); 33542082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33554543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 33562082Seschrock 33572082Seschrock if (ret != 0) { 33582417Sahrens int save_errno = errno; 33592417Sahrens 33602417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 33612417Sahrens zfs_close(pzhp); 33622417Sahrens 33632417Sahrens switch (save_errno) { 3364789Sahrens case EEXIST: 3365789Sahrens /* 33662082Seschrock * There is a conflicting snapshot name. We 33672082Seschrock * should have caught this above, but they could 33682082Seschrock * have renamed something in the mean time. 3369789Sahrens */ 33702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33712082Seschrock "conflicting snapshot name from parent '%s'"), 33722082Seschrock parent); 33732082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3374789Sahrens 3375789Sahrens default: 33762417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3377789Sahrens } 33782417Sahrens } else { 33792417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3380789Sahrens } 3381789Sahrens 33822417Sahrens zfs_close(pzhp); 3383789Sahrens return (ret); 3384789Sahrens } 3385789Sahrens 33864007Smmusante struct createdata { 33874007Smmusante const char *cd_snapname; 33884007Smmusante int cd_ifexists; 33894007Smmusante }; 33904007Smmusante 33912199Sahrens static int 33922199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 33932199Sahrens { 33944007Smmusante struct createdata *cd = arg; 33952676Seschrock int ret; 33962199Sahrens 33972199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33982199Sahrens char name[MAXPATHLEN]; 33992199Sahrens 34002676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 34012676Seschrock (void) strlcat(name, "@", sizeof (name)); 34024007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 34034007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 34044007Smmusante cd->cd_ifexists); 34052199Sahrens /* 34062199Sahrens * NB: this is simply a best-effort. We don't want to 34072199Sahrens * return an error, because then we wouldn't visit all 34082199Sahrens * the volumes. 34092199Sahrens */ 34102199Sahrens } 34112676Seschrock 34124007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 34132676Seschrock 34142676Seschrock zfs_close(zhp); 34152676Seschrock 34162676Seschrock return (ret); 34172199Sahrens } 34182199Sahrens 3419789Sahrens /* 34203504Sahl * Takes a snapshot of the given dataset. 3421789Sahrens */ 3422789Sahrens int 34232199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3424789Sahrens { 3425789Sahrens const char *delim; 3426789Sahrens char *parent; 3427789Sahrens zfs_handle_t *zhp; 3428789Sahrens zfs_cmd_t zc = { 0 }; 3429789Sahrens int ret; 34302082Seschrock char errbuf[1024]; 34312082Seschrock 34322082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34332082Seschrock "cannot snapshot '%s'"), path); 34342082Seschrock 34352082Seschrock /* validate the target name */ 34362082Seschrock if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT)) 34372082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3438789Sahrens 3439789Sahrens /* make sure the parent exists and is of the appropriate type */ 34402199Sahrens delim = strchr(path, '@'); 34412082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 34422082Seschrock return (-1); 3443789Sahrens (void) strncpy(parent, path, delim - path); 3444789Sahrens parent[delim - path] = '\0'; 3445789Sahrens 34462082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3447789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3448789Sahrens free(parent); 3449789Sahrens return (-1); 3450789Sahrens } 3451789Sahrens 34522199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 34532676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 34544543Smarks if (ZFS_IS_VOLUME(zhp)) 34554543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 34564543Smarks else 34574543Smarks zc.zc_objset_type = DMU_OST_ZFS; 34582199Sahrens zc.zc_cookie = recursive; 34594543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 34602199Sahrens 34612199Sahrens /* 34622199Sahrens * if it was recursive, the one that actually failed will be in 34632199Sahrens * zc.zc_name. 34642199Sahrens */ 34654543Smarks if (ret != 0) 34664543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34674543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 34684543Smarks 34692199Sahrens if (ret == 0 && recursive) { 34704007Smmusante struct createdata cd; 34714007Smmusante 34724007Smmusante cd.cd_snapname = delim + 1; 34734007Smmusante cd.cd_ifexists = B_FALSE; 34744007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 34752199Sahrens } 3476789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 34772082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 34782199Sahrens if (ret != 0) { 34794543Smarks (void) zfs_standard_error(hdl, errno, 34804543Smarks dgettext(TEXT_DOMAIN, 34814543Smarks "Volume successfully snapshotted, but device links " 34824543Smarks "were not created")); 34834543Smarks free(parent); 34844543Smarks zfs_close(zhp); 34854543Smarks return (-1); 34862199Sahrens } 3487789Sahrens } 3488789Sahrens 34892082Seschrock if (ret != 0) 34902082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3491789Sahrens 3492789Sahrens free(parent); 3493789Sahrens zfs_close(zhp); 3494789Sahrens 3495789Sahrens return (ret); 3496789Sahrens } 3497789Sahrens 3498789Sahrens /* 34993504Sahl * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 35003504Sahl * NULL) to the file descriptor specified by outfd. 3501789Sahrens */ 3502789Sahrens int 35033504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3504789Sahrens { 3505789Sahrens zfs_cmd_t zc = { 0 }; 35062082Seschrock char errbuf[1024]; 35072885Sahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 35082082Seschrock 35093504Sahl assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 35103504Sahl 35112885Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35122885Sahrens if (fromsnap) 35132885Sahrens (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 35143504Sahl zc.zc_cookie = outfd; 35153504Sahl 35163504Sahl if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 35173504Sahl (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35183504Sahl "cannot send '%s'"), zhp->zfs_name); 35193504Sahl 3520789Sahrens switch (errno) { 3521789Sahrens 3522789Sahrens case EXDEV: 35232082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35243413Smmusante "not an earlier snapshot from the same fs")); 35252082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3526789Sahrens 3527789Sahrens case EDQUOT: 3528789Sahrens case EFBIG: 3529789Sahrens case EIO: 3530789Sahrens case ENOLINK: 3531789Sahrens case ENOSPC: 3532789Sahrens case ENOSTR: 3533789Sahrens case ENXIO: 3534789Sahrens case EPIPE: 3535789Sahrens case ERANGE: 3536789Sahrens case EFAULT: 3537789Sahrens case EROFS: 35382082Seschrock zfs_error_aux(hdl, strerror(errno)); 35392082Seschrock return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3540789Sahrens 3541789Sahrens default: 35422082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3543789Sahrens } 3544789Sahrens } 3545789Sahrens 35463504Sahl return (0); 3547789Sahrens } 3548789Sahrens 3549789Sahrens /* 35502885Sahrens * Create ancestors of 'target', but not target itself, and not 35512885Sahrens * ancestors whose names are shorter than prefixlen. Die if 35522885Sahrens * prefixlen-ancestor does not exist. 35532885Sahrens */ 35542885Sahrens static int 35552885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 35562885Sahrens { 35572885Sahrens zfs_handle_t *h; 35582885Sahrens char *cp; 35592885Sahrens 35602885Sahrens /* make sure prefix exists */ 35612885Sahrens cp = strchr(target + prefixlen, '/'); 35622885Sahrens *cp = '\0'; 35632885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 35642885Sahrens *cp = '/'; 35652885Sahrens if (h == NULL) 35662885Sahrens return (-1); 35672885Sahrens zfs_close(h); 35682885Sahrens 35692885Sahrens /* 35702885Sahrens * Attempt to create, mount, and share any ancestor filesystems, 35712885Sahrens * up to the prefixlen-long one. 35722885Sahrens */ 35732885Sahrens for (cp = target + prefixlen + 1; 35742885Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 35752885Sahrens const char *opname; 35764543Smarks char *logstr; 35772885Sahrens 35782885Sahrens *cp = '\0'; 35792885Sahrens 35802885Sahrens h = make_dataset_handle(hdl, target); 35812885Sahrens if (h) { 35822885Sahrens /* it already exists, nothing to do here */ 35832885Sahrens zfs_close(h); 35842885Sahrens continue; 35852885Sahrens } 35862885Sahrens 35872885Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 35884543Smarks logstr = hdl->libzfs_log_str; 35894543Smarks hdl->libzfs_log_str = NULL; 35902885Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 35914543Smarks NULL) != 0) { 35924543Smarks hdl->libzfs_log_str = logstr; 35932885Sahrens goto ancestorerr; 35944543Smarks } 35954543Smarks 35964543Smarks hdl->libzfs_log_str = logstr; 35972885Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 35982885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 35992885Sahrens if (h == NULL) 36002885Sahrens goto ancestorerr; 36012885Sahrens 36022885Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 36032885Sahrens if (zfs_mount(h, NULL, 0) != 0) 36042885Sahrens goto ancestorerr; 36052885Sahrens 36062885Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 36072885Sahrens if (zfs_share(h) != 0) 36082885Sahrens goto ancestorerr; 36092885Sahrens 36102885Sahrens zfs_close(h); 36112885Sahrens 36122885Sahrens continue; 36132885Sahrens ancestorerr: 36142885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36152885Sahrens "failed to %s ancestor '%s'"), opname, target); 36162885Sahrens return (-1); 36172885Sahrens } 36182885Sahrens 36192885Sahrens return (0); 36202885Sahrens } 36212885Sahrens 36222885Sahrens /* 36233504Sahl * Restores a backup of tosnap from the file descriptor specified by infd. 3624789Sahrens */ 3625789Sahrens int 36262082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 36273504Sahl int verbose, int dryrun, boolean_t force, int infd) 3628789Sahrens { 3629789Sahrens zfs_cmd_t zc = { 0 }; 3630789Sahrens time_t begin_time; 36312885Sahrens int ioctl_err, err, bytes, size, choplen; 3632789Sahrens char *cp; 3633789Sahrens dmu_replay_record_t drr; 3634789Sahrens struct drr_begin *drrb = &zc.zc_begin_record; 36352082Seschrock char errbuf[1024]; 36362665Snd150628 prop_changelist_t *clp; 36372885Sahrens char chopprefix[ZFS_MAXNAMELEN]; 3638789Sahrens 3639789Sahrens begin_time = time(NULL); 3640789Sahrens 36412082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36422082Seschrock "cannot receive")); 36432082Seschrock 3644789Sahrens /* read in the BEGIN record */ 3645789Sahrens cp = (char *)&drr; 3646789Sahrens bytes = 0; 3647789Sahrens do { 36483504Sahl size = read(infd, cp, sizeof (drr) - bytes); 3649868Sahrens cp += size; 3650868Sahrens bytes += size; 3651868Sahrens } while (size > 0); 3652868Sahrens 3653868Sahrens if (size < 0 || bytes != sizeof (drr)) { 36542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36552082Seschrock "stream (failed to read first record)")); 36562082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3657789Sahrens } 3658789Sahrens 3659789Sahrens zc.zc_begin_record = drr.drr_u.drr_begin; 3660789Sahrens 3661789Sahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3662789Sahrens drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 36632082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36642082Seschrock "stream (bad magic number)")); 36652082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3666789Sahrens } 3667789Sahrens 3668789Sahrens if (drrb->drr_version != DMU_BACKUP_VERSION && 3669789Sahrens drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 36702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 36712082Seschrock "0x%llx is supported (stream is version 0x%llx)"), 3672789Sahrens DMU_BACKUP_VERSION, drrb->drr_version); 36732082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3674789Sahrens } 3675789Sahrens 36762885Sahrens if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 36772885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36783912Slling "stream (bad snapshot name)")); 36792885Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 36802885Sahrens } 3681789Sahrens /* 36822885Sahrens * Determine how much of the snapshot name stored in the stream 36832885Sahrens * we are going to tack on to the name they specified on the 36842885Sahrens * command line, and how much we are going to chop off. 36852885Sahrens * 36862885Sahrens * If they specified a snapshot, chop the entire name stored in 36872885Sahrens * the stream. 3688789Sahrens */ 36892885Sahrens (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3690789Sahrens if (isprefix) { 36912885Sahrens /* 36922885Sahrens * They specified a fs with -d, we want to tack on 36932885Sahrens * everything but the pool name stored in the stream 36942885Sahrens */ 36952885Sahrens if (strchr(tosnap, '@')) { 36962885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36972885Sahrens "argument - snapshot not allowed with -d")); 36982885Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3699789Sahrens } 37002885Sahrens cp = strchr(chopprefix, '/'); 3701789Sahrens if (cp == NULL) 37022885Sahrens cp = strchr(chopprefix, '@'); 37032885Sahrens *cp = '\0'; 3704789Sahrens } else if (strchr(tosnap, '@') == NULL) { 3705789Sahrens /* 37062885Sahrens * If they specified a filesystem without -d, we want to 37072885Sahrens * tack on everything after the fs specified in the 37082885Sahrens * first name from the stream. 3709789Sahrens */ 37102885Sahrens cp = strchr(chopprefix, '@'); 37112885Sahrens *cp = '\0'; 3712789Sahrens } 37132885Sahrens choplen = strlen(chopprefix); 37142885Sahrens 37152885Sahrens /* 37162885Sahrens * Determine name of destination snapshot, store in zc_value. 37172885Sahrens */ 37182885Sahrens (void) strcpy(zc.zc_value, tosnap); 37192885Sahrens (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 37202885Sahrens sizeof (zc.zc_value)); 37213265Sahrens if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT)) 37223265Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37232885Sahrens 37242885Sahrens (void) strcpy(zc.zc_name, zc.zc_value); 3725789Sahrens if (drrb->drr_fromguid) { 3726789Sahrens /* incremental backup stream */ 37272885Sahrens zfs_handle_t *h; 37282885Sahrens 37292885Sahrens /* do the recvbackup ioctl to the containing fs */ 37302885Sahrens *strchr(zc.zc_name, '@') = '\0'; 3731789Sahrens 3732789Sahrens /* make sure destination fs exists */ 37332082Seschrock h = zfs_open(hdl, zc.zc_name, 37342082Seschrock ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 37352082Seschrock if (h == NULL) 3736789Sahrens return (-1); 3737868Sahrens if (!dryrun) { 37382665Snd150628 /* 37392665Snd150628 * We need to unmount all the dependents of the dataset 37402665Snd150628 * and the dataset itself. If it's a volume 37412665Snd150628 * then remove device link. 37422665Snd150628 */ 3743868Sahrens if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 37442665Snd150628 clp = changelist_gather(h, ZFS_PROP_NAME, 0); 37452665Snd150628 if (clp == NULL) 37462665Snd150628 return (-1); 37472665Snd150628 if (changelist_prefix(clp) != 0) { 37482665Snd150628 changelist_free(clp); 37492665Snd150628 return (-1); 37502665Snd150628 } 3751868Sahrens } else { 37524543Smarks if (zvol_remove_link(hdl, h->zfs_name) != 0) { 37534543Smarks zfs_close(h); 37544543Smarks return (-1); 37554543Smarks } 37564543Smarks 3757868Sahrens } 3758868Sahrens } 3759789Sahrens zfs_close(h); 3760789Sahrens } else { 3761789Sahrens /* full backup stream */ 3762789Sahrens 3763868Sahrens /* Make sure destination fs does not exist */ 37642885Sahrens *strchr(zc.zc_name, '@') = '\0'; 37654490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 37662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37672082Seschrock "destination '%s' exists"), zc.zc_name); 37682082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3769868Sahrens } 3770868Sahrens 37712885Sahrens if (strchr(zc.zc_name, '/') == NULL) { 37722885Sahrens /* 37732885Sahrens * they're trying to do a recv into a 37742885Sahrens * nonexistant topmost filesystem. 37752885Sahrens */ 37762885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37772885Sahrens "destination does not exist"), zc.zc_name); 37782885Sahrens return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 37792885Sahrens } 37802885Sahrens 3781868Sahrens /* Do the recvbackup ioctl to the fs's parent. */ 37822885Sahrens *strrchr(zc.zc_name, '/') = '\0'; 37832885Sahrens 37842885Sahrens if (isprefix && (err = create_parents(hdl, 37852885Sahrens zc.zc_value, strlen(tosnap))) != 0) { 37862885Sahrens return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 37872885Sahrens } 37882885Sahrens 3789789Sahrens } 3790789Sahrens 37913504Sahl zc.zc_cookie = infd; 37922676Seschrock zc.zc_guid = force; 3793789Sahrens if (verbose) { 37941749Sahrens (void) printf("%s %s stream of %s into %s\n", 37951749Sahrens dryrun ? "would receive" : "receiving", 3796789Sahrens drrb->drr_fromguid ? "incremental" : "full", 3797789Sahrens drr.drr_u.drr_begin.drr_toname, 37982676Seschrock zc.zc_value); 3799789Sahrens (void) fflush(stdout); 3800789Sahrens } 3801789Sahrens if (dryrun) 3802789Sahrens return (0); 38034543Smarks err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3804868Sahrens if (ioctl_err != 0) { 3805789Sahrens switch (errno) { 3806789Sahrens case ENODEV: 38072082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38082082Seschrock "most recent snapshot does not match incremental " 38092082Seschrock "source")); 38102082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3811789Sahrens break; 3812789Sahrens case ETXTBSY: 38132082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38142082Seschrock "destination has been modified since most recent " 38152082Seschrock "snapshot")); 38162082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3817789Sahrens break; 3818789Sahrens case EEXIST: 3819789Sahrens if (drrb->drr_fromguid == 0) { 3820789Sahrens /* it's the containing fs that exists */ 38212676Seschrock cp = strchr(zc.zc_value, '@'); 3822789Sahrens *cp = '\0'; 3823789Sahrens } 38242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38252082Seschrock "destination already exists")); 38263237Slling (void) zfs_error_fmt(hdl, EZFS_EXISTS, 38273237Slling dgettext(TEXT_DOMAIN, "cannot restore to %s"), 38283237Slling zc.zc_value); 3829789Sahrens break; 3830789Sahrens case EINVAL: 38312082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3832868Sahrens break; 38331544Seschrock case ECKSUM: 38342082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38352082Seschrock "invalid stream (checksum mismatch)")); 38362082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3837789Sahrens break; 3838789Sahrens default: 38392082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3840789Sahrens } 3841789Sahrens } 3842789Sahrens 3843789Sahrens /* 3844868Sahrens * Mount or recreate the /dev links for the target filesystem 3845868Sahrens * (if created, or if we tore them down to do an incremental 3846868Sahrens * restore), and the /dev links for the new snapshot (if 38472665Snd150628 * created). Also mount any children of the target filesystem 38482665Snd150628 * if we did an incremental receive. 3849789Sahrens */ 38502676Seschrock cp = strchr(zc.zc_value, '@'); 3851868Sahrens if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3852789Sahrens zfs_handle_t *h; 3853789Sahrens 3854789Sahrens *cp = '\0'; 38552676Seschrock h = zfs_open(hdl, zc.zc_value, 3856789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3857868Sahrens *cp = '@'; 3858789Sahrens if (h) { 38592665Snd150628 if (h->zfs_type == ZFS_TYPE_VOLUME) { 38602082Seschrock err = zvol_create_link(hdl, h->zfs_name); 38611544Seschrock if (err == 0 && ioctl_err == 0) 38622082Seschrock err = zvol_create_link(hdl, 38632676Seschrock zc.zc_value); 38642665Snd150628 } else { 38652665Snd150628 if (drrb->drr_fromguid) { 38662665Snd150628 err = changelist_postfix(clp); 38672665Snd150628 changelist_free(clp); 38682665Snd150628 } else { 38692665Snd150628 err = zfs_mount(h, NULL, 0); 38702665Snd150628 } 3871868Sahrens } 38722665Snd150628 zfs_close(h); 3873789Sahrens } 3874789Sahrens } 3875789Sahrens 3876868Sahrens if (err || ioctl_err) 3877868Sahrens return (-1); 3878789Sahrens 3879789Sahrens if (verbose) { 3880789Sahrens char buf1[64]; 3881789Sahrens char buf2[64]; 3882789Sahrens uint64_t bytes = zc.zc_cookie; 3883789Sahrens time_t delta = time(NULL) - begin_time; 3884789Sahrens if (delta == 0) 3885789Sahrens delta = 1; 3886789Sahrens zfs_nicenum(bytes, buf1, sizeof (buf1)); 3887789Sahrens zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3888789Sahrens 38891749Sahrens (void) printf("received %sb stream in %lu seconds (%sb/sec)\n", 3890789Sahrens buf1, delta, buf2); 3891789Sahrens } 38922665Snd150628 3893789Sahrens return (0); 3894789Sahrens } 3895789Sahrens 3896789Sahrens /* 38971294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 38981294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 38991294Slling * is a dependent and we should just destroy it without checking the transaction 39001294Slling * group. 3901789Sahrens */ 39021294Slling typedef struct rollback_data { 39031294Slling const char *cb_target; /* the snapshot */ 39041294Slling uint64_t cb_create; /* creation time reference */ 39051294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 39061294Slling int cb_error; 39072082Seschrock boolean_t cb_dependent; 39081294Slling } rollback_data_t; 39091294Slling 39101294Slling static int 39111294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 39121294Slling { 39131294Slling rollback_data_t *cbp = data; 39141294Slling 39151294Slling if (!cbp->cb_dependent) { 39161294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 39171294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 39181294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 39191294Slling cbp->cb_create) { 39204543Smarks char *logstr; 39211294Slling 39222082Seschrock cbp->cb_dependent = B_TRUE; 39232474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 39242474Seschrock cbp) != 0) 39252474Seschrock cbp->cb_error = 1; 39262082Seschrock cbp->cb_dependent = B_FALSE; 39271294Slling 39284543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 39294543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 39301294Slling if (zfs_destroy(zhp) != 0) 39311294Slling cbp->cb_error = 1; 39321294Slling else 39331294Slling changelist_remove(zhp, cbp->cb_clp); 39344543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 39351294Slling } 39361294Slling } else { 39371294Slling if (zfs_destroy(zhp) != 0) 39381294Slling cbp->cb_error = 1; 39391294Slling else 39401294Slling changelist_remove(zhp, cbp->cb_clp); 39411294Slling } 39421294Slling 39431294Slling zfs_close(zhp); 39441294Slling return (0); 39451294Slling } 39461294Slling 39471294Slling /* 39481294Slling * Rollback the dataset to its latest snapshot. 39491294Slling */ 39501294Slling static int 39511294Slling do_rollback(zfs_handle_t *zhp) 3952789Sahrens { 3953789Sahrens int ret; 3954789Sahrens zfs_cmd_t zc = { 0 }; 3955789Sahrens 3956789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3957789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3958789Sahrens 3959789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 39602082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3961789Sahrens return (-1); 3962789Sahrens 3963789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3964789Sahrens 39652676Seschrock if (ZFS_IS_VOLUME(zhp)) 3966789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3967789Sahrens else 3968789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3969789Sahrens 3970789Sahrens /* 3971789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3972789Sahrens * for the given dataset. Given these constraints, we can simply pass 3973789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3974789Sahrens * condition where the user has taken a snapshot since we verified that 3975789Sahrens * this was the most recent. 3976789Sahrens */ 39774543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 39783237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 39792082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 39802082Seschrock zhp->zfs_name); 3981789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 39822082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3983789Sahrens } 3984789Sahrens 3985789Sahrens return (ret); 3986789Sahrens } 3987789Sahrens 3988789Sahrens /* 39891294Slling * Given a dataset, rollback to a specific snapshot, discarding any 39901294Slling * data changes since then and making it the active dataset. 39911294Slling * 39921294Slling * Any snapshots more recent than the target are destroyed, along with 39931294Slling * their dependents. 39941294Slling */ 39951294Slling int 39961294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 39971294Slling { 39981294Slling int ret; 39991294Slling rollback_data_t cb = { 0 }; 40001294Slling prop_changelist_t *clp; 40011294Slling 40021294Slling /* 40031294Slling * Unmount all dependendents of the dataset and the dataset itself. 40041294Slling * The list we need to gather is the same as for doing rename 40051294Slling */ 40061294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 40071294Slling if (clp == NULL) 40081294Slling return (-1); 40091294Slling 40101294Slling if ((ret = changelist_prefix(clp)) != 0) 40111294Slling goto out; 40121294Slling 40131294Slling /* 40141294Slling * Destroy all recent snapshots and its dependends. 40151294Slling */ 40161294Slling cb.cb_target = snap->zfs_name; 40171294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 40181294Slling cb.cb_clp = clp; 40191294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 40201294Slling 40211294Slling if ((ret = cb.cb_error) != 0) { 40221294Slling (void) changelist_postfix(clp); 40231294Slling goto out; 40241294Slling } 40251294Slling 40261294Slling /* 40271294Slling * Now that we have verified that the snapshot is the latest, 40281294Slling * rollback to the given snapshot. 40291294Slling */ 40301294Slling ret = do_rollback(zhp); 40311294Slling 40321294Slling if (ret != 0) { 40331294Slling (void) changelist_postfix(clp); 40341294Slling goto out; 40351294Slling } 40361294Slling 40371294Slling /* 40381294Slling * We only want to re-mount the filesystem if it was mounted in the 40391294Slling * first place. 40401294Slling */ 40411294Slling ret = changelist_postfix(clp); 40421294Slling 40431294Slling out: 40441294Slling changelist_free(clp); 40451294Slling return (ret); 40461294Slling } 40471294Slling 40481294Slling /* 4049789Sahrens * Iterate over all dependents for a given dataset. This includes both 4050789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 4051789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 4052789Sahrens * libzfs_graph.c. 4053789Sahrens */ 4054789Sahrens int 40552474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 40562474Seschrock zfs_iter_f func, void *data) 4057789Sahrens { 4058789Sahrens char **dependents; 4059789Sahrens size_t count; 4060789Sahrens int i; 4061789Sahrens zfs_handle_t *child; 4062789Sahrens int ret = 0; 4063789Sahrens 40642474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 40652474Seschrock &dependents, &count) != 0) 40662474Seschrock return (-1); 40672474Seschrock 4068789Sahrens for (i = 0; i < count; i++) { 40692082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 40702082Seschrock dependents[i])) == NULL) 4071789Sahrens continue; 4072789Sahrens 4073789Sahrens if ((ret = func(child, data)) != 0) 4074789Sahrens break; 4075789Sahrens } 4076789Sahrens 4077789Sahrens for (i = 0; i < count; i++) 4078789Sahrens free(dependents[i]); 4079789Sahrens free(dependents); 4080789Sahrens 4081789Sahrens return (ret); 4082789Sahrens } 4083789Sahrens 4084789Sahrens /* 4085789Sahrens * Renames the given dataset. 4086789Sahrens */ 4087789Sahrens int 40884490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 4089789Sahrens { 4090789Sahrens int ret; 4091789Sahrens zfs_cmd_t zc = { 0 }; 4092789Sahrens char *delim; 40934007Smmusante prop_changelist_t *cl = NULL; 40944007Smmusante zfs_handle_t *zhrp = NULL; 40954007Smmusante char *parentname = NULL; 4096789Sahrens char parent[ZFS_MAXNAMELEN]; 40972082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 40982082Seschrock char errbuf[1024]; 4099789Sahrens 4100789Sahrens /* if we have the same exact name, just return success */ 4101789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 4102789Sahrens return (0); 4103789Sahrens 41042082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 41052082Seschrock "cannot rename to '%s'"), target); 41062082Seschrock 4107789Sahrens /* 4108789Sahrens * Make sure the target name is valid 4109789Sahrens */ 4110789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 41112665Snd150628 if ((strchr(target, '@') == NULL) || 41122665Snd150628 *target == '@') { 41132665Snd150628 /* 41142665Snd150628 * Snapshot target name is abbreviated, 41152665Snd150628 * reconstruct full dataset name 41162665Snd150628 */ 41172665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 41182665Snd150628 sizeof (parent)); 41192665Snd150628 delim = strchr(parent, '@'); 41202665Snd150628 if (strchr(target, '@') == NULL) 41212665Snd150628 *(++delim) = '\0'; 41222665Snd150628 else 41232665Snd150628 *delim = '\0'; 41242665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 41252665Snd150628 target = parent; 41262665Snd150628 } else { 41272665Snd150628 /* 41282665Snd150628 * Make sure we're renaming within the same dataset. 41292665Snd150628 */ 41302665Snd150628 delim = strchr(target, '@'); 41312665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 41322665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 41332665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41342665Snd150628 "snapshots must be part of same " 41352665Snd150628 "dataset")); 41362665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 41373912Slling errbuf)); 41382665Snd150628 } 4139789Sahrens } 41402665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 41412665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4142789Sahrens } else { 41434007Smmusante if (recursive) { 41444007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41454007Smmusante "recursive rename must be a snapshot")); 41464007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 41474007Smmusante } 41484007Smmusante 41492665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 41502665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 41512676Seschrock uint64_t unused; 41522676Seschrock 4153789Sahrens /* validate parents */ 41544490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4155789Sahrens return (-1); 4156789Sahrens 4157789Sahrens (void) parent_name(target, parent, sizeof (parent)); 4158789Sahrens 4159789Sahrens /* make sure we're in the same pool */ 4160789Sahrens verify((delim = strchr(target, '/')) != NULL); 4161789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4162789Sahrens zhp->zfs_name[delim - target] != '/') { 41632082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41642082Seschrock "datasets must be within same pool")); 41652082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4166789Sahrens } 41672440Snd150628 41682440Snd150628 /* new name cannot be a child of the current dataset name */ 41692440Snd150628 if (strncmp(parent, zhp->zfs_name, 41703912Slling strlen(zhp->zfs_name)) == 0) { 41712440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41722440Snd150628 "New dataset name cannot be a descendent of " 41732440Snd150628 "current dataset name")); 41742440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 41752440Snd150628 } 4176789Sahrens } 4177789Sahrens 41782082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 41792082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 41802082Seschrock 4181789Sahrens if (getzoneid() == GLOBAL_ZONEID && 4182789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 41832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41842082Seschrock "dataset is used in a non-global zone")); 41852082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4186789Sahrens } 4187789Sahrens 41884007Smmusante if (recursive) { 41894007Smmusante struct destroydata dd; 41904007Smmusante 41914183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 41924183Smmusante if (parentname == NULL) { 41934183Smmusante ret = -1; 41944183Smmusante goto error; 41954183Smmusante } 41964007Smmusante delim = strchr(parentname, '@'); 41974007Smmusante *delim = '\0'; 41984007Smmusante zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY); 41994007Smmusante if (zhrp == NULL) { 42004183Smmusante ret = -1; 42014183Smmusante goto error; 42024007Smmusante } 42034007Smmusante 42044007Smmusante dd.snapname = delim + 1; 42054007Smmusante dd.gotone = B_FALSE; 42064183Smmusante dd.closezhp = B_TRUE; 42074007Smmusante 42084007Smmusante /* We remove any zvol links prior to renaming them */ 42094007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 42104007Smmusante if (ret) { 42114007Smmusante goto error; 42124007Smmusante } 42134007Smmusante } else { 42144007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 42154007Smmusante return (-1); 42164007Smmusante 42174007Smmusante if (changelist_haszonedchild(cl)) { 42184007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42194007Smmusante "child dataset with inherited mountpoint is used " 42204007Smmusante "in a non-global zone")); 42214007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 42224007Smmusante goto error; 42234007Smmusante } 42244007Smmusante 42254007Smmusante if ((ret = changelist_prefix(cl)) != 0) 42264007Smmusante goto error; 4227789Sahrens } 4228789Sahrens 42292676Seschrock if (ZFS_IS_VOLUME(zhp)) 4230789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4231789Sahrens else 4232789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4233789Sahrens 42342665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 42352676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 42362665Snd150628 42374007Smmusante zc.zc_cookie = recursive; 42384007Smmusante 42394543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 42404007Smmusante /* 42414007Smmusante * if it was recursive, the one that actually failed will 42424007Smmusante * be in zc.zc_name 42434007Smmusante */ 42444007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 42454007Smmusante "cannot rename to '%s'"), zc.zc_name); 42464007Smmusante 42474007Smmusante if (recursive && errno == EEXIST) { 42484007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42494007Smmusante "a child dataset already has a snapshot " 42504007Smmusante "with the new name")); 42514007Smmusante (void) zfs_error(hdl, EZFS_CROSSTARGET, errbuf); 42524007Smmusante } else { 42534007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 42544007Smmusante } 4255789Sahrens 4256789Sahrens /* 4257789Sahrens * On failure, we still want to remount any filesystems that 4258789Sahrens * were previously mounted, so we don't alter the system state. 4259789Sahrens */ 42604007Smmusante if (recursive) { 42614007Smmusante struct createdata cd; 42624007Smmusante 42634007Smmusante /* only create links for datasets that had existed */ 42644007Smmusante cd.cd_snapname = delim + 1; 42654007Smmusante cd.cd_ifexists = B_TRUE; 42664007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 42674007Smmusante &cd); 42684007Smmusante } else { 42694007Smmusante (void) changelist_postfix(cl); 42704007Smmusante } 4271789Sahrens } else { 42724007Smmusante if (recursive) { 42734007Smmusante struct createdata cd; 42744007Smmusante 42754007Smmusante /* only create links for datasets that had existed */ 42764007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 42774007Smmusante cd.cd_ifexists = B_TRUE; 42784007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 42794007Smmusante &cd); 42804007Smmusante } else { 42814007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 42824007Smmusante ret = changelist_postfix(cl); 42834007Smmusante } 4284789Sahrens } 4285789Sahrens 4286789Sahrens error: 42874007Smmusante if (parentname) { 42884007Smmusante free(parentname); 42894007Smmusante } 42904007Smmusante if (zhrp) { 42914007Smmusante zfs_close(zhrp); 42924007Smmusante } 42934007Smmusante if (cl) { 42944007Smmusante changelist_free(cl); 42954007Smmusante } 4296789Sahrens return (ret); 4297789Sahrens } 4298789Sahrens 4299789Sahrens /* 4300789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4301789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 4302789Sahrens */ 4303789Sahrens int 43042082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4305789Sahrens { 43064007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 43074007Smmusante } 43084007Smmusante 43094007Smmusante static int 43104007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 43114007Smmusante { 4312789Sahrens zfs_cmd_t zc = { 0 }; 43132082Seschrock di_devlink_handle_t dhdl; 43144543Smarks priv_set_t *priv_effective; 43154543Smarks int privileged; 4316789Sahrens 4317789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4318789Sahrens 4319789Sahrens /* 4320789Sahrens * Issue the appropriate ioctl. 4321789Sahrens */ 43222082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4323789Sahrens switch (errno) { 4324789Sahrens case EEXIST: 4325789Sahrens /* 4326789Sahrens * Silently ignore the case where the link already 4327789Sahrens * exists. This allows 'zfs volinit' to be run multiple 4328789Sahrens * times without errors. 4329789Sahrens */ 4330789Sahrens return (0); 4331789Sahrens 43324007Smmusante case ENOENT: 43334007Smmusante /* 43344007Smmusante * Dataset does not exist in the kernel. If we 43354007Smmusante * don't care (see zfs_rename), then ignore the 43364007Smmusante * error quietly. 43374007Smmusante */ 43384007Smmusante if (ifexists) { 43394007Smmusante return (0); 43404007Smmusante } 43414007Smmusante 43424007Smmusante /* FALLTHROUGH */ 43434007Smmusante 4344789Sahrens default: 43453237Slling return (zfs_standard_error_fmt(hdl, errno, 43462082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 43472082Seschrock "for '%s'"), dataset)); 4348789Sahrens } 4349789Sahrens } 4350789Sahrens 4351789Sahrens /* 43524543Smarks * If privileged call devfsadm and wait for the links to 43534543Smarks * magically appear. 43544543Smarks * Otherwise, print out an informational message. 4355789Sahrens */ 43564543Smarks 43574543Smarks priv_effective = priv_allocset(); 43584543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 43594543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 43604543Smarks priv_freeset(priv_effective); 43614543Smarks 43624543Smarks if (privileged) { 43634543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 43644543Smarks DI_MAKE_LINK)) == NULL) { 43654543Smarks zfs_error_aux(hdl, strerror(errno)); 43664543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 43674543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 43684543Smarks "for '%s'"), dataset); 43694543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 43704543Smarks return (-1); 43714543Smarks } else { 43724543Smarks (void) di_devlink_fini(&dhdl); 43734543Smarks } 4374789Sahrens } else { 43754543Smarks char pathname[MAXPATHLEN]; 43764543Smarks struct stat64 statbuf; 43774543Smarks int i; 43784543Smarks 43794543Smarks #define MAX_WAIT 10 43804543Smarks 43814543Smarks /* 43824543Smarks * This is the poor mans way of waiting for the link 43834543Smarks * to show up. If after 10 seconds we still don't 43844543Smarks * have it, then print out a message. 43854543Smarks */ 43864543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 43874543Smarks dataset); 43884543Smarks 43894543Smarks for (i = 0; i != MAX_WAIT; i++) { 43904543Smarks if (stat64(pathname, &statbuf) == 0) 43914543Smarks break; 43924543Smarks (void) sleep(1); 43934543Smarks } 43944543Smarks if (i == MAX_WAIT) 43954543Smarks (void) printf(gettext("%s may not be immediately " 43964543Smarks "available\n"), pathname); 4397789Sahrens } 4398789Sahrens 4399789Sahrens return (0); 4400789Sahrens } 4401789Sahrens 4402789Sahrens /* 4403789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4404789Sahrens */ 4405789Sahrens int 44062082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4407789Sahrens { 4408789Sahrens zfs_cmd_t zc = { 0 }; 4409789Sahrens 4410789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4411789Sahrens 44122082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4413789Sahrens switch (errno) { 4414789Sahrens case ENXIO: 4415789Sahrens /* 4416789Sahrens * Silently ignore the case where the link no longer 4417789Sahrens * exists, so that 'zfs volfini' can be run multiple 4418789Sahrens * times without errors. 4419789Sahrens */ 4420789Sahrens return (0); 4421789Sahrens 4422789Sahrens default: 44233237Slling return (zfs_standard_error_fmt(hdl, errno, 44242082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 44252082Seschrock "links for '%s'"), dataset)); 4426789Sahrens } 4427789Sahrens } 4428789Sahrens 4429789Sahrens return (0); 4430789Sahrens } 44312676Seschrock 44322676Seschrock nvlist_t * 44332676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 44342676Seschrock { 44352676Seschrock return (zhp->zfs_user_props); 44362676Seschrock } 44372676Seschrock 44382676Seschrock /* 44394451Seschrock * Given a comma-separated list of properties, construct a property list 44402676Seschrock * containing both user-defined and native properties. This function will 44412676Seschrock * return a NULL list if 'all' is specified, which can later be expanded on a 44422676Seschrock * per-dataset basis by zfs_expand_proplist(). 44432676Seschrock */ 44442676Seschrock int 44453912Slling zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields, 44463912Slling zfs_proplist_t **listp, zfs_type_t type) 44472676Seschrock { 44482676Seschrock size_t len; 44492676Seschrock char *s, *p; 44502676Seschrock char c; 44512676Seschrock zfs_prop_t prop; 44522676Seschrock zfs_proplist_t *entry; 44532676Seschrock zfs_proplist_t **last; 44542676Seschrock 44552676Seschrock *listp = NULL; 44562676Seschrock last = listp; 44572676Seschrock 44582676Seschrock /* 44592676Seschrock * If 'all' is specified, return a NULL list. 44602676Seschrock */ 44612676Seschrock if (strcmp(fields, "all") == 0) 44622676Seschrock return (0); 44632676Seschrock 44642676Seschrock /* 44652676Seschrock * If no fields were specified, return an error. 44662676Seschrock */ 44672676Seschrock if (fields[0] == '\0') { 44682676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44692676Seschrock "no properties specified")); 44702676Seschrock return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 44712676Seschrock "bad property list"))); 44722676Seschrock } 44732676Seschrock 44742676Seschrock /* 44752676Seschrock * It would be nice to use getsubopt() here, but the inclusion of column 44762676Seschrock * aliases makes this more effort than it's worth. 44772676Seschrock */ 44782676Seschrock s = fields; 44792676Seschrock while (*s != '\0') { 44802676Seschrock if ((p = strchr(s, ',')) == NULL) { 44812676Seschrock len = strlen(s); 44822676Seschrock p = s + len; 44832676Seschrock } else { 44842676Seschrock len = p - s; 44852676Seschrock } 44862676Seschrock 44872676Seschrock /* 44882676Seschrock * Check for empty options. 44892676Seschrock */ 44902676Seschrock if (len == 0) { 44912676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44922676Seschrock "empty property name")); 44932676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 44942676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 44952676Seschrock } 44962676Seschrock 44972676Seschrock /* 44982676Seschrock * Check all regular property names. 44992676Seschrock */ 45002676Seschrock c = s[len]; 45012676Seschrock s[len] = '\0'; 45024451Seschrock prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) : 45034451Seschrock zfs_name_to_prop(s); 45043912Slling 45053912Slling if (prop != ZFS_PROP_INVAL && 45063912Slling !zfs_prop_valid_for_type(prop, type)) 45073912Slling prop = ZFS_PROP_INVAL; 45082676Seschrock 45092676Seschrock /* 45103912Slling * When no property table entry can be found, return failure if 45113912Slling * this is a pool property or if this isn't a user-defined 45123912Slling * dataset property, 45132676Seschrock */ 45143912Slling if (prop == ZFS_PROP_INVAL && 45153912Slling (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) { 45162676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45172676Seschrock "invalid property '%s'"), s); 45182676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 45192676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 45202676Seschrock } 45212676Seschrock 45222676Seschrock if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL) 45232676Seschrock return (-1); 45242676Seschrock 45252676Seschrock entry->pl_prop = prop; 45262676Seschrock if (prop == ZFS_PROP_INVAL) { 45272676Seschrock if ((entry->pl_user_prop = 45282676Seschrock zfs_strdup(hdl, s)) == NULL) { 45292676Seschrock free(entry); 45302676Seschrock return (-1); 45312676Seschrock } 45322676Seschrock entry->pl_width = strlen(s); 45332676Seschrock } else { 45342676Seschrock entry->pl_width = zfs_prop_width(prop, 45352676Seschrock &entry->pl_fixed); 45362676Seschrock } 45372676Seschrock 45382676Seschrock *last = entry; 45392676Seschrock last = &entry->pl_next; 45402676Seschrock 45412676Seschrock s = p; 45422676Seschrock if (c == ',') 45432676Seschrock s++; 45442676Seschrock } 45452676Seschrock 45462676Seschrock return (0); 45472676Seschrock } 45482676Seschrock 45493912Slling int 45503912Slling zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp) 45513912Slling { 45523912Slling return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY)); 45533912Slling } 45543912Slling 45552676Seschrock void 45562676Seschrock zfs_free_proplist(zfs_proplist_t *pl) 45572676Seschrock { 45582676Seschrock zfs_proplist_t *next; 45592676Seschrock 45602676Seschrock while (pl != NULL) { 45612676Seschrock next = pl->pl_next; 45622676Seschrock free(pl->pl_user_prop); 45632676Seschrock free(pl); 45642676Seschrock pl = next; 45652676Seschrock } 45662676Seschrock } 45672676Seschrock 45683654Sgw25295 typedef struct expand_data { 45693654Sgw25295 zfs_proplist_t **last; 45703654Sgw25295 libzfs_handle_t *hdl; 45713654Sgw25295 } expand_data_t; 45723654Sgw25295 45733654Sgw25295 static zfs_prop_t 45743654Sgw25295 zfs_expand_proplist_cb(zfs_prop_t prop, void *cb) 45753654Sgw25295 { 45763654Sgw25295 zfs_proplist_t *entry; 45773654Sgw25295 expand_data_t *edp = cb; 45783654Sgw25295 45793654Sgw25295 if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL) 45803654Sgw25295 return (ZFS_PROP_INVAL); 45813654Sgw25295 45823654Sgw25295 entry->pl_prop = prop; 45833654Sgw25295 entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed); 45843654Sgw25295 entry->pl_all = B_TRUE; 45853654Sgw25295 45863654Sgw25295 *(edp->last) = entry; 45873654Sgw25295 edp->last = &entry->pl_next; 45883654Sgw25295 45893654Sgw25295 return (ZFS_PROP_CONT); 45903654Sgw25295 } 45913654Sgw25295 45922676Seschrock int 45933912Slling zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp, 45943912Slling zfs_type_t type) 45952676Seschrock { 45962676Seschrock zfs_proplist_t *entry; 45973912Slling zfs_proplist_t **last; 45983654Sgw25295 expand_data_t exp; 45992676Seschrock 46002676Seschrock if (*plp == NULL) { 46012676Seschrock /* 46022676Seschrock * If this is the very first time we've been called for an 'all' 46032676Seschrock * specification, expand the list to include all native 46042676Seschrock * properties. 46052676Seschrock */ 46062676Seschrock last = plp; 46073654Sgw25295 46083654Sgw25295 exp.last = last; 46093654Sgw25295 exp.hdl = hdl; 46103654Sgw25295 46113912Slling if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type, 46123654Sgw25295 B_FALSE) == ZFS_PROP_INVAL) 46133654Sgw25295 return (-1); 46142676Seschrock 46152676Seschrock /* 46162676Seschrock * Add 'name' to the beginning of the list, which is handled 46172676Seschrock * specially. 46182676Seschrock */ 46192676Seschrock if ((entry = zfs_alloc(hdl, 46202676Seschrock sizeof (zfs_proplist_t))) == NULL) 46212676Seschrock return (-1); 46222676Seschrock 46232676Seschrock entry->pl_prop = ZFS_PROP_NAME; 46242676Seschrock entry->pl_width = zfs_prop_width(ZFS_PROP_NAME, 46252676Seschrock &entry->pl_fixed); 46262676Seschrock entry->pl_all = B_TRUE; 46272676Seschrock entry->pl_next = *plp; 46282676Seschrock *plp = entry; 46292676Seschrock } 46303912Slling return (0); 46313912Slling } 46323912Slling 46333912Slling /* 46343912Slling * This function is used by 'zfs list' to determine the exact set of columns to 46353912Slling * display, and their maximum widths. This does two main things: 46363912Slling * 46373912Slling * - If this is a list of all properties, then expand the list to include 46383912Slling * all native properties, and set a flag so that for each dataset we look 46393912Slling * for new unique user properties and add them to the list. 46403912Slling * 46413912Slling * - For non fixed-width properties, keep track of the maximum width seen 46423912Slling * so that we can size the column appropriately. 46433912Slling */ 46443912Slling int 46453912Slling zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp) 46463912Slling { 46473912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 46483912Slling zfs_proplist_t *entry; 46493912Slling zfs_proplist_t **last, **start; 46503912Slling nvlist_t *userprops, *propval; 46513912Slling nvpair_t *elem; 46523912Slling char *strval; 46533912Slling char buf[ZFS_MAXPROPLEN]; 46543912Slling 46553912Slling if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0) 46563912Slling return (-1); 46572676Seschrock 46582676Seschrock userprops = zfs_get_user_props(zhp); 46592676Seschrock 46602676Seschrock entry = *plp; 46612676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 46622676Seschrock /* 46632676Seschrock * Go through and add any user properties as necessary. We 46642676Seschrock * start by incrementing our list pointer to the first 46652676Seschrock * non-native property. 46662676Seschrock */ 46672676Seschrock start = plp; 46682676Seschrock while (*start != NULL) { 46692676Seschrock if ((*start)->pl_prop == ZFS_PROP_INVAL) 46702676Seschrock break; 46712676Seschrock start = &(*start)->pl_next; 46722676Seschrock } 46732676Seschrock 46742676Seschrock elem = NULL; 46752676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 46762676Seschrock /* 46772676Seschrock * See if we've already found this property in our list. 46782676Seschrock */ 46792676Seschrock for (last = start; *last != NULL; 46802676Seschrock last = &(*last)->pl_next) { 46812676Seschrock if (strcmp((*last)->pl_user_prop, 46822676Seschrock nvpair_name(elem)) == 0) 46832676Seschrock break; 46842676Seschrock } 46852676Seschrock 46862676Seschrock if (*last == NULL) { 46872676Seschrock if ((entry = zfs_alloc(hdl, 46882676Seschrock sizeof (zfs_proplist_t))) == NULL || 46892676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 46902676Seschrock nvpair_name(elem)))) == NULL) { 46912676Seschrock free(entry); 46922676Seschrock return (-1); 46932676Seschrock } 46942676Seschrock 46952676Seschrock entry->pl_prop = ZFS_PROP_INVAL; 46962676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 46972676Seschrock entry->pl_all = B_TRUE; 46982676Seschrock *last = entry; 46992676Seschrock } 47002676Seschrock } 47012676Seschrock } 47022676Seschrock 47032676Seschrock /* 47042676Seschrock * Now go through and check the width of any non-fixed columns 47052676Seschrock */ 47062676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 47072676Seschrock if (entry->pl_fixed) 47082676Seschrock continue; 47092676Seschrock 47102676Seschrock if (entry->pl_prop != ZFS_PROP_INVAL) { 47112676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 47122676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 47132676Seschrock if (strlen(buf) > entry->pl_width) 47142676Seschrock entry->pl_width = strlen(buf); 47152676Seschrock } 47162676Seschrock } else if (nvlist_lookup_nvlist(userprops, 47172676Seschrock entry->pl_user_prop, &propval) == 0) { 47182676Seschrock verify(nvlist_lookup_string(propval, 47192676Seschrock ZFS_PROP_VALUE, &strval) == 0); 47202676Seschrock if (strlen(strval) > entry->pl_width) 47212676Seschrock entry->pl_width = strlen(strval); 47222676Seschrock } 47232676Seschrock } 47242676Seschrock 47252676Seschrock return (0); 47262676Seschrock } 47274543Smarks 47284543Smarks int 47294543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 47304543Smarks { 47314543Smarks zfs_cmd_t zc = { 0 }; 47324543Smarks nvlist_t *nvp; 47334543Smarks size_t sz; 47344543Smarks gid_t gid; 47354543Smarks uid_t uid; 47364543Smarks const gid_t *groups; 47374543Smarks int group_cnt; 47384543Smarks int error; 47394543Smarks 47404543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 47414543Smarks return (no_memory(hdl)); 47424543Smarks 47434543Smarks uid = ucred_geteuid(cred); 47444543Smarks gid = ucred_getegid(cred); 47454543Smarks group_cnt = ucred_getgroups(cred, &groups); 47464543Smarks 47474543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 47484543Smarks return (1); 47494543Smarks 47504543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 47514543Smarks nvlist_free(nvp); 47524543Smarks return (1); 47534543Smarks } 47544543Smarks 47554543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 47564543Smarks nvlist_free(nvp); 47574543Smarks return (1); 47584543Smarks } 47594543Smarks 47604543Smarks if (nvlist_add_uint32_array(nvp, 47614543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 47624543Smarks nvlist_free(nvp); 47634543Smarks return (1); 47644543Smarks } 47654543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 47664543Smarks 47674543Smarks if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz)) 47684543Smarks return (-1); 47694543Smarks 47704543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 47714543Smarks nvlist_free(nvp); 47724543Smarks return (error); 47734543Smarks } 47744543Smarks 47754543Smarks int 47764543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 47774543Smarks void *export, void *sharetab, int sharemax, boolean_t share_on) 47784543Smarks { 47794543Smarks zfs_cmd_t zc = { 0 }; 47804543Smarks int error; 47814543Smarks 47824543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 47834543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 47844543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 47854543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 47864543Smarks zc.zc_share.z_sharetype = share_on; 47874543Smarks zc.zc_share.z_sharemax = sharemax; 47884543Smarks 47894543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 47904543Smarks return (error); 47914543Smarks } 4792