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 /* 238802SSanjeev.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 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 6098228SEric.Taylor@Sun.COM avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 6108228SEric.Taylor@Sun.COM sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 6118811SEric.Taylor@Sun.COM } 6128811SEric.Taylor@Sun.COM 6138811SEric.Taylor@Sun.COM void 6148811SEric.Taylor@Sun.COM libzfs_mnttab_update(libzfs_handle_t *hdl) 6158811SEric.Taylor@Sun.COM { 6168811SEric.Taylor@Sun.COM struct mnttab entry; 6178228SEric.Taylor@Sun.COM 6188228SEric.Taylor@Sun.COM rewind(hdl->libzfs_mnttab); 6198228SEric.Taylor@Sun.COM while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 6208228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6218228SEric.Taylor@Sun.COM 6228228SEric.Taylor@Sun.COM if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 6238228SEric.Taylor@Sun.COM continue; 6248228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 6258228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 6268228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 6278228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 6288228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 6298228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 6308228SEric.Taylor@Sun.COM } 6318228SEric.Taylor@Sun.COM } 6328228SEric.Taylor@Sun.COM 6338228SEric.Taylor@Sun.COM void 6348228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl) 6358228SEric.Taylor@Sun.COM { 6368228SEric.Taylor@Sun.COM void *cookie = NULL; 6378228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6388228SEric.Taylor@Sun.COM 6398228SEric.Taylor@Sun.COM while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) { 6408228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_special); 6418228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mountp); 6428228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_fstype); 6438228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mntopts); 6448228SEric.Taylor@Sun.COM free(mtn); 6458228SEric.Taylor@Sun.COM } 6468228SEric.Taylor@Sun.COM avl_destroy(&hdl->libzfs_mnttab_cache); 6478228SEric.Taylor@Sun.COM } 6488228SEric.Taylor@Sun.COM 6498811SEric.Taylor@Sun.COM void 6508811SEric.Taylor@Sun.COM libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 6518811SEric.Taylor@Sun.COM { 6528811SEric.Taylor@Sun.COM hdl->libzfs_mnttab_enable = enable; 6538811SEric.Taylor@Sun.COM } 6548811SEric.Taylor@Sun.COM 6558228SEric.Taylor@Sun.COM int 6568228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 6578228SEric.Taylor@Sun.COM struct mnttab *entry) 6588228SEric.Taylor@Sun.COM { 6598228SEric.Taylor@Sun.COM mnttab_node_t find; 6608228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6618228SEric.Taylor@Sun.COM 6628811SEric.Taylor@Sun.COM if (!hdl->libzfs_mnttab_enable) { 6638811SEric.Taylor@Sun.COM struct mnttab srch = { 0 }; 6648811SEric.Taylor@Sun.COM 6658811SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 6668811SEric.Taylor@Sun.COM libzfs_mnttab_fini(hdl); 6678811SEric.Taylor@Sun.COM rewind(hdl->libzfs_mnttab); 6688811SEric.Taylor@Sun.COM srch.mnt_special = (char *)fsname; 6698811SEric.Taylor@Sun.COM srch.mnt_fstype = MNTTYPE_ZFS; 6708811SEric.Taylor@Sun.COM if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 6718811SEric.Taylor@Sun.COM return (0); 6728811SEric.Taylor@Sun.COM else 6738811SEric.Taylor@Sun.COM return (ENOENT); 6748811SEric.Taylor@Sun.COM } 6758811SEric.Taylor@Sun.COM 6768228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 6778811SEric.Taylor@Sun.COM libzfs_mnttab_update(hdl); 6788228SEric.Taylor@Sun.COM 6798228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 6808228SEric.Taylor@Sun.COM mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 6818228SEric.Taylor@Sun.COM if (mtn) { 6828228SEric.Taylor@Sun.COM *entry = mtn->mtn_mt; 6838228SEric.Taylor@Sun.COM return (0); 6848228SEric.Taylor@Sun.COM } 6858228SEric.Taylor@Sun.COM return (ENOENT); 6868228SEric.Taylor@Sun.COM } 6878228SEric.Taylor@Sun.COM 6888228SEric.Taylor@Sun.COM void 6898228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 6908228SEric.Taylor@Sun.COM const char *mountp, const char *mntopts) 6918228SEric.Taylor@Sun.COM { 6928228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6938228SEric.Taylor@Sun.COM 6948228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 6958228SEric.Taylor@Sun.COM return; 6968228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 6978228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 6988228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 6998228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 7008228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 7018228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 7028228SEric.Taylor@Sun.COM } 7038228SEric.Taylor@Sun.COM 7048228SEric.Taylor@Sun.COM void 7058228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 7068228SEric.Taylor@Sun.COM { 7078228SEric.Taylor@Sun.COM mnttab_node_t find; 7088228SEric.Taylor@Sun.COM mnttab_node_t *ret; 7098228SEric.Taylor@Sun.COM 7108228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 7118228SEric.Taylor@Sun.COM if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) { 7128228SEric.Taylor@Sun.COM avl_remove(&hdl->libzfs_mnttab_cache, ret); 7138228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_special); 7148228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mountp); 7158228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_fstype); 7168228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mntopts); 7178228SEric.Taylor@Sun.COM free(ret); 7188228SEric.Taylor@Sun.COM } 7198228SEric.Taylor@Sun.COM } 7208228SEric.Taylor@Sun.COM 7215713Srm160521 int 7225713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 7235713Srm160521 { 7246865Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 7256865Srm160521 7265713Srm160521 if (zpool_handle == NULL) 7275713Srm160521 return (-1); 7285713Srm160521 7295713Srm160521 *spa_version = zpool_get_prop_int(zpool_handle, 7305713Srm160521 ZPOOL_PROP_VERSION, NULL); 7315713Srm160521 return (0); 7325713Srm160521 } 7335713Srm160521 7345713Srm160521 /* 7355713Srm160521 * The choice of reservation property depends on the SPA version. 7365713Srm160521 */ 7375713Srm160521 static int 7385713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 7395713Srm160521 { 7405713Srm160521 int spa_version; 7415713Srm160521 7425713Srm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 7435713Srm160521 return (-1); 7445713Srm160521 7455713Srm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 7465713Srm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 7475713Srm160521 else 7485713Srm160521 *resv_prop = ZFS_PROP_RESERVATION; 7495713Srm160521 7505713Srm160521 return (0); 7515713Srm160521 } 7525713Srm160521 7533912Slling /* 7542676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 7552676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 7562676Seschrock * strings. 757789Sahrens */ 7587184Stimh nvlist_t * 7597184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 7605094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 761789Sahrens { 7622676Seschrock nvpair_t *elem; 7632676Seschrock uint64_t intval; 7642676Seschrock char *strval; 7655094Slling zfs_prop_t prop; 7662676Seschrock nvlist_t *ret; 7675331Samw int chosen_normal = -1; 7685331Samw int chosen_utf = -1; 7692676Seschrock 7705094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 7715094Slling (void) no_memory(hdl); 7725094Slling return (NULL); 773789Sahrens } 774789Sahrens 7752676Seschrock elem = NULL; 7762676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 7775094Slling const char *propname = nvpair_name(elem); 7782676Seschrock 7792676Seschrock /* 7802676Seschrock * Make sure this property is valid and applies to this type. 7812676Seschrock */ 7825094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 7835094Slling if (!zfs_prop_user(propname)) { 7842676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7855094Slling "invalid property '%s'"), propname); 7865094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7875094Slling goto error; 7885094Slling } 7895094Slling 7905094Slling /* 7915094Slling * If this is a user property, make sure it's a 7925094Slling * string, and that it's less than ZAP_MAXNAMELEN. 7935094Slling */ 7945094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 7955094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7965094Slling "'%s' must be a string"), propname); 7975094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7985094Slling goto error; 7995094Slling } 8005094Slling 8015094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 8025094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8035094Slling "property name '%s' is too long"), 8042676Seschrock propname); 8052676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8062676Seschrock goto error; 8072676Seschrock } 8082676Seschrock 8092676Seschrock (void) nvpair_value_string(elem, &strval); 8102676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 8112676Seschrock (void) no_memory(hdl); 8122676Seschrock goto error; 8132676Seschrock } 8142676Seschrock continue; 815789Sahrens } 8162676Seschrock 8177265Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 8187265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8197265Sahrens "this property can not be modified for snapshots")); 8207265Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 8217265Sahrens goto error; 8227265Sahrens } 8237265Sahrens 8242676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 8252676Seschrock zfs_error_aux(hdl, 8262676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 8272676Seschrock "apply to datasets of this type"), propname); 8282676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 8292676Seschrock goto error; 8302676Seschrock } 8312676Seschrock 8322676Seschrock if (zfs_prop_readonly(prop) && 8335331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 8342676Seschrock zfs_error_aux(hdl, 8352676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 8362676Seschrock propname); 8372676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 8382676Seschrock goto error; 8392676Seschrock } 8402676Seschrock 8415094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 8425094Slling &strval, &intval, errbuf) != 0) 8432676Seschrock goto error; 8442676Seschrock 8452676Seschrock /* 8462676Seschrock * Perform some additional checks for specific properties. 8472676Seschrock */ 8482676Seschrock switch (prop) { 8494577Sahrens case ZFS_PROP_VERSION: 8504577Sahrens { 8514577Sahrens int version; 8524577Sahrens 8534577Sahrens if (zhp == NULL) 8544577Sahrens break; 8554577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 8564577Sahrens if (intval < version) { 8574577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8584577Sahrens "Can not downgrade; already at version %u"), 8594577Sahrens version); 8604577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8614577Sahrens goto error; 8624577Sahrens } 8634577Sahrens break; 8644577Sahrens } 8654577Sahrens 8662676Seschrock case ZFS_PROP_RECORDSIZE: 8672676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 8682676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 8692676Seschrock if (intval < SPA_MINBLOCKSIZE || 8702676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 8712082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8722676Seschrock "'%s' must be power of 2 from %u " 8732676Seschrock "to %uk"), propname, 8742676Seschrock (uint_t)SPA_MINBLOCKSIZE, 8752676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 8762676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8772676Seschrock goto error; 878789Sahrens } 879789Sahrens break; 880789Sahrens 8813126Sahl case ZFS_PROP_SHAREISCSI: 8823126Sahl if (strcmp(strval, "off") != 0 && 8833126Sahl strcmp(strval, "on") != 0 && 8843126Sahl strcmp(strval, "type=disk") != 0) { 8853126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8863126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 8873126Sahl propname); 8883126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8893126Sahl goto error; 8903126Sahl } 8913126Sahl 8923126Sahl break; 8933126Sahl 8942676Seschrock case ZFS_PROP_MOUNTPOINT: 8954778Srm160521 { 8964778Srm160521 namecheck_err_t why; 8974778Srm160521 8982676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 8992676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 9002676Seschrock break; 9012676Seschrock 9024778Srm160521 if (mountpoint_namecheck(strval, &why)) { 9034778Srm160521 switch (why) { 9044778Srm160521 case NAME_ERR_LEADING_SLASH: 9054778Srm160521 zfs_error_aux(hdl, 9064778Srm160521 dgettext(TEXT_DOMAIN, 9074778Srm160521 "'%s' must be an absolute path, " 9084778Srm160521 "'none', or 'legacy'"), propname); 9094778Srm160521 break; 9104778Srm160521 case NAME_ERR_TOOLONG: 9114778Srm160521 zfs_error_aux(hdl, 9124778Srm160521 dgettext(TEXT_DOMAIN, 9134778Srm160521 "component of '%s' is too long"), 9144778Srm160521 propname); 9154778Srm160521 break; 9164778Srm160521 } 9172676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9182676Seschrock goto error; 919789Sahrens } 9204778Srm160521 } 9214778Srm160521 9223126Sahl /*FALLTHRU*/ 9233126Sahl 9245331Samw case ZFS_PROP_SHARESMB: 9253126Sahl case ZFS_PROP_SHARENFS: 9263126Sahl /* 9275331Samw * For the mountpoint and sharenfs or sharesmb 9285331Samw * properties, check if it can be set in a 9295331Samw * global/non-global zone based on 9303126Sahl * the zoned property value: 9313126Sahl * 9323126Sahl * global zone non-global zone 9333126Sahl * -------------------------------------------------- 9343126Sahl * zoned=on mountpoint (no) mountpoint (yes) 9353126Sahl * sharenfs (no) sharenfs (no) 9365331Samw * sharesmb (no) sharesmb (no) 9373126Sahl * 9383126Sahl * zoned=off mountpoint (yes) N/A 9393126Sahl * sharenfs (yes) 9405331Samw * sharesmb (yes) 9413126Sahl */ 9422676Seschrock if (zoned) { 9432676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 9442676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9452676Seschrock "'%s' cannot be set on " 9462676Seschrock "dataset in a non-global zone"), 9472676Seschrock propname); 9482676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9492676Seschrock errbuf); 9502676Seschrock goto error; 9515331Samw } else if (prop == ZFS_PROP_SHARENFS || 9525331Samw prop == ZFS_PROP_SHARESMB) { 9532676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9542676Seschrock "'%s' cannot be set in " 9552676Seschrock "a non-global zone"), propname); 9562676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9572676Seschrock errbuf); 9582676Seschrock goto error; 9592676Seschrock } 9602676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 9612676Seschrock /* 9622676Seschrock * If zoned property is 'off', this must be in 9632676Seschrock * a globle zone. If not, something is wrong. 9642676Seschrock */ 9652676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9662676Seschrock "'%s' cannot be set while dataset " 9672676Seschrock "'zoned' property is set"), propname); 9682676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 9692676Seschrock goto error; 9702676Seschrock } 9713126Sahl 9724180Sdougm /* 9734180Sdougm * At this point, it is legitimate to set the 9744180Sdougm * property. Now we want to make sure that the 9754180Sdougm * property value is valid if it is sharenfs. 9764180Sdougm */ 9775331Samw if ((prop == ZFS_PROP_SHARENFS || 9785331Samw prop == ZFS_PROP_SHARESMB) && 9794217Seschrock strcmp(strval, "on") != 0 && 9804217Seschrock strcmp(strval, "off") != 0) { 9815331Samw zfs_share_proto_t proto; 9825331Samw 9835331Samw if (prop == ZFS_PROP_SHARESMB) 9845331Samw proto = PROTO_SMB; 9855331Samw else 9865331Samw proto = PROTO_NFS; 9874180Sdougm 9884180Sdougm /* 9895331Samw * Must be an valid sharing protocol 9905331Samw * option string so init the libshare 9915331Samw * in order to enable the parser and 9925331Samw * then parse the options. We use the 9935331Samw * control API since we don't care about 9945331Samw * the current configuration and don't 9954180Sdougm * want the overhead of loading it 9964180Sdougm * until we actually do something. 9974180Sdougm */ 9984180Sdougm 9994217Seschrock if (zfs_init_libshare(hdl, 10004217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 10014217Seschrock /* 10024217Seschrock * An error occurred so we can't do 10034217Seschrock * anything 10044217Seschrock */ 10054217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10064217Seschrock "'%s' cannot be set: problem " 10074217Seschrock "in share initialization"), 10084217Seschrock propname); 10094217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10104217Seschrock errbuf); 10114217Seschrock goto error; 10124217Seschrock } 10134217Seschrock 10145331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 10154217Seschrock /* 10164217Seschrock * There was an error in parsing so 10174217Seschrock * deal with it by issuing an error 10184217Seschrock * message and leaving after 10194217Seschrock * uninitializing the the libshare 10204217Seschrock * interface. 10214217Seschrock */ 10224217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10234217Seschrock "'%s' cannot be set to invalid " 10244217Seschrock "options"), propname); 10254217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10264217Seschrock errbuf); 10274217Seschrock zfs_uninit_libshare(hdl); 10284217Seschrock goto error; 10294217Seschrock } 10304180Sdougm zfs_uninit_libshare(hdl); 10314180Sdougm } 10324180Sdougm 10333126Sahl break; 10345331Samw case ZFS_PROP_UTF8ONLY: 10355331Samw chosen_utf = (int)intval; 10365331Samw break; 10375331Samw case ZFS_PROP_NORMALIZE: 10385331Samw chosen_normal = (int)intval; 10395331Samw break; 10402676Seschrock } 10412676Seschrock 10422676Seschrock /* 10432676Seschrock * For changes to existing volumes, we have some additional 10442676Seschrock * checks to enforce. 10452676Seschrock */ 10462676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 10472676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 10482676Seschrock ZFS_PROP_VOLSIZE); 10492676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 10502676Seschrock ZFS_PROP_VOLBLOCKSIZE); 10512676Seschrock char buf[64]; 10522676Seschrock 10532676Seschrock switch (prop) { 10542676Seschrock case ZFS_PROP_RESERVATION: 10555378Sck153898 case ZFS_PROP_REFRESERVATION: 10562676Seschrock if (intval > volsize) { 10572676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10582676Seschrock "'%s' is greater than current " 10592676Seschrock "volume size"), propname); 10602676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10612676Seschrock errbuf); 10622676Seschrock goto error; 10632676Seschrock } 10642676Seschrock break; 10652676Seschrock 10662676Seschrock case ZFS_PROP_VOLSIZE: 10672676Seschrock if (intval % blocksize != 0) { 10682676Seschrock zfs_nicenum(blocksize, buf, 10692676Seschrock sizeof (buf)); 10702676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10712676Seschrock "'%s' must be a multiple of " 10722676Seschrock "volume block size (%s)"), 10732676Seschrock propname, buf); 10742676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10752676Seschrock errbuf); 10762676Seschrock goto error; 10772676Seschrock } 10782676Seschrock 10792676Seschrock if (intval == 0) { 10802676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10812676Seschrock "'%s' cannot be zero"), 10822676Seschrock propname); 10832676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10842676Seschrock errbuf); 10852676Seschrock goto error; 1086789Sahrens } 10873126Sahl break; 1088789Sahrens } 1089789Sahrens } 1090789Sahrens } 1091789Sahrens 10922676Seschrock /* 10935331Samw * If normalization was chosen, but no UTF8 choice was made, 10945331Samw * enforce rejection of non-UTF8 names. 10955331Samw * 10965331Samw * If normalization was chosen, but rejecting non-UTF8 names 10975331Samw * was explicitly not chosen, it is an error. 10985331Samw */ 10995498Stimh if (chosen_normal > 0 && chosen_utf < 0) { 11005331Samw if (nvlist_add_uint64(ret, 11015331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 11025331Samw (void) no_memory(hdl); 11035331Samw goto error; 11045331Samw } 11055498Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 11065331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11075331Samw "'%s' must be set 'on' if normalization chosen"), 11085331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 11095331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 11105331Samw goto error; 11115331Samw } 11125331Samw 11135331Samw /* 11142676Seschrock * If this is an existing volume, and someone is setting the volsize, 11152676Seschrock * make sure that it matches the reservation, or add it if necessary. 11162676Seschrock */ 11172676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 11182676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 11192676Seschrock &intval) == 0) { 11202676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 11212676Seschrock ZFS_PROP_VOLSIZE); 11225481Sck153898 uint64_t old_reservation; 11232676Seschrock uint64_t new_reservation; 11245481Sck153898 zfs_prop_t resv_prop; 11255713Srm160521 11265713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 11275481Sck153898 goto error; 11285481Sck153898 old_reservation = zfs_prop_get_int(zhp, resv_prop); 11292676Seschrock 11302676Seschrock if (old_volsize == old_reservation && 11315481Sck153898 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop), 11322676Seschrock &new_reservation) != 0) { 11332676Seschrock if (nvlist_add_uint64(ret, 11345481Sck153898 zfs_prop_to_name(resv_prop), intval) != 0) { 11352676Seschrock (void) no_memory(hdl); 11362676Seschrock goto error; 11372676Seschrock } 11382676Seschrock } 11392676Seschrock } 11402676Seschrock return (ret); 11412676Seschrock 11422676Seschrock error: 11432676Seschrock nvlist_free(ret); 11442676Seschrock return (NULL); 1145789Sahrens } 1146789Sahrens 11474543Smarks static int 11484543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 11494543Smarks uint64_t *ret_who) 11504543Smarks { 11514543Smarks struct passwd *pwd; 11524543Smarks struct group *grp; 11534543Smarks uid_t id; 11544543Smarks 11554543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 11564543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 11574543Smarks *ret_who = -1; 11584543Smarks return (0); 11594543Smarks } 11604543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 11614543Smarks return (EZFS_BADWHO); 11624543Smarks 11634543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 11644543Smarks strcmp(who, "everyone") == 0) { 11654543Smarks *ret_who = -1; 11664543Smarks *who_type = ZFS_DELEG_EVERYONE; 11674543Smarks return (0); 11684543Smarks } 11694543Smarks 11704543Smarks pwd = getpwnam(who); 11714543Smarks grp = getgrnam(who); 11724543Smarks 11734543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 11744543Smarks *ret_who = pwd->pw_uid; 11754543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 11764543Smarks *ret_who = grp->gr_gid; 11774543Smarks } else if (pwd) { 11784543Smarks *ret_who = pwd->pw_uid; 11794543Smarks *who_type = ZFS_DELEG_USER; 11804543Smarks } else if (grp) { 11814543Smarks *ret_who = grp->gr_gid; 11824543Smarks *who_type = ZFS_DELEG_GROUP; 11834543Smarks } else { 11844543Smarks char *end; 11854543Smarks 11864543Smarks id = strtol(who, &end, 10); 11874543Smarks if (errno != 0 || *end != '\0') { 11884543Smarks return (EZFS_BADWHO); 11894543Smarks } else { 11904543Smarks *ret_who = id; 11914543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 11924543Smarks *who_type = ZFS_DELEG_USER; 11934543Smarks } 11944543Smarks } 11954543Smarks 11964543Smarks return (0); 11974543Smarks } 11984543Smarks 11994543Smarks static void 12004543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 12014543Smarks { 12024543Smarks if (perms_nvp != NULL) { 12034543Smarks verify(nvlist_add_nvlist(who_nvp, 12044543Smarks name, perms_nvp) == 0); 12054543Smarks } else { 12064543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 12074543Smarks } 12084543Smarks } 12094543Smarks 12104543Smarks static void 12114543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 12124543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 12134543Smarks nvlist_t *sets_nvp) 12144543Smarks { 12154543Smarks boolean_t do_perms, do_sets; 12164543Smarks char name[ZFS_MAX_DELEG_NAME]; 12174543Smarks 12184543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 12194543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 12204543Smarks 12214543Smarks if (!do_perms && !do_sets) 12224543Smarks do_perms = do_sets = B_TRUE; 12234543Smarks 12244543Smarks if (do_perms) { 12254543Smarks zfs_deleg_whokey(name, who_type, inherit, 12264543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 12274543Smarks whostr : (void *)&whoid); 12284543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 12294543Smarks } 12304543Smarks if (do_sets) { 12314543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 12324543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 12334543Smarks whostr : (void *)&whoid); 12344543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 12354543Smarks } 12364543Smarks } 12374543Smarks 12384543Smarks static void 12394543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 12404543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 12414543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 12424543Smarks { 12434543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 12444543Smarks helper(who_type, whoid, whostr, 0, 12454543Smarks who_nvp, perms_nvp, sets_nvp); 12464543Smarks } else { 12474543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 12484543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 12494543Smarks who_nvp, perms_nvp, sets_nvp); 12504543Smarks } 12514543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 12524543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 12534543Smarks who_nvp, perms_nvp, sets_nvp); 12544543Smarks } 12554543Smarks } 12564543Smarks } 12574543Smarks 12584543Smarks /* 12594543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 12604543Smarks * 12614543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 12624543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 12634543Smarks * base attribute named stored in the dsl. 12644543Smarks * Arguments: 12654543Smarks * 12664543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 12674543Smarks * whostr may be null for everyone or create perms. 12684543Smarks * who_type: is the type of entry in whostr. Typically this will be 12694543Smarks * ZFS_DELEG_WHO_UNKNOWN. 12705331Samw * perms: common separated list of permissions. May be null if user 12714543Smarks * is requested to remove permissions by who. 12724543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 12734543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 12744543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 12754543Smarks * The output nvp will look something like this. 12764543Smarks * ul$1234 -> {create ; destroy } 12774543Smarks * Ul$1234 -> { @myset } 12784543Smarks * s-$@myset - { snapshot; checksum; compression } 12794543Smarks */ 12804543Smarks int 12814543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 12824543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 12834543Smarks { 12844543Smarks nvlist_t *who_nvp; 12854543Smarks nvlist_t *perms_nvp = NULL; 12864543Smarks nvlist_t *sets_nvp = NULL; 12874543Smarks char errbuf[1024]; 12884787Sahrens char *who_tok, *perm; 12894543Smarks int error; 12904543Smarks 12914543Smarks *nvp = NULL; 12924543Smarks 12934543Smarks if (perms) { 12944543Smarks if ((error = nvlist_alloc(&perms_nvp, 12954543Smarks NV_UNIQUE_NAME, 0)) != 0) { 12964543Smarks return (1); 12974543Smarks } 12984543Smarks if ((error = nvlist_alloc(&sets_nvp, 12994543Smarks NV_UNIQUE_NAME, 0)) != 0) { 13004543Smarks nvlist_free(perms_nvp); 13014543Smarks return (1); 13024543Smarks } 13034543Smarks } 13044543Smarks 13054543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 13064543Smarks if (perms_nvp) 13074543Smarks nvlist_free(perms_nvp); 13084543Smarks if (sets_nvp) 13094543Smarks nvlist_free(sets_nvp); 13104543Smarks return (1); 13114543Smarks } 13124543Smarks 13134543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 13144543Smarks namecheck_err_t why; 13154543Smarks char what; 13164543Smarks 13174543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 13184787Sahrens nvlist_free(who_nvp); 13194787Sahrens if (perms_nvp) 13204787Sahrens nvlist_free(perms_nvp); 13214787Sahrens if (sets_nvp) 13224787Sahrens nvlist_free(sets_nvp); 13234787Sahrens 13244543Smarks switch (why) { 13254543Smarks case NAME_ERR_NO_AT: 13264543Smarks zfs_error_aux(zhp->zfs_hdl, 13274543Smarks dgettext(TEXT_DOMAIN, 13284543Smarks "set definition must begin with an '@' " 13294543Smarks "character")); 13304543Smarks } 13314543Smarks return (zfs_error(zhp->zfs_hdl, 13324543Smarks EZFS_BADPERMSET, whostr)); 13334543Smarks } 13344543Smarks } 13354543Smarks 13364543Smarks /* 13374543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 13384543Smarks * The first nvlist perms_nvp will have normal permissions and the 13394543Smarks * other sets_nvp will have only permssion set names in it. 13404543Smarks */ 13414787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 13424787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 13434787Sahrens 13444787Sahrens if (perm_canonical) { 13454787Sahrens verify(nvlist_add_boolean(perms_nvp, 13464787Sahrens perm_canonical) == 0); 13474787Sahrens } else if (perm[0] == '@') { 13484787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 13494787Sahrens } else { 13504787Sahrens nvlist_free(who_nvp); 13514787Sahrens nvlist_free(perms_nvp); 13524787Sahrens nvlist_free(sets_nvp); 13534787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 13544543Smarks } 13554543Smarks } 13564543Smarks 13574543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 13584543Smarks who_tok = strtok(whostr, ","); 13594543Smarks if (who_tok == NULL) { 13604543Smarks nvlist_free(who_nvp); 13614787Sahrens if (perms_nvp) 13624787Sahrens nvlist_free(perms_nvp); 13634543Smarks if (sets_nvp) 13644543Smarks nvlist_free(sets_nvp); 13654543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13664543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 13674543Smarks whostr); 13684543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 13694543Smarks } 13704543Smarks } 13714543Smarks 13724543Smarks /* 13734543Smarks * Now create the nvlist(s) 13744543Smarks */ 13754543Smarks do { 13764543Smarks uint64_t who_id; 13774543Smarks 13784543Smarks error = zfs_get_perm_who(who_tok, &who_type, 13794543Smarks &who_id); 13804543Smarks if (error) { 13814543Smarks nvlist_free(who_nvp); 13824787Sahrens if (perms_nvp) 13834787Sahrens nvlist_free(perms_nvp); 13844543Smarks if (sets_nvp) 13854543Smarks nvlist_free(sets_nvp); 13864543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13874543Smarks dgettext(TEXT_DOMAIN, 13884543Smarks "Unable to determine uid/gid for " 13894543Smarks "%s "), who_tok); 13904543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 13914543Smarks } 13924543Smarks 13934543Smarks /* 13944543Smarks * add entries for both local and descendent when required 13954543Smarks */ 13964543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 13974543Smarks perms_nvp, sets_nvp, who_type, inherit); 13984543Smarks 13994543Smarks } while (who_tok = strtok(NULL, ",")); 14004543Smarks *nvp = who_nvp; 14014543Smarks return (0); 14024543Smarks } 14034543Smarks 14044543Smarks static int 14054543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 14064543Smarks { 14074543Smarks zfs_cmd_t zc = { 0 }; 14084543Smarks int error; 14094543Smarks char errbuf[1024]; 14104543Smarks 14114543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14124543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 14134543Smarks zhp->zfs_name); 14144543Smarks 14155094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 14164543Smarks return (-1); 14174543Smarks 14184543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 14194543Smarks zc.zc_perm_action = unset; 14204543Smarks 14214543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 14224543Smarks if (error && errno == ENOTSUP) { 14234543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14244543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 14254543Smarks zcmd_free_nvlists(&zc); 14264543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 14274543Smarks } else if (error) { 14284543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 14294543Smarks } 14304543Smarks zcmd_free_nvlists(&zc); 14314543Smarks 14324543Smarks return (error); 14334543Smarks } 14344543Smarks 14354543Smarks int 14364543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 14374543Smarks { 14384543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 14394543Smarks } 14404543Smarks 14414543Smarks int 14424543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 14434543Smarks { 14444543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 14454543Smarks } 14464543Smarks 14474543Smarks static int 14484543Smarks perm_compare(const void *arg1, const void *arg2) 14494543Smarks { 14504543Smarks const zfs_perm_node_t *node1 = arg1; 14514543Smarks const zfs_perm_node_t *node2 = arg2; 14524543Smarks int ret; 14534543Smarks 14544543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 14554543Smarks 14564543Smarks if (ret > 0) 14574543Smarks return (1); 14584543Smarks if (ret < 0) 14594543Smarks return (-1); 14604543Smarks else 14614543Smarks return (0); 14624543Smarks } 14634543Smarks 14644543Smarks static void 14654543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 14664543Smarks { 14674543Smarks zfs_perm_node_t *permnode; 14685367Sahrens void *cookie = NULL; 14695367Sahrens 14705367Sahrens while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 14714543Smarks free(permnode); 14725367Sahrens avl_destroy(tree); 14734543Smarks } 14744543Smarks 14754543Smarks static void 14764543Smarks zfs_destroy_tree(avl_tree_t *tree) 14774543Smarks { 14784543Smarks zfs_allow_node_t *allownode; 14795367Sahrens void *cookie = NULL; 14805367Sahrens 14814543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 14824543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 14834543Smarks zfs_destroy_perm_tree(&allownode->z_local); 14844543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 14854543Smarks free(allownode); 14864543Smarks } 14875367Sahrens avl_destroy(tree); 14884543Smarks } 14894543Smarks 14904543Smarks void 14914543Smarks zfs_free_allows(zfs_allow_t *allow) 14924543Smarks { 14934543Smarks zfs_allow_t *allownext; 14944543Smarks zfs_allow_t *freeallow; 14954543Smarks 14964543Smarks allownext = allow; 14974543Smarks while (allownext) { 14984543Smarks zfs_destroy_tree(&allownext->z_sets); 14994543Smarks zfs_destroy_tree(&allownext->z_crperms); 15004543Smarks zfs_destroy_tree(&allownext->z_user); 15014543Smarks zfs_destroy_tree(&allownext->z_group); 15024543Smarks zfs_destroy_tree(&allownext->z_everyone); 15034543Smarks freeallow = allownext; 15044543Smarks allownext = allownext->z_next; 15054543Smarks free(freeallow); 15064543Smarks } 15074543Smarks } 15084543Smarks 15094543Smarks static zfs_allow_t * 15104543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 15114543Smarks { 15124543Smarks zfs_allow_t *ptree; 15134543Smarks 15144543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 15154543Smarks sizeof (zfs_allow_t))) == NULL) { 15164543Smarks return (NULL); 15174543Smarks } 15184543Smarks 15194543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 15204543Smarks avl_create(&ptree->z_sets, 15214543Smarks perm_compare, sizeof (zfs_allow_node_t), 15224543Smarks offsetof(zfs_allow_node_t, z_node)); 15234543Smarks avl_create(&ptree->z_crperms, 15244543Smarks perm_compare, sizeof (zfs_allow_node_t), 15254543Smarks offsetof(zfs_allow_node_t, z_node)); 15264543Smarks avl_create(&ptree->z_user, 15274543Smarks perm_compare, sizeof (zfs_allow_node_t), 15284543Smarks offsetof(zfs_allow_node_t, z_node)); 15294543Smarks avl_create(&ptree->z_group, 15304543Smarks perm_compare, sizeof (zfs_allow_node_t), 15314543Smarks offsetof(zfs_allow_node_t, z_node)); 15324543Smarks avl_create(&ptree->z_everyone, 15334543Smarks perm_compare, sizeof (zfs_allow_node_t), 15344543Smarks offsetof(zfs_allow_node_t, z_node)); 15354543Smarks 15364543Smarks if (prev) 15374543Smarks prev->z_next = ptree; 15384543Smarks ptree->z_next = NULL; 15394543Smarks return (ptree); 15404543Smarks } 15414543Smarks 15424543Smarks /* 15434543Smarks * Add permissions to the appropriate AVL permission tree. 15444543Smarks * The appropriate tree may not be the requested tree. 15454543Smarks * For example if ld indicates a local permission, but 15464543Smarks * same permission also exists as a descendent permission 15474543Smarks * then the permission will be removed from the descendent 15484543Smarks * tree and add the the local+descendent tree. 15494543Smarks */ 15504543Smarks static int 15514543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 15524543Smarks char *perm, char ld) 15534543Smarks { 15544543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 15554543Smarks zfs_perm_node_t *newnode; 15564543Smarks avl_index_t where, where2; 15574543Smarks avl_tree_t *tree, *altree; 15584543Smarks 15594543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 15604543Smarks 15614543Smarks if (ld == ZFS_DELEG_NA) { 15624543Smarks tree = &allownode->z_localdescend; 15634543Smarks altree = &allownode->z_descend; 15644543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 15654543Smarks tree = &allownode->z_local; 15664543Smarks altree = &allownode->z_descend; 15674543Smarks } else { 15684543Smarks tree = &allownode->z_descend; 15694543Smarks altree = &allownode->z_local; 15704543Smarks } 15714543Smarks permnode = avl_find(tree, &pnode, &where); 15724543Smarks permnode2 = avl_find(altree, &pnode, &where2); 15734543Smarks 15744543Smarks if (permnode2) { 15754543Smarks avl_remove(altree, permnode2); 15764543Smarks free(permnode2); 15774543Smarks if (permnode == NULL) { 15784543Smarks tree = &allownode->z_localdescend; 15794543Smarks } 15804543Smarks } 15814543Smarks 15824543Smarks /* 15834543Smarks * Now insert new permission in either requested location 15844543Smarks * local/descendent or into ld when perm will exist in both. 15854543Smarks */ 15864543Smarks if (permnode == NULL) { 15874543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 15884543Smarks sizeof (zfs_perm_node_t))) == NULL) { 15894543Smarks return (-1); 15904543Smarks } 15914543Smarks *newnode = pnode; 15924543Smarks avl_add(tree, newnode); 15934543Smarks } 15944543Smarks return (0); 15954543Smarks } 15964577Sahrens 15974543Smarks /* 15984543Smarks * Uggh, this is going to be a bit complicated. 15994543Smarks * we have an nvlist coming out of the kernel that 16004543Smarks * will indicate where the permission is set and then 16014543Smarks * it will contain allow of the various "who's", and what 16024543Smarks * their permissions are. To further complicate this 16034543Smarks * we will then have to coalesce the local,descendent 16044543Smarks * and local+descendent permissions where appropriate. 16054543Smarks * The kernel only knows about a permission as being local 16064543Smarks * or descendent, but not both. 16074543Smarks * 16084543Smarks * In order to make this easier for zfs_main to deal with 16094543Smarks * a series of AVL trees will be used to maintain 16104543Smarks * all of this, primarily for sorting purposes as well 16114543Smarks * as the ability to quickly locate a specific entry. 16124543Smarks * 16134543Smarks * What we end up with are tree's for sets, create perms, 16144543Smarks * user, groups and everyone. With each of those trees 16154543Smarks * we have subtrees for local, descendent and local+descendent 16164543Smarks * permissions. 16174543Smarks */ 16184543Smarks int 16194543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 16204543Smarks { 16214543Smarks zfs_cmd_t zc = { 0 }; 16224543Smarks int error; 16234543Smarks nvlist_t *nvlist; 16244543Smarks nvlist_t *permnv, *sourcenv; 16254543Smarks nvpair_t *who_pair, *source_pair; 16264543Smarks nvpair_t *perm_pair; 16274543Smarks char errbuf[1024]; 16284543Smarks zfs_allow_t *zallowp, *newallowp; 16294543Smarks char ld; 16304543Smarks char *nvpname; 16314543Smarks uid_t uid; 16324543Smarks gid_t gid; 16334543Smarks avl_tree_t *tree; 16344543Smarks avl_index_t where; 16354543Smarks 16364543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 16374543Smarks 16384543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 16394543Smarks return (-1); 16404543Smarks 16414543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 16424543Smarks if (errno == ENOMEM) { 16434543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 16444543Smarks zcmd_free_nvlists(&zc); 16454543Smarks return (-1); 16464543Smarks } 16474543Smarks } else if (errno == ENOTSUP) { 16484543Smarks zcmd_free_nvlists(&zc); 16494543Smarks (void) snprintf(errbuf, sizeof (errbuf), 16504543Smarks gettext("Pool must be upgraded to use 'allow'")); 16514543Smarks return (zfs_error(zhp->zfs_hdl, 16524543Smarks EZFS_BADVERSION, errbuf)); 16534543Smarks } else { 16544543Smarks zcmd_free_nvlists(&zc); 16554543Smarks return (-1); 16564543Smarks } 16574543Smarks } 16584543Smarks 16594543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 16604543Smarks zcmd_free_nvlists(&zc); 16614543Smarks return (-1); 16624543Smarks } 16634543Smarks 16644543Smarks zcmd_free_nvlists(&zc); 16654543Smarks 16664543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 16674543Smarks 16684543Smarks if (source_pair == NULL) { 16694543Smarks *zfs_perms = NULL; 16704543Smarks return (0); 16714543Smarks } 16724543Smarks 16734543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 16744543Smarks if (*zfs_perms == NULL) { 16754543Smarks return (0); 16764543Smarks } 16774543Smarks 16784543Smarks zallowp = *zfs_perms; 16794543Smarks 16804543Smarks for (;;) { 16814543Smarks struct passwd *pwd; 16824543Smarks struct group *grp; 16834543Smarks zfs_allow_node_t *allownode; 16844543Smarks zfs_allow_node_t findallownode; 16854543Smarks zfs_allow_node_t *newallownode; 16864543Smarks 16874543Smarks (void) strlcpy(zallowp->z_setpoint, 16884543Smarks nvpair_name(source_pair), 16894543Smarks sizeof (zallowp->z_setpoint)); 16904543Smarks 16914543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 16924543Smarks goto abort; 16934543Smarks 16944543Smarks /* 16954543Smarks * Make sure nvlist is composed correctly 16964543Smarks */ 16974543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 16984543Smarks goto abort; 16994543Smarks } 17004543Smarks 17014543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 17024543Smarks if (who_pair == NULL) { 17034543Smarks goto abort; 17044543Smarks } 17054543Smarks 17064543Smarks do { 17074543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 17084543Smarks if (error) { 17094543Smarks goto abort; 17104543Smarks } 17114543Smarks 17124543Smarks /* 17134543Smarks * First build up the key to use 17144543Smarks * for looking up in the various 17154543Smarks * who trees. 17164543Smarks */ 17174543Smarks ld = nvpair_name(who_pair)[1]; 17184543Smarks nvpname = nvpair_name(who_pair); 17194543Smarks switch (nvpair_name(who_pair)[0]) { 17204543Smarks case ZFS_DELEG_USER: 17214543Smarks case ZFS_DELEG_USER_SETS: 17224543Smarks tree = &zallowp->z_user; 17234543Smarks uid = atol(&nvpname[3]); 17244543Smarks pwd = getpwuid(uid); 17254543Smarks (void) snprintf(findallownode.z_key, 17264543Smarks sizeof (findallownode.z_key), "user %s", 17274543Smarks (pwd) ? pwd->pw_name : 17284543Smarks &nvpair_name(who_pair)[3]); 17294543Smarks break; 17304543Smarks case ZFS_DELEG_GROUP: 17314543Smarks case ZFS_DELEG_GROUP_SETS: 17324543Smarks tree = &zallowp->z_group; 17334543Smarks gid = atol(&nvpname[3]); 17344543Smarks grp = getgrgid(gid); 17354543Smarks (void) snprintf(findallownode.z_key, 17364543Smarks sizeof (findallownode.z_key), "group %s", 17374543Smarks (grp) ? grp->gr_name : 17384543Smarks &nvpair_name(who_pair)[3]); 17394543Smarks break; 17404543Smarks case ZFS_DELEG_CREATE: 17414543Smarks case ZFS_DELEG_CREATE_SETS: 17424543Smarks tree = &zallowp->z_crperms; 17434543Smarks (void) strlcpy(findallownode.z_key, "", 17444543Smarks sizeof (findallownode.z_key)); 17454543Smarks break; 17464543Smarks case ZFS_DELEG_EVERYONE: 17474543Smarks case ZFS_DELEG_EVERYONE_SETS: 17484543Smarks (void) snprintf(findallownode.z_key, 17494543Smarks sizeof (findallownode.z_key), "everyone"); 17504543Smarks tree = &zallowp->z_everyone; 17514543Smarks break; 17524543Smarks case ZFS_DELEG_NAMED_SET: 17534543Smarks case ZFS_DELEG_NAMED_SET_SETS: 17544543Smarks (void) snprintf(findallownode.z_key, 17554543Smarks sizeof (findallownode.z_key), "%s", 17564543Smarks &nvpair_name(who_pair)[3]); 17574543Smarks tree = &zallowp->z_sets; 17584543Smarks break; 17594543Smarks } 17604543Smarks 17614543Smarks /* 17624543Smarks * Place who in tree 17634543Smarks */ 17644543Smarks allownode = avl_find(tree, &findallownode, &where); 17654543Smarks if (allownode == NULL) { 17664543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 17674543Smarks sizeof (zfs_allow_node_t))) == NULL) { 17684543Smarks goto abort; 17694543Smarks } 17704543Smarks avl_create(&newallownode->z_localdescend, 17714543Smarks perm_compare, 17724543Smarks sizeof (zfs_perm_node_t), 17734543Smarks offsetof(zfs_perm_node_t, z_node)); 17744543Smarks avl_create(&newallownode->z_local, 17754543Smarks perm_compare, 17764543Smarks sizeof (zfs_perm_node_t), 17774543Smarks offsetof(zfs_perm_node_t, z_node)); 17784543Smarks avl_create(&newallownode->z_descend, 17794543Smarks perm_compare, 17804543Smarks sizeof (zfs_perm_node_t), 17814543Smarks offsetof(zfs_perm_node_t, z_node)); 17824543Smarks (void) strlcpy(newallownode->z_key, 17834543Smarks findallownode.z_key, 17844543Smarks sizeof (findallownode.z_key)); 17854543Smarks avl_insert(tree, newallownode, where); 17864543Smarks allownode = newallownode; 17874543Smarks } 17884543Smarks 17894543Smarks /* 17904543Smarks * Now iterate over the permissions and 17914543Smarks * place them in the appropriate local, 17924543Smarks * descendent or local+descendent tree. 17934543Smarks * 17944543Smarks * The permissions are added to the tree 17954543Smarks * via zfs_coalesce_perm(). 17964543Smarks */ 17974543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 17984543Smarks if (perm_pair == NULL) 17994543Smarks goto abort; 18004543Smarks do { 18014543Smarks if (zfs_coalesce_perm(zhp, allownode, 18024543Smarks nvpair_name(perm_pair), ld) != 0) 18034543Smarks goto abort; 18044543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 18054543Smarks perm_pair)); 18064543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 18074543Smarks 18084543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 18094543Smarks if (source_pair == NULL) 18104543Smarks break; 18114543Smarks 18124543Smarks /* 18134543Smarks * allocate another node from the link list of 18144543Smarks * zfs_allow_t structures 18154543Smarks */ 18164543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 18174543Smarks nvpair_name(source_pair)); 18184543Smarks if (newallowp == NULL) { 18194543Smarks goto abort; 18204543Smarks } 18214543Smarks zallowp = newallowp; 18224543Smarks } 18234543Smarks nvlist_free(nvlist); 18244543Smarks return (0); 18254543Smarks abort: 18264543Smarks zfs_free_allows(*zfs_perms); 18274543Smarks nvlist_free(nvlist); 18284543Smarks return (-1); 18294543Smarks } 18304543Smarks 18315993Smarks static char * 18325993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note) 18335993Smarks { 18345993Smarks /* 18355993Smarks * Don't put newlines on end of lines 18365993Smarks */ 18375993Smarks switch (note) { 18385993Smarks case ZFS_DELEG_NOTE_CREATE: 18395993Smarks return (dgettext(TEXT_DOMAIN, 18405993Smarks "Must also have the 'mount' ability")); 18415993Smarks case ZFS_DELEG_NOTE_DESTROY: 18425993Smarks return (dgettext(TEXT_DOMAIN, 18435993Smarks "Must also have the 'mount' ability")); 18445993Smarks case ZFS_DELEG_NOTE_SNAPSHOT: 18455993Smarks return (dgettext(TEXT_DOMAIN, 18465993Smarks "Must also have the 'mount' ability")); 18475993Smarks case ZFS_DELEG_NOTE_ROLLBACK: 18485993Smarks return (dgettext(TEXT_DOMAIN, 18495993Smarks "Must also have the 'mount' ability")); 18505993Smarks case ZFS_DELEG_NOTE_CLONE: 18515993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'create' " 18525993Smarks "ability and 'mount'\n" 18535993Smarks "\t\t\t\tability in the origin file system")); 18545993Smarks case ZFS_DELEG_NOTE_PROMOTE: 18555993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n" 18565993Smarks "\t\t\t\tand 'promote' ability in the origin file system")); 18575993Smarks case ZFS_DELEG_NOTE_RENAME: 18585993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' " 18595993Smarks "and 'create' \n\t\t\t\tability in the new parent")); 18605993Smarks case ZFS_DELEG_NOTE_RECEIVE: 18615993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'" 18625993Smarks " and 'create' ability")); 18635993Smarks case ZFS_DELEG_NOTE_USERPROP: 18645993Smarks return (dgettext(TEXT_DOMAIN, 18655993Smarks "Allows changing any user property")); 18665993Smarks case ZFS_DELEG_NOTE_ALLOW: 18675993Smarks return (dgettext(TEXT_DOMAIN, 18685993Smarks "Must also have the permission that is being\n" 18695993Smarks "\t\t\t\tallowed")); 18705993Smarks case ZFS_DELEG_NOTE_MOUNT: 18715993Smarks return (dgettext(TEXT_DOMAIN, 18725993Smarks "Allows mount/umount of ZFS datasets")); 18735993Smarks case ZFS_DELEG_NOTE_SHARE: 18745993Smarks return (dgettext(TEXT_DOMAIN, 18755993Smarks "Allows sharing file systems over NFS or SMB\n" 18765993Smarks "\t\t\t\tprotocols")); 18775993Smarks case ZFS_DELEG_NOTE_NONE: 18785993Smarks default: 18795993Smarks return (dgettext(TEXT_DOMAIN, "")); 18805993Smarks } 18815993Smarks } 18825993Smarks 18835993Smarks typedef enum { 18845993Smarks ZFS_DELEG_SUBCOMMAND, 18855993Smarks ZFS_DELEG_PROP, 18865993Smarks ZFS_DELEG_OTHER 18875993Smarks } zfs_deleg_perm_type_t; 18885993Smarks 18895993Smarks /* 18905993Smarks * is the permission a subcommand or other? 18915993Smarks */ 18925993Smarks zfs_deleg_perm_type_t 18935993Smarks zfs_deleg_perm_type(const char *perm) 18945993Smarks { 18955993Smarks if (strcmp(perm, "userprop") == 0) 18965993Smarks return (ZFS_DELEG_OTHER); 18975993Smarks else 18985993Smarks return (ZFS_DELEG_SUBCOMMAND); 18995993Smarks } 19005993Smarks 19015993Smarks static char * 19025993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type) 19035993Smarks { 19045993Smarks switch (type) { 19055993Smarks case ZFS_DELEG_SUBCOMMAND: 19065993Smarks return (dgettext(TEXT_DOMAIN, "subcommand")); 19075993Smarks case ZFS_DELEG_PROP: 19085993Smarks return (dgettext(TEXT_DOMAIN, "property")); 19095993Smarks case ZFS_DELEG_OTHER: 19105993Smarks return (dgettext(TEXT_DOMAIN, "other")); 19115993Smarks } 19125993Smarks return (""); 19135993Smarks } 19145993Smarks 19155993Smarks /*ARGSUSED*/ 19165993Smarks static int 19175993Smarks zfs_deleg_prop_cb(int prop, void *cb) 19185993Smarks { 19195993Smarks if (zfs_prop_delegatable(prop)) 19205993Smarks (void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop), 19215993Smarks zfs_deleg_perm_type_str(ZFS_DELEG_PROP)); 19225993Smarks 19235993Smarks return (ZPROP_CONT); 19245993Smarks } 19255993Smarks 19265993Smarks void 19275993Smarks zfs_deleg_permissions(void) 19285993Smarks { 19295993Smarks int i; 19305993Smarks 19315993Smarks (void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME", 19325993Smarks "TYPE", "NOTES"); 19335993Smarks 19345993Smarks /* 19355993Smarks * First print out the subcommands 19365993Smarks */ 19375993Smarks for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) { 19385993Smarks (void) fprintf(stderr, "%-15s %-15s\t%s\n", 19395993Smarks zfs_deleg_perm_tab[i].z_perm, 19405993Smarks zfs_deleg_perm_type_str( 19415993Smarks zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)), 19425993Smarks zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note)); 19435993Smarks } 19445993Smarks 19455993Smarks (void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE, 19465993Smarks ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME); 19475993Smarks } 19485993Smarks 1949789Sahrens /* 1950789Sahrens * Given a property name and value, set the property for the given dataset. 1951789Sahrens */ 1952789Sahrens int 19532676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1954789Sahrens { 1955789Sahrens zfs_cmd_t zc = { 0 }; 19562676Seschrock int ret = -1; 19572676Seschrock prop_changelist_t *cl = NULL; 19582082Seschrock char errbuf[1024]; 19592082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 19602676Seschrock nvlist_t *nvl = NULL, *realprops; 19612676Seschrock zfs_prop_t prop; 19627509SMark.Musante@Sun.COM boolean_t do_prefix; 19637509SMark.Musante@Sun.COM uint64_t idx; 19642082Seschrock 19652082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 19662676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 19672082Seschrock zhp->zfs_name); 19682082Seschrock 19692676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 19702676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 19712676Seschrock (void) no_memory(hdl); 19722676Seschrock goto error; 1973789Sahrens } 1974789Sahrens 19757184Stimh if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl, 19762676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 19772676Seschrock goto error; 19785094Slling 19792676Seschrock nvlist_free(nvl); 19802676Seschrock nvl = realprops; 19812676Seschrock 19822676Seschrock prop = zfs_name_to_prop(propname); 19832676Seschrock 19847366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 19852676Seschrock goto error; 1986789Sahrens 1987789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 19882082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19892082Seschrock "child dataset with inherited mountpoint is used " 19902082Seschrock "in a non-global zone")); 19912082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1992789Sahrens goto error; 1993789Sahrens } 1994789Sahrens 19957509SMark.Musante@Sun.COM /* 19967509SMark.Musante@Sun.COM * If the dataset's canmount property is being set to noauto, 19977509SMark.Musante@Sun.COM * then we want to prevent unmounting & remounting it. 19987509SMark.Musante@Sun.COM */ 19997509SMark.Musante@Sun.COM do_prefix = !((prop == ZFS_PROP_CANMOUNT) && 20007509SMark.Musante@Sun.COM (zprop_string_to_index(prop, propval, &idx, 20017509SMark.Musante@Sun.COM ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO)); 20026168Shs24103 20036168Shs24103 if (do_prefix && (ret = changelist_prefix(cl)) != 0) 20047509SMark.Musante@Sun.COM goto error; 2005789Sahrens 2006789Sahrens /* 2007789Sahrens * Execute the corresponding ioctl() to set this property. 2008789Sahrens */ 2009789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2010789Sahrens 20115094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 20122676Seschrock goto error; 20132676Seschrock 20144543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 2015*8845Samw@Sun.COM 2016789Sahrens if (ret != 0) { 2017789Sahrens switch (errno) { 2018789Sahrens 2019789Sahrens case ENOSPC: 2020789Sahrens /* 2021789Sahrens * For quotas and reservations, ENOSPC indicates 2022789Sahrens * something different; setting a quota or reservation 2023789Sahrens * doesn't use any disk space. 2024789Sahrens */ 2025789Sahrens switch (prop) { 2026789Sahrens case ZFS_PROP_QUOTA: 20275378Sck153898 case ZFS_PROP_REFQUOTA: 20282082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20292082Seschrock "size is less than current used or " 20302082Seschrock "reserved space")); 20312082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 2032789Sahrens break; 2033789Sahrens 2034789Sahrens case ZFS_PROP_RESERVATION: 20355378Sck153898 case ZFS_PROP_REFRESERVATION: 20362082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20372082Seschrock "size is greater than available space")); 20382082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 2039789Sahrens break; 2040789Sahrens 2041789Sahrens default: 20422082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 2043789Sahrens break; 2044789Sahrens } 2045789Sahrens break; 2046789Sahrens 2047789Sahrens case EBUSY: 20482082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 20492082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 20502082Seschrock else 20512676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 2052789Sahrens break; 2053789Sahrens 20541175Slling case EROFS: 20552082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 20561175Slling break; 20571175Slling 20583886Sahl case ENOTSUP: 20593886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20605977Smarks "pool and or dataset must be upgraded to set this " 20614603Sahrens "property or value")); 20623886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 20633886Sahl break; 20643886Sahl 20657042Sgw25295 case ERANGE: 20667042Sgw25295 if (prop == ZFS_PROP_COMPRESSION) { 20677042Sgw25295 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20687042Sgw25295 "property setting is not allowed on " 20697042Sgw25295 "bootable datasets")); 20707042Sgw25295 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 20717042Sgw25295 } else { 20727042Sgw25295 (void) zfs_standard_error(hdl, errno, errbuf); 20737042Sgw25295 } 20747042Sgw25295 break; 20757042Sgw25295 2076789Sahrens case EOVERFLOW: 2077789Sahrens /* 2078789Sahrens * This platform can't address a volume this big. 2079789Sahrens */ 2080789Sahrens #ifdef _ILP32 2081789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 20822082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 2083789Sahrens break; 2084789Sahrens } 2085789Sahrens #endif 20862082Seschrock /* FALLTHROUGH */ 2087789Sahrens default: 20882082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 2089789Sahrens } 2090789Sahrens } else { 20916168Shs24103 if (do_prefix) 20926168Shs24103 ret = changelist_postfix(cl); 20936168Shs24103 2094789Sahrens /* 2095789Sahrens * Refresh the statistics so the new property value 2096789Sahrens * is reflected. 2097789Sahrens */ 20986168Shs24103 if (ret == 0) 20992676Seschrock (void) get_stats(zhp); 2100789Sahrens } 2101789Sahrens 2102789Sahrens error: 21032676Seschrock nvlist_free(nvl); 21042676Seschrock zcmd_free_nvlists(&zc); 21052676Seschrock if (cl) 21062676Seschrock changelist_free(cl); 2107789Sahrens return (ret); 2108789Sahrens } 2109789Sahrens 2110789Sahrens /* 2111789Sahrens * Given a property, inherit the value from the parent dataset. 2112789Sahrens */ 2113789Sahrens int 21142676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 2115789Sahrens { 2116789Sahrens zfs_cmd_t zc = { 0 }; 2117789Sahrens int ret; 2118789Sahrens prop_changelist_t *cl; 21192082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 21202082Seschrock char errbuf[1024]; 21212676Seschrock zfs_prop_t prop; 21222082Seschrock 21232082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 21242082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 2125789Sahrens 21265094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 21272676Seschrock /* 21282676Seschrock * For user properties, the amount of work we have to do is very 21292676Seschrock * small, so just do it here. 21302676Seschrock */ 21312676Seschrock if (!zfs_prop_user(propname)) { 21322676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21332676Seschrock "invalid property")); 21342676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 21352676Seschrock } 21362676Seschrock 21372676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21382676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 21392676Seschrock 21404849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 21412676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 21422676Seschrock 21432676Seschrock return (0); 21442676Seschrock } 21452676Seschrock 2146789Sahrens /* 2147789Sahrens * Verify that this property is inheritable. 2148789Sahrens */ 21492082Seschrock if (zfs_prop_readonly(prop)) 21502082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 21512082Seschrock 21522082Seschrock if (!zfs_prop_inheritable(prop)) 21532082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2154789Sahrens 2155789Sahrens /* 2156789Sahrens * Check to see if the value applies to this type 2157789Sahrens */ 21582082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 21592082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2160789Sahrens 21613443Srm160521 /* 21623443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 21633443Srm160521 */ 21643443Srm160521 propname = zfs_prop_to_name(prop); 2165789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21662676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2167789Sahrens 2168789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2169789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 21702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21712082Seschrock "dataset is used in a non-global zone")); 21722082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2173789Sahrens } 2174789Sahrens 2175789Sahrens /* 2176789Sahrens * Determine datasets which will be affected by this change, if any. 2177789Sahrens */ 21787366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 2179789Sahrens return (-1); 2180789Sahrens 2181789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 21822082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21832082Seschrock "child dataset with inherited mountpoint is used " 21842082Seschrock "in a non-global zone")); 21852082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2186789Sahrens goto error; 2187789Sahrens } 2188789Sahrens 2189789Sahrens if ((ret = changelist_prefix(cl)) != 0) 2190789Sahrens goto error; 2191789Sahrens 21924849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 21932082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2194789Sahrens } else { 2195789Sahrens 21962169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 2197789Sahrens goto error; 2198789Sahrens 2199789Sahrens /* 2200789Sahrens * Refresh the statistics so the new property is reflected. 2201789Sahrens */ 2202789Sahrens (void) get_stats(zhp); 2203789Sahrens } 2204789Sahrens 2205789Sahrens error: 2206789Sahrens changelist_free(cl); 2207789Sahrens return (ret); 2208789Sahrens } 2209789Sahrens 2210789Sahrens /* 22111356Seschrock * True DSL properties are stored in an nvlist. The following two functions 22121356Seschrock * extract them appropriately. 22131356Seschrock */ 22141356Seschrock static uint64_t 22151356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 22161356Seschrock { 22171356Seschrock nvlist_t *nv; 22181356Seschrock uint64_t value; 22191356Seschrock 22202885Sahrens *source = NULL; 22211356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 22221356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 22235094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 22245094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 22251356Seschrock } else { 22268802SSanjeev.Bagewadi@Sun.COM verify(!zhp->zfs_props_table || 22278802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table[prop] == B_TRUE); 22281356Seschrock value = zfs_prop_default_numeric(prop); 22291356Seschrock *source = ""; 22301356Seschrock } 22311356Seschrock 22321356Seschrock return (value); 22331356Seschrock } 22341356Seschrock 22351356Seschrock static char * 22361356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 22371356Seschrock { 22381356Seschrock nvlist_t *nv; 22391356Seschrock char *value; 22401356Seschrock 22412885Sahrens *source = NULL; 22421356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 22431356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 22445094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 22455094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 22461356Seschrock } else { 22478802SSanjeev.Bagewadi@Sun.COM verify(!zhp->zfs_props_table || 22488802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table[prop] == B_TRUE); 22491356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 22501356Seschrock value = ""; 22511356Seschrock *source = ""; 22521356Seschrock } 22531356Seschrock 22541356Seschrock return (value); 22551356Seschrock } 22561356Seschrock 22571356Seschrock /* 2258789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2259789Sahrens * zfs_prop_get_int() are built using this interface. 2260789Sahrens * 2261789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2262789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2263789Sahrens * If they differ from the on-disk values, report the current values and mark 2264789Sahrens * the source "temporary". 2265789Sahrens */ 22662082Seschrock static int 22675094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 22682082Seschrock char **source, uint64_t *val) 2269789Sahrens { 22705147Srm160521 zfs_cmd_t zc = { 0 }; 22715592Stimh nvlist_t *zplprops = NULL; 2272789Sahrens struct mnttab mnt; 22733265Sahrens char *mntopt_on = NULL; 22743265Sahrens char *mntopt_off = NULL; 2275789Sahrens 2276789Sahrens *source = NULL; 2277789Sahrens 22783265Sahrens switch (prop) { 22793265Sahrens case ZFS_PROP_ATIME: 22803265Sahrens mntopt_on = MNTOPT_ATIME; 22813265Sahrens mntopt_off = MNTOPT_NOATIME; 22823265Sahrens break; 22833265Sahrens 22843265Sahrens case ZFS_PROP_DEVICES: 22853265Sahrens mntopt_on = MNTOPT_DEVICES; 22863265Sahrens mntopt_off = MNTOPT_NODEVICES; 22873265Sahrens break; 22883265Sahrens 22893265Sahrens case ZFS_PROP_EXEC: 22903265Sahrens mntopt_on = MNTOPT_EXEC; 22913265Sahrens mntopt_off = MNTOPT_NOEXEC; 22923265Sahrens break; 22933265Sahrens 22943265Sahrens case ZFS_PROP_READONLY: 22953265Sahrens mntopt_on = MNTOPT_RO; 22963265Sahrens mntopt_off = MNTOPT_RW; 22973265Sahrens break; 22983265Sahrens 22993265Sahrens case ZFS_PROP_SETUID: 23003265Sahrens mntopt_on = MNTOPT_SETUID; 23013265Sahrens mntopt_off = MNTOPT_NOSETUID; 23023265Sahrens break; 23033265Sahrens 23043265Sahrens case ZFS_PROP_XATTR: 23053265Sahrens mntopt_on = MNTOPT_XATTR; 23063265Sahrens mntopt_off = MNTOPT_NOXATTR; 23073265Sahrens break; 23085331Samw 23095331Samw case ZFS_PROP_NBMAND: 23105331Samw mntopt_on = MNTOPT_NBMAND; 23115331Samw mntopt_off = MNTOPT_NONBMAND; 23125331Samw break; 23133265Sahrens } 23143265Sahrens 23152474Seschrock /* 23162474Seschrock * Because looking up the mount options is potentially expensive 23172474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 23182474Seschrock * we're looking up a property which requires its presence. 23192474Seschrock */ 23202474Seschrock if (!zhp->zfs_mntcheck && 23213265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 23228228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 23238228SEric.Taylor@Sun.COM struct mnttab entry; 23248228SEric.Taylor@Sun.COM 23258228SEric.Taylor@Sun.COM if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 23268228SEric.Taylor@Sun.COM zhp->zfs_mntopts = zfs_strdup(hdl, 23273265Sahrens entry.mnt_mntopts); 23283265Sahrens if (zhp->zfs_mntopts == NULL) 23293265Sahrens return (-1); 23303265Sahrens } 23312474Seschrock 23322474Seschrock zhp->zfs_mntcheck = B_TRUE; 23332474Seschrock } 23342474Seschrock 2335789Sahrens if (zhp->zfs_mntopts == NULL) 2336789Sahrens mnt.mnt_mntopts = ""; 2337789Sahrens else 2338789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2339789Sahrens 2340789Sahrens switch (prop) { 2341789Sahrens case ZFS_PROP_ATIME: 23423265Sahrens case ZFS_PROP_DEVICES: 23433265Sahrens case ZFS_PROP_EXEC: 23443265Sahrens case ZFS_PROP_READONLY: 23453265Sahrens case ZFS_PROP_SETUID: 23463265Sahrens case ZFS_PROP_XATTR: 23475331Samw case ZFS_PROP_NBMAND: 23482082Seschrock *val = getprop_uint64(zhp, prop, source); 23492082Seschrock 23503265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 23512082Seschrock *val = B_TRUE; 2352789Sahrens if (src) 23535094Slling *src = ZPROP_SRC_TEMPORARY; 23543265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 23552082Seschrock *val = B_FALSE; 2356789Sahrens if (src) 23575094Slling *src = ZPROP_SRC_TEMPORARY; 2358789Sahrens } 23592082Seschrock break; 2360789Sahrens 23613265Sahrens case ZFS_PROP_CANMOUNT: 23622082Seschrock *val = getprop_uint64(zhp, prop, source); 23636168Shs24103 if (*val != ZFS_CANMOUNT_ON) 23643417Srm160521 *source = zhp->zfs_name; 23653417Srm160521 else 23663417Srm160521 *source = ""; /* default */ 23672082Seschrock break; 2368789Sahrens 2369789Sahrens case ZFS_PROP_QUOTA: 23705378Sck153898 case ZFS_PROP_REFQUOTA: 2371789Sahrens case ZFS_PROP_RESERVATION: 23725378Sck153898 case ZFS_PROP_REFRESERVATION: 23732885Sahrens *val = getprop_uint64(zhp, prop, source); 23742885Sahrens if (*val == 0) 2375789Sahrens *source = ""; /* default */ 2376789Sahrens else 2377789Sahrens *source = zhp->zfs_name; 23782082Seschrock break; 2379789Sahrens 2380789Sahrens case ZFS_PROP_MOUNTED: 23812082Seschrock *val = (zhp->zfs_mntopts != NULL); 23822082Seschrock break; 2383789Sahrens 23843377Seschrock case ZFS_PROP_NUMCLONES: 23853377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 23863377Seschrock break; 23873377Seschrock 23885147Srm160521 case ZFS_PROP_VERSION: 23895498Stimh case ZFS_PROP_NORMALIZE: 23905498Stimh case ZFS_PROP_UTF8ONLY: 23915498Stimh case ZFS_PROP_CASE: 23925498Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 23935498Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 23945473Srm160521 return (-1); 23955147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23965498Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 23975498Stimh zcmd_free_nvlists(&zc); 23985147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 23995498Stimh "unable to get %s property"), 24005498Stimh zfs_prop_to_name(prop)); 24015147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 24025147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 24035147Srm160521 } 24045498Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 24055498Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 24065498Stimh val) != 0) { 24075498Stimh zcmd_free_nvlists(&zc); 24085498Stimh zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 24095498Stimh "unable to get %s property"), 24105498Stimh zfs_prop_to_name(prop)); 24115498Stimh return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM, 24125498Stimh dgettext(TEXT_DOMAIN, "internal error"))); 24135498Stimh } 24145592Stimh if (zplprops) 24155592Stimh nvlist_free(zplprops); 24165498Stimh zcmd_free_nvlists(&zc); 24175147Srm160521 break; 24185147Srm160521 2419789Sahrens default: 24204577Sahrens switch (zfs_prop_get_type(prop)) { 24214787Sahrens case PROP_TYPE_NUMBER: 24224787Sahrens case PROP_TYPE_INDEX: 24234577Sahrens *val = getprop_uint64(zhp, prop, source); 24247390SMatthew.Ahrens@Sun.COM /* 24257390SMatthew.Ahrens@Sun.COM * If we tried to use a defalut value for a 24267390SMatthew.Ahrens@Sun.COM * readonly property, it means that it was not 24277390SMatthew.Ahrens@Sun.COM * present; return an error. 24287390SMatthew.Ahrens@Sun.COM */ 24297390SMatthew.Ahrens@Sun.COM if (zfs_prop_readonly(prop) && 24307390SMatthew.Ahrens@Sun.COM *source && (*source)[0] == '\0') { 24317390SMatthew.Ahrens@Sun.COM return (-1); 24327390SMatthew.Ahrens@Sun.COM } 24334577Sahrens break; 24344577Sahrens 24354787Sahrens case PROP_TYPE_STRING: 24364577Sahrens default: 24374577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 24384577Sahrens "cannot get non-numeric property")); 24394577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 24404577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 24414577Sahrens } 2442789Sahrens } 2443789Sahrens 2444789Sahrens return (0); 2445789Sahrens } 2446789Sahrens 2447789Sahrens /* 2448789Sahrens * Calculate the source type, given the raw source string. 2449789Sahrens */ 2450789Sahrens static void 24515094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2452789Sahrens char *statbuf, size_t statlen) 2453789Sahrens { 24545094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2455789Sahrens return; 2456789Sahrens 2457789Sahrens if (source == NULL) { 24585094Slling *srctype = ZPROP_SRC_NONE; 2459789Sahrens } else if (source[0] == '\0') { 24605094Slling *srctype = ZPROP_SRC_DEFAULT; 2461789Sahrens } else { 2462789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 24635094Slling *srctype = ZPROP_SRC_LOCAL; 2464789Sahrens } else { 2465789Sahrens (void) strlcpy(statbuf, source, statlen); 24665094Slling *srctype = ZPROP_SRC_INHERITED; 2467789Sahrens } 2468789Sahrens } 2469789Sahrens 2470789Sahrens } 2471789Sahrens 2472789Sahrens /* 2473789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2474789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2475789Sahrens * human-readable form. 2476789Sahrens * 2477789Sahrens * Returns 0 on success, or -1 on error. 2478789Sahrens */ 2479789Sahrens int 2480789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 24815094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2482789Sahrens { 2483789Sahrens char *source = NULL; 2484789Sahrens uint64_t val; 2485789Sahrens char *str; 24862676Seschrock const char *strval; 2487789Sahrens 2488789Sahrens /* 2489789Sahrens * Check to see if this property applies to our object 2490789Sahrens */ 2491789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2492789Sahrens return (-1); 2493789Sahrens 2494789Sahrens if (src) 24955094Slling *src = ZPROP_SRC_NONE; 2496789Sahrens 2497789Sahrens switch (prop) { 2498789Sahrens case ZFS_PROP_CREATION: 2499789Sahrens /* 2500789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2501789Sahrens * this into a string unless 'literal' is specified. 2502789Sahrens */ 2503789Sahrens { 25042885Sahrens val = getprop_uint64(zhp, prop, &source); 25052885Sahrens time_t time = (time_t)val; 2506789Sahrens struct tm t; 2507789Sahrens 2508789Sahrens if (literal || 2509789Sahrens localtime_r(&time, &t) == NULL || 2510789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2511789Sahrens &t) == 0) 25122885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2513789Sahrens } 2514789Sahrens break; 2515789Sahrens 2516789Sahrens case ZFS_PROP_MOUNTPOINT: 2517789Sahrens /* 2518789Sahrens * Getting the precise mountpoint can be tricky. 2519789Sahrens * 2520789Sahrens * - for 'none' or 'legacy', return those values. 2521789Sahrens * - for inherited mountpoints, we want to take everything 2522789Sahrens * after our ancestor and append it to the inherited value. 2523789Sahrens * 2524789Sahrens * If the pool has an alternate root, we want to prepend that 2525789Sahrens * root to any values we return. 2526789Sahrens */ 25276865Srm160521 25281356Seschrock str = getprop_string(zhp, prop, &source); 25291356Seschrock 25306612Sgw25295 if (str[0] == '/') { 25316865Srm160521 char buf[MAXPATHLEN]; 25326865Srm160521 char *root = buf; 25331356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2534789Sahrens 2535789Sahrens if (relpath[0] == '/') 2536789Sahrens relpath++; 25376612Sgw25295 25386865Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 25396865Srm160521 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) || 25406865Srm160521 (strcmp(root, "-") == 0)) 25416865Srm160521 root[0] = '\0'; 25426612Sgw25295 /* 25436612Sgw25295 * Special case an alternate root of '/'. This will 25446612Sgw25295 * avoid having multiple leading slashes in the 25456612Sgw25295 * mountpoint path. 25466612Sgw25295 */ 25476612Sgw25295 if (strcmp(root, "/") == 0) 25486612Sgw25295 root++; 25496612Sgw25295 25506612Sgw25295 /* 25516612Sgw25295 * If the mountpoint is '/' then skip over this 25526612Sgw25295 * if we are obtaining either an alternate root or 25536612Sgw25295 * an inherited mountpoint. 25546612Sgw25295 */ 25556612Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 25566612Sgw25295 relpath[0] != '\0')) 25571356Seschrock str++; 2558789Sahrens 2559789Sahrens if (relpath[0] == '\0') 2560789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 25611356Seschrock root, str); 2562789Sahrens else 2563789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 25641356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2565789Sahrens relpath); 2566789Sahrens } else { 2567789Sahrens /* 'legacy' or 'none' */ 25681356Seschrock (void) strlcpy(propbuf, str, proplen); 2569789Sahrens } 2570789Sahrens 2571789Sahrens break; 2572789Sahrens 2573789Sahrens case ZFS_PROP_ORIGIN: 25742885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2575789Sahrens proplen); 2576789Sahrens /* 2577789Sahrens * If there is no parent at all, return failure to indicate that 2578789Sahrens * it doesn't apply to this dataset. 2579789Sahrens */ 2580789Sahrens if (propbuf[0] == '\0') 2581789Sahrens return (-1); 2582789Sahrens break; 2583789Sahrens 2584789Sahrens case ZFS_PROP_QUOTA: 25855378Sck153898 case ZFS_PROP_REFQUOTA: 2586789Sahrens case ZFS_PROP_RESERVATION: 25875378Sck153898 case ZFS_PROP_REFRESERVATION: 25885378Sck153898 25892082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 25902082Seschrock return (-1); 2591789Sahrens 2592789Sahrens /* 2593789Sahrens * If quota or reservation is 0, we translate this into 'none' 2594789Sahrens * (unless literal is set), and indicate that it's the default 2595789Sahrens * value. Otherwise, we print the number nicely and indicate 2596789Sahrens * that its set locally. 2597789Sahrens */ 2598789Sahrens if (val == 0) { 2599789Sahrens if (literal) 2600789Sahrens (void) strlcpy(propbuf, "0", proplen); 2601789Sahrens else 2602789Sahrens (void) strlcpy(propbuf, "none", proplen); 2603789Sahrens } else { 2604789Sahrens if (literal) 26052856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 26063912Slling (u_longlong_t)val); 2607789Sahrens else 2608789Sahrens zfs_nicenum(val, propbuf, proplen); 2609789Sahrens } 2610789Sahrens break; 2611789Sahrens 2612789Sahrens case ZFS_PROP_COMPRESSRATIO: 26132082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 26142082Seschrock return (-1); 26152856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 26162856Snd150628 val / 100, (longlong_t)val % 100); 2617789Sahrens break; 2618789Sahrens 2619789Sahrens case ZFS_PROP_TYPE: 2620789Sahrens switch (zhp->zfs_type) { 2621789Sahrens case ZFS_TYPE_FILESYSTEM: 2622789Sahrens str = "filesystem"; 2623789Sahrens break; 2624789Sahrens case ZFS_TYPE_VOLUME: 2625789Sahrens str = "volume"; 2626789Sahrens break; 2627789Sahrens case ZFS_TYPE_SNAPSHOT: 2628789Sahrens str = "snapshot"; 2629789Sahrens break; 2630789Sahrens default: 26312082Seschrock abort(); 2632789Sahrens } 2633789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2634789Sahrens break; 2635789Sahrens 2636789Sahrens case ZFS_PROP_MOUNTED: 2637789Sahrens /* 2638789Sahrens * The 'mounted' property is a pseudo-property that described 2639789Sahrens * whether the filesystem is currently mounted. Even though 2640789Sahrens * it's a boolean value, the typical values of "on" and "off" 2641789Sahrens * don't make sense, so we translate to "yes" and "no". 2642789Sahrens */ 26432082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 26442082Seschrock src, &source, &val) != 0) 26452082Seschrock return (-1); 26462082Seschrock if (val) 2647789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2648789Sahrens else 2649789Sahrens (void) strlcpy(propbuf, "no", proplen); 2650789Sahrens break; 2651789Sahrens 2652789Sahrens case ZFS_PROP_NAME: 2653789Sahrens /* 2654789Sahrens * The 'name' property is a pseudo-property derived from the 2655789Sahrens * dataset name. It is presented as a real property to simplify 2656789Sahrens * consumers. 2657789Sahrens */ 2658789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2659789Sahrens break; 2660789Sahrens 2661789Sahrens default: 26624577Sahrens switch (zfs_prop_get_type(prop)) { 26634787Sahrens case PROP_TYPE_NUMBER: 26644577Sahrens if (get_numeric_property(zhp, prop, src, 26654577Sahrens &source, &val) != 0) 26664577Sahrens return (-1); 26674577Sahrens if (literal) 26684577Sahrens (void) snprintf(propbuf, proplen, "%llu", 26694577Sahrens (u_longlong_t)val); 26704577Sahrens else 26714577Sahrens zfs_nicenum(val, propbuf, proplen); 26724577Sahrens break; 26734577Sahrens 26744787Sahrens case PROP_TYPE_STRING: 26754577Sahrens (void) strlcpy(propbuf, 26764577Sahrens getprop_string(zhp, prop, &source), proplen); 26774577Sahrens break; 26784577Sahrens 26794787Sahrens case PROP_TYPE_INDEX: 26804861Sahrens if (get_numeric_property(zhp, prop, src, 26814861Sahrens &source, &val) != 0) 26824861Sahrens return (-1); 26834861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 26844577Sahrens return (-1); 26854577Sahrens (void) strlcpy(propbuf, strval, proplen); 26864577Sahrens break; 26874577Sahrens 26884577Sahrens default: 26894577Sahrens abort(); 26904577Sahrens } 2691789Sahrens } 2692789Sahrens 2693789Sahrens get_source(zhp, src, source, statbuf, statlen); 2694789Sahrens 2695789Sahrens return (0); 2696789Sahrens } 2697789Sahrens 2698789Sahrens /* 2699789Sahrens * Utility function to get the given numeric property. Does no validation that 2700789Sahrens * the given property is the appropriate type; should only be used with 2701789Sahrens * hard-coded property types. 2702789Sahrens */ 2703789Sahrens uint64_t 2704789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2705789Sahrens { 2706789Sahrens char *source; 27072082Seschrock uint64_t val; 27082082Seschrock 27095367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 27102082Seschrock 27112082Seschrock return (val); 2712789Sahrens } 2713789Sahrens 27145713Srm160521 int 27155713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 27165713Srm160521 { 27175713Srm160521 char buf[64]; 27185713Srm160521 27195713Srm160521 zfs_nicenum(val, buf, sizeof (buf)); 27205713Srm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 27215713Srm160521 } 27225713Srm160521 2723789Sahrens /* 2724789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2725789Sahrens */ 2726789Sahrens int 2727789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 27285094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2729789Sahrens { 2730789Sahrens char *source; 2731789Sahrens 2732789Sahrens /* 2733789Sahrens * Check to see if this property applies to our object 2734789Sahrens */ 27354849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 27363237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 27372082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 27382082Seschrock zfs_prop_to_name(prop))); 27394849Sahrens } 2740789Sahrens 2741789Sahrens if (src) 27425094Slling *src = ZPROP_SRC_NONE; 2743789Sahrens 27442082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 27452082Seschrock return (-1); 2746789Sahrens 2747789Sahrens get_source(zhp, src, source, statbuf, statlen); 2748789Sahrens 2749789Sahrens return (0); 2750789Sahrens } 2751789Sahrens 2752789Sahrens /* 2753789Sahrens * Returns the name of the given zfs handle. 2754789Sahrens */ 2755789Sahrens const char * 2756789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2757789Sahrens { 2758789Sahrens return (zhp->zfs_name); 2759789Sahrens } 2760789Sahrens 2761789Sahrens /* 2762789Sahrens * Returns the type of the given zfs handle. 2763789Sahrens */ 2764789Sahrens zfs_type_t 2765789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2766789Sahrens { 2767789Sahrens return (zhp->zfs_type); 2768789Sahrens } 2769789Sahrens 27708228SEric.Taylor@Sun.COM static int 27718228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc) 27728228SEric.Taylor@Sun.COM { 27738228SEric.Taylor@Sun.COM int rc; 27748228SEric.Taylor@Sun.COM uint64_t orig_cookie; 27758228SEric.Taylor@Sun.COM 27768228SEric.Taylor@Sun.COM orig_cookie = zc->zc_cookie; 27778228SEric.Taylor@Sun.COM top: 27788228SEric.Taylor@Sun.COM (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 27798228SEric.Taylor@Sun.COM rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc); 27808228SEric.Taylor@Sun.COM 27818228SEric.Taylor@Sun.COM if (rc == -1) { 27828228SEric.Taylor@Sun.COM switch (errno) { 27838228SEric.Taylor@Sun.COM case ENOMEM: 27848228SEric.Taylor@Sun.COM /* expand nvlist memory and try again */ 27858228SEric.Taylor@Sun.COM if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) { 27868228SEric.Taylor@Sun.COM zcmd_free_nvlists(zc); 27878228SEric.Taylor@Sun.COM return (-1); 27888228SEric.Taylor@Sun.COM } 27898228SEric.Taylor@Sun.COM zc->zc_cookie = orig_cookie; 27908228SEric.Taylor@Sun.COM goto top; 27918228SEric.Taylor@Sun.COM /* 27928228SEric.Taylor@Sun.COM * An errno value of ESRCH indicates normal completion. 27938228SEric.Taylor@Sun.COM * If ENOENT is returned, then the underlying dataset 27948228SEric.Taylor@Sun.COM * has been removed since we obtained the handle. 27958228SEric.Taylor@Sun.COM */ 27968228SEric.Taylor@Sun.COM case ESRCH: 27978228SEric.Taylor@Sun.COM case ENOENT: 27988228SEric.Taylor@Sun.COM rc = 1; 27998228SEric.Taylor@Sun.COM break; 28008228SEric.Taylor@Sun.COM default: 28018228SEric.Taylor@Sun.COM rc = zfs_standard_error(zhp->zfs_hdl, errno, 28028228SEric.Taylor@Sun.COM dgettext(TEXT_DOMAIN, 28038228SEric.Taylor@Sun.COM "cannot iterate filesystems")); 28048228SEric.Taylor@Sun.COM break; 28058228SEric.Taylor@Sun.COM } 28068228SEric.Taylor@Sun.COM } 28078228SEric.Taylor@Sun.COM return (rc); 28088228SEric.Taylor@Sun.COM } 28098228SEric.Taylor@Sun.COM 2810789Sahrens /* 28111356Seschrock * Iterate over all child filesystems 2812789Sahrens */ 2813789Sahrens int 28141356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2815789Sahrens { 2816789Sahrens zfs_cmd_t zc = { 0 }; 2817789Sahrens zfs_handle_t *nzhp; 2818789Sahrens int ret; 2819789Sahrens 28205367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 28215367Sahrens return (0); 28225367Sahrens 28238228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 28248228SEric.Taylor@Sun.COM return (-1); 28258228SEric.Taylor@Sun.COM 28268228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT, 28278228SEric.Taylor@Sun.COM &zc)) == 0) { 2828789Sahrens /* 2829789Sahrens * Ignore private dataset names. 2830789Sahrens */ 2831789Sahrens if (dataset_name_hidden(zc.zc_name)) 2832789Sahrens continue; 2833789Sahrens 2834789Sahrens /* 2835789Sahrens * Silently ignore errors, as the only plausible explanation is 2836789Sahrens * that the pool has since been removed. 2837789Sahrens */ 28388228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 28398228SEric.Taylor@Sun.COM &zc)) == NULL) { 2840789Sahrens continue; 28418228SEric.Taylor@Sun.COM } 28428228SEric.Taylor@Sun.COM 28438228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 28448228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2845789Sahrens return (ret); 28468228SEric.Taylor@Sun.COM } 2847789Sahrens } 28488228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 28498228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 28501356Seschrock } 28511356Seschrock 28521356Seschrock /* 28531356Seschrock * Iterate over all snapshots 28541356Seschrock */ 28551356Seschrock int 28561356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 28571356Seschrock { 28581356Seschrock zfs_cmd_t zc = { 0 }; 28591356Seschrock zfs_handle_t *nzhp; 28601356Seschrock int ret; 2861789Sahrens 28625367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 28635367Sahrens return (0); 28645367Sahrens 28658228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 28668228SEric.Taylor@Sun.COM return (-1); 28678228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, 28688228SEric.Taylor@Sun.COM &zc)) == 0) { 28698228SEric.Taylor@Sun.COM 28708228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 28718228SEric.Taylor@Sun.COM &zc)) == NULL) { 2872789Sahrens continue; 28738228SEric.Taylor@Sun.COM } 28748228SEric.Taylor@Sun.COM 28758228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 28768228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2877789Sahrens return (ret); 28788228SEric.Taylor@Sun.COM } 2879789Sahrens } 28808228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 28818228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 2882789Sahrens } 2883789Sahrens 2884789Sahrens /* 28851356Seschrock * Iterate over all children, snapshots and filesystems 28861356Seschrock */ 28871356Seschrock int 28881356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 28891356Seschrock { 28901356Seschrock int ret; 28911356Seschrock 28921356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 28931356Seschrock return (ret); 28941356Seschrock 28951356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 28961356Seschrock } 28971356Seschrock 28981356Seschrock /* 2899789Sahrens * Given a complete name, return just the portion that refers to the parent. 2900789Sahrens * Can return NULL if this is a pool. 2901789Sahrens */ 2902789Sahrens static int 2903789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2904789Sahrens { 2905789Sahrens char *loc; 2906789Sahrens 2907789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2908789Sahrens return (-1); 2909789Sahrens 2910789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2911789Sahrens buf[loc - path] = '\0'; 2912789Sahrens 2913789Sahrens return (0); 2914789Sahrens } 2915789Sahrens 2916789Sahrens /* 29174490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 29184490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 29194490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 29204490Svb160487 * length of already existing prefix of the given path. We also fetch the 29214490Svb160487 * 'zoned' property, which is used to validate property settings when creating 29224490Svb160487 * new datasets. 2923789Sahrens */ 2924789Sahrens static int 29254490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 29264490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2927789Sahrens { 2928789Sahrens zfs_cmd_t zc = { 0 }; 2929789Sahrens char parent[ZFS_MAXNAMELEN]; 2930789Sahrens char *slash; 29311356Seschrock zfs_handle_t *zhp; 29322082Seschrock char errbuf[1024]; 29332082Seschrock 29348269SMark.Musante@Sun.COM (void) snprintf(errbuf, sizeof (errbuf), 29358269SMark.Musante@Sun.COM dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 2936789Sahrens 2937789Sahrens /* get parent, and check to see if this is just a pool */ 2938789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 29392082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29402082Seschrock "missing dataset name")); 29412082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2942789Sahrens } 2943789Sahrens 2944789Sahrens /* check to see if the pool exists */ 2945789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2946789Sahrens slash = parent + strlen(parent); 2947789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2948789Sahrens zc.zc_name[slash - parent] = '\0'; 29492082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2950789Sahrens errno == ENOENT) { 29512082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29522082Seschrock "no such pool '%s'"), zc.zc_name); 29532082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2954789Sahrens } 2955789Sahrens 2956789Sahrens /* check to see if the parent dataset exists */ 29574490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 29584490Svb160487 if (errno == ENOENT && accept_ancestor) { 29594490Svb160487 /* 29604490Svb160487 * Go deeper to find an ancestor, give up on top level. 29614490Svb160487 */ 29624490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 29634490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29644490Svb160487 "no such pool '%s'"), zc.zc_name); 29654490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 29664490Svb160487 } 29674490Svb160487 } else if (errno == ENOENT) { 29682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29692082Seschrock "parent does not exist")); 29702082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 29714490Svb160487 } else 29722082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2973789Sahrens } 2974789Sahrens 29752676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2976789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 29772676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 29782082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 29791356Seschrock zfs_close(zhp); 2980789Sahrens return (-1); 2981789Sahrens } 2982789Sahrens 2983789Sahrens /* make sure parent is a filesystem */ 29841356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 29852082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29862082Seschrock "parent is not a filesystem")); 29872082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 29881356Seschrock zfs_close(zhp); 2989789Sahrens return (-1); 2990789Sahrens } 2991789Sahrens 29921356Seschrock zfs_close(zhp); 29934490Svb160487 if (prefixlen != NULL) 29944490Svb160487 *prefixlen = strlen(parent); 29954490Svb160487 return (0); 29964490Svb160487 } 29974490Svb160487 29984490Svb160487 /* 29994490Svb160487 * Finds whether the dataset of the given type(s) exists. 30004490Svb160487 */ 30014490Svb160487 boolean_t 30024490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 30034490Svb160487 { 30044490Svb160487 zfs_handle_t *zhp; 30054490Svb160487 30065326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 30074490Svb160487 return (B_FALSE); 30084490Svb160487 30094490Svb160487 /* 30104490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 30114490Svb160487 */ 30124490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 30134490Svb160487 int ds_type = zhp->zfs_type; 30144490Svb160487 30154490Svb160487 zfs_close(zhp); 30164490Svb160487 if (types & ds_type) 30174490Svb160487 return (B_TRUE); 30184490Svb160487 } 30194490Svb160487 return (B_FALSE); 30204490Svb160487 } 30214490Svb160487 30224490Svb160487 /* 30235367Sahrens * Given a path to 'target', create all the ancestors between 30245367Sahrens * the prefixlen portion of the path, and the target itself. 30255367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 30265367Sahrens */ 30275367Sahrens int 30285367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 30295367Sahrens { 30305367Sahrens zfs_handle_t *h; 30315367Sahrens char *cp; 30325367Sahrens const char *opname; 30335367Sahrens 30345367Sahrens /* make sure prefix exists */ 30355367Sahrens cp = target + prefixlen; 30365367Sahrens if (*cp != '/') { 30375367Sahrens assert(strchr(cp, '/') == NULL); 30385367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30395367Sahrens } else { 30405367Sahrens *cp = '\0'; 30415367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30425367Sahrens *cp = '/'; 30435367Sahrens } 30445367Sahrens if (h == NULL) 30455367Sahrens return (-1); 30465367Sahrens zfs_close(h); 30475367Sahrens 30485367Sahrens /* 30495367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 30505367Sahrens * up to the prefixlen-long one. 30515367Sahrens */ 30525367Sahrens for (cp = target + prefixlen + 1; 30535367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 30545367Sahrens char *logstr; 30555367Sahrens 30565367Sahrens *cp = '\0'; 30575367Sahrens 30585367Sahrens h = make_dataset_handle(hdl, target); 30595367Sahrens if (h) { 30605367Sahrens /* it already exists, nothing to do here */ 30615367Sahrens zfs_close(h); 30625367Sahrens continue; 30635367Sahrens } 30645367Sahrens 30655367Sahrens logstr = hdl->libzfs_log_str; 30665367Sahrens hdl->libzfs_log_str = NULL; 30675367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 30685367Sahrens NULL) != 0) { 30695367Sahrens hdl->libzfs_log_str = logstr; 30705367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 30715367Sahrens goto ancestorerr; 30725367Sahrens } 30735367Sahrens 30745367Sahrens hdl->libzfs_log_str = logstr; 30755367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30765367Sahrens if (h == NULL) { 30775367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 30785367Sahrens goto ancestorerr; 30795367Sahrens } 30805367Sahrens 30815367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 30825367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 30835367Sahrens goto ancestorerr; 30845367Sahrens } 30855367Sahrens 30865367Sahrens if (zfs_share(h) != 0) { 30875367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 30885367Sahrens goto ancestorerr; 30895367Sahrens } 30905367Sahrens 30915367Sahrens zfs_close(h); 30925367Sahrens } 30935367Sahrens 30945367Sahrens return (0); 30955367Sahrens 30965367Sahrens ancestorerr: 30975367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30985367Sahrens "failed to %s ancestor '%s'"), opname, target); 30995367Sahrens return (-1); 31005367Sahrens } 31015367Sahrens 31025367Sahrens /* 31034490Svb160487 * Creates non-existing ancestors of the given path. 31044490Svb160487 */ 31054490Svb160487 int 31064490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 31074490Svb160487 { 31084490Svb160487 int prefix; 31094490Svb160487 uint64_t zoned; 31104490Svb160487 char *path_copy; 31114490Svb160487 int rc; 31124490Svb160487 31134490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 31144490Svb160487 return (-1); 31154490Svb160487 31164490Svb160487 if ((path_copy = strdup(path)) != NULL) { 31174490Svb160487 rc = create_parents(hdl, path_copy, prefix); 31184490Svb160487 free(path_copy); 31194490Svb160487 } 31204490Svb160487 if (path_copy == NULL || rc != 0) 31214490Svb160487 return (-1); 31224490Svb160487 3123789Sahrens return (0); 3124789Sahrens } 3125789Sahrens 3126789Sahrens /* 31272676Seschrock * Create a new filesystem or volume. 3128789Sahrens */ 3129789Sahrens int 31302082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 31312676Seschrock nvlist_t *props) 3132789Sahrens { 3133789Sahrens zfs_cmd_t zc = { 0 }; 3134789Sahrens int ret; 3135789Sahrens uint64_t size = 0; 3136789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 31372082Seschrock char errbuf[1024]; 31382676Seschrock uint64_t zoned; 31392082Seschrock 31402082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31412082Seschrock "cannot create '%s'"), path); 3142789Sahrens 3143789Sahrens /* validate the path, taking care to note the extended error message */ 31445326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 31452082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3146789Sahrens 3147789Sahrens /* validate parents exist */ 31484490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3149789Sahrens return (-1); 3150789Sahrens 3151789Sahrens /* 3152789Sahrens * The failure modes when creating a dataset of a different type over 3153789Sahrens * one that already exists is a little strange. In particular, if you 3154789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 3155789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 3156789Sahrens * first try to see if the dataset exists. 3157789Sahrens */ 3158789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 31595094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 31602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31612082Seschrock "dataset already exists")); 31622082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3163789Sahrens } 3164789Sahrens 3165789Sahrens if (type == ZFS_TYPE_VOLUME) 3166789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3167789Sahrens else 3168789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3169789Sahrens 31707184Stimh if (props && (props = zfs_valid_proplist(hdl, type, props, 31713912Slling zoned, NULL, errbuf)) == 0) 31722676Seschrock return (-1); 31732676Seschrock 3174789Sahrens if (type == ZFS_TYPE_VOLUME) { 31751133Seschrock /* 31761133Seschrock * If we are creating a volume, the size and block size must 31771133Seschrock * satisfy a few restraints. First, the blocksize must be a 31781133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 31791133Seschrock * volsize must be a multiple of the block size, and cannot be 31801133Seschrock * zero. 31811133Seschrock */ 31822676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 31832676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 31842676Seschrock nvlist_free(props); 31852082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31862676Seschrock "missing volume size")); 31872676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3188789Sahrens } 3189789Sahrens 31902676Seschrock if ((ret = nvlist_lookup_uint64(props, 31912676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 31922676Seschrock &blocksize)) != 0) { 31932676Seschrock if (ret == ENOENT) { 31942676Seschrock blocksize = zfs_prop_default_numeric( 31952676Seschrock ZFS_PROP_VOLBLOCKSIZE); 31962676Seschrock } else { 31972676Seschrock nvlist_free(props); 31982676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31992676Seschrock "missing volume block size")); 32002676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 32012676Seschrock } 32022676Seschrock } 32032676Seschrock 32042676Seschrock if (size == 0) { 32052676Seschrock nvlist_free(props); 32062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32072676Seschrock "volume size cannot be zero")); 32082676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 32091133Seschrock } 32101133Seschrock 32111133Seschrock if (size % blocksize != 0) { 32122676Seschrock nvlist_free(props); 32132082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32142676Seschrock "volume size must be a multiple of volume block " 32152676Seschrock "size")); 32162676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 32171133Seschrock } 3218789Sahrens } 3219789Sahrens 32205094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 32212676Seschrock return (-1); 32222676Seschrock nvlist_free(props); 32232676Seschrock 3224789Sahrens /* create the dataset */ 32254543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 3226789Sahrens 32273912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 32282082Seschrock ret = zvol_create_link(hdl, path); 32293912Slling if (ret) { 32303912Slling (void) zfs_standard_error(hdl, errno, 32313912Slling dgettext(TEXT_DOMAIN, 32323912Slling "Volume successfully created, but device links " 32333912Slling "were not created")); 32343912Slling zcmd_free_nvlists(&zc); 32353912Slling return (-1); 32363912Slling } 32373912Slling } 3238789Sahrens 32392676Seschrock zcmd_free_nvlists(&zc); 32402676Seschrock 3241789Sahrens /* check for failure */ 3242789Sahrens if (ret != 0) { 3243789Sahrens char parent[ZFS_MAXNAMELEN]; 3244789Sahrens (void) parent_name(path, parent, sizeof (parent)); 3245789Sahrens 3246789Sahrens switch (errno) { 3247789Sahrens case ENOENT: 32482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32492082Seschrock "no such parent '%s'"), parent); 32502082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3251789Sahrens 3252789Sahrens case EINVAL: 32532082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32543413Smmusante "parent '%s' is not a filesystem"), parent); 32552082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3256789Sahrens 3257789Sahrens case EDOM: 32582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32592676Seschrock "volume block size must be power of 2 from " 32602676Seschrock "%u to %uk"), 3261789Sahrens (uint_t)SPA_MINBLOCKSIZE, 3262789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 32632082Seschrock 32642676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 32652082Seschrock 32664603Sahrens case ENOTSUP: 32674603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32684603Sahrens "pool must be upgraded to set this " 32694603Sahrens "property or value")); 32704603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3271789Sahrens #ifdef _ILP32 3272789Sahrens case EOVERFLOW: 3273789Sahrens /* 3274789Sahrens * This platform can't address a volume this big. 3275789Sahrens */ 32762082Seschrock if (type == ZFS_TYPE_VOLUME) 32772082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 32782082Seschrock errbuf)); 3279789Sahrens #endif 32802082Seschrock /* FALLTHROUGH */ 3281789Sahrens default: 32822082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3283789Sahrens } 3284789Sahrens } 3285789Sahrens 3286789Sahrens return (0); 3287789Sahrens } 3288789Sahrens 3289789Sahrens /* 3290789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 3291789Sahrens * isn't mounted, and that there are no active dependents. 3292789Sahrens */ 3293789Sahrens int 3294789Sahrens zfs_destroy(zfs_handle_t *zhp) 3295789Sahrens { 3296789Sahrens zfs_cmd_t zc = { 0 }; 3297789Sahrens 3298789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3299789Sahrens 33002676Seschrock if (ZFS_IS_VOLUME(zhp)) { 33013126Sahl /* 33024543Smarks * If user doesn't have permissions to unshare volume, then 33034543Smarks * abort the request. This would only happen for a 33044543Smarks * non-privileged user. 33053126Sahl */ 33064543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 33074543Smarks return (-1); 33084543Smarks } 33093126Sahl 33102082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3311789Sahrens return (-1); 3312789Sahrens 3313789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3314789Sahrens } else { 3315789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3316789Sahrens } 3317789Sahrens 33184543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 33193237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 33202082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 33212082Seschrock zhp->zfs_name)); 33222199Sahrens } 3323789Sahrens 3324789Sahrens remove_mountpoint(zhp); 3325789Sahrens 3326789Sahrens return (0); 3327789Sahrens } 3328789Sahrens 33292199Sahrens struct destroydata { 33302199Sahrens char *snapname; 33312199Sahrens boolean_t gotone; 33323265Sahrens boolean_t closezhp; 33332199Sahrens }; 33342199Sahrens 33352199Sahrens static int 33362199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 33372199Sahrens { 33382199Sahrens struct destroydata *dd = arg; 33392199Sahrens zfs_handle_t *szhp; 33402199Sahrens char name[ZFS_MAXNAMELEN]; 33413265Sahrens boolean_t closezhp = dd->closezhp; 33423265Sahrens int rv; 33432199Sahrens 33442676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 33452676Seschrock (void) strlcat(name, "@", sizeof (name)); 33462676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 33472199Sahrens 33482199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 33492199Sahrens if (szhp) { 33502199Sahrens dd->gotone = B_TRUE; 33512199Sahrens zfs_close(szhp); 33522199Sahrens } 33532199Sahrens 33542199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33552199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 33562199Sahrens /* 33572199Sahrens * NB: this is simply a best-effort. We don't want to 33582199Sahrens * return an error, because then we wouldn't visit all 33592199Sahrens * the volumes. 33602199Sahrens */ 33612199Sahrens } 33622199Sahrens 33633265Sahrens dd->closezhp = B_TRUE; 33643265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 33653265Sahrens if (closezhp) 33663265Sahrens zfs_close(zhp); 33673265Sahrens return (rv); 33682199Sahrens } 33692199Sahrens 33702199Sahrens /* 33712199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 33722199Sahrens */ 33732199Sahrens int 33742199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 33752199Sahrens { 33762199Sahrens zfs_cmd_t zc = { 0 }; 33772199Sahrens int ret; 33782199Sahrens struct destroydata dd = { 0 }; 33792199Sahrens 33802199Sahrens dd.snapname = snapname; 33812199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 33822199Sahrens 33832199Sahrens if (!dd.gotone) { 33843237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 33852199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 33862199Sahrens zhp->zfs_name, snapname)); 33872199Sahrens } 33882199Sahrens 33892199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33902676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 33912199Sahrens 33924543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 33932199Sahrens if (ret != 0) { 33942199Sahrens char errbuf[1024]; 33952199Sahrens 33962199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33972199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 33982199Sahrens 33992199Sahrens switch (errno) { 34002199Sahrens case EEXIST: 34012199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 34022199Sahrens "snapshot is cloned")); 34032199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 34042199Sahrens 34052199Sahrens default: 34062199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 34072199Sahrens errbuf)); 34082199Sahrens } 34092199Sahrens } 34102199Sahrens 34112199Sahrens return (0); 34122199Sahrens } 34132199Sahrens 3414789Sahrens /* 3415789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3416789Sahrens */ 3417789Sahrens int 34182676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3419789Sahrens { 3420789Sahrens zfs_cmd_t zc = { 0 }; 3421789Sahrens char parent[ZFS_MAXNAMELEN]; 3422789Sahrens int ret; 34232082Seschrock char errbuf[1024]; 34242082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 34252676Seschrock zfs_type_t type; 34262676Seschrock uint64_t zoned; 3427789Sahrens 3428789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3429789Sahrens 34302082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34312082Seschrock "cannot create '%s'"), target); 34322082Seschrock 3433789Sahrens /* validate the target name */ 34345326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 34352082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3436789Sahrens 3437789Sahrens /* validate parents exist */ 34384490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3439789Sahrens return (-1); 3440789Sahrens 3441789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3442789Sahrens 3443789Sahrens /* do the clone */ 34442676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3445789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 34462744Snn35248 type = ZFS_TYPE_VOLUME; 34472676Seschrock } else { 3448789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 34492744Snn35248 type = ZFS_TYPE_FILESYSTEM; 34502676Seschrock } 34512676Seschrock 34522676Seschrock if (props) { 34537184Stimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 34547184Stimh zhp, errbuf)) == NULL) 34552676Seschrock return (-1); 34562676Seschrock 34575094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 34582676Seschrock nvlist_free(props); 34592676Seschrock return (-1); 34602676Seschrock } 34612676Seschrock 34622676Seschrock nvlist_free(props); 34632676Seschrock } 3464789Sahrens 3465789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 34662676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 34674543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3468789Sahrens 34692676Seschrock zcmd_free_nvlists(&zc); 34702676Seschrock 3471789Sahrens if (ret != 0) { 3472789Sahrens switch (errno) { 3473789Sahrens 3474789Sahrens case ENOENT: 3475789Sahrens /* 3476789Sahrens * The parent doesn't exist. We should have caught this 3477789Sahrens * above, but there may a race condition that has since 3478789Sahrens * destroyed the parent. 3479789Sahrens * 3480789Sahrens * At this point, we don't know whether it's the source 3481789Sahrens * that doesn't exist anymore, or whether the target 3482789Sahrens * dataset doesn't exist. 3483789Sahrens */ 34842082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 34852082Seschrock "no such parent '%s'"), parent); 34862082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 34872082Seschrock 34882082Seschrock case EXDEV: 34892082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 34902082Seschrock "source and target pools differ")); 34912082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 34922082Seschrock errbuf)); 34932082Seschrock 34942082Seschrock default: 34952082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 34962082Seschrock errbuf)); 34972082Seschrock } 34982676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 34992082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 35002082Seschrock } 35012082Seschrock 35022082Seschrock return (ret); 35032082Seschrock } 35042082Seschrock 35052082Seschrock typedef struct promote_data { 35062082Seschrock char cb_mountpoint[MAXPATHLEN]; 35072082Seschrock const char *cb_target; 35082082Seschrock const char *cb_errbuf; 35092082Seschrock uint64_t cb_pivot_txg; 35102082Seschrock } promote_data_t; 35112082Seschrock 35122082Seschrock static int 35132082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 35142082Seschrock { 35152082Seschrock promote_data_t *pd = data; 35162082Seschrock zfs_handle_t *szhp; 35172082Seschrock char snapname[MAXPATHLEN]; 35183265Sahrens int rv = 0; 35192082Seschrock 35202082Seschrock /* We don't care about snapshots after the pivot point */ 35213265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 35223265Sahrens zfs_close(zhp); 35232082Seschrock return (0); 35243265Sahrens } 35252082Seschrock 35262417Sahrens /* Remove the device link if it's a zvol. */ 35272676Seschrock if (ZFS_IS_VOLUME(zhp)) 35282417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 35292082Seschrock 35302082Seschrock /* Check for conflicting names */ 35312676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 35322676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 35332082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 35342082Seschrock if (szhp != NULL) { 35352082Seschrock zfs_close(szhp); 35362082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 35372082Seschrock "snapshot name '%s' from origin \n" 35382082Seschrock "conflicts with '%s' from target"), 35392082Seschrock zhp->zfs_name, snapname); 35403265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 35412082Seschrock } 35423265Sahrens zfs_close(zhp); 35433265Sahrens return (rv); 35442082Seschrock } 35452082Seschrock 35462417Sahrens static int 35472417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 35482417Sahrens { 35492417Sahrens promote_data_t *pd = data; 35502417Sahrens 35512417Sahrens /* We don't care about snapshots after the pivot point */ 35523265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 35533265Sahrens /* Create the device link if it's a zvol. */ 35543265Sahrens if (ZFS_IS_VOLUME(zhp)) 35553265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 35563265Sahrens } 35573265Sahrens 35583265Sahrens zfs_close(zhp); 35592417Sahrens return (0); 35602417Sahrens } 35612417Sahrens 35622082Seschrock /* 35632082Seschrock * Promotes the given clone fs to be the clone parent. 35642082Seschrock */ 35652082Seschrock int 35662082Seschrock zfs_promote(zfs_handle_t *zhp) 35672082Seschrock { 35682082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 35692082Seschrock zfs_cmd_t zc = { 0 }; 35702082Seschrock char parent[MAXPATHLEN]; 35712082Seschrock char *cp; 35722082Seschrock int ret; 35732082Seschrock zfs_handle_t *pzhp; 35742082Seschrock promote_data_t pd; 35752082Seschrock char errbuf[1024]; 35762082Seschrock 35772082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35782082Seschrock "cannot promote '%s'"), zhp->zfs_name); 35792082Seschrock 35802082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 35812082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35822082Seschrock "snapshots can not be promoted")); 35832082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 35842082Seschrock } 35852082Seschrock 35865367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 35872082Seschrock if (parent[0] == '\0') { 35882082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35892082Seschrock "not a cloned filesystem")); 35902082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 35912082Seschrock } 35922082Seschrock cp = strchr(parent, '@'); 35932082Seschrock *cp = '\0'; 35942082Seschrock 35952082Seschrock /* Walk the snapshots we will be moving */ 35965367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 35972082Seschrock if (pzhp == NULL) 35982082Seschrock return (-1); 35992082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 36002082Seschrock zfs_close(pzhp); 36012082Seschrock pd.cb_target = zhp->zfs_name; 36022082Seschrock pd.cb_errbuf = errbuf; 36035094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 36042082Seschrock if (pzhp == NULL) 36052082Seschrock return (-1); 36062082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 36072082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 36082082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 36092417Sahrens if (ret != 0) { 36102417Sahrens zfs_close(pzhp); 36112082Seschrock return (-1); 36122417Sahrens } 36132082Seschrock 36142082Seschrock /* issue the ioctl */ 36155367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 36162676Seschrock sizeof (zc.zc_value)); 36172082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 36184543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 36192082Seschrock 36202082Seschrock if (ret != 0) { 36212417Sahrens int save_errno = errno; 36222417Sahrens 36232417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 36242417Sahrens zfs_close(pzhp); 36252417Sahrens 36262417Sahrens switch (save_errno) { 3627789Sahrens case EEXIST: 3628789Sahrens /* 36292082Seschrock * There is a conflicting snapshot name. We 36302082Seschrock * should have caught this above, but they could 36312082Seschrock * have renamed something in the mean time. 3632789Sahrens */ 36332082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36342082Seschrock "conflicting snapshot name from parent '%s'"), 36352082Seschrock parent); 36362082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3637789Sahrens 3638789Sahrens default: 36392417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3640789Sahrens } 36412417Sahrens } else { 36422417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3643789Sahrens } 3644789Sahrens 36452417Sahrens zfs_close(pzhp); 3646789Sahrens return (ret); 3647789Sahrens } 3648789Sahrens 36494007Smmusante struct createdata { 36504007Smmusante const char *cd_snapname; 36514007Smmusante int cd_ifexists; 36524007Smmusante }; 36534007Smmusante 36542199Sahrens static int 36552199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 36562199Sahrens { 36574007Smmusante struct createdata *cd = arg; 36582676Seschrock int ret; 36592199Sahrens 36602199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 36612199Sahrens char name[MAXPATHLEN]; 36622199Sahrens 36632676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 36642676Seschrock (void) strlcat(name, "@", sizeof (name)); 36654007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 36664007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 36674007Smmusante cd->cd_ifexists); 36682199Sahrens /* 36692199Sahrens * NB: this is simply a best-effort. We don't want to 36702199Sahrens * return an error, because then we wouldn't visit all 36712199Sahrens * the volumes. 36722199Sahrens */ 36732199Sahrens } 36742676Seschrock 36754007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 36762676Seschrock 36772676Seschrock zfs_close(zhp); 36782676Seschrock 36792676Seschrock return (ret); 36802199Sahrens } 36812199Sahrens 3682789Sahrens /* 36833504Sahl * Takes a snapshot of the given dataset. 3684789Sahrens */ 3685789Sahrens int 36867265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 36877265Sahrens nvlist_t *props) 3688789Sahrens { 3689789Sahrens const char *delim; 36907265Sahrens char parent[ZFS_MAXNAMELEN]; 3691789Sahrens zfs_handle_t *zhp; 3692789Sahrens zfs_cmd_t zc = { 0 }; 3693789Sahrens int ret; 36942082Seschrock char errbuf[1024]; 36952082Seschrock 36962082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36972082Seschrock "cannot snapshot '%s'"), path); 36982082Seschrock 36992082Seschrock /* validate the target name */ 37005326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 37012082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3702789Sahrens 37037265Sahrens if (props) { 37047265Sahrens if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 37057265Sahrens props, B_FALSE, NULL, errbuf)) == NULL) 37067265Sahrens return (-1); 37077265Sahrens 37087265Sahrens if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 37097265Sahrens nvlist_free(props); 37107265Sahrens return (-1); 37117265Sahrens } 37127265Sahrens 37137265Sahrens nvlist_free(props); 37147265Sahrens } 37157265Sahrens 3716789Sahrens /* make sure the parent exists and is of the appropriate type */ 37172199Sahrens delim = strchr(path, '@'); 3718789Sahrens (void) strncpy(parent, path, delim - path); 3719789Sahrens parent[delim - path] = '\0'; 3720789Sahrens 37212082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3722789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 37237265Sahrens zcmd_free_nvlists(&zc); 3724789Sahrens return (-1); 3725789Sahrens } 3726789Sahrens 37272199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 37282676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 37294543Smarks if (ZFS_IS_VOLUME(zhp)) 37304543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 37314543Smarks else 37324543Smarks zc.zc_objset_type = DMU_OST_ZFS; 37332199Sahrens zc.zc_cookie = recursive; 37344543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 37352199Sahrens 37367265Sahrens zcmd_free_nvlists(&zc); 37377265Sahrens 37382199Sahrens /* 37392199Sahrens * if it was recursive, the one that actually failed will be in 37402199Sahrens * zc.zc_name. 37412199Sahrens */ 37424543Smarks if (ret != 0) 37434543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37444543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 37454543Smarks 37462199Sahrens if (ret == 0 && recursive) { 37474007Smmusante struct createdata cd; 37484007Smmusante 37494007Smmusante cd.cd_snapname = delim + 1; 37504007Smmusante cd.cd_ifexists = B_FALSE; 37514007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 37522199Sahrens } 3753789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 37542082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 37552199Sahrens if (ret != 0) { 37564543Smarks (void) zfs_standard_error(hdl, errno, 37574543Smarks dgettext(TEXT_DOMAIN, 37584543Smarks "Volume successfully snapshotted, but device links " 37594543Smarks "were not created")); 37604543Smarks zfs_close(zhp); 37614543Smarks return (-1); 37622199Sahrens } 3763789Sahrens } 3764789Sahrens 37652082Seschrock if (ret != 0) 37662082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3767789Sahrens 3768789Sahrens zfs_close(zhp); 3769789Sahrens 3770789Sahrens return (ret); 3771789Sahrens } 3772789Sahrens 3773789Sahrens /* 37741294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 37751294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 37761294Slling * is a dependent and we should just destroy it without checking the transaction 37771294Slling * group. 3778789Sahrens */ 37791294Slling typedef struct rollback_data { 37801294Slling const char *cb_target; /* the snapshot */ 37811294Slling uint64_t cb_create; /* creation time reference */ 37825749Sahrens boolean_t cb_error; 37832082Seschrock boolean_t cb_dependent; 37845749Sahrens boolean_t cb_force; 37851294Slling } rollback_data_t; 37861294Slling 37871294Slling static int 37881294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 37891294Slling { 37901294Slling rollback_data_t *cbp = data; 37911294Slling 37921294Slling if (!cbp->cb_dependent) { 37931294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 37941294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 37951294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 37961294Slling cbp->cb_create) { 37974543Smarks char *logstr; 37981294Slling 37992082Seschrock cbp->cb_dependent = B_TRUE; 38005446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 38015446Sahrens rollback_destroy, cbp); 38022082Seschrock cbp->cb_dependent = B_FALSE; 38031294Slling 38044543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 38054543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 38065446Sahrens cbp->cb_error |= zfs_destroy(zhp); 38074543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 38081294Slling } 38091294Slling } else { 38105749Sahrens /* We must destroy this clone; first unmount it */ 38115749Sahrens prop_changelist_t *clp; 38125749Sahrens 38137366STim.Haley@Sun.COM clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 38145749Sahrens cbp->cb_force ? MS_FORCE: 0); 38155749Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 38165749Sahrens cbp->cb_error = B_TRUE; 38175749Sahrens zfs_close(zhp); 38185749Sahrens return (0); 38195749Sahrens } 38205749Sahrens if (zfs_destroy(zhp) != 0) 38215749Sahrens cbp->cb_error = B_TRUE; 38225749Sahrens else 38235749Sahrens changelist_remove(clp, zhp->zfs_name); 38245751Sahrens (void) changelist_postfix(clp); 38255749Sahrens changelist_free(clp); 38261294Slling } 38271294Slling 38281294Slling zfs_close(zhp); 38291294Slling return (0); 38301294Slling } 38311294Slling 38321294Slling /* 38335446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 38345446Sahrens * data changes since then and making it the active dataset. 38355446Sahrens * 38365446Sahrens * Any snapshots more recent than the target are destroyed, along with 38375446Sahrens * their dependents. 38381294Slling */ 38395446Sahrens int 38405749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3841789Sahrens { 38425446Sahrens rollback_data_t cb = { 0 }; 38435446Sahrens int err; 3844789Sahrens zfs_cmd_t zc = { 0 }; 38455713Srm160521 boolean_t restore_resv = 0; 38465713Srm160521 uint64_t old_volsize, new_volsize; 38475713Srm160521 zfs_prop_t resv_prop; 3848789Sahrens 3849789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3850789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3851789Sahrens 38525446Sahrens /* 38535446Sahrens * Destroy all recent snapshots and its dependends. 38545446Sahrens */ 38555749Sahrens cb.cb_force = force; 38565446Sahrens cb.cb_target = snap->zfs_name; 38575446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 38585446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 38595446Sahrens 38605749Sahrens if (cb.cb_error) 38615749Sahrens return (-1); 38625446Sahrens 38635446Sahrens /* 38645446Sahrens * Now that we have verified that the snapshot is the latest, 38655446Sahrens * rollback to the given snapshot. 38665446Sahrens */ 38675446Sahrens 38685713Srm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 38695713Srm160521 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 38705713Srm160521 return (-1); 38715713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 38725713Srm160521 return (-1); 38735713Srm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 38745713Srm160521 restore_resv = 38755713Srm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 38765713Srm160521 } 3877789Sahrens 3878789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3879789Sahrens 38802676Seschrock if (ZFS_IS_VOLUME(zhp)) 3881789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3882789Sahrens else 3883789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3884789Sahrens 3885789Sahrens /* 38865446Sahrens * We rely on zfs_iter_children() to verify that there are no 38875446Sahrens * newer snapshots for the given dataset. Therefore, we can 38885446Sahrens * simply pass the name on to the ioctl() call. There is still 38895446Sahrens * an unlikely race condition where the user has taken a 38905446Sahrens * snapshot since we verified that this was the most recent. 38915713Srm160521 * 3892789Sahrens */ 38935446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 38943237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 38952082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 38962082Seschrock zhp->zfs_name); 38975717Srm160521 return (err); 38985717Srm160521 } 38995713Srm160521 39005713Srm160521 /* 39015713Srm160521 * For volumes, if the pre-rollback volsize matched the pre- 39025713Srm160521 * rollback reservation and the volsize has changed then set 39035713Srm160521 * the reservation property to the post-rollback volsize. 39045713Srm160521 * Make a new handle since the rollback closed the dataset. 39055713Srm160521 */ 39065717Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 39075717Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 39085717Srm160521 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) { 39095717Srm160521 zfs_close(zhp); 39105713Srm160521 return (err); 39115717Srm160521 } 39125713Srm160521 if (restore_resv) { 39135713Srm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 39145713Srm160521 if (old_volsize != new_volsize) 39155717Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 39165717Srm160521 new_volsize); 39175713Srm160521 } 39185713Srm160521 zfs_close(zhp); 3919789Sahrens } 39205446Sahrens return (err); 39211294Slling } 39221294Slling 39231294Slling /* 3924789Sahrens * Iterate over all dependents for a given dataset. This includes both 3925789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3926789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3927789Sahrens * libzfs_graph.c. 3928789Sahrens */ 3929789Sahrens int 39302474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 39312474Seschrock zfs_iter_f func, void *data) 3932789Sahrens { 3933789Sahrens char **dependents; 3934789Sahrens size_t count; 3935789Sahrens int i; 3936789Sahrens zfs_handle_t *child; 3937789Sahrens int ret = 0; 3938789Sahrens 39392474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 39402474Seschrock &dependents, &count) != 0) 39412474Seschrock return (-1); 39422474Seschrock 3943789Sahrens for (i = 0; i < count; i++) { 39442082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 39452082Seschrock dependents[i])) == NULL) 3946789Sahrens continue; 3947789Sahrens 3948789Sahrens if ((ret = func(child, data)) != 0) 3949789Sahrens break; 3950789Sahrens } 3951789Sahrens 3952789Sahrens for (i = 0; i < count; i++) 3953789Sahrens free(dependents[i]); 3954789Sahrens free(dependents); 3955789Sahrens 3956789Sahrens return (ret); 3957789Sahrens } 3958789Sahrens 3959789Sahrens /* 3960789Sahrens * Renames the given dataset. 3961789Sahrens */ 3962789Sahrens int 39634490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3964789Sahrens { 3965789Sahrens int ret; 3966789Sahrens zfs_cmd_t zc = { 0 }; 3967789Sahrens char *delim; 39684007Smmusante prop_changelist_t *cl = NULL; 39694007Smmusante zfs_handle_t *zhrp = NULL; 39704007Smmusante char *parentname = NULL; 3971789Sahrens char parent[ZFS_MAXNAMELEN]; 39722082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 39732082Seschrock char errbuf[1024]; 3974789Sahrens 3975789Sahrens /* if we have the same exact name, just return success */ 3976789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3977789Sahrens return (0); 3978789Sahrens 39792082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 39802082Seschrock "cannot rename to '%s'"), target); 39812082Seschrock 3982789Sahrens /* 3983789Sahrens * Make sure the target name is valid 3984789Sahrens */ 3985789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 39862665Snd150628 if ((strchr(target, '@') == NULL) || 39872665Snd150628 *target == '@') { 39882665Snd150628 /* 39892665Snd150628 * Snapshot target name is abbreviated, 39902665Snd150628 * reconstruct full dataset name 39912665Snd150628 */ 39922665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 39932665Snd150628 sizeof (parent)); 39942665Snd150628 delim = strchr(parent, '@'); 39952665Snd150628 if (strchr(target, '@') == NULL) 39962665Snd150628 *(++delim) = '\0'; 39972665Snd150628 else 39982665Snd150628 *delim = '\0'; 39992665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 40002665Snd150628 target = parent; 40012665Snd150628 } else { 40022665Snd150628 /* 40032665Snd150628 * Make sure we're renaming within the same dataset. 40042665Snd150628 */ 40052665Snd150628 delim = strchr(target, '@'); 40062665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 40072665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 40082665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40092665Snd150628 "snapshots must be part of same " 40102665Snd150628 "dataset")); 40112665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 40123912Slling errbuf)); 40132665Snd150628 } 4014789Sahrens } 40155326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 40162665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 4017789Sahrens } else { 40184007Smmusante if (recursive) { 40194007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40204007Smmusante "recursive rename must be a snapshot")); 40214007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 40224007Smmusante } 40234007Smmusante 40245326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 40252665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40262676Seschrock uint64_t unused; 40272676Seschrock 4028789Sahrens /* validate parents */ 40294490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4030789Sahrens return (-1); 4031789Sahrens 4032789Sahrens (void) parent_name(target, parent, sizeof (parent)); 4033789Sahrens 4034789Sahrens /* make sure we're in the same pool */ 4035789Sahrens verify((delim = strchr(target, '/')) != NULL); 4036789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4037789Sahrens zhp->zfs_name[delim - target] != '/') { 40382082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40392082Seschrock "datasets must be within same pool")); 40402082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4041789Sahrens } 40422440Snd150628 40432440Snd150628 /* new name cannot be a child of the current dataset name */ 40442440Snd150628 if (strncmp(parent, zhp->zfs_name, 40453912Slling strlen(zhp->zfs_name)) == 0) { 40462440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40472440Snd150628 "New dataset name cannot be a descendent of " 40482440Snd150628 "current dataset name")); 40492440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40502440Snd150628 } 4051789Sahrens } 4052789Sahrens 40532082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 40542082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 40552082Seschrock 4056789Sahrens if (getzoneid() == GLOBAL_ZONEID && 4057789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 40582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40592082Seschrock "dataset is used in a non-global zone")); 40602082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4061789Sahrens } 4062789Sahrens 40634007Smmusante if (recursive) { 40644007Smmusante struct destroydata dd; 40654007Smmusante 40664183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 40674183Smmusante if (parentname == NULL) { 40684183Smmusante ret = -1; 40694183Smmusante goto error; 40704183Smmusante } 40714007Smmusante delim = strchr(parentname, '@'); 40724007Smmusante *delim = '\0'; 40735094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 40744007Smmusante if (zhrp == NULL) { 40754183Smmusante ret = -1; 40764183Smmusante goto error; 40774007Smmusante } 40784007Smmusante 40794007Smmusante dd.snapname = delim + 1; 40804007Smmusante dd.gotone = B_FALSE; 40814183Smmusante dd.closezhp = B_TRUE; 40824007Smmusante 40834007Smmusante /* We remove any zvol links prior to renaming them */ 40844007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 40854007Smmusante if (ret) { 40864007Smmusante goto error; 40874007Smmusante } 40884007Smmusante } else { 40897366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL) 40904007Smmusante return (-1); 40914007Smmusante 40924007Smmusante if (changelist_haszonedchild(cl)) { 40934007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40944007Smmusante "child dataset with inherited mountpoint is used " 40954007Smmusante "in a non-global zone")); 40964007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 40974007Smmusante goto error; 40984007Smmusante } 40994007Smmusante 41004007Smmusante if ((ret = changelist_prefix(cl)) != 0) 41014007Smmusante goto error; 4102789Sahrens } 4103789Sahrens 41042676Seschrock if (ZFS_IS_VOLUME(zhp)) 4105789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4106789Sahrens else 4107789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4108789Sahrens 41092665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 41102676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 41112665Snd150628 41124007Smmusante zc.zc_cookie = recursive; 41134007Smmusante 41144543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 41154007Smmusante /* 41164007Smmusante * if it was recursive, the one that actually failed will 41174007Smmusante * be in zc.zc_name 41184007Smmusante */ 41194007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 41205367Sahrens "cannot rename '%s'"), zc.zc_name); 41214007Smmusante 41224007Smmusante if (recursive && errno == EEXIST) { 41234007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 41244007Smmusante "a child dataset already has a snapshot " 41254007Smmusante "with the new name")); 41264801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 41274007Smmusante } else { 41284007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 41294007Smmusante } 4130789Sahrens 4131789Sahrens /* 4132789Sahrens * On failure, we still want to remount any filesystems that 4133789Sahrens * were previously mounted, so we don't alter the system state. 4134789Sahrens */ 41354007Smmusante if (recursive) { 41364007Smmusante struct createdata cd; 41374007Smmusante 41384007Smmusante /* only create links for datasets that had existed */ 41394007Smmusante cd.cd_snapname = delim + 1; 41404007Smmusante cd.cd_ifexists = B_TRUE; 41414007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41424007Smmusante &cd); 41434007Smmusante } else { 41444007Smmusante (void) changelist_postfix(cl); 41454007Smmusante } 4146789Sahrens } else { 41474007Smmusante if (recursive) { 41484007Smmusante struct createdata cd; 41494007Smmusante 41504007Smmusante /* only create links for datasets that had existed */ 41514007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 41524007Smmusante cd.cd_ifexists = B_TRUE; 41534007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41544007Smmusante &cd); 41554007Smmusante } else { 41564007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 41574007Smmusante ret = changelist_postfix(cl); 41584007Smmusante } 4159789Sahrens } 4160789Sahrens 4161789Sahrens error: 41624007Smmusante if (parentname) { 41634007Smmusante free(parentname); 41644007Smmusante } 41654007Smmusante if (zhrp) { 41664007Smmusante zfs_close(zhrp); 41674007Smmusante } 41684007Smmusante if (cl) { 41694007Smmusante changelist_free(cl); 41704007Smmusante } 4171789Sahrens return (ret); 4172789Sahrens } 4173789Sahrens 4174789Sahrens /* 4175789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4176789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 4177789Sahrens */ 4178789Sahrens int 41792082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4180789Sahrens { 41814007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 41824007Smmusante } 41834007Smmusante 41844007Smmusante static int 41854007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 41864007Smmusante { 4187789Sahrens zfs_cmd_t zc = { 0 }; 41882082Seschrock di_devlink_handle_t dhdl; 41894543Smarks priv_set_t *priv_effective; 41904543Smarks int privileged; 4191789Sahrens 4192789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4193789Sahrens 4194789Sahrens /* 4195789Sahrens * Issue the appropriate ioctl. 4196789Sahrens */ 41972082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4198789Sahrens switch (errno) { 4199789Sahrens case EEXIST: 4200789Sahrens /* 4201789Sahrens * Silently ignore the case where the link already 4202789Sahrens * exists. This allows 'zfs volinit' to be run multiple 4203789Sahrens * times without errors. 4204789Sahrens */ 4205789Sahrens return (0); 4206789Sahrens 42074007Smmusante case ENOENT: 42084007Smmusante /* 42094007Smmusante * Dataset does not exist in the kernel. If we 42104007Smmusante * don't care (see zfs_rename), then ignore the 42114007Smmusante * error quietly. 42124007Smmusante */ 42134007Smmusante if (ifexists) { 42144007Smmusante return (0); 42154007Smmusante } 42164007Smmusante 42174007Smmusante /* FALLTHROUGH */ 42184007Smmusante 4219789Sahrens default: 42203237Slling return (zfs_standard_error_fmt(hdl, errno, 42212082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 42222082Seschrock "for '%s'"), dataset)); 4223789Sahrens } 4224789Sahrens } 4225789Sahrens 4226789Sahrens /* 42274543Smarks * If privileged call devfsadm and wait for the links to 42284543Smarks * magically appear. 42294543Smarks * Otherwise, print out an informational message. 4230789Sahrens */ 42314543Smarks 42324543Smarks priv_effective = priv_allocset(); 42334543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 42344543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 42354543Smarks priv_freeset(priv_effective); 42364543Smarks 42374543Smarks if (privileged) { 42384543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 42394543Smarks DI_MAKE_LINK)) == NULL) { 42404543Smarks zfs_error_aux(hdl, strerror(errno)); 42417301SEric.Taylor@Sun.COM (void) zfs_error_fmt(hdl, errno, 42424543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 42434543Smarks "for '%s'"), dataset); 42444543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 42454543Smarks return (-1); 42464543Smarks } else { 42474543Smarks (void) di_devlink_fini(&dhdl); 42484543Smarks } 4249789Sahrens } else { 42504543Smarks char pathname[MAXPATHLEN]; 42514543Smarks struct stat64 statbuf; 42524543Smarks int i; 42534543Smarks 42544543Smarks #define MAX_WAIT 10 42554543Smarks 42564543Smarks /* 42574543Smarks * This is the poor mans way of waiting for the link 42584543Smarks * to show up. If after 10 seconds we still don't 42594543Smarks * have it, then print out a message. 42604543Smarks */ 42614543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 42624543Smarks dataset); 42634543Smarks 42644543Smarks for (i = 0; i != MAX_WAIT; i++) { 42654543Smarks if (stat64(pathname, &statbuf) == 0) 42664543Smarks break; 42674543Smarks (void) sleep(1); 42684543Smarks } 42694543Smarks if (i == MAX_WAIT) 42704543Smarks (void) printf(gettext("%s may not be immediately " 42714543Smarks "available\n"), pathname); 4272789Sahrens } 4273789Sahrens 4274789Sahrens return (0); 4275789Sahrens } 4276789Sahrens 4277789Sahrens /* 4278789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4279789Sahrens */ 4280789Sahrens int 42812082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4282789Sahrens { 4283789Sahrens zfs_cmd_t zc = { 0 }; 4284789Sahrens 4285789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4286789Sahrens 42872082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4288789Sahrens switch (errno) { 4289789Sahrens case ENXIO: 4290789Sahrens /* 4291789Sahrens * Silently ignore the case where the link no longer 4292789Sahrens * exists, so that 'zfs volfini' can be run multiple 4293789Sahrens * times without errors. 4294789Sahrens */ 4295789Sahrens return (0); 4296789Sahrens 4297789Sahrens default: 42983237Slling return (zfs_standard_error_fmt(hdl, errno, 42992082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 43002082Seschrock "links for '%s'"), dataset)); 4301789Sahrens } 4302789Sahrens } 4303789Sahrens 4304789Sahrens return (0); 4305789Sahrens } 43062676Seschrock 43072676Seschrock nvlist_t * 43082676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 43092676Seschrock { 43102676Seschrock return (zhp->zfs_user_props); 43112676Seschrock } 43122676Seschrock 43132676Seschrock /* 43143912Slling * This function is used by 'zfs list' to determine the exact set of columns to 43153912Slling * display, and their maximum widths. This does two main things: 43163912Slling * 43173912Slling * - If this is a list of all properties, then expand the list to include 43183912Slling * all native properties, and set a flag so that for each dataset we look 43193912Slling * for new unique user properties and add them to the list. 43203912Slling * 43213912Slling * - For non fixed-width properties, keep track of the maximum width seen 43223912Slling * so that we can size the column appropriately. 43233912Slling */ 43243912Slling int 43255094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 43263912Slling { 43273912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 43285094Slling zprop_list_t *entry; 43295094Slling zprop_list_t **last, **start; 43303912Slling nvlist_t *userprops, *propval; 43313912Slling nvpair_t *elem; 43323912Slling char *strval; 43333912Slling char buf[ZFS_MAXPROPLEN]; 43343912Slling 43355094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 43363912Slling return (-1); 43372676Seschrock 43382676Seschrock userprops = zfs_get_user_props(zhp); 43392676Seschrock 43402676Seschrock entry = *plp; 43412676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 43422676Seschrock /* 43432676Seschrock * Go through and add any user properties as necessary. We 43442676Seschrock * start by incrementing our list pointer to the first 43452676Seschrock * non-native property. 43462676Seschrock */ 43472676Seschrock start = plp; 43482676Seschrock while (*start != NULL) { 43495094Slling if ((*start)->pl_prop == ZPROP_INVAL) 43502676Seschrock break; 43512676Seschrock start = &(*start)->pl_next; 43522676Seschrock } 43532676Seschrock 43542676Seschrock elem = NULL; 43552676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 43562676Seschrock /* 43572676Seschrock * See if we've already found this property in our list. 43582676Seschrock */ 43592676Seschrock for (last = start; *last != NULL; 43602676Seschrock last = &(*last)->pl_next) { 43612676Seschrock if (strcmp((*last)->pl_user_prop, 43622676Seschrock nvpair_name(elem)) == 0) 43632676Seschrock break; 43642676Seschrock } 43652676Seschrock 43662676Seschrock if (*last == NULL) { 43672676Seschrock if ((entry = zfs_alloc(hdl, 43685094Slling sizeof (zprop_list_t))) == NULL || 43692676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 43702676Seschrock nvpair_name(elem)))) == NULL) { 43712676Seschrock free(entry); 43722676Seschrock return (-1); 43732676Seschrock } 43742676Seschrock 43755094Slling entry->pl_prop = ZPROP_INVAL; 43762676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 43772676Seschrock entry->pl_all = B_TRUE; 43782676Seschrock *last = entry; 43792676Seschrock } 43802676Seschrock } 43812676Seschrock } 43822676Seschrock 43832676Seschrock /* 43842676Seschrock * Now go through and check the width of any non-fixed columns 43852676Seschrock */ 43862676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 43872676Seschrock if (entry->pl_fixed) 43882676Seschrock continue; 43892676Seschrock 43905094Slling if (entry->pl_prop != ZPROP_INVAL) { 43912676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 43922676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 43932676Seschrock if (strlen(buf) > entry->pl_width) 43942676Seschrock entry->pl_width = strlen(buf); 43952676Seschrock } 43962676Seschrock } else if (nvlist_lookup_nvlist(userprops, 43972676Seschrock entry->pl_user_prop, &propval) == 0) { 43982676Seschrock verify(nvlist_lookup_string(propval, 43995094Slling ZPROP_VALUE, &strval) == 0); 44002676Seschrock if (strlen(strval) > entry->pl_width) 44012676Seschrock entry->pl_width = strlen(strval); 44022676Seschrock } 44032676Seschrock } 44042676Seschrock 44052676Seschrock return (0); 44062676Seschrock } 44074543Smarks 44084543Smarks int 44094543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 44104543Smarks { 44114543Smarks zfs_cmd_t zc = { 0 }; 44124543Smarks nvlist_t *nvp; 44134543Smarks gid_t gid; 44144543Smarks uid_t uid; 44154543Smarks const gid_t *groups; 44164543Smarks int group_cnt; 44174543Smarks int error; 44184543Smarks 44194543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 44204543Smarks return (no_memory(hdl)); 44214543Smarks 44224543Smarks uid = ucred_geteuid(cred); 44234543Smarks gid = ucred_getegid(cred); 44244543Smarks group_cnt = ucred_getgroups(cred, &groups); 44254543Smarks 44264543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 44274543Smarks return (1); 44284543Smarks 44294543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 44304543Smarks nvlist_free(nvp); 44314543Smarks return (1); 44324543Smarks } 44334543Smarks 44344543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 44354543Smarks nvlist_free(nvp); 44364543Smarks return (1); 44374543Smarks } 44384543Smarks 44394543Smarks if (nvlist_add_uint32_array(nvp, 44404543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 44414543Smarks nvlist_free(nvp); 44424543Smarks return (1); 44434543Smarks } 44444543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 44454543Smarks 44465094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 44474543Smarks return (-1); 44484543Smarks 44494543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 44504543Smarks nvlist_free(nvp); 44514543Smarks return (error); 44524543Smarks } 44534543Smarks 44544543Smarks int 44554543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4456*8845Samw@Sun.COM char *resource, void *export, void *sharetab, 4457*8845Samw@Sun.COM int sharemax, zfs_share_op_t operation) 44584543Smarks { 44594543Smarks zfs_cmd_t zc = { 0 }; 44604543Smarks int error; 44614543Smarks 44624543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 44634543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4464*8845Samw@Sun.COM if (resource) 4465*8845Samw@Sun.COM (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 44664543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 44674543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 44685331Samw zc.zc_share.z_sharetype = operation; 44694543Smarks zc.zc_share.z_sharemax = sharemax; 44704543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 44714543Smarks return (error); 44724543Smarks } 44738802SSanjeev.Bagewadi@Sun.COM 44748802SSanjeev.Bagewadi@Sun.COM void 44758802SSanjeev.Bagewadi@Sun.COM zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 44768802SSanjeev.Bagewadi@Sun.COM { 44778802SSanjeev.Bagewadi@Sun.COM nvpair_t *curr; 44788802SSanjeev.Bagewadi@Sun.COM 44798802SSanjeev.Bagewadi@Sun.COM /* 44808802SSanjeev.Bagewadi@Sun.COM * Keep a reference to the props-table against which we prune the 44818802SSanjeev.Bagewadi@Sun.COM * properties. 44828802SSanjeev.Bagewadi@Sun.COM */ 44838802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table = props; 44848802SSanjeev.Bagewadi@Sun.COM 44858802SSanjeev.Bagewadi@Sun.COM curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 44868802SSanjeev.Bagewadi@Sun.COM 44878802SSanjeev.Bagewadi@Sun.COM while (curr) { 44888802SSanjeev.Bagewadi@Sun.COM zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 44898802SSanjeev.Bagewadi@Sun.COM nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 44908802SSanjeev.Bagewadi@Sun.COM 44918802SSanjeev.Bagewadi@Sun.COM if (props[zfs_prop] == B_FALSE) 44928802SSanjeev.Bagewadi@Sun.COM (void) nvlist_remove(zhp->zfs_props, 44938802SSanjeev.Bagewadi@Sun.COM nvpair_name(curr), nvpair_type(curr)); 44948802SSanjeev.Bagewadi@Sun.COM curr = next; 44958802SSanjeev.Bagewadi@Sun.COM } 44968802SSanjeev.Bagewadi@Sun.COM } 4497*8845Samw@Sun.COM 4498*8845Samw@Sun.COM static int 4499*8845Samw@Sun.COM zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 4500*8845Samw@Sun.COM zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 4501*8845Samw@Sun.COM { 4502*8845Samw@Sun.COM zfs_cmd_t zc = { 0 }; 4503*8845Samw@Sun.COM nvlist_t *nvlist = NULL; 4504*8845Samw@Sun.COM int error; 4505*8845Samw@Sun.COM 4506*8845Samw@Sun.COM (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4507*8845Samw@Sun.COM (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4508*8845Samw@Sun.COM zc.zc_cookie = (uint64_t)cmd; 4509*8845Samw@Sun.COM 4510*8845Samw@Sun.COM if (cmd == ZFS_SMB_ACL_RENAME) { 4511*8845Samw@Sun.COM if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 4512*8845Samw@Sun.COM (void) no_memory(hdl); 4513*8845Samw@Sun.COM return (NULL); 4514*8845Samw@Sun.COM } 4515*8845Samw@Sun.COM } 4516*8845Samw@Sun.COM 4517*8845Samw@Sun.COM switch (cmd) { 4518*8845Samw@Sun.COM case ZFS_SMB_ACL_ADD: 4519*8845Samw@Sun.COM case ZFS_SMB_ACL_REMOVE: 4520*8845Samw@Sun.COM (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 4521*8845Samw@Sun.COM break; 4522*8845Samw@Sun.COM case ZFS_SMB_ACL_RENAME: 4523*8845Samw@Sun.COM if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 4524*8845Samw@Sun.COM resource1) != 0) { 4525*8845Samw@Sun.COM (void) no_memory(hdl); 4526*8845Samw@Sun.COM return (-1); 4527*8845Samw@Sun.COM } 4528*8845Samw@Sun.COM if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 4529*8845Samw@Sun.COM resource2) != 0) { 4530*8845Samw@Sun.COM (void) no_memory(hdl); 4531*8845Samw@Sun.COM return (-1); 4532*8845Samw@Sun.COM } 4533*8845Samw@Sun.COM if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 4534*8845Samw@Sun.COM nvlist_free(nvlist); 4535*8845Samw@Sun.COM return (-1); 4536*8845Samw@Sun.COM } 4537*8845Samw@Sun.COM break; 4538*8845Samw@Sun.COM case ZFS_SMB_ACL_PURGE: 4539*8845Samw@Sun.COM break; 4540*8845Samw@Sun.COM default: 4541*8845Samw@Sun.COM return (-1); 4542*8845Samw@Sun.COM } 4543*8845Samw@Sun.COM error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 4544*8845Samw@Sun.COM if (nvlist) 4545*8845Samw@Sun.COM nvlist_free(nvlist); 4546*8845Samw@Sun.COM return (error); 4547*8845Samw@Sun.COM } 4548*8845Samw@Sun.COM 4549*8845Samw@Sun.COM int 4550*8845Samw@Sun.COM zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 4551*8845Samw@Sun.COM char *path, char *resource) 4552*8845Samw@Sun.COM { 4553*8845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 4554*8845Samw@Sun.COM resource, NULL)); 4555*8845Samw@Sun.COM } 4556*8845Samw@Sun.COM 4557*8845Samw@Sun.COM int 4558*8845Samw@Sun.COM zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4559*8845Samw@Sun.COM char *path, char *resource) 4560*8845Samw@Sun.COM { 4561*8845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4562*8845Samw@Sun.COM resource, NULL)); 4563*8845Samw@Sun.COM } 4564*8845Samw@Sun.COM 4565*8845Samw@Sun.COM int 4566*8845Samw@Sun.COM zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4567*8845Samw@Sun.COM { 4568*8845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4569*8845Samw@Sun.COM NULL, NULL)); 4570*8845Samw@Sun.COM } 4571*8845Samw@Sun.COM 4572*8845Samw@Sun.COM int 4573*8845Samw@Sun.COM zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4574*8845Samw@Sun.COM char *oldname, char *newname) 4575*8845Samw@Sun.COM { 4576*8845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4577*8845Samw@Sun.COM oldname, newname)); 4578*8845Samw@Sun.COM } 4579