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> 395367Sahrens #include <stddef.h> 40789Sahrens #include <zone.h> 412082Seschrock #include <fcntl.h> 42789Sahrens #include <sys/mntent.h> 43789Sahrens #include <sys/mnttab.h> 441294Slling #include <sys/mount.h> 454543Smarks #include <sys/avl.h> 464543Smarks #include <priv.h> 474543Smarks #include <pwd.h> 484543Smarks #include <grp.h> 494543Smarks #include <stddef.h> 504543Smarks #include <ucred.h> 51789Sahrens 52789Sahrens #include <sys/spa.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 614007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 624007Smmusante 63789Sahrens /* 64789Sahrens * Given a single type (not a mask of types), return the type in a human 65789Sahrens * readable form. 66789Sahrens */ 67789Sahrens const char * 68789Sahrens zfs_type_to_name(zfs_type_t type) 69789Sahrens { 70789Sahrens switch (type) { 71789Sahrens case ZFS_TYPE_FILESYSTEM: 72789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 73789Sahrens case ZFS_TYPE_SNAPSHOT: 74789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 75789Sahrens case ZFS_TYPE_VOLUME: 76789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 77789Sahrens } 78789Sahrens 79789Sahrens return (NULL); 80789Sahrens } 81789Sahrens 82789Sahrens /* 83789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 84789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 85789Sahrens * We guess what the type would have been based on the path and the mask of 86789Sahrens * acceptable types. 87789Sahrens */ 88789Sahrens static const char * 89789Sahrens path_to_str(const char *path, int types) 90789Sahrens { 91789Sahrens /* 92789Sahrens * When given a single type, always report the exact type. 93789Sahrens */ 94789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 95789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 96789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 97789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 98789Sahrens if (types == ZFS_TYPE_VOLUME) 99789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 100789Sahrens 101789Sahrens /* 102789Sahrens * The user is requesting more than one type of dataset. If this is the 103789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 104789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 105789Sahrens * snapshot attribute and try again. 106789Sahrens */ 107789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 108789Sahrens if (strchr(path, '@') != NULL) 109789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 110789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 111789Sahrens } 112789Sahrens 113789Sahrens 114789Sahrens /* 115789Sahrens * The user has requested either filesystems or volumes. 116789Sahrens * We have no way of knowing a priori what type this would be, so always 117789Sahrens * report it as "filesystem" or "volume", our two primitive types. 118789Sahrens */ 119789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 120789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 121789Sahrens 122789Sahrens assert(types & ZFS_TYPE_VOLUME); 123789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 124789Sahrens } 125789Sahrens 126789Sahrens /* 127789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 128789Sahrens * provide a more meaningful error message. We place a more useful message in 129789Sahrens * 'buf' detailing exactly why the name was not valid. 130789Sahrens */ 131789Sahrens static int 1325326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 1335326Sek110237 boolean_t modifying) 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 2065326Sek110237 if (modifying && strchr(path, '%') != NULL) { 2075326Sek110237 if (hdl != NULL) 2085326Sek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2095326Sek110237 "invalid character %c in name"), '%'); 2105326Sek110237 return (0); 2115326Sek110237 } 2125326Sek110237 2132082Seschrock return (-1); 214789Sahrens } 215789Sahrens 216789Sahrens int 217789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 218789Sahrens { 2195326Sek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 220789Sahrens } 221789Sahrens 222789Sahrens /* 2232676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2242676Seschrock * properties into a separate nvlist. 2252676Seschrock */ 2264217Seschrock static nvlist_t * 2274217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2282676Seschrock { 2292676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2302676Seschrock nvpair_t *elem; 2312676Seschrock nvlist_t *propval; 2324217Seschrock nvlist_t *nvl; 2334217Seschrock 2344217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2354217Seschrock (void) no_memory(hdl); 2364217Seschrock return (NULL); 2374217Seschrock } 2382676Seschrock 2392676Seschrock elem = NULL; 2404217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2412676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2422676Seschrock continue; 2432676Seschrock 2442676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2454217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2464217Seschrock nvlist_free(nvl); 2474217Seschrock (void) no_memory(hdl); 2484217Seschrock return (NULL); 2494217Seschrock } 2502676Seschrock } 2512676Seschrock 2524217Seschrock return (nvl); 2532676Seschrock } 2542676Seschrock 2552676Seschrock /* 256789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 257789Sahrens */ 258789Sahrens static int 259789Sahrens get_stats(zfs_handle_t *zhp) 260789Sahrens { 261789Sahrens zfs_cmd_t zc = { 0 }; 2622676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2634217Seschrock nvlist_t *allprops, *userprops; 264789Sahrens 265789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 266789Sahrens 2672676Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 2682082Seschrock return (-1); 2691356Seschrock 2702082Seschrock while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 2711356Seschrock if (errno == ENOMEM) { 2722676Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2732676Seschrock zcmd_free_nvlists(&zc); 2742082Seschrock return (-1); 2752676Seschrock } 2761356Seschrock } else { 2772676Seschrock zcmd_free_nvlists(&zc); 2781356Seschrock return (-1); 2791356Seschrock } 2801356Seschrock } 281789Sahrens 2822885Sahrens zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 283789Sahrens 2842676Seschrock (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 2851544Seschrock 2864217Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 2872676Seschrock zcmd_free_nvlists(&zc); 2882082Seschrock return (-1); 2892082Seschrock } 290789Sahrens 2912676Seschrock zcmd_free_nvlists(&zc); 2922676Seschrock 2934217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 2944217Seschrock nvlist_free(allprops); 2952676Seschrock return (-1); 2964217Seschrock } 2974217Seschrock 2984217Seschrock nvlist_free(zhp->zfs_props); 2994217Seschrock nvlist_free(zhp->zfs_user_props); 3004217Seschrock 3014217Seschrock zhp->zfs_props = allprops; 3024217Seschrock zhp->zfs_user_props = userprops; 3032082Seschrock 304789Sahrens return (0); 305789Sahrens } 306789Sahrens 307789Sahrens /* 308789Sahrens * Refresh the properties currently stored in the handle. 309789Sahrens */ 310789Sahrens void 311789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 312789Sahrens { 313789Sahrens (void) get_stats(zhp); 314789Sahrens } 315789Sahrens 316789Sahrens /* 317789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 318789Sahrens * zfs_iter_* to create child handles on the fly. 319789Sahrens */ 320789Sahrens zfs_handle_t * 3212082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path) 322789Sahrens { 3232082Seschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 3244543Smarks char *logstr; 3252082Seschrock 3262082Seschrock if (zhp == NULL) 3272082Seschrock return (NULL); 3282082Seschrock 3292082Seschrock zhp->zfs_hdl = hdl; 330789Sahrens 3314543Smarks /* 3324543Smarks * Preserve history log string. 3334543Smarks * any changes performed here will be 3344543Smarks * logged as an internal event. 3354543Smarks */ 3364543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 3374543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 3381758Sahrens top: 339789Sahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 340789Sahrens 341789Sahrens if (get_stats(zhp) != 0) { 3424543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 343789Sahrens free(zhp); 344789Sahrens return (NULL); 345789Sahrens } 346789Sahrens 3471758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 3481758Sahrens zfs_cmd_t zc = { 0 }; 3491758Sahrens 3501758Sahrens /* 3511758Sahrens * If it is dds_inconsistent, then we've caught it in 3521758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 3531758Sahrens * it is inconsistent from the ZPL's point of view, so 3541758Sahrens * can't be mounted. However, it could also be that we 3551758Sahrens * have crashed in the middle of one of those 3561758Sahrens * operations, in which case we need to get rid of the 3571758Sahrens * inconsistent state. We do that by either rolling 3581758Sahrens * back to the previous snapshot (which will fail if 3591758Sahrens * there is none), or destroying the filesystem. Note 3601758Sahrens * that if we are still in the middle of an active 3611758Sahrens * 'receive' or 'destroy', then the rollback and destroy 3621758Sahrens * will fail with EBUSY and we will drive on as usual. 3631758Sahrens */ 3641758Sahrens 3651758Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3661758Sahrens 3672885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 3682082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 3691758Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3701758Sahrens } else { 3711758Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3721758Sahrens } 3731758Sahrens 3741758Sahrens /* 3755331Samw * If we can successfully destroy it, pretend that it 3761758Sahrens * never existed. 3771758Sahrens */ 3782082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 3794543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 3801758Sahrens free(zhp); 3811758Sahrens errno = ENOENT; 3821758Sahrens return (NULL); 3831758Sahrens } 3845367Sahrens /* If we can successfully roll it back, reget the stats */ 3855367Sahrens if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 3865367Sahrens goto top; 3871758Sahrens } 3881758Sahrens 389789Sahrens /* 390789Sahrens * We've managed to open the dataset and gather statistics. Determine 391789Sahrens * the high-level type. 392789Sahrens */ 3932885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 3942885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 3952885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 3962885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 3972885Sahrens else 3982885Sahrens abort(); 3992885Sahrens 400789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 401789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 402789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 403789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 404789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 405789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 406789Sahrens else 4072082Seschrock abort(); /* we should never see any other types */ 408789Sahrens 4094543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 410789Sahrens return (zhp); 411789Sahrens } 412789Sahrens 413789Sahrens /* 414789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 415789Sahrens * argument is a mask of acceptable types. The function will print an 416789Sahrens * appropriate error message and return NULL if it can't be opened. 417789Sahrens */ 418789Sahrens zfs_handle_t * 4192082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 420789Sahrens { 421789Sahrens zfs_handle_t *zhp; 4222082Seschrock char errbuf[1024]; 4232082Seschrock 4242082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 4252082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 426789Sahrens 427789Sahrens /* 4282082Seschrock * Validate the name before we even try to open it. 429789Sahrens */ 4305326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 4312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4322082Seschrock "invalid dataset name")); 4332082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 434789Sahrens return (NULL); 435789Sahrens } 436789Sahrens 437789Sahrens /* 438789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 439789Sahrens */ 440789Sahrens errno = 0; 4412082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 4423237Slling (void) zfs_standard_error(hdl, errno, errbuf); 443789Sahrens return (NULL); 444789Sahrens } 445789Sahrens 446789Sahrens if (!(types & zhp->zfs_type)) { 4472082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 4482142Seschrock zfs_close(zhp); 449789Sahrens return (NULL); 450789Sahrens } 451789Sahrens 452789Sahrens return (zhp); 453789Sahrens } 454789Sahrens 455789Sahrens /* 456789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 457789Sahrens */ 458789Sahrens void 459789Sahrens zfs_close(zfs_handle_t *zhp) 460789Sahrens { 461789Sahrens if (zhp->zfs_mntopts) 462789Sahrens free(zhp->zfs_mntopts); 4632676Seschrock nvlist_free(zhp->zfs_props); 4642676Seschrock nvlist_free(zhp->zfs_user_props); 465789Sahrens free(zhp); 466789Sahrens } 467789Sahrens 4685713Srm160521 int 4695713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 4705713Srm160521 { 4715713Srm160521 char *pool_name; 4725713Srm160521 zpool_handle_t *zpool_handle; 4735713Srm160521 char *p; 4745713Srm160521 4755713Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, MAXPATHLEN); 4765713Srm160521 if (zfs_prop_get(zhp, ZFS_PROP_NAME, pool_name, 4775713Srm160521 MAXPATHLEN, NULL, NULL, 0, B_FALSE) != 0) { 4785713Srm160521 free(pool_name); 4795713Srm160521 return (-1); 4805713Srm160521 } 4815713Srm160521 4825713Srm160521 if (p = strchr(pool_name, '/')) 4835713Srm160521 *p = '\0'; 4845713Srm160521 zpool_handle = zpool_open(zhp->zfs_hdl, pool_name); 4855713Srm160521 free(pool_name); 4865713Srm160521 if (zpool_handle == NULL) 4875713Srm160521 return (-1); 4885713Srm160521 4895713Srm160521 *spa_version = zpool_get_prop_int(zpool_handle, 4905713Srm160521 ZPOOL_PROP_VERSION, NULL); 4915713Srm160521 zpool_close(zpool_handle); 4925713Srm160521 return (0); 4935713Srm160521 } 4945713Srm160521 4955713Srm160521 /* 4965713Srm160521 * The choice of reservation property depends on the SPA version. 4975713Srm160521 */ 4985713Srm160521 static int 4995713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 5005713Srm160521 { 5015713Srm160521 int spa_version; 5025713Srm160521 5035713Srm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 5045713Srm160521 return (-1); 5055713Srm160521 5065713Srm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 5075713Srm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 5085713Srm160521 else 5095713Srm160521 *resv_prop = ZFS_PROP_RESERVATION; 5105713Srm160521 5115713Srm160521 return (0); 5125713Srm160521 } 5135713Srm160521 5143912Slling /* 5152676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 5162676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 5172676Seschrock * strings. 518789Sahrens */ 5195094Slling static nvlist_t * 5205094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 5215094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 522789Sahrens { 5232676Seschrock nvpair_t *elem; 5242676Seschrock uint64_t intval; 5252676Seschrock char *strval; 5265094Slling zfs_prop_t prop; 5272676Seschrock nvlist_t *ret; 5285331Samw int chosen_normal = -1; 5295331Samw int chosen_utf = -1; 5302676Seschrock 5312676Seschrock if (type == ZFS_TYPE_SNAPSHOT) { 5322676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5333413Smmusante "snapshot properties cannot be modified")); 5342676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 5355094Slling return (NULL); 5365094Slling } 5375094Slling 5385094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 5395094Slling (void) no_memory(hdl); 5405094Slling return (NULL); 541789Sahrens } 542789Sahrens 5432676Seschrock elem = NULL; 5442676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 5455094Slling const char *propname = nvpair_name(elem); 5462676Seschrock 5472676Seschrock /* 5482676Seschrock * Make sure this property is valid and applies to this type. 5492676Seschrock */ 5505094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 5515094Slling if (!zfs_prop_user(propname)) { 5522676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5535094Slling "invalid property '%s'"), propname); 5545094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5555094Slling goto error; 5565094Slling } 5575094Slling 5585094Slling /* 5595094Slling * If this is a user property, make sure it's a 5605094Slling * string, and that it's less than ZAP_MAXNAMELEN. 5615094Slling */ 5625094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 5635094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5645094Slling "'%s' must be a string"), propname); 5655094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5665094Slling goto error; 5675094Slling } 5685094Slling 5695094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 5705094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5715094Slling "property name '%s' is too long"), 5722676Seschrock propname); 5732676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5742676Seschrock goto error; 5752676Seschrock } 5762676Seschrock 5772676Seschrock (void) nvpair_value_string(elem, &strval); 5782676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 5792676Seschrock (void) no_memory(hdl); 5802676Seschrock goto error; 5812676Seschrock } 5822676Seschrock continue; 583789Sahrens } 5842676Seschrock 5852676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 5862676Seschrock zfs_error_aux(hdl, 5872676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 5882676Seschrock "apply to datasets of this type"), propname); 5892676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 5902676Seschrock goto error; 5912676Seschrock } 5922676Seschrock 5932676Seschrock if (zfs_prop_readonly(prop) && 5945331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 5952676Seschrock zfs_error_aux(hdl, 5962676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 5972676Seschrock propname); 5982676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 5992676Seschrock goto error; 6002676Seschrock } 6012676Seschrock 6025094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 6035094Slling &strval, &intval, errbuf) != 0) 6042676Seschrock goto error; 6052676Seschrock 6062676Seschrock /* 6072676Seschrock * Perform some additional checks for specific properties. 6082676Seschrock */ 6092676Seschrock switch (prop) { 6104577Sahrens case ZFS_PROP_VERSION: 6114577Sahrens { 6124577Sahrens int version; 6134577Sahrens 6144577Sahrens if (zhp == NULL) 6154577Sahrens break; 6164577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 6174577Sahrens if (intval < version) { 6184577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6194577Sahrens "Can not downgrade; already at version %u"), 6204577Sahrens version); 6214577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6224577Sahrens goto error; 6234577Sahrens } 6244577Sahrens break; 6254577Sahrens } 6264577Sahrens 6272676Seschrock case ZFS_PROP_RECORDSIZE: 6282676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 6292676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 6302676Seschrock if (intval < SPA_MINBLOCKSIZE || 6312676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 6322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6332676Seschrock "'%s' must be power of 2 from %u " 6342676Seschrock "to %uk"), propname, 6352676Seschrock (uint_t)SPA_MINBLOCKSIZE, 6362676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 6372676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6382676Seschrock goto error; 639789Sahrens } 640789Sahrens break; 641789Sahrens 6423126Sahl case ZFS_PROP_SHAREISCSI: 6433126Sahl if (strcmp(strval, "off") != 0 && 6443126Sahl strcmp(strval, "on") != 0 && 6453126Sahl strcmp(strval, "type=disk") != 0) { 6463126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6473126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 6483126Sahl propname); 6493126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6503126Sahl goto error; 6513126Sahl } 6523126Sahl 6533126Sahl break; 6543126Sahl 6552676Seschrock case ZFS_PROP_MOUNTPOINT: 6564778Srm160521 { 6574778Srm160521 namecheck_err_t why; 6584778Srm160521 6592676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 6602676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 6612676Seschrock break; 6622676Seschrock 6634778Srm160521 if (mountpoint_namecheck(strval, &why)) { 6644778Srm160521 switch (why) { 6654778Srm160521 case NAME_ERR_LEADING_SLASH: 6664778Srm160521 zfs_error_aux(hdl, 6674778Srm160521 dgettext(TEXT_DOMAIN, 6684778Srm160521 "'%s' must be an absolute path, " 6694778Srm160521 "'none', or 'legacy'"), propname); 6704778Srm160521 break; 6714778Srm160521 case NAME_ERR_TOOLONG: 6724778Srm160521 zfs_error_aux(hdl, 6734778Srm160521 dgettext(TEXT_DOMAIN, 6744778Srm160521 "component of '%s' is too long"), 6754778Srm160521 propname); 6764778Srm160521 break; 6774778Srm160521 } 6782676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6792676Seschrock goto error; 680789Sahrens } 6814778Srm160521 } 6824778Srm160521 6833126Sahl /*FALLTHRU*/ 6843126Sahl 6855331Samw case ZFS_PROP_SHARESMB: 6863126Sahl case ZFS_PROP_SHARENFS: 6873126Sahl /* 6885331Samw * For the mountpoint and sharenfs or sharesmb 6895331Samw * properties, check if it can be set in a 6905331Samw * global/non-global zone based on 6913126Sahl * the zoned property value: 6923126Sahl * 6933126Sahl * global zone non-global zone 6943126Sahl * -------------------------------------------------- 6953126Sahl * zoned=on mountpoint (no) mountpoint (yes) 6963126Sahl * sharenfs (no) sharenfs (no) 6975331Samw * sharesmb (no) sharesmb (no) 6983126Sahl * 6993126Sahl * zoned=off mountpoint (yes) N/A 7003126Sahl * sharenfs (yes) 7015331Samw * sharesmb (yes) 7023126Sahl */ 7032676Seschrock if (zoned) { 7042676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 7052676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7062676Seschrock "'%s' cannot be set on " 7072676Seschrock "dataset in a non-global zone"), 7082676Seschrock propname); 7092676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 7102676Seschrock errbuf); 7112676Seschrock goto error; 7125331Samw } else if (prop == ZFS_PROP_SHARENFS || 7135331Samw prop == ZFS_PROP_SHARESMB) { 7142676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7152676Seschrock "'%s' cannot be set in " 7162676Seschrock "a non-global zone"), propname); 7172676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 7182676Seschrock errbuf); 7192676Seschrock goto error; 7202676Seschrock } 7212676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 7222676Seschrock /* 7232676Seschrock * If zoned property is 'off', this must be in 7242676Seschrock * a globle zone. If not, something is wrong. 7252676Seschrock */ 7262676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7272676Seschrock "'%s' cannot be set while dataset " 7282676Seschrock "'zoned' property is set"), propname); 7292676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 7302676Seschrock goto error; 7312676Seschrock } 7323126Sahl 7334180Sdougm /* 7344180Sdougm * At this point, it is legitimate to set the 7354180Sdougm * property. Now we want to make sure that the 7364180Sdougm * property value is valid if it is sharenfs. 7374180Sdougm */ 7385331Samw if ((prop == ZFS_PROP_SHARENFS || 7395331Samw prop == ZFS_PROP_SHARESMB) && 7404217Seschrock strcmp(strval, "on") != 0 && 7414217Seschrock strcmp(strval, "off") != 0) { 7425331Samw zfs_share_proto_t proto; 7435331Samw 7445331Samw if (prop == ZFS_PROP_SHARESMB) 7455331Samw proto = PROTO_SMB; 7465331Samw else 7475331Samw proto = PROTO_NFS; 7484180Sdougm 7494180Sdougm /* 7505331Samw * Must be an valid sharing protocol 7515331Samw * option string so init the libshare 7525331Samw * in order to enable the parser and 7535331Samw * then parse the options. We use the 7545331Samw * control API since we don't care about 7555331Samw * the current configuration and don't 7564180Sdougm * want the overhead of loading it 7574180Sdougm * until we actually do something. 7584180Sdougm */ 7594180Sdougm 7604217Seschrock if (zfs_init_libshare(hdl, 7614217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 7624217Seschrock /* 7634217Seschrock * An error occurred so we can't do 7644217Seschrock * anything 7654217Seschrock */ 7664217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7674217Seschrock "'%s' cannot be set: problem " 7684217Seschrock "in share initialization"), 7694217Seschrock propname); 7704217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7714217Seschrock errbuf); 7724217Seschrock goto error; 7734217Seschrock } 7744217Seschrock 7755331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 7764217Seschrock /* 7774217Seschrock * There was an error in parsing so 7784217Seschrock * deal with it by issuing an error 7794217Seschrock * message and leaving after 7804217Seschrock * uninitializing the the libshare 7814217Seschrock * interface. 7824217Seschrock */ 7834217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7844217Seschrock "'%s' cannot be set to invalid " 7854217Seschrock "options"), propname); 7864217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7874217Seschrock errbuf); 7884217Seschrock zfs_uninit_libshare(hdl); 7894217Seschrock goto error; 7904217Seschrock } 7914180Sdougm zfs_uninit_libshare(hdl); 7924180Sdougm } 7934180Sdougm 7943126Sahl break; 7955331Samw case ZFS_PROP_UTF8ONLY: 7965331Samw chosen_utf = (int)intval; 7975331Samw break; 7985331Samw case ZFS_PROP_NORMALIZE: 7995331Samw chosen_normal = (int)intval; 8005331Samw break; 8012676Seschrock } 8022676Seschrock 8032676Seschrock /* 8042676Seschrock * For changes to existing volumes, we have some additional 8052676Seschrock * checks to enforce. 8062676Seschrock */ 8072676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 8082676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 8092676Seschrock ZFS_PROP_VOLSIZE); 8102676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 8112676Seschrock ZFS_PROP_VOLBLOCKSIZE); 8122676Seschrock char buf[64]; 8132676Seschrock 8142676Seschrock switch (prop) { 8152676Seschrock case ZFS_PROP_RESERVATION: 8165378Sck153898 case ZFS_PROP_REFRESERVATION: 8172676Seschrock if (intval > volsize) { 8182676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8192676Seschrock "'%s' is greater than current " 8202676Seschrock "volume size"), propname); 8212676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8222676Seschrock errbuf); 8232676Seschrock goto error; 8242676Seschrock } 8252676Seschrock break; 8262676Seschrock 8272676Seschrock case ZFS_PROP_VOLSIZE: 8282676Seschrock if (intval % blocksize != 0) { 8292676Seschrock zfs_nicenum(blocksize, buf, 8302676Seschrock sizeof (buf)); 8312676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8322676Seschrock "'%s' must be a multiple of " 8332676Seschrock "volume block size (%s)"), 8342676Seschrock propname, buf); 8352676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8362676Seschrock errbuf); 8372676Seschrock goto error; 8382676Seschrock } 8392676Seschrock 8402676Seschrock if (intval == 0) { 8412676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8422676Seschrock "'%s' cannot be zero"), 8432676Seschrock propname); 8442676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8452676Seschrock errbuf); 8462676Seschrock goto error; 847789Sahrens } 8483126Sahl break; 849789Sahrens } 850789Sahrens } 851789Sahrens } 852789Sahrens 8532676Seschrock /* 8545331Samw * If normalization was chosen, but no UTF8 choice was made, 8555331Samw * enforce rejection of non-UTF8 names. 8565331Samw * 8575331Samw * If normalization was chosen, but rejecting non-UTF8 names 8585331Samw * was explicitly not chosen, it is an error. 8595331Samw */ 8605498Stimh if (chosen_normal > 0 && chosen_utf < 0) { 8615331Samw if (nvlist_add_uint64(ret, 8625331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 8635331Samw (void) no_memory(hdl); 8645331Samw goto error; 8655331Samw } 8665498Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 8675331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8685331Samw "'%s' must be set 'on' if normalization chosen"), 8695331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 8705331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8715331Samw goto error; 8725331Samw } 8735331Samw 8745331Samw /* 8752676Seschrock * If this is an existing volume, and someone is setting the volsize, 8762676Seschrock * make sure that it matches the reservation, or add it if necessary. 8772676Seschrock */ 8782676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 8792676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 8802676Seschrock &intval) == 0) { 8812676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 8822676Seschrock ZFS_PROP_VOLSIZE); 8835481Sck153898 uint64_t old_reservation; 8842676Seschrock uint64_t new_reservation; 8855481Sck153898 zfs_prop_t resv_prop; 8865713Srm160521 8875713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 8885481Sck153898 goto error; 8895481Sck153898 old_reservation = zfs_prop_get_int(zhp, resv_prop); 8902676Seschrock 8912676Seschrock if (old_volsize == old_reservation && 8925481Sck153898 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop), 8932676Seschrock &new_reservation) != 0) { 8942676Seschrock if (nvlist_add_uint64(ret, 8955481Sck153898 zfs_prop_to_name(resv_prop), intval) != 0) { 8962676Seschrock (void) no_memory(hdl); 8972676Seschrock goto error; 8982676Seschrock } 8992676Seschrock } 9002676Seschrock } 9012676Seschrock return (ret); 9022676Seschrock 9032676Seschrock error: 9042676Seschrock nvlist_free(ret); 9052676Seschrock return (NULL); 906789Sahrens } 907789Sahrens 9084543Smarks static int 9094543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 9104543Smarks uint64_t *ret_who) 9114543Smarks { 9124543Smarks struct passwd *pwd; 9134543Smarks struct group *grp; 9144543Smarks uid_t id; 9154543Smarks 9164543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 9174543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 9184543Smarks *ret_who = -1; 9194543Smarks return (0); 9204543Smarks } 9214543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 9224543Smarks return (EZFS_BADWHO); 9234543Smarks 9244543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 9254543Smarks strcmp(who, "everyone") == 0) { 9264543Smarks *ret_who = -1; 9274543Smarks *who_type = ZFS_DELEG_EVERYONE; 9284543Smarks return (0); 9294543Smarks } 9304543Smarks 9314543Smarks pwd = getpwnam(who); 9324543Smarks grp = getgrnam(who); 9334543Smarks 9344543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 9354543Smarks *ret_who = pwd->pw_uid; 9364543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 9374543Smarks *ret_who = grp->gr_gid; 9384543Smarks } else if (pwd) { 9394543Smarks *ret_who = pwd->pw_uid; 9404543Smarks *who_type = ZFS_DELEG_USER; 9414543Smarks } else if (grp) { 9424543Smarks *ret_who = grp->gr_gid; 9434543Smarks *who_type = ZFS_DELEG_GROUP; 9444543Smarks } else { 9454543Smarks char *end; 9464543Smarks 9474543Smarks id = strtol(who, &end, 10); 9484543Smarks if (errno != 0 || *end != '\0') { 9494543Smarks return (EZFS_BADWHO); 9504543Smarks } else { 9514543Smarks *ret_who = id; 9524543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 9534543Smarks *who_type = ZFS_DELEG_USER; 9544543Smarks } 9554543Smarks } 9564543Smarks 9574543Smarks return (0); 9584543Smarks } 9594543Smarks 9604543Smarks static void 9614543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 9624543Smarks { 9634543Smarks if (perms_nvp != NULL) { 9644543Smarks verify(nvlist_add_nvlist(who_nvp, 9654543Smarks name, perms_nvp) == 0); 9664543Smarks } else { 9674543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 9684543Smarks } 9694543Smarks } 9704543Smarks 9714543Smarks static void 9724543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 9734543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 9744543Smarks nvlist_t *sets_nvp) 9754543Smarks { 9764543Smarks boolean_t do_perms, do_sets; 9774543Smarks char name[ZFS_MAX_DELEG_NAME]; 9784543Smarks 9794543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 9804543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 9814543Smarks 9824543Smarks if (!do_perms && !do_sets) 9834543Smarks do_perms = do_sets = B_TRUE; 9844543Smarks 9854543Smarks if (do_perms) { 9864543Smarks zfs_deleg_whokey(name, who_type, inherit, 9874543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9884543Smarks whostr : (void *)&whoid); 9894543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 9904543Smarks } 9914543Smarks if (do_sets) { 9924543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 9934543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9944543Smarks whostr : (void *)&whoid); 9954543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 9964543Smarks } 9974543Smarks } 9984543Smarks 9994543Smarks static void 10004543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 10014543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 10024543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 10034543Smarks { 10044543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 10054543Smarks helper(who_type, whoid, whostr, 0, 10064543Smarks who_nvp, perms_nvp, sets_nvp); 10074543Smarks } else { 10084543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 10094543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 10104543Smarks who_nvp, perms_nvp, sets_nvp); 10114543Smarks } 10124543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 10134543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 10144543Smarks who_nvp, perms_nvp, sets_nvp); 10154543Smarks } 10164543Smarks } 10174543Smarks } 10184543Smarks 10194543Smarks /* 10204543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 10214543Smarks * 10224543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 10234543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 10244543Smarks * base attribute named stored in the dsl. 10254543Smarks * Arguments: 10264543Smarks * 10274543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 10284543Smarks * whostr may be null for everyone or create perms. 10294543Smarks * who_type: is the type of entry in whostr. Typically this will be 10304543Smarks * ZFS_DELEG_WHO_UNKNOWN. 10315331Samw * perms: common separated list of permissions. May be null if user 10324543Smarks * is requested to remove permissions by who. 10334543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 10344543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 10354543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 10364543Smarks * The output nvp will look something like this. 10374543Smarks * ul$1234 -> {create ; destroy } 10384543Smarks * Ul$1234 -> { @myset } 10394543Smarks * s-$@myset - { snapshot; checksum; compression } 10404543Smarks */ 10414543Smarks int 10424543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 10434543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 10444543Smarks { 10454543Smarks nvlist_t *who_nvp; 10464543Smarks nvlist_t *perms_nvp = NULL; 10474543Smarks nvlist_t *sets_nvp = NULL; 10484543Smarks char errbuf[1024]; 10494787Sahrens char *who_tok, *perm; 10504543Smarks int error; 10514543Smarks 10524543Smarks *nvp = NULL; 10534543Smarks 10544543Smarks if (perms) { 10554543Smarks if ((error = nvlist_alloc(&perms_nvp, 10564543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10574543Smarks return (1); 10584543Smarks } 10594543Smarks if ((error = nvlist_alloc(&sets_nvp, 10604543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10614543Smarks nvlist_free(perms_nvp); 10624543Smarks return (1); 10634543Smarks } 10644543Smarks } 10654543Smarks 10664543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 10674543Smarks if (perms_nvp) 10684543Smarks nvlist_free(perms_nvp); 10694543Smarks if (sets_nvp) 10704543Smarks nvlist_free(sets_nvp); 10714543Smarks return (1); 10724543Smarks } 10734543Smarks 10744543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 10754543Smarks namecheck_err_t why; 10764543Smarks char what; 10774543Smarks 10784543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 10794787Sahrens nvlist_free(who_nvp); 10804787Sahrens if (perms_nvp) 10814787Sahrens nvlist_free(perms_nvp); 10824787Sahrens if (sets_nvp) 10834787Sahrens nvlist_free(sets_nvp); 10844787Sahrens 10854543Smarks switch (why) { 10864543Smarks case NAME_ERR_NO_AT: 10874543Smarks zfs_error_aux(zhp->zfs_hdl, 10884543Smarks dgettext(TEXT_DOMAIN, 10894543Smarks "set definition must begin with an '@' " 10904543Smarks "character")); 10914543Smarks } 10924543Smarks return (zfs_error(zhp->zfs_hdl, 10934543Smarks EZFS_BADPERMSET, whostr)); 10944543Smarks } 10954543Smarks } 10964543Smarks 10974543Smarks /* 10984543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 10994543Smarks * The first nvlist perms_nvp will have normal permissions and the 11004543Smarks * other sets_nvp will have only permssion set names in it. 11014543Smarks */ 11024787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 11034787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 11044787Sahrens 11054787Sahrens if (perm_canonical) { 11064787Sahrens verify(nvlist_add_boolean(perms_nvp, 11074787Sahrens perm_canonical) == 0); 11084787Sahrens } else if (perm[0] == '@') { 11094787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 11104787Sahrens } else { 11114787Sahrens nvlist_free(who_nvp); 11124787Sahrens nvlist_free(perms_nvp); 11134787Sahrens nvlist_free(sets_nvp); 11144787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 11154543Smarks } 11164543Smarks } 11174543Smarks 11184543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 11194543Smarks who_tok = strtok(whostr, ","); 11204543Smarks if (who_tok == NULL) { 11214543Smarks nvlist_free(who_nvp); 11224787Sahrens if (perms_nvp) 11234787Sahrens nvlist_free(perms_nvp); 11244543Smarks if (sets_nvp) 11254543Smarks nvlist_free(sets_nvp); 11264543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11274543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 11284543Smarks whostr); 11294543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11304543Smarks } 11314543Smarks } 11324543Smarks 11334543Smarks /* 11344543Smarks * Now create the nvlist(s) 11354543Smarks */ 11364543Smarks do { 11374543Smarks uint64_t who_id; 11384543Smarks 11394543Smarks error = zfs_get_perm_who(who_tok, &who_type, 11404543Smarks &who_id); 11414543Smarks if (error) { 11424543Smarks nvlist_free(who_nvp); 11434787Sahrens if (perms_nvp) 11444787Sahrens nvlist_free(perms_nvp); 11454543Smarks if (sets_nvp) 11464543Smarks nvlist_free(sets_nvp); 11474543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11484543Smarks dgettext(TEXT_DOMAIN, 11494543Smarks "Unable to determine uid/gid for " 11504543Smarks "%s "), who_tok); 11514543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11524543Smarks } 11534543Smarks 11544543Smarks /* 11554543Smarks * add entries for both local and descendent when required 11564543Smarks */ 11574543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 11584543Smarks perms_nvp, sets_nvp, who_type, inherit); 11594543Smarks 11604543Smarks } while (who_tok = strtok(NULL, ",")); 11614543Smarks *nvp = who_nvp; 11624543Smarks return (0); 11634543Smarks } 11644543Smarks 11654543Smarks static int 11664543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 11674543Smarks { 11684543Smarks zfs_cmd_t zc = { 0 }; 11694543Smarks int error; 11704543Smarks char errbuf[1024]; 11714543Smarks 11724543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11734543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 11744543Smarks zhp->zfs_name); 11754543Smarks 11765094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 11774543Smarks return (-1); 11784543Smarks 11794543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 11804543Smarks zc.zc_perm_action = unset; 11814543Smarks 11824543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 11834543Smarks if (error && errno == ENOTSUP) { 11844543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11854543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 11864543Smarks zcmd_free_nvlists(&zc); 11874543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 11884543Smarks } else if (error) { 11894543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 11904543Smarks } 11914543Smarks zcmd_free_nvlists(&zc); 11924543Smarks 11934543Smarks return (error); 11944543Smarks } 11954543Smarks 11964543Smarks int 11974543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 11984543Smarks { 11994543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 12004543Smarks } 12014543Smarks 12024543Smarks int 12034543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 12044543Smarks { 12054543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 12064543Smarks } 12074543Smarks 12084543Smarks static int 12094543Smarks perm_compare(const void *arg1, const void *arg2) 12104543Smarks { 12114543Smarks const zfs_perm_node_t *node1 = arg1; 12124543Smarks const zfs_perm_node_t *node2 = arg2; 12134543Smarks int ret; 12144543Smarks 12154543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 12164543Smarks 12174543Smarks if (ret > 0) 12184543Smarks return (1); 12194543Smarks if (ret < 0) 12204543Smarks return (-1); 12214543Smarks else 12224543Smarks return (0); 12234543Smarks } 12244543Smarks 12254543Smarks static void 12264543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 12274543Smarks { 12284543Smarks zfs_perm_node_t *permnode; 12295367Sahrens void *cookie = NULL; 12305367Sahrens 12315367Sahrens while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 12324543Smarks free(permnode); 12335367Sahrens avl_destroy(tree); 12344543Smarks } 12354543Smarks 12364543Smarks static void 12374543Smarks zfs_destroy_tree(avl_tree_t *tree) 12384543Smarks { 12394543Smarks zfs_allow_node_t *allownode; 12405367Sahrens void *cookie = NULL; 12415367Sahrens 12424543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 12434543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 12444543Smarks zfs_destroy_perm_tree(&allownode->z_local); 12454543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 12464543Smarks free(allownode); 12474543Smarks } 12485367Sahrens avl_destroy(tree); 12494543Smarks } 12504543Smarks 12514543Smarks void 12524543Smarks zfs_free_allows(zfs_allow_t *allow) 12534543Smarks { 12544543Smarks zfs_allow_t *allownext; 12554543Smarks zfs_allow_t *freeallow; 12564543Smarks 12574543Smarks allownext = allow; 12584543Smarks while (allownext) { 12594543Smarks zfs_destroy_tree(&allownext->z_sets); 12604543Smarks zfs_destroy_tree(&allownext->z_crperms); 12614543Smarks zfs_destroy_tree(&allownext->z_user); 12624543Smarks zfs_destroy_tree(&allownext->z_group); 12634543Smarks zfs_destroy_tree(&allownext->z_everyone); 12644543Smarks freeallow = allownext; 12654543Smarks allownext = allownext->z_next; 12664543Smarks free(freeallow); 12674543Smarks } 12684543Smarks } 12694543Smarks 12704543Smarks static zfs_allow_t * 12714543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 12724543Smarks { 12734543Smarks zfs_allow_t *ptree; 12744543Smarks 12754543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 12764543Smarks sizeof (zfs_allow_t))) == NULL) { 12774543Smarks return (NULL); 12784543Smarks } 12794543Smarks 12804543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 12814543Smarks avl_create(&ptree->z_sets, 12824543Smarks perm_compare, sizeof (zfs_allow_node_t), 12834543Smarks offsetof(zfs_allow_node_t, z_node)); 12844543Smarks avl_create(&ptree->z_crperms, 12854543Smarks perm_compare, sizeof (zfs_allow_node_t), 12864543Smarks offsetof(zfs_allow_node_t, z_node)); 12874543Smarks avl_create(&ptree->z_user, 12884543Smarks perm_compare, sizeof (zfs_allow_node_t), 12894543Smarks offsetof(zfs_allow_node_t, z_node)); 12904543Smarks avl_create(&ptree->z_group, 12914543Smarks perm_compare, sizeof (zfs_allow_node_t), 12924543Smarks offsetof(zfs_allow_node_t, z_node)); 12934543Smarks avl_create(&ptree->z_everyone, 12944543Smarks perm_compare, sizeof (zfs_allow_node_t), 12954543Smarks offsetof(zfs_allow_node_t, z_node)); 12964543Smarks 12974543Smarks if (prev) 12984543Smarks prev->z_next = ptree; 12994543Smarks ptree->z_next = NULL; 13004543Smarks return (ptree); 13014543Smarks } 13024543Smarks 13034543Smarks /* 13044543Smarks * Add permissions to the appropriate AVL permission tree. 13054543Smarks * The appropriate tree may not be the requested tree. 13064543Smarks * For example if ld indicates a local permission, but 13074543Smarks * same permission also exists as a descendent permission 13084543Smarks * then the permission will be removed from the descendent 13094543Smarks * tree and add the the local+descendent tree. 13104543Smarks */ 13114543Smarks static int 13124543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 13134543Smarks char *perm, char ld) 13144543Smarks { 13154543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 13164543Smarks zfs_perm_node_t *newnode; 13174543Smarks avl_index_t where, where2; 13184543Smarks avl_tree_t *tree, *altree; 13194543Smarks 13204543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 13214543Smarks 13224543Smarks if (ld == ZFS_DELEG_NA) { 13234543Smarks tree = &allownode->z_localdescend; 13244543Smarks altree = &allownode->z_descend; 13254543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 13264543Smarks tree = &allownode->z_local; 13274543Smarks altree = &allownode->z_descend; 13284543Smarks } else { 13294543Smarks tree = &allownode->z_descend; 13304543Smarks altree = &allownode->z_local; 13314543Smarks } 13324543Smarks permnode = avl_find(tree, &pnode, &where); 13334543Smarks permnode2 = avl_find(altree, &pnode, &where2); 13344543Smarks 13354543Smarks if (permnode2) { 13364543Smarks avl_remove(altree, permnode2); 13374543Smarks free(permnode2); 13384543Smarks if (permnode == NULL) { 13394543Smarks tree = &allownode->z_localdescend; 13404543Smarks } 13414543Smarks } 13424543Smarks 13434543Smarks /* 13444543Smarks * Now insert new permission in either requested location 13454543Smarks * local/descendent or into ld when perm will exist in both. 13464543Smarks */ 13474543Smarks if (permnode == NULL) { 13484543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 13494543Smarks sizeof (zfs_perm_node_t))) == NULL) { 13504543Smarks return (-1); 13514543Smarks } 13524543Smarks *newnode = pnode; 13534543Smarks avl_add(tree, newnode); 13544543Smarks } 13554543Smarks return (0); 13564543Smarks } 13574577Sahrens 13584543Smarks /* 13594543Smarks * Uggh, this is going to be a bit complicated. 13604543Smarks * we have an nvlist coming out of the kernel that 13614543Smarks * will indicate where the permission is set and then 13624543Smarks * it will contain allow of the various "who's", and what 13634543Smarks * their permissions are. To further complicate this 13644543Smarks * we will then have to coalesce the local,descendent 13654543Smarks * and local+descendent permissions where appropriate. 13664543Smarks * The kernel only knows about a permission as being local 13674543Smarks * or descendent, but not both. 13684543Smarks * 13694543Smarks * In order to make this easier for zfs_main to deal with 13704543Smarks * a series of AVL trees will be used to maintain 13714543Smarks * all of this, primarily for sorting purposes as well 13724543Smarks * as the ability to quickly locate a specific entry. 13734543Smarks * 13744543Smarks * What we end up with are tree's for sets, create perms, 13754543Smarks * user, groups and everyone. With each of those trees 13764543Smarks * we have subtrees for local, descendent and local+descendent 13774543Smarks * permissions. 13784543Smarks */ 13794543Smarks int 13804543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 13814543Smarks { 13824543Smarks zfs_cmd_t zc = { 0 }; 13834543Smarks int error; 13844543Smarks nvlist_t *nvlist; 13854543Smarks nvlist_t *permnv, *sourcenv; 13864543Smarks nvpair_t *who_pair, *source_pair; 13874543Smarks nvpair_t *perm_pair; 13884543Smarks char errbuf[1024]; 13894543Smarks zfs_allow_t *zallowp, *newallowp; 13904543Smarks char ld; 13914543Smarks char *nvpname; 13924543Smarks uid_t uid; 13934543Smarks gid_t gid; 13944543Smarks avl_tree_t *tree; 13954543Smarks avl_index_t where; 13964543Smarks 13974543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13984543Smarks 13994543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 14004543Smarks return (-1); 14014543Smarks 14024543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 14034543Smarks if (errno == ENOMEM) { 14044543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 14054543Smarks zcmd_free_nvlists(&zc); 14064543Smarks return (-1); 14074543Smarks } 14084543Smarks } else if (errno == ENOTSUP) { 14094543Smarks zcmd_free_nvlists(&zc); 14104543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14114543Smarks gettext("Pool must be upgraded to use 'allow'")); 14124543Smarks return (zfs_error(zhp->zfs_hdl, 14134543Smarks EZFS_BADVERSION, errbuf)); 14144543Smarks } else { 14154543Smarks zcmd_free_nvlists(&zc); 14164543Smarks return (-1); 14174543Smarks } 14184543Smarks } 14194543Smarks 14204543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 14214543Smarks zcmd_free_nvlists(&zc); 14224543Smarks return (-1); 14234543Smarks } 14244543Smarks 14254543Smarks zcmd_free_nvlists(&zc); 14264543Smarks 14274543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 14284543Smarks 14294543Smarks if (source_pair == NULL) { 14304543Smarks *zfs_perms = NULL; 14314543Smarks return (0); 14324543Smarks } 14334543Smarks 14344543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 14354543Smarks if (*zfs_perms == NULL) { 14364543Smarks return (0); 14374543Smarks } 14384543Smarks 14394543Smarks zallowp = *zfs_perms; 14404543Smarks 14414543Smarks for (;;) { 14424543Smarks struct passwd *pwd; 14434543Smarks struct group *grp; 14444543Smarks zfs_allow_node_t *allownode; 14454543Smarks zfs_allow_node_t findallownode; 14464543Smarks zfs_allow_node_t *newallownode; 14474543Smarks 14484543Smarks (void) strlcpy(zallowp->z_setpoint, 14494543Smarks nvpair_name(source_pair), 14504543Smarks sizeof (zallowp->z_setpoint)); 14514543Smarks 14524543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 14534543Smarks goto abort; 14544543Smarks 14554543Smarks /* 14564543Smarks * Make sure nvlist is composed correctly 14574543Smarks */ 14584543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 14594543Smarks goto abort; 14604543Smarks } 14614543Smarks 14624543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 14634543Smarks if (who_pair == NULL) { 14644543Smarks goto abort; 14654543Smarks } 14664543Smarks 14674543Smarks do { 14684543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 14694543Smarks if (error) { 14704543Smarks goto abort; 14714543Smarks } 14724543Smarks 14734543Smarks /* 14744543Smarks * First build up the key to use 14754543Smarks * for looking up in the various 14764543Smarks * who trees. 14774543Smarks */ 14784543Smarks ld = nvpair_name(who_pair)[1]; 14794543Smarks nvpname = nvpair_name(who_pair); 14804543Smarks switch (nvpair_name(who_pair)[0]) { 14814543Smarks case ZFS_DELEG_USER: 14824543Smarks case ZFS_DELEG_USER_SETS: 14834543Smarks tree = &zallowp->z_user; 14844543Smarks uid = atol(&nvpname[3]); 14854543Smarks pwd = getpwuid(uid); 14864543Smarks (void) snprintf(findallownode.z_key, 14874543Smarks sizeof (findallownode.z_key), "user %s", 14884543Smarks (pwd) ? pwd->pw_name : 14894543Smarks &nvpair_name(who_pair)[3]); 14904543Smarks break; 14914543Smarks case ZFS_DELEG_GROUP: 14924543Smarks case ZFS_DELEG_GROUP_SETS: 14934543Smarks tree = &zallowp->z_group; 14944543Smarks gid = atol(&nvpname[3]); 14954543Smarks grp = getgrgid(gid); 14964543Smarks (void) snprintf(findallownode.z_key, 14974543Smarks sizeof (findallownode.z_key), "group %s", 14984543Smarks (grp) ? grp->gr_name : 14994543Smarks &nvpair_name(who_pair)[3]); 15004543Smarks break; 15014543Smarks case ZFS_DELEG_CREATE: 15024543Smarks case ZFS_DELEG_CREATE_SETS: 15034543Smarks tree = &zallowp->z_crperms; 15044543Smarks (void) strlcpy(findallownode.z_key, "", 15054543Smarks sizeof (findallownode.z_key)); 15064543Smarks break; 15074543Smarks case ZFS_DELEG_EVERYONE: 15084543Smarks case ZFS_DELEG_EVERYONE_SETS: 15094543Smarks (void) snprintf(findallownode.z_key, 15104543Smarks sizeof (findallownode.z_key), "everyone"); 15114543Smarks tree = &zallowp->z_everyone; 15124543Smarks break; 15134543Smarks case ZFS_DELEG_NAMED_SET: 15144543Smarks case ZFS_DELEG_NAMED_SET_SETS: 15154543Smarks (void) snprintf(findallownode.z_key, 15164543Smarks sizeof (findallownode.z_key), "%s", 15174543Smarks &nvpair_name(who_pair)[3]); 15184543Smarks tree = &zallowp->z_sets; 15194543Smarks break; 15204543Smarks } 15214543Smarks 15224543Smarks /* 15234543Smarks * Place who in tree 15244543Smarks */ 15254543Smarks allownode = avl_find(tree, &findallownode, &where); 15264543Smarks if (allownode == NULL) { 15274543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 15284543Smarks sizeof (zfs_allow_node_t))) == NULL) { 15294543Smarks goto abort; 15304543Smarks } 15314543Smarks avl_create(&newallownode->z_localdescend, 15324543Smarks perm_compare, 15334543Smarks sizeof (zfs_perm_node_t), 15344543Smarks offsetof(zfs_perm_node_t, z_node)); 15354543Smarks avl_create(&newallownode->z_local, 15364543Smarks perm_compare, 15374543Smarks sizeof (zfs_perm_node_t), 15384543Smarks offsetof(zfs_perm_node_t, z_node)); 15394543Smarks avl_create(&newallownode->z_descend, 15404543Smarks perm_compare, 15414543Smarks sizeof (zfs_perm_node_t), 15424543Smarks offsetof(zfs_perm_node_t, z_node)); 15434543Smarks (void) strlcpy(newallownode->z_key, 15444543Smarks findallownode.z_key, 15454543Smarks sizeof (findallownode.z_key)); 15464543Smarks avl_insert(tree, newallownode, where); 15474543Smarks allownode = newallownode; 15484543Smarks } 15494543Smarks 15504543Smarks /* 15514543Smarks * Now iterate over the permissions and 15524543Smarks * place them in the appropriate local, 15534543Smarks * descendent or local+descendent tree. 15544543Smarks * 15554543Smarks * The permissions are added to the tree 15564543Smarks * via zfs_coalesce_perm(). 15574543Smarks */ 15584543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 15594543Smarks if (perm_pair == NULL) 15604543Smarks goto abort; 15614543Smarks do { 15624543Smarks if (zfs_coalesce_perm(zhp, allownode, 15634543Smarks nvpair_name(perm_pair), ld) != 0) 15644543Smarks goto abort; 15654543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 15664543Smarks perm_pair)); 15674543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 15684543Smarks 15694543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 15704543Smarks if (source_pair == NULL) 15714543Smarks break; 15724543Smarks 15734543Smarks /* 15744543Smarks * allocate another node from the link list of 15754543Smarks * zfs_allow_t structures 15764543Smarks */ 15774543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 15784543Smarks nvpair_name(source_pair)); 15794543Smarks if (newallowp == NULL) { 15804543Smarks goto abort; 15814543Smarks } 15824543Smarks zallowp = newallowp; 15834543Smarks } 15844543Smarks nvlist_free(nvlist); 15854543Smarks return (0); 15864543Smarks abort: 15874543Smarks zfs_free_allows(*zfs_perms); 15884543Smarks nvlist_free(nvlist); 15894543Smarks return (-1); 15904543Smarks } 15914543Smarks 1592789Sahrens /* 1593789Sahrens * Given a property name and value, set the property for the given dataset. 1594789Sahrens */ 1595789Sahrens int 15962676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1597789Sahrens { 1598789Sahrens zfs_cmd_t zc = { 0 }; 15992676Seschrock int ret = -1; 16002676Seschrock prop_changelist_t *cl = NULL; 16012082Seschrock char errbuf[1024]; 16022082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 16032676Seschrock nvlist_t *nvl = NULL, *realprops; 16042676Seschrock zfs_prop_t prop; 16052082Seschrock 16062082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 16072676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 16082082Seschrock zhp->zfs_name); 16092082Seschrock 16102676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 16112676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 16122676Seschrock (void) no_memory(hdl); 16132676Seschrock goto error; 1614789Sahrens } 1615789Sahrens 16165094Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 16172676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 16182676Seschrock goto error; 16195094Slling 16202676Seschrock nvlist_free(nvl); 16212676Seschrock nvl = realprops; 16222676Seschrock 16232676Seschrock prop = zfs_name_to_prop(propname); 16242676Seschrock 1625789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 16262676Seschrock goto error; 1627789Sahrens 1628789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 16292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16302082Seschrock "child dataset with inherited mountpoint is used " 16312082Seschrock "in a non-global zone")); 16322082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1633789Sahrens goto error; 1634789Sahrens } 1635789Sahrens 1636789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1637789Sahrens goto error; 1638789Sahrens 1639789Sahrens /* 1640789Sahrens * Execute the corresponding ioctl() to set this property. 1641789Sahrens */ 1642789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1643789Sahrens 16445094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 16452676Seschrock goto error; 16462676Seschrock 16474543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1648789Sahrens if (ret != 0) { 1649789Sahrens switch (errno) { 1650789Sahrens 1651789Sahrens case ENOSPC: 1652789Sahrens /* 1653789Sahrens * For quotas and reservations, ENOSPC indicates 1654789Sahrens * something different; setting a quota or reservation 1655789Sahrens * doesn't use any disk space. 1656789Sahrens */ 1657789Sahrens switch (prop) { 1658789Sahrens case ZFS_PROP_QUOTA: 16595378Sck153898 case ZFS_PROP_REFQUOTA: 16602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16612082Seschrock "size is less than current used or " 16622082Seschrock "reserved space")); 16632082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1664789Sahrens break; 1665789Sahrens 1666789Sahrens case ZFS_PROP_RESERVATION: 16675378Sck153898 case ZFS_PROP_REFRESERVATION: 16682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16692082Seschrock "size is greater than available space")); 16702082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1671789Sahrens break; 1672789Sahrens 1673789Sahrens default: 16742082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1675789Sahrens break; 1676789Sahrens } 1677789Sahrens break; 1678789Sahrens 1679789Sahrens case EBUSY: 16802082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 16812082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 16822082Seschrock else 16832676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1684789Sahrens break; 1685789Sahrens 16861175Slling case EROFS: 16872082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 16881175Slling break; 16891175Slling 16903886Sahl case ENOTSUP: 16913886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16924603Sahrens "pool must be upgraded to set this " 16934603Sahrens "property or value")); 16943886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 16953886Sahl break; 16963886Sahl 1697789Sahrens case EOVERFLOW: 1698789Sahrens /* 1699789Sahrens * This platform can't address a volume this big. 1700789Sahrens */ 1701789Sahrens #ifdef _ILP32 1702789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 17032082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1704789Sahrens break; 1705789Sahrens } 1706789Sahrens #endif 17072082Seschrock /* FALLTHROUGH */ 1708789Sahrens default: 17092082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1710789Sahrens } 1711789Sahrens } else { 1712789Sahrens /* 1713789Sahrens * Refresh the statistics so the new property value 1714789Sahrens * is reflected. 1715789Sahrens */ 17162676Seschrock if ((ret = changelist_postfix(cl)) == 0) 17172676Seschrock (void) get_stats(zhp); 1718789Sahrens } 1719789Sahrens 1720789Sahrens error: 17212676Seschrock nvlist_free(nvl); 17222676Seschrock zcmd_free_nvlists(&zc); 17232676Seschrock if (cl) 17242676Seschrock changelist_free(cl); 1725789Sahrens return (ret); 1726789Sahrens } 1727789Sahrens 1728789Sahrens /* 1729789Sahrens * Given a property, inherit the value from the parent dataset. 1730789Sahrens */ 1731789Sahrens int 17322676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1733789Sahrens { 1734789Sahrens zfs_cmd_t zc = { 0 }; 1735789Sahrens int ret; 1736789Sahrens prop_changelist_t *cl; 17372082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 17382082Seschrock char errbuf[1024]; 17392676Seschrock zfs_prop_t prop; 17402082Seschrock 17412082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 17422082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1743789Sahrens 17445094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 17452676Seschrock /* 17462676Seschrock * For user properties, the amount of work we have to do is very 17472676Seschrock * small, so just do it here. 17482676Seschrock */ 17492676Seschrock if (!zfs_prop_user(propname)) { 17502676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17512676Seschrock "invalid property")); 17522676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 17532676Seschrock } 17542676Seschrock 17552676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17562676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 17572676Seschrock 17584849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 17592676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 17602676Seschrock 17612676Seschrock return (0); 17622676Seschrock } 17632676Seschrock 1764789Sahrens /* 1765789Sahrens * Verify that this property is inheritable. 1766789Sahrens */ 17672082Seschrock if (zfs_prop_readonly(prop)) 17682082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 17692082Seschrock 17702082Seschrock if (!zfs_prop_inheritable(prop)) 17712082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1772789Sahrens 1773789Sahrens /* 1774789Sahrens * Check to see if the value applies to this type 1775789Sahrens */ 17762082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 17772082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1778789Sahrens 17793443Srm160521 /* 17803443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 17813443Srm160521 */ 17823443Srm160521 propname = zfs_prop_to_name(prop); 1783789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17842676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1785789Sahrens 1786789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1787789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 17882082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17892082Seschrock "dataset is used in a non-global zone")); 17902082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1791789Sahrens } 1792789Sahrens 1793789Sahrens /* 1794789Sahrens * Determine datasets which will be affected by this change, if any. 1795789Sahrens */ 1796789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1797789Sahrens return (-1); 1798789Sahrens 1799789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 18002082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18012082Seschrock "child dataset with inherited mountpoint is used " 18022082Seschrock "in a non-global zone")); 18032082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1804789Sahrens goto error; 1805789Sahrens } 1806789Sahrens 1807789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1808789Sahrens goto error; 1809789Sahrens 18104849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 18112082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1812789Sahrens } else { 1813789Sahrens 18142169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1815789Sahrens goto error; 1816789Sahrens 1817789Sahrens /* 1818789Sahrens * Refresh the statistics so the new property is reflected. 1819789Sahrens */ 1820789Sahrens (void) get_stats(zhp); 1821789Sahrens } 1822789Sahrens 1823789Sahrens error: 1824789Sahrens changelist_free(cl); 1825789Sahrens return (ret); 1826789Sahrens } 1827789Sahrens 1828789Sahrens /* 18291356Seschrock * True DSL properties are stored in an nvlist. The following two functions 18301356Seschrock * extract them appropriately. 18311356Seschrock */ 18321356Seschrock static uint64_t 18331356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18341356Seschrock { 18351356Seschrock nvlist_t *nv; 18361356Seschrock uint64_t value; 18371356Seschrock 18382885Sahrens *source = NULL; 18391356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18401356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 18415094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 18425094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18431356Seschrock } else { 18441356Seschrock value = zfs_prop_default_numeric(prop); 18451356Seschrock *source = ""; 18461356Seschrock } 18471356Seschrock 18481356Seschrock return (value); 18491356Seschrock } 18501356Seschrock 18511356Seschrock static char * 18521356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18531356Seschrock { 18541356Seschrock nvlist_t *nv; 18551356Seschrock char *value; 18561356Seschrock 18572885Sahrens *source = NULL; 18581356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18591356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 18605094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 18615094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18621356Seschrock } else { 18631356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 18641356Seschrock value = ""; 18651356Seschrock *source = ""; 18661356Seschrock } 18671356Seschrock 18681356Seschrock return (value); 18691356Seschrock } 18701356Seschrock 18711356Seschrock /* 1872789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1873789Sahrens * zfs_prop_get_int() are built using this interface. 1874789Sahrens * 1875789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1876789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1877789Sahrens * If they differ from the on-disk values, report the current values and mark 1878789Sahrens * the source "temporary". 1879789Sahrens */ 18802082Seschrock static int 18815094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 18822082Seschrock char **source, uint64_t *val) 1883789Sahrens { 18845147Srm160521 zfs_cmd_t zc = { 0 }; 18855592Stimh nvlist_t *zplprops = NULL; 1886789Sahrens struct mnttab mnt; 18873265Sahrens char *mntopt_on = NULL; 18883265Sahrens char *mntopt_off = NULL; 1889789Sahrens 1890789Sahrens *source = NULL; 1891789Sahrens 18923265Sahrens switch (prop) { 18933265Sahrens case ZFS_PROP_ATIME: 18943265Sahrens mntopt_on = MNTOPT_ATIME; 18953265Sahrens mntopt_off = MNTOPT_NOATIME; 18963265Sahrens break; 18973265Sahrens 18983265Sahrens case ZFS_PROP_DEVICES: 18993265Sahrens mntopt_on = MNTOPT_DEVICES; 19003265Sahrens mntopt_off = MNTOPT_NODEVICES; 19013265Sahrens break; 19023265Sahrens 19033265Sahrens case ZFS_PROP_EXEC: 19043265Sahrens mntopt_on = MNTOPT_EXEC; 19053265Sahrens mntopt_off = MNTOPT_NOEXEC; 19063265Sahrens break; 19073265Sahrens 19083265Sahrens case ZFS_PROP_READONLY: 19093265Sahrens mntopt_on = MNTOPT_RO; 19103265Sahrens mntopt_off = MNTOPT_RW; 19113265Sahrens break; 19123265Sahrens 19133265Sahrens case ZFS_PROP_SETUID: 19143265Sahrens mntopt_on = MNTOPT_SETUID; 19153265Sahrens mntopt_off = MNTOPT_NOSETUID; 19163265Sahrens break; 19173265Sahrens 19183265Sahrens case ZFS_PROP_XATTR: 19193265Sahrens mntopt_on = MNTOPT_XATTR; 19203265Sahrens mntopt_off = MNTOPT_NOXATTR; 19213265Sahrens break; 19225331Samw 19235331Samw case ZFS_PROP_NBMAND: 19245331Samw mntopt_on = MNTOPT_NBMAND; 19255331Samw mntopt_off = MNTOPT_NONBMAND; 19265331Samw break; 19273265Sahrens } 19283265Sahrens 19292474Seschrock /* 19302474Seschrock * Because looking up the mount options is potentially expensive 19312474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 19322474Seschrock * we're looking up a property which requires its presence. 19332474Seschrock */ 19342474Seschrock if (!zhp->zfs_mntcheck && 19353265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 19363265Sahrens struct mnttab entry, search = { 0 }; 19373265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 19382474Seschrock 19392474Seschrock search.mnt_special = (char *)zhp->zfs_name; 19402474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 19413265Sahrens rewind(mnttab); 19423265Sahrens 19433265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 19443265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 19453265Sahrens entry.mnt_mntopts); 19463265Sahrens if (zhp->zfs_mntopts == NULL) 19473265Sahrens return (-1); 19483265Sahrens } 19492474Seschrock 19502474Seschrock zhp->zfs_mntcheck = B_TRUE; 19512474Seschrock } 19522474Seschrock 1953789Sahrens if (zhp->zfs_mntopts == NULL) 1954789Sahrens mnt.mnt_mntopts = ""; 1955789Sahrens else 1956789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1957789Sahrens 1958789Sahrens switch (prop) { 1959789Sahrens case ZFS_PROP_ATIME: 19603265Sahrens case ZFS_PROP_DEVICES: 19613265Sahrens case ZFS_PROP_EXEC: 19623265Sahrens case ZFS_PROP_READONLY: 19633265Sahrens case ZFS_PROP_SETUID: 19643265Sahrens case ZFS_PROP_XATTR: 19655331Samw case ZFS_PROP_NBMAND: 19662082Seschrock *val = getprop_uint64(zhp, prop, source); 19672082Seschrock 19683265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 19692082Seschrock *val = B_TRUE; 1970789Sahrens if (src) 19715094Slling *src = ZPROP_SRC_TEMPORARY; 19723265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 19732082Seschrock *val = B_FALSE; 1974789Sahrens if (src) 19755094Slling *src = ZPROP_SRC_TEMPORARY; 1976789Sahrens } 19772082Seschrock break; 1978789Sahrens 19793265Sahrens case ZFS_PROP_CANMOUNT: 19802082Seschrock *val = getprop_uint64(zhp, prop, source); 19813417Srm160521 if (*val == 0) 19823417Srm160521 *source = zhp->zfs_name; 19833417Srm160521 else 19843417Srm160521 *source = ""; /* default */ 19852082Seschrock break; 1986789Sahrens 1987789Sahrens case ZFS_PROP_QUOTA: 19885378Sck153898 case ZFS_PROP_REFQUOTA: 1989789Sahrens case ZFS_PROP_RESERVATION: 19905378Sck153898 case ZFS_PROP_REFRESERVATION: 19912885Sahrens *val = getprop_uint64(zhp, prop, source); 19922885Sahrens if (*val == 0) 1993789Sahrens *source = ""; /* default */ 1994789Sahrens else 1995789Sahrens *source = zhp->zfs_name; 19962082Seschrock break; 1997789Sahrens 1998789Sahrens case ZFS_PROP_MOUNTED: 19992082Seschrock *val = (zhp->zfs_mntopts != NULL); 20002082Seschrock break; 2001789Sahrens 20023377Seschrock case ZFS_PROP_NUMCLONES: 20033377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 20043377Seschrock break; 20053377Seschrock 20065147Srm160521 case ZFS_PROP_VERSION: 20075498Stimh case ZFS_PROP_NORMALIZE: 20085498Stimh case ZFS_PROP_UTF8ONLY: 20095498Stimh case ZFS_PROP_CASE: 20105498Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 20115498Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 20125473Srm160521 return (-1); 20135147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 20145498Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 20155498Stimh zcmd_free_nvlists(&zc); 20165147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 20175498Stimh "unable to get %s property"), 20185498Stimh zfs_prop_to_name(prop)); 20195147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 20205147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 20215147Srm160521 } 20225498Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 20235498Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 20245498Stimh val) != 0) { 20255498Stimh zcmd_free_nvlists(&zc); 20265498Stimh zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 20275498Stimh "unable to get %s property"), 20285498Stimh zfs_prop_to_name(prop)); 20295498Stimh return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM, 20305498Stimh dgettext(TEXT_DOMAIN, "internal error"))); 20315498Stimh } 20325592Stimh if (zplprops) 20335592Stimh nvlist_free(zplprops); 20345498Stimh zcmd_free_nvlists(&zc); 20355147Srm160521 break; 20365147Srm160521 2037789Sahrens default: 20384577Sahrens switch (zfs_prop_get_type(prop)) { 20394787Sahrens case PROP_TYPE_NUMBER: 20404787Sahrens case PROP_TYPE_INDEX: 20414577Sahrens *val = getprop_uint64(zhp, prop, source); 20424577Sahrens break; 20434577Sahrens 20444787Sahrens case PROP_TYPE_STRING: 20454577Sahrens default: 20464577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 20474577Sahrens "cannot get non-numeric property")); 20484577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 20494577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 20504577Sahrens } 2051789Sahrens } 2052789Sahrens 2053789Sahrens return (0); 2054789Sahrens } 2055789Sahrens 2056789Sahrens /* 2057789Sahrens * Calculate the source type, given the raw source string. 2058789Sahrens */ 2059789Sahrens static void 20605094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2061789Sahrens char *statbuf, size_t statlen) 2062789Sahrens { 20635094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2064789Sahrens return; 2065789Sahrens 2066789Sahrens if (source == NULL) { 20675094Slling *srctype = ZPROP_SRC_NONE; 2068789Sahrens } else if (source[0] == '\0') { 20695094Slling *srctype = ZPROP_SRC_DEFAULT; 2070789Sahrens } else { 2071789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 20725094Slling *srctype = ZPROP_SRC_LOCAL; 2073789Sahrens } else { 2074789Sahrens (void) strlcpy(statbuf, source, statlen); 20755094Slling *srctype = ZPROP_SRC_INHERITED; 2076789Sahrens } 2077789Sahrens } 2078789Sahrens 2079789Sahrens } 2080789Sahrens 2081789Sahrens /* 2082789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2083789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2084789Sahrens * human-readable form. 2085789Sahrens * 2086789Sahrens * Returns 0 on success, or -1 on error. 2087789Sahrens */ 2088789Sahrens int 2089789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 20905094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2091789Sahrens { 2092789Sahrens char *source = NULL; 2093789Sahrens uint64_t val; 2094789Sahrens char *str; 2095789Sahrens const char *root; 20962676Seschrock const char *strval; 2097789Sahrens 2098789Sahrens /* 2099789Sahrens * Check to see if this property applies to our object 2100789Sahrens */ 2101789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2102789Sahrens return (-1); 2103789Sahrens 2104789Sahrens if (src) 21055094Slling *src = ZPROP_SRC_NONE; 2106789Sahrens 2107789Sahrens switch (prop) { 2108789Sahrens case ZFS_PROP_CREATION: 2109789Sahrens /* 2110789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2111789Sahrens * this into a string unless 'literal' is specified. 2112789Sahrens */ 2113789Sahrens { 21142885Sahrens val = getprop_uint64(zhp, prop, &source); 21152885Sahrens time_t time = (time_t)val; 2116789Sahrens struct tm t; 2117789Sahrens 2118789Sahrens if (literal || 2119789Sahrens localtime_r(&time, &t) == NULL || 2120789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2121789Sahrens &t) == 0) 21222885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2123789Sahrens } 2124789Sahrens break; 2125789Sahrens 2126789Sahrens case ZFS_PROP_MOUNTPOINT: 2127789Sahrens /* 2128789Sahrens * Getting the precise mountpoint can be tricky. 2129789Sahrens * 2130789Sahrens * - for 'none' or 'legacy', return those values. 2131789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2132789Sahrens * - for inherited mountpoints, we want to take everything 2133789Sahrens * after our ancestor and append it to the inherited value. 2134789Sahrens * 2135789Sahrens * If the pool has an alternate root, we want to prepend that 2136789Sahrens * root to any values we return. 2137789Sahrens */ 21381544Seschrock root = zhp->zfs_root; 21391356Seschrock str = getprop_string(zhp, prop, &source); 21401356Seschrock 21411356Seschrock if (str[0] == '\0') { 2142789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2143789Sahrens root, zhp->zfs_name); 21441356Seschrock } else if (str[0] == '/') { 21451356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2146789Sahrens 2147789Sahrens if (relpath[0] == '/') 2148789Sahrens relpath++; 21491356Seschrock if (str[1] == '\0') 21501356Seschrock str++; 2151789Sahrens 2152789Sahrens if (relpath[0] == '\0') 2153789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 21541356Seschrock root, str); 2155789Sahrens else 2156789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 21571356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2158789Sahrens relpath); 2159789Sahrens } else { 2160789Sahrens /* 'legacy' or 'none' */ 21611356Seschrock (void) strlcpy(propbuf, str, proplen); 2162789Sahrens } 2163789Sahrens 2164789Sahrens break; 2165789Sahrens 2166789Sahrens case ZFS_PROP_ORIGIN: 21672885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2168789Sahrens proplen); 2169789Sahrens /* 2170789Sahrens * If there is no parent at all, return failure to indicate that 2171789Sahrens * it doesn't apply to this dataset. 2172789Sahrens */ 2173789Sahrens if (propbuf[0] == '\0') 2174789Sahrens return (-1); 2175789Sahrens break; 2176789Sahrens 2177789Sahrens case ZFS_PROP_QUOTA: 21785378Sck153898 case ZFS_PROP_REFQUOTA: 2179789Sahrens case ZFS_PROP_RESERVATION: 21805378Sck153898 case ZFS_PROP_REFRESERVATION: 21815378Sck153898 21822082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21832082Seschrock return (-1); 2184789Sahrens 2185789Sahrens /* 2186789Sahrens * If quota or reservation is 0, we translate this into 'none' 2187789Sahrens * (unless literal is set), and indicate that it's the default 2188789Sahrens * value. Otherwise, we print the number nicely and indicate 2189789Sahrens * that its set locally. 2190789Sahrens */ 2191789Sahrens if (val == 0) { 2192789Sahrens if (literal) 2193789Sahrens (void) strlcpy(propbuf, "0", proplen); 2194789Sahrens else 2195789Sahrens (void) strlcpy(propbuf, "none", proplen); 2196789Sahrens } else { 2197789Sahrens if (literal) 21982856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 21993912Slling (u_longlong_t)val); 2200789Sahrens else 2201789Sahrens zfs_nicenum(val, propbuf, proplen); 2202789Sahrens } 2203789Sahrens break; 2204789Sahrens 2205789Sahrens case ZFS_PROP_COMPRESSRATIO: 22062082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 22072082Seschrock return (-1); 22082856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 22092856Snd150628 val / 100, (longlong_t)val % 100); 2210789Sahrens break; 2211789Sahrens 2212789Sahrens case ZFS_PROP_TYPE: 2213789Sahrens switch (zhp->zfs_type) { 2214789Sahrens case ZFS_TYPE_FILESYSTEM: 2215789Sahrens str = "filesystem"; 2216789Sahrens break; 2217789Sahrens case ZFS_TYPE_VOLUME: 2218789Sahrens str = "volume"; 2219789Sahrens break; 2220789Sahrens case ZFS_TYPE_SNAPSHOT: 2221789Sahrens str = "snapshot"; 2222789Sahrens break; 2223789Sahrens default: 22242082Seschrock abort(); 2225789Sahrens } 2226789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2227789Sahrens break; 2228789Sahrens 2229789Sahrens case ZFS_PROP_MOUNTED: 2230789Sahrens /* 2231789Sahrens * The 'mounted' property is a pseudo-property that described 2232789Sahrens * whether the filesystem is currently mounted. Even though 2233789Sahrens * it's a boolean value, the typical values of "on" and "off" 2234789Sahrens * don't make sense, so we translate to "yes" and "no". 2235789Sahrens */ 22362082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 22372082Seschrock src, &source, &val) != 0) 22382082Seschrock return (-1); 22392082Seschrock if (val) 2240789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2241789Sahrens else 2242789Sahrens (void) strlcpy(propbuf, "no", proplen); 2243789Sahrens break; 2244789Sahrens 2245789Sahrens case ZFS_PROP_NAME: 2246789Sahrens /* 2247789Sahrens * The 'name' property is a pseudo-property derived from the 2248789Sahrens * dataset name. It is presented as a real property to simplify 2249789Sahrens * consumers. 2250789Sahrens */ 2251789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2252789Sahrens break; 2253789Sahrens 2254789Sahrens default: 22554577Sahrens switch (zfs_prop_get_type(prop)) { 22564787Sahrens case PROP_TYPE_NUMBER: 22574577Sahrens if (get_numeric_property(zhp, prop, src, 22584577Sahrens &source, &val) != 0) 22594577Sahrens return (-1); 22604577Sahrens if (literal) 22614577Sahrens (void) snprintf(propbuf, proplen, "%llu", 22624577Sahrens (u_longlong_t)val); 22634577Sahrens else 22644577Sahrens zfs_nicenum(val, propbuf, proplen); 22654577Sahrens break; 22664577Sahrens 22674787Sahrens case PROP_TYPE_STRING: 22684577Sahrens (void) strlcpy(propbuf, 22694577Sahrens getprop_string(zhp, prop, &source), proplen); 22704577Sahrens break; 22714577Sahrens 22724787Sahrens case PROP_TYPE_INDEX: 22734861Sahrens if (get_numeric_property(zhp, prop, src, 22744861Sahrens &source, &val) != 0) 22754861Sahrens return (-1); 22764861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 22774577Sahrens return (-1); 22784577Sahrens (void) strlcpy(propbuf, strval, proplen); 22794577Sahrens break; 22804577Sahrens 22814577Sahrens default: 22824577Sahrens abort(); 22834577Sahrens } 2284789Sahrens } 2285789Sahrens 2286789Sahrens get_source(zhp, src, source, statbuf, statlen); 2287789Sahrens 2288789Sahrens return (0); 2289789Sahrens } 2290789Sahrens 2291789Sahrens /* 2292789Sahrens * Utility function to get the given numeric property. Does no validation that 2293789Sahrens * the given property is the appropriate type; should only be used with 2294789Sahrens * hard-coded property types. 2295789Sahrens */ 2296789Sahrens uint64_t 2297789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2298789Sahrens { 2299789Sahrens char *source; 23002082Seschrock uint64_t val; 23012082Seschrock 23025367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 23032082Seschrock 23042082Seschrock return (val); 2305789Sahrens } 2306789Sahrens 23075713Srm160521 int 23085713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 23095713Srm160521 { 23105713Srm160521 char buf[64]; 23115713Srm160521 23125713Srm160521 zfs_nicenum(val, buf, sizeof (buf)); 23135713Srm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 23145713Srm160521 } 23155713Srm160521 2316789Sahrens /* 2317789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2318789Sahrens */ 2319789Sahrens int 2320789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 23215094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2322789Sahrens { 2323789Sahrens char *source; 2324789Sahrens 2325789Sahrens /* 2326789Sahrens * Check to see if this property applies to our object 2327789Sahrens */ 23284849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 23293237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 23302082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 23312082Seschrock zfs_prop_to_name(prop))); 23324849Sahrens } 2333789Sahrens 2334789Sahrens if (src) 23355094Slling *src = ZPROP_SRC_NONE; 2336789Sahrens 23372082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 23382082Seschrock return (-1); 2339789Sahrens 2340789Sahrens get_source(zhp, src, source, statbuf, statlen); 2341789Sahrens 2342789Sahrens return (0); 2343789Sahrens } 2344789Sahrens 2345789Sahrens /* 2346789Sahrens * Returns the name of the given zfs handle. 2347789Sahrens */ 2348789Sahrens const char * 2349789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2350789Sahrens { 2351789Sahrens return (zhp->zfs_name); 2352789Sahrens } 2353789Sahrens 2354789Sahrens /* 2355789Sahrens * Returns the type of the given zfs handle. 2356789Sahrens */ 2357789Sahrens zfs_type_t 2358789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2359789Sahrens { 2360789Sahrens return (zhp->zfs_type); 2361789Sahrens } 2362789Sahrens 2363789Sahrens /* 23641356Seschrock * Iterate over all child filesystems 2365789Sahrens */ 2366789Sahrens int 23671356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2368789Sahrens { 2369789Sahrens zfs_cmd_t zc = { 0 }; 2370789Sahrens zfs_handle_t *nzhp; 2371789Sahrens int ret; 2372789Sahrens 23735367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 23745367Sahrens return (0); 23755367Sahrens 2376789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23772082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2378789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2379789Sahrens /* 2380789Sahrens * Ignore private dataset names. 2381789Sahrens */ 2382789Sahrens if (dataset_name_hidden(zc.zc_name)) 2383789Sahrens continue; 2384789Sahrens 2385789Sahrens /* 2386789Sahrens * Silently ignore errors, as the only plausible explanation is 2387789Sahrens * that the pool has since been removed. 2388789Sahrens */ 23892082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23902082Seschrock zc.zc_name)) == NULL) 2391789Sahrens continue; 2392789Sahrens 2393789Sahrens if ((ret = func(nzhp, data)) != 0) 2394789Sahrens return (ret); 2395789Sahrens } 2396789Sahrens 2397789Sahrens /* 2398789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2399789Sahrens * returned, then the underlying dataset has been removed since we 2400789Sahrens * obtained the handle. 2401789Sahrens */ 2402789Sahrens if (errno != ESRCH && errno != ENOENT) 24032082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 24042082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2405789Sahrens 24061356Seschrock return (0); 24071356Seschrock } 24081356Seschrock 24091356Seschrock /* 24101356Seschrock * Iterate over all snapshots 24111356Seschrock */ 24121356Seschrock int 24131356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 24141356Seschrock { 24151356Seschrock zfs_cmd_t zc = { 0 }; 24161356Seschrock zfs_handle_t *nzhp; 24171356Seschrock int ret; 2418789Sahrens 24195367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 24205367Sahrens return (0); 24215367Sahrens 2422789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 24232082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 24242082Seschrock &zc) == 0; 2425789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2426789Sahrens 24272082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 24282082Seschrock zc.zc_name)) == NULL) 2429789Sahrens continue; 2430789Sahrens 2431789Sahrens if ((ret = func(nzhp, data)) != 0) 2432789Sahrens return (ret); 2433789Sahrens } 2434789Sahrens 2435789Sahrens /* 2436789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2437789Sahrens * returned, then the underlying dataset has been removed since we 2438789Sahrens * obtained the handle. Silently ignore this case, and return success. 2439789Sahrens */ 2440789Sahrens if (errno != ESRCH && errno != ENOENT) 24412082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 24422082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2443789Sahrens 2444789Sahrens return (0); 2445789Sahrens } 2446789Sahrens 2447789Sahrens /* 24481356Seschrock * Iterate over all children, snapshots and filesystems 24491356Seschrock */ 24501356Seschrock int 24511356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 24521356Seschrock { 24531356Seschrock int ret; 24541356Seschrock 24551356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 24561356Seschrock return (ret); 24571356Seschrock 24581356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 24591356Seschrock } 24601356Seschrock 24611356Seschrock /* 2462789Sahrens * Given a complete name, return just the portion that refers to the parent. 2463789Sahrens * Can return NULL if this is a pool. 2464789Sahrens */ 2465789Sahrens static int 2466789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2467789Sahrens { 2468789Sahrens char *loc; 2469789Sahrens 2470789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2471789Sahrens return (-1); 2472789Sahrens 2473789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2474789Sahrens buf[loc - path] = '\0'; 2475789Sahrens 2476789Sahrens return (0); 2477789Sahrens } 2478789Sahrens 2479789Sahrens /* 24804490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 24814490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 24824490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 24834490Svb160487 * length of already existing prefix of the given path. We also fetch the 24844490Svb160487 * 'zoned' property, which is used to validate property settings when creating 24854490Svb160487 * new datasets. 2486789Sahrens */ 2487789Sahrens static int 24884490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 24894490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2490789Sahrens { 2491789Sahrens zfs_cmd_t zc = { 0 }; 2492789Sahrens char parent[ZFS_MAXNAMELEN]; 2493789Sahrens char *slash; 24941356Seschrock zfs_handle_t *zhp; 24952082Seschrock char errbuf[1024]; 24962082Seschrock 24972082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 24982082Seschrock path); 2499789Sahrens 2500789Sahrens /* get parent, and check to see if this is just a pool */ 2501789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 25022082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25032082Seschrock "missing dataset name")); 25042082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2505789Sahrens } 2506789Sahrens 2507789Sahrens /* check to see if the pool exists */ 2508789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2509789Sahrens slash = parent + strlen(parent); 2510789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2511789Sahrens zc.zc_name[slash - parent] = '\0'; 25122082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2513789Sahrens errno == ENOENT) { 25142082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25152082Seschrock "no such pool '%s'"), zc.zc_name); 25162082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2517789Sahrens } 2518789Sahrens 2519789Sahrens /* check to see if the parent dataset exists */ 25204490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 25214490Svb160487 if (errno == ENOENT && accept_ancestor) { 25224490Svb160487 /* 25234490Svb160487 * Go deeper to find an ancestor, give up on top level. 25244490Svb160487 */ 25254490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 25264490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25274490Svb160487 "no such pool '%s'"), zc.zc_name); 25284490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 25294490Svb160487 } 25304490Svb160487 } else if (errno == ENOENT) { 25312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25322082Seschrock "parent does not exist")); 25332082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 25344490Svb160487 } else 25352082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2536789Sahrens } 2537789Sahrens 25382676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2539789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 25402676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 25412082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 25421356Seschrock zfs_close(zhp); 2543789Sahrens return (-1); 2544789Sahrens } 2545789Sahrens 2546789Sahrens /* make sure parent is a filesystem */ 25471356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 25482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25492082Seschrock "parent is not a filesystem")); 25502082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 25511356Seschrock zfs_close(zhp); 2552789Sahrens return (-1); 2553789Sahrens } 2554789Sahrens 25551356Seschrock zfs_close(zhp); 25564490Svb160487 if (prefixlen != NULL) 25574490Svb160487 *prefixlen = strlen(parent); 25584490Svb160487 return (0); 25594490Svb160487 } 25604490Svb160487 25614490Svb160487 /* 25624490Svb160487 * Finds whether the dataset of the given type(s) exists. 25634490Svb160487 */ 25644490Svb160487 boolean_t 25654490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 25664490Svb160487 { 25674490Svb160487 zfs_handle_t *zhp; 25684490Svb160487 25695326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 25704490Svb160487 return (B_FALSE); 25714490Svb160487 25724490Svb160487 /* 25734490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 25744490Svb160487 */ 25754490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 25764490Svb160487 int ds_type = zhp->zfs_type; 25774490Svb160487 25784490Svb160487 zfs_close(zhp); 25794490Svb160487 if (types & ds_type) 25804490Svb160487 return (B_TRUE); 25814490Svb160487 } 25824490Svb160487 return (B_FALSE); 25834490Svb160487 } 25844490Svb160487 25854490Svb160487 /* 25865367Sahrens * Given a path to 'target', create all the ancestors between 25875367Sahrens * the prefixlen portion of the path, and the target itself. 25885367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 25895367Sahrens */ 25905367Sahrens int 25915367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 25925367Sahrens { 25935367Sahrens zfs_handle_t *h; 25945367Sahrens char *cp; 25955367Sahrens const char *opname; 25965367Sahrens 25975367Sahrens /* make sure prefix exists */ 25985367Sahrens cp = target + prefixlen; 25995367Sahrens if (*cp != '/') { 26005367Sahrens assert(strchr(cp, '/') == NULL); 26015367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 26025367Sahrens } else { 26035367Sahrens *cp = '\0'; 26045367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 26055367Sahrens *cp = '/'; 26065367Sahrens } 26075367Sahrens if (h == NULL) 26085367Sahrens return (-1); 26095367Sahrens zfs_close(h); 26105367Sahrens 26115367Sahrens /* 26125367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 26135367Sahrens * up to the prefixlen-long one. 26145367Sahrens */ 26155367Sahrens for (cp = target + prefixlen + 1; 26165367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 26175367Sahrens char *logstr; 26185367Sahrens 26195367Sahrens *cp = '\0'; 26205367Sahrens 26215367Sahrens h = make_dataset_handle(hdl, target); 26225367Sahrens if (h) { 26235367Sahrens /* it already exists, nothing to do here */ 26245367Sahrens zfs_close(h); 26255367Sahrens continue; 26265367Sahrens } 26275367Sahrens 26285367Sahrens logstr = hdl->libzfs_log_str; 26295367Sahrens hdl->libzfs_log_str = NULL; 26305367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 26315367Sahrens NULL) != 0) { 26325367Sahrens hdl->libzfs_log_str = logstr; 26335367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 26345367Sahrens goto ancestorerr; 26355367Sahrens } 26365367Sahrens 26375367Sahrens hdl->libzfs_log_str = logstr; 26385367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 26395367Sahrens if (h == NULL) { 26405367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 26415367Sahrens goto ancestorerr; 26425367Sahrens } 26435367Sahrens 26445367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 26455367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 26465367Sahrens goto ancestorerr; 26475367Sahrens } 26485367Sahrens 26495367Sahrens if (zfs_share(h) != 0) { 26505367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 26515367Sahrens goto ancestorerr; 26525367Sahrens } 26535367Sahrens 26545367Sahrens zfs_close(h); 26555367Sahrens } 26565367Sahrens 26575367Sahrens return (0); 26585367Sahrens 26595367Sahrens ancestorerr: 26605367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26615367Sahrens "failed to %s ancestor '%s'"), opname, target); 26625367Sahrens return (-1); 26635367Sahrens } 26645367Sahrens 26655367Sahrens /* 26664490Svb160487 * Creates non-existing ancestors of the given path. 26674490Svb160487 */ 26684490Svb160487 int 26694490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 26704490Svb160487 { 26714490Svb160487 int prefix; 26724490Svb160487 uint64_t zoned; 26734490Svb160487 char *path_copy; 26744490Svb160487 int rc; 26754490Svb160487 26764490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 26774490Svb160487 return (-1); 26784490Svb160487 26794490Svb160487 if ((path_copy = strdup(path)) != NULL) { 26804490Svb160487 rc = create_parents(hdl, path_copy, prefix); 26814490Svb160487 free(path_copy); 26824490Svb160487 } 26834490Svb160487 if (path_copy == NULL || rc != 0) 26844490Svb160487 return (-1); 26854490Svb160487 2686789Sahrens return (0); 2687789Sahrens } 2688789Sahrens 2689789Sahrens /* 26902676Seschrock * Create a new filesystem or volume. 2691789Sahrens */ 2692789Sahrens int 26932082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 26942676Seschrock nvlist_t *props) 2695789Sahrens { 2696789Sahrens zfs_cmd_t zc = { 0 }; 2697789Sahrens int ret; 2698789Sahrens uint64_t size = 0; 2699789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 27002082Seschrock char errbuf[1024]; 27012676Seschrock uint64_t zoned; 27022082Seschrock 27032082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 27042082Seschrock "cannot create '%s'"), path); 2705789Sahrens 2706789Sahrens /* validate the path, taking care to note the extended error message */ 27075326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 27082082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2709789Sahrens 2710789Sahrens /* validate parents exist */ 27114490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2712789Sahrens return (-1); 2713789Sahrens 2714789Sahrens /* 2715789Sahrens * The failure modes when creating a dataset of a different type over 2716789Sahrens * one that already exists is a little strange. In particular, if you 2717789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2718789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2719789Sahrens * first try to see if the dataset exists. 2720789Sahrens */ 2721789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 27225094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 27232082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27242082Seschrock "dataset already exists")); 27252082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2726789Sahrens } 2727789Sahrens 2728789Sahrens if (type == ZFS_TYPE_VOLUME) 2729789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2730789Sahrens else 2731789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2732789Sahrens 27335094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 27343912Slling zoned, NULL, errbuf)) == 0) 27352676Seschrock return (-1); 27362676Seschrock 2737789Sahrens if (type == ZFS_TYPE_VOLUME) { 27381133Seschrock /* 27391133Seschrock * If we are creating a volume, the size and block size must 27401133Seschrock * satisfy a few restraints. First, the blocksize must be a 27411133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 27421133Seschrock * volsize must be a multiple of the block size, and cannot be 27431133Seschrock * zero. 27441133Seschrock */ 27452676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 27462676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 27472676Seschrock nvlist_free(props); 27482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27492676Seschrock "missing volume size")); 27502676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2751789Sahrens } 2752789Sahrens 27532676Seschrock if ((ret = nvlist_lookup_uint64(props, 27542676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 27552676Seschrock &blocksize)) != 0) { 27562676Seschrock if (ret == ENOENT) { 27572676Seschrock blocksize = zfs_prop_default_numeric( 27582676Seschrock ZFS_PROP_VOLBLOCKSIZE); 27592676Seschrock } else { 27602676Seschrock nvlist_free(props); 27612676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27622676Seschrock "missing volume block size")); 27632676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27642676Seschrock } 27652676Seschrock } 27662676Seschrock 27672676Seschrock if (size == 0) { 27682676Seschrock nvlist_free(props); 27692082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27702676Seschrock "volume size cannot be zero")); 27712676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27721133Seschrock } 27731133Seschrock 27741133Seschrock if (size % blocksize != 0) { 27752676Seschrock nvlist_free(props); 27762082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27772676Seschrock "volume size must be a multiple of volume block " 27782676Seschrock "size")); 27792676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27801133Seschrock } 2781789Sahrens } 2782789Sahrens 27835094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 27842676Seschrock return (-1); 27852676Seschrock nvlist_free(props); 27862676Seschrock 2787789Sahrens /* create the dataset */ 27884543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2789789Sahrens 27903912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 27912082Seschrock ret = zvol_create_link(hdl, path); 27923912Slling if (ret) { 27933912Slling (void) zfs_standard_error(hdl, errno, 27943912Slling dgettext(TEXT_DOMAIN, 27953912Slling "Volume successfully created, but device links " 27963912Slling "were not created")); 27973912Slling zcmd_free_nvlists(&zc); 27983912Slling return (-1); 27993912Slling } 28003912Slling } 2801789Sahrens 28022676Seschrock zcmd_free_nvlists(&zc); 28032676Seschrock 2804789Sahrens /* check for failure */ 2805789Sahrens if (ret != 0) { 2806789Sahrens char parent[ZFS_MAXNAMELEN]; 2807789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2808789Sahrens 2809789Sahrens switch (errno) { 2810789Sahrens case ENOENT: 28112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28122082Seschrock "no such parent '%s'"), parent); 28132082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2814789Sahrens 2815789Sahrens case EINVAL: 28162082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28173413Smmusante "parent '%s' is not a filesystem"), parent); 28182082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2819789Sahrens 2820789Sahrens case EDOM: 28212082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28222676Seschrock "volume block size must be power of 2 from " 28232676Seschrock "%u to %uk"), 2824789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2825789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 28262082Seschrock 28272676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28282082Seschrock 28294603Sahrens case ENOTSUP: 28304603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28314603Sahrens "pool must be upgraded to set this " 28324603Sahrens "property or value")); 28334603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 28344603Sahrens 2835789Sahrens #ifdef _ILP32 2836789Sahrens case EOVERFLOW: 2837789Sahrens /* 2838789Sahrens * This platform can't address a volume this big. 2839789Sahrens */ 28402082Seschrock if (type == ZFS_TYPE_VOLUME) 28412082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 28422082Seschrock errbuf)); 2843789Sahrens #endif 28442082Seschrock /* FALLTHROUGH */ 2845789Sahrens default: 28462082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2847789Sahrens } 2848789Sahrens } 2849789Sahrens 2850789Sahrens return (0); 2851789Sahrens } 2852789Sahrens 2853789Sahrens /* 2854789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2855789Sahrens * isn't mounted, and that there are no active dependents. 2856789Sahrens */ 2857789Sahrens int 2858789Sahrens zfs_destroy(zfs_handle_t *zhp) 2859789Sahrens { 2860789Sahrens zfs_cmd_t zc = { 0 }; 2861789Sahrens 2862789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2863789Sahrens 28642676Seschrock if (ZFS_IS_VOLUME(zhp)) { 28653126Sahl /* 28664543Smarks * If user doesn't have permissions to unshare volume, then 28674543Smarks * abort the request. This would only happen for a 28684543Smarks * non-privileged user. 28693126Sahl */ 28704543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 28714543Smarks return (-1); 28724543Smarks } 28733126Sahl 28742082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2875789Sahrens return (-1); 2876789Sahrens 2877789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2878789Sahrens } else { 2879789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2880789Sahrens } 2881789Sahrens 28824543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 28833237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 28842082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 28852082Seschrock zhp->zfs_name)); 28862199Sahrens } 2887789Sahrens 2888789Sahrens remove_mountpoint(zhp); 2889789Sahrens 2890789Sahrens return (0); 2891789Sahrens } 2892789Sahrens 28932199Sahrens struct destroydata { 28942199Sahrens char *snapname; 28952199Sahrens boolean_t gotone; 28963265Sahrens boolean_t closezhp; 28972199Sahrens }; 28982199Sahrens 28992199Sahrens static int 29002199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 29012199Sahrens { 29022199Sahrens struct destroydata *dd = arg; 29032199Sahrens zfs_handle_t *szhp; 29042199Sahrens char name[ZFS_MAXNAMELEN]; 29053265Sahrens boolean_t closezhp = dd->closezhp; 29063265Sahrens int rv; 29072199Sahrens 29082676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 29092676Seschrock (void) strlcat(name, "@", sizeof (name)); 29102676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 29112199Sahrens 29122199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 29132199Sahrens if (szhp) { 29142199Sahrens dd->gotone = B_TRUE; 29152199Sahrens zfs_close(szhp); 29162199Sahrens } 29172199Sahrens 29182199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 29192199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 29202199Sahrens /* 29212199Sahrens * NB: this is simply a best-effort. We don't want to 29222199Sahrens * return an error, because then we wouldn't visit all 29232199Sahrens * the volumes. 29242199Sahrens */ 29252199Sahrens } 29262199Sahrens 29273265Sahrens dd->closezhp = B_TRUE; 29283265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 29293265Sahrens if (closezhp) 29303265Sahrens zfs_close(zhp); 29313265Sahrens return (rv); 29322199Sahrens } 29332199Sahrens 29342199Sahrens /* 29352199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 29362199Sahrens */ 29372199Sahrens int 29382199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 29392199Sahrens { 29402199Sahrens zfs_cmd_t zc = { 0 }; 29412199Sahrens int ret; 29422199Sahrens struct destroydata dd = { 0 }; 29432199Sahrens 29442199Sahrens dd.snapname = snapname; 29452199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 29462199Sahrens 29472199Sahrens if (!dd.gotone) { 29483237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 29492199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 29502199Sahrens zhp->zfs_name, snapname)); 29512199Sahrens } 29522199Sahrens 29532199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 29542676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 29552199Sahrens 29564543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 29572199Sahrens if (ret != 0) { 29582199Sahrens char errbuf[1024]; 29592199Sahrens 29602199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29612199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 29622199Sahrens 29632199Sahrens switch (errno) { 29642199Sahrens case EEXIST: 29652199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29662199Sahrens "snapshot is cloned")); 29672199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 29682199Sahrens 29692199Sahrens default: 29702199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 29712199Sahrens errbuf)); 29722199Sahrens } 29732199Sahrens } 29742199Sahrens 29752199Sahrens return (0); 29762199Sahrens } 29772199Sahrens 2978789Sahrens /* 2979789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2980789Sahrens */ 2981789Sahrens int 29822676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2983789Sahrens { 2984789Sahrens zfs_cmd_t zc = { 0 }; 2985789Sahrens char parent[ZFS_MAXNAMELEN]; 2986789Sahrens int ret; 29872082Seschrock char errbuf[1024]; 29882082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 29892676Seschrock zfs_type_t type; 29902676Seschrock uint64_t zoned; 2991789Sahrens 2992789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2993789Sahrens 29942082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29952082Seschrock "cannot create '%s'"), target); 29962082Seschrock 2997789Sahrens /* validate the target name */ 29985326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 29992082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3000789Sahrens 3001789Sahrens /* validate parents exist */ 30024490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3003789Sahrens return (-1); 3004789Sahrens 3005789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3006789Sahrens 3007789Sahrens /* do the clone */ 30082676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3009789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 30102744Snn35248 type = ZFS_TYPE_VOLUME; 30112676Seschrock } else { 3012789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 30132744Snn35248 type = ZFS_TYPE_FILESYSTEM; 30142676Seschrock } 30152676Seschrock 30162676Seschrock if (props) { 30175094Slling if ((props = zfs_validate_properties(hdl, type, props, 30183912Slling zoned, zhp, errbuf)) == NULL) 30192676Seschrock return (-1); 30202676Seschrock 30215094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 30222676Seschrock nvlist_free(props); 30232676Seschrock return (-1); 30242676Seschrock } 30252676Seschrock 30262676Seschrock nvlist_free(props); 30272676Seschrock } 3028789Sahrens 3029789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 30302676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 30314543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3032789Sahrens 30332676Seschrock zcmd_free_nvlists(&zc); 30342676Seschrock 3035789Sahrens if (ret != 0) { 3036789Sahrens switch (errno) { 3037789Sahrens 3038789Sahrens case ENOENT: 3039789Sahrens /* 3040789Sahrens * The parent doesn't exist. We should have caught this 3041789Sahrens * above, but there may a race condition that has since 3042789Sahrens * destroyed the parent. 3043789Sahrens * 3044789Sahrens * At this point, we don't know whether it's the source 3045789Sahrens * that doesn't exist anymore, or whether the target 3046789Sahrens * dataset doesn't exist. 3047789Sahrens */ 30482082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30492082Seschrock "no such parent '%s'"), parent); 30502082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 30512082Seschrock 30522082Seschrock case EXDEV: 30532082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30542082Seschrock "source and target pools differ")); 30552082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 30562082Seschrock errbuf)); 30572082Seschrock 30582082Seschrock default: 30592082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 30602082Seschrock errbuf)); 30612082Seschrock } 30622676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 30632082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 30642082Seschrock } 30652082Seschrock 30662082Seschrock return (ret); 30672082Seschrock } 30682082Seschrock 30692082Seschrock typedef struct promote_data { 30702082Seschrock char cb_mountpoint[MAXPATHLEN]; 30712082Seschrock const char *cb_target; 30722082Seschrock const char *cb_errbuf; 30732082Seschrock uint64_t cb_pivot_txg; 30742082Seschrock } promote_data_t; 30752082Seschrock 30762082Seschrock static int 30772082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 30782082Seschrock { 30792082Seschrock promote_data_t *pd = data; 30802082Seschrock zfs_handle_t *szhp; 30812082Seschrock char snapname[MAXPATHLEN]; 30823265Sahrens int rv = 0; 30832082Seschrock 30842082Seschrock /* We don't care about snapshots after the pivot point */ 30853265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 30863265Sahrens zfs_close(zhp); 30872082Seschrock return (0); 30883265Sahrens } 30892082Seschrock 30902417Sahrens /* Remove the device link if it's a zvol. */ 30912676Seschrock if (ZFS_IS_VOLUME(zhp)) 30922417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 30932082Seschrock 30942082Seschrock /* Check for conflicting names */ 30952676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 30962676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 30972082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 30982082Seschrock if (szhp != NULL) { 30992082Seschrock zfs_close(szhp); 31002082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31012082Seschrock "snapshot name '%s' from origin \n" 31022082Seschrock "conflicts with '%s' from target"), 31032082Seschrock zhp->zfs_name, snapname); 31043265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 31052082Seschrock } 31063265Sahrens zfs_close(zhp); 31073265Sahrens return (rv); 31082082Seschrock } 31092082Seschrock 31102417Sahrens static int 31112417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 31122417Sahrens { 31132417Sahrens promote_data_t *pd = data; 31142417Sahrens 31152417Sahrens /* We don't care about snapshots after the pivot point */ 31163265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 31173265Sahrens /* Create the device link if it's a zvol. */ 31183265Sahrens if (ZFS_IS_VOLUME(zhp)) 31193265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 31203265Sahrens } 31213265Sahrens 31223265Sahrens zfs_close(zhp); 31232417Sahrens return (0); 31242417Sahrens } 31252417Sahrens 31262082Seschrock /* 31272082Seschrock * Promotes the given clone fs to be the clone parent. 31282082Seschrock */ 31292082Seschrock int 31302082Seschrock zfs_promote(zfs_handle_t *zhp) 31312082Seschrock { 31322082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 31332082Seschrock zfs_cmd_t zc = { 0 }; 31342082Seschrock char parent[MAXPATHLEN]; 31352082Seschrock char *cp; 31362082Seschrock int ret; 31372082Seschrock zfs_handle_t *pzhp; 31382082Seschrock promote_data_t pd; 31392082Seschrock char errbuf[1024]; 31402082Seschrock 31412082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31422082Seschrock "cannot promote '%s'"), zhp->zfs_name); 31432082Seschrock 31442082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 31452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31462082Seschrock "snapshots can not be promoted")); 31472082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 31482082Seschrock } 31492082Seschrock 31505367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 31512082Seschrock if (parent[0] == '\0') { 31522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31532082Seschrock "not a cloned filesystem")); 31542082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 31552082Seschrock } 31562082Seschrock cp = strchr(parent, '@'); 31572082Seschrock *cp = '\0'; 31582082Seschrock 31592082Seschrock /* Walk the snapshots we will be moving */ 31605367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 31612082Seschrock if (pzhp == NULL) 31622082Seschrock return (-1); 31632082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 31642082Seschrock zfs_close(pzhp); 31652082Seschrock pd.cb_target = zhp->zfs_name; 31662082Seschrock pd.cb_errbuf = errbuf; 31675094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 31682082Seschrock if (pzhp == NULL) 31692082Seschrock return (-1); 31702082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 31712082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 31722082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 31732417Sahrens if (ret != 0) { 31742417Sahrens zfs_close(pzhp); 31752082Seschrock return (-1); 31762417Sahrens } 31772082Seschrock 31782082Seschrock /* issue the ioctl */ 31795367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 31802676Seschrock sizeof (zc.zc_value)); 31812082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31824543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 31832082Seschrock 31842082Seschrock if (ret != 0) { 31852417Sahrens int save_errno = errno; 31862417Sahrens 31872417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 31882417Sahrens zfs_close(pzhp); 31892417Sahrens 31902417Sahrens switch (save_errno) { 3191789Sahrens case EEXIST: 3192789Sahrens /* 31932082Seschrock * There is a conflicting snapshot name. We 31942082Seschrock * should have caught this above, but they could 31952082Seschrock * have renamed something in the mean time. 3196789Sahrens */ 31972082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31982082Seschrock "conflicting snapshot name from parent '%s'"), 31992082Seschrock parent); 32002082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3201789Sahrens 3202789Sahrens default: 32032417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3204789Sahrens } 32052417Sahrens } else { 32062417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3207789Sahrens } 3208789Sahrens 32092417Sahrens zfs_close(pzhp); 3210789Sahrens return (ret); 3211789Sahrens } 3212789Sahrens 32134007Smmusante struct createdata { 32144007Smmusante const char *cd_snapname; 32154007Smmusante int cd_ifexists; 32164007Smmusante }; 32174007Smmusante 32182199Sahrens static int 32192199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 32202199Sahrens { 32214007Smmusante struct createdata *cd = arg; 32222676Seschrock int ret; 32232199Sahrens 32242199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 32252199Sahrens char name[MAXPATHLEN]; 32262199Sahrens 32272676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 32282676Seschrock (void) strlcat(name, "@", sizeof (name)); 32294007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 32304007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 32314007Smmusante cd->cd_ifexists); 32322199Sahrens /* 32332199Sahrens * NB: this is simply a best-effort. We don't want to 32342199Sahrens * return an error, because then we wouldn't visit all 32352199Sahrens * the volumes. 32362199Sahrens */ 32372199Sahrens } 32382676Seschrock 32394007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 32402676Seschrock 32412676Seschrock zfs_close(zhp); 32422676Seschrock 32432676Seschrock return (ret); 32442199Sahrens } 32452199Sahrens 3246789Sahrens /* 32473504Sahl * Takes a snapshot of the given dataset. 3248789Sahrens */ 3249789Sahrens int 32502199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3251789Sahrens { 3252789Sahrens const char *delim; 3253789Sahrens char *parent; 3254789Sahrens zfs_handle_t *zhp; 3255789Sahrens zfs_cmd_t zc = { 0 }; 3256789Sahrens int ret; 32572082Seschrock char errbuf[1024]; 32582082Seschrock 32592082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32602082Seschrock "cannot snapshot '%s'"), path); 32612082Seschrock 32622082Seschrock /* validate the target name */ 32635326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 32642082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3265789Sahrens 3266789Sahrens /* make sure the parent exists and is of the appropriate type */ 32672199Sahrens delim = strchr(path, '@'); 32682082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 32692082Seschrock return (-1); 3270789Sahrens (void) strncpy(parent, path, delim - path); 3271789Sahrens parent[delim - path] = '\0'; 3272789Sahrens 32732082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3274789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3275789Sahrens free(parent); 3276789Sahrens return (-1); 3277789Sahrens } 3278789Sahrens 32792199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 32802676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 32814543Smarks if (ZFS_IS_VOLUME(zhp)) 32824543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 32834543Smarks else 32844543Smarks zc.zc_objset_type = DMU_OST_ZFS; 32852199Sahrens zc.zc_cookie = recursive; 32864543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 32872199Sahrens 32882199Sahrens /* 32892199Sahrens * if it was recursive, the one that actually failed will be in 32902199Sahrens * zc.zc_name. 32912199Sahrens */ 32924543Smarks if (ret != 0) 32934543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32944543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 32954543Smarks 32962199Sahrens if (ret == 0 && recursive) { 32974007Smmusante struct createdata cd; 32984007Smmusante 32994007Smmusante cd.cd_snapname = delim + 1; 33004007Smmusante cd.cd_ifexists = B_FALSE; 33014007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 33022199Sahrens } 3303789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 33042082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 33052199Sahrens if (ret != 0) { 33064543Smarks (void) zfs_standard_error(hdl, errno, 33074543Smarks dgettext(TEXT_DOMAIN, 33084543Smarks "Volume successfully snapshotted, but device links " 33094543Smarks "were not created")); 33104543Smarks free(parent); 33114543Smarks zfs_close(zhp); 33124543Smarks return (-1); 33132199Sahrens } 3314789Sahrens } 3315789Sahrens 33162082Seschrock if (ret != 0) 33172082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3318789Sahrens 3319789Sahrens free(parent); 3320789Sahrens zfs_close(zhp); 3321789Sahrens 3322789Sahrens return (ret); 3323789Sahrens } 3324789Sahrens 3325789Sahrens /* 33261294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 33271294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 33281294Slling * is a dependent and we should just destroy it without checking the transaction 33291294Slling * group. 3330789Sahrens */ 33311294Slling typedef struct rollback_data { 33321294Slling const char *cb_target; /* the snapshot */ 33331294Slling uint64_t cb_create; /* creation time reference */ 33341294Slling int cb_error; 33352082Seschrock boolean_t cb_dependent; 33361294Slling } rollback_data_t; 33371294Slling 33381294Slling static int 33391294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 33401294Slling { 33411294Slling rollback_data_t *cbp = data; 33421294Slling 33431294Slling if (!cbp->cb_dependent) { 33441294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 33451294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 33461294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 33471294Slling cbp->cb_create) { 33484543Smarks char *logstr; 33491294Slling 33502082Seschrock cbp->cb_dependent = B_TRUE; 33515446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 33525446Sahrens rollback_destroy, cbp); 33532082Seschrock cbp->cb_dependent = B_FALSE; 33541294Slling 33554543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 33564543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 33575446Sahrens cbp->cb_error |= zfs_destroy(zhp); 33584543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 33591294Slling } 33601294Slling } else { 33615446Sahrens cbp->cb_error |= zfs_destroy(zhp); 33621294Slling } 33631294Slling 33641294Slling zfs_close(zhp); 33651294Slling return (0); 33661294Slling } 33671294Slling 33681294Slling /* 33695446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 33705446Sahrens * data changes since then and making it the active dataset. 33715446Sahrens * 33725446Sahrens * Any snapshots more recent than the target are destroyed, along with 33735446Sahrens * their dependents. 33741294Slling */ 33755446Sahrens int 33765446Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap) 3377789Sahrens { 33785446Sahrens rollback_data_t cb = { 0 }; 33795446Sahrens int err; 3380789Sahrens zfs_cmd_t zc = { 0 }; 33815713Srm160521 boolean_t restore_resv = 0; 33825713Srm160521 uint64_t old_volsize, new_volsize; 33835713Srm160521 zfs_prop_t resv_prop; 3384789Sahrens 3385789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3386789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3387789Sahrens 33885446Sahrens /* 33895446Sahrens * Destroy all recent snapshots and its dependends. 33905446Sahrens */ 33915446Sahrens cb.cb_target = snap->zfs_name; 33925446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 33935446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 33945446Sahrens 33955446Sahrens if (cb.cb_error != 0) 33965446Sahrens return (cb.cb_error); 33975446Sahrens 33985446Sahrens /* 33995446Sahrens * Now that we have verified that the snapshot is the latest, 34005446Sahrens * rollback to the given snapshot. 34015446Sahrens */ 34025446Sahrens 34035713Srm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 34045713Srm160521 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 34055713Srm160521 return (-1); 34065713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 34075713Srm160521 return (-1); 34085713Srm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 34095713Srm160521 restore_resv = 34105713Srm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 34115713Srm160521 } 3412789Sahrens 3413789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3414789Sahrens 34152676Seschrock if (ZFS_IS_VOLUME(zhp)) 3416789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3417789Sahrens else 3418789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3419789Sahrens 3420789Sahrens /* 34215446Sahrens * We rely on zfs_iter_children() to verify that there are no 34225446Sahrens * newer snapshots for the given dataset. Therefore, we can 34235446Sahrens * simply pass the name on to the ioctl() call. There is still 34245446Sahrens * an unlikely race condition where the user has taken a 34255446Sahrens * snapshot since we verified that this was the most recent. 34265713Srm160521 * 3427789Sahrens */ 34285446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 34293237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 34302082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 34312082Seschrock zhp->zfs_name); 3432*5717Srm160521 return (err); 3433*5717Srm160521 } 34345713Srm160521 34355713Srm160521 /* 34365713Srm160521 * For volumes, if the pre-rollback volsize matched the pre- 34375713Srm160521 * rollback reservation and the volsize has changed then set 34385713Srm160521 * the reservation property to the post-rollback volsize. 34395713Srm160521 * Make a new handle since the rollback closed the dataset. 34405713Srm160521 */ 3441*5717Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 3442*5717Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 3443*5717Srm160521 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) { 3444*5717Srm160521 zfs_close(zhp); 34455713Srm160521 return (err); 3446*5717Srm160521 } 34475713Srm160521 if (restore_resv) { 34485713Srm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 34495713Srm160521 if (old_volsize != new_volsize) 3450*5717Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 3451*5717Srm160521 new_volsize); 34525713Srm160521 } 34535713Srm160521 zfs_close(zhp); 3454789Sahrens } 34555446Sahrens return (err); 34561294Slling } 34571294Slling 34581294Slling /* 3459789Sahrens * Iterate over all dependents for a given dataset. This includes both 3460789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3461789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3462789Sahrens * libzfs_graph.c. 3463789Sahrens */ 3464789Sahrens int 34652474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 34662474Seschrock zfs_iter_f func, void *data) 3467789Sahrens { 3468789Sahrens char **dependents; 3469789Sahrens size_t count; 3470789Sahrens int i; 3471789Sahrens zfs_handle_t *child; 3472789Sahrens int ret = 0; 3473789Sahrens 34742474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 34752474Seschrock &dependents, &count) != 0) 34762474Seschrock return (-1); 34772474Seschrock 3478789Sahrens for (i = 0; i < count; i++) { 34792082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 34802082Seschrock dependents[i])) == NULL) 3481789Sahrens continue; 3482789Sahrens 3483789Sahrens if ((ret = func(child, data)) != 0) 3484789Sahrens break; 3485789Sahrens } 3486789Sahrens 3487789Sahrens for (i = 0; i < count; i++) 3488789Sahrens free(dependents[i]); 3489789Sahrens free(dependents); 3490789Sahrens 3491789Sahrens return (ret); 3492789Sahrens } 3493789Sahrens 3494789Sahrens /* 3495789Sahrens * Renames the given dataset. 3496789Sahrens */ 3497789Sahrens int 34984490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3499789Sahrens { 3500789Sahrens int ret; 3501789Sahrens zfs_cmd_t zc = { 0 }; 3502789Sahrens char *delim; 35034007Smmusante prop_changelist_t *cl = NULL; 35044007Smmusante zfs_handle_t *zhrp = NULL; 35054007Smmusante char *parentname = NULL; 3506789Sahrens char parent[ZFS_MAXNAMELEN]; 35072082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 35082082Seschrock char errbuf[1024]; 3509789Sahrens 3510789Sahrens /* if we have the same exact name, just return success */ 3511789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3512789Sahrens return (0); 3513789Sahrens 35142082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35152082Seschrock "cannot rename to '%s'"), target); 35162082Seschrock 3517789Sahrens /* 3518789Sahrens * Make sure the target name is valid 3519789Sahrens */ 3520789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 35212665Snd150628 if ((strchr(target, '@') == NULL) || 35222665Snd150628 *target == '@') { 35232665Snd150628 /* 35242665Snd150628 * Snapshot target name is abbreviated, 35252665Snd150628 * reconstruct full dataset name 35262665Snd150628 */ 35272665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 35282665Snd150628 sizeof (parent)); 35292665Snd150628 delim = strchr(parent, '@'); 35302665Snd150628 if (strchr(target, '@') == NULL) 35312665Snd150628 *(++delim) = '\0'; 35322665Snd150628 else 35332665Snd150628 *delim = '\0'; 35342665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 35352665Snd150628 target = parent; 35362665Snd150628 } else { 35372665Snd150628 /* 35382665Snd150628 * Make sure we're renaming within the same dataset. 35392665Snd150628 */ 35402665Snd150628 delim = strchr(target, '@'); 35412665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 35422665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 35432665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35442665Snd150628 "snapshots must be part of same " 35452665Snd150628 "dataset")); 35462665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 35473912Slling errbuf)); 35482665Snd150628 } 3549789Sahrens } 35505326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 35512665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3552789Sahrens } else { 35534007Smmusante if (recursive) { 35544007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35554007Smmusante "recursive rename must be a snapshot")); 35564007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 35574007Smmusante } 35584007Smmusante 35595326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 35602665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 35612676Seschrock uint64_t unused; 35622676Seschrock 3563789Sahrens /* validate parents */ 35644490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3565789Sahrens return (-1); 3566789Sahrens 3567789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3568789Sahrens 3569789Sahrens /* make sure we're in the same pool */ 3570789Sahrens verify((delim = strchr(target, '/')) != NULL); 3571789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3572789Sahrens zhp->zfs_name[delim - target] != '/') { 35732082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35742082Seschrock "datasets must be within same pool")); 35752082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3576789Sahrens } 35772440Snd150628 35782440Snd150628 /* new name cannot be a child of the current dataset name */ 35792440Snd150628 if (strncmp(parent, zhp->zfs_name, 35803912Slling strlen(zhp->zfs_name)) == 0) { 35812440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35822440Snd150628 "New dataset name cannot be a descendent of " 35832440Snd150628 "current dataset name")); 35842440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 35852440Snd150628 } 3586789Sahrens } 3587789Sahrens 35882082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 35892082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 35902082Seschrock 3591789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3592789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 35932082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35942082Seschrock "dataset is used in a non-global zone")); 35952082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3596789Sahrens } 3597789Sahrens 35984007Smmusante if (recursive) { 35994007Smmusante struct destroydata dd; 36004007Smmusante 36014183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 36024183Smmusante if (parentname == NULL) { 36034183Smmusante ret = -1; 36044183Smmusante goto error; 36054183Smmusante } 36064007Smmusante delim = strchr(parentname, '@'); 36074007Smmusante *delim = '\0'; 36085094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 36094007Smmusante if (zhrp == NULL) { 36104183Smmusante ret = -1; 36114183Smmusante goto error; 36124007Smmusante } 36134007Smmusante 36144007Smmusante dd.snapname = delim + 1; 36154007Smmusante dd.gotone = B_FALSE; 36164183Smmusante dd.closezhp = B_TRUE; 36174007Smmusante 36184007Smmusante /* We remove any zvol links prior to renaming them */ 36194007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 36204007Smmusante if (ret) { 36214007Smmusante goto error; 36224007Smmusante } 36234007Smmusante } else { 36244007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 36254007Smmusante return (-1); 36264007Smmusante 36274007Smmusante if (changelist_haszonedchild(cl)) { 36284007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36294007Smmusante "child dataset with inherited mountpoint is used " 36304007Smmusante "in a non-global zone")); 36314007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 36324007Smmusante goto error; 36334007Smmusante } 36344007Smmusante 36354007Smmusante if ((ret = changelist_prefix(cl)) != 0) 36364007Smmusante goto error; 3637789Sahrens } 3638789Sahrens 36392676Seschrock if (ZFS_IS_VOLUME(zhp)) 3640789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3641789Sahrens else 3642789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3643789Sahrens 36442665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 36452676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 36462665Snd150628 36474007Smmusante zc.zc_cookie = recursive; 36484007Smmusante 36494543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 36504007Smmusante /* 36514007Smmusante * if it was recursive, the one that actually failed will 36524007Smmusante * be in zc.zc_name 36534007Smmusante */ 36544007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36555367Sahrens "cannot rename '%s'"), zc.zc_name); 36564007Smmusante 36574007Smmusante if (recursive && errno == EEXIST) { 36584007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36594007Smmusante "a child dataset already has a snapshot " 36604007Smmusante "with the new name")); 36614801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 36624007Smmusante } else { 36634007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 36644007Smmusante } 3665789Sahrens 3666789Sahrens /* 3667789Sahrens * On failure, we still want to remount any filesystems that 3668789Sahrens * were previously mounted, so we don't alter the system state. 3669789Sahrens */ 36704007Smmusante if (recursive) { 36714007Smmusante struct createdata cd; 36724007Smmusante 36734007Smmusante /* only create links for datasets that had existed */ 36744007Smmusante cd.cd_snapname = delim + 1; 36754007Smmusante cd.cd_ifexists = B_TRUE; 36764007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36774007Smmusante &cd); 36784007Smmusante } else { 36794007Smmusante (void) changelist_postfix(cl); 36804007Smmusante } 3681789Sahrens } else { 36824007Smmusante if (recursive) { 36834007Smmusante struct createdata cd; 36844007Smmusante 36854007Smmusante /* only create links for datasets that had existed */ 36864007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 36874007Smmusante cd.cd_ifexists = B_TRUE; 36884007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36894007Smmusante &cd); 36904007Smmusante } else { 36914007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 36924007Smmusante ret = changelist_postfix(cl); 36934007Smmusante } 3694789Sahrens } 3695789Sahrens 3696789Sahrens error: 36974007Smmusante if (parentname) { 36984007Smmusante free(parentname); 36994007Smmusante } 37004007Smmusante if (zhrp) { 37014007Smmusante zfs_close(zhrp); 37024007Smmusante } 37034007Smmusante if (cl) { 37044007Smmusante changelist_free(cl); 37054007Smmusante } 3706789Sahrens return (ret); 3707789Sahrens } 3708789Sahrens 3709789Sahrens /* 3710789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3711789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3712789Sahrens */ 3713789Sahrens int 37142082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3715789Sahrens { 37164007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 37174007Smmusante } 37184007Smmusante 37194007Smmusante static int 37204007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 37214007Smmusante { 3722789Sahrens zfs_cmd_t zc = { 0 }; 37232082Seschrock di_devlink_handle_t dhdl; 37244543Smarks priv_set_t *priv_effective; 37254543Smarks int privileged; 3726789Sahrens 3727789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3728789Sahrens 3729789Sahrens /* 3730789Sahrens * Issue the appropriate ioctl. 3731789Sahrens */ 37322082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3733789Sahrens switch (errno) { 3734789Sahrens case EEXIST: 3735789Sahrens /* 3736789Sahrens * Silently ignore the case where the link already 3737789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3738789Sahrens * times without errors. 3739789Sahrens */ 3740789Sahrens return (0); 3741789Sahrens 37424007Smmusante case ENOENT: 37434007Smmusante /* 37444007Smmusante * Dataset does not exist in the kernel. If we 37454007Smmusante * don't care (see zfs_rename), then ignore the 37464007Smmusante * error quietly. 37474007Smmusante */ 37484007Smmusante if (ifexists) { 37494007Smmusante return (0); 37504007Smmusante } 37514007Smmusante 37524007Smmusante /* FALLTHROUGH */ 37534007Smmusante 3754789Sahrens default: 37553237Slling return (zfs_standard_error_fmt(hdl, errno, 37562082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 37572082Seschrock "for '%s'"), dataset)); 3758789Sahrens } 3759789Sahrens } 3760789Sahrens 3761789Sahrens /* 37624543Smarks * If privileged call devfsadm and wait for the links to 37634543Smarks * magically appear. 37644543Smarks * Otherwise, print out an informational message. 3765789Sahrens */ 37664543Smarks 37674543Smarks priv_effective = priv_allocset(); 37684543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 37694543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 37704543Smarks priv_freeset(priv_effective); 37714543Smarks 37724543Smarks if (privileged) { 37734543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 37744543Smarks DI_MAKE_LINK)) == NULL) { 37754543Smarks zfs_error_aux(hdl, strerror(errno)); 37764543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 37774543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 37784543Smarks "for '%s'"), dataset); 37794543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 37804543Smarks return (-1); 37814543Smarks } else { 37824543Smarks (void) di_devlink_fini(&dhdl); 37834543Smarks } 3784789Sahrens } else { 37854543Smarks char pathname[MAXPATHLEN]; 37864543Smarks struct stat64 statbuf; 37874543Smarks int i; 37884543Smarks 37894543Smarks #define MAX_WAIT 10 37904543Smarks 37914543Smarks /* 37924543Smarks * This is the poor mans way of waiting for the link 37934543Smarks * to show up. If after 10 seconds we still don't 37944543Smarks * have it, then print out a message. 37954543Smarks */ 37964543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 37974543Smarks dataset); 37984543Smarks 37994543Smarks for (i = 0; i != MAX_WAIT; i++) { 38004543Smarks if (stat64(pathname, &statbuf) == 0) 38014543Smarks break; 38024543Smarks (void) sleep(1); 38034543Smarks } 38044543Smarks if (i == MAX_WAIT) 38054543Smarks (void) printf(gettext("%s may not be immediately " 38064543Smarks "available\n"), pathname); 3807789Sahrens } 3808789Sahrens 3809789Sahrens return (0); 3810789Sahrens } 3811789Sahrens 3812789Sahrens /* 3813789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 3814789Sahrens */ 3815789Sahrens int 38162082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3817789Sahrens { 3818789Sahrens zfs_cmd_t zc = { 0 }; 3819789Sahrens 3820789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3821789Sahrens 38222082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3823789Sahrens switch (errno) { 3824789Sahrens case ENXIO: 3825789Sahrens /* 3826789Sahrens * Silently ignore the case where the link no longer 3827789Sahrens * exists, so that 'zfs volfini' can be run multiple 3828789Sahrens * times without errors. 3829789Sahrens */ 3830789Sahrens return (0); 3831789Sahrens 3832789Sahrens default: 38333237Slling return (zfs_standard_error_fmt(hdl, errno, 38342082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 38352082Seschrock "links for '%s'"), dataset)); 3836789Sahrens } 3837789Sahrens } 3838789Sahrens 3839789Sahrens return (0); 3840789Sahrens } 38412676Seschrock 38422676Seschrock nvlist_t * 38432676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 38442676Seschrock { 38452676Seschrock return (zhp->zfs_user_props); 38462676Seschrock } 38472676Seschrock 38482676Seschrock /* 38493912Slling * This function is used by 'zfs list' to determine the exact set of columns to 38503912Slling * display, and their maximum widths. This does two main things: 38513912Slling * 38523912Slling * - If this is a list of all properties, then expand the list to include 38533912Slling * all native properties, and set a flag so that for each dataset we look 38543912Slling * for new unique user properties and add them to the list. 38553912Slling * 38563912Slling * - For non fixed-width properties, keep track of the maximum width seen 38573912Slling * so that we can size the column appropriately. 38583912Slling */ 38593912Slling int 38605094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 38613912Slling { 38623912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 38635094Slling zprop_list_t *entry; 38645094Slling zprop_list_t **last, **start; 38653912Slling nvlist_t *userprops, *propval; 38663912Slling nvpair_t *elem; 38673912Slling char *strval; 38683912Slling char buf[ZFS_MAXPROPLEN]; 38693912Slling 38705094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 38713912Slling return (-1); 38722676Seschrock 38732676Seschrock userprops = zfs_get_user_props(zhp); 38742676Seschrock 38752676Seschrock entry = *plp; 38762676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 38772676Seschrock /* 38782676Seschrock * Go through and add any user properties as necessary. We 38792676Seschrock * start by incrementing our list pointer to the first 38802676Seschrock * non-native property. 38812676Seschrock */ 38822676Seschrock start = plp; 38832676Seschrock while (*start != NULL) { 38845094Slling if ((*start)->pl_prop == ZPROP_INVAL) 38852676Seschrock break; 38862676Seschrock start = &(*start)->pl_next; 38872676Seschrock } 38882676Seschrock 38892676Seschrock elem = NULL; 38902676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 38912676Seschrock /* 38922676Seschrock * See if we've already found this property in our list. 38932676Seschrock */ 38942676Seschrock for (last = start; *last != NULL; 38952676Seschrock last = &(*last)->pl_next) { 38962676Seschrock if (strcmp((*last)->pl_user_prop, 38972676Seschrock nvpair_name(elem)) == 0) 38982676Seschrock break; 38992676Seschrock } 39002676Seschrock 39012676Seschrock if (*last == NULL) { 39022676Seschrock if ((entry = zfs_alloc(hdl, 39035094Slling sizeof (zprop_list_t))) == NULL || 39042676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 39052676Seschrock nvpair_name(elem)))) == NULL) { 39062676Seschrock free(entry); 39072676Seschrock return (-1); 39082676Seschrock } 39092676Seschrock 39105094Slling entry->pl_prop = ZPROP_INVAL; 39112676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 39122676Seschrock entry->pl_all = B_TRUE; 39132676Seschrock *last = entry; 39142676Seschrock } 39152676Seschrock } 39162676Seschrock } 39172676Seschrock 39182676Seschrock /* 39192676Seschrock * Now go through and check the width of any non-fixed columns 39202676Seschrock */ 39212676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 39222676Seschrock if (entry->pl_fixed) 39232676Seschrock continue; 39242676Seschrock 39255094Slling if (entry->pl_prop != ZPROP_INVAL) { 39262676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 39272676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 39282676Seschrock if (strlen(buf) > entry->pl_width) 39292676Seschrock entry->pl_width = strlen(buf); 39302676Seschrock } 39312676Seschrock } else if (nvlist_lookup_nvlist(userprops, 39322676Seschrock entry->pl_user_prop, &propval) == 0) { 39332676Seschrock verify(nvlist_lookup_string(propval, 39345094Slling ZPROP_VALUE, &strval) == 0); 39352676Seschrock if (strlen(strval) > entry->pl_width) 39362676Seschrock entry->pl_width = strlen(strval); 39372676Seschrock } 39382676Seschrock } 39392676Seschrock 39402676Seschrock return (0); 39412676Seschrock } 39424543Smarks 39434543Smarks int 39444543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 39454543Smarks { 39464543Smarks zfs_cmd_t zc = { 0 }; 39474543Smarks nvlist_t *nvp; 39484543Smarks gid_t gid; 39494543Smarks uid_t uid; 39504543Smarks const gid_t *groups; 39514543Smarks int group_cnt; 39524543Smarks int error; 39534543Smarks 39544543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 39554543Smarks return (no_memory(hdl)); 39564543Smarks 39574543Smarks uid = ucred_geteuid(cred); 39584543Smarks gid = ucred_getegid(cred); 39594543Smarks group_cnt = ucred_getgroups(cred, &groups); 39604543Smarks 39614543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 39624543Smarks return (1); 39634543Smarks 39644543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 39654543Smarks nvlist_free(nvp); 39664543Smarks return (1); 39674543Smarks } 39684543Smarks 39694543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 39704543Smarks nvlist_free(nvp); 39714543Smarks return (1); 39724543Smarks } 39734543Smarks 39744543Smarks if (nvlist_add_uint32_array(nvp, 39754543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 39764543Smarks nvlist_free(nvp); 39774543Smarks return (1); 39784543Smarks } 39794543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39804543Smarks 39815094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 39824543Smarks return (-1); 39834543Smarks 39844543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 39854543Smarks nvlist_free(nvp); 39864543Smarks return (error); 39874543Smarks } 39884543Smarks 39894543Smarks int 39904543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 39915331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 39924543Smarks { 39934543Smarks zfs_cmd_t zc = { 0 }; 39944543Smarks int error; 39954543Smarks 39964543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39974543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 39984543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 39994543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 40005331Samw zc.zc_share.z_sharetype = operation; 40014543Smarks zc.zc_share.z_sharemax = sharemax; 40024543Smarks 40034543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 40044543Smarks return (error); 40054543Smarks } 4006