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 */ 5014849Sahrens int 5024849Sahrens 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 5744849Sahrens * 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; 5934849Sahrens } 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 19574849Sahrens 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 20094849Sahrens 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: 2428*4861Sahrens if (get_numeric_property(zhp, prop, src, 2429*4861Sahrens &source, &val) != 0) 2430*4861Sahrens return (-1); 2431*4861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 24324577Sahrens return (-1); 24334577Sahrens (void) strlcpy(propbuf, strval, proplen); 24344577Sahrens break; 24354577Sahrens 24364577Sahrens default: 24374577Sahrens abort(); 24384577Sahrens } 2439789Sahrens } 2440789Sahrens 2441789Sahrens get_source(zhp, src, source, statbuf, statlen); 2442789Sahrens 2443789Sahrens return (0); 2444789Sahrens } 2445789Sahrens 2446789Sahrens /* 2447789Sahrens * Utility function to get the given numeric property. Does no validation that 2448789Sahrens * the given property is the appropriate type; should only be used with 2449789Sahrens * hard-coded property types. 2450789Sahrens */ 2451789Sahrens uint64_t 2452789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2453789Sahrens { 2454789Sahrens char *source; 2455789Sahrens zfs_source_t sourcetype = ZFS_SRC_NONE; 24562082Seschrock uint64_t val; 24572082Seschrock 24582082Seschrock (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 24592082Seschrock 24602082Seschrock return (val); 2461789Sahrens } 2462789Sahrens 2463789Sahrens /* 2464789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2465789Sahrens */ 2466789Sahrens int 2467789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2468789Sahrens zfs_source_t *src, char *statbuf, size_t statlen) 2469789Sahrens { 2470789Sahrens char *source; 2471789Sahrens 2472789Sahrens /* 2473789Sahrens * Check to see if this property applies to our object 2474789Sahrens */ 24754849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 24763237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 24772082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 24782082Seschrock zfs_prop_to_name(prop))); 24794849Sahrens } 2480789Sahrens 2481789Sahrens if (src) 2482789Sahrens *src = ZFS_SRC_NONE; 2483789Sahrens 24842082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 24852082Seschrock return (-1); 2486789Sahrens 2487789Sahrens get_source(zhp, src, source, statbuf, statlen); 2488789Sahrens 2489789Sahrens return (0); 2490789Sahrens } 2491789Sahrens 2492789Sahrens /* 2493789Sahrens * Returns the name of the given zfs handle. 2494789Sahrens */ 2495789Sahrens const char * 2496789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2497789Sahrens { 2498789Sahrens return (zhp->zfs_name); 2499789Sahrens } 2500789Sahrens 2501789Sahrens /* 2502789Sahrens * Returns the type of the given zfs handle. 2503789Sahrens */ 2504789Sahrens zfs_type_t 2505789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2506789Sahrens { 2507789Sahrens return (zhp->zfs_type); 2508789Sahrens } 2509789Sahrens 2510789Sahrens /* 25111356Seschrock * Iterate over all child filesystems 2512789Sahrens */ 2513789Sahrens int 25141356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2515789Sahrens { 2516789Sahrens zfs_cmd_t zc = { 0 }; 2517789Sahrens zfs_handle_t *nzhp; 2518789Sahrens int ret; 2519789Sahrens 2520789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 25212082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2522789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2523789Sahrens /* 2524789Sahrens * Ignore private dataset names. 2525789Sahrens */ 2526789Sahrens if (dataset_name_hidden(zc.zc_name)) 2527789Sahrens continue; 2528789Sahrens 2529789Sahrens /* 2530789Sahrens * Silently ignore errors, as the only plausible explanation is 2531789Sahrens * that the pool has since been removed. 2532789Sahrens */ 25332082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 25342082Seschrock zc.zc_name)) == NULL) 2535789Sahrens continue; 2536789Sahrens 2537789Sahrens if ((ret = func(nzhp, data)) != 0) 2538789Sahrens return (ret); 2539789Sahrens } 2540789Sahrens 2541789Sahrens /* 2542789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2543789Sahrens * returned, then the underlying dataset has been removed since we 2544789Sahrens * obtained the handle. 2545789Sahrens */ 2546789Sahrens if (errno != ESRCH && errno != ENOENT) 25472082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 25482082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2549789Sahrens 25501356Seschrock return (0); 25511356Seschrock } 25521356Seschrock 25531356Seschrock /* 25541356Seschrock * Iterate over all snapshots 25551356Seschrock */ 25561356Seschrock int 25571356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 25581356Seschrock { 25591356Seschrock zfs_cmd_t zc = { 0 }; 25601356Seschrock zfs_handle_t *nzhp; 25611356Seschrock int ret; 2562789Sahrens 2563789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 25642082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 25652082Seschrock &zc) == 0; 2566789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2567789Sahrens 25682082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 25692082Seschrock zc.zc_name)) == NULL) 2570789Sahrens continue; 2571789Sahrens 2572789Sahrens if ((ret = func(nzhp, data)) != 0) 2573789Sahrens return (ret); 2574789Sahrens } 2575789Sahrens 2576789Sahrens /* 2577789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2578789Sahrens * returned, then the underlying dataset has been removed since we 2579789Sahrens * obtained the handle. Silently ignore this case, and return success. 2580789Sahrens */ 2581789Sahrens if (errno != ESRCH && errno != ENOENT) 25822082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 25832082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2584789Sahrens 2585789Sahrens return (0); 2586789Sahrens } 2587789Sahrens 2588789Sahrens /* 25891356Seschrock * Iterate over all children, snapshots and filesystems 25901356Seschrock */ 25911356Seschrock int 25921356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 25931356Seschrock { 25941356Seschrock int ret; 25951356Seschrock 25961356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 25971356Seschrock return (ret); 25981356Seschrock 25991356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 26001356Seschrock } 26011356Seschrock 26021356Seschrock /* 2603789Sahrens * Given a complete name, return just the portion that refers to the parent. 2604789Sahrens * Can return NULL if this is a pool. 2605789Sahrens */ 2606789Sahrens static int 2607789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2608789Sahrens { 2609789Sahrens char *loc; 2610789Sahrens 2611789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2612789Sahrens return (-1); 2613789Sahrens 2614789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2615789Sahrens buf[loc - path] = '\0'; 2616789Sahrens 2617789Sahrens return (0); 2618789Sahrens } 2619789Sahrens 2620789Sahrens /* 26214490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 26224490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 26234490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 26244490Svb160487 * length of already existing prefix of the given path. We also fetch the 26254490Svb160487 * 'zoned' property, which is used to validate property settings when creating 26264490Svb160487 * new datasets. 2627789Sahrens */ 2628789Sahrens static int 26294490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 26304490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2631789Sahrens { 2632789Sahrens zfs_cmd_t zc = { 0 }; 2633789Sahrens char parent[ZFS_MAXNAMELEN]; 2634789Sahrens char *slash; 26351356Seschrock zfs_handle_t *zhp; 26362082Seschrock char errbuf[1024]; 26372082Seschrock 26382082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 26392082Seschrock path); 2640789Sahrens 2641789Sahrens /* get parent, and check to see if this is just a pool */ 2642789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 26432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26442082Seschrock "missing dataset name")); 26452082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2646789Sahrens } 2647789Sahrens 2648789Sahrens /* check to see if the pool exists */ 2649789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2650789Sahrens slash = parent + strlen(parent); 2651789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2652789Sahrens zc.zc_name[slash - parent] = '\0'; 26532082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2654789Sahrens errno == ENOENT) { 26552082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26562082Seschrock "no such pool '%s'"), zc.zc_name); 26572082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2658789Sahrens } 2659789Sahrens 2660789Sahrens /* check to see if the parent dataset exists */ 26614490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 26624490Svb160487 if (errno == ENOENT && accept_ancestor) { 26634490Svb160487 /* 26644490Svb160487 * Go deeper to find an ancestor, give up on top level. 26654490Svb160487 */ 26664490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 26674490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26684490Svb160487 "no such pool '%s'"), zc.zc_name); 26694490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 26704490Svb160487 } 26714490Svb160487 } else if (errno == ENOENT) { 26722082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26732082Seschrock "parent does not exist")); 26742082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 26754490Svb160487 } else 26762082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2677789Sahrens } 2678789Sahrens 26792676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2680789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 26812676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 26822082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 26831356Seschrock zfs_close(zhp); 2684789Sahrens return (-1); 2685789Sahrens } 2686789Sahrens 2687789Sahrens /* make sure parent is a filesystem */ 26881356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 26892082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26902082Seschrock "parent is not a filesystem")); 26912082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 26921356Seschrock zfs_close(zhp); 2693789Sahrens return (-1); 2694789Sahrens } 2695789Sahrens 26961356Seschrock zfs_close(zhp); 26974490Svb160487 if (prefixlen != NULL) 26984490Svb160487 *prefixlen = strlen(parent); 26994490Svb160487 return (0); 27004490Svb160487 } 27014490Svb160487 27024490Svb160487 /* 27034490Svb160487 * Finds whether the dataset of the given type(s) exists. 27044490Svb160487 */ 27054490Svb160487 boolean_t 27064490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 27074490Svb160487 { 27084490Svb160487 zfs_handle_t *zhp; 27094490Svb160487 27104490Svb160487 if (!zfs_validate_name(hdl, path, types)) 27114490Svb160487 return (B_FALSE); 27124490Svb160487 27134490Svb160487 /* 27144490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 27154490Svb160487 */ 27164490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 27174490Svb160487 int ds_type = zhp->zfs_type; 27184490Svb160487 27194490Svb160487 zfs_close(zhp); 27204490Svb160487 if (types & ds_type) 27214490Svb160487 return (B_TRUE); 27224490Svb160487 } 27234490Svb160487 return (B_FALSE); 27244490Svb160487 } 27254490Svb160487 27264490Svb160487 /* 27274490Svb160487 * Creates non-existing ancestors of the given path. 27284490Svb160487 */ 27294490Svb160487 int 27304490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 27314490Svb160487 { 27324490Svb160487 int prefix; 27334490Svb160487 uint64_t zoned; 27344490Svb160487 char *path_copy; 27354490Svb160487 int rc; 27364490Svb160487 27374490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 27384490Svb160487 return (-1); 27394490Svb160487 27404490Svb160487 if ((path_copy = strdup(path)) != NULL) { 27414490Svb160487 rc = create_parents(hdl, path_copy, prefix); 27424490Svb160487 free(path_copy); 27434490Svb160487 } 27444490Svb160487 if (path_copy == NULL || rc != 0) 27454490Svb160487 return (-1); 27464490Svb160487 2747789Sahrens return (0); 2748789Sahrens } 2749789Sahrens 2750789Sahrens /* 27512676Seschrock * Create a new filesystem or volume. 2752789Sahrens */ 2753789Sahrens int 27542082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 27552676Seschrock nvlist_t *props) 2756789Sahrens { 2757789Sahrens zfs_cmd_t zc = { 0 }; 2758789Sahrens int ret; 2759789Sahrens uint64_t size = 0; 2760789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 27612082Seschrock char errbuf[1024]; 27622676Seschrock uint64_t zoned; 27632082Seschrock 27642082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 27652082Seschrock "cannot create '%s'"), path); 2766789Sahrens 2767789Sahrens /* validate the path, taking care to note the extended error message */ 27682082Seschrock if (!zfs_validate_name(hdl, path, type)) 27692082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2770789Sahrens 2771789Sahrens /* validate parents exist */ 27724490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2773789Sahrens return (-1); 2774789Sahrens 2775789Sahrens /* 2776789Sahrens * The failure modes when creating a dataset of a different type over 2777789Sahrens * one that already exists is a little strange. In particular, if you 2778789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2779789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2780789Sahrens * first try to see if the dataset exists. 2781789Sahrens */ 2782789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 27834490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 27842082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27852082Seschrock "dataset already exists")); 27862082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2787789Sahrens } 2788789Sahrens 2789789Sahrens if (type == ZFS_TYPE_VOLUME) 2790789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2791789Sahrens else 2792789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2793789Sahrens 27943912Slling if (props && (props = zfs_validate_properties(hdl, type, NULL, props, 27953912Slling zoned, NULL, errbuf)) == 0) 27962676Seschrock return (-1); 27972676Seschrock 2798789Sahrens if (type == ZFS_TYPE_VOLUME) { 27991133Seschrock /* 28001133Seschrock * If we are creating a volume, the size and block size must 28011133Seschrock * satisfy a few restraints. First, the blocksize must be a 28021133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 28031133Seschrock * volsize must be a multiple of the block size, and cannot be 28041133Seschrock * zero. 28051133Seschrock */ 28062676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 28072676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 28082676Seschrock nvlist_free(props); 28092082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28102676Seschrock "missing volume size")); 28112676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2812789Sahrens } 2813789Sahrens 28142676Seschrock if ((ret = nvlist_lookup_uint64(props, 28152676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 28162676Seschrock &blocksize)) != 0) { 28172676Seschrock if (ret == ENOENT) { 28182676Seschrock blocksize = zfs_prop_default_numeric( 28192676Seschrock ZFS_PROP_VOLBLOCKSIZE); 28202676Seschrock } else { 28212676Seschrock nvlist_free(props); 28222676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28232676Seschrock "missing volume block size")); 28242676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28252676Seschrock } 28262676Seschrock } 28272676Seschrock 28282676Seschrock if (size == 0) { 28292676Seschrock nvlist_free(props); 28302082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28312676Seschrock "volume size cannot be zero")); 28322676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28331133Seschrock } 28341133Seschrock 28351133Seschrock if (size % blocksize != 0) { 28362676Seschrock nvlist_free(props); 28372082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28382676Seschrock "volume size must be a multiple of volume block " 28392676Seschrock "size")); 28402676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28411133Seschrock } 2842789Sahrens } 2843789Sahrens 28442676Seschrock if (props && 28452676Seschrock zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) 28462676Seschrock return (-1); 28472676Seschrock nvlist_free(props); 28482676Seschrock 2849789Sahrens /* create the dataset */ 28504543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2851789Sahrens 28523912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 28532082Seschrock ret = zvol_create_link(hdl, path); 28543912Slling if (ret) { 28553912Slling (void) zfs_standard_error(hdl, errno, 28563912Slling dgettext(TEXT_DOMAIN, 28573912Slling "Volume successfully created, but device links " 28583912Slling "were not created")); 28593912Slling zcmd_free_nvlists(&zc); 28603912Slling return (-1); 28613912Slling } 28623912Slling } 2863789Sahrens 28642676Seschrock zcmd_free_nvlists(&zc); 28652676Seschrock 2866789Sahrens /* check for failure */ 2867789Sahrens if (ret != 0) { 2868789Sahrens char parent[ZFS_MAXNAMELEN]; 2869789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2870789Sahrens 2871789Sahrens switch (errno) { 2872789Sahrens case ENOENT: 28732082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28742082Seschrock "no such parent '%s'"), parent); 28752082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2876789Sahrens 2877789Sahrens case EINVAL: 28782082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28793413Smmusante "parent '%s' is not a filesystem"), parent); 28802082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2881789Sahrens 2882789Sahrens case EDOM: 28832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28842676Seschrock "volume block size must be power of 2 from " 28852676Seschrock "%u to %uk"), 2886789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2887789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 28882082Seschrock 28892676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28902082Seschrock 28914603Sahrens case ENOTSUP: 28924603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28934603Sahrens "pool must be upgraded to set this " 28944603Sahrens "property or value")); 28954603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 28964603Sahrens 2897789Sahrens #ifdef _ILP32 2898789Sahrens case EOVERFLOW: 2899789Sahrens /* 2900789Sahrens * This platform can't address a volume this big. 2901789Sahrens */ 29022082Seschrock if (type == ZFS_TYPE_VOLUME) 29032082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 29042082Seschrock errbuf)); 2905789Sahrens #endif 29062082Seschrock /* FALLTHROUGH */ 2907789Sahrens default: 29082082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2909789Sahrens } 2910789Sahrens } 2911789Sahrens 2912789Sahrens return (0); 2913789Sahrens } 2914789Sahrens 2915789Sahrens /* 2916789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2917789Sahrens * isn't mounted, and that there are no active dependents. 2918789Sahrens */ 2919789Sahrens int 2920789Sahrens zfs_destroy(zfs_handle_t *zhp) 2921789Sahrens { 2922789Sahrens zfs_cmd_t zc = { 0 }; 2923789Sahrens 2924789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2925789Sahrens 29262676Seschrock if (ZFS_IS_VOLUME(zhp)) { 29273126Sahl /* 29284543Smarks * If user doesn't have permissions to unshare volume, then 29294543Smarks * abort the request. This would only happen for a 29304543Smarks * non-privileged user. 29313126Sahl */ 29324543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 29334543Smarks return (-1); 29344543Smarks } 29353126Sahl 29362082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2937789Sahrens return (-1); 2938789Sahrens 2939789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2940789Sahrens } else { 2941789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2942789Sahrens } 2943789Sahrens 29444543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 29453237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 29462082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 29472082Seschrock zhp->zfs_name)); 29482199Sahrens } 2949789Sahrens 2950789Sahrens remove_mountpoint(zhp); 2951789Sahrens 2952789Sahrens return (0); 2953789Sahrens } 2954789Sahrens 29552199Sahrens struct destroydata { 29562199Sahrens char *snapname; 29572199Sahrens boolean_t gotone; 29583265Sahrens boolean_t closezhp; 29592199Sahrens }; 29602199Sahrens 29612199Sahrens static int 29622199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 29632199Sahrens { 29642199Sahrens struct destroydata *dd = arg; 29652199Sahrens zfs_handle_t *szhp; 29662199Sahrens char name[ZFS_MAXNAMELEN]; 29673265Sahrens boolean_t closezhp = dd->closezhp; 29683265Sahrens int rv; 29692199Sahrens 29702676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 29712676Seschrock (void) strlcat(name, "@", sizeof (name)); 29722676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 29732199Sahrens 29742199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 29752199Sahrens if (szhp) { 29762199Sahrens dd->gotone = B_TRUE; 29772199Sahrens zfs_close(szhp); 29782199Sahrens } 29792199Sahrens 29802199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 29812199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 29822199Sahrens /* 29832199Sahrens * NB: this is simply a best-effort. We don't want to 29842199Sahrens * return an error, because then we wouldn't visit all 29852199Sahrens * the volumes. 29862199Sahrens */ 29872199Sahrens } 29882199Sahrens 29893265Sahrens dd->closezhp = B_TRUE; 29903265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 29913265Sahrens if (closezhp) 29923265Sahrens zfs_close(zhp); 29933265Sahrens return (rv); 29942199Sahrens } 29952199Sahrens 29962199Sahrens /* 29972199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 29982199Sahrens */ 29992199Sahrens int 30002199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 30012199Sahrens { 30022199Sahrens zfs_cmd_t zc = { 0 }; 30032199Sahrens int ret; 30042199Sahrens struct destroydata dd = { 0 }; 30052199Sahrens 30062199Sahrens dd.snapname = snapname; 30072199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 30082199Sahrens 30092199Sahrens if (!dd.gotone) { 30103237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 30112199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 30122199Sahrens zhp->zfs_name, snapname)); 30132199Sahrens } 30142199Sahrens 30152199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 30162676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 30172199Sahrens 30184543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 30192199Sahrens if (ret != 0) { 30202199Sahrens char errbuf[1024]; 30212199Sahrens 30222199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30232199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 30242199Sahrens 30252199Sahrens switch (errno) { 30262199Sahrens case EEXIST: 30272199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30282199Sahrens "snapshot is cloned")); 30292199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 30302199Sahrens 30312199Sahrens default: 30322199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 30332199Sahrens errbuf)); 30342199Sahrens } 30352199Sahrens } 30362199Sahrens 30372199Sahrens return (0); 30382199Sahrens } 30392199Sahrens 3040789Sahrens /* 3041789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3042789Sahrens */ 3043789Sahrens int 30442676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3045789Sahrens { 3046789Sahrens zfs_cmd_t zc = { 0 }; 3047789Sahrens char parent[ZFS_MAXNAMELEN]; 3048789Sahrens int ret; 30492082Seschrock char errbuf[1024]; 30502082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 30512676Seschrock zfs_type_t type; 30522676Seschrock uint64_t zoned; 3053789Sahrens 3054789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3055789Sahrens 30562082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30572082Seschrock "cannot create '%s'"), target); 30582082Seschrock 3059789Sahrens /* validate the target name */ 30602082Seschrock if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM)) 30612082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3062789Sahrens 3063789Sahrens /* validate parents exist */ 30644490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3065789Sahrens return (-1); 3066789Sahrens 3067789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3068789Sahrens 3069789Sahrens /* do the clone */ 30702676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3071789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 30722744Snn35248 type = ZFS_TYPE_VOLUME; 30732676Seschrock } else { 3074789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 30752744Snn35248 type = ZFS_TYPE_FILESYSTEM; 30762676Seschrock } 30772676Seschrock 30782676Seschrock if (props) { 30793912Slling if ((props = zfs_validate_properties(hdl, type, NULL, props, 30803912Slling zoned, zhp, errbuf)) == NULL) 30812676Seschrock return (-1); 30822676Seschrock 30832676Seschrock if (zcmd_write_src_nvlist(hdl, &zc, props, NULL) != 0) { 30842676Seschrock nvlist_free(props); 30852676Seschrock return (-1); 30862676Seschrock } 30872676Seschrock 30882676Seschrock nvlist_free(props); 30892676Seschrock } 3090789Sahrens 3091789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 30922676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 30934543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3094789Sahrens 30952676Seschrock zcmd_free_nvlists(&zc); 30962676Seschrock 3097789Sahrens if (ret != 0) { 3098789Sahrens switch (errno) { 3099789Sahrens 3100789Sahrens case ENOENT: 3101789Sahrens /* 3102789Sahrens * The parent doesn't exist. We should have caught this 3103789Sahrens * above, but there may a race condition that has since 3104789Sahrens * destroyed the parent. 3105789Sahrens * 3106789Sahrens * At this point, we don't know whether it's the source 3107789Sahrens * that doesn't exist anymore, or whether the target 3108789Sahrens * dataset doesn't exist. 3109789Sahrens */ 31102082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31112082Seschrock "no such parent '%s'"), parent); 31122082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 31132082Seschrock 31142082Seschrock case EXDEV: 31152082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31162082Seschrock "source and target pools differ")); 31172082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 31182082Seschrock errbuf)); 31192082Seschrock 31202082Seschrock default: 31212082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 31222082Seschrock errbuf)); 31232082Seschrock } 31242676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 31252082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 31262082Seschrock } 31272082Seschrock 31282082Seschrock return (ret); 31292082Seschrock } 31302082Seschrock 31312082Seschrock typedef struct promote_data { 31322082Seschrock char cb_mountpoint[MAXPATHLEN]; 31332082Seschrock const char *cb_target; 31342082Seschrock const char *cb_errbuf; 31352082Seschrock uint64_t cb_pivot_txg; 31362082Seschrock } promote_data_t; 31372082Seschrock 31382082Seschrock static int 31392082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 31402082Seschrock { 31412082Seschrock promote_data_t *pd = data; 31422082Seschrock zfs_handle_t *szhp; 31432082Seschrock char snapname[MAXPATHLEN]; 31443265Sahrens int rv = 0; 31452082Seschrock 31462082Seschrock /* We don't care about snapshots after the pivot point */ 31473265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 31483265Sahrens zfs_close(zhp); 31492082Seschrock return (0); 31503265Sahrens } 31512082Seschrock 31522417Sahrens /* Remove the device link if it's a zvol. */ 31532676Seschrock if (ZFS_IS_VOLUME(zhp)) 31542417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 31552082Seschrock 31562082Seschrock /* Check for conflicting names */ 31572676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 31582676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 31592082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 31602082Seschrock if (szhp != NULL) { 31612082Seschrock zfs_close(szhp); 31622082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31632082Seschrock "snapshot name '%s' from origin \n" 31642082Seschrock "conflicts with '%s' from target"), 31652082Seschrock zhp->zfs_name, snapname); 31663265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 31672082Seschrock } 31683265Sahrens zfs_close(zhp); 31693265Sahrens return (rv); 31702082Seschrock } 31712082Seschrock 31722417Sahrens static int 31732417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 31742417Sahrens { 31752417Sahrens promote_data_t *pd = data; 31762417Sahrens 31772417Sahrens /* We don't care about snapshots after the pivot point */ 31783265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 31793265Sahrens /* Create the device link if it's a zvol. */ 31803265Sahrens if (ZFS_IS_VOLUME(zhp)) 31813265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 31823265Sahrens } 31833265Sahrens 31843265Sahrens zfs_close(zhp); 31852417Sahrens return (0); 31862417Sahrens } 31872417Sahrens 31882082Seschrock /* 31892082Seschrock * Promotes the given clone fs to be the clone parent. 31902082Seschrock */ 31912082Seschrock int 31922082Seschrock zfs_promote(zfs_handle_t *zhp) 31932082Seschrock { 31942082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 31952082Seschrock zfs_cmd_t zc = { 0 }; 31962082Seschrock char parent[MAXPATHLEN]; 31972082Seschrock char *cp; 31982082Seschrock int ret; 31992082Seschrock zfs_handle_t *pzhp; 32002082Seschrock promote_data_t pd; 32012082Seschrock char errbuf[1024]; 32022082Seschrock 32032082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32042082Seschrock "cannot promote '%s'"), zhp->zfs_name); 32052082Seschrock 32062082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 32072082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32082082Seschrock "snapshots can not be promoted")); 32092082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 32102082Seschrock } 32112082Seschrock 32122676Seschrock (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 32132082Seschrock if (parent[0] == '\0') { 32142082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32152082Seschrock "not a cloned filesystem")); 32162082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 32172082Seschrock } 32182082Seschrock cp = strchr(parent, '@'); 32192082Seschrock *cp = '\0'; 32202082Seschrock 32212082Seschrock /* Walk the snapshots we will be moving */ 32222082Seschrock pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 32232082Seschrock if (pzhp == NULL) 32242082Seschrock return (-1); 32252082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 32262082Seschrock zfs_close(pzhp); 32272082Seschrock pd.cb_target = zhp->zfs_name; 32282082Seschrock pd.cb_errbuf = errbuf; 32292082Seschrock pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY); 32302082Seschrock if (pzhp == NULL) 32312082Seschrock return (-1); 32322082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 32332082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 32342082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 32352417Sahrens if (ret != 0) { 32362417Sahrens zfs_close(pzhp); 32372082Seschrock return (-1); 32382417Sahrens } 32392082Seschrock 32402082Seschrock /* issue the ioctl */ 32412676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 32422676Seschrock sizeof (zc.zc_value)); 32432082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 32444543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 32452082Seschrock 32462082Seschrock if (ret != 0) { 32472417Sahrens int save_errno = errno; 32482417Sahrens 32492417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 32502417Sahrens zfs_close(pzhp); 32512417Sahrens 32522417Sahrens switch (save_errno) { 3253789Sahrens case EEXIST: 3254789Sahrens /* 32552082Seschrock * There is a conflicting snapshot name. We 32562082Seschrock * should have caught this above, but they could 32572082Seschrock * have renamed something in the mean time. 3258789Sahrens */ 32592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32602082Seschrock "conflicting snapshot name from parent '%s'"), 32612082Seschrock parent); 32622082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3263789Sahrens 3264789Sahrens default: 32652417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3266789Sahrens } 32672417Sahrens } else { 32682417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3269789Sahrens } 3270789Sahrens 32712417Sahrens zfs_close(pzhp); 3272789Sahrens return (ret); 3273789Sahrens } 3274789Sahrens 32754007Smmusante struct createdata { 32764007Smmusante const char *cd_snapname; 32774007Smmusante int cd_ifexists; 32784007Smmusante }; 32794007Smmusante 32802199Sahrens static int 32812199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 32822199Sahrens { 32834007Smmusante struct createdata *cd = arg; 32842676Seschrock int ret; 32852199Sahrens 32862199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 32872199Sahrens char name[MAXPATHLEN]; 32882199Sahrens 32892676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 32902676Seschrock (void) strlcat(name, "@", sizeof (name)); 32914007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 32924007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 32934007Smmusante cd->cd_ifexists); 32942199Sahrens /* 32952199Sahrens * NB: this is simply a best-effort. We don't want to 32962199Sahrens * return an error, because then we wouldn't visit all 32972199Sahrens * the volumes. 32982199Sahrens */ 32992199Sahrens } 33002676Seschrock 33014007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 33022676Seschrock 33032676Seschrock zfs_close(zhp); 33042676Seschrock 33052676Seschrock return (ret); 33062199Sahrens } 33072199Sahrens 3308789Sahrens /* 33093504Sahl * Takes a snapshot of the given dataset. 3310789Sahrens */ 3311789Sahrens int 33122199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3313789Sahrens { 3314789Sahrens const char *delim; 3315789Sahrens char *parent; 3316789Sahrens zfs_handle_t *zhp; 3317789Sahrens zfs_cmd_t zc = { 0 }; 3318789Sahrens int ret; 33192082Seschrock char errbuf[1024]; 33202082Seschrock 33212082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33222082Seschrock "cannot snapshot '%s'"), path); 33232082Seschrock 33242082Seschrock /* validate the target name */ 33252082Seschrock if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT)) 33262082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3327789Sahrens 3328789Sahrens /* make sure the parent exists and is of the appropriate type */ 33292199Sahrens delim = strchr(path, '@'); 33302082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 33312082Seschrock return (-1); 3332789Sahrens (void) strncpy(parent, path, delim - path); 3333789Sahrens parent[delim - path] = '\0'; 3334789Sahrens 33352082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3336789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3337789Sahrens free(parent); 3338789Sahrens return (-1); 3339789Sahrens } 3340789Sahrens 33412199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33422676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 33434543Smarks if (ZFS_IS_VOLUME(zhp)) 33444543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 33454543Smarks else 33464543Smarks zc.zc_objset_type = DMU_OST_ZFS; 33472199Sahrens zc.zc_cookie = recursive; 33484543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 33492199Sahrens 33502199Sahrens /* 33512199Sahrens * if it was recursive, the one that actually failed will be in 33522199Sahrens * zc.zc_name. 33532199Sahrens */ 33544543Smarks if (ret != 0) 33554543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33564543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 33574543Smarks 33582199Sahrens if (ret == 0 && recursive) { 33594007Smmusante struct createdata cd; 33604007Smmusante 33614007Smmusante cd.cd_snapname = delim + 1; 33624007Smmusante cd.cd_ifexists = B_FALSE; 33634007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 33642199Sahrens } 3365789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 33662082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 33672199Sahrens if (ret != 0) { 33684543Smarks (void) zfs_standard_error(hdl, errno, 33694543Smarks dgettext(TEXT_DOMAIN, 33704543Smarks "Volume successfully snapshotted, but device links " 33714543Smarks "were not created")); 33724543Smarks free(parent); 33734543Smarks zfs_close(zhp); 33744543Smarks return (-1); 33752199Sahrens } 3376789Sahrens } 3377789Sahrens 33782082Seschrock if (ret != 0) 33792082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3380789Sahrens 3381789Sahrens free(parent); 3382789Sahrens zfs_close(zhp); 3383789Sahrens 3384789Sahrens return (ret); 3385789Sahrens } 3386789Sahrens 3387789Sahrens /* 33883504Sahl * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 33893504Sahl * NULL) to the file descriptor specified by outfd. 3390789Sahrens */ 3391789Sahrens int 33923504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3393789Sahrens { 3394789Sahrens zfs_cmd_t zc = { 0 }; 33952082Seschrock char errbuf[1024]; 33962885Sahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 33972082Seschrock 33983504Sahl assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 33993504Sahl 34002885Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 34012885Sahrens if (fromsnap) 34022885Sahrens (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 34033504Sahl zc.zc_cookie = outfd; 34043504Sahl 34053504Sahl if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 34063504Sahl (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34073504Sahl "cannot send '%s'"), zhp->zfs_name); 34083504Sahl 3409789Sahrens switch (errno) { 3410789Sahrens 3411789Sahrens case EXDEV: 34122082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34133413Smmusante "not an earlier snapshot from the same fs")); 34142082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3415789Sahrens 3416789Sahrens case EDQUOT: 3417789Sahrens case EFBIG: 3418789Sahrens case EIO: 3419789Sahrens case ENOLINK: 3420789Sahrens case ENOSPC: 3421789Sahrens case ENOSTR: 3422789Sahrens case ENXIO: 3423789Sahrens case EPIPE: 3424789Sahrens case ERANGE: 3425789Sahrens case EFAULT: 3426789Sahrens case EROFS: 34272082Seschrock zfs_error_aux(hdl, strerror(errno)); 34282082Seschrock return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3429789Sahrens 3430789Sahrens default: 34312082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3432789Sahrens } 3433789Sahrens } 3434789Sahrens 34353504Sahl return (0); 3436789Sahrens } 3437789Sahrens 3438789Sahrens /* 34392885Sahrens * Create ancestors of 'target', but not target itself, and not 34402885Sahrens * ancestors whose names are shorter than prefixlen. Die if 34412885Sahrens * prefixlen-ancestor does not exist. 34422885Sahrens */ 34432885Sahrens static int 34442885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 34452885Sahrens { 34462885Sahrens zfs_handle_t *h; 34472885Sahrens char *cp; 34482885Sahrens 34492885Sahrens /* make sure prefix exists */ 34502885Sahrens cp = strchr(target + prefixlen, '/'); 34514603Sahrens if (cp == NULL) { 34524603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 34534603Sahrens } else { 34544603Sahrens *cp = '\0'; 34554603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 34564603Sahrens *cp = '/'; 34574603Sahrens } 34582885Sahrens if (h == NULL) 34592885Sahrens return (-1); 34602885Sahrens zfs_close(h); 34612885Sahrens 34622885Sahrens /* 34632885Sahrens * Attempt to create, mount, and share any ancestor filesystems, 34642885Sahrens * up to the prefixlen-long one. 34652885Sahrens */ 34662885Sahrens for (cp = target + prefixlen + 1; 34672885Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 34682885Sahrens const char *opname; 34694543Smarks char *logstr; 34702885Sahrens 34712885Sahrens *cp = '\0'; 34722885Sahrens 34732885Sahrens h = make_dataset_handle(hdl, target); 34742885Sahrens if (h) { 34752885Sahrens /* it already exists, nothing to do here */ 34762885Sahrens zfs_close(h); 34772885Sahrens continue; 34782885Sahrens } 34792885Sahrens 34802885Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 34814543Smarks logstr = hdl->libzfs_log_str; 34824543Smarks hdl->libzfs_log_str = NULL; 34832885Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 34844543Smarks NULL) != 0) { 34854543Smarks hdl->libzfs_log_str = logstr; 34862885Sahrens goto ancestorerr; 34874543Smarks } 34884543Smarks 34894543Smarks hdl->libzfs_log_str = logstr; 34902885Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 34912885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 34922885Sahrens if (h == NULL) 34932885Sahrens goto ancestorerr; 34942885Sahrens 34952885Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 34962885Sahrens if (zfs_mount(h, NULL, 0) != 0) 34972885Sahrens goto ancestorerr; 34982885Sahrens 34992885Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 35002885Sahrens if (zfs_share(h) != 0) 35012885Sahrens goto ancestorerr; 35022885Sahrens 35032885Sahrens zfs_close(h); 35042885Sahrens 35052885Sahrens continue; 35062885Sahrens ancestorerr: 35072885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35082885Sahrens "failed to %s ancestor '%s'"), opname, target); 35092885Sahrens return (-1); 35102885Sahrens } 35112885Sahrens 35122885Sahrens return (0); 35132885Sahrens } 35142885Sahrens 35152885Sahrens /* 35163504Sahl * Restores a backup of tosnap from the file descriptor specified by infd. 3517789Sahrens */ 3518789Sahrens int 35192082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 35203504Sahl int verbose, int dryrun, boolean_t force, int infd) 3521789Sahrens { 3522789Sahrens zfs_cmd_t zc = { 0 }; 3523789Sahrens time_t begin_time; 35242885Sahrens int ioctl_err, err, bytes, size, choplen; 3525789Sahrens char *cp; 3526789Sahrens dmu_replay_record_t drr; 3527789Sahrens struct drr_begin *drrb = &zc.zc_begin_record; 35282082Seschrock char errbuf[1024]; 35292665Snd150628 prop_changelist_t *clp; 35302885Sahrens char chopprefix[ZFS_MAXNAMELEN]; 3531789Sahrens 3532789Sahrens begin_time = time(NULL); 3533789Sahrens 35342082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35352082Seschrock "cannot receive")); 35362082Seschrock 3537789Sahrens /* read in the BEGIN record */ 3538789Sahrens cp = (char *)&drr; 3539789Sahrens bytes = 0; 3540789Sahrens do { 35413504Sahl size = read(infd, cp, sizeof (drr) - bytes); 3542868Sahrens cp += size; 3543868Sahrens bytes += size; 3544868Sahrens } while (size > 0); 3545868Sahrens 3546868Sahrens if (size < 0 || bytes != sizeof (drr)) { 35472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 35482082Seschrock "stream (failed to read first record)")); 35492082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3550789Sahrens } 3551789Sahrens 3552789Sahrens zc.zc_begin_record = drr.drr_u.drr_begin; 3553789Sahrens 3554789Sahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3555789Sahrens drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 35562082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 35572082Seschrock "stream (bad magic number)")); 35582082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3559789Sahrens } 3560789Sahrens 3561789Sahrens if (drrb->drr_version != DMU_BACKUP_VERSION && 3562789Sahrens drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 35632082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 35642082Seschrock "0x%llx is supported (stream is version 0x%llx)"), 3565789Sahrens DMU_BACKUP_VERSION, drrb->drr_version); 35662082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3567789Sahrens } 3568789Sahrens 35692885Sahrens if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 35702885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 35713912Slling "stream (bad snapshot name)")); 35722885Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 35732885Sahrens } 3574789Sahrens /* 35752885Sahrens * Determine how much of the snapshot name stored in the stream 35762885Sahrens * we are going to tack on to the name they specified on the 35772885Sahrens * command line, and how much we are going to chop off. 35782885Sahrens * 35792885Sahrens * If they specified a snapshot, chop the entire name stored in 35802885Sahrens * the stream. 3581789Sahrens */ 35822885Sahrens (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3583789Sahrens if (isprefix) { 35842885Sahrens /* 35852885Sahrens * They specified a fs with -d, we want to tack on 35862885Sahrens * everything but the pool name stored in the stream 35872885Sahrens */ 35882885Sahrens if (strchr(tosnap, '@')) { 35892885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 35902885Sahrens "argument - snapshot not allowed with -d")); 35912885Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3592789Sahrens } 35932885Sahrens cp = strchr(chopprefix, '/'); 3594789Sahrens if (cp == NULL) 35952885Sahrens cp = strchr(chopprefix, '@'); 35962885Sahrens *cp = '\0'; 3597789Sahrens } else if (strchr(tosnap, '@') == NULL) { 3598789Sahrens /* 35992885Sahrens * If they specified a filesystem without -d, we want to 36002885Sahrens * tack on everything after the fs specified in the 36012885Sahrens * first name from the stream. 3602789Sahrens */ 36032885Sahrens cp = strchr(chopprefix, '@'); 36042885Sahrens *cp = '\0'; 3605789Sahrens } 36062885Sahrens choplen = strlen(chopprefix); 36072885Sahrens 36082885Sahrens /* 36092885Sahrens * Determine name of destination snapshot, store in zc_value. 36102885Sahrens */ 36112885Sahrens (void) strcpy(zc.zc_value, tosnap); 36122885Sahrens (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 36132885Sahrens sizeof (zc.zc_value)); 36143265Sahrens if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT)) 36153265Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 36162885Sahrens 36172885Sahrens (void) strcpy(zc.zc_name, zc.zc_value); 3618789Sahrens if (drrb->drr_fromguid) { 3619789Sahrens /* incremental backup stream */ 36202885Sahrens zfs_handle_t *h; 36212885Sahrens 36222885Sahrens /* do the recvbackup ioctl to the containing fs */ 36232885Sahrens *strchr(zc.zc_name, '@') = '\0'; 3624789Sahrens 3625789Sahrens /* make sure destination fs exists */ 36262082Seschrock h = zfs_open(hdl, zc.zc_name, 36272082Seschrock ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 36282082Seschrock if (h == NULL) 3629789Sahrens return (-1); 3630868Sahrens if (!dryrun) { 36312665Snd150628 /* 36322665Snd150628 * We need to unmount all the dependents of the dataset 36332665Snd150628 * and the dataset itself. If it's a volume 36342665Snd150628 * then remove device link. 36352665Snd150628 */ 3636868Sahrens if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { 36372665Snd150628 clp = changelist_gather(h, ZFS_PROP_NAME, 0); 36382665Snd150628 if (clp == NULL) 36392665Snd150628 return (-1); 36402665Snd150628 if (changelist_prefix(clp) != 0) { 36412665Snd150628 changelist_free(clp); 36422665Snd150628 return (-1); 36432665Snd150628 } 3644868Sahrens } else { 36454543Smarks if (zvol_remove_link(hdl, h->zfs_name) != 0) { 36464543Smarks zfs_close(h); 36474543Smarks return (-1); 36484543Smarks } 36494543Smarks 3650868Sahrens } 3651868Sahrens } 3652789Sahrens zfs_close(h); 3653789Sahrens } else { 3654789Sahrens /* full backup stream */ 3655789Sahrens 3656868Sahrens /* Make sure destination fs does not exist */ 36572885Sahrens *strchr(zc.zc_name, '@') = '\0'; 36584490Svb160487 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_ANY)) { 36592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36602082Seschrock "destination '%s' exists"), zc.zc_name); 36612082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3662868Sahrens } 3663868Sahrens 36642885Sahrens if (strchr(zc.zc_name, '/') == NULL) { 36652885Sahrens /* 36662885Sahrens * they're trying to do a recv into a 36672885Sahrens * nonexistant topmost filesystem. 36682885Sahrens */ 36692885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36702885Sahrens "destination does not exist"), zc.zc_name); 36712885Sahrens return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 36722885Sahrens } 36732885Sahrens 3674868Sahrens /* Do the recvbackup ioctl to the fs's parent. */ 36752885Sahrens *strrchr(zc.zc_name, '/') = '\0'; 36762885Sahrens 36772885Sahrens if (isprefix && (err = create_parents(hdl, 36782885Sahrens zc.zc_value, strlen(tosnap))) != 0) { 36792885Sahrens return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 36802885Sahrens } 36812885Sahrens 3682789Sahrens } 3683789Sahrens 36843504Sahl zc.zc_cookie = infd; 36852676Seschrock zc.zc_guid = force; 3686789Sahrens if (verbose) { 36871749Sahrens (void) printf("%s %s stream of %s into %s\n", 36881749Sahrens dryrun ? "would receive" : "receiving", 3689789Sahrens drrb->drr_fromguid ? "incremental" : "full", 3690789Sahrens drr.drr_u.drr_begin.drr_toname, 36912676Seschrock zc.zc_value); 3692789Sahrens (void) fflush(stdout); 3693789Sahrens } 3694789Sahrens if (dryrun) 3695789Sahrens return (0); 36964543Smarks err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3697868Sahrens if (ioctl_err != 0) { 3698789Sahrens switch (errno) { 3699789Sahrens case ENODEV: 37002082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37012082Seschrock "most recent snapshot does not match incremental " 37022082Seschrock "source")); 37032082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3704789Sahrens break; 3705789Sahrens case ETXTBSY: 37062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37072082Seschrock "destination has been modified since most recent " 37082082Seschrock "snapshot")); 37092082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3710789Sahrens break; 3711789Sahrens case EEXIST: 3712789Sahrens if (drrb->drr_fromguid == 0) { 3713789Sahrens /* it's the containing fs that exists */ 37142676Seschrock cp = strchr(zc.zc_value, '@'); 3715789Sahrens *cp = '\0'; 3716789Sahrens } 37172082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37182082Seschrock "destination already exists")); 37193237Slling (void) zfs_error_fmt(hdl, EZFS_EXISTS, 37203237Slling dgettext(TEXT_DOMAIN, "cannot restore to %s"), 37213237Slling zc.zc_value); 3722789Sahrens break; 3723789Sahrens case EINVAL: 37242082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3725868Sahrens break; 37261544Seschrock case ECKSUM: 37272082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37282082Seschrock "invalid stream (checksum mismatch)")); 37292082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3730789Sahrens break; 3731789Sahrens default: 37322082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3733789Sahrens } 3734789Sahrens } 3735789Sahrens 3736789Sahrens /* 3737868Sahrens * Mount or recreate the /dev links for the target filesystem 3738868Sahrens * (if created, or if we tore them down to do an incremental 3739868Sahrens * restore), and the /dev links for the new snapshot (if 37402665Snd150628 * created). Also mount any children of the target filesystem 37412665Snd150628 * if we did an incremental receive. 3742789Sahrens */ 37432676Seschrock cp = strchr(zc.zc_value, '@'); 3744868Sahrens if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3745789Sahrens zfs_handle_t *h; 3746789Sahrens 3747789Sahrens *cp = '\0'; 37482676Seschrock h = zfs_open(hdl, zc.zc_value, 3749789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3750868Sahrens *cp = '@'; 3751789Sahrens if (h) { 37522665Snd150628 if (h->zfs_type == ZFS_TYPE_VOLUME) { 37532082Seschrock err = zvol_create_link(hdl, h->zfs_name); 37541544Seschrock if (err == 0 && ioctl_err == 0) 37552082Seschrock err = zvol_create_link(hdl, 37562676Seschrock zc.zc_value); 37572665Snd150628 } else { 37582665Snd150628 if (drrb->drr_fromguid) { 37592665Snd150628 err = changelist_postfix(clp); 37602665Snd150628 changelist_free(clp); 37612665Snd150628 } else { 37622665Snd150628 err = zfs_mount(h, NULL, 0); 37632665Snd150628 } 3764868Sahrens } 37652665Snd150628 zfs_close(h); 3766789Sahrens } 3767789Sahrens } 3768789Sahrens 3769868Sahrens if (err || ioctl_err) 3770868Sahrens return (-1); 3771789Sahrens 3772789Sahrens if (verbose) { 3773789Sahrens char buf1[64]; 3774789Sahrens char buf2[64]; 3775789Sahrens uint64_t bytes = zc.zc_cookie; 3776789Sahrens time_t delta = time(NULL) - begin_time; 3777789Sahrens if (delta == 0) 3778789Sahrens delta = 1; 3779789Sahrens zfs_nicenum(bytes, buf1, sizeof (buf1)); 3780789Sahrens zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3781789Sahrens 37824603Sahrens (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 3783789Sahrens buf1, delta, buf2); 3784789Sahrens } 37852665Snd150628 3786789Sahrens return (0); 3787789Sahrens } 3788789Sahrens 3789789Sahrens /* 37901294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 37911294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 37921294Slling * is a dependent and we should just destroy it without checking the transaction 37931294Slling * group. 3794789Sahrens */ 37951294Slling typedef struct rollback_data { 37961294Slling const char *cb_target; /* the snapshot */ 37971294Slling uint64_t cb_create; /* creation time reference */ 37981294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 37991294Slling int cb_error; 38002082Seschrock boolean_t cb_dependent; 38011294Slling } rollback_data_t; 38021294Slling 38031294Slling static int 38041294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 38051294Slling { 38061294Slling rollback_data_t *cbp = data; 38071294Slling 38081294Slling if (!cbp->cb_dependent) { 38091294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 38101294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 38111294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 38121294Slling cbp->cb_create) { 38134543Smarks char *logstr; 38141294Slling 38152082Seschrock cbp->cb_dependent = B_TRUE; 38162474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 38172474Seschrock cbp) != 0) 38182474Seschrock cbp->cb_error = 1; 38192082Seschrock cbp->cb_dependent = B_FALSE; 38201294Slling 38214543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 38224543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 38231294Slling if (zfs_destroy(zhp) != 0) 38241294Slling cbp->cb_error = 1; 38251294Slling else 38261294Slling changelist_remove(zhp, cbp->cb_clp); 38274543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 38281294Slling } 38291294Slling } else { 38301294Slling if (zfs_destroy(zhp) != 0) 38311294Slling cbp->cb_error = 1; 38321294Slling else 38331294Slling changelist_remove(zhp, cbp->cb_clp); 38341294Slling } 38351294Slling 38361294Slling zfs_close(zhp); 38371294Slling return (0); 38381294Slling } 38391294Slling 38401294Slling /* 38411294Slling * Rollback the dataset to its latest snapshot. 38421294Slling */ 38431294Slling static int 38441294Slling do_rollback(zfs_handle_t *zhp) 3845789Sahrens { 3846789Sahrens int ret; 3847789Sahrens zfs_cmd_t zc = { 0 }; 3848789Sahrens 3849789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3850789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3851789Sahrens 3852789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 38532082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3854789Sahrens return (-1); 3855789Sahrens 3856789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3857789Sahrens 38582676Seschrock if (ZFS_IS_VOLUME(zhp)) 3859789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3860789Sahrens else 3861789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3862789Sahrens 3863789Sahrens /* 3864789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3865789Sahrens * for the given dataset. Given these constraints, we can simply pass 3866789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3867789Sahrens * condition where the user has taken a snapshot since we verified that 3868789Sahrens * this was the most recent. 3869789Sahrens */ 38704543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 38713237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 38722082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 38732082Seschrock zhp->zfs_name); 3874789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 38752082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3876789Sahrens } 3877789Sahrens 3878789Sahrens return (ret); 3879789Sahrens } 3880789Sahrens 3881789Sahrens /* 38821294Slling * Given a dataset, rollback to a specific snapshot, discarding any 38831294Slling * data changes since then and making it the active dataset. 38841294Slling * 38851294Slling * Any snapshots more recent than the target are destroyed, along with 38861294Slling * their dependents. 38871294Slling */ 38881294Slling int 38891294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 38901294Slling { 38911294Slling int ret; 38921294Slling rollback_data_t cb = { 0 }; 38931294Slling prop_changelist_t *clp; 38941294Slling 38951294Slling /* 38961294Slling * Unmount all dependendents of the dataset and the dataset itself. 38971294Slling * The list we need to gather is the same as for doing rename 38981294Slling */ 38991294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 39001294Slling if (clp == NULL) 39011294Slling return (-1); 39021294Slling 39031294Slling if ((ret = changelist_prefix(clp)) != 0) 39041294Slling goto out; 39051294Slling 39061294Slling /* 39071294Slling * Destroy all recent snapshots and its dependends. 39081294Slling */ 39091294Slling cb.cb_target = snap->zfs_name; 39101294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 39111294Slling cb.cb_clp = clp; 39121294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 39131294Slling 39141294Slling if ((ret = cb.cb_error) != 0) { 39151294Slling (void) changelist_postfix(clp); 39161294Slling goto out; 39171294Slling } 39181294Slling 39191294Slling /* 39201294Slling * Now that we have verified that the snapshot is the latest, 39211294Slling * rollback to the given snapshot. 39221294Slling */ 39231294Slling ret = do_rollback(zhp); 39241294Slling 39251294Slling if (ret != 0) { 39261294Slling (void) changelist_postfix(clp); 39271294Slling goto out; 39281294Slling } 39291294Slling 39301294Slling /* 39311294Slling * We only want to re-mount the filesystem if it was mounted in the 39321294Slling * first place. 39331294Slling */ 39341294Slling ret = changelist_postfix(clp); 39351294Slling 39361294Slling out: 39371294Slling changelist_free(clp); 39381294Slling return (ret); 39391294Slling } 39401294Slling 39411294Slling /* 3942789Sahrens * Iterate over all dependents for a given dataset. This includes both 3943789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3944789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3945789Sahrens * libzfs_graph.c. 3946789Sahrens */ 3947789Sahrens int 39482474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 39492474Seschrock zfs_iter_f func, void *data) 3950789Sahrens { 3951789Sahrens char **dependents; 3952789Sahrens size_t count; 3953789Sahrens int i; 3954789Sahrens zfs_handle_t *child; 3955789Sahrens int ret = 0; 3956789Sahrens 39572474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 39582474Seschrock &dependents, &count) != 0) 39592474Seschrock return (-1); 39602474Seschrock 3961789Sahrens for (i = 0; i < count; i++) { 39622082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 39632082Seschrock dependents[i])) == NULL) 3964789Sahrens continue; 3965789Sahrens 3966789Sahrens if ((ret = func(child, data)) != 0) 3967789Sahrens break; 3968789Sahrens } 3969789Sahrens 3970789Sahrens for (i = 0; i < count; i++) 3971789Sahrens free(dependents[i]); 3972789Sahrens free(dependents); 3973789Sahrens 3974789Sahrens return (ret); 3975789Sahrens } 3976789Sahrens 3977789Sahrens /* 3978789Sahrens * Renames the given dataset. 3979789Sahrens */ 3980789Sahrens int 39814490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3982789Sahrens { 3983789Sahrens int ret; 3984789Sahrens zfs_cmd_t zc = { 0 }; 3985789Sahrens char *delim; 39864007Smmusante prop_changelist_t *cl = NULL; 39874007Smmusante zfs_handle_t *zhrp = NULL; 39884007Smmusante char *parentname = NULL; 3989789Sahrens char parent[ZFS_MAXNAMELEN]; 39902082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 39912082Seschrock char errbuf[1024]; 3992789Sahrens 3993789Sahrens /* if we have the same exact name, just return success */ 3994789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3995789Sahrens return (0); 3996789Sahrens 39972082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 39982082Seschrock "cannot rename to '%s'"), target); 39992082Seschrock 4000789Sahrens /* 4001789Sahrens * Make sure the target name is valid 4002789Sahrens */ 4003789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 40042665Snd150628 if ((strchr(target, '@') == NULL) || 40052665Snd150628 *target == '@') { 40062665Snd150628 /* 40072665Snd150628 * Snapshot target name is abbreviated, 40082665Snd150628 * reconstruct full dataset name 40092665Snd150628 */ 40102665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 40112665Snd150628 sizeof (parent)); 40122665Snd150628 delim = strchr(parent, '@'); 40132665Snd150628 if (strchr(target, '@') == NULL) 40142665Snd150628 *(++delim) = '\0'; 40152665Snd150628 else 40162665Snd150628 *delim = '\0'; 40172665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 40182665Snd150628 target = parent; 40192665Snd150628 } else { 40202665Snd150628 /* 40212665Snd150628 * Make sure we're renaming within the same dataset. 40222665Snd150628 */ 40232665Snd150628 delim = strchr(target, '@'); 40242665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 40252665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 40262665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40272665Snd150628 "snapshots must be part of same " 40282665Snd150628 "dataset")); 40292665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 40303912Slling errbuf)); 40312665Snd150628 } 4032789Sahrens } 40332665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 40342665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4035789Sahrens } else { 40364007Smmusante if (recursive) { 40374007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40384007Smmusante "recursive rename must be a snapshot")); 40394007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 40404007Smmusante } 40414007Smmusante 40422665Snd150628 if (!zfs_validate_name(hdl, target, zhp->zfs_type)) 40432665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40442676Seschrock uint64_t unused; 40452676Seschrock 4046789Sahrens /* validate parents */ 40474490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4048789Sahrens return (-1); 4049789Sahrens 4050789Sahrens (void) parent_name(target, parent, sizeof (parent)); 4051789Sahrens 4052789Sahrens /* make sure we're in the same pool */ 4053789Sahrens verify((delim = strchr(target, '/')) != NULL); 4054789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4055789Sahrens zhp->zfs_name[delim - target] != '/') { 40562082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40572082Seschrock "datasets must be within same pool")); 40582082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4059789Sahrens } 40602440Snd150628 40612440Snd150628 /* new name cannot be a child of the current dataset name */ 40622440Snd150628 if (strncmp(parent, zhp->zfs_name, 40633912Slling strlen(zhp->zfs_name)) == 0) { 40642440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40652440Snd150628 "New dataset name cannot be a descendent of " 40662440Snd150628 "current dataset name")); 40672440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40682440Snd150628 } 4069789Sahrens } 4070789Sahrens 40712082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 40722082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 40732082Seschrock 4074789Sahrens if (getzoneid() == GLOBAL_ZONEID && 4075789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 40762082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40772082Seschrock "dataset is used in a non-global zone")); 40782082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4079789Sahrens } 4080789Sahrens 40814007Smmusante if (recursive) { 40824007Smmusante struct destroydata dd; 40834007Smmusante 40844183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 40854183Smmusante if (parentname == NULL) { 40864183Smmusante ret = -1; 40874183Smmusante goto error; 40884183Smmusante } 40894007Smmusante delim = strchr(parentname, '@'); 40904007Smmusante *delim = '\0'; 40914007Smmusante zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_ANY); 40924007Smmusante if (zhrp == NULL) { 40934183Smmusante ret = -1; 40944183Smmusante goto error; 40954007Smmusante } 40964007Smmusante 40974007Smmusante dd.snapname = delim + 1; 40984007Smmusante dd.gotone = B_FALSE; 40994183Smmusante dd.closezhp = B_TRUE; 41004007Smmusante 41014007Smmusante /* We remove any zvol links prior to renaming them */ 41024007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 41034007Smmusante if (ret) { 41044007Smmusante goto error; 41054007Smmusante } 41064007Smmusante } else { 41074007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 41084007Smmusante return (-1); 41094007Smmusante 41104007Smmusante if (changelist_haszonedchild(cl)) { 41114007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41124007Smmusante "child dataset with inherited mountpoint is used " 41134007Smmusante "in a non-global zone")); 41144007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 41154007Smmusante goto error; 41164007Smmusante } 41174007Smmusante 41184007Smmusante if ((ret = changelist_prefix(cl)) != 0) 41194007Smmusante goto error; 4120789Sahrens } 4121789Sahrens 41222676Seschrock if (ZFS_IS_VOLUME(zhp)) 4123789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4124789Sahrens else 4125789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4126789Sahrens 41272665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 41282676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 41292665Snd150628 41304007Smmusante zc.zc_cookie = recursive; 41314007Smmusante 41324543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 41334007Smmusante /* 41344007Smmusante * if it was recursive, the one that actually failed will 41354007Smmusante * be in zc.zc_name 41364007Smmusante */ 41374007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 41384007Smmusante "cannot rename to '%s'"), zc.zc_name); 41394007Smmusante 41404007Smmusante if (recursive && errno == EEXIST) { 41414007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41424007Smmusante "a child dataset already has a snapshot " 41434007Smmusante "with the new name")); 41444801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 41454007Smmusante } else { 41464007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 41474007Smmusante } 4148789Sahrens 4149789Sahrens /* 4150789Sahrens * On failure, we still want to remount any filesystems that 4151789Sahrens * were previously mounted, so we don't alter the system state. 4152789Sahrens */ 41534007Smmusante if (recursive) { 41544007Smmusante struct createdata cd; 41554007Smmusante 41564007Smmusante /* only create links for datasets that had existed */ 41574007Smmusante cd.cd_snapname = delim + 1; 41584007Smmusante cd.cd_ifexists = B_TRUE; 41594007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41604007Smmusante &cd); 41614007Smmusante } else { 41624007Smmusante (void) changelist_postfix(cl); 41634007Smmusante } 4164789Sahrens } else { 41654007Smmusante if (recursive) { 41664007Smmusante struct createdata cd; 41674007Smmusante 41684007Smmusante /* only create links for datasets that had existed */ 41694007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 41704007Smmusante cd.cd_ifexists = B_TRUE; 41714007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41724007Smmusante &cd); 41734007Smmusante } else { 41744007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 41754007Smmusante ret = changelist_postfix(cl); 41764007Smmusante } 4177789Sahrens } 4178789Sahrens 4179789Sahrens error: 41804007Smmusante if (parentname) { 41814007Smmusante free(parentname); 41824007Smmusante } 41834007Smmusante if (zhrp) { 41844007Smmusante zfs_close(zhrp); 41854007Smmusante } 41864007Smmusante if (cl) { 41874007Smmusante changelist_free(cl); 41884007Smmusante } 4189789Sahrens return (ret); 4190789Sahrens } 4191789Sahrens 4192789Sahrens /* 4193789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4194789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 4195789Sahrens */ 4196789Sahrens int 41972082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4198789Sahrens { 41994007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 42004007Smmusante } 42014007Smmusante 42024007Smmusante static int 42034007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 42044007Smmusante { 4205789Sahrens zfs_cmd_t zc = { 0 }; 42062082Seschrock di_devlink_handle_t dhdl; 42074543Smarks priv_set_t *priv_effective; 42084543Smarks int privileged; 4209789Sahrens 4210789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4211789Sahrens 4212789Sahrens /* 4213789Sahrens * Issue the appropriate ioctl. 4214789Sahrens */ 42152082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4216789Sahrens switch (errno) { 4217789Sahrens case EEXIST: 4218789Sahrens /* 4219789Sahrens * Silently ignore the case where the link already 4220789Sahrens * exists. This allows 'zfs volinit' to be run multiple 4221789Sahrens * times without errors. 4222789Sahrens */ 4223789Sahrens return (0); 4224789Sahrens 42254007Smmusante case ENOENT: 42264007Smmusante /* 42274007Smmusante * Dataset does not exist in the kernel. If we 42284007Smmusante * don't care (see zfs_rename), then ignore the 42294007Smmusante * error quietly. 42304007Smmusante */ 42314007Smmusante if (ifexists) { 42324007Smmusante return (0); 42334007Smmusante } 42344007Smmusante 42354007Smmusante /* FALLTHROUGH */ 42364007Smmusante 4237789Sahrens default: 42383237Slling return (zfs_standard_error_fmt(hdl, errno, 42392082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 42402082Seschrock "for '%s'"), dataset)); 4241789Sahrens } 4242789Sahrens } 4243789Sahrens 4244789Sahrens /* 42454543Smarks * If privileged call devfsadm and wait for the links to 42464543Smarks * magically appear. 42474543Smarks * Otherwise, print out an informational message. 4248789Sahrens */ 42494543Smarks 42504543Smarks priv_effective = priv_allocset(); 42514543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 42524543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 42534543Smarks priv_freeset(priv_effective); 42544543Smarks 42554543Smarks if (privileged) { 42564543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 42574543Smarks DI_MAKE_LINK)) == NULL) { 42584543Smarks zfs_error_aux(hdl, strerror(errno)); 42594543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 42604543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 42614543Smarks "for '%s'"), dataset); 42624543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 42634543Smarks return (-1); 42644543Smarks } else { 42654543Smarks (void) di_devlink_fini(&dhdl); 42664543Smarks } 4267789Sahrens } else { 42684543Smarks char pathname[MAXPATHLEN]; 42694543Smarks struct stat64 statbuf; 42704543Smarks int i; 42714543Smarks 42724543Smarks #define MAX_WAIT 10 42734543Smarks 42744543Smarks /* 42754543Smarks * This is the poor mans way of waiting for the link 42764543Smarks * to show up. If after 10 seconds we still don't 42774543Smarks * have it, then print out a message. 42784543Smarks */ 42794543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 42804543Smarks dataset); 42814543Smarks 42824543Smarks for (i = 0; i != MAX_WAIT; i++) { 42834543Smarks if (stat64(pathname, &statbuf) == 0) 42844543Smarks break; 42854543Smarks (void) sleep(1); 42864543Smarks } 42874543Smarks if (i == MAX_WAIT) 42884543Smarks (void) printf(gettext("%s may not be immediately " 42894543Smarks "available\n"), pathname); 4290789Sahrens } 4291789Sahrens 4292789Sahrens return (0); 4293789Sahrens } 4294789Sahrens 4295789Sahrens /* 4296789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4297789Sahrens */ 4298789Sahrens int 42992082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4300789Sahrens { 4301789Sahrens zfs_cmd_t zc = { 0 }; 4302789Sahrens 4303789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4304789Sahrens 43052082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4306789Sahrens switch (errno) { 4307789Sahrens case ENXIO: 4308789Sahrens /* 4309789Sahrens * Silently ignore the case where the link no longer 4310789Sahrens * exists, so that 'zfs volfini' can be run multiple 4311789Sahrens * times without errors. 4312789Sahrens */ 4313789Sahrens return (0); 4314789Sahrens 4315789Sahrens default: 43163237Slling return (zfs_standard_error_fmt(hdl, errno, 43172082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 43182082Seschrock "links for '%s'"), dataset)); 4319789Sahrens } 4320789Sahrens } 4321789Sahrens 4322789Sahrens return (0); 4323789Sahrens } 43242676Seschrock 43252676Seschrock nvlist_t * 43262676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 43272676Seschrock { 43282676Seschrock return (zhp->zfs_user_props); 43292676Seschrock } 43302676Seschrock 43312676Seschrock /* 43324451Seschrock * Given a comma-separated list of properties, construct a property list 43332676Seschrock * containing both user-defined and native properties. This function will 43342676Seschrock * return a NULL list if 'all' is specified, which can later be expanded on a 43352676Seschrock * per-dataset basis by zfs_expand_proplist(). 43362676Seschrock */ 43372676Seschrock int 43383912Slling zfs_get_proplist_common(libzfs_handle_t *hdl, char *fields, 43393912Slling zfs_proplist_t **listp, zfs_type_t type) 43402676Seschrock { 43412676Seschrock size_t len; 43422676Seschrock char *s, *p; 43432676Seschrock char c; 43442676Seschrock zfs_prop_t prop; 43452676Seschrock zfs_proplist_t *entry; 43462676Seschrock zfs_proplist_t **last; 43472676Seschrock 43482676Seschrock *listp = NULL; 43492676Seschrock last = listp; 43502676Seschrock 43512676Seschrock /* 43522676Seschrock * If 'all' is specified, return a NULL list. 43532676Seschrock */ 43542676Seschrock if (strcmp(fields, "all") == 0) 43552676Seschrock return (0); 43562676Seschrock 43572676Seschrock /* 43582676Seschrock * If no fields were specified, return an error. 43592676Seschrock */ 43602676Seschrock if (fields[0] == '\0') { 43612676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43622676Seschrock "no properties specified")); 43632676Seschrock return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, 43642676Seschrock "bad property list"))); 43652676Seschrock } 43662676Seschrock 43672676Seschrock /* 43682676Seschrock * It would be nice to use getsubopt() here, but the inclusion of column 43692676Seschrock * aliases makes this more effort than it's worth. 43702676Seschrock */ 43712676Seschrock s = fields; 43722676Seschrock while (*s != '\0') { 43732676Seschrock if ((p = strchr(s, ',')) == NULL) { 43742676Seschrock len = strlen(s); 43752676Seschrock p = s + len; 43762676Seschrock } else { 43772676Seschrock len = p - s; 43782676Seschrock } 43792676Seschrock 43802676Seschrock /* 43812676Seschrock * Check for empty options. 43822676Seschrock */ 43832676Seschrock if (len == 0) { 43842676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43852676Seschrock "empty property name")); 43862676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 43872676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 43882676Seschrock } 43892676Seschrock 43902676Seschrock /* 43912676Seschrock * Check all regular property names. 43922676Seschrock */ 43932676Seschrock c = s[len]; 43942676Seschrock s[len] = '\0'; 43954451Seschrock prop = type == ZFS_TYPE_POOL ? zpool_name_to_prop(s) : 43964451Seschrock zfs_name_to_prop(s); 43973912Slling 43983912Slling if (prop != ZFS_PROP_INVAL && 43993912Slling !zfs_prop_valid_for_type(prop, type)) 44003912Slling prop = ZFS_PROP_INVAL; 44012676Seschrock 44022676Seschrock /* 44033912Slling * When no property table entry can be found, return failure if 44043912Slling * this is a pool property or if this isn't a user-defined 44053912Slling * dataset property, 44062676Seschrock */ 44073912Slling if (prop == ZFS_PROP_INVAL && 44083912Slling (type & ZFS_TYPE_POOL || !zfs_prop_user(s))) { 44092676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44102676Seschrock "invalid property '%s'"), s); 44112676Seschrock return (zfs_error(hdl, EZFS_BADPROP, 44122676Seschrock dgettext(TEXT_DOMAIN, "bad property list"))); 44132676Seschrock } 44142676Seschrock 44152676Seschrock if ((entry = zfs_alloc(hdl, sizeof (zfs_proplist_t))) == NULL) 44162676Seschrock return (-1); 44172676Seschrock 44182676Seschrock entry->pl_prop = prop; 44192676Seschrock if (prop == ZFS_PROP_INVAL) { 44202676Seschrock if ((entry->pl_user_prop = 44212676Seschrock zfs_strdup(hdl, s)) == NULL) { 44222676Seschrock free(entry); 44232676Seschrock return (-1); 44242676Seschrock } 44252676Seschrock entry->pl_width = strlen(s); 44262676Seschrock } else { 44272676Seschrock entry->pl_width = zfs_prop_width(prop, 44282676Seschrock &entry->pl_fixed); 44292676Seschrock } 44302676Seschrock 44312676Seschrock *last = entry; 44322676Seschrock last = &entry->pl_next; 44332676Seschrock 44342676Seschrock s = p; 44352676Seschrock if (c == ',') 44362676Seschrock s++; 44372676Seschrock } 44382676Seschrock 44392676Seschrock return (0); 44402676Seschrock } 44412676Seschrock 44423912Slling int 44433912Slling zfs_get_proplist(libzfs_handle_t *hdl, char *fields, zfs_proplist_t **listp) 44443912Slling { 44453912Slling return (zfs_get_proplist_common(hdl, fields, listp, ZFS_TYPE_ANY)); 44463912Slling } 44473912Slling 44482676Seschrock void 44492676Seschrock zfs_free_proplist(zfs_proplist_t *pl) 44502676Seschrock { 44512676Seschrock zfs_proplist_t *next; 44522676Seschrock 44532676Seschrock while (pl != NULL) { 44542676Seschrock next = pl->pl_next; 44552676Seschrock free(pl->pl_user_prop); 44562676Seschrock free(pl); 44572676Seschrock pl = next; 44582676Seschrock } 44592676Seschrock } 44602676Seschrock 44613654Sgw25295 typedef struct expand_data { 44623654Sgw25295 zfs_proplist_t **last; 44633654Sgw25295 libzfs_handle_t *hdl; 44643654Sgw25295 } expand_data_t; 44653654Sgw25295 44663654Sgw25295 static zfs_prop_t 44673654Sgw25295 zfs_expand_proplist_cb(zfs_prop_t prop, void *cb) 44683654Sgw25295 { 44693654Sgw25295 zfs_proplist_t *entry; 44703654Sgw25295 expand_data_t *edp = cb; 44713654Sgw25295 44723654Sgw25295 if ((entry = zfs_alloc(edp->hdl, sizeof (zfs_proplist_t))) == NULL) 44733654Sgw25295 return (ZFS_PROP_INVAL); 44743654Sgw25295 44753654Sgw25295 entry->pl_prop = prop; 44763654Sgw25295 entry->pl_width = zfs_prop_width(prop, &entry->pl_fixed); 44773654Sgw25295 entry->pl_all = B_TRUE; 44783654Sgw25295 44793654Sgw25295 *(edp->last) = entry; 44803654Sgw25295 edp->last = &entry->pl_next; 44813654Sgw25295 44823654Sgw25295 return (ZFS_PROP_CONT); 44833654Sgw25295 } 44843654Sgw25295 44852676Seschrock int 44863912Slling zfs_expand_proplist_common(libzfs_handle_t *hdl, zfs_proplist_t **plp, 44873912Slling zfs_type_t type) 44882676Seschrock { 44892676Seschrock zfs_proplist_t *entry; 44903912Slling zfs_proplist_t **last; 44913654Sgw25295 expand_data_t exp; 44922676Seschrock 44932676Seschrock if (*plp == NULL) { 44942676Seschrock /* 44952676Seschrock * If this is the very first time we've been called for an 'all' 44962676Seschrock * specification, expand the list to include all native 44972676Seschrock * properties. 44982676Seschrock */ 44992676Seschrock last = plp; 45003654Sgw25295 45013654Sgw25295 exp.last = last; 45023654Sgw25295 exp.hdl = hdl; 45033654Sgw25295 45043912Slling if (zfs_prop_iter_common(zfs_expand_proplist_cb, &exp, type, 45054597Stimf B_FALSE, B_FALSE) == ZFS_PROP_INVAL) 45063654Sgw25295 return (-1); 45072676Seschrock 45082676Seschrock /* 45092676Seschrock * Add 'name' to the beginning of the list, which is handled 45102676Seschrock * specially. 45112676Seschrock */ 45122676Seschrock if ((entry = zfs_alloc(hdl, 45132676Seschrock sizeof (zfs_proplist_t))) == NULL) 45142676Seschrock return (-1); 45152676Seschrock 45162676Seschrock entry->pl_prop = ZFS_PROP_NAME; 45172676Seschrock entry->pl_width = zfs_prop_width(ZFS_PROP_NAME, 45182676Seschrock &entry->pl_fixed); 45192676Seschrock entry->pl_all = B_TRUE; 45202676Seschrock entry->pl_next = *plp; 45212676Seschrock *plp = entry; 45222676Seschrock } 45233912Slling return (0); 45243912Slling } 45253912Slling 45263912Slling /* 45273912Slling * This function is used by 'zfs list' to determine the exact set of columns to 45283912Slling * display, and their maximum widths. This does two main things: 45293912Slling * 45303912Slling * - If this is a list of all properties, then expand the list to include 45313912Slling * all native properties, and set a flag so that for each dataset we look 45323912Slling * for new unique user properties and add them to the list. 45333912Slling * 45343912Slling * - For non fixed-width properties, keep track of the maximum width seen 45353912Slling * so that we can size the column appropriately. 45363912Slling */ 45373912Slling int 45383912Slling zfs_expand_proplist(zfs_handle_t *zhp, zfs_proplist_t **plp) 45393912Slling { 45403912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 45413912Slling zfs_proplist_t *entry; 45423912Slling zfs_proplist_t **last, **start; 45433912Slling nvlist_t *userprops, *propval; 45443912Slling nvpair_t *elem; 45453912Slling char *strval; 45463912Slling char buf[ZFS_MAXPROPLEN]; 45473912Slling 45483912Slling if (zfs_expand_proplist_common(hdl, plp, ZFS_TYPE_ANY) != 0) 45493912Slling return (-1); 45502676Seschrock 45512676Seschrock userprops = zfs_get_user_props(zhp); 45522676Seschrock 45532676Seschrock entry = *plp; 45542676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 45552676Seschrock /* 45562676Seschrock * Go through and add any user properties as necessary. We 45572676Seschrock * start by incrementing our list pointer to the first 45582676Seschrock * non-native property. 45592676Seschrock */ 45602676Seschrock start = plp; 45612676Seschrock while (*start != NULL) { 45622676Seschrock if ((*start)->pl_prop == ZFS_PROP_INVAL) 45632676Seschrock break; 45642676Seschrock start = &(*start)->pl_next; 45652676Seschrock } 45662676Seschrock 45672676Seschrock elem = NULL; 45682676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 45692676Seschrock /* 45702676Seschrock * See if we've already found this property in our list. 45712676Seschrock */ 45722676Seschrock for (last = start; *last != NULL; 45732676Seschrock last = &(*last)->pl_next) { 45742676Seschrock if (strcmp((*last)->pl_user_prop, 45752676Seschrock nvpair_name(elem)) == 0) 45762676Seschrock break; 45772676Seschrock } 45782676Seschrock 45792676Seschrock if (*last == NULL) { 45802676Seschrock if ((entry = zfs_alloc(hdl, 45812676Seschrock sizeof (zfs_proplist_t))) == NULL || 45822676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 45832676Seschrock nvpair_name(elem)))) == NULL) { 45842676Seschrock free(entry); 45852676Seschrock return (-1); 45862676Seschrock } 45872676Seschrock 45882676Seschrock entry->pl_prop = ZFS_PROP_INVAL; 45892676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 45902676Seschrock entry->pl_all = B_TRUE; 45912676Seschrock *last = entry; 45922676Seschrock } 45932676Seschrock } 45942676Seschrock } 45952676Seschrock 45962676Seschrock /* 45972676Seschrock * Now go through and check the width of any non-fixed columns 45982676Seschrock */ 45992676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 46002676Seschrock if (entry->pl_fixed) 46012676Seschrock continue; 46022676Seschrock 46032676Seschrock if (entry->pl_prop != ZFS_PROP_INVAL) { 46042676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 46052676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 46062676Seschrock if (strlen(buf) > entry->pl_width) 46072676Seschrock entry->pl_width = strlen(buf); 46082676Seschrock } 46092676Seschrock } else if (nvlist_lookup_nvlist(userprops, 46102676Seschrock entry->pl_user_prop, &propval) == 0) { 46112676Seschrock verify(nvlist_lookup_string(propval, 46122676Seschrock ZFS_PROP_VALUE, &strval) == 0); 46132676Seschrock if (strlen(strval) > entry->pl_width) 46142676Seschrock entry->pl_width = strlen(strval); 46152676Seschrock } 46162676Seschrock } 46172676Seschrock 46182676Seschrock return (0); 46192676Seschrock } 46204543Smarks 46214543Smarks int 46224543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 46234543Smarks { 46244543Smarks zfs_cmd_t zc = { 0 }; 46254543Smarks nvlist_t *nvp; 46264543Smarks size_t sz; 46274543Smarks gid_t gid; 46284543Smarks uid_t uid; 46294543Smarks const gid_t *groups; 46304543Smarks int group_cnt; 46314543Smarks int error; 46324543Smarks 46334543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 46344543Smarks return (no_memory(hdl)); 46354543Smarks 46364543Smarks uid = ucred_geteuid(cred); 46374543Smarks gid = ucred_getegid(cred); 46384543Smarks group_cnt = ucred_getgroups(cred, &groups); 46394543Smarks 46404543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 46414543Smarks return (1); 46424543Smarks 46434543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 46444543Smarks nvlist_free(nvp); 46454543Smarks return (1); 46464543Smarks } 46474543Smarks 46484543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 46494543Smarks nvlist_free(nvp); 46504543Smarks return (1); 46514543Smarks } 46524543Smarks 46534543Smarks if (nvlist_add_uint32_array(nvp, 46544543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 46554543Smarks nvlist_free(nvp); 46564543Smarks return (1); 46574543Smarks } 46584543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 46594543Smarks 46604543Smarks if (zcmd_write_src_nvlist(hdl, &zc, nvp, &sz)) 46614543Smarks return (-1); 46624543Smarks 46634543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 46644543Smarks nvlist_free(nvp); 46654543Smarks return (error); 46664543Smarks } 46674543Smarks 46684543Smarks int 46694543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 46704543Smarks void *export, void *sharetab, int sharemax, boolean_t share_on) 46714543Smarks { 46724543Smarks zfs_cmd_t zc = { 0 }; 46734543Smarks int error; 46744543Smarks 46754543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 46764543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 46774543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 46784543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 46794543Smarks zc.zc_share.z_sharetype = share_on; 46804543Smarks zc.zc_share.z_sharemax = sharemax; 46814543Smarks 46824543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 46834543Smarks return (error); 46844543Smarks } 4685