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> 489396SMatthew.Ahrens@Sun.COM #include <idmap.h> 499396SMatthew.Ahrens@Sun.COM #include <aclutils.h> 50789Sahrens 51789Sahrens #include <sys/spa.h> 522676Seschrock #include <sys/zap.h> 53789Sahrens #include <libzfs.h> 54789Sahrens 55789Sahrens #include "zfs_namecheck.h" 56789Sahrens #include "zfs_prop.h" 57789Sahrens #include "libzfs_impl.h" 584543Smarks #include "zfs_deleg.h" 59789Sahrens 604007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 619396SMatthew.Ahrens@Sun.COM static int userquota_propname_decode(const char *propname, boolean_t zoned, 629396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 634007Smmusante 64789Sahrens /* 65789Sahrens * Given a single type (not a mask of types), return the type in a human 66789Sahrens * readable form. 67789Sahrens */ 68789Sahrens const char * 69789Sahrens zfs_type_to_name(zfs_type_t type) 70789Sahrens { 71789Sahrens switch (type) { 72789Sahrens case ZFS_TYPE_FILESYSTEM: 73789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 74789Sahrens case ZFS_TYPE_SNAPSHOT: 75789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 76789Sahrens case ZFS_TYPE_VOLUME: 77789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 78789Sahrens } 79789Sahrens 80789Sahrens return (NULL); 81789Sahrens } 82789Sahrens 83789Sahrens /* 84789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 85789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 86789Sahrens * We guess what the type would have been based on the path and the mask of 87789Sahrens * acceptable types. 88789Sahrens */ 89789Sahrens static const char * 90789Sahrens path_to_str(const char *path, int types) 91789Sahrens { 92789Sahrens /* 93789Sahrens * When given a single type, always report the exact type. 94789Sahrens */ 95789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 96789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 97789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 98789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 99789Sahrens if (types == ZFS_TYPE_VOLUME) 100789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 101789Sahrens 102789Sahrens /* 103789Sahrens * The user is requesting more than one type of dataset. If this is the 104789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 105789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 106789Sahrens * snapshot attribute and try again. 107789Sahrens */ 108789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 109789Sahrens if (strchr(path, '@') != NULL) 110789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 111789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 112789Sahrens } 113789Sahrens 114789Sahrens /* 115789Sahrens * The user has requested either filesystems or volumes. 116789Sahrens * We have no way of knowing a priori what type this would be, so always 117789Sahrens * report it as "filesystem" or "volume", our two primitive types. 118789Sahrens */ 119789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 120789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 121789Sahrens 122789Sahrens assert(types & ZFS_TYPE_VOLUME); 123789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 124789Sahrens } 125789Sahrens 126789Sahrens /* 127789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 1289396SMatthew.Ahrens@Sun.COM * provide a more meaningful error message. We call zfs_error_aux() to 1299396SMatthew.Ahrens@Sun.COM * explain exactly why the name was not valid. 130789Sahrens */ 131789Sahrens static int 1325326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 1335326Sek110237 boolean_t modifying) 134789Sahrens { 135789Sahrens namecheck_err_t why; 136789Sahrens char what; 137789Sahrens 138789Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 1392082Seschrock if (hdl != NULL) { 140789Sahrens switch (why) { 1411003Slling case NAME_ERR_TOOLONG: 1422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1432082Seschrock "name is too long")); 1441003Slling break; 1451003Slling 146789Sahrens case NAME_ERR_LEADING_SLASH: 1472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1482082Seschrock "leading slash in name")); 149789Sahrens break; 150789Sahrens 151789Sahrens case NAME_ERR_EMPTY_COMPONENT: 1522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1532082Seschrock "empty component in name")); 154789Sahrens break; 155789Sahrens 156789Sahrens case NAME_ERR_TRAILING_SLASH: 1572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1582082Seschrock "trailing slash in name")); 159789Sahrens break; 160789Sahrens 161789Sahrens case NAME_ERR_INVALCHAR: 1622082Seschrock zfs_error_aux(hdl, 163789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 1642082Seschrock "'%c' in name"), what); 165789Sahrens break; 166789Sahrens 167789Sahrens case NAME_ERR_MULTIPLE_AT: 1682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1692082Seschrock "multiple '@' delimiters in name")); 170789Sahrens break; 1712856Snd150628 1722856Snd150628 case NAME_ERR_NOLETTER: 1732856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1742856Snd150628 "pool doesn't begin with a letter")); 1752856Snd150628 break; 1762856Snd150628 1772856Snd150628 case NAME_ERR_RESERVED: 1782856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1792856Snd150628 "name is reserved")); 1802856Snd150628 break; 1812856Snd150628 1822856Snd150628 case NAME_ERR_DISKLIKE: 1832856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1842856Snd150628 "reserved disk name")); 1852856Snd150628 break; 186789Sahrens } 187789Sahrens } 188789Sahrens 189789Sahrens return (0); 190789Sahrens } 191789Sahrens 192789Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 1932082Seschrock if (hdl != NULL) 1942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1952082Seschrock "snapshot delimiter '@' in filesystem name")); 196789Sahrens return (0); 197789Sahrens } 198789Sahrens 1992199Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 2002199Sahrens if (hdl != NULL) 2012199Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2023413Smmusante "missing '@' delimiter in snapshot name")); 2032199Sahrens return (0); 2042199Sahrens } 2052199Sahrens 2065326Sek110237 if (modifying && strchr(path, '%') != NULL) { 2075326Sek110237 if (hdl != NULL) 2085326Sek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2095326Sek110237 "invalid character %c in name"), '%'); 2105326Sek110237 return (0); 2115326Sek110237 } 2125326Sek110237 2132082Seschrock return (-1); 214789Sahrens } 215789Sahrens 216789Sahrens int 217789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 218789Sahrens { 2196423Sgw25295 if (type == ZFS_TYPE_POOL) 2206423Sgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 2215326Sek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 222789Sahrens } 223789Sahrens 224789Sahrens /* 2252676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2262676Seschrock * properties into a separate nvlist. 2272676Seschrock */ 2284217Seschrock static nvlist_t * 2294217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2302676Seschrock { 2312676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2322676Seschrock nvpair_t *elem; 2332676Seschrock nvlist_t *propval; 2344217Seschrock nvlist_t *nvl; 2354217Seschrock 2364217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2374217Seschrock (void) no_memory(hdl); 2384217Seschrock return (NULL); 2394217Seschrock } 2402676Seschrock 2412676Seschrock elem = NULL; 2424217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2432676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2442676Seschrock continue; 2452676Seschrock 2462676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2474217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2484217Seschrock nvlist_free(nvl); 2494217Seschrock (void) no_memory(hdl); 2504217Seschrock return (NULL); 2514217Seschrock } 2522676Seschrock } 2532676Seschrock 2544217Seschrock return (nvl); 2552676Seschrock } 2562676Seschrock 2576865Srm160521 static zpool_handle_t * 2586865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 2596865Srm160521 { 2606865Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 2616865Srm160521 zpool_handle_t *zph; 2626865Srm160521 2636865Srm160521 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 2646865Srm160521 if (hdl->libzfs_pool_handles != NULL) 2656865Srm160521 zph->zpool_next = hdl->libzfs_pool_handles; 2666865Srm160521 hdl->libzfs_pool_handles = zph; 2676865Srm160521 } 2686865Srm160521 return (zph); 2696865Srm160521 } 2706865Srm160521 2716865Srm160521 static zpool_handle_t * 2726865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 2736865Srm160521 { 2746865Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 2756865Srm160521 zpool_handle_t *zph = hdl->libzfs_pool_handles; 2766865Srm160521 2776865Srm160521 while ((zph != NULL) && 2786865Srm160521 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 2796865Srm160521 zph = zph->zpool_next; 2806865Srm160521 return (zph); 2816865Srm160521 } 2826865Srm160521 2836865Srm160521 /* 2846865Srm160521 * Returns a handle to the pool that contains the provided dataset. 2856865Srm160521 * If a handle to that pool already exists then that handle is returned. 2866865Srm160521 * Otherwise, a new handle is created and added to the list of handles. 2876865Srm160521 */ 2886865Srm160521 static zpool_handle_t * 2896865Srm160521 zpool_handle(zfs_handle_t *zhp) 2906865Srm160521 { 2916865Srm160521 char *pool_name; 2926865Srm160521 int len; 2936865Srm160521 zpool_handle_t *zph; 2946865Srm160521 2956865Srm160521 len = strcspn(zhp->zfs_name, "/@") + 1; 2966865Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, len); 2976865Srm160521 (void) strlcpy(pool_name, zhp->zfs_name, len); 2986865Srm160521 2996865Srm160521 zph = zpool_find_handle(zhp, pool_name, len); 3006865Srm160521 if (zph == NULL) 3016865Srm160521 zph = zpool_add_handle(zhp, pool_name); 3026865Srm160521 3036865Srm160521 free(pool_name); 3046865Srm160521 return (zph); 3056865Srm160521 } 3066865Srm160521 3076865Srm160521 void 3086865Srm160521 zpool_free_handles(libzfs_handle_t *hdl) 3096865Srm160521 { 3106865Srm160521 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 3116865Srm160521 3126865Srm160521 while (zph != NULL) { 3136865Srm160521 next = zph->zpool_next; 3146865Srm160521 zpool_close(zph); 3156865Srm160521 zph = next; 3166865Srm160521 } 3176865Srm160521 hdl->libzfs_pool_handles = NULL; 3186865Srm160521 } 3196865Srm160521 3202676Seschrock /* 321789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 322789Sahrens */ 323789Sahrens static int 3248228SEric.Taylor@Sun.COM get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 325789Sahrens { 3262676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3278228SEric.Taylor@Sun.COM 3288228SEric.Taylor@Sun.COM (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 3298228SEric.Taylor@Sun.COM 3308228SEric.Taylor@Sun.COM while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3311356Seschrock if (errno == ENOMEM) { 3328228SEric.Taylor@Sun.COM if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 3332082Seschrock return (-1); 3342676Seschrock } 3351356Seschrock } else { 3361356Seschrock return (-1); 3371356Seschrock } 3381356Seschrock } 3398228SEric.Taylor@Sun.COM return (0); 3408228SEric.Taylor@Sun.COM } 3418228SEric.Taylor@Sun.COM 3428228SEric.Taylor@Sun.COM static int 3438228SEric.Taylor@Sun.COM put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 3448228SEric.Taylor@Sun.COM { 3458228SEric.Taylor@Sun.COM nvlist_t *allprops, *userprops; 3468228SEric.Taylor@Sun.COM 3478228SEric.Taylor@Sun.COM zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 3488228SEric.Taylor@Sun.COM 3498228SEric.Taylor@Sun.COM if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 3502082Seschrock return (-1); 3512082Seschrock } 352789Sahrens 3539396SMatthew.Ahrens@Sun.COM /* 3549396SMatthew.Ahrens@Sun.COM * XXX Why do we store the user props separately, in addition to 3559396SMatthew.Ahrens@Sun.COM * storing them in zfs_props? 3569396SMatthew.Ahrens@Sun.COM */ 3574217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 3584217Seschrock nvlist_free(allprops); 3592676Seschrock return (-1); 3604217Seschrock } 3614217Seschrock 3624217Seschrock nvlist_free(zhp->zfs_props); 3634217Seschrock nvlist_free(zhp->zfs_user_props); 3644217Seschrock 3654217Seschrock zhp->zfs_props = allprops; 3664217Seschrock zhp->zfs_user_props = userprops; 3672082Seschrock 368789Sahrens return (0); 369789Sahrens } 370789Sahrens 3718228SEric.Taylor@Sun.COM static int 3728228SEric.Taylor@Sun.COM get_stats(zfs_handle_t *zhp) 3738228SEric.Taylor@Sun.COM { 3748228SEric.Taylor@Sun.COM int rc = 0; 3758228SEric.Taylor@Sun.COM zfs_cmd_t zc = { 0 }; 3768228SEric.Taylor@Sun.COM 3778228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 3788228SEric.Taylor@Sun.COM return (-1); 3798228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, &zc) != 0) 3808228SEric.Taylor@Sun.COM rc = -1; 3818228SEric.Taylor@Sun.COM else if (put_stats_zhdl(zhp, &zc) != 0) 3828228SEric.Taylor@Sun.COM rc = -1; 3838228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 3848228SEric.Taylor@Sun.COM return (rc); 3858228SEric.Taylor@Sun.COM } 3868228SEric.Taylor@Sun.COM 387789Sahrens /* 388789Sahrens * Refresh the properties currently stored in the handle. 389789Sahrens */ 390789Sahrens void 391789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 392789Sahrens { 393789Sahrens (void) get_stats(zhp); 394789Sahrens } 395789Sahrens 396789Sahrens /* 397789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 398789Sahrens * zfs_iter_* to create child handles on the fly. 399789Sahrens */ 4008228SEric.Taylor@Sun.COM static int 4018228SEric.Taylor@Sun.COM make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 402789Sahrens { 4034543Smarks char *logstr; 4048228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 405789Sahrens 4064543Smarks /* 4074543Smarks * Preserve history log string. 4084543Smarks * any changes performed here will be 4094543Smarks * logged as an internal event. 4104543Smarks */ 4114543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 4124543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 4138228SEric.Taylor@Sun.COM 4141758Sahrens top: 4158228SEric.Taylor@Sun.COM if (put_stats_zhdl(zhp, zc) != 0) { 4164543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 4178228SEric.Taylor@Sun.COM return (-1); 418789Sahrens } 419789Sahrens 4208228SEric.Taylor@Sun.COM 4211758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 4228228SEric.Taylor@Sun.COM zfs_cmd_t zc2 = { 0 }; 4231758Sahrens 4241758Sahrens /* 4251758Sahrens * If it is dds_inconsistent, then we've caught it in 4261758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 4271758Sahrens * it is inconsistent from the ZPL's point of view, so 4281758Sahrens * can't be mounted. However, it could also be that we 4291758Sahrens * have crashed in the middle of one of those 4301758Sahrens * operations, in which case we need to get rid of the 4311758Sahrens * inconsistent state. We do that by either rolling 4321758Sahrens * back to the previous snapshot (which will fail if 4331758Sahrens * there is none), or destroying the filesystem. Note 4341758Sahrens * that if we are still in the middle of an active 4351758Sahrens * 'receive' or 'destroy', then the rollback and destroy 4361758Sahrens * will fail with EBUSY and we will drive on as usual. 4371758Sahrens */ 4381758Sahrens 4398228SEric.Taylor@Sun.COM (void) strlcpy(zc2.zc_name, zhp->zfs_name, 4408228SEric.Taylor@Sun.COM sizeof (zc2.zc_name)); 4411758Sahrens 4422885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 4432082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 4448228SEric.Taylor@Sun.COM zc2.zc_objset_type = DMU_OST_ZVOL; 4451758Sahrens } else { 4468228SEric.Taylor@Sun.COM zc2.zc_objset_type = DMU_OST_ZFS; 4471758Sahrens } 4481758Sahrens 4491758Sahrens /* 4505331Samw * If we can successfully destroy it, pretend that it 4511758Sahrens * never existed. 4521758Sahrens */ 4538228SEric.Taylor@Sun.COM if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) { 4544543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 4551758Sahrens errno = ENOENT; 4568228SEric.Taylor@Sun.COM return (-1); 4571758Sahrens } 4588228SEric.Taylor@Sun.COM /* If we can successfully roll it back, reset the stats */ 4598228SEric.Taylor@Sun.COM if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) { 4608228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, zc) != 0) { 4618228SEric.Taylor@Sun.COM zhp->zfs_hdl->libzfs_log_str = logstr; 4628228SEric.Taylor@Sun.COM return (-1); 4638228SEric.Taylor@Sun.COM } 4645367Sahrens goto top; 4658228SEric.Taylor@Sun.COM } 4661758Sahrens } 4671758Sahrens 468789Sahrens /* 469789Sahrens * We've managed to open the dataset and gather statistics. Determine 470789Sahrens * the high-level type. 471789Sahrens */ 4722885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 4732885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 4742885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 4752885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 4762885Sahrens else 4772885Sahrens abort(); 4782885Sahrens 479789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 480789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 481789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 482789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 483789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 484789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 485789Sahrens else 4862082Seschrock abort(); /* we should never see any other types */ 487789Sahrens 4884543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 4896865Srm160521 zhp->zpool_hdl = zpool_handle(zhp); 4908228SEric.Taylor@Sun.COM return (0); 4918228SEric.Taylor@Sun.COM } 4928228SEric.Taylor@Sun.COM 4938228SEric.Taylor@Sun.COM zfs_handle_t * 4948228SEric.Taylor@Sun.COM make_dataset_handle(libzfs_handle_t *hdl, const char *path) 4958228SEric.Taylor@Sun.COM { 4968228SEric.Taylor@Sun.COM zfs_cmd_t zc = { 0 }; 4978228SEric.Taylor@Sun.COM 4988228SEric.Taylor@Sun.COM zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 4998228SEric.Taylor@Sun.COM 5008228SEric.Taylor@Sun.COM if (zhp == NULL) 5018228SEric.Taylor@Sun.COM return (NULL); 5028228SEric.Taylor@Sun.COM 5038228SEric.Taylor@Sun.COM zhp->zfs_hdl = hdl; 5048228SEric.Taylor@Sun.COM (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 5058228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 5068228SEric.Taylor@Sun.COM free(zhp); 5078228SEric.Taylor@Sun.COM return (NULL); 5088228SEric.Taylor@Sun.COM } 5098228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, &zc) == -1) { 5108228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 5118228SEric.Taylor@Sun.COM free(zhp); 5128228SEric.Taylor@Sun.COM return (NULL); 5138228SEric.Taylor@Sun.COM } 5148228SEric.Taylor@Sun.COM if (make_dataset_handle_common(zhp, &zc) == -1) { 5158228SEric.Taylor@Sun.COM free(zhp); 5168228SEric.Taylor@Sun.COM zhp = NULL; 5178228SEric.Taylor@Sun.COM } 5188228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 5198228SEric.Taylor@Sun.COM return (zhp); 5208228SEric.Taylor@Sun.COM } 5218228SEric.Taylor@Sun.COM 5228228SEric.Taylor@Sun.COM static zfs_handle_t * 5238228SEric.Taylor@Sun.COM make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 5248228SEric.Taylor@Sun.COM { 5258228SEric.Taylor@Sun.COM zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 5268228SEric.Taylor@Sun.COM 5278228SEric.Taylor@Sun.COM if (zhp == NULL) 5288228SEric.Taylor@Sun.COM return (NULL); 5298228SEric.Taylor@Sun.COM 5308228SEric.Taylor@Sun.COM zhp->zfs_hdl = hdl; 5318228SEric.Taylor@Sun.COM (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 5328228SEric.Taylor@Sun.COM if (make_dataset_handle_common(zhp, zc) == -1) { 5338228SEric.Taylor@Sun.COM free(zhp); 5348228SEric.Taylor@Sun.COM return (NULL); 5358228SEric.Taylor@Sun.COM } 536789Sahrens return (zhp); 537789Sahrens } 538789Sahrens 539789Sahrens /* 540789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 541789Sahrens * argument is a mask of acceptable types. The function will print an 542789Sahrens * appropriate error message and return NULL if it can't be opened. 543789Sahrens */ 544789Sahrens zfs_handle_t * 5452082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 546789Sahrens { 547789Sahrens zfs_handle_t *zhp; 5482082Seschrock char errbuf[1024]; 5492082Seschrock 5502082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 5512082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 552789Sahrens 553789Sahrens /* 5542082Seschrock * Validate the name before we even try to open it. 555789Sahrens */ 5565326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 5572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5582082Seschrock "invalid dataset name")); 5592082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 560789Sahrens return (NULL); 561789Sahrens } 562789Sahrens 563789Sahrens /* 564789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 565789Sahrens */ 566789Sahrens errno = 0; 5672082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 5683237Slling (void) zfs_standard_error(hdl, errno, errbuf); 569789Sahrens return (NULL); 570789Sahrens } 571789Sahrens 572789Sahrens if (!(types & zhp->zfs_type)) { 5732082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 5742142Seschrock zfs_close(zhp); 575789Sahrens return (NULL); 576789Sahrens } 577789Sahrens 578789Sahrens return (zhp); 579789Sahrens } 580789Sahrens 581789Sahrens /* 582789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 583789Sahrens */ 584789Sahrens void 585789Sahrens zfs_close(zfs_handle_t *zhp) 586789Sahrens { 587789Sahrens if (zhp->zfs_mntopts) 588789Sahrens free(zhp->zfs_mntopts); 5892676Seschrock nvlist_free(zhp->zfs_props); 5902676Seschrock nvlist_free(zhp->zfs_user_props); 591789Sahrens free(zhp); 592789Sahrens } 593789Sahrens 5948228SEric.Taylor@Sun.COM typedef struct mnttab_node { 5958228SEric.Taylor@Sun.COM struct mnttab mtn_mt; 5968228SEric.Taylor@Sun.COM avl_node_t mtn_node; 5978228SEric.Taylor@Sun.COM } mnttab_node_t; 5988228SEric.Taylor@Sun.COM 5998228SEric.Taylor@Sun.COM static int 6008228SEric.Taylor@Sun.COM libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 6018228SEric.Taylor@Sun.COM { 6028228SEric.Taylor@Sun.COM const mnttab_node_t *mtn1 = arg1; 6038228SEric.Taylor@Sun.COM const mnttab_node_t *mtn2 = arg2; 6048228SEric.Taylor@Sun.COM int rv; 6058228SEric.Taylor@Sun.COM 6068228SEric.Taylor@Sun.COM rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 6078228SEric.Taylor@Sun.COM 6088228SEric.Taylor@Sun.COM if (rv == 0) 6098228SEric.Taylor@Sun.COM return (0); 6108228SEric.Taylor@Sun.COM return (rv > 0 ? 1 : -1); 6118228SEric.Taylor@Sun.COM } 6128228SEric.Taylor@Sun.COM 6138228SEric.Taylor@Sun.COM void 6148228SEric.Taylor@Sun.COM libzfs_mnttab_init(libzfs_handle_t *hdl) 6158228SEric.Taylor@Sun.COM { 6168228SEric.Taylor@Sun.COM assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 6178228SEric.Taylor@Sun.COM avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 6188228SEric.Taylor@Sun.COM sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 6198811SEric.Taylor@Sun.COM } 6208811SEric.Taylor@Sun.COM 6218811SEric.Taylor@Sun.COM void 6228811SEric.Taylor@Sun.COM libzfs_mnttab_update(libzfs_handle_t *hdl) 6238811SEric.Taylor@Sun.COM { 6248811SEric.Taylor@Sun.COM struct mnttab entry; 6258228SEric.Taylor@Sun.COM 6268228SEric.Taylor@Sun.COM rewind(hdl->libzfs_mnttab); 6278228SEric.Taylor@Sun.COM while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 6288228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6298228SEric.Taylor@Sun.COM 6308228SEric.Taylor@Sun.COM if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 6318228SEric.Taylor@Sun.COM continue; 6328228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 6338228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 6348228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 6358228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 6368228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 6378228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 6388228SEric.Taylor@Sun.COM } 6398228SEric.Taylor@Sun.COM } 6408228SEric.Taylor@Sun.COM 6418228SEric.Taylor@Sun.COM void 6428228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl) 6438228SEric.Taylor@Sun.COM { 6448228SEric.Taylor@Sun.COM void *cookie = NULL; 6458228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6468228SEric.Taylor@Sun.COM 6478228SEric.Taylor@Sun.COM while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) { 6488228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_special); 6498228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mountp); 6508228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_fstype); 6518228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mntopts); 6528228SEric.Taylor@Sun.COM free(mtn); 6538228SEric.Taylor@Sun.COM } 6548228SEric.Taylor@Sun.COM avl_destroy(&hdl->libzfs_mnttab_cache); 6558228SEric.Taylor@Sun.COM } 6568228SEric.Taylor@Sun.COM 6578811SEric.Taylor@Sun.COM void 6588811SEric.Taylor@Sun.COM libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 6598811SEric.Taylor@Sun.COM { 6608811SEric.Taylor@Sun.COM hdl->libzfs_mnttab_enable = enable; 6618811SEric.Taylor@Sun.COM } 6628811SEric.Taylor@Sun.COM 6638228SEric.Taylor@Sun.COM int 6648228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 6658228SEric.Taylor@Sun.COM struct mnttab *entry) 6668228SEric.Taylor@Sun.COM { 6678228SEric.Taylor@Sun.COM mnttab_node_t find; 6688228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6698228SEric.Taylor@Sun.COM 6708811SEric.Taylor@Sun.COM if (!hdl->libzfs_mnttab_enable) { 6718811SEric.Taylor@Sun.COM struct mnttab srch = { 0 }; 6728811SEric.Taylor@Sun.COM 6738811SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 6748811SEric.Taylor@Sun.COM libzfs_mnttab_fini(hdl); 6758811SEric.Taylor@Sun.COM rewind(hdl->libzfs_mnttab); 6768811SEric.Taylor@Sun.COM srch.mnt_special = (char *)fsname; 6778811SEric.Taylor@Sun.COM srch.mnt_fstype = MNTTYPE_ZFS; 6788811SEric.Taylor@Sun.COM if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 6798811SEric.Taylor@Sun.COM return (0); 6808811SEric.Taylor@Sun.COM else 6818811SEric.Taylor@Sun.COM return (ENOENT); 6828811SEric.Taylor@Sun.COM } 6838811SEric.Taylor@Sun.COM 6848228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 6858811SEric.Taylor@Sun.COM libzfs_mnttab_update(hdl); 6868228SEric.Taylor@Sun.COM 6878228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 6888228SEric.Taylor@Sun.COM mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 6898228SEric.Taylor@Sun.COM if (mtn) { 6908228SEric.Taylor@Sun.COM *entry = mtn->mtn_mt; 6918228SEric.Taylor@Sun.COM return (0); 6928228SEric.Taylor@Sun.COM } 6938228SEric.Taylor@Sun.COM return (ENOENT); 6948228SEric.Taylor@Sun.COM } 6958228SEric.Taylor@Sun.COM 6968228SEric.Taylor@Sun.COM void 6978228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 6988228SEric.Taylor@Sun.COM const char *mountp, const char *mntopts) 6998228SEric.Taylor@Sun.COM { 7008228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 7018228SEric.Taylor@Sun.COM 7028228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 7038228SEric.Taylor@Sun.COM return; 7048228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 7058228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 7068228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 7078228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 7088228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 7098228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 7108228SEric.Taylor@Sun.COM } 7118228SEric.Taylor@Sun.COM 7128228SEric.Taylor@Sun.COM void 7138228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 7148228SEric.Taylor@Sun.COM { 7158228SEric.Taylor@Sun.COM mnttab_node_t find; 7168228SEric.Taylor@Sun.COM mnttab_node_t *ret; 7178228SEric.Taylor@Sun.COM 7188228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 7198228SEric.Taylor@Sun.COM if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) { 7208228SEric.Taylor@Sun.COM avl_remove(&hdl->libzfs_mnttab_cache, ret); 7218228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_special); 7228228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mountp); 7238228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_fstype); 7248228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mntopts); 7258228SEric.Taylor@Sun.COM free(ret); 7268228SEric.Taylor@Sun.COM } 7278228SEric.Taylor@Sun.COM } 7288228SEric.Taylor@Sun.COM 7295713Srm160521 int 7305713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 7315713Srm160521 { 7326865Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 7336865Srm160521 7345713Srm160521 if (zpool_handle == NULL) 7355713Srm160521 return (-1); 7365713Srm160521 7375713Srm160521 *spa_version = zpool_get_prop_int(zpool_handle, 7385713Srm160521 ZPOOL_PROP_VERSION, NULL); 7395713Srm160521 return (0); 7405713Srm160521 } 7415713Srm160521 7425713Srm160521 /* 7435713Srm160521 * The choice of reservation property depends on the SPA version. 7445713Srm160521 */ 7455713Srm160521 static int 7465713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 7475713Srm160521 { 7485713Srm160521 int spa_version; 7495713Srm160521 7505713Srm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 7515713Srm160521 return (-1); 7525713Srm160521 7535713Srm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 7545713Srm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 7555713Srm160521 else 7565713Srm160521 *resv_prop = ZFS_PROP_RESERVATION; 7575713Srm160521 7585713Srm160521 return (0); 7595713Srm160521 } 7605713Srm160521 7613912Slling /* 7622676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 7632676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 7642676Seschrock * strings. 765789Sahrens */ 7667184Stimh nvlist_t * 7677184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 7685094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 769789Sahrens { 7702676Seschrock nvpair_t *elem; 7712676Seschrock uint64_t intval; 7722676Seschrock char *strval; 7735094Slling zfs_prop_t prop; 7742676Seschrock nvlist_t *ret; 7755331Samw int chosen_normal = -1; 7765331Samw int chosen_utf = -1; 7772676Seschrock 7785094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 7795094Slling (void) no_memory(hdl); 7805094Slling return (NULL); 781789Sahrens } 782789Sahrens 7839396SMatthew.Ahrens@Sun.COM /* 7849396SMatthew.Ahrens@Sun.COM * Make sure this property is valid and applies to this type. 7859396SMatthew.Ahrens@Sun.COM */ 7869396SMatthew.Ahrens@Sun.COM 7872676Seschrock elem = NULL; 7882676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 7895094Slling const char *propname = nvpair_name(elem); 7902676Seschrock 7919396SMatthew.Ahrens@Sun.COM prop = zfs_name_to_prop(propname); 7929396SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { 7935094Slling /* 7949396SMatthew.Ahrens@Sun.COM * This is a user property: make sure it's a 7955094Slling * string, and that it's less than ZAP_MAXNAMELEN. 7965094Slling */ 7975094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 7985094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7995094Slling "'%s' must be a string"), propname); 8005094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8015094Slling goto error; 8025094Slling } 8035094Slling 8045094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 8055094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8065094Slling "property name '%s' is too long"), 8072676Seschrock propname); 8082676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8092676Seschrock goto error; 8102676Seschrock } 8112676Seschrock 8122676Seschrock (void) nvpair_value_string(elem, &strval); 8132676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 8142676Seschrock (void) no_memory(hdl); 8152676Seschrock goto error; 8162676Seschrock } 8172676Seschrock continue; 818789Sahrens } 8192676Seschrock 8209396SMatthew.Ahrens@Sun.COM /* 8219396SMatthew.Ahrens@Sun.COM * Currently, only user properties can be modified on 8229396SMatthew.Ahrens@Sun.COM * snapshots. 8239396SMatthew.Ahrens@Sun.COM */ 8247265Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 8257265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8267265Sahrens "this property can not be modified for snapshots")); 8277265Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 8287265Sahrens goto error; 8297265Sahrens } 8307265Sahrens 8319396SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { 8329396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t uqtype; 8339396SMatthew.Ahrens@Sun.COM char newpropname[128]; 8349396SMatthew.Ahrens@Sun.COM char domain[128]; 8359396SMatthew.Ahrens@Sun.COM uint64_t rid; 8369396SMatthew.Ahrens@Sun.COM uint64_t valary[3]; 8379396SMatthew.Ahrens@Sun.COM 8389396SMatthew.Ahrens@Sun.COM if (userquota_propname_decode(propname, zoned, 8399396SMatthew.Ahrens@Sun.COM &uqtype, domain, sizeof (domain), &rid) != 0) { 8409396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, 8419396SMatthew.Ahrens@Sun.COM dgettext(TEXT_DOMAIN, 8429396SMatthew.Ahrens@Sun.COM "'%s' has an invalid user/group name"), 8439396SMatthew.Ahrens@Sun.COM propname); 8449396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8459396SMatthew.Ahrens@Sun.COM goto error; 8469396SMatthew.Ahrens@Sun.COM } 8479396SMatthew.Ahrens@Sun.COM 8489396SMatthew.Ahrens@Sun.COM if (uqtype != ZFS_PROP_USERQUOTA && 8499396SMatthew.Ahrens@Sun.COM uqtype != ZFS_PROP_GROUPQUOTA) { 8509396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, 8519396SMatthew.Ahrens@Sun.COM dgettext(TEXT_DOMAIN, "'%s' is readonly"), 8529396SMatthew.Ahrens@Sun.COM propname); 8539396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, EZFS_PROPREADONLY, 8549396SMatthew.Ahrens@Sun.COM errbuf); 8559396SMatthew.Ahrens@Sun.COM goto error; 8569396SMatthew.Ahrens@Sun.COM } 8579396SMatthew.Ahrens@Sun.COM 8589396SMatthew.Ahrens@Sun.COM if (nvpair_type(elem) == DATA_TYPE_STRING) { 8599396SMatthew.Ahrens@Sun.COM (void) nvpair_value_string(elem, &strval); 8609396SMatthew.Ahrens@Sun.COM if (strcmp(strval, "none") == 0) { 8619396SMatthew.Ahrens@Sun.COM intval = 0; 8629396SMatthew.Ahrens@Sun.COM } else if (zfs_nicestrtonum(hdl, 8639396SMatthew.Ahrens@Sun.COM strval, &intval) != 0) { 8649396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, 8659396SMatthew.Ahrens@Sun.COM EZFS_BADPROP, errbuf); 8669396SMatthew.Ahrens@Sun.COM goto error; 8679396SMatthew.Ahrens@Sun.COM } 8689396SMatthew.Ahrens@Sun.COM } else if (nvpair_type(elem) == 8699396SMatthew.Ahrens@Sun.COM DATA_TYPE_UINT64) { 8709396SMatthew.Ahrens@Sun.COM (void) nvpair_value_uint64(elem, &intval); 8719396SMatthew.Ahrens@Sun.COM if (intval == 0) { 8729396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8739396SMatthew.Ahrens@Sun.COM "use 'none' to disable " 8749396SMatthew.Ahrens@Sun.COM "userquota/groupquota")); 8759396SMatthew.Ahrens@Sun.COM goto error; 8769396SMatthew.Ahrens@Sun.COM } 8779396SMatthew.Ahrens@Sun.COM } else { 8789396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8799396SMatthew.Ahrens@Sun.COM "'%s' must be a number"), propname); 8809396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8819396SMatthew.Ahrens@Sun.COM goto error; 8829396SMatthew.Ahrens@Sun.COM } 8839396SMatthew.Ahrens@Sun.COM 8849396SMatthew.Ahrens@Sun.COM (void) snprintf(newpropname, sizeof (newpropname), 8859396SMatthew.Ahrens@Sun.COM "%s%s", zfs_userquota_prop_prefixes[uqtype], 8869396SMatthew.Ahrens@Sun.COM domain); 8879396SMatthew.Ahrens@Sun.COM valary[0] = uqtype; 8889396SMatthew.Ahrens@Sun.COM valary[1] = rid; 8899396SMatthew.Ahrens@Sun.COM valary[2] = intval; 8909396SMatthew.Ahrens@Sun.COM if (nvlist_add_uint64_array(ret, newpropname, 8919396SMatthew.Ahrens@Sun.COM valary, 3) != 0) { 8929396SMatthew.Ahrens@Sun.COM (void) no_memory(hdl); 8939396SMatthew.Ahrens@Sun.COM goto error; 8949396SMatthew.Ahrens@Sun.COM } 8959396SMatthew.Ahrens@Sun.COM continue; 8969396SMatthew.Ahrens@Sun.COM } 8979396SMatthew.Ahrens@Sun.COM 8989396SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL) { 8999396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9009396SMatthew.Ahrens@Sun.COM "invalid property '%s'"), propname); 9019396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9029396SMatthew.Ahrens@Sun.COM goto error; 9039396SMatthew.Ahrens@Sun.COM } 9049396SMatthew.Ahrens@Sun.COM 9052676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 9062676Seschrock zfs_error_aux(hdl, 9072676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 9082676Seschrock "apply to datasets of this type"), propname); 9092676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 9102676Seschrock goto error; 9112676Seschrock } 9122676Seschrock 9132676Seschrock if (zfs_prop_readonly(prop) && 9145331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 9152676Seschrock zfs_error_aux(hdl, 9162676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 9172676Seschrock propname); 9182676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 9192676Seschrock goto error; 9202676Seschrock } 9212676Seschrock 9225094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 9235094Slling &strval, &intval, errbuf) != 0) 9242676Seschrock goto error; 9252676Seschrock 9262676Seschrock /* 9272676Seschrock * Perform some additional checks for specific properties. 9282676Seschrock */ 9292676Seschrock switch (prop) { 9304577Sahrens case ZFS_PROP_VERSION: 9314577Sahrens { 9324577Sahrens int version; 9334577Sahrens 9344577Sahrens if (zhp == NULL) 9354577Sahrens break; 9364577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 9374577Sahrens if (intval < version) { 9384577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9394577Sahrens "Can not downgrade; already at version %u"), 9404577Sahrens version); 9414577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9424577Sahrens goto error; 9434577Sahrens } 9444577Sahrens break; 9454577Sahrens } 9464577Sahrens 9472676Seschrock case ZFS_PROP_RECORDSIZE: 9482676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 9492676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 9502676Seschrock if (intval < SPA_MINBLOCKSIZE || 9512676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 9522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9532676Seschrock "'%s' must be power of 2 from %u " 9542676Seschrock "to %uk"), propname, 9552676Seschrock (uint_t)SPA_MINBLOCKSIZE, 9562676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 9572676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9582676Seschrock goto error; 959789Sahrens } 960789Sahrens break; 961789Sahrens 9623126Sahl case ZFS_PROP_SHAREISCSI: 9633126Sahl if (strcmp(strval, "off") != 0 && 9643126Sahl strcmp(strval, "on") != 0 && 9653126Sahl strcmp(strval, "type=disk") != 0) { 9663126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9673126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 9683126Sahl propname); 9693126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9703126Sahl goto error; 9713126Sahl } 9723126Sahl 9733126Sahl break; 9743126Sahl 9752676Seschrock case ZFS_PROP_MOUNTPOINT: 9764778Srm160521 { 9774778Srm160521 namecheck_err_t why; 9784778Srm160521 9792676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 9802676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 9812676Seschrock break; 9822676Seschrock 9834778Srm160521 if (mountpoint_namecheck(strval, &why)) { 9844778Srm160521 switch (why) { 9854778Srm160521 case NAME_ERR_LEADING_SLASH: 9864778Srm160521 zfs_error_aux(hdl, 9874778Srm160521 dgettext(TEXT_DOMAIN, 9884778Srm160521 "'%s' must be an absolute path, " 9894778Srm160521 "'none', or 'legacy'"), propname); 9904778Srm160521 break; 9914778Srm160521 case NAME_ERR_TOOLONG: 9924778Srm160521 zfs_error_aux(hdl, 9934778Srm160521 dgettext(TEXT_DOMAIN, 9944778Srm160521 "component of '%s' is too long"), 9954778Srm160521 propname); 9964778Srm160521 break; 9974778Srm160521 } 9982676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9992676Seschrock goto error; 1000789Sahrens } 10014778Srm160521 } 10024778Srm160521 10033126Sahl /*FALLTHRU*/ 10043126Sahl 10055331Samw case ZFS_PROP_SHARESMB: 10063126Sahl case ZFS_PROP_SHARENFS: 10073126Sahl /* 10085331Samw * For the mountpoint and sharenfs or sharesmb 10095331Samw * properties, check if it can be set in a 10105331Samw * global/non-global zone based on 10113126Sahl * the zoned property value: 10123126Sahl * 10133126Sahl * global zone non-global zone 10143126Sahl * -------------------------------------------------- 10153126Sahl * zoned=on mountpoint (no) mountpoint (yes) 10163126Sahl * sharenfs (no) sharenfs (no) 10175331Samw * sharesmb (no) sharesmb (no) 10183126Sahl * 10193126Sahl * zoned=off mountpoint (yes) N/A 10203126Sahl * sharenfs (yes) 10215331Samw * sharesmb (yes) 10223126Sahl */ 10232676Seschrock if (zoned) { 10242676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 10252676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10262676Seschrock "'%s' cannot be set on " 10272676Seschrock "dataset in a non-global zone"), 10282676Seschrock propname); 10292676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 10302676Seschrock errbuf); 10312676Seschrock goto error; 10325331Samw } else if (prop == ZFS_PROP_SHARENFS || 10335331Samw prop == ZFS_PROP_SHARESMB) { 10342676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10352676Seschrock "'%s' cannot be set in " 10362676Seschrock "a non-global zone"), propname); 10372676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 10382676Seschrock errbuf); 10392676Seschrock goto error; 10402676Seschrock } 10412676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 10422676Seschrock /* 10432676Seschrock * If zoned property is 'off', this must be in 10449396SMatthew.Ahrens@Sun.COM * a global zone. If not, something is wrong. 10452676Seschrock */ 10462676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10472676Seschrock "'%s' cannot be set while dataset " 10482676Seschrock "'zoned' property is set"), propname); 10492676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 10502676Seschrock goto error; 10512676Seschrock } 10523126Sahl 10534180Sdougm /* 10544180Sdougm * At this point, it is legitimate to set the 10554180Sdougm * property. Now we want to make sure that the 10564180Sdougm * property value is valid if it is sharenfs. 10574180Sdougm */ 10585331Samw if ((prop == ZFS_PROP_SHARENFS || 10595331Samw prop == ZFS_PROP_SHARESMB) && 10604217Seschrock strcmp(strval, "on") != 0 && 10614217Seschrock strcmp(strval, "off") != 0) { 10625331Samw zfs_share_proto_t proto; 10635331Samw 10645331Samw if (prop == ZFS_PROP_SHARESMB) 10655331Samw proto = PROTO_SMB; 10665331Samw else 10675331Samw proto = PROTO_NFS; 10684180Sdougm 10694180Sdougm /* 10705331Samw * Must be an valid sharing protocol 10715331Samw * option string so init the libshare 10725331Samw * in order to enable the parser and 10735331Samw * then parse the options. We use the 10745331Samw * control API since we don't care about 10755331Samw * the current configuration and don't 10764180Sdougm * want the overhead of loading it 10774180Sdougm * until we actually do something. 10784180Sdougm */ 10794180Sdougm 10804217Seschrock if (zfs_init_libshare(hdl, 10814217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 10824217Seschrock /* 10834217Seschrock * An error occurred so we can't do 10844217Seschrock * anything 10854217Seschrock */ 10864217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10874217Seschrock "'%s' cannot be set: problem " 10884217Seschrock "in share initialization"), 10894217Seschrock propname); 10904217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10914217Seschrock errbuf); 10924217Seschrock goto error; 10934217Seschrock } 10944217Seschrock 10955331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 10964217Seschrock /* 10974217Seschrock * There was an error in parsing so 10984217Seschrock * deal with it by issuing an error 10994217Seschrock * message and leaving after 11004217Seschrock * uninitializing the the libshare 11014217Seschrock * interface. 11024217Seschrock */ 11034217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11044217Seschrock "'%s' cannot be set to invalid " 11054217Seschrock "options"), propname); 11064217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 11074217Seschrock errbuf); 11084217Seschrock zfs_uninit_libshare(hdl); 11094217Seschrock goto error; 11104217Seschrock } 11114180Sdougm zfs_uninit_libshare(hdl); 11124180Sdougm } 11134180Sdougm 11143126Sahl break; 11155331Samw case ZFS_PROP_UTF8ONLY: 11165331Samw chosen_utf = (int)intval; 11175331Samw break; 11185331Samw case ZFS_PROP_NORMALIZE: 11195331Samw chosen_normal = (int)intval; 11205331Samw break; 11212676Seschrock } 11222676Seschrock 11232676Seschrock /* 11242676Seschrock * For changes to existing volumes, we have some additional 11252676Seschrock * checks to enforce. 11262676Seschrock */ 11272676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 11282676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 11292676Seschrock ZFS_PROP_VOLSIZE); 11302676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 11312676Seschrock ZFS_PROP_VOLBLOCKSIZE); 11322676Seschrock char buf[64]; 11332676Seschrock 11342676Seschrock switch (prop) { 11352676Seschrock case ZFS_PROP_RESERVATION: 11365378Sck153898 case ZFS_PROP_REFRESERVATION: 11372676Seschrock if (intval > volsize) { 11382676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11392676Seschrock "'%s' is greater than current " 11402676Seschrock "volume size"), propname); 11412676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 11422676Seschrock errbuf); 11432676Seschrock goto error; 11442676Seschrock } 11452676Seschrock break; 11462676Seschrock 11472676Seschrock case ZFS_PROP_VOLSIZE: 11482676Seschrock if (intval % blocksize != 0) { 11492676Seschrock zfs_nicenum(blocksize, buf, 11502676Seschrock sizeof (buf)); 11512676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11522676Seschrock "'%s' must be a multiple of " 11532676Seschrock "volume block size (%s)"), 11542676Seschrock propname, buf); 11552676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 11562676Seschrock errbuf); 11572676Seschrock goto error; 11582676Seschrock } 11592676Seschrock 11602676Seschrock if (intval == 0) { 11612676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11622676Seschrock "'%s' cannot be zero"), 11632676Seschrock propname); 11642676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 11652676Seschrock errbuf); 11662676Seschrock goto error; 1167789Sahrens } 11683126Sahl break; 1169789Sahrens } 1170789Sahrens } 1171789Sahrens } 1172789Sahrens 11732676Seschrock /* 11745331Samw * If normalization was chosen, but no UTF8 choice was made, 11755331Samw * enforce rejection of non-UTF8 names. 11765331Samw * 11775331Samw * If normalization was chosen, but rejecting non-UTF8 names 11785331Samw * was explicitly not chosen, it is an error. 11795331Samw */ 11805498Stimh if (chosen_normal > 0 && chosen_utf < 0) { 11815331Samw if (nvlist_add_uint64(ret, 11825331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 11835331Samw (void) no_memory(hdl); 11845331Samw goto error; 11855331Samw } 11865498Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 11875331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11885331Samw "'%s' must be set 'on' if normalization chosen"), 11895331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 11905331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 11915331Samw goto error; 11925331Samw } 11935331Samw 11945331Samw /* 11952676Seschrock * If this is an existing volume, and someone is setting the volsize, 11962676Seschrock * make sure that it matches the reservation, or add it if necessary. 11972676Seschrock */ 11982676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 11992676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 12002676Seschrock &intval) == 0) { 12012676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 12022676Seschrock ZFS_PROP_VOLSIZE); 12035481Sck153898 uint64_t old_reservation; 12042676Seschrock uint64_t new_reservation; 12055481Sck153898 zfs_prop_t resv_prop; 12065713Srm160521 12075713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 12085481Sck153898 goto error; 12095481Sck153898 old_reservation = zfs_prop_get_int(zhp, resv_prop); 12102676Seschrock 12112676Seschrock if (old_volsize == old_reservation && 12125481Sck153898 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop), 12132676Seschrock &new_reservation) != 0) { 12142676Seschrock if (nvlist_add_uint64(ret, 12155481Sck153898 zfs_prop_to_name(resv_prop), intval) != 0) { 12162676Seschrock (void) no_memory(hdl); 12172676Seschrock goto error; 12182676Seschrock } 12192676Seschrock } 12202676Seschrock } 12212676Seschrock return (ret); 12222676Seschrock 12232676Seschrock error: 12242676Seschrock nvlist_free(ret); 12252676Seschrock return (NULL); 1226789Sahrens } 1227789Sahrens 1228789Sahrens /* 1229789Sahrens * Given a property name and value, set the property for the given dataset. 1230789Sahrens */ 1231789Sahrens int 12322676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1233789Sahrens { 1234789Sahrens zfs_cmd_t zc = { 0 }; 12352676Seschrock int ret = -1; 12362676Seschrock prop_changelist_t *cl = NULL; 12372082Seschrock char errbuf[1024]; 12382082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 12392676Seschrock nvlist_t *nvl = NULL, *realprops; 12402676Seschrock zfs_prop_t prop; 12417509SMark.Musante@Sun.COM boolean_t do_prefix; 12427509SMark.Musante@Sun.COM uint64_t idx; 12432082Seschrock 12442082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 12452676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 12462082Seschrock zhp->zfs_name); 12472082Seschrock 12482676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 12492676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 12502676Seschrock (void) no_memory(hdl); 12512676Seschrock goto error; 1252789Sahrens } 1253789Sahrens 12547184Stimh if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl, 12552676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 12562676Seschrock goto error; 12575094Slling 12582676Seschrock nvlist_free(nvl); 12592676Seschrock nvl = realprops; 12602676Seschrock 12612676Seschrock prop = zfs_name_to_prop(propname); 12622676Seschrock 12637366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 12642676Seschrock goto error; 1265789Sahrens 1266789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 12672082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12682082Seschrock "child dataset with inherited mountpoint is used " 12692082Seschrock "in a non-global zone")); 12702082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1271789Sahrens goto error; 1272789Sahrens } 1273789Sahrens 12747509SMark.Musante@Sun.COM /* 12757509SMark.Musante@Sun.COM * If the dataset's canmount property is being set to noauto, 12767509SMark.Musante@Sun.COM * then we want to prevent unmounting & remounting it. 12777509SMark.Musante@Sun.COM */ 12787509SMark.Musante@Sun.COM do_prefix = !((prop == ZFS_PROP_CANMOUNT) && 12797509SMark.Musante@Sun.COM (zprop_string_to_index(prop, propval, &idx, 12807509SMark.Musante@Sun.COM ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO)); 12816168Shs24103 12826168Shs24103 if (do_prefix && (ret = changelist_prefix(cl)) != 0) 12837509SMark.Musante@Sun.COM goto error; 1284789Sahrens 1285789Sahrens /* 1286789Sahrens * Execute the corresponding ioctl() to set this property. 1287789Sahrens */ 1288789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1289789Sahrens 12905094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 12912676Seschrock goto error; 12922676Seschrock 12934543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 12948845Samw@Sun.COM 1295789Sahrens if (ret != 0) { 1296789Sahrens switch (errno) { 1297789Sahrens 1298789Sahrens case ENOSPC: 1299789Sahrens /* 1300789Sahrens * For quotas and reservations, ENOSPC indicates 1301789Sahrens * something different; setting a quota or reservation 1302789Sahrens * doesn't use any disk space. 1303789Sahrens */ 1304789Sahrens switch (prop) { 1305789Sahrens case ZFS_PROP_QUOTA: 13065378Sck153898 case ZFS_PROP_REFQUOTA: 13072082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13082082Seschrock "size is less than current used or " 13092082Seschrock "reserved space")); 13102082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1311789Sahrens break; 1312789Sahrens 1313789Sahrens case ZFS_PROP_RESERVATION: 13145378Sck153898 case ZFS_PROP_REFRESERVATION: 13152082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13162082Seschrock "size is greater than available space")); 13172082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1318789Sahrens break; 1319789Sahrens 1320789Sahrens default: 13212082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1322789Sahrens break; 1323789Sahrens } 1324789Sahrens break; 1325789Sahrens 1326789Sahrens case EBUSY: 13272082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 13282082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 13292082Seschrock else 13302676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1331789Sahrens break; 1332789Sahrens 13331175Slling case EROFS: 13342082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 13351175Slling break; 13361175Slling 13373886Sahl case ENOTSUP: 13383886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13395977Smarks "pool and or dataset must be upgraded to set this " 13404603Sahrens "property or value")); 13413886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 13423886Sahl break; 13433886Sahl 13447042Sgw25295 case ERANGE: 13457042Sgw25295 if (prop == ZFS_PROP_COMPRESSION) { 13467042Sgw25295 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13477042Sgw25295 "property setting is not allowed on " 13487042Sgw25295 "bootable datasets")); 13497042Sgw25295 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 13507042Sgw25295 } else { 13517042Sgw25295 (void) zfs_standard_error(hdl, errno, errbuf); 13527042Sgw25295 } 13537042Sgw25295 break; 13547042Sgw25295 1355789Sahrens case EOVERFLOW: 1356789Sahrens /* 1357789Sahrens * This platform can't address a volume this big. 1358789Sahrens */ 1359789Sahrens #ifdef _ILP32 1360789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 13612082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1362789Sahrens break; 1363789Sahrens } 1364789Sahrens #endif 13652082Seschrock /* FALLTHROUGH */ 1366789Sahrens default: 13672082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1368789Sahrens } 1369789Sahrens } else { 13706168Shs24103 if (do_prefix) 13716168Shs24103 ret = changelist_postfix(cl); 13726168Shs24103 1373789Sahrens /* 1374789Sahrens * Refresh the statistics so the new property value 1375789Sahrens * is reflected. 1376789Sahrens */ 13776168Shs24103 if (ret == 0) 13782676Seschrock (void) get_stats(zhp); 1379789Sahrens } 1380789Sahrens 1381789Sahrens error: 13822676Seschrock nvlist_free(nvl); 13832676Seschrock zcmd_free_nvlists(&zc); 13842676Seschrock if (cl) 13852676Seschrock changelist_free(cl); 1386789Sahrens return (ret); 1387789Sahrens } 1388789Sahrens 1389789Sahrens /* 1390789Sahrens * Given a property, inherit the value from the parent dataset. 1391789Sahrens */ 1392789Sahrens int 13932676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1394789Sahrens { 1395789Sahrens zfs_cmd_t zc = { 0 }; 1396789Sahrens int ret; 1397789Sahrens prop_changelist_t *cl; 13982082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 13992082Seschrock char errbuf[1024]; 14002676Seschrock zfs_prop_t prop; 14012082Seschrock 14022082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 14032082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1404789Sahrens 14055094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 14062676Seschrock /* 14072676Seschrock * For user properties, the amount of work we have to do is very 14082676Seschrock * small, so just do it here. 14092676Seschrock */ 14102676Seschrock if (!zfs_prop_user(propname)) { 14112676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14122676Seschrock "invalid property")); 14132676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 14142676Seschrock } 14152676Seschrock 14162676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 14172676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 14182676Seschrock 14194849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 14202676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 14212676Seschrock 14222676Seschrock return (0); 14232676Seschrock } 14242676Seschrock 1425789Sahrens /* 1426789Sahrens * Verify that this property is inheritable. 1427789Sahrens */ 14282082Seschrock if (zfs_prop_readonly(prop)) 14292082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 14302082Seschrock 14312082Seschrock if (!zfs_prop_inheritable(prop)) 14322082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1433789Sahrens 1434789Sahrens /* 1435789Sahrens * Check to see if the value applies to this type 1436789Sahrens */ 14372082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 14382082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1439789Sahrens 14403443Srm160521 /* 14413443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 14423443Srm160521 */ 14433443Srm160521 propname = zfs_prop_to_name(prop); 1444789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 14452676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1446789Sahrens 1447789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1448789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 14492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14502082Seschrock "dataset is used in a non-global zone")); 14512082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1452789Sahrens } 1453789Sahrens 1454789Sahrens /* 1455789Sahrens * Determine datasets which will be affected by this change, if any. 1456789Sahrens */ 14577366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1458789Sahrens return (-1); 1459789Sahrens 1460789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 14612082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14622082Seschrock "child dataset with inherited mountpoint is used " 14632082Seschrock "in a non-global zone")); 14642082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1465789Sahrens goto error; 1466789Sahrens } 1467789Sahrens 1468789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1469789Sahrens goto error; 1470789Sahrens 14714849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 14722082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1473789Sahrens } else { 1474789Sahrens 14752169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1476789Sahrens goto error; 1477789Sahrens 1478789Sahrens /* 1479789Sahrens * Refresh the statistics so the new property is reflected. 1480789Sahrens */ 1481789Sahrens (void) get_stats(zhp); 1482789Sahrens } 1483789Sahrens 1484789Sahrens error: 1485789Sahrens changelist_free(cl); 1486789Sahrens return (ret); 1487789Sahrens } 1488789Sahrens 1489789Sahrens /* 14901356Seschrock * True DSL properties are stored in an nvlist. The following two functions 14911356Seschrock * extract them appropriately. 14921356Seschrock */ 14931356Seschrock static uint64_t 14941356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 14951356Seschrock { 14961356Seschrock nvlist_t *nv; 14971356Seschrock uint64_t value; 14981356Seschrock 14992885Sahrens *source = NULL; 15001356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 15011356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 15025094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 15035094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 15041356Seschrock } else { 15058802SSanjeev.Bagewadi@Sun.COM verify(!zhp->zfs_props_table || 15068802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table[prop] == B_TRUE); 15071356Seschrock value = zfs_prop_default_numeric(prop); 15081356Seschrock *source = ""; 15091356Seschrock } 15101356Seschrock 15111356Seschrock return (value); 15121356Seschrock } 15131356Seschrock 15141356Seschrock static char * 15151356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 15161356Seschrock { 15171356Seschrock nvlist_t *nv; 15181356Seschrock char *value; 15191356Seschrock 15202885Sahrens *source = NULL; 15211356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 15221356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 15235094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 15245094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 15251356Seschrock } else { 15268802SSanjeev.Bagewadi@Sun.COM verify(!zhp->zfs_props_table || 15278802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table[prop] == B_TRUE); 15281356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 15291356Seschrock value = ""; 15301356Seschrock *source = ""; 15311356Seschrock } 15321356Seschrock 15331356Seschrock return (value); 15341356Seschrock } 15351356Seschrock 15361356Seschrock /* 1537789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1538789Sahrens * zfs_prop_get_int() are built using this interface. 1539789Sahrens * 1540789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1541789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1542789Sahrens * If they differ from the on-disk values, report the current values and mark 1543789Sahrens * the source "temporary". 1544789Sahrens */ 15452082Seschrock static int 15465094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 15472082Seschrock char **source, uint64_t *val) 1548789Sahrens { 15495147Srm160521 zfs_cmd_t zc = { 0 }; 15505592Stimh nvlist_t *zplprops = NULL; 1551789Sahrens struct mnttab mnt; 15523265Sahrens char *mntopt_on = NULL; 15533265Sahrens char *mntopt_off = NULL; 1554789Sahrens 1555789Sahrens *source = NULL; 1556789Sahrens 15573265Sahrens switch (prop) { 15583265Sahrens case ZFS_PROP_ATIME: 15593265Sahrens mntopt_on = MNTOPT_ATIME; 15603265Sahrens mntopt_off = MNTOPT_NOATIME; 15613265Sahrens break; 15623265Sahrens 15633265Sahrens case ZFS_PROP_DEVICES: 15643265Sahrens mntopt_on = MNTOPT_DEVICES; 15653265Sahrens mntopt_off = MNTOPT_NODEVICES; 15663265Sahrens break; 15673265Sahrens 15683265Sahrens case ZFS_PROP_EXEC: 15693265Sahrens mntopt_on = MNTOPT_EXEC; 15703265Sahrens mntopt_off = MNTOPT_NOEXEC; 15713265Sahrens break; 15723265Sahrens 15733265Sahrens case ZFS_PROP_READONLY: 15743265Sahrens mntopt_on = MNTOPT_RO; 15753265Sahrens mntopt_off = MNTOPT_RW; 15763265Sahrens break; 15773265Sahrens 15783265Sahrens case ZFS_PROP_SETUID: 15793265Sahrens mntopt_on = MNTOPT_SETUID; 15803265Sahrens mntopt_off = MNTOPT_NOSETUID; 15813265Sahrens break; 15823265Sahrens 15833265Sahrens case ZFS_PROP_XATTR: 15843265Sahrens mntopt_on = MNTOPT_XATTR; 15853265Sahrens mntopt_off = MNTOPT_NOXATTR; 15863265Sahrens break; 15875331Samw 15885331Samw case ZFS_PROP_NBMAND: 15895331Samw mntopt_on = MNTOPT_NBMAND; 15905331Samw mntopt_off = MNTOPT_NONBMAND; 15915331Samw break; 15923265Sahrens } 15933265Sahrens 15942474Seschrock /* 15952474Seschrock * Because looking up the mount options is potentially expensive 15962474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 15972474Seschrock * we're looking up a property which requires its presence. 15982474Seschrock */ 15992474Seschrock if (!zhp->zfs_mntcheck && 16003265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 16018228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 16028228SEric.Taylor@Sun.COM struct mnttab entry; 16038228SEric.Taylor@Sun.COM 16048228SEric.Taylor@Sun.COM if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 16058228SEric.Taylor@Sun.COM zhp->zfs_mntopts = zfs_strdup(hdl, 16063265Sahrens entry.mnt_mntopts); 16073265Sahrens if (zhp->zfs_mntopts == NULL) 16083265Sahrens return (-1); 16093265Sahrens } 16102474Seschrock 16112474Seschrock zhp->zfs_mntcheck = B_TRUE; 16122474Seschrock } 16132474Seschrock 1614789Sahrens if (zhp->zfs_mntopts == NULL) 1615789Sahrens mnt.mnt_mntopts = ""; 1616789Sahrens else 1617789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1618789Sahrens 1619789Sahrens switch (prop) { 1620789Sahrens case ZFS_PROP_ATIME: 16213265Sahrens case ZFS_PROP_DEVICES: 16223265Sahrens case ZFS_PROP_EXEC: 16233265Sahrens case ZFS_PROP_READONLY: 16243265Sahrens case ZFS_PROP_SETUID: 16253265Sahrens case ZFS_PROP_XATTR: 16265331Samw case ZFS_PROP_NBMAND: 16272082Seschrock *val = getprop_uint64(zhp, prop, source); 16282082Seschrock 16293265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 16302082Seschrock *val = B_TRUE; 1631789Sahrens if (src) 16325094Slling *src = ZPROP_SRC_TEMPORARY; 16333265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 16342082Seschrock *val = B_FALSE; 1635789Sahrens if (src) 16365094Slling *src = ZPROP_SRC_TEMPORARY; 1637789Sahrens } 16382082Seschrock break; 1639789Sahrens 16403265Sahrens case ZFS_PROP_CANMOUNT: 16412082Seschrock *val = getprop_uint64(zhp, prop, source); 16426168Shs24103 if (*val != ZFS_CANMOUNT_ON) 16433417Srm160521 *source = zhp->zfs_name; 16443417Srm160521 else 16453417Srm160521 *source = ""; /* default */ 16462082Seschrock break; 1647789Sahrens 1648789Sahrens case ZFS_PROP_QUOTA: 16495378Sck153898 case ZFS_PROP_REFQUOTA: 1650789Sahrens case ZFS_PROP_RESERVATION: 16515378Sck153898 case ZFS_PROP_REFRESERVATION: 16522885Sahrens *val = getprop_uint64(zhp, prop, source); 16532885Sahrens if (*val == 0) 1654789Sahrens *source = ""; /* default */ 1655789Sahrens else 1656789Sahrens *source = zhp->zfs_name; 16572082Seschrock break; 1658789Sahrens 1659789Sahrens case ZFS_PROP_MOUNTED: 16602082Seschrock *val = (zhp->zfs_mntopts != NULL); 16612082Seschrock break; 1662789Sahrens 16633377Seschrock case ZFS_PROP_NUMCLONES: 16643377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 16653377Seschrock break; 16663377Seschrock 16675147Srm160521 case ZFS_PROP_VERSION: 16685498Stimh case ZFS_PROP_NORMALIZE: 16695498Stimh case ZFS_PROP_UTF8ONLY: 16705498Stimh case ZFS_PROP_CASE: 16715498Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 16725498Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 16735473Srm160521 return (-1); 16745147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 16755498Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 16765498Stimh zcmd_free_nvlists(&zc); 16775147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 16785498Stimh "unable to get %s property"), 16795498Stimh zfs_prop_to_name(prop)); 16805147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 16815147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 16825147Srm160521 } 16835498Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 16845498Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 16855498Stimh val) != 0) { 16865498Stimh zcmd_free_nvlists(&zc); 16875498Stimh zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 16885498Stimh "unable to get %s property"), 16895498Stimh zfs_prop_to_name(prop)); 16905498Stimh return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM, 16915498Stimh dgettext(TEXT_DOMAIN, "internal error"))); 16925498Stimh } 16935592Stimh if (zplprops) 16945592Stimh nvlist_free(zplprops); 16955498Stimh zcmd_free_nvlists(&zc); 16965147Srm160521 break; 16975147Srm160521 1698789Sahrens default: 16994577Sahrens switch (zfs_prop_get_type(prop)) { 17004787Sahrens case PROP_TYPE_NUMBER: 17014787Sahrens case PROP_TYPE_INDEX: 17024577Sahrens *val = getprop_uint64(zhp, prop, source); 17037390SMatthew.Ahrens@Sun.COM /* 17049396SMatthew.Ahrens@Sun.COM * If we tried to use a default value for a 17057390SMatthew.Ahrens@Sun.COM * readonly property, it means that it was not 17067390SMatthew.Ahrens@Sun.COM * present; return an error. 17077390SMatthew.Ahrens@Sun.COM */ 17087390SMatthew.Ahrens@Sun.COM if (zfs_prop_readonly(prop) && 17097390SMatthew.Ahrens@Sun.COM *source && (*source)[0] == '\0') { 17107390SMatthew.Ahrens@Sun.COM return (-1); 17117390SMatthew.Ahrens@Sun.COM } 17124577Sahrens break; 17134577Sahrens 17144787Sahrens case PROP_TYPE_STRING: 17154577Sahrens default: 17164577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 17174577Sahrens "cannot get non-numeric property")); 17184577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 17194577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 17204577Sahrens } 1721789Sahrens } 1722789Sahrens 1723789Sahrens return (0); 1724789Sahrens } 1725789Sahrens 1726789Sahrens /* 1727789Sahrens * Calculate the source type, given the raw source string. 1728789Sahrens */ 1729789Sahrens static void 17305094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1731789Sahrens char *statbuf, size_t statlen) 1732789Sahrens { 17335094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1734789Sahrens return; 1735789Sahrens 1736789Sahrens if (source == NULL) { 17375094Slling *srctype = ZPROP_SRC_NONE; 1738789Sahrens } else if (source[0] == '\0') { 17395094Slling *srctype = ZPROP_SRC_DEFAULT; 1740789Sahrens } else { 1741789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 17425094Slling *srctype = ZPROP_SRC_LOCAL; 1743789Sahrens } else { 1744789Sahrens (void) strlcpy(statbuf, source, statlen); 17455094Slling *srctype = ZPROP_SRC_INHERITED; 1746789Sahrens } 1747789Sahrens } 1748789Sahrens 1749789Sahrens } 1750789Sahrens 1751789Sahrens /* 1752789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 1753789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 1754789Sahrens * human-readable form. 1755789Sahrens * 1756789Sahrens * Returns 0 on success, or -1 on error. 1757789Sahrens */ 1758789Sahrens int 1759789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 17605094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 1761789Sahrens { 1762789Sahrens char *source = NULL; 1763789Sahrens uint64_t val; 1764789Sahrens char *str; 17652676Seschrock const char *strval; 1766789Sahrens 1767789Sahrens /* 1768789Sahrens * Check to see if this property applies to our object 1769789Sahrens */ 1770789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1771789Sahrens return (-1); 1772789Sahrens 1773789Sahrens if (src) 17745094Slling *src = ZPROP_SRC_NONE; 1775789Sahrens 1776789Sahrens switch (prop) { 1777789Sahrens case ZFS_PROP_CREATION: 1778789Sahrens /* 1779789Sahrens * 'creation' is a time_t stored in the statistics. We convert 1780789Sahrens * this into a string unless 'literal' is specified. 1781789Sahrens */ 1782789Sahrens { 17832885Sahrens val = getprop_uint64(zhp, prop, &source); 17842885Sahrens time_t time = (time_t)val; 1785789Sahrens struct tm t; 1786789Sahrens 1787789Sahrens if (literal || 1788789Sahrens localtime_r(&time, &t) == NULL || 1789789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 1790789Sahrens &t) == 0) 17912885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 1792789Sahrens } 1793789Sahrens break; 1794789Sahrens 1795789Sahrens case ZFS_PROP_MOUNTPOINT: 1796789Sahrens /* 1797789Sahrens * Getting the precise mountpoint can be tricky. 1798789Sahrens * 1799789Sahrens * - for 'none' or 'legacy', return those values. 1800789Sahrens * - for inherited mountpoints, we want to take everything 1801789Sahrens * after our ancestor and append it to the inherited value. 1802789Sahrens * 1803789Sahrens * If the pool has an alternate root, we want to prepend that 1804789Sahrens * root to any values we return. 1805789Sahrens */ 18066865Srm160521 18071356Seschrock str = getprop_string(zhp, prop, &source); 18081356Seschrock 18096612Sgw25295 if (str[0] == '/') { 18106865Srm160521 char buf[MAXPATHLEN]; 18116865Srm160521 char *root = buf; 18121356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 1813789Sahrens 1814789Sahrens if (relpath[0] == '/') 1815789Sahrens relpath++; 18166612Sgw25295 18176865Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 18186865Srm160521 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) || 18196865Srm160521 (strcmp(root, "-") == 0)) 18206865Srm160521 root[0] = '\0'; 18216612Sgw25295 /* 18226612Sgw25295 * Special case an alternate root of '/'. This will 18236612Sgw25295 * avoid having multiple leading slashes in the 18246612Sgw25295 * mountpoint path. 18256612Sgw25295 */ 18266612Sgw25295 if (strcmp(root, "/") == 0) 18276612Sgw25295 root++; 18286612Sgw25295 18296612Sgw25295 /* 18306612Sgw25295 * If the mountpoint is '/' then skip over this 18316612Sgw25295 * if we are obtaining either an alternate root or 18326612Sgw25295 * an inherited mountpoint. 18336612Sgw25295 */ 18346612Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 18356612Sgw25295 relpath[0] != '\0')) 18361356Seschrock str++; 1837789Sahrens 1838789Sahrens if (relpath[0] == '\0') 1839789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 18401356Seschrock root, str); 1841789Sahrens else 1842789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 18431356Seschrock root, str, relpath[0] == '@' ? "" : "/", 1844789Sahrens relpath); 1845789Sahrens } else { 1846789Sahrens /* 'legacy' or 'none' */ 18471356Seschrock (void) strlcpy(propbuf, str, proplen); 1848789Sahrens } 1849789Sahrens 1850789Sahrens break; 1851789Sahrens 1852789Sahrens case ZFS_PROP_ORIGIN: 18532885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 1854789Sahrens proplen); 1855789Sahrens /* 1856789Sahrens * If there is no parent at all, return failure to indicate that 1857789Sahrens * it doesn't apply to this dataset. 1858789Sahrens */ 1859789Sahrens if (propbuf[0] == '\0') 1860789Sahrens return (-1); 1861789Sahrens break; 1862789Sahrens 1863789Sahrens case ZFS_PROP_QUOTA: 18645378Sck153898 case ZFS_PROP_REFQUOTA: 1865789Sahrens case ZFS_PROP_RESERVATION: 18665378Sck153898 case ZFS_PROP_REFRESERVATION: 18675378Sck153898 18682082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 18692082Seschrock return (-1); 1870789Sahrens 1871789Sahrens /* 1872789Sahrens * If quota or reservation is 0, we translate this into 'none' 1873789Sahrens * (unless literal is set), and indicate that it's the default 1874789Sahrens * value. Otherwise, we print the number nicely and indicate 1875789Sahrens * that its set locally. 1876789Sahrens */ 1877789Sahrens if (val == 0) { 1878789Sahrens if (literal) 1879789Sahrens (void) strlcpy(propbuf, "0", proplen); 1880789Sahrens else 1881789Sahrens (void) strlcpy(propbuf, "none", proplen); 1882789Sahrens } else { 1883789Sahrens if (literal) 18842856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 18853912Slling (u_longlong_t)val); 1886789Sahrens else 1887789Sahrens zfs_nicenum(val, propbuf, proplen); 1888789Sahrens } 1889789Sahrens break; 1890789Sahrens 1891789Sahrens case ZFS_PROP_COMPRESSRATIO: 18922082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 18932082Seschrock return (-1); 18942856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 18952856Snd150628 val / 100, (longlong_t)val % 100); 1896789Sahrens break; 1897789Sahrens 1898789Sahrens case ZFS_PROP_TYPE: 1899789Sahrens switch (zhp->zfs_type) { 1900789Sahrens case ZFS_TYPE_FILESYSTEM: 1901789Sahrens str = "filesystem"; 1902789Sahrens break; 1903789Sahrens case ZFS_TYPE_VOLUME: 1904789Sahrens str = "volume"; 1905789Sahrens break; 1906789Sahrens case ZFS_TYPE_SNAPSHOT: 1907789Sahrens str = "snapshot"; 1908789Sahrens break; 1909789Sahrens default: 19102082Seschrock abort(); 1911789Sahrens } 1912789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 1913789Sahrens break; 1914789Sahrens 1915789Sahrens case ZFS_PROP_MOUNTED: 1916789Sahrens /* 1917789Sahrens * The 'mounted' property is a pseudo-property that described 1918789Sahrens * whether the filesystem is currently mounted. Even though 1919789Sahrens * it's a boolean value, the typical values of "on" and "off" 1920789Sahrens * don't make sense, so we translate to "yes" and "no". 1921789Sahrens */ 19222082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 19232082Seschrock src, &source, &val) != 0) 19242082Seschrock return (-1); 19252082Seschrock if (val) 1926789Sahrens (void) strlcpy(propbuf, "yes", proplen); 1927789Sahrens else 1928789Sahrens (void) strlcpy(propbuf, "no", proplen); 1929789Sahrens break; 1930789Sahrens 1931789Sahrens case ZFS_PROP_NAME: 1932789Sahrens /* 1933789Sahrens * The 'name' property is a pseudo-property derived from the 1934789Sahrens * dataset name. It is presented as a real property to simplify 1935789Sahrens * consumers. 1936789Sahrens */ 1937789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 1938789Sahrens break; 1939789Sahrens 1940789Sahrens default: 19414577Sahrens switch (zfs_prop_get_type(prop)) { 19424787Sahrens case PROP_TYPE_NUMBER: 19434577Sahrens if (get_numeric_property(zhp, prop, src, 19444577Sahrens &source, &val) != 0) 19454577Sahrens return (-1); 19464577Sahrens if (literal) 19474577Sahrens (void) snprintf(propbuf, proplen, "%llu", 19484577Sahrens (u_longlong_t)val); 19494577Sahrens else 19504577Sahrens zfs_nicenum(val, propbuf, proplen); 19514577Sahrens break; 19524577Sahrens 19534787Sahrens case PROP_TYPE_STRING: 19544577Sahrens (void) strlcpy(propbuf, 19554577Sahrens getprop_string(zhp, prop, &source), proplen); 19564577Sahrens break; 19574577Sahrens 19584787Sahrens case PROP_TYPE_INDEX: 19594861Sahrens if (get_numeric_property(zhp, prop, src, 19604861Sahrens &source, &val) != 0) 19614861Sahrens return (-1); 19624861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 19634577Sahrens return (-1); 19644577Sahrens (void) strlcpy(propbuf, strval, proplen); 19654577Sahrens break; 19664577Sahrens 19674577Sahrens default: 19684577Sahrens abort(); 19694577Sahrens } 1970789Sahrens } 1971789Sahrens 1972789Sahrens get_source(zhp, src, source, statbuf, statlen); 1973789Sahrens 1974789Sahrens return (0); 1975789Sahrens } 1976789Sahrens 1977789Sahrens /* 1978789Sahrens * Utility function to get the given numeric property. Does no validation that 1979789Sahrens * the given property is the appropriate type; should only be used with 1980789Sahrens * hard-coded property types. 1981789Sahrens */ 1982789Sahrens uint64_t 1983789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 1984789Sahrens { 1985789Sahrens char *source; 19862082Seschrock uint64_t val; 19872082Seschrock 19885367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 19892082Seschrock 19902082Seschrock return (val); 1991789Sahrens } 1992789Sahrens 19935713Srm160521 int 19945713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 19955713Srm160521 { 19965713Srm160521 char buf[64]; 19975713Srm160521 19989396SMatthew.Ahrens@Sun.COM (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 19995713Srm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 20005713Srm160521 } 20015713Srm160521 2002789Sahrens /* 2003789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2004789Sahrens */ 2005789Sahrens int 2006789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 20075094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2008789Sahrens { 2009789Sahrens char *source; 2010789Sahrens 2011789Sahrens /* 2012789Sahrens * Check to see if this property applies to our object 2013789Sahrens */ 20144849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 20153237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 20162082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 20172082Seschrock zfs_prop_to_name(prop))); 20184849Sahrens } 2019789Sahrens 2020789Sahrens if (src) 20215094Slling *src = ZPROP_SRC_NONE; 2022789Sahrens 20232082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 20242082Seschrock return (-1); 2025789Sahrens 2026789Sahrens get_source(zhp, src, source, statbuf, statlen); 2027789Sahrens 2028789Sahrens return (0); 2029789Sahrens } 2030789Sahrens 20319396SMatthew.Ahrens@Sun.COM static int 20329396SMatthew.Ahrens@Sun.COM idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 20339396SMatthew.Ahrens@Sun.COM char **domainp, idmap_rid_t *ridp) 20349396SMatthew.Ahrens@Sun.COM { 20359396SMatthew.Ahrens@Sun.COM idmap_handle_t *idmap_hdl = NULL; 20369396SMatthew.Ahrens@Sun.COM idmap_get_handle_t *get_hdl = NULL; 20379396SMatthew.Ahrens@Sun.COM idmap_stat status; 20389396SMatthew.Ahrens@Sun.COM int err = EINVAL; 20399396SMatthew.Ahrens@Sun.COM 20409396SMatthew.Ahrens@Sun.COM if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS) 20419396SMatthew.Ahrens@Sun.COM goto out; 20429396SMatthew.Ahrens@Sun.COM if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS) 20439396SMatthew.Ahrens@Sun.COM goto out; 20449396SMatthew.Ahrens@Sun.COM 20459396SMatthew.Ahrens@Sun.COM if (isuser) { 20469396SMatthew.Ahrens@Sun.COM err = idmap_get_sidbyuid(get_hdl, id, 20479396SMatthew.Ahrens@Sun.COM IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 20489396SMatthew.Ahrens@Sun.COM } else { 20499396SMatthew.Ahrens@Sun.COM err = idmap_get_sidbygid(get_hdl, id, 20509396SMatthew.Ahrens@Sun.COM IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 20519396SMatthew.Ahrens@Sun.COM } 20529396SMatthew.Ahrens@Sun.COM if (err == IDMAP_SUCCESS && 20539396SMatthew.Ahrens@Sun.COM idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 20549396SMatthew.Ahrens@Sun.COM status == IDMAP_SUCCESS) 20559396SMatthew.Ahrens@Sun.COM err = 0; 20569396SMatthew.Ahrens@Sun.COM else 20579396SMatthew.Ahrens@Sun.COM err = EINVAL; 20589396SMatthew.Ahrens@Sun.COM out: 20599396SMatthew.Ahrens@Sun.COM if (get_hdl) 20609396SMatthew.Ahrens@Sun.COM idmap_get_destroy(get_hdl); 20619396SMatthew.Ahrens@Sun.COM if (idmap_hdl) 20629396SMatthew.Ahrens@Sun.COM (void) idmap_fini(idmap_hdl); 20639396SMatthew.Ahrens@Sun.COM return (err); 20649396SMatthew.Ahrens@Sun.COM } 20659396SMatthew.Ahrens@Sun.COM 20669396SMatthew.Ahrens@Sun.COM /* 20679396SMatthew.Ahrens@Sun.COM * convert the propname into parameters needed by kernel 20689396SMatthew.Ahrens@Sun.COM * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 20699396SMatthew.Ahrens@Sun.COM * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 20709396SMatthew.Ahrens@Sun.COM */ 20719396SMatthew.Ahrens@Sun.COM static int 20729396SMatthew.Ahrens@Sun.COM userquota_propname_decode(const char *propname, boolean_t zoned, 20739396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 20749396SMatthew.Ahrens@Sun.COM { 20759396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t type; 20769396SMatthew.Ahrens@Sun.COM char *cp, *end; 20779396SMatthew.Ahrens@Sun.COM boolean_t isuser; 20789396SMatthew.Ahrens@Sun.COM 20799396SMatthew.Ahrens@Sun.COM domain[0] = '\0'; 20809396SMatthew.Ahrens@Sun.COM 20819396SMatthew.Ahrens@Sun.COM /* Figure out the property type ({user|group}{quota|space}) */ 20829396SMatthew.Ahrens@Sun.COM for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 20839396SMatthew.Ahrens@Sun.COM if (strncmp(propname, zfs_userquota_prop_prefixes[type], 20849396SMatthew.Ahrens@Sun.COM strlen(zfs_userquota_prop_prefixes[type])) == 0) 20859396SMatthew.Ahrens@Sun.COM break; 20869396SMatthew.Ahrens@Sun.COM } 20879396SMatthew.Ahrens@Sun.COM if (type == ZFS_NUM_USERQUOTA_PROPS) 20889396SMatthew.Ahrens@Sun.COM return (EINVAL); 20899396SMatthew.Ahrens@Sun.COM *typep = type; 20909396SMatthew.Ahrens@Sun.COM 20919396SMatthew.Ahrens@Sun.COM isuser = (type == ZFS_PROP_USERQUOTA || 20929396SMatthew.Ahrens@Sun.COM type == ZFS_PROP_USERUSED); 20939396SMatthew.Ahrens@Sun.COM 20949396SMatthew.Ahrens@Sun.COM cp = strchr(propname, '@') + 1; 20959396SMatthew.Ahrens@Sun.COM 20969396SMatthew.Ahrens@Sun.COM if (strchr(cp, '@')) { 20979396SMatthew.Ahrens@Sun.COM /* 20989396SMatthew.Ahrens@Sun.COM * It's a SID name (eg "user@domain") that needs to be 20999396SMatthew.Ahrens@Sun.COM * turned into S-1-domainID-RID. There should be a 21009396SMatthew.Ahrens@Sun.COM * better way to do this, but for now just translate it 21019396SMatthew.Ahrens@Sun.COM * to the (possibly ephemeral) uid and then back to the 21029396SMatthew.Ahrens@Sun.COM * SID. This is like getsidname(noresolve=TRUE). 21039396SMatthew.Ahrens@Sun.COM */ 21049396SMatthew.Ahrens@Sun.COM uid_t id; 21059396SMatthew.Ahrens@Sun.COM idmap_rid_t rid; 21069396SMatthew.Ahrens@Sun.COM char *mapdomain; 21079396SMatthew.Ahrens@Sun.COM 21089396SMatthew.Ahrens@Sun.COM if (zoned && getzoneid() == GLOBAL_ZONEID) 21099396SMatthew.Ahrens@Sun.COM return (ENOENT); 21109396SMatthew.Ahrens@Sun.COM if (sid_to_id(cp, isuser, &id) != 0) 21119396SMatthew.Ahrens@Sun.COM return (ENOENT); 21129396SMatthew.Ahrens@Sun.COM if (idmap_id_to_numeric_domain_rid(id, isuser, 21139396SMatthew.Ahrens@Sun.COM &mapdomain, &rid) != 0) 21149396SMatthew.Ahrens@Sun.COM return (ENOENT); 21159396SMatthew.Ahrens@Sun.COM (void) strlcpy(domain, mapdomain, domainlen); 21169396SMatthew.Ahrens@Sun.COM *ridp = rid; 21179396SMatthew.Ahrens@Sun.COM } else if (strncmp(cp, "S-1-", 4) == 0) { 21189396SMatthew.Ahrens@Sun.COM /* It's a numeric SID (eg "S-1-234-567-89") */ 21199396SMatthew.Ahrens@Sun.COM (void) strcpy(domain, cp); 21209396SMatthew.Ahrens@Sun.COM cp = strrchr(domain, '-'); 21219396SMatthew.Ahrens@Sun.COM *cp = '\0'; 21229396SMatthew.Ahrens@Sun.COM cp++; 21239396SMatthew.Ahrens@Sun.COM 21249396SMatthew.Ahrens@Sun.COM errno = 0; 21259396SMatthew.Ahrens@Sun.COM *ridp = strtoull(cp, &end, 10); 21269396SMatthew.Ahrens@Sun.COM if (errno == 0 || *end != '\0') 21279396SMatthew.Ahrens@Sun.COM return (EINVAL); 21289396SMatthew.Ahrens@Sun.COM } else if (!isdigit(*cp)) { 21299396SMatthew.Ahrens@Sun.COM /* 21309396SMatthew.Ahrens@Sun.COM * It's a user/group name (eg "user") that needs to be 21319396SMatthew.Ahrens@Sun.COM * turned into a uid/gid 21329396SMatthew.Ahrens@Sun.COM */ 21339396SMatthew.Ahrens@Sun.COM if (zoned && getzoneid() == GLOBAL_ZONEID) 21349396SMatthew.Ahrens@Sun.COM return (ENOENT); 21359396SMatthew.Ahrens@Sun.COM if (isuser) { 21369396SMatthew.Ahrens@Sun.COM struct passwd *pw; 21379396SMatthew.Ahrens@Sun.COM pw = getpwnam(cp); 21389396SMatthew.Ahrens@Sun.COM if (pw == NULL) 21399396SMatthew.Ahrens@Sun.COM return (ENOENT); 21409396SMatthew.Ahrens@Sun.COM *ridp = pw->pw_uid; 21419396SMatthew.Ahrens@Sun.COM } else { 21429396SMatthew.Ahrens@Sun.COM struct group *gr; 21439396SMatthew.Ahrens@Sun.COM gr = getgrnam(cp); 21449396SMatthew.Ahrens@Sun.COM if (gr == NULL) 21459396SMatthew.Ahrens@Sun.COM return (ENOENT); 21469396SMatthew.Ahrens@Sun.COM *ridp = gr->gr_gid; 21479396SMatthew.Ahrens@Sun.COM } 21489396SMatthew.Ahrens@Sun.COM } else { 21499396SMatthew.Ahrens@Sun.COM /* It's a user/group ID (eg "12345"). */ 21509396SMatthew.Ahrens@Sun.COM uid_t id = strtoul(cp, &end, 10); 21519396SMatthew.Ahrens@Sun.COM idmap_rid_t rid; 21529396SMatthew.Ahrens@Sun.COM char *mapdomain; 21539396SMatthew.Ahrens@Sun.COM 21549396SMatthew.Ahrens@Sun.COM if (*end != '\0') 21559396SMatthew.Ahrens@Sun.COM return (EINVAL); 21569396SMatthew.Ahrens@Sun.COM if (id > MAXUID) { 21579396SMatthew.Ahrens@Sun.COM /* It's an ephemeral ID. */ 21589396SMatthew.Ahrens@Sun.COM if (idmap_id_to_numeric_domain_rid(id, isuser, 21599396SMatthew.Ahrens@Sun.COM &mapdomain, &rid) != 0) 21609396SMatthew.Ahrens@Sun.COM return (ENOENT); 21619396SMatthew.Ahrens@Sun.COM (void) strcpy(domain, mapdomain); 21629396SMatthew.Ahrens@Sun.COM *ridp = rid; 21639396SMatthew.Ahrens@Sun.COM } else { 21649396SMatthew.Ahrens@Sun.COM *ridp = id; 21659396SMatthew.Ahrens@Sun.COM } 21669396SMatthew.Ahrens@Sun.COM } 21679396SMatthew.Ahrens@Sun.COM 21689396SMatthew.Ahrens@Sun.COM return (0); 21699396SMatthew.Ahrens@Sun.COM } 21709396SMatthew.Ahrens@Sun.COM 21719469SLin.Ling@Sun.COM static int 21729469SLin.Ling@Sun.COM zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 21739469SLin.Ling@Sun.COM uint64_t *propvalue, zfs_userquota_prop_t *typep) 21749396SMatthew.Ahrens@Sun.COM { 21759396SMatthew.Ahrens@Sun.COM int err; 21769396SMatthew.Ahrens@Sun.COM zfs_cmd_t zc = { 0 }; 21779396SMatthew.Ahrens@Sun.COM 21789396SMatthew.Ahrens@Sun.COM (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21799396SMatthew.Ahrens@Sun.COM 21809396SMatthew.Ahrens@Sun.COM err = userquota_propname_decode(propname, 21819396SMatthew.Ahrens@Sun.COM zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 21829469SLin.Ling@Sun.COM typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 21839469SLin.Ling@Sun.COM zc.zc_objset_type = *typep; 21849396SMatthew.Ahrens@Sun.COM if (err) 21859396SMatthew.Ahrens@Sun.COM return (err); 21869396SMatthew.Ahrens@Sun.COM 21879396SMatthew.Ahrens@Sun.COM err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 21889396SMatthew.Ahrens@Sun.COM if (err) 21899396SMatthew.Ahrens@Sun.COM return (err); 21909396SMatthew.Ahrens@Sun.COM 21919469SLin.Ling@Sun.COM *propvalue = zc.zc_cookie; 21929469SLin.Ling@Sun.COM return (0); 21939469SLin.Ling@Sun.COM } 21949469SLin.Ling@Sun.COM 21959469SLin.Ling@Sun.COM int 21969469SLin.Ling@Sun.COM zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 21979469SLin.Ling@Sun.COM uint64_t *propvalue) 21989469SLin.Ling@Sun.COM { 21999469SLin.Ling@Sun.COM zfs_userquota_prop_t type; 22009469SLin.Ling@Sun.COM 22019469SLin.Ling@Sun.COM return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 22029469SLin.Ling@Sun.COM &type)); 22039469SLin.Ling@Sun.COM } 22049469SLin.Ling@Sun.COM 22059469SLin.Ling@Sun.COM int 22069469SLin.Ling@Sun.COM zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 22079469SLin.Ling@Sun.COM char *propbuf, int proplen, boolean_t literal) 22089469SLin.Ling@Sun.COM { 22099469SLin.Ling@Sun.COM int err; 22109469SLin.Ling@Sun.COM uint64_t propvalue; 22119469SLin.Ling@Sun.COM zfs_userquota_prop_t type; 22129469SLin.Ling@Sun.COM 22139469SLin.Ling@Sun.COM err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 22149469SLin.Ling@Sun.COM &type); 22159469SLin.Ling@Sun.COM 22169469SLin.Ling@Sun.COM if (err) 22179469SLin.Ling@Sun.COM return (err); 22189469SLin.Ling@Sun.COM 22199396SMatthew.Ahrens@Sun.COM if (literal) { 22209469SLin.Ling@Sun.COM (void) snprintf(propbuf, proplen, "%llu", propvalue); 22219469SLin.Ling@Sun.COM } else if (propvalue == 0 && 22229396SMatthew.Ahrens@Sun.COM (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) { 22239396SMatthew.Ahrens@Sun.COM (void) strlcpy(propbuf, "none", proplen); 22249396SMatthew.Ahrens@Sun.COM } else { 22259469SLin.Ling@Sun.COM zfs_nicenum(propvalue, propbuf, proplen); 22269396SMatthew.Ahrens@Sun.COM } 22279396SMatthew.Ahrens@Sun.COM return (0); 22289396SMatthew.Ahrens@Sun.COM } 22299396SMatthew.Ahrens@Sun.COM 2230789Sahrens /* 2231789Sahrens * Returns the name of the given zfs handle. 2232789Sahrens */ 2233789Sahrens const char * 2234789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2235789Sahrens { 2236789Sahrens return (zhp->zfs_name); 2237789Sahrens } 2238789Sahrens 2239789Sahrens /* 2240789Sahrens * Returns the type of the given zfs handle. 2241789Sahrens */ 2242789Sahrens zfs_type_t 2243789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2244789Sahrens { 2245789Sahrens return (zhp->zfs_type); 2246789Sahrens } 2247789Sahrens 22488228SEric.Taylor@Sun.COM static int 22498228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc) 22508228SEric.Taylor@Sun.COM { 22518228SEric.Taylor@Sun.COM int rc; 22528228SEric.Taylor@Sun.COM uint64_t orig_cookie; 22538228SEric.Taylor@Sun.COM 22548228SEric.Taylor@Sun.COM orig_cookie = zc->zc_cookie; 22558228SEric.Taylor@Sun.COM top: 22568228SEric.Taylor@Sun.COM (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 22578228SEric.Taylor@Sun.COM rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc); 22588228SEric.Taylor@Sun.COM 22598228SEric.Taylor@Sun.COM if (rc == -1) { 22608228SEric.Taylor@Sun.COM switch (errno) { 22618228SEric.Taylor@Sun.COM case ENOMEM: 22628228SEric.Taylor@Sun.COM /* expand nvlist memory and try again */ 22638228SEric.Taylor@Sun.COM if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) { 22648228SEric.Taylor@Sun.COM zcmd_free_nvlists(zc); 22658228SEric.Taylor@Sun.COM return (-1); 22668228SEric.Taylor@Sun.COM } 22678228SEric.Taylor@Sun.COM zc->zc_cookie = orig_cookie; 22688228SEric.Taylor@Sun.COM goto top; 22698228SEric.Taylor@Sun.COM /* 22708228SEric.Taylor@Sun.COM * An errno value of ESRCH indicates normal completion. 22718228SEric.Taylor@Sun.COM * If ENOENT is returned, then the underlying dataset 22728228SEric.Taylor@Sun.COM * has been removed since we obtained the handle. 22738228SEric.Taylor@Sun.COM */ 22748228SEric.Taylor@Sun.COM case ESRCH: 22758228SEric.Taylor@Sun.COM case ENOENT: 22768228SEric.Taylor@Sun.COM rc = 1; 22778228SEric.Taylor@Sun.COM break; 22788228SEric.Taylor@Sun.COM default: 22798228SEric.Taylor@Sun.COM rc = zfs_standard_error(zhp->zfs_hdl, errno, 22808228SEric.Taylor@Sun.COM dgettext(TEXT_DOMAIN, 22818228SEric.Taylor@Sun.COM "cannot iterate filesystems")); 22828228SEric.Taylor@Sun.COM break; 22838228SEric.Taylor@Sun.COM } 22848228SEric.Taylor@Sun.COM } 22858228SEric.Taylor@Sun.COM return (rc); 22868228SEric.Taylor@Sun.COM } 22878228SEric.Taylor@Sun.COM 2288789Sahrens /* 22891356Seschrock * Iterate over all child filesystems 2290789Sahrens */ 2291789Sahrens int 22921356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2293789Sahrens { 2294789Sahrens zfs_cmd_t zc = { 0 }; 2295789Sahrens zfs_handle_t *nzhp; 2296789Sahrens int ret; 2297789Sahrens 22985367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 22995367Sahrens return (0); 23005367Sahrens 23018228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 23028228SEric.Taylor@Sun.COM return (-1); 23038228SEric.Taylor@Sun.COM 23048228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT, 23058228SEric.Taylor@Sun.COM &zc)) == 0) { 2306789Sahrens /* 2307789Sahrens * Silently ignore errors, as the only plausible explanation is 2308789Sahrens * that the pool has since been removed. 2309789Sahrens */ 23108228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 23118228SEric.Taylor@Sun.COM &zc)) == NULL) { 2312789Sahrens continue; 23138228SEric.Taylor@Sun.COM } 23148228SEric.Taylor@Sun.COM 23158228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 23168228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2317789Sahrens return (ret); 23188228SEric.Taylor@Sun.COM } 2319789Sahrens } 23208228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 23218228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 23221356Seschrock } 23231356Seschrock 23241356Seschrock /* 23251356Seschrock * Iterate over all snapshots 23261356Seschrock */ 23271356Seschrock int 23281356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23291356Seschrock { 23301356Seschrock zfs_cmd_t zc = { 0 }; 23311356Seschrock zfs_handle_t *nzhp; 23321356Seschrock int ret; 2333789Sahrens 23345367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 23355367Sahrens return (0); 23365367Sahrens 23378228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 23388228SEric.Taylor@Sun.COM return (-1); 23398228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, 23408228SEric.Taylor@Sun.COM &zc)) == 0) { 23418228SEric.Taylor@Sun.COM 23428228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 23438228SEric.Taylor@Sun.COM &zc)) == NULL) { 2344789Sahrens continue; 23458228SEric.Taylor@Sun.COM } 23468228SEric.Taylor@Sun.COM 23478228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 23488228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2349789Sahrens return (ret); 23508228SEric.Taylor@Sun.COM } 2351789Sahrens } 23528228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 23538228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 2354789Sahrens } 2355789Sahrens 2356789Sahrens /* 23571356Seschrock * Iterate over all children, snapshots and filesystems 23581356Seschrock */ 23591356Seschrock int 23601356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23611356Seschrock { 23621356Seschrock int ret; 23631356Seschrock 23641356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 23651356Seschrock return (ret); 23661356Seschrock 23671356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 23681356Seschrock } 23691356Seschrock 23701356Seschrock /* 2371789Sahrens * Given a complete name, return just the portion that refers to the parent. 2372789Sahrens * Can return NULL if this is a pool. 2373789Sahrens */ 2374789Sahrens static int 2375789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2376789Sahrens { 2377789Sahrens char *loc; 2378789Sahrens 2379789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2380789Sahrens return (-1); 2381789Sahrens 2382789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2383789Sahrens buf[loc - path] = '\0'; 2384789Sahrens 2385789Sahrens return (0); 2386789Sahrens } 2387789Sahrens 2388789Sahrens /* 23894490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 23904490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 23914490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 23924490Svb160487 * length of already existing prefix of the given path. We also fetch the 23934490Svb160487 * 'zoned' property, which is used to validate property settings when creating 23944490Svb160487 * new datasets. 2395789Sahrens */ 2396789Sahrens static int 23974490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 23984490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2399789Sahrens { 2400789Sahrens zfs_cmd_t zc = { 0 }; 2401789Sahrens char parent[ZFS_MAXNAMELEN]; 2402789Sahrens char *slash; 24031356Seschrock zfs_handle_t *zhp; 24042082Seschrock char errbuf[1024]; 24052082Seschrock 24068269SMark.Musante@Sun.COM (void) snprintf(errbuf, sizeof (errbuf), 24078269SMark.Musante@Sun.COM dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 2408789Sahrens 2409789Sahrens /* get parent, and check to see if this is just a pool */ 2410789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 24112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24122082Seschrock "missing dataset name")); 24132082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2414789Sahrens } 2415789Sahrens 2416789Sahrens /* check to see if the pool exists */ 2417789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2418789Sahrens slash = parent + strlen(parent); 2419789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2420789Sahrens zc.zc_name[slash - parent] = '\0'; 24212082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2422789Sahrens errno == ENOENT) { 24232082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24242082Seschrock "no such pool '%s'"), zc.zc_name); 24252082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2426789Sahrens } 2427789Sahrens 2428789Sahrens /* check to see if the parent dataset exists */ 24294490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 24304490Svb160487 if (errno == ENOENT && accept_ancestor) { 24314490Svb160487 /* 24324490Svb160487 * Go deeper to find an ancestor, give up on top level. 24334490Svb160487 */ 24344490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 24354490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24364490Svb160487 "no such pool '%s'"), zc.zc_name); 24374490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24384490Svb160487 } 24394490Svb160487 } else if (errno == ENOENT) { 24402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24412082Seschrock "parent does not exist")); 24422082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24434490Svb160487 } else 24442082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2445789Sahrens } 2446789Sahrens 24472676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2448789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 24492676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 24502082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 24511356Seschrock zfs_close(zhp); 2452789Sahrens return (-1); 2453789Sahrens } 2454789Sahrens 2455789Sahrens /* make sure parent is a filesystem */ 24561356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 24572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24582082Seschrock "parent is not a filesystem")); 24592082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 24601356Seschrock zfs_close(zhp); 2461789Sahrens return (-1); 2462789Sahrens } 2463789Sahrens 24641356Seschrock zfs_close(zhp); 24654490Svb160487 if (prefixlen != NULL) 24664490Svb160487 *prefixlen = strlen(parent); 24674490Svb160487 return (0); 24684490Svb160487 } 24694490Svb160487 24704490Svb160487 /* 24714490Svb160487 * Finds whether the dataset of the given type(s) exists. 24724490Svb160487 */ 24734490Svb160487 boolean_t 24744490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 24754490Svb160487 { 24764490Svb160487 zfs_handle_t *zhp; 24774490Svb160487 24785326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 24794490Svb160487 return (B_FALSE); 24804490Svb160487 24814490Svb160487 /* 24824490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 24834490Svb160487 */ 24844490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 24854490Svb160487 int ds_type = zhp->zfs_type; 24864490Svb160487 24874490Svb160487 zfs_close(zhp); 24884490Svb160487 if (types & ds_type) 24894490Svb160487 return (B_TRUE); 24904490Svb160487 } 24914490Svb160487 return (B_FALSE); 24924490Svb160487 } 24934490Svb160487 24944490Svb160487 /* 24955367Sahrens * Given a path to 'target', create all the ancestors between 24965367Sahrens * the prefixlen portion of the path, and the target itself. 24975367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 24985367Sahrens */ 24995367Sahrens int 25005367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 25015367Sahrens { 25025367Sahrens zfs_handle_t *h; 25035367Sahrens char *cp; 25045367Sahrens const char *opname; 25055367Sahrens 25065367Sahrens /* make sure prefix exists */ 25075367Sahrens cp = target + prefixlen; 25085367Sahrens if (*cp != '/') { 25095367Sahrens assert(strchr(cp, '/') == NULL); 25105367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25115367Sahrens } else { 25125367Sahrens *cp = '\0'; 25135367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25145367Sahrens *cp = '/'; 25155367Sahrens } 25165367Sahrens if (h == NULL) 25175367Sahrens return (-1); 25185367Sahrens zfs_close(h); 25195367Sahrens 25205367Sahrens /* 25215367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 25225367Sahrens * up to the prefixlen-long one. 25235367Sahrens */ 25245367Sahrens for (cp = target + prefixlen + 1; 25255367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 25265367Sahrens char *logstr; 25275367Sahrens 25285367Sahrens *cp = '\0'; 25295367Sahrens 25305367Sahrens h = make_dataset_handle(hdl, target); 25315367Sahrens if (h) { 25325367Sahrens /* it already exists, nothing to do here */ 25335367Sahrens zfs_close(h); 25345367Sahrens continue; 25355367Sahrens } 25365367Sahrens 25375367Sahrens logstr = hdl->libzfs_log_str; 25385367Sahrens hdl->libzfs_log_str = NULL; 25395367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 25405367Sahrens NULL) != 0) { 25415367Sahrens hdl->libzfs_log_str = logstr; 25425367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 25435367Sahrens goto ancestorerr; 25445367Sahrens } 25455367Sahrens 25465367Sahrens hdl->libzfs_log_str = logstr; 25475367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25485367Sahrens if (h == NULL) { 25495367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 25505367Sahrens goto ancestorerr; 25515367Sahrens } 25525367Sahrens 25535367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 25545367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 25555367Sahrens goto ancestorerr; 25565367Sahrens } 25575367Sahrens 25585367Sahrens if (zfs_share(h) != 0) { 25595367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 25605367Sahrens goto ancestorerr; 25615367Sahrens } 25625367Sahrens 25635367Sahrens zfs_close(h); 25645367Sahrens } 25655367Sahrens 25665367Sahrens return (0); 25675367Sahrens 25685367Sahrens ancestorerr: 25695367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25705367Sahrens "failed to %s ancestor '%s'"), opname, target); 25715367Sahrens return (-1); 25725367Sahrens } 25735367Sahrens 25745367Sahrens /* 25754490Svb160487 * Creates non-existing ancestors of the given path. 25764490Svb160487 */ 25774490Svb160487 int 25784490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 25794490Svb160487 { 25804490Svb160487 int prefix; 25814490Svb160487 uint64_t zoned; 25824490Svb160487 char *path_copy; 25834490Svb160487 int rc; 25844490Svb160487 25854490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 25864490Svb160487 return (-1); 25874490Svb160487 25884490Svb160487 if ((path_copy = strdup(path)) != NULL) { 25894490Svb160487 rc = create_parents(hdl, path_copy, prefix); 25904490Svb160487 free(path_copy); 25914490Svb160487 } 25924490Svb160487 if (path_copy == NULL || rc != 0) 25934490Svb160487 return (-1); 25944490Svb160487 2595789Sahrens return (0); 2596789Sahrens } 2597789Sahrens 2598789Sahrens /* 25992676Seschrock * Create a new filesystem or volume. 2600789Sahrens */ 2601789Sahrens int 26022082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 26032676Seschrock nvlist_t *props) 2604789Sahrens { 2605789Sahrens zfs_cmd_t zc = { 0 }; 2606789Sahrens int ret; 2607789Sahrens uint64_t size = 0; 2608789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 26092082Seschrock char errbuf[1024]; 26102676Seschrock uint64_t zoned; 26112082Seschrock 26122082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 26132082Seschrock "cannot create '%s'"), path); 2614789Sahrens 2615789Sahrens /* validate the path, taking care to note the extended error message */ 26165326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 26172082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2618789Sahrens 2619789Sahrens /* validate parents exist */ 26204490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2621789Sahrens return (-1); 2622789Sahrens 2623789Sahrens /* 2624789Sahrens * The failure modes when creating a dataset of a different type over 2625789Sahrens * one that already exists is a little strange. In particular, if you 2626789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2627789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2628789Sahrens * first try to see if the dataset exists. 2629789Sahrens */ 2630789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 26315094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 26322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26332082Seschrock "dataset already exists")); 26342082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2635789Sahrens } 2636789Sahrens 2637789Sahrens if (type == ZFS_TYPE_VOLUME) 2638789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2639789Sahrens else 2640789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2641789Sahrens 26427184Stimh if (props && (props = zfs_valid_proplist(hdl, type, props, 26433912Slling zoned, NULL, errbuf)) == 0) 26442676Seschrock return (-1); 26452676Seschrock 2646789Sahrens if (type == ZFS_TYPE_VOLUME) { 26471133Seschrock /* 26481133Seschrock * If we are creating a volume, the size and block size must 26491133Seschrock * satisfy a few restraints. First, the blocksize must be a 26501133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 26511133Seschrock * volsize must be a multiple of the block size, and cannot be 26521133Seschrock * zero. 26531133Seschrock */ 26542676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 26552676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 26562676Seschrock nvlist_free(props); 26572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26582676Seschrock "missing volume size")); 26592676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2660789Sahrens } 2661789Sahrens 26622676Seschrock if ((ret = nvlist_lookup_uint64(props, 26632676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 26642676Seschrock &blocksize)) != 0) { 26652676Seschrock if (ret == ENOENT) { 26662676Seschrock blocksize = zfs_prop_default_numeric( 26672676Seschrock ZFS_PROP_VOLBLOCKSIZE); 26682676Seschrock } else { 26692676Seschrock nvlist_free(props); 26702676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26712676Seschrock "missing volume block size")); 26722676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26732676Seschrock } 26742676Seschrock } 26752676Seschrock 26762676Seschrock if (size == 0) { 26772676Seschrock nvlist_free(props); 26782082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26792676Seschrock "volume size cannot be zero")); 26802676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26811133Seschrock } 26821133Seschrock 26831133Seschrock if (size % blocksize != 0) { 26842676Seschrock nvlist_free(props); 26852082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26862676Seschrock "volume size must be a multiple of volume block " 26872676Seschrock "size")); 26882676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26891133Seschrock } 2690789Sahrens } 2691789Sahrens 26925094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 26932676Seschrock return (-1); 26942676Seschrock nvlist_free(props); 26952676Seschrock 2696789Sahrens /* create the dataset */ 26974543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2698789Sahrens 26993912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 27002082Seschrock ret = zvol_create_link(hdl, path); 27013912Slling if (ret) { 27023912Slling (void) zfs_standard_error(hdl, errno, 27033912Slling dgettext(TEXT_DOMAIN, 27043912Slling "Volume successfully created, but device links " 27053912Slling "were not created")); 27063912Slling zcmd_free_nvlists(&zc); 27073912Slling return (-1); 27083912Slling } 27093912Slling } 2710789Sahrens 27112676Seschrock zcmd_free_nvlists(&zc); 27122676Seschrock 2713789Sahrens /* check for failure */ 2714789Sahrens if (ret != 0) { 2715789Sahrens char parent[ZFS_MAXNAMELEN]; 2716789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2717789Sahrens 2718789Sahrens switch (errno) { 2719789Sahrens case ENOENT: 27202082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27212082Seschrock "no such parent '%s'"), parent); 27222082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2723789Sahrens 2724789Sahrens case EINVAL: 27252082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27263413Smmusante "parent '%s' is not a filesystem"), parent); 27272082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2728789Sahrens 2729789Sahrens case EDOM: 27302082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27312676Seschrock "volume block size must be power of 2 from " 27322676Seschrock "%u to %uk"), 2733789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2734789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 27352082Seschrock 27362676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27372082Seschrock 27384603Sahrens case ENOTSUP: 27394603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27404603Sahrens "pool must be upgraded to set this " 27414603Sahrens "property or value")); 27424603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 2743789Sahrens #ifdef _ILP32 2744789Sahrens case EOVERFLOW: 2745789Sahrens /* 2746789Sahrens * This platform can't address a volume this big. 2747789Sahrens */ 27482082Seschrock if (type == ZFS_TYPE_VOLUME) 27492082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 27502082Seschrock errbuf)); 2751789Sahrens #endif 27522082Seschrock /* FALLTHROUGH */ 2753789Sahrens default: 27542082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2755789Sahrens } 2756789Sahrens } 2757789Sahrens 2758789Sahrens return (0); 2759789Sahrens } 2760789Sahrens 2761789Sahrens /* 2762789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2763789Sahrens * isn't mounted, and that there are no active dependents. 2764789Sahrens */ 2765789Sahrens int 2766789Sahrens zfs_destroy(zfs_handle_t *zhp) 2767789Sahrens { 2768789Sahrens zfs_cmd_t zc = { 0 }; 2769789Sahrens 2770789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2771789Sahrens 27722676Seschrock if (ZFS_IS_VOLUME(zhp)) { 27733126Sahl /* 27744543Smarks * If user doesn't have permissions to unshare volume, then 27754543Smarks * abort the request. This would only happen for a 27764543Smarks * non-privileged user. 27773126Sahl */ 27784543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 27794543Smarks return (-1); 27804543Smarks } 27813126Sahl 27822082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2783789Sahrens return (-1); 2784789Sahrens 2785789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2786789Sahrens } else { 2787789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2788789Sahrens } 2789789Sahrens 27904543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 27913237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 27922082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 27932082Seschrock zhp->zfs_name)); 27942199Sahrens } 2795789Sahrens 2796789Sahrens remove_mountpoint(zhp); 2797789Sahrens 2798789Sahrens return (0); 2799789Sahrens } 2800789Sahrens 28012199Sahrens struct destroydata { 28022199Sahrens char *snapname; 28032199Sahrens boolean_t gotone; 28043265Sahrens boolean_t closezhp; 28052199Sahrens }; 28062199Sahrens 28072199Sahrens static int 28082199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 28092199Sahrens { 28102199Sahrens struct destroydata *dd = arg; 28112199Sahrens zfs_handle_t *szhp; 28122199Sahrens char name[ZFS_MAXNAMELEN]; 28133265Sahrens boolean_t closezhp = dd->closezhp; 28143265Sahrens int rv; 28152199Sahrens 28162676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 28172676Seschrock (void) strlcat(name, "@", sizeof (name)); 28182676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 28192199Sahrens 28202199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 28212199Sahrens if (szhp) { 28222199Sahrens dd->gotone = B_TRUE; 28232199Sahrens zfs_close(szhp); 28242199Sahrens } 28252199Sahrens 28262199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 28272199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 28282199Sahrens /* 28292199Sahrens * NB: this is simply a best-effort. We don't want to 28302199Sahrens * return an error, because then we wouldn't visit all 28312199Sahrens * the volumes. 28322199Sahrens */ 28332199Sahrens } 28342199Sahrens 28353265Sahrens dd->closezhp = B_TRUE; 28363265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 28373265Sahrens if (closezhp) 28383265Sahrens zfs_close(zhp); 28393265Sahrens return (rv); 28402199Sahrens } 28412199Sahrens 28422199Sahrens /* 28432199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 28442199Sahrens */ 28452199Sahrens int 28462199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 28472199Sahrens { 28482199Sahrens zfs_cmd_t zc = { 0 }; 28492199Sahrens int ret; 28502199Sahrens struct destroydata dd = { 0 }; 28512199Sahrens 28522199Sahrens dd.snapname = snapname; 28532199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 28542199Sahrens 28552199Sahrens if (!dd.gotone) { 28563237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 28572199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 28582199Sahrens zhp->zfs_name, snapname)); 28592199Sahrens } 28602199Sahrens 28612199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 28622676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 28632199Sahrens 28644543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 28652199Sahrens if (ret != 0) { 28662199Sahrens char errbuf[1024]; 28672199Sahrens 28682199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28692199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 28702199Sahrens 28712199Sahrens switch (errno) { 28722199Sahrens case EEXIST: 28732199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28742199Sahrens "snapshot is cloned")); 28752199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 28762199Sahrens 28772199Sahrens default: 28782199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 28792199Sahrens errbuf)); 28802199Sahrens } 28812199Sahrens } 28822199Sahrens 28832199Sahrens return (0); 28842199Sahrens } 28852199Sahrens 2886789Sahrens /* 2887789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2888789Sahrens */ 2889789Sahrens int 28902676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2891789Sahrens { 2892789Sahrens zfs_cmd_t zc = { 0 }; 2893789Sahrens char parent[ZFS_MAXNAMELEN]; 2894789Sahrens int ret; 28952082Seschrock char errbuf[1024]; 28962082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 28972676Seschrock zfs_type_t type; 28982676Seschrock uint64_t zoned; 2899789Sahrens 2900789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2901789Sahrens 29022082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29032082Seschrock "cannot create '%s'"), target); 29042082Seschrock 2905789Sahrens /* validate the target name */ 29065326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 29072082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2908789Sahrens 2909789Sahrens /* validate parents exist */ 29104490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2911789Sahrens return (-1); 2912789Sahrens 2913789Sahrens (void) parent_name(target, parent, sizeof (parent)); 2914789Sahrens 2915789Sahrens /* do the clone */ 29162676Seschrock if (ZFS_IS_VOLUME(zhp)) { 2917789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 29182744Snn35248 type = ZFS_TYPE_VOLUME; 29192676Seschrock } else { 2920789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 29212744Snn35248 type = ZFS_TYPE_FILESYSTEM; 29222676Seschrock } 29232676Seschrock 29242676Seschrock if (props) { 29257184Stimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 29267184Stimh zhp, errbuf)) == NULL) 29272676Seschrock return (-1); 29282676Seschrock 29295094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 29302676Seschrock nvlist_free(props); 29312676Seschrock return (-1); 29322676Seschrock } 29332676Seschrock 29342676Seschrock nvlist_free(props); 29352676Seschrock } 2936789Sahrens 2937789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 29382676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 29394543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2940789Sahrens 29412676Seschrock zcmd_free_nvlists(&zc); 29422676Seschrock 2943789Sahrens if (ret != 0) { 2944789Sahrens switch (errno) { 2945789Sahrens 2946789Sahrens case ENOENT: 2947789Sahrens /* 2948789Sahrens * The parent doesn't exist. We should have caught this 2949789Sahrens * above, but there may a race condition that has since 2950789Sahrens * destroyed the parent. 2951789Sahrens * 2952789Sahrens * At this point, we don't know whether it's the source 2953789Sahrens * that doesn't exist anymore, or whether the target 2954789Sahrens * dataset doesn't exist. 2955789Sahrens */ 29562082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29572082Seschrock "no such parent '%s'"), parent); 29582082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 29592082Seschrock 29602082Seschrock case EXDEV: 29612082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29622082Seschrock "source and target pools differ")); 29632082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 29642082Seschrock errbuf)); 29652082Seschrock 29662082Seschrock default: 29672082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 29682082Seschrock errbuf)); 29692082Seschrock } 29702676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 29712082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 29722082Seschrock } 29732082Seschrock 29742082Seschrock return (ret); 29752082Seschrock } 29762082Seschrock 29772082Seschrock typedef struct promote_data { 29782082Seschrock char cb_mountpoint[MAXPATHLEN]; 29792082Seschrock const char *cb_target; 29802082Seschrock const char *cb_errbuf; 29812082Seschrock uint64_t cb_pivot_txg; 29822082Seschrock } promote_data_t; 29832082Seschrock 29842082Seschrock static int 29852082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 29862082Seschrock { 29872082Seschrock promote_data_t *pd = data; 29882082Seschrock zfs_handle_t *szhp; 29892082Seschrock char snapname[MAXPATHLEN]; 29903265Sahrens int rv = 0; 29912082Seschrock 29922082Seschrock /* We don't care about snapshots after the pivot point */ 29933265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 29943265Sahrens zfs_close(zhp); 29952082Seschrock return (0); 29963265Sahrens } 29972082Seschrock 29982417Sahrens /* Remove the device link if it's a zvol. */ 29992676Seschrock if (ZFS_IS_VOLUME(zhp)) 30002417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 30012082Seschrock 30022082Seschrock /* Check for conflicting names */ 30032676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 30042676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 30052082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 30062082Seschrock if (szhp != NULL) { 30072082Seschrock zfs_close(szhp); 30082082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30092082Seschrock "snapshot name '%s' from origin \n" 30102082Seschrock "conflicts with '%s' from target"), 30112082Seschrock zhp->zfs_name, snapname); 30123265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 30132082Seschrock } 30143265Sahrens zfs_close(zhp); 30153265Sahrens return (rv); 30162082Seschrock } 30172082Seschrock 30182417Sahrens static int 30192417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 30202417Sahrens { 30212417Sahrens promote_data_t *pd = data; 30222417Sahrens 30232417Sahrens /* We don't care about snapshots after the pivot point */ 30243265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 30253265Sahrens /* Create the device link if it's a zvol. */ 30263265Sahrens if (ZFS_IS_VOLUME(zhp)) 30273265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 30283265Sahrens } 30293265Sahrens 30303265Sahrens zfs_close(zhp); 30312417Sahrens return (0); 30322417Sahrens } 30332417Sahrens 30342082Seschrock /* 30352082Seschrock * Promotes the given clone fs to be the clone parent. 30362082Seschrock */ 30372082Seschrock int 30382082Seschrock zfs_promote(zfs_handle_t *zhp) 30392082Seschrock { 30402082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 30412082Seschrock zfs_cmd_t zc = { 0 }; 30422082Seschrock char parent[MAXPATHLEN]; 30432082Seschrock char *cp; 30442082Seschrock int ret; 30452082Seschrock zfs_handle_t *pzhp; 30462082Seschrock promote_data_t pd; 30472082Seschrock char errbuf[1024]; 30482082Seschrock 30492082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30502082Seschrock "cannot promote '%s'"), zhp->zfs_name); 30512082Seschrock 30522082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 30532082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30542082Seschrock "snapshots can not be promoted")); 30552082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 30562082Seschrock } 30572082Seschrock 30585367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 30592082Seschrock if (parent[0] == '\0') { 30602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30612082Seschrock "not a cloned filesystem")); 30622082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 30632082Seschrock } 30642082Seschrock cp = strchr(parent, '@'); 30652082Seschrock *cp = '\0'; 30662082Seschrock 30672082Seschrock /* Walk the snapshots we will be moving */ 30685367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 30692082Seschrock if (pzhp == NULL) 30702082Seschrock return (-1); 30712082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 30722082Seschrock zfs_close(pzhp); 30732082Seschrock pd.cb_target = zhp->zfs_name; 30742082Seschrock pd.cb_errbuf = errbuf; 30755094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 30762082Seschrock if (pzhp == NULL) 30772082Seschrock return (-1); 30782082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 30792082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 30802082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 30812417Sahrens if (ret != 0) { 30822417Sahrens zfs_close(pzhp); 30832082Seschrock return (-1); 30842417Sahrens } 30852082Seschrock 30862082Seschrock /* issue the ioctl */ 30875367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 30882676Seschrock sizeof (zc.zc_value)); 30892082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 30904543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 30912082Seschrock 30922082Seschrock if (ret != 0) { 30932417Sahrens int save_errno = errno; 30942417Sahrens 30952417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 30962417Sahrens zfs_close(pzhp); 30972417Sahrens 30982417Sahrens switch (save_errno) { 3099789Sahrens case EEXIST: 3100789Sahrens /* 31012082Seschrock * There is a conflicting snapshot name. We 31022082Seschrock * should have caught this above, but they could 31032082Seschrock * have renamed something in the mean time. 3104789Sahrens */ 31052082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31062082Seschrock "conflicting snapshot name from parent '%s'"), 31072082Seschrock parent); 31082082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3109789Sahrens 3110789Sahrens default: 31112417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3112789Sahrens } 31132417Sahrens } else { 31142417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3115789Sahrens } 3116789Sahrens 31172417Sahrens zfs_close(pzhp); 3118789Sahrens return (ret); 3119789Sahrens } 3120789Sahrens 31214007Smmusante struct createdata { 31224007Smmusante const char *cd_snapname; 31234007Smmusante int cd_ifexists; 31244007Smmusante }; 31254007Smmusante 31262199Sahrens static int 31272199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 31282199Sahrens { 31294007Smmusante struct createdata *cd = arg; 31302676Seschrock int ret; 31312199Sahrens 31322199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 31332199Sahrens char name[MAXPATHLEN]; 31342199Sahrens 31352676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 31362676Seschrock (void) strlcat(name, "@", sizeof (name)); 31374007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 31384007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 31394007Smmusante cd->cd_ifexists); 31402199Sahrens /* 31412199Sahrens * NB: this is simply a best-effort. We don't want to 31422199Sahrens * return an error, because then we wouldn't visit all 31432199Sahrens * the volumes. 31442199Sahrens */ 31452199Sahrens } 31462676Seschrock 31474007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 31482676Seschrock 31492676Seschrock zfs_close(zhp); 31502676Seschrock 31512676Seschrock return (ret); 31522199Sahrens } 31532199Sahrens 3154789Sahrens /* 31553504Sahl * Takes a snapshot of the given dataset. 3156789Sahrens */ 3157789Sahrens int 31587265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 31597265Sahrens nvlist_t *props) 3160789Sahrens { 3161789Sahrens const char *delim; 31627265Sahrens char parent[ZFS_MAXNAMELEN]; 3163789Sahrens zfs_handle_t *zhp; 3164789Sahrens zfs_cmd_t zc = { 0 }; 3165789Sahrens int ret; 31662082Seschrock char errbuf[1024]; 31672082Seschrock 31682082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31692082Seschrock "cannot snapshot '%s'"), path); 31702082Seschrock 31712082Seschrock /* validate the target name */ 31725326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 31732082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3174789Sahrens 31757265Sahrens if (props) { 31767265Sahrens if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 31777265Sahrens props, B_FALSE, NULL, errbuf)) == NULL) 31787265Sahrens return (-1); 31797265Sahrens 31807265Sahrens if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 31817265Sahrens nvlist_free(props); 31827265Sahrens return (-1); 31837265Sahrens } 31847265Sahrens 31857265Sahrens nvlist_free(props); 31867265Sahrens } 31877265Sahrens 3188789Sahrens /* make sure the parent exists and is of the appropriate type */ 31892199Sahrens delim = strchr(path, '@'); 3190789Sahrens (void) strncpy(parent, path, delim - path); 3191789Sahrens parent[delim - path] = '\0'; 3192789Sahrens 31932082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3194789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 31957265Sahrens zcmd_free_nvlists(&zc); 3196789Sahrens return (-1); 3197789Sahrens } 3198789Sahrens 31992199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 32002676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 32014543Smarks if (ZFS_IS_VOLUME(zhp)) 32024543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 32034543Smarks else 32044543Smarks zc.zc_objset_type = DMU_OST_ZFS; 32052199Sahrens zc.zc_cookie = recursive; 32064543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 32072199Sahrens 32087265Sahrens zcmd_free_nvlists(&zc); 32097265Sahrens 32102199Sahrens /* 32112199Sahrens * if it was recursive, the one that actually failed will be in 32122199Sahrens * zc.zc_name. 32132199Sahrens */ 32144543Smarks if (ret != 0) 32154543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32164543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 32174543Smarks 32182199Sahrens if (ret == 0 && recursive) { 32194007Smmusante struct createdata cd; 32204007Smmusante 32214007Smmusante cd.cd_snapname = delim + 1; 32224007Smmusante cd.cd_ifexists = B_FALSE; 32234007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 32242199Sahrens } 3225789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 32262082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 32272199Sahrens if (ret != 0) { 32284543Smarks (void) zfs_standard_error(hdl, errno, 32294543Smarks dgettext(TEXT_DOMAIN, 32304543Smarks "Volume successfully snapshotted, but device links " 32314543Smarks "were not created")); 32324543Smarks zfs_close(zhp); 32334543Smarks return (-1); 32342199Sahrens } 3235789Sahrens } 3236789Sahrens 32372082Seschrock if (ret != 0) 32382082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3239789Sahrens 3240789Sahrens zfs_close(zhp); 3241789Sahrens 3242789Sahrens return (ret); 3243789Sahrens } 3244789Sahrens 3245789Sahrens /* 32461294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 32471294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 32481294Slling * is a dependent and we should just destroy it without checking the transaction 32491294Slling * group. 3250789Sahrens */ 32511294Slling typedef struct rollback_data { 32521294Slling const char *cb_target; /* the snapshot */ 32531294Slling uint64_t cb_create; /* creation time reference */ 32545749Sahrens boolean_t cb_error; 32552082Seschrock boolean_t cb_dependent; 32565749Sahrens boolean_t cb_force; 32571294Slling } rollback_data_t; 32581294Slling 32591294Slling static int 32601294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 32611294Slling { 32621294Slling rollback_data_t *cbp = data; 32631294Slling 32641294Slling if (!cbp->cb_dependent) { 32651294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 32661294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 32671294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 32681294Slling cbp->cb_create) { 32694543Smarks char *logstr; 32701294Slling 32712082Seschrock cbp->cb_dependent = B_TRUE; 32725446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 32735446Sahrens rollback_destroy, cbp); 32742082Seschrock cbp->cb_dependent = B_FALSE; 32751294Slling 32764543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 32774543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 32785446Sahrens cbp->cb_error |= zfs_destroy(zhp); 32794543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 32801294Slling } 32811294Slling } else { 32825749Sahrens /* We must destroy this clone; first unmount it */ 32835749Sahrens prop_changelist_t *clp; 32845749Sahrens 32857366STim.Haley@Sun.COM clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 32865749Sahrens cbp->cb_force ? MS_FORCE: 0); 32875749Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 32885749Sahrens cbp->cb_error = B_TRUE; 32895749Sahrens zfs_close(zhp); 32905749Sahrens return (0); 32915749Sahrens } 32925749Sahrens if (zfs_destroy(zhp) != 0) 32935749Sahrens cbp->cb_error = B_TRUE; 32945749Sahrens else 32955749Sahrens changelist_remove(clp, zhp->zfs_name); 32965751Sahrens (void) changelist_postfix(clp); 32975749Sahrens changelist_free(clp); 32981294Slling } 32991294Slling 33001294Slling zfs_close(zhp); 33011294Slling return (0); 33021294Slling } 33031294Slling 33041294Slling /* 33055446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 33065446Sahrens * data changes since then and making it the active dataset. 33075446Sahrens * 33085446Sahrens * Any snapshots more recent than the target are destroyed, along with 33095446Sahrens * their dependents. 33101294Slling */ 33115446Sahrens int 33125749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3313789Sahrens { 33145446Sahrens rollback_data_t cb = { 0 }; 33155446Sahrens int err; 3316789Sahrens zfs_cmd_t zc = { 0 }; 33175713Srm160521 boolean_t restore_resv = 0; 33185713Srm160521 uint64_t old_volsize, new_volsize; 33195713Srm160521 zfs_prop_t resv_prop; 3320789Sahrens 3321789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3322789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3323789Sahrens 33245446Sahrens /* 33255446Sahrens * Destroy all recent snapshots and its dependends. 33265446Sahrens */ 33275749Sahrens cb.cb_force = force; 33285446Sahrens cb.cb_target = snap->zfs_name; 33295446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 33305446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 33315446Sahrens 33325749Sahrens if (cb.cb_error) 33335749Sahrens return (-1); 33345446Sahrens 33355446Sahrens /* 33365446Sahrens * Now that we have verified that the snapshot is the latest, 33375446Sahrens * rollback to the given snapshot. 33385446Sahrens */ 33395446Sahrens 33405713Srm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33415713Srm160521 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 33425713Srm160521 return (-1); 33435713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 33445713Srm160521 return (-1); 33455713Srm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 33465713Srm160521 restore_resv = 33475713Srm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 33485713Srm160521 } 3349789Sahrens 3350789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3351789Sahrens 33522676Seschrock if (ZFS_IS_VOLUME(zhp)) 3353789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3354789Sahrens else 3355789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3356789Sahrens 3357789Sahrens /* 33585446Sahrens * We rely on zfs_iter_children() to verify that there are no 33595446Sahrens * newer snapshots for the given dataset. Therefore, we can 33605446Sahrens * simply pass the name on to the ioctl() call. There is still 33615446Sahrens * an unlikely race condition where the user has taken a 33625446Sahrens * snapshot since we verified that this was the most recent. 33635713Srm160521 * 3364789Sahrens */ 33655446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 33663237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 33672082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 33682082Seschrock zhp->zfs_name); 33695717Srm160521 return (err); 33705717Srm160521 } 33715713Srm160521 33725713Srm160521 /* 33735713Srm160521 * For volumes, if the pre-rollback volsize matched the pre- 33745713Srm160521 * rollback reservation and the volsize has changed then set 33755713Srm160521 * the reservation property to the post-rollback volsize. 33765713Srm160521 * Make a new handle since the rollback closed the dataset. 33775713Srm160521 */ 33785717Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 33795717Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 33805717Srm160521 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) { 33815717Srm160521 zfs_close(zhp); 33825713Srm160521 return (err); 33835717Srm160521 } 33845713Srm160521 if (restore_resv) { 33855713Srm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 33865713Srm160521 if (old_volsize != new_volsize) 33875717Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 33885717Srm160521 new_volsize); 33895713Srm160521 } 33905713Srm160521 zfs_close(zhp); 3391789Sahrens } 33925446Sahrens return (err); 33931294Slling } 33941294Slling 33951294Slling /* 3396789Sahrens * Iterate over all dependents for a given dataset. This includes both 3397789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3398789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3399789Sahrens * libzfs_graph.c. 3400789Sahrens */ 3401789Sahrens int 34022474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 34032474Seschrock zfs_iter_f func, void *data) 3404789Sahrens { 3405789Sahrens char **dependents; 3406789Sahrens size_t count; 3407789Sahrens int i; 3408789Sahrens zfs_handle_t *child; 3409789Sahrens int ret = 0; 3410789Sahrens 34112474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 34122474Seschrock &dependents, &count) != 0) 34132474Seschrock return (-1); 34142474Seschrock 3415789Sahrens for (i = 0; i < count; i++) { 34162082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 34172082Seschrock dependents[i])) == NULL) 3418789Sahrens continue; 3419789Sahrens 3420789Sahrens if ((ret = func(child, data)) != 0) 3421789Sahrens break; 3422789Sahrens } 3423789Sahrens 3424789Sahrens for (i = 0; i < count; i++) 3425789Sahrens free(dependents[i]); 3426789Sahrens free(dependents); 3427789Sahrens 3428789Sahrens return (ret); 3429789Sahrens } 3430789Sahrens 3431789Sahrens /* 3432789Sahrens * Renames the given dataset. 3433789Sahrens */ 3434789Sahrens int 34354490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3436789Sahrens { 3437789Sahrens int ret; 3438789Sahrens zfs_cmd_t zc = { 0 }; 3439789Sahrens char *delim; 34404007Smmusante prop_changelist_t *cl = NULL; 34414007Smmusante zfs_handle_t *zhrp = NULL; 34424007Smmusante char *parentname = NULL; 3443789Sahrens char parent[ZFS_MAXNAMELEN]; 34442082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 34452082Seschrock char errbuf[1024]; 3446789Sahrens 3447789Sahrens /* if we have the same exact name, just return success */ 3448789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3449789Sahrens return (0); 3450789Sahrens 34512082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34522082Seschrock "cannot rename to '%s'"), target); 34532082Seschrock 3454789Sahrens /* 3455789Sahrens * Make sure the target name is valid 3456789Sahrens */ 3457789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 34582665Snd150628 if ((strchr(target, '@') == NULL) || 34592665Snd150628 *target == '@') { 34602665Snd150628 /* 34612665Snd150628 * Snapshot target name is abbreviated, 34622665Snd150628 * reconstruct full dataset name 34632665Snd150628 */ 34642665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 34652665Snd150628 sizeof (parent)); 34662665Snd150628 delim = strchr(parent, '@'); 34672665Snd150628 if (strchr(target, '@') == NULL) 34682665Snd150628 *(++delim) = '\0'; 34692665Snd150628 else 34702665Snd150628 *delim = '\0'; 34712665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 34722665Snd150628 target = parent; 34732665Snd150628 } else { 34742665Snd150628 /* 34752665Snd150628 * Make sure we're renaming within the same dataset. 34762665Snd150628 */ 34772665Snd150628 delim = strchr(target, '@'); 34782665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 34792665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 34802665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34812665Snd150628 "snapshots must be part of same " 34822665Snd150628 "dataset")); 34832665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 34843912Slling errbuf)); 34852665Snd150628 } 3486789Sahrens } 34875326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 34882665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3489789Sahrens } else { 34904007Smmusante if (recursive) { 34914007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34924007Smmusante "recursive rename must be a snapshot")); 34934007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 34944007Smmusante } 34954007Smmusante 34965326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 34972665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 34982676Seschrock uint64_t unused; 34992676Seschrock 3500789Sahrens /* validate parents */ 35014490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3502789Sahrens return (-1); 3503789Sahrens 3504789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3505789Sahrens 3506789Sahrens /* make sure we're in the same pool */ 3507789Sahrens verify((delim = strchr(target, '/')) != NULL); 3508789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3509789Sahrens zhp->zfs_name[delim - target] != '/') { 35102082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35112082Seschrock "datasets must be within same pool")); 35122082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3513789Sahrens } 35142440Snd150628 35152440Snd150628 /* new name cannot be a child of the current dataset name */ 35162440Snd150628 if (strncmp(parent, zhp->zfs_name, 35173912Slling strlen(zhp->zfs_name)) == 0) { 35182440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35192440Snd150628 "New dataset name cannot be a descendent of " 35202440Snd150628 "current dataset name")); 35212440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 35222440Snd150628 } 3523789Sahrens } 3524789Sahrens 35252082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 35262082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 35272082Seschrock 3528789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3529789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 35302082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35312082Seschrock "dataset is used in a non-global zone")); 35322082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3533789Sahrens } 3534789Sahrens 35354007Smmusante if (recursive) { 35364007Smmusante struct destroydata dd; 35374007Smmusante 35384183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 35394183Smmusante if (parentname == NULL) { 35404183Smmusante ret = -1; 35414183Smmusante goto error; 35424183Smmusante } 35434007Smmusante delim = strchr(parentname, '@'); 35444007Smmusante *delim = '\0'; 35455094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 35464007Smmusante if (zhrp == NULL) { 35474183Smmusante ret = -1; 35484183Smmusante goto error; 35494007Smmusante } 35504007Smmusante 35514007Smmusante dd.snapname = delim + 1; 35524007Smmusante dd.gotone = B_FALSE; 35534183Smmusante dd.closezhp = B_TRUE; 35544007Smmusante 35554007Smmusante /* We remove any zvol links prior to renaming them */ 35564007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 35574007Smmusante if (ret) { 35584007Smmusante goto error; 35594007Smmusante } 35604007Smmusante } else { 35617366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL) 35624007Smmusante return (-1); 35634007Smmusante 35644007Smmusante if (changelist_haszonedchild(cl)) { 35654007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35664007Smmusante "child dataset with inherited mountpoint is used " 35674007Smmusante "in a non-global zone")); 35684007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 35694007Smmusante goto error; 35704007Smmusante } 35714007Smmusante 35724007Smmusante if ((ret = changelist_prefix(cl)) != 0) 35734007Smmusante goto error; 3574789Sahrens } 3575789Sahrens 35762676Seschrock if (ZFS_IS_VOLUME(zhp)) 3577789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3578789Sahrens else 3579789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3580789Sahrens 35812665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35822676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 35832665Snd150628 35844007Smmusante zc.zc_cookie = recursive; 35854007Smmusante 35864543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 35874007Smmusante /* 35884007Smmusante * if it was recursive, the one that actually failed will 35894007Smmusante * be in zc.zc_name 35904007Smmusante */ 35914007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35925367Sahrens "cannot rename '%s'"), zc.zc_name); 35934007Smmusante 35944007Smmusante if (recursive && errno == EEXIST) { 35954007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35964007Smmusante "a child dataset already has a snapshot " 35974007Smmusante "with the new name")); 35984801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 35994007Smmusante } else { 36004007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 36014007Smmusante } 3602789Sahrens 3603789Sahrens /* 3604789Sahrens * On failure, we still want to remount any filesystems that 3605789Sahrens * were previously mounted, so we don't alter the system state. 3606789Sahrens */ 36074007Smmusante if (recursive) { 36084007Smmusante struct createdata cd; 36094007Smmusante 36104007Smmusante /* only create links for datasets that had existed */ 36114007Smmusante cd.cd_snapname = delim + 1; 36124007Smmusante cd.cd_ifexists = B_TRUE; 36134007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36144007Smmusante &cd); 36154007Smmusante } else { 36164007Smmusante (void) changelist_postfix(cl); 36174007Smmusante } 3618789Sahrens } else { 36194007Smmusante if (recursive) { 36204007Smmusante struct createdata cd; 36214007Smmusante 36224007Smmusante /* only create links for datasets that had existed */ 36234007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 36244007Smmusante cd.cd_ifexists = B_TRUE; 36254007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36264007Smmusante &cd); 36274007Smmusante } else { 36284007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 36294007Smmusante ret = changelist_postfix(cl); 36304007Smmusante } 3631789Sahrens } 3632789Sahrens 3633789Sahrens error: 36344007Smmusante if (parentname) { 36354007Smmusante free(parentname); 36364007Smmusante } 36374007Smmusante if (zhrp) { 36384007Smmusante zfs_close(zhrp); 36394007Smmusante } 36404007Smmusante if (cl) { 36414007Smmusante changelist_free(cl); 36424007Smmusante } 3643789Sahrens return (ret); 3644789Sahrens } 3645789Sahrens 3646789Sahrens /* 3647789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3648789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3649789Sahrens */ 3650789Sahrens int 36512082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3652789Sahrens { 36534007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 36544007Smmusante } 36554007Smmusante 36564007Smmusante static int 36574007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 36584007Smmusante { 3659789Sahrens zfs_cmd_t zc = { 0 }; 36602082Seschrock di_devlink_handle_t dhdl; 36614543Smarks priv_set_t *priv_effective; 36624543Smarks int privileged; 3663789Sahrens 3664789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3665789Sahrens 3666789Sahrens /* 3667789Sahrens * Issue the appropriate ioctl. 3668789Sahrens */ 36692082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3670789Sahrens switch (errno) { 3671789Sahrens case EEXIST: 3672789Sahrens /* 3673789Sahrens * Silently ignore the case where the link already 3674789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3675789Sahrens * times without errors. 3676789Sahrens */ 3677789Sahrens return (0); 3678789Sahrens 36794007Smmusante case ENOENT: 36804007Smmusante /* 36814007Smmusante * Dataset does not exist in the kernel. If we 36824007Smmusante * don't care (see zfs_rename), then ignore the 36834007Smmusante * error quietly. 36844007Smmusante */ 36854007Smmusante if (ifexists) { 36864007Smmusante return (0); 36874007Smmusante } 36884007Smmusante 36894007Smmusante /* FALLTHROUGH */ 36904007Smmusante 3691789Sahrens default: 36923237Slling return (zfs_standard_error_fmt(hdl, errno, 36932082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 36942082Seschrock "for '%s'"), dataset)); 3695789Sahrens } 3696789Sahrens } 3697789Sahrens 3698789Sahrens /* 36994543Smarks * If privileged call devfsadm and wait for the links to 37004543Smarks * magically appear. 37014543Smarks * Otherwise, print out an informational message. 3702789Sahrens */ 37034543Smarks 37044543Smarks priv_effective = priv_allocset(); 37054543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 37064543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 37074543Smarks priv_freeset(priv_effective); 37084543Smarks 37094543Smarks if (privileged) { 37104543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 37114543Smarks DI_MAKE_LINK)) == NULL) { 37124543Smarks zfs_error_aux(hdl, strerror(errno)); 37137301SEric.Taylor@Sun.COM (void) zfs_error_fmt(hdl, errno, 37144543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 37154543Smarks "for '%s'"), dataset); 37164543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 37174543Smarks return (-1); 37184543Smarks } else { 37194543Smarks (void) di_devlink_fini(&dhdl); 37204543Smarks } 3721789Sahrens } else { 37224543Smarks char pathname[MAXPATHLEN]; 37234543Smarks struct stat64 statbuf; 37244543Smarks int i; 37254543Smarks 37264543Smarks #define MAX_WAIT 10 37274543Smarks 37284543Smarks /* 37294543Smarks * This is the poor mans way of waiting for the link 37304543Smarks * to show up. If after 10 seconds we still don't 37314543Smarks * have it, then print out a message. 37324543Smarks */ 37334543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 37344543Smarks dataset); 37354543Smarks 37364543Smarks for (i = 0; i != MAX_WAIT; i++) { 37374543Smarks if (stat64(pathname, &statbuf) == 0) 37384543Smarks break; 37394543Smarks (void) sleep(1); 37404543Smarks } 37414543Smarks if (i == MAX_WAIT) 37424543Smarks (void) printf(gettext("%s may not be immediately " 37434543Smarks "available\n"), pathname); 3744789Sahrens } 3745789Sahrens 3746789Sahrens return (0); 3747789Sahrens } 3748789Sahrens 3749789Sahrens /* 3750789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 3751789Sahrens */ 3752789Sahrens int 37532082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3754789Sahrens { 3755789Sahrens zfs_cmd_t zc = { 0 }; 3756789Sahrens 3757789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3758789Sahrens 37592082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3760789Sahrens switch (errno) { 3761789Sahrens case ENXIO: 3762789Sahrens /* 3763789Sahrens * Silently ignore the case where the link no longer 3764789Sahrens * exists, so that 'zfs volfini' can be run multiple 3765789Sahrens * times without errors. 3766789Sahrens */ 3767789Sahrens return (0); 3768789Sahrens 3769789Sahrens default: 37703237Slling return (zfs_standard_error_fmt(hdl, errno, 37712082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 37722082Seschrock "links for '%s'"), dataset)); 3773789Sahrens } 3774789Sahrens } 3775789Sahrens 3776789Sahrens return (0); 3777789Sahrens } 37782676Seschrock 37792676Seschrock nvlist_t * 37802676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 37812676Seschrock { 37822676Seschrock return (zhp->zfs_user_props); 37832676Seschrock } 37842676Seschrock 37852676Seschrock /* 37863912Slling * This function is used by 'zfs list' to determine the exact set of columns to 37873912Slling * display, and their maximum widths. This does two main things: 37883912Slling * 37893912Slling * - If this is a list of all properties, then expand the list to include 37903912Slling * all native properties, and set a flag so that for each dataset we look 37913912Slling * for new unique user properties and add them to the list. 37923912Slling * 37933912Slling * - For non fixed-width properties, keep track of the maximum width seen 37943912Slling * so that we can size the column appropriately. 37953912Slling */ 37963912Slling int 37975094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 37983912Slling { 37993912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 38005094Slling zprop_list_t *entry; 38015094Slling zprop_list_t **last, **start; 38023912Slling nvlist_t *userprops, *propval; 38033912Slling nvpair_t *elem; 38043912Slling char *strval; 38053912Slling char buf[ZFS_MAXPROPLEN]; 38063912Slling 38075094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 38083912Slling return (-1); 38092676Seschrock 38102676Seschrock userprops = zfs_get_user_props(zhp); 38112676Seschrock 38122676Seschrock entry = *plp; 38132676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 38142676Seschrock /* 38152676Seschrock * Go through and add any user properties as necessary. We 38162676Seschrock * start by incrementing our list pointer to the first 38172676Seschrock * non-native property. 38182676Seschrock */ 38192676Seschrock start = plp; 38202676Seschrock while (*start != NULL) { 38215094Slling if ((*start)->pl_prop == ZPROP_INVAL) 38222676Seschrock break; 38232676Seschrock start = &(*start)->pl_next; 38242676Seschrock } 38252676Seschrock 38262676Seschrock elem = NULL; 38272676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 38282676Seschrock /* 38292676Seschrock * See if we've already found this property in our list. 38302676Seschrock */ 38312676Seschrock for (last = start; *last != NULL; 38322676Seschrock last = &(*last)->pl_next) { 38332676Seschrock if (strcmp((*last)->pl_user_prop, 38342676Seschrock nvpair_name(elem)) == 0) 38352676Seschrock break; 38362676Seschrock } 38372676Seschrock 38382676Seschrock if (*last == NULL) { 38392676Seschrock if ((entry = zfs_alloc(hdl, 38405094Slling sizeof (zprop_list_t))) == NULL || 38412676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 38422676Seschrock nvpair_name(elem)))) == NULL) { 38432676Seschrock free(entry); 38442676Seschrock return (-1); 38452676Seschrock } 38462676Seschrock 38475094Slling entry->pl_prop = ZPROP_INVAL; 38482676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 38492676Seschrock entry->pl_all = B_TRUE; 38502676Seschrock *last = entry; 38512676Seschrock } 38522676Seschrock } 38532676Seschrock } 38542676Seschrock 38552676Seschrock /* 38562676Seschrock * Now go through and check the width of any non-fixed columns 38572676Seschrock */ 38582676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 38592676Seschrock if (entry->pl_fixed) 38602676Seschrock continue; 38612676Seschrock 38625094Slling if (entry->pl_prop != ZPROP_INVAL) { 38632676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 38642676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 38652676Seschrock if (strlen(buf) > entry->pl_width) 38662676Seschrock entry->pl_width = strlen(buf); 38672676Seschrock } 38682676Seschrock } else if (nvlist_lookup_nvlist(userprops, 38692676Seschrock entry->pl_user_prop, &propval) == 0) { 38702676Seschrock verify(nvlist_lookup_string(propval, 38715094Slling ZPROP_VALUE, &strval) == 0); 38722676Seschrock if (strlen(strval) > entry->pl_width) 38732676Seschrock entry->pl_width = strlen(strval); 38742676Seschrock } 38752676Seschrock } 38762676Seschrock 38772676Seschrock return (0); 38782676Seschrock } 38794543Smarks 38804543Smarks int 38814543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 38824543Smarks { 38834543Smarks zfs_cmd_t zc = { 0 }; 38844543Smarks nvlist_t *nvp; 38854543Smarks gid_t gid; 38864543Smarks uid_t uid; 38874543Smarks const gid_t *groups; 38884543Smarks int group_cnt; 38894543Smarks int error; 38904543Smarks 38914543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 38924543Smarks return (no_memory(hdl)); 38934543Smarks 38944543Smarks uid = ucred_geteuid(cred); 38954543Smarks gid = ucred_getegid(cred); 38964543Smarks group_cnt = ucred_getgroups(cred, &groups); 38974543Smarks 38984543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 38994543Smarks return (1); 39004543Smarks 39014543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 39024543Smarks nvlist_free(nvp); 39034543Smarks return (1); 39044543Smarks } 39054543Smarks 39064543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 39074543Smarks nvlist_free(nvp); 39084543Smarks return (1); 39094543Smarks } 39104543Smarks 39114543Smarks if (nvlist_add_uint32_array(nvp, 39124543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 39134543Smarks nvlist_free(nvp); 39144543Smarks return (1); 39154543Smarks } 39164543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39174543Smarks 39185094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 39194543Smarks return (-1); 39204543Smarks 39214543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 39224543Smarks nvlist_free(nvp); 39234543Smarks return (error); 39244543Smarks } 39254543Smarks 39264543Smarks int 39274543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 39288845Samw@Sun.COM char *resource, void *export, void *sharetab, 39298845Samw@Sun.COM int sharemax, zfs_share_op_t operation) 39304543Smarks { 39314543Smarks zfs_cmd_t zc = { 0 }; 39324543Smarks int error; 39334543Smarks 39344543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39354543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 39368845Samw@Sun.COM if (resource) 39378845Samw@Sun.COM (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 39384543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 39394543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 39405331Samw zc.zc_share.z_sharetype = operation; 39414543Smarks zc.zc_share.z_sharemax = sharemax; 39424543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 39434543Smarks return (error); 39444543Smarks } 39458802SSanjeev.Bagewadi@Sun.COM 39468802SSanjeev.Bagewadi@Sun.COM void 39478802SSanjeev.Bagewadi@Sun.COM zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 39488802SSanjeev.Bagewadi@Sun.COM { 39498802SSanjeev.Bagewadi@Sun.COM nvpair_t *curr; 39508802SSanjeev.Bagewadi@Sun.COM 39518802SSanjeev.Bagewadi@Sun.COM /* 39528802SSanjeev.Bagewadi@Sun.COM * Keep a reference to the props-table against which we prune the 39538802SSanjeev.Bagewadi@Sun.COM * properties. 39548802SSanjeev.Bagewadi@Sun.COM */ 39558802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table = props; 39568802SSanjeev.Bagewadi@Sun.COM 39578802SSanjeev.Bagewadi@Sun.COM curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 39588802SSanjeev.Bagewadi@Sun.COM 39598802SSanjeev.Bagewadi@Sun.COM while (curr) { 39608802SSanjeev.Bagewadi@Sun.COM zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 39618802SSanjeev.Bagewadi@Sun.COM nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 39628802SSanjeev.Bagewadi@Sun.COM 39639396SMatthew.Ahrens@Sun.COM /* 39649396SMatthew.Ahrens@Sun.COM * We leave user:props in the nvlist, so there will be 39659396SMatthew.Ahrens@Sun.COM * some ZPROP_INVAL. To be extra safe, don't prune 39669396SMatthew.Ahrens@Sun.COM * those. 39679396SMatthew.Ahrens@Sun.COM */ 39689396SMatthew.Ahrens@Sun.COM if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 39698802SSanjeev.Bagewadi@Sun.COM (void) nvlist_remove(zhp->zfs_props, 39708802SSanjeev.Bagewadi@Sun.COM nvpair_name(curr), nvpair_type(curr)); 39718802SSanjeev.Bagewadi@Sun.COM curr = next; 39728802SSanjeev.Bagewadi@Sun.COM } 39738802SSanjeev.Bagewadi@Sun.COM } 39748845Samw@Sun.COM 39758845Samw@Sun.COM static int 39768845Samw@Sun.COM zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 39778845Samw@Sun.COM zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 39788845Samw@Sun.COM { 39798845Samw@Sun.COM zfs_cmd_t zc = { 0 }; 39808845Samw@Sun.COM nvlist_t *nvlist = NULL; 39818845Samw@Sun.COM int error; 39828845Samw@Sun.COM 39838845Samw@Sun.COM (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39848845Samw@Sun.COM (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 39858845Samw@Sun.COM zc.zc_cookie = (uint64_t)cmd; 39868845Samw@Sun.COM 39878845Samw@Sun.COM if (cmd == ZFS_SMB_ACL_RENAME) { 39888845Samw@Sun.COM if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 39898845Samw@Sun.COM (void) no_memory(hdl); 39908845Samw@Sun.COM return (NULL); 39918845Samw@Sun.COM } 39928845Samw@Sun.COM } 39938845Samw@Sun.COM 39948845Samw@Sun.COM switch (cmd) { 39958845Samw@Sun.COM case ZFS_SMB_ACL_ADD: 39968845Samw@Sun.COM case ZFS_SMB_ACL_REMOVE: 39978845Samw@Sun.COM (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 39988845Samw@Sun.COM break; 39998845Samw@Sun.COM case ZFS_SMB_ACL_RENAME: 40008845Samw@Sun.COM if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 40018845Samw@Sun.COM resource1) != 0) { 40028845Samw@Sun.COM (void) no_memory(hdl); 40038845Samw@Sun.COM return (-1); 40048845Samw@Sun.COM } 40058845Samw@Sun.COM if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 40068845Samw@Sun.COM resource2) != 0) { 40078845Samw@Sun.COM (void) no_memory(hdl); 40088845Samw@Sun.COM return (-1); 40098845Samw@Sun.COM } 40108845Samw@Sun.COM if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 40118845Samw@Sun.COM nvlist_free(nvlist); 40128845Samw@Sun.COM return (-1); 40138845Samw@Sun.COM } 40148845Samw@Sun.COM break; 40158845Samw@Sun.COM case ZFS_SMB_ACL_PURGE: 40168845Samw@Sun.COM break; 40178845Samw@Sun.COM default: 40188845Samw@Sun.COM return (-1); 40198845Samw@Sun.COM } 40208845Samw@Sun.COM error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 40218845Samw@Sun.COM if (nvlist) 40228845Samw@Sun.COM nvlist_free(nvlist); 40238845Samw@Sun.COM return (error); 40248845Samw@Sun.COM } 40258845Samw@Sun.COM 40268845Samw@Sun.COM int 40278845Samw@Sun.COM zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 40288845Samw@Sun.COM char *path, char *resource) 40298845Samw@Sun.COM { 40308845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 40318845Samw@Sun.COM resource, NULL)); 40328845Samw@Sun.COM } 40338845Samw@Sun.COM 40348845Samw@Sun.COM int 40358845Samw@Sun.COM zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 40368845Samw@Sun.COM char *path, char *resource) 40378845Samw@Sun.COM { 40388845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 40398845Samw@Sun.COM resource, NULL)); 40408845Samw@Sun.COM } 40418845Samw@Sun.COM 40428845Samw@Sun.COM int 40438845Samw@Sun.COM zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 40448845Samw@Sun.COM { 40458845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 40468845Samw@Sun.COM NULL, NULL)); 40478845Samw@Sun.COM } 40488845Samw@Sun.COM 40498845Samw@Sun.COM int 40508845Samw@Sun.COM zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 40518845Samw@Sun.COM char *oldname, char *newname) 40528845Samw@Sun.COM { 40538845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 40548845Samw@Sun.COM oldname, newname)); 40558845Samw@Sun.COM } 40569396SMatthew.Ahrens@Sun.COM 40579396SMatthew.Ahrens@Sun.COM int 40589396SMatthew.Ahrens@Sun.COM zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 40599396SMatthew.Ahrens@Sun.COM zfs_userspace_cb_t func, void *arg) 40609396SMatthew.Ahrens@Sun.COM { 40619396SMatthew.Ahrens@Sun.COM zfs_cmd_t zc = { 0 }; 40629396SMatthew.Ahrens@Sun.COM int error; 40639396SMatthew.Ahrens@Sun.COM zfs_useracct_t buf[100]; 40649396SMatthew.Ahrens@Sun.COM 40659396SMatthew.Ahrens@Sun.COM (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 40669396SMatthew.Ahrens@Sun.COM 40679396SMatthew.Ahrens@Sun.COM zc.zc_objset_type = type; 40689396SMatthew.Ahrens@Sun.COM zc.zc_nvlist_dst = (uintptr_t)buf; 40699396SMatthew.Ahrens@Sun.COM 40709396SMatthew.Ahrens@Sun.COM /* CONSTCOND */ 40719396SMatthew.Ahrens@Sun.COM while (1) { 40729396SMatthew.Ahrens@Sun.COM zfs_useracct_t *zua = buf; 40739396SMatthew.Ahrens@Sun.COM 40749396SMatthew.Ahrens@Sun.COM zc.zc_nvlist_dst_size = sizeof (buf); 40759396SMatthew.Ahrens@Sun.COM error = ioctl(zhp->zfs_hdl->libzfs_fd, 40769396SMatthew.Ahrens@Sun.COM ZFS_IOC_USERSPACE_MANY, &zc); 40779396SMatthew.Ahrens@Sun.COM if (error || zc.zc_nvlist_dst_size == 0) 40789396SMatthew.Ahrens@Sun.COM break; 40799396SMatthew.Ahrens@Sun.COM 40809396SMatthew.Ahrens@Sun.COM while (zc.zc_nvlist_dst_size > 0) { 4081*9554SMatthew.Ahrens@Sun.COM error = func(arg, zua->zu_domain, zua->zu_rid, 4082*9554SMatthew.Ahrens@Sun.COM zua->zu_space); 4083*9554SMatthew.Ahrens@Sun.COM if (error != 0) 4084*9554SMatthew.Ahrens@Sun.COM return (error); 40859396SMatthew.Ahrens@Sun.COM zua++; 40869396SMatthew.Ahrens@Sun.COM zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 40879396SMatthew.Ahrens@Sun.COM } 40889396SMatthew.Ahrens@Sun.COM } 40899396SMatthew.Ahrens@Sun.COM 40909396SMatthew.Ahrens@Sun.COM return (error); 40919396SMatthew.Ahrens@Sun.COM } 4092