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 <ctype.h> 28789Sahrens #include <errno.h> 29789Sahrens #include <libintl.h> 30789Sahrens #include <math.h> 31789Sahrens #include <stdio.h> 32789Sahrens #include <stdlib.h> 33789Sahrens #include <strings.h> 34789Sahrens #include <unistd.h> 355367Sahrens #include <stddef.h> 36789Sahrens #include <zone.h> 372082Seschrock #include <fcntl.h> 38789Sahrens #include <sys/mntent.h> 391294Slling #include <sys/mount.h> 404543Smarks #include <priv.h> 414543Smarks #include <pwd.h> 424543Smarks #include <grp.h> 434543Smarks #include <stddef.h> 444543Smarks #include <ucred.h> 459396SMatthew.Ahrens@Sun.COM #include <idmap.h> 469396SMatthew.Ahrens@Sun.COM #include <aclutils.h> 4710160SMatthew.Ahrens@Sun.COM #include <directory.h> 48789Sahrens 49789Sahrens #include <sys/spa.h> 502676Seschrock #include <sys/zap.h> 51789Sahrens #include <libzfs.h> 52789Sahrens 53789Sahrens #include "zfs_namecheck.h" 54789Sahrens #include "zfs_prop.h" 55789Sahrens #include "libzfs_impl.h" 564543Smarks #include "zfs_deleg.h" 57789Sahrens 589396SMatthew.Ahrens@Sun.COM static int userquota_propname_decode(const char *propname, boolean_t zoned, 599396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 604007Smmusante 61789Sahrens /* 62789Sahrens * Given a single type (not a mask of types), return the type in a human 63789Sahrens * readable form. 64789Sahrens */ 65789Sahrens const char * 66789Sahrens zfs_type_to_name(zfs_type_t type) 67789Sahrens { 68789Sahrens switch (type) { 69789Sahrens case ZFS_TYPE_FILESYSTEM: 70789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 71789Sahrens case ZFS_TYPE_SNAPSHOT: 72789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 73789Sahrens case ZFS_TYPE_VOLUME: 74789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 75789Sahrens } 76789Sahrens 77789Sahrens return (NULL); 78789Sahrens } 79789Sahrens 80789Sahrens /* 81789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 82789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 83789Sahrens * We guess what the type would have been based on the path and the mask of 84789Sahrens * acceptable types. 85789Sahrens */ 86789Sahrens static const char * 87789Sahrens path_to_str(const char *path, int types) 88789Sahrens { 89789Sahrens /* 90789Sahrens * When given a single type, always report the exact type. 91789Sahrens */ 92789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 93789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 94789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 95789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 96789Sahrens if (types == ZFS_TYPE_VOLUME) 97789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 98789Sahrens 99789Sahrens /* 100789Sahrens * The user is requesting more than one type of dataset. If this is the 101789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 102789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 103789Sahrens * snapshot attribute and try again. 104789Sahrens */ 105789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 106789Sahrens if (strchr(path, '@') != NULL) 107789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 108789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 109789Sahrens } 110789Sahrens 111789Sahrens /* 112789Sahrens * The user has requested either filesystems or volumes. 113789Sahrens * We have no way of knowing a priori what type this would be, so always 114789Sahrens * report it as "filesystem" or "volume", our two primitive types. 115789Sahrens */ 116789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 117789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 118789Sahrens 119789Sahrens assert(types & ZFS_TYPE_VOLUME); 120789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 121789Sahrens } 122789Sahrens 123789Sahrens /* 124789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 1259396SMatthew.Ahrens@Sun.COM * provide a more meaningful error message. We call zfs_error_aux() to 1269396SMatthew.Ahrens@Sun.COM * explain exactly why the name was not valid. 127789Sahrens */ 128789Sahrens static int 1295326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 1305326Sek110237 boolean_t modifying) 131789Sahrens { 132789Sahrens namecheck_err_t why; 133789Sahrens char what; 134789Sahrens 135789Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 1362082Seschrock if (hdl != NULL) { 137789Sahrens switch (why) { 1381003Slling case NAME_ERR_TOOLONG: 1392082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1402082Seschrock "name is too long")); 1411003Slling break; 1421003Slling 143789Sahrens case NAME_ERR_LEADING_SLASH: 1442082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1452082Seschrock "leading slash in name")); 146789Sahrens break; 147789Sahrens 148789Sahrens case NAME_ERR_EMPTY_COMPONENT: 1492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1502082Seschrock "empty component in name")); 151789Sahrens break; 152789Sahrens 153789Sahrens case NAME_ERR_TRAILING_SLASH: 1542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1552082Seschrock "trailing slash in name")); 156789Sahrens break; 157789Sahrens 158789Sahrens case NAME_ERR_INVALCHAR: 1592082Seschrock zfs_error_aux(hdl, 160789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 1612082Seschrock "'%c' in name"), what); 162789Sahrens break; 163789Sahrens 164789Sahrens case NAME_ERR_MULTIPLE_AT: 1652082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1662082Seschrock "multiple '@' delimiters in name")); 167789Sahrens break; 1682856Snd150628 1692856Snd150628 case NAME_ERR_NOLETTER: 1702856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1712856Snd150628 "pool doesn't begin with a letter")); 1722856Snd150628 break; 1732856Snd150628 1742856Snd150628 case NAME_ERR_RESERVED: 1752856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1762856Snd150628 "name is reserved")); 1772856Snd150628 break; 1782856Snd150628 1792856Snd150628 case NAME_ERR_DISKLIKE: 1802856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1812856Snd150628 "reserved disk name")); 1822856Snd150628 break; 183789Sahrens } 184789Sahrens } 185789Sahrens 186789Sahrens return (0); 187789Sahrens } 188789Sahrens 189789Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 1902082Seschrock if (hdl != NULL) 1912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1922082Seschrock "snapshot delimiter '@' in filesystem name")); 193789Sahrens return (0); 194789Sahrens } 195789Sahrens 1962199Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 1972199Sahrens if (hdl != NULL) 1982199Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1993413Smmusante "missing '@' delimiter in snapshot name")); 2002199Sahrens return (0); 2012199Sahrens } 2022199Sahrens 2035326Sek110237 if (modifying && strchr(path, '%') != NULL) { 2045326Sek110237 if (hdl != NULL) 2055326Sek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2065326Sek110237 "invalid character %c in name"), '%'); 2075326Sek110237 return (0); 2085326Sek110237 } 2095326Sek110237 2102082Seschrock return (-1); 211789Sahrens } 212789Sahrens 213789Sahrens int 214789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 215789Sahrens { 2166423Sgw25295 if (type == ZFS_TYPE_POOL) 2176423Sgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 2185326Sek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 219789Sahrens } 220789Sahrens 221789Sahrens /* 2222676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2232676Seschrock * properties into a separate nvlist. 2242676Seschrock */ 2254217Seschrock static nvlist_t * 2264217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2272676Seschrock { 2282676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2292676Seschrock nvpair_t *elem; 2302676Seschrock nvlist_t *propval; 2314217Seschrock nvlist_t *nvl; 2324217Seschrock 2334217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2344217Seschrock (void) no_memory(hdl); 2354217Seschrock return (NULL); 2364217Seschrock } 2372676Seschrock 2382676Seschrock elem = NULL; 2394217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2402676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2412676Seschrock continue; 2422676Seschrock 2432676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2444217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2454217Seschrock nvlist_free(nvl); 2464217Seschrock (void) no_memory(hdl); 2474217Seschrock return (NULL); 2484217Seschrock } 2492676Seschrock } 2502676Seschrock 2514217Seschrock return (nvl); 2522676Seschrock } 2532676Seschrock 2546865Srm160521 static zpool_handle_t * 2556865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 2566865Srm160521 { 2576865Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 2586865Srm160521 zpool_handle_t *zph; 2596865Srm160521 2606865Srm160521 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 2616865Srm160521 if (hdl->libzfs_pool_handles != NULL) 2626865Srm160521 zph->zpool_next = hdl->libzfs_pool_handles; 2636865Srm160521 hdl->libzfs_pool_handles = zph; 2646865Srm160521 } 2656865Srm160521 return (zph); 2666865Srm160521 } 2676865Srm160521 2686865Srm160521 static zpool_handle_t * 2696865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 2706865Srm160521 { 2716865Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 2726865Srm160521 zpool_handle_t *zph = hdl->libzfs_pool_handles; 2736865Srm160521 2746865Srm160521 while ((zph != NULL) && 2756865Srm160521 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 2766865Srm160521 zph = zph->zpool_next; 2776865Srm160521 return (zph); 2786865Srm160521 } 2796865Srm160521 2806865Srm160521 /* 2816865Srm160521 * Returns a handle to the pool that contains the provided dataset. 2826865Srm160521 * If a handle to that pool already exists then that handle is returned. 2836865Srm160521 * Otherwise, a new handle is created and added to the list of handles. 2846865Srm160521 */ 2856865Srm160521 static zpool_handle_t * 2866865Srm160521 zpool_handle(zfs_handle_t *zhp) 2876865Srm160521 { 2886865Srm160521 char *pool_name; 2896865Srm160521 int len; 2906865Srm160521 zpool_handle_t *zph; 2916865Srm160521 2926865Srm160521 len = strcspn(zhp->zfs_name, "/@") + 1; 2936865Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, len); 2946865Srm160521 (void) strlcpy(pool_name, zhp->zfs_name, len); 2956865Srm160521 2966865Srm160521 zph = zpool_find_handle(zhp, pool_name, len); 2976865Srm160521 if (zph == NULL) 2986865Srm160521 zph = zpool_add_handle(zhp, pool_name); 2996865Srm160521 3006865Srm160521 free(pool_name); 3016865Srm160521 return (zph); 3026865Srm160521 } 3036865Srm160521 3046865Srm160521 void 3056865Srm160521 zpool_free_handles(libzfs_handle_t *hdl) 3066865Srm160521 { 3076865Srm160521 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 3086865Srm160521 3096865Srm160521 while (zph != NULL) { 3106865Srm160521 next = zph->zpool_next; 3116865Srm160521 zpool_close(zph); 3126865Srm160521 zph = next; 3136865Srm160521 } 3146865Srm160521 hdl->libzfs_pool_handles = NULL; 3156865Srm160521 } 3166865Srm160521 3172676Seschrock /* 318789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 319789Sahrens */ 320789Sahrens static int 3218228SEric.Taylor@Sun.COM get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 322789Sahrens { 3232676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3248228SEric.Taylor@Sun.COM 3258228SEric.Taylor@Sun.COM (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 3268228SEric.Taylor@Sun.COM 3278228SEric.Taylor@Sun.COM while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3281356Seschrock if (errno == ENOMEM) { 3298228SEric.Taylor@Sun.COM if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 3302082Seschrock return (-1); 3312676Seschrock } 3321356Seschrock } else { 3331356Seschrock return (-1); 3341356Seschrock } 3351356Seschrock } 3368228SEric.Taylor@Sun.COM return (0); 3378228SEric.Taylor@Sun.COM } 3388228SEric.Taylor@Sun.COM 3398228SEric.Taylor@Sun.COM static int 3408228SEric.Taylor@Sun.COM put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 3418228SEric.Taylor@Sun.COM { 3428228SEric.Taylor@Sun.COM nvlist_t *allprops, *userprops; 3438228SEric.Taylor@Sun.COM 3448228SEric.Taylor@Sun.COM zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 3458228SEric.Taylor@Sun.COM 3468228SEric.Taylor@Sun.COM if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 3472082Seschrock return (-1); 3482082Seschrock } 349789Sahrens 3509396SMatthew.Ahrens@Sun.COM /* 3519396SMatthew.Ahrens@Sun.COM * XXX Why do we store the user props separately, in addition to 3529396SMatthew.Ahrens@Sun.COM * storing them in zfs_props? 3539396SMatthew.Ahrens@Sun.COM */ 3544217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 3554217Seschrock nvlist_free(allprops); 3562676Seschrock return (-1); 3574217Seschrock } 3584217Seschrock 3594217Seschrock nvlist_free(zhp->zfs_props); 3604217Seschrock nvlist_free(zhp->zfs_user_props); 3614217Seschrock 3624217Seschrock zhp->zfs_props = allprops; 3634217Seschrock zhp->zfs_user_props = userprops; 3642082Seschrock 365789Sahrens return (0); 366789Sahrens } 367789Sahrens 3688228SEric.Taylor@Sun.COM static int 3698228SEric.Taylor@Sun.COM get_stats(zfs_handle_t *zhp) 3708228SEric.Taylor@Sun.COM { 3718228SEric.Taylor@Sun.COM int rc = 0; 3728228SEric.Taylor@Sun.COM zfs_cmd_t zc = { 0 }; 3738228SEric.Taylor@Sun.COM 3748228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 3758228SEric.Taylor@Sun.COM return (-1); 3768228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, &zc) != 0) 3778228SEric.Taylor@Sun.COM rc = -1; 3788228SEric.Taylor@Sun.COM else if (put_stats_zhdl(zhp, &zc) != 0) 3798228SEric.Taylor@Sun.COM rc = -1; 3808228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 3818228SEric.Taylor@Sun.COM return (rc); 3828228SEric.Taylor@Sun.COM } 3838228SEric.Taylor@Sun.COM 384789Sahrens /* 385789Sahrens * Refresh the properties currently stored in the handle. 386789Sahrens */ 387789Sahrens void 388789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 389789Sahrens { 390789Sahrens (void) get_stats(zhp); 391789Sahrens } 392789Sahrens 393789Sahrens /* 394789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 395789Sahrens * zfs_iter_* to create child handles on the fly. 396789Sahrens */ 3978228SEric.Taylor@Sun.COM static int 3988228SEric.Taylor@Sun.COM make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 399789Sahrens { 40010298SMatthew.Ahrens@Sun.COM if (put_stats_zhdl(zhp, zc) != 0) 4018228SEric.Taylor@Sun.COM return (-1); 4021758Sahrens 403789Sahrens /* 404789Sahrens * We've managed to open the dataset and gather statistics. Determine 405789Sahrens * the high-level type. 406789Sahrens */ 4072885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 4082885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 4092885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 4102885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 4112885Sahrens else 4122885Sahrens abort(); 4132885Sahrens 414789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 415789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 416789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 417789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 418789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 419789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 420789Sahrens else 4212082Seschrock abort(); /* we should never see any other types */ 422789Sahrens 4236865Srm160521 zhp->zpool_hdl = zpool_handle(zhp); 4248228SEric.Taylor@Sun.COM return (0); 4258228SEric.Taylor@Sun.COM } 4268228SEric.Taylor@Sun.COM 4278228SEric.Taylor@Sun.COM zfs_handle_t * 4288228SEric.Taylor@Sun.COM make_dataset_handle(libzfs_handle_t *hdl, const char *path) 4298228SEric.Taylor@Sun.COM { 4308228SEric.Taylor@Sun.COM zfs_cmd_t zc = { 0 }; 4318228SEric.Taylor@Sun.COM 4328228SEric.Taylor@Sun.COM zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 4338228SEric.Taylor@Sun.COM 4348228SEric.Taylor@Sun.COM if (zhp == NULL) 4358228SEric.Taylor@Sun.COM return (NULL); 4368228SEric.Taylor@Sun.COM 4378228SEric.Taylor@Sun.COM zhp->zfs_hdl = hdl; 4388228SEric.Taylor@Sun.COM (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 4398228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 4408228SEric.Taylor@Sun.COM free(zhp); 4418228SEric.Taylor@Sun.COM return (NULL); 4428228SEric.Taylor@Sun.COM } 4438228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, &zc) == -1) { 4448228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 4458228SEric.Taylor@Sun.COM free(zhp); 4468228SEric.Taylor@Sun.COM return (NULL); 4478228SEric.Taylor@Sun.COM } 4488228SEric.Taylor@Sun.COM if (make_dataset_handle_common(zhp, &zc) == -1) { 4498228SEric.Taylor@Sun.COM free(zhp); 4508228SEric.Taylor@Sun.COM zhp = NULL; 4518228SEric.Taylor@Sun.COM } 4528228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 4538228SEric.Taylor@Sun.COM return (zhp); 4548228SEric.Taylor@Sun.COM } 4558228SEric.Taylor@Sun.COM 4568228SEric.Taylor@Sun.COM static zfs_handle_t * 4578228SEric.Taylor@Sun.COM make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 4588228SEric.Taylor@Sun.COM { 4598228SEric.Taylor@Sun.COM zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 4608228SEric.Taylor@Sun.COM 4618228SEric.Taylor@Sun.COM if (zhp == NULL) 4628228SEric.Taylor@Sun.COM return (NULL); 4638228SEric.Taylor@Sun.COM 4648228SEric.Taylor@Sun.COM zhp->zfs_hdl = hdl; 4658228SEric.Taylor@Sun.COM (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 4668228SEric.Taylor@Sun.COM if (make_dataset_handle_common(zhp, zc) == -1) { 4678228SEric.Taylor@Sun.COM free(zhp); 4688228SEric.Taylor@Sun.COM return (NULL); 4698228SEric.Taylor@Sun.COM } 470789Sahrens return (zhp); 471789Sahrens } 472789Sahrens 473789Sahrens /* 474789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 475789Sahrens * argument is a mask of acceptable types. The function will print an 476789Sahrens * appropriate error message and return NULL if it can't be opened. 477789Sahrens */ 478789Sahrens zfs_handle_t * 4792082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 480789Sahrens { 481789Sahrens zfs_handle_t *zhp; 4822082Seschrock char errbuf[1024]; 4832082Seschrock 4842082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 4852082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 486789Sahrens 487789Sahrens /* 4882082Seschrock * Validate the name before we even try to open it. 489789Sahrens */ 4905326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 4912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4922082Seschrock "invalid dataset name")); 4932082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 494789Sahrens return (NULL); 495789Sahrens } 496789Sahrens 497789Sahrens /* 498789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 499789Sahrens */ 500789Sahrens errno = 0; 5012082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 5023237Slling (void) zfs_standard_error(hdl, errno, errbuf); 503789Sahrens return (NULL); 504789Sahrens } 505789Sahrens 506789Sahrens if (!(types & zhp->zfs_type)) { 5072082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 5082142Seschrock zfs_close(zhp); 509789Sahrens return (NULL); 510789Sahrens } 511789Sahrens 512789Sahrens return (zhp); 513789Sahrens } 514789Sahrens 515789Sahrens /* 516789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 517789Sahrens */ 518789Sahrens void 519789Sahrens zfs_close(zfs_handle_t *zhp) 520789Sahrens { 521789Sahrens if (zhp->zfs_mntopts) 522789Sahrens free(zhp->zfs_mntopts); 5232676Seschrock nvlist_free(zhp->zfs_props); 5242676Seschrock nvlist_free(zhp->zfs_user_props); 525789Sahrens free(zhp); 526789Sahrens } 527789Sahrens 5288228SEric.Taylor@Sun.COM typedef struct mnttab_node { 5298228SEric.Taylor@Sun.COM struct mnttab mtn_mt; 5308228SEric.Taylor@Sun.COM avl_node_t mtn_node; 5318228SEric.Taylor@Sun.COM } mnttab_node_t; 5328228SEric.Taylor@Sun.COM 5338228SEric.Taylor@Sun.COM static int 5348228SEric.Taylor@Sun.COM libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 5358228SEric.Taylor@Sun.COM { 5368228SEric.Taylor@Sun.COM const mnttab_node_t *mtn1 = arg1; 5378228SEric.Taylor@Sun.COM const mnttab_node_t *mtn2 = arg2; 5388228SEric.Taylor@Sun.COM int rv; 5398228SEric.Taylor@Sun.COM 5408228SEric.Taylor@Sun.COM rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 5418228SEric.Taylor@Sun.COM 5428228SEric.Taylor@Sun.COM if (rv == 0) 5438228SEric.Taylor@Sun.COM return (0); 5448228SEric.Taylor@Sun.COM return (rv > 0 ? 1 : -1); 5458228SEric.Taylor@Sun.COM } 5468228SEric.Taylor@Sun.COM 5478228SEric.Taylor@Sun.COM void 5488228SEric.Taylor@Sun.COM libzfs_mnttab_init(libzfs_handle_t *hdl) 5498228SEric.Taylor@Sun.COM { 5508228SEric.Taylor@Sun.COM assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 5518228SEric.Taylor@Sun.COM avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 5528228SEric.Taylor@Sun.COM sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 5538811SEric.Taylor@Sun.COM } 5548811SEric.Taylor@Sun.COM 5558811SEric.Taylor@Sun.COM void 5568811SEric.Taylor@Sun.COM libzfs_mnttab_update(libzfs_handle_t *hdl) 5578811SEric.Taylor@Sun.COM { 5588811SEric.Taylor@Sun.COM struct mnttab entry; 5598228SEric.Taylor@Sun.COM 5608228SEric.Taylor@Sun.COM rewind(hdl->libzfs_mnttab); 5618228SEric.Taylor@Sun.COM while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 5628228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 5638228SEric.Taylor@Sun.COM 5648228SEric.Taylor@Sun.COM if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 5658228SEric.Taylor@Sun.COM continue; 5668228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 5678228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 5688228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 5698228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 5708228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 5718228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 5728228SEric.Taylor@Sun.COM } 5738228SEric.Taylor@Sun.COM } 5748228SEric.Taylor@Sun.COM 5758228SEric.Taylor@Sun.COM void 5768228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl) 5778228SEric.Taylor@Sun.COM { 5788228SEric.Taylor@Sun.COM void *cookie = NULL; 5798228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 5808228SEric.Taylor@Sun.COM 5818228SEric.Taylor@Sun.COM while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) { 5828228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_special); 5838228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mountp); 5848228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_fstype); 5858228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mntopts); 5868228SEric.Taylor@Sun.COM free(mtn); 5878228SEric.Taylor@Sun.COM } 5888228SEric.Taylor@Sun.COM avl_destroy(&hdl->libzfs_mnttab_cache); 5898228SEric.Taylor@Sun.COM } 5908228SEric.Taylor@Sun.COM 5918811SEric.Taylor@Sun.COM void 5928811SEric.Taylor@Sun.COM libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 5938811SEric.Taylor@Sun.COM { 5948811SEric.Taylor@Sun.COM hdl->libzfs_mnttab_enable = enable; 5958811SEric.Taylor@Sun.COM } 5968811SEric.Taylor@Sun.COM 5978228SEric.Taylor@Sun.COM int 5988228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 5998228SEric.Taylor@Sun.COM struct mnttab *entry) 6008228SEric.Taylor@Sun.COM { 6018228SEric.Taylor@Sun.COM mnttab_node_t find; 6028228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6038228SEric.Taylor@Sun.COM 6048811SEric.Taylor@Sun.COM if (!hdl->libzfs_mnttab_enable) { 6058811SEric.Taylor@Sun.COM struct mnttab srch = { 0 }; 6068811SEric.Taylor@Sun.COM 6078811SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 6088811SEric.Taylor@Sun.COM libzfs_mnttab_fini(hdl); 6098811SEric.Taylor@Sun.COM rewind(hdl->libzfs_mnttab); 6108811SEric.Taylor@Sun.COM srch.mnt_special = (char *)fsname; 6118811SEric.Taylor@Sun.COM srch.mnt_fstype = MNTTYPE_ZFS; 6128811SEric.Taylor@Sun.COM if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 6138811SEric.Taylor@Sun.COM return (0); 6148811SEric.Taylor@Sun.COM else 6158811SEric.Taylor@Sun.COM return (ENOENT); 6168811SEric.Taylor@Sun.COM } 6178811SEric.Taylor@Sun.COM 6188228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 6198811SEric.Taylor@Sun.COM libzfs_mnttab_update(hdl); 6208228SEric.Taylor@Sun.COM 6218228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 6228228SEric.Taylor@Sun.COM mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 6238228SEric.Taylor@Sun.COM if (mtn) { 6248228SEric.Taylor@Sun.COM *entry = mtn->mtn_mt; 6258228SEric.Taylor@Sun.COM return (0); 6268228SEric.Taylor@Sun.COM } 6278228SEric.Taylor@Sun.COM return (ENOENT); 6288228SEric.Taylor@Sun.COM } 6298228SEric.Taylor@Sun.COM 6308228SEric.Taylor@Sun.COM void 6318228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 6328228SEric.Taylor@Sun.COM const char *mountp, const char *mntopts) 6338228SEric.Taylor@Sun.COM { 6348228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 6358228SEric.Taylor@Sun.COM 6368228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 6378228SEric.Taylor@Sun.COM return; 6388228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 6398228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 6408228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 6418228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 6428228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 6438228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 6448228SEric.Taylor@Sun.COM } 6458228SEric.Taylor@Sun.COM 6468228SEric.Taylor@Sun.COM void 6478228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 6488228SEric.Taylor@Sun.COM { 6498228SEric.Taylor@Sun.COM mnttab_node_t find; 6508228SEric.Taylor@Sun.COM mnttab_node_t *ret; 6518228SEric.Taylor@Sun.COM 6528228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 6538228SEric.Taylor@Sun.COM if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) { 6548228SEric.Taylor@Sun.COM avl_remove(&hdl->libzfs_mnttab_cache, ret); 6558228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_special); 6568228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mountp); 6578228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_fstype); 6588228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mntopts); 6598228SEric.Taylor@Sun.COM free(ret); 6608228SEric.Taylor@Sun.COM } 6618228SEric.Taylor@Sun.COM } 6628228SEric.Taylor@Sun.COM 6635713Srm160521 int 6645713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 6655713Srm160521 { 6666865Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 6676865Srm160521 6685713Srm160521 if (zpool_handle == NULL) 6695713Srm160521 return (-1); 6705713Srm160521 6715713Srm160521 *spa_version = zpool_get_prop_int(zpool_handle, 6725713Srm160521 ZPOOL_PROP_VERSION, NULL); 6735713Srm160521 return (0); 6745713Srm160521 } 6755713Srm160521 6765713Srm160521 /* 6775713Srm160521 * The choice of reservation property depends on the SPA version. 6785713Srm160521 */ 6795713Srm160521 static int 6805713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 6815713Srm160521 { 6825713Srm160521 int spa_version; 6835713Srm160521 6845713Srm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 6855713Srm160521 return (-1); 6865713Srm160521 6875713Srm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 6885713Srm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 6895713Srm160521 else 6905713Srm160521 *resv_prop = ZFS_PROP_RESERVATION; 6915713Srm160521 6925713Srm160521 return (0); 6935713Srm160521 } 6945713Srm160521 6953912Slling /* 6962676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 6972676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 6982676Seschrock * strings. 699789Sahrens */ 7007184Stimh nvlist_t * 7017184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 7025094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 703789Sahrens { 7042676Seschrock nvpair_t *elem; 7052676Seschrock uint64_t intval; 7062676Seschrock char *strval; 7075094Slling zfs_prop_t prop; 7082676Seschrock nvlist_t *ret; 7095331Samw int chosen_normal = -1; 7105331Samw int chosen_utf = -1; 7112676Seschrock 7125094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 7135094Slling (void) no_memory(hdl); 7145094Slling return (NULL); 715789Sahrens } 716789Sahrens 7179396SMatthew.Ahrens@Sun.COM /* 7189396SMatthew.Ahrens@Sun.COM * Make sure this property is valid and applies to this type. 7199396SMatthew.Ahrens@Sun.COM */ 7209396SMatthew.Ahrens@Sun.COM 7212676Seschrock elem = NULL; 7222676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 7235094Slling const char *propname = nvpair_name(elem); 7242676Seschrock 7259396SMatthew.Ahrens@Sun.COM prop = zfs_name_to_prop(propname); 7269396SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { 7275094Slling /* 7289396SMatthew.Ahrens@Sun.COM * This is a user property: make sure it's a 7295094Slling * string, and that it's less than ZAP_MAXNAMELEN. 7305094Slling */ 7315094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 7325094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7335094Slling "'%s' must be a string"), propname); 7345094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7355094Slling goto error; 7365094Slling } 7375094Slling 7385094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 7395094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7405094Slling "property name '%s' is too long"), 7412676Seschrock propname); 7422676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7432676Seschrock goto error; 7442676Seschrock } 7452676Seschrock 7462676Seschrock (void) nvpair_value_string(elem, &strval); 7472676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 7482676Seschrock (void) no_memory(hdl); 7492676Seschrock goto error; 7502676Seschrock } 7512676Seschrock continue; 752789Sahrens } 7532676Seschrock 7549396SMatthew.Ahrens@Sun.COM /* 7559396SMatthew.Ahrens@Sun.COM * Currently, only user properties can be modified on 7569396SMatthew.Ahrens@Sun.COM * snapshots. 7579396SMatthew.Ahrens@Sun.COM */ 7587265Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 7597265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7607265Sahrens "this property can not be modified for snapshots")); 7617265Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 7627265Sahrens goto error; 7637265Sahrens } 7647265Sahrens 7659396SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { 7669396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t uqtype; 7679396SMatthew.Ahrens@Sun.COM char newpropname[128]; 7689396SMatthew.Ahrens@Sun.COM char domain[128]; 7699396SMatthew.Ahrens@Sun.COM uint64_t rid; 7709396SMatthew.Ahrens@Sun.COM uint64_t valary[3]; 7719396SMatthew.Ahrens@Sun.COM 7729396SMatthew.Ahrens@Sun.COM if (userquota_propname_decode(propname, zoned, 7739396SMatthew.Ahrens@Sun.COM &uqtype, domain, sizeof (domain), &rid) != 0) { 7749396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, 7759396SMatthew.Ahrens@Sun.COM dgettext(TEXT_DOMAIN, 7769396SMatthew.Ahrens@Sun.COM "'%s' has an invalid user/group name"), 7779396SMatthew.Ahrens@Sun.COM propname); 7789396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7799396SMatthew.Ahrens@Sun.COM goto error; 7809396SMatthew.Ahrens@Sun.COM } 7819396SMatthew.Ahrens@Sun.COM 7829396SMatthew.Ahrens@Sun.COM if (uqtype != ZFS_PROP_USERQUOTA && 7839396SMatthew.Ahrens@Sun.COM uqtype != ZFS_PROP_GROUPQUOTA) { 7849396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, 7859396SMatthew.Ahrens@Sun.COM dgettext(TEXT_DOMAIN, "'%s' is readonly"), 7869396SMatthew.Ahrens@Sun.COM propname); 7879396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, EZFS_PROPREADONLY, 7889396SMatthew.Ahrens@Sun.COM errbuf); 7899396SMatthew.Ahrens@Sun.COM goto error; 7909396SMatthew.Ahrens@Sun.COM } 7919396SMatthew.Ahrens@Sun.COM 7929396SMatthew.Ahrens@Sun.COM if (nvpair_type(elem) == DATA_TYPE_STRING) { 7939396SMatthew.Ahrens@Sun.COM (void) nvpair_value_string(elem, &strval); 7949396SMatthew.Ahrens@Sun.COM if (strcmp(strval, "none") == 0) { 7959396SMatthew.Ahrens@Sun.COM intval = 0; 7969396SMatthew.Ahrens@Sun.COM } else if (zfs_nicestrtonum(hdl, 7979396SMatthew.Ahrens@Sun.COM strval, &intval) != 0) { 7989396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, 7999396SMatthew.Ahrens@Sun.COM EZFS_BADPROP, errbuf); 8009396SMatthew.Ahrens@Sun.COM goto error; 8019396SMatthew.Ahrens@Sun.COM } 8029396SMatthew.Ahrens@Sun.COM } else if (nvpair_type(elem) == 8039396SMatthew.Ahrens@Sun.COM DATA_TYPE_UINT64) { 8049396SMatthew.Ahrens@Sun.COM (void) nvpair_value_uint64(elem, &intval); 8059396SMatthew.Ahrens@Sun.COM if (intval == 0) { 8069396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8079396SMatthew.Ahrens@Sun.COM "use 'none' to disable " 8089396SMatthew.Ahrens@Sun.COM "userquota/groupquota")); 8099396SMatthew.Ahrens@Sun.COM goto error; 8109396SMatthew.Ahrens@Sun.COM } 8119396SMatthew.Ahrens@Sun.COM } else { 8129396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8139396SMatthew.Ahrens@Sun.COM "'%s' must be a number"), propname); 8149396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8159396SMatthew.Ahrens@Sun.COM goto error; 8169396SMatthew.Ahrens@Sun.COM } 8179396SMatthew.Ahrens@Sun.COM 818*10969SMatthew.Ahrens@Sun.COM /* 819*10969SMatthew.Ahrens@Sun.COM * Encode the prop name as 820*10969SMatthew.Ahrens@Sun.COM * userquota@<hex-rid>-domain, to make it easy 821*10969SMatthew.Ahrens@Sun.COM * for the kernel to decode. 822*10969SMatthew.Ahrens@Sun.COM */ 8239396SMatthew.Ahrens@Sun.COM (void) snprintf(newpropname, sizeof (newpropname), 824*10969SMatthew.Ahrens@Sun.COM "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype], 825*10969SMatthew.Ahrens@Sun.COM (longlong_t)rid, domain); 8269396SMatthew.Ahrens@Sun.COM valary[0] = uqtype; 8279396SMatthew.Ahrens@Sun.COM valary[1] = rid; 8289396SMatthew.Ahrens@Sun.COM valary[2] = intval; 8299396SMatthew.Ahrens@Sun.COM if (nvlist_add_uint64_array(ret, newpropname, 8309396SMatthew.Ahrens@Sun.COM valary, 3) != 0) { 8319396SMatthew.Ahrens@Sun.COM (void) no_memory(hdl); 8329396SMatthew.Ahrens@Sun.COM goto error; 8339396SMatthew.Ahrens@Sun.COM } 8349396SMatthew.Ahrens@Sun.COM continue; 8359396SMatthew.Ahrens@Sun.COM } 8369396SMatthew.Ahrens@Sun.COM 8379396SMatthew.Ahrens@Sun.COM if (prop == ZPROP_INVAL) { 8389396SMatthew.Ahrens@Sun.COM zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8399396SMatthew.Ahrens@Sun.COM "invalid property '%s'"), propname); 8409396SMatthew.Ahrens@Sun.COM (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8419396SMatthew.Ahrens@Sun.COM goto error; 8429396SMatthew.Ahrens@Sun.COM } 8439396SMatthew.Ahrens@Sun.COM 8442676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 8452676Seschrock zfs_error_aux(hdl, 8462676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 8472676Seschrock "apply to datasets of this type"), propname); 8482676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 8492676Seschrock goto error; 8502676Seschrock } 8512676Seschrock 8522676Seschrock if (zfs_prop_readonly(prop) && 8535331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 8542676Seschrock zfs_error_aux(hdl, 8552676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 8562676Seschrock propname); 8572676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 8582676Seschrock goto error; 8592676Seschrock } 8602676Seschrock 8615094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 8625094Slling &strval, &intval, errbuf) != 0) 8632676Seschrock goto error; 8642676Seschrock 8652676Seschrock /* 8662676Seschrock * Perform some additional checks for specific properties. 8672676Seschrock */ 8682676Seschrock switch (prop) { 8694577Sahrens case ZFS_PROP_VERSION: 8704577Sahrens { 8714577Sahrens int version; 8724577Sahrens 8734577Sahrens if (zhp == NULL) 8744577Sahrens break; 8754577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 8764577Sahrens if (intval < version) { 8774577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8784577Sahrens "Can not downgrade; already at version %u"), 8794577Sahrens version); 8804577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8814577Sahrens goto error; 8824577Sahrens } 8834577Sahrens break; 8844577Sahrens } 8854577Sahrens 8862676Seschrock case ZFS_PROP_RECORDSIZE: 8872676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 8882676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 8892676Seschrock if (intval < SPA_MINBLOCKSIZE || 8902676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 8912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8922676Seschrock "'%s' must be power of 2 from %u " 8932676Seschrock "to %uk"), propname, 8942676Seschrock (uint_t)SPA_MINBLOCKSIZE, 8952676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 8962676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8972676Seschrock goto error; 898789Sahrens } 899789Sahrens break; 900789Sahrens 9013126Sahl case ZFS_PROP_SHAREISCSI: 9023126Sahl if (strcmp(strval, "off") != 0 && 9033126Sahl strcmp(strval, "on") != 0 && 9043126Sahl strcmp(strval, "type=disk") != 0) { 9053126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9063126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 9073126Sahl propname); 9083126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9093126Sahl goto error; 9103126Sahl } 9113126Sahl 9123126Sahl break; 9133126Sahl 9142676Seschrock case ZFS_PROP_MOUNTPOINT: 9154778Srm160521 { 9164778Srm160521 namecheck_err_t why; 9174778Srm160521 9182676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 9192676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 9202676Seschrock break; 9212676Seschrock 9224778Srm160521 if (mountpoint_namecheck(strval, &why)) { 9234778Srm160521 switch (why) { 9244778Srm160521 case NAME_ERR_LEADING_SLASH: 9254778Srm160521 zfs_error_aux(hdl, 9264778Srm160521 dgettext(TEXT_DOMAIN, 9274778Srm160521 "'%s' must be an absolute path, " 9284778Srm160521 "'none', or 'legacy'"), propname); 9294778Srm160521 break; 9304778Srm160521 case NAME_ERR_TOOLONG: 9314778Srm160521 zfs_error_aux(hdl, 9324778Srm160521 dgettext(TEXT_DOMAIN, 9334778Srm160521 "component of '%s' is too long"), 9344778Srm160521 propname); 9354778Srm160521 break; 9364778Srm160521 } 9372676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 9382676Seschrock goto error; 939789Sahrens } 9404778Srm160521 } 9414778Srm160521 9423126Sahl /*FALLTHRU*/ 9433126Sahl 9445331Samw case ZFS_PROP_SHARESMB: 9453126Sahl case ZFS_PROP_SHARENFS: 9463126Sahl /* 9475331Samw * For the mountpoint and sharenfs or sharesmb 9485331Samw * properties, check if it can be set in a 9495331Samw * global/non-global zone based on 9503126Sahl * the zoned property value: 9513126Sahl * 9523126Sahl * global zone non-global zone 9533126Sahl * -------------------------------------------------- 9543126Sahl * zoned=on mountpoint (no) mountpoint (yes) 9553126Sahl * sharenfs (no) sharenfs (no) 9565331Samw * sharesmb (no) sharesmb (no) 9573126Sahl * 9583126Sahl * zoned=off mountpoint (yes) N/A 9593126Sahl * sharenfs (yes) 9605331Samw * sharesmb (yes) 9613126Sahl */ 9622676Seschrock if (zoned) { 9632676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 9642676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9652676Seschrock "'%s' cannot be set on " 9662676Seschrock "dataset in a non-global zone"), 9672676Seschrock propname); 9682676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9692676Seschrock errbuf); 9702676Seschrock goto error; 9715331Samw } else if (prop == ZFS_PROP_SHARENFS || 9725331Samw prop == ZFS_PROP_SHARESMB) { 9732676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9742676Seschrock "'%s' cannot be set in " 9752676Seschrock "a non-global zone"), propname); 9762676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9772676Seschrock errbuf); 9782676Seschrock goto error; 9792676Seschrock } 9802676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 9812676Seschrock /* 9822676Seschrock * If zoned property is 'off', this must be in 9839396SMatthew.Ahrens@Sun.COM * a global zone. If not, something is wrong. 9842676Seschrock */ 9852676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9862676Seschrock "'%s' cannot be set while dataset " 9872676Seschrock "'zoned' property is set"), propname); 9882676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 9892676Seschrock goto error; 9902676Seschrock } 9913126Sahl 9924180Sdougm /* 9934180Sdougm * At this point, it is legitimate to set the 9944180Sdougm * property. Now we want to make sure that the 9954180Sdougm * property value is valid if it is sharenfs. 9964180Sdougm */ 9975331Samw if ((prop == ZFS_PROP_SHARENFS || 9985331Samw prop == ZFS_PROP_SHARESMB) && 9994217Seschrock strcmp(strval, "on") != 0 && 10004217Seschrock strcmp(strval, "off") != 0) { 10015331Samw zfs_share_proto_t proto; 10025331Samw 10035331Samw if (prop == ZFS_PROP_SHARESMB) 10045331Samw proto = PROTO_SMB; 10055331Samw else 10065331Samw proto = PROTO_NFS; 10074180Sdougm 10084180Sdougm /* 10095331Samw * Must be an valid sharing protocol 10105331Samw * option string so init the libshare 10115331Samw * in order to enable the parser and 10125331Samw * then parse the options. We use the 10135331Samw * control API since we don't care about 10145331Samw * the current configuration and don't 10154180Sdougm * want the overhead of loading it 10164180Sdougm * until we actually do something. 10174180Sdougm */ 10184180Sdougm 10194217Seschrock if (zfs_init_libshare(hdl, 10204217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 10214217Seschrock /* 10224217Seschrock * An error occurred so we can't do 10234217Seschrock * anything 10244217Seschrock */ 10254217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10264217Seschrock "'%s' cannot be set: problem " 10274217Seschrock "in share initialization"), 10284217Seschrock propname); 10294217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10304217Seschrock errbuf); 10314217Seschrock goto error; 10324217Seschrock } 10334217Seschrock 10345331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 10354217Seschrock /* 10364217Seschrock * There was an error in parsing so 10374217Seschrock * deal with it by issuing an error 10384217Seschrock * message and leaving after 10394217Seschrock * uninitializing the the libshare 10404217Seschrock * interface. 10414217Seschrock */ 10424217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10434217Seschrock "'%s' cannot be set to invalid " 10444217Seschrock "options"), propname); 10454217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10464217Seschrock errbuf); 10474217Seschrock zfs_uninit_libshare(hdl); 10484217Seschrock goto error; 10494217Seschrock } 10504180Sdougm zfs_uninit_libshare(hdl); 10514180Sdougm } 10524180Sdougm 10533126Sahl break; 10545331Samw case ZFS_PROP_UTF8ONLY: 10555331Samw chosen_utf = (int)intval; 10565331Samw break; 10575331Samw case ZFS_PROP_NORMALIZE: 10585331Samw chosen_normal = (int)intval; 10595331Samw break; 10602676Seschrock } 10612676Seschrock 10622676Seschrock /* 10632676Seschrock * For changes to existing volumes, we have some additional 10642676Seschrock * checks to enforce. 10652676Seschrock */ 10662676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 10672676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 10682676Seschrock ZFS_PROP_VOLSIZE); 10692676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 10702676Seschrock ZFS_PROP_VOLBLOCKSIZE); 10712676Seschrock char buf[64]; 10722676Seschrock 10732676Seschrock switch (prop) { 10742676Seschrock case ZFS_PROP_RESERVATION: 10755378Sck153898 case ZFS_PROP_REFRESERVATION: 10762676Seschrock if (intval > volsize) { 10772676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10782676Seschrock "'%s' is greater than current " 10792676Seschrock "volume size"), propname); 10802676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10812676Seschrock errbuf); 10822676Seschrock goto error; 10832676Seschrock } 10842676Seschrock break; 10852676Seschrock 10862676Seschrock case ZFS_PROP_VOLSIZE: 10872676Seschrock if (intval % blocksize != 0) { 10882676Seschrock zfs_nicenum(blocksize, buf, 10892676Seschrock sizeof (buf)); 10902676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10912676Seschrock "'%s' must be a multiple of " 10922676Seschrock "volume block size (%s)"), 10932676Seschrock propname, buf); 10942676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10952676Seschrock errbuf); 10962676Seschrock goto error; 10972676Seschrock } 10982676Seschrock 10992676Seschrock if (intval == 0) { 11002676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11012676Seschrock "'%s' cannot be zero"), 11022676Seschrock propname); 11032676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 11042676Seschrock errbuf); 11052676Seschrock goto error; 1106789Sahrens } 11073126Sahl break; 1108789Sahrens } 1109789Sahrens } 1110789Sahrens } 1111789Sahrens 11122676Seschrock /* 11135331Samw * If normalization was chosen, but no UTF8 choice was made, 11145331Samw * enforce rejection of non-UTF8 names. 11155331Samw * 11165331Samw * If normalization was chosen, but rejecting non-UTF8 names 11175331Samw * was explicitly not chosen, it is an error. 11185331Samw */ 11195498Stimh if (chosen_normal > 0 && chosen_utf < 0) { 11205331Samw if (nvlist_add_uint64(ret, 11215331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 11225331Samw (void) no_memory(hdl); 11235331Samw goto error; 11245331Samw } 11255498Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 11265331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11275331Samw "'%s' must be set 'on' if normalization chosen"), 11285331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 11295331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 11305331Samw goto error; 11315331Samw } 11325331Samw 11335331Samw /* 11342676Seschrock * If this is an existing volume, and someone is setting the volsize, 11352676Seschrock * make sure that it matches the reservation, or add it if necessary. 11362676Seschrock */ 11372676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 11382676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 11392676Seschrock &intval) == 0) { 11402676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 11412676Seschrock ZFS_PROP_VOLSIZE); 11425481Sck153898 uint64_t old_reservation; 11432676Seschrock uint64_t new_reservation; 11445481Sck153898 zfs_prop_t resv_prop; 11455713Srm160521 11465713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 11475481Sck153898 goto error; 11485481Sck153898 old_reservation = zfs_prop_get_int(zhp, resv_prop); 11492676Seschrock 11502676Seschrock if (old_volsize == old_reservation && 11515481Sck153898 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop), 11522676Seschrock &new_reservation) != 0) { 11532676Seschrock if (nvlist_add_uint64(ret, 11545481Sck153898 zfs_prop_to_name(resv_prop), intval) != 0) { 11552676Seschrock (void) no_memory(hdl); 11562676Seschrock goto error; 11572676Seschrock } 11582676Seschrock } 11592676Seschrock } 11602676Seschrock return (ret); 11612676Seschrock 11622676Seschrock error: 11632676Seschrock nvlist_free(ret); 11642676Seschrock return (NULL); 1165789Sahrens } 1166789Sahrens 1167789Sahrens /* 1168789Sahrens * Given a property name and value, set the property for the given dataset. 1169789Sahrens */ 1170789Sahrens int 11712676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1172789Sahrens { 1173789Sahrens zfs_cmd_t zc = { 0 }; 11742676Seschrock int ret = -1; 11752676Seschrock prop_changelist_t *cl = NULL; 11762082Seschrock char errbuf[1024]; 11772082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 11782676Seschrock nvlist_t *nvl = NULL, *realprops; 11792676Seschrock zfs_prop_t prop; 11807509SMark.Musante@Sun.COM boolean_t do_prefix; 11817509SMark.Musante@Sun.COM uint64_t idx; 11822082Seschrock 11832082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 11842676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 11852082Seschrock zhp->zfs_name); 11862082Seschrock 11872676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 11882676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 11892676Seschrock (void) no_memory(hdl); 11902676Seschrock goto error; 1191789Sahrens } 1192789Sahrens 11937184Stimh if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl, 11942676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 11952676Seschrock goto error; 11965094Slling 11972676Seschrock nvlist_free(nvl); 11982676Seschrock nvl = realprops; 11992676Seschrock 12002676Seschrock prop = zfs_name_to_prop(propname); 12012676Seschrock 12027366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 12032676Seschrock goto error; 1204789Sahrens 1205789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 12062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12072082Seschrock "child dataset with inherited mountpoint is used " 12082082Seschrock "in a non-global zone")); 12092082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1210789Sahrens goto error; 1211789Sahrens } 1212789Sahrens 12137509SMark.Musante@Sun.COM /* 12147509SMark.Musante@Sun.COM * If the dataset's canmount property is being set to noauto, 12157509SMark.Musante@Sun.COM * then we want to prevent unmounting & remounting it. 12167509SMark.Musante@Sun.COM */ 12177509SMark.Musante@Sun.COM do_prefix = !((prop == ZFS_PROP_CANMOUNT) && 12187509SMark.Musante@Sun.COM (zprop_string_to_index(prop, propval, &idx, 12197509SMark.Musante@Sun.COM ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO)); 12206168Shs24103 12216168Shs24103 if (do_prefix && (ret = changelist_prefix(cl)) != 0) 12227509SMark.Musante@Sun.COM goto error; 1223789Sahrens 1224789Sahrens /* 1225789Sahrens * Execute the corresponding ioctl() to set this property. 1226789Sahrens */ 1227789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1228789Sahrens 12295094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 12302676Seschrock goto error; 12312676Seschrock 12324543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 12338845Samw@Sun.COM 1234789Sahrens if (ret != 0) { 1235789Sahrens switch (errno) { 1236789Sahrens 1237789Sahrens case ENOSPC: 1238789Sahrens /* 1239789Sahrens * For quotas and reservations, ENOSPC indicates 1240789Sahrens * something different; setting a quota or reservation 1241789Sahrens * doesn't use any disk space. 1242789Sahrens */ 1243789Sahrens switch (prop) { 1244789Sahrens case ZFS_PROP_QUOTA: 12455378Sck153898 case ZFS_PROP_REFQUOTA: 12462082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12472082Seschrock "size is less than current used or " 12482082Seschrock "reserved space")); 12492082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1250789Sahrens break; 1251789Sahrens 1252789Sahrens case ZFS_PROP_RESERVATION: 12535378Sck153898 case ZFS_PROP_REFRESERVATION: 12542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12552082Seschrock "size is greater than available space")); 12562082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1257789Sahrens break; 1258789Sahrens 1259789Sahrens default: 12602082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1261789Sahrens break; 1262789Sahrens } 1263789Sahrens break; 1264789Sahrens 1265789Sahrens case EBUSY: 126610588SEric.Taylor@Sun.COM (void) zfs_standard_error(hdl, EBUSY, errbuf); 1267789Sahrens break; 1268789Sahrens 12691175Slling case EROFS: 12702082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 12711175Slling break; 12721175Slling 12733886Sahl case ENOTSUP: 12743886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12755977Smarks "pool and or dataset must be upgraded to set this " 12764603Sahrens "property or value")); 12773886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 12783886Sahl break; 12793886Sahl 12807042Sgw25295 case ERANGE: 12817042Sgw25295 if (prop == ZFS_PROP_COMPRESSION) { 12827042Sgw25295 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12837042Sgw25295 "property setting is not allowed on " 12847042Sgw25295 "bootable datasets")); 12857042Sgw25295 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 12867042Sgw25295 } else { 12877042Sgw25295 (void) zfs_standard_error(hdl, errno, errbuf); 12887042Sgw25295 } 12897042Sgw25295 break; 12907042Sgw25295 1291789Sahrens case EOVERFLOW: 1292789Sahrens /* 1293789Sahrens * This platform can't address a volume this big. 1294789Sahrens */ 1295789Sahrens #ifdef _ILP32 1296789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 12972082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1298789Sahrens break; 1299789Sahrens } 1300789Sahrens #endif 13012082Seschrock /* FALLTHROUGH */ 1302789Sahrens default: 13032082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1304789Sahrens } 1305789Sahrens } else { 13066168Shs24103 if (do_prefix) 13076168Shs24103 ret = changelist_postfix(cl); 13086168Shs24103 1309789Sahrens /* 1310789Sahrens * Refresh the statistics so the new property value 1311789Sahrens * is reflected. 1312789Sahrens */ 13136168Shs24103 if (ret == 0) 13142676Seschrock (void) get_stats(zhp); 1315789Sahrens } 1316789Sahrens 1317789Sahrens error: 13182676Seschrock nvlist_free(nvl); 13192676Seschrock zcmd_free_nvlists(&zc); 13202676Seschrock if (cl) 13212676Seschrock changelist_free(cl); 1322789Sahrens return (ret); 1323789Sahrens } 1324789Sahrens 1325789Sahrens /* 1326789Sahrens * Given a property, inherit the value from the parent dataset. 1327789Sahrens */ 1328789Sahrens int 13292676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1330789Sahrens { 1331789Sahrens zfs_cmd_t zc = { 0 }; 1332789Sahrens int ret; 1333789Sahrens prop_changelist_t *cl; 13342082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 13352082Seschrock char errbuf[1024]; 13362676Seschrock zfs_prop_t prop; 13372082Seschrock 13382082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 13392082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1340789Sahrens 13415094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 13422676Seschrock /* 13432676Seschrock * For user properties, the amount of work we have to do is very 13442676Seschrock * small, so just do it here. 13452676Seschrock */ 13462676Seschrock if (!zfs_prop_user(propname)) { 13472676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13482676Seschrock "invalid property")); 13492676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 13502676Seschrock } 13512676Seschrock 13522676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13532676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 13542676Seschrock 13554849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 13562676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 13572676Seschrock 13582676Seschrock return (0); 13592676Seschrock } 13602676Seschrock 1361789Sahrens /* 1362789Sahrens * Verify that this property is inheritable. 1363789Sahrens */ 13642082Seschrock if (zfs_prop_readonly(prop)) 13652082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 13662082Seschrock 13672082Seschrock if (!zfs_prop_inheritable(prop)) 13682082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1369789Sahrens 1370789Sahrens /* 1371789Sahrens * Check to see if the value applies to this type 1372789Sahrens */ 13732082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 13742082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1375789Sahrens 13763443Srm160521 /* 13773443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 13783443Srm160521 */ 13793443Srm160521 propname = zfs_prop_to_name(prop); 1380789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13812676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1382789Sahrens 1383789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1384789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 13852082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13862082Seschrock "dataset is used in a non-global zone")); 13872082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1388789Sahrens } 1389789Sahrens 1390789Sahrens /* 1391789Sahrens * Determine datasets which will be affected by this change, if any. 1392789Sahrens */ 13937366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1394789Sahrens return (-1); 1395789Sahrens 1396789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 13972082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13982082Seschrock "child dataset with inherited mountpoint is used " 13992082Seschrock "in a non-global zone")); 14002082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1401789Sahrens goto error; 1402789Sahrens } 1403789Sahrens 1404789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1405789Sahrens goto error; 1406789Sahrens 14074849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 14082082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1409789Sahrens } else { 1410789Sahrens 14112169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1412789Sahrens goto error; 1413789Sahrens 1414789Sahrens /* 1415789Sahrens * Refresh the statistics so the new property is reflected. 1416789Sahrens */ 1417789Sahrens (void) get_stats(zhp); 1418789Sahrens } 1419789Sahrens 1420789Sahrens error: 1421789Sahrens changelist_free(cl); 1422789Sahrens return (ret); 1423789Sahrens } 1424789Sahrens 1425789Sahrens /* 14261356Seschrock * True DSL properties are stored in an nvlist. The following two functions 14271356Seschrock * extract them appropriately. 14281356Seschrock */ 14291356Seschrock static uint64_t 14301356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 14311356Seschrock { 14321356Seschrock nvlist_t *nv; 14331356Seschrock uint64_t value; 14341356Seschrock 14352885Sahrens *source = NULL; 14361356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 14371356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 14385094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 14395094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 14401356Seschrock } else { 14418802SSanjeev.Bagewadi@Sun.COM verify(!zhp->zfs_props_table || 14428802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table[prop] == B_TRUE); 14431356Seschrock value = zfs_prop_default_numeric(prop); 14441356Seschrock *source = ""; 14451356Seschrock } 14461356Seschrock 14471356Seschrock return (value); 14481356Seschrock } 14491356Seschrock 14501356Seschrock static char * 14511356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 14521356Seschrock { 14531356Seschrock nvlist_t *nv; 14541356Seschrock char *value; 14551356Seschrock 14562885Sahrens *source = NULL; 14571356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 14581356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 14595094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 14605094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 14611356Seschrock } else { 14628802SSanjeev.Bagewadi@Sun.COM verify(!zhp->zfs_props_table || 14638802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table[prop] == B_TRUE); 14641356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 14651356Seschrock value = ""; 14661356Seschrock *source = ""; 14671356Seschrock } 14681356Seschrock 14691356Seschrock return (value); 14701356Seschrock } 14711356Seschrock 14721356Seschrock /* 1473789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1474789Sahrens * zfs_prop_get_int() are built using this interface. 1475789Sahrens * 1476789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1477789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1478789Sahrens * If they differ from the on-disk values, report the current values and mark 1479789Sahrens * the source "temporary". 1480789Sahrens */ 14812082Seschrock static int 14825094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 14832082Seschrock char **source, uint64_t *val) 1484789Sahrens { 14855147Srm160521 zfs_cmd_t zc = { 0 }; 14865592Stimh nvlist_t *zplprops = NULL; 1487789Sahrens struct mnttab mnt; 14883265Sahrens char *mntopt_on = NULL; 14893265Sahrens char *mntopt_off = NULL; 1490789Sahrens 1491789Sahrens *source = NULL; 1492789Sahrens 14933265Sahrens switch (prop) { 14943265Sahrens case ZFS_PROP_ATIME: 14953265Sahrens mntopt_on = MNTOPT_ATIME; 14963265Sahrens mntopt_off = MNTOPT_NOATIME; 14973265Sahrens break; 14983265Sahrens 14993265Sahrens case ZFS_PROP_DEVICES: 15003265Sahrens mntopt_on = MNTOPT_DEVICES; 15013265Sahrens mntopt_off = MNTOPT_NODEVICES; 15023265Sahrens break; 15033265Sahrens 15043265Sahrens case ZFS_PROP_EXEC: 15053265Sahrens mntopt_on = MNTOPT_EXEC; 15063265Sahrens mntopt_off = MNTOPT_NOEXEC; 15073265Sahrens break; 15083265Sahrens 15093265Sahrens case ZFS_PROP_READONLY: 15103265Sahrens mntopt_on = MNTOPT_RO; 15113265Sahrens mntopt_off = MNTOPT_RW; 15123265Sahrens break; 15133265Sahrens 15143265Sahrens case ZFS_PROP_SETUID: 15153265Sahrens mntopt_on = MNTOPT_SETUID; 15163265Sahrens mntopt_off = MNTOPT_NOSETUID; 15173265Sahrens break; 15183265Sahrens 15193265Sahrens case ZFS_PROP_XATTR: 15203265Sahrens mntopt_on = MNTOPT_XATTR; 15213265Sahrens mntopt_off = MNTOPT_NOXATTR; 15223265Sahrens break; 15235331Samw 15245331Samw case ZFS_PROP_NBMAND: 15255331Samw mntopt_on = MNTOPT_NBMAND; 15265331Samw mntopt_off = MNTOPT_NONBMAND; 15275331Samw break; 15283265Sahrens } 15293265Sahrens 15302474Seschrock /* 15312474Seschrock * Because looking up the mount options is potentially expensive 15322474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 15332474Seschrock * we're looking up a property which requires its presence. 15342474Seschrock */ 15352474Seschrock if (!zhp->zfs_mntcheck && 15363265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 15378228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 15388228SEric.Taylor@Sun.COM struct mnttab entry; 15398228SEric.Taylor@Sun.COM 15408228SEric.Taylor@Sun.COM if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 15418228SEric.Taylor@Sun.COM zhp->zfs_mntopts = zfs_strdup(hdl, 15423265Sahrens entry.mnt_mntopts); 15433265Sahrens if (zhp->zfs_mntopts == NULL) 15443265Sahrens return (-1); 15453265Sahrens } 15462474Seschrock 15472474Seschrock zhp->zfs_mntcheck = B_TRUE; 15482474Seschrock } 15492474Seschrock 1550789Sahrens if (zhp->zfs_mntopts == NULL) 1551789Sahrens mnt.mnt_mntopts = ""; 1552789Sahrens else 1553789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1554789Sahrens 1555789Sahrens switch (prop) { 1556789Sahrens case ZFS_PROP_ATIME: 15573265Sahrens case ZFS_PROP_DEVICES: 15583265Sahrens case ZFS_PROP_EXEC: 15593265Sahrens case ZFS_PROP_READONLY: 15603265Sahrens case ZFS_PROP_SETUID: 15613265Sahrens case ZFS_PROP_XATTR: 15625331Samw case ZFS_PROP_NBMAND: 15632082Seschrock *val = getprop_uint64(zhp, prop, source); 15642082Seschrock 15653265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 15662082Seschrock *val = B_TRUE; 1567789Sahrens if (src) 15685094Slling *src = ZPROP_SRC_TEMPORARY; 15693265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 15702082Seschrock *val = B_FALSE; 1571789Sahrens if (src) 15725094Slling *src = ZPROP_SRC_TEMPORARY; 1573789Sahrens } 15742082Seschrock break; 1575789Sahrens 15763265Sahrens case ZFS_PROP_CANMOUNT: 15772082Seschrock *val = getprop_uint64(zhp, prop, source); 15786168Shs24103 if (*val != ZFS_CANMOUNT_ON) 15793417Srm160521 *source = zhp->zfs_name; 15803417Srm160521 else 15813417Srm160521 *source = ""; /* default */ 15822082Seschrock break; 1583789Sahrens 1584789Sahrens case ZFS_PROP_QUOTA: 15855378Sck153898 case ZFS_PROP_REFQUOTA: 1586789Sahrens case ZFS_PROP_RESERVATION: 15875378Sck153898 case ZFS_PROP_REFRESERVATION: 15882885Sahrens *val = getprop_uint64(zhp, prop, source); 15892885Sahrens if (*val == 0) 1590789Sahrens *source = ""; /* default */ 1591789Sahrens else 1592789Sahrens *source = zhp->zfs_name; 15932082Seschrock break; 1594789Sahrens 1595789Sahrens case ZFS_PROP_MOUNTED: 15962082Seschrock *val = (zhp->zfs_mntopts != NULL); 15972082Seschrock break; 1598789Sahrens 15993377Seschrock case ZFS_PROP_NUMCLONES: 16003377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 16013377Seschrock break; 16023377Seschrock 16035147Srm160521 case ZFS_PROP_VERSION: 16045498Stimh case ZFS_PROP_NORMALIZE: 16055498Stimh case ZFS_PROP_UTF8ONLY: 16065498Stimh case ZFS_PROP_CASE: 16075498Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 16085498Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 16095473Srm160521 return (-1); 16105147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 16115498Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 16125498Stimh zcmd_free_nvlists(&zc); 161310204SMatthew.Ahrens@Sun.COM return (-1); 16145147Srm160521 } 16155498Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 16165498Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 16175498Stimh val) != 0) { 16185498Stimh zcmd_free_nvlists(&zc); 161910204SMatthew.Ahrens@Sun.COM return (-1); 16205498Stimh } 16215592Stimh if (zplprops) 16225592Stimh nvlist_free(zplprops); 16235498Stimh zcmd_free_nvlists(&zc); 16245147Srm160521 break; 16255147Srm160521 1626789Sahrens default: 16274577Sahrens switch (zfs_prop_get_type(prop)) { 16284787Sahrens case PROP_TYPE_NUMBER: 16294787Sahrens case PROP_TYPE_INDEX: 16304577Sahrens *val = getprop_uint64(zhp, prop, source); 16317390SMatthew.Ahrens@Sun.COM /* 16329396SMatthew.Ahrens@Sun.COM * If we tried to use a default value for a 16337390SMatthew.Ahrens@Sun.COM * readonly property, it means that it was not 16347390SMatthew.Ahrens@Sun.COM * present; return an error. 16357390SMatthew.Ahrens@Sun.COM */ 16367390SMatthew.Ahrens@Sun.COM if (zfs_prop_readonly(prop) && 16377390SMatthew.Ahrens@Sun.COM *source && (*source)[0] == '\0') { 16387390SMatthew.Ahrens@Sun.COM return (-1); 16397390SMatthew.Ahrens@Sun.COM } 16404577Sahrens break; 16414577Sahrens 16424787Sahrens case PROP_TYPE_STRING: 16434577Sahrens default: 16444577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 16454577Sahrens "cannot get non-numeric property")); 16464577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 16474577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 16484577Sahrens } 1649789Sahrens } 1650789Sahrens 1651789Sahrens return (0); 1652789Sahrens } 1653789Sahrens 1654789Sahrens /* 1655789Sahrens * Calculate the source type, given the raw source string. 1656789Sahrens */ 1657789Sahrens static void 16585094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1659789Sahrens char *statbuf, size_t statlen) 1660789Sahrens { 16615094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1662789Sahrens return; 1663789Sahrens 1664789Sahrens if (source == NULL) { 16655094Slling *srctype = ZPROP_SRC_NONE; 1666789Sahrens } else if (source[0] == '\0') { 16675094Slling *srctype = ZPROP_SRC_DEFAULT; 1668789Sahrens } else { 1669789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 16705094Slling *srctype = ZPROP_SRC_LOCAL; 1671789Sahrens } else { 1672789Sahrens (void) strlcpy(statbuf, source, statlen); 16735094Slling *srctype = ZPROP_SRC_INHERITED; 1674789Sahrens } 1675789Sahrens } 1676789Sahrens 1677789Sahrens } 1678789Sahrens 1679789Sahrens /* 1680789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 1681789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 1682789Sahrens * human-readable form. 1683789Sahrens * 1684789Sahrens * Returns 0 on success, or -1 on error. 1685789Sahrens */ 1686789Sahrens int 1687789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 16885094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 1689789Sahrens { 1690789Sahrens char *source = NULL; 1691789Sahrens uint64_t val; 1692789Sahrens char *str; 16932676Seschrock const char *strval; 1694789Sahrens 1695789Sahrens /* 1696789Sahrens * Check to see if this property applies to our object 1697789Sahrens */ 1698789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1699789Sahrens return (-1); 1700789Sahrens 1701789Sahrens if (src) 17025094Slling *src = ZPROP_SRC_NONE; 1703789Sahrens 1704789Sahrens switch (prop) { 1705789Sahrens case ZFS_PROP_CREATION: 1706789Sahrens /* 1707789Sahrens * 'creation' is a time_t stored in the statistics. We convert 1708789Sahrens * this into a string unless 'literal' is specified. 1709789Sahrens */ 1710789Sahrens { 17112885Sahrens val = getprop_uint64(zhp, prop, &source); 17122885Sahrens time_t time = (time_t)val; 1713789Sahrens struct tm t; 1714789Sahrens 1715789Sahrens if (literal || 1716789Sahrens localtime_r(&time, &t) == NULL || 1717789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 1718789Sahrens &t) == 0) 17192885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 1720789Sahrens } 1721789Sahrens break; 1722789Sahrens 1723789Sahrens case ZFS_PROP_MOUNTPOINT: 1724789Sahrens /* 1725789Sahrens * Getting the precise mountpoint can be tricky. 1726789Sahrens * 1727789Sahrens * - for 'none' or 'legacy', return those values. 1728789Sahrens * - for inherited mountpoints, we want to take everything 1729789Sahrens * after our ancestor and append it to the inherited value. 1730789Sahrens * 1731789Sahrens * If the pool has an alternate root, we want to prepend that 1732789Sahrens * root to any values we return. 1733789Sahrens */ 17346865Srm160521 17351356Seschrock str = getprop_string(zhp, prop, &source); 17361356Seschrock 17376612Sgw25295 if (str[0] == '/') { 17386865Srm160521 char buf[MAXPATHLEN]; 17396865Srm160521 char *root = buf; 17401356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 1741789Sahrens 1742789Sahrens if (relpath[0] == '/') 1743789Sahrens relpath++; 17446612Sgw25295 17456865Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 17466865Srm160521 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) || 17476865Srm160521 (strcmp(root, "-") == 0)) 17486865Srm160521 root[0] = '\0'; 17496612Sgw25295 /* 17506612Sgw25295 * Special case an alternate root of '/'. This will 17516612Sgw25295 * avoid having multiple leading slashes in the 17526612Sgw25295 * mountpoint path. 17536612Sgw25295 */ 17546612Sgw25295 if (strcmp(root, "/") == 0) 17556612Sgw25295 root++; 17566612Sgw25295 17576612Sgw25295 /* 17586612Sgw25295 * If the mountpoint is '/' then skip over this 17596612Sgw25295 * if we are obtaining either an alternate root or 17606612Sgw25295 * an inherited mountpoint. 17616612Sgw25295 */ 17626612Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 17636612Sgw25295 relpath[0] != '\0')) 17641356Seschrock str++; 1765789Sahrens 1766789Sahrens if (relpath[0] == '\0') 1767789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 17681356Seschrock root, str); 1769789Sahrens else 1770789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 17711356Seschrock root, str, relpath[0] == '@' ? "" : "/", 1772789Sahrens relpath); 1773789Sahrens } else { 1774789Sahrens /* 'legacy' or 'none' */ 17751356Seschrock (void) strlcpy(propbuf, str, proplen); 1776789Sahrens } 1777789Sahrens 1778789Sahrens break; 1779789Sahrens 1780789Sahrens case ZFS_PROP_ORIGIN: 17812885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 1782789Sahrens proplen); 1783789Sahrens /* 1784789Sahrens * If there is no parent at all, return failure to indicate that 1785789Sahrens * it doesn't apply to this dataset. 1786789Sahrens */ 1787789Sahrens if (propbuf[0] == '\0') 1788789Sahrens return (-1); 1789789Sahrens break; 1790789Sahrens 1791789Sahrens case ZFS_PROP_QUOTA: 17925378Sck153898 case ZFS_PROP_REFQUOTA: 1793789Sahrens case ZFS_PROP_RESERVATION: 17945378Sck153898 case ZFS_PROP_REFRESERVATION: 17955378Sck153898 17962082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 17972082Seschrock return (-1); 1798789Sahrens 1799789Sahrens /* 1800789Sahrens * If quota or reservation is 0, we translate this into 'none' 1801789Sahrens * (unless literal is set), and indicate that it's the default 1802789Sahrens * value. Otherwise, we print the number nicely and indicate 1803789Sahrens * that its set locally. 1804789Sahrens */ 1805789Sahrens if (val == 0) { 1806789Sahrens if (literal) 1807789Sahrens (void) strlcpy(propbuf, "0", proplen); 1808789Sahrens else 1809789Sahrens (void) strlcpy(propbuf, "none", proplen); 1810789Sahrens } else { 1811789Sahrens if (literal) 18122856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 18133912Slling (u_longlong_t)val); 1814789Sahrens else 1815789Sahrens zfs_nicenum(val, propbuf, proplen); 1816789Sahrens } 1817789Sahrens break; 1818789Sahrens 1819789Sahrens case ZFS_PROP_COMPRESSRATIO: 18202082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 18212082Seschrock return (-1); 182210922SJeff.Bonwick@Sun.COM (void) snprintf(propbuf, proplen, "%llu.%02llux", 182310922SJeff.Bonwick@Sun.COM (u_longlong_t)(val / 100), 182410922SJeff.Bonwick@Sun.COM (u_longlong_t)(val % 100)); 1825789Sahrens break; 1826789Sahrens 1827789Sahrens case ZFS_PROP_TYPE: 1828789Sahrens switch (zhp->zfs_type) { 1829789Sahrens case ZFS_TYPE_FILESYSTEM: 1830789Sahrens str = "filesystem"; 1831789Sahrens break; 1832789Sahrens case ZFS_TYPE_VOLUME: 1833789Sahrens str = "volume"; 1834789Sahrens break; 1835789Sahrens case ZFS_TYPE_SNAPSHOT: 1836789Sahrens str = "snapshot"; 1837789Sahrens break; 1838789Sahrens default: 18392082Seschrock abort(); 1840789Sahrens } 1841789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 1842789Sahrens break; 1843789Sahrens 1844789Sahrens case ZFS_PROP_MOUNTED: 1845789Sahrens /* 1846789Sahrens * The 'mounted' property is a pseudo-property that described 1847789Sahrens * whether the filesystem is currently mounted. Even though 1848789Sahrens * it's a boolean value, the typical values of "on" and "off" 1849789Sahrens * don't make sense, so we translate to "yes" and "no". 1850789Sahrens */ 18512082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 18522082Seschrock src, &source, &val) != 0) 18532082Seschrock return (-1); 18542082Seschrock if (val) 1855789Sahrens (void) strlcpy(propbuf, "yes", proplen); 1856789Sahrens else 1857789Sahrens (void) strlcpy(propbuf, "no", proplen); 1858789Sahrens break; 1859789Sahrens 1860789Sahrens case ZFS_PROP_NAME: 1861789Sahrens /* 1862789Sahrens * The 'name' property is a pseudo-property derived from the 1863789Sahrens * dataset name. It is presented as a real property to simplify 1864789Sahrens * consumers. 1865789Sahrens */ 1866789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 1867789Sahrens break; 1868789Sahrens 1869789Sahrens default: 18704577Sahrens switch (zfs_prop_get_type(prop)) { 18714787Sahrens case PROP_TYPE_NUMBER: 18724577Sahrens if (get_numeric_property(zhp, prop, src, 18734577Sahrens &source, &val) != 0) 18744577Sahrens return (-1); 18754577Sahrens if (literal) 18764577Sahrens (void) snprintf(propbuf, proplen, "%llu", 18774577Sahrens (u_longlong_t)val); 18784577Sahrens else 18794577Sahrens zfs_nicenum(val, propbuf, proplen); 18804577Sahrens break; 18814577Sahrens 18824787Sahrens case PROP_TYPE_STRING: 18834577Sahrens (void) strlcpy(propbuf, 18844577Sahrens getprop_string(zhp, prop, &source), proplen); 18854577Sahrens break; 18864577Sahrens 18874787Sahrens case PROP_TYPE_INDEX: 18884861Sahrens if (get_numeric_property(zhp, prop, src, 18894861Sahrens &source, &val) != 0) 18904861Sahrens return (-1); 18914861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 18924577Sahrens return (-1); 18934577Sahrens (void) strlcpy(propbuf, strval, proplen); 18944577Sahrens break; 18954577Sahrens 18964577Sahrens default: 18974577Sahrens abort(); 18984577Sahrens } 1899789Sahrens } 1900789Sahrens 1901789Sahrens get_source(zhp, src, source, statbuf, statlen); 1902789Sahrens 1903789Sahrens return (0); 1904789Sahrens } 1905789Sahrens 1906789Sahrens /* 1907789Sahrens * Utility function to get the given numeric property. Does no validation that 1908789Sahrens * the given property is the appropriate type; should only be used with 1909789Sahrens * hard-coded property types. 1910789Sahrens */ 1911789Sahrens uint64_t 1912789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 1913789Sahrens { 1914789Sahrens char *source; 19152082Seschrock uint64_t val; 19162082Seschrock 19175367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 19182082Seschrock 19192082Seschrock return (val); 1920789Sahrens } 1921789Sahrens 19225713Srm160521 int 19235713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 19245713Srm160521 { 19255713Srm160521 char buf[64]; 19265713Srm160521 19279396SMatthew.Ahrens@Sun.COM (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 19285713Srm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 19295713Srm160521 } 19305713Srm160521 1931789Sahrens /* 1932789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 1933789Sahrens */ 1934789Sahrens int 1935789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 19365094Slling zprop_source_t *src, char *statbuf, size_t statlen) 1937789Sahrens { 1938789Sahrens char *source; 1939789Sahrens 1940789Sahrens /* 1941789Sahrens * Check to see if this property applies to our object 1942789Sahrens */ 19434849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 19443237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 19452082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 19462082Seschrock zfs_prop_to_name(prop))); 19474849Sahrens } 1948789Sahrens 1949789Sahrens if (src) 19505094Slling *src = ZPROP_SRC_NONE; 1951789Sahrens 19522082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 19532082Seschrock return (-1); 1954789Sahrens 1955789Sahrens get_source(zhp, src, source, statbuf, statlen); 1956789Sahrens 1957789Sahrens return (0); 1958789Sahrens } 1959789Sahrens 19609396SMatthew.Ahrens@Sun.COM static int 19619396SMatthew.Ahrens@Sun.COM idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 19629396SMatthew.Ahrens@Sun.COM char **domainp, idmap_rid_t *ridp) 19639396SMatthew.Ahrens@Sun.COM { 19649396SMatthew.Ahrens@Sun.COM idmap_handle_t *idmap_hdl = NULL; 19659396SMatthew.Ahrens@Sun.COM idmap_get_handle_t *get_hdl = NULL; 19669396SMatthew.Ahrens@Sun.COM idmap_stat status; 19679396SMatthew.Ahrens@Sun.COM int err = EINVAL; 19689396SMatthew.Ahrens@Sun.COM 19699396SMatthew.Ahrens@Sun.COM if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS) 19709396SMatthew.Ahrens@Sun.COM goto out; 19719396SMatthew.Ahrens@Sun.COM if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS) 19729396SMatthew.Ahrens@Sun.COM goto out; 19739396SMatthew.Ahrens@Sun.COM 19749396SMatthew.Ahrens@Sun.COM if (isuser) { 19759396SMatthew.Ahrens@Sun.COM err = idmap_get_sidbyuid(get_hdl, id, 19769396SMatthew.Ahrens@Sun.COM IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 19779396SMatthew.Ahrens@Sun.COM } else { 19789396SMatthew.Ahrens@Sun.COM err = idmap_get_sidbygid(get_hdl, id, 19799396SMatthew.Ahrens@Sun.COM IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 19809396SMatthew.Ahrens@Sun.COM } 19819396SMatthew.Ahrens@Sun.COM if (err == IDMAP_SUCCESS && 19829396SMatthew.Ahrens@Sun.COM idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 19839396SMatthew.Ahrens@Sun.COM status == IDMAP_SUCCESS) 19849396SMatthew.Ahrens@Sun.COM err = 0; 19859396SMatthew.Ahrens@Sun.COM else 19869396SMatthew.Ahrens@Sun.COM err = EINVAL; 19879396SMatthew.Ahrens@Sun.COM out: 19889396SMatthew.Ahrens@Sun.COM if (get_hdl) 19899396SMatthew.Ahrens@Sun.COM idmap_get_destroy(get_hdl); 19909396SMatthew.Ahrens@Sun.COM if (idmap_hdl) 19919396SMatthew.Ahrens@Sun.COM (void) idmap_fini(idmap_hdl); 19929396SMatthew.Ahrens@Sun.COM return (err); 19939396SMatthew.Ahrens@Sun.COM } 19949396SMatthew.Ahrens@Sun.COM 19959396SMatthew.Ahrens@Sun.COM /* 19969396SMatthew.Ahrens@Sun.COM * convert the propname into parameters needed by kernel 19979396SMatthew.Ahrens@Sun.COM * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 19989396SMatthew.Ahrens@Sun.COM * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 19999396SMatthew.Ahrens@Sun.COM */ 20009396SMatthew.Ahrens@Sun.COM static int 20019396SMatthew.Ahrens@Sun.COM userquota_propname_decode(const char *propname, boolean_t zoned, 20029396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 20039396SMatthew.Ahrens@Sun.COM { 20049396SMatthew.Ahrens@Sun.COM zfs_userquota_prop_t type; 20059396SMatthew.Ahrens@Sun.COM char *cp, *end; 200610160SMatthew.Ahrens@Sun.COM char *numericsid = NULL; 20079396SMatthew.Ahrens@Sun.COM boolean_t isuser; 20089396SMatthew.Ahrens@Sun.COM 20099396SMatthew.Ahrens@Sun.COM domain[0] = '\0'; 20109396SMatthew.Ahrens@Sun.COM 20119396SMatthew.Ahrens@Sun.COM /* Figure out the property type ({user|group}{quota|space}) */ 20129396SMatthew.Ahrens@Sun.COM for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 20139396SMatthew.Ahrens@Sun.COM if (strncmp(propname, zfs_userquota_prop_prefixes[type], 20149396SMatthew.Ahrens@Sun.COM strlen(zfs_userquota_prop_prefixes[type])) == 0) 20159396SMatthew.Ahrens@Sun.COM break; 20169396SMatthew.Ahrens@Sun.COM } 20179396SMatthew.Ahrens@Sun.COM if (type == ZFS_NUM_USERQUOTA_PROPS) 20189396SMatthew.Ahrens@Sun.COM return (EINVAL); 20199396SMatthew.Ahrens@Sun.COM *typep = type; 20209396SMatthew.Ahrens@Sun.COM 20219396SMatthew.Ahrens@Sun.COM isuser = (type == ZFS_PROP_USERQUOTA || 20229396SMatthew.Ahrens@Sun.COM type == ZFS_PROP_USERUSED); 20239396SMatthew.Ahrens@Sun.COM 20249396SMatthew.Ahrens@Sun.COM cp = strchr(propname, '@') + 1; 20259396SMatthew.Ahrens@Sun.COM 20269396SMatthew.Ahrens@Sun.COM if (strchr(cp, '@')) { 20279396SMatthew.Ahrens@Sun.COM /* 20289396SMatthew.Ahrens@Sun.COM * It's a SID name (eg "user@domain") that needs to be 202910160SMatthew.Ahrens@Sun.COM * turned into S-1-domainID-RID. 20309396SMatthew.Ahrens@Sun.COM */ 203110160SMatthew.Ahrens@Sun.COM directory_error_t e; 20329396SMatthew.Ahrens@Sun.COM if (zoned && getzoneid() == GLOBAL_ZONEID) 20339396SMatthew.Ahrens@Sun.COM return (ENOENT); 203410160SMatthew.Ahrens@Sun.COM if (isuser) { 203510160SMatthew.Ahrens@Sun.COM e = directory_sid_from_user_name(NULL, 203610160SMatthew.Ahrens@Sun.COM cp, &numericsid); 203710160SMatthew.Ahrens@Sun.COM } else { 203810160SMatthew.Ahrens@Sun.COM e = directory_sid_from_group_name(NULL, 203910160SMatthew.Ahrens@Sun.COM cp, &numericsid); 204010160SMatthew.Ahrens@Sun.COM } 204110160SMatthew.Ahrens@Sun.COM if (e != NULL) { 204210160SMatthew.Ahrens@Sun.COM directory_error_free(e); 20439396SMatthew.Ahrens@Sun.COM return (ENOENT); 204410160SMatthew.Ahrens@Sun.COM } 204510160SMatthew.Ahrens@Sun.COM if (numericsid == NULL) 204610160SMatthew.Ahrens@Sun.COM return (ENOENT); 204710160SMatthew.Ahrens@Sun.COM cp = numericsid; 204810160SMatthew.Ahrens@Sun.COM /* will be further decoded below */ 204910160SMatthew.Ahrens@Sun.COM } 205010160SMatthew.Ahrens@Sun.COM 205110160SMatthew.Ahrens@Sun.COM if (strncmp(cp, "S-1-", 4) == 0) { 20529396SMatthew.Ahrens@Sun.COM /* It's a numeric SID (eg "S-1-234-567-89") */ 205310160SMatthew.Ahrens@Sun.COM (void) strlcpy(domain, cp, domainlen); 20549396SMatthew.Ahrens@Sun.COM cp = strrchr(domain, '-'); 20559396SMatthew.Ahrens@Sun.COM *cp = '\0'; 20569396SMatthew.Ahrens@Sun.COM cp++; 20579396SMatthew.Ahrens@Sun.COM 20589396SMatthew.Ahrens@Sun.COM errno = 0; 20599396SMatthew.Ahrens@Sun.COM *ridp = strtoull(cp, &end, 10); 206010160SMatthew.Ahrens@Sun.COM if (numericsid) { 206110160SMatthew.Ahrens@Sun.COM free(numericsid); 206210160SMatthew.Ahrens@Sun.COM numericsid = NULL; 206310160SMatthew.Ahrens@Sun.COM } 20649688SMatthew.Ahrens@Sun.COM if (errno != 0 || *end != '\0') 20659396SMatthew.Ahrens@Sun.COM return (EINVAL); 20669396SMatthew.Ahrens@Sun.COM } else if (!isdigit(*cp)) { 20679396SMatthew.Ahrens@Sun.COM /* 20689396SMatthew.Ahrens@Sun.COM * It's a user/group name (eg "user") that needs to be 20699396SMatthew.Ahrens@Sun.COM * turned into a uid/gid 20709396SMatthew.Ahrens@Sun.COM */ 20719396SMatthew.Ahrens@Sun.COM if (zoned && getzoneid() == GLOBAL_ZONEID) 20729396SMatthew.Ahrens@Sun.COM return (ENOENT); 20739396SMatthew.Ahrens@Sun.COM if (isuser) { 20749396SMatthew.Ahrens@Sun.COM struct passwd *pw; 20759396SMatthew.Ahrens@Sun.COM pw = getpwnam(cp); 20769396SMatthew.Ahrens@Sun.COM if (pw == NULL) 20779396SMatthew.Ahrens@Sun.COM return (ENOENT); 20789396SMatthew.Ahrens@Sun.COM *ridp = pw->pw_uid; 20799396SMatthew.Ahrens@Sun.COM } else { 20809396SMatthew.Ahrens@Sun.COM struct group *gr; 20819396SMatthew.Ahrens@Sun.COM gr = getgrnam(cp); 20829396SMatthew.Ahrens@Sun.COM if (gr == NULL) 20839396SMatthew.Ahrens@Sun.COM return (ENOENT); 20849396SMatthew.Ahrens@Sun.COM *ridp = gr->gr_gid; 20859396SMatthew.Ahrens@Sun.COM } 20869396SMatthew.Ahrens@Sun.COM } else { 20879396SMatthew.Ahrens@Sun.COM /* It's a user/group ID (eg "12345"). */ 20889396SMatthew.Ahrens@Sun.COM uid_t id = strtoul(cp, &end, 10); 20899396SMatthew.Ahrens@Sun.COM idmap_rid_t rid; 20909396SMatthew.Ahrens@Sun.COM char *mapdomain; 20919396SMatthew.Ahrens@Sun.COM 20929396SMatthew.Ahrens@Sun.COM if (*end != '\0') 20939396SMatthew.Ahrens@Sun.COM return (EINVAL); 20949396SMatthew.Ahrens@Sun.COM if (id > MAXUID) { 20959396SMatthew.Ahrens@Sun.COM /* It's an ephemeral ID. */ 20969396SMatthew.Ahrens@Sun.COM if (idmap_id_to_numeric_domain_rid(id, isuser, 20979396SMatthew.Ahrens@Sun.COM &mapdomain, &rid) != 0) 20989396SMatthew.Ahrens@Sun.COM return (ENOENT); 209910160SMatthew.Ahrens@Sun.COM (void) strlcpy(domain, mapdomain, domainlen); 21009396SMatthew.Ahrens@Sun.COM *ridp = rid; 21019396SMatthew.Ahrens@Sun.COM } else { 21029396SMatthew.Ahrens@Sun.COM *ridp = id; 21039396SMatthew.Ahrens@Sun.COM } 21049396SMatthew.Ahrens@Sun.COM } 21059396SMatthew.Ahrens@Sun.COM 210610160SMatthew.Ahrens@Sun.COM ASSERT3P(numericsid, ==, NULL); 21079396SMatthew.Ahrens@Sun.COM return (0); 21089396SMatthew.Ahrens@Sun.COM } 21099396SMatthew.Ahrens@Sun.COM 21109469SLin.Ling@Sun.COM static int 21119469SLin.Ling@Sun.COM zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 21129469SLin.Ling@Sun.COM uint64_t *propvalue, zfs_userquota_prop_t *typep) 21139396SMatthew.Ahrens@Sun.COM { 21149396SMatthew.Ahrens@Sun.COM int err; 21159396SMatthew.Ahrens@Sun.COM zfs_cmd_t zc = { 0 }; 21169396SMatthew.Ahrens@Sun.COM 21179396SMatthew.Ahrens@Sun.COM (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21189396SMatthew.Ahrens@Sun.COM 21199396SMatthew.Ahrens@Sun.COM err = userquota_propname_decode(propname, 21209396SMatthew.Ahrens@Sun.COM zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 21219469SLin.Ling@Sun.COM typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 21229469SLin.Ling@Sun.COM zc.zc_objset_type = *typep; 21239396SMatthew.Ahrens@Sun.COM if (err) 21249396SMatthew.Ahrens@Sun.COM return (err); 21259396SMatthew.Ahrens@Sun.COM 21269396SMatthew.Ahrens@Sun.COM err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 21279396SMatthew.Ahrens@Sun.COM if (err) 21289396SMatthew.Ahrens@Sun.COM return (err); 21299396SMatthew.Ahrens@Sun.COM 21309469SLin.Ling@Sun.COM *propvalue = zc.zc_cookie; 21319469SLin.Ling@Sun.COM return (0); 21329469SLin.Ling@Sun.COM } 21339469SLin.Ling@Sun.COM 21349469SLin.Ling@Sun.COM int 21359469SLin.Ling@Sun.COM zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 21369469SLin.Ling@Sun.COM uint64_t *propvalue) 21379469SLin.Ling@Sun.COM { 21389469SLin.Ling@Sun.COM zfs_userquota_prop_t type; 21399469SLin.Ling@Sun.COM 21409469SLin.Ling@Sun.COM return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 21419469SLin.Ling@Sun.COM &type)); 21429469SLin.Ling@Sun.COM } 21439469SLin.Ling@Sun.COM 21449469SLin.Ling@Sun.COM int 21459469SLin.Ling@Sun.COM zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 21469469SLin.Ling@Sun.COM char *propbuf, int proplen, boolean_t literal) 21479469SLin.Ling@Sun.COM { 21489469SLin.Ling@Sun.COM int err; 21499469SLin.Ling@Sun.COM uint64_t propvalue; 21509469SLin.Ling@Sun.COM zfs_userquota_prop_t type; 21519469SLin.Ling@Sun.COM 21529469SLin.Ling@Sun.COM err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 21539469SLin.Ling@Sun.COM &type); 21549469SLin.Ling@Sun.COM 21559469SLin.Ling@Sun.COM if (err) 21569469SLin.Ling@Sun.COM return (err); 21579469SLin.Ling@Sun.COM 21589396SMatthew.Ahrens@Sun.COM if (literal) { 21599469SLin.Ling@Sun.COM (void) snprintf(propbuf, proplen, "%llu", propvalue); 21609469SLin.Ling@Sun.COM } else if (propvalue == 0 && 21619396SMatthew.Ahrens@Sun.COM (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) { 21629396SMatthew.Ahrens@Sun.COM (void) strlcpy(propbuf, "none", proplen); 21639396SMatthew.Ahrens@Sun.COM } else { 21649469SLin.Ling@Sun.COM zfs_nicenum(propvalue, propbuf, proplen); 21659396SMatthew.Ahrens@Sun.COM } 21669396SMatthew.Ahrens@Sun.COM return (0); 21679396SMatthew.Ahrens@Sun.COM } 21689396SMatthew.Ahrens@Sun.COM 2169789Sahrens /* 2170789Sahrens * Returns the name of the given zfs handle. 2171789Sahrens */ 2172789Sahrens const char * 2173789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2174789Sahrens { 2175789Sahrens return (zhp->zfs_name); 2176789Sahrens } 2177789Sahrens 2178789Sahrens /* 2179789Sahrens * Returns the type of the given zfs handle. 2180789Sahrens */ 2181789Sahrens zfs_type_t 2182789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2183789Sahrens { 2184789Sahrens return (zhp->zfs_type); 2185789Sahrens } 2186789Sahrens 21878228SEric.Taylor@Sun.COM static int 21888228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc) 21898228SEric.Taylor@Sun.COM { 21908228SEric.Taylor@Sun.COM int rc; 21918228SEric.Taylor@Sun.COM uint64_t orig_cookie; 21928228SEric.Taylor@Sun.COM 21938228SEric.Taylor@Sun.COM orig_cookie = zc->zc_cookie; 21948228SEric.Taylor@Sun.COM top: 21958228SEric.Taylor@Sun.COM (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 21968228SEric.Taylor@Sun.COM rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc); 21978228SEric.Taylor@Sun.COM 21988228SEric.Taylor@Sun.COM if (rc == -1) { 21998228SEric.Taylor@Sun.COM switch (errno) { 22008228SEric.Taylor@Sun.COM case ENOMEM: 22018228SEric.Taylor@Sun.COM /* expand nvlist memory and try again */ 22028228SEric.Taylor@Sun.COM if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) { 22038228SEric.Taylor@Sun.COM zcmd_free_nvlists(zc); 22048228SEric.Taylor@Sun.COM return (-1); 22058228SEric.Taylor@Sun.COM } 22068228SEric.Taylor@Sun.COM zc->zc_cookie = orig_cookie; 22078228SEric.Taylor@Sun.COM goto top; 22088228SEric.Taylor@Sun.COM /* 22098228SEric.Taylor@Sun.COM * An errno value of ESRCH indicates normal completion. 22108228SEric.Taylor@Sun.COM * If ENOENT is returned, then the underlying dataset 22118228SEric.Taylor@Sun.COM * has been removed since we obtained the handle. 22128228SEric.Taylor@Sun.COM */ 22138228SEric.Taylor@Sun.COM case ESRCH: 22148228SEric.Taylor@Sun.COM case ENOENT: 22158228SEric.Taylor@Sun.COM rc = 1; 22168228SEric.Taylor@Sun.COM break; 22178228SEric.Taylor@Sun.COM default: 22188228SEric.Taylor@Sun.COM rc = zfs_standard_error(zhp->zfs_hdl, errno, 22198228SEric.Taylor@Sun.COM dgettext(TEXT_DOMAIN, 22208228SEric.Taylor@Sun.COM "cannot iterate filesystems")); 22218228SEric.Taylor@Sun.COM break; 22228228SEric.Taylor@Sun.COM } 22238228SEric.Taylor@Sun.COM } 22248228SEric.Taylor@Sun.COM return (rc); 22258228SEric.Taylor@Sun.COM } 22268228SEric.Taylor@Sun.COM 2227789Sahrens /* 22281356Seschrock * Iterate over all child filesystems 2229789Sahrens */ 2230789Sahrens int 22311356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2232789Sahrens { 2233789Sahrens zfs_cmd_t zc = { 0 }; 2234789Sahrens zfs_handle_t *nzhp; 2235789Sahrens int ret; 2236789Sahrens 22375367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 22385367Sahrens return (0); 22395367Sahrens 22408228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 22418228SEric.Taylor@Sun.COM return (-1); 22428228SEric.Taylor@Sun.COM 22438228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT, 22448228SEric.Taylor@Sun.COM &zc)) == 0) { 2245789Sahrens /* 2246789Sahrens * Silently ignore errors, as the only plausible explanation is 2247789Sahrens * that the pool has since been removed. 2248789Sahrens */ 22498228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 22508228SEric.Taylor@Sun.COM &zc)) == NULL) { 2251789Sahrens continue; 22528228SEric.Taylor@Sun.COM } 22538228SEric.Taylor@Sun.COM 22548228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 22558228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2256789Sahrens return (ret); 22578228SEric.Taylor@Sun.COM } 2258789Sahrens } 22598228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 22608228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 22611356Seschrock } 22621356Seschrock 22631356Seschrock /* 22641356Seschrock * Iterate over all snapshots 22651356Seschrock */ 22661356Seschrock int 22671356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 22681356Seschrock { 22691356Seschrock zfs_cmd_t zc = { 0 }; 22701356Seschrock zfs_handle_t *nzhp; 22711356Seschrock int ret; 2272789Sahrens 22735367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 22745367Sahrens return (0); 22755367Sahrens 22768228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 22778228SEric.Taylor@Sun.COM return (-1); 22788228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, 22798228SEric.Taylor@Sun.COM &zc)) == 0) { 22808228SEric.Taylor@Sun.COM 22818228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 22828228SEric.Taylor@Sun.COM &zc)) == NULL) { 2283789Sahrens continue; 22848228SEric.Taylor@Sun.COM } 22858228SEric.Taylor@Sun.COM 22868228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 22878228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2288789Sahrens return (ret); 22898228SEric.Taylor@Sun.COM } 2290789Sahrens } 22918228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 22928228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 2293789Sahrens } 2294789Sahrens 2295789Sahrens /* 22961356Seschrock * Iterate over all children, snapshots and filesystems 22971356Seschrock */ 22981356Seschrock int 22991356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23001356Seschrock { 23011356Seschrock int ret; 23021356Seschrock 23031356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 23041356Seschrock return (ret); 23051356Seschrock 23061356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 23071356Seschrock } 23081356Seschrock 23091356Seschrock /* 2310789Sahrens * Given a complete name, return just the portion that refers to the parent. 2311789Sahrens * Can return NULL if this is a pool. 2312789Sahrens */ 2313789Sahrens static int 2314789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2315789Sahrens { 2316789Sahrens char *loc; 2317789Sahrens 2318789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2319789Sahrens return (-1); 2320789Sahrens 2321789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2322789Sahrens buf[loc - path] = '\0'; 2323789Sahrens 2324789Sahrens return (0); 2325789Sahrens } 2326789Sahrens 2327789Sahrens /* 23284490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 23294490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 23304490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 23314490Svb160487 * length of already existing prefix of the given path. We also fetch the 23324490Svb160487 * 'zoned' property, which is used to validate property settings when creating 23334490Svb160487 * new datasets. 2334789Sahrens */ 2335789Sahrens static int 23364490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 23374490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2338789Sahrens { 2339789Sahrens zfs_cmd_t zc = { 0 }; 2340789Sahrens char parent[ZFS_MAXNAMELEN]; 2341789Sahrens char *slash; 23421356Seschrock zfs_handle_t *zhp; 23432082Seschrock char errbuf[1024]; 23442082Seschrock 23458269SMark.Musante@Sun.COM (void) snprintf(errbuf, sizeof (errbuf), 23468269SMark.Musante@Sun.COM dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 2347789Sahrens 2348789Sahrens /* get parent, and check to see if this is just a pool */ 2349789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 23502082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23512082Seschrock "missing dataset name")); 23522082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2353789Sahrens } 2354789Sahrens 2355789Sahrens /* check to see if the pool exists */ 2356789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2357789Sahrens slash = parent + strlen(parent); 2358789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2359789Sahrens zc.zc_name[slash - parent] = '\0'; 23602082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2361789Sahrens errno == ENOENT) { 23622082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23632082Seschrock "no such pool '%s'"), zc.zc_name); 23642082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2365789Sahrens } 2366789Sahrens 2367789Sahrens /* check to see if the parent dataset exists */ 23684490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 23694490Svb160487 if (errno == ENOENT && accept_ancestor) { 23704490Svb160487 /* 23714490Svb160487 * Go deeper to find an ancestor, give up on top level. 23724490Svb160487 */ 23734490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 23744490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23754490Svb160487 "no such pool '%s'"), zc.zc_name); 23764490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 23774490Svb160487 } 23784490Svb160487 } else if (errno == ENOENT) { 23792082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23802082Seschrock "parent does not exist")); 23812082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 23824490Svb160487 } else 23832082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2384789Sahrens } 2385789Sahrens 23862676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2387789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 23882676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 23892082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 23901356Seschrock zfs_close(zhp); 2391789Sahrens return (-1); 2392789Sahrens } 2393789Sahrens 2394789Sahrens /* make sure parent is a filesystem */ 23951356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 23962082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23972082Seschrock "parent is not a filesystem")); 23982082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 23991356Seschrock zfs_close(zhp); 2400789Sahrens return (-1); 2401789Sahrens } 2402789Sahrens 24031356Seschrock zfs_close(zhp); 24044490Svb160487 if (prefixlen != NULL) 24054490Svb160487 *prefixlen = strlen(parent); 24064490Svb160487 return (0); 24074490Svb160487 } 24084490Svb160487 24094490Svb160487 /* 24104490Svb160487 * Finds whether the dataset of the given type(s) exists. 24114490Svb160487 */ 24124490Svb160487 boolean_t 24134490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 24144490Svb160487 { 24154490Svb160487 zfs_handle_t *zhp; 24164490Svb160487 24175326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 24184490Svb160487 return (B_FALSE); 24194490Svb160487 24204490Svb160487 /* 24214490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 24224490Svb160487 */ 24234490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 24244490Svb160487 int ds_type = zhp->zfs_type; 24254490Svb160487 24264490Svb160487 zfs_close(zhp); 24274490Svb160487 if (types & ds_type) 24284490Svb160487 return (B_TRUE); 24294490Svb160487 } 24304490Svb160487 return (B_FALSE); 24314490Svb160487 } 24324490Svb160487 24334490Svb160487 /* 24345367Sahrens * Given a path to 'target', create all the ancestors between 24355367Sahrens * the prefixlen portion of the path, and the target itself. 24365367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 24375367Sahrens */ 24385367Sahrens int 24395367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 24405367Sahrens { 24415367Sahrens zfs_handle_t *h; 24425367Sahrens char *cp; 24435367Sahrens const char *opname; 24445367Sahrens 24455367Sahrens /* make sure prefix exists */ 24465367Sahrens cp = target + prefixlen; 24475367Sahrens if (*cp != '/') { 24485367Sahrens assert(strchr(cp, '/') == NULL); 24495367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 24505367Sahrens } else { 24515367Sahrens *cp = '\0'; 24525367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 24535367Sahrens *cp = '/'; 24545367Sahrens } 24555367Sahrens if (h == NULL) 24565367Sahrens return (-1); 24575367Sahrens zfs_close(h); 24585367Sahrens 24595367Sahrens /* 24605367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 24615367Sahrens * up to the prefixlen-long one. 24625367Sahrens */ 24635367Sahrens for (cp = target + prefixlen + 1; 24645367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 24655367Sahrens char *logstr; 24665367Sahrens 24675367Sahrens *cp = '\0'; 24685367Sahrens 24695367Sahrens h = make_dataset_handle(hdl, target); 24705367Sahrens if (h) { 24715367Sahrens /* it already exists, nothing to do here */ 24725367Sahrens zfs_close(h); 24735367Sahrens continue; 24745367Sahrens } 24755367Sahrens 24765367Sahrens logstr = hdl->libzfs_log_str; 24775367Sahrens hdl->libzfs_log_str = NULL; 24785367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 24795367Sahrens NULL) != 0) { 24805367Sahrens hdl->libzfs_log_str = logstr; 24815367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 24825367Sahrens goto ancestorerr; 24835367Sahrens } 24845367Sahrens 24855367Sahrens hdl->libzfs_log_str = logstr; 24865367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 24875367Sahrens if (h == NULL) { 24885367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 24895367Sahrens goto ancestorerr; 24905367Sahrens } 24915367Sahrens 24925367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 24935367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 24945367Sahrens goto ancestorerr; 24955367Sahrens } 24965367Sahrens 24975367Sahrens if (zfs_share(h) != 0) { 24985367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 24995367Sahrens goto ancestorerr; 25005367Sahrens } 25015367Sahrens 25025367Sahrens zfs_close(h); 25035367Sahrens } 25045367Sahrens 25055367Sahrens return (0); 25065367Sahrens 25075367Sahrens ancestorerr: 25085367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25095367Sahrens "failed to %s ancestor '%s'"), opname, target); 25105367Sahrens return (-1); 25115367Sahrens } 25125367Sahrens 25135367Sahrens /* 25144490Svb160487 * Creates non-existing ancestors of the given path. 25154490Svb160487 */ 25164490Svb160487 int 25174490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 25184490Svb160487 { 25194490Svb160487 int prefix; 25204490Svb160487 uint64_t zoned; 25214490Svb160487 char *path_copy; 25224490Svb160487 int rc; 25234490Svb160487 25244490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 25254490Svb160487 return (-1); 25264490Svb160487 25274490Svb160487 if ((path_copy = strdup(path)) != NULL) { 25284490Svb160487 rc = create_parents(hdl, path_copy, prefix); 25294490Svb160487 free(path_copy); 25304490Svb160487 } 25314490Svb160487 if (path_copy == NULL || rc != 0) 25324490Svb160487 return (-1); 25334490Svb160487 2534789Sahrens return (0); 2535789Sahrens } 2536789Sahrens 2537789Sahrens /* 25382676Seschrock * Create a new filesystem or volume. 2539789Sahrens */ 2540789Sahrens int 25412082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 25422676Seschrock nvlist_t *props) 2543789Sahrens { 2544789Sahrens zfs_cmd_t zc = { 0 }; 2545789Sahrens int ret; 2546789Sahrens uint64_t size = 0; 2547789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 25482082Seschrock char errbuf[1024]; 25492676Seschrock uint64_t zoned; 25502082Seschrock 25512082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 25522082Seschrock "cannot create '%s'"), path); 2553789Sahrens 2554789Sahrens /* validate the path, taking care to note the extended error message */ 25555326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 25562082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2557789Sahrens 2558789Sahrens /* validate parents exist */ 25594490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2560789Sahrens return (-1); 2561789Sahrens 2562789Sahrens /* 2563789Sahrens * The failure modes when creating a dataset of a different type over 2564789Sahrens * one that already exists is a little strange. In particular, if you 2565789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2566789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2567789Sahrens * first try to see if the dataset exists. 2568789Sahrens */ 2569789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 25705094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 25712082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25722082Seschrock "dataset already exists")); 25732082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2574789Sahrens } 2575789Sahrens 2576789Sahrens if (type == ZFS_TYPE_VOLUME) 2577789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2578789Sahrens else 2579789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2580789Sahrens 25817184Stimh if (props && (props = zfs_valid_proplist(hdl, type, props, 25823912Slling zoned, NULL, errbuf)) == 0) 25832676Seschrock return (-1); 25842676Seschrock 2585789Sahrens if (type == ZFS_TYPE_VOLUME) { 25861133Seschrock /* 25871133Seschrock * If we are creating a volume, the size and block size must 25881133Seschrock * satisfy a few restraints. First, the blocksize must be a 25891133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 25901133Seschrock * volsize must be a multiple of the block size, and cannot be 25911133Seschrock * zero. 25921133Seschrock */ 25932676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 25942676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 25952676Seschrock nvlist_free(props); 25962082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25972676Seschrock "missing volume size")); 25982676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2599789Sahrens } 2600789Sahrens 26012676Seschrock if ((ret = nvlist_lookup_uint64(props, 26022676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 26032676Seschrock &blocksize)) != 0) { 26042676Seschrock if (ret == ENOENT) { 26052676Seschrock blocksize = zfs_prop_default_numeric( 26062676Seschrock ZFS_PROP_VOLBLOCKSIZE); 26072676Seschrock } else { 26082676Seschrock nvlist_free(props); 26092676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26102676Seschrock "missing volume block size")); 26112676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26122676Seschrock } 26132676Seschrock } 26142676Seschrock 26152676Seschrock if (size == 0) { 26162676Seschrock nvlist_free(props); 26172082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26182676Seschrock "volume size cannot be zero")); 26192676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26201133Seschrock } 26211133Seschrock 26221133Seschrock if (size % blocksize != 0) { 26232676Seschrock nvlist_free(props); 26242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26252676Seschrock "volume size must be a multiple of volume block " 26262676Seschrock "size")); 26272676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26281133Seschrock } 2629789Sahrens } 2630789Sahrens 26315094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 26322676Seschrock return (-1); 26332676Seschrock nvlist_free(props); 26342676Seschrock 2635789Sahrens /* create the dataset */ 26364543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2637789Sahrens 26382676Seschrock zcmd_free_nvlists(&zc); 26392676Seschrock 2640789Sahrens /* check for failure */ 2641789Sahrens if (ret != 0) { 2642789Sahrens char parent[ZFS_MAXNAMELEN]; 2643789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2644789Sahrens 2645789Sahrens switch (errno) { 2646789Sahrens case ENOENT: 26472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26482082Seschrock "no such parent '%s'"), parent); 26492082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2650789Sahrens 2651789Sahrens case EINVAL: 26522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26533413Smmusante "parent '%s' is not a filesystem"), parent); 26542082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2655789Sahrens 2656789Sahrens case EDOM: 26572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26582676Seschrock "volume block size must be power of 2 from " 26592676Seschrock "%u to %uk"), 2660789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2661789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 26622082Seschrock 26632676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26642082Seschrock 26654603Sahrens case ENOTSUP: 26664603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26674603Sahrens "pool must be upgraded to set this " 26684603Sahrens "property or value")); 26694603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 2670789Sahrens #ifdef _ILP32 2671789Sahrens case EOVERFLOW: 2672789Sahrens /* 2673789Sahrens * This platform can't address a volume this big. 2674789Sahrens */ 26752082Seschrock if (type == ZFS_TYPE_VOLUME) 26762082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 26772082Seschrock errbuf)); 2678789Sahrens #endif 26792082Seschrock /* FALLTHROUGH */ 2680789Sahrens default: 26812082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2682789Sahrens } 2683789Sahrens } 2684789Sahrens 2685789Sahrens return (0); 2686789Sahrens } 2687789Sahrens 2688789Sahrens /* 2689789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2690789Sahrens * isn't mounted, and that there are no active dependents. 2691789Sahrens */ 2692789Sahrens int 269310242Schris.kirby@sun.com zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 2694789Sahrens { 2695789Sahrens zfs_cmd_t zc = { 0 }; 2696789Sahrens 2697789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2698789Sahrens 26992676Seschrock if (ZFS_IS_VOLUME(zhp)) { 27003126Sahl /* 27014543Smarks * If user doesn't have permissions to unshare volume, then 27024543Smarks * abort the request. This would only happen for a 27034543Smarks * non-privileged user. 27043126Sahl */ 27054543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 27064543Smarks return (-1); 27074543Smarks } 27083126Sahl 2709789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2710789Sahrens } else { 2711789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2712789Sahrens } 2713789Sahrens 271410242Schris.kirby@sun.com zc.zc_defer_destroy = defer; 27154543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 27163237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 27172082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 27182082Seschrock zhp->zfs_name)); 27192199Sahrens } 2720789Sahrens 2721789Sahrens remove_mountpoint(zhp); 2722789Sahrens 2723789Sahrens return (0); 2724789Sahrens } 2725789Sahrens 27262199Sahrens struct destroydata { 27272199Sahrens char *snapname; 27282199Sahrens boolean_t gotone; 27293265Sahrens boolean_t closezhp; 27302199Sahrens }; 27312199Sahrens 27322199Sahrens static int 273310588SEric.Taylor@Sun.COM zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 27342199Sahrens { 27352199Sahrens struct destroydata *dd = arg; 27362199Sahrens zfs_handle_t *szhp; 27372199Sahrens char name[ZFS_MAXNAMELEN]; 27383265Sahrens boolean_t closezhp = dd->closezhp; 273910588SEric.Taylor@Sun.COM int rv = 0; 27402199Sahrens 27412676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 27422676Seschrock (void) strlcat(name, "@", sizeof (name)); 27432676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 27442199Sahrens 27452199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 27462199Sahrens if (szhp) { 27472199Sahrens dd->gotone = B_TRUE; 27482199Sahrens zfs_close(szhp); 27492199Sahrens } 27502199Sahrens 27513265Sahrens dd->closezhp = B_TRUE; 275210588SEric.Taylor@Sun.COM if (!dd->gotone) 275310588SEric.Taylor@Sun.COM rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg); 27543265Sahrens if (closezhp) 27553265Sahrens zfs_close(zhp); 27563265Sahrens return (rv); 27572199Sahrens } 27582199Sahrens 27592199Sahrens /* 27602199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 27612199Sahrens */ 27622199Sahrens int 276310242Schris.kirby@sun.com zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 27642199Sahrens { 27652199Sahrens zfs_cmd_t zc = { 0 }; 27662199Sahrens int ret; 27672199Sahrens struct destroydata dd = { 0 }; 27682199Sahrens 27692199Sahrens dd.snapname = snapname; 277010588SEric.Taylor@Sun.COM (void) zfs_check_snap_cb(zhp, &dd); 27712199Sahrens 27722199Sahrens if (!dd.gotone) { 27733237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 27742199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 27752199Sahrens zhp->zfs_name, snapname)); 27762199Sahrens } 27772199Sahrens 27782199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 27792676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 278010242Schris.kirby@sun.com zc.zc_defer_destroy = defer; 27812199Sahrens 27824543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 27832199Sahrens if (ret != 0) { 27842199Sahrens char errbuf[1024]; 27852199Sahrens 27862199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 27872199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 27882199Sahrens 27892199Sahrens switch (errno) { 27902199Sahrens case EEXIST: 27912199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 27922199Sahrens "snapshot is cloned")); 27932199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 27942199Sahrens 27952199Sahrens default: 27962199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 27972199Sahrens errbuf)); 27982199Sahrens } 27992199Sahrens } 28002199Sahrens 28012199Sahrens return (0); 28022199Sahrens } 28032199Sahrens 2804789Sahrens /* 2805789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2806789Sahrens */ 2807789Sahrens int 28082676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2809789Sahrens { 2810789Sahrens zfs_cmd_t zc = { 0 }; 2811789Sahrens char parent[ZFS_MAXNAMELEN]; 2812789Sahrens int ret; 28132082Seschrock char errbuf[1024]; 28142082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 28152676Seschrock zfs_type_t type; 28162676Seschrock uint64_t zoned; 2817789Sahrens 2818789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2819789Sahrens 28202082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28212082Seschrock "cannot create '%s'"), target); 28222082Seschrock 2823789Sahrens /* validate the target name */ 28245326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 28252082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2826789Sahrens 2827789Sahrens /* validate parents exist */ 28284490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2829789Sahrens return (-1); 2830789Sahrens 2831789Sahrens (void) parent_name(target, parent, sizeof (parent)); 2832789Sahrens 2833789Sahrens /* do the clone */ 28342676Seschrock if (ZFS_IS_VOLUME(zhp)) { 2835789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 28362744Snn35248 type = ZFS_TYPE_VOLUME; 28372676Seschrock } else { 2838789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 28392744Snn35248 type = ZFS_TYPE_FILESYSTEM; 28402676Seschrock } 28412676Seschrock 28422676Seschrock if (props) { 28437184Stimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 28447184Stimh zhp, errbuf)) == NULL) 28452676Seschrock return (-1); 28462676Seschrock 28475094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 28482676Seschrock nvlist_free(props); 28492676Seschrock return (-1); 28502676Seschrock } 28512676Seschrock 28522676Seschrock nvlist_free(props); 28532676Seschrock } 2854789Sahrens 2855789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 28562676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 28574543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2858789Sahrens 28592676Seschrock zcmd_free_nvlists(&zc); 28602676Seschrock 2861789Sahrens if (ret != 0) { 2862789Sahrens switch (errno) { 2863789Sahrens 2864789Sahrens case ENOENT: 2865789Sahrens /* 2866789Sahrens * The parent doesn't exist. We should have caught this 2867789Sahrens * above, but there may a race condition that has since 2868789Sahrens * destroyed the parent. 2869789Sahrens * 2870789Sahrens * At this point, we don't know whether it's the source 2871789Sahrens * that doesn't exist anymore, or whether the target 2872789Sahrens * dataset doesn't exist. 2873789Sahrens */ 28742082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28752082Seschrock "no such parent '%s'"), parent); 28762082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 28772082Seschrock 28782082Seschrock case EXDEV: 28792082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28802082Seschrock "source and target pools differ")); 28812082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 28822082Seschrock errbuf)); 28832082Seschrock 28842082Seschrock default: 28852082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 28862082Seschrock errbuf)); 28872082Seschrock } 28882082Seschrock } 28892082Seschrock 28902082Seschrock return (ret); 28912082Seschrock } 28922082Seschrock 28932082Seschrock /* 28942082Seschrock * Promotes the given clone fs to be the clone parent. 28952082Seschrock */ 28962082Seschrock int 28972082Seschrock zfs_promote(zfs_handle_t *zhp) 28982082Seschrock { 28992082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 29002082Seschrock zfs_cmd_t zc = { 0 }; 29012082Seschrock char parent[MAXPATHLEN]; 29022082Seschrock int ret; 29032082Seschrock char errbuf[1024]; 29042082Seschrock 29052082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29062082Seschrock "cannot promote '%s'"), zhp->zfs_name); 29072082Seschrock 29082082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 29092082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29102082Seschrock "snapshots can not be promoted")); 29112082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 29122082Seschrock } 29132082Seschrock 29145367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 29152082Seschrock if (parent[0] == '\0') { 29162082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29172082Seschrock "not a cloned filesystem")); 29182082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 29192082Seschrock } 292010588SEric.Taylor@Sun.COM 29215367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 29222676Seschrock sizeof (zc.zc_value)); 29232082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 29244543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 29252082Seschrock 29262082Seschrock if (ret != 0) { 29272417Sahrens int save_errno = errno; 29282417Sahrens 29292417Sahrens switch (save_errno) { 2930789Sahrens case EEXIST: 293110588SEric.Taylor@Sun.COM /* There is a conflicting snapshot name. */ 29322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 293310588SEric.Taylor@Sun.COM "conflicting snapshot '%s' from parent '%s'"), 293410588SEric.Taylor@Sun.COM zc.zc_string, parent); 29352082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2936789Sahrens 2937789Sahrens default: 29382417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 2939789Sahrens } 2940789Sahrens } 29412676Seschrock return (ret); 29422199Sahrens } 29432199Sahrens 2944789Sahrens /* 29453504Sahl * Takes a snapshot of the given dataset. 2946789Sahrens */ 2947789Sahrens int 29487265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 29497265Sahrens nvlist_t *props) 2950789Sahrens { 2951789Sahrens const char *delim; 29527265Sahrens char parent[ZFS_MAXNAMELEN]; 2953789Sahrens zfs_handle_t *zhp; 2954789Sahrens zfs_cmd_t zc = { 0 }; 2955789Sahrens int ret; 29562082Seschrock char errbuf[1024]; 29572082Seschrock 29582082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29592082Seschrock "cannot snapshot '%s'"), path); 29602082Seschrock 29612082Seschrock /* validate the target name */ 29625326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 29632082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2964789Sahrens 29657265Sahrens if (props) { 29667265Sahrens if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 29677265Sahrens props, B_FALSE, NULL, errbuf)) == NULL) 29687265Sahrens return (-1); 29697265Sahrens 29707265Sahrens if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 29717265Sahrens nvlist_free(props); 29727265Sahrens return (-1); 29737265Sahrens } 29747265Sahrens 29757265Sahrens nvlist_free(props); 29767265Sahrens } 29777265Sahrens 2978789Sahrens /* make sure the parent exists and is of the appropriate type */ 29792199Sahrens delim = strchr(path, '@'); 2980789Sahrens (void) strncpy(parent, path, delim - path); 2981789Sahrens parent[delim - path] = '\0'; 2982789Sahrens 29832082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 2984789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 29857265Sahrens zcmd_free_nvlists(&zc); 2986789Sahrens return (-1); 2987789Sahrens } 2988789Sahrens 29892199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 29902676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 29914543Smarks if (ZFS_IS_VOLUME(zhp)) 29924543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 29934543Smarks else 29944543Smarks zc.zc_objset_type = DMU_OST_ZFS; 29952199Sahrens zc.zc_cookie = recursive; 29964543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 29972199Sahrens 29987265Sahrens zcmd_free_nvlists(&zc); 29997265Sahrens 30002199Sahrens /* 30012199Sahrens * if it was recursive, the one that actually failed will be in 30022199Sahrens * zc.zc_name. 30032199Sahrens */ 300410588SEric.Taylor@Sun.COM if (ret != 0) { 30054543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30064543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 300710588SEric.Taylor@Sun.COM (void) zfs_standard_error(hdl, errno, errbuf); 30082199Sahrens } 3009789Sahrens 3010789Sahrens zfs_close(zhp); 3011789Sahrens 3012789Sahrens return (ret); 3013789Sahrens } 3014789Sahrens 3015789Sahrens /* 30161294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 30171294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 30181294Slling * is a dependent and we should just destroy it without checking the transaction 30191294Slling * group. 3020789Sahrens */ 30211294Slling typedef struct rollback_data { 30221294Slling const char *cb_target; /* the snapshot */ 30231294Slling uint64_t cb_create; /* creation time reference */ 30245749Sahrens boolean_t cb_error; 30252082Seschrock boolean_t cb_dependent; 30265749Sahrens boolean_t cb_force; 30271294Slling } rollback_data_t; 30281294Slling 30291294Slling static int 30301294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 30311294Slling { 30321294Slling rollback_data_t *cbp = data; 30331294Slling 30341294Slling if (!cbp->cb_dependent) { 30351294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 30361294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 30371294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 30381294Slling cbp->cb_create) { 30394543Smarks char *logstr; 30401294Slling 30412082Seschrock cbp->cb_dependent = B_TRUE; 30425446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 30435446Sahrens rollback_destroy, cbp); 30442082Seschrock cbp->cb_dependent = B_FALSE; 30451294Slling 30464543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 30474543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 304810242Schris.kirby@sun.com cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 30494543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 30501294Slling } 30511294Slling } else { 30525749Sahrens /* We must destroy this clone; first unmount it */ 30535749Sahrens prop_changelist_t *clp; 30545749Sahrens 30557366STim.Haley@Sun.COM clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 30565749Sahrens cbp->cb_force ? MS_FORCE: 0); 30575749Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 30585749Sahrens cbp->cb_error = B_TRUE; 30595749Sahrens zfs_close(zhp); 30605749Sahrens return (0); 30615749Sahrens } 306210242Schris.kirby@sun.com if (zfs_destroy(zhp, B_FALSE) != 0) 30635749Sahrens cbp->cb_error = B_TRUE; 30645749Sahrens else 30655749Sahrens changelist_remove(clp, zhp->zfs_name); 30665751Sahrens (void) changelist_postfix(clp); 30675749Sahrens changelist_free(clp); 30681294Slling } 30691294Slling 30701294Slling zfs_close(zhp); 30711294Slling return (0); 30721294Slling } 30731294Slling 30741294Slling /* 30755446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 30765446Sahrens * data changes since then and making it the active dataset. 30775446Sahrens * 30785446Sahrens * Any snapshots more recent than the target are destroyed, along with 30795446Sahrens * their dependents. 30801294Slling */ 30815446Sahrens int 30825749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3083789Sahrens { 30845446Sahrens rollback_data_t cb = { 0 }; 30855446Sahrens int err; 3086789Sahrens zfs_cmd_t zc = { 0 }; 30875713Srm160521 boolean_t restore_resv = 0; 30885713Srm160521 uint64_t old_volsize, new_volsize; 30895713Srm160521 zfs_prop_t resv_prop; 3090789Sahrens 3091789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3092789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3093789Sahrens 30945446Sahrens /* 30955446Sahrens * Destroy all recent snapshots and its dependends. 30965446Sahrens */ 30975749Sahrens cb.cb_force = force; 30985446Sahrens cb.cb_target = snap->zfs_name; 30995446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 31005446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 31015446Sahrens 31025749Sahrens if (cb.cb_error) 31035749Sahrens return (-1); 31045446Sahrens 31055446Sahrens /* 31065446Sahrens * Now that we have verified that the snapshot is the latest, 31075446Sahrens * rollback to the given snapshot. 31085446Sahrens */ 31095446Sahrens 31105713Srm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 31115713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 31125713Srm160521 return (-1); 31135713Srm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 31145713Srm160521 restore_resv = 31155713Srm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 31165713Srm160521 } 3117789Sahrens 3118789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3119789Sahrens 31202676Seschrock if (ZFS_IS_VOLUME(zhp)) 3121789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3122789Sahrens else 3123789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3124789Sahrens 3125789Sahrens /* 31265446Sahrens * We rely on zfs_iter_children() to verify that there are no 31275446Sahrens * newer snapshots for the given dataset. Therefore, we can 31285446Sahrens * simply pass the name on to the ioctl() call. There is still 31295446Sahrens * an unlikely race condition where the user has taken a 31305446Sahrens * snapshot since we verified that this was the most recent. 31315713Srm160521 * 3132789Sahrens */ 31335446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 31343237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 31352082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 31362082Seschrock zhp->zfs_name); 31375717Srm160521 return (err); 31385717Srm160521 } 31395713Srm160521 31405713Srm160521 /* 31415713Srm160521 * For volumes, if the pre-rollback volsize matched the pre- 31425713Srm160521 * rollback reservation and the volsize has changed then set 31435713Srm160521 * the reservation property to the post-rollback volsize. 31445713Srm160521 * Make a new handle since the rollback closed the dataset. 31455713Srm160521 */ 31465717Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 31475717Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 31485713Srm160521 if (restore_resv) { 31495713Srm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 31505713Srm160521 if (old_volsize != new_volsize) 31515717Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 31525717Srm160521 new_volsize); 31535713Srm160521 } 31545713Srm160521 zfs_close(zhp); 3155789Sahrens } 31565446Sahrens return (err); 31571294Slling } 31581294Slling 31591294Slling /* 3160789Sahrens * Iterate over all dependents for a given dataset. This includes both 3161789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3162789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3163789Sahrens * libzfs_graph.c. 3164789Sahrens */ 3165789Sahrens int 31662474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 31672474Seschrock zfs_iter_f func, void *data) 3168789Sahrens { 3169789Sahrens char **dependents; 3170789Sahrens size_t count; 3171789Sahrens int i; 3172789Sahrens zfs_handle_t *child; 3173789Sahrens int ret = 0; 3174789Sahrens 31752474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 31762474Seschrock &dependents, &count) != 0) 31772474Seschrock return (-1); 31782474Seschrock 3179789Sahrens for (i = 0; i < count; i++) { 31802082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 31812082Seschrock dependents[i])) == NULL) 3182789Sahrens continue; 3183789Sahrens 3184789Sahrens if ((ret = func(child, data)) != 0) 3185789Sahrens break; 3186789Sahrens } 3187789Sahrens 3188789Sahrens for (i = 0; i < count; i++) 3189789Sahrens free(dependents[i]); 3190789Sahrens free(dependents); 3191789Sahrens 3192789Sahrens return (ret); 3193789Sahrens } 3194789Sahrens 3195789Sahrens /* 3196789Sahrens * Renames the given dataset. 3197789Sahrens */ 3198789Sahrens int 31994490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3200789Sahrens { 3201789Sahrens int ret; 3202789Sahrens zfs_cmd_t zc = { 0 }; 3203789Sahrens char *delim; 32044007Smmusante prop_changelist_t *cl = NULL; 32054007Smmusante zfs_handle_t *zhrp = NULL; 32064007Smmusante char *parentname = NULL; 3207789Sahrens char parent[ZFS_MAXNAMELEN]; 32082082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 32092082Seschrock char errbuf[1024]; 3210789Sahrens 3211789Sahrens /* if we have the same exact name, just return success */ 3212789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3213789Sahrens return (0); 3214789Sahrens 32152082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32162082Seschrock "cannot rename to '%s'"), target); 32172082Seschrock 3218789Sahrens /* 3219789Sahrens * Make sure the target name is valid 3220789Sahrens */ 3221789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 32222665Snd150628 if ((strchr(target, '@') == NULL) || 32232665Snd150628 *target == '@') { 32242665Snd150628 /* 32252665Snd150628 * Snapshot target name is abbreviated, 32262665Snd150628 * reconstruct full dataset name 32272665Snd150628 */ 32282665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 32292665Snd150628 sizeof (parent)); 32302665Snd150628 delim = strchr(parent, '@'); 32312665Snd150628 if (strchr(target, '@') == NULL) 32322665Snd150628 *(++delim) = '\0'; 32332665Snd150628 else 32342665Snd150628 *delim = '\0'; 32352665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 32362665Snd150628 target = parent; 32372665Snd150628 } else { 32382665Snd150628 /* 32392665Snd150628 * Make sure we're renaming within the same dataset. 32402665Snd150628 */ 32412665Snd150628 delim = strchr(target, '@'); 32422665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 32432665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 32442665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32452665Snd150628 "snapshots must be part of same " 32462665Snd150628 "dataset")); 32472665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 32483912Slling errbuf)); 32492665Snd150628 } 3250789Sahrens } 32515326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 32522665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3253789Sahrens } else { 32544007Smmusante if (recursive) { 32554007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32564007Smmusante "recursive rename must be a snapshot")); 32574007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 32584007Smmusante } 32594007Smmusante 32605326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 32612665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 32622676Seschrock uint64_t unused; 32632676Seschrock 3264789Sahrens /* validate parents */ 32654490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3266789Sahrens return (-1); 3267789Sahrens 3268789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3269789Sahrens 3270789Sahrens /* make sure we're in the same pool */ 3271789Sahrens verify((delim = strchr(target, '/')) != NULL); 3272789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3273789Sahrens zhp->zfs_name[delim - target] != '/') { 32742082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32752082Seschrock "datasets must be within same pool")); 32762082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3277789Sahrens } 32782440Snd150628 32792440Snd150628 /* new name cannot be a child of the current dataset name */ 32802440Snd150628 if (strncmp(parent, zhp->zfs_name, 32813912Slling strlen(zhp->zfs_name)) == 0) { 32822440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32832440Snd150628 "New dataset name cannot be a descendent of " 32842440Snd150628 "current dataset name")); 32852440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 32862440Snd150628 } 3287789Sahrens } 3288789Sahrens 32892082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 32902082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 32912082Seschrock 3292789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3293789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 32942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32952082Seschrock "dataset is used in a non-global zone")); 32962082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3297789Sahrens } 3298789Sahrens 32994007Smmusante if (recursive) { 33004007Smmusante 33014183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 33024183Smmusante if (parentname == NULL) { 33034183Smmusante ret = -1; 33044183Smmusante goto error; 33054183Smmusante } 33064007Smmusante delim = strchr(parentname, '@'); 33074007Smmusante *delim = '\0'; 33085094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 33094007Smmusante if (zhrp == NULL) { 33104183Smmusante ret = -1; 33114183Smmusante goto error; 33124007Smmusante } 33134007Smmusante 33144007Smmusante } else { 33157366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL) 33164007Smmusante return (-1); 33174007Smmusante 33184007Smmusante if (changelist_haszonedchild(cl)) { 33194007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33204007Smmusante "child dataset with inherited mountpoint is used " 33214007Smmusante "in a non-global zone")); 33224007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 33234007Smmusante goto error; 33244007Smmusante } 33254007Smmusante 33264007Smmusante if ((ret = changelist_prefix(cl)) != 0) 33274007Smmusante goto error; 3328789Sahrens } 3329789Sahrens 33302676Seschrock if (ZFS_IS_VOLUME(zhp)) 3331789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3332789Sahrens else 3333789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3334789Sahrens 33352665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33362676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 33372665Snd150628 33384007Smmusante zc.zc_cookie = recursive; 33394007Smmusante 33404543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 33414007Smmusante /* 33424007Smmusante * if it was recursive, the one that actually failed will 33434007Smmusante * be in zc.zc_name 33444007Smmusante */ 33454007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33465367Sahrens "cannot rename '%s'"), zc.zc_name); 33474007Smmusante 33484007Smmusante if (recursive && errno == EEXIST) { 33494007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33504007Smmusante "a child dataset already has a snapshot " 33514007Smmusante "with the new name")); 33524801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 33534007Smmusante } else { 33544007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 33554007Smmusante } 3356789Sahrens 3357789Sahrens /* 3358789Sahrens * On failure, we still want to remount any filesystems that 3359789Sahrens * were previously mounted, so we don't alter the system state. 3360789Sahrens */ 336110588SEric.Taylor@Sun.COM if (!recursive) 33624007Smmusante (void) changelist_postfix(cl); 3363789Sahrens } else { 336410588SEric.Taylor@Sun.COM if (!recursive) { 33654007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 33664007Smmusante ret = changelist_postfix(cl); 33674007Smmusante } 3368789Sahrens } 3369789Sahrens 3370789Sahrens error: 33714007Smmusante if (parentname) { 33724007Smmusante free(parentname); 33734007Smmusante } 33744007Smmusante if (zhrp) { 33754007Smmusante zfs_close(zhrp); 33764007Smmusante } 33774007Smmusante if (cl) { 33784007Smmusante changelist_free(cl); 33794007Smmusante } 3380789Sahrens return (ret); 3381789Sahrens } 3382789Sahrens 33832676Seschrock nvlist_t * 33842676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 33852676Seschrock { 33862676Seschrock return (zhp->zfs_user_props); 33872676Seschrock } 33882676Seschrock 33892676Seschrock /* 33903912Slling * This function is used by 'zfs list' to determine the exact set of columns to 33913912Slling * display, and their maximum widths. This does two main things: 33923912Slling * 33933912Slling * - If this is a list of all properties, then expand the list to include 33943912Slling * all native properties, and set a flag so that for each dataset we look 33953912Slling * for new unique user properties and add them to the list. 33963912Slling * 33973912Slling * - For non fixed-width properties, keep track of the maximum width seen 33983912Slling * so that we can size the column appropriately. 33993912Slling */ 34003912Slling int 34015094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 34023912Slling { 34033912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 34045094Slling zprop_list_t *entry; 34055094Slling zprop_list_t **last, **start; 34063912Slling nvlist_t *userprops, *propval; 34073912Slling nvpair_t *elem; 34083912Slling char *strval; 34093912Slling char buf[ZFS_MAXPROPLEN]; 34103912Slling 34115094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 34123912Slling return (-1); 34132676Seschrock 34142676Seschrock userprops = zfs_get_user_props(zhp); 34152676Seschrock 34162676Seschrock entry = *plp; 34172676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 34182676Seschrock /* 34192676Seschrock * Go through and add any user properties as necessary. We 34202676Seschrock * start by incrementing our list pointer to the first 34212676Seschrock * non-native property. 34222676Seschrock */ 34232676Seschrock start = plp; 34242676Seschrock while (*start != NULL) { 34255094Slling if ((*start)->pl_prop == ZPROP_INVAL) 34262676Seschrock break; 34272676Seschrock start = &(*start)->pl_next; 34282676Seschrock } 34292676Seschrock 34302676Seschrock elem = NULL; 34312676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 34322676Seschrock /* 34332676Seschrock * See if we've already found this property in our list. 34342676Seschrock */ 34352676Seschrock for (last = start; *last != NULL; 34362676Seschrock last = &(*last)->pl_next) { 34372676Seschrock if (strcmp((*last)->pl_user_prop, 34382676Seschrock nvpair_name(elem)) == 0) 34392676Seschrock break; 34402676Seschrock } 34412676Seschrock 34422676Seschrock if (*last == NULL) { 34432676Seschrock if ((entry = zfs_alloc(hdl, 34445094Slling sizeof (zprop_list_t))) == NULL || 34452676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 34462676Seschrock nvpair_name(elem)))) == NULL) { 34472676Seschrock free(entry); 34482676Seschrock return (-1); 34492676Seschrock } 34502676Seschrock 34515094Slling entry->pl_prop = ZPROP_INVAL; 34522676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 34532676Seschrock entry->pl_all = B_TRUE; 34542676Seschrock *last = entry; 34552676Seschrock } 34562676Seschrock } 34572676Seschrock } 34582676Seschrock 34592676Seschrock /* 34602676Seschrock * Now go through and check the width of any non-fixed columns 34612676Seschrock */ 34622676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 34632676Seschrock if (entry->pl_fixed) 34642676Seschrock continue; 34652676Seschrock 34665094Slling if (entry->pl_prop != ZPROP_INVAL) { 34672676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 34682676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 34692676Seschrock if (strlen(buf) > entry->pl_width) 34702676Seschrock entry->pl_width = strlen(buf); 34712676Seschrock } 34722676Seschrock } else if (nvlist_lookup_nvlist(userprops, 34732676Seschrock entry->pl_user_prop, &propval) == 0) { 34742676Seschrock verify(nvlist_lookup_string(propval, 34755094Slling ZPROP_VALUE, &strval) == 0); 34762676Seschrock if (strlen(strval) > entry->pl_width) 34772676Seschrock entry->pl_width = strlen(strval); 34782676Seschrock } 34792676Seschrock } 34802676Seschrock 34812676Seschrock return (0); 34822676Seschrock } 34834543Smarks 34844543Smarks int 34854543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 34864543Smarks { 34874543Smarks zfs_cmd_t zc = { 0 }; 34884543Smarks nvlist_t *nvp; 34894543Smarks gid_t gid; 34904543Smarks uid_t uid; 34914543Smarks const gid_t *groups; 34924543Smarks int group_cnt; 34934543Smarks int error; 34944543Smarks 34954543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 34964543Smarks return (no_memory(hdl)); 34974543Smarks 34984543Smarks uid = ucred_geteuid(cred); 34994543Smarks gid = ucred_getegid(cred); 35004543Smarks group_cnt = ucred_getgroups(cred, &groups); 35014543Smarks 35024543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 35034543Smarks return (1); 35044543Smarks 35054543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 35064543Smarks nvlist_free(nvp); 35074543Smarks return (1); 35084543Smarks } 35094543Smarks 35104543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 35114543Smarks nvlist_free(nvp); 35124543Smarks return (1); 35134543Smarks } 35144543Smarks 35154543Smarks if (nvlist_add_uint32_array(nvp, 35164543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 35174543Smarks nvlist_free(nvp); 35184543Smarks return (1); 35194543Smarks } 35204543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 35214543Smarks 35225094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 35234543Smarks return (-1); 35244543Smarks 35254543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 35264543Smarks nvlist_free(nvp); 35274543Smarks return (error); 35284543Smarks } 35294543Smarks 35304543Smarks int 35314543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 35328845Samw@Sun.COM char *resource, void *export, void *sharetab, 35338845Samw@Sun.COM int sharemax, zfs_share_op_t operation) 35344543Smarks { 35354543Smarks zfs_cmd_t zc = { 0 }; 35364543Smarks int error; 35374543Smarks 35384543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 35394543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 35408845Samw@Sun.COM if (resource) 35418845Samw@Sun.COM (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 35424543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 35434543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 35445331Samw zc.zc_share.z_sharetype = operation; 35454543Smarks zc.zc_share.z_sharemax = sharemax; 35464543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 35474543Smarks return (error); 35484543Smarks } 35498802SSanjeev.Bagewadi@Sun.COM 35508802SSanjeev.Bagewadi@Sun.COM void 35518802SSanjeev.Bagewadi@Sun.COM zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 35528802SSanjeev.Bagewadi@Sun.COM { 35538802SSanjeev.Bagewadi@Sun.COM nvpair_t *curr; 35548802SSanjeev.Bagewadi@Sun.COM 35558802SSanjeev.Bagewadi@Sun.COM /* 35568802SSanjeev.Bagewadi@Sun.COM * Keep a reference to the props-table against which we prune the 35578802SSanjeev.Bagewadi@Sun.COM * properties. 35588802SSanjeev.Bagewadi@Sun.COM */ 35598802SSanjeev.Bagewadi@Sun.COM zhp->zfs_props_table = props; 35608802SSanjeev.Bagewadi@Sun.COM 35618802SSanjeev.Bagewadi@Sun.COM curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 35628802SSanjeev.Bagewadi@Sun.COM 35638802SSanjeev.Bagewadi@Sun.COM while (curr) { 35648802SSanjeev.Bagewadi@Sun.COM zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 35658802SSanjeev.Bagewadi@Sun.COM nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 35668802SSanjeev.Bagewadi@Sun.COM 35679396SMatthew.Ahrens@Sun.COM /* 356810960SEric.Schrock@Sun.COM * User properties will result in ZPROP_INVAL, and since we 356910960SEric.Schrock@Sun.COM * only know how to prune standard ZFS properties, we always 357010960SEric.Schrock@Sun.COM * leave these in the list. This can also happen if we 357110960SEric.Schrock@Sun.COM * encounter an unknown DSL property (when running older 357210960SEric.Schrock@Sun.COM * software, for example). 35739396SMatthew.Ahrens@Sun.COM */ 35749396SMatthew.Ahrens@Sun.COM if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 35758802SSanjeev.Bagewadi@Sun.COM (void) nvlist_remove(zhp->zfs_props, 35768802SSanjeev.Bagewadi@Sun.COM nvpair_name(curr), nvpair_type(curr)); 35778802SSanjeev.Bagewadi@Sun.COM curr = next; 35788802SSanjeev.Bagewadi@Sun.COM } 35798802SSanjeev.Bagewadi@Sun.COM } 35808845Samw@Sun.COM 35818845Samw@Sun.COM static int 35828845Samw@Sun.COM zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 35838845Samw@Sun.COM zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 35848845Samw@Sun.COM { 35858845Samw@Sun.COM zfs_cmd_t zc = { 0 }; 35868845Samw@Sun.COM nvlist_t *nvlist = NULL; 35878845Samw@Sun.COM int error; 35888845Samw@Sun.COM 35898845Samw@Sun.COM (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 35908845Samw@Sun.COM (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 35918845Samw@Sun.COM zc.zc_cookie = (uint64_t)cmd; 35928845Samw@Sun.COM 35938845Samw@Sun.COM if (cmd == ZFS_SMB_ACL_RENAME) { 35948845Samw@Sun.COM if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 35958845Samw@Sun.COM (void) no_memory(hdl); 35968845Samw@Sun.COM return (NULL); 35978845Samw@Sun.COM } 35988845Samw@Sun.COM } 35998845Samw@Sun.COM 36008845Samw@Sun.COM switch (cmd) { 36018845Samw@Sun.COM case ZFS_SMB_ACL_ADD: 36028845Samw@Sun.COM case ZFS_SMB_ACL_REMOVE: 36038845Samw@Sun.COM (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 36048845Samw@Sun.COM break; 36058845Samw@Sun.COM case ZFS_SMB_ACL_RENAME: 36068845Samw@Sun.COM if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 36078845Samw@Sun.COM resource1) != 0) { 36088845Samw@Sun.COM (void) no_memory(hdl); 36098845Samw@Sun.COM return (-1); 36108845Samw@Sun.COM } 36118845Samw@Sun.COM if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 36128845Samw@Sun.COM resource2) != 0) { 36138845Samw@Sun.COM (void) no_memory(hdl); 36148845Samw@Sun.COM return (-1); 36158845Samw@Sun.COM } 36168845Samw@Sun.COM if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 36178845Samw@Sun.COM nvlist_free(nvlist); 36188845Samw@Sun.COM return (-1); 36198845Samw@Sun.COM } 36208845Samw@Sun.COM break; 36218845Samw@Sun.COM case ZFS_SMB_ACL_PURGE: 36228845Samw@Sun.COM break; 36238845Samw@Sun.COM default: 36248845Samw@Sun.COM return (-1); 36258845Samw@Sun.COM } 36268845Samw@Sun.COM error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 36278845Samw@Sun.COM if (nvlist) 36288845Samw@Sun.COM nvlist_free(nvlist); 36298845Samw@Sun.COM return (error); 36308845Samw@Sun.COM } 36318845Samw@Sun.COM 36328845Samw@Sun.COM int 36338845Samw@Sun.COM zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 36348845Samw@Sun.COM char *path, char *resource) 36358845Samw@Sun.COM { 36368845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 36378845Samw@Sun.COM resource, NULL)); 36388845Samw@Sun.COM } 36398845Samw@Sun.COM 36408845Samw@Sun.COM int 36418845Samw@Sun.COM zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 36428845Samw@Sun.COM char *path, char *resource) 36438845Samw@Sun.COM { 36448845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 36458845Samw@Sun.COM resource, NULL)); 36468845Samw@Sun.COM } 36478845Samw@Sun.COM 36488845Samw@Sun.COM int 36498845Samw@Sun.COM zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 36508845Samw@Sun.COM { 36518845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 36528845Samw@Sun.COM NULL, NULL)); 36538845Samw@Sun.COM } 36548845Samw@Sun.COM 36558845Samw@Sun.COM int 36568845Samw@Sun.COM zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 36578845Samw@Sun.COM char *oldname, char *newname) 36588845Samw@Sun.COM { 36598845Samw@Sun.COM return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 36608845Samw@Sun.COM oldname, newname)); 36618845Samw@Sun.COM } 36629396SMatthew.Ahrens@Sun.COM 36639396SMatthew.Ahrens@Sun.COM int 36649396SMatthew.Ahrens@Sun.COM zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 36659396SMatthew.Ahrens@Sun.COM zfs_userspace_cb_t func, void *arg) 36669396SMatthew.Ahrens@Sun.COM { 36679396SMatthew.Ahrens@Sun.COM zfs_cmd_t zc = { 0 }; 36689396SMatthew.Ahrens@Sun.COM int error; 36699396SMatthew.Ahrens@Sun.COM zfs_useracct_t buf[100]; 36709396SMatthew.Ahrens@Sun.COM 36719396SMatthew.Ahrens@Sun.COM (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 36729396SMatthew.Ahrens@Sun.COM 36739396SMatthew.Ahrens@Sun.COM zc.zc_objset_type = type; 36749396SMatthew.Ahrens@Sun.COM zc.zc_nvlist_dst = (uintptr_t)buf; 36759396SMatthew.Ahrens@Sun.COM 36769396SMatthew.Ahrens@Sun.COM /* CONSTCOND */ 36779396SMatthew.Ahrens@Sun.COM while (1) { 36789396SMatthew.Ahrens@Sun.COM zfs_useracct_t *zua = buf; 36799396SMatthew.Ahrens@Sun.COM 36809396SMatthew.Ahrens@Sun.COM zc.zc_nvlist_dst_size = sizeof (buf); 36819396SMatthew.Ahrens@Sun.COM error = ioctl(zhp->zfs_hdl->libzfs_fd, 36829396SMatthew.Ahrens@Sun.COM ZFS_IOC_USERSPACE_MANY, &zc); 36839396SMatthew.Ahrens@Sun.COM if (error || zc.zc_nvlist_dst_size == 0) 36849396SMatthew.Ahrens@Sun.COM break; 36859396SMatthew.Ahrens@Sun.COM 36869396SMatthew.Ahrens@Sun.COM while (zc.zc_nvlist_dst_size > 0) { 36879554SMatthew.Ahrens@Sun.COM error = func(arg, zua->zu_domain, zua->zu_rid, 36889554SMatthew.Ahrens@Sun.COM zua->zu_space); 36899554SMatthew.Ahrens@Sun.COM if (error != 0) 36909554SMatthew.Ahrens@Sun.COM return (error); 36919396SMatthew.Ahrens@Sun.COM zua++; 36929396SMatthew.Ahrens@Sun.COM zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 36939396SMatthew.Ahrens@Sun.COM } 36949396SMatthew.Ahrens@Sun.COM } 36959396SMatthew.Ahrens@Sun.COM 36969396SMatthew.Ahrens@Sun.COM return (error); 36979396SMatthew.Ahrens@Sun.COM } 369810242Schris.kirby@sun.com 369910242Schris.kirby@sun.com int 370010242Schris.kirby@sun.com zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 370110342Schris.kirby@sun.com boolean_t recursive, boolean_t temphold) 370210242Schris.kirby@sun.com { 370310242Schris.kirby@sun.com zfs_cmd_t zc = { 0 }; 370410242Schris.kirby@sun.com libzfs_handle_t *hdl = zhp->zfs_hdl; 370510242Schris.kirby@sun.com 370610242Schris.kirby@sun.com (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 370710242Schris.kirby@sun.com (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 370810342Schris.kirby@sun.com if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string)) 370910342Schris.kirby@sun.com >= sizeof (zc.zc_string)) 371010342Schris.kirby@sun.com return (zfs_error(hdl, EZFS_TAGTOOLONG, tag)); 371110242Schris.kirby@sun.com zc.zc_cookie = recursive; 371210342Schris.kirby@sun.com zc.zc_temphold = temphold; 371310242Schris.kirby@sun.com 371410242Schris.kirby@sun.com if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) { 371510242Schris.kirby@sun.com char errbuf[ZFS_MAXNAMELEN+32]; 371610242Schris.kirby@sun.com 371710242Schris.kirby@sun.com /* 371810242Schris.kirby@sun.com * if it was recursive, the one that actually failed will be in 371910242Schris.kirby@sun.com * zc.zc_name. 372010242Schris.kirby@sun.com */ 372110242Schris.kirby@sun.com (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 372210242Schris.kirby@sun.com "cannot hold '%s@%s'"), zc.zc_name, snapname); 372310242Schris.kirby@sun.com switch (errno) { 372410951SChris.Kirby@sun.com case E2BIG: 372510951SChris.Kirby@sun.com /* 372610951SChris.Kirby@sun.com * Temporary tags wind up having the ds object id 372710951SChris.Kirby@sun.com * prepended. So even if we passed the length check 372810951SChris.Kirby@sun.com * above, it's still possible for the tag to wind 372910951SChris.Kirby@sun.com * up being slightly too long. 373010951SChris.Kirby@sun.com */ 373110951SChris.Kirby@sun.com return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf)); 373210242Schris.kirby@sun.com case ENOTSUP: 373310242Schris.kirby@sun.com zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 373410242Schris.kirby@sun.com "pool must be upgraded")); 373510242Schris.kirby@sun.com return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 373610242Schris.kirby@sun.com case EINVAL: 373710242Schris.kirby@sun.com return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 373810242Schris.kirby@sun.com case EEXIST: 373910242Schris.kirby@sun.com return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf)); 374010242Schris.kirby@sun.com default: 374110242Schris.kirby@sun.com return (zfs_standard_error_fmt(hdl, errno, errbuf)); 374210242Schris.kirby@sun.com } 374310242Schris.kirby@sun.com } 374410242Schris.kirby@sun.com 374510242Schris.kirby@sun.com return (0); 374610242Schris.kirby@sun.com } 374710242Schris.kirby@sun.com 374810342Schris.kirby@sun.com struct hold_range_arg { 374910342Schris.kirby@sun.com zfs_handle_t *origin; 375010342Schris.kirby@sun.com const char *fromsnap; 375110342Schris.kirby@sun.com const char *tosnap; 375210342Schris.kirby@sun.com char lastsnapheld[ZFS_MAXNAMELEN]; 375310342Schris.kirby@sun.com const char *tag; 375410342Schris.kirby@sun.com boolean_t temphold; 375510342Schris.kirby@sun.com boolean_t seento; 375610342Schris.kirby@sun.com boolean_t seenfrom; 375710342Schris.kirby@sun.com boolean_t holding; 375810342Schris.kirby@sun.com }; 375910342Schris.kirby@sun.com 376010342Schris.kirby@sun.com static int 376110342Schris.kirby@sun.com zfs_hold_range_one(zfs_handle_t *zhp, void *arg) 376210342Schris.kirby@sun.com { 376310342Schris.kirby@sun.com struct hold_range_arg *hra = arg; 376410342Schris.kirby@sun.com const char *thissnap; 376510342Schris.kirby@sun.com int error; 376610342Schris.kirby@sun.com 376710342Schris.kirby@sun.com thissnap = strchr(zfs_get_name(zhp), '@') + 1; 376810342Schris.kirby@sun.com 376910342Schris.kirby@sun.com if (hra->fromsnap && !hra->seenfrom && 377010342Schris.kirby@sun.com strcmp(hra->fromsnap, thissnap) == 0) 377110342Schris.kirby@sun.com hra->seenfrom = B_TRUE; 377210342Schris.kirby@sun.com 377310342Schris.kirby@sun.com /* snap is older or newer than the desired range, ignore it */ 377410342Schris.kirby@sun.com if (hra->seento || !hra->seenfrom) { 377510342Schris.kirby@sun.com zfs_close(zhp); 377610342Schris.kirby@sun.com return (0); 377710342Schris.kirby@sun.com } 377810342Schris.kirby@sun.com 377910342Schris.kirby@sun.com if (hra->holding) { 378010342Schris.kirby@sun.com error = zfs_hold(hra->origin, thissnap, hra->tag, B_FALSE, 378110342Schris.kirby@sun.com hra->temphold); 378210342Schris.kirby@sun.com if (error == 0) { 378310342Schris.kirby@sun.com (void) strlcpy(hra->lastsnapheld, zfs_get_name(zhp), 378410342Schris.kirby@sun.com sizeof (hra->lastsnapheld)); 378510342Schris.kirby@sun.com } 378610342Schris.kirby@sun.com } else { 378710342Schris.kirby@sun.com error = zfs_release(hra->origin, thissnap, hra->tag, B_FALSE); 378810342Schris.kirby@sun.com } 378910342Schris.kirby@sun.com 379010342Schris.kirby@sun.com if (!hra->seento && strcmp(hra->tosnap, thissnap) == 0) 379110342Schris.kirby@sun.com hra->seento = B_TRUE; 379210342Schris.kirby@sun.com 379310342Schris.kirby@sun.com zfs_close(zhp); 379410342Schris.kirby@sun.com return (error); 379510342Schris.kirby@sun.com } 379610342Schris.kirby@sun.com 379710342Schris.kirby@sun.com /* 379810342Schris.kirby@sun.com * Add a user hold on the set of snapshots starting with fromsnap up to 379910342Schris.kirby@sun.com * and including tosnap. If we're unable to to acquire a particular hold, 380010342Schris.kirby@sun.com * undo any holds up to that point. 380110342Schris.kirby@sun.com */ 380210342Schris.kirby@sun.com int 380310342Schris.kirby@sun.com zfs_hold_range(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, 380410342Schris.kirby@sun.com const char *tag, boolean_t temphold) 380510342Schris.kirby@sun.com { 380610342Schris.kirby@sun.com struct hold_range_arg arg = { 0 }; 380710342Schris.kirby@sun.com int error; 380810342Schris.kirby@sun.com 380910342Schris.kirby@sun.com arg.origin = zhp; 381010342Schris.kirby@sun.com arg.fromsnap = fromsnap; 381110342Schris.kirby@sun.com arg.tosnap = tosnap; 381210342Schris.kirby@sun.com arg.tag = tag; 381310342Schris.kirby@sun.com arg.temphold = temphold; 381410342Schris.kirby@sun.com arg.holding = B_TRUE; 381510342Schris.kirby@sun.com 381610342Schris.kirby@sun.com error = zfs_iter_snapshots_sorted(zhp, zfs_hold_range_one, &arg); 381710342Schris.kirby@sun.com 381810342Schris.kirby@sun.com /* 381910342Schris.kirby@sun.com * Make sure we either hold the entire range or none. 382010342Schris.kirby@sun.com */ 382110342Schris.kirby@sun.com if (error && arg.lastsnapheld[0] != '\0') { 382210342Schris.kirby@sun.com (void) zfs_release_range(zhp, fromsnap, 382310342Schris.kirby@sun.com (const char *)arg.lastsnapheld, tag); 382410342Schris.kirby@sun.com } 382510342Schris.kirby@sun.com return (error); 382610342Schris.kirby@sun.com } 382710342Schris.kirby@sun.com 382810242Schris.kirby@sun.com int 382910242Schris.kirby@sun.com zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 383010242Schris.kirby@sun.com boolean_t recursive) 383110242Schris.kirby@sun.com { 383210242Schris.kirby@sun.com zfs_cmd_t zc = { 0 }; 383310242Schris.kirby@sun.com libzfs_handle_t *hdl = zhp->zfs_hdl; 383410242Schris.kirby@sun.com 383510242Schris.kirby@sun.com (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 383610242Schris.kirby@sun.com (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 383710342Schris.kirby@sun.com if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string)) 383810342Schris.kirby@sun.com >= sizeof (zc.zc_string)) 383910342Schris.kirby@sun.com return (zfs_error(hdl, EZFS_TAGTOOLONG, tag)); 384010242Schris.kirby@sun.com zc.zc_cookie = recursive; 384110242Schris.kirby@sun.com 384210242Schris.kirby@sun.com if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) { 384310242Schris.kirby@sun.com char errbuf[ZFS_MAXNAMELEN+32]; 384410242Schris.kirby@sun.com 384510242Schris.kirby@sun.com /* 384610242Schris.kirby@sun.com * if it was recursive, the one that actually failed will be in 384710242Schris.kirby@sun.com * zc.zc_name. 384810242Schris.kirby@sun.com */ 384910242Schris.kirby@sun.com (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 385010242Schris.kirby@sun.com "cannot release '%s@%s'"), zc.zc_name, snapname); 385110242Schris.kirby@sun.com switch (errno) { 385210242Schris.kirby@sun.com case ESRCH: 385310242Schris.kirby@sun.com return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf)); 385410242Schris.kirby@sun.com case ENOTSUP: 385510242Schris.kirby@sun.com zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 385610242Schris.kirby@sun.com "pool must be upgraded")); 385710242Schris.kirby@sun.com return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 385810242Schris.kirby@sun.com case EINVAL: 385910242Schris.kirby@sun.com return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 386010242Schris.kirby@sun.com default: 386110242Schris.kirby@sun.com return (zfs_standard_error_fmt(hdl, errno, errbuf)); 386210242Schris.kirby@sun.com } 386310242Schris.kirby@sun.com } 386410242Schris.kirby@sun.com 386510242Schris.kirby@sun.com return (0); 386610242Schris.kirby@sun.com } 386710342Schris.kirby@sun.com 386810342Schris.kirby@sun.com /* 386910342Schris.kirby@sun.com * Release a user hold from the set of snapshots starting with fromsnap 387010342Schris.kirby@sun.com * up to and including tosnap. 387110342Schris.kirby@sun.com */ 387210342Schris.kirby@sun.com int 387310342Schris.kirby@sun.com zfs_release_range(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap, 387410342Schris.kirby@sun.com const char *tag) 387510342Schris.kirby@sun.com { 387610342Schris.kirby@sun.com struct hold_range_arg arg = { 0 }; 387710342Schris.kirby@sun.com 387810342Schris.kirby@sun.com arg.origin = zhp; 387910342Schris.kirby@sun.com arg.fromsnap = fromsnap; 388010342Schris.kirby@sun.com arg.tosnap = tosnap; 388110342Schris.kirby@sun.com arg.tag = tag; 388210342Schris.kirby@sun.com 388310342Schris.kirby@sun.com return (zfs_iter_snapshots_sorted(zhp, zfs_hold_range_one, &arg)); 388410342Schris.kirby@sun.com } 3885