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> 39*5367Sahrens #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 } 385*5367Sahrens /* If we can successfully roll it back, reget the stats */ 386*5367Sahrens if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 387*5367Sahrens 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; 4835331Samw int chosen_normal = -1; 4845331Samw int chosen_utf = -1; 4852676Seschrock 4862676Seschrock if (type == ZFS_TYPE_SNAPSHOT) { 4872676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4883413Smmusante "snapshot properties cannot be modified")); 4892676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 4905094Slling return (NULL); 4915094Slling } 4925094Slling 4935094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 4945094Slling (void) no_memory(hdl); 4955094Slling return (NULL); 496789Sahrens } 497789Sahrens 4982676Seschrock elem = NULL; 4992676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 5005094Slling const char *propname = nvpair_name(elem); 5012676Seschrock 5022676Seschrock /* 5032676Seschrock * Make sure this property is valid and applies to this type. 5042676Seschrock */ 5055094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 5065094Slling if (!zfs_prop_user(propname)) { 5072676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5085094Slling "invalid property '%s'"), propname); 5095094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5105094Slling goto error; 5115094Slling } 5125094Slling 5135094Slling /* 5145094Slling * If this is a user property, make sure it's a 5155094Slling * string, and that it's less than ZAP_MAXNAMELEN. 5165094Slling */ 5175094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 5185094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5195094Slling "'%s' must be a string"), propname); 5205094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5215094Slling goto error; 5225094Slling } 5235094Slling 5245094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 5255094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5265094Slling "property name '%s' is too long"), 5272676Seschrock propname); 5282676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5292676Seschrock goto error; 5302676Seschrock } 5312676Seschrock 5322676Seschrock (void) nvpair_value_string(elem, &strval); 5332676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 5342676Seschrock (void) no_memory(hdl); 5352676Seschrock goto error; 5362676Seschrock } 5372676Seschrock continue; 538789Sahrens } 5392676Seschrock 5402676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 5412676Seschrock zfs_error_aux(hdl, 5422676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 5432676Seschrock "apply to datasets of this type"), propname); 5442676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 5452676Seschrock goto error; 5462676Seschrock } 5472676Seschrock 5482676Seschrock if (zfs_prop_readonly(prop) && 5495331Samw (!zfs_prop_setonce(prop) || zhp != NULL)) { 5502676Seschrock zfs_error_aux(hdl, 5512676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 5522676Seschrock propname); 5532676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 5542676Seschrock goto error; 5552676Seschrock } 5562676Seschrock 5575094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 5585094Slling &strval, &intval, errbuf) != 0) 5592676Seschrock goto error; 5602676Seschrock 5612676Seschrock /* 5622676Seschrock * Perform some additional checks for specific properties. 5632676Seschrock */ 5642676Seschrock switch (prop) { 5654577Sahrens case ZFS_PROP_VERSION: 5664577Sahrens { 5674577Sahrens int version; 5684577Sahrens 5694577Sahrens if (zhp == NULL) 5704577Sahrens break; 5714577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 5724577Sahrens if (intval < version) { 5734577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5744577Sahrens "Can not downgrade; already at version %u"), 5754577Sahrens version); 5764577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5774577Sahrens goto error; 5784577Sahrens } 5794577Sahrens break; 5804577Sahrens } 5814577Sahrens 5822676Seschrock case ZFS_PROP_RECORDSIZE: 5832676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 5842676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 5852676Seschrock if (intval < SPA_MINBLOCKSIZE || 5862676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 5872082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5882676Seschrock "'%s' must be power of 2 from %u " 5892676Seschrock "to %uk"), propname, 5902676Seschrock (uint_t)SPA_MINBLOCKSIZE, 5912676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 5922676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5932676Seschrock goto error; 594789Sahrens } 595789Sahrens break; 596789Sahrens 5973126Sahl case ZFS_PROP_SHAREISCSI: 5983126Sahl if (strcmp(strval, "off") != 0 && 5993126Sahl strcmp(strval, "on") != 0 && 6003126Sahl strcmp(strval, "type=disk") != 0) { 6013126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6023126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 6033126Sahl propname); 6043126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6053126Sahl goto error; 6063126Sahl } 6073126Sahl 6083126Sahl break; 6093126Sahl 6102676Seschrock case ZFS_PROP_MOUNTPOINT: 6114778Srm160521 { 6124778Srm160521 namecheck_err_t why; 6134778Srm160521 6142676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 6152676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 6162676Seschrock break; 6172676Seschrock 6184778Srm160521 if (mountpoint_namecheck(strval, &why)) { 6194778Srm160521 switch (why) { 6204778Srm160521 case NAME_ERR_LEADING_SLASH: 6214778Srm160521 zfs_error_aux(hdl, 6224778Srm160521 dgettext(TEXT_DOMAIN, 6234778Srm160521 "'%s' must be an absolute path, " 6244778Srm160521 "'none', or 'legacy'"), propname); 6254778Srm160521 break; 6264778Srm160521 case NAME_ERR_TOOLONG: 6274778Srm160521 zfs_error_aux(hdl, 6284778Srm160521 dgettext(TEXT_DOMAIN, 6294778Srm160521 "component of '%s' is too long"), 6304778Srm160521 propname); 6314778Srm160521 break; 6324778Srm160521 } 6332676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6342676Seschrock goto error; 635789Sahrens } 6364778Srm160521 } 6374778Srm160521 6383126Sahl /*FALLTHRU*/ 6393126Sahl 6405331Samw case ZFS_PROP_SHARESMB: 6413126Sahl case ZFS_PROP_SHARENFS: 6423126Sahl /* 6435331Samw * For the mountpoint and sharenfs or sharesmb 6445331Samw * properties, check if it can be set in a 6455331Samw * global/non-global zone based on 6463126Sahl * the zoned property value: 6473126Sahl * 6483126Sahl * global zone non-global zone 6493126Sahl * -------------------------------------------------- 6503126Sahl * zoned=on mountpoint (no) mountpoint (yes) 6513126Sahl * sharenfs (no) sharenfs (no) 6525331Samw * sharesmb (no) sharesmb (no) 6533126Sahl * 6543126Sahl * zoned=off mountpoint (yes) N/A 6553126Sahl * sharenfs (yes) 6565331Samw * sharesmb (yes) 6573126Sahl */ 6582676Seschrock if (zoned) { 6592676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 6602676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6612676Seschrock "'%s' cannot be set on " 6622676Seschrock "dataset in a non-global zone"), 6632676Seschrock propname); 6642676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6652676Seschrock errbuf); 6662676Seschrock goto error; 6675331Samw } else if (prop == ZFS_PROP_SHARENFS || 6685331Samw prop == ZFS_PROP_SHARESMB) { 6692676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6702676Seschrock "'%s' cannot be set in " 6712676Seschrock "a non-global zone"), propname); 6722676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6732676Seschrock errbuf); 6742676Seschrock goto error; 6752676Seschrock } 6762676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 6772676Seschrock /* 6782676Seschrock * If zoned property is 'off', this must be in 6792676Seschrock * a globle zone. If not, something is wrong. 6802676Seschrock */ 6812676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6822676Seschrock "'%s' cannot be set while dataset " 6832676Seschrock "'zoned' property is set"), propname); 6842676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 6852676Seschrock goto error; 6862676Seschrock } 6873126Sahl 6884180Sdougm /* 6894180Sdougm * At this point, it is legitimate to set the 6904180Sdougm * property. Now we want to make sure that the 6914180Sdougm * property value is valid if it is sharenfs. 6924180Sdougm */ 6935331Samw if ((prop == ZFS_PROP_SHARENFS || 6945331Samw prop == ZFS_PROP_SHARESMB) && 6954217Seschrock strcmp(strval, "on") != 0 && 6964217Seschrock strcmp(strval, "off") != 0) { 6975331Samw zfs_share_proto_t proto; 6985331Samw 6995331Samw if (prop == ZFS_PROP_SHARESMB) 7005331Samw proto = PROTO_SMB; 7015331Samw else 7025331Samw proto = PROTO_NFS; 7034180Sdougm 7044180Sdougm /* 7055331Samw * Must be an valid sharing protocol 7065331Samw * option string so init the libshare 7075331Samw * in order to enable the parser and 7085331Samw * then parse the options. We use the 7095331Samw * control API since we don't care about 7105331Samw * the current configuration and don't 7114180Sdougm * want the overhead of loading it 7124180Sdougm * until we actually do something. 7134180Sdougm */ 7144180Sdougm 7154217Seschrock if (zfs_init_libshare(hdl, 7164217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 7174217Seschrock /* 7184217Seschrock * An error occurred so we can't do 7194217Seschrock * anything 7204217Seschrock */ 7214217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7224217Seschrock "'%s' cannot be set: problem " 7234217Seschrock "in share initialization"), 7244217Seschrock propname); 7254217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7264217Seschrock errbuf); 7274217Seschrock goto error; 7284217Seschrock } 7294217Seschrock 7305331Samw if (zfs_parse_options(strval, proto) != SA_OK) { 7314217Seschrock /* 7324217Seschrock * There was an error in parsing so 7334217Seschrock * deal with it by issuing an error 7344217Seschrock * message and leaving after 7354217Seschrock * uninitializing the the libshare 7364217Seschrock * interface. 7374217Seschrock */ 7384217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7394217Seschrock "'%s' cannot be set to invalid " 7404217Seschrock "options"), propname); 7414217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7424217Seschrock errbuf); 7434217Seschrock zfs_uninit_libshare(hdl); 7444217Seschrock goto error; 7454217Seschrock } 7464180Sdougm zfs_uninit_libshare(hdl); 7474180Sdougm } 7484180Sdougm 7493126Sahl break; 7505331Samw case ZFS_PROP_UTF8ONLY: 7515331Samw chosen_utf = (int)intval; 7525331Samw break; 7535331Samw case ZFS_PROP_NORMALIZE: 7545331Samw chosen_normal = (int)intval; 7555331Samw break; 7562676Seschrock } 7572676Seschrock 7582676Seschrock /* 7592676Seschrock * For changes to existing volumes, we have some additional 7602676Seschrock * checks to enforce. 7612676Seschrock */ 7622676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 7632676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 7642676Seschrock ZFS_PROP_VOLSIZE); 7652676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 7662676Seschrock ZFS_PROP_VOLBLOCKSIZE); 7672676Seschrock char buf[64]; 7682676Seschrock 7692676Seschrock switch (prop) { 7702676Seschrock case ZFS_PROP_RESERVATION: 7712676Seschrock if (intval > volsize) { 7722676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7732676Seschrock "'%s' is greater than current " 7742676Seschrock "volume size"), propname); 7752676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7762676Seschrock errbuf); 7772676Seschrock goto error; 7782676Seschrock } 7792676Seschrock break; 7802676Seschrock 7812676Seschrock case ZFS_PROP_VOLSIZE: 7822676Seschrock if (intval % blocksize != 0) { 7832676Seschrock zfs_nicenum(blocksize, buf, 7842676Seschrock sizeof (buf)); 7852676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7862676Seschrock "'%s' must be a multiple of " 7872676Seschrock "volume block size (%s)"), 7882676Seschrock propname, buf); 7892676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7902676Seschrock errbuf); 7912676Seschrock goto error; 7922676Seschrock } 7932676Seschrock 7942676Seschrock if (intval == 0) { 7952676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7962676Seschrock "'%s' cannot be zero"), 7972676Seschrock propname); 7982676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7992676Seschrock errbuf); 8002676Seschrock goto error; 801789Sahrens } 8023126Sahl break; 803789Sahrens } 804789Sahrens } 805789Sahrens } 806789Sahrens 8072676Seschrock /* 8085331Samw * If normalization was chosen, but no UTF8 choice was made, 8095331Samw * enforce rejection of non-UTF8 names. 8105331Samw * 8115331Samw * If normalization was chosen, but rejecting non-UTF8 names 8125331Samw * was explicitly not chosen, it is an error. 8135331Samw */ 8145331Samw if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf < 0) { 8155331Samw if (nvlist_add_uint64(ret, 8165331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 8175331Samw (void) no_memory(hdl); 8185331Samw goto error; 8195331Samw } 8205331Samw } else if (chosen_normal > ZFS_NORMALIZE_NONE && chosen_utf == 0) { 8215331Samw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 8225331Samw "'%s' must be set 'on' if normalization chosen"), 8235331Samw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 8245331Samw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 8255331Samw goto error; 8265331Samw } 8275331Samw 8285331Samw /* 8292676Seschrock * If this is an existing volume, and someone is setting the volsize, 8302676Seschrock * make sure that it matches the reservation, or add it if necessary. 8312676Seschrock */ 8322676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 8332676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 8342676Seschrock &intval) == 0) { 8352676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 8362676Seschrock ZFS_PROP_VOLSIZE); 8372676Seschrock uint64_t old_reservation = zfs_prop_get_int(zhp, 8382676Seschrock ZFS_PROP_RESERVATION); 8392676Seschrock uint64_t new_reservation; 8402676Seschrock 8412676Seschrock if (old_volsize == old_reservation && 8422676Seschrock nvlist_lookup_uint64(ret, 8432676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8442676Seschrock &new_reservation) != 0) { 8452676Seschrock if (nvlist_add_uint64(ret, 8462676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8472676Seschrock intval) != 0) { 8482676Seschrock (void) no_memory(hdl); 8492676Seschrock goto error; 8502676Seschrock } 8512676Seschrock } 8522676Seschrock } 8532676Seschrock 8542676Seschrock return (ret); 8552676Seschrock 8562676Seschrock error: 8572676Seschrock nvlist_free(ret); 8582676Seschrock return (NULL); 859789Sahrens } 860789Sahrens 8614543Smarks static int 8624543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 8634543Smarks uint64_t *ret_who) 8644543Smarks { 8654543Smarks struct passwd *pwd; 8664543Smarks struct group *grp; 8674543Smarks uid_t id; 8684543Smarks 8694543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 8704543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 8714543Smarks *ret_who = -1; 8724543Smarks return (0); 8734543Smarks } 8744543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 8754543Smarks return (EZFS_BADWHO); 8764543Smarks 8774543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 8784543Smarks strcmp(who, "everyone") == 0) { 8794543Smarks *ret_who = -1; 8804543Smarks *who_type = ZFS_DELEG_EVERYONE; 8814543Smarks return (0); 8824543Smarks } 8834543Smarks 8844543Smarks pwd = getpwnam(who); 8854543Smarks grp = getgrnam(who); 8864543Smarks 8874543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 8884543Smarks *ret_who = pwd->pw_uid; 8894543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 8904543Smarks *ret_who = grp->gr_gid; 8914543Smarks } else if (pwd) { 8924543Smarks *ret_who = pwd->pw_uid; 8934543Smarks *who_type = ZFS_DELEG_USER; 8944543Smarks } else if (grp) { 8954543Smarks *ret_who = grp->gr_gid; 8964543Smarks *who_type = ZFS_DELEG_GROUP; 8974543Smarks } else { 8984543Smarks char *end; 8994543Smarks 9004543Smarks id = strtol(who, &end, 10); 9014543Smarks if (errno != 0 || *end != '\0') { 9024543Smarks return (EZFS_BADWHO); 9034543Smarks } else { 9044543Smarks *ret_who = id; 9054543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 9064543Smarks *who_type = ZFS_DELEG_USER; 9074543Smarks } 9084543Smarks } 9094543Smarks 9104543Smarks return (0); 9114543Smarks } 9124543Smarks 9134543Smarks static void 9144543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 9154543Smarks { 9164543Smarks if (perms_nvp != NULL) { 9174543Smarks verify(nvlist_add_nvlist(who_nvp, 9184543Smarks name, perms_nvp) == 0); 9194543Smarks } else { 9204543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 9214543Smarks } 9224543Smarks } 9234543Smarks 9244543Smarks static void 9254543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 9264543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 9274543Smarks nvlist_t *sets_nvp) 9284543Smarks { 9294543Smarks boolean_t do_perms, do_sets; 9304543Smarks char name[ZFS_MAX_DELEG_NAME]; 9314543Smarks 9324543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 9334543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 9344543Smarks 9354543Smarks if (!do_perms && !do_sets) 9364543Smarks do_perms = do_sets = B_TRUE; 9374543Smarks 9384543Smarks if (do_perms) { 9394543Smarks zfs_deleg_whokey(name, who_type, inherit, 9404543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9414543Smarks whostr : (void *)&whoid); 9424543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 9434543Smarks } 9444543Smarks if (do_sets) { 9454543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 9464543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9474543Smarks whostr : (void *)&whoid); 9484543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 9494543Smarks } 9504543Smarks } 9514543Smarks 9524543Smarks static void 9534543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 9544543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 9554543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 9564543Smarks { 9574543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 9584543Smarks helper(who_type, whoid, whostr, 0, 9594543Smarks who_nvp, perms_nvp, sets_nvp); 9604543Smarks } else { 9614543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 9624543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 9634543Smarks who_nvp, perms_nvp, sets_nvp); 9644543Smarks } 9654543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 9664543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 9674543Smarks who_nvp, perms_nvp, sets_nvp); 9684543Smarks } 9694543Smarks } 9704543Smarks } 9714543Smarks 9724543Smarks /* 9734543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 9744543Smarks * 9754543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 9764543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 9774543Smarks * base attribute named stored in the dsl. 9784543Smarks * Arguments: 9794543Smarks * 9804543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 9814543Smarks * whostr may be null for everyone or create perms. 9824543Smarks * who_type: is the type of entry in whostr. Typically this will be 9834543Smarks * ZFS_DELEG_WHO_UNKNOWN. 9845331Samw * perms: common separated list of permissions. May be null if user 9854543Smarks * is requested to remove permissions by who. 9864543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 9874543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 9884543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 9894543Smarks * The output nvp will look something like this. 9904543Smarks * ul$1234 -> {create ; destroy } 9914543Smarks * Ul$1234 -> { @myset } 9924543Smarks * s-$@myset - { snapshot; checksum; compression } 9934543Smarks */ 9944543Smarks int 9954543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 9964543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 9974543Smarks { 9984543Smarks nvlist_t *who_nvp; 9994543Smarks nvlist_t *perms_nvp = NULL; 10004543Smarks nvlist_t *sets_nvp = NULL; 10014543Smarks char errbuf[1024]; 10024787Sahrens char *who_tok, *perm; 10034543Smarks int error; 10044543Smarks 10054543Smarks *nvp = NULL; 10064543Smarks 10074543Smarks if (perms) { 10084543Smarks if ((error = nvlist_alloc(&perms_nvp, 10094543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10104543Smarks return (1); 10114543Smarks } 10124543Smarks if ((error = nvlist_alloc(&sets_nvp, 10134543Smarks NV_UNIQUE_NAME, 0)) != 0) { 10144543Smarks nvlist_free(perms_nvp); 10154543Smarks return (1); 10164543Smarks } 10174543Smarks } 10184543Smarks 10194543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 10204543Smarks if (perms_nvp) 10214543Smarks nvlist_free(perms_nvp); 10224543Smarks if (sets_nvp) 10234543Smarks nvlist_free(sets_nvp); 10244543Smarks return (1); 10254543Smarks } 10264543Smarks 10274543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 10284543Smarks namecheck_err_t why; 10294543Smarks char what; 10304543Smarks 10314543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 10324787Sahrens nvlist_free(who_nvp); 10334787Sahrens if (perms_nvp) 10344787Sahrens nvlist_free(perms_nvp); 10354787Sahrens if (sets_nvp) 10364787Sahrens nvlist_free(sets_nvp); 10374787Sahrens 10384543Smarks switch (why) { 10394543Smarks case NAME_ERR_NO_AT: 10404543Smarks zfs_error_aux(zhp->zfs_hdl, 10414543Smarks dgettext(TEXT_DOMAIN, 10424543Smarks "set definition must begin with an '@' " 10434543Smarks "character")); 10444543Smarks } 10454543Smarks return (zfs_error(zhp->zfs_hdl, 10464543Smarks EZFS_BADPERMSET, whostr)); 10474543Smarks } 10484543Smarks } 10494543Smarks 10504543Smarks /* 10514543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 10524543Smarks * The first nvlist perms_nvp will have normal permissions and the 10534543Smarks * other sets_nvp will have only permssion set names in it. 10544543Smarks */ 10554787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 10564787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 10574787Sahrens 10584787Sahrens if (perm_canonical) { 10594787Sahrens verify(nvlist_add_boolean(perms_nvp, 10604787Sahrens perm_canonical) == 0); 10614787Sahrens } else if (perm[0] == '@') { 10624787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 10634787Sahrens } else { 10644787Sahrens nvlist_free(who_nvp); 10654787Sahrens nvlist_free(perms_nvp); 10664787Sahrens nvlist_free(sets_nvp); 10674787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 10684543Smarks } 10694543Smarks } 10704543Smarks 10714543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 10724543Smarks who_tok = strtok(whostr, ","); 10734543Smarks if (who_tok == NULL) { 10744543Smarks nvlist_free(who_nvp); 10754787Sahrens if (perms_nvp) 10764787Sahrens nvlist_free(perms_nvp); 10774543Smarks if (sets_nvp) 10784543Smarks nvlist_free(sets_nvp); 10794543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10804543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 10814543Smarks whostr); 10824543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 10834543Smarks } 10844543Smarks } 10854543Smarks 10864543Smarks /* 10874543Smarks * Now create the nvlist(s) 10884543Smarks */ 10894543Smarks do { 10904543Smarks uint64_t who_id; 10914543Smarks 10924543Smarks error = zfs_get_perm_who(who_tok, &who_type, 10934543Smarks &who_id); 10944543Smarks if (error) { 10954543Smarks nvlist_free(who_nvp); 10964787Sahrens if (perms_nvp) 10974787Sahrens nvlist_free(perms_nvp); 10984543Smarks if (sets_nvp) 10994543Smarks nvlist_free(sets_nvp); 11004543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11014543Smarks dgettext(TEXT_DOMAIN, 11024543Smarks "Unable to determine uid/gid for " 11034543Smarks "%s "), who_tok); 11044543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 11054543Smarks } 11064543Smarks 11074543Smarks /* 11084543Smarks * add entries for both local and descendent when required 11094543Smarks */ 11104543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 11114543Smarks perms_nvp, sets_nvp, who_type, inherit); 11124543Smarks 11134543Smarks } while (who_tok = strtok(NULL, ",")); 11144543Smarks *nvp = who_nvp; 11154543Smarks return (0); 11164543Smarks } 11174543Smarks 11184543Smarks static int 11194543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 11204543Smarks { 11214543Smarks zfs_cmd_t zc = { 0 }; 11224543Smarks int error; 11234543Smarks char errbuf[1024]; 11244543Smarks 11254543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11264543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 11274543Smarks zhp->zfs_name); 11284543Smarks 11295094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 11304543Smarks return (-1); 11314543Smarks 11324543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 11334543Smarks zc.zc_perm_action = unset; 11344543Smarks 11354543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 11364543Smarks if (error && errno == ENOTSUP) { 11374543Smarks (void) snprintf(errbuf, sizeof (errbuf), 11384543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 11394543Smarks zcmd_free_nvlists(&zc); 11404543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 11414543Smarks } else if (error) { 11424543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 11434543Smarks } 11444543Smarks zcmd_free_nvlists(&zc); 11454543Smarks 11464543Smarks return (error); 11474543Smarks } 11484543Smarks 11494543Smarks int 11504543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 11514543Smarks { 11524543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 11534543Smarks } 11544543Smarks 11554543Smarks int 11564543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 11574543Smarks { 11584543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 11594543Smarks } 11604543Smarks 11614543Smarks static int 11624543Smarks perm_compare(const void *arg1, const void *arg2) 11634543Smarks { 11644543Smarks const zfs_perm_node_t *node1 = arg1; 11654543Smarks const zfs_perm_node_t *node2 = arg2; 11664543Smarks int ret; 11674543Smarks 11684543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 11694543Smarks 11704543Smarks if (ret > 0) 11714543Smarks return (1); 11724543Smarks if (ret < 0) 11734543Smarks return (-1); 11744543Smarks else 11754543Smarks return (0); 11764543Smarks } 11774543Smarks 11784543Smarks static void 11794543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 11804543Smarks { 11814543Smarks zfs_perm_node_t *permnode; 1182*5367Sahrens void *cookie = NULL; 1183*5367Sahrens 1184*5367Sahrens while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) 11854543Smarks free(permnode); 1186*5367Sahrens avl_destroy(tree); 11874543Smarks } 11884543Smarks 11894543Smarks static void 11904543Smarks zfs_destroy_tree(avl_tree_t *tree) 11914543Smarks { 11924543Smarks zfs_allow_node_t *allownode; 1193*5367Sahrens void *cookie = NULL; 1194*5367Sahrens 11954543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 11964543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 11974543Smarks zfs_destroy_perm_tree(&allownode->z_local); 11984543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 11994543Smarks free(allownode); 12004543Smarks } 1201*5367Sahrens avl_destroy(tree); 12024543Smarks } 12034543Smarks 12044543Smarks void 12054543Smarks zfs_free_allows(zfs_allow_t *allow) 12064543Smarks { 12074543Smarks zfs_allow_t *allownext; 12084543Smarks zfs_allow_t *freeallow; 12094543Smarks 12104543Smarks allownext = allow; 12114543Smarks while (allownext) { 12124543Smarks zfs_destroy_tree(&allownext->z_sets); 12134543Smarks zfs_destroy_tree(&allownext->z_crperms); 12144543Smarks zfs_destroy_tree(&allownext->z_user); 12154543Smarks zfs_destroy_tree(&allownext->z_group); 12164543Smarks zfs_destroy_tree(&allownext->z_everyone); 12174543Smarks freeallow = allownext; 12184543Smarks allownext = allownext->z_next; 12194543Smarks free(freeallow); 12204543Smarks } 12214543Smarks } 12224543Smarks 12234543Smarks static zfs_allow_t * 12244543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 12254543Smarks { 12264543Smarks zfs_allow_t *ptree; 12274543Smarks 12284543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 12294543Smarks sizeof (zfs_allow_t))) == NULL) { 12304543Smarks return (NULL); 12314543Smarks } 12324543Smarks 12334543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 12344543Smarks avl_create(&ptree->z_sets, 12354543Smarks perm_compare, sizeof (zfs_allow_node_t), 12364543Smarks offsetof(zfs_allow_node_t, z_node)); 12374543Smarks avl_create(&ptree->z_crperms, 12384543Smarks perm_compare, sizeof (zfs_allow_node_t), 12394543Smarks offsetof(zfs_allow_node_t, z_node)); 12404543Smarks avl_create(&ptree->z_user, 12414543Smarks perm_compare, sizeof (zfs_allow_node_t), 12424543Smarks offsetof(zfs_allow_node_t, z_node)); 12434543Smarks avl_create(&ptree->z_group, 12444543Smarks perm_compare, sizeof (zfs_allow_node_t), 12454543Smarks offsetof(zfs_allow_node_t, z_node)); 12464543Smarks avl_create(&ptree->z_everyone, 12474543Smarks perm_compare, sizeof (zfs_allow_node_t), 12484543Smarks offsetof(zfs_allow_node_t, z_node)); 12494543Smarks 12504543Smarks if (prev) 12514543Smarks prev->z_next = ptree; 12524543Smarks ptree->z_next = NULL; 12534543Smarks return (ptree); 12544543Smarks } 12554543Smarks 12564543Smarks /* 12574543Smarks * Add permissions to the appropriate AVL permission tree. 12584543Smarks * The appropriate tree may not be the requested tree. 12594543Smarks * For example if ld indicates a local permission, but 12604543Smarks * same permission also exists as a descendent permission 12614543Smarks * then the permission will be removed from the descendent 12624543Smarks * tree and add the the local+descendent tree. 12634543Smarks */ 12644543Smarks static int 12654543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 12664543Smarks char *perm, char ld) 12674543Smarks { 12684543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 12694543Smarks zfs_perm_node_t *newnode; 12704543Smarks avl_index_t where, where2; 12714543Smarks avl_tree_t *tree, *altree; 12724543Smarks 12734543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 12744543Smarks 12754543Smarks if (ld == ZFS_DELEG_NA) { 12764543Smarks tree = &allownode->z_localdescend; 12774543Smarks altree = &allownode->z_descend; 12784543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 12794543Smarks tree = &allownode->z_local; 12804543Smarks altree = &allownode->z_descend; 12814543Smarks } else { 12824543Smarks tree = &allownode->z_descend; 12834543Smarks altree = &allownode->z_local; 12844543Smarks } 12854543Smarks permnode = avl_find(tree, &pnode, &where); 12864543Smarks permnode2 = avl_find(altree, &pnode, &where2); 12874543Smarks 12884543Smarks if (permnode2) { 12894543Smarks avl_remove(altree, permnode2); 12904543Smarks free(permnode2); 12914543Smarks if (permnode == NULL) { 12924543Smarks tree = &allownode->z_localdescend; 12934543Smarks } 12944543Smarks } 12954543Smarks 12964543Smarks /* 12974543Smarks * Now insert new permission in either requested location 12984543Smarks * local/descendent or into ld when perm will exist in both. 12994543Smarks */ 13004543Smarks if (permnode == NULL) { 13014543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 13024543Smarks sizeof (zfs_perm_node_t))) == NULL) { 13034543Smarks return (-1); 13044543Smarks } 13054543Smarks *newnode = pnode; 13064543Smarks avl_add(tree, newnode); 13074543Smarks } 13084543Smarks return (0); 13094543Smarks } 13104577Sahrens 13114543Smarks /* 13124543Smarks * Uggh, this is going to be a bit complicated. 13134543Smarks * we have an nvlist coming out of the kernel that 13144543Smarks * will indicate where the permission is set and then 13154543Smarks * it will contain allow of the various "who's", and what 13164543Smarks * their permissions are. To further complicate this 13174543Smarks * we will then have to coalesce the local,descendent 13184543Smarks * and local+descendent permissions where appropriate. 13194543Smarks * The kernel only knows about a permission as being local 13204543Smarks * or descendent, but not both. 13214543Smarks * 13224543Smarks * In order to make this easier for zfs_main to deal with 13234543Smarks * a series of AVL trees will be used to maintain 13244543Smarks * all of this, primarily for sorting purposes as well 13254543Smarks * as the ability to quickly locate a specific entry. 13264543Smarks * 13274543Smarks * What we end up with are tree's for sets, create perms, 13284543Smarks * user, groups and everyone. With each of those trees 13294543Smarks * we have subtrees for local, descendent and local+descendent 13304543Smarks * permissions. 13314543Smarks */ 13324543Smarks int 13334543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 13344543Smarks { 13354543Smarks zfs_cmd_t zc = { 0 }; 13364543Smarks int error; 13374543Smarks nvlist_t *nvlist; 13384543Smarks nvlist_t *permnv, *sourcenv; 13394543Smarks nvpair_t *who_pair, *source_pair; 13404543Smarks nvpair_t *perm_pair; 13414543Smarks char errbuf[1024]; 13424543Smarks zfs_allow_t *zallowp, *newallowp; 13434543Smarks char ld; 13444543Smarks char *nvpname; 13454543Smarks uid_t uid; 13464543Smarks gid_t gid; 13474543Smarks avl_tree_t *tree; 13484543Smarks avl_index_t where; 13494543Smarks 13504543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13514543Smarks 13524543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 13534543Smarks return (-1); 13544543Smarks 13554543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 13564543Smarks if (errno == ENOMEM) { 13574543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 13584543Smarks zcmd_free_nvlists(&zc); 13594543Smarks return (-1); 13604543Smarks } 13614543Smarks } else if (errno == ENOTSUP) { 13624543Smarks zcmd_free_nvlists(&zc); 13634543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13644543Smarks gettext("Pool must be upgraded to use 'allow'")); 13654543Smarks return (zfs_error(zhp->zfs_hdl, 13664543Smarks EZFS_BADVERSION, errbuf)); 13674543Smarks } else { 13684543Smarks zcmd_free_nvlists(&zc); 13694543Smarks return (-1); 13704543Smarks } 13714543Smarks } 13724543Smarks 13734543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 13744543Smarks zcmd_free_nvlists(&zc); 13754543Smarks return (-1); 13764543Smarks } 13774543Smarks 13784543Smarks zcmd_free_nvlists(&zc); 13794543Smarks 13804543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 13814543Smarks 13824543Smarks if (source_pair == NULL) { 13834543Smarks *zfs_perms = NULL; 13844543Smarks return (0); 13854543Smarks } 13864543Smarks 13874543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 13884543Smarks if (*zfs_perms == NULL) { 13894543Smarks return (0); 13904543Smarks } 13914543Smarks 13924543Smarks zallowp = *zfs_perms; 13934543Smarks 13944543Smarks for (;;) { 13954543Smarks struct passwd *pwd; 13964543Smarks struct group *grp; 13974543Smarks zfs_allow_node_t *allownode; 13984543Smarks zfs_allow_node_t findallownode; 13994543Smarks zfs_allow_node_t *newallownode; 14004543Smarks 14014543Smarks (void) strlcpy(zallowp->z_setpoint, 14024543Smarks nvpair_name(source_pair), 14034543Smarks sizeof (zallowp->z_setpoint)); 14044543Smarks 14054543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 14064543Smarks goto abort; 14074543Smarks 14084543Smarks /* 14094543Smarks * Make sure nvlist is composed correctly 14104543Smarks */ 14114543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 14124543Smarks goto abort; 14134543Smarks } 14144543Smarks 14154543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 14164543Smarks if (who_pair == NULL) { 14174543Smarks goto abort; 14184543Smarks } 14194543Smarks 14204543Smarks do { 14214543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 14224543Smarks if (error) { 14234543Smarks goto abort; 14244543Smarks } 14254543Smarks 14264543Smarks /* 14274543Smarks * First build up the key to use 14284543Smarks * for looking up in the various 14294543Smarks * who trees. 14304543Smarks */ 14314543Smarks ld = nvpair_name(who_pair)[1]; 14324543Smarks nvpname = nvpair_name(who_pair); 14334543Smarks switch (nvpair_name(who_pair)[0]) { 14344543Smarks case ZFS_DELEG_USER: 14354543Smarks case ZFS_DELEG_USER_SETS: 14364543Smarks tree = &zallowp->z_user; 14374543Smarks uid = atol(&nvpname[3]); 14384543Smarks pwd = getpwuid(uid); 14394543Smarks (void) snprintf(findallownode.z_key, 14404543Smarks sizeof (findallownode.z_key), "user %s", 14414543Smarks (pwd) ? pwd->pw_name : 14424543Smarks &nvpair_name(who_pair)[3]); 14434543Smarks break; 14444543Smarks case ZFS_DELEG_GROUP: 14454543Smarks case ZFS_DELEG_GROUP_SETS: 14464543Smarks tree = &zallowp->z_group; 14474543Smarks gid = atol(&nvpname[3]); 14484543Smarks grp = getgrgid(gid); 14494543Smarks (void) snprintf(findallownode.z_key, 14504543Smarks sizeof (findallownode.z_key), "group %s", 14514543Smarks (grp) ? grp->gr_name : 14524543Smarks &nvpair_name(who_pair)[3]); 14534543Smarks break; 14544543Smarks case ZFS_DELEG_CREATE: 14554543Smarks case ZFS_DELEG_CREATE_SETS: 14564543Smarks tree = &zallowp->z_crperms; 14574543Smarks (void) strlcpy(findallownode.z_key, "", 14584543Smarks sizeof (findallownode.z_key)); 14594543Smarks break; 14604543Smarks case ZFS_DELEG_EVERYONE: 14614543Smarks case ZFS_DELEG_EVERYONE_SETS: 14624543Smarks (void) snprintf(findallownode.z_key, 14634543Smarks sizeof (findallownode.z_key), "everyone"); 14644543Smarks tree = &zallowp->z_everyone; 14654543Smarks break; 14664543Smarks case ZFS_DELEG_NAMED_SET: 14674543Smarks case ZFS_DELEG_NAMED_SET_SETS: 14684543Smarks (void) snprintf(findallownode.z_key, 14694543Smarks sizeof (findallownode.z_key), "%s", 14704543Smarks &nvpair_name(who_pair)[3]); 14714543Smarks tree = &zallowp->z_sets; 14724543Smarks break; 14734543Smarks } 14744543Smarks 14754543Smarks /* 14764543Smarks * Place who in tree 14774543Smarks */ 14784543Smarks allownode = avl_find(tree, &findallownode, &where); 14794543Smarks if (allownode == NULL) { 14804543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 14814543Smarks sizeof (zfs_allow_node_t))) == NULL) { 14824543Smarks goto abort; 14834543Smarks } 14844543Smarks avl_create(&newallownode->z_localdescend, 14854543Smarks perm_compare, 14864543Smarks sizeof (zfs_perm_node_t), 14874543Smarks offsetof(zfs_perm_node_t, z_node)); 14884543Smarks avl_create(&newallownode->z_local, 14894543Smarks perm_compare, 14904543Smarks sizeof (zfs_perm_node_t), 14914543Smarks offsetof(zfs_perm_node_t, z_node)); 14924543Smarks avl_create(&newallownode->z_descend, 14934543Smarks perm_compare, 14944543Smarks sizeof (zfs_perm_node_t), 14954543Smarks offsetof(zfs_perm_node_t, z_node)); 14964543Smarks (void) strlcpy(newallownode->z_key, 14974543Smarks findallownode.z_key, 14984543Smarks sizeof (findallownode.z_key)); 14994543Smarks avl_insert(tree, newallownode, where); 15004543Smarks allownode = newallownode; 15014543Smarks } 15024543Smarks 15034543Smarks /* 15044543Smarks * Now iterate over the permissions and 15054543Smarks * place them in the appropriate local, 15064543Smarks * descendent or local+descendent tree. 15074543Smarks * 15084543Smarks * The permissions are added to the tree 15094543Smarks * via zfs_coalesce_perm(). 15104543Smarks */ 15114543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 15124543Smarks if (perm_pair == NULL) 15134543Smarks goto abort; 15144543Smarks do { 15154543Smarks if (zfs_coalesce_perm(zhp, allownode, 15164543Smarks nvpair_name(perm_pair), ld) != 0) 15174543Smarks goto abort; 15184543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 15194543Smarks perm_pair)); 15204543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 15214543Smarks 15224543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 15234543Smarks if (source_pair == NULL) 15244543Smarks break; 15254543Smarks 15264543Smarks /* 15274543Smarks * allocate another node from the link list of 15284543Smarks * zfs_allow_t structures 15294543Smarks */ 15304543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 15314543Smarks nvpair_name(source_pair)); 15324543Smarks if (newallowp == NULL) { 15334543Smarks goto abort; 15344543Smarks } 15354543Smarks zallowp = newallowp; 15364543Smarks } 15374543Smarks nvlist_free(nvlist); 15384543Smarks return (0); 15394543Smarks abort: 15404543Smarks zfs_free_allows(*zfs_perms); 15414543Smarks nvlist_free(nvlist); 15424543Smarks return (-1); 15434543Smarks } 15444543Smarks 1545789Sahrens /* 1546789Sahrens * Given a property name and value, set the property for the given dataset. 1547789Sahrens */ 1548789Sahrens int 15492676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1550789Sahrens { 1551789Sahrens zfs_cmd_t zc = { 0 }; 15522676Seschrock int ret = -1; 15532676Seschrock prop_changelist_t *cl = NULL; 15542082Seschrock char errbuf[1024]; 15552082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 15562676Seschrock nvlist_t *nvl = NULL, *realprops; 15572676Seschrock zfs_prop_t prop; 15582082Seschrock 15592082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 15602676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 15612082Seschrock zhp->zfs_name); 15622082Seschrock 15632676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 15642676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 15652676Seschrock (void) no_memory(hdl); 15662676Seschrock goto error; 1567789Sahrens } 1568789Sahrens 15695094Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 15702676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 15712676Seschrock goto error; 15725094Slling 15732676Seschrock nvlist_free(nvl); 15742676Seschrock nvl = realprops; 15752676Seschrock 15762676Seschrock prop = zfs_name_to_prop(propname); 15772676Seschrock 1578789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 15792676Seschrock goto error; 1580789Sahrens 1581789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 15822082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15832082Seschrock "child dataset with inherited mountpoint is used " 15842082Seschrock "in a non-global zone")); 15852082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1586789Sahrens goto error; 1587789Sahrens } 1588789Sahrens 1589789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1590789Sahrens goto error; 1591789Sahrens 1592789Sahrens /* 1593789Sahrens * Execute the corresponding ioctl() to set this property. 1594789Sahrens */ 1595789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1596789Sahrens 15975094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 15982676Seschrock goto error; 15992676Seschrock 16004543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1601789Sahrens 1602789Sahrens if (ret != 0) { 1603789Sahrens switch (errno) { 1604789Sahrens 1605789Sahrens case ENOSPC: 1606789Sahrens /* 1607789Sahrens * For quotas and reservations, ENOSPC indicates 1608789Sahrens * something different; setting a quota or reservation 1609789Sahrens * doesn't use any disk space. 1610789Sahrens */ 1611789Sahrens switch (prop) { 1612789Sahrens case ZFS_PROP_QUOTA: 16132082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16142082Seschrock "size is less than current used or " 16152082Seschrock "reserved space")); 16162082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1617789Sahrens break; 1618789Sahrens 1619789Sahrens case ZFS_PROP_RESERVATION: 16202082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16212082Seschrock "size is greater than available space")); 16222082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1623789Sahrens break; 1624789Sahrens 1625789Sahrens default: 16262082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1627789Sahrens break; 1628789Sahrens } 1629789Sahrens break; 1630789Sahrens 1631789Sahrens case EBUSY: 16322082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 16332082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 16342082Seschrock else 16352676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1636789Sahrens break; 1637789Sahrens 16381175Slling case EROFS: 16392082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 16401175Slling break; 16411175Slling 16423886Sahl case ENOTSUP: 16433886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16444603Sahrens "pool must be upgraded to set this " 16454603Sahrens "property or value")); 16463886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 16473886Sahl break; 16483886Sahl 1649789Sahrens case EOVERFLOW: 1650789Sahrens /* 1651789Sahrens * This platform can't address a volume this big. 1652789Sahrens */ 1653789Sahrens #ifdef _ILP32 1654789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 16552082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1656789Sahrens break; 1657789Sahrens } 1658789Sahrens #endif 16592082Seschrock /* FALLTHROUGH */ 1660789Sahrens default: 16612082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1662789Sahrens } 1663789Sahrens } else { 1664789Sahrens /* 1665789Sahrens * Refresh the statistics so the new property value 1666789Sahrens * is reflected. 1667789Sahrens */ 16682676Seschrock if ((ret = changelist_postfix(cl)) == 0) 16692676Seschrock (void) get_stats(zhp); 1670789Sahrens } 1671789Sahrens 1672789Sahrens error: 16732676Seschrock nvlist_free(nvl); 16742676Seschrock zcmd_free_nvlists(&zc); 16752676Seschrock if (cl) 16762676Seschrock changelist_free(cl); 1677789Sahrens return (ret); 1678789Sahrens } 1679789Sahrens 1680789Sahrens /* 1681789Sahrens * Given a property, inherit the value from the parent dataset. 1682789Sahrens */ 1683789Sahrens int 16842676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1685789Sahrens { 1686789Sahrens zfs_cmd_t zc = { 0 }; 1687789Sahrens int ret; 1688789Sahrens prop_changelist_t *cl; 16892082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 16902082Seschrock char errbuf[1024]; 16912676Seschrock zfs_prop_t prop; 16922082Seschrock 16932082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 16942082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1695789Sahrens 16965094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 16972676Seschrock /* 16982676Seschrock * For user properties, the amount of work we have to do is very 16992676Seschrock * small, so just do it here. 17002676Seschrock */ 17012676Seschrock if (!zfs_prop_user(propname)) { 17022676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17032676Seschrock "invalid property")); 17042676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 17052676Seschrock } 17062676Seschrock 17072676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17082676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 17092676Seschrock 17104849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 17112676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 17122676Seschrock 17132676Seschrock return (0); 17142676Seschrock } 17152676Seschrock 1716789Sahrens /* 1717789Sahrens * Verify that this property is inheritable. 1718789Sahrens */ 17192082Seschrock if (zfs_prop_readonly(prop)) 17202082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 17212082Seschrock 17222082Seschrock if (!zfs_prop_inheritable(prop)) 17232082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1724789Sahrens 1725789Sahrens /* 1726789Sahrens * Check to see if the value applies to this type 1727789Sahrens */ 17282082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 17292082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1730789Sahrens 17313443Srm160521 /* 17323443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 17333443Srm160521 */ 17343443Srm160521 propname = zfs_prop_to_name(prop); 1735789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 17362676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1737789Sahrens 1738789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1739789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 17402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17412082Seschrock "dataset is used in a non-global zone")); 17422082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1743789Sahrens } 1744789Sahrens 1745789Sahrens /* 1746789Sahrens * Determine datasets which will be affected by this change, if any. 1747789Sahrens */ 1748789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1749789Sahrens return (-1); 1750789Sahrens 1751789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 17522082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17532082Seschrock "child dataset with inherited mountpoint is used " 17542082Seschrock "in a non-global zone")); 17552082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1756789Sahrens goto error; 1757789Sahrens } 1758789Sahrens 1759789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1760789Sahrens goto error; 1761789Sahrens 17624849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 17632082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1764789Sahrens } else { 1765789Sahrens 17662169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1767789Sahrens goto error; 1768789Sahrens 1769789Sahrens /* 1770789Sahrens * Refresh the statistics so the new property is reflected. 1771789Sahrens */ 1772789Sahrens (void) get_stats(zhp); 1773789Sahrens } 1774789Sahrens 1775789Sahrens error: 1776789Sahrens changelist_free(cl); 1777789Sahrens return (ret); 1778789Sahrens } 1779789Sahrens 1780789Sahrens /* 17811356Seschrock * True DSL properties are stored in an nvlist. The following two functions 17821356Seschrock * extract them appropriately. 17831356Seschrock */ 17841356Seschrock static uint64_t 17851356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 17861356Seschrock { 17871356Seschrock nvlist_t *nv; 17881356Seschrock uint64_t value; 17891356Seschrock 17902885Sahrens *source = NULL; 17911356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 17921356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 17935094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 17945094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 17951356Seschrock } else { 17961356Seschrock value = zfs_prop_default_numeric(prop); 17971356Seschrock *source = ""; 17981356Seschrock } 17991356Seschrock 18001356Seschrock return (value); 18011356Seschrock } 18021356Seschrock 18031356Seschrock static char * 18041356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18051356Seschrock { 18061356Seschrock nvlist_t *nv; 18071356Seschrock char *value; 18081356Seschrock 18092885Sahrens *source = NULL; 18101356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18111356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 18125094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 18135094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18141356Seschrock } else { 18151356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 18161356Seschrock value = ""; 18171356Seschrock *source = ""; 18181356Seschrock } 18191356Seschrock 18201356Seschrock return (value); 18211356Seschrock } 18221356Seschrock 18231356Seschrock /* 1824789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1825789Sahrens * zfs_prop_get_int() are built using this interface. 1826789Sahrens * 1827789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1828789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1829789Sahrens * If they differ from the on-disk values, report the current values and mark 1830789Sahrens * the source "temporary". 1831789Sahrens */ 18322082Seschrock static int 18335094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 18342082Seschrock char **source, uint64_t *val) 1835789Sahrens { 18365147Srm160521 zfs_cmd_t zc = { 0 }; 1837789Sahrens struct mnttab mnt; 18383265Sahrens char *mntopt_on = NULL; 18393265Sahrens char *mntopt_off = NULL; 1840789Sahrens 1841789Sahrens *source = NULL; 1842789Sahrens 18433265Sahrens switch (prop) { 18443265Sahrens case ZFS_PROP_ATIME: 18453265Sahrens mntopt_on = MNTOPT_ATIME; 18463265Sahrens mntopt_off = MNTOPT_NOATIME; 18473265Sahrens break; 18483265Sahrens 18493265Sahrens case ZFS_PROP_DEVICES: 18503265Sahrens mntopt_on = MNTOPT_DEVICES; 18513265Sahrens mntopt_off = MNTOPT_NODEVICES; 18523265Sahrens break; 18533265Sahrens 18543265Sahrens case ZFS_PROP_EXEC: 18553265Sahrens mntopt_on = MNTOPT_EXEC; 18563265Sahrens mntopt_off = MNTOPT_NOEXEC; 18573265Sahrens break; 18583265Sahrens 18593265Sahrens case ZFS_PROP_READONLY: 18603265Sahrens mntopt_on = MNTOPT_RO; 18613265Sahrens mntopt_off = MNTOPT_RW; 18623265Sahrens break; 18633265Sahrens 18643265Sahrens case ZFS_PROP_SETUID: 18653265Sahrens mntopt_on = MNTOPT_SETUID; 18663265Sahrens mntopt_off = MNTOPT_NOSETUID; 18673265Sahrens break; 18683265Sahrens 18693265Sahrens case ZFS_PROP_XATTR: 18703265Sahrens mntopt_on = MNTOPT_XATTR; 18713265Sahrens mntopt_off = MNTOPT_NOXATTR; 18723265Sahrens break; 18735331Samw 18745331Samw case ZFS_PROP_NBMAND: 18755331Samw mntopt_on = MNTOPT_NBMAND; 18765331Samw mntopt_off = MNTOPT_NONBMAND; 18775331Samw break; 18783265Sahrens } 18793265Sahrens 18802474Seschrock /* 18812474Seschrock * Because looking up the mount options is potentially expensive 18822474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 18832474Seschrock * we're looking up a property which requires its presence. 18842474Seschrock */ 18852474Seschrock if (!zhp->zfs_mntcheck && 18863265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 18873265Sahrens struct mnttab entry, search = { 0 }; 18883265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 18892474Seschrock 18902474Seschrock search.mnt_special = (char *)zhp->zfs_name; 18912474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 18923265Sahrens rewind(mnttab); 18933265Sahrens 18943265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 18953265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 18963265Sahrens entry.mnt_mntopts); 18973265Sahrens if (zhp->zfs_mntopts == NULL) 18983265Sahrens return (-1); 18993265Sahrens } 19002474Seschrock 19012474Seschrock zhp->zfs_mntcheck = B_TRUE; 19022474Seschrock } 19032474Seschrock 1904789Sahrens if (zhp->zfs_mntopts == NULL) 1905789Sahrens mnt.mnt_mntopts = ""; 1906789Sahrens else 1907789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1908789Sahrens 1909789Sahrens switch (prop) { 1910789Sahrens case ZFS_PROP_ATIME: 19113265Sahrens case ZFS_PROP_DEVICES: 19123265Sahrens case ZFS_PROP_EXEC: 19133265Sahrens case ZFS_PROP_READONLY: 19143265Sahrens case ZFS_PROP_SETUID: 19153265Sahrens case ZFS_PROP_XATTR: 19165331Samw case ZFS_PROP_NBMAND: 19172082Seschrock *val = getprop_uint64(zhp, prop, source); 19182082Seschrock 19193265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 19202082Seschrock *val = B_TRUE; 1921789Sahrens if (src) 19225094Slling *src = ZPROP_SRC_TEMPORARY; 19233265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 19242082Seschrock *val = B_FALSE; 1925789Sahrens if (src) 19265094Slling *src = ZPROP_SRC_TEMPORARY; 1927789Sahrens } 19282082Seschrock break; 1929789Sahrens 19303265Sahrens case ZFS_PROP_CANMOUNT: 19312082Seschrock *val = getprop_uint64(zhp, prop, source); 19323417Srm160521 if (*val == 0) 19333417Srm160521 *source = zhp->zfs_name; 19343417Srm160521 else 19353417Srm160521 *source = ""; /* default */ 19362082Seschrock break; 1937789Sahrens 1938789Sahrens case ZFS_PROP_QUOTA: 1939789Sahrens case ZFS_PROP_RESERVATION: 19402885Sahrens *val = getprop_uint64(zhp, prop, source); 19412885Sahrens if (*val == 0) 1942789Sahrens *source = ""; /* default */ 1943789Sahrens else 1944789Sahrens *source = zhp->zfs_name; 19452082Seschrock break; 1946789Sahrens 1947789Sahrens case ZFS_PROP_MOUNTED: 19482082Seschrock *val = (zhp->zfs_mntopts != NULL); 19492082Seschrock break; 1950789Sahrens 19513377Seschrock case ZFS_PROP_NUMCLONES: 19523377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 19533377Seschrock break; 19543377Seschrock 19555147Srm160521 case ZFS_PROP_VERSION: 19565147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19575147Srm160521 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_VERSION, &zc) || 19585147Srm160521 (zc.zc_cookie == 0)) { 19595147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19605147Srm160521 "unable to get version property")); 19615147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 19625147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 19635147Srm160521 } 19645147Srm160521 *val = zc.zc_cookie; 19655147Srm160521 break; 19665147Srm160521 1967789Sahrens default: 19684577Sahrens switch (zfs_prop_get_type(prop)) { 19694787Sahrens case PROP_TYPE_NUMBER: 19704787Sahrens case PROP_TYPE_INDEX: 19714577Sahrens *val = getprop_uint64(zhp, prop, source); 19724577Sahrens break; 19734577Sahrens 19744787Sahrens case PROP_TYPE_STRING: 19754577Sahrens default: 19764577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19774577Sahrens "cannot get non-numeric property")); 19784577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 19794577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 19804577Sahrens } 1981789Sahrens } 1982789Sahrens 1983789Sahrens return (0); 1984789Sahrens } 1985789Sahrens 1986789Sahrens /* 1987789Sahrens * Calculate the source type, given the raw source string. 1988789Sahrens */ 1989789Sahrens static void 19905094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1991789Sahrens char *statbuf, size_t statlen) 1992789Sahrens { 19935094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1994789Sahrens return; 1995789Sahrens 1996789Sahrens if (source == NULL) { 19975094Slling *srctype = ZPROP_SRC_NONE; 1998789Sahrens } else if (source[0] == '\0') { 19995094Slling *srctype = ZPROP_SRC_DEFAULT; 2000789Sahrens } else { 2001789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 20025094Slling *srctype = ZPROP_SRC_LOCAL; 2003789Sahrens } else { 2004789Sahrens (void) strlcpy(statbuf, source, statlen); 20055094Slling *srctype = ZPROP_SRC_INHERITED; 2006789Sahrens } 2007789Sahrens } 2008789Sahrens 2009789Sahrens } 2010789Sahrens 2011789Sahrens /* 2012789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2013789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2014789Sahrens * human-readable form. 2015789Sahrens * 2016789Sahrens * Returns 0 on success, or -1 on error. 2017789Sahrens */ 2018789Sahrens int 2019789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 20205094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2021789Sahrens { 2022789Sahrens char *source = NULL; 2023789Sahrens uint64_t val; 2024789Sahrens char *str; 2025789Sahrens const char *root; 20262676Seschrock const char *strval; 2027789Sahrens 2028789Sahrens /* 2029789Sahrens * Check to see if this property applies to our object 2030789Sahrens */ 2031789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2032789Sahrens return (-1); 2033789Sahrens 2034789Sahrens if (src) 20355094Slling *src = ZPROP_SRC_NONE; 2036789Sahrens 2037789Sahrens switch (prop) { 2038789Sahrens case ZFS_PROP_CREATION: 2039789Sahrens /* 2040789Sahrens * 'creation' is a time_t stored in the statistics. We convert 2041789Sahrens * this into a string unless 'literal' is specified. 2042789Sahrens */ 2043789Sahrens { 20442885Sahrens val = getprop_uint64(zhp, prop, &source); 20452885Sahrens time_t time = (time_t)val; 2046789Sahrens struct tm t; 2047789Sahrens 2048789Sahrens if (literal || 2049789Sahrens localtime_r(&time, &t) == NULL || 2050789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2051789Sahrens &t) == 0) 20522885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2053789Sahrens } 2054789Sahrens break; 2055789Sahrens 2056789Sahrens case ZFS_PROP_MOUNTPOINT: 2057789Sahrens /* 2058789Sahrens * Getting the precise mountpoint can be tricky. 2059789Sahrens * 2060789Sahrens * - for 'none' or 'legacy', return those values. 2061789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2062789Sahrens * - for inherited mountpoints, we want to take everything 2063789Sahrens * after our ancestor and append it to the inherited value. 2064789Sahrens * 2065789Sahrens * If the pool has an alternate root, we want to prepend that 2066789Sahrens * root to any values we return. 2067789Sahrens */ 20681544Seschrock root = zhp->zfs_root; 20691356Seschrock str = getprop_string(zhp, prop, &source); 20701356Seschrock 20711356Seschrock if (str[0] == '\0') { 2072789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2073789Sahrens root, zhp->zfs_name); 20741356Seschrock } else if (str[0] == '/') { 20751356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2076789Sahrens 2077789Sahrens if (relpath[0] == '/') 2078789Sahrens relpath++; 20791356Seschrock if (str[1] == '\0') 20801356Seschrock str++; 2081789Sahrens 2082789Sahrens if (relpath[0] == '\0') 2083789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 20841356Seschrock root, str); 2085789Sahrens else 2086789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 20871356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2088789Sahrens relpath); 2089789Sahrens } else { 2090789Sahrens /* 'legacy' or 'none' */ 20911356Seschrock (void) strlcpy(propbuf, str, proplen); 2092789Sahrens } 2093789Sahrens 2094789Sahrens break; 2095789Sahrens 2096789Sahrens case ZFS_PROP_ORIGIN: 20972885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2098789Sahrens proplen); 2099789Sahrens /* 2100789Sahrens * If there is no parent at all, return failure to indicate that 2101789Sahrens * it doesn't apply to this dataset. 2102789Sahrens */ 2103789Sahrens if (propbuf[0] == '\0') 2104789Sahrens return (-1); 2105789Sahrens break; 2106789Sahrens 2107789Sahrens case ZFS_PROP_QUOTA: 2108789Sahrens case ZFS_PROP_RESERVATION: 21092082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21102082Seschrock return (-1); 2111789Sahrens 2112789Sahrens /* 2113789Sahrens * If quota or reservation is 0, we translate this into 'none' 2114789Sahrens * (unless literal is set), and indicate that it's the default 2115789Sahrens * value. Otherwise, we print the number nicely and indicate 2116789Sahrens * that its set locally. 2117789Sahrens */ 2118789Sahrens if (val == 0) { 2119789Sahrens if (literal) 2120789Sahrens (void) strlcpy(propbuf, "0", proplen); 2121789Sahrens else 2122789Sahrens (void) strlcpy(propbuf, "none", proplen); 2123789Sahrens } else { 2124789Sahrens if (literal) 21252856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 21263912Slling (u_longlong_t)val); 2127789Sahrens else 2128789Sahrens zfs_nicenum(val, propbuf, proplen); 2129789Sahrens } 2130789Sahrens break; 2131789Sahrens 2132789Sahrens case ZFS_PROP_COMPRESSRATIO: 21332082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 21342082Seschrock return (-1); 21352856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 21362856Snd150628 val / 100, (longlong_t)val % 100); 2137789Sahrens break; 2138789Sahrens 2139789Sahrens case ZFS_PROP_TYPE: 2140789Sahrens switch (zhp->zfs_type) { 2141789Sahrens case ZFS_TYPE_FILESYSTEM: 2142789Sahrens str = "filesystem"; 2143789Sahrens break; 2144789Sahrens case ZFS_TYPE_VOLUME: 2145789Sahrens str = "volume"; 2146789Sahrens break; 2147789Sahrens case ZFS_TYPE_SNAPSHOT: 2148789Sahrens str = "snapshot"; 2149789Sahrens break; 2150789Sahrens default: 21512082Seschrock abort(); 2152789Sahrens } 2153789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2154789Sahrens break; 2155789Sahrens 2156789Sahrens case ZFS_PROP_MOUNTED: 2157789Sahrens /* 2158789Sahrens * The 'mounted' property is a pseudo-property that described 2159789Sahrens * whether the filesystem is currently mounted. Even though 2160789Sahrens * it's a boolean value, the typical values of "on" and "off" 2161789Sahrens * don't make sense, so we translate to "yes" and "no". 2162789Sahrens */ 21632082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 21642082Seschrock src, &source, &val) != 0) 21652082Seschrock return (-1); 21662082Seschrock if (val) 2167789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2168789Sahrens else 2169789Sahrens (void) strlcpy(propbuf, "no", proplen); 2170789Sahrens break; 2171789Sahrens 2172789Sahrens case ZFS_PROP_NAME: 2173789Sahrens /* 2174789Sahrens * The 'name' property is a pseudo-property derived from the 2175789Sahrens * dataset name. It is presented as a real property to simplify 2176789Sahrens * consumers. 2177789Sahrens */ 2178789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2179789Sahrens break; 2180789Sahrens 2181789Sahrens default: 21824577Sahrens switch (zfs_prop_get_type(prop)) { 21834787Sahrens case PROP_TYPE_NUMBER: 21844577Sahrens if (get_numeric_property(zhp, prop, src, 21854577Sahrens &source, &val) != 0) 21864577Sahrens return (-1); 21874577Sahrens if (literal) 21884577Sahrens (void) snprintf(propbuf, proplen, "%llu", 21894577Sahrens (u_longlong_t)val); 21904577Sahrens else 21914577Sahrens zfs_nicenum(val, propbuf, proplen); 21924577Sahrens break; 21934577Sahrens 21944787Sahrens case PROP_TYPE_STRING: 21954577Sahrens (void) strlcpy(propbuf, 21964577Sahrens getprop_string(zhp, prop, &source), proplen); 21974577Sahrens break; 21984577Sahrens 21994787Sahrens case PROP_TYPE_INDEX: 22004861Sahrens if (get_numeric_property(zhp, prop, src, 22014861Sahrens &source, &val) != 0) 22024861Sahrens return (-1); 22034861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 22044577Sahrens return (-1); 22054577Sahrens (void) strlcpy(propbuf, strval, proplen); 22064577Sahrens break; 22074577Sahrens 22084577Sahrens default: 22094577Sahrens abort(); 22104577Sahrens } 2211789Sahrens } 2212789Sahrens 2213789Sahrens get_source(zhp, src, source, statbuf, statlen); 2214789Sahrens 2215789Sahrens return (0); 2216789Sahrens } 2217789Sahrens 2218789Sahrens /* 2219789Sahrens * Utility function to get the given numeric property. Does no validation that 2220789Sahrens * the given property is the appropriate type; should only be used with 2221789Sahrens * hard-coded property types. 2222789Sahrens */ 2223789Sahrens uint64_t 2224789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2225789Sahrens { 2226789Sahrens char *source; 22272082Seschrock uint64_t val; 22282082Seschrock 2229*5367Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 22302082Seschrock 22312082Seschrock return (val); 2232789Sahrens } 2233789Sahrens 2234789Sahrens /* 2235789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2236789Sahrens */ 2237789Sahrens int 2238789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 22395094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2240789Sahrens { 2241789Sahrens char *source; 2242789Sahrens 2243789Sahrens /* 2244789Sahrens * Check to see if this property applies to our object 2245789Sahrens */ 22464849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 22473237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 22482082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 22492082Seschrock zfs_prop_to_name(prop))); 22504849Sahrens } 2251789Sahrens 2252789Sahrens if (src) 22535094Slling *src = ZPROP_SRC_NONE; 2254789Sahrens 22552082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 22562082Seschrock return (-1); 2257789Sahrens 2258789Sahrens get_source(zhp, src, source, statbuf, statlen); 2259789Sahrens 2260789Sahrens return (0); 2261789Sahrens } 2262789Sahrens 2263789Sahrens /* 2264789Sahrens * Returns the name of the given zfs handle. 2265789Sahrens */ 2266789Sahrens const char * 2267789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2268789Sahrens { 2269789Sahrens return (zhp->zfs_name); 2270789Sahrens } 2271789Sahrens 2272789Sahrens /* 2273789Sahrens * Returns the type of the given zfs handle. 2274789Sahrens */ 2275789Sahrens zfs_type_t 2276789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2277789Sahrens { 2278789Sahrens return (zhp->zfs_type); 2279789Sahrens } 2280789Sahrens 2281789Sahrens /* 22821356Seschrock * Iterate over all child filesystems 2283789Sahrens */ 2284789Sahrens int 22851356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2286789Sahrens { 2287789Sahrens zfs_cmd_t zc = { 0 }; 2288789Sahrens zfs_handle_t *nzhp; 2289789Sahrens int ret; 2290789Sahrens 2291*5367Sahrens if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM) 2292*5367Sahrens return (0); 2293*5367Sahrens 2294789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 22952082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2296789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2297789Sahrens /* 2298789Sahrens * Ignore private dataset names. 2299789Sahrens */ 2300789Sahrens if (dataset_name_hidden(zc.zc_name)) 2301789Sahrens continue; 2302789Sahrens 2303789Sahrens /* 2304789Sahrens * Silently ignore errors, as the only plausible explanation is 2305789Sahrens * that the pool has since been removed. 2306789Sahrens */ 23072082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23082082Seschrock zc.zc_name)) == NULL) 2309789Sahrens continue; 2310789Sahrens 2311789Sahrens if ((ret = func(nzhp, data)) != 0) 2312789Sahrens return (ret); 2313789Sahrens } 2314789Sahrens 2315789Sahrens /* 2316789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2317789Sahrens * returned, then the underlying dataset has been removed since we 2318789Sahrens * obtained the handle. 2319789Sahrens */ 2320789Sahrens if (errno != ESRCH && errno != ENOENT) 23212082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23222082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2323789Sahrens 23241356Seschrock return (0); 23251356Seschrock } 23261356Seschrock 23271356Seschrock /* 23281356Seschrock * Iterate over all snapshots 23291356Seschrock */ 23301356Seschrock int 23311356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23321356Seschrock { 23331356Seschrock zfs_cmd_t zc = { 0 }; 23341356Seschrock zfs_handle_t *nzhp; 23351356Seschrock int ret; 2336789Sahrens 2337*5367Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) 2338*5367Sahrens return (0); 2339*5367Sahrens 2340789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 23412082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 23422082Seschrock &zc) == 0; 2343789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2344789Sahrens 23452082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 23462082Seschrock zc.zc_name)) == NULL) 2347789Sahrens continue; 2348789Sahrens 2349789Sahrens if ((ret = func(nzhp, data)) != 0) 2350789Sahrens return (ret); 2351789Sahrens } 2352789Sahrens 2353789Sahrens /* 2354789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2355789Sahrens * returned, then the underlying dataset has been removed since we 2356789Sahrens * obtained the handle. Silently ignore this case, and return success. 2357789Sahrens */ 2358789Sahrens if (errno != ESRCH && errno != ENOENT) 23592082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23602082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2361789Sahrens 2362789Sahrens return (0); 2363789Sahrens } 2364789Sahrens 2365789Sahrens /* 23661356Seschrock * Iterate over all children, snapshots and filesystems 23671356Seschrock */ 23681356Seschrock int 23691356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23701356Seschrock { 23711356Seschrock int ret; 23721356Seschrock 23731356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 23741356Seschrock return (ret); 23751356Seschrock 23761356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 23771356Seschrock } 23781356Seschrock 23791356Seschrock /* 2380789Sahrens * Given a complete name, return just the portion that refers to the parent. 2381789Sahrens * Can return NULL if this is a pool. 2382789Sahrens */ 2383789Sahrens static int 2384789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2385789Sahrens { 2386789Sahrens char *loc; 2387789Sahrens 2388789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2389789Sahrens return (-1); 2390789Sahrens 2391789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2392789Sahrens buf[loc - path] = '\0'; 2393789Sahrens 2394789Sahrens return (0); 2395789Sahrens } 2396789Sahrens 2397789Sahrens /* 23984490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 23994490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 24004490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 24014490Svb160487 * length of already existing prefix of the given path. We also fetch the 24024490Svb160487 * 'zoned' property, which is used to validate property settings when creating 24034490Svb160487 * new datasets. 2404789Sahrens */ 2405789Sahrens static int 24064490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 24074490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2408789Sahrens { 2409789Sahrens zfs_cmd_t zc = { 0 }; 2410789Sahrens char parent[ZFS_MAXNAMELEN]; 2411789Sahrens char *slash; 24121356Seschrock zfs_handle_t *zhp; 24132082Seschrock char errbuf[1024]; 24142082Seschrock 24152082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 24162082Seschrock path); 2417789Sahrens 2418789Sahrens /* get parent, and check to see if this is just a pool */ 2419789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 24202082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24212082Seschrock "missing dataset name")); 24222082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2423789Sahrens } 2424789Sahrens 2425789Sahrens /* check to see if the pool exists */ 2426789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2427789Sahrens slash = parent + strlen(parent); 2428789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2429789Sahrens zc.zc_name[slash - parent] = '\0'; 24302082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2431789Sahrens errno == ENOENT) { 24322082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24332082Seschrock "no such pool '%s'"), zc.zc_name); 24342082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2435789Sahrens } 2436789Sahrens 2437789Sahrens /* check to see if the parent dataset exists */ 24384490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 24394490Svb160487 if (errno == ENOENT && accept_ancestor) { 24404490Svb160487 /* 24414490Svb160487 * Go deeper to find an ancestor, give up on top level. 24424490Svb160487 */ 24434490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 24444490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24454490Svb160487 "no such pool '%s'"), zc.zc_name); 24464490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24474490Svb160487 } 24484490Svb160487 } else if (errno == ENOENT) { 24492082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24502082Seschrock "parent does not exist")); 24512082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24524490Svb160487 } else 24532082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2454789Sahrens } 2455789Sahrens 24562676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2457789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 24582676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 24592082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 24601356Seschrock zfs_close(zhp); 2461789Sahrens return (-1); 2462789Sahrens } 2463789Sahrens 2464789Sahrens /* make sure parent is a filesystem */ 24651356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 24662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24672082Seschrock "parent is not a filesystem")); 24682082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 24691356Seschrock zfs_close(zhp); 2470789Sahrens return (-1); 2471789Sahrens } 2472789Sahrens 24731356Seschrock zfs_close(zhp); 24744490Svb160487 if (prefixlen != NULL) 24754490Svb160487 *prefixlen = strlen(parent); 24764490Svb160487 return (0); 24774490Svb160487 } 24784490Svb160487 24794490Svb160487 /* 24804490Svb160487 * Finds whether the dataset of the given type(s) exists. 24814490Svb160487 */ 24824490Svb160487 boolean_t 24834490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 24844490Svb160487 { 24854490Svb160487 zfs_handle_t *zhp; 24864490Svb160487 24875326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 24884490Svb160487 return (B_FALSE); 24894490Svb160487 24904490Svb160487 /* 24914490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 24924490Svb160487 */ 24934490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 24944490Svb160487 int ds_type = zhp->zfs_type; 24954490Svb160487 24964490Svb160487 zfs_close(zhp); 24974490Svb160487 if (types & ds_type) 24984490Svb160487 return (B_TRUE); 24994490Svb160487 } 25004490Svb160487 return (B_FALSE); 25014490Svb160487 } 25024490Svb160487 25034490Svb160487 /* 2504*5367Sahrens * Given a path to 'target', create all the ancestors between 2505*5367Sahrens * the prefixlen portion of the path, and the target itself. 2506*5367Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 2507*5367Sahrens */ 2508*5367Sahrens int 2509*5367Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 2510*5367Sahrens { 2511*5367Sahrens zfs_handle_t *h; 2512*5367Sahrens char *cp; 2513*5367Sahrens const char *opname; 2514*5367Sahrens 2515*5367Sahrens /* make sure prefix exists */ 2516*5367Sahrens cp = target + prefixlen; 2517*5367Sahrens if (*cp != '/') { 2518*5367Sahrens assert(strchr(cp, '/') == NULL); 2519*5367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2520*5367Sahrens } else { 2521*5367Sahrens *cp = '\0'; 2522*5367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2523*5367Sahrens *cp = '/'; 2524*5367Sahrens } 2525*5367Sahrens if (h == NULL) 2526*5367Sahrens return (-1); 2527*5367Sahrens zfs_close(h); 2528*5367Sahrens 2529*5367Sahrens /* 2530*5367Sahrens * Attempt to create, mount, and share any ancestor filesystems, 2531*5367Sahrens * up to the prefixlen-long one. 2532*5367Sahrens */ 2533*5367Sahrens for (cp = target + prefixlen + 1; 2534*5367Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 2535*5367Sahrens char *logstr; 2536*5367Sahrens 2537*5367Sahrens *cp = '\0'; 2538*5367Sahrens 2539*5367Sahrens h = make_dataset_handle(hdl, target); 2540*5367Sahrens if (h) { 2541*5367Sahrens /* it already exists, nothing to do here */ 2542*5367Sahrens zfs_close(h); 2543*5367Sahrens continue; 2544*5367Sahrens } 2545*5367Sahrens 2546*5367Sahrens logstr = hdl->libzfs_log_str; 2547*5367Sahrens hdl->libzfs_log_str = NULL; 2548*5367Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 2549*5367Sahrens NULL) != 0) { 2550*5367Sahrens hdl->libzfs_log_str = logstr; 2551*5367Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 2552*5367Sahrens goto ancestorerr; 2553*5367Sahrens } 2554*5367Sahrens 2555*5367Sahrens hdl->libzfs_log_str = logstr; 2556*5367Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 2557*5367Sahrens if (h == NULL) { 2558*5367Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 2559*5367Sahrens goto ancestorerr; 2560*5367Sahrens } 2561*5367Sahrens 2562*5367Sahrens if (zfs_mount(h, NULL, 0) != 0) { 2563*5367Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 2564*5367Sahrens goto ancestorerr; 2565*5367Sahrens } 2566*5367Sahrens 2567*5367Sahrens if (zfs_share(h) != 0) { 2568*5367Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 2569*5367Sahrens goto ancestorerr; 2570*5367Sahrens } 2571*5367Sahrens 2572*5367Sahrens zfs_close(h); 2573*5367Sahrens } 2574*5367Sahrens 2575*5367Sahrens return (0); 2576*5367Sahrens 2577*5367Sahrens ancestorerr: 2578*5367Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2579*5367Sahrens "failed to %s ancestor '%s'"), opname, target); 2580*5367Sahrens return (-1); 2581*5367Sahrens } 2582*5367Sahrens 2583*5367Sahrens /* 25844490Svb160487 * Creates non-existing ancestors of the given path. 25854490Svb160487 */ 25864490Svb160487 int 25874490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 25884490Svb160487 { 25894490Svb160487 int prefix; 25904490Svb160487 uint64_t zoned; 25914490Svb160487 char *path_copy; 25924490Svb160487 int rc; 25934490Svb160487 25944490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 25954490Svb160487 return (-1); 25964490Svb160487 25974490Svb160487 if ((path_copy = strdup(path)) != NULL) { 25984490Svb160487 rc = create_parents(hdl, path_copy, prefix); 25994490Svb160487 free(path_copy); 26004490Svb160487 } 26014490Svb160487 if (path_copy == NULL || rc != 0) 26024490Svb160487 return (-1); 26034490Svb160487 2604789Sahrens return (0); 2605789Sahrens } 2606789Sahrens 2607789Sahrens /* 26082676Seschrock * Create a new filesystem or volume. 2609789Sahrens */ 2610789Sahrens int 26112082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 26122676Seschrock nvlist_t *props) 2613789Sahrens { 2614789Sahrens zfs_cmd_t zc = { 0 }; 2615789Sahrens int ret; 2616789Sahrens uint64_t size = 0; 2617789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 26182082Seschrock char errbuf[1024]; 26192676Seschrock uint64_t zoned; 26202082Seschrock 26212082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 26222082Seschrock "cannot create '%s'"), path); 2623789Sahrens 2624789Sahrens /* validate the path, taking care to note the extended error message */ 26255326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 26262082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2627789Sahrens 2628789Sahrens /* validate parents exist */ 26294490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2630789Sahrens return (-1); 2631789Sahrens 2632789Sahrens /* 2633789Sahrens * The failure modes when creating a dataset of a different type over 2634789Sahrens * one that already exists is a little strange. In particular, if you 2635789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2636789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2637789Sahrens * first try to see if the dataset exists. 2638789Sahrens */ 2639789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 26405094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 26412082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26422082Seschrock "dataset already exists")); 26432082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2644789Sahrens } 2645789Sahrens 2646789Sahrens if (type == ZFS_TYPE_VOLUME) 2647789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2648789Sahrens else 2649789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2650789Sahrens 26515094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 26523912Slling zoned, NULL, errbuf)) == 0) 26532676Seschrock return (-1); 26542676Seschrock 2655789Sahrens if (type == ZFS_TYPE_VOLUME) { 26561133Seschrock /* 26571133Seschrock * If we are creating a volume, the size and block size must 26581133Seschrock * satisfy a few restraints. First, the blocksize must be a 26591133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 26601133Seschrock * volsize must be a multiple of the block size, and cannot be 26611133Seschrock * zero. 26621133Seschrock */ 26632676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 26642676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 26652676Seschrock nvlist_free(props); 26662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26672676Seschrock "missing volume size")); 26682676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2669789Sahrens } 2670789Sahrens 26712676Seschrock if ((ret = nvlist_lookup_uint64(props, 26722676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 26732676Seschrock &blocksize)) != 0) { 26742676Seschrock if (ret == ENOENT) { 26752676Seschrock blocksize = zfs_prop_default_numeric( 26762676Seschrock ZFS_PROP_VOLBLOCKSIZE); 26772676Seschrock } else { 26782676Seschrock nvlist_free(props); 26792676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26802676Seschrock "missing volume block size")); 26812676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26822676Seschrock } 26832676Seschrock } 26842676Seschrock 26852676Seschrock if (size == 0) { 26862676Seschrock nvlist_free(props); 26872082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26882676Seschrock "volume size cannot be zero")); 26892676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26901133Seschrock } 26911133Seschrock 26921133Seschrock if (size % blocksize != 0) { 26932676Seschrock nvlist_free(props); 26942082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26952676Seschrock "volume size must be a multiple of volume block " 26962676Seschrock "size")); 26972676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26981133Seschrock } 2699789Sahrens } 2700789Sahrens 27015094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 27022676Seschrock return (-1); 27032676Seschrock nvlist_free(props); 27042676Seschrock 2705789Sahrens /* create the dataset */ 27064543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2707789Sahrens 27083912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 27092082Seschrock ret = zvol_create_link(hdl, path); 27103912Slling if (ret) { 27113912Slling (void) zfs_standard_error(hdl, errno, 27123912Slling dgettext(TEXT_DOMAIN, 27133912Slling "Volume successfully created, but device links " 27143912Slling "were not created")); 27153912Slling zcmd_free_nvlists(&zc); 27163912Slling return (-1); 27173912Slling } 27183912Slling } 2719789Sahrens 27202676Seschrock zcmd_free_nvlists(&zc); 27212676Seschrock 2722789Sahrens /* check for failure */ 2723789Sahrens if (ret != 0) { 2724789Sahrens char parent[ZFS_MAXNAMELEN]; 2725789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2726789Sahrens 2727789Sahrens switch (errno) { 2728789Sahrens case ENOENT: 27292082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27302082Seschrock "no such parent '%s'"), parent); 27312082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2732789Sahrens 2733789Sahrens case EINVAL: 27342082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27353413Smmusante "parent '%s' is not a filesystem"), parent); 27362082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2737789Sahrens 2738789Sahrens case EDOM: 27392082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27402676Seschrock "volume block size must be power of 2 from " 27412676Seschrock "%u to %uk"), 2742789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2743789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 27442082Seschrock 27452676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 27462082Seschrock 27474603Sahrens case ENOTSUP: 27484603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27494603Sahrens "pool must be upgraded to set this " 27504603Sahrens "property or value")); 27514603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 27524603Sahrens 2753789Sahrens #ifdef _ILP32 2754789Sahrens case EOVERFLOW: 2755789Sahrens /* 2756789Sahrens * This platform can't address a volume this big. 2757789Sahrens */ 27582082Seschrock if (type == ZFS_TYPE_VOLUME) 27592082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 27602082Seschrock errbuf)); 2761789Sahrens #endif 27622082Seschrock /* FALLTHROUGH */ 2763789Sahrens default: 27642082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2765789Sahrens } 2766789Sahrens } 2767789Sahrens 2768789Sahrens return (0); 2769789Sahrens } 2770789Sahrens 2771789Sahrens /* 2772789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2773789Sahrens * isn't mounted, and that there are no active dependents. 2774789Sahrens */ 2775789Sahrens int 2776789Sahrens zfs_destroy(zfs_handle_t *zhp) 2777789Sahrens { 2778789Sahrens zfs_cmd_t zc = { 0 }; 2779789Sahrens 2780789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2781789Sahrens 27822676Seschrock if (ZFS_IS_VOLUME(zhp)) { 27833126Sahl /* 27844543Smarks * If user doesn't have permissions to unshare volume, then 27854543Smarks * abort the request. This would only happen for a 27864543Smarks * non-privileged user. 27873126Sahl */ 27884543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 27894543Smarks return (-1); 27904543Smarks } 27913126Sahl 27922082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2793789Sahrens return (-1); 2794789Sahrens 2795789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2796789Sahrens } else { 2797789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2798789Sahrens } 2799789Sahrens 28004543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 28013237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 28022082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 28032082Seschrock zhp->zfs_name)); 28042199Sahrens } 2805789Sahrens 2806789Sahrens remove_mountpoint(zhp); 2807789Sahrens 2808789Sahrens return (0); 2809789Sahrens } 2810789Sahrens 28112199Sahrens struct destroydata { 28122199Sahrens char *snapname; 28132199Sahrens boolean_t gotone; 28143265Sahrens boolean_t closezhp; 28152199Sahrens }; 28162199Sahrens 28172199Sahrens static int 28182199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 28192199Sahrens { 28202199Sahrens struct destroydata *dd = arg; 28212199Sahrens zfs_handle_t *szhp; 28222199Sahrens char name[ZFS_MAXNAMELEN]; 28233265Sahrens boolean_t closezhp = dd->closezhp; 28243265Sahrens int rv; 28252199Sahrens 28262676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 28272676Seschrock (void) strlcat(name, "@", sizeof (name)); 28282676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 28292199Sahrens 28302199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 28312199Sahrens if (szhp) { 28322199Sahrens dd->gotone = B_TRUE; 28332199Sahrens zfs_close(szhp); 28342199Sahrens } 28352199Sahrens 28362199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 28372199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 28382199Sahrens /* 28392199Sahrens * NB: this is simply a best-effort. We don't want to 28402199Sahrens * return an error, because then we wouldn't visit all 28412199Sahrens * the volumes. 28422199Sahrens */ 28432199Sahrens } 28442199Sahrens 28453265Sahrens dd->closezhp = B_TRUE; 28463265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 28473265Sahrens if (closezhp) 28483265Sahrens zfs_close(zhp); 28493265Sahrens return (rv); 28502199Sahrens } 28512199Sahrens 28522199Sahrens /* 28532199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 28542199Sahrens */ 28552199Sahrens int 28562199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 28572199Sahrens { 28582199Sahrens zfs_cmd_t zc = { 0 }; 28592199Sahrens int ret; 28602199Sahrens struct destroydata dd = { 0 }; 28612199Sahrens 28622199Sahrens dd.snapname = snapname; 28632199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 28642199Sahrens 28652199Sahrens if (!dd.gotone) { 28663237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 28672199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 28682199Sahrens zhp->zfs_name, snapname)); 28692199Sahrens } 28702199Sahrens 28712199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 28722676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 28732199Sahrens 28744543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 28752199Sahrens if (ret != 0) { 28762199Sahrens char errbuf[1024]; 28772199Sahrens 28782199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 28792199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 28802199Sahrens 28812199Sahrens switch (errno) { 28822199Sahrens case EEXIST: 28832199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28842199Sahrens "snapshot is cloned")); 28852199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 28862199Sahrens 28872199Sahrens default: 28882199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 28892199Sahrens errbuf)); 28902199Sahrens } 28912199Sahrens } 28922199Sahrens 28932199Sahrens return (0); 28942199Sahrens } 28952199Sahrens 2896789Sahrens /* 2897789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2898789Sahrens */ 2899789Sahrens int 29002676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2901789Sahrens { 2902789Sahrens zfs_cmd_t zc = { 0 }; 2903789Sahrens char parent[ZFS_MAXNAMELEN]; 2904789Sahrens int ret; 29052082Seschrock char errbuf[1024]; 29062082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 29072676Seschrock zfs_type_t type; 29082676Seschrock uint64_t zoned; 2909789Sahrens 2910789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2911789Sahrens 29122082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29132082Seschrock "cannot create '%s'"), target); 29142082Seschrock 2915789Sahrens /* validate the target name */ 29165326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 29172082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2918789Sahrens 2919789Sahrens /* validate parents exist */ 29204490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2921789Sahrens return (-1); 2922789Sahrens 2923789Sahrens (void) parent_name(target, parent, sizeof (parent)); 2924789Sahrens 2925789Sahrens /* do the clone */ 29262676Seschrock if (ZFS_IS_VOLUME(zhp)) { 2927789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 29282744Snn35248 type = ZFS_TYPE_VOLUME; 29292676Seschrock } else { 2930789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 29312744Snn35248 type = ZFS_TYPE_FILESYSTEM; 29322676Seschrock } 29332676Seschrock 29342676Seschrock if (props) { 29355094Slling if ((props = zfs_validate_properties(hdl, type, props, 29363912Slling zoned, zhp, errbuf)) == NULL) 29372676Seschrock return (-1); 29382676Seschrock 29395094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 29402676Seschrock nvlist_free(props); 29412676Seschrock return (-1); 29422676Seschrock } 29432676Seschrock 29442676Seschrock nvlist_free(props); 29452676Seschrock } 2946789Sahrens 2947789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 29482676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 29494543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2950789Sahrens 29512676Seschrock zcmd_free_nvlists(&zc); 29522676Seschrock 2953789Sahrens if (ret != 0) { 2954789Sahrens switch (errno) { 2955789Sahrens 2956789Sahrens case ENOENT: 2957789Sahrens /* 2958789Sahrens * The parent doesn't exist. We should have caught this 2959789Sahrens * above, but there may a race condition that has since 2960789Sahrens * destroyed the parent. 2961789Sahrens * 2962789Sahrens * At this point, we don't know whether it's the source 2963789Sahrens * that doesn't exist anymore, or whether the target 2964789Sahrens * dataset doesn't exist. 2965789Sahrens */ 29662082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29672082Seschrock "no such parent '%s'"), parent); 29682082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 29692082Seschrock 29702082Seschrock case EXDEV: 29712082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 29722082Seschrock "source and target pools differ")); 29732082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 29742082Seschrock errbuf)); 29752082Seschrock 29762082Seschrock default: 29772082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 29782082Seschrock errbuf)); 29792082Seschrock } 29802676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 29812082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 29822082Seschrock } 29832082Seschrock 29842082Seschrock return (ret); 29852082Seschrock } 29862082Seschrock 29872082Seschrock typedef struct promote_data { 29882082Seschrock char cb_mountpoint[MAXPATHLEN]; 29892082Seschrock const char *cb_target; 29902082Seschrock const char *cb_errbuf; 29912082Seschrock uint64_t cb_pivot_txg; 29922082Seschrock } promote_data_t; 29932082Seschrock 29942082Seschrock static int 29952082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 29962082Seschrock { 29972082Seschrock promote_data_t *pd = data; 29982082Seschrock zfs_handle_t *szhp; 29992082Seschrock char snapname[MAXPATHLEN]; 30003265Sahrens int rv = 0; 30012082Seschrock 30022082Seschrock /* We don't care about snapshots after the pivot point */ 30033265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 30043265Sahrens zfs_close(zhp); 30052082Seschrock return (0); 30063265Sahrens } 30072082Seschrock 30082417Sahrens /* Remove the device link if it's a zvol. */ 30092676Seschrock if (ZFS_IS_VOLUME(zhp)) 30102417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 30112082Seschrock 30122082Seschrock /* Check for conflicting names */ 30132676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 30142676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 30152082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 30162082Seschrock if (szhp != NULL) { 30172082Seschrock zfs_close(szhp); 30182082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 30192082Seschrock "snapshot name '%s' from origin \n" 30202082Seschrock "conflicts with '%s' from target"), 30212082Seschrock zhp->zfs_name, snapname); 30223265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 30232082Seschrock } 30243265Sahrens zfs_close(zhp); 30253265Sahrens return (rv); 30262082Seschrock } 30272082Seschrock 30282417Sahrens static int 30292417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 30302417Sahrens { 30312417Sahrens promote_data_t *pd = data; 30322417Sahrens 30332417Sahrens /* We don't care about snapshots after the pivot point */ 30343265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 30353265Sahrens /* Create the device link if it's a zvol. */ 30363265Sahrens if (ZFS_IS_VOLUME(zhp)) 30373265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 30383265Sahrens } 30393265Sahrens 30403265Sahrens zfs_close(zhp); 30412417Sahrens return (0); 30422417Sahrens } 30432417Sahrens 30442082Seschrock /* 30452082Seschrock * Promotes the given clone fs to be the clone parent. 30462082Seschrock */ 30472082Seschrock int 30482082Seschrock zfs_promote(zfs_handle_t *zhp) 30492082Seschrock { 30502082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 30512082Seschrock zfs_cmd_t zc = { 0 }; 30522082Seschrock char parent[MAXPATHLEN]; 30532082Seschrock char *cp; 30542082Seschrock int ret; 30552082Seschrock zfs_handle_t *pzhp; 30562082Seschrock promote_data_t pd; 30572082Seschrock char errbuf[1024]; 30582082Seschrock 30592082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30602082Seschrock "cannot promote '%s'"), zhp->zfs_name); 30612082Seschrock 30622082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 30632082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30642082Seschrock "snapshots can not be promoted")); 30652082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 30662082Seschrock } 30672082Seschrock 3068*5367Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 30692082Seschrock if (parent[0] == '\0') { 30702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30712082Seschrock "not a cloned filesystem")); 30722082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 30732082Seschrock } 30742082Seschrock cp = strchr(parent, '@'); 30752082Seschrock *cp = '\0'; 30762082Seschrock 30772082Seschrock /* Walk the snapshots we will be moving */ 3078*5367Sahrens pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT); 30792082Seschrock if (pzhp == NULL) 30802082Seschrock return (-1); 30812082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 30822082Seschrock zfs_close(pzhp); 30832082Seschrock pd.cb_target = zhp->zfs_name; 30842082Seschrock pd.cb_errbuf = errbuf; 30855094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 30862082Seschrock if (pzhp == NULL) 30872082Seschrock return (-1); 30882082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 30892082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 30902082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 30912417Sahrens if (ret != 0) { 30922417Sahrens zfs_close(pzhp); 30932082Seschrock return (-1); 30942417Sahrens } 30952082Seschrock 30962082Seschrock /* issue the ioctl */ 3097*5367Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 30982676Seschrock sizeof (zc.zc_value)); 30992082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31004543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 31012082Seschrock 31022082Seschrock if (ret != 0) { 31032417Sahrens int save_errno = errno; 31042417Sahrens 31052417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 31062417Sahrens zfs_close(pzhp); 31072417Sahrens 31082417Sahrens switch (save_errno) { 3109789Sahrens case EEXIST: 3110789Sahrens /* 31112082Seschrock * There is a conflicting snapshot name. We 31122082Seschrock * should have caught this above, but they could 31132082Seschrock * have renamed something in the mean time. 3114789Sahrens */ 31152082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31162082Seschrock "conflicting snapshot name from parent '%s'"), 31172082Seschrock parent); 31182082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3119789Sahrens 3120789Sahrens default: 31212417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3122789Sahrens } 31232417Sahrens } else { 31242417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 3125789Sahrens } 3126789Sahrens 31272417Sahrens zfs_close(pzhp); 3128789Sahrens return (ret); 3129789Sahrens } 3130789Sahrens 31314007Smmusante struct createdata { 31324007Smmusante const char *cd_snapname; 31334007Smmusante int cd_ifexists; 31344007Smmusante }; 31354007Smmusante 31362199Sahrens static int 31372199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 31382199Sahrens { 31394007Smmusante struct createdata *cd = arg; 31402676Seschrock int ret; 31412199Sahrens 31422199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 31432199Sahrens char name[MAXPATHLEN]; 31442199Sahrens 31452676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 31462676Seschrock (void) strlcat(name, "@", sizeof (name)); 31474007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 31484007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 31494007Smmusante cd->cd_ifexists); 31502199Sahrens /* 31512199Sahrens * NB: this is simply a best-effort. We don't want to 31522199Sahrens * return an error, because then we wouldn't visit all 31532199Sahrens * the volumes. 31542199Sahrens */ 31552199Sahrens } 31562676Seschrock 31574007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 31582676Seschrock 31592676Seschrock zfs_close(zhp); 31602676Seschrock 31612676Seschrock return (ret); 31622199Sahrens } 31632199Sahrens 3164789Sahrens /* 31653504Sahl * Takes a snapshot of the given dataset. 3166789Sahrens */ 3167789Sahrens int 31682199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3169789Sahrens { 3170789Sahrens const char *delim; 3171789Sahrens char *parent; 3172789Sahrens zfs_handle_t *zhp; 3173789Sahrens zfs_cmd_t zc = { 0 }; 3174789Sahrens int ret; 31752082Seschrock char errbuf[1024]; 31762082Seschrock 31772082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31782082Seschrock "cannot snapshot '%s'"), path); 31792082Seschrock 31802082Seschrock /* validate the target name */ 31815326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 31822082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3183789Sahrens 3184789Sahrens /* make sure the parent exists and is of the appropriate type */ 31852199Sahrens delim = strchr(path, '@'); 31862082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 31872082Seschrock return (-1); 3188789Sahrens (void) strncpy(parent, path, delim - path); 3189789Sahrens parent[delim - path] = '\0'; 3190789Sahrens 31912082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3192789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3193789Sahrens free(parent); 3194789Sahrens return (-1); 3195789Sahrens } 3196789Sahrens 31972199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31982676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 31994543Smarks if (ZFS_IS_VOLUME(zhp)) 32004543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 32014543Smarks else 32024543Smarks zc.zc_objset_type = DMU_OST_ZFS; 32032199Sahrens zc.zc_cookie = recursive; 32044543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 32052199Sahrens 32062199Sahrens /* 32072199Sahrens * if it was recursive, the one that actually failed will be in 32082199Sahrens * zc.zc_name. 32092199Sahrens */ 32104543Smarks if (ret != 0) 32114543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32124543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 32134543Smarks 32142199Sahrens if (ret == 0 && recursive) { 32154007Smmusante struct createdata cd; 32164007Smmusante 32174007Smmusante cd.cd_snapname = delim + 1; 32184007Smmusante cd.cd_ifexists = B_FALSE; 32194007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 32202199Sahrens } 3221789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 32222082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 32232199Sahrens if (ret != 0) { 32244543Smarks (void) zfs_standard_error(hdl, errno, 32254543Smarks dgettext(TEXT_DOMAIN, 32264543Smarks "Volume successfully snapshotted, but device links " 32274543Smarks "were not created")); 32284543Smarks free(parent); 32294543Smarks zfs_close(zhp); 32304543Smarks return (-1); 32312199Sahrens } 3232789Sahrens } 3233789Sahrens 32342082Seschrock if (ret != 0) 32352082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3236789Sahrens 3237789Sahrens free(parent); 3238789Sahrens zfs_close(zhp); 3239789Sahrens 3240789Sahrens return (ret); 3241789Sahrens } 3242789Sahrens 3243789Sahrens /* 32441294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 32451294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 32461294Slling * is a dependent and we should just destroy it without checking the transaction 32471294Slling * group. 3248789Sahrens */ 32491294Slling typedef struct rollback_data { 32501294Slling const char *cb_target; /* the snapshot */ 32511294Slling uint64_t cb_create; /* creation time reference */ 32521294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 32531294Slling int cb_error; 32542082Seschrock boolean_t cb_dependent; 32551294Slling } rollback_data_t; 32561294Slling 32571294Slling static int 32581294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 32591294Slling { 32601294Slling rollback_data_t *cbp = data; 32611294Slling 32621294Slling if (!cbp->cb_dependent) { 32631294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 32641294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 32651294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 32661294Slling cbp->cb_create) { 32674543Smarks char *logstr; 32681294Slling 32692082Seschrock cbp->cb_dependent = B_TRUE; 32702474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 32712474Seschrock cbp) != 0) 32722474Seschrock cbp->cb_error = 1; 32732082Seschrock cbp->cb_dependent = B_FALSE; 32741294Slling 32754543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 32764543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 32771294Slling if (zfs_destroy(zhp) != 0) 32781294Slling cbp->cb_error = 1; 32791294Slling else 3280*5367Sahrens changelist_remove(cbp->cb_clp, zhp->zfs_name); 32814543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 32821294Slling } 32831294Slling } else { 32841294Slling if (zfs_destroy(zhp) != 0) 32851294Slling cbp->cb_error = 1; 32861294Slling else 3287*5367Sahrens changelist_remove(cbp->cb_clp, zhp->zfs_name); 32881294Slling } 32891294Slling 32901294Slling zfs_close(zhp); 32911294Slling return (0); 32921294Slling } 32931294Slling 32941294Slling /* 32951294Slling * Rollback the dataset to its latest snapshot. 32961294Slling */ 32971294Slling static int 32981294Slling do_rollback(zfs_handle_t *zhp) 3299789Sahrens { 3300789Sahrens int ret; 3301789Sahrens zfs_cmd_t zc = { 0 }; 3302789Sahrens 3303789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3304789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3305789Sahrens 3306789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 33072082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3308789Sahrens return (-1); 3309789Sahrens 3310789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3311789Sahrens 33122676Seschrock if (ZFS_IS_VOLUME(zhp)) 3313789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3314789Sahrens else 3315789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3316789Sahrens 3317789Sahrens /* 3318789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3319789Sahrens * for the given dataset. Given these constraints, we can simply pass 3320789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3321789Sahrens * condition where the user has taken a snapshot since we verified that 3322789Sahrens * this was the most recent. 3323789Sahrens */ 33244543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 33253237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 33262082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 33272082Seschrock zhp->zfs_name); 3328789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 33292082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3330789Sahrens } 3331789Sahrens 3332789Sahrens return (ret); 3333789Sahrens } 3334789Sahrens 3335789Sahrens /* 33361294Slling * Given a dataset, rollback to a specific snapshot, discarding any 33371294Slling * data changes since then and making it the active dataset. 33381294Slling * 33391294Slling * Any snapshots more recent than the target are destroyed, along with 33401294Slling * their dependents. 33411294Slling */ 33421294Slling int 33431294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 33441294Slling { 33451294Slling int ret; 33461294Slling rollback_data_t cb = { 0 }; 33471294Slling prop_changelist_t *clp; 33481294Slling 33491294Slling /* 33501294Slling * Unmount all dependendents of the dataset and the dataset itself. 33511294Slling * The list we need to gather is the same as for doing rename 33521294Slling */ 33531294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 33541294Slling if (clp == NULL) 33551294Slling return (-1); 33561294Slling 33571294Slling if ((ret = changelist_prefix(clp)) != 0) 33581294Slling goto out; 33591294Slling 33601294Slling /* 33611294Slling * Destroy all recent snapshots and its dependends. 33621294Slling */ 33631294Slling cb.cb_target = snap->zfs_name; 33641294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 33651294Slling cb.cb_clp = clp; 33661294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 33671294Slling 33681294Slling if ((ret = cb.cb_error) != 0) { 33691294Slling (void) changelist_postfix(clp); 33701294Slling goto out; 33711294Slling } 33721294Slling 33731294Slling /* 33741294Slling * Now that we have verified that the snapshot is the latest, 33751294Slling * rollback to the given snapshot. 33761294Slling */ 33771294Slling ret = do_rollback(zhp); 33781294Slling 33791294Slling if (ret != 0) { 33801294Slling (void) changelist_postfix(clp); 33811294Slling goto out; 33821294Slling } 33831294Slling 33841294Slling /* 33851294Slling * We only want to re-mount the filesystem if it was mounted in the 33861294Slling * first place. 33871294Slling */ 33881294Slling ret = changelist_postfix(clp); 33891294Slling 33901294Slling out: 33911294Slling changelist_free(clp); 33921294Slling return (ret); 33931294Slling } 33941294Slling 33951294Slling /* 3396789Sahrens * Iterate over all dependents for a given dataset. This includes both 3397789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3398789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3399789Sahrens * libzfs_graph.c. 3400789Sahrens */ 3401789Sahrens int 34022474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 34032474Seschrock zfs_iter_f func, void *data) 3404789Sahrens { 3405789Sahrens char **dependents; 3406789Sahrens size_t count; 3407789Sahrens int i; 3408789Sahrens zfs_handle_t *child; 3409789Sahrens int ret = 0; 3410789Sahrens 34112474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 34122474Seschrock &dependents, &count) != 0) 34132474Seschrock return (-1); 34142474Seschrock 3415789Sahrens for (i = 0; i < count; i++) { 34162082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 34172082Seschrock dependents[i])) == NULL) 3418789Sahrens continue; 3419789Sahrens 3420789Sahrens if ((ret = func(child, data)) != 0) 3421789Sahrens break; 3422789Sahrens } 3423789Sahrens 3424789Sahrens for (i = 0; i < count; i++) 3425789Sahrens free(dependents[i]); 3426789Sahrens free(dependents); 3427789Sahrens 3428789Sahrens return (ret); 3429789Sahrens } 3430789Sahrens 3431789Sahrens /* 3432789Sahrens * Renames the given dataset. 3433789Sahrens */ 3434789Sahrens int 34354490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3436789Sahrens { 3437789Sahrens int ret; 3438789Sahrens zfs_cmd_t zc = { 0 }; 3439789Sahrens char *delim; 34404007Smmusante prop_changelist_t *cl = NULL; 34414007Smmusante zfs_handle_t *zhrp = NULL; 34424007Smmusante char *parentname = NULL; 3443789Sahrens char parent[ZFS_MAXNAMELEN]; 34442082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 34452082Seschrock char errbuf[1024]; 3446789Sahrens 3447789Sahrens /* if we have the same exact name, just return success */ 3448789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3449789Sahrens return (0); 3450789Sahrens 34512082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34522082Seschrock "cannot rename to '%s'"), target); 34532082Seschrock 3454789Sahrens /* 3455789Sahrens * Make sure the target name is valid 3456789Sahrens */ 3457789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 34582665Snd150628 if ((strchr(target, '@') == NULL) || 34592665Snd150628 *target == '@') { 34602665Snd150628 /* 34612665Snd150628 * Snapshot target name is abbreviated, 34622665Snd150628 * reconstruct full dataset name 34632665Snd150628 */ 34642665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 34652665Snd150628 sizeof (parent)); 34662665Snd150628 delim = strchr(parent, '@'); 34672665Snd150628 if (strchr(target, '@') == NULL) 34682665Snd150628 *(++delim) = '\0'; 34692665Snd150628 else 34702665Snd150628 *delim = '\0'; 34712665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 34722665Snd150628 target = parent; 34732665Snd150628 } else { 34742665Snd150628 /* 34752665Snd150628 * Make sure we're renaming within the same dataset. 34762665Snd150628 */ 34772665Snd150628 delim = strchr(target, '@'); 34782665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 34792665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 34802665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34812665Snd150628 "snapshots must be part of same " 34822665Snd150628 "dataset")); 34832665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 34843912Slling errbuf)); 34852665Snd150628 } 3486789Sahrens } 34875326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 34882665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3489789Sahrens } else { 34904007Smmusante if (recursive) { 34914007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34924007Smmusante "recursive rename must be a snapshot")); 34934007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 34944007Smmusante } 34954007Smmusante 34965326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 34972665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 34982676Seschrock uint64_t unused; 34992676Seschrock 3500789Sahrens /* validate parents */ 35014490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3502789Sahrens return (-1); 3503789Sahrens 3504789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3505789Sahrens 3506789Sahrens /* make sure we're in the same pool */ 3507789Sahrens verify((delim = strchr(target, '/')) != NULL); 3508789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3509789Sahrens zhp->zfs_name[delim - target] != '/') { 35102082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35112082Seschrock "datasets must be within same pool")); 35122082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3513789Sahrens } 35142440Snd150628 35152440Snd150628 /* new name cannot be a child of the current dataset name */ 35162440Snd150628 if (strncmp(parent, zhp->zfs_name, 35173912Slling strlen(zhp->zfs_name)) == 0) { 35182440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35192440Snd150628 "New dataset name cannot be a descendent of " 35202440Snd150628 "current dataset name")); 35212440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 35222440Snd150628 } 3523789Sahrens } 3524789Sahrens 35252082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 35262082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 35272082Seschrock 3528789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3529789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 35302082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35312082Seschrock "dataset is used in a non-global zone")); 35322082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3533789Sahrens } 3534789Sahrens 35354007Smmusante if (recursive) { 35364007Smmusante struct destroydata dd; 35374007Smmusante 35384183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 35394183Smmusante if (parentname == NULL) { 35404183Smmusante ret = -1; 35414183Smmusante goto error; 35424183Smmusante } 35434007Smmusante delim = strchr(parentname, '@'); 35444007Smmusante *delim = '\0'; 35455094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 35464007Smmusante if (zhrp == NULL) { 35474183Smmusante ret = -1; 35484183Smmusante goto error; 35494007Smmusante } 35504007Smmusante 35514007Smmusante dd.snapname = delim + 1; 35524007Smmusante dd.gotone = B_FALSE; 35534183Smmusante dd.closezhp = B_TRUE; 35544007Smmusante 35554007Smmusante /* We remove any zvol links prior to renaming them */ 35564007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 35574007Smmusante if (ret) { 35584007Smmusante goto error; 35594007Smmusante } 35604007Smmusante } else { 35614007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 35624007Smmusante return (-1); 35634007Smmusante 35644007Smmusante if (changelist_haszonedchild(cl)) { 35654007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35664007Smmusante "child dataset with inherited mountpoint is used " 35674007Smmusante "in a non-global zone")); 35684007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 35694007Smmusante goto error; 35704007Smmusante } 35714007Smmusante 35724007Smmusante if ((ret = changelist_prefix(cl)) != 0) 35734007Smmusante goto error; 3574789Sahrens } 3575789Sahrens 35762676Seschrock if (ZFS_IS_VOLUME(zhp)) 3577789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3578789Sahrens else 3579789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3580789Sahrens 35812665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35822676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 35832665Snd150628 35844007Smmusante zc.zc_cookie = recursive; 35854007Smmusante 35864543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 35874007Smmusante /* 35884007Smmusante * if it was recursive, the one that actually failed will 35894007Smmusante * be in zc.zc_name 35904007Smmusante */ 35914007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 3592*5367Sahrens "cannot rename '%s'"), zc.zc_name); 35934007Smmusante 35944007Smmusante if (recursive && errno == EEXIST) { 35954007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 35964007Smmusante "a child dataset already has a snapshot " 35974007Smmusante "with the new name")); 35984801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 35994007Smmusante } else { 36004007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 36014007Smmusante } 3602789Sahrens 3603789Sahrens /* 3604789Sahrens * On failure, we still want to remount any filesystems that 3605789Sahrens * were previously mounted, so we don't alter the system state. 3606789Sahrens */ 36074007Smmusante if (recursive) { 36084007Smmusante struct createdata cd; 36094007Smmusante 36104007Smmusante /* only create links for datasets that had existed */ 36114007Smmusante cd.cd_snapname = delim + 1; 36124007Smmusante cd.cd_ifexists = B_TRUE; 36134007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36144007Smmusante &cd); 36154007Smmusante } else { 36164007Smmusante (void) changelist_postfix(cl); 36174007Smmusante } 3618789Sahrens } else { 36194007Smmusante if (recursive) { 36204007Smmusante struct createdata cd; 36214007Smmusante 36224007Smmusante /* only create links for datasets that had existed */ 36234007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 36244007Smmusante cd.cd_ifexists = B_TRUE; 36254007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 36264007Smmusante &cd); 36274007Smmusante } else { 36284007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 36294007Smmusante ret = changelist_postfix(cl); 36304007Smmusante } 3631789Sahrens } 3632789Sahrens 3633789Sahrens error: 36344007Smmusante if (parentname) { 36354007Smmusante free(parentname); 36364007Smmusante } 36374007Smmusante if (zhrp) { 36384007Smmusante zfs_close(zhrp); 36394007Smmusante } 36404007Smmusante if (cl) { 36414007Smmusante changelist_free(cl); 36424007Smmusante } 3643789Sahrens return (ret); 3644789Sahrens } 3645789Sahrens 3646789Sahrens /* 3647789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3648789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3649789Sahrens */ 3650789Sahrens int 36512082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3652789Sahrens { 36534007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 36544007Smmusante } 36554007Smmusante 36564007Smmusante static int 36574007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 36584007Smmusante { 3659789Sahrens zfs_cmd_t zc = { 0 }; 36602082Seschrock di_devlink_handle_t dhdl; 36614543Smarks priv_set_t *priv_effective; 36624543Smarks int privileged; 3663789Sahrens 3664789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3665789Sahrens 3666789Sahrens /* 3667789Sahrens * Issue the appropriate ioctl. 3668789Sahrens */ 36692082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3670789Sahrens switch (errno) { 3671789Sahrens case EEXIST: 3672789Sahrens /* 3673789Sahrens * Silently ignore the case where the link already 3674789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3675789Sahrens * times without errors. 3676789Sahrens */ 3677789Sahrens return (0); 3678789Sahrens 36794007Smmusante case ENOENT: 36804007Smmusante /* 36814007Smmusante * Dataset does not exist in the kernel. If we 36824007Smmusante * don't care (see zfs_rename), then ignore the 36834007Smmusante * error quietly. 36844007Smmusante */ 36854007Smmusante if (ifexists) { 36864007Smmusante return (0); 36874007Smmusante } 36884007Smmusante 36894007Smmusante /* FALLTHROUGH */ 36904007Smmusante 3691789Sahrens default: 36923237Slling return (zfs_standard_error_fmt(hdl, errno, 36932082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 36942082Seschrock "for '%s'"), dataset)); 3695789Sahrens } 3696789Sahrens } 3697789Sahrens 3698789Sahrens /* 36994543Smarks * If privileged call devfsadm and wait for the links to 37004543Smarks * magically appear. 37014543Smarks * Otherwise, print out an informational message. 3702789Sahrens */ 37034543Smarks 37044543Smarks priv_effective = priv_allocset(); 37054543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 37064543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 37074543Smarks priv_freeset(priv_effective); 37084543Smarks 37094543Smarks if (privileged) { 37104543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 37114543Smarks DI_MAKE_LINK)) == NULL) { 37124543Smarks zfs_error_aux(hdl, strerror(errno)); 37134543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 37144543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 37154543Smarks "for '%s'"), dataset); 37164543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 37174543Smarks return (-1); 37184543Smarks } else { 37194543Smarks (void) di_devlink_fini(&dhdl); 37204543Smarks } 3721789Sahrens } else { 37224543Smarks char pathname[MAXPATHLEN]; 37234543Smarks struct stat64 statbuf; 37244543Smarks int i; 37254543Smarks 37264543Smarks #define MAX_WAIT 10 37274543Smarks 37284543Smarks /* 37294543Smarks * This is the poor mans way of waiting for the link 37304543Smarks * to show up. If after 10 seconds we still don't 37314543Smarks * have it, then print out a message. 37324543Smarks */ 37334543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 37344543Smarks dataset); 37354543Smarks 37364543Smarks for (i = 0; i != MAX_WAIT; i++) { 37374543Smarks if (stat64(pathname, &statbuf) == 0) 37384543Smarks break; 37394543Smarks (void) sleep(1); 37404543Smarks } 37414543Smarks if (i == MAX_WAIT) 37424543Smarks (void) printf(gettext("%s may not be immediately " 37434543Smarks "available\n"), pathname); 3744789Sahrens } 3745789Sahrens 3746789Sahrens return (0); 3747789Sahrens } 3748789Sahrens 3749789Sahrens /* 3750789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 3751789Sahrens */ 3752789Sahrens int 37532082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 3754789Sahrens { 3755789Sahrens zfs_cmd_t zc = { 0 }; 3756789Sahrens 3757789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3758789Sahrens 37592082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 3760789Sahrens switch (errno) { 3761789Sahrens case ENXIO: 3762789Sahrens /* 3763789Sahrens * Silently ignore the case where the link no longer 3764789Sahrens * exists, so that 'zfs volfini' can be run multiple 3765789Sahrens * times without errors. 3766789Sahrens */ 3767789Sahrens return (0); 3768789Sahrens 3769789Sahrens default: 37703237Slling return (zfs_standard_error_fmt(hdl, errno, 37712082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 37722082Seschrock "links for '%s'"), dataset)); 3773789Sahrens } 3774789Sahrens } 3775789Sahrens 3776789Sahrens return (0); 3777789Sahrens } 37782676Seschrock 37792676Seschrock nvlist_t * 37802676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 37812676Seschrock { 37822676Seschrock return (zhp->zfs_user_props); 37832676Seschrock } 37842676Seschrock 37852676Seschrock /* 37863912Slling * This function is used by 'zfs list' to determine the exact set of columns to 37873912Slling * display, and their maximum widths. This does two main things: 37883912Slling * 37893912Slling * - If this is a list of all properties, then expand the list to include 37903912Slling * all native properties, and set a flag so that for each dataset we look 37913912Slling * for new unique user properties and add them to the list. 37923912Slling * 37933912Slling * - For non fixed-width properties, keep track of the maximum width seen 37943912Slling * so that we can size the column appropriately. 37953912Slling */ 37963912Slling int 37975094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 37983912Slling { 37993912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 38005094Slling zprop_list_t *entry; 38015094Slling zprop_list_t **last, **start; 38023912Slling nvlist_t *userprops, *propval; 38033912Slling nvpair_t *elem; 38043912Slling char *strval; 38053912Slling char buf[ZFS_MAXPROPLEN]; 38063912Slling 38075094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 38083912Slling return (-1); 38092676Seschrock 38102676Seschrock userprops = zfs_get_user_props(zhp); 38112676Seschrock 38122676Seschrock entry = *plp; 38132676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 38142676Seschrock /* 38152676Seschrock * Go through and add any user properties as necessary. We 38162676Seschrock * start by incrementing our list pointer to the first 38172676Seschrock * non-native property. 38182676Seschrock */ 38192676Seschrock start = plp; 38202676Seschrock while (*start != NULL) { 38215094Slling if ((*start)->pl_prop == ZPROP_INVAL) 38222676Seschrock break; 38232676Seschrock start = &(*start)->pl_next; 38242676Seschrock } 38252676Seschrock 38262676Seschrock elem = NULL; 38272676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 38282676Seschrock /* 38292676Seschrock * See if we've already found this property in our list. 38302676Seschrock */ 38312676Seschrock for (last = start; *last != NULL; 38322676Seschrock last = &(*last)->pl_next) { 38332676Seschrock if (strcmp((*last)->pl_user_prop, 38342676Seschrock nvpair_name(elem)) == 0) 38352676Seschrock break; 38362676Seschrock } 38372676Seschrock 38382676Seschrock if (*last == NULL) { 38392676Seschrock if ((entry = zfs_alloc(hdl, 38405094Slling sizeof (zprop_list_t))) == NULL || 38412676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 38422676Seschrock nvpair_name(elem)))) == NULL) { 38432676Seschrock free(entry); 38442676Seschrock return (-1); 38452676Seschrock } 38462676Seschrock 38475094Slling entry->pl_prop = ZPROP_INVAL; 38482676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 38492676Seschrock entry->pl_all = B_TRUE; 38502676Seschrock *last = entry; 38512676Seschrock } 38522676Seschrock } 38532676Seschrock } 38542676Seschrock 38552676Seschrock /* 38562676Seschrock * Now go through and check the width of any non-fixed columns 38572676Seschrock */ 38582676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 38592676Seschrock if (entry->pl_fixed) 38602676Seschrock continue; 38612676Seschrock 38625094Slling if (entry->pl_prop != ZPROP_INVAL) { 38632676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 38642676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 38652676Seschrock if (strlen(buf) > entry->pl_width) 38662676Seschrock entry->pl_width = strlen(buf); 38672676Seschrock } 38682676Seschrock } else if (nvlist_lookup_nvlist(userprops, 38692676Seschrock entry->pl_user_prop, &propval) == 0) { 38702676Seschrock verify(nvlist_lookup_string(propval, 38715094Slling ZPROP_VALUE, &strval) == 0); 38722676Seschrock if (strlen(strval) > entry->pl_width) 38732676Seschrock entry->pl_width = strlen(strval); 38742676Seschrock } 38752676Seschrock } 38762676Seschrock 38772676Seschrock return (0); 38782676Seschrock } 38794543Smarks 38804543Smarks int 38814543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 38824543Smarks { 38834543Smarks zfs_cmd_t zc = { 0 }; 38844543Smarks nvlist_t *nvp; 38854543Smarks gid_t gid; 38864543Smarks uid_t uid; 38874543Smarks const gid_t *groups; 38884543Smarks int group_cnt; 38894543Smarks int error; 38904543Smarks 38914543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 38924543Smarks return (no_memory(hdl)); 38934543Smarks 38944543Smarks uid = ucred_geteuid(cred); 38954543Smarks gid = ucred_getegid(cred); 38964543Smarks group_cnt = ucred_getgroups(cred, &groups); 38974543Smarks 38984543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 38994543Smarks return (1); 39004543Smarks 39014543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 39024543Smarks nvlist_free(nvp); 39034543Smarks return (1); 39044543Smarks } 39054543Smarks 39064543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 39074543Smarks nvlist_free(nvp); 39084543Smarks return (1); 39094543Smarks } 39104543Smarks 39114543Smarks if (nvlist_add_uint32_array(nvp, 39124543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 39134543Smarks nvlist_free(nvp); 39144543Smarks return (1); 39154543Smarks } 39164543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39174543Smarks 39185094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 39194543Smarks return (-1); 39204543Smarks 39214543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 39224543Smarks nvlist_free(nvp); 39234543Smarks return (error); 39244543Smarks } 39254543Smarks 39264543Smarks int 39274543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 39285331Samw void *export, void *sharetab, int sharemax, zfs_share_op_t operation) 39294543Smarks { 39304543Smarks zfs_cmd_t zc = { 0 }; 39314543Smarks int error; 39324543Smarks 39334543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 39344543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 39354543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 39364543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 39375331Samw zc.zc_share.z_sharetype = operation; 39384543Smarks zc.zc_share.z_sharemax = sharemax; 39394543Smarks 39404543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 39414543Smarks return (error); 39424543Smarks } 3943