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> 39789Sahrens #include <zone.h> 402082Seschrock #include <fcntl.h> 41789Sahrens #include <sys/mntent.h> 42789Sahrens #include <sys/mnttab.h> 431294Slling #include <sys/mount.h> 444543Smarks #include <sys/avl.h> 454543Smarks #include <priv.h> 464543Smarks #include <pwd.h> 474543Smarks #include <grp.h> 484543Smarks #include <stddef.h> 494543Smarks #include <ucred.h> 50789Sahrens 51789Sahrens #include <sys/spa.h> 52789Sahrens #include <sys/zio.h> 532676Seschrock #include <sys/zap.h> 54789Sahrens #include <libzfs.h> 55789Sahrens 56789Sahrens #include "zfs_namecheck.h" 57789Sahrens #include "zfs_prop.h" 58789Sahrens #include "libzfs_impl.h" 594543Smarks #include "zfs_deleg.h" 60789Sahrens 614490Svb160487 static int create_parents(libzfs_handle_t *, char *, int); 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 133*5326Sek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 134*5326Sek110237 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 207*5326Sek110237 if (modifying && strchr(path, '%') != NULL) { 208*5326Sek110237 if (hdl != NULL) 209*5326Sek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 210*5326Sek110237 "invalid character %c in name"), '%'); 211*5326Sek110237 return (0); 212*5326Sek110237 } 213*5326Sek110237 2142082Seschrock return (-1); 215789Sahrens } 216789Sahrens 217789Sahrens int 218789Sahrens zfs_name_valid(const char *name, zfs_type_t type) 219789Sahrens { 220*5326Sek110237 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 /* If we can successfully roll it back, reget the stats */ 3762082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) 3771758Sahrens goto top; 3781758Sahrens /* 3791758Sahrens * If we can sucessfully destroy it, pretend that it 3801758Sahrens * never existed. 3811758Sahrens */ 3822082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { 3834543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 3841758Sahrens free(zhp); 3851758Sahrens errno = ENOENT; 3861758Sahrens return (NULL); 3871758Sahrens } 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 */ 431*5326Sek110237 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 4703912Slling /* 4712676Seschrock * Given an nvlist of properties to set, validates that they are correct, and 4722676Seschrock * parses any numeric properties (index, boolean, etc) if they are specified as 4732676Seschrock * strings. 474789Sahrens */ 4755094Slling static nvlist_t * 4765094Slling zfs_validate_properties(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 4775094Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 478789Sahrens { 4792676Seschrock nvpair_t *elem; 4802676Seschrock uint64_t intval; 4812676Seschrock char *strval; 4825094Slling zfs_prop_t prop; 4832676Seschrock nvlist_t *ret; 4842676Seschrock 4852676Seschrock if (type == ZFS_TYPE_SNAPSHOT) { 4862676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4873413Smmusante "snapshot properties cannot be modified")); 4882676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 4895094Slling return (NULL); 4905094Slling } 4915094Slling 4925094Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 4935094Slling (void) no_memory(hdl); 4945094Slling return (NULL); 495789Sahrens } 496789Sahrens 4972676Seschrock elem = NULL; 4982676Seschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 4995094Slling const char *propname = nvpair_name(elem); 5002676Seschrock 5012676Seschrock /* 5022676Seschrock * Make sure this property is valid and applies to this type. 5032676Seschrock */ 5045094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 5055094Slling if (!zfs_prop_user(propname)) { 5062676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5075094Slling "invalid property '%s'"), propname); 5085094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5095094Slling goto error; 5105094Slling } 5115094Slling 5125094Slling /* 5135094Slling * If this is a user property, make sure it's a 5145094Slling * string, and that it's less than ZAP_MAXNAMELEN. 5155094Slling */ 5165094Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 5175094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5185094Slling "'%s' must be a string"), propname); 5195094Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5205094Slling goto error; 5215094Slling } 5225094Slling 5235094Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 5245094Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5255094Slling "property name '%s' is too long"), 5262676Seschrock propname); 5272676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5282676Seschrock goto error; 5292676Seschrock } 5302676Seschrock 5312676Seschrock (void) nvpair_value_string(elem, &strval); 5322676Seschrock if (nvlist_add_string(ret, propname, strval) != 0) { 5332676Seschrock (void) no_memory(hdl); 5342676Seschrock goto error; 5352676Seschrock } 5362676Seschrock continue; 537789Sahrens } 5382676Seschrock 5392676Seschrock if (!zfs_prop_valid_for_type(prop, type)) { 5402676Seschrock zfs_error_aux(hdl, 5412676Seschrock dgettext(TEXT_DOMAIN, "'%s' does not " 5422676Seschrock "apply to datasets of this type"), propname); 5432676Seschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 5442676Seschrock goto error; 5452676Seschrock } 5462676Seschrock 5472676Seschrock if (zfs_prop_readonly(prop) && 5482676Seschrock (prop != ZFS_PROP_VOLBLOCKSIZE || zhp != NULL)) { 5492676Seschrock zfs_error_aux(hdl, 5502676Seschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 5512676Seschrock propname); 5522676Seschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 5532676Seschrock goto error; 5542676Seschrock } 5552676Seschrock 5565094Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 5575094Slling &strval, &intval, errbuf) != 0) 5582676Seschrock goto error; 5592676Seschrock 5602676Seschrock /* 5612676Seschrock * Perform some additional checks for specific properties. 5622676Seschrock */ 5632676Seschrock switch (prop) { 5644577Sahrens case ZFS_PROP_VERSION: 5654577Sahrens { 5664577Sahrens int version; 5674577Sahrens 5684577Sahrens if (zhp == NULL) 5694577Sahrens break; 5704577Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 5714577Sahrens if (intval < version) { 5724577Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5734577Sahrens "Can not downgrade; already at version %u"), 5744577Sahrens version); 5754577Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5764577Sahrens goto error; 5774577Sahrens } 5784577Sahrens break; 5794577Sahrens } 5804577Sahrens 5812676Seschrock case ZFS_PROP_RECORDSIZE: 5822676Seschrock case ZFS_PROP_VOLBLOCKSIZE: 5832676Seschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 5842676Seschrock if (intval < SPA_MINBLOCKSIZE || 5852676Seschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 5862082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 5872676Seschrock "'%s' must be power of 2 from %u " 5882676Seschrock "to %uk"), propname, 5892676Seschrock (uint_t)SPA_MINBLOCKSIZE, 5902676Seschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 5912676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 5922676Seschrock goto error; 593789Sahrens } 594789Sahrens break; 595789Sahrens 5963126Sahl case ZFS_PROP_SHAREISCSI: 5973126Sahl if (strcmp(strval, "off") != 0 && 5983126Sahl strcmp(strval, "on") != 0 && 5993126Sahl strcmp(strval, "type=disk") != 0) { 6003126Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6013126Sahl "'%s' must be 'on', 'off', or 'type=disk'"), 6023126Sahl propname); 6033126Sahl (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6043126Sahl goto error; 6053126Sahl } 6063126Sahl 6073126Sahl break; 6083126Sahl 6092676Seschrock case ZFS_PROP_MOUNTPOINT: 6104778Srm160521 { 6114778Srm160521 namecheck_err_t why; 6124778Srm160521 6132676Seschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 6142676Seschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 6152676Seschrock break; 6162676Seschrock 6174778Srm160521 if (mountpoint_namecheck(strval, &why)) { 6184778Srm160521 switch (why) { 6194778Srm160521 case NAME_ERR_LEADING_SLASH: 6204778Srm160521 zfs_error_aux(hdl, 6214778Srm160521 dgettext(TEXT_DOMAIN, 6224778Srm160521 "'%s' must be an absolute path, " 6234778Srm160521 "'none', or 'legacy'"), propname); 6244778Srm160521 break; 6254778Srm160521 case NAME_ERR_TOOLONG: 6264778Srm160521 zfs_error_aux(hdl, 6274778Srm160521 dgettext(TEXT_DOMAIN, 6284778Srm160521 "component of '%s' is too long"), 6294778Srm160521 propname); 6304778Srm160521 break; 6314778Srm160521 } 6322676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 6332676Seschrock goto error; 634789Sahrens } 6354778Srm160521 } 6364778Srm160521 6373126Sahl /*FALLTHRU*/ 6383126Sahl 6393126Sahl case ZFS_PROP_SHARENFS: 6403126Sahl /* 6413126Sahl * For the mountpoint and sharenfs properties, check if 6423126Sahl * it can be set in a global/non-global zone based on 6433126Sahl * the zoned property value: 6443126Sahl * 6453126Sahl * global zone non-global zone 6463126Sahl * -------------------------------------------------- 6473126Sahl * zoned=on mountpoint (no) mountpoint (yes) 6483126Sahl * sharenfs (no) sharenfs (no) 6493126Sahl * 6503126Sahl * zoned=off mountpoint (yes) N/A 6513126Sahl * sharenfs (yes) 6523126Sahl */ 6532676Seschrock if (zoned) { 6542676Seschrock if (getzoneid() == GLOBAL_ZONEID) { 6552676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6562676Seschrock "'%s' cannot be set on " 6572676Seschrock "dataset in a non-global zone"), 6582676Seschrock propname); 6592676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6602676Seschrock errbuf); 6612676Seschrock goto error; 6622676Seschrock } else if (prop == ZFS_PROP_SHARENFS) { 6632676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6642676Seschrock "'%s' cannot be set in " 6652676Seschrock "a non-global zone"), propname); 6662676Seschrock (void) zfs_error(hdl, EZFS_ZONED, 6672676Seschrock errbuf); 6682676Seschrock goto error; 6692676Seschrock } 6702676Seschrock } else if (getzoneid() != GLOBAL_ZONEID) { 6712676Seschrock /* 6722676Seschrock * If zoned property is 'off', this must be in 6732676Seschrock * a globle zone. If not, something is wrong. 6742676Seschrock */ 6752676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 6762676Seschrock "'%s' cannot be set while dataset " 6772676Seschrock "'zoned' property is set"), propname); 6782676Seschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 6792676Seschrock goto error; 6802676Seschrock } 6813126Sahl 6824180Sdougm /* 6834180Sdougm * At this point, it is legitimate to set the 6844180Sdougm * property. Now we want to make sure that the 6854180Sdougm * property value is valid if it is sharenfs. 6864180Sdougm */ 6874180Sdougm if (prop == ZFS_PROP_SHARENFS && 6884217Seschrock strcmp(strval, "on") != 0 && 6894217Seschrock strcmp(strval, "off") != 0) { 6904180Sdougm 6914180Sdougm /* 6924180Sdougm * Must be an NFS option string so 6934180Sdougm * init the libshare in order to 6944180Sdougm * enable the parser and then parse 6954180Sdougm * the options. We use the control API 6964180Sdougm * since we don't care about the 6974180Sdougm * current configuration and don't 6984180Sdougm * want the overhead of loading it 6994180Sdougm * until we actually do something. 7004180Sdougm */ 7014180Sdougm 7024217Seschrock if (zfs_init_libshare(hdl, 7034217Seschrock SA_INIT_CONTROL_API) != SA_OK) { 7044217Seschrock /* 7054217Seschrock * An error occurred so we can't do 7064217Seschrock * anything 7074217Seschrock */ 7084217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7094217Seschrock "'%s' cannot be set: problem " 7104217Seschrock "in share initialization"), 7114217Seschrock propname); 7124217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7134217Seschrock errbuf); 7144217Seschrock goto error; 7154217Seschrock } 7164217Seschrock 7174217Seschrock if (zfs_parse_options(strval, "nfs") != SA_OK) { 7184217Seschrock /* 7194217Seschrock * There was an error in parsing so 7204217Seschrock * deal with it by issuing an error 7214217Seschrock * message and leaving after 7224217Seschrock * uninitializing the the libshare 7234217Seschrock * interface. 7244217Seschrock */ 7254217Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7264217Seschrock "'%s' cannot be set to invalid " 7274217Seschrock "options"), propname); 7284217Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7294217Seschrock errbuf); 7304217Seschrock zfs_uninit_libshare(hdl); 7314217Seschrock goto error; 7324217Seschrock } 7334180Sdougm zfs_uninit_libshare(hdl); 7344180Sdougm } 7354180Sdougm 7363126Sahl break; 7372676Seschrock } 7382676Seschrock 7392676Seschrock /* 7402676Seschrock * For changes to existing volumes, we have some additional 7412676Seschrock * checks to enforce. 7422676Seschrock */ 7432676Seschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 7442676Seschrock uint64_t volsize = zfs_prop_get_int(zhp, 7452676Seschrock ZFS_PROP_VOLSIZE); 7462676Seschrock uint64_t blocksize = zfs_prop_get_int(zhp, 7472676Seschrock ZFS_PROP_VOLBLOCKSIZE); 7482676Seschrock char buf[64]; 7492676Seschrock 7502676Seschrock switch (prop) { 7512676Seschrock case ZFS_PROP_RESERVATION: 7522676Seschrock if (intval > volsize) { 7532676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7542676Seschrock "'%s' is greater than current " 7552676Seschrock "volume size"), propname); 7562676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7572676Seschrock errbuf); 7582676Seschrock goto error; 7592676Seschrock } 7602676Seschrock break; 7612676Seschrock 7622676Seschrock case ZFS_PROP_VOLSIZE: 7632676Seschrock if (intval % blocksize != 0) { 7642676Seschrock zfs_nicenum(blocksize, buf, 7652676Seschrock sizeof (buf)); 7662676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7672676Seschrock "'%s' must be a multiple of " 7682676Seschrock "volume block size (%s)"), 7692676Seschrock propname, buf); 7702676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7712676Seschrock errbuf); 7722676Seschrock goto error; 7732676Seschrock } 7742676Seschrock 7752676Seschrock if (intval == 0) { 7762676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 7772676Seschrock "'%s' cannot be zero"), 7782676Seschrock propname); 7792676Seschrock (void) zfs_error(hdl, EZFS_BADPROP, 7802676Seschrock errbuf); 7812676Seschrock goto error; 782789Sahrens } 7833126Sahl break; 784789Sahrens } 785789Sahrens } 786789Sahrens } 787789Sahrens 7882676Seschrock /* 7892676Seschrock * If this is an existing volume, and someone is setting the volsize, 7902676Seschrock * make sure that it matches the reservation, or add it if necessary. 7912676Seschrock */ 7922676Seschrock if (zhp != NULL && type == ZFS_TYPE_VOLUME && 7932676Seschrock nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 7942676Seschrock &intval) == 0) { 7952676Seschrock uint64_t old_volsize = zfs_prop_get_int(zhp, 7962676Seschrock ZFS_PROP_VOLSIZE); 7972676Seschrock uint64_t old_reservation = zfs_prop_get_int(zhp, 7982676Seschrock ZFS_PROP_RESERVATION); 7992676Seschrock uint64_t new_reservation; 8002676Seschrock 8012676Seschrock if (old_volsize == old_reservation && 8022676Seschrock nvlist_lookup_uint64(ret, 8032676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8042676Seschrock &new_reservation) != 0) { 8052676Seschrock if (nvlist_add_uint64(ret, 8062676Seschrock zfs_prop_to_name(ZFS_PROP_RESERVATION), 8072676Seschrock intval) != 0) { 8082676Seschrock (void) no_memory(hdl); 8092676Seschrock goto error; 8102676Seschrock } 8112676Seschrock } 8122676Seschrock } 8132676Seschrock 8142676Seschrock return (ret); 8152676Seschrock 8162676Seschrock error: 8172676Seschrock nvlist_free(ret); 8182676Seschrock return (NULL); 819789Sahrens } 820789Sahrens 8214543Smarks static int 8224543Smarks zfs_get_perm_who(const char *who, zfs_deleg_who_type_t *who_type, 8234543Smarks uint64_t *ret_who) 8244543Smarks { 8254543Smarks struct passwd *pwd; 8264543Smarks struct group *grp; 8274543Smarks uid_t id; 8284543Smarks 8294543Smarks if (*who_type == ZFS_DELEG_EVERYONE || *who_type == ZFS_DELEG_CREATE || 8304543Smarks *who_type == ZFS_DELEG_NAMED_SET) { 8314543Smarks *ret_who = -1; 8324543Smarks return (0); 8334543Smarks } 8344543Smarks if (who == NULL && !(*who_type == ZFS_DELEG_EVERYONE)) 8354543Smarks return (EZFS_BADWHO); 8364543Smarks 8374543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN && 8384543Smarks strcmp(who, "everyone") == 0) { 8394543Smarks *ret_who = -1; 8404543Smarks *who_type = ZFS_DELEG_EVERYONE; 8414543Smarks return (0); 8424543Smarks } 8434543Smarks 8444543Smarks pwd = getpwnam(who); 8454543Smarks grp = getgrnam(who); 8464543Smarks 8474543Smarks if ((*who_type == ZFS_DELEG_USER) && pwd) { 8484543Smarks *ret_who = pwd->pw_uid; 8494543Smarks } else if ((*who_type == ZFS_DELEG_GROUP) && grp) { 8504543Smarks *ret_who = grp->gr_gid; 8514543Smarks } else if (pwd) { 8524543Smarks *ret_who = pwd->pw_uid; 8534543Smarks *who_type = ZFS_DELEG_USER; 8544543Smarks } else if (grp) { 8554543Smarks *ret_who = grp->gr_gid; 8564543Smarks *who_type = ZFS_DELEG_GROUP; 8574543Smarks } else { 8584543Smarks char *end; 8594543Smarks 8604543Smarks id = strtol(who, &end, 10); 8614543Smarks if (errno != 0 || *end != '\0') { 8624543Smarks return (EZFS_BADWHO); 8634543Smarks } else { 8644543Smarks *ret_who = id; 8654543Smarks if (*who_type == ZFS_DELEG_WHO_UNKNOWN) 8664543Smarks *who_type = ZFS_DELEG_USER; 8674543Smarks } 8684543Smarks } 8694543Smarks 8704543Smarks return (0); 8714543Smarks } 8724543Smarks 8734543Smarks static void 8744543Smarks zfs_perms_add_to_nvlist(nvlist_t *who_nvp, char *name, nvlist_t *perms_nvp) 8754543Smarks { 8764543Smarks if (perms_nvp != NULL) { 8774543Smarks verify(nvlist_add_nvlist(who_nvp, 8784543Smarks name, perms_nvp) == 0); 8794543Smarks } else { 8804543Smarks verify(nvlist_add_boolean(who_nvp, name) == 0); 8814543Smarks } 8824543Smarks } 8834543Smarks 8844543Smarks static void 8854543Smarks helper(zfs_deleg_who_type_t who_type, uint64_t whoid, char *whostr, 8864543Smarks zfs_deleg_inherit_t inherit, nvlist_t *who_nvp, nvlist_t *perms_nvp, 8874543Smarks nvlist_t *sets_nvp) 8884543Smarks { 8894543Smarks boolean_t do_perms, do_sets; 8904543Smarks char name[ZFS_MAX_DELEG_NAME]; 8914543Smarks 8924543Smarks do_perms = (nvlist_next_nvpair(perms_nvp, NULL) != NULL); 8934543Smarks do_sets = (nvlist_next_nvpair(sets_nvp, NULL) != NULL); 8944543Smarks 8954543Smarks if (!do_perms && !do_sets) 8964543Smarks do_perms = do_sets = B_TRUE; 8974543Smarks 8984543Smarks if (do_perms) { 8994543Smarks zfs_deleg_whokey(name, who_type, inherit, 9004543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9014543Smarks whostr : (void *)&whoid); 9024543Smarks zfs_perms_add_to_nvlist(who_nvp, name, perms_nvp); 9034543Smarks } 9044543Smarks if (do_sets) { 9054543Smarks zfs_deleg_whokey(name, toupper(who_type), inherit, 9064543Smarks (who_type == ZFS_DELEG_NAMED_SET) ? 9074543Smarks whostr : (void *)&whoid); 9084543Smarks zfs_perms_add_to_nvlist(who_nvp, name, sets_nvp); 9094543Smarks } 9104543Smarks } 9114543Smarks 9124543Smarks static void 9134543Smarks zfs_perms_add_who_nvlist(nvlist_t *who_nvp, uint64_t whoid, void *whostr, 9144543Smarks nvlist_t *perms_nvp, nvlist_t *sets_nvp, 9154543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit) 9164543Smarks { 9174543Smarks if (who_type == ZFS_DELEG_NAMED_SET || who_type == ZFS_DELEG_CREATE) { 9184543Smarks helper(who_type, whoid, whostr, 0, 9194543Smarks who_nvp, perms_nvp, sets_nvp); 9204543Smarks } else { 9214543Smarks if (inherit & ZFS_DELEG_PERM_LOCAL) { 9224543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_LOCAL, 9234543Smarks who_nvp, perms_nvp, sets_nvp); 9244543Smarks } 9254543Smarks if (inherit & ZFS_DELEG_PERM_DESCENDENT) { 9264543Smarks helper(who_type, whoid, whostr, ZFS_DELEG_DESCENDENT, 9274543Smarks who_nvp, perms_nvp, sets_nvp); 9284543Smarks } 9294543Smarks } 9304543Smarks } 9314543Smarks 9324543Smarks /* 9334543Smarks * Construct nvlist to pass down to kernel for setting/removing permissions. 9344543Smarks * 9354543Smarks * The nvlist is constructed as a series of nvpairs with an optional embedded 9364543Smarks * nvlist of permissions to remove or set. The topmost nvpairs are the actual 9374543Smarks * base attribute named stored in the dsl. 9384543Smarks * Arguments: 9394543Smarks * 9404543Smarks * whostr: is a comma separated list of users, groups, or a single set name. 9414543Smarks * whostr may be null for everyone or create perms. 9424543Smarks * who_type: is the type of entry in whostr. Typically this will be 9434543Smarks * ZFS_DELEG_WHO_UNKNOWN. 9444543Smarks * perms: comman separated list of permissions. May be null if user 9454543Smarks * is requested to remove permissions by who. 9464543Smarks * inherit: Specifies the inheritance of the permissions. Will be either 9474543Smarks * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT. 9484543Smarks * nvp The constructed nvlist to pass to zfs_perm_set(). 9494543Smarks * The output nvp will look something like this. 9504543Smarks * ul$1234 -> {create ; destroy } 9514543Smarks * Ul$1234 -> { @myset } 9524543Smarks * s-$@myset - { snapshot; checksum; compression } 9534543Smarks */ 9544543Smarks int 9554543Smarks zfs_build_perms(zfs_handle_t *zhp, char *whostr, char *perms, 9564543Smarks zfs_deleg_who_type_t who_type, zfs_deleg_inherit_t inherit, nvlist_t **nvp) 9574543Smarks { 9584543Smarks nvlist_t *who_nvp; 9594543Smarks nvlist_t *perms_nvp = NULL; 9604543Smarks nvlist_t *sets_nvp = NULL; 9614543Smarks char errbuf[1024]; 9624787Sahrens char *who_tok, *perm; 9634543Smarks int error; 9644543Smarks 9654543Smarks *nvp = NULL; 9664543Smarks 9674543Smarks if (perms) { 9684543Smarks if ((error = nvlist_alloc(&perms_nvp, 9694543Smarks NV_UNIQUE_NAME, 0)) != 0) { 9704543Smarks return (1); 9714543Smarks } 9724543Smarks if ((error = nvlist_alloc(&sets_nvp, 9734543Smarks NV_UNIQUE_NAME, 0)) != 0) { 9744543Smarks nvlist_free(perms_nvp); 9754543Smarks return (1); 9764543Smarks } 9774543Smarks } 9784543Smarks 9794543Smarks if ((error = nvlist_alloc(&who_nvp, NV_UNIQUE_NAME, 0)) != 0) { 9804543Smarks if (perms_nvp) 9814543Smarks nvlist_free(perms_nvp); 9824543Smarks if (sets_nvp) 9834543Smarks nvlist_free(sets_nvp); 9844543Smarks return (1); 9854543Smarks } 9864543Smarks 9874543Smarks if (who_type == ZFS_DELEG_NAMED_SET) { 9884543Smarks namecheck_err_t why; 9894543Smarks char what; 9904543Smarks 9914543Smarks if ((error = permset_namecheck(whostr, &why, &what)) != 0) { 9924787Sahrens nvlist_free(who_nvp); 9934787Sahrens if (perms_nvp) 9944787Sahrens nvlist_free(perms_nvp); 9954787Sahrens if (sets_nvp) 9964787Sahrens nvlist_free(sets_nvp); 9974787Sahrens 9984543Smarks switch (why) { 9994543Smarks case NAME_ERR_NO_AT: 10004543Smarks zfs_error_aux(zhp->zfs_hdl, 10014543Smarks dgettext(TEXT_DOMAIN, 10024543Smarks "set definition must begin with an '@' " 10034543Smarks "character")); 10044543Smarks } 10054543Smarks return (zfs_error(zhp->zfs_hdl, 10064543Smarks EZFS_BADPERMSET, whostr)); 10074543Smarks } 10084543Smarks } 10094543Smarks 10104543Smarks /* 10114543Smarks * Build up nvlist(s) of permissions. Two nvlists are maintained. 10124543Smarks * The first nvlist perms_nvp will have normal permissions and the 10134543Smarks * other sets_nvp will have only permssion set names in it. 10144543Smarks */ 10154787Sahrens for (perm = strtok(perms, ","); perm; perm = strtok(NULL, ",")) { 10164787Sahrens const char *perm_canonical = zfs_deleg_canonicalize_perm(perm); 10174787Sahrens 10184787Sahrens if (perm_canonical) { 10194787Sahrens verify(nvlist_add_boolean(perms_nvp, 10204787Sahrens perm_canonical) == 0); 10214787Sahrens } else if (perm[0] == '@') { 10224787Sahrens verify(nvlist_add_boolean(sets_nvp, perm) == 0); 10234787Sahrens } else { 10244787Sahrens nvlist_free(who_nvp); 10254787Sahrens nvlist_free(perms_nvp); 10264787Sahrens nvlist_free(sets_nvp); 10274787Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPERM, perm)); 10284543Smarks } 10294543Smarks } 10304543Smarks 10314543Smarks if (whostr && who_type != ZFS_DELEG_CREATE) { 10324543Smarks who_tok = strtok(whostr, ","); 10334543Smarks if (who_tok == NULL) { 10344543Smarks nvlist_free(who_nvp); 10354787Sahrens if (perms_nvp) 10364787Sahrens nvlist_free(perms_nvp); 10374543Smarks if (sets_nvp) 10384543Smarks nvlist_free(sets_nvp); 10394543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10404543Smarks dgettext(TEXT_DOMAIN, "Who string is NULL"), 10414543Smarks whostr); 10424543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 10434543Smarks } 10444543Smarks } 10454543Smarks 10464543Smarks /* 10474543Smarks * Now create the nvlist(s) 10484543Smarks */ 10494543Smarks do { 10504543Smarks uint64_t who_id; 10514543Smarks 10524543Smarks error = zfs_get_perm_who(who_tok, &who_type, 10534543Smarks &who_id); 10544543Smarks if (error) { 10554543Smarks nvlist_free(who_nvp); 10564787Sahrens if (perms_nvp) 10574787Sahrens nvlist_free(perms_nvp); 10584543Smarks if (sets_nvp) 10594543Smarks nvlist_free(sets_nvp); 10604543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10614543Smarks dgettext(TEXT_DOMAIN, 10624543Smarks "Unable to determine uid/gid for " 10634543Smarks "%s "), who_tok); 10644543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADWHO, errbuf)); 10654543Smarks } 10664543Smarks 10674543Smarks /* 10684543Smarks * add entries for both local and descendent when required 10694543Smarks */ 10704543Smarks zfs_perms_add_who_nvlist(who_nvp, who_id, who_tok, 10714543Smarks perms_nvp, sets_nvp, who_type, inherit); 10724543Smarks 10734543Smarks } while (who_tok = strtok(NULL, ",")); 10744543Smarks *nvp = who_nvp; 10754543Smarks return (0); 10764543Smarks } 10774543Smarks 10784543Smarks static int 10794543Smarks zfs_perm_set_common(zfs_handle_t *zhp, nvlist_t *nvp, boolean_t unset) 10804543Smarks { 10814543Smarks zfs_cmd_t zc = { 0 }; 10824543Smarks int error; 10834543Smarks char errbuf[1024]; 10844543Smarks 10854543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10864543Smarks dgettext(TEXT_DOMAIN, "Cannot update 'allows' for '%s'"), 10874543Smarks zhp->zfs_name); 10884543Smarks 10895094Slling if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, nvp)) 10904543Smarks return (-1); 10914543Smarks 10924543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 10934543Smarks zc.zc_perm_action = unset; 10944543Smarks 10954543Smarks error = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SET_FSACL, &zc); 10964543Smarks if (error && errno == ENOTSUP) { 10974543Smarks (void) snprintf(errbuf, sizeof (errbuf), 10984543Smarks gettext("Pool must be upgraded to use 'allow/unallow'")); 10994543Smarks zcmd_free_nvlists(&zc); 11004543Smarks return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, errbuf)); 11014543Smarks } else if (error) { 11024543Smarks return (zfs_standard_error(zhp->zfs_hdl, errno, errbuf)); 11034543Smarks } 11044543Smarks zcmd_free_nvlists(&zc); 11054543Smarks 11064543Smarks return (error); 11074543Smarks } 11084543Smarks 11094543Smarks int 11104543Smarks zfs_perm_set(zfs_handle_t *zhp, nvlist_t *nvp) 11114543Smarks { 11124543Smarks return (zfs_perm_set_common(zhp, nvp, B_FALSE)); 11134543Smarks } 11144543Smarks 11154543Smarks int 11164543Smarks zfs_perm_remove(zfs_handle_t *zhp, nvlist_t *perms) 11174543Smarks { 11184543Smarks return (zfs_perm_set_common(zhp, perms, B_TRUE)); 11194543Smarks } 11204543Smarks 11214543Smarks static int 11224543Smarks perm_compare(const void *arg1, const void *arg2) 11234543Smarks { 11244543Smarks const zfs_perm_node_t *node1 = arg1; 11254543Smarks const zfs_perm_node_t *node2 = arg2; 11264543Smarks int ret; 11274543Smarks 11284543Smarks ret = strcmp(node1->z_pname, node2->z_pname); 11294543Smarks 11304543Smarks if (ret > 0) 11314543Smarks return (1); 11324543Smarks if (ret < 0) 11334543Smarks return (-1); 11344543Smarks else 11354543Smarks return (0); 11364543Smarks } 11374543Smarks 11384543Smarks static void 11394543Smarks zfs_destroy_perm_tree(avl_tree_t *tree) 11404543Smarks { 11414543Smarks zfs_perm_node_t *permnode; 11424543Smarks void *cookie; 11434543Smarks 11444543Smarks cookie = NULL; 11454543Smarks while ((permnode = avl_destroy_nodes(tree, &cookie)) != NULL) { 11464543Smarks avl_remove(tree, permnode); 11474543Smarks free(permnode); 11484543Smarks } 11494543Smarks } 11504543Smarks 11514543Smarks static void 11524543Smarks zfs_destroy_tree(avl_tree_t *tree) 11534543Smarks { 11544543Smarks zfs_allow_node_t *allownode; 11554543Smarks void *cookie; 11564543Smarks 11574543Smarks cookie = NULL; 11584543Smarks while ((allownode = avl_destroy_nodes(tree, &cookie)) != NULL) { 11594543Smarks zfs_destroy_perm_tree(&allownode->z_localdescend); 11604543Smarks zfs_destroy_perm_tree(&allownode->z_local); 11614543Smarks zfs_destroy_perm_tree(&allownode->z_descend); 11624543Smarks avl_remove(tree, allownode); 11634543Smarks free(allownode); 11644543Smarks } 11654543Smarks } 11664543Smarks 11674543Smarks void 11684543Smarks zfs_free_allows(zfs_allow_t *allow) 11694543Smarks { 11704543Smarks zfs_allow_t *allownext; 11714543Smarks zfs_allow_t *freeallow; 11724543Smarks 11734543Smarks allownext = allow; 11744543Smarks while (allownext) { 11754543Smarks zfs_destroy_tree(&allownext->z_sets); 11764543Smarks zfs_destroy_tree(&allownext->z_crperms); 11774543Smarks zfs_destroy_tree(&allownext->z_user); 11784543Smarks zfs_destroy_tree(&allownext->z_group); 11794543Smarks zfs_destroy_tree(&allownext->z_everyone); 11804543Smarks freeallow = allownext; 11814543Smarks allownext = allownext->z_next; 11824543Smarks free(freeallow); 11834543Smarks } 11844543Smarks } 11854543Smarks 11864543Smarks static zfs_allow_t * 11874543Smarks zfs_alloc_perm_tree(zfs_handle_t *zhp, zfs_allow_t *prev, char *setpoint) 11884543Smarks { 11894543Smarks zfs_allow_t *ptree; 11904543Smarks 11914543Smarks if ((ptree = zfs_alloc(zhp->zfs_hdl, 11924543Smarks sizeof (zfs_allow_t))) == NULL) { 11934543Smarks return (NULL); 11944543Smarks } 11954543Smarks 11964543Smarks (void) strlcpy(ptree->z_setpoint, setpoint, sizeof (ptree->z_setpoint)); 11974543Smarks avl_create(&ptree->z_sets, 11984543Smarks perm_compare, sizeof (zfs_allow_node_t), 11994543Smarks offsetof(zfs_allow_node_t, z_node)); 12004543Smarks avl_create(&ptree->z_crperms, 12014543Smarks perm_compare, sizeof (zfs_allow_node_t), 12024543Smarks offsetof(zfs_allow_node_t, z_node)); 12034543Smarks avl_create(&ptree->z_user, 12044543Smarks perm_compare, sizeof (zfs_allow_node_t), 12054543Smarks offsetof(zfs_allow_node_t, z_node)); 12064543Smarks avl_create(&ptree->z_group, 12074543Smarks perm_compare, sizeof (zfs_allow_node_t), 12084543Smarks offsetof(zfs_allow_node_t, z_node)); 12094543Smarks avl_create(&ptree->z_everyone, 12104543Smarks perm_compare, sizeof (zfs_allow_node_t), 12114543Smarks offsetof(zfs_allow_node_t, z_node)); 12124543Smarks 12134543Smarks if (prev) 12144543Smarks prev->z_next = ptree; 12154543Smarks ptree->z_next = NULL; 12164543Smarks return (ptree); 12174543Smarks } 12184543Smarks 12194543Smarks /* 12204543Smarks * Add permissions to the appropriate AVL permission tree. 12214543Smarks * The appropriate tree may not be the requested tree. 12224543Smarks * For example if ld indicates a local permission, but 12234543Smarks * same permission also exists as a descendent permission 12244543Smarks * then the permission will be removed from the descendent 12254543Smarks * tree and add the the local+descendent tree. 12264543Smarks */ 12274543Smarks static int 12284543Smarks zfs_coalesce_perm(zfs_handle_t *zhp, zfs_allow_node_t *allownode, 12294543Smarks char *perm, char ld) 12304543Smarks { 12314543Smarks zfs_perm_node_t pnode, *permnode, *permnode2; 12324543Smarks zfs_perm_node_t *newnode; 12334543Smarks avl_index_t where, where2; 12344543Smarks avl_tree_t *tree, *altree; 12354543Smarks 12364543Smarks (void) strlcpy(pnode.z_pname, perm, sizeof (pnode.z_pname)); 12374543Smarks 12384543Smarks if (ld == ZFS_DELEG_NA) { 12394543Smarks tree = &allownode->z_localdescend; 12404543Smarks altree = &allownode->z_descend; 12414543Smarks } else if (ld == ZFS_DELEG_LOCAL) { 12424543Smarks tree = &allownode->z_local; 12434543Smarks altree = &allownode->z_descend; 12444543Smarks } else { 12454543Smarks tree = &allownode->z_descend; 12464543Smarks altree = &allownode->z_local; 12474543Smarks } 12484543Smarks permnode = avl_find(tree, &pnode, &where); 12494543Smarks permnode2 = avl_find(altree, &pnode, &where2); 12504543Smarks 12514543Smarks if (permnode2) { 12524543Smarks avl_remove(altree, permnode2); 12534543Smarks free(permnode2); 12544543Smarks if (permnode == NULL) { 12554543Smarks tree = &allownode->z_localdescend; 12564543Smarks } 12574543Smarks } 12584543Smarks 12594543Smarks /* 12604543Smarks * Now insert new permission in either requested location 12614543Smarks * local/descendent or into ld when perm will exist in both. 12624543Smarks */ 12634543Smarks if (permnode == NULL) { 12644543Smarks if ((newnode = zfs_alloc(zhp->zfs_hdl, 12654543Smarks sizeof (zfs_perm_node_t))) == NULL) { 12664543Smarks return (-1); 12674543Smarks } 12684543Smarks *newnode = pnode; 12694543Smarks avl_add(tree, newnode); 12704543Smarks } 12714543Smarks return (0); 12724543Smarks } 12734577Sahrens 12744543Smarks /* 12754543Smarks * Uggh, this is going to be a bit complicated. 12764543Smarks * we have an nvlist coming out of the kernel that 12774543Smarks * will indicate where the permission is set and then 12784543Smarks * it will contain allow of the various "who's", and what 12794543Smarks * their permissions are. To further complicate this 12804543Smarks * we will then have to coalesce the local,descendent 12814543Smarks * and local+descendent permissions where appropriate. 12824543Smarks * The kernel only knows about a permission as being local 12834543Smarks * or descendent, but not both. 12844543Smarks * 12854543Smarks * In order to make this easier for zfs_main to deal with 12864543Smarks * a series of AVL trees will be used to maintain 12874543Smarks * all of this, primarily for sorting purposes as well 12884543Smarks * as the ability to quickly locate a specific entry. 12894543Smarks * 12904543Smarks * What we end up with are tree's for sets, create perms, 12914543Smarks * user, groups and everyone. With each of those trees 12924543Smarks * we have subtrees for local, descendent and local+descendent 12934543Smarks * permissions. 12944543Smarks */ 12954543Smarks int 12964543Smarks zfs_perm_get(zfs_handle_t *zhp, zfs_allow_t **zfs_perms) 12974543Smarks { 12984543Smarks zfs_cmd_t zc = { 0 }; 12994543Smarks int error; 13004543Smarks nvlist_t *nvlist; 13014543Smarks nvlist_t *permnv, *sourcenv; 13024543Smarks nvpair_t *who_pair, *source_pair; 13034543Smarks nvpair_t *perm_pair; 13044543Smarks char errbuf[1024]; 13054543Smarks zfs_allow_t *zallowp, *newallowp; 13064543Smarks char ld; 13074543Smarks char *nvpname; 13084543Smarks uid_t uid; 13094543Smarks gid_t gid; 13104543Smarks avl_tree_t *tree; 13114543Smarks avl_index_t where; 13124543Smarks 13134543Smarks (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 13144543Smarks 13154543Smarks if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 13164543Smarks return (-1); 13174543Smarks 13184543Smarks while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 13194543Smarks if (errno == ENOMEM) { 13204543Smarks if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, &zc) != 0) { 13214543Smarks zcmd_free_nvlists(&zc); 13224543Smarks return (-1); 13234543Smarks } 13244543Smarks } else if (errno == ENOTSUP) { 13254543Smarks zcmd_free_nvlists(&zc); 13264543Smarks (void) snprintf(errbuf, sizeof (errbuf), 13274543Smarks gettext("Pool must be upgraded to use 'allow'")); 13284543Smarks return (zfs_error(zhp->zfs_hdl, 13294543Smarks EZFS_BADVERSION, errbuf)); 13304543Smarks } else { 13314543Smarks zcmd_free_nvlists(&zc); 13324543Smarks return (-1); 13334543Smarks } 13344543Smarks } 13354543Smarks 13364543Smarks if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &nvlist) != 0) { 13374543Smarks zcmd_free_nvlists(&zc); 13384543Smarks return (-1); 13394543Smarks } 13404543Smarks 13414543Smarks zcmd_free_nvlists(&zc); 13424543Smarks 13434543Smarks source_pair = nvlist_next_nvpair(nvlist, NULL); 13444543Smarks 13454543Smarks if (source_pair == NULL) { 13464543Smarks *zfs_perms = NULL; 13474543Smarks return (0); 13484543Smarks } 13494543Smarks 13504543Smarks *zfs_perms = zfs_alloc_perm_tree(zhp, NULL, nvpair_name(source_pair)); 13514543Smarks if (*zfs_perms == NULL) { 13524543Smarks return (0); 13534543Smarks } 13544543Smarks 13554543Smarks zallowp = *zfs_perms; 13564543Smarks 13574543Smarks for (;;) { 13584543Smarks struct passwd *pwd; 13594543Smarks struct group *grp; 13604543Smarks zfs_allow_node_t *allownode; 13614543Smarks zfs_allow_node_t findallownode; 13624543Smarks zfs_allow_node_t *newallownode; 13634543Smarks 13644543Smarks (void) strlcpy(zallowp->z_setpoint, 13654543Smarks nvpair_name(source_pair), 13664543Smarks sizeof (zallowp->z_setpoint)); 13674543Smarks 13684543Smarks if ((error = nvpair_value_nvlist(source_pair, &sourcenv)) != 0) 13694543Smarks goto abort; 13704543Smarks 13714543Smarks /* 13724543Smarks * Make sure nvlist is composed correctly 13734543Smarks */ 13744543Smarks if (zfs_deleg_verify_nvlist(sourcenv)) { 13754543Smarks goto abort; 13764543Smarks } 13774543Smarks 13784543Smarks who_pair = nvlist_next_nvpair(sourcenv, NULL); 13794543Smarks if (who_pair == NULL) { 13804543Smarks goto abort; 13814543Smarks } 13824543Smarks 13834543Smarks do { 13844543Smarks error = nvpair_value_nvlist(who_pair, &permnv); 13854543Smarks if (error) { 13864543Smarks goto abort; 13874543Smarks } 13884543Smarks 13894543Smarks /* 13904543Smarks * First build up the key to use 13914543Smarks * for looking up in the various 13924543Smarks * who trees. 13934543Smarks */ 13944543Smarks ld = nvpair_name(who_pair)[1]; 13954543Smarks nvpname = nvpair_name(who_pair); 13964543Smarks switch (nvpair_name(who_pair)[0]) { 13974543Smarks case ZFS_DELEG_USER: 13984543Smarks case ZFS_DELEG_USER_SETS: 13994543Smarks tree = &zallowp->z_user; 14004543Smarks uid = atol(&nvpname[3]); 14014543Smarks pwd = getpwuid(uid); 14024543Smarks (void) snprintf(findallownode.z_key, 14034543Smarks sizeof (findallownode.z_key), "user %s", 14044543Smarks (pwd) ? pwd->pw_name : 14054543Smarks &nvpair_name(who_pair)[3]); 14064543Smarks break; 14074543Smarks case ZFS_DELEG_GROUP: 14084543Smarks case ZFS_DELEG_GROUP_SETS: 14094543Smarks tree = &zallowp->z_group; 14104543Smarks gid = atol(&nvpname[3]); 14114543Smarks grp = getgrgid(gid); 14124543Smarks (void) snprintf(findallownode.z_key, 14134543Smarks sizeof (findallownode.z_key), "group %s", 14144543Smarks (grp) ? grp->gr_name : 14154543Smarks &nvpair_name(who_pair)[3]); 14164543Smarks break; 14174543Smarks case ZFS_DELEG_CREATE: 14184543Smarks case ZFS_DELEG_CREATE_SETS: 14194543Smarks tree = &zallowp->z_crperms; 14204543Smarks (void) strlcpy(findallownode.z_key, "", 14214543Smarks sizeof (findallownode.z_key)); 14224543Smarks break; 14234543Smarks case ZFS_DELEG_EVERYONE: 14244543Smarks case ZFS_DELEG_EVERYONE_SETS: 14254543Smarks (void) snprintf(findallownode.z_key, 14264543Smarks sizeof (findallownode.z_key), "everyone"); 14274543Smarks tree = &zallowp->z_everyone; 14284543Smarks break; 14294543Smarks case ZFS_DELEG_NAMED_SET: 14304543Smarks case ZFS_DELEG_NAMED_SET_SETS: 14314543Smarks (void) snprintf(findallownode.z_key, 14324543Smarks sizeof (findallownode.z_key), "%s", 14334543Smarks &nvpair_name(who_pair)[3]); 14344543Smarks tree = &zallowp->z_sets; 14354543Smarks break; 14364543Smarks } 14374543Smarks 14384543Smarks /* 14394543Smarks * Place who in tree 14404543Smarks */ 14414543Smarks allownode = avl_find(tree, &findallownode, &where); 14424543Smarks if (allownode == NULL) { 14434543Smarks if ((newallownode = zfs_alloc(zhp->zfs_hdl, 14444543Smarks sizeof (zfs_allow_node_t))) == NULL) { 14454543Smarks goto abort; 14464543Smarks } 14474543Smarks avl_create(&newallownode->z_localdescend, 14484543Smarks perm_compare, 14494543Smarks sizeof (zfs_perm_node_t), 14504543Smarks offsetof(zfs_perm_node_t, z_node)); 14514543Smarks avl_create(&newallownode->z_local, 14524543Smarks perm_compare, 14534543Smarks sizeof (zfs_perm_node_t), 14544543Smarks offsetof(zfs_perm_node_t, z_node)); 14554543Smarks avl_create(&newallownode->z_descend, 14564543Smarks perm_compare, 14574543Smarks sizeof (zfs_perm_node_t), 14584543Smarks offsetof(zfs_perm_node_t, z_node)); 14594543Smarks (void) strlcpy(newallownode->z_key, 14604543Smarks findallownode.z_key, 14614543Smarks sizeof (findallownode.z_key)); 14624543Smarks avl_insert(tree, newallownode, where); 14634543Smarks allownode = newallownode; 14644543Smarks } 14654543Smarks 14664543Smarks /* 14674543Smarks * Now iterate over the permissions and 14684543Smarks * place them in the appropriate local, 14694543Smarks * descendent or local+descendent tree. 14704543Smarks * 14714543Smarks * The permissions are added to the tree 14724543Smarks * via zfs_coalesce_perm(). 14734543Smarks */ 14744543Smarks perm_pair = nvlist_next_nvpair(permnv, NULL); 14754543Smarks if (perm_pair == NULL) 14764543Smarks goto abort; 14774543Smarks do { 14784543Smarks if (zfs_coalesce_perm(zhp, allownode, 14794543Smarks nvpair_name(perm_pair), ld) != 0) 14804543Smarks goto abort; 14814543Smarks } while (perm_pair = nvlist_next_nvpair(permnv, 14824543Smarks perm_pair)); 14834543Smarks } while (who_pair = nvlist_next_nvpair(sourcenv, who_pair)); 14844543Smarks 14854543Smarks source_pair = nvlist_next_nvpair(nvlist, source_pair); 14864543Smarks if (source_pair == NULL) 14874543Smarks break; 14884543Smarks 14894543Smarks /* 14904543Smarks * allocate another node from the link list of 14914543Smarks * zfs_allow_t structures 14924543Smarks */ 14934543Smarks newallowp = zfs_alloc_perm_tree(zhp, zallowp, 14944543Smarks nvpair_name(source_pair)); 14954543Smarks if (newallowp == NULL) { 14964543Smarks goto abort; 14974543Smarks } 14984543Smarks zallowp = newallowp; 14994543Smarks } 15004543Smarks nvlist_free(nvlist); 15014543Smarks return (0); 15024543Smarks abort: 15034543Smarks zfs_free_allows(*zfs_perms); 15044543Smarks nvlist_free(nvlist); 15054543Smarks return (-1); 15064543Smarks } 15074543Smarks 1508789Sahrens /* 1509789Sahrens * Given a property name and value, set the property for the given dataset. 1510789Sahrens */ 1511789Sahrens int 15122676Seschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1513789Sahrens { 1514789Sahrens zfs_cmd_t zc = { 0 }; 15152676Seschrock int ret = -1; 15162676Seschrock prop_changelist_t *cl = NULL; 15172082Seschrock char errbuf[1024]; 15182082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 15192676Seschrock nvlist_t *nvl = NULL, *realprops; 15202676Seschrock zfs_prop_t prop; 15212082Seschrock 15222082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 15232676Seschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 15242082Seschrock zhp->zfs_name); 15252082Seschrock 15262676Seschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 15272676Seschrock nvlist_add_string(nvl, propname, propval) != 0) { 15282676Seschrock (void) no_memory(hdl); 15292676Seschrock goto error; 1530789Sahrens } 1531789Sahrens 15325094Slling if ((realprops = zfs_validate_properties(hdl, zhp->zfs_type, nvl, 15332676Seschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 15342676Seschrock goto error; 15355094Slling 15362676Seschrock nvlist_free(nvl); 15372676Seschrock nvl = realprops; 15382676Seschrock 15392676Seschrock prop = zfs_name_to_prop(propname); 15402676Seschrock 1541789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 15422676Seschrock goto error; 1543789Sahrens 1544789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 15452082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15462082Seschrock "child dataset with inherited mountpoint is used " 15472082Seschrock "in a non-global zone")); 15482082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1549789Sahrens goto error; 1550789Sahrens } 1551789Sahrens 1552789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1553789Sahrens goto error; 1554789Sahrens 1555789Sahrens /* 1556789Sahrens * Execute the corresponding ioctl() to set this property. 1557789Sahrens */ 1558789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1559789Sahrens 15605094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 15612676Seschrock goto error; 15622676Seschrock 15634543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1564789Sahrens 1565789Sahrens if (ret != 0) { 1566789Sahrens switch (errno) { 1567789Sahrens 1568789Sahrens case ENOSPC: 1569789Sahrens /* 1570789Sahrens * For quotas and reservations, ENOSPC indicates 1571789Sahrens * something different; setting a quota or reservation 1572789Sahrens * doesn't use any disk space. 1573789Sahrens */ 1574789Sahrens switch (prop) { 1575789Sahrens case ZFS_PROP_QUOTA: 15762082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15772082Seschrock "size is less than current used or " 15782082Seschrock "reserved space")); 15792082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1580789Sahrens break; 1581789Sahrens 1582789Sahrens case ZFS_PROP_RESERVATION: 15832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15842082Seschrock "size is greater than available space")); 15852082Seschrock (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 1586789Sahrens break; 1587789Sahrens 1588789Sahrens default: 15892082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1590789Sahrens break; 1591789Sahrens } 1592789Sahrens break; 1593789Sahrens 1594789Sahrens case EBUSY: 15952082Seschrock if (prop == ZFS_PROP_VOLBLOCKSIZE) 15962082Seschrock (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); 15972082Seschrock else 15982676Seschrock (void) zfs_standard_error(hdl, EBUSY, errbuf); 1599789Sahrens break; 1600789Sahrens 16011175Slling case EROFS: 16022082Seschrock (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 16031175Slling break; 16041175Slling 16053886Sahl case ENOTSUP: 16063886Sahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16074603Sahrens "pool must be upgraded to set this " 16084603Sahrens "property or value")); 16093886Sahl (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 16103886Sahl break; 16113886Sahl 1612789Sahrens case EOVERFLOW: 1613789Sahrens /* 1614789Sahrens * This platform can't address a volume this big. 1615789Sahrens */ 1616789Sahrens #ifdef _ILP32 1617789Sahrens if (prop == ZFS_PROP_VOLSIZE) { 16182082Seschrock (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 1619789Sahrens break; 1620789Sahrens } 1621789Sahrens #endif 16222082Seschrock /* FALLTHROUGH */ 1623789Sahrens default: 16242082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 1625789Sahrens } 1626789Sahrens } else { 1627789Sahrens /* 1628789Sahrens * Refresh the statistics so the new property value 1629789Sahrens * is reflected. 1630789Sahrens */ 16312676Seschrock if ((ret = changelist_postfix(cl)) == 0) 16322676Seschrock (void) get_stats(zhp); 1633789Sahrens } 1634789Sahrens 1635789Sahrens error: 16362676Seschrock nvlist_free(nvl); 16372676Seschrock zcmd_free_nvlists(&zc); 16382676Seschrock if (cl) 16392676Seschrock changelist_free(cl); 1640789Sahrens return (ret); 1641789Sahrens } 1642789Sahrens 1643789Sahrens /* 1644789Sahrens * Given a property, inherit the value from the parent dataset. 1645789Sahrens */ 1646789Sahrens int 16472676Seschrock zfs_prop_inherit(zfs_handle_t *zhp, const char *propname) 1648789Sahrens { 1649789Sahrens zfs_cmd_t zc = { 0 }; 1650789Sahrens int ret; 1651789Sahrens prop_changelist_t *cl; 16522082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 16532082Seschrock char errbuf[1024]; 16542676Seschrock zfs_prop_t prop; 16552082Seschrock 16562082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 16572082Seschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1658789Sahrens 16595094Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 16602676Seschrock /* 16612676Seschrock * For user properties, the amount of work we have to do is very 16622676Seschrock * small, so just do it here. 16632676Seschrock */ 16642676Seschrock if (!zfs_prop_user(propname)) { 16652676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16662676Seschrock "invalid property")); 16672676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 16682676Seschrock } 16692676Seschrock 16702676Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 16712676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 16722676Seschrock 16734849Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 16742676Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 16752676Seschrock 16762676Seschrock return (0); 16772676Seschrock } 16782676Seschrock 1679789Sahrens /* 1680789Sahrens * Verify that this property is inheritable. 1681789Sahrens */ 16822082Seschrock if (zfs_prop_readonly(prop)) 16832082Seschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 16842082Seschrock 16852082Seschrock if (!zfs_prop_inheritable(prop)) 16862082Seschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1687789Sahrens 1688789Sahrens /* 1689789Sahrens * Check to see if the value applies to this type 1690789Sahrens */ 16912082Seschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 16922082Seschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1693789Sahrens 16943443Srm160521 /* 16953443Srm160521 * Normalize the name, to get rid of shorthand abbrevations. 16963443Srm160521 */ 16973443Srm160521 propname = zfs_prop_to_name(prop); 1698789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 16992676Seschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1700789Sahrens 1701789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1702789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 17032082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17042082Seschrock "dataset is used in a non-global zone")); 17052082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1706789Sahrens } 1707789Sahrens 1708789Sahrens /* 1709789Sahrens * Determine datasets which will be affected by this change, if any. 1710789Sahrens */ 1711789Sahrens if ((cl = changelist_gather(zhp, prop, 0)) == NULL) 1712789Sahrens return (-1); 1713789Sahrens 1714789Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 17152082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17162082Seschrock "child dataset with inherited mountpoint is used " 17172082Seschrock "in a non-global zone")); 17182082Seschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1719789Sahrens goto error; 1720789Sahrens } 1721789Sahrens 1722789Sahrens if ((ret = changelist_prefix(cl)) != 0) 1723789Sahrens goto error; 1724789Sahrens 17254849Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 17262082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 1727789Sahrens } else { 1728789Sahrens 17292169Snd150628 if ((ret = changelist_postfix(cl)) != 0) 1730789Sahrens goto error; 1731789Sahrens 1732789Sahrens /* 1733789Sahrens * Refresh the statistics so the new property is reflected. 1734789Sahrens */ 1735789Sahrens (void) get_stats(zhp); 1736789Sahrens } 1737789Sahrens 1738789Sahrens error: 1739789Sahrens changelist_free(cl); 1740789Sahrens return (ret); 1741789Sahrens } 1742789Sahrens 1743789Sahrens /* 17441356Seschrock * True DSL properties are stored in an nvlist. The following two functions 17451356Seschrock * extract them appropriately. 17461356Seschrock */ 17471356Seschrock static uint64_t 17481356Seschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 17491356Seschrock { 17501356Seschrock nvlist_t *nv; 17511356Seschrock uint64_t value; 17521356Seschrock 17532885Sahrens *source = NULL; 17541356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 17551356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 17565094Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 17575094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 17581356Seschrock } else { 17591356Seschrock value = zfs_prop_default_numeric(prop); 17601356Seschrock *source = ""; 17611356Seschrock } 17621356Seschrock 17631356Seschrock return (value); 17641356Seschrock } 17651356Seschrock 17661356Seschrock static char * 17671356Seschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 17681356Seschrock { 17691356Seschrock nvlist_t *nv; 17701356Seschrock char *value; 17711356Seschrock 17722885Sahrens *source = NULL; 17731356Seschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 17741356Seschrock zfs_prop_to_name(prop), &nv) == 0) { 17755094Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 17765094Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 17771356Seschrock } else { 17781356Seschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 17791356Seschrock value = ""; 17801356Seschrock *source = ""; 17811356Seschrock } 17821356Seschrock 17831356Seschrock return (value); 17841356Seschrock } 17851356Seschrock 17861356Seschrock /* 1787789Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1788789Sahrens * zfs_prop_get_int() are built using this interface. 1789789Sahrens * 1790789Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1791789Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1792789Sahrens * If they differ from the on-disk values, report the current values and mark 1793789Sahrens * the source "temporary". 1794789Sahrens */ 17952082Seschrock static int 17965094Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 17972082Seschrock char **source, uint64_t *val) 1798789Sahrens { 17995147Srm160521 zfs_cmd_t zc = { 0 }; 1800789Sahrens struct mnttab mnt; 18013265Sahrens char *mntopt_on = NULL; 18023265Sahrens char *mntopt_off = NULL; 1803789Sahrens 1804789Sahrens *source = NULL; 1805789Sahrens 18063265Sahrens switch (prop) { 18073265Sahrens case ZFS_PROP_ATIME: 18083265Sahrens mntopt_on = MNTOPT_ATIME; 18093265Sahrens mntopt_off = MNTOPT_NOATIME; 18103265Sahrens break; 18113265Sahrens 18123265Sahrens case ZFS_PROP_DEVICES: 18133265Sahrens mntopt_on = MNTOPT_DEVICES; 18143265Sahrens mntopt_off = MNTOPT_NODEVICES; 18153265Sahrens break; 18163265Sahrens 18173265Sahrens case ZFS_PROP_EXEC: 18183265Sahrens mntopt_on = MNTOPT_EXEC; 18193265Sahrens mntopt_off = MNTOPT_NOEXEC; 18203265Sahrens break; 18213265Sahrens 18223265Sahrens case ZFS_PROP_READONLY: 18233265Sahrens mntopt_on = MNTOPT_RO; 18243265Sahrens mntopt_off = MNTOPT_RW; 18253265Sahrens break; 18263265Sahrens 18273265Sahrens case ZFS_PROP_SETUID: 18283265Sahrens mntopt_on = MNTOPT_SETUID; 18293265Sahrens mntopt_off = MNTOPT_NOSETUID; 18303265Sahrens break; 18313265Sahrens 18323265Sahrens case ZFS_PROP_XATTR: 18333265Sahrens mntopt_on = MNTOPT_XATTR; 18343265Sahrens mntopt_off = MNTOPT_NOXATTR; 18353265Sahrens break; 18363265Sahrens } 18373265Sahrens 18382474Seschrock /* 18392474Seschrock * Because looking up the mount options is potentially expensive 18402474Seschrock * (iterating over all of /etc/mnttab), we defer its calculation until 18412474Seschrock * we're looking up a property which requires its presence. 18422474Seschrock */ 18432474Seschrock if (!zhp->zfs_mntcheck && 18443265Sahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 18453265Sahrens struct mnttab entry, search = { 0 }; 18463265Sahrens FILE *mnttab = zhp->zfs_hdl->libzfs_mnttab; 18472474Seschrock 18482474Seschrock search.mnt_special = (char *)zhp->zfs_name; 18492474Seschrock search.mnt_fstype = MNTTYPE_ZFS; 18503265Sahrens rewind(mnttab); 18513265Sahrens 18523265Sahrens if (getmntany(mnttab, &entry, &search) == 0) { 18533265Sahrens zhp->zfs_mntopts = zfs_strdup(zhp->zfs_hdl, 18543265Sahrens entry.mnt_mntopts); 18553265Sahrens if (zhp->zfs_mntopts == NULL) 18563265Sahrens return (-1); 18573265Sahrens } 18582474Seschrock 18592474Seschrock zhp->zfs_mntcheck = B_TRUE; 18602474Seschrock } 18612474Seschrock 1862789Sahrens if (zhp->zfs_mntopts == NULL) 1863789Sahrens mnt.mnt_mntopts = ""; 1864789Sahrens else 1865789Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1866789Sahrens 1867789Sahrens switch (prop) { 1868789Sahrens case ZFS_PROP_ATIME: 18693265Sahrens case ZFS_PROP_DEVICES: 18703265Sahrens case ZFS_PROP_EXEC: 18713265Sahrens case ZFS_PROP_READONLY: 18723265Sahrens case ZFS_PROP_SETUID: 18733265Sahrens case ZFS_PROP_XATTR: 18742082Seschrock *val = getprop_uint64(zhp, prop, source); 18752082Seschrock 18763265Sahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 18772082Seschrock *val = B_TRUE; 1878789Sahrens if (src) 18795094Slling *src = ZPROP_SRC_TEMPORARY; 18803265Sahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 18812082Seschrock *val = B_FALSE; 1882789Sahrens if (src) 18835094Slling *src = ZPROP_SRC_TEMPORARY; 1884789Sahrens } 18852082Seschrock break; 1886789Sahrens 18873265Sahrens case ZFS_PROP_CANMOUNT: 18882082Seschrock *val = getprop_uint64(zhp, prop, source); 18893417Srm160521 if (*val == 0) 18903417Srm160521 *source = zhp->zfs_name; 18913417Srm160521 else 18923417Srm160521 *source = ""; /* default */ 18932082Seschrock break; 1894789Sahrens 1895789Sahrens case ZFS_PROP_QUOTA: 1896789Sahrens case ZFS_PROP_RESERVATION: 18972885Sahrens *val = getprop_uint64(zhp, prop, source); 18982885Sahrens if (*val == 0) 1899789Sahrens *source = ""; /* default */ 1900789Sahrens else 1901789Sahrens *source = zhp->zfs_name; 19022082Seschrock break; 1903789Sahrens 1904789Sahrens case ZFS_PROP_MOUNTED: 19052082Seschrock *val = (zhp->zfs_mntopts != NULL); 19062082Seschrock break; 1907789Sahrens 19083377Seschrock case ZFS_PROP_NUMCLONES: 19093377Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 19103377Seschrock break; 19113377Seschrock 19125147Srm160521 case ZFS_PROP_VERSION: 19135147Srm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 19145147Srm160521 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_VERSION, &zc) || 19155147Srm160521 (zc.zc_cookie == 0)) { 19165147Srm160521 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19175147Srm160521 "unable to get version property")); 19185147Srm160521 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION, 19195147Srm160521 dgettext(TEXT_DOMAIN, "internal error"))); 19205147Srm160521 } 19215147Srm160521 *val = zc.zc_cookie; 19225147Srm160521 break; 19235147Srm160521 1924789Sahrens default: 19254577Sahrens switch (zfs_prop_get_type(prop)) { 19264787Sahrens case PROP_TYPE_NUMBER: 19274787Sahrens case PROP_TYPE_INDEX: 19284577Sahrens *val = getprop_uint64(zhp, prop, source); 19294577Sahrens break; 19304577Sahrens 19314787Sahrens case PROP_TYPE_STRING: 19324577Sahrens default: 19334577Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 19344577Sahrens "cannot get non-numeric property")); 19354577Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 19364577Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 19374577Sahrens } 1938789Sahrens } 1939789Sahrens 1940789Sahrens return (0); 1941789Sahrens } 1942789Sahrens 1943789Sahrens /* 1944789Sahrens * Calculate the source type, given the raw source string. 1945789Sahrens */ 1946789Sahrens static void 19475094Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1948789Sahrens char *statbuf, size_t statlen) 1949789Sahrens { 19505094Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1951789Sahrens return; 1952789Sahrens 1953789Sahrens if (source == NULL) { 19545094Slling *srctype = ZPROP_SRC_NONE; 1955789Sahrens } else if (source[0] == '\0') { 19565094Slling *srctype = ZPROP_SRC_DEFAULT; 1957789Sahrens } else { 1958789Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 19595094Slling *srctype = ZPROP_SRC_LOCAL; 1960789Sahrens } else { 1961789Sahrens (void) strlcpy(statbuf, source, statlen); 19625094Slling *srctype = ZPROP_SRC_INHERITED; 1963789Sahrens } 1964789Sahrens } 1965789Sahrens 1966789Sahrens } 1967789Sahrens 1968789Sahrens /* 1969789Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 1970789Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 1971789Sahrens * human-readable form. 1972789Sahrens * 1973789Sahrens * Returns 0 on success, or -1 on error. 1974789Sahrens */ 1975789Sahrens int 1976789Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 19775094Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 1978789Sahrens { 1979789Sahrens char *source = NULL; 1980789Sahrens uint64_t val; 1981789Sahrens char *str; 1982789Sahrens const char *root; 19832676Seschrock const char *strval; 1984789Sahrens 1985789Sahrens /* 1986789Sahrens * Check to see if this property applies to our object 1987789Sahrens */ 1988789Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 1989789Sahrens return (-1); 1990789Sahrens 1991789Sahrens if (src) 19925094Slling *src = ZPROP_SRC_NONE; 1993789Sahrens 1994789Sahrens switch (prop) { 1995789Sahrens case ZFS_PROP_CREATION: 1996789Sahrens /* 1997789Sahrens * 'creation' is a time_t stored in the statistics. We convert 1998789Sahrens * this into a string unless 'literal' is specified. 1999789Sahrens */ 2000789Sahrens { 20012885Sahrens val = getprop_uint64(zhp, prop, &source); 20022885Sahrens time_t time = (time_t)val; 2003789Sahrens struct tm t; 2004789Sahrens 2005789Sahrens if (literal || 2006789Sahrens localtime_r(&time, &t) == NULL || 2007789Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2008789Sahrens &t) == 0) 20092885Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2010789Sahrens } 2011789Sahrens break; 2012789Sahrens 2013789Sahrens case ZFS_PROP_MOUNTPOINT: 2014789Sahrens /* 2015789Sahrens * Getting the precise mountpoint can be tricky. 2016789Sahrens * 2017789Sahrens * - for 'none' or 'legacy', return those values. 2018789Sahrens * - for default mountpoints, construct it as /zfs/<dataset> 2019789Sahrens * - for inherited mountpoints, we want to take everything 2020789Sahrens * after our ancestor and append it to the inherited value. 2021789Sahrens * 2022789Sahrens * If the pool has an alternate root, we want to prepend that 2023789Sahrens * root to any values we return. 2024789Sahrens */ 20251544Seschrock root = zhp->zfs_root; 20261356Seschrock str = getprop_string(zhp, prop, &source); 20271356Seschrock 20281356Seschrock if (str[0] == '\0') { 2029789Sahrens (void) snprintf(propbuf, proplen, "%s/zfs/%s", 2030789Sahrens root, zhp->zfs_name); 20311356Seschrock } else if (str[0] == '/') { 20321356Seschrock const char *relpath = zhp->zfs_name + strlen(source); 2033789Sahrens 2034789Sahrens if (relpath[0] == '/') 2035789Sahrens relpath++; 20361356Seschrock if (str[1] == '\0') 20371356Seschrock str++; 2038789Sahrens 2039789Sahrens if (relpath[0] == '\0') 2040789Sahrens (void) snprintf(propbuf, proplen, "%s%s", 20411356Seschrock root, str); 2042789Sahrens else 2043789Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 20441356Seschrock root, str, relpath[0] == '@' ? "" : "/", 2045789Sahrens relpath); 2046789Sahrens } else { 2047789Sahrens /* 'legacy' or 'none' */ 20481356Seschrock (void) strlcpy(propbuf, str, proplen); 2049789Sahrens } 2050789Sahrens 2051789Sahrens break; 2052789Sahrens 2053789Sahrens case ZFS_PROP_ORIGIN: 20542885Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2055789Sahrens proplen); 2056789Sahrens /* 2057789Sahrens * If there is no parent at all, return failure to indicate that 2058789Sahrens * it doesn't apply to this dataset. 2059789Sahrens */ 2060789Sahrens if (propbuf[0] == '\0') 2061789Sahrens return (-1); 2062789Sahrens break; 2063789Sahrens 2064789Sahrens case ZFS_PROP_QUOTA: 2065789Sahrens case ZFS_PROP_RESERVATION: 20662082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 20672082Seschrock return (-1); 2068789Sahrens 2069789Sahrens /* 2070789Sahrens * If quota or reservation is 0, we translate this into 'none' 2071789Sahrens * (unless literal is set), and indicate that it's the default 2072789Sahrens * value. Otherwise, we print the number nicely and indicate 2073789Sahrens * that its set locally. 2074789Sahrens */ 2075789Sahrens if (val == 0) { 2076789Sahrens if (literal) 2077789Sahrens (void) strlcpy(propbuf, "0", proplen); 2078789Sahrens else 2079789Sahrens (void) strlcpy(propbuf, "none", proplen); 2080789Sahrens } else { 2081789Sahrens if (literal) 20822856Snd150628 (void) snprintf(propbuf, proplen, "%llu", 20833912Slling (u_longlong_t)val); 2084789Sahrens else 2085789Sahrens zfs_nicenum(val, propbuf, proplen); 2086789Sahrens } 2087789Sahrens break; 2088789Sahrens 2089789Sahrens case ZFS_PROP_COMPRESSRATIO: 20902082Seschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 20912082Seschrock return (-1); 20922856Snd150628 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t) 20932856Snd150628 val / 100, (longlong_t)val % 100); 2094789Sahrens break; 2095789Sahrens 2096789Sahrens case ZFS_PROP_TYPE: 2097789Sahrens switch (zhp->zfs_type) { 2098789Sahrens case ZFS_TYPE_FILESYSTEM: 2099789Sahrens str = "filesystem"; 2100789Sahrens break; 2101789Sahrens case ZFS_TYPE_VOLUME: 2102789Sahrens str = "volume"; 2103789Sahrens break; 2104789Sahrens case ZFS_TYPE_SNAPSHOT: 2105789Sahrens str = "snapshot"; 2106789Sahrens break; 2107789Sahrens default: 21082082Seschrock abort(); 2109789Sahrens } 2110789Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2111789Sahrens break; 2112789Sahrens 2113789Sahrens case ZFS_PROP_MOUNTED: 2114789Sahrens /* 2115789Sahrens * The 'mounted' property is a pseudo-property that described 2116789Sahrens * whether the filesystem is currently mounted. Even though 2117789Sahrens * it's a boolean value, the typical values of "on" and "off" 2118789Sahrens * don't make sense, so we translate to "yes" and "no". 2119789Sahrens */ 21202082Seschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 21212082Seschrock src, &source, &val) != 0) 21222082Seschrock return (-1); 21232082Seschrock if (val) 2124789Sahrens (void) strlcpy(propbuf, "yes", proplen); 2125789Sahrens else 2126789Sahrens (void) strlcpy(propbuf, "no", proplen); 2127789Sahrens break; 2128789Sahrens 2129789Sahrens case ZFS_PROP_NAME: 2130789Sahrens /* 2131789Sahrens * The 'name' property is a pseudo-property derived from the 2132789Sahrens * dataset name. It is presented as a real property to simplify 2133789Sahrens * consumers. 2134789Sahrens */ 2135789Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2136789Sahrens break; 2137789Sahrens 2138789Sahrens default: 21394577Sahrens switch (zfs_prop_get_type(prop)) { 21404787Sahrens case PROP_TYPE_NUMBER: 21414577Sahrens if (get_numeric_property(zhp, prop, src, 21424577Sahrens &source, &val) != 0) 21434577Sahrens return (-1); 21444577Sahrens if (literal) 21454577Sahrens (void) snprintf(propbuf, proplen, "%llu", 21464577Sahrens (u_longlong_t)val); 21474577Sahrens else 21484577Sahrens zfs_nicenum(val, propbuf, proplen); 21494577Sahrens break; 21504577Sahrens 21514787Sahrens case PROP_TYPE_STRING: 21524577Sahrens (void) strlcpy(propbuf, 21534577Sahrens getprop_string(zhp, prop, &source), proplen); 21544577Sahrens break; 21554577Sahrens 21564787Sahrens case PROP_TYPE_INDEX: 21574861Sahrens if (get_numeric_property(zhp, prop, src, 21584861Sahrens &source, &val) != 0) 21594861Sahrens return (-1); 21604861Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 21614577Sahrens return (-1); 21624577Sahrens (void) strlcpy(propbuf, strval, proplen); 21634577Sahrens break; 21644577Sahrens 21654577Sahrens default: 21664577Sahrens abort(); 21674577Sahrens } 2168789Sahrens } 2169789Sahrens 2170789Sahrens get_source(zhp, src, source, statbuf, statlen); 2171789Sahrens 2172789Sahrens return (0); 2173789Sahrens } 2174789Sahrens 2175789Sahrens /* 2176789Sahrens * Utility function to get the given numeric property. Does no validation that 2177789Sahrens * the given property is the appropriate type; should only be used with 2178789Sahrens * hard-coded property types. 2179789Sahrens */ 2180789Sahrens uint64_t 2181789Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2182789Sahrens { 2183789Sahrens char *source; 21845094Slling zprop_source_t sourcetype = ZPROP_SRC_NONE; 21852082Seschrock uint64_t val; 21862082Seschrock 21872082Seschrock (void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); 21882082Seschrock 21892082Seschrock return (val); 2190789Sahrens } 2191789Sahrens 2192789Sahrens /* 2193789Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2194789Sahrens */ 2195789Sahrens int 2196789Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 21975094Slling zprop_source_t *src, char *statbuf, size_t statlen) 2198789Sahrens { 2199789Sahrens char *source; 2200789Sahrens 2201789Sahrens /* 2202789Sahrens * Check to see if this property applies to our object 2203789Sahrens */ 22044849Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 22053237Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 22062082Seschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 22072082Seschrock zfs_prop_to_name(prop))); 22084849Sahrens } 2209789Sahrens 2210789Sahrens if (src) 22115094Slling *src = ZPROP_SRC_NONE; 2212789Sahrens 22132082Seschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 22142082Seschrock return (-1); 2215789Sahrens 2216789Sahrens get_source(zhp, src, source, statbuf, statlen); 2217789Sahrens 2218789Sahrens return (0); 2219789Sahrens } 2220789Sahrens 2221789Sahrens /* 2222789Sahrens * Returns the name of the given zfs handle. 2223789Sahrens */ 2224789Sahrens const char * 2225789Sahrens zfs_get_name(const zfs_handle_t *zhp) 2226789Sahrens { 2227789Sahrens return (zhp->zfs_name); 2228789Sahrens } 2229789Sahrens 2230789Sahrens /* 2231789Sahrens * Returns the type of the given zfs handle. 2232789Sahrens */ 2233789Sahrens zfs_type_t 2234789Sahrens zfs_get_type(const zfs_handle_t *zhp) 2235789Sahrens { 2236789Sahrens return (zhp->zfs_type); 2237789Sahrens } 2238789Sahrens 2239789Sahrens /* 22401356Seschrock * Iterate over all child filesystems 2241789Sahrens */ 2242789Sahrens int 22431356Seschrock zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) 2244789Sahrens { 2245789Sahrens zfs_cmd_t zc = { 0 }; 2246789Sahrens zfs_handle_t *nzhp; 2247789Sahrens int ret; 2248789Sahrens 2249789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 22502082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; 2251789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2252789Sahrens /* 2253789Sahrens * Ignore private dataset names. 2254789Sahrens */ 2255789Sahrens if (dataset_name_hidden(zc.zc_name)) 2256789Sahrens continue; 2257789Sahrens 2258789Sahrens /* 2259789Sahrens * Silently ignore errors, as the only plausible explanation is 2260789Sahrens * that the pool has since been removed. 2261789Sahrens */ 22622082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 22632082Seschrock zc.zc_name)) == NULL) 2264789Sahrens continue; 2265789Sahrens 2266789Sahrens if ((ret = func(nzhp, data)) != 0) 2267789Sahrens return (ret); 2268789Sahrens } 2269789Sahrens 2270789Sahrens /* 2271789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2272789Sahrens * returned, then the underlying dataset has been removed since we 2273789Sahrens * obtained the handle. 2274789Sahrens */ 2275789Sahrens if (errno != ESRCH && errno != ENOENT) 22762082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 22772082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2278789Sahrens 22791356Seschrock return (0); 22801356Seschrock } 22811356Seschrock 22821356Seschrock /* 22831356Seschrock * Iterate over all snapshots 22841356Seschrock */ 22851356Seschrock int 22861356Seschrock zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) 22871356Seschrock { 22881356Seschrock zfs_cmd_t zc = { 0 }; 22891356Seschrock zfs_handle_t *nzhp; 22901356Seschrock int ret; 2291789Sahrens 2292789Sahrens for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 22932082Seschrock ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, 22942082Seschrock &zc) == 0; 2295789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { 2296789Sahrens 22972082Seschrock if ((nzhp = make_dataset_handle(zhp->zfs_hdl, 22982082Seschrock zc.zc_name)) == NULL) 2299789Sahrens continue; 2300789Sahrens 2301789Sahrens if ((ret = func(nzhp, data)) != 0) 2302789Sahrens return (ret); 2303789Sahrens } 2304789Sahrens 2305789Sahrens /* 2306789Sahrens * An errno value of ESRCH indicates normal completion. If ENOENT is 2307789Sahrens * returned, then the underlying dataset has been removed since we 2308789Sahrens * obtained the handle. Silently ignore this case, and return success. 2309789Sahrens */ 2310789Sahrens if (errno != ESRCH && errno != ENOENT) 23112082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 23122082Seschrock dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); 2313789Sahrens 2314789Sahrens return (0); 2315789Sahrens } 2316789Sahrens 2317789Sahrens /* 23181356Seschrock * Iterate over all children, snapshots and filesystems 23191356Seschrock */ 23201356Seschrock int 23211356Seschrock zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) 23221356Seschrock { 23231356Seschrock int ret; 23241356Seschrock 23251356Seschrock if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) 23261356Seschrock return (ret); 23271356Seschrock 23281356Seschrock return (zfs_iter_snapshots(zhp, func, data)); 23291356Seschrock } 23301356Seschrock 23311356Seschrock /* 2332789Sahrens * Given a complete name, return just the portion that refers to the parent. 2333789Sahrens * Can return NULL if this is a pool. 2334789Sahrens */ 2335789Sahrens static int 2336789Sahrens parent_name(const char *path, char *buf, size_t buflen) 2337789Sahrens { 2338789Sahrens char *loc; 2339789Sahrens 2340789Sahrens if ((loc = strrchr(path, '/')) == NULL) 2341789Sahrens return (-1); 2342789Sahrens 2343789Sahrens (void) strncpy(buf, path, MIN(buflen, loc - path)); 2344789Sahrens buf[loc - path] = '\0'; 2345789Sahrens 2346789Sahrens return (0); 2347789Sahrens } 2348789Sahrens 2349789Sahrens /* 23504490Svb160487 * If accept_ancestor is false, then check to make sure that the given path has 23514490Svb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 23524490Svb160487 * closest existing ancestor for the given path. In prefixlen return the 23534490Svb160487 * length of already existing prefix of the given path. We also fetch the 23544490Svb160487 * 'zoned' property, which is used to validate property settings when creating 23554490Svb160487 * new datasets. 2356789Sahrens */ 2357789Sahrens static int 23584490Svb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 23594490Svb160487 boolean_t accept_ancestor, int *prefixlen) 2360789Sahrens { 2361789Sahrens zfs_cmd_t zc = { 0 }; 2362789Sahrens char parent[ZFS_MAXNAMELEN]; 2363789Sahrens char *slash; 23641356Seschrock zfs_handle_t *zhp; 23652082Seschrock char errbuf[1024]; 23662082Seschrock 23672082Seschrock (void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", 23682082Seschrock path); 2369789Sahrens 2370789Sahrens /* get parent, and check to see if this is just a pool */ 2371789Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 23722082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23732082Seschrock "missing dataset name")); 23742082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2375789Sahrens } 2376789Sahrens 2377789Sahrens /* check to see if the pool exists */ 2378789Sahrens if ((slash = strchr(parent, '/')) == NULL) 2379789Sahrens slash = parent + strlen(parent); 2380789Sahrens (void) strncpy(zc.zc_name, parent, slash - parent); 2381789Sahrens zc.zc_name[slash - parent] = '\0'; 23822082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2383789Sahrens errno == ENOENT) { 23842082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23852082Seschrock "no such pool '%s'"), zc.zc_name); 23862082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2387789Sahrens } 2388789Sahrens 2389789Sahrens /* check to see if the parent dataset exists */ 23904490Svb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 23914490Svb160487 if (errno == ENOENT && accept_ancestor) { 23924490Svb160487 /* 23934490Svb160487 * Go deeper to find an ancestor, give up on top level. 23944490Svb160487 */ 23954490Svb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 23964490Svb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 23974490Svb160487 "no such pool '%s'"), zc.zc_name); 23984490Svb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 23994490Svb160487 } 24004490Svb160487 } else if (errno == ENOENT) { 24012082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24022082Seschrock "parent does not exist")); 24032082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 24044490Svb160487 } else 24052082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2406789Sahrens } 2407789Sahrens 24082676Seschrock *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2409789Sahrens /* we are in a non-global zone, but parent is in the global zone */ 24102676Seschrock if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) { 24112082Seschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 24121356Seschrock zfs_close(zhp); 2413789Sahrens return (-1); 2414789Sahrens } 2415789Sahrens 2416789Sahrens /* make sure parent is a filesystem */ 24171356Seschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 24182082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 24192082Seschrock "parent is not a filesystem")); 24202082Seschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 24211356Seschrock zfs_close(zhp); 2422789Sahrens return (-1); 2423789Sahrens } 2424789Sahrens 24251356Seschrock zfs_close(zhp); 24264490Svb160487 if (prefixlen != NULL) 24274490Svb160487 *prefixlen = strlen(parent); 24284490Svb160487 return (0); 24294490Svb160487 } 24304490Svb160487 24314490Svb160487 /* 24324490Svb160487 * Finds whether the dataset of the given type(s) exists. 24334490Svb160487 */ 24344490Svb160487 boolean_t 24354490Svb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 24364490Svb160487 { 24374490Svb160487 zfs_handle_t *zhp; 24384490Svb160487 2439*5326Sek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 24404490Svb160487 return (B_FALSE); 24414490Svb160487 24424490Svb160487 /* 24434490Svb160487 * Try to get stats for the dataset, which will tell us if it exists. 24444490Svb160487 */ 24454490Svb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 24464490Svb160487 int ds_type = zhp->zfs_type; 24474490Svb160487 24484490Svb160487 zfs_close(zhp); 24494490Svb160487 if (types & ds_type) 24504490Svb160487 return (B_TRUE); 24514490Svb160487 } 24524490Svb160487 return (B_FALSE); 24534490Svb160487 } 24544490Svb160487 24554490Svb160487 /* 24564490Svb160487 * Creates non-existing ancestors of the given path. 24574490Svb160487 */ 24584490Svb160487 int 24594490Svb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 24604490Svb160487 { 24614490Svb160487 int prefix; 24624490Svb160487 uint64_t zoned; 24634490Svb160487 char *path_copy; 24644490Svb160487 int rc; 24654490Svb160487 24664490Svb160487 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0) 24674490Svb160487 return (-1); 24684490Svb160487 24694490Svb160487 if ((path_copy = strdup(path)) != NULL) { 24704490Svb160487 rc = create_parents(hdl, path_copy, prefix); 24714490Svb160487 free(path_copy); 24724490Svb160487 } 24734490Svb160487 if (path_copy == NULL || rc != 0) 24744490Svb160487 return (-1); 24754490Svb160487 2476789Sahrens return (0); 2477789Sahrens } 2478789Sahrens 2479789Sahrens /* 24802676Seschrock * Create a new filesystem or volume. 2481789Sahrens */ 2482789Sahrens int 24832082Seschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 24842676Seschrock nvlist_t *props) 2485789Sahrens { 2486789Sahrens zfs_cmd_t zc = { 0 }; 2487789Sahrens int ret; 2488789Sahrens uint64_t size = 0; 2489789Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 24902082Seschrock char errbuf[1024]; 24912676Seschrock uint64_t zoned; 24922082Seschrock 24932082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 24942082Seschrock "cannot create '%s'"), path); 2495789Sahrens 2496789Sahrens /* validate the path, taking care to note the extended error message */ 2497*5326Sek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 24982082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2499789Sahrens 2500789Sahrens /* validate parents exist */ 25014490Svb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2502789Sahrens return (-1); 2503789Sahrens 2504789Sahrens /* 2505789Sahrens * The failure modes when creating a dataset of a different type over 2506789Sahrens * one that already exists is a little strange. In particular, if you 2507789Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2508789Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2509789Sahrens * first try to see if the dataset exists. 2510789Sahrens */ 2511789Sahrens (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); 25125094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 25132082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25142082Seschrock "dataset already exists")); 25152082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2516789Sahrens } 2517789Sahrens 2518789Sahrens if (type == ZFS_TYPE_VOLUME) 2519789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2520789Sahrens else 2521789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2522789Sahrens 25235094Slling if (props && (props = zfs_validate_properties(hdl, type, props, 25243912Slling zoned, NULL, errbuf)) == 0) 25252676Seschrock return (-1); 25262676Seschrock 2527789Sahrens if (type == ZFS_TYPE_VOLUME) { 25281133Seschrock /* 25291133Seschrock * If we are creating a volume, the size and block size must 25301133Seschrock * satisfy a few restraints. First, the blocksize must be a 25311133Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 25321133Seschrock * volsize must be a multiple of the block size, and cannot be 25331133Seschrock * zero. 25341133Seschrock */ 25352676Seschrock if (props == NULL || nvlist_lookup_uint64(props, 25362676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 25372676Seschrock nvlist_free(props); 25382082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25392676Seschrock "missing volume size")); 25402676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2541789Sahrens } 2542789Sahrens 25432676Seschrock if ((ret = nvlist_lookup_uint64(props, 25442676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 25452676Seschrock &blocksize)) != 0) { 25462676Seschrock if (ret == ENOENT) { 25472676Seschrock blocksize = zfs_prop_default_numeric( 25482676Seschrock ZFS_PROP_VOLBLOCKSIZE); 25492676Seschrock } else { 25502676Seschrock nvlist_free(props); 25512676Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25522676Seschrock "missing volume block size")); 25532676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 25542676Seschrock } 25552676Seschrock } 25562676Seschrock 25572676Seschrock if (size == 0) { 25582676Seschrock nvlist_free(props); 25592082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25602676Seschrock "volume size cannot be zero")); 25612676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 25621133Seschrock } 25631133Seschrock 25641133Seschrock if (size % blocksize != 0) { 25652676Seschrock nvlist_free(props); 25662082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 25672676Seschrock "volume size must be a multiple of volume block " 25682676Seschrock "size")); 25692676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 25701133Seschrock } 2571789Sahrens } 2572789Sahrens 25735094Slling if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0) 25742676Seschrock return (-1); 25752676Seschrock nvlist_free(props); 25762676Seschrock 2577789Sahrens /* create the dataset */ 25784543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc); 2579789Sahrens 25803912Slling if (ret == 0 && type == ZFS_TYPE_VOLUME) { 25812082Seschrock ret = zvol_create_link(hdl, path); 25823912Slling if (ret) { 25833912Slling (void) zfs_standard_error(hdl, errno, 25843912Slling dgettext(TEXT_DOMAIN, 25853912Slling "Volume successfully created, but device links " 25863912Slling "were not created")); 25873912Slling zcmd_free_nvlists(&zc); 25883912Slling return (-1); 25893912Slling } 25903912Slling } 2591789Sahrens 25922676Seschrock zcmd_free_nvlists(&zc); 25932676Seschrock 2594789Sahrens /* check for failure */ 2595789Sahrens if (ret != 0) { 2596789Sahrens char parent[ZFS_MAXNAMELEN]; 2597789Sahrens (void) parent_name(path, parent, sizeof (parent)); 2598789Sahrens 2599789Sahrens switch (errno) { 2600789Sahrens case ENOENT: 26012082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26022082Seschrock "no such parent '%s'"), parent); 26032082Seschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2604789Sahrens 2605789Sahrens case EINVAL: 26062082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26073413Smmusante "parent '%s' is not a filesystem"), parent); 26082082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 2609789Sahrens 2610789Sahrens case EDOM: 26112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26122676Seschrock "volume block size must be power of 2 from " 26132676Seschrock "%u to %uk"), 2614789Sahrens (uint_t)SPA_MINBLOCKSIZE, 2615789Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 26162082Seschrock 26172676Seschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 26182082Seschrock 26194603Sahrens case ENOTSUP: 26204603Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 26214603Sahrens "pool must be upgraded to set this " 26224603Sahrens "property or value")); 26234603Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 26244603Sahrens 2625789Sahrens #ifdef _ILP32 2626789Sahrens case EOVERFLOW: 2627789Sahrens /* 2628789Sahrens * This platform can't address a volume this big. 2629789Sahrens */ 26302082Seschrock if (type == ZFS_TYPE_VOLUME) 26312082Seschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 26322082Seschrock errbuf)); 2633789Sahrens #endif 26342082Seschrock /* FALLTHROUGH */ 2635789Sahrens default: 26362082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 2637789Sahrens } 2638789Sahrens } 2639789Sahrens 2640789Sahrens return (0); 2641789Sahrens } 2642789Sahrens 2643789Sahrens /* 2644789Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 2645789Sahrens * isn't mounted, and that there are no active dependents. 2646789Sahrens */ 2647789Sahrens int 2648789Sahrens zfs_destroy(zfs_handle_t *zhp) 2649789Sahrens { 2650789Sahrens zfs_cmd_t zc = { 0 }; 2651789Sahrens 2652789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2653789Sahrens 26542676Seschrock if (ZFS_IS_VOLUME(zhp)) { 26553126Sahl /* 26564543Smarks * If user doesn't have permissions to unshare volume, then 26574543Smarks * abort the request. This would only happen for a 26584543Smarks * non-privileged user. 26593126Sahl */ 26604543Smarks if (zfs_unshare_iscsi(zhp) != 0) { 26614543Smarks return (-1); 26624543Smarks } 26633126Sahl 26642082Seschrock if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 2665789Sahrens return (-1); 2666789Sahrens 2667789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 2668789Sahrens } else { 2669789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 2670789Sahrens } 2671789Sahrens 26724543Smarks if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) { 26733237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 26742082Seschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 26752082Seschrock zhp->zfs_name)); 26762199Sahrens } 2677789Sahrens 2678789Sahrens remove_mountpoint(zhp); 2679789Sahrens 2680789Sahrens return (0); 2681789Sahrens } 2682789Sahrens 26832199Sahrens struct destroydata { 26842199Sahrens char *snapname; 26852199Sahrens boolean_t gotone; 26863265Sahrens boolean_t closezhp; 26872199Sahrens }; 26882199Sahrens 26892199Sahrens static int 26902199Sahrens zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) 26912199Sahrens { 26922199Sahrens struct destroydata *dd = arg; 26932199Sahrens zfs_handle_t *szhp; 26942199Sahrens char name[ZFS_MAXNAMELEN]; 26953265Sahrens boolean_t closezhp = dd->closezhp; 26963265Sahrens int rv; 26972199Sahrens 26982676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 26992676Seschrock (void) strlcat(name, "@", sizeof (name)); 27002676Seschrock (void) strlcat(name, dd->snapname, sizeof (name)); 27012199Sahrens 27022199Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 27032199Sahrens if (szhp) { 27042199Sahrens dd->gotone = B_TRUE; 27052199Sahrens zfs_close(szhp); 27062199Sahrens } 27072199Sahrens 27082199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 27092199Sahrens (void) zvol_remove_link(zhp->zfs_hdl, name); 27102199Sahrens /* 27112199Sahrens * NB: this is simply a best-effort. We don't want to 27122199Sahrens * return an error, because then we wouldn't visit all 27132199Sahrens * the volumes. 27142199Sahrens */ 27152199Sahrens } 27162199Sahrens 27173265Sahrens dd->closezhp = B_TRUE; 27183265Sahrens rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg); 27193265Sahrens if (closezhp) 27203265Sahrens zfs_close(zhp); 27213265Sahrens return (rv); 27222199Sahrens } 27232199Sahrens 27242199Sahrens /* 27252199Sahrens * Destroys all snapshots with the given name in zhp & descendants. 27262199Sahrens */ 27272199Sahrens int 27282199Sahrens zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) 27292199Sahrens { 27302199Sahrens zfs_cmd_t zc = { 0 }; 27312199Sahrens int ret; 27322199Sahrens struct destroydata dd = { 0 }; 27332199Sahrens 27342199Sahrens dd.snapname = snapname; 27352199Sahrens (void) zfs_remove_link_cb(zhp, &dd); 27362199Sahrens 27372199Sahrens if (!dd.gotone) { 27383237Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 27392199Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 27402199Sahrens zhp->zfs_name, snapname)); 27412199Sahrens } 27422199Sahrens 27432199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 27442676Seschrock (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 27452199Sahrens 27464543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc); 27472199Sahrens if (ret != 0) { 27482199Sahrens char errbuf[1024]; 27492199Sahrens 27502199Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 27512199Sahrens "cannot destroy '%s@%s'"), zc.zc_name, snapname); 27522199Sahrens 27532199Sahrens switch (errno) { 27542199Sahrens case EEXIST: 27552199Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 27562199Sahrens "snapshot is cloned")); 27572199Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); 27582199Sahrens 27592199Sahrens default: 27602199Sahrens return (zfs_standard_error(zhp->zfs_hdl, errno, 27612199Sahrens errbuf)); 27622199Sahrens } 27632199Sahrens } 27642199Sahrens 27652199Sahrens return (0); 27662199Sahrens } 27672199Sahrens 2768789Sahrens /* 2769789Sahrens * Clones the given dataset. The target must be of the same type as the source. 2770789Sahrens */ 2771789Sahrens int 27722676Seschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 2773789Sahrens { 2774789Sahrens zfs_cmd_t zc = { 0 }; 2775789Sahrens char parent[ZFS_MAXNAMELEN]; 2776789Sahrens int ret; 27772082Seschrock char errbuf[1024]; 27782082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 27792676Seschrock zfs_type_t type; 27802676Seschrock uint64_t zoned; 2781789Sahrens 2782789Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 2783789Sahrens 27842082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 27852082Seschrock "cannot create '%s'"), target); 27862082Seschrock 2787789Sahrens /* validate the target name */ 2788*5326Sek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 27892082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2790789Sahrens 2791789Sahrens /* validate parents exist */ 27924490Svb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 2793789Sahrens return (-1); 2794789Sahrens 2795789Sahrens (void) parent_name(target, parent, sizeof (parent)); 2796789Sahrens 2797789Sahrens /* do the clone */ 27982676Seschrock if (ZFS_IS_VOLUME(zhp)) { 2799789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 28002744Snn35248 type = ZFS_TYPE_VOLUME; 28012676Seschrock } else { 2802789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 28032744Snn35248 type = ZFS_TYPE_FILESYSTEM; 28042676Seschrock } 28052676Seschrock 28062676Seschrock if (props) { 28075094Slling if ((props = zfs_validate_properties(hdl, type, props, 28083912Slling zoned, zhp, errbuf)) == NULL) 28092676Seschrock return (-1); 28102676Seschrock 28115094Slling if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) { 28122676Seschrock nvlist_free(props); 28132676Seschrock return (-1); 28142676Seschrock } 28152676Seschrock 28162676Seschrock nvlist_free(props); 28172676Seschrock } 2818789Sahrens 2819789Sahrens (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); 28202676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value)); 28214543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc); 2822789Sahrens 28232676Seschrock zcmd_free_nvlists(&zc); 28242676Seschrock 2825789Sahrens if (ret != 0) { 2826789Sahrens switch (errno) { 2827789Sahrens 2828789Sahrens case ENOENT: 2829789Sahrens /* 2830789Sahrens * The parent doesn't exist. We should have caught this 2831789Sahrens * above, but there may a race condition that has since 2832789Sahrens * destroyed the parent. 2833789Sahrens * 2834789Sahrens * At this point, we don't know whether it's the source 2835789Sahrens * that doesn't exist anymore, or whether the target 2836789Sahrens * dataset doesn't exist. 2837789Sahrens */ 28382082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28392082Seschrock "no such parent '%s'"), parent); 28402082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 28412082Seschrock 28422082Seschrock case EXDEV: 28432082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28442082Seschrock "source and target pools differ")); 28452082Seschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 28462082Seschrock errbuf)); 28472082Seschrock 28482082Seschrock default: 28492082Seschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 28502082Seschrock errbuf)); 28512082Seschrock } 28522676Seschrock } else if (ZFS_IS_VOLUME(zhp)) { 28532082Seschrock ret = zvol_create_link(zhp->zfs_hdl, target); 28542082Seschrock } 28552082Seschrock 28562082Seschrock return (ret); 28572082Seschrock } 28582082Seschrock 28592082Seschrock typedef struct promote_data { 28602082Seschrock char cb_mountpoint[MAXPATHLEN]; 28612082Seschrock const char *cb_target; 28622082Seschrock const char *cb_errbuf; 28632082Seschrock uint64_t cb_pivot_txg; 28642082Seschrock } promote_data_t; 28652082Seschrock 28662082Seschrock static int 28672082Seschrock promote_snap_cb(zfs_handle_t *zhp, void *data) 28682082Seschrock { 28692082Seschrock promote_data_t *pd = data; 28702082Seschrock zfs_handle_t *szhp; 28712082Seschrock char snapname[MAXPATHLEN]; 28723265Sahrens int rv = 0; 28732082Seschrock 28742082Seschrock /* We don't care about snapshots after the pivot point */ 28753265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) { 28763265Sahrens zfs_close(zhp); 28772082Seschrock return (0); 28783265Sahrens } 28792082Seschrock 28802417Sahrens /* Remove the device link if it's a zvol. */ 28812676Seschrock if (ZFS_IS_VOLUME(zhp)) 28822417Sahrens (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); 28832082Seschrock 28842082Seschrock /* Check for conflicting names */ 28852676Seschrock (void) strlcpy(snapname, pd->cb_target, sizeof (snapname)); 28862676Seschrock (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname)); 28872082Seschrock szhp = make_dataset_handle(zhp->zfs_hdl, snapname); 28882082Seschrock if (szhp != NULL) { 28892082Seschrock zfs_close(szhp); 28902082Seschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 28912082Seschrock "snapshot name '%s' from origin \n" 28922082Seschrock "conflicts with '%s' from target"), 28932082Seschrock zhp->zfs_name, snapname); 28943265Sahrens rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf); 28952082Seschrock } 28963265Sahrens zfs_close(zhp); 28973265Sahrens return (rv); 28982082Seschrock } 28992082Seschrock 29002417Sahrens static int 29012417Sahrens promote_snap_done_cb(zfs_handle_t *zhp, void *data) 29022417Sahrens { 29032417Sahrens promote_data_t *pd = data; 29042417Sahrens 29052417Sahrens /* We don't care about snapshots after the pivot point */ 29063265Sahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) { 29073265Sahrens /* Create the device link if it's a zvol. */ 29083265Sahrens if (ZFS_IS_VOLUME(zhp)) 29093265Sahrens (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 29103265Sahrens } 29113265Sahrens 29123265Sahrens zfs_close(zhp); 29132417Sahrens return (0); 29142417Sahrens } 29152417Sahrens 29162082Seschrock /* 29172082Seschrock * Promotes the given clone fs to be the clone parent. 29182082Seschrock */ 29192082Seschrock int 29202082Seschrock zfs_promote(zfs_handle_t *zhp) 29212082Seschrock { 29222082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 29232082Seschrock zfs_cmd_t zc = { 0 }; 29242082Seschrock char parent[MAXPATHLEN]; 29252082Seschrock char *cp; 29262082Seschrock int ret; 29272082Seschrock zfs_handle_t *pzhp; 29282082Seschrock promote_data_t pd; 29292082Seschrock char errbuf[1024]; 29302082Seschrock 29312082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 29322082Seschrock "cannot promote '%s'"), zhp->zfs_name); 29332082Seschrock 29342082Seschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 29352082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29362082Seschrock "snapshots can not be promoted")); 29372082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 29382082Seschrock } 29392082Seschrock 29402676Seschrock (void) strlcpy(parent, zhp->zfs_dmustats.dds_clone_of, sizeof (parent)); 29412082Seschrock if (parent[0] == '\0') { 29422082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29432082Seschrock "not a cloned filesystem")); 29442082Seschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 29452082Seschrock } 29462082Seschrock cp = strchr(parent, '@'); 29472082Seschrock *cp = '\0'; 29482082Seschrock 29492082Seschrock /* Walk the snapshots we will be moving */ 29502082Seschrock pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); 29512082Seschrock if (pzhp == NULL) 29522082Seschrock return (-1); 29532082Seschrock pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); 29542082Seschrock zfs_close(pzhp); 29552082Seschrock pd.cb_target = zhp->zfs_name; 29562082Seschrock pd.cb_errbuf = errbuf; 29575094Slling pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET); 29582082Seschrock if (pzhp == NULL) 29592082Seschrock return (-1); 29602082Seschrock (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, 29612082Seschrock sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); 29622082Seschrock ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); 29632417Sahrens if (ret != 0) { 29642417Sahrens zfs_close(pzhp); 29652082Seschrock return (-1); 29662417Sahrens } 29672082Seschrock 29682082Seschrock /* issue the ioctl */ 29692676Seschrock (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_clone_of, 29702676Seschrock sizeof (zc.zc_value)); 29712082Seschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 29724543Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 29732082Seschrock 29742082Seschrock if (ret != 0) { 29752417Sahrens int save_errno = errno; 29762417Sahrens 29772417Sahrens (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); 29782417Sahrens zfs_close(pzhp); 29792417Sahrens 29802417Sahrens switch (save_errno) { 2981789Sahrens case EEXIST: 2982789Sahrens /* 29832082Seschrock * There is a conflicting snapshot name. We 29842082Seschrock * should have caught this above, but they could 29852082Seschrock * have renamed something in the mean time. 2986789Sahrens */ 29872082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29882082Seschrock "conflicting snapshot name from parent '%s'"), 29892082Seschrock parent); 29902082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2991789Sahrens 2992789Sahrens default: 29932417Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 2994789Sahrens } 29952417Sahrens } else { 29962417Sahrens (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); 2997789Sahrens } 2998789Sahrens 29992417Sahrens zfs_close(pzhp); 3000789Sahrens return (ret); 3001789Sahrens } 3002789Sahrens 30034007Smmusante struct createdata { 30044007Smmusante const char *cd_snapname; 30054007Smmusante int cd_ifexists; 30064007Smmusante }; 30074007Smmusante 30082199Sahrens static int 30092199Sahrens zfs_create_link_cb(zfs_handle_t *zhp, void *arg) 30102199Sahrens { 30114007Smmusante struct createdata *cd = arg; 30122676Seschrock int ret; 30132199Sahrens 30142199Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 30152199Sahrens char name[MAXPATHLEN]; 30162199Sahrens 30172676Seschrock (void) strlcpy(name, zhp->zfs_name, sizeof (name)); 30182676Seschrock (void) strlcat(name, "@", sizeof (name)); 30194007Smmusante (void) strlcat(name, cd->cd_snapname, sizeof (name)); 30204007Smmusante (void) zvol_create_link_common(zhp->zfs_hdl, name, 30214007Smmusante cd->cd_ifexists); 30222199Sahrens /* 30232199Sahrens * NB: this is simply a best-effort. We don't want to 30242199Sahrens * return an error, because then we wouldn't visit all 30252199Sahrens * the volumes. 30262199Sahrens */ 30272199Sahrens } 30282676Seschrock 30294007Smmusante ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd); 30302676Seschrock 30312676Seschrock zfs_close(zhp); 30322676Seschrock 30332676Seschrock return (ret); 30342199Sahrens } 30352199Sahrens 3036789Sahrens /* 30373504Sahl * Takes a snapshot of the given dataset. 3038789Sahrens */ 3039789Sahrens int 30402199Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) 3041789Sahrens { 3042789Sahrens const char *delim; 3043789Sahrens char *parent; 3044789Sahrens zfs_handle_t *zhp; 3045789Sahrens zfs_cmd_t zc = { 0 }; 3046789Sahrens int ret; 30472082Seschrock char errbuf[1024]; 30482082Seschrock 30492082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30502082Seschrock "cannot snapshot '%s'"), path); 30512082Seschrock 30522082Seschrock /* validate the target name */ 3053*5326Sek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 30542082Seschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3055789Sahrens 3056789Sahrens /* make sure the parent exists and is of the appropriate type */ 30572199Sahrens delim = strchr(path, '@'); 30582082Seschrock if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) 30592082Seschrock return (-1); 3060789Sahrens (void) strncpy(parent, path, delim - path); 3061789Sahrens parent[delim - path] = '\0'; 3062789Sahrens 30632082Seschrock if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | 3064789Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3065789Sahrens free(parent); 3066789Sahrens return (-1); 3067789Sahrens } 3068789Sahrens 30692199Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 30702676Seschrock (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value)); 30714543Smarks if (ZFS_IS_VOLUME(zhp)) 30724543Smarks zc.zc_objset_type = DMU_OST_ZVOL; 30734543Smarks else 30744543Smarks zc.zc_objset_type = DMU_OST_ZFS; 30752199Sahrens zc.zc_cookie = recursive; 30764543Smarks ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc); 30772199Sahrens 30782199Sahrens /* 30792199Sahrens * if it was recursive, the one that actually failed will be in 30802199Sahrens * zc.zc_name. 30812199Sahrens */ 30824543Smarks if (ret != 0) 30834543Smarks (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 30844543Smarks "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value); 30854543Smarks 30862199Sahrens if (ret == 0 && recursive) { 30874007Smmusante struct createdata cd; 30884007Smmusante 30894007Smmusante cd.cd_snapname = delim + 1; 30904007Smmusante cd.cd_ifexists = B_FALSE; 30914007Smmusante (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd); 30922199Sahrens } 3093789Sahrens if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { 30942082Seschrock ret = zvol_create_link(zhp->zfs_hdl, path); 30952199Sahrens if (ret != 0) { 30964543Smarks (void) zfs_standard_error(hdl, errno, 30974543Smarks dgettext(TEXT_DOMAIN, 30984543Smarks "Volume successfully snapshotted, but device links " 30994543Smarks "were not created")); 31004543Smarks free(parent); 31014543Smarks zfs_close(zhp); 31024543Smarks return (-1); 31032199Sahrens } 3104789Sahrens } 3105789Sahrens 31062082Seschrock if (ret != 0) 31072082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3108789Sahrens 3109789Sahrens free(parent); 3110789Sahrens zfs_close(zhp); 3111789Sahrens 3112789Sahrens return (ret); 3113789Sahrens } 3114789Sahrens 3115789Sahrens /* 31163504Sahl * Dumps a backup of the given snapshot (incremental from fromsnap if it's not 31173504Sahl * NULL) to the file descriptor specified by outfd. 3118789Sahrens */ 3119789Sahrens int 31203504Sahl zfs_send(zfs_handle_t *zhp, const char *fromsnap, int outfd) 3121789Sahrens { 3122789Sahrens zfs_cmd_t zc = { 0 }; 31232082Seschrock char errbuf[1024]; 31242885Sahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 31252082Seschrock 31263504Sahl assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 31273504Sahl 31282885Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 31292885Sahrens if (fromsnap) 31302885Sahrens (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_name)); 31313504Sahl zc.zc_cookie = outfd; 31323504Sahl 31333504Sahl if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc) != 0) { 31343504Sahl (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 31353504Sahl "cannot send '%s'"), zhp->zfs_name); 31363504Sahl 3137789Sahrens switch (errno) { 3138789Sahrens 3139789Sahrens case EXDEV: 31402082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31413413Smmusante "not an earlier snapshot from the same fs")); 31422082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3143789Sahrens 3144789Sahrens case EDQUOT: 3145789Sahrens case EFBIG: 3146789Sahrens case EIO: 3147789Sahrens case ENOLINK: 3148789Sahrens case ENOSPC: 3149789Sahrens case ENOSTR: 3150789Sahrens case ENXIO: 3151789Sahrens case EPIPE: 3152789Sahrens case ERANGE: 3153789Sahrens case EFAULT: 3154789Sahrens case EROFS: 31552082Seschrock zfs_error_aux(hdl, strerror(errno)); 31562082Seschrock return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); 3157789Sahrens 3158789Sahrens default: 31592082Seschrock return (zfs_standard_error(hdl, errno, errbuf)); 3160789Sahrens } 3161789Sahrens } 3162789Sahrens 31633504Sahl return (0); 3164789Sahrens } 3165789Sahrens 3166789Sahrens /* 31672885Sahrens * Create ancestors of 'target', but not target itself, and not 31682885Sahrens * ancestors whose names are shorter than prefixlen. Die if 31692885Sahrens * prefixlen-ancestor does not exist. 31702885Sahrens */ 31712885Sahrens static int 31722885Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 31732885Sahrens { 31742885Sahrens zfs_handle_t *h; 31752885Sahrens char *cp; 31762885Sahrens 31772885Sahrens /* make sure prefix exists */ 31782885Sahrens cp = strchr(target + prefixlen, '/'); 31794603Sahrens if (cp == NULL) { 31804603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 31814603Sahrens } else { 31824603Sahrens *cp = '\0'; 31834603Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 31844603Sahrens *cp = '/'; 31854603Sahrens } 31862885Sahrens if (h == NULL) 31872885Sahrens return (-1); 31882885Sahrens zfs_close(h); 31892885Sahrens 31902885Sahrens /* 31912885Sahrens * Attempt to create, mount, and share any ancestor filesystems, 31922885Sahrens * up to the prefixlen-long one. 31932885Sahrens */ 31942885Sahrens for (cp = target + prefixlen + 1; 31952885Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 31962885Sahrens const char *opname; 31974543Smarks char *logstr; 31982885Sahrens 31992885Sahrens *cp = '\0'; 32002885Sahrens 32012885Sahrens h = make_dataset_handle(hdl, target); 32022885Sahrens if (h) { 32032885Sahrens /* it already exists, nothing to do here */ 32042885Sahrens zfs_close(h); 32052885Sahrens continue; 32062885Sahrens } 32072885Sahrens 32082885Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 32094543Smarks logstr = hdl->libzfs_log_str; 32104543Smarks hdl->libzfs_log_str = NULL; 32112885Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 32124543Smarks NULL) != 0) { 32134543Smarks hdl->libzfs_log_str = logstr; 32142885Sahrens goto ancestorerr; 32154543Smarks } 32164543Smarks 32174543Smarks hdl->libzfs_log_str = logstr; 32182885Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 32192885Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 32202885Sahrens if (h == NULL) 32212885Sahrens goto ancestorerr; 32222885Sahrens 32232885Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 32242885Sahrens if (zfs_mount(h, NULL, 0) != 0) 32252885Sahrens goto ancestorerr; 32262885Sahrens 32272885Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 32282885Sahrens if (zfs_share(h) != 0) 32292885Sahrens goto ancestorerr; 32302885Sahrens 32312885Sahrens zfs_close(h); 32322885Sahrens 32332885Sahrens continue; 32342885Sahrens ancestorerr: 32352885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 32362885Sahrens "failed to %s ancestor '%s'"), opname, target); 32372885Sahrens return (-1); 32382885Sahrens } 32392885Sahrens 32402885Sahrens return (0); 32412885Sahrens } 32422885Sahrens 32432885Sahrens /* 32443504Sahl * Restores a backup of tosnap from the file descriptor specified by infd. 3245789Sahrens */ 3246789Sahrens int 32472082Seschrock zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, 32483504Sahl int verbose, int dryrun, boolean_t force, int infd) 3249789Sahrens { 3250789Sahrens zfs_cmd_t zc = { 0 }; 3251789Sahrens time_t begin_time; 32522885Sahrens int ioctl_err, err, bytes, size, choplen; 3253789Sahrens char *cp; 3254789Sahrens dmu_replay_record_t drr; 3255789Sahrens struct drr_begin *drrb = &zc.zc_begin_record; 32562082Seschrock char errbuf[1024]; 32572885Sahrens char chopprefix[ZFS_MAXNAMELEN]; 3258789Sahrens 3259789Sahrens begin_time = time(NULL); 3260789Sahrens 32612082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 32622082Seschrock "cannot receive")); 32632082Seschrock 3264789Sahrens /* read in the BEGIN record */ 3265789Sahrens cp = (char *)&drr; 3266789Sahrens bytes = 0; 3267789Sahrens do { 32683504Sahl size = read(infd, cp, sizeof (drr) - bytes); 3269868Sahrens cp += size; 3270868Sahrens bytes += size; 3271868Sahrens } while (size > 0); 3272868Sahrens 3273868Sahrens if (size < 0 || bytes != sizeof (drr)) { 32742082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 32752082Seschrock "stream (failed to read first record)")); 32762082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3277789Sahrens } 3278789Sahrens 3279789Sahrens zc.zc_begin_record = drr.drr_u.drr_begin; 3280789Sahrens 3281789Sahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC && 3282789Sahrens drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { 32832082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 32842082Seschrock "stream (bad magic number)")); 32852082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3286789Sahrens } 3287789Sahrens 3288789Sahrens if (drrb->drr_version != DMU_BACKUP_VERSION && 3289789Sahrens drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { 32902082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " 32912082Seschrock "0x%llx is supported (stream is version 0x%llx)"), 3292789Sahrens DMU_BACKUP_VERSION, drrb->drr_version); 32932082Seschrock return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 3294789Sahrens } 3295789Sahrens 32962885Sahrens if (strchr(drr.drr_u.drr_begin.drr_toname, '@') == NULL) { 32972885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 32983912Slling "stream (bad snapshot name)")); 32992885Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); 33002885Sahrens } 3301789Sahrens /* 33022885Sahrens * Determine how much of the snapshot name stored in the stream 33032885Sahrens * we are going to tack on to the name they specified on the 33042885Sahrens * command line, and how much we are going to chop off. 33052885Sahrens * 33062885Sahrens * If they specified a snapshot, chop the entire name stored in 33072885Sahrens * the stream. 3308789Sahrens */ 33092885Sahrens (void) strcpy(chopprefix, drr.drr_u.drr_begin.drr_toname); 3310789Sahrens if (isprefix) { 33112885Sahrens /* 33122885Sahrens * They specified a fs with -d, we want to tack on 33132885Sahrens * everything but the pool name stored in the stream 33142885Sahrens */ 33152885Sahrens if (strchr(tosnap, '@')) { 33162885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " 33172885Sahrens "argument - snapshot not allowed with -d")); 33182885Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3319789Sahrens } 33202885Sahrens cp = strchr(chopprefix, '/'); 3321789Sahrens if (cp == NULL) 33222885Sahrens cp = strchr(chopprefix, '@'); 33232885Sahrens *cp = '\0'; 3324789Sahrens } else if (strchr(tosnap, '@') == NULL) { 3325789Sahrens /* 33262885Sahrens * If they specified a filesystem without -d, we want to 33272885Sahrens * tack on everything after the fs specified in the 33282885Sahrens * first name from the stream. 3329789Sahrens */ 33302885Sahrens cp = strchr(chopprefix, '@'); 33312885Sahrens *cp = '\0'; 3332789Sahrens } 33332885Sahrens choplen = strlen(chopprefix); 33342885Sahrens 33352885Sahrens /* 33362885Sahrens * Determine name of destination snapshot, store in zc_value. 33372885Sahrens */ 33382885Sahrens (void) strcpy(zc.zc_value, tosnap); 33392885Sahrens (void) strncat(zc.zc_value, drr.drr_u.drr_begin.drr_toname+choplen, 33402885Sahrens sizeof (zc.zc_value)); 3341*5326Sek110237 if (!zfs_validate_name(hdl, zc.zc_value, ZFS_TYPE_SNAPSHOT, B_TRUE)) 33423265Sahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 33432885Sahrens 33442885Sahrens (void) strcpy(zc.zc_name, zc.zc_value); 3345789Sahrens if (drrb->drr_fromguid) { 3346789Sahrens /* incremental backup stream */ 33472885Sahrens zfs_handle_t *h; 33482885Sahrens 33492885Sahrens /* do the recvbackup ioctl to the containing fs */ 33502885Sahrens *strchr(zc.zc_name, '@') = '\0'; 3351789Sahrens 3352789Sahrens /* make sure destination fs exists */ 33532082Seschrock h = zfs_open(hdl, zc.zc_name, 33542082Seschrock ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 33552082Seschrock if (h == NULL) 3356789Sahrens return (-1); 3357*5326Sek110237 if (!dryrun && h->zfs_type == ZFS_TYPE_VOLUME) { 3358*5326Sek110237 if (zvol_remove_link(hdl, h->zfs_name) != 0) { 3359*5326Sek110237 zfs_close(h); 3360*5326Sek110237 return (-1); 3361868Sahrens } 3362868Sahrens } 3363789Sahrens zfs_close(h); 3364789Sahrens } else { 3365789Sahrens /* full backup stream */ 3366789Sahrens 3367868Sahrens /* Make sure destination fs does not exist */ 33682885Sahrens *strchr(zc.zc_name, '@') = '\0'; 33695094Slling if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) { 33702082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33712082Seschrock "destination '%s' exists"), zc.zc_name); 33722082Seschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3373868Sahrens } 3374868Sahrens 33752885Sahrens if (strchr(zc.zc_name, '/') == NULL) { 33762885Sahrens /* 33772885Sahrens * they're trying to do a recv into a 33782885Sahrens * nonexistant topmost filesystem. 33792885Sahrens */ 33802885Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33812885Sahrens "destination does not exist"), zc.zc_name); 33822885Sahrens return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 33832885Sahrens } 33842885Sahrens 3385868Sahrens /* Do the recvbackup ioctl to the fs's parent. */ 33862885Sahrens *strrchr(zc.zc_name, '/') = '\0'; 33872885Sahrens 33882885Sahrens if (isprefix && (err = create_parents(hdl, 33892885Sahrens zc.zc_value, strlen(tosnap))) != 0) { 33902885Sahrens return (zfs_error(hdl, EZFS_BADRESTORE, errbuf)); 33912885Sahrens } 33922885Sahrens 3393789Sahrens } 3394789Sahrens 33953504Sahl zc.zc_cookie = infd; 33962676Seschrock zc.zc_guid = force; 3397789Sahrens if (verbose) { 33981749Sahrens (void) printf("%s %s stream of %s into %s\n", 33991749Sahrens dryrun ? "would receive" : "receiving", 3400789Sahrens drrb->drr_fromguid ? "incremental" : "full", 3401789Sahrens drr.drr_u.drr_begin.drr_toname, 34022676Seschrock zc.zc_value); 3403789Sahrens (void) fflush(stdout); 3404789Sahrens } 3405789Sahrens if (dryrun) 3406789Sahrens return (0); 34074543Smarks err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECVBACKUP, &zc); 3408868Sahrens if (ioctl_err != 0) { 3409789Sahrens switch (errno) { 3410789Sahrens case ENODEV: 34112082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34122082Seschrock "most recent snapshot does not match incremental " 34132082Seschrock "source")); 34142082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3415789Sahrens break; 3416789Sahrens case ETXTBSY: 34172082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34182082Seschrock "destination has been modified since most recent " 34192082Seschrock "snapshot")); 34202082Seschrock (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); 3421789Sahrens break; 3422789Sahrens case EEXIST: 3423789Sahrens if (drrb->drr_fromguid == 0) { 3424789Sahrens /* it's the containing fs that exists */ 34252676Seschrock cp = strchr(zc.zc_value, '@'); 3426789Sahrens *cp = '\0'; 3427789Sahrens } 34282082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34292082Seschrock "destination already exists")); 34303237Slling (void) zfs_error_fmt(hdl, EZFS_EXISTS, 34313237Slling dgettext(TEXT_DOMAIN, "cannot restore to %s"), 34323237Slling zc.zc_value); 3433789Sahrens break; 3434789Sahrens case EINVAL: 34352082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3436868Sahrens break; 34371544Seschrock case ECKSUM: 34382082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 34392082Seschrock "invalid stream (checksum mismatch)")); 34402082Seschrock (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); 3441789Sahrens break; 3442789Sahrens default: 34432082Seschrock (void) zfs_standard_error(hdl, errno, errbuf); 3444789Sahrens } 3445789Sahrens } 3446789Sahrens 3447789Sahrens /* 3448868Sahrens * Mount or recreate the /dev links for the target filesystem 3449868Sahrens * (if created, or if we tore them down to do an incremental 3450868Sahrens * restore), and the /dev links for the new snapshot (if 34512665Snd150628 * created). Also mount any children of the target filesystem 34522665Snd150628 * if we did an incremental receive. 3453789Sahrens */ 34542676Seschrock cp = strchr(zc.zc_value, '@'); 3455868Sahrens if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { 3456789Sahrens zfs_handle_t *h; 3457789Sahrens 3458789Sahrens *cp = '\0'; 34592676Seschrock h = zfs_open(hdl, zc.zc_value, 3460789Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3461868Sahrens *cp = '@'; 3462789Sahrens if (h) { 34632665Snd150628 if (h->zfs_type == ZFS_TYPE_VOLUME) { 34642082Seschrock err = zvol_create_link(hdl, h->zfs_name); 34651544Seschrock if (err == 0 && ioctl_err == 0) 34662082Seschrock err = zvol_create_link(hdl, 34672676Seschrock zc.zc_value); 3468*5326Sek110237 } else if (!drrb->drr_fromguid) { 3469*5326Sek110237 err = zfs_mount(h, NULL, 0); 3470868Sahrens } 34712665Snd150628 zfs_close(h); 3472789Sahrens } 3473789Sahrens } 3474789Sahrens 3475868Sahrens if (err || ioctl_err) 3476868Sahrens return (-1); 3477789Sahrens 3478789Sahrens if (verbose) { 3479789Sahrens char buf1[64]; 3480789Sahrens char buf2[64]; 3481789Sahrens uint64_t bytes = zc.zc_cookie; 3482789Sahrens time_t delta = time(NULL) - begin_time; 3483789Sahrens if (delta == 0) 3484789Sahrens delta = 1; 3485789Sahrens zfs_nicenum(bytes, buf1, sizeof (buf1)); 3486789Sahrens zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); 3487789Sahrens 34884603Sahrens (void) printf("received %sB stream in %lu seconds (%sB/sec)\n", 3489789Sahrens buf1, delta, buf2); 3490789Sahrens } 34912665Snd150628 3492789Sahrens return (0); 3493789Sahrens } 3494789Sahrens 3495789Sahrens /* 34961294Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 34971294Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 34981294Slling * is a dependent and we should just destroy it without checking the transaction 34991294Slling * group. 3500789Sahrens */ 35011294Slling typedef struct rollback_data { 35021294Slling const char *cb_target; /* the snapshot */ 35031294Slling uint64_t cb_create; /* creation time reference */ 35041294Slling prop_changelist_t *cb_clp; /* changelist pointer */ 35051294Slling int cb_error; 35062082Seschrock boolean_t cb_dependent; 35071294Slling } rollback_data_t; 35081294Slling 35091294Slling static int 35101294Slling rollback_destroy(zfs_handle_t *zhp, void *data) 35111294Slling { 35121294Slling rollback_data_t *cbp = data; 35131294Slling 35141294Slling if (!cbp->cb_dependent) { 35151294Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 35161294Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 35171294Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 35181294Slling cbp->cb_create) { 35194543Smarks char *logstr; 35201294Slling 35212082Seschrock cbp->cb_dependent = B_TRUE; 35222474Seschrock if (zfs_iter_dependents(zhp, B_FALSE, rollback_destroy, 35232474Seschrock cbp) != 0) 35242474Seschrock cbp->cb_error = 1; 35252082Seschrock cbp->cb_dependent = B_FALSE; 35261294Slling 35274543Smarks logstr = zhp->zfs_hdl->libzfs_log_str; 35284543Smarks zhp->zfs_hdl->libzfs_log_str = NULL; 35291294Slling if (zfs_destroy(zhp) != 0) 35301294Slling cbp->cb_error = 1; 35311294Slling else 35321294Slling changelist_remove(zhp, cbp->cb_clp); 35334543Smarks zhp->zfs_hdl->libzfs_log_str = logstr; 35341294Slling } 35351294Slling } else { 35361294Slling if (zfs_destroy(zhp) != 0) 35371294Slling cbp->cb_error = 1; 35381294Slling else 35391294Slling changelist_remove(zhp, cbp->cb_clp); 35401294Slling } 35411294Slling 35421294Slling zfs_close(zhp); 35431294Slling return (0); 35441294Slling } 35451294Slling 35461294Slling /* 35471294Slling * Rollback the dataset to its latest snapshot. 35481294Slling */ 35491294Slling static int 35501294Slling do_rollback(zfs_handle_t *zhp) 3551789Sahrens { 3552789Sahrens int ret; 3553789Sahrens zfs_cmd_t zc = { 0 }; 3554789Sahrens 3555789Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3556789Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3557789Sahrens 3558789Sahrens if (zhp->zfs_type == ZFS_TYPE_VOLUME && 35592082Seschrock zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) 3560789Sahrens return (-1); 3561789Sahrens 3562789Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3563789Sahrens 35642676Seschrock if (ZFS_IS_VOLUME(zhp)) 3565789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3566789Sahrens else 3567789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3568789Sahrens 3569789Sahrens /* 3570789Sahrens * We rely on the consumer to verify that there are no newer snapshots 3571789Sahrens * for the given dataset. Given these constraints, we can simply pass 3572789Sahrens * the name on to the ioctl() call. There is still an unlikely race 3573789Sahrens * condition where the user has taken a snapshot since we verified that 3574789Sahrens * this was the most recent. 3575789Sahrens */ 35764543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 35773237Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 35782082Seschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 35792082Seschrock zhp->zfs_name); 3580789Sahrens } else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 35812082Seschrock ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); 3582789Sahrens } 3583789Sahrens 3584789Sahrens return (ret); 3585789Sahrens } 3586789Sahrens 3587789Sahrens /* 35881294Slling * Given a dataset, rollback to a specific snapshot, discarding any 35891294Slling * data changes since then and making it the active dataset. 35901294Slling * 35911294Slling * Any snapshots more recent than the target are destroyed, along with 35921294Slling * their dependents. 35931294Slling */ 35941294Slling int 35951294Slling zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) 35961294Slling { 35971294Slling int ret; 35981294Slling rollback_data_t cb = { 0 }; 35991294Slling prop_changelist_t *clp; 36001294Slling 36011294Slling /* 36021294Slling * Unmount all dependendents of the dataset and the dataset itself. 36031294Slling * The list we need to gather is the same as for doing rename 36041294Slling */ 36051294Slling clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); 36061294Slling if (clp == NULL) 36071294Slling return (-1); 36081294Slling 36091294Slling if ((ret = changelist_prefix(clp)) != 0) 36101294Slling goto out; 36111294Slling 36121294Slling /* 36131294Slling * Destroy all recent snapshots and its dependends. 36141294Slling */ 36151294Slling cb.cb_target = snap->zfs_name; 36161294Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 36171294Slling cb.cb_clp = clp; 36181294Slling (void) zfs_iter_children(zhp, rollback_destroy, &cb); 36191294Slling 36201294Slling if ((ret = cb.cb_error) != 0) { 36211294Slling (void) changelist_postfix(clp); 36221294Slling goto out; 36231294Slling } 36241294Slling 36251294Slling /* 36261294Slling * Now that we have verified that the snapshot is the latest, 36271294Slling * rollback to the given snapshot. 36281294Slling */ 36291294Slling ret = do_rollback(zhp); 36301294Slling 36311294Slling if (ret != 0) { 36321294Slling (void) changelist_postfix(clp); 36331294Slling goto out; 36341294Slling } 36351294Slling 36361294Slling /* 36371294Slling * We only want to re-mount the filesystem if it was mounted in the 36381294Slling * first place. 36391294Slling */ 36401294Slling ret = changelist_postfix(clp); 36411294Slling 36421294Slling out: 36431294Slling changelist_free(clp); 36441294Slling return (ret); 36451294Slling } 36461294Slling 36471294Slling /* 3648789Sahrens * Iterate over all dependents for a given dataset. This includes both 3649789Sahrens * hierarchical dependents (children) and data dependents (snapshots and 3650789Sahrens * clones). The bulk of the processing occurs in get_dependents() in 3651789Sahrens * libzfs_graph.c. 3652789Sahrens */ 3653789Sahrens int 36542474Seschrock zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, 36552474Seschrock zfs_iter_f func, void *data) 3656789Sahrens { 3657789Sahrens char **dependents; 3658789Sahrens size_t count; 3659789Sahrens int i; 3660789Sahrens zfs_handle_t *child; 3661789Sahrens int ret = 0; 3662789Sahrens 36632474Seschrock if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name, 36642474Seschrock &dependents, &count) != 0) 36652474Seschrock return (-1); 36662474Seschrock 3667789Sahrens for (i = 0; i < count; i++) { 36682082Seschrock if ((child = make_dataset_handle(zhp->zfs_hdl, 36692082Seschrock dependents[i])) == NULL) 3670789Sahrens continue; 3671789Sahrens 3672789Sahrens if ((ret = func(child, data)) != 0) 3673789Sahrens break; 3674789Sahrens } 3675789Sahrens 3676789Sahrens for (i = 0; i < count; i++) 3677789Sahrens free(dependents[i]); 3678789Sahrens free(dependents); 3679789Sahrens 3680789Sahrens return (ret); 3681789Sahrens } 3682789Sahrens 3683789Sahrens /* 3684789Sahrens * Renames the given dataset. 3685789Sahrens */ 3686789Sahrens int 36874490Svb160487 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive) 3688789Sahrens { 3689789Sahrens int ret; 3690789Sahrens zfs_cmd_t zc = { 0 }; 3691789Sahrens char *delim; 36924007Smmusante prop_changelist_t *cl = NULL; 36934007Smmusante zfs_handle_t *zhrp = NULL; 36944007Smmusante char *parentname = NULL; 3695789Sahrens char parent[ZFS_MAXNAMELEN]; 36962082Seschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 36972082Seschrock char errbuf[1024]; 3698789Sahrens 3699789Sahrens /* if we have the same exact name, just return success */ 3700789Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3701789Sahrens return (0); 3702789Sahrens 37032082Seschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37042082Seschrock "cannot rename to '%s'"), target); 37052082Seschrock 3706789Sahrens /* 3707789Sahrens * Make sure the target name is valid 3708789Sahrens */ 3709789Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 37102665Snd150628 if ((strchr(target, '@') == NULL) || 37112665Snd150628 *target == '@') { 37122665Snd150628 /* 37132665Snd150628 * Snapshot target name is abbreviated, 37142665Snd150628 * reconstruct full dataset name 37152665Snd150628 */ 37162665Snd150628 (void) strlcpy(parent, zhp->zfs_name, 37172665Snd150628 sizeof (parent)); 37182665Snd150628 delim = strchr(parent, '@'); 37192665Snd150628 if (strchr(target, '@') == NULL) 37202665Snd150628 *(++delim) = '\0'; 37212665Snd150628 else 37222665Snd150628 *delim = '\0'; 37232665Snd150628 (void) strlcat(parent, target, sizeof (parent)); 37242665Snd150628 target = parent; 37252665Snd150628 } else { 37262665Snd150628 /* 37272665Snd150628 * Make sure we're renaming within the same dataset. 37282665Snd150628 */ 37292665Snd150628 delim = strchr(target, '@'); 37302665Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 37312665Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 37322665Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37332665Snd150628 "snapshots must be part of same " 37342665Snd150628 "dataset")); 37352665Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 37363912Slling errbuf)); 37372665Snd150628 } 3738789Sahrens } 3739*5326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 37402665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3741789Sahrens } else { 37424007Smmusante if (recursive) { 37434007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37444007Smmusante "recursive rename must be a snapshot")); 37454007Smmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 37464007Smmusante } 37474007Smmusante 3748*5326Sek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 37492665Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37502676Seschrock uint64_t unused; 37512676Seschrock 3752789Sahrens /* validate parents */ 37534490Svb160487 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0) 3754789Sahrens return (-1); 3755789Sahrens 3756789Sahrens (void) parent_name(target, parent, sizeof (parent)); 3757789Sahrens 3758789Sahrens /* make sure we're in the same pool */ 3759789Sahrens verify((delim = strchr(target, '/')) != NULL); 3760789Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3761789Sahrens zhp->zfs_name[delim - target] != '/') { 37622082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37632082Seschrock "datasets must be within same pool")); 37642082Seschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3765789Sahrens } 37662440Snd150628 37672440Snd150628 /* new name cannot be a child of the current dataset name */ 37682440Snd150628 if (strncmp(parent, zhp->zfs_name, 37693912Slling strlen(zhp->zfs_name)) == 0) { 37702440Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37712440Snd150628 "New dataset name cannot be a descendent of " 37722440Snd150628 "current dataset name")); 37732440Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37742440Snd150628 } 3775789Sahrens } 3776789Sahrens 37772082Seschrock (void) snprintf(errbuf, sizeof (errbuf), 37782082Seschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 37792082Seschrock 3780789Sahrens if (getzoneid() == GLOBAL_ZONEID && 3781789Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 37822082Seschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 37832082Seschrock "dataset is used in a non-global zone")); 37842082Seschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3785789Sahrens } 3786789Sahrens 37874007Smmusante if (recursive) { 37884007Smmusante struct destroydata dd; 37894007Smmusante 37904183Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 37914183Smmusante if (parentname == NULL) { 37924183Smmusante ret = -1; 37934183Smmusante goto error; 37944183Smmusante } 37954007Smmusante delim = strchr(parentname, '@'); 37964007Smmusante *delim = '\0'; 37975094Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 37984007Smmusante if (zhrp == NULL) { 37994183Smmusante ret = -1; 38004183Smmusante goto error; 38014007Smmusante } 38024007Smmusante 38034007Smmusante dd.snapname = delim + 1; 38044007Smmusante dd.gotone = B_FALSE; 38054183Smmusante dd.closezhp = B_TRUE; 38064007Smmusante 38074007Smmusante /* We remove any zvol links prior to renaming them */ 38084007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd); 38094007Smmusante if (ret) { 38104007Smmusante goto error; 38114007Smmusante } 38124007Smmusante } else { 38134007Smmusante if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) 38144007Smmusante return (-1); 38154007Smmusante 38164007Smmusante if (changelist_haszonedchild(cl)) { 38174007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38184007Smmusante "child dataset with inherited mountpoint is used " 38194007Smmusante "in a non-global zone")); 38204007Smmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 38214007Smmusante goto error; 38224007Smmusante } 38234007Smmusante 38244007Smmusante if ((ret = changelist_prefix(cl)) != 0) 38254007Smmusante goto error; 3826789Sahrens } 3827789Sahrens 38282676Seschrock if (ZFS_IS_VOLUME(zhp)) 3829789Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3830789Sahrens else 3831789Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3832789Sahrens 38332665Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 38342676Seschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 38352665Snd150628 38364007Smmusante zc.zc_cookie = recursive; 38374007Smmusante 38384543Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 38394007Smmusante /* 38404007Smmusante * if it was recursive, the one that actually failed will 38414007Smmusante * be in zc.zc_name 38424007Smmusante */ 38434007Smmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 38444007Smmusante "cannot rename to '%s'"), zc.zc_name); 38454007Smmusante 38464007Smmusante if (recursive && errno == EEXIST) { 38474007Smmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 38484007Smmusante "a child dataset already has a snapshot " 38494007Smmusante "with the new name")); 38504801Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 38514007Smmusante } else { 38524007Smmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 38534007Smmusante } 3854789Sahrens 3855789Sahrens /* 3856789Sahrens * On failure, we still want to remount any filesystems that 3857789Sahrens * were previously mounted, so we don't alter the system state. 3858789Sahrens */ 38594007Smmusante if (recursive) { 38604007Smmusante struct createdata cd; 38614007Smmusante 38624007Smmusante /* only create links for datasets that had existed */ 38634007Smmusante cd.cd_snapname = delim + 1; 38644007Smmusante cd.cd_ifexists = B_TRUE; 38654007Smmusante (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb, 38664007Smmusante &cd); 38674007Smmusante } else { 38684007Smmusante (void) changelist_postfix(cl); 38694007Smmusante } 3870789Sahrens } else { 38714007Smmusante if (recursive) { 38724007Smmusante struct createdata cd; 38734007Smmusante 38744007Smmusante /* only create links for datasets that had existed */ 38754007Smmusante cd.cd_snapname = strchr(target, '@') + 1; 38764007Smmusante cd.cd_ifexists = B_TRUE; 38774007Smmusante ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb, 38784007Smmusante &cd); 38794007Smmusante } else { 38804007Smmusante changelist_rename(cl, zfs_get_name(zhp), target); 38814007Smmusante ret = changelist_postfix(cl); 38824007Smmusante } 3883789Sahrens } 3884789Sahrens 3885789Sahrens error: 38864007Smmusante if (parentname) { 38874007Smmusante free(parentname); 38884007Smmusante } 38894007Smmusante if (zhrp) { 38904007Smmusante zfs_close(zhrp); 38914007Smmusante } 38924007Smmusante if (cl) { 38934007Smmusante changelist_free(cl); 38944007Smmusante } 3895789Sahrens return (ret); 3896789Sahrens } 3897789Sahrens 3898789Sahrens /* 3899789Sahrens * Given a zvol dataset, issue the ioctl to create the appropriate minor node, 3900789Sahrens * poke devfsadm to create the /dev link, and then wait for the link to appear. 3901789Sahrens */ 3902789Sahrens int 39032082Seschrock zvol_create_link(libzfs_handle_t *hdl, const char *dataset) 3904789Sahrens { 39054007Smmusante return (zvol_create_link_common(hdl, dataset, B_FALSE)); 39064007Smmusante } 39074007Smmusante 39084007Smmusante static int 39094007Smmusante zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists) 39104007Smmusante { 3911789Sahrens zfs_cmd_t zc = { 0 }; 39122082Seschrock di_devlink_handle_t dhdl; 39134543Smarks priv_set_t *priv_effective; 39144543Smarks int privileged; 3915789Sahrens 3916789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3917789Sahrens 3918789Sahrens /* 3919789Sahrens * Issue the appropriate ioctl. 3920789Sahrens */ 39212082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { 3922789Sahrens switch (errno) { 3923789Sahrens case EEXIST: 3924789Sahrens /* 3925789Sahrens * Silently ignore the case where the link already 3926789Sahrens * exists. This allows 'zfs volinit' to be run multiple 3927789Sahrens * times without errors. 3928789Sahrens */ 3929789Sahrens return (0); 3930789Sahrens 39314007Smmusante case ENOENT: 39324007Smmusante /* 39334007Smmusante * Dataset does not exist in the kernel. If we 39344007Smmusante * don't care (see zfs_rename), then ignore the 39354007Smmusante * error quietly. 39364007Smmusante */ 39374007Smmusante if (ifexists) { 39384007Smmusante return (0); 39394007Smmusante } 39404007Smmusante 39414007Smmusante /* FALLTHROUGH */ 39424007Smmusante 3943789Sahrens default: 39443237Slling return (zfs_standard_error_fmt(hdl, errno, 39452082Seschrock dgettext(TEXT_DOMAIN, "cannot create device links " 39462082Seschrock "for '%s'"), dataset)); 3947789Sahrens } 3948789Sahrens } 3949789Sahrens 3950789Sahrens /* 39514543Smarks * If privileged call devfsadm and wait for the links to 39524543Smarks * magically appear. 39534543Smarks * Otherwise, print out an informational message. 3954789Sahrens */ 39554543Smarks 39564543Smarks priv_effective = priv_allocset(); 39574543Smarks (void) getppriv(PRIV_EFFECTIVE, priv_effective); 39584543Smarks privileged = (priv_isfullset(priv_effective) == B_TRUE); 39594543Smarks priv_freeset(priv_effective); 39604543Smarks 39614543Smarks if (privileged) { 39624543Smarks if ((dhdl = di_devlink_init(ZFS_DRIVER, 39634543Smarks DI_MAKE_LINK)) == NULL) { 39644543Smarks zfs_error_aux(hdl, strerror(errno)); 39654543Smarks (void) zfs_standard_error_fmt(hdl, EZFS_DEVLINKS, 39664543Smarks dgettext(TEXT_DOMAIN, "cannot create device links " 39674543Smarks "for '%s'"), dataset); 39684543Smarks (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); 39694543Smarks return (-1); 39704543Smarks } else { 39714543Smarks (void) di_devlink_fini(&dhdl); 39724543Smarks } 3973789Sahrens } else { 39744543Smarks char pathname[MAXPATHLEN]; 39754543Smarks struct stat64 statbuf; 39764543Smarks int i; 39774543Smarks 39784543Smarks #define MAX_WAIT 10 39794543Smarks 39804543Smarks /* 39814543Smarks * This is the poor mans way of waiting for the link 39824543Smarks * to show up. If after 10 seconds we still don't 39834543Smarks * have it, then print out a message. 39844543Smarks */ 39854543Smarks (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s", 39864543Smarks dataset); 39874543Smarks 39884543Smarks for (i = 0; i != MAX_WAIT; i++) { 39894543Smarks if (stat64(pathname, &statbuf) == 0) 39904543Smarks break; 39914543Smarks (void) sleep(1); 39924543Smarks } 39934543Smarks if (i == MAX_WAIT) 39944543Smarks (void) printf(gettext("%s may not be immediately " 39954543Smarks "available\n"), pathname); 3996789Sahrens } 3997789Sahrens 3998789Sahrens return (0); 3999789Sahrens } 4000789Sahrens 4001789Sahrens /* 4002789Sahrens * Remove a minor node for the given zvol and the associated /dev links. 4003789Sahrens */ 4004789Sahrens int 40052082Seschrock zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) 4006789Sahrens { 4007789Sahrens zfs_cmd_t zc = { 0 }; 4008789Sahrens 4009789Sahrens (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4010789Sahrens 40112082Seschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { 4012789Sahrens switch (errno) { 4013789Sahrens case ENXIO: 4014789Sahrens /* 4015789Sahrens * Silently ignore the case where the link no longer 4016789Sahrens * exists, so that 'zfs volfini' can be run multiple 4017789Sahrens * times without errors. 4018789Sahrens */ 4019789Sahrens return (0); 4020789Sahrens 4021789Sahrens default: 40223237Slling return (zfs_standard_error_fmt(hdl, errno, 40232082Seschrock dgettext(TEXT_DOMAIN, "cannot remove device " 40242082Seschrock "links for '%s'"), dataset)); 4025789Sahrens } 4026789Sahrens } 4027789Sahrens 4028789Sahrens return (0); 4029789Sahrens } 40302676Seschrock 40312676Seschrock nvlist_t * 40322676Seschrock zfs_get_user_props(zfs_handle_t *zhp) 40332676Seschrock { 40342676Seschrock return (zhp->zfs_user_props); 40352676Seschrock } 40362676Seschrock 40372676Seschrock /* 40383912Slling * This function is used by 'zfs list' to determine the exact set of columns to 40393912Slling * display, and their maximum widths. This does two main things: 40403912Slling * 40413912Slling * - If this is a list of all properties, then expand the list to include 40423912Slling * all native properties, and set a flag so that for each dataset we look 40433912Slling * for new unique user properties and add them to the list. 40443912Slling * 40453912Slling * - For non fixed-width properties, keep track of the maximum width seen 40463912Slling * so that we can size the column appropriately. 40473912Slling */ 40483912Slling int 40495094Slling zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp) 40503912Slling { 40513912Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 40525094Slling zprop_list_t *entry; 40535094Slling zprop_list_t **last, **start; 40543912Slling nvlist_t *userprops, *propval; 40553912Slling nvpair_t *elem; 40563912Slling char *strval; 40573912Slling char buf[ZFS_MAXPROPLEN]; 40583912Slling 40595094Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 40603912Slling return (-1); 40612676Seschrock 40622676Seschrock userprops = zfs_get_user_props(zhp); 40632676Seschrock 40642676Seschrock entry = *plp; 40652676Seschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 40662676Seschrock /* 40672676Seschrock * Go through and add any user properties as necessary. We 40682676Seschrock * start by incrementing our list pointer to the first 40692676Seschrock * non-native property. 40702676Seschrock */ 40712676Seschrock start = plp; 40722676Seschrock while (*start != NULL) { 40735094Slling if ((*start)->pl_prop == ZPROP_INVAL) 40742676Seschrock break; 40752676Seschrock start = &(*start)->pl_next; 40762676Seschrock } 40772676Seschrock 40782676Seschrock elem = NULL; 40792676Seschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 40802676Seschrock /* 40812676Seschrock * See if we've already found this property in our list. 40822676Seschrock */ 40832676Seschrock for (last = start; *last != NULL; 40842676Seschrock last = &(*last)->pl_next) { 40852676Seschrock if (strcmp((*last)->pl_user_prop, 40862676Seschrock nvpair_name(elem)) == 0) 40872676Seschrock break; 40882676Seschrock } 40892676Seschrock 40902676Seschrock if (*last == NULL) { 40912676Seschrock if ((entry = zfs_alloc(hdl, 40925094Slling sizeof (zprop_list_t))) == NULL || 40932676Seschrock ((entry->pl_user_prop = zfs_strdup(hdl, 40942676Seschrock nvpair_name(elem)))) == NULL) { 40952676Seschrock free(entry); 40962676Seschrock return (-1); 40972676Seschrock } 40982676Seschrock 40995094Slling entry->pl_prop = ZPROP_INVAL; 41002676Seschrock entry->pl_width = strlen(nvpair_name(elem)); 41012676Seschrock entry->pl_all = B_TRUE; 41022676Seschrock *last = entry; 41032676Seschrock } 41042676Seschrock } 41052676Seschrock } 41062676Seschrock 41072676Seschrock /* 41082676Seschrock * Now go through and check the width of any non-fixed columns 41092676Seschrock */ 41102676Seschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 41112676Seschrock if (entry->pl_fixed) 41122676Seschrock continue; 41132676Seschrock 41145094Slling if (entry->pl_prop != ZPROP_INVAL) { 41152676Seschrock if (zfs_prop_get(zhp, entry->pl_prop, 41162676Seschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 41172676Seschrock if (strlen(buf) > entry->pl_width) 41182676Seschrock entry->pl_width = strlen(buf); 41192676Seschrock } 41202676Seschrock } else if (nvlist_lookup_nvlist(userprops, 41212676Seschrock entry->pl_user_prop, &propval) == 0) { 41222676Seschrock verify(nvlist_lookup_string(propval, 41235094Slling ZPROP_VALUE, &strval) == 0); 41242676Seschrock if (strlen(strval) > entry->pl_width) 41252676Seschrock entry->pl_width = strlen(strval); 41262676Seschrock } 41272676Seschrock } 41282676Seschrock 41292676Seschrock return (0); 41302676Seschrock } 41314543Smarks 41324543Smarks int 41334543Smarks zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred) 41344543Smarks { 41354543Smarks zfs_cmd_t zc = { 0 }; 41364543Smarks nvlist_t *nvp; 41374543Smarks gid_t gid; 41384543Smarks uid_t uid; 41394543Smarks const gid_t *groups; 41404543Smarks int group_cnt; 41414543Smarks int error; 41424543Smarks 41434543Smarks if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0) 41444543Smarks return (no_memory(hdl)); 41454543Smarks 41464543Smarks uid = ucred_geteuid(cred); 41474543Smarks gid = ucred_getegid(cred); 41484543Smarks group_cnt = ucred_getgroups(cred, &groups); 41494543Smarks 41504543Smarks if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1) 41514543Smarks return (1); 41524543Smarks 41534543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) { 41544543Smarks nvlist_free(nvp); 41554543Smarks return (1); 41564543Smarks } 41574543Smarks 41584543Smarks if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) { 41594543Smarks nvlist_free(nvp); 41604543Smarks return (1); 41614543Smarks } 41624543Smarks 41634543Smarks if (nvlist_add_uint32_array(nvp, 41644543Smarks ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) { 41654543Smarks nvlist_free(nvp); 41664543Smarks return (1); 41674543Smarks } 41684543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 41694543Smarks 41705094Slling if (zcmd_write_src_nvlist(hdl, &zc, nvp)) 41714543Smarks return (-1); 41724543Smarks 41734543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc); 41744543Smarks nvlist_free(nvp); 41754543Smarks return (error); 41764543Smarks } 41774543Smarks 41784543Smarks int 41794543Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 41804543Smarks void *export, void *sharetab, int sharemax, boolean_t share_on) 41814543Smarks { 41824543Smarks zfs_cmd_t zc = { 0 }; 41834543Smarks int error; 41844543Smarks 41854543Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 41864543Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 41874543Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 41884543Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 41894543Smarks zc.zc_share.z_sharetype = share_on; 41904543Smarks zc.zc_share.z_sharemax = sharemax; 41914543Smarks 41924543Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 41934543Smarks return (error); 41944543Smarks } 4195