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 /* 233363Sgw25295 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 29789Sahrens #include <assert.h> 30789Sahrens #include <ctype.h> 31789Sahrens #include <errno.h> 32789Sahrens #include <libdevinfo.h> 33789Sahrens #include <libintl.h> 34789Sahrens #include <math.h> 35789Sahrens #include <stdio.h> 36789Sahrens #include <stdlib.h> 37789Sahrens #include <strings.h> 38789Sahrens #include <unistd.h> 395367Sahrens #include <stddef.h> 40789Sahrens #include <zone.h> 412082Seschrock #include <fcntl.h> 42789Sahrens #include <sys/mntent.h> 43789Sahrens #include <sys/mnttab.h> 441294Slling #include <sys/mount.h> 454543Smarks #include <sys/avl.h> 464543Smarks #include <priv.h> 474543Smarks #include <pwd.h> 484543Smarks #include <grp.h> 494543Smarks #include <stddef.h> 504543Smarks #include <ucred.h> 51789Sahrens 52789Sahrens #include <sys/spa.h> 532676Seschrock #include <sys/zap.h> 545331Samw #include <sys/zfs_i18n.h> 55789Sahrens #include <libzfs.h> 56789Sahrens 57789Sahrens #include "zfs_namecheck.h" 58789Sahrens #include "zfs_prop.h" 59789Sahrens #include "libzfs_impl.h" 604543Smarks #include "zfs_deleg.h" 61789Sahrens 624007Smmusante static int zvol_create_link_common(libzfs_handle_t *, const char *, int); 634007Smmusante 64789Sahrens /* 65789Sahrens * Given a single type (not a mask of types), return the type in a human 66789Sahrens * readable form. 67789Sahrens */ 68789Sahrens const char * 69789Sahrens zfs_type_to_name(zfs_type_t type) 70789Sahrens { 71789Sahrens switch (type) { 72789Sahrens case ZFS_TYPE_FILESYSTEM: 73789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 74789Sahrens case ZFS_TYPE_SNAPSHOT: 75789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 76789Sahrens case ZFS_TYPE_VOLUME: 77789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 78789Sahrens } 79789Sahrens 80789Sahrens return (NULL); 81789Sahrens } 82789Sahrens 83789Sahrens /* 84789Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 85789Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 86789Sahrens * We guess what the type would have been based on the path and the mask of 87789Sahrens * acceptable types. 88789Sahrens */ 89789Sahrens static const char * 90789Sahrens path_to_str(const char *path, int types) 91789Sahrens { 92789Sahrens /* 93789Sahrens * When given a single type, always report the exact type. 94789Sahrens */ 95789Sahrens if (types == ZFS_TYPE_SNAPSHOT) 96789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 97789Sahrens if (types == ZFS_TYPE_FILESYSTEM) 98789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 99789Sahrens if (types == ZFS_TYPE_VOLUME) 100789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 101789Sahrens 102789Sahrens /* 103789Sahrens * The user is requesting more than one type of dataset. If this is the 104789Sahrens * case, consult the path itself. If we're looking for a snapshot, and 105789Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 106789Sahrens * snapshot attribute and try again. 107789Sahrens */ 108789Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 109789Sahrens if (strchr(path, '@') != NULL) 110789Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 111789Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 112789Sahrens } 113789Sahrens 114789Sahrens 115789Sahrens /* 116789Sahrens * The user has requested either filesystems or volumes. 117789Sahrens * We have no way of knowing a priori what type this would be, so always 118789Sahrens * report it as "filesystem" or "volume", our two primitive types. 119789Sahrens */ 120789Sahrens if (types & ZFS_TYPE_FILESYSTEM) 121789Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 122789Sahrens 123789Sahrens assert(types & ZFS_TYPE_VOLUME); 124789Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 125789Sahrens } 126789Sahrens 127789Sahrens /* 128789Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 129789Sahrens * provide a more meaningful error message. We place a more useful message in 130789Sahrens * 'buf' detailing exactly why the name was not valid. 131789Sahrens */ 132789Sahrens static int 1335326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 1345326Sek110237 boolean_t modifying) 135789Sahrens { 136789Sahrens namecheck_err_t why; 137789Sahrens char what; 138789Sahrens 139789Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 1402082Seschrock if (hdl != NULL) { 141789Sahrens switch (why) { 1421003Slling case NAME_ERR_TOOLONG: 1432082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1442082Seschrock "name is too long")); 1451003Slling break; 1461003Slling 147789Sahrens case NAME_ERR_LEADING_SLASH: 1482082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1492082Seschrock "leading slash in name")); 150789Sahrens break; 151789Sahrens 152789Sahrens case NAME_ERR_EMPTY_COMPONENT: 1532082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1542082Seschrock "empty component in name")); 155789Sahrens break; 156789Sahrens 157789Sahrens case NAME_ERR_TRAILING_SLASH: 1582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1592082Seschrock "trailing slash in name")); 160789Sahrens break; 161789Sahrens 162789Sahrens case NAME_ERR_INVALCHAR: 1632082Seschrock zfs_error_aux(hdl, 164789Sahrens dgettext(TEXT_DOMAIN, "invalid character " 1652082Seschrock "'%c' in name"), what); 166789Sahrens break; 167789Sahrens 168789Sahrens case NAME_ERR_MULTIPLE_AT: 1692082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1702082Seschrock "multiple '@' delimiters in name")); 171789Sahrens break; 1722856Snd150628 1732856Snd150628 case NAME_ERR_NOLETTER: 1742856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1752856Snd150628 "pool doesn't begin with a letter")); 1762856Snd150628 break; 1772856Snd150628 1782856Snd150628 case NAME_ERR_RESERVED: 1792856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1802856Snd150628 "name is reserved")); 1812856Snd150628 break; 1822856Snd150628 1832856Snd150628 case NAME_ERR_DISKLIKE: 1842856Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1852856Snd150628 "reserved disk name")); 1862856Snd150628 break; 187789Sahrens } 188789Sahrens } 189789Sahrens 190789Sahrens return (0); 191789Sahrens } 192789Sahrens 193789Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 1942082Seschrock if (hdl != NULL) 1952082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1962082Seschrock "snapshot delimiter '@' in filesystem name")); 197789Sahrens return (0); 198789Sahrens } 199789Sahrens 2002199Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 2012199Sahrens if (hdl != NULL) 2022199Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2033413Smmusante "missing '@' delimiter in snapshot name")); 2042199Sahrens return (0); 2052199Sahrens } 2062199Sahrens 2075326Sek110237 if (modifying && strchr(path, '%') != NULL) { 2085326Sek110237 if (hdl != NULL) 2095326Sek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2105326Sek110237 "invalid character %c in name"), '%'); 2115326Sek110237 return (0); 2125326Sek110237 } 2135326Sek110237 2142082Seschrock return (-1); 215789Sahrens } 216789Sahrens 217789Sahrens int 218789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 219789Sahrens { 2205326Sek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 221789Sahrens } 222789Sahrens 223789Sahrens /* 2242676Seschrock * This function takes the raw DSL properties, and filters out the user-defined 2252676Seschrock * properties into a separate nvlist. 2262676Seschrock */ 2274217Seschrock static nvlist_t * 2284217Seschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 2292676Seschrock { 2302676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2312676Seschrock nvpair_t *elem; 2322676Seschrock nvlist_t *propval; 2334217Seschrock nvlist_t *nvl; 2344217Seschrock 2354217Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2364217Seschrock (void) no_memory(hdl); 2374217Seschrock return (NULL); 2384217Seschrock } 2392676Seschrock 2402676Seschrock elem = NULL; 2414217Seschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 2422676Seschrock if (!zfs_prop_user(nvpair_name(elem))) 2432676Seschrock continue; 2442676Seschrock 2452676Seschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 2464217Seschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 2474217Seschrock nvlist_free(nvl); 2484217Seschrock (void) no_memory(hdl); 2494217Seschrock return (NULL); 2504217Seschrock } 2512676Seschrock } 2522676Seschrock 2534217Seschrock return (nvl); 2542676Seschrock } 2552676Seschrock 2562676Seschrock /* 257789Sahrens * Utility function to gather stats (objset and zpl) for the given object. 258789Sahrens */ 259789Sahrens static int 260789Sahrens get_stats(zfs_handle_t *zhp) 261789Sahrens { 262789Sahrens zfs_cmd_t zc = { 0 }; 2632676Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 2644217Seschrock nvlist_t *allprops, *userprops; 265789Sahrens 266789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 267789Sahrens 2682676Seschrock if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 2692082Seschrock return (-1); 2701356Seschrock 2712082Seschrock while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { 2721356Seschrock if (errno == ENOMEM) { 2732676Seschrock if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 2742676Seschrock zcmd_free_nvlists(&zc); 2752082Seschrock return (-1); 2762676Seschrock } 2771356Seschrock } else { 2782676Seschrock zcmd_free_nvlists(&zc); 2791356Seschrock return (-1); 2801356Seschrock } 2811356Seschrock } 282789Sahrens 2832885Sahrens zhp->zfs_dmustats = zc.zc_objset_stats; /* structure assignment */ 284789Sahrens 2852676Seschrock (void) strlcpy(zhp->zfs_root, zc.zc_value, sizeof (zhp->zfs_root)); 2861544Seschrock 2874217Seschrock if (zcmd_read_dst_nvlist(hdl, &zc, &allprops) != 0) { 2882676Seschrock zcmd_free_nvlists(&zc); 2892082Seschrock return (-1); 2902082Seschrock } 291789Sahrens 2922676Seschrock zcmd_free_nvlists(&zc); 2932676Seschrock 2944217Seschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 2954217Seschrock nvlist_free(allprops); 2962676Seschrock return (-1); 2974217Seschrock } 2984217Seschrock 2994217Seschrock nvlist_free(zhp->zfs_props); 3004217Seschrock nvlist_free(zhp->zfs_user_props); 3014217Seschrock 3024217Seschrock zhp->zfs_props = allprops; 3034217Seschrock zhp->zfs_user_props = userprops; 3042082Seschrock 305789Sahrens return (0); 306789Sahrens } 307789Sahrens 308789Sahrens /* 309789Sahrens * Refresh the properties currently stored in the handle. 310789Sahrens */ 311789Sahrens void 312789Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 313789Sahrens { 314789Sahrens (void) get_stats(zhp); 315789Sahrens } 316789Sahrens 317789Sahrens /* 318789Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 319789Sahrens * zfs_iter_* to create child handles on the fly. 320789Sahrens */ 321789Sahrens zfs_handle_t * 3222082Seschrock make_dataset_handle(libzfs_handle_t *hdl, const char *path) 323789Sahrens { 3242082Seschrock zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 3254543Smarks char *logstr; 3262082Seschrock 3272082Seschrock if (zhp == NULL) 3282082Seschrock return (NULL); 3292082Seschrock 3302082Seschrock zhp->zfs_hdl = hdl; 331789Sahrens 3324543Smarks /* 3334543Smarks * Preserve history log string. 3344543Smarks * any changes performed here will be 3354543Smarks * logged as an internal event. 3364543Smarks */ 3374543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 3384543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 3391758Sahrens top: 340789Sahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 341789Sahrens 342789Sahrens if (get_stats(zhp) != 0) { 3434543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 344789Sahrens free(zhp); 345789Sahrens return (NULL); 346789Sahrens } 347789Sahrens 3481758Sahrens if (zhp->zfs_dmustats.dds_inconsistent) { 3491758Sahrens zfs_cmd_t zc = { 0 }; 3501758Sahrens 3511758Sahrens /* 3521758Sahrens * If it is dds_inconsistent, then we've caught it in 3531758Sahrens * the middle of a 'zfs receive' or 'zfs destroy', and 3541758Sahrens * it is inconsistent from the ZPL's point of view, so 3551758Sahrens * can't be mounted. However, it could also be that we 3561758Sahrens * have crashed in the middle of one of those 3571758Sahrens * operations, in which case we need to get rid of the 3581758Sahrens * inconsistent state. We do that by either rolling 3591758Sahrens * back to the previous snapshot (which will fail if 3601758Sahrens * there is none), or destroying the filesystem. Note 3611758Sahrens * that if we are still in the middle of an active 3621758Sahrens * 'receive' or 'destroy', then the rollback and destroy 3631758Sahrens * will fail with EBUSY and we will drive on as usual. 3641758Sahrens */ 3651758Sahrens 3661758Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3671758Sahrens 3682885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) { 3692082Seschrock (void) zvol_remove_link(hdl, zhp->zfs_name); 3701758Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3711758Sahrens } else { 3721758Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3731758Sahrens } 3741758Sahrens 3751758Sahrens /* 3765331Samw * If we can successfully destroy it, pretend that it 3771758Sahrens * never existed. 3781758Sahrens */ 3792082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 3804543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 3811758Sahrens free(zhp); 3821758Sahrens errno = ENOENT; 3831758Sahrens return (NULL); 3841758Sahrens } 3855367Sahrens /* If we can successfully roll it back, reget the stats */ 3865367Sahrens if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 3875367Sahrens goto top; 3881758Sahrens } 3891758Sahrens 390789Sahrens /* 391789Sahrens * We've managed to open the dataset and gather statistics. Determine 392789Sahrens * the high-level type. 393789Sahrens */ 3942885Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 3952885Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 3962885Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 3972885Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 3982885Sahrens else 3992885Sahrens abort(); 4002885Sahrens 401789Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 402789Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 403789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 404789Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 405789Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 406789Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 407789Sahrens else 4082082Seschrock abort(); /* we should never see any other types */ 409789Sahrens 4104543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 411789Sahrens return (zhp); 412789Sahrens } 413789Sahrens 414789Sahrens /* 415789Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 416789Sahrens * argument is a mask of acceptable types. The function will print an 417789Sahrens * appropriate error message and return NULL if it can't be opened. 418789Sahrens */ 419789Sahrens zfs_handle_t * 4202082Seschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 421789Sahrens { 422789Sahrens zfs_handle_t *zhp; 4232082Seschrock char errbuf[1024]; 4242082Seschrock 4252082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 4262082Seschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 427789Sahrens 428789Sahrens /* 4292082Seschrock * Validate the name before we even try to open it. 430789Sahrens */ 4315326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 4322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4332082Seschrock "invalid dataset name")); 4342082Seschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 435789Sahrens return (NULL); 436789Sahrens } 437789Sahrens 438789Sahrens /* 439789Sahrens * Try to get stats for the dataset, which will tell us if it exists. 440789Sahrens */ 441789Sahrens errno = 0; 4422082Seschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 4433237Slling (void) zfs_standard_error(hdl, errno, errbuf); 444789Sahrens return (NULL); 445789Sahrens } 446789Sahrens 447789Sahrens if (!(types & zhp->zfs_type)) { 4482082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 4492142Seschrock zfs_close(zhp); 450789Sahrens return (NULL); 451789Sahrens } 452789Sahrens 453789Sahrens return (zhp); 454789Sahrens } 455789Sahrens 456789Sahrens /* 457789Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 458789Sahrens */ 459789Sahrens void 460789Sahrens zfs_close(zfs_handle_t *zhp) 461789Sahrens { 462789Sahrens if (zhp->zfs_mntopts) 463789Sahrens free(zhp->zfs_mntopts); 4642676Seschrock nvlist_free(zhp->zfs_props); 4652676Seschrock nvlist_free(zhp->zfs_user_props); 466789Sahrens free(zhp); 467789Sahrens } 468789Sahrens 4693912Slling /* 4702676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 4712676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 4722676Seschrock * strings. 473789Sahrens */ 4745094Slling static nvlist_t * 4755094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 4765094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 477789Sahrens { 4782676Seschrock nvpair_t *elem; 4792676Seschrock uint64_t intval; 4802676Seschrock char *strval; 4815094Slling zfs_prop_t prop; 4822676Seschrock nvlist_t *ret; 4835375Stimh int chosen_sense = -1; 4845331Samw int chosen_normal = -1; 4855331Samw int chosen_utf = -1; 4862676Seschrock 4872676Seschrock if (type == ZFS_TYPE_SNAPSHOT) { 4882676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4893413Smmusante "snapshot properties cannot be modified")); 4902676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 4915094Slling return (NULL); 4925094Slling } 4935094Slling 4945094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 4955094Slling (void) no_memory(hdl); 4965094Slling return (NULL); 497789Sahrens } 498789Sahrens 4992676Seschrock elem = NULL; 5002676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 5015094Slling const char *propname = nvpair_name(elem); 5022676Seschrock 5032676Seschrock /* 5042676Seschrock * Make sure this property is valid and applies to this type. 5052676Seschrock */ 5065094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 5075094Slling if (!zfs_prop_user(propname)) { 5082676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5095094Slling "invalid property '%s'"), propname); 5105094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5115094Slling goto error; 5125094Slling } 5135094Slling 5145094Slling /* 5155094Slling * If this is a user property, make sure it's a 5165094Slling * string, and that it's less than ZAP_MAXNAMELEN. 5175094Slling */ 5185094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 5195094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5205094Slling "'%s' must be a string"), propname); 5215094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5225094Slling goto error; 5235094Slling } 5245094Slling 5255094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 5265094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5275094Slling "property name '%s' is too long"), 5282676Seschrock propname); 5292676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5302676Seschrock goto error; 5312676Seschrock } 5322676Seschrock 5332676Seschrock (void) nvpair_value_string(elem, &strval); 5342676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 5352676Seschrock (void) no_memory(hdl); 5362676Seschrock goto error; 5372676Seschrock } 5382676Seschrock continue; 539789Sahrens } 5402676Seschrock 5412676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 5422676Seschrock zfs_error_aux(hdl, 5432676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 5442676Seschrock "apply to datasets of this type"), propname); 5452676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 5462676Seschrock goto error; 5472676Seschrock } 5482676Seschrock 5492676Seschrock if (zfs_prop_readonly(prop) && 5505331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 5512676Seschrock zfs_error_aux(hdl, 5522676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 5532676Seschrock propname); 5542676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 5552676Seschrock goto error; 5562676Seschrock } 5572676Seschrock 5585094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 5595094Slling &strval, &intval, errbuf) != 0) 5602676Seschrock goto error; 5612676Seschrock 5622676Seschrock /* 5632676Seschrock * Perform some additional checks for specific properties. 5642676Seschrock */ 5652676Seschrock switch (prop) { 5664577Sahrens case ZFS_PROP_VERSION: 5674577Sahrens { 5684577Sahrens int version; 5694577Sahrens 5704577Sahrens if (zhp == NULL) 5714577Sahrens break; 5724577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 5734577Sahrens if (intval < version) { 5744577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5754577Sahrens "Can not downgrade; already at version %u"), 5764577Sahrens version); 5774577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5784577Sahrens goto error; 5794577Sahrens } 5804577Sahrens break; 5814577Sahrens } 5824577Sahrens 5832676Seschrock case ZFS_PROP_RECORDSIZE: 5842676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 5852676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 5862676Seschrock if (intval < SPA_MINBLOCKSIZE || 5872676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 5882082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5892676Seschrock "'%s' must be power of 2 from %u " 5902676Seschrock "to %uk"), propname, 5912676Seschrock (uint_t)SPA_MINBLOCKSIZE, 5922676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 5932676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5942676Seschrock goto error; 595789Sahrens } 596789Sahrens break; 597789Sahrens 5983126Sahl case ZFS_PROP_SHAREISCSI: 5993126Sahl if (strcmp(strval, "off") != 0 && 6003126Sahl strcmp(strval, "on") != 0 && 6013126Sahl strcmp(strval, "type=disk") != 0) { 6023126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6033126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 6043126Sahl propname); 6053126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6063126Sahl goto error; 6073126Sahl } 6083126Sahl 6093126Sahl break; 6103126Sahl 6112676Seschrock case ZFS_PROP_MOUNTPOINT: 6124778Srm160521 { 6134778Srm160521 namecheck_err_t why; 6144778Srm160521 6152676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 6162676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 6172676Seschrock break; 6182676Seschrock 6194778Srm160521 if (mountpoint_namecheck(strval, &why)) { 6204778Srm160521 switch (why) { 6214778Srm160521 case NAME_ERR_LEADING_SLASH: 6224778Srm160521 zfs_error_aux(hdl, 6234778Srm160521 dgettext(TEXT_DOMAIN, 6244778Srm160521 "'%s' must be an absolute path, " 6254778Srm160521 "'none', or 'legacy'"), propname); 6264778Srm160521 break; 6274778Srm160521 case NAME_ERR_TOOLONG: 6284778Srm160521 zfs_error_aux(hdl, 6294778Srm160521 dgettext(TEXT_DOMAIN, 6304778Srm160521 "component of '%s' is too long"), 6314778Srm160521 propname); 6324778Srm160521 break; 6334778Srm160521 } 6342676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6352676Seschrock goto error; 636789Sahrens } 6374778Srm160521 } 6384778Srm160521 6393126Sahl /*FALLTHRU*/ 6403126Sahl 6415331Samw case ZFS_PROP_SHARESMB: 6423126Sahl case ZFS_PROP_SHARENFS: 6433126Sahl /* 6445331Samw * For the mountpoint and sharenfs or sharesmb 6455331Samw * properties, check if it can be set in a 6465331Samw * global/non-global zone based on 6473126Sahl * the zoned property value: 6483126Sahl * 6493126Sahl * global zone non-global zone 6503126Sahl * -------------------------------------------------- 6513126Sahl * zoned=on mountpoint (no) mountpoint (yes) 6523126Sahl * sharenfs (no) sharenfs (no) 6535331Samw * sharesmb (no) sharesmb (no) 6543126Sahl * 6553126Sahl * zoned=off mountpoint (yes) N/A 6563126Sahl * sharenfs (yes) 6575331Samw * sharesmb (yes) 6583126Sahl */ 6592676Seschrock if (zoned) { 6602676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 6612676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6622676Seschrock "'%s' cannot be set on " 6632676Seschrock "dataset in a non-global zone"), 6642676Seschrock propname); 6652676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6662676Seschrock errbuf); 6672676Seschrock goto error; 6685331Samw } else if (prop == ZFS_PROP_SHARENFS || 6695331Samw prop == ZFS_PROP_SHARESMB) { 6702676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6712676Seschrock "'%s' cannot be set in " 6722676Seschrock "a non-global zone"), propname); 6732676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6742676Seschrock errbuf); 6752676Seschrock goto error; 6762676Seschrock } 6772676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 6782676Seschrock /* 6792676Seschrock * If zoned property is 'off', this must be in 6802676Seschrock * a globle zone. If not, something is wrong. 6812676Seschrock */ 6822676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6832676Seschrock "'%s' cannot be set while dataset " 6842676Seschrock "'zoned' property is set"), propname); 6852676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 6862676Seschrock goto error; 6872676Seschrock } 6883126Sahl 6894180Sdougm /* 6904180Sdougm * At this point, it is legitimate to set the 6914180Sdougm * property. Now we want to make sure that the 6924180Sdougm * property value is valid if it is sharenfs. 6934180Sdougm */ 6945331Samw if ((prop == ZFS_PROP_SHARENFS || 6955331Samw prop == ZFS_PROP_SHARESMB) && 6964217Seschrock strcmp(strval, "on") != 0 && 6974217Seschrock strcmp(strval, "off") != 0) { 6985331Samw zfs_share_proto_t proto; 6995331Samw 7005331Samw if (prop == ZFS_PROP_SHARESMB) 7015331Samw proto = PROTO_SMB; 7025331Samw else 7035331Samw proto = PROTO_NFS; 7044180Sdougm 7054180Sdougm /* 7065331Samw * Must be an valid sharing protocol 7075331Samw * option string so init the libshare 7085331Samw * in order to enable the parser and 7095331Samw * then parse the options. We use the 7105331Samw * control API since we don't care about 7115331Samw * the current configuration and don't 7124180Sdougm * want the overhead of loading it 7134180Sdougm * until we actually do something. 7144180Sdougm */ 7154180Sdougm 7164217Seschrock if (zfs_init_libshare(hdl, 7174217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 7184217Seschrock /* 7194217Seschrock * An error occurred so we can't do 7204217Seschrock * anything 7214217Seschrock */ 7224217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7234217Seschrock "'%s' cannot be set: problem " 7244217Seschrock "in share initialization"), 7254217Seschrock propname); 7264217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7274217Seschrock errbuf); 7284217Seschrock goto error; 7294217Seschrock } 7304217Seschrock 7315331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 7324217Seschrock /* 7334217Seschrock * There was an error in parsing so 7344217Seschrock * deal with it by issuing an error 7354217Seschrock * message and leaving after 7364217Seschrock * uninitializing the the libshare 7374217Seschrock * interface. 7384217Seschrock */ 7394217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7404217Seschrock "'%s' cannot be set to invalid " 7414217Seschrock "options"), propname); 7424217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7434217Seschrock errbuf); 7444217Seschrock zfs_uninit_libshare(hdl); 7454217Seschrock goto error; 7464217Seschrock } 7474180Sdougm zfs_uninit_libshare(hdl); 7484180Sdougm } 7494180Sdougm 7503126Sahl break; 7515375Stimh case ZFS_PROP_CASE: 7525375Stimh chosen_sense = (int)intval; 7535375Stimh break; 7545331Samw case ZFS_PROP_UTF8ONLY: 7555331Samw chosen_utf = (int)intval; 7565331Samw break; 7575331Samw case ZFS_PROP_NORMALIZE: 7585331Samw chosen_normal = (int)intval; 7595331Samw break; 7602676Seschrock } 7612676Seschrock 7622676Seschrock /* 7632676Seschrock * For changes to existing volumes, we have some additional 7642676Seschrock * checks to enforce. 7652676Seschrock */ 7662676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 7672676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 7682676Seschrock ZFS_PROP_VOLSIZE); 7692676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 7702676Seschrock ZFS_PROP_VOLBLOCKSIZE); 7712676Seschrock char buf[64]; 7722676Seschrock 7732676Seschrock switch (prop) { 7742676Seschrock case ZFS_PROP_RESERVATION: 7755378Sck153898 case ZFS_PROP_REFRESERVATION: 7762676Seschrock if (intval > volsize) { 7772676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7782676Seschrock "'%s' is greater than current " 7792676Seschrock "volume size"), propname); 7802676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7812676Seschrock errbuf); 7822676Seschrock goto error; 7832676Seschrock } 7842676Seschrock break; 7852676Seschrock 7862676Seschrock case ZFS_PROP_VOLSIZE: 7872676Seschrock if (intval % blocksize != 0) { 7882676Seschrock zfs_nicenum(blocksize, buf, 7892676Seschrock sizeof (buf)); 7902676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7912676Seschrock "'%s' must be a multiple of " 7922676Seschrock "volume block size (%s)"), 7932676Seschrock propname, buf); 7942676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7952676Seschrock errbuf); 7962676Seschrock goto error; 7972676Seschrock } 7982676Seschrock 7992676Seschrock if (intval == 0) { 8002676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8012676Seschrock "'%s' cannot be zero"), 8022676Seschrock propname); 8032676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8042676Seschrock errbuf); 8052676Seschrock goto error; 806789Sahrens } 8073126Sahl break; 808789Sahrens } 809789Sahrens } 810789Sahrens } 811789Sahrens 8122676Seschrock /* 8135375Stimh * Temporarily disallow any non-default settings for 8145375Stimh * casesensitivity, normalization, and/or utf8only. 8155375Stimh */ 8165375Stimh if (chosen_sense > ZFS_CASE_SENSITIVE || chosen_utf > 0 || 8175375Stimh chosen_normal > ZFS_NORMALIZE_NONE) { 8185375Stimh zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8195375Stimh "Non-default values for casesensitivity, utf8only, and " 8205375Stimh "normalization are (temporarily) disabled")); 8215375Stimh (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8225375Stimh goto error; 8235375Stimh } 8245375Stimh 8255375Stimh /* 8265331Samw * If normalization was chosen, but no UTF8 choice was made, 8275331Samw * enforce rejection of non-UTF8 names. 8285331Samw * 8295331Samw * If normalization was chosen, but rejecting non-UTF8 names 8305331Samw * was explicitly not chosen, it is an error. 8315331Samw */ 8325331Samw if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf < 0) { 8335331Samw if (nvlist_add_uint64(ret, 8345331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 8355331Samw (void) no_memory(hdl); 8365331Samw goto error; 8375331Samw } 8385331Samw } else if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf == 0) { 8395331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8405331Samw "'%s' must be set 'on' if normalization chosen"), 8415331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 8425331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8435331Samw goto error; 8445331Samw } 8455331Samw 8465331Samw /* 8472676Seschrock * If this is an existing volume, and someone is setting the volsize, 8482676Seschrock * make sure that it matches the reservation, or add it if necessary. 8492676Seschrock */ 8502676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 8512676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 8522676Seschrock &intval) == 0) { 8532676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 8542676Seschrock ZFS_PROP_VOLSIZE); 8552676Seschrock uint64_t old_reservation = zfs_prop_get_int(zhp, 8562676Seschrock ZFS_PROP_RESERVATION); 8572676Seschrock uint64_t new_reservation; 8582676Seschrock 8592676Seschrock if (old_volsize == old_reservation && 8602676Seschrock nvlist_lookup_uint64(ret, 8612676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8622676Seschrock &new_reservation) != 0) { 8632676Seschrock if (nvlist_add_uint64(ret, 8642676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8652676Seschrock intval) != 0) { 8662676Seschrock (void) no_memory(hdl); 8672676Seschrock goto error; 8682676Seschrock } 8692676Seschrock } 8702676Seschrock } 8712676Seschrock 8722676Seschrock return (ret); 8732676Seschrock 8742676Seschrock error: 8752676Seschrock nvlist_free(ret); 8762676Seschrock return (NULL); 877789Sahrens } 878789Sahrens 8794543Smarks static int 8804543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 8814543Smarks uint64_t *ret_who) 8824543Smarks { 8834543Smarks struct passwd *pwd; 8844543Smarks struct group *grp; 8854543Smarks uid_t id; 8864543Smarks 8874543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 8884543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 8894543Smarks *ret_who = -1; 8904543Smarks return (0); 8914543Smarks } 8924543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 8934543Smarks return (EZFS_BADWHO); 8944543Smarks 8954543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 8964543Smarks strcmp(who, "everyone") == 0) { 8974543Smarks *ret_who = -1; 8984543Smarks *who_type = ZFS_DELEG_EVERYONE; 8994543Smarks return (0); 9004543Smarks } 9014543Smarks 9024543Smarks pwd = getpwnam(who); 9034543Smarks grp = getgrnam(who); 9044543Smarks 9054543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 9064543Smarks *ret_who = pwd->pw_uid; 9074543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 9084543Smarks *ret_who = grp->gr_gid; 9094543Smarks } else if (pwd) { 9104543Smarks *ret_who = pwd->pw_uid; 9114543Smarks *who_type = ZFS_DELEG_USER; 9124543Smarks } else if (grp) { 9134543Smarks *ret_who = grp->gr_gid; 9144543Smarks *who_type = ZFS_DELEG_GROUP; 9154543Smarks } else { 9164543Smarks char *end; 9174543Smarks 9184543Smarks id = strtol(who, &end, 10); 9194543Smarks if (errno != 0 || *end != '\0') { 9204543Smarks return (EZFS_BADWHO); 9214543Smarks } else { 9224543Smarks *ret_who = id; 9234543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 9244543Smarks *who_type = ZFS_DELEG_USER; 9254543Smarks } 9264543Smarks } 9274543Smarks 9284543Smarks return (0); 9294543Smarks } 9304543Smarks 9314543Smarks static void 9324543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 9334543Smarks { 9344543Smarks if (perms_nvp != NULL) { 9354543Smarks verify(nvlist_add_nvlist(who_nvp, 9364543Smarks name, perms_nvp) == 0); 9374543Smarks } else { 9384543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 9394543Smarks } 9404543Smarks } 9414543Smarks 9424543Smarks static void 9434543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 9444543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 9454543Smarks nvlist_t *sets_nvp) 9464543Smarks { 9474543Smarks boolean_t do_perms, do_sets; 9484543Smarks char name[ZFS_MAX_DELEG_NAME]; 9494543Smarks 9504543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 9514543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 9524543Smarks 9534543Smarks if (!do_perms && !do_sets) 9544543Smarks do_perms = do_sets = B_TRUE; 9554543Smarks 9564543Smarks if (do_perms) { 9574543Smarks zfs_deleg_whokey(name, who_type, inherit, 9584543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9594543Smarks whostr : (void *)&whoid); 9604543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 9614543Smarks } 9624543Smarks if (do_sets) { 9634543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 9644543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9654543Smarks whostr : (void *)&whoid); 9664543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 9674543Smarks } 9684543Smarks } 9694543Smarks 9704543Smarks static void 9714543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 9724543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 9734543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 9744543Smarks { 9754543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 9764543Smarks helper(who_type, whoid, whostr, 0, 9774543Smarks who_nvp, perms_nvp, sets_nvp); 9784543Smarks } else { 9794543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 9804543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 9814543Smarks who_nvp, perms_nvp, sets_nvp); 9824543Smarks } 9834543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 9844543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 9854543Smarks who_nvp, perms_nvp, sets_nvp); 9864543Smarks } 9874543Smarks } 9884543Smarks } 9894543Smarks 9904543Smarks /* 9914543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 9924543Smarks * 9934543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 9944543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 9954543Smarks * base attribute named stored in the dsl. 9964543Smarks * Arguments: 9974543Smarks * 9984543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 9994543Smarks * whostr may be null for everyone or create perms. 10004543Smarks * who_type: is the type of entry in whostr. Typically this will be 10014543Smarks * ZFS_DELEG_WHO_UNKNOWN. 10025331Samw * perms: common separated list of permissions. May be null if user 10034543Smarks * is requested to remove permissions by who. 10044543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 10054543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 10064543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 10074543Smarks * The output nvp will look something like this. 10084543Smarks * ul$1234 -> {create ; destroy } 10094543Smarks * Ul$1234 -> { @myset } 10104543Smarks * s-$@myset - { snapshot; checksum; compression } 10114543Smarks */ 10124543Smarks int 10134543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 10144543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 10154543Smarks { 10164543Smarks nvlist_t *who_nvp; 10174543Smarks nvlist_t *perms_nvp = NULL; 10184543Smarks nvlist_t *sets_nvp = NULL; 10194543Smarks char errbuf[1024]; 10204787Sahrens char *who_tok, *perm; 10214543Smarks int error; 10224543Smarks 10234543Smarks *nvp = NULL; 10244543Smarks 10254543Smarks if (perms) { 10264543Smarks if ((error = nvlist_alloc(&perms_nvp, 10274543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10284543Smarks return (1); 10294543Smarks } 10304543Smarks if ((error = nvlist_alloc(&sets_nvp, 10314543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10324543Smarks nvlist_free(perms_nvp); 10334543Smarks return (1); 10344543Smarks } 10354543Smarks } 10364543Smarks 10374543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 10384543Smarks if (perms_nvp) 10394543Smarks nvlist_free(perms_nvp); 10404543Smarks if (sets_nvp) 10414543Smarks nvlist_free(sets_nvp); 10424543Smarks return (1); 10434543Smarks } 10444543Smarks 10454543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 10464543Smarks namecheck_err_t why; 10474543Smarks char what; 10484543Smarks 10494543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 10504787Sahrens nvlist_free(who_nvp); 10514787Sahrens if (perms_nvp) 10524787Sahrens nvlist_free(perms_nvp); 10534787Sahrens if (sets_nvp) 10544787Sahrens nvlist_free(sets_nvp); 10554787Sahrens 10564543Smarks switch (why) { 10574543Smarks case NAME_ERR_NO_AT: 10584543Smarks zfs_error_aux(zhp->zfs_hdl, 10594543Smarks dgettext(TEXT_DOMAIN, 10604543Smarks "set definition must begin with an '@' " 10614543Smarks "character")); 10624543Smarks } 10634543Smarks return (zfs_error(zhp->zfs_hdl, 10644543Smarks EZFS_BADPERMSET, whostr)); 10654543Smarks } 10664543Smarks } 10674543Smarks 10684543Smarks /* 10694543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 10704543Smarks * The first nvlist perms_nvp will have normal permissions and the 10714543Smarks * other sets_nvp will have only permssion set names in it. 10724543Smarks */ 10734787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 10744787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 10754787Sahrens 10764787Sahrens if (perm_canonical) { 10774787Sahrens verify(nvlist_add_boolean(perms_nvp, 10784787Sahrens perm_canonical) == 0); 10794787Sahrens } else if (perm[0] == '@') { 10804787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 10814787Sahrens } else { 10824787Sahrens nvlist_free(who_nvp); 10834787Sahrens nvlist_free(perms_nvp); 10844787Sahrens nvlist_free(sets_nvp); 10854787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 10864543Smarks } 10874543Smarks } 10884543Smarks 10894543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 10904543Smarks who_tok = strtok(whostr, ","); 10914543Smarks if (who_tok == NULL) { 10924543Smarks nvlist_free(who_nvp); 10934787Sahrens if (perms_nvp) 10944787Sahrens nvlist_free(perms_nvp); 10954543Smarks if (sets_nvp) 10964543Smarks nvlist_free(sets_nvp); 10974543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10984543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 10994543Smarks whostr); 11004543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11014543Smarks } 11024543Smarks } 11034543Smarks 11044543Smarks /* 11054543Smarks * Now create the nvlist(s) 11064543Smarks */ 11074543Smarks do { 11084543Smarks uint64_t who_id; 11094543Smarks 11104543Smarks error = zfs_get_perm_who(who_tok, &who_type, 11114543Smarks &who_id); 11124543Smarks if (error) { 11134543Smarks nvlist_free(who_nvp); 11144787Sahrens if (perms_nvp) 11154787Sahrens nvlist_free(perms_nvp); 11164543Smarks if (sets_nvp) 11174543Smarks nvlist_free(sets_nvp); 11184543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11194543Smarks dgettext(TEXT_DOMAIN, 11204543Smarks "Unable to determine uid/gid for " 11214543Smarks "%s "), who_tok); 11224543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11234543Smarks } 11244543Smarks 11254543Smarks /* 11264543Smarks * add entries for both local and descendent when required 11274543Smarks */ 11284543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 11294543Smarks perms_nvp, sets_nvp, who_type, inherit); 11304543Smarks 11314543Smarks } while (who_tok = strtok(NULL, ",")); 11324543Smarks *nvp = who_nvp; 11334543Smarks return (0); 11344543Smarks } 11354543Smarks 11364543Smarks static int 11374543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 11384543Smarks { 11394543Smarks zfs_cmd_t zc = { 0 }; 11404543Smarks int error; 11414543Smarks char errbuf[1024]; 11424543Smarks 11434543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11444543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 11454543Smarks zhp->zfs_name); 11464543Smarks 11475094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 11484543Smarks return (-1); 11494543Smarks 11504543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 11514543Smarks zc.zc_perm_action = unset; 11524543Smarks 11534543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 11544543Smarks if (error && errno == ENOTSUP) { 11554543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11564543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 11574543Smarks zcmd_free_nvlists(&zc); 11584543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 11594543Smarks } else if (error) { 11604543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 11614543Smarks } 11624543Smarks zcmd_free_nvlists(&zc); 11634543Smarks 11644543Smarks return (error); 11654543Smarks } 11664543Smarks 11674543Smarks int 11684543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 11694543Smarks { 11704543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 11714543Smarks } 11724543Smarks 11734543Smarks int 11744543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 11754543Smarks { 11764543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 11774543Smarks } 11784543Smarks 11794543Smarks static int 11804543Smarks perm_compare(const void *arg1, const void *arg2) 11814543Smarks { 11824543Smarks const zfs_perm_node_t *node1 = arg1; 11834543Smarks const zfs_perm_node_t *node2 = arg2; 11844543Smarks int ret; 11854543Smarks 11864543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 11874543Smarks 11884543Smarks if (ret > 0) 11894543Smarks return (1); 11904543Smarks if (ret < 0) 11914543Smarks return (-1); 11924543Smarks else 11934543Smarks return (0); 11944543Smarks } 11954543Smarks 11964543Smarks static void 11974543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 11984543Smarks { 11994543Smarks zfs_perm_node_t *permnode; 12005367Sahrens void *cookie = NULL; 12015367Sahrens 12025367Sahrens while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 12034543Smarks free(permnode); 12045367Sahrens avl_destroy(tree); 12054543Smarks } 12064543Smarks 12074543Smarks static void 12084543Smarks zfs_destroy_tree(avl_tree_t *tree) 12094543Smarks { 12104543Smarks zfs_allow_node_t *allownode; 12115367Sahrens void *cookie = NULL; 12125367Sahrens 12134543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 12144543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 12154543Smarks zfs_destroy_perm_tree(&allownode->z_local); 12164543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 12174543Smarks free(allownode); 12184543Smarks } 12195367Sahrens avl_destroy(tree); 12204543Smarks } 12214543Smarks 12224543Smarks void 12234543Smarks zfs_free_allows(zfs_allow_t *allow) 12244543Smarks { 12254543Smarks zfs_allow_t *allownext; 12264543Smarks zfs_allow_t *freeallow; 12274543Smarks 12284543Smarks allownext = allow; 12294543Smarks while (allownext) { 12304543Smarks zfs_destroy_tree(&allownext->z_sets); 12314543Smarks zfs_destroy_tree(&allownext->z_crperms); 12324543Smarks zfs_destroy_tree(&allownext->z_user); 12334543Smarks zfs_destroy_tree(&allownext->z_group); 12344543Smarks zfs_destroy_tree(&allownext->z_everyone); 12354543Smarks freeallow = allownext; 12364543Smarks allownext = allownext->z_next; 12374543Smarks free(freeallow); 12384543Smarks } 12394543Smarks } 12404543Smarks 12414543Smarks static zfs_allow_t * 12424543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 12434543Smarks { 12444543Smarks zfs_allow_t *ptree; 12454543Smarks 12464543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 12474543Smarks sizeof (zfs_allow_t))) == NULL) { 12484543Smarks return (NULL); 12494543Smarks } 12504543Smarks 12514543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 12524543Smarks avl_create(&ptree->z_sets, 12534543Smarks perm_compare, sizeof (zfs_allow_node_t), 12544543Smarks offsetof(zfs_allow_node_t, z_node)); 12554543Smarks avl_create(&ptree->z_crperms, 12564543Smarks perm_compare, sizeof (zfs_allow_node_t), 12574543Smarks offsetof(zfs_allow_node_t, z_node)); 12584543Smarks avl_create(&ptree->z_user, 12594543Smarks perm_compare, sizeof (zfs_allow_node_t), 12604543Smarks offsetof(zfs_allow_node_t, z_node)); 12614543Smarks avl_create(&ptree->z_group, 12624543Smarks perm_compare, sizeof (zfs_allow_node_t), 12634543Smarks offsetof(zfs_allow_node_t, z_node)); 12644543Smarks avl_create(&ptree->z_everyone, 12654543Smarks perm_compare, sizeof (zfs_allow_node_t), 12664543Smarks offsetof(zfs_allow_node_t, z_node)); 12674543Smarks 12684543Smarks if (prev) 12694543Smarks prev->z_next = ptree; 12704543Smarks ptree->z_next = NULL; 12714543Smarks return (ptree); 12724543Smarks } 12734543Smarks 12744543Smarks /* 12754543Smarks * Add permissions to the appropriate AVL permission tree. 12764543Smarks * The appropriate tree may not be the requested tree. 12774543Smarks * For example if ld indicates a local permission, but 12784543Smarks * same permission also exists as a descendent permission 12794543Smarks * then the permission will be removed from the descendent 12804543Smarks * tree and add the the local+descendent tree. 12814543Smarks */ 12824543Smarks static int 12834543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 12844543Smarks char *perm, char ld) 12854543Smarks { 12864543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 12874543Smarks zfs_perm_node_t *newnode; 12884543Smarks avl_index_t where, where2; 12894543Smarks avl_tree_t *tree, *altree; 12904543Smarks 12914543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 12924543Smarks 12934543Smarks if (ld == ZFS_DELEG_NA) { 12944543Smarks tree = &allownode->z_localdescend; 12954543Smarks altree = &allownode->z_descend; 12964543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 12974543Smarks tree = &allownode->z_local; 12984543Smarks altree = &allownode->z_descend; 12994543Smarks } else { 13004543Smarks tree = &allownode->z_descend; 13014543Smarks altree = &allownode->z_local; 13024543Smarks } 13034543Smarks permnode = avl_find(tree, &pnode, &where); 13044543Smarks permnode2 = avl_find(altree, &pnode, &where2); 13054543Smarks 13064543Smarks if (permnode2) { 13074543Smarks avl_remove(altree, permnode2); 13084543Smarks free(permnode2); 13094543Smarks if (permnode == NULL) { 13104543Smarks tree = &allownode->z_localdescend; 13114543Smarks } 13124543Smarks } 13134543Smarks 13144543Smarks /* 13154543Smarks * Now insert new permission in either requested location 13164543Smarks * local/descendent or into ld when perm will exist in both. 13174543Smarks */ 13184543Smarks if (permnode == NULL) { 13194543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 13204543Smarks sizeof (zfs_perm_node_t))) == NULL) { 13214543Smarks return (-1); 13224543Smarks } 13234543Smarks *newnode = pnode; 13244543Smarks avl_add(tree, newnode); 13254543Smarks } 13264543Smarks return (0); 13274543Smarks } 13284577Sahrens 13294543Smarks /* 13304543Smarks * Uggh, this is going to be a bit complicated. 13314543Smarks * we have an nvlist coming out of the kernel that 13324543Smarks * will indicate where the permission is set and then 13334543Smarks * it will contain allow of the various "who's", and what 13344543Smarks * their permissions are. To further complicate this 13354543Smarks * we will then have to coalesce the local,descendent 13364543Smarks * and local+descendent permissions where appropriate. 13374543Smarks * The kernel only knows about a permission as being local 13384543Smarks * or descendent, but not both. 13394543Smarks * 13404543Smarks * In order to make this easier for zfs_main to deal with 13414543Smarks * a series of AVL trees will be used to maintain 13424543Smarks * all of this, primarily for sorting purposes as well 13434543Smarks * as the ability to quickly locate a specific entry. 13444543Smarks * 13454543Smarks * What we end up with are tree's for sets, create perms, 13464543Smarks * user, groups and everyone. With each of those trees 13474543Smarks * we have subtrees for local, descendent and local+descendent 13484543Smarks * permissions. 13494543Smarks */ 13504543Smarks int 13514543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 13524543Smarks { 13534543Smarks zfs_cmd_t zc = { 0 }; 13544543Smarks int error; 13554543Smarks nvlist_t *nvlist; 13564543Smarks nvlist_t *permnv, *sourcenv; 13574543Smarks nvpair_t *who_pair, *source_pair; 13584543Smarks nvpair_t *perm_pair; 13594543Smarks char errbuf[1024]; 13604543Smarks zfs_allow_t *zallowp, *newallowp; 13614543Smarks char ld; 13624543Smarks char *nvpname; 13634543Smarks uid_t uid; 13644543Smarks gid_t gid; 13654543Smarks avl_tree_t *tree; 13664543Smarks avl_index_t where; 13674543Smarks 13684543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13694543Smarks 13704543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 13714543Smarks return (-1); 13724543Smarks 13734543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 13744543Smarks if (errno == ENOMEM) { 13754543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 13764543Smarks zcmd_free_nvlists(&zc); 13774543Smarks return (-1); 13784543Smarks } 13794543Smarks } else if (errno == ENOTSUP) { 13804543Smarks zcmd_free_nvlists(&zc); 13814543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13824543Smarks gettext("Pool must be upgraded to use 'allow'")); 13834543Smarks return (zfs_error(zhp->zfs_hdl, 13844543Smarks EZFS_BADVERSION, errbuf)); 13854543Smarks } else { 13864543Smarks zcmd_free_nvlists(&zc); 13874543Smarks return (-1); 13884543Smarks } 13894543Smarks } 13904543Smarks 13914543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 13924543Smarks zcmd_free_nvlists(&zc); 13934543Smarks return (-1); 13944543Smarks } 13954543Smarks 13964543Smarks zcmd_free_nvlists(&zc); 13974543Smarks 13984543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 13994543Smarks 14004543Smarks if (source_pair == NULL) { 14014543Smarks *zfs_perms = NULL; 14024543Smarks return (0); 14034543Smarks } 14044543Smarks 14054543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 14064543Smarks if (*zfs_perms == NULL) { 14074543Smarks return (0); 14084543Smarks } 14094543Smarks 14104543Smarks zallowp = *zfs_perms; 14114543Smarks 14124543Smarks for (;;) { 14134543Smarks struct passwd *pwd; 14144543Smarks struct group *grp; 14154543Smarks zfs_allow_node_t *allownode; 14164543Smarks zfs_allow_node_t findallownode; 14174543Smarks zfs_allow_node_t *newallownode; 14184543Smarks 14194543Smarks (void) strlcpy(zallowp->z_setpoint, 14204543Smarks nvpair_name(source_pair), 14214543Smarks sizeof (zallowp->z_setpoint)); 14224543Smarks 14234543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 14244543Smarks goto abort; 14254543Smarks 14264543Smarks /* 14274543Smarks * Make sure nvlist is composed correctly 14284543Smarks */ 14294543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 14304543Smarks goto abort; 14314543Smarks } 14324543Smarks 14334543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 14344543Smarks if (who_pair == NULL) { 14354543Smarks goto abort; 14364543Smarks } 14374543Smarks 14384543Smarks do { 14394543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 14404543Smarks if (error) { 14414543Smarks goto abort; 14424543Smarks } 14434543Smarks 14444543Smarks /* 14454543Smarks * First build up the key to use 14464543Smarks * for looking up in the various 14474543Smarks * who trees. 14484543Smarks */ 14494543Smarks ld = nvpair_name(who_pair)[1]; 14504543Smarks nvpname = nvpair_name(who_pair); 14514543Smarks switch (nvpair_name(who_pair)[0]) { 14524543Smarks case ZFS_DELEG_USER: 14534543Smarks case ZFS_DELEG_USER_SETS: 14544543Smarks tree = &zallowp->z_user; 14554543Smarks uid = atol(&nvpname[3]); 14564543Smarks pwd = getpwuid(uid); 14574543Smarks (void) snprintf(findallownode.z_key, 14584543Smarks sizeof (findallownode.z_key), "user %s", 14594543Smarks (pwd) ? pwd->pw_name : 14604543Smarks &nvpair_name(who_pair)[3]); 14614543Smarks break; 14624543Smarks case ZFS_DELEG_GROUP: 14634543Smarks case ZFS_DELEG_GROUP_SETS: 14644543Smarks tree = &zallowp->z_group; 14654543Smarks gid = atol(&nvpname[3]); 14664543Smarks grp = getgrgid(gid); 14674543Smarks (void) snprintf(findallownode.z_key, 14684543Smarks sizeof (findallownode.z_key), "group %s", 14694543Smarks (grp) ? grp->gr_name : 14704543Smarks &nvpair_name(who_pair)[3]); 14714543Smarks break; 14724543Smarks case ZFS_DELEG_CREATE: 14734543Smarks case ZFS_DELEG_CREATE_SETS: 14744543Smarks tree = &zallowp->z_crperms; 14754543Smarks (void) strlcpy(findallownode.z_key, "", 14764543Smarks sizeof (findallownode.z_key)); 14774543Smarks break; 14784543Smarks case ZFS_DELEG_EVERYONE: 14794543Smarks case ZFS_DELEG_EVERYONE_SETS: 14804543Smarks (void) snprintf(findallownode.z_key, 14814543Smarks sizeof (findallownode.z_key), "everyone"); 14824543Smarks tree = &zallowp->z_everyone; 14834543Smarks break; 14844543Smarks case ZFS_DELEG_NAMED_SET: 14854543Smarks case ZFS_DELEG_NAMED_SET_SETS: 14864543Smarks (void) snprintf(findallownode.z_key, 14874543Smarks sizeof (findallownode.z_key), "%s", 14884543Smarks &nvpair_name(who_pair)[3]); 14894543Smarks tree = &zallowp->z_sets; 14904543Smarks break; 14914543Smarks } 14924543Smarks 14934543Smarks /* 14944543Smarks * Place who in tree 14954543Smarks */ 14964543Smarks allownode = avl_find(tree, &findallownode, &where); 14974543Smarks if (allownode == NULL) { 14984543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 14994543Smarks sizeof (zfs_allow_node_t))) == NULL) { 15004543Smarks goto abort; 15014543Smarks } 15024543Smarks avl_create(&newallownode->z_localdescend, 15034543Smarks perm_compare, 15044543Smarks sizeof (zfs_perm_node_t), 15054543Smarks offsetof(zfs_perm_node_t, z_node)); 15064543Smarks avl_create(&newallownode->z_local, 15074543Smarks perm_compare, 15084543Smarks sizeof (zfs_perm_node_t), 15094543Smarks offsetof(zfs_perm_node_t, z_node)); 15104543Smarks avl_create(&newallownode->z_descend, 15114543Smarks perm_compare, 15124543Smarks sizeof (zfs_perm_node_t), 15134543Smarks offsetof(zfs_perm_node_t, z_node)); 15144543Smarks (void) strlcpy(newallownode->z_key, 15154543Smarks findallownode.z_key, 15164543Smarks sizeof (findallownode.z_key)); 15174543Smarks avl_insert(tree, newallownode, where); 15184543Smarks allownode = newallownode; 15194543Smarks } 15204543Smarks 15214543Smarks /* 15224543Smarks * Now iterate over the permissions and 15234543Smarks * place them in the appropriate local, 15244543Smarks * descendent or local+descendent tree. 15254543Smarks * 15264543Smarks * The permissions are added to the tree 15274543Smarks * via zfs_coalesce_perm(). 15284543Smarks */ 15294543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 15304543Smarks if (perm_pair == NULL) 15314543Smarks goto abort; 15324543Smarks do { 15334543Smarks if (zfs_coalesce_perm(zhp, allownode, 15344543Smarks nvpair_name(perm_pair), ld) != 0) 15354543Smarks goto abort; 15364543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 15374543Smarks perm_pair)); 15384543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 15394543Smarks 15404543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 15414543Smarks if (source_pair == NULL) 15424543Smarks break; 15434543Smarks 15444543Smarks /* 15454543Smarks * allocate another node from the link list of 15464543Smarks * zfs_allow_t structures 15474543Smarks */ 15484543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 15494543Smarks nvpair_name(source_pair)); 15504543Smarks if (newallowp == NULL) { 15514543Smarks goto abort; 15524543Smarks } 15534543Smarks zallowp = newallowp; 15544543Smarks } 15554543Smarks nvlist_free(nvlist); 15564543Smarks return (0); 15574543Smarks abort: 15584543Smarks zfs_free_allows(*zfs_perms); 15594543Smarks nvlist_free(nvlist); 15604543Smarks return (-1); 15614543Smarks } 15624543Smarks 1563789Sahrens /* 1564789Sahrens * Given a property name and value, set the property for the given dataset. 1565789Sahrens */ 1566789Sahrens int 15672676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1568789Sahrens { 1569789Sahrens zfs_cmd_t zc = { 0 }; 15702676Seschrock int ret = -1; 15712676Seschrock prop_changelist_t *cl = NULL; 15722082Seschrock char errbuf[1024]; 15732082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 15742676Seschrock nvlist_t *nvl = NULL, *realprops; 15752676Seschrock zfs_prop_t prop; 15762082Seschrock 15772082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 15782676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 15792082Seschrock zhp->zfs_name); 15802082Seschrock 15812676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 15822676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 15832676Seschrock (void) no_memory(hdl); 15842676Seschrock goto error; 1585789Sahrens } 1586789Sahrens 15875094Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 15882676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 15892676Seschrock goto error; 15905094Slling 15912676Seschrock nvlist_free(nvl); 15922676Seschrock nvl = realprops; 15932676Seschrock 15942676Seschrock prop = zfs_name_to_prop(propname); 15952676Seschrock 1596789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 15972676Seschrock goto error; 1598789Sahrens 1599789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 16002082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16012082Seschrock "child dataset with inherited mountpoint is used " 16022082Seschrock "in a non-global zone")); 16032082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1604789Sahrens goto error; 1605789Sahrens } 1606789Sahrens 1607789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1608789Sahrens goto error; 1609789Sahrens 1610789Sahrens /* 1611789Sahrens * Execute the corresponding ioctl() to set this property. 1612789Sahrens */ 1613789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1614789Sahrens 16155094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 16162676Seschrock goto error; 16172676Seschrock 16184543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1619789Sahrens 1620789Sahrens if (ret != 0) { 1621789Sahrens switch (errno) { 1622789Sahrens 1623789Sahrens case ENOSPC: 1624789Sahrens /* 1625789Sahrens * For quotas and reservations, ENOSPC indicates 1626789Sahrens * something different; setting a quota or reservation 1627789Sahrens * doesn't use any disk space. 1628789Sahrens */ 1629789Sahrens switch (prop) { 1630789Sahrens case ZFS_PROP_QUOTA: 16315378Sck153898 case ZFS_PROP_REFQUOTA: 16322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16332082Seschrock "size is less than current used or " 16342082Seschrock "reserved space")); 16352082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1636789Sahrens break; 1637789Sahrens 1638789Sahrens case ZFS_PROP_RESERVATION: 16395378Sck153898 case ZFS_PROP_REFRESERVATION: 16402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16412082Seschrock "size is greater than available space")); 16422082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1643789Sahrens break; 1644789Sahrens 1645789Sahrens default: 16462082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1647789Sahrens break; 1648789Sahrens } 1649789Sahrens break; 1650789Sahrens 1651789Sahrens case EBUSY: 16522082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 16532082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 16542082Seschrock else 16552676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1656789Sahrens break; 1657789Sahrens 16581175Slling case EROFS: 16592082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 16601175Slling break; 16611175Slling 16623886Sahl case ENOTSUP: 16633886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16644603Sahrens "pool must be upgraded to set this " 16654603Sahrens "property or value")); 16663886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 16673886Sahl break; 16683886Sahl 1669789Sahrens case EOVERFLOW: 1670789Sahrens /* 1671789Sahrens * This platform can't address a volume this big. 1672789Sahrens */ 1673789Sahrens #ifdef _ILP32 1674789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 16752082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1676789Sahrens break; 1677789Sahrens } 1678789Sahrens #endif 16792082Seschrock /* FALLTHROUGH */ 1680789Sahrens default: 16812082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1682789Sahrens } 1683789Sahrens } else { 1684789Sahrens /* 1685789Sahrens * Refresh the statistics so the new property value 1686789Sahrens * is reflected. 1687789Sahrens */ 16882676Seschrock if ((ret = changelist_postfix(cl)) == 0) 16892676Seschrock (void) get_stats(zhp); 1690789Sahrens } 1691789Sahrens 1692789Sahrens error: 16932676Seschrock nvlist_free(nvl); 16942676Seschrock zcmd_free_nvlists(&zc); 16952676Seschrock if (cl) 16962676Seschrock changelist_free(cl); 1697789Sahrens return (ret); 1698789Sahrens } 1699789Sahrens 1700789Sahrens /* 1701789Sahrens * Given a property, inherit the value from the parent dataset. 1702789Sahrens */ 1703789Sahrens int 17042676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1705789Sahrens { 1706789Sahrens zfs_cmd_t zc = { 0 }; 1707789Sahrens int ret; 1708789Sahrens prop_changelist_t *cl; 17092082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 17102082Seschrock char errbuf[1024]; 17112676Seschrock zfs_prop_t prop; 17122082Seschrock 17132082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 17142082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1715789Sahrens 17165094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 17172676Seschrock /* 17182676Seschrock * For user properties, the amount of work we have to do is very 17192676Seschrock * small, so just do it here. 17202676Seschrock */ 17212676Seschrock if (!zfs_prop_user(propname)) { 17222676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17232676Seschrock "invalid property")); 17242676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 17252676Seschrock } 17262676Seschrock 17272676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17282676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 17292676Seschrock 17304849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 17312676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 17322676Seschrock 17332676Seschrock return (0); 17342676Seschrock } 17352676Seschrock 1736789Sahrens /* 1737789Sahrens * Verify that this property is inheritable. 1738789Sahrens */ 17392082Seschrock if (zfs_prop_readonly(prop)) 17402082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 17412082Seschrock 17422082Seschrock if (!zfs_prop_inheritable(prop)) 17432082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1744789Sahrens 1745789Sahrens /* 1746789Sahrens * Check to see if the value applies to this type 1747789Sahrens */ 17482082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 17492082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1750789Sahrens 17513443Srm160521 /* 17523443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 17533443Srm160521 */ 17543443Srm160521 propname = zfs_prop_to_name(prop); 1755789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17562676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1757789Sahrens 1758789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1759789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 17602082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17612082Seschrock "dataset is used in a non-global zone")); 17622082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1763789Sahrens } 1764789Sahrens 1765789Sahrens /* 1766789Sahrens * Determine datasets which will be affected by this change, if any. 1767789Sahrens */ 1768789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1769789Sahrens return (-1); 1770789Sahrens 1771789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 17722082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17732082Seschrock "child dataset with inherited mountpoint is used " 17742082Seschrock "in a non-global zone")); 17752082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1776789Sahrens goto error; 1777789Sahrens } 1778789Sahrens 1779789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1780789Sahrens goto error; 1781789Sahrens 17824849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 17832082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1784789Sahrens } else { 1785789Sahrens 17862169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1787789Sahrens goto error; 1788789Sahrens 1789789Sahrens /* 1790789Sahrens * Refresh the statistics so the new property is reflected. 1791789Sahrens */ 1792789Sahrens (void) get_stats(zhp); 1793789Sahrens } 1794789Sahrens 1795789Sahrens error: 1796789Sahrens changelist_free(cl); 1797789Sahrens return (ret); 1798789Sahrens } 1799789Sahrens 1800789Sahrens /* 18011356Seschrock * True DSL properties are stored in an nvlist. The following two functions 18021356Seschrock * extract them appropriately. 18031356Seschrock */ 18041356Seschrock static uint64_t 18051356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18061356Seschrock { 18071356Seschrock nvlist_t *nv; 18081356Seschrock uint64_t value; 18091356Seschrock 18102885Sahrens *source = NULL; 18111356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18121356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 18135094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 18145094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18151356Seschrock } else { 18161356Seschrock value = zfs_prop_default_numeric(prop); 18171356Seschrock *source = ""; 18181356Seschrock } 18191356Seschrock 18201356Seschrock return (value); 18211356Seschrock } 18221356Seschrock 18231356Seschrock static char * 18241356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18251356Seschrock { 18261356Seschrock nvlist_t *nv; 18271356Seschrock char *value; 18281356Seschrock 18292885Sahrens *source = NULL; 18301356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18311356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 18325094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 18335094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18341356Seschrock } else { 18351356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 18361356Seschrock value = ""; 18371356Seschrock *source = ""; 18381356Seschrock } 18391356Seschrock 18401356Seschrock return (value); 18411356Seschrock } 18421356Seschrock 18431356Seschrock /* 1844789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1845789Sahrens * zfs_prop_get_int() are built using this interface. 1846789Sahrens * 1847789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1848789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1849789Sahrens * If they differ from the on-disk values, report the current values and mark 1850789Sahrens * the source "temporary". 1851789Sahrens */ 18522082Seschrock static int 18535094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 18542082Seschrock char **source, uint64_t *val) 1855789Sahrens { 18565147Srm160521 zfs_cmd_t zc = { 0 }; 1857789Sahrens struct mnttab mnt; 18583265Sahrens char *mntopt_on = NULL; 18593265Sahrens char *mntopt_off = NULL; 1860789Sahrens 1861789Sahrens *source = NULL; 1862789Sahrens 18633265Sahrens switch (prop) { 18643265Sahrens case ZFS_PROP_ATIME: 18653265Sahrens mntopt_on = MNTOPT_ATIME; 18663265Sahrens mntopt_off = MNTOPT_NOATIME; 18673265Sahrens break; 18683265Sahrens 18693265Sahrens case ZFS_PROP_DEVICES: 18703265Sahrens mntopt_on = MNTOPT_DEVICES; 18713265Sahrens mntopt_off = MNTOPT_NODEVICES; 18723265Sahrens break; 18733265Sahrens 18743265Sahrens case ZFS_PROP_EXEC: 18753265Sahrens mntopt_on = MNTOPT_EXEC; 18763265Sahrens mntopt_off = MNTOPT_NOEXEC; 18773265Sahrens break; 18783265Sahrens 18793265Sahrens case ZFS_PROP_READONLY: 18803265Sahrens mntopt_on = MNTOPT_RO; 18813265Sahrens mntopt_off = MNTOPT_RW; 18823265Sahrens break; 18833265Sahrens 18843265Sahrens case ZFS_PROP_SETUID: 18853265Sahrens mntopt_on = MNTOPT_SETUID; 18863265Sahrens mntopt_off = MNTOPT_NOSETUID; 18873265Sahrens break; 18883265Sahrens 18893265Sahrens case ZFS_PROP_XATTR: 18903265Sahrens mntopt_on = MNTOPT_XATTR; 18913265Sahrens mntopt_off = MNTOPT_NOXATTR; 18923265Sahrens break; 18935331Samw 18945331Samw case ZFS_PROP_NBMAND: 18955331Samw mntopt_on = MNTOPT_NBMAND; 18965331Samw mntopt_off = MNTOPT_NONBMAND; 18975331Samw break; 18983265Sahrens } 18993265Sahrens 19002474Seschrock /* 19012474Seschrock * Because looking up the mount options is potentially expensive 19022474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 19032474Seschrock * we're looking up a property which requires its presence. 19042474Seschrock */ 19052474Seschrock if (!zhp->zfs_mntcheck && 19063265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 19073265Sahrens struct mnttab entry, search = { 0 }; 19083265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 19092474Seschrock 19102474Seschrock search.mnt_special = (char *)zhp->zfs_name; 19112474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 19123265Sahrens rewind(mnttab); 19133265Sahrens 19143265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 19153265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 19163265Sahrens entry.mnt_mntopts); 19173265Sahrens if (zhp->zfs_mntopts == NULL) 19183265Sahrens return (-1); 19193265Sahrens } 19202474Seschrock 19212474Seschrock zhp->zfs_mntcheck = B_TRUE; 19222474Seschrock } 19232474Seschrock 1924789Sahrens if (zhp->zfs_mntopts == NULL) 1925789Sahrens mnt.mnt_mntopts = ""; 1926789Sahrens else 1927789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1928789Sahrens 1929789Sahrens switch (prop) { 1930789Sahrens case ZFS_PROP_ATIME: 19313265Sahrens case ZFS_PROP_DEVICES: 19323265Sahrens case ZFS_PROP_EXEC: 19333265Sahrens case ZFS_PROP_READONLY: 19343265Sahrens case ZFS_PROP_SETUID: 19353265Sahrens case ZFS_PROP_XATTR: 19365331Samw case ZFS_PROP_NBMAND: 19372082Seschrock *val = getprop_uint64(zhp, prop, source); 19382082Seschrock 19393265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 19402082Seschrock *val = B_TRUE; 1941789Sahrens if (src) 19425094Slling *src = ZPROP_SRC_TEMPORARY; 19433265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 19442082Seschrock *val = B_FALSE; 1945789Sahrens if (src) 19465094Slling *src = ZPROP_SRC_TEMPORARY; 1947789Sahrens } 19482082Seschrock break; 1949789Sahrens 19503265Sahrens case ZFS_PROP_CANMOUNT: 19512082Seschrock *val = getprop_uint64(zhp, prop, source); 19523417Srm160521 if (*val == 0) 19533417Srm160521 *source = zhp->zfs_name; 19543417Srm160521 else 19553417Srm160521 *source = ""; /* default */ 19562082Seschrock break; 1957789Sahrens 1958789Sahrens case ZFS_PROP_QUOTA: 19595378Sck153898 case ZFS_PROP_REFQUOTA: 1960789Sahrens case ZFS_PROP_RESERVATION: 19615378Sck153898 case ZFS_PROP_REFRESERVATION: 19622885Sahrens *val = getprop_uint64(zhp, prop, source); 19632885Sahrens if (*val == 0) 1964789Sahrens *source = ""; /* default */ 1965789Sahrens else 1966789Sahrens *source = zhp->zfs_name; 19672082Seschrock break; 1968789Sahrens 1969789Sahrens case ZFS_PROP_MOUNTED: 19702082Seschrock *val = (zhp->zfs_mntopts != NULL); 19712082Seschrock break; 1972789Sahrens 19733377Seschrock case ZFS_PROP_NUMCLONES: 19743377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 19753377Seschrock break; 19763377Seschrock 19775147Srm160521 case ZFS_PROP_VERSION: 1978*5473Srm160521 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type)) 1979*5473Srm160521 return (-1); 19805147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19815147Srm160521 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_VERSION, &zc) || 19825147Srm160521 (zc.zc_cookie == 0)) { 19835147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19845147Srm160521 "unable to get version property")); 19855147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 19865147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 19875147Srm160521 } 19885147Srm160521 *val = zc.zc_cookie; 19895147Srm160521 break; 19905147Srm160521 1991789Sahrens default: 19924577Sahrens switch (zfs_prop_get_type(prop)) { 19934787Sahrens case PROP_TYPE_NUMBER: 19944787Sahrens case PROP_TYPE_INDEX: 19954577Sahrens *val = getprop_uint64(zhp, prop, source); 19964577Sahrens break; 19974577Sahrens 19984787Sahrens case PROP_TYPE_STRING: 19994577Sahrens default: 20004577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 20014577Sahrens "cannot get non-numeric property")); 20024577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 20034577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 20044577Sahrens } 2005789Sahrens } 2006789Sahrens 2007789Sahrens return (0); 2008789Sahrens } 2009789Sahrens 2010789Sahrens /* 2011789Sahrens * Calculate the source type, given the raw source string. 2012789Sahrens */ 2013789Sahrens static void 20145094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2015789Sahrens char *statbuf, size_t statlen) 2016789Sahrens { 20175094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2018789Sahrens return; 2019789Sahrens 2020789Sahrens if (source == NULL) { 20215094Slling *srctype = ZPROP_SRC_NONE; 2022789Sahrens } else if (source[0] == '\0') { 20235094Slling *srctype = ZPROP_SRC_DEFAULT; 2024789Sahrens } else { 2025789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 20265094Slling *srctype = ZPROP_SRC_LOCAL; 2027789Sahrens } else { 2028789Sahrens (void) strlcpy(statbuf, source, statlen); 20295094Slling *srctype = ZPROP_SRC_INHERITED; 2030789Sahrens } 2031789Sahrens } 2032789Sahrens 2033789Sahrens } 2034789Sahrens 2035789Sahrens /* 2036789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2037789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2038789Sahrens * human-readable form. 2039789Sahrens * 2040789Sahrens * Returns 0 on success, or -1 on error. 2041789Sahrens */ 2042789Sahrens int 2043789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 20445094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2045789Sahrens { 2046789Sahrens char *source = NULL; 2047789Sahrens uint64_t val; 2048789Sahrens char *str; 2049789Sahrens const char *root; 20502676Seschrock const char *strval; 2051789Sahrens 2052789Sahrens /* 2053789Sahrens * Check to see if this property applies to our object 2054789Sahrens */ 2055789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2056789Sahrens return (-1); 2057789Sahrens 2058789Sahrens if (src) 20595094Slling *src = ZPROP_SRC_NONE; 2060789Sahrens 2061789Sahrens switch (prop) { 2062789Sahrens case ZFS_PROP_CREATION: 2063789Sahrens /* 2064789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2065789Sahrens * this into a string unless 'literal' is specified. 2066789Sahrens */ 2067789Sahrens { 20682885Sahrens val = getprop_uint64(zhp, prop, &source); 20692885Sahrens time_t time = (time_t)val; 2070789Sahrens struct tm t; 2071789Sahrens 2072789Sahrens if (literal || 2073789Sahrens localtime_r(&time, &t) == NULL || 2074789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2075789Sahrens &t) == 0) 20762885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2077789Sahrens } 2078789Sahrens break; 2079789Sahrens 2080789Sahrens case ZFS_PROP_MOUNTPOINT: 2081789Sahrens /* 2082789Sahrens * Getting the precise mountpoint can be tricky. 2083789Sahrens * 2084789Sahrens * - for 'none' or 'legacy', return those values. 2085789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2086789Sahrens * - for inherited mountpoints, we want to take everything 2087789Sahrens * after our ancestor and append it to the inherited value. 2088789Sahrens * 2089789Sahrens * If the pool has an alternate root, we want to prepend that 2090789Sahrens * root to any values we return. 2091789Sahrens */ 20921544Seschrock root = zhp->zfs_root; 20931356Seschrock str = getprop_string(zhp, prop, &source); 20941356Seschrock 20951356Seschrock if (str[0] == '\0') { 2096789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2097789Sahrens root, zhp->zfs_name); 20981356Seschrock } else if (str[0] == '/') { 20991356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2100789Sahrens 2101789Sahrens if (relpath[0] == '/') 2102789Sahrens relpath++; 21031356Seschrock if (str[1] == '\0') 21041356Seschrock str++; 2105789Sahrens 2106789Sahrens if (relpath[0] == '\0') 2107789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 21081356Seschrock root, str); 2109789Sahrens else 2110789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 21111356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2112789Sahrens relpath); 2113789Sahrens } else { 2114789Sahrens /* 'legacy' or 'none' */ 21151356Seschrock (void) strlcpy(propbuf, str, proplen); 2116789Sahrens } 2117789Sahrens 2118789Sahrens break; 2119789Sahrens 2120789Sahrens case ZFS_PROP_ORIGIN: 21212885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2122789Sahrens proplen); 2123789Sahrens /* 2124789Sahrens * If there is no parent at all, return failure to indicate that 2125789Sahrens * it doesn't apply to this dataset. 2126789Sahrens */ 2127789Sahrens if (propbuf[0] == '\0') 2128789Sahrens return (-1); 2129789Sahrens break; 2130789Sahrens 2131789Sahrens case ZFS_PROP_QUOTA: 21325378Sck153898 case ZFS_PROP_REFQUOTA: 2133789Sahrens case ZFS_PROP_RESERVATION: 21345378Sck153898 case ZFS_PROP_REFRESERVATION: 21355378Sck153898 21362082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21372082Seschrock return (-1); 2138789Sahrens 2139789Sahrens /* 2140789Sahrens * If quota or reservation is 0, we translate this into 'none' 2141789Sahrens * (unless literal is set), and indicate that it's the default 2142789Sahrens * value. Otherwise, we print the number nicely and indicate 2143789Sahrens * that its set locally. 2144789Sahrens */ 2145789Sahrens if (val == 0) { 2146789Sahrens if (literal) 2147789Sahrens (void) strlcpy(propbuf, "0", proplen); 2148789Sahrens else 2149789Sahrens (void) strlcpy(propbuf, "none", proplen); 2150789Sahrens } else { 2151789Sahrens if (literal) 21522856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 21533912Slling (u_longlong_t)val); 2154789Sahrens else 2155789Sahrens zfs_nicenum(val, propbuf, proplen); 2156789Sahrens } 2157789Sahrens break; 2158789Sahrens 2159789Sahrens case ZFS_PROP_COMPRESSRATIO: 21602082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21612082Seschrock return (-1); 21622856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 21632856Snd150628 val / 100, (longlong_t)val % 100); 2164789Sahrens break; 2165789Sahrens 2166789Sahrens case ZFS_PROP_TYPE: 2167789Sahrens switch (zhp->zfs_type) { 2168789Sahrens case ZFS_TYPE_FILESYSTEM: 2169789Sahrens str = "filesystem"; 2170789Sahrens break; 2171789Sahrens case ZFS_TYPE_VOLUME: 2172789Sahrens str = "volume"; 2173789Sahrens break; 2174789Sahrens case ZFS_TYPE_SNAPSHOT: 2175789Sahrens str = "snapshot"; 2176789Sahrens break; 2177789Sahrens default: 21782082Seschrock abort(); 2179789Sahrens } 2180789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2181789Sahrens break; 2182789Sahrens 2183789Sahrens case ZFS_PROP_MOUNTED: 2184789Sahrens /* 2185789Sahrens * The 'mounted' property is a pseudo-property that described 2186789Sahrens * whether the filesystem is currently mounted. Even though 2187789Sahrens * it's a boolean value, the typical values of "on" and "off" 2188789Sahrens * don't make sense, so we translate to "yes" and "no". 2189789Sahrens */ 21902082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 21912082Seschrock src, &source, &val) != 0) 21922082Seschrock return (-1); 21932082Seschrock if (val) 2194789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2195789Sahrens else 2196789Sahrens (void) strlcpy(propbuf, "no", proplen); 2197789Sahrens break; 2198789Sahrens 2199789Sahrens case ZFS_PROP_NAME: 2200789Sahrens /* 2201789Sahrens * The 'name' property is a pseudo-property derived from the 2202789Sahrens * dataset name. It is presented as a real property to simplify 2203789Sahrens * consumers. 2204789Sahrens */ 2205789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2206789Sahrens break; 2207789Sahrens 2208789Sahrens default: 22094577Sahrens switch (zfs_prop_get_type(prop)) { 22104787Sahrens case PROP_TYPE_NUMBER: 22114577Sahrens if (get_numeric_property(zhp, prop, src, 22124577Sahrens &source, &val) != 0) 22134577Sahrens return (-1); 22144577Sahrens if (literal) 22154577Sahrens (void) snprintf(propbuf, proplen, "%llu", 22164577Sahrens (u_longlong_t)val); 22174577Sahrens else 22184577Sahrens zfs_nicenum(val, propbuf, proplen); 22194577Sahrens break; 22204577Sahrens 22214787Sahrens case PROP_TYPE_STRING: 22224577Sahrens (void) strlcpy(propbuf, 22234577Sahrens getprop_string(zhp, prop, &source), proplen); 22244577Sahrens break; 22254577Sahrens 22264787Sahrens case PROP_TYPE_INDEX: 22274861Sahrens if (get_numeric_property(zhp, prop, src, 22284861Sahrens &source, &val) != 0) 22294861Sahrens return (-1); 22304861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 22314577Sahrens return (-1); 22324577Sahrens (void) strlcpy(propbuf, strval, proplen); 22334577Sahrens break; 22344577Sahrens 22354577Sahrens default: 22364577Sahrens abort(); 22374577Sahrens } 2238789Sahrens } 2239789Sahrens 2240789Sahrens get_source(zhp, src, source, statbuf, statlen); 2241789Sahrens 2242789Sahrens return (0); 2243789Sahrens } 2244789Sahrens 2245789Sahrens /* 2246789Sahrens * Utility function to get the given numeric property. Does no validation that 2247789Sahrens * the given property is the appropriate type; should only be used with 2248789Sahrens * hard-coded property types. 2249789Sahrens */ 2250789Sahrens uint64_t 2251789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2252789Sahrens { 2253789Sahrens char *source; 22542082Seschrock uint64_t val; 22552082Seschrock 22565367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 22572082Seschrock 22582082Seschrock return (val); 2259789Sahrens } 2260789Sahrens 2261789Sahrens /* 2262789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2263789Sahrens */ 2264789Sahrens int 2265789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 22665094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2267789Sahrens { 2268789Sahrens char *source; 2269789Sahrens 2270789Sahrens /* 2271789Sahrens * Check to see if this property applies to our object 2272789Sahrens */ 22734849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 22743237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 22752082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 22762082Seschrock zfs_prop_to_name(prop))); 22774849Sahrens } 2278789Sahrens 2279789Sahrens if (src) 22805094Slling *src = ZPROP_SRC_NONE; 2281789Sahrens 22822082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 22832082Seschrock return (-1); 2284789Sahrens 2285789Sahrens get_source(zhp, src, source, statbuf, statlen); 2286789Sahrens 2287789Sahrens return (0); 2288789Sahrens } 2289789Sahrens 2290789Sahrens /* 2291789Sahrens * Returns the name of the given zfs handle. 2292789Sahrens */ 2293789Sahrens const char * 2294789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2295789Sahrens { 2296789Sahrens return (zhp->zfs_name); 2297789Sahrens } 2298789Sahrens 2299789Sahrens /* 2300789Sahrens * Returns the type of the given zfs handle. 2301789Sahrens */ 2302789Sahrens zfs_type_t 2303789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2304789Sahrens { 2305789Sahrens return (zhp->zfs_type); 2306789Sahrens } 2307789Sahrens 2308789Sahrens /* 23091356Seschrock * Iterate over all child filesystems 2310789Sahrens */ 2311789Sahrens int 23121356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2313789Sahrens { 2314789Sahrens zfs_cmd_t zc = { 0 }; 2315789Sahrens zfs_handle_t *nzhp; 2316789Sahrens int ret; 2317789Sahrens 23185367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 23195367Sahrens return (0); 23205367Sahrens 2321789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23222082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2323789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2324789Sahrens /* 2325789Sahrens * Ignore private dataset names. 2326789Sahrens */ 2327789Sahrens if (dataset_name_hidden(zc.zc_name)) 2328789Sahrens continue; 2329789Sahrens 2330789Sahrens /* 2331789Sahrens * Silently ignore errors, as the only plausible explanation is 2332789Sahrens * that the pool has since been removed. 2333789Sahrens */ 23342082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23352082Seschrock zc.zc_name)) == NULL) 2336789Sahrens continue; 2337789Sahrens 2338789Sahrens if ((ret = func(nzhp, data)) != 0) 2339789Sahrens return (ret); 2340789Sahrens } 2341789Sahrens 2342789Sahrens /* 2343789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2344789Sahrens * returned, then the underlying dataset has been removed since we 2345789Sahrens * obtained the handle. 2346789Sahrens */ 2347789Sahrens if (errno != ESRCH && errno != ENOENT) 23482082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23492082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2350789Sahrens 23511356Seschrock return (0); 23521356Seschrock } 23531356Seschrock 23541356Seschrock /* 23551356Seschrock * Iterate over all snapshots 23561356Seschrock */ 23571356Seschrock int 23581356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23591356Seschrock { 23601356Seschrock zfs_cmd_t zc = { 0 }; 23611356Seschrock zfs_handle_t *nzhp; 23621356Seschrock int ret; 2363789Sahrens 23645367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 23655367Sahrens return (0); 23665367Sahrens 2367789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23682082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 23692082Seschrock &zc) == 0; 2370789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2371789Sahrens 23722082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23732082Seschrock zc.zc_name)) == NULL) 2374789Sahrens continue; 2375789Sahrens 2376789Sahrens if ((ret = func(nzhp, data)) != 0) 2377789Sahrens return (ret); 2378789Sahrens } 2379789Sahrens 2380789Sahrens /* 2381789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2382789Sahrens * returned, then the underlying dataset has been removed since we 2383789Sahrens * obtained the handle. Silently ignore this case, and return success. 2384789Sahrens */ 2385789Sahrens if (errno != ESRCH && errno != ENOENT) 23862082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23872082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2388789Sahrens 2389789Sahrens return (0); 2390789Sahrens } 2391789Sahrens 2392789Sahrens /* 23931356Seschrock * Iterate over all children, snapshots and filesystems 23941356Seschrock */ 23951356Seschrock int 23961356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23971356Seschrock { 23981356Seschrock int ret; 23991356Seschrock 24001356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 24011356Seschrock return (ret); 24021356Seschrock 24031356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 24041356Seschrock } 24051356Seschrock 24061356Seschrock /* 2407789Sahrens * Given a complete name, return just the portion that refers to the parent. 2408789Sahrens * Can return NULL if this is a pool. 2409789Sahrens */ 2410789Sahrens static int 2411789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2412789Sahrens { 2413789Sahrens char *loc; 2414789Sahrens 2415789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2416789Sahrens return (-1); 2417789Sahrens 2418789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2419789Sahrens buf[loc - path] = '\0'; 2420789Sahrens 2421789Sahrens return (0); 2422789Sahrens } 2423789Sahrens 2424789Sahrens /* 24254490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 24264490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 24274490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 24284490Svb160487 * length of already existing prefix of the given path. We also fetch the 24294490Svb160487 * 'zoned' property, which is used to validate property settings when creating 24304490Svb160487 * new datasets. 2431789Sahrens */ 2432789Sahrens static int 24334490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 24344490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2435789Sahrens { 2436789Sahrens zfs_cmd_t zc = { 0 }; 2437789Sahrens char parent[ZFS_MAXNAMELEN]; 2438789Sahrens char *slash; 24391356Seschrock zfs_handle_t *zhp; 24402082Seschrock char errbuf[1024]; 24412082Seschrock 24422082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 24432082Seschrock path); 2444789Sahrens 2445789Sahrens /* get parent, and check to see if this is just a pool */ 2446789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 24472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24482082Seschrock "missing dataset name")); 24492082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2450789Sahrens } 2451789Sahrens 2452789Sahrens /* check to see if the pool exists */ 2453789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2454789Sahrens slash = parent + strlen(parent); 2455789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2456789Sahrens zc.zc_name[slash - parent] = '\0'; 24572082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2458789Sahrens errno == ENOENT) { 24592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24602082Seschrock "no such pool '%s'"), zc.zc_name); 24612082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2462789Sahrens } 2463789Sahrens 2464789Sahrens /* check to see if the parent dataset exists */ 24654490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 24664490Svb160487 if (errno == ENOENT && accept_ancestor) { 24674490Svb160487 /* 24684490Svb160487 * Go deeper to find an ancestor, give up on top level. 24694490Svb160487 */ 24704490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 24714490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24724490Svb160487 "no such pool '%s'"), zc.zc_name); 24734490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24744490Svb160487 } 24754490Svb160487 } else if (errno == ENOENT) { 24762082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24772082Seschrock "parent does not exist")); 24782082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24794490Svb160487 } else 24802082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2481789Sahrens } 2482789Sahrens 24832676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2484789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 24852676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 24862082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 24871356Seschrock zfs_close(zhp); 2488789Sahrens return (-1); 2489789Sahrens } 2490789Sahrens 2491789Sahrens /* make sure parent is a filesystem */ 24921356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 24932082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24942082Seschrock "parent is not a filesystem")); 24952082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 24961356Seschrock zfs_close(zhp); 2497789Sahrens return (-1); 2498789Sahrens } 2499789Sahrens 25001356Seschrock zfs_close(zhp); 25014490Svb160487 if (prefixlen != NULL) 25024490Svb160487 *prefixlen = strlen(parent); 25034490Svb160487 return (0); 25044490Svb160487 } 25054490Svb160487 25064490Svb160487 /* 25074490Svb160487 * Finds whether the dataset of the given type(s) exists. 25084490Svb160487 */ 25094490Svb160487 boolean_t 25104490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 25114490Svb160487 { 25124490Svb160487 zfs_handle_t *zhp; 25134490Svb160487 25145326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 25154490Svb160487 return (B_FALSE); 25164490Svb160487 25174490Svb160487 /* 25184490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 25194490Svb160487 */ 25204490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 25214490Svb160487 int ds_type = zhp->zfs_type; 25224490Svb160487 25234490Svb160487 zfs_close(zhp); 25244490Svb160487 if (types & ds_type) 25254490Svb160487 return (B_TRUE); 25264490Svb160487 } 25274490Svb160487 return (B_FALSE); 25284490Svb160487 } 25294490Svb160487 25304490Svb160487 /* 25315367Sahrens * Given a path to 'target', create all the ancestors between 25325367Sahrens * the prefixlen portion of the path, and the target itself. 25335367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 25345367Sahrens */ 25355367Sahrens int 25365367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 25375367Sahrens { 25385367Sahrens zfs_handle_t *h; 25395367Sahrens char *cp; 25405367Sahrens const char *opname; 25415367Sahrens 25425367Sahrens /* make sure prefix exists */ 25435367Sahrens cp = target + prefixlen; 25445367Sahrens if (*cp != '/') { 25455367Sahrens assert(strchr(cp, '/') == NULL); 25465367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25475367Sahrens } else { 25485367Sahrens *cp = '\0'; 25495367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25505367Sahrens *cp = '/'; 25515367Sahrens } 25525367Sahrens if (h == NULL) 25535367Sahrens return (-1); 25545367Sahrens zfs_close(h); 25555367Sahrens 25565367Sahrens /* 25575367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 25585367Sahrens * up to the prefixlen-long one. 25595367Sahrens */ 25605367Sahrens for (cp = target + prefixlen + 1; 25615367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 25625367Sahrens char *logstr; 25635367Sahrens 25645367Sahrens *cp = '\0'; 25655367Sahrens 25665367Sahrens h = make_dataset_handle(hdl, target); 25675367Sahrens if (h) { 25685367Sahrens /* it already exists, nothing to do here */ 25695367Sahrens zfs_close(h); 25705367Sahrens continue; 25715367Sahrens } 25725367Sahrens 25735367Sahrens logstr = hdl->libzfs_log_str; 25745367Sahrens hdl->libzfs_log_str = NULL; 25755367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 25765367Sahrens NULL) != 0) { 25775367Sahrens hdl->libzfs_log_str = logstr; 25785367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 25795367Sahrens goto ancestorerr; 25805367Sahrens } 25815367Sahrens 25825367Sahrens hdl->libzfs_log_str = logstr; 25835367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25845367Sahrens if (h == NULL) { 25855367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 25865367Sahrens goto ancestorerr; 25875367Sahrens } 25885367Sahrens 25895367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 25905367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 25915367Sahrens goto ancestorerr; 25925367Sahrens } 25935367Sahrens 25945367Sahrens if (zfs_share(h) != 0) { 25955367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 25965367Sahrens goto ancestorerr; 25975367Sahrens } 25985367Sahrens 25995367Sahrens zfs_close(h); 26005367Sahrens } 26015367Sahrens 26025367Sahrens return (0); 26035367Sahrens 26045367Sahrens ancestorerr: 26055367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26065367Sahrens "failed to %s ancestor '%s'"), opname, target); 26075367Sahrens return (-1); 26085367Sahrens } 26095367Sahrens 26105367Sahrens /* 26114490Svb160487 * Creates non-existing ancestors of the given path. 26124490Svb160487 */ 26134490Svb160487 int 26144490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 26154490Svb160487 { 26164490Svb160487 int prefix; 26174490Svb160487 uint64_t zoned; 26184490Svb160487 char *path_copy; 26194490Svb160487 int rc; 26204490Svb160487 26214490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 26224490Svb160487 return (-1); 26234490Svb160487 26244490Svb160487 if ((path_copy = strdup(path)) != NULL) { 26254490Svb160487 rc = create_parents(hdl, path_copy, prefix); 26264490Svb160487 free(path_copy); 26274490Svb160487 } 26284490Svb160487 if (path_copy == NULL || rc != 0) 26294490Svb160487 return (-1); 26304490Svb160487 2631789Sahrens return (0); 2632789Sahrens } 2633789Sahrens 2634789Sahrens /* 26352676Seschrock * Create a new filesystem or volume. 2636789Sahrens */ 2637789Sahrens int 26382082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 26392676Seschrock nvlist_t *props) 2640789Sahrens { 2641789Sahrens zfs_cmd_t zc = { 0 }; 2642789Sahrens int ret; 2643789Sahrens uint64_t size = 0; 2644789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 26452082Seschrock char errbuf[1024]; 26462676Seschrock uint64_t zoned; 26472082Seschrock 26482082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 26492082Seschrock "cannot create '%s'"), path); 2650789Sahrens 2651789Sahrens /* validate the path, taking care to note the extended error message */ 26525326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 26532082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2654789Sahrens 2655789Sahrens /* validate parents exist */ 26564490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2657789Sahrens return (-1); 2658789Sahrens 2659789Sahrens /* 2660789Sahrens * The failure modes when creating a dataset of a different type over 2661789Sahrens * one that already exists is a little strange. In particular, if you 2662789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2663789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2664789Sahrens * first try to see if the dataset exists. 2665789Sahrens */ 2666789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 26675094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 26682082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26692082Seschrock "dataset already exists")); 26702082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2671789Sahrens } 2672789Sahrens 2673789Sahrens if (type == ZFS_TYPE_VOLUME) 2674789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2675789Sahrens else 2676789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2677789Sahrens 26785094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 26793912Slling zoned, NULL, errbuf)) == 0) 26802676Seschrock return (-1); 26812676Seschrock 2682789Sahrens if (type == ZFS_TYPE_VOLUME) { 26831133Seschrock /* 26841133Seschrock * If we are creating a volume, the size and block size must 26851133Seschrock * satisfy a few restraints. First, the blocksize must be a 26861133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 26871133Seschrock * volsize must be a multiple of the block size, and cannot be 26881133Seschrock * zero. 26891133Seschrock */ 26902676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 26912676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 26922676Seschrock nvlist_free(props); 26932082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26942676Seschrock "missing volume size")); 26952676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2696789Sahrens } 2697789Sahrens 26982676Seschrock if ((ret = nvlist_lookup_uint64(props, 26992676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 27002676Seschrock &blocksize)) != 0) { 27012676Seschrock if (ret == ENOENT) { 27022676Seschrock blocksize = zfs_prop_default_numeric( 27032676Seschrock ZFS_PROP_VOLBLOCKSIZE); 27042676Seschrock } else { 27052676Seschrock nvlist_free(props); 27062676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27072676Seschrock "missing volume block size")); 27082676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27092676Seschrock } 27102676Seschrock } 27112676Seschrock 27122676Seschrock if (size == 0) { 27132676Seschrock nvlist_free(props); 27142082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27152676Seschrock "volume size cannot be zero")); 27162676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27171133Seschrock } 27181133Seschrock 27191133Seschrock if (size % blocksize != 0) { 27202676Seschrock nvlist_free(props); 27212082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27222676Seschrock "volume size must be a multiple of volume block " 27232676Seschrock "size")); 27242676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27251133Seschrock } 2726789Sahrens } 2727789Sahrens 27285094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 27292676Seschrock return (-1); 27302676Seschrock nvlist_free(props); 27312676Seschrock 2732789Sahrens /* create the dataset */ 27334543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2734789Sahrens 27353912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 27362082Seschrock ret = zvol_create_link(hdl, path); 27373912Slling if (ret) { 27383912Slling (void) zfs_standard_error(hdl, errno, 27393912Slling dgettext(TEXT_DOMAIN, 27403912Slling "Volume successfully created, but device links " 27413912Slling "were not created")); 27423912Slling zcmd_free_nvlists(&zc); 27433912Slling return (-1); 27443912Slling } 27453912Slling } 2746789Sahrens 27472676Seschrock zcmd_free_nvlists(&zc); 27482676Seschrock 2749789Sahrens /* check for failure */ 2750789Sahrens if (ret != 0) { 2751789Sahrens char parent[ZFS_MAXNAMELEN]; 2752789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2753789Sahrens 2754789Sahrens switch (errno) { 2755789Sahrens case ENOENT: 27562082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27572082Seschrock "no such parent '%s'"), parent); 27582082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2759789Sahrens 2760789Sahrens case EINVAL: 27612082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27623413Smmusante "parent '%s' is not a filesystem"), parent); 27632082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2764789Sahrens 2765789Sahrens case EDOM: 27662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27672676Seschrock "volume block size must be power of 2 from " 27682676Seschrock "%u to %uk"), 2769789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2770789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 27712082Seschrock 27722676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27732082Seschrock 27744603Sahrens case ENOTSUP: 27754603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27764603Sahrens "pool must be upgraded to set this " 27774603Sahrens "property or value")); 27784603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 27794603Sahrens 2780789Sahrens #ifdef _ILP32 2781789Sahrens case EOVERFLOW: 2782789Sahrens /* 2783789Sahrens * This platform can't address a volume this big. 2784789Sahrens */ 27852082Seschrock if (type == ZFS_TYPE_VOLUME) 27862082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 27872082Seschrock errbuf)); 2788789Sahrens #endif 27892082Seschrock /* FALLTHROUGH */ 2790789Sahrens default: 27912082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2792789Sahrens } 2793789Sahrens } 2794789Sahrens 2795789Sahrens return (0); 2796789Sahrens } 2797789Sahrens 2798789Sahrens /* 2799789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2800789Sahrens * isn't mounted, and that there are no active dependents. 2801789Sahrens */ 2802789Sahrens int 2803789Sahrens zfs_destroy(zfs_handle_t *zhp) 2804789Sahrens { 2805789Sahrens zfs_cmd_t zc = { 0 }; 2806789Sahrens 2807789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2808789Sahrens 28092676Seschrock if (ZFS_IS_VOLUME(zhp)) { 28103126Sahl /* 28114543Smarks * If user doesn't have permissions to unshare volume, then 28124543Smarks * abort the request. This would only happen for a 28134543Smarks * non-privileged user. 28143126Sahl */ 28154543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 28164543Smarks return (-1); 28174543Smarks } 28183126Sahl 28192082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2820789Sahrens return (-1); 2821789Sahrens 2822789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2823789Sahrens } else { 2824789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2825789Sahrens } 2826789Sahrens 28274543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 28283237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 28292082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 28302082Seschrock zhp->zfs_name)); 28312199Sahrens } 2832789Sahrens 2833789Sahrens remove_mountpoint(zhp); 2834789Sahrens 2835789Sahrens return (0); 2836789Sahrens } 2837789Sahrens 28382199Sahrens struct destroydata { 28392199Sahrens char *snapname; 28402199Sahrens boolean_t gotone; 28413265Sahrens boolean_t closezhp; 28422199Sahrens }; 28432199Sahrens 28442199Sahrens static int 28452199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 28462199Sahrens { 28472199Sahrens struct destroydata *dd = arg; 28482199Sahrens zfs_handle_t *szhp; 28492199Sahrens char name[ZFS_MAXNAMELEN]; 28503265Sahrens boolean_t closezhp = dd->closezhp; 28513265Sahrens int rv; 28522199Sahrens 28532676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 28542676Seschrock (void) strlcat(name, "@", sizeof (name)); 28552676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 28562199Sahrens 28572199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 28582199Sahrens if (szhp) { 28592199Sahrens dd->gotone = B_TRUE; 28602199Sahrens zfs_close(szhp); 28612199Sahrens } 28622199Sahrens 28632199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 28642199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 28652199Sahrens /* 28662199Sahrens * NB: this is simply a best-effort. We don't want to 28672199Sahrens * return an error, because then we wouldn't visit all 28682199Sahrens * the volumes. 28692199Sahrens */ 28702199Sahrens } 28712199Sahrens 28723265Sahrens dd->closezhp = B_TRUE; 28733265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 28743265Sahrens if (closezhp) 28753265Sahrens zfs_close(zhp); 28763265Sahrens return (rv); 28772199Sahrens } 28782199Sahrens 28792199Sahrens /* 28802199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 28812199Sahrens */ 28822199Sahrens int 28832199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 28842199Sahrens { 28852199Sahrens zfs_cmd_t zc = { 0 }; 28862199Sahrens int ret; 28872199Sahrens struct destroydata dd = { 0 }; 28882199Sahrens 28892199Sahrens dd.snapname = snapname; 28902199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 28912199Sahrens 28922199Sahrens if (!dd.gotone) { 28933237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 28942199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 28952199Sahrens zhp->zfs_name, snapname)); 28962199Sahrens } 28972199Sahrens 28982199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 28992676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 29002199Sahrens 29014543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 29022199Sahrens if (ret != 0) { 29032199Sahrens char errbuf[1024]; 29042199Sahrens 29052199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29062199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 29072199Sahrens 29082199Sahrens switch (errno) { 29092199Sahrens case EEXIST: 29102199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29112199Sahrens "snapshot is cloned")); 29122199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 29132199Sahrens 29142199Sahrens default: 29152199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 29162199Sahrens errbuf)); 29172199Sahrens } 29182199Sahrens } 29192199Sahrens 29202199Sahrens return (0); 29212199Sahrens } 29222199Sahrens 2923789Sahrens /* 2924789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2925789Sahrens */ 2926789Sahrens int 29272676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2928789Sahrens { 2929789Sahrens zfs_cmd_t zc = { 0 }; 2930789Sahrens char parent[ZFS_MAXNAMELEN]; 2931789Sahrens int ret; 29322082Seschrock char errbuf[1024]; 29332082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 29342676Seschrock zfs_type_t type; 29352676Seschrock uint64_t zoned; 2936789Sahrens 2937789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2938789Sahrens 29392082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29402082Seschrock "cannot create '%s'"), target); 29412082Seschrock 2942789Sahrens /* validate the target name */ 29435326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 29442082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2945789Sahrens 2946789Sahrens /* validate parents exist */ 29474490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2948789Sahrens return (-1); 2949789Sahrens 2950789Sahrens (void) parent_name(target, parent, sizeof (parent)); 2951789Sahrens 2952789Sahrens /* do the clone */ 29532676Seschrock if (ZFS_IS_VOLUME(zhp)) { 2954789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 29552744Snn35248 type = ZFS_TYPE_VOLUME; 29562676Seschrock } else { 2957789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 29582744Snn35248 type = ZFS_TYPE_FILESYSTEM; 29592676Seschrock } 29602676Seschrock 29612676Seschrock if (props) { 29625094Slling if ((props = zfs_validate_properties(hdl, type, props, 29633912Slling zoned, zhp, errbuf)) == NULL) 29642676Seschrock return (-1); 29652676Seschrock 29665094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 29672676Seschrock nvlist_free(props); 29682676Seschrock return (-1); 29692676Seschrock } 29702676Seschrock 29712676Seschrock nvlist_free(props); 29722676Seschrock } 2973789Sahrens 2974789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 29752676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 29764543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2977789Sahrens 29782676Seschrock zcmd_free_nvlists(&zc); 29792676Seschrock 2980789Sahrens if (ret != 0) { 2981789Sahrens switch (errno) { 2982789Sahrens 2983789Sahrens case ENOENT: 2984789Sahrens /* 2985789Sahrens * The parent doesn't exist. We should have caught this 2986789Sahrens * above, but there may a race condition that has since 2987789Sahrens * destroyed the parent. 2988789Sahrens * 2989789Sahrens * At this point, we don't know whether it's the source 2990789Sahrens * that doesn't exist anymore, or whether the target 2991789Sahrens * dataset doesn't exist. 2992789Sahrens */ 29932082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29942082Seschrock "no such parent '%s'"), parent); 29952082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 29962082Seschrock 29972082Seschrock case EXDEV: 29982082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29992082Seschrock "source and target pools differ")); 30002082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 30012082Seschrock errbuf)); 30022082Seschrock 30032082Seschrock default: 30042082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 30052082Seschrock errbuf)); 30062082Seschrock } 30072676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 30082082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 30092082Seschrock } 30102082Seschrock 30112082Seschrock return (ret); 30122082Seschrock } 30132082Seschrock 30142082Seschrock typedef struct promote_data { 30152082Seschrock char cb_mountpoint[MAXPATHLEN]; 30162082Seschrock const char *cb_target; 30172082Seschrock const char *cb_errbuf; 30182082Seschrock uint64_t cb_pivot_txg; 30192082Seschrock } promote_data_t; 30202082Seschrock 30212082Seschrock static int 30222082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 30232082Seschrock { 30242082Seschrock promote_data_t *pd = data; 30252082Seschrock zfs_handle_t *szhp; 30262082Seschrock char snapname[MAXPATHLEN]; 30273265Sahrens int rv = 0; 30282082Seschrock 30292082Seschrock /* We don't care about snapshots after the pivot point */ 30303265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 30313265Sahrens zfs_close(zhp); 30322082Seschrock return (0); 30333265Sahrens } 30342082Seschrock 30352417Sahrens /* Remove the device link if it's a zvol. */ 30362676Seschrock if (ZFS_IS_VOLUME(zhp)) 30372417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 30382082Seschrock 30392082Seschrock /* Check for conflicting names */ 30402676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 30412676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 30422082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 30432082Seschrock if (szhp != NULL) { 30442082Seschrock zfs_close(szhp); 30452082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30462082Seschrock "snapshot name '%s' from origin \n" 30472082Seschrock "conflicts with '%s' from target"), 30482082Seschrock zhp->zfs_name, snapname); 30493265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 30502082Seschrock } 30513265Sahrens zfs_close(zhp); 30523265Sahrens return (rv); 30532082Seschrock } 30542082Seschrock 30552417Sahrens static int 30562417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 30572417Sahrens { 30582417Sahrens promote_data_t *pd = data; 30592417Sahrens 30602417Sahrens /* We don't care about snapshots after the pivot point */ 30613265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 30623265Sahrens /* Create the device link if it's a zvol. */ 30633265Sahrens if (ZFS_IS_VOLUME(zhp)) 30643265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 30653265Sahrens } 30663265Sahrens 30673265Sahrens zfs_close(zhp); 30682417Sahrens return (0); 30692417Sahrens } 30702417Sahrens 30712082Seschrock /* 30722082Seschrock * Promotes the given clone fs to be the clone parent. 30732082Seschrock */ 30742082Seschrock int 30752082Seschrock zfs_promote(zfs_handle_t *zhp) 30762082Seschrock { 30772082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 30782082Seschrock zfs_cmd_t zc = { 0 }; 30792082Seschrock char parent[MAXPATHLEN]; 30802082Seschrock char *cp; 30812082Seschrock int ret; 30822082Seschrock zfs_handle_t *pzhp; 30832082Seschrock promote_data_t pd; 30842082Seschrock char errbuf[1024]; 30852082Seschrock 30862082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30872082Seschrock "cannot promote '%s'"), zhp->zfs_name); 30882082Seschrock 30892082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 30902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30912082Seschrock "snapshots can not be promoted")); 30922082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 30932082Seschrock } 30942082Seschrock 30955367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 30962082Seschrock if (parent[0] == '\0') { 30972082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30982082Seschrock "not a cloned filesystem")); 30992082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 31002082Seschrock } 31012082Seschrock cp = strchr(parent, '@'); 31022082Seschrock *cp = '\0'; 31032082Seschrock 31042082Seschrock /* Walk the snapshots we will be moving */ 31055367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 31062082Seschrock if (pzhp == NULL) 31072082Seschrock return (-1); 31082082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 31092082Seschrock zfs_close(pzhp); 31102082Seschrock pd.cb_target = zhp->zfs_name; 31112082Seschrock pd.cb_errbuf = errbuf; 31125094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 31132082Seschrock if (pzhp == NULL) 31142082Seschrock return (-1); 31152082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 31162082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 31172082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 31182417Sahrens if (ret != 0) { 31192417Sahrens zfs_close(pzhp); 31202082Seschrock return (-1); 31212417Sahrens } 31222082Seschrock 31232082Seschrock /* issue the ioctl */ 31245367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 31252676Seschrock sizeof (zc.zc_value)); 31262082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31274543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 31282082Seschrock 31292082Seschrock if (ret != 0) { 31302417Sahrens int save_errno = errno; 31312417Sahrens 31322417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 31332417Sahrens zfs_close(pzhp); 31342417Sahrens 31352417Sahrens switch (save_errno) { 3136789Sahrens case EEXIST: 3137789Sahrens /* 31382082Seschrock * There is a conflicting snapshot name. We 31392082Seschrock * should have caught this above, but they could 31402082Seschrock * have renamed something in the mean time. 3141789Sahrens */ 31422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31432082Seschrock "conflicting snapshot name from parent '%s'"), 31442082Seschrock parent); 31452082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3146789Sahrens 3147789Sahrens default: 31482417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3149789Sahrens } 31502417Sahrens } else { 31512417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3152789Sahrens } 3153789Sahrens 31542417Sahrens zfs_close(pzhp); 3155789Sahrens return (ret); 3156789Sahrens } 3157789Sahrens 31584007Smmusante struct createdata { 31594007Smmusante const char *cd_snapname; 31604007Smmusante int cd_ifexists; 31614007Smmusante }; 31624007Smmusante 31632199Sahrens static int 31642199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 31652199Sahrens { 31664007Smmusante struct createdata *cd = arg; 31672676Seschrock int ret; 31682199Sahrens 31692199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 31702199Sahrens char name[MAXPATHLEN]; 31712199Sahrens 31722676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 31732676Seschrock (void) strlcat(name, "@", sizeof (name)); 31744007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 31754007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 31764007Smmusante cd->cd_ifexists); 31772199Sahrens /* 31782199Sahrens * NB: this is simply a best-effort. We don't want to 31792199Sahrens * return an error, because then we wouldn't visit all 31802199Sahrens * the volumes. 31812199Sahrens */ 31822199Sahrens } 31832676Seschrock 31844007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 31852676Seschrock 31862676Seschrock zfs_close(zhp); 31872676Seschrock 31882676Seschrock return (ret); 31892199Sahrens } 31902199Sahrens 3191789Sahrens /* 31923504Sahl * Takes a snapshot of the given dataset. 3193789Sahrens */ 3194789Sahrens int 31952199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3196789Sahrens { 3197789Sahrens const char *delim; 3198789Sahrens char *parent; 3199789Sahrens zfs_handle_t *zhp; 3200789Sahrens zfs_cmd_t zc = { 0 }; 3201789Sahrens int ret; 32022082Seschrock char errbuf[1024]; 32032082Seschrock 32042082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32052082Seschrock "cannot snapshot '%s'"), path); 32062082Seschrock 32072082Seschrock /* validate the target name */ 32085326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 32092082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3210789Sahrens 3211789Sahrens /* make sure the parent exists and is of the appropriate type */ 32122199Sahrens delim = strchr(path, '@'); 32132082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 32142082Seschrock return (-1); 3215789Sahrens (void) strncpy(parent, path, delim - path); 3216789Sahrens parent[delim - path] = '\0'; 3217789Sahrens 32182082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3219789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3220789Sahrens free(parent); 3221789Sahrens return (-1); 3222789Sahrens } 3223789Sahrens 32242199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 32252676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 32264543Smarks if (ZFS_IS_VOLUME(zhp)) 32274543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 32284543Smarks else 32294543Smarks zc.zc_objset_type = DMU_OST_ZFS; 32302199Sahrens zc.zc_cookie = recursive; 32314543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 32322199Sahrens 32332199Sahrens /* 32342199Sahrens * if it was recursive, the one that actually failed will be in 32352199Sahrens * zc.zc_name. 32362199Sahrens */ 32374543Smarks if (ret != 0) 32384543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32394543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 32404543Smarks 32412199Sahrens if (ret == 0 && recursive) { 32424007Smmusante struct createdata cd; 32434007Smmusante 32444007Smmusante cd.cd_snapname = delim + 1; 32454007Smmusante cd.cd_ifexists = B_FALSE; 32464007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 32472199Sahrens } 3248789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 32492082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 32502199Sahrens if (ret != 0) { 32514543Smarks (void) zfs_standard_error(hdl, errno, 32524543Smarks dgettext(TEXT_DOMAIN, 32534543Smarks "Volume successfully snapshotted, but device links " 32544543Smarks "were not created")); 32554543Smarks free(parent); 32564543Smarks zfs_close(zhp); 32574543Smarks return (-1); 32582199Sahrens } 3259789Sahrens } 3260789Sahrens 32612082Seschrock if (ret != 0) 32622082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3263789Sahrens 3264789Sahrens free(parent); 3265789Sahrens zfs_close(zhp); 3266789Sahrens 3267789Sahrens return (ret); 3268789Sahrens } 3269789Sahrens 3270789Sahrens /* 32711294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 32721294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 32731294Slling * is a dependent and we should just destroy it without checking the transaction 32741294Slling * group. 3275789Sahrens */ 32761294Slling typedef struct rollback_data { 32771294Slling const char *cb_target; /* the snapshot */ 32781294Slling uint64_t cb_create; /* creation time reference */ 32791294Slling int cb_error; 32802082Seschrock boolean_t cb_dependent; 32811294Slling } rollback_data_t; 32821294Slling 32831294Slling static int 32841294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 32851294Slling { 32861294Slling rollback_data_t *cbp = data; 32871294Slling 32881294Slling if (!cbp->cb_dependent) { 32891294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 32901294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 32911294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 32921294Slling cbp->cb_create) { 32934543Smarks char *logstr; 32941294Slling 32952082Seschrock cbp->cb_dependent = B_TRUE; 32965446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 32975446Sahrens rollback_destroy, cbp); 32982082Seschrock cbp->cb_dependent = B_FALSE; 32991294Slling 33004543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 33014543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 33025446Sahrens cbp->cb_error |= zfs_destroy(zhp); 33034543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 33041294Slling } 33051294Slling } else { 33065446Sahrens cbp->cb_error |= zfs_destroy(zhp); 33071294Slling } 33081294Slling 33091294Slling zfs_close(zhp); 33101294Slling return (0); 33111294Slling } 33121294Slling 33131294Slling /* 33145446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 33155446Sahrens * data changes since then and making it the active dataset. 33165446Sahrens * 33175446Sahrens * Any snapshots more recent than the target are destroyed, along with 33185446Sahrens * their dependents. 33191294Slling */ 33205446Sahrens int 33215446Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap) 3322789Sahrens { 33235446Sahrens rollback_data_t cb = { 0 }; 33245446Sahrens int err; 3325789Sahrens zfs_cmd_t zc = { 0 }; 3326789Sahrens 3327789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3328789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3329789Sahrens 33305446Sahrens /* 33315446Sahrens * Destroy all recent snapshots and its dependends. 33325446Sahrens */ 33335446Sahrens cb.cb_target = snap->zfs_name; 33345446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 33355446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 33365446Sahrens 33375446Sahrens if (cb.cb_error != 0) 33385446Sahrens return (cb.cb_error); 33395446Sahrens 33405446Sahrens /* 33415446Sahrens * Now that we have verified that the snapshot is the latest, 33425446Sahrens * rollback to the given snapshot. 33435446Sahrens */ 33445446Sahrens 3345789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 33462082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3347789Sahrens return (-1); 3348789Sahrens 3349789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3350789Sahrens 33512676Seschrock if (ZFS_IS_VOLUME(zhp)) 3352789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3353789Sahrens else 3354789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3355789Sahrens 3356789Sahrens /* 33575446Sahrens * We rely on zfs_iter_children() to verify that there are no 33585446Sahrens * newer snapshots for the given dataset. Therefore, we can 33595446Sahrens * simply pass the name on to the ioctl() call. There is still 33605446Sahrens * an unlikely race condition where the user has taken a 33615446Sahrens * snapshot since we verified that this was the most recent. 3362789Sahrens */ 33635446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 33643237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 33652082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 33662082Seschrock zhp->zfs_name); 3367789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33685446Sahrens err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3369789Sahrens } 3370789Sahrens 33715446Sahrens return (err); 33721294Slling } 33731294Slling 33741294Slling /* 3375789Sahrens * Iterate over all dependents for a given dataset. This includes both 3376789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3377789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3378789Sahrens * libzfs_graph.c. 3379789Sahrens */ 3380789Sahrens int 33812474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 33822474Seschrock zfs_iter_f func, void *data) 3383789Sahrens { 3384789Sahrens char **dependents; 3385789Sahrens size_t count; 3386789Sahrens int i; 3387789Sahrens zfs_handle_t *child; 3388789Sahrens int ret = 0; 3389789Sahrens 33902474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 33912474Seschrock &dependents, &count) != 0) 33922474Seschrock return (-1); 33932474Seschrock 3394789Sahrens for (i = 0; i < count; i++) { 33952082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 33962082Seschrock dependents[i])) == NULL) 3397789Sahrens continue; 3398789Sahrens 3399789Sahrens if ((ret = func(child, data)) != 0) 3400789Sahrens break; 3401789Sahrens } 3402789Sahrens 3403789Sahrens for (i = 0; i < count; i++) 3404789Sahrens free(dependents[i]); 3405789Sahrens free(dependents); 3406789Sahrens 3407789Sahrens return (ret); 3408789Sahrens } 3409789Sahrens 3410789Sahrens /* 3411789Sahrens * Renames the given dataset. 3412789Sahrens */ 3413789Sahrens int 34144490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3415789Sahrens { 3416789Sahrens int ret; 3417789Sahrens zfs_cmd_t zc = { 0 }; 3418789Sahrens char *delim; 34194007Smmusante prop_changelist_t *cl = NULL; 34204007Smmusante zfs_handle_t *zhrp = NULL; 34214007Smmusante char *parentname = NULL; 3422789Sahrens char parent[ZFS_MAXNAMELEN]; 34232082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 34242082Seschrock char errbuf[1024]; 3425789Sahrens 3426789Sahrens /* if we have the same exact name, just return success */ 3427789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3428789Sahrens return (0); 3429789Sahrens 34302082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34312082Seschrock "cannot rename to '%s'"), target); 34322082Seschrock 3433789Sahrens /* 3434789Sahrens * Make sure the target name is valid 3435789Sahrens */ 3436789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 34372665Snd150628 if ((strchr(target, '@') == NULL) || 34382665Snd150628 *target == '@') { 34392665Snd150628 /* 34402665Snd150628 * Snapshot target name is abbreviated, 34412665Snd150628 * reconstruct full dataset name 34422665Snd150628 */ 34432665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 34442665Snd150628 sizeof (parent)); 34452665Snd150628 delim = strchr(parent, '@'); 34462665Snd150628 if (strchr(target, '@') == NULL) 34472665Snd150628 *(++delim) = '\0'; 34482665Snd150628 else 34492665Snd150628 *delim = '\0'; 34502665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 34512665Snd150628 target = parent; 34522665Snd150628 } else { 34532665Snd150628 /* 34542665Snd150628 * Make sure we're renaming within the same dataset. 34552665Snd150628 */ 34562665Snd150628 delim = strchr(target, '@'); 34572665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 34582665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 34592665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34602665Snd150628 "snapshots must be part of same " 34612665Snd150628 "dataset")); 34622665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 34633912Slling errbuf)); 34642665Snd150628 } 3465789Sahrens } 34665326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 34672665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3468789Sahrens } else { 34694007Smmusante if (recursive) { 34704007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34714007Smmusante "recursive rename must be a snapshot")); 34724007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 34734007Smmusante } 34744007Smmusante 34755326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 34762665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 34772676Seschrock uint64_t unused; 34782676Seschrock 3479789Sahrens /* validate parents */ 34804490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3481789Sahrens return (-1); 3482789Sahrens 3483789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3484789Sahrens 3485789Sahrens /* make sure we're in the same pool */ 3486789Sahrens verify((delim = strchr(target, '/')) != NULL); 3487789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3488789Sahrens zhp->zfs_name[delim - target] != '/') { 34892082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34902082Seschrock "datasets must be within same pool")); 34912082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3492789Sahrens } 34932440Snd150628 34942440Snd150628 /* new name cannot be a child of the current dataset name */ 34952440Snd150628 if (strncmp(parent, zhp->zfs_name, 34963912Slling strlen(zhp->zfs_name)) == 0) { 34972440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34982440Snd150628 "New dataset name cannot be a descendent of " 34992440Snd150628 "current dataset name")); 35002440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 35012440Snd150628 } 3502789Sahrens } 3503789Sahrens 35042082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 35052082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 35062082Seschrock 3507789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3508789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 35092082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35102082Seschrock "dataset is used in a non-global zone")); 35112082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3512789Sahrens } 3513789Sahrens 35144007Smmusante if (recursive) { 35154007Smmusante struct destroydata dd; 35164007Smmusante 35174183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 35184183Smmusante if (parentname == NULL) { 35194183Smmusante ret = -1; 35204183Smmusante goto error; 35214183Smmusante } 35224007Smmusante delim = strchr(parentname, '@'); 35234007Smmusante *delim = '\0'; 35245094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 35254007Smmusante if (zhrp == NULL) { 35264183Smmusante ret = -1; 35274183Smmusante goto error; 35284007Smmusante } 35294007Smmusante 35304007Smmusante dd.snapname = delim + 1; 35314007Smmusante dd.gotone = B_FALSE; 35324183Smmusante dd.closezhp = B_TRUE; 35334007Smmusante 35344007Smmusante /* We remove any zvol links prior to renaming them */ 35354007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 35364007Smmusante if (ret) { 35374007Smmusante goto error; 35384007Smmusante } 35394007Smmusante } else { 35404007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 35414007Smmusante return (-1); 35424007Smmusante 35434007Smmusante if (changelist_haszonedchild(cl)) { 35444007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35454007Smmusante "child dataset with inherited mountpoint is used " 35464007Smmusante "in a non-global zone")); 35474007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 35484007Smmusante goto error; 35494007Smmusante } 35504007Smmusante 35514007Smmusante if ((ret = changelist_prefix(cl)) != 0) 35524007Smmusante goto error; 3553789Sahrens } 3554789Sahrens 35552676Seschrock if (ZFS_IS_VOLUME(zhp)) 3556789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3557789Sahrens else 3558789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3559789Sahrens 35602665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35612676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 35622665Snd150628 35634007Smmusante zc.zc_cookie = recursive; 35644007Smmusante 35654543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 35664007Smmusante /* 35674007Smmusante * if it was recursive, the one that actually failed will 35684007Smmusante * be in zc.zc_name 35694007Smmusante */ 35704007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35715367Sahrens "cannot rename '%s'"), zc.zc_name); 35724007Smmusante 35734007Smmusante if (recursive && errno == EEXIST) { 35744007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35754007Smmusante "a child dataset already has a snapshot " 35764007Smmusante "with the new name")); 35774801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 35784007Smmusante } else { 35794007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 35804007Smmusante } 3581789Sahrens 3582789Sahrens /* 3583789Sahrens * On failure, we still want to remount any filesystems that 3584789Sahrens * were previously mounted, so we don't alter the system state. 3585789Sahrens */ 35864007Smmusante if (recursive) { 35874007Smmusante struct createdata cd; 35884007Smmusante 35894007Smmusante /* only create links for datasets that had existed */ 35904007Smmusante cd.cd_snapname = delim + 1; 35914007Smmusante cd.cd_ifexists = B_TRUE; 35924007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 35934007Smmusante &cd); 35944007Smmusante } else { 35954007Smmusante (void) changelist_postfix(cl); 35964007Smmusante } 3597789Sahrens } else { 35984007Smmusante if (recursive) { 35994007Smmusante struct createdata cd; 36004007Smmusante 36014007Smmusante /* only create links for datasets that had existed */ 36024007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 36034007Smmusante cd.cd_ifexists = B_TRUE; 36044007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36054007Smmusante &cd); 36064007Smmusante } else { 36074007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 36084007Smmusante ret = changelist_postfix(cl); 36094007Smmusante } 3610789Sahrens } 3611789Sahrens 3612789Sahrens error: 36134007Smmusante if (parentname) { 36144007Smmusante free(parentname); 36154007Smmusante } 36164007Smmusante if (zhrp) { 36174007Smmusante zfs_close(zhrp); 36184007Smmusante } 36194007Smmusante if (cl) { 36204007Smmusante changelist_free(cl); 36214007Smmusante } 3622789Sahrens return (ret); 3623789Sahrens } 3624789Sahrens 3625789Sahrens /* 3626789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3627789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3628789Sahrens */ 3629789Sahrens int 36302082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3631789Sahrens { 36324007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 36334007Smmusante } 36344007Smmusante 36354007Smmusante static int 36364007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 36374007Smmusante { 3638789Sahrens zfs_cmd_t zc = { 0 }; 36392082Seschrock di_devlink_handle_t dhdl; 36404543Smarks priv_set_t *priv_effective; 36414543Smarks int privileged; 3642789Sahrens 3643789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3644789Sahrens 3645789Sahrens /* 3646789Sahrens * Issue the appropriate ioctl. 3647789Sahrens */ 36482082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3649789Sahrens switch (errno) { 3650789Sahrens case EEXIST: 3651789Sahrens /* 3652789Sahrens * Silently ignore the case where the link already 3653789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3654789Sahrens * times without errors. 3655789Sahrens */ 3656789Sahrens return (0); 3657789Sahrens 36584007Smmusante case ENOENT: 36594007Smmusante /* 36604007Smmusante * Dataset does not exist in the kernel. If we 36614007Smmusante * don't care (see zfs_rename), then ignore the 36624007Smmusante * error quietly. 36634007Smmusante */ 36644007Smmusante if (ifexists) { 36654007Smmusante return (0); 36664007Smmusante } 36674007Smmusante 36684007Smmusante /* FALLTHROUGH */ 36694007Smmusante 3670789Sahrens default: 36713237Slling return (zfs_standard_error_fmt(hdl, errno, 36722082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 36732082Seschrock "for '%s'"), dataset)); 3674789Sahrens } 3675789Sahrens } 3676789Sahrens 3677789Sahrens /* 36784543Smarks * If privileged call devfsadm and wait for the links to 36794543Smarks * magically appear. 36804543Smarks * Otherwise, print out an informational message. 3681789Sahrens */ 36824543Smarks 36834543Smarks priv_effective = priv_allocset(); 36844543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 36854543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 36864543Smarks priv_freeset(priv_effective); 36874543Smarks 36884543Smarks if (privileged) { 36894543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 36904543Smarks DI_MAKE_LINK)) == NULL) { 36914543Smarks zfs_error_aux(hdl, strerror(errno)); 36924543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 36934543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 36944543Smarks "for '%s'"), dataset); 36954543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 36964543Smarks return (-1); 36974543Smarks } else { 36984543Smarks (void) di_devlink_fini(&dhdl); 36994543Smarks } 3700789Sahrens } else { 37014543Smarks char pathname[MAXPATHLEN]; 37024543Smarks struct stat64 statbuf; 37034543Smarks int i; 37044543Smarks 37054543Smarks #define MAX_WAIT 10 37064543Smarks 37074543Smarks /* 37084543Smarks * This is the poor mans way of waiting for the link 37094543Smarks * to show up. If after 10 seconds we still don't 37104543Smarks * have it, then print out a message. 37114543Smarks */ 37124543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 37134543Smarks dataset); 37144543Smarks 37154543Smarks for (i = 0; i != MAX_WAIT; i++) { 37164543Smarks if (stat64(pathname, &statbuf) == 0) 37174543Smarks break; 37184543Smarks (void) sleep(1); 37194543Smarks } 37204543Smarks if (i == MAX_WAIT) 37214543Smarks (void) printf(gettext("%s may not be immediately " 37224543Smarks "available\n"), pathname); 3723789Sahrens } 3724789Sahrens 3725789Sahrens return (0); 3726789Sahrens } 3727789Sahrens 3728789Sahrens /* 3729789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 3730789Sahrens */ 3731789Sahrens int 37322082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3733789Sahrens { 3734789Sahrens zfs_cmd_t zc = { 0 }; 3735789Sahrens 3736789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3737789Sahrens 37382082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3739789Sahrens switch (errno) { 3740789Sahrens case ENXIO: 3741789Sahrens /* 3742789Sahrens * Silently ignore the case where the link no longer 3743789Sahrens * exists, so that 'zfs volfini' can be run multiple 3744789Sahrens * times without errors. 3745789Sahrens */ 3746789Sahrens return (0); 3747789Sahrens 3748789Sahrens default: 37493237Slling return (zfs_standard_error_fmt(hdl, errno, 37502082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 37512082Seschrock "links for '%s'"), dataset)); 3752789Sahrens } 3753789Sahrens } 3754789Sahrens 3755789Sahrens return (0); 3756789Sahrens } 37572676Seschrock 37582676Seschrock nvlist_t * 37592676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 37602676Seschrock { 37612676Seschrock return (zhp->zfs_user_props); 37622676Seschrock } 37632676Seschrock 37642676Seschrock /* 37653912Slling * This function is used by 'zfs list' to determine the exact set of columns to 37663912Slling * display, and their maximum widths. This does two main things: 37673912Slling * 37683912Slling * - If this is a list of all properties, then expand the list to include 37693912Slling * all native properties, and set a flag so that for each dataset we look 37703912Slling * for new unique user properties and add them to the list. 37713912Slling * 37723912Slling * - For non fixed-width properties, keep track of the maximum width seen 37733912Slling * so that we can size the column appropriately. 37743912Slling */ 37753912Slling int 37765094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 37773912Slling { 37783912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 37795094Slling zprop_list_t *entry; 37805094Slling zprop_list_t **last, **start; 37813912Slling nvlist_t *userprops, *propval; 37823912Slling nvpair_t *elem; 37833912Slling char *strval; 37843912Slling char buf[ZFS_MAXPROPLEN]; 37853912Slling 37865094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 37873912Slling return (-1); 37882676Seschrock 37892676Seschrock userprops = zfs_get_user_props(zhp); 37902676Seschrock 37912676Seschrock entry = *plp; 37922676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 37932676Seschrock /* 37942676Seschrock * Go through and add any user properties as necessary. We 37952676Seschrock * start by incrementing our list pointer to the first 37962676Seschrock * non-native property. 37972676Seschrock */ 37982676Seschrock start = plp; 37992676Seschrock while (*start != NULL) { 38005094Slling if ((*start)->pl_prop == ZPROP_INVAL) 38012676Seschrock break; 38022676Seschrock start = &(*start)->pl_next; 38032676Seschrock } 38042676Seschrock 38052676Seschrock elem = NULL; 38062676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 38072676Seschrock /* 38082676Seschrock * See if we've already found this property in our list. 38092676Seschrock */ 38102676Seschrock for (last = start; *last != NULL; 38112676Seschrock last = &(*last)->pl_next) { 38122676Seschrock if (strcmp((*last)->pl_user_prop, 38132676Seschrock nvpair_name(elem)) == 0) 38142676Seschrock break; 38152676Seschrock } 38162676Seschrock 38172676Seschrock if (*last == NULL) { 38182676Seschrock if ((entry = zfs_alloc(hdl, 38195094Slling sizeof (zprop_list_t))) == NULL || 38202676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 38212676Seschrock nvpair_name(elem)))) == NULL) { 38222676Seschrock free(entry); 38232676Seschrock return (-1); 38242676Seschrock } 38252676Seschrock 38265094Slling entry->pl_prop = ZPROP_INVAL; 38272676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 38282676Seschrock entry->pl_all = B_TRUE; 38292676Seschrock *last = entry; 38302676Seschrock } 38312676Seschrock } 38322676Seschrock } 38332676Seschrock 38342676Seschrock /* 38352676Seschrock * Now go through and check the width of any non-fixed columns 38362676Seschrock */ 38372676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 38382676Seschrock if (entry->pl_fixed) 38392676Seschrock continue; 38402676Seschrock 38415094Slling if (entry->pl_prop != ZPROP_INVAL) { 38422676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 38432676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 38442676Seschrock if (strlen(buf) > entry->pl_width) 38452676Seschrock entry->pl_width = strlen(buf); 38462676Seschrock } 38472676Seschrock } else if (nvlist_lookup_nvlist(userprops, 38482676Seschrock entry->pl_user_prop, &propval) == 0) { 38492676Seschrock verify(nvlist_lookup_string(propval, 38505094Slling ZPROP_VALUE, &strval) == 0); 38512676Seschrock if (strlen(strval) > entry->pl_width) 38522676Seschrock entry->pl_width = strlen(strval); 38532676Seschrock } 38542676Seschrock } 38552676Seschrock 38562676Seschrock return (0); 38572676Seschrock } 38584543Smarks 38594543Smarks int 38604543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 38614543Smarks { 38624543Smarks zfs_cmd_t zc = { 0 }; 38634543Smarks nvlist_t *nvp; 38644543Smarks gid_t gid; 38654543Smarks uid_t uid; 38664543Smarks const gid_t *groups; 38674543Smarks int group_cnt; 38684543Smarks int error; 38694543Smarks 38704543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 38714543Smarks return (no_memory(hdl)); 38724543Smarks 38734543Smarks uid = ucred_geteuid(cred); 38744543Smarks gid = ucred_getegid(cred); 38754543Smarks group_cnt = ucred_getgroups(cred, &groups); 38764543Smarks 38774543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 38784543Smarks return (1); 38794543Smarks 38804543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 38814543Smarks nvlist_free(nvp); 38824543Smarks return (1); 38834543Smarks } 38844543Smarks 38854543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 38864543Smarks nvlist_free(nvp); 38874543Smarks return (1); 38884543Smarks } 38894543Smarks 38904543Smarks if (nvlist_add_uint32_array(nvp, 38914543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 38924543Smarks nvlist_free(nvp); 38934543Smarks return (1); 38944543Smarks } 38954543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 38964543Smarks 38975094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 38984543Smarks return (-1); 38994543Smarks 39004543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 39014543Smarks nvlist_free(nvp); 39024543Smarks return (error); 39034543Smarks } 39044543Smarks 39054543Smarks int 39064543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 39075331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 39084543Smarks { 39094543Smarks zfs_cmd_t zc = { 0 }; 39104543Smarks int error; 39114543Smarks 39124543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39134543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 39144543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 39154543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 39165331Samw zc.zc_share.z_sharetype = operation; 39174543Smarks zc.zc_share.z_sharemax = sharemax; 39184543Smarks 39194543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 39204543Smarks return (error); 39214543Smarks } 3922