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 /* 235977Smarks * Copyright 2008 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 1592*5993Smarks static char * 1593*5993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note) 1594*5993Smarks { 1595*5993Smarks /* 1596*5993Smarks * Don't put newlines on end of lines 1597*5993Smarks */ 1598*5993Smarks switch (note) { 1599*5993Smarks case ZFS_DELEG_NOTE_CREATE: 1600*5993Smarks return (dgettext(TEXT_DOMAIN, 1601*5993Smarks "Must also have the 'mount' ability")); 1602*5993Smarks case ZFS_DELEG_NOTE_DESTROY: 1603*5993Smarks return (dgettext(TEXT_DOMAIN, 1604*5993Smarks "Must also have the 'mount' ability")); 1605*5993Smarks case ZFS_DELEG_NOTE_SNAPSHOT: 1606*5993Smarks return (dgettext(TEXT_DOMAIN, 1607*5993Smarks "Must also have the 'mount' ability")); 1608*5993Smarks case ZFS_DELEG_NOTE_ROLLBACK: 1609*5993Smarks return (dgettext(TEXT_DOMAIN, 1610*5993Smarks "Must also have the 'mount' ability")); 1611*5993Smarks case ZFS_DELEG_NOTE_CLONE: 1612*5993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'create' " 1613*5993Smarks "ability and 'mount'\n" 1614*5993Smarks "\t\t\t\tability in the origin file system")); 1615*5993Smarks case ZFS_DELEG_NOTE_PROMOTE: 1616*5993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n" 1617*5993Smarks "\t\t\t\tand 'promote' ability in the origin file system")); 1618*5993Smarks case ZFS_DELEG_NOTE_RENAME: 1619*5993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' " 1620*5993Smarks "and 'create' \n\t\t\t\tability in the new parent")); 1621*5993Smarks case ZFS_DELEG_NOTE_RECEIVE: 1622*5993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'" 1623*5993Smarks " and 'create' ability")); 1624*5993Smarks case ZFS_DELEG_NOTE_USERPROP: 1625*5993Smarks return (dgettext(TEXT_DOMAIN, 1626*5993Smarks "Allows changing any user property")); 1627*5993Smarks case ZFS_DELEG_NOTE_ALLOW: 1628*5993Smarks return (dgettext(TEXT_DOMAIN, 1629*5993Smarks "Must also have the permission that is being\n" 1630*5993Smarks "\t\t\t\tallowed")); 1631*5993Smarks case ZFS_DELEG_NOTE_MOUNT: 1632*5993Smarks return (dgettext(TEXT_DOMAIN, 1633*5993Smarks "Allows mount/umount of ZFS datasets")); 1634*5993Smarks case ZFS_DELEG_NOTE_SHARE: 1635*5993Smarks return (dgettext(TEXT_DOMAIN, 1636*5993Smarks "Allows sharing file systems over NFS or SMB\n" 1637*5993Smarks "\t\t\t\tprotocols")); 1638*5993Smarks case ZFS_DELEG_NOTE_NONE: 1639*5993Smarks default: 1640*5993Smarks return (dgettext(TEXT_DOMAIN, "")); 1641*5993Smarks } 1642*5993Smarks } 1643*5993Smarks 1644*5993Smarks typedef enum { 1645*5993Smarks ZFS_DELEG_SUBCOMMAND, 1646*5993Smarks ZFS_DELEG_PROP, 1647*5993Smarks ZFS_DELEG_OTHER 1648*5993Smarks } zfs_deleg_perm_type_t; 1649*5993Smarks 1650*5993Smarks /* 1651*5993Smarks * is the permission a subcommand or other? 1652*5993Smarks */ 1653*5993Smarks zfs_deleg_perm_type_t 1654*5993Smarks zfs_deleg_perm_type(const char *perm) 1655*5993Smarks { 1656*5993Smarks if (strcmp(perm, "userprop") == 0) 1657*5993Smarks return (ZFS_DELEG_OTHER); 1658*5993Smarks else 1659*5993Smarks return (ZFS_DELEG_SUBCOMMAND); 1660*5993Smarks } 1661*5993Smarks 1662*5993Smarks static char * 1663*5993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type) 1664*5993Smarks { 1665*5993Smarks switch (type) { 1666*5993Smarks case ZFS_DELEG_SUBCOMMAND: 1667*5993Smarks return (dgettext(TEXT_DOMAIN, "subcommand")); 1668*5993Smarks case ZFS_DELEG_PROP: 1669*5993Smarks return (dgettext(TEXT_DOMAIN, "property")); 1670*5993Smarks case ZFS_DELEG_OTHER: 1671*5993Smarks return (dgettext(TEXT_DOMAIN, "other")); 1672*5993Smarks } 1673*5993Smarks return (""); 1674*5993Smarks } 1675*5993Smarks 1676*5993Smarks /*ARGSUSED*/ 1677*5993Smarks static int 1678*5993Smarks zfs_deleg_prop_cb(int prop, void *cb) 1679*5993Smarks { 1680*5993Smarks if (zfs_prop_delegatable(prop)) 1681*5993Smarks (void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop), 1682*5993Smarks zfs_deleg_perm_type_str(ZFS_DELEG_PROP)); 1683*5993Smarks 1684*5993Smarks return (ZPROP_CONT); 1685*5993Smarks } 1686*5993Smarks 1687*5993Smarks void 1688*5993Smarks zfs_deleg_permissions(void) 1689*5993Smarks { 1690*5993Smarks int i; 1691*5993Smarks 1692*5993Smarks (void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME", 1693*5993Smarks "TYPE", "NOTES"); 1694*5993Smarks 1695*5993Smarks /* 1696*5993Smarks * First print out the subcommands 1697*5993Smarks */ 1698*5993Smarks for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) { 1699*5993Smarks (void) fprintf(stderr, "%-15s %-15s\t%s\n", 1700*5993Smarks zfs_deleg_perm_tab[i].z_perm, 1701*5993Smarks zfs_deleg_perm_type_str( 1702*5993Smarks zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)), 1703*5993Smarks zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note)); 1704*5993Smarks } 1705*5993Smarks 1706*5993Smarks (void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE, 1707*5993Smarks ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME); 1708*5993Smarks } 1709*5993Smarks 1710789Sahrens /* 1711789Sahrens * Given a property name and value, set the property for the given dataset. 1712789Sahrens */ 1713789Sahrens int 17142676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1715789Sahrens { 1716789Sahrens zfs_cmd_t zc = { 0 }; 17172676Seschrock int ret = -1; 17182676Seschrock prop_changelist_t *cl = NULL; 17192082Seschrock char errbuf[1024]; 17202082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 17212676Seschrock nvlist_t *nvl = NULL, *realprops; 17222676Seschrock zfs_prop_t prop; 17232082Seschrock 17242082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 17252676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 17262082Seschrock zhp->zfs_name); 17272082Seschrock 17282676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 17292676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 17302676Seschrock (void) no_memory(hdl); 17312676Seschrock goto error; 1732789Sahrens } 1733789Sahrens 17345094Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 17352676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 17362676Seschrock goto error; 17375094Slling 17382676Seschrock nvlist_free(nvl); 17392676Seschrock nvl = realprops; 17402676Seschrock 17412676Seschrock prop = zfs_name_to_prop(propname); 17422676Seschrock 1743789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 17442676Seschrock goto error; 1745789Sahrens 1746789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 17472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17482082Seschrock "child dataset with inherited mountpoint is used " 17492082Seschrock "in a non-global zone")); 17502082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1751789Sahrens goto error; 1752789Sahrens } 1753789Sahrens 1754789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1755789Sahrens goto error; 1756789Sahrens 1757789Sahrens /* 1758789Sahrens * Execute the corresponding ioctl() to set this property. 1759789Sahrens */ 1760789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1761789Sahrens 17625094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 17632676Seschrock goto error; 17642676Seschrock 17654543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1766789Sahrens if (ret != 0) { 1767789Sahrens switch (errno) { 1768789Sahrens 1769789Sahrens case ENOSPC: 1770789Sahrens /* 1771789Sahrens * For quotas and reservations, ENOSPC indicates 1772789Sahrens * something different; setting a quota or reservation 1773789Sahrens * doesn't use any disk space. 1774789Sahrens */ 1775789Sahrens switch (prop) { 1776789Sahrens case ZFS_PROP_QUOTA: 17775378Sck153898 case ZFS_PROP_REFQUOTA: 17782082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17792082Seschrock "size is less than current used or " 17802082Seschrock "reserved space")); 17812082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1782789Sahrens break; 1783789Sahrens 1784789Sahrens case ZFS_PROP_RESERVATION: 17855378Sck153898 case ZFS_PROP_REFRESERVATION: 17862082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17872082Seschrock "size is greater than available space")); 17882082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1789789Sahrens break; 1790789Sahrens 1791789Sahrens default: 17922082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1793789Sahrens break; 1794789Sahrens } 1795789Sahrens break; 1796789Sahrens 1797789Sahrens case EBUSY: 17982082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 17992082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 18002082Seschrock else 18012676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1802789Sahrens break; 1803789Sahrens 18041175Slling case EROFS: 18052082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 18061175Slling break; 18071175Slling 18083886Sahl case ENOTSUP: 18093886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18105977Smarks "pool and or dataset must be upgraded to set this " 18114603Sahrens "property or value")); 18123886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 18133886Sahl break; 18143886Sahl 1815789Sahrens case EOVERFLOW: 1816789Sahrens /* 1817789Sahrens * This platform can't address a volume this big. 1818789Sahrens */ 1819789Sahrens #ifdef _ILP32 1820789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 18212082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1822789Sahrens break; 1823789Sahrens } 1824789Sahrens #endif 18252082Seschrock /* FALLTHROUGH */ 1826789Sahrens default: 18272082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1828789Sahrens } 1829789Sahrens } else { 1830789Sahrens /* 1831789Sahrens * Refresh the statistics so the new property value 1832789Sahrens * is reflected. 1833789Sahrens */ 18342676Seschrock if ((ret = changelist_postfix(cl)) == 0) 18352676Seschrock (void) get_stats(zhp); 1836789Sahrens } 1837789Sahrens 1838789Sahrens error: 18392676Seschrock nvlist_free(nvl); 18402676Seschrock zcmd_free_nvlists(&zc); 18412676Seschrock if (cl) 18422676Seschrock changelist_free(cl); 1843789Sahrens return (ret); 1844789Sahrens } 1845789Sahrens 1846789Sahrens /* 1847789Sahrens * Given a property, inherit the value from the parent dataset. 1848789Sahrens */ 1849789Sahrens int 18502676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1851789Sahrens { 1852789Sahrens zfs_cmd_t zc = { 0 }; 1853789Sahrens int ret; 1854789Sahrens prop_changelist_t *cl; 18552082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 18562082Seschrock char errbuf[1024]; 18572676Seschrock zfs_prop_t prop; 18582082Seschrock 18592082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 18602082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1861789Sahrens 18625094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 18632676Seschrock /* 18642676Seschrock * For user properties, the amount of work we have to do is very 18652676Seschrock * small, so just do it here. 18662676Seschrock */ 18672676Seschrock if (!zfs_prop_user(propname)) { 18682676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18692676Seschrock "invalid property")); 18702676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 18712676Seschrock } 18722676Seschrock 18732676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 18742676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 18752676Seschrock 18764849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 18772676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 18782676Seschrock 18792676Seschrock return (0); 18802676Seschrock } 18812676Seschrock 1882789Sahrens /* 1883789Sahrens * Verify that this property is inheritable. 1884789Sahrens */ 18852082Seschrock if (zfs_prop_readonly(prop)) 18862082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 18872082Seschrock 18882082Seschrock if (!zfs_prop_inheritable(prop)) 18892082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1890789Sahrens 1891789Sahrens /* 1892789Sahrens * Check to see if the value applies to this type 1893789Sahrens */ 18942082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 18952082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1896789Sahrens 18973443Srm160521 /* 18983443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 18993443Srm160521 */ 19003443Srm160521 propname = zfs_prop_to_name(prop); 1901789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19022676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1903789Sahrens 1904789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1905789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 19062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19072082Seschrock "dataset is used in a non-global zone")); 19082082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1909789Sahrens } 1910789Sahrens 1911789Sahrens /* 1912789Sahrens * Determine datasets which will be affected by this change, if any. 1913789Sahrens */ 1914789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1915789Sahrens return (-1); 1916789Sahrens 1917789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 19182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19192082Seschrock "child dataset with inherited mountpoint is used " 19202082Seschrock "in a non-global zone")); 19212082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1922789Sahrens goto error; 1923789Sahrens } 1924789Sahrens 1925789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1926789Sahrens goto error; 1927789Sahrens 19284849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 19292082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1930789Sahrens } else { 1931789Sahrens 19322169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1933789Sahrens goto error; 1934789Sahrens 1935789Sahrens /* 1936789Sahrens * Refresh the statistics so the new property is reflected. 1937789Sahrens */ 1938789Sahrens (void) get_stats(zhp); 1939789Sahrens } 1940789Sahrens 1941789Sahrens error: 1942789Sahrens changelist_free(cl); 1943789Sahrens return (ret); 1944789Sahrens } 1945789Sahrens 1946789Sahrens /* 19471356Seschrock * True DSL properties are stored in an nvlist. The following two functions 19481356Seschrock * extract them appropriately. 19491356Seschrock */ 19501356Seschrock static uint64_t 19511356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 19521356Seschrock { 19531356Seschrock nvlist_t *nv; 19541356Seschrock uint64_t value; 19551356Seschrock 19562885Sahrens *source = NULL; 19571356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 19581356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 19595094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 19605094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 19611356Seschrock } else { 19621356Seschrock value = zfs_prop_default_numeric(prop); 19631356Seschrock *source = ""; 19641356Seschrock } 19651356Seschrock 19661356Seschrock return (value); 19671356Seschrock } 19681356Seschrock 19691356Seschrock static char * 19701356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 19711356Seschrock { 19721356Seschrock nvlist_t *nv; 19731356Seschrock char *value; 19741356Seschrock 19752885Sahrens *source = NULL; 19761356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 19771356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 19785094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 19795094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 19801356Seschrock } else { 19811356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 19821356Seschrock value = ""; 19831356Seschrock *source = ""; 19841356Seschrock } 19851356Seschrock 19861356Seschrock return (value); 19871356Seschrock } 19881356Seschrock 19891356Seschrock /* 1990789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1991789Sahrens * zfs_prop_get_int() are built using this interface. 1992789Sahrens * 1993789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1994789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1995789Sahrens * If they differ from the on-disk values, report the current values and mark 1996789Sahrens * the source "temporary". 1997789Sahrens */ 19982082Seschrock static int 19995094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 20002082Seschrock char **source, uint64_t *val) 2001789Sahrens { 20025147Srm160521 zfs_cmd_t zc = { 0 }; 20035592Stimh nvlist_t *zplprops = NULL; 2004789Sahrens struct mnttab mnt; 20053265Sahrens char *mntopt_on = NULL; 20063265Sahrens char *mntopt_off = NULL; 2007789Sahrens 2008789Sahrens *source = NULL; 2009789Sahrens 20103265Sahrens switch (prop) { 20113265Sahrens case ZFS_PROP_ATIME: 20123265Sahrens mntopt_on = MNTOPT_ATIME; 20133265Sahrens mntopt_off = MNTOPT_NOATIME; 20143265Sahrens break; 20153265Sahrens 20163265Sahrens case ZFS_PROP_DEVICES: 20173265Sahrens mntopt_on = MNTOPT_DEVICES; 20183265Sahrens mntopt_off = MNTOPT_NODEVICES; 20193265Sahrens break; 20203265Sahrens 20213265Sahrens case ZFS_PROP_EXEC: 20223265Sahrens mntopt_on = MNTOPT_EXEC; 20233265Sahrens mntopt_off = MNTOPT_NOEXEC; 20243265Sahrens break; 20253265Sahrens 20263265Sahrens case ZFS_PROP_READONLY: 20273265Sahrens mntopt_on = MNTOPT_RO; 20283265Sahrens mntopt_off = MNTOPT_RW; 20293265Sahrens break; 20303265Sahrens 20313265Sahrens case ZFS_PROP_SETUID: 20323265Sahrens mntopt_on = MNTOPT_SETUID; 20333265Sahrens mntopt_off = MNTOPT_NOSETUID; 20343265Sahrens break; 20353265Sahrens 20363265Sahrens case ZFS_PROP_XATTR: 20373265Sahrens mntopt_on = MNTOPT_XATTR; 20383265Sahrens mntopt_off = MNTOPT_NOXATTR; 20393265Sahrens break; 20405331Samw 20415331Samw case ZFS_PROP_NBMAND: 20425331Samw mntopt_on = MNTOPT_NBMAND; 20435331Samw mntopt_off = MNTOPT_NONBMAND; 20445331Samw break; 20453265Sahrens } 20463265Sahrens 20472474Seschrock /* 20482474Seschrock * Because looking up the mount options is potentially expensive 20492474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 20502474Seschrock * we're looking up a property which requires its presence. 20512474Seschrock */ 20522474Seschrock if (!zhp->zfs_mntcheck && 20533265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 20543265Sahrens struct mnttab entry, search = { 0 }; 20553265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 20562474Seschrock 20572474Seschrock search.mnt_special = (char *)zhp->zfs_name; 20582474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 20593265Sahrens rewind(mnttab); 20603265Sahrens 20613265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 20623265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 20633265Sahrens entry.mnt_mntopts); 20643265Sahrens if (zhp->zfs_mntopts == NULL) 20653265Sahrens return (-1); 20663265Sahrens } 20672474Seschrock 20682474Seschrock zhp->zfs_mntcheck = B_TRUE; 20692474Seschrock } 20702474Seschrock 2071789Sahrens if (zhp->zfs_mntopts == NULL) 2072789Sahrens mnt.mnt_mntopts = ""; 2073789Sahrens else 2074789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2075789Sahrens 2076789Sahrens switch (prop) { 2077789Sahrens case ZFS_PROP_ATIME: 20783265Sahrens case ZFS_PROP_DEVICES: 20793265Sahrens case ZFS_PROP_EXEC: 20803265Sahrens case ZFS_PROP_READONLY: 20813265Sahrens case ZFS_PROP_SETUID: 20823265Sahrens case ZFS_PROP_XATTR: 20835331Samw case ZFS_PROP_NBMAND: 20842082Seschrock *val = getprop_uint64(zhp, prop, source); 20852082Seschrock 20863265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 20872082Seschrock *val = B_TRUE; 2088789Sahrens if (src) 20895094Slling *src = ZPROP_SRC_TEMPORARY; 20903265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 20912082Seschrock *val = B_FALSE; 2092789Sahrens if (src) 20935094Slling *src = ZPROP_SRC_TEMPORARY; 2094789Sahrens } 20952082Seschrock break; 2096789Sahrens 20973265Sahrens case ZFS_PROP_CANMOUNT: 20982082Seschrock *val = getprop_uint64(zhp, prop, source); 20993417Srm160521 if (*val == 0) 21003417Srm160521 *source = zhp->zfs_name; 21013417Srm160521 else 21023417Srm160521 *source = ""; /* default */ 21032082Seschrock break; 2104789Sahrens 2105789Sahrens case ZFS_PROP_QUOTA: 21065378Sck153898 case ZFS_PROP_REFQUOTA: 2107789Sahrens case ZFS_PROP_RESERVATION: 21085378Sck153898 case ZFS_PROP_REFRESERVATION: 21092885Sahrens *val = getprop_uint64(zhp, prop, source); 21102885Sahrens if (*val == 0) 2111789Sahrens *source = ""; /* default */ 2112789Sahrens else 2113789Sahrens *source = zhp->zfs_name; 21142082Seschrock break; 2115789Sahrens 2116789Sahrens case ZFS_PROP_MOUNTED: 21172082Seschrock *val = (zhp->zfs_mntopts != NULL); 21182082Seschrock break; 2119789Sahrens 21203377Seschrock case ZFS_PROP_NUMCLONES: 21213377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 21223377Seschrock break; 21233377Seschrock 21245147Srm160521 case ZFS_PROP_VERSION: 21255498Stimh case ZFS_PROP_NORMALIZE: 21265498Stimh case ZFS_PROP_UTF8ONLY: 21275498Stimh case ZFS_PROP_CASE: 21285498Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 21295498Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 21305473Srm160521 return (-1); 21315147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21325498Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 21335498Stimh zcmd_free_nvlists(&zc); 21345147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 21355498Stimh "unable to get %s property"), 21365498Stimh zfs_prop_to_name(prop)); 21375147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 21385147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 21395147Srm160521 } 21405498Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 21415498Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 21425498Stimh val) != 0) { 21435498Stimh zcmd_free_nvlists(&zc); 21445498Stimh zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 21455498Stimh "unable to get %s property"), 21465498Stimh zfs_prop_to_name(prop)); 21475498Stimh return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM, 21485498Stimh dgettext(TEXT_DOMAIN, "internal error"))); 21495498Stimh } 21505592Stimh if (zplprops) 21515592Stimh nvlist_free(zplprops); 21525498Stimh zcmd_free_nvlists(&zc); 21535147Srm160521 break; 21545147Srm160521 2155789Sahrens default: 21564577Sahrens switch (zfs_prop_get_type(prop)) { 21574787Sahrens case PROP_TYPE_NUMBER: 21584787Sahrens case PROP_TYPE_INDEX: 21594577Sahrens *val = getprop_uint64(zhp, prop, source); 21604577Sahrens break; 21614577Sahrens 21624787Sahrens case PROP_TYPE_STRING: 21634577Sahrens default: 21644577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 21654577Sahrens "cannot get non-numeric property")); 21664577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 21674577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 21684577Sahrens } 2169789Sahrens } 2170789Sahrens 2171789Sahrens return (0); 2172789Sahrens } 2173789Sahrens 2174789Sahrens /* 2175789Sahrens * Calculate the source type, given the raw source string. 2176789Sahrens */ 2177789Sahrens static void 21785094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2179789Sahrens char *statbuf, size_t statlen) 2180789Sahrens { 21815094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2182789Sahrens return; 2183789Sahrens 2184789Sahrens if (source == NULL) { 21855094Slling *srctype = ZPROP_SRC_NONE; 2186789Sahrens } else if (source[0] == '\0') { 21875094Slling *srctype = ZPROP_SRC_DEFAULT; 2188789Sahrens } else { 2189789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 21905094Slling *srctype = ZPROP_SRC_LOCAL; 2191789Sahrens } else { 2192789Sahrens (void) strlcpy(statbuf, source, statlen); 21935094Slling *srctype = ZPROP_SRC_INHERITED; 2194789Sahrens } 2195789Sahrens } 2196789Sahrens 2197789Sahrens } 2198789Sahrens 2199789Sahrens /* 2200789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2201789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2202789Sahrens * human-readable form. 2203789Sahrens * 2204789Sahrens * Returns 0 on success, or -1 on error. 2205789Sahrens */ 2206789Sahrens int 2207789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 22085094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2209789Sahrens { 2210789Sahrens char *source = NULL; 2211789Sahrens uint64_t val; 2212789Sahrens char *str; 2213789Sahrens const char *root; 22142676Seschrock const char *strval; 2215789Sahrens 2216789Sahrens /* 2217789Sahrens * Check to see if this property applies to our object 2218789Sahrens */ 2219789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2220789Sahrens return (-1); 2221789Sahrens 2222789Sahrens if (src) 22235094Slling *src = ZPROP_SRC_NONE; 2224789Sahrens 2225789Sahrens switch (prop) { 2226789Sahrens case ZFS_PROP_CREATION: 2227789Sahrens /* 2228789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2229789Sahrens * this into a string unless 'literal' is specified. 2230789Sahrens */ 2231789Sahrens { 22322885Sahrens val = getprop_uint64(zhp, prop, &source); 22332885Sahrens time_t time = (time_t)val; 2234789Sahrens struct tm t; 2235789Sahrens 2236789Sahrens if (literal || 2237789Sahrens localtime_r(&time, &t) == NULL || 2238789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2239789Sahrens &t) == 0) 22402885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2241789Sahrens } 2242789Sahrens break; 2243789Sahrens 2244789Sahrens case ZFS_PROP_MOUNTPOINT: 2245789Sahrens /* 2246789Sahrens * Getting the precise mountpoint can be tricky. 2247789Sahrens * 2248789Sahrens * - for 'none' or 'legacy', return those values. 2249789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2250789Sahrens * - for inherited mountpoints, we want to take everything 2251789Sahrens * after our ancestor and append it to the inherited value. 2252789Sahrens * 2253789Sahrens * If the pool has an alternate root, we want to prepend that 2254789Sahrens * root to any values we return. 2255789Sahrens */ 22561544Seschrock root = zhp->zfs_root; 22571356Seschrock str = getprop_string(zhp, prop, &source); 22581356Seschrock 22591356Seschrock if (str[0] == '\0') { 2260789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2261789Sahrens root, zhp->zfs_name); 22621356Seschrock } else if (str[0] == '/') { 22631356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2264789Sahrens 2265789Sahrens if (relpath[0] == '/') 2266789Sahrens relpath++; 22671356Seschrock if (str[1] == '\0') 22681356Seschrock str++; 2269789Sahrens 2270789Sahrens if (relpath[0] == '\0') 2271789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 22721356Seschrock root, str); 2273789Sahrens else 2274789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 22751356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2276789Sahrens relpath); 2277789Sahrens } else { 2278789Sahrens /* 'legacy' or 'none' */ 22791356Seschrock (void) strlcpy(propbuf, str, proplen); 2280789Sahrens } 2281789Sahrens 2282789Sahrens break; 2283789Sahrens 2284789Sahrens case ZFS_PROP_ORIGIN: 22852885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2286789Sahrens proplen); 2287789Sahrens /* 2288789Sahrens * If there is no parent at all, return failure to indicate that 2289789Sahrens * it doesn't apply to this dataset. 2290789Sahrens */ 2291789Sahrens if (propbuf[0] == '\0') 2292789Sahrens return (-1); 2293789Sahrens break; 2294789Sahrens 2295789Sahrens case ZFS_PROP_QUOTA: 22965378Sck153898 case ZFS_PROP_REFQUOTA: 2297789Sahrens case ZFS_PROP_RESERVATION: 22985378Sck153898 case ZFS_PROP_REFRESERVATION: 22995378Sck153898 23002082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 23012082Seschrock return (-1); 2302789Sahrens 2303789Sahrens /* 2304789Sahrens * If quota or reservation is 0, we translate this into 'none' 2305789Sahrens * (unless literal is set), and indicate that it's the default 2306789Sahrens * value. Otherwise, we print the number nicely and indicate 2307789Sahrens * that its set locally. 2308789Sahrens */ 2309789Sahrens if (val == 0) { 2310789Sahrens if (literal) 2311789Sahrens (void) strlcpy(propbuf, "0", proplen); 2312789Sahrens else 2313789Sahrens (void) strlcpy(propbuf, "none", proplen); 2314789Sahrens } else { 2315789Sahrens if (literal) 23162856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 23173912Slling (u_longlong_t)val); 2318789Sahrens else 2319789Sahrens zfs_nicenum(val, propbuf, proplen); 2320789Sahrens } 2321789Sahrens break; 2322789Sahrens 2323789Sahrens case ZFS_PROP_COMPRESSRATIO: 23242082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 23252082Seschrock return (-1); 23262856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 23272856Snd150628 val / 100, (longlong_t)val % 100); 2328789Sahrens break; 2329789Sahrens 2330789Sahrens case ZFS_PROP_TYPE: 2331789Sahrens switch (zhp->zfs_type) { 2332789Sahrens case ZFS_TYPE_FILESYSTEM: 2333789Sahrens str = "filesystem"; 2334789Sahrens break; 2335789Sahrens case ZFS_TYPE_VOLUME: 2336789Sahrens str = "volume"; 2337789Sahrens break; 2338789Sahrens case ZFS_TYPE_SNAPSHOT: 2339789Sahrens str = "snapshot"; 2340789Sahrens break; 2341789Sahrens default: 23422082Seschrock abort(); 2343789Sahrens } 2344789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2345789Sahrens break; 2346789Sahrens 2347789Sahrens case ZFS_PROP_MOUNTED: 2348789Sahrens /* 2349789Sahrens * The 'mounted' property is a pseudo-property that described 2350789Sahrens * whether the filesystem is currently mounted. Even though 2351789Sahrens * it's a boolean value, the typical values of "on" and "off" 2352789Sahrens * don't make sense, so we translate to "yes" and "no". 2353789Sahrens */ 23542082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 23552082Seschrock src, &source, &val) != 0) 23562082Seschrock return (-1); 23572082Seschrock if (val) 2358789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2359789Sahrens else 2360789Sahrens (void) strlcpy(propbuf, "no", proplen); 2361789Sahrens break; 2362789Sahrens 2363789Sahrens case ZFS_PROP_NAME: 2364789Sahrens /* 2365789Sahrens * The 'name' property is a pseudo-property derived from the 2366789Sahrens * dataset name. It is presented as a real property to simplify 2367789Sahrens * consumers. 2368789Sahrens */ 2369789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2370789Sahrens break; 2371789Sahrens 2372789Sahrens default: 23734577Sahrens switch (zfs_prop_get_type(prop)) { 23744787Sahrens case PROP_TYPE_NUMBER: 23754577Sahrens if (get_numeric_property(zhp, prop, src, 23764577Sahrens &source, &val) != 0) 23774577Sahrens return (-1); 23784577Sahrens if (literal) 23794577Sahrens (void) snprintf(propbuf, proplen, "%llu", 23804577Sahrens (u_longlong_t)val); 23814577Sahrens else 23824577Sahrens zfs_nicenum(val, propbuf, proplen); 23834577Sahrens break; 23844577Sahrens 23854787Sahrens case PROP_TYPE_STRING: 23864577Sahrens (void) strlcpy(propbuf, 23874577Sahrens getprop_string(zhp, prop, &source), proplen); 23884577Sahrens break; 23894577Sahrens 23904787Sahrens case PROP_TYPE_INDEX: 23914861Sahrens if (get_numeric_property(zhp, prop, src, 23924861Sahrens &source, &val) != 0) 23934861Sahrens return (-1); 23944861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 23954577Sahrens return (-1); 23964577Sahrens (void) strlcpy(propbuf, strval, proplen); 23974577Sahrens break; 23984577Sahrens 23994577Sahrens default: 24004577Sahrens abort(); 24014577Sahrens } 2402789Sahrens } 2403789Sahrens 2404789Sahrens get_source(zhp, src, source, statbuf, statlen); 2405789Sahrens 2406789Sahrens return (0); 2407789Sahrens } 2408789Sahrens 2409789Sahrens /* 2410789Sahrens * Utility function to get the given numeric property. Does no validation that 2411789Sahrens * the given property is the appropriate type; should only be used with 2412789Sahrens * hard-coded property types. 2413789Sahrens */ 2414789Sahrens uint64_t 2415789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2416789Sahrens { 2417789Sahrens char *source; 24182082Seschrock uint64_t val; 24192082Seschrock 24205367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 24212082Seschrock 24222082Seschrock return (val); 2423789Sahrens } 2424789Sahrens 24255713Srm160521 int 24265713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 24275713Srm160521 { 24285713Srm160521 char buf[64]; 24295713Srm160521 24305713Srm160521 zfs_nicenum(val, buf, sizeof (buf)); 24315713Srm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 24325713Srm160521 } 24335713Srm160521 2434789Sahrens /* 2435789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2436789Sahrens */ 2437789Sahrens int 2438789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 24395094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2440789Sahrens { 2441789Sahrens char *source; 2442789Sahrens 2443789Sahrens /* 2444789Sahrens * Check to see if this property applies to our object 2445789Sahrens */ 24464849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 24473237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 24482082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 24492082Seschrock zfs_prop_to_name(prop))); 24504849Sahrens } 2451789Sahrens 2452789Sahrens if (src) 24535094Slling *src = ZPROP_SRC_NONE; 2454789Sahrens 24552082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 24562082Seschrock return (-1); 2457789Sahrens 2458789Sahrens get_source(zhp, src, source, statbuf, statlen); 2459789Sahrens 2460789Sahrens return (0); 2461789Sahrens } 2462789Sahrens 2463789Sahrens /* 2464789Sahrens * Returns the name of the given zfs handle. 2465789Sahrens */ 2466789Sahrens const char * 2467789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2468789Sahrens { 2469789Sahrens return (zhp->zfs_name); 2470789Sahrens } 2471789Sahrens 2472789Sahrens /* 2473789Sahrens * Returns the type of the given zfs handle. 2474789Sahrens */ 2475789Sahrens zfs_type_t 2476789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2477789Sahrens { 2478789Sahrens return (zhp->zfs_type); 2479789Sahrens } 2480789Sahrens 2481789Sahrens /* 24821356Seschrock * Iterate over all child filesystems 2483789Sahrens */ 2484789Sahrens int 24851356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2486789Sahrens { 2487789Sahrens zfs_cmd_t zc = { 0 }; 2488789Sahrens zfs_handle_t *nzhp; 2489789Sahrens int ret; 2490789Sahrens 24915367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 24925367Sahrens return (0); 24935367Sahrens 2494789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 24952082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2496789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2497789Sahrens /* 2498789Sahrens * Ignore private dataset names. 2499789Sahrens */ 2500789Sahrens if (dataset_name_hidden(zc.zc_name)) 2501789Sahrens continue; 2502789Sahrens 2503789Sahrens /* 2504789Sahrens * Silently ignore errors, as the only plausible explanation is 2505789Sahrens * that the pool has since been removed. 2506789Sahrens */ 25072082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 25082082Seschrock zc.zc_name)) == NULL) 2509789Sahrens continue; 2510789Sahrens 2511789Sahrens if ((ret = func(nzhp, data)) != 0) 2512789Sahrens return (ret); 2513789Sahrens } 2514789Sahrens 2515789Sahrens /* 2516789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2517789Sahrens * returned, then the underlying dataset has been removed since we 2518789Sahrens * obtained the handle. 2519789Sahrens */ 2520789Sahrens if (errno != ESRCH && errno != ENOENT) 25212082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 25222082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2523789Sahrens 25241356Seschrock return (0); 25251356Seschrock } 25261356Seschrock 25271356Seschrock /* 25281356Seschrock * Iterate over all snapshots 25291356Seschrock */ 25301356Seschrock int 25311356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 25321356Seschrock { 25331356Seschrock zfs_cmd_t zc = { 0 }; 25341356Seschrock zfs_handle_t *nzhp; 25351356Seschrock int ret; 2536789Sahrens 25375367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 25385367Sahrens return (0); 25395367Sahrens 2540789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 25412082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 25422082Seschrock &zc) == 0; 2543789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2544789Sahrens 25452082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 25462082Seschrock zc.zc_name)) == NULL) 2547789Sahrens continue; 2548789Sahrens 2549789Sahrens if ((ret = func(nzhp, data)) != 0) 2550789Sahrens return (ret); 2551789Sahrens } 2552789Sahrens 2553789Sahrens /* 2554789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2555789Sahrens * returned, then the underlying dataset has been removed since we 2556789Sahrens * obtained the handle. Silently ignore this case, and return success. 2557789Sahrens */ 2558789Sahrens if (errno != ESRCH && errno != ENOENT) 25592082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 25602082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2561789Sahrens 2562789Sahrens return (0); 2563789Sahrens } 2564789Sahrens 2565789Sahrens /* 25661356Seschrock * Iterate over all children, snapshots and filesystems 25671356Seschrock */ 25681356Seschrock int 25691356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 25701356Seschrock { 25711356Seschrock int ret; 25721356Seschrock 25731356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 25741356Seschrock return (ret); 25751356Seschrock 25761356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 25771356Seschrock } 25781356Seschrock 25791356Seschrock /* 2580789Sahrens * Given a complete name, return just the portion that refers to the parent. 2581789Sahrens * Can return NULL if this is a pool. 2582789Sahrens */ 2583789Sahrens static int 2584789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2585789Sahrens { 2586789Sahrens char *loc; 2587789Sahrens 2588789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2589789Sahrens return (-1); 2590789Sahrens 2591789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2592789Sahrens buf[loc - path] = '\0'; 2593789Sahrens 2594789Sahrens return (0); 2595789Sahrens } 2596789Sahrens 2597789Sahrens /* 25984490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 25994490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 26004490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 26014490Svb160487 * length of already existing prefix of the given path. We also fetch the 26024490Svb160487 * 'zoned' property, which is used to validate property settings when creating 26034490Svb160487 * new datasets. 2604789Sahrens */ 2605789Sahrens static int 26064490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 26074490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2608789Sahrens { 2609789Sahrens zfs_cmd_t zc = { 0 }; 2610789Sahrens char parent[ZFS_MAXNAMELEN]; 2611789Sahrens char *slash; 26121356Seschrock zfs_handle_t *zhp; 26132082Seschrock char errbuf[1024]; 26142082Seschrock 26152082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 26162082Seschrock path); 2617789Sahrens 2618789Sahrens /* get parent, and check to see if this is just a pool */ 2619789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 26202082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26212082Seschrock "missing dataset name")); 26222082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2623789Sahrens } 2624789Sahrens 2625789Sahrens /* check to see if the pool exists */ 2626789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2627789Sahrens slash = parent + strlen(parent); 2628789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2629789Sahrens zc.zc_name[slash - parent] = '\0'; 26302082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2631789Sahrens errno == ENOENT) { 26322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26332082Seschrock "no such pool '%s'"), zc.zc_name); 26342082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2635789Sahrens } 2636789Sahrens 2637789Sahrens /* check to see if the parent dataset exists */ 26384490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 26394490Svb160487 if (errno == ENOENT && accept_ancestor) { 26404490Svb160487 /* 26414490Svb160487 * Go deeper to find an ancestor, give up on top level. 26424490Svb160487 */ 26434490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 26444490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26454490Svb160487 "no such pool '%s'"), zc.zc_name); 26464490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 26474490Svb160487 } 26484490Svb160487 } else if (errno == ENOENT) { 26492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26502082Seschrock "parent does not exist")); 26512082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 26524490Svb160487 } else 26532082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2654789Sahrens } 2655789Sahrens 26562676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2657789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 26582676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 26592082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 26601356Seschrock zfs_close(zhp); 2661789Sahrens return (-1); 2662789Sahrens } 2663789Sahrens 2664789Sahrens /* make sure parent is a filesystem */ 26651356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 26662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26672082Seschrock "parent is not a filesystem")); 26682082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 26691356Seschrock zfs_close(zhp); 2670789Sahrens return (-1); 2671789Sahrens } 2672789Sahrens 26731356Seschrock zfs_close(zhp); 26744490Svb160487 if (prefixlen != NULL) 26754490Svb160487 *prefixlen = strlen(parent); 26764490Svb160487 return (0); 26774490Svb160487 } 26784490Svb160487 26794490Svb160487 /* 26804490Svb160487 * Finds whether the dataset of the given type(s) exists. 26814490Svb160487 */ 26824490Svb160487 boolean_t 26834490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 26844490Svb160487 { 26854490Svb160487 zfs_handle_t *zhp; 26864490Svb160487 26875326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 26884490Svb160487 return (B_FALSE); 26894490Svb160487 26904490Svb160487 /* 26914490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 26924490Svb160487 */ 26934490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 26944490Svb160487 int ds_type = zhp->zfs_type; 26954490Svb160487 26964490Svb160487 zfs_close(zhp); 26974490Svb160487 if (types & ds_type) 26984490Svb160487 return (B_TRUE); 26994490Svb160487 } 27004490Svb160487 return (B_FALSE); 27014490Svb160487 } 27024490Svb160487 27034490Svb160487 /* 27045367Sahrens * Given a path to 'target', create all the ancestors between 27055367Sahrens * the prefixlen portion of the path, and the target itself. 27065367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 27075367Sahrens */ 27085367Sahrens int 27095367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 27105367Sahrens { 27115367Sahrens zfs_handle_t *h; 27125367Sahrens char *cp; 27135367Sahrens const char *opname; 27145367Sahrens 27155367Sahrens /* make sure prefix exists */ 27165367Sahrens cp = target + prefixlen; 27175367Sahrens if (*cp != '/') { 27185367Sahrens assert(strchr(cp, '/') == NULL); 27195367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 27205367Sahrens } else { 27215367Sahrens *cp = '\0'; 27225367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 27235367Sahrens *cp = '/'; 27245367Sahrens } 27255367Sahrens if (h == NULL) 27265367Sahrens return (-1); 27275367Sahrens zfs_close(h); 27285367Sahrens 27295367Sahrens /* 27305367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 27315367Sahrens * up to the prefixlen-long one. 27325367Sahrens */ 27335367Sahrens for (cp = target + prefixlen + 1; 27345367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 27355367Sahrens char *logstr; 27365367Sahrens 27375367Sahrens *cp = '\0'; 27385367Sahrens 27395367Sahrens h = make_dataset_handle(hdl, target); 27405367Sahrens if (h) { 27415367Sahrens /* it already exists, nothing to do here */ 27425367Sahrens zfs_close(h); 27435367Sahrens continue; 27445367Sahrens } 27455367Sahrens 27465367Sahrens logstr = hdl->libzfs_log_str; 27475367Sahrens hdl->libzfs_log_str = NULL; 27485367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 27495367Sahrens NULL) != 0) { 27505367Sahrens hdl->libzfs_log_str = logstr; 27515367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 27525367Sahrens goto ancestorerr; 27535367Sahrens } 27545367Sahrens 27555367Sahrens hdl->libzfs_log_str = logstr; 27565367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 27575367Sahrens if (h == NULL) { 27585367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 27595367Sahrens goto ancestorerr; 27605367Sahrens } 27615367Sahrens 27625367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 27635367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 27645367Sahrens goto ancestorerr; 27655367Sahrens } 27665367Sahrens 27675367Sahrens if (zfs_share(h) != 0) { 27685367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 27695367Sahrens goto ancestorerr; 27705367Sahrens } 27715367Sahrens 27725367Sahrens zfs_close(h); 27735367Sahrens } 27745367Sahrens 27755367Sahrens return (0); 27765367Sahrens 27775367Sahrens ancestorerr: 27785367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27795367Sahrens "failed to %s ancestor '%s'"), opname, target); 27805367Sahrens return (-1); 27815367Sahrens } 27825367Sahrens 27835367Sahrens /* 27844490Svb160487 * Creates non-existing ancestors of the given path. 27854490Svb160487 */ 27864490Svb160487 int 27874490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 27884490Svb160487 { 27894490Svb160487 int prefix; 27904490Svb160487 uint64_t zoned; 27914490Svb160487 char *path_copy; 27924490Svb160487 int rc; 27934490Svb160487 27944490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 27954490Svb160487 return (-1); 27964490Svb160487 27974490Svb160487 if ((path_copy = strdup(path)) != NULL) { 27984490Svb160487 rc = create_parents(hdl, path_copy, prefix); 27994490Svb160487 free(path_copy); 28004490Svb160487 } 28014490Svb160487 if (path_copy == NULL || rc != 0) 28024490Svb160487 return (-1); 28034490Svb160487 2804789Sahrens return (0); 2805789Sahrens } 2806789Sahrens 2807789Sahrens /* 28082676Seschrock * Create a new filesystem or volume. 2809789Sahrens */ 2810789Sahrens int 28112082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 28122676Seschrock nvlist_t *props) 2813789Sahrens { 2814789Sahrens zfs_cmd_t zc = { 0 }; 2815789Sahrens int ret; 2816789Sahrens uint64_t size = 0; 2817789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 28182082Seschrock char errbuf[1024]; 28192676Seschrock uint64_t zoned; 28202082Seschrock 28212082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28222082Seschrock "cannot create '%s'"), path); 2823789Sahrens 2824789Sahrens /* validate the path, taking care to note the extended error message */ 28255326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 28262082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2827789Sahrens 2828789Sahrens /* validate parents exist */ 28294490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2830789Sahrens return (-1); 2831789Sahrens 2832789Sahrens /* 2833789Sahrens * The failure modes when creating a dataset of a different type over 2834789Sahrens * one that already exists is a little strange. In particular, if you 2835789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2836789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2837789Sahrens * first try to see if the dataset exists. 2838789Sahrens */ 2839789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 28405094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 28412082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28422082Seschrock "dataset already exists")); 28432082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2844789Sahrens } 2845789Sahrens 2846789Sahrens if (type == ZFS_TYPE_VOLUME) 2847789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2848789Sahrens else 2849789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2850789Sahrens 28515094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 28523912Slling zoned, NULL, errbuf)) == 0) 28532676Seschrock return (-1); 28542676Seschrock 2855789Sahrens if (type == ZFS_TYPE_VOLUME) { 28561133Seschrock /* 28571133Seschrock * If we are creating a volume, the size and block size must 28581133Seschrock * satisfy a few restraints. First, the blocksize must be a 28591133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 28601133Seschrock * volsize must be a multiple of the block size, and cannot be 28611133Seschrock * zero. 28621133Seschrock */ 28632676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 28642676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 28652676Seschrock nvlist_free(props); 28662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28672676Seschrock "missing volume size")); 28682676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2869789Sahrens } 2870789Sahrens 28712676Seschrock if ((ret = nvlist_lookup_uint64(props, 28722676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 28732676Seschrock &blocksize)) != 0) { 28742676Seschrock if (ret == ENOENT) { 28752676Seschrock blocksize = zfs_prop_default_numeric( 28762676Seschrock ZFS_PROP_VOLBLOCKSIZE); 28772676Seschrock } else { 28782676Seschrock nvlist_free(props); 28792676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28802676Seschrock "missing volume block size")); 28812676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28822676Seschrock } 28832676Seschrock } 28842676Seschrock 28852676Seschrock if (size == 0) { 28862676Seschrock nvlist_free(props); 28872082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28882676Seschrock "volume size cannot be zero")); 28892676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28901133Seschrock } 28911133Seschrock 28921133Seschrock if (size % blocksize != 0) { 28932676Seschrock nvlist_free(props); 28942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28952676Seschrock "volume size must be a multiple of volume block " 28962676Seschrock "size")); 28972676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28981133Seschrock } 2899789Sahrens } 2900789Sahrens 29015094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 29022676Seschrock return (-1); 29032676Seschrock nvlist_free(props); 29042676Seschrock 2905789Sahrens /* create the dataset */ 29064543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2907789Sahrens 29083912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 29092082Seschrock ret = zvol_create_link(hdl, path); 29103912Slling if (ret) { 29113912Slling (void) zfs_standard_error(hdl, errno, 29123912Slling dgettext(TEXT_DOMAIN, 29133912Slling "Volume successfully created, but device links " 29143912Slling "were not created")); 29153912Slling zcmd_free_nvlists(&zc); 29163912Slling return (-1); 29173912Slling } 29183912Slling } 2919789Sahrens 29202676Seschrock zcmd_free_nvlists(&zc); 29212676Seschrock 2922789Sahrens /* check for failure */ 2923789Sahrens if (ret != 0) { 2924789Sahrens char parent[ZFS_MAXNAMELEN]; 2925789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2926789Sahrens 2927789Sahrens switch (errno) { 2928789Sahrens case ENOENT: 29292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29302082Seschrock "no such parent '%s'"), parent); 29312082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2932789Sahrens 2933789Sahrens case EINVAL: 29342082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29353413Smmusante "parent '%s' is not a filesystem"), parent); 29362082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2937789Sahrens 2938789Sahrens case EDOM: 29392082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29402676Seschrock "volume block size must be power of 2 from " 29412676Seschrock "%u to %uk"), 2942789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2943789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 29442082Seschrock 29452676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29462082Seschrock 29474603Sahrens case ENOTSUP: 29484603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29494603Sahrens "pool must be upgraded to set this " 29504603Sahrens "property or value")); 29514603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 29524603Sahrens 2953789Sahrens #ifdef _ILP32 2954789Sahrens case EOVERFLOW: 2955789Sahrens /* 2956789Sahrens * This platform can't address a volume this big. 2957789Sahrens */ 29582082Seschrock if (type == ZFS_TYPE_VOLUME) 29592082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 29602082Seschrock errbuf)); 2961789Sahrens #endif 29622082Seschrock /* FALLTHROUGH */ 2963789Sahrens default: 29642082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2965789Sahrens } 2966789Sahrens } 2967789Sahrens 2968789Sahrens return (0); 2969789Sahrens } 2970789Sahrens 2971789Sahrens /* 2972789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2973789Sahrens * isn't mounted, and that there are no active dependents. 2974789Sahrens */ 2975789Sahrens int 2976789Sahrens zfs_destroy(zfs_handle_t *zhp) 2977789Sahrens { 2978789Sahrens zfs_cmd_t zc = { 0 }; 2979789Sahrens 2980789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2981789Sahrens 29822676Seschrock if (ZFS_IS_VOLUME(zhp)) { 29833126Sahl /* 29844543Smarks * If user doesn't have permissions to unshare volume, then 29854543Smarks * abort the request. This would only happen for a 29864543Smarks * non-privileged user. 29873126Sahl */ 29884543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 29894543Smarks return (-1); 29904543Smarks } 29913126Sahl 29922082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2993789Sahrens return (-1); 2994789Sahrens 2995789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2996789Sahrens } else { 2997789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2998789Sahrens } 2999789Sahrens 30004543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 30013237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 30022082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 30032082Seschrock zhp->zfs_name)); 30042199Sahrens } 3005789Sahrens 3006789Sahrens remove_mountpoint(zhp); 3007789Sahrens 3008789Sahrens return (0); 3009789Sahrens } 3010789Sahrens 30112199Sahrens struct destroydata { 30122199Sahrens char *snapname; 30132199Sahrens boolean_t gotone; 30143265Sahrens boolean_t closezhp; 30152199Sahrens }; 30162199Sahrens 30172199Sahrens static int 30182199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 30192199Sahrens { 30202199Sahrens struct destroydata *dd = arg; 30212199Sahrens zfs_handle_t *szhp; 30222199Sahrens char name[ZFS_MAXNAMELEN]; 30233265Sahrens boolean_t closezhp = dd->closezhp; 30243265Sahrens int rv; 30252199Sahrens 30262676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 30272676Seschrock (void) strlcat(name, "@", sizeof (name)); 30282676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 30292199Sahrens 30302199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 30312199Sahrens if (szhp) { 30322199Sahrens dd->gotone = B_TRUE; 30332199Sahrens zfs_close(szhp); 30342199Sahrens } 30352199Sahrens 30362199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 30372199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 30382199Sahrens /* 30392199Sahrens * NB: this is simply a best-effort. We don't want to 30402199Sahrens * return an error, because then we wouldn't visit all 30412199Sahrens * the volumes. 30422199Sahrens */ 30432199Sahrens } 30442199Sahrens 30453265Sahrens dd->closezhp = B_TRUE; 30463265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 30473265Sahrens if (closezhp) 30483265Sahrens zfs_close(zhp); 30493265Sahrens return (rv); 30502199Sahrens } 30512199Sahrens 30522199Sahrens /* 30532199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 30542199Sahrens */ 30552199Sahrens int 30562199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 30572199Sahrens { 30582199Sahrens zfs_cmd_t zc = { 0 }; 30592199Sahrens int ret; 30602199Sahrens struct destroydata dd = { 0 }; 30612199Sahrens 30622199Sahrens dd.snapname = snapname; 30632199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 30642199Sahrens 30652199Sahrens if (!dd.gotone) { 30663237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 30672199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 30682199Sahrens zhp->zfs_name, snapname)); 30692199Sahrens } 30702199Sahrens 30712199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 30722676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 30732199Sahrens 30744543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 30752199Sahrens if (ret != 0) { 30762199Sahrens char errbuf[1024]; 30772199Sahrens 30782199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30792199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 30802199Sahrens 30812199Sahrens switch (errno) { 30822199Sahrens case EEXIST: 30832199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30842199Sahrens "snapshot is cloned")); 30852199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 30862199Sahrens 30872199Sahrens default: 30882199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 30892199Sahrens errbuf)); 30902199Sahrens } 30912199Sahrens } 30922199Sahrens 30932199Sahrens return (0); 30942199Sahrens } 30952199Sahrens 3096789Sahrens /* 3097789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3098789Sahrens */ 3099789Sahrens int 31002676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3101789Sahrens { 3102789Sahrens zfs_cmd_t zc = { 0 }; 3103789Sahrens char parent[ZFS_MAXNAMELEN]; 3104789Sahrens int ret; 31052082Seschrock char errbuf[1024]; 31062082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 31072676Seschrock zfs_type_t type; 31082676Seschrock uint64_t zoned; 3109789Sahrens 3110789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3111789Sahrens 31122082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31132082Seschrock "cannot create '%s'"), target); 31142082Seschrock 3115789Sahrens /* validate the target name */ 31165326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 31172082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3118789Sahrens 3119789Sahrens /* validate parents exist */ 31204490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3121789Sahrens return (-1); 3122789Sahrens 3123789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3124789Sahrens 3125789Sahrens /* do the clone */ 31262676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3127789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 31282744Snn35248 type = ZFS_TYPE_VOLUME; 31292676Seschrock } else { 3130789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 31312744Snn35248 type = ZFS_TYPE_FILESYSTEM; 31322676Seschrock } 31332676Seschrock 31342676Seschrock if (props) { 31355094Slling if ((props = zfs_validate_properties(hdl, type, props, 31363912Slling zoned, zhp, errbuf)) == NULL) 31372676Seschrock return (-1); 31382676Seschrock 31395094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 31402676Seschrock nvlist_free(props); 31412676Seschrock return (-1); 31422676Seschrock } 31432676Seschrock 31442676Seschrock nvlist_free(props); 31452676Seschrock } 3146789Sahrens 3147789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 31482676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 31494543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3150789Sahrens 31512676Seschrock zcmd_free_nvlists(&zc); 31522676Seschrock 3153789Sahrens if (ret != 0) { 3154789Sahrens switch (errno) { 3155789Sahrens 3156789Sahrens case ENOENT: 3157789Sahrens /* 3158789Sahrens * The parent doesn't exist. We should have caught this 3159789Sahrens * above, but there may a race condition that has since 3160789Sahrens * destroyed the parent. 3161789Sahrens * 3162789Sahrens * At this point, we don't know whether it's the source 3163789Sahrens * that doesn't exist anymore, or whether the target 3164789Sahrens * dataset doesn't exist. 3165789Sahrens */ 31662082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31672082Seschrock "no such parent '%s'"), parent); 31682082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 31692082Seschrock 31702082Seschrock case EXDEV: 31712082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31722082Seschrock "source and target pools differ")); 31732082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 31742082Seschrock errbuf)); 31752082Seschrock 31762082Seschrock default: 31772082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 31782082Seschrock errbuf)); 31792082Seschrock } 31802676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 31812082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 31822082Seschrock } 31832082Seschrock 31842082Seschrock return (ret); 31852082Seschrock } 31862082Seschrock 31872082Seschrock typedef struct promote_data { 31882082Seschrock char cb_mountpoint[MAXPATHLEN]; 31892082Seschrock const char *cb_target; 31902082Seschrock const char *cb_errbuf; 31912082Seschrock uint64_t cb_pivot_txg; 31922082Seschrock } promote_data_t; 31932082Seschrock 31942082Seschrock static int 31952082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 31962082Seschrock { 31972082Seschrock promote_data_t *pd = data; 31982082Seschrock zfs_handle_t *szhp; 31992082Seschrock char snapname[MAXPATHLEN]; 32003265Sahrens int rv = 0; 32012082Seschrock 32022082Seschrock /* We don't care about snapshots after the pivot point */ 32033265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 32043265Sahrens zfs_close(zhp); 32052082Seschrock return (0); 32063265Sahrens } 32072082Seschrock 32082417Sahrens /* Remove the device link if it's a zvol. */ 32092676Seschrock if (ZFS_IS_VOLUME(zhp)) 32102417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 32112082Seschrock 32122082Seschrock /* Check for conflicting names */ 32132676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 32142676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 32152082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 32162082Seschrock if (szhp != NULL) { 32172082Seschrock zfs_close(szhp); 32182082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32192082Seschrock "snapshot name '%s' from origin \n" 32202082Seschrock "conflicts with '%s' from target"), 32212082Seschrock zhp->zfs_name, snapname); 32223265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 32232082Seschrock } 32243265Sahrens zfs_close(zhp); 32253265Sahrens return (rv); 32262082Seschrock } 32272082Seschrock 32282417Sahrens static int 32292417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 32302417Sahrens { 32312417Sahrens promote_data_t *pd = data; 32322417Sahrens 32332417Sahrens /* We don't care about snapshots after the pivot point */ 32343265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 32353265Sahrens /* Create the device link if it's a zvol. */ 32363265Sahrens if (ZFS_IS_VOLUME(zhp)) 32373265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 32383265Sahrens } 32393265Sahrens 32403265Sahrens zfs_close(zhp); 32412417Sahrens return (0); 32422417Sahrens } 32432417Sahrens 32442082Seschrock /* 32452082Seschrock * Promotes the given clone fs to be the clone parent. 32462082Seschrock */ 32472082Seschrock int 32482082Seschrock zfs_promote(zfs_handle_t *zhp) 32492082Seschrock { 32502082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 32512082Seschrock zfs_cmd_t zc = { 0 }; 32522082Seschrock char parent[MAXPATHLEN]; 32532082Seschrock char *cp; 32542082Seschrock int ret; 32552082Seschrock zfs_handle_t *pzhp; 32562082Seschrock promote_data_t pd; 32572082Seschrock char errbuf[1024]; 32582082Seschrock 32592082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32602082Seschrock "cannot promote '%s'"), zhp->zfs_name); 32612082Seschrock 32622082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 32632082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32642082Seschrock "snapshots can not be promoted")); 32652082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 32662082Seschrock } 32672082Seschrock 32685367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 32692082Seschrock if (parent[0] == '\0') { 32702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32712082Seschrock "not a cloned filesystem")); 32722082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 32732082Seschrock } 32742082Seschrock cp = strchr(parent, '@'); 32752082Seschrock *cp = '\0'; 32762082Seschrock 32772082Seschrock /* Walk the snapshots we will be moving */ 32785367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 32792082Seschrock if (pzhp == NULL) 32802082Seschrock return (-1); 32812082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 32822082Seschrock zfs_close(pzhp); 32832082Seschrock pd.cb_target = zhp->zfs_name; 32842082Seschrock pd.cb_errbuf = errbuf; 32855094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 32862082Seschrock if (pzhp == NULL) 32872082Seschrock return (-1); 32882082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 32892082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 32902082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 32912417Sahrens if (ret != 0) { 32922417Sahrens zfs_close(pzhp); 32932082Seschrock return (-1); 32942417Sahrens } 32952082Seschrock 32962082Seschrock /* issue the ioctl */ 32975367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 32982676Seschrock sizeof (zc.zc_value)); 32992082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33004543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 33012082Seschrock 33022082Seschrock if (ret != 0) { 33032417Sahrens int save_errno = errno; 33042417Sahrens 33052417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 33062417Sahrens zfs_close(pzhp); 33072417Sahrens 33082417Sahrens switch (save_errno) { 3309789Sahrens case EEXIST: 3310789Sahrens /* 33112082Seschrock * There is a conflicting snapshot name. We 33122082Seschrock * should have caught this above, but they could 33132082Seschrock * have renamed something in the mean time. 3314789Sahrens */ 33152082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33162082Seschrock "conflicting snapshot name from parent '%s'"), 33172082Seschrock parent); 33182082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3319789Sahrens 3320789Sahrens default: 33212417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3322789Sahrens } 33232417Sahrens } else { 33242417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3325789Sahrens } 3326789Sahrens 33272417Sahrens zfs_close(pzhp); 3328789Sahrens return (ret); 3329789Sahrens } 3330789Sahrens 33314007Smmusante struct createdata { 33324007Smmusante const char *cd_snapname; 33334007Smmusante int cd_ifexists; 33344007Smmusante }; 33354007Smmusante 33362199Sahrens static int 33372199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 33382199Sahrens { 33394007Smmusante struct createdata *cd = arg; 33402676Seschrock int ret; 33412199Sahrens 33422199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33432199Sahrens char name[MAXPATHLEN]; 33442199Sahrens 33452676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 33462676Seschrock (void) strlcat(name, "@", sizeof (name)); 33474007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 33484007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 33494007Smmusante cd->cd_ifexists); 33502199Sahrens /* 33512199Sahrens * NB: this is simply a best-effort. We don't want to 33522199Sahrens * return an error, because then we wouldn't visit all 33532199Sahrens * the volumes. 33542199Sahrens */ 33552199Sahrens } 33562676Seschrock 33574007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 33582676Seschrock 33592676Seschrock zfs_close(zhp); 33602676Seschrock 33612676Seschrock return (ret); 33622199Sahrens } 33632199Sahrens 3364789Sahrens /* 33653504Sahl * Takes a snapshot of the given dataset. 3366789Sahrens */ 3367789Sahrens int 33682199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3369789Sahrens { 3370789Sahrens const char *delim; 3371789Sahrens char *parent; 3372789Sahrens zfs_handle_t *zhp; 3373789Sahrens zfs_cmd_t zc = { 0 }; 3374789Sahrens int ret; 33752082Seschrock char errbuf[1024]; 33762082Seschrock 33772082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33782082Seschrock "cannot snapshot '%s'"), path); 33792082Seschrock 33802082Seschrock /* validate the target name */ 33815326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 33822082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3383789Sahrens 3384789Sahrens /* make sure the parent exists and is of the appropriate type */ 33852199Sahrens delim = strchr(path, '@'); 33862082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 33872082Seschrock return (-1); 3388789Sahrens (void) strncpy(parent, path, delim - path); 3389789Sahrens parent[delim - path] = '\0'; 3390789Sahrens 33912082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3392789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3393789Sahrens free(parent); 3394789Sahrens return (-1); 3395789Sahrens } 3396789Sahrens 33972199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33982676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 33994543Smarks if (ZFS_IS_VOLUME(zhp)) 34004543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 34014543Smarks else 34024543Smarks zc.zc_objset_type = DMU_OST_ZFS; 34032199Sahrens zc.zc_cookie = recursive; 34044543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 34052199Sahrens 34062199Sahrens /* 34072199Sahrens * if it was recursive, the one that actually failed will be in 34082199Sahrens * zc.zc_name. 34092199Sahrens */ 34104543Smarks if (ret != 0) 34114543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34124543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 34134543Smarks 34142199Sahrens if (ret == 0 && recursive) { 34154007Smmusante struct createdata cd; 34164007Smmusante 34174007Smmusante cd.cd_snapname = delim + 1; 34184007Smmusante cd.cd_ifexists = B_FALSE; 34194007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 34202199Sahrens } 3421789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 34222082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 34232199Sahrens if (ret != 0) { 34244543Smarks (void) zfs_standard_error(hdl, errno, 34254543Smarks dgettext(TEXT_DOMAIN, 34264543Smarks "Volume successfully snapshotted, but device links " 34274543Smarks "were not created")); 34284543Smarks free(parent); 34294543Smarks zfs_close(zhp); 34304543Smarks return (-1); 34312199Sahrens } 3432789Sahrens } 3433789Sahrens 34342082Seschrock if (ret != 0) 34352082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3436789Sahrens 3437789Sahrens free(parent); 3438789Sahrens zfs_close(zhp); 3439789Sahrens 3440789Sahrens return (ret); 3441789Sahrens } 3442789Sahrens 3443789Sahrens /* 34441294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 34451294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 34461294Slling * is a dependent and we should just destroy it without checking the transaction 34471294Slling * group. 3448789Sahrens */ 34491294Slling typedef struct rollback_data { 34501294Slling const char *cb_target; /* the snapshot */ 34511294Slling uint64_t cb_create; /* creation time reference */ 34525749Sahrens boolean_t cb_error; 34532082Seschrock boolean_t cb_dependent; 34545749Sahrens boolean_t cb_force; 34551294Slling } rollback_data_t; 34561294Slling 34571294Slling static int 34581294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 34591294Slling { 34601294Slling rollback_data_t *cbp = data; 34611294Slling 34621294Slling if (!cbp->cb_dependent) { 34631294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 34641294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 34651294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 34661294Slling cbp->cb_create) { 34674543Smarks char *logstr; 34681294Slling 34692082Seschrock cbp->cb_dependent = B_TRUE; 34705446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 34715446Sahrens rollback_destroy, cbp); 34722082Seschrock cbp->cb_dependent = B_FALSE; 34731294Slling 34744543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 34754543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 34765446Sahrens cbp->cb_error |= zfs_destroy(zhp); 34774543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 34781294Slling } 34791294Slling } else { 34805749Sahrens /* We must destroy this clone; first unmount it */ 34815749Sahrens prop_changelist_t *clp; 34825749Sahrens 34835749Sahrens clp = changelist_gather(zhp, ZFS_PROP_NAME, 34845749Sahrens cbp->cb_force ? MS_FORCE: 0); 34855749Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 34865749Sahrens cbp->cb_error = B_TRUE; 34875749Sahrens zfs_close(zhp); 34885749Sahrens return (0); 34895749Sahrens } 34905749Sahrens if (zfs_destroy(zhp) != 0) 34915749Sahrens cbp->cb_error = B_TRUE; 34925749Sahrens else 34935749Sahrens changelist_remove(clp, zhp->zfs_name); 34945751Sahrens (void) changelist_postfix(clp); 34955749Sahrens changelist_free(clp); 34961294Slling } 34971294Slling 34981294Slling zfs_close(zhp); 34991294Slling return (0); 35001294Slling } 35011294Slling 35021294Slling /* 35035446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 35045446Sahrens * data changes since then and making it the active dataset. 35055446Sahrens * 35065446Sahrens * Any snapshots more recent than the target are destroyed, along with 35075446Sahrens * their dependents. 35081294Slling */ 35095446Sahrens int 35105749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3511789Sahrens { 35125446Sahrens rollback_data_t cb = { 0 }; 35135446Sahrens int err; 3514789Sahrens zfs_cmd_t zc = { 0 }; 35155713Srm160521 boolean_t restore_resv = 0; 35165713Srm160521 uint64_t old_volsize, new_volsize; 35175713Srm160521 zfs_prop_t resv_prop; 3518789Sahrens 3519789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3520789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3521789Sahrens 35225446Sahrens /* 35235446Sahrens * Destroy all recent snapshots and its dependends. 35245446Sahrens */ 35255749Sahrens cb.cb_force = force; 35265446Sahrens cb.cb_target = snap->zfs_name; 35275446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 35285446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 35295446Sahrens 35305749Sahrens if (cb.cb_error) 35315749Sahrens return (-1); 35325446Sahrens 35335446Sahrens /* 35345446Sahrens * Now that we have verified that the snapshot is the latest, 35355446Sahrens * rollback to the given snapshot. 35365446Sahrens */ 35375446Sahrens 35385713Srm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 35395713Srm160521 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 35405713Srm160521 return (-1); 35415713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 35425713Srm160521 return (-1); 35435713Srm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 35445713Srm160521 restore_resv = 35455713Srm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 35465713Srm160521 } 3547789Sahrens 3548789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3549789Sahrens 35502676Seschrock if (ZFS_IS_VOLUME(zhp)) 3551789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3552789Sahrens else 3553789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3554789Sahrens 3555789Sahrens /* 35565446Sahrens * We rely on zfs_iter_children() to verify that there are no 35575446Sahrens * newer snapshots for the given dataset. Therefore, we can 35585446Sahrens * simply pass the name on to the ioctl() call. There is still 35595446Sahrens * an unlikely race condition where the user has taken a 35605446Sahrens * snapshot since we verified that this was the most recent. 35615713Srm160521 * 3562789Sahrens */ 35635446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 35643237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 35652082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 35662082Seschrock zhp->zfs_name); 35675717Srm160521 return (err); 35685717Srm160521 } 35695713Srm160521 35705713Srm160521 /* 35715713Srm160521 * For volumes, if the pre-rollback volsize matched the pre- 35725713Srm160521 * rollback reservation and the volsize has changed then set 35735713Srm160521 * the reservation property to the post-rollback volsize. 35745713Srm160521 * Make a new handle since the rollback closed the dataset. 35755713Srm160521 */ 35765717Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 35775717Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 35785717Srm160521 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) { 35795717Srm160521 zfs_close(zhp); 35805713Srm160521 return (err); 35815717Srm160521 } 35825713Srm160521 if (restore_resv) { 35835713Srm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 35845713Srm160521 if (old_volsize != new_volsize) 35855717Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 35865717Srm160521 new_volsize); 35875713Srm160521 } 35885713Srm160521 zfs_close(zhp); 3589789Sahrens } 35905446Sahrens return (err); 35911294Slling } 35921294Slling 35931294Slling /* 3594789Sahrens * Iterate over all dependents for a given dataset. This includes both 3595789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3596789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3597789Sahrens * libzfs_graph.c. 3598789Sahrens */ 3599789Sahrens int 36002474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 36012474Seschrock zfs_iter_f func, void *data) 3602789Sahrens { 3603789Sahrens char **dependents; 3604789Sahrens size_t count; 3605789Sahrens int i; 3606789Sahrens zfs_handle_t *child; 3607789Sahrens int ret = 0; 3608789Sahrens 36092474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 36102474Seschrock &dependents, &count) != 0) 36112474Seschrock return (-1); 36122474Seschrock 3613789Sahrens for (i = 0; i < count; i++) { 36142082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 36152082Seschrock dependents[i])) == NULL) 3616789Sahrens continue; 3617789Sahrens 3618789Sahrens if ((ret = func(child, data)) != 0) 3619789Sahrens break; 3620789Sahrens } 3621789Sahrens 3622789Sahrens for (i = 0; i < count; i++) 3623789Sahrens free(dependents[i]); 3624789Sahrens free(dependents); 3625789Sahrens 3626789Sahrens return (ret); 3627789Sahrens } 3628789Sahrens 3629789Sahrens /* 3630789Sahrens * Renames the given dataset. 3631789Sahrens */ 3632789Sahrens int 36334490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3634789Sahrens { 3635789Sahrens int ret; 3636789Sahrens zfs_cmd_t zc = { 0 }; 3637789Sahrens char *delim; 36384007Smmusante prop_changelist_t *cl = NULL; 36394007Smmusante zfs_handle_t *zhrp = NULL; 36404007Smmusante char *parentname = NULL; 3641789Sahrens char parent[ZFS_MAXNAMELEN]; 36422082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 36432082Seschrock char errbuf[1024]; 3644789Sahrens 3645789Sahrens /* if we have the same exact name, just return success */ 3646789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3647789Sahrens return (0); 3648789Sahrens 36492082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36502082Seschrock "cannot rename to '%s'"), target); 36512082Seschrock 3652789Sahrens /* 3653789Sahrens * Make sure the target name is valid 3654789Sahrens */ 3655789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 36562665Snd150628 if ((strchr(target, '@') == NULL) || 36572665Snd150628 *target == '@') { 36582665Snd150628 /* 36592665Snd150628 * Snapshot target name is abbreviated, 36602665Snd150628 * reconstruct full dataset name 36612665Snd150628 */ 36622665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 36632665Snd150628 sizeof (parent)); 36642665Snd150628 delim = strchr(parent, '@'); 36652665Snd150628 if (strchr(target, '@') == NULL) 36662665Snd150628 *(++delim) = '\0'; 36672665Snd150628 else 36682665Snd150628 *delim = '\0'; 36692665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 36702665Snd150628 target = parent; 36712665Snd150628 } else { 36722665Snd150628 /* 36732665Snd150628 * Make sure we're renaming within the same dataset. 36742665Snd150628 */ 36752665Snd150628 delim = strchr(target, '@'); 36762665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 36772665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 36782665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36792665Snd150628 "snapshots must be part of same " 36802665Snd150628 "dataset")); 36812665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 36823912Slling errbuf)); 36832665Snd150628 } 3684789Sahrens } 36855326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 36862665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3687789Sahrens } else { 36884007Smmusante if (recursive) { 36894007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36904007Smmusante "recursive rename must be a snapshot")); 36914007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 36924007Smmusante } 36934007Smmusante 36945326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 36952665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 36962676Seschrock uint64_t unused; 36972676Seschrock 3698789Sahrens /* validate parents */ 36994490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3700789Sahrens return (-1); 3701789Sahrens 3702789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3703789Sahrens 3704789Sahrens /* make sure we're in the same pool */ 3705789Sahrens verify((delim = strchr(target, '/')) != NULL); 3706789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3707789Sahrens zhp->zfs_name[delim - target] != '/') { 37082082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37092082Seschrock "datasets must be within same pool")); 37102082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3711789Sahrens } 37122440Snd150628 37132440Snd150628 /* new name cannot be a child of the current dataset name */ 37142440Snd150628 if (strncmp(parent, zhp->zfs_name, 37153912Slling strlen(zhp->zfs_name)) == 0) { 37162440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37172440Snd150628 "New dataset name cannot be a descendent of " 37182440Snd150628 "current dataset name")); 37192440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37202440Snd150628 } 3721789Sahrens } 3722789Sahrens 37232082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 37242082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 37252082Seschrock 3726789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3727789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 37282082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37292082Seschrock "dataset is used in a non-global zone")); 37302082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3731789Sahrens } 3732789Sahrens 37334007Smmusante if (recursive) { 37344007Smmusante struct destroydata dd; 37354007Smmusante 37364183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 37374183Smmusante if (parentname == NULL) { 37384183Smmusante ret = -1; 37394183Smmusante goto error; 37404183Smmusante } 37414007Smmusante delim = strchr(parentname, '@'); 37424007Smmusante *delim = '\0'; 37435094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 37444007Smmusante if (zhrp == NULL) { 37454183Smmusante ret = -1; 37464183Smmusante goto error; 37474007Smmusante } 37484007Smmusante 37494007Smmusante dd.snapname = delim + 1; 37504007Smmusante dd.gotone = B_FALSE; 37514183Smmusante dd.closezhp = B_TRUE; 37524007Smmusante 37534007Smmusante /* We remove any zvol links prior to renaming them */ 37544007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 37554007Smmusante if (ret) { 37564007Smmusante goto error; 37574007Smmusante } 37584007Smmusante } else { 37594007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 37604007Smmusante return (-1); 37614007Smmusante 37624007Smmusante if (changelist_haszonedchild(cl)) { 37634007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37644007Smmusante "child dataset with inherited mountpoint is used " 37654007Smmusante "in a non-global zone")); 37664007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 37674007Smmusante goto error; 37684007Smmusante } 37694007Smmusante 37704007Smmusante if ((ret = changelist_prefix(cl)) != 0) 37714007Smmusante goto error; 3772789Sahrens } 3773789Sahrens 37742676Seschrock if (ZFS_IS_VOLUME(zhp)) 3775789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3776789Sahrens else 3777789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3778789Sahrens 37792665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 37802676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 37812665Snd150628 37824007Smmusante zc.zc_cookie = recursive; 37834007Smmusante 37844543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 37854007Smmusante /* 37864007Smmusante * if it was recursive, the one that actually failed will 37874007Smmusante * be in zc.zc_name 37884007Smmusante */ 37894007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37905367Sahrens "cannot rename '%s'"), zc.zc_name); 37914007Smmusante 37924007Smmusante if (recursive && errno == EEXIST) { 37934007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37944007Smmusante "a child dataset already has a snapshot " 37954007Smmusante "with the new name")); 37964801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 37974007Smmusante } else { 37984007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 37994007Smmusante } 3800789Sahrens 3801789Sahrens /* 3802789Sahrens * On failure, we still want to remount any filesystems that 3803789Sahrens * were previously mounted, so we don't alter the system state. 3804789Sahrens */ 38054007Smmusante if (recursive) { 38064007Smmusante struct createdata cd; 38074007Smmusante 38084007Smmusante /* only create links for datasets that had existed */ 38094007Smmusante cd.cd_snapname = delim + 1; 38104007Smmusante cd.cd_ifexists = B_TRUE; 38114007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 38124007Smmusante &cd); 38134007Smmusante } else { 38144007Smmusante (void) changelist_postfix(cl); 38154007Smmusante } 3816789Sahrens } else { 38174007Smmusante if (recursive) { 38184007Smmusante struct createdata cd; 38194007Smmusante 38204007Smmusante /* only create links for datasets that had existed */ 38214007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 38224007Smmusante cd.cd_ifexists = B_TRUE; 38234007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 38244007Smmusante &cd); 38254007Smmusante } else { 38264007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 38274007Smmusante ret = changelist_postfix(cl); 38284007Smmusante } 3829789Sahrens } 3830789Sahrens 3831789Sahrens error: 38324007Smmusante if (parentname) { 38334007Smmusante free(parentname); 38344007Smmusante } 38354007Smmusante if (zhrp) { 38364007Smmusante zfs_close(zhrp); 38374007Smmusante } 38384007Smmusante if (cl) { 38394007Smmusante changelist_free(cl); 38404007Smmusante } 3841789Sahrens return (ret); 3842789Sahrens } 3843789Sahrens 3844789Sahrens /* 3845789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3846789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3847789Sahrens */ 3848789Sahrens int 38492082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3850789Sahrens { 38514007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 38524007Smmusante } 38534007Smmusante 38544007Smmusante static int 38554007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 38564007Smmusante { 3857789Sahrens zfs_cmd_t zc = { 0 }; 38582082Seschrock di_devlink_handle_t dhdl; 38594543Smarks priv_set_t *priv_effective; 38604543Smarks int privileged; 3861789Sahrens 3862789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3863789Sahrens 3864789Sahrens /* 3865789Sahrens * Issue the appropriate ioctl. 3866789Sahrens */ 38672082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3868789Sahrens switch (errno) { 3869789Sahrens case EEXIST: 3870789Sahrens /* 3871789Sahrens * Silently ignore the case where the link already 3872789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3873789Sahrens * times without errors. 3874789Sahrens */ 3875789Sahrens return (0); 3876789Sahrens 38774007Smmusante case ENOENT: 38784007Smmusante /* 38794007Smmusante * Dataset does not exist in the kernel. If we 38804007Smmusante * don't care (see zfs_rename), then ignore the 38814007Smmusante * error quietly. 38824007Smmusante */ 38834007Smmusante if (ifexists) { 38844007Smmusante return (0); 38854007Smmusante } 38864007Smmusante 38874007Smmusante /* FALLTHROUGH */ 38884007Smmusante 3889789Sahrens default: 38903237Slling return (zfs_standard_error_fmt(hdl, errno, 38912082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 38922082Seschrock "for '%s'"), dataset)); 3893789Sahrens } 3894789Sahrens } 3895789Sahrens 3896789Sahrens /* 38974543Smarks * If privileged call devfsadm and wait for the links to 38984543Smarks * magically appear. 38994543Smarks * Otherwise, print out an informational message. 3900789Sahrens */ 39014543Smarks 39024543Smarks priv_effective = priv_allocset(); 39034543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 39044543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 39054543Smarks priv_freeset(priv_effective); 39064543Smarks 39074543Smarks if (privileged) { 39084543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 39094543Smarks DI_MAKE_LINK)) == NULL) { 39104543Smarks zfs_error_aux(hdl, strerror(errno)); 39114543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 39124543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 39134543Smarks "for '%s'"), dataset); 39144543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 39154543Smarks return (-1); 39164543Smarks } else { 39174543Smarks (void) di_devlink_fini(&dhdl); 39184543Smarks } 3919789Sahrens } else { 39204543Smarks char pathname[MAXPATHLEN]; 39214543Smarks struct stat64 statbuf; 39224543Smarks int i; 39234543Smarks 39244543Smarks #define MAX_WAIT 10 39254543Smarks 39264543Smarks /* 39274543Smarks * This is the poor mans way of waiting for the link 39284543Smarks * to show up. If after 10 seconds we still don't 39294543Smarks * have it, then print out a message. 39304543Smarks */ 39314543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 39324543Smarks dataset); 39334543Smarks 39344543Smarks for (i = 0; i != MAX_WAIT; i++) { 39354543Smarks if (stat64(pathname, &statbuf) == 0) 39364543Smarks break; 39374543Smarks (void) sleep(1); 39384543Smarks } 39394543Smarks if (i == MAX_WAIT) 39404543Smarks (void) printf(gettext("%s may not be immediately " 39414543Smarks "available\n"), pathname); 3942789Sahrens } 3943789Sahrens 3944789Sahrens return (0); 3945789Sahrens } 3946789Sahrens 3947789Sahrens /* 3948789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 3949789Sahrens */ 3950789Sahrens int 39512082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3952789Sahrens { 3953789Sahrens zfs_cmd_t zc = { 0 }; 3954789Sahrens 3955789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3956789Sahrens 39572082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3958789Sahrens switch (errno) { 3959789Sahrens case ENXIO: 3960789Sahrens /* 3961789Sahrens * Silently ignore the case where the link no longer 3962789Sahrens * exists, so that 'zfs volfini' can be run multiple 3963789Sahrens * times without errors. 3964789Sahrens */ 3965789Sahrens return (0); 3966789Sahrens 3967789Sahrens default: 39683237Slling return (zfs_standard_error_fmt(hdl, errno, 39692082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 39702082Seschrock "links for '%s'"), dataset)); 3971789Sahrens } 3972789Sahrens } 3973789Sahrens 3974789Sahrens return (0); 3975789Sahrens } 39762676Seschrock 39772676Seschrock nvlist_t * 39782676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 39792676Seschrock { 39802676Seschrock return (zhp->zfs_user_props); 39812676Seschrock } 39822676Seschrock 39832676Seschrock /* 39843912Slling * This function is used by 'zfs list' to determine the exact set of columns to 39853912Slling * display, and their maximum widths. This does two main things: 39863912Slling * 39873912Slling * - If this is a list of all properties, then expand the list to include 39883912Slling * all native properties, and set a flag so that for each dataset we look 39893912Slling * for new unique user properties and add them to the list. 39903912Slling * 39913912Slling * - For non fixed-width properties, keep track of the maximum width seen 39923912Slling * so that we can size the column appropriately. 39933912Slling */ 39943912Slling int 39955094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 39963912Slling { 39973912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 39985094Slling zprop_list_t *entry; 39995094Slling zprop_list_t **last, **start; 40003912Slling nvlist_t *userprops, *propval; 40013912Slling nvpair_t *elem; 40023912Slling char *strval; 40033912Slling char buf[ZFS_MAXPROPLEN]; 40043912Slling 40055094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 40063912Slling return (-1); 40072676Seschrock 40082676Seschrock userprops = zfs_get_user_props(zhp); 40092676Seschrock 40102676Seschrock entry = *plp; 40112676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 40122676Seschrock /* 40132676Seschrock * Go through and add any user properties as necessary. We 40142676Seschrock * start by incrementing our list pointer to the first 40152676Seschrock * non-native property. 40162676Seschrock */ 40172676Seschrock start = plp; 40182676Seschrock while (*start != NULL) { 40195094Slling if ((*start)->pl_prop == ZPROP_INVAL) 40202676Seschrock break; 40212676Seschrock start = &(*start)->pl_next; 40222676Seschrock } 40232676Seschrock 40242676Seschrock elem = NULL; 40252676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 40262676Seschrock /* 40272676Seschrock * See if we've already found this property in our list. 40282676Seschrock */ 40292676Seschrock for (last = start; *last != NULL; 40302676Seschrock last = &(*last)->pl_next) { 40312676Seschrock if (strcmp((*last)->pl_user_prop, 40322676Seschrock nvpair_name(elem)) == 0) 40332676Seschrock break; 40342676Seschrock } 40352676Seschrock 40362676Seschrock if (*last == NULL) { 40372676Seschrock if ((entry = zfs_alloc(hdl, 40385094Slling sizeof (zprop_list_t))) == NULL || 40392676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 40402676Seschrock nvpair_name(elem)))) == NULL) { 40412676Seschrock free(entry); 40422676Seschrock return (-1); 40432676Seschrock } 40442676Seschrock 40455094Slling entry->pl_prop = ZPROP_INVAL; 40462676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 40472676Seschrock entry->pl_all = B_TRUE; 40482676Seschrock *last = entry; 40492676Seschrock } 40502676Seschrock } 40512676Seschrock } 40522676Seschrock 40532676Seschrock /* 40542676Seschrock * Now go through and check the width of any non-fixed columns 40552676Seschrock */ 40562676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 40572676Seschrock if (entry->pl_fixed) 40582676Seschrock continue; 40592676Seschrock 40605094Slling if (entry->pl_prop != ZPROP_INVAL) { 40612676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 40622676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 40632676Seschrock if (strlen(buf) > entry->pl_width) 40642676Seschrock entry->pl_width = strlen(buf); 40652676Seschrock } 40662676Seschrock } else if (nvlist_lookup_nvlist(userprops, 40672676Seschrock entry->pl_user_prop, &propval) == 0) { 40682676Seschrock verify(nvlist_lookup_string(propval, 40695094Slling ZPROP_VALUE, &strval) == 0); 40702676Seschrock if (strlen(strval) > entry->pl_width) 40712676Seschrock entry->pl_width = strlen(strval); 40722676Seschrock } 40732676Seschrock } 40742676Seschrock 40752676Seschrock return (0); 40762676Seschrock } 40774543Smarks 40784543Smarks int 40794543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 40804543Smarks { 40814543Smarks zfs_cmd_t zc = { 0 }; 40824543Smarks nvlist_t *nvp; 40834543Smarks gid_t gid; 40844543Smarks uid_t uid; 40854543Smarks const gid_t *groups; 40864543Smarks int group_cnt; 40874543Smarks int error; 40884543Smarks 40894543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 40904543Smarks return (no_memory(hdl)); 40914543Smarks 40924543Smarks uid = ucred_geteuid(cred); 40934543Smarks gid = ucred_getegid(cred); 40944543Smarks group_cnt = ucred_getgroups(cred, &groups); 40954543Smarks 40964543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 40974543Smarks return (1); 40984543Smarks 40994543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 41004543Smarks nvlist_free(nvp); 41014543Smarks return (1); 41024543Smarks } 41034543Smarks 41044543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 41054543Smarks nvlist_free(nvp); 41064543Smarks return (1); 41074543Smarks } 41084543Smarks 41094543Smarks if (nvlist_add_uint32_array(nvp, 41104543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 41114543Smarks nvlist_free(nvp); 41124543Smarks return (1); 41134543Smarks } 41144543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 41154543Smarks 41165094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 41174543Smarks return (-1); 41184543Smarks 41194543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 41204543Smarks nvlist_free(nvp); 41214543Smarks return (error); 41224543Smarks } 41234543Smarks 41244543Smarks int 41254543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 41265331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 41274543Smarks { 41284543Smarks zfs_cmd_t zc = { 0 }; 41294543Smarks int error; 41304543Smarks 41314543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 41324543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 41334543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 41344543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 41355331Samw zc.zc_share.z_sharetype = operation; 41364543Smarks zc.zc_share.z_sharemax = sharemax; 41374543Smarks 41384543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 41394543Smarks return (error); 41404543Smarks } 4141