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; 483*5375Stimh 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; 751*5375Stimh case ZFS_PROP_CASE: 752*5375Stimh chosen_sense = (int)intval; 753*5375Stimh 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: 7752676Seschrock if (intval > volsize) { 7762676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7772676Seschrock "'%s' is greater than current " 7782676Seschrock "volume size"), propname); 7792676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7802676Seschrock errbuf); 7812676Seschrock goto error; 7822676Seschrock } 7832676Seschrock break; 7842676Seschrock 7852676Seschrock case ZFS_PROP_VOLSIZE: 7862676Seschrock if (intval % blocksize != 0) { 7872676Seschrock zfs_nicenum(blocksize, buf, 7882676Seschrock sizeof (buf)); 7892676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7902676Seschrock "'%s' must be a multiple of " 7912676Seschrock "volume block size (%s)"), 7922676Seschrock propname, buf); 7932676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7942676Seschrock errbuf); 7952676Seschrock goto error; 7962676Seschrock } 7972676Seschrock 7982676Seschrock if (intval == 0) { 7992676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8002676Seschrock "'%s' cannot be zero"), 8012676Seschrock propname); 8022676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 8032676Seschrock errbuf); 8042676Seschrock goto error; 805789Sahrens } 8063126Sahl break; 807789Sahrens } 808789Sahrens } 809789Sahrens } 810789Sahrens 8112676Seschrock /* 812*5375Stimh * Temporarily disallow any non-default settings for 813*5375Stimh * casesensitivity, normalization, and/or utf8only. 814*5375Stimh */ 815*5375Stimh if (chosen_sense > ZFS_CASE_SENSITIVE || chosen_utf > 0 || 816*5375Stimh chosen_normal > ZFS_NORMALIZE_NONE) { 817*5375Stimh zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 818*5375Stimh "Non-default values for casesensitivity, utf8only, and " 819*5375Stimh "normalization are (temporarily) disabled")); 820*5375Stimh (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 821*5375Stimh goto error; 822*5375Stimh } 823*5375Stimh 824*5375Stimh /* 8255331Samw * If normalization was chosen, but no UTF8 choice was made, 8265331Samw * enforce rejection of non-UTF8 names. 8275331Samw * 8285331Samw * If normalization was chosen, but rejecting non-UTF8 names 8295331Samw * was explicitly not chosen, it is an error. 8305331Samw */ 8315331Samw if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf < 0) { 8325331Samw if (nvlist_add_uint64(ret, 8335331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 8345331Samw (void) no_memory(hdl); 8355331Samw goto error; 8365331Samw } 8375331Samw } else if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf == 0) { 8385331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8395331Samw "'%s' must be set 'on' if normalization chosen"), 8405331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 8415331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8425331Samw goto error; 8435331Samw } 8445331Samw 8455331Samw /* 8462676Seschrock * If this is an existing volume, and someone is setting the volsize, 8472676Seschrock * make sure that it matches the reservation, or add it if necessary. 8482676Seschrock */ 8492676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 8502676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 8512676Seschrock &intval) == 0) { 8522676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 8532676Seschrock ZFS_PROP_VOLSIZE); 8542676Seschrock uint64_t old_reservation = zfs_prop_get_int(zhp, 8552676Seschrock ZFS_PROP_RESERVATION); 8562676Seschrock uint64_t new_reservation; 8572676Seschrock 8582676Seschrock if (old_volsize == old_reservation && 8592676Seschrock nvlist_lookup_uint64(ret, 8602676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8612676Seschrock &new_reservation) != 0) { 8622676Seschrock if (nvlist_add_uint64(ret, 8632676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8642676Seschrock intval) != 0) { 8652676Seschrock (void) no_memory(hdl); 8662676Seschrock goto error; 8672676Seschrock } 8682676Seschrock } 8692676Seschrock } 8702676Seschrock 8712676Seschrock return (ret); 8722676Seschrock 8732676Seschrock error: 8742676Seschrock nvlist_free(ret); 8752676Seschrock return (NULL); 876789Sahrens } 877789Sahrens 8784543Smarks static int 8794543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 8804543Smarks uint64_t *ret_who) 8814543Smarks { 8824543Smarks struct passwd *pwd; 8834543Smarks struct group *grp; 8844543Smarks uid_t id; 8854543Smarks 8864543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 8874543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 8884543Smarks *ret_who = -1; 8894543Smarks return (0); 8904543Smarks } 8914543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 8924543Smarks return (EZFS_BADWHO); 8934543Smarks 8944543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 8954543Smarks strcmp(who, "everyone") == 0) { 8964543Smarks *ret_who = -1; 8974543Smarks *who_type = ZFS_DELEG_EVERYONE; 8984543Smarks return (0); 8994543Smarks } 9004543Smarks 9014543Smarks pwd = getpwnam(who); 9024543Smarks grp = getgrnam(who); 9034543Smarks 9044543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 9054543Smarks *ret_who = pwd->pw_uid; 9064543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 9074543Smarks *ret_who = grp->gr_gid; 9084543Smarks } else if (pwd) { 9094543Smarks *ret_who = pwd->pw_uid; 9104543Smarks *who_type = ZFS_DELEG_USER; 9114543Smarks } else if (grp) { 9124543Smarks *ret_who = grp->gr_gid; 9134543Smarks *who_type = ZFS_DELEG_GROUP; 9144543Smarks } else { 9154543Smarks char *end; 9164543Smarks 9174543Smarks id = strtol(who, &end, 10); 9184543Smarks if (errno != 0 || *end != '\0') { 9194543Smarks return (EZFS_BADWHO); 9204543Smarks } else { 9214543Smarks *ret_who = id; 9224543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 9234543Smarks *who_type = ZFS_DELEG_USER; 9244543Smarks } 9254543Smarks } 9264543Smarks 9274543Smarks return (0); 9284543Smarks } 9294543Smarks 9304543Smarks static void 9314543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 9324543Smarks { 9334543Smarks if (perms_nvp != NULL) { 9344543Smarks verify(nvlist_add_nvlist(who_nvp, 9354543Smarks name, perms_nvp) == 0); 9364543Smarks } else { 9374543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 9384543Smarks } 9394543Smarks } 9404543Smarks 9414543Smarks static void 9424543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 9434543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 9444543Smarks nvlist_t *sets_nvp) 9454543Smarks { 9464543Smarks boolean_t do_perms, do_sets; 9474543Smarks char name[ZFS_MAX_DELEG_NAME]; 9484543Smarks 9494543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 9504543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 9514543Smarks 9524543Smarks if (!do_perms && !do_sets) 9534543Smarks do_perms = do_sets = B_TRUE; 9544543Smarks 9554543Smarks if (do_perms) { 9564543Smarks zfs_deleg_whokey(name, who_type, inherit, 9574543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9584543Smarks whostr : (void *)&whoid); 9594543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 9604543Smarks } 9614543Smarks if (do_sets) { 9624543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 9634543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9644543Smarks whostr : (void *)&whoid); 9654543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 9664543Smarks } 9674543Smarks } 9684543Smarks 9694543Smarks static void 9704543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 9714543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 9724543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 9734543Smarks { 9744543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 9754543Smarks helper(who_type, whoid, whostr, 0, 9764543Smarks who_nvp, perms_nvp, sets_nvp); 9774543Smarks } else { 9784543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 9794543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 9804543Smarks who_nvp, perms_nvp, sets_nvp); 9814543Smarks } 9824543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 9834543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 9844543Smarks who_nvp, perms_nvp, sets_nvp); 9854543Smarks } 9864543Smarks } 9874543Smarks } 9884543Smarks 9894543Smarks /* 9904543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 9914543Smarks * 9924543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 9934543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 9944543Smarks * base attribute named stored in the dsl. 9954543Smarks * Arguments: 9964543Smarks * 9974543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 9984543Smarks * whostr may be null for everyone or create perms. 9994543Smarks * who_type: is the type of entry in whostr. Typically this will be 10004543Smarks * ZFS_DELEG_WHO_UNKNOWN. 10015331Samw * perms: common separated list of permissions. May be null if user 10024543Smarks * is requested to remove permissions by who. 10034543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 10044543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 10054543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 10064543Smarks * The output nvp will look something like this. 10074543Smarks * ul$1234 -> {create ; destroy } 10084543Smarks * Ul$1234 -> { @myset } 10094543Smarks * s-$@myset - { snapshot; checksum; compression } 10104543Smarks */ 10114543Smarks int 10124543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 10134543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 10144543Smarks { 10154543Smarks nvlist_t *who_nvp; 10164543Smarks nvlist_t *perms_nvp = NULL; 10174543Smarks nvlist_t *sets_nvp = NULL; 10184543Smarks char errbuf[1024]; 10194787Sahrens char *who_tok, *perm; 10204543Smarks int error; 10214543Smarks 10224543Smarks *nvp = NULL; 10234543Smarks 10244543Smarks if (perms) { 10254543Smarks if ((error = nvlist_alloc(&perms_nvp, 10264543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10274543Smarks return (1); 10284543Smarks } 10294543Smarks if ((error = nvlist_alloc(&sets_nvp, 10304543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10314543Smarks nvlist_free(perms_nvp); 10324543Smarks return (1); 10334543Smarks } 10344543Smarks } 10354543Smarks 10364543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 10374543Smarks if (perms_nvp) 10384543Smarks nvlist_free(perms_nvp); 10394543Smarks if (sets_nvp) 10404543Smarks nvlist_free(sets_nvp); 10414543Smarks return (1); 10424543Smarks } 10434543Smarks 10444543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 10454543Smarks namecheck_err_t why; 10464543Smarks char what; 10474543Smarks 10484543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 10494787Sahrens nvlist_free(who_nvp); 10504787Sahrens if (perms_nvp) 10514787Sahrens nvlist_free(perms_nvp); 10524787Sahrens if (sets_nvp) 10534787Sahrens nvlist_free(sets_nvp); 10544787Sahrens 10554543Smarks switch (why) { 10564543Smarks case NAME_ERR_NO_AT: 10574543Smarks zfs_error_aux(zhp->zfs_hdl, 10584543Smarks dgettext(TEXT_DOMAIN, 10594543Smarks "set definition must begin with an '@' " 10604543Smarks "character")); 10614543Smarks } 10624543Smarks return (zfs_error(zhp->zfs_hdl, 10634543Smarks EZFS_BADPERMSET, whostr)); 10644543Smarks } 10654543Smarks } 10664543Smarks 10674543Smarks /* 10684543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 10694543Smarks * The first nvlist perms_nvp will have normal permissions and the 10704543Smarks * other sets_nvp will have only permssion set names in it. 10714543Smarks */ 10724787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 10734787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 10744787Sahrens 10754787Sahrens if (perm_canonical) { 10764787Sahrens verify(nvlist_add_boolean(perms_nvp, 10774787Sahrens perm_canonical) == 0); 10784787Sahrens } else if (perm[0] == '@') { 10794787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 10804787Sahrens } else { 10814787Sahrens nvlist_free(who_nvp); 10824787Sahrens nvlist_free(perms_nvp); 10834787Sahrens nvlist_free(sets_nvp); 10844787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 10854543Smarks } 10864543Smarks } 10874543Smarks 10884543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 10894543Smarks who_tok = strtok(whostr, ","); 10904543Smarks if (who_tok == NULL) { 10914543Smarks nvlist_free(who_nvp); 10924787Sahrens if (perms_nvp) 10934787Sahrens nvlist_free(perms_nvp); 10944543Smarks if (sets_nvp) 10954543Smarks nvlist_free(sets_nvp); 10964543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10974543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 10984543Smarks whostr); 10994543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11004543Smarks } 11014543Smarks } 11024543Smarks 11034543Smarks /* 11044543Smarks * Now create the nvlist(s) 11054543Smarks */ 11064543Smarks do { 11074543Smarks uint64_t who_id; 11084543Smarks 11094543Smarks error = zfs_get_perm_who(who_tok, &who_type, 11104543Smarks &who_id); 11114543Smarks if (error) { 11124543Smarks nvlist_free(who_nvp); 11134787Sahrens if (perms_nvp) 11144787Sahrens nvlist_free(perms_nvp); 11154543Smarks if (sets_nvp) 11164543Smarks nvlist_free(sets_nvp); 11174543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11184543Smarks dgettext(TEXT_DOMAIN, 11194543Smarks "Unable to determine uid/gid for " 11204543Smarks "%s "), who_tok); 11214543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11224543Smarks } 11234543Smarks 11244543Smarks /* 11254543Smarks * add entries for both local and descendent when required 11264543Smarks */ 11274543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 11284543Smarks perms_nvp, sets_nvp, who_type, inherit); 11294543Smarks 11304543Smarks } while (who_tok = strtok(NULL, ",")); 11314543Smarks *nvp = who_nvp; 11324543Smarks return (0); 11334543Smarks } 11344543Smarks 11354543Smarks static int 11364543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 11374543Smarks { 11384543Smarks zfs_cmd_t zc = { 0 }; 11394543Smarks int error; 11404543Smarks char errbuf[1024]; 11414543Smarks 11424543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11434543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 11444543Smarks zhp->zfs_name); 11454543Smarks 11465094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 11474543Smarks return (-1); 11484543Smarks 11494543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 11504543Smarks zc.zc_perm_action = unset; 11514543Smarks 11524543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 11534543Smarks if (error && errno == ENOTSUP) { 11544543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11554543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 11564543Smarks zcmd_free_nvlists(&zc); 11574543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 11584543Smarks } else if (error) { 11594543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 11604543Smarks } 11614543Smarks zcmd_free_nvlists(&zc); 11624543Smarks 11634543Smarks return (error); 11644543Smarks } 11654543Smarks 11664543Smarks int 11674543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 11684543Smarks { 11694543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 11704543Smarks } 11714543Smarks 11724543Smarks int 11734543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 11744543Smarks { 11754543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 11764543Smarks } 11774543Smarks 11784543Smarks static int 11794543Smarks perm_compare(const void *arg1, const void *arg2) 11804543Smarks { 11814543Smarks const zfs_perm_node_t *node1 = arg1; 11824543Smarks const zfs_perm_node_t *node2 = arg2; 11834543Smarks int ret; 11844543Smarks 11854543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 11864543Smarks 11874543Smarks if (ret > 0) 11884543Smarks return (1); 11894543Smarks if (ret < 0) 11904543Smarks return (-1); 11914543Smarks else 11924543Smarks return (0); 11934543Smarks } 11944543Smarks 11954543Smarks static void 11964543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 11974543Smarks { 11984543Smarks zfs_perm_node_t *permnode; 11995367Sahrens void *cookie = NULL; 12005367Sahrens 12015367Sahrens while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 12024543Smarks free(permnode); 12035367Sahrens avl_destroy(tree); 12044543Smarks } 12054543Smarks 12064543Smarks static void 12074543Smarks zfs_destroy_tree(avl_tree_t *tree) 12084543Smarks { 12094543Smarks zfs_allow_node_t *allownode; 12105367Sahrens void *cookie = NULL; 12115367Sahrens 12124543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 12134543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 12144543Smarks zfs_destroy_perm_tree(&allownode->z_local); 12154543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 12164543Smarks free(allownode); 12174543Smarks } 12185367Sahrens avl_destroy(tree); 12194543Smarks } 12204543Smarks 12214543Smarks void 12224543Smarks zfs_free_allows(zfs_allow_t *allow) 12234543Smarks { 12244543Smarks zfs_allow_t *allownext; 12254543Smarks zfs_allow_t *freeallow; 12264543Smarks 12274543Smarks allownext = allow; 12284543Smarks while (allownext) { 12294543Smarks zfs_destroy_tree(&allownext->z_sets); 12304543Smarks zfs_destroy_tree(&allownext->z_crperms); 12314543Smarks zfs_destroy_tree(&allownext->z_user); 12324543Smarks zfs_destroy_tree(&allownext->z_group); 12334543Smarks zfs_destroy_tree(&allownext->z_everyone); 12344543Smarks freeallow = allownext; 12354543Smarks allownext = allownext->z_next; 12364543Smarks free(freeallow); 12374543Smarks } 12384543Smarks } 12394543Smarks 12404543Smarks static zfs_allow_t * 12414543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 12424543Smarks { 12434543Smarks zfs_allow_t *ptree; 12444543Smarks 12454543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 12464543Smarks sizeof (zfs_allow_t))) == NULL) { 12474543Smarks return (NULL); 12484543Smarks } 12494543Smarks 12504543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 12514543Smarks avl_create(&ptree->z_sets, 12524543Smarks perm_compare, sizeof (zfs_allow_node_t), 12534543Smarks offsetof(zfs_allow_node_t, z_node)); 12544543Smarks avl_create(&ptree->z_crperms, 12554543Smarks perm_compare, sizeof (zfs_allow_node_t), 12564543Smarks offsetof(zfs_allow_node_t, z_node)); 12574543Smarks avl_create(&ptree->z_user, 12584543Smarks perm_compare, sizeof (zfs_allow_node_t), 12594543Smarks offsetof(zfs_allow_node_t, z_node)); 12604543Smarks avl_create(&ptree->z_group, 12614543Smarks perm_compare, sizeof (zfs_allow_node_t), 12624543Smarks offsetof(zfs_allow_node_t, z_node)); 12634543Smarks avl_create(&ptree->z_everyone, 12644543Smarks perm_compare, sizeof (zfs_allow_node_t), 12654543Smarks offsetof(zfs_allow_node_t, z_node)); 12664543Smarks 12674543Smarks if (prev) 12684543Smarks prev->z_next = ptree; 12694543Smarks ptree->z_next = NULL; 12704543Smarks return (ptree); 12714543Smarks } 12724543Smarks 12734543Smarks /* 12744543Smarks * Add permissions to the appropriate AVL permission tree. 12754543Smarks * The appropriate tree may not be the requested tree. 12764543Smarks * For example if ld indicates a local permission, but 12774543Smarks * same permission also exists as a descendent permission 12784543Smarks * then the permission will be removed from the descendent 12794543Smarks * tree and add the the local+descendent tree. 12804543Smarks */ 12814543Smarks static int 12824543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 12834543Smarks char *perm, char ld) 12844543Smarks { 12854543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 12864543Smarks zfs_perm_node_t *newnode; 12874543Smarks avl_index_t where, where2; 12884543Smarks avl_tree_t *tree, *altree; 12894543Smarks 12904543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 12914543Smarks 12924543Smarks if (ld == ZFS_DELEG_NA) { 12934543Smarks tree = &allownode->z_localdescend; 12944543Smarks altree = &allownode->z_descend; 12954543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 12964543Smarks tree = &allownode->z_local; 12974543Smarks altree = &allownode->z_descend; 12984543Smarks } else { 12994543Smarks tree = &allownode->z_descend; 13004543Smarks altree = &allownode->z_local; 13014543Smarks } 13024543Smarks permnode = avl_find(tree, &pnode, &where); 13034543Smarks permnode2 = avl_find(altree, &pnode, &where2); 13044543Smarks 13054543Smarks if (permnode2) { 13064543Smarks avl_remove(altree, permnode2); 13074543Smarks free(permnode2); 13084543Smarks if (permnode == NULL) { 13094543Smarks tree = &allownode->z_localdescend; 13104543Smarks } 13114543Smarks } 13124543Smarks 13134543Smarks /* 13144543Smarks * Now insert new permission in either requested location 13154543Smarks * local/descendent or into ld when perm will exist in both. 13164543Smarks */ 13174543Smarks if (permnode == NULL) { 13184543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 13194543Smarks sizeof (zfs_perm_node_t))) == NULL) { 13204543Smarks return (-1); 13214543Smarks } 13224543Smarks *newnode = pnode; 13234543Smarks avl_add(tree, newnode); 13244543Smarks } 13254543Smarks return (0); 13264543Smarks } 13274577Sahrens 13284543Smarks /* 13294543Smarks * Uggh, this is going to be a bit complicated. 13304543Smarks * we have an nvlist coming out of the kernel that 13314543Smarks * will indicate where the permission is set and then 13324543Smarks * it will contain allow of the various "who's", and what 13334543Smarks * their permissions are. To further complicate this 13344543Smarks * we will then have to coalesce the local,descendent 13354543Smarks * and local+descendent permissions where appropriate. 13364543Smarks * The kernel only knows about a permission as being local 13374543Smarks * or descendent, but not both. 13384543Smarks * 13394543Smarks * In order to make this easier for zfs_main to deal with 13404543Smarks * a series of AVL trees will be used to maintain 13414543Smarks * all of this, primarily for sorting purposes as well 13424543Smarks * as the ability to quickly locate a specific entry. 13434543Smarks * 13444543Smarks * What we end up with are tree's for sets, create perms, 13454543Smarks * user, groups and everyone. With each of those trees 13464543Smarks * we have subtrees for local, descendent and local+descendent 13474543Smarks * permissions. 13484543Smarks */ 13494543Smarks int 13504543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 13514543Smarks { 13524543Smarks zfs_cmd_t zc = { 0 }; 13534543Smarks int error; 13544543Smarks nvlist_t *nvlist; 13554543Smarks nvlist_t *permnv, *sourcenv; 13564543Smarks nvpair_t *who_pair, *source_pair; 13574543Smarks nvpair_t *perm_pair; 13584543Smarks char errbuf[1024]; 13594543Smarks zfs_allow_t *zallowp, *newallowp; 13604543Smarks char ld; 13614543Smarks char *nvpname; 13624543Smarks uid_t uid; 13634543Smarks gid_t gid; 13644543Smarks avl_tree_t *tree; 13654543Smarks avl_index_t where; 13664543Smarks 13674543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13684543Smarks 13694543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 13704543Smarks return (-1); 13714543Smarks 13724543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 13734543Smarks if (errno == ENOMEM) { 13744543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 13754543Smarks zcmd_free_nvlists(&zc); 13764543Smarks return (-1); 13774543Smarks } 13784543Smarks } else if (errno == ENOTSUP) { 13794543Smarks zcmd_free_nvlists(&zc); 13804543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13814543Smarks gettext("Pool must be upgraded to use 'allow'")); 13824543Smarks return (zfs_error(zhp->zfs_hdl, 13834543Smarks EZFS_BADVERSION, errbuf)); 13844543Smarks } else { 13854543Smarks zcmd_free_nvlists(&zc); 13864543Smarks return (-1); 13874543Smarks } 13884543Smarks } 13894543Smarks 13904543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 13914543Smarks zcmd_free_nvlists(&zc); 13924543Smarks return (-1); 13934543Smarks } 13944543Smarks 13954543Smarks zcmd_free_nvlists(&zc); 13964543Smarks 13974543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 13984543Smarks 13994543Smarks if (source_pair == NULL) { 14004543Smarks *zfs_perms = NULL; 14014543Smarks return (0); 14024543Smarks } 14034543Smarks 14044543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 14054543Smarks if (*zfs_perms == NULL) { 14064543Smarks return (0); 14074543Smarks } 14084543Smarks 14094543Smarks zallowp = *zfs_perms; 14104543Smarks 14114543Smarks for (;;) { 14124543Smarks struct passwd *pwd; 14134543Smarks struct group *grp; 14144543Smarks zfs_allow_node_t *allownode; 14154543Smarks zfs_allow_node_t findallownode; 14164543Smarks zfs_allow_node_t *newallownode; 14174543Smarks 14184543Smarks (void) strlcpy(zallowp->z_setpoint, 14194543Smarks nvpair_name(source_pair), 14204543Smarks sizeof (zallowp->z_setpoint)); 14214543Smarks 14224543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 14234543Smarks goto abort; 14244543Smarks 14254543Smarks /* 14264543Smarks * Make sure nvlist is composed correctly 14274543Smarks */ 14284543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 14294543Smarks goto abort; 14304543Smarks } 14314543Smarks 14324543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 14334543Smarks if (who_pair == NULL) { 14344543Smarks goto abort; 14354543Smarks } 14364543Smarks 14374543Smarks do { 14384543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 14394543Smarks if (error) { 14404543Smarks goto abort; 14414543Smarks } 14424543Smarks 14434543Smarks /* 14444543Smarks * First build up the key to use 14454543Smarks * for looking up in the various 14464543Smarks * who trees. 14474543Smarks */ 14484543Smarks ld = nvpair_name(who_pair)[1]; 14494543Smarks nvpname = nvpair_name(who_pair); 14504543Smarks switch (nvpair_name(who_pair)[0]) { 14514543Smarks case ZFS_DELEG_USER: 14524543Smarks case ZFS_DELEG_USER_SETS: 14534543Smarks tree = &zallowp->z_user; 14544543Smarks uid = atol(&nvpname[3]); 14554543Smarks pwd = getpwuid(uid); 14564543Smarks (void) snprintf(findallownode.z_key, 14574543Smarks sizeof (findallownode.z_key), "user %s", 14584543Smarks (pwd) ? pwd->pw_name : 14594543Smarks &nvpair_name(who_pair)[3]); 14604543Smarks break; 14614543Smarks case ZFS_DELEG_GROUP: 14624543Smarks case ZFS_DELEG_GROUP_SETS: 14634543Smarks tree = &zallowp->z_group; 14644543Smarks gid = atol(&nvpname[3]); 14654543Smarks grp = getgrgid(gid); 14664543Smarks (void) snprintf(findallownode.z_key, 14674543Smarks sizeof (findallownode.z_key), "group %s", 14684543Smarks (grp) ? grp->gr_name : 14694543Smarks &nvpair_name(who_pair)[3]); 14704543Smarks break; 14714543Smarks case ZFS_DELEG_CREATE: 14724543Smarks case ZFS_DELEG_CREATE_SETS: 14734543Smarks tree = &zallowp->z_crperms; 14744543Smarks (void) strlcpy(findallownode.z_key, "", 14754543Smarks sizeof (findallownode.z_key)); 14764543Smarks break; 14774543Smarks case ZFS_DELEG_EVERYONE: 14784543Smarks case ZFS_DELEG_EVERYONE_SETS: 14794543Smarks (void) snprintf(findallownode.z_key, 14804543Smarks sizeof (findallownode.z_key), "everyone"); 14814543Smarks tree = &zallowp->z_everyone; 14824543Smarks break; 14834543Smarks case ZFS_DELEG_NAMED_SET: 14844543Smarks case ZFS_DELEG_NAMED_SET_SETS: 14854543Smarks (void) snprintf(findallownode.z_key, 14864543Smarks sizeof (findallownode.z_key), "%s", 14874543Smarks &nvpair_name(who_pair)[3]); 14884543Smarks tree = &zallowp->z_sets; 14894543Smarks break; 14904543Smarks } 14914543Smarks 14924543Smarks /* 14934543Smarks * Place who in tree 14944543Smarks */ 14954543Smarks allownode = avl_find(tree, &findallownode, &where); 14964543Smarks if (allownode == NULL) { 14974543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 14984543Smarks sizeof (zfs_allow_node_t))) == NULL) { 14994543Smarks goto abort; 15004543Smarks } 15014543Smarks avl_create(&newallownode->z_localdescend, 15024543Smarks perm_compare, 15034543Smarks sizeof (zfs_perm_node_t), 15044543Smarks offsetof(zfs_perm_node_t, z_node)); 15054543Smarks avl_create(&newallownode->z_local, 15064543Smarks perm_compare, 15074543Smarks sizeof (zfs_perm_node_t), 15084543Smarks offsetof(zfs_perm_node_t, z_node)); 15094543Smarks avl_create(&newallownode->z_descend, 15104543Smarks perm_compare, 15114543Smarks sizeof (zfs_perm_node_t), 15124543Smarks offsetof(zfs_perm_node_t, z_node)); 15134543Smarks (void) strlcpy(newallownode->z_key, 15144543Smarks findallownode.z_key, 15154543Smarks sizeof (findallownode.z_key)); 15164543Smarks avl_insert(tree, newallownode, where); 15174543Smarks allownode = newallownode; 15184543Smarks } 15194543Smarks 15204543Smarks /* 15214543Smarks * Now iterate over the permissions and 15224543Smarks * place them in the appropriate local, 15234543Smarks * descendent or local+descendent tree. 15244543Smarks * 15254543Smarks * The permissions are added to the tree 15264543Smarks * via zfs_coalesce_perm(). 15274543Smarks */ 15284543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 15294543Smarks if (perm_pair == NULL) 15304543Smarks goto abort; 15314543Smarks do { 15324543Smarks if (zfs_coalesce_perm(zhp, allownode, 15334543Smarks nvpair_name(perm_pair), ld) != 0) 15344543Smarks goto abort; 15354543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 15364543Smarks perm_pair)); 15374543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 15384543Smarks 15394543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 15404543Smarks if (source_pair == NULL) 15414543Smarks break; 15424543Smarks 15434543Smarks /* 15444543Smarks * allocate another node from the link list of 15454543Smarks * zfs_allow_t structures 15464543Smarks */ 15474543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 15484543Smarks nvpair_name(source_pair)); 15494543Smarks if (newallowp == NULL) { 15504543Smarks goto abort; 15514543Smarks } 15524543Smarks zallowp = newallowp; 15534543Smarks } 15544543Smarks nvlist_free(nvlist); 15554543Smarks return (0); 15564543Smarks abort: 15574543Smarks zfs_free_allows(*zfs_perms); 15584543Smarks nvlist_free(nvlist); 15594543Smarks return (-1); 15604543Smarks } 15614543Smarks 1562789Sahrens /* 1563789Sahrens * Given a property name and value, set the property for the given dataset. 1564789Sahrens */ 1565789Sahrens int 15662676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1567789Sahrens { 1568789Sahrens zfs_cmd_t zc = { 0 }; 15692676Seschrock int ret = -1; 15702676Seschrock prop_changelist_t *cl = NULL; 15712082Seschrock char errbuf[1024]; 15722082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 15732676Seschrock nvlist_t *nvl = NULL, *realprops; 15742676Seschrock zfs_prop_t prop; 15752082Seschrock 15762082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 15772676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 15782082Seschrock zhp->zfs_name); 15792082Seschrock 15802676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 15812676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 15822676Seschrock (void) no_memory(hdl); 15832676Seschrock goto error; 1584789Sahrens } 1585789Sahrens 15865094Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 15872676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 15882676Seschrock goto error; 15895094Slling 15902676Seschrock nvlist_free(nvl); 15912676Seschrock nvl = realprops; 15922676Seschrock 15932676Seschrock prop = zfs_name_to_prop(propname); 15942676Seschrock 1595789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 15962676Seschrock goto error; 1597789Sahrens 1598789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 15992082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16002082Seschrock "child dataset with inherited mountpoint is used " 16012082Seschrock "in a non-global zone")); 16022082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1603789Sahrens goto error; 1604789Sahrens } 1605789Sahrens 1606789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1607789Sahrens goto error; 1608789Sahrens 1609789Sahrens /* 1610789Sahrens * Execute the corresponding ioctl() to set this property. 1611789Sahrens */ 1612789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1613789Sahrens 16145094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 16152676Seschrock goto error; 16162676Seschrock 16174543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1618789Sahrens 1619789Sahrens if (ret != 0) { 1620789Sahrens switch (errno) { 1621789Sahrens 1622789Sahrens case ENOSPC: 1623789Sahrens /* 1624789Sahrens * For quotas and reservations, ENOSPC indicates 1625789Sahrens * something different; setting a quota or reservation 1626789Sahrens * doesn't use any disk space. 1627789Sahrens */ 1628789Sahrens switch (prop) { 1629789Sahrens case ZFS_PROP_QUOTA: 16302082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16312082Seschrock "size is less than current used or " 16322082Seschrock "reserved space")); 16332082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1634789Sahrens break; 1635789Sahrens 1636789Sahrens case ZFS_PROP_RESERVATION: 16372082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16382082Seschrock "size is greater than available space")); 16392082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1640789Sahrens break; 1641789Sahrens 1642789Sahrens default: 16432082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1644789Sahrens break; 1645789Sahrens } 1646789Sahrens break; 1647789Sahrens 1648789Sahrens case EBUSY: 16492082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 16502082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 16512082Seschrock else 16522676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1653789Sahrens break; 1654789Sahrens 16551175Slling case EROFS: 16562082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 16571175Slling break; 16581175Slling 16593886Sahl case ENOTSUP: 16603886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16614603Sahrens "pool must be upgraded to set this " 16624603Sahrens "property or value")); 16633886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 16643886Sahl break; 16653886Sahl 1666789Sahrens case EOVERFLOW: 1667789Sahrens /* 1668789Sahrens * This platform can't address a volume this big. 1669789Sahrens */ 1670789Sahrens #ifdef _ILP32 1671789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 16722082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1673789Sahrens break; 1674789Sahrens } 1675789Sahrens #endif 16762082Seschrock /* FALLTHROUGH */ 1677789Sahrens default: 16782082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1679789Sahrens } 1680789Sahrens } else { 1681789Sahrens /* 1682789Sahrens * Refresh the statistics so the new property value 1683789Sahrens * is reflected. 1684789Sahrens */ 16852676Seschrock if ((ret = changelist_postfix(cl)) == 0) 16862676Seschrock (void) get_stats(zhp); 1687789Sahrens } 1688789Sahrens 1689789Sahrens error: 16902676Seschrock nvlist_free(nvl); 16912676Seschrock zcmd_free_nvlists(&zc); 16922676Seschrock if (cl) 16932676Seschrock changelist_free(cl); 1694789Sahrens return (ret); 1695789Sahrens } 1696789Sahrens 1697789Sahrens /* 1698789Sahrens * Given a property, inherit the value from the parent dataset. 1699789Sahrens */ 1700789Sahrens int 17012676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1702789Sahrens { 1703789Sahrens zfs_cmd_t zc = { 0 }; 1704789Sahrens int ret; 1705789Sahrens prop_changelist_t *cl; 17062082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 17072082Seschrock char errbuf[1024]; 17082676Seschrock zfs_prop_t prop; 17092082Seschrock 17102082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 17112082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1712789Sahrens 17135094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 17142676Seschrock /* 17152676Seschrock * For user properties, the amount of work we have to do is very 17162676Seschrock * small, so just do it here. 17172676Seschrock */ 17182676Seschrock if (!zfs_prop_user(propname)) { 17192676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17202676Seschrock "invalid property")); 17212676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 17222676Seschrock } 17232676Seschrock 17242676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17252676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 17262676Seschrock 17274849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 17282676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 17292676Seschrock 17302676Seschrock return (0); 17312676Seschrock } 17322676Seschrock 1733789Sahrens /* 1734789Sahrens * Verify that this property is inheritable. 1735789Sahrens */ 17362082Seschrock if (zfs_prop_readonly(prop)) 17372082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 17382082Seschrock 17392082Seschrock if (!zfs_prop_inheritable(prop)) 17402082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1741789Sahrens 1742789Sahrens /* 1743789Sahrens * Check to see if the value applies to this type 1744789Sahrens */ 17452082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 17462082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1747789Sahrens 17483443Srm160521 /* 17493443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 17503443Srm160521 */ 17513443Srm160521 propname = zfs_prop_to_name(prop); 1752789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17532676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1754789Sahrens 1755789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1756789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 17572082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17582082Seschrock "dataset is used in a non-global zone")); 17592082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1760789Sahrens } 1761789Sahrens 1762789Sahrens /* 1763789Sahrens * Determine datasets which will be affected by this change, if any. 1764789Sahrens */ 1765789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1766789Sahrens return (-1); 1767789Sahrens 1768789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 17692082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17702082Seschrock "child dataset with inherited mountpoint is used " 17712082Seschrock "in a non-global zone")); 17722082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1773789Sahrens goto error; 1774789Sahrens } 1775789Sahrens 1776789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1777789Sahrens goto error; 1778789Sahrens 17794849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 17802082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1781789Sahrens } else { 1782789Sahrens 17832169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1784789Sahrens goto error; 1785789Sahrens 1786789Sahrens /* 1787789Sahrens * Refresh the statistics so the new property is reflected. 1788789Sahrens */ 1789789Sahrens (void) get_stats(zhp); 1790789Sahrens } 1791789Sahrens 1792789Sahrens error: 1793789Sahrens changelist_free(cl); 1794789Sahrens return (ret); 1795789Sahrens } 1796789Sahrens 1797789Sahrens /* 17981356Seschrock * True DSL properties are stored in an nvlist. The following two functions 17991356Seschrock * extract them appropriately. 18001356Seschrock */ 18011356Seschrock static uint64_t 18021356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18031356Seschrock { 18041356Seschrock nvlist_t *nv; 18051356Seschrock uint64_t value; 18061356Seschrock 18072885Sahrens *source = NULL; 18081356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18091356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 18105094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 18115094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18121356Seschrock } else { 18131356Seschrock value = zfs_prop_default_numeric(prop); 18141356Seschrock *source = ""; 18151356Seschrock } 18161356Seschrock 18171356Seschrock return (value); 18181356Seschrock } 18191356Seschrock 18201356Seschrock static char * 18211356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18221356Seschrock { 18231356Seschrock nvlist_t *nv; 18241356Seschrock char *value; 18251356Seschrock 18262885Sahrens *source = NULL; 18271356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18281356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 18295094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 18305094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18311356Seschrock } else { 18321356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 18331356Seschrock value = ""; 18341356Seschrock *source = ""; 18351356Seschrock } 18361356Seschrock 18371356Seschrock return (value); 18381356Seschrock } 18391356Seschrock 18401356Seschrock /* 1841789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1842789Sahrens * zfs_prop_get_int() are built using this interface. 1843789Sahrens * 1844789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1845789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1846789Sahrens * If they differ from the on-disk values, report the current values and mark 1847789Sahrens * the source "temporary". 1848789Sahrens */ 18492082Seschrock static int 18505094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 18512082Seschrock char **source, uint64_t *val) 1852789Sahrens { 18535147Srm160521 zfs_cmd_t zc = { 0 }; 1854789Sahrens struct mnttab mnt; 18553265Sahrens char *mntopt_on = NULL; 18563265Sahrens char *mntopt_off = NULL; 1857789Sahrens 1858789Sahrens *source = NULL; 1859789Sahrens 18603265Sahrens switch (prop) { 18613265Sahrens case ZFS_PROP_ATIME: 18623265Sahrens mntopt_on = MNTOPT_ATIME; 18633265Sahrens mntopt_off = MNTOPT_NOATIME; 18643265Sahrens break; 18653265Sahrens 18663265Sahrens case ZFS_PROP_DEVICES: 18673265Sahrens mntopt_on = MNTOPT_DEVICES; 18683265Sahrens mntopt_off = MNTOPT_NODEVICES; 18693265Sahrens break; 18703265Sahrens 18713265Sahrens case ZFS_PROP_EXEC: 18723265Sahrens mntopt_on = MNTOPT_EXEC; 18733265Sahrens mntopt_off = MNTOPT_NOEXEC; 18743265Sahrens break; 18753265Sahrens 18763265Sahrens case ZFS_PROP_READONLY: 18773265Sahrens mntopt_on = MNTOPT_RO; 18783265Sahrens mntopt_off = MNTOPT_RW; 18793265Sahrens break; 18803265Sahrens 18813265Sahrens case ZFS_PROP_SETUID: 18823265Sahrens mntopt_on = MNTOPT_SETUID; 18833265Sahrens mntopt_off = MNTOPT_NOSETUID; 18843265Sahrens break; 18853265Sahrens 18863265Sahrens case ZFS_PROP_XATTR: 18873265Sahrens mntopt_on = MNTOPT_XATTR; 18883265Sahrens mntopt_off = MNTOPT_NOXATTR; 18893265Sahrens break; 18905331Samw 18915331Samw case ZFS_PROP_NBMAND: 18925331Samw mntopt_on = MNTOPT_NBMAND; 18935331Samw mntopt_off = MNTOPT_NONBMAND; 18945331Samw break; 18953265Sahrens } 18963265Sahrens 18972474Seschrock /* 18982474Seschrock * Because looking up the mount options is potentially expensive 18992474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 19002474Seschrock * we're looking up a property which requires its presence. 19012474Seschrock */ 19022474Seschrock if (!zhp->zfs_mntcheck && 19033265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 19043265Sahrens struct mnttab entry, search = { 0 }; 19053265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 19062474Seschrock 19072474Seschrock search.mnt_special = (char *)zhp->zfs_name; 19082474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 19093265Sahrens rewind(mnttab); 19103265Sahrens 19113265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 19123265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 19133265Sahrens entry.mnt_mntopts); 19143265Sahrens if (zhp->zfs_mntopts == NULL) 19153265Sahrens return (-1); 19163265Sahrens } 19172474Seschrock 19182474Seschrock zhp->zfs_mntcheck = B_TRUE; 19192474Seschrock } 19202474Seschrock 1921789Sahrens if (zhp->zfs_mntopts == NULL) 1922789Sahrens mnt.mnt_mntopts = ""; 1923789Sahrens else 1924789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1925789Sahrens 1926789Sahrens switch (prop) { 1927789Sahrens case ZFS_PROP_ATIME: 19283265Sahrens case ZFS_PROP_DEVICES: 19293265Sahrens case ZFS_PROP_EXEC: 19303265Sahrens case ZFS_PROP_READONLY: 19313265Sahrens case ZFS_PROP_SETUID: 19323265Sahrens case ZFS_PROP_XATTR: 19335331Samw case ZFS_PROP_NBMAND: 19342082Seschrock *val = getprop_uint64(zhp, prop, source); 19352082Seschrock 19363265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 19372082Seschrock *val = B_TRUE; 1938789Sahrens if (src) 19395094Slling *src = ZPROP_SRC_TEMPORARY; 19403265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 19412082Seschrock *val = B_FALSE; 1942789Sahrens if (src) 19435094Slling *src = ZPROP_SRC_TEMPORARY; 1944789Sahrens } 19452082Seschrock break; 1946789Sahrens 19473265Sahrens case ZFS_PROP_CANMOUNT: 19482082Seschrock *val = getprop_uint64(zhp, prop, source); 19493417Srm160521 if (*val == 0) 19503417Srm160521 *source = zhp->zfs_name; 19513417Srm160521 else 19523417Srm160521 *source = ""; /* default */ 19532082Seschrock break; 1954789Sahrens 1955789Sahrens case ZFS_PROP_QUOTA: 1956789Sahrens case ZFS_PROP_RESERVATION: 19572885Sahrens *val = getprop_uint64(zhp, prop, source); 19582885Sahrens if (*val == 0) 1959789Sahrens *source = ""; /* default */ 1960789Sahrens else 1961789Sahrens *source = zhp->zfs_name; 19622082Seschrock break; 1963789Sahrens 1964789Sahrens case ZFS_PROP_MOUNTED: 19652082Seschrock *val = (zhp->zfs_mntopts != NULL); 19662082Seschrock break; 1967789Sahrens 19683377Seschrock case ZFS_PROP_NUMCLONES: 19693377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 19703377Seschrock break; 19713377Seschrock 19725147Srm160521 case ZFS_PROP_VERSION: 19735147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19745147Srm160521 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_VERSION, &zc) || 19755147Srm160521 (zc.zc_cookie == 0)) { 19765147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19775147Srm160521 "unable to get version property")); 19785147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 19795147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 19805147Srm160521 } 19815147Srm160521 *val = zc.zc_cookie; 19825147Srm160521 break; 19835147Srm160521 1984789Sahrens default: 19854577Sahrens switch (zfs_prop_get_type(prop)) { 19864787Sahrens case PROP_TYPE_NUMBER: 19874787Sahrens case PROP_TYPE_INDEX: 19884577Sahrens *val = getprop_uint64(zhp, prop, source); 19894577Sahrens break; 19904577Sahrens 19914787Sahrens case PROP_TYPE_STRING: 19924577Sahrens default: 19934577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19944577Sahrens "cannot get non-numeric property")); 19954577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 19964577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 19974577Sahrens } 1998789Sahrens } 1999789Sahrens 2000789Sahrens return (0); 2001789Sahrens } 2002789Sahrens 2003789Sahrens /* 2004789Sahrens * Calculate the source type, given the raw source string. 2005789Sahrens */ 2006789Sahrens static void 20075094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2008789Sahrens char *statbuf, size_t statlen) 2009789Sahrens { 20105094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2011789Sahrens return; 2012789Sahrens 2013789Sahrens if (source == NULL) { 20145094Slling *srctype = ZPROP_SRC_NONE; 2015789Sahrens } else if (source[0] == '\0') { 20165094Slling *srctype = ZPROP_SRC_DEFAULT; 2017789Sahrens } else { 2018789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 20195094Slling *srctype = ZPROP_SRC_LOCAL; 2020789Sahrens } else { 2021789Sahrens (void) strlcpy(statbuf, source, statlen); 20225094Slling *srctype = ZPROP_SRC_INHERITED; 2023789Sahrens } 2024789Sahrens } 2025789Sahrens 2026789Sahrens } 2027789Sahrens 2028789Sahrens /* 2029789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2030789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2031789Sahrens * human-readable form. 2032789Sahrens * 2033789Sahrens * Returns 0 on success, or -1 on error. 2034789Sahrens */ 2035789Sahrens int 2036789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 20375094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2038789Sahrens { 2039789Sahrens char *source = NULL; 2040789Sahrens uint64_t val; 2041789Sahrens char *str; 2042789Sahrens const char *root; 20432676Seschrock const char *strval; 2044789Sahrens 2045789Sahrens /* 2046789Sahrens * Check to see if this property applies to our object 2047789Sahrens */ 2048789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2049789Sahrens return (-1); 2050789Sahrens 2051789Sahrens if (src) 20525094Slling *src = ZPROP_SRC_NONE; 2053789Sahrens 2054789Sahrens switch (prop) { 2055789Sahrens case ZFS_PROP_CREATION: 2056789Sahrens /* 2057789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2058789Sahrens * this into a string unless 'literal' is specified. 2059789Sahrens */ 2060789Sahrens { 20612885Sahrens val = getprop_uint64(zhp, prop, &source); 20622885Sahrens time_t time = (time_t)val; 2063789Sahrens struct tm t; 2064789Sahrens 2065789Sahrens if (literal || 2066789Sahrens localtime_r(&time, &t) == NULL || 2067789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2068789Sahrens &t) == 0) 20692885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2070789Sahrens } 2071789Sahrens break; 2072789Sahrens 2073789Sahrens case ZFS_PROP_MOUNTPOINT: 2074789Sahrens /* 2075789Sahrens * Getting the precise mountpoint can be tricky. 2076789Sahrens * 2077789Sahrens * - for 'none' or 'legacy', return those values. 2078789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2079789Sahrens * - for inherited mountpoints, we want to take everything 2080789Sahrens * after our ancestor and append it to the inherited value. 2081789Sahrens * 2082789Sahrens * If the pool has an alternate root, we want to prepend that 2083789Sahrens * root to any values we return. 2084789Sahrens */ 20851544Seschrock root = zhp->zfs_root; 20861356Seschrock str = getprop_string(zhp, prop, &source); 20871356Seschrock 20881356Seschrock if (str[0] == '\0') { 2089789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2090789Sahrens root, zhp->zfs_name); 20911356Seschrock } else if (str[0] == '/') { 20921356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2093789Sahrens 2094789Sahrens if (relpath[0] == '/') 2095789Sahrens relpath++; 20961356Seschrock if (str[1] == '\0') 20971356Seschrock str++; 2098789Sahrens 2099789Sahrens if (relpath[0] == '\0') 2100789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 21011356Seschrock root, str); 2102789Sahrens else 2103789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 21041356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2105789Sahrens relpath); 2106789Sahrens } else { 2107789Sahrens /* 'legacy' or 'none' */ 21081356Seschrock (void) strlcpy(propbuf, str, proplen); 2109789Sahrens } 2110789Sahrens 2111789Sahrens break; 2112789Sahrens 2113789Sahrens case ZFS_PROP_ORIGIN: 21142885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2115789Sahrens proplen); 2116789Sahrens /* 2117789Sahrens * If there is no parent at all, return failure to indicate that 2118789Sahrens * it doesn't apply to this dataset. 2119789Sahrens */ 2120789Sahrens if (propbuf[0] == '\0') 2121789Sahrens return (-1); 2122789Sahrens break; 2123789Sahrens 2124789Sahrens case ZFS_PROP_QUOTA: 2125789Sahrens case ZFS_PROP_RESERVATION: 21262082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21272082Seschrock return (-1); 2128789Sahrens 2129789Sahrens /* 2130789Sahrens * If quota or reservation is 0, we translate this into 'none' 2131789Sahrens * (unless literal is set), and indicate that it's the default 2132789Sahrens * value. Otherwise, we print the number nicely and indicate 2133789Sahrens * that its set locally. 2134789Sahrens */ 2135789Sahrens if (val == 0) { 2136789Sahrens if (literal) 2137789Sahrens (void) strlcpy(propbuf, "0", proplen); 2138789Sahrens else 2139789Sahrens (void) strlcpy(propbuf, "none", proplen); 2140789Sahrens } else { 2141789Sahrens if (literal) 21422856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 21433912Slling (u_longlong_t)val); 2144789Sahrens else 2145789Sahrens zfs_nicenum(val, propbuf, proplen); 2146789Sahrens } 2147789Sahrens break; 2148789Sahrens 2149789Sahrens case ZFS_PROP_COMPRESSRATIO: 21502082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21512082Seschrock return (-1); 21522856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 21532856Snd150628 val / 100, (longlong_t)val % 100); 2154789Sahrens break; 2155789Sahrens 2156789Sahrens case ZFS_PROP_TYPE: 2157789Sahrens switch (zhp->zfs_type) { 2158789Sahrens case ZFS_TYPE_FILESYSTEM: 2159789Sahrens str = "filesystem"; 2160789Sahrens break; 2161789Sahrens case ZFS_TYPE_VOLUME: 2162789Sahrens str = "volume"; 2163789Sahrens break; 2164789Sahrens case ZFS_TYPE_SNAPSHOT: 2165789Sahrens str = "snapshot"; 2166789Sahrens break; 2167789Sahrens default: 21682082Seschrock abort(); 2169789Sahrens } 2170789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2171789Sahrens break; 2172789Sahrens 2173789Sahrens case ZFS_PROP_MOUNTED: 2174789Sahrens /* 2175789Sahrens * The 'mounted' property is a pseudo-property that described 2176789Sahrens * whether the filesystem is currently mounted. Even though 2177789Sahrens * it's a boolean value, the typical values of "on" and "off" 2178789Sahrens * don't make sense, so we translate to "yes" and "no". 2179789Sahrens */ 21802082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 21812082Seschrock src, &source, &val) != 0) 21822082Seschrock return (-1); 21832082Seschrock if (val) 2184789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2185789Sahrens else 2186789Sahrens (void) strlcpy(propbuf, "no", proplen); 2187789Sahrens break; 2188789Sahrens 2189789Sahrens case ZFS_PROP_NAME: 2190789Sahrens /* 2191789Sahrens * The 'name' property is a pseudo-property derived from the 2192789Sahrens * dataset name. It is presented as a real property to simplify 2193789Sahrens * consumers. 2194789Sahrens */ 2195789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2196789Sahrens break; 2197789Sahrens 2198789Sahrens default: 21994577Sahrens switch (zfs_prop_get_type(prop)) { 22004787Sahrens case PROP_TYPE_NUMBER: 22014577Sahrens if (get_numeric_property(zhp, prop, src, 22024577Sahrens &source, &val) != 0) 22034577Sahrens return (-1); 22044577Sahrens if (literal) 22054577Sahrens (void) snprintf(propbuf, proplen, "%llu", 22064577Sahrens (u_longlong_t)val); 22074577Sahrens else 22084577Sahrens zfs_nicenum(val, propbuf, proplen); 22094577Sahrens break; 22104577Sahrens 22114787Sahrens case PROP_TYPE_STRING: 22124577Sahrens (void) strlcpy(propbuf, 22134577Sahrens getprop_string(zhp, prop, &source), proplen); 22144577Sahrens break; 22154577Sahrens 22164787Sahrens case PROP_TYPE_INDEX: 22174861Sahrens if (get_numeric_property(zhp, prop, src, 22184861Sahrens &source, &val) != 0) 22194861Sahrens return (-1); 22204861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 22214577Sahrens return (-1); 22224577Sahrens (void) strlcpy(propbuf, strval, proplen); 22234577Sahrens break; 22244577Sahrens 22254577Sahrens default: 22264577Sahrens abort(); 22274577Sahrens } 2228789Sahrens } 2229789Sahrens 2230789Sahrens get_source(zhp, src, source, statbuf, statlen); 2231789Sahrens 2232789Sahrens return (0); 2233789Sahrens } 2234789Sahrens 2235789Sahrens /* 2236789Sahrens * Utility function to get the given numeric property. Does no validation that 2237789Sahrens * the given property is the appropriate type; should only be used with 2238789Sahrens * hard-coded property types. 2239789Sahrens */ 2240789Sahrens uint64_t 2241789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2242789Sahrens { 2243789Sahrens char *source; 22442082Seschrock uint64_t val; 22452082Seschrock 22465367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 22472082Seschrock 22482082Seschrock return (val); 2249789Sahrens } 2250789Sahrens 2251789Sahrens /* 2252789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2253789Sahrens */ 2254789Sahrens int 2255789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 22565094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2257789Sahrens { 2258789Sahrens char *source; 2259789Sahrens 2260789Sahrens /* 2261789Sahrens * Check to see if this property applies to our object 2262789Sahrens */ 22634849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 22643237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 22652082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 22662082Seschrock zfs_prop_to_name(prop))); 22674849Sahrens } 2268789Sahrens 2269789Sahrens if (src) 22705094Slling *src = ZPROP_SRC_NONE; 2271789Sahrens 22722082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 22732082Seschrock return (-1); 2274789Sahrens 2275789Sahrens get_source(zhp, src, source, statbuf, statlen); 2276789Sahrens 2277789Sahrens return (0); 2278789Sahrens } 2279789Sahrens 2280789Sahrens /* 2281789Sahrens * Returns the name of the given zfs handle. 2282789Sahrens */ 2283789Sahrens const char * 2284789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2285789Sahrens { 2286789Sahrens return (zhp->zfs_name); 2287789Sahrens } 2288789Sahrens 2289789Sahrens /* 2290789Sahrens * Returns the type of the given zfs handle. 2291789Sahrens */ 2292789Sahrens zfs_type_t 2293789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2294789Sahrens { 2295789Sahrens return (zhp->zfs_type); 2296789Sahrens } 2297789Sahrens 2298789Sahrens /* 22991356Seschrock * Iterate over all child filesystems 2300789Sahrens */ 2301789Sahrens int 23021356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2303789Sahrens { 2304789Sahrens zfs_cmd_t zc = { 0 }; 2305789Sahrens zfs_handle_t *nzhp; 2306789Sahrens int ret; 2307789Sahrens 23085367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 23095367Sahrens return (0); 23105367Sahrens 2311789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23122082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2313789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2314789Sahrens /* 2315789Sahrens * Ignore private dataset names. 2316789Sahrens */ 2317789Sahrens if (dataset_name_hidden(zc.zc_name)) 2318789Sahrens continue; 2319789Sahrens 2320789Sahrens /* 2321789Sahrens * Silently ignore errors, as the only plausible explanation is 2322789Sahrens * that the pool has since been removed. 2323789Sahrens */ 23242082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23252082Seschrock zc.zc_name)) == NULL) 2326789Sahrens continue; 2327789Sahrens 2328789Sahrens if ((ret = func(nzhp, data)) != 0) 2329789Sahrens return (ret); 2330789Sahrens } 2331789Sahrens 2332789Sahrens /* 2333789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2334789Sahrens * returned, then the underlying dataset has been removed since we 2335789Sahrens * obtained the handle. 2336789Sahrens */ 2337789Sahrens if (errno != ESRCH && errno != ENOENT) 23382082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23392082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2340789Sahrens 23411356Seschrock return (0); 23421356Seschrock } 23431356Seschrock 23441356Seschrock /* 23451356Seschrock * Iterate over all snapshots 23461356Seschrock */ 23471356Seschrock int 23481356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23491356Seschrock { 23501356Seschrock zfs_cmd_t zc = { 0 }; 23511356Seschrock zfs_handle_t *nzhp; 23521356Seschrock int ret; 2353789Sahrens 23545367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 23555367Sahrens return (0); 23565367Sahrens 2357789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23582082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 23592082Seschrock &zc) == 0; 2360789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2361789Sahrens 23622082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23632082Seschrock zc.zc_name)) == NULL) 2364789Sahrens continue; 2365789Sahrens 2366789Sahrens if ((ret = func(nzhp, data)) != 0) 2367789Sahrens return (ret); 2368789Sahrens } 2369789Sahrens 2370789Sahrens /* 2371789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2372789Sahrens * returned, then the underlying dataset has been removed since we 2373789Sahrens * obtained the handle. Silently ignore this case, and return success. 2374789Sahrens */ 2375789Sahrens if (errno != ESRCH && errno != ENOENT) 23762082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23772082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2378789Sahrens 2379789Sahrens return (0); 2380789Sahrens } 2381789Sahrens 2382789Sahrens /* 23831356Seschrock * Iterate over all children, snapshots and filesystems 23841356Seschrock */ 23851356Seschrock int 23861356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23871356Seschrock { 23881356Seschrock int ret; 23891356Seschrock 23901356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 23911356Seschrock return (ret); 23921356Seschrock 23931356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 23941356Seschrock } 23951356Seschrock 23961356Seschrock /* 2397789Sahrens * Given a complete name, return just the portion that refers to the parent. 2398789Sahrens * Can return NULL if this is a pool. 2399789Sahrens */ 2400789Sahrens static int 2401789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2402789Sahrens { 2403789Sahrens char *loc; 2404789Sahrens 2405789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2406789Sahrens return (-1); 2407789Sahrens 2408789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2409789Sahrens buf[loc - path] = '\0'; 2410789Sahrens 2411789Sahrens return (0); 2412789Sahrens } 2413789Sahrens 2414789Sahrens /* 24154490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 24164490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 24174490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 24184490Svb160487 * length of already existing prefix of the given path. We also fetch the 24194490Svb160487 * 'zoned' property, which is used to validate property settings when creating 24204490Svb160487 * new datasets. 2421789Sahrens */ 2422789Sahrens static int 24234490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 24244490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2425789Sahrens { 2426789Sahrens zfs_cmd_t zc = { 0 }; 2427789Sahrens char parent[ZFS_MAXNAMELEN]; 2428789Sahrens char *slash; 24291356Seschrock zfs_handle_t *zhp; 24302082Seschrock char errbuf[1024]; 24312082Seschrock 24322082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 24332082Seschrock path); 2434789Sahrens 2435789Sahrens /* get parent, and check to see if this is just a pool */ 2436789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 24372082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24382082Seschrock "missing dataset name")); 24392082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2440789Sahrens } 2441789Sahrens 2442789Sahrens /* check to see if the pool exists */ 2443789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2444789Sahrens slash = parent + strlen(parent); 2445789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2446789Sahrens zc.zc_name[slash - parent] = '\0'; 24472082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2448789Sahrens errno == ENOENT) { 24492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24502082Seschrock "no such pool '%s'"), zc.zc_name); 24512082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2452789Sahrens } 2453789Sahrens 2454789Sahrens /* check to see if the parent dataset exists */ 24554490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 24564490Svb160487 if (errno == ENOENT && accept_ancestor) { 24574490Svb160487 /* 24584490Svb160487 * Go deeper to find an ancestor, give up on top level. 24594490Svb160487 */ 24604490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 24614490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24624490Svb160487 "no such pool '%s'"), zc.zc_name); 24634490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24644490Svb160487 } 24654490Svb160487 } else if (errno == ENOENT) { 24662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24672082Seschrock "parent does not exist")); 24682082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24694490Svb160487 } else 24702082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2471789Sahrens } 2472789Sahrens 24732676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2474789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 24752676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 24762082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 24771356Seschrock zfs_close(zhp); 2478789Sahrens return (-1); 2479789Sahrens } 2480789Sahrens 2481789Sahrens /* make sure parent is a filesystem */ 24821356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 24832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24842082Seschrock "parent is not a filesystem")); 24852082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 24861356Seschrock zfs_close(zhp); 2487789Sahrens return (-1); 2488789Sahrens } 2489789Sahrens 24901356Seschrock zfs_close(zhp); 24914490Svb160487 if (prefixlen != NULL) 24924490Svb160487 *prefixlen = strlen(parent); 24934490Svb160487 return (0); 24944490Svb160487 } 24954490Svb160487 24964490Svb160487 /* 24974490Svb160487 * Finds whether the dataset of the given type(s) exists. 24984490Svb160487 */ 24994490Svb160487 boolean_t 25004490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 25014490Svb160487 { 25024490Svb160487 zfs_handle_t *zhp; 25034490Svb160487 25045326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 25054490Svb160487 return (B_FALSE); 25064490Svb160487 25074490Svb160487 /* 25084490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 25094490Svb160487 */ 25104490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 25114490Svb160487 int ds_type = zhp->zfs_type; 25124490Svb160487 25134490Svb160487 zfs_close(zhp); 25144490Svb160487 if (types & ds_type) 25154490Svb160487 return (B_TRUE); 25164490Svb160487 } 25174490Svb160487 return (B_FALSE); 25184490Svb160487 } 25194490Svb160487 25204490Svb160487 /* 25215367Sahrens * Given a path to 'target', create all the ancestors between 25225367Sahrens * the prefixlen portion of the path, and the target itself. 25235367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 25245367Sahrens */ 25255367Sahrens int 25265367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 25275367Sahrens { 25285367Sahrens zfs_handle_t *h; 25295367Sahrens char *cp; 25305367Sahrens const char *opname; 25315367Sahrens 25325367Sahrens /* make sure prefix exists */ 25335367Sahrens cp = target + prefixlen; 25345367Sahrens if (*cp != '/') { 25355367Sahrens assert(strchr(cp, '/') == NULL); 25365367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25375367Sahrens } else { 25385367Sahrens *cp = '\0'; 25395367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25405367Sahrens *cp = '/'; 25415367Sahrens } 25425367Sahrens if (h == NULL) 25435367Sahrens return (-1); 25445367Sahrens zfs_close(h); 25455367Sahrens 25465367Sahrens /* 25475367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 25485367Sahrens * up to the prefixlen-long one. 25495367Sahrens */ 25505367Sahrens for (cp = target + prefixlen + 1; 25515367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 25525367Sahrens char *logstr; 25535367Sahrens 25545367Sahrens *cp = '\0'; 25555367Sahrens 25565367Sahrens h = make_dataset_handle(hdl, target); 25575367Sahrens if (h) { 25585367Sahrens /* it already exists, nothing to do here */ 25595367Sahrens zfs_close(h); 25605367Sahrens continue; 25615367Sahrens } 25625367Sahrens 25635367Sahrens logstr = hdl->libzfs_log_str; 25645367Sahrens hdl->libzfs_log_str = NULL; 25655367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 25665367Sahrens NULL) != 0) { 25675367Sahrens hdl->libzfs_log_str = logstr; 25685367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 25695367Sahrens goto ancestorerr; 25705367Sahrens } 25715367Sahrens 25725367Sahrens hdl->libzfs_log_str = logstr; 25735367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 25745367Sahrens if (h == NULL) { 25755367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 25765367Sahrens goto ancestorerr; 25775367Sahrens } 25785367Sahrens 25795367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 25805367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 25815367Sahrens goto ancestorerr; 25825367Sahrens } 25835367Sahrens 25845367Sahrens if (zfs_share(h) != 0) { 25855367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 25865367Sahrens goto ancestorerr; 25875367Sahrens } 25885367Sahrens 25895367Sahrens zfs_close(h); 25905367Sahrens } 25915367Sahrens 25925367Sahrens return (0); 25935367Sahrens 25945367Sahrens ancestorerr: 25955367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25965367Sahrens "failed to %s ancestor '%s'"), opname, target); 25975367Sahrens return (-1); 25985367Sahrens } 25995367Sahrens 26005367Sahrens /* 26014490Svb160487 * Creates non-existing ancestors of the given path. 26024490Svb160487 */ 26034490Svb160487 int 26044490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 26054490Svb160487 { 26064490Svb160487 int prefix; 26074490Svb160487 uint64_t zoned; 26084490Svb160487 char *path_copy; 26094490Svb160487 int rc; 26104490Svb160487 26114490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 26124490Svb160487 return (-1); 26134490Svb160487 26144490Svb160487 if ((path_copy = strdup(path)) != NULL) { 26154490Svb160487 rc = create_parents(hdl, path_copy, prefix); 26164490Svb160487 free(path_copy); 26174490Svb160487 } 26184490Svb160487 if (path_copy == NULL || rc != 0) 26194490Svb160487 return (-1); 26204490Svb160487 2621789Sahrens return (0); 2622789Sahrens } 2623789Sahrens 2624789Sahrens /* 26252676Seschrock * Create a new filesystem or volume. 2626789Sahrens */ 2627789Sahrens int 26282082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 26292676Seschrock nvlist_t *props) 2630789Sahrens { 2631789Sahrens zfs_cmd_t zc = { 0 }; 2632789Sahrens int ret; 2633789Sahrens uint64_t size = 0; 2634789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 26352082Seschrock char errbuf[1024]; 26362676Seschrock uint64_t zoned; 26372082Seschrock 26382082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 26392082Seschrock "cannot create '%s'"), path); 2640789Sahrens 2641789Sahrens /* validate the path, taking care to note the extended error message */ 26425326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 26432082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2644789Sahrens 2645789Sahrens /* validate parents exist */ 26464490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2647789Sahrens return (-1); 2648789Sahrens 2649789Sahrens /* 2650789Sahrens * The failure modes when creating a dataset of a different type over 2651789Sahrens * one that already exists is a little strange. In particular, if you 2652789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2653789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2654789Sahrens * first try to see if the dataset exists. 2655789Sahrens */ 2656789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 26575094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 26582082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26592082Seschrock "dataset already exists")); 26602082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2661789Sahrens } 2662789Sahrens 2663789Sahrens if (type == ZFS_TYPE_VOLUME) 2664789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2665789Sahrens else 2666789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2667789Sahrens 26685094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 26693912Slling zoned, NULL, errbuf)) == 0) 26702676Seschrock return (-1); 26712676Seschrock 2672789Sahrens if (type == ZFS_TYPE_VOLUME) { 26731133Seschrock /* 26741133Seschrock * If we are creating a volume, the size and block size must 26751133Seschrock * satisfy a few restraints. First, the blocksize must be a 26761133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 26771133Seschrock * volsize must be a multiple of the block size, and cannot be 26781133Seschrock * zero. 26791133Seschrock */ 26802676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 26812676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 26822676Seschrock nvlist_free(props); 26832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26842676Seschrock "missing volume size")); 26852676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2686789Sahrens } 2687789Sahrens 26882676Seschrock if ((ret = nvlist_lookup_uint64(props, 26892676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 26902676Seschrock &blocksize)) != 0) { 26912676Seschrock if (ret == ENOENT) { 26922676Seschrock blocksize = zfs_prop_default_numeric( 26932676Seschrock ZFS_PROP_VOLBLOCKSIZE); 26942676Seschrock } else { 26952676Seschrock nvlist_free(props); 26962676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26972676Seschrock "missing volume block size")); 26982676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26992676Seschrock } 27002676Seschrock } 27012676Seschrock 27022676Seschrock if (size == 0) { 27032676Seschrock nvlist_free(props); 27042082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27052676Seschrock "volume size cannot be zero")); 27062676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27071133Seschrock } 27081133Seschrock 27091133Seschrock if (size % blocksize != 0) { 27102676Seschrock nvlist_free(props); 27112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27122676Seschrock "volume size must be a multiple of volume block " 27132676Seschrock "size")); 27142676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27151133Seschrock } 2716789Sahrens } 2717789Sahrens 27185094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 27192676Seschrock return (-1); 27202676Seschrock nvlist_free(props); 27212676Seschrock 2722789Sahrens /* create the dataset */ 27234543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2724789Sahrens 27253912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 27262082Seschrock ret = zvol_create_link(hdl, path); 27273912Slling if (ret) { 27283912Slling (void) zfs_standard_error(hdl, errno, 27293912Slling dgettext(TEXT_DOMAIN, 27303912Slling "Volume successfully created, but device links " 27313912Slling "were not created")); 27323912Slling zcmd_free_nvlists(&zc); 27333912Slling return (-1); 27343912Slling } 27353912Slling } 2736789Sahrens 27372676Seschrock zcmd_free_nvlists(&zc); 27382676Seschrock 2739789Sahrens /* check for failure */ 2740789Sahrens if (ret != 0) { 2741789Sahrens char parent[ZFS_MAXNAMELEN]; 2742789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2743789Sahrens 2744789Sahrens switch (errno) { 2745789Sahrens case ENOENT: 27462082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27472082Seschrock "no such parent '%s'"), parent); 27482082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2749789Sahrens 2750789Sahrens case EINVAL: 27512082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27523413Smmusante "parent '%s' is not a filesystem"), parent); 27532082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2754789Sahrens 2755789Sahrens case EDOM: 27562082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27572676Seschrock "volume block size must be power of 2 from " 27582676Seschrock "%u to %uk"), 2759789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2760789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 27612082Seschrock 27622676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27632082Seschrock 27644603Sahrens case ENOTSUP: 27654603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27664603Sahrens "pool must be upgraded to set this " 27674603Sahrens "property or value")); 27684603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 27694603Sahrens 2770789Sahrens #ifdef _ILP32 2771789Sahrens case EOVERFLOW: 2772789Sahrens /* 2773789Sahrens * This platform can't address a volume this big. 2774789Sahrens */ 27752082Seschrock if (type == ZFS_TYPE_VOLUME) 27762082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 27772082Seschrock errbuf)); 2778789Sahrens #endif 27792082Seschrock /* FALLTHROUGH */ 2780789Sahrens default: 27812082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2782789Sahrens } 2783789Sahrens } 2784789Sahrens 2785789Sahrens return (0); 2786789Sahrens } 2787789Sahrens 2788789Sahrens /* 2789789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2790789Sahrens * isn't mounted, and that there are no active dependents. 2791789Sahrens */ 2792789Sahrens int 2793789Sahrens zfs_destroy(zfs_handle_t *zhp) 2794789Sahrens { 2795789Sahrens zfs_cmd_t zc = { 0 }; 2796789Sahrens 2797789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2798789Sahrens 27992676Seschrock if (ZFS_IS_VOLUME(zhp)) { 28003126Sahl /* 28014543Smarks * If user doesn't have permissions to unshare volume, then 28024543Smarks * abort the request. This would only happen for a 28034543Smarks * non-privileged user. 28043126Sahl */ 28054543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 28064543Smarks return (-1); 28074543Smarks } 28083126Sahl 28092082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2810789Sahrens return (-1); 2811789Sahrens 2812789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2813789Sahrens } else { 2814789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2815789Sahrens } 2816789Sahrens 28174543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 28183237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 28192082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 28202082Seschrock zhp->zfs_name)); 28212199Sahrens } 2822789Sahrens 2823789Sahrens remove_mountpoint(zhp); 2824789Sahrens 2825789Sahrens return (0); 2826789Sahrens } 2827789Sahrens 28282199Sahrens struct destroydata { 28292199Sahrens char *snapname; 28302199Sahrens boolean_t gotone; 28313265Sahrens boolean_t closezhp; 28322199Sahrens }; 28332199Sahrens 28342199Sahrens static int 28352199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 28362199Sahrens { 28372199Sahrens struct destroydata *dd = arg; 28382199Sahrens zfs_handle_t *szhp; 28392199Sahrens char name[ZFS_MAXNAMELEN]; 28403265Sahrens boolean_t closezhp = dd->closezhp; 28413265Sahrens int rv; 28422199Sahrens 28432676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 28442676Seschrock (void) strlcat(name, "@", sizeof (name)); 28452676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 28462199Sahrens 28472199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 28482199Sahrens if (szhp) { 28492199Sahrens dd->gotone = B_TRUE; 28502199Sahrens zfs_close(szhp); 28512199Sahrens } 28522199Sahrens 28532199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 28542199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 28552199Sahrens /* 28562199Sahrens * NB: this is simply a best-effort. We don't want to 28572199Sahrens * return an error, because then we wouldn't visit all 28582199Sahrens * the volumes. 28592199Sahrens */ 28602199Sahrens } 28612199Sahrens 28623265Sahrens dd->closezhp = B_TRUE; 28633265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 28643265Sahrens if (closezhp) 28653265Sahrens zfs_close(zhp); 28663265Sahrens return (rv); 28672199Sahrens } 28682199Sahrens 28692199Sahrens /* 28702199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 28712199Sahrens */ 28722199Sahrens int 28732199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 28742199Sahrens { 28752199Sahrens zfs_cmd_t zc = { 0 }; 28762199Sahrens int ret; 28772199Sahrens struct destroydata dd = { 0 }; 28782199Sahrens 28792199Sahrens dd.snapname = snapname; 28802199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 28812199Sahrens 28822199Sahrens if (!dd.gotone) { 28833237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 28842199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 28852199Sahrens zhp->zfs_name, snapname)); 28862199Sahrens } 28872199Sahrens 28882199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 28892676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 28902199Sahrens 28914543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 28922199Sahrens if (ret != 0) { 28932199Sahrens char errbuf[1024]; 28942199Sahrens 28952199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28962199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 28972199Sahrens 28982199Sahrens switch (errno) { 28992199Sahrens case EEXIST: 29002199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29012199Sahrens "snapshot is cloned")); 29022199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 29032199Sahrens 29042199Sahrens default: 29052199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 29062199Sahrens errbuf)); 29072199Sahrens } 29082199Sahrens } 29092199Sahrens 29102199Sahrens return (0); 29112199Sahrens } 29122199Sahrens 2913789Sahrens /* 2914789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2915789Sahrens */ 2916789Sahrens int 29172676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2918789Sahrens { 2919789Sahrens zfs_cmd_t zc = { 0 }; 2920789Sahrens char parent[ZFS_MAXNAMELEN]; 2921789Sahrens int ret; 29222082Seschrock char errbuf[1024]; 29232082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 29242676Seschrock zfs_type_t type; 29252676Seschrock uint64_t zoned; 2926789Sahrens 2927789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2928789Sahrens 29292082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29302082Seschrock "cannot create '%s'"), target); 29312082Seschrock 2932789Sahrens /* validate the target name */ 29335326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 29342082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2935789Sahrens 2936789Sahrens /* validate parents exist */ 29374490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2938789Sahrens return (-1); 2939789Sahrens 2940789Sahrens (void) parent_name(target, parent, sizeof (parent)); 2941789Sahrens 2942789Sahrens /* do the clone */ 29432676Seschrock if (ZFS_IS_VOLUME(zhp)) { 2944789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 29452744Snn35248 type = ZFS_TYPE_VOLUME; 29462676Seschrock } else { 2947789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 29482744Snn35248 type = ZFS_TYPE_FILESYSTEM; 29492676Seschrock } 29502676Seschrock 29512676Seschrock if (props) { 29525094Slling if ((props = zfs_validate_properties(hdl, type, props, 29533912Slling zoned, zhp, errbuf)) == NULL) 29542676Seschrock return (-1); 29552676Seschrock 29565094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 29572676Seschrock nvlist_free(props); 29582676Seschrock return (-1); 29592676Seschrock } 29602676Seschrock 29612676Seschrock nvlist_free(props); 29622676Seschrock } 2963789Sahrens 2964789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 29652676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 29664543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2967789Sahrens 29682676Seschrock zcmd_free_nvlists(&zc); 29692676Seschrock 2970789Sahrens if (ret != 0) { 2971789Sahrens switch (errno) { 2972789Sahrens 2973789Sahrens case ENOENT: 2974789Sahrens /* 2975789Sahrens * The parent doesn't exist. We should have caught this 2976789Sahrens * above, but there may a race condition that has since 2977789Sahrens * destroyed the parent. 2978789Sahrens * 2979789Sahrens * At this point, we don't know whether it's the source 2980789Sahrens * that doesn't exist anymore, or whether the target 2981789Sahrens * dataset doesn't exist. 2982789Sahrens */ 29832082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29842082Seschrock "no such parent '%s'"), parent); 29852082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 29862082Seschrock 29872082Seschrock case EXDEV: 29882082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29892082Seschrock "source and target pools differ")); 29902082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 29912082Seschrock errbuf)); 29922082Seschrock 29932082Seschrock default: 29942082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 29952082Seschrock errbuf)); 29962082Seschrock } 29972676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 29982082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 29992082Seschrock } 30002082Seschrock 30012082Seschrock return (ret); 30022082Seschrock } 30032082Seschrock 30042082Seschrock typedef struct promote_data { 30052082Seschrock char cb_mountpoint[MAXPATHLEN]; 30062082Seschrock const char *cb_target; 30072082Seschrock const char *cb_errbuf; 30082082Seschrock uint64_t cb_pivot_txg; 30092082Seschrock } promote_data_t; 30102082Seschrock 30112082Seschrock static int 30122082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 30132082Seschrock { 30142082Seschrock promote_data_t *pd = data; 30152082Seschrock zfs_handle_t *szhp; 30162082Seschrock char snapname[MAXPATHLEN]; 30173265Sahrens int rv = 0; 30182082Seschrock 30192082Seschrock /* We don't care about snapshots after the pivot point */ 30203265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 30213265Sahrens zfs_close(zhp); 30222082Seschrock return (0); 30233265Sahrens } 30242082Seschrock 30252417Sahrens /* Remove the device link if it's a zvol. */ 30262676Seschrock if (ZFS_IS_VOLUME(zhp)) 30272417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 30282082Seschrock 30292082Seschrock /* Check for conflicting names */ 30302676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 30312676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 30322082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 30332082Seschrock if (szhp != NULL) { 30342082Seschrock zfs_close(szhp); 30352082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30362082Seschrock "snapshot name '%s' from origin \n" 30372082Seschrock "conflicts with '%s' from target"), 30382082Seschrock zhp->zfs_name, snapname); 30393265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 30402082Seschrock } 30413265Sahrens zfs_close(zhp); 30423265Sahrens return (rv); 30432082Seschrock } 30442082Seschrock 30452417Sahrens static int 30462417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 30472417Sahrens { 30482417Sahrens promote_data_t *pd = data; 30492417Sahrens 30502417Sahrens /* We don't care about snapshots after the pivot point */ 30513265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 30523265Sahrens /* Create the device link if it's a zvol. */ 30533265Sahrens if (ZFS_IS_VOLUME(zhp)) 30543265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 30553265Sahrens } 30563265Sahrens 30573265Sahrens zfs_close(zhp); 30582417Sahrens return (0); 30592417Sahrens } 30602417Sahrens 30612082Seschrock /* 30622082Seschrock * Promotes the given clone fs to be the clone parent. 30632082Seschrock */ 30642082Seschrock int 30652082Seschrock zfs_promote(zfs_handle_t *zhp) 30662082Seschrock { 30672082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 30682082Seschrock zfs_cmd_t zc = { 0 }; 30692082Seschrock char parent[MAXPATHLEN]; 30702082Seschrock char *cp; 30712082Seschrock int ret; 30722082Seschrock zfs_handle_t *pzhp; 30732082Seschrock promote_data_t pd; 30742082Seschrock char errbuf[1024]; 30752082Seschrock 30762082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30772082Seschrock "cannot promote '%s'"), zhp->zfs_name); 30782082Seschrock 30792082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 30802082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30812082Seschrock "snapshots can not be promoted")); 30822082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 30832082Seschrock } 30842082Seschrock 30855367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 30862082Seschrock if (parent[0] == '\0') { 30872082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30882082Seschrock "not a cloned filesystem")); 30892082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 30902082Seschrock } 30912082Seschrock cp = strchr(parent, '@'); 30922082Seschrock *cp = '\0'; 30932082Seschrock 30942082Seschrock /* Walk the snapshots we will be moving */ 30955367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 30962082Seschrock if (pzhp == NULL) 30972082Seschrock return (-1); 30982082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 30992082Seschrock zfs_close(pzhp); 31002082Seschrock pd.cb_target = zhp->zfs_name; 31012082Seschrock pd.cb_errbuf = errbuf; 31025094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 31032082Seschrock if (pzhp == NULL) 31042082Seschrock return (-1); 31052082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 31062082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 31072082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 31082417Sahrens if (ret != 0) { 31092417Sahrens zfs_close(pzhp); 31102082Seschrock return (-1); 31112417Sahrens } 31122082Seschrock 31132082Seschrock /* issue the ioctl */ 31145367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 31152676Seschrock sizeof (zc.zc_value)); 31162082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31174543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 31182082Seschrock 31192082Seschrock if (ret != 0) { 31202417Sahrens int save_errno = errno; 31212417Sahrens 31222417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 31232417Sahrens zfs_close(pzhp); 31242417Sahrens 31252417Sahrens switch (save_errno) { 3126789Sahrens case EEXIST: 3127789Sahrens /* 31282082Seschrock * There is a conflicting snapshot name. We 31292082Seschrock * should have caught this above, but they could 31302082Seschrock * have renamed something in the mean time. 3131789Sahrens */ 31322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31332082Seschrock "conflicting snapshot name from parent '%s'"), 31342082Seschrock parent); 31352082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3136789Sahrens 3137789Sahrens default: 31382417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3139789Sahrens } 31402417Sahrens } else { 31412417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3142789Sahrens } 3143789Sahrens 31442417Sahrens zfs_close(pzhp); 3145789Sahrens return (ret); 3146789Sahrens } 3147789Sahrens 31484007Smmusante struct createdata { 31494007Smmusante const char *cd_snapname; 31504007Smmusante int cd_ifexists; 31514007Smmusante }; 31524007Smmusante 31532199Sahrens static int 31542199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 31552199Sahrens { 31564007Smmusante struct createdata *cd = arg; 31572676Seschrock int ret; 31582199Sahrens 31592199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 31602199Sahrens char name[MAXPATHLEN]; 31612199Sahrens 31622676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 31632676Seschrock (void) strlcat(name, "@", sizeof (name)); 31644007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 31654007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 31664007Smmusante cd->cd_ifexists); 31672199Sahrens /* 31682199Sahrens * NB: this is simply a best-effort. We don't want to 31692199Sahrens * return an error, because then we wouldn't visit all 31702199Sahrens * the volumes. 31712199Sahrens */ 31722199Sahrens } 31732676Seschrock 31744007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 31752676Seschrock 31762676Seschrock zfs_close(zhp); 31772676Seschrock 31782676Seschrock return (ret); 31792199Sahrens } 31802199Sahrens 3181789Sahrens /* 31823504Sahl * Takes a snapshot of the given dataset. 3183789Sahrens */ 3184789Sahrens int 31852199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3186789Sahrens { 3187789Sahrens const char *delim; 3188789Sahrens char *parent; 3189789Sahrens zfs_handle_t *zhp; 3190789Sahrens zfs_cmd_t zc = { 0 }; 3191789Sahrens int ret; 31922082Seschrock char errbuf[1024]; 31932082Seschrock 31942082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31952082Seschrock "cannot snapshot '%s'"), path); 31962082Seschrock 31972082Seschrock /* validate the target name */ 31985326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 31992082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3200789Sahrens 3201789Sahrens /* make sure the parent exists and is of the appropriate type */ 32022199Sahrens delim = strchr(path, '@'); 32032082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 32042082Seschrock return (-1); 3205789Sahrens (void) strncpy(parent, path, delim - path); 3206789Sahrens parent[delim - path] = '\0'; 3207789Sahrens 32082082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3209789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3210789Sahrens free(parent); 3211789Sahrens return (-1); 3212789Sahrens } 3213789Sahrens 32142199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 32152676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 32164543Smarks if (ZFS_IS_VOLUME(zhp)) 32174543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 32184543Smarks else 32194543Smarks zc.zc_objset_type = DMU_OST_ZFS; 32202199Sahrens zc.zc_cookie = recursive; 32214543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 32222199Sahrens 32232199Sahrens /* 32242199Sahrens * if it was recursive, the one that actually failed will be in 32252199Sahrens * zc.zc_name. 32262199Sahrens */ 32274543Smarks if (ret != 0) 32284543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32294543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 32304543Smarks 32312199Sahrens if (ret == 0 && recursive) { 32324007Smmusante struct createdata cd; 32334007Smmusante 32344007Smmusante cd.cd_snapname = delim + 1; 32354007Smmusante cd.cd_ifexists = B_FALSE; 32364007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 32372199Sahrens } 3238789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 32392082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 32402199Sahrens if (ret != 0) { 32414543Smarks (void) zfs_standard_error(hdl, errno, 32424543Smarks dgettext(TEXT_DOMAIN, 32434543Smarks "Volume successfully snapshotted, but device links " 32444543Smarks "were not created")); 32454543Smarks free(parent); 32464543Smarks zfs_close(zhp); 32474543Smarks return (-1); 32482199Sahrens } 3249789Sahrens } 3250789Sahrens 32512082Seschrock if (ret != 0) 32522082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3253789Sahrens 3254789Sahrens free(parent); 3255789Sahrens zfs_close(zhp); 3256789Sahrens 3257789Sahrens return (ret); 3258789Sahrens } 3259789Sahrens 3260789Sahrens /* 32611294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 32621294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 32631294Slling * is a dependent and we should just destroy it without checking the transaction 32641294Slling * group. 3265789Sahrens */ 32661294Slling typedef struct rollback_data { 32671294Slling const char *cb_target; /* the snapshot */ 32681294Slling uint64_t cb_create; /* creation time reference */ 32691294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 32701294Slling int cb_error; 32712082Seschrock boolean_t cb_dependent; 32721294Slling } rollback_data_t; 32731294Slling 32741294Slling static int 32751294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 32761294Slling { 32771294Slling rollback_data_t *cbp = data; 32781294Slling 32791294Slling if (!cbp->cb_dependent) { 32801294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 32811294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 32821294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 32831294Slling cbp->cb_create) { 32844543Smarks char *logstr; 32851294Slling 32862082Seschrock cbp->cb_dependent = B_TRUE; 32872474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 32882474Seschrock cbp) != 0) 32892474Seschrock cbp->cb_error = 1; 32902082Seschrock cbp->cb_dependent = B_FALSE; 32911294Slling 32924543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 32934543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 32941294Slling if (zfs_destroy(zhp) != 0) 32951294Slling cbp->cb_error = 1; 32961294Slling else 32975367Sahrens changelist_remove(cbp->cb_clp, zhp->zfs_name); 32984543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 32991294Slling } 33001294Slling } else { 33011294Slling if (zfs_destroy(zhp) != 0) 33021294Slling cbp->cb_error = 1; 33031294Slling else 33045367Sahrens changelist_remove(cbp->cb_clp, zhp->zfs_name); 33051294Slling } 33061294Slling 33071294Slling zfs_close(zhp); 33081294Slling return (0); 33091294Slling } 33101294Slling 33111294Slling /* 33121294Slling * Rollback the dataset to its latest snapshot. 33131294Slling */ 33141294Slling static int 33151294Slling do_rollback(zfs_handle_t *zhp) 3316789Sahrens { 3317789Sahrens int ret; 3318789Sahrens zfs_cmd_t zc = { 0 }; 3319789Sahrens 3320789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3321789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3322789Sahrens 3323789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 33242082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3325789Sahrens return (-1); 3326789Sahrens 3327789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3328789Sahrens 33292676Seschrock if (ZFS_IS_VOLUME(zhp)) 3330789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3331789Sahrens else 3332789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3333789Sahrens 3334789Sahrens /* 3335789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3336789Sahrens * for the given dataset. Given these constraints, we can simply pass 3337789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3338789Sahrens * condition where the user has taken a snapshot since we verified that 3339789Sahrens * this was the most recent. 3340789Sahrens */ 33414543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 33423237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 33432082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 33442082Seschrock zhp->zfs_name); 3345789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33462082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3347789Sahrens } 3348789Sahrens 3349789Sahrens return (ret); 3350789Sahrens } 3351789Sahrens 3352789Sahrens /* 33531294Slling * Given a dataset, rollback to a specific snapshot, discarding any 33541294Slling * data changes since then and making it the active dataset. 33551294Slling * 33561294Slling * Any snapshots more recent than the target are destroyed, along with 33571294Slling * their dependents. 33581294Slling */ 33591294Slling int 33601294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 33611294Slling { 33621294Slling int ret; 33631294Slling rollback_data_t cb = { 0 }; 33641294Slling prop_changelist_t *clp; 33651294Slling 33661294Slling /* 33671294Slling * Unmount all dependendents of the dataset and the dataset itself. 33681294Slling * The list we need to gather is the same as for doing rename 33691294Slling */ 33701294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 33711294Slling if (clp == NULL) 33721294Slling return (-1); 33731294Slling 33741294Slling if ((ret = changelist_prefix(clp)) != 0) 33751294Slling goto out; 33761294Slling 33771294Slling /* 33781294Slling * Destroy all recent snapshots and its dependends. 33791294Slling */ 33801294Slling cb.cb_target = snap->zfs_name; 33811294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 33821294Slling cb.cb_clp = clp; 33831294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 33841294Slling 33851294Slling if ((ret = cb.cb_error) != 0) { 33861294Slling (void) changelist_postfix(clp); 33871294Slling goto out; 33881294Slling } 33891294Slling 33901294Slling /* 33911294Slling * Now that we have verified that the snapshot is the latest, 33921294Slling * rollback to the given snapshot. 33931294Slling */ 33941294Slling ret = do_rollback(zhp); 33951294Slling 33961294Slling if (ret != 0) { 33971294Slling (void) changelist_postfix(clp); 33981294Slling goto out; 33991294Slling } 34001294Slling 34011294Slling /* 34021294Slling * We only want to re-mount the filesystem if it was mounted in the 34031294Slling * first place. 34041294Slling */ 34051294Slling ret = changelist_postfix(clp); 34061294Slling 34071294Slling out: 34081294Slling changelist_free(clp); 34091294Slling return (ret); 34101294Slling } 34111294Slling 34121294Slling /* 3413789Sahrens * Iterate over all dependents for a given dataset. This includes both 3414789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3415789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3416789Sahrens * libzfs_graph.c. 3417789Sahrens */ 3418789Sahrens int 34192474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 34202474Seschrock zfs_iter_f func, void *data) 3421789Sahrens { 3422789Sahrens char **dependents; 3423789Sahrens size_t count; 3424789Sahrens int i; 3425789Sahrens zfs_handle_t *child; 3426789Sahrens int ret = 0; 3427789Sahrens 34282474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 34292474Seschrock &dependents, &count) != 0) 34302474Seschrock return (-1); 34312474Seschrock 3432789Sahrens for (i = 0; i < count; i++) { 34332082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 34342082Seschrock dependents[i])) == NULL) 3435789Sahrens continue; 3436789Sahrens 3437789Sahrens if ((ret = func(child, data)) != 0) 3438789Sahrens break; 3439789Sahrens } 3440789Sahrens 3441789Sahrens for (i = 0; i < count; i++) 3442789Sahrens free(dependents[i]); 3443789Sahrens free(dependents); 3444789Sahrens 3445789Sahrens return (ret); 3446789Sahrens } 3447789Sahrens 3448789Sahrens /* 3449789Sahrens * Renames the given dataset. 3450789Sahrens */ 3451789Sahrens int 34524490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3453789Sahrens { 3454789Sahrens int ret; 3455789Sahrens zfs_cmd_t zc = { 0 }; 3456789Sahrens char *delim; 34574007Smmusante prop_changelist_t *cl = NULL; 34584007Smmusante zfs_handle_t *zhrp = NULL; 34594007Smmusante char *parentname = NULL; 3460789Sahrens char parent[ZFS_MAXNAMELEN]; 34612082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 34622082Seschrock char errbuf[1024]; 3463789Sahrens 3464789Sahrens /* if we have the same exact name, just return success */ 3465789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3466789Sahrens return (0); 3467789Sahrens 34682082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34692082Seschrock "cannot rename to '%s'"), target); 34702082Seschrock 3471789Sahrens /* 3472789Sahrens * Make sure the target name is valid 3473789Sahrens */ 3474789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 34752665Snd150628 if ((strchr(target, '@') == NULL) || 34762665Snd150628 *target == '@') { 34772665Snd150628 /* 34782665Snd150628 * Snapshot target name is abbreviated, 34792665Snd150628 * reconstruct full dataset name 34802665Snd150628 */ 34812665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 34822665Snd150628 sizeof (parent)); 34832665Snd150628 delim = strchr(parent, '@'); 34842665Snd150628 if (strchr(target, '@') == NULL) 34852665Snd150628 *(++delim) = '\0'; 34862665Snd150628 else 34872665Snd150628 *delim = '\0'; 34882665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 34892665Snd150628 target = parent; 34902665Snd150628 } else { 34912665Snd150628 /* 34922665Snd150628 * Make sure we're renaming within the same dataset. 34932665Snd150628 */ 34942665Snd150628 delim = strchr(target, '@'); 34952665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 34962665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 34972665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34982665Snd150628 "snapshots must be part of same " 34992665Snd150628 "dataset")); 35002665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 35013912Slling errbuf)); 35022665Snd150628 } 3503789Sahrens } 35045326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 35052665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3506789Sahrens } else { 35074007Smmusante if (recursive) { 35084007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35094007Smmusante "recursive rename must be a snapshot")); 35104007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 35114007Smmusante } 35124007Smmusante 35135326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 35142665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 35152676Seschrock uint64_t unused; 35162676Seschrock 3517789Sahrens /* validate parents */ 35184490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3519789Sahrens return (-1); 3520789Sahrens 3521789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3522789Sahrens 3523789Sahrens /* make sure we're in the same pool */ 3524789Sahrens verify((delim = strchr(target, '/')) != NULL); 3525789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3526789Sahrens zhp->zfs_name[delim - target] != '/') { 35272082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35282082Seschrock "datasets must be within same pool")); 35292082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3530789Sahrens } 35312440Snd150628 35322440Snd150628 /* new name cannot be a child of the current dataset name */ 35332440Snd150628 if (strncmp(parent, zhp->zfs_name, 35343912Slling strlen(zhp->zfs_name)) == 0) { 35352440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35362440Snd150628 "New dataset name cannot be a descendent of " 35372440Snd150628 "current dataset name")); 35382440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 35392440Snd150628 } 3540789Sahrens } 3541789Sahrens 35422082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 35432082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 35442082Seschrock 3545789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3546789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 35472082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35482082Seschrock "dataset is used in a non-global zone")); 35492082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3550789Sahrens } 3551789Sahrens 35524007Smmusante if (recursive) { 35534007Smmusante struct destroydata dd; 35544007Smmusante 35554183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 35564183Smmusante if (parentname == NULL) { 35574183Smmusante ret = -1; 35584183Smmusante goto error; 35594183Smmusante } 35604007Smmusante delim = strchr(parentname, '@'); 35614007Smmusante *delim = '\0'; 35625094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 35634007Smmusante if (zhrp == NULL) { 35644183Smmusante ret = -1; 35654183Smmusante goto error; 35664007Smmusante } 35674007Smmusante 35684007Smmusante dd.snapname = delim + 1; 35694007Smmusante dd.gotone = B_FALSE; 35704183Smmusante dd.closezhp = B_TRUE; 35714007Smmusante 35724007Smmusante /* We remove any zvol links prior to renaming them */ 35734007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 35744007Smmusante if (ret) { 35754007Smmusante goto error; 35764007Smmusante } 35774007Smmusante } else { 35784007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 35794007Smmusante return (-1); 35804007Smmusante 35814007Smmusante if (changelist_haszonedchild(cl)) { 35824007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35834007Smmusante "child dataset with inherited mountpoint is used " 35844007Smmusante "in a non-global zone")); 35854007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 35864007Smmusante goto error; 35874007Smmusante } 35884007Smmusante 35894007Smmusante if ((ret = changelist_prefix(cl)) != 0) 35904007Smmusante goto error; 3591789Sahrens } 3592789Sahrens 35932676Seschrock if (ZFS_IS_VOLUME(zhp)) 3594789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3595789Sahrens else 3596789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3597789Sahrens 35982665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35992676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 36002665Snd150628 36014007Smmusante zc.zc_cookie = recursive; 36024007Smmusante 36034543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 36044007Smmusante /* 36054007Smmusante * if it was recursive, the one that actually failed will 36064007Smmusante * be in zc.zc_name 36074007Smmusante */ 36084007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36095367Sahrens "cannot rename '%s'"), zc.zc_name); 36104007Smmusante 36114007Smmusante if (recursive && errno == EEXIST) { 36124007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36134007Smmusante "a child dataset already has a snapshot " 36144007Smmusante "with the new name")); 36154801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 36164007Smmusante } else { 36174007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 36184007Smmusante } 3619789Sahrens 3620789Sahrens /* 3621789Sahrens * On failure, we still want to remount any filesystems that 3622789Sahrens * were previously mounted, so we don't alter the system state. 3623789Sahrens */ 36244007Smmusante if (recursive) { 36254007Smmusante struct createdata cd; 36264007Smmusante 36274007Smmusante /* only create links for datasets that had existed */ 36284007Smmusante cd.cd_snapname = delim + 1; 36294007Smmusante cd.cd_ifexists = B_TRUE; 36304007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36314007Smmusante &cd); 36324007Smmusante } else { 36334007Smmusante (void) changelist_postfix(cl); 36344007Smmusante } 3635789Sahrens } else { 36364007Smmusante if (recursive) { 36374007Smmusante struct createdata cd; 36384007Smmusante 36394007Smmusante /* only create links for datasets that had existed */ 36404007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 36414007Smmusante cd.cd_ifexists = B_TRUE; 36424007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36434007Smmusante &cd); 36444007Smmusante } else { 36454007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 36464007Smmusante ret = changelist_postfix(cl); 36474007Smmusante } 3648789Sahrens } 3649789Sahrens 3650789Sahrens error: 36514007Smmusante if (parentname) { 36524007Smmusante free(parentname); 36534007Smmusante } 36544007Smmusante if (zhrp) { 36554007Smmusante zfs_close(zhrp); 36564007Smmusante } 36574007Smmusante if (cl) { 36584007Smmusante changelist_free(cl); 36594007Smmusante } 3660789Sahrens return (ret); 3661789Sahrens } 3662789Sahrens 3663789Sahrens /* 3664789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3665789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3666789Sahrens */ 3667789Sahrens int 36682082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3669789Sahrens { 36704007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 36714007Smmusante } 36724007Smmusante 36734007Smmusante static int 36744007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 36754007Smmusante { 3676789Sahrens zfs_cmd_t zc = { 0 }; 36772082Seschrock di_devlink_handle_t dhdl; 36784543Smarks priv_set_t *priv_effective; 36794543Smarks int privileged; 3680789Sahrens 3681789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3682789Sahrens 3683789Sahrens /* 3684789Sahrens * Issue the appropriate ioctl. 3685789Sahrens */ 36862082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3687789Sahrens switch (errno) { 3688789Sahrens case EEXIST: 3689789Sahrens /* 3690789Sahrens * Silently ignore the case where the link already 3691789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3692789Sahrens * times without errors. 3693789Sahrens */ 3694789Sahrens return (0); 3695789Sahrens 36964007Smmusante case ENOENT: 36974007Smmusante /* 36984007Smmusante * Dataset does not exist in the kernel. If we 36994007Smmusante * don't care (see zfs_rename), then ignore the 37004007Smmusante * error quietly. 37014007Smmusante */ 37024007Smmusante if (ifexists) { 37034007Smmusante return (0); 37044007Smmusante } 37054007Smmusante 37064007Smmusante /* FALLTHROUGH */ 37074007Smmusante 3708789Sahrens default: 37093237Slling return (zfs_standard_error_fmt(hdl, errno, 37102082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 37112082Seschrock "for '%s'"), dataset)); 3712789Sahrens } 3713789Sahrens } 3714789Sahrens 3715789Sahrens /* 37164543Smarks * If privileged call devfsadm and wait for the links to 37174543Smarks * magically appear. 37184543Smarks * Otherwise, print out an informational message. 3719789Sahrens */ 37204543Smarks 37214543Smarks priv_effective = priv_allocset(); 37224543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 37234543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 37244543Smarks priv_freeset(priv_effective); 37254543Smarks 37264543Smarks if (privileged) { 37274543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 37284543Smarks DI_MAKE_LINK)) == NULL) { 37294543Smarks zfs_error_aux(hdl, strerror(errno)); 37304543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 37314543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 37324543Smarks "for '%s'"), dataset); 37334543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 37344543Smarks return (-1); 37354543Smarks } else { 37364543Smarks (void) di_devlink_fini(&dhdl); 37374543Smarks } 3738789Sahrens } else { 37394543Smarks char pathname[MAXPATHLEN]; 37404543Smarks struct stat64 statbuf; 37414543Smarks int i; 37424543Smarks 37434543Smarks #define MAX_WAIT 10 37444543Smarks 37454543Smarks /* 37464543Smarks * This is the poor mans way of waiting for the link 37474543Smarks * to show up. If after 10 seconds we still don't 37484543Smarks * have it, then print out a message. 37494543Smarks */ 37504543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 37514543Smarks dataset); 37524543Smarks 37534543Smarks for (i = 0; i != MAX_WAIT; i++) { 37544543Smarks if (stat64(pathname, &statbuf) == 0) 37554543Smarks break; 37564543Smarks (void) sleep(1); 37574543Smarks } 37584543Smarks if (i == MAX_WAIT) 37594543Smarks (void) printf(gettext("%s may not be immediately " 37604543Smarks "available\n"), pathname); 3761789Sahrens } 3762789Sahrens 3763789Sahrens return (0); 3764789Sahrens } 3765789Sahrens 3766789Sahrens /* 3767789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 3768789Sahrens */ 3769789Sahrens int 37702082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3771789Sahrens { 3772789Sahrens zfs_cmd_t zc = { 0 }; 3773789Sahrens 3774789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3775789Sahrens 37762082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3777789Sahrens switch (errno) { 3778789Sahrens case ENXIO: 3779789Sahrens /* 3780789Sahrens * Silently ignore the case where the link no longer 3781789Sahrens * exists, so that 'zfs volfini' can be run multiple 3782789Sahrens * times without errors. 3783789Sahrens */ 3784789Sahrens return (0); 3785789Sahrens 3786789Sahrens default: 37873237Slling return (zfs_standard_error_fmt(hdl, errno, 37882082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 37892082Seschrock "links for '%s'"), dataset)); 3790789Sahrens } 3791789Sahrens } 3792789Sahrens 3793789Sahrens return (0); 3794789Sahrens } 37952676Seschrock 37962676Seschrock nvlist_t * 37972676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 37982676Seschrock { 37992676Seschrock return (zhp->zfs_user_props); 38002676Seschrock } 38012676Seschrock 38022676Seschrock /* 38033912Slling * This function is used by 'zfs list' to determine the exact set of columns to 38043912Slling * display, and their maximum widths. This does two main things: 38053912Slling * 38063912Slling * - If this is a list of all properties, then expand the list to include 38073912Slling * all native properties, and set a flag so that for each dataset we look 38083912Slling * for new unique user properties and add them to the list. 38093912Slling * 38103912Slling * - For non fixed-width properties, keep track of the maximum width seen 38113912Slling * so that we can size the column appropriately. 38123912Slling */ 38133912Slling int 38145094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 38153912Slling { 38163912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 38175094Slling zprop_list_t *entry; 38185094Slling zprop_list_t **last, **start; 38193912Slling nvlist_t *userprops, *propval; 38203912Slling nvpair_t *elem; 38213912Slling char *strval; 38223912Slling char buf[ZFS_MAXPROPLEN]; 38233912Slling 38245094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 38253912Slling return (-1); 38262676Seschrock 38272676Seschrock userprops = zfs_get_user_props(zhp); 38282676Seschrock 38292676Seschrock entry = *plp; 38302676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 38312676Seschrock /* 38322676Seschrock * Go through and add any user properties as necessary. We 38332676Seschrock * start by incrementing our list pointer to the first 38342676Seschrock * non-native property. 38352676Seschrock */ 38362676Seschrock start = plp; 38372676Seschrock while (*start != NULL) { 38385094Slling if ((*start)->pl_prop == ZPROP_INVAL) 38392676Seschrock break; 38402676Seschrock start = &(*start)->pl_next; 38412676Seschrock } 38422676Seschrock 38432676Seschrock elem = NULL; 38442676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 38452676Seschrock /* 38462676Seschrock * See if we've already found this property in our list. 38472676Seschrock */ 38482676Seschrock for (last = start; *last != NULL; 38492676Seschrock last = &(*last)->pl_next) { 38502676Seschrock if (strcmp((*last)->pl_user_prop, 38512676Seschrock nvpair_name(elem)) == 0) 38522676Seschrock break; 38532676Seschrock } 38542676Seschrock 38552676Seschrock if (*last == NULL) { 38562676Seschrock if ((entry = zfs_alloc(hdl, 38575094Slling sizeof (zprop_list_t))) == NULL || 38582676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 38592676Seschrock nvpair_name(elem)))) == NULL) { 38602676Seschrock free(entry); 38612676Seschrock return (-1); 38622676Seschrock } 38632676Seschrock 38645094Slling entry->pl_prop = ZPROP_INVAL; 38652676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 38662676Seschrock entry->pl_all = B_TRUE; 38672676Seschrock *last = entry; 38682676Seschrock } 38692676Seschrock } 38702676Seschrock } 38712676Seschrock 38722676Seschrock /* 38732676Seschrock * Now go through and check the width of any non-fixed columns 38742676Seschrock */ 38752676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 38762676Seschrock if (entry->pl_fixed) 38772676Seschrock continue; 38782676Seschrock 38795094Slling if (entry->pl_prop != ZPROP_INVAL) { 38802676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 38812676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 38822676Seschrock if (strlen(buf) > entry->pl_width) 38832676Seschrock entry->pl_width = strlen(buf); 38842676Seschrock } 38852676Seschrock } else if (nvlist_lookup_nvlist(userprops, 38862676Seschrock entry->pl_user_prop, &propval) == 0) { 38872676Seschrock verify(nvlist_lookup_string(propval, 38885094Slling ZPROP_VALUE, &strval) == 0); 38892676Seschrock if (strlen(strval) > entry->pl_width) 38902676Seschrock entry->pl_width = strlen(strval); 38912676Seschrock } 38922676Seschrock } 38932676Seschrock 38942676Seschrock return (0); 38952676Seschrock } 38964543Smarks 38974543Smarks int 38984543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 38994543Smarks { 39004543Smarks zfs_cmd_t zc = { 0 }; 39014543Smarks nvlist_t *nvp; 39024543Smarks gid_t gid; 39034543Smarks uid_t uid; 39044543Smarks const gid_t *groups; 39054543Smarks int group_cnt; 39064543Smarks int error; 39074543Smarks 39084543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 39094543Smarks return (no_memory(hdl)); 39104543Smarks 39114543Smarks uid = ucred_geteuid(cred); 39124543Smarks gid = ucred_getegid(cred); 39134543Smarks group_cnt = ucred_getgroups(cred, &groups); 39144543Smarks 39154543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 39164543Smarks return (1); 39174543Smarks 39184543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 39194543Smarks nvlist_free(nvp); 39204543Smarks return (1); 39214543Smarks } 39224543Smarks 39234543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 39244543Smarks nvlist_free(nvp); 39254543Smarks return (1); 39264543Smarks } 39274543Smarks 39284543Smarks if (nvlist_add_uint32_array(nvp, 39294543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 39304543Smarks nvlist_free(nvp); 39314543Smarks return (1); 39324543Smarks } 39334543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39344543Smarks 39355094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 39364543Smarks return (-1); 39374543Smarks 39384543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 39394543Smarks nvlist_free(nvp); 39404543Smarks return (error); 39414543Smarks } 39424543Smarks 39434543Smarks int 39444543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 39455331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 39464543Smarks { 39474543Smarks zfs_cmd_t zc = { 0 }; 39484543Smarks int error; 39494543Smarks 39504543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39514543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 39524543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 39534543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 39545331Samw zc.zc_share.z_sharetype = operation; 39554543Smarks zc.zc_share.z_sharemax = sharemax; 39564543Smarks 39574543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 39584543Smarks return (error); 39594543Smarks } 3960