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: 19785147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19795147Srm160521 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_VERSION, &zc) || 19805147Srm160521 (zc.zc_cookie == 0)) { 19815147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19825147Srm160521 "unable to get version property")); 19835147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 19845147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 19855147Srm160521 } 19865147Srm160521 *val = zc.zc_cookie; 19875147Srm160521 break; 19885147Srm160521 1989789Sahrens default: 19904577Sahrens switch (zfs_prop_get_type(prop)) { 19914787Sahrens case PROP_TYPE_NUMBER: 19924787Sahrens case PROP_TYPE_INDEX: 19934577Sahrens *val = getprop_uint64(zhp, prop, source); 19944577Sahrens break; 19954577Sahrens 19964787Sahrens case PROP_TYPE_STRING: 19974577Sahrens default: 19984577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19994577Sahrens "cannot get non-numeric property")); 20004577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 20014577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 20024577Sahrens } 2003789Sahrens } 2004789Sahrens 2005789Sahrens return (0); 2006789Sahrens } 2007789Sahrens 2008789Sahrens /* 2009789Sahrens * Calculate the source type, given the raw source string. 2010789Sahrens */ 2011789Sahrens static void 20125094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2013789Sahrens char *statbuf, size_t statlen) 2014789Sahrens { 20155094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2016789Sahrens return; 2017789Sahrens 2018789Sahrens if (source == NULL) { 20195094Slling *srctype = ZPROP_SRC_NONE; 2020789Sahrens } else if (source[0] == '\0') { 20215094Slling *srctype = ZPROP_SRC_DEFAULT; 2022789Sahrens } else { 2023789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 20245094Slling *srctype = ZPROP_SRC_LOCAL; 2025789Sahrens } else { 2026789Sahrens (void) strlcpy(statbuf, source, statlen); 20275094Slling *srctype = ZPROP_SRC_INHERITED; 2028789Sahrens } 2029789Sahrens } 2030789Sahrens 2031789Sahrens } 2032789Sahrens 2033789Sahrens /* 2034789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2035789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2036789Sahrens * human-readable form. 2037789Sahrens * 2038789Sahrens * Returns 0 on success, or -1 on error. 2039789Sahrens */ 2040789Sahrens int 2041789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 20425094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2043789Sahrens { 2044789Sahrens char *source = NULL; 2045789Sahrens uint64_t val; 2046789Sahrens char *str; 2047789Sahrens const char *root; 20482676Seschrock const char *strval; 2049789Sahrens 2050789Sahrens /* 2051789Sahrens * Check to see if this property applies to our object 2052789Sahrens */ 2053789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2054789Sahrens return (-1); 2055789Sahrens 2056789Sahrens if (src) 20575094Slling *src = ZPROP_SRC_NONE; 2058789Sahrens 2059789Sahrens switch (prop) { 2060789Sahrens case ZFS_PROP_CREATION: 2061789Sahrens /* 2062789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2063789Sahrens * this into a string unless 'literal' is specified. 2064789Sahrens */ 2065789Sahrens { 20662885Sahrens val = getprop_uint64(zhp, prop, &source); 20672885Sahrens time_t time = (time_t)val; 2068789Sahrens struct tm t; 2069789Sahrens 2070789Sahrens if (literal || 2071789Sahrens localtime_r(&time, &t) == NULL || 2072789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2073789Sahrens &t) == 0) 20742885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2075789Sahrens } 2076789Sahrens break; 2077789Sahrens 2078789Sahrens case ZFS_PROP_MOUNTPOINT: 2079789Sahrens /* 2080789Sahrens * Getting the precise mountpoint can be tricky. 2081789Sahrens * 2082789Sahrens * - for 'none' or 'legacy', return those values. 2083789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2084789Sahrens * - for inherited mountpoints, we want to take everything 2085789Sahrens * after our ancestor and append it to the inherited value. 2086789Sahrens * 2087789Sahrens * If the pool has an alternate root, we want to prepend that 2088789Sahrens * root to any values we return. 2089789Sahrens */ 20901544Seschrock root = zhp->zfs_root; 20911356Seschrock str = getprop_string(zhp, prop, &source); 20921356Seschrock 20931356Seschrock if (str[0] == '\0') { 2094789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2095789Sahrens root, zhp->zfs_name); 20961356Seschrock } else if (str[0] == '/') { 20971356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2098789Sahrens 2099789Sahrens if (relpath[0] == '/') 2100789Sahrens relpath++; 21011356Seschrock if (str[1] == '\0') 21021356Seschrock str++; 2103789Sahrens 2104789Sahrens if (relpath[0] == '\0') 2105789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 21061356Seschrock root, str); 2107789Sahrens else 2108789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 21091356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2110789Sahrens relpath); 2111789Sahrens } else { 2112789Sahrens /* 'legacy' or 'none' */ 21131356Seschrock (void) strlcpy(propbuf, str, proplen); 2114789Sahrens } 2115789Sahrens 2116789Sahrens break; 2117789Sahrens 2118789Sahrens case ZFS_PROP_ORIGIN: 21192885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2120789Sahrens proplen); 2121789Sahrens /* 2122789Sahrens * If there is no parent at all, return failure to indicate that 2123789Sahrens * it doesn't apply to this dataset. 2124789Sahrens */ 2125789Sahrens if (propbuf[0] == '\0') 2126789Sahrens return (-1); 2127789Sahrens break; 2128789Sahrens 2129789Sahrens case ZFS_PROP_QUOTA: 21305378Sck153898 case ZFS_PROP_REFQUOTA: 2131789Sahrens case ZFS_PROP_RESERVATION: 21325378Sck153898 case ZFS_PROP_REFRESERVATION: 21335378Sck153898 21342082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21352082Seschrock return (-1); 2136789Sahrens 2137789Sahrens /* 2138789Sahrens * If quota or reservation is 0, we translate this into 'none' 2139789Sahrens * (unless literal is set), and indicate that it's the default 2140789Sahrens * value. Otherwise, we print the number nicely and indicate 2141789Sahrens * that its set locally. 2142789Sahrens */ 2143789Sahrens if (val == 0) { 2144789Sahrens if (literal) 2145789Sahrens (void) strlcpy(propbuf, "0", proplen); 2146789Sahrens else 2147789Sahrens (void) strlcpy(propbuf, "none", proplen); 2148789Sahrens } else { 2149789Sahrens if (literal) 21502856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 21513912Slling (u_longlong_t)val); 2152789Sahrens else 2153789Sahrens zfs_nicenum(val, propbuf, proplen); 2154789Sahrens } 2155789Sahrens break; 2156789Sahrens 2157789Sahrens case ZFS_PROP_COMPRESSRATIO: 21582082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21592082Seschrock return (-1); 21602856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 21612856Snd150628 val / 100, (longlong_t)val % 100); 2162789Sahrens break; 2163789Sahrens 2164789Sahrens case ZFS_PROP_TYPE: 2165789Sahrens switch (zhp->zfs_type) { 2166789Sahrens case ZFS_TYPE_FILESYSTEM: 2167789Sahrens str = "filesystem"; 2168789Sahrens break; 2169789Sahrens case ZFS_TYPE_VOLUME: 2170789Sahrens str = "volume"; 2171789Sahrens break; 2172789Sahrens case ZFS_TYPE_SNAPSHOT: 2173789Sahrens str = "snapshot"; 2174789Sahrens break; 2175789Sahrens default: 21762082Seschrock abort(); 2177789Sahrens } 2178789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2179789Sahrens break; 2180789Sahrens 2181789Sahrens case ZFS_PROP_MOUNTED: 2182789Sahrens /* 2183789Sahrens * The 'mounted' property is a pseudo-property that described 2184789Sahrens * whether the filesystem is currently mounted. Even though 2185789Sahrens * it's a boolean value, the typical values of "on" and "off" 2186789Sahrens * don't make sense, so we translate to "yes" and "no". 2187789Sahrens */ 21882082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 21892082Seschrock src, &source, &val) != 0) 21902082Seschrock return (-1); 21912082Seschrock if (val) 2192789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2193789Sahrens else 2194789Sahrens (void) strlcpy(propbuf, "no", proplen); 2195789Sahrens break; 2196789Sahrens 2197789Sahrens case ZFS_PROP_NAME: 2198789Sahrens /* 2199789Sahrens * The 'name' property is a pseudo-property derived from the 2200789Sahrens * dataset name. It is presented as a real property to simplify 2201789Sahrens * consumers. 2202789Sahrens */ 2203789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2204789Sahrens break; 2205789Sahrens 2206789Sahrens default: 22074577Sahrens switch (zfs_prop_get_type(prop)) { 22084787Sahrens case PROP_TYPE_NUMBER: 22094577Sahrens if (get_numeric_property(zhp, prop, src, 22104577Sahrens &source, &val) != 0) 22114577Sahrens return (-1); 22124577Sahrens if (literal) 22134577Sahrens (void) snprintf(propbuf, proplen, "%llu", 22144577Sahrens (u_longlong_t)val); 22154577Sahrens else 22164577Sahrens zfs_nicenum(val, propbuf, proplen); 22174577Sahrens break; 22184577Sahrens 22194787Sahrens case PROP_TYPE_STRING: 22204577Sahrens (void) strlcpy(propbuf, 22214577Sahrens getprop_string(zhp, prop, &source), proplen); 22224577Sahrens break; 22234577Sahrens 22244787Sahrens case PROP_TYPE_INDEX: 22254861Sahrens if (get_numeric_property(zhp, prop, src, 22264861Sahrens &source, &val) != 0) 22274861Sahrens return (-1); 22284861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 22294577Sahrens return (-1); 22304577Sahrens (void) strlcpy(propbuf, strval, proplen); 22314577Sahrens break; 22324577Sahrens 22334577Sahrens default: 22344577Sahrens abort(); 22354577Sahrens } 2236789Sahrens } 2237789Sahrens 2238789Sahrens get_source(zhp, src, source, statbuf, statlen); 2239789Sahrens 2240789Sahrens return (0); 2241789Sahrens } 2242789Sahrens 2243789Sahrens /* 2244789Sahrens * Utility function to get the given numeric property. Does no validation that 2245789Sahrens * the given property is the appropriate type; should only be used with 2246789Sahrens * hard-coded property types. 2247789Sahrens */ 2248789Sahrens uint64_t 2249789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2250789Sahrens { 2251789Sahrens char *source; 22522082Seschrock uint64_t val; 22532082Seschrock 22545367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 22552082Seschrock 22562082Seschrock return (val); 2257789Sahrens } 2258789Sahrens 2259789Sahrens /* 2260789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2261789Sahrens */ 2262789Sahrens int 2263789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 22645094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2265789Sahrens { 2266789Sahrens char *source; 2267789Sahrens 2268789Sahrens /* 2269789Sahrens * Check to see if this property applies to our object 2270789Sahrens */ 22714849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 22723237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 22732082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 22742082Seschrock zfs_prop_to_name(prop))); 22754849Sahrens } 2276789Sahrens 2277789Sahrens if (src) 22785094Slling *src = ZPROP_SRC_NONE; 2279789Sahrens 22802082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 22812082Seschrock return (-1); 2282789Sahrens 2283789Sahrens get_source(zhp, src, source, statbuf, statlen); 2284789Sahrens 2285789Sahrens return (0); 2286789Sahrens } 2287789Sahrens 2288789Sahrens /* 2289789Sahrens * Returns the name of the given zfs handle. 2290789Sahrens */ 2291789Sahrens const char * 2292789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2293789Sahrens { 2294789Sahrens return (zhp->zfs_name); 2295789Sahrens } 2296789Sahrens 2297789Sahrens /* 2298789Sahrens * Returns the type of the given zfs handle. 2299789Sahrens */ 2300789Sahrens zfs_type_t 2301789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2302789Sahrens { 2303789Sahrens return (zhp->zfs_type); 2304789Sahrens } 2305789Sahrens 2306789Sahrens /* 23071356Seschrock * Iterate over all child filesystems 2308789Sahrens */ 2309789Sahrens int 23101356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2311789Sahrens { 2312789Sahrens zfs_cmd_t zc = { 0 }; 2313789Sahrens zfs_handle_t *nzhp; 2314789Sahrens int ret; 2315789Sahrens 23165367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 23175367Sahrens return (0); 23185367Sahrens 2319789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23202082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2321789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2322789Sahrens /* 2323789Sahrens * Ignore private dataset names. 2324789Sahrens */ 2325789Sahrens if (dataset_name_hidden(zc.zc_name)) 2326789Sahrens continue; 2327789Sahrens 2328789Sahrens /* 2329789Sahrens * Silently ignore errors, as the only plausible explanation is 2330789Sahrens * that the pool has since been removed. 2331789Sahrens */ 23322082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23332082Seschrock zc.zc_name)) == NULL) 2334789Sahrens continue; 2335789Sahrens 2336789Sahrens if ((ret = func(nzhp, data)) != 0) 2337789Sahrens return (ret); 2338789Sahrens } 2339789Sahrens 2340789Sahrens /* 2341789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2342789Sahrens * returned, then the underlying dataset has been removed since we 2343789Sahrens * obtained the handle. 2344789Sahrens */ 2345789Sahrens if (errno != ESRCH && errno != ENOENT) 23462082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23472082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2348789Sahrens 23491356Seschrock return (0); 23501356Seschrock } 23511356Seschrock 23521356Seschrock /* 23531356Seschrock * Iterate over all snapshots 23541356Seschrock */ 23551356Seschrock int 23561356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23571356Seschrock { 23581356Seschrock zfs_cmd_t zc = { 0 }; 23591356Seschrock zfs_handle_t *nzhp; 23601356Seschrock int ret; 2361789Sahrens 23625367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 23635367Sahrens return (0); 23645367Sahrens 2365789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23662082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 23672082Seschrock &zc) == 0; 2368789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2369789Sahrens 23702082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23712082Seschrock zc.zc_name)) == NULL) 2372789Sahrens continue; 2373789Sahrens 2374789Sahrens if ((ret = func(nzhp, data)) != 0) 2375789Sahrens return (ret); 2376789Sahrens } 2377789Sahrens 2378789Sahrens /* 2379789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2380789Sahrens * returned, then the underlying dataset has been removed since we 2381789Sahrens * obtained the handle. Silently ignore this case, and return success. 2382789Sahrens */ 2383789Sahrens if (errno != ESRCH && errno != ENOENT) 23842082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23852082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2386789Sahrens 2387789Sahrens return (0); 2388789Sahrens } 2389789Sahrens 2390789Sahrens /* 23911356Seschrock * Iterate over all children, snapshots and filesystems 23921356Seschrock */ 23931356Seschrock int 23941356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23951356Seschrock { 23961356Seschrock int ret; 23971356Seschrock 23981356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 23991356Seschrock return (ret); 24001356Seschrock 24011356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 24021356Seschrock } 24031356Seschrock 24041356Seschrock /* 2405789Sahrens * Given a complete name, return just the portion that refers to the parent. 2406789Sahrens * Can return NULL if this is a pool. 2407789Sahrens */ 2408789Sahrens static int 2409789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2410789Sahrens { 2411789Sahrens char *loc; 2412789Sahrens 2413789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2414789Sahrens return (-1); 2415789Sahrens 2416789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2417789Sahrens buf[loc - path] = '\0'; 2418789Sahrens 2419789Sahrens return (0); 2420789Sahrens } 2421789Sahrens 2422789Sahrens /* 24234490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 24244490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 24254490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 24264490Svb160487 * length of already existing prefix of the given path. We also fetch the 24274490Svb160487 * 'zoned' property, which is used to validate property settings when creating 24284490Svb160487 * new datasets. 2429789Sahrens */ 2430789Sahrens static int 24314490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 24324490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2433789Sahrens { 2434789Sahrens zfs_cmd_t zc = { 0 }; 2435789Sahrens char parent[ZFS_MAXNAMELEN]; 2436789Sahrens char *slash; 24371356Seschrock zfs_handle_t *zhp; 24382082Seschrock char errbuf[1024]; 24392082Seschrock 24402082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 24412082Seschrock path); 2442789Sahrens 2443789Sahrens /* get parent, and check to see if this is just a pool */ 2444789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 24452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24462082Seschrock "missing dataset name")); 24472082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2448789Sahrens } 2449789Sahrens 2450789Sahrens /* check to see if the pool exists */ 2451789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2452789Sahrens slash = parent + strlen(parent); 2453789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2454789Sahrens zc.zc_name[slash - parent] = '\0'; 24552082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2456789Sahrens errno == ENOENT) { 24572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24582082Seschrock "no such pool '%s'"), zc.zc_name); 24592082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2460789Sahrens } 2461789Sahrens 2462789Sahrens /* check to see if the parent dataset exists */ 24634490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 24644490Svb160487 if (errno == ENOENT && accept_ancestor) { 24654490Svb160487 /* 24664490Svb160487 * Go deeper to find an ancestor, give up on top level. 24674490Svb160487 */ 24684490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 24694490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24704490Svb160487 "no such pool '%s'"), zc.zc_name); 24714490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24724490Svb160487 } 24734490Svb160487 } else if (errno == ENOENT) { 24742082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24752082Seschrock "parent does not exist")); 24762082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24774490Svb160487 } else 24782082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2479789Sahrens } 2480789Sahrens 24812676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2482789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 24832676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 24842082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 24851356Seschrock zfs_close(zhp); 2486789Sahrens return (-1); 2487789Sahrens } 2488789Sahrens 2489789Sahrens /* make sure parent is a filesystem */ 24901356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 24912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24922082Seschrock "parent is not a filesystem")); 24932082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 24941356Seschrock zfs_close(zhp); 2495789Sahrens return (-1); 2496789Sahrens } 2497789Sahrens 24981356Seschrock zfs_close(zhp); 24994490Svb160487 if (prefixlen != NULL) 25004490Svb160487 *prefixlen = strlen(parent); 25014490Svb160487 return (0); 25024490Svb160487 } 25034490Svb160487 25044490Svb160487 /* 25054490Svb160487 * Finds whether the dataset of the given type(s) exists. 25064490Svb160487 */ 25074490Svb160487 boolean_t 25084490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 25094490Svb160487 { 25104490Svb160487 zfs_handle_t *zhp; 25114490Svb160487 25125326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 25134490Svb160487 return (B_FALSE); 25144490Svb160487 25154490Svb160487 /* 25164490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 25174490Svb160487 */ 25184490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 25194490Svb160487 int ds_type = zhp->zfs_type; 25204490Svb160487 25214490Svb160487 zfs_close(zhp); 25224490Svb160487 if (types & ds_type) 25234490Svb160487 return (B_TRUE); 25244490Svb160487 } 25254490Svb160487 return (B_FALSE); 25264490Svb160487 } 25274490Svb160487 25284490Svb160487 /* 25295367Sahrens * Given a path to 'target', create all the ancestors between 25305367Sahrens * the prefixlen portion of the path, and the target itself. 25315367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 25325367Sahrens */ 25335367Sahrens int 25345367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 25355367Sahrens { 25365367Sahrens zfs_handle_t *h; 25375367Sahrens char *cp; 25385367Sahrens const char *opname; 25395367Sahrens 25405367Sahrens /* make sure prefix exists */ 25415367Sahrens cp = target + prefixlen; 25425367Sahrens if (*cp != '/') { 25435367Sahrens assert(strchr(cp, '/') == NULL); 25445367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25455367Sahrens } else { 25465367Sahrens *cp = '\0'; 25475367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25485367Sahrens *cp = '/'; 25495367Sahrens } 25505367Sahrens if (h == NULL) 25515367Sahrens return (-1); 25525367Sahrens zfs_close(h); 25535367Sahrens 25545367Sahrens /* 25555367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 25565367Sahrens * up to the prefixlen-long one. 25575367Sahrens */ 25585367Sahrens for (cp = target + prefixlen + 1; 25595367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 25605367Sahrens char *logstr; 25615367Sahrens 25625367Sahrens *cp = '\0'; 25635367Sahrens 25645367Sahrens h = make_dataset_handle(hdl, target); 25655367Sahrens if (h) { 25665367Sahrens /* it already exists, nothing to do here */ 25675367Sahrens zfs_close(h); 25685367Sahrens continue; 25695367Sahrens } 25705367Sahrens 25715367Sahrens logstr = hdl->libzfs_log_str; 25725367Sahrens hdl->libzfs_log_str = NULL; 25735367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 25745367Sahrens NULL) != 0) { 25755367Sahrens hdl->libzfs_log_str = logstr; 25765367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 25775367Sahrens goto ancestorerr; 25785367Sahrens } 25795367Sahrens 25805367Sahrens hdl->libzfs_log_str = logstr; 25815367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25825367Sahrens if (h == NULL) { 25835367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 25845367Sahrens goto ancestorerr; 25855367Sahrens } 25865367Sahrens 25875367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 25885367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 25895367Sahrens goto ancestorerr; 25905367Sahrens } 25915367Sahrens 25925367Sahrens if (zfs_share(h) != 0) { 25935367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 25945367Sahrens goto ancestorerr; 25955367Sahrens } 25965367Sahrens 25975367Sahrens zfs_close(h); 25985367Sahrens } 25995367Sahrens 26005367Sahrens return (0); 26015367Sahrens 26025367Sahrens ancestorerr: 26035367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26045367Sahrens "failed to %s ancestor '%s'"), opname, target); 26055367Sahrens return (-1); 26065367Sahrens } 26075367Sahrens 26085367Sahrens /* 26094490Svb160487 * Creates non-existing ancestors of the given path. 26104490Svb160487 */ 26114490Svb160487 int 26124490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 26134490Svb160487 { 26144490Svb160487 int prefix; 26154490Svb160487 uint64_t zoned; 26164490Svb160487 char *path_copy; 26174490Svb160487 int rc; 26184490Svb160487 26194490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 26204490Svb160487 return (-1); 26214490Svb160487 26224490Svb160487 if ((path_copy = strdup(path)) != NULL) { 26234490Svb160487 rc = create_parents(hdl, path_copy, prefix); 26244490Svb160487 free(path_copy); 26254490Svb160487 } 26264490Svb160487 if (path_copy == NULL || rc != 0) 26274490Svb160487 return (-1); 26284490Svb160487 2629789Sahrens return (0); 2630789Sahrens } 2631789Sahrens 2632789Sahrens /* 26332676Seschrock * Create a new filesystem or volume. 2634789Sahrens */ 2635789Sahrens int 26362082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 26372676Seschrock nvlist_t *props) 2638789Sahrens { 2639789Sahrens zfs_cmd_t zc = { 0 }; 2640789Sahrens int ret; 2641789Sahrens uint64_t size = 0; 2642789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 26432082Seschrock char errbuf[1024]; 26442676Seschrock uint64_t zoned; 26452082Seschrock 26462082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 26472082Seschrock "cannot create '%s'"), path); 2648789Sahrens 2649789Sahrens /* validate the path, taking care to note the extended error message */ 26505326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 26512082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2652789Sahrens 2653789Sahrens /* validate parents exist */ 26544490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2655789Sahrens return (-1); 2656789Sahrens 2657789Sahrens /* 2658789Sahrens * The failure modes when creating a dataset of a different type over 2659789Sahrens * one that already exists is a little strange. In particular, if you 2660789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2661789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2662789Sahrens * first try to see if the dataset exists. 2663789Sahrens */ 2664789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 26655094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 26662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26672082Seschrock "dataset already exists")); 26682082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2669789Sahrens } 2670789Sahrens 2671789Sahrens if (type == ZFS_TYPE_VOLUME) 2672789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2673789Sahrens else 2674789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2675789Sahrens 26765094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 26773912Slling zoned, NULL, errbuf)) == 0) 26782676Seschrock return (-1); 26792676Seschrock 2680789Sahrens if (type == ZFS_TYPE_VOLUME) { 26811133Seschrock /* 26821133Seschrock * If we are creating a volume, the size and block size must 26831133Seschrock * satisfy a few restraints. First, the blocksize must be a 26841133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 26851133Seschrock * volsize must be a multiple of the block size, and cannot be 26861133Seschrock * zero. 26871133Seschrock */ 26882676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 26892676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 26902676Seschrock nvlist_free(props); 26912082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26922676Seschrock "missing volume size")); 26932676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2694789Sahrens } 2695789Sahrens 26962676Seschrock if ((ret = nvlist_lookup_uint64(props, 26972676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 26982676Seschrock &blocksize)) != 0) { 26992676Seschrock if (ret == ENOENT) { 27002676Seschrock blocksize = zfs_prop_default_numeric( 27012676Seschrock ZFS_PROP_VOLBLOCKSIZE); 27022676Seschrock } else { 27032676Seschrock nvlist_free(props); 27042676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27052676Seschrock "missing volume block size")); 27062676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27072676Seschrock } 27082676Seschrock } 27092676Seschrock 27102676Seschrock if (size == 0) { 27112676Seschrock nvlist_free(props); 27122082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27132676Seschrock "volume size cannot be zero")); 27142676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27151133Seschrock } 27161133Seschrock 27171133Seschrock if (size % blocksize != 0) { 27182676Seschrock nvlist_free(props); 27192082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27202676Seschrock "volume size must be a multiple of volume block " 27212676Seschrock "size")); 27222676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27231133Seschrock } 2724789Sahrens } 2725789Sahrens 27265094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 27272676Seschrock return (-1); 27282676Seschrock nvlist_free(props); 27292676Seschrock 2730789Sahrens /* create the dataset */ 27314543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2732789Sahrens 27333912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 27342082Seschrock ret = zvol_create_link(hdl, path); 27353912Slling if (ret) { 27363912Slling (void) zfs_standard_error(hdl, errno, 27373912Slling dgettext(TEXT_DOMAIN, 27383912Slling "Volume successfully created, but device links " 27393912Slling "were not created")); 27403912Slling zcmd_free_nvlists(&zc); 27413912Slling return (-1); 27423912Slling } 27433912Slling } 2744789Sahrens 27452676Seschrock zcmd_free_nvlists(&zc); 27462676Seschrock 2747789Sahrens /* check for failure */ 2748789Sahrens if (ret != 0) { 2749789Sahrens char parent[ZFS_MAXNAMELEN]; 2750789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2751789Sahrens 2752789Sahrens switch (errno) { 2753789Sahrens case ENOENT: 27542082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27552082Seschrock "no such parent '%s'"), parent); 27562082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2757789Sahrens 2758789Sahrens case EINVAL: 27592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27603413Smmusante "parent '%s' is not a filesystem"), parent); 27612082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2762789Sahrens 2763789Sahrens case EDOM: 27642082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27652676Seschrock "volume block size must be power of 2 from " 27662676Seschrock "%u to %uk"), 2767789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2768789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 27692082Seschrock 27702676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27712082Seschrock 27724603Sahrens case ENOTSUP: 27734603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27744603Sahrens "pool must be upgraded to set this " 27754603Sahrens "property or value")); 27764603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 27774603Sahrens 2778789Sahrens #ifdef _ILP32 2779789Sahrens case EOVERFLOW: 2780789Sahrens /* 2781789Sahrens * This platform can't address a volume this big. 2782789Sahrens */ 27832082Seschrock if (type == ZFS_TYPE_VOLUME) 27842082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 27852082Seschrock errbuf)); 2786789Sahrens #endif 27872082Seschrock /* FALLTHROUGH */ 2788789Sahrens default: 27892082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2790789Sahrens } 2791789Sahrens } 2792789Sahrens 2793789Sahrens return (0); 2794789Sahrens } 2795789Sahrens 2796789Sahrens /* 2797789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2798789Sahrens * isn't mounted, and that there are no active dependents. 2799789Sahrens */ 2800789Sahrens int 2801789Sahrens zfs_destroy(zfs_handle_t *zhp) 2802789Sahrens { 2803789Sahrens zfs_cmd_t zc = { 0 }; 2804789Sahrens 2805789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2806789Sahrens 28072676Seschrock if (ZFS_IS_VOLUME(zhp)) { 28083126Sahl /* 28094543Smarks * If user doesn't have permissions to unshare volume, then 28104543Smarks * abort the request. This would only happen for a 28114543Smarks * non-privileged user. 28123126Sahl */ 28134543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 28144543Smarks return (-1); 28154543Smarks } 28163126Sahl 28172082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2818789Sahrens return (-1); 2819789Sahrens 2820789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2821789Sahrens } else { 2822789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2823789Sahrens } 2824789Sahrens 28254543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 28263237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 28272082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 28282082Seschrock zhp->zfs_name)); 28292199Sahrens } 2830789Sahrens 2831789Sahrens remove_mountpoint(zhp); 2832789Sahrens 2833789Sahrens return (0); 2834789Sahrens } 2835789Sahrens 28362199Sahrens struct destroydata { 28372199Sahrens char *snapname; 28382199Sahrens boolean_t gotone; 28393265Sahrens boolean_t closezhp; 28402199Sahrens }; 28412199Sahrens 28422199Sahrens static int 28432199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 28442199Sahrens { 28452199Sahrens struct destroydata *dd = arg; 28462199Sahrens zfs_handle_t *szhp; 28472199Sahrens char name[ZFS_MAXNAMELEN]; 28483265Sahrens boolean_t closezhp = dd->closezhp; 28493265Sahrens int rv; 28502199Sahrens 28512676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 28522676Seschrock (void) strlcat(name, "@", sizeof (name)); 28532676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 28542199Sahrens 28552199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 28562199Sahrens if (szhp) { 28572199Sahrens dd->gotone = B_TRUE; 28582199Sahrens zfs_close(szhp); 28592199Sahrens } 28602199Sahrens 28612199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 28622199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 28632199Sahrens /* 28642199Sahrens * NB: this is simply a best-effort. We don't want to 28652199Sahrens * return an error, because then we wouldn't visit all 28662199Sahrens * the volumes. 28672199Sahrens */ 28682199Sahrens } 28692199Sahrens 28703265Sahrens dd->closezhp = B_TRUE; 28713265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 28723265Sahrens if (closezhp) 28733265Sahrens zfs_close(zhp); 28743265Sahrens return (rv); 28752199Sahrens } 28762199Sahrens 28772199Sahrens /* 28782199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 28792199Sahrens */ 28802199Sahrens int 28812199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 28822199Sahrens { 28832199Sahrens zfs_cmd_t zc = { 0 }; 28842199Sahrens int ret; 28852199Sahrens struct destroydata dd = { 0 }; 28862199Sahrens 28872199Sahrens dd.snapname = snapname; 28882199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 28892199Sahrens 28902199Sahrens if (!dd.gotone) { 28913237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 28922199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 28932199Sahrens zhp->zfs_name, snapname)); 28942199Sahrens } 28952199Sahrens 28962199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 28972676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 28982199Sahrens 28994543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 29002199Sahrens if (ret != 0) { 29012199Sahrens char errbuf[1024]; 29022199Sahrens 29032199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29042199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 29052199Sahrens 29062199Sahrens switch (errno) { 29072199Sahrens case EEXIST: 29082199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29092199Sahrens "snapshot is cloned")); 29102199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 29112199Sahrens 29122199Sahrens default: 29132199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 29142199Sahrens errbuf)); 29152199Sahrens } 29162199Sahrens } 29172199Sahrens 29182199Sahrens return (0); 29192199Sahrens } 29202199Sahrens 2921789Sahrens /* 2922789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2923789Sahrens */ 2924789Sahrens int 29252676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2926789Sahrens { 2927789Sahrens zfs_cmd_t zc = { 0 }; 2928789Sahrens char parent[ZFS_MAXNAMELEN]; 2929789Sahrens int ret; 29302082Seschrock char errbuf[1024]; 29312082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 29322676Seschrock zfs_type_t type; 29332676Seschrock uint64_t zoned; 2934789Sahrens 2935789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2936789Sahrens 29372082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29382082Seschrock "cannot create '%s'"), target); 29392082Seschrock 2940789Sahrens /* validate the target name */ 29415326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 29422082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2943789Sahrens 2944789Sahrens /* validate parents exist */ 29454490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2946789Sahrens return (-1); 2947789Sahrens 2948789Sahrens (void) parent_name(target, parent, sizeof (parent)); 2949789Sahrens 2950789Sahrens /* do the clone */ 29512676Seschrock if (ZFS_IS_VOLUME(zhp)) { 2952789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 29532744Snn35248 type = ZFS_TYPE_VOLUME; 29542676Seschrock } else { 2955789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 29562744Snn35248 type = ZFS_TYPE_FILESYSTEM; 29572676Seschrock } 29582676Seschrock 29592676Seschrock if (props) { 29605094Slling if ((props = zfs_validate_properties(hdl, type, props, 29613912Slling zoned, zhp, errbuf)) == NULL) 29622676Seschrock return (-1); 29632676Seschrock 29645094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 29652676Seschrock nvlist_free(props); 29662676Seschrock return (-1); 29672676Seschrock } 29682676Seschrock 29692676Seschrock nvlist_free(props); 29702676Seschrock } 2971789Sahrens 2972789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 29732676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 29744543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2975789Sahrens 29762676Seschrock zcmd_free_nvlists(&zc); 29772676Seschrock 2978789Sahrens if (ret != 0) { 2979789Sahrens switch (errno) { 2980789Sahrens 2981789Sahrens case ENOENT: 2982789Sahrens /* 2983789Sahrens * The parent doesn't exist. We should have caught this 2984789Sahrens * above, but there may a race condition that has since 2985789Sahrens * destroyed the parent. 2986789Sahrens * 2987789Sahrens * At this point, we don't know whether it's the source 2988789Sahrens * that doesn't exist anymore, or whether the target 2989789Sahrens * dataset doesn't exist. 2990789Sahrens */ 29912082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29922082Seschrock "no such parent '%s'"), parent); 29932082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 29942082Seschrock 29952082Seschrock case EXDEV: 29962082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29972082Seschrock "source and target pools differ")); 29982082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 29992082Seschrock errbuf)); 30002082Seschrock 30012082Seschrock default: 30022082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 30032082Seschrock errbuf)); 30042082Seschrock } 30052676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 30062082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 30072082Seschrock } 30082082Seschrock 30092082Seschrock return (ret); 30102082Seschrock } 30112082Seschrock 30122082Seschrock typedef struct promote_data { 30132082Seschrock char cb_mountpoint[MAXPATHLEN]; 30142082Seschrock const char *cb_target; 30152082Seschrock const char *cb_errbuf; 30162082Seschrock uint64_t cb_pivot_txg; 30172082Seschrock } promote_data_t; 30182082Seschrock 30192082Seschrock static int 30202082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 30212082Seschrock { 30222082Seschrock promote_data_t *pd = data; 30232082Seschrock zfs_handle_t *szhp; 30242082Seschrock char snapname[MAXPATHLEN]; 30253265Sahrens int rv = 0; 30262082Seschrock 30272082Seschrock /* We don't care about snapshots after the pivot point */ 30283265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 30293265Sahrens zfs_close(zhp); 30302082Seschrock return (0); 30313265Sahrens } 30322082Seschrock 30332417Sahrens /* Remove the device link if it's a zvol. */ 30342676Seschrock if (ZFS_IS_VOLUME(zhp)) 30352417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 30362082Seschrock 30372082Seschrock /* Check for conflicting names */ 30382676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 30392676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 30402082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 30412082Seschrock if (szhp != NULL) { 30422082Seschrock zfs_close(szhp); 30432082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30442082Seschrock "snapshot name '%s' from origin \n" 30452082Seschrock "conflicts with '%s' from target"), 30462082Seschrock zhp->zfs_name, snapname); 30473265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 30482082Seschrock } 30493265Sahrens zfs_close(zhp); 30503265Sahrens return (rv); 30512082Seschrock } 30522082Seschrock 30532417Sahrens static int 30542417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 30552417Sahrens { 30562417Sahrens promote_data_t *pd = data; 30572417Sahrens 30582417Sahrens /* We don't care about snapshots after the pivot point */ 30593265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 30603265Sahrens /* Create the device link if it's a zvol. */ 30613265Sahrens if (ZFS_IS_VOLUME(zhp)) 30623265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 30633265Sahrens } 30643265Sahrens 30653265Sahrens zfs_close(zhp); 30662417Sahrens return (0); 30672417Sahrens } 30682417Sahrens 30692082Seschrock /* 30702082Seschrock * Promotes the given clone fs to be the clone parent. 30712082Seschrock */ 30722082Seschrock int 30732082Seschrock zfs_promote(zfs_handle_t *zhp) 30742082Seschrock { 30752082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 30762082Seschrock zfs_cmd_t zc = { 0 }; 30772082Seschrock char parent[MAXPATHLEN]; 30782082Seschrock char *cp; 30792082Seschrock int ret; 30802082Seschrock zfs_handle_t *pzhp; 30812082Seschrock promote_data_t pd; 30822082Seschrock char errbuf[1024]; 30832082Seschrock 30842082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30852082Seschrock "cannot promote '%s'"), zhp->zfs_name); 30862082Seschrock 30872082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 30882082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30892082Seschrock "snapshots can not be promoted")); 30902082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 30912082Seschrock } 30922082Seschrock 30935367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 30942082Seschrock if (parent[0] == '\0') { 30952082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30962082Seschrock "not a cloned filesystem")); 30972082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 30982082Seschrock } 30992082Seschrock cp = strchr(parent, '@'); 31002082Seschrock *cp = '\0'; 31012082Seschrock 31022082Seschrock /* Walk the snapshots we will be moving */ 31035367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 31042082Seschrock if (pzhp == NULL) 31052082Seschrock return (-1); 31062082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 31072082Seschrock zfs_close(pzhp); 31082082Seschrock pd.cb_target = zhp->zfs_name; 31092082Seschrock pd.cb_errbuf = errbuf; 31105094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 31112082Seschrock if (pzhp == NULL) 31122082Seschrock return (-1); 31132082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 31142082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 31152082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 31162417Sahrens if (ret != 0) { 31172417Sahrens zfs_close(pzhp); 31182082Seschrock return (-1); 31192417Sahrens } 31202082Seschrock 31212082Seschrock /* issue the ioctl */ 31225367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 31232676Seschrock sizeof (zc.zc_value)); 31242082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31254543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 31262082Seschrock 31272082Seschrock if (ret != 0) { 31282417Sahrens int save_errno = errno; 31292417Sahrens 31302417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 31312417Sahrens zfs_close(pzhp); 31322417Sahrens 31332417Sahrens switch (save_errno) { 3134789Sahrens case EEXIST: 3135789Sahrens /* 31362082Seschrock * There is a conflicting snapshot name. We 31372082Seschrock * should have caught this above, but they could 31382082Seschrock * have renamed something in the mean time. 3139789Sahrens */ 31402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31412082Seschrock "conflicting snapshot name from parent '%s'"), 31422082Seschrock parent); 31432082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3144789Sahrens 3145789Sahrens default: 31462417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3147789Sahrens } 31482417Sahrens } else { 31492417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3150789Sahrens } 3151789Sahrens 31522417Sahrens zfs_close(pzhp); 3153789Sahrens return (ret); 3154789Sahrens } 3155789Sahrens 31564007Smmusante struct createdata { 31574007Smmusante const char *cd_snapname; 31584007Smmusante int cd_ifexists; 31594007Smmusante }; 31604007Smmusante 31612199Sahrens static int 31622199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 31632199Sahrens { 31644007Smmusante struct createdata *cd = arg; 31652676Seschrock int ret; 31662199Sahrens 31672199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 31682199Sahrens char name[MAXPATHLEN]; 31692199Sahrens 31702676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 31712676Seschrock (void) strlcat(name, "@", sizeof (name)); 31724007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 31734007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 31744007Smmusante cd->cd_ifexists); 31752199Sahrens /* 31762199Sahrens * NB: this is simply a best-effort. We don't want to 31772199Sahrens * return an error, because then we wouldn't visit all 31782199Sahrens * the volumes. 31792199Sahrens */ 31802199Sahrens } 31812676Seschrock 31824007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 31832676Seschrock 31842676Seschrock zfs_close(zhp); 31852676Seschrock 31862676Seschrock return (ret); 31872199Sahrens } 31882199Sahrens 3189789Sahrens /* 31903504Sahl * Takes a snapshot of the given dataset. 3191789Sahrens */ 3192789Sahrens int 31932199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3194789Sahrens { 3195789Sahrens const char *delim; 3196789Sahrens char *parent; 3197789Sahrens zfs_handle_t *zhp; 3198789Sahrens zfs_cmd_t zc = { 0 }; 3199789Sahrens int ret; 32002082Seschrock char errbuf[1024]; 32012082Seschrock 32022082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32032082Seschrock "cannot snapshot '%s'"), path); 32042082Seschrock 32052082Seschrock /* validate the target name */ 32065326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 32072082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3208789Sahrens 3209789Sahrens /* make sure the parent exists and is of the appropriate type */ 32102199Sahrens delim = strchr(path, '@'); 32112082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 32122082Seschrock return (-1); 3213789Sahrens (void) strncpy(parent, path, delim - path); 3214789Sahrens parent[delim - path] = '\0'; 3215789Sahrens 32162082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3217789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3218789Sahrens free(parent); 3219789Sahrens return (-1); 3220789Sahrens } 3221789Sahrens 32222199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 32232676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 32244543Smarks if (ZFS_IS_VOLUME(zhp)) 32254543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 32264543Smarks else 32274543Smarks zc.zc_objset_type = DMU_OST_ZFS; 32282199Sahrens zc.zc_cookie = recursive; 32294543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 32302199Sahrens 32312199Sahrens /* 32322199Sahrens * if it was recursive, the one that actually failed will be in 32332199Sahrens * zc.zc_name. 32342199Sahrens */ 32354543Smarks if (ret != 0) 32364543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32374543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 32384543Smarks 32392199Sahrens if (ret == 0 && recursive) { 32404007Smmusante struct createdata cd; 32414007Smmusante 32424007Smmusante cd.cd_snapname = delim + 1; 32434007Smmusante cd.cd_ifexists = B_FALSE; 32444007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 32452199Sahrens } 3246789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 32472082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 32482199Sahrens if (ret != 0) { 32494543Smarks (void) zfs_standard_error(hdl, errno, 32504543Smarks dgettext(TEXT_DOMAIN, 32514543Smarks "Volume successfully snapshotted, but device links " 32524543Smarks "were not created")); 32534543Smarks free(parent); 32544543Smarks zfs_close(zhp); 32554543Smarks return (-1); 32562199Sahrens } 3257789Sahrens } 3258789Sahrens 32592082Seschrock if (ret != 0) 32602082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3261789Sahrens 3262789Sahrens free(parent); 3263789Sahrens zfs_close(zhp); 3264789Sahrens 3265789Sahrens return (ret); 3266789Sahrens } 3267789Sahrens 3268789Sahrens /* 32691294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 32701294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 32711294Slling * is a dependent and we should just destroy it without checking the transaction 32721294Slling * group. 3273789Sahrens */ 32741294Slling typedef struct rollback_data { 32751294Slling const char *cb_target; /* the snapshot */ 32761294Slling uint64_t cb_create; /* creation time reference */ 32771294Slling int cb_error; 32782082Seschrock boolean_t cb_dependent; 32791294Slling } rollback_data_t; 32801294Slling 32811294Slling static int 32821294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 32831294Slling { 32841294Slling rollback_data_t *cbp = data; 32851294Slling 32861294Slling if (!cbp->cb_dependent) { 32871294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 32881294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 32891294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 32901294Slling cbp->cb_create) { 32914543Smarks char *logstr; 32921294Slling 32932082Seschrock cbp->cb_dependent = B_TRUE; 3294*5446Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 3295*5446Sahrens rollback_destroy, cbp); 32962082Seschrock cbp->cb_dependent = B_FALSE; 32971294Slling 32984543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 32994543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 3300*5446Sahrens cbp->cb_error |= zfs_destroy(zhp); 33014543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 33021294Slling } 33031294Slling } else { 3304*5446Sahrens cbp->cb_error |= zfs_destroy(zhp); 33051294Slling } 33061294Slling 33071294Slling zfs_close(zhp); 33081294Slling return (0); 33091294Slling } 33101294Slling 33111294Slling /* 3312*5446Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 3313*5446Sahrens * data changes since then and making it the active dataset. 3314*5446Sahrens * 3315*5446Sahrens * Any snapshots more recent than the target are destroyed, along with 3316*5446Sahrens * their dependents. 33171294Slling */ 3318*5446Sahrens int 3319*5446Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap) 3320789Sahrens { 3321*5446Sahrens rollback_data_t cb = { 0 }; 3322*5446Sahrens int err; 3323789Sahrens zfs_cmd_t zc = { 0 }; 3324789Sahrens 3325789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3326789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3327789Sahrens 3328*5446Sahrens /* 3329*5446Sahrens * Destroy all recent snapshots and its dependends. 3330*5446Sahrens */ 3331*5446Sahrens cb.cb_target = snap->zfs_name; 3332*5446Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 3333*5446Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 3334*5446Sahrens 3335*5446Sahrens if (cb.cb_error != 0) 3336*5446Sahrens return (cb.cb_error); 3337*5446Sahrens 3338*5446Sahrens /* 3339*5446Sahrens * Now that we have verified that the snapshot is the latest, 3340*5446Sahrens * rollback to the given snapshot. 3341*5446Sahrens */ 3342*5446Sahrens 3343789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 33442082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3345789Sahrens return (-1); 3346789Sahrens 3347789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3348789Sahrens 33492676Seschrock if (ZFS_IS_VOLUME(zhp)) 3350789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3351789Sahrens else 3352789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3353789Sahrens 3354789Sahrens /* 3355*5446Sahrens * We rely on zfs_iter_children() to verify that there are no 3356*5446Sahrens * newer snapshots for the given dataset. Therefore, we can 3357*5446Sahrens * simply pass the name on to the ioctl() call. There is still 3358*5446Sahrens * an unlikely race condition where the user has taken a 3359*5446Sahrens * snapshot since we verified that this was the most recent. 3360789Sahrens */ 3361*5446Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 33623237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 33632082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 33642082Seschrock zhp->zfs_name); 3365789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 3366*5446Sahrens err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3367789Sahrens } 3368789Sahrens 3369*5446Sahrens return (err); 33701294Slling } 33711294Slling 33721294Slling /* 3373789Sahrens * Iterate over all dependents for a given dataset. This includes both 3374789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3375789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3376789Sahrens * libzfs_graph.c. 3377789Sahrens */ 3378789Sahrens int 33792474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 33802474Seschrock zfs_iter_f func, void *data) 3381789Sahrens { 3382789Sahrens char **dependents; 3383789Sahrens size_t count; 3384789Sahrens int i; 3385789Sahrens zfs_handle_t *child; 3386789Sahrens int ret = 0; 3387789Sahrens 33882474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 33892474Seschrock &dependents, &count) != 0) 33902474Seschrock return (-1); 33912474Seschrock 3392789Sahrens for (i = 0; i < count; i++) { 33932082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 33942082Seschrock dependents[i])) == NULL) 3395789Sahrens continue; 3396789Sahrens 3397789Sahrens if ((ret = func(child, data)) != 0) 3398789Sahrens break; 3399789Sahrens } 3400789Sahrens 3401789Sahrens for (i = 0; i < count; i++) 3402789Sahrens free(dependents[i]); 3403789Sahrens free(dependents); 3404789Sahrens 3405789Sahrens return (ret); 3406789Sahrens } 3407789Sahrens 3408789Sahrens /* 3409789Sahrens * Renames the given dataset. 3410789Sahrens */ 3411789Sahrens int 34124490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3413789Sahrens { 3414789Sahrens int ret; 3415789Sahrens zfs_cmd_t zc = { 0 }; 3416789Sahrens char *delim; 34174007Smmusante prop_changelist_t *cl = NULL; 34184007Smmusante zfs_handle_t *zhrp = NULL; 34194007Smmusante char *parentname = NULL; 3420789Sahrens char parent[ZFS_MAXNAMELEN]; 34212082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 34222082Seschrock char errbuf[1024]; 3423789Sahrens 3424789Sahrens /* if we have the same exact name, just return success */ 3425789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3426789Sahrens return (0); 3427789Sahrens 34282082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34292082Seschrock "cannot rename to '%s'"), target); 34302082Seschrock 3431789Sahrens /* 3432789Sahrens * Make sure the target name is valid 3433789Sahrens */ 3434789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 34352665Snd150628 if ((strchr(target, '@') == NULL) || 34362665Snd150628 *target == '@') { 34372665Snd150628 /* 34382665Snd150628 * Snapshot target name is abbreviated, 34392665Snd150628 * reconstruct full dataset name 34402665Snd150628 */ 34412665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 34422665Snd150628 sizeof (parent)); 34432665Snd150628 delim = strchr(parent, '@'); 34442665Snd150628 if (strchr(target, '@') == NULL) 34452665Snd150628 *(++delim) = '\0'; 34462665Snd150628 else 34472665Snd150628 *delim = '\0'; 34482665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 34492665Snd150628 target = parent; 34502665Snd150628 } else { 34512665Snd150628 /* 34522665Snd150628 * Make sure we're renaming within the same dataset. 34532665Snd150628 */ 34542665Snd150628 delim = strchr(target, '@'); 34552665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 34562665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 34572665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34582665Snd150628 "snapshots must be part of same " 34592665Snd150628 "dataset")); 34602665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 34613912Slling errbuf)); 34622665Snd150628 } 3463789Sahrens } 34645326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 34652665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3466789Sahrens } else { 34674007Smmusante if (recursive) { 34684007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34694007Smmusante "recursive rename must be a snapshot")); 34704007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 34714007Smmusante } 34724007Smmusante 34735326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 34742665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 34752676Seschrock uint64_t unused; 34762676Seschrock 3477789Sahrens /* validate parents */ 34784490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3479789Sahrens return (-1); 3480789Sahrens 3481789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3482789Sahrens 3483789Sahrens /* make sure we're in the same pool */ 3484789Sahrens verify((delim = strchr(target, '/')) != NULL); 3485789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3486789Sahrens zhp->zfs_name[delim - target] != '/') { 34872082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34882082Seschrock "datasets must be within same pool")); 34892082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3490789Sahrens } 34912440Snd150628 34922440Snd150628 /* new name cannot be a child of the current dataset name */ 34932440Snd150628 if (strncmp(parent, zhp->zfs_name, 34943912Slling strlen(zhp->zfs_name)) == 0) { 34952440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34962440Snd150628 "New dataset name cannot be a descendent of " 34972440Snd150628 "current dataset name")); 34982440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 34992440Snd150628 } 3500789Sahrens } 3501789Sahrens 35022082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 35032082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 35042082Seschrock 3505789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3506789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 35072082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35082082Seschrock "dataset is used in a non-global zone")); 35092082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3510789Sahrens } 3511789Sahrens 35124007Smmusante if (recursive) { 35134007Smmusante struct destroydata dd; 35144007Smmusante 35154183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 35164183Smmusante if (parentname == NULL) { 35174183Smmusante ret = -1; 35184183Smmusante goto error; 35194183Smmusante } 35204007Smmusante delim = strchr(parentname, '@'); 35214007Smmusante *delim = '\0'; 35225094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 35234007Smmusante if (zhrp == NULL) { 35244183Smmusante ret = -1; 35254183Smmusante goto error; 35264007Smmusante } 35274007Smmusante 35284007Smmusante dd.snapname = delim + 1; 35294007Smmusante dd.gotone = B_FALSE; 35304183Smmusante dd.closezhp = B_TRUE; 35314007Smmusante 35324007Smmusante /* We remove any zvol links prior to renaming them */ 35334007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 35344007Smmusante if (ret) { 35354007Smmusante goto error; 35364007Smmusante } 35374007Smmusante } else { 35384007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 35394007Smmusante return (-1); 35404007Smmusante 35414007Smmusante if (changelist_haszonedchild(cl)) { 35424007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35434007Smmusante "child dataset with inherited mountpoint is used " 35444007Smmusante "in a non-global zone")); 35454007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 35464007Smmusante goto error; 35474007Smmusante } 35484007Smmusante 35494007Smmusante if ((ret = changelist_prefix(cl)) != 0) 35504007Smmusante goto error; 3551789Sahrens } 3552789Sahrens 35532676Seschrock if (ZFS_IS_VOLUME(zhp)) 3554789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3555789Sahrens else 3556789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3557789Sahrens 35582665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35592676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 35602665Snd150628 35614007Smmusante zc.zc_cookie = recursive; 35624007Smmusante 35634543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 35644007Smmusante /* 35654007Smmusante * if it was recursive, the one that actually failed will 35664007Smmusante * be in zc.zc_name 35674007Smmusante */ 35684007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35695367Sahrens "cannot rename '%s'"), zc.zc_name); 35704007Smmusante 35714007Smmusante if (recursive && errno == EEXIST) { 35724007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35734007Smmusante "a child dataset already has a snapshot " 35744007Smmusante "with the new name")); 35754801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 35764007Smmusante } else { 35774007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 35784007Smmusante } 3579789Sahrens 3580789Sahrens /* 3581789Sahrens * On failure, we still want to remount any filesystems that 3582789Sahrens * were previously mounted, so we don't alter the system state. 3583789Sahrens */ 35844007Smmusante if (recursive) { 35854007Smmusante struct createdata cd; 35864007Smmusante 35874007Smmusante /* only create links for datasets that had existed */ 35884007Smmusante cd.cd_snapname = delim + 1; 35894007Smmusante cd.cd_ifexists = B_TRUE; 35904007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 35914007Smmusante &cd); 35924007Smmusante } else { 35934007Smmusante (void) changelist_postfix(cl); 35944007Smmusante } 3595789Sahrens } else { 35964007Smmusante if (recursive) { 35974007Smmusante struct createdata cd; 35984007Smmusante 35994007Smmusante /* only create links for datasets that had existed */ 36004007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 36014007Smmusante cd.cd_ifexists = B_TRUE; 36024007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36034007Smmusante &cd); 36044007Smmusante } else { 36054007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 36064007Smmusante ret = changelist_postfix(cl); 36074007Smmusante } 3608789Sahrens } 3609789Sahrens 3610789Sahrens error: 36114007Smmusante if (parentname) { 36124007Smmusante free(parentname); 36134007Smmusante } 36144007Smmusante if (zhrp) { 36154007Smmusante zfs_close(zhrp); 36164007Smmusante } 36174007Smmusante if (cl) { 36184007Smmusante changelist_free(cl); 36194007Smmusante } 3620789Sahrens return (ret); 3621789Sahrens } 3622789Sahrens 3623789Sahrens /* 3624789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3625789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3626789Sahrens */ 3627789Sahrens int 36282082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3629789Sahrens { 36304007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 36314007Smmusante } 36324007Smmusante 36334007Smmusante static int 36344007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 36354007Smmusante { 3636789Sahrens zfs_cmd_t zc = { 0 }; 36372082Seschrock di_devlink_handle_t dhdl; 36384543Smarks priv_set_t *priv_effective; 36394543Smarks int privileged; 3640789Sahrens 3641789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3642789Sahrens 3643789Sahrens /* 3644789Sahrens * Issue the appropriate ioctl. 3645789Sahrens */ 36462082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3647789Sahrens switch (errno) { 3648789Sahrens case EEXIST: 3649789Sahrens /* 3650789Sahrens * Silently ignore the case where the link already 3651789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3652789Sahrens * times without errors. 3653789Sahrens */ 3654789Sahrens return (0); 3655789Sahrens 36564007Smmusante case ENOENT: 36574007Smmusante /* 36584007Smmusante * Dataset does not exist in the kernel. If we 36594007Smmusante * don't care (see zfs_rename), then ignore the 36604007Smmusante * error quietly. 36614007Smmusante */ 36624007Smmusante if (ifexists) { 36634007Smmusante return (0); 36644007Smmusante } 36654007Smmusante 36664007Smmusante /* FALLTHROUGH */ 36674007Smmusante 3668789Sahrens default: 36693237Slling return (zfs_standard_error_fmt(hdl, errno, 36702082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 36712082Seschrock "for '%s'"), dataset)); 3672789Sahrens } 3673789Sahrens } 3674789Sahrens 3675789Sahrens /* 36764543Smarks * If privileged call devfsadm and wait for the links to 36774543Smarks * magically appear. 36784543Smarks * Otherwise, print out an informational message. 3679789Sahrens */ 36804543Smarks 36814543Smarks priv_effective = priv_allocset(); 36824543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 36834543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 36844543Smarks priv_freeset(priv_effective); 36854543Smarks 36864543Smarks if (privileged) { 36874543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 36884543Smarks DI_MAKE_LINK)) == NULL) { 36894543Smarks zfs_error_aux(hdl, strerror(errno)); 36904543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 36914543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 36924543Smarks "for '%s'"), dataset); 36934543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 36944543Smarks return (-1); 36954543Smarks } else { 36964543Smarks (void) di_devlink_fini(&dhdl); 36974543Smarks } 3698789Sahrens } else { 36994543Smarks char pathname[MAXPATHLEN]; 37004543Smarks struct stat64 statbuf; 37014543Smarks int i; 37024543Smarks 37034543Smarks #define MAX_WAIT 10 37044543Smarks 37054543Smarks /* 37064543Smarks * This is the poor mans way of waiting for the link 37074543Smarks * to show up. If after 10 seconds we still don't 37084543Smarks * have it, then print out a message. 37094543Smarks */ 37104543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 37114543Smarks dataset); 37124543Smarks 37134543Smarks for (i = 0; i != MAX_WAIT; i++) { 37144543Smarks if (stat64(pathname, &statbuf) == 0) 37154543Smarks break; 37164543Smarks (void) sleep(1); 37174543Smarks } 37184543Smarks if (i == MAX_WAIT) 37194543Smarks (void) printf(gettext("%s may not be immediately " 37204543Smarks "available\n"), pathname); 3721789Sahrens } 3722789Sahrens 3723789Sahrens return (0); 3724789Sahrens } 3725789Sahrens 3726789Sahrens /* 3727789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 3728789Sahrens */ 3729789Sahrens int 37302082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3731789Sahrens { 3732789Sahrens zfs_cmd_t zc = { 0 }; 3733789Sahrens 3734789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3735789Sahrens 37362082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3737789Sahrens switch (errno) { 3738789Sahrens case ENXIO: 3739789Sahrens /* 3740789Sahrens * Silently ignore the case where the link no longer 3741789Sahrens * exists, so that 'zfs volfini' can be run multiple 3742789Sahrens * times without errors. 3743789Sahrens */ 3744789Sahrens return (0); 3745789Sahrens 3746789Sahrens default: 37473237Slling return (zfs_standard_error_fmt(hdl, errno, 37482082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 37492082Seschrock "links for '%s'"), dataset)); 3750789Sahrens } 3751789Sahrens } 3752789Sahrens 3753789Sahrens return (0); 3754789Sahrens } 37552676Seschrock 37562676Seschrock nvlist_t * 37572676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 37582676Seschrock { 37592676Seschrock return (zhp->zfs_user_props); 37602676Seschrock } 37612676Seschrock 37622676Seschrock /* 37633912Slling * This function is used by 'zfs list' to determine the exact set of columns to 37643912Slling * display, and their maximum widths. This does two main things: 37653912Slling * 37663912Slling * - If this is a list of all properties, then expand the list to include 37673912Slling * all native properties, and set a flag so that for each dataset we look 37683912Slling * for new unique user properties and add them to the list. 37693912Slling * 37703912Slling * - For non fixed-width properties, keep track of the maximum width seen 37713912Slling * so that we can size the column appropriately. 37723912Slling */ 37733912Slling int 37745094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 37753912Slling { 37763912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 37775094Slling zprop_list_t *entry; 37785094Slling zprop_list_t **last, **start; 37793912Slling nvlist_t *userprops, *propval; 37803912Slling nvpair_t *elem; 37813912Slling char *strval; 37823912Slling char buf[ZFS_MAXPROPLEN]; 37833912Slling 37845094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 37853912Slling return (-1); 37862676Seschrock 37872676Seschrock userprops = zfs_get_user_props(zhp); 37882676Seschrock 37892676Seschrock entry = *plp; 37902676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 37912676Seschrock /* 37922676Seschrock * Go through and add any user properties as necessary. We 37932676Seschrock * start by incrementing our list pointer to the first 37942676Seschrock * non-native property. 37952676Seschrock */ 37962676Seschrock start = plp; 37972676Seschrock while (*start != NULL) { 37985094Slling if ((*start)->pl_prop == ZPROP_INVAL) 37992676Seschrock break; 38002676Seschrock start = &(*start)->pl_next; 38012676Seschrock } 38022676Seschrock 38032676Seschrock elem = NULL; 38042676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 38052676Seschrock /* 38062676Seschrock * See if we've already found this property in our list. 38072676Seschrock */ 38082676Seschrock for (last = start; *last != NULL; 38092676Seschrock last = &(*last)->pl_next) { 38102676Seschrock if (strcmp((*last)->pl_user_prop, 38112676Seschrock nvpair_name(elem)) == 0) 38122676Seschrock break; 38132676Seschrock } 38142676Seschrock 38152676Seschrock if (*last == NULL) { 38162676Seschrock if ((entry = zfs_alloc(hdl, 38175094Slling sizeof (zprop_list_t))) == NULL || 38182676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 38192676Seschrock nvpair_name(elem)))) == NULL) { 38202676Seschrock free(entry); 38212676Seschrock return (-1); 38222676Seschrock } 38232676Seschrock 38245094Slling entry->pl_prop = ZPROP_INVAL; 38252676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 38262676Seschrock entry->pl_all = B_TRUE; 38272676Seschrock *last = entry; 38282676Seschrock } 38292676Seschrock } 38302676Seschrock } 38312676Seschrock 38322676Seschrock /* 38332676Seschrock * Now go through and check the width of any non-fixed columns 38342676Seschrock */ 38352676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 38362676Seschrock if (entry->pl_fixed) 38372676Seschrock continue; 38382676Seschrock 38395094Slling if (entry->pl_prop != ZPROP_INVAL) { 38402676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 38412676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 38422676Seschrock if (strlen(buf) > entry->pl_width) 38432676Seschrock entry->pl_width = strlen(buf); 38442676Seschrock } 38452676Seschrock } else if (nvlist_lookup_nvlist(userprops, 38462676Seschrock entry->pl_user_prop, &propval) == 0) { 38472676Seschrock verify(nvlist_lookup_string(propval, 38485094Slling ZPROP_VALUE, &strval) == 0); 38492676Seschrock if (strlen(strval) > entry->pl_width) 38502676Seschrock entry->pl_width = strlen(strval); 38512676Seschrock } 38522676Seschrock } 38532676Seschrock 38542676Seschrock return (0); 38552676Seschrock } 38564543Smarks 38574543Smarks int 38584543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 38594543Smarks { 38604543Smarks zfs_cmd_t zc = { 0 }; 38614543Smarks nvlist_t *nvp; 38624543Smarks gid_t gid; 38634543Smarks uid_t uid; 38644543Smarks const gid_t *groups; 38654543Smarks int group_cnt; 38664543Smarks int error; 38674543Smarks 38684543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 38694543Smarks return (no_memory(hdl)); 38704543Smarks 38714543Smarks uid = ucred_geteuid(cred); 38724543Smarks gid = ucred_getegid(cred); 38734543Smarks group_cnt = ucred_getgroups(cred, &groups); 38744543Smarks 38754543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 38764543Smarks return (1); 38774543Smarks 38784543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 38794543Smarks nvlist_free(nvp); 38804543Smarks return (1); 38814543Smarks } 38824543Smarks 38834543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 38844543Smarks nvlist_free(nvp); 38854543Smarks return (1); 38864543Smarks } 38874543Smarks 38884543Smarks if (nvlist_add_uint32_array(nvp, 38894543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 38904543Smarks nvlist_free(nvp); 38914543Smarks return (1); 38924543Smarks } 38934543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 38944543Smarks 38955094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 38964543Smarks return (-1); 38974543Smarks 38984543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 38994543Smarks nvlist_free(nvp); 39004543Smarks return (error); 39014543Smarks } 39024543Smarks 39034543Smarks int 39044543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 39055331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 39064543Smarks { 39074543Smarks zfs_cmd_t zc = { 0 }; 39084543Smarks int error; 39094543Smarks 39104543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39114543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 39124543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 39134543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 39145331Samw zc.zc_share.z_sharetype = operation; 39154543Smarks zc.zc_share.z_sharemax = sharemax; 39164543Smarks 39174543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 39184543Smarks return (error); 39194543Smarks } 3920