1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51485Slling * Common Development and Distribution License (the "License"). 61485Slling * 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 */ 21789Sahrens /* 228525SEric.Schrock@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/types.h> 27789Sahrens #include <sys/param.h> 28789Sahrens #include <sys/errno.h> 29789Sahrens #include <sys/uio.h> 30789Sahrens #include <sys/buf.h> 31789Sahrens #include <sys/modctl.h> 32789Sahrens #include <sys/open.h> 33789Sahrens #include <sys/file.h> 34789Sahrens #include <sys/kmem.h> 35789Sahrens #include <sys/conf.h> 36789Sahrens #include <sys/cmn_err.h> 37789Sahrens #include <sys/stat.h> 38789Sahrens #include <sys/zfs_ioctl.h> 3910972SRic.Aleshire@Sun.COM #include <sys/zfs_vfsops.h> 405331Samw #include <sys/zfs_znode.h> 41789Sahrens #include <sys/zap.h> 42789Sahrens #include <sys/spa.h> 433912Slling #include <sys/spa_impl.h> 44789Sahrens #include <sys/vdev.h> 4510972SRic.Aleshire@Sun.COM #include <sys/priv_impl.h> 46789Sahrens #include <sys/dmu.h> 47789Sahrens #include <sys/dsl_dir.h> 48789Sahrens #include <sys/dsl_dataset.h> 49789Sahrens #include <sys/dsl_prop.h> 504543Smarks #include <sys/dsl_deleg.h> 514543Smarks #include <sys/dmu_objset.h> 52789Sahrens #include <sys/ddi.h> 53789Sahrens #include <sys/sunddi.h> 54789Sahrens #include <sys/sunldi.h> 55789Sahrens #include <sys/policy.h> 56789Sahrens #include <sys/zone.h> 57789Sahrens #include <sys/nvpair.h> 58789Sahrens #include <sys/pathname.h> 59789Sahrens #include <sys/mount.h> 60789Sahrens #include <sys/sdt.h> 61789Sahrens #include <sys/fs/zfs.h> 62789Sahrens #include <sys/zfs_ctldir.h> 635331Samw #include <sys/zfs_dir.h> 642885Sahrens #include <sys/zvol.h> 654543Smarks #include <sharefs/share.h> 665326Sek110237 #include <sys/dmu_objset.h> 67789Sahrens 68789Sahrens #include "zfs_namecheck.h" 692676Seschrock #include "zfs_prop.h" 704543Smarks #include "zfs_deleg.h" 71789Sahrens 72789Sahrens extern struct modlfs zfs_modlfs; 73789Sahrens 74789Sahrens extern void zfs_init(void); 75789Sahrens extern void zfs_fini(void); 76789Sahrens 77789Sahrens ldi_ident_t zfs_li = NULL; 78789Sahrens dev_info_t *zfs_dip; 79789Sahrens 80789Sahrens typedef int zfs_ioc_func_t(zfs_cmd_t *); 814543Smarks typedef int zfs_secpolicy_func_t(zfs_cmd_t *, cred_t *); 82789Sahrens 839234SGeorge.Wilson@Sun.COM typedef enum { 849234SGeorge.Wilson@Sun.COM NO_NAME, 859234SGeorge.Wilson@Sun.COM POOL_NAME, 869234SGeorge.Wilson@Sun.COM DATASET_NAME 879234SGeorge.Wilson@Sun.COM } zfs_ioc_namecheck_t; 889234SGeorge.Wilson@Sun.COM 89789Sahrens typedef struct zfs_ioc_vec { 90789Sahrens zfs_ioc_func_t *zvec_func; 91789Sahrens zfs_secpolicy_func_t *zvec_secpolicy; 929234SGeorge.Wilson@Sun.COM zfs_ioc_namecheck_t zvec_namecheck; 934543Smarks boolean_t zvec_his_log; 949234SGeorge.Wilson@Sun.COM boolean_t zvec_pool_check; 95789Sahrens } zfs_ioc_vec_t; 96789Sahrens 979396SMatthew.Ahrens@Sun.COM /* This array is indexed by zfs_userquota_prop_t */ 989396SMatthew.Ahrens@Sun.COM static const char *userquota_perms[] = { 999396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERUSED, 1009396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERQUOTA, 1019396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_GROUPUSED, 1029396SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_GROUPQUOTA, 1039396SMatthew.Ahrens@Sun.COM }; 1049396SMatthew.Ahrens@Sun.COM 1059396SMatthew.Ahrens@Sun.COM static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); 10611022STom.Erickson@Sun.COM static int zfs_check_settable(const char *name, nvpair_t *property, 10711022STom.Erickson@Sun.COM cred_t *cr); 10811022STom.Erickson@Sun.COM static int zfs_check_clearable(char *dataset, nvlist_t *props, 10911022STom.Erickson@Sun.COM nvlist_t **errors); 1107184Stimh static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, 1117184Stimh boolean_t *); 11211022STom.Erickson@Sun.COM int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t **); 1137184Stimh 114789Sahrens /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */ 115789Sahrens void 116789Sahrens __dprintf(const char *file, const char *func, int line, const char *fmt, ...) 117789Sahrens { 118789Sahrens const char *newfile; 119789Sahrens char buf[256]; 120789Sahrens va_list adx; 121789Sahrens 122789Sahrens /* 123789Sahrens * Get rid of annoying "../common/" prefix to filename. 124789Sahrens */ 125789Sahrens newfile = strrchr(file, '/'); 126789Sahrens if (newfile != NULL) { 127789Sahrens newfile = newfile + 1; /* Get rid of leading / */ 128789Sahrens } else { 129789Sahrens newfile = file; 130789Sahrens } 131789Sahrens 132789Sahrens va_start(adx, fmt); 133789Sahrens (void) vsnprintf(buf, sizeof (buf), fmt, adx); 134789Sahrens va_end(adx); 135789Sahrens 136789Sahrens /* 137789Sahrens * To get this data, use the zfs-dprintf probe as so: 138789Sahrens * dtrace -q -n 'zfs-dprintf \ 139789Sahrens * /stringof(arg0) == "dbuf.c"/ \ 140789Sahrens * {printf("%s: %s", stringof(arg1), stringof(arg3))}' 141789Sahrens * arg0 = file name 142789Sahrens * arg1 = function name 143789Sahrens * arg2 = line number 144789Sahrens * arg3 = message 145789Sahrens */ 146789Sahrens DTRACE_PROBE4(zfs__dprintf, 147789Sahrens char *, newfile, char *, func, int, line, char *, buf); 148789Sahrens } 149789Sahrens 1504543Smarks static void 1514715Sek110237 history_str_free(char *buf) 1524715Sek110237 { 1534715Sek110237 kmem_free(buf, HIS_MAX_RECORD_LEN); 1544715Sek110237 } 1554715Sek110237 1564715Sek110237 static char * 1574715Sek110237 history_str_get(zfs_cmd_t *zc) 1584715Sek110237 { 1594715Sek110237 char *buf; 1604715Sek110237 1614715Sek110237 if (zc->zc_history == NULL) 1624715Sek110237 return (NULL); 1634715Sek110237 1644715Sek110237 buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); 1654715Sek110237 if (copyinstr((void *)(uintptr_t)zc->zc_history, 1664715Sek110237 buf, HIS_MAX_RECORD_LEN, NULL) != 0) { 1674715Sek110237 history_str_free(buf); 1684715Sek110237 return (NULL); 1694715Sek110237 } 1704715Sek110237 1714715Sek110237 buf[HIS_MAX_RECORD_LEN -1] = '\0'; 1724715Sek110237 1734715Sek110237 return (buf); 1744715Sek110237 } 1754715Sek110237 1765375Stimh /* 1777042Sgw25295 * Check to see if the named dataset is currently defined as bootable 1787042Sgw25295 */ 1797042Sgw25295 static boolean_t 1807042Sgw25295 zfs_is_bootfs(const char *name) 1817042Sgw25295 { 18210298SMatthew.Ahrens@Sun.COM objset_t *os; 18310298SMatthew.Ahrens@Sun.COM 18410298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(name, FTAG, &os) == 0) { 18510298SMatthew.Ahrens@Sun.COM boolean_t ret; 18610922SJeff.Bonwick@Sun.COM ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os))); 18710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 18810298SMatthew.Ahrens@Sun.COM return (ret); 1897042Sgw25295 } 19010298SMatthew.Ahrens@Sun.COM return (B_FALSE); 1917042Sgw25295 } 1927042Sgw25295 1937042Sgw25295 /* 1947184Stimh * zfs_earlier_version 1955375Stimh * 1965375Stimh * Return non-zero if the spa version is less than requested version. 1975375Stimh */ 1985331Samw static int 1997184Stimh zfs_earlier_version(const char *name, int version) 2005331Samw { 2015331Samw spa_t *spa; 2025331Samw 2035331Samw if (spa_open(name, &spa, FTAG) == 0) { 2045331Samw if (spa_version(spa) < version) { 2055331Samw spa_close(spa, FTAG); 2065331Samw return (1); 2075331Samw } 2085331Samw spa_close(spa, FTAG); 2095331Samw } 2105331Samw return (0); 2115331Samw } 2125331Samw 2135977Smarks /* 2146689Smaybee * zpl_earlier_version 2155977Smarks * 2166689Smaybee * Return TRUE if the ZPL version is less than requested version. 2175977Smarks */ 2186689Smaybee static boolean_t 2196689Smaybee zpl_earlier_version(const char *name, int version) 2205977Smarks { 2215977Smarks objset_t *os; 2226689Smaybee boolean_t rc = B_TRUE; 2235977Smarks 22410298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(name, FTAG, &os) == 0) { 2256689Smaybee uint64_t zplversion; 2266689Smaybee 22710298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 22810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 22910298SMatthew.Ahrens@Sun.COM return (B_TRUE); 23010298SMatthew.Ahrens@Sun.COM } 23110298SMatthew.Ahrens@Sun.COM /* XXX reading from non-owned objset */ 2326689Smaybee if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) 2336689Smaybee rc = zplversion < version; 23410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 2355977Smarks } 2365977Smarks return (rc); 2375977Smarks } 2385977Smarks 2394715Sek110237 static void 2404543Smarks zfs_log_history(zfs_cmd_t *zc) 2414543Smarks { 2424543Smarks spa_t *spa; 2434603Sahrens char *buf; 2444543Smarks 2454715Sek110237 if ((buf = history_str_get(zc)) == NULL) 2464577Sahrens return; 2474577Sahrens 2484715Sek110237 if (spa_open(zc->zc_name, &spa, FTAG) == 0) { 2494715Sek110237 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) 2504715Sek110237 (void) spa_history_log(spa, buf, LOG_CMD_NORMAL); 2514715Sek110237 spa_close(spa, FTAG); 2524543Smarks } 2534715Sek110237 history_str_free(buf); 2544543Smarks } 2554543Smarks 256789Sahrens /* 257789Sahrens * Policy for top-level read operations (list pools). Requires no privileges, 258789Sahrens * and can be used in the local zone, as there is no associated dataset. 259789Sahrens */ 260789Sahrens /* ARGSUSED */ 261789Sahrens static int 2624543Smarks zfs_secpolicy_none(zfs_cmd_t *zc, cred_t *cr) 263789Sahrens { 264789Sahrens return (0); 265789Sahrens } 266789Sahrens 267789Sahrens /* 268789Sahrens * Policy for dataset read operations (list children, get statistics). Requires 269789Sahrens * no privileges, but must be visible in the local zone. 270789Sahrens */ 271789Sahrens /* ARGSUSED */ 272789Sahrens static int 2734543Smarks zfs_secpolicy_read(zfs_cmd_t *zc, cred_t *cr) 274789Sahrens { 275789Sahrens if (INGLOBALZONE(curproc) || 2764543Smarks zone_dataset_visible(zc->zc_name, NULL)) 277789Sahrens return (0); 278789Sahrens 279789Sahrens return (ENOENT); 280789Sahrens } 281789Sahrens 282789Sahrens static int 283789Sahrens zfs_dozonecheck(const char *dataset, cred_t *cr) 284789Sahrens { 285789Sahrens uint64_t zoned; 286789Sahrens int writable = 1; 287789Sahrens 288789Sahrens /* 289789Sahrens * The dataset must be visible by this zone -- check this first 290789Sahrens * so they don't see EPERM on something they shouldn't know about. 291789Sahrens */ 292789Sahrens if (!INGLOBALZONE(curproc) && 293789Sahrens !zone_dataset_visible(dataset, &writable)) 294789Sahrens return (ENOENT); 295789Sahrens 296789Sahrens if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) 297789Sahrens return (ENOENT); 298789Sahrens 299789Sahrens if (INGLOBALZONE(curproc)) { 300789Sahrens /* 301789Sahrens * If the fs is zoned, only root can access it from the 302789Sahrens * global zone. 303789Sahrens */ 304789Sahrens if (secpolicy_zfs(cr) && zoned) 305789Sahrens return (EPERM); 306789Sahrens } else { 307789Sahrens /* 308789Sahrens * If we are in a local zone, the 'zoned' property must be set. 309789Sahrens */ 310789Sahrens if (!zoned) 311789Sahrens return (EPERM); 312789Sahrens 313789Sahrens /* must be writable by this zone */ 314789Sahrens if (!writable) 315789Sahrens return (EPERM); 316789Sahrens } 317789Sahrens return (0); 318789Sahrens } 319789Sahrens 320789Sahrens int 3214543Smarks zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) 322789Sahrens { 323789Sahrens int error; 324789Sahrens 3254543Smarks error = zfs_dozonecheck(name, cr); 3264543Smarks if (error == 0) { 3274543Smarks error = secpolicy_zfs(cr); 3284670Sahrens if (error) 3294543Smarks error = dsl_deleg_access(name, perm, cr); 3304543Smarks } 3314543Smarks return (error); 3324543Smarks } 3334543Smarks 33410972SRic.Aleshire@Sun.COM /* 33510972SRic.Aleshire@Sun.COM * Policy for setting the security label property. 33610972SRic.Aleshire@Sun.COM * 33710972SRic.Aleshire@Sun.COM * Returns 0 for success, non-zero for access and other errors. 33810972SRic.Aleshire@Sun.COM */ 33910972SRic.Aleshire@Sun.COM static int 34011022STom.Erickson@Sun.COM zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr) 34110972SRic.Aleshire@Sun.COM { 34210972SRic.Aleshire@Sun.COM char ds_hexsl[MAXNAMELEN]; 34310972SRic.Aleshire@Sun.COM bslabel_t ds_sl, new_sl; 34410972SRic.Aleshire@Sun.COM boolean_t new_default = FALSE; 34510972SRic.Aleshire@Sun.COM uint64_t zoned; 34610972SRic.Aleshire@Sun.COM int needed_priv = -1; 34710972SRic.Aleshire@Sun.COM int error; 34810972SRic.Aleshire@Sun.COM 34910972SRic.Aleshire@Sun.COM /* First get the existing dataset label. */ 35010972SRic.Aleshire@Sun.COM error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 35110972SRic.Aleshire@Sun.COM 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 35210972SRic.Aleshire@Sun.COM if (error) 35310972SRic.Aleshire@Sun.COM return (EPERM); 35410972SRic.Aleshire@Sun.COM 35510972SRic.Aleshire@Sun.COM if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 35610972SRic.Aleshire@Sun.COM new_default = TRUE; 35710972SRic.Aleshire@Sun.COM 35810972SRic.Aleshire@Sun.COM /* The label must be translatable */ 35910972SRic.Aleshire@Sun.COM if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) 36010972SRic.Aleshire@Sun.COM return (EINVAL); 36110972SRic.Aleshire@Sun.COM 36210972SRic.Aleshire@Sun.COM /* 36310972SRic.Aleshire@Sun.COM * In a non-global zone, disallow attempts to set a label that 36410972SRic.Aleshire@Sun.COM * doesn't match that of the zone; otherwise no other checks 36510972SRic.Aleshire@Sun.COM * are needed. 36610972SRic.Aleshire@Sun.COM */ 36710972SRic.Aleshire@Sun.COM if (!INGLOBALZONE(curproc)) { 36810972SRic.Aleshire@Sun.COM if (new_default || !blequal(&new_sl, CR_SL(CRED()))) 36910972SRic.Aleshire@Sun.COM return (EPERM); 37010972SRic.Aleshire@Sun.COM return (0); 37110972SRic.Aleshire@Sun.COM } 37210972SRic.Aleshire@Sun.COM 37310972SRic.Aleshire@Sun.COM /* 37410972SRic.Aleshire@Sun.COM * For global-zone datasets (i.e., those whose zoned property is 37510972SRic.Aleshire@Sun.COM * "off", verify that the specified new label is valid for the 37610972SRic.Aleshire@Sun.COM * global zone. 37710972SRic.Aleshire@Sun.COM */ 37810972SRic.Aleshire@Sun.COM if (dsl_prop_get_integer(name, 37910972SRic.Aleshire@Sun.COM zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 38010972SRic.Aleshire@Sun.COM return (EPERM); 38110972SRic.Aleshire@Sun.COM if (!zoned) { 38210972SRic.Aleshire@Sun.COM if (zfs_check_global_label(name, strval) != 0) 38310972SRic.Aleshire@Sun.COM return (EPERM); 38410972SRic.Aleshire@Sun.COM } 38510972SRic.Aleshire@Sun.COM 38610972SRic.Aleshire@Sun.COM /* 38710972SRic.Aleshire@Sun.COM * If the existing dataset label is nondefault, check if the 38810972SRic.Aleshire@Sun.COM * dataset is mounted (label cannot be changed while mounted). 38910972SRic.Aleshire@Sun.COM * Get the zfsvfs; if there isn't one, then the dataset isn't 39010972SRic.Aleshire@Sun.COM * mounted (or isn't a dataset, doesn't exist, ...). 39110972SRic.Aleshire@Sun.COM */ 39210972SRic.Aleshire@Sun.COM if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { 39311022STom.Erickson@Sun.COM objset_t *os; 39411022STom.Erickson@Sun.COM static char *setsl_tag = "setsl_tag"; 39511022STom.Erickson@Sun.COM 39610972SRic.Aleshire@Sun.COM /* 39710972SRic.Aleshire@Sun.COM * Try to own the dataset; abort if there is any error, 39810972SRic.Aleshire@Sun.COM * (e.g., already mounted, in use, or other error). 39910972SRic.Aleshire@Sun.COM */ 40010972SRic.Aleshire@Sun.COM error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, 40111022STom.Erickson@Sun.COM setsl_tag, &os); 40210972SRic.Aleshire@Sun.COM if (error) 40310972SRic.Aleshire@Sun.COM return (EPERM); 40410972SRic.Aleshire@Sun.COM 40511022STom.Erickson@Sun.COM dmu_objset_disown(os, setsl_tag); 40611022STom.Erickson@Sun.COM 40710972SRic.Aleshire@Sun.COM if (new_default) { 40810972SRic.Aleshire@Sun.COM needed_priv = PRIV_FILE_DOWNGRADE_SL; 40910972SRic.Aleshire@Sun.COM goto out_check; 41010972SRic.Aleshire@Sun.COM } 41110972SRic.Aleshire@Sun.COM 41210972SRic.Aleshire@Sun.COM if (hexstr_to_label(strval, &new_sl) != 0) 41310972SRic.Aleshire@Sun.COM return (EPERM); 41410972SRic.Aleshire@Sun.COM 41510972SRic.Aleshire@Sun.COM if (blstrictdom(&ds_sl, &new_sl)) 41610972SRic.Aleshire@Sun.COM needed_priv = PRIV_FILE_DOWNGRADE_SL; 41710972SRic.Aleshire@Sun.COM else if (blstrictdom(&new_sl, &ds_sl)) 41810972SRic.Aleshire@Sun.COM needed_priv = PRIV_FILE_UPGRADE_SL; 41910972SRic.Aleshire@Sun.COM } else { 42010972SRic.Aleshire@Sun.COM /* dataset currently has a default label */ 42110972SRic.Aleshire@Sun.COM if (!new_default) 42210972SRic.Aleshire@Sun.COM needed_priv = PRIV_FILE_UPGRADE_SL; 42310972SRic.Aleshire@Sun.COM } 42410972SRic.Aleshire@Sun.COM 42510972SRic.Aleshire@Sun.COM out_check: 42610972SRic.Aleshire@Sun.COM if (needed_priv != -1) 42710972SRic.Aleshire@Sun.COM return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); 42810972SRic.Aleshire@Sun.COM return (0); 42910972SRic.Aleshire@Sun.COM } 43010972SRic.Aleshire@Sun.COM 4314543Smarks static int 43211022STom.Erickson@Sun.COM zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, 43311022STom.Erickson@Sun.COM cred_t *cr) 4344543Smarks { 43511022STom.Erickson@Sun.COM char *strval; 43611022STom.Erickson@Sun.COM 4374543Smarks /* 4384543Smarks * Check permissions for special properties. 4394543Smarks */ 4404543Smarks switch (prop) { 4414543Smarks case ZFS_PROP_ZONED: 4424543Smarks /* 4434543Smarks * Disallow setting of 'zoned' from within a local zone. 4444543Smarks */ 4454543Smarks if (!INGLOBALZONE(curproc)) 4464543Smarks return (EPERM); 4474543Smarks break; 448789Sahrens 4494543Smarks case ZFS_PROP_QUOTA: 4504543Smarks if (!INGLOBALZONE(curproc)) { 4514543Smarks uint64_t zoned; 4524543Smarks char setpoint[MAXNAMELEN]; 4534543Smarks /* 4544543Smarks * Unprivileged users are allowed to modify the 4554543Smarks * quota on things *under* (ie. contained by) 4564543Smarks * the thing they own. 4574543Smarks */ 45811022STom.Erickson@Sun.COM if (dsl_prop_get_integer(dsname, "zoned", &zoned, 4594543Smarks setpoint)) 4604543Smarks return (EPERM); 46111022STom.Erickson@Sun.COM if (!zoned || strlen(dsname) <= strlen(setpoint)) 4624543Smarks return (EPERM); 4634543Smarks } 4644670Sahrens break; 46510972SRic.Aleshire@Sun.COM 46610972SRic.Aleshire@Sun.COM case ZFS_PROP_MLSLABEL: 46710972SRic.Aleshire@Sun.COM if (!is_system_labeled()) 46810972SRic.Aleshire@Sun.COM return (EPERM); 46911022STom.Erickson@Sun.COM 47011022STom.Erickson@Sun.COM if (nvpair_value_string(propval, &strval) == 0) { 47111022STom.Erickson@Sun.COM int err; 47211022STom.Erickson@Sun.COM 47311022STom.Erickson@Sun.COM err = zfs_set_slabel_policy(dsname, strval, CRED()); 47411022STom.Erickson@Sun.COM if (err != 0) 47511022STom.Erickson@Sun.COM return (err); 47611022STom.Erickson@Sun.COM } 47710972SRic.Aleshire@Sun.COM break; 4784543Smarks } 4794543Smarks 48011022STom.Erickson@Sun.COM return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); 481789Sahrens } 482789Sahrens 4834543Smarks int 4844543Smarks zfs_secpolicy_fsacl(zfs_cmd_t *zc, cred_t *cr) 4854543Smarks { 4864543Smarks int error; 4874543Smarks 4884543Smarks error = zfs_dozonecheck(zc->zc_name, cr); 4894543Smarks if (error) 4904543Smarks return (error); 4914543Smarks 4924543Smarks /* 4934543Smarks * permission to set permissions will be evaluated later in 4944543Smarks * dsl_deleg_can_allow() 4954543Smarks */ 4964543Smarks return (0); 4974543Smarks } 4984543Smarks 4994543Smarks int 5004543Smarks zfs_secpolicy_rollback(zfs_cmd_t *zc, cred_t *cr) 5014543Smarks { 50210588SEric.Taylor@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 50310588SEric.Taylor@Sun.COM ZFS_DELEG_PERM_ROLLBACK, cr)); 5044543Smarks } 5054543Smarks 5064543Smarks int 5074543Smarks zfs_secpolicy_send(zfs_cmd_t *zc, cred_t *cr) 5084543Smarks { 5094543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 5104543Smarks ZFS_DELEG_PERM_SEND, cr)); 5114543Smarks } 5124543Smarks 5138845Samw@Sun.COM static int 5148845Samw@Sun.COM zfs_secpolicy_deleg_share(zfs_cmd_t *zc, cred_t *cr) 5158845Samw@Sun.COM { 5168845Samw@Sun.COM vnode_t *vp; 5178845Samw@Sun.COM int error; 5188845Samw@Sun.COM 5198845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 5208845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 5218845Samw@Sun.COM return (error); 5228845Samw@Sun.COM 5238845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 5248845Samw@Sun.COM 5258845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 5268845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 5278845Samw@Sun.COM zc->zc_name) != 0)) { 5288845Samw@Sun.COM VN_RELE(vp); 5298845Samw@Sun.COM return (EPERM); 5308845Samw@Sun.COM } 5318845Samw@Sun.COM 5328845Samw@Sun.COM VN_RELE(vp); 5338845Samw@Sun.COM return (dsl_deleg_access(zc->zc_name, 5348845Samw@Sun.COM ZFS_DELEG_PERM_SHARE, cr)); 5358845Samw@Sun.COM } 5368845Samw@Sun.COM 5374543Smarks int 5384543Smarks zfs_secpolicy_share(zfs_cmd_t *zc, cred_t *cr) 5394543Smarks { 5404543Smarks if (!INGLOBALZONE(curproc)) 5414543Smarks return (EPERM); 5424543Smarks 5435367Sahrens if (secpolicy_nfs(cr) == 0) { 5444543Smarks return (0); 5454543Smarks } else { 5468845Samw@Sun.COM return (zfs_secpolicy_deleg_share(zc, cr)); 5478845Samw@Sun.COM } 5488845Samw@Sun.COM } 5498845Samw@Sun.COM 5508845Samw@Sun.COM int 5518845Samw@Sun.COM zfs_secpolicy_smb_acl(zfs_cmd_t *zc, cred_t *cr) 5528845Samw@Sun.COM { 5538845Samw@Sun.COM if (!INGLOBALZONE(curproc)) 5548845Samw@Sun.COM return (EPERM); 5558845Samw@Sun.COM 5568845Samw@Sun.COM if (secpolicy_smb(cr) == 0) { 5578845Samw@Sun.COM return (0); 5588845Samw@Sun.COM } else { 5598845Samw@Sun.COM return (zfs_secpolicy_deleg_share(zc, cr)); 5604543Smarks } 5614543Smarks } 5624543Smarks 563789Sahrens static int 5644543Smarks zfs_get_parent(const char *datasetname, char *parent, int parentsize) 565789Sahrens { 566789Sahrens char *cp; 567789Sahrens 568789Sahrens /* 569789Sahrens * Remove the @bla or /bla from the end of the name to get the parent. 570789Sahrens */ 5714543Smarks (void) strncpy(parent, datasetname, parentsize); 5724543Smarks cp = strrchr(parent, '@'); 573789Sahrens if (cp != NULL) { 574789Sahrens cp[0] = '\0'; 575789Sahrens } else { 5764543Smarks cp = strrchr(parent, '/'); 577789Sahrens if (cp == NULL) 578789Sahrens return (ENOENT); 579789Sahrens cp[0] = '\0'; 580789Sahrens } 581789Sahrens 5824543Smarks return (0); 5834543Smarks } 5844543Smarks 5854543Smarks int 5864543Smarks zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) 5874543Smarks { 5884543Smarks int error; 5894543Smarks 5904543Smarks if ((error = zfs_secpolicy_write_perms(name, 5914543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 5924543Smarks return (error); 5934543Smarks 5944543Smarks return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); 5954543Smarks } 5964543Smarks 5974543Smarks static int 5984543Smarks zfs_secpolicy_destroy(zfs_cmd_t *zc, cred_t *cr) 5994543Smarks { 6004543Smarks return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); 6014543Smarks } 6024543Smarks 6034543Smarks /* 6044543Smarks * Must have sys_config privilege to check the iscsi permission 6054543Smarks */ 6064543Smarks /* ARGSUSED */ 6074543Smarks static int 6084543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr) 6094543Smarks { 6104543Smarks return (secpolicy_zfs(cr)); 6114543Smarks } 6124543Smarks 6134543Smarks int 6144543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 6154543Smarks { 61611022STom.Erickson@Sun.COM char parentname[MAXNAMELEN]; 6174543Smarks int error; 6184543Smarks 6194543Smarks if ((error = zfs_secpolicy_write_perms(from, 6204543Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0) 6214543Smarks return (error); 6224543Smarks 6234543Smarks if ((error = zfs_secpolicy_write_perms(from, 6244543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 6254543Smarks return (error); 6264543Smarks 6274543Smarks if ((error = zfs_get_parent(to, parentname, 6284543Smarks sizeof (parentname))) != 0) 6294543Smarks return (error); 6304543Smarks 6314543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 6324543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 6334543Smarks return (error); 6344543Smarks 6354543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 6364543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 6374543Smarks return (error); 6384543Smarks 6394543Smarks return (error); 6404543Smarks } 6414543Smarks 6424543Smarks static int 6434543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 6444543Smarks { 6454543Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 6464543Smarks } 6474543Smarks 6484543Smarks static int 6494543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 6504543Smarks { 65111022STom.Erickson@Sun.COM char parentname[MAXNAMELEN]; 6524543Smarks objset_t *clone; 6534543Smarks int error; 6544543Smarks 6554543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 6564543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 6574543Smarks if (error) 6584543Smarks return (error); 6594543Smarks 66010298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &clone); 6614543Smarks 6624543Smarks if (error == 0) { 6634543Smarks dsl_dataset_t *pclone = NULL; 6644543Smarks dsl_dir_t *dd; 66510298SMatthew.Ahrens@Sun.COM dd = clone->os_dsl_dataset->ds_dir; 6664543Smarks 6674543Smarks rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 6686689Smaybee error = dsl_dataset_hold_obj(dd->dd_pool, 6696689Smaybee dd->dd_phys->dd_origin_obj, FTAG, &pclone); 6704543Smarks rw_exit(&dd->dd_pool->dp_config_rwlock); 6714543Smarks if (error) { 67210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 6734543Smarks return (error); 6744543Smarks } 6754543Smarks 6764543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 6774543Smarks ZFS_DELEG_PERM_MOUNT, cr); 6784543Smarks 6794543Smarks dsl_dataset_name(pclone, parentname); 68010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 6816689Smaybee dsl_dataset_rele(pclone, FTAG); 6824543Smarks if (error == 0) 6834543Smarks error = zfs_secpolicy_write_perms(parentname, 6844543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 6854543Smarks } 6864543Smarks return (error); 6874543Smarks } 6884543Smarks 6894543Smarks static int 6904543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 6914543Smarks { 6924543Smarks int error; 6934543Smarks 6944543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 6954543Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 6964543Smarks return (error); 6974543Smarks 6984543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 6994543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 7004543Smarks return (error); 7014543Smarks 7024543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 7034543Smarks ZFS_DELEG_PERM_CREATE, cr)); 7044543Smarks } 7054543Smarks 7064543Smarks int 7074543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 7084543Smarks { 70910588SEric.Taylor@Sun.COM return (zfs_secpolicy_write_perms(name, 71010588SEric.Taylor@Sun.COM ZFS_DELEG_PERM_SNAPSHOT, cr)); 7114543Smarks } 7124543Smarks 7134543Smarks static int 7144543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 7154543Smarks { 7164543Smarks 7174543Smarks return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 7184543Smarks } 7194543Smarks 7204543Smarks static int 7214543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 7224543Smarks { 72311022STom.Erickson@Sun.COM char parentname[MAXNAMELEN]; 72411022STom.Erickson@Sun.COM int error; 7254543Smarks 7264543Smarks if ((error = zfs_get_parent(zc->zc_name, parentname, 7274543Smarks sizeof (parentname))) != 0) 7284543Smarks return (error); 7294543Smarks 7304543Smarks if (zc->zc_value[0] != '\0') { 7314543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_value, 7324543Smarks ZFS_DELEG_PERM_CLONE, cr)) != 0) 7334543Smarks return (error); 7344543Smarks } 7354543Smarks 7364543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 7374543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 7384543Smarks return (error); 7394543Smarks 7404543Smarks error = zfs_secpolicy_write_perms(parentname, 7414543Smarks ZFS_DELEG_PERM_MOUNT, cr); 7424543Smarks 7434543Smarks return (error); 7444543Smarks } 7454543Smarks 7464543Smarks static int 7474543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 7484543Smarks { 7494543Smarks int error; 7504543Smarks 7514543Smarks error = secpolicy_fs_unmount(cr, NULL); 7524543Smarks if (error) { 7534543Smarks error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 7544543Smarks } 7554543Smarks return (error); 756789Sahrens } 757789Sahrens 758789Sahrens /* 759789Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 760789Sahrens * SYS_CONFIG privilege, which is not available in a local zone. 761789Sahrens */ 762789Sahrens /* ARGSUSED */ 763789Sahrens static int 7644543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 765789Sahrens { 766789Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0) 767789Sahrens return (EPERM); 768789Sahrens 769789Sahrens return (0); 770789Sahrens } 771789Sahrens 772789Sahrens /* 7731544Seschrock * Policy for fault injection. Requires all privileges. 7741544Seschrock */ 7751544Seschrock /* ARGSUSED */ 7761544Seschrock static int 7774543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 7781544Seschrock { 7791544Seschrock return (secpolicy_zinject(cr)); 7801544Seschrock } 7811544Seschrock 7824849Sahrens static int 7834849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 7844849Sahrens { 7854849Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 7864849Sahrens 7875094Slling if (prop == ZPROP_INVAL) { 7884849Sahrens if (!zfs_prop_user(zc->zc_value)) 7894849Sahrens return (EINVAL); 7904849Sahrens return (zfs_secpolicy_write_perms(zc->zc_name, 7914849Sahrens ZFS_DELEG_PERM_USERPROP, cr)); 7924849Sahrens } else { 79311022STom.Erickson@Sun.COM return (zfs_secpolicy_setprop(zc->zc_name, prop, 79411022STom.Erickson@Sun.COM NULL, cr)); 7954849Sahrens } 7964849Sahrens } 7974849Sahrens 7989396SMatthew.Ahrens@Sun.COM static int 7999396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr) 8009396SMatthew.Ahrens@Sun.COM { 8019396SMatthew.Ahrens@Sun.COM int err = zfs_secpolicy_read(zc, cr); 8029396SMatthew.Ahrens@Sun.COM if (err) 8039396SMatthew.Ahrens@Sun.COM return (err); 8049396SMatthew.Ahrens@Sun.COM 8059396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 8069396SMatthew.Ahrens@Sun.COM return (EINVAL); 8079396SMatthew.Ahrens@Sun.COM 8089396SMatthew.Ahrens@Sun.COM if (zc->zc_value[0] == 0) { 8099396SMatthew.Ahrens@Sun.COM /* 8109396SMatthew.Ahrens@Sun.COM * They are asking about a posix uid/gid. If it's 8119396SMatthew.Ahrens@Sun.COM * themself, allow it. 8129396SMatthew.Ahrens@Sun.COM */ 8139396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type == ZFS_PROP_USERUSED || 8149396SMatthew.Ahrens@Sun.COM zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 8159396SMatthew.Ahrens@Sun.COM if (zc->zc_guid == crgetuid(cr)) 8169396SMatthew.Ahrens@Sun.COM return (0); 8179396SMatthew.Ahrens@Sun.COM } else { 8189396SMatthew.Ahrens@Sun.COM if (groupmember(zc->zc_guid, cr)) 8199396SMatthew.Ahrens@Sun.COM return (0); 8209396SMatthew.Ahrens@Sun.COM } 8219396SMatthew.Ahrens@Sun.COM } 8229396SMatthew.Ahrens@Sun.COM 8239396SMatthew.Ahrens@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 8249396SMatthew.Ahrens@Sun.COM userquota_perms[zc->zc_objset_type], cr)); 8259396SMatthew.Ahrens@Sun.COM } 8269396SMatthew.Ahrens@Sun.COM 8279396SMatthew.Ahrens@Sun.COM static int 8289396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr) 8299396SMatthew.Ahrens@Sun.COM { 8309396SMatthew.Ahrens@Sun.COM int err = zfs_secpolicy_read(zc, cr); 8319396SMatthew.Ahrens@Sun.COM if (err) 8329396SMatthew.Ahrens@Sun.COM return (err); 8339396SMatthew.Ahrens@Sun.COM 8349396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 8359396SMatthew.Ahrens@Sun.COM return (EINVAL); 8369396SMatthew.Ahrens@Sun.COM 8379396SMatthew.Ahrens@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 8389396SMatthew.Ahrens@Sun.COM userquota_perms[zc->zc_objset_type], cr)); 8399396SMatthew.Ahrens@Sun.COM } 8409396SMatthew.Ahrens@Sun.COM 8419396SMatthew.Ahrens@Sun.COM static int 8429396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr) 8439396SMatthew.Ahrens@Sun.COM { 84411022STom.Erickson@Sun.COM return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 84511022STom.Erickson@Sun.COM NULL, cr)); 8469396SMatthew.Ahrens@Sun.COM } 8479396SMatthew.Ahrens@Sun.COM 84810242Schris.kirby@sun.com static int 84910242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr) 85010242Schris.kirby@sun.com { 85110242Schris.kirby@sun.com return (zfs_secpolicy_write_perms(zc->zc_name, 85210242Schris.kirby@sun.com ZFS_DELEG_PERM_HOLD, cr)); 85310242Schris.kirby@sun.com } 85410242Schris.kirby@sun.com 85510242Schris.kirby@sun.com static int 85610242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr) 85710242Schris.kirby@sun.com { 85810242Schris.kirby@sun.com return (zfs_secpolicy_write_perms(zc->zc_name, 85910242Schris.kirby@sun.com ZFS_DELEG_PERM_RELEASE, cr)); 86010242Schris.kirby@sun.com } 86110242Schris.kirby@sun.com 8621544Seschrock /* 863789Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t. 864789Sahrens */ 865789Sahrens static int 8669643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 867789Sahrens { 868789Sahrens char *packed; 869789Sahrens int error; 8705094Slling nvlist_t *list = NULL; 871789Sahrens 872789Sahrens /* 8732676Seschrock * Read in and unpack the user-supplied nvlist. 874789Sahrens */ 8755094Slling if (size == 0) 876789Sahrens return (EINVAL); 877789Sahrens 878789Sahrens packed = kmem_alloc(size, KM_SLEEP); 879789Sahrens 8809643SEric.Taylor@Sun.COM if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 8819643SEric.Taylor@Sun.COM iflag)) != 0) { 882789Sahrens kmem_free(packed, size); 883789Sahrens return (error); 884789Sahrens } 885789Sahrens 8865094Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 887789Sahrens kmem_free(packed, size); 888789Sahrens return (error); 889789Sahrens } 890789Sahrens 891789Sahrens kmem_free(packed, size); 892789Sahrens 8935094Slling *nvp = list; 894789Sahrens return (0); 895789Sahrens } 896789Sahrens 897789Sahrens static int 89811022STom.Erickson@Sun.COM fit_error_list(zfs_cmd_t *zc, nvlist_t **errors) 89911022STom.Erickson@Sun.COM { 90011022STom.Erickson@Sun.COM size_t size; 90111022STom.Erickson@Sun.COM 90211022STom.Erickson@Sun.COM VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 90311022STom.Erickson@Sun.COM 90411022STom.Erickson@Sun.COM if (size > zc->zc_nvlist_dst_size) { 90511022STom.Erickson@Sun.COM nvpair_t *more_errors; 90611022STom.Erickson@Sun.COM int n = 0; 90711022STom.Erickson@Sun.COM 90811022STom.Erickson@Sun.COM if (zc->zc_nvlist_dst_size < 1024) 90911022STom.Erickson@Sun.COM return (ENOMEM); 91011022STom.Erickson@Sun.COM 91111022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0); 91211022STom.Erickson@Sun.COM more_errors = nvlist_prev_nvpair(*errors, NULL); 91311022STom.Erickson@Sun.COM 91411022STom.Erickson@Sun.COM do { 91511022STom.Erickson@Sun.COM nvpair_t *pair = nvlist_prev_nvpair(*errors, 91611022STom.Erickson@Sun.COM more_errors); 91711022STom.Erickson@Sun.COM VERIFY(nvlist_remove_nvpair(*errors, pair) == 0); 91811022STom.Erickson@Sun.COM n++; 91911022STom.Erickson@Sun.COM VERIFY(nvlist_size(*errors, &size, 92011022STom.Erickson@Sun.COM NV_ENCODE_NATIVE) == 0); 92111022STom.Erickson@Sun.COM } while (size > zc->zc_nvlist_dst_size); 92211022STom.Erickson@Sun.COM 92311022STom.Erickson@Sun.COM VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0); 92411022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0); 92511022STom.Erickson@Sun.COM ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 92611022STom.Erickson@Sun.COM ASSERT(size <= zc->zc_nvlist_dst_size); 92711022STom.Erickson@Sun.COM } 92811022STom.Erickson@Sun.COM 92911022STom.Erickson@Sun.COM return (0); 93011022STom.Erickson@Sun.COM } 93111022STom.Erickson@Sun.COM 93211022STom.Erickson@Sun.COM static int 9332676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 9342676Seschrock { 9352676Seschrock char *packed = NULL; 9362676Seschrock size_t size; 9372676Seschrock int error; 9382676Seschrock 9392676Seschrock VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 9402676Seschrock 9412676Seschrock if (size > zc->zc_nvlist_dst_size) { 9422676Seschrock error = ENOMEM; 9432676Seschrock } else { 9444611Smarks packed = kmem_alloc(size, KM_SLEEP); 9452676Seschrock VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 9462676Seschrock KM_SLEEP) == 0); 9479643SEric.Taylor@Sun.COM error = ddi_copyout(packed, 9489643SEric.Taylor@Sun.COM (void *)(uintptr_t)zc->zc_nvlist_dst, size, zc->zc_iflags); 9492676Seschrock kmem_free(packed, size); 9502676Seschrock } 9512676Seschrock 9522676Seschrock zc->zc_nvlist_dst_size = size; 9532676Seschrock return (error); 9542676Seschrock } 9552676Seschrock 9562676Seschrock static int 9579396SMatthew.Ahrens@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zvp) 9589396SMatthew.Ahrens@Sun.COM { 9599396SMatthew.Ahrens@Sun.COM objset_t *os; 9609396SMatthew.Ahrens@Sun.COM int error; 9619396SMatthew.Ahrens@Sun.COM 96210298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(dsname, FTAG, &os); 9639396SMatthew.Ahrens@Sun.COM if (error) 9649396SMatthew.Ahrens@Sun.COM return (error); 96510298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 96610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 96710298SMatthew.Ahrens@Sun.COM return (EINVAL); 96810298SMatthew.Ahrens@Sun.COM } 96910298SMatthew.Ahrens@Sun.COM 97010298SMatthew.Ahrens@Sun.COM mutex_enter(&os->os_user_ptr_lock); 9719396SMatthew.Ahrens@Sun.COM *zvp = dmu_objset_get_user(os); 9729396SMatthew.Ahrens@Sun.COM if (*zvp) { 9739396SMatthew.Ahrens@Sun.COM VFS_HOLD((*zvp)->z_vfs); 9749396SMatthew.Ahrens@Sun.COM } else { 9759396SMatthew.Ahrens@Sun.COM error = ESRCH; 9769396SMatthew.Ahrens@Sun.COM } 97710298SMatthew.Ahrens@Sun.COM mutex_exit(&os->os_user_ptr_lock); 97810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 9799396SMatthew.Ahrens@Sun.COM return (error); 9809396SMatthew.Ahrens@Sun.COM } 9819396SMatthew.Ahrens@Sun.COM 9829396SMatthew.Ahrens@Sun.COM /* 9839396SMatthew.Ahrens@Sun.COM * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 9849396SMatthew.Ahrens@Sun.COM * case its z_vfs will be NULL, and it will be opened as the owner. 9859396SMatthew.Ahrens@Sun.COM */ 9869396SMatthew.Ahrens@Sun.COM static int 98710298SMatthew.Ahrens@Sun.COM zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zvp) 9889396SMatthew.Ahrens@Sun.COM { 9899396SMatthew.Ahrens@Sun.COM int error = 0; 9909396SMatthew.Ahrens@Sun.COM 9919396SMatthew.Ahrens@Sun.COM if (getzfsvfs(name, zvp) != 0) 99210298SMatthew.Ahrens@Sun.COM error = zfsvfs_create(name, zvp); 9939396SMatthew.Ahrens@Sun.COM if (error == 0) { 9949396SMatthew.Ahrens@Sun.COM rrw_enter(&(*zvp)->z_teardown_lock, RW_READER, tag); 9959396SMatthew.Ahrens@Sun.COM if ((*zvp)->z_unmounted) { 9969396SMatthew.Ahrens@Sun.COM /* 9979396SMatthew.Ahrens@Sun.COM * XXX we could probably try again, since the unmounting 9989396SMatthew.Ahrens@Sun.COM * thread should be just about to disassociate the 9999396SMatthew.Ahrens@Sun.COM * objset from the zfsvfs. 10009396SMatthew.Ahrens@Sun.COM */ 10019396SMatthew.Ahrens@Sun.COM rrw_exit(&(*zvp)->z_teardown_lock, tag); 10029396SMatthew.Ahrens@Sun.COM return (EBUSY); 10039396SMatthew.Ahrens@Sun.COM } 10049396SMatthew.Ahrens@Sun.COM } 10059396SMatthew.Ahrens@Sun.COM return (error); 10069396SMatthew.Ahrens@Sun.COM } 10079396SMatthew.Ahrens@Sun.COM 10089396SMatthew.Ahrens@Sun.COM static void 10099396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 10109396SMatthew.Ahrens@Sun.COM { 10119396SMatthew.Ahrens@Sun.COM rrw_exit(&zfsvfs->z_teardown_lock, tag); 10129396SMatthew.Ahrens@Sun.COM 10139396SMatthew.Ahrens@Sun.COM if (zfsvfs->z_vfs) { 10149396SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 10159396SMatthew.Ahrens@Sun.COM } else { 101610298SMatthew.Ahrens@Sun.COM dmu_objset_disown(zfsvfs->z_os, zfsvfs); 10179396SMatthew.Ahrens@Sun.COM zfsvfs_free(zfsvfs); 10189396SMatthew.Ahrens@Sun.COM } 10199396SMatthew.Ahrens@Sun.COM } 10209396SMatthew.Ahrens@Sun.COM 10219396SMatthew.Ahrens@Sun.COM static int 1022789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc) 1023789Sahrens { 1024789Sahrens int error; 10255094Slling nvlist_t *config, *props = NULL; 10267184Stimh nvlist_t *rootprops = NULL; 10277184Stimh nvlist_t *zplprops = NULL; 10284715Sek110237 char *buf; 1029789Sahrens 10305094Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 10319643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) 10324988Sek110237 return (error); 10334715Sek110237 10345094Slling if (zc->zc_nvlist_src_size != 0 && (error = 10359643SEric.Taylor@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 10369643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) { 10375094Slling nvlist_free(config); 10385094Slling return (error); 10395094Slling } 10405094Slling 10417184Stimh if (props) { 10427184Stimh nvlist_t *nvl = NULL; 10437184Stimh uint64_t version = SPA_VERSION; 10447184Stimh 10457184Stimh (void) nvlist_lookup_uint64(props, 10467184Stimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 10477184Stimh if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 10487184Stimh error = EINVAL; 10497184Stimh goto pool_props_bad; 10507184Stimh } 10517184Stimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 10527184Stimh if (nvl) { 10537184Stimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 10547184Stimh if (error != 0) { 10557184Stimh nvlist_free(config); 10567184Stimh nvlist_free(props); 10577184Stimh return (error); 10587184Stimh } 10597184Stimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 10607184Stimh } 10617184Stimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 10627184Stimh error = zfs_fill_zplprops_root(version, rootprops, 10637184Stimh zplprops, NULL); 10647184Stimh if (error) 10657184Stimh goto pool_props_bad; 10667184Stimh } 10677184Stimh 10684988Sek110237 buf = history_str_get(zc); 1069789Sahrens 10707184Stimh error = spa_create(zc->zc_name, config, props, buf, zplprops); 10717184Stimh 10727184Stimh /* 10737184Stimh * Set the remaining root properties 10747184Stimh */ 107511022STom.Erickson@Sun.COM if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, 107611022STom.Erickson@Sun.COM ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) 10777184Stimh (void) spa_destroy(zc->zc_name); 1078789Sahrens 10794988Sek110237 if (buf != NULL) 10804988Sek110237 history_str_free(buf); 10815094Slling 10827184Stimh pool_props_bad: 10837184Stimh nvlist_free(rootprops); 10847184Stimh nvlist_free(zplprops); 1085789Sahrens nvlist_free(config); 10867184Stimh nvlist_free(props); 10875094Slling 1088789Sahrens return (error); 1089789Sahrens } 1090789Sahrens 1091789Sahrens static int 1092789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1093789Sahrens { 10944543Smarks int error; 10954543Smarks zfs_log_history(zc); 10964543Smarks error = spa_destroy(zc->zc_name); 109710588SEric.Taylor@Sun.COM if (error == 0) 109810588SEric.Taylor@Sun.COM zvol_remove_minors(zc->zc_name); 10994543Smarks return (error); 1100789Sahrens } 1101789Sahrens 1102789Sahrens static int 1103789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc) 1104789Sahrens { 11055094Slling nvlist_t *config, *props = NULL; 1106789Sahrens uint64_t guid; 110710921STim.Haley@Sun.COM int error; 1108789Sahrens 11095094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 11109643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) != 0) 1111789Sahrens return (error); 1112789Sahrens 11135094Slling if (zc->zc_nvlist_src_size != 0 && (error = 11149643SEric.Taylor@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 11159643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) { 11165094Slling nvlist_free(config); 11175094Slling return (error); 11185094Slling } 11195094Slling 1120789Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 11211544Seschrock guid != zc->zc_guid) 1122789Sahrens error = EINVAL; 11236643Seschrock else if (zc->zc_cookie) 112410921STim.Haley@Sun.COM error = spa_import_verbatim(zc->zc_name, config, props); 1125789Sahrens else 11265094Slling error = spa_import(zc->zc_name, config, props); 1127789Sahrens 112810921STim.Haley@Sun.COM if (zc->zc_nvlist_dst != 0) 112910921STim.Haley@Sun.COM (void) put_nvlist(zc, config); 113010921STim.Haley@Sun.COM 1131789Sahrens nvlist_free(config); 1132789Sahrens 11335094Slling if (props) 11345094Slling nvlist_free(props); 11355094Slling 1136789Sahrens return (error); 1137789Sahrens } 1138789Sahrens 1139789Sahrens static int 1140789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc) 1141789Sahrens { 11424543Smarks int error; 11437214Slling boolean_t force = (boolean_t)zc->zc_cookie; 11448211SGeorge.Wilson@Sun.COM boolean_t hardforce = (boolean_t)zc->zc_guid; 11457214Slling 11464543Smarks zfs_log_history(zc); 11478211SGeorge.Wilson@Sun.COM error = spa_export(zc->zc_name, NULL, force, hardforce); 114810588SEric.Taylor@Sun.COM if (error == 0) 114910588SEric.Taylor@Sun.COM zvol_remove_minors(zc->zc_name); 11504543Smarks return (error); 1151789Sahrens } 1152789Sahrens 1153789Sahrens static int 1154789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 1155789Sahrens { 1156789Sahrens nvlist_t *configs; 1157789Sahrens int error; 1158789Sahrens 1159789Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1160789Sahrens return (EEXIST); 1161789Sahrens 11622676Seschrock error = put_nvlist(zc, configs); 1163789Sahrens 1164789Sahrens nvlist_free(configs); 1165789Sahrens 1166789Sahrens return (error); 1167789Sahrens } 1168789Sahrens 1169789Sahrens static int 1170789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 1171789Sahrens { 1172789Sahrens nvlist_t *config; 1173789Sahrens int error; 11741544Seschrock int ret = 0; 1175789Sahrens 11762676Seschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 11772676Seschrock sizeof (zc->zc_value)); 1178789Sahrens 1179789Sahrens if (config != NULL) { 11802676Seschrock ret = put_nvlist(zc, config); 1181789Sahrens nvlist_free(config); 11821544Seschrock 11831544Seschrock /* 11841544Seschrock * The config may be present even if 'error' is non-zero. 11851544Seschrock * In this case we return success, and preserve the real errno 11861544Seschrock * in 'zc_cookie'. 11871544Seschrock */ 11881544Seschrock zc->zc_cookie = error; 1189789Sahrens } else { 11901544Seschrock ret = error; 1191789Sahrens } 1192789Sahrens 11931544Seschrock return (ret); 1194789Sahrens } 1195789Sahrens 1196789Sahrens /* 1197789Sahrens * Try to import the given pool, returning pool stats as appropriate so that 1198789Sahrens * user land knows which devices are available and overall pool health. 1199789Sahrens */ 1200789Sahrens static int 1201789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1202789Sahrens { 1203789Sahrens nvlist_t *tryconfig, *config; 1204789Sahrens int error; 1205789Sahrens 12065094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 12079643SEric.Taylor@Sun.COM zc->zc_iflags, &tryconfig)) != 0) 1208789Sahrens return (error); 1209789Sahrens 1210789Sahrens config = spa_tryimport(tryconfig); 1211789Sahrens 1212789Sahrens nvlist_free(tryconfig); 1213789Sahrens 1214789Sahrens if (config == NULL) 1215789Sahrens return (EINVAL); 1216789Sahrens 12172676Seschrock error = put_nvlist(zc, config); 1218789Sahrens nvlist_free(config); 1219789Sahrens 1220789Sahrens return (error); 1221789Sahrens } 1222789Sahrens 1223789Sahrens static int 1224789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc) 1225789Sahrens { 1226789Sahrens spa_t *spa; 1227789Sahrens int error; 1228789Sahrens 12292926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 12302926Sek110237 return (error); 12312926Sek110237 12327046Sahrens error = spa_scrub(spa, zc->zc_cookie); 12332926Sek110237 12342926Sek110237 spa_close(spa, FTAG); 12352926Sek110237 1236789Sahrens return (error); 1237789Sahrens } 1238789Sahrens 1239789Sahrens static int 1240789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1241789Sahrens { 1242789Sahrens spa_t *spa; 1243789Sahrens int error; 1244789Sahrens 1245789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1246789Sahrens if (error == 0) { 1247789Sahrens spa_freeze(spa); 1248789Sahrens spa_close(spa, FTAG); 1249789Sahrens } 1250789Sahrens return (error); 1251789Sahrens } 1252789Sahrens 1253789Sahrens static int 12541760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 12551760Seschrock { 12561760Seschrock spa_t *spa; 12571760Seschrock int error; 12581760Seschrock 12592926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 12602926Sek110237 return (error); 12612926Sek110237 12625118Slling if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 12635118Slling spa_close(spa, FTAG); 12645118Slling return (EINVAL); 12655118Slling } 12665118Slling 12675094Slling spa_upgrade(spa, zc->zc_cookie); 12682926Sek110237 spa_close(spa, FTAG); 12692926Sek110237 12702926Sek110237 return (error); 12712926Sek110237 } 12722926Sek110237 12732926Sek110237 static int 12742926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 12752926Sek110237 { 12762926Sek110237 spa_t *spa; 12772926Sek110237 char *hist_buf; 12782926Sek110237 uint64_t size; 12792926Sek110237 int error; 12802926Sek110237 12812926Sek110237 if ((size = zc->zc_history_len) == 0) 12822926Sek110237 return (EINVAL); 12832926Sek110237 12842926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 12852926Sek110237 return (error); 12862926Sek110237 12874577Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 12883863Sek110237 spa_close(spa, FTAG); 12893863Sek110237 return (ENOTSUP); 12903863Sek110237 } 12913863Sek110237 12922926Sek110237 hist_buf = kmem_alloc(size, KM_SLEEP); 12932926Sek110237 if ((error = spa_history_get(spa, &zc->zc_history_offset, 12942926Sek110237 &zc->zc_history_len, hist_buf)) == 0) { 12959643SEric.Taylor@Sun.COM error = ddi_copyout(hist_buf, 12969643SEric.Taylor@Sun.COM (void *)(uintptr_t)zc->zc_history, 12979643SEric.Taylor@Sun.COM zc->zc_history_len, zc->zc_iflags); 12982926Sek110237 } 12992926Sek110237 13002926Sek110237 spa_close(spa, FTAG); 13012926Sek110237 kmem_free(hist_buf, size); 13022926Sek110237 return (error); 13032926Sek110237 } 13042926Sek110237 13052926Sek110237 static int 13063444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 13073444Sek110237 { 13083444Sek110237 int error; 13093444Sek110237 13103912Slling if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 13113444Sek110237 return (error); 13123444Sek110237 13133444Sek110237 return (0); 13143444Sek110237 } 13153444Sek110237 131610298SMatthew.Ahrens@Sun.COM /* 131710298SMatthew.Ahrens@Sun.COM * inputs: 131810298SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 131910298SMatthew.Ahrens@Sun.COM * zc_obj object to find 132010298SMatthew.Ahrens@Sun.COM * 132110298SMatthew.Ahrens@Sun.COM * outputs: 132210298SMatthew.Ahrens@Sun.COM * zc_value name of object 132310298SMatthew.Ahrens@Sun.COM */ 13243444Sek110237 static int 13253444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 13263444Sek110237 { 132710298SMatthew.Ahrens@Sun.COM objset_t *os; 13283444Sek110237 int error; 13293444Sek110237 133010298SMatthew.Ahrens@Sun.COM /* XXX reading from objset not owned */ 133110298SMatthew.Ahrens@Sun.COM if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 13323444Sek110237 return (error); 133310298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 133410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 133510298SMatthew.Ahrens@Sun.COM return (EINVAL); 133610298SMatthew.Ahrens@Sun.COM } 133710298SMatthew.Ahrens@Sun.COM error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 13383444Sek110237 sizeof (zc->zc_value)); 133910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 13403444Sek110237 13413444Sek110237 return (error); 13423444Sek110237 } 13433444Sek110237 13443444Sek110237 static int 1345789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1346789Sahrens { 1347789Sahrens spa_t *spa; 1348789Sahrens int error; 13496423Sgw25295 nvlist_t *config, **l2cache, **spares; 13506423Sgw25295 uint_t nl2cache = 0, nspares = 0; 1351789Sahrens 1352789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1353789Sahrens if (error != 0) 1354789Sahrens return (error); 1355789Sahrens 13565450Sbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 13579643SEric.Taylor@Sun.COM zc->zc_iflags, &config); 13585450Sbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 13595450Sbrendan &l2cache, &nl2cache); 13605450Sbrendan 13616423Sgw25295 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 13626423Sgw25295 &spares, &nspares); 13636423Sgw25295 13643912Slling /* 13653912Slling * A root pool with concatenated devices is not supported. 13666423Sgw25295 * Thus, can not add a device to a root pool. 13676423Sgw25295 * 13686423Sgw25295 * Intent log device can not be added to a rootpool because 13696423Sgw25295 * during mountroot, zil is replayed, a seperated log device 13706423Sgw25295 * can not be accessed during the mountroot time. 13716423Sgw25295 * 13726423Sgw25295 * l2cache and spare devices are ok to be added to a rootpool. 13733912Slling */ 137410922SJeff.Bonwick@Sun.COM if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) { 13753912Slling spa_close(spa, FTAG); 13763912Slling return (EDOM); 13773912Slling } 13783912Slling 13795450Sbrendan if (error == 0) { 1380789Sahrens error = spa_vdev_add(spa, config); 1381789Sahrens nvlist_free(config); 1382789Sahrens } 1383789Sahrens spa_close(spa, FTAG); 1384789Sahrens return (error); 1385789Sahrens } 1386789Sahrens 1387789Sahrens static int 1388789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1389789Sahrens { 13902082Seschrock spa_t *spa; 13912082Seschrock int error; 13922082Seschrock 13932082Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 13942082Seschrock if (error != 0) 13952082Seschrock return (error); 13962082Seschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 13972082Seschrock spa_close(spa, FTAG); 13982082Seschrock return (error); 1399789Sahrens } 1400789Sahrens 1401789Sahrens static int 14024451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1403789Sahrens { 1404789Sahrens spa_t *spa; 1405789Sahrens int error; 14064451Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1407789Sahrens 14082926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1409789Sahrens return (error); 14104451Seschrock switch (zc->zc_cookie) { 14114451Seschrock case VDEV_STATE_ONLINE: 14124451Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 14134451Seschrock break; 14144451Seschrock 14154451Seschrock case VDEV_STATE_OFFLINE: 14164451Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 14174451Seschrock break; 1418789Sahrens 14194451Seschrock case VDEV_STATE_FAULTED: 142010817SEric.Schrock@Sun.COM if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 142110817SEric.Schrock@Sun.COM zc->zc_obj != VDEV_AUX_EXTERNAL) 142210817SEric.Schrock@Sun.COM zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 142310817SEric.Schrock@Sun.COM 142410817SEric.Schrock@Sun.COM error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 14254451Seschrock break; 1426789Sahrens 14274451Seschrock case VDEV_STATE_DEGRADED: 142810817SEric.Schrock@Sun.COM if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 142910817SEric.Schrock@Sun.COM zc->zc_obj != VDEV_AUX_EXTERNAL) 143010817SEric.Schrock@Sun.COM zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 143110817SEric.Schrock@Sun.COM 143210817SEric.Schrock@Sun.COM error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 14334451Seschrock break; 14344451Seschrock 14354451Seschrock default: 14364451Seschrock error = EINVAL; 14374451Seschrock } 14384451Seschrock zc->zc_cookie = newstate; 1439789Sahrens spa_close(spa, FTAG); 1440789Sahrens return (error); 1441789Sahrens } 1442789Sahrens 1443789Sahrens static int 1444789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1445789Sahrens { 1446789Sahrens spa_t *spa; 1447789Sahrens int replacing = zc->zc_cookie; 1448789Sahrens nvlist_t *config; 1449789Sahrens int error; 1450789Sahrens 14512926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1452789Sahrens return (error); 1453789Sahrens 14545094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 14559643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) == 0) { 14561544Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1457789Sahrens nvlist_free(config); 1458789Sahrens } 1459789Sahrens 1460789Sahrens spa_close(spa, FTAG); 1461789Sahrens return (error); 1462789Sahrens } 1463789Sahrens 1464789Sahrens static int 1465789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1466789Sahrens { 1467789Sahrens spa_t *spa; 1468789Sahrens int error; 1469789Sahrens 14702926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1471789Sahrens return (error); 1472789Sahrens 14738241SJeff.Bonwick@Sun.COM error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1474789Sahrens 1475789Sahrens spa_close(spa, FTAG); 1476789Sahrens return (error); 1477789Sahrens } 1478789Sahrens 1479789Sahrens static int 14801354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 14811354Seschrock { 14821354Seschrock spa_t *spa; 14832676Seschrock char *path = zc->zc_value; 14841544Seschrock uint64_t guid = zc->zc_guid; 14851354Seschrock int error; 14861354Seschrock 14871354Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 14881354Seschrock if (error != 0) 14891354Seschrock return (error); 14901354Seschrock 14911354Seschrock error = spa_vdev_setpath(spa, guid, path); 14921354Seschrock spa_close(spa, FTAG); 14931354Seschrock return (error); 14941354Seschrock } 14951354Seschrock 14969425SEric.Schrock@Sun.COM static int 14979425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 14989425SEric.Schrock@Sun.COM { 14999425SEric.Schrock@Sun.COM spa_t *spa; 15009425SEric.Schrock@Sun.COM char *fru = zc->zc_value; 15019425SEric.Schrock@Sun.COM uint64_t guid = zc->zc_guid; 15029425SEric.Schrock@Sun.COM int error; 15039425SEric.Schrock@Sun.COM 15049425SEric.Schrock@Sun.COM error = spa_open(zc->zc_name, &spa, FTAG); 15059425SEric.Schrock@Sun.COM if (error != 0) 15069425SEric.Schrock@Sun.COM return (error); 15079425SEric.Schrock@Sun.COM 15089425SEric.Schrock@Sun.COM error = spa_vdev_setfru(spa, guid, fru); 15099425SEric.Schrock@Sun.COM spa_close(spa, FTAG); 15109425SEric.Schrock@Sun.COM return (error); 15119425SEric.Schrock@Sun.COM } 15129425SEric.Schrock@Sun.COM 15135367Sahrens /* 15145367Sahrens * inputs: 15155367Sahrens * zc_name name of filesystem 15165367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 15175367Sahrens * 15185367Sahrens * outputs: 15195367Sahrens * zc_objset_stats stats 15205367Sahrens * zc_nvlist_dst property nvlist 15215367Sahrens * zc_nvlist_dst_size size of property nvlist 15225367Sahrens */ 15231354Seschrock static int 1524789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc) 1525789Sahrens { 1526789Sahrens objset_t *os = NULL; 1527789Sahrens int error; 15281356Seschrock nvlist_t *nv; 1529789Sahrens 153010298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1531789Sahrens return (error); 1532789Sahrens 15332885Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1534789Sahrens 15352856Snd150628 if (zc->zc_nvlist_dst != 0 && 153611022STom.Erickson@Sun.COM (error = dsl_prop_get_all(os, &nv)) == 0) { 15372885Sahrens dmu_objset_stats(os, nv); 15383087Sahrens /* 15395147Srm160521 * NB: zvol_get_stats() will read the objset contents, 15403087Sahrens * which we aren't supposed to do with a 15416689Smaybee * DS_MODE_USER hold, because it could be 15423087Sahrens * inconsistent. So this is a bit of a workaround... 154310298SMatthew.Ahrens@Sun.COM * XXX reading with out owning 15443087Sahrens */ 15454577Sahrens if (!zc->zc_objset_stats.dds_inconsistent) { 15464577Sahrens if (dmu_objset_type(os) == DMU_OST_ZVOL) 15474577Sahrens VERIFY(zvol_get_stats(os, nv) == 0); 15484577Sahrens } 15492676Seschrock error = put_nvlist(zc, nv); 15501356Seschrock nvlist_free(nv); 15511356Seschrock } 1552789Sahrens 155310298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1554789Sahrens return (error); 1555789Sahrens } 1556789Sahrens 155711022STom.Erickson@Sun.COM /* 155811022STom.Erickson@Sun.COM * inputs: 155911022STom.Erickson@Sun.COM * zc_name name of filesystem 156011022STom.Erickson@Sun.COM * zc_nvlist_dst_size size of buffer for property nvlist 156111022STom.Erickson@Sun.COM * 156211022STom.Erickson@Sun.COM * outputs: 156311022STom.Erickson@Sun.COM * zc_nvlist_dst received property nvlist 156411022STom.Erickson@Sun.COM * zc_nvlist_dst_size size of received property nvlist 156511022STom.Erickson@Sun.COM * 156611022STom.Erickson@Sun.COM * Gets received properties (distinct from local properties on or after 156711022STom.Erickson@Sun.COM * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 156811022STom.Erickson@Sun.COM * local property values. 156911022STom.Erickson@Sun.COM */ 157011022STom.Erickson@Sun.COM static int 157111022STom.Erickson@Sun.COM zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 157211022STom.Erickson@Sun.COM { 157311022STom.Erickson@Sun.COM objset_t *os = NULL; 157411022STom.Erickson@Sun.COM int error; 157511022STom.Erickson@Sun.COM nvlist_t *nv; 157611022STom.Erickson@Sun.COM 157711022STom.Erickson@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 157811022STom.Erickson@Sun.COM return (error); 157911022STom.Erickson@Sun.COM 158011022STom.Erickson@Sun.COM /* 158111022STom.Erickson@Sun.COM * Without this check, we would return local property values if the 158211022STom.Erickson@Sun.COM * caller has not already received properties on or after 158311022STom.Erickson@Sun.COM * SPA_VERSION_RECVD_PROPS. 158411022STom.Erickson@Sun.COM */ 158511022STom.Erickson@Sun.COM if (!dsl_prop_get_hasrecvd(os)) { 158611022STom.Erickson@Sun.COM dmu_objset_rele(os, FTAG); 158711022STom.Erickson@Sun.COM return (ENOTSUP); 158811022STom.Erickson@Sun.COM } 158911022STom.Erickson@Sun.COM 159011022STom.Erickson@Sun.COM if (zc->zc_nvlist_dst != 0 && 159111022STom.Erickson@Sun.COM (error = dsl_prop_get_received(os, &nv)) == 0) { 159211022STom.Erickson@Sun.COM error = put_nvlist(zc, nv); 159311022STom.Erickson@Sun.COM nvlist_free(nv); 159411022STom.Erickson@Sun.COM } 159511022STom.Erickson@Sun.COM 159611022STom.Erickson@Sun.COM dmu_objset_rele(os, FTAG); 159711022STom.Erickson@Sun.COM return (error); 159811022STom.Erickson@Sun.COM } 159911022STom.Erickson@Sun.COM 16005498Stimh static int 16015498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 16025498Stimh { 16035498Stimh uint64_t value; 16045498Stimh int error; 16055498Stimh 16065498Stimh /* 16075498Stimh * zfs_get_zplprop() will either find a value or give us 16085498Stimh * the default value (if there is one). 16095498Stimh */ 16105498Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 16115498Stimh return (error); 16125498Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 16135498Stimh return (0); 16145498Stimh } 16155498Stimh 16165498Stimh /* 16175498Stimh * inputs: 16185498Stimh * zc_name name of filesystem 16195498Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 16205498Stimh * 16215498Stimh * outputs: 16225498Stimh * zc_nvlist_dst zpl property nvlist 16235498Stimh * zc_nvlist_dst_size size of zpl property nvlist 16245498Stimh */ 16255498Stimh static int 16265498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 16275498Stimh { 16285498Stimh objset_t *os; 16295498Stimh int err; 16305498Stimh 163110298SMatthew.Ahrens@Sun.COM /* XXX reading without owning */ 163210298SMatthew.Ahrens@Sun.COM if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 16335498Stimh return (err); 16345498Stimh 16355498Stimh dmu_objset_fast_stat(os, &zc->zc_objset_stats); 16365498Stimh 16375498Stimh /* 16385498Stimh * NB: nvl_add_zplprop() will read the objset contents, 16396689Smaybee * which we aren't supposed to do with a DS_MODE_USER 16406689Smaybee * hold, because it could be inconsistent. 16415498Stimh */ 16425498Stimh if (zc->zc_nvlist_dst != NULL && 16435498Stimh !zc->zc_objset_stats.dds_inconsistent && 16445498Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 16455498Stimh nvlist_t *nv; 16465498Stimh 16475498Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 16485498Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 16495498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 16505498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 16515498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 16525498Stimh err = put_nvlist(zc, nv); 16535498Stimh nvlist_free(nv); 16545498Stimh } else { 16555498Stimh err = ENOENT; 16565498Stimh } 165710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 16585498Stimh return (err); 16595498Stimh } 16605498Stimh 16619396SMatthew.Ahrens@Sun.COM static boolean_t 16629396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name) 16639396SMatthew.Ahrens@Sun.COM { 16649396SMatthew.Ahrens@Sun.COM /* 16659396SMatthew.Ahrens@Sun.COM * Skip over datasets that are not visible in this zone, 16669396SMatthew.Ahrens@Sun.COM * internal datasets (which have a $ in their name), and 16679396SMatthew.Ahrens@Sun.COM * temporary datasets (which have a % in their name). 16689396SMatthew.Ahrens@Sun.COM */ 16699396SMatthew.Ahrens@Sun.COM if (strchr(name, '$') != NULL) 16709396SMatthew.Ahrens@Sun.COM return (B_TRUE); 16719396SMatthew.Ahrens@Sun.COM if (strchr(name, '%') != NULL) 16729396SMatthew.Ahrens@Sun.COM return (B_TRUE); 16739396SMatthew.Ahrens@Sun.COM if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) 16749396SMatthew.Ahrens@Sun.COM return (B_TRUE); 16759396SMatthew.Ahrens@Sun.COM return (B_FALSE); 16769396SMatthew.Ahrens@Sun.COM } 16779396SMatthew.Ahrens@Sun.COM 16785367Sahrens /* 16795367Sahrens * inputs: 16805367Sahrens * zc_name name of filesystem 16815367Sahrens * zc_cookie zap cursor 16825367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 16835367Sahrens * 16845367Sahrens * outputs: 16855367Sahrens * zc_name name of next filesystem 16869396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 16875367Sahrens * zc_objset_stats stats 16885367Sahrens * zc_nvlist_dst property nvlist 16895367Sahrens * zc_nvlist_dst_size size of property nvlist 16905367Sahrens */ 1691789Sahrens static int 1692789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1693789Sahrens { 1694885Sahrens objset_t *os; 1695789Sahrens int error; 1696789Sahrens char *p; 1697789Sahrens 169810298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 1699885Sahrens if (error == ENOENT) 1700885Sahrens error = ESRCH; 1701885Sahrens return (error); 1702789Sahrens } 1703789Sahrens 1704789Sahrens p = strrchr(zc->zc_name, '/'); 1705789Sahrens if (p == NULL || p[1] != '\0') 1706789Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1707789Sahrens p = zc->zc_name + strlen(zc->zc_name); 1708789Sahrens 17098697SRichard.Morris@Sun.COM /* 17108697SRichard.Morris@Sun.COM * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 17118697SRichard.Morris@Sun.COM * but is not declared void because its called by dmu_objset_find(). 17128697SRichard.Morris@Sun.COM */ 17138415SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) { 17148415SRichard.Morris@Sun.COM uint64_t cookie = 0; 17158415SRichard.Morris@Sun.COM int len = sizeof (zc->zc_name) - (p - zc->zc_name); 17168415SRichard.Morris@Sun.COM 17178415SRichard.Morris@Sun.COM while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) 17188697SRichard.Morris@Sun.COM (void) dmu_objset_prefetch(p, NULL); 17198415SRichard.Morris@Sun.COM } 17208415SRichard.Morris@Sun.COM 1721789Sahrens do { 1722885Sahrens error = dmu_dir_list_next(os, 1723885Sahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 1724885Sahrens NULL, &zc->zc_cookie); 1725789Sahrens if (error == ENOENT) 1726789Sahrens error = ESRCH; 172710588SEric.Taylor@Sun.COM } while (error == 0 && dataset_name_hidden(zc->zc_name) && 172810588SEric.Taylor@Sun.COM !(zc->zc_iflags & FKIOCTL)); 172910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1730789Sahrens 173110588SEric.Taylor@Sun.COM /* 173210588SEric.Taylor@Sun.COM * If it's an internal dataset (ie. with a '$' in its name), 173310588SEric.Taylor@Sun.COM * don't try to get stats for it, otherwise we'll return ENOENT. 173410588SEric.Taylor@Sun.COM */ 173510588SEric.Taylor@Sun.COM if (error == 0 && strchr(zc->zc_name, '$') == NULL) 1736885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 1737789Sahrens return (error); 1738789Sahrens } 1739789Sahrens 17405367Sahrens /* 17415367Sahrens * inputs: 17425367Sahrens * zc_name name of filesystem 17435367Sahrens * zc_cookie zap cursor 17445367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 17455367Sahrens * 17465367Sahrens * outputs: 17475367Sahrens * zc_name name of next snapshot 17485367Sahrens * zc_objset_stats stats 17495367Sahrens * zc_nvlist_dst property nvlist 17505367Sahrens * zc_nvlist_dst_size size of property nvlist 17515367Sahrens */ 1752789Sahrens static int 1753789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1754789Sahrens { 1755885Sahrens objset_t *os; 1756789Sahrens int error; 1757789Sahrens 175810474SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) 175910474SRichard.Morris@Sun.COM (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 176010474SRichard.Morris@Sun.COM NULL, DS_FIND_SNAPSHOTS); 176110474SRichard.Morris@Sun.COM 176210298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 17636689Smaybee if (error) 17646689Smaybee return (error == ENOENT ? ESRCH : error); 1765789Sahrens 17661003Slling /* 17671003Slling * A dataset name of maximum length cannot have any snapshots, 17681003Slling * so exit immediately. 17691003Slling */ 17701003Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 177110298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 17721003Slling return (ESRCH); 1773789Sahrens } 1774789Sahrens 1775885Sahrens error = dmu_snapshot_list_next(os, 1776885Sahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 17775663Sck153898 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 177810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1779885Sahrens if (error == 0) 1780885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 17816689Smaybee else if (error == ENOENT) 17826689Smaybee error = ESRCH; 1783789Sahrens 17845367Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 17856689Smaybee if (error) 17865367Sahrens *strchr(zc->zc_name, '@') = '\0'; 1787789Sahrens return (error); 1788789Sahrens } 1789789Sahrens 179011022STom.Erickson@Sun.COM static int 179111022STom.Erickson@Sun.COM zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 179211022STom.Erickson@Sun.COM { 179311022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 179411022STom.Erickson@Sun.COM uint64_t *valary; 179511022STom.Erickson@Sun.COM unsigned int vallen; 179611022STom.Erickson@Sun.COM const char *domain; 179711022STom.Erickson@Sun.COM zfs_userquota_prop_t type; 179811022STom.Erickson@Sun.COM uint64_t rid; 179911022STom.Erickson@Sun.COM uint64_t quota; 180011022STom.Erickson@Sun.COM zfsvfs_t *zfsvfs; 180111022STom.Erickson@Sun.COM int err; 180211022STom.Erickson@Sun.COM 180311022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 180411022STom.Erickson@Sun.COM nvlist_t *attrs; 180511022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 180611022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 180711022STom.Erickson@Sun.COM &pair) == 0); 180811022STom.Erickson@Sun.COM } 180911022STom.Erickson@Sun.COM 181011022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64_array(pair, &valary, &vallen) == 0); 181111022STom.Erickson@Sun.COM VERIFY(vallen == 3); 181211022STom.Erickson@Sun.COM type = valary[0]; 181311022STom.Erickson@Sun.COM rid = valary[1]; 181411022STom.Erickson@Sun.COM quota = valary[2]; 181511022STom.Erickson@Sun.COM /* 181611022STom.Erickson@Sun.COM * The propname is encoded as 181711022STom.Erickson@Sun.COM * userquota@<rid>-<domain>. 181811022STom.Erickson@Sun.COM */ 181911022STom.Erickson@Sun.COM domain = strchr(propname, '-') + 1; 182011022STom.Erickson@Sun.COM 182111022STom.Erickson@Sun.COM err = zfsvfs_hold(dsname, FTAG, &zfsvfs); 182211022STom.Erickson@Sun.COM if (err == 0) { 182311022STom.Erickson@Sun.COM err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 182411022STom.Erickson@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 182511022STom.Erickson@Sun.COM } 182611022STom.Erickson@Sun.COM 182711022STom.Erickson@Sun.COM return (err); 182811022STom.Erickson@Sun.COM } 182911022STom.Erickson@Sun.COM 183011022STom.Erickson@Sun.COM /* 183111022STom.Erickson@Sun.COM * If the named property is one that has a special function to set its value, 183211022STom.Erickson@Sun.COM * return 0 on success and a positive error code on failure; otherwise if it is 183311022STom.Erickson@Sun.COM * not one of the special properties handled by this function, return -1. 183411022STom.Erickson@Sun.COM * 183511022STom.Erickson@Sun.COM * XXX: It would be better for callers of the properety interface if we handled 183611022STom.Erickson@Sun.COM * these special cases in dsl_prop.c (in the dsl layer). 183711022STom.Erickson@Sun.COM */ 183811022STom.Erickson@Sun.COM static int 183911022STom.Erickson@Sun.COM zfs_prop_set_special(const char *dsname, zprop_source_t source, 184011022STom.Erickson@Sun.COM nvpair_t *pair) 1841789Sahrens { 184211022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 184311022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(propname); 184411022STom.Erickson@Sun.COM uint64_t intval; 184511022STom.Erickson@Sun.COM int err; 184611022STom.Erickson@Sun.COM 184711022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 184811022STom.Erickson@Sun.COM if (zfs_prop_userquota(propname)) 184911022STom.Erickson@Sun.COM return (zfs_prop_set_userquota(dsname, pair)); 185011022STom.Erickson@Sun.COM return (-1); 185111022STom.Erickson@Sun.COM } 185211022STom.Erickson@Sun.COM 185311022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 185411022STom.Erickson@Sun.COM nvlist_t *attrs; 185511022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 185611022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 185711022STom.Erickson@Sun.COM &pair) == 0); 185811022STom.Erickson@Sun.COM } 185911022STom.Erickson@Sun.COM 186011022STom.Erickson@Sun.COM if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) 186111022STom.Erickson@Sun.COM return (-1); 186211022STom.Erickson@Sun.COM 186311022STom.Erickson@Sun.COM VERIFY(0 == nvpair_value_uint64(pair, &intval)); 186411022STom.Erickson@Sun.COM 186511022STom.Erickson@Sun.COM switch (prop) { 186611022STom.Erickson@Sun.COM case ZFS_PROP_QUOTA: 186711022STom.Erickson@Sun.COM err = dsl_dir_set_quota(dsname, source, intval); 186811022STom.Erickson@Sun.COM break; 186911022STom.Erickson@Sun.COM case ZFS_PROP_REFQUOTA: 187011022STom.Erickson@Sun.COM err = dsl_dataset_set_quota(dsname, source, intval); 187111022STom.Erickson@Sun.COM break; 187211022STom.Erickson@Sun.COM case ZFS_PROP_RESERVATION: 187311022STom.Erickson@Sun.COM err = dsl_dir_set_reservation(dsname, source, intval); 187411022STom.Erickson@Sun.COM break; 187511022STom.Erickson@Sun.COM case ZFS_PROP_REFRESERVATION: 187611022STom.Erickson@Sun.COM err = dsl_dataset_set_reservation(dsname, source, intval); 187711022STom.Erickson@Sun.COM break; 187811022STom.Erickson@Sun.COM case ZFS_PROP_VOLSIZE: 187911022STom.Erickson@Sun.COM err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip), 188011022STom.Erickson@Sun.COM intval); 188111022STom.Erickson@Sun.COM break; 188211022STom.Erickson@Sun.COM case ZFS_PROP_VERSION: 188311022STom.Erickson@Sun.COM { 188411022STom.Erickson@Sun.COM zfsvfs_t *zfsvfs; 188511022STom.Erickson@Sun.COM 188611022STom.Erickson@Sun.COM if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs)) != 0) 188711022STom.Erickson@Sun.COM break; 188811022STom.Erickson@Sun.COM 188911022STom.Erickson@Sun.COM err = zfs_set_version(zfsvfs, intval); 189011022STom.Erickson@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 189111022STom.Erickson@Sun.COM 189211022STom.Erickson@Sun.COM if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 1893*11147SGeorge.Wilson@Sun.COM zfs_cmd_t *zc; 1894*11147SGeorge.Wilson@Sun.COM 1895*11147SGeorge.Wilson@Sun.COM zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 1896*11147SGeorge.Wilson@Sun.COM (void) strcpy(zc->zc_name, dsname); 1897*11147SGeorge.Wilson@Sun.COM (void) zfs_ioc_userspace_upgrade(zc); 1898*11147SGeorge.Wilson@Sun.COM kmem_free(zc, sizeof (zfs_cmd_t)); 189911022STom.Erickson@Sun.COM } 190011022STom.Erickson@Sun.COM break; 190111022STom.Erickson@Sun.COM } 190211022STom.Erickson@Sun.COM 190311022STom.Erickson@Sun.COM default: 190411022STom.Erickson@Sun.COM err = -1; 190511022STom.Erickson@Sun.COM } 190611022STom.Erickson@Sun.COM 190711022STom.Erickson@Sun.COM return (err); 190811022STom.Erickson@Sun.COM } 190911022STom.Erickson@Sun.COM 191011022STom.Erickson@Sun.COM /* 191111022STom.Erickson@Sun.COM * This function is best effort. If it fails to set any of the given properties, 191211022STom.Erickson@Sun.COM * it continues to set as many as it can and returns the first error 191311022STom.Erickson@Sun.COM * encountered. If the caller provides a non-NULL errlist, it also gives the 191411022STom.Erickson@Sun.COM * complete list of names of all the properties it failed to set along with the 191511022STom.Erickson@Sun.COM * corresponding error numbers. The caller is responsible for freeing the 191611022STom.Erickson@Sun.COM * returned errlist. 191711022STom.Erickson@Sun.COM * 191811022STom.Erickson@Sun.COM * If every property is set successfully, zero is returned and the list pointed 191911022STom.Erickson@Sun.COM * at by errlist is NULL. 192011022STom.Erickson@Sun.COM */ 192111022STom.Erickson@Sun.COM int 192211022STom.Erickson@Sun.COM zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 192311022STom.Erickson@Sun.COM nvlist_t **errlist) 192411022STom.Erickson@Sun.COM { 192511022STom.Erickson@Sun.COM nvpair_t *pair; 192611022STom.Erickson@Sun.COM nvpair_t *propval; 192711045STom.Erickson@Sun.COM int err = 0; 192811045STom.Erickson@Sun.COM int rv = 0; 19292676Seschrock uint64_t intval; 19302676Seschrock char *strval; 19318697SRichard.Morris@Sun.COM nvlist_t *genericnvl; 193211022STom.Erickson@Sun.COM nvlist_t *errors; 193311022STom.Erickson@Sun.COM nvlist_t *retrynvl; 19344543Smarks 19358697SRichard.Morris@Sun.COM VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 193611022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 193711022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 193811022STom.Erickson@Sun.COM 193911022STom.Erickson@Sun.COM retry: 194011022STom.Erickson@Sun.COM pair = NULL; 194111022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 194211022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 19434670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 19444543Smarks 194511022STom.Erickson@Sun.COM /* decode the property value */ 194611022STom.Erickson@Sun.COM propval = pair; 194711022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 194811022STom.Erickson@Sun.COM nvlist_t *attrs; 194911022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 195011022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 195111022STom.Erickson@Sun.COM &propval) == 0); 19524543Smarks } 19532676Seschrock 195411022STom.Erickson@Sun.COM /* Validate value type */ 195511022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 195611022STom.Erickson@Sun.COM if (zfs_prop_user(propname)) { 195711022STom.Erickson@Sun.COM if (nvpair_type(propval) != DATA_TYPE_STRING) 195811022STom.Erickson@Sun.COM err = EINVAL; 195911022STom.Erickson@Sun.COM } else if (zfs_prop_userquota(propname)) { 196011022STom.Erickson@Sun.COM if (nvpair_type(propval) != 196111022STom.Erickson@Sun.COM DATA_TYPE_UINT64_ARRAY) 196211022STom.Erickson@Sun.COM err = EINVAL; 19639396SMatthew.Ahrens@Sun.COM } 196411022STom.Erickson@Sun.COM } else { 196511022STom.Erickson@Sun.COM if (nvpair_type(propval) == DATA_TYPE_STRING) { 196611022STom.Erickson@Sun.COM if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 196711022STom.Erickson@Sun.COM err = EINVAL; 196811022STom.Erickson@Sun.COM } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 19692885Sahrens const char *unused; 19702885Sahrens 197111022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(propval, 197211022STom.Erickson@Sun.COM &intval) == 0); 19732676Seschrock 19742676Seschrock switch (zfs_prop_get_type(prop)) { 19754787Sahrens case PROP_TYPE_NUMBER: 19762676Seschrock break; 19774787Sahrens case PROP_TYPE_STRING: 197811022STom.Erickson@Sun.COM err = EINVAL; 197911022STom.Erickson@Sun.COM break; 19804787Sahrens case PROP_TYPE_INDEX: 19812717Seschrock if (zfs_prop_index_to_string(prop, 198211022STom.Erickson@Sun.COM intval, &unused) != 0) 198311022STom.Erickson@Sun.COM err = EINVAL; 19842676Seschrock break; 19852676Seschrock default: 19864577Sahrens cmn_err(CE_PANIC, 19874577Sahrens "unknown property type"); 19882676Seschrock } 19892676Seschrock } else { 199011022STom.Erickson@Sun.COM err = EINVAL; 199111022STom.Erickson@Sun.COM } 199211022STom.Erickson@Sun.COM } 199311022STom.Erickson@Sun.COM 199411022STom.Erickson@Sun.COM /* Validate permissions */ 199511022STom.Erickson@Sun.COM if (err == 0) 199611022STom.Erickson@Sun.COM err = zfs_check_settable(dsname, pair, CRED()); 199711022STom.Erickson@Sun.COM 199811022STom.Erickson@Sun.COM if (err == 0) { 199911022STom.Erickson@Sun.COM err = zfs_prop_set_special(dsname, source, pair); 200011022STom.Erickson@Sun.COM if (err == -1) { 200111022STom.Erickson@Sun.COM /* 200211022STom.Erickson@Sun.COM * For better performance we build up a list of 200311022STom.Erickson@Sun.COM * properties to set in a single transaction. 200411022STom.Erickson@Sun.COM */ 200511022STom.Erickson@Sun.COM err = nvlist_add_nvpair(genericnvl, pair); 200611022STom.Erickson@Sun.COM } else if (err != 0 && nvl != retrynvl) { 200711022STom.Erickson@Sun.COM /* 200811022STom.Erickson@Sun.COM * This may be a spurious error caused by 200911022STom.Erickson@Sun.COM * receiving quota and reservation out of order. 201011022STom.Erickson@Sun.COM * Try again in a second pass. 201111022STom.Erickson@Sun.COM */ 201211022STom.Erickson@Sun.COM err = nvlist_add_nvpair(retrynvl, pair); 20132676Seschrock } 201411022STom.Erickson@Sun.COM } 201511022STom.Erickson@Sun.COM 201611022STom.Erickson@Sun.COM if (err != 0) 201711022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(errors, propname, err) == 0); 201811022STom.Erickson@Sun.COM } 201911022STom.Erickson@Sun.COM 202011022STom.Erickson@Sun.COM if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 202111022STom.Erickson@Sun.COM nvl = retrynvl; 202211022STom.Erickson@Sun.COM goto retry; 202311022STom.Erickson@Sun.COM } 202411022STom.Erickson@Sun.COM 202511022STom.Erickson@Sun.COM if (!nvlist_empty(genericnvl) && 202611022STom.Erickson@Sun.COM dsl_props_set(dsname, source, genericnvl) != 0) { 202711022STom.Erickson@Sun.COM /* 202811022STom.Erickson@Sun.COM * If this fails, we still want to set as many properties as we 202911022STom.Erickson@Sun.COM * can, so try setting them individually. 203011022STom.Erickson@Sun.COM */ 203111022STom.Erickson@Sun.COM pair = NULL; 203211022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 203311022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 203411022STom.Erickson@Sun.COM 203511022STom.Erickson@Sun.COM propval = pair; 203611022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 203711022STom.Erickson@Sun.COM nvlist_t *attrs; 203811022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 203911022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 204011022STom.Erickson@Sun.COM &propval) == 0); 204111022STom.Erickson@Sun.COM } 204211022STom.Erickson@Sun.COM 204311022STom.Erickson@Sun.COM if (nvpair_type(propval) == DATA_TYPE_STRING) { 204411022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(propval, 204511022STom.Erickson@Sun.COM &strval) == 0); 204611022STom.Erickson@Sun.COM err = dsl_prop_set(dsname, propname, source, 1, 204711022STom.Erickson@Sun.COM strlen(strval) + 1, strval); 204811022STom.Erickson@Sun.COM } else { 204911022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(propval, 205011022STom.Erickson@Sun.COM &intval) == 0); 205111022STom.Erickson@Sun.COM err = dsl_prop_set(dsname, propname, source, 8, 205211022STom.Erickson@Sun.COM 1, &intval); 205311022STom.Erickson@Sun.COM } 205411022STom.Erickson@Sun.COM 205511022STom.Erickson@Sun.COM if (err != 0) { 205611022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(errors, propname, 205711022STom.Erickson@Sun.COM err) == 0); 205811022STom.Erickson@Sun.COM } 20592676Seschrock } 20602676Seschrock } 206111022STom.Erickson@Sun.COM nvlist_free(genericnvl); 206211022STom.Erickson@Sun.COM nvlist_free(retrynvl); 206311022STom.Erickson@Sun.COM 206411022STom.Erickson@Sun.COM if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 206511022STom.Erickson@Sun.COM nvlist_free(errors); 206611022STom.Erickson@Sun.COM errors = NULL; 206711022STom.Erickson@Sun.COM } else { 206811022STom.Erickson@Sun.COM VERIFY(nvpair_value_int32(pair, &rv) == 0); 20698697SRichard.Morris@Sun.COM } 207011022STom.Erickson@Sun.COM 207111022STom.Erickson@Sun.COM if (errlist == NULL) 207211022STom.Erickson@Sun.COM nvlist_free(errors); 207311022STom.Erickson@Sun.COM else 207411022STom.Erickson@Sun.COM *errlist = errors; 207511022STom.Erickson@Sun.COM 207611022STom.Erickson@Sun.COM return (rv); 2077789Sahrens } 2078789Sahrens 20795367Sahrens /* 20809355SMatthew.Ahrens@Sun.COM * Check that all the properties are valid user properties. 20819355SMatthew.Ahrens@Sun.COM */ 20829355SMatthew.Ahrens@Sun.COM static int 20839355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl) 20849355SMatthew.Ahrens@Sun.COM { 208511022STom.Erickson@Sun.COM nvpair_t *pair = NULL; 20869355SMatthew.Ahrens@Sun.COM int error = 0; 20879355SMatthew.Ahrens@Sun.COM 208811022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 208911022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 20909355SMatthew.Ahrens@Sun.COM char *valstr; 20919355SMatthew.Ahrens@Sun.COM 20929355SMatthew.Ahrens@Sun.COM if (!zfs_prop_user(propname) || 209311022STom.Erickson@Sun.COM nvpair_type(pair) != DATA_TYPE_STRING) 20949355SMatthew.Ahrens@Sun.COM return (EINVAL); 20959355SMatthew.Ahrens@Sun.COM 20969355SMatthew.Ahrens@Sun.COM if (error = zfs_secpolicy_write_perms(fsname, 20979355SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERPROP, CRED())) 20989355SMatthew.Ahrens@Sun.COM return (error); 20999355SMatthew.Ahrens@Sun.COM 21009355SMatthew.Ahrens@Sun.COM if (strlen(propname) >= ZAP_MAXNAMELEN) 21019355SMatthew.Ahrens@Sun.COM return (ENAMETOOLONG); 21029355SMatthew.Ahrens@Sun.COM 210311022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(pair, &valstr) == 0); 21049355SMatthew.Ahrens@Sun.COM if (strlen(valstr) >= ZAP_MAXVALUELEN) 21059355SMatthew.Ahrens@Sun.COM return (E2BIG); 21069355SMatthew.Ahrens@Sun.COM } 21079355SMatthew.Ahrens@Sun.COM return (0); 21089355SMatthew.Ahrens@Sun.COM } 21099355SMatthew.Ahrens@Sun.COM 211011022STom.Erickson@Sun.COM static void 211111022STom.Erickson@Sun.COM props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 211211022STom.Erickson@Sun.COM { 211311022STom.Erickson@Sun.COM nvpair_t *pair; 211411022STom.Erickson@Sun.COM 211511022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 211611022STom.Erickson@Sun.COM 211711022STom.Erickson@Sun.COM pair = NULL; 211811022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 211911022STom.Erickson@Sun.COM if (nvlist_exists(skipped, nvpair_name(pair))) 212011022STom.Erickson@Sun.COM continue; 212111022STom.Erickson@Sun.COM 212211022STom.Erickson@Sun.COM VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 212311022STom.Erickson@Sun.COM } 212411022STom.Erickson@Sun.COM } 212511022STom.Erickson@Sun.COM 212611022STom.Erickson@Sun.COM static int 212711022STom.Erickson@Sun.COM clear_received_props(objset_t *os, const char *fs, nvlist_t *props, 212811022STom.Erickson@Sun.COM nvlist_t *skipped) 212911022STom.Erickson@Sun.COM { 213011022STom.Erickson@Sun.COM int err = 0; 213111022STom.Erickson@Sun.COM nvlist_t *cleared_props = NULL; 213211022STom.Erickson@Sun.COM props_skip(props, skipped, &cleared_props); 213311022STom.Erickson@Sun.COM if (!nvlist_empty(cleared_props)) { 213411022STom.Erickson@Sun.COM /* 213511022STom.Erickson@Sun.COM * Acts on local properties until the dataset has received 213611022STom.Erickson@Sun.COM * properties at least once on or after SPA_VERSION_RECVD_PROPS. 213711022STom.Erickson@Sun.COM */ 213811022STom.Erickson@Sun.COM zprop_source_t flags = (ZPROP_SRC_NONE | 213911022STom.Erickson@Sun.COM (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0)); 214011022STom.Erickson@Sun.COM err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL); 214111022STom.Erickson@Sun.COM } 214211022STom.Erickson@Sun.COM nvlist_free(cleared_props); 214311022STom.Erickson@Sun.COM return (err); 214411022STom.Erickson@Sun.COM } 214511022STom.Erickson@Sun.COM 21469355SMatthew.Ahrens@Sun.COM /* 21475367Sahrens * inputs: 21485367Sahrens * zc_name name of filesystem 21498697SRichard.Morris@Sun.COM * zc_value name of property to set 21505367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 215111022STom.Erickson@Sun.COM * zc_cookie received properties flag 21525367Sahrens * 215311022STom.Erickson@Sun.COM * outputs: 215411022STom.Erickson@Sun.COM * zc_nvlist_dst{_size} error for each unapplied received property 21555367Sahrens */ 2156789Sahrens static int 21572676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 2158789Sahrens { 21592676Seschrock nvlist_t *nvl; 216011022STom.Erickson@Sun.COM boolean_t received = zc->zc_cookie; 216111022STom.Erickson@Sun.COM zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 216211022STom.Erickson@Sun.COM ZPROP_SRC_LOCAL); 216311022STom.Erickson@Sun.COM nvlist_t *errors = NULL; 21642676Seschrock int error; 2165789Sahrens 21665094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 21679643SEric.Taylor@Sun.COM zc->zc_iflags, &nvl)) != 0) 21682676Seschrock return (error); 21692676Seschrock 217011022STom.Erickson@Sun.COM if (received) { 21717265Sahrens nvlist_t *origprops; 21727265Sahrens objset_t *os; 21737265Sahrens 217410298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) { 217511022STom.Erickson@Sun.COM if (dsl_prop_get_received(os, &origprops) == 0) { 217611022STom.Erickson@Sun.COM (void) clear_received_props(os, 217711022STom.Erickson@Sun.COM zc->zc_name, origprops, nvl); 21787265Sahrens nvlist_free(origprops); 21797265Sahrens } 218011022STom.Erickson@Sun.COM 218111022STom.Erickson@Sun.COM dsl_prop_set_hasrecvd(os); 218210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 21837265Sahrens } 21847265Sahrens } 21857265Sahrens 218611022STom.Erickson@Sun.COM error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors); 218711022STom.Erickson@Sun.COM 218811022STom.Erickson@Sun.COM if (zc->zc_nvlist_dst != NULL && errors != NULL) { 218911022STom.Erickson@Sun.COM (void) put_nvlist(zc, errors); 219011022STom.Erickson@Sun.COM } 219111022STom.Erickson@Sun.COM 219211022STom.Erickson@Sun.COM nvlist_free(errors); 21932676Seschrock nvlist_free(nvl); 21942676Seschrock return (error); 2195789Sahrens } 2196789Sahrens 21975367Sahrens /* 21985367Sahrens * inputs: 21995367Sahrens * zc_name name of filesystem 22005367Sahrens * zc_value name of property to inherit 220111022STom.Erickson@Sun.COM * zc_cookie revert to received value if TRUE 22025367Sahrens * 22035367Sahrens * outputs: none 22045367Sahrens */ 2205789Sahrens static int 22064849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 22074849Sahrens { 220811022STom.Erickson@Sun.COM const char *propname = zc->zc_value; 220911022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(propname); 221011022STom.Erickson@Sun.COM boolean_t received = zc->zc_cookie; 221111022STom.Erickson@Sun.COM zprop_source_t source = (received 221211022STom.Erickson@Sun.COM ? ZPROP_SRC_NONE /* revert to received value, if any */ 221311022STom.Erickson@Sun.COM : ZPROP_SRC_INHERITED); /* explicitly inherit */ 221411022STom.Erickson@Sun.COM 221511022STom.Erickson@Sun.COM if (received) { 221611022STom.Erickson@Sun.COM nvlist_t *dummy; 221711022STom.Erickson@Sun.COM nvpair_t *pair; 221811022STom.Erickson@Sun.COM zprop_type_t type; 221911022STom.Erickson@Sun.COM int err; 222011022STom.Erickson@Sun.COM 222111022STom.Erickson@Sun.COM /* 222211022STom.Erickson@Sun.COM * zfs_prop_set_special() expects properties in the form of an 222311022STom.Erickson@Sun.COM * nvpair with type info. 222411022STom.Erickson@Sun.COM */ 222511022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 222611022STom.Erickson@Sun.COM if (!zfs_prop_user(propname)) 222711022STom.Erickson@Sun.COM return (EINVAL); 222811022STom.Erickson@Sun.COM 222911022STom.Erickson@Sun.COM type = PROP_TYPE_STRING; 223011022STom.Erickson@Sun.COM } else { 223111022STom.Erickson@Sun.COM type = zfs_prop_get_type(prop); 223211022STom.Erickson@Sun.COM } 223311022STom.Erickson@Sun.COM 223411022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0); 223511022STom.Erickson@Sun.COM 223611022STom.Erickson@Sun.COM switch (type) { 223711022STom.Erickson@Sun.COM case PROP_TYPE_STRING: 223811022STom.Erickson@Sun.COM VERIFY(0 == nvlist_add_string(dummy, propname, "")); 223911022STom.Erickson@Sun.COM break; 224011022STom.Erickson@Sun.COM case PROP_TYPE_NUMBER: 224111022STom.Erickson@Sun.COM case PROP_TYPE_INDEX: 224211022STom.Erickson@Sun.COM VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 224311022STom.Erickson@Sun.COM break; 224411022STom.Erickson@Sun.COM default: 224511022STom.Erickson@Sun.COM nvlist_free(dummy); 224611022STom.Erickson@Sun.COM return (EINVAL); 224711022STom.Erickson@Sun.COM } 224811022STom.Erickson@Sun.COM 224911022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(dummy, NULL); 225011022STom.Erickson@Sun.COM err = zfs_prop_set_special(zc->zc_name, source, pair); 225111022STom.Erickson@Sun.COM nvlist_free(dummy); 225211022STom.Erickson@Sun.COM if (err != -1) 225311022STom.Erickson@Sun.COM return (err); /* special property already handled */ 225411022STom.Erickson@Sun.COM } else { 225511022STom.Erickson@Sun.COM /* 225611022STom.Erickson@Sun.COM * Only check this in the non-received case. We want to allow 225711022STom.Erickson@Sun.COM * 'inherit -S' to revert non-inheritable properties like quota 225811022STom.Erickson@Sun.COM * and reservation to the received or default values even though 225911022STom.Erickson@Sun.COM * they are not considered inheritable. 226011022STom.Erickson@Sun.COM */ 226111022STom.Erickson@Sun.COM if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 226211022STom.Erickson@Sun.COM return (EINVAL); 226311022STom.Erickson@Sun.COM } 226411022STom.Erickson@Sun.COM 22654849Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 226611022STom.Erickson@Sun.COM return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL)); 22674849Sahrens } 22684849Sahrens 22694849Sahrens static int 22704098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 22713912Slling { 22725094Slling nvlist_t *props; 22733912Slling spa_t *spa; 22745094Slling int error; 227511022STom.Erickson@Sun.COM nvpair_t *pair; 227611022STom.Erickson@Sun.COM 227711022STom.Erickson@Sun.COM if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 227811022STom.Erickson@Sun.COM zc->zc_iflags, &props)) 22793912Slling return (error); 22803912Slling 22818525SEric.Schrock@Sun.COM /* 22828525SEric.Schrock@Sun.COM * If the only property is the configfile, then just do a spa_lookup() 22838525SEric.Schrock@Sun.COM * to handle the faulted case. 22848525SEric.Schrock@Sun.COM */ 228511022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(props, NULL); 228611022STom.Erickson@Sun.COM if (pair != NULL && strcmp(nvpair_name(pair), 22878525SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 228811022STom.Erickson@Sun.COM nvlist_next_nvpair(props, pair) == NULL) { 22898525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 22908525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) { 22918525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 22928525SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 22938525SEric.Schrock@Sun.COM } 22948525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 229510672SEric.Schrock@Sun.COM if (spa != NULL) { 229610672SEric.Schrock@Sun.COM nvlist_free(props); 22978525SEric.Schrock@Sun.COM return (0); 229810672SEric.Schrock@Sun.COM } 22998525SEric.Schrock@Sun.COM } 23008525SEric.Schrock@Sun.COM 23013912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 23025094Slling nvlist_free(props); 23033912Slling return (error); 23043912Slling } 23053912Slling 23065094Slling error = spa_prop_set(spa, props); 23073912Slling 23085094Slling nvlist_free(props); 23093912Slling spa_close(spa, FTAG); 23103912Slling 23113912Slling return (error); 23123912Slling } 23133912Slling 23143912Slling static int 23154098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 23163912Slling { 23173912Slling spa_t *spa; 23183912Slling int error; 23193912Slling nvlist_t *nvp = NULL; 23203912Slling 23218525SEric.Schrock@Sun.COM if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 23228525SEric.Schrock@Sun.COM /* 23238525SEric.Schrock@Sun.COM * If the pool is faulted, there may be properties we can still 23248525SEric.Schrock@Sun.COM * get (such as altroot and cachefile), so attempt to get them 23258525SEric.Schrock@Sun.COM * anyway. 23268525SEric.Schrock@Sun.COM */ 23278525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 23288525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) 23298525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 23308525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 23318525SEric.Schrock@Sun.COM } else { 23328525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 23338525SEric.Schrock@Sun.COM spa_close(spa, FTAG); 23348525SEric.Schrock@Sun.COM } 23353912Slling 23363912Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 23373912Slling error = put_nvlist(zc, nvp); 23383912Slling else 23393912Slling error = EFAULT; 23403912Slling 23418525SEric.Schrock@Sun.COM nvlist_free(nvp); 23423912Slling return (error); 23433912Slling } 23443912Slling 23453912Slling static int 23464543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 23474543Smarks { 23484543Smarks nvlist_t *nvp; 23494543Smarks int error; 23504543Smarks uint32_t uid; 23514543Smarks uint32_t gid; 23524543Smarks uint32_t *groups; 23534543Smarks uint_t group_cnt; 23544543Smarks cred_t *usercred; 23554543Smarks 23565094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 23579643SEric.Taylor@Sun.COM zc->zc_iflags, &nvp)) != 0) { 23584543Smarks return (error); 23594543Smarks } 23604543Smarks 23614543Smarks if ((error = nvlist_lookup_uint32(nvp, 23624543Smarks ZFS_DELEG_PERM_UID, &uid)) != 0) { 23634543Smarks nvlist_free(nvp); 23644543Smarks return (EPERM); 23654543Smarks } 23664543Smarks 23674543Smarks if ((error = nvlist_lookup_uint32(nvp, 23684543Smarks ZFS_DELEG_PERM_GID, &gid)) != 0) { 23694543Smarks nvlist_free(nvp); 23704543Smarks return (EPERM); 23714543Smarks } 23724543Smarks 23734543Smarks if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 23744543Smarks &groups, &group_cnt)) != 0) { 23754543Smarks nvlist_free(nvp); 23764543Smarks return (EPERM); 23774543Smarks } 23784543Smarks usercred = cralloc(); 23794543Smarks if ((crsetugid(usercred, uid, gid) != 0) || 23804543Smarks (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 23814543Smarks nvlist_free(nvp); 23824543Smarks crfree(usercred); 23834543Smarks return (EPERM); 23844543Smarks } 23854543Smarks nvlist_free(nvp); 23864543Smarks error = dsl_deleg_access(zc->zc_name, 23874787Sahrens zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred); 23884543Smarks crfree(usercred); 23894543Smarks return (error); 23904543Smarks } 23914543Smarks 23925367Sahrens /* 23935367Sahrens * inputs: 23945367Sahrens * zc_name name of filesystem 23955367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 23965367Sahrens * zc_perm_action allow/unallow flag 23975367Sahrens * 23985367Sahrens * outputs: none 23995367Sahrens */ 24004543Smarks static int 24014543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 24024543Smarks { 24034543Smarks int error; 24044543Smarks nvlist_t *fsaclnv = NULL; 24054543Smarks 24065094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 24079643SEric.Taylor@Sun.COM zc->zc_iflags, &fsaclnv)) != 0) 24084543Smarks return (error); 24094543Smarks 24104543Smarks /* 24114543Smarks * Verify nvlist is constructed correctly 24124543Smarks */ 24134543Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 24144543Smarks nvlist_free(fsaclnv); 24154543Smarks return (EINVAL); 24164543Smarks } 24174543Smarks 24184543Smarks /* 24194543Smarks * If we don't have PRIV_SYS_MOUNT, then validate 24204543Smarks * that user is allowed to hand out each permission in 24214543Smarks * the nvlist(s) 24224543Smarks */ 24234543Smarks 24244787Sahrens error = secpolicy_zfs(CRED()); 24254543Smarks if (error) { 24264787Sahrens if (zc->zc_perm_action == B_FALSE) { 24274787Sahrens error = dsl_deleg_can_allow(zc->zc_name, 24284787Sahrens fsaclnv, CRED()); 24294787Sahrens } else { 24304787Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 24314787Sahrens fsaclnv, CRED()); 24324787Sahrens } 24334543Smarks } 24344543Smarks 24354543Smarks if (error == 0) 24364543Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 24374543Smarks 24384543Smarks nvlist_free(fsaclnv); 24394543Smarks return (error); 24404543Smarks } 24414543Smarks 24425367Sahrens /* 24435367Sahrens * inputs: 24445367Sahrens * zc_name name of filesystem 24455367Sahrens * 24465367Sahrens * outputs: 24475367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 24485367Sahrens */ 24494543Smarks static int 24504543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 24514543Smarks { 24524543Smarks nvlist_t *nvp; 24534543Smarks int error; 24544543Smarks 24554543Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 24564543Smarks error = put_nvlist(zc, nvp); 24574543Smarks nvlist_free(nvp); 24584543Smarks } 24594543Smarks 24604543Smarks return (error); 24614543Smarks } 24624543Smarks 24635367Sahrens /* 2464789Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 2465789Sahrens * or NULL if no suitable entry is found. The caller of this routine 2466789Sahrens * is responsible for releasing the returned vfs pointer. 2467789Sahrens */ 2468789Sahrens static vfs_t * 2469789Sahrens zfs_get_vfs(const char *resource) 2470789Sahrens { 2471789Sahrens struct vfs *vfsp; 2472789Sahrens struct vfs *vfs_found = NULL; 2473789Sahrens 2474789Sahrens vfs_list_read_lock(); 2475789Sahrens vfsp = rootvfs; 2476789Sahrens do { 2477789Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2478789Sahrens VFS_HOLD(vfsp); 2479789Sahrens vfs_found = vfsp; 2480789Sahrens break; 2481789Sahrens } 2482789Sahrens vfsp = vfsp->vfs_next; 2483789Sahrens } while (vfsp != rootvfs); 2484789Sahrens vfs_list_unlock(); 2485789Sahrens return (vfs_found); 2486789Sahrens } 2487789Sahrens 24884543Smarks /* ARGSUSED */ 2489789Sahrens static void 24904543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2491789Sahrens { 24925331Samw zfs_creat_t *zct = arg; 24935498Stimh 24945498Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 24955331Samw } 24965331Samw 24975498Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 24985498Stimh 24995331Samw /* 25005498Stimh * inputs: 25017184Stimh * createprops list of properties requested by creator 25027184Stimh * default_zplver zpl version to use if unspecified in createprops 25037184Stimh * fuids_ok fuids allowed in this version of the spa? 25047184Stimh * os parent objset pointer (NULL if root fs) 25055331Samw * 25065498Stimh * outputs: 25075498Stimh * zplprops values for the zplprops we attach to the master node object 25087184Stimh * is_ci true if requested file system will be purely case-insensitive 25095331Samw * 25105498Stimh * Determine the settings for utf8only, normalization and 25115498Stimh * casesensitivity. Specific values may have been requested by the 25125498Stimh * creator and/or we can inherit values from the parent dataset. If 25135498Stimh * the file system is of too early a vintage, a creator can not 25145498Stimh * request settings for these properties, even if the requested 25155498Stimh * setting is the default value. We don't actually want to create dsl 25165498Stimh * properties for these, so remove them from the source nvlist after 25175498Stimh * processing. 25185331Samw */ 25195331Samw static int 25209396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 25217184Stimh boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops, 25227184Stimh boolean_t *is_ci) 25235331Samw { 25245498Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 25255498Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 25265498Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 25275498Stimh 25285498Stimh ASSERT(zplprops != NULL); 25295498Stimh 25305375Stimh /* 25315498Stimh * Pull out creator prop choices, if any. 25325375Stimh */ 25335498Stimh if (createprops) { 25345498Stimh (void) nvlist_lookup_uint64(createprops, 25357184Stimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 25367184Stimh (void) nvlist_lookup_uint64(createprops, 25375498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 25385498Stimh (void) nvlist_remove_all(createprops, 25395498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 25405498Stimh (void) nvlist_lookup_uint64(createprops, 25415498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 25425498Stimh (void) nvlist_remove_all(createprops, 25435498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 25445498Stimh (void) nvlist_lookup_uint64(createprops, 25455498Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 25465498Stimh (void) nvlist_remove_all(createprops, 25475498Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 25485331Samw } 25495331Samw 25505375Stimh /* 25517184Stimh * If the zpl version requested is whacky or the file system 25527184Stimh * or pool is version is too "young" to support normalization 25537184Stimh * and the creator tried to set a value for one of the props, 25547184Stimh * error out. 25555498Stimh */ 25567184Stimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 25577184Stimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 25587184Stimh (zplver < ZPL_VERSION_NORMALIZATION && 25595498Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 25607184Stimh sense != ZFS_PROP_UNDEFINED))) 25615498Stimh return (ENOTSUP); 25625498Stimh 25635498Stimh /* 25645498Stimh * Put the version in the zplprops 25655498Stimh */ 25665498Stimh VERIFY(nvlist_add_uint64(zplprops, 25675498Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 25685498Stimh 25695498Stimh if (norm == ZFS_PROP_UNDEFINED) 25705498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 25715498Stimh VERIFY(nvlist_add_uint64(zplprops, 25725498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 25735498Stimh 25745498Stimh /* 25755498Stimh * If we're normalizing, names must always be valid UTF-8 strings. 25765498Stimh */ 25775498Stimh if (norm) 25785498Stimh u8 = 1; 25795498Stimh if (u8 == ZFS_PROP_UNDEFINED) 25805498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 25815498Stimh VERIFY(nvlist_add_uint64(zplprops, 25825498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 25835498Stimh 25845498Stimh if (sense == ZFS_PROP_UNDEFINED) 25855498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 25865498Stimh VERIFY(nvlist_add_uint64(zplprops, 25875498Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 25885498Stimh 25896492Stimh if (is_ci) 25906492Stimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 25916492Stimh 25927184Stimh return (0); 25937184Stimh } 25947184Stimh 25957184Stimh static int 25967184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 25977184Stimh nvlist_t *zplprops, boolean_t *is_ci) 25987184Stimh { 25997184Stimh boolean_t fuids_ok = B_TRUE; 26007184Stimh uint64_t zplver = ZPL_VERSION; 26017184Stimh objset_t *os = NULL; 26027184Stimh char parentname[MAXNAMELEN]; 26037184Stimh char *cp; 26047184Stimh int error; 26057184Stimh 26067184Stimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 26077184Stimh cp = strrchr(parentname, '/'); 26087184Stimh ASSERT(cp != NULL); 26097184Stimh cp[0] = '\0'; 26107184Stimh 26119396SMatthew.Ahrens@Sun.COM if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE)) 26129396SMatthew.Ahrens@Sun.COM zplver = ZPL_VERSION_USERSPACE - 1; 26137184Stimh if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) { 26147184Stimh zplver = ZPL_VERSION_FUID - 1; 26157184Stimh fuids_ok = B_FALSE; 26167184Stimh } 26177184Stimh 26187184Stimh /* 26197184Stimh * Open parent object set so we can inherit zplprop values. 26207184Stimh */ 262110298SMatthew.Ahrens@Sun.COM if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 26227184Stimh return (error); 26237184Stimh 26247184Stimh error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops, 26257184Stimh zplprops, is_ci); 262610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 26277184Stimh return (error); 26287184Stimh } 26297184Stimh 26307184Stimh static int 26317184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 26327184Stimh nvlist_t *zplprops, boolean_t *is_ci) 26337184Stimh { 26347184Stimh boolean_t fuids_ok = B_TRUE; 26357184Stimh uint64_t zplver = ZPL_VERSION; 26367184Stimh int error; 26377184Stimh 26387184Stimh if (spa_vers < SPA_VERSION_FUID) { 26397184Stimh zplver = ZPL_VERSION_FUID - 1; 26407184Stimh fuids_ok = B_FALSE; 26417184Stimh } 26427184Stimh 26437184Stimh error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops, 26447184Stimh zplprops, is_ci); 26457184Stimh return (error); 2646789Sahrens } 2647789Sahrens 26485367Sahrens /* 26495367Sahrens * inputs: 26505367Sahrens * zc_objset_type type of objset to create (fs vs zvol) 26515367Sahrens * zc_name name of new objset 26525367Sahrens * zc_value name of snapshot to clone from (may be empty) 26535367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 26545367Sahrens * 26555498Stimh * outputs: none 26565367Sahrens */ 2657789Sahrens static int 2658789Sahrens zfs_ioc_create(zfs_cmd_t *zc) 2659789Sahrens { 2660789Sahrens objset_t *clone; 2661789Sahrens int error = 0; 26625331Samw zfs_creat_t zct; 26634543Smarks nvlist_t *nvprops = NULL; 26644543Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2665789Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2666789Sahrens 2667789Sahrens switch (type) { 2668789Sahrens 2669789Sahrens case DMU_OST_ZFS: 2670789Sahrens cbfunc = zfs_create_cb; 2671789Sahrens break; 2672789Sahrens 2673789Sahrens case DMU_OST_ZVOL: 2674789Sahrens cbfunc = zvol_create_cb; 2675789Sahrens break; 2676789Sahrens 2677789Sahrens default: 26782199Sahrens cbfunc = NULL; 26796423Sgw25295 break; 26802199Sahrens } 26815326Sek110237 if (strchr(zc->zc_name, '@') || 26825326Sek110237 strchr(zc->zc_name, '%')) 2683789Sahrens return (EINVAL); 2684789Sahrens 26852676Seschrock if (zc->zc_nvlist_src != NULL && 26865094Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 26879643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 26882676Seschrock return (error); 26892676Seschrock 26905498Stimh zct.zct_zplprops = NULL; 26915331Samw zct.zct_props = nvprops; 26925331Samw 26932676Seschrock if (zc->zc_value[0] != '\0') { 2694789Sahrens /* 2695789Sahrens * We're creating a clone of an existing snapshot. 2696789Sahrens */ 26972676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 26982676Seschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 26994543Smarks nvlist_free(nvprops); 2700789Sahrens return (EINVAL); 27012676Seschrock } 2702789Sahrens 270310298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_value, FTAG, &clone); 27042676Seschrock if (error) { 27054543Smarks nvlist_free(nvprops); 2706789Sahrens return (error); 27072676Seschrock } 27086492Stimh 270910272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0); 271010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 27115331Samw if (error) { 27125331Samw nvlist_free(nvprops); 27135331Samw return (error); 27145331Samw } 2715789Sahrens } else { 27166492Stimh boolean_t is_insensitive = B_FALSE; 27176492Stimh 27182676Seschrock if (cbfunc == NULL) { 27194543Smarks nvlist_free(nvprops); 27202199Sahrens return (EINVAL); 27212676Seschrock } 27222676Seschrock 2723789Sahrens if (type == DMU_OST_ZVOL) { 27242676Seschrock uint64_t volsize, volblocksize; 27252676Seschrock 27264543Smarks if (nvprops == NULL || 27274543Smarks nvlist_lookup_uint64(nvprops, 27282676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 27292676Seschrock &volsize) != 0) { 27304543Smarks nvlist_free(nvprops); 27312676Seschrock return (EINVAL); 27322676Seschrock } 27332676Seschrock 27344543Smarks if ((error = nvlist_lookup_uint64(nvprops, 27352676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 27362676Seschrock &volblocksize)) != 0 && error != ENOENT) { 27374543Smarks nvlist_free(nvprops); 27382676Seschrock return (EINVAL); 27392676Seschrock } 27401133Seschrock 27412676Seschrock if (error != 0) 27422676Seschrock volblocksize = zfs_prop_default_numeric( 27432676Seschrock ZFS_PROP_VOLBLOCKSIZE); 27442676Seschrock 27452676Seschrock if ((error = zvol_check_volblocksize( 27462676Seschrock volblocksize)) != 0 || 27472676Seschrock (error = zvol_check_volsize(volsize, 27482676Seschrock volblocksize)) != 0) { 27494543Smarks nvlist_free(nvprops); 2750789Sahrens return (error); 27512676Seschrock } 27524577Sahrens } else if (type == DMU_OST_ZFS) { 27535331Samw int error; 27545331Samw 27555498Stimh /* 27565331Samw * We have to have normalization and 27575331Samw * case-folding flags correct when we do the 27585331Samw * file system creation, so go figure them out 27595498Stimh * now. 27605331Samw */ 27615498Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 27625498Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 27635498Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 27647184Stimh zct.zct_zplprops, &is_insensitive); 27655331Samw if (error != 0) { 27665331Samw nvlist_free(nvprops); 27675498Stimh nvlist_free(zct.zct_zplprops); 27685331Samw return (error); 27694577Sahrens } 27702676Seschrock } 277110272SMatthew.Ahrens@Sun.COM error = dmu_objset_create(zc->zc_name, type, 27726492Stimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 27735498Stimh nvlist_free(zct.zct_zplprops); 2774789Sahrens } 27752676Seschrock 27762676Seschrock /* 27772676Seschrock * It would be nice to do this atomically. 27782676Seschrock */ 27792676Seschrock if (error == 0) { 278011022STom.Erickson@Sun.COM error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL, 278111022STom.Erickson@Sun.COM nvprops, NULL); 278211022STom.Erickson@Sun.COM if (error != 0) 278310242Schris.kirby@sun.com (void) dmu_objset_destroy(zc->zc_name, B_FALSE); 27842676Seschrock } 27854543Smarks nvlist_free(nvprops); 2786789Sahrens return (error); 2787789Sahrens } 2788789Sahrens 27895367Sahrens /* 27905367Sahrens * inputs: 27915367Sahrens * zc_name name of filesystem 27925367Sahrens * zc_value short name of snapshot 27935367Sahrens * zc_cookie recursive flag 27949396SMatthew.Ahrens@Sun.COM * zc_nvlist_src[_size] property list 27955367Sahrens * 279610588SEric.Taylor@Sun.COM * outputs: 279710588SEric.Taylor@Sun.COM * zc_value short snapname (i.e. part after the '@') 27985367Sahrens */ 2799789Sahrens static int 28002199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 28012199Sahrens { 28027265Sahrens nvlist_t *nvprops = NULL; 28037265Sahrens int error; 28047265Sahrens boolean_t recursive = zc->zc_cookie; 28057265Sahrens 28062676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 28072199Sahrens return (EINVAL); 28087265Sahrens 28097265Sahrens if (zc->zc_nvlist_src != NULL && 28107265Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 28119643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 28127265Sahrens return (error); 28137265Sahrens 28149355SMatthew.Ahrens@Sun.COM error = zfs_check_userprops(zc->zc_name, nvprops); 28159355SMatthew.Ahrens@Sun.COM if (error) 28169355SMatthew.Ahrens@Sun.COM goto out; 28179355SMatthew.Ahrens@Sun.COM 281811022STom.Erickson@Sun.COM if (!nvlist_empty(nvprops) && 28199355SMatthew.Ahrens@Sun.COM zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) { 28209355SMatthew.Ahrens@Sun.COM error = ENOTSUP; 28219355SMatthew.Ahrens@Sun.COM goto out; 28227265Sahrens } 28239355SMatthew.Ahrens@Sun.COM 28249355SMatthew.Ahrens@Sun.COM error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, 28259355SMatthew.Ahrens@Sun.COM nvprops, recursive); 28269355SMatthew.Ahrens@Sun.COM 28279355SMatthew.Ahrens@Sun.COM out: 28287265Sahrens nvlist_free(nvprops); 28297265Sahrens return (error); 28302199Sahrens } 28312199Sahrens 28324007Smmusante int 28332199Sahrens zfs_unmount_snap(char *name, void *arg) 2834789Sahrens { 28352417Sahrens vfs_t *vfsp = NULL; 28362199Sahrens 28376689Smaybee if (arg) { 28386689Smaybee char *snapname = arg; 28396689Smaybee int len = strlen(name) + strlen(snapname) + 2; 28406689Smaybee char *buf = kmem_alloc(len, KM_SLEEP); 28416689Smaybee 28426689Smaybee (void) strcpy(buf, name); 28436689Smaybee (void) strcat(buf, "@"); 28446689Smaybee (void) strcat(buf, snapname); 28456689Smaybee vfsp = zfs_get_vfs(buf); 28466689Smaybee kmem_free(buf, len); 28472417Sahrens } else if (strchr(name, '@')) { 28482199Sahrens vfsp = zfs_get_vfs(name); 28492199Sahrens } 28502199Sahrens 28512199Sahrens if (vfsp) { 28522199Sahrens /* 28532199Sahrens * Always force the unmount for snapshots. 28542199Sahrens */ 28552199Sahrens int flag = MS_FORCE; 2856789Sahrens int err; 2857789Sahrens 28582199Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 28592199Sahrens VFS_RELE(vfsp); 28602199Sahrens return (err); 28612199Sahrens } 28622199Sahrens VFS_RELE(vfsp); 28632199Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 28642199Sahrens return (err); 28652199Sahrens } 28662199Sahrens return (0); 28672199Sahrens } 28682199Sahrens 28695367Sahrens /* 28705367Sahrens * inputs: 287110242Schris.kirby@sun.com * zc_name name of filesystem 287210242Schris.kirby@sun.com * zc_value short name of snapshot 287310242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 28745367Sahrens * 28755367Sahrens * outputs: none 28765367Sahrens */ 28772199Sahrens static int 28782199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 28792199Sahrens { 28802199Sahrens int err; 2881789Sahrens 28822676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 28832199Sahrens return (EINVAL); 28842199Sahrens err = dmu_objset_find(zc->zc_name, 28852676Seschrock zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 28862199Sahrens if (err) 28872199Sahrens return (err); 288810242Schris.kirby@sun.com return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value, 288910242Schris.kirby@sun.com zc->zc_defer_destroy)); 28902199Sahrens } 28912199Sahrens 28925367Sahrens /* 28935367Sahrens * inputs: 28945367Sahrens * zc_name name of dataset to destroy 28955367Sahrens * zc_objset_type type of objset 289610242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 28975367Sahrens * 28985367Sahrens * outputs: none 28995367Sahrens */ 29002199Sahrens static int 29012199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 29022199Sahrens { 290310588SEric.Taylor@Sun.COM int err; 29042199Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 290510588SEric.Taylor@Sun.COM err = zfs_unmount_snap(zc->zc_name, NULL); 29062199Sahrens if (err) 29072199Sahrens return (err); 2908789Sahrens } 2909789Sahrens 291010588SEric.Taylor@Sun.COM err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy); 291110588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 291210693Schris.kirby@sun.com (void) zvol_remove_minor(zc->zc_name); 291310588SEric.Taylor@Sun.COM return (err); 2914789Sahrens } 2915789Sahrens 29165367Sahrens /* 29175367Sahrens * inputs: 29185446Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 29195367Sahrens * 29205367Sahrens * outputs: none 29215367Sahrens */ 2922789Sahrens static int 2923789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 2924789Sahrens { 292510272SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds, *clone; 29265446Sahrens int error; 292710272SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 292810272SMatthew.Ahrens@Sun.COM char *clone_name; 292910272SMatthew.Ahrens@Sun.COM 293010272SMatthew.Ahrens@Sun.COM error = dsl_dataset_hold(zc->zc_name, FTAG, &ds); 293110272SMatthew.Ahrens@Sun.COM if (error) 293210272SMatthew.Ahrens@Sun.COM return (error); 293310272SMatthew.Ahrens@Sun.COM 293410272SMatthew.Ahrens@Sun.COM /* must not be a snapshot */ 293510272SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds)) { 293610272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 293710272SMatthew.Ahrens@Sun.COM return (EINVAL); 293810272SMatthew.Ahrens@Sun.COM } 293910272SMatthew.Ahrens@Sun.COM 294010272SMatthew.Ahrens@Sun.COM /* must have a most recent snapshot */ 294110272SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 294210272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 294310272SMatthew.Ahrens@Sun.COM return (EINVAL); 294410272SMatthew.Ahrens@Sun.COM } 29455446Sahrens 29465446Sahrens /* 294710272SMatthew.Ahrens@Sun.COM * Create clone of most recent snapshot. 29485446Sahrens */ 294910272SMatthew.Ahrens@Sun.COM clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name); 295010272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT); 29515446Sahrens if (error) 295210272SMatthew.Ahrens@Sun.COM goto out; 295310272SMatthew.Ahrens@Sun.COM 295410298SMatthew.Ahrens@Sun.COM error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone); 295510272SMatthew.Ahrens@Sun.COM if (error) 295610272SMatthew.Ahrens@Sun.COM goto out; 295710272SMatthew.Ahrens@Sun.COM 295810272SMatthew.Ahrens@Sun.COM /* 295910272SMatthew.Ahrens@Sun.COM * Do clone swap. 296010272SMatthew.Ahrens@Sun.COM */ 29619396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 296210298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 29636083Sek110237 if (error == 0) { 29646083Sek110237 int resume_err; 29656083Sek110237 296610272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 296710272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, 296810272SMatthew.Ahrens@Sun.COM B_TRUE); 296910272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 297010272SMatthew.Ahrens@Sun.COM ds = NULL; 297110272SMatthew.Ahrens@Sun.COM } else { 297210272SMatthew.Ahrens@Sun.COM error = EBUSY; 297310272SMatthew.Ahrens@Sun.COM } 297410298SMatthew.Ahrens@Sun.COM resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); 29756083Sek110237 error = error ? error : resume_err; 29766083Sek110237 } 29775446Sahrens VFS_RELE(zfsvfs->z_vfs); 29785446Sahrens } else { 297910272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 298010272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, B_TRUE); 298110272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 298210272SMatthew.Ahrens@Sun.COM ds = NULL; 298310272SMatthew.Ahrens@Sun.COM } else { 298410272SMatthew.Ahrens@Sun.COM error = EBUSY; 298510272SMatthew.Ahrens@Sun.COM } 29865446Sahrens } 298710272SMatthew.Ahrens@Sun.COM 298810272SMatthew.Ahrens@Sun.COM /* 298910272SMatthew.Ahrens@Sun.COM * Destroy clone (which also closes it). 299010272SMatthew.Ahrens@Sun.COM */ 299110272SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(clone, FTAG, B_FALSE); 299210272SMatthew.Ahrens@Sun.COM 299310272SMatthew.Ahrens@Sun.COM out: 299410272SMatthew.Ahrens@Sun.COM strfree(clone_name); 299510272SMatthew.Ahrens@Sun.COM if (ds) 299610272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 29975446Sahrens return (error); 2998789Sahrens } 2999789Sahrens 30005367Sahrens /* 30015367Sahrens * inputs: 30025367Sahrens * zc_name old name of dataset 30035367Sahrens * zc_value new name of dataset 30045367Sahrens * zc_cookie recursive flag (only valid for snapshots) 30055367Sahrens * 30065367Sahrens * outputs: none 30075367Sahrens */ 3008789Sahrens static int 3009789Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 3010789Sahrens { 30114490Svb160487 boolean_t recursive = zc->zc_cookie & 1; 30124007Smmusante 30132676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 30145326Sek110237 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 30155326Sek110237 strchr(zc->zc_value, '%')) 3016789Sahrens return (EINVAL); 3017789Sahrens 30184007Smmusante /* 30194007Smmusante * Unmount snapshot unless we're doing a recursive rename, 30204007Smmusante * in which case the dataset code figures out which snapshots 30214007Smmusante * to unmount. 30224007Smmusante */ 30234007Smmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 3024789Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 30252199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 30262199Sahrens if (err) 30272199Sahrens return (err); 3028789Sahrens } 302910588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL) 303010588SEric.Taylor@Sun.COM (void) zvol_remove_minor(zc->zc_name); 30314007Smmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 3032789Sahrens } 3033789Sahrens 303411022STom.Erickson@Sun.COM static int 303511022STom.Erickson@Sun.COM zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 303611022STom.Erickson@Sun.COM { 303711022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 303811022STom.Erickson@Sun.COM boolean_t issnap = (strchr(dsname, '@') != NULL); 303911022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(propname); 304011022STom.Erickson@Sun.COM uint64_t intval; 304111022STom.Erickson@Sun.COM int err; 304211022STom.Erickson@Sun.COM 304311022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 304411022STom.Erickson@Sun.COM if (zfs_prop_user(propname)) { 304511022STom.Erickson@Sun.COM if (err = zfs_secpolicy_write_perms(dsname, 304611022STom.Erickson@Sun.COM ZFS_DELEG_PERM_USERPROP, cr)) 304711022STom.Erickson@Sun.COM return (err); 304811022STom.Erickson@Sun.COM return (0); 304911022STom.Erickson@Sun.COM } 305011022STom.Erickson@Sun.COM 305111022STom.Erickson@Sun.COM if (!issnap && zfs_prop_userquota(propname)) { 305211022STom.Erickson@Sun.COM const char *perm = NULL; 305311022STom.Erickson@Sun.COM const char *uq_prefix = 305411022STom.Erickson@Sun.COM zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 305511022STom.Erickson@Sun.COM const char *gq_prefix = 305611022STom.Erickson@Sun.COM zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 305711022STom.Erickson@Sun.COM 305811022STom.Erickson@Sun.COM if (strncmp(propname, uq_prefix, 305911022STom.Erickson@Sun.COM strlen(uq_prefix)) == 0) { 306011022STom.Erickson@Sun.COM perm = ZFS_DELEG_PERM_USERQUOTA; 306111022STom.Erickson@Sun.COM } else if (strncmp(propname, gq_prefix, 306211022STom.Erickson@Sun.COM strlen(gq_prefix)) == 0) { 306311022STom.Erickson@Sun.COM perm = ZFS_DELEG_PERM_GROUPQUOTA; 306411022STom.Erickson@Sun.COM } else { 306511022STom.Erickson@Sun.COM /* USERUSED and GROUPUSED are read-only */ 306611022STom.Erickson@Sun.COM return (EINVAL); 306711022STom.Erickson@Sun.COM } 306811022STom.Erickson@Sun.COM 306911022STom.Erickson@Sun.COM if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 307011022STom.Erickson@Sun.COM return (err); 307111022STom.Erickson@Sun.COM return (0); 307211022STom.Erickson@Sun.COM } 307311022STom.Erickson@Sun.COM 307411022STom.Erickson@Sun.COM return (EINVAL); 307511022STom.Erickson@Sun.COM } 307611022STom.Erickson@Sun.COM 307711022STom.Erickson@Sun.COM if (issnap) 307811022STom.Erickson@Sun.COM return (EINVAL); 307911022STom.Erickson@Sun.COM 308011022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 308111022STom.Erickson@Sun.COM /* 308211022STom.Erickson@Sun.COM * dsl_prop_get_all_impl() returns properties in this 308311022STom.Erickson@Sun.COM * format. 308411022STom.Erickson@Sun.COM */ 308511022STom.Erickson@Sun.COM nvlist_t *attrs; 308611022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 308711022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 308811022STom.Erickson@Sun.COM &pair) == 0); 308911022STom.Erickson@Sun.COM } 309011022STom.Erickson@Sun.COM 309111022STom.Erickson@Sun.COM /* 309211022STom.Erickson@Sun.COM * Check that this value is valid for this pool version 309311022STom.Erickson@Sun.COM */ 309411022STom.Erickson@Sun.COM switch (prop) { 309511022STom.Erickson@Sun.COM case ZFS_PROP_COMPRESSION: 309611022STom.Erickson@Sun.COM /* 309711022STom.Erickson@Sun.COM * If the user specified gzip compression, make sure 309811022STom.Erickson@Sun.COM * the SPA supports it. We ignore any errors here since 309911022STom.Erickson@Sun.COM * we'll catch them later. 310011022STom.Erickson@Sun.COM */ 310111022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_UINT64 && 310211022STom.Erickson@Sun.COM nvpair_value_uint64(pair, &intval) == 0) { 310311022STom.Erickson@Sun.COM if (intval >= ZIO_COMPRESS_GZIP_1 && 310411022STom.Erickson@Sun.COM intval <= ZIO_COMPRESS_GZIP_9 && 310511022STom.Erickson@Sun.COM zfs_earlier_version(dsname, 310611022STom.Erickson@Sun.COM SPA_VERSION_GZIP_COMPRESSION)) { 310711022STom.Erickson@Sun.COM return (ENOTSUP); 310811022STom.Erickson@Sun.COM } 310911022STom.Erickson@Sun.COM 311011022STom.Erickson@Sun.COM if (intval == ZIO_COMPRESS_ZLE && 311111022STom.Erickson@Sun.COM zfs_earlier_version(dsname, 311211022STom.Erickson@Sun.COM SPA_VERSION_ZLE_COMPRESSION)) 311311022STom.Erickson@Sun.COM return (ENOTSUP); 311411022STom.Erickson@Sun.COM 311511022STom.Erickson@Sun.COM /* 311611022STom.Erickson@Sun.COM * If this is a bootable dataset then 311711022STom.Erickson@Sun.COM * verify that the compression algorithm 311811022STom.Erickson@Sun.COM * is supported for booting. We must return 311911022STom.Erickson@Sun.COM * something other than ENOTSUP since it 312011022STom.Erickson@Sun.COM * implies a downrev pool version. 312111022STom.Erickson@Sun.COM */ 312211022STom.Erickson@Sun.COM if (zfs_is_bootfs(dsname) && 312311022STom.Erickson@Sun.COM !BOOTFS_COMPRESS_VALID(intval)) { 312411022STom.Erickson@Sun.COM return (ERANGE); 312511022STom.Erickson@Sun.COM } 312611022STom.Erickson@Sun.COM } 312711022STom.Erickson@Sun.COM break; 312811022STom.Erickson@Sun.COM 312911022STom.Erickson@Sun.COM case ZFS_PROP_COPIES: 313011022STom.Erickson@Sun.COM if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 313111022STom.Erickson@Sun.COM return (ENOTSUP); 313211022STom.Erickson@Sun.COM break; 313311022STom.Erickson@Sun.COM 313411022STom.Erickson@Sun.COM case ZFS_PROP_DEDUP: 313511022STom.Erickson@Sun.COM if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 313611022STom.Erickson@Sun.COM return (ENOTSUP); 313711022STom.Erickson@Sun.COM break; 313811022STom.Erickson@Sun.COM 313911022STom.Erickson@Sun.COM case ZFS_PROP_SHARESMB: 314011022STom.Erickson@Sun.COM if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 314111022STom.Erickson@Sun.COM return (ENOTSUP); 314211022STom.Erickson@Sun.COM break; 314311022STom.Erickson@Sun.COM 314411022STom.Erickson@Sun.COM case ZFS_PROP_ACLINHERIT: 314511022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_UINT64 && 314611022STom.Erickson@Sun.COM nvpair_value_uint64(pair, &intval) == 0) { 314711022STom.Erickson@Sun.COM if (intval == ZFS_ACL_PASSTHROUGH_X && 314811022STom.Erickson@Sun.COM zfs_earlier_version(dsname, 314911022STom.Erickson@Sun.COM SPA_VERSION_PASSTHROUGH_X)) 315011022STom.Erickson@Sun.COM return (ENOTSUP); 315111022STom.Erickson@Sun.COM } 315211022STom.Erickson@Sun.COM break; 315311022STom.Erickson@Sun.COM } 315411022STom.Erickson@Sun.COM 315511022STom.Erickson@Sun.COM return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 315611022STom.Erickson@Sun.COM } 315711022STom.Erickson@Sun.COM 315811022STom.Erickson@Sun.COM /* 315911022STom.Erickson@Sun.COM * Removes properties from the given props list that fail permission checks 316011022STom.Erickson@Sun.COM * needed to clear them and to restore them in case of a receive error. For each 316111022STom.Erickson@Sun.COM * property, make sure we have both set and inherit permissions. 316211022STom.Erickson@Sun.COM * 316311022STom.Erickson@Sun.COM * Returns the first error encountered if any permission checks fail. If the 316411022STom.Erickson@Sun.COM * caller provides a non-NULL errlist, it also gives the complete list of names 316511022STom.Erickson@Sun.COM * of all the properties that failed a permission check along with the 316611022STom.Erickson@Sun.COM * corresponding error numbers. The caller is responsible for freeing the 316711022STom.Erickson@Sun.COM * returned errlist. 316811022STom.Erickson@Sun.COM * 316911022STom.Erickson@Sun.COM * If every property checks out successfully, zero is returned and the list 317011022STom.Erickson@Sun.COM * pointed at by errlist is NULL. 317111022STom.Erickson@Sun.COM */ 317211022STom.Erickson@Sun.COM static int 317311022STom.Erickson@Sun.COM zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 31746689Smaybee { 31756689Smaybee zfs_cmd_t *zc; 317611022STom.Erickson@Sun.COM nvpair_t *pair, *next_pair; 317711022STom.Erickson@Sun.COM nvlist_t *errors; 317811022STom.Erickson@Sun.COM int err, rv = 0; 31796689Smaybee 31806689Smaybee if (props == NULL) 318111022STom.Erickson@Sun.COM return (0); 318211022STom.Erickson@Sun.COM 318311022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 318411022STom.Erickson@Sun.COM 31856689Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 31866689Smaybee (void) strcpy(zc->zc_name, dataset); 318711022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(props, NULL); 318811022STom.Erickson@Sun.COM while (pair != NULL) { 318911022STom.Erickson@Sun.COM next_pair = nvlist_next_nvpair(props, pair); 319011022STom.Erickson@Sun.COM 319111022STom.Erickson@Sun.COM (void) strcpy(zc->zc_value, nvpair_name(pair)); 319211022STom.Erickson@Sun.COM if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 319311022STom.Erickson@Sun.COM (err = zfs_secpolicy_inherit(zc, CRED())) != 0) { 319411022STom.Erickson@Sun.COM VERIFY(nvlist_remove_nvpair(props, pair) == 0); 319511022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(errors, 319611022STom.Erickson@Sun.COM zc->zc_value, err) == 0); 319711022STom.Erickson@Sun.COM } 319811022STom.Erickson@Sun.COM pair = next_pair; 31996689Smaybee } 32006689Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 320111022STom.Erickson@Sun.COM 320211022STom.Erickson@Sun.COM if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 320311022STom.Erickson@Sun.COM nvlist_free(errors); 320411022STom.Erickson@Sun.COM errors = NULL; 320511022STom.Erickson@Sun.COM } else { 320611022STom.Erickson@Sun.COM VERIFY(nvpair_value_int32(pair, &rv) == 0); 320711022STom.Erickson@Sun.COM } 320811022STom.Erickson@Sun.COM 320911022STom.Erickson@Sun.COM if (errlist == NULL) 321011022STom.Erickson@Sun.COM nvlist_free(errors); 321111022STom.Erickson@Sun.COM else 321211022STom.Erickson@Sun.COM *errlist = errors; 321311022STom.Erickson@Sun.COM 321411022STom.Erickson@Sun.COM return (rv); 32156689Smaybee } 32166689Smaybee 321711022STom.Erickson@Sun.COM static boolean_t 321811022STom.Erickson@Sun.COM propval_equals(nvpair_t *p1, nvpair_t *p2) 321911022STom.Erickson@Sun.COM { 322011022STom.Erickson@Sun.COM if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 322111022STom.Erickson@Sun.COM /* dsl_prop_get_all_impl() format */ 322211022STom.Erickson@Sun.COM nvlist_t *attrs; 322311022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 322411022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 322511022STom.Erickson@Sun.COM &p1) == 0); 322611022STom.Erickson@Sun.COM } 322711022STom.Erickson@Sun.COM 322811022STom.Erickson@Sun.COM if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 322911022STom.Erickson@Sun.COM nvlist_t *attrs; 323011022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 323111022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 323211022STom.Erickson@Sun.COM &p2) == 0); 323311022STom.Erickson@Sun.COM } 323411022STom.Erickson@Sun.COM 323511022STom.Erickson@Sun.COM if (nvpair_type(p1) != nvpair_type(p2)) 323611022STom.Erickson@Sun.COM return (B_FALSE); 323711022STom.Erickson@Sun.COM 323811022STom.Erickson@Sun.COM if (nvpair_type(p1) == DATA_TYPE_STRING) { 323911022STom.Erickson@Sun.COM char *valstr1, *valstr2; 324011022STom.Erickson@Sun.COM 324111022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 324211022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 324311022STom.Erickson@Sun.COM return (strcmp(valstr1, valstr2) == 0); 324411022STom.Erickson@Sun.COM } else { 324511022STom.Erickson@Sun.COM uint64_t intval1, intval2; 324611022STom.Erickson@Sun.COM 324711022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 324811022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 324911022STom.Erickson@Sun.COM return (intval1 == intval2); 325011022STom.Erickson@Sun.COM } 325111022STom.Erickson@Sun.COM } 325211022STom.Erickson@Sun.COM 325311022STom.Erickson@Sun.COM /* 325411022STom.Erickson@Sun.COM * Remove properties from props if they are not going to change (as determined 325511022STom.Erickson@Sun.COM * by comparison with origprops). Remove them from origprops as well, since we 325611022STom.Erickson@Sun.COM * do not need to clear or restore properties that won't change. 325711022STom.Erickson@Sun.COM */ 325811022STom.Erickson@Sun.COM static void 325911022STom.Erickson@Sun.COM props_reduce(nvlist_t *props, nvlist_t *origprops) 326011022STom.Erickson@Sun.COM { 326111022STom.Erickson@Sun.COM nvpair_t *pair, *next_pair; 326211022STom.Erickson@Sun.COM 326311022STom.Erickson@Sun.COM if (origprops == NULL) 326411022STom.Erickson@Sun.COM return; /* all props need to be received */ 326511022STom.Erickson@Sun.COM 326611022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(props, NULL); 326711022STom.Erickson@Sun.COM while (pair != NULL) { 326811022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 326911022STom.Erickson@Sun.COM nvpair_t *match; 327011022STom.Erickson@Sun.COM 327111022STom.Erickson@Sun.COM next_pair = nvlist_next_nvpair(props, pair); 327211022STom.Erickson@Sun.COM 327311022STom.Erickson@Sun.COM if ((nvlist_lookup_nvpair(origprops, propname, 327411022STom.Erickson@Sun.COM &match) != 0) || !propval_equals(pair, match)) 327511022STom.Erickson@Sun.COM goto next; /* need to set received value */ 327611022STom.Erickson@Sun.COM 327711022STom.Erickson@Sun.COM /* don't clear the existing received value */ 327811022STom.Erickson@Sun.COM (void) nvlist_remove_nvpair(origprops, match); 327911022STom.Erickson@Sun.COM /* don't bother receiving the property */ 328011022STom.Erickson@Sun.COM (void) nvlist_remove_nvpair(props, pair); 328111022STom.Erickson@Sun.COM next: 328211022STom.Erickson@Sun.COM pair = next_pair; 328311022STom.Erickson@Sun.COM } 328411022STom.Erickson@Sun.COM } 328511022STom.Erickson@Sun.COM 328611022STom.Erickson@Sun.COM #ifdef DEBUG 328711022STom.Erickson@Sun.COM static boolean_t zfs_ioc_recv_inject_err; 328811022STom.Erickson@Sun.COM #endif 328911022STom.Erickson@Sun.COM 32905367Sahrens /* 32915367Sahrens * inputs: 32925367Sahrens * zc_name name of containing filesystem 32935367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 32945367Sahrens * zc_value name of snapshot to create 32955367Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 32965367Sahrens * zc_cookie file descriptor to recv from 32975367Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 32985367Sahrens * zc_guid force flag 32995367Sahrens * 33005367Sahrens * outputs: 33015367Sahrens * zc_cookie number of bytes read 330211022STom.Erickson@Sun.COM * zc_nvlist_dst{_size} error for each unapplied received property 330311022STom.Erickson@Sun.COM * zc_obj zprop_errflags_t 33045367Sahrens */ 3305789Sahrens static int 33065367Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 3307789Sahrens { 3308789Sahrens file_t *fp; 33095326Sek110237 objset_t *os; 33105367Sahrens dmu_recv_cookie_t drc; 33115326Sek110237 boolean_t force = (boolean_t)zc->zc_guid; 331211022STom.Erickson@Sun.COM int fd; 331311022STom.Erickson@Sun.COM int error = 0; 331411022STom.Erickson@Sun.COM int props_error = 0; 331511022STom.Erickson@Sun.COM nvlist_t *errors; 33165367Sahrens offset_t off; 331711022STom.Erickson@Sun.COM nvlist_t *props = NULL; /* sent properties */ 331811022STom.Erickson@Sun.COM nvlist_t *origprops = NULL; /* existing properties */ 33195367Sahrens objset_t *origin = NULL; 33205367Sahrens char *tosnap; 33215367Sahrens char tofs[ZFS_MAXNAMELEN]; 332211022STom.Erickson@Sun.COM boolean_t first_recvd_props = B_FALSE; 3323789Sahrens 33243265Sahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 33255326Sek110237 strchr(zc->zc_value, '@') == NULL || 33265326Sek110237 strchr(zc->zc_value, '%')) 33273265Sahrens return (EINVAL); 33283265Sahrens 33295367Sahrens (void) strcpy(tofs, zc->zc_value); 33305367Sahrens tosnap = strchr(tofs, '@'); 333111022STom.Erickson@Sun.COM *tosnap++ = '\0'; 33325367Sahrens 33335367Sahrens if (zc->zc_nvlist_src != NULL && 33345367Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 33359643SEric.Taylor@Sun.COM zc->zc_iflags, &props)) != 0) 33365367Sahrens return (error); 33375367Sahrens 3338789Sahrens fd = zc->zc_cookie; 3339789Sahrens fp = getf(fd); 33405367Sahrens if (fp == NULL) { 33415367Sahrens nvlist_free(props); 3342789Sahrens return (EBADF); 33435367Sahrens } 33445326Sek110237 334511022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 334611022STom.Erickson@Sun.COM 334710298SMatthew.Ahrens@Sun.COM if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) { 334811022STom.Erickson@Sun.COM if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) && 334911022STom.Erickson@Sun.COM !dsl_prop_get_hasrecvd(os)) { 335011022STom.Erickson@Sun.COM first_recvd_props = B_TRUE; 335111022STom.Erickson@Sun.COM } 335211022STom.Erickson@Sun.COM 33536689Smaybee /* 335411022STom.Erickson@Sun.COM * If new received properties are supplied, they are to 335511022STom.Erickson@Sun.COM * completely replace the existing received properties, so stash 335611022STom.Erickson@Sun.COM * away the existing ones. 33576689Smaybee */ 335811022STom.Erickson@Sun.COM if (dsl_prop_get_received(os, &origprops) == 0) { 335911022STom.Erickson@Sun.COM nvlist_t *errlist = NULL; 336011022STom.Erickson@Sun.COM /* 336111022STom.Erickson@Sun.COM * Don't bother writing a property if its value won't 336211022STom.Erickson@Sun.COM * change (and avoid the unnecessary security checks). 336311022STom.Erickson@Sun.COM * 336411022STom.Erickson@Sun.COM * The first receive after SPA_VERSION_RECVD_PROPS is a 336511022STom.Erickson@Sun.COM * special case where we blow away all local properties 336611022STom.Erickson@Sun.COM * regardless. 336711022STom.Erickson@Sun.COM */ 336811022STom.Erickson@Sun.COM if (!first_recvd_props) 336911022STom.Erickson@Sun.COM props_reduce(props, origprops); 337011022STom.Erickson@Sun.COM if (zfs_check_clearable(tofs, origprops, 337111022STom.Erickson@Sun.COM &errlist) != 0) 337211022STom.Erickson@Sun.COM (void) nvlist_merge(errors, errlist, 0); 337311022STom.Erickson@Sun.COM nvlist_free(errlist); 337411022STom.Erickson@Sun.COM } 337510298SMatthew.Ahrens@Sun.COM 337610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 33775326Sek110237 } 33785326Sek110237 33795367Sahrens if (zc->zc_string[0]) { 338010298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_string, FTAG, &origin); 33816689Smaybee if (error) 33826689Smaybee goto out; 33835367Sahrens } 33845367Sahrens 338511007SLori.Alt@Sun.COM error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds, 338611007SLori.Alt@Sun.COM &zc->zc_begin_record, force, origin, &drc); 33875367Sahrens if (origin) 338810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(origin, FTAG); 33896689Smaybee if (error) 33906689Smaybee goto out; 33915326Sek110237 33925326Sek110237 /* 339311022STom.Erickson@Sun.COM * Set properties before we receive the stream so that they are applied 339411022STom.Erickson@Sun.COM * to the new data. Note that we must call dmu_recv_stream() if 339511022STom.Erickson@Sun.COM * dmu_recv_begin() succeeds. 33965326Sek110237 */ 33975367Sahrens if (props) { 339811022STom.Erickson@Sun.COM nvlist_t *errlist; 339911022STom.Erickson@Sun.COM 340011022STom.Erickson@Sun.COM if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) { 340111022STom.Erickson@Sun.COM if (drc.drc_newfs) { 340211022STom.Erickson@Sun.COM if (spa_version(os->os_spa) >= 340311022STom.Erickson@Sun.COM SPA_VERSION_RECVD_PROPS) 340411022STom.Erickson@Sun.COM first_recvd_props = B_TRUE; 340511022STom.Erickson@Sun.COM } else if (origprops != NULL) { 340611022STom.Erickson@Sun.COM if (clear_received_props(os, tofs, origprops, 340711022STom.Erickson@Sun.COM first_recvd_props ? NULL : props) != 0) 340811022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NOCLEAR; 340911022STom.Erickson@Sun.COM } else { 341011022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NOCLEAR; 341111022STom.Erickson@Sun.COM } 341211022STom.Erickson@Sun.COM dsl_prop_set_hasrecvd(os); 341311022STom.Erickson@Sun.COM } else if (!drc.drc_newfs) { 341411022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NOCLEAR; 341511022STom.Erickson@Sun.COM } 341611022STom.Erickson@Sun.COM 341711022STom.Erickson@Sun.COM (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 341811022STom.Erickson@Sun.COM props, &errlist); 341911022STom.Erickson@Sun.COM (void) nvlist_merge(errors, errlist, 0); 342011022STom.Erickson@Sun.COM nvlist_free(errlist); 342111022STom.Erickson@Sun.COM } 342211022STom.Erickson@Sun.COM 342311022STom.Erickson@Sun.COM if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) { 34246689Smaybee /* 342511022STom.Erickson@Sun.COM * Caller made zc->zc_nvlist_dst less than the minimum expected 342611022STom.Erickson@Sun.COM * size or supplied an invalid address. 34276689Smaybee */ 342811022STom.Erickson@Sun.COM props_error = EINVAL; 34295367Sahrens } 34305367Sahrens 34315367Sahrens off = fp->f_offset; 34325367Sahrens error = dmu_recv_stream(&drc, fp->f_vnode, &off); 34335367Sahrens 343410204SMatthew.Ahrens@Sun.COM if (error == 0) { 343510204SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs = NULL; 343610204SMatthew.Ahrens@Sun.COM 343710204SMatthew.Ahrens@Sun.COM if (getzfsvfs(tofs, &zfsvfs) == 0) { 343810204SMatthew.Ahrens@Sun.COM /* online recv */ 343910204SMatthew.Ahrens@Sun.COM int end_err; 344010298SMatthew.Ahrens@Sun.COM 344110298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 344210204SMatthew.Ahrens@Sun.COM /* 344310204SMatthew.Ahrens@Sun.COM * If the suspend fails, then the recv_end will 344410204SMatthew.Ahrens@Sun.COM * likely also fail, and clean up after itself. 344510204SMatthew.Ahrens@Sun.COM */ 344610204SMatthew.Ahrens@Sun.COM end_err = dmu_recv_end(&drc); 344710204SMatthew.Ahrens@Sun.COM if (error == 0) { 344810204SMatthew.Ahrens@Sun.COM int resume_err = 344910298SMatthew.Ahrens@Sun.COM zfs_resume_fs(zfsvfs, tofs); 345010204SMatthew.Ahrens@Sun.COM error = error ? error : resume_err; 345110204SMatthew.Ahrens@Sun.COM } 345210204SMatthew.Ahrens@Sun.COM error = error ? error : end_err; 345310204SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 345410204SMatthew.Ahrens@Sun.COM } else { 34556689Smaybee error = dmu_recv_end(&drc); 34565367Sahrens } 34576083Sek110237 } 34585367Sahrens 34595367Sahrens zc->zc_cookie = off - fp->f_offset; 34605367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 34615367Sahrens fp->f_offset = off; 34622885Sahrens 346311022STom.Erickson@Sun.COM #ifdef DEBUG 346411022STom.Erickson@Sun.COM if (zfs_ioc_recv_inject_err) { 346511022STom.Erickson@Sun.COM zfs_ioc_recv_inject_err = B_FALSE; 346611022STom.Erickson@Sun.COM error = 1; 346711022STom.Erickson@Sun.COM } 346811022STom.Erickson@Sun.COM #endif 34696689Smaybee /* 34706689Smaybee * On error, restore the original props. 34716689Smaybee */ 34726689Smaybee if (error && props) { 347311022STom.Erickson@Sun.COM if (dmu_objset_hold(tofs, FTAG, &os) == 0) { 347411022STom.Erickson@Sun.COM if (clear_received_props(os, tofs, props, NULL) != 0) { 347511022STom.Erickson@Sun.COM /* 347611022STom.Erickson@Sun.COM * We failed to clear the received properties. 347711022STom.Erickson@Sun.COM * Since we may have left a $recvd value on the 347811022STom.Erickson@Sun.COM * system, we can't clear the $hasrecvd flag. 347911022STom.Erickson@Sun.COM */ 348011022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 348111022STom.Erickson@Sun.COM } else if (first_recvd_props) { 348211022STom.Erickson@Sun.COM dsl_prop_unset_hasrecvd(os); 348311022STom.Erickson@Sun.COM } 348411022STom.Erickson@Sun.COM dmu_objset_rele(os, FTAG); 348511022STom.Erickson@Sun.COM } else if (!drc.drc_newfs) { 348611022STom.Erickson@Sun.COM /* We failed to clear the received properties. */ 348711022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 348811022STom.Erickson@Sun.COM } 348911022STom.Erickson@Sun.COM 349011022STom.Erickson@Sun.COM if (origprops == NULL && !drc.drc_newfs) { 349111022STom.Erickson@Sun.COM /* We failed to stash the original properties. */ 349211022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 349311022STom.Erickson@Sun.COM } 349411022STom.Erickson@Sun.COM 349511022STom.Erickson@Sun.COM /* 349611022STom.Erickson@Sun.COM * dsl_props_set() will not convert RECEIVED to LOCAL on or 349711022STom.Erickson@Sun.COM * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 349811022STom.Erickson@Sun.COM * explictly if we're restoring local properties cleared in the 349911022STom.Erickson@Sun.COM * first new-style receive. 350011022STom.Erickson@Sun.COM */ 350111022STom.Erickson@Sun.COM if (origprops != NULL && 350211022STom.Erickson@Sun.COM zfs_set_prop_nvlist(tofs, (first_recvd_props ? 350311022STom.Erickson@Sun.COM ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 350411022STom.Erickson@Sun.COM origprops, NULL) != 0) { 350511022STom.Erickson@Sun.COM /* 350611022STom.Erickson@Sun.COM * We stashed the original properties but failed to 350711022STom.Erickson@Sun.COM * restore them. 350811022STom.Erickson@Sun.COM */ 350911022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 351011022STom.Erickson@Sun.COM } 35116689Smaybee } 35126689Smaybee out: 35136689Smaybee nvlist_free(props); 35146689Smaybee nvlist_free(origprops); 351511022STom.Erickson@Sun.COM nvlist_free(errors); 3516789Sahrens releasef(fd); 351711022STom.Erickson@Sun.COM 351811022STom.Erickson@Sun.COM if (error == 0) 351911022STom.Erickson@Sun.COM error = props_error; 352011022STom.Erickson@Sun.COM 3521789Sahrens return (error); 3522789Sahrens } 3523789Sahrens 35245367Sahrens /* 35255367Sahrens * inputs: 35265367Sahrens * zc_name name of snapshot to send 35275367Sahrens * zc_value short name of incremental fromsnap (may be empty) 35285367Sahrens * zc_cookie file descriptor to send stream to 35295367Sahrens * zc_obj fromorigin flag (mutually exclusive with zc_value) 35305367Sahrens * 35315367Sahrens * outputs: none 35325367Sahrens */ 3533789Sahrens static int 35345367Sahrens zfs_ioc_send(zfs_cmd_t *zc) 3535789Sahrens { 3536789Sahrens objset_t *fromsnap = NULL; 3537789Sahrens objset_t *tosnap; 3538789Sahrens file_t *fp; 3539789Sahrens int error; 35405367Sahrens offset_t off; 3541789Sahrens 354210298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap); 3543789Sahrens if (error) 3544789Sahrens return (error); 3545789Sahrens 35462676Seschrock if (zc->zc_value[0] != '\0') { 35478012SEric.Taylor@Sun.COM char *buf; 35482885Sahrens char *cp; 35492885Sahrens 35508012SEric.Taylor@Sun.COM buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 35518012SEric.Taylor@Sun.COM (void) strncpy(buf, zc->zc_name, MAXPATHLEN); 35522885Sahrens cp = strchr(buf, '@'); 35532885Sahrens if (cp) 35542885Sahrens *(cp+1) = 0; 35558012SEric.Taylor@Sun.COM (void) strncat(buf, zc->zc_value, MAXPATHLEN); 355610298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(buf, FTAG, &fromsnap); 35578012SEric.Taylor@Sun.COM kmem_free(buf, MAXPATHLEN); 3558789Sahrens if (error) { 355910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 3560789Sahrens return (error); 3561789Sahrens } 3562789Sahrens } 3563789Sahrens 3564789Sahrens fp = getf(zc->zc_cookie); 3565789Sahrens if (fp == NULL) { 356610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 3567789Sahrens if (fromsnap) 356810298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 3569789Sahrens return (EBADF); 3570789Sahrens } 3571789Sahrens 35725367Sahrens off = fp->f_offset; 35735367Sahrens error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 35745367Sahrens 35755367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 35765367Sahrens fp->f_offset = off; 3577789Sahrens releasef(zc->zc_cookie); 3578789Sahrens if (fromsnap) 357910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 358010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 3581789Sahrens return (error); 3582789Sahrens } 3583789Sahrens 35841544Seschrock static int 35851544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 35861544Seschrock { 35871544Seschrock int id, error; 35881544Seschrock 35891544Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 35901544Seschrock &zc->zc_inject_record); 35911544Seschrock 35921544Seschrock if (error == 0) 35931544Seschrock zc->zc_guid = (uint64_t)id; 35941544Seschrock 35951544Seschrock return (error); 35961544Seschrock } 35971544Seschrock 35981544Seschrock static int 35991544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 36001544Seschrock { 36011544Seschrock return (zio_clear_fault((int)zc->zc_guid)); 36021544Seschrock } 36031544Seschrock 36041544Seschrock static int 36051544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 36061544Seschrock { 36071544Seschrock int id = (int)zc->zc_guid; 36081544Seschrock int error; 36091544Seschrock 36101544Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 36111544Seschrock &zc->zc_inject_record); 36121544Seschrock 36131544Seschrock zc->zc_guid = id; 36141544Seschrock 36151544Seschrock return (error); 36161544Seschrock } 36171544Seschrock 36181544Seschrock static int 36191544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 36201544Seschrock { 36211544Seschrock spa_t *spa; 36221544Seschrock int error; 36232676Seschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 36241544Seschrock 36251544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 36261544Seschrock return (error); 36271544Seschrock 36282676Seschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 36291544Seschrock &count); 36301544Seschrock if (error == 0) 36312676Seschrock zc->zc_nvlist_dst_size = count; 36321544Seschrock else 36332676Seschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 36341544Seschrock 36351544Seschrock spa_close(spa, FTAG); 36361544Seschrock 36371544Seschrock return (error); 36381544Seschrock } 36391544Seschrock 36401544Seschrock static int 36411544Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 36421544Seschrock { 36431544Seschrock spa_t *spa; 36441544Seschrock vdev_t *vd; 36451544Seschrock int error; 36461544Seschrock 36477294Sperrin /* 36487294Sperrin * On zpool clear we also fix up missing slogs 36497294Sperrin */ 36507294Sperrin mutex_enter(&spa_namespace_lock); 36517294Sperrin spa = spa_lookup(zc->zc_name); 36527294Sperrin if (spa == NULL) { 36537294Sperrin mutex_exit(&spa_namespace_lock); 36547294Sperrin return (EIO); 36557294Sperrin } 365610922SJeff.Bonwick@Sun.COM if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 36577294Sperrin /* we need to let spa_open/spa_load clear the chains */ 365810922SJeff.Bonwick@Sun.COM spa_set_log_state(spa, SPA_LOG_CLEAR); 36597294Sperrin } 366010921STim.Haley@Sun.COM spa->spa_last_open_failed = 0; 36617294Sperrin mutex_exit(&spa_namespace_lock); 36627294Sperrin 366310921STim.Haley@Sun.COM if (zc->zc_cookie == ZPOOL_NO_REWIND) { 366410921STim.Haley@Sun.COM error = spa_open(zc->zc_name, &spa, FTAG); 366510921STim.Haley@Sun.COM } else { 366610921STim.Haley@Sun.COM nvlist_t *policy; 366710921STim.Haley@Sun.COM nvlist_t *config = NULL; 366810921STim.Haley@Sun.COM 366910921STim.Haley@Sun.COM if (zc->zc_nvlist_src == NULL) 367010921STim.Haley@Sun.COM return (EINVAL); 367110921STim.Haley@Sun.COM 367210921STim.Haley@Sun.COM if ((error = get_nvlist(zc->zc_nvlist_src, 367310921STim.Haley@Sun.COM zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 367410921STim.Haley@Sun.COM error = spa_open_rewind(zc->zc_name, &spa, FTAG, 367510921STim.Haley@Sun.COM policy, &config); 367610921STim.Haley@Sun.COM if (config != NULL) { 367710921STim.Haley@Sun.COM (void) put_nvlist(zc, config); 367810921STim.Haley@Sun.COM nvlist_free(config); 367910921STim.Haley@Sun.COM } 368010921STim.Haley@Sun.COM nvlist_free(policy); 368110921STim.Haley@Sun.COM } 368210921STim.Haley@Sun.COM } 368310921STim.Haley@Sun.COM 368410921STim.Haley@Sun.COM if (error) 36851544Seschrock return (error); 36861544Seschrock 368710685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 36881544Seschrock 36892676Seschrock if (zc->zc_guid == 0) { 36901544Seschrock vd = NULL; 36916643Seschrock } else { 36926643Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 36935450Sbrendan if (vd == NULL) { 36947754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, ENODEV); 36955450Sbrendan spa_close(spa, FTAG); 36965450Sbrendan return (ENODEV); 36975450Sbrendan } 36981544Seschrock } 36991544Seschrock 37007754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 37017754SJeff.Bonwick@Sun.COM 37027754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 37037754SJeff.Bonwick@Sun.COM 37047754SJeff.Bonwick@Sun.COM /* 37057754SJeff.Bonwick@Sun.COM * Resume any suspended I/Os. 37067754SJeff.Bonwick@Sun.COM */ 37079234SGeorge.Wilson@Sun.COM if (zio_resume(spa) != 0) 37089234SGeorge.Wilson@Sun.COM error = EIO; 37091544Seschrock 37101544Seschrock spa_close(spa, FTAG); 37111544Seschrock 37129234SGeorge.Wilson@Sun.COM return (error); 37131544Seschrock } 37141544Seschrock 37155367Sahrens /* 37165367Sahrens * inputs: 37175367Sahrens * zc_name name of filesystem 37185367Sahrens * zc_value name of origin snapshot 37195367Sahrens * 372010588SEric.Taylor@Sun.COM * outputs: 372110588SEric.Taylor@Sun.COM * zc_string name of conflicting snapshot, if there is one 37225367Sahrens */ 37231544Seschrock static int 37242082Seschrock zfs_ioc_promote(zfs_cmd_t *zc) 37252082Seschrock { 37262417Sahrens char *cp; 37272417Sahrens 37282417Sahrens /* 37292417Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 37302417Sahrens * it's easier. 37312417Sahrens */ 37322676Seschrock cp = strchr(zc->zc_value, '@'); 37332417Sahrens if (cp) 37342417Sahrens *cp = '\0'; 37352676Seschrock (void) dmu_objset_find(zc->zc_value, 37362417Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 373710588SEric.Taylor@Sun.COM return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 37382082Seschrock } 37392082Seschrock 37404543Smarks /* 37419396SMatthew.Ahrens@Sun.COM * Retrieve a single {user|group}{used|quota}@... property. 37429396SMatthew.Ahrens@Sun.COM * 37439396SMatthew.Ahrens@Sun.COM * inputs: 37449396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 37459396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 37469396SMatthew.Ahrens@Sun.COM * zc_value domain name (eg. "S-1-234-567-89") 37479396SMatthew.Ahrens@Sun.COM * zc_guid RID/UID/GID 37489396SMatthew.Ahrens@Sun.COM * 37499396SMatthew.Ahrens@Sun.COM * outputs: 37509396SMatthew.Ahrens@Sun.COM * zc_cookie property value 37519396SMatthew.Ahrens@Sun.COM */ 37529396SMatthew.Ahrens@Sun.COM static int 37539396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc) 37549396SMatthew.Ahrens@Sun.COM { 37559396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 37569396SMatthew.Ahrens@Sun.COM int error; 37579396SMatthew.Ahrens@Sun.COM 37589396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 37599396SMatthew.Ahrens@Sun.COM return (EINVAL); 37609396SMatthew.Ahrens@Sun.COM 376110298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 37629396SMatthew.Ahrens@Sun.COM if (error) 37639396SMatthew.Ahrens@Sun.COM return (error); 37649396SMatthew.Ahrens@Sun.COM 37659396SMatthew.Ahrens@Sun.COM error = zfs_userspace_one(zfsvfs, 37669396SMatthew.Ahrens@Sun.COM zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 37679396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 37689396SMatthew.Ahrens@Sun.COM 37699396SMatthew.Ahrens@Sun.COM return (error); 37709396SMatthew.Ahrens@Sun.COM } 37719396SMatthew.Ahrens@Sun.COM 37729396SMatthew.Ahrens@Sun.COM /* 37739396SMatthew.Ahrens@Sun.COM * inputs: 37749396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 37759396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 37769396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 37779396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 37789396SMatthew.Ahrens@Sun.COM * 37799396SMatthew.Ahrens@Sun.COM * outputs: 37809396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 37819396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 37829396SMatthew.Ahrens@Sun.COM */ 37839396SMatthew.Ahrens@Sun.COM static int 37849396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc) 37859396SMatthew.Ahrens@Sun.COM { 37869396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 37879396SMatthew.Ahrens@Sun.COM int error; 37889396SMatthew.Ahrens@Sun.COM 378910298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 37909396SMatthew.Ahrens@Sun.COM if (error) 37919396SMatthew.Ahrens@Sun.COM return (error); 37929396SMatthew.Ahrens@Sun.COM 37939396SMatthew.Ahrens@Sun.COM int bufsize = zc->zc_nvlist_dst_size; 37949396SMatthew.Ahrens@Sun.COM void *buf = kmem_alloc(bufsize, KM_SLEEP); 37959396SMatthew.Ahrens@Sun.COM 37969396SMatthew.Ahrens@Sun.COM error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 37979396SMatthew.Ahrens@Sun.COM buf, &zc->zc_nvlist_dst_size); 37989396SMatthew.Ahrens@Sun.COM 37999396SMatthew.Ahrens@Sun.COM if (error == 0) { 38009396SMatthew.Ahrens@Sun.COM error = xcopyout(buf, 38019396SMatthew.Ahrens@Sun.COM (void *)(uintptr_t)zc->zc_nvlist_dst, 38029396SMatthew.Ahrens@Sun.COM zc->zc_nvlist_dst_size); 38039396SMatthew.Ahrens@Sun.COM } 38049396SMatthew.Ahrens@Sun.COM kmem_free(buf, bufsize); 38059396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 38069396SMatthew.Ahrens@Sun.COM 38079396SMatthew.Ahrens@Sun.COM return (error); 38089396SMatthew.Ahrens@Sun.COM } 38099396SMatthew.Ahrens@Sun.COM 38109396SMatthew.Ahrens@Sun.COM /* 38119396SMatthew.Ahrens@Sun.COM * inputs: 38129396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 38139396SMatthew.Ahrens@Sun.COM * 38149396SMatthew.Ahrens@Sun.COM * outputs: 38159396SMatthew.Ahrens@Sun.COM * none 38169396SMatthew.Ahrens@Sun.COM */ 38179396SMatthew.Ahrens@Sun.COM static int 38189396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 38199396SMatthew.Ahrens@Sun.COM { 38209396SMatthew.Ahrens@Sun.COM objset_t *os; 38219396SMatthew.Ahrens@Sun.COM int error; 38229396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 38239396SMatthew.Ahrens@Sun.COM 38249396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 382510298SMatthew.Ahrens@Sun.COM if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 38269396SMatthew.Ahrens@Sun.COM /* 38279396SMatthew.Ahrens@Sun.COM * If userused is not enabled, it may be because the 38289396SMatthew.Ahrens@Sun.COM * objset needs to be closed & reopened (to grow the 38299396SMatthew.Ahrens@Sun.COM * objset_phys_t). Suspend/resume the fs will do that. 38309396SMatthew.Ahrens@Sun.COM */ 383110298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 383210298SMatthew.Ahrens@Sun.COM if (error == 0) 383310298SMatthew.Ahrens@Sun.COM error = zfs_resume_fs(zfsvfs, zc->zc_name); 38349396SMatthew.Ahrens@Sun.COM } 38359396SMatthew.Ahrens@Sun.COM if (error == 0) 38369396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 38379396SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 38389396SMatthew.Ahrens@Sun.COM } else { 383910298SMatthew.Ahrens@Sun.COM /* XXX kind of reading contents without owning */ 384010298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 38419396SMatthew.Ahrens@Sun.COM if (error) 38429396SMatthew.Ahrens@Sun.COM return (error); 38439396SMatthew.Ahrens@Sun.COM 38449396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(os); 384510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 38469396SMatthew.Ahrens@Sun.COM } 38479396SMatthew.Ahrens@Sun.COM 38489396SMatthew.Ahrens@Sun.COM return (error); 38499396SMatthew.Ahrens@Sun.COM } 38509396SMatthew.Ahrens@Sun.COM 38519396SMatthew.Ahrens@Sun.COM /* 38524543Smarks * We don't want to have a hard dependency 38534543Smarks * against some special symbols in sharefs 38545331Samw * nfs, and smbsrv. Determine them if needed when 38554543Smarks * the first file system is shared. 38565331Samw * Neither sharefs, nfs or smbsrv are unloadable modules. 38574543Smarks */ 38585331Samw int (*znfsexport_fs)(void *arg); 38594543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 38605331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 38615331Samw 38625331Samw int zfs_nfsshare_inited; 38635331Samw int zfs_smbshare_inited; 38645331Samw 38654543Smarks ddi_modhandle_t nfs_mod; 38664543Smarks ddi_modhandle_t sharefs_mod; 38675331Samw ddi_modhandle_t smbsrv_mod; 38684543Smarks kmutex_t zfs_share_lock; 38694543Smarks 38704543Smarks static int 38715331Samw zfs_init_sharefs() 38725331Samw { 38735331Samw int error; 38745331Samw 38755331Samw ASSERT(MUTEX_HELD(&zfs_share_lock)); 38765331Samw /* Both NFS and SMB shares also require sharetab support. */ 38775331Samw if (sharefs_mod == NULL && ((sharefs_mod = 38785331Samw ddi_modopen("fs/sharefs", 38795331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 38805331Samw return (ENOSYS); 38815331Samw } 38825331Samw if (zshare_fs == NULL && ((zshare_fs = 38835331Samw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 38845331Samw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 38855331Samw return (ENOSYS); 38865331Samw } 38875331Samw return (0); 38885331Samw } 38895331Samw 38905331Samw static int 38914543Smarks zfs_ioc_share(zfs_cmd_t *zc) 38924543Smarks { 38934543Smarks int error; 38944543Smarks int opcode; 38954543Smarks 38965331Samw switch (zc->zc_share.z_sharetype) { 38975331Samw case ZFS_SHARE_NFS: 38985331Samw case ZFS_UNSHARE_NFS: 38995331Samw if (zfs_nfsshare_inited == 0) { 39005331Samw mutex_enter(&zfs_share_lock); 39015331Samw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 39025331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 39035331Samw mutex_exit(&zfs_share_lock); 39045331Samw return (ENOSYS); 39055331Samw } 39065331Samw if (znfsexport_fs == NULL && 39075331Samw ((znfsexport_fs = (int (*)(void *)) 39085331Samw ddi_modsym(nfs_mod, 39095331Samw "nfs_export", &error)) == NULL)) { 39105331Samw mutex_exit(&zfs_share_lock); 39115331Samw return (ENOSYS); 39125331Samw } 39135331Samw error = zfs_init_sharefs(); 39145331Samw if (error) { 39155331Samw mutex_exit(&zfs_share_lock); 39165331Samw return (ENOSYS); 39175331Samw } 39185331Samw zfs_nfsshare_inited = 1; 39194543Smarks mutex_exit(&zfs_share_lock); 39204543Smarks } 39215331Samw break; 39225331Samw case ZFS_SHARE_SMB: 39235331Samw case ZFS_UNSHARE_SMB: 39245331Samw if (zfs_smbshare_inited == 0) { 39255331Samw mutex_enter(&zfs_share_lock); 39265331Samw if (smbsrv_mod == NULL && ((smbsrv_mod = 39275331Samw ddi_modopen("drv/smbsrv", 39285331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 39295331Samw mutex_exit(&zfs_share_lock); 39305331Samw return (ENOSYS); 39315331Samw } 39325331Samw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 39335331Samw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 39346139Sjb150015 "smb_server_share", &error)) == NULL)) { 39355331Samw mutex_exit(&zfs_share_lock); 39365331Samw return (ENOSYS); 39375331Samw } 39385331Samw error = zfs_init_sharefs(); 39395331Samw if (error) { 39405331Samw mutex_exit(&zfs_share_lock); 39415331Samw return (ENOSYS); 39425331Samw } 39435331Samw zfs_smbshare_inited = 1; 39444543Smarks mutex_exit(&zfs_share_lock); 39454543Smarks } 39465331Samw break; 39475331Samw default: 39485331Samw return (EINVAL); 39494543Smarks } 39504543Smarks 39515331Samw switch (zc->zc_share.z_sharetype) { 39525331Samw case ZFS_SHARE_NFS: 39535331Samw case ZFS_UNSHARE_NFS: 39545331Samw if (error = 39555331Samw znfsexport_fs((void *) 39565331Samw (uintptr_t)zc->zc_share.z_exportdata)) 39575331Samw return (error); 39585331Samw break; 39595331Samw case ZFS_SHARE_SMB: 39605331Samw case ZFS_UNSHARE_SMB: 39615331Samw if (error = zsmbexport_fs((void *) 39625331Samw (uintptr_t)zc->zc_share.z_exportdata, 39635331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 39648845Samw@Sun.COM B_TRUE: B_FALSE)) { 39655331Samw return (error); 39665331Samw } 39675331Samw break; 39685331Samw } 39695331Samw 39705331Samw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 39715331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 39724543Smarks SHAREFS_ADD : SHAREFS_REMOVE; 39734543Smarks 39745331Samw /* 39755331Samw * Add or remove share from sharetab 39765331Samw */ 39774543Smarks error = zshare_fs(opcode, 39784543Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 39794543Smarks zc->zc_share.z_sharemax); 39804543Smarks 39814543Smarks return (error); 39824543Smarks 39834543Smarks } 39844543Smarks 39858845Samw@Sun.COM ace_t full_access[] = { 39868845Samw@Sun.COM {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 39878845Samw@Sun.COM }; 39888845Samw@Sun.COM 39898845Samw@Sun.COM /* 39908845Samw@Sun.COM * Remove all ACL files in shares dir 39918845Samw@Sun.COM */ 39928845Samw@Sun.COM static int 39938845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp) 39948845Samw@Sun.COM { 39958845Samw@Sun.COM zap_cursor_t zc; 39968845Samw@Sun.COM zap_attribute_t zap; 39978845Samw@Sun.COM zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 39988845Samw@Sun.COM int error; 39998845Samw@Sun.COM 40008845Samw@Sun.COM for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 40018845Samw@Sun.COM (error = zap_cursor_retrieve(&zc, &zap)) == 0; 40028845Samw@Sun.COM zap_cursor_advance(&zc)) { 40038845Samw@Sun.COM if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 40048845Samw@Sun.COM NULL, 0)) != 0) 40058845Samw@Sun.COM break; 40068845Samw@Sun.COM } 40078845Samw@Sun.COM zap_cursor_fini(&zc); 40088845Samw@Sun.COM return (error); 40098845Samw@Sun.COM } 40108845Samw@Sun.COM 40118845Samw@Sun.COM static int 40128845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc) 40138845Samw@Sun.COM { 40148845Samw@Sun.COM vnode_t *vp; 40158845Samw@Sun.COM znode_t *dzp; 40168845Samw@Sun.COM vnode_t *resourcevp = NULL; 40178845Samw@Sun.COM znode_t *sharedir; 40188845Samw@Sun.COM zfsvfs_t *zfsvfs; 40198845Samw@Sun.COM nvlist_t *nvlist; 40208845Samw@Sun.COM char *src, *target; 40218845Samw@Sun.COM vattr_t vattr; 40228845Samw@Sun.COM vsecattr_t vsec; 40238845Samw@Sun.COM int error = 0; 40248845Samw@Sun.COM 40258845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 40268845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 40278845Samw@Sun.COM return (error); 40288845Samw@Sun.COM 40298845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 40308845Samw@Sun.COM 40318845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 40328845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 40338845Samw@Sun.COM zc->zc_name) != 0)) { 40348845Samw@Sun.COM VN_RELE(vp); 40358845Samw@Sun.COM return (EINVAL); 40368845Samw@Sun.COM } 40378845Samw@Sun.COM 40388845Samw@Sun.COM dzp = VTOZ(vp); 40398845Samw@Sun.COM zfsvfs = dzp->z_zfsvfs; 40408845Samw@Sun.COM ZFS_ENTER(zfsvfs); 40418845Samw@Sun.COM 40429030SMark.Shellenbaum@Sun.COM /* 40439030SMark.Shellenbaum@Sun.COM * Create share dir if its missing. 40449030SMark.Shellenbaum@Sun.COM */ 40459030SMark.Shellenbaum@Sun.COM mutex_enter(&zfsvfs->z_lock); 40469030SMark.Shellenbaum@Sun.COM if (zfsvfs->z_shares_dir == 0) { 40479030SMark.Shellenbaum@Sun.COM dmu_tx_t *tx; 40489030SMark.Shellenbaum@Sun.COM 40499030SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(zfsvfs->z_os); 40509030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 40519030SMark.Shellenbaum@Sun.COM ZFS_SHARES_DIR); 40529030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 40539030SMark.Shellenbaum@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 40549030SMark.Shellenbaum@Sun.COM if (error) { 40559030SMark.Shellenbaum@Sun.COM dmu_tx_abort(tx); 40569030SMark.Shellenbaum@Sun.COM } else { 40579030SMark.Shellenbaum@Sun.COM error = zfs_create_share_dir(zfsvfs, tx); 40589030SMark.Shellenbaum@Sun.COM dmu_tx_commit(tx); 40599030SMark.Shellenbaum@Sun.COM } 40609030SMark.Shellenbaum@Sun.COM if (error) { 40619030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 40629030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 40639030SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 40649030SMark.Shellenbaum@Sun.COM return (error); 40659030SMark.Shellenbaum@Sun.COM } 40669030SMark.Shellenbaum@Sun.COM } 40679030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 40689030SMark.Shellenbaum@Sun.COM 40699030SMark.Shellenbaum@Sun.COM ASSERT(zfsvfs->z_shares_dir); 40708845Samw@Sun.COM if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 40719030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 40728845Samw@Sun.COM ZFS_EXIT(zfsvfs); 40738845Samw@Sun.COM return (error); 40748845Samw@Sun.COM } 40758845Samw@Sun.COM 40768845Samw@Sun.COM switch (zc->zc_cookie) { 40778845Samw@Sun.COM case ZFS_SMB_ACL_ADD: 40788845Samw@Sun.COM vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 40798845Samw@Sun.COM vattr.va_type = VREG; 40808845Samw@Sun.COM vattr.va_mode = S_IFREG|0777; 40818845Samw@Sun.COM vattr.va_uid = 0; 40828845Samw@Sun.COM vattr.va_gid = 0; 40838845Samw@Sun.COM 40848845Samw@Sun.COM vsec.vsa_mask = VSA_ACE; 40858845Samw@Sun.COM vsec.vsa_aclentp = &full_access; 40868845Samw@Sun.COM vsec.vsa_aclentsz = sizeof (full_access); 40878845Samw@Sun.COM vsec.vsa_aclcnt = 1; 40888845Samw@Sun.COM 40898845Samw@Sun.COM error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 40908845Samw@Sun.COM &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 40918845Samw@Sun.COM if (resourcevp) 40928845Samw@Sun.COM VN_RELE(resourcevp); 40938845Samw@Sun.COM break; 40948845Samw@Sun.COM 40958845Samw@Sun.COM case ZFS_SMB_ACL_REMOVE: 40968845Samw@Sun.COM error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 40978845Samw@Sun.COM NULL, 0); 40988845Samw@Sun.COM break; 40998845Samw@Sun.COM 41008845Samw@Sun.COM case ZFS_SMB_ACL_RENAME: 41018845Samw@Sun.COM if ((error = get_nvlist(zc->zc_nvlist_src, 41029643SEric.Taylor@Sun.COM zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 41038845Samw@Sun.COM VN_RELE(vp); 41048845Samw@Sun.COM ZFS_EXIT(zfsvfs); 41058845Samw@Sun.COM return (error); 41068845Samw@Sun.COM } 41078845Samw@Sun.COM if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 41088845Samw@Sun.COM nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 41098845Samw@Sun.COM &target)) { 41108845Samw@Sun.COM VN_RELE(vp); 41119179SMark.Shellenbaum@Sun.COM VN_RELE(ZTOV(sharedir)); 41128845Samw@Sun.COM ZFS_EXIT(zfsvfs); 41138845Samw@Sun.COM return (error); 41148845Samw@Sun.COM } 41158845Samw@Sun.COM error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 41168845Samw@Sun.COM kcred, NULL, 0); 41178845Samw@Sun.COM nvlist_free(nvlist); 41188845Samw@Sun.COM break; 41198845Samw@Sun.COM 41208845Samw@Sun.COM case ZFS_SMB_ACL_PURGE: 41218845Samw@Sun.COM error = zfs_smb_acl_purge(sharedir); 41228845Samw@Sun.COM break; 41238845Samw@Sun.COM 41248845Samw@Sun.COM default: 41258845Samw@Sun.COM error = EINVAL; 41268845Samw@Sun.COM break; 41278845Samw@Sun.COM } 41288845Samw@Sun.COM 41298845Samw@Sun.COM VN_RELE(vp); 41308845Samw@Sun.COM VN_RELE(ZTOV(sharedir)); 41318845Samw@Sun.COM 41328845Samw@Sun.COM ZFS_EXIT(zfsvfs); 41338845Samw@Sun.COM 41348845Samw@Sun.COM return (error); 41358845Samw@Sun.COM } 41368845Samw@Sun.COM 41374543Smarks /* 413810242Schris.kirby@sun.com * inputs: 413910242Schris.kirby@sun.com * zc_name name of filesystem 414010242Schris.kirby@sun.com * zc_value short name of snap 414110242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 414210242Schris.kirby@sun.com * zc_cookie recursive flag 414310342Schris.kirby@sun.com * zc_temphold set if hold is temporary 414410242Schris.kirby@sun.com * 414510242Schris.kirby@sun.com * outputs: none 414610242Schris.kirby@sun.com */ 414710242Schris.kirby@sun.com static int 414810242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc) 414910242Schris.kirby@sun.com { 415010242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 415110242Schris.kirby@sun.com 415210242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 415310242Schris.kirby@sun.com return (EINVAL); 415410242Schris.kirby@sun.com 415510242Schris.kirby@sun.com return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value, 415610342Schris.kirby@sun.com zc->zc_string, recursive, zc->zc_temphold)); 415710242Schris.kirby@sun.com } 415810242Schris.kirby@sun.com 415910242Schris.kirby@sun.com /* 416010242Schris.kirby@sun.com * inputs: 416110242Schris.kirby@sun.com * zc_name name of dataset from which we're releasing a user reference 416210242Schris.kirby@sun.com * zc_value short name of snap 416310242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 416410242Schris.kirby@sun.com * zc_cookie recursive flag 416510242Schris.kirby@sun.com * 416610242Schris.kirby@sun.com * outputs: none 416710242Schris.kirby@sun.com */ 416810242Schris.kirby@sun.com static int 416910242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc) 417010242Schris.kirby@sun.com { 417110242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 417210242Schris.kirby@sun.com 417310242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 417410242Schris.kirby@sun.com return (EINVAL); 417510242Schris.kirby@sun.com 417610242Schris.kirby@sun.com return (dsl_dataset_user_release(zc->zc_name, zc->zc_value, 417710242Schris.kirby@sun.com zc->zc_string, recursive)); 417810242Schris.kirby@sun.com } 417910242Schris.kirby@sun.com 418010242Schris.kirby@sun.com /* 418110242Schris.kirby@sun.com * inputs: 418210242Schris.kirby@sun.com * zc_name name of filesystem 418310242Schris.kirby@sun.com * 418410242Schris.kirby@sun.com * outputs: 418510242Schris.kirby@sun.com * zc_nvlist_src{_size} nvlist of snapshot holds 418610242Schris.kirby@sun.com */ 418710242Schris.kirby@sun.com static int 418810242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc) 418910242Schris.kirby@sun.com { 419010242Schris.kirby@sun.com nvlist_t *nvp; 419110242Schris.kirby@sun.com int error; 419210242Schris.kirby@sun.com 419310242Schris.kirby@sun.com if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) { 419410242Schris.kirby@sun.com error = put_nvlist(zc, nvp); 419510242Schris.kirby@sun.com nvlist_free(nvp); 419610242Schris.kirby@sun.com } 419710242Schris.kirby@sun.com 419810242Schris.kirby@sun.com return (error); 419910242Schris.kirby@sun.com } 420010242Schris.kirby@sun.com 420110242Schris.kirby@sun.com /* 42024988Sek110237 * pool create, destroy, and export don't log the history as part of 42034988Sek110237 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 42044988Sek110237 * do the logging of those commands. 42054543Smarks */ 4206789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 42079234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42089234SGeorge.Wilson@Sun.COM B_FALSE }, 42099234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42109234SGeorge.Wilson@Sun.COM B_FALSE }, 42119234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42129234SGeorge.Wilson@Sun.COM B_FALSE }, 42139234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42149234SGeorge.Wilson@Sun.COM B_FALSE }, 42159234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE, 42169234SGeorge.Wilson@Sun.COM B_FALSE }, 42179234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE, 42189234SGeorge.Wilson@Sun.COM B_FALSE }, 42199234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE, 42209234SGeorge.Wilson@Sun.COM B_FALSE }, 42219234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42229234SGeorge.Wilson@Sun.COM B_TRUE }, 42239234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE, 42249234SGeorge.Wilson@Sun.COM B_FALSE }, 42259234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42269234SGeorge.Wilson@Sun.COM B_TRUE }, 42279234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42289234SGeorge.Wilson@Sun.COM B_FALSE }, 42299234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42309234SGeorge.Wilson@Sun.COM B_TRUE }, 42319234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42329234SGeorge.Wilson@Sun.COM B_TRUE }, 42339234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42349234SGeorge.Wilson@Sun.COM B_FALSE }, 42359234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42369234SGeorge.Wilson@Sun.COM B_TRUE }, 42379234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42389234SGeorge.Wilson@Sun.COM B_TRUE }, 42399234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42409234SGeorge.Wilson@Sun.COM B_TRUE }, 42419425SEric.Schrock@Sun.COM { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42429425SEric.Schrock@Sun.COM B_TRUE }, 42439234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 42449234SGeorge.Wilson@Sun.COM B_FALSE }, 42459234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 42469234SGeorge.Wilson@Sun.COM B_FALSE }, 42479234SGeorge.Wilson@Sun.COM { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 42489234SGeorge.Wilson@Sun.COM B_FALSE }, 42499234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 42509234SGeorge.Wilson@Sun.COM B_FALSE }, 42519234SGeorge.Wilson@Sun.COM { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE }, 42529234SGeorge.Wilson@Sun.COM { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE }, 42539234SGeorge.Wilson@Sun.COM { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 42549234SGeorge.Wilson@Sun.COM B_TRUE}, 42559234SGeorge.Wilson@Sun.COM { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE, 42569234SGeorge.Wilson@Sun.COM B_TRUE }, 42579234SGeorge.Wilson@Sun.COM { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE }, 42589234SGeorge.Wilson@Sun.COM { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE }, 42599234SGeorge.Wilson@Sun.COM { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE }, 42609234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 42619234SGeorge.Wilson@Sun.COM B_FALSE }, 42629234SGeorge.Wilson@Sun.COM { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 42639234SGeorge.Wilson@Sun.COM B_FALSE }, 42649234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE, 42659234SGeorge.Wilson@Sun.COM B_FALSE }, 42669234SGeorge.Wilson@Sun.COM { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE, 42679234SGeorge.Wilson@Sun.COM B_FALSE }, 42689234SGeorge.Wilson@Sun.COM { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE }, 42699234SGeorge.Wilson@Sun.COM { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE, 42709234SGeorge.Wilson@Sun.COM B_TRUE }, 42719234SGeorge.Wilson@Sun.COM { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 42729234SGeorge.Wilson@Sun.COM B_TRUE }, 42739234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE, 42749234SGeorge.Wilson@Sun.COM B_TRUE }, 42759234SGeorge.Wilson@Sun.COM { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42769234SGeorge.Wilson@Sun.COM B_FALSE }, 427710233SGeorge.Wilson@Sun.COM { zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE, 427810233SGeorge.Wilson@Sun.COM B_TRUE }, 42799234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42809234SGeorge.Wilson@Sun.COM B_TRUE }, 42819234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE, 42829234SGeorge.Wilson@Sun.COM B_FALSE }, 42839234SGeorge.Wilson@Sun.COM { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE, 42849234SGeorge.Wilson@Sun.COM B_TRUE }, 42859234SGeorge.Wilson@Sun.COM { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 42869234SGeorge.Wilson@Sun.COM B_FALSE }, 42879234SGeorge.Wilson@Sun.COM { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE, 42889234SGeorge.Wilson@Sun.COM B_FALSE }, 42899234SGeorge.Wilson@Sun.COM { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE }, 42909234SGeorge.Wilson@Sun.COM { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE, 42919234SGeorge.Wilson@Sun.COM B_TRUE }, 42929234SGeorge.Wilson@Sun.COM { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE, 42939396SMatthew.Ahrens@Sun.COM B_FALSE }, 42949396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, 42959396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 42969396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, 42979396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 42989396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 42999396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_TRUE }, 430010242Schris.kirby@sun.com { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE }, 430110242Schris.kirby@sun.com { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE, 430210242Schris.kirby@sun.com B_TRUE }, 430310242Schris.kirby@sun.com { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 430411022STom.Erickson@Sun.COM B_TRUE }, 430511022STom.Erickson@Sun.COM { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 430611022STom.Erickson@Sun.COM B_FALSE } 4307789Sahrens }; 4308789Sahrens 43099234SGeorge.Wilson@Sun.COM int 43109234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type) 43119234SGeorge.Wilson@Sun.COM { 43129234SGeorge.Wilson@Sun.COM spa_t *spa; 43139234SGeorge.Wilson@Sun.COM int error; 43149234SGeorge.Wilson@Sun.COM 43159234SGeorge.Wilson@Sun.COM ASSERT(type == POOL_NAME || type == DATASET_NAME); 43169234SGeorge.Wilson@Sun.COM 43179396SMatthew.Ahrens@Sun.COM error = spa_open(name, &spa, FTAG); 43189234SGeorge.Wilson@Sun.COM if (error == 0) { 43199234SGeorge.Wilson@Sun.COM if (spa_suspended(spa)) 43209234SGeorge.Wilson@Sun.COM error = EAGAIN; 43219234SGeorge.Wilson@Sun.COM spa_close(spa, FTAG); 43229234SGeorge.Wilson@Sun.COM } 43239234SGeorge.Wilson@Sun.COM return (error); 43249234SGeorge.Wilson@Sun.COM } 43259234SGeorge.Wilson@Sun.COM 4326789Sahrens static int 4327789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 4328789Sahrens { 4329789Sahrens zfs_cmd_t *zc; 4330789Sahrens uint_t vec; 43312199Sahrens int error, rc; 4332789Sahrens 4333789Sahrens if (getminor(dev) != 0) 4334789Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 4335789Sahrens 4336789Sahrens vec = cmd - ZFS_IOC; 43374787Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 4338789Sahrens 4339789Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 4340789Sahrens return (EINVAL); 4341789Sahrens 4342789Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 4343789Sahrens 43449643SEric.Taylor@Sun.COM error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 4345789Sahrens 434610588SEric.Taylor@Sun.COM if ((error == 0) && !(flag & FKIOCTL)) 43474543Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 4348789Sahrens 4349789Sahrens /* 4350789Sahrens * Ensure that all pool/dataset names are valid before we pass down to 4351789Sahrens * the lower layers. 4352789Sahrens */ 4353789Sahrens if (error == 0) { 4354789Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 43559643SEric.Taylor@Sun.COM zc->zc_iflags = flag & FKIOCTL; 4356789Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 43574577Sahrens case POOL_NAME: 4358789Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 4359789Sahrens error = EINVAL; 43609234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 43619234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 43629234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 4363789Sahrens break; 4364789Sahrens 43654577Sahrens case DATASET_NAME: 4366789Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 4367789Sahrens error = EINVAL; 43689234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 43699234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 43709234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 4371789Sahrens break; 43722856Snd150628 43734577Sahrens case NO_NAME: 43742856Snd150628 break; 4375789Sahrens } 4376789Sahrens } 4377789Sahrens 4378789Sahrens if (error == 0) 4379789Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 4380789Sahrens 43819643SEric.Taylor@Sun.COM rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 43824543Smarks if (error == 0) { 43832199Sahrens error = rc; 43849396SMatthew.Ahrens@Sun.COM if (zfs_ioc_vec[vec].zvec_his_log) 43854543Smarks zfs_log_history(zc); 43864543Smarks } 4387789Sahrens 4388789Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 4389789Sahrens return (error); 4390789Sahrens } 4391789Sahrens 4392789Sahrens static int 4393789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 4394789Sahrens { 4395789Sahrens if (cmd != DDI_ATTACH) 4396789Sahrens return (DDI_FAILURE); 4397789Sahrens 4398789Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 4399789Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 4400789Sahrens return (DDI_FAILURE); 4401789Sahrens 4402789Sahrens zfs_dip = dip; 4403789Sahrens 4404789Sahrens ddi_report_dev(dip); 4405789Sahrens 4406789Sahrens return (DDI_SUCCESS); 4407789Sahrens } 4408789Sahrens 4409789Sahrens static int 4410789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 4411789Sahrens { 4412789Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 4413789Sahrens return (DDI_FAILURE); 4414789Sahrens 4415789Sahrens if (cmd != DDI_DETACH) 4416789Sahrens return (DDI_FAILURE); 4417789Sahrens 4418789Sahrens zfs_dip = NULL; 4419789Sahrens 4420789Sahrens ddi_prop_remove_all(dip); 4421789Sahrens ddi_remove_minor_node(dip, NULL); 4422789Sahrens 4423789Sahrens return (DDI_SUCCESS); 4424789Sahrens } 4425789Sahrens 4426789Sahrens /*ARGSUSED*/ 4427789Sahrens static int 4428789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 4429789Sahrens { 4430789Sahrens switch (infocmd) { 4431789Sahrens case DDI_INFO_DEVT2DEVINFO: 4432789Sahrens *result = zfs_dip; 4433789Sahrens return (DDI_SUCCESS); 4434789Sahrens 4435789Sahrens case DDI_INFO_DEVT2INSTANCE: 4436849Sbonwick *result = (void *)0; 4437789Sahrens return (DDI_SUCCESS); 4438789Sahrens } 4439789Sahrens 4440789Sahrens return (DDI_FAILURE); 4441789Sahrens } 4442789Sahrens 4443789Sahrens /* 4444789Sahrens * OK, so this is a little weird. 4445789Sahrens * 4446789Sahrens * /dev/zfs is the control node, i.e. minor 0. 4447789Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 4448789Sahrens * 4449789Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 4450789Sahrens * so most of the standard driver entry points are in zvol.c. 4451789Sahrens */ 4452789Sahrens static struct cb_ops zfs_cb_ops = { 4453789Sahrens zvol_open, /* open */ 4454789Sahrens zvol_close, /* close */ 4455789Sahrens zvol_strategy, /* strategy */ 4456789Sahrens nodev, /* print */ 44576423Sgw25295 zvol_dump, /* dump */ 4458789Sahrens zvol_read, /* read */ 4459789Sahrens zvol_write, /* write */ 4460789Sahrens zfsdev_ioctl, /* ioctl */ 4461789Sahrens nodev, /* devmap */ 4462789Sahrens nodev, /* mmap */ 4463789Sahrens nodev, /* segmap */ 4464789Sahrens nochpoll, /* poll */ 4465789Sahrens ddi_prop_op, /* prop_op */ 4466789Sahrens NULL, /* streamtab */ 4467789Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 4468789Sahrens CB_REV, /* version */ 44693638Sbillm nodev, /* async read */ 44703638Sbillm nodev, /* async write */ 4471789Sahrens }; 4472789Sahrens 4473789Sahrens static struct dev_ops zfs_dev_ops = { 4474789Sahrens DEVO_REV, /* version */ 4475789Sahrens 0, /* refcnt */ 4476789Sahrens zfs_info, /* info */ 4477789Sahrens nulldev, /* identify */ 4478789Sahrens nulldev, /* probe */ 4479789Sahrens zfs_attach, /* attach */ 4480789Sahrens zfs_detach, /* detach */ 4481789Sahrens nodev, /* reset */ 4482789Sahrens &zfs_cb_ops, /* driver operations */ 44837656SSherry.Moore@Sun.COM NULL, /* no bus operations */ 44847656SSherry.Moore@Sun.COM NULL, /* power */ 44857656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */ 4486789Sahrens }; 4487789Sahrens 4488789Sahrens static struct modldrv zfs_modldrv = { 44897656SSherry.Moore@Sun.COM &mod_driverops, 44907656SSherry.Moore@Sun.COM "ZFS storage pool", 44917656SSherry.Moore@Sun.COM &zfs_dev_ops 4492789Sahrens }; 4493789Sahrens 4494789Sahrens static struct modlinkage modlinkage = { 4495789Sahrens MODREV_1, 4496789Sahrens (void *)&zfs_modlfs, 4497789Sahrens (void *)&zfs_modldrv, 4498789Sahrens NULL 4499789Sahrens }; 4500789Sahrens 45014720Sfr157268 45024720Sfr157268 uint_t zfs_fsyncer_key; 45035326Sek110237 extern uint_t rrw_tsd_key; 45044720Sfr157268 4505789Sahrens int 4506789Sahrens _init(void) 4507789Sahrens { 4508789Sahrens int error; 4509789Sahrens 4510849Sbonwick spa_init(FREAD | FWRITE); 4511849Sbonwick zfs_init(); 4512849Sbonwick zvol_init(); 4513849Sbonwick 4514849Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 4515849Sbonwick zvol_fini(); 4516849Sbonwick zfs_fini(); 4517849Sbonwick spa_fini(); 4518789Sahrens return (error); 4519849Sbonwick } 4520789Sahrens 45214720Sfr157268 tsd_create(&zfs_fsyncer_key, NULL); 45225326Sek110237 tsd_create(&rrw_tsd_key, NULL); 45234720Sfr157268 4524789Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 4525789Sahrens ASSERT(error == 0); 45264543Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 4527789Sahrens 4528789Sahrens return (0); 4529789Sahrens } 4530789Sahrens 4531789Sahrens int 4532789Sahrens _fini(void) 4533789Sahrens { 4534789Sahrens int error; 4535789Sahrens 45361544Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 4537789Sahrens return (EBUSY); 4538789Sahrens 4539789Sahrens if ((error = mod_remove(&modlinkage)) != 0) 4540789Sahrens return (error); 4541789Sahrens 4542789Sahrens zvol_fini(); 4543789Sahrens zfs_fini(); 4544789Sahrens spa_fini(); 45455331Samw if (zfs_nfsshare_inited) 45464543Smarks (void) ddi_modclose(nfs_mod); 45475331Samw if (zfs_smbshare_inited) 45485331Samw (void) ddi_modclose(smbsrv_mod); 45495331Samw if (zfs_nfsshare_inited || zfs_smbshare_inited) 45504543Smarks (void) ddi_modclose(sharefs_mod); 4551789Sahrens 45524720Sfr157268 tsd_destroy(&zfs_fsyncer_key); 4553789Sahrens ldi_ident_release(zfs_li); 4554789Sahrens zfs_li = NULL; 45554543Smarks mutex_destroy(&zfs_share_lock); 4556789Sahrens 4557789Sahrens return (error); 4558789Sahrens } 4559789Sahrens 4560789Sahrens int 4561789Sahrens _info(struct modinfo *modinfop) 4562789Sahrens { 4563789Sahrens return (mod_info(&modlinkage, modinfop)); 4564789Sahrens } 4565