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 /* 23*8802SSanjeev.Bagewadi@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #include <assert.h> 28789Sahrens #include <ctype.h> 29789Sahrens #include <errno.h> 30789Sahrens #include <libdevinfo.h> 31789Sahrens #include <libintl.h> 32789Sahrens #include <math.h> 33789Sahrens #include <stdio.h> 34789Sahrens #include <stdlib.h> 35789Sahrens #include <strings.h> 36789Sahrens #include <unistd.h> 375367Sahrens #include <stddef.h> 38789Sahrens #include <zone.h> 392082Seschrock #include <fcntl.h> 40789Sahrens #include <sys/mntent.h> 411294Slling #include <sys/mount.h> 424543Smarks #include <sys/avl.h> 434543Smarks #include <priv.h> 444543Smarks #include <pwd.h> 454543Smarks #include <grp.h> 464543Smarks #include <stddef.h> 474543Smarks #include <ucred.h> 48789Sahrens 49789Sahrens #include <sys/spa.h> 502676Seschrock #include <sys/zap.h> 51789Sahrens #include <libzfs.h> 52789Sahrens 53789Sahrens #include "zfs_namecheck.h" 54789Sahrens #include "zfs_prop.h" 55789Sahrens #include "libzfs_impl.h" 564543Smarks #include "zfs_deleg.h" 57789Sahrens 584007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 594007Smmusante 60789Sahrens /* 61789Sahrens * Given a single type (not a mask of types), return the type in a human 62789Sahrens * readable form. 63789Sahrens */ 64789Sahrens const char * 65789Sahrens zfs_type_to_name(zfs_type_t type) 66789Sahrens { 67789Sahrens switch (type) { 68789Sahrens case ZFS_TYPE_FILESYSTEM: 69789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 70789Sahrens case ZFS_TYPE_SNAPSHOT: 71789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 72789Sahrens case ZFS_TYPE_VOLUME: 73789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 74789Sahrens } 75789Sahrens 76789Sahrens return (NULL); 77789Sahrens } 78789Sahrens 79789Sahrens /* 80789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 81789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 82789Sahrens * We guess what the type would have been based on the path and the mask of 83789Sahrens * acceptable types. 84789Sahrens */ 85789Sahrens static const char * 86789Sahrens path_to_str(const char *path, int types) 87789Sahrens { 88789Sahrens /* 89789Sahrens * When given a single type, always report the exact type. 90789Sahrens */ 91789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 92789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 93789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 94789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 95789Sahrens if (types == ZFS_TYPE_VOLUME) 96789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 97789Sahrens 98789Sahrens /* 99789Sahrens * The user is requesting more than one type of dataset. If this is the 100789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 101789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 102789Sahrens * snapshot attribute and try again. 103789Sahrens */ 104789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 105789Sahrens if (strchr(path, '@') != NULL) 106789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 107789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 108789Sahrens } 109789Sahrens 110789Sahrens /* 111789Sahrens * The user has requested either filesystems or volumes. 112789Sahrens * We have no way of knowing a priori what type this would be, so always 113789Sahrens * report it as "filesystem" or "volume", our two primitive types. 114789Sahrens */ 115789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 116789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 117789Sahrens 118789Sahrens assert(types & ZFS_TYPE_VOLUME); 119789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 120789Sahrens } 121789Sahrens 122789Sahrens /* 123789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 124789Sahrens * provide a more meaningful error message. We place a more useful message in 125789Sahrens * 'buf' detailing exactly why the name was not valid. 126789Sahrens */ 127789Sahrens static int 1285326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 1295326Sek110237 boolean_t modifying) 130789Sahrens { 131789Sahrens namecheck_err_t why; 132789Sahrens char what; 133789Sahrens 134789Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 1352082Seschrock if (hdl != NULL) { 136789Sahrens switch (why) { 1371003Slling case NAME_ERR_TOOLONG: 1382082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1392082Seschrock "name is too long")); 1401003Slling break; 1411003Slling 142789Sahrens case NAME_ERR_LEADING_SLASH: 1432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1442082Seschrock "leading slash in name")); 145789Sahrens break; 146789Sahrens 147789Sahrens case NAME_ERR_EMPTY_COMPONENT: 1482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1492082Seschrock "empty component in name")); 150789Sahrens break; 151789Sahrens 152789Sahrens case NAME_ERR_TRAILING_SLASH: 1532082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1542082Seschrock "trailing slash in name")); 155789Sahrens break; 156789Sahrens 157789Sahrens case NAME_ERR_INVALCHAR: 1582082Seschrock zfs_error_aux(hdl, 159789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 1602082Seschrock "'%c' in name"), what); 161789Sahrens break; 162789Sahrens 163789Sahrens case NAME_ERR_MULTIPLE_AT: 1642082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1652082Seschrock "multiple '@' delimiters in name")); 166789Sahrens break; 1672856Snd150628 1682856Snd150628 case NAME_ERR_NOLETTER: 1692856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1702856Snd150628 "pool doesn't begin with a letter")); 1712856Snd150628 break; 1722856Snd150628 1732856Snd150628 case NAME_ERR_RESERVED: 1742856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1752856Snd150628 "name is reserved")); 1762856Snd150628 break; 1772856Snd150628 1782856Snd150628 case NAME_ERR_DISKLIKE: 1792856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1802856Snd150628 "reserved disk name")); 1812856Snd150628 break; 182789Sahrens } 183789Sahrens } 184789Sahrens 185789Sahrens return (0); 186789Sahrens } 187789Sahrens 188789Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 1892082Seschrock if (hdl != NULL) 1902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1912082Seschrock "snapshot delimiter '@' in filesystem name")); 192789Sahrens return (0); 193789Sahrens } 194789Sahrens 1952199Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 1962199Sahrens if (hdl != NULL) 1972199Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1983413Smmusante "missing '@' delimiter in snapshot name")); 1992199Sahrens return (0); 2002199Sahrens } 2012199Sahrens 2025326Sek110237 if (modifying && strchr(path, '%') != NULL) { 2035326Sek110237 if (hdl != NULL) 2045326Sek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2055326Sek110237 "invalid character %c in name"), '%'); 2065326Sek110237 return (0); 2075326Sek110237 } 2085326Sek110237 2092082Seschrock return (-1); 210789Sahrens } 211789Sahrens 212789Sahrens int 213789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 214789Sahrens { 2156423Sgw25295 if (type == ZFS_TYPE_POOL) 2166423Sgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 2175326Sek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 218789Sahrens } 219789Sahrens 220789Sahrens /* 2212676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2222676Seschrock * properties into a separate nvlist. 2232676Seschrock */ 2244217Seschrock static nvlist_t * 2254217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2262676Seschrock { 2272676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2282676Seschrock nvpair_t *elem; 2292676Seschrock nvlist_t *propval; 2304217Seschrock nvlist_t *nvl; 2314217Seschrock 2324217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2334217Seschrock (void) no_memory(hdl); 2344217Seschrock return (NULL); 2354217Seschrock } 2362676Seschrock 2372676Seschrock elem = NULL; 2384217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2392676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2402676Seschrock continue; 2412676Seschrock 2422676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2434217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2444217Seschrock nvlist_free(nvl); 2454217Seschrock (void) no_memory(hdl); 2464217Seschrock return (NULL); 2474217Seschrock } 2482676Seschrock } 2492676Seschrock 2504217Seschrock return (nvl); 2512676Seschrock } 2522676Seschrock 2536865Srm160521 static zpool_handle_t * 2546865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 2556865Srm160521 { 2566865Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 2576865Srm160521 zpool_handle_t *zph; 2586865Srm160521 2596865Srm160521 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 2606865Srm160521 if (hdl->libzfs_pool_handles != NULL) 2616865Srm160521 zph->zpool_next = hdl->libzfs_pool_handles; 2626865Srm160521 hdl->libzfs_pool_handles = zph; 2636865Srm160521 } 2646865Srm160521 return (zph); 2656865Srm160521 } 2666865Srm160521 2676865Srm160521 static zpool_handle_t * 2686865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 2696865Srm160521 { 2706865Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 2716865Srm160521 zpool_handle_t *zph = hdl->libzfs_pool_handles; 2726865Srm160521 2736865Srm160521 while ((zph != NULL) && 2746865Srm160521 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 2756865Srm160521 zph = zph->zpool_next; 2766865Srm160521 return (zph); 2776865Srm160521 } 2786865Srm160521 2796865Srm160521 /* 2806865Srm160521 * Returns a handle to the pool that contains the provided dataset. 2816865Srm160521 * If a handle to that pool already exists then that handle is returned. 2826865Srm160521 * Otherwise, a new handle is created and added to the list of handles. 2836865Srm160521 */ 2846865Srm160521 static zpool_handle_t * 2856865Srm160521 zpool_handle(zfs_handle_t *zhp) 2866865Srm160521 { 2876865Srm160521 char *pool_name; 2886865Srm160521 int len; 2896865Srm160521 zpool_handle_t *zph; 2906865Srm160521 2916865Srm160521 len = strcspn(zhp->zfs_name, "/@") + 1; 2926865Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, len); 2936865Srm160521 (void) strlcpy(pool_name, zhp->zfs_name, len); 2946865Srm160521 2956865Srm160521 zph = zpool_find_handle(zhp, pool_name, len); 2966865Srm160521 if (zph == NULL) 2976865Srm160521 zph = zpool_add_handle(zhp, pool_name); 2986865Srm160521 2996865Srm160521 free(pool_name); 3006865Srm160521 return (zph); 3016865Srm160521 } 3026865Srm160521 3036865Srm160521 void 3046865Srm160521 zpool_free_handles(libzfs_handle_t *hdl) 3056865Srm160521 { 3066865Srm160521 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 3076865Srm160521 3086865Srm160521 while (zph != NULL) { 3096865Srm160521 next = zph->zpool_next; 3106865Srm160521 zpool_close(zph); 3116865Srm160521 zph = next; 3126865Srm160521 } 3136865Srm160521 hdl->libzfs_pool_handles = NULL; 3146865Srm160521 } 3156865Srm160521 3162676Seschrock /* 317789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 318789Sahrens */ 319789Sahrens static int 3208228SEric.Taylor@Sun.COM get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 321789Sahrens { 3222676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3238228SEric.Taylor@Sun.COM 3248228SEric.Taylor@Sun.COM (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 3258228SEric.Taylor@Sun.COM 3268228SEric.Taylor@Sun.COM while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3271356Seschrock if (errno == ENOMEM) { 3288228SEric.Taylor@Sun.COM if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 3292082Seschrock return (-1); 3302676Seschrock } 3311356Seschrock } else { 3321356Seschrock return (-1); 3331356Seschrock } 3341356Seschrock } 3358228SEric.Taylor@Sun.COM return (0); 3368228SEric.Taylor@Sun.COM } 3378228SEric.Taylor@Sun.COM 3388228SEric.Taylor@Sun.COM static int 3398228SEric.Taylor@Sun.COM put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 3408228SEric.Taylor@Sun.COM { 3418228SEric.Taylor@Sun.COM nvlist_t *allprops, *userprops; 3428228SEric.Taylor@Sun.COM 3438228SEric.Taylor@Sun.COM zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 3448228SEric.Taylor@Sun.COM 3458228SEric.Taylor@Sun.COM if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 3462082Seschrock return (-1); 3472082Seschrock } 348789Sahrens 3494217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 3504217Seschrock nvlist_free(allprops); 3512676Seschrock return (-1); 3524217Seschrock } 3534217Seschrock 3544217Seschrock nvlist_free(zhp->zfs_props); 3554217Seschrock nvlist_free(zhp->zfs_user_props); 3564217Seschrock 3574217Seschrock zhp->zfs_props = allprops; 3584217Seschrock zhp->zfs_user_props = userprops; 3592082Seschrock 360789Sahrens return (0); 361789Sahrens } 362789Sahrens 3638228SEric.Taylor@Sun.COM static int 3648228SEric.Taylor@Sun.COM get_stats(zfs_handle_t *zhp) 3658228SEric.Taylor@Sun.COM { 3668228SEric.Taylor@Sun.COM int rc = 0; 3678228SEric.Taylor@Sun.COM zfs_cmd_t zc = { 0 }; 3688228SEric.Taylor@Sun.COM 3698228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 3708228SEric.Taylor@Sun.COM return (-1); 3718228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, &zc) != 0) 3728228SEric.Taylor@Sun.COM rc = -1; 3738228SEric.Taylor@Sun.COM else if (put_stats_zhdl(zhp, &zc) != 0) 3748228SEric.Taylor@Sun.COM rc = -1; 3758228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 3768228SEric.Taylor@Sun.COM return (rc); 3778228SEric.Taylor@Sun.COM } 3788228SEric.Taylor@Sun.COM 379789Sahrens /* 380789Sahrens * Refresh the properties currently stored in the handle. 381789Sahrens */ 382789Sahrens void 383789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 384789Sahrens { 385789Sahrens (void) get_stats(zhp); 386789Sahrens } 387789Sahrens 388789Sahrens /* 389789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 390789Sahrens * zfs_iter_* to create child handles on the fly. 391789Sahrens */ 3928228SEric.Taylor@Sun.COM static int 3938228SEric.Taylor@Sun.COM make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 394789Sahrens { 3954543Smarks char *logstr; 3968228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 397789Sahrens 3984543Smarks /* 3994543Smarks * Preserve history log string. 4004543Smarks * any changes performed here will be 4014543Smarks * logged as an internal event. 4024543Smarks */ 4034543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 4044543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 4058228SEric.Taylor@Sun.COM 4061758Sahrens top: 4078228SEric.Taylor@Sun.COM if (put_stats_zhdl(zhp, zc) != 0) { 4084543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 4098228SEric.Taylor@Sun.COM return (-1); 410789Sahrens } 411789Sahrens 4128228SEric.Taylor@Sun.COM 4131758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 4148228SEric.Taylor@Sun.COM zfs_cmd_t zc2 = { 0 }; 4151758Sahrens 4161758Sahrens /* 4171758Sahrens * If it is dds_inconsistent, then we've caught it in 4181758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 4191758Sahrens * it is inconsistent from the ZPL's point of view, so 4201758Sahrens * can't be mounted. However, it could also be that we 4211758Sahrens * have crashed in the middle of one of those 4221758Sahrens * operations, in which case we need to get rid of the 4231758Sahrens * inconsistent state. We do that by either rolling 4241758Sahrens * back to the previous snapshot (which will fail if 4251758Sahrens * there is none), or destroying the filesystem. Note 4261758Sahrens * that if we are still in the middle of an active 4271758Sahrens * 'receive' or 'destroy', then the rollback and destroy 4281758Sahrens * will fail with EBUSY and we will drive on as usual. 4291758Sahrens */ 4301758Sahrens 4318228SEric.Taylor@Sun.COM (void) strlcpy(zc2.zc_name, zhp->zfs_name, 4328228SEric.Taylor@Sun.COM sizeof (zc2.zc_name)); 4331758Sahrens 4342885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 4352082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 4368228SEric.Taylor@Sun.COM zc2.zc_objset_type = DMU_OST_ZVOL; 4371758Sahrens } else { 4388228SEric.Taylor@Sun.COM zc2.zc_objset_type = DMU_OST_ZFS; 4391758Sahrens } 4401758Sahrens 4411758Sahrens /* 4425331Samw * If we can successfully destroy it, pretend that it 4431758Sahrens * never existed. 4441758Sahrens */ 4458228SEric.Taylor@Sun.COM if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) { 4464543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 4471758Sahrens errno = ENOENT; 4488228SEric.Taylor@Sun.COM return (-1); 4491758Sahrens } 4508228SEric.Taylor@Sun.COM /* If we can successfully roll it back, reset the stats */ 4518228SEric.Taylor@Sun.COM if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) { 4528228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, zc) != 0) { 4538228SEric.Taylor@Sun.COM zhp->zfs_hdl->libzfs_log_str = logstr; 4548228SEric.Taylor@Sun.COM return (-1); 4558228SEric.Taylor@Sun.COM } 4565367Sahrens goto top; 4578228SEric.Taylor@Sun.COM } 4581758Sahrens } 4591758Sahrens 460789Sahrens /* 461789Sahrens * We've managed to open the dataset and gather statistics. Determine 462789Sahrens * the high-level type. 463789Sahrens */ 4642885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 4652885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 4662885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 4672885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 4682885Sahrens else 4692885Sahrens abort(); 4702885Sahrens 471789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 472789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 473789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 474789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 475789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 476789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 477789Sahrens else 4782082Seschrock abort(); /* we should never see any other types */ 479789Sahrens 4804543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 4816865Srm160521 zhp->zpool_hdl = zpool_handle(zhp); 4828228SEric.Taylor@Sun.COM return (0); 4838228SEric.Taylor@Sun.COM } 4848228SEric.Taylor@Sun.COM 4858228SEric.Taylor@Sun.COM zfs_handle_t * 4868228SEric.Taylor@Sun.COM make_dataset_handle(libzfs_handle_t *hdl, const char *path) 4878228SEric.Taylor@Sun.COM { 4888228SEric.Taylor@Sun.COM zfs_cmd_t zc = { 0 }; 4898228SEric.Taylor@Sun.COM 4908228SEric.Taylor@Sun.COM zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 4918228SEric.Taylor@Sun.COM 4928228SEric.Taylor@Sun.COM if (zhp == NULL) 4938228SEric.Taylor@Sun.COM return (NULL); 4948228SEric.Taylor@Sun.COM 4958228SEric.Taylor@Sun.COM zhp->zfs_hdl = hdl; 4968228SEric.Taylor@Sun.COM (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 4978228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 4988228SEric.Taylor@Sun.COM free(zhp); 4998228SEric.Taylor@Sun.COM return (NULL); 5008228SEric.Taylor@Sun.COM } 5018228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, &zc) == -1) { 5028228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 5038228SEric.Taylor@Sun.COM free(zhp); 5048228SEric.Taylor@Sun.COM return (NULL); 5058228SEric.Taylor@Sun.COM } 5068228SEric.Taylor@Sun.COM if (make_dataset_handle_common(zhp, &zc) == -1) { 5078228SEric.Taylor@Sun.COM free(zhp); 5088228SEric.Taylor@Sun.COM zhp = NULL; 5098228SEric.Taylor@Sun.COM } 5108228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 5118228SEric.Taylor@Sun.COM return (zhp); 5128228SEric.Taylor@Sun.COM } 5138228SEric.Taylor@Sun.COM 5148228SEric.Taylor@Sun.COM static zfs_handle_t * 5158228SEric.Taylor@Sun.COM make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 5168228SEric.Taylor@Sun.COM { 5178228SEric.Taylor@Sun.COM zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 5188228SEric.Taylor@Sun.COM 5198228SEric.Taylor@Sun.COM if (zhp == NULL) 5208228SEric.Taylor@Sun.COM return (NULL); 5218228SEric.Taylor@Sun.COM 5228228SEric.Taylor@Sun.COM zhp->zfs_hdl = hdl; 5238228SEric.Taylor@Sun.COM (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 5248228SEric.Taylor@Sun.COM if (make_dataset_handle_common(zhp, zc) == -1) { 5258228SEric.Taylor@Sun.COM free(zhp); 5268228SEric.Taylor@Sun.COM return (NULL); 5278228SEric.Taylor@Sun.COM } 528789Sahrens return (zhp); 529789Sahrens } 530789Sahrens 531789Sahrens /* 532789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 533789Sahrens * argument is a mask of acceptable types. The function will print an 534789Sahrens * appropriate error message and return NULL if it can't be opened. 535789Sahrens */ 536789Sahrens zfs_handle_t * 5372082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 538789Sahrens { 539789Sahrens zfs_handle_t *zhp; 5402082Seschrock char errbuf[1024]; 5412082Seschrock 5422082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 5432082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 544789Sahrens 545789Sahrens /* 5462082Seschrock * Validate the name before we even try to open it. 547789Sahrens */ 5485326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 5492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5502082Seschrock "invalid dataset name")); 5512082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 552789Sahrens return (NULL); 553789Sahrens } 554789Sahrens 555789Sahrens /* 556789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 557789Sahrens */ 558789Sahrens errno = 0; 5592082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 5603237Slling (void) zfs_standard_error(hdl, errno, errbuf); 561789Sahrens return (NULL); 562789Sahrens } 563789Sahrens 564789Sahrens if (!(types & zhp->zfs_type)) { 5652082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 5662142Seschrock zfs_close(zhp); 567789Sahrens return (NULL); 568789Sahrens } 569789Sahrens 570789Sahrens return (zhp); 571789Sahrens } 572789Sahrens 573789Sahrens /* 574789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 575789Sahrens */ 576789Sahrens void 577789Sahrens zfs_close(zfs_handle_t *zhp) 578789Sahrens { 579789Sahrens if (zhp->zfs_mntopts) 580789Sahrens free(zhp->zfs_mntopts); 5812676Seschrock nvlist_free(zhp->zfs_props); 5822676Seschrock nvlist_free(zhp->zfs_user_props); 583789Sahrens free(zhp); 584789Sahrens } 585789Sahrens 5868228SEric.Taylor@Sun.COM typedef struct mnttab_node { 5878228SEric.Taylor@Sun.COM struct mnttab mtn_mt; 5888228SEric.Taylor@Sun.COM avl_node_t mtn_node; 5898228SEric.Taylor@Sun.COM } mnttab_node_t; 5908228SEric.Taylor@Sun.COM 5918228SEric.Taylor@Sun.COM static int 5928228SEric.Taylor@Sun.COM libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 5938228SEric.Taylor@Sun.COM { 5948228SEric.Taylor@Sun.COM const mnttab_node_t *mtn1 = arg1; 5958228SEric.Taylor@Sun.COM const mnttab_node_t *mtn2 = arg2; 5968228SEric.Taylor@Sun.COM int rv; 5978228SEric.Taylor@Sun.COM 5988228SEric.Taylor@Sun.COM rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 5998228SEric.Taylor@Sun.COM 6008228SEric.Taylor@Sun.COM if (rv == 0) 6018228SEric.Taylor@Sun.COM return (0); 6028228SEric.Taylor@Sun.COM return (rv > 0 ? 1 : -1); 6038228SEric.Taylor@Sun.COM } 6048228SEric.Taylor@Sun.COM 6058228SEric.Taylor@Sun.COM void 6068228SEric.Taylor@Sun.COM libzfs_mnttab_init(libzfs_handle_t *hdl) 6078228SEric.Taylor@Sun.COM { 6088228SEric.Taylor@Sun.COM struct mnttab entry; 6098228SEric.Taylor@Sun.COM 6108228SEric.Taylor@Sun.COM assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 6118228SEric.Taylor@Sun.COM avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 6128228SEric.Taylor@Sun.COM sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 6138228SEric.Taylor@Sun.COM 6148228SEric.Taylor@Sun.COM rewind(hdl->libzfs_mnttab); 6158228SEric.Taylor@Sun.COM while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 6168228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6178228SEric.Taylor@Sun.COM 6188228SEric.Taylor@Sun.COM if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 6198228SEric.Taylor@Sun.COM continue; 6208228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 6218228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 6228228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 6238228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 6248228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 6258228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 6268228SEric.Taylor@Sun.COM } 6278228SEric.Taylor@Sun.COM } 6288228SEric.Taylor@Sun.COM 6298228SEric.Taylor@Sun.COM void 6308228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl) 6318228SEric.Taylor@Sun.COM { 6328228SEric.Taylor@Sun.COM void *cookie = NULL; 6338228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6348228SEric.Taylor@Sun.COM 6358228SEric.Taylor@Sun.COM while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) { 6368228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_special); 6378228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mountp); 6388228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_fstype); 6398228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mntopts); 6408228SEric.Taylor@Sun.COM free(mtn); 6418228SEric.Taylor@Sun.COM } 6428228SEric.Taylor@Sun.COM avl_destroy(&hdl->libzfs_mnttab_cache); 6438228SEric.Taylor@Sun.COM } 6448228SEric.Taylor@Sun.COM 6458228SEric.Taylor@Sun.COM int 6468228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 6478228SEric.Taylor@Sun.COM struct mnttab *entry) 6488228SEric.Taylor@Sun.COM { 6498228SEric.Taylor@Sun.COM mnttab_node_t find; 6508228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6518228SEric.Taylor@Sun.COM 6528228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 6538228SEric.Taylor@Sun.COM libzfs_mnttab_init(hdl); 6548228SEric.Taylor@Sun.COM 6558228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 6568228SEric.Taylor@Sun.COM mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 6578228SEric.Taylor@Sun.COM if (mtn) { 6588228SEric.Taylor@Sun.COM *entry = mtn->mtn_mt; 6598228SEric.Taylor@Sun.COM return (0); 6608228SEric.Taylor@Sun.COM } 6618228SEric.Taylor@Sun.COM return (ENOENT); 6628228SEric.Taylor@Sun.COM } 6638228SEric.Taylor@Sun.COM 6648228SEric.Taylor@Sun.COM void 6658228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 6668228SEric.Taylor@Sun.COM const char *mountp, const char *mntopts) 6678228SEric.Taylor@Sun.COM { 6688228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6698228SEric.Taylor@Sun.COM 6708228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 6718228SEric.Taylor@Sun.COM return; 6728228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 6738228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 6748228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 6758228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 6768228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 6778228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 6788228SEric.Taylor@Sun.COM } 6798228SEric.Taylor@Sun.COM 6808228SEric.Taylor@Sun.COM void 6818228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 6828228SEric.Taylor@Sun.COM { 6838228SEric.Taylor@Sun.COM mnttab_node_t find; 6848228SEric.Taylor@Sun.COM mnttab_node_t *ret; 6858228SEric.Taylor@Sun.COM 6868228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 6878228SEric.Taylor@Sun.COM if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) { 6888228SEric.Taylor@Sun.COM avl_remove(&hdl->libzfs_mnttab_cache, ret); 6898228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_special); 6908228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mountp); 6918228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_fstype); 6928228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mntopts); 6938228SEric.Taylor@Sun.COM free(ret); 6948228SEric.Taylor@Sun.COM } 6958228SEric.Taylor@Sun.COM } 6968228SEric.Taylor@Sun.COM 6975713Srm160521 int 6985713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 6995713Srm160521 { 7006865Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 7016865Srm160521 7025713Srm160521 if (zpool_handle == NULL) 7035713Srm160521 return (-1); 7045713Srm160521 7055713Srm160521 *spa_version = zpool_get_prop_int(zpool_handle, 7065713Srm160521 ZPOOL_PROP_VERSION, NULL); 7075713Srm160521 return (0); 7085713Srm160521 } 7095713Srm160521 7105713Srm160521 /* 7115713Srm160521 * The choice of reservation property depends on the SPA version. 7125713Srm160521 */ 7135713Srm160521 static int 7145713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 7155713Srm160521 { 7165713Srm160521 int spa_version; 7175713Srm160521 7185713Srm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 7195713Srm160521 return (-1); 7205713Srm160521 7215713Srm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 7225713Srm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 7235713Srm160521 else 7245713Srm160521 *resv_prop = ZFS_PROP_RESERVATION; 7255713Srm160521 7265713Srm160521 return (0); 7275713Srm160521 } 7285713Srm160521 7293912Slling /* 7302676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 7312676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 7322676Seschrock * strings. 733789Sahrens */ 7347184Stimh nvlist_t * 7357184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 7365094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 737789Sahrens { 7382676Seschrock nvpair_t *elem; 7392676Seschrock uint64_t intval; 7402676Seschrock char *strval; 7415094Slling zfs_prop_t prop; 7422676Seschrock nvlist_t *ret; 7435331Samw int chosen_normal = -1; 7445331Samw int chosen_utf = -1; 7452676Seschrock 7465094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 7475094Slling (void) no_memory(hdl); 7485094Slling return (NULL); 749789Sahrens } 750789Sahrens 7512676Seschrock elem = NULL; 7522676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 7535094Slling const char *propname = nvpair_name(elem); 7542676Seschrock 7552676Seschrock /* 7562676Seschrock * Make sure this property is valid and applies to this type. 7572676Seschrock */ 7585094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 7595094Slling if (!zfs_prop_user(propname)) { 7602676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7615094Slling "invalid property '%s'"), propname); 7625094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7635094Slling goto error; 7645094Slling } 7655094Slling 7665094Slling /* 7675094Slling * If this is a user property, make sure it's a 7685094Slling * string, and that it's less than ZAP_MAXNAMELEN. 7695094Slling */ 7705094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 7715094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7725094Slling "'%s' must be a string"), propname); 7735094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7745094Slling goto error; 7755094Slling } 7765094Slling 7775094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 7785094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7795094Slling "property name '%s' is too long"), 7802676Seschrock propname); 7812676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7822676Seschrock goto error; 7832676Seschrock } 7842676Seschrock 7852676Seschrock (void) nvpair_value_string(elem, &strval); 7862676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 7872676Seschrock (void) no_memory(hdl); 7882676Seschrock goto error; 7892676Seschrock } 7902676Seschrock continue; 791789Sahrens } 7922676Seschrock 7937265Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 7947265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7957265Sahrens "this property can not be modified for snapshots")); 7967265Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 7977265Sahrens goto error; 7987265Sahrens } 7997265Sahrens 8002676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 8012676Seschrock zfs_error_aux(hdl, 8022676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 8032676Seschrock "apply to datasets of this type"), propname); 8042676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 8052676Seschrock goto error; 8062676Seschrock } 8072676Seschrock 8082676Seschrock if (zfs_prop_readonly(prop) && 8095331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 8102676Seschrock zfs_error_aux(hdl, 8112676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 8122676Seschrock propname); 8132676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 8142676Seschrock goto error; 8152676Seschrock } 8162676Seschrock 8175094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 8185094Slling &strval, &intval, errbuf) != 0) 8192676Seschrock goto error; 8202676Seschrock 8212676Seschrock /* 8222676Seschrock * Perform some additional checks for specific properties. 8232676Seschrock */ 8242676Seschrock switch (prop) { 8254577Sahrens case ZFS_PROP_VERSION: 8264577Sahrens { 8274577Sahrens int version; 8284577Sahrens 8294577Sahrens if (zhp == NULL) 8304577Sahrens break; 8314577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 8324577Sahrens if (intval < version) { 8334577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8344577Sahrens "Can not downgrade; already at version %u"), 8354577Sahrens version); 8364577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8374577Sahrens goto error; 8384577Sahrens } 8394577Sahrens break; 8404577Sahrens } 8414577Sahrens 8422676Seschrock case ZFS_PROP_RECORDSIZE: 8432676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 8442676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 8452676Seschrock if (intval < SPA_MINBLOCKSIZE || 8462676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 8472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8482676Seschrock "'%s' must be power of 2 from %u " 8492676Seschrock "to %uk"), propname, 8502676Seschrock (uint_t)SPA_MINBLOCKSIZE, 8512676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 8522676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8532676Seschrock goto error; 854789Sahrens } 855789Sahrens break; 856789Sahrens 8573126Sahl case ZFS_PROP_SHAREISCSI: 8583126Sahl if (strcmp(strval, "off") != 0 && 8593126Sahl strcmp(strval, "on") != 0 && 8603126Sahl strcmp(strval, "type=disk") != 0) { 8613126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8623126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 8633126Sahl propname); 8643126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8653126Sahl goto error; 8663126Sahl } 8673126Sahl 8683126Sahl break; 8693126Sahl 8702676Seschrock case ZFS_PROP_MOUNTPOINT: 8714778Srm160521 { 8724778Srm160521 namecheck_err_t why; 8734778Srm160521 8742676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 8752676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 8762676Seschrock break; 8772676Seschrock 8784778Srm160521 if (mountpoint_namecheck(strval, &why)) { 8794778Srm160521 switch (why) { 8804778Srm160521 case NAME_ERR_LEADING_SLASH: 8814778Srm160521 zfs_error_aux(hdl, 8824778Srm160521 dgettext(TEXT_DOMAIN, 8834778Srm160521 "'%s' must be an absolute path, " 8844778Srm160521 "'none', or 'legacy'"), propname); 8854778Srm160521 break; 8864778Srm160521 case NAME_ERR_TOOLONG: 8874778Srm160521 zfs_error_aux(hdl, 8884778Srm160521 dgettext(TEXT_DOMAIN, 8894778Srm160521 "component of '%s' is too long"), 8904778Srm160521 propname); 8914778Srm160521 break; 8924778Srm160521 } 8932676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8942676Seschrock goto error; 895789Sahrens } 8964778Srm160521 } 8974778Srm160521 8983126Sahl /*FALLTHRU*/ 8993126Sahl 9005331Samw case ZFS_PROP_SHARESMB: 9013126Sahl case ZFS_PROP_SHARENFS: 9023126Sahl /* 9035331Samw * For the mountpoint and sharenfs or sharesmb 9045331Samw * properties, check if it can be set in a 9055331Samw * global/non-global zone based on 9063126Sahl * the zoned property value: 9073126Sahl * 9083126Sahl * global zone non-global zone 9093126Sahl * -------------------------------------------------- 9103126Sahl * zoned=on mountpoint (no) mountpoint (yes) 9113126Sahl * sharenfs (no) sharenfs (no) 9125331Samw * sharesmb (no) sharesmb (no) 9133126Sahl * 9143126Sahl * zoned=off mountpoint (yes) N/A 9153126Sahl * sharenfs (yes) 9165331Samw * sharesmb (yes) 9173126Sahl */ 9182676Seschrock if (zoned) { 9192676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 9202676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9212676Seschrock "'%s' cannot be set on " 9222676Seschrock "dataset in a non-global zone"), 9232676Seschrock propname); 9242676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9252676Seschrock errbuf); 9262676Seschrock goto error; 9275331Samw } else if (prop == ZFS_PROP_SHARENFS || 9285331Samw prop == ZFS_PROP_SHARESMB) { 9292676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9302676Seschrock "'%s' cannot be set in " 9312676Seschrock "a non-global zone"), propname); 9322676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9332676Seschrock errbuf); 9342676Seschrock goto error; 9352676Seschrock } 9362676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 9372676Seschrock /* 9382676Seschrock * If zoned property is 'off', this must be in 9392676Seschrock * a globle zone. If not, something is wrong. 9402676Seschrock */ 9412676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9422676Seschrock "'%s' cannot be set while dataset " 9432676Seschrock "'zoned' property is set"), propname); 9442676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 9452676Seschrock goto error; 9462676Seschrock } 9473126Sahl 9484180Sdougm /* 9494180Sdougm * At this point, it is legitimate to set the 9504180Sdougm * property. Now we want to make sure that the 9514180Sdougm * property value is valid if it is sharenfs. 9524180Sdougm */ 9535331Samw if ((prop == ZFS_PROP_SHARENFS || 9545331Samw prop == ZFS_PROP_SHARESMB) && 9554217Seschrock strcmp(strval, "on") != 0 && 9564217Seschrock strcmp(strval, "off") != 0) { 9575331Samw zfs_share_proto_t proto; 9585331Samw 9595331Samw if (prop == ZFS_PROP_SHARESMB) 9605331Samw proto = PROTO_SMB; 9615331Samw else 9625331Samw proto = PROTO_NFS; 9634180Sdougm 9644180Sdougm /* 9655331Samw * Must be an valid sharing protocol 9665331Samw * option string so init the libshare 9675331Samw * in order to enable the parser and 9685331Samw * then parse the options. We use the 9695331Samw * control API since we don't care about 9705331Samw * the current configuration and don't 9714180Sdougm * want the overhead of loading it 9724180Sdougm * until we actually do something. 9734180Sdougm */ 9744180Sdougm 9754217Seschrock if (zfs_init_libshare(hdl, 9764217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 9774217Seschrock /* 9784217Seschrock * An error occurred so we can't do 9794217Seschrock * anything 9804217Seschrock */ 9814217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9824217Seschrock "'%s' cannot be set: problem " 9834217Seschrock "in share initialization"), 9844217Seschrock propname); 9854217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 9864217Seschrock errbuf); 9874217Seschrock goto error; 9884217Seschrock } 9894217Seschrock 9905331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 9914217Seschrock /* 9924217Seschrock * There was an error in parsing so 9934217Seschrock * deal with it by issuing an error 9944217Seschrock * message and leaving after 9954217Seschrock * uninitializing the the libshare 9964217Seschrock * interface. 9974217Seschrock */ 9984217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9994217Seschrock "'%s' cannot be set to invalid " 10004217Seschrock "options"), propname); 10014217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10024217Seschrock errbuf); 10034217Seschrock zfs_uninit_libshare(hdl); 10044217Seschrock goto error; 10054217Seschrock } 10064180Sdougm zfs_uninit_libshare(hdl); 10074180Sdougm } 10084180Sdougm 10093126Sahl break; 10105331Samw case ZFS_PROP_UTF8ONLY: 10115331Samw chosen_utf = (int)intval; 10125331Samw break; 10135331Samw case ZFS_PROP_NORMALIZE: 10145331Samw chosen_normal = (int)intval; 10155331Samw break; 10162676Seschrock } 10172676Seschrock 10182676Seschrock /* 10192676Seschrock * For changes to existing volumes, we have some additional 10202676Seschrock * checks to enforce. 10212676Seschrock */ 10222676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 10232676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 10242676Seschrock ZFS_PROP_VOLSIZE); 10252676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 10262676Seschrock ZFS_PROP_VOLBLOCKSIZE); 10272676Seschrock char buf[64]; 10282676Seschrock 10292676Seschrock switch (prop) { 10302676Seschrock case ZFS_PROP_RESERVATION: 10315378Sck153898 case ZFS_PROP_REFRESERVATION: 10322676Seschrock if (intval > volsize) { 10332676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10342676Seschrock "'%s' is greater than current " 10352676Seschrock "volume size"), propname); 10362676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10372676Seschrock errbuf); 10382676Seschrock goto error; 10392676Seschrock } 10402676Seschrock break; 10412676Seschrock 10422676Seschrock case ZFS_PROP_VOLSIZE: 10432676Seschrock if (intval % blocksize != 0) { 10442676Seschrock zfs_nicenum(blocksize, buf, 10452676Seschrock sizeof (buf)); 10462676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10472676Seschrock "'%s' must be a multiple of " 10482676Seschrock "volume block size (%s)"), 10492676Seschrock propname, buf); 10502676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10512676Seschrock errbuf); 10522676Seschrock goto error; 10532676Seschrock } 10542676Seschrock 10552676Seschrock if (intval == 0) { 10562676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10572676Seschrock "'%s' cannot be zero"), 10582676Seschrock propname); 10592676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10602676Seschrock errbuf); 10612676Seschrock goto error; 1062789Sahrens } 10633126Sahl break; 1064789Sahrens } 1065789Sahrens } 1066789Sahrens } 1067789Sahrens 10682676Seschrock /* 10695331Samw * If normalization was chosen, but no UTF8 choice was made, 10705331Samw * enforce rejection of non-UTF8 names. 10715331Samw * 10725331Samw * If normalization was chosen, but rejecting non-UTF8 names 10735331Samw * was explicitly not chosen, it is an error. 10745331Samw */ 10755498Stimh if (chosen_normal > 0 && chosen_utf < 0) { 10765331Samw if (nvlist_add_uint64(ret, 10775331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 10785331Samw (void) no_memory(hdl); 10795331Samw goto error; 10805331Samw } 10815498Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 10825331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10835331Samw "'%s' must be set 'on' if normalization chosen"), 10845331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 10855331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 10865331Samw goto error; 10875331Samw } 10885331Samw 10895331Samw /* 10902676Seschrock * If this is an existing volume, and someone is setting the volsize, 10912676Seschrock * make sure that it matches the reservation, or add it if necessary. 10922676Seschrock */ 10932676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 10942676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 10952676Seschrock &intval) == 0) { 10962676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 10972676Seschrock ZFS_PROP_VOLSIZE); 10985481Sck153898 uint64_t old_reservation; 10992676Seschrock uint64_t new_reservation; 11005481Sck153898 zfs_prop_t resv_prop; 11015713Srm160521 11025713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 11035481Sck153898 goto error; 11045481Sck153898 old_reservation = zfs_prop_get_int(zhp, resv_prop); 11052676Seschrock 11062676Seschrock if (old_volsize == old_reservation && 11075481Sck153898 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop), 11082676Seschrock &new_reservation) != 0) { 11092676Seschrock if (nvlist_add_uint64(ret, 11105481Sck153898 zfs_prop_to_name(resv_prop), intval) != 0) { 11112676Seschrock (void) no_memory(hdl); 11122676Seschrock goto error; 11132676Seschrock } 11142676Seschrock } 11152676Seschrock } 11162676Seschrock return (ret); 11172676Seschrock 11182676Seschrock error: 11192676Seschrock nvlist_free(ret); 11202676Seschrock return (NULL); 1121789Sahrens } 1122789Sahrens 11234543Smarks static int 11244543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 11254543Smarks uint64_t *ret_who) 11264543Smarks { 11274543Smarks struct passwd *pwd; 11284543Smarks struct group *grp; 11294543Smarks uid_t id; 11304543Smarks 11314543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 11324543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 11334543Smarks *ret_who = -1; 11344543Smarks return (0); 11354543Smarks } 11364543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 11374543Smarks return (EZFS_BADWHO); 11384543Smarks 11394543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 11404543Smarks strcmp(who, "everyone") == 0) { 11414543Smarks *ret_who = -1; 11424543Smarks *who_type = ZFS_DELEG_EVERYONE; 11434543Smarks return (0); 11444543Smarks } 11454543Smarks 11464543Smarks pwd = getpwnam(who); 11474543Smarks grp = getgrnam(who); 11484543Smarks 11494543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 11504543Smarks *ret_who = pwd->pw_uid; 11514543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 11524543Smarks *ret_who = grp->gr_gid; 11534543Smarks } else if (pwd) { 11544543Smarks *ret_who = pwd->pw_uid; 11554543Smarks *who_type = ZFS_DELEG_USER; 11564543Smarks } else if (grp) { 11574543Smarks *ret_who = grp->gr_gid; 11584543Smarks *who_type = ZFS_DELEG_GROUP; 11594543Smarks } else { 11604543Smarks char *end; 11614543Smarks 11624543Smarks id = strtol(who, &end, 10); 11634543Smarks if (errno != 0 || *end != '\0') { 11644543Smarks return (EZFS_BADWHO); 11654543Smarks } else { 11664543Smarks *ret_who = id; 11674543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 11684543Smarks *who_type = ZFS_DELEG_USER; 11694543Smarks } 11704543Smarks } 11714543Smarks 11724543Smarks return (0); 11734543Smarks } 11744543Smarks 11754543Smarks static void 11764543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 11774543Smarks { 11784543Smarks if (perms_nvp != NULL) { 11794543Smarks verify(nvlist_add_nvlist(who_nvp, 11804543Smarks name, perms_nvp) == 0); 11814543Smarks } else { 11824543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 11834543Smarks } 11844543Smarks } 11854543Smarks 11864543Smarks static void 11874543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 11884543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 11894543Smarks nvlist_t *sets_nvp) 11904543Smarks { 11914543Smarks boolean_t do_perms, do_sets; 11924543Smarks char name[ZFS_MAX_DELEG_NAME]; 11934543Smarks 11944543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 11954543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 11964543Smarks 11974543Smarks if (!do_perms && !do_sets) 11984543Smarks do_perms = do_sets = B_TRUE; 11994543Smarks 12004543Smarks if (do_perms) { 12014543Smarks zfs_deleg_whokey(name, who_type, inherit, 12024543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 12034543Smarks whostr : (void *)&whoid); 12044543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 12054543Smarks } 12064543Smarks if (do_sets) { 12074543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 12084543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 12094543Smarks whostr : (void *)&whoid); 12104543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 12114543Smarks } 12124543Smarks } 12134543Smarks 12144543Smarks static void 12154543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 12164543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 12174543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 12184543Smarks { 12194543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 12204543Smarks helper(who_type, whoid, whostr, 0, 12214543Smarks who_nvp, perms_nvp, sets_nvp); 12224543Smarks } else { 12234543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 12244543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 12254543Smarks who_nvp, perms_nvp, sets_nvp); 12264543Smarks } 12274543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 12284543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 12294543Smarks who_nvp, perms_nvp, sets_nvp); 12304543Smarks } 12314543Smarks } 12324543Smarks } 12334543Smarks 12344543Smarks /* 12354543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 12364543Smarks * 12374543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 12384543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 12394543Smarks * base attribute named stored in the dsl. 12404543Smarks * Arguments: 12414543Smarks * 12424543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 12434543Smarks * whostr may be null for everyone or create perms. 12444543Smarks * who_type: is the type of entry in whostr. Typically this will be 12454543Smarks * ZFS_DELEG_WHO_UNKNOWN. 12465331Samw * perms: common separated list of permissions. May be null if user 12474543Smarks * is requested to remove permissions by who. 12484543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 12494543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 12504543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 12514543Smarks * The output nvp will look something like this. 12524543Smarks * ul$1234 -> {create ; destroy } 12534543Smarks * Ul$1234 -> { @myset } 12544543Smarks * s-$@myset - { snapshot; checksum; compression } 12554543Smarks */ 12564543Smarks int 12574543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 12584543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 12594543Smarks { 12604543Smarks nvlist_t *who_nvp; 12614543Smarks nvlist_t *perms_nvp = NULL; 12624543Smarks nvlist_t *sets_nvp = NULL; 12634543Smarks char errbuf[1024]; 12644787Sahrens char *who_tok, *perm; 12654543Smarks int error; 12664543Smarks 12674543Smarks *nvp = NULL; 12684543Smarks 12694543Smarks if (perms) { 12704543Smarks if ((error = nvlist_alloc(&perms_nvp, 12714543Smarks NV_UNIQUE_NAME, 0)) != 0) { 12724543Smarks return (1); 12734543Smarks } 12744543Smarks if ((error = nvlist_alloc(&sets_nvp, 12754543Smarks NV_UNIQUE_NAME, 0)) != 0) { 12764543Smarks nvlist_free(perms_nvp); 12774543Smarks return (1); 12784543Smarks } 12794543Smarks } 12804543Smarks 12814543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 12824543Smarks if (perms_nvp) 12834543Smarks nvlist_free(perms_nvp); 12844543Smarks if (sets_nvp) 12854543Smarks nvlist_free(sets_nvp); 12864543Smarks return (1); 12874543Smarks } 12884543Smarks 12894543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 12904543Smarks namecheck_err_t why; 12914543Smarks char what; 12924543Smarks 12934543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 12944787Sahrens nvlist_free(who_nvp); 12954787Sahrens if (perms_nvp) 12964787Sahrens nvlist_free(perms_nvp); 12974787Sahrens if (sets_nvp) 12984787Sahrens nvlist_free(sets_nvp); 12994787Sahrens 13004543Smarks switch (why) { 13014543Smarks case NAME_ERR_NO_AT: 13024543Smarks zfs_error_aux(zhp->zfs_hdl, 13034543Smarks dgettext(TEXT_DOMAIN, 13044543Smarks "set definition must begin with an '@' " 13054543Smarks "character")); 13064543Smarks } 13074543Smarks return (zfs_error(zhp->zfs_hdl, 13084543Smarks EZFS_BADPERMSET, whostr)); 13094543Smarks } 13104543Smarks } 13114543Smarks 13124543Smarks /* 13134543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 13144543Smarks * The first nvlist perms_nvp will have normal permissions and the 13154543Smarks * other sets_nvp will have only permssion set names in it. 13164543Smarks */ 13174787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 13184787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 13194787Sahrens 13204787Sahrens if (perm_canonical) { 13214787Sahrens verify(nvlist_add_boolean(perms_nvp, 13224787Sahrens perm_canonical) == 0); 13234787Sahrens } else if (perm[0] == '@') { 13244787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 13254787Sahrens } else { 13264787Sahrens nvlist_free(who_nvp); 13274787Sahrens nvlist_free(perms_nvp); 13284787Sahrens nvlist_free(sets_nvp); 13294787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 13304543Smarks } 13314543Smarks } 13324543Smarks 13334543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 13344543Smarks who_tok = strtok(whostr, ","); 13354543Smarks if (who_tok == NULL) { 13364543Smarks nvlist_free(who_nvp); 13374787Sahrens if (perms_nvp) 13384787Sahrens nvlist_free(perms_nvp); 13394543Smarks if (sets_nvp) 13404543Smarks nvlist_free(sets_nvp); 13414543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13424543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 13434543Smarks whostr); 13444543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 13454543Smarks } 13464543Smarks } 13474543Smarks 13484543Smarks /* 13494543Smarks * Now create the nvlist(s) 13504543Smarks */ 13514543Smarks do { 13524543Smarks uint64_t who_id; 13534543Smarks 13544543Smarks error = zfs_get_perm_who(who_tok, &who_type, 13554543Smarks &who_id); 13564543Smarks if (error) { 13574543Smarks nvlist_free(who_nvp); 13584787Sahrens if (perms_nvp) 13594787Sahrens nvlist_free(perms_nvp); 13604543Smarks if (sets_nvp) 13614543Smarks nvlist_free(sets_nvp); 13624543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13634543Smarks dgettext(TEXT_DOMAIN, 13644543Smarks "Unable to determine uid/gid for " 13654543Smarks "%s "), who_tok); 13664543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 13674543Smarks } 13684543Smarks 13694543Smarks /* 13704543Smarks * add entries for both local and descendent when required 13714543Smarks */ 13724543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 13734543Smarks perms_nvp, sets_nvp, who_type, inherit); 13744543Smarks 13754543Smarks } while (who_tok = strtok(NULL, ",")); 13764543Smarks *nvp = who_nvp; 13774543Smarks return (0); 13784543Smarks } 13794543Smarks 13804543Smarks static int 13814543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 13824543Smarks { 13834543Smarks zfs_cmd_t zc = { 0 }; 13844543Smarks int error; 13854543Smarks char errbuf[1024]; 13864543Smarks 13874543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13884543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 13894543Smarks zhp->zfs_name); 13904543Smarks 13915094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 13924543Smarks return (-1); 13934543Smarks 13944543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13954543Smarks zc.zc_perm_action = unset; 13964543Smarks 13974543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 13984543Smarks if (error && errno == ENOTSUP) { 13994543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14004543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 14014543Smarks zcmd_free_nvlists(&zc); 14024543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 14034543Smarks } else if (error) { 14044543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 14054543Smarks } 14064543Smarks zcmd_free_nvlists(&zc); 14074543Smarks 14084543Smarks return (error); 14094543Smarks } 14104543Smarks 14114543Smarks int 14124543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 14134543Smarks { 14144543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 14154543Smarks } 14164543Smarks 14174543Smarks int 14184543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 14194543Smarks { 14204543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 14214543Smarks } 14224543Smarks 14234543Smarks static int 14244543Smarks perm_compare(const void *arg1, const void *arg2) 14254543Smarks { 14264543Smarks const zfs_perm_node_t *node1 = arg1; 14274543Smarks const zfs_perm_node_t *node2 = arg2; 14284543Smarks int ret; 14294543Smarks 14304543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 14314543Smarks 14324543Smarks if (ret > 0) 14334543Smarks return (1); 14344543Smarks if (ret < 0) 14354543Smarks return (-1); 14364543Smarks else 14374543Smarks return (0); 14384543Smarks } 14394543Smarks 14404543Smarks static void 14414543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 14424543Smarks { 14434543Smarks zfs_perm_node_t *permnode; 14445367Sahrens void *cookie = NULL; 14455367Sahrens 14465367Sahrens while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 14474543Smarks free(permnode); 14485367Sahrens avl_destroy(tree); 14494543Smarks } 14504543Smarks 14514543Smarks static void 14524543Smarks zfs_destroy_tree(avl_tree_t *tree) 14534543Smarks { 14544543Smarks zfs_allow_node_t *allownode; 14555367Sahrens void *cookie = NULL; 14565367Sahrens 14574543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 14584543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 14594543Smarks zfs_destroy_perm_tree(&allownode->z_local); 14604543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 14614543Smarks free(allownode); 14624543Smarks } 14635367Sahrens avl_destroy(tree); 14644543Smarks } 14654543Smarks 14664543Smarks void 14674543Smarks zfs_free_allows(zfs_allow_t *allow) 14684543Smarks { 14694543Smarks zfs_allow_t *allownext; 14704543Smarks zfs_allow_t *freeallow; 14714543Smarks 14724543Smarks allownext = allow; 14734543Smarks while (allownext) { 14744543Smarks zfs_destroy_tree(&allownext->z_sets); 14754543Smarks zfs_destroy_tree(&allownext->z_crperms); 14764543Smarks zfs_destroy_tree(&allownext->z_user); 14774543Smarks zfs_destroy_tree(&allownext->z_group); 14784543Smarks zfs_destroy_tree(&allownext->z_everyone); 14794543Smarks freeallow = allownext; 14804543Smarks allownext = allownext->z_next; 14814543Smarks free(freeallow); 14824543Smarks } 14834543Smarks } 14844543Smarks 14854543Smarks static zfs_allow_t * 14864543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 14874543Smarks { 14884543Smarks zfs_allow_t *ptree; 14894543Smarks 14904543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 14914543Smarks sizeof (zfs_allow_t))) == NULL) { 14924543Smarks return (NULL); 14934543Smarks } 14944543Smarks 14954543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 14964543Smarks avl_create(&ptree->z_sets, 14974543Smarks perm_compare, sizeof (zfs_allow_node_t), 14984543Smarks offsetof(zfs_allow_node_t, z_node)); 14994543Smarks avl_create(&ptree->z_crperms, 15004543Smarks perm_compare, sizeof (zfs_allow_node_t), 15014543Smarks offsetof(zfs_allow_node_t, z_node)); 15024543Smarks avl_create(&ptree->z_user, 15034543Smarks perm_compare, sizeof (zfs_allow_node_t), 15044543Smarks offsetof(zfs_allow_node_t, z_node)); 15054543Smarks avl_create(&ptree->z_group, 15064543Smarks perm_compare, sizeof (zfs_allow_node_t), 15074543Smarks offsetof(zfs_allow_node_t, z_node)); 15084543Smarks avl_create(&ptree->z_everyone, 15094543Smarks perm_compare, sizeof (zfs_allow_node_t), 15104543Smarks offsetof(zfs_allow_node_t, z_node)); 15114543Smarks 15124543Smarks if (prev) 15134543Smarks prev->z_next = ptree; 15144543Smarks ptree->z_next = NULL; 15154543Smarks return (ptree); 15164543Smarks } 15174543Smarks 15184543Smarks /* 15194543Smarks * Add permissions to the appropriate AVL permission tree. 15204543Smarks * The appropriate tree may not be the requested tree. 15214543Smarks * For example if ld indicates a local permission, but 15224543Smarks * same permission also exists as a descendent permission 15234543Smarks * then the permission will be removed from the descendent 15244543Smarks * tree and add the the local+descendent tree. 15254543Smarks */ 15264543Smarks static int 15274543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 15284543Smarks char *perm, char ld) 15294543Smarks { 15304543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 15314543Smarks zfs_perm_node_t *newnode; 15324543Smarks avl_index_t where, where2; 15334543Smarks avl_tree_t *tree, *altree; 15344543Smarks 15354543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 15364543Smarks 15374543Smarks if (ld == ZFS_DELEG_NA) { 15384543Smarks tree = &allownode->z_localdescend; 15394543Smarks altree = &allownode->z_descend; 15404543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 15414543Smarks tree = &allownode->z_local; 15424543Smarks altree = &allownode->z_descend; 15434543Smarks } else { 15444543Smarks tree = &allownode->z_descend; 15454543Smarks altree = &allownode->z_local; 15464543Smarks } 15474543Smarks permnode = avl_find(tree, &pnode, &where); 15484543Smarks permnode2 = avl_find(altree, &pnode, &where2); 15494543Smarks 15504543Smarks if (permnode2) { 15514543Smarks avl_remove(altree, permnode2); 15524543Smarks free(permnode2); 15534543Smarks if (permnode == NULL) { 15544543Smarks tree = &allownode->z_localdescend; 15554543Smarks } 15564543Smarks } 15574543Smarks 15584543Smarks /* 15594543Smarks * Now insert new permission in either requested location 15604543Smarks * local/descendent or into ld when perm will exist in both. 15614543Smarks */ 15624543Smarks if (permnode == NULL) { 15634543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 15644543Smarks sizeof (zfs_perm_node_t))) == NULL) { 15654543Smarks return (-1); 15664543Smarks } 15674543Smarks *newnode = pnode; 15684543Smarks avl_add(tree, newnode); 15694543Smarks } 15704543Smarks return (0); 15714543Smarks } 15724577Sahrens 15734543Smarks /* 15744543Smarks * Uggh, this is going to be a bit complicated. 15754543Smarks * we have an nvlist coming out of the kernel that 15764543Smarks * will indicate where the permission is set and then 15774543Smarks * it will contain allow of the various "who's", and what 15784543Smarks * their permissions are. To further complicate this 15794543Smarks * we will then have to coalesce the local,descendent 15804543Smarks * and local+descendent permissions where appropriate. 15814543Smarks * The kernel only knows about a permission as being local 15824543Smarks * or descendent, but not both. 15834543Smarks * 15844543Smarks * In order to make this easier for zfs_main to deal with 15854543Smarks * a series of AVL trees will be used to maintain 15864543Smarks * all of this, primarily for sorting purposes as well 15874543Smarks * as the ability to quickly locate a specific entry. 15884543Smarks * 15894543Smarks * What we end up with are tree's for sets, create perms, 15904543Smarks * user, groups and everyone. With each of those trees 15914543Smarks * we have subtrees for local, descendent and local+descendent 15924543Smarks * permissions. 15934543Smarks */ 15944543Smarks int 15954543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 15964543Smarks { 15974543Smarks zfs_cmd_t zc = { 0 }; 15984543Smarks int error; 15994543Smarks nvlist_t *nvlist; 16004543Smarks nvlist_t *permnv, *sourcenv; 16014543Smarks nvpair_t *who_pair, *source_pair; 16024543Smarks nvpair_t *perm_pair; 16034543Smarks char errbuf[1024]; 16044543Smarks zfs_allow_t *zallowp, *newallowp; 16054543Smarks char ld; 16064543Smarks char *nvpname; 16074543Smarks uid_t uid; 16084543Smarks gid_t gid; 16094543Smarks avl_tree_t *tree; 16104543Smarks avl_index_t where; 16114543Smarks 16124543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 16134543Smarks 16144543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 16154543Smarks return (-1); 16164543Smarks 16174543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 16184543Smarks if (errno == ENOMEM) { 16194543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 16204543Smarks zcmd_free_nvlists(&zc); 16214543Smarks return (-1); 16224543Smarks } 16234543Smarks } else if (errno == ENOTSUP) { 16244543Smarks zcmd_free_nvlists(&zc); 16254543Smarks (void) snprintf(errbuf, sizeof (errbuf), 16264543Smarks gettext("Pool must be upgraded to use 'allow'")); 16274543Smarks return (zfs_error(zhp->zfs_hdl, 16284543Smarks EZFS_BADVERSION, errbuf)); 16294543Smarks } else { 16304543Smarks zcmd_free_nvlists(&zc); 16314543Smarks return (-1); 16324543Smarks } 16334543Smarks } 16344543Smarks 16354543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 16364543Smarks zcmd_free_nvlists(&zc); 16374543Smarks return (-1); 16384543Smarks } 16394543Smarks 16404543Smarks zcmd_free_nvlists(&zc); 16414543Smarks 16424543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 16434543Smarks 16444543Smarks if (source_pair == NULL) { 16454543Smarks *zfs_perms = NULL; 16464543Smarks return (0); 16474543Smarks } 16484543Smarks 16494543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 16504543Smarks if (*zfs_perms == NULL) { 16514543Smarks return (0); 16524543Smarks } 16534543Smarks 16544543Smarks zallowp = *zfs_perms; 16554543Smarks 16564543Smarks for (;;) { 16574543Smarks struct passwd *pwd; 16584543Smarks struct group *grp; 16594543Smarks zfs_allow_node_t *allownode; 16604543Smarks zfs_allow_node_t findallownode; 16614543Smarks zfs_allow_node_t *newallownode; 16624543Smarks 16634543Smarks (void) strlcpy(zallowp->z_setpoint, 16644543Smarks nvpair_name(source_pair), 16654543Smarks sizeof (zallowp->z_setpoint)); 16664543Smarks 16674543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 16684543Smarks goto abort; 16694543Smarks 16704543Smarks /* 16714543Smarks * Make sure nvlist is composed correctly 16724543Smarks */ 16734543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 16744543Smarks goto abort; 16754543Smarks } 16764543Smarks 16774543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 16784543Smarks if (who_pair == NULL) { 16794543Smarks goto abort; 16804543Smarks } 16814543Smarks 16824543Smarks do { 16834543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 16844543Smarks if (error) { 16854543Smarks goto abort; 16864543Smarks } 16874543Smarks 16884543Smarks /* 16894543Smarks * First build up the key to use 16904543Smarks * for looking up in the various 16914543Smarks * who trees. 16924543Smarks */ 16934543Smarks ld = nvpair_name(who_pair)[1]; 16944543Smarks nvpname = nvpair_name(who_pair); 16954543Smarks switch (nvpair_name(who_pair)[0]) { 16964543Smarks case ZFS_DELEG_USER: 16974543Smarks case ZFS_DELEG_USER_SETS: 16984543Smarks tree = &zallowp->z_user; 16994543Smarks uid = atol(&nvpname[3]); 17004543Smarks pwd = getpwuid(uid); 17014543Smarks (void) snprintf(findallownode.z_key, 17024543Smarks sizeof (findallownode.z_key), "user %s", 17034543Smarks (pwd) ? pwd->pw_name : 17044543Smarks &nvpair_name(who_pair)[3]); 17054543Smarks break; 17064543Smarks case ZFS_DELEG_GROUP: 17074543Smarks case ZFS_DELEG_GROUP_SETS: 17084543Smarks tree = &zallowp->z_group; 17094543Smarks gid = atol(&nvpname[3]); 17104543Smarks grp = getgrgid(gid); 17114543Smarks (void) snprintf(findallownode.z_key, 17124543Smarks sizeof (findallownode.z_key), "group %s", 17134543Smarks (grp) ? grp->gr_name : 17144543Smarks &nvpair_name(who_pair)[3]); 17154543Smarks break; 17164543Smarks case ZFS_DELEG_CREATE: 17174543Smarks case ZFS_DELEG_CREATE_SETS: 17184543Smarks tree = &zallowp->z_crperms; 17194543Smarks (void) strlcpy(findallownode.z_key, "", 17204543Smarks sizeof (findallownode.z_key)); 17214543Smarks break; 17224543Smarks case ZFS_DELEG_EVERYONE: 17234543Smarks case ZFS_DELEG_EVERYONE_SETS: 17244543Smarks (void) snprintf(findallownode.z_key, 17254543Smarks sizeof (findallownode.z_key), "everyone"); 17264543Smarks tree = &zallowp->z_everyone; 17274543Smarks break; 17284543Smarks case ZFS_DELEG_NAMED_SET: 17294543Smarks case ZFS_DELEG_NAMED_SET_SETS: 17304543Smarks (void) snprintf(findallownode.z_key, 17314543Smarks sizeof (findallownode.z_key), "%s", 17324543Smarks &nvpair_name(who_pair)[3]); 17334543Smarks tree = &zallowp->z_sets; 17344543Smarks break; 17354543Smarks } 17364543Smarks 17374543Smarks /* 17384543Smarks * Place who in tree 17394543Smarks */ 17404543Smarks allownode = avl_find(tree, &findallownode, &where); 17414543Smarks if (allownode == NULL) { 17424543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 17434543Smarks sizeof (zfs_allow_node_t))) == NULL) { 17444543Smarks goto abort; 17454543Smarks } 17464543Smarks avl_create(&newallownode->z_localdescend, 17474543Smarks perm_compare, 17484543Smarks sizeof (zfs_perm_node_t), 17494543Smarks offsetof(zfs_perm_node_t, z_node)); 17504543Smarks avl_create(&newallownode->z_local, 17514543Smarks perm_compare, 17524543Smarks sizeof (zfs_perm_node_t), 17534543Smarks offsetof(zfs_perm_node_t, z_node)); 17544543Smarks avl_create(&newallownode->z_descend, 17554543Smarks perm_compare, 17564543Smarks sizeof (zfs_perm_node_t), 17574543Smarks offsetof(zfs_perm_node_t, z_node)); 17584543Smarks (void) strlcpy(newallownode->z_key, 17594543Smarks findallownode.z_key, 17604543Smarks sizeof (findallownode.z_key)); 17614543Smarks avl_insert(tree, newallownode, where); 17624543Smarks allownode = newallownode; 17634543Smarks } 17644543Smarks 17654543Smarks /* 17664543Smarks * Now iterate over the permissions and 17674543Smarks * place them in the appropriate local, 17684543Smarks * descendent or local+descendent tree. 17694543Smarks * 17704543Smarks * The permissions are added to the tree 17714543Smarks * via zfs_coalesce_perm(). 17724543Smarks */ 17734543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 17744543Smarks if (perm_pair == NULL) 17754543Smarks goto abort; 17764543Smarks do { 17774543Smarks if (zfs_coalesce_perm(zhp, allownode, 17784543Smarks nvpair_name(perm_pair), ld) != 0) 17794543Smarks goto abort; 17804543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 17814543Smarks perm_pair)); 17824543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 17834543Smarks 17844543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 17854543Smarks if (source_pair == NULL) 17864543Smarks break; 17874543Smarks 17884543Smarks /* 17894543Smarks * allocate another node from the link list of 17904543Smarks * zfs_allow_t structures 17914543Smarks */ 17924543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 17934543Smarks nvpair_name(source_pair)); 17944543Smarks if (newallowp == NULL) { 17954543Smarks goto abort; 17964543Smarks } 17974543Smarks zallowp = newallowp; 17984543Smarks } 17994543Smarks nvlist_free(nvlist); 18004543Smarks return (0); 18014543Smarks abort: 18024543Smarks zfs_free_allows(*zfs_perms); 18034543Smarks nvlist_free(nvlist); 18044543Smarks return (-1); 18054543Smarks } 18064543Smarks 18075993Smarks static char * 18085993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note) 18095993Smarks { 18105993Smarks /* 18115993Smarks * Don't put newlines on end of lines 18125993Smarks */ 18135993Smarks switch (note) { 18145993Smarks case ZFS_DELEG_NOTE_CREATE: 18155993Smarks return (dgettext(TEXT_DOMAIN, 18165993Smarks "Must also have the 'mount' ability")); 18175993Smarks case ZFS_DELEG_NOTE_DESTROY: 18185993Smarks return (dgettext(TEXT_DOMAIN, 18195993Smarks "Must also have the 'mount' ability")); 18205993Smarks case ZFS_DELEG_NOTE_SNAPSHOT: 18215993Smarks return (dgettext(TEXT_DOMAIN, 18225993Smarks "Must also have the 'mount' ability")); 18235993Smarks case ZFS_DELEG_NOTE_ROLLBACK: 18245993Smarks return (dgettext(TEXT_DOMAIN, 18255993Smarks "Must also have the 'mount' ability")); 18265993Smarks case ZFS_DELEG_NOTE_CLONE: 18275993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'create' " 18285993Smarks "ability and 'mount'\n" 18295993Smarks "\t\t\t\tability in the origin file system")); 18305993Smarks case ZFS_DELEG_NOTE_PROMOTE: 18315993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n" 18325993Smarks "\t\t\t\tand 'promote' ability in the origin file system")); 18335993Smarks case ZFS_DELEG_NOTE_RENAME: 18345993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' " 18355993Smarks "and 'create' \n\t\t\t\tability in the new parent")); 18365993Smarks case ZFS_DELEG_NOTE_RECEIVE: 18375993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'" 18385993Smarks " and 'create' ability")); 18395993Smarks case ZFS_DELEG_NOTE_USERPROP: 18405993Smarks return (dgettext(TEXT_DOMAIN, 18415993Smarks "Allows changing any user property")); 18425993Smarks case ZFS_DELEG_NOTE_ALLOW: 18435993Smarks return (dgettext(TEXT_DOMAIN, 18445993Smarks "Must also have the permission that is being\n" 18455993Smarks "\t\t\t\tallowed")); 18465993Smarks case ZFS_DELEG_NOTE_MOUNT: 18475993Smarks return (dgettext(TEXT_DOMAIN, 18485993Smarks "Allows mount/umount of ZFS datasets")); 18495993Smarks case ZFS_DELEG_NOTE_SHARE: 18505993Smarks return (dgettext(TEXT_DOMAIN, 18515993Smarks "Allows sharing file systems over NFS or SMB\n" 18525993Smarks "\t\t\t\tprotocols")); 18535993Smarks case ZFS_DELEG_NOTE_NONE: 18545993Smarks default: 18555993Smarks return (dgettext(TEXT_DOMAIN, "")); 18565993Smarks } 18575993Smarks } 18585993Smarks 18595993Smarks typedef enum { 18605993Smarks ZFS_DELEG_SUBCOMMAND, 18615993Smarks ZFS_DELEG_PROP, 18625993Smarks ZFS_DELEG_OTHER 18635993Smarks } zfs_deleg_perm_type_t; 18645993Smarks 18655993Smarks /* 18665993Smarks * is the permission a subcommand or other? 18675993Smarks */ 18685993Smarks zfs_deleg_perm_type_t 18695993Smarks zfs_deleg_perm_type(const char *perm) 18705993Smarks { 18715993Smarks if (strcmp(perm, "userprop") == 0) 18725993Smarks return (ZFS_DELEG_OTHER); 18735993Smarks else 18745993Smarks return (ZFS_DELEG_SUBCOMMAND); 18755993Smarks } 18765993Smarks 18775993Smarks static char * 18785993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type) 18795993Smarks { 18805993Smarks switch (type) { 18815993Smarks case ZFS_DELEG_SUBCOMMAND: 18825993Smarks return (dgettext(TEXT_DOMAIN, "subcommand")); 18835993Smarks case ZFS_DELEG_PROP: 18845993Smarks return (dgettext(TEXT_DOMAIN, "property")); 18855993Smarks case ZFS_DELEG_OTHER: 18865993Smarks return (dgettext(TEXT_DOMAIN, "other")); 18875993Smarks } 18885993Smarks return (""); 18895993Smarks } 18905993Smarks 18915993Smarks /*ARGSUSED*/ 18925993Smarks static int 18935993Smarks zfs_deleg_prop_cb(int prop, void *cb) 18945993Smarks { 18955993Smarks if (zfs_prop_delegatable(prop)) 18965993Smarks (void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop), 18975993Smarks zfs_deleg_perm_type_str(ZFS_DELEG_PROP)); 18985993Smarks 18995993Smarks return (ZPROP_CONT); 19005993Smarks } 19015993Smarks 19025993Smarks void 19035993Smarks zfs_deleg_permissions(void) 19045993Smarks { 19055993Smarks int i; 19065993Smarks 19075993Smarks (void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME", 19085993Smarks "TYPE", "NOTES"); 19095993Smarks 19105993Smarks /* 19115993Smarks * First print out the subcommands 19125993Smarks */ 19135993Smarks for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) { 19145993Smarks (void) fprintf(stderr, "%-15s %-15s\t%s\n", 19155993Smarks zfs_deleg_perm_tab[i].z_perm, 19165993Smarks zfs_deleg_perm_type_str( 19175993Smarks zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)), 19185993Smarks zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note)); 19195993Smarks } 19205993Smarks 19215993Smarks (void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE, 19225993Smarks ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME); 19235993Smarks } 19245993Smarks 1925789Sahrens /* 1926789Sahrens * Given a property name and value, set the property for the given dataset. 1927789Sahrens */ 1928789Sahrens int 19292676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1930789Sahrens { 1931789Sahrens zfs_cmd_t zc = { 0 }; 19322676Seschrock int ret = -1; 19332676Seschrock prop_changelist_t *cl = NULL; 19342082Seschrock char errbuf[1024]; 19352082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 19362676Seschrock nvlist_t *nvl = NULL, *realprops; 19372676Seschrock zfs_prop_t prop; 19387509SMark.Musante@Sun.COM boolean_t do_prefix; 19397509SMark.Musante@Sun.COM uint64_t idx; 19402082Seschrock 19412082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 19422676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 19432082Seschrock zhp->zfs_name); 19442082Seschrock 19452676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 19462676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 19472676Seschrock (void) no_memory(hdl); 19482676Seschrock goto error; 1949789Sahrens } 1950789Sahrens 19517184Stimh if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl, 19522676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 19532676Seschrock goto error; 19545094Slling 19552676Seschrock nvlist_free(nvl); 19562676Seschrock nvl = realprops; 19572676Seschrock 19582676Seschrock prop = zfs_name_to_prop(propname); 19592676Seschrock 19607366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 19612676Seschrock goto error; 1962789Sahrens 1963789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 19642082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19652082Seschrock "child dataset with inherited mountpoint is used " 19662082Seschrock "in a non-global zone")); 19672082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1968789Sahrens goto error; 1969789Sahrens } 1970789Sahrens 19717509SMark.Musante@Sun.COM /* 19727509SMark.Musante@Sun.COM * If the dataset's canmount property is being set to noauto, 19737509SMark.Musante@Sun.COM * then we want to prevent unmounting & remounting it. 19747509SMark.Musante@Sun.COM */ 19757509SMark.Musante@Sun.COM do_prefix = !((prop == ZFS_PROP_CANMOUNT) && 19767509SMark.Musante@Sun.COM (zprop_string_to_index(prop, propval, &idx, 19777509SMark.Musante@Sun.COM ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO)); 19786168Shs24103 19796168Shs24103 if (do_prefix && (ret = changelist_prefix(cl)) != 0) 19807509SMark.Musante@Sun.COM goto error; 1981789Sahrens 1982789Sahrens /* 1983789Sahrens * Execute the corresponding ioctl() to set this property. 1984789Sahrens */ 1985789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1986789Sahrens 19875094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 19882676Seschrock goto error; 19892676Seschrock 19904543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1991789Sahrens if (ret != 0) { 1992789Sahrens switch (errno) { 1993789Sahrens 1994789Sahrens case ENOSPC: 1995789Sahrens /* 1996789Sahrens * For quotas and reservations, ENOSPC indicates 1997789Sahrens * something different; setting a quota or reservation 1998789Sahrens * doesn't use any disk space. 1999789Sahrens */ 2000789Sahrens switch (prop) { 2001789Sahrens case ZFS_PROP_QUOTA: 20025378Sck153898 case ZFS_PROP_REFQUOTA: 20032082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20042082Seschrock "size is less than current used or " 20052082Seschrock "reserved space")); 20062082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 2007789Sahrens break; 2008789Sahrens 2009789Sahrens case ZFS_PROP_RESERVATION: 20105378Sck153898 case ZFS_PROP_REFRESERVATION: 20112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20122082Seschrock "size is greater than available space")); 20132082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 2014789Sahrens break; 2015789Sahrens 2016789Sahrens default: 20172082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 2018789Sahrens break; 2019789Sahrens } 2020789Sahrens break; 2021789Sahrens 2022789Sahrens case EBUSY: 20232082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 20242082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 20252082Seschrock else 20262676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 2027789Sahrens break; 2028789Sahrens 20291175Slling case EROFS: 20302082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 20311175Slling break; 20321175Slling 20333886Sahl case ENOTSUP: 20343886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20355977Smarks "pool and or dataset must be upgraded to set this " 20364603Sahrens "property or value")); 20373886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 20383886Sahl break; 20393886Sahl 20407042Sgw25295 case ERANGE: 20417042Sgw25295 if (prop == ZFS_PROP_COMPRESSION) { 20427042Sgw25295 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20437042Sgw25295 "property setting is not allowed on " 20447042Sgw25295 "bootable datasets")); 20457042Sgw25295 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 20467042Sgw25295 } else { 20477042Sgw25295 (void) zfs_standard_error(hdl, errno, errbuf); 20487042Sgw25295 } 20497042Sgw25295 break; 20507042Sgw25295 2051789Sahrens case EOVERFLOW: 2052789Sahrens /* 2053789Sahrens * This platform can't address a volume this big. 2054789Sahrens */ 2055789Sahrens #ifdef _ILP32 2056789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 20572082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 2058789Sahrens break; 2059789Sahrens } 2060789Sahrens #endif 20612082Seschrock /* FALLTHROUGH */ 2062789Sahrens default: 20632082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 2064789Sahrens } 2065789Sahrens } else { 20666168Shs24103 if (do_prefix) 20676168Shs24103 ret = changelist_postfix(cl); 20686168Shs24103 2069789Sahrens /* 2070789Sahrens * Refresh the statistics so the new property value 2071789Sahrens * is reflected. 2072789Sahrens */ 20736168Shs24103 if (ret == 0) 20742676Seschrock (void) get_stats(zhp); 2075789Sahrens } 2076789Sahrens 2077789Sahrens error: 20782676Seschrock nvlist_free(nvl); 20792676Seschrock zcmd_free_nvlists(&zc); 20802676Seschrock if (cl) 20812676Seschrock changelist_free(cl); 2082789Sahrens return (ret); 2083789Sahrens } 2084789Sahrens 2085789Sahrens /* 2086789Sahrens * Given a property, inherit the value from the parent dataset. 2087789Sahrens */ 2088789Sahrens int 20892676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 2090789Sahrens { 2091789Sahrens zfs_cmd_t zc = { 0 }; 2092789Sahrens int ret; 2093789Sahrens prop_changelist_t *cl; 20942082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 20952082Seschrock char errbuf[1024]; 20962676Seschrock zfs_prop_t prop; 20972082Seschrock 20982082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 20992082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 2100789Sahrens 21015094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 21022676Seschrock /* 21032676Seschrock * For user properties, the amount of work we have to do is very 21042676Seschrock * small, so just do it here. 21052676Seschrock */ 21062676Seschrock if (!zfs_prop_user(propname)) { 21072676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21082676Seschrock "invalid property")); 21092676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 21102676Seschrock } 21112676Seschrock 21122676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21132676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 21142676Seschrock 21154849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 21162676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 21172676Seschrock 21182676Seschrock return (0); 21192676Seschrock } 21202676Seschrock 2121789Sahrens /* 2122789Sahrens * Verify that this property is inheritable. 2123789Sahrens */ 21242082Seschrock if (zfs_prop_readonly(prop)) 21252082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 21262082Seschrock 21272082Seschrock if (!zfs_prop_inheritable(prop)) 21282082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2129789Sahrens 2130789Sahrens /* 2131789Sahrens * Check to see if the value applies to this type 2132789Sahrens */ 21332082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 21342082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2135789Sahrens 21363443Srm160521 /* 21373443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 21383443Srm160521 */ 21393443Srm160521 propname = zfs_prop_to_name(prop); 2140789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21412676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2142789Sahrens 2143789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2144789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 21452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21462082Seschrock "dataset is used in a non-global zone")); 21472082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2148789Sahrens } 2149789Sahrens 2150789Sahrens /* 2151789Sahrens * Determine datasets which will be affected by this change, if any. 2152789Sahrens */ 21537366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 2154789Sahrens return (-1); 2155789Sahrens 2156789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 21572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21582082Seschrock "child dataset with inherited mountpoint is used " 21592082Seschrock "in a non-global zone")); 21602082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2161789Sahrens goto error; 2162789Sahrens } 2163789Sahrens 2164789Sahrens if ((ret = changelist_prefix(cl)) != 0) 2165789Sahrens goto error; 2166789Sahrens 21674849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 21682082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2169789Sahrens } else { 2170789Sahrens 21712169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 2172789Sahrens goto error; 2173789Sahrens 2174789Sahrens /* 2175789Sahrens * Refresh the statistics so the new property is reflected. 2176789Sahrens */ 2177789Sahrens (void) get_stats(zhp); 2178789Sahrens } 2179789Sahrens 2180789Sahrens error: 2181789Sahrens changelist_free(cl); 2182789Sahrens return (ret); 2183789Sahrens } 2184789Sahrens 2185789Sahrens /* 21861356Seschrock * True DSL properties are stored in an nvlist. The following two functions 21871356Seschrock * extract them appropriately. 21881356Seschrock */ 21891356Seschrock static uint64_t 21901356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 21911356Seschrock { 21921356Seschrock nvlist_t *nv; 21931356Seschrock uint64_t value; 21941356Seschrock 21952885Sahrens *source = NULL; 21961356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 21971356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 21985094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 21995094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 22001356Seschrock } else { 2201*8802SSanjeev.Bagewadi@Sun.COM verify(!zhp->zfs_props_table || 2202*8802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table[prop] == B_TRUE); 22031356Seschrock value = zfs_prop_default_numeric(prop); 22041356Seschrock *source = ""; 22051356Seschrock } 22061356Seschrock 22071356Seschrock return (value); 22081356Seschrock } 22091356Seschrock 22101356Seschrock static char * 22111356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 22121356Seschrock { 22131356Seschrock nvlist_t *nv; 22141356Seschrock char *value; 22151356Seschrock 22162885Sahrens *source = NULL; 22171356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 22181356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 22195094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 22205094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 22211356Seschrock } else { 2222*8802SSanjeev.Bagewadi@Sun.COM verify(!zhp->zfs_props_table || 2223*8802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table[prop] == B_TRUE); 22241356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 22251356Seschrock value = ""; 22261356Seschrock *source = ""; 22271356Seschrock } 22281356Seschrock 22291356Seschrock return (value); 22301356Seschrock } 22311356Seschrock 22321356Seschrock /* 2233789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2234789Sahrens * zfs_prop_get_int() are built using this interface. 2235789Sahrens * 2236789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2237789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2238789Sahrens * If they differ from the on-disk values, report the current values and mark 2239789Sahrens * the source "temporary". 2240789Sahrens */ 22412082Seschrock static int 22425094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 22432082Seschrock char **source, uint64_t *val) 2244789Sahrens { 22455147Srm160521 zfs_cmd_t zc = { 0 }; 22465592Stimh nvlist_t *zplprops = NULL; 2247789Sahrens struct mnttab mnt; 22483265Sahrens char *mntopt_on = NULL; 22493265Sahrens char *mntopt_off = NULL; 2250789Sahrens 2251789Sahrens *source = NULL; 2252789Sahrens 22533265Sahrens switch (prop) { 22543265Sahrens case ZFS_PROP_ATIME: 22553265Sahrens mntopt_on = MNTOPT_ATIME; 22563265Sahrens mntopt_off = MNTOPT_NOATIME; 22573265Sahrens break; 22583265Sahrens 22593265Sahrens case ZFS_PROP_DEVICES: 22603265Sahrens mntopt_on = MNTOPT_DEVICES; 22613265Sahrens mntopt_off = MNTOPT_NODEVICES; 22623265Sahrens break; 22633265Sahrens 22643265Sahrens case ZFS_PROP_EXEC: 22653265Sahrens mntopt_on = MNTOPT_EXEC; 22663265Sahrens mntopt_off = MNTOPT_NOEXEC; 22673265Sahrens break; 22683265Sahrens 22693265Sahrens case ZFS_PROP_READONLY: 22703265Sahrens mntopt_on = MNTOPT_RO; 22713265Sahrens mntopt_off = MNTOPT_RW; 22723265Sahrens break; 22733265Sahrens 22743265Sahrens case ZFS_PROP_SETUID: 22753265Sahrens mntopt_on = MNTOPT_SETUID; 22763265Sahrens mntopt_off = MNTOPT_NOSETUID; 22773265Sahrens break; 22783265Sahrens 22793265Sahrens case ZFS_PROP_XATTR: 22803265Sahrens mntopt_on = MNTOPT_XATTR; 22813265Sahrens mntopt_off = MNTOPT_NOXATTR; 22823265Sahrens break; 22835331Samw 22845331Samw case ZFS_PROP_NBMAND: 22855331Samw mntopt_on = MNTOPT_NBMAND; 22865331Samw mntopt_off = MNTOPT_NONBMAND; 22875331Samw break; 22883265Sahrens } 22893265Sahrens 22902474Seschrock /* 22912474Seschrock * Because looking up the mount options is potentially expensive 22922474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 22932474Seschrock * we're looking up a property which requires its presence. 22942474Seschrock */ 22952474Seschrock if (!zhp->zfs_mntcheck && 22963265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 22978228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 22988228SEric.Taylor@Sun.COM struct mnttab entry; 22998228SEric.Taylor@Sun.COM 23008228SEric.Taylor@Sun.COM if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 23018228SEric.Taylor@Sun.COM zhp->zfs_mntopts = zfs_strdup(hdl, 23023265Sahrens entry.mnt_mntopts); 23033265Sahrens if (zhp->zfs_mntopts == NULL) 23043265Sahrens return (-1); 23053265Sahrens } 23062474Seschrock 23072474Seschrock zhp->zfs_mntcheck = B_TRUE; 23082474Seschrock } 23092474Seschrock 2310789Sahrens if (zhp->zfs_mntopts == NULL) 2311789Sahrens mnt.mnt_mntopts = ""; 2312789Sahrens else 2313789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2314789Sahrens 2315789Sahrens switch (prop) { 2316789Sahrens case ZFS_PROP_ATIME: 23173265Sahrens case ZFS_PROP_DEVICES: 23183265Sahrens case ZFS_PROP_EXEC: 23193265Sahrens case ZFS_PROP_READONLY: 23203265Sahrens case ZFS_PROP_SETUID: 23213265Sahrens case ZFS_PROP_XATTR: 23225331Samw case ZFS_PROP_NBMAND: 23232082Seschrock *val = getprop_uint64(zhp, prop, source); 23242082Seschrock 23253265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 23262082Seschrock *val = B_TRUE; 2327789Sahrens if (src) 23285094Slling *src = ZPROP_SRC_TEMPORARY; 23293265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 23302082Seschrock *val = B_FALSE; 2331789Sahrens if (src) 23325094Slling *src = ZPROP_SRC_TEMPORARY; 2333789Sahrens } 23342082Seschrock break; 2335789Sahrens 23363265Sahrens case ZFS_PROP_CANMOUNT: 23372082Seschrock *val = getprop_uint64(zhp, prop, source); 23386168Shs24103 if (*val != ZFS_CANMOUNT_ON) 23393417Srm160521 *source = zhp->zfs_name; 23403417Srm160521 else 23413417Srm160521 *source = ""; /* default */ 23422082Seschrock break; 2343789Sahrens 2344789Sahrens case ZFS_PROP_QUOTA: 23455378Sck153898 case ZFS_PROP_REFQUOTA: 2346789Sahrens case ZFS_PROP_RESERVATION: 23475378Sck153898 case ZFS_PROP_REFRESERVATION: 23482885Sahrens *val = getprop_uint64(zhp, prop, source); 23492885Sahrens if (*val == 0) 2350789Sahrens *source = ""; /* default */ 2351789Sahrens else 2352789Sahrens *source = zhp->zfs_name; 23532082Seschrock break; 2354789Sahrens 2355789Sahrens case ZFS_PROP_MOUNTED: 23562082Seschrock *val = (zhp->zfs_mntopts != NULL); 23572082Seschrock break; 2358789Sahrens 23593377Seschrock case ZFS_PROP_NUMCLONES: 23603377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 23613377Seschrock break; 23623377Seschrock 23635147Srm160521 case ZFS_PROP_VERSION: 23645498Stimh case ZFS_PROP_NORMALIZE: 23655498Stimh case ZFS_PROP_UTF8ONLY: 23665498Stimh case ZFS_PROP_CASE: 23675498Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 23685498Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 23695473Srm160521 return (-1); 23705147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23715498Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 23725498Stimh zcmd_free_nvlists(&zc); 23735147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 23745498Stimh "unable to get %s property"), 23755498Stimh zfs_prop_to_name(prop)); 23765147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 23775147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 23785147Srm160521 } 23795498Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 23805498Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 23815498Stimh val) != 0) { 23825498Stimh zcmd_free_nvlists(&zc); 23835498Stimh zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 23845498Stimh "unable to get %s property"), 23855498Stimh zfs_prop_to_name(prop)); 23865498Stimh return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM, 23875498Stimh dgettext(TEXT_DOMAIN, "internal error"))); 23885498Stimh } 23895592Stimh if (zplprops) 23905592Stimh nvlist_free(zplprops); 23915498Stimh zcmd_free_nvlists(&zc); 23925147Srm160521 break; 23935147Srm160521 2394789Sahrens default: 23954577Sahrens switch (zfs_prop_get_type(prop)) { 23964787Sahrens case PROP_TYPE_NUMBER: 23974787Sahrens case PROP_TYPE_INDEX: 23984577Sahrens *val = getprop_uint64(zhp, prop, source); 23997390SMatthew.Ahrens@Sun.COM /* 24007390SMatthew.Ahrens@Sun.COM * If we tried to use a defalut value for a 24017390SMatthew.Ahrens@Sun.COM * readonly property, it means that it was not 24027390SMatthew.Ahrens@Sun.COM * present; return an error. 24037390SMatthew.Ahrens@Sun.COM */ 24047390SMatthew.Ahrens@Sun.COM if (zfs_prop_readonly(prop) && 24057390SMatthew.Ahrens@Sun.COM *source && (*source)[0] == '\0') { 24067390SMatthew.Ahrens@Sun.COM return (-1); 24077390SMatthew.Ahrens@Sun.COM } 24084577Sahrens break; 24094577Sahrens 24104787Sahrens case PROP_TYPE_STRING: 24114577Sahrens default: 24124577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 24134577Sahrens "cannot get non-numeric property")); 24144577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 24154577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 24164577Sahrens } 2417789Sahrens } 2418789Sahrens 2419789Sahrens return (0); 2420789Sahrens } 2421789Sahrens 2422789Sahrens /* 2423789Sahrens * Calculate the source type, given the raw source string. 2424789Sahrens */ 2425789Sahrens static void 24265094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2427789Sahrens char *statbuf, size_t statlen) 2428789Sahrens { 24295094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2430789Sahrens return; 2431789Sahrens 2432789Sahrens if (source == NULL) { 24335094Slling *srctype = ZPROP_SRC_NONE; 2434789Sahrens } else if (source[0] == '\0') { 24355094Slling *srctype = ZPROP_SRC_DEFAULT; 2436789Sahrens } else { 2437789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 24385094Slling *srctype = ZPROP_SRC_LOCAL; 2439789Sahrens } else { 2440789Sahrens (void) strlcpy(statbuf, source, statlen); 24415094Slling *srctype = ZPROP_SRC_INHERITED; 2442789Sahrens } 2443789Sahrens } 2444789Sahrens 2445789Sahrens } 2446789Sahrens 2447789Sahrens /* 2448789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2449789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2450789Sahrens * human-readable form. 2451789Sahrens * 2452789Sahrens * Returns 0 on success, or -1 on error. 2453789Sahrens */ 2454789Sahrens int 2455789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 24565094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2457789Sahrens { 2458789Sahrens char *source = NULL; 2459789Sahrens uint64_t val; 2460789Sahrens char *str; 24612676Seschrock const char *strval; 2462789Sahrens 2463789Sahrens /* 2464789Sahrens * Check to see if this property applies to our object 2465789Sahrens */ 2466789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2467789Sahrens return (-1); 2468789Sahrens 2469789Sahrens if (src) 24705094Slling *src = ZPROP_SRC_NONE; 2471789Sahrens 2472789Sahrens switch (prop) { 2473789Sahrens case ZFS_PROP_CREATION: 2474789Sahrens /* 2475789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2476789Sahrens * this into a string unless 'literal' is specified. 2477789Sahrens */ 2478789Sahrens { 24792885Sahrens val = getprop_uint64(zhp, prop, &source); 24802885Sahrens time_t time = (time_t)val; 2481789Sahrens struct tm t; 2482789Sahrens 2483789Sahrens if (literal || 2484789Sahrens localtime_r(&time, &t) == NULL || 2485789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2486789Sahrens &t) == 0) 24872885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2488789Sahrens } 2489789Sahrens break; 2490789Sahrens 2491789Sahrens case ZFS_PROP_MOUNTPOINT: 2492789Sahrens /* 2493789Sahrens * Getting the precise mountpoint can be tricky. 2494789Sahrens * 2495789Sahrens * - for 'none' or 'legacy', return those values. 2496789Sahrens * - for inherited mountpoints, we want to take everything 2497789Sahrens * after our ancestor and append it to the inherited value. 2498789Sahrens * 2499789Sahrens * If the pool has an alternate root, we want to prepend that 2500789Sahrens * root to any values we return. 2501789Sahrens */ 25026865Srm160521 25031356Seschrock str = getprop_string(zhp, prop, &source); 25041356Seschrock 25056612Sgw25295 if (str[0] == '/') { 25066865Srm160521 char buf[MAXPATHLEN]; 25076865Srm160521 char *root = buf; 25081356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2509789Sahrens 2510789Sahrens if (relpath[0] == '/') 2511789Sahrens relpath++; 25126612Sgw25295 25136865Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 25146865Srm160521 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) || 25156865Srm160521 (strcmp(root, "-") == 0)) 25166865Srm160521 root[0] = '\0'; 25176612Sgw25295 /* 25186612Sgw25295 * Special case an alternate root of '/'. This will 25196612Sgw25295 * avoid having multiple leading slashes in the 25206612Sgw25295 * mountpoint path. 25216612Sgw25295 */ 25226612Sgw25295 if (strcmp(root, "/") == 0) 25236612Sgw25295 root++; 25246612Sgw25295 25256612Sgw25295 /* 25266612Sgw25295 * If the mountpoint is '/' then skip over this 25276612Sgw25295 * if we are obtaining either an alternate root or 25286612Sgw25295 * an inherited mountpoint. 25296612Sgw25295 */ 25306612Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 25316612Sgw25295 relpath[0] != '\0')) 25321356Seschrock str++; 2533789Sahrens 2534789Sahrens if (relpath[0] == '\0') 2535789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 25361356Seschrock root, str); 2537789Sahrens else 2538789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 25391356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2540789Sahrens relpath); 2541789Sahrens } else { 2542789Sahrens /* 'legacy' or 'none' */ 25431356Seschrock (void) strlcpy(propbuf, str, proplen); 2544789Sahrens } 2545789Sahrens 2546789Sahrens break; 2547789Sahrens 2548789Sahrens case ZFS_PROP_ORIGIN: 25492885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2550789Sahrens proplen); 2551789Sahrens /* 2552789Sahrens * If there is no parent at all, return failure to indicate that 2553789Sahrens * it doesn't apply to this dataset. 2554789Sahrens */ 2555789Sahrens if (propbuf[0] == '\0') 2556789Sahrens return (-1); 2557789Sahrens break; 2558789Sahrens 2559789Sahrens case ZFS_PROP_QUOTA: 25605378Sck153898 case ZFS_PROP_REFQUOTA: 2561789Sahrens case ZFS_PROP_RESERVATION: 25625378Sck153898 case ZFS_PROP_REFRESERVATION: 25635378Sck153898 25642082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 25652082Seschrock return (-1); 2566789Sahrens 2567789Sahrens /* 2568789Sahrens * If quota or reservation is 0, we translate this into 'none' 2569789Sahrens * (unless literal is set), and indicate that it's the default 2570789Sahrens * value. Otherwise, we print the number nicely and indicate 2571789Sahrens * that its set locally. 2572789Sahrens */ 2573789Sahrens if (val == 0) { 2574789Sahrens if (literal) 2575789Sahrens (void) strlcpy(propbuf, "0", proplen); 2576789Sahrens else 2577789Sahrens (void) strlcpy(propbuf, "none", proplen); 2578789Sahrens } else { 2579789Sahrens if (literal) 25802856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 25813912Slling (u_longlong_t)val); 2582789Sahrens else 2583789Sahrens zfs_nicenum(val, propbuf, proplen); 2584789Sahrens } 2585789Sahrens break; 2586789Sahrens 2587789Sahrens case ZFS_PROP_COMPRESSRATIO: 25882082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 25892082Seschrock return (-1); 25902856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 25912856Snd150628 val / 100, (longlong_t)val % 100); 2592789Sahrens break; 2593789Sahrens 2594789Sahrens case ZFS_PROP_TYPE: 2595789Sahrens switch (zhp->zfs_type) { 2596789Sahrens case ZFS_TYPE_FILESYSTEM: 2597789Sahrens str = "filesystem"; 2598789Sahrens break; 2599789Sahrens case ZFS_TYPE_VOLUME: 2600789Sahrens str = "volume"; 2601789Sahrens break; 2602789Sahrens case ZFS_TYPE_SNAPSHOT: 2603789Sahrens str = "snapshot"; 2604789Sahrens break; 2605789Sahrens default: 26062082Seschrock abort(); 2607789Sahrens } 2608789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2609789Sahrens break; 2610789Sahrens 2611789Sahrens case ZFS_PROP_MOUNTED: 2612789Sahrens /* 2613789Sahrens * The 'mounted' property is a pseudo-property that described 2614789Sahrens * whether the filesystem is currently mounted. Even though 2615789Sahrens * it's a boolean value, the typical values of "on" and "off" 2616789Sahrens * don't make sense, so we translate to "yes" and "no". 2617789Sahrens */ 26182082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 26192082Seschrock src, &source, &val) != 0) 26202082Seschrock return (-1); 26212082Seschrock if (val) 2622789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2623789Sahrens else 2624789Sahrens (void) strlcpy(propbuf, "no", proplen); 2625789Sahrens break; 2626789Sahrens 2627789Sahrens case ZFS_PROP_NAME: 2628789Sahrens /* 2629789Sahrens * The 'name' property is a pseudo-property derived from the 2630789Sahrens * dataset name. It is presented as a real property to simplify 2631789Sahrens * consumers. 2632789Sahrens */ 2633789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2634789Sahrens break; 2635789Sahrens 2636789Sahrens default: 26374577Sahrens switch (zfs_prop_get_type(prop)) { 26384787Sahrens case PROP_TYPE_NUMBER: 26394577Sahrens if (get_numeric_property(zhp, prop, src, 26404577Sahrens &source, &val) != 0) 26414577Sahrens return (-1); 26424577Sahrens if (literal) 26434577Sahrens (void) snprintf(propbuf, proplen, "%llu", 26444577Sahrens (u_longlong_t)val); 26454577Sahrens else 26464577Sahrens zfs_nicenum(val, propbuf, proplen); 26474577Sahrens break; 26484577Sahrens 26494787Sahrens case PROP_TYPE_STRING: 26504577Sahrens (void) strlcpy(propbuf, 26514577Sahrens getprop_string(zhp, prop, &source), proplen); 26524577Sahrens break; 26534577Sahrens 26544787Sahrens case PROP_TYPE_INDEX: 26554861Sahrens if (get_numeric_property(zhp, prop, src, 26564861Sahrens &source, &val) != 0) 26574861Sahrens return (-1); 26584861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 26594577Sahrens return (-1); 26604577Sahrens (void) strlcpy(propbuf, strval, proplen); 26614577Sahrens break; 26624577Sahrens 26634577Sahrens default: 26644577Sahrens abort(); 26654577Sahrens } 2666789Sahrens } 2667789Sahrens 2668789Sahrens get_source(zhp, src, source, statbuf, statlen); 2669789Sahrens 2670789Sahrens return (0); 2671789Sahrens } 2672789Sahrens 2673789Sahrens /* 2674789Sahrens * Utility function to get the given numeric property. Does no validation that 2675789Sahrens * the given property is the appropriate type; should only be used with 2676789Sahrens * hard-coded property types. 2677789Sahrens */ 2678789Sahrens uint64_t 2679789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2680789Sahrens { 2681789Sahrens char *source; 26822082Seschrock uint64_t val; 26832082Seschrock 26845367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 26852082Seschrock 26862082Seschrock return (val); 2687789Sahrens } 2688789Sahrens 26895713Srm160521 int 26905713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 26915713Srm160521 { 26925713Srm160521 char buf[64]; 26935713Srm160521 26945713Srm160521 zfs_nicenum(val, buf, sizeof (buf)); 26955713Srm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 26965713Srm160521 } 26975713Srm160521 2698789Sahrens /* 2699789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2700789Sahrens */ 2701789Sahrens int 2702789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 27035094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2704789Sahrens { 2705789Sahrens char *source; 2706789Sahrens 2707789Sahrens /* 2708789Sahrens * Check to see if this property applies to our object 2709789Sahrens */ 27104849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 27113237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 27122082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 27132082Seschrock zfs_prop_to_name(prop))); 27144849Sahrens } 2715789Sahrens 2716789Sahrens if (src) 27175094Slling *src = ZPROP_SRC_NONE; 2718789Sahrens 27192082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 27202082Seschrock return (-1); 2721789Sahrens 2722789Sahrens get_source(zhp, src, source, statbuf, statlen); 2723789Sahrens 2724789Sahrens return (0); 2725789Sahrens } 2726789Sahrens 2727789Sahrens /* 2728789Sahrens * Returns the name of the given zfs handle. 2729789Sahrens */ 2730789Sahrens const char * 2731789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2732789Sahrens { 2733789Sahrens return (zhp->zfs_name); 2734789Sahrens } 2735789Sahrens 2736789Sahrens /* 2737789Sahrens * Returns the type of the given zfs handle. 2738789Sahrens */ 2739789Sahrens zfs_type_t 2740789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2741789Sahrens { 2742789Sahrens return (zhp->zfs_type); 2743789Sahrens } 2744789Sahrens 27458228SEric.Taylor@Sun.COM static int 27468228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc) 27478228SEric.Taylor@Sun.COM { 27488228SEric.Taylor@Sun.COM int rc; 27498228SEric.Taylor@Sun.COM uint64_t orig_cookie; 27508228SEric.Taylor@Sun.COM 27518228SEric.Taylor@Sun.COM orig_cookie = zc->zc_cookie; 27528228SEric.Taylor@Sun.COM top: 27538228SEric.Taylor@Sun.COM (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 27548228SEric.Taylor@Sun.COM rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc); 27558228SEric.Taylor@Sun.COM 27568228SEric.Taylor@Sun.COM if (rc == -1) { 27578228SEric.Taylor@Sun.COM switch (errno) { 27588228SEric.Taylor@Sun.COM case ENOMEM: 27598228SEric.Taylor@Sun.COM /* expand nvlist memory and try again */ 27608228SEric.Taylor@Sun.COM if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) { 27618228SEric.Taylor@Sun.COM zcmd_free_nvlists(zc); 27628228SEric.Taylor@Sun.COM return (-1); 27638228SEric.Taylor@Sun.COM } 27648228SEric.Taylor@Sun.COM zc->zc_cookie = orig_cookie; 27658228SEric.Taylor@Sun.COM goto top; 27668228SEric.Taylor@Sun.COM /* 27678228SEric.Taylor@Sun.COM * An errno value of ESRCH indicates normal completion. 27688228SEric.Taylor@Sun.COM * If ENOENT is returned, then the underlying dataset 27698228SEric.Taylor@Sun.COM * has been removed since we obtained the handle. 27708228SEric.Taylor@Sun.COM */ 27718228SEric.Taylor@Sun.COM case ESRCH: 27728228SEric.Taylor@Sun.COM case ENOENT: 27738228SEric.Taylor@Sun.COM rc = 1; 27748228SEric.Taylor@Sun.COM break; 27758228SEric.Taylor@Sun.COM default: 27768228SEric.Taylor@Sun.COM rc = zfs_standard_error(zhp->zfs_hdl, errno, 27778228SEric.Taylor@Sun.COM dgettext(TEXT_DOMAIN, 27788228SEric.Taylor@Sun.COM "cannot iterate filesystems")); 27798228SEric.Taylor@Sun.COM break; 27808228SEric.Taylor@Sun.COM } 27818228SEric.Taylor@Sun.COM } 27828228SEric.Taylor@Sun.COM return (rc); 27838228SEric.Taylor@Sun.COM } 27848228SEric.Taylor@Sun.COM 2785789Sahrens /* 27861356Seschrock * Iterate over all child filesystems 2787789Sahrens */ 2788789Sahrens int 27891356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2790789Sahrens { 2791789Sahrens zfs_cmd_t zc = { 0 }; 2792789Sahrens zfs_handle_t *nzhp; 2793789Sahrens int ret; 2794789Sahrens 27955367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 27965367Sahrens return (0); 27975367Sahrens 27988228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 27998228SEric.Taylor@Sun.COM return (-1); 28008228SEric.Taylor@Sun.COM 28018228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT, 28028228SEric.Taylor@Sun.COM &zc)) == 0) { 2803789Sahrens /* 2804789Sahrens * Ignore private dataset names. 2805789Sahrens */ 2806789Sahrens if (dataset_name_hidden(zc.zc_name)) 2807789Sahrens continue; 2808789Sahrens 2809789Sahrens /* 2810789Sahrens * Silently ignore errors, as the only plausible explanation is 2811789Sahrens * that the pool has since been removed. 2812789Sahrens */ 28138228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 28148228SEric.Taylor@Sun.COM &zc)) == NULL) { 2815789Sahrens continue; 28168228SEric.Taylor@Sun.COM } 28178228SEric.Taylor@Sun.COM 28188228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 28198228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2820789Sahrens return (ret); 28218228SEric.Taylor@Sun.COM } 2822789Sahrens } 28238228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 28248228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 28251356Seschrock } 28261356Seschrock 28271356Seschrock /* 28281356Seschrock * Iterate over all snapshots 28291356Seschrock */ 28301356Seschrock int 28311356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 28321356Seschrock { 28331356Seschrock zfs_cmd_t zc = { 0 }; 28341356Seschrock zfs_handle_t *nzhp; 28351356Seschrock int ret; 2836789Sahrens 28375367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 28385367Sahrens return (0); 28395367Sahrens 28408228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 28418228SEric.Taylor@Sun.COM return (-1); 28428228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, 28438228SEric.Taylor@Sun.COM &zc)) == 0) { 28448228SEric.Taylor@Sun.COM 28458228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 28468228SEric.Taylor@Sun.COM &zc)) == NULL) { 2847789Sahrens continue; 28488228SEric.Taylor@Sun.COM } 28498228SEric.Taylor@Sun.COM 28508228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 28518228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2852789Sahrens return (ret); 28538228SEric.Taylor@Sun.COM } 2854789Sahrens } 28558228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 28568228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 2857789Sahrens } 2858789Sahrens 2859789Sahrens /* 28601356Seschrock * Iterate over all children, snapshots and filesystems 28611356Seschrock */ 28621356Seschrock int 28631356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 28641356Seschrock { 28651356Seschrock int ret; 28661356Seschrock 28671356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 28681356Seschrock return (ret); 28691356Seschrock 28701356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 28711356Seschrock } 28721356Seschrock 28731356Seschrock /* 2874789Sahrens * Given a complete name, return just the portion that refers to the parent. 2875789Sahrens * Can return NULL if this is a pool. 2876789Sahrens */ 2877789Sahrens static int 2878789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2879789Sahrens { 2880789Sahrens char *loc; 2881789Sahrens 2882789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2883789Sahrens return (-1); 2884789Sahrens 2885789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2886789Sahrens buf[loc - path] = '\0'; 2887789Sahrens 2888789Sahrens return (0); 2889789Sahrens } 2890789Sahrens 2891789Sahrens /* 28924490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 28934490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 28944490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 28954490Svb160487 * length of already existing prefix of the given path. We also fetch the 28964490Svb160487 * 'zoned' property, which is used to validate property settings when creating 28974490Svb160487 * new datasets. 2898789Sahrens */ 2899789Sahrens static int 29004490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 29014490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2902789Sahrens { 2903789Sahrens zfs_cmd_t zc = { 0 }; 2904789Sahrens char parent[ZFS_MAXNAMELEN]; 2905789Sahrens char *slash; 29061356Seschrock zfs_handle_t *zhp; 29072082Seschrock char errbuf[1024]; 29082082Seschrock 29098269SMark.Musante@Sun.COM (void) snprintf(errbuf, sizeof (errbuf), 29108269SMark.Musante@Sun.COM dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 2911789Sahrens 2912789Sahrens /* get parent, and check to see if this is just a pool */ 2913789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 29142082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29152082Seschrock "missing dataset name")); 29162082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2917789Sahrens } 2918789Sahrens 2919789Sahrens /* check to see if the pool exists */ 2920789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2921789Sahrens slash = parent + strlen(parent); 2922789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2923789Sahrens zc.zc_name[slash - parent] = '\0'; 29242082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2925789Sahrens errno == ENOENT) { 29262082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29272082Seschrock "no such pool '%s'"), zc.zc_name); 29282082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2929789Sahrens } 2930789Sahrens 2931789Sahrens /* check to see if the parent dataset exists */ 29324490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 29334490Svb160487 if (errno == ENOENT && accept_ancestor) { 29344490Svb160487 /* 29354490Svb160487 * Go deeper to find an ancestor, give up on top level. 29364490Svb160487 */ 29374490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 29384490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29394490Svb160487 "no such pool '%s'"), zc.zc_name); 29404490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 29414490Svb160487 } 29424490Svb160487 } else if (errno == ENOENT) { 29432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29442082Seschrock "parent does not exist")); 29452082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 29464490Svb160487 } else 29472082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2948789Sahrens } 2949789Sahrens 29502676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2951789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 29522676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 29532082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 29541356Seschrock zfs_close(zhp); 2955789Sahrens return (-1); 2956789Sahrens } 2957789Sahrens 2958789Sahrens /* make sure parent is a filesystem */ 29591356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 29602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29612082Seschrock "parent is not a filesystem")); 29622082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 29631356Seschrock zfs_close(zhp); 2964789Sahrens return (-1); 2965789Sahrens } 2966789Sahrens 29671356Seschrock zfs_close(zhp); 29684490Svb160487 if (prefixlen != NULL) 29694490Svb160487 *prefixlen = strlen(parent); 29704490Svb160487 return (0); 29714490Svb160487 } 29724490Svb160487 29734490Svb160487 /* 29744490Svb160487 * Finds whether the dataset of the given type(s) exists. 29754490Svb160487 */ 29764490Svb160487 boolean_t 29774490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 29784490Svb160487 { 29794490Svb160487 zfs_handle_t *zhp; 29804490Svb160487 29815326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 29824490Svb160487 return (B_FALSE); 29834490Svb160487 29844490Svb160487 /* 29854490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 29864490Svb160487 */ 29874490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 29884490Svb160487 int ds_type = zhp->zfs_type; 29894490Svb160487 29904490Svb160487 zfs_close(zhp); 29914490Svb160487 if (types & ds_type) 29924490Svb160487 return (B_TRUE); 29934490Svb160487 } 29944490Svb160487 return (B_FALSE); 29954490Svb160487 } 29964490Svb160487 29974490Svb160487 /* 29985367Sahrens * Given a path to 'target', create all the ancestors between 29995367Sahrens * the prefixlen portion of the path, and the target itself. 30005367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 30015367Sahrens */ 30025367Sahrens int 30035367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 30045367Sahrens { 30055367Sahrens zfs_handle_t *h; 30065367Sahrens char *cp; 30075367Sahrens const char *opname; 30085367Sahrens 30095367Sahrens /* make sure prefix exists */ 30105367Sahrens cp = target + prefixlen; 30115367Sahrens if (*cp != '/') { 30125367Sahrens assert(strchr(cp, '/') == NULL); 30135367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30145367Sahrens } else { 30155367Sahrens *cp = '\0'; 30165367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30175367Sahrens *cp = '/'; 30185367Sahrens } 30195367Sahrens if (h == NULL) 30205367Sahrens return (-1); 30215367Sahrens zfs_close(h); 30225367Sahrens 30235367Sahrens /* 30245367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 30255367Sahrens * up to the prefixlen-long one. 30265367Sahrens */ 30275367Sahrens for (cp = target + prefixlen + 1; 30285367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 30295367Sahrens char *logstr; 30305367Sahrens 30315367Sahrens *cp = '\0'; 30325367Sahrens 30335367Sahrens h = make_dataset_handle(hdl, target); 30345367Sahrens if (h) { 30355367Sahrens /* it already exists, nothing to do here */ 30365367Sahrens zfs_close(h); 30375367Sahrens continue; 30385367Sahrens } 30395367Sahrens 30405367Sahrens logstr = hdl->libzfs_log_str; 30415367Sahrens hdl->libzfs_log_str = NULL; 30425367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 30435367Sahrens NULL) != 0) { 30445367Sahrens hdl->libzfs_log_str = logstr; 30455367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 30465367Sahrens goto ancestorerr; 30475367Sahrens } 30485367Sahrens 30495367Sahrens hdl->libzfs_log_str = logstr; 30505367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30515367Sahrens if (h == NULL) { 30525367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 30535367Sahrens goto ancestorerr; 30545367Sahrens } 30555367Sahrens 30565367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 30575367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 30585367Sahrens goto ancestorerr; 30595367Sahrens } 30605367Sahrens 30615367Sahrens if (zfs_share(h) != 0) { 30625367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 30635367Sahrens goto ancestorerr; 30645367Sahrens } 30655367Sahrens 30665367Sahrens zfs_close(h); 30675367Sahrens } 30685367Sahrens 30695367Sahrens return (0); 30705367Sahrens 30715367Sahrens ancestorerr: 30725367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30735367Sahrens "failed to %s ancestor '%s'"), opname, target); 30745367Sahrens return (-1); 30755367Sahrens } 30765367Sahrens 30775367Sahrens /* 30784490Svb160487 * Creates non-existing ancestors of the given path. 30794490Svb160487 */ 30804490Svb160487 int 30814490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 30824490Svb160487 { 30834490Svb160487 int prefix; 30844490Svb160487 uint64_t zoned; 30854490Svb160487 char *path_copy; 30864490Svb160487 int rc; 30874490Svb160487 30884490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 30894490Svb160487 return (-1); 30904490Svb160487 30914490Svb160487 if ((path_copy = strdup(path)) != NULL) { 30924490Svb160487 rc = create_parents(hdl, path_copy, prefix); 30934490Svb160487 free(path_copy); 30944490Svb160487 } 30954490Svb160487 if (path_copy == NULL || rc != 0) 30964490Svb160487 return (-1); 30974490Svb160487 3098789Sahrens return (0); 3099789Sahrens } 3100789Sahrens 3101789Sahrens /* 31022676Seschrock * Create a new filesystem or volume. 3103789Sahrens */ 3104789Sahrens int 31052082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 31062676Seschrock nvlist_t *props) 3107789Sahrens { 3108789Sahrens zfs_cmd_t zc = { 0 }; 3109789Sahrens int ret; 3110789Sahrens uint64_t size = 0; 3111789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 31122082Seschrock char errbuf[1024]; 31132676Seschrock uint64_t zoned; 31142082Seschrock 31152082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31162082Seschrock "cannot create '%s'"), path); 3117789Sahrens 3118789Sahrens /* validate the path, taking care to note the extended error message */ 31195326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 31202082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3121789Sahrens 3122789Sahrens /* validate parents exist */ 31234490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3124789Sahrens return (-1); 3125789Sahrens 3126789Sahrens /* 3127789Sahrens * The failure modes when creating a dataset of a different type over 3128789Sahrens * one that already exists is a little strange. In particular, if you 3129789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 3130789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 3131789Sahrens * first try to see if the dataset exists. 3132789Sahrens */ 3133789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 31345094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 31352082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31362082Seschrock "dataset already exists")); 31372082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3138789Sahrens } 3139789Sahrens 3140789Sahrens if (type == ZFS_TYPE_VOLUME) 3141789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3142789Sahrens else 3143789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3144789Sahrens 31457184Stimh if (props && (props = zfs_valid_proplist(hdl, type, props, 31463912Slling zoned, NULL, errbuf)) == 0) 31472676Seschrock return (-1); 31482676Seschrock 3149789Sahrens if (type == ZFS_TYPE_VOLUME) { 31501133Seschrock /* 31511133Seschrock * If we are creating a volume, the size and block size must 31521133Seschrock * satisfy a few restraints. First, the blocksize must be a 31531133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 31541133Seschrock * volsize must be a multiple of the block size, and cannot be 31551133Seschrock * zero. 31561133Seschrock */ 31572676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 31582676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 31592676Seschrock nvlist_free(props); 31602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31612676Seschrock "missing volume size")); 31622676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3163789Sahrens } 3164789Sahrens 31652676Seschrock if ((ret = nvlist_lookup_uint64(props, 31662676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 31672676Seschrock &blocksize)) != 0) { 31682676Seschrock if (ret == ENOENT) { 31692676Seschrock blocksize = zfs_prop_default_numeric( 31702676Seschrock ZFS_PROP_VOLBLOCKSIZE); 31712676Seschrock } else { 31722676Seschrock nvlist_free(props); 31732676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31742676Seschrock "missing volume block size")); 31752676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 31762676Seschrock } 31772676Seschrock } 31782676Seschrock 31792676Seschrock if (size == 0) { 31802676Seschrock nvlist_free(props); 31812082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31822676Seschrock "volume size cannot be zero")); 31832676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 31841133Seschrock } 31851133Seschrock 31861133Seschrock if (size % blocksize != 0) { 31872676Seschrock nvlist_free(props); 31882082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31892676Seschrock "volume size must be a multiple of volume block " 31902676Seschrock "size")); 31912676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 31921133Seschrock } 3193789Sahrens } 3194789Sahrens 31955094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 31962676Seschrock return (-1); 31972676Seschrock nvlist_free(props); 31982676Seschrock 3199789Sahrens /* create the dataset */ 32004543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 3201789Sahrens 32023912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 32032082Seschrock ret = zvol_create_link(hdl, path); 32043912Slling if (ret) { 32053912Slling (void) zfs_standard_error(hdl, errno, 32063912Slling dgettext(TEXT_DOMAIN, 32073912Slling "Volume successfully created, but device links " 32083912Slling "were not created")); 32093912Slling zcmd_free_nvlists(&zc); 32103912Slling return (-1); 32113912Slling } 32123912Slling } 3213789Sahrens 32142676Seschrock zcmd_free_nvlists(&zc); 32152676Seschrock 3216789Sahrens /* check for failure */ 3217789Sahrens if (ret != 0) { 3218789Sahrens char parent[ZFS_MAXNAMELEN]; 3219789Sahrens (void) parent_name(path, parent, sizeof (parent)); 3220789Sahrens 3221789Sahrens switch (errno) { 3222789Sahrens case ENOENT: 32232082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32242082Seschrock "no such parent '%s'"), parent); 32252082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3226789Sahrens 3227789Sahrens case EINVAL: 32282082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32293413Smmusante "parent '%s' is not a filesystem"), parent); 32302082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3231789Sahrens 3232789Sahrens case EDOM: 32332082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32342676Seschrock "volume block size must be power of 2 from " 32352676Seschrock "%u to %uk"), 3236789Sahrens (uint_t)SPA_MINBLOCKSIZE, 3237789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 32382082Seschrock 32392676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 32402082Seschrock 32414603Sahrens case ENOTSUP: 32424603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32434603Sahrens "pool must be upgraded to set this " 32444603Sahrens "property or value")); 32454603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3246789Sahrens #ifdef _ILP32 3247789Sahrens case EOVERFLOW: 3248789Sahrens /* 3249789Sahrens * This platform can't address a volume this big. 3250789Sahrens */ 32512082Seschrock if (type == ZFS_TYPE_VOLUME) 32522082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 32532082Seschrock errbuf)); 3254789Sahrens #endif 32552082Seschrock /* FALLTHROUGH */ 3256789Sahrens default: 32572082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3258789Sahrens } 3259789Sahrens } 3260789Sahrens 3261789Sahrens return (0); 3262789Sahrens } 3263789Sahrens 3264789Sahrens /* 3265789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 3266789Sahrens * isn't mounted, and that there are no active dependents. 3267789Sahrens */ 3268789Sahrens int 3269789Sahrens zfs_destroy(zfs_handle_t *zhp) 3270789Sahrens { 3271789Sahrens zfs_cmd_t zc = { 0 }; 3272789Sahrens 3273789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3274789Sahrens 32752676Seschrock if (ZFS_IS_VOLUME(zhp)) { 32763126Sahl /* 32774543Smarks * If user doesn't have permissions to unshare volume, then 32784543Smarks * abort the request. This would only happen for a 32794543Smarks * non-privileged user. 32803126Sahl */ 32814543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 32824543Smarks return (-1); 32834543Smarks } 32843126Sahl 32852082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3286789Sahrens return (-1); 3287789Sahrens 3288789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3289789Sahrens } else { 3290789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3291789Sahrens } 3292789Sahrens 32934543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 32943237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 32952082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 32962082Seschrock zhp->zfs_name)); 32972199Sahrens } 3298789Sahrens 3299789Sahrens remove_mountpoint(zhp); 3300789Sahrens 3301789Sahrens return (0); 3302789Sahrens } 3303789Sahrens 33042199Sahrens struct destroydata { 33052199Sahrens char *snapname; 33062199Sahrens boolean_t gotone; 33073265Sahrens boolean_t closezhp; 33082199Sahrens }; 33092199Sahrens 33102199Sahrens static int 33112199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 33122199Sahrens { 33132199Sahrens struct destroydata *dd = arg; 33142199Sahrens zfs_handle_t *szhp; 33152199Sahrens char name[ZFS_MAXNAMELEN]; 33163265Sahrens boolean_t closezhp = dd->closezhp; 33173265Sahrens int rv; 33182199Sahrens 33192676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 33202676Seschrock (void) strlcat(name, "@", sizeof (name)); 33212676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 33222199Sahrens 33232199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 33242199Sahrens if (szhp) { 33252199Sahrens dd->gotone = B_TRUE; 33262199Sahrens zfs_close(szhp); 33272199Sahrens } 33282199Sahrens 33292199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33302199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 33312199Sahrens /* 33322199Sahrens * NB: this is simply a best-effort. We don't want to 33332199Sahrens * return an error, because then we wouldn't visit all 33342199Sahrens * the volumes. 33352199Sahrens */ 33362199Sahrens } 33372199Sahrens 33383265Sahrens dd->closezhp = B_TRUE; 33393265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 33403265Sahrens if (closezhp) 33413265Sahrens zfs_close(zhp); 33423265Sahrens return (rv); 33432199Sahrens } 33442199Sahrens 33452199Sahrens /* 33462199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 33472199Sahrens */ 33482199Sahrens int 33492199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 33502199Sahrens { 33512199Sahrens zfs_cmd_t zc = { 0 }; 33522199Sahrens int ret; 33532199Sahrens struct destroydata dd = { 0 }; 33542199Sahrens 33552199Sahrens dd.snapname = snapname; 33562199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 33572199Sahrens 33582199Sahrens if (!dd.gotone) { 33593237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 33602199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 33612199Sahrens zhp->zfs_name, snapname)); 33622199Sahrens } 33632199Sahrens 33642199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33652676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 33662199Sahrens 33674543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 33682199Sahrens if (ret != 0) { 33692199Sahrens char errbuf[1024]; 33702199Sahrens 33712199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33722199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 33732199Sahrens 33742199Sahrens switch (errno) { 33752199Sahrens case EEXIST: 33762199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 33772199Sahrens "snapshot is cloned")); 33782199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 33792199Sahrens 33802199Sahrens default: 33812199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 33822199Sahrens errbuf)); 33832199Sahrens } 33842199Sahrens } 33852199Sahrens 33862199Sahrens return (0); 33872199Sahrens } 33882199Sahrens 3389789Sahrens /* 3390789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3391789Sahrens */ 3392789Sahrens int 33932676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3394789Sahrens { 3395789Sahrens zfs_cmd_t zc = { 0 }; 3396789Sahrens char parent[ZFS_MAXNAMELEN]; 3397789Sahrens int ret; 33982082Seschrock char errbuf[1024]; 33992082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 34002676Seschrock zfs_type_t type; 34012676Seschrock uint64_t zoned; 3402789Sahrens 3403789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3404789Sahrens 34052082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34062082Seschrock "cannot create '%s'"), target); 34072082Seschrock 3408789Sahrens /* validate the target name */ 34095326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 34102082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3411789Sahrens 3412789Sahrens /* validate parents exist */ 34134490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3414789Sahrens return (-1); 3415789Sahrens 3416789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3417789Sahrens 3418789Sahrens /* do the clone */ 34192676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3420789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 34212744Snn35248 type = ZFS_TYPE_VOLUME; 34222676Seschrock } else { 3423789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 34242744Snn35248 type = ZFS_TYPE_FILESYSTEM; 34252676Seschrock } 34262676Seschrock 34272676Seschrock if (props) { 34287184Stimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 34297184Stimh zhp, errbuf)) == NULL) 34302676Seschrock return (-1); 34312676Seschrock 34325094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 34332676Seschrock nvlist_free(props); 34342676Seschrock return (-1); 34352676Seschrock } 34362676Seschrock 34372676Seschrock nvlist_free(props); 34382676Seschrock } 3439789Sahrens 3440789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 34412676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 34424543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3443789Sahrens 34442676Seschrock zcmd_free_nvlists(&zc); 34452676Seschrock 3446789Sahrens if (ret != 0) { 3447789Sahrens switch (errno) { 3448789Sahrens 3449789Sahrens case ENOENT: 3450789Sahrens /* 3451789Sahrens * The parent doesn't exist. We should have caught this 3452789Sahrens * above, but there may a race condition that has since 3453789Sahrens * destroyed the parent. 3454789Sahrens * 3455789Sahrens * At this point, we don't know whether it's the source 3456789Sahrens * that doesn't exist anymore, or whether the target 3457789Sahrens * dataset doesn't exist. 3458789Sahrens */ 34592082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 34602082Seschrock "no such parent '%s'"), parent); 34612082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 34622082Seschrock 34632082Seschrock case EXDEV: 34642082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 34652082Seschrock "source and target pools differ")); 34662082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 34672082Seschrock errbuf)); 34682082Seschrock 34692082Seschrock default: 34702082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 34712082Seschrock errbuf)); 34722082Seschrock } 34732676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 34742082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 34752082Seschrock } 34762082Seschrock 34772082Seschrock return (ret); 34782082Seschrock } 34792082Seschrock 34802082Seschrock typedef struct promote_data { 34812082Seschrock char cb_mountpoint[MAXPATHLEN]; 34822082Seschrock const char *cb_target; 34832082Seschrock const char *cb_errbuf; 34842082Seschrock uint64_t cb_pivot_txg; 34852082Seschrock } promote_data_t; 34862082Seschrock 34872082Seschrock static int 34882082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 34892082Seschrock { 34902082Seschrock promote_data_t *pd = data; 34912082Seschrock zfs_handle_t *szhp; 34922082Seschrock char snapname[MAXPATHLEN]; 34933265Sahrens int rv = 0; 34942082Seschrock 34952082Seschrock /* We don't care about snapshots after the pivot point */ 34963265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 34973265Sahrens zfs_close(zhp); 34982082Seschrock return (0); 34993265Sahrens } 35002082Seschrock 35012417Sahrens /* Remove the device link if it's a zvol. */ 35022676Seschrock if (ZFS_IS_VOLUME(zhp)) 35032417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 35042082Seschrock 35052082Seschrock /* Check for conflicting names */ 35062676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 35072676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 35082082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 35092082Seschrock if (szhp != NULL) { 35102082Seschrock zfs_close(szhp); 35112082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 35122082Seschrock "snapshot name '%s' from origin \n" 35132082Seschrock "conflicts with '%s' from target"), 35142082Seschrock zhp->zfs_name, snapname); 35153265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 35162082Seschrock } 35173265Sahrens zfs_close(zhp); 35183265Sahrens return (rv); 35192082Seschrock } 35202082Seschrock 35212417Sahrens static int 35222417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 35232417Sahrens { 35242417Sahrens promote_data_t *pd = data; 35252417Sahrens 35262417Sahrens /* We don't care about snapshots after the pivot point */ 35273265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 35283265Sahrens /* Create the device link if it's a zvol. */ 35293265Sahrens if (ZFS_IS_VOLUME(zhp)) 35303265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 35313265Sahrens } 35323265Sahrens 35333265Sahrens zfs_close(zhp); 35342417Sahrens return (0); 35352417Sahrens } 35362417Sahrens 35372082Seschrock /* 35382082Seschrock * Promotes the given clone fs to be the clone parent. 35392082Seschrock */ 35402082Seschrock int 35412082Seschrock zfs_promote(zfs_handle_t *zhp) 35422082Seschrock { 35432082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 35442082Seschrock zfs_cmd_t zc = { 0 }; 35452082Seschrock char parent[MAXPATHLEN]; 35462082Seschrock char *cp; 35472082Seschrock int ret; 35482082Seschrock zfs_handle_t *pzhp; 35492082Seschrock promote_data_t pd; 35502082Seschrock char errbuf[1024]; 35512082Seschrock 35522082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35532082Seschrock "cannot promote '%s'"), zhp->zfs_name); 35542082Seschrock 35552082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 35562082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35572082Seschrock "snapshots can not be promoted")); 35582082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 35592082Seschrock } 35602082Seschrock 35615367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 35622082Seschrock if (parent[0] == '\0') { 35632082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35642082Seschrock "not a cloned filesystem")); 35652082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 35662082Seschrock } 35672082Seschrock cp = strchr(parent, '@'); 35682082Seschrock *cp = '\0'; 35692082Seschrock 35702082Seschrock /* Walk the snapshots we will be moving */ 35715367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 35722082Seschrock if (pzhp == NULL) 35732082Seschrock return (-1); 35742082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 35752082Seschrock zfs_close(pzhp); 35762082Seschrock pd.cb_target = zhp->zfs_name; 35772082Seschrock pd.cb_errbuf = errbuf; 35785094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 35792082Seschrock if (pzhp == NULL) 35802082Seschrock return (-1); 35812082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 35822082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 35832082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 35842417Sahrens if (ret != 0) { 35852417Sahrens zfs_close(pzhp); 35862082Seschrock return (-1); 35872417Sahrens } 35882082Seschrock 35892082Seschrock /* issue the ioctl */ 35905367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 35912676Seschrock sizeof (zc.zc_value)); 35922082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35934543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 35942082Seschrock 35952082Seschrock if (ret != 0) { 35962417Sahrens int save_errno = errno; 35972417Sahrens 35982417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 35992417Sahrens zfs_close(pzhp); 36002417Sahrens 36012417Sahrens switch (save_errno) { 3602789Sahrens case EEXIST: 3603789Sahrens /* 36042082Seschrock * There is a conflicting snapshot name. We 36052082Seschrock * should have caught this above, but they could 36062082Seschrock * have renamed something in the mean time. 3607789Sahrens */ 36082082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36092082Seschrock "conflicting snapshot name from parent '%s'"), 36102082Seschrock parent); 36112082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3612789Sahrens 3613789Sahrens default: 36142417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3615789Sahrens } 36162417Sahrens } else { 36172417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3618789Sahrens } 3619789Sahrens 36202417Sahrens zfs_close(pzhp); 3621789Sahrens return (ret); 3622789Sahrens } 3623789Sahrens 36244007Smmusante struct createdata { 36254007Smmusante const char *cd_snapname; 36264007Smmusante int cd_ifexists; 36274007Smmusante }; 36284007Smmusante 36292199Sahrens static int 36302199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 36312199Sahrens { 36324007Smmusante struct createdata *cd = arg; 36332676Seschrock int ret; 36342199Sahrens 36352199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 36362199Sahrens char name[MAXPATHLEN]; 36372199Sahrens 36382676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 36392676Seschrock (void) strlcat(name, "@", sizeof (name)); 36404007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 36414007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 36424007Smmusante cd->cd_ifexists); 36432199Sahrens /* 36442199Sahrens * NB: this is simply a best-effort. We don't want to 36452199Sahrens * return an error, because then we wouldn't visit all 36462199Sahrens * the volumes. 36472199Sahrens */ 36482199Sahrens } 36492676Seschrock 36504007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 36512676Seschrock 36522676Seschrock zfs_close(zhp); 36532676Seschrock 36542676Seschrock return (ret); 36552199Sahrens } 36562199Sahrens 3657789Sahrens /* 36583504Sahl * Takes a snapshot of the given dataset. 3659789Sahrens */ 3660789Sahrens int 36617265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 36627265Sahrens nvlist_t *props) 3663789Sahrens { 3664789Sahrens const char *delim; 36657265Sahrens char parent[ZFS_MAXNAMELEN]; 3666789Sahrens zfs_handle_t *zhp; 3667789Sahrens zfs_cmd_t zc = { 0 }; 3668789Sahrens int ret; 36692082Seschrock char errbuf[1024]; 36702082Seschrock 36712082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36722082Seschrock "cannot snapshot '%s'"), path); 36732082Seschrock 36742082Seschrock /* validate the target name */ 36755326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 36762082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3677789Sahrens 36787265Sahrens if (props) { 36797265Sahrens if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 36807265Sahrens props, B_FALSE, NULL, errbuf)) == NULL) 36817265Sahrens return (-1); 36827265Sahrens 36837265Sahrens if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 36847265Sahrens nvlist_free(props); 36857265Sahrens return (-1); 36867265Sahrens } 36877265Sahrens 36887265Sahrens nvlist_free(props); 36897265Sahrens } 36907265Sahrens 3691789Sahrens /* make sure the parent exists and is of the appropriate type */ 36922199Sahrens delim = strchr(path, '@'); 3693789Sahrens (void) strncpy(parent, path, delim - path); 3694789Sahrens parent[delim - path] = '\0'; 3695789Sahrens 36962082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3697789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 36987265Sahrens zcmd_free_nvlists(&zc); 3699789Sahrens return (-1); 3700789Sahrens } 3701789Sahrens 37022199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 37032676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 37044543Smarks if (ZFS_IS_VOLUME(zhp)) 37054543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 37064543Smarks else 37074543Smarks zc.zc_objset_type = DMU_OST_ZFS; 37082199Sahrens zc.zc_cookie = recursive; 37094543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 37102199Sahrens 37117265Sahrens zcmd_free_nvlists(&zc); 37127265Sahrens 37132199Sahrens /* 37142199Sahrens * if it was recursive, the one that actually failed will be in 37152199Sahrens * zc.zc_name. 37162199Sahrens */ 37174543Smarks if (ret != 0) 37184543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37194543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 37204543Smarks 37212199Sahrens if (ret == 0 && recursive) { 37224007Smmusante struct createdata cd; 37234007Smmusante 37244007Smmusante cd.cd_snapname = delim + 1; 37254007Smmusante cd.cd_ifexists = B_FALSE; 37264007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 37272199Sahrens } 3728789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 37292082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 37302199Sahrens if (ret != 0) { 37314543Smarks (void) zfs_standard_error(hdl, errno, 37324543Smarks dgettext(TEXT_DOMAIN, 37334543Smarks "Volume successfully snapshotted, but device links " 37344543Smarks "were not created")); 37354543Smarks zfs_close(zhp); 37364543Smarks return (-1); 37372199Sahrens } 3738789Sahrens } 3739789Sahrens 37402082Seschrock if (ret != 0) 37412082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3742789Sahrens 3743789Sahrens zfs_close(zhp); 3744789Sahrens 3745789Sahrens return (ret); 3746789Sahrens } 3747789Sahrens 3748789Sahrens /* 37491294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 37501294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 37511294Slling * is a dependent and we should just destroy it without checking the transaction 37521294Slling * group. 3753789Sahrens */ 37541294Slling typedef struct rollback_data { 37551294Slling const char *cb_target; /* the snapshot */ 37561294Slling uint64_t cb_create; /* creation time reference */ 37575749Sahrens boolean_t cb_error; 37582082Seschrock boolean_t cb_dependent; 37595749Sahrens boolean_t cb_force; 37601294Slling } rollback_data_t; 37611294Slling 37621294Slling static int 37631294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 37641294Slling { 37651294Slling rollback_data_t *cbp = data; 37661294Slling 37671294Slling if (!cbp->cb_dependent) { 37681294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 37691294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 37701294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 37711294Slling cbp->cb_create) { 37724543Smarks char *logstr; 37731294Slling 37742082Seschrock cbp->cb_dependent = B_TRUE; 37755446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 37765446Sahrens rollback_destroy, cbp); 37772082Seschrock cbp->cb_dependent = B_FALSE; 37781294Slling 37794543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 37804543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 37815446Sahrens cbp->cb_error |= zfs_destroy(zhp); 37824543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 37831294Slling } 37841294Slling } else { 37855749Sahrens /* We must destroy this clone; first unmount it */ 37865749Sahrens prop_changelist_t *clp; 37875749Sahrens 37887366STim.Haley@Sun.COM clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 37895749Sahrens cbp->cb_force ? MS_FORCE: 0); 37905749Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 37915749Sahrens cbp->cb_error = B_TRUE; 37925749Sahrens zfs_close(zhp); 37935749Sahrens return (0); 37945749Sahrens } 37955749Sahrens if (zfs_destroy(zhp) != 0) 37965749Sahrens cbp->cb_error = B_TRUE; 37975749Sahrens else 37985749Sahrens changelist_remove(clp, zhp->zfs_name); 37995751Sahrens (void) changelist_postfix(clp); 38005749Sahrens changelist_free(clp); 38011294Slling } 38021294Slling 38031294Slling zfs_close(zhp); 38041294Slling return (0); 38051294Slling } 38061294Slling 38071294Slling /* 38085446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 38095446Sahrens * data changes since then and making it the active dataset. 38105446Sahrens * 38115446Sahrens * Any snapshots more recent than the target are destroyed, along with 38125446Sahrens * their dependents. 38131294Slling */ 38145446Sahrens int 38155749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3816789Sahrens { 38175446Sahrens rollback_data_t cb = { 0 }; 38185446Sahrens int err; 3819789Sahrens zfs_cmd_t zc = { 0 }; 38205713Srm160521 boolean_t restore_resv = 0; 38215713Srm160521 uint64_t old_volsize, new_volsize; 38225713Srm160521 zfs_prop_t resv_prop; 3823789Sahrens 3824789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3825789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3826789Sahrens 38275446Sahrens /* 38285446Sahrens * Destroy all recent snapshots and its dependends. 38295446Sahrens */ 38305749Sahrens cb.cb_force = force; 38315446Sahrens cb.cb_target = snap->zfs_name; 38325446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 38335446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 38345446Sahrens 38355749Sahrens if (cb.cb_error) 38365749Sahrens return (-1); 38375446Sahrens 38385446Sahrens /* 38395446Sahrens * Now that we have verified that the snapshot is the latest, 38405446Sahrens * rollback to the given snapshot. 38415446Sahrens */ 38425446Sahrens 38435713Srm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 38445713Srm160521 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 38455713Srm160521 return (-1); 38465713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 38475713Srm160521 return (-1); 38485713Srm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 38495713Srm160521 restore_resv = 38505713Srm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 38515713Srm160521 } 3852789Sahrens 3853789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3854789Sahrens 38552676Seschrock if (ZFS_IS_VOLUME(zhp)) 3856789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3857789Sahrens else 3858789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3859789Sahrens 3860789Sahrens /* 38615446Sahrens * We rely on zfs_iter_children() to verify that there are no 38625446Sahrens * newer snapshots for the given dataset. Therefore, we can 38635446Sahrens * simply pass the name on to the ioctl() call. There is still 38645446Sahrens * an unlikely race condition where the user has taken a 38655446Sahrens * snapshot since we verified that this was the most recent. 38665713Srm160521 * 3867789Sahrens */ 38685446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 38693237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 38702082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 38712082Seschrock zhp->zfs_name); 38725717Srm160521 return (err); 38735717Srm160521 } 38745713Srm160521 38755713Srm160521 /* 38765713Srm160521 * For volumes, if the pre-rollback volsize matched the pre- 38775713Srm160521 * rollback reservation and the volsize has changed then set 38785713Srm160521 * the reservation property to the post-rollback volsize. 38795713Srm160521 * Make a new handle since the rollback closed the dataset. 38805713Srm160521 */ 38815717Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 38825717Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 38835717Srm160521 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) { 38845717Srm160521 zfs_close(zhp); 38855713Srm160521 return (err); 38865717Srm160521 } 38875713Srm160521 if (restore_resv) { 38885713Srm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 38895713Srm160521 if (old_volsize != new_volsize) 38905717Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 38915717Srm160521 new_volsize); 38925713Srm160521 } 38935713Srm160521 zfs_close(zhp); 3894789Sahrens } 38955446Sahrens return (err); 38961294Slling } 38971294Slling 38981294Slling /* 3899789Sahrens * Iterate over all dependents for a given dataset. This includes both 3900789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3901789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3902789Sahrens * libzfs_graph.c. 3903789Sahrens */ 3904789Sahrens int 39052474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 39062474Seschrock zfs_iter_f func, void *data) 3907789Sahrens { 3908789Sahrens char **dependents; 3909789Sahrens size_t count; 3910789Sahrens int i; 3911789Sahrens zfs_handle_t *child; 3912789Sahrens int ret = 0; 3913789Sahrens 39142474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 39152474Seschrock &dependents, &count) != 0) 39162474Seschrock return (-1); 39172474Seschrock 3918789Sahrens for (i = 0; i < count; i++) { 39192082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 39202082Seschrock dependents[i])) == NULL) 3921789Sahrens continue; 3922789Sahrens 3923789Sahrens if ((ret = func(child, data)) != 0) 3924789Sahrens break; 3925789Sahrens } 3926789Sahrens 3927789Sahrens for (i = 0; i < count; i++) 3928789Sahrens free(dependents[i]); 3929789Sahrens free(dependents); 3930789Sahrens 3931789Sahrens return (ret); 3932789Sahrens } 3933789Sahrens 3934789Sahrens /* 3935789Sahrens * Renames the given dataset. 3936789Sahrens */ 3937789Sahrens int 39384490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3939789Sahrens { 3940789Sahrens int ret; 3941789Sahrens zfs_cmd_t zc = { 0 }; 3942789Sahrens char *delim; 39434007Smmusante prop_changelist_t *cl = NULL; 39444007Smmusante zfs_handle_t *zhrp = NULL; 39454007Smmusante char *parentname = NULL; 3946789Sahrens char parent[ZFS_MAXNAMELEN]; 39472082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 39482082Seschrock char errbuf[1024]; 3949789Sahrens 3950789Sahrens /* if we have the same exact name, just return success */ 3951789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3952789Sahrens return (0); 3953789Sahrens 39542082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 39552082Seschrock "cannot rename to '%s'"), target); 39562082Seschrock 3957789Sahrens /* 3958789Sahrens * Make sure the target name is valid 3959789Sahrens */ 3960789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 39612665Snd150628 if ((strchr(target, '@') == NULL) || 39622665Snd150628 *target == '@') { 39632665Snd150628 /* 39642665Snd150628 * Snapshot target name is abbreviated, 39652665Snd150628 * reconstruct full dataset name 39662665Snd150628 */ 39672665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 39682665Snd150628 sizeof (parent)); 39692665Snd150628 delim = strchr(parent, '@'); 39702665Snd150628 if (strchr(target, '@') == NULL) 39712665Snd150628 *(++delim) = '\0'; 39722665Snd150628 else 39732665Snd150628 *delim = '\0'; 39742665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 39752665Snd150628 target = parent; 39762665Snd150628 } else { 39772665Snd150628 /* 39782665Snd150628 * Make sure we're renaming within the same dataset. 39792665Snd150628 */ 39802665Snd150628 delim = strchr(target, '@'); 39812665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 39822665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 39832665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 39842665Snd150628 "snapshots must be part of same " 39852665Snd150628 "dataset")); 39862665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 39873912Slling errbuf)); 39882665Snd150628 } 3989789Sahrens } 39905326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 39912665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3992789Sahrens } else { 39934007Smmusante if (recursive) { 39944007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 39954007Smmusante "recursive rename must be a snapshot")); 39964007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 39974007Smmusante } 39984007Smmusante 39995326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 40002665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40012676Seschrock uint64_t unused; 40022676Seschrock 4003789Sahrens /* validate parents */ 40044490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4005789Sahrens return (-1); 4006789Sahrens 4007789Sahrens (void) parent_name(target, parent, sizeof (parent)); 4008789Sahrens 4009789Sahrens /* make sure we're in the same pool */ 4010789Sahrens verify((delim = strchr(target, '/')) != NULL); 4011789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4012789Sahrens zhp->zfs_name[delim - target] != '/') { 40132082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40142082Seschrock "datasets must be within same pool")); 40152082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4016789Sahrens } 40172440Snd150628 40182440Snd150628 /* new name cannot be a child of the current dataset name */ 40192440Snd150628 if (strncmp(parent, zhp->zfs_name, 40203912Slling strlen(zhp->zfs_name)) == 0) { 40212440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40222440Snd150628 "New dataset name cannot be a descendent of " 40232440Snd150628 "current dataset name")); 40242440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40252440Snd150628 } 4026789Sahrens } 4027789Sahrens 40282082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 40292082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 40302082Seschrock 4031789Sahrens if (getzoneid() == GLOBAL_ZONEID && 4032789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 40332082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40342082Seschrock "dataset is used in a non-global zone")); 40352082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4036789Sahrens } 4037789Sahrens 40384007Smmusante if (recursive) { 40394007Smmusante struct destroydata dd; 40404007Smmusante 40414183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 40424183Smmusante if (parentname == NULL) { 40434183Smmusante ret = -1; 40444183Smmusante goto error; 40454183Smmusante } 40464007Smmusante delim = strchr(parentname, '@'); 40474007Smmusante *delim = '\0'; 40485094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 40494007Smmusante if (zhrp == NULL) { 40504183Smmusante ret = -1; 40514183Smmusante goto error; 40524007Smmusante } 40534007Smmusante 40544007Smmusante dd.snapname = delim + 1; 40554007Smmusante dd.gotone = B_FALSE; 40564183Smmusante dd.closezhp = B_TRUE; 40574007Smmusante 40584007Smmusante /* We remove any zvol links prior to renaming them */ 40594007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 40604007Smmusante if (ret) { 40614007Smmusante goto error; 40624007Smmusante } 40634007Smmusante } else { 40647366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL) 40654007Smmusante return (-1); 40664007Smmusante 40674007Smmusante if (changelist_haszonedchild(cl)) { 40684007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40694007Smmusante "child dataset with inherited mountpoint is used " 40704007Smmusante "in a non-global zone")); 40714007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 40724007Smmusante goto error; 40734007Smmusante } 40744007Smmusante 40754007Smmusante if ((ret = changelist_prefix(cl)) != 0) 40764007Smmusante goto error; 4077789Sahrens } 4078789Sahrens 40792676Seschrock if (ZFS_IS_VOLUME(zhp)) 4080789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4081789Sahrens else 4082789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4083789Sahrens 40842665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 40852676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 40862665Snd150628 40874007Smmusante zc.zc_cookie = recursive; 40884007Smmusante 40894543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 40904007Smmusante /* 40914007Smmusante * if it was recursive, the one that actually failed will 40924007Smmusante * be in zc.zc_name 40934007Smmusante */ 40944007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 40955367Sahrens "cannot rename '%s'"), zc.zc_name); 40964007Smmusante 40974007Smmusante if (recursive && errno == EEXIST) { 40984007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40994007Smmusante "a child dataset already has a snapshot " 41004007Smmusante "with the new name")); 41014801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 41024007Smmusante } else { 41034007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 41044007Smmusante } 4105789Sahrens 4106789Sahrens /* 4107789Sahrens * On failure, we still want to remount any filesystems that 4108789Sahrens * were previously mounted, so we don't alter the system state. 4109789Sahrens */ 41104007Smmusante if (recursive) { 41114007Smmusante struct createdata cd; 41124007Smmusante 41134007Smmusante /* only create links for datasets that had existed */ 41144007Smmusante cd.cd_snapname = delim + 1; 41154007Smmusante cd.cd_ifexists = B_TRUE; 41164007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41174007Smmusante &cd); 41184007Smmusante } else { 41194007Smmusante (void) changelist_postfix(cl); 41204007Smmusante } 4121789Sahrens } else { 41224007Smmusante if (recursive) { 41234007Smmusante struct createdata cd; 41244007Smmusante 41254007Smmusante /* only create links for datasets that had existed */ 41264007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 41274007Smmusante cd.cd_ifexists = B_TRUE; 41284007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41294007Smmusante &cd); 41304007Smmusante } else { 41314007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 41324007Smmusante ret = changelist_postfix(cl); 41334007Smmusante } 4134789Sahrens } 4135789Sahrens 4136789Sahrens error: 41374007Smmusante if (parentname) { 41384007Smmusante free(parentname); 41394007Smmusante } 41404007Smmusante if (zhrp) { 41414007Smmusante zfs_close(zhrp); 41424007Smmusante } 41434007Smmusante if (cl) { 41444007Smmusante changelist_free(cl); 41454007Smmusante } 4146789Sahrens return (ret); 4147789Sahrens } 4148789Sahrens 4149789Sahrens /* 4150789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4151789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 4152789Sahrens */ 4153789Sahrens int 41542082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4155789Sahrens { 41564007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 41574007Smmusante } 41584007Smmusante 41594007Smmusante static int 41604007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 41614007Smmusante { 4162789Sahrens zfs_cmd_t zc = { 0 }; 41632082Seschrock di_devlink_handle_t dhdl; 41644543Smarks priv_set_t *priv_effective; 41654543Smarks int privileged; 4166789Sahrens 4167789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4168789Sahrens 4169789Sahrens /* 4170789Sahrens * Issue the appropriate ioctl. 4171789Sahrens */ 41722082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4173789Sahrens switch (errno) { 4174789Sahrens case EEXIST: 4175789Sahrens /* 4176789Sahrens * Silently ignore the case where the link already 4177789Sahrens * exists. This allows 'zfs volinit' to be run multiple 4178789Sahrens * times without errors. 4179789Sahrens */ 4180789Sahrens return (0); 4181789Sahrens 41824007Smmusante case ENOENT: 41834007Smmusante /* 41844007Smmusante * Dataset does not exist in the kernel. If we 41854007Smmusante * don't care (see zfs_rename), then ignore the 41864007Smmusante * error quietly. 41874007Smmusante */ 41884007Smmusante if (ifexists) { 41894007Smmusante return (0); 41904007Smmusante } 41914007Smmusante 41924007Smmusante /* FALLTHROUGH */ 41934007Smmusante 4194789Sahrens default: 41953237Slling return (zfs_standard_error_fmt(hdl, errno, 41962082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 41972082Seschrock "for '%s'"), dataset)); 4198789Sahrens } 4199789Sahrens } 4200789Sahrens 4201789Sahrens /* 42024543Smarks * If privileged call devfsadm and wait for the links to 42034543Smarks * magically appear. 42044543Smarks * Otherwise, print out an informational message. 4205789Sahrens */ 42064543Smarks 42074543Smarks priv_effective = priv_allocset(); 42084543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 42094543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 42104543Smarks priv_freeset(priv_effective); 42114543Smarks 42124543Smarks if (privileged) { 42134543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 42144543Smarks DI_MAKE_LINK)) == NULL) { 42154543Smarks zfs_error_aux(hdl, strerror(errno)); 42167301SEric.Taylor@Sun.COM (void) zfs_error_fmt(hdl, errno, 42174543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 42184543Smarks "for '%s'"), dataset); 42194543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 42204543Smarks return (-1); 42214543Smarks } else { 42224543Smarks (void) di_devlink_fini(&dhdl); 42234543Smarks } 4224789Sahrens } else { 42254543Smarks char pathname[MAXPATHLEN]; 42264543Smarks struct stat64 statbuf; 42274543Smarks int i; 42284543Smarks 42294543Smarks #define MAX_WAIT 10 42304543Smarks 42314543Smarks /* 42324543Smarks * This is the poor mans way of waiting for the link 42334543Smarks * to show up. If after 10 seconds we still don't 42344543Smarks * have it, then print out a message. 42354543Smarks */ 42364543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 42374543Smarks dataset); 42384543Smarks 42394543Smarks for (i = 0; i != MAX_WAIT; i++) { 42404543Smarks if (stat64(pathname, &statbuf) == 0) 42414543Smarks break; 42424543Smarks (void) sleep(1); 42434543Smarks } 42444543Smarks if (i == MAX_WAIT) 42454543Smarks (void) printf(gettext("%s may not be immediately " 42464543Smarks "available\n"), pathname); 4247789Sahrens } 4248789Sahrens 4249789Sahrens return (0); 4250789Sahrens } 4251789Sahrens 4252789Sahrens /* 4253789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4254789Sahrens */ 4255789Sahrens int 42562082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4257789Sahrens { 4258789Sahrens zfs_cmd_t zc = { 0 }; 4259789Sahrens 4260789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4261789Sahrens 42622082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4263789Sahrens switch (errno) { 4264789Sahrens case ENXIO: 4265789Sahrens /* 4266789Sahrens * Silently ignore the case where the link no longer 4267789Sahrens * exists, so that 'zfs volfini' can be run multiple 4268789Sahrens * times without errors. 4269789Sahrens */ 4270789Sahrens return (0); 4271789Sahrens 4272789Sahrens default: 42733237Slling return (zfs_standard_error_fmt(hdl, errno, 42742082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 42752082Seschrock "links for '%s'"), dataset)); 4276789Sahrens } 4277789Sahrens } 4278789Sahrens 4279789Sahrens return (0); 4280789Sahrens } 42812676Seschrock 42822676Seschrock nvlist_t * 42832676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 42842676Seschrock { 42852676Seschrock return (zhp->zfs_user_props); 42862676Seschrock } 42872676Seschrock 42882676Seschrock /* 42893912Slling * This function is used by 'zfs list' to determine the exact set of columns to 42903912Slling * display, and their maximum widths. This does two main things: 42913912Slling * 42923912Slling * - If this is a list of all properties, then expand the list to include 42933912Slling * all native properties, and set a flag so that for each dataset we look 42943912Slling * for new unique user properties and add them to the list. 42953912Slling * 42963912Slling * - For non fixed-width properties, keep track of the maximum width seen 42973912Slling * so that we can size the column appropriately. 42983912Slling */ 42993912Slling int 43005094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 43013912Slling { 43023912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 43035094Slling zprop_list_t *entry; 43045094Slling zprop_list_t **last, **start; 43053912Slling nvlist_t *userprops, *propval; 43063912Slling nvpair_t *elem; 43073912Slling char *strval; 43083912Slling char buf[ZFS_MAXPROPLEN]; 43093912Slling 43105094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 43113912Slling return (-1); 43122676Seschrock 43132676Seschrock userprops = zfs_get_user_props(zhp); 43142676Seschrock 43152676Seschrock entry = *plp; 43162676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 43172676Seschrock /* 43182676Seschrock * Go through and add any user properties as necessary. We 43192676Seschrock * start by incrementing our list pointer to the first 43202676Seschrock * non-native property. 43212676Seschrock */ 43222676Seschrock start = plp; 43232676Seschrock while (*start != NULL) { 43245094Slling if ((*start)->pl_prop == ZPROP_INVAL) 43252676Seschrock break; 43262676Seschrock start = &(*start)->pl_next; 43272676Seschrock } 43282676Seschrock 43292676Seschrock elem = NULL; 43302676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 43312676Seschrock /* 43322676Seschrock * See if we've already found this property in our list. 43332676Seschrock */ 43342676Seschrock for (last = start; *last != NULL; 43352676Seschrock last = &(*last)->pl_next) { 43362676Seschrock if (strcmp((*last)->pl_user_prop, 43372676Seschrock nvpair_name(elem)) == 0) 43382676Seschrock break; 43392676Seschrock } 43402676Seschrock 43412676Seschrock if (*last == NULL) { 43422676Seschrock if ((entry = zfs_alloc(hdl, 43435094Slling sizeof (zprop_list_t))) == NULL || 43442676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 43452676Seschrock nvpair_name(elem)))) == NULL) { 43462676Seschrock free(entry); 43472676Seschrock return (-1); 43482676Seschrock } 43492676Seschrock 43505094Slling entry->pl_prop = ZPROP_INVAL; 43512676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 43522676Seschrock entry->pl_all = B_TRUE; 43532676Seschrock *last = entry; 43542676Seschrock } 43552676Seschrock } 43562676Seschrock } 43572676Seschrock 43582676Seschrock /* 43592676Seschrock * Now go through and check the width of any non-fixed columns 43602676Seschrock */ 43612676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 43622676Seschrock if (entry->pl_fixed) 43632676Seschrock continue; 43642676Seschrock 43655094Slling if (entry->pl_prop != ZPROP_INVAL) { 43662676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 43672676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 43682676Seschrock if (strlen(buf) > entry->pl_width) 43692676Seschrock entry->pl_width = strlen(buf); 43702676Seschrock } 43712676Seschrock } else if (nvlist_lookup_nvlist(userprops, 43722676Seschrock entry->pl_user_prop, &propval) == 0) { 43732676Seschrock verify(nvlist_lookup_string(propval, 43745094Slling ZPROP_VALUE, &strval) == 0); 43752676Seschrock if (strlen(strval) > entry->pl_width) 43762676Seschrock entry->pl_width = strlen(strval); 43772676Seschrock } 43782676Seschrock } 43792676Seschrock 43802676Seschrock return (0); 43812676Seschrock } 43824543Smarks 43834543Smarks int 43844543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 43854543Smarks { 43864543Smarks zfs_cmd_t zc = { 0 }; 43874543Smarks nvlist_t *nvp; 43884543Smarks gid_t gid; 43894543Smarks uid_t uid; 43904543Smarks const gid_t *groups; 43914543Smarks int group_cnt; 43924543Smarks int error; 43934543Smarks 43944543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 43954543Smarks return (no_memory(hdl)); 43964543Smarks 43974543Smarks uid = ucred_geteuid(cred); 43984543Smarks gid = ucred_getegid(cred); 43994543Smarks group_cnt = ucred_getgroups(cred, &groups); 44004543Smarks 44014543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 44024543Smarks return (1); 44034543Smarks 44044543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 44054543Smarks nvlist_free(nvp); 44064543Smarks return (1); 44074543Smarks } 44084543Smarks 44094543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 44104543Smarks nvlist_free(nvp); 44114543Smarks return (1); 44124543Smarks } 44134543Smarks 44144543Smarks if (nvlist_add_uint32_array(nvp, 44154543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 44164543Smarks nvlist_free(nvp); 44174543Smarks return (1); 44184543Smarks } 44194543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 44204543Smarks 44215094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 44224543Smarks return (-1); 44234543Smarks 44244543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 44254543Smarks nvlist_free(nvp); 44264543Smarks return (error); 44274543Smarks } 44284543Smarks 44294543Smarks int 44304543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 44315331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 44324543Smarks { 44334543Smarks zfs_cmd_t zc = { 0 }; 44344543Smarks int error; 44354543Smarks 44364543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 44374543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 44384543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 44394543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 44405331Samw zc.zc_share.z_sharetype = operation; 44414543Smarks zc.zc_share.z_sharemax = sharemax; 44424543Smarks 44434543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 44444543Smarks return (error); 44454543Smarks } 4446*8802SSanjeev.Bagewadi@Sun.COM 4447*8802SSanjeev.Bagewadi@Sun.COM void 4448*8802SSanjeev.Bagewadi@Sun.COM zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 4449*8802SSanjeev.Bagewadi@Sun.COM { 4450*8802SSanjeev.Bagewadi@Sun.COM nvpair_t *curr; 4451*8802SSanjeev.Bagewadi@Sun.COM 4452*8802SSanjeev.Bagewadi@Sun.COM /* 4453*8802SSanjeev.Bagewadi@Sun.COM * Keep a reference to the props-table against which we prune the 4454*8802SSanjeev.Bagewadi@Sun.COM * properties. 4455*8802SSanjeev.Bagewadi@Sun.COM */ 4456*8802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table = props; 4457*8802SSanjeev.Bagewadi@Sun.COM 4458*8802SSanjeev.Bagewadi@Sun.COM curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 4459*8802SSanjeev.Bagewadi@Sun.COM 4460*8802SSanjeev.Bagewadi@Sun.COM while (curr) { 4461*8802SSanjeev.Bagewadi@Sun.COM zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 4462*8802SSanjeev.Bagewadi@Sun.COM nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 4463*8802SSanjeev.Bagewadi@Sun.COM 4464*8802SSanjeev.Bagewadi@Sun.COM if (props[zfs_prop] == B_FALSE) 4465*8802SSanjeev.Bagewadi@Sun.COM (void) nvlist_remove(zhp->zfs_props, 4466*8802SSanjeev.Bagewadi@Sun.COM nvpair_name(curr), nvpair_type(curr)); 4467*8802SSanjeev.Bagewadi@Sun.COM curr = next; 4468*8802SSanjeev.Bagewadi@Sun.COM } 4469*8802SSanjeev.Bagewadi@Sun.COM } 4470