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> 44*4543Smarks #include <sys/avl.h> 45*4543Smarks #include <priv.h> 46*4543Smarks #include <pwd.h> 47*4543Smarks #include <grp.h> 48*4543Smarks #include <stddef.h> 49*4543Smarks #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" 59*4543Smarks #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); 317*4543Smarks char *logstr; 3182082Seschrock 3192082Seschrock if (zhp == NULL) 3202082Seschrock return (NULL); 3212082Seschrock 3222082Seschrock zhp->zfs_hdl = hdl; 323789Sahrens 324*4543Smarks /* 325*4543Smarks * Preserve history log string. 326*4543Smarks * any changes performed here will be 327*4543Smarks * logged as an internal event. 328*4543Smarks */ 329*4543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 330*4543Smarks 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) { 335*4543Smarks 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) { 375*4543Smarks 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 402*4543Smarks 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) { 9012676Seschrock case ZFS_PROP_RECORDSIZE: 9022676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 9032676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 9042676Seschrock if (intval < SPA_MINBLOCKSIZE || 9052676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 9062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9072676Seschrock "'%s' must be power of 2 from %u " 9082676Seschrock "to %uk"), propname, 9092676Seschrock (uint_t)SPA_MINBLOCKSIZE, 9102676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 9112676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9122676Seschrock goto error; 913789Sahrens } 914789Sahrens break; 915789Sahrens 9163126Sahl case ZFS_PROP_SHAREISCSI: 9173126Sahl if (strcmp(strval, "off") != 0 && 9183126Sahl strcmp(strval, "on") != 0 && 9193126Sahl strcmp(strval, "type=disk") != 0) { 9203126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9213126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 9223126Sahl propname); 9233126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9243126Sahl goto error; 9253126Sahl } 9263126Sahl 9273126Sahl break; 9283126Sahl 9292676Seschrock case ZFS_PROP_MOUNTPOINT: 9302676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 9312676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 9322676Seschrock break; 9332676Seschrock 9342676Seschrock if (strval[0] != '/') { 9352082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9362676Seschrock "'%s' must be an absolute path, " 9372676Seschrock "'none', or 'legacy'"), propname); 9382676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9392676Seschrock goto error; 940789Sahrens } 9413126Sahl /*FALLTHRU*/ 9423126Sahl 9433126Sahl case ZFS_PROP_SHARENFS: 9443126Sahl /* 9453126Sahl * For the mountpoint and sharenfs properties, check if 9463126Sahl * it can be set in a global/non-global zone based on 9473126Sahl * the zoned property value: 9483126Sahl * 9493126Sahl * global zone non-global zone 9503126Sahl * -------------------------------------------------- 9513126Sahl * zoned=on mountpoint (no) mountpoint (yes) 9523126Sahl * sharenfs (no) sharenfs (no) 9533126Sahl * 9543126Sahl * zoned=off mountpoint (yes) N/A 9553126Sahl * sharenfs (yes) 9563126Sahl */ 9572676Seschrock if (zoned) { 9582676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 9592676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9602676Seschrock "'%s' cannot be set on " 9612676Seschrock "dataset in a non-global zone"), 9622676Seschrock propname); 9632676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9642676Seschrock errbuf); 9652676Seschrock goto error; 9662676Seschrock } else if (prop == ZFS_PROP_SHARENFS) { 9672676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9682676Seschrock "'%s' cannot be set in " 9692676Seschrock "a non-global zone"), propname); 9702676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9712676Seschrock errbuf); 9722676Seschrock goto error; 9732676Seschrock } 9742676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 9752676Seschrock /* 9762676Seschrock * If zoned property is 'off', this must be in 9772676Seschrock * a globle zone. If not, something is wrong. 9782676Seschrock */ 9792676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9802676Seschrock "'%s' cannot be set while dataset " 9812676Seschrock "'zoned' property is set"), propname); 9822676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 9832676Seschrock goto error; 9842676Seschrock } 9853126Sahl 9864180Sdougm /* 9874180Sdougm * At this point, it is legitimate to set the 9884180Sdougm * property. Now we want to make sure that the 9894180Sdougm * property value is valid if it is sharenfs. 9904180Sdougm */ 9914180Sdougm if (prop == ZFS_PROP_SHARENFS && 9924217Seschrock strcmp(strval, "on") != 0 && 9934217Seschrock strcmp(strval, "off") != 0) { 9944180Sdougm 9954180Sdougm /* 9964180Sdougm * Must be an NFS option string so 9974180Sdougm * init the libshare in order to 9984180Sdougm * enable the parser and then parse 9994180Sdougm * the options. We use the control API 10004180Sdougm * since we don't care about the 10014180Sdougm * current configuration and don't 10024180Sdougm * want the overhead of loading it 10034180Sdougm * until we actually do something. 10044180Sdougm */ 10054180Sdougm 10064217Seschrock if (zfs_init_libshare(hdl, 10074217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 10084217Seschrock /* 10094217Seschrock * An error occurred so we can't do 10104217Seschrock * anything 10114217Seschrock */ 10124217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10134217Seschrock "'%s' cannot be set: problem " 10144217Seschrock "in share initialization"), 10154217Seschrock propname); 10164217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10174217Seschrock errbuf); 10184217Seschrock goto error; 10194217Seschrock } 10204217Seschrock 10214217Seschrock if (zfs_parse_options(strval, "nfs") != SA_OK) { 10224217Seschrock /* 10234217Seschrock * There was an error in parsing so 10244217Seschrock * deal with it by issuing an error 10254217Seschrock * message and leaving after 10264217Seschrock * uninitializing the the libshare 10274217Seschrock * interface. 10284217Seschrock */ 10294217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10304217Seschrock "'%s' cannot be set to invalid " 10314217Seschrock "options"), propname); 10324217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10334217Seschrock errbuf); 10344217Seschrock zfs_uninit_libshare(hdl); 10354217Seschrock goto error; 10364217Seschrock } 10374180Sdougm zfs_uninit_libshare(hdl); 10384180Sdougm } 10394180Sdougm 10403126Sahl break; 10413912Slling 10424451Seschrock case ZPOOL_PROP_BOOTFS: 10433912Slling /* 10443912Slling * bootfs property value has to be a dataset name and 10453912Slling * the dataset has to be in the same pool as it sets to. 10463912Slling */ 10473912Slling if (strval[0] != '\0' && (!zfs_name_valid(strval, 10483912Slling ZFS_TYPE_FILESYSTEM) || !bootfs_poolname_valid( 10493912Slling pool_name, strval))) { 10503912Slling 10513912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 10523912Slling "is an invalid name"), strval); 10533912Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 10543912Slling goto error; 10553912Slling } 10563912Slling break; 10572676Seschrock } 10582676Seschrock 10592676Seschrock /* 10602676Seschrock * For changes to existing volumes, we have some additional 10612676Seschrock * checks to enforce. 10622676Seschrock */ 10632676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 10642676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 10652676Seschrock ZFS_PROP_VOLSIZE); 10662676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 10672676Seschrock ZFS_PROP_VOLBLOCKSIZE); 10682676Seschrock char buf[64]; 10692676Seschrock 10702676Seschrock switch (prop) { 10712676Seschrock case ZFS_PROP_RESERVATION: 10722676Seschrock if (intval > volsize) { 10732676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10742676Seschrock "'%s' is greater than current " 10752676Seschrock "volume size"), propname); 10762676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10772676Seschrock errbuf); 10782676Seschrock goto error; 10792676Seschrock } 10802676Seschrock break; 10812676Seschrock 10822676Seschrock case ZFS_PROP_VOLSIZE: 10832676Seschrock if (intval % blocksize != 0) { 10842676Seschrock zfs_nicenum(blocksize, buf, 10852676Seschrock sizeof (buf)); 10862676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10872676Seschrock "'%s' must be a multiple of " 10882676Seschrock "volume block size (%s)"), 10892676Seschrock propname, buf); 10902676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10912676Seschrock errbuf); 10922676Seschrock goto error; 10932676Seschrock } 10942676Seschrock 10952676Seschrock if (intval == 0) { 10962676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10972676Seschrock "'%s' cannot be zero"), 10982676Seschrock propname); 10992676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 11002676Seschrock errbuf); 11012676Seschrock goto error; 1102789Sahrens } 11033126Sahl break; 1104789Sahrens } 1105789Sahrens } 1106789Sahrens } 1107789Sahrens 11082676Seschrock /* 11092676Seschrock * If this is an existing volume, and someone is setting the volsize, 11102676Seschrock * make sure that it matches the reservation, or add it if necessary. 11112676Seschrock */ 11122676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 11132676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 11142676Seschrock &intval) == 0) { 11152676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 11162676Seschrock ZFS_PROP_VOLSIZE); 11172676Seschrock uint64_t old_reservation = zfs_prop_get_int(zhp, 11182676Seschrock ZFS_PROP_RESERVATION); 11192676Seschrock uint64_t new_reservation; 11202676Seschrock 11212676Seschrock if (old_volsize == old_reservation && 11222676Seschrock nvlist_lookup_uint64(ret, 11232676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 11242676Seschrock &new_reservation) != 0) { 11252676Seschrock if (nvlist_add_uint64(ret, 11262676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 11272676Seschrock intval) != 0) { 11282676Seschrock (void) no_memory(hdl); 11292676Seschrock goto error; 11302676Seschrock } 11312676Seschrock } 11322676Seschrock } 11332676Seschrock 11342676Seschrock return (ret); 11352676Seschrock 11362676Seschrock error: 11372676Seschrock nvlist_free(ret); 11382676Seschrock return (NULL); 1139789Sahrens } 1140789Sahrens 1141*4543Smarks static int 1142*4543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 1143*4543Smarks uint64_t *ret_who) 1144*4543Smarks { 1145*4543Smarks struct passwd *pwd; 1146*4543Smarks struct group *grp; 1147*4543Smarks uid_t id; 1148*4543Smarks 1149*4543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 1150*4543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 1151*4543Smarks *ret_who = -1; 1152*4543Smarks return (0); 1153*4543Smarks } 1154*4543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 1155*4543Smarks return (EZFS_BADWHO); 1156*4543Smarks 1157*4543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 1158*4543Smarks strcmp(who, "everyone") == 0) { 1159*4543Smarks *ret_who = -1; 1160*4543Smarks *who_type = ZFS_DELEG_EVERYONE; 1161*4543Smarks return (0); 1162*4543Smarks } 1163*4543Smarks 1164*4543Smarks pwd = getpwnam(who); 1165*4543Smarks grp = getgrnam(who); 1166*4543Smarks 1167*4543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 1168*4543Smarks *ret_who = pwd->pw_uid; 1169*4543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 1170*4543Smarks *ret_who = grp->gr_gid; 1171*4543Smarks } else if (pwd) { 1172*4543Smarks *ret_who = pwd->pw_uid; 1173*4543Smarks *who_type = ZFS_DELEG_USER; 1174*4543Smarks } else if (grp) { 1175*4543Smarks *ret_who = grp->gr_gid; 1176*4543Smarks *who_type = ZFS_DELEG_GROUP; 1177*4543Smarks } else { 1178*4543Smarks char *end; 1179*4543Smarks 1180*4543Smarks id = strtol(who, &end, 10); 1181*4543Smarks if (errno != 0 || *end != '\0') { 1182*4543Smarks return (EZFS_BADWHO); 1183*4543Smarks } else { 1184*4543Smarks *ret_who = id; 1185*4543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 1186*4543Smarks *who_type = ZFS_DELEG_USER; 1187*4543Smarks } 1188*4543Smarks } 1189*4543Smarks 1190*4543Smarks return (0); 1191*4543Smarks } 1192*4543Smarks 1193*4543Smarks static void 1194*4543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 1195*4543Smarks { 1196*4543Smarks if (perms_nvp != NULL) { 1197*4543Smarks verify(nvlist_add_nvlist(who_nvp, 1198*4543Smarks name, perms_nvp) == 0); 1199*4543Smarks } else { 1200*4543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 1201*4543Smarks } 1202*4543Smarks } 1203*4543Smarks 1204*4543Smarks static void 1205*4543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 1206*4543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 1207*4543Smarks nvlist_t *sets_nvp) 1208*4543Smarks { 1209*4543Smarks boolean_t do_perms, do_sets; 1210*4543Smarks char name[ZFS_MAX_DELEG_NAME]; 1211*4543Smarks 1212*4543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 1213*4543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 1214*4543Smarks 1215*4543Smarks if (!do_perms && !do_sets) 1216*4543Smarks do_perms = do_sets = B_TRUE; 1217*4543Smarks 1218*4543Smarks if (do_perms) { 1219*4543Smarks zfs_deleg_whokey(name, who_type, inherit, 1220*4543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 1221*4543Smarks whostr : (void *)&whoid); 1222*4543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 1223*4543Smarks } 1224*4543Smarks if (do_sets) { 1225*4543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 1226*4543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 1227*4543Smarks whostr : (void *)&whoid); 1228*4543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 1229*4543Smarks } 1230*4543Smarks } 1231*4543Smarks 1232*4543Smarks static void 1233*4543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 1234*4543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 1235*4543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 1236*4543Smarks { 1237*4543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 1238*4543Smarks helper(who_type, whoid, whostr, 0, 1239*4543Smarks who_nvp, perms_nvp, sets_nvp); 1240*4543Smarks } else { 1241*4543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 1242*4543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 1243*4543Smarks who_nvp, perms_nvp, sets_nvp); 1244*4543Smarks } 1245*4543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 1246*4543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 1247*4543Smarks who_nvp, perms_nvp, sets_nvp); 1248*4543Smarks } 1249*4543Smarks } 1250*4543Smarks } 1251*4543Smarks 1252*4543Smarks /* 1253*4543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 1254*4543Smarks * 1255*4543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 1256*4543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 1257*4543Smarks * base attribute named stored in the dsl. 1258*4543Smarks * Arguments: 1259*4543Smarks * 1260*4543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 1261*4543Smarks * whostr may be null for everyone or create perms. 1262*4543Smarks * who_type: is the type of entry in whostr. Typically this will be 1263*4543Smarks * ZFS_DELEG_WHO_UNKNOWN. 1264*4543Smarks * perms: comman separated list of permissions. May be null if user 1265*4543Smarks * is requested to remove permissions by who. 1266*4543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 1267*4543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 1268*4543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 1269*4543Smarks * The output nvp will look something like this. 1270*4543Smarks * ul$1234 -> {create ; destroy } 1271*4543Smarks * Ul$1234 -> { @myset } 1272*4543Smarks * s-$@myset - { snapshot; checksum; compression } 1273*4543Smarks */ 1274*4543Smarks int 1275*4543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 1276*4543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 1277*4543Smarks { 1278*4543Smarks nvlist_t *who_nvp; 1279*4543Smarks nvlist_t *perms_nvp = NULL; 1280*4543Smarks nvlist_t *sets_nvp = NULL; 1281*4543Smarks char errbuf[1024]; 1282*4543Smarks char *who_tok; 1283*4543Smarks int error; 1284*4543Smarks 1285*4543Smarks *nvp = NULL; 1286*4543Smarks 1287*4543Smarks if (perms) { 1288*4543Smarks /* Make sure permission string doesn't have an '=' sign in it */ 1289*4543Smarks if (strchr(perms, '=') != NULL) { 1290*4543Smarks (void) snprintf(errbuf, sizeof (errbuf), 1291*4543Smarks dgettext(TEXT_DOMAIN, 1292*4543Smarks "permissions can't contain equal sign : '%s'"), 1293*4543Smarks perms); 1294*4543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, errbuf)); 1295*4543Smarks } 1296*4543Smarks 1297*4543Smarks if ((error = nvlist_alloc(&perms_nvp, 1298*4543Smarks NV_UNIQUE_NAME, 0)) != 0) { 1299*4543Smarks return (1); 1300*4543Smarks } 1301*4543Smarks if ((error = nvlist_alloc(&sets_nvp, 1302*4543Smarks NV_UNIQUE_NAME, 0)) != 0) { 1303*4543Smarks nvlist_free(perms_nvp); 1304*4543Smarks return (1); 1305*4543Smarks } 1306*4543Smarks } 1307*4543Smarks 1308*4543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 1309*4543Smarks if (perms_nvp) 1310*4543Smarks nvlist_free(perms_nvp); 1311*4543Smarks if (sets_nvp) 1312*4543Smarks nvlist_free(sets_nvp); 1313*4543Smarks return (1); 1314*4543Smarks } 1315*4543Smarks 1316*4543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 1317*4543Smarks namecheck_err_t why; 1318*4543Smarks char what; 1319*4543Smarks 1320*4543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 1321*4543Smarks switch (why) { 1322*4543Smarks case NAME_ERR_NO_AT: 1323*4543Smarks zfs_error_aux(zhp->zfs_hdl, 1324*4543Smarks dgettext(TEXT_DOMAIN, 1325*4543Smarks "set definition must begin with an '@' " 1326*4543Smarks "character")); 1327*4543Smarks } 1328*4543Smarks return (zfs_error(zhp->zfs_hdl, 1329*4543Smarks EZFS_BADPERMSET, whostr)); 1330*4543Smarks } 1331*4543Smarks } 1332*4543Smarks 1333*4543Smarks /* 1334*4543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 1335*4543Smarks * The first nvlist perms_nvp will have normal permissions and the 1336*4543Smarks * other sets_nvp will have only permssion set names in it. 1337*4543Smarks */ 1338*4543Smarks 1339*4543Smarks 1340*4543Smarks while (perms && *perms != '\0') { 1341*4543Smarks char *value; 1342*4543Smarks char *perm_name; 1343*4543Smarks nvlist_t *update_nvp; 1344*4543Smarks int perm_num; 1345*4543Smarks char canonical_name[64]; 1346*4543Smarks char *canonicalp = canonical_name; 1347*4543Smarks 1348*4543Smarks 1349*4543Smarks update_nvp = perms_nvp; 1350*4543Smarks 1351*4543Smarks perm_num = getsubopt(&perms, zfs_deleg_perm_tab, &value); 1352*4543Smarks if (perm_num == -1) { 1353*4543Smarks zfs_prop_t prop; 1354*4543Smarks 1355*4543Smarks prop = zfs_name_to_prop(value); 1356*4543Smarks if (prop != ZFS_PROP_INVAL) { 1357*4543Smarks (void) snprintf(canonical_name, 1358*4543Smarks sizeof (canonical_name), "%s", 1359*4543Smarks zfs_prop_to_name(prop)); 1360*4543Smarks perm_num = getsubopt(&canonicalp, 1361*4543Smarks zfs_deleg_perm_tab, &value); 1362*4543Smarks } 1363*4543Smarks } 1364*4543Smarks if (perm_num != -1) { 1365*4543Smarks perm_name = zfs_deleg_perm_tab[perm_num]; 1366*4543Smarks } else { /* check and see if permission is a named set */ 1367*4543Smarks if (value[0] == '@') { 1368*4543Smarks 1369*4543Smarks /* 1370*4543Smarks * make sure permssion set isn't defined 1371*4543Smarks * in terms of itself. ie. 1372*4543Smarks * @set1 = create,destroy,@set1 1373*4543Smarks */ 1374*4543Smarks if (who_type == ZFS_DELEG_NAMED_SET && 1375*4543Smarks strcmp(value, whostr) == 0) { 1376*4543Smarks nvlist_free(who_nvp); 1377*4543Smarks nvlist_free(perms_nvp); 1378*4543Smarks if (sets_nvp) 1379*4543Smarks nvlist_free(sets_nvp); 1380*4543Smarks (void) snprintf(errbuf, 1381*4543Smarks sizeof (errbuf), 1382*4543Smarks dgettext(TEXT_DOMAIN, 1383*4543Smarks "Invalid permission %s"), value); 1384*4543Smarks return (zfs_error(zhp->zfs_hdl, 1385*4543Smarks EZFS_PERMSET_CIRCULAR, errbuf)); 1386*4543Smarks } 1387*4543Smarks update_nvp = sets_nvp; 1388*4543Smarks perm_name = value; 1389*4543Smarks } else { 1390*4543Smarks nvlist_free(who_nvp); 1391*4543Smarks nvlist_free(perms_nvp); 1392*4543Smarks if (sets_nvp) 1393*4543Smarks nvlist_free(sets_nvp); 1394*4543Smarks return (zfs_error(zhp->zfs_hdl, 1395*4543Smarks EZFS_BADPERM, value)); 1396*4543Smarks } 1397*4543Smarks } 1398*4543Smarks verify(nvlist_add_boolean(update_nvp, perm_name) == 0); 1399*4543Smarks } 1400*4543Smarks 1401*4543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 1402*4543Smarks who_tok = strtok(whostr, ","); 1403*4543Smarks if (who_tok == NULL) { 1404*4543Smarks nvlist_free(who_nvp); 1405*4543Smarks nvlist_free(perms_nvp); 1406*4543Smarks if (sets_nvp) 1407*4543Smarks nvlist_free(sets_nvp); 1408*4543Smarks (void) snprintf(errbuf, sizeof (errbuf), 1409*4543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 1410*4543Smarks whostr); 1411*4543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 1412*4543Smarks } 1413*4543Smarks } 1414*4543Smarks 1415*4543Smarks /* 1416*4543Smarks * Now create the nvlist(s) 1417*4543Smarks */ 1418*4543Smarks do { 1419*4543Smarks uint64_t who_id; 1420*4543Smarks 1421*4543Smarks error = zfs_get_perm_who(who_tok, &who_type, 1422*4543Smarks &who_id); 1423*4543Smarks if (error) { 1424*4543Smarks nvlist_free(who_nvp); 1425*4543Smarks nvlist_free(perms_nvp); 1426*4543Smarks if (sets_nvp) 1427*4543Smarks nvlist_free(sets_nvp); 1428*4543Smarks (void) snprintf(errbuf, sizeof (errbuf), 1429*4543Smarks dgettext(TEXT_DOMAIN, 1430*4543Smarks "Unable to determine uid/gid for " 1431*4543Smarks "%s "), who_tok); 1432*4543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 1433*4543Smarks } 1434*4543Smarks 1435*4543Smarks /* 1436*4543Smarks * add entries for both local and descendent when required 1437*4543Smarks */ 1438*4543Smarks 1439*4543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 1440*4543Smarks perms_nvp, sets_nvp, who_type, inherit); 1441*4543Smarks 1442*4543Smarks } while (who_tok = strtok(NULL, ",")); 1443*4543Smarks *nvp = who_nvp; 1444*4543Smarks return (0); 1445*4543Smarks } 1446*4543Smarks 1447*4543Smarks static int 1448*4543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 1449*4543Smarks { 1450*4543Smarks zfs_cmd_t zc = { 0 }; 1451*4543Smarks int error; 1452*4543Smarks size_t sz; 1453*4543Smarks char errbuf[1024]; 1454*4543Smarks 1455*4543Smarks (void) snprintf(errbuf, sizeof (errbuf), 1456*4543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 1457*4543Smarks zhp->zfs_name); 1458*4543Smarks 1459*4543Smarks if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp, &sz)) 1460*4543Smarks return (-1); 1461*4543Smarks 1462*4543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1463*4543Smarks zc.zc_perm_action = unset; 1464*4543Smarks 1465*4543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 1466*4543Smarks if (error && errno == ENOTSUP) { 1467*4543Smarks (void) snprintf(errbuf, sizeof (errbuf), 1468*4543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 1469*4543Smarks zcmd_free_nvlists(&zc); 1470*4543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 1471*4543Smarks } else if (error) { 1472*4543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 1473*4543Smarks } 1474*4543Smarks zcmd_free_nvlists(&zc); 1475*4543Smarks 1476*4543Smarks return (error); 1477*4543Smarks } 1478*4543Smarks 1479*4543Smarks int 1480*4543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 1481*4543Smarks { 1482*4543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 1483*4543Smarks } 1484*4543Smarks 1485*4543Smarks int 1486*4543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 1487*4543Smarks { 1488*4543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 1489*4543Smarks } 1490*4543Smarks 1491*4543Smarks static int 1492*4543Smarks perm_compare(const void *arg1, const void *arg2) 1493*4543Smarks { 1494*4543Smarks const zfs_perm_node_t *node1 = arg1; 1495*4543Smarks const zfs_perm_node_t *node2 = arg2; 1496*4543Smarks int ret; 1497*4543Smarks 1498*4543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 1499*4543Smarks 1500*4543Smarks if (ret > 0) 1501*4543Smarks return (1); 1502*4543Smarks if (ret < 0) 1503*4543Smarks return (-1); 1504*4543Smarks else 1505*4543Smarks return (0); 1506*4543Smarks } 1507*4543Smarks 1508*4543Smarks static void 1509*4543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 1510*4543Smarks { 1511*4543Smarks zfs_perm_node_t *permnode; 1512*4543Smarks void *cookie; 1513*4543Smarks 1514*4543Smarks cookie = NULL; 1515*4543Smarks while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) { 1516*4543Smarks avl_remove(tree, permnode); 1517*4543Smarks free(permnode); 1518*4543Smarks } 1519*4543Smarks } 1520*4543Smarks 1521*4543Smarks static void 1522*4543Smarks zfs_destroy_tree(avl_tree_t *tree) 1523*4543Smarks { 1524*4543Smarks zfs_allow_node_t *allownode; 1525*4543Smarks void *cookie; 1526*4543Smarks 1527*4543Smarks cookie = NULL; 1528*4543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 1529*4543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 1530*4543Smarks zfs_destroy_perm_tree(&allownode->z_local); 1531*4543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 1532*4543Smarks avl_remove(tree, allownode); 1533*4543Smarks free(allownode); 1534*4543Smarks } 1535*4543Smarks } 1536*4543Smarks 1537*4543Smarks void 1538*4543Smarks zfs_free_allows(zfs_allow_t *allow) 1539*4543Smarks { 1540*4543Smarks zfs_allow_t *allownext; 1541*4543Smarks zfs_allow_t *freeallow; 1542*4543Smarks 1543*4543Smarks allownext = allow; 1544*4543Smarks while (allownext) { 1545*4543Smarks zfs_destroy_tree(&allownext->z_sets); 1546*4543Smarks zfs_destroy_tree(&allownext->z_crperms); 1547*4543Smarks zfs_destroy_tree(&allownext->z_user); 1548*4543Smarks zfs_destroy_tree(&allownext->z_group); 1549*4543Smarks zfs_destroy_tree(&allownext->z_everyone); 1550*4543Smarks freeallow = allownext; 1551*4543Smarks allownext = allownext->z_next; 1552*4543Smarks free(freeallow); 1553*4543Smarks } 1554*4543Smarks } 1555*4543Smarks 1556*4543Smarks static zfs_allow_t * 1557*4543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 1558*4543Smarks { 1559*4543Smarks zfs_allow_t *ptree; 1560*4543Smarks 1561*4543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 1562*4543Smarks sizeof (zfs_allow_t))) == NULL) { 1563*4543Smarks return (NULL); 1564*4543Smarks } 1565*4543Smarks 1566*4543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 1567*4543Smarks avl_create(&ptree->z_sets, 1568*4543Smarks perm_compare, sizeof (zfs_allow_node_t), 1569*4543Smarks offsetof(zfs_allow_node_t, z_node)); 1570*4543Smarks avl_create(&ptree->z_crperms, 1571*4543Smarks perm_compare, sizeof (zfs_allow_node_t), 1572*4543Smarks offsetof(zfs_allow_node_t, z_node)); 1573*4543Smarks avl_create(&ptree->z_user, 1574*4543Smarks perm_compare, sizeof (zfs_allow_node_t), 1575*4543Smarks offsetof(zfs_allow_node_t, z_node)); 1576*4543Smarks avl_create(&ptree->z_group, 1577*4543Smarks perm_compare, sizeof (zfs_allow_node_t), 1578*4543Smarks offsetof(zfs_allow_node_t, z_node)); 1579*4543Smarks avl_create(&ptree->z_everyone, 1580*4543Smarks perm_compare, sizeof (zfs_allow_node_t), 1581*4543Smarks offsetof(zfs_allow_node_t, z_node)); 1582*4543Smarks 1583*4543Smarks if (prev) 1584*4543Smarks prev->z_next = ptree; 1585*4543Smarks ptree->z_next = NULL; 1586*4543Smarks return (ptree); 1587*4543Smarks } 1588*4543Smarks 1589*4543Smarks /* 1590*4543Smarks * Add permissions to the appropriate AVL permission tree. 1591*4543Smarks * The appropriate tree may not be the requested tree. 1592*4543Smarks * For example if ld indicates a local permission, but 1593*4543Smarks * same permission also exists as a descendent permission 1594*4543Smarks * then the permission will be removed from the descendent 1595*4543Smarks * tree and add the the local+descendent tree. 1596*4543Smarks */ 1597*4543Smarks static int 1598*4543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 1599*4543Smarks char *perm, char ld) 1600*4543Smarks { 1601*4543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 1602*4543Smarks zfs_perm_node_t *newnode; 1603*4543Smarks avl_index_t where, where2; 1604*4543Smarks avl_tree_t *tree, *altree; 1605*4543Smarks 1606*4543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 1607*4543Smarks 1608*4543Smarks if (ld == ZFS_DELEG_NA) { 1609*4543Smarks tree = &allownode->z_localdescend; 1610*4543Smarks altree = &allownode->z_descend; 1611*4543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 1612*4543Smarks tree = &allownode->z_local; 1613*4543Smarks altree = &allownode->z_descend; 1614*4543Smarks } else { 1615*4543Smarks tree = &allownode->z_descend; 1616*4543Smarks altree = &allownode->z_local; 1617*4543Smarks } 1618*4543Smarks permnode = avl_find(tree, &pnode, &where); 1619*4543Smarks permnode2 = avl_find(altree, &pnode, &where2); 1620*4543Smarks 1621*4543Smarks if (permnode2) { 1622*4543Smarks avl_remove(altree, permnode2); 1623*4543Smarks free(permnode2); 1624*4543Smarks if (permnode == NULL) { 1625*4543Smarks tree = &allownode->z_localdescend; 1626*4543Smarks } 1627*4543Smarks } 1628*4543Smarks 1629*4543Smarks /* 1630*4543Smarks * Now insert new permission in either requested location 1631*4543Smarks * local/descendent or into ld when perm will exist in both. 1632*4543Smarks */ 1633*4543Smarks if (permnode == NULL) { 1634*4543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 1635*4543Smarks sizeof (zfs_perm_node_t))) == NULL) { 1636*4543Smarks return (-1); 1637*4543Smarks } 1638*4543Smarks *newnode = pnode; 1639*4543Smarks avl_add(tree, newnode); 1640*4543Smarks } 1641*4543Smarks return (0); 1642*4543Smarks } 1643*4543Smarks /* 1644*4543Smarks * Uggh, this is going to be a bit complicated. 1645*4543Smarks * we have an nvlist coming out of the kernel that 1646*4543Smarks * will indicate where the permission is set and then 1647*4543Smarks * it will contain allow of the various "who's", and what 1648*4543Smarks * their permissions are. To further complicate this 1649*4543Smarks * we will then have to coalesce the local,descendent 1650*4543Smarks * and local+descendent permissions where appropriate. 1651*4543Smarks * The kernel only knows about a permission as being local 1652*4543Smarks * or descendent, but not both. 1653*4543Smarks * 1654*4543Smarks * In order to make this easier for zfs_main to deal with 1655*4543Smarks * a series of AVL trees will be used to maintain 1656*4543Smarks * all of this, primarily for sorting purposes as well 1657*4543Smarks * as the ability to quickly locate a specific entry. 1658*4543Smarks * 1659*4543Smarks * What we end up with are tree's for sets, create perms, 1660*4543Smarks * user, groups and everyone. With each of those trees 1661*4543Smarks * we have subtrees for local, descendent and local+descendent 1662*4543Smarks * permissions. 1663*4543Smarks */ 1664*4543Smarks int 1665*4543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 1666*4543Smarks { 1667*4543Smarks zfs_cmd_t zc = { 0 }; 1668*4543Smarks int error; 1669*4543Smarks nvlist_t *nvlist; 1670*4543Smarks nvlist_t *permnv, *sourcenv; 1671*4543Smarks nvpair_t *who_pair, *source_pair; 1672*4543Smarks nvpair_t *perm_pair; 1673*4543Smarks char errbuf[1024]; 1674*4543Smarks zfs_allow_t *zallowp, *newallowp; 1675*4543Smarks char ld; 1676*4543Smarks char *nvpname; 1677*4543Smarks uid_t uid; 1678*4543Smarks gid_t gid; 1679*4543Smarks avl_tree_t *tree; 1680*4543Smarks avl_index_t where; 1681*4543Smarks 1682*4543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1683*4543Smarks 1684*4543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 1685*4543Smarks return (-1); 1686*4543Smarks 1687*4543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 1688*4543Smarks if (errno == ENOMEM) { 1689*4543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 1690*4543Smarks zcmd_free_nvlists(&zc); 1691*4543Smarks return (-1); 1692*4543Smarks } 1693*4543Smarks } else if (errno == ENOTSUP) { 1694*4543Smarks zcmd_free_nvlists(&zc); 1695*4543Smarks (void) snprintf(errbuf, sizeof (errbuf), 1696*4543Smarks gettext("Pool must be upgraded to use 'allow'")); 1697*4543Smarks return (zfs_error(zhp->zfs_hdl, 1698*4543Smarks EZFS_BADVERSION, errbuf)); 1699*4543Smarks } else { 1700*4543Smarks zcmd_free_nvlists(&zc); 1701*4543Smarks return (-1); 1702*4543Smarks } 1703*4543Smarks } 1704*4543Smarks 1705*4543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 1706*4543Smarks zcmd_free_nvlists(&zc); 1707*4543Smarks return (-1); 1708*4543Smarks } 1709*4543Smarks 1710*4543Smarks zcmd_free_nvlists(&zc); 1711*4543Smarks 1712*4543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 1713*4543Smarks 1714*4543Smarks if (source_pair == NULL) { 1715*4543Smarks *zfs_perms = NULL; 1716*4543Smarks return (0); 1717*4543Smarks } 1718*4543Smarks 1719*4543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 1720*4543Smarks if (*zfs_perms == NULL) { 1721*4543Smarks return (0); 1722*4543Smarks } 1723*4543Smarks 1724*4543Smarks zallowp = *zfs_perms; 1725*4543Smarks 1726*4543Smarks for (;;) { 1727*4543Smarks struct passwd *pwd; 1728*4543Smarks struct group *grp; 1729*4543Smarks zfs_allow_node_t *allownode; 1730*4543Smarks zfs_allow_node_t findallownode; 1731*4543Smarks zfs_allow_node_t *newallownode; 1732*4543Smarks 1733*4543Smarks (void) strlcpy(zallowp->z_setpoint, 1734*4543Smarks nvpair_name(source_pair), 1735*4543Smarks sizeof (zallowp->z_setpoint)); 1736*4543Smarks 1737*4543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 1738*4543Smarks goto abort; 1739*4543Smarks 1740*4543Smarks /* 1741*4543Smarks * Make sure nvlist is composed correctly 1742*4543Smarks */ 1743*4543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 1744*4543Smarks goto abort; 1745*4543Smarks } 1746*4543Smarks 1747*4543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 1748*4543Smarks if (who_pair == NULL) { 1749*4543Smarks goto abort; 1750*4543Smarks } 1751*4543Smarks 1752*4543Smarks do { 1753*4543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 1754*4543Smarks if (error) { 1755*4543Smarks goto abort; 1756*4543Smarks } 1757*4543Smarks 1758*4543Smarks /* 1759*4543Smarks * First build up the key to use 1760*4543Smarks * for looking up in the various 1761*4543Smarks * who trees. 1762*4543Smarks */ 1763*4543Smarks ld = nvpair_name(who_pair)[1]; 1764*4543Smarks nvpname = nvpair_name(who_pair); 1765*4543Smarks switch (nvpair_name(who_pair)[0]) { 1766*4543Smarks case ZFS_DELEG_USER: 1767*4543Smarks case ZFS_DELEG_USER_SETS: 1768*4543Smarks tree = &zallowp->z_user; 1769*4543Smarks uid = atol(&nvpname[3]); 1770*4543Smarks pwd = getpwuid(uid); 1771*4543Smarks (void) snprintf(findallownode.z_key, 1772*4543Smarks sizeof (findallownode.z_key), "user %s", 1773*4543Smarks (pwd) ? pwd->pw_name : 1774*4543Smarks &nvpair_name(who_pair)[3]); 1775*4543Smarks break; 1776*4543Smarks case ZFS_DELEG_GROUP: 1777*4543Smarks case ZFS_DELEG_GROUP_SETS: 1778*4543Smarks tree = &zallowp->z_group; 1779*4543Smarks gid = atol(&nvpname[3]); 1780*4543Smarks grp = getgrgid(gid); 1781*4543Smarks (void) snprintf(findallownode.z_key, 1782*4543Smarks sizeof (findallownode.z_key), "group %s", 1783*4543Smarks (grp) ? grp->gr_name : 1784*4543Smarks &nvpair_name(who_pair)[3]); 1785*4543Smarks break; 1786*4543Smarks case ZFS_DELEG_CREATE: 1787*4543Smarks case ZFS_DELEG_CREATE_SETS: 1788*4543Smarks tree = &zallowp->z_crperms; 1789*4543Smarks (void) strlcpy(findallownode.z_key, "", 1790*4543Smarks sizeof (findallownode.z_key)); 1791*4543Smarks break; 1792*4543Smarks case ZFS_DELEG_EVERYONE: 1793*4543Smarks case ZFS_DELEG_EVERYONE_SETS: 1794*4543Smarks (void) snprintf(findallownode.z_key, 1795*4543Smarks sizeof (findallownode.z_key), "everyone"); 1796*4543Smarks tree = &zallowp->z_everyone; 1797*4543Smarks break; 1798*4543Smarks case ZFS_DELEG_NAMED_SET: 1799*4543Smarks case ZFS_DELEG_NAMED_SET_SETS: 1800*4543Smarks (void) snprintf(findallownode.z_key, 1801*4543Smarks sizeof (findallownode.z_key), "%s", 1802*4543Smarks &nvpair_name(who_pair)[3]); 1803*4543Smarks tree = &zallowp->z_sets; 1804*4543Smarks break; 1805*4543Smarks } 1806*4543Smarks 1807*4543Smarks /* 1808*4543Smarks * Place who in tree 1809*4543Smarks */ 1810*4543Smarks allownode = avl_find(tree, &findallownode, &where); 1811*4543Smarks if (allownode == NULL) { 1812*4543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 1813*4543Smarks sizeof (zfs_allow_node_t))) == NULL) { 1814*4543Smarks goto abort; 1815*4543Smarks } 1816*4543Smarks avl_create(&newallownode->z_localdescend, 1817*4543Smarks perm_compare, 1818*4543Smarks sizeof (zfs_perm_node_t), 1819*4543Smarks offsetof(zfs_perm_node_t, z_node)); 1820*4543Smarks avl_create(&newallownode->z_local, 1821*4543Smarks perm_compare, 1822*4543Smarks sizeof (zfs_perm_node_t), 1823*4543Smarks offsetof(zfs_perm_node_t, z_node)); 1824*4543Smarks avl_create(&newallownode->z_descend, 1825*4543Smarks perm_compare, 1826*4543Smarks sizeof (zfs_perm_node_t), 1827*4543Smarks offsetof(zfs_perm_node_t, z_node)); 1828*4543Smarks (void) strlcpy(newallownode->z_key, 1829*4543Smarks findallownode.z_key, 1830*4543Smarks sizeof (findallownode.z_key)); 1831*4543Smarks avl_insert(tree, newallownode, where); 1832*4543Smarks allownode = newallownode; 1833*4543Smarks } 1834*4543Smarks 1835*4543Smarks /* 1836*4543Smarks * Now iterate over the permissions and 1837*4543Smarks * place them in the appropriate local, 1838*4543Smarks * descendent or local+descendent tree. 1839*4543Smarks * 1840*4543Smarks * The permissions are added to the tree 1841*4543Smarks * via zfs_coalesce_perm(). 1842*4543Smarks */ 1843*4543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 1844*4543Smarks if (perm_pair == NULL) 1845*4543Smarks goto abort; 1846*4543Smarks do { 1847*4543Smarks if (zfs_coalesce_perm(zhp, allownode, 1848*4543Smarks nvpair_name(perm_pair), ld) != 0) 1849*4543Smarks goto abort; 1850*4543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 1851*4543Smarks perm_pair)); 1852*4543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 1853*4543Smarks 1854*4543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 1855*4543Smarks if (source_pair == NULL) 1856*4543Smarks break; 1857*4543Smarks 1858*4543Smarks /* 1859*4543Smarks * allocate another node from the link list of 1860*4543Smarks * zfs_allow_t structures 1861*4543Smarks */ 1862*4543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 1863*4543Smarks nvpair_name(source_pair)); 1864*4543Smarks if (newallowp == NULL) { 1865*4543Smarks goto abort; 1866*4543Smarks } 1867*4543Smarks zallowp = newallowp; 1868*4543Smarks } 1869*4543Smarks nvlist_free(nvlist); 1870*4543Smarks return (0); 1871*4543Smarks abort: 1872*4543Smarks zfs_free_allows(*zfs_perms); 1873*4543Smarks nvlist_free(nvlist); 1874*4543Smarks return (-1); 1875*4543Smarks } 1876*4543Smarks 1877789Sahrens /* 1878789Sahrens * Given a property name and value, set the property for the given dataset. 1879789Sahrens */ 1880789Sahrens int 18812676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1882789Sahrens { 1883789Sahrens zfs_cmd_t zc = { 0 }; 18842676Seschrock int ret = -1; 18852676Seschrock prop_changelist_t *cl = NULL; 18862082Seschrock char errbuf[1024]; 18872082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 18882676Seschrock nvlist_t *nvl = NULL, *realprops; 18892676Seschrock zfs_prop_t prop; 18902082Seschrock 18912082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 18922676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 18932082Seschrock zhp->zfs_name); 18942082Seschrock 18952676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 18962676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 18972676Seschrock (void) no_memory(hdl); 18982676Seschrock goto error; 1899789Sahrens } 1900789Sahrens 19013912Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, NULL, nvl, 19022676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 19032676Seschrock goto error; 19042676Seschrock nvlist_free(nvl); 19052676Seschrock nvl = realprops; 19062676Seschrock 19072676Seschrock prop = zfs_name_to_prop(propname); 19082676Seschrock 1909789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 19102676Seschrock goto error; 1911789Sahrens 1912789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 19132082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19142082Seschrock "child dataset with inherited mountpoint is used " 19152082Seschrock "in a non-global zone")); 19162082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1917789Sahrens goto error; 1918789Sahrens } 1919789Sahrens 1920789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1921789Sahrens goto error; 1922789Sahrens 1923789Sahrens /* 1924789Sahrens * Execute the corresponding ioctl() to set this property. 1925789Sahrens */ 1926789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1927789Sahrens 19282676Seschrock if (zcmd_write_src_nvlist(hdl, &zc, nvl, NULL) != 0) 19292676Seschrock goto error; 19302676Seschrock 1931*4543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1932789Sahrens 1933789Sahrens if (ret != 0) { 1934789Sahrens switch (errno) { 1935789Sahrens 1936789Sahrens case ENOSPC: 1937789Sahrens /* 1938789Sahrens * For quotas and reservations, ENOSPC indicates 1939789Sahrens * something different; setting a quota or reservation 1940789Sahrens * doesn't use any disk space. 1941789Sahrens */ 1942789Sahrens switch (prop) { 1943789Sahrens case ZFS_PROP_QUOTA: 19442082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19452082Seschrock "size is less than current used or " 19462082Seschrock "reserved space")); 19472082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1948789Sahrens break; 1949789Sahrens 1950789Sahrens case ZFS_PROP_RESERVATION: 19512082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19522082Seschrock "size is greater than available space")); 19532082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1954789Sahrens break; 1955789Sahrens 1956789Sahrens default: 19572082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1958789Sahrens break; 1959789Sahrens } 1960789Sahrens break; 1961789Sahrens 1962789Sahrens case EBUSY: 19632082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 19642082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 19652082Seschrock else 19662676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1967789Sahrens break; 1968789Sahrens 19691175Slling case EROFS: 19702082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 19711175Slling break; 19721175Slling 19733886Sahl case ENOTSUP: 19743886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19753886Sahl "pool must be upgraded to allow gzip compression")); 19763886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 19773886Sahl break; 19783886Sahl 1979789Sahrens case EOVERFLOW: 1980789Sahrens /* 1981789Sahrens * This platform can't address a volume this big. 1982789Sahrens */ 1983789Sahrens #ifdef _ILP32 1984789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 19852082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1986789Sahrens break; 1987789Sahrens } 1988789Sahrens #endif 19892082Seschrock /* FALLTHROUGH */ 1990789Sahrens default: 19912082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1992789Sahrens } 1993789Sahrens } else { 1994789Sahrens /* 1995789Sahrens * Refresh the statistics so the new property value 1996789Sahrens * is reflected. 1997789Sahrens */ 19982676Seschrock if ((ret = changelist_postfix(cl)) == 0) 19992676Seschrock (void) get_stats(zhp); 2000789Sahrens } 2001789Sahrens 2002789Sahrens error: 20032676Seschrock nvlist_free(nvl); 20042676Seschrock zcmd_free_nvlists(&zc); 20052676Seschrock if (cl) 20062676Seschrock changelist_free(cl); 2007789Sahrens return (ret); 2008789Sahrens } 2009789Sahrens 2010789Sahrens /* 2011789Sahrens * Given a property, inherit the value from the parent dataset. 2012789Sahrens */ 2013789Sahrens int 20142676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 2015789Sahrens { 2016789Sahrens zfs_cmd_t zc = { 0 }; 2017789Sahrens int ret; 2018789Sahrens prop_changelist_t *cl; 20192082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 20202082Seschrock char errbuf[1024]; 20212676Seschrock zfs_prop_t prop; 20222082Seschrock 20232082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 20242082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 2025789Sahrens 20262676Seschrock if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) { 20272676Seschrock /* 20282676Seschrock * For user properties, the amount of work we have to do is very 20292676Seschrock * small, so just do it here. 20302676Seschrock */ 20312676Seschrock if (!zfs_prop_user(propname)) { 20322676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20332676Seschrock "invalid property")); 20342676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 20352676Seschrock } 20362676Seschrock 20372676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 20382676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 20392676Seschrock 2040*4543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc) != 0) 20412676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 20422676Seschrock 20432676Seschrock return (0); 20442676Seschrock } 20452676Seschrock 2046789Sahrens /* 2047789Sahrens * Verify that this property is inheritable. 2048789Sahrens */ 20492082Seschrock if (zfs_prop_readonly(prop)) 20502082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 20512082Seschrock 20522082Seschrock if (!zfs_prop_inheritable(prop)) 20532082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2054789Sahrens 2055789Sahrens /* 2056789Sahrens * Check to see if the value applies to this type 2057789Sahrens */ 20582082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 20592082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2060789Sahrens 20613443Srm160521 /* 20623443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 20633443Srm160521 */ 20643443Srm160521 propname = zfs_prop_to_name(prop); 2065789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 20662676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2067789Sahrens 2068789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2069789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 20702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20712082Seschrock "dataset is used in a non-global zone")); 20722082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2073789Sahrens } 2074789Sahrens 2075789Sahrens /* 2076789Sahrens * Determine datasets which will be affected by this change, if any. 2077789Sahrens */ 2078789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 2079789Sahrens return (-1); 2080789Sahrens 2081789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 20822082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20832082Seschrock "child dataset with inherited mountpoint is used " 20842082Seschrock "in a non-global zone")); 20852082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2086789Sahrens goto error; 2087789Sahrens } 2088789Sahrens 2089789Sahrens if ((ret = changelist_prefix(cl)) != 0) 2090789Sahrens goto error; 2091789Sahrens 2092*4543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_PROP, &zc)) != 0) { 20932082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2094789Sahrens } else { 2095789Sahrens 20962169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 2097789Sahrens goto error; 2098789Sahrens 2099789Sahrens /* 2100789Sahrens * Refresh the statistics so the new property is reflected. 2101789Sahrens */ 2102789Sahrens (void) get_stats(zhp); 2103789Sahrens } 2104789Sahrens 2105789Sahrens error: 2106789Sahrens changelist_free(cl); 2107789Sahrens return (ret); 2108789Sahrens } 2109789Sahrens 21103912Slling void 2111789Sahrens nicebool(int value, char *buf, size_t buflen) 2112789Sahrens { 2113789Sahrens if (value) 2114789Sahrens (void) strlcpy(buf, "on", buflen); 2115789Sahrens else 2116789Sahrens (void) strlcpy(buf, "off", buflen); 2117789Sahrens } 2118789Sahrens 2119789Sahrens /* 21201356Seschrock * True DSL properties are stored in an nvlist. The following two functions 21211356Seschrock * extract them appropriately. 21221356Seschrock */ 21231356Seschrock static uint64_t 21241356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 21251356Seschrock { 21261356Seschrock nvlist_t *nv; 21271356Seschrock uint64_t value; 21281356Seschrock 21292885Sahrens *source = NULL; 21301356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 21311356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 21321356Seschrock verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0); 21332885Sahrens (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 21341356Seschrock } else { 21351356Seschrock value = zfs_prop_default_numeric(prop); 21361356Seschrock *source = ""; 21371356Seschrock } 21381356Seschrock 21391356Seschrock return (value); 21401356Seschrock } 21411356Seschrock 21421356Seschrock static char * 21431356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 21441356Seschrock { 21451356Seschrock nvlist_t *nv; 21461356Seschrock char *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_string(nv, ZFS_PROP_VALUE, &value) == 0); 21522885Sahrens (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 21531356Seschrock } else { 21541356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 21551356Seschrock value = ""; 21561356Seschrock *source = ""; 21571356Seschrock } 21581356Seschrock 21591356Seschrock return (value); 21601356Seschrock } 21611356Seschrock 21621356Seschrock /* 2163789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2164789Sahrens * zfs_prop_get_int() are built using this interface. 2165789Sahrens * 2166789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2167789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2168789Sahrens * If they differ from the on-disk values, report the current values and mark 2169789Sahrens * the source "temporary". 2170789Sahrens */ 21712082Seschrock static int 2172789Sahrens get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src, 21732082Seschrock char **source, uint64_t *val) 2174789Sahrens { 2175789Sahrens struct mnttab mnt; 21763265Sahrens char *mntopt_on = NULL; 21773265Sahrens char *mntopt_off = NULL; 2178789Sahrens 2179789Sahrens *source = NULL; 2180789Sahrens 21813265Sahrens switch (prop) { 21823265Sahrens case ZFS_PROP_ATIME: 21833265Sahrens mntopt_on = MNTOPT_ATIME; 21843265Sahrens mntopt_off = MNTOPT_NOATIME; 21853265Sahrens break; 21863265Sahrens 21873265Sahrens case ZFS_PROP_DEVICES: 21883265Sahrens mntopt_on = MNTOPT_DEVICES; 21893265Sahrens mntopt_off = MNTOPT_NODEVICES; 21903265Sahrens break; 21913265Sahrens 21923265Sahrens case ZFS_PROP_EXEC: 21933265Sahrens mntopt_on = MNTOPT_EXEC; 21943265Sahrens mntopt_off = MNTOPT_NOEXEC; 21953265Sahrens break; 21963265Sahrens 21973265Sahrens case ZFS_PROP_READONLY: 21983265Sahrens mntopt_on = MNTOPT_RO; 21993265Sahrens mntopt_off = MNTOPT_RW; 22003265Sahrens break; 22013265Sahrens 22023265Sahrens case ZFS_PROP_SETUID: 22033265Sahrens mntopt_on = MNTOPT_SETUID; 22043265Sahrens mntopt_off = MNTOPT_NOSETUID; 22053265Sahrens break; 22063265Sahrens 22073265Sahrens case ZFS_PROP_XATTR: 22083265Sahrens mntopt_on = MNTOPT_XATTR; 22093265Sahrens mntopt_off = MNTOPT_NOXATTR; 22103265Sahrens break; 22113265Sahrens } 22123265Sahrens 22132474Seschrock /* 22142474Seschrock * Because looking up the mount options is potentially expensive 22152474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 22162474Seschrock * we're looking up a property which requires its presence. 22172474Seschrock */ 22182474Seschrock if (!zhp->zfs_mntcheck && 22193265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 22203265Sahrens struct mnttab entry, search = { 0 }; 22213265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 22222474Seschrock 22232474Seschrock search.mnt_special = (char *)zhp->zfs_name; 22242474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 22253265Sahrens rewind(mnttab); 22263265Sahrens 22273265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 22283265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 22293265Sahrens entry.mnt_mntopts); 22303265Sahrens if (zhp->zfs_mntopts == NULL) 22313265Sahrens return (-1); 22323265Sahrens } 22332474Seschrock 22342474Seschrock zhp->zfs_mntcheck = B_TRUE; 22352474Seschrock } 22362474Seschrock 2237789Sahrens if (zhp->zfs_mntopts == NULL) 2238789Sahrens mnt.mnt_mntopts = ""; 2239789Sahrens else 2240789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2241789Sahrens 2242789Sahrens switch (prop) { 2243789Sahrens case ZFS_PROP_ATIME: 22443265Sahrens case ZFS_PROP_DEVICES: 22453265Sahrens case ZFS_PROP_EXEC: 22463265Sahrens case ZFS_PROP_READONLY: 22473265Sahrens case ZFS_PROP_SETUID: 22483265Sahrens case ZFS_PROP_XATTR: 22492082Seschrock *val = getprop_uint64(zhp, prop, source); 22502082Seschrock 22513265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 22522082Seschrock *val = B_TRUE; 2253789Sahrens if (src) 2254789Sahrens *src = ZFS_SRC_TEMPORARY; 22553265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 22562082Seschrock *val = B_FALSE; 2257789Sahrens if (src) 2258789Sahrens *src = ZFS_SRC_TEMPORARY; 2259789Sahrens } 22602082Seschrock break; 2261789Sahrens 2262789Sahrens case ZFS_PROP_RECORDSIZE: 2263789Sahrens case ZFS_PROP_COMPRESSION: 22641356Seschrock case ZFS_PROP_ZONED: 22652885Sahrens case ZFS_PROP_CREATION: 22662885Sahrens case ZFS_PROP_COMPRESSRATIO: 22672885Sahrens case ZFS_PROP_REFERENCED: 22682885Sahrens case ZFS_PROP_USED: 22692885Sahrens case ZFS_PROP_CREATETXG: 22702885Sahrens case ZFS_PROP_AVAILABLE: 22712885Sahrens case ZFS_PROP_VOLSIZE: 22722885Sahrens case ZFS_PROP_VOLBLOCKSIZE: 22733417Srm160521 *val = getprop_uint64(zhp, prop, source); 22743417Srm160521 break; 22753417Srm160521 22763265Sahrens case ZFS_PROP_CANMOUNT: 22772082Seschrock *val = getprop_uint64(zhp, prop, source); 22783417Srm160521 if (*val == 0) 22793417Srm160521 *source = zhp->zfs_name; 22803417Srm160521 else 22813417Srm160521 *source = ""; /* default */ 22822082Seschrock break; 2283789Sahrens 2284789Sahrens case ZFS_PROP_QUOTA: 2285789Sahrens case ZFS_PROP_RESERVATION: 22862885Sahrens *val = getprop_uint64(zhp, prop, source); 22872885Sahrens if (*val == 0) 2288789Sahrens *source = ""; /* default */ 2289789Sahrens else 2290789Sahrens *source = zhp->zfs_name; 22912082Seschrock break; 2292789Sahrens 2293789Sahrens case ZFS_PROP_MOUNTED: 22942082Seschrock *val = (zhp->zfs_mntopts != NULL); 22952082Seschrock break; 2296789Sahrens 22973377Seschrock case ZFS_PROP_NUMCLONES: 22983377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 22993377Seschrock break; 23003377Seschrock 2301789Sahrens default: 23022082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 23032082Seschrock "cannot get non-numeric property")); 23042082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 23052082Seschrock dgettext(TEXT_DOMAIN, "internal error"))); 2306789Sahrens } 2307789Sahrens 2308789Sahrens return (0); 2309789Sahrens } 2310789Sahrens 2311789Sahrens /* 2312789Sahrens * Calculate the source type, given the raw source string. 2313789Sahrens */ 2314789Sahrens static void 2315789Sahrens get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source, 2316789Sahrens char *statbuf, size_t statlen) 2317789Sahrens { 2318789Sahrens if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY) 2319789Sahrens return; 2320789Sahrens 2321789Sahrens if (source == NULL) { 2322789Sahrens *srctype = ZFS_SRC_NONE; 2323789Sahrens } else if (source[0] == '\0') { 2324789Sahrens *srctype = ZFS_SRC_DEFAULT; 2325789Sahrens } else { 2326789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 2327789Sahrens *srctype = ZFS_SRC_LOCAL; 2328789Sahrens } else { 2329789Sahrens (void) strlcpy(statbuf, source, statlen); 2330789Sahrens *srctype = ZFS_SRC_INHERITED; 2331789Sahrens } 2332789Sahrens } 2333789Sahrens 2334789Sahrens } 2335789Sahrens 2336789Sahrens /* 2337789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2338789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2339789Sahrens * human-readable form. 2340789Sahrens * 2341789Sahrens * Returns 0 on success, or -1 on error. 2342789Sahrens */ 2343789Sahrens int 2344789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 23452082Seschrock zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2346789Sahrens { 2347789Sahrens char *source = NULL; 2348789Sahrens uint64_t val; 2349789Sahrens char *str; 2350789Sahrens const char *root; 23512676Seschrock const char *strval; 2352789Sahrens 2353789Sahrens /* 2354789Sahrens * Check to see if this property applies to our object 2355789Sahrens */ 2356789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2357789Sahrens return (-1); 2358789Sahrens 2359789Sahrens if (src) 2360789Sahrens *src = ZFS_SRC_NONE; 2361789Sahrens 2362789Sahrens switch (prop) { 2363789Sahrens case ZFS_PROP_ATIME: 2364789Sahrens case ZFS_PROP_READONLY: 2365789Sahrens case ZFS_PROP_SETUID: 2366789Sahrens case ZFS_PROP_ZONED: 2367789Sahrens case ZFS_PROP_DEVICES: 2368789Sahrens case ZFS_PROP_EXEC: 23692676Seschrock case ZFS_PROP_CANMOUNT: 23703234Sck153898 case ZFS_PROP_XATTR: 2371789Sahrens /* 2372789Sahrens * Basic boolean values are built on top of 2373789Sahrens * get_numeric_property(). 2374789Sahrens */ 23752082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 23762082Seschrock return (-1); 23772082Seschrock nicebool(val, propbuf, proplen); 2378789Sahrens 2379789Sahrens break; 2380789Sahrens 2381789Sahrens case ZFS_PROP_AVAILABLE: 2382789Sahrens case ZFS_PROP_RECORDSIZE: 2383789Sahrens case ZFS_PROP_CREATETXG: 2384789Sahrens case ZFS_PROP_REFERENCED: 2385789Sahrens case ZFS_PROP_USED: 2386789Sahrens case ZFS_PROP_VOLSIZE: 2387789Sahrens case ZFS_PROP_VOLBLOCKSIZE: 23883377Seschrock case ZFS_PROP_NUMCLONES: 2389789Sahrens /* 2390789Sahrens * Basic numeric values are built on top of 2391789Sahrens * get_numeric_property(). 2392789Sahrens */ 23932082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 23942082Seschrock return (-1); 2395789Sahrens if (literal) 23962856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 23973912Slling (u_longlong_t)val); 2398789Sahrens else 2399789Sahrens zfs_nicenum(val, propbuf, proplen); 2400789Sahrens break; 2401789Sahrens 2402789Sahrens case ZFS_PROP_COMPRESSION: 2403789Sahrens case ZFS_PROP_CHECKSUM: 2404789Sahrens case ZFS_PROP_SNAPDIR: 2405789Sahrens case ZFS_PROP_ACLMODE: 2406789Sahrens case ZFS_PROP_ACLINHERIT: 24073835Sahrens case ZFS_PROP_COPIES: 24081356Seschrock val = getprop_uint64(zhp, prop, &source); 24092676Seschrock verify(zfs_prop_index_to_string(prop, val, &strval) == 0); 24102676Seschrock (void) strlcpy(propbuf, strval, proplen); 2411789Sahrens break; 2412789Sahrens 2413789Sahrens case ZFS_PROP_CREATION: 2414789Sahrens /* 2415789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2416789Sahrens * this into a string unless 'literal' is specified. 2417789Sahrens */ 2418789Sahrens { 24192885Sahrens val = getprop_uint64(zhp, prop, &source); 24202885Sahrens time_t time = (time_t)val; 2421789Sahrens struct tm t; 2422789Sahrens 2423789Sahrens if (literal || 2424789Sahrens localtime_r(&time, &t) == NULL || 2425789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2426789Sahrens &t) == 0) 24272885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2428789Sahrens } 2429789Sahrens break; 2430789Sahrens 2431789Sahrens case ZFS_PROP_MOUNTPOINT: 2432789Sahrens /* 2433789Sahrens * Getting the precise mountpoint can be tricky. 2434789Sahrens * 2435789Sahrens * - for 'none' or 'legacy', return those values. 2436789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2437789Sahrens * - for inherited mountpoints, we want to take everything 2438789Sahrens * after our ancestor and append it to the inherited value. 2439789Sahrens * 2440789Sahrens * If the pool has an alternate root, we want to prepend that 2441789Sahrens * root to any values we return. 2442789Sahrens */ 24431544Seschrock root = zhp->zfs_root; 24441356Seschrock str = getprop_string(zhp, prop, &source); 24451356Seschrock 24461356Seschrock if (str[0] == '\0') { 2447789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2448789Sahrens root, zhp->zfs_name); 24491356Seschrock } else if (str[0] == '/') { 24501356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2451789Sahrens 2452789Sahrens if (relpath[0] == '/') 2453789Sahrens relpath++; 24541356Seschrock if (str[1] == '\0') 24551356Seschrock str++; 2456789Sahrens 2457789Sahrens if (relpath[0] == '\0') 2458789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 24591356Seschrock root, str); 2460789Sahrens else 2461789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 24621356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2463789Sahrens relpath); 2464789Sahrens } else { 2465789Sahrens /* 'legacy' or 'none' */ 24661356Seschrock (void) strlcpy(propbuf, str, proplen); 2467789Sahrens } 2468789Sahrens 2469789Sahrens break; 2470789Sahrens 2471789Sahrens case ZFS_PROP_SHARENFS: 24723126Sahl case ZFS_PROP_SHAREISCSI: 24733126Sahl case ZFS_PROP_ISCSIOPTIONS: 24741356Seschrock (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 24751356Seschrock proplen); 2476789Sahrens break; 2477789Sahrens 2478789Sahrens case ZFS_PROP_ORIGIN: 24792885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2480789Sahrens proplen); 2481789Sahrens /* 2482789Sahrens * If there is no parent at all, return failure to indicate that 2483789Sahrens * it doesn't apply to this dataset. 2484789Sahrens */ 2485789Sahrens if (propbuf[0] == '\0') 2486789Sahrens return (-1); 2487789Sahrens break; 2488789Sahrens 2489789Sahrens case ZFS_PROP_QUOTA: 2490789Sahrens case ZFS_PROP_RESERVATION: 24912082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 24922082Seschrock return (-1); 2493789Sahrens 2494789Sahrens /* 2495789Sahrens * If quota or reservation is 0, we translate this into 'none' 2496789Sahrens * (unless literal is set), and indicate that it's the default 2497789Sahrens * value. Otherwise, we print the number nicely and indicate 2498789Sahrens * that its set locally. 2499789Sahrens */ 2500789Sahrens if (val == 0) { 2501789Sahrens if (literal) 2502789Sahrens (void) strlcpy(propbuf, "0", proplen); 2503789Sahrens else 2504789Sahrens (void) strlcpy(propbuf, "none", proplen); 2505789Sahrens } else { 2506789Sahrens if (literal) 25072856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 25083912Slling (u_longlong_t)val); 2509789Sahrens else 2510789Sahrens zfs_nicenum(val, propbuf, proplen); 2511789Sahrens } 2512789Sahrens break; 2513789Sahrens 2514789Sahrens case ZFS_PROP_COMPRESSRATIO: 25152082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 25162082Seschrock return (-1); 25172856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 25182856Snd150628 val / 100, (longlong_t)val % 100); 2519789Sahrens break; 2520789Sahrens 2521789Sahrens case ZFS_PROP_TYPE: 2522789Sahrens switch (zhp->zfs_type) { 2523789Sahrens case ZFS_TYPE_FILESYSTEM: 2524789Sahrens str = "filesystem"; 2525789Sahrens break; 2526789Sahrens case ZFS_TYPE_VOLUME: 2527789Sahrens str = "volume"; 2528789Sahrens break; 2529789Sahrens case ZFS_TYPE_SNAPSHOT: 2530789Sahrens str = "snapshot"; 2531789Sahrens break; 2532789Sahrens default: 25332082Seschrock abort(); 2534789Sahrens } 2535789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2536789Sahrens break; 2537789Sahrens 2538789Sahrens case ZFS_PROP_MOUNTED: 2539789Sahrens /* 2540789Sahrens * The 'mounted' property is a pseudo-property that described 2541789Sahrens * whether the filesystem is currently mounted. Even though 2542789Sahrens * it's a boolean value, the typical values of "on" and "off" 2543789Sahrens * don't make sense, so we translate to "yes" and "no". 2544789Sahrens */ 25452082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 25462082Seschrock src, &source, &val) != 0) 25472082Seschrock return (-1); 25482082Seschrock if (val) 2549789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2550789Sahrens else 2551789Sahrens (void) strlcpy(propbuf, "no", proplen); 2552789Sahrens break; 2553789Sahrens 2554789Sahrens case ZFS_PROP_NAME: 2555789Sahrens /* 2556789Sahrens * The 'name' property is a pseudo-property derived from the 2557789Sahrens * dataset name. It is presented as a real property to simplify 2558789Sahrens * consumers. 2559789Sahrens */ 2560789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2561789Sahrens break; 2562789Sahrens 2563789Sahrens default: 25642082Seschrock abort(); 2565789Sahrens } 2566789Sahrens 2567789Sahrens get_source(zhp, src, source, statbuf, statlen); 2568789Sahrens 2569789Sahrens return (0); 2570789Sahrens } 2571789Sahrens 2572789Sahrens /* 2573789Sahrens * Utility function to get the given numeric property. Does no validation that 2574789Sahrens * the given property is the appropriate type; should only be used with 2575789Sahrens * hard-coded property types. 2576789Sahrens */ 2577789Sahrens uint64_t 2578789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2579789Sahrens { 2580789Sahrens char *source; 2581789Sahrens zfs_source_t sourcetype = ZFS_SRC_NONE; 25822082Seschrock uint64_t val; 25832082Seschrock 25842082Seschrock (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 25852082Seschrock 25862082Seschrock return (val); 2587789Sahrens } 2588789Sahrens 2589789Sahrens /* 2590789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2591789Sahrens */ 2592789Sahrens int 2593789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2594789Sahrens zfs_source_t *src, char *statbuf, size_t statlen) 2595789Sahrens { 2596789Sahrens char *source; 2597789Sahrens 2598789Sahrens /* 2599789Sahrens * Check to see if this property applies to our object 2600789Sahrens */ 2601789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 26023237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 26032082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 26042082Seschrock zfs_prop_to_name(prop))); 2605789Sahrens 2606789Sahrens if (src) 2607789Sahrens *src = ZFS_SRC_NONE; 2608789Sahrens 26092082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 26102082Seschrock return (-1); 2611789Sahrens 2612789Sahrens get_source(zhp, src, source, statbuf, statlen); 2613789Sahrens 2614789Sahrens return (0); 2615789Sahrens } 2616789Sahrens 2617789Sahrens /* 2618789Sahrens * Returns the name of the given zfs handle. 2619789Sahrens */ 2620789Sahrens const char * 2621789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2622789Sahrens { 2623789Sahrens return (zhp->zfs_name); 2624789Sahrens } 2625789Sahrens 2626789Sahrens /* 2627789Sahrens * Returns the type of the given zfs handle. 2628789Sahrens */ 2629789Sahrens zfs_type_t 2630789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2631789Sahrens { 2632789Sahrens return (zhp->zfs_type); 2633789Sahrens } 2634789Sahrens 2635789Sahrens /* 26361356Seschrock * Iterate over all child filesystems 2637789Sahrens */ 2638789Sahrens int 26391356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2640789Sahrens { 2641789Sahrens zfs_cmd_t zc = { 0 }; 2642789Sahrens zfs_handle_t *nzhp; 2643789Sahrens int ret; 2644789Sahrens 2645789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 26462082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2647789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2648789Sahrens /* 2649789Sahrens * Ignore private dataset names. 2650789Sahrens */ 2651789Sahrens if (dataset_name_hidden(zc.zc_name)) 2652789Sahrens continue; 2653789Sahrens 2654789Sahrens /* 2655789Sahrens * Silently ignore errors, as the only plausible explanation is 2656789Sahrens * that the pool has since been removed. 2657789Sahrens */ 26582082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 26592082Seschrock zc.zc_name)) == NULL) 2660789Sahrens continue; 2661789Sahrens 2662789Sahrens if ((ret = func(nzhp, data)) != 0) 2663789Sahrens return (ret); 2664789Sahrens } 2665789Sahrens 2666789Sahrens /* 2667789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2668789Sahrens * returned, then the underlying dataset has been removed since we 2669789Sahrens * obtained the handle. 2670789Sahrens */ 2671789Sahrens if (errno != ESRCH && errno != ENOENT) 26722082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 26732082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2674789Sahrens 26751356Seschrock return (0); 26761356Seschrock } 26771356Seschrock 26781356Seschrock /* 26791356Seschrock * Iterate over all snapshots 26801356Seschrock */ 26811356Seschrock int 26821356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 26831356Seschrock { 26841356Seschrock zfs_cmd_t zc = { 0 }; 26851356Seschrock zfs_handle_t *nzhp; 26861356Seschrock int ret; 2687789Sahrens 2688789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 26892082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 26902082Seschrock &zc) == 0; 2691789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2692789Sahrens 26932082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 26942082Seschrock zc.zc_name)) == NULL) 2695789Sahrens continue; 2696789Sahrens 2697789Sahrens if ((ret = func(nzhp, data)) != 0) 2698789Sahrens return (ret); 2699789Sahrens } 2700789Sahrens 2701789Sahrens /* 2702789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2703789Sahrens * returned, then the underlying dataset has been removed since we 2704789Sahrens * obtained the handle. Silently ignore this case, and return success. 2705789Sahrens */ 2706789Sahrens if (errno != ESRCH && errno != ENOENT) 27072082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 27082082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2709789Sahrens 2710789Sahrens return (0); 2711789Sahrens } 2712789Sahrens 2713789Sahrens /* 27141356Seschrock * Iterate over all children, snapshots and filesystems 27151356Seschrock */ 27161356Seschrock int 27171356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 27181356Seschrock { 27191356Seschrock int ret; 27201356Seschrock 27211356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 27221356Seschrock return (ret); 27231356Seschrock 27241356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 27251356Seschrock } 27261356Seschrock 27271356Seschrock /* 2728789Sahrens * Given a complete name, return just the portion that refers to the parent. 2729789Sahrens * Can return NULL if this is a pool. 2730789Sahrens */ 2731789Sahrens static int 2732789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2733789Sahrens { 2734789Sahrens char *loc; 2735789Sahrens 2736789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2737789Sahrens return (-1); 2738789Sahrens 2739789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2740789Sahrens buf[loc - path] = '\0'; 2741789Sahrens 2742789Sahrens return (0); 2743789Sahrens } 2744789Sahrens 2745789Sahrens /* 27464490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 27474490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 27484490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 27494490Svb160487 * length of already existing prefix of the given path. We also fetch the 27504490Svb160487 * 'zoned' property, which is used to validate property settings when creating 27514490Svb160487 * new datasets. 2752789Sahrens */ 2753789Sahrens static int 27544490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 27554490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2756789Sahrens { 2757789Sahrens zfs_cmd_t zc = { 0 }; 2758789Sahrens char parent[ZFS_MAXNAMELEN]; 2759789Sahrens char *slash; 27601356Seschrock zfs_handle_t *zhp; 27612082Seschrock char errbuf[1024]; 27622082Seschrock 27632082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 27642082Seschrock path); 2765789Sahrens 2766789Sahrens /* get parent, and check to see if this is just a pool */ 2767789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 27682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27692082Seschrock "missing dataset name")); 27702082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2771789Sahrens } 2772789Sahrens 2773789Sahrens /* check to see if the pool exists */ 2774789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2775789Sahrens slash = parent + strlen(parent); 2776789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2777789Sahrens zc.zc_name[slash - parent] = '\0'; 27782082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2779789Sahrens errno == ENOENT) { 27802082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27812082Seschrock "no such pool '%s'"), zc.zc_name); 27822082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2783789Sahrens } 2784789Sahrens 2785789Sahrens /* check to see if the parent dataset exists */ 27864490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 27874490Svb160487 if (errno == ENOENT && accept_ancestor) { 27884490Svb160487 /* 27894490Svb160487 * Go deeper to find an ancestor, give up on top level. 27904490Svb160487 */ 27914490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 27924490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27934490Svb160487 "no such pool '%s'"), zc.zc_name); 27944490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 27954490Svb160487 } 27964490Svb160487 } else if (errno == ENOENT) { 27972082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27982082Seschrock "parent does not exist")); 27992082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 28004490Svb160487 } else 28012082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2802789Sahrens } 2803789Sahrens 28042676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2805789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 28062676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 28072082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 28081356Seschrock zfs_close(zhp); 2809789Sahrens return (-1); 2810789Sahrens } 2811789Sahrens 2812789Sahrens /* make sure parent is a filesystem */ 28131356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 28142082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28152082Seschrock "parent is not a filesystem")); 28162082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 28171356Seschrock zfs_close(zhp); 2818789Sahrens return (-1); 2819789Sahrens } 2820789Sahrens 28211356Seschrock zfs_close(zhp); 28224490Svb160487 if (prefixlen != NULL) 28234490Svb160487 *prefixlen = strlen(parent); 28244490Svb160487 return (0); 28254490Svb160487 } 28264490Svb160487 28274490Svb160487 /* 28284490Svb160487 * Finds whether the dataset of the given type(s) exists. 28294490Svb160487 */ 28304490Svb160487 boolean_t 28314490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 28324490Svb160487 { 28334490Svb160487 zfs_handle_t *zhp; 28344490Svb160487 28354490Svb160487 if (!zfs_validate_name(hdl, path, types)) 28364490Svb160487 return (B_FALSE); 28374490Svb160487 28384490Svb160487 /* 28394490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 28404490Svb160487 */ 28414490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 28424490Svb160487 int ds_type = zhp->zfs_type; 28434490Svb160487 28444490Svb160487 zfs_close(zhp); 28454490Svb160487 if (types & ds_type) 28464490Svb160487 return (B_TRUE); 28474490Svb160487 } 28484490Svb160487 return (B_FALSE); 28494490Svb160487 } 28504490Svb160487 28514490Svb160487 /* 28524490Svb160487 * Creates non-existing ancestors of the given path. 28534490Svb160487 */ 28544490Svb160487 int 28554490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 28564490Svb160487 { 28574490Svb160487 int prefix; 28584490Svb160487 uint64_t zoned; 28594490Svb160487 char *path_copy; 28604490Svb160487 int rc; 28614490Svb160487 28624490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 28634490Svb160487 return (-1); 28644490Svb160487 28654490Svb160487 if ((path_copy = strdup(path)) != NULL) { 28664490Svb160487 rc = create_parents(hdl, path_copy, prefix); 28674490Svb160487 free(path_copy); 28684490Svb160487 } 28694490Svb160487 if (path_copy == NULL || rc != 0) 28704490Svb160487 return (-1); 28714490Svb160487 2872789Sahrens return (0); 2873789Sahrens } 2874789Sahrens 2875789Sahrens /* 28762676Seschrock * Create a new filesystem or volume. 2877789Sahrens */ 2878789Sahrens int 28792082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 28802676Seschrock nvlist_t *props) 2881789Sahrens { 2882789Sahrens zfs_cmd_t zc = { 0 }; 2883789Sahrens int ret; 2884789Sahrens uint64_t size = 0; 2885789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 28862082Seschrock char errbuf[1024]; 28872676Seschrock uint64_t zoned; 28882082Seschrock 28892082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28902082Seschrock "cannot create '%s'"), path); 2891789Sahrens 2892789Sahrens /* validate the path, taking care to note the extended error message */ 28932082Seschrock if (!zfs_validate_name(hdl, path, type)) 28942082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2895789Sahrens 2896789Sahrens /* validate parents exist */ 28974490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2898789Sahrens return (-1); 2899789Sahrens 2900789Sahrens /* 2901789Sahrens * The failure modes when creating a dataset of a different type over 2902789Sahrens * one that already exists is a little strange. In particular, if you 2903789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2904789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2905789Sahrens * first try to see if the dataset exists. 2906789Sahrens */ 2907789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 29084490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 29092082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29102082Seschrock "dataset already exists")); 29112082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2912789Sahrens } 2913789Sahrens 2914789Sahrens if (type == ZFS_TYPE_VOLUME) 2915789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2916789Sahrens else 2917789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2918789Sahrens 29193912Slling if (props && (props = zfs_validate_properties(hdl, type, NULL, props, 29203912Slling zoned, NULL, errbuf)) == 0) 29212676Seschrock return (-1); 29222676Seschrock 2923789Sahrens if (type == ZFS_TYPE_VOLUME) { 29241133Seschrock /* 29251133Seschrock * If we are creating a volume, the size and block size must 29261133Seschrock * satisfy a few restraints. First, the blocksize must be a 29271133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 29281133Seschrock * volsize must be a multiple of the block size, and cannot be 29291133Seschrock * zero. 29301133Seschrock */ 29312676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 29322676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 29332676Seschrock nvlist_free(props); 29342082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29352676Seschrock "missing volume size")); 29362676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2937789Sahrens } 2938789Sahrens 29392676Seschrock if ((ret = nvlist_lookup_uint64(props, 29402676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 29412676Seschrock &blocksize)) != 0) { 29422676Seschrock if (ret == ENOENT) { 29432676Seschrock blocksize = zfs_prop_default_numeric( 29442676Seschrock ZFS_PROP_VOLBLOCKSIZE); 29452676Seschrock } else { 29462676Seschrock nvlist_free(props); 29472676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29482676Seschrock "missing volume block size")); 29492676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29502676Seschrock } 29512676Seschrock } 29522676Seschrock 29532676Seschrock if (size == 0) { 29542676Seschrock nvlist_free(props); 29552082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29562676Seschrock "volume size cannot be zero")); 29572676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29581133Seschrock } 29591133Seschrock 29601133Seschrock if (size % blocksize != 0) { 29612676Seschrock nvlist_free(props); 29622082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29632676Seschrock "volume size must be a multiple of volume block " 29642676Seschrock "size")); 29652676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29661133Seschrock } 2967789Sahrens } 2968789Sahrens 29692676Seschrock if (props && 29702676Seschrock zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) 29712676Seschrock return (-1); 29722676Seschrock nvlist_free(props); 29732676Seschrock 2974789Sahrens /* create the dataset */ 2975*4543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2976789Sahrens 29773912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 29782082Seschrock ret = zvol_create_link(hdl, path); 29793912Slling if (ret) { 29803912Slling (void) zfs_standard_error(hdl, errno, 29813912Slling dgettext(TEXT_DOMAIN, 29823912Slling "Volume successfully created, but device links " 29833912Slling "were not created")); 29843912Slling zcmd_free_nvlists(&zc); 29853912Slling return (-1); 29863912Slling } 29873912Slling } 2988789Sahrens 29892676Seschrock zcmd_free_nvlists(&zc); 29902676Seschrock 2991789Sahrens /* check for failure */ 2992789Sahrens if (ret != 0) { 2993789Sahrens char parent[ZFS_MAXNAMELEN]; 2994789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2995789Sahrens 2996789Sahrens switch (errno) { 2997789Sahrens case ENOENT: 29982082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29992082Seschrock "no such parent '%s'"), parent); 30002082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3001789Sahrens 3002789Sahrens case EINVAL: 30032082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30043413Smmusante "parent '%s' is not a filesystem"), parent); 30052082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3006789Sahrens 3007789Sahrens case EDOM: 30082082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30092676Seschrock "volume block size must be power of 2 from " 30102676Seschrock "%u to %uk"), 3011789Sahrens (uint_t)SPA_MINBLOCKSIZE, 3012789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 30132082Seschrock 30142676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 30152082Seschrock 3016789Sahrens #ifdef _ILP32 3017789Sahrens case EOVERFLOW: 3018789Sahrens /* 3019789Sahrens * This platform can't address a volume this big. 3020789Sahrens */ 30212082Seschrock if (type == ZFS_TYPE_VOLUME) 30222082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 30232082Seschrock errbuf)); 3024789Sahrens #endif 30252082Seschrock /* FALLTHROUGH */ 3026789Sahrens default: 30272082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3028789Sahrens } 3029789Sahrens } 3030789Sahrens 3031789Sahrens return (0); 3032789Sahrens } 3033789Sahrens 3034789Sahrens /* 3035789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 3036789Sahrens * isn't mounted, and that there are no active dependents. 3037789Sahrens */ 3038789Sahrens int 3039789Sahrens zfs_destroy(zfs_handle_t *zhp) 3040789Sahrens { 3041789Sahrens zfs_cmd_t zc = { 0 }; 3042789Sahrens 3043789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3044789Sahrens 30452676Seschrock if (ZFS_IS_VOLUME(zhp)) { 30463126Sahl /* 3047*4543Smarks * If user doesn't have permissions to unshare volume, then 3048*4543Smarks * abort the request. This would only happen for a 3049*4543Smarks * non-privileged user. 30503126Sahl */ 3051*4543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 3052*4543Smarks return (-1); 3053*4543Smarks } 30543126Sahl 30552082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3056789Sahrens return (-1); 3057789Sahrens 3058789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3059789Sahrens } else { 3060789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3061789Sahrens } 3062789Sahrens 3063*4543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 30643237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 30652082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 30662082Seschrock zhp->zfs_name)); 30672199Sahrens } 3068789Sahrens 3069789Sahrens remove_mountpoint(zhp); 3070789Sahrens 3071789Sahrens return (0); 3072789Sahrens } 3073789Sahrens 30742199Sahrens struct destroydata { 30752199Sahrens char *snapname; 30762199Sahrens boolean_t gotone; 30773265Sahrens boolean_t closezhp; 30782199Sahrens }; 30792199Sahrens 30802199Sahrens static int 30812199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 30822199Sahrens { 30832199Sahrens struct destroydata *dd = arg; 30842199Sahrens zfs_handle_t *szhp; 30852199Sahrens char name[ZFS_MAXNAMELEN]; 30863265Sahrens boolean_t closezhp = dd->closezhp; 30873265Sahrens int rv; 30882199Sahrens 30892676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 30902676Seschrock (void) strlcat(name, "@", sizeof (name)); 30912676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 30922199Sahrens 30932199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 30942199Sahrens if (szhp) { 30952199Sahrens dd->gotone = B_TRUE; 30962199Sahrens zfs_close(szhp); 30972199Sahrens } 30982199Sahrens 30992199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 31002199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 31012199Sahrens /* 31022199Sahrens * NB: this is simply a best-effort. We don't want to 31032199Sahrens * return an error, because then we wouldn't visit all 31042199Sahrens * the volumes. 31052199Sahrens */ 31062199Sahrens } 31072199Sahrens 31083265Sahrens dd->closezhp = B_TRUE; 31093265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 31103265Sahrens if (closezhp) 31113265Sahrens zfs_close(zhp); 31123265Sahrens return (rv); 31132199Sahrens } 31142199Sahrens 31152199Sahrens /* 31162199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 31172199Sahrens */ 31182199Sahrens int 31192199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 31202199Sahrens { 31212199Sahrens zfs_cmd_t zc = { 0 }; 31222199Sahrens int ret; 31232199Sahrens struct destroydata dd = { 0 }; 31242199Sahrens 31252199Sahrens dd.snapname = snapname; 31262199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 31272199Sahrens 31282199Sahrens if (!dd.gotone) { 31293237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 31302199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 31312199Sahrens zhp->zfs_name, snapname)); 31322199Sahrens } 31332199Sahrens 31342199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31352676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 31362199Sahrens 3137*4543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 31382199Sahrens if (ret != 0) { 31392199Sahrens char errbuf[1024]; 31402199Sahrens 31412199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31422199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 31432199Sahrens 31442199Sahrens switch (errno) { 31452199Sahrens case EEXIST: 31462199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31472199Sahrens "snapshot is cloned")); 31482199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 31492199Sahrens 31502199Sahrens default: 31512199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 31522199Sahrens errbuf)); 31532199Sahrens } 31542199Sahrens } 31552199Sahrens 31562199Sahrens return (0); 31572199Sahrens } 31582199Sahrens 3159789Sahrens /* 3160789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3161789Sahrens */ 3162789Sahrens int 31632676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3164789Sahrens { 3165789Sahrens zfs_cmd_t zc = { 0 }; 3166789Sahrens char parent[ZFS_MAXNAMELEN]; 3167789Sahrens int ret; 31682082Seschrock char errbuf[1024]; 31692082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 31702676Seschrock zfs_type_t type; 31712676Seschrock uint64_t zoned; 3172789Sahrens 3173789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3174789Sahrens 31752082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31762082Seschrock "cannot create '%s'"), target); 31772082Seschrock 3178789Sahrens /* validate the target name */ 31792082Seschrock if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM)) 31802082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3181789Sahrens 3182789Sahrens /* validate parents exist */ 31834490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3184789Sahrens return (-1); 3185789Sahrens 3186789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3187789Sahrens 3188789Sahrens /* do the clone */ 31892676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3190789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 31912744Snn35248 type = ZFS_TYPE_VOLUME; 31922676Seschrock } else { 3193789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 31942744Snn35248 type = ZFS_TYPE_FILESYSTEM; 31952676Seschrock } 31962676Seschrock 31972676Seschrock if (props) { 31983912Slling if ((props = zfs_validate_properties(hdl, type, NULL, props, 31993912Slling zoned, zhp, errbuf)) == NULL) 32002676Seschrock return (-1); 32012676Seschrock 32022676Seschrock if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) { 32032676Seschrock nvlist_free(props); 32042676Seschrock return (-1); 32052676Seschrock } 32062676Seschrock 32072676Seschrock nvlist_free(props); 32082676Seschrock } 3209789Sahrens 3210789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 32112676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 3212*4543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3213789Sahrens 32142676Seschrock zcmd_free_nvlists(&zc); 32152676Seschrock 3216789Sahrens if (ret != 0) { 3217789Sahrens switch (errno) { 3218789Sahrens 3219789Sahrens case ENOENT: 3220789Sahrens /* 3221789Sahrens * The parent doesn't exist. We should have caught this 3222789Sahrens * above, but there may a race condition that has since 3223789Sahrens * destroyed the parent. 3224789Sahrens * 3225789Sahrens * At this point, we don't know whether it's the source 3226789Sahrens * that doesn't exist anymore, or whether the target 3227789Sahrens * dataset doesn't exist. 3228789Sahrens */ 32292082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32302082Seschrock "no such parent '%s'"), parent); 32312082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 32322082Seschrock 32332082Seschrock case EXDEV: 32342082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32352082Seschrock "source and target pools differ")); 32362082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 32372082Seschrock errbuf)); 32382082Seschrock 32392082Seschrock default: 32402082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 32412082Seschrock errbuf)); 32422082Seschrock } 32432676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 32442082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 32452082Seschrock } 32462082Seschrock 32472082Seschrock return (ret); 32482082Seschrock } 32492082Seschrock 32502082Seschrock typedef struct promote_data { 32512082Seschrock char cb_mountpoint[MAXPATHLEN]; 32522082Seschrock const char *cb_target; 32532082Seschrock const char *cb_errbuf; 32542082Seschrock uint64_t cb_pivot_txg; 32552082Seschrock } promote_data_t; 32562082Seschrock 32572082Seschrock static int 32582082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 32592082Seschrock { 32602082Seschrock promote_data_t *pd = data; 32612082Seschrock zfs_handle_t *szhp; 32622082Seschrock char snapname[MAXPATHLEN]; 32633265Sahrens int rv = 0; 32642082Seschrock 32652082Seschrock /* We don't care about snapshots after the pivot point */ 32663265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 32673265Sahrens zfs_close(zhp); 32682082Seschrock return (0); 32693265Sahrens } 32702082Seschrock 32712417Sahrens /* Remove the device link if it's a zvol. */ 32722676Seschrock if (ZFS_IS_VOLUME(zhp)) 32732417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 32742082Seschrock 32752082Seschrock /* Check for conflicting names */ 32762676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 32772676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 32782082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 32792082Seschrock if (szhp != NULL) { 32802082Seschrock zfs_close(szhp); 32812082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32822082Seschrock "snapshot name '%s' from origin \n" 32832082Seschrock "conflicts with '%s' from target"), 32842082Seschrock zhp->zfs_name, snapname); 32853265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 32862082Seschrock } 32873265Sahrens zfs_close(zhp); 32883265Sahrens return (rv); 32892082Seschrock } 32902082Seschrock 32912417Sahrens static int 32922417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 32932417Sahrens { 32942417Sahrens promote_data_t *pd = data; 32952417Sahrens 32962417Sahrens /* We don't care about snapshots after the pivot point */ 32973265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 32983265Sahrens /* Create the device link if it's a zvol. */ 32993265Sahrens if (ZFS_IS_VOLUME(zhp)) 33003265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 33013265Sahrens } 33023265Sahrens 33033265Sahrens zfs_close(zhp); 33042417Sahrens return (0); 33052417Sahrens } 33062417Sahrens 33072082Seschrock /* 33082082Seschrock * Promotes the given clone fs to be the clone parent. 33092082Seschrock */ 33102082Seschrock int 33112082Seschrock zfs_promote(zfs_handle_t *zhp) 33122082Seschrock { 33132082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 33142082Seschrock zfs_cmd_t zc = { 0 }; 33152082Seschrock char parent[MAXPATHLEN]; 33162082Seschrock char *cp; 33172082Seschrock int ret; 33182082Seschrock zfs_handle_t *pzhp; 33192082Seschrock promote_data_t pd; 33202082Seschrock char errbuf[1024]; 33212082Seschrock 33222082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33232082Seschrock "cannot promote '%s'"), zhp->zfs_name); 33242082Seschrock 33252082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 33262082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33272082Seschrock "snapshots can not be promoted")); 33282082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 33292082Seschrock } 33302082Seschrock 33312676Seschrock (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 33322082Seschrock if (parent[0] == '\0') { 33332082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33342082Seschrock "not a cloned filesystem")); 33352082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 33362082Seschrock } 33372082Seschrock cp = strchr(parent, '@'); 33382082Seschrock *cp = '\0'; 33392082Seschrock 33402082Seschrock /* Walk the snapshots we will be moving */ 33412082Seschrock pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 33422082Seschrock if (pzhp == NULL) 33432082Seschrock return (-1); 33442082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 33452082Seschrock zfs_close(pzhp); 33462082Seschrock pd.cb_target = zhp->zfs_name; 33472082Seschrock pd.cb_errbuf = errbuf; 33482082Seschrock pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY); 33492082Seschrock if (pzhp == NULL) 33502082Seschrock return (-1); 33512082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 33522082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 33532082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 33542417Sahrens if (ret != 0) { 33552417Sahrens zfs_close(pzhp); 33562082Seschrock return (-1); 33572417Sahrens } 33582082Seschrock 33592082Seschrock /* issue the ioctl */ 33602676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 33612676Seschrock sizeof (zc.zc_value)); 33622082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3363*4543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 33642082Seschrock 33652082Seschrock if (ret != 0) { 33662417Sahrens int save_errno = errno; 33672417Sahrens 33682417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 33692417Sahrens zfs_close(pzhp); 33702417Sahrens 33712417Sahrens switch (save_errno) { 3372789Sahrens case EEXIST: 3373789Sahrens /* 33742082Seschrock * There is a conflicting snapshot name. We 33752082Seschrock * should have caught this above, but they could 33762082Seschrock * have renamed something in the mean time. 3377789Sahrens */ 33782082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33792082Seschrock "conflicting snapshot name from parent '%s'"), 33802082Seschrock parent); 33812082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3382789Sahrens 3383789Sahrens default: 33842417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3385789Sahrens } 33862417Sahrens } else { 33872417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3388789Sahrens } 3389789Sahrens 33902417Sahrens zfs_close(pzhp); 3391789Sahrens return (ret); 3392789Sahrens } 3393789Sahrens 33944007Smmusante struct createdata { 33954007Smmusante const char *cd_snapname; 33964007Smmusante int cd_ifexists; 33974007Smmusante }; 33984007Smmusante 33992199Sahrens static int 34002199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 34012199Sahrens { 34024007Smmusante struct createdata *cd = arg; 34032676Seschrock int ret; 34042199Sahrens 34052199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 34062199Sahrens char name[MAXPATHLEN]; 34072199Sahrens 34082676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 34092676Seschrock (void) strlcat(name, "@", sizeof (name)); 34104007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 34114007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 34124007Smmusante cd->cd_ifexists); 34132199Sahrens /* 34142199Sahrens * NB: this is simply a best-effort. We don't want to 34152199Sahrens * return an error, because then we wouldn't visit all 34162199Sahrens * the volumes. 34172199Sahrens */ 34182199Sahrens } 34192676Seschrock 34204007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 34212676Seschrock 34222676Seschrock zfs_close(zhp); 34232676Seschrock 34242676Seschrock return (ret); 34252199Sahrens } 34262199Sahrens 3427789Sahrens /* 34283504Sahl * Takes a snapshot of the given dataset. 3429789Sahrens */ 3430789Sahrens int 34312199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3432789Sahrens { 3433789Sahrens const char *delim; 3434789Sahrens char *parent; 3435789Sahrens zfs_handle_t *zhp; 3436789Sahrens zfs_cmd_t zc = { 0 }; 3437789Sahrens int ret; 34382082Seschrock char errbuf[1024]; 34392082Seschrock 34402082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34412082Seschrock "cannot snapshot '%s'"), path); 34422082Seschrock 34432082Seschrock /* validate the target name */ 34442082Seschrock if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT)) 34452082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3446789Sahrens 3447789Sahrens /* make sure the parent exists and is of the appropriate type */ 34482199Sahrens delim = strchr(path, '@'); 34492082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 34502082Seschrock return (-1); 3451789Sahrens (void) strncpy(parent, path, delim - path); 3452789Sahrens parent[delim - path] = '\0'; 3453789Sahrens 34542082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3455789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3456789Sahrens free(parent); 3457789Sahrens return (-1); 3458789Sahrens } 3459789Sahrens 34602199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 34612676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 3462*4543Smarks if (ZFS_IS_VOLUME(zhp)) 3463*4543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 3464*4543Smarks else 3465*4543Smarks zc.zc_objset_type = DMU_OST_ZFS; 34662199Sahrens zc.zc_cookie = recursive; 3467*4543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 34682199Sahrens 34692199Sahrens /* 34702199Sahrens * if it was recursive, the one that actually failed will be in 34712199Sahrens * zc.zc_name. 34722199Sahrens */ 3473*4543Smarks if (ret != 0) 3474*4543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3475*4543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 3476*4543Smarks 34772199Sahrens if (ret == 0 && recursive) { 34784007Smmusante struct createdata cd; 34794007Smmusante 34804007Smmusante cd.cd_snapname = delim + 1; 34814007Smmusante cd.cd_ifexists = B_FALSE; 34824007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 34832199Sahrens } 3484789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 34852082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 34862199Sahrens if (ret != 0) { 3487*4543Smarks (void) zfs_standard_error(hdl, errno, 3488*4543Smarks dgettext(TEXT_DOMAIN, 3489*4543Smarks "Volume successfully snapshotted, but device links " 3490*4543Smarks "were not created")); 3491*4543Smarks free(parent); 3492*4543Smarks zfs_close(zhp); 3493*4543Smarks return (-1); 34942199Sahrens } 3495789Sahrens } 3496789Sahrens 34972082Seschrock if (ret != 0) 34982082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3499789Sahrens 3500789Sahrens free(parent); 3501789Sahrens zfs_close(zhp); 3502789Sahrens 3503789Sahrens return (ret); 3504789Sahrens } 3505789Sahrens 3506789Sahrens /* 35073504Sahl * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 35083504Sahl * NULL) to the file descriptor specified by outfd. 3509789Sahrens */ 3510789Sahrens int 35113504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3512789Sahrens { 3513789Sahrens zfs_cmd_t zc = { 0 }; 35142082Seschrock char errbuf[1024]; 35152885Sahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 35162082Seschrock 35173504Sahl assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 35183504Sahl 35192885Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35202885Sahrens if (fromsnap) 35212885Sahrens (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 35223504Sahl zc.zc_cookie = outfd; 35233504Sahl 35243504Sahl if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 35253504Sahl (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35263504Sahl "cannot send '%s'"), zhp->zfs_name); 35273504Sahl 3528789Sahrens switch (errno) { 3529789Sahrens 3530789Sahrens case EXDEV: 35312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35323413Smmusante "not an earlier snapshot from the same fs")); 35332082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3534789Sahrens 3535789Sahrens case EDQUOT: 3536789Sahrens case EFBIG: 3537789Sahrens case EIO: 3538789Sahrens case ENOLINK: 3539789Sahrens case ENOSPC: 3540789Sahrens case ENOSTR: 3541789Sahrens case ENXIO: 3542789Sahrens case EPIPE: 3543789Sahrens case ERANGE: 3544789Sahrens case EFAULT: 3545789Sahrens case EROFS: 35462082Seschrock zfs_error_aux(hdl, strerror(errno)); 35472082Seschrock return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3548789Sahrens 3549789Sahrens default: 35502082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3551789Sahrens } 3552789Sahrens } 3553789Sahrens 35543504Sahl return (0); 3555789Sahrens } 3556789Sahrens 3557789Sahrens /* 35582885Sahrens * Create ancestors of 'target', but not target itself, and not 35592885Sahrens * ancestors whose names are shorter than prefixlen. Die if 35602885Sahrens * prefixlen-ancestor does not exist. 35612885Sahrens */ 35622885Sahrens static int 35632885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 35642885Sahrens { 35652885Sahrens zfs_handle_t *h; 35662885Sahrens char *cp; 35672885Sahrens 35682885Sahrens /* make sure prefix exists */ 35692885Sahrens cp = strchr(target + prefixlen, '/'); 35702885Sahrens *cp = '\0'; 35712885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 35722885Sahrens *cp = '/'; 35732885Sahrens if (h == NULL) 35742885Sahrens return (-1); 35752885Sahrens zfs_close(h); 35762885Sahrens 35772885Sahrens /* 35782885Sahrens * Attempt to create, mount, and share any ancestor filesystems, 35792885Sahrens * up to the prefixlen-long one. 35802885Sahrens */ 35812885Sahrens for (cp = target + prefixlen + 1; 35822885Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 35832885Sahrens const char *opname; 3584*4543Smarks char *logstr; 35852885Sahrens 35862885Sahrens *cp = '\0'; 35872885Sahrens 35882885Sahrens h = make_dataset_handle(hdl, target); 35892885Sahrens if (h) { 35902885Sahrens /* it already exists, nothing to do here */ 35912885Sahrens zfs_close(h); 35922885Sahrens continue; 35932885Sahrens } 35942885Sahrens 35952885Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 3596*4543Smarks logstr = hdl->libzfs_log_str; 3597*4543Smarks hdl->libzfs_log_str = NULL; 35982885Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 3599*4543Smarks NULL) != 0) { 3600*4543Smarks hdl->libzfs_log_str = logstr; 36012885Sahrens goto ancestorerr; 3602*4543Smarks } 3603*4543Smarks 3604*4543Smarks hdl->libzfs_log_str = logstr; 36052885Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 36062885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 36072885Sahrens if (h == NULL) 36082885Sahrens goto ancestorerr; 36092885Sahrens 36102885Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 36112885Sahrens if (zfs_mount(h, NULL, 0) != 0) 36122885Sahrens goto ancestorerr; 36132885Sahrens 36142885Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 36152885Sahrens if (zfs_share(h) != 0) 36162885Sahrens goto ancestorerr; 36172885Sahrens 36182885Sahrens zfs_close(h); 36192885Sahrens 36202885Sahrens continue; 36212885Sahrens ancestorerr: 36222885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36232885Sahrens "failed to %s ancestor '%s'"), opname, target); 36242885Sahrens return (-1); 36252885Sahrens } 36262885Sahrens 36272885Sahrens return (0); 36282885Sahrens } 36292885Sahrens 36302885Sahrens /* 36313504Sahl * Restores a backup of tosnap from the file descriptor specified by infd. 3632789Sahrens */ 3633789Sahrens int 36342082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 36353504Sahl int verbose, int dryrun, boolean_t force, int infd) 3636789Sahrens { 3637789Sahrens zfs_cmd_t zc = { 0 }; 3638789Sahrens time_t begin_time; 36392885Sahrens int ioctl_err, err, bytes, size, choplen; 3640789Sahrens char *cp; 3641789Sahrens dmu_replay_record_t drr; 3642789Sahrens struct drr_begin *drrb = &zc.zc_begin_record; 36432082Seschrock char errbuf[1024]; 36442665Snd150628 prop_changelist_t *clp; 36452885Sahrens char chopprefix[ZFS_MAXNAMELEN]; 3646789Sahrens 3647789Sahrens begin_time = time(NULL); 3648789Sahrens 36492082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36502082Seschrock "cannot receive")); 36512082Seschrock 3652789Sahrens /* read in the BEGIN record */ 3653789Sahrens cp = (char *)&drr; 3654789Sahrens bytes = 0; 3655789Sahrens do { 36563504Sahl size = read(infd, cp, sizeof (drr) - bytes); 3657868Sahrens cp += size; 3658868Sahrens bytes += size; 3659868Sahrens } while (size > 0); 3660868Sahrens 3661868Sahrens if (size < 0 || bytes != sizeof (drr)) { 36622082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36632082Seschrock "stream (failed to read first record)")); 36642082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3665789Sahrens } 3666789Sahrens 3667789Sahrens zc.zc_begin_record = drr.drr_u.drr_begin; 3668789Sahrens 3669789Sahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3670789Sahrens drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 36712082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36722082Seschrock "stream (bad magic number)")); 36732082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3674789Sahrens } 3675789Sahrens 3676789Sahrens if (drrb->drr_version != DMU_BACKUP_VERSION && 3677789Sahrens drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 36782082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 36792082Seschrock "0x%llx is supported (stream is version 0x%llx)"), 3680789Sahrens DMU_BACKUP_VERSION, drrb->drr_version); 36812082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3682789Sahrens } 3683789Sahrens 36842885Sahrens if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 36852885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 36863912Slling "stream (bad snapshot name)")); 36872885Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 36882885Sahrens } 3689789Sahrens /* 36902885Sahrens * Determine how much of the snapshot name stored in the stream 36912885Sahrens * we are going to tack on to the name they specified on the 36922885Sahrens * command line, and how much we are going to chop off. 36932885Sahrens * 36942885Sahrens * If they specified a snapshot, chop the entire name stored in 36952885Sahrens * the stream. 3696789Sahrens */ 36972885Sahrens (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3698789Sahrens if (isprefix) { 36992885Sahrens /* 37002885Sahrens * They specified a fs with -d, we want to tack on 37012885Sahrens * everything but the pool name stored in the stream 37022885Sahrens */ 37032885Sahrens if (strchr(tosnap, '@')) { 37042885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 37052885Sahrens "argument - snapshot not allowed with -d")); 37062885Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3707789Sahrens } 37082885Sahrens cp = strchr(chopprefix, '/'); 3709789Sahrens if (cp == NULL) 37102885Sahrens cp = strchr(chopprefix, '@'); 37112885Sahrens *cp = '\0'; 3712789Sahrens } else if (strchr(tosnap, '@') == NULL) { 3713789Sahrens /* 37142885Sahrens * If they specified a filesystem without -d, we want to 37152885Sahrens * tack on everything after the fs specified in the 37162885Sahrens * first name from the stream. 3717789Sahrens */ 37182885Sahrens cp = strchr(chopprefix, '@'); 37192885Sahrens *cp = '\0'; 3720789Sahrens } 37212885Sahrens choplen = strlen(chopprefix); 37222885Sahrens 37232885Sahrens /* 37242885Sahrens * Determine name of destination snapshot, store in zc_value. 37252885Sahrens */ 37262885Sahrens (void) strcpy(zc.zc_value, tosnap); 37272885Sahrens (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 37282885Sahrens sizeof (zc.zc_value)); 37293265Sahrens if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT)) 37303265Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37312885Sahrens 37322885Sahrens (void) strcpy(zc.zc_name, zc.zc_value); 3733789Sahrens if (drrb->drr_fromguid) { 3734789Sahrens /* incremental backup stream */ 37352885Sahrens zfs_handle_t *h; 37362885Sahrens 37372885Sahrens /* do the recvbackup ioctl to the containing fs */ 37382885Sahrens *strchr(zc.zc_name, '@') = '\0'; 3739789Sahrens 3740789Sahrens /* make sure destination fs exists */ 37412082Seschrock h = zfs_open(hdl, zc.zc_name, 37422082Seschrock ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 37432082Seschrock if (h == NULL) 3744789Sahrens return (-1); 3745868Sahrens if (!dryrun) { 37462665Snd150628 /* 37472665Snd150628 * We need to unmount all the dependents of the dataset 37482665Snd150628 * and the dataset itself. If it's a volume 37492665Snd150628 * then remove device link. 37502665Snd150628 */ 3751868Sahrens if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 37522665Snd150628 clp = changelist_gather(h, ZFS_PROP_NAME, 0); 37532665Snd150628 if (clp == NULL) 37542665Snd150628 return (-1); 37552665Snd150628 if (changelist_prefix(clp) != 0) { 37562665Snd150628 changelist_free(clp); 37572665Snd150628 return (-1); 37582665Snd150628 } 3759868Sahrens } else { 3760*4543Smarks if (zvol_remove_link(hdl, h->zfs_name) != 0) { 3761*4543Smarks zfs_close(h); 3762*4543Smarks return (-1); 3763*4543Smarks } 3764*4543Smarks 3765868Sahrens } 3766868Sahrens } 3767789Sahrens zfs_close(h); 3768789Sahrens } else { 3769789Sahrens /* full backup stream */ 3770789Sahrens 3771868Sahrens /* Make sure destination fs does not exist */ 37722885Sahrens *strchr(zc.zc_name, '@') = '\0'; 37734490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 37742082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37752082Seschrock "destination '%s' exists"), zc.zc_name); 37762082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3777868Sahrens } 3778868Sahrens 37792885Sahrens if (strchr(zc.zc_name, '/') == NULL) { 37802885Sahrens /* 37812885Sahrens * they're trying to do a recv into a 37822885Sahrens * nonexistant topmost filesystem. 37832885Sahrens */ 37842885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37852885Sahrens "destination does not exist"), zc.zc_name); 37862885Sahrens return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 37872885Sahrens } 37882885Sahrens 3789868Sahrens /* Do the recvbackup ioctl to the fs's parent. */ 37902885Sahrens *strrchr(zc.zc_name, '/') = '\0'; 37912885Sahrens 37922885Sahrens if (isprefix && (err = create_parents(hdl, 37932885Sahrens zc.zc_value, strlen(tosnap))) != 0) { 37942885Sahrens return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 37952885Sahrens } 37962885Sahrens 3797789Sahrens } 3798789Sahrens 37993504Sahl zc.zc_cookie = infd; 38002676Seschrock zc.zc_guid = force; 3801789Sahrens if (verbose) { 38021749Sahrens (void) printf("%s %s stream of %s into %s\n", 38031749Sahrens dryrun ? "would receive" : "receiving", 3804789Sahrens drrb->drr_fromguid ? "incremental" : "full", 3805789Sahrens drr.drr_u.drr_begin.drr_toname, 38062676Seschrock zc.zc_value); 3807789Sahrens (void) fflush(stdout); 3808789Sahrens } 3809789Sahrens if (dryrun) 3810789Sahrens return (0); 3811*4543Smarks err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3812868Sahrens if (ioctl_err != 0) { 3813789Sahrens switch (errno) { 3814789Sahrens case ENODEV: 38152082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38162082Seschrock "most recent snapshot does not match incremental " 38172082Seschrock "source")); 38182082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3819789Sahrens break; 3820789Sahrens case ETXTBSY: 38212082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38222082Seschrock "destination has been modified since most recent " 38232082Seschrock "snapshot")); 38242082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3825789Sahrens break; 3826789Sahrens case EEXIST: 3827789Sahrens if (drrb->drr_fromguid == 0) { 3828789Sahrens /* it's the containing fs that exists */ 38292676Seschrock cp = strchr(zc.zc_value, '@'); 3830789Sahrens *cp = '\0'; 3831789Sahrens } 38322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38332082Seschrock "destination already exists")); 38343237Slling (void) zfs_error_fmt(hdl, EZFS_EXISTS, 38353237Slling dgettext(TEXT_DOMAIN, "cannot restore to %s"), 38363237Slling zc.zc_value); 3837789Sahrens break; 3838789Sahrens case EINVAL: 38392082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3840868Sahrens break; 38411544Seschrock case ECKSUM: 38422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38432082Seschrock "invalid stream (checksum mismatch)")); 38442082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3845789Sahrens break; 3846789Sahrens default: 38472082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3848789Sahrens } 3849789Sahrens } 3850789Sahrens 3851789Sahrens /* 3852868Sahrens * Mount or recreate the /dev links for the target filesystem 3853868Sahrens * (if created, or if we tore them down to do an incremental 3854868Sahrens * restore), and the /dev links for the new snapshot (if 38552665Snd150628 * created). Also mount any children of the target filesystem 38562665Snd150628 * if we did an incremental receive. 3857789Sahrens */ 38582676Seschrock cp = strchr(zc.zc_value, '@'); 3859868Sahrens if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3860789Sahrens zfs_handle_t *h; 3861789Sahrens 3862789Sahrens *cp = '\0'; 38632676Seschrock h = zfs_open(hdl, zc.zc_value, 3864789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3865868Sahrens *cp = '@'; 3866789Sahrens if (h) { 38672665Snd150628 if (h->zfs_type == ZFS_TYPE_VOLUME) { 38682082Seschrock err = zvol_create_link(hdl, h->zfs_name); 38691544Seschrock if (err == 0 && ioctl_err == 0) 38702082Seschrock err = zvol_create_link(hdl, 38712676Seschrock zc.zc_value); 38722665Snd150628 } else { 38732665Snd150628 if (drrb->drr_fromguid) { 38742665Snd150628 err = changelist_postfix(clp); 38752665Snd150628 changelist_free(clp); 38762665Snd150628 } else { 38772665Snd150628 err = zfs_mount(h, NULL, 0); 38782665Snd150628 } 3879868Sahrens } 38802665Snd150628 zfs_close(h); 3881789Sahrens } 3882789Sahrens } 3883789Sahrens 3884868Sahrens if (err || ioctl_err) 3885868Sahrens return (-1); 3886789Sahrens 3887789Sahrens if (verbose) { 3888789Sahrens char buf1[64]; 3889789Sahrens char buf2[64]; 3890789Sahrens uint64_t bytes = zc.zc_cookie; 3891789Sahrens time_t delta = time(NULL) - begin_time; 3892789Sahrens if (delta == 0) 3893789Sahrens delta = 1; 3894789Sahrens zfs_nicenum(bytes, buf1, sizeof (buf1)); 3895789Sahrens zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3896789Sahrens 38971749Sahrens (void) printf("received %sb stream in %lu seconds (%sb/sec)\n", 3898789Sahrens buf1, delta, buf2); 3899789Sahrens } 39002665Snd150628 3901789Sahrens return (0); 3902789Sahrens } 3903789Sahrens 3904789Sahrens /* 39051294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 39061294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 39071294Slling * is a dependent and we should just destroy it without checking the transaction 39081294Slling * group. 3909789Sahrens */ 39101294Slling typedef struct rollback_data { 39111294Slling const char *cb_target; /* the snapshot */ 39121294Slling uint64_t cb_create; /* creation time reference */ 39131294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 39141294Slling int cb_error; 39152082Seschrock boolean_t cb_dependent; 39161294Slling } rollback_data_t; 39171294Slling 39181294Slling static int 39191294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 39201294Slling { 39211294Slling rollback_data_t *cbp = data; 39221294Slling 39231294Slling if (!cbp->cb_dependent) { 39241294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 39251294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 39261294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 39271294Slling cbp->cb_create) { 3928*4543Smarks char *logstr; 39291294Slling 39302082Seschrock cbp->cb_dependent = B_TRUE; 39312474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 39322474Seschrock cbp) != 0) 39332474Seschrock cbp->cb_error = 1; 39342082Seschrock cbp->cb_dependent = B_FALSE; 39351294Slling 3936*4543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 3937*4543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 39381294Slling if (zfs_destroy(zhp) != 0) 39391294Slling cbp->cb_error = 1; 39401294Slling else 39411294Slling changelist_remove(zhp, cbp->cb_clp); 3942*4543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 39431294Slling } 39441294Slling } else { 39451294Slling if (zfs_destroy(zhp) != 0) 39461294Slling cbp->cb_error = 1; 39471294Slling else 39481294Slling changelist_remove(zhp, cbp->cb_clp); 39491294Slling } 39501294Slling 39511294Slling zfs_close(zhp); 39521294Slling return (0); 39531294Slling } 39541294Slling 39551294Slling /* 39561294Slling * Rollback the dataset to its latest snapshot. 39571294Slling */ 39581294Slling static int 39591294Slling do_rollback(zfs_handle_t *zhp) 3960789Sahrens { 3961789Sahrens int ret; 3962789Sahrens zfs_cmd_t zc = { 0 }; 3963789Sahrens 3964789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3965789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3966789Sahrens 3967789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 39682082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3969789Sahrens return (-1); 3970789Sahrens 3971789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3972789Sahrens 39732676Seschrock if (ZFS_IS_VOLUME(zhp)) 3974789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3975789Sahrens else 3976789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3977789Sahrens 3978789Sahrens /* 3979789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3980789Sahrens * for the given dataset. Given these constraints, we can simply pass 3981789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3982789Sahrens * condition where the user has taken a snapshot since we verified that 3983789Sahrens * this was the most recent. 3984789Sahrens */ 3985*4543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 39863237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 39872082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 39882082Seschrock zhp->zfs_name); 3989789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 39902082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3991789Sahrens } 3992789Sahrens 3993789Sahrens return (ret); 3994789Sahrens } 3995789Sahrens 3996789Sahrens /* 39971294Slling * Given a dataset, rollback to a specific snapshot, discarding any 39981294Slling * data changes since then and making it the active dataset. 39991294Slling * 40001294Slling * Any snapshots more recent than the target are destroyed, along with 40011294Slling * their dependents. 40021294Slling */ 40031294Slling int 40041294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 40051294Slling { 40061294Slling int ret; 40071294Slling rollback_data_t cb = { 0 }; 40081294Slling prop_changelist_t *clp; 40091294Slling 40101294Slling /* 40111294Slling * Unmount all dependendents of the dataset and the dataset itself. 40121294Slling * The list we need to gather is the same as for doing rename 40131294Slling */ 40141294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 40151294Slling if (clp == NULL) 40161294Slling return (-1); 40171294Slling 40181294Slling if ((ret = changelist_prefix(clp)) != 0) 40191294Slling goto out; 40201294Slling 40211294Slling /* 40221294Slling * Destroy all recent snapshots and its dependends. 40231294Slling */ 40241294Slling cb.cb_target = snap->zfs_name; 40251294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 40261294Slling cb.cb_clp = clp; 40271294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 40281294Slling 40291294Slling if ((ret = cb.cb_error) != 0) { 40301294Slling (void) changelist_postfix(clp); 40311294Slling goto out; 40321294Slling } 40331294Slling 40341294Slling /* 40351294Slling * Now that we have verified that the snapshot is the latest, 40361294Slling * rollback to the given snapshot. 40371294Slling */ 40381294Slling ret = do_rollback(zhp); 40391294Slling 40401294Slling if (ret != 0) { 40411294Slling (void) changelist_postfix(clp); 40421294Slling goto out; 40431294Slling } 40441294Slling 40451294Slling /* 40461294Slling * We only want to re-mount the filesystem if it was mounted in the 40471294Slling * first place. 40481294Slling */ 40491294Slling ret = changelist_postfix(clp); 40501294Slling 40511294Slling out: 40521294Slling changelist_free(clp); 40531294Slling return (ret); 40541294Slling } 40551294Slling 40561294Slling /* 4057789Sahrens * Iterate over all dependents for a given dataset. This includes both 4058789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 4059789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 4060789Sahrens * libzfs_graph.c. 4061789Sahrens */ 4062789Sahrens int 40632474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 40642474Seschrock zfs_iter_f func, void *data) 4065789Sahrens { 4066789Sahrens char **dependents; 4067789Sahrens size_t count; 4068789Sahrens int i; 4069789Sahrens zfs_handle_t *child; 4070789Sahrens int ret = 0; 4071789Sahrens 40722474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 40732474Seschrock &dependents, &count) != 0) 40742474Seschrock return (-1); 40752474Seschrock 4076789Sahrens for (i = 0; i < count; i++) { 40772082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 40782082Seschrock dependents[i])) == NULL) 4079789Sahrens continue; 4080789Sahrens 4081789Sahrens if ((ret = func(child, data)) != 0) 4082789Sahrens break; 4083789Sahrens } 4084789Sahrens 4085789Sahrens for (i = 0; i < count; i++) 4086789Sahrens free(dependents[i]); 4087789Sahrens free(dependents); 4088789Sahrens 4089789Sahrens return (ret); 4090789Sahrens } 4091789Sahrens 4092789Sahrens /* 4093789Sahrens * Renames the given dataset. 4094789Sahrens */ 4095789Sahrens int 40964490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 4097789Sahrens { 4098789Sahrens int ret; 4099789Sahrens zfs_cmd_t zc = { 0 }; 4100789Sahrens char *delim; 41014007Smmusante prop_changelist_t *cl = NULL; 41024007Smmusante zfs_handle_t *zhrp = NULL; 41034007Smmusante char *parentname = NULL; 4104789Sahrens char parent[ZFS_MAXNAMELEN]; 41052082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 41062082Seschrock char errbuf[1024]; 4107789Sahrens 4108789Sahrens /* if we have the same exact name, just return success */ 4109789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 4110789Sahrens return (0); 4111789Sahrens 41122082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 41132082Seschrock "cannot rename to '%s'"), target); 41142082Seschrock 4115789Sahrens /* 4116789Sahrens * Make sure the target name is valid 4117789Sahrens */ 4118789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 41192665Snd150628 if ((strchr(target, '@') == NULL) || 41202665Snd150628 *target == '@') { 41212665Snd150628 /* 41222665Snd150628 * Snapshot target name is abbreviated, 41232665Snd150628 * reconstruct full dataset name 41242665Snd150628 */ 41252665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 41262665Snd150628 sizeof (parent)); 41272665Snd150628 delim = strchr(parent, '@'); 41282665Snd150628 if (strchr(target, '@') == NULL) 41292665Snd150628 *(++delim) = '\0'; 41302665Snd150628 else 41312665Snd150628 *delim = '\0'; 41322665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 41332665Snd150628 target = parent; 41342665Snd150628 } else { 41352665Snd150628 /* 41362665Snd150628 * Make sure we're renaming within the same dataset. 41372665Snd150628 */ 41382665Snd150628 delim = strchr(target, '@'); 41392665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 41402665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 41412665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41422665Snd150628 "snapshots must be part of same " 41432665Snd150628 "dataset")); 41442665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 41453912Slling errbuf)); 41462665Snd150628 } 4147789Sahrens } 41482665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 41492665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4150789Sahrens } else { 41514007Smmusante if (recursive) { 41524007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41534007Smmusante "recursive rename must be a snapshot")); 41544007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 41554007Smmusante } 41564007Smmusante 41572665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 41582665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 41592676Seschrock uint64_t unused; 41602676Seschrock 4161789Sahrens /* validate parents */ 41624490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4163789Sahrens return (-1); 4164789Sahrens 4165789Sahrens (void) parent_name(target, parent, sizeof (parent)); 4166789Sahrens 4167789Sahrens /* make sure we're in the same pool */ 4168789Sahrens verify((delim = strchr(target, '/')) != NULL); 4169789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4170789Sahrens zhp->zfs_name[delim - target] != '/') { 41712082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41722082Seschrock "datasets must be within same pool")); 41732082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4174789Sahrens } 41752440Snd150628 41762440Snd150628 /* new name cannot be a child of the current dataset name */ 41772440Snd150628 if (strncmp(parent, zhp->zfs_name, 41783912Slling strlen(zhp->zfs_name)) == 0) { 41792440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41802440Snd150628 "New dataset name cannot be a descendent of " 41812440Snd150628 "current dataset name")); 41822440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 41832440Snd150628 } 4184789Sahrens } 4185789Sahrens 41862082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 41872082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 41882082Seschrock 4189789Sahrens if (getzoneid() == GLOBAL_ZONEID && 4190789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 41912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41922082Seschrock "dataset is used in a non-global zone")); 41932082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4194789Sahrens } 4195789Sahrens 41964007Smmusante if (recursive) { 41974007Smmusante struct destroydata dd; 41984007Smmusante 41994183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 42004183Smmusante if (parentname == NULL) { 42014183Smmusante ret = -1; 42024183Smmusante goto error; 42034183Smmusante } 42044007Smmusante delim = strchr(parentname, '@'); 42054007Smmusante *delim = '\0'; 42064007Smmusante zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY); 42074007Smmusante if (zhrp == NULL) { 42084183Smmusante ret = -1; 42094183Smmusante goto error; 42104007Smmusante } 42114007Smmusante 42124007Smmusante dd.snapname = delim + 1; 42134007Smmusante dd.gotone = B_FALSE; 42144183Smmusante dd.closezhp = B_TRUE; 42154007Smmusante 42164007Smmusante /* We remove any zvol links prior to renaming them */ 42174007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 42184007Smmusante if (ret) { 42194007Smmusante goto error; 42204007Smmusante } 42214007Smmusante } else { 42224007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 42234007Smmusante return (-1); 42244007Smmusante 42254007Smmusante if (changelist_haszonedchild(cl)) { 42264007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42274007Smmusante "child dataset with inherited mountpoint is used " 42284007Smmusante "in a non-global zone")); 42294007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 42304007Smmusante goto error; 42314007Smmusante } 42324007Smmusante 42334007Smmusante if ((ret = changelist_prefix(cl)) != 0) 42344007Smmusante goto error; 4235789Sahrens } 4236789Sahrens 42372676Seschrock if (ZFS_IS_VOLUME(zhp)) 4238789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4239789Sahrens else 4240789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4241789Sahrens 42422665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 42432676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 42442665Snd150628 42454007Smmusante zc.zc_cookie = recursive; 42464007Smmusante 4247*4543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 42484007Smmusante /* 42494007Smmusante * if it was recursive, the one that actually failed will 42504007Smmusante * be in zc.zc_name 42514007Smmusante */ 42524007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 42534007Smmusante "cannot rename to '%s'"), zc.zc_name); 42544007Smmusante 42554007Smmusante if (recursive && errno == EEXIST) { 42564007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42574007Smmusante "a child dataset already has a snapshot " 42584007Smmusante "with the new name")); 42594007Smmusante (void) zfs_error(hdl, EZFS_CROSSTARGET, errbuf); 42604007Smmusante } else { 42614007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 42624007Smmusante } 4263789Sahrens 4264789Sahrens /* 4265789Sahrens * On failure, we still want to remount any filesystems that 4266789Sahrens * were previously mounted, so we don't alter the system state. 4267789Sahrens */ 42684007Smmusante if (recursive) { 42694007Smmusante struct createdata cd; 42704007Smmusante 42714007Smmusante /* only create links for datasets that had existed */ 42724007Smmusante cd.cd_snapname = delim + 1; 42734007Smmusante cd.cd_ifexists = B_TRUE; 42744007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 42754007Smmusante &cd); 42764007Smmusante } else { 42774007Smmusante (void) changelist_postfix(cl); 42784007Smmusante } 4279789Sahrens } else { 42804007Smmusante if (recursive) { 42814007Smmusante struct createdata cd; 42824007Smmusante 42834007Smmusante /* only create links for datasets that had existed */ 42844007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 42854007Smmusante cd.cd_ifexists = B_TRUE; 42864007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 42874007Smmusante &cd); 42884007Smmusante } else { 42894007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 42904007Smmusante ret = changelist_postfix(cl); 42914007Smmusante } 4292789Sahrens } 4293789Sahrens 4294789Sahrens error: 42954007Smmusante if (parentname) { 42964007Smmusante free(parentname); 42974007Smmusante } 42984007Smmusante if (zhrp) { 42994007Smmusante zfs_close(zhrp); 43004007Smmusante } 43014007Smmusante if (cl) { 43024007Smmusante changelist_free(cl); 43034007Smmusante } 4304789Sahrens return (ret); 4305789Sahrens } 4306789Sahrens 4307789Sahrens /* 4308789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4309789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 4310789Sahrens */ 4311789Sahrens int 43122082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4313789Sahrens { 43144007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 43154007Smmusante } 43164007Smmusante 43174007Smmusante static int 43184007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 43194007Smmusante { 4320789Sahrens zfs_cmd_t zc = { 0 }; 43212082Seschrock di_devlink_handle_t dhdl; 4322*4543Smarks priv_set_t *priv_effective; 4323*4543Smarks int privileged; 4324789Sahrens 4325789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4326789Sahrens 4327789Sahrens /* 4328789Sahrens * Issue the appropriate ioctl. 4329789Sahrens */ 43302082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4331789Sahrens switch (errno) { 4332789Sahrens case EEXIST: 4333789Sahrens /* 4334789Sahrens * Silently ignore the case where the link already 4335789Sahrens * exists. This allows 'zfs volinit' to be run multiple 4336789Sahrens * times without errors. 4337789Sahrens */ 4338789Sahrens return (0); 4339789Sahrens 43404007Smmusante case ENOENT: 43414007Smmusante /* 43424007Smmusante * Dataset does not exist in the kernel. If we 43434007Smmusante * don't care (see zfs_rename), then ignore the 43444007Smmusante * error quietly. 43454007Smmusante */ 43464007Smmusante if (ifexists) { 43474007Smmusante return (0); 43484007Smmusante } 43494007Smmusante 43504007Smmusante /* FALLTHROUGH */ 43514007Smmusante 4352789Sahrens default: 43533237Slling return (zfs_standard_error_fmt(hdl, errno, 43542082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 43552082Seschrock "for '%s'"), dataset)); 4356789Sahrens } 4357789Sahrens } 4358789Sahrens 4359789Sahrens /* 4360*4543Smarks * If privileged call devfsadm and wait for the links to 4361*4543Smarks * magically appear. 4362*4543Smarks * Otherwise, print out an informational message. 4363789Sahrens */ 4364*4543Smarks 4365*4543Smarks priv_effective = priv_allocset(); 4366*4543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 4367*4543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 4368*4543Smarks priv_freeset(priv_effective); 4369*4543Smarks 4370*4543Smarks if (privileged) { 4371*4543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 4372*4543Smarks DI_MAKE_LINK)) == NULL) { 4373*4543Smarks zfs_error_aux(hdl, strerror(errno)); 4374*4543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 4375*4543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 4376*4543Smarks "for '%s'"), dataset); 4377*4543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 4378*4543Smarks return (-1); 4379*4543Smarks } else { 4380*4543Smarks (void) di_devlink_fini(&dhdl); 4381*4543Smarks } 4382789Sahrens } else { 4383*4543Smarks char pathname[MAXPATHLEN]; 4384*4543Smarks struct stat64 statbuf; 4385*4543Smarks int i; 4386*4543Smarks 4387*4543Smarks #define MAX_WAIT 10 4388*4543Smarks 4389*4543Smarks /* 4390*4543Smarks * This is the poor mans way of waiting for the link 4391*4543Smarks * to show up. If after 10 seconds we still don't 4392*4543Smarks * have it, then print out a message. 4393*4543Smarks */ 4394*4543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 4395*4543Smarks dataset); 4396*4543Smarks 4397*4543Smarks for (i = 0; i != MAX_WAIT; i++) { 4398*4543Smarks if (stat64(pathname, &statbuf) == 0) 4399*4543Smarks break; 4400*4543Smarks (void) sleep(1); 4401*4543Smarks } 4402*4543Smarks if (i == MAX_WAIT) 4403*4543Smarks (void) printf(gettext("%s may not be immediately " 4404*4543Smarks "available\n"), pathname); 4405789Sahrens } 4406789Sahrens 4407789Sahrens return (0); 4408789Sahrens } 4409789Sahrens 4410789Sahrens /* 4411789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4412789Sahrens */ 4413789Sahrens int 44142082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4415789Sahrens { 4416789Sahrens zfs_cmd_t zc = { 0 }; 4417789Sahrens 4418789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4419789Sahrens 44202082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4421789Sahrens switch (errno) { 4422789Sahrens case ENXIO: 4423789Sahrens /* 4424789Sahrens * Silently ignore the case where the link no longer 4425789Sahrens * exists, so that 'zfs volfini' can be run multiple 4426789Sahrens * times without errors. 4427789Sahrens */ 4428789Sahrens return (0); 4429789Sahrens 4430789Sahrens default: 44313237Slling return (zfs_standard_error_fmt(hdl, errno, 44322082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 44332082Seschrock "links for '%s'"), dataset)); 4434789Sahrens } 4435789Sahrens } 4436789Sahrens 4437789Sahrens return (0); 4438789Sahrens } 44392676Seschrock 44402676Seschrock nvlist_t * 44412676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 44422676Seschrock { 44432676Seschrock return (zhp->zfs_user_props); 44442676Seschrock } 44452676Seschrock 44462676Seschrock /* 44474451Seschrock * Given a comma-separated list of properties, construct a property list 44482676Seschrock * containing both user-defined and native properties. This function will 44492676Seschrock * return a NULL list if 'all' is specified, which can later be expanded on a 44502676Seschrock * per-dataset basis by zfs_expand_proplist(). 44512676Seschrock */ 44522676Seschrock int 44533912Slling zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields, 44543912Slling zfs_proplist_t **listp, zfs_type_t type) 44552676Seschrock { 44562676Seschrock size_t len; 44572676Seschrock char *s, *p; 44582676Seschrock char c; 44592676Seschrock zfs_prop_t prop; 44602676Seschrock zfs_proplist_t *entry; 44612676Seschrock zfs_proplist_t **last; 44622676Seschrock 44632676Seschrock *listp = NULL; 44642676Seschrock last = listp; 44652676Seschrock 44662676Seschrock /* 44672676Seschrock * If 'all' is specified, return a NULL list. 44682676Seschrock */ 44692676Seschrock if (strcmp(fields, "all") == 0) 44702676Seschrock return (0); 44712676Seschrock 44722676Seschrock /* 44732676Seschrock * If no fields were specified, return an error. 44742676Seschrock */ 44752676Seschrock if (fields[0] == '\0') { 44762676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44772676Seschrock "no properties specified")); 44782676Seschrock return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 44792676Seschrock "bad property list"))); 44802676Seschrock } 44812676Seschrock 44822676Seschrock /* 44832676Seschrock * It would be nice to use getsubopt() here, but the inclusion of column 44842676Seschrock * aliases makes this more effort than it's worth. 44852676Seschrock */ 44862676Seschrock s = fields; 44872676Seschrock while (*s != '\0') { 44882676Seschrock if ((p = strchr(s, ',')) == NULL) { 44892676Seschrock len = strlen(s); 44902676Seschrock p = s + len; 44912676Seschrock } else { 44922676Seschrock len = p - s; 44932676Seschrock } 44942676Seschrock 44952676Seschrock /* 44962676Seschrock * Check for empty options. 44972676Seschrock */ 44982676Seschrock if (len == 0) { 44992676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45002676Seschrock "empty property name")); 45012676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 45022676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 45032676Seschrock } 45042676Seschrock 45052676Seschrock /* 45062676Seschrock * Check all regular property names. 45072676Seschrock */ 45082676Seschrock c = s[len]; 45092676Seschrock s[len] = '\0'; 45104451Seschrock prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) : 45114451Seschrock zfs_name_to_prop(s); 45123912Slling 45133912Slling if (prop != ZFS_PROP_INVAL && 45143912Slling !zfs_prop_valid_for_type(prop, type)) 45153912Slling prop = ZFS_PROP_INVAL; 45162676Seschrock 45172676Seschrock /* 45183912Slling * When no property table entry can be found, return failure if 45193912Slling * this is a pool property or if this isn't a user-defined 45203912Slling * dataset property, 45212676Seschrock */ 45223912Slling if (prop == ZFS_PROP_INVAL && 45233912Slling (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) { 45242676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45252676Seschrock "invalid property '%s'"), s); 45262676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 45272676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 45282676Seschrock } 45292676Seschrock 45302676Seschrock if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL) 45312676Seschrock return (-1); 45322676Seschrock 45332676Seschrock entry->pl_prop = prop; 45342676Seschrock if (prop == ZFS_PROP_INVAL) { 45352676Seschrock if ((entry->pl_user_prop = 45362676Seschrock zfs_strdup(hdl, s)) == NULL) { 45372676Seschrock free(entry); 45382676Seschrock return (-1); 45392676Seschrock } 45402676Seschrock entry->pl_width = strlen(s); 45412676Seschrock } else { 45422676Seschrock entry->pl_width = zfs_prop_width(prop, 45432676Seschrock &entry->pl_fixed); 45442676Seschrock } 45452676Seschrock 45462676Seschrock *last = entry; 45472676Seschrock last = &entry->pl_next; 45482676Seschrock 45492676Seschrock s = p; 45502676Seschrock if (c == ',') 45512676Seschrock s++; 45522676Seschrock } 45532676Seschrock 45542676Seschrock return (0); 45552676Seschrock } 45562676Seschrock 45573912Slling int 45583912Slling zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp) 45593912Slling { 45603912Slling return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY)); 45613912Slling } 45623912Slling 45632676Seschrock void 45642676Seschrock zfs_free_proplist(zfs_proplist_t *pl) 45652676Seschrock { 45662676Seschrock zfs_proplist_t *next; 45672676Seschrock 45682676Seschrock while (pl != NULL) { 45692676Seschrock next = pl->pl_next; 45702676Seschrock free(pl->pl_user_prop); 45712676Seschrock free(pl); 45722676Seschrock pl = next; 45732676Seschrock } 45742676Seschrock } 45752676Seschrock 45763654Sgw25295 typedef struct expand_data { 45773654Sgw25295 zfs_proplist_t **last; 45783654Sgw25295 libzfs_handle_t *hdl; 45793654Sgw25295 } expand_data_t; 45803654Sgw25295 45813654Sgw25295 static zfs_prop_t 45823654Sgw25295 zfs_expand_proplist_cb(zfs_prop_t prop, void *cb) 45833654Sgw25295 { 45843654Sgw25295 zfs_proplist_t *entry; 45853654Sgw25295 expand_data_t *edp = cb; 45863654Sgw25295 45873654Sgw25295 if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL) 45883654Sgw25295 return (ZFS_PROP_INVAL); 45893654Sgw25295 45903654Sgw25295 entry->pl_prop = prop; 45913654Sgw25295 entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed); 45923654Sgw25295 entry->pl_all = B_TRUE; 45933654Sgw25295 45943654Sgw25295 *(edp->last) = entry; 45953654Sgw25295 edp->last = &entry->pl_next; 45963654Sgw25295 45973654Sgw25295 return (ZFS_PROP_CONT); 45983654Sgw25295 } 45993654Sgw25295 46002676Seschrock int 46013912Slling zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp, 46023912Slling zfs_type_t type) 46032676Seschrock { 46042676Seschrock zfs_proplist_t *entry; 46053912Slling zfs_proplist_t **last; 46063654Sgw25295 expand_data_t exp; 46072676Seschrock 46082676Seschrock if (*plp == NULL) { 46092676Seschrock /* 46102676Seschrock * If this is the very first time we've been called for an 'all' 46112676Seschrock * specification, expand the list to include all native 46122676Seschrock * properties. 46132676Seschrock */ 46142676Seschrock last = plp; 46153654Sgw25295 46163654Sgw25295 exp.last = last; 46173654Sgw25295 exp.hdl = hdl; 46183654Sgw25295 46193912Slling if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type, 46203654Sgw25295 B_FALSE) == ZFS_PROP_INVAL) 46213654Sgw25295 return (-1); 46222676Seschrock 46232676Seschrock /* 46242676Seschrock * Add 'name' to the beginning of the list, which is handled 46252676Seschrock * specially. 46262676Seschrock */ 46272676Seschrock if ((entry = zfs_alloc(hdl, 46282676Seschrock sizeof (zfs_proplist_t))) == NULL) 46292676Seschrock return (-1); 46302676Seschrock 46312676Seschrock entry->pl_prop = ZFS_PROP_NAME; 46322676Seschrock entry->pl_width = zfs_prop_width(ZFS_PROP_NAME, 46332676Seschrock &entry->pl_fixed); 46342676Seschrock entry->pl_all = B_TRUE; 46352676Seschrock entry->pl_next = *plp; 46362676Seschrock *plp = entry; 46372676Seschrock } 46383912Slling return (0); 46393912Slling } 46403912Slling 46413912Slling /* 46423912Slling * This function is used by 'zfs list' to determine the exact set of columns to 46433912Slling * display, and their maximum widths. This does two main things: 46443912Slling * 46453912Slling * - If this is a list of all properties, then expand the list to include 46463912Slling * all native properties, and set a flag so that for each dataset we look 46473912Slling * for new unique user properties and add them to the list. 46483912Slling * 46493912Slling * - For non fixed-width properties, keep track of the maximum width seen 46503912Slling * so that we can size the column appropriately. 46513912Slling */ 46523912Slling int 46533912Slling zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp) 46543912Slling { 46553912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 46563912Slling zfs_proplist_t *entry; 46573912Slling zfs_proplist_t **last, **start; 46583912Slling nvlist_t *userprops, *propval; 46593912Slling nvpair_t *elem; 46603912Slling char *strval; 46613912Slling char buf[ZFS_MAXPROPLEN]; 46623912Slling 46633912Slling if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0) 46643912Slling return (-1); 46652676Seschrock 46662676Seschrock userprops = zfs_get_user_props(zhp); 46672676Seschrock 46682676Seschrock entry = *plp; 46692676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 46702676Seschrock /* 46712676Seschrock * Go through and add any user properties as necessary. We 46722676Seschrock * start by incrementing our list pointer to the first 46732676Seschrock * non-native property. 46742676Seschrock */ 46752676Seschrock start = plp; 46762676Seschrock while (*start != NULL) { 46772676Seschrock if ((*start)->pl_prop == ZFS_PROP_INVAL) 46782676Seschrock break; 46792676Seschrock start = &(*start)->pl_next; 46802676Seschrock } 46812676Seschrock 46822676Seschrock elem = NULL; 46832676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 46842676Seschrock /* 46852676Seschrock * See if we've already found this property in our list. 46862676Seschrock */ 46872676Seschrock for (last = start; *last != NULL; 46882676Seschrock last = &(*last)->pl_next) { 46892676Seschrock if (strcmp((*last)->pl_user_prop, 46902676Seschrock nvpair_name(elem)) == 0) 46912676Seschrock break; 46922676Seschrock } 46932676Seschrock 46942676Seschrock if (*last == NULL) { 46952676Seschrock if ((entry = zfs_alloc(hdl, 46962676Seschrock sizeof (zfs_proplist_t))) == NULL || 46972676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 46982676Seschrock nvpair_name(elem)))) == NULL) { 46992676Seschrock free(entry); 47002676Seschrock return (-1); 47012676Seschrock } 47022676Seschrock 47032676Seschrock entry->pl_prop = ZFS_PROP_INVAL; 47042676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 47052676Seschrock entry->pl_all = B_TRUE; 47062676Seschrock *last = entry; 47072676Seschrock } 47082676Seschrock } 47092676Seschrock } 47102676Seschrock 47112676Seschrock /* 47122676Seschrock * Now go through and check the width of any non-fixed columns 47132676Seschrock */ 47142676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 47152676Seschrock if (entry->pl_fixed) 47162676Seschrock continue; 47172676Seschrock 47182676Seschrock if (entry->pl_prop != ZFS_PROP_INVAL) { 47192676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 47202676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 47212676Seschrock if (strlen(buf) > entry->pl_width) 47222676Seschrock entry->pl_width = strlen(buf); 47232676Seschrock } 47242676Seschrock } else if (nvlist_lookup_nvlist(userprops, 47252676Seschrock entry->pl_user_prop, &propval) == 0) { 47262676Seschrock verify(nvlist_lookup_string(propval, 47272676Seschrock ZFS_PROP_VALUE, &strval) == 0); 47282676Seschrock if (strlen(strval) > entry->pl_width) 47292676Seschrock entry->pl_width = strlen(strval); 47302676Seschrock } 47312676Seschrock } 47322676Seschrock 47332676Seschrock return (0); 47342676Seschrock } 4735*4543Smarks 4736*4543Smarks int 4737*4543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 4738*4543Smarks { 4739*4543Smarks zfs_cmd_t zc = { 0 }; 4740*4543Smarks nvlist_t *nvp; 4741*4543Smarks size_t sz; 4742*4543Smarks gid_t gid; 4743*4543Smarks uid_t uid; 4744*4543Smarks const gid_t *groups; 4745*4543Smarks int group_cnt; 4746*4543Smarks int error; 4747*4543Smarks 4748*4543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 4749*4543Smarks return (no_memory(hdl)); 4750*4543Smarks 4751*4543Smarks uid = ucred_geteuid(cred); 4752*4543Smarks gid = ucred_getegid(cred); 4753*4543Smarks group_cnt = ucred_getgroups(cred, &groups); 4754*4543Smarks 4755*4543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 4756*4543Smarks return (1); 4757*4543Smarks 4758*4543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 4759*4543Smarks nvlist_free(nvp); 4760*4543Smarks return (1); 4761*4543Smarks } 4762*4543Smarks 4763*4543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 4764*4543Smarks nvlist_free(nvp); 4765*4543Smarks return (1); 4766*4543Smarks } 4767*4543Smarks 4768*4543Smarks if (nvlist_add_uint32_array(nvp, 4769*4543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 4770*4543Smarks nvlist_free(nvp); 4771*4543Smarks return (1); 4772*4543Smarks } 4773*4543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4774*4543Smarks 4775*4543Smarks if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz)) 4776*4543Smarks return (-1); 4777*4543Smarks 4778*4543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 4779*4543Smarks nvlist_free(nvp); 4780*4543Smarks return (error); 4781*4543Smarks } 4782*4543Smarks 4783*4543Smarks int 4784*4543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4785*4543Smarks void *export, void *sharetab, int sharemax, boolean_t share_on) 4786*4543Smarks { 4787*4543Smarks zfs_cmd_t zc = { 0 }; 4788*4543Smarks int error; 4789*4543Smarks 4790*4543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4791*4543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4792*4543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 4793*4543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4794*4543Smarks zc.zc_share.z_sharetype = share_on; 4795*4543Smarks zc.zc_share.z_sharemax = sharemax; 4796*4543Smarks 4797*4543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4798*4543Smarks return (error); 4799*4543Smarks } 4800