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 /* 2211422SMark.Musante@Sun.COM * Copyright 2010 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 /* 60411314Swilliam.gorrell@sun.com * Destroying snapshots with delegated permissions requires 60511314Swilliam.gorrell@sun.com * descendent mount and destroy permissions. 60611314Swilliam.gorrell@sun.com * Reassemble the full filesystem@snap name so dsl_deleg_access() 60711314Swilliam.gorrell@sun.com * can do the correct permission check. 60811314Swilliam.gorrell@sun.com * 60911314Swilliam.gorrell@sun.com * Since this routine is used when doing a recursive destroy of snapshots 61011314Swilliam.gorrell@sun.com * and destroying snapshots requires descendent permissions, a successfull 61111314Swilliam.gorrell@sun.com * check of the top level snapshot applies to snapshots of all descendent 61211314Swilliam.gorrell@sun.com * datasets as well. 61311314Swilliam.gorrell@sun.com */ 61411314Swilliam.gorrell@sun.com static int 61511314Swilliam.gorrell@sun.com zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, cred_t *cr) 61611314Swilliam.gorrell@sun.com { 61711314Swilliam.gorrell@sun.com int error; 61811314Swilliam.gorrell@sun.com char *dsname; 61911314Swilliam.gorrell@sun.com 62011314Swilliam.gorrell@sun.com dsname = kmem_asprintf("%s@%s", zc->zc_name, zc->zc_value); 62111314Swilliam.gorrell@sun.com 62211314Swilliam.gorrell@sun.com error = zfs_secpolicy_destroy_perms(dsname, cr); 62311314Swilliam.gorrell@sun.com 62411314Swilliam.gorrell@sun.com strfree(dsname); 62511314Swilliam.gorrell@sun.com return (error); 62611314Swilliam.gorrell@sun.com } 62711314Swilliam.gorrell@sun.com 62811314Swilliam.gorrell@sun.com /* 6294543Smarks * Must have sys_config privilege to check the iscsi permission 6304543Smarks */ 6314543Smarks /* ARGSUSED */ 6324543Smarks static int 6334543Smarks zfs_secpolicy_iscsi(zfs_cmd_t *zc, cred_t *cr) 6344543Smarks { 6354543Smarks return (secpolicy_zfs(cr)); 6364543Smarks } 6374543Smarks 6384543Smarks int 6394543Smarks zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) 6404543Smarks { 64111022STom.Erickson@Sun.COM char parentname[MAXNAMELEN]; 6424543Smarks int error; 6434543Smarks 6444543Smarks if ((error = zfs_secpolicy_write_perms(from, 6454543Smarks ZFS_DELEG_PERM_RENAME, cr)) != 0) 6464543Smarks return (error); 6474543Smarks 6484543Smarks if ((error = zfs_secpolicy_write_perms(from, 6494543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 6504543Smarks return (error); 6514543Smarks 6524543Smarks if ((error = zfs_get_parent(to, parentname, 6534543Smarks sizeof (parentname))) != 0) 6544543Smarks return (error); 6554543Smarks 6564543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 6574543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 6584543Smarks return (error); 6594543Smarks 6604543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 6614543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 6624543Smarks return (error); 6634543Smarks 6644543Smarks return (error); 6654543Smarks } 6664543Smarks 6674543Smarks static int 6684543Smarks zfs_secpolicy_rename(zfs_cmd_t *zc, cred_t *cr) 6694543Smarks { 6704543Smarks return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); 6714543Smarks } 6724543Smarks 6734543Smarks static int 6744543Smarks zfs_secpolicy_promote(zfs_cmd_t *zc, cred_t *cr) 6754543Smarks { 67611022STom.Erickson@Sun.COM char parentname[MAXNAMELEN]; 6774543Smarks objset_t *clone; 6784543Smarks int error; 6794543Smarks 6804543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 6814543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 6824543Smarks if (error) 6834543Smarks return (error); 6844543Smarks 68510298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &clone); 6864543Smarks 6874543Smarks if (error == 0) { 6884543Smarks dsl_dataset_t *pclone = NULL; 6894543Smarks dsl_dir_t *dd; 69010298SMatthew.Ahrens@Sun.COM dd = clone->os_dsl_dataset->ds_dir; 6914543Smarks 6924543Smarks rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); 6936689Smaybee error = dsl_dataset_hold_obj(dd->dd_pool, 6946689Smaybee dd->dd_phys->dd_origin_obj, FTAG, &pclone); 6954543Smarks rw_exit(&dd->dd_pool->dp_config_rwlock); 6964543Smarks if (error) { 69710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 6984543Smarks return (error); 6994543Smarks } 7004543Smarks 7014543Smarks error = zfs_secpolicy_write_perms(zc->zc_name, 7024543Smarks ZFS_DELEG_PERM_MOUNT, cr); 7034543Smarks 7044543Smarks dsl_dataset_name(pclone, parentname); 70510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 7066689Smaybee dsl_dataset_rele(pclone, FTAG); 7074543Smarks if (error == 0) 7084543Smarks error = zfs_secpolicy_write_perms(parentname, 7094543Smarks ZFS_DELEG_PERM_PROMOTE, cr); 7104543Smarks } 7114543Smarks return (error); 7124543Smarks } 7134543Smarks 7144543Smarks static int 7154543Smarks zfs_secpolicy_receive(zfs_cmd_t *zc, cred_t *cr) 7164543Smarks { 7174543Smarks int error; 7184543Smarks 7194543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 7204543Smarks ZFS_DELEG_PERM_RECEIVE, cr)) != 0) 7214543Smarks return (error); 7224543Smarks 7234543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_name, 7244543Smarks ZFS_DELEG_PERM_MOUNT, cr)) != 0) 7254543Smarks return (error); 7264543Smarks 7274543Smarks return (zfs_secpolicy_write_perms(zc->zc_name, 7284543Smarks ZFS_DELEG_PERM_CREATE, cr)); 7294543Smarks } 7304543Smarks 7314543Smarks int 7324543Smarks zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) 7334543Smarks { 73410588SEric.Taylor@Sun.COM return (zfs_secpolicy_write_perms(name, 73510588SEric.Taylor@Sun.COM ZFS_DELEG_PERM_SNAPSHOT, cr)); 7364543Smarks } 7374543Smarks 7384543Smarks static int 7394543Smarks zfs_secpolicy_snapshot(zfs_cmd_t *zc, cred_t *cr) 7404543Smarks { 7414543Smarks 7424543Smarks return (zfs_secpolicy_snapshot_perms(zc->zc_name, cr)); 7434543Smarks } 7444543Smarks 7454543Smarks static int 7464543Smarks zfs_secpolicy_create(zfs_cmd_t *zc, cred_t *cr) 7474543Smarks { 74811022STom.Erickson@Sun.COM char parentname[MAXNAMELEN]; 74911022STom.Erickson@Sun.COM int error; 7504543Smarks 7514543Smarks if ((error = zfs_get_parent(zc->zc_name, parentname, 7524543Smarks sizeof (parentname))) != 0) 7534543Smarks return (error); 7544543Smarks 7554543Smarks if (zc->zc_value[0] != '\0') { 7564543Smarks if ((error = zfs_secpolicy_write_perms(zc->zc_value, 7574543Smarks ZFS_DELEG_PERM_CLONE, cr)) != 0) 7584543Smarks return (error); 7594543Smarks } 7604543Smarks 7614543Smarks if ((error = zfs_secpolicy_write_perms(parentname, 7624543Smarks ZFS_DELEG_PERM_CREATE, cr)) != 0) 7634543Smarks return (error); 7644543Smarks 7654543Smarks error = zfs_secpolicy_write_perms(parentname, 7664543Smarks ZFS_DELEG_PERM_MOUNT, cr); 7674543Smarks 7684543Smarks return (error); 7694543Smarks } 7704543Smarks 7714543Smarks static int 7724543Smarks zfs_secpolicy_umount(zfs_cmd_t *zc, cred_t *cr) 7734543Smarks { 7744543Smarks int error; 7754543Smarks 7764543Smarks error = secpolicy_fs_unmount(cr, NULL); 7774543Smarks if (error) { 7784543Smarks error = dsl_deleg_access(zc->zc_name, ZFS_DELEG_PERM_MOUNT, cr); 7794543Smarks } 7804543Smarks return (error); 781789Sahrens } 782789Sahrens 783789Sahrens /* 784789Sahrens * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires 785789Sahrens * SYS_CONFIG privilege, which is not available in a local zone. 786789Sahrens */ 787789Sahrens /* ARGSUSED */ 788789Sahrens static int 7894543Smarks zfs_secpolicy_config(zfs_cmd_t *zc, cred_t *cr) 790789Sahrens { 791789Sahrens if (secpolicy_sys_config(cr, B_FALSE) != 0) 792789Sahrens return (EPERM); 793789Sahrens 794789Sahrens return (0); 795789Sahrens } 796789Sahrens 797789Sahrens /* 7981544Seschrock * Policy for fault injection. Requires all privileges. 7991544Seschrock */ 8001544Seschrock /* ARGSUSED */ 8011544Seschrock static int 8024543Smarks zfs_secpolicy_inject(zfs_cmd_t *zc, cred_t *cr) 8031544Seschrock { 8041544Seschrock return (secpolicy_zinject(cr)); 8051544Seschrock } 8061544Seschrock 8074849Sahrens static int 8084849Sahrens zfs_secpolicy_inherit(zfs_cmd_t *zc, cred_t *cr) 8094849Sahrens { 8104849Sahrens zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); 8114849Sahrens 8125094Slling if (prop == ZPROP_INVAL) { 8134849Sahrens if (!zfs_prop_user(zc->zc_value)) 8144849Sahrens return (EINVAL); 8154849Sahrens return (zfs_secpolicy_write_perms(zc->zc_name, 8164849Sahrens ZFS_DELEG_PERM_USERPROP, cr)); 8174849Sahrens } else { 81811022STom.Erickson@Sun.COM return (zfs_secpolicy_setprop(zc->zc_name, prop, 81911022STom.Erickson@Sun.COM NULL, cr)); 8204849Sahrens } 8214849Sahrens } 8224849Sahrens 8239396SMatthew.Ahrens@Sun.COM static int 8249396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_one(zfs_cmd_t *zc, cred_t *cr) 8259396SMatthew.Ahrens@Sun.COM { 8269396SMatthew.Ahrens@Sun.COM int err = zfs_secpolicy_read(zc, cr); 8279396SMatthew.Ahrens@Sun.COM if (err) 8289396SMatthew.Ahrens@Sun.COM return (err); 8299396SMatthew.Ahrens@Sun.COM 8309396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 8319396SMatthew.Ahrens@Sun.COM return (EINVAL); 8329396SMatthew.Ahrens@Sun.COM 8339396SMatthew.Ahrens@Sun.COM if (zc->zc_value[0] == 0) { 8349396SMatthew.Ahrens@Sun.COM /* 8359396SMatthew.Ahrens@Sun.COM * They are asking about a posix uid/gid. If it's 8369396SMatthew.Ahrens@Sun.COM * themself, allow it. 8379396SMatthew.Ahrens@Sun.COM */ 8389396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type == ZFS_PROP_USERUSED || 8399396SMatthew.Ahrens@Sun.COM zc->zc_objset_type == ZFS_PROP_USERQUOTA) { 8409396SMatthew.Ahrens@Sun.COM if (zc->zc_guid == crgetuid(cr)) 8419396SMatthew.Ahrens@Sun.COM return (0); 8429396SMatthew.Ahrens@Sun.COM } else { 8439396SMatthew.Ahrens@Sun.COM if (groupmember(zc->zc_guid, cr)) 8449396SMatthew.Ahrens@Sun.COM return (0); 8459396SMatthew.Ahrens@Sun.COM } 8469396SMatthew.Ahrens@Sun.COM } 8479396SMatthew.Ahrens@Sun.COM 8489396SMatthew.Ahrens@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 8499396SMatthew.Ahrens@Sun.COM userquota_perms[zc->zc_objset_type], cr)); 8509396SMatthew.Ahrens@Sun.COM } 8519396SMatthew.Ahrens@Sun.COM 8529396SMatthew.Ahrens@Sun.COM static int 8539396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_many(zfs_cmd_t *zc, cred_t *cr) 8549396SMatthew.Ahrens@Sun.COM { 8559396SMatthew.Ahrens@Sun.COM int err = zfs_secpolicy_read(zc, cr); 8569396SMatthew.Ahrens@Sun.COM if (err) 8579396SMatthew.Ahrens@Sun.COM return (err); 8589396SMatthew.Ahrens@Sun.COM 8599396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 8609396SMatthew.Ahrens@Sun.COM return (EINVAL); 8619396SMatthew.Ahrens@Sun.COM 8629396SMatthew.Ahrens@Sun.COM return (zfs_secpolicy_write_perms(zc->zc_name, 8639396SMatthew.Ahrens@Sun.COM userquota_perms[zc->zc_objset_type], cr)); 8649396SMatthew.Ahrens@Sun.COM } 8659396SMatthew.Ahrens@Sun.COM 8669396SMatthew.Ahrens@Sun.COM static int 8679396SMatthew.Ahrens@Sun.COM zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, cred_t *cr) 8689396SMatthew.Ahrens@Sun.COM { 86911022STom.Erickson@Sun.COM return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, 87011022STom.Erickson@Sun.COM NULL, cr)); 8719396SMatthew.Ahrens@Sun.COM } 8729396SMatthew.Ahrens@Sun.COM 87310242Schris.kirby@sun.com static int 87410242Schris.kirby@sun.com zfs_secpolicy_hold(zfs_cmd_t *zc, cred_t *cr) 87510242Schris.kirby@sun.com { 87610242Schris.kirby@sun.com return (zfs_secpolicy_write_perms(zc->zc_name, 87710242Schris.kirby@sun.com ZFS_DELEG_PERM_HOLD, cr)); 87810242Schris.kirby@sun.com } 87910242Schris.kirby@sun.com 88010242Schris.kirby@sun.com static int 88110242Schris.kirby@sun.com zfs_secpolicy_release(zfs_cmd_t *zc, cred_t *cr) 88210242Schris.kirby@sun.com { 88310242Schris.kirby@sun.com return (zfs_secpolicy_write_perms(zc->zc_name, 88410242Schris.kirby@sun.com ZFS_DELEG_PERM_RELEASE, cr)); 88510242Schris.kirby@sun.com } 88610242Schris.kirby@sun.com 8871544Seschrock /* 888789Sahrens * Returns the nvlist as specified by the user in the zfs_cmd_t. 889789Sahrens */ 890789Sahrens static int 8919643SEric.Taylor@Sun.COM get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) 892789Sahrens { 893789Sahrens char *packed; 894789Sahrens int error; 8955094Slling nvlist_t *list = NULL; 896789Sahrens 897789Sahrens /* 8982676Seschrock * Read in and unpack the user-supplied nvlist. 899789Sahrens */ 9005094Slling if (size == 0) 901789Sahrens return (EINVAL); 902789Sahrens 903789Sahrens packed = kmem_alloc(size, KM_SLEEP); 904789Sahrens 9059643SEric.Taylor@Sun.COM if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, 9069643SEric.Taylor@Sun.COM iflag)) != 0) { 907789Sahrens kmem_free(packed, size); 908789Sahrens return (error); 909789Sahrens } 910789Sahrens 9115094Slling if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { 912789Sahrens kmem_free(packed, size); 913789Sahrens return (error); 914789Sahrens } 915789Sahrens 916789Sahrens kmem_free(packed, size); 917789Sahrens 9185094Slling *nvp = list; 919789Sahrens return (0); 920789Sahrens } 921789Sahrens 922789Sahrens static int 92311022STom.Erickson@Sun.COM fit_error_list(zfs_cmd_t *zc, nvlist_t **errors) 92411022STom.Erickson@Sun.COM { 92511022STom.Erickson@Sun.COM size_t size; 92611022STom.Erickson@Sun.COM 92711022STom.Erickson@Sun.COM VERIFY(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 92811022STom.Erickson@Sun.COM 92911022STom.Erickson@Sun.COM if (size > zc->zc_nvlist_dst_size) { 93011022STom.Erickson@Sun.COM nvpair_t *more_errors; 93111022STom.Erickson@Sun.COM int n = 0; 93211022STom.Erickson@Sun.COM 93311022STom.Erickson@Sun.COM if (zc->zc_nvlist_dst_size < 1024) 93411022STom.Erickson@Sun.COM return (ENOMEM); 93511022STom.Erickson@Sun.COM 93611022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, 0) == 0); 93711022STom.Erickson@Sun.COM more_errors = nvlist_prev_nvpair(*errors, NULL); 93811022STom.Erickson@Sun.COM 93911022STom.Erickson@Sun.COM do { 94011022STom.Erickson@Sun.COM nvpair_t *pair = nvlist_prev_nvpair(*errors, 94111022STom.Erickson@Sun.COM more_errors); 94211022STom.Erickson@Sun.COM VERIFY(nvlist_remove_nvpair(*errors, pair) == 0); 94311022STom.Erickson@Sun.COM n++; 94411022STom.Erickson@Sun.COM VERIFY(nvlist_size(*errors, &size, 94511022STom.Erickson@Sun.COM NV_ENCODE_NATIVE) == 0); 94611022STom.Erickson@Sun.COM } while (size > zc->zc_nvlist_dst_size); 94711022STom.Erickson@Sun.COM 94811022STom.Erickson@Sun.COM VERIFY(nvlist_remove_nvpair(*errors, more_errors) == 0); 94911022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(*errors, ZPROP_N_MORE_ERRORS, n) == 0); 95011022STom.Erickson@Sun.COM ASSERT(nvlist_size(*errors, &size, NV_ENCODE_NATIVE) == 0); 95111022STom.Erickson@Sun.COM ASSERT(size <= zc->zc_nvlist_dst_size); 95211022STom.Erickson@Sun.COM } 95311022STom.Erickson@Sun.COM 95411022STom.Erickson@Sun.COM return (0); 95511022STom.Erickson@Sun.COM } 95611022STom.Erickson@Sun.COM 95711022STom.Erickson@Sun.COM static int 9582676Seschrock put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) 9592676Seschrock { 9602676Seschrock char *packed = NULL; 961*11807SSam.Falkner@Sun.COM int error = 0; 9622676Seschrock size_t size; 9632676Seschrock 9642676Seschrock VERIFY(nvlist_size(nvl, &size, NV_ENCODE_NATIVE) == 0); 9652676Seschrock 9662676Seschrock if (size > zc->zc_nvlist_dst_size) { 9672676Seschrock error = ENOMEM; 9682676Seschrock } else { 9694611Smarks packed = kmem_alloc(size, KM_SLEEP); 9702676Seschrock VERIFY(nvlist_pack(nvl, &packed, &size, NV_ENCODE_NATIVE, 9712676Seschrock KM_SLEEP) == 0); 972*11807SSam.Falkner@Sun.COM if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, 973*11807SSam.Falkner@Sun.COM size, zc->zc_iflags) != 0) 974*11807SSam.Falkner@Sun.COM error = EFAULT; 9752676Seschrock kmem_free(packed, size); 9762676Seschrock } 9772676Seschrock 9782676Seschrock zc->zc_nvlist_dst_size = size; 9792676Seschrock return (error); 9802676Seschrock } 9812676Seschrock 9822676Seschrock static int 98311185SSean.McEnroe@Sun.COM getzfsvfs(const char *dsname, zfsvfs_t **zfvp) 9849396SMatthew.Ahrens@Sun.COM { 9859396SMatthew.Ahrens@Sun.COM objset_t *os; 9869396SMatthew.Ahrens@Sun.COM int error; 9879396SMatthew.Ahrens@Sun.COM 98810298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(dsname, FTAG, &os); 9899396SMatthew.Ahrens@Sun.COM if (error) 9909396SMatthew.Ahrens@Sun.COM return (error); 99110298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 99210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 99310298SMatthew.Ahrens@Sun.COM return (EINVAL); 99410298SMatthew.Ahrens@Sun.COM } 99510298SMatthew.Ahrens@Sun.COM 99610298SMatthew.Ahrens@Sun.COM mutex_enter(&os->os_user_ptr_lock); 99711185SSean.McEnroe@Sun.COM *zfvp = dmu_objset_get_user(os); 99811185SSean.McEnroe@Sun.COM if (*zfvp) { 99911185SSean.McEnroe@Sun.COM VFS_HOLD((*zfvp)->z_vfs); 10009396SMatthew.Ahrens@Sun.COM } else { 10019396SMatthew.Ahrens@Sun.COM error = ESRCH; 10029396SMatthew.Ahrens@Sun.COM } 100310298SMatthew.Ahrens@Sun.COM mutex_exit(&os->os_user_ptr_lock); 100410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 10059396SMatthew.Ahrens@Sun.COM return (error); 10069396SMatthew.Ahrens@Sun.COM } 10079396SMatthew.Ahrens@Sun.COM 10089396SMatthew.Ahrens@Sun.COM /* 10099396SMatthew.Ahrens@Sun.COM * Find a zfsvfs_t for a mounted filesystem, or create our own, in which 10109396SMatthew.Ahrens@Sun.COM * case its z_vfs will be NULL, and it will be opened as the owner. 10119396SMatthew.Ahrens@Sun.COM */ 10129396SMatthew.Ahrens@Sun.COM static int 101311185SSean.McEnroe@Sun.COM zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp) 10149396SMatthew.Ahrens@Sun.COM { 10159396SMatthew.Ahrens@Sun.COM int error = 0; 10169396SMatthew.Ahrens@Sun.COM 101711185SSean.McEnroe@Sun.COM if (getzfsvfs(name, zfvp) != 0) 101811185SSean.McEnroe@Sun.COM error = zfsvfs_create(name, zfvp); 10199396SMatthew.Ahrens@Sun.COM if (error == 0) { 102011185SSean.McEnroe@Sun.COM rrw_enter(&(*zfvp)->z_teardown_lock, RW_READER, tag); 102111185SSean.McEnroe@Sun.COM if ((*zfvp)->z_unmounted) { 10229396SMatthew.Ahrens@Sun.COM /* 10239396SMatthew.Ahrens@Sun.COM * XXX we could probably try again, since the unmounting 10249396SMatthew.Ahrens@Sun.COM * thread should be just about to disassociate the 10259396SMatthew.Ahrens@Sun.COM * objset from the zfsvfs. 10269396SMatthew.Ahrens@Sun.COM */ 102711185SSean.McEnroe@Sun.COM rrw_exit(&(*zfvp)->z_teardown_lock, tag); 10289396SMatthew.Ahrens@Sun.COM return (EBUSY); 10299396SMatthew.Ahrens@Sun.COM } 10309396SMatthew.Ahrens@Sun.COM } 10319396SMatthew.Ahrens@Sun.COM return (error); 10329396SMatthew.Ahrens@Sun.COM } 10339396SMatthew.Ahrens@Sun.COM 10349396SMatthew.Ahrens@Sun.COM static void 10359396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) 10369396SMatthew.Ahrens@Sun.COM { 10379396SMatthew.Ahrens@Sun.COM rrw_exit(&zfsvfs->z_teardown_lock, tag); 10389396SMatthew.Ahrens@Sun.COM 10399396SMatthew.Ahrens@Sun.COM if (zfsvfs->z_vfs) { 10409396SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 10419396SMatthew.Ahrens@Sun.COM } else { 104210298SMatthew.Ahrens@Sun.COM dmu_objset_disown(zfsvfs->z_os, zfsvfs); 10439396SMatthew.Ahrens@Sun.COM zfsvfs_free(zfsvfs); 10449396SMatthew.Ahrens@Sun.COM } 10459396SMatthew.Ahrens@Sun.COM } 10469396SMatthew.Ahrens@Sun.COM 10479396SMatthew.Ahrens@Sun.COM static int 1048789Sahrens zfs_ioc_pool_create(zfs_cmd_t *zc) 1049789Sahrens { 1050789Sahrens int error; 10515094Slling nvlist_t *config, *props = NULL; 10527184Stimh nvlist_t *rootprops = NULL; 10537184Stimh nvlist_t *zplprops = NULL; 10544715Sek110237 char *buf; 1055789Sahrens 10565094Slling if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 10579643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) 10584988Sek110237 return (error); 10594715Sek110237 10605094Slling if (zc->zc_nvlist_src_size != 0 && (error = 10619643SEric.Taylor@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 10629643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) { 10635094Slling nvlist_free(config); 10645094Slling return (error); 10655094Slling } 10665094Slling 10677184Stimh if (props) { 10687184Stimh nvlist_t *nvl = NULL; 10697184Stimh uint64_t version = SPA_VERSION; 10707184Stimh 10717184Stimh (void) nvlist_lookup_uint64(props, 10727184Stimh zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); 10737184Stimh if (version < SPA_VERSION_INITIAL || version > SPA_VERSION) { 10747184Stimh error = EINVAL; 10757184Stimh goto pool_props_bad; 10767184Stimh } 10777184Stimh (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); 10787184Stimh if (nvl) { 10797184Stimh error = nvlist_dup(nvl, &rootprops, KM_SLEEP); 10807184Stimh if (error != 0) { 10817184Stimh nvlist_free(config); 10827184Stimh nvlist_free(props); 10837184Stimh return (error); 10847184Stimh } 10857184Stimh (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); 10867184Stimh } 10877184Stimh VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 10887184Stimh error = zfs_fill_zplprops_root(version, rootprops, 10897184Stimh zplprops, NULL); 10907184Stimh if (error) 10917184Stimh goto pool_props_bad; 10927184Stimh } 10937184Stimh 10944988Sek110237 buf = history_str_get(zc); 1095789Sahrens 10967184Stimh error = spa_create(zc->zc_name, config, props, buf, zplprops); 10977184Stimh 10987184Stimh /* 10997184Stimh * Set the remaining root properties 11007184Stimh */ 110111022STom.Erickson@Sun.COM if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, 110211022STom.Erickson@Sun.COM ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) 11037184Stimh (void) spa_destroy(zc->zc_name); 1104789Sahrens 11054988Sek110237 if (buf != NULL) 11064988Sek110237 history_str_free(buf); 11075094Slling 11087184Stimh pool_props_bad: 11097184Stimh nvlist_free(rootprops); 11107184Stimh nvlist_free(zplprops); 1111789Sahrens nvlist_free(config); 11127184Stimh nvlist_free(props); 11135094Slling 1114789Sahrens return (error); 1115789Sahrens } 1116789Sahrens 1117789Sahrens static int 1118789Sahrens zfs_ioc_pool_destroy(zfs_cmd_t *zc) 1119789Sahrens { 11204543Smarks int error; 11214543Smarks zfs_log_history(zc); 11224543Smarks error = spa_destroy(zc->zc_name); 112310588SEric.Taylor@Sun.COM if (error == 0) 112410588SEric.Taylor@Sun.COM zvol_remove_minors(zc->zc_name); 11254543Smarks return (error); 1126789Sahrens } 1127789Sahrens 1128789Sahrens static int 1129789Sahrens zfs_ioc_pool_import(zfs_cmd_t *zc) 1130789Sahrens { 11315094Slling nvlist_t *config, *props = NULL; 1132789Sahrens uint64_t guid; 113310921STim.Haley@Sun.COM int error; 1134789Sahrens 11355094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 11369643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) != 0) 1137789Sahrens return (error); 1138789Sahrens 11395094Slling if (zc->zc_nvlist_src_size != 0 && (error = 11409643SEric.Taylor@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 11419643SEric.Taylor@Sun.COM zc->zc_iflags, &props))) { 11425094Slling nvlist_free(config); 11435094Slling return (error); 11445094Slling } 11455094Slling 1146789Sahrens if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || 11471544Seschrock guid != zc->zc_guid) 1148789Sahrens error = EINVAL; 11496643Seschrock else if (zc->zc_cookie) 115010921STim.Haley@Sun.COM error = spa_import_verbatim(zc->zc_name, config, props); 1151789Sahrens else 11525094Slling error = spa_import(zc->zc_name, config, props); 1153789Sahrens 115410921STim.Haley@Sun.COM if (zc->zc_nvlist_dst != 0) 115510921STim.Haley@Sun.COM (void) put_nvlist(zc, config); 115610921STim.Haley@Sun.COM 1157789Sahrens nvlist_free(config); 1158789Sahrens 11595094Slling if (props) 11605094Slling nvlist_free(props); 11615094Slling 1162789Sahrens return (error); 1163789Sahrens } 1164789Sahrens 1165789Sahrens static int 1166789Sahrens zfs_ioc_pool_export(zfs_cmd_t *zc) 1167789Sahrens { 11684543Smarks int error; 11697214Slling boolean_t force = (boolean_t)zc->zc_cookie; 11708211SGeorge.Wilson@Sun.COM boolean_t hardforce = (boolean_t)zc->zc_guid; 11717214Slling 11724543Smarks zfs_log_history(zc); 11738211SGeorge.Wilson@Sun.COM error = spa_export(zc->zc_name, NULL, force, hardforce); 117410588SEric.Taylor@Sun.COM if (error == 0) 117510588SEric.Taylor@Sun.COM zvol_remove_minors(zc->zc_name); 11764543Smarks return (error); 1177789Sahrens } 1178789Sahrens 1179789Sahrens static int 1180789Sahrens zfs_ioc_pool_configs(zfs_cmd_t *zc) 1181789Sahrens { 1182789Sahrens nvlist_t *configs; 1183789Sahrens int error; 1184789Sahrens 1185789Sahrens if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) 1186789Sahrens return (EEXIST); 1187789Sahrens 11882676Seschrock error = put_nvlist(zc, configs); 1189789Sahrens 1190789Sahrens nvlist_free(configs); 1191789Sahrens 1192789Sahrens return (error); 1193789Sahrens } 1194789Sahrens 1195789Sahrens static int 1196789Sahrens zfs_ioc_pool_stats(zfs_cmd_t *zc) 1197789Sahrens { 1198789Sahrens nvlist_t *config; 1199789Sahrens int error; 12001544Seschrock int ret = 0; 1201789Sahrens 12022676Seschrock error = spa_get_stats(zc->zc_name, &config, zc->zc_value, 12032676Seschrock sizeof (zc->zc_value)); 1204789Sahrens 1205789Sahrens if (config != NULL) { 12062676Seschrock ret = put_nvlist(zc, config); 1207789Sahrens nvlist_free(config); 12081544Seschrock 12091544Seschrock /* 12101544Seschrock * The config may be present even if 'error' is non-zero. 12111544Seschrock * In this case we return success, and preserve the real errno 12121544Seschrock * in 'zc_cookie'. 12131544Seschrock */ 12141544Seschrock zc->zc_cookie = error; 1215789Sahrens } else { 12161544Seschrock ret = error; 1217789Sahrens } 1218789Sahrens 12191544Seschrock return (ret); 1220789Sahrens } 1221789Sahrens 1222789Sahrens /* 1223789Sahrens * Try to import the given pool, returning pool stats as appropriate so that 1224789Sahrens * user land knows which devices are available and overall pool health. 1225789Sahrens */ 1226789Sahrens static int 1227789Sahrens zfs_ioc_pool_tryimport(zfs_cmd_t *zc) 1228789Sahrens { 1229789Sahrens nvlist_t *tryconfig, *config; 1230789Sahrens int error; 1231789Sahrens 12325094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 12339643SEric.Taylor@Sun.COM zc->zc_iflags, &tryconfig)) != 0) 1234789Sahrens return (error); 1235789Sahrens 1236789Sahrens config = spa_tryimport(tryconfig); 1237789Sahrens 1238789Sahrens nvlist_free(tryconfig); 1239789Sahrens 1240789Sahrens if (config == NULL) 1241789Sahrens return (EINVAL); 1242789Sahrens 12432676Seschrock error = put_nvlist(zc, config); 1244789Sahrens nvlist_free(config); 1245789Sahrens 1246789Sahrens return (error); 1247789Sahrens } 1248789Sahrens 1249789Sahrens static int 1250789Sahrens zfs_ioc_pool_scrub(zfs_cmd_t *zc) 1251789Sahrens { 1252789Sahrens spa_t *spa; 1253789Sahrens int error; 1254789Sahrens 12552926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 12562926Sek110237 return (error); 12572926Sek110237 12587046Sahrens error = spa_scrub(spa, zc->zc_cookie); 12592926Sek110237 12602926Sek110237 spa_close(spa, FTAG); 12612926Sek110237 1262789Sahrens return (error); 1263789Sahrens } 1264789Sahrens 1265789Sahrens static int 1266789Sahrens zfs_ioc_pool_freeze(zfs_cmd_t *zc) 1267789Sahrens { 1268789Sahrens spa_t *spa; 1269789Sahrens int error; 1270789Sahrens 1271789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1272789Sahrens if (error == 0) { 1273789Sahrens spa_freeze(spa); 1274789Sahrens spa_close(spa, FTAG); 1275789Sahrens } 1276789Sahrens return (error); 1277789Sahrens } 1278789Sahrens 1279789Sahrens static int 12801760Seschrock zfs_ioc_pool_upgrade(zfs_cmd_t *zc) 12811760Seschrock { 12821760Seschrock spa_t *spa; 12831760Seschrock int error; 12841760Seschrock 12852926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 12862926Sek110237 return (error); 12872926Sek110237 12885118Slling if (zc->zc_cookie < spa_version(spa) || zc->zc_cookie > SPA_VERSION) { 12895118Slling spa_close(spa, FTAG); 12905118Slling return (EINVAL); 12915118Slling } 12925118Slling 12935094Slling spa_upgrade(spa, zc->zc_cookie); 12942926Sek110237 spa_close(spa, FTAG); 12952926Sek110237 12962926Sek110237 return (error); 12972926Sek110237 } 12982926Sek110237 12992926Sek110237 static int 13002926Sek110237 zfs_ioc_pool_get_history(zfs_cmd_t *zc) 13012926Sek110237 { 13022926Sek110237 spa_t *spa; 13032926Sek110237 char *hist_buf; 13042926Sek110237 uint64_t size; 13052926Sek110237 int error; 13062926Sek110237 13072926Sek110237 if ((size = zc->zc_history_len) == 0) 13082926Sek110237 return (EINVAL); 13092926Sek110237 13102926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 13112926Sek110237 return (error); 13122926Sek110237 13134577Sahrens if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { 13143863Sek110237 spa_close(spa, FTAG); 13153863Sek110237 return (ENOTSUP); 13163863Sek110237 } 13173863Sek110237 13182926Sek110237 hist_buf = kmem_alloc(size, KM_SLEEP); 13192926Sek110237 if ((error = spa_history_get(spa, &zc->zc_history_offset, 13202926Sek110237 &zc->zc_history_len, hist_buf)) == 0) { 13219643SEric.Taylor@Sun.COM error = ddi_copyout(hist_buf, 13229643SEric.Taylor@Sun.COM (void *)(uintptr_t)zc->zc_history, 13239643SEric.Taylor@Sun.COM zc->zc_history_len, zc->zc_iflags); 13242926Sek110237 } 13252926Sek110237 13262926Sek110237 spa_close(spa, FTAG); 13272926Sek110237 kmem_free(hist_buf, size); 13282926Sek110237 return (error); 13292926Sek110237 } 13302926Sek110237 13312926Sek110237 static int 13323444Sek110237 zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) 13333444Sek110237 { 13343444Sek110237 int error; 13353444Sek110237 13363912Slling if (error = dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)) 13373444Sek110237 return (error); 13383444Sek110237 13393444Sek110237 return (0); 13403444Sek110237 } 13413444Sek110237 134210298SMatthew.Ahrens@Sun.COM /* 134310298SMatthew.Ahrens@Sun.COM * inputs: 134410298SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 134510298SMatthew.Ahrens@Sun.COM * zc_obj object to find 134610298SMatthew.Ahrens@Sun.COM * 134710298SMatthew.Ahrens@Sun.COM * outputs: 134810298SMatthew.Ahrens@Sun.COM * zc_value name of object 134910298SMatthew.Ahrens@Sun.COM */ 13503444Sek110237 static int 13513444Sek110237 zfs_ioc_obj_to_path(zfs_cmd_t *zc) 13523444Sek110237 { 135310298SMatthew.Ahrens@Sun.COM objset_t *os; 13543444Sek110237 int error; 13553444Sek110237 135610298SMatthew.Ahrens@Sun.COM /* XXX reading from objset not owned */ 135710298SMatthew.Ahrens@Sun.COM if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0) 13583444Sek110237 return (error); 135910298SMatthew.Ahrens@Sun.COM if (dmu_objset_type(os) != DMU_OST_ZFS) { 136010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 136110298SMatthew.Ahrens@Sun.COM return (EINVAL); 136210298SMatthew.Ahrens@Sun.COM } 136310298SMatthew.Ahrens@Sun.COM error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, 13643444Sek110237 sizeof (zc->zc_value)); 136510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 13663444Sek110237 13673444Sek110237 return (error); 13683444Sek110237 } 13693444Sek110237 13703444Sek110237 static int 1371789Sahrens zfs_ioc_vdev_add(zfs_cmd_t *zc) 1372789Sahrens { 1373789Sahrens spa_t *spa; 1374789Sahrens int error; 13756423Sgw25295 nvlist_t *config, **l2cache, **spares; 13766423Sgw25295 uint_t nl2cache = 0, nspares = 0; 1377789Sahrens 1378789Sahrens error = spa_open(zc->zc_name, &spa, FTAG); 1379789Sahrens if (error != 0) 1380789Sahrens return (error); 1381789Sahrens 13825450Sbrendan error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 13839643SEric.Taylor@Sun.COM zc->zc_iflags, &config); 13845450Sbrendan (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE, 13855450Sbrendan &l2cache, &nl2cache); 13865450Sbrendan 13876423Sgw25295 (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES, 13886423Sgw25295 &spares, &nspares); 13896423Sgw25295 13903912Slling /* 13913912Slling * A root pool with concatenated devices is not supported. 13926423Sgw25295 * Thus, can not add a device to a root pool. 13936423Sgw25295 * 13946423Sgw25295 * Intent log device can not be added to a rootpool because 13956423Sgw25295 * during mountroot, zil is replayed, a seperated log device 13966423Sgw25295 * can not be accessed during the mountroot time. 13976423Sgw25295 * 13986423Sgw25295 * l2cache and spare devices are ok to be added to a rootpool. 13993912Slling */ 140010922SJeff.Bonwick@Sun.COM if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) { 140111422SMark.Musante@Sun.COM nvlist_free(config); 14023912Slling spa_close(spa, FTAG); 14033912Slling return (EDOM); 14043912Slling } 14053912Slling 14065450Sbrendan if (error == 0) { 1407789Sahrens error = spa_vdev_add(spa, config); 1408789Sahrens nvlist_free(config); 1409789Sahrens } 1410789Sahrens spa_close(spa, FTAG); 1411789Sahrens return (error); 1412789Sahrens } 1413789Sahrens 1414789Sahrens static int 1415789Sahrens zfs_ioc_vdev_remove(zfs_cmd_t *zc) 1416789Sahrens { 14172082Seschrock spa_t *spa; 14182082Seschrock int error; 14192082Seschrock 14202082Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 14212082Seschrock if (error != 0) 14222082Seschrock return (error); 14232082Seschrock error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); 14242082Seschrock spa_close(spa, FTAG); 14252082Seschrock return (error); 1426789Sahrens } 1427789Sahrens 1428789Sahrens static int 14294451Seschrock zfs_ioc_vdev_set_state(zfs_cmd_t *zc) 1430789Sahrens { 1431789Sahrens spa_t *spa; 1432789Sahrens int error; 14334451Seschrock vdev_state_t newstate = VDEV_STATE_UNKNOWN; 1434789Sahrens 14352926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1436789Sahrens return (error); 14374451Seschrock switch (zc->zc_cookie) { 14384451Seschrock case VDEV_STATE_ONLINE: 14394451Seschrock error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); 14404451Seschrock break; 14414451Seschrock 14424451Seschrock case VDEV_STATE_OFFLINE: 14434451Seschrock error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); 14444451Seschrock break; 1445789Sahrens 14464451Seschrock case VDEV_STATE_FAULTED: 144710817SEric.Schrock@Sun.COM if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 144810817SEric.Schrock@Sun.COM zc->zc_obj != VDEV_AUX_EXTERNAL) 144910817SEric.Schrock@Sun.COM zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 145010817SEric.Schrock@Sun.COM 145110817SEric.Schrock@Sun.COM error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); 14524451Seschrock break; 1453789Sahrens 14544451Seschrock case VDEV_STATE_DEGRADED: 145510817SEric.Schrock@Sun.COM if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && 145610817SEric.Schrock@Sun.COM zc->zc_obj != VDEV_AUX_EXTERNAL) 145710817SEric.Schrock@Sun.COM zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; 145810817SEric.Schrock@Sun.COM 145910817SEric.Schrock@Sun.COM error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); 14604451Seschrock break; 14614451Seschrock 14624451Seschrock default: 14634451Seschrock error = EINVAL; 14644451Seschrock } 14654451Seschrock zc->zc_cookie = newstate; 1466789Sahrens spa_close(spa, FTAG); 1467789Sahrens return (error); 1468789Sahrens } 1469789Sahrens 1470789Sahrens static int 1471789Sahrens zfs_ioc_vdev_attach(zfs_cmd_t *zc) 1472789Sahrens { 1473789Sahrens spa_t *spa; 1474789Sahrens int replacing = zc->zc_cookie; 1475789Sahrens nvlist_t *config; 1476789Sahrens int error; 1477789Sahrens 14782926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1479789Sahrens return (error); 1480789Sahrens 14815094Slling if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 14829643SEric.Taylor@Sun.COM zc->zc_iflags, &config)) == 0) { 14831544Seschrock error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); 1484789Sahrens nvlist_free(config); 1485789Sahrens } 1486789Sahrens 1487789Sahrens spa_close(spa, FTAG); 1488789Sahrens return (error); 1489789Sahrens } 1490789Sahrens 1491789Sahrens static int 1492789Sahrens zfs_ioc_vdev_detach(zfs_cmd_t *zc) 1493789Sahrens { 1494789Sahrens spa_t *spa; 1495789Sahrens int error; 1496789Sahrens 14972926Sek110237 if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 1498789Sahrens return (error); 1499789Sahrens 15008241SJeff.Bonwick@Sun.COM error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); 1501789Sahrens 1502789Sahrens spa_close(spa, FTAG); 1503789Sahrens return (error); 1504789Sahrens } 1505789Sahrens 1506789Sahrens static int 150711422SMark.Musante@Sun.COM zfs_ioc_vdev_split(zfs_cmd_t *zc) 150811422SMark.Musante@Sun.COM { 150911422SMark.Musante@Sun.COM spa_t *spa; 151011422SMark.Musante@Sun.COM nvlist_t *config, *props = NULL; 151111422SMark.Musante@Sun.COM int error; 151211422SMark.Musante@Sun.COM boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); 151311422SMark.Musante@Sun.COM 151411422SMark.Musante@Sun.COM if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 151511422SMark.Musante@Sun.COM return (error); 151611422SMark.Musante@Sun.COM 151711422SMark.Musante@Sun.COM if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, 151811422SMark.Musante@Sun.COM zc->zc_iflags, &config)) { 151911422SMark.Musante@Sun.COM spa_close(spa, FTAG); 152011422SMark.Musante@Sun.COM return (error); 152111422SMark.Musante@Sun.COM } 152211422SMark.Musante@Sun.COM 152311422SMark.Musante@Sun.COM if (zc->zc_nvlist_src_size != 0 && (error = 152411422SMark.Musante@Sun.COM get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 152511422SMark.Musante@Sun.COM zc->zc_iflags, &props))) { 152611422SMark.Musante@Sun.COM spa_close(spa, FTAG); 152711422SMark.Musante@Sun.COM nvlist_free(config); 152811422SMark.Musante@Sun.COM return (error); 152911422SMark.Musante@Sun.COM } 153011422SMark.Musante@Sun.COM 153111422SMark.Musante@Sun.COM error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); 153211422SMark.Musante@Sun.COM 153311422SMark.Musante@Sun.COM spa_close(spa, FTAG); 153411422SMark.Musante@Sun.COM 153511422SMark.Musante@Sun.COM nvlist_free(config); 153611422SMark.Musante@Sun.COM nvlist_free(props); 153711422SMark.Musante@Sun.COM 153811422SMark.Musante@Sun.COM return (error); 153911422SMark.Musante@Sun.COM } 154011422SMark.Musante@Sun.COM 154111422SMark.Musante@Sun.COM static int 15421354Seschrock zfs_ioc_vdev_setpath(zfs_cmd_t *zc) 15431354Seschrock { 15441354Seschrock spa_t *spa; 15452676Seschrock char *path = zc->zc_value; 15461544Seschrock uint64_t guid = zc->zc_guid; 15471354Seschrock int error; 15481354Seschrock 15491354Seschrock error = spa_open(zc->zc_name, &spa, FTAG); 15501354Seschrock if (error != 0) 15511354Seschrock return (error); 15521354Seschrock 15531354Seschrock error = spa_vdev_setpath(spa, guid, path); 15541354Seschrock spa_close(spa, FTAG); 15551354Seschrock return (error); 15561354Seschrock } 15571354Seschrock 15589425SEric.Schrock@Sun.COM static int 15599425SEric.Schrock@Sun.COM zfs_ioc_vdev_setfru(zfs_cmd_t *zc) 15609425SEric.Schrock@Sun.COM { 15619425SEric.Schrock@Sun.COM spa_t *spa; 15629425SEric.Schrock@Sun.COM char *fru = zc->zc_value; 15639425SEric.Schrock@Sun.COM uint64_t guid = zc->zc_guid; 15649425SEric.Schrock@Sun.COM int error; 15659425SEric.Schrock@Sun.COM 15669425SEric.Schrock@Sun.COM error = spa_open(zc->zc_name, &spa, FTAG); 15679425SEric.Schrock@Sun.COM if (error != 0) 15689425SEric.Schrock@Sun.COM return (error); 15699425SEric.Schrock@Sun.COM 15709425SEric.Schrock@Sun.COM error = spa_vdev_setfru(spa, guid, fru); 15719425SEric.Schrock@Sun.COM spa_close(spa, FTAG); 15729425SEric.Schrock@Sun.COM return (error); 15739425SEric.Schrock@Sun.COM } 15749425SEric.Schrock@Sun.COM 15755367Sahrens /* 15765367Sahrens * inputs: 15775367Sahrens * zc_name name of filesystem 15785367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 15795367Sahrens * 15805367Sahrens * outputs: 15815367Sahrens * zc_objset_stats stats 15825367Sahrens * zc_nvlist_dst property nvlist 15835367Sahrens * zc_nvlist_dst_size size of property nvlist 15845367Sahrens */ 15851354Seschrock static int 1586789Sahrens zfs_ioc_objset_stats(zfs_cmd_t *zc) 1587789Sahrens { 1588789Sahrens objset_t *os = NULL; 1589789Sahrens int error; 15901356Seschrock nvlist_t *nv; 1591789Sahrens 159210298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 1593789Sahrens return (error); 1594789Sahrens 15952885Sahrens dmu_objset_fast_stat(os, &zc->zc_objset_stats); 1596789Sahrens 15972856Snd150628 if (zc->zc_nvlist_dst != 0 && 159811022STom.Erickson@Sun.COM (error = dsl_prop_get_all(os, &nv)) == 0) { 15992885Sahrens dmu_objset_stats(os, nv); 16003087Sahrens /* 16015147Srm160521 * NB: zvol_get_stats() will read the objset contents, 16023087Sahrens * which we aren't supposed to do with a 16036689Smaybee * DS_MODE_USER hold, because it could be 16043087Sahrens * inconsistent. So this is a bit of a workaround... 160510298SMatthew.Ahrens@Sun.COM * XXX reading with out owning 16063087Sahrens */ 16074577Sahrens if (!zc->zc_objset_stats.dds_inconsistent) { 16084577Sahrens if (dmu_objset_type(os) == DMU_OST_ZVOL) 16094577Sahrens VERIFY(zvol_get_stats(os, nv) == 0); 16104577Sahrens } 16112676Seschrock error = put_nvlist(zc, nv); 16121356Seschrock nvlist_free(nv); 16131356Seschrock } 1614789Sahrens 161510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1616789Sahrens return (error); 1617789Sahrens } 1618789Sahrens 161911022STom.Erickson@Sun.COM /* 162011022STom.Erickson@Sun.COM * inputs: 162111022STom.Erickson@Sun.COM * zc_name name of filesystem 162211022STom.Erickson@Sun.COM * zc_nvlist_dst_size size of buffer for property nvlist 162311022STom.Erickson@Sun.COM * 162411022STom.Erickson@Sun.COM * outputs: 162511022STom.Erickson@Sun.COM * zc_nvlist_dst received property nvlist 162611022STom.Erickson@Sun.COM * zc_nvlist_dst_size size of received property nvlist 162711022STom.Erickson@Sun.COM * 162811022STom.Erickson@Sun.COM * Gets received properties (distinct from local properties on or after 162911022STom.Erickson@Sun.COM * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from 163011022STom.Erickson@Sun.COM * local property values. 163111022STom.Erickson@Sun.COM */ 163211022STom.Erickson@Sun.COM static int 163311022STom.Erickson@Sun.COM zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) 163411022STom.Erickson@Sun.COM { 163511022STom.Erickson@Sun.COM objset_t *os = NULL; 163611022STom.Erickson@Sun.COM int error; 163711022STom.Erickson@Sun.COM nvlist_t *nv; 163811022STom.Erickson@Sun.COM 163911022STom.Erickson@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) 164011022STom.Erickson@Sun.COM return (error); 164111022STom.Erickson@Sun.COM 164211022STom.Erickson@Sun.COM /* 164311022STom.Erickson@Sun.COM * Without this check, we would return local property values if the 164411022STom.Erickson@Sun.COM * caller has not already received properties on or after 164511022STom.Erickson@Sun.COM * SPA_VERSION_RECVD_PROPS. 164611022STom.Erickson@Sun.COM */ 164711022STom.Erickson@Sun.COM if (!dsl_prop_get_hasrecvd(os)) { 164811022STom.Erickson@Sun.COM dmu_objset_rele(os, FTAG); 164911022STom.Erickson@Sun.COM return (ENOTSUP); 165011022STom.Erickson@Sun.COM } 165111022STom.Erickson@Sun.COM 165211022STom.Erickson@Sun.COM if (zc->zc_nvlist_dst != 0 && 165311022STom.Erickson@Sun.COM (error = dsl_prop_get_received(os, &nv)) == 0) { 165411022STom.Erickson@Sun.COM error = put_nvlist(zc, nv); 165511022STom.Erickson@Sun.COM nvlist_free(nv); 165611022STom.Erickson@Sun.COM } 165711022STom.Erickson@Sun.COM 165811022STom.Erickson@Sun.COM dmu_objset_rele(os, FTAG); 165911022STom.Erickson@Sun.COM return (error); 166011022STom.Erickson@Sun.COM } 166111022STom.Erickson@Sun.COM 16625498Stimh static int 16635498Stimh nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) 16645498Stimh { 16655498Stimh uint64_t value; 16665498Stimh int error; 16675498Stimh 16685498Stimh /* 16695498Stimh * zfs_get_zplprop() will either find a value or give us 16705498Stimh * the default value (if there is one). 16715498Stimh */ 16725498Stimh if ((error = zfs_get_zplprop(os, prop, &value)) != 0) 16735498Stimh return (error); 16745498Stimh VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); 16755498Stimh return (0); 16765498Stimh } 16775498Stimh 16785498Stimh /* 16795498Stimh * inputs: 16805498Stimh * zc_name name of filesystem 16815498Stimh * zc_nvlist_dst_size size of buffer for zpl property nvlist 16825498Stimh * 16835498Stimh * outputs: 16845498Stimh * zc_nvlist_dst zpl property nvlist 16855498Stimh * zc_nvlist_dst_size size of zpl property nvlist 16865498Stimh */ 16875498Stimh static int 16885498Stimh zfs_ioc_objset_zplprops(zfs_cmd_t *zc) 16895498Stimh { 16905498Stimh objset_t *os; 16915498Stimh int err; 16925498Stimh 169310298SMatthew.Ahrens@Sun.COM /* XXX reading without owning */ 169410298SMatthew.Ahrens@Sun.COM if (err = dmu_objset_hold(zc->zc_name, FTAG, &os)) 16955498Stimh return (err); 16965498Stimh 16975498Stimh dmu_objset_fast_stat(os, &zc->zc_objset_stats); 16985498Stimh 16995498Stimh /* 17005498Stimh * NB: nvl_add_zplprop() will read the objset contents, 17016689Smaybee * which we aren't supposed to do with a DS_MODE_USER 17026689Smaybee * hold, because it could be inconsistent. 17035498Stimh */ 17045498Stimh if (zc->zc_nvlist_dst != NULL && 17055498Stimh !zc->zc_objset_stats.dds_inconsistent && 17065498Stimh dmu_objset_type(os) == DMU_OST_ZFS) { 17075498Stimh nvlist_t *nv; 17085498Stimh 17095498Stimh VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); 17105498Stimh if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && 17115498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && 17125498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && 17135498Stimh (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) 17145498Stimh err = put_nvlist(zc, nv); 17155498Stimh nvlist_free(nv); 17165498Stimh } else { 17175498Stimh err = ENOENT; 17185498Stimh } 171910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 17205498Stimh return (err); 17215498Stimh } 17225498Stimh 17239396SMatthew.Ahrens@Sun.COM static boolean_t 17249396SMatthew.Ahrens@Sun.COM dataset_name_hidden(const char *name) 17259396SMatthew.Ahrens@Sun.COM { 17269396SMatthew.Ahrens@Sun.COM /* 17279396SMatthew.Ahrens@Sun.COM * Skip over datasets that are not visible in this zone, 17289396SMatthew.Ahrens@Sun.COM * internal datasets (which have a $ in their name), and 17299396SMatthew.Ahrens@Sun.COM * temporary datasets (which have a % in their name). 17309396SMatthew.Ahrens@Sun.COM */ 17319396SMatthew.Ahrens@Sun.COM if (strchr(name, '$') != NULL) 17329396SMatthew.Ahrens@Sun.COM return (B_TRUE); 17339396SMatthew.Ahrens@Sun.COM if (strchr(name, '%') != NULL) 17349396SMatthew.Ahrens@Sun.COM return (B_TRUE); 17359396SMatthew.Ahrens@Sun.COM if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) 17369396SMatthew.Ahrens@Sun.COM return (B_TRUE); 17379396SMatthew.Ahrens@Sun.COM return (B_FALSE); 17389396SMatthew.Ahrens@Sun.COM } 17399396SMatthew.Ahrens@Sun.COM 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 filesystem 17489396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 17495367Sahrens * zc_objset_stats stats 17505367Sahrens * zc_nvlist_dst property nvlist 17515367Sahrens * zc_nvlist_dst_size size of property nvlist 17525367Sahrens */ 1753789Sahrens static int 1754789Sahrens zfs_ioc_dataset_list_next(zfs_cmd_t *zc) 1755789Sahrens { 1756885Sahrens objset_t *os; 1757789Sahrens int error; 1758789Sahrens char *p; 175911546SChris.Kirby@sun.com size_t orig_len = strlen(zc->zc_name); 176011546SChris.Kirby@sun.com 176111546SChris.Kirby@sun.com top: 176210298SMatthew.Ahrens@Sun.COM if (error = dmu_objset_hold(zc->zc_name, FTAG, &os)) { 1763885Sahrens if (error == ENOENT) 1764885Sahrens error = ESRCH; 1765885Sahrens return (error); 1766789Sahrens } 1767789Sahrens 1768789Sahrens p = strrchr(zc->zc_name, '/'); 1769789Sahrens if (p == NULL || p[1] != '\0') 1770789Sahrens (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); 1771789Sahrens p = zc->zc_name + strlen(zc->zc_name); 1772789Sahrens 17738697SRichard.Morris@Sun.COM /* 17748697SRichard.Morris@Sun.COM * Pre-fetch the datasets. dmu_objset_prefetch() always returns 0 17758697SRichard.Morris@Sun.COM * but is not declared void because its called by dmu_objset_find(). 17768697SRichard.Morris@Sun.COM */ 17778415SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) { 17788415SRichard.Morris@Sun.COM uint64_t cookie = 0; 17798415SRichard.Morris@Sun.COM int len = sizeof (zc->zc_name) - (p - zc->zc_name); 17808415SRichard.Morris@Sun.COM 17818415SRichard.Morris@Sun.COM while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) 17828697SRichard.Morris@Sun.COM (void) dmu_objset_prefetch(p, NULL); 17838415SRichard.Morris@Sun.COM } 17848415SRichard.Morris@Sun.COM 1785789Sahrens do { 1786885Sahrens error = dmu_dir_list_next(os, 1787885Sahrens sizeof (zc->zc_name) - (p - zc->zc_name), p, 1788885Sahrens NULL, &zc->zc_cookie); 1789789Sahrens if (error == ENOENT) 1790789Sahrens error = ESRCH; 179110588SEric.Taylor@Sun.COM } while (error == 0 && dataset_name_hidden(zc->zc_name) && 179210588SEric.Taylor@Sun.COM !(zc->zc_iflags & FKIOCTL)); 179310298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 1794789Sahrens 179510588SEric.Taylor@Sun.COM /* 179610588SEric.Taylor@Sun.COM * If it's an internal dataset (ie. with a '$' in its name), 179710588SEric.Taylor@Sun.COM * don't try to get stats for it, otherwise we'll return ENOENT. 179810588SEric.Taylor@Sun.COM */ 179911546SChris.Kirby@sun.com if (error == 0 && strchr(zc->zc_name, '$') == NULL) { 1800885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 180111546SChris.Kirby@sun.com if (error == ENOENT) { 180211546SChris.Kirby@sun.com /* We lost a race with destroy, get the next one. */ 180311546SChris.Kirby@sun.com zc->zc_name[orig_len] = '\0'; 180411546SChris.Kirby@sun.com goto top; 180511546SChris.Kirby@sun.com } 180611546SChris.Kirby@sun.com } 1807789Sahrens return (error); 1808789Sahrens } 1809789Sahrens 18105367Sahrens /* 18115367Sahrens * inputs: 18125367Sahrens * zc_name name of filesystem 18135367Sahrens * zc_cookie zap cursor 18145367Sahrens * zc_nvlist_dst_size size of buffer for property nvlist 18155367Sahrens * 18165367Sahrens * outputs: 18175367Sahrens * zc_name name of next snapshot 18185367Sahrens * zc_objset_stats stats 18195367Sahrens * zc_nvlist_dst property nvlist 18205367Sahrens * zc_nvlist_dst_size size of property nvlist 18215367Sahrens */ 1822789Sahrens static int 1823789Sahrens zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) 1824789Sahrens { 1825885Sahrens objset_t *os; 1826789Sahrens int error; 1827789Sahrens 182811546SChris.Kirby@sun.com top: 182910474SRichard.Morris@Sun.COM if (zc->zc_cookie == 0) 183010474SRichard.Morris@Sun.COM (void) dmu_objset_find(zc->zc_name, dmu_objset_prefetch, 183110474SRichard.Morris@Sun.COM NULL, DS_FIND_SNAPSHOTS); 183210474SRichard.Morris@Sun.COM 183310298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 18346689Smaybee if (error) 18356689Smaybee return (error == ENOENT ? ESRCH : error); 1836789Sahrens 18371003Slling /* 18381003Slling * A dataset name of maximum length cannot have any snapshots, 18391003Slling * so exit immediately. 18401003Slling */ 18411003Slling if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= MAXNAMELEN) { 184210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 18431003Slling return (ESRCH); 1844789Sahrens } 1845789Sahrens 1846885Sahrens error = dmu_snapshot_list_next(os, 1847885Sahrens sizeof (zc->zc_name) - strlen(zc->zc_name), 18485663Sck153898 zc->zc_name + strlen(zc->zc_name), NULL, &zc->zc_cookie, NULL); 184910298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 185011546SChris.Kirby@sun.com if (error == 0) { 1851885Sahrens error = zfs_ioc_objset_stats(zc); /* fill in the stats */ 185211546SChris.Kirby@sun.com if (error == ENOENT) { 185311546SChris.Kirby@sun.com /* We lost a race with destroy, get the next one. */ 185411546SChris.Kirby@sun.com *strchr(zc->zc_name, '@') = '\0'; 185511546SChris.Kirby@sun.com goto top; 185611546SChris.Kirby@sun.com } 185711546SChris.Kirby@sun.com } else if (error == ENOENT) { 18586689Smaybee error = ESRCH; 185911546SChris.Kirby@sun.com } 1860789Sahrens 18615367Sahrens /* if we failed, undo the @ that we tacked on to zc_name */ 18626689Smaybee if (error) 18635367Sahrens *strchr(zc->zc_name, '@') = '\0'; 1864789Sahrens return (error); 1865789Sahrens } 1866789Sahrens 186711022STom.Erickson@Sun.COM static int 186811022STom.Erickson@Sun.COM zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) 186911022STom.Erickson@Sun.COM { 187011022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 187111022STom.Erickson@Sun.COM uint64_t *valary; 187211022STom.Erickson@Sun.COM unsigned int vallen; 187311022STom.Erickson@Sun.COM const char *domain; 187411022STom.Erickson@Sun.COM zfs_userquota_prop_t type; 187511022STom.Erickson@Sun.COM uint64_t rid; 187611022STom.Erickson@Sun.COM uint64_t quota; 187711022STom.Erickson@Sun.COM zfsvfs_t *zfsvfs; 187811022STom.Erickson@Sun.COM int err; 187911022STom.Erickson@Sun.COM 188011022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 188111022STom.Erickson@Sun.COM nvlist_t *attrs; 188211022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 188311022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 188411022STom.Erickson@Sun.COM &pair) == 0); 188511022STom.Erickson@Sun.COM } 188611022STom.Erickson@Sun.COM 188711022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64_array(pair, &valary, &vallen) == 0); 188811022STom.Erickson@Sun.COM VERIFY(vallen == 3); 188911022STom.Erickson@Sun.COM type = valary[0]; 189011022STom.Erickson@Sun.COM rid = valary[1]; 189111022STom.Erickson@Sun.COM quota = valary[2]; 189211022STom.Erickson@Sun.COM /* 189311022STom.Erickson@Sun.COM * The propname is encoded as 189411022STom.Erickson@Sun.COM * userquota@<rid>-<domain>. 189511022STom.Erickson@Sun.COM */ 189611022STom.Erickson@Sun.COM domain = strchr(propname, '-') + 1; 189711022STom.Erickson@Sun.COM 189811022STom.Erickson@Sun.COM err = zfsvfs_hold(dsname, FTAG, &zfsvfs); 189911022STom.Erickson@Sun.COM if (err == 0) { 190011022STom.Erickson@Sun.COM err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); 190111022STom.Erickson@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 190211022STom.Erickson@Sun.COM } 190311022STom.Erickson@Sun.COM 190411022STom.Erickson@Sun.COM return (err); 190511022STom.Erickson@Sun.COM } 190611022STom.Erickson@Sun.COM 190711022STom.Erickson@Sun.COM /* 190811022STom.Erickson@Sun.COM * If the named property is one that has a special function to set its value, 190911022STom.Erickson@Sun.COM * return 0 on success and a positive error code on failure; otherwise if it is 191011022STom.Erickson@Sun.COM * not one of the special properties handled by this function, return -1. 191111022STom.Erickson@Sun.COM * 191211022STom.Erickson@Sun.COM * XXX: It would be better for callers of the properety interface if we handled 191311022STom.Erickson@Sun.COM * these special cases in dsl_prop.c (in the dsl layer). 191411022STom.Erickson@Sun.COM */ 191511022STom.Erickson@Sun.COM static int 191611022STom.Erickson@Sun.COM zfs_prop_set_special(const char *dsname, zprop_source_t source, 191711022STom.Erickson@Sun.COM nvpair_t *pair) 1918789Sahrens { 191911022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 192011022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(propname); 192111022STom.Erickson@Sun.COM uint64_t intval; 192211022STom.Erickson@Sun.COM int err; 192311022STom.Erickson@Sun.COM 192411022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 192511022STom.Erickson@Sun.COM if (zfs_prop_userquota(propname)) 192611022STom.Erickson@Sun.COM return (zfs_prop_set_userquota(dsname, pair)); 192711022STom.Erickson@Sun.COM return (-1); 192811022STom.Erickson@Sun.COM } 192911022STom.Erickson@Sun.COM 193011022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 193111022STom.Erickson@Sun.COM nvlist_t *attrs; 193211022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 193311022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 193411022STom.Erickson@Sun.COM &pair) == 0); 193511022STom.Erickson@Sun.COM } 193611022STom.Erickson@Sun.COM 193711022STom.Erickson@Sun.COM if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) 193811022STom.Erickson@Sun.COM return (-1); 193911022STom.Erickson@Sun.COM 194011022STom.Erickson@Sun.COM VERIFY(0 == nvpair_value_uint64(pair, &intval)); 194111022STom.Erickson@Sun.COM 194211022STom.Erickson@Sun.COM switch (prop) { 194311022STom.Erickson@Sun.COM case ZFS_PROP_QUOTA: 194411022STom.Erickson@Sun.COM err = dsl_dir_set_quota(dsname, source, intval); 194511022STom.Erickson@Sun.COM break; 194611022STom.Erickson@Sun.COM case ZFS_PROP_REFQUOTA: 194711022STom.Erickson@Sun.COM err = dsl_dataset_set_quota(dsname, source, intval); 194811022STom.Erickson@Sun.COM break; 194911022STom.Erickson@Sun.COM case ZFS_PROP_RESERVATION: 195011022STom.Erickson@Sun.COM err = dsl_dir_set_reservation(dsname, source, intval); 195111022STom.Erickson@Sun.COM break; 195211022STom.Erickson@Sun.COM case ZFS_PROP_REFRESERVATION: 195311022STom.Erickson@Sun.COM err = dsl_dataset_set_reservation(dsname, source, intval); 195411022STom.Erickson@Sun.COM break; 195511022STom.Erickson@Sun.COM case ZFS_PROP_VOLSIZE: 195611022STom.Erickson@Sun.COM err = zvol_set_volsize(dsname, ddi_driver_major(zfs_dip), 195711022STom.Erickson@Sun.COM intval); 195811022STom.Erickson@Sun.COM break; 195911022STom.Erickson@Sun.COM case ZFS_PROP_VERSION: 196011022STom.Erickson@Sun.COM { 196111022STom.Erickson@Sun.COM zfsvfs_t *zfsvfs; 196211022STom.Erickson@Sun.COM 196311022STom.Erickson@Sun.COM if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs)) != 0) 196411022STom.Erickson@Sun.COM break; 196511022STom.Erickson@Sun.COM 196611022STom.Erickson@Sun.COM err = zfs_set_version(zfsvfs, intval); 196711022STom.Erickson@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 196811022STom.Erickson@Sun.COM 196911022STom.Erickson@Sun.COM if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { 197011147SGeorge.Wilson@Sun.COM zfs_cmd_t *zc; 197111147SGeorge.Wilson@Sun.COM 197211147SGeorge.Wilson@Sun.COM zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 197311147SGeorge.Wilson@Sun.COM (void) strcpy(zc->zc_name, dsname); 197411147SGeorge.Wilson@Sun.COM (void) zfs_ioc_userspace_upgrade(zc); 197511147SGeorge.Wilson@Sun.COM kmem_free(zc, sizeof (zfs_cmd_t)); 197611022STom.Erickson@Sun.COM } 197711022STom.Erickson@Sun.COM break; 197811022STom.Erickson@Sun.COM } 197911022STom.Erickson@Sun.COM 198011022STom.Erickson@Sun.COM default: 198111022STom.Erickson@Sun.COM err = -1; 198211022STom.Erickson@Sun.COM } 198311022STom.Erickson@Sun.COM 198411022STom.Erickson@Sun.COM return (err); 198511022STom.Erickson@Sun.COM } 198611022STom.Erickson@Sun.COM 198711022STom.Erickson@Sun.COM /* 198811022STom.Erickson@Sun.COM * This function is best effort. If it fails to set any of the given properties, 198911022STom.Erickson@Sun.COM * it continues to set as many as it can and returns the first error 199011022STom.Erickson@Sun.COM * encountered. If the caller provides a non-NULL errlist, it also gives the 199111022STom.Erickson@Sun.COM * complete list of names of all the properties it failed to set along with the 199211022STom.Erickson@Sun.COM * corresponding error numbers. The caller is responsible for freeing the 199311022STom.Erickson@Sun.COM * returned errlist. 199411022STom.Erickson@Sun.COM * 199511022STom.Erickson@Sun.COM * If every property is set successfully, zero is returned and the list pointed 199611022STom.Erickson@Sun.COM * at by errlist is NULL. 199711022STom.Erickson@Sun.COM */ 199811022STom.Erickson@Sun.COM int 199911022STom.Erickson@Sun.COM zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, 200011022STom.Erickson@Sun.COM nvlist_t **errlist) 200111022STom.Erickson@Sun.COM { 200211022STom.Erickson@Sun.COM nvpair_t *pair; 200311022STom.Erickson@Sun.COM nvpair_t *propval; 200411045STom.Erickson@Sun.COM int rv = 0; 20052676Seschrock uint64_t intval; 20062676Seschrock char *strval; 20078697SRichard.Morris@Sun.COM nvlist_t *genericnvl; 200811022STom.Erickson@Sun.COM nvlist_t *errors; 200911022STom.Erickson@Sun.COM nvlist_t *retrynvl; 20104543Smarks 20118697SRichard.Morris@Sun.COM VERIFY(nvlist_alloc(&genericnvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 201211022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 201311022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&retrynvl, NV_UNIQUE_NAME, KM_SLEEP) == 0); 201411022STom.Erickson@Sun.COM 201511022STom.Erickson@Sun.COM retry: 201611022STom.Erickson@Sun.COM pair = NULL; 201711022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 201811022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 20194670Sahrens zfs_prop_t prop = zfs_name_to_prop(propname); 202011181STom.Erickson@Sun.COM int err = 0; 20214543Smarks 202211022STom.Erickson@Sun.COM /* decode the property value */ 202311022STom.Erickson@Sun.COM propval = pair; 202411022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 202511022STom.Erickson@Sun.COM nvlist_t *attrs; 202611022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 202711022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 202811022STom.Erickson@Sun.COM &propval) == 0); 20294543Smarks } 20302676Seschrock 203111022STom.Erickson@Sun.COM /* Validate value type */ 203211022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 203311022STom.Erickson@Sun.COM if (zfs_prop_user(propname)) { 203411022STom.Erickson@Sun.COM if (nvpair_type(propval) != DATA_TYPE_STRING) 203511022STom.Erickson@Sun.COM err = EINVAL; 203611022STom.Erickson@Sun.COM } else if (zfs_prop_userquota(propname)) { 203711022STom.Erickson@Sun.COM if (nvpair_type(propval) != 203811022STom.Erickson@Sun.COM DATA_TYPE_UINT64_ARRAY) 203911022STom.Erickson@Sun.COM err = EINVAL; 20409396SMatthew.Ahrens@Sun.COM } 204111022STom.Erickson@Sun.COM } else { 204211022STom.Erickson@Sun.COM if (nvpair_type(propval) == DATA_TYPE_STRING) { 204311022STom.Erickson@Sun.COM if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) 204411022STom.Erickson@Sun.COM err = EINVAL; 204511022STom.Erickson@Sun.COM } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { 20462885Sahrens const char *unused; 20472885Sahrens 204811022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(propval, 204911022STom.Erickson@Sun.COM &intval) == 0); 20502676Seschrock 20512676Seschrock switch (zfs_prop_get_type(prop)) { 20524787Sahrens case PROP_TYPE_NUMBER: 20532676Seschrock break; 20544787Sahrens case PROP_TYPE_STRING: 205511022STom.Erickson@Sun.COM err = EINVAL; 205611022STom.Erickson@Sun.COM break; 20574787Sahrens case PROP_TYPE_INDEX: 20582717Seschrock if (zfs_prop_index_to_string(prop, 205911022STom.Erickson@Sun.COM intval, &unused) != 0) 206011022STom.Erickson@Sun.COM err = EINVAL; 20612676Seschrock break; 20622676Seschrock default: 20634577Sahrens cmn_err(CE_PANIC, 20644577Sahrens "unknown property type"); 20652676Seschrock } 20662676Seschrock } else { 206711022STom.Erickson@Sun.COM err = EINVAL; 206811022STom.Erickson@Sun.COM } 206911022STom.Erickson@Sun.COM } 207011022STom.Erickson@Sun.COM 207111022STom.Erickson@Sun.COM /* Validate permissions */ 207211022STom.Erickson@Sun.COM if (err == 0) 207311022STom.Erickson@Sun.COM err = zfs_check_settable(dsname, pair, CRED()); 207411022STom.Erickson@Sun.COM 207511022STom.Erickson@Sun.COM if (err == 0) { 207611022STom.Erickson@Sun.COM err = zfs_prop_set_special(dsname, source, pair); 207711022STom.Erickson@Sun.COM if (err == -1) { 207811022STom.Erickson@Sun.COM /* 207911022STom.Erickson@Sun.COM * For better performance we build up a list of 208011022STom.Erickson@Sun.COM * properties to set in a single transaction. 208111022STom.Erickson@Sun.COM */ 208211022STom.Erickson@Sun.COM err = nvlist_add_nvpair(genericnvl, pair); 208311022STom.Erickson@Sun.COM } else if (err != 0 && nvl != retrynvl) { 208411022STom.Erickson@Sun.COM /* 208511022STom.Erickson@Sun.COM * This may be a spurious error caused by 208611022STom.Erickson@Sun.COM * receiving quota and reservation out of order. 208711022STom.Erickson@Sun.COM * Try again in a second pass. 208811022STom.Erickson@Sun.COM */ 208911022STom.Erickson@Sun.COM err = nvlist_add_nvpair(retrynvl, pair); 20902676Seschrock } 209111022STom.Erickson@Sun.COM } 209211022STom.Erickson@Sun.COM 209311022STom.Erickson@Sun.COM if (err != 0) 209411022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(errors, propname, err) == 0); 209511022STom.Erickson@Sun.COM } 209611022STom.Erickson@Sun.COM 209711022STom.Erickson@Sun.COM if (nvl != retrynvl && !nvlist_empty(retrynvl)) { 209811022STom.Erickson@Sun.COM nvl = retrynvl; 209911022STom.Erickson@Sun.COM goto retry; 210011022STom.Erickson@Sun.COM } 210111022STom.Erickson@Sun.COM 210211022STom.Erickson@Sun.COM if (!nvlist_empty(genericnvl) && 210311022STom.Erickson@Sun.COM dsl_props_set(dsname, source, genericnvl) != 0) { 210411022STom.Erickson@Sun.COM /* 210511022STom.Erickson@Sun.COM * If this fails, we still want to set as many properties as we 210611022STom.Erickson@Sun.COM * can, so try setting them individually. 210711022STom.Erickson@Sun.COM */ 210811022STom.Erickson@Sun.COM pair = NULL; 210911022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { 211011022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 211111181STom.Erickson@Sun.COM int err = 0; 211211022STom.Erickson@Sun.COM 211311022STom.Erickson@Sun.COM propval = pair; 211411022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 211511022STom.Erickson@Sun.COM nvlist_t *attrs; 211611022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 211711022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 211811022STom.Erickson@Sun.COM &propval) == 0); 211911022STom.Erickson@Sun.COM } 212011022STom.Erickson@Sun.COM 212111022STom.Erickson@Sun.COM if (nvpair_type(propval) == DATA_TYPE_STRING) { 212211022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(propval, 212311022STom.Erickson@Sun.COM &strval) == 0); 212411022STom.Erickson@Sun.COM err = dsl_prop_set(dsname, propname, source, 1, 212511022STom.Erickson@Sun.COM strlen(strval) + 1, strval); 212611022STom.Erickson@Sun.COM } else { 212711022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(propval, 212811022STom.Erickson@Sun.COM &intval) == 0); 212911022STom.Erickson@Sun.COM err = dsl_prop_set(dsname, propname, source, 8, 213011022STom.Erickson@Sun.COM 1, &intval); 213111022STom.Erickson@Sun.COM } 213211022STom.Erickson@Sun.COM 213311022STom.Erickson@Sun.COM if (err != 0) { 213411022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(errors, propname, 213511022STom.Erickson@Sun.COM err) == 0); 213611022STom.Erickson@Sun.COM } 21372676Seschrock } 21382676Seschrock } 213911022STom.Erickson@Sun.COM nvlist_free(genericnvl); 214011022STom.Erickson@Sun.COM nvlist_free(retrynvl); 214111022STom.Erickson@Sun.COM 214211022STom.Erickson@Sun.COM if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 214311022STom.Erickson@Sun.COM nvlist_free(errors); 214411022STom.Erickson@Sun.COM errors = NULL; 214511022STom.Erickson@Sun.COM } else { 214611022STom.Erickson@Sun.COM VERIFY(nvpair_value_int32(pair, &rv) == 0); 21478697SRichard.Morris@Sun.COM } 214811022STom.Erickson@Sun.COM 214911022STom.Erickson@Sun.COM if (errlist == NULL) 215011022STom.Erickson@Sun.COM nvlist_free(errors); 215111022STom.Erickson@Sun.COM else 215211022STom.Erickson@Sun.COM *errlist = errors; 215311022STom.Erickson@Sun.COM 215411022STom.Erickson@Sun.COM return (rv); 2155789Sahrens } 2156789Sahrens 21575367Sahrens /* 21589355SMatthew.Ahrens@Sun.COM * Check that all the properties are valid user properties. 21599355SMatthew.Ahrens@Sun.COM */ 21609355SMatthew.Ahrens@Sun.COM static int 21619355SMatthew.Ahrens@Sun.COM zfs_check_userprops(char *fsname, nvlist_t *nvl) 21629355SMatthew.Ahrens@Sun.COM { 216311022STom.Erickson@Sun.COM nvpair_t *pair = NULL; 21649355SMatthew.Ahrens@Sun.COM int error = 0; 21659355SMatthew.Ahrens@Sun.COM 216611022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { 216711022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 21689355SMatthew.Ahrens@Sun.COM char *valstr; 21699355SMatthew.Ahrens@Sun.COM 21709355SMatthew.Ahrens@Sun.COM if (!zfs_prop_user(propname) || 217111022STom.Erickson@Sun.COM nvpair_type(pair) != DATA_TYPE_STRING) 21729355SMatthew.Ahrens@Sun.COM return (EINVAL); 21739355SMatthew.Ahrens@Sun.COM 21749355SMatthew.Ahrens@Sun.COM if (error = zfs_secpolicy_write_perms(fsname, 21759355SMatthew.Ahrens@Sun.COM ZFS_DELEG_PERM_USERPROP, CRED())) 21769355SMatthew.Ahrens@Sun.COM return (error); 21779355SMatthew.Ahrens@Sun.COM 21789355SMatthew.Ahrens@Sun.COM if (strlen(propname) >= ZAP_MAXNAMELEN) 21799355SMatthew.Ahrens@Sun.COM return (ENAMETOOLONG); 21809355SMatthew.Ahrens@Sun.COM 218111022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(pair, &valstr) == 0); 21829355SMatthew.Ahrens@Sun.COM if (strlen(valstr) >= ZAP_MAXVALUELEN) 21839355SMatthew.Ahrens@Sun.COM return (E2BIG); 21849355SMatthew.Ahrens@Sun.COM } 21859355SMatthew.Ahrens@Sun.COM return (0); 21869355SMatthew.Ahrens@Sun.COM } 21879355SMatthew.Ahrens@Sun.COM 218811022STom.Erickson@Sun.COM static void 218911022STom.Erickson@Sun.COM props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) 219011022STom.Erickson@Sun.COM { 219111022STom.Erickson@Sun.COM nvpair_t *pair; 219211022STom.Erickson@Sun.COM 219311022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); 219411022STom.Erickson@Sun.COM 219511022STom.Erickson@Sun.COM pair = NULL; 219611022STom.Erickson@Sun.COM while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { 219711022STom.Erickson@Sun.COM if (nvlist_exists(skipped, nvpair_name(pair))) 219811022STom.Erickson@Sun.COM continue; 219911022STom.Erickson@Sun.COM 220011022STom.Erickson@Sun.COM VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); 220111022STom.Erickson@Sun.COM } 220211022STom.Erickson@Sun.COM } 220311022STom.Erickson@Sun.COM 220411022STom.Erickson@Sun.COM static int 220511022STom.Erickson@Sun.COM clear_received_props(objset_t *os, const char *fs, nvlist_t *props, 220611022STom.Erickson@Sun.COM nvlist_t *skipped) 220711022STom.Erickson@Sun.COM { 220811022STom.Erickson@Sun.COM int err = 0; 220911022STom.Erickson@Sun.COM nvlist_t *cleared_props = NULL; 221011022STom.Erickson@Sun.COM props_skip(props, skipped, &cleared_props); 221111022STom.Erickson@Sun.COM if (!nvlist_empty(cleared_props)) { 221211022STom.Erickson@Sun.COM /* 221311022STom.Erickson@Sun.COM * Acts on local properties until the dataset has received 221411022STom.Erickson@Sun.COM * properties at least once on or after SPA_VERSION_RECVD_PROPS. 221511022STom.Erickson@Sun.COM */ 221611022STom.Erickson@Sun.COM zprop_source_t flags = (ZPROP_SRC_NONE | 221711022STom.Erickson@Sun.COM (dsl_prop_get_hasrecvd(os) ? ZPROP_SRC_RECEIVED : 0)); 221811022STom.Erickson@Sun.COM err = zfs_set_prop_nvlist(fs, flags, cleared_props, NULL); 221911022STom.Erickson@Sun.COM } 222011022STom.Erickson@Sun.COM nvlist_free(cleared_props); 222111022STom.Erickson@Sun.COM return (err); 222211022STom.Erickson@Sun.COM } 222311022STom.Erickson@Sun.COM 22249355SMatthew.Ahrens@Sun.COM /* 22255367Sahrens * inputs: 22265367Sahrens * zc_name name of filesystem 22278697SRichard.Morris@Sun.COM * zc_value name of property to set 22285367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 222911022STom.Erickson@Sun.COM * zc_cookie received properties flag 22305367Sahrens * 223111022STom.Erickson@Sun.COM * outputs: 223211022STom.Erickson@Sun.COM * zc_nvlist_dst{_size} error for each unapplied received property 22335367Sahrens */ 2234789Sahrens static int 22352676Seschrock zfs_ioc_set_prop(zfs_cmd_t *zc) 2236789Sahrens { 22372676Seschrock nvlist_t *nvl; 223811022STom.Erickson@Sun.COM boolean_t received = zc->zc_cookie; 223911022STom.Erickson@Sun.COM zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : 224011022STom.Erickson@Sun.COM ZPROP_SRC_LOCAL); 224111022STom.Erickson@Sun.COM nvlist_t *errors = NULL; 22422676Seschrock int error; 2243789Sahrens 22445094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 22459643SEric.Taylor@Sun.COM zc->zc_iflags, &nvl)) != 0) 22462676Seschrock return (error); 22472676Seschrock 224811022STom.Erickson@Sun.COM if (received) { 22497265Sahrens nvlist_t *origprops; 22507265Sahrens objset_t *os; 22517265Sahrens 225210298SMatthew.Ahrens@Sun.COM if (dmu_objset_hold(zc->zc_name, FTAG, &os) == 0) { 225311022STom.Erickson@Sun.COM if (dsl_prop_get_received(os, &origprops) == 0) { 225411022STom.Erickson@Sun.COM (void) clear_received_props(os, 225511022STom.Erickson@Sun.COM zc->zc_name, origprops, nvl); 22567265Sahrens nvlist_free(origprops); 22577265Sahrens } 225811022STom.Erickson@Sun.COM 225911022STom.Erickson@Sun.COM dsl_prop_set_hasrecvd(os); 226010298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 22617265Sahrens } 22627265Sahrens } 22637265Sahrens 226411022STom.Erickson@Sun.COM error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, &errors); 226511022STom.Erickson@Sun.COM 226611022STom.Erickson@Sun.COM if (zc->zc_nvlist_dst != NULL && errors != NULL) { 226711022STom.Erickson@Sun.COM (void) put_nvlist(zc, errors); 226811022STom.Erickson@Sun.COM } 226911022STom.Erickson@Sun.COM 227011022STom.Erickson@Sun.COM nvlist_free(errors); 22712676Seschrock nvlist_free(nvl); 22722676Seschrock return (error); 2273789Sahrens } 2274789Sahrens 22755367Sahrens /* 22765367Sahrens * inputs: 22775367Sahrens * zc_name name of filesystem 22785367Sahrens * zc_value name of property to inherit 227911022STom.Erickson@Sun.COM * zc_cookie revert to received value if TRUE 22805367Sahrens * 22815367Sahrens * outputs: none 22825367Sahrens */ 2283789Sahrens static int 22844849Sahrens zfs_ioc_inherit_prop(zfs_cmd_t *zc) 22854849Sahrens { 228611022STom.Erickson@Sun.COM const char *propname = zc->zc_value; 228711022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(propname); 228811022STom.Erickson@Sun.COM boolean_t received = zc->zc_cookie; 228911022STom.Erickson@Sun.COM zprop_source_t source = (received 229011022STom.Erickson@Sun.COM ? ZPROP_SRC_NONE /* revert to received value, if any */ 229111022STom.Erickson@Sun.COM : ZPROP_SRC_INHERITED); /* explicitly inherit */ 229211022STom.Erickson@Sun.COM 229311022STom.Erickson@Sun.COM if (received) { 229411022STom.Erickson@Sun.COM nvlist_t *dummy; 229511022STom.Erickson@Sun.COM nvpair_t *pair; 229611022STom.Erickson@Sun.COM zprop_type_t type; 229711022STom.Erickson@Sun.COM int err; 229811022STom.Erickson@Sun.COM 229911022STom.Erickson@Sun.COM /* 230011022STom.Erickson@Sun.COM * zfs_prop_set_special() expects properties in the form of an 230111022STom.Erickson@Sun.COM * nvpair with type info. 230211022STom.Erickson@Sun.COM */ 230311022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 230411022STom.Erickson@Sun.COM if (!zfs_prop_user(propname)) 230511022STom.Erickson@Sun.COM return (EINVAL); 230611022STom.Erickson@Sun.COM 230711022STom.Erickson@Sun.COM type = PROP_TYPE_STRING; 230811515STom.Erickson@Sun.COM } else if (prop == ZFS_PROP_VOLSIZE || 230911515STom.Erickson@Sun.COM prop == ZFS_PROP_VERSION) { 231011515STom.Erickson@Sun.COM return (EINVAL); 231111022STom.Erickson@Sun.COM } else { 231211022STom.Erickson@Sun.COM type = zfs_prop_get_type(prop); 231311022STom.Erickson@Sun.COM } 231411022STom.Erickson@Sun.COM 231511022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&dummy, NV_UNIQUE_NAME, KM_SLEEP) == 0); 231611022STom.Erickson@Sun.COM 231711022STom.Erickson@Sun.COM switch (type) { 231811022STom.Erickson@Sun.COM case PROP_TYPE_STRING: 231911022STom.Erickson@Sun.COM VERIFY(0 == nvlist_add_string(dummy, propname, "")); 232011022STom.Erickson@Sun.COM break; 232111022STom.Erickson@Sun.COM case PROP_TYPE_NUMBER: 232211022STom.Erickson@Sun.COM case PROP_TYPE_INDEX: 232311022STom.Erickson@Sun.COM VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); 232411022STom.Erickson@Sun.COM break; 232511022STom.Erickson@Sun.COM default: 232611022STom.Erickson@Sun.COM nvlist_free(dummy); 232711022STom.Erickson@Sun.COM return (EINVAL); 232811022STom.Erickson@Sun.COM } 232911022STom.Erickson@Sun.COM 233011022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(dummy, NULL); 233111022STom.Erickson@Sun.COM err = zfs_prop_set_special(zc->zc_name, source, pair); 233211022STom.Erickson@Sun.COM nvlist_free(dummy); 233311022STom.Erickson@Sun.COM if (err != -1) 233411022STom.Erickson@Sun.COM return (err); /* special property already handled */ 233511022STom.Erickson@Sun.COM } else { 233611022STom.Erickson@Sun.COM /* 233711022STom.Erickson@Sun.COM * Only check this in the non-received case. We want to allow 233811022STom.Erickson@Sun.COM * 'inherit -S' to revert non-inheritable properties like quota 233911022STom.Erickson@Sun.COM * and reservation to the received or default values even though 234011022STom.Erickson@Sun.COM * they are not considered inheritable. 234111022STom.Erickson@Sun.COM */ 234211022STom.Erickson@Sun.COM if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) 234311022STom.Erickson@Sun.COM return (EINVAL); 234411022STom.Erickson@Sun.COM } 234511022STom.Erickson@Sun.COM 23464849Sahrens /* the property name has been validated by zfs_secpolicy_inherit() */ 234711022STom.Erickson@Sun.COM return (dsl_prop_set(zc->zc_name, zc->zc_value, source, 0, 0, NULL)); 23484849Sahrens } 23494849Sahrens 23504849Sahrens static int 23514098Slling zfs_ioc_pool_set_props(zfs_cmd_t *zc) 23523912Slling { 23535094Slling nvlist_t *props; 23543912Slling spa_t *spa; 23555094Slling int error; 235611022STom.Erickson@Sun.COM nvpair_t *pair; 235711022STom.Erickson@Sun.COM 235811022STom.Erickson@Sun.COM if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 235911022STom.Erickson@Sun.COM zc->zc_iflags, &props)) 23603912Slling return (error); 23613912Slling 23628525SEric.Schrock@Sun.COM /* 23638525SEric.Schrock@Sun.COM * If the only property is the configfile, then just do a spa_lookup() 23648525SEric.Schrock@Sun.COM * to handle the faulted case. 23658525SEric.Schrock@Sun.COM */ 236611022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(props, NULL); 236711022STom.Erickson@Sun.COM if (pair != NULL && strcmp(nvpair_name(pair), 23688525SEric.Schrock@Sun.COM zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && 236911022STom.Erickson@Sun.COM nvlist_next_nvpair(props, pair) == NULL) { 23708525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 23718525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) { 23728525SEric.Schrock@Sun.COM spa_configfile_set(spa, props, B_FALSE); 23738525SEric.Schrock@Sun.COM spa_config_sync(spa, B_FALSE, B_TRUE); 23748525SEric.Schrock@Sun.COM } 23758525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 237610672SEric.Schrock@Sun.COM if (spa != NULL) { 237710672SEric.Schrock@Sun.COM nvlist_free(props); 23788525SEric.Schrock@Sun.COM return (0); 237910672SEric.Schrock@Sun.COM } 23808525SEric.Schrock@Sun.COM } 23818525SEric.Schrock@Sun.COM 23823912Slling if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 23835094Slling nvlist_free(props); 23843912Slling return (error); 23853912Slling } 23863912Slling 23875094Slling error = spa_prop_set(spa, props); 23883912Slling 23895094Slling nvlist_free(props); 23903912Slling spa_close(spa, FTAG); 23913912Slling 23923912Slling return (error); 23933912Slling } 23943912Slling 23953912Slling static int 23964098Slling zfs_ioc_pool_get_props(zfs_cmd_t *zc) 23973912Slling { 23983912Slling spa_t *spa; 23993912Slling int error; 24003912Slling nvlist_t *nvp = NULL; 24013912Slling 24028525SEric.Schrock@Sun.COM if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { 24038525SEric.Schrock@Sun.COM /* 24048525SEric.Schrock@Sun.COM * If the pool is faulted, there may be properties we can still 24058525SEric.Schrock@Sun.COM * get (such as altroot and cachefile), so attempt to get them 24068525SEric.Schrock@Sun.COM * anyway. 24078525SEric.Schrock@Sun.COM */ 24088525SEric.Schrock@Sun.COM mutex_enter(&spa_namespace_lock); 24098525SEric.Schrock@Sun.COM if ((spa = spa_lookup(zc->zc_name)) != NULL) 24108525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 24118525SEric.Schrock@Sun.COM mutex_exit(&spa_namespace_lock); 24128525SEric.Schrock@Sun.COM } else { 24138525SEric.Schrock@Sun.COM error = spa_prop_get(spa, &nvp); 24148525SEric.Schrock@Sun.COM spa_close(spa, FTAG); 24158525SEric.Schrock@Sun.COM } 24163912Slling 24173912Slling if (error == 0 && zc->zc_nvlist_dst != NULL) 24183912Slling error = put_nvlist(zc, nvp); 24193912Slling else 24203912Slling error = EFAULT; 24213912Slling 24228525SEric.Schrock@Sun.COM nvlist_free(nvp); 24233912Slling return (error); 24243912Slling } 24253912Slling 24263912Slling static int 24274543Smarks zfs_ioc_iscsi_perm_check(zfs_cmd_t *zc) 24284543Smarks { 24294543Smarks nvlist_t *nvp; 24304543Smarks int error; 24314543Smarks uint32_t uid; 24324543Smarks uint32_t gid; 24334543Smarks uint32_t *groups; 24344543Smarks uint_t group_cnt; 24354543Smarks cred_t *usercred; 24364543Smarks 24375094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 24389643SEric.Taylor@Sun.COM zc->zc_iflags, &nvp)) != 0) { 24394543Smarks return (error); 24404543Smarks } 24414543Smarks 24424543Smarks if ((error = nvlist_lookup_uint32(nvp, 24434543Smarks ZFS_DELEG_PERM_UID, &uid)) != 0) { 24444543Smarks nvlist_free(nvp); 24454543Smarks return (EPERM); 24464543Smarks } 24474543Smarks 24484543Smarks if ((error = nvlist_lookup_uint32(nvp, 24494543Smarks ZFS_DELEG_PERM_GID, &gid)) != 0) { 24504543Smarks nvlist_free(nvp); 24514543Smarks return (EPERM); 24524543Smarks } 24534543Smarks 24544543Smarks if ((error = nvlist_lookup_uint32_array(nvp, ZFS_DELEG_PERM_GROUPS, 24554543Smarks &groups, &group_cnt)) != 0) { 24564543Smarks nvlist_free(nvp); 24574543Smarks return (EPERM); 24584543Smarks } 24594543Smarks usercred = cralloc(); 24604543Smarks if ((crsetugid(usercred, uid, gid) != 0) || 24614543Smarks (crsetgroups(usercred, group_cnt, (gid_t *)groups) != 0)) { 24624543Smarks nvlist_free(nvp); 24634543Smarks crfree(usercred); 24644543Smarks return (EPERM); 24654543Smarks } 24664543Smarks nvlist_free(nvp); 24674543Smarks error = dsl_deleg_access(zc->zc_name, 24684787Sahrens zfs_prop_to_name(ZFS_PROP_SHAREISCSI), usercred); 24694543Smarks crfree(usercred); 24704543Smarks return (error); 24714543Smarks } 24724543Smarks 24735367Sahrens /* 24745367Sahrens * inputs: 24755367Sahrens * zc_name name of filesystem 24765367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 24775367Sahrens * zc_perm_action allow/unallow flag 24785367Sahrens * 24795367Sahrens * outputs: none 24805367Sahrens */ 24814543Smarks static int 24824543Smarks zfs_ioc_set_fsacl(zfs_cmd_t *zc) 24834543Smarks { 24844543Smarks int error; 24854543Smarks nvlist_t *fsaclnv = NULL; 24864543Smarks 24875094Slling if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 24889643SEric.Taylor@Sun.COM zc->zc_iflags, &fsaclnv)) != 0) 24894543Smarks return (error); 24904543Smarks 24914543Smarks /* 24924543Smarks * Verify nvlist is constructed correctly 24934543Smarks */ 24944543Smarks if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { 24954543Smarks nvlist_free(fsaclnv); 24964543Smarks return (EINVAL); 24974543Smarks } 24984543Smarks 24994543Smarks /* 25004543Smarks * If we don't have PRIV_SYS_MOUNT, then validate 25014543Smarks * that user is allowed to hand out each permission in 25024543Smarks * the nvlist(s) 25034543Smarks */ 25044543Smarks 25054787Sahrens error = secpolicy_zfs(CRED()); 25064543Smarks if (error) { 25074787Sahrens if (zc->zc_perm_action == B_FALSE) { 25084787Sahrens error = dsl_deleg_can_allow(zc->zc_name, 25094787Sahrens fsaclnv, CRED()); 25104787Sahrens } else { 25114787Sahrens error = dsl_deleg_can_unallow(zc->zc_name, 25124787Sahrens fsaclnv, CRED()); 25134787Sahrens } 25144543Smarks } 25154543Smarks 25164543Smarks if (error == 0) 25174543Smarks error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); 25184543Smarks 25194543Smarks nvlist_free(fsaclnv); 25204543Smarks return (error); 25214543Smarks } 25224543Smarks 25235367Sahrens /* 25245367Sahrens * inputs: 25255367Sahrens * zc_name name of filesystem 25265367Sahrens * 25275367Sahrens * outputs: 25285367Sahrens * zc_nvlist_src{_size} nvlist of delegated permissions 25295367Sahrens */ 25304543Smarks static int 25314543Smarks zfs_ioc_get_fsacl(zfs_cmd_t *zc) 25324543Smarks { 25334543Smarks nvlist_t *nvp; 25344543Smarks int error; 25354543Smarks 25364543Smarks if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { 25374543Smarks error = put_nvlist(zc, nvp); 25384543Smarks nvlist_free(nvp); 25394543Smarks } 25404543Smarks 25414543Smarks return (error); 25424543Smarks } 25434543Smarks 25445367Sahrens /* 2545789Sahrens * Search the vfs list for a specified resource. Returns a pointer to it 2546789Sahrens * or NULL if no suitable entry is found. The caller of this routine 2547789Sahrens * is responsible for releasing the returned vfs pointer. 2548789Sahrens */ 2549789Sahrens static vfs_t * 2550789Sahrens zfs_get_vfs(const char *resource) 2551789Sahrens { 2552789Sahrens struct vfs *vfsp; 2553789Sahrens struct vfs *vfs_found = NULL; 2554789Sahrens 2555789Sahrens vfs_list_read_lock(); 2556789Sahrens vfsp = rootvfs; 2557789Sahrens do { 2558789Sahrens if (strcmp(refstr_value(vfsp->vfs_resource), resource) == 0) { 2559789Sahrens VFS_HOLD(vfsp); 2560789Sahrens vfs_found = vfsp; 2561789Sahrens break; 2562789Sahrens } 2563789Sahrens vfsp = vfsp->vfs_next; 2564789Sahrens } while (vfsp != rootvfs); 2565789Sahrens vfs_list_unlock(); 2566789Sahrens return (vfs_found); 2567789Sahrens } 2568789Sahrens 25694543Smarks /* ARGSUSED */ 2570789Sahrens static void 25714543Smarks zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) 2572789Sahrens { 25735331Samw zfs_creat_t *zct = arg; 25745498Stimh 25755498Stimh zfs_create_fs(os, cr, zct->zct_zplprops, tx); 25765331Samw } 25775331Samw 25785498Stimh #define ZFS_PROP_UNDEFINED ((uint64_t)-1) 25795498Stimh 25805331Samw /* 25815498Stimh * inputs: 25827184Stimh * createprops list of properties requested by creator 25837184Stimh * default_zplver zpl version to use if unspecified in createprops 25847184Stimh * fuids_ok fuids allowed in this version of the spa? 25857184Stimh * os parent objset pointer (NULL if root fs) 25865331Samw * 25875498Stimh * outputs: 25885498Stimh * zplprops values for the zplprops we attach to the master node object 25897184Stimh * is_ci true if requested file system will be purely case-insensitive 25905331Samw * 25915498Stimh * Determine the settings for utf8only, normalization and 25925498Stimh * casesensitivity. Specific values may have been requested by the 25935498Stimh * creator and/or we can inherit values from the parent dataset. If 25945498Stimh * the file system is of too early a vintage, a creator can not 25955498Stimh * request settings for these properties, even if the requested 25965498Stimh * setting is the default value. We don't actually want to create dsl 25975498Stimh * properties for these, so remove them from the source nvlist after 25985498Stimh * processing. 25995331Samw */ 26005331Samw static int 26019396SMatthew.Ahrens@Sun.COM zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, 26027184Stimh boolean_t fuids_ok, nvlist_t *createprops, nvlist_t *zplprops, 26037184Stimh boolean_t *is_ci) 26045331Samw { 26055498Stimh uint64_t sense = ZFS_PROP_UNDEFINED; 26065498Stimh uint64_t norm = ZFS_PROP_UNDEFINED; 26075498Stimh uint64_t u8 = ZFS_PROP_UNDEFINED; 26085498Stimh 26095498Stimh ASSERT(zplprops != NULL); 26105498Stimh 26115375Stimh /* 26125498Stimh * Pull out creator prop choices, if any. 26135375Stimh */ 26145498Stimh if (createprops) { 26155498Stimh (void) nvlist_lookup_uint64(createprops, 26167184Stimh zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); 26177184Stimh (void) nvlist_lookup_uint64(createprops, 26185498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); 26195498Stimh (void) nvlist_remove_all(createprops, 26205498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE)); 26215498Stimh (void) nvlist_lookup_uint64(createprops, 26225498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); 26235498Stimh (void) nvlist_remove_all(createprops, 26245498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 26255498Stimh (void) nvlist_lookup_uint64(createprops, 26265498Stimh zfs_prop_to_name(ZFS_PROP_CASE), &sense); 26275498Stimh (void) nvlist_remove_all(createprops, 26285498Stimh zfs_prop_to_name(ZFS_PROP_CASE)); 26295331Samw } 26305331Samw 26315375Stimh /* 26327184Stimh * If the zpl version requested is whacky or the file system 26337184Stimh * or pool is version is too "young" to support normalization 26347184Stimh * and the creator tried to set a value for one of the props, 26357184Stimh * error out. 26365498Stimh */ 26377184Stimh if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || 26387184Stimh (zplver >= ZPL_VERSION_FUID && !fuids_ok) || 26397184Stimh (zplver < ZPL_VERSION_NORMALIZATION && 26405498Stimh (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || 26417184Stimh sense != ZFS_PROP_UNDEFINED))) 26425498Stimh return (ENOTSUP); 26435498Stimh 26445498Stimh /* 26455498Stimh * Put the version in the zplprops 26465498Stimh */ 26475498Stimh VERIFY(nvlist_add_uint64(zplprops, 26485498Stimh zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); 26495498Stimh 26505498Stimh if (norm == ZFS_PROP_UNDEFINED) 26515498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0); 26525498Stimh VERIFY(nvlist_add_uint64(zplprops, 26535498Stimh zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); 26545498Stimh 26555498Stimh /* 26565498Stimh * If we're normalizing, names must always be valid UTF-8 strings. 26575498Stimh */ 26585498Stimh if (norm) 26595498Stimh u8 = 1; 26605498Stimh if (u8 == ZFS_PROP_UNDEFINED) 26615498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0); 26625498Stimh VERIFY(nvlist_add_uint64(zplprops, 26635498Stimh zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); 26645498Stimh 26655498Stimh if (sense == ZFS_PROP_UNDEFINED) 26665498Stimh VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0); 26675498Stimh VERIFY(nvlist_add_uint64(zplprops, 26685498Stimh zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); 26695498Stimh 26706492Stimh if (is_ci) 26716492Stimh *is_ci = (sense == ZFS_CASE_INSENSITIVE); 26726492Stimh 26737184Stimh return (0); 26747184Stimh } 26757184Stimh 26767184Stimh static int 26777184Stimh zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, 26787184Stimh nvlist_t *zplprops, boolean_t *is_ci) 26797184Stimh { 26807184Stimh boolean_t fuids_ok = B_TRUE; 26817184Stimh uint64_t zplver = ZPL_VERSION; 26827184Stimh objset_t *os = NULL; 26837184Stimh char parentname[MAXNAMELEN]; 26847184Stimh char *cp; 26857184Stimh int error; 26867184Stimh 26877184Stimh (void) strlcpy(parentname, dataset, sizeof (parentname)); 26887184Stimh cp = strrchr(parentname, '/'); 26897184Stimh ASSERT(cp != NULL); 26907184Stimh cp[0] = '\0'; 26917184Stimh 26929396SMatthew.Ahrens@Sun.COM if (zfs_earlier_version(dataset, SPA_VERSION_USERSPACE)) 26939396SMatthew.Ahrens@Sun.COM zplver = ZPL_VERSION_USERSPACE - 1; 26947184Stimh if (zfs_earlier_version(dataset, SPA_VERSION_FUID)) { 26957184Stimh zplver = ZPL_VERSION_FUID - 1; 26967184Stimh fuids_ok = B_FALSE; 26977184Stimh } 26987184Stimh 26997184Stimh /* 27007184Stimh * Open parent object set so we can inherit zplprop values. 27017184Stimh */ 270210298SMatthew.Ahrens@Sun.COM if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) 27037184Stimh return (error); 27047184Stimh 27057184Stimh error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, createprops, 27067184Stimh zplprops, is_ci); 270710298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 27087184Stimh return (error); 27097184Stimh } 27107184Stimh 27117184Stimh static int 27127184Stimh zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, 27137184Stimh nvlist_t *zplprops, boolean_t *is_ci) 27147184Stimh { 27157184Stimh boolean_t fuids_ok = B_TRUE; 27167184Stimh uint64_t zplver = ZPL_VERSION; 27177184Stimh int error; 27187184Stimh 27197184Stimh if (spa_vers < SPA_VERSION_FUID) { 27207184Stimh zplver = ZPL_VERSION_FUID - 1; 27217184Stimh fuids_ok = B_FALSE; 27227184Stimh } 27237184Stimh 27247184Stimh error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, createprops, 27257184Stimh zplprops, is_ci); 27267184Stimh return (error); 2727789Sahrens } 2728789Sahrens 27295367Sahrens /* 27305367Sahrens * inputs: 27315367Sahrens * zc_objset_type type of objset to create (fs vs zvol) 27325367Sahrens * zc_name name of new objset 27335367Sahrens * zc_value name of snapshot to clone from (may be empty) 27345367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 27355367Sahrens * 27365498Stimh * outputs: none 27375367Sahrens */ 2738789Sahrens static int 2739789Sahrens zfs_ioc_create(zfs_cmd_t *zc) 2740789Sahrens { 2741789Sahrens objset_t *clone; 2742789Sahrens int error = 0; 27435331Samw zfs_creat_t zct; 27444543Smarks nvlist_t *nvprops = NULL; 27454543Smarks void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); 2746789Sahrens dmu_objset_type_t type = zc->zc_objset_type; 2747789Sahrens 2748789Sahrens switch (type) { 2749789Sahrens 2750789Sahrens case DMU_OST_ZFS: 2751789Sahrens cbfunc = zfs_create_cb; 2752789Sahrens break; 2753789Sahrens 2754789Sahrens case DMU_OST_ZVOL: 2755789Sahrens cbfunc = zvol_create_cb; 2756789Sahrens break; 2757789Sahrens 2758789Sahrens default: 27592199Sahrens cbfunc = NULL; 27606423Sgw25295 break; 27612199Sahrens } 27625326Sek110237 if (strchr(zc->zc_name, '@') || 27635326Sek110237 strchr(zc->zc_name, '%')) 2764789Sahrens return (EINVAL); 2765789Sahrens 27662676Seschrock if (zc->zc_nvlist_src != NULL && 27675094Slling (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 27689643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 27692676Seschrock return (error); 27702676Seschrock 27715498Stimh zct.zct_zplprops = NULL; 27725331Samw zct.zct_props = nvprops; 27735331Samw 27742676Seschrock if (zc->zc_value[0] != '\0') { 2775789Sahrens /* 2776789Sahrens * We're creating a clone of an existing snapshot. 2777789Sahrens */ 27782676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 27792676Seschrock if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0) { 27804543Smarks nvlist_free(nvprops); 2781789Sahrens return (EINVAL); 27822676Seschrock } 2783789Sahrens 278410298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_value, FTAG, &clone); 27852676Seschrock if (error) { 27864543Smarks nvlist_free(nvprops); 2787789Sahrens return (error); 27882676Seschrock } 27896492Stimh 279010272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(zc->zc_name, dmu_objset_ds(clone), 0); 279110298SMatthew.Ahrens@Sun.COM dmu_objset_rele(clone, FTAG); 27925331Samw if (error) { 27935331Samw nvlist_free(nvprops); 27945331Samw return (error); 27955331Samw } 2796789Sahrens } else { 27976492Stimh boolean_t is_insensitive = B_FALSE; 27986492Stimh 27992676Seschrock if (cbfunc == NULL) { 28004543Smarks nvlist_free(nvprops); 28012199Sahrens return (EINVAL); 28022676Seschrock } 28032676Seschrock 2804789Sahrens if (type == DMU_OST_ZVOL) { 28052676Seschrock uint64_t volsize, volblocksize; 28062676Seschrock 28074543Smarks if (nvprops == NULL || 28084543Smarks nvlist_lookup_uint64(nvprops, 28092676Seschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), 28102676Seschrock &volsize) != 0) { 28114543Smarks nvlist_free(nvprops); 28122676Seschrock return (EINVAL); 28132676Seschrock } 28142676Seschrock 28154543Smarks if ((error = nvlist_lookup_uint64(nvprops, 28162676Seschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 28172676Seschrock &volblocksize)) != 0 && error != ENOENT) { 28184543Smarks nvlist_free(nvprops); 28192676Seschrock return (EINVAL); 28202676Seschrock } 28211133Seschrock 28222676Seschrock if (error != 0) 28232676Seschrock volblocksize = zfs_prop_default_numeric( 28242676Seschrock ZFS_PROP_VOLBLOCKSIZE); 28252676Seschrock 28262676Seschrock if ((error = zvol_check_volblocksize( 28272676Seschrock volblocksize)) != 0 || 28282676Seschrock (error = zvol_check_volsize(volsize, 28292676Seschrock volblocksize)) != 0) { 28304543Smarks nvlist_free(nvprops); 2831789Sahrens return (error); 28322676Seschrock } 28334577Sahrens } else if (type == DMU_OST_ZFS) { 28345331Samw int error; 28355331Samw 28365498Stimh /* 28375331Samw * We have to have normalization and 28385331Samw * case-folding flags correct when we do the 28395331Samw * file system creation, so go figure them out 28405498Stimh * now. 28415331Samw */ 28425498Stimh VERIFY(nvlist_alloc(&zct.zct_zplprops, 28435498Stimh NV_UNIQUE_NAME, KM_SLEEP) == 0); 28445498Stimh error = zfs_fill_zplprops(zc->zc_name, nvprops, 28457184Stimh zct.zct_zplprops, &is_insensitive); 28465331Samw if (error != 0) { 28475331Samw nvlist_free(nvprops); 28485498Stimh nvlist_free(zct.zct_zplprops); 28495331Samw return (error); 28504577Sahrens } 28512676Seschrock } 285210272SMatthew.Ahrens@Sun.COM error = dmu_objset_create(zc->zc_name, type, 28536492Stimh is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct); 28545498Stimh nvlist_free(zct.zct_zplprops); 2855789Sahrens } 28562676Seschrock 28572676Seschrock /* 28582676Seschrock * It would be nice to do this atomically. 28592676Seschrock */ 28602676Seschrock if (error == 0) { 286111022STom.Erickson@Sun.COM error = zfs_set_prop_nvlist(zc->zc_name, ZPROP_SRC_LOCAL, 286211022STom.Erickson@Sun.COM nvprops, NULL); 286311022STom.Erickson@Sun.COM if (error != 0) 286410242Schris.kirby@sun.com (void) dmu_objset_destroy(zc->zc_name, B_FALSE); 28652676Seschrock } 28664543Smarks nvlist_free(nvprops); 2867789Sahrens return (error); 2868789Sahrens } 2869789Sahrens 28705367Sahrens /* 28715367Sahrens * inputs: 28725367Sahrens * zc_name name of filesystem 28735367Sahrens * zc_value short name of snapshot 28745367Sahrens * zc_cookie recursive flag 28759396SMatthew.Ahrens@Sun.COM * zc_nvlist_src[_size] property list 28765367Sahrens * 287710588SEric.Taylor@Sun.COM * outputs: 287810588SEric.Taylor@Sun.COM * zc_value short snapname (i.e. part after the '@') 28795367Sahrens */ 2880789Sahrens static int 28812199Sahrens zfs_ioc_snapshot(zfs_cmd_t *zc) 28822199Sahrens { 28837265Sahrens nvlist_t *nvprops = NULL; 28847265Sahrens int error; 28857265Sahrens boolean_t recursive = zc->zc_cookie; 28867265Sahrens 28872676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 28882199Sahrens return (EINVAL); 28897265Sahrens 28907265Sahrens if (zc->zc_nvlist_src != NULL && 28917265Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 28929643SEric.Taylor@Sun.COM zc->zc_iflags, &nvprops)) != 0) 28937265Sahrens return (error); 28947265Sahrens 28959355SMatthew.Ahrens@Sun.COM error = zfs_check_userprops(zc->zc_name, nvprops); 28969355SMatthew.Ahrens@Sun.COM if (error) 28979355SMatthew.Ahrens@Sun.COM goto out; 28989355SMatthew.Ahrens@Sun.COM 289911022STom.Erickson@Sun.COM if (!nvlist_empty(nvprops) && 29009355SMatthew.Ahrens@Sun.COM zfs_earlier_version(zc->zc_name, SPA_VERSION_SNAP_PROPS)) { 29019355SMatthew.Ahrens@Sun.COM error = ENOTSUP; 29029355SMatthew.Ahrens@Sun.COM goto out; 29037265Sahrens } 29049355SMatthew.Ahrens@Sun.COM 29059355SMatthew.Ahrens@Sun.COM error = dmu_objset_snapshot(zc->zc_name, zc->zc_value, 29069355SMatthew.Ahrens@Sun.COM nvprops, recursive); 29079355SMatthew.Ahrens@Sun.COM 29089355SMatthew.Ahrens@Sun.COM out: 29097265Sahrens nvlist_free(nvprops); 29107265Sahrens return (error); 29112199Sahrens } 29122199Sahrens 29134007Smmusante int 291411209SMatthew.Ahrens@Sun.COM zfs_unmount_snap(const char *name, void *arg) 2915789Sahrens { 29162417Sahrens vfs_t *vfsp = NULL; 29172199Sahrens 29186689Smaybee if (arg) { 29196689Smaybee char *snapname = arg; 292011209SMatthew.Ahrens@Sun.COM char *fullname = kmem_asprintf("%s@%s", name, snapname); 292111209SMatthew.Ahrens@Sun.COM vfsp = zfs_get_vfs(fullname); 292211209SMatthew.Ahrens@Sun.COM strfree(fullname); 29232417Sahrens } else if (strchr(name, '@')) { 29242199Sahrens vfsp = zfs_get_vfs(name); 29252199Sahrens } 29262199Sahrens 29272199Sahrens if (vfsp) { 29282199Sahrens /* 29292199Sahrens * Always force the unmount for snapshots. 29302199Sahrens */ 29312199Sahrens int flag = MS_FORCE; 2932789Sahrens int err; 2933789Sahrens 29342199Sahrens if ((err = vn_vfswlock(vfsp->vfs_vnodecovered)) != 0) { 29352199Sahrens VFS_RELE(vfsp); 29362199Sahrens return (err); 29372199Sahrens } 29382199Sahrens VFS_RELE(vfsp); 29392199Sahrens if ((err = dounmount(vfsp, flag, kcred)) != 0) 29402199Sahrens return (err); 29412199Sahrens } 29422199Sahrens return (0); 29432199Sahrens } 29442199Sahrens 29455367Sahrens /* 29465367Sahrens * inputs: 294710242Schris.kirby@sun.com * zc_name name of filesystem 294810242Schris.kirby@sun.com * zc_value short name of snapshot 294910242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 29505367Sahrens * 29515367Sahrens * outputs: none 29525367Sahrens */ 29532199Sahrens static int 29542199Sahrens zfs_ioc_destroy_snaps(zfs_cmd_t *zc) 29552199Sahrens { 29562199Sahrens int err; 2957789Sahrens 29582676Seschrock if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 29592199Sahrens return (EINVAL); 29602199Sahrens err = dmu_objset_find(zc->zc_name, 29612676Seschrock zfs_unmount_snap, zc->zc_value, DS_FIND_CHILDREN); 29622199Sahrens if (err) 29632199Sahrens return (err); 296410242Schris.kirby@sun.com return (dmu_snapshots_destroy(zc->zc_name, zc->zc_value, 296510242Schris.kirby@sun.com zc->zc_defer_destroy)); 29662199Sahrens } 29672199Sahrens 29685367Sahrens /* 29695367Sahrens * inputs: 29705367Sahrens * zc_name name of dataset to destroy 29715367Sahrens * zc_objset_type type of objset 297210242Schris.kirby@sun.com * zc_defer_destroy mark for deferred destroy 29735367Sahrens * 29745367Sahrens * outputs: none 29755367Sahrens */ 29762199Sahrens static int 29772199Sahrens zfs_ioc_destroy(zfs_cmd_t *zc) 29782199Sahrens { 297910588SEric.Taylor@Sun.COM int err; 29802199Sahrens if (strchr(zc->zc_name, '@') && zc->zc_objset_type == DMU_OST_ZFS) { 298110588SEric.Taylor@Sun.COM err = zfs_unmount_snap(zc->zc_name, NULL); 29822199Sahrens if (err) 29832199Sahrens return (err); 2984789Sahrens } 2985789Sahrens 298610588SEric.Taylor@Sun.COM err = dmu_objset_destroy(zc->zc_name, zc->zc_defer_destroy); 298710588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0) 298810693Schris.kirby@sun.com (void) zvol_remove_minor(zc->zc_name); 298910588SEric.Taylor@Sun.COM return (err); 2990789Sahrens } 2991789Sahrens 29925367Sahrens /* 29935367Sahrens * inputs: 29945446Sahrens * zc_name name of dataset to rollback (to most recent snapshot) 29955367Sahrens * 29965367Sahrens * outputs: none 29975367Sahrens */ 2998789Sahrens static int 2999789Sahrens zfs_ioc_rollback(zfs_cmd_t *zc) 3000789Sahrens { 300110272SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds, *clone; 30025446Sahrens int error; 300310272SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 300410272SMatthew.Ahrens@Sun.COM char *clone_name; 300510272SMatthew.Ahrens@Sun.COM 300610272SMatthew.Ahrens@Sun.COM error = dsl_dataset_hold(zc->zc_name, FTAG, &ds); 300710272SMatthew.Ahrens@Sun.COM if (error) 300810272SMatthew.Ahrens@Sun.COM return (error); 300910272SMatthew.Ahrens@Sun.COM 301010272SMatthew.Ahrens@Sun.COM /* must not be a snapshot */ 301110272SMatthew.Ahrens@Sun.COM if (dsl_dataset_is_snapshot(ds)) { 301210272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 301310272SMatthew.Ahrens@Sun.COM return (EINVAL); 301410272SMatthew.Ahrens@Sun.COM } 301510272SMatthew.Ahrens@Sun.COM 301610272SMatthew.Ahrens@Sun.COM /* must have a most recent snapshot */ 301710272SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) { 301810272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 301910272SMatthew.Ahrens@Sun.COM return (EINVAL); 302010272SMatthew.Ahrens@Sun.COM } 30215446Sahrens 30225446Sahrens /* 302310272SMatthew.Ahrens@Sun.COM * Create clone of most recent snapshot. 30245446Sahrens */ 302510272SMatthew.Ahrens@Sun.COM clone_name = kmem_asprintf("%s/%%rollback", zc->zc_name); 302610272SMatthew.Ahrens@Sun.COM error = dmu_objset_clone(clone_name, ds->ds_prev, DS_FLAG_INCONSISTENT); 30275446Sahrens if (error) 302810272SMatthew.Ahrens@Sun.COM goto out; 302910272SMatthew.Ahrens@Sun.COM 303010298SMatthew.Ahrens@Sun.COM error = dsl_dataset_own(clone_name, B_TRUE, FTAG, &clone); 303110272SMatthew.Ahrens@Sun.COM if (error) 303210272SMatthew.Ahrens@Sun.COM goto out; 303310272SMatthew.Ahrens@Sun.COM 303410272SMatthew.Ahrens@Sun.COM /* 303510272SMatthew.Ahrens@Sun.COM * Do clone swap. 303610272SMatthew.Ahrens@Sun.COM */ 30379396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 303810298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 30396083Sek110237 if (error == 0) { 30406083Sek110237 int resume_err; 30416083Sek110237 304210272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 304310272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, 304410272SMatthew.Ahrens@Sun.COM B_TRUE); 304510272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 304610272SMatthew.Ahrens@Sun.COM ds = NULL; 304710272SMatthew.Ahrens@Sun.COM } else { 304810272SMatthew.Ahrens@Sun.COM error = EBUSY; 304910272SMatthew.Ahrens@Sun.COM } 305010298SMatthew.Ahrens@Sun.COM resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); 30516083Sek110237 error = error ? error : resume_err; 30526083Sek110237 } 30535446Sahrens VFS_RELE(zfsvfs->z_vfs); 30545446Sahrens } else { 305510272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, B_FALSE, FTAG)) { 305610272SMatthew.Ahrens@Sun.COM error = dsl_dataset_clone_swap(clone, ds, B_TRUE); 305710272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, FTAG); 305810272SMatthew.Ahrens@Sun.COM ds = NULL; 305910272SMatthew.Ahrens@Sun.COM } else { 306010272SMatthew.Ahrens@Sun.COM error = EBUSY; 306110272SMatthew.Ahrens@Sun.COM } 30625446Sahrens } 306310272SMatthew.Ahrens@Sun.COM 306410272SMatthew.Ahrens@Sun.COM /* 306510272SMatthew.Ahrens@Sun.COM * Destroy clone (which also closes it). 306610272SMatthew.Ahrens@Sun.COM */ 306710272SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(clone, FTAG, B_FALSE); 306810272SMatthew.Ahrens@Sun.COM 306910272SMatthew.Ahrens@Sun.COM out: 307010272SMatthew.Ahrens@Sun.COM strfree(clone_name); 307110272SMatthew.Ahrens@Sun.COM if (ds) 307210272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG); 30735446Sahrens return (error); 3074789Sahrens } 3075789Sahrens 30765367Sahrens /* 30775367Sahrens * inputs: 30785367Sahrens * zc_name old name of dataset 30795367Sahrens * zc_value new name of dataset 30805367Sahrens * zc_cookie recursive flag (only valid for snapshots) 30815367Sahrens * 30825367Sahrens * outputs: none 30835367Sahrens */ 3084789Sahrens static int 3085789Sahrens zfs_ioc_rename(zfs_cmd_t *zc) 3086789Sahrens { 30874490Svb160487 boolean_t recursive = zc->zc_cookie & 1; 30884007Smmusante 30892676Seschrock zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; 30905326Sek110237 if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 30915326Sek110237 strchr(zc->zc_value, '%')) 3092789Sahrens return (EINVAL); 3093789Sahrens 30944007Smmusante /* 30954007Smmusante * Unmount snapshot unless we're doing a recursive rename, 30964007Smmusante * in which case the dataset code figures out which snapshots 30974007Smmusante * to unmount. 30984007Smmusante */ 30994007Smmusante if (!recursive && strchr(zc->zc_name, '@') != NULL && 3100789Sahrens zc->zc_objset_type == DMU_OST_ZFS) { 31012199Sahrens int err = zfs_unmount_snap(zc->zc_name, NULL); 31022199Sahrens if (err) 31032199Sahrens return (err); 3104789Sahrens } 310510588SEric.Taylor@Sun.COM if (zc->zc_objset_type == DMU_OST_ZVOL) 310610588SEric.Taylor@Sun.COM (void) zvol_remove_minor(zc->zc_name); 31074007Smmusante return (dmu_objset_rename(zc->zc_name, zc->zc_value, recursive)); 3108789Sahrens } 3109789Sahrens 311011022STom.Erickson@Sun.COM static int 311111022STom.Erickson@Sun.COM zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) 311211022STom.Erickson@Sun.COM { 311311022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 311411022STom.Erickson@Sun.COM boolean_t issnap = (strchr(dsname, '@') != NULL); 311511022STom.Erickson@Sun.COM zfs_prop_t prop = zfs_name_to_prop(propname); 311611022STom.Erickson@Sun.COM uint64_t intval; 311711022STom.Erickson@Sun.COM int err; 311811022STom.Erickson@Sun.COM 311911022STom.Erickson@Sun.COM if (prop == ZPROP_INVAL) { 312011022STom.Erickson@Sun.COM if (zfs_prop_user(propname)) { 312111022STom.Erickson@Sun.COM if (err = zfs_secpolicy_write_perms(dsname, 312211022STom.Erickson@Sun.COM ZFS_DELEG_PERM_USERPROP, cr)) 312311022STom.Erickson@Sun.COM return (err); 312411022STom.Erickson@Sun.COM return (0); 312511022STom.Erickson@Sun.COM } 312611022STom.Erickson@Sun.COM 312711022STom.Erickson@Sun.COM if (!issnap && zfs_prop_userquota(propname)) { 312811022STom.Erickson@Sun.COM const char *perm = NULL; 312911022STom.Erickson@Sun.COM const char *uq_prefix = 313011022STom.Erickson@Sun.COM zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; 313111022STom.Erickson@Sun.COM const char *gq_prefix = 313211022STom.Erickson@Sun.COM zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; 313311022STom.Erickson@Sun.COM 313411022STom.Erickson@Sun.COM if (strncmp(propname, uq_prefix, 313511022STom.Erickson@Sun.COM strlen(uq_prefix)) == 0) { 313611022STom.Erickson@Sun.COM perm = ZFS_DELEG_PERM_USERQUOTA; 313711022STom.Erickson@Sun.COM } else if (strncmp(propname, gq_prefix, 313811022STom.Erickson@Sun.COM strlen(gq_prefix)) == 0) { 313911022STom.Erickson@Sun.COM perm = ZFS_DELEG_PERM_GROUPQUOTA; 314011022STom.Erickson@Sun.COM } else { 314111022STom.Erickson@Sun.COM /* USERUSED and GROUPUSED are read-only */ 314211022STom.Erickson@Sun.COM return (EINVAL); 314311022STom.Erickson@Sun.COM } 314411022STom.Erickson@Sun.COM 314511022STom.Erickson@Sun.COM if (err = zfs_secpolicy_write_perms(dsname, perm, cr)) 314611022STom.Erickson@Sun.COM return (err); 314711022STom.Erickson@Sun.COM return (0); 314811022STom.Erickson@Sun.COM } 314911022STom.Erickson@Sun.COM 315011022STom.Erickson@Sun.COM return (EINVAL); 315111022STom.Erickson@Sun.COM } 315211022STom.Erickson@Sun.COM 315311022STom.Erickson@Sun.COM if (issnap) 315411022STom.Erickson@Sun.COM return (EINVAL); 315511022STom.Erickson@Sun.COM 315611022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_NVLIST) { 315711022STom.Erickson@Sun.COM /* 315811022STom.Erickson@Sun.COM * dsl_prop_get_all_impl() returns properties in this 315911022STom.Erickson@Sun.COM * format. 316011022STom.Erickson@Sun.COM */ 316111022STom.Erickson@Sun.COM nvlist_t *attrs; 316211022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); 316311022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 316411022STom.Erickson@Sun.COM &pair) == 0); 316511022STom.Erickson@Sun.COM } 316611022STom.Erickson@Sun.COM 316711022STom.Erickson@Sun.COM /* 316811022STom.Erickson@Sun.COM * Check that this value is valid for this pool version 316911022STom.Erickson@Sun.COM */ 317011022STom.Erickson@Sun.COM switch (prop) { 317111022STom.Erickson@Sun.COM case ZFS_PROP_COMPRESSION: 317211022STom.Erickson@Sun.COM /* 317311022STom.Erickson@Sun.COM * If the user specified gzip compression, make sure 317411022STom.Erickson@Sun.COM * the SPA supports it. We ignore any errors here since 317511022STom.Erickson@Sun.COM * we'll catch them later. 317611022STom.Erickson@Sun.COM */ 317711022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_UINT64 && 317811022STom.Erickson@Sun.COM nvpair_value_uint64(pair, &intval) == 0) { 317911022STom.Erickson@Sun.COM if (intval >= ZIO_COMPRESS_GZIP_1 && 318011022STom.Erickson@Sun.COM intval <= ZIO_COMPRESS_GZIP_9 && 318111022STom.Erickson@Sun.COM zfs_earlier_version(dsname, 318211022STom.Erickson@Sun.COM SPA_VERSION_GZIP_COMPRESSION)) { 318311022STom.Erickson@Sun.COM return (ENOTSUP); 318411022STom.Erickson@Sun.COM } 318511022STom.Erickson@Sun.COM 318611022STom.Erickson@Sun.COM if (intval == ZIO_COMPRESS_ZLE && 318711022STom.Erickson@Sun.COM zfs_earlier_version(dsname, 318811022STom.Erickson@Sun.COM SPA_VERSION_ZLE_COMPRESSION)) 318911022STom.Erickson@Sun.COM return (ENOTSUP); 319011022STom.Erickson@Sun.COM 319111022STom.Erickson@Sun.COM /* 319211022STom.Erickson@Sun.COM * If this is a bootable dataset then 319311022STom.Erickson@Sun.COM * verify that the compression algorithm 319411022STom.Erickson@Sun.COM * is supported for booting. We must return 319511022STom.Erickson@Sun.COM * something other than ENOTSUP since it 319611022STom.Erickson@Sun.COM * implies a downrev pool version. 319711022STom.Erickson@Sun.COM */ 319811022STom.Erickson@Sun.COM if (zfs_is_bootfs(dsname) && 319911022STom.Erickson@Sun.COM !BOOTFS_COMPRESS_VALID(intval)) { 320011022STom.Erickson@Sun.COM return (ERANGE); 320111022STom.Erickson@Sun.COM } 320211022STom.Erickson@Sun.COM } 320311022STom.Erickson@Sun.COM break; 320411022STom.Erickson@Sun.COM 320511022STom.Erickson@Sun.COM case ZFS_PROP_COPIES: 320611022STom.Erickson@Sun.COM if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) 320711022STom.Erickson@Sun.COM return (ENOTSUP); 320811022STom.Erickson@Sun.COM break; 320911022STom.Erickson@Sun.COM 321011022STom.Erickson@Sun.COM case ZFS_PROP_DEDUP: 321111022STom.Erickson@Sun.COM if (zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) 321211022STom.Erickson@Sun.COM return (ENOTSUP); 321311022STom.Erickson@Sun.COM break; 321411022STom.Erickson@Sun.COM 321511022STom.Erickson@Sun.COM case ZFS_PROP_SHARESMB: 321611022STom.Erickson@Sun.COM if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) 321711022STom.Erickson@Sun.COM return (ENOTSUP); 321811022STom.Erickson@Sun.COM break; 321911022STom.Erickson@Sun.COM 322011022STom.Erickson@Sun.COM case ZFS_PROP_ACLINHERIT: 322111022STom.Erickson@Sun.COM if (nvpair_type(pair) == DATA_TYPE_UINT64 && 322211022STom.Erickson@Sun.COM nvpair_value_uint64(pair, &intval) == 0) { 322311022STom.Erickson@Sun.COM if (intval == ZFS_ACL_PASSTHROUGH_X && 322411022STom.Erickson@Sun.COM zfs_earlier_version(dsname, 322511022STom.Erickson@Sun.COM SPA_VERSION_PASSTHROUGH_X)) 322611022STom.Erickson@Sun.COM return (ENOTSUP); 322711022STom.Erickson@Sun.COM } 322811022STom.Erickson@Sun.COM break; 322911022STom.Erickson@Sun.COM } 323011022STom.Erickson@Sun.COM 323111022STom.Erickson@Sun.COM return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); 323211022STom.Erickson@Sun.COM } 323311022STom.Erickson@Sun.COM 323411022STom.Erickson@Sun.COM /* 323511022STom.Erickson@Sun.COM * Removes properties from the given props list that fail permission checks 323611022STom.Erickson@Sun.COM * needed to clear them and to restore them in case of a receive error. For each 323711022STom.Erickson@Sun.COM * property, make sure we have both set and inherit permissions. 323811022STom.Erickson@Sun.COM * 323911022STom.Erickson@Sun.COM * Returns the first error encountered if any permission checks fail. If the 324011022STom.Erickson@Sun.COM * caller provides a non-NULL errlist, it also gives the complete list of names 324111022STom.Erickson@Sun.COM * of all the properties that failed a permission check along with the 324211022STom.Erickson@Sun.COM * corresponding error numbers. The caller is responsible for freeing the 324311022STom.Erickson@Sun.COM * returned errlist. 324411022STom.Erickson@Sun.COM * 324511022STom.Erickson@Sun.COM * If every property checks out successfully, zero is returned and the list 324611022STom.Erickson@Sun.COM * pointed at by errlist is NULL. 324711022STom.Erickson@Sun.COM */ 324811022STom.Erickson@Sun.COM static int 324911022STom.Erickson@Sun.COM zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) 32506689Smaybee { 32516689Smaybee zfs_cmd_t *zc; 325211022STom.Erickson@Sun.COM nvpair_t *pair, *next_pair; 325311022STom.Erickson@Sun.COM nvlist_t *errors; 325411022STom.Erickson@Sun.COM int err, rv = 0; 32556689Smaybee 32566689Smaybee if (props == NULL) 325711022STom.Erickson@Sun.COM return (0); 325811022STom.Erickson@Sun.COM 325911022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 326011022STom.Erickson@Sun.COM 32616689Smaybee zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); 32626689Smaybee (void) strcpy(zc->zc_name, dataset); 326311022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(props, NULL); 326411022STom.Erickson@Sun.COM while (pair != NULL) { 326511022STom.Erickson@Sun.COM next_pair = nvlist_next_nvpair(props, pair); 326611022STom.Erickson@Sun.COM 326711022STom.Erickson@Sun.COM (void) strcpy(zc->zc_value, nvpair_name(pair)); 326811022STom.Erickson@Sun.COM if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || 326911022STom.Erickson@Sun.COM (err = zfs_secpolicy_inherit(zc, CRED())) != 0) { 327011022STom.Erickson@Sun.COM VERIFY(nvlist_remove_nvpair(props, pair) == 0); 327111022STom.Erickson@Sun.COM VERIFY(nvlist_add_int32(errors, 327211022STom.Erickson@Sun.COM zc->zc_value, err) == 0); 327311022STom.Erickson@Sun.COM } 327411022STom.Erickson@Sun.COM pair = next_pair; 32756689Smaybee } 32766689Smaybee kmem_free(zc, sizeof (zfs_cmd_t)); 327711022STom.Erickson@Sun.COM 327811022STom.Erickson@Sun.COM if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { 327911022STom.Erickson@Sun.COM nvlist_free(errors); 328011022STom.Erickson@Sun.COM errors = NULL; 328111022STom.Erickson@Sun.COM } else { 328211022STom.Erickson@Sun.COM VERIFY(nvpair_value_int32(pair, &rv) == 0); 328311022STom.Erickson@Sun.COM } 328411022STom.Erickson@Sun.COM 328511022STom.Erickson@Sun.COM if (errlist == NULL) 328611022STom.Erickson@Sun.COM nvlist_free(errors); 328711022STom.Erickson@Sun.COM else 328811022STom.Erickson@Sun.COM *errlist = errors; 328911022STom.Erickson@Sun.COM 329011022STom.Erickson@Sun.COM return (rv); 32916689Smaybee } 32926689Smaybee 329311022STom.Erickson@Sun.COM static boolean_t 329411022STom.Erickson@Sun.COM propval_equals(nvpair_t *p1, nvpair_t *p2) 329511022STom.Erickson@Sun.COM { 329611022STom.Erickson@Sun.COM if (nvpair_type(p1) == DATA_TYPE_NVLIST) { 329711022STom.Erickson@Sun.COM /* dsl_prop_get_all_impl() format */ 329811022STom.Erickson@Sun.COM nvlist_t *attrs; 329911022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); 330011022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 330111022STom.Erickson@Sun.COM &p1) == 0); 330211022STom.Erickson@Sun.COM } 330311022STom.Erickson@Sun.COM 330411022STom.Erickson@Sun.COM if (nvpair_type(p2) == DATA_TYPE_NVLIST) { 330511022STom.Erickson@Sun.COM nvlist_t *attrs; 330611022STom.Erickson@Sun.COM VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); 330711022STom.Erickson@Sun.COM VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, 330811022STom.Erickson@Sun.COM &p2) == 0); 330911022STom.Erickson@Sun.COM } 331011022STom.Erickson@Sun.COM 331111022STom.Erickson@Sun.COM if (nvpair_type(p1) != nvpair_type(p2)) 331211022STom.Erickson@Sun.COM return (B_FALSE); 331311022STom.Erickson@Sun.COM 331411022STom.Erickson@Sun.COM if (nvpair_type(p1) == DATA_TYPE_STRING) { 331511022STom.Erickson@Sun.COM char *valstr1, *valstr2; 331611022STom.Erickson@Sun.COM 331711022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); 331811022STom.Erickson@Sun.COM VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); 331911022STom.Erickson@Sun.COM return (strcmp(valstr1, valstr2) == 0); 332011022STom.Erickson@Sun.COM } else { 332111022STom.Erickson@Sun.COM uint64_t intval1, intval2; 332211022STom.Erickson@Sun.COM 332311022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(p1, &intval1) == 0); 332411022STom.Erickson@Sun.COM VERIFY(nvpair_value_uint64(p2, &intval2) == 0); 332511022STom.Erickson@Sun.COM return (intval1 == intval2); 332611022STom.Erickson@Sun.COM } 332711022STom.Erickson@Sun.COM } 332811022STom.Erickson@Sun.COM 332911022STom.Erickson@Sun.COM /* 333011022STom.Erickson@Sun.COM * Remove properties from props if they are not going to change (as determined 333111022STom.Erickson@Sun.COM * by comparison with origprops). Remove them from origprops as well, since we 333211022STom.Erickson@Sun.COM * do not need to clear or restore properties that won't change. 333311022STom.Erickson@Sun.COM */ 333411022STom.Erickson@Sun.COM static void 333511022STom.Erickson@Sun.COM props_reduce(nvlist_t *props, nvlist_t *origprops) 333611022STom.Erickson@Sun.COM { 333711022STom.Erickson@Sun.COM nvpair_t *pair, *next_pair; 333811022STom.Erickson@Sun.COM 333911022STom.Erickson@Sun.COM if (origprops == NULL) 334011022STom.Erickson@Sun.COM return; /* all props need to be received */ 334111022STom.Erickson@Sun.COM 334211022STom.Erickson@Sun.COM pair = nvlist_next_nvpair(props, NULL); 334311022STom.Erickson@Sun.COM while (pair != NULL) { 334411022STom.Erickson@Sun.COM const char *propname = nvpair_name(pair); 334511022STom.Erickson@Sun.COM nvpair_t *match; 334611022STom.Erickson@Sun.COM 334711022STom.Erickson@Sun.COM next_pair = nvlist_next_nvpair(props, pair); 334811022STom.Erickson@Sun.COM 334911022STom.Erickson@Sun.COM if ((nvlist_lookup_nvpair(origprops, propname, 335011022STom.Erickson@Sun.COM &match) != 0) || !propval_equals(pair, match)) 335111022STom.Erickson@Sun.COM goto next; /* need to set received value */ 335211022STom.Erickson@Sun.COM 335311022STom.Erickson@Sun.COM /* don't clear the existing received value */ 335411022STom.Erickson@Sun.COM (void) nvlist_remove_nvpair(origprops, match); 335511022STom.Erickson@Sun.COM /* don't bother receiving the property */ 335611022STom.Erickson@Sun.COM (void) nvlist_remove_nvpair(props, pair); 335711022STom.Erickson@Sun.COM next: 335811022STom.Erickson@Sun.COM pair = next_pair; 335911022STom.Erickson@Sun.COM } 336011022STom.Erickson@Sun.COM } 336111022STom.Erickson@Sun.COM 336211022STom.Erickson@Sun.COM #ifdef DEBUG 336311022STom.Erickson@Sun.COM static boolean_t zfs_ioc_recv_inject_err; 336411022STom.Erickson@Sun.COM #endif 336511022STom.Erickson@Sun.COM 33665367Sahrens /* 33675367Sahrens * inputs: 33685367Sahrens * zc_name name of containing filesystem 33695367Sahrens * zc_nvlist_src{_size} nvlist of properties to apply 33705367Sahrens * zc_value name of snapshot to create 33715367Sahrens * zc_string name of clone origin (if DRR_FLAG_CLONE) 33725367Sahrens * zc_cookie file descriptor to recv from 33735367Sahrens * zc_begin_record the BEGIN record of the stream (not byteswapped) 33745367Sahrens * zc_guid force flag 33755367Sahrens * 33765367Sahrens * outputs: 33775367Sahrens * zc_cookie number of bytes read 337811022STom.Erickson@Sun.COM * zc_nvlist_dst{_size} error for each unapplied received property 337911022STom.Erickson@Sun.COM * zc_obj zprop_errflags_t 33805367Sahrens */ 3381789Sahrens static int 33825367Sahrens zfs_ioc_recv(zfs_cmd_t *zc) 3383789Sahrens { 3384789Sahrens file_t *fp; 33855326Sek110237 objset_t *os; 33865367Sahrens dmu_recv_cookie_t drc; 33875326Sek110237 boolean_t force = (boolean_t)zc->zc_guid; 338811022STom.Erickson@Sun.COM int fd; 338911022STom.Erickson@Sun.COM int error = 0; 339011022STom.Erickson@Sun.COM int props_error = 0; 339111022STom.Erickson@Sun.COM nvlist_t *errors; 33925367Sahrens offset_t off; 339311022STom.Erickson@Sun.COM nvlist_t *props = NULL; /* sent properties */ 339411022STom.Erickson@Sun.COM nvlist_t *origprops = NULL; /* existing properties */ 33955367Sahrens objset_t *origin = NULL; 33965367Sahrens char *tosnap; 33975367Sahrens char tofs[ZFS_MAXNAMELEN]; 339811022STom.Erickson@Sun.COM boolean_t first_recvd_props = B_FALSE; 3399789Sahrens 34003265Sahrens if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || 34015326Sek110237 strchr(zc->zc_value, '@') == NULL || 34025326Sek110237 strchr(zc->zc_value, '%')) 34033265Sahrens return (EINVAL); 34043265Sahrens 34055367Sahrens (void) strcpy(tofs, zc->zc_value); 34065367Sahrens tosnap = strchr(tofs, '@'); 340711022STom.Erickson@Sun.COM *tosnap++ = '\0'; 34085367Sahrens 34095367Sahrens if (zc->zc_nvlist_src != NULL && 34105367Sahrens (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, 34119643SEric.Taylor@Sun.COM zc->zc_iflags, &props)) != 0) 34125367Sahrens return (error); 34135367Sahrens 3414789Sahrens fd = zc->zc_cookie; 3415789Sahrens fp = getf(fd); 34165367Sahrens if (fp == NULL) { 34175367Sahrens nvlist_free(props); 3418789Sahrens return (EBADF); 34195367Sahrens } 34205326Sek110237 342111022STom.Erickson@Sun.COM VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); 342211022STom.Erickson@Sun.COM 342310298SMatthew.Ahrens@Sun.COM if (props && dmu_objset_hold(tofs, FTAG, &os) == 0) { 342411022STom.Erickson@Sun.COM if ((spa_version(os->os_spa) >= SPA_VERSION_RECVD_PROPS) && 342511022STom.Erickson@Sun.COM !dsl_prop_get_hasrecvd(os)) { 342611022STom.Erickson@Sun.COM first_recvd_props = B_TRUE; 342711022STom.Erickson@Sun.COM } 342811022STom.Erickson@Sun.COM 34296689Smaybee /* 343011022STom.Erickson@Sun.COM * If new received properties are supplied, they are to 343111022STom.Erickson@Sun.COM * completely replace the existing received properties, so stash 343211022STom.Erickson@Sun.COM * away the existing ones. 34336689Smaybee */ 343411022STom.Erickson@Sun.COM if (dsl_prop_get_received(os, &origprops) == 0) { 343511022STom.Erickson@Sun.COM nvlist_t *errlist = NULL; 343611022STom.Erickson@Sun.COM /* 343711022STom.Erickson@Sun.COM * Don't bother writing a property if its value won't 343811022STom.Erickson@Sun.COM * change (and avoid the unnecessary security checks). 343911022STom.Erickson@Sun.COM * 344011022STom.Erickson@Sun.COM * The first receive after SPA_VERSION_RECVD_PROPS is a 344111022STom.Erickson@Sun.COM * special case where we blow away all local properties 344211022STom.Erickson@Sun.COM * regardless. 344311022STom.Erickson@Sun.COM */ 344411022STom.Erickson@Sun.COM if (!first_recvd_props) 344511022STom.Erickson@Sun.COM props_reduce(props, origprops); 344611022STom.Erickson@Sun.COM if (zfs_check_clearable(tofs, origprops, 344711022STom.Erickson@Sun.COM &errlist) != 0) 344811022STom.Erickson@Sun.COM (void) nvlist_merge(errors, errlist, 0); 344911022STom.Erickson@Sun.COM nvlist_free(errlist); 345011022STom.Erickson@Sun.COM } 345110298SMatthew.Ahrens@Sun.COM 345210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 34535326Sek110237 } 34545326Sek110237 34555367Sahrens if (zc->zc_string[0]) { 345610298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_string, FTAG, &origin); 34576689Smaybee if (error) 34586689Smaybee goto out; 34595367Sahrens } 34605367Sahrens 346111007SLori.Alt@Sun.COM error = dmu_recv_begin(tofs, tosnap, zc->zc_top_ds, 346211007SLori.Alt@Sun.COM &zc->zc_begin_record, force, origin, &drc); 34635367Sahrens if (origin) 346410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(origin, FTAG); 34656689Smaybee if (error) 34666689Smaybee goto out; 34675326Sek110237 34685326Sek110237 /* 346911022STom.Erickson@Sun.COM * Set properties before we receive the stream so that they are applied 347011022STom.Erickson@Sun.COM * to the new data. Note that we must call dmu_recv_stream() if 347111022STom.Erickson@Sun.COM * dmu_recv_begin() succeeds. 34725326Sek110237 */ 34735367Sahrens if (props) { 347411022STom.Erickson@Sun.COM nvlist_t *errlist; 347511022STom.Erickson@Sun.COM 347611022STom.Erickson@Sun.COM if (dmu_objset_from_ds(drc.drc_logical_ds, &os) == 0) { 347711022STom.Erickson@Sun.COM if (drc.drc_newfs) { 347811022STom.Erickson@Sun.COM if (spa_version(os->os_spa) >= 347911022STom.Erickson@Sun.COM SPA_VERSION_RECVD_PROPS) 348011022STom.Erickson@Sun.COM first_recvd_props = B_TRUE; 348111022STom.Erickson@Sun.COM } else if (origprops != NULL) { 348211022STom.Erickson@Sun.COM if (clear_received_props(os, tofs, origprops, 348311022STom.Erickson@Sun.COM first_recvd_props ? NULL : props) != 0) 348411022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NOCLEAR; 348511022STom.Erickson@Sun.COM } else { 348611022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NOCLEAR; 348711022STom.Erickson@Sun.COM } 348811022STom.Erickson@Sun.COM dsl_prop_set_hasrecvd(os); 348911022STom.Erickson@Sun.COM } else if (!drc.drc_newfs) { 349011022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NOCLEAR; 349111022STom.Erickson@Sun.COM } 349211022STom.Erickson@Sun.COM 349311022STom.Erickson@Sun.COM (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, 349411022STom.Erickson@Sun.COM props, &errlist); 349511022STom.Erickson@Sun.COM (void) nvlist_merge(errors, errlist, 0); 349611022STom.Erickson@Sun.COM nvlist_free(errlist); 349711022STom.Erickson@Sun.COM } 349811022STom.Erickson@Sun.COM 349911022STom.Erickson@Sun.COM if (fit_error_list(zc, &errors) != 0 || put_nvlist(zc, errors) != 0) { 35006689Smaybee /* 350111022STom.Erickson@Sun.COM * Caller made zc->zc_nvlist_dst less than the minimum expected 350211022STom.Erickson@Sun.COM * size or supplied an invalid address. 35036689Smaybee */ 350411022STom.Erickson@Sun.COM props_error = EINVAL; 35055367Sahrens } 35065367Sahrens 35075367Sahrens off = fp->f_offset; 35085367Sahrens error = dmu_recv_stream(&drc, fp->f_vnode, &off); 35095367Sahrens 351010204SMatthew.Ahrens@Sun.COM if (error == 0) { 351110204SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs = NULL; 351210204SMatthew.Ahrens@Sun.COM 351310204SMatthew.Ahrens@Sun.COM if (getzfsvfs(tofs, &zfsvfs) == 0) { 351410204SMatthew.Ahrens@Sun.COM /* online recv */ 351510204SMatthew.Ahrens@Sun.COM int end_err; 351610298SMatthew.Ahrens@Sun.COM 351710298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 351810204SMatthew.Ahrens@Sun.COM /* 351910204SMatthew.Ahrens@Sun.COM * If the suspend fails, then the recv_end will 352010204SMatthew.Ahrens@Sun.COM * likely also fail, and clean up after itself. 352110204SMatthew.Ahrens@Sun.COM */ 352210204SMatthew.Ahrens@Sun.COM end_err = dmu_recv_end(&drc); 352310204SMatthew.Ahrens@Sun.COM if (error == 0) { 352410204SMatthew.Ahrens@Sun.COM int resume_err = 352510298SMatthew.Ahrens@Sun.COM zfs_resume_fs(zfsvfs, tofs); 352610204SMatthew.Ahrens@Sun.COM error = error ? error : resume_err; 352710204SMatthew.Ahrens@Sun.COM } 352810204SMatthew.Ahrens@Sun.COM error = error ? error : end_err; 352910204SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 353010204SMatthew.Ahrens@Sun.COM } else { 35316689Smaybee error = dmu_recv_end(&drc); 35325367Sahrens } 35336083Sek110237 } 35345367Sahrens 35355367Sahrens zc->zc_cookie = off - fp->f_offset; 35365367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 35375367Sahrens fp->f_offset = off; 35382885Sahrens 353911022STom.Erickson@Sun.COM #ifdef DEBUG 354011022STom.Erickson@Sun.COM if (zfs_ioc_recv_inject_err) { 354111022STom.Erickson@Sun.COM zfs_ioc_recv_inject_err = B_FALSE; 354211022STom.Erickson@Sun.COM error = 1; 354311022STom.Erickson@Sun.COM } 354411022STom.Erickson@Sun.COM #endif 35456689Smaybee /* 35466689Smaybee * On error, restore the original props. 35476689Smaybee */ 35486689Smaybee if (error && props) { 354911022STom.Erickson@Sun.COM if (dmu_objset_hold(tofs, FTAG, &os) == 0) { 355011022STom.Erickson@Sun.COM if (clear_received_props(os, tofs, props, NULL) != 0) { 355111022STom.Erickson@Sun.COM /* 355211022STom.Erickson@Sun.COM * We failed to clear the received properties. 355311022STom.Erickson@Sun.COM * Since we may have left a $recvd value on the 355411022STom.Erickson@Sun.COM * system, we can't clear the $hasrecvd flag. 355511022STom.Erickson@Sun.COM */ 355611022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 355711022STom.Erickson@Sun.COM } else if (first_recvd_props) { 355811022STom.Erickson@Sun.COM dsl_prop_unset_hasrecvd(os); 355911022STom.Erickson@Sun.COM } 356011022STom.Erickson@Sun.COM dmu_objset_rele(os, FTAG); 356111022STom.Erickson@Sun.COM } else if (!drc.drc_newfs) { 356211022STom.Erickson@Sun.COM /* We failed to clear the received properties. */ 356311022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 356411022STom.Erickson@Sun.COM } 356511022STom.Erickson@Sun.COM 356611022STom.Erickson@Sun.COM if (origprops == NULL && !drc.drc_newfs) { 356711022STom.Erickson@Sun.COM /* We failed to stash the original properties. */ 356811022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 356911022STom.Erickson@Sun.COM } 357011022STom.Erickson@Sun.COM 357111022STom.Erickson@Sun.COM /* 357211022STom.Erickson@Sun.COM * dsl_props_set() will not convert RECEIVED to LOCAL on or 357311022STom.Erickson@Sun.COM * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL 357411022STom.Erickson@Sun.COM * explictly if we're restoring local properties cleared in the 357511022STom.Erickson@Sun.COM * first new-style receive. 357611022STom.Erickson@Sun.COM */ 357711022STom.Erickson@Sun.COM if (origprops != NULL && 357811022STom.Erickson@Sun.COM zfs_set_prop_nvlist(tofs, (first_recvd_props ? 357911022STom.Erickson@Sun.COM ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), 358011022STom.Erickson@Sun.COM origprops, NULL) != 0) { 358111022STom.Erickson@Sun.COM /* 358211022STom.Erickson@Sun.COM * We stashed the original properties but failed to 358311022STom.Erickson@Sun.COM * restore them. 358411022STom.Erickson@Sun.COM */ 358511022STom.Erickson@Sun.COM zc->zc_obj |= ZPROP_ERR_NORESTORE; 358611022STom.Erickson@Sun.COM } 35876689Smaybee } 35886689Smaybee out: 35896689Smaybee nvlist_free(props); 35906689Smaybee nvlist_free(origprops); 359111022STom.Erickson@Sun.COM nvlist_free(errors); 3592789Sahrens releasef(fd); 359311022STom.Erickson@Sun.COM 359411022STom.Erickson@Sun.COM if (error == 0) 359511022STom.Erickson@Sun.COM error = props_error; 359611022STom.Erickson@Sun.COM 3597789Sahrens return (error); 3598789Sahrens } 3599789Sahrens 36005367Sahrens /* 36015367Sahrens * inputs: 36025367Sahrens * zc_name name of snapshot to send 36035367Sahrens * zc_value short name of incremental fromsnap (may be empty) 36045367Sahrens * zc_cookie file descriptor to send stream to 36055367Sahrens * zc_obj fromorigin flag (mutually exclusive with zc_value) 36065367Sahrens * 36075367Sahrens * outputs: none 36085367Sahrens */ 3609789Sahrens static int 36105367Sahrens zfs_ioc_send(zfs_cmd_t *zc) 3611789Sahrens { 3612789Sahrens objset_t *fromsnap = NULL; 3613789Sahrens objset_t *tosnap; 3614789Sahrens file_t *fp; 3615789Sahrens int error; 36165367Sahrens offset_t off; 3617789Sahrens 361810298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &tosnap); 3619789Sahrens if (error) 3620789Sahrens return (error); 3621789Sahrens 36222676Seschrock if (zc->zc_value[0] != '\0') { 36238012SEric.Taylor@Sun.COM char *buf; 36242885Sahrens char *cp; 36252885Sahrens 36268012SEric.Taylor@Sun.COM buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 36278012SEric.Taylor@Sun.COM (void) strncpy(buf, zc->zc_name, MAXPATHLEN); 36282885Sahrens cp = strchr(buf, '@'); 36292885Sahrens if (cp) 36302885Sahrens *(cp+1) = 0; 36318012SEric.Taylor@Sun.COM (void) strncat(buf, zc->zc_value, MAXPATHLEN); 363210298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(buf, FTAG, &fromsnap); 36338012SEric.Taylor@Sun.COM kmem_free(buf, MAXPATHLEN); 3634789Sahrens if (error) { 363510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 3636789Sahrens return (error); 3637789Sahrens } 3638789Sahrens } 3639789Sahrens 3640789Sahrens fp = getf(zc->zc_cookie); 3641789Sahrens if (fp == NULL) { 364210298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 3643789Sahrens if (fromsnap) 364410298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 3645789Sahrens return (EBADF); 3646789Sahrens } 3647789Sahrens 36485367Sahrens off = fp->f_offset; 36495367Sahrens error = dmu_sendbackup(tosnap, fromsnap, zc->zc_obj, fp->f_vnode, &off); 36505367Sahrens 36515367Sahrens if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) 36525367Sahrens fp->f_offset = off; 3653789Sahrens releasef(zc->zc_cookie); 3654789Sahrens if (fromsnap) 365510298SMatthew.Ahrens@Sun.COM dmu_objset_rele(fromsnap, FTAG); 365610298SMatthew.Ahrens@Sun.COM dmu_objset_rele(tosnap, FTAG); 3657789Sahrens return (error); 3658789Sahrens } 3659789Sahrens 36601544Seschrock static int 36611544Seschrock zfs_ioc_inject_fault(zfs_cmd_t *zc) 36621544Seschrock { 36631544Seschrock int id, error; 36641544Seschrock 36651544Seschrock error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, 36661544Seschrock &zc->zc_inject_record); 36671544Seschrock 36681544Seschrock if (error == 0) 36691544Seschrock zc->zc_guid = (uint64_t)id; 36701544Seschrock 36711544Seschrock return (error); 36721544Seschrock } 36731544Seschrock 36741544Seschrock static int 36751544Seschrock zfs_ioc_clear_fault(zfs_cmd_t *zc) 36761544Seschrock { 36771544Seschrock return (zio_clear_fault((int)zc->zc_guid)); 36781544Seschrock } 36791544Seschrock 36801544Seschrock static int 36811544Seschrock zfs_ioc_inject_list_next(zfs_cmd_t *zc) 36821544Seschrock { 36831544Seschrock int id = (int)zc->zc_guid; 36841544Seschrock int error; 36851544Seschrock 36861544Seschrock error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), 36871544Seschrock &zc->zc_inject_record); 36881544Seschrock 36891544Seschrock zc->zc_guid = id; 36901544Seschrock 36911544Seschrock return (error); 36921544Seschrock } 36931544Seschrock 36941544Seschrock static int 36951544Seschrock zfs_ioc_error_log(zfs_cmd_t *zc) 36961544Seschrock { 36971544Seschrock spa_t *spa; 36981544Seschrock int error; 36992676Seschrock size_t count = (size_t)zc->zc_nvlist_dst_size; 37001544Seschrock 37011544Seschrock if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) 37021544Seschrock return (error); 37031544Seschrock 37042676Seschrock error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, 37051544Seschrock &count); 37061544Seschrock if (error == 0) 37072676Seschrock zc->zc_nvlist_dst_size = count; 37081544Seschrock else 37092676Seschrock zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); 37101544Seschrock 37111544Seschrock spa_close(spa, FTAG); 37121544Seschrock 37131544Seschrock return (error); 37141544Seschrock } 37151544Seschrock 37161544Seschrock static int 37171544Seschrock zfs_ioc_clear(zfs_cmd_t *zc) 37181544Seschrock { 37191544Seschrock spa_t *spa; 37201544Seschrock vdev_t *vd; 37211544Seschrock int error; 37221544Seschrock 37237294Sperrin /* 37247294Sperrin * On zpool clear we also fix up missing slogs 37257294Sperrin */ 37267294Sperrin mutex_enter(&spa_namespace_lock); 37277294Sperrin spa = spa_lookup(zc->zc_name); 37287294Sperrin if (spa == NULL) { 37297294Sperrin mutex_exit(&spa_namespace_lock); 37307294Sperrin return (EIO); 37317294Sperrin } 373210922SJeff.Bonwick@Sun.COM if (spa_get_log_state(spa) == SPA_LOG_MISSING) { 37337294Sperrin /* we need to let spa_open/spa_load clear the chains */ 373410922SJeff.Bonwick@Sun.COM spa_set_log_state(spa, SPA_LOG_CLEAR); 37357294Sperrin } 373610921STim.Haley@Sun.COM spa->spa_last_open_failed = 0; 37377294Sperrin mutex_exit(&spa_namespace_lock); 37387294Sperrin 373911727SVictor.Latushkin@Sun.COM if (zc->zc_cookie & ZPOOL_NO_REWIND) { 374010921STim.Haley@Sun.COM error = spa_open(zc->zc_name, &spa, FTAG); 374110921STim.Haley@Sun.COM } else { 374210921STim.Haley@Sun.COM nvlist_t *policy; 374310921STim.Haley@Sun.COM nvlist_t *config = NULL; 374410921STim.Haley@Sun.COM 374510921STim.Haley@Sun.COM if (zc->zc_nvlist_src == NULL) 374610921STim.Haley@Sun.COM return (EINVAL); 374710921STim.Haley@Sun.COM 374810921STim.Haley@Sun.COM if ((error = get_nvlist(zc->zc_nvlist_src, 374910921STim.Haley@Sun.COM zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { 375010921STim.Haley@Sun.COM error = spa_open_rewind(zc->zc_name, &spa, FTAG, 375110921STim.Haley@Sun.COM policy, &config); 375210921STim.Haley@Sun.COM if (config != NULL) { 375310921STim.Haley@Sun.COM (void) put_nvlist(zc, config); 375410921STim.Haley@Sun.COM nvlist_free(config); 375510921STim.Haley@Sun.COM } 375610921STim.Haley@Sun.COM nvlist_free(policy); 375710921STim.Haley@Sun.COM } 375810921STim.Haley@Sun.COM } 375910921STim.Haley@Sun.COM 376010921STim.Haley@Sun.COM if (error) 37611544Seschrock return (error); 37621544Seschrock 376310685SGeorge.Wilson@Sun.COM spa_vdev_state_enter(spa, SCL_NONE); 37641544Seschrock 37652676Seschrock if (zc->zc_guid == 0) { 37661544Seschrock vd = NULL; 37676643Seschrock } else { 37686643Seschrock vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); 37695450Sbrendan if (vd == NULL) { 37707754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, ENODEV); 37715450Sbrendan spa_close(spa, FTAG); 37725450Sbrendan return (ENODEV); 37735450Sbrendan } 37741544Seschrock } 37751544Seschrock 37767754SJeff.Bonwick@Sun.COM vdev_clear(spa, vd); 37777754SJeff.Bonwick@Sun.COM 37787754SJeff.Bonwick@Sun.COM (void) spa_vdev_state_exit(spa, NULL, 0); 37797754SJeff.Bonwick@Sun.COM 37807754SJeff.Bonwick@Sun.COM /* 37817754SJeff.Bonwick@Sun.COM * Resume any suspended I/Os. 37827754SJeff.Bonwick@Sun.COM */ 37839234SGeorge.Wilson@Sun.COM if (zio_resume(spa) != 0) 37849234SGeorge.Wilson@Sun.COM error = EIO; 37851544Seschrock 37861544Seschrock spa_close(spa, FTAG); 37871544Seschrock 37889234SGeorge.Wilson@Sun.COM return (error); 37891544Seschrock } 37901544Seschrock 37915367Sahrens /* 37925367Sahrens * inputs: 37935367Sahrens * zc_name name of filesystem 37945367Sahrens * zc_value name of origin snapshot 37955367Sahrens * 379610588SEric.Taylor@Sun.COM * outputs: 379710588SEric.Taylor@Sun.COM * zc_string name of conflicting snapshot, if there is one 37985367Sahrens */ 37991544Seschrock static int 38002082Seschrock zfs_ioc_promote(zfs_cmd_t *zc) 38012082Seschrock { 38022417Sahrens char *cp; 38032417Sahrens 38042417Sahrens /* 38052417Sahrens * We don't need to unmount *all* the origin fs's snapshots, but 38062417Sahrens * it's easier. 38072417Sahrens */ 38082676Seschrock cp = strchr(zc->zc_value, '@'); 38092417Sahrens if (cp) 38102417Sahrens *cp = '\0'; 38112676Seschrock (void) dmu_objset_find(zc->zc_value, 38122417Sahrens zfs_unmount_snap, NULL, DS_FIND_SNAPSHOTS); 381310588SEric.Taylor@Sun.COM return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); 38142082Seschrock } 38152082Seschrock 38164543Smarks /* 38179396SMatthew.Ahrens@Sun.COM * Retrieve a single {user|group}{used|quota}@... property. 38189396SMatthew.Ahrens@Sun.COM * 38199396SMatthew.Ahrens@Sun.COM * inputs: 38209396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 38219396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 38229396SMatthew.Ahrens@Sun.COM * zc_value domain name (eg. "S-1-234-567-89") 38239396SMatthew.Ahrens@Sun.COM * zc_guid RID/UID/GID 38249396SMatthew.Ahrens@Sun.COM * 38259396SMatthew.Ahrens@Sun.COM * outputs: 38269396SMatthew.Ahrens@Sun.COM * zc_cookie property value 38279396SMatthew.Ahrens@Sun.COM */ 38289396SMatthew.Ahrens@Sun.COM static int 38299396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_one(zfs_cmd_t *zc) 38309396SMatthew.Ahrens@Sun.COM { 38319396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 38329396SMatthew.Ahrens@Sun.COM int error; 38339396SMatthew.Ahrens@Sun.COM 38349396SMatthew.Ahrens@Sun.COM if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) 38359396SMatthew.Ahrens@Sun.COM return (EINVAL); 38369396SMatthew.Ahrens@Sun.COM 383710298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 38389396SMatthew.Ahrens@Sun.COM if (error) 38399396SMatthew.Ahrens@Sun.COM return (error); 38409396SMatthew.Ahrens@Sun.COM 38419396SMatthew.Ahrens@Sun.COM error = zfs_userspace_one(zfsvfs, 38429396SMatthew.Ahrens@Sun.COM zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); 38439396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 38449396SMatthew.Ahrens@Sun.COM 38459396SMatthew.Ahrens@Sun.COM return (error); 38469396SMatthew.Ahrens@Sun.COM } 38479396SMatthew.Ahrens@Sun.COM 38489396SMatthew.Ahrens@Sun.COM /* 38499396SMatthew.Ahrens@Sun.COM * inputs: 38509396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 38519396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 38529396SMatthew.Ahrens@Sun.COM * zc_objset_type zfs_userquota_prop_t 38539396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) 38549396SMatthew.Ahrens@Sun.COM * 38559396SMatthew.Ahrens@Sun.COM * outputs: 38569396SMatthew.Ahrens@Sun.COM * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) 38579396SMatthew.Ahrens@Sun.COM * zc_cookie zap cursor 38589396SMatthew.Ahrens@Sun.COM */ 38599396SMatthew.Ahrens@Sun.COM static int 38609396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_many(zfs_cmd_t *zc) 38619396SMatthew.Ahrens@Sun.COM { 38629396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 38639396SMatthew.Ahrens@Sun.COM int error; 38649396SMatthew.Ahrens@Sun.COM 386510298SMatthew.Ahrens@Sun.COM error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs); 38669396SMatthew.Ahrens@Sun.COM if (error) 38679396SMatthew.Ahrens@Sun.COM return (error); 38689396SMatthew.Ahrens@Sun.COM 38699396SMatthew.Ahrens@Sun.COM int bufsize = zc->zc_nvlist_dst_size; 38709396SMatthew.Ahrens@Sun.COM void *buf = kmem_alloc(bufsize, KM_SLEEP); 38719396SMatthew.Ahrens@Sun.COM 38729396SMatthew.Ahrens@Sun.COM error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, 38739396SMatthew.Ahrens@Sun.COM buf, &zc->zc_nvlist_dst_size); 38749396SMatthew.Ahrens@Sun.COM 38759396SMatthew.Ahrens@Sun.COM if (error == 0) { 38769396SMatthew.Ahrens@Sun.COM error = xcopyout(buf, 38779396SMatthew.Ahrens@Sun.COM (void *)(uintptr_t)zc->zc_nvlist_dst, 38789396SMatthew.Ahrens@Sun.COM zc->zc_nvlist_dst_size); 38799396SMatthew.Ahrens@Sun.COM } 38809396SMatthew.Ahrens@Sun.COM kmem_free(buf, bufsize); 38819396SMatthew.Ahrens@Sun.COM zfsvfs_rele(zfsvfs, FTAG); 38829396SMatthew.Ahrens@Sun.COM 38839396SMatthew.Ahrens@Sun.COM return (error); 38849396SMatthew.Ahrens@Sun.COM } 38859396SMatthew.Ahrens@Sun.COM 38869396SMatthew.Ahrens@Sun.COM /* 38879396SMatthew.Ahrens@Sun.COM * inputs: 38889396SMatthew.Ahrens@Sun.COM * zc_name name of filesystem 38899396SMatthew.Ahrens@Sun.COM * 38909396SMatthew.Ahrens@Sun.COM * outputs: 38919396SMatthew.Ahrens@Sun.COM * none 38929396SMatthew.Ahrens@Sun.COM */ 38939396SMatthew.Ahrens@Sun.COM static int 38949396SMatthew.Ahrens@Sun.COM zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) 38959396SMatthew.Ahrens@Sun.COM { 38969396SMatthew.Ahrens@Sun.COM objset_t *os; 389711422SMark.Musante@Sun.COM int error = 0; 38989396SMatthew.Ahrens@Sun.COM zfsvfs_t *zfsvfs; 38999396SMatthew.Ahrens@Sun.COM 39009396SMatthew.Ahrens@Sun.COM if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { 390110298SMatthew.Ahrens@Sun.COM if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { 39029396SMatthew.Ahrens@Sun.COM /* 39039396SMatthew.Ahrens@Sun.COM * If userused is not enabled, it may be because the 39049396SMatthew.Ahrens@Sun.COM * objset needs to be closed & reopened (to grow the 39059396SMatthew.Ahrens@Sun.COM * objset_phys_t). Suspend/resume the fs will do that. 39069396SMatthew.Ahrens@Sun.COM */ 390710298SMatthew.Ahrens@Sun.COM error = zfs_suspend_fs(zfsvfs); 390810298SMatthew.Ahrens@Sun.COM if (error == 0) 390910298SMatthew.Ahrens@Sun.COM error = zfs_resume_fs(zfsvfs, zc->zc_name); 39109396SMatthew.Ahrens@Sun.COM } 39119396SMatthew.Ahrens@Sun.COM if (error == 0) 39129396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(zfsvfs->z_os); 39139396SMatthew.Ahrens@Sun.COM VFS_RELE(zfsvfs->z_vfs); 39149396SMatthew.Ahrens@Sun.COM } else { 391510298SMatthew.Ahrens@Sun.COM /* XXX kind of reading contents without owning */ 391610298SMatthew.Ahrens@Sun.COM error = dmu_objset_hold(zc->zc_name, FTAG, &os); 39179396SMatthew.Ahrens@Sun.COM if (error) 39189396SMatthew.Ahrens@Sun.COM return (error); 39199396SMatthew.Ahrens@Sun.COM 39209396SMatthew.Ahrens@Sun.COM error = dmu_objset_userspace_upgrade(os); 392110298SMatthew.Ahrens@Sun.COM dmu_objset_rele(os, FTAG); 39229396SMatthew.Ahrens@Sun.COM } 39239396SMatthew.Ahrens@Sun.COM 39249396SMatthew.Ahrens@Sun.COM return (error); 39259396SMatthew.Ahrens@Sun.COM } 39269396SMatthew.Ahrens@Sun.COM 39279396SMatthew.Ahrens@Sun.COM /* 39284543Smarks * We don't want to have a hard dependency 39294543Smarks * against some special symbols in sharefs 39305331Samw * nfs, and smbsrv. Determine them if needed when 39314543Smarks * the first file system is shared. 39325331Samw * Neither sharefs, nfs or smbsrv are unloadable modules. 39334543Smarks */ 39345331Samw int (*znfsexport_fs)(void *arg); 39354543Smarks int (*zshare_fs)(enum sharefs_sys_op, share_t *, uint32_t); 39365331Samw int (*zsmbexport_fs)(void *arg, boolean_t add_share); 39375331Samw 39385331Samw int zfs_nfsshare_inited; 39395331Samw int zfs_smbshare_inited; 39405331Samw 39414543Smarks ddi_modhandle_t nfs_mod; 39424543Smarks ddi_modhandle_t sharefs_mod; 39435331Samw ddi_modhandle_t smbsrv_mod; 39444543Smarks kmutex_t zfs_share_lock; 39454543Smarks 39464543Smarks static int 39475331Samw zfs_init_sharefs() 39485331Samw { 39495331Samw int error; 39505331Samw 39515331Samw ASSERT(MUTEX_HELD(&zfs_share_lock)); 39525331Samw /* Both NFS and SMB shares also require sharetab support. */ 39535331Samw if (sharefs_mod == NULL && ((sharefs_mod = 39545331Samw ddi_modopen("fs/sharefs", 39555331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 39565331Samw return (ENOSYS); 39575331Samw } 39585331Samw if (zshare_fs == NULL && ((zshare_fs = 39595331Samw (int (*)(enum sharefs_sys_op, share_t *, uint32_t)) 39605331Samw ddi_modsym(sharefs_mod, "sharefs_impl", &error)) == NULL)) { 39615331Samw return (ENOSYS); 39625331Samw } 39635331Samw return (0); 39645331Samw } 39655331Samw 39665331Samw static int 39674543Smarks zfs_ioc_share(zfs_cmd_t *zc) 39684543Smarks { 39694543Smarks int error; 39704543Smarks int opcode; 39714543Smarks 39725331Samw switch (zc->zc_share.z_sharetype) { 39735331Samw case ZFS_SHARE_NFS: 39745331Samw case ZFS_UNSHARE_NFS: 39755331Samw if (zfs_nfsshare_inited == 0) { 39765331Samw mutex_enter(&zfs_share_lock); 39775331Samw if (nfs_mod == NULL && ((nfs_mod = ddi_modopen("fs/nfs", 39785331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 39795331Samw mutex_exit(&zfs_share_lock); 39805331Samw return (ENOSYS); 39815331Samw } 39825331Samw if (znfsexport_fs == NULL && 39835331Samw ((znfsexport_fs = (int (*)(void *)) 39845331Samw ddi_modsym(nfs_mod, 39855331Samw "nfs_export", &error)) == NULL)) { 39865331Samw mutex_exit(&zfs_share_lock); 39875331Samw return (ENOSYS); 39885331Samw } 39895331Samw error = zfs_init_sharefs(); 39905331Samw if (error) { 39915331Samw mutex_exit(&zfs_share_lock); 39925331Samw return (ENOSYS); 39935331Samw } 39945331Samw zfs_nfsshare_inited = 1; 39954543Smarks mutex_exit(&zfs_share_lock); 39964543Smarks } 39975331Samw break; 39985331Samw case ZFS_SHARE_SMB: 39995331Samw case ZFS_UNSHARE_SMB: 40005331Samw if (zfs_smbshare_inited == 0) { 40015331Samw mutex_enter(&zfs_share_lock); 40025331Samw if (smbsrv_mod == NULL && ((smbsrv_mod = 40035331Samw ddi_modopen("drv/smbsrv", 40045331Samw KRTLD_MODE_FIRST, &error)) == NULL)) { 40055331Samw mutex_exit(&zfs_share_lock); 40065331Samw return (ENOSYS); 40075331Samw } 40085331Samw if (zsmbexport_fs == NULL && ((zsmbexport_fs = 40095331Samw (int (*)(void *, boolean_t))ddi_modsym(smbsrv_mod, 40106139Sjb150015 "smb_server_share", &error)) == NULL)) { 40115331Samw mutex_exit(&zfs_share_lock); 40125331Samw return (ENOSYS); 40135331Samw } 40145331Samw error = zfs_init_sharefs(); 40155331Samw if (error) { 40165331Samw mutex_exit(&zfs_share_lock); 40175331Samw return (ENOSYS); 40185331Samw } 40195331Samw zfs_smbshare_inited = 1; 40204543Smarks mutex_exit(&zfs_share_lock); 40214543Smarks } 40225331Samw break; 40235331Samw default: 40245331Samw return (EINVAL); 40254543Smarks } 40264543Smarks 40275331Samw switch (zc->zc_share.z_sharetype) { 40285331Samw case ZFS_SHARE_NFS: 40295331Samw case ZFS_UNSHARE_NFS: 40305331Samw if (error = 40315331Samw znfsexport_fs((void *) 40325331Samw (uintptr_t)zc->zc_share.z_exportdata)) 40335331Samw return (error); 40345331Samw break; 40355331Samw case ZFS_SHARE_SMB: 40365331Samw case ZFS_UNSHARE_SMB: 40375331Samw if (error = zsmbexport_fs((void *) 40385331Samw (uintptr_t)zc->zc_share.z_exportdata, 40395331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB ? 40408845Samw@Sun.COM B_TRUE: B_FALSE)) { 40415331Samw return (error); 40425331Samw } 40435331Samw break; 40445331Samw } 40455331Samw 40465331Samw opcode = (zc->zc_share.z_sharetype == ZFS_SHARE_NFS || 40475331Samw zc->zc_share.z_sharetype == ZFS_SHARE_SMB) ? 40484543Smarks SHAREFS_ADD : SHAREFS_REMOVE; 40494543Smarks 40505331Samw /* 40515331Samw * Add or remove share from sharetab 40525331Samw */ 40534543Smarks error = zshare_fs(opcode, 40544543Smarks (void *)(uintptr_t)zc->zc_share.z_sharedata, 40554543Smarks zc->zc_share.z_sharemax); 40564543Smarks 40574543Smarks return (error); 40584543Smarks 40594543Smarks } 40604543Smarks 40618845Samw@Sun.COM ace_t full_access[] = { 40628845Samw@Sun.COM {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} 40638845Samw@Sun.COM }; 40648845Samw@Sun.COM 40658845Samw@Sun.COM /* 40668845Samw@Sun.COM * Remove all ACL files in shares dir 40678845Samw@Sun.COM */ 40688845Samw@Sun.COM static int 40698845Samw@Sun.COM zfs_smb_acl_purge(znode_t *dzp) 40708845Samw@Sun.COM { 40718845Samw@Sun.COM zap_cursor_t zc; 40728845Samw@Sun.COM zap_attribute_t zap; 40738845Samw@Sun.COM zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 40748845Samw@Sun.COM int error; 40758845Samw@Sun.COM 40768845Samw@Sun.COM for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 40778845Samw@Sun.COM (error = zap_cursor_retrieve(&zc, &zap)) == 0; 40788845Samw@Sun.COM zap_cursor_advance(&zc)) { 40798845Samw@Sun.COM if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, 40808845Samw@Sun.COM NULL, 0)) != 0) 40818845Samw@Sun.COM break; 40828845Samw@Sun.COM } 40838845Samw@Sun.COM zap_cursor_fini(&zc); 40848845Samw@Sun.COM return (error); 40858845Samw@Sun.COM } 40868845Samw@Sun.COM 40878845Samw@Sun.COM static int 40888845Samw@Sun.COM zfs_ioc_smb_acl(zfs_cmd_t *zc) 40898845Samw@Sun.COM { 40908845Samw@Sun.COM vnode_t *vp; 40918845Samw@Sun.COM znode_t *dzp; 40928845Samw@Sun.COM vnode_t *resourcevp = NULL; 40938845Samw@Sun.COM znode_t *sharedir; 40948845Samw@Sun.COM zfsvfs_t *zfsvfs; 40958845Samw@Sun.COM nvlist_t *nvlist; 40968845Samw@Sun.COM char *src, *target; 40978845Samw@Sun.COM vattr_t vattr; 40988845Samw@Sun.COM vsecattr_t vsec; 40998845Samw@Sun.COM int error = 0; 41008845Samw@Sun.COM 41018845Samw@Sun.COM if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, 41028845Samw@Sun.COM NO_FOLLOW, NULL, &vp)) != 0) 41038845Samw@Sun.COM return (error); 41048845Samw@Sun.COM 41058845Samw@Sun.COM /* Now make sure mntpnt and dataset are ZFS */ 41068845Samw@Sun.COM 41078845Samw@Sun.COM if (vp->v_vfsp->vfs_fstype != zfsfstype || 41088845Samw@Sun.COM (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), 41098845Samw@Sun.COM zc->zc_name) != 0)) { 41108845Samw@Sun.COM VN_RELE(vp); 41118845Samw@Sun.COM return (EINVAL); 41128845Samw@Sun.COM } 41138845Samw@Sun.COM 41148845Samw@Sun.COM dzp = VTOZ(vp); 41158845Samw@Sun.COM zfsvfs = dzp->z_zfsvfs; 41168845Samw@Sun.COM ZFS_ENTER(zfsvfs); 41178845Samw@Sun.COM 41189030SMark.Shellenbaum@Sun.COM /* 41199030SMark.Shellenbaum@Sun.COM * Create share dir if its missing. 41209030SMark.Shellenbaum@Sun.COM */ 41219030SMark.Shellenbaum@Sun.COM mutex_enter(&zfsvfs->z_lock); 41229030SMark.Shellenbaum@Sun.COM if (zfsvfs->z_shares_dir == 0) { 41239030SMark.Shellenbaum@Sun.COM dmu_tx_t *tx; 41249030SMark.Shellenbaum@Sun.COM 41259030SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(zfsvfs->z_os); 41269030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, 41279030SMark.Shellenbaum@Sun.COM ZFS_SHARES_DIR); 41289030SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 41299030SMark.Shellenbaum@Sun.COM error = dmu_tx_assign(tx, TXG_WAIT); 41309030SMark.Shellenbaum@Sun.COM if (error) { 41319030SMark.Shellenbaum@Sun.COM dmu_tx_abort(tx); 41329030SMark.Shellenbaum@Sun.COM } else { 41339030SMark.Shellenbaum@Sun.COM error = zfs_create_share_dir(zfsvfs, tx); 41349030SMark.Shellenbaum@Sun.COM dmu_tx_commit(tx); 41359030SMark.Shellenbaum@Sun.COM } 41369030SMark.Shellenbaum@Sun.COM if (error) { 41379030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 41389030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 41399030SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 41409030SMark.Shellenbaum@Sun.COM return (error); 41419030SMark.Shellenbaum@Sun.COM } 41429030SMark.Shellenbaum@Sun.COM } 41439030SMark.Shellenbaum@Sun.COM mutex_exit(&zfsvfs->z_lock); 41449030SMark.Shellenbaum@Sun.COM 41459030SMark.Shellenbaum@Sun.COM ASSERT(zfsvfs->z_shares_dir); 41468845Samw@Sun.COM if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { 41479030SMark.Shellenbaum@Sun.COM VN_RELE(vp); 41488845Samw@Sun.COM ZFS_EXIT(zfsvfs); 41498845Samw@Sun.COM return (error); 41508845Samw@Sun.COM } 41518845Samw@Sun.COM 41528845Samw@Sun.COM switch (zc->zc_cookie) { 41538845Samw@Sun.COM case ZFS_SMB_ACL_ADD: 41548845Samw@Sun.COM vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 41558845Samw@Sun.COM vattr.va_type = VREG; 41568845Samw@Sun.COM vattr.va_mode = S_IFREG|0777; 41578845Samw@Sun.COM vattr.va_uid = 0; 41588845Samw@Sun.COM vattr.va_gid = 0; 41598845Samw@Sun.COM 41608845Samw@Sun.COM vsec.vsa_mask = VSA_ACE; 41618845Samw@Sun.COM vsec.vsa_aclentp = &full_access; 41628845Samw@Sun.COM vsec.vsa_aclentsz = sizeof (full_access); 41638845Samw@Sun.COM vsec.vsa_aclcnt = 1; 41648845Samw@Sun.COM 41658845Samw@Sun.COM error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, 41668845Samw@Sun.COM &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); 41678845Samw@Sun.COM if (resourcevp) 41688845Samw@Sun.COM VN_RELE(resourcevp); 41698845Samw@Sun.COM break; 41708845Samw@Sun.COM 41718845Samw@Sun.COM case ZFS_SMB_ACL_REMOVE: 41728845Samw@Sun.COM error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, 41738845Samw@Sun.COM NULL, 0); 41748845Samw@Sun.COM break; 41758845Samw@Sun.COM 41768845Samw@Sun.COM case ZFS_SMB_ACL_RENAME: 41778845Samw@Sun.COM if ((error = get_nvlist(zc->zc_nvlist_src, 41789643SEric.Taylor@Sun.COM zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { 41798845Samw@Sun.COM VN_RELE(vp); 41808845Samw@Sun.COM ZFS_EXIT(zfsvfs); 41818845Samw@Sun.COM return (error); 41828845Samw@Sun.COM } 41838845Samw@Sun.COM if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || 41848845Samw@Sun.COM nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, 41858845Samw@Sun.COM &target)) { 41868845Samw@Sun.COM VN_RELE(vp); 41879179SMark.Shellenbaum@Sun.COM VN_RELE(ZTOV(sharedir)); 41888845Samw@Sun.COM ZFS_EXIT(zfsvfs); 418911422SMark.Musante@Sun.COM nvlist_free(nvlist); 41908845Samw@Sun.COM return (error); 41918845Samw@Sun.COM } 41928845Samw@Sun.COM error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, 41938845Samw@Sun.COM kcred, NULL, 0); 41948845Samw@Sun.COM nvlist_free(nvlist); 41958845Samw@Sun.COM break; 41968845Samw@Sun.COM 41978845Samw@Sun.COM case ZFS_SMB_ACL_PURGE: 41988845Samw@Sun.COM error = zfs_smb_acl_purge(sharedir); 41998845Samw@Sun.COM break; 42008845Samw@Sun.COM 42018845Samw@Sun.COM default: 42028845Samw@Sun.COM error = EINVAL; 42038845Samw@Sun.COM break; 42048845Samw@Sun.COM } 42058845Samw@Sun.COM 42068845Samw@Sun.COM VN_RELE(vp); 42078845Samw@Sun.COM VN_RELE(ZTOV(sharedir)); 42088845Samw@Sun.COM 42098845Samw@Sun.COM ZFS_EXIT(zfsvfs); 42108845Samw@Sun.COM 42118845Samw@Sun.COM return (error); 42128845Samw@Sun.COM } 42138845Samw@Sun.COM 42144543Smarks /* 421510242Schris.kirby@sun.com * inputs: 421610242Schris.kirby@sun.com * zc_name name of filesystem 421710242Schris.kirby@sun.com * zc_value short name of snap 421810242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 421910242Schris.kirby@sun.com * zc_cookie recursive flag 422010342Schris.kirby@sun.com * zc_temphold set if hold is temporary 422110242Schris.kirby@sun.com * 422210242Schris.kirby@sun.com * outputs: none 422310242Schris.kirby@sun.com */ 422410242Schris.kirby@sun.com static int 422510242Schris.kirby@sun.com zfs_ioc_hold(zfs_cmd_t *zc) 422610242Schris.kirby@sun.com { 422710242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 422810242Schris.kirby@sun.com 422910242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 423010242Schris.kirby@sun.com return (EINVAL); 423110242Schris.kirby@sun.com 423210242Schris.kirby@sun.com return (dsl_dataset_user_hold(zc->zc_name, zc->zc_value, 423310342Schris.kirby@sun.com zc->zc_string, recursive, zc->zc_temphold)); 423410242Schris.kirby@sun.com } 423510242Schris.kirby@sun.com 423610242Schris.kirby@sun.com /* 423710242Schris.kirby@sun.com * inputs: 423810242Schris.kirby@sun.com * zc_name name of dataset from which we're releasing a user reference 423910242Schris.kirby@sun.com * zc_value short name of snap 424010242Schris.kirby@sun.com * zc_string user-supplied tag for this reference 424110242Schris.kirby@sun.com * zc_cookie recursive flag 424210242Schris.kirby@sun.com * 424310242Schris.kirby@sun.com * outputs: none 424410242Schris.kirby@sun.com */ 424510242Schris.kirby@sun.com static int 424610242Schris.kirby@sun.com zfs_ioc_release(zfs_cmd_t *zc) 424710242Schris.kirby@sun.com { 424810242Schris.kirby@sun.com boolean_t recursive = zc->zc_cookie; 424910242Schris.kirby@sun.com 425010242Schris.kirby@sun.com if (snapshot_namecheck(zc->zc_value, NULL, NULL) != 0) 425110242Schris.kirby@sun.com return (EINVAL); 425210242Schris.kirby@sun.com 425310242Schris.kirby@sun.com return (dsl_dataset_user_release(zc->zc_name, zc->zc_value, 425410242Schris.kirby@sun.com zc->zc_string, recursive)); 425510242Schris.kirby@sun.com } 425610242Schris.kirby@sun.com 425710242Schris.kirby@sun.com /* 425810242Schris.kirby@sun.com * inputs: 425910242Schris.kirby@sun.com * zc_name name of filesystem 426010242Schris.kirby@sun.com * 426110242Schris.kirby@sun.com * outputs: 426210242Schris.kirby@sun.com * zc_nvlist_src{_size} nvlist of snapshot holds 426310242Schris.kirby@sun.com */ 426410242Schris.kirby@sun.com static int 426510242Schris.kirby@sun.com zfs_ioc_get_holds(zfs_cmd_t *zc) 426610242Schris.kirby@sun.com { 426710242Schris.kirby@sun.com nvlist_t *nvp; 426810242Schris.kirby@sun.com int error; 426910242Schris.kirby@sun.com 427010242Schris.kirby@sun.com if ((error = dsl_dataset_get_holds(zc->zc_name, &nvp)) == 0) { 427110242Schris.kirby@sun.com error = put_nvlist(zc, nvp); 427210242Schris.kirby@sun.com nvlist_free(nvp); 427310242Schris.kirby@sun.com } 427410242Schris.kirby@sun.com 427510242Schris.kirby@sun.com return (error); 427610242Schris.kirby@sun.com } 427710242Schris.kirby@sun.com 427810242Schris.kirby@sun.com /* 42794988Sek110237 * pool create, destroy, and export don't log the history as part of 42804988Sek110237 * zfsdev_ioctl, but rather zfs_ioc_pool_create, and zfs_ioc_pool_export 42814988Sek110237 * do the logging of those commands. 42824543Smarks */ 4283789Sahrens static zfs_ioc_vec_t zfs_ioc_vec[] = { 42849234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_create, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42859234SGeorge.Wilson@Sun.COM B_FALSE }, 42869234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_destroy, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42879234SGeorge.Wilson@Sun.COM B_FALSE }, 42889234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_import, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42899234SGeorge.Wilson@Sun.COM B_FALSE }, 42909234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_export, zfs_secpolicy_config, POOL_NAME, B_FALSE, 42919234SGeorge.Wilson@Sun.COM B_FALSE }, 42929234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_configs, zfs_secpolicy_none, NO_NAME, B_FALSE, 42939234SGeorge.Wilson@Sun.COM B_FALSE }, 42949234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_stats, zfs_secpolicy_read, POOL_NAME, B_FALSE, 42959234SGeorge.Wilson@Sun.COM B_FALSE }, 42969234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_tryimport, zfs_secpolicy_config, NO_NAME, B_FALSE, 42979234SGeorge.Wilson@Sun.COM B_FALSE }, 42989234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_scrub, zfs_secpolicy_config, POOL_NAME, B_TRUE, 42999234SGeorge.Wilson@Sun.COM B_TRUE }, 43009234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_freeze, zfs_secpolicy_config, NO_NAME, B_FALSE, 43019234SGeorge.Wilson@Sun.COM B_FALSE }, 43029234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_upgrade, zfs_secpolicy_config, POOL_NAME, B_TRUE, 43039234SGeorge.Wilson@Sun.COM B_TRUE }, 43049234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_history, zfs_secpolicy_config, POOL_NAME, B_FALSE, 43059234SGeorge.Wilson@Sun.COM B_FALSE }, 43069234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_add, zfs_secpolicy_config, POOL_NAME, B_TRUE, 43079234SGeorge.Wilson@Sun.COM B_TRUE }, 43089234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_remove, zfs_secpolicy_config, POOL_NAME, B_TRUE, 43099234SGeorge.Wilson@Sun.COM B_TRUE }, 43109234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_set_state, zfs_secpolicy_config, POOL_NAME, B_TRUE, 43119234SGeorge.Wilson@Sun.COM B_FALSE }, 43129234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_attach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 43139234SGeorge.Wilson@Sun.COM B_TRUE }, 43149234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_detach, zfs_secpolicy_config, POOL_NAME, B_TRUE, 43159234SGeorge.Wilson@Sun.COM B_TRUE }, 43169234SGeorge.Wilson@Sun.COM { zfs_ioc_vdev_setpath, zfs_secpolicy_config, POOL_NAME, B_FALSE, 43179234SGeorge.Wilson@Sun.COM B_TRUE }, 43189425SEric.Schrock@Sun.COM { zfs_ioc_vdev_setfru, zfs_secpolicy_config, POOL_NAME, B_FALSE, 43199425SEric.Schrock@Sun.COM B_TRUE }, 43209234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_stats, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 432111454SSanjeev.Bagewadi@Sun.COM B_TRUE }, 43229234SGeorge.Wilson@Sun.COM { zfs_ioc_objset_zplprops, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 43239234SGeorge.Wilson@Sun.COM B_FALSE }, 43249234SGeorge.Wilson@Sun.COM { zfs_ioc_dataset_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 432511454SSanjeev.Bagewadi@Sun.COM B_TRUE }, 43269234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot_list_next, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 432711454SSanjeev.Bagewadi@Sun.COM B_TRUE }, 43289234SGeorge.Wilson@Sun.COM { zfs_ioc_set_prop, zfs_secpolicy_none, DATASET_NAME, B_TRUE, B_TRUE }, 43299234SGeorge.Wilson@Sun.COM { zfs_ioc_create, zfs_secpolicy_create, DATASET_NAME, B_TRUE, B_TRUE }, 43309234SGeorge.Wilson@Sun.COM { zfs_ioc_destroy, zfs_secpolicy_destroy, DATASET_NAME, B_TRUE, 43319234SGeorge.Wilson@Sun.COM B_TRUE}, 43329234SGeorge.Wilson@Sun.COM { zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, B_TRUE, 43339234SGeorge.Wilson@Sun.COM B_TRUE }, 43349234SGeorge.Wilson@Sun.COM { zfs_ioc_rename, zfs_secpolicy_rename, DATASET_NAME, B_TRUE, B_TRUE }, 43359234SGeorge.Wilson@Sun.COM { zfs_ioc_recv, zfs_secpolicy_receive, DATASET_NAME, B_TRUE, B_TRUE }, 43369234SGeorge.Wilson@Sun.COM { zfs_ioc_send, zfs_secpolicy_send, DATASET_NAME, B_TRUE, B_FALSE }, 43379234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 43389234SGeorge.Wilson@Sun.COM B_FALSE }, 43399234SGeorge.Wilson@Sun.COM { zfs_ioc_clear_fault, zfs_secpolicy_inject, NO_NAME, B_FALSE, 43409234SGeorge.Wilson@Sun.COM B_FALSE }, 43419234SGeorge.Wilson@Sun.COM { zfs_ioc_inject_list_next, zfs_secpolicy_inject, NO_NAME, B_FALSE, 43429234SGeorge.Wilson@Sun.COM B_FALSE }, 43439234SGeorge.Wilson@Sun.COM { zfs_ioc_error_log, zfs_secpolicy_inject, POOL_NAME, B_FALSE, 43449234SGeorge.Wilson@Sun.COM B_FALSE }, 43459234SGeorge.Wilson@Sun.COM { zfs_ioc_clear, zfs_secpolicy_config, POOL_NAME, B_TRUE, B_FALSE }, 43469234SGeorge.Wilson@Sun.COM { zfs_ioc_promote, zfs_secpolicy_promote, DATASET_NAME, B_TRUE, 43479234SGeorge.Wilson@Sun.COM B_TRUE }, 434811314Swilliam.gorrell@sun.com { zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, DATASET_NAME, 434911314Swilliam.gorrell@sun.com B_TRUE, B_TRUE }, 43509234SGeorge.Wilson@Sun.COM { zfs_ioc_snapshot, zfs_secpolicy_snapshot, DATASET_NAME, B_TRUE, 43519234SGeorge.Wilson@Sun.COM B_TRUE }, 43529234SGeorge.Wilson@Sun.COM { zfs_ioc_dsobj_to_dsname, zfs_secpolicy_config, POOL_NAME, B_FALSE, 43539234SGeorge.Wilson@Sun.COM B_FALSE }, 435410233SGeorge.Wilson@Sun.COM { zfs_ioc_obj_to_path, zfs_secpolicy_config, DATASET_NAME, B_FALSE, 435510233SGeorge.Wilson@Sun.COM B_TRUE }, 43569234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_set_props, zfs_secpolicy_config, POOL_NAME, B_TRUE, 43579234SGeorge.Wilson@Sun.COM B_TRUE }, 43589234SGeorge.Wilson@Sun.COM { zfs_ioc_pool_get_props, zfs_secpolicy_read, POOL_NAME, B_FALSE, 43599234SGeorge.Wilson@Sun.COM B_FALSE }, 43609234SGeorge.Wilson@Sun.COM { zfs_ioc_set_fsacl, zfs_secpolicy_fsacl, DATASET_NAME, B_TRUE, 43619234SGeorge.Wilson@Sun.COM B_TRUE }, 43629234SGeorge.Wilson@Sun.COM { zfs_ioc_get_fsacl, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 43639234SGeorge.Wilson@Sun.COM B_FALSE }, 43649234SGeorge.Wilson@Sun.COM { zfs_ioc_iscsi_perm_check, zfs_secpolicy_iscsi, DATASET_NAME, B_FALSE, 43659234SGeorge.Wilson@Sun.COM B_FALSE }, 43669234SGeorge.Wilson@Sun.COM { zfs_ioc_share, zfs_secpolicy_share, DATASET_NAME, B_FALSE, B_FALSE }, 43679234SGeorge.Wilson@Sun.COM { zfs_ioc_inherit_prop, zfs_secpolicy_inherit, DATASET_NAME, B_TRUE, 43689234SGeorge.Wilson@Sun.COM B_TRUE }, 43699234SGeorge.Wilson@Sun.COM { zfs_ioc_smb_acl, zfs_secpolicy_smb_acl, DATASET_NAME, B_FALSE, 43709396SMatthew.Ahrens@Sun.COM B_FALSE }, 43719396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_one, zfs_secpolicy_userspace_one, 43729396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 43739396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_many, zfs_secpolicy_userspace_many, 43749396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_FALSE }, 43759396SMatthew.Ahrens@Sun.COM { zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, 43769396SMatthew.Ahrens@Sun.COM DATASET_NAME, B_FALSE, B_TRUE }, 437710242Schris.kirby@sun.com { zfs_ioc_hold, zfs_secpolicy_hold, DATASET_NAME, B_TRUE, B_TRUE }, 437810242Schris.kirby@sun.com { zfs_ioc_release, zfs_secpolicy_release, DATASET_NAME, B_TRUE, 437910242Schris.kirby@sun.com B_TRUE }, 438010242Schris.kirby@sun.com { zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 438111022STom.Erickson@Sun.COM B_TRUE }, 438211022STom.Erickson@Sun.COM { zfs_ioc_objset_recvd_props, zfs_secpolicy_read, DATASET_NAME, B_FALSE, 438311422SMark.Musante@Sun.COM B_FALSE }, 438411422SMark.Musante@Sun.COM { zfs_ioc_vdev_split, zfs_secpolicy_config, POOL_NAME, B_TRUE, 438511422SMark.Musante@Sun.COM B_TRUE } 4386789Sahrens }; 4387789Sahrens 43889234SGeorge.Wilson@Sun.COM int 43899234SGeorge.Wilson@Sun.COM pool_status_check(const char *name, zfs_ioc_namecheck_t type) 43909234SGeorge.Wilson@Sun.COM { 43919234SGeorge.Wilson@Sun.COM spa_t *spa; 43929234SGeorge.Wilson@Sun.COM int error; 43939234SGeorge.Wilson@Sun.COM 43949234SGeorge.Wilson@Sun.COM ASSERT(type == POOL_NAME || type == DATASET_NAME); 43959234SGeorge.Wilson@Sun.COM 43969396SMatthew.Ahrens@Sun.COM error = spa_open(name, &spa, FTAG); 43979234SGeorge.Wilson@Sun.COM if (error == 0) { 43989234SGeorge.Wilson@Sun.COM if (spa_suspended(spa)) 43999234SGeorge.Wilson@Sun.COM error = EAGAIN; 44009234SGeorge.Wilson@Sun.COM spa_close(spa, FTAG); 44019234SGeorge.Wilson@Sun.COM } 44029234SGeorge.Wilson@Sun.COM return (error); 44039234SGeorge.Wilson@Sun.COM } 44049234SGeorge.Wilson@Sun.COM 4405789Sahrens static int 4406789Sahrens zfsdev_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 4407789Sahrens { 4408789Sahrens zfs_cmd_t *zc; 4409789Sahrens uint_t vec; 44102199Sahrens int error, rc; 4411789Sahrens 4412789Sahrens if (getminor(dev) != 0) 4413789Sahrens return (zvol_ioctl(dev, cmd, arg, flag, cr, rvalp)); 4414789Sahrens 4415789Sahrens vec = cmd - ZFS_IOC; 44164787Sahrens ASSERT3U(getmajor(dev), ==, ddi_driver_major(zfs_dip)); 4417789Sahrens 4418789Sahrens if (vec >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) 4419789Sahrens return (EINVAL); 4420789Sahrens 4421789Sahrens zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); 4422789Sahrens 44239643SEric.Taylor@Sun.COM error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); 4424*11807SSam.Falkner@Sun.COM if (error != 0) 4425*11807SSam.Falkner@Sun.COM error = EFAULT; 4426789Sahrens 442710588SEric.Taylor@Sun.COM if ((error == 0) && !(flag & FKIOCTL)) 44284543Smarks error = zfs_ioc_vec[vec].zvec_secpolicy(zc, cr); 4429789Sahrens 4430789Sahrens /* 4431789Sahrens * Ensure that all pool/dataset names are valid before we pass down to 4432789Sahrens * the lower layers. 4433789Sahrens */ 4434789Sahrens if (error == 0) { 4435789Sahrens zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; 44369643SEric.Taylor@Sun.COM zc->zc_iflags = flag & FKIOCTL; 4437789Sahrens switch (zfs_ioc_vec[vec].zvec_namecheck) { 44384577Sahrens case POOL_NAME: 4439789Sahrens if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) 4440789Sahrens error = EINVAL; 44419234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 44429234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 44439234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 4444789Sahrens break; 4445789Sahrens 44464577Sahrens case DATASET_NAME: 4447789Sahrens if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) 4448789Sahrens error = EINVAL; 44499234SGeorge.Wilson@Sun.COM if (zfs_ioc_vec[vec].zvec_pool_check) 44509234SGeorge.Wilson@Sun.COM error = pool_status_check(zc->zc_name, 44519234SGeorge.Wilson@Sun.COM zfs_ioc_vec[vec].zvec_namecheck); 4452789Sahrens break; 44532856Snd150628 44544577Sahrens case NO_NAME: 44552856Snd150628 break; 4456789Sahrens } 4457789Sahrens } 4458789Sahrens 4459789Sahrens if (error == 0) 4460789Sahrens error = zfs_ioc_vec[vec].zvec_func(zc); 4461789Sahrens 44629643SEric.Taylor@Sun.COM rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); 44634543Smarks if (error == 0) { 4464*11807SSam.Falkner@Sun.COM if (rc != 0) 4465*11807SSam.Falkner@Sun.COM error = EFAULT; 44669396SMatthew.Ahrens@Sun.COM if (zfs_ioc_vec[vec].zvec_his_log) 44674543Smarks zfs_log_history(zc); 44684543Smarks } 4469789Sahrens 4470789Sahrens kmem_free(zc, sizeof (zfs_cmd_t)); 4471789Sahrens return (error); 4472789Sahrens } 4473789Sahrens 4474789Sahrens static int 4475789Sahrens zfs_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 4476789Sahrens { 4477789Sahrens if (cmd != DDI_ATTACH) 4478789Sahrens return (DDI_FAILURE); 4479789Sahrens 4480789Sahrens if (ddi_create_minor_node(dip, "zfs", S_IFCHR, 0, 4481789Sahrens DDI_PSEUDO, 0) == DDI_FAILURE) 4482789Sahrens return (DDI_FAILURE); 4483789Sahrens 4484789Sahrens zfs_dip = dip; 4485789Sahrens 4486789Sahrens ddi_report_dev(dip); 4487789Sahrens 4488789Sahrens return (DDI_SUCCESS); 4489789Sahrens } 4490789Sahrens 4491789Sahrens static int 4492789Sahrens zfs_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 4493789Sahrens { 4494789Sahrens if (spa_busy() || zfs_busy() || zvol_busy()) 4495789Sahrens return (DDI_FAILURE); 4496789Sahrens 4497789Sahrens if (cmd != DDI_DETACH) 4498789Sahrens return (DDI_FAILURE); 4499789Sahrens 4500789Sahrens zfs_dip = NULL; 4501789Sahrens 4502789Sahrens ddi_prop_remove_all(dip); 4503789Sahrens ddi_remove_minor_node(dip, NULL); 4504789Sahrens 4505789Sahrens return (DDI_SUCCESS); 4506789Sahrens } 4507789Sahrens 4508789Sahrens /*ARGSUSED*/ 4509789Sahrens static int 4510789Sahrens zfs_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 4511789Sahrens { 4512789Sahrens switch (infocmd) { 4513789Sahrens case DDI_INFO_DEVT2DEVINFO: 4514789Sahrens *result = zfs_dip; 4515789Sahrens return (DDI_SUCCESS); 4516789Sahrens 4517789Sahrens case DDI_INFO_DEVT2INSTANCE: 4518849Sbonwick *result = (void *)0; 4519789Sahrens return (DDI_SUCCESS); 4520789Sahrens } 4521789Sahrens 4522789Sahrens return (DDI_FAILURE); 4523789Sahrens } 4524789Sahrens 4525789Sahrens /* 4526789Sahrens * OK, so this is a little weird. 4527789Sahrens * 4528789Sahrens * /dev/zfs is the control node, i.e. minor 0. 4529789Sahrens * /dev/zvol/[r]dsk/pool/dataset are the zvols, minor > 0. 4530789Sahrens * 4531789Sahrens * /dev/zfs has basically nothing to do except serve up ioctls, 4532789Sahrens * so most of the standard driver entry points are in zvol.c. 4533789Sahrens */ 4534789Sahrens static struct cb_ops zfs_cb_ops = { 4535789Sahrens zvol_open, /* open */ 4536789Sahrens zvol_close, /* close */ 4537789Sahrens zvol_strategy, /* strategy */ 4538789Sahrens nodev, /* print */ 45396423Sgw25295 zvol_dump, /* dump */ 4540789Sahrens zvol_read, /* read */ 4541789Sahrens zvol_write, /* write */ 4542789Sahrens zfsdev_ioctl, /* ioctl */ 4543789Sahrens nodev, /* devmap */ 4544789Sahrens nodev, /* mmap */ 4545789Sahrens nodev, /* segmap */ 4546789Sahrens nochpoll, /* poll */ 4547789Sahrens ddi_prop_op, /* prop_op */ 4548789Sahrens NULL, /* streamtab */ 4549789Sahrens D_NEW | D_MP | D_64BIT, /* Driver compatibility flag */ 4550789Sahrens CB_REV, /* version */ 45513638Sbillm nodev, /* async read */ 45523638Sbillm nodev, /* async write */ 4553789Sahrens }; 4554789Sahrens 4555789Sahrens static struct dev_ops zfs_dev_ops = { 4556789Sahrens DEVO_REV, /* version */ 4557789Sahrens 0, /* refcnt */ 4558789Sahrens zfs_info, /* info */ 4559789Sahrens nulldev, /* identify */ 4560789Sahrens nulldev, /* probe */ 4561789Sahrens zfs_attach, /* attach */ 4562789Sahrens zfs_detach, /* detach */ 4563789Sahrens nodev, /* reset */ 4564789Sahrens &zfs_cb_ops, /* driver operations */ 45657656SSherry.Moore@Sun.COM NULL, /* no bus operations */ 45667656SSherry.Moore@Sun.COM NULL, /* power */ 45677656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */ 4568789Sahrens }; 4569789Sahrens 4570789Sahrens static struct modldrv zfs_modldrv = { 45717656SSherry.Moore@Sun.COM &mod_driverops, 45727656SSherry.Moore@Sun.COM "ZFS storage pool", 45737656SSherry.Moore@Sun.COM &zfs_dev_ops 4574789Sahrens }; 4575789Sahrens 4576789Sahrens static struct modlinkage modlinkage = { 4577789Sahrens MODREV_1, 4578789Sahrens (void *)&zfs_modlfs, 4579789Sahrens (void *)&zfs_modldrv, 4580789Sahrens NULL 4581789Sahrens }; 4582789Sahrens 45834720Sfr157268 45844720Sfr157268 uint_t zfs_fsyncer_key; 45855326Sek110237 extern uint_t rrw_tsd_key; 45864720Sfr157268 4587789Sahrens int 4588789Sahrens _init(void) 4589789Sahrens { 4590789Sahrens int error; 4591789Sahrens 4592849Sbonwick spa_init(FREAD | FWRITE); 4593849Sbonwick zfs_init(); 4594849Sbonwick zvol_init(); 4595849Sbonwick 4596849Sbonwick if ((error = mod_install(&modlinkage)) != 0) { 4597849Sbonwick zvol_fini(); 4598849Sbonwick zfs_fini(); 4599849Sbonwick spa_fini(); 4600789Sahrens return (error); 4601849Sbonwick } 4602789Sahrens 46034720Sfr157268 tsd_create(&zfs_fsyncer_key, NULL); 46045326Sek110237 tsd_create(&rrw_tsd_key, NULL); 46054720Sfr157268 4606789Sahrens error = ldi_ident_from_mod(&modlinkage, &zfs_li); 4607789Sahrens ASSERT(error == 0); 46084543Smarks mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL); 4609789Sahrens 4610789Sahrens return (0); 4611789Sahrens } 4612789Sahrens 4613789Sahrens int 4614789Sahrens _fini(void) 4615789Sahrens { 4616789Sahrens int error; 4617789Sahrens 46181544Seschrock if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled) 4619789Sahrens return (EBUSY); 4620789Sahrens 4621789Sahrens if ((error = mod_remove(&modlinkage)) != 0) 4622789Sahrens return (error); 4623789Sahrens 4624789Sahrens zvol_fini(); 4625789Sahrens zfs_fini(); 4626789Sahrens spa_fini(); 46275331Samw if (zfs_nfsshare_inited) 46284543Smarks (void) ddi_modclose(nfs_mod); 46295331Samw if (zfs_smbshare_inited) 46305331Samw (void) ddi_modclose(smbsrv_mod); 46315331Samw if (zfs_nfsshare_inited || zfs_smbshare_inited) 46324543Smarks (void) ddi_modclose(sharefs_mod); 4633789Sahrens 46344720Sfr157268 tsd_destroy(&zfs_fsyncer_key); 4635789Sahrens ldi_ident_release(zfs_li); 4636789Sahrens zfs_li = NULL; 46374543Smarks mutex_destroy(&zfs_share_lock); 4638789Sahrens 4639789Sahrens return (error); 4640789Sahrens } 4641789Sahrens 4642789Sahrens int 4643789Sahrens _info(struct modinfo *modinfop) 4644789Sahrens { 4645789Sahrens return (mod_info(&modlinkage, modinfop)); 4646789Sahrens } 4647