1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 213126Sahl 22789Sahrens /* 233363Sgw25295 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 29789Sahrens #include <assert.h> 30789Sahrens #include <ctype.h> 31789Sahrens #include <errno.h> 32789Sahrens #include <libdevinfo.h> 33789Sahrens #include <libintl.h> 34789Sahrens #include <math.h> 35789Sahrens #include <stdio.h> 36789Sahrens #include <stdlib.h> 37789Sahrens #include <strings.h> 38789Sahrens #include <unistd.h> 39789Sahrens #include <zone.h> 402082Seschrock #include <fcntl.h> 41789Sahrens #include <sys/mntent.h> 42789Sahrens #include <sys/mnttab.h> 431294Slling #include <sys/mount.h> 444543Smarks #include <sys/avl.h> 454543Smarks #include <priv.h> 464543Smarks #include <pwd.h> 474543Smarks #include <grp.h> 484543Smarks #include <stddef.h> 494543Smarks #include <ucred.h> 50789Sahrens 51789Sahrens #include <sys/spa.h> 52789Sahrens #include <sys/zio.h> 532676Seschrock #include <sys/zap.h> 54789Sahrens #include <libzfs.h> 55789Sahrens 56789Sahrens #include "zfs_namecheck.h" 57789Sahrens #include "zfs_prop.h" 58789Sahrens #include "libzfs_impl.h" 594543Smarks #include "zfs_deleg.h" 60789Sahrens 614490Svb160487 static int create_parents(libzfs_handle_t *, char *, int); 624007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 634007Smmusante 64789Sahrens /* 65789Sahrens * Given a single type (not a mask of types), return the type in a human 66789Sahrens * readable form. 67789Sahrens */ 68789Sahrens const char * 69789Sahrens zfs_type_to_name(zfs_type_t type) 70789Sahrens { 71789Sahrens switch (type) { 72789Sahrens case ZFS_TYPE_FILESYSTEM: 73789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 74789Sahrens case ZFS_TYPE_SNAPSHOT: 75789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 76789Sahrens case ZFS_TYPE_VOLUME: 77789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 78789Sahrens } 79789Sahrens 80789Sahrens return (NULL); 81789Sahrens } 82789Sahrens 83789Sahrens /* 84789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 85789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 86789Sahrens * We guess what the type would have been based on the path and the mask of 87789Sahrens * acceptable types. 88789Sahrens */ 89789Sahrens static const char * 90789Sahrens path_to_str(const char *path, int types) 91789Sahrens { 92789Sahrens /* 93789Sahrens * When given a single type, always report the exact type. 94789Sahrens */ 95789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 96789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 97789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 98789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 99789Sahrens if (types == ZFS_TYPE_VOLUME) 100789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 101789Sahrens 102789Sahrens /* 103789Sahrens * The user is requesting more than one type of dataset. If this is the 104789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 105789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 106789Sahrens * snapshot attribute and try again. 107789Sahrens */ 108789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 109789Sahrens if (strchr(path, '@') != NULL) 110789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 111789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 112789Sahrens } 113789Sahrens 114789Sahrens 115789Sahrens /* 116789Sahrens * The user has requested either filesystems or volumes. 117789Sahrens * We have no way of knowing a priori what type this would be, so always 118789Sahrens * report it as "filesystem" or "volume", our two primitive types. 119789Sahrens */ 120789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 121789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 122789Sahrens 123789Sahrens assert(types & ZFS_TYPE_VOLUME); 124789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 125789Sahrens } 126789Sahrens 127789Sahrens /* 128789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 129789Sahrens * provide a more meaningful error message. We place a more useful message in 130789Sahrens * 'buf' detailing exactly why the name was not valid. 131789Sahrens */ 132789Sahrens static int 1332082Seschrock zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type) 134789Sahrens { 135789Sahrens namecheck_err_t why; 136789Sahrens char what; 137789Sahrens 138789Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 1392082Seschrock if (hdl != NULL) { 140789Sahrens switch (why) { 1411003Slling case NAME_ERR_TOOLONG: 1422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1432082Seschrock "name is too long")); 1441003Slling break; 1451003Slling 146789Sahrens case NAME_ERR_LEADING_SLASH: 1472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1482082Seschrock "leading slash in name")); 149789Sahrens break; 150789Sahrens 151789Sahrens case NAME_ERR_EMPTY_COMPONENT: 1522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1532082Seschrock "empty component in name")); 154789Sahrens break; 155789Sahrens 156789Sahrens case NAME_ERR_TRAILING_SLASH: 1572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1582082Seschrock "trailing slash in name")); 159789Sahrens break; 160789Sahrens 161789Sahrens case NAME_ERR_INVALCHAR: 1622082Seschrock zfs_error_aux(hdl, 163789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 1642082Seschrock "'%c' in name"), what); 165789Sahrens break; 166789Sahrens 167789Sahrens case NAME_ERR_MULTIPLE_AT: 1682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1692082Seschrock "multiple '@' delimiters in name")); 170789Sahrens break; 1712856Snd150628 1722856Snd150628 case NAME_ERR_NOLETTER: 1732856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1742856Snd150628 "pool doesn't begin with a letter")); 1752856Snd150628 break; 1762856Snd150628 1772856Snd150628 case NAME_ERR_RESERVED: 1782856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1792856Snd150628 "name is reserved")); 1802856Snd150628 break; 1812856Snd150628 1822856Snd150628 case NAME_ERR_DISKLIKE: 1832856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1842856Snd150628 "reserved disk name")); 1852856Snd150628 break; 186789Sahrens } 187789Sahrens } 188789Sahrens 189789Sahrens return (0); 190789Sahrens } 191789Sahrens 192789Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 1932082Seschrock if (hdl != NULL) 1942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1952082Seschrock "snapshot delimiter '@' in filesystem name")); 196789Sahrens return (0); 197789Sahrens } 198789Sahrens 1992199Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 2002199Sahrens if (hdl != NULL) 2012199Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2023413Smmusante "missing '@' delimiter in snapshot name")); 2032199Sahrens return (0); 2042199Sahrens } 2052199Sahrens 2062082Seschrock return (-1); 207789Sahrens } 208789Sahrens 209789Sahrens int 210789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 211789Sahrens { 2122082Seschrock return (zfs_validate_name(NULL, name, type)); 213789Sahrens } 214789Sahrens 215789Sahrens /* 2162676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2172676Seschrock * properties into a separate nvlist. 2182676Seschrock */ 2194217Seschrock static nvlist_t * 2204217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2212676Seschrock { 2222676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2232676Seschrock nvpair_t *elem; 2242676Seschrock nvlist_t *propval; 2254217Seschrock nvlist_t *nvl; 2264217Seschrock 2274217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2284217Seschrock (void) no_memory(hdl); 2294217Seschrock return (NULL); 2304217Seschrock } 2312676Seschrock 2322676Seschrock elem = NULL; 2334217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2342676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2352676Seschrock continue; 2362676Seschrock 2372676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2384217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2394217Seschrock nvlist_free(nvl); 2404217Seschrock (void) no_memory(hdl); 2414217Seschrock return (NULL); 2424217Seschrock } 2432676Seschrock } 2442676Seschrock 2454217Seschrock return (nvl); 2462676Seschrock } 2472676Seschrock 2482676Seschrock /* 249789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 250789Sahrens */ 251789Sahrens static int 252789Sahrens get_stats(zfs_handle_t *zhp) 253789Sahrens { 254789Sahrens zfs_cmd_t zc = { 0 }; 2552676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2564217Seschrock nvlist_t *allprops, *userprops; 257789Sahrens 258789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 259789Sahrens 2602676Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 2612082Seschrock return (-1); 2621356Seschrock 2632082Seschrock while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 2641356Seschrock if (errno == ENOMEM) { 2652676Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2662676Seschrock zcmd_free_nvlists(&zc); 2672082Seschrock return (-1); 2682676Seschrock } 2691356Seschrock } else { 2702676Seschrock zcmd_free_nvlists(&zc); 2711356Seschrock return (-1); 2721356Seschrock } 2731356Seschrock } 274789Sahrens 2752885Sahrens zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 276789Sahrens 2772676Seschrock (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 2781544Seschrock 2794217Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 2802676Seschrock zcmd_free_nvlists(&zc); 2812082Seschrock return (-1); 2822082Seschrock } 283789Sahrens 2842676Seschrock zcmd_free_nvlists(&zc); 2852676Seschrock 2864217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 2874217Seschrock nvlist_free(allprops); 2882676Seschrock return (-1); 2894217Seschrock } 2904217Seschrock 2914217Seschrock nvlist_free(zhp->zfs_props); 2924217Seschrock nvlist_free(zhp->zfs_user_props); 2934217Seschrock 2944217Seschrock zhp->zfs_props = allprops; 2954217Seschrock zhp->zfs_user_props = userprops; 2962082Seschrock 297789Sahrens return (0); 298789Sahrens } 299789Sahrens 300789Sahrens /* 301789Sahrens * Refresh the properties currently stored in the handle. 302789Sahrens */ 303789Sahrens void 304789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 305789Sahrens { 306789Sahrens (void) get_stats(zhp); 307789Sahrens } 308789Sahrens 309789Sahrens /* 310789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 311789Sahrens * zfs_iter_* to create child handles on the fly. 312789Sahrens */ 313789Sahrens zfs_handle_t * 3142082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path) 315789Sahrens { 3162082Seschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 3174543Smarks char *logstr; 3182082Seschrock 3192082Seschrock if (zhp == NULL) 3202082Seschrock return (NULL); 3212082Seschrock 3222082Seschrock zhp->zfs_hdl = hdl; 323789Sahrens 3244543Smarks /* 3254543Smarks * Preserve history log string. 3264543Smarks * any changes performed here will be 3274543Smarks * logged as an internal event. 3284543Smarks */ 3294543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 3304543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 3311758Sahrens top: 332789Sahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 333789Sahrens 334789Sahrens if (get_stats(zhp) != 0) { 3354543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 336789Sahrens free(zhp); 337789Sahrens return (NULL); 338789Sahrens } 339789Sahrens 3401758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 3411758Sahrens zfs_cmd_t zc = { 0 }; 3421758Sahrens 3431758Sahrens /* 3441758Sahrens * If it is dds_inconsistent, then we've caught it in 3451758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 3461758Sahrens * it is inconsistent from the ZPL's point of view, so 3471758Sahrens * can't be mounted. However, it could also be that we 3481758Sahrens * have crashed in the middle of one of those 3491758Sahrens * operations, in which case we need to get rid of the 3501758Sahrens * inconsistent state. We do that by either rolling 3511758Sahrens * back to the previous snapshot (which will fail if 3521758Sahrens * there is none), or destroying the filesystem. Note 3531758Sahrens * that if we are still in the middle of an active 3541758Sahrens * 'receive' or 'destroy', then the rollback and destroy 3551758Sahrens * will fail with EBUSY and we will drive on as usual. 3561758Sahrens */ 3571758Sahrens 3581758Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3591758Sahrens 3602885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 3612082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 3621758Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3631758Sahrens } else { 3641758Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3651758Sahrens } 3661758Sahrens 3671758Sahrens /* If we can successfully roll it back, reget the stats */ 3682082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 3691758Sahrens goto top; 3701758Sahrens /* 3711758Sahrens * If we can sucessfully destroy it, pretend that it 3721758Sahrens * never existed. 3731758Sahrens */ 3742082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 3754543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 3761758Sahrens free(zhp); 3771758Sahrens errno = ENOENT; 3781758Sahrens return (NULL); 3791758Sahrens } 3801758Sahrens } 3811758Sahrens 382789Sahrens /* 383789Sahrens * We've managed to open the dataset and gather statistics. Determine 384789Sahrens * the high-level type. 385789Sahrens */ 3862885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 3872885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 3882885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 3892885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 3902885Sahrens else 3912885Sahrens abort(); 3922885Sahrens 393789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 394789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 395789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 396789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 397789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 398789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 399789Sahrens else 4002082Seschrock abort(); /* we should never see any other types */ 401789Sahrens 4024543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 403789Sahrens return (zhp); 404789Sahrens } 405789Sahrens 406789Sahrens /* 407789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 408789Sahrens * argument is a mask of acceptable types. The function will print an 409789Sahrens * appropriate error message and return NULL if it can't be opened. 410789Sahrens */ 411789Sahrens zfs_handle_t * 4122082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 413789Sahrens { 414789Sahrens zfs_handle_t *zhp; 4152082Seschrock char errbuf[1024]; 4162082Seschrock 4172082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 4182082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 419789Sahrens 420789Sahrens /* 4212082Seschrock * Validate the name before we even try to open it. 422789Sahrens */ 4232082Seschrock if (!zfs_validate_name(hdl, path, ZFS_TYPE_ANY)) { 4242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4252082Seschrock "invalid dataset name")); 4262082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 427789Sahrens return (NULL); 428789Sahrens } 429789Sahrens 430789Sahrens /* 431789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 432789Sahrens */ 433789Sahrens errno = 0; 4342082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 4353237Slling (void) zfs_standard_error(hdl, errno, errbuf); 436789Sahrens return (NULL); 437789Sahrens } 438789Sahrens 439789Sahrens if (!(types & zhp->zfs_type)) { 4402082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 4412142Seschrock zfs_close(zhp); 442789Sahrens return (NULL); 443789Sahrens } 444789Sahrens 445789Sahrens return (zhp); 446789Sahrens } 447789Sahrens 448789Sahrens /* 449789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 450789Sahrens */ 451789Sahrens void 452789Sahrens zfs_close(zfs_handle_t *zhp) 453789Sahrens { 454789Sahrens if (zhp->zfs_mntopts) 455789Sahrens free(zhp->zfs_mntopts); 4562676Seschrock nvlist_free(zhp->zfs_props); 4572676Seschrock nvlist_free(zhp->zfs_user_props); 458789Sahrens free(zhp); 459789Sahrens } 460789Sahrens 461789Sahrens /* 462789Sahrens * Given a numeric suffix, convert the value into a number of bits that the 463789Sahrens * resulting value must be shifted. 464789Sahrens */ 465789Sahrens static int 4662082Seschrock str2shift(libzfs_handle_t *hdl, const char *buf) 467789Sahrens { 468789Sahrens const char *ends = "BKMGTPEZ"; 469789Sahrens int i; 470789Sahrens 471789Sahrens if (buf[0] == '\0') 472789Sahrens return (0); 473789Sahrens for (i = 0; i < strlen(ends); i++) { 474789Sahrens if (toupper(buf[0]) == ends[i]) 475789Sahrens break; 476789Sahrens } 477789Sahrens if (i == strlen(ends)) { 4782082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4792082Seschrock "invalid numeric suffix '%s'"), buf); 480789Sahrens return (-1); 481789Sahrens } 482789Sahrens 483789Sahrens /* 484789Sahrens * We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't 485789Sahrens * allow 'BB' - that's just weird. 486789Sahrens */ 487789Sahrens if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && 4882082Seschrock toupper(buf[0]) != 'B')) 489789Sahrens return (10*i); 4902082Seschrock 4912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4922082Seschrock "invalid numeric suffix '%s'"), buf); 493789Sahrens return (-1); 494789Sahrens } 495789Sahrens 496789Sahrens /* 497789Sahrens * Convert a string of the form '100G' into a real number. Used when setting 498789Sahrens * properties or creating a volume. 'buf' is used to place an extended error 499789Sahrens * message for the caller to use. 500789Sahrens */ 501*4849Sahrens int 502*4849Sahrens zfs_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 5702676Seschrock /* 5712676Seschrock * The prop_parse_*() functions are designed to allow flexibility in callers 5722676Seschrock * when setting properties. At the DSL layer, all properties are either 64-bit 5732676Seschrock * numbers or strings. We want the user to be able to ignore this fact and 574*4849Sahrens * specify properties as native values (numbers, for example) or as strings (to 5752676Seschrock * simplify command line utilities). This also handles converting index types 5762676Seschrock * (compression, checksum, etc) from strings to their on-disk index. 5772676Seschrock */ 5782676Seschrock static int 5792676Seschrock prop_parse_number(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop, 5802676Seschrock uint64_t *val) 5812676Seschrock { 5822676Seschrock uint64_t ret; 5832676Seschrock boolean_t isnone = B_FALSE; 5842676Seschrock 5852676Seschrock switch (nvpair_type(elem)) { 5862676Seschrock case DATA_TYPE_STRING: 5872676Seschrock { 5882676Seschrock char *value; 5892676Seschrock (void) nvpair_value_string(elem, &value); 5902676Seschrock if (strcmp(value, "none") == 0) { 5912676Seschrock isnone = B_TRUE; 5922676Seschrock ret = 0; 593*4849Sahrens } else if (zfs_nicestrtonum(hdl, value, &ret) != 0) { 5942676Seschrock return (-1); 5952676Seschrock } 5962676Seschrock break; 5972676Seschrock } 5982676Seschrock 5992676Seschrock case DATA_TYPE_UINT64: 6002676Seschrock (void) nvpair_value_uint64(elem, &ret); 6012676Seschrock break; 6022676Seschrock 6032676Seschrock default: 6042676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6052676Seschrock "'%s' must be a number"), 6062676Seschrock nvpair_name(elem)); 6072676Seschrock return (-1); 6082676Seschrock } 6092676Seschrock 6102676Seschrock /* 6112676Seschrock * Quota special: force 'none' and don't allow 0. 6122676Seschrock */ 6132676Seschrock if (ret == 0 && !isnone && prop == ZFS_PROP_QUOTA) { 6142676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6152676Seschrock "use 'none' to disable quota")); 6162676Seschrock return (-1); 6172676Seschrock } 6182676Seschrock 6192676Seschrock *val = ret; 6202676Seschrock return (0); 6212676Seschrock } 6222676Seschrock 6232676Seschrock static int 6242676Seschrock prop_parse_index(libzfs_handle_t *hdl, nvpair_t *elem, zfs_prop_t prop, 6252676Seschrock uint64_t *val) 6262676Seschrock { 6272676Seschrock char *propname = nvpair_name(elem); 6282676Seschrock char *value; 6292676Seschrock 6302676Seschrock if (nvpair_type(elem) != DATA_TYPE_STRING) { 6312676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6322676Seschrock "'%s' must be a string"), propname); 6332676Seschrock return (-1); 6342676Seschrock } 6352676Seschrock 6362676Seschrock (void) nvpair_value_string(elem, &value); 6372676Seschrock 6382676Seschrock if (zfs_prop_string_to_index(prop, value, val) != 0) { 6392676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6402676Seschrock "'%s' must be one of '%s'"), propname, 6412676Seschrock zfs_prop_values(prop)); 6422676Seschrock return (-1); 6432676Seschrock } 6442676Seschrock 6452676Seschrock return (0); 646789Sahrens } 647789Sahrens 648789Sahrens /* 6493912Slling * Check if the bootfs name has the same pool name as it is set to. 6503912Slling * Assuming bootfs is a valid dataset name. 6513912Slling */ 6523912Slling static boolean_t 6533912Slling bootfs_poolname_valid(char *pool, char *bootfs) 6543912Slling { 6553912Slling char ch, *pname; 6563912Slling 6573912Slling /* get the pool name from the bootfs name */ 6583912Slling pname = bootfs; 6593912Slling while (*bootfs && !isspace(*bootfs) && *bootfs != '/') 6603912Slling bootfs++; 6613912Slling 6623912Slling ch = *bootfs; 6633912Slling *bootfs = 0; 6643912Slling 6653912Slling if (strcmp(pool, pname) == 0) { 6663912Slling *bootfs = ch; 6673912Slling return (B_TRUE); 6683912Slling } 6693912Slling 6703912Slling *bootfs = ch; 6713912Slling return (B_FALSE); 6723912Slling } 6733912Slling 6743912Slling /* 6752676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 6762676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 6772676Seschrock * strings. 678789Sahrens */ 6793912Slling nvlist_t * 6803912Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, char *pool_name, 6813912Slling nvlist_t *nvl, uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 682789Sahrens { 6832676Seschrock nvpair_t *elem; 6842676Seschrock const char *propname; 6852676Seschrock zfs_prop_t prop; 6862676Seschrock uint64_t intval; 6872676Seschrock char *strval; 6882676Seschrock nvlist_t *ret; 6893912Slling int isuser; 6902676Seschrock 6912676Seschrock if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 6922676Seschrock (void) no_memory(hdl); 6932676Seschrock return (NULL); 6942676Seschrock } 6952676Seschrock 6962676Seschrock if (type == ZFS_TYPE_SNAPSHOT) { 6972676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6983413Smmusante "snapshot properties cannot be modified")); 6992676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 7002676Seschrock goto error; 701789Sahrens } 702789Sahrens 7032676Seschrock elem = NULL; 7042676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 7052676Seschrock propname = nvpair_name(elem); 7062676Seschrock 7072676Seschrock /* 7082676Seschrock * Make sure this property is valid and applies to this type. 7092676Seschrock */ 7103912Slling if ((prop = zfs_name_to_prop_common(propname, type)) 7113912Slling == ZFS_PROP_INVAL) { 7123912Slling isuser = zfs_prop_user(propname); 7133912Slling if (!isuser || (isuser && (type & ZFS_TYPE_POOL))) { 7142676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7152676Seschrock "invalid property '%s'"), 7162676Seschrock propname); 7172676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7182676Seschrock goto error; 7192676Seschrock } else { 7202676Seschrock /* 7212676Seschrock * If this is a user property, make sure it's a 7222676Seschrock * string, and that it's less than 7232676Seschrock * ZAP_MAXNAMELEN. 7242676Seschrock */ 7252676Seschrock if (nvpair_type(elem) != DATA_TYPE_STRING) { 7262676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7272676Seschrock "'%s' must be a string"), 7282676Seschrock propname); 7292676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7302676Seschrock errbuf); 7312676Seschrock goto error; 7322676Seschrock } 7332676Seschrock 7342676Seschrock if (strlen(nvpair_name(elem)) >= 7352676Seschrock ZAP_MAXNAMELEN) { 7362676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7372676Seschrock "property name '%s' is too long"), 7382676Seschrock propname); 7392676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7402676Seschrock errbuf); 7412676Seschrock goto error; 7422676Seschrock } 7432676Seschrock } 7442676Seschrock 7452676Seschrock (void) nvpair_value_string(elem, &strval); 7462676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 7472676Seschrock (void) no_memory(hdl); 7482676Seschrock goto error; 7492676Seschrock } 7502676Seschrock continue; 751789Sahrens } 7522676Seschrock 7532676Seschrock /* 7542676Seschrock * Normalize the name, to get rid of shorthand abbrevations. 7552676Seschrock */ 7562676Seschrock propname = zfs_prop_to_name(prop); 7572676Seschrock 7582676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 7592676Seschrock zfs_error_aux(hdl, 7602676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 7612676Seschrock "apply to datasets of this type"), propname); 7622676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 7632676Seschrock goto error; 7642676Seschrock } 7652676Seschrock 7662676Seschrock if (zfs_prop_readonly(prop) && 7672676Seschrock (prop != ZFS_PROP_VOLBLOCKSIZE || zhp != NULL)) { 7682676Seschrock zfs_error_aux(hdl, 7692676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 7702676Seschrock propname); 7712676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 7722676Seschrock goto error; 7732676Seschrock } 7742676Seschrock 7752676Seschrock /* 7762676Seschrock * Convert any properties to the internal DSL value types. 7772676Seschrock */ 7782676Seschrock strval = NULL; 7792676Seschrock switch (zfs_prop_get_type(prop)) { 7804787Sahrens case PROP_TYPE_STRING: 7812676Seschrock if (nvpair_type(elem) != DATA_TYPE_STRING) { 7822082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7832676Seschrock "'%s' must be a string"), 7842676Seschrock propname); 7852676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7862676Seschrock goto error; 787789Sahrens } 7882676Seschrock (void) nvpair_value_string(elem, &strval); 7892676Seschrock if (strlen(strval) >= ZFS_MAXPROPLEN) { 7902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7912676Seschrock "'%s' is too long"), propname); 7922676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7932676Seschrock goto error; 7942676Seschrock } 7952676Seschrock break; 7962676Seschrock 7974787Sahrens case PROP_TYPE_NUMBER: 7982676Seschrock if (prop_parse_number(hdl, elem, prop, &intval) != 0) { 7992676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8002676Seschrock goto error; 8012676Seschrock } 8022676Seschrock break; 8032676Seschrock 8044787Sahrens case PROP_TYPE_INDEX: 8052676Seschrock if (prop_parse_index(hdl, elem, prop, &intval) != 0) { 8062676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8072676Seschrock goto error; 808789Sahrens } 809789Sahrens break; 810789Sahrens 8112676Seschrock default: 8122676Seschrock abort(); 8132676Seschrock } 8142676Seschrock 8152676Seschrock /* 8162676Seschrock * Add the result to our return set of properties. 8172676Seschrock */ 8182676Seschrock if (strval) { 8192676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 8202676Seschrock (void) no_memory(hdl); 8212676Seschrock goto error; 822789Sahrens } 8232676Seschrock } else if (nvlist_add_uint64(ret, propname, intval) != 0) { 8242676Seschrock (void) no_memory(hdl); 8252676Seschrock goto error; 8262676Seschrock } 8272676Seschrock 8282676Seschrock /* 8292676Seschrock * Perform some additional checks for specific properties. 8302676Seschrock */ 8312676Seschrock switch (prop) { 8324577Sahrens case ZFS_PROP_VERSION: 8334577Sahrens { 8344577Sahrens int version; 8354577Sahrens 8364577Sahrens if (zhp == NULL) 8374577Sahrens break; 8384577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 8394577Sahrens if (intval < version) { 8404577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8414577Sahrens "Can not downgrade; already at version %u"), 8424577Sahrens version); 8434577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8444577Sahrens goto error; 8454577Sahrens } 8464577Sahrens break; 8474577Sahrens } 8484577Sahrens 8492676Seschrock case ZFS_PROP_RECORDSIZE: 8502676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 8512676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 8522676Seschrock if (intval < SPA_MINBLOCKSIZE || 8532676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 8542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8552676Seschrock "'%s' must be power of 2 from %u " 8562676Seschrock "to %uk"), propname, 8572676Seschrock (uint_t)SPA_MINBLOCKSIZE, 8582676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 8592676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8602676Seschrock goto error; 861789Sahrens } 862789Sahrens break; 863789Sahrens 8643126Sahl case ZFS_PROP_SHAREISCSI: 8653126Sahl if (strcmp(strval, "off") != 0 && 8663126Sahl strcmp(strval, "on") != 0 && 8673126Sahl strcmp(strval, "type=disk") != 0) { 8683126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8693126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 8703126Sahl propname); 8713126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8723126Sahl goto error; 8733126Sahl } 8743126Sahl 8753126Sahl break; 8763126Sahl 8772676Seschrock case ZFS_PROP_MOUNTPOINT: 8784778Srm160521 { 8794778Srm160521 namecheck_err_t why; 8804778Srm160521 8812676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 8822676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 8832676Seschrock break; 8842676Seschrock 8854778Srm160521 if (mountpoint_namecheck(strval, &why)) { 8864778Srm160521 switch (why) { 8874778Srm160521 case NAME_ERR_LEADING_SLASH: 8884778Srm160521 zfs_error_aux(hdl, 8894778Srm160521 dgettext(TEXT_DOMAIN, 8904778Srm160521 "'%s' must be an absolute path, " 8914778Srm160521 "'none', or 'legacy'"), propname); 8924778Srm160521 break; 8934778Srm160521 case NAME_ERR_TOOLONG: 8944778Srm160521 zfs_error_aux(hdl, 8954778Srm160521 dgettext(TEXT_DOMAIN, 8964778Srm160521 "component of '%s' is too long"), 8974778Srm160521 propname); 8984778Srm160521 break; 8994778Srm160521 } 9002676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9012676Seschrock goto error; 902789Sahrens } 9034778Srm160521 } 9044778Srm160521 9053126Sahl /*FALLTHRU*/ 9063126Sahl 9073126Sahl case ZFS_PROP_SHARENFS: 9083126Sahl /* 9093126Sahl * For the mountpoint and sharenfs properties, check if 9103126Sahl * it can be set in a global/non-global zone based on 9113126Sahl * the zoned property value: 9123126Sahl * 9133126Sahl * global zone non-global zone 9143126Sahl * -------------------------------------------------- 9153126Sahl * zoned=on mountpoint (no) mountpoint (yes) 9163126Sahl * sharenfs (no) sharenfs (no) 9173126Sahl * 9183126Sahl * zoned=off mountpoint (yes) N/A 9193126Sahl * sharenfs (yes) 9203126Sahl */ 9212676Seschrock if (zoned) { 9222676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 9232676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9242676Seschrock "'%s' cannot be set on " 9252676Seschrock "dataset in a non-global zone"), 9262676Seschrock propname); 9272676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9282676Seschrock errbuf); 9292676Seschrock goto error; 9302676Seschrock } else if (prop == ZFS_PROP_SHARENFS) { 9312676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9322676Seschrock "'%s' cannot be set in " 9332676Seschrock "a non-global zone"), propname); 9342676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9352676Seschrock errbuf); 9362676Seschrock goto error; 9372676Seschrock } 9382676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 9392676Seschrock /* 9402676Seschrock * If zoned property is 'off', this must be in 9412676Seschrock * a globle zone. If not, something is wrong. 9422676Seschrock */ 9432676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9442676Seschrock "'%s' cannot be set while dataset " 9452676Seschrock "'zoned' property is set"), propname); 9462676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 9472676Seschrock goto error; 9482676Seschrock } 9493126Sahl 9504180Sdougm /* 9514180Sdougm * At this point, it is legitimate to set the 9524180Sdougm * property. Now we want to make sure that the 9534180Sdougm * property value is valid if it is sharenfs. 9544180Sdougm */ 9554180Sdougm if (prop == ZFS_PROP_SHARENFS && 9564217Seschrock strcmp(strval, "on") != 0 && 9574217Seschrock strcmp(strval, "off") != 0) { 9584180Sdougm 9594180Sdougm /* 9604180Sdougm * Must be an NFS option string so 9614180Sdougm * init the libshare in order to 9624180Sdougm * enable the parser and then parse 9634180Sdougm * the options. We use the control API 9644180Sdougm * since we don't care about the 9654180Sdougm * current configuration and don't 9664180Sdougm * want the overhead of loading it 9674180Sdougm * until we actually do something. 9684180Sdougm */ 9694180Sdougm 9704217Seschrock if (zfs_init_libshare(hdl, 9714217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 9724217Seschrock /* 9734217Seschrock * An error occurred so we can't do 9744217Seschrock * anything 9754217Seschrock */ 9764217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9774217Seschrock "'%s' cannot be set: problem " 9784217Seschrock "in share initialization"), 9794217Seschrock propname); 9804217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 9814217Seschrock errbuf); 9824217Seschrock goto error; 9834217Seschrock } 9844217Seschrock 9854217Seschrock if (zfs_parse_options(strval, "nfs") != SA_OK) { 9864217Seschrock /* 9874217Seschrock * There was an error in parsing so 9884217Seschrock * deal with it by issuing an error 9894217Seschrock * message and leaving after 9904217Seschrock * uninitializing the the libshare 9914217Seschrock * interface. 9924217Seschrock */ 9934217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9944217Seschrock "'%s' cannot be set to invalid " 9954217Seschrock "options"), propname); 9964217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 9974217Seschrock errbuf); 9984217Seschrock zfs_uninit_libshare(hdl); 9994217Seschrock goto error; 10004217Seschrock } 10014180Sdougm zfs_uninit_libshare(hdl); 10024180Sdougm } 10034180Sdougm 10043126Sahl break; 10053912Slling 10064451Seschrock case ZPOOL_PROP_BOOTFS: 10073912Slling /* 10083912Slling * bootfs property value has to be a dataset name and 10093912Slling * the dataset has to be in the same pool as it sets to. 10103912Slling */ 10113912Slling if (strval[0] != '\0' && (!zfs_name_valid(strval, 10123912Slling ZFS_TYPE_FILESYSTEM) || !bootfs_poolname_valid( 10133912Slling pool_name, strval))) { 10143912Slling 10153912Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' " 10163912Slling "is an invalid name"), strval); 10173912Slling (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 10183912Slling goto error; 10193912Slling } 10203912Slling break; 10212676Seschrock } 10222676Seschrock 10232676Seschrock /* 10242676Seschrock * For changes to existing volumes, we have some additional 10252676Seschrock * checks to enforce. 10262676Seschrock */ 10272676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 10282676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 10292676Seschrock ZFS_PROP_VOLSIZE); 10302676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 10312676Seschrock ZFS_PROP_VOLBLOCKSIZE); 10322676Seschrock char buf[64]; 10332676Seschrock 10342676Seschrock switch (prop) { 10352676Seschrock case ZFS_PROP_RESERVATION: 10362676Seschrock if (intval > volsize) { 10372676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10382676Seschrock "'%s' is greater than current " 10392676Seschrock "volume size"), propname); 10402676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10412676Seschrock errbuf); 10422676Seschrock goto error; 10432676Seschrock } 10442676Seschrock break; 10452676Seschrock 10462676Seschrock case ZFS_PROP_VOLSIZE: 10472676Seschrock if (intval % blocksize != 0) { 10482676Seschrock zfs_nicenum(blocksize, buf, 10492676Seschrock sizeof (buf)); 10502676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10512676Seschrock "'%s' must be a multiple of " 10522676Seschrock "volume block size (%s)"), 10532676Seschrock propname, buf); 10542676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10552676Seschrock errbuf); 10562676Seschrock goto error; 10572676Seschrock } 10582676Seschrock 10592676Seschrock if (intval == 0) { 10602676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10612676Seschrock "'%s' cannot be zero"), 10622676Seschrock propname); 10632676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10642676Seschrock errbuf); 10652676Seschrock goto error; 1066789Sahrens } 10673126Sahl break; 1068789Sahrens } 1069789Sahrens } 1070789Sahrens } 1071789Sahrens 10722676Seschrock /* 10732676Seschrock * If this is an existing volume, and someone is setting the volsize, 10742676Seschrock * make sure that it matches the reservation, or add it if necessary. 10752676Seschrock */ 10762676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 10772676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 10782676Seschrock &intval) == 0) { 10792676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 10802676Seschrock ZFS_PROP_VOLSIZE); 10812676Seschrock uint64_t old_reservation = zfs_prop_get_int(zhp, 10822676Seschrock ZFS_PROP_RESERVATION); 10832676Seschrock uint64_t new_reservation; 10842676Seschrock 10852676Seschrock if (old_volsize == old_reservation && 10862676Seschrock nvlist_lookup_uint64(ret, 10872676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 10882676Seschrock &new_reservation) != 0) { 10892676Seschrock if (nvlist_add_uint64(ret, 10902676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 10912676Seschrock intval) != 0) { 10922676Seschrock (void) no_memory(hdl); 10932676Seschrock goto error; 10942676Seschrock } 10952676Seschrock } 10962676Seschrock } 10972676Seschrock 10982676Seschrock return (ret); 10992676Seschrock 11002676Seschrock error: 11012676Seschrock nvlist_free(ret); 11022676Seschrock return (NULL); 1103789Sahrens } 1104789Sahrens 11054543Smarks static int 11064543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 11074543Smarks uint64_t *ret_who) 11084543Smarks { 11094543Smarks struct passwd *pwd; 11104543Smarks struct group *grp; 11114543Smarks uid_t id; 11124543Smarks 11134543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 11144543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 11154543Smarks *ret_who = -1; 11164543Smarks return (0); 11174543Smarks } 11184543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 11194543Smarks return (EZFS_BADWHO); 11204543Smarks 11214543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 11224543Smarks strcmp(who, "everyone") == 0) { 11234543Smarks *ret_who = -1; 11244543Smarks *who_type = ZFS_DELEG_EVERYONE; 11254543Smarks return (0); 11264543Smarks } 11274543Smarks 11284543Smarks pwd = getpwnam(who); 11294543Smarks grp = getgrnam(who); 11304543Smarks 11314543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 11324543Smarks *ret_who = pwd->pw_uid; 11334543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 11344543Smarks *ret_who = grp->gr_gid; 11354543Smarks } else if (pwd) { 11364543Smarks *ret_who = pwd->pw_uid; 11374543Smarks *who_type = ZFS_DELEG_USER; 11384543Smarks } else if (grp) { 11394543Smarks *ret_who = grp->gr_gid; 11404543Smarks *who_type = ZFS_DELEG_GROUP; 11414543Smarks } else { 11424543Smarks char *end; 11434543Smarks 11444543Smarks id = strtol(who, &end, 10); 11454543Smarks if (errno != 0 || *end != '\0') { 11464543Smarks return (EZFS_BADWHO); 11474543Smarks } else { 11484543Smarks *ret_who = id; 11494543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 11504543Smarks *who_type = ZFS_DELEG_USER; 11514543Smarks } 11524543Smarks } 11534543Smarks 11544543Smarks return (0); 11554543Smarks } 11564543Smarks 11574543Smarks static void 11584543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 11594543Smarks { 11604543Smarks if (perms_nvp != NULL) { 11614543Smarks verify(nvlist_add_nvlist(who_nvp, 11624543Smarks name, perms_nvp) == 0); 11634543Smarks } else { 11644543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 11654543Smarks } 11664543Smarks } 11674543Smarks 11684543Smarks static void 11694543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 11704543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 11714543Smarks nvlist_t *sets_nvp) 11724543Smarks { 11734543Smarks boolean_t do_perms, do_sets; 11744543Smarks char name[ZFS_MAX_DELEG_NAME]; 11754543Smarks 11764543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 11774543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 11784543Smarks 11794543Smarks if (!do_perms && !do_sets) 11804543Smarks do_perms = do_sets = B_TRUE; 11814543Smarks 11824543Smarks if (do_perms) { 11834543Smarks zfs_deleg_whokey(name, who_type, inherit, 11844543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 11854543Smarks whostr : (void *)&whoid); 11864543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 11874543Smarks } 11884543Smarks if (do_sets) { 11894543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 11904543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 11914543Smarks whostr : (void *)&whoid); 11924543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 11934543Smarks } 11944543Smarks } 11954543Smarks 11964543Smarks static void 11974543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 11984543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 11994543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 12004543Smarks { 12014543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 12024543Smarks helper(who_type, whoid, whostr, 0, 12034543Smarks who_nvp, perms_nvp, sets_nvp); 12044543Smarks } else { 12054543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 12064543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 12074543Smarks who_nvp, perms_nvp, sets_nvp); 12084543Smarks } 12094543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 12104543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 12114543Smarks who_nvp, perms_nvp, sets_nvp); 12124543Smarks } 12134543Smarks } 12144543Smarks } 12154543Smarks 12164543Smarks /* 12174543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 12184543Smarks * 12194543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 12204543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 12214543Smarks * base attribute named stored in the dsl. 12224543Smarks * Arguments: 12234543Smarks * 12244543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 12254543Smarks * whostr may be null for everyone or create perms. 12264543Smarks * who_type: is the type of entry in whostr. Typically this will be 12274543Smarks * ZFS_DELEG_WHO_UNKNOWN. 12284543Smarks * perms: comman separated list of permissions. May be null if user 12294543Smarks * is requested to remove permissions by who. 12304543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 12314543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 12324543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 12334543Smarks * The output nvp will look something like this. 12344543Smarks * ul$1234 -> {create ; destroy } 12354543Smarks * Ul$1234 -> { @myset } 12364543Smarks * s-$@myset - { snapshot; checksum; compression } 12374543Smarks */ 12384543Smarks int 12394543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 12404543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 12414543Smarks { 12424543Smarks nvlist_t *who_nvp; 12434543Smarks nvlist_t *perms_nvp = NULL; 12444543Smarks nvlist_t *sets_nvp = NULL; 12454543Smarks char errbuf[1024]; 12464787Sahrens char *who_tok, *perm; 12474543Smarks int error; 12484543Smarks 12494543Smarks *nvp = NULL; 12504543Smarks 12514543Smarks if (perms) { 12524543Smarks if ((error = nvlist_alloc(&perms_nvp, 12534543Smarks NV_UNIQUE_NAME, 0)) != 0) { 12544543Smarks return (1); 12554543Smarks } 12564543Smarks if ((error = nvlist_alloc(&sets_nvp, 12574543Smarks NV_UNIQUE_NAME, 0)) != 0) { 12584543Smarks nvlist_free(perms_nvp); 12594543Smarks return (1); 12604543Smarks } 12614543Smarks } 12624543Smarks 12634543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 12644543Smarks if (perms_nvp) 12654543Smarks nvlist_free(perms_nvp); 12664543Smarks if (sets_nvp) 12674543Smarks nvlist_free(sets_nvp); 12684543Smarks return (1); 12694543Smarks } 12704543Smarks 12714543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 12724543Smarks namecheck_err_t why; 12734543Smarks char what; 12744543Smarks 12754543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 12764787Sahrens nvlist_free(who_nvp); 12774787Sahrens if (perms_nvp) 12784787Sahrens nvlist_free(perms_nvp); 12794787Sahrens if (sets_nvp) 12804787Sahrens nvlist_free(sets_nvp); 12814787Sahrens 12824543Smarks switch (why) { 12834543Smarks case NAME_ERR_NO_AT: 12844543Smarks zfs_error_aux(zhp->zfs_hdl, 12854543Smarks dgettext(TEXT_DOMAIN, 12864543Smarks "set definition must begin with an '@' " 12874543Smarks "character")); 12884543Smarks } 12894543Smarks return (zfs_error(zhp->zfs_hdl, 12904543Smarks EZFS_BADPERMSET, whostr)); 12914543Smarks } 12924543Smarks } 12934543Smarks 12944543Smarks /* 12954543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 12964543Smarks * The first nvlist perms_nvp will have normal permissions and the 12974543Smarks * other sets_nvp will have only permssion set names in it. 12984543Smarks */ 12994787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 13004787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 13014787Sahrens 13024787Sahrens if (perm_canonical) { 13034787Sahrens verify(nvlist_add_boolean(perms_nvp, 13044787Sahrens perm_canonical) == 0); 13054787Sahrens } else if (perm[0] == '@') { 13064787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 13074787Sahrens } else { 13084787Sahrens nvlist_free(who_nvp); 13094787Sahrens nvlist_free(perms_nvp); 13104787Sahrens nvlist_free(sets_nvp); 13114787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 13124543Smarks } 13134543Smarks } 13144543Smarks 13154543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 13164543Smarks who_tok = strtok(whostr, ","); 13174543Smarks if (who_tok == NULL) { 13184543Smarks nvlist_free(who_nvp); 13194787Sahrens if (perms_nvp) 13204787Sahrens nvlist_free(perms_nvp); 13214543Smarks if (sets_nvp) 13224543Smarks nvlist_free(sets_nvp); 13234543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13244543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 13254543Smarks whostr); 13264543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 13274543Smarks } 13284543Smarks } 13294543Smarks 13304543Smarks /* 13314543Smarks * Now create the nvlist(s) 13324543Smarks */ 13334543Smarks do { 13344543Smarks uint64_t who_id; 13354543Smarks 13364543Smarks error = zfs_get_perm_who(who_tok, &who_type, 13374543Smarks &who_id); 13384543Smarks if (error) { 13394543Smarks nvlist_free(who_nvp); 13404787Sahrens if (perms_nvp) 13414787Sahrens nvlist_free(perms_nvp); 13424543Smarks if (sets_nvp) 13434543Smarks nvlist_free(sets_nvp); 13444543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13454543Smarks dgettext(TEXT_DOMAIN, 13464543Smarks "Unable to determine uid/gid for " 13474543Smarks "%s "), who_tok); 13484543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 13494543Smarks } 13504543Smarks 13514543Smarks /* 13524543Smarks * add entries for both local and descendent when required 13534543Smarks */ 13544543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 13554543Smarks perms_nvp, sets_nvp, who_type, inherit); 13564543Smarks 13574543Smarks } while (who_tok = strtok(NULL, ",")); 13584543Smarks *nvp = who_nvp; 13594543Smarks return (0); 13604543Smarks } 13614543Smarks 13624543Smarks static int 13634543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 13644543Smarks { 13654543Smarks zfs_cmd_t zc = { 0 }; 13664543Smarks int error; 13674543Smarks size_t sz; 13684543Smarks char errbuf[1024]; 13694543Smarks 13704543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13714543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 13724543Smarks zhp->zfs_name); 13734543Smarks 13744543Smarks if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp, &sz)) 13754543Smarks return (-1); 13764543Smarks 13774543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13784543Smarks zc.zc_perm_action = unset; 13794543Smarks 13804543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 13814543Smarks if (error && errno == ENOTSUP) { 13824543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13834543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 13844543Smarks zcmd_free_nvlists(&zc); 13854543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 13864543Smarks } else if (error) { 13874543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 13884543Smarks } 13894543Smarks zcmd_free_nvlists(&zc); 13904543Smarks 13914543Smarks return (error); 13924543Smarks } 13934543Smarks 13944543Smarks int 13954543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 13964543Smarks { 13974543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 13984543Smarks } 13994543Smarks 14004543Smarks int 14014543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 14024543Smarks { 14034543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 14044543Smarks } 14054543Smarks 14064543Smarks static int 14074543Smarks perm_compare(const void *arg1, const void *arg2) 14084543Smarks { 14094543Smarks const zfs_perm_node_t *node1 = arg1; 14104543Smarks const zfs_perm_node_t *node2 = arg2; 14114543Smarks int ret; 14124543Smarks 14134543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 14144543Smarks 14154543Smarks if (ret > 0) 14164543Smarks return (1); 14174543Smarks if (ret < 0) 14184543Smarks return (-1); 14194543Smarks else 14204543Smarks return (0); 14214543Smarks } 14224543Smarks 14234543Smarks static void 14244543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 14254543Smarks { 14264543Smarks zfs_perm_node_t *permnode; 14274543Smarks void *cookie; 14284543Smarks 14294543Smarks cookie = NULL; 14304543Smarks while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) { 14314543Smarks avl_remove(tree, permnode); 14324543Smarks free(permnode); 14334543Smarks } 14344543Smarks } 14354543Smarks 14364543Smarks static void 14374543Smarks zfs_destroy_tree(avl_tree_t *tree) 14384543Smarks { 14394543Smarks zfs_allow_node_t *allownode; 14404543Smarks void *cookie; 14414543Smarks 14424543Smarks cookie = NULL; 14434543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 14444543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 14454543Smarks zfs_destroy_perm_tree(&allownode->z_local); 14464543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 14474543Smarks avl_remove(tree, allownode); 14484543Smarks free(allownode); 14494543Smarks } 14504543Smarks } 14514543Smarks 14524543Smarks void 14534543Smarks zfs_free_allows(zfs_allow_t *allow) 14544543Smarks { 14554543Smarks zfs_allow_t *allownext; 14564543Smarks zfs_allow_t *freeallow; 14574543Smarks 14584543Smarks allownext = allow; 14594543Smarks while (allownext) { 14604543Smarks zfs_destroy_tree(&allownext->z_sets); 14614543Smarks zfs_destroy_tree(&allownext->z_crperms); 14624543Smarks zfs_destroy_tree(&allownext->z_user); 14634543Smarks zfs_destroy_tree(&allownext->z_group); 14644543Smarks zfs_destroy_tree(&allownext->z_everyone); 14654543Smarks freeallow = allownext; 14664543Smarks allownext = allownext->z_next; 14674543Smarks free(freeallow); 14684543Smarks } 14694543Smarks } 14704543Smarks 14714543Smarks static zfs_allow_t * 14724543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 14734543Smarks { 14744543Smarks zfs_allow_t *ptree; 14754543Smarks 14764543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 14774543Smarks sizeof (zfs_allow_t))) == NULL) { 14784543Smarks return (NULL); 14794543Smarks } 14804543Smarks 14814543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 14824543Smarks avl_create(&ptree->z_sets, 14834543Smarks perm_compare, sizeof (zfs_allow_node_t), 14844543Smarks offsetof(zfs_allow_node_t, z_node)); 14854543Smarks avl_create(&ptree->z_crperms, 14864543Smarks perm_compare, sizeof (zfs_allow_node_t), 14874543Smarks offsetof(zfs_allow_node_t, z_node)); 14884543Smarks avl_create(&ptree->z_user, 14894543Smarks perm_compare, sizeof (zfs_allow_node_t), 14904543Smarks offsetof(zfs_allow_node_t, z_node)); 14914543Smarks avl_create(&ptree->z_group, 14924543Smarks perm_compare, sizeof (zfs_allow_node_t), 14934543Smarks offsetof(zfs_allow_node_t, z_node)); 14944543Smarks avl_create(&ptree->z_everyone, 14954543Smarks perm_compare, sizeof (zfs_allow_node_t), 14964543Smarks offsetof(zfs_allow_node_t, z_node)); 14974543Smarks 14984543Smarks if (prev) 14994543Smarks prev->z_next = ptree; 15004543Smarks ptree->z_next = NULL; 15014543Smarks return (ptree); 15024543Smarks } 15034543Smarks 15044543Smarks /* 15054543Smarks * Add permissions to the appropriate AVL permission tree. 15064543Smarks * The appropriate tree may not be the requested tree. 15074543Smarks * For example if ld indicates a local permission, but 15084543Smarks * same permission also exists as a descendent permission 15094543Smarks * then the permission will be removed from the descendent 15104543Smarks * tree and add the the local+descendent tree. 15114543Smarks */ 15124543Smarks static int 15134543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 15144543Smarks char *perm, char ld) 15154543Smarks { 15164543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 15174543Smarks zfs_perm_node_t *newnode; 15184543Smarks avl_index_t where, where2; 15194543Smarks avl_tree_t *tree, *altree; 15204543Smarks 15214543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 15224543Smarks 15234543Smarks if (ld == ZFS_DELEG_NA) { 15244543Smarks tree = &allownode->z_localdescend; 15254543Smarks altree = &allownode->z_descend; 15264543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 15274543Smarks tree = &allownode->z_local; 15284543Smarks altree = &allownode->z_descend; 15294543Smarks } else { 15304543Smarks tree = &allownode->z_descend; 15314543Smarks altree = &allownode->z_local; 15324543Smarks } 15334543Smarks permnode = avl_find(tree, &pnode, &where); 15344543Smarks permnode2 = avl_find(altree, &pnode, &where2); 15354543Smarks 15364543Smarks if (permnode2) { 15374543Smarks avl_remove(altree, permnode2); 15384543Smarks free(permnode2); 15394543Smarks if (permnode == NULL) { 15404543Smarks tree = &allownode->z_localdescend; 15414543Smarks } 15424543Smarks } 15434543Smarks 15444543Smarks /* 15454543Smarks * Now insert new permission in either requested location 15464543Smarks * local/descendent or into ld when perm will exist in both. 15474543Smarks */ 15484543Smarks if (permnode == NULL) { 15494543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 15504543Smarks sizeof (zfs_perm_node_t))) == NULL) { 15514543Smarks return (-1); 15524543Smarks } 15534543Smarks *newnode = pnode; 15544543Smarks avl_add(tree, newnode); 15554543Smarks } 15564543Smarks return (0); 15574543Smarks } 15584577Sahrens 15594543Smarks /* 15604543Smarks * Uggh, this is going to be a bit complicated. 15614543Smarks * we have an nvlist coming out of the kernel that 15624543Smarks * will indicate where the permission is set and then 15634543Smarks * it will contain allow of the various "who's", and what 15644543Smarks * their permissions are. To further complicate this 15654543Smarks * we will then have to coalesce the local,descendent 15664543Smarks * and local+descendent permissions where appropriate. 15674543Smarks * The kernel only knows about a permission as being local 15684543Smarks * or descendent, but not both. 15694543Smarks * 15704543Smarks * In order to make this easier for zfs_main to deal with 15714543Smarks * a series of AVL trees will be used to maintain 15724543Smarks * all of this, primarily for sorting purposes as well 15734543Smarks * as the ability to quickly locate a specific entry. 15744543Smarks * 15754543Smarks * What we end up with are tree's for sets, create perms, 15764543Smarks * user, groups and everyone. With each of those trees 15774543Smarks * we have subtrees for local, descendent and local+descendent 15784543Smarks * permissions. 15794543Smarks */ 15804543Smarks int 15814543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 15824543Smarks { 15834543Smarks zfs_cmd_t zc = { 0 }; 15844543Smarks int error; 15854543Smarks nvlist_t *nvlist; 15864543Smarks nvlist_t *permnv, *sourcenv; 15874543Smarks nvpair_t *who_pair, *source_pair; 15884543Smarks nvpair_t *perm_pair; 15894543Smarks char errbuf[1024]; 15904543Smarks zfs_allow_t *zallowp, *newallowp; 15914543Smarks char ld; 15924543Smarks char *nvpname; 15934543Smarks uid_t uid; 15944543Smarks gid_t gid; 15954543Smarks avl_tree_t *tree; 15964543Smarks avl_index_t where; 15974543Smarks 15984543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 15994543Smarks 16004543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 16014543Smarks return (-1); 16024543Smarks 16034543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 16044543Smarks if (errno == ENOMEM) { 16054543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 16064543Smarks zcmd_free_nvlists(&zc); 16074543Smarks return (-1); 16084543Smarks } 16094543Smarks } else if (errno == ENOTSUP) { 16104543Smarks zcmd_free_nvlists(&zc); 16114543Smarks (void) snprintf(errbuf, sizeof (errbuf), 16124543Smarks gettext("Pool must be upgraded to use 'allow'")); 16134543Smarks return (zfs_error(zhp->zfs_hdl, 16144543Smarks EZFS_BADVERSION, errbuf)); 16154543Smarks } else { 16164543Smarks zcmd_free_nvlists(&zc); 16174543Smarks return (-1); 16184543Smarks } 16194543Smarks } 16204543Smarks 16214543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 16224543Smarks zcmd_free_nvlists(&zc); 16234543Smarks return (-1); 16244543Smarks } 16254543Smarks 16264543Smarks zcmd_free_nvlists(&zc); 16274543Smarks 16284543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 16294543Smarks 16304543Smarks if (source_pair == NULL) { 16314543Smarks *zfs_perms = NULL; 16324543Smarks return (0); 16334543Smarks } 16344543Smarks 16354543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 16364543Smarks if (*zfs_perms == NULL) { 16374543Smarks return (0); 16384543Smarks } 16394543Smarks 16404543Smarks zallowp = *zfs_perms; 16414543Smarks 16424543Smarks for (;;) { 16434543Smarks struct passwd *pwd; 16444543Smarks struct group *grp; 16454543Smarks zfs_allow_node_t *allownode; 16464543Smarks zfs_allow_node_t findallownode; 16474543Smarks zfs_allow_node_t *newallownode; 16484543Smarks 16494543Smarks (void) strlcpy(zallowp->z_setpoint, 16504543Smarks nvpair_name(source_pair), 16514543Smarks sizeof (zallowp->z_setpoint)); 16524543Smarks 16534543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 16544543Smarks goto abort; 16554543Smarks 16564543Smarks /* 16574543Smarks * Make sure nvlist is composed correctly 16584543Smarks */ 16594543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 16604543Smarks goto abort; 16614543Smarks } 16624543Smarks 16634543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 16644543Smarks if (who_pair == NULL) { 16654543Smarks goto abort; 16664543Smarks } 16674543Smarks 16684543Smarks do { 16694543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 16704543Smarks if (error) { 16714543Smarks goto abort; 16724543Smarks } 16734543Smarks 16744543Smarks /* 16754543Smarks * First build up the key to use 16764543Smarks * for looking up in the various 16774543Smarks * who trees. 16784543Smarks */ 16794543Smarks ld = nvpair_name(who_pair)[1]; 16804543Smarks nvpname = nvpair_name(who_pair); 16814543Smarks switch (nvpair_name(who_pair)[0]) { 16824543Smarks case ZFS_DELEG_USER: 16834543Smarks case ZFS_DELEG_USER_SETS: 16844543Smarks tree = &zallowp->z_user; 16854543Smarks uid = atol(&nvpname[3]); 16864543Smarks pwd = getpwuid(uid); 16874543Smarks (void) snprintf(findallownode.z_key, 16884543Smarks sizeof (findallownode.z_key), "user %s", 16894543Smarks (pwd) ? pwd->pw_name : 16904543Smarks &nvpair_name(who_pair)[3]); 16914543Smarks break; 16924543Smarks case ZFS_DELEG_GROUP: 16934543Smarks case ZFS_DELEG_GROUP_SETS: 16944543Smarks tree = &zallowp->z_group; 16954543Smarks gid = atol(&nvpname[3]); 16964543Smarks grp = getgrgid(gid); 16974543Smarks (void) snprintf(findallownode.z_key, 16984543Smarks sizeof (findallownode.z_key), "group %s", 16994543Smarks (grp) ? grp->gr_name : 17004543Smarks &nvpair_name(who_pair)[3]); 17014543Smarks break; 17024543Smarks case ZFS_DELEG_CREATE: 17034543Smarks case ZFS_DELEG_CREATE_SETS: 17044543Smarks tree = &zallowp->z_crperms; 17054543Smarks (void) strlcpy(findallownode.z_key, "", 17064543Smarks sizeof (findallownode.z_key)); 17074543Smarks break; 17084543Smarks case ZFS_DELEG_EVERYONE: 17094543Smarks case ZFS_DELEG_EVERYONE_SETS: 17104543Smarks (void) snprintf(findallownode.z_key, 17114543Smarks sizeof (findallownode.z_key), "everyone"); 17124543Smarks tree = &zallowp->z_everyone; 17134543Smarks break; 17144543Smarks case ZFS_DELEG_NAMED_SET: 17154543Smarks case ZFS_DELEG_NAMED_SET_SETS: 17164543Smarks (void) snprintf(findallownode.z_key, 17174543Smarks sizeof (findallownode.z_key), "%s", 17184543Smarks &nvpair_name(who_pair)[3]); 17194543Smarks tree = &zallowp->z_sets; 17204543Smarks break; 17214543Smarks } 17224543Smarks 17234543Smarks /* 17244543Smarks * Place who in tree 17254543Smarks */ 17264543Smarks allownode = avl_find(tree, &findallownode, &where); 17274543Smarks if (allownode == NULL) { 17284543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 17294543Smarks sizeof (zfs_allow_node_t))) == NULL) { 17304543Smarks goto abort; 17314543Smarks } 17324543Smarks avl_create(&newallownode->z_localdescend, 17334543Smarks perm_compare, 17344543Smarks sizeof (zfs_perm_node_t), 17354543Smarks offsetof(zfs_perm_node_t, z_node)); 17364543Smarks avl_create(&newallownode->z_local, 17374543Smarks perm_compare, 17384543Smarks sizeof (zfs_perm_node_t), 17394543Smarks offsetof(zfs_perm_node_t, z_node)); 17404543Smarks avl_create(&newallownode->z_descend, 17414543Smarks perm_compare, 17424543Smarks sizeof (zfs_perm_node_t), 17434543Smarks offsetof(zfs_perm_node_t, z_node)); 17444543Smarks (void) strlcpy(newallownode->z_key, 17454543Smarks findallownode.z_key, 17464543Smarks sizeof (findallownode.z_key)); 17474543Smarks avl_insert(tree, newallownode, where); 17484543Smarks allownode = newallownode; 17494543Smarks } 17504543Smarks 17514543Smarks /* 17524543Smarks * Now iterate over the permissions and 17534543Smarks * place them in the appropriate local, 17544543Smarks * descendent or local+descendent tree. 17554543Smarks * 17564543Smarks * The permissions are added to the tree 17574543Smarks * via zfs_coalesce_perm(). 17584543Smarks */ 17594543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 17604543Smarks if (perm_pair == NULL) 17614543Smarks goto abort; 17624543Smarks do { 17634543Smarks if (zfs_coalesce_perm(zhp, allownode, 17644543Smarks nvpair_name(perm_pair), ld) != 0) 17654543Smarks goto abort; 17664543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 17674543Smarks perm_pair)); 17684543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 17694543Smarks 17704543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 17714543Smarks if (source_pair == NULL) 17724543Smarks break; 17734543Smarks 17744543Smarks /* 17754543Smarks * allocate another node from the link list of 17764543Smarks * zfs_allow_t structures 17774543Smarks */ 17784543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 17794543Smarks nvpair_name(source_pair)); 17804543Smarks if (newallowp == NULL) { 17814543Smarks goto abort; 17824543Smarks } 17834543Smarks zallowp = newallowp; 17844543Smarks } 17854543Smarks nvlist_free(nvlist); 17864543Smarks return (0); 17874543Smarks abort: 17884543Smarks zfs_free_allows(*zfs_perms); 17894543Smarks nvlist_free(nvlist); 17904543Smarks return (-1); 17914543Smarks } 17924543Smarks 1793789Sahrens /* 1794789Sahrens * Given a property name and value, set the property for the given dataset. 1795789Sahrens */ 1796789Sahrens int 17972676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1798789Sahrens { 1799789Sahrens zfs_cmd_t zc = { 0 }; 18002676Seschrock int ret = -1; 18012676Seschrock prop_changelist_t *cl = NULL; 18022082Seschrock char errbuf[1024]; 18032082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 18042676Seschrock nvlist_t *nvl = NULL, *realprops; 18052676Seschrock zfs_prop_t prop; 18062082Seschrock 18072082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 18082676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 18092082Seschrock zhp->zfs_name); 18102082Seschrock 18112676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 18122676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 18132676Seschrock (void) no_memory(hdl); 18142676Seschrock goto error; 1815789Sahrens } 1816789Sahrens 18173912Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, NULL, nvl, 18182676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 18192676Seschrock goto error; 18202676Seschrock nvlist_free(nvl); 18212676Seschrock nvl = realprops; 18222676Seschrock 18232676Seschrock prop = zfs_name_to_prop(propname); 18242676Seschrock 1825789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 18262676Seschrock goto error; 1827789Sahrens 1828789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 18292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18302082Seschrock "child dataset with inherited mountpoint is used " 18312082Seschrock "in a non-global zone")); 18322082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1833789Sahrens goto error; 1834789Sahrens } 1835789Sahrens 1836789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1837789Sahrens goto error; 1838789Sahrens 1839789Sahrens /* 1840789Sahrens * Execute the corresponding ioctl() to set this property. 1841789Sahrens */ 1842789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1843789Sahrens 18442676Seschrock if (zcmd_write_src_nvlist(hdl, &zc, nvl, NULL) != 0) 18452676Seschrock goto error; 18462676Seschrock 18474543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1848789Sahrens 1849789Sahrens if (ret != 0) { 1850789Sahrens switch (errno) { 1851789Sahrens 1852789Sahrens case ENOSPC: 1853789Sahrens /* 1854789Sahrens * For quotas and reservations, ENOSPC indicates 1855789Sahrens * something different; setting a quota or reservation 1856789Sahrens * doesn't use any disk space. 1857789Sahrens */ 1858789Sahrens switch (prop) { 1859789Sahrens case ZFS_PROP_QUOTA: 18602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18612082Seschrock "size is less than current used or " 18622082Seschrock "reserved space")); 18632082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1864789Sahrens break; 1865789Sahrens 1866789Sahrens case ZFS_PROP_RESERVATION: 18672082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18682082Seschrock "size is greater than available space")); 18692082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1870789Sahrens break; 1871789Sahrens 1872789Sahrens default: 18732082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1874789Sahrens break; 1875789Sahrens } 1876789Sahrens break; 1877789Sahrens 1878789Sahrens case EBUSY: 18792082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 18802082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 18812082Seschrock else 18822676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1883789Sahrens break; 1884789Sahrens 18851175Slling case EROFS: 18862082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 18871175Slling break; 18881175Slling 18893886Sahl case ENOTSUP: 18903886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18914603Sahrens "pool must be upgraded to set this " 18924603Sahrens "property or value")); 18933886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 18943886Sahl break; 18953886Sahl 1896789Sahrens case EOVERFLOW: 1897789Sahrens /* 1898789Sahrens * This platform can't address a volume this big. 1899789Sahrens */ 1900789Sahrens #ifdef _ILP32 1901789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 19022082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1903789Sahrens break; 1904789Sahrens } 1905789Sahrens #endif 19062082Seschrock /* FALLTHROUGH */ 1907789Sahrens default: 19082082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1909789Sahrens } 1910789Sahrens } else { 1911789Sahrens /* 1912789Sahrens * Refresh the statistics so the new property value 1913789Sahrens * is reflected. 1914789Sahrens */ 19152676Seschrock if ((ret = changelist_postfix(cl)) == 0) 19162676Seschrock (void) get_stats(zhp); 1917789Sahrens } 1918789Sahrens 1919789Sahrens error: 19202676Seschrock nvlist_free(nvl); 19212676Seschrock zcmd_free_nvlists(&zc); 19222676Seschrock if (cl) 19232676Seschrock changelist_free(cl); 1924789Sahrens return (ret); 1925789Sahrens } 1926789Sahrens 1927789Sahrens /* 1928789Sahrens * Given a property, inherit the value from the parent dataset. 1929789Sahrens */ 1930789Sahrens int 19312676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1932789Sahrens { 1933789Sahrens zfs_cmd_t zc = { 0 }; 1934789Sahrens int ret; 1935789Sahrens prop_changelist_t *cl; 19362082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 19372082Seschrock char errbuf[1024]; 19382676Seschrock zfs_prop_t prop; 19392082Seschrock 19402082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 19412082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1942789Sahrens 19432676Seschrock if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) { 19442676Seschrock /* 19452676Seschrock * For user properties, the amount of work we have to do is very 19462676Seschrock * small, so just do it here. 19472676Seschrock */ 19482676Seschrock if (!zfs_prop_user(propname)) { 19492676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19502676Seschrock "invalid property")); 19512676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 19522676Seschrock } 19532676Seschrock 19542676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19552676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 19562676Seschrock 1957*4849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 19582676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 19592676Seschrock 19602676Seschrock return (0); 19612676Seschrock } 19622676Seschrock 1963789Sahrens /* 1964789Sahrens * Verify that this property is inheritable. 1965789Sahrens */ 19662082Seschrock if (zfs_prop_readonly(prop)) 19672082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 19682082Seschrock 19692082Seschrock if (!zfs_prop_inheritable(prop)) 19702082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1971789Sahrens 1972789Sahrens /* 1973789Sahrens * Check to see if the value applies to this type 1974789Sahrens */ 19752082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 19762082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1977789Sahrens 19783443Srm160521 /* 19793443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 19803443Srm160521 */ 19813443Srm160521 propname = zfs_prop_to_name(prop); 1982789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19832676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1984789Sahrens 1985789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1986789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 19872082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19882082Seschrock "dataset is used in a non-global zone")); 19892082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1990789Sahrens } 1991789Sahrens 1992789Sahrens /* 1993789Sahrens * Determine datasets which will be affected by this change, if any. 1994789Sahrens */ 1995789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1996789Sahrens return (-1); 1997789Sahrens 1998789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 19992082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20002082Seschrock "child dataset with inherited mountpoint is used " 20012082Seschrock "in a non-global zone")); 20022082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2003789Sahrens goto error; 2004789Sahrens } 2005789Sahrens 2006789Sahrens if ((ret = changelist_prefix(cl)) != 0) 2007789Sahrens goto error; 2008789Sahrens 2009*4849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 20102082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2011789Sahrens } else { 2012789Sahrens 20132169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 2014789Sahrens goto error; 2015789Sahrens 2016789Sahrens /* 2017789Sahrens * Refresh the statistics so the new property is reflected. 2018789Sahrens */ 2019789Sahrens (void) get_stats(zhp); 2020789Sahrens } 2021789Sahrens 2022789Sahrens error: 2023789Sahrens changelist_free(cl); 2024789Sahrens return (ret); 2025789Sahrens } 2026789Sahrens 2027789Sahrens /* 20281356Seschrock * True DSL properties are stored in an nvlist. The following two functions 20291356Seschrock * extract them appropriately. 20301356Seschrock */ 20311356Seschrock static uint64_t 20321356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 20331356Seschrock { 20341356Seschrock nvlist_t *nv; 20351356Seschrock uint64_t value; 20361356Seschrock 20372885Sahrens *source = NULL; 20381356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 20391356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 20401356Seschrock verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0); 20412885Sahrens (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 20421356Seschrock } else { 20431356Seschrock value = zfs_prop_default_numeric(prop); 20441356Seschrock *source = ""; 20451356Seschrock } 20461356Seschrock 20471356Seschrock return (value); 20481356Seschrock } 20491356Seschrock 20501356Seschrock static char * 20511356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 20521356Seschrock { 20531356Seschrock nvlist_t *nv; 20541356Seschrock char *value; 20551356Seschrock 20562885Sahrens *source = NULL; 20571356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 20581356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 20591356Seschrock verify(nvlist_lookup_string(nv, ZFS_PROP_VALUE, &value) == 0); 20602885Sahrens (void) nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source); 20611356Seschrock } else { 20621356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 20631356Seschrock value = ""; 20641356Seschrock *source = ""; 20651356Seschrock } 20661356Seschrock 20671356Seschrock return (value); 20681356Seschrock } 20691356Seschrock 20701356Seschrock /* 2071789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2072789Sahrens * zfs_prop_get_int() are built using this interface. 2073789Sahrens * 2074789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2075789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2076789Sahrens * If they differ from the on-disk values, report the current values and mark 2077789Sahrens * the source "temporary". 2078789Sahrens */ 20792082Seschrock static int 2080789Sahrens get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src, 20812082Seschrock char **source, uint64_t *val) 2082789Sahrens { 2083789Sahrens struct mnttab mnt; 20843265Sahrens char *mntopt_on = NULL; 20853265Sahrens char *mntopt_off = NULL; 2086789Sahrens 2087789Sahrens *source = NULL; 2088789Sahrens 20893265Sahrens switch (prop) { 20903265Sahrens case ZFS_PROP_ATIME: 20913265Sahrens mntopt_on = MNTOPT_ATIME; 20923265Sahrens mntopt_off = MNTOPT_NOATIME; 20933265Sahrens break; 20943265Sahrens 20953265Sahrens case ZFS_PROP_DEVICES: 20963265Sahrens mntopt_on = MNTOPT_DEVICES; 20973265Sahrens mntopt_off = MNTOPT_NODEVICES; 20983265Sahrens break; 20993265Sahrens 21003265Sahrens case ZFS_PROP_EXEC: 21013265Sahrens mntopt_on = MNTOPT_EXEC; 21023265Sahrens mntopt_off = MNTOPT_NOEXEC; 21033265Sahrens break; 21043265Sahrens 21053265Sahrens case ZFS_PROP_READONLY: 21063265Sahrens mntopt_on = MNTOPT_RO; 21073265Sahrens mntopt_off = MNTOPT_RW; 21083265Sahrens break; 21093265Sahrens 21103265Sahrens case ZFS_PROP_SETUID: 21113265Sahrens mntopt_on = MNTOPT_SETUID; 21123265Sahrens mntopt_off = MNTOPT_NOSETUID; 21133265Sahrens break; 21143265Sahrens 21153265Sahrens case ZFS_PROP_XATTR: 21163265Sahrens mntopt_on = MNTOPT_XATTR; 21173265Sahrens mntopt_off = MNTOPT_NOXATTR; 21183265Sahrens break; 21193265Sahrens } 21203265Sahrens 21212474Seschrock /* 21222474Seschrock * Because looking up the mount options is potentially expensive 21232474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 21242474Seschrock * we're looking up a property which requires its presence. 21252474Seschrock */ 21262474Seschrock if (!zhp->zfs_mntcheck && 21273265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 21283265Sahrens struct mnttab entry, search = { 0 }; 21293265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 21302474Seschrock 21312474Seschrock search.mnt_special = (char *)zhp->zfs_name; 21322474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 21333265Sahrens rewind(mnttab); 21343265Sahrens 21353265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 21363265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 21373265Sahrens entry.mnt_mntopts); 21383265Sahrens if (zhp->zfs_mntopts == NULL) 21393265Sahrens return (-1); 21403265Sahrens } 21412474Seschrock 21422474Seschrock zhp->zfs_mntcheck = B_TRUE; 21432474Seschrock } 21442474Seschrock 2145789Sahrens if (zhp->zfs_mntopts == NULL) 2146789Sahrens mnt.mnt_mntopts = ""; 2147789Sahrens else 2148789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2149789Sahrens 2150789Sahrens switch (prop) { 2151789Sahrens case ZFS_PROP_ATIME: 21523265Sahrens case ZFS_PROP_DEVICES: 21533265Sahrens case ZFS_PROP_EXEC: 21543265Sahrens case ZFS_PROP_READONLY: 21553265Sahrens case ZFS_PROP_SETUID: 21563265Sahrens case ZFS_PROP_XATTR: 21572082Seschrock *val = getprop_uint64(zhp, prop, source); 21582082Seschrock 21593265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 21602082Seschrock *val = B_TRUE; 2161789Sahrens if (src) 2162789Sahrens *src = ZFS_SRC_TEMPORARY; 21633265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 21642082Seschrock *val = B_FALSE; 2165789Sahrens if (src) 2166789Sahrens *src = ZFS_SRC_TEMPORARY; 2167789Sahrens } 21682082Seschrock break; 2169789Sahrens 21703265Sahrens case ZFS_PROP_CANMOUNT: 21712082Seschrock *val = getprop_uint64(zhp, prop, source); 21723417Srm160521 if (*val == 0) 21733417Srm160521 *source = zhp->zfs_name; 21743417Srm160521 else 21753417Srm160521 *source = ""; /* default */ 21762082Seschrock break; 2177789Sahrens 2178789Sahrens case ZFS_PROP_QUOTA: 2179789Sahrens case ZFS_PROP_RESERVATION: 21802885Sahrens *val = getprop_uint64(zhp, prop, source); 21812885Sahrens if (*val == 0) 2182789Sahrens *source = ""; /* default */ 2183789Sahrens else 2184789Sahrens *source = zhp->zfs_name; 21852082Seschrock break; 2186789Sahrens 2187789Sahrens case ZFS_PROP_MOUNTED: 21882082Seschrock *val = (zhp->zfs_mntopts != NULL); 21892082Seschrock break; 2190789Sahrens 21913377Seschrock case ZFS_PROP_NUMCLONES: 21923377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 21933377Seschrock break; 21943377Seschrock 2195789Sahrens default: 21964577Sahrens switch (zfs_prop_get_type(prop)) { 21974787Sahrens case PROP_TYPE_NUMBER: 21984787Sahrens case PROP_TYPE_INDEX: 21994577Sahrens *val = getprop_uint64(zhp, prop, source); 22004577Sahrens break; 22014577Sahrens 22024787Sahrens case PROP_TYPE_STRING: 22034577Sahrens default: 22044577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 22054577Sahrens "cannot get non-numeric property")); 22064577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 22074577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 22084577Sahrens } 2209789Sahrens } 2210789Sahrens 2211789Sahrens return (0); 2212789Sahrens } 2213789Sahrens 2214789Sahrens /* 2215789Sahrens * Calculate the source type, given the raw source string. 2216789Sahrens */ 2217789Sahrens static void 2218789Sahrens get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source, 2219789Sahrens char *statbuf, size_t statlen) 2220789Sahrens { 2221789Sahrens if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY) 2222789Sahrens return; 2223789Sahrens 2224789Sahrens if (source == NULL) { 2225789Sahrens *srctype = ZFS_SRC_NONE; 2226789Sahrens } else if (source[0] == '\0') { 2227789Sahrens *srctype = ZFS_SRC_DEFAULT; 2228789Sahrens } else { 2229789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 2230789Sahrens *srctype = ZFS_SRC_LOCAL; 2231789Sahrens } else { 2232789Sahrens (void) strlcpy(statbuf, source, statlen); 2233789Sahrens *srctype = ZFS_SRC_INHERITED; 2234789Sahrens } 2235789Sahrens } 2236789Sahrens 2237789Sahrens } 2238789Sahrens 2239789Sahrens /* 2240789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2241789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2242789Sahrens * human-readable form. 2243789Sahrens * 2244789Sahrens * Returns 0 on success, or -1 on error. 2245789Sahrens */ 2246789Sahrens int 2247789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 22482082Seschrock zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2249789Sahrens { 2250789Sahrens char *source = NULL; 2251789Sahrens uint64_t val; 2252789Sahrens char *str; 2253789Sahrens const char *root; 22542676Seschrock const char *strval; 2255789Sahrens 2256789Sahrens /* 2257789Sahrens * Check to see if this property applies to our object 2258789Sahrens */ 2259789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2260789Sahrens return (-1); 2261789Sahrens 2262789Sahrens if (src) 2263789Sahrens *src = ZFS_SRC_NONE; 2264789Sahrens 2265789Sahrens switch (prop) { 2266789Sahrens case ZFS_PROP_CREATION: 2267789Sahrens /* 2268789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2269789Sahrens * this into a string unless 'literal' is specified. 2270789Sahrens */ 2271789Sahrens { 22722885Sahrens val = getprop_uint64(zhp, prop, &source); 22732885Sahrens time_t time = (time_t)val; 2274789Sahrens struct tm t; 2275789Sahrens 2276789Sahrens if (literal || 2277789Sahrens localtime_r(&time, &t) == NULL || 2278789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2279789Sahrens &t) == 0) 22802885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2281789Sahrens } 2282789Sahrens break; 2283789Sahrens 2284789Sahrens case ZFS_PROP_MOUNTPOINT: 2285789Sahrens /* 2286789Sahrens * Getting the precise mountpoint can be tricky. 2287789Sahrens * 2288789Sahrens * - for 'none' or 'legacy', return those values. 2289789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2290789Sahrens * - for inherited mountpoints, we want to take everything 2291789Sahrens * after our ancestor and append it to the inherited value. 2292789Sahrens * 2293789Sahrens * If the pool has an alternate root, we want to prepend that 2294789Sahrens * root to any values we return. 2295789Sahrens */ 22961544Seschrock root = zhp->zfs_root; 22971356Seschrock str = getprop_string(zhp, prop, &source); 22981356Seschrock 22991356Seschrock if (str[0] == '\0') { 2300789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2301789Sahrens root, zhp->zfs_name); 23021356Seschrock } else if (str[0] == '/') { 23031356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2304789Sahrens 2305789Sahrens if (relpath[0] == '/') 2306789Sahrens relpath++; 23071356Seschrock if (str[1] == '\0') 23081356Seschrock str++; 2309789Sahrens 2310789Sahrens if (relpath[0] == '\0') 2311789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 23121356Seschrock root, str); 2313789Sahrens else 2314789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 23151356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2316789Sahrens relpath); 2317789Sahrens } else { 2318789Sahrens /* 'legacy' or 'none' */ 23191356Seschrock (void) strlcpy(propbuf, str, proplen); 2320789Sahrens } 2321789Sahrens 2322789Sahrens break; 2323789Sahrens 2324789Sahrens case ZFS_PROP_ORIGIN: 23252885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2326789Sahrens proplen); 2327789Sahrens /* 2328789Sahrens * If there is no parent at all, return failure to indicate that 2329789Sahrens * it doesn't apply to this dataset. 2330789Sahrens */ 2331789Sahrens if (propbuf[0] == '\0') 2332789Sahrens return (-1); 2333789Sahrens break; 2334789Sahrens 2335789Sahrens case ZFS_PROP_QUOTA: 2336789Sahrens case ZFS_PROP_RESERVATION: 23372082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 23382082Seschrock return (-1); 2339789Sahrens 2340789Sahrens /* 2341789Sahrens * If quota or reservation is 0, we translate this into 'none' 2342789Sahrens * (unless literal is set), and indicate that it's the default 2343789Sahrens * value. Otherwise, we print the number nicely and indicate 2344789Sahrens * that its set locally. 2345789Sahrens */ 2346789Sahrens if (val == 0) { 2347789Sahrens if (literal) 2348789Sahrens (void) strlcpy(propbuf, "0", proplen); 2349789Sahrens else 2350789Sahrens (void) strlcpy(propbuf, "none", proplen); 2351789Sahrens } else { 2352789Sahrens if (literal) 23532856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 23543912Slling (u_longlong_t)val); 2355789Sahrens else 2356789Sahrens zfs_nicenum(val, propbuf, proplen); 2357789Sahrens } 2358789Sahrens break; 2359789Sahrens 2360789Sahrens case ZFS_PROP_COMPRESSRATIO: 23612082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 23622082Seschrock return (-1); 23632856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 23642856Snd150628 val / 100, (longlong_t)val % 100); 2365789Sahrens break; 2366789Sahrens 2367789Sahrens case ZFS_PROP_TYPE: 2368789Sahrens switch (zhp->zfs_type) { 2369789Sahrens case ZFS_TYPE_FILESYSTEM: 2370789Sahrens str = "filesystem"; 2371789Sahrens break; 2372789Sahrens case ZFS_TYPE_VOLUME: 2373789Sahrens str = "volume"; 2374789Sahrens break; 2375789Sahrens case ZFS_TYPE_SNAPSHOT: 2376789Sahrens str = "snapshot"; 2377789Sahrens break; 2378789Sahrens default: 23792082Seschrock abort(); 2380789Sahrens } 2381789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2382789Sahrens break; 2383789Sahrens 2384789Sahrens case ZFS_PROP_MOUNTED: 2385789Sahrens /* 2386789Sahrens * The 'mounted' property is a pseudo-property that described 2387789Sahrens * whether the filesystem is currently mounted. Even though 2388789Sahrens * it's a boolean value, the typical values of "on" and "off" 2389789Sahrens * don't make sense, so we translate to "yes" and "no". 2390789Sahrens */ 23912082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 23922082Seschrock src, &source, &val) != 0) 23932082Seschrock return (-1); 23942082Seschrock if (val) 2395789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2396789Sahrens else 2397789Sahrens (void) strlcpy(propbuf, "no", proplen); 2398789Sahrens break; 2399789Sahrens 2400789Sahrens case ZFS_PROP_NAME: 2401789Sahrens /* 2402789Sahrens * The 'name' property is a pseudo-property derived from the 2403789Sahrens * dataset name. It is presented as a real property to simplify 2404789Sahrens * consumers. 2405789Sahrens */ 2406789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2407789Sahrens break; 2408789Sahrens 2409789Sahrens default: 24104577Sahrens switch (zfs_prop_get_type(prop)) { 24114787Sahrens case PROP_TYPE_NUMBER: 24124577Sahrens if (get_numeric_property(zhp, prop, src, 24134577Sahrens &source, &val) != 0) 24144577Sahrens return (-1); 24154577Sahrens if (literal) 24164577Sahrens (void) snprintf(propbuf, proplen, "%llu", 24174577Sahrens (u_longlong_t)val); 24184577Sahrens else 24194577Sahrens zfs_nicenum(val, propbuf, proplen); 24204577Sahrens break; 24214577Sahrens 24224787Sahrens case PROP_TYPE_STRING: 24234577Sahrens (void) strlcpy(propbuf, 24244577Sahrens getprop_string(zhp, prop, &source), proplen); 24254577Sahrens break; 24264577Sahrens 24274787Sahrens case PROP_TYPE_INDEX: 24284577Sahrens val = getprop_uint64(zhp, prop, &source); 24294577Sahrens if (zfs_prop_index_to_string(prop, val, 24304577Sahrens &strval) != 0) 24314577Sahrens return (-1); 24324577Sahrens (void) strlcpy(propbuf, strval, proplen); 24334577Sahrens break; 24344577Sahrens 24354577Sahrens default: 24364577Sahrens abort(); 24374577Sahrens } 2438789Sahrens } 2439789Sahrens 2440789Sahrens get_source(zhp, src, source, statbuf, statlen); 2441789Sahrens 2442789Sahrens return (0); 2443789Sahrens } 2444789Sahrens 2445789Sahrens /* 2446789Sahrens * Utility function to get the given numeric property. Does no validation that 2447789Sahrens * the given property is the appropriate type; should only be used with 2448789Sahrens * hard-coded property types. 2449789Sahrens */ 2450789Sahrens uint64_t 2451789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2452789Sahrens { 2453789Sahrens char *source; 2454789Sahrens zfs_source_t sourcetype = ZFS_SRC_NONE; 24552082Seschrock uint64_t val; 24562082Seschrock 24572082Seschrock (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 24582082Seschrock 24592082Seschrock return (val); 2460789Sahrens } 2461789Sahrens 2462789Sahrens /* 2463789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2464789Sahrens */ 2465789Sahrens int 2466789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2467789Sahrens zfs_source_t *src, char *statbuf, size_t statlen) 2468789Sahrens { 2469789Sahrens char *source; 2470789Sahrens 2471789Sahrens /* 2472789Sahrens * Check to see if this property applies to our object 2473789Sahrens */ 2474*4849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 24753237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 24762082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 24772082Seschrock zfs_prop_to_name(prop))); 2478*4849Sahrens } 2479789Sahrens 2480789Sahrens if (src) 2481789Sahrens *src = ZFS_SRC_NONE; 2482789Sahrens 24832082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 24842082Seschrock return (-1); 2485789Sahrens 2486789Sahrens get_source(zhp, src, source, statbuf, statlen); 2487789Sahrens 2488789Sahrens return (0); 2489789Sahrens } 2490789Sahrens 2491789Sahrens /* 2492789Sahrens * Returns the name of the given zfs handle. 2493789Sahrens */ 2494789Sahrens const char * 2495789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2496789Sahrens { 2497789Sahrens return (zhp->zfs_name); 2498789Sahrens } 2499789Sahrens 2500789Sahrens /* 2501789Sahrens * Returns the type of the given zfs handle. 2502789Sahrens */ 2503789Sahrens zfs_type_t 2504789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2505789Sahrens { 2506789Sahrens return (zhp->zfs_type); 2507789Sahrens } 2508789Sahrens 2509789Sahrens /* 25101356Seschrock * Iterate over all child filesystems 2511789Sahrens */ 2512789Sahrens int 25131356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2514789Sahrens { 2515789Sahrens zfs_cmd_t zc = { 0 }; 2516789Sahrens zfs_handle_t *nzhp; 2517789Sahrens int ret; 2518789Sahrens 2519789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 25202082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2521789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2522789Sahrens /* 2523789Sahrens * Ignore private dataset names. 2524789Sahrens */ 2525789Sahrens if (dataset_name_hidden(zc.zc_name)) 2526789Sahrens continue; 2527789Sahrens 2528789Sahrens /* 2529789Sahrens * Silently ignore errors, as the only plausible explanation is 2530789Sahrens * that the pool has since been removed. 2531789Sahrens */ 25322082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 25332082Seschrock zc.zc_name)) == NULL) 2534789Sahrens continue; 2535789Sahrens 2536789Sahrens if ((ret = func(nzhp, data)) != 0) 2537789Sahrens return (ret); 2538789Sahrens } 2539789Sahrens 2540789Sahrens /* 2541789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2542789Sahrens * returned, then the underlying dataset has been removed since we 2543789Sahrens * obtained the handle. 2544789Sahrens */ 2545789Sahrens if (errno != ESRCH && errno != ENOENT) 25462082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 25472082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2548789Sahrens 25491356Seschrock return (0); 25501356Seschrock } 25511356Seschrock 25521356Seschrock /* 25531356Seschrock * Iterate over all snapshots 25541356Seschrock */ 25551356Seschrock int 25561356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 25571356Seschrock { 25581356Seschrock zfs_cmd_t zc = { 0 }; 25591356Seschrock zfs_handle_t *nzhp; 25601356Seschrock int ret; 2561789Sahrens 2562789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 25632082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 25642082Seschrock &zc) == 0; 2565789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2566789Sahrens 25672082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 25682082Seschrock zc.zc_name)) == NULL) 2569789Sahrens continue; 2570789Sahrens 2571789Sahrens if ((ret = func(nzhp, data)) != 0) 2572789Sahrens return (ret); 2573789Sahrens } 2574789Sahrens 2575789Sahrens /* 2576789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2577789Sahrens * returned, then the underlying dataset has been removed since we 2578789Sahrens * obtained the handle. Silently ignore this case, and return success. 2579789Sahrens */ 2580789Sahrens if (errno != ESRCH && errno != ENOENT) 25812082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 25822082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2583789Sahrens 2584789Sahrens return (0); 2585789Sahrens } 2586789Sahrens 2587789Sahrens /* 25881356Seschrock * Iterate over all children, snapshots and filesystems 25891356Seschrock */ 25901356Seschrock int 25911356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 25921356Seschrock { 25931356Seschrock int ret; 25941356Seschrock 25951356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 25961356Seschrock return (ret); 25971356Seschrock 25981356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 25991356Seschrock } 26001356Seschrock 26011356Seschrock /* 2602789Sahrens * Given a complete name, return just the portion that refers to the parent. 2603789Sahrens * Can return NULL if this is a pool. 2604789Sahrens */ 2605789Sahrens static int 2606789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2607789Sahrens { 2608789Sahrens char *loc; 2609789Sahrens 2610789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2611789Sahrens return (-1); 2612789Sahrens 2613789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2614789Sahrens buf[loc - path] = '\0'; 2615789Sahrens 2616789Sahrens return (0); 2617789Sahrens } 2618789Sahrens 2619789Sahrens /* 26204490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 26214490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 26224490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 26234490Svb160487 * length of already existing prefix of the given path. We also fetch the 26244490Svb160487 * 'zoned' property, which is used to validate property settings when creating 26254490Svb160487 * new datasets. 2626789Sahrens */ 2627789Sahrens static int 26284490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 26294490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2630789Sahrens { 2631789Sahrens zfs_cmd_t zc = { 0 }; 2632789Sahrens char parent[ZFS_MAXNAMELEN]; 2633789Sahrens char *slash; 26341356Seschrock zfs_handle_t *zhp; 26352082Seschrock char errbuf[1024]; 26362082Seschrock 26372082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 26382082Seschrock path); 2639789Sahrens 2640789Sahrens /* get parent, and check to see if this is just a pool */ 2641789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 26422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26432082Seschrock "missing dataset name")); 26442082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2645789Sahrens } 2646789Sahrens 2647789Sahrens /* check to see if the pool exists */ 2648789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2649789Sahrens slash = parent + strlen(parent); 2650789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2651789Sahrens zc.zc_name[slash - parent] = '\0'; 26522082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2653789Sahrens errno == ENOENT) { 26542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26552082Seschrock "no such pool '%s'"), zc.zc_name); 26562082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2657789Sahrens } 2658789Sahrens 2659789Sahrens /* check to see if the parent dataset exists */ 26604490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 26614490Svb160487 if (errno == ENOENT && accept_ancestor) { 26624490Svb160487 /* 26634490Svb160487 * Go deeper to find an ancestor, give up on top level. 26644490Svb160487 */ 26654490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 26664490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26674490Svb160487 "no such pool '%s'"), zc.zc_name); 26684490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 26694490Svb160487 } 26704490Svb160487 } else if (errno == ENOENT) { 26712082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26722082Seschrock "parent does not exist")); 26732082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 26744490Svb160487 } else 26752082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2676789Sahrens } 2677789Sahrens 26782676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2679789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 26802676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 26812082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 26821356Seschrock zfs_close(zhp); 2683789Sahrens return (-1); 2684789Sahrens } 2685789Sahrens 2686789Sahrens /* make sure parent is a filesystem */ 26871356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 26882082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26892082Seschrock "parent is not a filesystem")); 26902082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 26911356Seschrock zfs_close(zhp); 2692789Sahrens return (-1); 2693789Sahrens } 2694789Sahrens 26951356Seschrock zfs_close(zhp); 26964490Svb160487 if (prefixlen != NULL) 26974490Svb160487 *prefixlen = strlen(parent); 26984490Svb160487 return (0); 26994490Svb160487 } 27004490Svb160487 27014490Svb160487 /* 27024490Svb160487 * Finds whether the dataset of the given type(s) exists. 27034490Svb160487 */ 27044490Svb160487 boolean_t 27054490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 27064490Svb160487 { 27074490Svb160487 zfs_handle_t *zhp; 27084490Svb160487 27094490Svb160487 if (!zfs_validate_name(hdl, path, types)) 27104490Svb160487 return (B_FALSE); 27114490Svb160487 27124490Svb160487 /* 27134490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 27144490Svb160487 */ 27154490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 27164490Svb160487 int ds_type = zhp->zfs_type; 27174490Svb160487 27184490Svb160487 zfs_close(zhp); 27194490Svb160487 if (types & ds_type) 27204490Svb160487 return (B_TRUE); 27214490Svb160487 } 27224490Svb160487 return (B_FALSE); 27234490Svb160487 } 27244490Svb160487 27254490Svb160487 /* 27264490Svb160487 * Creates non-existing ancestors of the given path. 27274490Svb160487 */ 27284490Svb160487 int 27294490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 27304490Svb160487 { 27314490Svb160487 int prefix; 27324490Svb160487 uint64_t zoned; 27334490Svb160487 char *path_copy; 27344490Svb160487 int rc; 27354490Svb160487 27364490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 27374490Svb160487 return (-1); 27384490Svb160487 27394490Svb160487 if ((path_copy = strdup(path)) != NULL) { 27404490Svb160487 rc = create_parents(hdl, path_copy, prefix); 27414490Svb160487 free(path_copy); 27424490Svb160487 } 27434490Svb160487 if (path_copy == NULL || rc != 0) 27444490Svb160487 return (-1); 27454490Svb160487 2746789Sahrens return (0); 2747789Sahrens } 2748789Sahrens 2749789Sahrens /* 27502676Seschrock * Create a new filesystem or volume. 2751789Sahrens */ 2752789Sahrens int 27532082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 27542676Seschrock nvlist_t *props) 2755789Sahrens { 2756789Sahrens zfs_cmd_t zc = { 0 }; 2757789Sahrens int ret; 2758789Sahrens uint64_t size = 0; 2759789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 27602082Seschrock char errbuf[1024]; 27612676Seschrock uint64_t zoned; 27622082Seschrock 27632082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 27642082Seschrock "cannot create '%s'"), path); 2765789Sahrens 2766789Sahrens /* validate the path, taking care to note the extended error message */ 27672082Seschrock if (!zfs_validate_name(hdl, path, type)) 27682082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2769789Sahrens 2770789Sahrens /* validate parents exist */ 27714490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2772789Sahrens return (-1); 2773789Sahrens 2774789Sahrens /* 2775789Sahrens * The failure modes when creating a dataset of a different type over 2776789Sahrens * one that already exists is a little strange. In particular, if you 2777789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2778789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2779789Sahrens * first try to see if the dataset exists. 2780789Sahrens */ 2781789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 27824490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 27832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27842082Seschrock "dataset already exists")); 27852082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2786789Sahrens } 2787789Sahrens 2788789Sahrens if (type == ZFS_TYPE_VOLUME) 2789789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2790789Sahrens else 2791789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2792789Sahrens 27933912Slling if (props && (props = zfs_validate_properties(hdl, type, NULL, props, 27943912Slling zoned, NULL, errbuf)) == 0) 27952676Seschrock return (-1); 27962676Seschrock 2797789Sahrens if (type == ZFS_TYPE_VOLUME) { 27981133Seschrock /* 27991133Seschrock * If we are creating a volume, the size and block size must 28001133Seschrock * satisfy a few restraints. First, the blocksize must be a 28011133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 28021133Seschrock * volsize must be a multiple of the block size, and cannot be 28031133Seschrock * zero. 28041133Seschrock */ 28052676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 28062676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 28072676Seschrock nvlist_free(props); 28082082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28092676Seschrock "missing volume size")); 28102676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2811789Sahrens } 2812789Sahrens 28132676Seschrock if ((ret = nvlist_lookup_uint64(props, 28142676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 28152676Seschrock &blocksize)) != 0) { 28162676Seschrock if (ret == ENOENT) { 28172676Seschrock blocksize = zfs_prop_default_numeric( 28182676Seschrock ZFS_PROP_VOLBLOCKSIZE); 28192676Seschrock } else { 28202676Seschrock nvlist_free(props); 28212676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28222676Seschrock "missing volume block size")); 28232676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28242676Seschrock } 28252676Seschrock } 28262676Seschrock 28272676Seschrock if (size == 0) { 28282676Seschrock nvlist_free(props); 28292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28302676Seschrock "volume size cannot be zero")); 28312676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28321133Seschrock } 28331133Seschrock 28341133Seschrock if (size % blocksize != 0) { 28352676Seschrock nvlist_free(props); 28362082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28372676Seschrock "volume size must be a multiple of volume block " 28382676Seschrock "size")); 28392676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28401133Seschrock } 2841789Sahrens } 2842789Sahrens 28432676Seschrock if (props && 28442676Seschrock zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) 28452676Seschrock return (-1); 28462676Seschrock nvlist_free(props); 28472676Seschrock 2848789Sahrens /* create the dataset */ 28494543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2850789Sahrens 28513912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 28522082Seschrock ret = zvol_create_link(hdl, path); 28533912Slling if (ret) { 28543912Slling (void) zfs_standard_error(hdl, errno, 28553912Slling dgettext(TEXT_DOMAIN, 28563912Slling "Volume successfully created, but device links " 28573912Slling "were not created")); 28583912Slling zcmd_free_nvlists(&zc); 28593912Slling return (-1); 28603912Slling } 28613912Slling } 2862789Sahrens 28632676Seschrock zcmd_free_nvlists(&zc); 28642676Seschrock 2865789Sahrens /* check for failure */ 2866789Sahrens if (ret != 0) { 2867789Sahrens char parent[ZFS_MAXNAMELEN]; 2868789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2869789Sahrens 2870789Sahrens switch (errno) { 2871789Sahrens case ENOENT: 28722082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28732082Seschrock "no such parent '%s'"), parent); 28742082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2875789Sahrens 2876789Sahrens case EINVAL: 28772082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28783413Smmusante "parent '%s' is not a filesystem"), parent); 28792082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2880789Sahrens 2881789Sahrens case EDOM: 28822082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28832676Seschrock "volume block size must be power of 2 from " 28842676Seschrock "%u to %uk"), 2885789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2886789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 28872082Seschrock 28882676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28892082Seschrock 28904603Sahrens case ENOTSUP: 28914603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28924603Sahrens "pool must be upgraded to set this " 28934603Sahrens "property or value")); 28944603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 28954603Sahrens 2896789Sahrens #ifdef _ILP32 2897789Sahrens case EOVERFLOW: 2898789Sahrens /* 2899789Sahrens * This platform can't address a volume this big. 2900789Sahrens */ 29012082Seschrock if (type == ZFS_TYPE_VOLUME) 29022082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 29032082Seschrock errbuf)); 2904789Sahrens #endif 29052082Seschrock /* FALLTHROUGH */ 2906789Sahrens default: 29072082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2908789Sahrens } 2909789Sahrens } 2910789Sahrens 2911789Sahrens return (0); 2912789Sahrens } 2913789Sahrens 2914789Sahrens /* 2915789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2916789Sahrens * isn't mounted, and that there are no active dependents. 2917789Sahrens */ 2918789Sahrens int 2919789Sahrens zfs_destroy(zfs_handle_t *zhp) 2920789Sahrens { 2921789Sahrens zfs_cmd_t zc = { 0 }; 2922789Sahrens 2923789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2924789Sahrens 29252676Seschrock if (ZFS_IS_VOLUME(zhp)) { 29263126Sahl /* 29274543Smarks * If user doesn't have permissions to unshare volume, then 29284543Smarks * abort the request. This would only happen for a 29294543Smarks * non-privileged user. 29303126Sahl */ 29314543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 29324543Smarks return (-1); 29334543Smarks } 29343126Sahl 29352082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2936789Sahrens return (-1); 2937789Sahrens 2938789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2939789Sahrens } else { 2940789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2941789Sahrens } 2942789Sahrens 29434543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 29443237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 29452082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 29462082Seschrock zhp->zfs_name)); 29472199Sahrens } 2948789Sahrens 2949789Sahrens remove_mountpoint(zhp); 2950789Sahrens 2951789Sahrens return (0); 2952789Sahrens } 2953789Sahrens 29542199Sahrens struct destroydata { 29552199Sahrens char *snapname; 29562199Sahrens boolean_t gotone; 29573265Sahrens boolean_t closezhp; 29582199Sahrens }; 29592199Sahrens 29602199Sahrens static int 29612199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 29622199Sahrens { 29632199Sahrens struct destroydata *dd = arg; 29642199Sahrens zfs_handle_t *szhp; 29652199Sahrens char name[ZFS_MAXNAMELEN]; 29663265Sahrens boolean_t closezhp = dd->closezhp; 29673265Sahrens int rv; 29682199Sahrens 29692676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 29702676Seschrock (void) strlcat(name, "@", sizeof (name)); 29712676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 29722199Sahrens 29732199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 29742199Sahrens if (szhp) { 29752199Sahrens dd->gotone = B_TRUE; 29762199Sahrens zfs_close(szhp); 29772199Sahrens } 29782199Sahrens 29792199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 29802199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 29812199Sahrens /* 29822199Sahrens * NB: this is simply a best-effort. We don't want to 29832199Sahrens * return an error, because then we wouldn't visit all 29842199Sahrens * the volumes. 29852199Sahrens */ 29862199Sahrens } 29872199Sahrens 29883265Sahrens dd->closezhp = B_TRUE; 29893265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 29903265Sahrens if (closezhp) 29913265Sahrens zfs_close(zhp); 29923265Sahrens return (rv); 29932199Sahrens } 29942199Sahrens 29952199Sahrens /* 29962199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 29972199Sahrens */ 29982199Sahrens int 29992199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 30002199Sahrens { 30012199Sahrens zfs_cmd_t zc = { 0 }; 30022199Sahrens int ret; 30032199Sahrens struct destroydata dd = { 0 }; 30042199Sahrens 30052199Sahrens dd.snapname = snapname; 30062199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 30072199Sahrens 30082199Sahrens if (!dd.gotone) { 30093237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 30102199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 30112199Sahrens zhp->zfs_name, snapname)); 30122199Sahrens } 30132199Sahrens 30142199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 30152676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 30162199Sahrens 30174543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 30182199Sahrens if (ret != 0) { 30192199Sahrens char errbuf[1024]; 30202199Sahrens 30212199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30222199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 30232199Sahrens 30242199Sahrens switch (errno) { 30252199Sahrens case EEXIST: 30262199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30272199Sahrens "snapshot is cloned")); 30282199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 30292199Sahrens 30302199Sahrens default: 30312199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 30322199Sahrens errbuf)); 30332199Sahrens } 30342199Sahrens } 30352199Sahrens 30362199Sahrens return (0); 30372199Sahrens } 30382199Sahrens 3039789Sahrens /* 3040789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3041789Sahrens */ 3042789Sahrens int 30432676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3044789Sahrens { 3045789Sahrens zfs_cmd_t zc = { 0 }; 3046789Sahrens char parent[ZFS_MAXNAMELEN]; 3047789Sahrens int ret; 30482082Seschrock char errbuf[1024]; 30492082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 30502676Seschrock zfs_type_t type; 30512676Seschrock uint64_t zoned; 3052789Sahrens 3053789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3054789Sahrens 30552082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30562082Seschrock "cannot create '%s'"), target); 30572082Seschrock 3058789Sahrens /* validate the target name */ 30592082Seschrock if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM)) 30602082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3061789Sahrens 3062789Sahrens /* validate parents exist */ 30634490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3064789Sahrens return (-1); 3065789Sahrens 3066789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3067789Sahrens 3068789Sahrens /* do the clone */ 30692676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3070789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 30712744Snn35248 type = ZFS_TYPE_VOLUME; 30722676Seschrock } else { 3073789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 30742744Snn35248 type = ZFS_TYPE_FILESYSTEM; 30752676Seschrock } 30762676Seschrock 30772676Seschrock if (props) { 30783912Slling if ((props = zfs_validate_properties(hdl, type, NULL, props, 30793912Slling zoned, zhp, errbuf)) == NULL) 30802676Seschrock return (-1); 30812676Seschrock 30822676Seschrock if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) { 30832676Seschrock nvlist_free(props); 30842676Seschrock return (-1); 30852676Seschrock } 30862676Seschrock 30872676Seschrock nvlist_free(props); 30882676Seschrock } 3089789Sahrens 3090789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 30912676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 30924543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3093789Sahrens 30942676Seschrock zcmd_free_nvlists(&zc); 30952676Seschrock 3096789Sahrens if (ret != 0) { 3097789Sahrens switch (errno) { 3098789Sahrens 3099789Sahrens case ENOENT: 3100789Sahrens /* 3101789Sahrens * The parent doesn't exist. We should have caught this 3102789Sahrens * above, but there may a race condition that has since 3103789Sahrens * destroyed the parent. 3104789Sahrens * 3105789Sahrens * At this point, we don't know whether it's the source 3106789Sahrens * that doesn't exist anymore, or whether the target 3107789Sahrens * dataset doesn't exist. 3108789Sahrens */ 31092082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31102082Seschrock "no such parent '%s'"), parent); 31112082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 31122082Seschrock 31132082Seschrock case EXDEV: 31142082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31152082Seschrock "source and target pools differ")); 31162082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 31172082Seschrock errbuf)); 31182082Seschrock 31192082Seschrock default: 31202082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 31212082Seschrock errbuf)); 31222082Seschrock } 31232676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 31242082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 31252082Seschrock } 31262082Seschrock 31272082Seschrock return (ret); 31282082Seschrock } 31292082Seschrock 31302082Seschrock typedef struct promote_data { 31312082Seschrock char cb_mountpoint[MAXPATHLEN]; 31322082Seschrock const char *cb_target; 31332082Seschrock const char *cb_errbuf; 31342082Seschrock uint64_t cb_pivot_txg; 31352082Seschrock } promote_data_t; 31362082Seschrock 31372082Seschrock static int 31382082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 31392082Seschrock { 31402082Seschrock promote_data_t *pd = data; 31412082Seschrock zfs_handle_t *szhp; 31422082Seschrock char snapname[MAXPATHLEN]; 31433265Sahrens int rv = 0; 31442082Seschrock 31452082Seschrock /* We don't care about snapshots after the pivot point */ 31463265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 31473265Sahrens zfs_close(zhp); 31482082Seschrock return (0); 31493265Sahrens } 31502082Seschrock 31512417Sahrens /* Remove the device link if it's a zvol. */ 31522676Seschrock if (ZFS_IS_VOLUME(zhp)) 31532417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 31542082Seschrock 31552082Seschrock /* Check for conflicting names */ 31562676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 31572676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 31582082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 31592082Seschrock if (szhp != NULL) { 31602082Seschrock zfs_close(szhp); 31612082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31622082Seschrock "snapshot name '%s' from origin \n" 31632082Seschrock "conflicts with '%s' from target"), 31642082Seschrock zhp->zfs_name, snapname); 31653265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 31662082Seschrock } 31673265Sahrens zfs_close(zhp); 31683265Sahrens return (rv); 31692082Seschrock } 31702082Seschrock 31712417Sahrens static int 31722417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 31732417Sahrens { 31742417Sahrens promote_data_t *pd = data; 31752417Sahrens 31762417Sahrens /* We don't care about snapshots after the pivot point */ 31773265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 31783265Sahrens /* Create the device link if it's a zvol. */ 31793265Sahrens if (ZFS_IS_VOLUME(zhp)) 31803265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 31813265Sahrens } 31823265Sahrens 31833265Sahrens zfs_close(zhp); 31842417Sahrens return (0); 31852417Sahrens } 31862417Sahrens 31872082Seschrock /* 31882082Seschrock * Promotes the given clone fs to be the clone parent. 31892082Seschrock */ 31902082Seschrock int 31912082Seschrock zfs_promote(zfs_handle_t *zhp) 31922082Seschrock { 31932082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 31942082Seschrock zfs_cmd_t zc = { 0 }; 31952082Seschrock char parent[MAXPATHLEN]; 31962082Seschrock char *cp; 31972082Seschrock int ret; 31982082Seschrock zfs_handle_t *pzhp; 31992082Seschrock promote_data_t pd; 32002082Seschrock char errbuf[1024]; 32012082Seschrock 32022082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32032082Seschrock "cannot promote '%s'"), zhp->zfs_name); 32042082Seschrock 32052082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 32062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32072082Seschrock "snapshots can not be promoted")); 32082082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 32092082Seschrock } 32102082Seschrock 32112676Seschrock (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 32122082Seschrock if (parent[0] == '\0') { 32132082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32142082Seschrock "not a cloned filesystem")); 32152082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 32162082Seschrock } 32172082Seschrock cp = strchr(parent, '@'); 32182082Seschrock *cp = '\0'; 32192082Seschrock 32202082Seschrock /* Walk the snapshots we will be moving */ 32212082Seschrock pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 32222082Seschrock if (pzhp == NULL) 32232082Seschrock return (-1); 32242082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 32252082Seschrock zfs_close(pzhp); 32262082Seschrock pd.cb_target = zhp->zfs_name; 32272082Seschrock pd.cb_errbuf = errbuf; 32282082Seschrock pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY); 32292082Seschrock if (pzhp == NULL) 32302082Seschrock return (-1); 32312082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 32322082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 32332082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 32342417Sahrens if (ret != 0) { 32352417Sahrens zfs_close(pzhp); 32362082Seschrock return (-1); 32372417Sahrens } 32382082Seschrock 32392082Seschrock /* issue the ioctl */ 32402676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 32412676Seschrock sizeof (zc.zc_value)); 32422082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 32434543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 32442082Seschrock 32452082Seschrock if (ret != 0) { 32462417Sahrens int save_errno = errno; 32472417Sahrens 32482417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 32492417Sahrens zfs_close(pzhp); 32502417Sahrens 32512417Sahrens switch (save_errno) { 3252789Sahrens case EEXIST: 3253789Sahrens /* 32542082Seschrock * There is a conflicting snapshot name. We 32552082Seschrock * should have caught this above, but they could 32562082Seschrock * have renamed something in the mean time. 3257789Sahrens */ 32582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32592082Seschrock "conflicting snapshot name from parent '%s'"), 32602082Seschrock parent); 32612082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3262789Sahrens 3263789Sahrens default: 32642417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3265789Sahrens } 32662417Sahrens } else { 32672417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3268789Sahrens } 3269789Sahrens 32702417Sahrens zfs_close(pzhp); 3271789Sahrens return (ret); 3272789Sahrens } 3273789Sahrens 32744007Smmusante struct createdata { 32754007Smmusante const char *cd_snapname; 32764007Smmusante int cd_ifexists; 32774007Smmusante }; 32784007Smmusante 32792199Sahrens static int 32802199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 32812199Sahrens { 32824007Smmusante struct createdata *cd = arg; 32832676Seschrock int ret; 32842199Sahrens 32852199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 32862199Sahrens char name[MAXPATHLEN]; 32872199Sahrens 32882676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 32892676Seschrock (void) strlcat(name, "@", sizeof (name)); 32904007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 32914007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 32924007Smmusante cd->cd_ifexists); 32932199Sahrens /* 32942199Sahrens * NB: this is simply a best-effort. We don't want to 32952199Sahrens * return an error, because then we wouldn't visit all 32962199Sahrens * the volumes. 32972199Sahrens */ 32982199Sahrens } 32992676Seschrock 33004007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 33012676Seschrock 33022676Seschrock zfs_close(zhp); 33032676Seschrock 33042676Seschrock return (ret); 33052199Sahrens } 33062199Sahrens 3307789Sahrens /* 33083504Sahl * Takes a snapshot of the given dataset. 3309789Sahrens */ 3310789Sahrens int 33112199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3312789Sahrens { 3313789Sahrens const char *delim; 3314789Sahrens char *parent; 3315789Sahrens zfs_handle_t *zhp; 3316789Sahrens zfs_cmd_t zc = { 0 }; 3317789Sahrens int ret; 33182082Seschrock char errbuf[1024]; 33192082Seschrock 33202082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33212082Seschrock "cannot snapshot '%s'"), path); 33222082Seschrock 33232082Seschrock /* validate the target name */ 33242082Seschrock if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT)) 33252082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3326789Sahrens 3327789Sahrens /* make sure the parent exists and is of the appropriate type */ 33282199Sahrens delim = strchr(path, '@'); 33292082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 33302082Seschrock return (-1); 3331789Sahrens (void) strncpy(parent, path, delim - path); 3332789Sahrens parent[delim - path] = '\0'; 3333789Sahrens 33342082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3335789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3336789Sahrens free(parent); 3337789Sahrens return (-1); 3338789Sahrens } 3339789Sahrens 33402199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33412676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 33424543Smarks if (ZFS_IS_VOLUME(zhp)) 33434543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 33444543Smarks else 33454543Smarks zc.zc_objset_type = DMU_OST_ZFS; 33462199Sahrens zc.zc_cookie = recursive; 33474543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 33482199Sahrens 33492199Sahrens /* 33502199Sahrens * if it was recursive, the one that actually failed will be in 33512199Sahrens * zc.zc_name. 33522199Sahrens */ 33534543Smarks if (ret != 0) 33544543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33554543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 33564543Smarks 33572199Sahrens if (ret == 0 && recursive) { 33584007Smmusante struct createdata cd; 33594007Smmusante 33604007Smmusante cd.cd_snapname = delim + 1; 33614007Smmusante cd.cd_ifexists = B_FALSE; 33624007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 33632199Sahrens } 3364789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 33652082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 33662199Sahrens if (ret != 0) { 33674543Smarks (void) zfs_standard_error(hdl, errno, 33684543Smarks dgettext(TEXT_DOMAIN, 33694543Smarks "Volume successfully snapshotted, but device links " 33704543Smarks "were not created")); 33714543Smarks free(parent); 33724543Smarks zfs_close(zhp); 33734543Smarks return (-1); 33742199Sahrens } 3375789Sahrens } 3376789Sahrens 33772082Seschrock if (ret != 0) 33782082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3379789Sahrens 3380789Sahrens free(parent); 3381789Sahrens zfs_close(zhp); 3382789Sahrens 3383789Sahrens return (ret); 3384789Sahrens } 3385789Sahrens 3386789Sahrens /* 33873504Sahl * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 33883504Sahl * NULL) to the file descriptor specified by outfd. 3389789Sahrens */ 3390789Sahrens int 33913504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3392789Sahrens { 3393789Sahrens zfs_cmd_t zc = { 0 }; 33942082Seschrock char errbuf[1024]; 33952885Sahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 33962082Seschrock 33973504Sahl assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 33983504Sahl 33992885Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 34002885Sahrens if (fromsnap) 34012885Sahrens (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 34023504Sahl zc.zc_cookie = outfd; 34033504Sahl 34043504Sahl if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 34053504Sahl (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34063504Sahl "cannot send '%s'"), zhp->zfs_name); 34073504Sahl 3408789Sahrens switch (errno) { 3409789Sahrens 3410789Sahrens case EXDEV: 34112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34123413Smmusante "not an earlier snapshot from the same fs")); 34132082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3414789Sahrens 3415789Sahrens case EDQUOT: 3416789Sahrens case EFBIG: 3417789Sahrens case EIO: 3418789Sahrens case ENOLINK: 3419789Sahrens case ENOSPC: 3420789Sahrens case ENOSTR: 3421789Sahrens case ENXIO: 3422789Sahrens case EPIPE: 3423789Sahrens case ERANGE: 3424789Sahrens case EFAULT: 3425789Sahrens case EROFS: 34262082Seschrock zfs_error_aux(hdl, strerror(errno)); 34272082Seschrock return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3428789Sahrens 3429789Sahrens default: 34302082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3431789Sahrens } 3432789Sahrens } 3433789Sahrens 34343504Sahl return (0); 3435789Sahrens } 3436789Sahrens 3437789Sahrens /* 34382885Sahrens * Create ancestors of 'target', but not target itself, and not 34392885Sahrens * ancestors whose names are shorter than prefixlen. Die if 34402885Sahrens * prefixlen-ancestor does not exist. 34412885Sahrens */ 34422885Sahrens static int 34432885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 34442885Sahrens { 34452885Sahrens zfs_handle_t *h; 34462885Sahrens char *cp; 34472885Sahrens 34482885Sahrens /* make sure prefix exists */ 34492885Sahrens cp = strchr(target + prefixlen, '/'); 34504603Sahrens if (cp == NULL) { 34514603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 34524603Sahrens } else { 34534603Sahrens *cp = '\0'; 34544603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 34554603Sahrens *cp = '/'; 34564603Sahrens } 34572885Sahrens if (h == NULL) 34582885Sahrens return (-1); 34592885Sahrens zfs_close(h); 34602885Sahrens 34612885Sahrens /* 34622885Sahrens * Attempt to create, mount, and share any ancestor filesystems, 34632885Sahrens * up to the prefixlen-long one. 34642885Sahrens */ 34652885Sahrens for (cp = target + prefixlen + 1; 34662885Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 34672885Sahrens const char *opname; 34684543Smarks char *logstr; 34692885Sahrens 34702885Sahrens *cp = '\0'; 34712885Sahrens 34722885Sahrens h = make_dataset_handle(hdl, target); 34732885Sahrens if (h) { 34742885Sahrens /* it already exists, nothing to do here */ 34752885Sahrens zfs_close(h); 34762885Sahrens continue; 34772885Sahrens } 34782885Sahrens 34792885Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 34804543Smarks logstr = hdl->libzfs_log_str; 34814543Smarks hdl->libzfs_log_str = NULL; 34822885Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 34834543Smarks NULL) != 0) { 34844543Smarks hdl->libzfs_log_str = logstr; 34852885Sahrens goto ancestorerr; 34864543Smarks } 34874543Smarks 34884543Smarks hdl->libzfs_log_str = logstr; 34892885Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 34902885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 34912885Sahrens if (h == NULL) 34922885Sahrens goto ancestorerr; 34932885Sahrens 34942885Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 34952885Sahrens if (zfs_mount(h, NULL, 0) != 0) 34962885Sahrens goto ancestorerr; 34972885Sahrens 34982885Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 34992885Sahrens if (zfs_share(h) != 0) 35002885Sahrens goto ancestorerr; 35012885Sahrens 35022885Sahrens zfs_close(h); 35032885Sahrens 35042885Sahrens continue; 35052885Sahrens ancestorerr: 35062885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35072885Sahrens "failed to %s ancestor '%s'"), opname, target); 35082885Sahrens return (-1); 35092885Sahrens } 35102885Sahrens 35112885Sahrens return (0); 35122885Sahrens } 35132885Sahrens 35142885Sahrens /* 35153504Sahl * Restores a backup of tosnap from the file descriptor specified by infd. 3516789Sahrens */ 3517789Sahrens int 35182082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 35193504Sahl int verbose, int dryrun, boolean_t force, int infd) 3520789Sahrens { 3521789Sahrens zfs_cmd_t zc = { 0 }; 3522789Sahrens time_t begin_time; 35232885Sahrens int ioctl_err, err, bytes, size, choplen; 3524789Sahrens char *cp; 3525789Sahrens dmu_replay_record_t drr; 3526789Sahrens struct drr_begin *drrb = &zc.zc_begin_record; 35272082Seschrock char errbuf[1024]; 35282665Snd150628 prop_changelist_t *clp; 35292885Sahrens char chopprefix[ZFS_MAXNAMELEN]; 3530789Sahrens 3531789Sahrens begin_time = time(NULL); 3532789Sahrens 35332082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35342082Seschrock "cannot receive")); 35352082Seschrock 3536789Sahrens /* read in the BEGIN record */ 3537789Sahrens cp = (char *)&drr; 3538789Sahrens bytes = 0; 3539789Sahrens do { 35403504Sahl size = read(infd, cp, sizeof (drr) - bytes); 3541868Sahrens cp += size; 3542868Sahrens bytes += size; 3543868Sahrens } while (size > 0); 3544868Sahrens 3545868Sahrens if (size < 0 || bytes != sizeof (drr)) { 35462082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 35472082Seschrock "stream (failed to read first record)")); 35482082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3549789Sahrens } 3550789Sahrens 3551789Sahrens zc.zc_begin_record = drr.drr_u.drr_begin; 3552789Sahrens 3553789Sahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3554789Sahrens drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 35552082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 35562082Seschrock "stream (bad magic number)")); 35572082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3558789Sahrens } 3559789Sahrens 3560789Sahrens if (drrb->drr_version != DMU_BACKUP_VERSION && 3561789Sahrens drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 35622082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 35632082Seschrock "0x%llx is supported (stream is version 0x%llx)"), 3564789Sahrens DMU_BACKUP_VERSION, drrb->drr_version); 35652082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3566789Sahrens } 3567789Sahrens 35682885Sahrens if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 35692885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 35703912Slling "stream (bad snapshot name)")); 35712885Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 35722885Sahrens } 3573789Sahrens /* 35742885Sahrens * Determine how much of the snapshot name stored in the stream 35752885Sahrens * we are going to tack on to the name they specified on the 35762885Sahrens * command line, and how much we are going to chop off. 35772885Sahrens * 35782885Sahrens * If they specified a snapshot, chop the entire name stored in 35792885Sahrens * the stream. 3580789Sahrens */ 35812885Sahrens (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3582789Sahrens if (isprefix) { 35832885Sahrens /* 35842885Sahrens * They specified a fs with -d, we want to tack on 35852885Sahrens * everything but the pool name stored in the stream 35862885Sahrens */ 35872885Sahrens if (strchr(tosnap, '@')) { 35882885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 35892885Sahrens "argument - snapshot not allowed with -d")); 35902885Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3591789Sahrens } 35922885Sahrens cp = strchr(chopprefix, '/'); 3593789Sahrens if (cp == NULL) 35942885Sahrens cp = strchr(chopprefix, '@'); 35952885Sahrens *cp = '\0'; 3596789Sahrens } else if (strchr(tosnap, '@') == NULL) { 3597789Sahrens /* 35982885Sahrens * If they specified a filesystem without -d, we want to 35992885Sahrens * tack on everything after the fs specified in the 36002885Sahrens * first name from the stream. 3601789Sahrens */ 36022885Sahrens cp = strchr(chopprefix, '@'); 36032885Sahrens *cp = '\0'; 3604789Sahrens } 36052885Sahrens choplen = strlen(chopprefix); 36062885Sahrens 36072885Sahrens /* 36082885Sahrens * Determine name of destination snapshot, store in zc_value. 36092885Sahrens */ 36102885Sahrens (void) strcpy(zc.zc_value, tosnap); 36112885Sahrens (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 36122885Sahrens sizeof (zc.zc_value)); 36133265Sahrens if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT)) 36143265Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 36152885Sahrens 36162885Sahrens (void) strcpy(zc.zc_name, zc.zc_value); 3617789Sahrens if (drrb->drr_fromguid) { 3618789Sahrens /* incremental backup stream */ 36192885Sahrens zfs_handle_t *h; 36202885Sahrens 36212885Sahrens /* do the recvbackup ioctl to the containing fs */ 36222885Sahrens *strchr(zc.zc_name, '@') = '\0'; 3623789Sahrens 3624789Sahrens /* make sure destination fs exists */ 36252082Seschrock h = zfs_open(hdl, zc.zc_name, 36262082Seschrock ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 36272082Seschrock if (h == NULL) 3628789Sahrens return (-1); 3629868Sahrens if (!dryrun) { 36302665Snd150628 /* 36312665Snd150628 * We need to unmount all the dependents of the dataset 36322665Snd150628 * and the dataset itself. If it's a volume 36332665Snd150628 * then remove device link. 36342665Snd150628 */ 3635868Sahrens if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 36362665Snd150628 clp = changelist_gather(h, ZFS_PROP_NAME, 0); 36372665Snd150628 if (clp == NULL) 36382665Snd150628 return (-1); 36392665Snd150628 if (changelist_prefix(clp) != 0) { 36402665Snd150628 changelist_free(clp); 36412665Snd150628 return (-1); 36422665Snd150628 } 3643868Sahrens } else { 36444543Smarks if (zvol_remove_link(hdl, h->zfs_name) != 0) { 36454543Smarks zfs_close(h); 36464543Smarks return (-1); 36474543Smarks } 36484543Smarks 3649868Sahrens } 3650868Sahrens } 3651789Sahrens zfs_close(h); 3652789Sahrens } else { 3653789Sahrens /* full backup stream */ 3654789Sahrens 3655868Sahrens /* Make sure destination fs does not exist */ 36562885Sahrens *strchr(zc.zc_name, '@') = '\0'; 36574490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 36582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36592082Seschrock "destination '%s' exists"), zc.zc_name); 36602082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3661868Sahrens } 3662868Sahrens 36632885Sahrens if (strchr(zc.zc_name, '/') == NULL) { 36642885Sahrens /* 36652885Sahrens * they're trying to do a recv into a 36662885Sahrens * nonexistant topmost filesystem. 36672885Sahrens */ 36682885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36692885Sahrens "destination does not exist"), zc.zc_name); 36702885Sahrens return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 36712885Sahrens } 36722885Sahrens 3673868Sahrens /* Do the recvbackup ioctl to the fs's parent. */ 36742885Sahrens *strrchr(zc.zc_name, '/') = '\0'; 36752885Sahrens 36762885Sahrens if (isprefix && (err = create_parents(hdl, 36772885Sahrens zc.zc_value, strlen(tosnap))) != 0) { 36782885Sahrens return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 36792885Sahrens } 36802885Sahrens 3681789Sahrens } 3682789Sahrens 36833504Sahl zc.zc_cookie = infd; 36842676Seschrock zc.zc_guid = force; 3685789Sahrens if (verbose) { 36861749Sahrens (void) printf("%s %s stream of %s into %s\n", 36871749Sahrens dryrun ? "would receive" : "receiving", 3688789Sahrens drrb->drr_fromguid ? "incremental" : "full", 3689789Sahrens drr.drr_u.drr_begin.drr_toname, 36902676Seschrock zc.zc_value); 3691789Sahrens (void) fflush(stdout); 3692789Sahrens } 3693789Sahrens if (dryrun) 3694789Sahrens return (0); 36954543Smarks err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3696868Sahrens if (ioctl_err != 0) { 3697789Sahrens switch (errno) { 3698789Sahrens case ENODEV: 36992082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37002082Seschrock "most recent snapshot does not match incremental " 37012082Seschrock "source")); 37022082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3703789Sahrens break; 3704789Sahrens case ETXTBSY: 37052082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37062082Seschrock "destination has been modified since most recent " 37072082Seschrock "snapshot")); 37082082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3709789Sahrens break; 3710789Sahrens case EEXIST: 3711789Sahrens if (drrb->drr_fromguid == 0) { 3712789Sahrens /* it's the containing fs that exists */ 37132676Seschrock cp = strchr(zc.zc_value, '@'); 3714789Sahrens *cp = '\0'; 3715789Sahrens } 37162082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37172082Seschrock "destination already exists")); 37183237Slling (void) zfs_error_fmt(hdl, EZFS_EXISTS, 37193237Slling dgettext(TEXT_DOMAIN, "cannot restore to %s"), 37203237Slling zc.zc_value); 3721789Sahrens break; 3722789Sahrens case EINVAL: 37232082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3724868Sahrens break; 37251544Seschrock case ECKSUM: 37262082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37272082Seschrock "invalid stream (checksum mismatch)")); 37282082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3729789Sahrens break; 3730789Sahrens default: 37312082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3732789Sahrens } 3733789Sahrens } 3734789Sahrens 3735789Sahrens /* 3736868Sahrens * Mount or recreate the /dev links for the target filesystem 3737868Sahrens * (if created, or if we tore them down to do an incremental 3738868Sahrens * restore), and the /dev links for the new snapshot (if 37392665Snd150628 * created). Also mount any children of the target filesystem 37402665Snd150628 * if we did an incremental receive. 3741789Sahrens */ 37422676Seschrock cp = strchr(zc.zc_value, '@'); 3743868Sahrens if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3744789Sahrens zfs_handle_t *h; 3745789Sahrens 3746789Sahrens *cp = '\0'; 37472676Seschrock h = zfs_open(hdl, zc.zc_value, 3748789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3749868Sahrens *cp = '@'; 3750789Sahrens if (h) { 37512665Snd150628 if (h->zfs_type == ZFS_TYPE_VOLUME) { 37522082Seschrock err = zvol_create_link(hdl, h->zfs_name); 37531544Seschrock if (err == 0 && ioctl_err == 0) 37542082Seschrock err = zvol_create_link(hdl, 37552676Seschrock zc.zc_value); 37562665Snd150628 } else { 37572665Snd150628 if (drrb->drr_fromguid) { 37582665Snd150628 err = changelist_postfix(clp); 37592665Snd150628 changelist_free(clp); 37602665Snd150628 } else { 37612665Snd150628 err = zfs_mount(h, NULL, 0); 37622665Snd150628 } 3763868Sahrens } 37642665Snd150628 zfs_close(h); 3765789Sahrens } 3766789Sahrens } 3767789Sahrens 3768868Sahrens if (err || ioctl_err) 3769868Sahrens return (-1); 3770789Sahrens 3771789Sahrens if (verbose) { 3772789Sahrens char buf1[64]; 3773789Sahrens char buf2[64]; 3774789Sahrens uint64_t bytes = zc.zc_cookie; 3775789Sahrens time_t delta = time(NULL) - begin_time; 3776789Sahrens if (delta == 0) 3777789Sahrens delta = 1; 3778789Sahrens zfs_nicenum(bytes, buf1, sizeof (buf1)); 3779789Sahrens zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3780789Sahrens 37814603Sahrens (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 3782789Sahrens buf1, delta, buf2); 3783789Sahrens } 37842665Snd150628 3785789Sahrens return (0); 3786789Sahrens } 3787789Sahrens 3788789Sahrens /* 37891294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 37901294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 37911294Slling * is a dependent and we should just destroy it without checking the transaction 37921294Slling * group. 3793789Sahrens */ 37941294Slling typedef struct rollback_data { 37951294Slling const char *cb_target; /* the snapshot */ 37961294Slling uint64_t cb_create; /* creation time reference */ 37971294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 37981294Slling int cb_error; 37992082Seschrock boolean_t cb_dependent; 38001294Slling } rollback_data_t; 38011294Slling 38021294Slling static int 38031294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 38041294Slling { 38051294Slling rollback_data_t *cbp = data; 38061294Slling 38071294Slling if (!cbp->cb_dependent) { 38081294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 38091294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 38101294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 38111294Slling cbp->cb_create) { 38124543Smarks char *logstr; 38131294Slling 38142082Seschrock cbp->cb_dependent = B_TRUE; 38152474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 38162474Seschrock cbp) != 0) 38172474Seschrock cbp->cb_error = 1; 38182082Seschrock cbp->cb_dependent = B_FALSE; 38191294Slling 38204543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 38214543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 38221294Slling if (zfs_destroy(zhp) != 0) 38231294Slling cbp->cb_error = 1; 38241294Slling else 38251294Slling changelist_remove(zhp, cbp->cb_clp); 38264543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 38271294Slling } 38281294Slling } else { 38291294Slling if (zfs_destroy(zhp) != 0) 38301294Slling cbp->cb_error = 1; 38311294Slling else 38321294Slling changelist_remove(zhp, cbp->cb_clp); 38331294Slling } 38341294Slling 38351294Slling zfs_close(zhp); 38361294Slling return (0); 38371294Slling } 38381294Slling 38391294Slling /* 38401294Slling * Rollback the dataset to its latest snapshot. 38411294Slling */ 38421294Slling static int 38431294Slling do_rollback(zfs_handle_t *zhp) 3844789Sahrens { 3845789Sahrens int ret; 3846789Sahrens zfs_cmd_t zc = { 0 }; 3847789Sahrens 3848789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3849789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3850789Sahrens 3851789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 38522082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3853789Sahrens return (-1); 3854789Sahrens 3855789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3856789Sahrens 38572676Seschrock if (ZFS_IS_VOLUME(zhp)) 3858789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3859789Sahrens else 3860789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3861789Sahrens 3862789Sahrens /* 3863789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3864789Sahrens * for the given dataset. Given these constraints, we can simply pass 3865789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3866789Sahrens * condition where the user has taken a snapshot since we verified that 3867789Sahrens * this was the most recent. 3868789Sahrens */ 38694543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 38703237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 38712082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 38722082Seschrock zhp->zfs_name); 3873789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 38742082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3875789Sahrens } 3876789Sahrens 3877789Sahrens return (ret); 3878789Sahrens } 3879789Sahrens 3880789Sahrens /* 38811294Slling * Given a dataset, rollback to a specific snapshot, discarding any 38821294Slling * data changes since then and making it the active dataset. 38831294Slling * 38841294Slling * Any snapshots more recent than the target are destroyed, along with 38851294Slling * their dependents. 38861294Slling */ 38871294Slling int 38881294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 38891294Slling { 38901294Slling int ret; 38911294Slling rollback_data_t cb = { 0 }; 38921294Slling prop_changelist_t *clp; 38931294Slling 38941294Slling /* 38951294Slling * Unmount all dependendents of the dataset and the dataset itself. 38961294Slling * The list we need to gather is the same as for doing rename 38971294Slling */ 38981294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 38991294Slling if (clp == NULL) 39001294Slling return (-1); 39011294Slling 39021294Slling if ((ret = changelist_prefix(clp)) != 0) 39031294Slling goto out; 39041294Slling 39051294Slling /* 39061294Slling * Destroy all recent snapshots and its dependends. 39071294Slling */ 39081294Slling cb.cb_target = snap->zfs_name; 39091294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 39101294Slling cb.cb_clp = clp; 39111294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 39121294Slling 39131294Slling if ((ret = cb.cb_error) != 0) { 39141294Slling (void) changelist_postfix(clp); 39151294Slling goto out; 39161294Slling } 39171294Slling 39181294Slling /* 39191294Slling * Now that we have verified that the snapshot is the latest, 39201294Slling * rollback to the given snapshot. 39211294Slling */ 39221294Slling ret = do_rollback(zhp); 39231294Slling 39241294Slling if (ret != 0) { 39251294Slling (void) changelist_postfix(clp); 39261294Slling goto out; 39271294Slling } 39281294Slling 39291294Slling /* 39301294Slling * We only want to re-mount the filesystem if it was mounted in the 39311294Slling * first place. 39321294Slling */ 39331294Slling ret = changelist_postfix(clp); 39341294Slling 39351294Slling out: 39361294Slling changelist_free(clp); 39371294Slling return (ret); 39381294Slling } 39391294Slling 39401294Slling /* 3941789Sahrens * Iterate over all dependents for a given dataset. This includes both 3942789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3943789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3944789Sahrens * libzfs_graph.c. 3945789Sahrens */ 3946789Sahrens int 39472474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 39482474Seschrock zfs_iter_f func, void *data) 3949789Sahrens { 3950789Sahrens char **dependents; 3951789Sahrens size_t count; 3952789Sahrens int i; 3953789Sahrens zfs_handle_t *child; 3954789Sahrens int ret = 0; 3955789Sahrens 39562474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 39572474Seschrock &dependents, &count) != 0) 39582474Seschrock return (-1); 39592474Seschrock 3960789Sahrens for (i = 0; i < count; i++) { 39612082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 39622082Seschrock dependents[i])) == NULL) 3963789Sahrens continue; 3964789Sahrens 3965789Sahrens if ((ret = func(child, data)) != 0) 3966789Sahrens break; 3967789Sahrens } 3968789Sahrens 3969789Sahrens for (i = 0; i < count; i++) 3970789Sahrens free(dependents[i]); 3971789Sahrens free(dependents); 3972789Sahrens 3973789Sahrens return (ret); 3974789Sahrens } 3975789Sahrens 3976789Sahrens /* 3977789Sahrens * Renames the given dataset. 3978789Sahrens */ 3979789Sahrens int 39804490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3981789Sahrens { 3982789Sahrens int ret; 3983789Sahrens zfs_cmd_t zc = { 0 }; 3984789Sahrens char *delim; 39854007Smmusante prop_changelist_t *cl = NULL; 39864007Smmusante zfs_handle_t *zhrp = NULL; 39874007Smmusante char *parentname = NULL; 3988789Sahrens char parent[ZFS_MAXNAMELEN]; 39892082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 39902082Seschrock char errbuf[1024]; 3991789Sahrens 3992789Sahrens /* if we have the same exact name, just return success */ 3993789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3994789Sahrens return (0); 3995789Sahrens 39962082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 39972082Seschrock "cannot rename to '%s'"), target); 39982082Seschrock 3999789Sahrens /* 4000789Sahrens * Make sure the target name is valid 4001789Sahrens */ 4002789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 40032665Snd150628 if ((strchr(target, '@') == NULL) || 40042665Snd150628 *target == '@') { 40052665Snd150628 /* 40062665Snd150628 * Snapshot target name is abbreviated, 40072665Snd150628 * reconstruct full dataset name 40082665Snd150628 */ 40092665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 40102665Snd150628 sizeof (parent)); 40112665Snd150628 delim = strchr(parent, '@'); 40122665Snd150628 if (strchr(target, '@') == NULL) 40132665Snd150628 *(++delim) = '\0'; 40142665Snd150628 else 40152665Snd150628 *delim = '\0'; 40162665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 40172665Snd150628 target = parent; 40182665Snd150628 } else { 40192665Snd150628 /* 40202665Snd150628 * Make sure we're renaming within the same dataset. 40212665Snd150628 */ 40222665Snd150628 delim = strchr(target, '@'); 40232665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 40242665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 40252665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40262665Snd150628 "snapshots must be part of same " 40272665Snd150628 "dataset")); 40282665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 40293912Slling errbuf)); 40302665Snd150628 } 4031789Sahrens } 40322665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 40332665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4034789Sahrens } else { 40354007Smmusante if (recursive) { 40364007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40374007Smmusante "recursive rename must be a snapshot")); 40384007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 40394007Smmusante } 40404007Smmusante 40412665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 40422665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40432676Seschrock uint64_t unused; 40442676Seschrock 4045789Sahrens /* validate parents */ 40464490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4047789Sahrens return (-1); 4048789Sahrens 4049789Sahrens (void) parent_name(target, parent, sizeof (parent)); 4050789Sahrens 4051789Sahrens /* make sure we're in the same pool */ 4052789Sahrens verify((delim = strchr(target, '/')) != NULL); 4053789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4054789Sahrens zhp->zfs_name[delim - target] != '/') { 40552082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40562082Seschrock "datasets must be within same pool")); 40572082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4058789Sahrens } 40592440Snd150628 40602440Snd150628 /* new name cannot be a child of the current dataset name */ 40612440Snd150628 if (strncmp(parent, zhp->zfs_name, 40623912Slling strlen(zhp->zfs_name)) == 0) { 40632440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40642440Snd150628 "New dataset name cannot be a descendent of " 40652440Snd150628 "current dataset name")); 40662440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40672440Snd150628 } 4068789Sahrens } 4069789Sahrens 40702082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 40712082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 40722082Seschrock 4073789Sahrens if (getzoneid() == GLOBAL_ZONEID && 4074789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 40752082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40762082Seschrock "dataset is used in a non-global zone")); 40772082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4078789Sahrens } 4079789Sahrens 40804007Smmusante if (recursive) { 40814007Smmusante struct destroydata dd; 40824007Smmusante 40834183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 40844183Smmusante if (parentname == NULL) { 40854183Smmusante ret = -1; 40864183Smmusante goto error; 40874183Smmusante } 40884007Smmusante delim = strchr(parentname, '@'); 40894007Smmusante *delim = '\0'; 40904007Smmusante zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY); 40914007Smmusante if (zhrp == NULL) { 40924183Smmusante ret = -1; 40934183Smmusante goto error; 40944007Smmusante } 40954007Smmusante 40964007Smmusante dd.snapname = delim + 1; 40974007Smmusante dd.gotone = B_FALSE; 40984183Smmusante dd.closezhp = B_TRUE; 40994007Smmusante 41004007Smmusante /* We remove any zvol links prior to renaming them */ 41014007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 41024007Smmusante if (ret) { 41034007Smmusante goto error; 41044007Smmusante } 41054007Smmusante } else { 41064007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 41074007Smmusante return (-1); 41084007Smmusante 41094007Smmusante if (changelist_haszonedchild(cl)) { 41104007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41114007Smmusante "child dataset with inherited mountpoint is used " 41124007Smmusante "in a non-global zone")); 41134007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 41144007Smmusante goto error; 41154007Smmusante } 41164007Smmusante 41174007Smmusante if ((ret = changelist_prefix(cl)) != 0) 41184007Smmusante goto error; 4119789Sahrens } 4120789Sahrens 41212676Seschrock if (ZFS_IS_VOLUME(zhp)) 4122789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4123789Sahrens else 4124789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4125789Sahrens 41262665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 41272676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 41282665Snd150628 41294007Smmusante zc.zc_cookie = recursive; 41304007Smmusante 41314543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 41324007Smmusante /* 41334007Smmusante * if it was recursive, the one that actually failed will 41344007Smmusante * be in zc.zc_name 41354007Smmusante */ 41364007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 41374007Smmusante "cannot rename to '%s'"), zc.zc_name); 41384007Smmusante 41394007Smmusante if (recursive && errno == EEXIST) { 41404007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41414007Smmusante "a child dataset already has a snapshot " 41424007Smmusante "with the new name")); 41434801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 41444007Smmusante } else { 41454007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 41464007Smmusante } 4147789Sahrens 4148789Sahrens /* 4149789Sahrens * On failure, we still want to remount any filesystems that 4150789Sahrens * were previously mounted, so we don't alter the system state. 4151789Sahrens */ 41524007Smmusante if (recursive) { 41534007Smmusante struct createdata cd; 41544007Smmusante 41554007Smmusante /* only create links for datasets that had existed */ 41564007Smmusante cd.cd_snapname = delim + 1; 41574007Smmusante cd.cd_ifexists = B_TRUE; 41584007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41594007Smmusante &cd); 41604007Smmusante } else { 41614007Smmusante (void) changelist_postfix(cl); 41624007Smmusante } 4163789Sahrens } else { 41644007Smmusante if (recursive) { 41654007Smmusante struct createdata cd; 41664007Smmusante 41674007Smmusante /* only create links for datasets that had existed */ 41684007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 41694007Smmusante cd.cd_ifexists = B_TRUE; 41704007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41714007Smmusante &cd); 41724007Smmusante } else { 41734007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 41744007Smmusante ret = changelist_postfix(cl); 41754007Smmusante } 4176789Sahrens } 4177789Sahrens 4178789Sahrens error: 41794007Smmusante if (parentname) { 41804007Smmusante free(parentname); 41814007Smmusante } 41824007Smmusante if (zhrp) { 41834007Smmusante zfs_close(zhrp); 41844007Smmusante } 41854007Smmusante if (cl) { 41864007Smmusante changelist_free(cl); 41874007Smmusante } 4188789Sahrens return (ret); 4189789Sahrens } 4190789Sahrens 4191789Sahrens /* 4192789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4193789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 4194789Sahrens */ 4195789Sahrens int 41962082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4197789Sahrens { 41984007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 41994007Smmusante } 42004007Smmusante 42014007Smmusante static int 42024007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 42034007Smmusante { 4204789Sahrens zfs_cmd_t zc = { 0 }; 42052082Seschrock di_devlink_handle_t dhdl; 42064543Smarks priv_set_t *priv_effective; 42074543Smarks int privileged; 4208789Sahrens 4209789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4210789Sahrens 4211789Sahrens /* 4212789Sahrens * Issue the appropriate ioctl. 4213789Sahrens */ 42142082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4215789Sahrens switch (errno) { 4216789Sahrens case EEXIST: 4217789Sahrens /* 4218789Sahrens * Silently ignore the case where the link already 4219789Sahrens * exists. This allows 'zfs volinit' to be run multiple 4220789Sahrens * times without errors. 4221789Sahrens */ 4222789Sahrens return (0); 4223789Sahrens 42244007Smmusante case ENOENT: 42254007Smmusante /* 42264007Smmusante * Dataset does not exist in the kernel. If we 42274007Smmusante * don't care (see zfs_rename), then ignore the 42284007Smmusante * error quietly. 42294007Smmusante */ 42304007Smmusante if (ifexists) { 42314007Smmusante return (0); 42324007Smmusante } 42334007Smmusante 42344007Smmusante /* FALLTHROUGH */ 42354007Smmusante 4236789Sahrens default: 42373237Slling return (zfs_standard_error_fmt(hdl, errno, 42382082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 42392082Seschrock "for '%s'"), dataset)); 4240789Sahrens } 4241789Sahrens } 4242789Sahrens 4243789Sahrens /* 42444543Smarks * If privileged call devfsadm and wait for the links to 42454543Smarks * magically appear. 42464543Smarks * Otherwise, print out an informational message. 4247789Sahrens */ 42484543Smarks 42494543Smarks priv_effective = priv_allocset(); 42504543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 42514543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 42524543Smarks priv_freeset(priv_effective); 42534543Smarks 42544543Smarks if (privileged) { 42554543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 42564543Smarks DI_MAKE_LINK)) == NULL) { 42574543Smarks zfs_error_aux(hdl, strerror(errno)); 42584543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 42594543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 42604543Smarks "for '%s'"), dataset); 42614543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 42624543Smarks return (-1); 42634543Smarks } else { 42644543Smarks (void) di_devlink_fini(&dhdl); 42654543Smarks } 4266789Sahrens } else { 42674543Smarks char pathname[MAXPATHLEN]; 42684543Smarks struct stat64 statbuf; 42694543Smarks int i; 42704543Smarks 42714543Smarks #define MAX_WAIT 10 42724543Smarks 42734543Smarks /* 42744543Smarks * This is the poor mans way of waiting for the link 42754543Smarks * to show up. If after 10 seconds we still don't 42764543Smarks * have it, then print out a message. 42774543Smarks */ 42784543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 42794543Smarks dataset); 42804543Smarks 42814543Smarks for (i = 0; i != MAX_WAIT; i++) { 42824543Smarks if (stat64(pathname, &statbuf) == 0) 42834543Smarks break; 42844543Smarks (void) sleep(1); 42854543Smarks } 42864543Smarks if (i == MAX_WAIT) 42874543Smarks (void) printf(gettext("%s may not be immediately " 42884543Smarks "available\n"), pathname); 4289789Sahrens } 4290789Sahrens 4291789Sahrens return (0); 4292789Sahrens } 4293789Sahrens 4294789Sahrens /* 4295789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4296789Sahrens */ 4297789Sahrens int 42982082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4299789Sahrens { 4300789Sahrens zfs_cmd_t zc = { 0 }; 4301789Sahrens 4302789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4303789Sahrens 43042082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4305789Sahrens switch (errno) { 4306789Sahrens case ENXIO: 4307789Sahrens /* 4308789Sahrens * Silently ignore the case where the link no longer 4309789Sahrens * exists, so that 'zfs volfini' can be run multiple 4310789Sahrens * times without errors. 4311789Sahrens */ 4312789Sahrens return (0); 4313789Sahrens 4314789Sahrens default: 43153237Slling return (zfs_standard_error_fmt(hdl, errno, 43162082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 43172082Seschrock "links for '%s'"), dataset)); 4318789Sahrens } 4319789Sahrens } 4320789Sahrens 4321789Sahrens return (0); 4322789Sahrens } 43232676Seschrock 43242676Seschrock nvlist_t * 43252676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 43262676Seschrock { 43272676Seschrock return (zhp->zfs_user_props); 43282676Seschrock } 43292676Seschrock 43302676Seschrock /* 43314451Seschrock * Given a comma-separated list of properties, construct a property list 43322676Seschrock * containing both user-defined and native properties. This function will 43332676Seschrock * return a NULL list if 'all' is specified, which can later be expanded on a 43342676Seschrock * per-dataset basis by zfs_expand_proplist(). 43352676Seschrock */ 43362676Seschrock int 43373912Slling zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields, 43383912Slling zfs_proplist_t **listp, zfs_type_t type) 43392676Seschrock { 43402676Seschrock size_t len; 43412676Seschrock char *s, *p; 43422676Seschrock char c; 43432676Seschrock zfs_prop_t prop; 43442676Seschrock zfs_proplist_t *entry; 43452676Seschrock zfs_proplist_t **last; 43462676Seschrock 43472676Seschrock *listp = NULL; 43482676Seschrock last = listp; 43492676Seschrock 43502676Seschrock /* 43512676Seschrock * If 'all' is specified, return a NULL list. 43522676Seschrock */ 43532676Seschrock if (strcmp(fields, "all") == 0) 43542676Seschrock return (0); 43552676Seschrock 43562676Seschrock /* 43572676Seschrock * If no fields were specified, return an error. 43582676Seschrock */ 43592676Seschrock if (fields[0] == '\0') { 43602676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43612676Seschrock "no properties specified")); 43622676Seschrock return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 43632676Seschrock "bad property list"))); 43642676Seschrock } 43652676Seschrock 43662676Seschrock /* 43672676Seschrock * It would be nice to use getsubopt() here, but the inclusion of column 43682676Seschrock * aliases makes this more effort than it's worth. 43692676Seschrock */ 43702676Seschrock s = fields; 43712676Seschrock while (*s != '\0') { 43722676Seschrock if ((p = strchr(s, ',')) == NULL) { 43732676Seschrock len = strlen(s); 43742676Seschrock p = s + len; 43752676Seschrock } else { 43762676Seschrock len = p - s; 43772676Seschrock } 43782676Seschrock 43792676Seschrock /* 43802676Seschrock * Check for empty options. 43812676Seschrock */ 43822676Seschrock if (len == 0) { 43832676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43842676Seschrock "empty property name")); 43852676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 43862676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 43872676Seschrock } 43882676Seschrock 43892676Seschrock /* 43902676Seschrock * Check all regular property names. 43912676Seschrock */ 43922676Seschrock c = s[len]; 43932676Seschrock s[len] = '\0'; 43944451Seschrock prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) : 43954451Seschrock zfs_name_to_prop(s); 43963912Slling 43973912Slling if (prop != ZFS_PROP_INVAL && 43983912Slling !zfs_prop_valid_for_type(prop, type)) 43993912Slling prop = ZFS_PROP_INVAL; 44002676Seschrock 44012676Seschrock /* 44023912Slling * When no property table entry can be found, return failure if 44033912Slling * this is a pool property or if this isn't a user-defined 44043912Slling * dataset property, 44052676Seschrock */ 44063912Slling if (prop == ZFS_PROP_INVAL && 44073912Slling (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) { 44082676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44092676Seschrock "invalid property '%s'"), s); 44102676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 44112676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 44122676Seschrock } 44132676Seschrock 44142676Seschrock if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL) 44152676Seschrock return (-1); 44162676Seschrock 44172676Seschrock entry->pl_prop = prop; 44182676Seschrock if (prop == ZFS_PROP_INVAL) { 44192676Seschrock if ((entry->pl_user_prop = 44202676Seschrock zfs_strdup(hdl, s)) == NULL) { 44212676Seschrock free(entry); 44222676Seschrock return (-1); 44232676Seschrock } 44242676Seschrock entry->pl_width = strlen(s); 44252676Seschrock } else { 44262676Seschrock entry->pl_width = zfs_prop_width(prop, 44272676Seschrock &entry->pl_fixed); 44282676Seschrock } 44292676Seschrock 44302676Seschrock *last = entry; 44312676Seschrock last = &entry->pl_next; 44322676Seschrock 44332676Seschrock s = p; 44342676Seschrock if (c == ',') 44352676Seschrock s++; 44362676Seschrock } 44372676Seschrock 44382676Seschrock return (0); 44392676Seschrock } 44402676Seschrock 44413912Slling int 44423912Slling zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp) 44433912Slling { 44443912Slling return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY)); 44453912Slling } 44463912Slling 44472676Seschrock void 44482676Seschrock zfs_free_proplist(zfs_proplist_t *pl) 44492676Seschrock { 44502676Seschrock zfs_proplist_t *next; 44512676Seschrock 44522676Seschrock while (pl != NULL) { 44532676Seschrock next = pl->pl_next; 44542676Seschrock free(pl->pl_user_prop); 44552676Seschrock free(pl); 44562676Seschrock pl = next; 44572676Seschrock } 44582676Seschrock } 44592676Seschrock 44603654Sgw25295 typedef struct expand_data { 44613654Sgw25295 zfs_proplist_t **last; 44623654Sgw25295 libzfs_handle_t *hdl; 44633654Sgw25295 } expand_data_t; 44643654Sgw25295 44653654Sgw25295 static zfs_prop_t 44663654Sgw25295 zfs_expand_proplist_cb(zfs_prop_t prop, void *cb) 44673654Sgw25295 { 44683654Sgw25295 zfs_proplist_t *entry; 44693654Sgw25295 expand_data_t *edp = cb; 44703654Sgw25295 44713654Sgw25295 if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL) 44723654Sgw25295 return (ZFS_PROP_INVAL); 44733654Sgw25295 44743654Sgw25295 entry->pl_prop = prop; 44753654Sgw25295 entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed); 44763654Sgw25295 entry->pl_all = B_TRUE; 44773654Sgw25295 44783654Sgw25295 *(edp->last) = entry; 44793654Sgw25295 edp->last = &entry->pl_next; 44803654Sgw25295 44813654Sgw25295 return (ZFS_PROP_CONT); 44823654Sgw25295 } 44833654Sgw25295 44842676Seschrock int 44853912Slling zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp, 44863912Slling zfs_type_t type) 44872676Seschrock { 44882676Seschrock zfs_proplist_t *entry; 44893912Slling zfs_proplist_t **last; 44903654Sgw25295 expand_data_t exp; 44912676Seschrock 44922676Seschrock if (*plp == NULL) { 44932676Seschrock /* 44942676Seschrock * If this is the very first time we've been called for an 'all' 44952676Seschrock * specification, expand the list to include all native 44962676Seschrock * properties. 44972676Seschrock */ 44982676Seschrock last = plp; 44993654Sgw25295 45003654Sgw25295 exp.last = last; 45013654Sgw25295 exp.hdl = hdl; 45023654Sgw25295 45033912Slling if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type, 45044597Stimf B_FALSE, B_FALSE) == ZFS_PROP_INVAL) 45053654Sgw25295 return (-1); 45062676Seschrock 45072676Seschrock /* 45082676Seschrock * Add 'name' to the beginning of the list, which is handled 45092676Seschrock * specially. 45102676Seschrock */ 45112676Seschrock if ((entry = zfs_alloc(hdl, 45122676Seschrock sizeof (zfs_proplist_t))) == NULL) 45132676Seschrock return (-1); 45142676Seschrock 45152676Seschrock entry->pl_prop = ZFS_PROP_NAME; 45162676Seschrock entry->pl_width = zfs_prop_width(ZFS_PROP_NAME, 45172676Seschrock &entry->pl_fixed); 45182676Seschrock entry->pl_all = B_TRUE; 45192676Seschrock entry->pl_next = *plp; 45202676Seschrock *plp = entry; 45212676Seschrock } 45223912Slling return (0); 45233912Slling } 45243912Slling 45253912Slling /* 45263912Slling * This function is used by 'zfs list' to determine the exact set of columns to 45273912Slling * display, and their maximum widths. This does two main things: 45283912Slling * 45293912Slling * - If this is a list of all properties, then expand the list to include 45303912Slling * all native properties, and set a flag so that for each dataset we look 45313912Slling * for new unique user properties and add them to the list. 45323912Slling * 45333912Slling * - For non fixed-width properties, keep track of the maximum width seen 45343912Slling * so that we can size the column appropriately. 45353912Slling */ 45363912Slling int 45373912Slling zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp) 45383912Slling { 45393912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 45403912Slling zfs_proplist_t *entry; 45413912Slling zfs_proplist_t **last, **start; 45423912Slling nvlist_t *userprops, *propval; 45433912Slling nvpair_t *elem; 45443912Slling char *strval; 45453912Slling char buf[ZFS_MAXPROPLEN]; 45463912Slling 45473912Slling if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0) 45483912Slling return (-1); 45492676Seschrock 45502676Seschrock userprops = zfs_get_user_props(zhp); 45512676Seschrock 45522676Seschrock entry = *plp; 45532676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 45542676Seschrock /* 45552676Seschrock * Go through and add any user properties as necessary. We 45562676Seschrock * start by incrementing our list pointer to the first 45572676Seschrock * non-native property. 45582676Seschrock */ 45592676Seschrock start = plp; 45602676Seschrock while (*start != NULL) { 45612676Seschrock if ((*start)->pl_prop == ZFS_PROP_INVAL) 45622676Seschrock break; 45632676Seschrock start = &(*start)->pl_next; 45642676Seschrock } 45652676Seschrock 45662676Seschrock elem = NULL; 45672676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 45682676Seschrock /* 45692676Seschrock * See if we've already found this property in our list. 45702676Seschrock */ 45712676Seschrock for (last = start; *last != NULL; 45722676Seschrock last = &(*last)->pl_next) { 45732676Seschrock if (strcmp((*last)->pl_user_prop, 45742676Seschrock nvpair_name(elem)) == 0) 45752676Seschrock break; 45762676Seschrock } 45772676Seschrock 45782676Seschrock if (*last == NULL) { 45792676Seschrock if ((entry = zfs_alloc(hdl, 45802676Seschrock sizeof (zfs_proplist_t))) == NULL || 45812676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 45822676Seschrock nvpair_name(elem)))) == NULL) { 45832676Seschrock free(entry); 45842676Seschrock return (-1); 45852676Seschrock } 45862676Seschrock 45872676Seschrock entry->pl_prop = ZFS_PROP_INVAL; 45882676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 45892676Seschrock entry->pl_all = B_TRUE; 45902676Seschrock *last = entry; 45912676Seschrock } 45922676Seschrock } 45932676Seschrock } 45942676Seschrock 45952676Seschrock /* 45962676Seschrock * Now go through and check the width of any non-fixed columns 45972676Seschrock */ 45982676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 45992676Seschrock if (entry->pl_fixed) 46002676Seschrock continue; 46012676Seschrock 46022676Seschrock if (entry->pl_prop != ZFS_PROP_INVAL) { 46032676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 46042676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 46052676Seschrock if (strlen(buf) > entry->pl_width) 46062676Seschrock entry->pl_width = strlen(buf); 46072676Seschrock } 46082676Seschrock } else if (nvlist_lookup_nvlist(userprops, 46092676Seschrock entry->pl_user_prop, &propval) == 0) { 46102676Seschrock verify(nvlist_lookup_string(propval, 46112676Seschrock ZFS_PROP_VALUE, &strval) == 0); 46122676Seschrock if (strlen(strval) > entry->pl_width) 46132676Seschrock entry->pl_width = strlen(strval); 46142676Seschrock } 46152676Seschrock } 46162676Seschrock 46172676Seschrock return (0); 46182676Seschrock } 46194543Smarks 46204543Smarks int 46214543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 46224543Smarks { 46234543Smarks zfs_cmd_t zc = { 0 }; 46244543Smarks nvlist_t *nvp; 46254543Smarks size_t sz; 46264543Smarks gid_t gid; 46274543Smarks uid_t uid; 46284543Smarks const gid_t *groups; 46294543Smarks int group_cnt; 46304543Smarks int error; 46314543Smarks 46324543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 46334543Smarks return (no_memory(hdl)); 46344543Smarks 46354543Smarks uid = ucred_geteuid(cred); 46364543Smarks gid = ucred_getegid(cred); 46374543Smarks group_cnt = ucred_getgroups(cred, &groups); 46384543Smarks 46394543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 46404543Smarks return (1); 46414543Smarks 46424543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 46434543Smarks nvlist_free(nvp); 46444543Smarks return (1); 46454543Smarks } 46464543Smarks 46474543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 46484543Smarks nvlist_free(nvp); 46494543Smarks return (1); 46504543Smarks } 46514543Smarks 46524543Smarks if (nvlist_add_uint32_array(nvp, 46534543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 46544543Smarks nvlist_free(nvp); 46554543Smarks return (1); 46564543Smarks } 46574543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 46584543Smarks 46594543Smarks if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz)) 46604543Smarks return (-1); 46614543Smarks 46624543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 46634543Smarks nvlist_free(nvp); 46644543Smarks return (error); 46654543Smarks } 46664543Smarks 46674543Smarks int 46684543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 46694543Smarks void *export, void *sharetab, int sharemax, boolean_t share_on) 46704543Smarks { 46714543Smarks zfs_cmd_t zc = { 0 }; 46724543Smarks int error; 46734543Smarks 46744543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 46754543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 46764543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 46774543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 46784543Smarks zc.zc_share.z_sharetype = share_on; 46794543Smarks zc.zc_share.z_sharemax = sharemax; 46804543Smarks 46814543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 46824543Smarks return (error); 46834543Smarks } 4684