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 { 219*6423Sgw25295 if (type == ZFS_TYPE_POOL) 220*6423Sgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 2215326Sek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 222789Sahrens } 223789Sahrens 224789Sahrens /* 2252676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2262676Seschrock * properties into a separate nvlist. 2272676Seschrock */ 2284217Seschrock static nvlist_t * 2294217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2302676Seschrock { 2312676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2322676Seschrock nvpair_t *elem; 2332676Seschrock nvlist_t *propval; 2344217Seschrock nvlist_t *nvl; 2354217Seschrock 2364217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2374217Seschrock (void) no_memory(hdl); 2384217Seschrock return (NULL); 2394217Seschrock } 2402676Seschrock 2412676Seschrock elem = NULL; 2424217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2432676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2442676Seschrock continue; 2452676Seschrock 2462676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2474217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2484217Seschrock nvlist_free(nvl); 2494217Seschrock (void) no_memory(hdl); 2504217Seschrock return (NULL); 2514217Seschrock } 2522676Seschrock } 2532676Seschrock 2544217Seschrock return (nvl); 2552676Seschrock } 2562676Seschrock 2572676Seschrock /* 258789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 259789Sahrens */ 260789Sahrens static int 261789Sahrens get_stats(zfs_handle_t *zhp) 262789Sahrens { 263789Sahrens zfs_cmd_t zc = { 0 }; 2642676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2654217Seschrock nvlist_t *allprops, *userprops; 266789Sahrens 267789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 268789Sahrens 2692676Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 2702082Seschrock return (-1); 2711356Seschrock 2722082Seschrock while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 2731356Seschrock if (errno == ENOMEM) { 2742676Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2752676Seschrock zcmd_free_nvlists(&zc); 2762082Seschrock return (-1); 2772676Seschrock } 2781356Seschrock } else { 2792676Seschrock zcmd_free_nvlists(&zc); 2801356Seschrock return (-1); 2811356Seschrock } 2821356Seschrock } 283789Sahrens 2842885Sahrens zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 285789Sahrens 2862676Seschrock (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 2871544Seschrock 2884217Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 2892676Seschrock zcmd_free_nvlists(&zc); 2902082Seschrock return (-1); 2912082Seschrock } 292789Sahrens 2932676Seschrock zcmd_free_nvlists(&zc); 2942676Seschrock 2954217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 2964217Seschrock nvlist_free(allprops); 2972676Seschrock return (-1); 2984217Seschrock } 2994217Seschrock 3004217Seschrock nvlist_free(zhp->zfs_props); 3014217Seschrock nvlist_free(zhp->zfs_user_props); 3024217Seschrock 3034217Seschrock zhp->zfs_props = allprops; 3044217Seschrock zhp->zfs_user_props = userprops; 3052082Seschrock 306789Sahrens return (0); 307789Sahrens } 308789Sahrens 309789Sahrens /* 310789Sahrens * Refresh the properties currently stored in the handle. 311789Sahrens */ 312789Sahrens void 313789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 314789Sahrens { 315789Sahrens (void) get_stats(zhp); 316789Sahrens } 317789Sahrens 318789Sahrens /* 319789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 320789Sahrens * zfs_iter_* to create child handles on the fly. 321789Sahrens */ 322789Sahrens zfs_handle_t * 3232082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path) 324789Sahrens { 3252082Seschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 3264543Smarks char *logstr; 3272082Seschrock 3282082Seschrock if (zhp == NULL) 3292082Seschrock return (NULL); 3302082Seschrock 3312082Seschrock zhp->zfs_hdl = hdl; 332789Sahrens 3334543Smarks /* 3344543Smarks * Preserve history log string. 3354543Smarks * any changes performed here will be 3364543Smarks * logged as an internal event. 3374543Smarks */ 3384543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 3394543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 3401758Sahrens top: 341789Sahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 342789Sahrens 343789Sahrens if (get_stats(zhp) != 0) { 3444543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 345789Sahrens free(zhp); 346789Sahrens return (NULL); 347789Sahrens } 348789Sahrens 3491758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 3501758Sahrens zfs_cmd_t zc = { 0 }; 3511758Sahrens 3521758Sahrens /* 3531758Sahrens * If it is dds_inconsistent, then we've caught it in 3541758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 3551758Sahrens * it is inconsistent from the ZPL's point of view, so 3561758Sahrens * can't be mounted. However, it could also be that we 3571758Sahrens * have crashed in the middle of one of those 3581758Sahrens * operations, in which case we need to get rid of the 3591758Sahrens * inconsistent state. We do that by either rolling 3601758Sahrens * back to the previous snapshot (which will fail if 3611758Sahrens * there is none), or destroying the filesystem. Note 3621758Sahrens * that if we are still in the middle of an active 3631758Sahrens * 'receive' or 'destroy', then the rollback and destroy 3641758Sahrens * will fail with EBUSY and we will drive on as usual. 3651758Sahrens */ 3661758Sahrens 3671758Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3681758Sahrens 3692885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 3702082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 3711758Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3721758Sahrens } else { 3731758Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3741758Sahrens } 3751758Sahrens 3761758Sahrens /* 3775331Samw * If we can successfully destroy it, pretend that it 3781758Sahrens * never existed. 3791758Sahrens */ 3802082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 3814543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 3821758Sahrens free(zhp); 3831758Sahrens errno = ENOENT; 3841758Sahrens return (NULL); 3851758Sahrens } 3865367Sahrens /* If we can successfully roll it back, reget the stats */ 3875367Sahrens if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 3885367Sahrens goto top; 3891758Sahrens } 3901758Sahrens 391789Sahrens /* 392789Sahrens * We've managed to open the dataset and gather statistics. Determine 393789Sahrens * the high-level type. 394789Sahrens */ 3952885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 3962885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 3972885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 3982885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 3992885Sahrens else 4002885Sahrens abort(); 4012885Sahrens 402789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 403789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 404789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 405789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 406789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 407789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 408789Sahrens else 4092082Seschrock abort(); /* we should never see any other types */ 410789Sahrens 4114543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 412789Sahrens return (zhp); 413789Sahrens } 414789Sahrens 415789Sahrens /* 416789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 417789Sahrens * argument is a mask of acceptable types. The function will print an 418789Sahrens * appropriate error message and return NULL if it can't be opened. 419789Sahrens */ 420789Sahrens zfs_handle_t * 4212082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 422789Sahrens { 423789Sahrens zfs_handle_t *zhp; 4242082Seschrock char errbuf[1024]; 4252082Seschrock 4262082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 4272082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 428789Sahrens 429789Sahrens /* 4302082Seschrock * Validate the name before we even try to open it. 431789Sahrens */ 4325326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 4332082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4342082Seschrock "invalid dataset name")); 4352082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 436789Sahrens return (NULL); 437789Sahrens } 438789Sahrens 439789Sahrens /* 440789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 441789Sahrens */ 442789Sahrens errno = 0; 4432082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 4443237Slling (void) zfs_standard_error(hdl, errno, errbuf); 445789Sahrens return (NULL); 446789Sahrens } 447789Sahrens 448789Sahrens if (!(types & zhp->zfs_type)) { 4492082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 4502142Seschrock zfs_close(zhp); 451789Sahrens return (NULL); 452789Sahrens } 453789Sahrens 454789Sahrens return (zhp); 455789Sahrens } 456789Sahrens 457789Sahrens /* 458789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 459789Sahrens */ 460789Sahrens void 461789Sahrens zfs_close(zfs_handle_t *zhp) 462789Sahrens { 463789Sahrens if (zhp->zfs_mntopts) 464789Sahrens free(zhp->zfs_mntopts); 4652676Seschrock nvlist_free(zhp->zfs_props); 4662676Seschrock nvlist_free(zhp->zfs_user_props); 467789Sahrens free(zhp); 468789Sahrens } 469789Sahrens 4705713Srm160521 int 4715713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 4725713Srm160521 { 4735713Srm160521 char *pool_name; 4745713Srm160521 zpool_handle_t *zpool_handle; 4755713Srm160521 char *p; 4765713Srm160521 4775713Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, MAXPATHLEN); 4785713Srm160521 if (zfs_prop_get(zhp, ZFS_PROP_NAME, pool_name, 4795713Srm160521 MAXPATHLEN, NULL, NULL, 0, B_FALSE) != 0) { 4805713Srm160521 free(pool_name); 4815713Srm160521 return (-1); 4825713Srm160521 } 4835713Srm160521 4845713Srm160521 if (p = strchr(pool_name, '/')) 4855713Srm160521 *p = '\0'; 4865713Srm160521 zpool_handle = zpool_open(zhp->zfs_hdl, pool_name); 4875713Srm160521 free(pool_name); 4885713Srm160521 if (zpool_handle == NULL) 4895713Srm160521 return (-1); 4905713Srm160521 4915713Srm160521 *spa_version = zpool_get_prop_int(zpool_handle, 4925713Srm160521 ZPOOL_PROP_VERSION, NULL); 4935713Srm160521 zpool_close(zpool_handle); 4945713Srm160521 return (0); 4955713Srm160521 } 4965713Srm160521 4975713Srm160521 /* 4985713Srm160521 * The choice of reservation property depends on the SPA version. 4995713Srm160521 */ 5005713Srm160521 static int 5015713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 5025713Srm160521 { 5035713Srm160521 int spa_version; 5045713Srm160521 5055713Srm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 5065713Srm160521 return (-1); 5075713Srm160521 5085713Srm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 5095713Srm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 5105713Srm160521 else 5115713Srm160521 *resv_prop = ZFS_PROP_RESERVATION; 5125713Srm160521 5135713Srm160521 return (0); 5145713Srm160521 } 5155713Srm160521 5163912Slling /* 5172676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 5182676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 5192676Seschrock * strings. 520789Sahrens */ 5215094Slling static nvlist_t * 5225094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 5235094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 524789Sahrens { 5252676Seschrock nvpair_t *elem; 5262676Seschrock uint64_t intval; 5272676Seschrock char *strval; 5285094Slling zfs_prop_t prop; 5292676Seschrock nvlist_t *ret; 5305331Samw int chosen_normal = -1; 5315331Samw int chosen_utf = -1; 5322676Seschrock 5332676Seschrock if (type == ZFS_TYPE_SNAPSHOT) { 5342676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5353413Smmusante "snapshot properties cannot be modified")); 5362676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 5375094Slling return (NULL); 5385094Slling } 5395094Slling 5405094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 5415094Slling (void) no_memory(hdl); 5425094Slling return (NULL); 543789Sahrens } 544789Sahrens 5452676Seschrock elem = NULL; 5462676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 5475094Slling const char *propname = nvpair_name(elem); 5482676Seschrock 5492676Seschrock /* 5502676Seschrock * Make sure this property is valid and applies to this type. 5512676Seschrock */ 5525094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 5535094Slling if (!zfs_prop_user(propname)) { 5542676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5555094Slling "invalid property '%s'"), propname); 5565094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5575094Slling goto error; 5585094Slling } 5595094Slling 5605094Slling /* 5615094Slling * If this is a user property, make sure it's a 5625094Slling * string, and that it's less than ZAP_MAXNAMELEN. 5635094Slling */ 5645094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 5655094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5665094Slling "'%s' must be a string"), propname); 5675094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5685094Slling goto error; 5695094Slling } 5705094Slling 5715094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 5725094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5735094Slling "property name '%s' is too long"), 5742676Seschrock propname); 5752676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5762676Seschrock goto error; 5772676Seschrock } 5782676Seschrock 5792676Seschrock (void) nvpair_value_string(elem, &strval); 5802676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 5812676Seschrock (void) no_memory(hdl); 5822676Seschrock goto error; 5832676Seschrock } 5842676Seschrock continue; 585789Sahrens } 5862676Seschrock 5872676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 5882676Seschrock zfs_error_aux(hdl, 5892676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 5902676Seschrock "apply to datasets of this type"), propname); 5912676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 5922676Seschrock goto error; 5932676Seschrock } 5942676Seschrock 5952676Seschrock if (zfs_prop_readonly(prop) && 5965331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 5972676Seschrock zfs_error_aux(hdl, 5982676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 5992676Seschrock propname); 6002676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 6012676Seschrock goto error; 6022676Seschrock } 6032676Seschrock 6045094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 6055094Slling &strval, &intval, errbuf) != 0) 6062676Seschrock goto error; 6072676Seschrock 6082676Seschrock /* 6092676Seschrock * Perform some additional checks for specific properties. 6102676Seschrock */ 6112676Seschrock switch (prop) { 6124577Sahrens case ZFS_PROP_VERSION: 6134577Sahrens { 6144577Sahrens int version; 6154577Sahrens 6164577Sahrens if (zhp == NULL) 6174577Sahrens break; 6184577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 6194577Sahrens if (intval < version) { 6204577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6214577Sahrens "Can not downgrade; already at version %u"), 6224577Sahrens version); 6234577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6244577Sahrens goto error; 6254577Sahrens } 6264577Sahrens break; 6274577Sahrens } 6284577Sahrens 6292676Seschrock case ZFS_PROP_RECORDSIZE: 6302676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 6312676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 6322676Seschrock if (intval < SPA_MINBLOCKSIZE || 6332676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 6342082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6352676Seschrock "'%s' must be power of 2 from %u " 6362676Seschrock "to %uk"), propname, 6372676Seschrock (uint_t)SPA_MINBLOCKSIZE, 6382676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 6392676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6402676Seschrock goto error; 641789Sahrens } 642789Sahrens break; 643789Sahrens 6443126Sahl case ZFS_PROP_SHAREISCSI: 6453126Sahl if (strcmp(strval, "off") != 0 && 6463126Sahl strcmp(strval, "on") != 0 && 6473126Sahl strcmp(strval, "type=disk") != 0) { 6483126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6493126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 6503126Sahl propname); 6513126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6523126Sahl goto error; 6533126Sahl } 6543126Sahl 6553126Sahl break; 6563126Sahl 6572676Seschrock case ZFS_PROP_MOUNTPOINT: 6584778Srm160521 { 6594778Srm160521 namecheck_err_t why; 6604778Srm160521 6612676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 6622676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 6632676Seschrock break; 6642676Seschrock 6654778Srm160521 if (mountpoint_namecheck(strval, &why)) { 6664778Srm160521 switch (why) { 6674778Srm160521 case NAME_ERR_LEADING_SLASH: 6684778Srm160521 zfs_error_aux(hdl, 6694778Srm160521 dgettext(TEXT_DOMAIN, 6704778Srm160521 "'%s' must be an absolute path, " 6714778Srm160521 "'none', or 'legacy'"), propname); 6724778Srm160521 break; 6734778Srm160521 case NAME_ERR_TOOLONG: 6744778Srm160521 zfs_error_aux(hdl, 6754778Srm160521 dgettext(TEXT_DOMAIN, 6764778Srm160521 "component of '%s' is too long"), 6774778Srm160521 propname); 6784778Srm160521 break; 6794778Srm160521 } 6802676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6812676Seschrock goto error; 682789Sahrens } 6834778Srm160521 } 6844778Srm160521 6853126Sahl /*FALLTHRU*/ 6863126Sahl 6875331Samw case ZFS_PROP_SHARESMB: 6883126Sahl case ZFS_PROP_SHARENFS: 6893126Sahl /* 6905331Samw * For the mountpoint and sharenfs or sharesmb 6915331Samw * properties, check if it can be set in a 6925331Samw * global/non-global zone based on 6933126Sahl * the zoned property value: 6943126Sahl * 6953126Sahl * global zone non-global zone 6963126Sahl * -------------------------------------------------- 6973126Sahl * zoned=on mountpoint (no) mountpoint (yes) 6983126Sahl * sharenfs (no) sharenfs (no) 6995331Samw * sharesmb (no) sharesmb (no) 7003126Sahl * 7013126Sahl * zoned=off mountpoint (yes) N/A 7023126Sahl * sharenfs (yes) 7035331Samw * sharesmb (yes) 7043126Sahl */ 7052676Seschrock if (zoned) { 7062676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 7072676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7082676Seschrock "'%s' cannot be set on " 7092676Seschrock "dataset in a non-global zone"), 7102676Seschrock propname); 7112676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 7122676Seschrock errbuf); 7132676Seschrock goto error; 7145331Samw } else if (prop == ZFS_PROP_SHARENFS || 7155331Samw prop == ZFS_PROP_SHARESMB) { 7162676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7172676Seschrock "'%s' cannot be set in " 7182676Seschrock "a non-global zone"), propname); 7192676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 7202676Seschrock errbuf); 7212676Seschrock goto error; 7222676Seschrock } 7232676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 7242676Seschrock /* 7252676Seschrock * If zoned property is 'off', this must be in 7262676Seschrock * a globle zone. If not, something is wrong. 7272676Seschrock */ 7282676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7292676Seschrock "'%s' cannot be set while dataset " 7302676Seschrock "'zoned' property is set"), propname); 7312676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 7322676Seschrock goto error; 7332676Seschrock } 7343126Sahl 7354180Sdougm /* 7364180Sdougm * At this point, it is legitimate to set the 7374180Sdougm * property. Now we want to make sure that the 7384180Sdougm * property value is valid if it is sharenfs. 7394180Sdougm */ 7405331Samw if ((prop == ZFS_PROP_SHARENFS || 7415331Samw prop == ZFS_PROP_SHARESMB) && 7424217Seschrock strcmp(strval, "on") != 0 && 7434217Seschrock strcmp(strval, "off") != 0) { 7445331Samw zfs_share_proto_t proto; 7455331Samw 7465331Samw if (prop == ZFS_PROP_SHARESMB) 7475331Samw proto = PROTO_SMB; 7485331Samw else 7495331Samw proto = PROTO_NFS; 7504180Sdougm 7514180Sdougm /* 7525331Samw * Must be an valid sharing protocol 7535331Samw * option string so init the libshare 7545331Samw * in order to enable the parser and 7555331Samw * then parse the options. We use the 7565331Samw * control API since we don't care about 7575331Samw * the current configuration and don't 7584180Sdougm * want the overhead of loading it 7594180Sdougm * until we actually do something. 7604180Sdougm */ 7614180Sdougm 7624217Seschrock if (zfs_init_libshare(hdl, 7634217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 7644217Seschrock /* 7654217Seschrock * An error occurred so we can't do 7664217Seschrock * anything 7674217Seschrock */ 7684217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7694217Seschrock "'%s' cannot be set: problem " 7704217Seschrock "in share initialization"), 7714217Seschrock propname); 7724217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7734217Seschrock errbuf); 7744217Seschrock goto error; 7754217Seschrock } 7764217Seschrock 7775331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 7784217Seschrock /* 7794217Seschrock * There was an error in parsing so 7804217Seschrock * deal with it by issuing an error 7814217Seschrock * message and leaving after 7824217Seschrock * uninitializing the the libshare 7834217Seschrock * interface. 7844217Seschrock */ 7854217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7864217Seschrock "'%s' cannot be set to invalid " 7874217Seschrock "options"), propname); 7884217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7894217Seschrock errbuf); 7904217Seschrock zfs_uninit_libshare(hdl); 7914217Seschrock goto error; 7924217Seschrock } 7934180Sdougm zfs_uninit_libshare(hdl); 7944180Sdougm } 7954180Sdougm 7963126Sahl break; 7975331Samw case ZFS_PROP_UTF8ONLY: 7985331Samw chosen_utf = (int)intval; 7995331Samw break; 8005331Samw case ZFS_PROP_NORMALIZE: 8015331Samw chosen_normal = (int)intval; 8025331Samw break; 8032676Seschrock } 8042676Seschrock 8052676Seschrock /* 8062676Seschrock * For changes to existing volumes, we have some additional 8072676Seschrock * checks to enforce. 8082676Seschrock */ 8092676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 8102676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 8112676Seschrock ZFS_PROP_VOLSIZE); 8122676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 8132676Seschrock ZFS_PROP_VOLBLOCKSIZE); 8142676Seschrock char buf[64]; 8152676Seschrock 8162676Seschrock switch (prop) { 8172676Seschrock case ZFS_PROP_RESERVATION: 8185378Sck153898 case ZFS_PROP_REFRESERVATION: 8192676Seschrock if (intval > volsize) { 8202676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8212676Seschrock "'%s' is greater than current " 8222676Seschrock "volume size"), propname); 8232676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8242676Seschrock errbuf); 8252676Seschrock goto error; 8262676Seschrock } 8272676Seschrock break; 8282676Seschrock 8292676Seschrock case ZFS_PROP_VOLSIZE: 8302676Seschrock if (intval % blocksize != 0) { 8312676Seschrock zfs_nicenum(blocksize, buf, 8322676Seschrock sizeof (buf)); 8332676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8342676Seschrock "'%s' must be a multiple of " 8352676Seschrock "volume block size (%s)"), 8362676Seschrock propname, buf); 8372676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8382676Seschrock errbuf); 8392676Seschrock goto error; 8402676Seschrock } 8412676Seschrock 8422676Seschrock if (intval == 0) { 8432676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8442676Seschrock "'%s' cannot be zero"), 8452676Seschrock propname); 8462676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8472676Seschrock errbuf); 8482676Seschrock goto error; 849789Sahrens } 8503126Sahl break; 851789Sahrens } 852789Sahrens } 853789Sahrens } 854789Sahrens 8552676Seschrock /* 8565331Samw * If normalization was chosen, but no UTF8 choice was made, 8575331Samw * enforce rejection of non-UTF8 names. 8585331Samw * 8595331Samw * If normalization was chosen, but rejecting non-UTF8 names 8605331Samw * was explicitly not chosen, it is an error. 8615331Samw */ 8625498Stimh if (chosen_normal > 0 && chosen_utf < 0) { 8635331Samw if (nvlist_add_uint64(ret, 8645331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 8655331Samw (void) no_memory(hdl); 8665331Samw goto error; 8675331Samw } 8685498Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 8695331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8705331Samw "'%s' must be set 'on' if normalization chosen"), 8715331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 8725331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8735331Samw goto error; 8745331Samw } 8755331Samw 8765331Samw /* 8772676Seschrock * If this is an existing volume, and someone is setting the volsize, 8782676Seschrock * make sure that it matches the reservation, or add it if necessary. 8792676Seschrock */ 8802676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 8812676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 8822676Seschrock &intval) == 0) { 8832676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 8842676Seschrock ZFS_PROP_VOLSIZE); 8855481Sck153898 uint64_t old_reservation; 8862676Seschrock uint64_t new_reservation; 8875481Sck153898 zfs_prop_t resv_prop; 8885713Srm160521 8895713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 8905481Sck153898 goto error; 8915481Sck153898 old_reservation = zfs_prop_get_int(zhp, resv_prop); 8922676Seschrock 8932676Seschrock if (old_volsize == old_reservation && 8945481Sck153898 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop), 8952676Seschrock &new_reservation) != 0) { 8962676Seschrock if (nvlist_add_uint64(ret, 8975481Sck153898 zfs_prop_to_name(resv_prop), intval) != 0) { 8982676Seschrock (void) no_memory(hdl); 8992676Seschrock goto error; 9002676Seschrock } 9012676Seschrock } 9022676Seschrock } 9032676Seschrock return (ret); 9042676Seschrock 9052676Seschrock error: 9062676Seschrock nvlist_free(ret); 9072676Seschrock return (NULL); 908789Sahrens } 909789Sahrens 9104543Smarks static int 9114543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 9124543Smarks uint64_t *ret_who) 9134543Smarks { 9144543Smarks struct passwd *pwd; 9154543Smarks struct group *grp; 9164543Smarks uid_t id; 9174543Smarks 9184543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 9194543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 9204543Smarks *ret_who = -1; 9214543Smarks return (0); 9224543Smarks } 9234543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 9244543Smarks return (EZFS_BADWHO); 9254543Smarks 9264543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 9274543Smarks strcmp(who, "everyone") == 0) { 9284543Smarks *ret_who = -1; 9294543Smarks *who_type = ZFS_DELEG_EVERYONE; 9304543Smarks return (0); 9314543Smarks } 9324543Smarks 9334543Smarks pwd = getpwnam(who); 9344543Smarks grp = getgrnam(who); 9354543Smarks 9364543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 9374543Smarks *ret_who = pwd->pw_uid; 9384543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 9394543Smarks *ret_who = grp->gr_gid; 9404543Smarks } else if (pwd) { 9414543Smarks *ret_who = pwd->pw_uid; 9424543Smarks *who_type = ZFS_DELEG_USER; 9434543Smarks } else if (grp) { 9444543Smarks *ret_who = grp->gr_gid; 9454543Smarks *who_type = ZFS_DELEG_GROUP; 9464543Smarks } else { 9474543Smarks char *end; 9484543Smarks 9494543Smarks id = strtol(who, &end, 10); 9504543Smarks if (errno != 0 || *end != '\0') { 9514543Smarks return (EZFS_BADWHO); 9524543Smarks } else { 9534543Smarks *ret_who = id; 9544543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 9554543Smarks *who_type = ZFS_DELEG_USER; 9564543Smarks } 9574543Smarks } 9584543Smarks 9594543Smarks return (0); 9604543Smarks } 9614543Smarks 9624543Smarks static void 9634543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 9644543Smarks { 9654543Smarks if (perms_nvp != NULL) { 9664543Smarks verify(nvlist_add_nvlist(who_nvp, 9674543Smarks name, perms_nvp) == 0); 9684543Smarks } else { 9694543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 9704543Smarks } 9714543Smarks } 9724543Smarks 9734543Smarks static void 9744543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 9754543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 9764543Smarks nvlist_t *sets_nvp) 9774543Smarks { 9784543Smarks boolean_t do_perms, do_sets; 9794543Smarks char name[ZFS_MAX_DELEG_NAME]; 9804543Smarks 9814543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 9824543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 9834543Smarks 9844543Smarks if (!do_perms && !do_sets) 9854543Smarks do_perms = do_sets = B_TRUE; 9864543Smarks 9874543Smarks if (do_perms) { 9884543Smarks zfs_deleg_whokey(name, who_type, inherit, 9894543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9904543Smarks whostr : (void *)&whoid); 9914543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 9924543Smarks } 9934543Smarks if (do_sets) { 9944543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 9954543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9964543Smarks whostr : (void *)&whoid); 9974543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 9984543Smarks } 9994543Smarks } 10004543Smarks 10014543Smarks static void 10024543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 10034543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 10044543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 10054543Smarks { 10064543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 10074543Smarks helper(who_type, whoid, whostr, 0, 10084543Smarks who_nvp, perms_nvp, sets_nvp); 10094543Smarks } else { 10104543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 10114543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 10124543Smarks who_nvp, perms_nvp, sets_nvp); 10134543Smarks } 10144543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 10154543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 10164543Smarks who_nvp, perms_nvp, sets_nvp); 10174543Smarks } 10184543Smarks } 10194543Smarks } 10204543Smarks 10214543Smarks /* 10224543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 10234543Smarks * 10244543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 10254543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 10264543Smarks * base attribute named stored in the dsl. 10274543Smarks * Arguments: 10284543Smarks * 10294543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 10304543Smarks * whostr may be null for everyone or create perms. 10314543Smarks * who_type: is the type of entry in whostr. Typically this will be 10324543Smarks * ZFS_DELEG_WHO_UNKNOWN. 10335331Samw * perms: common separated list of permissions. May be null if user 10344543Smarks * is requested to remove permissions by who. 10354543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 10364543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 10374543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 10384543Smarks * The output nvp will look something like this. 10394543Smarks * ul$1234 -> {create ; destroy } 10404543Smarks * Ul$1234 -> { @myset } 10414543Smarks * s-$@myset - { snapshot; checksum; compression } 10424543Smarks */ 10434543Smarks int 10444543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 10454543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 10464543Smarks { 10474543Smarks nvlist_t *who_nvp; 10484543Smarks nvlist_t *perms_nvp = NULL; 10494543Smarks nvlist_t *sets_nvp = NULL; 10504543Smarks char errbuf[1024]; 10514787Sahrens char *who_tok, *perm; 10524543Smarks int error; 10534543Smarks 10544543Smarks *nvp = NULL; 10554543Smarks 10564543Smarks if (perms) { 10574543Smarks if ((error = nvlist_alloc(&perms_nvp, 10584543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10594543Smarks return (1); 10604543Smarks } 10614543Smarks if ((error = nvlist_alloc(&sets_nvp, 10624543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10634543Smarks nvlist_free(perms_nvp); 10644543Smarks return (1); 10654543Smarks } 10664543Smarks } 10674543Smarks 10684543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 10694543Smarks if (perms_nvp) 10704543Smarks nvlist_free(perms_nvp); 10714543Smarks if (sets_nvp) 10724543Smarks nvlist_free(sets_nvp); 10734543Smarks return (1); 10744543Smarks } 10754543Smarks 10764543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 10774543Smarks namecheck_err_t why; 10784543Smarks char what; 10794543Smarks 10804543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 10814787Sahrens nvlist_free(who_nvp); 10824787Sahrens if (perms_nvp) 10834787Sahrens nvlist_free(perms_nvp); 10844787Sahrens if (sets_nvp) 10854787Sahrens nvlist_free(sets_nvp); 10864787Sahrens 10874543Smarks switch (why) { 10884543Smarks case NAME_ERR_NO_AT: 10894543Smarks zfs_error_aux(zhp->zfs_hdl, 10904543Smarks dgettext(TEXT_DOMAIN, 10914543Smarks "set definition must begin with an '@' " 10924543Smarks "character")); 10934543Smarks } 10944543Smarks return (zfs_error(zhp->zfs_hdl, 10954543Smarks EZFS_BADPERMSET, whostr)); 10964543Smarks } 10974543Smarks } 10984543Smarks 10994543Smarks /* 11004543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 11014543Smarks * The first nvlist perms_nvp will have normal permissions and the 11024543Smarks * other sets_nvp will have only permssion set names in it. 11034543Smarks */ 11044787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 11054787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 11064787Sahrens 11074787Sahrens if (perm_canonical) { 11084787Sahrens verify(nvlist_add_boolean(perms_nvp, 11094787Sahrens perm_canonical) == 0); 11104787Sahrens } else if (perm[0] == '@') { 11114787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 11124787Sahrens } else { 11134787Sahrens nvlist_free(who_nvp); 11144787Sahrens nvlist_free(perms_nvp); 11154787Sahrens nvlist_free(sets_nvp); 11164787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 11174543Smarks } 11184543Smarks } 11194543Smarks 11204543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 11214543Smarks who_tok = strtok(whostr, ","); 11224543Smarks if (who_tok == NULL) { 11234543Smarks nvlist_free(who_nvp); 11244787Sahrens if (perms_nvp) 11254787Sahrens nvlist_free(perms_nvp); 11264543Smarks if (sets_nvp) 11274543Smarks nvlist_free(sets_nvp); 11284543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11294543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 11304543Smarks whostr); 11314543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11324543Smarks } 11334543Smarks } 11344543Smarks 11354543Smarks /* 11364543Smarks * Now create the nvlist(s) 11374543Smarks */ 11384543Smarks do { 11394543Smarks uint64_t who_id; 11404543Smarks 11414543Smarks error = zfs_get_perm_who(who_tok, &who_type, 11424543Smarks &who_id); 11434543Smarks if (error) { 11444543Smarks nvlist_free(who_nvp); 11454787Sahrens if (perms_nvp) 11464787Sahrens nvlist_free(perms_nvp); 11474543Smarks if (sets_nvp) 11484543Smarks nvlist_free(sets_nvp); 11494543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11504543Smarks dgettext(TEXT_DOMAIN, 11514543Smarks "Unable to determine uid/gid for " 11524543Smarks "%s "), who_tok); 11534543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11544543Smarks } 11554543Smarks 11564543Smarks /* 11574543Smarks * add entries for both local and descendent when required 11584543Smarks */ 11594543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 11604543Smarks perms_nvp, sets_nvp, who_type, inherit); 11614543Smarks 11624543Smarks } while (who_tok = strtok(NULL, ",")); 11634543Smarks *nvp = who_nvp; 11644543Smarks return (0); 11654543Smarks } 11664543Smarks 11674543Smarks static int 11684543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 11694543Smarks { 11704543Smarks zfs_cmd_t zc = { 0 }; 11714543Smarks int error; 11724543Smarks char errbuf[1024]; 11734543Smarks 11744543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11754543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 11764543Smarks zhp->zfs_name); 11774543Smarks 11785094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 11794543Smarks return (-1); 11804543Smarks 11814543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 11824543Smarks zc.zc_perm_action = unset; 11834543Smarks 11844543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 11854543Smarks if (error && errno == ENOTSUP) { 11864543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11874543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 11884543Smarks zcmd_free_nvlists(&zc); 11894543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 11904543Smarks } else if (error) { 11914543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 11924543Smarks } 11934543Smarks zcmd_free_nvlists(&zc); 11944543Smarks 11954543Smarks return (error); 11964543Smarks } 11974543Smarks 11984543Smarks int 11994543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 12004543Smarks { 12014543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 12024543Smarks } 12034543Smarks 12044543Smarks int 12054543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 12064543Smarks { 12074543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 12084543Smarks } 12094543Smarks 12104543Smarks static int 12114543Smarks perm_compare(const void *arg1, const void *arg2) 12124543Smarks { 12134543Smarks const zfs_perm_node_t *node1 = arg1; 12144543Smarks const zfs_perm_node_t *node2 = arg2; 12154543Smarks int ret; 12164543Smarks 12174543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 12184543Smarks 12194543Smarks if (ret > 0) 12204543Smarks return (1); 12214543Smarks if (ret < 0) 12224543Smarks return (-1); 12234543Smarks else 12244543Smarks return (0); 12254543Smarks } 12264543Smarks 12274543Smarks static void 12284543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 12294543Smarks { 12304543Smarks zfs_perm_node_t *permnode; 12315367Sahrens void *cookie = NULL; 12325367Sahrens 12335367Sahrens while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 12344543Smarks free(permnode); 12355367Sahrens avl_destroy(tree); 12364543Smarks } 12374543Smarks 12384543Smarks static void 12394543Smarks zfs_destroy_tree(avl_tree_t *tree) 12404543Smarks { 12414543Smarks zfs_allow_node_t *allownode; 12425367Sahrens void *cookie = NULL; 12435367Sahrens 12444543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 12454543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 12464543Smarks zfs_destroy_perm_tree(&allownode->z_local); 12474543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 12484543Smarks free(allownode); 12494543Smarks } 12505367Sahrens avl_destroy(tree); 12514543Smarks } 12524543Smarks 12534543Smarks void 12544543Smarks zfs_free_allows(zfs_allow_t *allow) 12554543Smarks { 12564543Smarks zfs_allow_t *allownext; 12574543Smarks zfs_allow_t *freeallow; 12584543Smarks 12594543Smarks allownext = allow; 12604543Smarks while (allownext) { 12614543Smarks zfs_destroy_tree(&allownext->z_sets); 12624543Smarks zfs_destroy_tree(&allownext->z_crperms); 12634543Smarks zfs_destroy_tree(&allownext->z_user); 12644543Smarks zfs_destroy_tree(&allownext->z_group); 12654543Smarks zfs_destroy_tree(&allownext->z_everyone); 12664543Smarks freeallow = allownext; 12674543Smarks allownext = allownext->z_next; 12684543Smarks free(freeallow); 12694543Smarks } 12704543Smarks } 12714543Smarks 12724543Smarks static zfs_allow_t * 12734543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 12744543Smarks { 12754543Smarks zfs_allow_t *ptree; 12764543Smarks 12774543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 12784543Smarks sizeof (zfs_allow_t))) == NULL) { 12794543Smarks return (NULL); 12804543Smarks } 12814543Smarks 12824543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 12834543Smarks avl_create(&ptree->z_sets, 12844543Smarks perm_compare, sizeof (zfs_allow_node_t), 12854543Smarks offsetof(zfs_allow_node_t, z_node)); 12864543Smarks avl_create(&ptree->z_crperms, 12874543Smarks perm_compare, sizeof (zfs_allow_node_t), 12884543Smarks offsetof(zfs_allow_node_t, z_node)); 12894543Smarks avl_create(&ptree->z_user, 12904543Smarks perm_compare, sizeof (zfs_allow_node_t), 12914543Smarks offsetof(zfs_allow_node_t, z_node)); 12924543Smarks avl_create(&ptree->z_group, 12934543Smarks perm_compare, sizeof (zfs_allow_node_t), 12944543Smarks offsetof(zfs_allow_node_t, z_node)); 12954543Smarks avl_create(&ptree->z_everyone, 12964543Smarks perm_compare, sizeof (zfs_allow_node_t), 12974543Smarks offsetof(zfs_allow_node_t, z_node)); 12984543Smarks 12994543Smarks if (prev) 13004543Smarks prev->z_next = ptree; 13014543Smarks ptree->z_next = NULL; 13024543Smarks return (ptree); 13034543Smarks } 13044543Smarks 13054543Smarks /* 13064543Smarks * Add permissions to the appropriate AVL permission tree. 13074543Smarks * The appropriate tree may not be the requested tree. 13084543Smarks * For example if ld indicates a local permission, but 13094543Smarks * same permission also exists as a descendent permission 13104543Smarks * then the permission will be removed from the descendent 13114543Smarks * tree and add the the local+descendent tree. 13124543Smarks */ 13134543Smarks static int 13144543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 13154543Smarks char *perm, char ld) 13164543Smarks { 13174543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 13184543Smarks zfs_perm_node_t *newnode; 13194543Smarks avl_index_t where, where2; 13204543Smarks avl_tree_t *tree, *altree; 13214543Smarks 13224543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 13234543Smarks 13244543Smarks if (ld == ZFS_DELEG_NA) { 13254543Smarks tree = &allownode->z_localdescend; 13264543Smarks altree = &allownode->z_descend; 13274543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 13284543Smarks tree = &allownode->z_local; 13294543Smarks altree = &allownode->z_descend; 13304543Smarks } else { 13314543Smarks tree = &allownode->z_descend; 13324543Smarks altree = &allownode->z_local; 13334543Smarks } 13344543Smarks permnode = avl_find(tree, &pnode, &where); 13354543Smarks permnode2 = avl_find(altree, &pnode, &where2); 13364543Smarks 13374543Smarks if (permnode2) { 13384543Smarks avl_remove(altree, permnode2); 13394543Smarks free(permnode2); 13404543Smarks if (permnode == NULL) { 13414543Smarks tree = &allownode->z_localdescend; 13424543Smarks } 13434543Smarks } 13444543Smarks 13454543Smarks /* 13464543Smarks * Now insert new permission in either requested location 13474543Smarks * local/descendent or into ld when perm will exist in both. 13484543Smarks */ 13494543Smarks if (permnode == NULL) { 13504543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 13514543Smarks sizeof (zfs_perm_node_t))) == NULL) { 13524543Smarks return (-1); 13534543Smarks } 13544543Smarks *newnode = pnode; 13554543Smarks avl_add(tree, newnode); 13564543Smarks } 13574543Smarks return (0); 13584543Smarks } 13594577Sahrens 13604543Smarks /* 13614543Smarks * Uggh, this is going to be a bit complicated. 13624543Smarks * we have an nvlist coming out of the kernel that 13634543Smarks * will indicate where the permission is set and then 13644543Smarks * it will contain allow of the various "who's", and what 13654543Smarks * their permissions are. To further complicate this 13664543Smarks * we will then have to coalesce the local,descendent 13674543Smarks * and local+descendent permissions where appropriate. 13684543Smarks * The kernel only knows about a permission as being local 13694543Smarks * or descendent, but not both. 13704543Smarks * 13714543Smarks * In order to make this easier for zfs_main to deal with 13724543Smarks * a series of AVL trees will be used to maintain 13734543Smarks * all of this, primarily for sorting purposes as well 13744543Smarks * as the ability to quickly locate a specific entry. 13754543Smarks * 13764543Smarks * What we end up with are tree's for sets, create perms, 13774543Smarks * user, groups and everyone. With each of those trees 13784543Smarks * we have subtrees for local, descendent and local+descendent 13794543Smarks * permissions. 13804543Smarks */ 13814543Smarks int 13824543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 13834543Smarks { 13844543Smarks zfs_cmd_t zc = { 0 }; 13854543Smarks int error; 13864543Smarks nvlist_t *nvlist; 13874543Smarks nvlist_t *permnv, *sourcenv; 13884543Smarks nvpair_t *who_pair, *source_pair; 13894543Smarks nvpair_t *perm_pair; 13904543Smarks char errbuf[1024]; 13914543Smarks zfs_allow_t *zallowp, *newallowp; 13924543Smarks char ld; 13934543Smarks char *nvpname; 13944543Smarks uid_t uid; 13954543Smarks gid_t gid; 13964543Smarks avl_tree_t *tree; 13974543Smarks avl_index_t where; 13984543Smarks 13994543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 14004543Smarks 14014543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 14024543Smarks return (-1); 14034543Smarks 14044543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 14054543Smarks if (errno == ENOMEM) { 14064543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 14074543Smarks zcmd_free_nvlists(&zc); 14084543Smarks return (-1); 14094543Smarks } 14104543Smarks } else if (errno == ENOTSUP) { 14114543Smarks zcmd_free_nvlists(&zc); 14124543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14134543Smarks gettext("Pool must be upgraded to use 'allow'")); 14144543Smarks return (zfs_error(zhp->zfs_hdl, 14154543Smarks EZFS_BADVERSION, errbuf)); 14164543Smarks } else { 14174543Smarks zcmd_free_nvlists(&zc); 14184543Smarks return (-1); 14194543Smarks } 14204543Smarks } 14214543Smarks 14224543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 14234543Smarks zcmd_free_nvlists(&zc); 14244543Smarks return (-1); 14254543Smarks } 14264543Smarks 14274543Smarks zcmd_free_nvlists(&zc); 14284543Smarks 14294543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 14304543Smarks 14314543Smarks if (source_pair == NULL) { 14324543Smarks *zfs_perms = NULL; 14334543Smarks return (0); 14344543Smarks } 14354543Smarks 14364543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 14374543Smarks if (*zfs_perms == NULL) { 14384543Smarks return (0); 14394543Smarks } 14404543Smarks 14414543Smarks zallowp = *zfs_perms; 14424543Smarks 14434543Smarks for (;;) { 14444543Smarks struct passwd *pwd; 14454543Smarks struct group *grp; 14464543Smarks zfs_allow_node_t *allownode; 14474543Smarks zfs_allow_node_t findallownode; 14484543Smarks zfs_allow_node_t *newallownode; 14494543Smarks 14504543Smarks (void) strlcpy(zallowp->z_setpoint, 14514543Smarks nvpair_name(source_pair), 14524543Smarks sizeof (zallowp->z_setpoint)); 14534543Smarks 14544543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 14554543Smarks goto abort; 14564543Smarks 14574543Smarks /* 14584543Smarks * Make sure nvlist is composed correctly 14594543Smarks */ 14604543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 14614543Smarks goto abort; 14624543Smarks } 14634543Smarks 14644543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 14654543Smarks if (who_pair == NULL) { 14664543Smarks goto abort; 14674543Smarks } 14684543Smarks 14694543Smarks do { 14704543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 14714543Smarks if (error) { 14724543Smarks goto abort; 14734543Smarks } 14744543Smarks 14754543Smarks /* 14764543Smarks * First build up the key to use 14774543Smarks * for looking up in the various 14784543Smarks * who trees. 14794543Smarks */ 14804543Smarks ld = nvpair_name(who_pair)[1]; 14814543Smarks nvpname = nvpair_name(who_pair); 14824543Smarks switch (nvpair_name(who_pair)[0]) { 14834543Smarks case ZFS_DELEG_USER: 14844543Smarks case ZFS_DELEG_USER_SETS: 14854543Smarks tree = &zallowp->z_user; 14864543Smarks uid = atol(&nvpname[3]); 14874543Smarks pwd = getpwuid(uid); 14884543Smarks (void) snprintf(findallownode.z_key, 14894543Smarks sizeof (findallownode.z_key), "user %s", 14904543Smarks (pwd) ? pwd->pw_name : 14914543Smarks &nvpair_name(who_pair)[3]); 14924543Smarks break; 14934543Smarks case ZFS_DELEG_GROUP: 14944543Smarks case ZFS_DELEG_GROUP_SETS: 14954543Smarks tree = &zallowp->z_group; 14964543Smarks gid = atol(&nvpname[3]); 14974543Smarks grp = getgrgid(gid); 14984543Smarks (void) snprintf(findallownode.z_key, 14994543Smarks sizeof (findallownode.z_key), "group %s", 15004543Smarks (grp) ? grp->gr_name : 15014543Smarks &nvpair_name(who_pair)[3]); 15024543Smarks break; 15034543Smarks case ZFS_DELEG_CREATE: 15044543Smarks case ZFS_DELEG_CREATE_SETS: 15054543Smarks tree = &zallowp->z_crperms; 15064543Smarks (void) strlcpy(findallownode.z_key, "", 15074543Smarks sizeof (findallownode.z_key)); 15084543Smarks break; 15094543Smarks case ZFS_DELEG_EVERYONE: 15104543Smarks case ZFS_DELEG_EVERYONE_SETS: 15114543Smarks (void) snprintf(findallownode.z_key, 15124543Smarks sizeof (findallownode.z_key), "everyone"); 15134543Smarks tree = &zallowp->z_everyone; 15144543Smarks break; 15154543Smarks case ZFS_DELEG_NAMED_SET: 15164543Smarks case ZFS_DELEG_NAMED_SET_SETS: 15174543Smarks (void) snprintf(findallownode.z_key, 15184543Smarks sizeof (findallownode.z_key), "%s", 15194543Smarks &nvpair_name(who_pair)[3]); 15204543Smarks tree = &zallowp->z_sets; 15214543Smarks break; 15224543Smarks } 15234543Smarks 15244543Smarks /* 15254543Smarks * Place who in tree 15264543Smarks */ 15274543Smarks allownode = avl_find(tree, &findallownode, &where); 15284543Smarks if (allownode == NULL) { 15294543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 15304543Smarks sizeof (zfs_allow_node_t))) == NULL) { 15314543Smarks goto abort; 15324543Smarks } 15334543Smarks avl_create(&newallownode->z_localdescend, 15344543Smarks perm_compare, 15354543Smarks sizeof (zfs_perm_node_t), 15364543Smarks offsetof(zfs_perm_node_t, z_node)); 15374543Smarks avl_create(&newallownode->z_local, 15384543Smarks perm_compare, 15394543Smarks sizeof (zfs_perm_node_t), 15404543Smarks offsetof(zfs_perm_node_t, z_node)); 15414543Smarks avl_create(&newallownode->z_descend, 15424543Smarks perm_compare, 15434543Smarks sizeof (zfs_perm_node_t), 15444543Smarks offsetof(zfs_perm_node_t, z_node)); 15454543Smarks (void) strlcpy(newallownode->z_key, 15464543Smarks findallownode.z_key, 15474543Smarks sizeof (findallownode.z_key)); 15484543Smarks avl_insert(tree, newallownode, where); 15494543Smarks allownode = newallownode; 15504543Smarks } 15514543Smarks 15524543Smarks /* 15534543Smarks * Now iterate over the permissions and 15544543Smarks * place them in the appropriate local, 15554543Smarks * descendent or local+descendent tree. 15564543Smarks * 15574543Smarks * The permissions are added to the tree 15584543Smarks * via zfs_coalesce_perm(). 15594543Smarks */ 15604543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 15614543Smarks if (perm_pair == NULL) 15624543Smarks goto abort; 15634543Smarks do { 15644543Smarks if (zfs_coalesce_perm(zhp, allownode, 15654543Smarks nvpair_name(perm_pair), ld) != 0) 15664543Smarks goto abort; 15674543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 15684543Smarks perm_pair)); 15694543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 15704543Smarks 15714543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 15724543Smarks if (source_pair == NULL) 15734543Smarks break; 15744543Smarks 15754543Smarks /* 15764543Smarks * allocate another node from the link list of 15774543Smarks * zfs_allow_t structures 15784543Smarks */ 15794543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 15804543Smarks nvpair_name(source_pair)); 15814543Smarks if (newallowp == NULL) { 15824543Smarks goto abort; 15834543Smarks } 15844543Smarks zallowp = newallowp; 15854543Smarks } 15864543Smarks nvlist_free(nvlist); 15874543Smarks return (0); 15884543Smarks abort: 15894543Smarks zfs_free_allows(*zfs_perms); 15904543Smarks nvlist_free(nvlist); 15914543Smarks return (-1); 15924543Smarks } 15934543Smarks 15945993Smarks static char * 15955993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note) 15965993Smarks { 15975993Smarks /* 15985993Smarks * Don't put newlines on end of lines 15995993Smarks */ 16005993Smarks switch (note) { 16015993Smarks case ZFS_DELEG_NOTE_CREATE: 16025993Smarks return (dgettext(TEXT_DOMAIN, 16035993Smarks "Must also have the 'mount' ability")); 16045993Smarks case ZFS_DELEG_NOTE_DESTROY: 16055993Smarks return (dgettext(TEXT_DOMAIN, 16065993Smarks "Must also have the 'mount' ability")); 16075993Smarks case ZFS_DELEG_NOTE_SNAPSHOT: 16085993Smarks return (dgettext(TEXT_DOMAIN, 16095993Smarks "Must also have the 'mount' ability")); 16105993Smarks case ZFS_DELEG_NOTE_ROLLBACK: 16115993Smarks return (dgettext(TEXT_DOMAIN, 16125993Smarks "Must also have the 'mount' ability")); 16135993Smarks case ZFS_DELEG_NOTE_CLONE: 16145993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'create' " 16155993Smarks "ability and 'mount'\n" 16165993Smarks "\t\t\t\tability in the origin file system")); 16175993Smarks case ZFS_DELEG_NOTE_PROMOTE: 16185993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n" 16195993Smarks "\t\t\t\tand 'promote' ability in the origin file system")); 16205993Smarks case ZFS_DELEG_NOTE_RENAME: 16215993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' " 16225993Smarks "and 'create' \n\t\t\t\tability in the new parent")); 16235993Smarks case ZFS_DELEG_NOTE_RECEIVE: 16245993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'" 16255993Smarks " and 'create' ability")); 16265993Smarks case ZFS_DELEG_NOTE_USERPROP: 16275993Smarks return (dgettext(TEXT_DOMAIN, 16285993Smarks "Allows changing any user property")); 16295993Smarks case ZFS_DELEG_NOTE_ALLOW: 16305993Smarks return (dgettext(TEXT_DOMAIN, 16315993Smarks "Must also have the permission that is being\n" 16325993Smarks "\t\t\t\tallowed")); 16335993Smarks case ZFS_DELEG_NOTE_MOUNT: 16345993Smarks return (dgettext(TEXT_DOMAIN, 16355993Smarks "Allows mount/umount of ZFS datasets")); 16365993Smarks case ZFS_DELEG_NOTE_SHARE: 16375993Smarks return (dgettext(TEXT_DOMAIN, 16385993Smarks "Allows sharing file systems over NFS or SMB\n" 16395993Smarks "\t\t\t\tprotocols")); 16405993Smarks case ZFS_DELEG_NOTE_NONE: 16415993Smarks default: 16425993Smarks return (dgettext(TEXT_DOMAIN, "")); 16435993Smarks } 16445993Smarks } 16455993Smarks 16465993Smarks typedef enum { 16475993Smarks ZFS_DELEG_SUBCOMMAND, 16485993Smarks ZFS_DELEG_PROP, 16495993Smarks ZFS_DELEG_OTHER 16505993Smarks } zfs_deleg_perm_type_t; 16515993Smarks 16525993Smarks /* 16535993Smarks * is the permission a subcommand or other? 16545993Smarks */ 16555993Smarks zfs_deleg_perm_type_t 16565993Smarks zfs_deleg_perm_type(const char *perm) 16575993Smarks { 16585993Smarks if (strcmp(perm, "userprop") == 0) 16595993Smarks return (ZFS_DELEG_OTHER); 16605993Smarks else 16615993Smarks return (ZFS_DELEG_SUBCOMMAND); 16625993Smarks } 16635993Smarks 16645993Smarks static char * 16655993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type) 16665993Smarks { 16675993Smarks switch (type) { 16685993Smarks case ZFS_DELEG_SUBCOMMAND: 16695993Smarks return (dgettext(TEXT_DOMAIN, "subcommand")); 16705993Smarks case ZFS_DELEG_PROP: 16715993Smarks return (dgettext(TEXT_DOMAIN, "property")); 16725993Smarks case ZFS_DELEG_OTHER: 16735993Smarks return (dgettext(TEXT_DOMAIN, "other")); 16745993Smarks } 16755993Smarks return (""); 16765993Smarks } 16775993Smarks 16785993Smarks /*ARGSUSED*/ 16795993Smarks static int 16805993Smarks zfs_deleg_prop_cb(int prop, void *cb) 16815993Smarks { 16825993Smarks if (zfs_prop_delegatable(prop)) 16835993Smarks (void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop), 16845993Smarks zfs_deleg_perm_type_str(ZFS_DELEG_PROP)); 16855993Smarks 16865993Smarks return (ZPROP_CONT); 16875993Smarks } 16885993Smarks 16895993Smarks void 16905993Smarks zfs_deleg_permissions(void) 16915993Smarks { 16925993Smarks int i; 16935993Smarks 16945993Smarks (void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME", 16955993Smarks "TYPE", "NOTES"); 16965993Smarks 16975993Smarks /* 16985993Smarks * First print out the subcommands 16995993Smarks */ 17005993Smarks for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) { 17015993Smarks (void) fprintf(stderr, "%-15s %-15s\t%s\n", 17025993Smarks zfs_deleg_perm_tab[i].z_perm, 17035993Smarks zfs_deleg_perm_type_str( 17045993Smarks zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)), 17055993Smarks zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note)); 17065993Smarks } 17075993Smarks 17085993Smarks (void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE, 17095993Smarks ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME); 17105993Smarks } 17115993Smarks 1712789Sahrens /* 1713789Sahrens * Given a property name and value, set the property for the given dataset. 1714789Sahrens */ 1715789Sahrens int 17162676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1717789Sahrens { 1718789Sahrens zfs_cmd_t zc = { 0 }; 17192676Seschrock int ret = -1; 17202676Seschrock prop_changelist_t *cl = NULL; 17212082Seschrock char errbuf[1024]; 17222082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 17232676Seschrock nvlist_t *nvl = NULL, *realprops; 17242676Seschrock zfs_prop_t prop; 17256168Shs24103 int do_prefix = 1; 17262082Seschrock 17272082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 17282676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 17292082Seschrock zhp->zfs_name); 17302082Seschrock 17312676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 17322676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 17332676Seschrock (void) no_memory(hdl); 17342676Seschrock goto error; 1735789Sahrens } 1736789Sahrens 17375094Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 17382676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 17392676Seschrock goto error; 17405094Slling 17412676Seschrock nvlist_free(nvl); 17422676Seschrock nvl = realprops; 17432676Seschrock 17442676Seschrock prop = zfs_name_to_prop(propname); 17452676Seschrock 1746789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 17472676Seschrock goto error; 1748789Sahrens 1749789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 17502082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17512082Seschrock "child dataset with inherited mountpoint is used " 17522082Seschrock "in a non-global zone")); 17532082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1754789Sahrens goto error; 1755789Sahrens } 1756789Sahrens 17576168Shs24103 17586168Shs24103 /* do not unmount dataset if canmount is being set to noauto */ 17596168Shs24103 if (prop == ZFS_PROP_CANMOUNT && *propval == ZFS_CANMOUNT_NOAUTO) 17606168Shs24103 do_prefix = 0; 17616168Shs24103 17626168Shs24103 if (do_prefix && (ret = changelist_prefix(cl)) != 0) 17636168Shs24103 goto error; 1764789Sahrens 1765789Sahrens /* 1766789Sahrens * Execute the corresponding ioctl() to set this property. 1767789Sahrens */ 1768789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1769789Sahrens 17705094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 17712676Seschrock goto error; 17722676Seschrock 17734543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1774789Sahrens if (ret != 0) { 1775789Sahrens switch (errno) { 1776789Sahrens 1777789Sahrens case ENOSPC: 1778789Sahrens /* 1779789Sahrens * For quotas and reservations, ENOSPC indicates 1780789Sahrens * something different; setting a quota or reservation 1781789Sahrens * doesn't use any disk space. 1782789Sahrens */ 1783789Sahrens switch (prop) { 1784789Sahrens case ZFS_PROP_QUOTA: 17855378Sck153898 case ZFS_PROP_REFQUOTA: 17862082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17872082Seschrock "size is less than current used or " 17882082Seschrock "reserved space")); 17892082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1790789Sahrens break; 1791789Sahrens 1792789Sahrens case ZFS_PROP_RESERVATION: 17935378Sck153898 case ZFS_PROP_REFRESERVATION: 17942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17952082Seschrock "size is greater than available space")); 17962082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1797789Sahrens break; 1798789Sahrens 1799789Sahrens default: 18002082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1801789Sahrens break; 1802789Sahrens } 1803789Sahrens break; 1804789Sahrens 1805789Sahrens case EBUSY: 18062082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 18072082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 18082082Seschrock else 18092676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1810789Sahrens break; 1811789Sahrens 18121175Slling case EROFS: 18132082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 18141175Slling break; 18151175Slling 18163886Sahl case ENOTSUP: 18173886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18185977Smarks "pool and or dataset must be upgraded to set this " 18194603Sahrens "property or value")); 18203886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 18213886Sahl break; 18223886Sahl 1823789Sahrens case EOVERFLOW: 1824789Sahrens /* 1825789Sahrens * This platform can't address a volume this big. 1826789Sahrens */ 1827789Sahrens #ifdef _ILP32 1828789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 18292082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1830789Sahrens break; 1831789Sahrens } 1832789Sahrens #endif 18332082Seschrock /* FALLTHROUGH */ 1834789Sahrens default: 18352082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1836789Sahrens } 1837789Sahrens } else { 18386168Shs24103 if (do_prefix) 18396168Shs24103 ret = changelist_postfix(cl); 18406168Shs24103 1841789Sahrens /* 1842789Sahrens * Refresh the statistics so the new property value 1843789Sahrens * is reflected. 1844789Sahrens */ 18456168Shs24103 if (ret == 0) 18462676Seschrock (void) get_stats(zhp); 1847789Sahrens } 1848789Sahrens 1849789Sahrens error: 18502676Seschrock nvlist_free(nvl); 18512676Seschrock zcmd_free_nvlists(&zc); 18522676Seschrock if (cl) 18532676Seschrock changelist_free(cl); 1854789Sahrens return (ret); 1855789Sahrens } 1856789Sahrens 1857789Sahrens /* 1858789Sahrens * Given a property, inherit the value from the parent dataset. 1859789Sahrens */ 1860789Sahrens int 18612676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1862789Sahrens { 1863789Sahrens zfs_cmd_t zc = { 0 }; 1864789Sahrens int ret; 1865789Sahrens prop_changelist_t *cl; 18662082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 18672082Seschrock char errbuf[1024]; 18682676Seschrock zfs_prop_t prop; 18692082Seschrock 18702082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 18712082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1872789Sahrens 18735094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 18742676Seschrock /* 18752676Seschrock * For user properties, the amount of work we have to do is very 18762676Seschrock * small, so just do it here. 18772676Seschrock */ 18782676Seschrock if (!zfs_prop_user(propname)) { 18792676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 18802676Seschrock "invalid property")); 18812676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 18822676Seschrock } 18832676Seschrock 18842676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 18852676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 18862676Seschrock 18874849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 18882676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 18892676Seschrock 18902676Seschrock return (0); 18912676Seschrock } 18922676Seschrock 1893789Sahrens /* 1894789Sahrens * Verify that this property is inheritable. 1895789Sahrens */ 18962082Seschrock if (zfs_prop_readonly(prop)) 18972082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 18982082Seschrock 18992082Seschrock if (!zfs_prop_inheritable(prop)) 19002082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1901789Sahrens 1902789Sahrens /* 1903789Sahrens * Check to see if the value applies to this type 1904789Sahrens */ 19052082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 19062082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1907789Sahrens 19083443Srm160521 /* 19093443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 19103443Srm160521 */ 19113443Srm160521 propname = zfs_prop_to_name(prop); 1912789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19132676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1914789Sahrens 1915789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1916789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 19172082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19182082Seschrock "dataset is used in a non-global zone")); 19192082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1920789Sahrens } 1921789Sahrens 1922789Sahrens /* 1923789Sahrens * Determine datasets which will be affected by this change, if any. 1924789Sahrens */ 1925789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1926789Sahrens return (-1); 1927789Sahrens 1928789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 19292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19302082Seschrock "child dataset with inherited mountpoint is used " 19312082Seschrock "in a non-global zone")); 19322082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1933789Sahrens goto error; 1934789Sahrens } 1935789Sahrens 1936789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1937789Sahrens goto error; 1938789Sahrens 19394849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 19402082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1941789Sahrens } else { 1942789Sahrens 19432169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1944789Sahrens goto error; 1945789Sahrens 1946789Sahrens /* 1947789Sahrens * Refresh the statistics so the new property is reflected. 1948789Sahrens */ 1949789Sahrens (void) get_stats(zhp); 1950789Sahrens } 1951789Sahrens 1952789Sahrens error: 1953789Sahrens changelist_free(cl); 1954789Sahrens return (ret); 1955789Sahrens } 1956789Sahrens 1957789Sahrens /* 19581356Seschrock * True DSL properties are stored in an nvlist. The following two functions 19591356Seschrock * extract them appropriately. 19601356Seschrock */ 19611356Seschrock static uint64_t 19621356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 19631356Seschrock { 19641356Seschrock nvlist_t *nv; 19651356Seschrock uint64_t value; 19661356Seschrock 19672885Sahrens *source = NULL; 19681356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 19691356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 19705094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 19715094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 19721356Seschrock } else { 19731356Seschrock value = zfs_prop_default_numeric(prop); 19741356Seschrock *source = ""; 19751356Seschrock } 19761356Seschrock 19771356Seschrock return (value); 19781356Seschrock } 19791356Seschrock 19801356Seschrock static char * 19811356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 19821356Seschrock { 19831356Seschrock nvlist_t *nv; 19841356Seschrock char *value; 19851356Seschrock 19862885Sahrens *source = NULL; 19871356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 19881356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 19895094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 19905094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 19911356Seschrock } else { 19921356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 19931356Seschrock value = ""; 19941356Seschrock *source = ""; 19951356Seschrock } 19961356Seschrock 19971356Seschrock return (value); 19981356Seschrock } 19991356Seschrock 20001356Seschrock /* 2001789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2002789Sahrens * zfs_prop_get_int() are built using this interface. 2003789Sahrens * 2004789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2005789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2006789Sahrens * If they differ from the on-disk values, report the current values and mark 2007789Sahrens * the source "temporary". 2008789Sahrens */ 20092082Seschrock static int 20105094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 20112082Seschrock char **source, uint64_t *val) 2012789Sahrens { 20135147Srm160521 zfs_cmd_t zc = { 0 }; 20145592Stimh nvlist_t *zplprops = NULL; 2015789Sahrens struct mnttab mnt; 20163265Sahrens char *mntopt_on = NULL; 20173265Sahrens char *mntopt_off = NULL; 2018789Sahrens 2019789Sahrens *source = NULL; 2020789Sahrens 20213265Sahrens switch (prop) { 20223265Sahrens case ZFS_PROP_ATIME: 20233265Sahrens mntopt_on = MNTOPT_ATIME; 20243265Sahrens mntopt_off = MNTOPT_NOATIME; 20253265Sahrens break; 20263265Sahrens 20273265Sahrens case ZFS_PROP_DEVICES: 20283265Sahrens mntopt_on = MNTOPT_DEVICES; 20293265Sahrens mntopt_off = MNTOPT_NODEVICES; 20303265Sahrens break; 20313265Sahrens 20323265Sahrens case ZFS_PROP_EXEC: 20333265Sahrens mntopt_on = MNTOPT_EXEC; 20343265Sahrens mntopt_off = MNTOPT_NOEXEC; 20353265Sahrens break; 20363265Sahrens 20373265Sahrens case ZFS_PROP_READONLY: 20383265Sahrens mntopt_on = MNTOPT_RO; 20393265Sahrens mntopt_off = MNTOPT_RW; 20403265Sahrens break; 20413265Sahrens 20423265Sahrens case ZFS_PROP_SETUID: 20433265Sahrens mntopt_on = MNTOPT_SETUID; 20443265Sahrens mntopt_off = MNTOPT_NOSETUID; 20453265Sahrens break; 20463265Sahrens 20473265Sahrens case ZFS_PROP_XATTR: 20483265Sahrens mntopt_on = MNTOPT_XATTR; 20493265Sahrens mntopt_off = MNTOPT_NOXATTR; 20503265Sahrens break; 20515331Samw 20525331Samw case ZFS_PROP_NBMAND: 20535331Samw mntopt_on = MNTOPT_NBMAND; 20545331Samw mntopt_off = MNTOPT_NONBMAND; 20555331Samw break; 20563265Sahrens } 20573265Sahrens 20582474Seschrock /* 20592474Seschrock * Because looking up the mount options is potentially expensive 20602474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 20612474Seschrock * we're looking up a property which requires its presence. 20622474Seschrock */ 20632474Seschrock if (!zhp->zfs_mntcheck && 20643265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 20653265Sahrens struct mnttab entry, search = { 0 }; 20663265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 20672474Seschrock 20682474Seschrock search.mnt_special = (char *)zhp->zfs_name; 20692474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 20703265Sahrens rewind(mnttab); 20713265Sahrens 20723265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 20733265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 20743265Sahrens entry.mnt_mntopts); 20753265Sahrens if (zhp->zfs_mntopts == NULL) 20763265Sahrens return (-1); 20773265Sahrens } 20782474Seschrock 20792474Seschrock zhp->zfs_mntcheck = B_TRUE; 20802474Seschrock } 20812474Seschrock 2082789Sahrens if (zhp->zfs_mntopts == NULL) 2083789Sahrens mnt.mnt_mntopts = ""; 2084789Sahrens else 2085789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2086789Sahrens 2087789Sahrens switch (prop) { 2088789Sahrens case ZFS_PROP_ATIME: 20893265Sahrens case ZFS_PROP_DEVICES: 20903265Sahrens case ZFS_PROP_EXEC: 20913265Sahrens case ZFS_PROP_READONLY: 20923265Sahrens case ZFS_PROP_SETUID: 20933265Sahrens case ZFS_PROP_XATTR: 20945331Samw case ZFS_PROP_NBMAND: 20952082Seschrock *val = getprop_uint64(zhp, prop, source); 20962082Seschrock 20973265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 20982082Seschrock *val = B_TRUE; 2099789Sahrens if (src) 21005094Slling *src = ZPROP_SRC_TEMPORARY; 21013265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 21022082Seschrock *val = B_FALSE; 2103789Sahrens if (src) 21045094Slling *src = ZPROP_SRC_TEMPORARY; 2105789Sahrens } 21062082Seschrock break; 2107789Sahrens 21083265Sahrens case ZFS_PROP_CANMOUNT: 21092082Seschrock *val = getprop_uint64(zhp, prop, source); 21106168Shs24103 if (*val != ZFS_CANMOUNT_ON) 21113417Srm160521 *source = zhp->zfs_name; 21123417Srm160521 else 21133417Srm160521 *source = ""; /* default */ 21142082Seschrock break; 2115789Sahrens 2116789Sahrens case ZFS_PROP_QUOTA: 21175378Sck153898 case ZFS_PROP_REFQUOTA: 2118789Sahrens case ZFS_PROP_RESERVATION: 21195378Sck153898 case ZFS_PROP_REFRESERVATION: 21202885Sahrens *val = getprop_uint64(zhp, prop, source); 21212885Sahrens if (*val == 0) 2122789Sahrens *source = ""; /* default */ 2123789Sahrens else 2124789Sahrens *source = zhp->zfs_name; 21252082Seschrock break; 2126789Sahrens 2127789Sahrens case ZFS_PROP_MOUNTED: 21282082Seschrock *val = (zhp->zfs_mntopts != NULL); 21292082Seschrock break; 2130789Sahrens 21313377Seschrock case ZFS_PROP_NUMCLONES: 21323377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 21333377Seschrock break; 21343377Seschrock 21355147Srm160521 case ZFS_PROP_VERSION: 21365498Stimh case ZFS_PROP_NORMALIZE: 21375498Stimh case ZFS_PROP_UTF8ONLY: 21385498Stimh case ZFS_PROP_CASE: 21395498Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 21405498Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 21415473Srm160521 return (-1); 21425147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21435498Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 21445498Stimh zcmd_free_nvlists(&zc); 21455147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 21465498Stimh "unable to get %s property"), 21475498Stimh zfs_prop_to_name(prop)); 21485147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 21495147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 21505147Srm160521 } 21515498Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 21525498Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 21535498Stimh val) != 0) { 21545498Stimh zcmd_free_nvlists(&zc); 21555498Stimh zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 21565498Stimh "unable to get %s property"), 21575498Stimh zfs_prop_to_name(prop)); 21585498Stimh return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM, 21595498Stimh dgettext(TEXT_DOMAIN, "internal error"))); 21605498Stimh } 21615592Stimh if (zplprops) 21625592Stimh nvlist_free(zplprops); 21635498Stimh zcmd_free_nvlists(&zc); 21645147Srm160521 break; 21655147Srm160521 2166789Sahrens default: 21674577Sahrens switch (zfs_prop_get_type(prop)) { 21684787Sahrens case PROP_TYPE_NUMBER: 21694787Sahrens case PROP_TYPE_INDEX: 21704577Sahrens *val = getprop_uint64(zhp, prop, source); 21714577Sahrens break; 21724577Sahrens 21734787Sahrens case PROP_TYPE_STRING: 21744577Sahrens default: 21754577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 21764577Sahrens "cannot get non-numeric property")); 21774577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 21784577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 21794577Sahrens } 2180789Sahrens } 2181789Sahrens 2182789Sahrens return (0); 2183789Sahrens } 2184789Sahrens 2185789Sahrens /* 2186789Sahrens * Calculate the source type, given the raw source string. 2187789Sahrens */ 2188789Sahrens static void 21895094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2190789Sahrens char *statbuf, size_t statlen) 2191789Sahrens { 21925094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2193789Sahrens return; 2194789Sahrens 2195789Sahrens if (source == NULL) { 21965094Slling *srctype = ZPROP_SRC_NONE; 2197789Sahrens } else if (source[0] == '\0') { 21985094Slling *srctype = ZPROP_SRC_DEFAULT; 2199789Sahrens } else { 2200789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 22015094Slling *srctype = ZPROP_SRC_LOCAL; 2202789Sahrens } else { 2203789Sahrens (void) strlcpy(statbuf, source, statlen); 22045094Slling *srctype = ZPROP_SRC_INHERITED; 2205789Sahrens } 2206789Sahrens } 2207789Sahrens 2208789Sahrens } 2209789Sahrens 2210789Sahrens /* 2211789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2212789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2213789Sahrens * human-readable form. 2214789Sahrens * 2215789Sahrens * Returns 0 on success, or -1 on error. 2216789Sahrens */ 2217789Sahrens int 2218789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 22195094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2220789Sahrens { 2221789Sahrens char *source = NULL; 2222789Sahrens uint64_t val; 2223789Sahrens char *str; 2224789Sahrens const char *root; 22252676Seschrock const char *strval; 2226789Sahrens 2227789Sahrens /* 2228789Sahrens * Check to see if this property applies to our object 2229789Sahrens */ 2230789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2231789Sahrens return (-1); 2232789Sahrens 2233789Sahrens if (src) 22345094Slling *src = ZPROP_SRC_NONE; 2235789Sahrens 2236789Sahrens switch (prop) { 2237789Sahrens case ZFS_PROP_CREATION: 2238789Sahrens /* 2239789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2240789Sahrens * this into a string unless 'literal' is specified. 2241789Sahrens */ 2242789Sahrens { 22432885Sahrens val = getprop_uint64(zhp, prop, &source); 22442885Sahrens time_t time = (time_t)val; 2245789Sahrens struct tm t; 2246789Sahrens 2247789Sahrens if (literal || 2248789Sahrens localtime_r(&time, &t) == NULL || 2249789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2250789Sahrens &t) == 0) 22512885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2252789Sahrens } 2253789Sahrens break; 2254789Sahrens 2255789Sahrens case ZFS_PROP_MOUNTPOINT: 2256789Sahrens /* 2257789Sahrens * Getting the precise mountpoint can be tricky. 2258789Sahrens * 2259789Sahrens * - for 'none' or 'legacy', return those values. 2260789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2261789Sahrens * - for inherited mountpoints, we want to take everything 2262789Sahrens * after our ancestor and append it to the inherited value. 2263789Sahrens * 2264789Sahrens * If the pool has an alternate root, we want to prepend that 2265789Sahrens * root to any values we return. 2266789Sahrens */ 22671544Seschrock root = zhp->zfs_root; 22681356Seschrock str = getprop_string(zhp, prop, &source); 22691356Seschrock 22701356Seschrock if (str[0] == '\0') { 2271789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2272789Sahrens root, zhp->zfs_name); 22731356Seschrock } else if (str[0] == '/') { 22741356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2275789Sahrens 2276789Sahrens if (relpath[0] == '/') 2277789Sahrens relpath++; 22781356Seschrock if (str[1] == '\0') 22791356Seschrock str++; 2280789Sahrens 2281789Sahrens if (relpath[0] == '\0') 2282789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 22831356Seschrock root, str); 2284789Sahrens else 2285789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 22861356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2287789Sahrens relpath); 2288789Sahrens } else { 2289789Sahrens /* 'legacy' or 'none' */ 22901356Seschrock (void) strlcpy(propbuf, str, proplen); 2291789Sahrens } 2292789Sahrens 2293789Sahrens break; 2294789Sahrens 2295789Sahrens case ZFS_PROP_ORIGIN: 22962885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2297789Sahrens proplen); 2298789Sahrens /* 2299789Sahrens * If there is no parent at all, return failure to indicate that 2300789Sahrens * it doesn't apply to this dataset. 2301789Sahrens */ 2302789Sahrens if (propbuf[0] == '\0') 2303789Sahrens return (-1); 2304789Sahrens break; 2305789Sahrens 2306789Sahrens case ZFS_PROP_QUOTA: 23075378Sck153898 case ZFS_PROP_REFQUOTA: 2308789Sahrens case ZFS_PROP_RESERVATION: 23095378Sck153898 case ZFS_PROP_REFRESERVATION: 23105378Sck153898 23112082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 23122082Seschrock return (-1); 2313789Sahrens 2314789Sahrens /* 2315789Sahrens * If quota or reservation is 0, we translate this into 'none' 2316789Sahrens * (unless literal is set), and indicate that it's the default 2317789Sahrens * value. Otherwise, we print the number nicely and indicate 2318789Sahrens * that its set locally. 2319789Sahrens */ 2320789Sahrens if (val == 0) { 2321789Sahrens if (literal) 2322789Sahrens (void) strlcpy(propbuf, "0", proplen); 2323789Sahrens else 2324789Sahrens (void) strlcpy(propbuf, "none", proplen); 2325789Sahrens } else { 2326789Sahrens if (literal) 23272856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 23283912Slling (u_longlong_t)val); 2329789Sahrens else 2330789Sahrens zfs_nicenum(val, propbuf, proplen); 2331789Sahrens } 2332789Sahrens break; 2333789Sahrens 2334789Sahrens case ZFS_PROP_COMPRESSRATIO: 23352082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 23362082Seschrock return (-1); 23372856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 23382856Snd150628 val / 100, (longlong_t)val % 100); 2339789Sahrens break; 2340789Sahrens 2341789Sahrens case ZFS_PROP_TYPE: 2342789Sahrens switch (zhp->zfs_type) { 2343789Sahrens case ZFS_TYPE_FILESYSTEM: 2344789Sahrens str = "filesystem"; 2345789Sahrens break; 2346789Sahrens case ZFS_TYPE_VOLUME: 2347789Sahrens str = "volume"; 2348789Sahrens break; 2349789Sahrens case ZFS_TYPE_SNAPSHOT: 2350789Sahrens str = "snapshot"; 2351789Sahrens break; 2352789Sahrens default: 23532082Seschrock abort(); 2354789Sahrens } 2355789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2356789Sahrens break; 2357789Sahrens 2358789Sahrens case ZFS_PROP_MOUNTED: 2359789Sahrens /* 2360789Sahrens * The 'mounted' property is a pseudo-property that described 2361789Sahrens * whether the filesystem is currently mounted. Even though 2362789Sahrens * it's a boolean value, the typical values of "on" and "off" 2363789Sahrens * don't make sense, so we translate to "yes" and "no". 2364789Sahrens */ 23652082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 23662082Seschrock src, &source, &val) != 0) 23672082Seschrock return (-1); 23682082Seschrock if (val) 2369789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2370789Sahrens else 2371789Sahrens (void) strlcpy(propbuf, "no", proplen); 2372789Sahrens break; 2373789Sahrens 2374789Sahrens case ZFS_PROP_NAME: 2375789Sahrens /* 2376789Sahrens * The 'name' property is a pseudo-property derived from the 2377789Sahrens * dataset name. It is presented as a real property to simplify 2378789Sahrens * consumers. 2379789Sahrens */ 2380789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2381789Sahrens break; 2382789Sahrens 2383789Sahrens default: 23844577Sahrens switch (zfs_prop_get_type(prop)) { 23854787Sahrens case PROP_TYPE_NUMBER: 23864577Sahrens if (get_numeric_property(zhp, prop, src, 23874577Sahrens &source, &val) != 0) 23884577Sahrens return (-1); 23894577Sahrens if (literal) 23904577Sahrens (void) snprintf(propbuf, proplen, "%llu", 23914577Sahrens (u_longlong_t)val); 23924577Sahrens else 23934577Sahrens zfs_nicenum(val, propbuf, proplen); 23944577Sahrens break; 23954577Sahrens 23964787Sahrens case PROP_TYPE_STRING: 23974577Sahrens (void) strlcpy(propbuf, 23984577Sahrens getprop_string(zhp, prop, &source), proplen); 23994577Sahrens break; 24004577Sahrens 24014787Sahrens case PROP_TYPE_INDEX: 24024861Sahrens if (get_numeric_property(zhp, prop, src, 24034861Sahrens &source, &val) != 0) 24044861Sahrens return (-1); 24054861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 24064577Sahrens return (-1); 24074577Sahrens (void) strlcpy(propbuf, strval, proplen); 24084577Sahrens break; 24094577Sahrens 24104577Sahrens default: 24114577Sahrens abort(); 24124577Sahrens } 2413789Sahrens } 2414789Sahrens 2415789Sahrens get_source(zhp, src, source, statbuf, statlen); 2416789Sahrens 2417789Sahrens return (0); 2418789Sahrens } 2419789Sahrens 2420789Sahrens /* 2421789Sahrens * Utility function to get the given numeric property. Does no validation that 2422789Sahrens * the given property is the appropriate type; should only be used with 2423789Sahrens * hard-coded property types. 2424789Sahrens */ 2425789Sahrens uint64_t 2426789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2427789Sahrens { 2428789Sahrens char *source; 24292082Seschrock uint64_t val; 24302082Seschrock 24315367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 24322082Seschrock 24332082Seschrock return (val); 2434789Sahrens } 2435789Sahrens 24365713Srm160521 int 24375713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 24385713Srm160521 { 24395713Srm160521 char buf[64]; 24405713Srm160521 24415713Srm160521 zfs_nicenum(val, buf, sizeof (buf)); 24425713Srm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 24435713Srm160521 } 24445713Srm160521 2445789Sahrens /* 2446789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2447789Sahrens */ 2448789Sahrens int 2449789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 24505094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2451789Sahrens { 2452789Sahrens char *source; 2453789Sahrens 2454789Sahrens /* 2455789Sahrens * Check to see if this property applies to our object 2456789Sahrens */ 24574849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 24583237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 24592082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 24602082Seschrock zfs_prop_to_name(prop))); 24614849Sahrens } 2462789Sahrens 2463789Sahrens if (src) 24645094Slling *src = ZPROP_SRC_NONE; 2465789Sahrens 24662082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 24672082Seschrock return (-1); 2468789Sahrens 2469789Sahrens get_source(zhp, src, source, statbuf, statlen); 2470789Sahrens 2471789Sahrens return (0); 2472789Sahrens } 2473789Sahrens 2474789Sahrens /* 2475789Sahrens * Returns the name of the given zfs handle. 2476789Sahrens */ 2477789Sahrens const char * 2478789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2479789Sahrens { 2480789Sahrens return (zhp->zfs_name); 2481789Sahrens } 2482789Sahrens 2483789Sahrens /* 2484789Sahrens * Returns the type of the given zfs handle. 2485789Sahrens */ 2486789Sahrens zfs_type_t 2487789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2488789Sahrens { 2489789Sahrens return (zhp->zfs_type); 2490789Sahrens } 2491789Sahrens 2492789Sahrens /* 24931356Seschrock * Iterate over all child filesystems 2494789Sahrens */ 2495789Sahrens int 24961356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2497789Sahrens { 2498789Sahrens zfs_cmd_t zc = { 0 }; 2499789Sahrens zfs_handle_t *nzhp; 2500789Sahrens int ret; 2501789Sahrens 25025367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 25035367Sahrens return (0); 25045367Sahrens 2505789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 25062082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2507789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2508789Sahrens /* 2509789Sahrens * Ignore private dataset names. 2510789Sahrens */ 2511789Sahrens if (dataset_name_hidden(zc.zc_name)) 2512789Sahrens continue; 2513789Sahrens 2514789Sahrens /* 2515789Sahrens * Silently ignore errors, as the only plausible explanation is 2516789Sahrens * that the pool has since been removed. 2517789Sahrens */ 25182082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 25192082Seschrock zc.zc_name)) == NULL) 2520789Sahrens continue; 2521789Sahrens 2522789Sahrens if ((ret = func(nzhp, data)) != 0) 2523789Sahrens return (ret); 2524789Sahrens } 2525789Sahrens 2526789Sahrens /* 2527789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2528789Sahrens * returned, then the underlying dataset has been removed since we 2529789Sahrens * obtained the handle. 2530789Sahrens */ 2531789Sahrens if (errno != ESRCH && errno != ENOENT) 25322082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 25332082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2534789Sahrens 25351356Seschrock return (0); 25361356Seschrock } 25371356Seschrock 25381356Seschrock /* 25391356Seschrock * Iterate over all snapshots 25401356Seschrock */ 25411356Seschrock int 25421356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 25431356Seschrock { 25441356Seschrock zfs_cmd_t zc = { 0 }; 25451356Seschrock zfs_handle_t *nzhp; 25461356Seschrock int ret; 2547789Sahrens 25485367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 25495367Sahrens return (0); 25505367Sahrens 2551789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 25522082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 25532082Seschrock &zc) == 0; 2554789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2555789Sahrens 25562082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 25572082Seschrock zc.zc_name)) == NULL) 2558789Sahrens continue; 2559789Sahrens 2560789Sahrens if ((ret = func(nzhp, data)) != 0) 2561789Sahrens return (ret); 2562789Sahrens } 2563789Sahrens 2564789Sahrens /* 2565789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2566789Sahrens * returned, then the underlying dataset has been removed since we 2567789Sahrens * obtained the handle. Silently ignore this case, and return success. 2568789Sahrens */ 2569789Sahrens if (errno != ESRCH && errno != ENOENT) 25702082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 25712082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2572789Sahrens 2573789Sahrens return (0); 2574789Sahrens } 2575789Sahrens 2576789Sahrens /* 25771356Seschrock * Iterate over all children, snapshots and filesystems 25781356Seschrock */ 25791356Seschrock int 25801356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 25811356Seschrock { 25821356Seschrock int ret; 25831356Seschrock 25841356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 25851356Seschrock return (ret); 25861356Seschrock 25871356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 25881356Seschrock } 25891356Seschrock 25901356Seschrock /* 2591789Sahrens * Given a complete name, return just the portion that refers to the parent. 2592789Sahrens * Can return NULL if this is a pool. 2593789Sahrens */ 2594789Sahrens static int 2595789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2596789Sahrens { 2597789Sahrens char *loc; 2598789Sahrens 2599789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2600789Sahrens return (-1); 2601789Sahrens 2602789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2603789Sahrens buf[loc - path] = '\0'; 2604789Sahrens 2605789Sahrens return (0); 2606789Sahrens } 2607789Sahrens 2608789Sahrens /* 26094490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 26104490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 26114490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 26124490Svb160487 * length of already existing prefix of the given path. We also fetch the 26134490Svb160487 * 'zoned' property, which is used to validate property settings when creating 26144490Svb160487 * new datasets. 2615789Sahrens */ 2616789Sahrens static int 26174490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 26184490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2619789Sahrens { 2620789Sahrens zfs_cmd_t zc = { 0 }; 2621789Sahrens char parent[ZFS_MAXNAMELEN]; 2622789Sahrens char *slash; 26231356Seschrock zfs_handle_t *zhp; 26242082Seschrock char errbuf[1024]; 26252082Seschrock 26262082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 26272082Seschrock path); 2628789Sahrens 2629789Sahrens /* get parent, and check to see if this is just a pool */ 2630789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 26312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26322082Seschrock "missing dataset name")); 26332082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2634789Sahrens } 2635789Sahrens 2636789Sahrens /* check to see if the pool exists */ 2637789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2638789Sahrens slash = parent + strlen(parent); 2639789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2640789Sahrens zc.zc_name[slash - parent] = '\0'; 26412082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2642789Sahrens errno == ENOENT) { 26432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26442082Seschrock "no such pool '%s'"), zc.zc_name); 26452082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2646789Sahrens } 2647789Sahrens 2648789Sahrens /* check to see if the parent dataset exists */ 26494490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 26504490Svb160487 if (errno == ENOENT && accept_ancestor) { 26514490Svb160487 /* 26524490Svb160487 * Go deeper to find an ancestor, give up on top level. 26534490Svb160487 */ 26544490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 26554490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26564490Svb160487 "no such pool '%s'"), zc.zc_name); 26574490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 26584490Svb160487 } 26594490Svb160487 } else if (errno == ENOENT) { 26602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26612082Seschrock "parent does not exist")); 26622082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 26634490Svb160487 } else 26642082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2665789Sahrens } 2666789Sahrens 26672676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2668789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 26692676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 26702082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 26711356Seschrock zfs_close(zhp); 2672789Sahrens return (-1); 2673789Sahrens } 2674789Sahrens 2675789Sahrens /* make sure parent is a filesystem */ 26761356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 26772082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26782082Seschrock "parent is not a filesystem")); 26792082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 26801356Seschrock zfs_close(zhp); 2681789Sahrens return (-1); 2682789Sahrens } 2683789Sahrens 26841356Seschrock zfs_close(zhp); 26854490Svb160487 if (prefixlen != NULL) 26864490Svb160487 *prefixlen = strlen(parent); 26874490Svb160487 return (0); 26884490Svb160487 } 26894490Svb160487 26904490Svb160487 /* 26914490Svb160487 * Finds whether the dataset of the given type(s) exists. 26924490Svb160487 */ 26934490Svb160487 boolean_t 26944490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 26954490Svb160487 { 26964490Svb160487 zfs_handle_t *zhp; 26974490Svb160487 26985326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 26994490Svb160487 return (B_FALSE); 27004490Svb160487 27014490Svb160487 /* 27024490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 27034490Svb160487 */ 27044490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 27054490Svb160487 int ds_type = zhp->zfs_type; 27064490Svb160487 27074490Svb160487 zfs_close(zhp); 27084490Svb160487 if (types & ds_type) 27094490Svb160487 return (B_TRUE); 27104490Svb160487 } 27114490Svb160487 return (B_FALSE); 27124490Svb160487 } 27134490Svb160487 27144490Svb160487 /* 27155367Sahrens * Given a path to 'target', create all the ancestors between 27165367Sahrens * the prefixlen portion of the path, and the target itself. 27175367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 27185367Sahrens */ 27195367Sahrens int 27205367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 27215367Sahrens { 27225367Sahrens zfs_handle_t *h; 27235367Sahrens char *cp; 27245367Sahrens const char *opname; 27255367Sahrens 27265367Sahrens /* make sure prefix exists */ 27275367Sahrens cp = target + prefixlen; 27285367Sahrens if (*cp != '/') { 27295367Sahrens assert(strchr(cp, '/') == NULL); 27305367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 27315367Sahrens } else { 27325367Sahrens *cp = '\0'; 27335367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 27345367Sahrens *cp = '/'; 27355367Sahrens } 27365367Sahrens if (h == NULL) 27375367Sahrens return (-1); 27385367Sahrens zfs_close(h); 27395367Sahrens 27405367Sahrens /* 27415367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 27425367Sahrens * up to the prefixlen-long one. 27435367Sahrens */ 27445367Sahrens for (cp = target + prefixlen + 1; 27455367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 27465367Sahrens char *logstr; 27475367Sahrens 27485367Sahrens *cp = '\0'; 27495367Sahrens 27505367Sahrens h = make_dataset_handle(hdl, target); 27515367Sahrens if (h) { 27525367Sahrens /* it already exists, nothing to do here */ 27535367Sahrens zfs_close(h); 27545367Sahrens continue; 27555367Sahrens } 27565367Sahrens 27575367Sahrens logstr = hdl->libzfs_log_str; 27585367Sahrens hdl->libzfs_log_str = NULL; 27595367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 27605367Sahrens NULL) != 0) { 27615367Sahrens hdl->libzfs_log_str = logstr; 27625367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 27635367Sahrens goto ancestorerr; 27645367Sahrens } 27655367Sahrens 27665367Sahrens hdl->libzfs_log_str = logstr; 27675367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 27685367Sahrens if (h == NULL) { 27695367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 27705367Sahrens goto ancestorerr; 27715367Sahrens } 27725367Sahrens 27735367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 27745367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 27755367Sahrens goto ancestorerr; 27765367Sahrens } 27775367Sahrens 27785367Sahrens if (zfs_share(h) != 0) { 27795367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 27805367Sahrens goto ancestorerr; 27815367Sahrens } 27825367Sahrens 27835367Sahrens zfs_close(h); 27845367Sahrens } 27855367Sahrens 27865367Sahrens return (0); 27875367Sahrens 27885367Sahrens ancestorerr: 27895367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27905367Sahrens "failed to %s ancestor '%s'"), opname, target); 27915367Sahrens return (-1); 27925367Sahrens } 27935367Sahrens 27945367Sahrens /* 27954490Svb160487 * Creates non-existing ancestors of the given path. 27964490Svb160487 */ 27974490Svb160487 int 27984490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 27994490Svb160487 { 28004490Svb160487 int prefix; 28014490Svb160487 uint64_t zoned; 28024490Svb160487 char *path_copy; 28034490Svb160487 int rc; 28044490Svb160487 28054490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 28064490Svb160487 return (-1); 28074490Svb160487 28084490Svb160487 if ((path_copy = strdup(path)) != NULL) { 28094490Svb160487 rc = create_parents(hdl, path_copy, prefix); 28104490Svb160487 free(path_copy); 28114490Svb160487 } 28124490Svb160487 if (path_copy == NULL || rc != 0) 28134490Svb160487 return (-1); 28144490Svb160487 2815789Sahrens return (0); 2816789Sahrens } 2817789Sahrens 2818789Sahrens /* 28192676Seschrock * Create a new filesystem or volume. 2820789Sahrens */ 2821789Sahrens int 28222082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 28232676Seschrock nvlist_t *props) 2824789Sahrens { 2825789Sahrens zfs_cmd_t zc = { 0 }; 2826789Sahrens int ret; 2827789Sahrens uint64_t size = 0; 2828789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 28292082Seschrock char errbuf[1024]; 28302676Seschrock uint64_t zoned; 28312082Seschrock 28322082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28332082Seschrock "cannot create '%s'"), path); 2834789Sahrens 2835789Sahrens /* validate the path, taking care to note the extended error message */ 28365326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 28372082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2838789Sahrens 2839789Sahrens /* validate parents exist */ 28404490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2841789Sahrens return (-1); 2842789Sahrens 2843789Sahrens /* 2844789Sahrens * The failure modes when creating a dataset of a different type over 2845789Sahrens * one that already exists is a little strange. In particular, if you 2846789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2847789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2848789Sahrens * first try to see if the dataset exists. 2849789Sahrens */ 2850789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 28515094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 28522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28532082Seschrock "dataset already exists")); 28542082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2855789Sahrens } 2856789Sahrens 2857789Sahrens if (type == ZFS_TYPE_VOLUME) 2858789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2859789Sahrens else 2860789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2861789Sahrens 28625094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 28633912Slling zoned, NULL, errbuf)) == 0) 28642676Seschrock return (-1); 28652676Seschrock 2866789Sahrens if (type == ZFS_TYPE_VOLUME) { 28671133Seschrock /* 28681133Seschrock * If we are creating a volume, the size and block size must 28691133Seschrock * satisfy a few restraints. First, the blocksize must be a 28701133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 28711133Seschrock * volsize must be a multiple of the block size, and cannot be 28721133Seschrock * zero. 28731133Seschrock */ 28742676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 28752676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 28762676Seschrock nvlist_free(props); 28772082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28782676Seschrock "missing volume size")); 28792676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2880789Sahrens } 2881789Sahrens 28822676Seschrock if ((ret = nvlist_lookup_uint64(props, 28832676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 28842676Seschrock &blocksize)) != 0) { 28852676Seschrock if (ret == ENOENT) { 28862676Seschrock blocksize = zfs_prop_default_numeric( 28872676Seschrock ZFS_PROP_VOLBLOCKSIZE); 28882676Seschrock } else { 28892676Seschrock nvlist_free(props); 28902676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28912676Seschrock "missing volume block size")); 28922676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 28932676Seschrock } 28942676Seschrock } 28952676Seschrock 28962676Seschrock if (size == 0) { 28972676Seschrock nvlist_free(props); 28982082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28992676Seschrock "volume size cannot be zero")); 29002676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29011133Seschrock } 29021133Seschrock 29031133Seschrock if (size % blocksize != 0) { 29042676Seschrock nvlist_free(props); 29052082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29062676Seschrock "volume size must be a multiple of volume block " 29072676Seschrock "size")); 29082676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29091133Seschrock } 2910789Sahrens } 2911789Sahrens 29125094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 29132676Seschrock return (-1); 29142676Seschrock nvlist_free(props); 29152676Seschrock 2916789Sahrens /* create the dataset */ 29174543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2918789Sahrens 29193912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 29202082Seschrock ret = zvol_create_link(hdl, path); 29213912Slling if (ret) { 29223912Slling (void) zfs_standard_error(hdl, errno, 29233912Slling dgettext(TEXT_DOMAIN, 29243912Slling "Volume successfully created, but device links " 29253912Slling "were not created")); 29263912Slling zcmd_free_nvlists(&zc); 29273912Slling return (-1); 29283912Slling } 29293912Slling } 2930789Sahrens 29312676Seschrock zcmd_free_nvlists(&zc); 29322676Seschrock 2933789Sahrens /* check for failure */ 2934789Sahrens if (ret != 0) { 2935789Sahrens char parent[ZFS_MAXNAMELEN]; 2936789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2937789Sahrens 2938789Sahrens switch (errno) { 2939789Sahrens case ENOENT: 29402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29412082Seschrock "no such parent '%s'"), parent); 29422082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2943789Sahrens 2944789Sahrens case EINVAL: 29452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29463413Smmusante "parent '%s' is not a filesystem"), parent); 29472082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2948789Sahrens 2949789Sahrens case EDOM: 29502082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29512676Seschrock "volume block size must be power of 2 from " 29522676Seschrock "%u to %uk"), 2953789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2954789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 29552082Seschrock 29562676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29572082Seschrock 29584603Sahrens case ENOTSUP: 29594603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29604603Sahrens "pool must be upgraded to set this " 29614603Sahrens "property or value")); 29624603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 2963789Sahrens #ifdef _ILP32 2964789Sahrens case EOVERFLOW: 2965789Sahrens /* 2966789Sahrens * This platform can't address a volume this big. 2967789Sahrens */ 29682082Seschrock if (type == ZFS_TYPE_VOLUME) 29692082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 29702082Seschrock errbuf)); 2971789Sahrens #endif 29722082Seschrock /* FALLTHROUGH */ 2973789Sahrens default: 29742082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2975789Sahrens } 2976789Sahrens } 2977789Sahrens 2978789Sahrens return (0); 2979789Sahrens } 2980789Sahrens 2981789Sahrens /* 2982789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2983789Sahrens * isn't mounted, and that there are no active dependents. 2984789Sahrens */ 2985789Sahrens int 2986789Sahrens zfs_destroy(zfs_handle_t *zhp) 2987789Sahrens { 2988789Sahrens zfs_cmd_t zc = { 0 }; 2989789Sahrens 2990789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2991789Sahrens 29922676Seschrock if (ZFS_IS_VOLUME(zhp)) { 29933126Sahl /* 29944543Smarks * If user doesn't have permissions to unshare volume, then 29954543Smarks * abort the request. This would only happen for a 29964543Smarks * non-privileged user. 29973126Sahl */ 29984543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 29994543Smarks return (-1); 30004543Smarks } 30013126Sahl 30022082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3003789Sahrens return (-1); 3004789Sahrens 3005789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3006789Sahrens } else { 3007789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3008789Sahrens } 3009789Sahrens 30104543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 30113237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 30122082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 30132082Seschrock zhp->zfs_name)); 30142199Sahrens } 3015789Sahrens 3016789Sahrens remove_mountpoint(zhp); 3017789Sahrens 3018789Sahrens return (0); 3019789Sahrens } 3020789Sahrens 30212199Sahrens struct destroydata { 30222199Sahrens char *snapname; 30232199Sahrens boolean_t gotone; 30243265Sahrens boolean_t closezhp; 30252199Sahrens }; 30262199Sahrens 30272199Sahrens static int 30282199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 30292199Sahrens { 30302199Sahrens struct destroydata *dd = arg; 30312199Sahrens zfs_handle_t *szhp; 30322199Sahrens char name[ZFS_MAXNAMELEN]; 30333265Sahrens boolean_t closezhp = dd->closezhp; 30343265Sahrens int rv; 30352199Sahrens 30362676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 30372676Seschrock (void) strlcat(name, "@", sizeof (name)); 30382676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 30392199Sahrens 30402199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 30412199Sahrens if (szhp) { 30422199Sahrens dd->gotone = B_TRUE; 30432199Sahrens zfs_close(szhp); 30442199Sahrens } 30452199Sahrens 30462199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 30472199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 30482199Sahrens /* 30492199Sahrens * NB: this is simply a best-effort. We don't want to 30502199Sahrens * return an error, because then we wouldn't visit all 30512199Sahrens * the volumes. 30522199Sahrens */ 30532199Sahrens } 30542199Sahrens 30553265Sahrens dd->closezhp = B_TRUE; 30563265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 30573265Sahrens if (closezhp) 30583265Sahrens zfs_close(zhp); 30593265Sahrens return (rv); 30602199Sahrens } 30612199Sahrens 30622199Sahrens /* 30632199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 30642199Sahrens */ 30652199Sahrens int 30662199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 30672199Sahrens { 30682199Sahrens zfs_cmd_t zc = { 0 }; 30692199Sahrens int ret; 30702199Sahrens struct destroydata dd = { 0 }; 30712199Sahrens 30722199Sahrens dd.snapname = snapname; 30732199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 30742199Sahrens 30752199Sahrens if (!dd.gotone) { 30763237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 30772199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 30782199Sahrens zhp->zfs_name, snapname)); 30792199Sahrens } 30802199Sahrens 30812199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 30822676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 30832199Sahrens 30844543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 30852199Sahrens if (ret != 0) { 30862199Sahrens char errbuf[1024]; 30872199Sahrens 30882199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30892199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 30902199Sahrens 30912199Sahrens switch (errno) { 30922199Sahrens case EEXIST: 30932199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30942199Sahrens "snapshot is cloned")); 30952199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 30962199Sahrens 30972199Sahrens default: 30982199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 30992199Sahrens errbuf)); 31002199Sahrens } 31012199Sahrens } 31022199Sahrens 31032199Sahrens return (0); 31042199Sahrens } 31052199Sahrens 3106789Sahrens /* 3107789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3108789Sahrens */ 3109789Sahrens int 31102676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3111789Sahrens { 3112789Sahrens zfs_cmd_t zc = { 0 }; 3113789Sahrens char parent[ZFS_MAXNAMELEN]; 3114789Sahrens int ret; 31152082Seschrock char errbuf[1024]; 31162082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 31172676Seschrock zfs_type_t type; 31182676Seschrock uint64_t zoned; 3119789Sahrens 3120789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3121789Sahrens 31222082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31232082Seschrock "cannot create '%s'"), target); 31242082Seschrock 3125789Sahrens /* validate the target name */ 31265326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 31272082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3128789Sahrens 3129789Sahrens /* validate parents exist */ 31304490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3131789Sahrens return (-1); 3132789Sahrens 3133789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3134789Sahrens 3135789Sahrens /* do the clone */ 31362676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3137789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 31382744Snn35248 type = ZFS_TYPE_VOLUME; 31392676Seschrock } else { 3140789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 31412744Snn35248 type = ZFS_TYPE_FILESYSTEM; 31422676Seschrock } 31432676Seschrock 31442676Seschrock if (props) { 31455094Slling if ((props = zfs_validate_properties(hdl, type, props, 31463912Slling zoned, zhp, errbuf)) == NULL) 31472676Seschrock return (-1); 31482676Seschrock 31495094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 31502676Seschrock nvlist_free(props); 31512676Seschrock return (-1); 31522676Seschrock } 31532676Seschrock 31542676Seschrock nvlist_free(props); 31552676Seschrock } 3156789Sahrens 3157789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 31582676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 31594543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3160789Sahrens 31612676Seschrock zcmd_free_nvlists(&zc); 31622676Seschrock 3163789Sahrens if (ret != 0) { 3164789Sahrens switch (errno) { 3165789Sahrens 3166789Sahrens case ENOENT: 3167789Sahrens /* 3168789Sahrens * The parent doesn't exist. We should have caught this 3169789Sahrens * above, but there may a race condition that has since 3170789Sahrens * destroyed the parent. 3171789Sahrens * 3172789Sahrens * At this point, we don't know whether it's the source 3173789Sahrens * that doesn't exist anymore, or whether the target 3174789Sahrens * dataset doesn't exist. 3175789Sahrens */ 31762082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31772082Seschrock "no such parent '%s'"), parent); 31782082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 31792082Seschrock 31802082Seschrock case EXDEV: 31812082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 31822082Seschrock "source and target pools differ")); 31832082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 31842082Seschrock errbuf)); 31852082Seschrock 31862082Seschrock default: 31872082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 31882082Seschrock errbuf)); 31892082Seschrock } 31902676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 31912082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 31922082Seschrock } 31932082Seschrock 31942082Seschrock return (ret); 31952082Seschrock } 31962082Seschrock 31972082Seschrock typedef struct promote_data { 31982082Seschrock char cb_mountpoint[MAXPATHLEN]; 31992082Seschrock const char *cb_target; 32002082Seschrock const char *cb_errbuf; 32012082Seschrock uint64_t cb_pivot_txg; 32022082Seschrock } promote_data_t; 32032082Seschrock 32042082Seschrock static int 32052082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 32062082Seschrock { 32072082Seschrock promote_data_t *pd = data; 32082082Seschrock zfs_handle_t *szhp; 32092082Seschrock char snapname[MAXPATHLEN]; 32103265Sahrens int rv = 0; 32112082Seschrock 32122082Seschrock /* We don't care about snapshots after the pivot point */ 32133265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 32143265Sahrens zfs_close(zhp); 32152082Seschrock return (0); 32163265Sahrens } 32172082Seschrock 32182417Sahrens /* Remove the device link if it's a zvol. */ 32192676Seschrock if (ZFS_IS_VOLUME(zhp)) 32202417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 32212082Seschrock 32222082Seschrock /* Check for conflicting names */ 32232676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 32242676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 32252082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 32262082Seschrock if (szhp != NULL) { 32272082Seschrock zfs_close(szhp); 32282082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 32292082Seschrock "snapshot name '%s' from origin \n" 32302082Seschrock "conflicts with '%s' from target"), 32312082Seschrock zhp->zfs_name, snapname); 32323265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 32332082Seschrock } 32343265Sahrens zfs_close(zhp); 32353265Sahrens return (rv); 32362082Seschrock } 32372082Seschrock 32382417Sahrens static int 32392417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 32402417Sahrens { 32412417Sahrens promote_data_t *pd = data; 32422417Sahrens 32432417Sahrens /* We don't care about snapshots after the pivot point */ 32443265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 32453265Sahrens /* Create the device link if it's a zvol. */ 32463265Sahrens if (ZFS_IS_VOLUME(zhp)) 32473265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 32483265Sahrens } 32493265Sahrens 32503265Sahrens zfs_close(zhp); 32512417Sahrens return (0); 32522417Sahrens } 32532417Sahrens 32542082Seschrock /* 32552082Seschrock * Promotes the given clone fs to be the clone parent. 32562082Seschrock */ 32572082Seschrock int 32582082Seschrock zfs_promote(zfs_handle_t *zhp) 32592082Seschrock { 32602082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 32612082Seschrock zfs_cmd_t zc = { 0 }; 32622082Seschrock char parent[MAXPATHLEN]; 32632082Seschrock char *cp; 32642082Seschrock int ret; 32652082Seschrock zfs_handle_t *pzhp; 32662082Seschrock promote_data_t pd; 32672082Seschrock char errbuf[1024]; 32682082Seschrock 32692082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32702082Seschrock "cannot promote '%s'"), zhp->zfs_name); 32712082Seschrock 32722082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 32732082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32742082Seschrock "snapshots can not be promoted")); 32752082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 32762082Seschrock } 32772082Seschrock 32785367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 32792082Seschrock if (parent[0] == '\0') { 32802082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32812082Seschrock "not a cloned filesystem")); 32822082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 32832082Seschrock } 32842082Seschrock cp = strchr(parent, '@'); 32852082Seschrock *cp = '\0'; 32862082Seschrock 32872082Seschrock /* Walk the snapshots we will be moving */ 32885367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 32892082Seschrock if (pzhp == NULL) 32902082Seschrock return (-1); 32912082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 32922082Seschrock zfs_close(pzhp); 32932082Seschrock pd.cb_target = zhp->zfs_name; 32942082Seschrock pd.cb_errbuf = errbuf; 32955094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 32962082Seschrock if (pzhp == NULL) 32972082Seschrock return (-1); 32982082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 32992082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 33002082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 33012417Sahrens if (ret != 0) { 33022417Sahrens zfs_close(pzhp); 33032082Seschrock return (-1); 33042417Sahrens } 33052082Seschrock 33062082Seschrock /* issue the ioctl */ 33075367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 33082676Seschrock sizeof (zc.zc_value)); 33092082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33104543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 33112082Seschrock 33122082Seschrock if (ret != 0) { 33132417Sahrens int save_errno = errno; 33142417Sahrens 33152417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 33162417Sahrens zfs_close(pzhp); 33172417Sahrens 33182417Sahrens switch (save_errno) { 3319789Sahrens case EEXIST: 3320789Sahrens /* 33212082Seschrock * There is a conflicting snapshot name. We 33222082Seschrock * should have caught this above, but they could 33232082Seschrock * have renamed something in the mean time. 3324789Sahrens */ 33252082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33262082Seschrock "conflicting snapshot name from parent '%s'"), 33272082Seschrock parent); 33282082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3329789Sahrens 3330789Sahrens default: 33312417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3332789Sahrens } 33332417Sahrens } else { 33342417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3335789Sahrens } 3336789Sahrens 33372417Sahrens zfs_close(pzhp); 3338789Sahrens return (ret); 3339789Sahrens } 3340789Sahrens 33414007Smmusante struct createdata { 33424007Smmusante const char *cd_snapname; 33434007Smmusante int cd_ifexists; 33444007Smmusante }; 33454007Smmusante 33462199Sahrens static int 33472199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 33482199Sahrens { 33494007Smmusante struct createdata *cd = arg; 33502676Seschrock int ret; 33512199Sahrens 33522199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33532199Sahrens char name[MAXPATHLEN]; 33542199Sahrens 33552676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 33562676Seschrock (void) strlcat(name, "@", sizeof (name)); 33574007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 33584007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 33594007Smmusante cd->cd_ifexists); 33602199Sahrens /* 33612199Sahrens * NB: this is simply a best-effort. We don't want to 33622199Sahrens * return an error, because then we wouldn't visit all 33632199Sahrens * the volumes. 33642199Sahrens */ 33652199Sahrens } 33662676Seschrock 33674007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 33682676Seschrock 33692676Seschrock zfs_close(zhp); 33702676Seschrock 33712676Seschrock return (ret); 33722199Sahrens } 33732199Sahrens 3374789Sahrens /* 33753504Sahl * Takes a snapshot of the given dataset. 3376789Sahrens */ 3377789Sahrens int 33782199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3379789Sahrens { 3380789Sahrens const char *delim; 3381789Sahrens char *parent; 3382789Sahrens zfs_handle_t *zhp; 3383789Sahrens zfs_cmd_t zc = { 0 }; 3384789Sahrens int ret; 33852082Seschrock char errbuf[1024]; 33862082Seschrock 33872082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33882082Seschrock "cannot snapshot '%s'"), path); 33892082Seschrock 33902082Seschrock /* validate the target name */ 33915326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 33922082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3393789Sahrens 3394789Sahrens /* make sure the parent exists and is of the appropriate type */ 33952199Sahrens delim = strchr(path, '@'); 33962082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 33972082Seschrock return (-1); 3398789Sahrens (void) strncpy(parent, path, delim - path); 3399789Sahrens parent[delim - path] = '\0'; 3400789Sahrens 34012082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3402789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3403789Sahrens free(parent); 3404789Sahrens return (-1); 3405789Sahrens } 3406789Sahrens 34072199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 34082676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 34094543Smarks if (ZFS_IS_VOLUME(zhp)) 34104543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 34114543Smarks else 34124543Smarks zc.zc_objset_type = DMU_OST_ZFS; 34132199Sahrens zc.zc_cookie = recursive; 34144543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 34152199Sahrens 34162199Sahrens /* 34172199Sahrens * if it was recursive, the one that actually failed will be in 34182199Sahrens * zc.zc_name. 34192199Sahrens */ 34204543Smarks if (ret != 0) 34214543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34224543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 34234543Smarks 34242199Sahrens if (ret == 0 && recursive) { 34254007Smmusante struct createdata cd; 34264007Smmusante 34274007Smmusante cd.cd_snapname = delim + 1; 34284007Smmusante cd.cd_ifexists = B_FALSE; 34294007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 34302199Sahrens } 3431789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 34322082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 34332199Sahrens if (ret != 0) { 34344543Smarks (void) zfs_standard_error(hdl, errno, 34354543Smarks dgettext(TEXT_DOMAIN, 34364543Smarks "Volume successfully snapshotted, but device links " 34374543Smarks "were not created")); 34384543Smarks free(parent); 34394543Smarks zfs_close(zhp); 34404543Smarks return (-1); 34412199Sahrens } 3442789Sahrens } 3443789Sahrens 34442082Seschrock if (ret != 0) 34452082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3446789Sahrens 3447789Sahrens free(parent); 3448789Sahrens zfs_close(zhp); 3449789Sahrens 3450789Sahrens return (ret); 3451789Sahrens } 3452789Sahrens 3453789Sahrens /* 34541294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 34551294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 34561294Slling * is a dependent and we should just destroy it without checking the transaction 34571294Slling * group. 3458789Sahrens */ 34591294Slling typedef struct rollback_data { 34601294Slling const char *cb_target; /* the snapshot */ 34611294Slling uint64_t cb_create; /* creation time reference */ 34625749Sahrens boolean_t cb_error; 34632082Seschrock boolean_t cb_dependent; 34645749Sahrens boolean_t cb_force; 34651294Slling } rollback_data_t; 34661294Slling 34671294Slling static int 34681294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 34691294Slling { 34701294Slling rollback_data_t *cbp = data; 34711294Slling 34721294Slling if (!cbp->cb_dependent) { 34731294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 34741294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 34751294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 34761294Slling cbp->cb_create) { 34774543Smarks char *logstr; 34781294Slling 34792082Seschrock cbp->cb_dependent = B_TRUE; 34805446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 34815446Sahrens rollback_destroy, cbp); 34822082Seschrock cbp->cb_dependent = B_FALSE; 34831294Slling 34844543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 34854543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 34865446Sahrens cbp->cb_error |= zfs_destroy(zhp); 34874543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 34881294Slling } 34891294Slling } else { 34905749Sahrens /* We must destroy this clone; first unmount it */ 34915749Sahrens prop_changelist_t *clp; 34925749Sahrens 34935749Sahrens clp = changelist_gather(zhp, ZFS_PROP_NAME, 34945749Sahrens cbp->cb_force ? MS_FORCE: 0); 34955749Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 34965749Sahrens cbp->cb_error = B_TRUE; 34975749Sahrens zfs_close(zhp); 34985749Sahrens return (0); 34995749Sahrens } 35005749Sahrens if (zfs_destroy(zhp) != 0) 35015749Sahrens cbp->cb_error = B_TRUE; 35025749Sahrens else 35035749Sahrens changelist_remove(clp, zhp->zfs_name); 35045751Sahrens (void) changelist_postfix(clp); 35055749Sahrens changelist_free(clp); 35061294Slling } 35071294Slling 35081294Slling zfs_close(zhp); 35091294Slling return (0); 35101294Slling } 35111294Slling 35121294Slling /* 35135446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 35145446Sahrens * data changes since then and making it the active dataset. 35155446Sahrens * 35165446Sahrens * Any snapshots more recent than the target are destroyed, along with 35175446Sahrens * their dependents. 35181294Slling */ 35195446Sahrens int 35205749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3521789Sahrens { 35225446Sahrens rollback_data_t cb = { 0 }; 35235446Sahrens int err; 3524789Sahrens zfs_cmd_t zc = { 0 }; 35255713Srm160521 boolean_t restore_resv = 0; 35265713Srm160521 uint64_t old_volsize, new_volsize; 35275713Srm160521 zfs_prop_t resv_prop; 3528789Sahrens 3529789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3530789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3531789Sahrens 35325446Sahrens /* 35335446Sahrens * Destroy all recent snapshots and its dependends. 35345446Sahrens */ 35355749Sahrens cb.cb_force = force; 35365446Sahrens cb.cb_target = snap->zfs_name; 35375446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 35385446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 35395446Sahrens 35405749Sahrens if (cb.cb_error) 35415749Sahrens return (-1); 35425446Sahrens 35435446Sahrens /* 35445446Sahrens * Now that we have verified that the snapshot is the latest, 35455446Sahrens * rollback to the given snapshot. 35465446Sahrens */ 35475446Sahrens 35485713Srm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 35495713Srm160521 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 35505713Srm160521 return (-1); 35515713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 35525713Srm160521 return (-1); 35535713Srm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 35545713Srm160521 restore_resv = 35555713Srm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 35565713Srm160521 } 3557789Sahrens 3558789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3559789Sahrens 35602676Seschrock if (ZFS_IS_VOLUME(zhp)) 3561789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3562789Sahrens else 3563789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3564789Sahrens 3565789Sahrens /* 35665446Sahrens * We rely on zfs_iter_children() to verify that there are no 35675446Sahrens * newer snapshots for the given dataset. Therefore, we can 35685446Sahrens * simply pass the name on to the ioctl() call. There is still 35695446Sahrens * an unlikely race condition where the user has taken a 35705446Sahrens * snapshot since we verified that this was the most recent. 35715713Srm160521 * 3572789Sahrens */ 35735446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 35743237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 35752082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 35762082Seschrock zhp->zfs_name); 35775717Srm160521 return (err); 35785717Srm160521 } 35795713Srm160521 35805713Srm160521 /* 35815713Srm160521 * For volumes, if the pre-rollback volsize matched the pre- 35825713Srm160521 * rollback reservation and the volsize has changed then set 35835713Srm160521 * the reservation property to the post-rollback volsize. 35845713Srm160521 * Make a new handle since the rollback closed the dataset. 35855713Srm160521 */ 35865717Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 35875717Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 35885717Srm160521 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) { 35895717Srm160521 zfs_close(zhp); 35905713Srm160521 return (err); 35915717Srm160521 } 35925713Srm160521 if (restore_resv) { 35935713Srm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 35945713Srm160521 if (old_volsize != new_volsize) 35955717Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 35965717Srm160521 new_volsize); 35975713Srm160521 } 35985713Srm160521 zfs_close(zhp); 3599789Sahrens } 36005446Sahrens return (err); 36011294Slling } 36021294Slling 36031294Slling /* 3604789Sahrens * Iterate over all dependents for a given dataset. This includes both 3605789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3606789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3607789Sahrens * libzfs_graph.c. 3608789Sahrens */ 3609789Sahrens int 36102474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 36112474Seschrock zfs_iter_f func, void *data) 3612789Sahrens { 3613789Sahrens char **dependents; 3614789Sahrens size_t count; 3615789Sahrens int i; 3616789Sahrens zfs_handle_t *child; 3617789Sahrens int ret = 0; 3618789Sahrens 36192474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 36202474Seschrock &dependents, &count) != 0) 36212474Seschrock return (-1); 36222474Seschrock 3623789Sahrens for (i = 0; i < count; i++) { 36242082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 36252082Seschrock dependents[i])) == NULL) 3626789Sahrens continue; 3627789Sahrens 3628789Sahrens if ((ret = func(child, data)) != 0) 3629789Sahrens break; 3630789Sahrens } 3631789Sahrens 3632789Sahrens for (i = 0; i < count; i++) 3633789Sahrens free(dependents[i]); 3634789Sahrens free(dependents); 3635789Sahrens 3636789Sahrens return (ret); 3637789Sahrens } 3638789Sahrens 3639789Sahrens /* 3640789Sahrens * Renames the given dataset. 3641789Sahrens */ 3642789Sahrens int 36434490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3644789Sahrens { 3645789Sahrens int ret; 3646789Sahrens zfs_cmd_t zc = { 0 }; 3647789Sahrens char *delim; 36484007Smmusante prop_changelist_t *cl = NULL; 36494007Smmusante zfs_handle_t *zhrp = NULL; 36504007Smmusante char *parentname = NULL; 3651789Sahrens char parent[ZFS_MAXNAMELEN]; 36522082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 36532082Seschrock char errbuf[1024]; 3654789Sahrens 3655789Sahrens /* if we have the same exact name, just return success */ 3656789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3657789Sahrens return (0); 3658789Sahrens 36592082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36602082Seschrock "cannot rename to '%s'"), target); 36612082Seschrock 3662789Sahrens /* 3663789Sahrens * Make sure the target name is valid 3664789Sahrens */ 3665789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 36662665Snd150628 if ((strchr(target, '@') == NULL) || 36672665Snd150628 *target == '@') { 36682665Snd150628 /* 36692665Snd150628 * Snapshot target name is abbreviated, 36702665Snd150628 * reconstruct full dataset name 36712665Snd150628 */ 36722665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 36732665Snd150628 sizeof (parent)); 36742665Snd150628 delim = strchr(parent, '@'); 36752665Snd150628 if (strchr(target, '@') == NULL) 36762665Snd150628 *(++delim) = '\0'; 36772665Snd150628 else 36782665Snd150628 *delim = '\0'; 36792665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 36802665Snd150628 target = parent; 36812665Snd150628 } else { 36822665Snd150628 /* 36832665Snd150628 * Make sure we're renaming within the same dataset. 36842665Snd150628 */ 36852665Snd150628 delim = strchr(target, '@'); 36862665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 36872665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 36882665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36892665Snd150628 "snapshots must be part of same " 36902665Snd150628 "dataset")); 36912665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 36923912Slling errbuf)); 36932665Snd150628 } 3694789Sahrens } 36955326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 36962665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3697789Sahrens } else { 36984007Smmusante if (recursive) { 36994007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37004007Smmusante "recursive rename must be a snapshot")); 37014007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 37024007Smmusante } 37034007Smmusante 37045326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 37052665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37062676Seschrock uint64_t unused; 37072676Seschrock 3708789Sahrens /* validate parents */ 37094490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3710789Sahrens return (-1); 3711789Sahrens 3712789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3713789Sahrens 3714789Sahrens /* make sure we're in the same pool */ 3715789Sahrens verify((delim = strchr(target, '/')) != NULL); 3716789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3717789Sahrens zhp->zfs_name[delim - target] != '/') { 37182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37192082Seschrock "datasets must be within same pool")); 37202082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3721789Sahrens } 37222440Snd150628 37232440Snd150628 /* new name cannot be a child of the current dataset name */ 37242440Snd150628 if (strncmp(parent, zhp->zfs_name, 37253912Slling strlen(zhp->zfs_name)) == 0) { 37262440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37272440Snd150628 "New dataset name cannot be a descendent of " 37282440Snd150628 "current dataset name")); 37292440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37302440Snd150628 } 3731789Sahrens } 3732789Sahrens 37332082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 37342082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 37352082Seschrock 3736789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3737789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 37382082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37392082Seschrock "dataset is used in a non-global zone")); 37402082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3741789Sahrens } 3742789Sahrens 37434007Smmusante if (recursive) { 37444007Smmusante struct destroydata dd; 37454007Smmusante 37464183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 37474183Smmusante if (parentname == NULL) { 37484183Smmusante ret = -1; 37494183Smmusante goto error; 37504183Smmusante } 37514007Smmusante delim = strchr(parentname, '@'); 37524007Smmusante *delim = '\0'; 37535094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 37544007Smmusante if (zhrp == NULL) { 37554183Smmusante ret = -1; 37564183Smmusante goto error; 37574007Smmusante } 37584007Smmusante 37594007Smmusante dd.snapname = delim + 1; 37604007Smmusante dd.gotone = B_FALSE; 37614183Smmusante dd.closezhp = B_TRUE; 37624007Smmusante 37634007Smmusante /* We remove any zvol links prior to renaming them */ 37644007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 37654007Smmusante if (ret) { 37664007Smmusante goto error; 37674007Smmusante } 37684007Smmusante } else { 37694007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 37704007Smmusante return (-1); 37714007Smmusante 37724007Smmusante if (changelist_haszonedchild(cl)) { 37734007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37744007Smmusante "child dataset with inherited mountpoint is used " 37754007Smmusante "in a non-global zone")); 37764007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 37774007Smmusante goto error; 37784007Smmusante } 37794007Smmusante 37804007Smmusante if ((ret = changelist_prefix(cl)) != 0) 37814007Smmusante goto error; 3782789Sahrens } 3783789Sahrens 37842676Seschrock if (ZFS_IS_VOLUME(zhp)) 3785789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3786789Sahrens else 3787789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3788789Sahrens 37892665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 37902676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 37912665Snd150628 37924007Smmusante zc.zc_cookie = recursive; 37934007Smmusante 37944543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 37954007Smmusante /* 37964007Smmusante * if it was recursive, the one that actually failed will 37974007Smmusante * be in zc.zc_name 37984007Smmusante */ 37994007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 38005367Sahrens "cannot rename '%s'"), zc.zc_name); 38014007Smmusante 38024007Smmusante if (recursive && errno == EEXIST) { 38034007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38044007Smmusante "a child dataset already has a snapshot " 38054007Smmusante "with the new name")); 38064801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 38074007Smmusante } else { 38084007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 38094007Smmusante } 3810789Sahrens 3811789Sahrens /* 3812789Sahrens * On failure, we still want to remount any filesystems that 3813789Sahrens * were previously mounted, so we don't alter the system state. 3814789Sahrens */ 38154007Smmusante if (recursive) { 38164007Smmusante struct createdata cd; 38174007Smmusante 38184007Smmusante /* only create links for datasets that had existed */ 38194007Smmusante cd.cd_snapname = delim + 1; 38204007Smmusante cd.cd_ifexists = B_TRUE; 38214007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 38224007Smmusante &cd); 38234007Smmusante } else { 38244007Smmusante (void) changelist_postfix(cl); 38254007Smmusante } 3826789Sahrens } else { 38274007Smmusante if (recursive) { 38284007Smmusante struct createdata cd; 38294007Smmusante 38304007Smmusante /* only create links for datasets that had existed */ 38314007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 38324007Smmusante cd.cd_ifexists = B_TRUE; 38334007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 38344007Smmusante &cd); 38354007Smmusante } else { 38364007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 38374007Smmusante ret = changelist_postfix(cl); 38384007Smmusante } 3839789Sahrens } 3840789Sahrens 3841789Sahrens error: 38424007Smmusante if (parentname) { 38434007Smmusante free(parentname); 38444007Smmusante } 38454007Smmusante if (zhrp) { 38464007Smmusante zfs_close(zhrp); 38474007Smmusante } 38484007Smmusante if (cl) { 38494007Smmusante changelist_free(cl); 38504007Smmusante } 3851789Sahrens return (ret); 3852789Sahrens } 3853789Sahrens 3854789Sahrens /* 3855789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3856789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3857789Sahrens */ 3858789Sahrens int 38592082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3860789Sahrens { 38614007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 38624007Smmusante } 38634007Smmusante 38644007Smmusante static int 38654007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 38664007Smmusante { 3867789Sahrens zfs_cmd_t zc = { 0 }; 38682082Seschrock di_devlink_handle_t dhdl; 38694543Smarks priv_set_t *priv_effective; 38704543Smarks int privileged; 3871789Sahrens 3872789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3873789Sahrens 3874789Sahrens /* 3875789Sahrens * Issue the appropriate ioctl. 3876789Sahrens */ 38772082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3878789Sahrens switch (errno) { 3879789Sahrens case EEXIST: 3880789Sahrens /* 3881789Sahrens * Silently ignore the case where the link already 3882789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3883789Sahrens * times without errors. 3884789Sahrens */ 3885789Sahrens return (0); 3886789Sahrens 38874007Smmusante case ENOENT: 38884007Smmusante /* 38894007Smmusante * Dataset does not exist in the kernel. If we 38904007Smmusante * don't care (see zfs_rename), then ignore the 38914007Smmusante * error quietly. 38924007Smmusante */ 38934007Smmusante if (ifexists) { 38944007Smmusante return (0); 38954007Smmusante } 38964007Smmusante 38974007Smmusante /* FALLTHROUGH */ 38984007Smmusante 3899789Sahrens default: 39003237Slling return (zfs_standard_error_fmt(hdl, errno, 39012082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 39022082Seschrock "for '%s'"), dataset)); 3903789Sahrens } 3904789Sahrens } 3905789Sahrens 3906789Sahrens /* 39074543Smarks * If privileged call devfsadm and wait for the links to 39084543Smarks * magically appear. 39094543Smarks * Otherwise, print out an informational message. 3910789Sahrens */ 39114543Smarks 39124543Smarks priv_effective = priv_allocset(); 39134543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 39144543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 39154543Smarks priv_freeset(priv_effective); 39164543Smarks 39174543Smarks if (privileged) { 39184543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 39194543Smarks DI_MAKE_LINK)) == NULL) { 39204543Smarks zfs_error_aux(hdl, strerror(errno)); 39214543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 39224543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 39234543Smarks "for '%s'"), dataset); 39244543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 39254543Smarks return (-1); 39264543Smarks } else { 39274543Smarks (void) di_devlink_fini(&dhdl); 39284543Smarks } 3929789Sahrens } else { 39304543Smarks char pathname[MAXPATHLEN]; 39314543Smarks struct stat64 statbuf; 39324543Smarks int i; 39334543Smarks 39344543Smarks #define MAX_WAIT 10 39354543Smarks 39364543Smarks /* 39374543Smarks * This is the poor mans way of waiting for the link 39384543Smarks * to show up. If after 10 seconds we still don't 39394543Smarks * have it, then print out a message. 39404543Smarks */ 39414543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 39424543Smarks dataset); 39434543Smarks 39444543Smarks for (i = 0; i != MAX_WAIT; i++) { 39454543Smarks if (stat64(pathname, &statbuf) == 0) 39464543Smarks break; 39474543Smarks (void) sleep(1); 39484543Smarks } 39494543Smarks if (i == MAX_WAIT) 39504543Smarks (void) printf(gettext("%s may not be immediately " 39514543Smarks "available\n"), pathname); 3952789Sahrens } 3953789Sahrens 3954789Sahrens return (0); 3955789Sahrens } 3956789Sahrens 3957789Sahrens /* 3958789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 3959789Sahrens */ 3960789Sahrens int 39612082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3962789Sahrens { 3963789Sahrens zfs_cmd_t zc = { 0 }; 3964789Sahrens 3965789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3966789Sahrens 39672082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3968789Sahrens switch (errno) { 3969789Sahrens case ENXIO: 3970789Sahrens /* 3971789Sahrens * Silently ignore the case where the link no longer 3972789Sahrens * exists, so that 'zfs volfini' can be run multiple 3973789Sahrens * times without errors. 3974789Sahrens */ 3975789Sahrens return (0); 3976789Sahrens 3977789Sahrens default: 39783237Slling return (zfs_standard_error_fmt(hdl, errno, 39792082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 39802082Seschrock "links for '%s'"), dataset)); 3981789Sahrens } 3982789Sahrens } 3983789Sahrens 3984789Sahrens return (0); 3985789Sahrens } 39862676Seschrock 39872676Seschrock nvlist_t * 39882676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 39892676Seschrock { 39902676Seschrock return (zhp->zfs_user_props); 39912676Seschrock } 39922676Seschrock 39932676Seschrock /* 39943912Slling * This function is used by 'zfs list' to determine the exact set of columns to 39953912Slling * display, and their maximum widths. This does two main things: 39963912Slling * 39973912Slling * - If this is a list of all properties, then expand the list to include 39983912Slling * all native properties, and set a flag so that for each dataset we look 39993912Slling * for new unique user properties and add them to the list. 40003912Slling * 40013912Slling * - For non fixed-width properties, keep track of the maximum width seen 40023912Slling * so that we can size the column appropriately. 40033912Slling */ 40043912Slling int 40055094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 40063912Slling { 40073912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 40085094Slling zprop_list_t *entry; 40095094Slling zprop_list_t **last, **start; 40103912Slling nvlist_t *userprops, *propval; 40113912Slling nvpair_t *elem; 40123912Slling char *strval; 40133912Slling char buf[ZFS_MAXPROPLEN]; 40143912Slling 40155094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 40163912Slling return (-1); 40172676Seschrock 40182676Seschrock userprops = zfs_get_user_props(zhp); 40192676Seschrock 40202676Seschrock entry = *plp; 40212676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 40222676Seschrock /* 40232676Seschrock * Go through and add any user properties as necessary. We 40242676Seschrock * start by incrementing our list pointer to the first 40252676Seschrock * non-native property. 40262676Seschrock */ 40272676Seschrock start = plp; 40282676Seschrock while (*start != NULL) { 40295094Slling if ((*start)->pl_prop == ZPROP_INVAL) 40302676Seschrock break; 40312676Seschrock start = &(*start)->pl_next; 40322676Seschrock } 40332676Seschrock 40342676Seschrock elem = NULL; 40352676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 40362676Seschrock /* 40372676Seschrock * See if we've already found this property in our list. 40382676Seschrock */ 40392676Seschrock for (last = start; *last != NULL; 40402676Seschrock last = &(*last)->pl_next) { 40412676Seschrock if (strcmp((*last)->pl_user_prop, 40422676Seschrock nvpair_name(elem)) == 0) 40432676Seschrock break; 40442676Seschrock } 40452676Seschrock 40462676Seschrock if (*last == NULL) { 40472676Seschrock if ((entry = zfs_alloc(hdl, 40485094Slling sizeof (zprop_list_t))) == NULL || 40492676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 40502676Seschrock nvpair_name(elem)))) == NULL) { 40512676Seschrock free(entry); 40522676Seschrock return (-1); 40532676Seschrock } 40542676Seschrock 40555094Slling entry->pl_prop = ZPROP_INVAL; 40562676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 40572676Seschrock entry->pl_all = B_TRUE; 40582676Seschrock *last = entry; 40592676Seschrock } 40602676Seschrock } 40612676Seschrock } 40622676Seschrock 40632676Seschrock /* 40642676Seschrock * Now go through and check the width of any non-fixed columns 40652676Seschrock */ 40662676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 40672676Seschrock if (entry->pl_fixed) 40682676Seschrock continue; 40692676Seschrock 40705094Slling if (entry->pl_prop != ZPROP_INVAL) { 40712676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 40722676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 40732676Seschrock if (strlen(buf) > entry->pl_width) 40742676Seschrock entry->pl_width = strlen(buf); 40752676Seschrock } 40762676Seschrock } else if (nvlist_lookup_nvlist(userprops, 40772676Seschrock entry->pl_user_prop, &propval) == 0) { 40782676Seschrock verify(nvlist_lookup_string(propval, 40795094Slling ZPROP_VALUE, &strval) == 0); 40802676Seschrock if (strlen(strval) > entry->pl_width) 40812676Seschrock entry->pl_width = strlen(strval); 40822676Seschrock } 40832676Seschrock } 40842676Seschrock 40852676Seschrock return (0); 40862676Seschrock } 40874543Smarks 40884543Smarks int 40894543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 40904543Smarks { 40914543Smarks zfs_cmd_t zc = { 0 }; 40924543Smarks nvlist_t *nvp; 40934543Smarks gid_t gid; 40944543Smarks uid_t uid; 40954543Smarks const gid_t *groups; 40964543Smarks int group_cnt; 40974543Smarks int error; 40984543Smarks 40994543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 41004543Smarks return (no_memory(hdl)); 41014543Smarks 41024543Smarks uid = ucred_geteuid(cred); 41034543Smarks gid = ucred_getegid(cred); 41044543Smarks group_cnt = ucred_getgroups(cred, &groups); 41054543Smarks 41064543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 41074543Smarks return (1); 41084543Smarks 41094543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 41104543Smarks nvlist_free(nvp); 41114543Smarks return (1); 41124543Smarks } 41134543Smarks 41144543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 41154543Smarks nvlist_free(nvp); 41164543Smarks return (1); 41174543Smarks } 41184543Smarks 41194543Smarks if (nvlist_add_uint32_array(nvp, 41204543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 41214543Smarks nvlist_free(nvp); 41224543Smarks return (1); 41234543Smarks } 41244543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 41254543Smarks 41265094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 41274543Smarks return (-1); 41284543Smarks 41294543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 41304543Smarks nvlist_free(nvp); 41314543Smarks return (error); 41324543Smarks } 41334543Smarks 41344543Smarks int 41354543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 41365331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 41374543Smarks { 41384543Smarks zfs_cmd_t zc = { 0 }; 41394543Smarks int error; 41404543Smarks 41414543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 41424543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 41434543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 41444543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 41455331Samw zc.zc_share.z_sharetype = operation; 41464543Smarks zc.zc_share.z_sharemax = sharemax; 41474543Smarks 41484543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 41494543Smarks return (error); 41504543Smarks } 4151