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) { 9014577Sahrens case ZFS_PROP_VERSION: 9024577Sahrens { 9034577Sahrens int version; 9044577Sahrens 9054577Sahrens if (zhp == NULL) 9064577Sahrens break; 9074577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 9084577Sahrens if (intval < version) { 9094577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9104577Sahrens "Can not downgrade; already at version %u"), 9114577Sahrens version); 9124577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9134577Sahrens goto error; 9144577Sahrens } 9154577Sahrens break; 9164577Sahrens } 9174577Sahrens 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 } 16604577Sahrens 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, 1993*4603Sahrens "pool must be upgraded to set this " 1994*4603Sahrens "property or value")); 19953886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 19963886Sahl break; 19973886Sahl 1998789Sahrens case EOVERFLOW: 1999789Sahrens /* 2000789Sahrens * This platform can't address a volume this big. 2001789Sahrens */ 2002789Sahrens #ifdef _ILP32 2003789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 20042082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 2005789Sahrens break; 2006789Sahrens } 2007789Sahrens #endif 20082082Seschrock /* FALLTHROUGH */ 2009789Sahrens default: 20102082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 2011789Sahrens } 2012789Sahrens } else { 2013789Sahrens /* 2014789Sahrens * Refresh the statistics so the new property value 2015789Sahrens * is reflected. 2016789Sahrens */ 20172676Seschrock if ((ret = changelist_postfix(cl)) == 0) 20182676Seschrock (void) get_stats(zhp); 2019789Sahrens } 2020789Sahrens 2021789Sahrens error: 20222676Seschrock nvlist_free(nvl); 20232676Seschrock zcmd_free_nvlists(&zc); 20242676Seschrock if (cl) 20252676Seschrock changelist_free(cl); 2026789Sahrens return (ret); 2027789Sahrens } 2028789Sahrens 2029789Sahrens /* 2030789Sahrens * Given a property, inherit the value from the parent dataset. 2031789Sahrens */ 2032789Sahrens int 20332676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 2034789Sahrens { 2035789Sahrens zfs_cmd_t zc = { 0 }; 2036789Sahrens int ret; 2037789Sahrens prop_changelist_t *cl; 20382082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 20392082Seschrock char errbuf[1024]; 20402676Seschrock zfs_prop_t prop; 20412082Seschrock 20422082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 20432082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 2044789Sahrens 20452676Seschrock if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) { 20462676Seschrock /* 20472676Seschrock * For user properties, the amount of work we have to do is very 20482676Seschrock * small, so just do it here. 20492676Seschrock */ 20502676Seschrock if (!zfs_prop_user(propname)) { 20512676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20522676Seschrock "invalid property")); 20532676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 20542676Seschrock } 20552676Seschrock 20562676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 20572676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 20582676Seschrock 20594543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc) != 0) 20602676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 20612676Seschrock 20622676Seschrock return (0); 20632676Seschrock } 20642676Seschrock 2065789Sahrens /* 2066789Sahrens * Verify that this property is inheritable. 2067789Sahrens */ 20682082Seschrock if (zfs_prop_readonly(prop)) 20692082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 20702082Seschrock 20712082Seschrock if (!zfs_prop_inheritable(prop)) 20722082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2073789Sahrens 2074789Sahrens /* 2075789Sahrens * Check to see if the value applies to this type 2076789Sahrens */ 20772082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 20782082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2079789Sahrens 20803443Srm160521 /* 20813443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 20823443Srm160521 */ 20833443Srm160521 propname = zfs_prop_to_name(prop); 2084789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 20852676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2086789Sahrens 2087789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2088789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 20892082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20902082Seschrock "dataset is used in a non-global zone")); 20912082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2092789Sahrens } 2093789Sahrens 2094789Sahrens /* 2095789Sahrens * Determine datasets which will be affected by this change, if any. 2096789Sahrens */ 2097789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 2098789Sahrens return (-1); 2099789Sahrens 2100789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 21012082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21022082Seschrock "child dataset with inherited mountpoint is used " 21032082Seschrock "in a non-global zone")); 21042082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2105789Sahrens goto error; 2106789Sahrens } 2107789Sahrens 2108789Sahrens if ((ret = changelist_prefix(cl)) != 0) 2109789Sahrens goto error; 2110789Sahrens 21114543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc)) != 0) { 21122082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2113789Sahrens } else { 2114789Sahrens 21152169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 2116789Sahrens goto error; 2117789Sahrens 2118789Sahrens /* 2119789Sahrens * Refresh the statistics so the new property is reflected. 2120789Sahrens */ 2121789Sahrens (void) get_stats(zhp); 2122789Sahrens } 2123789Sahrens 2124789Sahrens error: 2125789Sahrens changelist_free(cl); 2126789Sahrens return (ret); 2127789Sahrens } 2128789Sahrens 21293912Slling void 2130789Sahrens nicebool(int value, char *buf, size_t buflen) 2131789Sahrens { 2132789Sahrens if (value) 2133789Sahrens (void) strlcpy(buf, "on", buflen); 2134789Sahrens else 2135789Sahrens (void) strlcpy(buf, "off", buflen); 2136789Sahrens } 2137789Sahrens 2138789Sahrens /* 21391356Seschrock * True DSL properties are stored in an nvlist. The following two functions 21401356Seschrock * extract them appropriately. 21411356Seschrock */ 21421356Seschrock static uint64_t 21431356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 21441356Seschrock { 21451356Seschrock nvlist_t *nv; 21461356Seschrock uint64_t value; 21471356Seschrock 21482885Sahrens *source = NULL; 21491356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 21501356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 21511356Seschrock verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0); 21522885Sahrens (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 21531356Seschrock } else { 21541356Seschrock value = zfs_prop_default_numeric(prop); 21551356Seschrock *source = ""; 21561356Seschrock } 21571356Seschrock 21581356Seschrock return (value); 21591356Seschrock } 21601356Seschrock 21611356Seschrock static char * 21621356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 21631356Seschrock { 21641356Seschrock nvlist_t *nv; 21651356Seschrock char *value; 21661356Seschrock 21672885Sahrens *source = NULL; 21681356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 21691356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 21701356Seschrock verify(nvlist_lookup_string(nv, ZFS_PROP_VALUE, &value) == 0); 21712885Sahrens (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 21721356Seschrock } else { 21731356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 21741356Seschrock value = ""; 21751356Seschrock *source = ""; 21761356Seschrock } 21771356Seschrock 21781356Seschrock return (value); 21791356Seschrock } 21801356Seschrock 21811356Seschrock /* 2182789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2183789Sahrens * zfs_prop_get_int() are built using this interface. 2184789Sahrens * 2185789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2186789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2187789Sahrens * If they differ from the on-disk values, report the current values and mark 2188789Sahrens * the source "temporary". 2189789Sahrens */ 21902082Seschrock static int 2191789Sahrens get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src, 21922082Seschrock char **source, uint64_t *val) 2193789Sahrens { 2194789Sahrens struct mnttab mnt; 21953265Sahrens char *mntopt_on = NULL; 21963265Sahrens char *mntopt_off = NULL; 2197789Sahrens 2198789Sahrens *source = NULL; 2199789Sahrens 22003265Sahrens switch (prop) { 22013265Sahrens case ZFS_PROP_ATIME: 22023265Sahrens mntopt_on = MNTOPT_ATIME; 22033265Sahrens mntopt_off = MNTOPT_NOATIME; 22043265Sahrens break; 22053265Sahrens 22063265Sahrens case ZFS_PROP_DEVICES: 22073265Sahrens mntopt_on = MNTOPT_DEVICES; 22083265Sahrens mntopt_off = MNTOPT_NODEVICES; 22093265Sahrens break; 22103265Sahrens 22113265Sahrens case ZFS_PROP_EXEC: 22123265Sahrens mntopt_on = MNTOPT_EXEC; 22133265Sahrens mntopt_off = MNTOPT_NOEXEC; 22143265Sahrens break; 22153265Sahrens 22163265Sahrens case ZFS_PROP_READONLY: 22173265Sahrens mntopt_on = MNTOPT_RO; 22183265Sahrens mntopt_off = MNTOPT_RW; 22193265Sahrens break; 22203265Sahrens 22213265Sahrens case ZFS_PROP_SETUID: 22223265Sahrens mntopt_on = MNTOPT_SETUID; 22233265Sahrens mntopt_off = MNTOPT_NOSETUID; 22243265Sahrens break; 22253265Sahrens 22263265Sahrens case ZFS_PROP_XATTR: 22273265Sahrens mntopt_on = MNTOPT_XATTR; 22283265Sahrens mntopt_off = MNTOPT_NOXATTR; 22293265Sahrens break; 22303265Sahrens } 22313265Sahrens 22322474Seschrock /* 22332474Seschrock * Because looking up the mount options is potentially expensive 22342474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 22352474Seschrock * we're looking up a property which requires its presence. 22362474Seschrock */ 22372474Seschrock if (!zhp->zfs_mntcheck && 22383265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 22393265Sahrens struct mnttab entry, search = { 0 }; 22403265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 22412474Seschrock 22422474Seschrock search.mnt_special = (char *)zhp->zfs_name; 22432474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 22443265Sahrens rewind(mnttab); 22453265Sahrens 22463265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 22473265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 22483265Sahrens entry.mnt_mntopts); 22493265Sahrens if (zhp->zfs_mntopts == NULL) 22503265Sahrens return (-1); 22513265Sahrens } 22522474Seschrock 22532474Seschrock zhp->zfs_mntcheck = B_TRUE; 22542474Seschrock } 22552474Seschrock 2256789Sahrens if (zhp->zfs_mntopts == NULL) 2257789Sahrens mnt.mnt_mntopts = ""; 2258789Sahrens else 2259789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2260789Sahrens 2261789Sahrens switch (prop) { 2262789Sahrens case ZFS_PROP_ATIME: 22633265Sahrens case ZFS_PROP_DEVICES: 22643265Sahrens case ZFS_PROP_EXEC: 22653265Sahrens case ZFS_PROP_READONLY: 22663265Sahrens case ZFS_PROP_SETUID: 22673265Sahrens case ZFS_PROP_XATTR: 22682082Seschrock *val = getprop_uint64(zhp, prop, source); 22692082Seschrock 22703265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 22712082Seschrock *val = B_TRUE; 2272789Sahrens if (src) 2273789Sahrens *src = ZFS_SRC_TEMPORARY; 22743265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 22752082Seschrock *val = B_FALSE; 2276789Sahrens if (src) 2277789Sahrens *src = ZFS_SRC_TEMPORARY; 2278789Sahrens } 22792082Seschrock break; 2280789Sahrens 22813265Sahrens case ZFS_PROP_CANMOUNT: 22822082Seschrock *val = getprop_uint64(zhp, prop, source); 22833417Srm160521 if (*val == 0) 22843417Srm160521 *source = zhp->zfs_name; 22853417Srm160521 else 22863417Srm160521 *source = ""; /* default */ 22872082Seschrock break; 2288789Sahrens 2289789Sahrens case ZFS_PROP_QUOTA: 2290789Sahrens case ZFS_PROP_RESERVATION: 22912885Sahrens *val = getprop_uint64(zhp, prop, source); 22922885Sahrens if (*val == 0) 2293789Sahrens *source = ""; /* default */ 2294789Sahrens else 2295789Sahrens *source = zhp->zfs_name; 22962082Seschrock break; 2297789Sahrens 2298789Sahrens case ZFS_PROP_MOUNTED: 22992082Seschrock *val = (zhp->zfs_mntopts != NULL); 23002082Seschrock break; 2301789Sahrens 23023377Seschrock case ZFS_PROP_NUMCLONES: 23033377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 23043377Seschrock break; 23053377Seschrock 2306789Sahrens default: 23074577Sahrens switch (zfs_prop_get_type(prop)) { 23084577Sahrens case prop_type_number: 23094577Sahrens case prop_type_boolean: 23104577Sahrens case prop_type_index: 23114577Sahrens *val = getprop_uint64(zhp, prop, source); 23124577Sahrens break; 23134577Sahrens 23144577Sahrens case prop_type_string: 23154577Sahrens default: 23164577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 23174577Sahrens "cannot get non-numeric property")); 23184577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 23194577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 23204577Sahrens } 2321789Sahrens } 2322789Sahrens 2323789Sahrens return (0); 2324789Sahrens } 2325789Sahrens 2326789Sahrens /* 2327789Sahrens * Calculate the source type, given the raw source string. 2328789Sahrens */ 2329789Sahrens static void 2330789Sahrens get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source, 2331789Sahrens char *statbuf, size_t statlen) 2332789Sahrens { 2333789Sahrens if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY) 2334789Sahrens return; 2335789Sahrens 2336789Sahrens if (source == NULL) { 2337789Sahrens *srctype = ZFS_SRC_NONE; 2338789Sahrens } else if (source[0] == '\0') { 2339789Sahrens *srctype = ZFS_SRC_DEFAULT; 2340789Sahrens } else { 2341789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 2342789Sahrens *srctype = ZFS_SRC_LOCAL; 2343789Sahrens } else { 2344789Sahrens (void) strlcpy(statbuf, source, statlen); 2345789Sahrens *srctype = ZFS_SRC_INHERITED; 2346789Sahrens } 2347789Sahrens } 2348789Sahrens 2349789Sahrens } 2350789Sahrens 2351789Sahrens /* 2352789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2353789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2354789Sahrens * human-readable form. 2355789Sahrens * 2356789Sahrens * Returns 0 on success, or -1 on error. 2357789Sahrens */ 2358789Sahrens int 2359789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 23602082Seschrock zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2361789Sahrens { 2362789Sahrens char *source = NULL; 2363789Sahrens uint64_t val; 2364789Sahrens char *str; 2365789Sahrens const char *root; 23662676Seschrock const char *strval; 2367789Sahrens 2368789Sahrens /* 2369789Sahrens * Check to see if this property applies to our object 2370789Sahrens */ 2371789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2372789Sahrens return (-1); 2373789Sahrens 2374789Sahrens if (src) 2375789Sahrens *src = ZFS_SRC_NONE; 2376789Sahrens 2377789Sahrens switch (prop) { 2378789Sahrens case ZFS_PROP_CREATION: 2379789Sahrens /* 2380789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2381789Sahrens * this into a string unless 'literal' is specified. 2382789Sahrens */ 2383789Sahrens { 23842885Sahrens val = getprop_uint64(zhp, prop, &source); 23852885Sahrens time_t time = (time_t)val; 2386789Sahrens struct tm t; 2387789Sahrens 2388789Sahrens if (literal || 2389789Sahrens localtime_r(&time, &t) == NULL || 2390789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2391789Sahrens &t) == 0) 23922885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2393789Sahrens } 2394789Sahrens break; 2395789Sahrens 2396789Sahrens case ZFS_PROP_MOUNTPOINT: 2397789Sahrens /* 2398789Sahrens * Getting the precise mountpoint can be tricky. 2399789Sahrens * 2400789Sahrens * - for 'none' or 'legacy', return those values. 2401789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2402789Sahrens * - for inherited mountpoints, we want to take everything 2403789Sahrens * after our ancestor and append it to the inherited value. 2404789Sahrens * 2405789Sahrens * If the pool has an alternate root, we want to prepend that 2406789Sahrens * root to any values we return. 2407789Sahrens */ 24081544Seschrock root = zhp->zfs_root; 24091356Seschrock str = getprop_string(zhp, prop, &source); 24101356Seschrock 24111356Seschrock if (str[0] == '\0') { 2412789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2413789Sahrens root, zhp->zfs_name); 24141356Seschrock } else if (str[0] == '/') { 24151356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2416789Sahrens 2417789Sahrens if (relpath[0] == '/') 2418789Sahrens relpath++; 24191356Seschrock if (str[1] == '\0') 24201356Seschrock str++; 2421789Sahrens 2422789Sahrens if (relpath[0] == '\0') 2423789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 24241356Seschrock root, str); 2425789Sahrens else 2426789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 24271356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2428789Sahrens relpath); 2429789Sahrens } else { 2430789Sahrens /* 'legacy' or 'none' */ 24311356Seschrock (void) strlcpy(propbuf, str, proplen); 2432789Sahrens } 2433789Sahrens 2434789Sahrens break; 2435789Sahrens 2436789Sahrens case ZFS_PROP_ORIGIN: 24372885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2438789Sahrens proplen); 2439789Sahrens /* 2440789Sahrens * If there is no parent at all, return failure to indicate that 2441789Sahrens * it doesn't apply to this dataset. 2442789Sahrens */ 2443789Sahrens if (propbuf[0] == '\0') 2444789Sahrens return (-1); 2445789Sahrens break; 2446789Sahrens 2447789Sahrens case ZFS_PROP_QUOTA: 2448789Sahrens case ZFS_PROP_RESERVATION: 24492082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 24502082Seschrock return (-1); 2451789Sahrens 2452789Sahrens /* 2453789Sahrens * If quota or reservation is 0, we translate this into 'none' 2454789Sahrens * (unless literal is set), and indicate that it's the default 2455789Sahrens * value. Otherwise, we print the number nicely and indicate 2456789Sahrens * that its set locally. 2457789Sahrens */ 2458789Sahrens if (val == 0) { 2459789Sahrens if (literal) 2460789Sahrens (void) strlcpy(propbuf, "0", proplen); 2461789Sahrens else 2462789Sahrens (void) strlcpy(propbuf, "none", proplen); 2463789Sahrens } else { 2464789Sahrens if (literal) 24652856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 24663912Slling (u_longlong_t)val); 2467789Sahrens else 2468789Sahrens zfs_nicenum(val, propbuf, proplen); 2469789Sahrens } 2470789Sahrens break; 2471789Sahrens 2472789Sahrens case ZFS_PROP_COMPRESSRATIO: 24732082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 24742082Seschrock return (-1); 24752856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 24762856Snd150628 val / 100, (longlong_t)val % 100); 2477789Sahrens break; 2478789Sahrens 2479789Sahrens case ZFS_PROP_TYPE: 2480789Sahrens switch (zhp->zfs_type) { 2481789Sahrens case ZFS_TYPE_FILESYSTEM: 2482789Sahrens str = "filesystem"; 2483789Sahrens break; 2484789Sahrens case ZFS_TYPE_VOLUME: 2485789Sahrens str = "volume"; 2486789Sahrens break; 2487789Sahrens case ZFS_TYPE_SNAPSHOT: 2488789Sahrens str = "snapshot"; 2489789Sahrens break; 2490789Sahrens default: 24912082Seschrock abort(); 2492789Sahrens } 2493789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2494789Sahrens break; 2495789Sahrens 2496789Sahrens case ZFS_PROP_MOUNTED: 2497789Sahrens /* 2498789Sahrens * The 'mounted' property is a pseudo-property that described 2499789Sahrens * whether the filesystem is currently mounted. Even though 2500789Sahrens * it's a boolean value, the typical values of "on" and "off" 2501789Sahrens * don't make sense, so we translate to "yes" and "no". 2502789Sahrens */ 25032082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 25042082Seschrock src, &source, &val) != 0) 25052082Seschrock return (-1); 25062082Seschrock if (val) 2507789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2508789Sahrens else 2509789Sahrens (void) strlcpy(propbuf, "no", proplen); 2510789Sahrens break; 2511789Sahrens 2512789Sahrens case ZFS_PROP_NAME: 2513789Sahrens /* 2514789Sahrens * The 'name' property is a pseudo-property derived from the 2515789Sahrens * dataset name. It is presented as a real property to simplify 2516789Sahrens * consumers. 2517789Sahrens */ 2518789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2519789Sahrens break; 2520789Sahrens 2521789Sahrens default: 25224577Sahrens switch (zfs_prop_get_type(prop)) { 25234577Sahrens case prop_type_number: 25244577Sahrens if (get_numeric_property(zhp, prop, src, 25254577Sahrens &source, &val) != 0) 25264577Sahrens return (-1); 25274577Sahrens if (literal) 25284577Sahrens (void) snprintf(propbuf, proplen, "%llu", 25294577Sahrens (u_longlong_t)val); 25304577Sahrens else 25314577Sahrens zfs_nicenum(val, propbuf, proplen); 25324577Sahrens break; 25334577Sahrens 25344577Sahrens case prop_type_string: 25354577Sahrens (void) strlcpy(propbuf, 25364577Sahrens getprop_string(zhp, prop, &source), proplen); 25374577Sahrens break; 25384577Sahrens 25394577Sahrens case prop_type_boolean: 25404577Sahrens if (get_numeric_property(zhp, prop, src, 25414577Sahrens &source, &val) != 0) 25424577Sahrens return (-1); 25434577Sahrens nicebool(val, propbuf, proplen); 25444577Sahrens 25454577Sahrens break; 25464577Sahrens 25474577Sahrens case prop_type_index: 25484577Sahrens val = getprop_uint64(zhp, prop, &source); 25494577Sahrens if (zfs_prop_index_to_string(prop, val, 25504577Sahrens &strval) != 0) 25514577Sahrens return (-1); 25524577Sahrens (void) strlcpy(propbuf, strval, proplen); 25534577Sahrens break; 25544577Sahrens 25554577Sahrens default: 25564577Sahrens abort(); 25574577Sahrens } 2558789Sahrens } 2559789Sahrens 2560789Sahrens get_source(zhp, src, source, statbuf, statlen); 2561789Sahrens 2562789Sahrens return (0); 2563789Sahrens } 2564789Sahrens 2565789Sahrens /* 2566789Sahrens * Utility function to get the given numeric property. Does no validation that 2567789Sahrens * the given property is the appropriate type; should only be used with 2568789Sahrens * hard-coded property types. 2569789Sahrens */ 2570789Sahrens uint64_t 2571789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2572789Sahrens { 2573789Sahrens char *source; 2574789Sahrens zfs_source_t sourcetype = ZFS_SRC_NONE; 25752082Seschrock uint64_t val; 25762082Seschrock 25772082Seschrock (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 25782082Seschrock 25792082Seschrock return (val); 2580789Sahrens } 2581789Sahrens 2582789Sahrens /* 2583789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2584789Sahrens */ 2585789Sahrens int 2586789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2587789Sahrens zfs_source_t *src, char *statbuf, size_t statlen) 2588789Sahrens { 2589789Sahrens char *source; 2590789Sahrens 2591789Sahrens /* 2592789Sahrens * Check to see if this property applies to our object 2593789Sahrens */ 2594789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 25953237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 25962082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 25972082Seschrock zfs_prop_to_name(prop))); 2598789Sahrens 2599789Sahrens if (src) 2600789Sahrens *src = ZFS_SRC_NONE; 2601789Sahrens 26022082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 26032082Seschrock return (-1); 2604789Sahrens 2605789Sahrens get_source(zhp, src, source, statbuf, statlen); 2606789Sahrens 2607789Sahrens return (0); 2608789Sahrens } 2609789Sahrens 2610789Sahrens /* 2611789Sahrens * Returns the name of the given zfs handle. 2612789Sahrens */ 2613789Sahrens const char * 2614789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2615789Sahrens { 2616789Sahrens return (zhp->zfs_name); 2617789Sahrens } 2618789Sahrens 2619789Sahrens /* 2620789Sahrens * Returns the type of the given zfs handle. 2621789Sahrens */ 2622789Sahrens zfs_type_t 2623789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2624789Sahrens { 2625789Sahrens return (zhp->zfs_type); 2626789Sahrens } 2627789Sahrens 2628789Sahrens /* 26291356Seschrock * Iterate over all child filesystems 2630789Sahrens */ 2631789Sahrens int 26321356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2633789Sahrens { 2634789Sahrens zfs_cmd_t zc = { 0 }; 2635789Sahrens zfs_handle_t *nzhp; 2636789Sahrens int ret; 2637789Sahrens 2638789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 26392082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2640789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2641789Sahrens /* 2642789Sahrens * Ignore private dataset names. 2643789Sahrens */ 2644789Sahrens if (dataset_name_hidden(zc.zc_name)) 2645789Sahrens continue; 2646789Sahrens 2647789Sahrens /* 2648789Sahrens * Silently ignore errors, as the only plausible explanation is 2649789Sahrens * that the pool has since been removed. 2650789Sahrens */ 26512082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 26522082Seschrock zc.zc_name)) == NULL) 2653789Sahrens continue; 2654789Sahrens 2655789Sahrens if ((ret = func(nzhp, data)) != 0) 2656789Sahrens return (ret); 2657789Sahrens } 2658789Sahrens 2659789Sahrens /* 2660789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2661789Sahrens * returned, then the underlying dataset has been removed since we 2662789Sahrens * obtained the handle. 2663789Sahrens */ 2664789Sahrens if (errno != ESRCH && errno != ENOENT) 26652082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 26662082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2667789Sahrens 26681356Seschrock return (0); 26691356Seschrock } 26701356Seschrock 26711356Seschrock /* 26721356Seschrock * Iterate over all snapshots 26731356Seschrock */ 26741356Seschrock int 26751356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 26761356Seschrock { 26771356Seschrock zfs_cmd_t zc = { 0 }; 26781356Seschrock zfs_handle_t *nzhp; 26791356Seschrock int ret; 2680789Sahrens 2681789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 26822082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 26832082Seschrock &zc) == 0; 2684789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2685789Sahrens 26862082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 26872082Seschrock zc.zc_name)) == NULL) 2688789Sahrens continue; 2689789Sahrens 2690789Sahrens if ((ret = func(nzhp, data)) != 0) 2691789Sahrens return (ret); 2692789Sahrens } 2693789Sahrens 2694789Sahrens /* 2695789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2696789Sahrens * returned, then the underlying dataset has been removed since we 2697789Sahrens * obtained the handle. Silently ignore this case, and return success. 2698789Sahrens */ 2699789Sahrens if (errno != ESRCH && errno != ENOENT) 27002082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 27012082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2702789Sahrens 2703789Sahrens return (0); 2704789Sahrens } 2705789Sahrens 2706789Sahrens /* 27071356Seschrock * Iterate over all children, snapshots and filesystems 27081356Seschrock */ 27091356Seschrock int 27101356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 27111356Seschrock { 27121356Seschrock int ret; 27131356Seschrock 27141356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 27151356Seschrock return (ret); 27161356Seschrock 27171356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 27181356Seschrock } 27191356Seschrock 27201356Seschrock /* 2721789Sahrens * Given a complete name, return just the portion that refers to the parent. 2722789Sahrens * Can return NULL if this is a pool. 2723789Sahrens */ 2724789Sahrens static int 2725789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2726789Sahrens { 2727789Sahrens char *loc; 2728789Sahrens 2729789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2730789Sahrens return (-1); 2731789Sahrens 2732789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2733789Sahrens buf[loc - path] = '\0'; 2734789Sahrens 2735789Sahrens return (0); 2736789Sahrens } 2737789Sahrens 2738789Sahrens /* 27394490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 27404490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 27414490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 27424490Svb160487 * length of already existing prefix of the given path. We also fetch the 27434490Svb160487 * 'zoned' property, which is used to validate property settings when creating 27444490Svb160487 * new datasets. 2745789Sahrens */ 2746789Sahrens static int 27474490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 27484490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2749789Sahrens { 2750789Sahrens zfs_cmd_t zc = { 0 }; 2751789Sahrens char parent[ZFS_MAXNAMELEN]; 2752789Sahrens char *slash; 27531356Seschrock zfs_handle_t *zhp; 27542082Seschrock char errbuf[1024]; 27552082Seschrock 27562082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 27572082Seschrock path); 2758789Sahrens 2759789Sahrens /* get parent, and check to see if this is just a pool */ 2760789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 27612082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27622082Seschrock "missing dataset name")); 27632082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2764789Sahrens } 2765789Sahrens 2766789Sahrens /* check to see if the pool exists */ 2767789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2768789Sahrens slash = parent + strlen(parent); 2769789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2770789Sahrens zc.zc_name[slash - parent] = '\0'; 27712082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2772789Sahrens errno == ENOENT) { 27732082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27742082Seschrock "no such pool '%s'"), zc.zc_name); 27752082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2776789Sahrens } 2777789Sahrens 2778789Sahrens /* check to see if the parent dataset exists */ 27794490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 27804490Svb160487 if (errno == ENOENT && accept_ancestor) { 27814490Svb160487 /* 27824490Svb160487 * Go deeper to find an ancestor, give up on top level. 27834490Svb160487 */ 27844490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 27854490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27864490Svb160487 "no such pool '%s'"), zc.zc_name); 27874490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 27884490Svb160487 } 27894490Svb160487 } else if (errno == ENOENT) { 27902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27912082Seschrock "parent does not exist")); 27922082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 27934490Svb160487 } else 27942082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2795789Sahrens } 2796789Sahrens 27972676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2798789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 27992676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 28002082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 28011356Seschrock zfs_close(zhp); 2802789Sahrens return (-1); 2803789Sahrens } 2804789Sahrens 2805789Sahrens /* make sure parent is a filesystem */ 28061356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 28072082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28082082Seschrock "parent is not a filesystem")); 28092082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 28101356Seschrock zfs_close(zhp); 2811789Sahrens return (-1); 2812789Sahrens } 2813789Sahrens 28141356Seschrock zfs_close(zhp); 28154490Svb160487 if (prefixlen != NULL) 28164490Svb160487 *prefixlen = strlen(parent); 28174490Svb160487 return (0); 28184490Svb160487 } 28194490Svb160487 28204490Svb160487 /* 28214490Svb160487 * Finds whether the dataset of the given type(s) exists. 28224490Svb160487 */ 28234490Svb160487 boolean_t 28244490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 28254490Svb160487 { 28264490Svb160487 zfs_handle_t *zhp; 28274490Svb160487 28284490Svb160487 if (!zfs_validate_name(hdl, path, types)) 28294490Svb160487 return (B_FALSE); 28304490Svb160487 28314490Svb160487 /* 28324490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 28334490Svb160487 */ 28344490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 28354490Svb160487 int ds_type = zhp->zfs_type; 28364490Svb160487 28374490Svb160487 zfs_close(zhp); 28384490Svb160487 if (types & ds_type) 28394490Svb160487 return (B_TRUE); 28404490Svb160487 } 28414490Svb160487 return (B_FALSE); 28424490Svb160487 } 28434490Svb160487 28444490Svb160487 /* 28454490Svb160487 * Creates non-existing ancestors of the given path. 28464490Svb160487 */ 28474490Svb160487 int 28484490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 28494490Svb160487 { 28504490Svb160487 int prefix; 28514490Svb160487 uint64_t zoned; 28524490Svb160487 char *path_copy; 28534490Svb160487 int rc; 28544490Svb160487 28554490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 28564490Svb160487 return (-1); 28574490Svb160487 28584490Svb160487 if ((path_copy = strdup(path)) != NULL) { 28594490Svb160487 rc = create_parents(hdl, path_copy, prefix); 28604490Svb160487 free(path_copy); 28614490Svb160487 } 28624490Svb160487 if (path_copy == NULL || rc != 0) 28634490Svb160487 return (-1); 28644490Svb160487 2865789Sahrens return (0); 2866789Sahrens } 2867789Sahrens 2868789Sahrens /* 28692676Seschrock * Create a new filesystem or volume. 2870789Sahrens */ 2871789Sahrens int 28722082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 28732676Seschrock nvlist_t *props) 2874789Sahrens { 2875789Sahrens zfs_cmd_t zc = { 0 }; 2876789Sahrens int ret; 2877789Sahrens uint64_t size = 0; 2878789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 28792082Seschrock char errbuf[1024]; 28802676Seschrock uint64_t zoned; 28812082Seschrock 28822082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28832082Seschrock "cannot create '%s'"), path); 2884789Sahrens 2885789Sahrens /* validate the path, taking care to note the extended error message */ 28862082Seschrock if (!zfs_validate_name(hdl, path, type)) 28872082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2888789Sahrens 2889789Sahrens /* validate parents exist */ 28904490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2891789Sahrens return (-1); 2892789Sahrens 2893789Sahrens /* 2894789Sahrens * The failure modes when creating a dataset of a different type over 2895789Sahrens * one that already exists is a little strange. In particular, if you 2896789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2897789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2898789Sahrens * first try to see if the dataset exists. 2899789Sahrens */ 2900789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 29014490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 29022082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29032082Seschrock "dataset already exists")); 29042082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2905789Sahrens } 2906789Sahrens 2907789Sahrens if (type == ZFS_TYPE_VOLUME) 2908789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2909789Sahrens else 2910789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2911789Sahrens 29123912Slling if (props && (props = zfs_validate_properties(hdl, type, NULL, props, 29133912Slling zoned, NULL, errbuf)) == 0) 29142676Seschrock return (-1); 29152676Seschrock 2916789Sahrens if (type == ZFS_TYPE_VOLUME) { 29171133Seschrock /* 29181133Seschrock * If we are creating a volume, the size and block size must 29191133Seschrock * satisfy a few restraints. First, the blocksize must be a 29201133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 29211133Seschrock * volsize must be a multiple of the block size, and cannot be 29221133Seschrock * zero. 29231133Seschrock */ 29242676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 29252676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 29262676Seschrock nvlist_free(props); 29272082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29282676Seschrock "missing volume size")); 29292676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2930789Sahrens } 2931789Sahrens 29322676Seschrock if ((ret = nvlist_lookup_uint64(props, 29332676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 29342676Seschrock &blocksize)) != 0) { 29352676Seschrock if (ret == ENOENT) { 29362676Seschrock blocksize = zfs_prop_default_numeric( 29372676Seschrock ZFS_PROP_VOLBLOCKSIZE); 29382676Seschrock } else { 29392676Seschrock nvlist_free(props); 29402676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29412676Seschrock "missing volume block size")); 29422676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29432676Seschrock } 29442676Seschrock } 29452676Seschrock 29462676Seschrock if (size == 0) { 29472676Seschrock nvlist_free(props); 29482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29492676Seschrock "volume size cannot be zero")); 29502676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29511133Seschrock } 29521133Seschrock 29531133Seschrock if (size % blocksize != 0) { 29542676Seschrock nvlist_free(props); 29552082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29562676Seschrock "volume size must be a multiple of volume block " 29572676Seschrock "size")); 29582676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29591133Seschrock } 2960789Sahrens } 2961789Sahrens 29622676Seschrock if (props && 29632676Seschrock zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) 29642676Seschrock return (-1); 29652676Seschrock nvlist_free(props); 29662676Seschrock 2967789Sahrens /* create the dataset */ 29684543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2969789Sahrens 29703912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 29712082Seschrock ret = zvol_create_link(hdl, path); 29723912Slling if (ret) { 29733912Slling (void) zfs_standard_error(hdl, errno, 29743912Slling dgettext(TEXT_DOMAIN, 29753912Slling "Volume successfully created, but device links " 29763912Slling "were not created")); 29773912Slling zcmd_free_nvlists(&zc); 29783912Slling return (-1); 29793912Slling } 29803912Slling } 2981789Sahrens 29822676Seschrock zcmd_free_nvlists(&zc); 29832676Seschrock 2984789Sahrens /* check for failure */ 2985789Sahrens if (ret != 0) { 2986789Sahrens char parent[ZFS_MAXNAMELEN]; 2987789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2988789Sahrens 2989789Sahrens switch (errno) { 2990789Sahrens case ENOENT: 29912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29922082Seschrock "no such parent '%s'"), parent); 29932082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2994789Sahrens 2995789Sahrens case EINVAL: 29962082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29973413Smmusante "parent '%s' is not a filesystem"), parent); 29982082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2999789Sahrens 3000789Sahrens case EDOM: 30012082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30022676Seschrock "volume block size must be power of 2 from " 30032676Seschrock "%u to %uk"), 3004789Sahrens (uint_t)SPA_MINBLOCKSIZE, 3005789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 30062082Seschrock 30072676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 30082082Seschrock 3009*4603Sahrens case ENOTSUP: 3010*4603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3011*4603Sahrens "pool must be upgraded to set this " 3012*4603Sahrens "property or value")); 3013*4603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3014*4603Sahrens 3015789Sahrens #ifdef _ILP32 3016789Sahrens case EOVERFLOW: 3017789Sahrens /* 3018789Sahrens * This platform can't address a volume this big. 3019789Sahrens */ 30202082Seschrock if (type == ZFS_TYPE_VOLUME) 30212082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 30222082Seschrock errbuf)); 3023789Sahrens #endif 30242082Seschrock /* FALLTHROUGH */ 3025789Sahrens default: 30262082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3027789Sahrens } 3028789Sahrens } 3029789Sahrens 3030789Sahrens return (0); 3031789Sahrens } 3032789Sahrens 3033789Sahrens /* 3034789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 3035789Sahrens * isn't mounted, and that there are no active dependents. 3036789Sahrens */ 3037789Sahrens int 3038789Sahrens zfs_destroy(zfs_handle_t *zhp) 3039789Sahrens { 3040789Sahrens zfs_cmd_t zc = { 0 }; 3041789Sahrens 3042789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3043789Sahrens 30442676Seschrock if (ZFS_IS_VOLUME(zhp)) { 30453126Sahl /* 30464543Smarks * If user doesn't have permissions to unshare volume, then 30474543Smarks * abort the request. This would only happen for a 30484543Smarks * non-privileged user. 30493126Sahl */ 30504543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 30514543Smarks return (-1); 30524543Smarks } 30533126Sahl 30542082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3055789Sahrens return (-1); 3056789Sahrens 3057789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3058789Sahrens } else { 3059789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3060789Sahrens } 3061789Sahrens 30624543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 30633237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 30642082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 30652082Seschrock zhp->zfs_name)); 30662199Sahrens } 3067789Sahrens 3068789Sahrens remove_mountpoint(zhp); 3069789Sahrens 3070789Sahrens return (0); 3071789Sahrens } 3072789Sahrens 30732199Sahrens struct destroydata { 30742199Sahrens char *snapname; 30752199Sahrens boolean_t gotone; 30763265Sahrens boolean_t closezhp; 30772199Sahrens }; 30782199Sahrens 30792199Sahrens static int 30802199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 30812199Sahrens { 30822199Sahrens struct destroydata *dd = arg; 30832199Sahrens zfs_handle_t *szhp; 30842199Sahrens char name[ZFS_MAXNAMELEN]; 30853265Sahrens boolean_t closezhp = dd->closezhp; 30863265Sahrens int rv; 30872199Sahrens 30882676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 30892676Seschrock (void) strlcat(name, "@", sizeof (name)); 30902676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 30912199Sahrens 30922199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 30932199Sahrens if (szhp) { 30942199Sahrens dd->gotone = B_TRUE; 30952199Sahrens zfs_close(szhp); 30962199Sahrens } 30972199Sahrens 30982199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 30992199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 31002199Sahrens /* 31012199Sahrens * NB: this is simply a best-effort. We don't want to 31022199Sahrens * return an error, because then we wouldn't visit all 31032199Sahrens * the volumes. 31042199Sahrens */ 31052199Sahrens } 31062199Sahrens 31073265Sahrens dd->closezhp = B_TRUE; 31083265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 31093265Sahrens if (closezhp) 31103265Sahrens zfs_close(zhp); 31113265Sahrens return (rv); 31122199Sahrens } 31132199Sahrens 31142199Sahrens /* 31152199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 31162199Sahrens */ 31172199Sahrens int 31182199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 31192199Sahrens { 31202199Sahrens zfs_cmd_t zc = { 0 }; 31212199Sahrens int ret; 31222199Sahrens struct destroydata dd = { 0 }; 31232199Sahrens 31242199Sahrens dd.snapname = snapname; 31252199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 31262199Sahrens 31272199Sahrens if (!dd.gotone) { 31283237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 31292199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 31302199Sahrens zhp->zfs_name, snapname)); 31312199Sahrens } 31322199Sahrens 31332199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31342676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 31352199Sahrens 31364543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 31372199Sahrens if (ret != 0) { 31382199Sahrens char errbuf[1024]; 31392199Sahrens 31402199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31412199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 31422199Sahrens 31432199Sahrens switch (errno) { 31442199Sahrens case EEXIST: 31452199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31462199Sahrens "snapshot is cloned")); 31472199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 31482199Sahrens 31492199Sahrens default: 31502199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 31512199Sahrens errbuf)); 31522199Sahrens } 31532199Sahrens } 31542199Sahrens 31552199Sahrens return (0); 31562199Sahrens } 31572199Sahrens 3158789Sahrens /* 3159789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3160789Sahrens */ 3161789Sahrens int 31622676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3163789Sahrens { 3164789Sahrens zfs_cmd_t zc = { 0 }; 3165789Sahrens char parent[ZFS_MAXNAMELEN]; 3166789Sahrens int ret; 31672082Seschrock char errbuf[1024]; 31682082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 31692676Seschrock zfs_type_t type; 31702676Seschrock uint64_t zoned; 3171789Sahrens 3172789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3173789Sahrens 31742082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31752082Seschrock "cannot create '%s'"), target); 31762082Seschrock 3177789Sahrens /* validate the target name */ 31782082Seschrock if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM)) 31792082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3180789Sahrens 3181789Sahrens /* validate parents exist */ 31824490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3183789Sahrens return (-1); 3184789Sahrens 3185789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3186789Sahrens 3187789Sahrens /* do the clone */ 31882676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3189789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 31902744Snn35248 type = ZFS_TYPE_VOLUME; 31912676Seschrock } else { 3192789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 31932744Snn35248 type = ZFS_TYPE_FILESYSTEM; 31942676Seschrock } 31952676Seschrock 31962676Seschrock if (props) { 31973912Slling if ((props = zfs_validate_properties(hdl, type, NULL, props, 31983912Slling zoned, zhp, errbuf)) == NULL) 31992676Seschrock return (-1); 32002676Seschrock 32012676Seschrock if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) { 32022676Seschrock nvlist_free(props); 32032676Seschrock return (-1); 32042676Seschrock } 32052676Seschrock 32062676Seschrock nvlist_free(props); 32072676Seschrock } 3208789Sahrens 3209789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 32102676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 32114543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3212789Sahrens 32132676Seschrock zcmd_free_nvlists(&zc); 32142676Seschrock 3215789Sahrens if (ret != 0) { 3216789Sahrens switch (errno) { 3217789Sahrens 3218789Sahrens case ENOENT: 3219789Sahrens /* 3220789Sahrens * The parent doesn't exist. We should have caught this 3221789Sahrens * above, but there may a race condition that has since 3222789Sahrens * destroyed the parent. 3223789Sahrens * 3224789Sahrens * At this point, we don't know whether it's the source 3225789Sahrens * that doesn't exist anymore, or whether the target 3226789Sahrens * dataset doesn't exist. 3227789Sahrens */ 32282082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32292082Seschrock "no such parent '%s'"), parent); 32302082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 32312082Seschrock 32322082Seschrock case EXDEV: 32332082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32342082Seschrock "source and target pools differ")); 32352082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 32362082Seschrock errbuf)); 32372082Seschrock 32382082Seschrock default: 32392082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 32402082Seschrock errbuf)); 32412082Seschrock } 32422676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 32432082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 32442082Seschrock } 32452082Seschrock 32462082Seschrock return (ret); 32472082Seschrock } 32482082Seschrock 32492082Seschrock typedef struct promote_data { 32502082Seschrock char cb_mountpoint[MAXPATHLEN]; 32512082Seschrock const char *cb_target; 32522082Seschrock const char *cb_errbuf; 32532082Seschrock uint64_t cb_pivot_txg; 32542082Seschrock } promote_data_t; 32552082Seschrock 32562082Seschrock static int 32572082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 32582082Seschrock { 32592082Seschrock promote_data_t *pd = data; 32602082Seschrock zfs_handle_t *szhp; 32612082Seschrock char snapname[MAXPATHLEN]; 32623265Sahrens int rv = 0; 32632082Seschrock 32642082Seschrock /* We don't care about snapshots after the pivot point */ 32653265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 32663265Sahrens zfs_close(zhp); 32672082Seschrock return (0); 32683265Sahrens } 32692082Seschrock 32702417Sahrens /* Remove the device link if it's a zvol. */ 32712676Seschrock if (ZFS_IS_VOLUME(zhp)) 32722417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 32732082Seschrock 32742082Seschrock /* Check for conflicting names */ 32752676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 32762676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 32772082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 32782082Seschrock if (szhp != NULL) { 32792082Seschrock zfs_close(szhp); 32802082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32812082Seschrock "snapshot name '%s' from origin \n" 32822082Seschrock "conflicts with '%s' from target"), 32832082Seschrock zhp->zfs_name, snapname); 32843265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 32852082Seschrock } 32863265Sahrens zfs_close(zhp); 32873265Sahrens return (rv); 32882082Seschrock } 32892082Seschrock 32902417Sahrens static int 32912417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 32922417Sahrens { 32932417Sahrens promote_data_t *pd = data; 32942417Sahrens 32952417Sahrens /* We don't care about snapshots after the pivot point */ 32963265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 32973265Sahrens /* Create the device link if it's a zvol. */ 32983265Sahrens if (ZFS_IS_VOLUME(zhp)) 32993265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 33003265Sahrens } 33013265Sahrens 33023265Sahrens zfs_close(zhp); 33032417Sahrens return (0); 33042417Sahrens } 33052417Sahrens 33062082Seschrock /* 33072082Seschrock * Promotes the given clone fs to be the clone parent. 33082082Seschrock */ 33092082Seschrock int 33102082Seschrock zfs_promote(zfs_handle_t *zhp) 33112082Seschrock { 33122082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 33132082Seschrock zfs_cmd_t zc = { 0 }; 33142082Seschrock char parent[MAXPATHLEN]; 33152082Seschrock char *cp; 33162082Seschrock int ret; 33172082Seschrock zfs_handle_t *pzhp; 33182082Seschrock promote_data_t pd; 33192082Seschrock char errbuf[1024]; 33202082Seschrock 33212082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33222082Seschrock "cannot promote '%s'"), zhp->zfs_name); 33232082Seschrock 33242082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 33252082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33262082Seschrock "snapshots can not be promoted")); 33272082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 33282082Seschrock } 33292082Seschrock 33302676Seschrock (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 33312082Seschrock if (parent[0] == '\0') { 33322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33332082Seschrock "not a cloned filesystem")); 33342082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 33352082Seschrock } 33362082Seschrock cp = strchr(parent, '@'); 33372082Seschrock *cp = '\0'; 33382082Seschrock 33392082Seschrock /* Walk the snapshots we will be moving */ 33402082Seschrock pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 33412082Seschrock if (pzhp == NULL) 33422082Seschrock return (-1); 33432082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 33442082Seschrock zfs_close(pzhp); 33452082Seschrock pd.cb_target = zhp->zfs_name; 33462082Seschrock pd.cb_errbuf = errbuf; 33472082Seschrock pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY); 33482082Seschrock if (pzhp == NULL) 33492082Seschrock return (-1); 33502082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 33512082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 33522082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 33532417Sahrens if (ret != 0) { 33542417Sahrens zfs_close(pzhp); 33552082Seschrock return (-1); 33562417Sahrens } 33572082Seschrock 33582082Seschrock /* issue the ioctl */ 33592676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 33602676Seschrock sizeof (zc.zc_value)); 33612082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33624543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 33632082Seschrock 33642082Seschrock if (ret != 0) { 33652417Sahrens int save_errno = errno; 33662417Sahrens 33672417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 33682417Sahrens zfs_close(pzhp); 33692417Sahrens 33702417Sahrens switch (save_errno) { 3371789Sahrens case EEXIST: 3372789Sahrens /* 33732082Seschrock * There is a conflicting snapshot name. We 33742082Seschrock * should have caught this above, but they could 33752082Seschrock * have renamed something in the mean time. 3376789Sahrens */ 33772082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33782082Seschrock "conflicting snapshot name from parent '%s'"), 33792082Seschrock parent); 33802082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3381789Sahrens 3382789Sahrens default: 33832417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3384789Sahrens } 33852417Sahrens } else { 33862417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3387789Sahrens } 3388789Sahrens 33892417Sahrens zfs_close(pzhp); 3390789Sahrens return (ret); 3391789Sahrens } 3392789Sahrens 33934007Smmusante struct createdata { 33944007Smmusante const char *cd_snapname; 33954007Smmusante int cd_ifexists; 33964007Smmusante }; 33974007Smmusante 33982199Sahrens static int 33992199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 34002199Sahrens { 34014007Smmusante struct createdata *cd = arg; 34022676Seschrock int ret; 34032199Sahrens 34042199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 34052199Sahrens char name[MAXPATHLEN]; 34062199Sahrens 34072676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 34082676Seschrock (void) strlcat(name, "@", sizeof (name)); 34094007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 34104007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 34114007Smmusante cd->cd_ifexists); 34122199Sahrens /* 34132199Sahrens * NB: this is simply a best-effort. We don't want to 34142199Sahrens * return an error, because then we wouldn't visit all 34152199Sahrens * the volumes. 34162199Sahrens */ 34172199Sahrens } 34182676Seschrock 34194007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 34202676Seschrock 34212676Seschrock zfs_close(zhp); 34222676Seschrock 34232676Seschrock return (ret); 34242199Sahrens } 34252199Sahrens 3426789Sahrens /* 34273504Sahl * Takes a snapshot of the given dataset. 3428789Sahrens */ 3429789Sahrens int 34302199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3431789Sahrens { 3432789Sahrens const char *delim; 3433789Sahrens char *parent; 3434789Sahrens zfs_handle_t *zhp; 3435789Sahrens zfs_cmd_t zc = { 0 }; 3436789Sahrens int ret; 34372082Seschrock char errbuf[1024]; 34382082Seschrock 34392082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34402082Seschrock "cannot snapshot '%s'"), path); 34412082Seschrock 34422082Seschrock /* validate the target name */ 34432082Seschrock if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT)) 34442082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3445789Sahrens 3446789Sahrens /* make sure the parent exists and is of the appropriate type */ 34472199Sahrens delim = strchr(path, '@'); 34482082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 34492082Seschrock return (-1); 3450789Sahrens (void) strncpy(parent, path, delim - path); 3451789Sahrens parent[delim - path] = '\0'; 3452789Sahrens 34532082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3454789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3455789Sahrens free(parent); 3456789Sahrens return (-1); 3457789Sahrens } 3458789Sahrens 34592199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 34602676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 34614543Smarks if (ZFS_IS_VOLUME(zhp)) 34624543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 34634543Smarks else 34644543Smarks zc.zc_objset_type = DMU_OST_ZFS; 34652199Sahrens zc.zc_cookie = recursive; 34664543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 34672199Sahrens 34682199Sahrens /* 34692199Sahrens * if it was recursive, the one that actually failed will be in 34702199Sahrens * zc.zc_name. 34712199Sahrens */ 34724543Smarks if (ret != 0) 34734543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34744543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 34754543Smarks 34762199Sahrens if (ret == 0 && recursive) { 34774007Smmusante struct createdata cd; 34784007Smmusante 34794007Smmusante cd.cd_snapname = delim + 1; 34804007Smmusante cd.cd_ifexists = B_FALSE; 34814007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 34822199Sahrens } 3483789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 34842082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 34852199Sahrens if (ret != 0) { 34864543Smarks (void) zfs_standard_error(hdl, errno, 34874543Smarks dgettext(TEXT_DOMAIN, 34884543Smarks "Volume successfully snapshotted, but device links " 34894543Smarks "were not created")); 34904543Smarks free(parent); 34914543Smarks zfs_close(zhp); 34924543Smarks return (-1); 34932199Sahrens } 3494789Sahrens } 3495789Sahrens 34962082Seschrock if (ret != 0) 34972082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3498789Sahrens 3499789Sahrens free(parent); 3500789Sahrens zfs_close(zhp); 3501789Sahrens 3502789Sahrens return (ret); 3503789Sahrens } 3504789Sahrens 3505789Sahrens /* 35063504Sahl * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 35073504Sahl * NULL) to the file descriptor specified by outfd. 3508789Sahrens */ 3509789Sahrens int 35103504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3511789Sahrens { 3512789Sahrens zfs_cmd_t zc = { 0 }; 35132082Seschrock char errbuf[1024]; 35142885Sahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 35152082Seschrock 35163504Sahl assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 35173504Sahl 35182885Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35192885Sahrens if (fromsnap) 35202885Sahrens (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 35213504Sahl zc.zc_cookie = outfd; 35223504Sahl 35233504Sahl if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 35243504Sahl (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35253504Sahl "cannot send '%s'"), zhp->zfs_name); 35263504Sahl 3527789Sahrens switch (errno) { 3528789Sahrens 3529789Sahrens case EXDEV: 35302082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35313413Smmusante "not an earlier snapshot from the same fs")); 35322082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3533789Sahrens 3534789Sahrens case EDQUOT: 3535789Sahrens case EFBIG: 3536789Sahrens case EIO: 3537789Sahrens case ENOLINK: 3538789Sahrens case ENOSPC: 3539789Sahrens case ENOSTR: 3540789Sahrens case ENXIO: 3541789Sahrens case EPIPE: 3542789Sahrens case ERANGE: 3543789Sahrens case EFAULT: 3544789Sahrens case EROFS: 35452082Seschrock zfs_error_aux(hdl, strerror(errno)); 35462082Seschrock return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3547789Sahrens 3548789Sahrens default: 35492082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3550789Sahrens } 3551789Sahrens } 3552789Sahrens 35533504Sahl return (0); 3554789Sahrens } 3555789Sahrens 3556789Sahrens /* 35572885Sahrens * Create ancestors of 'target', but not target itself, and not 35582885Sahrens * ancestors whose names are shorter than prefixlen. Die if 35592885Sahrens * prefixlen-ancestor does not exist. 35602885Sahrens */ 35612885Sahrens static int 35622885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 35632885Sahrens { 35642885Sahrens zfs_handle_t *h; 35652885Sahrens char *cp; 35662885Sahrens 35672885Sahrens /* make sure prefix exists */ 35682885Sahrens cp = strchr(target + prefixlen, '/'); 3569*4603Sahrens if (cp == NULL) { 3570*4603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3571*4603Sahrens } else { 3572*4603Sahrens *cp = '\0'; 3573*4603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 3574*4603Sahrens *cp = '/'; 3575*4603Sahrens } 35762885Sahrens if (h == NULL) 35772885Sahrens return (-1); 35782885Sahrens zfs_close(h); 35792885Sahrens 35802885Sahrens /* 35812885Sahrens * Attempt to create, mount, and share any ancestor filesystems, 35822885Sahrens * up to the prefixlen-long one. 35832885Sahrens */ 35842885Sahrens for (cp = target + prefixlen + 1; 35852885Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 35862885Sahrens const char *opname; 35874543Smarks char *logstr; 35882885Sahrens 35892885Sahrens *cp = '\0'; 35902885Sahrens 35912885Sahrens h = make_dataset_handle(hdl, target); 35922885Sahrens if (h) { 35932885Sahrens /* it already exists, nothing to do here */ 35942885Sahrens zfs_close(h); 35952885Sahrens continue; 35962885Sahrens } 35972885Sahrens 35982885Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 35994543Smarks logstr = hdl->libzfs_log_str; 36004543Smarks hdl->libzfs_log_str = NULL; 36012885Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 36024543Smarks NULL) != 0) { 36034543Smarks hdl->libzfs_log_str = logstr; 36042885Sahrens goto ancestorerr; 36054543Smarks } 36064543Smarks 36074543Smarks hdl->libzfs_log_str = logstr; 36082885Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 36092885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 36102885Sahrens if (h == NULL) 36112885Sahrens goto ancestorerr; 36122885Sahrens 36132885Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 36142885Sahrens if (zfs_mount(h, NULL, 0) != 0) 36152885Sahrens goto ancestorerr; 36162885Sahrens 36172885Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 36182885Sahrens if (zfs_share(h) != 0) 36192885Sahrens goto ancestorerr; 36202885Sahrens 36212885Sahrens zfs_close(h); 36222885Sahrens 36232885Sahrens continue; 36242885Sahrens ancestorerr: 36252885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36262885Sahrens "failed to %s ancestor '%s'"), opname, target); 36272885Sahrens return (-1); 36282885Sahrens } 36292885Sahrens 36302885Sahrens return (0); 36312885Sahrens } 36322885Sahrens 36332885Sahrens /* 36343504Sahl * Restores a backup of tosnap from the file descriptor specified by infd. 3635789Sahrens */ 3636789Sahrens int 36372082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 36383504Sahl int verbose, int dryrun, boolean_t force, int infd) 3639789Sahrens { 3640789Sahrens zfs_cmd_t zc = { 0 }; 3641789Sahrens time_t begin_time; 36422885Sahrens int ioctl_err, err, bytes, size, choplen; 3643789Sahrens char *cp; 3644789Sahrens dmu_replay_record_t drr; 3645789Sahrens struct drr_begin *drrb = &zc.zc_begin_record; 36462082Seschrock char errbuf[1024]; 36472665Snd150628 prop_changelist_t *clp; 36482885Sahrens char chopprefix[ZFS_MAXNAMELEN]; 3649789Sahrens 3650789Sahrens begin_time = time(NULL); 3651789Sahrens 36522082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36532082Seschrock "cannot receive")); 36542082Seschrock 3655789Sahrens /* read in the BEGIN record */ 3656789Sahrens cp = (char *)&drr; 3657789Sahrens bytes = 0; 3658789Sahrens do { 36593504Sahl size = read(infd, cp, sizeof (drr) - bytes); 3660868Sahrens cp += size; 3661868Sahrens bytes += size; 3662868Sahrens } while (size > 0); 3663868Sahrens 3664868Sahrens if (size < 0 || bytes != sizeof (drr)) { 36652082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36662082Seschrock "stream (failed to read first record)")); 36672082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3668789Sahrens } 3669789Sahrens 3670789Sahrens zc.zc_begin_record = drr.drr_u.drr_begin; 3671789Sahrens 3672789Sahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3673789Sahrens drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 36742082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36752082Seschrock "stream (bad magic number)")); 36762082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3677789Sahrens } 3678789Sahrens 3679789Sahrens if (drrb->drr_version != DMU_BACKUP_VERSION && 3680789Sahrens drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 36812082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 36822082Seschrock "0x%llx is supported (stream is version 0x%llx)"), 3683789Sahrens DMU_BACKUP_VERSION, drrb->drr_version); 36842082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3685789Sahrens } 3686789Sahrens 36872885Sahrens if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 36882885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36893912Slling "stream (bad snapshot name)")); 36902885Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 36912885Sahrens } 3692789Sahrens /* 36932885Sahrens * Determine how much of the snapshot name stored in the stream 36942885Sahrens * we are going to tack on to the name they specified on the 36952885Sahrens * command line, and how much we are going to chop off. 36962885Sahrens * 36972885Sahrens * If they specified a snapshot, chop the entire name stored in 36982885Sahrens * the stream. 3699789Sahrens */ 37002885Sahrens (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3701789Sahrens if (isprefix) { 37022885Sahrens /* 37032885Sahrens * They specified a fs with -d, we want to tack on 37042885Sahrens * everything but the pool name stored in the stream 37052885Sahrens */ 37062885Sahrens if (strchr(tosnap, '@')) { 37072885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 37082885Sahrens "argument - snapshot not allowed with -d")); 37092885Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3710789Sahrens } 37112885Sahrens cp = strchr(chopprefix, '/'); 3712789Sahrens if (cp == NULL) 37132885Sahrens cp = strchr(chopprefix, '@'); 37142885Sahrens *cp = '\0'; 3715789Sahrens } else if (strchr(tosnap, '@') == NULL) { 3716789Sahrens /* 37172885Sahrens * If they specified a filesystem without -d, we want to 37182885Sahrens * tack on everything after the fs specified in the 37192885Sahrens * first name from the stream. 3720789Sahrens */ 37212885Sahrens cp = strchr(chopprefix, '@'); 37222885Sahrens *cp = '\0'; 3723789Sahrens } 37242885Sahrens choplen = strlen(chopprefix); 37252885Sahrens 37262885Sahrens /* 37272885Sahrens * Determine name of destination snapshot, store in zc_value. 37282885Sahrens */ 37292885Sahrens (void) strcpy(zc.zc_value, tosnap); 37302885Sahrens (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 37312885Sahrens sizeof (zc.zc_value)); 37323265Sahrens if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT)) 37333265Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37342885Sahrens 37352885Sahrens (void) strcpy(zc.zc_name, zc.zc_value); 3736789Sahrens if (drrb->drr_fromguid) { 3737789Sahrens /* incremental backup stream */ 37382885Sahrens zfs_handle_t *h; 37392885Sahrens 37402885Sahrens /* do the recvbackup ioctl to the containing fs */ 37412885Sahrens *strchr(zc.zc_name, '@') = '\0'; 3742789Sahrens 3743789Sahrens /* make sure destination fs exists */ 37442082Seschrock h = zfs_open(hdl, zc.zc_name, 37452082Seschrock ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 37462082Seschrock if (h == NULL) 3747789Sahrens return (-1); 3748868Sahrens if (!dryrun) { 37492665Snd150628 /* 37502665Snd150628 * We need to unmount all the dependents of the dataset 37512665Snd150628 * and the dataset itself. If it's a volume 37522665Snd150628 * then remove device link. 37532665Snd150628 */ 3754868Sahrens if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 37552665Snd150628 clp = changelist_gather(h, ZFS_PROP_NAME, 0); 37562665Snd150628 if (clp == NULL) 37572665Snd150628 return (-1); 37582665Snd150628 if (changelist_prefix(clp) != 0) { 37592665Snd150628 changelist_free(clp); 37602665Snd150628 return (-1); 37612665Snd150628 } 3762868Sahrens } else { 37634543Smarks if (zvol_remove_link(hdl, h->zfs_name) != 0) { 37644543Smarks zfs_close(h); 37654543Smarks return (-1); 37664543Smarks } 37674543Smarks 3768868Sahrens } 3769868Sahrens } 3770789Sahrens zfs_close(h); 3771789Sahrens } else { 3772789Sahrens /* full backup stream */ 3773789Sahrens 3774868Sahrens /* Make sure destination fs does not exist */ 37752885Sahrens *strchr(zc.zc_name, '@') = '\0'; 37764490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 37772082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37782082Seschrock "destination '%s' exists"), zc.zc_name); 37792082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3780868Sahrens } 3781868Sahrens 37822885Sahrens if (strchr(zc.zc_name, '/') == NULL) { 37832885Sahrens /* 37842885Sahrens * they're trying to do a recv into a 37852885Sahrens * nonexistant topmost filesystem. 37862885Sahrens */ 37872885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37882885Sahrens "destination does not exist"), zc.zc_name); 37892885Sahrens return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 37902885Sahrens } 37912885Sahrens 3792868Sahrens /* Do the recvbackup ioctl to the fs's parent. */ 37932885Sahrens *strrchr(zc.zc_name, '/') = '\0'; 37942885Sahrens 37952885Sahrens if (isprefix && (err = create_parents(hdl, 37962885Sahrens zc.zc_value, strlen(tosnap))) != 0) { 37972885Sahrens return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 37982885Sahrens } 37992885Sahrens 3800789Sahrens } 3801789Sahrens 38023504Sahl zc.zc_cookie = infd; 38032676Seschrock zc.zc_guid = force; 3804789Sahrens if (verbose) { 38051749Sahrens (void) printf("%s %s stream of %s into %s\n", 38061749Sahrens dryrun ? "would receive" : "receiving", 3807789Sahrens drrb->drr_fromguid ? "incremental" : "full", 3808789Sahrens drr.drr_u.drr_begin.drr_toname, 38092676Seschrock zc.zc_value); 3810789Sahrens (void) fflush(stdout); 3811789Sahrens } 3812789Sahrens if (dryrun) 3813789Sahrens return (0); 38144543Smarks err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3815868Sahrens if (ioctl_err != 0) { 3816789Sahrens switch (errno) { 3817789Sahrens case ENODEV: 38182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38192082Seschrock "most recent snapshot does not match incremental " 38202082Seschrock "source")); 38212082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3822789Sahrens break; 3823789Sahrens case ETXTBSY: 38242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38252082Seschrock "destination has been modified since most recent " 38262082Seschrock "snapshot")); 38272082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3828789Sahrens break; 3829789Sahrens case EEXIST: 3830789Sahrens if (drrb->drr_fromguid == 0) { 3831789Sahrens /* it's the containing fs that exists */ 38322676Seschrock cp = strchr(zc.zc_value, '@'); 3833789Sahrens *cp = '\0'; 3834789Sahrens } 38352082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38362082Seschrock "destination already exists")); 38373237Slling (void) zfs_error_fmt(hdl, EZFS_EXISTS, 38383237Slling dgettext(TEXT_DOMAIN, "cannot restore to %s"), 38393237Slling zc.zc_value); 3840789Sahrens break; 3841789Sahrens case EINVAL: 38422082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3843868Sahrens break; 38441544Seschrock case ECKSUM: 38452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38462082Seschrock "invalid stream (checksum mismatch)")); 38472082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3848789Sahrens break; 3849789Sahrens default: 38502082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3851789Sahrens } 3852789Sahrens } 3853789Sahrens 3854789Sahrens /* 3855868Sahrens * Mount or recreate the /dev links for the target filesystem 3856868Sahrens * (if created, or if we tore them down to do an incremental 3857868Sahrens * restore), and the /dev links for the new snapshot (if 38582665Snd150628 * created). Also mount any children of the target filesystem 38592665Snd150628 * if we did an incremental receive. 3860789Sahrens */ 38612676Seschrock cp = strchr(zc.zc_value, '@'); 3862868Sahrens if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3863789Sahrens zfs_handle_t *h; 3864789Sahrens 3865789Sahrens *cp = '\0'; 38662676Seschrock h = zfs_open(hdl, zc.zc_value, 3867789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3868868Sahrens *cp = '@'; 3869789Sahrens if (h) { 38702665Snd150628 if (h->zfs_type == ZFS_TYPE_VOLUME) { 38712082Seschrock err = zvol_create_link(hdl, h->zfs_name); 38721544Seschrock if (err == 0 && ioctl_err == 0) 38732082Seschrock err = zvol_create_link(hdl, 38742676Seschrock zc.zc_value); 38752665Snd150628 } else { 38762665Snd150628 if (drrb->drr_fromguid) { 38772665Snd150628 err = changelist_postfix(clp); 38782665Snd150628 changelist_free(clp); 38792665Snd150628 } else { 38802665Snd150628 err = zfs_mount(h, NULL, 0); 38812665Snd150628 } 3882868Sahrens } 38832665Snd150628 zfs_close(h); 3884789Sahrens } 3885789Sahrens } 3886789Sahrens 3887868Sahrens if (err || ioctl_err) 3888868Sahrens return (-1); 3889789Sahrens 3890789Sahrens if (verbose) { 3891789Sahrens char buf1[64]; 3892789Sahrens char buf2[64]; 3893789Sahrens uint64_t bytes = zc.zc_cookie; 3894789Sahrens time_t delta = time(NULL) - begin_time; 3895789Sahrens if (delta == 0) 3896789Sahrens delta = 1; 3897789Sahrens zfs_nicenum(bytes, buf1, sizeof (buf1)); 3898789Sahrens zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3899789Sahrens 3900*4603Sahrens (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 3901789Sahrens buf1, delta, buf2); 3902789Sahrens } 39032665Snd150628 3904789Sahrens return (0); 3905789Sahrens } 3906789Sahrens 3907789Sahrens /* 39081294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 39091294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 39101294Slling * is a dependent and we should just destroy it without checking the transaction 39111294Slling * group. 3912789Sahrens */ 39131294Slling typedef struct rollback_data { 39141294Slling const char *cb_target; /* the snapshot */ 39151294Slling uint64_t cb_create; /* creation time reference */ 39161294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 39171294Slling int cb_error; 39182082Seschrock boolean_t cb_dependent; 39191294Slling } rollback_data_t; 39201294Slling 39211294Slling static int 39221294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 39231294Slling { 39241294Slling rollback_data_t *cbp = data; 39251294Slling 39261294Slling if (!cbp->cb_dependent) { 39271294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 39281294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 39291294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 39301294Slling cbp->cb_create) { 39314543Smarks char *logstr; 39321294Slling 39332082Seschrock cbp->cb_dependent = B_TRUE; 39342474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 39352474Seschrock cbp) != 0) 39362474Seschrock cbp->cb_error = 1; 39372082Seschrock cbp->cb_dependent = B_FALSE; 39381294Slling 39394543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 39404543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 39411294Slling if (zfs_destroy(zhp) != 0) 39421294Slling cbp->cb_error = 1; 39431294Slling else 39441294Slling changelist_remove(zhp, cbp->cb_clp); 39454543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 39461294Slling } 39471294Slling } else { 39481294Slling if (zfs_destroy(zhp) != 0) 39491294Slling cbp->cb_error = 1; 39501294Slling else 39511294Slling changelist_remove(zhp, cbp->cb_clp); 39521294Slling } 39531294Slling 39541294Slling zfs_close(zhp); 39551294Slling return (0); 39561294Slling } 39571294Slling 39581294Slling /* 39591294Slling * Rollback the dataset to its latest snapshot. 39601294Slling */ 39611294Slling static int 39621294Slling do_rollback(zfs_handle_t *zhp) 3963789Sahrens { 3964789Sahrens int ret; 3965789Sahrens zfs_cmd_t zc = { 0 }; 3966789Sahrens 3967789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3968789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3969789Sahrens 3970789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 39712082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3972789Sahrens return (-1); 3973789Sahrens 3974789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3975789Sahrens 39762676Seschrock if (ZFS_IS_VOLUME(zhp)) 3977789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3978789Sahrens else 3979789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3980789Sahrens 3981789Sahrens /* 3982789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3983789Sahrens * for the given dataset. Given these constraints, we can simply pass 3984789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3985789Sahrens * condition where the user has taken a snapshot since we verified that 3986789Sahrens * this was the most recent. 3987789Sahrens */ 39884543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 39893237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 39902082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 39912082Seschrock zhp->zfs_name); 3992789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 39932082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3994789Sahrens } 3995789Sahrens 3996789Sahrens return (ret); 3997789Sahrens } 3998789Sahrens 3999789Sahrens /* 40001294Slling * Given a dataset, rollback to a specific snapshot, discarding any 40011294Slling * data changes since then and making it the active dataset. 40021294Slling * 40031294Slling * Any snapshots more recent than the target are destroyed, along with 40041294Slling * their dependents. 40051294Slling */ 40061294Slling int 40071294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 40081294Slling { 40091294Slling int ret; 40101294Slling rollback_data_t cb = { 0 }; 40111294Slling prop_changelist_t *clp; 40121294Slling 40131294Slling /* 40141294Slling * Unmount all dependendents of the dataset and the dataset itself. 40151294Slling * The list we need to gather is the same as for doing rename 40161294Slling */ 40171294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 40181294Slling if (clp == NULL) 40191294Slling return (-1); 40201294Slling 40211294Slling if ((ret = changelist_prefix(clp)) != 0) 40221294Slling goto out; 40231294Slling 40241294Slling /* 40251294Slling * Destroy all recent snapshots and its dependends. 40261294Slling */ 40271294Slling cb.cb_target = snap->zfs_name; 40281294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 40291294Slling cb.cb_clp = clp; 40301294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 40311294Slling 40321294Slling if ((ret = cb.cb_error) != 0) { 40331294Slling (void) changelist_postfix(clp); 40341294Slling goto out; 40351294Slling } 40361294Slling 40371294Slling /* 40381294Slling * Now that we have verified that the snapshot is the latest, 40391294Slling * rollback to the given snapshot. 40401294Slling */ 40411294Slling ret = do_rollback(zhp); 40421294Slling 40431294Slling if (ret != 0) { 40441294Slling (void) changelist_postfix(clp); 40451294Slling goto out; 40461294Slling } 40471294Slling 40481294Slling /* 40491294Slling * We only want to re-mount the filesystem if it was mounted in the 40501294Slling * first place. 40511294Slling */ 40521294Slling ret = changelist_postfix(clp); 40531294Slling 40541294Slling out: 40551294Slling changelist_free(clp); 40561294Slling return (ret); 40571294Slling } 40581294Slling 40591294Slling /* 4060789Sahrens * Iterate over all dependents for a given dataset. This includes both 4061789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 4062789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 4063789Sahrens * libzfs_graph.c. 4064789Sahrens */ 4065789Sahrens int 40662474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 40672474Seschrock zfs_iter_f func, void *data) 4068789Sahrens { 4069789Sahrens char **dependents; 4070789Sahrens size_t count; 4071789Sahrens int i; 4072789Sahrens zfs_handle_t *child; 4073789Sahrens int ret = 0; 4074789Sahrens 40752474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 40762474Seschrock &dependents, &count) != 0) 40772474Seschrock return (-1); 40782474Seschrock 4079789Sahrens for (i = 0; i < count; i++) { 40802082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 40812082Seschrock dependents[i])) == NULL) 4082789Sahrens continue; 4083789Sahrens 4084789Sahrens if ((ret = func(child, data)) != 0) 4085789Sahrens break; 4086789Sahrens } 4087789Sahrens 4088789Sahrens for (i = 0; i < count; i++) 4089789Sahrens free(dependents[i]); 4090789Sahrens free(dependents); 4091789Sahrens 4092789Sahrens return (ret); 4093789Sahrens } 4094789Sahrens 4095789Sahrens /* 4096789Sahrens * Renames the given dataset. 4097789Sahrens */ 4098789Sahrens int 40994490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 4100789Sahrens { 4101789Sahrens int ret; 4102789Sahrens zfs_cmd_t zc = { 0 }; 4103789Sahrens char *delim; 41044007Smmusante prop_changelist_t *cl = NULL; 41054007Smmusante zfs_handle_t *zhrp = NULL; 41064007Smmusante char *parentname = NULL; 4107789Sahrens char parent[ZFS_MAXNAMELEN]; 41082082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 41092082Seschrock char errbuf[1024]; 4110789Sahrens 4111789Sahrens /* if we have the same exact name, just return success */ 4112789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 4113789Sahrens return (0); 4114789Sahrens 41152082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 41162082Seschrock "cannot rename to '%s'"), target); 41172082Seschrock 4118789Sahrens /* 4119789Sahrens * Make sure the target name is valid 4120789Sahrens */ 4121789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 41222665Snd150628 if ((strchr(target, '@') == NULL) || 41232665Snd150628 *target == '@') { 41242665Snd150628 /* 41252665Snd150628 * Snapshot target name is abbreviated, 41262665Snd150628 * reconstruct full dataset name 41272665Snd150628 */ 41282665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 41292665Snd150628 sizeof (parent)); 41302665Snd150628 delim = strchr(parent, '@'); 41312665Snd150628 if (strchr(target, '@') == NULL) 41322665Snd150628 *(++delim) = '\0'; 41332665Snd150628 else 41342665Snd150628 *delim = '\0'; 41352665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 41362665Snd150628 target = parent; 41372665Snd150628 } else { 41382665Snd150628 /* 41392665Snd150628 * Make sure we're renaming within the same dataset. 41402665Snd150628 */ 41412665Snd150628 delim = strchr(target, '@'); 41422665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 41432665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 41442665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41452665Snd150628 "snapshots must be part of same " 41462665Snd150628 "dataset")); 41472665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 41483912Slling errbuf)); 41492665Snd150628 } 4150789Sahrens } 41512665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 41522665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4153789Sahrens } else { 41544007Smmusante if (recursive) { 41554007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41564007Smmusante "recursive rename must be a snapshot")); 41574007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 41584007Smmusante } 41594007Smmusante 41602665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 41612665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 41622676Seschrock uint64_t unused; 41632676Seschrock 4164789Sahrens /* validate parents */ 41654490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4166789Sahrens return (-1); 4167789Sahrens 4168789Sahrens (void) parent_name(target, parent, sizeof (parent)); 4169789Sahrens 4170789Sahrens /* make sure we're in the same pool */ 4171789Sahrens verify((delim = strchr(target, '/')) != NULL); 4172789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4173789Sahrens zhp->zfs_name[delim - target] != '/') { 41742082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41752082Seschrock "datasets must be within same pool")); 41762082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4177789Sahrens } 41782440Snd150628 41792440Snd150628 /* new name cannot be a child of the current dataset name */ 41802440Snd150628 if (strncmp(parent, zhp->zfs_name, 41813912Slling strlen(zhp->zfs_name)) == 0) { 41822440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41832440Snd150628 "New dataset name cannot be a descendent of " 41842440Snd150628 "current dataset name")); 41852440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 41862440Snd150628 } 4187789Sahrens } 4188789Sahrens 41892082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 41902082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 41912082Seschrock 4192789Sahrens if (getzoneid() == GLOBAL_ZONEID && 4193789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 41942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41952082Seschrock "dataset is used in a non-global zone")); 41962082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4197789Sahrens } 4198789Sahrens 41994007Smmusante if (recursive) { 42004007Smmusante struct destroydata dd; 42014007Smmusante 42024183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 42034183Smmusante if (parentname == NULL) { 42044183Smmusante ret = -1; 42054183Smmusante goto error; 42064183Smmusante } 42074007Smmusante delim = strchr(parentname, '@'); 42084007Smmusante *delim = '\0'; 42094007Smmusante zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY); 42104007Smmusante if (zhrp == NULL) { 42114183Smmusante ret = -1; 42124183Smmusante goto error; 42134007Smmusante } 42144007Smmusante 42154007Smmusante dd.snapname = delim + 1; 42164007Smmusante dd.gotone = B_FALSE; 42174183Smmusante dd.closezhp = B_TRUE; 42184007Smmusante 42194007Smmusante /* We remove any zvol links prior to renaming them */ 42204007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 42214007Smmusante if (ret) { 42224007Smmusante goto error; 42234007Smmusante } 42244007Smmusante } else { 42254007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 42264007Smmusante return (-1); 42274007Smmusante 42284007Smmusante if (changelist_haszonedchild(cl)) { 42294007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42304007Smmusante "child dataset with inherited mountpoint is used " 42314007Smmusante "in a non-global zone")); 42324007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 42334007Smmusante goto error; 42344007Smmusante } 42354007Smmusante 42364007Smmusante if ((ret = changelist_prefix(cl)) != 0) 42374007Smmusante goto error; 4238789Sahrens } 4239789Sahrens 42402676Seschrock if (ZFS_IS_VOLUME(zhp)) 4241789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4242789Sahrens else 4243789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4244789Sahrens 42452665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 42462676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 42472665Snd150628 42484007Smmusante zc.zc_cookie = recursive; 42494007Smmusante 42504543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 42514007Smmusante /* 42524007Smmusante * if it was recursive, the one that actually failed will 42534007Smmusante * be in zc.zc_name 42544007Smmusante */ 42554007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 42564007Smmusante "cannot rename to '%s'"), zc.zc_name); 42574007Smmusante 42584007Smmusante if (recursive && errno == EEXIST) { 42594007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42604007Smmusante "a child dataset already has a snapshot " 42614007Smmusante "with the new name")); 42624007Smmusante (void) zfs_error(hdl, EZFS_CROSSTARGET, errbuf); 42634007Smmusante } else { 42644007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 42654007Smmusante } 4266789Sahrens 4267789Sahrens /* 4268789Sahrens * On failure, we still want to remount any filesystems that 4269789Sahrens * were previously mounted, so we don't alter the system state. 4270789Sahrens */ 42714007Smmusante if (recursive) { 42724007Smmusante struct createdata cd; 42734007Smmusante 42744007Smmusante /* only create links for datasets that had existed */ 42754007Smmusante cd.cd_snapname = delim + 1; 42764007Smmusante cd.cd_ifexists = B_TRUE; 42774007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 42784007Smmusante &cd); 42794007Smmusante } else { 42804007Smmusante (void) changelist_postfix(cl); 42814007Smmusante } 4282789Sahrens } else { 42834007Smmusante if (recursive) { 42844007Smmusante struct createdata cd; 42854007Smmusante 42864007Smmusante /* only create links for datasets that had existed */ 42874007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 42884007Smmusante cd.cd_ifexists = B_TRUE; 42894007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 42904007Smmusante &cd); 42914007Smmusante } else { 42924007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 42934007Smmusante ret = changelist_postfix(cl); 42944007Smmusante } 4295789Sahrens } 4296789Sahrens 4297789Sahrens error: 42984007Smmusante if (parentname) { 42994007Smmusante free(parentname); 43004007Smmusante } 43014007Smmusante if (zhrp) { 43024007Smmusante zfs_close(zhrp); 43034007Smmusante } 43044007Smmusante if (cl) { 43054007Smmusante changelist_free(cl); 43064007Smmusante } 4307789Sahrens return (ret); 4308789Sahrens } 4309789Sahrens 4310789Sahrens /* 4311789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4312789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 4313789Sahrens */ 4314789Sahrens int 43152082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4316789Sahrens { 43174007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 43184007Smmusante } 43194007Smmusante 43204007Smmusante static int 43214007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 43224007Smmusante { 4323789Sahrens zfs_cmd_t zc = { 0 }; 43242082Seschrock di_devlink_handle_t dhdl; 43254543Smarks priv_set_t *priv_effective; 43264543Smarks int privileged; 4327789Sahrens 4328789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4329789Sahrens 4330789Sahrens /* 4331789Sahrens * Issue the appropriate ioctl. 4332789Sahrens */ 43332082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4334789Sahrens switch (errno) { 4335789Sahrens case EEXIST: 4336789Sahrens /* 4337789Sahrens * Silently ignore the case where the link already 4338789Sahrens * exists. This allows 'zfs volinit' to be run multiple 4339789Sahrens * times without errors. 4340789Sahrens */ 4341789Sahrens return (0); 4342789Sahrens 43434007Smmusante case ENOENT: 43444007Smmusante /* 43454007Smmusante * Dataset does not exist in the kernel. If we 43464007Smmusante * don't care (see zfs_rename), then ignore the 43474007Smmusante * error quietly. 43484007Smmusante */ 43494007Smmusante if (ifexists) { 43504007Smmusante return (0); 43514007Smmusante } 43524007Smmusante 43534007Smmusante /* FALLTHROUGH */ 43544007Smmusante 4355789Sahrens default: 43563237Slling return (zfs_standard_error_fmt(hdl, errno, 43572082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 43582082Seschrock "for '%s'"), dataset)); 4359789Sahrens } 4360789Sahrens } 4361789Sahrens 4362789Sahrens /* 43634543Smarks * If privileged call devfsadm and wait for the links to 43644543Smarks * magically appear. 43654543Smarks * Otherwise, print out an informational message. 4366789Sahrens */ 43674543Smarks 43684543Smarks priv_effective = priv_allocset(); 43694543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 43704543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 43714543Smarks priv_freeset(priv_effective); 43724543Smarks 43734543Smarks if (privileged) { 43744543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 43754543Smarks DI_MAKE_LINK)) == NULL) { 43764543Smarks zfs_error_aux(hdl, strerror(errno)); 43774543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 43784543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 43794543Smarks "for '%s'"), dataset); 43804543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 43814543Smarks return (-1); 43824543Smarks } else { 43834543Smarks (void) di_devlink_fini(&dhdl); 43844543Smarks } 4385789Sahrens } else { 43864543Smarks char pathname[MAXPATHLEN]; 43874543Smarks struct stat64 statbuf; 43884543Smarks int i; 43894543Smarks 43904543Smarks #define MAX_WAIT 10 43914543Smarks 43924543Smarks /* 43934543Smarks * This is the poor mans way of waiting for the link 43944543Smarks * to show up. If after 10 seconds we still don't 43954543Smarks * have it, then print out a message. 43964543Smarks */ 43974543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 43984543Smarks dataset); 43994543Smarks 44004543Smarks for (i = 0; i != MAX_WAIT; i++) { 44014543Smarks if (stat64(pathname, &statbuf) == 0) 44024543Smarks break; 44034543Smarks (void) sleep(1); 44044543Smarks } 44054543Smarks if (i == MAX_WAIT) 44064543Smarks (void) printf(gettext("%s may not be immediately " 44074543Smarks "available\n"), pathname); 4408789Sahrens } 4409789Sahrens 4410789Sahrens return (0); 4411789Sahrens } 4412789Sahrens 4413789Sahrens /* 4414789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4415789Sahrens */ 4416789Sahrens int 44172082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4418789Sahrens { 4419789Sahrens zfs_cmd_t zc = { 0 }; 4420789Sahrens 4421789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4422789Sahrens 44232082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4424789Sahrens switch (errno) { 4425789Sahrens case ENXIO: 4426789Sahrens /* 4427789Sahrens * Silently ignore the case where the link no longer 4428789Sahrens * exists, so that 'zfs volfini' can be run multiple 4429789Sahrens * times without errors. 4430789Sahrens */ 4431789Sahrens return (0); 4432789Sahrens 4433789Sahrens default: 44343237Slling return (zfs_standard_error_fmt(hdl, errno, 44352082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 44362082Seschrock "links for '%s'"), dataset)); 4437789Sahrens } 4438789Sahrens } 4439789Sahrens 4440789Sahrens return (0); 4441789Sahrens } 44422676Seschrock 44432676Seschrock nvlist_t * 44442676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 44452676Seschrock { 44462676Seschrock return (zhp->zfs_user_props); 44472676Seschrock } 44482676Seschrock 44492676Seschrock /* 44504451Seschrock * Given a comma-separated list of properties, construct a property list 44512676Seschrock * containing both user-defined and native properties. This function will 44522676Seschrock * return a NULL list if 'all' is specified, which can later be expanded on a 44532676Seschrock * per-dataset basis by zfs_expand_proplist(). 44542676Seschrock */ 44552676Seschrock int 44563912Slling zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields, 44573912Slling zfs_proplist_t **listp, zfs_type_t type) 44582676Seschrock { 44592676Seschrock size_t len; 44602676Seschrock char *s, *p; 44612676Seschrock char c; 44622676Seschrock zfs_prop_t prop; 44632676Seschrock zfs_proplist_t *entry; 44642676Seschrock zfs_proplist_t **last; 44652676Seschrock 44662676Seschrock *listp = NULL; 44672676Seschrock last = listp; 44682676Seschrock 44692676Seschrock /* 44702676Seschrock * If 'all' is specified, return a NULL list. 44712676Seschrock */ 44722676Seschrock if (strcmp(fields, "all") == 0) 44732676Seschrock return (0); 44742676Seschrock 44752676Seschrock /* 44762676Seschrock * If no fields were specified, return an error. 44772676Seschrock */ 44782676Seschrock if (fields[0] == '\0') { 44792676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44802676Seschrock "no properties specified")); 44812676Seschrock return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 44822676Seschrock "bad property list"))); 44832676Seschrock } 44842676Seschrock 44852676Seschrock /* 44862676Seschrock * It would be nice to use getsubopt() here, but the inclusion of column 44872676Seschrock * aliases makes this more effort than it's worth. 44882676Seschrock */ 44892676Seschrock s = fields; 44902676Seschrock while (*s != '\0') { 44912676Seschrock if ((p = strchr(s, ',')) == NULL) { 44922676Seschrock len = strlen(s); 44932676Seschrock p = s + len; 44942676Seschrock } else { 44952676Seschrock len = p - s; 44962676Seschrock } 44972676Seschrock 44982676Seschrock /* 44992676Seschrock * Check for empty options. 45002676Seschrock */ 45012676Seschrock if (len == 0) { 45022676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45032676Seschrock "empty property name")); 45042676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 45052676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 45062676Seschrock } 45072676Seschrock 45082676Seschrock /* 45092676Seschrock * Check all regular property names. 45102676Seschrock */ 45112676Seschrock c = s[len]; 45122676Seschrock s[len] = '\0'; 45134451Seschrock prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) : 45144451Seschrock zfs_name_to_prop(s); 45153912Slling 45163912Slling if (prop != ZFS_PROP_INVAL && 45173912Slling !zfs_prop_valid_for_type(prop, type)) 45183912Slling prop = ZFS_PROP_INVAL; 45192676Seschrock 45202676Seschrock /* 45213912Slling * When no property table entry can be found, return failure if 45223912Slling * this is a pool property or if this isn't a user-defined 45233912Slling * dataset property, 45242676Seschrock */ 45253912Slling if (prop == ZFS_PROP_INVAL && 45263912Slling (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) { 45272676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45282676Seschrock "invalid property '%s'"), s); 45292676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 45302676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 45312676Seschrock } 45322676Seschrock 45332676Seschrock if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL) 45342676Seschrock return (-1); 45352676Seschrock 45362676Seschrock entry->pl_prop = prop; 45372676Seschrock if (prop == ZFS_PROP_INVAL) { 45382676Seschrock if ((entry->pl_user_prop = 45392676Seschrock zfs_strdup(hdl, s)) == NULL) { 45402676Seschrock free(entry); 45412676Seschrock return (-1); 45422676Seschrock } 45432676Seschrock entry->pl_width = strlen(s); 45442676Seschrock } else { 45452676Seschrock entry->pl_width = zfs_prop_width(prop, 45462676Seschrock &entry->pl_fixed); 45472676Seschrock } 45482676Seschrock 45492676Seschrock *last = entry; 45502676Seschrock last = &entry->pl_next; 45512676Seschrock 45522676Seschrock s = p; 45532676Seschrock if (c == ',') 45542676Seschrock s++; 45552676Seschrock } 45562676Seschrock 45572676Seschrock return (0); 45582676Seschrock } 45592676Seschrock 45603912Slling int 45613912Slling zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp) 45623912Slling { 45633912Slling return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY)); 45643912Slling } 45653912Slling 45662676Seschrock void 45672676Seschrock zfs_free_proplist(zfs_proplist_t *pl) 45682676Seschrock { 45692676Seschrock zfs_proplist_t *next; 45702676Seschrock 45712676Seschrock while (pl != NULL) { 45722676Seschrock next = pl->pl_next; 45732676Seschrock free(pl->pl_user_prop); 45742676Seschrock free(pl); 45752676Seschrock pl = next; 45762676Seschrock } 45772676Seschrock } 45782676Seschrock 45793654Sgw25295 typedef struct expand_data { 45803654Sgw25295 zfs_proplist_t **last; 45813654Sgw25295 libzfs_handle_t *hdl; 45823654Sgw25295 } expand_data_t; 45833654Sgw25295 45843654Sgw25295 static zfs_prop_t 45853654Sgw25295 zfs_expand_proplist_cb(zfs_prop_t prop, void *cb) 45863654Sgw25295 { 45873654Sgw25295 zfs_proplist_t *entry; 45883654Sgw25295 expand_data_t *edp = cb; 45893654Sgw25295 45903654Sgw25295 if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL) 45913654Sgw25295 return (ZFS_PROP_INVAL); 45923654Sgw25295 45933654Sgw25295 entry->pl_prop = prop; 45943654Sgw25295 entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed); 45953654Sgw25295 entry->pl_all = B_TRUE; 45963654Sgw25295 45973654Sgw25295 *(edp->last) = entry; 45983654Sgw25295 edp->last = &entry->pl_next; 45993654Sgw25295 46003654Sgw25295 return (ZFS_PROP_CONT); 46013654Sgw25295 } 46023654Sgw25295 46032676Seschrock int 46043912Slling zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp, 46053912Slling zfs_type_t type) 46062676Seschrock { 46072676Seschrock zfs_proplist_t *entry; 46083912Slling zfs_proplist_t **last; 46093654Sgw25295 expand_data_t exp; 46102676Seschrock 46112676Seschrock if (*plp == NULL) { 46122676Seschrock /* 46132676Seschrock * If this is the very first time we've been called for an 'all' 46142676Seschrock * specification, expand the list to include all native 46152676Seschrock * properties. 46162676Seschrock */ 46172676Seschrock last = plp; 46183654Sgw25295 46193654Sgw25295 exp.last = last; 46203654Sgw25295 exp.hdl = hdl; 46213654Sgw25295 46223912Slling if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type, 46234597Stimf B_FALSE, B_FALSE) == ZFS_PROP_INVAL) 46243654Sgw25295 return (-1); 46252676Seschrock 46262676Seschrock /* 46272676Seschrock * Add 'name' to the beginning of the list, which is handled 46282676Seschrock * specially. 46292676Seschrock */ 46302676Seschrock if ((entry = zfs_alloc(hdl, 46312676Seschrock sizeof (zfs_proplist_t))) == NULL) 46322676Seschrock return (-1); 46332676Seschrock 46342676Seschrock entry->pl_prop = ZFS_PROP_NAME; 46352676Seschrock entry->pl_width = zfs_prop_width(ZFS_PROP_NAME, 46362676Seschrock &entry->pl_fixed); 46372676Seschrock entry->pl_all = B_TRUE; 46382676Seschrock entry->pl_next = *plp; 46392676Seschrock *plp = entry; 46402676Seschrock } 46413912Slling return (0); 46423912Slling } 46433912Slling 46443912Slling /* 46453912Slling * This function is used by 'zfs list' to determine the exact set of columns to 46463912Slling * display, and their maximum widths. This does two main things: 46473912Slling * 46483912Slling * - If this is a list of all properties, then expand the list to include 46493912Slling * all native properties, and set a flag so that for each dataset we look 46503912Slling * for new unique user properties and add them to the list. 46513912Slling * 46523912Slling * - For non fixed-width properties, keep track of the maximum width seen 46533912Slling * so that we can size the column appropriately. 46543912Slling */ 46553912Slling int 46563912Slling zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp) 46573912Slling { 46583912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 46593912Slling zfs_proplist_t *entry; 46603912Slling zfs_proplist_t **last, **start; 46613912Slling nvlist_t *userprops, *propval; 46623912Slling nvpair_t *elem; 46633912Slling char *strval; 46643912Slling char buf[ZFS_MAXPROPLEN]; 46653912Slling 46663912Slling if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0) 46673912Slling return (-1); 46682676Seschrock 46692676Seschrock userprops = zfs_get_user_props(zhp); 46702676Seschrock 46712676Seschrock entry = *plp; 46722676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 46732676Seschrock /* 46742676Seschrock * Go through and add any user properties as necessary. We 46752676Seschrock * start by incrementing our list pointer to the first 46762676Seschrock * non-native property. 46772676Seschrock */ 46782676Seschrock start = plp; 46792676Seschrock while (*start != NULL) { 46802676Seschrock if ((*start)->pl_prop == ZFS_PROP_INVAL) 46812676Seschrock break; 46822676Seschrock start = &(*start)->pl_next; 46832676Seschrock } 46842676Seschrock 46852676Seschrock elem = NULL; 46862676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 46872676Seschrock /* 46882676Seschrock * See if we've already found this property in our list. 46892676Seschrock */ 46902676Seschrock for (last = start; *last != NULL; 46912676Seschrock last = &(*last)->pl_next) { 46922676Seschrock if (strcmp((*last)->pl_user_prop, 46932676Seschrock nvpair_name(elem)) == 0) 46942676Seschrock break; 46952676Seschrock } 46962676Seschrock 46972676Seschrock if (*last == NULL) { 46982676Seschrock if ((entry = zfs_alloc(hdl, 46992676Seschrock sizeof (zfs_proplist_t))) == NULL || 47002676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 47012676Seschrock nvpair_name(elem)))) == NULL) { 47022676Seschrock free(entry); 47032676Seschrock return (-1); 47042676Seschrock } 47052676Seschrock 47062676Seschrock entry->pl_prop = ZFS_PROP_INVAL; 47072676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 47082676Seschrock entry->pl_all = B_TRUE; 47092676Seschrock *last = entry; 47102676Seschrock } 47112676Seschrock } 47122676Seschrock } 47132676Seschrock 47142676Seschrock /* 47152676Seschrock * Now go through and check the width of any non-fixed columns 47162676Seschrock */ 47172676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 47182676Seschrock if (entry->pl_fixed) 47192676Seschrock continue; 47202676Seschrock 47212676Seschrock if (entry->pl_prop != ZFS_PROP_INVAL) { 47222676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 47232676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 47242676Seschrock if (strlen(buf) > entry->pl_width) 47252676Seschrock entry->pl_width = strlen(buf); 47262676Seschrock } 47272676Seschrock } else if (nvlist_lookup_nvlist(userprops, 47282676Seschrock entry->pl_user_prop, &propval) == 0) { 47292676Seschrock verify(nvlist_lookup_string(propval, 47302676Seschrock ZFS_PROP_VALUE, &strval) == 0); 47312676Seschrock if (strlen(strval) > entry->pl_width) 47322676Seschrock entry->pl_width = strlen(strval); 47332676Seschrock } 47342676Seschrock } 47352676Seschrock 47362676Seschrock return (0); 47372676Seschrock } 47384543Smarks 47394543Smarks int 47404543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 47414543Smarks { 47424543Smarks zfs_cmd_t zc = { 0 }; 47434543Smarks nvlist_t *nvp; 47444543Smarks size_t sz; 47454543Smarks gid_t gid; 47464543Smarks uid_t uid; 47474543Smarks const gid_t *groups; 47484543Smarks int group_cnt; 47494543Smarks int error; 47504543Smarks 47514543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 47524543Smarks return (no_memory(hdl)); 47534543Smarks 47544543Smarks uid = ucred_geteuid(cred); 47554543Smarks gid = ucred_getegid(cred); 47564543Smarks group_cnt = ucred_getgroups(cred, &groups); 47574543Smarks 47584543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 47594543Smarks return (1); 47604543Smarks 47614543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 47624543Smarks nvlist_free(nvp); 47634543Smarks return (1); 47644543Smarks } 47654543Smarks 47664543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 47674543Smarks nvlist_free(nvp); 47684543Smarks return (1); 47694543Smarks } 47704543Smarks 47714543Smarks if (nvlist_add_uint32_array(nvp, 47724543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 47734543Smarks nvlist_free(nvp); 47744543Smarks return (1); 47754543Smarks } 47764543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 47774543Smarks 47784543Smarks if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz)) 47794543Smarks return (-1); 47804543Smarks 47814543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 47824543Smarks nvlist_free(nvp); 47834543Smarks return (error); 47844543Smarks } 47854543Smarks 47864543Smarks int 47874543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 47884543Smarks void *export, void *sharetab, int sharemax, boolean_t share_on) 47894543Smarks { 47904543Smarks zfs_cmd_t zc = { 0 }; 47914543Smarks int error; 47924543Smarks 47934543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 47944543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 47954543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 47964543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 47974543Smarks zc.zc_share.z_sharetype = share_on; 47984543Smarks zc.zc_share.z_sharemax = sharemax; 47994543Smarks 48004543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 48014543Smarks return (error); 48024543Smarks } 4803