1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 213126Sahl 22789Sahrens /* 235977Smarks * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #include <assert.h> 28789Sahrens #include <ctype.h> 29789Sahrens #include <errno.h> 30789Sahrens #include <libdevinfo.h> 31789Sahrens #include <libintl.h> 32789Sahrens #include <math.h> 33789Sahrens #include <stdio.h> 34789Sahrens #include <stdlib.h> 35789Sahrens #include <strings.h> 36789Sahrens #include <unistd.h> 375367Sahrens #include <stddef.h> 38789Sahrens #include <zone.h> 392082Seschrock #include <fcntl.h> 40789Sahrens #include <sys/mntent.h> 411294Slling #include <sys/mount.h> 424543Smarks #include <sys/avl.h> 434543Smarks #include <priv.h> 444543Smarks #include <pwd.h> 454543Smarks #include <grp.h> 464543Smarks #include <stddef.h> 474543Smarks #include <ucred.h> 48789Sahrens 49789Sahrens #include <sys/spa.h> 502676Seschrock #include <sys/zap.h> 51789Sahrens #include <libzfs.h> 52789Sahrens 53789Sahrens #include "zfs_namecheck.h" 54789Sahrens #include "zfs_prop.h" 55789Sahrens #include "libzfs_impl.h" 564543Smarks #include "zfs_deleg.h" 57789Sahrens 584007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 594007Smmusante 60789Sahrens /* 61789Sahrens * Given a single type (not a mask of types), return the type in a human 62789Sahrens * readable form. 63789Sahrens */ 64789Sahrens const char * 65789Sahrens zfs_type_to_name(zfs_type_t type) 66789Sahrens { 67789Sahrens switch (type) { 68789Sahrens case ZFS_TYPE_FILESYSTEM: 69789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 70789Sahrens case ZFS_TYPE_SNAPSHOT: 71789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 72789Sahrens case ZFS_TYPE_VOLUME: 73789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 74789Sahrens } 75789Sahrens 76789Sahrens return (NULL); 77789Sahrens } 78789Sahrens 79789Sahrens /* 80789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 81789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 82789Sahrens * We guess what the type would have been based on the path and the mask of 83789Sahrens * acceptable types. 84789Sahrens */ 85789Sahrens static const char * 86789Sahrens path_to_str(const char *path, int types) 87789Sahrens { 88789Sahrens /* 89789Sahrens * When given a single type, always report the exact type. 90789Sahrens */ 91789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 92789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 93789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 94789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 95789Sahrens if (types == ZFS_TYPE_VOLUME) 96789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 97789Sahrens 98789Sahrens /* 99789Sahrens * The user is requesting more than one type of dataset. If this is the 100789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 101789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 102789Sahrens * snapshot attribute and try again. 103789Sahrens */ 104789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 105789Sahrens if (strchr(path, '@') != NULL) 106789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 107789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 108789Sahrens } 109789Sahrens 110789Sahrens /* 111789Sahrens * The user has requested either filesystems or volumes. 112789Sahrens * We have no way of knowing a priori what type this would be, so always 113789Sahrens * report it as "filesystem" or "volume", our two primitive types. 114789Sahrens */ 115789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 116789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 117789Sahrens 118789Sahrens assert(types & ZFS_TYPE_VOLUME); 119789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 120789Sahrens } 121789Sahrens 122789Sahrens /* 123789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 124789Sahrens * provide a more meaningful error message. We place a more useful message in 125789Sahrens * 'buf' detailing exactly why the name was not valid. 126789Sahrens */ 127789Sahrens static int 1285326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 1295326Sek110237 boolean_t modifying) 130789Sahrens { 131789Sahrens namecheck_err_t why; 132789Sahrens char what; 133789Sahrens 134789Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 1352082Seschrock if (hdl != NULL) { 136789Sahrens switch (why) { 1371003Slling case NAME_ERR_TOOLONG: 1382082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1392082Seschrock "name is too long")); 1401003Slling break; 1411003Slling 142789Sahrens case NAME_ERR_LEADING_SLASH: 1432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1442082Seschrock "leading slash in name")); 145789Sahrens break; 146789Sahrens 147789Sahrens case NAME_ERR_EMPTY_COMPONENT: 1482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1492082Seschrock "empty component in name")); 150789Sahrens break; 151789Sahrens 152789Sahrens case NAME_ERR_TRAILING_SLASH: 1532082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1542082Seschrock "trailing slash in name")); 155789Sahrens break; 156789Sahrens 157789Sahrens case NAME_ERR_INVALCHAR: 1582082Seschrock zfs_error_aux(hdl, 159789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 1602082Seschrock "'%c' in name"), what); 161789Sahrens break; 162789Sahrens 163789Sahrens case NAME_ERR_MULTIPLE_AT: 1642082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1652082Seschrock "multiple '@' delimiters in name")); 166789Sahrens break; 1672856Snd150628 1682856Snd150628 case NAME_ERR_NOLETTER: 1692856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1702856Snd150628 "pool doesn't begin with a letter")); 1712856Snd150628 break; 1722856Snd150628 1732856Snd150628 case NAME_ERR_RESERVED: 1742856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1752856Snd150628 "name is reserved")); 1762856Snd150628 break; 1772856Snd150628 1782856Snd150628 case NAME_ERR_DISKLIKE: 1792856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1802856Snd150628 "reserved disk name")); 1812856Snd150628 break; 182789Sahrens } 183789Sahrens } 184789Sahrens 185789Sahrens return (0); 186789Sahrens } 187789Sahrens 188789Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 1892082Seschrock if (hdl != NULL) 1902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1912082Seschrock "snapshot delimiter '@' in filesystem name")); 192789Sahrens return (0); 193789Sahrens } 194789Sahrens 1952199Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 1962199Sahrens if (hdl != NULL) 1972199Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1983413Smmusante "missing '@' delimiter in snapshot name")); 1992199Sahrens return (0); 2002199Sahrens } 2012199Sahrens 2025326Sek110237 if (modifying && strchr(path, '%') != NULL) { 2035326Sek110237 if (hdl != NULL) 2045326Sek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2055326Sek110237 "invalid character %c in name"), '%'); 2065326Sek110237 return (0); 2075326Sek110237 } 2085326Sek110237 2092082Seschrock return (-1); 210789Sahrens } 211789Sahrens 212789Sahrens int 213789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 214789Sahrens { 2156423Sgw25295 if (type == ZFS_TYPE_POOL) 2166423Sgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 2175326Sek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 218789Sahrens } 219789Sahrens 220789Sahrens /* 2212676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2222676Seschrock * properties into a separate nvlist. 2232676Seschrock */ 2244217Seschrock static nvlist_t * 2254217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2262676Seschrock { 2272676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2282676Seschrock nvpair_t *elem; 2292676Seschrock nvlist_t *propval; 2304217Seschrock nvlist_t *nvl; 2314217Seschrock 2324217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2334217Seschrock (void) no_memory(hdl); 2344217Seschrock return (NULL); 2354217Seschrock } 2362676Seschrock 2372676Seschrock elem = NULL; 2384217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2392676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2402676Seschrock continue; 2412676Seschrock 2422676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2434217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2444217Seschrock nvlist_free(nvl); 2454217Seschrock (void) no_memory(hdl); 2464217Seschrock return (NULL); 2474217Seschrock } 2482676Seschrock } 2492676Seschrock 2504217Seschrock return (nvl); 2512676Seschrock } 2522676Seschrock 2536865Srm160521 static zpool_handle_t * 2546865Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 2556865Srm160521 { 2566865Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 2576865Srm160521 zpool_handle_t *zph; 2586865Srm160521 2596865Srm160521 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 2606865Srm160521 if (hdl->libzfs_pool_handles != NULL) 2616865Srm160521 zph->zpool_next = hdl->libzfs_pool_handles; 2626865Srm160521 hdl->libzfs_pool_handles = zph; 2636865Srm160521 } 2646865Srm160521 return (zph); 2656865Srm160521 } 2666865Srm160521 2676865Srm160521 static zpool_handle_t * 2686865Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 2696865Srm160521 { 2706865Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 2716865Srm160521 zpool_handle_t *zph = hdl->libzfs_pool_handles; 2726865Srm160521 2736865Srm160521 while ((zph != NULL) && 2746865Srm160521 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 2756865Srm160521 zph = zph->zpool_next; 2766865Srm160521 return (zph); 2776865Srm160521 } 2786865Srm160521 2796865Srm160521 /* 2806865Srm160521 * Returns a handle to the pool that contains the provided dataset. 2816865Srm160521 * If a handle to that pool already exists then that handle is returned. 2826865Srm160521 * Otherwise, a new handle is created and added to the list of handles. 2836865Srm160521 */ 2846865Srm160521 static zpool_handle_t * 2856865Srm160521 zpool_handle(zfs_handle_t *zhp) 2866865Srm160521 { 2876865Srm160521 char *pool_name; 2886865Srm160521 int len; 2896865Srm160521 zpool_handle_t *zph; 2906865Srm160521 2916865Srm160521 len = strcspn(zhp->zfs_name, "/@") + 1; 2926865Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, len); 2936865Srm160521 (void) strlcpy(pool_name, zhp->zfs_name, len); 2946865Srm160521 2956865Srm160521 zph = zpool_find_handle(zhp, pool_name, len); 2966865Srm160521 if (zph == NULL) 2976865Srm160521 zph = zpool_add_handle(zhp, pool_name); 2986865Srm160521 2996865Srm160521 free(pool_name); 3006865Srm160521 return (zph); 3016865Srm160521 } 3026865Srm160521 3036865Srm160521 void 3046865Srm160521 zpool_free_handles(libzfs_handle_t *hdl) 3056865Srm160521 { 3066865Srm160521 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 3076865Srm160521 3086865Srm160521 while (zph != NULL) { 3096865Srm160521 next = zph->zpool_next; 3106865Srm160521 zpool_close(zph); 3116865Srm160521 zph = next; 3126865Srm160521 } 3136865Srm160521 hdl->libzfs_pool_handles = NULL; 3146865Srm160521 } 3156865Srm160521 3162676Seschrock /* 317789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 318789Sahrens */ 319789Sahrens static int 320*8228SEric.Taylor@Sun.COM get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 321789Sahrens { 3222676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 323*8228SEric.Taylor@Sun.COM 324*8228SEric.Taylor@Sun.COM (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 325*8228SEric.Taylor@Sun.COM 326*8228SEric.Taylor@Sun.COM while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3271356Seschrock if (errno == ENOMEM) { 328*8228SEric.Taylor@Sun.COM if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 3292082Seschrock return (-1); 3302676Seschrock } 3311356Seschrock } else { 3321356Seschrock return (-1); 3331356Seschrock } 3341356Seschrock } 335*8228SEric.Taylor@Sun.COM return (0); 336*8228SEric.Taylor@Sun.COM } 337*8228SEric.Taylor@Sun.COM 338*8228SEric.Taylor@Sun.COM static int 339*8228SEric.Taylor@Sun.COM put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 340*8228SEric.Taylor@Sun.COM { 341*8228SEric.Taylor@Sun.COM nvlist_t *allprops, *userprops; 342*8228SEric.Taylor@Sun.COM 343*8228SEric.Taylor@Sun.COM zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 344*8228SEric.Taylor@Sun.COM 345*8228SEric.Taylor@Sun.COM if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 3462082Seschrock return (-1); 3472082Seschrock } 348789Sahrens 3494217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 3504217Seschrock nvlist_free(allprops); 3512676Seschrock return (-1); 3524217Seschrock } 3534217Seschrock 3544217Seschrock nvlist_free(zhp->zfs_props); 3554217Seschrock nvlist_free(zhp->zfs_user_props); 3564217Seschrock 3574217Seschrock zhp->zfs_props = allprops; 3584217Seschrock zhp->zfs_user_props = userprops; 3592082Seschrock 360789Sahrens return (0); 361789Sahrens } 362789Sahrens 363*8228SEric.Taylor@Sun.COM static int 364*8228SEric.Taylor@Sun.COM get_stats(zfs_handle_t *zhp) 365*8228SEric.Taylor@Sun.COM { 366*8228SEric.Taylor@Sun.COM int rc = 0; 367*8228SEric.Taylor@Sun.COM zfs_cmd_t zc = { 0 }; 368*8228SEric.Taylor@Sun.COM 369*8228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 370*8228SEric.Taylor@Sun.COM return (-1); 371*8228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, &zc) != 0) 372*8228SEric.Taylor@Sun.COM rc = -1; 373*8228SEric.Taylor@Sun.COM else if (put_stats_zhdl(zhp, &zc) != 0) 374*8228SEric.Taylor@Sun.COM rc = -1; 375*8228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 376*8228SEric.Taylor@Sun.COM return (rc); 377*8228SEric.Taylor@Sun.COM } 378*8228SEric.Taylor@Sun.COM 379789Sahrens /* 380789Sahrens * Refresh the properties currently stored in the handle. 381789Sahrens */ 382789Sahrens void 383789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 384789Sahrens { 385789Sahrens (void) get_stats(zhp); 386789Sahrens } 387789Sahrens 388789Sahrens /* 389789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 390789Sahrens * zfs_iter_* to create child handles on the fly. 391789Sahrens */ 392*8228SEric.Taylor@Sun.COM static int 393*8228SEric.Taylor@Sun.COM make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 394789Sahrens { 3954543Smarks char *logstr; 396*8228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 397789Sahrens 3984543Smarks /* 3994543Smarks * Preserve history log string. 4004543Smarks * any changes performed here will be 4014543Smarks * logged as an internal event. 4024543Smarks */ 4034543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 4044543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 405*8228SEric.Taylor@Sun.COM 4061758Sahrens top: 407*8228SEric.Taylor@Sun.COM if (put_stats_zhdl(zhp, zc) != 0) { 4084543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 409*8228SEric.Taylor@Sun.COM return (-1); 410789Sahrens } 411789Sahrens 412*8228SEric.Taylor@Sun.COM 4131758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 414*8228SEric.Taylor@Sun.COM zfs_cmd_t zc2 = { 0 }; 4151758Sahrens 4161758Sahrens /* 4171758Sahrens * If it is dds_inconsistent, then we've caught it in 4181758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 4191758Sahrens * it is inconsistent from the ZPL's point of view, so 4201758Sahrens * can't be mounted. However, it could also be that we 4211758Sahrens * have crashed in the middle of one of those 4221758Sahrens * operations, in which case we need to get rid of the 4231758Sahrens * inconsistent state. We do that by either rolling 4241758Sahrens * back to the previous snapshot (which will fail if 4251758Sahrens * there is none), or destroying the filesystem. Note 4261758Sahrens * that if we are still in the middle of an active 4271758Sahrens * 'receive' or 'destroy', then the rollback and destroy 4281758Sahrens * will fail with EBUSY and we will drive on as usual. 4291758Sahrens */ 4301758Sahrens 431*8228SEric.Taylor@Sun.COM (void) strlcpy(zc2.zc_name, zhp->zfs_name, 432*8228SEric.Taylor@Sun.COM sizeof (zc2.zc_name)); 4331758Sahrens 4342885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 4352082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 436*8228SEric.Taylor@Sun.COM zc2.zc_objset_type = DMU_OST_ZVOL; 4371758Sahrens } else { 438*8228SEric.Taylor@Sun.COM zc2.zc_objset_type = DMU_OST_ZFS; 4391758Sahrens } 4401758Sahrens 4411758Sahrens /* 4425331Samw * If we can successfully destroy it, pretend that it 4431758Sahrens * never existed. 4441758Sahrens */ 445*8228SEric.Taylor@Sun.COM if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) { 4464543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 4471758Sahrens errno = ENOENT; 448*8228SEric.Taylor@Sun.COM return (-1); 4491758Sahrens } 450*8228SEric.Taylor@Sun.COM /* If we can successfully roll it back, reset the stats */ 451*8228SEric.Taylor@Sun.COM if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) { 452*8228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, zc) != 0) { 453*8228SEric.Taylor@Sun.COM zhp->zfs_hdl->libzfs_log_str = logstr; 454*8228SEric.Taylor@Sun.COM return (-1); 455*8228SEric.Taylor@Sun.COM } 4565367Sahrens goto top; 457*8228SEric.Taylor@Sun.COM } 4581758Sahrens } 4591758Sahrens 460789Sahrens /* 461789Sahrens * We've managed to open the dataset and gather statistics. Determine 462789Sahrens * the high-level type. 463789Sahrens */ 4642885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 4652885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 4662885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 4672885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 4682885Sahrens else 4692885Sahrens abort(); 4702885Sahrens 471789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 472789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 473789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 474789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 475789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 476789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 477789Sahrens else 4782082Seschrock abort(); /* we should never see any other types */ 479789Sahrens 4804543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 4816865Srm160521 zhp->zpool_hdl = zpool_handle(zhp); 482*8228SEric.Taylor@Sun.COM return (0); 483*8228SEric.Taylor@Sun.COM } 484*8228SEric.Taylor@Sun.COM 485*8228SEric.Taylor@Sun.COM zfs_handle_t * 486*8228SEric.Taylor@Sun.COM make_dataset_handle(libzfs_handle_t *hdl, const char *path) 487*8228SEric.Taylor@Sun.COM { 488*8228SEric.Taylor@Sun.COM zfs_cmd_t zc = { 0 }; 489*8228SEric.Taylor@Sun.COM 490*8228SEric.Taylor@Sun.COM zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 491*8228SEric.Taylor@Sun.COM 492*8228SEric.Taylor@Sun.COM if (zhp == NULL) 493*8228SEric.Taylor@Sun.COM return (NULL); 494*8228SEric.Taylor@Sun.COM 495*8228SEric.Taylor@Sun.COM zhp->zfs_hdl = hdl; 496*8228SEric.Taylor@Sun.COM (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 497*8228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 498*8228SEric.Taylor@Sun.COM free(zhp); 499*8228SEric.Taylor@Sun.COM return (NULL); 500*8228SEric.Taylor@Sun.COM } 501*8228SEric.Taylor@Sun.COM if (get_stats_ioctl(zhp, &zc) == -1) { 502*8228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 503*8228SEric.Taylor@Sun.COM free(zhp); 504*8228SEric.Taylor@Sun.COM return (NULL); 505*8228SEric.Taylor@Sun.COM } 506*8228SEric.Taylor@Sun.COM if (make_dataset_handle_common(zhp, &zc) == -1) { 507*8228SEric.Taylor@Sun.COM free(zhp); 508*8228SEric.Taylor@Sun.COM zhp = NULL; 509*8228SEric.Taylor@Sun.COM } 510*8228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 511*8228SEric.Taylor@Sun.COM return (zhp); 512*8228SEric.Taylor@Sun.COM } 513*8228SEric.Taylor@Sun.COM 514*8228SEric.Taylor@Sun.COM static zfs_handle_t * 515*8228SEric.Taylor@Sun.COM make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 516*8228SEric.Taylor@Sun.COM { 517*8228SEric.Taylor@Sun.COM zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 518*8228SEric.Taylor@Sun.COM 519*8228SEric.Taylor@Sun.COM if (zhp == NULL) 520*8228SEric.Taylor@Sun.COM return (NULL); 521*8228SEric.Taylor@Sun.COM 522*8228SEric.Taylor@Sun.COM zhp->zfs_hdl = hdl; 523*8228SEric.Taylor@Sun.COM (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 524*8228SEric.Taylor@Sun.COM if (make_dataset_handle_common(zhp, zc) == -1) { 525*8228SEric.Taylor@Sun.COM free(zhp); 526*8228SEric.Taylor@Sun.COM return (NULL); 527*8228SEric.Taylor@Sun.COM } 528789Sahrens return (zhp); 529789Sahrens } 530789Sahrens 531789Sahrens /* 532789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 533789Sahrens * argument is a mask of acceptable types. The function will print an 534789Sahrens * appropriate error message and return NULL if it can't be opened. 535789Sahrens */ 536789Sahrens zfs_handle_t * 5372082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 538789Sahrens { 539789Sahrens zfs_handle_t *zhp; 5402082Seschrock char errbuf[1024]; 5412082Seschrock 5422082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 5432082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 544789Sahrens 545789Sahrens /* 5462082Seschrock * Validate the name before we even try to open it. 547789Sahrens */ 5485326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 5492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5502082Seschrock "invalid dataset name")); 5512082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 552789Sahrens return (NULL); 553789Sahrens } 554789Sahrens 555789Sahrens /* 556789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 557789Sahrens */ 558789Sahrens errno = 0; 5592082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 5603237Slling (void) zfs_standard_error(hdl, errno, errbuf); 561789Sahrens return (NULL); 562789Sahrens } 563789Sahrens 564789Sahrens if (!(types & zhp->zfs_type)) { 5652082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 5662142Seschrock zfs_close(zhp); 567789Sahrens return (NULL); 568789Sahrens } 569789Sahrens 570789Sahrens return (zhp); 571789Sahrens } 572789Sahrens 573789Sahrens /* 574789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 575789Sahrens */ 576789Sahrens void 577789Sahrens zfs_close(zfs_handle_t *zhp) 578789Sahrens { 579789Sahrens if (zhp->zfs_mntopts) 580789Sahrens free(zhp->zfs_mntopts); 5812676Seschrock nvlist_free(zhp->zfs_props); 5822676Seschrock nvlist_free(zhp->zfs_user_props); 583789Sahrens free(zhp); 584789Sahrens } 585789Sahrens 586*8228SEric.Taylor@Sun.COM typedef struct mnttab_node { 587*8228SEric.Taylor@Sun.COM struct mnttab mtn_mt; 588*8228SEric.Taylor@Sun.COM avl_node_t mtn_node; 589*8228SEric.Taylor@Sun.COM } mnttab_node_t; 590*8228SEric.Taylor@Sun.COM 591*8228SEric.Taylor@Sun.COM static int 592*8228SEric.Taylor@Sun.COM libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 593*8228SEric.Taylor@Sun.COM { 594*8228SEric.Taylor@Sun.COM const mnttab_node_t *mtn1 = arg1; 595*8228SEric.Taylor@Sun.COM const mnttab_node_t *mtn2 = arg2; 596*8228SEric.Taylor@Sun.COM int rv; 597*8228SEric.Taylor@Sun.COM 598*8228SEric.Taylor@Sun.COM rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 599*8228SEric.Taylor@Sun.COM 600*8228SEric.Taylor@Sun.COM if (rv == 0) 601*8228SEric.Taylor@Sun.COM return (0); 602*8228SEric.Taylor@Sun.COM return (rv > 0 ? 1 : -1); 603*8228SEric.Taylor@Sun.COM } 604*8228SEric.Taylor@Sun.COM 605*8228SEric.Taylor@Sun.COM void 606*8228SEric.Taylor@Sun.COM libzfs_mnttab_init(libzfs_handle_t *hdl) 607*8228SEric.Taylor@Sun.COM { 608*8228SEric.Taylor@Sun.COM struct mnttab entry; 609*8228SEric.Taylor@Sun.COM 610*8228SEric.Taylor@Sun.COM assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 611*8228SEric.Taylor@Sun.COM avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 612*8228SEric.Taylor@Sun.COM sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 613*8228SEric.Taylor@Sun.COM 614*8228SEric.Taylor@Sun.COM rewind(hdl->libzfs_mnttab); 615*8228SEric.Taylor@Sun.COM while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 616*8228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 617*8228SEric.Taylor@Sun.COM 618*8228SEric.Taylor@Sun.COM if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 619*8228SEric.Taylor@Sun.COM continue; 620*8228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 621*8228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 622*8228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 623*8228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 624*8228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 625*8228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 626*8228SEric.Taylor@Sun.COM } 627*8228SEric.Taylor@Sun.COM } 628*8228SEric.Taylor@Sun.COM 629*8228SEric.Taylor@Sun.COM void 630*8228SEric.Taylor@Sun.COM libzfs_mnttab_fini(libzfs_handle_t *hdl) 631*8228SEric.Taylor@Sun.COM { 632*8228SEric.Taylor@Sun.COM void *cookie = NULL; 633*8228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 634*8228SEric.Taylor@Sun.COM 635*8228SEric.Taylor@Sun.COM while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) { 636*8228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_special); 637*8228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mountp); 638*8228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_fstype); 639*8228SEric.Taylor@Sun.COM free(mtn->mtn_mt.mnt_mntopts); 640*8228SEric.Taylor@Sun.COM free(mtn); 641*8228SEric.Taylor@Sun.COM } 642*8228SEric.Taylor@Sun.COM avl_destroy(&hdl->libzfs_mnttab_cache); 643*8228SEric.Taylor@Sun.COM } 644*8228SEric.Taylor@Sun.COM 645*8228SEric.Taylor@Sun.COM int 646*8228SEric.Taylor@Sun.COM libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 647*8228SEric.Taylor@Sun.COM struct mnttab *entry) 648*8228SEric.Taylor@Sun.COM { 649*8228SEric.Taylor@Sun.COM mnttab_node_t find; 650*8228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 651*8228SEric.Taylor@Sun.COM 652*8228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 653*8228SEric.Taylor@Sun.COM libzfs_mnttab_init(hdl); 654*8228SEric.Taylor@Sun.COM 655*8228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 656*8228SEric.Taylor@Sun.COM mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 657*8228SEric.Taylor@Sun.COM if (mtn) { 658*8228SEric.Taylor@Sun.COM *entry = mtn->mtn_mt; 659*8228SEric.Taylor@Sun.COM return (0); 660*8228SEric.Taylor@Sun.COM } 661*8228SEric.Taylor@Sun.COM return (ENOENT); 662*8228SEric.Taylor@Sun.COM } 663*8228SEric.Taylor@Sun.COM 664*8228SEric.Taylor@Sun.COM void 665*8228SEric.Taylor@Sun.COM libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 666*8228SEric.Taylor@Sun.COM const char *mountp, const char *mntopts) 667*8228SEric.Taylor@Sun.COM { 668*8228SEric.Taylor@Sun.COM mnttab_node_t *mtn; 669*8228SEric.Taylor@Sun.COM 670*8228SEric.Taylor@Sun.COM if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 671*8228SEric.Taylor@Sun.COM return; 672*8228SEric.Taylor@Sun.COM mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 673*8228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 674*8228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 675*8228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 676*8228SEric.Taylor@Sun.COM mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 677*8228SEric.Taylor@Sun.COM avl_add(&hdl->libzfs_mnttab_cache, mtn); 678*8228SEric.Taylor@Sun.COM } 679*8228SEric.Taylor@Sun.COM 680*8228SEric.Taylor@Sun.COM void 681*8228SEric.Taylor@Sun.COM libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 682*8228SEric.Taylor@Sun.COM { 683*8228SEric.Taylor@Sun.COM mnttab_node_t find; 684*8228SEric.Taylor@Sun.COM mnttab_node_t *ret; 685*8228SEric.Taylor@Sun.COM 686*8228SEric.Taylor@Sun.COM find.mtn_mt.mnt_special = (char *)fsname; 687*8228SEric.Taylor@Sun.COM if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) { 688*8228SEric.Taylor@Sun.COM avl_remove(&hdl->libzfs_mnttab_cache, ret); 689*8228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_special); 690*8228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mountp); 691*8228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_fstype); 692*8228SEric.Taylor@Sun.COM free(ret->mtn_mt.mnt_mntopts); 693*8228SEric.Taylor@Sun.COM free(ret); 694*8228SEric.Taylor@Sun.COM } 695*8228SEric.Taylor@Sun.COM } 696*8228SEric.Taylor@Sun.COM 6975713Srm160521 int 6985713Srm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 6995713Srm160521 { 7006865Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 7016865Srm160521 7025713Srm160521 if (zpool_handle == NULL) 7035713Srm160521 return (-1); 7045713Srm160521 7055713Srm160521 *spa_version = zpool_get_prop_int(zpool_handle, 7065713Srm160521 ZPOOL_PROP_VERSION, NULL); 7075713Srm160521 return (0); 7085713Srm160521 } 7095713Srm160521 7105713Srm160521 /* 7115713Srm160521 * The choice of reservation property depends on the SPA version. 7125713Srm160521 */ 7135713Srm160521 static int 7145713Srm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 7155713Srm160521 { 7165713Srm160521 int spa_version; 7175713Srm160521 7185713Srm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 7195713Srm160521 return (-1); 7205713Srm160521 7215713Srm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 7225713Srm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 7235713Srm160521 else 7245713Srm160521 *resv_prop = ZFS_PROP_RESERVATION; 7255713Srm160521 7265713Srm160521 return (0); 7275713Srm160521 } 7285713Srm160521 7293912Slling /* 7302676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 7312676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 7322676Seschrock * strings. 733789Sahrens */ 7347184Stimh nvlist_t * 7357184Stimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 7365094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 737789Sahrens { 7382676Seschrock nvpair_t *elem; 7392676Seschrock uint64_t intval; 7402676Seschrock char *strval; 7415094Slling zfs_prop_t prop; 7422676Seschrock nvlist_t *ret; 7435331Samw int chosen_normal = -1; 7445331Samw int chosen_utf = -1; 7452676Seschrock 7465094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 7475094Slling (void) no_memory(hdl); 7485094Slling return (NULL); 749789Sahrens } 750789Sahrens 7512676Seschrock elem = NULL; 7522676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 7535094Slling const char *propname = nvpair_name(elem); 7542676Seschrock 7552676Seschrock /* 7562676Seschrock * Make sure this property is valid and applies to this type. 7572676Seschrock */ 7585094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 7595094Slling if (!zfs_prop_user(propname)) { 7602676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7615094Slling "invalid property '%s'"), propname); 7625094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7635094Slling goto error; 7645094Slling } 7655094Slling 7665094Slling /* 7675094Slling * If this is a user property, make sure it's a 7685094Slling * string, and that it's less than ZAP_MAXNAMELEN. 7695094Slling */ 7705094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 7715094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7725094Slling "'%s' must be a string"), propname); 7735094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7745094Slling goto error; 7755094Slling } 7765094Slling 7775094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 7785094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7795094Slling "property name '%s' is too long"), 7802676Seschrock propname); 7812676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 7822676Seschrock goto error; 7832676Seschrock } 7842676Seschrock 7852676Seschrock (void) nvpair_value_string(elem, &strval); 7862676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 7872676Seschrock (void) no_memory(hdl); 7882676Seschrock goto error; 7892676Seschrock } 7902676Seschrock continue; 791789Sahrens } 7922676Seschrock 7937265Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 7947265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7957265Sahrens "this property can not be modified for snapshots")); 7967265Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 7977265Sahrens goto error; 7987265Sahrens } 7997265Sahrens 8002676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 8012676Seschrock zfs_error_aux(hdl, 8022676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 8032676Seschrock "apply to datasets of this type"), propname); 8042676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 8052676Seschrock goto error; 8062676Seschrock } 8072676Seschrock 8082676Seschrock if (zfs_prop_readonly(prop) && 8095331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 8102676Seschrock zfs_error_aux(hdl, 8112676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 8122676Seschrock propname); 8132676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 8142676Seschrock goto error; 8152676Seschrock } 8162676Seschrock 8175094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 8185094Slling &strval, &intval, errbuf) != 0) 8192676Seschrock goto error; 8202676Seschrock 8212676Seschrock /* 8222676Seschrock * Perform some additional checks for specific properties. 8232676Seschrock */ 8242676Seschrock switch (prop) { 8254577Sahrens case ZFS_PROP_VERSION: 8264577Sahrens { 8274577Sahrens int version; 8284577Sahrens 8294577Sahrens if (zhp == NULL) 8304577Sahrens break; 8314577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 8324577Sahrens if (intval < version) { 8334577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8344577Sahrens "Can not downgrade; already at version %u"), 8354577Sahrens version); 8364577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8374577Sahrens goto error; 8384577Sahrens } 8394577Sahrens break; 8404577Sahrens } 8414577Sahrens 8422676Seschrock case ZFS_PROP_RECORDSIZE: 8432676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 8442676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 8452676Seschrock if (intval < SPA_MINBLOCKSIZE || 8462676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 8472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8482676Seschrock "'%s' must be power of 2 from %u " 8492676Seschrock "to %uk"), propname, 8502676Seschrock (uint_t)SPA_MINBLOCKSIZE, 8512676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 8522676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8532676Seschrock goto error; 854789Sahrens } 855789Sahrens break; 856789Sahrens 8573126Sahl case ZFS_PROP_SHAREISCSI: 8583126Sahl if (strcmp(strval, "off") != 0 && 8593126Sahl strcmp(strval, "on") != 0 && 8603126Sahl strcmp(strval, "type=disk") != 0) { 8613126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8623126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 8633126Sahl propname); 8643126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8653126Sahl goto error; 8663126Sahl } 8673126Sahl 8683126Sahl break; 8693126Sahl 8702676Seschrock case ZFS_PROP_MOUNTPOINT: 8714778Srm160521 { 8724778Srm160521 namecheck_err_t why; 8734778Srm160521 8742676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 8752676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 8762676Seschrock break; 8772676Seschrock 8784778Srm160521 if (mountpoint_namecheck(strval, &why)) { 8794778Srm160521 switch (why) { 8804778Srm160521 case NAME_ERR_LEADING_SLASH: 8814778Srm160521 zfs_error_aux(hdl, 8824778Srm160521 dgettext(TEXT_DOMAIN, 8834778Srm160521 "'%s' must be an absolute path, " 8844778Srm160521 "'none', or 'legacy'"), propname); 8854778Srm160521 break; 8864778Srm160521 case NAME_ERR_TOOLONG: 8874778Srm160521 zfs_error_aux(hdl, 8884778Srm160521 dgettext(TEXT_DOMAIN, 8894778Srm160521 "component of '%s' is too long"), 8904778Srm160521 propname); 8914778Srm160521 break; 8924778Srm160521 } 8932676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8942676Seschrock goto error; 895789Sahrens } 8964778Srm160521 } 8974778Srm160521 8983126Sahl /*FALLTHRU*/ 8993126Sahl 9005331Samw case ZFS_PROP_SHARESMB: 9013126Sahl case ZFS_PROP_SHARENFS: 9023126Sahl /* 9035331Samw * For the mountpoint and sharenfs or sharesmb 9045331Samw * properties, check if it can be set in a 9055331Samw * global/non-global zone based on 9063126Sahl * the zoned property value: 9073126Sahl * 9083126Sahl * global zone non-global zone 9093126Sahl * -------------------------------------------------- 9103126Sahl * zoned=on mountpoint (no) mountpoint (yes) 9113126Sahl * sharenfs (no) sharenfs (no) 9125331Samw * sharesmb (no) sharesmb (no) 9133126Sahl * 9143126Sahl * zoned=off mountpoint (yes) N/A 9153126Sahl * sharenfs (yes) 9165331Samw * sharesmb (yes) 9173126Sahl */ 9182676Seschrock if (zoned) { 9192676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 9202676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9212676Seschrock "'%s' cannot be set on " 9222676Seschrock "dataset in a non-global zone"), 9232676Seschrock propname); 9242676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9252676Seschrock errbuf); 9262676Seschrock goto error; 9275331Samw } else if (prop == ZFS_PROP_SHARENFS || 9285331Samw prop == ZFS_PROP_SHARESMB) { 9292676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9302676Seschrock "'%s' cannot be set in " 9312676Seschrock "a non-global zone"), propname); 9322676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 9332676Seschrock errbuf); 9342676Seschrock goto error; 9352676Seschrock } 9362676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 9372676Seschrock /* 9382676Seschrock * If zoned property is 'off', this must be in 9392676Seschrock * a globle zone. If not, something is wrong. 9402676Seschrock */ 9412676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9422676Seschrock "'%s' cannot be set while dataset " 9432676Seschrock "'zoned' property is set"), propname); 9442676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 9452676Seschrock goto error; 9462676Seschrock } 9473126Sahl 9484180Sdougm /* 9494180Sdougm * At this point, it is legitimate to set the 9504180Sdougm * property. Now we want to make sure that the 9514180Sdougm * property value is valid if it is sharenfs. 9524180Sdougm */ 9535331Samw if ((prop == ZFS_PROP_SHARENFS || 9545331Samw prop == ZFS_PROP_SHARESMB) && 9554217Seschrock strcmp(strval, "on") != 0 && 9564217Seschrock strcmp(strval, "off") != 0) { 9575331Samw zfs_share_proto_t proto; 9585331Samw 9595331Samw if (prop == ZFS_PROP_SHARESMB) 9605331Samw proto = PROTO_SMB; 9615331Samw else 9625331Samw proto = PROTO_NFS; 9634180Sdougm 9644180Sdougm /* 9655331Samw * Must be an valid sharing protocol 9665331Samw * option string so init the libshare 9675331Samw * in order to enable the parser and 9685331Samw * then parse the options. We use the 9695331Samw * control API since we don't care about 9705331Samw * the current configuration and don't 9714180Sdougm * want the overhead of loading it 9724180Sdougm * until we actually do something. 9734180Sdougm */ 9744180Sdougm 9754217Seschrock if (zfs_init_libshare(hdl, 9764217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 9774217Seschrock /* 9784217Seschrock * An error occurred so we can't do 9794217Seschrock * anything 9804217Seschrock */ 9814217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9824217Seschrock "'%s' cannot be set: problem " 9834217Seschrock "in share initialization"), 9844217Seschrock propname); 9854217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 9864217Seschrock errbuf); 9874217Seschrock goto error; 9884217Seschrock } 9894217Seschrock 9905331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 9914217Seschrock /* 9924217Seschrock * There was an error in parsing so 9934217Seschrock * deal with it by issuing an error 9944217Seschrock * message and leaving after 9954217Seschrock * uninitializing the the libshare 9964217Seschrock * interface. 9974217Seschrock */ 9984217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 9994217Seschrock "'%s' cannot be set to invalid " 10004217Seschrock "options"), propname); 10014217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10024217Seschrock errbuf); 10034217Seschrock zfs_uninit_libshare(hdl); 10044217Seschrock goto error; 10054217Seschrock } 10064180Sdougm zfs_uninit_libshare(hdl); 10074180Sdougm } 10084180Sdougm 10093126Sahl break; 10105331Samw case ZFS_PROP_UTF8ONLY: 10115331Samw chosen_utf = (int)intval; 10125331Samw break; 10135331Samw case ZFS_PROP_NORMALIZE: 10145331Samw chosen_normal = (int)intval; 10155331Samw break; 10162676Seschrock } 10172676Seschrock 10182676Seschrock /* 10192676Seschrock * For changes to existing volumes, we have some additional 10202676Seschrock * checks to enforce. 10212676Seschrock */ 10222676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 10232676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 10242676Seschrock ZFS_PROP_VOLSIZE); 10252676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 10262676Seschrock ZFS_PROP_VOLBLOCKSIZE); 10272676Seschrock char buf[64]; 10282676Seschrock 10292676Seschrock switch (prop) { 10302676Seschrock case ZFS_PROP_RESERVATION: 10315378Sck153898 case ZFS_PROP_REFRESERVATION: 10322676Seschrock if (intval > volsize) { 10332676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10342676Seschrock "'%s' is greater than current " 10352676Seschrock "volume size"), propname); 10362676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10372676Seschrock errbuf); 10382676Seschrock goto error; 10392676Seschrock } 10402676Seschrock break; 10412676Seschrock 10422676Seschrock case ZFS_PROP_VOLSIZE: 10432676Seschrock if (intval % blocksize != 0) { 10442676Seschrock zfs_nicenum(blocksize, buf, 10452676Seschrock sizeof (buf)); 10462676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10472676Seschrock "'%s' must be a multiple of " 10482676Seschrock "volume block size (%s)"), 10492676Seschrock propname, buf); 10502676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10512676Seschrock errbuf); 10522676Seschrock goto error; 10532676Seschrock } 10542676Seschrock 10552676Seschrock if (intval == 0) { 10562676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10572676Seschrock "'%s' cannot be zero"), 10582676Seschrock propname); 10592676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 10602676Seschrock errbuf); 10612676Seschrock goto error; 1062789Sahrens } 10633126Sahl break; 1064789Sahrens } 1065789Sahrens } 1066789Sahrens } 1067789Sahrens 10682676Seschrock /* 10695331Samw * If normalization was chosen, but no UTF8 choice was made, 10705331Samw * enforce rejection of non-UTF8 names. 10715331Samw * 10725331Samw * If normalization was chosen, but rejecting non-UTF8 names 10735331Samw * was explicitly not chosen, it is an error. 10745331Samw */ 10755498Stimh if (chosen_normal > 0 && chosen_utf < 0) { 10765331Samw if (nvlist_add_uint64(ret, 10775331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 10785331Samw (void) no_memory(hdl); 10795331Samw goto error; 10805331Samw } 10815498Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 10825331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10835331Samw "'%s' must be set 'on' if normalization chosen"), 10845331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 10855331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 10865331Samw goto error; 10875331Samw } 10885331Samw 10895331Samw /* 10902676Seschrock * If this is an existing volume, and someone is setting the volsize, 10912676Seschrock * make sure that it matches the reservation, or add it if necessary. 10922676Seschrock */ 10932676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 10942676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 10952676Seschrock &intval) == 0) { 10962676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 10972676Seschrock ZFS_PROP_VOLSIZE); 10985481Sck153898 uint64_t old_reservation; 10992676Seschrock uint64_t new_reservation; 11005481Sck153898 zfs_prop_t resv_prop; 11015713Srm160521 11025713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 11035481Sck153898 goto error; 11045481Sck153898 old_reservation = zfs_prop_get_int(zhp, resv_prop); 11052676Seschrock 11062676Seschrock if (old_volsize == old_reservation && 11075481Sck153898 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop), 11082676Seschrock &new_reservation) != 0) { 11092676Seschrock if (nvlist_add_uint64(ret, 11105481Sck153898 zfs_prop_to_name(resv_prop), intval) != 0) { 11112676Seschrock (void) no_memory(hdl); 11122676Seschrock goto error; 11132676Seschrock } 11142676Seschrock } 11152676Seschrock } 11162676Seschrock return (ret); 11172676Seschrock 11182676Seschrock error: 11192676Seschrock nvlist_free(ret); 11202676Seschrock return (NULL); 1121789Sahrens } 1122789Sahrens 11234543Smarks static int 11244543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 11254543Smarks uint64_t *ret_who) 11264543Smarks { 11274543Smarks struct passwd *pwd; 11284543Smarks struct group *grp; 11294543Smarks uid_t id; 11304543Smarks 11314543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 11324543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 11334543Smarks *ret_who = -1; 11344543Smarks return (0); 11354543Smarks } 11364543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 11374543Smarks return (EZFS_BADWHO); 11384543Smarks 11394543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 11404543Smarks strcmp(who, "everyone") == 0) { 11414543Smarks *ret_who = -1; 11424543Smarks *who_type = ZFS_DELEG_EVERYONE; 11434543Smarks return (0); 11444543Smarks } 11454543Smarks 11464543Smarks pwd = getpwnam(who); 11474543Smarks grp = getgrnam(who); 11484543Smarks 11494543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 11504543Smarks *ret_who = pwd->pw_uid; 11514543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 11524543Smarks *ret_who = grp->gr_gid; 11534543Smarks } else if (pwd) { 11544543Smarks *ret_who = pwd->pw_uid; 11554543Smarks *who_type = ZFS_DELEG_USER; 11564543Smarks } else if (grp) { 11574543Smarks *ret_who = grp->gr_gid; 11584543Smarks *who_type = ZFS_DELEG_GROUP; 11594543Smarks } else { 11604543Smarks char *end; 11614543Smarks 11624543Smarks id = strtol(who, &end, 10); 11634543Smarks if (errno != 0 || *end != '\0') { 11644543Smarks return (EZFS_BADWHO); 11654543Smarks } else { 11664543Smarks *ret_who = id; 11674543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 11684543Smarks *who_type = ZFS_DELEG_USER; 11694543Smarks } 11704543Smarks } 11714543Smarks 11724543Smarks return (0); 11734543Smarks } 11744543Smarks 11754543Smarks static void 11764543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 11774543Smarks { 11784543Smarks if (perms_nvp != NULL) { 11794543Smarks verify(nvlist_add_nvlist(who_nvp, 11804543Smarks name, perms_nvp) == 0); 11814543Smarks } else { 11824543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 11834543Smarks } 11844543Smarks } 11854543Smarks 11864543Smarks static void 11874543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 11884543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 11894543Smarks nvlist_t *sets_nvp) 11904543Smarks { 11914543Smarks boolean_t do_perms, do_sets; 11924543Smarks char name[ZFS_MAX_DELEG_NAME]; 11934543Smarks 11944543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 11954543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 11964543Smarks 11974543Smarks if (!do_perms && !do_sets) 11984543Smarks do_perms = do_sets = B_TRUE; 11994543Smarks 12004543Smarks if (do_perms) { 12014543Smarks zfs_deleg_whokey(name, who_type, inherit, 12024543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 12034543Smarks whostr : (void *)&whoid); 12044543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 12054543Smarks } 12064543Smarks if (do_sets) { 12074543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 12084543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 12094543Smarks whostr : (void *)&whoid); 12104543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 12114543Smarks } 12124543Smarks } 12134543Smarks 12144543Smarks static void 12154543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 12164543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 12174543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 12184543Smarks { 12194543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 12204543Smarks helper(who_type, whoid, whostr, 0, 12214543Smarks who_nvp, perms_nvp, sets_nvp); 12224543Smarks } else { 12234543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 12244543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 12254543Smarks who_nvp, perms_nvp, sets_nvp); 12264543Smarks } 12274543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 12284543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 12294543Smarks who_nvp, perms_nvp, sets_nvp); 12304543Smarks } 12314543Smarks } 12324543Smarks } 12334543Smarks 12344543Smarks /* 12354543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 12364543Smarks * 12374543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 12384543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 12394543Smarks * base attribute named stored in the dsl. 12404543Smarks * Arguments: 12414543Smarks * 12424543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 12434543Smarks * whostr may be null for everyone or create perms. 12444543Smarks * who_type: is the type of entry in whostr. Typically this will be 12454543Smarks * ZFS_DELEG_WHO_UNKNOWN. 12465331Samw * perms: common separated list of permissions. May be null if user 12474543Smarks * is requested to remove permissions by who. 12484543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 12494543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 12504543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 12514543Smarks * The output nvp will look something like this. 12524543Smarks * ul$1234 -> {create ; destroy } 12534543Smarks * Ul$1234 -> { @myset } 12544543Smarks * s-$@myset - { snapshot; checksum; compression } 12554543Smarks */ 12564543Smarks int 12574543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 12584543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 12594543Smarks { 12604543Smarks nvlist_t *who_nvp; 12614543Smarks nvlist_t *perms_nvp = NULL; 12624543Smarks nvlist_t *sets_nvp = NULL; 12634543Smarks char errbuf[1024]; 12644787Sahrens char *who_tok, *perm; 12654543Smarks int error; 12664543Smarks 12674543Smarks *nvp = NULL; 12684543Smarks 12694543Smarks if (perms) { 12704543Smarks if ((error = nvlist_alloc(&perms_nvp, 12714543Smarks NV_UNIQUE_NAME, 0)) != 0) { 12724543Smarks return (1); 12734543Smarks } 12744543Smarks if ((error = nvlist_alloc(&sets_nvp, 12754543Smarks NV_UNIQUE_NAME, 0)) != 0) { 12764543Smarks nvlist_free(perms_nvp); 12774543Smarks return (1); 12784543Smarks } 12794543Smarks } 12804543Smarks 12814543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 12824543Smarks if (perms_nvp) 12834543Smarks nvlist_free(perms_nvp); 12844543Smarks if (sets_nvp) 12854543Smarks nvlist_free(sets_nvp); 12864543Smarks return (1); 12874543Smarks } 12884543Smarks 12894543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 12904543Smarks namecheck_err_t why; 12914543Smarks char what; 12924543Smarks 12934543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 12944787Sahrens nvlist_free(who_nvp); 12954787Sahrens if (perms_nvp) 12964787Sahrens nvlist_free(perms_nvp); 12974787Sahrens if (sets_nvp) 12984787Sahrens nvlist_free(sets_nvp); 12994787Sahrens 13004543Smarks switch (why) { 13014543Smarks case NAME_ERR_NO_AT: 13024543Smarks zfs_error_aux(zhp->zfs_hdl, 13034543Smarks dgettext(TEXT_DOMAIN, 13044543Smarks "set definition must begin with an '@' " 13054543Smarks "character")); 13064543Smarks } 13074543Smarks return (zfs_error(zhp->zfs_hdl, 13084543Smarks EZFS_BADPERMSET, whostr)); 13094543Smarks } 13104543Smarks } 13114543Smarks 13124543Smarks /* 13134543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 13144543Smarks * The first nvlist perms_nvp will have normal permissions and the 13154543Smarks * other sets_nvp will have only permssion set names in it. 13164543Smarks */ 13174787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 13184787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 13194787Sahrens 13204787Sahrens if (perm_canonical) { 13214787Sahrens verify(nvlist_add_boolean(perms_nvp, 13224787Sahrens perm_canonical) == 0); 13234787Sahrens } else if (perm[0] == '@') { 13244787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 13254787Sahrens } else { 13264787Sahrens nvlist_free(who_nvp); 13274787Sahrens nvlist_free(perms_nvp); 13284787Sahrens nvlist_free(sets_nvp); 13294787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 13304543Smarks } 13314543Smarks } 13324543Smarks 13334543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 13344543Smarks who_tok = strtok(whostr, ","); 13354543Smarks if (who_tok == NULL) { 13364543Smarks nvlist_free(who_nvp); 13374787Sahrens if (perms_nvp) 13384787Sahrens nvlist_free(perms_nvp); 13394543Smarks if (sets_nvp) 13404543Smarks nvlist_free(sets_nvp); 13414543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13424543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 13434543Smarks whostr); 13444543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 13454543Smarks } 13464543Smarks } 13474543Smarks 13484543Smarks /* 13494543Smarks * Now create the nvlist(s) 13504543Smarks */ 13514543Smarks do { 13524543Smarks uint64_t who_id; 13534543Smarks 13544543Smarks error = zfs_get_perm_who(who_tok, &who_type, 13554543Smarks &who_id); 13564543Smarks if (error) { 13574543Smarks nvlist_free(who_nvp); 13584787Sahrens if (perms_nvp) 13594787Sahrens nvlist_free(perms_nvp); 13604543Smarks if (sets_nvp) 13614543Smarks nvlist_free(sets_nvp); 13624543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13634543Smarks dgettext(TEXT_DOMAIN, 13644543Smarks "Unable to determine uid/gid for " 13654543Smarks "%s "), who_tok); 13664543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 13674543Smarks } 13684543Smarks 13694543Smarks /* 13704543Smarks * add entries for both local and descendent when required 13714543Smarks */ 13724543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 13734543Smarks perms_nvp, sets_nvp, who_type, inherit); 13744543Smarks 13754543Smarks } while (who_tok = strtok(NULL, ",")); 13764543Smarks *nvp = who_nvp; 13774543Smarks return (0); 13784543Smarks } 13794543Smarks 13804543Smarks static int 13814543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 13824543Smarks { 13834543Smarks zfs_cmd_t zc = { 0 }; 13844543Smarks int error; 13854543Smarks char errbuf[1024]; 13864543Smarks 13874543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13884543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 13894543Smarks zhp->zfs_name); 13904543Smarks 13915094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 13924543Smarks return (-1); 13934543Smarks 13944543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13954543Smarks zc.zc_perm_action = unset; 13964543Smarks 13974543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 13984543Smarks if (error && errno == ENOTSUP) { 13994543Smarks (void) snprintf(errbuf, sizeof (errbuf), 14004543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 14014543Smarks zcmd_free_nvlists(&zc); 14024543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 14034543Smarks } else if (error) { 14044543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 14054543Smarks } 14064543Smarks zcmd_free_nvlists(&zc); 14074543Smarks 14084543Smarks return (error); 14094543Smarks } 14104543Smarks 14114543Smarks int 14124543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 14134543Smarks { 14144543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 14154543Smarks } 14164543Smarks 14174543Smarks int 14184543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 14194543Smarks { 14204543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 14214543Smarks } 14224543Smarks 14234543Smarks static int 14244543Smarks perm_compare(const void *arg1, const void *arg2) 14254543Smarks { 14264543Smarks const zfs_perm_node_t *node1 = arg1; 14274543Smarks const zfs_perm_node_t *node2 = arg2; 14284543Smarks int ret; 14294543Smarks 14304543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 14314543Smarks 14324543Smarks if (ret > 0) 14334543Smarks return (1); 14344543Smarks if (ret < 0) 14354543Smarks return (-1); 14364543Smarks else 14374543Smarks return (0); 14384543Smarks } 14394543Smarks 14404543Smarks static void 14414543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 14424543Smarks { 14434543Smarks zfs_perm_node_t *permnode; 14445367Sahrens void *cookie = NULL; 14455367Sahrens 14465367Sahrens while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 14474543Smarks free(permnode); 14485367Sahrens avl_destroy(tree); 14494543Smarks } 14504543Smarks 14514543Smarks static void 14524543Smarks zfs_destroy_tree(avl_tree_t *tree) 14534543Smarks { 14544543Smarks zfs_allow_node_t *allownode; 14555367Sahrens void *cookie = NULL; 14565367Sahrens 14574543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 14584543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 14594543Smarks zfs_destroy_perm_tree(&allownode->z_local); 14604543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 14614543Smarks free(allownode); 14624543Smarks } 14635367Sahrens avl_destroy(tree); 14644543Smarks } 14654543Smarks 14664543Smarks void 14674543Smarks zfs_free_allows(zfs_allow_t *allow) 14684543Smarks { 14694543Smarks zfs_allow_t *allownext; 14704543Smarks zfs_allow_t *freeallow; 14714543Smarks 14724543Smarks allownext = allow; 14734543Smarks while (allownext) { 14744543Smarks zfs_destroy_tree(&allownext->z_sets); 14754543Smarks zfs_destroy_tree(&allownext->z_crperms); 14764543Smarks zfs_destroy_tree(&allownext->z_user); 14774543Smarks zfs_destroy_tree(&allownext->z_group); 14784543Smarks zfs_destroy_tree(&allownext->z_everyone); 14794543Smarks freeallow = allownext; 14804543Smarks allownext = allownext->z_next; 14814543Smarks free(freeallow); 14824543Smarks } 14834543Smarks } 14844543Smarks 14854543Smarks static zfs_allow_t * 14864543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 14874543Smarks { 14884543Smarks zfs_allow_t *ptree; 14894543Smarks 14904543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 14914543Smarks sizeof (zfs_allow_t))) == NULL) { 14924543Smarks return (NULL); 14934543Smarks } 14944543Smarks 14954543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 14964543Smarks avl_create(&ptree->z_sets, 14974543Smarks perm_compare, sizeof (zfs_allow_node_t), 14984543Smarks offsetof(zfs_allow_node_t, z_node)); 14994543Smarks avl_create(&ptree->z_crperms, 15004543Smarks perm_compare, sizeof (zfs_allow_node_t), 15014543Smarks offsetof(zfs_allow_node_t, z_node)); 15024543Smarks avl_create(&ptree->z_user, 15034543Smarks perm_compare, sizeof (zfs_allow_node_t), 15044543Smarks offsetof(zfs_allow_node_t, z_node)); 15054543Smarks avl_create(&ptree->z_group, 15064543Smarks perm_compare, sizeof (zfs_allow_node_t), 15074543Smarks offsetof(zfs_allow_node_t, z_node)); 15084543Smarks avl_create(&ptree->z_everyone, 15094543Smarks perm_compare, sizeof (zfs_allow_node_t), 15104543Smarks offsetof(zfs_allow_node_t, z_node)); 15114543Smarks 15124543Smarks if (prev) 15134543Smarks prev->z_next = ptree; 15144543Smarks ptree->z_next = NULL; 15154543Smarks return (ptree); 15164543Smarks } 15174543Smarks 15184543Smarks /* 15194543Smarks * Add permissions to the appropriate AVL permission tree. 15204543Smarks * The appropriate tree may not be the requested tree. 15214543Smarks * For example if ld indicates a local permission, but 15224543Smarks * same permission also exists as a descendent permission 15234543Smarks * then the permission will be removed from the descendent 15244543Smarks * tree and add the the local+descendent tree. 15254543Smarks */ 15264543Smarks static int 15274543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 15284543Smarks char *perm, char ld) 15294543Smarks { 15304543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 15314543Smarks zfs_perm_node_t *newnode; 15324543Smarks avl_index_t where, where2; 15334543Smarks avl_tree_t *tree, *altree; 15344543Smarks 15354543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 15364543Smarks 15374543Smarks if (ld == ZFS_DELEG_NA) { 15384543Smarks tree = &allownode->z_localdescend; 15394543Smarks altree = &allownode->z_descend; 15404543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 15414543Smarks tree = &allownode->z_local; 15424543Smarks altree = &allownode->z_descend; 15434543Smarks } else { 15444543Smarks tree = &allownode->z_descend; 15454543Smarks altree = &allownode->z_local; 15464543Smarks } 15474543Smarks permnode = avl_find(tree, &pnode, &where); 15484543Smarks permnode2 = avl_find(altree, &pnode, &where2); 15494543Smarks 15504543Smarks if (permnode2) { 15514543Smarks avl_remove(altree, permnode2); 15524543Smarks free(permnode2); 15534543Smarks if (permnode == NULL) { 15544543Smarks tree = &allownode->z_localdescend; 15554543Smarks } 15564543Smarks } 15574543Smarks 15584543Smarks /* 15594543Smarks * Now insert new permission in either requested location 15604543Smarks * local/descendent or into ld when perm will exist in both. 15614543Smarks */ 15624543Smarks if (permnode == NULL) { 15634543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 15644543Smarks sizeof (zfs_perm_node_t))) == NULL) { 15654543Smarks return (-1); 15664543Smarks } 15674543Smarks *newnode = pnode; 15684543Smarks avl_add(tree, newnode); 15694543Smarks } 15704543Smarks return (0); 15714543Smarks } 15724577Sahrens 15734543Smarks /* 15744543Smarks * Uggh, this is going to be a bit complicated. 15754543Smarks * we have an nvlist coming out of the kernel that 15764543Smarks * will indicate where the permission is set and then 15774543Smarks * it will contain allow of the various "who's", and what 15784543Smarks * their permissions are. To further complicate this 15794543Smarks * we will then have to coalesce the local,descendent 15804543Smarks * and local+descendent permissions where appropriate. 15814543Smarks * The kernel only knows about a permission as being local 15824543Smarks * or descendent, but not both. 15834543Smarks * 15844543Smarks * In order to make this easier for zfs_main to deal with 15854543Smarks * a series of AVL trees will be used to maintain 15864543Smarks * all of this, primarily for sorting purposes as well 15874543Smarks * as the ability to quickly locate a specific entry. 15884543Smarks * 15894543Smarks * What we end up with are tree's for sets, create perms, 15904543Smarks * user, groups and everyone. With each of those trees 15914543Smarks * we have subtrees for local, descendent and local+descendent 15924543Smarks * permissions. 15934543Smarks */ 15944543Smarks int 15954543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 15964543Smarks { 15974543Smarks zfs_cmd_t zc = { 0 }; 15984543Smarks int error; 15994543Smarks nvlist_t *nvlist; 16004543Smarks nvlist_t *permnv, *sourcenv; 16014543Smarks nvpair_t *who_pair, *source_pair; 16024543Smarks nvpair_t *perm_pair; 16034543Smarks char errbuf[1024]; 16044543Smarks zfs_allow_t *zallowp, *newallowp; 16054543Smarks char ld; 16064543Smarks char *nvpname; 16074543Smarks uid_t uid; 16084543Smarks gid_t gid; 16094543Smarks avl_tree_t *tree; 16104543Smarks avl_index_t where; 16114543Smarks 16124543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 16134543Smarks 16144543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 16154543Smarks return (-1); 16164543Smarks 16174543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 16184543Smarks if (errno == ENOMEM) { 16194543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 16204543Smarks zcmd_free_nvlists(&zc); 16214543Smarks return (-1); 16224543Smarks } 16234543Smarks } else if (errno == ENOTSUP) { 16244543Smarks zcmd_free_nvlists(&zc); 16254543Smarks (void) snprintf(errbuf, sizeof (errbuf), 16264543Smarks gettext("Pool must be upgraded to use 'allow'")); 16274543Smarks return (zfs_error(zhp->zfs_hdl, 16284543Smarks EZFS_BADVERSION, errbuf)); 16294543Smarks } else { 16304543Smarks zcmd_free_nvlists(&zc); 16314543Smarks return (-1); 16324543Smarks } 16334543Smarks } 16344543Smarks 16354543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 16364543Smarks zcmd_free_nvlists(&zc); 16374543Smarks return (-1); 16384543Smarks } 16394543Smarks 16404543Smarks zcmd_free_nvlists(&zc); 16414543Smarks 16424543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 16434543Smarks 16444543Smarks if (source_pair == NULL) { 16454543Smarks *zfs_perms = NULL; 16464543Smarks return (0); 16474543Smarks } 16484543Smarks 16494543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 16504543Smarks if (*zfs_perms == NULL) { 16514543Smarks return (0); 16524543Smarks } 16534543Smarks 16544543Smarks zallowp = *zfs_perms; 16554543Smarks 16564543Smarks for (;;) { 16574543Smarks struct passwd *pwd; 16584543Smarks struct group *grp; 16594543Smarks zfs_allow_node_t *allownode; 16604543Smarks zfs_allow_node_t findallownode; 16614543Smarks zfs_allow_node_t *newallownode; 16624543Smarks 16634543Smarks (void) strlcpy(zallowp->z_setpoint, 16644543Smarks nvpair_name(source_pair), 16654543Smarks sizeof (zallowp->z_setpoint)); 16664543Smarks 16674543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 16684543Smarks goto abort; 16694543Smarks 16704543Smarks /* 16714543Smarks * Make sure nvlist is composed correctly 16724543Smarks */ 16734543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 16744543Smarks goto abort; 16754543Smarks } 16764543Smarks 16774543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 16784543Smarks if (who_pair == NULL) { 16794543Smarks goto abort; 16804543Smarks } 16814543Smarks 16824543Smarks do { 16834543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 16844543Smarks if (error) { 16854543Smarks goto abort; 16864543Smarks } 16874543Smarks 16884543Smarks /* 16894543Smarks * First build up the key to use 16904543Smarks * for looking up in the various 16914543Smarks * who trees. 16924543Smarks */ 16934543Smarks ld = nvpair_name(who_pair)[1]; 16944543Smarks nvpname = nvpair_name(who_pair); 16954543Smarks switch (nvpair_name(who_pair)[0]) { 16964543Smarks case ZFS_DELEG_USER: 16974543Smarks case ZFS_DELEG_USER_SETS: 16984543Smarks tree = &zallowp->z_user; 16994543Smarks uid = atol(&nvpname[3]); 17004543Smarks pwd = getpwuid(uid); 17014543Smarks (void) snprintf(findallownode.z_key, 17024543Smarks sizeof (findallownode.z_key), "user %s", 17034543Smarks (pwd) ? pwd->pw_name : 17044543Smarks &nvpair_name(who_pair)[3]); 17054543Smarks break; 17064543Smarks case ZFS_DELEG_GROUP: 17074543Smarks case ZFS_DELEG_GROUP_SETS: 17084543Smarks tree = &zallowp->z_group; 17094543Smarks gid = atol(&nvpname[3]); 17104543Smarks grp = getgrgid(gid); 17114543Smarks (void) snprintf(findallownode.z_key, 17124543Smarks sizeof (findallownode.z_key), "group %s", 17134543Smarks (grp) ? grp->gr_name : 17144543Smarks &nvpair_name(who_pair)[3]); 17154543Smarks break; 17164543Smarks case ZFS_DELEG_CREATE: 17174543Smarks case ZFS_DELEG_CREATE_SETS: 17184543Smarks tree = &zallowp->z_crperms; 17194543Smarks (void) strlcpy(findallownode.z_key, "", 17204543Smarks sizeof (findallownode.z_key)); 17214543Smarks break; 17224543Smarks case ZFS_DELEG_EVERYONE: 17234543Smarks case ZFS_DELEG_EVERYONE_SETS: 17244543Smarks (void) snprintf(findallownode.z_key, 17254543Smarks sizeof (findallownode.z_key), "everyone"); 17264543Smarks tree = &zallowp->z_everyone; 17274543Smarks break; 17284543Smarks case ZFS_DELEG_NAMED_SET: 17294543Smarks case ZFS_DELEG_NAMED_SET_SETS: 17304543Smarks (void) snprintf(findallownode.z_key, 17314543Smarks sizeof (findallownode.z_key), "%s", 17324543Smarks &nvpair_name(who_pair)[3]); 17334543Smarks tree = &zallowp->z_sets; 17344543Smarks break; 17354543Smarks } 17364543Smarks 17374543Smarks /* 17384543Smarks * Place who in tree 17394543Smarks */ 17404543Smarks allownode = avl_find(tree, &findallownode, &where); 17414543Smarks if (allownode == NULL) { 17424543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 17434543Smarks sizeof (zfs_allow_node_t))) == NULL) { 17444543Smarks goto abort; 17454543Smarks } 17464543Smarks avl_create(&newallownode->z_localdescend, 17474543Smarks perm_compare, 17484543Smarks sizeof (zfs_perm_node_t), 17494543Smarks offsetof(zfs_perm_node_t, z_node)); 17504543Smarks avl_create(&newallownode->z_local, 17514543Smarks perm_compare, 17524543Smarks sizeof (zfs_perm_node_t), 17534543Smarks offsetof(zfs_perm_node_t, z_node)); 17544543Smarks avl_create(&newallownode->z_descend, 17554543Smarks perm_compare, 17564543Smarks sizeof (zfs_perm_node_t), 17574543Smarks offsetof(zfs_perm_node_t, z_node)); 17584543Smarks (void) strlcpy(newallownode->z_key, 17594543Smarks findallownode.z_key, 17604543Smarks sizeof (findallownode.z_key)); 17614543Smarks avl_insert(tree, newallownode, where); 17624543Smarks allownode = newallownode; 17634543Smarks } 17644543Smarks 17654543Smarks /* 17664543Smarks * Now iterate over the permissions and 17674543Smarks * place them in the appropriate local, 17684543Smarks * descendent or local+descendent tree. 17694543Smarks * 17704543Smarks * The permissions are added to the tree 17714543Smarks * via zfs_coalesce_perm(). 17724543Smarks */ 17734543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 17744543Smarks if (perm_pair == NULL) 17754543Smarks goto abort; 17764543Smarks do { 17774543Smarks if (zfs_coalesce_perm(zhp, allownode, 17784543Smarks nvpair_name(perm_pair), ld) != 0) 17794543Smarks goto abort; 17804543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 17814543Smarks perm_pair)); 17824543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 17834543Smarks 17844543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 17854543Smarks if (source_pair == NULL) 17864543Smarks break; 17874543Smarks 17884543Smarks /* 17894543Smarks * allocate another node from the link list of 17904543Smarks * zfs_allow_t structures 17914543Smarks */ 17924543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 17934543Smarks nvpair_name(source_pair)); 17944543Smarks if (newallowp == NULL) { 17954543Smarks goto abort; 17964543Smarks } 17974543Smarks zallowp = newallowp; 17984543Smarks } 17994543Smarks nvlist_free(nvlist); 18004543Smarks return (0); 18014543Smarks abort: 18024543Smarks zfs_free_allows(*zfs_perms); 18034543Smarks nvlist_free(nvlist); 18044543Smarks return (-1); 18054543Smarks } 18064543Smarks 18075993Smarks static char * 18085993Smarks zfs_deleg_perm_note(zfs_deleg_note_t note) 18095993Smarks { 18105993Smarks /* 18115993Smarks * Don't put newlines on end of lines 18125993Smarks */ 18135993Smarks switch (note) { 18145993Smarks case ZFS_DELEG_NOTE_CREATE: 18155993Smarks return (dgettext(TEXT_DOMAIN, 18165993Smarks "Must also have the 'mount' ability")); 18175993Smarks case ZFS_DELEG_NOTE_DESTROY: 18185993Smarks return (dgettext(TEXT_DOMAIN, 18195993Smarks "Must also have the 'mount' ability")); 18205993Smarks case ZFS_DELEG_NOTE_SNAPSHOT: 18215993Smarks return (dgettext(TEXT_DOMAIN, 18225993Smarks "Must also have the 'mount' ability")); 18235993Smarks case ZFS_DELEG_NOTE_ROLLBACK: 18245993Smarks return (dgettext(TEXT_DOMAIN, 18255993Smarks "Must also have the 'mount' ability")); 18265993Smarks case ZFS_DELEG_NOTE_CLONE: 18275993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'create' " 18285993Smarks "ability and 'mount'\n" 18295993Smarks "\t\t\t\tability in the origin file system")); 18305993Smarks case ZFS_DELEG_NOTE_PROMOTE: 18315993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'\n" 18325993Smarks "\t\t\t\tand 'promote' ability in the origin file system")); 18335993Smarks case ZFS_DELEG_NOTE_RENAME: 18345993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount' " 18355993Smarks "and 'create' \n\t\t\t\tability in the new parent")); 18365993Smarks case ZFS_DELEG_NOTE_RECEIVE: 18375993Smarks return (dgettext(TEXT_DOMAIN, "Must also have the 'mount'" 18385993Smarks " and 'create' ability")); 18395993Smarks case ZFS_DELEG_NOTE_USERPROP: 18405993Smarks return (dgettext(TEXT_DOMAIN, 18415993Smarks "Allows changing any user property")); 18425993Smarks case ZFS_DELEG_NOTE_ALLOW: 18435993Smarks return (dgettext(TEXT_DOMAIN, 18445993Smarks "Must also have the permission that is being\n" 18455993Smarks "\t\t\t\tallowed")); 18465993Smarks case ZFS_DELEG_NOTE_MOUNT: 18475993Smarks return (dgettext(TEXT_DOMAIN, 18485993Smarks "Allows mount/umount of ZFS datasets")); 18495993Smarks case ZFS_DELEG_NOTE_SHARE: 18505993Smarks return (dgettext(TEXT_DOMAIN, 18515993Smarks "Allows sharing file systems over NFS or SMB\n" 18525993Smarks "\t\t\t\tprotocols")); 18535993Smarks case ZFS_DELEG_NOTE_NONE: 18545993Smarks default: 18555993Smarks return (dgettext(TEXT_DOMAIN, "")); 18565993Smarks } 18575993Smarks } 18585993Smarks 18595993Smarks typedef enum { 18605993Smarks ZFS_DELEG_SUBCOMMAND, 18615993Smarks ZFS_DELEG_PROP, 18625993Smarks ZFS_DELEG_OTHER 18635993Smarks } zfs_deleg_perm_type_t; 18645993Smarks 18655993Smarks /* 18665993Smarks * is the permission a subcommand or other? 18675993Smarks */ 18685993Smarks zfs_deleg_perm_type_t 18695993Smarks zfs_deleg_perm_type(const char *perm) 18705993Smarks { 18715993Smarks if (strcmp(perm, "userprop") == 0) 18725993Smarks return (ZFS_DELEG_OTHER); 18735993Smarks else 18745993Smarks return (ZFS_DELEG_SUBCOMMAND); 18755993Smarks } 18765993Smarks 18775993Smarks static char * 18785993Smarks zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type) 18795993Smarks { 18805993Smarks switch (type) { 18815993Smarks case ZFS_DELEG_SUBCOMMAND: 18825993Smarks return (dgettext(TEXT_DOMAIN, "subcommand")); 18835993Smarks case ZFS_DELEG_PROP: 18845993Smarks return (dgettext(TEXT_DOMAIN, "property")); 18855993Smarks case ZFS_DELEG_OTHER: 18865993Smarks return (dgettext(TEXT_DOMAIN, "other")); 18875993Smarks } 18885993Smarks return (""); 18895993Smarks } 18905993Smarks 18915993Smarks /*ARGSUSED*/ 18925993Smarks static int 18935993Smarks zfs_deleg_prop_cb(int prop, void *cb) 18945993Smarks { 18955993Smarks if (zfs_prop_delegatable(prop)) 18965993Smarks (void) fprintf(stderr, "%-15s %-15s\n", zfs_prop_to_name(prop), 18975993Smarks zfs_deleg_perm_type_str(ZFS_DELEG_PROP)); 18985993Smarks 18995993Smarks return (ZPROP_CONT); 19005993Smarks } 19015993Smarks 19025993Smarks void 19035993Smarks zfs_deleg_permissions(void) 19045993Smarks { 19055993Smarks int i; 19065993Smarks 19075993Smarks (void) fprintf(stderr, "\n%-15s %-15s\t%s\n\n", "NAME", 19085993Smarks "TYPE", "NOTES"); 19095993Smarks 19105993Smarks /* 19115993Smarks * First print out the subcommands 19125993Smarks */ 19135993Smarks for (i = 0; zfs_deleg_perm_tab[i].z_perm != NULL; i++) { 19145993Smarks (void) fprintf(stderr, "%-15s %-15s\t%s\n", 19155993Smarks zfs_deleg_perm_tab[i].z_perm, 19165993Smarks zfs_deleg_perm_type_str( 19175993Smarks zfs_deleg_perm_type(zfs_deleg_perm_tab[i].z_perm)), 19185993Smarks zfs_deleg_perm_note(zfs_deleg_perm_tab[i].z_note)); 19195993Smarks } 19205993Smarks 19215993Smarks (void) zprop_iter(zfs_deleg_prop_cb, NULL, B_FALSE, B_TRUE, 19225993Smarks ZFS_TYPE_DATASET|ZFS_TYPE_VOLUME); 19235993Smarks } 19245993Smarks 1925789Sahrens /* 1926789Sahrens * Given a property name and value, set the property for the given dataset. 1927789Sahrens */ 1928789Sahrens int 19292676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1930789Sahrens { 1931789Sahrens zfs_cmd_t zc = { 0 }; 19322676Seschrock int ret = -1; 19332676Seschrock prop_changelist_t *cl = NULL; 19342082Seschrock char errbuf[1024]; 19352082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 19362676Seschrock nvlist_t *nvl = NULL, *realprops; 19372676Seschrock zfs_prop_t prop; 19387509SMark.Musante@Sun.COM boolean_t do_prefix; 19397509SMark.Musante@Sun.COM uint64_t idx; 19402082Seschrock 19412082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 19422676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 19432082Seschrock zhp->zfs_name); 19442082Seschrock 19452676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 19462676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 19472676Seschrock (void) no_memory(hdl); 19482676Seschrock goto error; 1949789Sahrens } 1950789Sahrens 19517184Stimh if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl, 19522676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 19532676Seschrock goto error; 19545094Slling 19552676Seschrock nvlist_free(nvl); 19562676Seschrock nvl = realprops; 19572676Seschrock 19582676Seschrock prop = zfs_name_to_prop(propname); 19592676Seschrock 19607366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 19612676Seschrock goto error; 1962789Sahrens 1963789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 19642082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19652082Seschrock "child dataset with inherited mountpoint is used " 19662082Seschrock "in a non-global zone")); 19672082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1968789Sahrens goto error; 1969789Sahrens } 1970789Sahrens 19717509SMark.Musante@Sun.COM /* 19727509SMark.Musante@Sun.COM * If the dataset's canmount property is being set to noauto, 19737509SMark.Musante@Sun.COM * then we want to prevent unmounting & remounting it. 19747509SMark.Musante@Sun.COM */ 19757509SMark.Musante@Sun.COM do_prefix = !((prop == ZFS_PROP_CANMOUNT) && 19767509SMark.Musante@Sun.COM (zprop_string_to_index(prop, propval, &idx, 19777509SMark.Musante@Sun.COM ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO)); 19786168Shs24103 19796168Shs24103 if (do_prefix && (ret = changelist_prefix(cl)) != 0) 19807509SMark.Musante@Sun.COM goto error; 1981789Sahrens 1982789Sahrens /* 1983789Sahrens * Execute the corresponding ioctl() to set this property. 1984789Sahrens */ 1985789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1986789Sahrens 19875094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 19882676Seschrock goto error; 19892676Seschrock 19904543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1991789Sahrens if (ret != 0) { 1992789Sahrens switch (errno) { 1993789Sahrens 1994789Sahrens case ENOSPC: 1995789Sahrens /* 1996789Sahrens * For quotas and reservations, ENOSPC indicates 1997789Sahrens * something different; setting a quota or reservation 1998789Sahrens * doesn't use any disk space. 1999789Sahrens */ 2000789Sahrens switch (prop) { 2001789Sahrens case ZFS_PROP_QUOTA: 20025378Sck153898 case ZFS_PROP_REFQUOTA: 20032082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20042082Seschrock "size is less than current used or " 20052082Seschrock "reserved space")); 20062082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 2007789Sahrens break; 2008789Sahrens 2009789Sahrens case ZFS_PROP_RESERVATION: 20105378Sck153898 case ZFS_PROP_REFRESERVATION: 20112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20122082Seschrock "size is greater than available space")); 20132082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 2014789Sahrens break; 2015789Sahrens 2016789Sahrens default: 20172082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 2018789Sahrens break; 2019789Sahrens } 2020789Sahrens break; 2021789Sahrens 2022789Sahrens case EBUSY: 20232082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 20242082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 20252082Seschrock else 20262676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 2027789Sahrens break; 2028789Sahrens 20291175Slling case EROFS: 20302082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 20311175Slling break; 20321175Slling 20333886Sahl case ENOTSUP: 20343886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20355977Smarks "pool and or dataset must be upgraded to set this " 20364603Sahrens "property or value")); 20373886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 20383886Sahl break; 20393886Sahl 20407042Sgw25295 case ERANGE: 20417042Sgw25295 if (prop == ZFS_PROP_COMPRESSION) { 20427042Sgw25295 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20437042Sgw25295 "property setting is not allowed on " 20447042Sgw25295 "bootable datasets")); 20457042Sgw25295 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 20467042Sgw25295 } else { 20477042Sgw25295 (void) zfs_standard_error(hdl, errno, errbuf); 20487042Sgw25295 } 20497042Sgw25295 break; 20507042Sgw25295 2051789Sahrens case EOVERFLOW: 2052789Sahrens /* 2053789Sahrens * This platform can't address a volume this big. 2054789Sahrens */ 2055789Sahrens #ifdef _ILP32 2056789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 20572082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 2058789Sahrens break; 2059789Sahrens } 2060789Sahrens #endif 20612082Seschrock /* FALLTHROUGH */ 2062789Sahrens default: 20632082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 2064789Sahrens } 2065789Sahrens } else { 20666168Shs24103 if (do_prefix) 20676168Shs24103 ret = changelist_postfix(cl); 20686168Shs24103 2069789Sahrens /* 2070789Sahrens * Refresh the statistics so the new property value 2071789Sahrens * is reflected. 2072789Sahrens */ 20736168Shs24103 if (ret == 0) 20742676Seschrock (void) get_stats(zhp); 2075789Sahrens } 2076789Sahrens 2077789Sahrens error: 20782676Seschrock nvlist_free(nvl); 20792676Seschrock zcmd_free_nvlists(&zc); 20802676Seschrock if (cl) 20812676Seschrock changelist_free(cl); 2082789Sahrens return (ret); 2083789Sahrens } 2084789Sahrens 2085789Sahrens /* 2086789Sahrens * Given a property, inherit the value from the parent dataset. 2087789Sahrens */ 2088789Sahrens int 20892676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 2090789Sahrens { 2091789Sahrens zfs_cmd_t zc = { 0 }; 2092789Sahrens int ret; 2093789Sahrens prop_changelist_t *cl; 20942082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 20952082Seschrock char errbuf[1024]; 20962676Seschrock zfs_prop_t prop; 20972082Seschrock 20982082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 20992082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 2100789Sahrens 21015094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 21022676Seschrock /* 21032676Seschrock * For user properties, the amount of work we have to do is very 21042676Seschrock * small, so just do it here. 21052676Seschrock */ 21062676Seschrock if (!zfs_prop_user(propname)) { 21072676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21082676Seschrock "invalid property")); 21092676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 21102676Seschrock } 21112676Seschrock 21122676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21132676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 21142676Seschrock 21154849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 21162676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 21172676Seschrock 21182676Seschrock return (0); 21192676Seschrock } 21202676Seschrock 2121789Sahrens /* 2122789Sahrens * Verify that this property is inheritable. 2123789Sahrens */ 21242082Seschrock if (zfs_prop_readonly(prop)) 21252082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 21262082Seschrock 21272082Seschrock if (!zfs_prop_inheritable(prop)) 21282082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 2129789Sahrens 2130789Sahrens /* 2131789Sahrens * Check to see if the value applies to this type 2132789Sahrens */ 21332082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 21342082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 2135789Sahrens 21363443Srm160521 /* 21373443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 21383443Srm160521 */ 21393443Srm160521 propname = zfs_prop_to_name(prop); 2140789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 21412676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 2142789Sahrens 2143789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 2144789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 21452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21462082Seschrock "dataset is used in a non-global zone")); 21472082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 2148789Sahrens } 2149789Sahrens 2150789Sahrens /* 2151789Sahrens * Determine datasets which will be affected by this change, if any. 2152789Sahrens */ 21537366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 2154789Sahrens return (-1); 2155789Sahrens 2156789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 21572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 21582082Seschrock "child dataset with inherited mountpoint is used " 21592082Seschrock "in a non-global zone")); 21602082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 2161789Sahrens goto error; 2162789Sahrens } 2163789Sahrens 2164789Sahrens if ((ret = changelist_prefix(cl)) != 0) 2165789Sahrens goto error; 2166789Sahrens 21674849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 21682082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2169789Sahrens } else { 2170789Sahrens 21712169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 2172789Sahrens goto error; 2173789Sahrens 2174789Sahrens /* 2175789Sahrens * Refresh the statistics so the new property is reflected. 2176789Sahrens */ 2177789Sahrens (void) get_stats(zhp); 2178789Sahrens } 2179789Sahrens 2180789Sahrens error: 2181789Sahrens changelist_free(cl); 2182789Sahrens return (ret); 2183789Sahrens } 2184789Sahrens 2185789Sahrens /* 21861356Seschrock * True DSL properties are stored in an nvlist. The following two functions 21871356Seschrock * extract them appropriately. 21881356Seschrock */ 21891356Seschrock static uint64_t 21901356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 21911356Seschrock { 21921356Seschrock nvlist_t *nv; 21931356Seschrock uint64_t value; 21941356Seschrock 21952885Sahrens *source = NULL; 21961356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 21971356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 21985094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 21995094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 22001356Seschrock } else { 22011356Seschrock value = zfs_prop_default_numeric(prop); 22021356Seschrock *source = ""; 22031356Seschrock } 22041356Seschrock 22051356Seschrock return (value); 22061356Seschrock } 22071356Seschrock 22081356Seschrock static char * 22091356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 22101356Seschrock { 22111356Seschrock nvlist_t *nv; 22121356Seschrock char *value; 22131356Seschrock 22142885Sahrens *source = NULL; 22151356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 22161356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 22175094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 22185094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 22191356Seschrock } else { 22201356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 22211356Seschrock value = ""; 22221356Seschrock *source = ""; 22231356Seschrock } 22241356Seschrock 22251356Seschrock return (value); 22261356Seschrock } 22271356Seschrock 22281356Seschrock /* 2229789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 2230789Sahrens * zfs_prop_get_int() are built using this interface. 2231789Sahrens * 2232789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 2233789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 2234789Sahrens * If they differ from the on-disk values, report the current values and mark 2235789Sahrens * the source "temporary". 2236789Sahrens */ 22372082Seschrock static int 22385094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 22392082Seschrock char **source, uint64_t *val) 2240789Sahrens { 22415147Srm160521 zfs_cmd_t zc = { 0 }; 22425592Stimh nvlist_t *zplprops = NULL; 2243789Sahrens struct mnttab mnt; 22443265Sahrens char *mntopt_on = NULL; 22453265Sahrens char *mntopt_off = NULL; 2246789Sahrens 2247789Sahrens *source = NULL; 2248789Sahrens 22493265Sahrens switch (prop) { 22503265Sahrens case ZFS_PROP_ATIME: 22513265Sahrens mntopt_on = MNTOPT_ATIME; 22523265Sahrens mntopt_off = MNTOPT_NOATIME; 22533265Sahrens break; 22543265Sahrens 22553265Sahrens case ZFS_PROP_DEVICES: 22563265Sahrens mntopt_on = MNTOPT_DEVICES; 22573265Sahrens mntopt_off = MNTOPT_NODEVICES; 22583265Sahrens break; 22593265Sahrens 22603265Sahrens case ZFS_PROP_EXEC: 22613265Sahrens mntopt_on = MNTOPT_EXEC; 22623265Sahrens mntopt_off = MNTOPT_NOEXEC; 22633265Sahrens break; 22643265Sahrens 22653265Sahrens case ZFS_PROP_READONLY: 22663265Sahrens mntopt_on = MNTOPT_RO; 22673265Sahrens mntopt_off = MNTOPT_RW; 22683265Sahrens break; 22693265Sahrens 22703265Sahrens case ZFS_PROP_SETUID: 22713265Sahrens mntopt_on = MNTOPT_SETUID; 22723265Sahrens mntopt_off = MNTOPT_NOSETUID; 22733265Sahrens break; 22743265Sahrens 22753265Sahrens case ZFS_PROP_XATTR: 22763265Sahrens mntopt_on = MNTOPT_XATTR; 22773265Sahrens mntopt_off = MNTOPT_NOXATTR; 22783265Sahrens break; 22795331Samw 22805331Samw case ZFS_PROP_NBMAND: 22815331Samw mntopt_on = MNTOPT_NBMAND; 22825331Samw mntopt_off = MNTOPT_NONBMAND; 22835331Samw break; 22843265Sahrens } 22853265Sahrens 22862474Seschrock /* 22872474Seschrock * Because looking up the mount options is potentially expensive 22882474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 22892474Seschrock * we're looking up a property which requires its presence. 22902474Seschrock */ 22912474Seschrock if (!zhp->zfs_mntcheck && 22923265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 2293*8228SEric.Taylor@Sun.COM libzfs_handle_t *hdl = zhp->zfs_hdl; 2294*8228SEric.Taylor@Sun.COM struct mnttab entry; 2295*8228SEric.Taylor@Sun.COM 2296*8228SEric.Taylor@Sun.COM if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 2297*8228SEric.Taylor@Sun.COM zhp->zfs_mntopts = zfs_strdup(hdl, 22983265Sahrens entry.mnt_mntopts); 22993265Sahrens if (zhp->zfs_mntopts == NULL) 23003265Sahrens return (-1); 23013265Sahrens } 23022474Seschrock 23032474Seschrock zhp->zfs_mntcheck = B_TRUE; 23042474Seschrock } 23052474Seschrock 2306789Sahrens if (zhp->zfs_mntopts == NULL) 2307789Sahrens mnt.mnt_mntopts = ""; 2308789Sahrens else 2309789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 2310789Sahrens 2311789Sahrens switch (prop) { 2312789Sahrens case ZFS_PROP_ATIME: 23133265Sahrens case ZFS_PROP_DEVICES: 23143265Sahrens case ZFS_PROP_EXEC: 23153265Sahrens case ZFS_PROP_READONLY: 23163265Sahrens case ZFS_PROP_SETUID: 23173265Sahrens case ZFS_PROP_XATTR: 23185331Samw case ZFS_PROP_NBMAND: 23192082Seschrock *val = getprop_uint64(zhp, prop, source); 23202082Seschrock 23213265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 23222082Seschrock *val = B_TRUE; 2323789Sahrens if (src) 23245094Slling *src = ZPROP_SRC_TEMPORARY; 23253265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 23262082Seschrock *val = B_FALSE; 2327789Sahrens if (src) 23285094Slling *src = ZPROP_SRC_TEMPORARY; 2329789Sahrens } 23302082Seschrock break; 2331789Sahrens 23323265Sahrens case ZFS_PROP_CANMOUNT: 23332082Seschrock *val = getprop_uint64(zhp, prop, source); 23346168Shs24103 if (*val != ZFS_CANMOUNT_ON) 23353417Srm160521 *source = zhp->zfs_name; 23363417Srm160521 else 23373417Srm160521 *source = ""; /* default */ 23382082Seschrock break; 2339789Sahrens 2340789Sahrens case ZFS_PROP_QUOTA: 23415378Sck153898 case ZFS_PROP_REFQUOTA: 2342789Sahrens case ZFS_PROP_RESERVATION: 23435378Sck153898 case ZFS_PROP_REFRESERVATION: 23442885Sahrens *val = getprop_uint64(zhp, prop, source); 23452885Sahrens if (*val == 0) 2346789Sahrens *source = ""; /* default */ 2347789Sahrens else 2348789Sahrens *source = zhp->zfs_name; 23492082Seschrock break; 2350789Sahrens 2351789Sahrens case ZFS_PROP_MOUNTED: 23522082Seschrock *val = (zhp->zfs_mntopts != NULL); 23532082Seschrock break; 2354789Sahrens 23553377Seschrock case ZFS_PROP_NUMCLONES: 23563377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 23573377Seschrock break; 23583377Seschrock 23595147Srm160521 case ZFS_PROP_VERSION: 23605498Stimh case ZFS_PROP_NORMALIZE: 23615498Stimh case ZFS_PROP_UTF8ONLY: 23625498Stimh case ZFS_PROP_CASE: 23635498Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 23645498Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 23655473Srm160521 return (-1); 23665147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23675498Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 23685498Stimh zcmd_free_nvlists(&zc); 23695147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 23705498Stimh "unable to get %s property"), 23715498Stimh zfs_prop_to_name(prop)); 23725147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 23735147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 23745147Srm160521 } 23755498Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 23765498Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 23775498Stimh val) != 0) { 23785498Stimh zcmd_free_nvlists(&zc); 23795498Stimh zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 23805498Stimh "unable to get %s property"), 23815498Stimh zfs_prop_to_name(prop)); 23825498Stimh return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM, 23835498Stimh dgettext(TEXT_DOMAIN, "internal error"))); 23845498Stimh } 23855592Stimh if (zplprops) 23865592Stimh nvlist_free(zplprops); 23875498Stimh zcmd_free_nvlists(&zc); 23885147Srm160521 break; 23895147Srm160521 2390789Sahrens default: 23914577Sahrens switch (zfs_prop_get_type(prop)) { 23924787Sahrens case PROP_TYPE_NUMBER: 23934787Sahrens case PROP_TYPE_INDEX: 23944577Sahrens *val = getprop_uint64(zhp, prop, source); 23957390SMatthew.Ahrens@Sun.COM /* 23967390SMatthew.Ahrens@Sun.COM * If we tried to use a defalut value for a 23977390SMatthew.Ahrens@Sun.COM * readonly property, it means that it was not 23987390SMatthew.Ahrens@Sun.COM * present; return an error. 23997390SMatthew.Ahrens@Sun.COM */ 24007390SMatthew.Ahrens@Sun.COM if (zfs_prop_readonly(prop) && 24017390SMatthew.Ahrens@Sun.COM *source && (*source)[0] == '\0') { 24027390SMatthew.Ahrens@Sun.COM return (-1); 24037390SMatthew.Ahrens@Sun.COM } 24044577Sahrens break; 24054577Sahrens 24064787Sahrens case PROP_TYPE_STRING: 24074577Sahrens default: 24084577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 24094577Sahrens "cannot get non-numeric property")); 24104577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 24114577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 24124577Sahrens } 2413789Sahrens } 2414789Sahrens 2415789Sahrens return (0); 2416789Sahrens } 2417789Sahrens 2418789Sahrens /* 2419789Sahrens * Calculate the source type, given the raw source string. 2420789Sahrens */ 2421789Sahrens static void 24225094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2423789Sahrens char *statbuf, size_t statlen) 2424789Sahrens { 24255094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2426789Sahrens return; 2427789Sahrens 2428789Sahrens if (source == NULL) { 24295094Slling *srctype = ZPROP_SRC_NONE; 2430789Sahrens } else if (source[0] == '\0') { 24315094Slling *srctype = ZPROP_SRC_DEFAULT; 2432789Sahrens } else { 2433789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 24345094Slling *srctype = ZPROP_SRC_LOCAL; 2435789Sahrens } else { 2436789Sahrens (void) strlcpy(statbuf, source, statlen); 24375094Slling *srctype = ZPROP_SRC_INHERITED; 2438789Sahrens } 2439789Sahrens } 2440789Sahrens 2441789Sahrens } 2442789Sahrens 2443789Sahrens /* 2444789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2445789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2446789Sahrens * human-readable form. 2447789Sahrens * 2448789Sahrens * Returns 0 on success, or -1 on error. 2449789Sahrens */ 2450789Sahrens int 2451789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 24525094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2453789Sahrens { 2454789Sahrens char *source = NULL; 2455789Sahrens uint64_t val; 2456789Sahrens char *str; 24572676Seschrock const char *strval; 2458789Sahrens 2459789Sahrens /* 2460789Sahrens * Check to see if this property applies to our object 2461789Sahrens */ 2462789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2463789Sahrens return (-1); 2464789Sahrens 2465789Sahrens if (src) 24665094Slling *src = ZPROP_SRC_NONE; 2467789Sahrens 2468789Sahrens switch (prop) { 2469789Sahrens case ZFS_PROP_CREATION: 2470789Sahrens /* 2471789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2472789Sahrens * this into a string unless 'literal' is specified. 2473789Sahrens */ 2474789Sahrens { 24752885Sahrens val = getprop_uint64(zhp, prop, &source); 24762885Sahrens time_t time = (time_t)val; 2477789Sahrens struct tm t; 2478789Sahrens 2479789Sahrens if (literal || 2480789Sahrens localtime_r(&time, &t) == NULL || 2481789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2482789Sahrens &t) == 0) 24832885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2484789Sahrens } 2485789Sahrens break; 2486789Sahrens 2487789Sahrens case ZFS_PROP_MOUNTPOINT: 2488789Sahrens /* 2489789Sahrens * Getting the precise mountpoint can be tricky. 2490789Sahrens * 2491789Sahrens * - for 'none' or 'legacy', return those values. 2492789Sahrens * - for inherited mountpoints, we want to take everything 2493789Sahrens * after our ancestor and append it to the inherited value. 2494789Sahrens * 2495789Sahrens * If the pool has an alternate root, we want to prepend that 2496789Sahrens * root to any values we return. 2497789Sahrens */ 24986865Srm160521 24991356Seschrock str = getprop_string(zhp, prop, &source); 25001356Seschrock 25016612Sgw25295 if (str[0] == '/') { 25026865Srm160521 char buf[MAXPATHLEN]; 25036865Srm160521 char *root = buf; 25041356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2505789Sahrens 2506789Sahrens if (relpath[0] == '/') 2507789Sahrens relpath++; 25086612Sgw25295 25096865Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 25106865Srm160521 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) || 25116865Srm160521 (strcmp(root, "-") == 0)) 25126865Srm160521 root[0] = '\0'; 25136612Sgw25295 /* 25146612Sgw25295 * Special case an alternate root of '/'. This will 25156612Sgw25295 * avoid having multiple leading slashes in the 25166612Sgw25295 * mountpoint path. 25176612Sgw25295 */ 25186612Sgw25295 if (strcmp(root, "/") == 0) 25196612Sgw25295 root++; 25206612Sgw25295 25216612Sgw25295 /* 25226612Sgw25295 * If the mountpoint is '/' then skip over this 25236612Sgw25295 * if we are obtaining either an alternate root or 25246612Sgw25295 * an inherited mountpoint. 25256612Sgw25295 */ 25266612Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 25276612Sgw25295 relpath[0] != '\0')) 25281356Seschrock str++; 2529789Sahrens 2530789Sahrens if (relpath[0] == '\0') 2531789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 25321356Seschrock root, str); 2533789Sahrens else 2534789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 25351356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2536789Sahrens relpath); 2537789Sahrens } else { 2538789Sahrens /* 'legacy' or 'none' */ 25391356Seschrock (void) strlcpy(propbuf, str, proplen); 2540789Sahrens } 2541789Sahrens 2542789Sahrens break; 2543789Sahrens 2544789Sahrens case ZFS_PROP_ORIGIN: 25452885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2546789Sahrens proplen); 2547789Sahrens /* 2548789Sahrens * If there is no parent at all, return failure to indicate that 2549789Sahrens * it doesn't apply to this dataset. 2550789Sahrens */ 2551789Sahrens if (propbuf[0] == '\0') 2552789Sahrens return (-1); 2553789Sahrens break; 2554789Sahrens 2555789Sahrens case ZFS_PROP_QUOTA: 25565378Sck153898 case ZFS_PROP_REFQUOTA: 2557789Sahrens case ZFS_PROP_RESERVATION: 25585378Sck153898 case ZFS_PROP_REFRESERVATION: 25595378Sck153898 25602082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 25612082Seschrock return (-1); 2562789Sahrens 2563789Sahrens /* 2564789Sahrens * If quota or reservation is 0, we translate this into 'none' 2565789Sahrens * (unless literal is set), and indicate that it's the default 2566789Sahrens * value. Otherwise, we print the number nicely and indicate 2567789Sahrens * that its set locally. 2568789Sahrens */ 2569789Sahrens if (val == 0) { 2570789Sahrens if (literal) 2571789Sahrens (void) strlcpy(propbuf, "0", proplen); 2572789Sahrens else 2573789Sahrens (void) strlcpy(propbuf, "none", proplen); 2574789Sahrens } else { 2575789Sahrens if (literal) 25762856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 25773912Slling (u_longlong_t)val); 2578789Sahrens else 2579789Sahrens zfs_nicenum(val, propbuf, proplen); 2580789Sahrens } 2581789Sahrens break; 2582789Sahrens 2583789Sahrens case ZFS_PROP_COMPRESSRATIO: 25842082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 25852082Seschrock return (-1); 25862856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 25872856Snd150628 val / 100, (longlong_t)val % 100); 2588789Sahrens break; 2589789Sahrens 2590789Sahrens case ZFS_PROP_TYPE: 2591789Sahrens switch (zhp->zfs_type) { 2592789Sahrens case ZFS_TYPE_FILESYSTEM: 2593789Sahrens str = "filesystem"; 2594789Sahrens break; 2595789Sahrens case ZFS_TYPE_VOLUME: 2596789Sahrens str = "volume"; 2597789Sahrens break; 2598789Sahrens case ZFS_TYPE_SNAPSHOT: 2599789Sahrens str = "snapshot"; 2600789Sahrens break; 2601789Sahrens default: 26022082Seschrock abort(); 2603789Sahrens } 2604789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2605789Sahrens break; 2606789Sahrens 2607789Sahrens case ZFS_PROP_MOUNTED: 2608789Sahrens /* 2609789Sahrens * The 'mounted' property is a pseudo-property that described 2610789Sahrens * whether the filesystem is currently mounted. Even though 2611789Sahrens * it's a boolean value, the typical values of "on" and "off" 2612789Sahrens * don't make sense, so we translate to "yes" and "no". 2613789Sahrens */ 26142082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 26152082Seschrock src, &source, &val) != 0) 26162082Seschrock return (-1); 26172082Seschrock if (val) 2618789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2619789Sahrens else 2620789Sahrens (void) strlcpy(propbuf, "no", proplen); 2621789Sahrens break; 2622789Sahrens 2623789Sahrens case ZFS_PROP_NAME: 2624789Sahrens /* 2625789Sahrens * The 'name' property is a pseudo-property derived from the 2626789Sahrens * dataset name. It is presented as a real property to simplify 2627789Sahrens * consumers. 2628789Sahrens */ 2629789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2630789Sahrens break; 2631789Sahrens 2632789Sahrens default: 26334577Sahrens switch (zfs_prop_get_type(prop)) { 26344787Sahrens case PROP_TYPE_NUMBER: 26354577Sahrens if (get_numeric_property(zhp, prop, src, 26364577Sahrens &source, &val) != 0) 26374577Sahrens return (-1); 26384577Sahrens if (literal) 26394577Sahrens (void) snprintf(propbuf, proplen, "%llu", 26404577Sahrens (u_longlong_t)val); 26414577Sahrens else 26424577Sahrens zfs_nicenum(val, propbuf, proplen); 26434577Sahrens break; 26444577Sahrens 26454787Sahrens case PROP_TYPE_STRING: 26464577Sahrens (void) strlcpy(propbuf, 26474577Sahrens getprop_string(zhp, prop, &source), proplen); 26484577Sahrens break; 26494577Sahrens 26504787Sahrens case PROP_TYPE_INDEX: 26514861Sahrens if (get_numeric_property(zhp, prop, src, 26524861Sahrens &source, &val) != 0) 26534861Sahrens return (-1); 26544861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 26554577Sahrens return (-1); 26564577Sahrens (void) strlcpy(propbuf, strval, proplen); 26574577Sahrens break; 26584577Sahrens 26594577Sahrens default: 26604577Sahrens abort(); 26614577Sahrens } 2662789Sahrens } 2663789Sahrens 2664789Sahrens get_source(zhp, src, source, statbuf, statlen); 2665789Sahrens 2666789Sahrens return (0); 2667789Sahrens } 2668789Sahrens 2669789Sahrens /* 2670789Sahrens * Utility function to get the given numeric property. Does no validation that 2671789Sahrens * the given property is the appropriate type; should only be used with 2672789Sahrens * hard-coded property types. 2673789Sahrens */ 2674789Sahrens uint64_t 2675789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2676789Sahrens { 2677789Sahrens char *source; 26782082Seschrock uint64_t val; 26792082Seschrock 26805367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 26812082Seschrock 26822082Seschrock return (val); 2683789Sahrens } 2684789Sahrens 26855713Srm160521 int 26865713Srm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 26875713Srm160521 { 26885713Srm160521 char buf[64]; 26895713Srm160521 26905713Srm160521 zfs_nicenum(val, buf, sizeof (buf)); 26915713Srm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 26925713Srm160521 } 26935713Srm160521 2694789Sahrens /* 2695789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2696789Sahrens */ 2697789Sahrens int 2698789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 26995094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2700789Sahrens { 2701789Sahrens char *source; 2702789Sahrens 2703789Sahrens /* 2704789Sahrens * Check to see if this property applies to our object 2705789Sahrens */ 27064849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 27073237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 27082082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 27092082Seschrock zfs_prop_to_name(prop))); 27104849Sahrens } 2711789Sahrens 2712789Sahrens if (src) 27135094Slling *src = ZPROP_SRC_NONE; 2714789Sahrens 27152082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 27162082Seschrock return (-1); 2717789Sahrens 2718789Sahrens get_source(zhp, src, source, statbuf, statlen); 2719789Sahrens 2720789Sahrens return (0); 2721789Sahrens } 2722789Sahrens 2723789Sahrens /* 2724789Sahrens * Returns the name of the given zfs handle. 2725789Sahrens */ 2726789Sahrens const char * 2727789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2728789Sahrens { 2729789Sahrens return (zhp->zfs_name); 2730789Sahrens } 2731789Sahrens 2732789Sahrens /* 2733789Sahrens * Returns the type of the given zfs handle. 2734789Sahrens */ 2735789Sahrens zfs_type_t 2736789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2737789Sahrens { 2738789Sahrens return (zhp->zfs_type); 2739789Sahrens } 2740789Sahrens 2741*8228SEric.Taylor@Sun.COM static int 2742*8228SEric.Taylor@Sun.COM zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc) 2743*8228SEric.Taylor@Sun.COM { 2744*8228SEric.Taylor@Sun.COM int rc; 2745*8228SEric.Taylor@Sun.COM uint64_t orig_cookie; 2746*8228SEric.Taylor@Sun.COM 2747*8228SEric.Taylor@Sun.COM orig_cookie = zc->zc_cookie; 2748*8228SEric.Taylor@Sun.COM top: 2749*8228SEric.Taylor@Sun.COM (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 2750*8228SEric.Taylor@Sun.COM rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc); 2751*8228SEric.Taylor@Sun.COM 2752*8228SEric.Taylor@Sun.COM if (rc == -1) { 2753*8228SEric.Taylor@Sun.COM switch (errno) { 2754*8228SEric.Taylor@Sun.COM case ENOMEM: 2755*8228SEric.Taylor@Sun.COM /* expand nvlist memory and try again */ 2756*8228SEric.Taylor@Sun.COM if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) { 2757*8228SEric.Taylor@Sun.COM zcmd_free_nvlists(zc); 2758*8228SEric.Taylor@Sun.COM return (-1); 2759*8228SEric.Taylor@Sun.COM } 2760*8228SEric.Taylor@Sun.COM zc->zc_cookie = orig_cookie; 2761*8228SEric.Taylor@Sun.COM goto top; 2762*8228SEric.Taylor@Sun.COM /* 2763*8228SEric.Taylor@Sun.COM * An errno value of ESRCH indicates normal completion. 2764*8228SEric.Taylor@Sun.COM * If ENOENT is returned, then the underlying dataset 2765*8228SEric.Taylor@Sun.COM * has been removed since we obtained the handle. 2766*8228SEric.Taylor@Sun.COM */ 2767*8228SEric.Taylor@Sun.COM case ESRCH: 2768*8228SEric.Taylor@Sun.COM case ENOENT: 2769*8228SEric.Taylor@Sun.COM rc = 1; 2770*8228SEric.Taylor@Sun.COM break; 2771*8228SEric.Taylor@Sun.COM default: 2772*8228SEric.Taylor@Sun.COM rc = zfs_standard_error(zhp->zfs_hdl, errno, 2773*8228SEric.Taylor@Sun.COM dgettext(TEXT_DOMAIN, 2774*8228SEric.Taylor@Sun.COM "cannot iterate filesystems")); 2775*8228SEric.Taylor@Sun.COM break; 2776*8228SEric.Taylor@Sun.COM } 2777*8228SEric.Taylor@Sun.COM } 2778*8228SEric.Taylor@Sun.COM return (rc); 2779*8228SEric.Taylor@Sun.COM } 2780*8228SEric.Taylor@Sun.COM 2781789Sahrens /* 27821356Seschrock * Iterate over all child filesystems 2783789Sahrens */ 2784789Sahrens int 27851356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2786789Sahrens { 2787789Sahrens zfs_cmd_t zc = { 0 }; 2788789Sahrens zfs_handle_t *nzhp; 2789789Sahrens int ret; 2790789Sahrens 27915367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 27925367Sahrens return (0); 27935367Sahrens 2794*8228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 2795*8228SEric.Taylor@Sun.COM return (-1); 2796*8228SEric.Taylor@Sun.COM 2797*8228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT, 2798*8228SEric.Taylor@Sun.COM &zc)) == 0) { 2799789Sahrens /* 2800789Sahrens * Ignore private dataset names. 2801789Sahrens */ 2802789Sahrens if (dataset_name_hidden(zc.zc_name)) 2803789Sahrens continue; 2804789Sahrens 2805789Sahrens /* 2806789Sahrens * Silently ignore errors, as the only plausible explanation is 2807789Sahrens * that the pool has since been removed. 2808789Sahrens */ 2809*8228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 2810*8228SEric.Taylor@Sun.COM &zc)) == NULL) { 2811789Sahrens continue; 2812*8228SEric.Taylor@Sun.COM } 2813*8228SEric.Taylor@Sun.COM 2814*8228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 2815*8228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2816789Sahrens return (ret); 2817*8228SEric.Taylor@Sun.COM } 2818789Sahrens } 2819*8228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2820*8228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 28211356Seschrock } 28221356Seschrock 28231356Seschrock /* 28241356Seschrock * Iterate over all snapshots 28251356Seschrock */ 28261356Seschrock int 28271356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 28281356Seschrock { 28291356Seschrock zfs_cmd_t zc = { 0 }; 28301356Seschrock zfs_handle_t *nzhp; 28311356Seschrock int ret; 2832789Sahrens 28335367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 28345367Sahrens return (0); 28355367Sahrens 2836*8228SEric.Taylor@Sun.COM if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 2837*8228SEric.Taylor@Sun.COM return (-1); 2838*8228SEric.Taylor@Sun.COM while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, 2839*8228SEric.Taylor@Sun.COM &zc)) == 0) { 2840*8228SEric.Taylor@Sun.COM 2841*8228SEric.Taylor@Sun.COM if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl, 2842*8228SEric.Taylor@Sun.COM &zc)) == NULL) { 2843789Sahrens continue; 2844*8228SEric.Taylor@Sun.COM } 2845*8228SEric.Taylor@Sun.COM 2846*8228SEric.Taylor@Sun.COM if ((ret = func(nzhp, data)) != 0) { 2847*8228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2848789Sahrens return (ret); 2849*8228SEric.Taylor@Sun.COM } 2850789Sahrens } 2851*8228SEric.Taylor@Sun.COM zcmd_free_nvlists(&zc); 2852*8228SEric.Taylor@Sun.COM return ((ret < 0) ? ret : 0); 2853789Sahrens } 2854789Sahrens 2855789Sahrens /* 28561356Seschrock * Iterate over all children, snapshots and filesystems 28571356Seschrock */ 28581356Seschrock int 28591356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 28601356Seschrock { 28611356Seschrock int ret; 28621356Seschrock 28631356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 28641356Seschrock return (ret); 28651356Seschrock 28661356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 28671356Seschrock } 28681356Seschrock 28691356Seschrock /* 2870789Sahrens * Given a complete name, return just the portion that refers to the parent. 2871789Sahrens * Can return NULL if this is a pool. 2872789Sahrens */ 2873789Sahrens static int 2874789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2875789Sahrens { 2876789Sahrens char *loc; 2877789Sahrens 2878789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2879789Sahrens return (-1); 2880789Sahrens 2881789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2882789Sahrens buf[loc - path] = '\0'; 2883789Sahrens 2884789Sahrens return (0); 2885789Sahrens } 2886789Sahrens 2887789Sahrens /* 28884490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 28894490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 28904490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 28914490Svb160487 * length of already existing prefix of the given path. We also fetch the 28924490Svb160487 * 'zoned' property, which is used to validate property settings when creating 28934490Svb160487 * new datasets. 2894789Sahrens */ 2895789Sahrens static int 28964490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 28974490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2898789Sahrens { 2899789Sahrens zfs_cmd_t zc = { 0 }; 2900789Sahrens char parent[ZFS_MAXNAMELEN]; 2901789Sahrens char *slash; 29021356Seschrock zfs_handle_t *zhp; 29032082Seschrock char errbuf[1024]; 29042082Seschrock 29052082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 29062082Seschrock path); 2907789Sahrens 2908789Sahrens /* get parent, and check to see if this is just a pool */ 2909789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 29102082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29112082Seschrock "missing dataset name")); 29122082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2913789Sahrens } 2914789Sahrens 2915789Sahrens /* check to see if the pool exists */ 2916789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2917789Sahrens slash = parent + strlen(parent); 2918789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2919789Sahrens zc.zc_name[slash - parent] = '\0'; 29202082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2921789Sahrens errno == ENOENT) { 29222082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29232082Seschrock "no such pool '%s'"), zc.zc_name); 29242082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2925789Sahrens } 2926789Sahrens 2927789Sahrens /* check to see if the parent dataset exists */ 29284490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 29294490Svb160487 if (errno == ENOENT && accept_ancestor) { 29304490Svb160487 /* 29314490Svb160487 * Go deeper to find an ancestor, give up on top level. 29324490Svb160487 */ 29334490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 29344490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29354490Svb160487 "no such pool '%s'"), zc.zc_name); 29364490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 29374490Svb160487 } 29384490Svb160487 } else if (errno == ENOENT) { 29392082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29402082Seschrock "parent does not exist")); 29412082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 29424490Svb160487 } else 29432082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2944789Sahrens } 2945789Sahrens 29462676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2947789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 29482676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 29492082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 29501356Seschrock zfs_close(zhp); 2951789Sahrens return (-1); 2952789Sahrens } 2953789Sahrens 2954789Sahrens /* make sure parent is a filesystem */ 29551356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 29562082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29572082Seschrock "parent is not a filesystem")); 29582082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 29591356Seschrock zfs_close(zhp); 2960789Sahrens return (-1); 2961789Sahrens } 2962789Sahrens 29631356Seschrock zfs_close(zhp); 29644490Svb160487 if (prefixlen != NULL) 29654490Svb160487 *prefixlen = strlen(parent); 29664490Svb160487 return (0); 29674490Svb160487 } 29684490Svb160487 29694490Svb160487 /* 29704490Svb160487 * Finds whether the dataset of the given type(s) exists. 29714490Svb160487 */ 29724490Svb160487 boolean_t 29734490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 29744490Svb160487 { 29754490Svb160487 zfs_handle_t *zhp; 29764490Svb160487 29775326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 29784490Svb160487 return (B_FALSE); 29794490Svb160487 29804490Svb160487 /* 29814490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 29824490Svb160487 */ 29834490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 29844490Svb160487 int ds_type = zhp->zfs_type; 29854490Svb160487 29864490Svb160487 zfs_close(zhp); 29874490Svb160487 if (types & ds_type) 29884490Svb160487 return (B_TRUE); 29894490Svb160487 } 29904490Svb160487 return (B_FALSE); 29914490Svb160487 } 29924490Svb160487 29934490Svb160487 /* 29945367Sahrens * Given a path to 'target', create all the ancestors between 29955367Sahrens * the prefixlen portion of the path, and the target itself. 29965367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 29975367Sahrens */ 29985367Sahrens int 29995367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 30005367Sahrens { 30015367Sahrens zfs_handle_t *h; 30025367Sahrens char *cp; 30035367Sahrens const char *opname; 30045367Sahrens 30055367Sahrens /* make sure prefix exists */ 30065367Sahrens cp = target + prefixlen; 30075367Sahrens if (*cp != '/') { 30085367Sahrens assert(strchr(cp, '/') == NULL); 30095367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30105367Sahrens } else { 30115367Sahrens *cp = '\0'; 30125367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30135367Sahrens *cp = '/'; 30145367Sahrens } 30155367Sahrens if (h == NULL) 30165367Sahrens return (-1); 30175367Sahrens zfs_close(h); 30185367Sahrens 30195367Sahrens /* 30205367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 30215367Sahrens * up to the prefixlen-long one. 30225367Sahrens */ 30235367Sahrens for (cp = target + prefixlen + 1; 30245367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 30255367Sahrens char *logstr; 30265367Sahrens 30275367Sahrens *cp = '\0'; 30285367Sahrens 30295367Sahrens h = make_dataset_handle(hdl, target); 30305367Sahrens if (h) { 30315367Sahrens /* it already exists, nothing to do here */ 30325367Sahrens zfs_close(h); 30335367Sahrens continue; 30345367Sahrens } 30355367Sahrens 30365367Sahrens logstr = hdl->libzfs_log_str; 30375367Sahrens hdl->libzfs_log_str = NULL; 30385367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 30395367Sahrens NULL) != 0) { 30405367Sahrens hdl->libzfs_log_str = logstr; 30415367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 30425367Sahrens goto ancestorerr; 30435367Sahrens } 30445367Sahrens 30455367Sahrens hdl->libzfs_log_str = logstr; 30465367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30475367Sahrens if (h == NULL) { 30485367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 30495367Sahrens goto ancestorerr; 30505367Sahrens } 30515367Sahrens 30525367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 30535367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 30545367Sahrens goto ancestorerr; 30555367Sahrens } 30565367Sahrens 30575367Sahrens if (zfs_share(h) != 0) { 30585367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 30595367Sahrens goto ancestorerr; 30605367Sahrens } 30615367Sahrens 30625367Sahrens zfs_close(h); 30635367Sahrens } 30645367Sahrens 30655367Sahrens return (0); 30665367Sahrens 30675367Sahrens ancestorerr: 30685367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30695367Sahrens "failed to %s ancestor '%s'"), opname, target); 30705367Sahrens return (-1); 30715367Sahrens } 30725367Sahrens 30735367Sahrens /* 30744490Svb160487 * Creates non-existing ancestors of the given path. 30754490Svb160487 */ 30764490Svb160487 int 30774490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 30784490Svb160487 { 30794490Svb160487 int prefix; 30804490Svb160487 uint64_t zoned; 30814490Svb160487 char *path_copy; 30824490Svb160487 int rc; 30834490Svb160487 30844490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 30854490Svb160487 return (-1); 30864490Svb160487 30874490Svb160487 if ((path_copy = strdup(path)) != NULL) { 30884490Svb160487 rc = create_parents(hdl, path_copy, prefix); 30894490Svb160487 free(path_copy); 30904490Svb160487 } 30914490Svb160487 if (path_copy == NULL || rc != 0) 30924490Svb160487 return (-1); 30934490Svb160487 3094789Sahrens return (0); 3095789Sahrens } 3096789Sahrens 3097789Sahrens /* 30982676Seschrock * Create a new filesystem or volume. 3099789Sahrens */ 3100789Sahrens int 31012082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 31022676Seschrock nvlist_t *props) 3103789Sahrens { 3104789Sahrens zfs_cmd_t zc = { 0 }; 3105789Sahrens int ret; 3106789Sahrens uint64_t size = 0; 3107789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 31082082Seschrock char errbuf[1024]; 31092676Seschrock uint64_t zoned; 31102082Seschrock 31112082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31122082Seschrock "cannot create '%s'"), path); 3113789Sahrens 3114789Sahrens /* validate the path, taking care to note the extended error message */ 31155326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 31162082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3117789Sahrens 3118789Sahrens /* validate parents exist */ 31194490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3120789Sahrens return (-1); 3121789Sahrens 3122789Sahrens /* 3123789Sahrens * The failure modes when creating a dataset of a different type over 3124789Sahrens * one that already exists is a little strange. In particular, if you 3125789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 3126789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 3127789Sahrens * first try to see if the dataset exists. 3128789Sahrens */ 3129789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 31305094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 31312082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31322082Seschrock "dataset already exists")); 31332082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3134789Sahrens } 3135789Sahrens 3136789Sahrens if (type == ZFS_TYPE_VOLUME) 3137789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3138789Sahrens else 3139789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3140789Sahrens 31417184Stimh if (props && (props = zfs_valid_proplist(hdl, type, props, 31423912Slling zoned, NULL, errbuf)) == 0) 31432676Seschrock return (-1); 31442676Seschrock 3145789Sahrens if (type == ZFS_TYPE_VOLUME) { 31461133Seschrock /* 31471133Seschrock * If we are creating a volume, the size and block size must 31481133Seschrock * satisfy a few restraints. First, the blocksize must be a 31491133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 31501133Seschrock * volsize must be a multiple of the block size, and cannot be 31511133Seschrock * zero. 31521133Seschrock */ 31532676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 31542676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 31552676Seschrock nvlist_free(props); 31562082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31572676Seschrock "missing volume size")); 31582676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3159789Sahrens } 3160789Sahrens 31612676Seschrock if ((ret = nvlist_lookup_uint64(props, 31622676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 31632676Seschrock &blocksize)) != 0) { 31642676Seschrock if (ret == ENOENT) { 31652676Seschrock blocksize = zfs_prop_default_numeric( 31662676Seschrock ZFS_PROP_VOLBLOCKSIZE); 31672676Seschrock } else { 31682676Seschrock nvlist_free(props); 31692676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31702676Seschrock "missing volume block size")); 31712676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 31722676Seschrock } 31732676Seschrock } 31742676Seschrock 31752676Seschrock if (size == 0) { 31762676Seschrock nvlist_free(props); 31772082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31782676Seschrock "volume size cannot be zero")); 31792676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 31801133Seschrock } 31811133Seschrock 31821133Seschrock if (size % blocksize != 0) { 31832676Seschrock nvlist_free(props); 31842082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31852676Seschrock "volume size must be a multiple of volume block " 31862676Seschrock "size")); 31872676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 31881133Seschrock } 3189789Sahrens } 3190789Sahrens 31915094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 31922676Seschrock return (-1); 31932676Seschrock nvlist_free(props); 31942676Seschrock 3195789Sahrens /* create the dataset */ 31964543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 3197789Sahrens 31983912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 31992082Seschrock ret = zvol_create_link(hdl, path); 32003912Slling if (ret) { 32013912Slling (void) zfs_standard_error(hdl, errno, 32023912Slling dgettext(TEXT_DOMAIN, 32033912Slling "Volume successfully created, but device links " 32043912Slling "were not created")); 32053912Slling zcmd_free_nvlists(&zc); 32063912Slling return (-1); 32073912Slling } 32083912Slling } 3209789Sahrens 32102676Seschrock zcmd_free_nvlists(&zc); 32112676Seschrock 3212789Sahrens /* check for failure */ 3213789Sahrens if (ret != 0) { 3214789Sahrens char parent[ZFS_MAXNAMELEN]; 3215789Sahrens (void) parent_name(path, parent, sizeof (parent)); 3216789Sahrens 3217789Sahrens switch (errno) { 3218789Sahrens case ENOENT: 32192082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32202082Seschrock "no such parent '%s'"), parent); 32212082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3222789Sahrens 3223789Sahrens case EINVAL: 32242082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32253413Smmusante "parent '%s' is not a filesystem"), parent); 32262082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3227789Sahrens 3228789Sahrens case EDOM: 32292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32302676Seschrock "volume block size must be power of 2 from " 32312676Seschrock "%u to %uk"), 3232789Sahrens (uint_t)SPA_MINBLOCKSIZE, 3233789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 32342082Seschrock 32352676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 32362082Seschrock 32374603Sahrens case ENOTSUP: 32384603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32394603Sahrens "pool must be upgraded to set this " 32404603Sahrens "property or value")); 32414603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3242789Sahrens #ifdef _ILP32 3243789Sahrens case EOVERFLOW: 3244789Sahrens /* 3245789Sahrens * This platform can't address a volume this big. 3246789Sahrens */ 32472082Seschrock if (type == ZFS_TYPE_VOLUME) 32482082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 32492082Seschrock errbuf)); 3250789Sahrens #endif 32512082Seschrock /* FALLTHROUGH */ 3252789Sahrens default: 32532082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3254789Sahrens } 3255789Sahrens } 3256789Sahrens 3257789Sahrens return (0); 3258789Sahrens } 3259789Sahrens 3260789Sahrens /* 3261789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 3262789Sahrens * isn't mounted, and that there are no active dependents. 3263789Sahrens */ 3264789Sahrens int 3265789Sahrens zfs_destroy(zfs_handle_t *zhp) 3266789Sahrens { 3267789Sahrens zfs_cmd_t zc = { 0 }; 3268789Sahrens 3269789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3270789Sahrens 32712676Seschrock if (ZFS_IS_VOLUME(zhp)) { 32723126Sahl /* 32734543Smarks * If user doesn't have permissions to unshare volume, then 32744543Smarks * abort the request. This would only happen for a 32754543Smarks * non-privileged user. 32763126Sahl */ 32774543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 32784543Smarks return (-1); 32794543Smarks } 32803126Sahl 32812082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3282789Sahrens return (-1); 3283789Sahrens 3284789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3285789Sahrens } else { 3286789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3287789Sahrens } 3288789Sahrens 32894543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 32903237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 32912082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 32922082Seschrock zhp->zfs_name)); 32932199Sahrens } 3294789Sahrens 3295789Sahrens remove_mountpoint(zhp); 3296789Sahrens 3297789Sahrens return (0); 3298789Sahrens } 3299789Sahrens 33002199Sahrens struct destroydata { 33012199Sahrens char *snapname; 33022199Sahrens boolean_t gotone; 33033265Sahrens boolean_t closezhp; 33042199Sahrens }; 33052199Sahrens 33062199Sahrens static int 33072199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 33082199Sahrens { 33092199Sahrens struct destroydata *dd = arg; 33102199Sahrens zfs_handle_t *szhp; 33112199Sahrens char name[ZFS_MAXNAMELEN]; 33123265Sahrens boolean_t closezhp = dd->closezhp; 33133265Sahrens int rv; 33142199Sahrens 33152676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 33162676Seschrock (void) strlcat(name, "@", sizeof (name)); 33172676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 33182199Sahrens 33192199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 33202199Sahrens if (szhp) { 33212199Sahrens dd->gotone = B_TRUE; 33222199Sahrens zfs_close(szhp); 33232199Sahrens } 33242199Sahrens 33252199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33262199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 33272199Sahrens /* 33282199Sahrens * NB: this is simply a best-effort. We don't want to 33292199Sahrens * return an error, because then we wouldn't visit all 33302199Sahrens * the volumes. 33312199Sahrens */ 33322199Sahrens } 33332199Sahrens 33343265Sahrens dd->closezhp = B_TRUE; 33353265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 33363265Sahrens if (closezhp) 33373265Sahrens zfs_close(zhp); 33383265Sahrens return (rv); 33392199Sahrens } 33402199Sahrens 33412199Sahrens /* 33422199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 33432199Sahrens */ 33442199Sahrens int 33452199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 33462199Sahrens { 33472199Sahrens zfs_cmd_t zc = { 0 }; 33482199Sahrens int ret; 33492199Sahrens struct destroydata dd = { 0 }; 33502199Sahrens 33512199Sahrens dd.snapname = snapname; 33522199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 33532199Sahrens 33542199Sahrens if (!dd.gotone) { 33553237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 33562199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 33572199Sahrens zhp->zfs_name, snapname)); 33582199Sahrens } 33592199Sahrens 33602199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33612676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 33622199Sahrens 33634543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 33642199Sahrens if (ret != 0) { 33652199Sahrens char errbuf[1024]; 33662199Sahrens 33672199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33682199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 33692199Sahrens 33702199Sahrens switch (errno) { 33712199Sahrens case EEXIST: 33722199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 33732199Sahrens "snapshot is cloned")); 33742199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 33752199Sahrens 33762199Sahrens default: 33772199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 33782199Sahrens errbuf)); 33792199Sahrens } 33802199Sahrens } 33812199Sahrens 33822199Sahrens return (0); 33832199Sahrens } 33842199Sahrens 3385789Sahrens /* 3386789Sahrens * Clones the given dataset. The target must be of the same type as the source. 3387789Sahrens */ 3388789Sahrens int 33892676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3390789Sahrens { 3391789Sahrens zfs_cmd_t zc = { 0 }; 3392789Sahrens char parent[ZFS_MAXNAMELEN]; 3393789Sahrens int ret; 33942082Seschrock char errbuf[1024]; 33952082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 33962676Seschrock zfs_type_t type; 33972676Seschrock uint64_t zoned; 3398789Sahrens 3399789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3400789Sahrens 34012082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34022082Seschrock "cannot create '%s'"), target); 34032082Seschrock 3404789Sahrens /* validate the target name */ 34055326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 34062082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3407789Sahrens 3408789Sahrens /* validate parents exist */ 34094490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3410789Sahrens return (-1); 3411789Sahrens 3412789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3413789Sahrens 3414789Sahrens /* do the clone */ 34152676Seschrock if (ZFS_IS_VOLUME(zhp)) { 3416789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 34172744Snn35248 type = ZFS_TYPE_VOLUME; 34182676Seschrock } else { 3419789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 34202744Snn35248 type = ZFS_TYPE_FILESYSTEM; 34212676Seschrock } 34222676Seschrock 34232676Seschrock if (props) { 34247184Stimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 34257184Stimh zhp, errbuf)) == NULL) 34262676Seschrock return (-1); 34272676Seschrock 34285094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 34292676Seschrock nvlist_free(props); 34302676Seschrock return (-1); 34312676Seschrock } 34322676Seschrock 34332676Seschrock nvlist_free(props); 34342676Seschrock } 3435789Sahrens 3436789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 34372676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 34384543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 3439789Sahrens 34402676Seschrock zcmd_free_nvlists(&zc); 34412676Seschrock 3442789Sahrens if (ret != 0) { 3443789Sahrens switch (errno) { 3444789Sahrens 3445789Sahrens case ENOENT: 3446789Sahrens /* 3447789Sahrens * The parent doesn't exist. We should have caught this 3448789Sahrens * above, but there may a race condition that has since 3449789Sahrens * destroyed the parent. 3450789Sahrens * 3451789Sahrens * At this point, we don't know whether it's the source 3452789Sahrens * that doesn't exist anymore, or whether the target 3453789Sahrens * dataset doesn't exist. 3454789Sahrens */ 34552082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 34562082Seschrock "no such parent '%s'"), parent); 34572082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 34582082Seschrock 34592082Seschrock case EXDEV: 34602082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 34612082Seschrock "source and target pools differ")); 34622082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 34632082Seschrock errbuf)); 34642082Seschrock 34652082Seschrock default: 34662082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 34672082Seschrock errbuf)); 34682082Seschrock } 34692676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 34702082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 34712082Seschrock } 34722082Seschrock 34732082Seschrock return (ret); 34742082Seschrock } 34752082Seschrock 34762082Seschrock typedef struct promote_data { 34772082Seschrock char cb_mountpoint[MAXPATHLEN]; 34782082Seschrock const char *cb_target; 34792082Seschrock const char *cb_errbuf; 34802082Seschrock uint64_t cb_pivot_txg; 34812082Seschrock } promote_data_t; 34822082Seschrock 34832082Seschrock static int 34842082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 34852082Seschrock { 34862082Seschrock promote_data_t *pd = data; 34872082Seschrock zfs_handle_t *szhp; 34882082Seschrock char snapname[MAXPATHLEN]; 34893265Sahrens int rv = 0; 34902082Seschrock 34912082Seschrock /* We don't care about snapshots after the pivot point */ 34923265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 34933265Sahrens zfs_close(zhp); 34942082Seschrock return (0); 34953265Sahrens } 34962082Seschrock 34972417Sahrens /* Remove the device link if it's a zvol. */ 34982676Seschrock if (ZFS_IS_VOLUME(zhp)) 34992417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 35002082Seschrock 35012082Seschrock /* Check for conflicting names */ 35022676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 35032676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 35042082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 35052082Seschrock if (szhp != NULL) { 35062082Seschrock zfs_close(szhp); 35072082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 35082082Seschrock "snapshot name '%s' from origin \n" 35092082Seschrock "conflicts with '%s' from target"), 35102082Seschrock zhp->zfs_name, snapname); 35113265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 35122082Seschrock } 35133265Sahrens zfs_close(zhp); 35143265Sahrens return (rv); 35152082Seschrock } 35162082Seschrock 35172417Sahrens static int 35182417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 35192417Sahrens { 35202417Sahrens promote_data_t *pd = data; 35212417Sahrens 35222417Sahrens /* We don't care about snapshots after the pivot point */ 35233265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 35243265Sahrens /* Create the device link if it's a zvol. */ 35253265Sahrens if (ZFS_IS_VOLUME(zhp)) 35263265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 35273265Sahrens } 35283265Sahrens 35293265Sahrens zfs_close(zhp); 35302417Sahrens return (0); 35312417Sahrens } 35322417Sahrens 35332082Seschrock /* 35342082Seschrock * Promotes the given clone fs to be the clone parent. 35352082Seschrock */ 35362082Seschrock int 35372082Seschrock zfs_promote(zfs_handle_t *zhp) 35382082Seschrock { 35392082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 35402082Seschrock zfs_cmd_t zc = { 0 }; 35412082Seschrock char parent[MAXPATHLEN]; 35422082Seschrock char *cp; 35432082Seschrock int ret; 35442082Seschrock zfs_handle_t *pzhp; 35452082Seschrock promote_data_t pd; 35462082Seschrock char errbuf[1024]; 35472082Seschrock 35482082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35492082Seschrock "cannot promote '%s'"), zhp->zfs_name); 35502082Seschrock 35512082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 35522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35532082Seschrock "snapshots can not be promoted")); 35542082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 35552082Seschrock } 35562082Seschrock 35575367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 35582082Seschrock if (parent[0] == '\0') { 35592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35602082Seschrock "not a cloned filesystem")); 35612082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 35622082Seschrock } 35632082Seschrock cp = strchr(parent, '@'); 35642082Seschrock *cp = '\0'; 35652082Seschrock 35662082Seschrock /* Walk the snapshots we will be moving */ 35675367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 35682082Seschrock if (pzhp == NULL) 35692082Seschrock return (-1); 35702082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 35712082Seschrock zfs_close(pzhp); 35722082Seschrock pd.cb_target = zhp->zfs_name; 35732082Seschrock pd.cb_errbuf = errbuf; 35745094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 35752082Seschrock if (pzhp == NULL) 35762082Seschrock return (-1); 35772082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 35782082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 35792082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 35802417Sahrens if (ret != 0) { 35812417Sahrens zfs_close(pzhp); 35822082Seschrock return (-1); 35832417Sahrens } 35842082Seschrock 35852082Seschrock /* issue the ioctl */ 35865367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 35872676Seschrock sizeof (zc.zc_value)); 35882082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35894543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 35902082Seschrock 35912082Seschrock if (ret != 0) { 35922417Sahrens int save_errno = errno; 35932417Sahrens 35942417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 35952417Sahrens zfs_close(pzhp); 35962417Sahrens 35972417Sahrens switch (save_errno) { 3598789Sahrens case EEXIST: 3599789Sahrens /* 36002082Seschrock * There is a conflicting snapshot name. We 36012082Seschrock * should have caught this above, but they could 36022082Seschrock * have renamed something in the mean time. 3603789Sahrens */ 36042082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36052082Seschrock "conflicting snapshot name from parent '%s'"), 36062082Seschrock parent); 36072082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3608789Sahrens 3609789Sahrens default: 36102417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3611789Sahrens } 36122417Sahrens } else { 36132417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3614789Sahrens } 3615789Sahrens 36162417Sahrens zfs_close(pzhp); 3617789Sahrens return (ret); 3618789Sahrens } 3619789Sahrens 36204007Smmusante struct createdata { 36214007Smmusante const char *cd_snapname; 36224007Smmusante int cd_ifexists; 36234007Smmusante }; 36244007Smmusante 36252199Sahrens static int 36262199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 36272199Sahrens { 36284007Smmusante struct createdata *cd = arg; 36292676Seschrock int ret; 36302199Sahrens 36312199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 36322199Sahrens char name[MAXPATHLEN]; 36332199Sahrens 36342676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 36352676Seschrock (void) strlcat(name, "@", sizeof (name)); 36364007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 36374007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 36384007Smmusante cd->cd_ifexists); 36392199Sahrens /* 36402199Sahrens * NB: this is simply a best-effort. We don't want to 36412199Sahrens * return an error, because then we wouldn't visit all 36422199Sahrens * the volumes. 36432199Sahrens */ 36442199Sahrens } 36452676Seschrock 36464007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 36472676Seschrock 36482676Seschrock zfs_close(zhp); 36492676Seschrock 36502676Seschrock return (ret); 36512199Sahrens } 36522199Sahrens 3653789Sahrens /* 36543504Sahl * Takes a snapshot of the given dataset. 3655789Sahrens */ 3656789Sahrens int 36577265Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 36587265Sahrens nvlist_t *props) 3659789Sahrens { 3660789Sahrens const char *delim; 36617265Sahrens char parent[ZFS_MAXNAMELEN]; 3662789Sahrens zfs_handle_t *zhp; 3663789Sahrens zfs_cmd_t zc = { 0 }; 3664789Sahrens int ret; 36652082Seschrock char errbuf[1024]; 36662082Seschrock 36672082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36682082Seschrock "cannot snapshot '%s'"), path); 36692082Seschrock 36702082Seschrock /* validate the target name */ 36715326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 36722082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3673789Sahrens 36747265Sahrens if (props) { 36757265Sahrens if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 36767265Sahrens props, B_FALSE, NULL, errbuf)) == NULL) 36777265Sahrens return (-1); 36787265Sahrens 36797265Sahrens if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 36807265Sahrens nvlist_free(props); 36817265Sahrens return (-1); 36827265Sahrens } 36837265Sahrens 36847265Sahrens nvlist_free(props); 36857265Sahrens } 36867265Sahrens 3687789Sahrens /* make sure the parent exists and is of the appropriate type */ 36882199Sahrens delim = strchr(path, '@'); 3689789Sahrens (void) strncpy(parent, path, delim - path); 3690789Sahrens parent[delim - path] = '\0'; 3691789Sahrens 36922082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3693789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 36947265Sahrens zcmd_free_nvlists(&zc); 3695789Sahrens return (-1); 3696789Sahrens } 3697789Sahrens 36982199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 36992676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 37004543Smarks if (ZFS_IS_VOLUME(zhp)) 37014543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 37024543Smarks else 37034543Smarks zc.zc_objset_type = DMU_OST_ZFS; 37042199Sahrens zc.zc_cookie = recursive; 37054543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 37062199Sahrens 37077265Sahrens zcmd_free_nvlists(&zc); 37087265Sahrens 37092199Sahrens /* 37102199Sahrens * if it was recursive, the one that actually failed will be in 37112199Sahrens * zc.zc_name. 37122199Sahrens */ 37134543Smarks if (ret != 0) 37144543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37154543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 37164543Smarks 37172199Sahrens if (ret == 0 && recursive) { 37184007Smmusante struct createdata cd; 37194007Smmusante 37204007Smmusante cd.cd_snapname = delim + 1; 37214007Smmusante cd.cd_ifexists = B_FALSE; 37224007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 37232199Sahrens } 3724789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 37252082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 37262199Sahrens if (ret != 0) { 37274543Smarks (void) zfs_standard_error(hdl, errno, 37284543Smarks dgettext(TEXT_DOMAIN, 37294543Smarks "Volume successfully snapshotted, but device links " 37304543Smarks "were not created")); 37314543Smarks zfs_close(zhp); 37324543Smarks return (-1); 37332199Sahrens } 3734789Sahrens } 3735789Sahrens 37362082Seschrock if (ret != 0) 37372082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3738789Sahrens 3739789Sahrens zfs_close(zhp); 3740789Sahrens 3741789Sahrens return (ret); 3742789Sahrens } 3743789Sahrens 3744789Sahrens /* 37451294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 37461294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 37471294Slling * is a dependent and we should just destroy it without checking the transaction 37481294Slling * group. 3749789Sahrens */ 37501294Slling typedef struct rollback_data { 37511294Slling const char *cb_target; /* the snapshot */ 37521294Slling uint64_t cb_create; /* creation time reference */ 37535749Sahrens boolean_t cb_error; 37542082Seschrock boolean_t cb_dependent; 37555749Sahrens boolean_t cb_force; 37561294Slling } rollback_data_t; 37571294Slling 37581294Slling static int 37591294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 37601294Slling { 37611294Slling rollback_data_t *cbp = data; 37621294Slling 37631294Slling if (!cbp->cb_dependent) { 37641294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 37651294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 37661294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 37671294Slling cbp->cb_create) { 37684543Smarks char *logstr; 37691294Slling 37702082Seschrock cbp->cb_dependent = B_TRUE; 37715446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 37725446Sahrens rollback_destroy, cbp); 37732082Seschrock cbp->cb_dependent = B_FALSE; 37741294Slling 37754543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 37764543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 37775446Sahrens cbp->cb_error |= zfs_destroy(zhp); 37784543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 37791294Slling } 37801294Slling } else { 37815749Sahrens /* We must destroy this clone; first unmount it */ 37825749Sahrens prop_changelist_t *clp; 37835749Sahrens 37847366STim.Haley@Sun.COM clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 37855749Sahrens cbp->cb_force ? MS_FORCE: 0); 37865749Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 37875749Sahrens cbp->cb_error = B_TRUE; 37885749Sahrens zfs_close(zhp); 37895749Sahrens return (0); 37905749Sahrens } 37915749Sahrens if (zfs_destroy(zhp) != 0) 37925749Sahrens cbp->cb_error = B_TRUE; 37935749Sahrens else 37945749Sahrens changelist_remove(clp, zhp->zfs_name); 37955751Sahrens (void) changelist_postfix(clp); 37965749Sahrens changelist_free(clp); 37971294Slling } 37981294Slling 37991294Slling zfs_close(zhp); 38001294Slling return (0); 38011294Slling } 38021294Slling 38031294Slling /* 38045446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 38055446Sahrens * data changes since then and making it the active dataset. 38065446Sahrens * 38075446Sahrens * Any snapshots more recent than the target are destroyed, along with 38085446Sahrens * their dependents. 38091294Slling */ 38105446Sahrens int 38115749Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3812789Sahrens { 38135446Sahrens rollback_data_t cb = { 0 }; 38145446Sahrens int err; 3815789Sahrens zfs_cmd_t zc = { 0 }; 38165713Srm160521 boolean_t restore_resv = 0; 38175713Srm160521 uint64_t old_volsize, new_volsize; 38185713Srm160521 zfs_prop_t resv_prop; 3819789Sahrens 3820789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3821789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3822789Sahrens 38235446Sahrens /* 38245446Sahrens * Destroy all recent snapshots and its dependends. 38255446Sahrens */ 38265749Sahrens cb.cb_force = force; 38275446Sahrens cb.cb_target = snap->zfs_name; 38285446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 38295446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 38305446Sahrens 38315749Sahrens if (cb.cb_error) 38325749Sahrens return (-1); 38335446Sahrens 38345446Sahrens /* 38355446Sahrens * Now that we have verified that the snapshot is the latest, 38365446Sahrens * rollback to the given snapshot. 38375446Sahrens */ 38385446Sahrens 38395713Srm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 38405713Srm160521 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 38415713Srm160521 return (-1); 38425713Srm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 38435713Srm160521 return (-1); 38445713Srm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 38455713Srm160521 restore_resv = 38465713Srm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 38475713Srm160521 } 3848789Sahrens 3849789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3850789Sahrens 38512676Seschrock if (ZFS_IS_VOLUME(zhp)) 3852789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3853789Sahrens else 3854789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3855789Sahrens 3856789Sahrens /* 38575446Sahrens * We rely on zfs_iter_children() to verify that there are no 38585446Sahrens * newer snapshots for the given dataset. Therefore, we can 38595446Sahrens * simply pass the name on to the ioctl() call. There is still 38605446Sahrens * an unlikely race condition where the user has taken a 38615446Sahrens * snapshot since we verified that this was the most recent. 38625713Srm160521 * 3863789Sahrens */ 38645446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 38653237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 38662082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 38672082Seschrock zhp->zfs_name); 38685717Srm160521 return (err); 38695717Srm160521 } 38705713Srm160521 38715713Srm160521 /* 38725713Srm160521 * For volumes, if the pre-rollback volsize matched the pre- 38735713Srm160521 * rollback reservation and the volsize has changed then set 38745713Srm160521 * the reservation property to the post-rollback volsize. 38755713Srm160521 * Make a new handle since the rollback closed the dataset. 38765713Srm160521 */ 38775717Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 38785717Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 38795717Srm160521 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) { 38805717Srm160521 zfs_close(zhp); 38815713Srm160521 return (err); 38825717Srm160521 } 38835713Srm160521 if (restore_resv) { 38845713Srm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 38855713Srm160521 if (old_volsize != new_volsize) 38865717Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 38875717Srm160521 new_volsize); 38885713Srm160521 } 38895713Srm160521 zfs_close(zhp); 3890789Sahrens } 38915446Sahrens return (err); 38921294Slling } 38931294Slling 38941294Slling /* 3895789Sahrens * Iterate over all dependents for a given dataset. This includes both 3896789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3897789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3898789Sahrens * libzfs_graph.c. 3899789Sahrens */ 3900789Sahrens int 39012474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 39022474Seschrock zfs_iter_f func, void *data) 3903789Sahrens { 3904789Sahrens char **dependents; 3905789Sahrens size_t count; 3906789Sahrens int i; 3907789Sahrens zfs_handle_t *child; 3908789Sahrens int ret = 0; 3909789Sahrens 39102474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 39112474Seschrock &dependents, &count) != 0) 39122474Seschrock return (-1); 39132474Seschrock 3914789Sahrens for (i = 0; i < count; i++) { 39152082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 39162082Seschrock dependents[i])) == NULL) 3917789Sahrens continue; 3918789Sahrens 3919789Sahrens if ((ret = func(child, data)) != 0) 3920789Sahrens break; 3921789Sahrens } 3922789Sahrens 3923789Sahrens for (i = 0; i < count; i++) 3924789Sahrens free(dependents[i]); 3925789Sahrens free(dependents); 3926789Sahrens 3927789Sahrens return (ret); 3928789Sahrens } 3929789Sahrens 3930789Sahrens /* 3931789Sahrens * Renames the given dataset. 3932789Sahrens */ 3933789Sahrens int 39344490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3935789Sahrens { 3936789Sahrens int ret; 3937789Sahrens zfs_cmd_t zc = { 0 }; 3938789Sahrens char *delim; 39394007Smmusante prop_changelist_t *cl = NULL; 39404007Smmusante zfs_handle_t *zhrp = NULL; 39414007Smmusante char *parentname = NULL; 3942789Sahrens char parent[ZFS_MAXNAMELEN]; 39432082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 39442082Seschrock char errbuf[1024]; 3945789Sahrens 3946789Sahrens /* if we have the same exact name, just return success */ 3947789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3948789Sahrens return (0); 3949789Sahrens 39502082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 39512082Seschrock "cannot rename to '%s'"), target); 39522082Seschrock 3953789Sahrens /* 3954789Sahrens * Make sure the target name is valid 3955789Sahrens */ 3956789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 39572665Snd150628 if ((strchr(target, '@') == NULL) || 39582665Snd150628 *target == '@') { 39592665Snd150628 /* 39602665Snd150628 * Snapshot target name is abbreviated, 39612665Snd150628 * reconstruct full dataset name 39622665Snd150628 */ 39632665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 39642665Snd150628 sizeof (parent)); 39652665Snd150628 delim = strchr(parent, '@'); 39662665Snd150628 if (strchr(target, '@') == NULL) 39672665Snd150628 *(++delim) = '\0'; 39682665Snd150628 else 39692665Snd150628 *delim = '\0'; 39702665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 39712665Snd150628 target = parent; 39722665Snd150628 } else { 39732665Snd150628 /* 39742665Snd150628 * Make sure we're renaming within the same dataset. 39752665Snd150628 */ 39762665Snd150628 delim = strchr(target, '@'); 39772665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 39782665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 39792665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 39802665Snd150628 "snapshots must be part of same " 39812665Snd150628 "dataset")); 39822665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 39833912Slling errbuf)); 39842665Snd150628 } 3985789Sahrens } 39865326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 39872665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3988789Sahrens } else { 39894007Smmusante if (recursive) { 39904007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 39914007Smmusante "recursive rename must be a snapshot")); 39924007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 39934007Smmusante } 39944007Smmusante 39955326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 39962665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 39972676Seschrock uint64_t unused; 39982676Seschrock 3999789Sahrens /* validate parents */ 40004490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 4001789Sahrens return (-1); 4002789Sahrens 4003789Sahrens (void) parent_name(target, parent, sizeof (parent)); 4004789Sahrens 4005789Sahrens /* make sure we're in the same pool */ 4006789Sahrens verify((delim = strchr(target, '/')) != NULL); 4007789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 4008789Sahrens zhp->zfs_name[delim - target] != '/') { 40092082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40102082Seschrock "datasets must be within same pool")); 40112082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 4012789Sahrens } 40132440Snd150628 40142440Snd150628 /* new name cannot be a child of the current dataset name */ 40152440Snd150628 if (strncmp(parent, zhp->zfs_name, 40163912Slling strlen(zhp->zfs_name)) == 0) { 40172440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40182440Snd150628 "New dataset name cannot be a descendent of " 40192440Snd150628 "current dataset name")); 40202440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 40212440Snd150628 } 4022789Sahrens } 4023789Sahrens 40242082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 40252082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 40262082Seschrock 4027789Sahrens if (getzoneid() == GLOBAL_ZONEID && 4028789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 40292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40302082Seschrock "dataset is used in a non-global zone")); 40312082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 4032789Sahrens } 4033789Sahrens 40344007Smmusante if (recursive) { 40354007Smmusante struct destroydata dd; 40364007Smmusante 40374183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 40384183Smmusante if (parentname == NULL) { 40394183Smmusante ret = -1; 40404183Smmusante goto error; 40414183Smmusante } 40424007Smmusante delim = strchr(parentname, '@'); 40434007Smmusante *delim = '\0'; 40445094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 40454007Smmusante if (zhrp == NULL) { 40464183Smmusante ret = -1; 40474183Smmusante goto error; 40484007Smmusante } 40494007Smmusante 40504007Smmusante dd.snapname = delim + 1; 40514007Smmusante dd.gotone = B_FALSE; 40524183Smmusante dd.closezhp = B_TRUE; 40534007Smmusante 40544007Smmusante /* We remove any zvol links prior to renaming them */ 40554007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 40564007Smmusante if (ret) { 40574007Smmusante goto error; 40584007Smmusante } 40594007Smmusante } else { 40607366STim.Haley@Sun.COM if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL) 40614007Smmusante return (-1); 40624007Smmusante 40634007Smmusante if (changelist_haszonedchild(cl)) { 40644007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40654007Smmusante "child dataset with inherited mountpoint is used " 40664007Smmusante "in a non-global zone")); 40674007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 40684007Smmusante goto error; 40694007Smmusante } 40704007Smmusante 40714007Smmusante if ((ret = changelist_prefix(cl)) != 0) 40724007Smmusante goto error; 4073789Sahrens } 4074789Sahrens 40752676Seschrock if (ZFS_IS_VOLUME(zhp)) 4076789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 4077789Sahrens else 4078789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 4079789Sahrens 40802665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 40812676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 40822665Snd150628 40834007Smmusante zc.zc_cookie = recursive; 40844007Smmusante 40854543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 40864007Smmusante /* 40874007Smmusante * if it was recursive, the one that actually failed will 40884007Smmusante * be in zc.zc_name 40894007Smmusante */ 40904007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 40915367Sahrens "cannot rename '%s'"), zc.zc_name); 40924007Smmusante 40934007Smmusante if (recursive && errno == EEXIST) { 40944007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 40954007Smmusante "a child dataset already has a snapshot " 40964007Smmusante "with the new name")); 40974801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 40984007Smmusante } else { 40994007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 41004007Smmusante } 4101789Sahrens 4102789Sahrens /* 4103789Sahrens * On failure, we still want to remount any filesystems that 4104789Sahrens * were previously mounted, so we don't alter the system state. 4105789Sahrens */ 41064007Smmusante if (recursive) { 41074007Smmusante struct createdata cd; 41084007Smmusante 41094007Smmusante /* only create links for datasets that had existed */ 41104007Smmusante cd.cd_snapname = delim + 1; 41114007Smmusante cd.cd_ifexists = B_TRUE; 41124007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41134007Smmusante &cd); 41144007Smmusante } else { 41154007Smmusante (void) changelist_postfix(cl); 41164007Smmusante } 4117789Sahrens } else { 41184007Smmusante if (recursive) { 41194007Smmusante struct createdata cd; 41204007Smmusante 41214007Smmusante /* only create links for datasets that had existed */ 41224007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 41234007Smmusante cd.cd_ifexists = B_TRUE; 41244007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 41254007Smmusante &cd); 41264007Smmusante } else { 41274007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 41284007Smmusante ret = changelist_postfix(cl); 41294007Smmusante } 4130789Sahrens } 4131789Sahrens 4132789Sahrens error: 41334007Smmusante if (parentname) { 41344007Smmusante free(parentname); 41354007Smmusante } 41364007Smmusante if (zhrp) { 41374007Smmusante zfs_close(zhrp); 41384007Smmusante } 41394007Smmusante if (cl) { 41404007Smmusante changelist_free(cl); 41414007Smmusante } 4142789Sahrens return (ret); 4143789Sahrens } 4144789Sahrens 4145789Sahrens /* 4146789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 4147789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 4148789Sahrens */ 4149789Sahrens int 41502082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 4151789Sahrens { 41524007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 41534007Smmusante } 41544007Smmusante 41554007Smmusante static int 41564007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 41574007Smmusante { 4158789Sahrens zfs_cmd_t zc = { 0 }; 41592082Seschrock di_devlink_handle_t dhdl; 41604543Smarks priv_set_t *priv_effective; 41614543Smarks int privileged; 4162789Sahrens 4163789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4164789Sahrens 4165789Sahrens /* 4166789Sahrens * Issue the appropriate ioctl. 4167789Sahrens */ 41682082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 4169789Sahrens switch (errno) { 4170789Sahrens case EEXIST: 4171789Sahrens /* 4172789Sahrens * Silently ignore the case where the link already 4173789Sahrens * exists. This allows 'zfs volinit' to be run multiple 4174789Sahrens * times without errors. 4175789Sahrens */ 4176789Sahrens return (0); 4177789Sahrens 41784007Smmusante case ENOENT: 41794007Smmusante /* 41804007Smmusante * Dataset does not exist in the kernel. If we 41814007Smmusante * don't care (see zfs_rename), then ignore the 41824007Smmusante * error quietly. 41834007Smmusante */ 41844007Smmusante if (ifexists) { 41854007Smmusante return (0); 41864007Smmusante } 41874007Smmusante 41884007Smmusante /* FALLTHROUGH */ 41894007Smmusante 4190789Sahrens default: 41913237Slling return (zfs_standard_error_fmt(hdl, errno, 41922082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 41932082Seschrock "for '%s'"), dataset)); 4194789Sahrens } 4195789Sahrens } 4196789Sahrens 4197789Sahrens /* 41984543Smarks * If privileged call devfsadm and wait for the links to 41994543Smarks * magically appear. 42004543Smarks * Otherwise, print out an informational message. 4201789Sahrens */ 42024543Smarks 42034543Smarks priv_effective = priv_allocset(); 42044543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 42054543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 42064543Smarks priv_freeset(priv_effective); 42074543Smarks 42084543Smarks if (privileged) { 42094543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 42104543Smarks DI_MAKE_LINK)) == NULL) { 42114543Smarks zfs_error_aux(hdl, strerror(errno)); 42127301SEric.Taylor@Sun.COM (void) zfs_error_fmt(hdl, errno, 42134543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 42144543Smarks "for '%s'"), dataset); 42154543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 42164543Smarks return (-1); 42174543Smarks } else { 42184543Smarks (void) di_devlink_fini(&dhdl); 42194543Smarks } 4220789Sahrens } else { 42214543Smarks char pathname[MAXPATHLEN]; 42224543Smarks struct stat64 statbuf; 42234543Smarks int i; 42244543Smarks 42254543Smarks #define MAX_WAIT 10 42264543Smarks 42274543Smarks /* 42284543Smarks * This is the poor mans way of waiting for the link 42294543Smarks * to show up. If after 10 seconds we still don't 42304543Smarks * have it, then print out a message. 42314543Smarks */ 42324543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 42334543Smarks dataset); 42344543Smarks 42354543Smarks for (i = 0; i != MAX_WAIT; i++) { 42364543Smarks if (stat64(pathname, &statbuf) == 0) 42374543Smarks break; 42384543Smarks (void) sleep(1); 42394543Smarks } 42404543Smarks if (i == MAX_WAIT) 42414543Smarks (void) printf(gettext("%s may not be immediately " 42424543Smarks "available\n"), pathname); 4243789Sahrens } 4244789Sahrens 4245789Sahrens return (0); 4246789Sahrens } 4247789Sahrens 4248789Sahrens /* 4249789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4250789Sahrens */ 4251789Sahrens int 42522082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4253789Sahrens { 4254789Sahrens zfs_cmd_t zc = { 0 }; 4255789Sahrens 4256789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4257789Sahrens 42582082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4259789Sahrens switch (errno) { 4260789Sahrens case ENXIO: 4261789Sahrens /* 4262789Sahrens * Silently ignore the case where the link no longer 4263789Sahrens * exists, so that 'zfs volfini' can be run multiple 4264789Sahrens * times without errors. 4265789Sahrens */ 4266789Sahrens return (0); 4267789Sahrens 4268789Sahrens default: 42693237Slling return (zfs_standard_error_fmt(hdl, errno, 42702082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 42712082Seschrock "links for '%s'"), dataset)); 4272789Sahrens } 4273789Sahrens } 4274789Sahrens 4275789Sahrens return (0); 4276789Sahrens } 42772676Seschrock 42782676Seschrock nvlist_t * 42792676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 42802676Seschrock { 42812676Seschrock return (zhp->zfs_user_props); 42822676Seschrock } 42832676Seschrock 42842676Seschrock /* 42853912Slling * This function is used by 'zfs list' to determine the exact set of columns to 42863912Slling * display, and their maximum widths. This does two main things: 42873912Slling * 42883912Slling * - If this is a list of all properties, then expand the list to include 42893912Slling * all native properties, and set a flag so that for each dataset we look 42903912Slling * for new unique user properties and add them to the list. 42913912Slling * 42923912Slling * - For non fixed-width properties, keep track of the maximum width seen 42933912Slling * so that we can size the column appropriately. 42943912Slling */ 42953912Slling int 42965094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 42973912Slling { 42983912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 42995094Slling zprop_list_t *entry; 43005094Slling zprop_list_t **last, **start; 43013912Slling nvlist_t *userprops, *propval; 43023912Slling nvpair_t *elem; 43033912Slling char *strval; 43043912Slling char buf[ZFS_MAXPROPLEN]; 43053912Slling 43065094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 43073912Slling return (-1); 43082676Seschrock 43092676Seschrock userprops = zfs_get_user_props(zhp); 43102676Seschrock 43112676Seschrock entry = *plp; 43122676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 43132676Seschrock /* 43142676Seschrock * Go through and add any user properties as necessary. We 43152676Seschrock * start by incrementing our list pointer to the first 43162676Seschrock * non-native property. 43172676Seschrock */ 43182676Seschrock start = plp; 43192676Seschrock while (*start != NULL) { 43205094Slling if ((*start)->pl_prop == ZPROP_INVAL) 43212676Seschrock break; 43222676Seschrock start = &(*start)->pl_next; 43232676Seschrock } 43242676Seschrock 43252676Seschrock elem = NULL; 43262676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 43272676Seschrock /* 43282676Seschrock * See if we've already found this property in our list. 43292676Seschrock */ 43302676Seschrock for (last = start; *last != NULL; 43312676Seschrock last = &(*last)->pl_next) { 43322676Seschrock if (strcmp((*last)->pl_user_prop, 43332676Seschrock nvpair_name(elem)) == 0) 43342676Seschrock break; 43352676Seschrock } 43362676Seschrock 43372676Seschrock if (*last == NULL) { 43382676Seschrock if ((entry = zfs_alloc(hdl, 43395094Slling sizeof (zprop_list_t))) == NULL || 43402676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 43412676Seschrock nvpair_name(elem)))) == NULL) { 43422676Seschrock free(entry); 43432676Seschrock return (-1); 43442676Seschrock } 43452676Seschrock 43465094Slling entry->pl_prop = ZPROP_INVAL; 43472676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 43482676Seschrock entry->pl_all = B_TRUE; 43492676Seschrock *last = entry; 43502676Seschrock } 43512676Seschrock } 43522676Seschrock } 43532676Seschrock 43542676Seschrock /* 43552676Seschrock * Now go through and check the width of any non-fixed columns 43562676Seschrock */ 43572676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 43582676Seschrock if (entry->pl_fixed) 43592676Seschrock continue; 43602676Seschrock 43615094Slling if (entry->pl_prop != ZPROP_INVAL) { 43622676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 43632676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 43642676Seschrock if (strlen(buf) > entry->pl_width) 43652676Seschrock entry->pl_width = strlen(buf); 43662676Seschrock } 43672676Seschrock } else if (nvlist_lookup_nvlist(userprops, 43682676Seschrock entry->pl_user_prop, &propval) == 0) { 43692676Seschrock verify(nvlist_lookup_string(propval, 43705094Slling ZPROP_VALUE, &strval) == 0); 43712676Seschrock if (strlen(strval) > entry->pl_width) 43722676Seschrock entry->pl_width = strlen(strval); 43732676Seschrock } 43742676Seschrock } 43752676Seschrock 43762676Seschrock return (0); 43772676Seschrock } 43784543Smarks 43794543Smarks int 43804543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 43814543Smarks { 43824543Smarks zfs_cmd_t zc = { 0 }; 43834543Smarks nvlist_t *nvp; 43844543Smarks gid_t gid; 43854543Smarks uid_t uid; 43864543Smarks const gid_t *groups; 43874543Smarks int group_cnt; 43884543Smarks int error; 43894543Smarks 43904543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 43914543Smarks return (no_memory(hdl)); 43924543Smarks 43934543Smarks uid = ucred_geteuid(cred); 43944543Smarks gid = ucred_getegid(cred); 43954543Smarks group_cnt = ucred_getgroups(cred, &groups); 43964543Smarks 43974543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 43984543Smarks return (1); 43994543Smarks 44004543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 44014543Smarks nvlist_free(nvp); 44024543Smarks return (1); 44034543Smarks } 44044543Smarks 44054543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 44064543Smarks nvlist_free(nvp); 44074543Smarks return (1); 44084543Smarks } 44094543Smarks 44104543Smarks if (nvlist_add_uint32_array(nvp, 44114543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 44124543Smarks nvlist_free(nvp); 44134543Smarks return (1); 44144543Smarks } 44154543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 44164543Smarks 44175094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 44184543Smarks return (-1); 44194543Smarks 44204543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 44214543Smarks nvlist_free(nvp); 44224543Smarks return (error); 44234543Smarks } 44244543Smarks 44254543Smarks int 44264543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 44275331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 44284543Smarks { 44294543Smarks zfs_cmd_t zc = { 0 }; 44304543Smarks int error; 44314543Smarks 44324543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 44334543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 44344543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 44354543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 44365331Samw zc.zc_share.z_sharetype = operation; 44374543Smarks zc.zc_share.z_sharemax = sharemax; 44384543Smarks 44394543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 44404543Smarks return (error); 44414543Smarks } 4442